basic nested type resolution

generics
cosmonaut 2021-04-24 13:53:21 -07:00
parent b00cde2193
commit 8e7cc00789
1 changed files with 45 additions and 31 deletions

View File

@ -129,6 +129,27 @@ static void AddLocalVariable(Scope *scope, LLVMValueRef pointer, char *name)
scopeFrame->localVariableCount += 1;
}
static LLVMTypeRef WraithTypeToLLVMType(PrimitiveType type)
{
switch (type)
{
case Int:
return LLVMInt64Type();
case UInt:
return LLVMInt64Type();
case Bool:
return LLVMInt1Type();
case Void:
return LLVMVoidType();
}
fprintf(stderr, "Unrecognized type!");
return NULL;
}
static LLVMTypeRef FindStructType(char *name)
{
uint32_t i;
@ -287,6 +308,28 @@ static LLVMTypeRef LookupCustomType(char *name)
return NULL;
}
static LLVMTypeRef ResolveType(Node* typeNode)
{
if (IsPrimitiveType(typeNode))
{
return WraithTypeToLLVMType(typeNode->children[0]->primitiveType);
}
else if (typeNode->children[0]->syntaxKind == CustomTypeNode)
{
char *typeName = typeNode->children[0]->value.string;
return LookupCustomType(typeName);
}
else if (typeNode->children[0]->syntaxKind == ReferenceTypeNode)
{
return LLVMPointerType(ResolveType(typeNode->children[0]->children[0]), 0);
}
else
{
fprintf(stderr, "Unknown type node!\n");
return NULL;
}
}
static LLVMValueRef LookupFunctionByType(
LLVMTypeRef structType,
char *name,
@ -389,27 +432,6 @@ static LLVMValueRef CompileExpression(
Node *binaryExpression
);
static LLVMTypeRef WraithTypeToLLVMType(PrimitiveType type)
{
switch (type)
{
case Int:
return LLVMInt64Type();
case UInt:
return LLVMInt64Type();
case Bool:
return LLVMInt1Type();
case Void:
return LLVMVoidType();
}
fprintf(stderr, "Unrecognized type!");
return NULL;
}
static LLVMValueRef CompileNumber(
Node *numberExpression
) {
@ -588,15 +610,7 @@ static void CompileFunctionVariableDeclaration(LLVMBuilderRef builder, Node *var
char *ptrName = strdup(variableName);
strcat(ptrName, "_ptr");
if (IsPrimitiveType(variableDeclaration->children[0]))
{
variable = LLVMBuildAlloca(builder, WraithTypeToLLVMType(variableDeclaration->children[0]->children[0]->primitiveType), ptrName);
}
else
{
char *customTypeName = variableDeclaration->children[0]->children[0]->value.string;
variable = LLVMBuildAlloca(builder, LookupCustomType(customTypeName), ptrName);
}
variable = LLVMBuildAlloca(builder, ResolveType(variableDeclaration->children[0]), ptrName);
free(ptrName);
@ -751,7 +765,7 @@ static void CompileStruct(LLVMModuleRef module, LLVMContextRef context, Node *no
switch (currentDeclarationNode->syntaxKind)
{
case Declaration: /* this is badly named */
types[fieldCount] = WraithTypeToLLVMType(currentDeclarationNode->children[0]->children[0]->primitiveType);
types[fieldCount] = ResolveType(currentDeclarationNode->children[0]);
fieldDeclarations[fieldCount] = currentDeclarationNode;
fieldCount += 1;
break;