From 77bc5999ce17dcc0eb39bae01e7c679c99a084f3 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Thu, 11 Aug 2022 22:28:40 +0200 Subject: [PATCH] - Backend update from GZDoom. * ZScript compiler fixes for type promotion * FileSystem.FindLumpFullName * Statusbar text scaling fixes. * removed scalar addition operators from vectors. --- .../common/scripting/frontend/zcc_compile.cpp | 69 ++++++++++++++++--- .../common/scripting/frontend/zcc_compile.h | 3 +- .../common/scripting/interface/vmnatives.cpp | 10 +++ source/common/statusbar/base_sbar.cpp | 14 ++-- source/common/utility/vectors.h | 4 ++ wadsrc/static/zscript/engine/base.zs | 1 + wadsrc/static/zscript/engine/dynarrays.zs | 64 ++++++++--------- .../zscript/engine/ui/menu/optionmenuitems.zs | 2 +- 8 files changed, 117 insertions(+), 50 deletions(-) diff --git a/source/common/scripting/frontend/zcc_compile.cpp b/source/common/scripting/frontend/zcc_compile.cpp index 16940339a..00cc4ace4 100644 --- a/source/common/scripting/frontend/zcc_compile.cpp +++ b/source/common/scripting/frontend/zcc_compile.cpp @@ -1813,7 +1813,7 @@ PType *ZCCCompiler::DetermineType(PType *outertype, ZCC_TreeNode *field, FName n { Error(field, "%s: @ not allowed for user scripts", name.GetChars()); } - retval = ResolveUserType(btype, outertype? &outertype->Symbols : nullptr, true); + retval = ResolveUserType(btype, btype->UserType, outertype? &outertype->Symbols : nullptr, true); break; case ZCC_UserType: @@ -1837,7 +1837,7 @@ PType *ZCCCompiler::DetermineType(PType *outertype, ZCC_TreeNode *field, FName n break; default: - retval = ResolveUserType(btype, outertype ? &outertype->Symbols : nullptr, false); + retval = ResolveUserType(btype, btype->UserType, outertype ? &outertype->Symbols : nullptr, false); break; } break; @@ -1936,18 +1936,25 @@ PType *ZCCCompiler::DetermineType(PType *outertype, ZCC_TreeNode *field, FName n // // ZCCCompiler :: ResolveUserType // -// resolves a user type and returns a matching PType -// //========================================================================== -PType *ZCCCompiler::ResolveUserType(ZCC_BasicType *type, PSymbolTable *symt, bool nativetype) +/** +* Resolves a user type and returns a matching PType. +* +* @param type The tree node with the identifiers to look for. +* @param type The current identifier being looked for. This must be in type's UserType list. +* @param symt The symbol table to search in. If id is the first identifier and not found in symt, then OutNamespace will also be searched. +* @param nativetype Distinguishes between searching for a native type or a user type. +* @returns the PType found for this user type +*/ +PType *ZCCCompiler::ResolveUserType(ZCC_BasicType *type, ZCC_Identifier *id, PSymbolTable *symt, bool nativetype) { // Check the symbol table for the identifier. PSymbol *sym = nullptr; // We first look in the current class and its parents, and then in the current namespace and its parents. - if (symt != nullptr) sym = symt->FindSymbol(type->UserType->Id, true); - if (sym == nullptr) sym = OutNamespace->Symbols.FindSymbol(type->UserType->Id, true); + if (symt != nullptr) sym = symt->FindSymbol(id->Id, true); + if (sym == nullptr && type->UserType == id) sym = OutNamespace->Symbols.FindSymbol(id->Id, true); if (sym != nullptr && sym->IsKindOf(RUNTIME_CLASS(PSymbolType))) { auto ptype = static_cast(sym)->Type; @@ -1957,6 +1964,21 @@ PType *ZCCCompiler::ResolveUserType(ZCC_BasicType *type, PSymbolTable *symt, boo return TypeError; } + if (id->SiblingNext != type->UserType) + { + assert(id->SiblingNext->NodeType == AST_Identifier); + ptype = ResolveUserType( + type, + static_cast(id->SiblingNext), + &ptype->Symbols, + nativetype + ); + if (ptype == TypeError) + { + return ptype; + } + } + if (ptype->isEnum()) { if (!nativetype) return TypeSInt32; // hack this to an integer until we can resolve the enum mess. @@ -1972,11 +1994,36 @@ PType *ZCCCompiler::ResolveUserType(ZCC_BasicType *type, PSymbolTable *symt, boo } if (!nativetype) return ptype; } - Error(type, "Unable to resolve %s%s as type.", nativetype? "@" : "", FName(type->UserType->Id).GetChars()); + Error(type, "Unable to resolve %s%s as a type.", nativetype? "@" : "", UserTypeName(type).GetChars()); return TypeError; } +//========================================================================== +// +// ZCCCompiler :: UserTypeName STATIC +// +// Returns the full name for a UserType node. +// +//========================================================================== + +FString ZCCCompiler::UserTypeName(ZCC_BasicType *type) +{ + FString out; + ZCC_Identifier *id = type->UserType; + + do + { + assert(id->NodeType == AST_Identifier); + if (out.Len() > 0) + { + out += '.'; + } + out += FName(id->Id).GetChars(); + } while ((id = static_cast(id->SiblingNext)) != type->UserType); + return out; +} + //========================================================================== // // ZCCCompiler :: ResolveArraySize @@ -2318,7 +2365,11 @@ void ZCCCompiler::CompileFunction(ZCC_StructWork *c, ZCC_FuncDeclarator *f, bool } if (type->GetRegType() == REGT_NIL && type != TypeVector2 && type != TypeVector3 && type != TypeFVector2 && type != TypeFVector3) { - Error(p, "Invalid type %s for function parameter", type->DescriptiveName()); + // If it's TypeError, then an error was already given + if (type != TypeError) + { + Error(p, "Invalid type %s for function parameter", type->DescriptiveName()); + } } else if (p->Default != nullptr) { diff --git a/source/common/scripting/frontend/zcc_compile.h b/source/common/scripting/frontend/zcc_compile.h index adfadaff1..b6f32018d 100644 --- a/source/common/scripting/frontend/zcc_compile.h +++ b/source/common/scripting/frontend/zcc_compile.h @@ -134,7 +134,8 @@ protected: FString FlagsToString(uint32_t flags); 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); + PType *ResolveUserType(ZCC_BasicType *type, ZCC_Identifier *id, PSymbolTable *sym, bool nativetype); + static FString UserTypeName(ZCC_BasicType *type); TArray OrderStructs(); void AddStruct(TArray &new_order, ZCC_StructWork *struct_def); ZCC_StructWork *StructTypeToWork(const PStruct *type) const; diff --git a/source/common/scripting/interface/vmnatives.cpp b/source/common/scripting/interface/vmnatives.cpp index 5a7d25cce..8f9f8dfad 100644 --- a/source/common/scripting/interface/vmnatives.cpp +++ b/source/common/scripting/interface/vmnatives.cpp @@ -758,6 +758,16 @@ DEFINE_ACTION_FUNCTION(_Wads, FindLump) ACTION_RETURN_INT(isLumpValid ? fileSystem.FindLump(name, &startlump, 0 != ns) : -1); } +DEFINE_ACTION_FUNCTION(_Wads, FindLumpFullName) +{ + PARAM_PROLOGUE; + PARAM_STRING(name); + PARAM_INT(startlump); + PARAM_BOOL(noext); + const bool isLumpValid = startlump >= 0 && startlump < fileSystem.GetNumEntries(); + ACTION_RETURN_INT(isLumpValid ? fileSystem.FindLumpFullName(name, &startlump, noext) : -1); +} + DEFINE_ACTION_FUNCTION(_Wads, GetLumpName) { PARAM_PROLOGUE; diff --git a/source/common/statusbar/base_sbar.cpp b/source/common/statusbar/base_sbar.cpp index 68fe29230..bac2bbd3a 100644 --- a/source/common/statusbar/base_sbar.cpp +++ b/source/common/statusbar/base_sbar.cpp @@ -745,7 +745,7 @@ void DStatusBarCore::DrawString(FFont* font, const FString& cstring, double x, d { if (ch == ' ') { - x += monospaced ? spacing : font->GetSpaceWidth() + spacing; + x += (monospaced ? spacing : font->GetSpaceWidth() + spacing) * scaleX; continue; } else if (ch == TEXTCOLOR_ESCAPE) @@ -765,7 +765,7 @@ void DStatusBarCore::DrawString(FFont* font, const FString& cstring, double x, d width += font->GetDefaultKerning(); if (!monospaced) //If we are monospaced lets use the offset - x += (c->GetDisplayLeftOffset() + 1); //ignore x offsets since we adapt to character size + x += (c->GetDisplayLeftOffset() * scaleX + 1); //ignore x offsets since we adapt to character size double rx, ry, rw, rh; rx = x + drawOffset.X; @@ -816,12 +816,12 @@ void DStatusBarCore::DrawString(FFont* font, const FString& cstring, double x, d DTA_LegacyRenderStyle, ERenderStyle(style), TAG_DONE); - dx = monospaced - ? spacing - : width + spacing - (c->GetDisplayLeftOffset() + 1); - // Take text scale into account - x += dx * scaleX; + dx = monospaced + ? spacing * scaleX + : (double(width) + spacing - c->GetDisplayLeftOffset()) * scaleX - 1; + + x += dx; } } diff --git a/source/common/utility/vectors.h b/source/common/utility/vectors.h index cb927b4c2..48f85bafa 100644 --- a/source/common/utility/vectors.h +++ b/source/common/utility/vectors.h @@ -141,11 +141,13 @@ struct TVector2 } // Scalar addition +#if 0 TVector2 &operator+= (double scalar) { X += scalar, Y += scalar; return *this; } +#endif friend TVector2 operator+ (const TVector2 &v, vec_t scalar) { @@ -385,11 +387,13 @@ struct TVector3 } // Scalar addition +#if 0 TVector3 &operator+= (vec_t scalar) { X += scalar, Y += scalar, Z += scalar; return *this; } +#endif friend TVector3 operator+ (const TVector3 &v, vec_t scalar) { diff --git a/wadsrc/static/zscript/engine/base.zs b/wadsrc/static/zscript/engine/base.zs index a4bd15208..75fb62b9d 100644 --- a/wadsrc/static/zscript/engine/base.zs +++ b/wadsrc/static/zscript/engine/base.zs @@ -830,6 +830,7 @@ struct Wads // todo: make FileSystem an alias to 'Wads' native static int CheckNumForName(string name, int ns, int wadnum = -1, bool exact = false); native static int CheckNumForFullName(string name); native static int FindLump(string name, int startlump = 0, FindLumpNamespace ns = GlobalNamespace); + native static int FindLumpFullName(string name, int startlump = 0, bool noext = false); native static string ReadLump(int lump); native static int GetNumLumps(); diff --git a/wadsrc/static/zscript/engine/dynarrays.zs b/wadsrc/static/zscript/engine/dynarrays.zs index 85d135d87..f9e6a680d 100644 --- a/wadsrc/static/zscript/engine/dynarrays.zs +++ b/wadsrc/static/zscript/engine/dynarrays.zs @@ -8,16 +8,16 @@ struct DynArray_I8 native native void Copy(DynArray_I8 other); native void Move(DynArray_I8 other); native void Append (DynArray_I8 other); - native uint Find(int item) const; - native uint Push (int item); + native int Find(int item) const; + native int Push(int item); native bool Pop (); native void Delete (uint index, int deletecount = 1); native void Insert (uint index, int item); native void ShrinkToFit (); native void Grow (uint amount); native void Resize (uint amount); - native uint Reserve (uint amount); - native uint Max () const; + native int Reserve(uint amount); + native int Max() const; native void Clear (); } @@ -28,16 +28,16 @@ struct DynArray_I16 native native void Copy(DynArray_I16 other); native void Move(DynArray_I16 other); native void Append (DynArray_I16 other); - native uint Find(int item) const; - native uint Push (int item); + native int Find(int item) const; + native int Push(int item); native bool Pop (); native void Delete (uint index, int deletecount = 1); native void Insert (uint index, int item); native void ShrinkToFit (); native void Grow (uint amount); native void Resize (uint amount); - native uint Reserve (uint amount); - native uint Max () const; + native int Reserve(uint amount); + native int Max() const; native void Clear (); } @@ -48,8 +48,8 @@ struct DynArray_I32 native native void Copy(DynArray_I32 other); native void Move(DynArray_I32 other); native void Append (DynArray_I32 other); - native uint Find(int item) const; - native uint Push (int item); + native int Find(int item) const; + native int Push(int item); native vararg uint PushV (int item, ...); native bool Pop (); native void Delete (uint index, int deletecount = 1); @@ -57,8 +57,8 @@ struct DynArray_I32 native native void ShrinkToFit (); native void Grow (uint amount); native void Resize (uint amount); - native uint Reserve (uint amount); - native uint Max () const; + native int Reserve(uint amount); + native int Max() const; native void Clear (); } @@ -69,16 +69,16 @@ struct DynArray_F32 native native void Copy(DynArray_F32 other); native void Move(DynArray_F32 other); native void Append (DynArray_F32 other); - native uint Find(double item) const; - native uint Push (double item); + native int Find(double item) const; + native int Push(double item); native bool Pop (); native void Delete (uint index, int deletecount = 1); native void Insert (uint index, double item); native void ShrinkToFit (); native void Grow (uint amount); native void Resize (uint amount); - native uint Reserve (uint amount); - native uint Max () const; + native int Reserve(uint amount); + native int Max() const; native void Clear (); } @@ -89,16 +89,16 @@ struct DynArray_F64 native native void Copy(DynArray_F64 other); native void Move(DynArray_F64 other); native void Append (DynArray_F64 other); - native uint Find(double item) const; - native uint Push (double item); + native int Find(double item) const; + native int Push(double item); native bool Pop (); native void Delete (uint index, int deletecount = 1); native void Insert (uint index, double item); native void ShrinkToFit (); native void Grow (uint amount); native void Resize (uint amount); - native uint Reserve (uint amount); - native uint Max () const; + native int Reserve(uint amount); + native int Max() const; native void Clear (); } @@ -109,16 +109,16 @@ struct DynArray_Ptr native native void Copy(DynArray_Ptr other); native void Move(DynArray_Ptr other); native void Append (DynArray_Ptr other); - native uint Find(voidptr item) const; - native uint Push (voidptr item); + native int Find(voidptr item) const; + native int Push(voidptr item); native bool Pop (); native void Delete (uint index, int deletecount = 1); native void Insert (uint index, voidptr item); native void ShrinkToFit (); native void Grow (uint amount); native void Resize (uint amount); - native uint Reserve (uint amount); - native uint Max () const; + native int Reserve(uint amount); + native int Max() const; native void Clear (); } @@ -129,16 +129,16 @@ struct DynArray_Obj native native void Copy(DynArray_Obj other); native void Move(DynArray_Obj other); native void Append (DynArray_Obj other); - native uint Find(Object item) const; - native uint Push (Object item); + native int Find(Object item) const; + native int Push(Object item); native bool Pop (); native void Delete (uint index, int deletecount = 1); native void Insert (uint index, Object item); native void ShrinkToFit (); native void Grow (uint amount); native void Resize (uint amount); - native uint Reserve (uint amount); - native uint Max () const; + native int Reserve(uint amount); + native int Max() const; native void Clear (); } @@ -149,8 +149,8 @@ struct DynArray_String native native void Copy(DynArray_String other); native void Move(DynArray_String other); native void Append (DynArray_String other); - native uint Find(String item) const; - native uint Push (String item); + native int Find(String item) const; + native int Push(String item); native vararg uint PushV(String item, ...); native bool Pop (); native void Delete (uint index, int deletecount = 1); @@ -158,7 +158,7 @@ struct DynArray_String native native void ShrinkToFit (); native void Grow (uint amount); native void Resize (uint amount); - native uint Reserve (uint amount); - native uint Max () const; + native int Reserve(uint amount); + native int Max() const; native void Clear (); } diff --git a/wadsrc/static/zscript/engine/ui/menu/optionmenuitems.zs b/wadsrc/static/zscript/engine/ui/menu/optionmenuitems.zs index 02b9d0a89..4733d5b3b 100644 --- a/wadsrc/static/zscript/engine/ui/menu/optionmenuitems.zs +++ b/wadsrc/static/zscript/engine/ui/menu/optionmenuitems.zs @@ -304,7 +304,7 @@ class OptionMenuItemOptionBase : OptionMenuItem int Selection = GetSelection(); String text = StringTable.Localize(OptionValues.GetText(mValues, Selection)); - if (text.Length() == 0) text = "Unknown"; + if (text.Length() == 0) text = StringTable.Localize("$TXT_UNKNOWN"); drawValue(indent, y, OptionMenuSettings.mFontColorValue, text, isGrayed()); return indent; }