more generic stuff

generics
cosmonaut 2021-05-15 12:47:38 -07:00
parent e5e5397e7e
commit a0efcf548d
2 changed files with 44 additions and 10 deletions

View File

@ -283,7 +283,7 @@ Node* MakeGenericConstraintNode(Node *identifierNode, Node *interfaceNode)
node->childCount = 2; node->childCount = 2;
node->children = (Node**) malloc(sizeof(Node*) * 2); node->children = (Node**) malloc(sizeof(Node*) * 2);
node->children[0] = identifierNode; node->children[0] = identifierNode;
node->children[1] = interfaceNode; node->children[1] = interfaceNode; /* can be NULL */
return node; return node;
} }
@ -627,7 +627,7 @@ TypeTag* MakeTypeTag(Node *node) {
break; break;
default: default:
fprintf(stderr, fprintf(stderr,
"wraith: Attempted to call MakeTypeTag on" "wraith: Attempted to call MakeTypeTag on"
" node with unsupported SyntaxKind: %s\n", " node with unsupported SyntaxKind: %s\n",
SyntaxKindString(node->syntaxKind)); SyntaxKindString(node->syntaxKind));

View File

@ -56,10 +56,22 @@ typedef struct StructTypeFunction
uint8_t isStatic; uint8_t isStatic;
} StructTypeFunction; } StructTypeFunction;
typedef struct MonomorphizedGenericStructFunction
{
TypeTag **typeTags;
LLVMValueRef functionReference;
} MonomorphizedGenericStructFunction;
typedef struct GenericStructFunction typedef struct GenericStructFunction
{ {
char *name; char *name;
Node *functionDeclaration; Node *functionDeclaration;
char **genericTypeNames;
uint32_t genericTypeCount;
MonomorphizedGenericStructFunction *monomorphizedFunctions;
uint32_t monomorphizedFunctionCount;
} GenericStructFunction; } GenericStructFunction;
typedef struct StructTypeDeclaration typedef struct StructTypeDeclaration
@ -312,7 +324,7 @@ static void DeclareGenericFunction(
LLVMTypeRef structPointerType, LLVMTypeRef structPointerType,
Node *functionDeclaration Node *functionDeclaration
) { ) {
uint32_t i, index; uint32_t i, j, index;
for (i = 0; i < structTypeDeclarationCount; i += 1) for (i = 0; i < structTypeDeclarationCount; i += 1)
{ {
@ -320,8 +332,13 @@ static void DeclareGenericFunction(
{ {
index = structTypeDeclarations[i].genericFunctionCount; index = structTypeDeclarations[i].genericFunctionCount;
structTypeDeclarations[i].genericFunctions = realloc(structTypeDeclarations[i].genericFunctions, sizeof(GenericStructFunction) * (structTypeDeclarations[i].genericFunctionCount + 1)); 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[index].name = strdup(functionDeclaration->children[0]->children[0]->value.string);
structTypeDeclarations[i].genericFunctions[i].functionDeclaration = functionDeclaration; 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; structTypeDeclarations[i].genericFunctionCount += 1;
return; return;
@ -368,11 +385,19 @@ static LLVMTypeRef ResolveType(Node* typeNode)
} }
static LLVMValueRef LookupGenericFunction( 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( static LLVMValueRef LookupFunctionByType(
LLVMTypeRef structType, LLVMTypeRef structType,
char *name, char *name,
@ -405,7 +430,9 @@ static LLVMValueRef LookupFunctionByPointerType(
LLVMTypeRef structPointerType, LLVMTypeRef structPointerType,
char *name, char *name,
LLVMTypeRef *pReturnType, LLVMTypeRef *pReturnType,
uint8_t *pStatic uint8_t *pStatic,
TypeTag **argTypes,
uint32_t argCount
) { ) {
uint32_t i, j; uint32_t i, j;
@ -441,9 +468,10 @@ static LLVMValueRef LookupFunctionByInstance(
LLVMValueRef structPointer, LLVMValueRef structPointer,
char *name, char *name,
LLVMTypeRef *pReturnType, 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( static void AddStructVariablesToScope(
@ -542,6 +570,7 @@ static LLVMValueRef CompileFunctionCallExpression(
) { ) {
uint32_t i; uint32_t i;
uint32_t argumentCount = 0; uint32_t argumentCount = 0;
TypeTag *argTypes[expression->children[1]->childCount + 1];
LLVMValueRef args[expression->children[1]->childCount + 1]; LLVMValueRef args[expression->children[1]->childCount + 1];
LLVMValueRef function; LLVMValueRef function;
uint8_t isStatic; uint8_t isStatic;
@ -549,6 +578,11 @@ static LLVMValueRef CompileFunctionCallExpression(
LLVMTypeRef functionReturnType; LLVMTypeRef functionReturnType;
char *returnName = ""; 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 */ /* FIXME: this needs to be recursive on access chains */
if (expression->children[0]->syntaxKind == AccessExpression) if (expression->children[0]->syntaxKind == AccessExpression)
{ {
@ -568,7 +602,7 @@ static LLVMValueRef CompileFunctionCallExpression(
else else
{ {
structInstance = FindVariablePointer(expression->children[0]->children[0]->value.string); 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 else