basic nested type resolution

pull/1/head
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; 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) static LLVMTypeRef FindStructType(char *name)
{ {
uint32_t i; uint32_t i;
@ -287,6 +308,28 @@ static LLVMTypeRef LookupCustomType(char *name)
return NULL; 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( static LLVMValueRef LookupFunctionByType(
LLVMTypeRef structType, LLVMTypeRef structType,
char *name, char *name,
@ -389,27 +432,6 @@ static LLVMValueRef CompileExpression(
Node *binaryExpression 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( static LLVMValueRef CompileNumber(
Node *numberExpression Node *numberExpression
) { ) {
@ -588,15 +610,7 @@ static void CompileFunctionVariableDeclaration(LLVMBuilderRef builder, Node *var
char *ptrName = strdup(variableName); char *ptrName = strdup(variableName);
strcat(ptrName, "_ptr"); strcat(ptrName, "_ptr");
if (IsPrimitiveType(variableDeclaration->children[0])) variable = LLVMBuildAlloca(builder, ResolveType(variableDeclaration->children[0]), ptrName);
{
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);
}
free(ptrName); free(ptrName);
@ -751,7 +765,7 @@ static void CompileStruct(LLVMModuleRef module, LLVMContextRef context, Node *no
switch (currentDeclarationNode->syntaxKind) switch (currentDeclarationNode->syntaxKind)
{ {
case Declaration: /* this is badly named */ case Declaration: /* this is badly named */
types[fieldCount] = WraithTypeToLLVMType(currentDeclarationNode->children[0]->children[0]->primitiveType); types[fieldCount] = ResolveType(currentDeclarationNode->children[0]);
fieldDeclarations[fieldCount] = currentDeclarationNode; fieldDeclarations[fieldCount] = currentDeclarationNode;
fieldCount += 1; fieldCount += 1;
break; break;