Skip to content

Commit a1806c5

Browse files
committed
Use semanticIdentifierTable for identifiers which end up in the scope table
1 parent 72aaa85 commit a1806c5

File tree

3 files changed

+90
-56
lines changed

3 files changed

+90
-56
lines changed

semantic/metac_expr_semantic.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,13 +1314,14 @@ metac_sema_expr_t* MetaCSemantic_doExprSemantic_(metac_sema_state_t* self,
13141314

13151315
case expr_identifier:
13161316
{
1317-
1317+
metac_identifier_ptr_t semaIdentifier =
1318+
MetaCIdentifierTable_CopyIdentifier(
1319+
self->ParserIdentifierTable, &self->SemanticIdentifierTable, expr->IdentifierPtr
1320+
);
13181321
//printf("Looking up: %s\n",
13191322
// IdentifierPtrToCharPtr(self->ParserIdentifierTable, result->IdentifierPtr));
13201323

1321-
metac_node_t node =
1322-
MetaCSemantic_LookupIdentifier(self,
1323-
expr->IdentifierPtr);
1324+
metac_node_t node = MetaCSemantic_LookupIdentifier(self, semaIdentifier);
13241325
if (node == emptyPointer)
13251326
{
13261327
xfprintf(stderr, "Identifier lookup failed\n");

semantic/metac_semantic.c

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
#include "metac_type_semantic.c"
2727
#include "metac_expr_semantic.c"
2828

29+
#define USE_SEMA_ID_IN_SCOPE_TABLE 1
30+
31+
2932
const char* MetaCExprKind_toChars(metac_expr_kind_t);
3033
bool IsExprNode(metac_node_kind_t);
3134

@@ -443,7 +446,7 @@ metac_sema_stmt_t* MetaCSemantic_doStmtSemantic_(metac_sema_state_t* self,
443446
stmt_while_t* whileStmt = cast(stmt_while_t*) stmt;
444447
sema_stmt_while_t* semaWhileStmt =
445448
AllocNewSemaStmt(self, stmt_while, &result);
446-
449+
447450
hash ^= stmt->Kind;
448451

449452
semaWhileStmt->Kind = stmt->Kind;
@@ -795,7 +798,14 @@ scope_insert_error_t MetaCSemantic_RegisterInScope(metac_sema_state_t* self,
795798
metac_identifier_ptr_t idPtr,
796799
metac_node_t node)
797800
{
798-
const char* idChars = IdentifierPtrToCharPtr(self->ParserIdentifierTable, idPtr);
801+
const metac_identifier_table_t* idSource =
802+
#if USE_SEMA_ID_IN_SCOPE_TABLE
803+
&self->SemanticIdentifierTable
804+
#else
805+
self->ParserIdentifierTable
806+
#endif
807+
;
808+
const char* idChars = IdentifierPtrToCharPtr(idSource, idPtr);
799809
scope_insert_error_t result = no_scope;
800810
ALIGN_STACK();
801811
MetaCSemantic_LRU_RemoveIdentifier(self, idPtr);
@@ -804,6 +814,7 @@ scope_insert_error_t MetaCSemantic_RegisterInScope(metac_sema_state_t* self,
804814
if (self->CurrentScope != 0)
805815
{
806816
result = MetaCScope_RegisterIdentifier(self->CurrentScope, idPtr, node);
817+
assert(result == success);
807818
}
808819
else
809820
{
@@ -841,7 +852,6 @@ scope_insert_error_t MetaCSemantic_RegisterInScope(metac_sema_state_t* self,
841852
return result;
842853
}
843854

844-
845855
sema_decl_function_t* MetaCSemantic_doFunctionSemantic(metac_sema_state_t* self,
846856
decl_function_t* func)
847857
{
@@ -852,14 +862,14 @@ sema_decl_function_t* MetaCSemantic_doFunctionSemantic(metac_sema_state_t* self,
852862

853863
sema_decl_function_t* f = AllocNewSemaFunction(self, func);
854864
// for now we don't nest functions.
855-
/* TODO use semanId
865+
# if USE_SEMA_ID_IN_SCOPE_TABLE
856866
metac_identifier_ptr_t semaIdentifier =
857867
MetaCIdentifierTable_CopyIdentifier(self->ParserIdentifierTable,
858868
&self->SemanticIdentifierTable, func->Identifier);
859869
f->Identifier = semaIdentifier;
860-
*/
870+
#else
861871
f->Identifier = func->Identifier;
862-
872+
#endif
863873
// printf("doing Function: %s\n", IdentifierPtrToCharPtr(self->ParserIdentifierTable, func->Identifier));
864874

865875
// let's first do the parameters
@@ -876,7 +886,7 @@ sema_decl_function_t* MetaCSemantic_doFunctionSemantic(metac_sema_state_t* self,
876886
// as we have an easier time if we know at which
877887
// param we are and how many follow
878888
decl_variable_t* paramVar = currentParam->Parameter;
879-
#if 0
889+
#if USE_SEMA_ID_IN_SCOPE_TABLE
880890
// TODO use identifier in sema table
881891
{
882892
metac_identifier_ptr_t semaId =
@@ -1137,7 +1147,7 @@ const char* doDeclSemantic_PrintFunction(task_t* task)
11371147
task->Context;
11381148
metac_alloc_t tmpAlloc = ctx->Sema->TempAlloc;
11391149
const char* declPrint;
1140-
1150+
11411151
MetaCPrinter_Init(&printer, ctx->Sema->ParserIdentifierTable, ctx->Sema->ParserStringTable, &tmpAlloc);
11421152
declPrint = MetaCPrinter_PrintDecl(&printer, ctx->Decl);
11431153

@@ -1176,6 +1186,10 @@ metac_sema_decl_t* MetaCSemantic_declSemantic(metac_sema_state_t* self,
11761186

11771187
var->Hash = v->Hash;
11781188
var->TypeIndex = MetaCSemantic_doTypeSemantic(self, v->VarType);
1189+
var->VarIdentifier = MetaCIdentifierTable_CopyIdentifier(
1190+
self->ParserIdentifierTable, &self->SemanticIdentifierTable,
1191+
v->VarIdentifier);
1192+
11791193

11801194
if (METAC_NODE(v->VarInitExpr) != emptyNode)
11811195
{
@@ -1188,7 +1202,6 @@ metac_sema_decl_t* MetaCSemantic_declSemantic(metac_sema_state_t* self,
11881202

11891203
//TODO make sure nLocals is reset at the end of a function
11901204
// also this doesn't deal with static properly
1191-
var->VarIdentifier = v->VarIdentifier;
11921205
if (v->StorageClass == storageclass_local)
11931206
{
11941207
var->Storage.v = STORAGE_V(storage_local, self->nLocals++);
@@ -1199,11 +1212,10 @@ metac_sema_decl_t* MetaCSemantic_declSemantic(metac_sema_state_t* self,
11991212
// TODO maybe we have to mark the global as having been inserted.
12001213
ARENA_ARRAY_ADD(self->Globals, cast(metac_sema_decl_t*)var);
12011214
}
1202-
12031215
MetaCSemantic_RegisterInScope(self, var->VarIdentifier, METAC_NODE(var));
12041216
/*
12051217
Info("Introducing variable: %s\n",
1206-
IdentifierPtrToCharPtr(self->ParserIdentifierTable, v->VarIdentifier));
1218+
IdentifierPtrToCharPtr(&self->SemanticIdentifierTable, v->VarIdentifier));
12071219
*/
12081220
} break;
12091221
case decl_type_typeof:
@@ -1252,7 +1264,6 @@ metac_sema_decl_t* MetaCSemantic_declSemantic(metac_sema_state_t* self,
12521264
#ifndef NO_FIBERS
12531265
void MetaCSemantic_doDeclSemantic_Task(task_t* task)
12541266
{
1255-
12561267
MetaCSemantic_doDeclSemantic_task_context_t* ctx =
12571268
(MetaCSemantic_doDeclSemantic_task_context_t*)
12581269
task->Context;
@@ -1428,7 +1439,7 @@ const char* TypeToChars(metac_sema_state_t* self, metac_type_index_t typeIndex)
14281439
const char* result = 0;
14291440
static metac_printer_t printer = {0};
14301441
if (!printer.StringMemory)
1431-
MetaCPrinter_InitSz(&printer, self->ParserIdentifierTable, 0, (metac_alloc_t*)0, 32);
1442+
MetaCPrinter_InitSz(&printer, &self->SemanticIdentifierTable, 0, (metac_alloc_t*)0, 32);
14321443
else
14331444
MetaCPrinter_Reset(&printer);
14341445
TypeToCharsP(self, &printer, typeIndex);

semantic/metac_type_semantic.c

Lines changed: 61 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -598,42 +598,39 @@ void PopulateTemporaryAggregateScope(metac_sema_state_t* self,
598598
AllocateAggregateScopeAndFields(&self->TempAlloc, nFields);
599599

600600
metac_type_aggregate_field_t* fields = scopeAndFields.Fields;
601-
602-
// ??? do we want to the the parent like this?
603-
// scopeAndFields.Scope->Parent = self->CurrentScope;
604-
// or do we want the aggregate scope to exist in isolation ...
605-
// I think we want it in isolation.
601+
decl_field_t* aggField = agg->Fields;
606602

607603
tmpSemaAgg->Scope = scopeAndFields.Scope;
608604
tmpSemaAgg->Fields = fields;
609605
tmpSemaAgg->FieldCount = nFields;
610-
decl_field_t* aggField = agg->Fields;
611606

612607
MetaCSemantic_MountScope(self, tmpSemaAgg->Scope);
613608

614609
for(uint32_t i = 0; i < nFields; i++)
615610
{
616611
metac_type_aggregate_field_t* semaField = &fields[i];
617-
metac_identifier_ptr_t fieldName = aggField->Field->VarIdentifier;
618-
// ??? should we Register this identifier in the semanticIdentifierTable
619-
// and using a pointer to that in the sema node?
612+
metac_identifier_ptr_t parserFieldName = aggField->Field->VarIdentifier;
613+
metac_identifier_ptr_t semaFieldName =
614+
MetaCIdentifierTable_CopyIdentifier(self->ParserIdentifierTable,
615+
&self->SemanticIdentifierTable,
616+
parserFieldName);
617+
scope_insert_error_t inserted;
620618

621619
uint32_t declNodePtr = 42;
622620
semaField->Header.Kind = node_decl_field;
623621

624-
semaField->Identifier = fieldName;
625-
// placeholder while we don't have hash-table for decl_node pointers
622+
semaField->Identifier = semaFieldName;
626623
semaField->Index = cast(uint16_t) i;
627-
semaField->Type.v = TYPE_INDEX_V(type_index_unknown, declNodePtr);
628-
//metac_sema_identifier_t
629-
// semaId = MetaCSemantic_RegisterIdentifier(agg->Fields[i].Field.VarIdentifier);
630-
scope_insert_error_t inserted =
631-
MetaCSemantic_RegisterInScope(self, fieldName,
624+
semaField->Type.v = TYPE_INDEX_V(type_index_unknown, 0);
625+
626+
inserted =
627+
MetaCSemantic_RegisterInScope(self, semaFieldName,
632628
cast(metac_node_t)semaField);
629+
633630
if (inserted != success)
634631
{
635632
SemanticError(0, "%s could not be inserted",
636-
IdentifierPtrToCharPtr(self->ParserIdentifierTable, fieldName));
633+
IdentifierPtrToCharPtr(self->ParserIdentifierTable, parserFieldName));
637634
}
638635

639636
aggField = aggField->Next;
@@ -968,9 +965,16 @@ metac_type_index_t TypeArraySemantic(metac_sema_state_t* self,
968965
MetaCSemantic_doExprSemantic(self, arrayType->Dim, 0)
969966
);
970967

968+
uint32_t dimValue = cast(uint32_t) -1;
969+
971970
if (METAC_NODE(dim) != emptyNode)
972971
{
973-
if (dim->Kind != expr_signed_integer)
972+
if (dim->Kind == expr_unknown_value)
973+
{
974+
// this is essentially allowd b/c of templates.
975+
// TODO we might have to do something more here, maybe?
976+
}
977+
else if (dim->Kind != expr_signed_integer)
974978
{
975979
xprintf("Array dimension should eval to integer but it is: %s\n",
976980
MetaCExprKind_toChars(dim->Kind));
@@ -979,9 +983,11 @@ metac_type_index_t TypeArraySemantic(metac_sema_state_t* self,
979983
return invalidTypeIndex;
980984
}
981985
}
982-
uint32_t dimValue = (
983-
METAC_NODE(dim) != emptyNode ?
984-
(uint32_t)dim->ValueU64 : -1);
986+
987+
if (dim->Kind == expr_signed_integer)
988+
{
989+
dimValue = cast(int32_t)dim->ValueI64;
990+
}
985991

986992
result =
987993
MetaCSemantic_GetArrayTypeOf(self, elementType, dimValue);
@@ -1160,67 +1166,76 @@ metac_type_index_t MetaCSemantic_TypeSemantic(metac_sema_state_t* self,
11601166
else if (IsAggregateTypeDecl(type->Kind))
11611167
{
11621168
STACK_ARENA_ARRAY(metac_type_aggregate_field_t, tmpFields, 128, &self->TempAlloc)
1163-
1164-
decl_type_struct_t* agg = (decl_type_struct_t*) type;
1169+
metac_scope_owner_kind_t ownerKind = scope_owner_unknown;
1170+
decl_type_struct_t* agg = cast(decl_type_struct_t*) type;
11651171
metac_type_aggregate_t tmpSemaAggMem = {(metac_decl_kind_t)0};
11661172
metac_type_aggregate_t* tmpSemaAgg = &tmpSemaAggMem;
11671173
metac_scope_t tmpTemplateScope = {0};
11681174

11691175

11701176
if (type->Kind == decl_type_struct)
1177+
{
11711178
typeKind = type_struct;
1179+
ownerKind = scope_owner_struct;
1180+
}
11721181
else if (type->Kind == decl_type_union)
1182+
{
11731183
typeKind = type_union;
1184+
}
11741185
else
1186+
{
11751187
assert(0);
1176-
1188+
}
1189+
11771190
ARENA_ARRAY_ENSURE_SIZE(tmpFields, agg->FieldCount);
11781191

11791192
tmpSemaAgg->Header.Kind = agg->Kind;
11801193
tmpSemaAgg->Identifier = agg->Identifier;
11811194
tmpSemaAgg->Fields = tmpFields;
11821195
tmpSemaAgg->FieldCount = agg->FieldCount;
11831196

1184-
PopulateTemporaryAggregateScope(self, tmpSemaAgg, agg);
1185-
MetaCSemantic_MountScope(self, tmpSemaAgg->Scope);
11861197

11871198
if (agg->ParameterCount != 0)
11881199
{
11891200
uint32_t parameterCount = agg->ParameterCount;
11901201
decl_parameter_t* param = agg->Parameters;
11911202
U32(tmpTemplateScope.ScopeFlags) |= scope_flag_temporary;
1203+
tmpTemplateScope.Owner.v = SCOPE_OWNER_V(scope_owner_template, 0);
1204+
MetaCScopeTable_InitN(&tmpTemplateScope.ScopeTable, agg->ParameterCount, &self->TempAlloc);
1205+
11921206
MetaCSemantic_PushTemporaryScope(self, &tmpTemplateScope);
11931207

11941208
for(uint32_t paramIdx = 0; paramIdx < parameterCount; paramIdx++)
11951209
{
1210+
scope_insert_error_t inserted;
11961211
metac_identifier_ptr_t paramIdent = param->Parameter->VarIdentifier;
11971212
metac_location_ptr_t locIdx = param->Parameter->LocationIdx;
11981213
metac_expr_t placeholderExpr;
11991214
metac_sema_expr_t* placeholder = AllocNewSemaExpr(self, &placeholderExpr);
1200-
1215+
// we need to register the parser Identifier in the semantic table for resolution.
1216+
metac_identifier_ptr_t semaId =
1217+
MetaCIdentifierTable_CopyIdentifier(self->ParserIdentifierTable, &self->SemanticIdentifierTable, paramIdent);
1218+
12011219
placeholder->Kind = expr_unknown_value;
12021220
placeholder->TypeIndex = MetaCSemantic_TypeSemantic(self, param->Parameter->VarType);
12031221
METAC_NODE(placeholder->Expr) = emptyNode;
12041222
placeholder->LocationIdx = locIdx;
12051223

1206-
1207-
MetaCSemantic_RegisterInScope(self, paramIdent, METAC_NODE(placeholder));
1224+
inserted = MetaCSemantic_RegisterInScope(self, semaId, METAC_NODE(placeholder));
12081225

12091226
param = param->Next;
12101227
}
12111228
}
1229+
1230+
PopulateTemporaryAggregateScope(self, tmpSemaAgg, agg);
1231+
MetaCSemantic_MountScope(self, tmpSemaAgg->Scope);
1232+
12121233

12131234
switch(typeKind)
12141235
{
12151236
case type_struct:
12161237
{
1217-
uint32_t hash;
12181238
MetaCSemantic_ComputeStructLayout(self, agg, tmpSemaAgg);
1219-
1220-
if (agg->ParameterCount != 0)
1221-
{
1222-
MetaCSemantic_PopTemporaryScope(self);
1223-
}
12241239
tmpSemaAgg->Header.Hash = AggregateHash(tmpSemaAgg);
12251240

12261241
result =
@@ -1252,6 +1267,10 @@ metac_type_index_t MetaCSemantic_TypeSemantic(metac_sema_state_t* self,
12521267
}
12531268

12541269
MetaCSemantic_UnmountScope(self);
1270+
if (agg->ParameterCount != 0)
1271+
{
1272+
MetaCSemantic_PopTemporaryScope(self);
1273+
}
12551274
}
12561275
else if (IsPointerType(type->Kind))
12571276
{
@@ -1334,8 +1353,9 @@ metac_type_index_t MetaCSemantic_TypeSemantic(metac_sema_state_t* self,
13341353
metac_scope_t tmpScope = {scope_flag_temporary};
13351354
MetaCSemantic_PushTemporaryScope(self, &tmpScope);
13361355

1337-
for(metac_expr_t* e = args->Expr; METAC_NODE(args) != emptyNode; args = args->Next, e = args->Expr)
1356+
for(; METAC_NODE(args) != emptyNode; args = args->Next)
13381357
{
1358+
metac_expr_t* e = args->Expr;
13391359
ARENA_ARRAY_ADD(semaArguments, MetaCSemantic_doExprSemantic(self, e, 0));
13401360
}
13411361

@@ -1355,7 +1375,7 @@ metac_type_index_t MetaCSemantic_TypeSemantic(metac_sema_state_t* self,
13551375
header,
13561376
zeroIdx,
13571377
symbol,
1358-
arguments,
1378+
tInst->Arguments, // we may need to copy the args here?
13591379
nArguments,
13601380
};
13611381
result = MetaCTypeTable_GetOrEmptyTemplateType(&self->TemplateTypeTable, &key);
@@ -1366,7 +1386,7 @@ metac_type_index_t MetaCSemantic_TypeSemantic(metac_sema_state_t* self,
13661386
}
13671387

13681388
}
1369-
1389+
MetaCSemantic_PopTemporaryScope(self);
13701390
}
13711391
else if (type->Kind == decl_type)
13721392
{
@@ -1665,7 +1685,9 @@ bool MetaCSemantic_ComputeStructLayout(metac_sema_state_t* self,
16651685
semaField < onePastLast;
16661686
semaField++)
16671687
{
1668-
semaField->Identifier = declField->Field->VarIdentifier;
1688+
semaField->Identifier =
1689+
MetaCIdentifierTable_CopyIdentifier(
1690+
self->ParserIdentifierTable, &self->SemanticIdentifierTable, declField->Field->VarIdentifier);
16691691
semaField->Header.Kind = node_decl_field;
16701692

16711693
semaField->Type =

0 commit comments

Comments
 (0)