Navigation Menu

Skip to content

Commit

Permalink
Fix definition order of ZScript structs
Browse files Browse the repository at this point in the history
Do a first pass over the Structs array in CompileAllFields() to reorder them such that if a struct uses other structs, those structs will be resolved first.
  • Loading branch information
rheit authored and coelckers committed Jul 29, 2022
1 parent 742ae8c commit b9501a7
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/common/scripting/core/types.cpp
Expand Up @@ -334,6 +334,7 @@ void PType::StaticInit()
TypeVector2->moveOp = OP_MOVEV2;
TypeVector2->RegType = REGT_FLOAT;
TypeVector2->RegCount = 2;
TypeVector2->isOrdered = true;

TypeVector3 = new PStruct(NAME_Vector3, nullptr);
TypeVector3->AddField(NAME_X, TypeFloat64);
Expand All @@ -347,6 +348,7 @@ void PType::StaticInit()
TypeVector3->moveOp = OP_MOVEV3;
TypeVector3->RegType = REGT_FLOAT;
TypeVector3->RegCount = 3;
TypeVector3->isOrdered = true;


TypeFVector2 = new PStruct(NAME_FVector2, nullptr);
Expand All @@ -358,6 +360,7 @@ void PType::StaticInit()
TypeFVector2->moveOp = OP_MOVEV2;
TypeFVector2->RegType = REGT_FLOAT;
TypeFVector2->RegCount = 2;
TypeFVector2->isOrdered = true;

TypeFVector3 = new PStruct(NAME_FVector3, nullptr);
TypeFVector3->AddField(NAME_X, TypeFloat32);
Expand All @@ -371,6 +374,7 @@ void PType::StaticInit()
TypeFVector3->moveOp = OP_MOVEV3;
TypeFVector3->RegType = REGT_FLOAT;
TypeFVector3->RegCount = 3;
TypeFVector3->isOrdered = true;

Namespaces.GlobalNamespace->Symbols.AddSymbol(Create<PSymbolType>(NAME_sByte, TypeSInt8));
Namespaces.GlobalNamespace->Symbols.AddSymbol(Create<PSymbolType>(NAME_Byte, TypeUInt8));
Expand Down
1 change: 1 addition & 0 deletions src/common/scripting/core/types.h
Expand Up @@ -537,6 +537,7 @@ class PStruct : public PContainerType
PStruct(FName name, PTypeBase *outer, bool isnative = false);

bool isNative;
bool isOrdered = false;
// Some internal structs require explicit construction and destruction of fields the VM cannot handle directly so use these two functions for it.
VMFunction *mConstructor = nullptr;
VMFunction *mDestructor = nullptr;
Expand Down
79 changes: 78 additions & 1 deletion src/common/scripting/frontend/zcc_compile.cpp
Expand Up @@ -1325,7 +1325,7 @@ void ZCCCompiler::CompileAllFields()
{
// Create copies of the arrays which can be altered
auto Classes = this->Classes;
auto Structs = this->Structs;
auto Structs = OrderStructs();
TMap<FName, bool> HasNativeChildren;

// first step: Look for native classes with native children.
Expand Down Expand Up @@ -1612,6 +1612,83 @@ bool ZCCCompiler::CompileFields(PContainerType *type, TArray<ZCC_VarDeclarator *
return Fields.Size() == 0;
}

//==========================================================================
//
// ZCCCompiler :: OrderStructs
//
// Order the Structs array so that the least-dependant structs come first
//
//==========================================================================

TArray<ZCC_StructWork *> ZCCCompiler::OrderStructs()
{
TArray<ZCC_StructWork *> new_order;

for (auto struct_def : Structs)
{
if (std::find(new_order.begin(), new_order.end(), struct_def) != new_order.end())
{
continue;
}
AddStruct(new_order, struct_def);
}
return new_order;
}

//==========================================================================
//
// ZCCCompiler :: AddStruct
//
// Adds a struct to the Structs array, preceded by all its dependant structs
//
//==========================================================================

void ZCCCompiler::AddStruct(TArray<ZCC_StructWork *> &new_order, ZCC_StructWork *my_def)
{
PStruct *my_type = static_cast<PStruct *>(my_def->Type());
if (my_type)
{
if (my_type->isOrdered)
{
return;
}
my_type->isOrdered = true;
}

// Find all struct fields and add them before this one
for (const auto field : my_def->Fields)
{
PType *fieldtype = DetermineType(my_type, field, field->Names->Name, field->Type, true, true);
if (fieldtype->isStruct() && !static_cast<PStruct *>(fieldtype)->isOrdered)
{
AddStruct(new_order, StructTypeToWork(static_cast<PStruct *>(fieldtype)));
}
}
new_order.Push(my_def);
}

//==========================================================================
//
// ZCCCompiler :: StructTypeToWork
//
// Find the ZCC_StructWork that corresponds to a PStruct
//
//==========================================================================

ZCC_StructWork *ZCCCompiler::StructTypeToWork(const PStruct *type) const
{
assert(type->isStruct());
for (auto &def : Structs)
{
if (def->Type() == type)
{
return def;
}
}
assert(false && "Struct not found");
return nullptr;
}

//==========================================================================
//
// ZCCCompiler :: FieldFlagsToString
Expand Down
3 changes: 3 additions & 0 deletions src/common/scripting/frontend/zcc_compile.h
Expand Up @@ -135,6 +135,9 @@ class ZCCCompiler
PType *DetermineType(PType *outertype, ZCC_TreeNode *field, FName name, ZCC_Type *ztype, bool allowarraytypes, bool formember);
PType *ResolveArraySize(PType *baseType, ZCC_Expression *arraysize, PContainerType *cls, bool *nosize);
PType *ResolveUserType(ZCC_BasicType *type, PSymbolTable *sym, bool nativetype);
TArray<ZCC_StructWork *> OrderStructs();
void AddStruct(TArray<ZCC_StructWork *> &new_order, ZCC_StructWork *struct_def);
ZCC_StructWork *StructTypeToWork(const PStruct *type) const;

void CompileFunction(ZCC_StructWork *c, ZCC_FuncDeclarator *f, bool forclass);

Expand Down

0 comments on commit b9501a7

Please sign in to comment.