start on generic codegen

generics
cosmonaut 2021-05-14 11:00:31 -07:00
parent a529bb6e4f
commit e5e5397e7e
1 changed files with 50 additions and 0 deletions

View File

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