skeleton of generic function lookup

traversal
cosmonaut 2021-05-19 18:09:33 -07:00
parent 24bcef6d87
commit 0d94e89045
1 changed files with 76 additions and 0 deletions

View File

@ -411,9 +411,22 @@ static LLVMTypeRef ResolveType(Node *typeNode)
} }
} }
static LLVMValueRef LookupGenericFunction(
StructTypeGenericFunction *genericFunction,
TypeTag **genericArgumentTypes,
uint32_t genericArgumentTypeCount,
LLVMTypeRef *pReturnType,
uint8_t *pStatic)
{
/* TODO: hash the argument types */
/* TODO: compile the monomorphism if doesnt exist */
}
static LLVMValueRef LookupFunctionByType( static LLVMValueRef LookupFunctionByType(
LLVMTypeRef structType, LLVMTypeRef structType,
char *name, char *name,
TypeTag **genericArgumentTypes,
uint32_t genericArgumentTypeCount,
LLVMTypeRef *pReturnType, LLVMTypeRef *pReturnType,
uint8_t *pStatic) uint8_t *pStatic)
{ {
@ -434,6 +447,22 @@ static LLVMValueRef LookupFunctionByType(
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(
&structTypeDeclarations[i].genericFunctions[j],
genericArgumentTypes,
genericArgumentTypeCount,
pReturnType,
pStatic);
}
}
} }
} }
@ -444,6 +473,8 @@ static LLVMValueRef LookupFunctionByType(
static LLVMValueRef LookupFunctionByPointerType( static LLVMValueRef LookupFunctionByPointerType(
LLVMTypeRef structPointerType, LLVMTypeRef structPointerType,
char *name, char *name,
TypeTag **genericArgumentTypes,
uint32_t genericArgumentTypeCount,
LLVMTypeRef *pReturnType, LLVMTypeRef *pReturnType,
uint8_t *pStatic) uint8_t *pStatic)
{ {
@ -464,6 +495,22 @@ 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(
&structTypeDeclarations[i].genericFunctions[j],
genericArgumentTypes,
genericArgumentTypeCount,
pReturnType,
pStatic);
}
}
} }
} }
@ -474,12 +521,16 @@ static LLVMValueRef LookupFunctionByPointerType(
static LLVMValueRef LookupFunctionByInstance( static LLVMValueRef LookupFunctionByInstance(
LLVMValueRef structPointer, LLVMValueRef structPointer,
char *name, char *name,
TypeTag **genericArgumentTypes,
uint32_t genericArgumentTypeCount,
LLVMTypeRef *pReturnType, LLVMTypeRef *pReturnType,
uint8_t *pStatic) uint8_t *pStatic)
{ {
return LookupFunctionByPointerType( return LookupFunctionByPointerType(
LLVMTypeOf(structPointer), LLVMTypeOf(structPointer),
name, name,
genericArgumentTypes,
genericArgumentTypeCount,
pReturnType, pReturnType,
pStatic); pStatic);
} }
@ -591,16 +642,37 @@ static LLVMValueRef CompileFunctionCallExpression(
{ {
uint32_t i; uint32_t i;
uint32_t argumentCount = 0; uint32_t argumentCount = 0;
uint32_t genericArgumentCount = 0;
LLVMValueRef args LLVMValueRef args
[functionCallExpression->functionCallExpression.argumentSequence [functionCallExpression->functionCallExpression.argumentSequence
->functionArgumentSequence.count + ->functionArgumentSequence.count +
1]; 1];
TypeTag *genericArgumentTypes[functionCallExpression->functionCallExpression
.argumentSequence
->functionArgumentSequence.count];
LLVMValueRef function; LLVMValueRef function;
uint8_t isStatic; uint8_t isStatic;
LLVMValueRef structInstance; LLVMValueRef structInstance;
LLVMTypeRef functionReturnType; LLVMTypeRef functionReturnType;
char *returnName = ""; char *returnName = "";
for (i = 0; i < functionCallExpression->functionCallExpression
.argumentSequence->functionArgumentSequence.count;
i += 1)
{
if (functionCallExpression->functionCallExpression.argumentSequence
->functionArgumentSequence.sequence[i]
->syntaxKind == GenericArgument)
{
genericArgumentTypes[genericArgumentCount] =
functionCallExpression->functionCallExpression.argumentSequence
->functionArgumentSequence.sequence[i]
->typeTag;
genericArgumentCount += 1;
}
}
/* FIXME: this needs to be recursive on access chains */ /* FIXME: this needs to be recursive on access chains */
/* FIXME: this needs to be able to call same-struct functions implicitly */ /* FIXME: this needs to be able to call same-struct functions implicitly */
if (functionCallExpression->functionCallExpression.identifier->syntaxKind == if (functionCallExpression->functionCallExpression.identifier->syntaxKind ==
@ -616,6 +688,8 @@ static LLVMValueRef CompileFunctionCallExpression(
typeReference, typeReference,
functionCallExpression->functionCallExpression.identifier functionCallExpression->functionCallExpression.identifier
->accessExpression.accessor->identifier.name, ->accessExpression.accessor->identifier.name,
genericArgumentTypes,
genericArgumentCount,
&functionReturnType, &functionReturnType,
&isStatic); &isStatic);
} }
@ -628,6 +702,8 @@ static LLVMValueRef CompileFunctionCallExpression(
structInstance, structInstance,
functionCallExpression->functionCallExpression.identifier functionCallExpression->functionCallExpression.identifier
->accessExpression.accessor->identifier.name, ->accessExpression.accessor->identifier.name,
genericArgumentTypes,
genericArgumentCount,
&functionReturnType, &functionReturnType,
&isStatic); &isStatic);
} }