fix var lookups on generic structs

pull/11/head
cosmonaut 2021-06-03 15:08:01 -07:00
parent a870a2c32e
commit ca053585ac
3 changed files with 46 additions and 48 deletions

View File

@ -294,11 +294,11 @@ Statements : Statement
$$ = AddStatement($1, $2);
}
Arguments : PrimaryExpression
Arguments : Expression
{
$$ = StartFunctionArgumentSequenceNode($1);
}
| Arguments COMMA PrimaryExpression
| Arguments COMMA Expression
{
$$ = AddFunctionArgumentNode($1, $3);
}

View File

@ -25,8 +25,12 @@ struct Program {
x: int = 4;
y: int = Foo.Func(x);
block: MemoryBlock<int>;
addr: MemoryAddress = @malloc(y);
@free(addr);
return x;
block.capacity = y;
block.start = @malloc(y * @sizeof<int>());
z: MemoryAddress = block.AddressOf(2);
Console.PrintLine("%p", block.start);
Console.PrintLine("%p", z);
@free(block.start);
return 0;
}
}

View File

@ -119,7 +119,7 @@ typedef struct MonomorphizedGenericStructHashEntry
uint64_t key;
TypeTag **types;
uint32_t typeCount;
StructTypeDeclaration structDeclaration;
StructTypeDeclaration *structDeclaration;
} MonomorphizedGenericStructHashEntry;
typedef struct MonomorphizedGenericStructHashArray
@ -409,7 +409,33 @@ static LLVMTypeRef LookupCustomType(char *name)
return NULL;
}
static StructTypeDeclaration CompileMonomorphizedGenericStruct(
static StructTypeDeclaration *AddStructDeclaration(
LLVMModuleRef module,
LLVMTypeRef wStructType,
LLVMTypeRef wStructPointerType,
char *name)
{
uint32_t index = structTypeDeclarationCount;
structTypeDeclarations = realloc(
structTypeDeclarations,
sizeof(StructTypeDeclaration) * (structTypeDeclarationCount + 1));
structTypeDeclarations[index].module = module;
structTypeDeclarations[index].structType = wStructType;
structTypeDeclarations[index].structPointerType = wStructPointerType;
structTypeDeclarations[index].name = strdup(name);
structTypeDeclarations[index].fields = NULL;
structTypeDeclarations[index].fieldCount = 0;
structTypeDeclarations[index].functions = NULL;
structTypeDeclarations[index].functionCount = 0;
structTypeDeclarations[index].genericFunctions = NULL;
structTypeDeclarations[index].genericFunctionCount = 0;
structTypeDeclarationCount += 1;
return &structTypeDeclarations[index];
}
static StructTypeDeclaration *CompileMonomorphizedGenericStruct(
GenericStructTypeDeclaration *genericStructTypeDeclaration,
TypeTag **genericArgumentTypes,
uint32_t genericArgumentTypeCount)
@ -454,17 +480,11 @@ static StructTypeDeclaration CompileMonomorphizedGenericStruct(
LLVMTypeRef wStructType = LLVMStructCreateNamed(context, structName);
LLVMTypeRef wStructPointerType = LLVMPointerType(wStructType, 0);
StructTypeDeclaration declaration;
declaration.module = genericStructTypeDeclaration->module;
declaration.name = structName;
declaration.structType = wStructType;
declaration.structPointerType = wStructPointerType;
declaration.genericFunctions = NULL;
declaration.genericFunctionCount = 0;
declaration.functions = NULL;
declaration.functionCount = 0;
declaration.fields = NULL;
declaration.fieldCount = 0;
StructTypeDeclaration *declaration = AddStructDeclaration(
genericStructTypeDeclaration->module,
wStructType,
wStructPointerType,
structName);
/* first build the structure def */
for (i = 0; i < declarationCount; i += 1)
@ -479,7 +499,7 @@ static StructTypeDeclaration CompileMonomorphizedGenericStruct(
->declarationSequence.sequence[i]
->declaration.identifier->typeTag);
AddFieldToStructDeclaration(
&declaration,
declaration,
structDeclarationNode->structDeclaration.declarationSequence
->declarationSequence.sequence[i]
->declaration.identifier->identifier.name);
@ -499,7 +519,7 @@ static StructTypeDeclaration CompileMonomorphizedGenericStruct(
{
case FunctionDeclaration:
CompileFunction(
&declaration,
declaration,
structDeclarationNode->structDeclaration.declarationSequence
->declarationSequence.sequence[i]);
break;
@ -563,7 +583,7 @@ static StructTypeDeclaration *LookupGenericStructType(
if (hashEntry == NULL)
{
StructTypeDeclaration structTypeDeclaration =
StructTypeDeclaration *structTypeDeclaration =
CompileMonomorphizedGenericStruct(
&genericStructTypeDeclarations[i],
genericTypeTags,
@ -590,7 +610,7 @@ static StructTypeDeclaration *LookupGenericStructType(
hashEntry = &hashArray->elements[hashArray->count - 1];
}
return &hashEntry->structDeclaration;
return hashEntry->structDeclaration;
}
}
@ -756,32 +776,6 @@ static LLVMValueRef FindVariableValue(LLVMBuilderRef builder, char *name)
return NULL;
}
static StructTypeDeclaration *AddStructDeclaration(
LLVMModuleRef module,
LLVMTypeRef wStructType,
LLVMTypeRef wStructPointerType,
char *name)
{
uint32_t index = structTypeDeclarationCount;
structTypeDeclarations = realloc(
structTypeDeclarations,
sizeof(StructTypeDeclaration) * (structTypeDeclarationCount + 1));
structTypeDeclarations[index].module = module;
structTypeDeclarations[index].structType = wStructType;
structTypeDeclarations[index].structPointerType = wStructPointerType;
structTypeDeclarations[index].name = strdup(name);
structTypeDeclarations[index].fields = NULL;
structTypeDeclarations[index].fieldCount = 0;
structTypeDeclarations[index].functions = NULL;
structTypeDeclarations[index].functionCount = 0;
structTypeDeclarations[index].genericFunctions = NULL;
structTypeDeclarations[index].genericFunctionCount = 0;
structTypeDeclarationCount += 1;
return &structTypeDeclarations[index];
}
static void DeclareStructFunction(
StructTypeDeclaration *structTypeDeclaration,
LLVMValueRef function,