- merged PStruct and PNativeStruct.

There were exactly 4 places in the code that checked for the difference, this is better done with a flag.
This commit is contained in:
Christoph Oelckers 2017-04-12 15:12:41 +02:00
commit 0d7b7d6ab1
7 changed files with 40 additions and 82 deletions

View file

@ -1772,7 +1772,7 @@ FxExpression *FxTypeCast::Resolve(FCompileContext &ctx)
if (fromtype->IsDescendantOf(totype)) goto basereturn;
}
}
else if (basex->ValueType->IsA(RUNTIME_CLASS(PNativeStruct)) && ValueType->IsKindOf(RUNTIME_CLASS(PPointer)) && static_cast<PPointer*>(ValueType)->PointedType == basex->ValueType)
else if (basex->IsNativeStruct() && ValueType->IsKindOf(RUNTIME_CLASS(PPointer)) && static_cast<PPointer*>(ValueType)->PointedType == basex->ValueType)
{
bool writable;
basex->RequestAddress(ctx, &writable);
@ -2493,7 +2493,7 @@ FxExpression *FxAssign::Resolve(FCompileContext &ctx)
}
// Both types are the same so this is ok.
}
else if (Right->ValueType->IsA(RUNTIME_CLASS(PNativeStruct)) && Base->ValueType->IsKindOf(RUNTIME_CLASS(PPointer)) && static_cast<PPointer*>(Base->ValueType)->PointedType == Right->ValueType)
else if (Right->IsNativeStruct() && Base->ValueType->IsKindOf(RUNTIME_CLASS(PPointer)) && static_cast<PPointer*>(Base->ValueType)->PointedType == Right->ValueType)
{
// allow conversion of native structs to pointers of the same type. This is necessary to assign elements from global arrays like players, sectors, etc. to local pointers.
// For all other types this is not needed. Structs are not assignable and classes can only exist as references.

View file

@ -337,6 +337,7 @@ public:
bool IsArray() const { return ValueType->IsKindOf(RUNTIME_CLASS(PArray)) || (ValueType->IsKindOf(RUNTIME_CLASS(PPointer)) && static_cast<PPointer*>(ValueType)->PointedType->IsKindOf(RUNTIME_CLASS(PArray))); }
bool IsResizableArray() const { return (ValueType->IsKindOf(RUNTIME_CLASS(PPointer)) && static_cast<PPointer*>(ValueType)->PointedType->IsKindOf(RUNTIME_CLASS(PStaticArray))); } // can only exist in pointer form.
bool IsDynamicArray() const { return (ValueType->IsKindOf(RUNTIME_CLASS(PDynArray))); }
bool IsNativeStruct() const { return (ValueType->IsA(RUNTIME_CLASS(PStruct)) && static_cast<PStruct*>(ValueType)->isNative); }
virtual ExpEmit Emit(VMFunctionBuilder *build);
void EmitStatement(VMFunctionBuilder *build);

View file

@ -777,11 +777,11 @@ static int fieldcmp(const void * a, const void * b)
void InitThingdef()
{
// Some native types need size and serialization information added before the scripts get compiled.
auto secplanestruct = NewNativeStruct("Secplane", nullptr);
auto secplanestruct = NewStruct("Secplane", nullptr, true);
secplanestruct->Size = sizeof(secplane_t);
secplanestruct->Align = alignof(secplane_t);
auto sectorstruct = NewNativeStruct("Sector", nullptr);
auto sectorstruct = NewStruct("Sector", nullptr, true);
sectorstruct->Size = sizeof(sector_t);
sectorstruct->Align = alignof(sector_t);
NewPointer(sectorstruct, false)->InstallHandlers(
@ -796,7 +796,7 @@ void InitThingdef()
}
);
auto linestruct = NewNativeStruct("Line", nullptr);
auto linestruct = NewStruct("Line", nullptr, true);
linestruct->Size = sizeof(line_t);
linestruct->Align = alignof(line_t);
NewPointer(linestruct, false)->InstallHandlers(
@ -811,7 +811,7 @@ void InitThingdef()
}
);
auto sidestruct = NewNativeStruct("Side", nullptr);
auto sidestruct = NewStruct("Side", nullptr, true);
sidestruct->Size = sizeof(side_t);
sidestruct->Align = alignof(side_t);
NewPointer(sidestruct, false)->InstallHandlers(
@ -826,7 +826,7 @@ void InitThingdef()
}
);
auto vertstruct = NewNativeStruct("Vertex", nullptr);
auto vertstruct = NewStruct("Vertex", nullptr, true);
vertstruct->Size = sizeof(vertex_t);
vertstruct->Align = alignof(vertex_t);
NewPointer(vertstruct, false)->InstallHandlers(
@ -841,23 +841,23 @@ void InitThingdef()
}
);
auto sectorportalstruct = NewNativeStruct("SectorPortal", nullptr);
auto sectorportalstruct = NewStruct("SectorPortal", nullptr, true);
sectorportalstruct->Size = sizeof(FSectorPortal);
sectorportalstruct->Align = alignof(FSectorPortal);
auto playerclassstruct = NewNativeStruct("PlayerClass", nullptr);
auto playerclassstruct = NewStruct("PlayerClass", nullptr, true);
playerclassstruct->Size = sizeof(FPlayerClass);
playerclassstruct->Align = alignof(FPlayerClass);
auto playerskinstruct = NewNativeStruct("PlayerSkin", nullptr);
auto playerskinstruct = NewStruct("PlayerSkin", nullptr, true);
playerskinstruct->Size = sizeof(FPlayerSkin);
playerskinstruct->Align = alignof(FPlayerSkin);
auto teamstruct = NewNativeStruct("Team", nullptr);
auto teamstruct = NewStruct("Team", nullptr, true);
teamstruct->Size = sizeof(FTeam);
teamstruct->Align = alignof(FTeam);
PStruct *pstruct = NewNativeStruct("PlayerInfo", nullptr);
PStruct *pstruct = NewStruct("PlayerInfo", nullptr, true);
pstruct->Size = sizeof(player_t);
pstruct->Align = alignof(player_t);
NewPointer(pstruct, false)->InstallHandlers(
@ -872,7 +872,7 @@ void InitThingdef()
}
);
auto fontstruct = NewNativeStruct("FFont", nullptr);
auto fontstruct = NewStruct("FFont", nullptr, true);
fontstruct->Size = sizeof(FFont);
fontstruct->Align = alignof(FFont);
NewPointer(fontstruct, false)->InstallHandlers(
@ -887,7 +887,7 @@ void InitThingdef()
}
);
auto wbplayerstruct = NewNativeStruct("WBPlayerStruct", nullptr);
auto wbplayerstruct = NewStruct("WBPlayerStruct", nullptr, true);
wbplayerstruct->Size = sizeof(wbplayerstruct_t);
wbplayerstruct->Align = alignof(wbplayerstruct_t);

