From a0efcf548d99800963a3dd362d62a687e216c1f0 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Sat, 15 May 2021 12:47:38 -0700 Subject: [PATCH] more generic stuff --- src/ast.c | 4 ++-- src/codegen.c | 50 ++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/ast.c b/src/ast.c index ae6ef36..51c6539 100644 --- a/src/ast.c +++ b/src/ast.c @@ -283,7 +283,7 @@ Node* MakeGenericConstraintNode(Node *identifierNode, Node *interfaceNode) node->childCount = 2; node->children = (Node**) malloc(sizeof(Node*) * 2); node->children[0] = identifierNode; - node->children[1] = interfaceNode; + node->children[1] = interfaceNode; /* can be NULL */ return node; } @@ -627,7 +627,7 @@ TypeTag* MakeTypeTag(Node *node) { break; default: - fprintf(stderr, + fprintf(stderr, "wraith: Attempted to call MakeTypeTag on" " node with unsupported SyntaxKind: %s\n", SyntaxKindString(node->syntaxKind)); diff --git a/src/codegen.c b/src/codegen.c index 6ef9ff2..9292bb0 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -56,10 +56,22 @@ typedef struct StructTypeFunction uint8_t isStatic; } StructTypeFunction; +typedef struct MonomorphizedGenericStructFunction +{ + TypeTag **typeTags; + LLVMValueRef functionReference; +} MonomorphizedGenericStructFunction; + typedef struct GenericStructFunction { char *name; Node *functionDeclaration; + + char **genericTypeNames; + uint32_t genericTypeCount; + + MonomorphizedGenericStructFunction *monomorphizedFunctions; + uint32_t monomorphizedFunctionCount; } GenericStructFunction; typedef struct StructTypeDeclaration @@ -312,7 +324,7 @@ static void DeclareGenericFunction( LLVMTypeRef structPointerType, Node *functionDeclaration ) { - uint32_t i, index; + uint32_t i, j, index; for (i = 0; i < structTypeDeclarationCount; i += 1) { @@ -320,8 +332,13 @@ static void DeclareGenericFunction( { index = structTypeDeclarations[i].genericFunctionCount; structTypeDeclarations[i].genericFunctions = realloc(structTypeDeclarations[i].genericFunctions, sizeof(GenericStructFunction) * (structTypeDeclarations[i].genericFunctionCount + 1)); - structTypeDeclarations[i].genericFunctions[i].name = strdup(functionDeclaration->children[0]->children[0]->value.string); - structTypeDeclarations[i].genericFunctions[i].functionDeclaration = functionDeclaration; + structTypeDeclarations[i].genericFunctions[index].name = strdup(functionDeclaration->children[0]->children[0]->value.string); + structTypeDeclarations[i].genericFunctions[index].functionDeclaration = functionDeclaration; + structTypeDeclarations[i].genericFunctions[index].genericTypeCount = functionDeclaration->children[0]->children[4]->childCount; + for (j = 0; j < structTypeDeclarations[i].genericFunctions[index].genericTypeCount; j += 1) + { + structTypeDeclarations[i].genericFunctions[index].genericTypeNames[j] = strdup(functionDeclaration->children[0]->children[4]->children[j]->children[0]->value.string); + } structTypeDeclarations[i].genericFunctionCount += 1; return; @@ -368,11 +385,19 @@ static LLVMTypeRef ResolveType(Node* typeNode) } static LLVMValueRef LookupGenericFunction( - + StructTypeDeclaration *structDeclaration, + GenericStructFunction *genericFunction, + TypeTag **argTypes, + uint32_t argCount ) { + uint32_t i; + for (i = 0; i < argCount; i += 1) + { + } } +/* FIXME: add generics */ static LLVMValueRef LookupFunctionByType( LLVMTypeRef structType, char *name, @@ -405,7 +430,9 @@ static LLVMValueRef LookupFunctionByPointerType( LLVMTypeRef structPointerType, char *name, LLVMTypeRef *pReturnType, - uint8_t *pStatic + uint8_t *pStatic, + TypeTag **argTypes, + uint32_t argCount ) { uint32_t i, j; @@ -441,9 +468,10 @@ static LLVMValueRef LookupFunctionByInstance( LLVMValueRef structPointer, char *name, LLVMTypeRef *pReturnType, - uint8_t *pStatic + uint8_t *pStatic, + TypeTag **argTypes ) { - return LookupFunctionByPointerType(LLVMTypeOf(structPointer), name, pReturnType, pStatic); + return LookupFunctionByPointerType(LLVMTypeOf(structPointer), name, pReturnType, pStatic, argTypes); } static void AddStructVariablesToScope( @@ -542,6 +570,7 @@ static LLVMValueRef CompileFunctionCallExpression( ) { uint32_t i; uint32_t argumentCount = 0; + TypeTag *argTypes[expression->children[1]->childCount + 1]; LLVMValueRef args[expression->children[1]->childCount + 1]; LLVMValueRef function; uint8_t isStatic; @@ -549,6 +578,11 @@ static LLVMValueRef CompileFunctionCallExpression( LLVMTypeRef functionReturnType; char *returnName = ""; + for (i = 0; i < expression->children[1]->childCount; i += 1) + { + argTypes[i] = expression->children[1]->children[i]->typeTag; + } + /* FIXME: this needs to be recursive on access chains */ if (expression->children[0]->syntaxKind == AccessExpression) { @@ -568,7 +602,7 @@ static LLVMValueRef CompileFunctionCallExpression( else { structInstance = FindVariablePointer(expression->children[0]->children[0]->value.string); - function = LookupFunctionByInstance(structInstance, expression->children[0]->children[1]->value.string, &functionReturnType, &isStatic); + function = LookupFunctionByInstance(structInstance, expression->children[0]->children[1]->value.string, &functionReturnType, &isStatic, argTypes); } } else