diff --git a/src/codegen.c b/src/codegen.c index b5e9bf6..6ef9ff2 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -56,6 +56,12 @@ typedef struct StructTypeFunction uint8_t isStatic; } StructTypeFunction; +typedef struct GenericStructFunction +{ + char *name; + Node *functionDeclaration; +} GenericStructFunction; + typedef struct StructTypeDeclaration { char *name; @@ -66,6 +72,9 @@ typedef struct StructTypeDeclaration StructTypeFunction *functions; uint32_t functionCount; + + GenericStructFunction *genericFunctions; + uint32_t genericFunctionCount; } StructTypeDeclaration; StructTypeDeclaration *structTypeDeclarations; @@ -299,6 +308,27 @@ static void DeclareStructFunction( fprintf(stderr, "Could not find struct type for function!\n"); } +static void DeclareGenericFunction( + LLVMTypeRef structPointerType, + Node *functionDeclaration +) { + uint32_t i, index; + + for (i = 0; i < structTypeDeclarationCount; i += 1) + { + if (structTypeDeclarations[i].structPointerType == structPointerType) + { + 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].genericFunctionCount += 1; + + return; + } + } +} + static LLVMTypeRef LookupCustomType(char *name) { uint32_t i; @@ -337,6 +367,12 @@ static LLVMTypeRef ResolveType(Node* typeNode) } } +static LLVMValueRef LookupGenericFunction( + +) { + +} + static LLVMValueRef LookupFunctionByType( LLVMTypeRef structType, char *name, @@ -386,6 +422,14 @@ static LLVMValueRef LookupFunctionByPointerType( return structTypeDeclarations[i].functions[j].function; } } + + for (j = 0; j < structTypeDeclarations[i].genericFunctionCount; j += 1) + { + if (strcmp(structTypeDeclarations[i].genericFunctions[j].name, name) == 0) + { + return LookupGenericFunction() + } + } } } @@ -872,6 +916,12 @@ static void CompileFunction( paramIndex += 1; } + if (functionSignature->children[4]->childCount > 0) + { + DeclareGenericFunction(wStructPointerType, functionDeclaration); + return; + } + PushScopeFrame(scope); /* FIXME: should work for non-primitive types */