View file

@ -513,7 +513,7 @@ void ZCCCompiler::CreateStructTypes()
}
else if (s->strct->Flags & ZCC_Native)
{
s->strct->Type = NewNativeStruct(s->NodeName(), outer);
s->strct->Type = NewStruct(s->NodeName(), outer, true);
}
else
{
@ -1315,7 +1315,8 @@ bool ZCCCompiler::CompileFields(PStruct *type, TArray<ZCC_VarDeclarator *> &Fiel
Error(field, "The member variable '%s.%s' has not been exported from the executable.", type == nullptr? "" : type->TypeName.GetChars(), FName(name->Name).GetChars());
}
// For native structs a size check cannot be done because they normally have no size. But for a native reference they are still fine.
else if (thisfieldtype->Size != ~0u && fd->FieldSize != ~0u && thisfieldtype->Size != fd->FieldSize && fd->BitValue == 0 && !thisfieldtype->IsA(RUNTIME_CLASS(PNativeStruct)))
else if (thisfieldtype->Size != ~0u && fd->FieldSize != ~0u && thisfieldtype->Size != fd->FieldSize && fd->BitValue == 0 &&
(!thisfieldtype->IsA(RUNTIME_CLASS(PStruct)) || !static_cast<PStruct*>(thisfieldtype)->isNative))
{
Error(field, "The member variable '%s.%s' has mismatching sizes in internal and external declaration. (Internal = %d, External = %d)", type == nullptr ? "" : type->TypeName.GetChars(), FName(name->Name).GetChars(), fd->FieldSize, thisfieldtype->Size);
}
@ -1702,10 +1703,14 @@ PType *ZCCCompiler::ResolveUserType(ZCC_BasicType *type, PSymbolTable *symt, boo
{
if (!nativetype) return TypeSInt32; // hack this to an integer until we can resolve the enum mess.
}
if (ptype->IsKindOf(RUNTIME_CLASS(PNativeStruct))) // native structs and classes cannot be instantiated, they always get used as reference.
else if (ptype->IsKindOf(RUNTIME_CLASS(PClass))) // classes cannot be instantiated at all, they always get used as references.
{
return NewPointer(ptype, type->isconst);
}
else if (ptype->IsKindOf(RUNTIME_CLASS(PStruct)) && static_cast<PStruct*>(ptype)->isNative) // native structs and classes cannot be instantiated, they always get used as reference.
{
if (!nativetype) return NewPointer(ptype, type->isconst);
if (!ptype->IsKindOf(RUNTIME_CLASS(PClass))) return ptype; // instantiation of native structs. Only for internal use.
return ptype; // instantiation of native structs. Only for internal use.
}
if (!nativetype) return ptype;
}