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); $$ = AddStatement($1, $2);
} }
Arguments : PrimaryExpression Arguments : Expression
{ {
$$ = StartFunctionArgumentSequenceNode($1); $$ = StartFunctionArgumentSequenceNode($1);
} }
| Arguments COMMA PrimaryExpression | Arguments COMMA Expression
{ {
$$ = AddFunctionArgumentNode($1, $3); $$ = AddFunctionArgumentNode($1, $3);
} }

View File

@ -25,8 +25,12 @@ struct Program {
x: int = 4; x: int = 4;
y: int = Foo.Func(x); y: int = Foo.Func(x);
block: MemoryBlock<int>; block: MemoryBlock<int>;
addr: MemoryAddress = @malloc(y); block.capacity = y;
@free(addr); block.start = @malloc(y * @sizeof<int>());
return x; 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; uint64_t key;
TypeTag **types; TypeTag **types;
uint32_t typeCount; uint32_t typeCount;
StructTypeDeclaration structDeclaration; StructTypeDeclaration *structDeclaration;
} MonomorphizedGenericStructHashEntry; } MonomorphizedGenericStructHashEntry;
typedef struct MonomorphizedGenericStructHashArray typedef struct MonomorphizedGenericStructHashArray
@ -409,7 +409,33 @@ static LLVMTypeRef LookupCustomType(char *name)
return NULL; 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, GenericStructTypeDeclaration *genericStructTypeDeclaration,
TypeTag **genericArgumentTypes, TypeTag **genericArgumentTypes,
uint32_t genericArgumentTypeCount) uint32_t genericArgumentTypeCount)
@ -454,17 +480,11 @@ static StructTypeDeclaration CompileMonomorphizedGenericStruct(
LLVMTypeRef wStructType = LLVMStructCreateNamed(context, structName); LLVMTypeRef wStructType = LLVMStructCreateNamed(context, structName);
LLVMTypeRef wStructPointerType = LLVMPointerType(wStructType, 0); LLVMTypeRef wStructPointerType = LLVMPointerType(wStructType, 0);
StructTypeDeclaration declaration; StructTypeDeclaration *declaration = AddStructDeclaration(
declaration.module = genericStructTypeDeclaration->module; genericStructTypeDeclaration->module,
declaration.name = structName; wStructType,
declaration.structType = wStructType; wStructPointerType,
declaration.structPointerType = wStructPointerType; structName);
declaration.genericFunctions = NULL;
declaration.genericFunctionCount = 0;
declaration.functions = NULL;
declaration.functionCount = 0;
declaration.fields = NULL;
declaration.fieldCount = 0;
/* first build the structure def */ /* first build the structure def */
for (i = 0; i < declarationCount; i += 1) for (i = 0; i < declarationCount; i += 1)
@ -479,7 +499,7 @@ static StructTypeDeclaration CompileMonomorphizedGenericStruct(
->declarationSequence.sequence[i] ->declarationSequence.sequence[i]
->declaration.identifier->typeTag); ->declaration.identifier->typeTag);
AddFieldToStructDeclaration( AddFieldToStructDeclaration(
&declaration, declaration,
structDeclarationNode->structDeclaration.declarationSequence structDeclarationNode->structDeclaration.declarationSequence
->declarationSequence.sequence[i] ->declarationSequence.sequence[i]
->declaration.identifier->identifier.name); ->declaration.identifier->identifier.name);
@ -499,7 +519,7 @@ static StructTypeDeclaration CompileMonomorphizedGenericStruct(
{ {
case FunctionDeclaration: case FunctionDeclaration:
CompileFunction( CompileFunction(
&declaration, declaration,
structDeclarationNode->structDeclaration.declarationSequence structDeclarationNode->structDeclaration.declarationSequence
->declarationSequence.sequence[i]); ->declarationSequence.sequence[i]);
break; break;
@ -563,7 +583,7 @@ static StructTypeDeclaration *LookupGenericStructType(
if (hashEntry == NULL) if (hashEntry == NULL)
{ {
StructTypeDeclaration structTypeDeclaration = StructTypeDeclaration *structTypeDeclaration =
CompileMonomorphizedGenericStruct( CompileMonomorphizedGenericStruct(
&genericStructTypeDeclarations[i], &genericStructTypeDeclarations[i],
genericTypeTags, genericTypeTags,
@ -590,7 +610,7 @@ static StructTypeDeclaration *LookupGenericStructType(
hashEntry = &hashArray->elements[hashArray->count - 1]; 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; 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( static void DeclareStructFunction(
StructTypeDeclaration *structTypeDeclaration, StructTypeDeclaration *structTypeDeclaration,
LLVMValueRef function, LLVMValueRef function,