diff --git a/src/dobjtype.cpp b/src/dobjtype.cpp index 37ff26fe3..38c9f6fa2 100644 --- a/src/dobjtype.cpp +++ b/src/dobjtype.cpp @@ -3197,7 +3197,7 @@ void PClass::BuildFlatPointers () // // PClass :: NativeClass // -// Finds the underlying native type underlying this class. +// Finds the native type underlying this class. // //========================================================================== diff --git a/src/zscript/zcc-parse.lemon b/src/zscript/zcc-parse.lemon index 6d22f9ea4..9fdc4603c 100644 --- a/src/zscript/zcc-parse.lemon +++ b/src/zscript/zcc-parse.lemon @@ -248,6 +248,7 @@ struct_def(X) ::= STRUCT(T) IDENTIFIER(A) LBRACE opt_struct_body(B) RBRACE opt_s NEW_AST_NODE(Struct,def,T); def->NodeName = A.Name(); def->Body = B; + def->Type = nullptr; X = def; } diff --git a/src/zscript/zcc_compile.cpp b/src/zscript/zcc_compile.cpp index 68261635f..9dfbab847 100644 --- a/src/zscript/zcc_compile.cpp +++ b/src/zscript/zcc_compile.cpp @@ -51,8 +51,8 @@ // //========================================================================== -ZCCCompiler::ZCCCompiler(ZCC_AST &ast, DObject *_outer, PSymbolTable &_symbols) -: Outer(_outer), Symbols(&_symbols), AST(ast), ErrorCount(0), WarnCount(0) +ZCCCompiler::ZCCCompiler(ZCC_AST &ast, DObject *_outer, PSymbolTable &_symbols, PSymbolTable &_outsymbols) +: Outer(_outer), Symbols(&_symbols), OutputSymbols(&_outsymbols), AST(ast), ErrorCount(0), WarnCount(0) { // Group top-level nodes by type if (ast.TopNode != NULL) @@ -181,11 +181,28 @@ void ZCCCompiler::MessageV(ZCC_TreeNode *node, const char *txtcolor, const char int ZCCCompiler::Compile() { - CreateClasses(); + CreateClassTypes(); + CreateStructTypes(); CompileConstants(Constants); return ErrorCount; } +//========================================================================== +// +// ZCCCompiler :: CreateStructTypes +// +// Creates a PStruct for every struct. +// +//========================================================================== + +void ZCCCompiler::CreateStructTypes() +{ + for(auto s : Structs) + { + s->Type = NewStruct(s->NodeName, nullptr); + } +} + //========================================================================== // // ZCCCompiler :: CreateClassTypes @@ -197,20 +214,20 @@ int ZCCCompiler::Compile() // //========================================================================== -void ZCCCompiler::CreateClasses() +void ZCCCompiler::CreateClassTypes() { auto OrigClasses = std::move(Classes); Classes.Clear(); bool donesomething = true; while (donesomething) { - for (unsigned i=0;iParentName != nullptr && c->ParentName->SiblingNext == c->ParentName) parent = PClass::FindClass(c->ParentName->Id); else if (c->ParentName == nullptr) parent = RUNTIME_CLASS(DObject); else @@ -235,13 +252,17 @@ void ZCCCompiler::CreateClasses() // The parent exists, we may create a type for this class if (c->Flags & ZCC_Native) { - // If this is a native class, its own type must also already exist. + // If this is a native class, its own type must also already exist and not be a runtime class. auto me = PClass::FindClass(c->NodeName); if (me == nullptr) { Error(c, "Unknown native class %s", FName(c->NodeName).GetChars()); me = parent->FindClassTentative(c->NodeName); } + else if (me->bRuntimeClass) + { + Error(c, "%s is not a native class", FName(c->NodeName).GetChars()); + } else { DPrintf(DMSG_SPAMMY, "Registered %s as native with parent %s\n", me->TypeName.GetChars(), parent->TypeName.GetChars()); @@ -250,17 +271,8 @@ void ZCCCompiler::CreateClasses() } else { - auto me = PClass::FindClass(c->NodeName); - if (me != nullptr) - { - Error(c, "Redefining class %s", FName(c->NodeName).GetChars()); - } - else - { - me = parent->FindClassTentative(c->NodeName); - DPrintf(DMSG_SPAMMY, "Created %s with parent %s\n", me->TypeName.GetChars(), parent->TypeName.GetChars()); - } - c->Type = me; + // We will never get here if the name is a duplicate, so we can just do the assignment. + c->Type = parent->FindClassTentative(c->NodeName); } Classes.Push(c); OrigClasses.Delete(i); @@ -371,7 +383,7 @@ PSymbolConst *ZCCCompiler::CompileConstant(ZCC_ConstantDef *def) sym = new PSymbolConstNumeric(def->NodeName, TypeError, 0); } def->Symbol = sym; - Symbols->ReplaceSymbol(sym); + OutputSymbols->ReplaceSymbol(sym); return sym; } diff --git a/src/zscript/zcc_compile.h b/src/zscript/zcc_compile.h index 24497acd9..6ef4a34f2 100644 --- a/src/zscript/zcc_compile.h +++ b/src/zscript/zcc_compile.h @@ -4,11 +4,12 @@ class ZCCCompiler { public: - ZCCCompiler(ZCC_AST &tree, DObject *outer, PSymbolTable &symbols); + ZCCCompiler(ZCC_AST &tree, DObject *outer, PSymbolTable &symbols, PSymbolTable &outsymbols); int Compile(); private: - void CreateClasses(); + void CreateStructTypes(); + void CreateClassTypes(); void CompileConstants(const TArray &defs); PSymbolConst *CompileConstant(ZCC_ConstantDef *def); @@ -47,6 +48,7 @@ private: DObject *Outer; PSymbolTable *Symbols; + PSymbolTable *OutputSymbols; ZCC_AST * int ErrorCount; int WarnCount; diff --git a/src/zscript/zcc_parser.cpp b/src/zscript/zcc_parser.cpp index 3a34abe07..4f226f3f9 100644 --- a/src/zscript/zcc_parser.cpp +++ b/src/zscript/zcc_parser.cpp @@ -338,7 +338,8 @@ static void DoParse(int lumpnum) #endif } - ZCCCompiler cc(state, NULL, GlobalSymbols); + PSymbolTable symtable(&GlobalSymbols); + ZCCCompiler cc(state, NULL, symtable, GlobalSymbols); cc.Compile(); // ... and another one afterward so we can see what the compiler does with the data. #ifdef _DEBUG diff --git a/src/zscript/zcc_parser.h b/src/zscript/zcc_parser.h index 8b82b7eae..31ad22c5e 100644 --- a/src/zscript/zcc_parser.h +++ b/src/zscript/zcc_parser.h @@ -192,6 +192,7 @@ struct ZCC_Class : ZCC_NamedNode struct ZCC_Struct : ZCC_NamedNode { ZCC_TreeNode *Body; + PStruct *Type; }; struct ZCC_Enum : ZCC_NamedNode