From 35c44c7e21f230000a31db51acfb7916a2683599 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Lu=C3=ADs=20Vaz=20Silva?= Date: Sun, 2 Mar 2025 15:19:43 -0300 Subject: [PATCH] don't allow backing types of string/array/map/etc to be referenced as actual types --- src/common/scripting/core/types.h | 2 + src/common/scripting/frontend/zcc-parse.lemon | 1 + src/common/scripting/frontend/zcc_compile.cpp | 29 +++++++-- src/common/scripting/frontend/zcc_compile.h | 2 +- wadsrc/static/zscript/engine/base.zs | 6 +- wadsrc/static/zscript/engine/dynarrays.zs | 16 ++--- wadsrc/static/zscript/engine/maps.zs | 64 +++++++++---------- 7 files changed, 72 insertions(+), 48 deletions(-) diff --git a/src/common/scripting/core/types.h b/src/common/scripting/core/types.h index 2568367e6e..5dc8fd1820 100644 --- a/src/common/scripting/core/types.h +++ b/src/common/scripting/core/types.h @@ -110,6 +110,8 @@ public: EScopeFlags ScopeFlags = (EScopeFlags)0; bool SizeKnown = true; + bool TypeInternal = false; + PType * LocalType = nullptr; PType * SetLocalType(PType * LocalType) { this->LocalType = LocalType; return this; } diff --git a/src/common/scripting/frontend/zcc-parse.lemon b/src/common/scripting/frontend/zcc-parse.lemon index ab46782aac..c6781db12b 100644 --- a/src/common/scripting/frontend/zcc-parse.lemon +++ b/src/common/scripting/frontend/zcc-parse.lemon @@ -432,6 +432,7 @@ struct_flags(X) ::= struct_flags(A) UI. { X.Flags = A.Flags | ZCC_UIFlag; } struct_flags(X) ::= struct_flags(A) PLAY. { X.Flags = A.Flags | ZCC_Play; } struct_flags(X) ::= struct_flags(A) CLEARSCOPE. { X.Flags = A.Flags | ZCC_ClearScope; } struct_flags(X) ::= struct_flags(A) NATIVE. { X.Flags = A.Flags | ZCC_Native; } +struct_flags(X) ::= struct_flags(A) INTERNAL. { X.Flags = A.Flags | ZCC_Internal; } struct_flags(X) ::= struct_flags(A) VERSION LPAREN STRCONST(C) RPAREN. { X.Flags = A.Flags | ZCC_Version; X.Version = C.String->GetChars(); } opt_struct_body(X) ::= . { X = NULL; } diff --git a/src/common/scripting/frontend/zcc_compile.cpp b/src/common/scripting/frontend/zcc_compile.cpp index 8a432e8c10..dfe9dfa13b 100644 --- a/src/common/scripting/frontend/zcc_compile.cpp +++ b/src/common/scripting/frontend/zcc_compile.cpp @@ -729,6 +729,8 @@ void ZCCCompiler::CreateStructTypes() syms = &OutNamespace->Symbols; } + + if (s->NodeName() == NAME__ && fileSystem.GetFileContainer(Lump) == 0) { // This is just a container for syntactic purposes. @@ -743,11 +745,17 @@ void ZCCCompiler::CreateStructTypes() { s->strct->Type = NewStruct(s->NodeName(), outer, false, AST.FileNo); } + if (s->strct->Flags & ZCC_Version) { s->strct->Type->mVersion = s->strct->Version; } + if (s->strct->Flags & ZCC_Internal) + { + s->strct->Type->TypeInternal = true; + } + auto &sf = s->Type()->ScopeFlags; if (mVersion >= MakeVersion(2, 4, 0)) { @@ -809,6 +817,12 @@ void ZCCCompiler::CreateClassTypes() PClass *parent; auto ParentName = c->cls->ParentName; + if (c->cls->Flags & ZCC_Internal) + { + Error(c->cls, "'Internal' not allowed for classes"); + } + + // The parent exists, we may create a type for this class if (ParentName != nullptr && ParentName->SiblingNext == ParentName) { parent = PClass::FindClass(ParentName->Id); @@ -845,7 +859,6 @@ void ZCCCompiler::CreateClassTypes() Error(c->cls, "Class '%s' cannot extend sealed class '%s'", FName(c->NodeName()).GetChars(), parent->TypeName.GetChars()); } - // The parent exists, we may create a type for this class if (c->cls->Flags & ZCC_Native) { // If this is a native class, its own type must also already exist and not be a runtime class. @@ -1868,7 +1881,7 @@ PType *ZCCCompiler::DetermineType(PType *outertype, ZCC_TreeNode *field, FName n { Error(field, "%s: @ not allowed for user scripts", name.GetChars()); } - retval = ResolveUserType(btype, btype->UserType, outertype? &outertype->Symbols : nullptr, true); + retval = ResolveUserType(outertype, btype, btype->UserType, outertype? &outertype->Symbols : nullptr, true); break; case ZCC_UserType: @@ -1898,7 +1911,7 @@ PType *ZCCCompiler::DetermineType(PType *outertype, ZCC_TreeNode *field, FName n default: - retval = ResolveUserType(btype, btype->UserType, outertype ? &outertype->Symbols : nullptr, false); + retval = ResolveUserType(outertype, btype, btype->UserType, outertype ? &outertype->Symbols : nullptr, false); break; } break; @@ -2153,7 +2166,7 @@ PType *ZCCCompiler::DetermineType(PType *outertype, ZCC_TreeNode *field, FName n * @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) +PType *ZCCCompiler::ResolveUserType(PType *outertype, ZCC_BasicType *type, ZCC_Identifier *id, PSymbolTable *symt, bool nativetype) { // Check the symbol table for the identifier. PSymbol *sym = nullptr; @@ -2170,10 +2183,18 @@ PType *ZCCCompiler::ResolveUserType(ZCC_BasicType *type, ZCC_Identifier *id, PSy return TypeError; } + //only allow references to internal types inside internal types + if (ptype->TypeInternal && !outertype->TypeInternal) + { + Error(type, "Type %s not accessible", FName(type->UserType->Id).GetChars()); + return TypeError; + } + if (id->SiblingNext != type->UserType) { assert(id->SiblingNext->NodeType == AST_Identifier); ptype = ResolveUserType( + outertype, type, static_cast(id->SiblingNext), &ptype->Symbols, diff --git a/src/common/scripting/frontend/zcc_compile.h b/src/common/scripting/frontend/zcc_compile.h index 19e9f8ff07..bf14071890 100644 --- a/src/common/scripting/frontend/zcc_compile.h +++ b/src/common/scripting/frontend/zcc_compile.h @@ -134,7 +134,7 @@ 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, ZCC_Identifier *id, PSymbolTable *sym, bool nativetype); + PType *ResolveUserType(PType *outertype, 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); diff --git a/wadsrc/static/zscript/engine/base.zs b/wadsrc/static/zscript/engine/base.zs index fa212ba38f..14cedaed4e 100644 --- a/wadsrc/static/zscript/engine/base.zs +++ b/wadsrc/static/zscript/engine/base.zs @@ -189,7 +189,7 @@ struct Vector3 } */ -struct _ native // These are the global variables, the struct is only here to avoid extending the parser for this. +struct _ native internal // These are the global variables, the struct is only here to avoid extending the parser for this. { native readonly Array AllClasses; native internal readonly Map AllServices; @@ -903,7 +903,7 @@ enum EmptyTokenType // Although String is a builtin type, this is a convenient way to attach methods to it. // All of these methods are available on strings -struct StringStruct native +struct StringStruct native internal { native static vararg String Format(String fmt, ...); native vararg void AppendFormat(String fmt, ...); @@ -952,7 +952,7 @@ struct Translation version("2.4") } // Convenient way to attach functions to Quat -struct QuatStruct native +struct QuatStruct native internal { native static Quat SLerp(Quat from, Quat to, double t); native static Quat NLerp(Quat from, Quat to, double t); diff --git a/wadsrc/static/zscript/engine/dynarrays.zs b/wadsrc/static/zscript/engine/dynarrays.zs index f9e6a680d3..28f36b313c 100644 --- a/wadsrc/static/zscript/engine/dynarrays.zs +++ b/wadsrc/static/zscript/engine/dynarrays.zs @@ -1,7 +1,7 @@ // The VM uses 7 integral data types, so for dynamic array support we need one specific set of functions for each of these types. // Do not use these structs directly, they are incomplete and only needed to create prototypes for the needed functions. -struct DynArray_I8 native +struct DynArray_I8 native internal { native readonly int Size; @@ -21,7 +21,7 @@ struct DynArray_I8 native native void Clear (); } -struct DynArray_I16 native +struct DynArray_I16 native internal { native readonly int Size; @@ -41,7 +41,7 @@ struct DynArray_I16 native native void Clear (); } -struct DynArray_I32 native +struct DynArray_I32 native internal { native readonly int Size; @@ -62,7 +62,7 @@ struct DynArray_I32 native native void Clear (); } -struct DynArray_F32 native +struct DynArray_F32 native internal { native readonly int Size; @@ -82,7 +82,7 @@ struct DynArray_F32 native native void Clear (); } -struct DynArray_F64 native +struct DynArray_F64 native internal { native readonly int Size; @@ -102,7 +102,7 @@ struct DynArray_F64 native native void Clear (); } -struct DynArray_Ptr native +struct DynArray_Ptr native internal { native readonly int Size; @@ -122,7 +122,7 @@ struct DynArray_Ptr native native void Clear (); } -struct DynArray_Obj native +struct DynArray_Obj native internal { native readonly int Size; @@ -142,7 +142,7 @@ struct DynArray_Obj native native void Clear (); } -struct DynArray_String native +struct DynArray_String native internal { native readonly int Size; diff --git a/wadsrc/static/zscript/engine/maps.zs b/wadsrc/static/zscript/engine/maps.zs index b871edf050..0e71c3b05f 100644 --- a/wadsrc/static/zscript/engine/maps.zs +++ b/wadsrc/static/zscript/engine/maps.zs @@ -1,5 +1,5 @@ -struct Map_I32_I8 native +struct Map_I32_I8 native internal { native void Copy(Map_I32_I8 other); native void Move(Map_I32_I8 other); @@ -18,7 +18,7 @@ struct Map_I32_I8 native native void Remove(int key); } -struct MapIterator_I32_I8 native +struct MapIterator_I32_I8 native internal { native bool Init(Map_I32_I8 other); native bool ReInit(); @@ -31,7 +31,7 @@ struct MapIterator_I32_I8 native native void SetValue(int value); } -struct Map_I32_I16 native +struct Map_I32_I16 native internal { native void Copy(Map_I32_I16 other); native void Move(Map_I32_I16 other); @@ -50,7 +50,7 @@ struct Map_I32_I16 native native void Remove(int key); } -struct MapIterator_I32_I16 native +struct MapIterator_I32_I16 native internal { native bool Init(Map_I32_I16 other); native bool ReInit(); @@ -63,7 +63,7 @@ struct MapIterator_I32_I16 native native void SetValue(int value); } -struct Map_I32_I32 native +struct Map_I32_I32 native internal { native void Copy(Map_I32_I32 other); native void Move(Map_I32_I32 other); @@ -82,7 +82,7 @@ struct Map_I32_I32 native native void Remove(int key); } -struct MapIterator_I32_I32 native +struct MapIterator_I32_I32 native internal { native bool Init(Map_I32_I32 other); native bool ReInit(); @@ -95,7 +95,7 @@ struct MapIterator_I32_I32 native native void SetValue(int value); } -struct Map_I32_F32 native +struct Map_I32_F32 native internal { native void Copy(Map_I32_F32 other); native void Move(Map_I32_F32 other); @@ -114,7 +114,7 @@ struct Map_I32_F32 native native void Remove(int key); } -struct MapIterator_I32_F32 native +struct MapIterator_I32_F32 native internal { native bool Init(Map_I32_F32 other); native bool ReInit(); @@ -127,7 +127,7 @@ struct MapIterator_I32_F32 native native void SetValue(double value); } -struct Map_I32_F64 native +struct Map_I32_F64 native internal { native void Copy(Map_I32_F64 other); native void Move(Map_I32_F64 other); @@ -146,7 +146,7 @@ struct Map_I32_F64 native native void Remove(int key); } -struct MapIterator_I32_F64 native +struct MapIterator_I32_F64 native internal { native bool Init(Map_I32_F64 other); native bool ReInit(); @@ -159,7 +159,7 @@ struct MapIterator_I32_F64 native native void SetValue(double value); } -struct Map_I32_Obj native +struct Map_I32_Obj native internal { native void Copy(Map_I32_Obj other); native void Move(Map_I32_Obj other); @@ -178,7 +178,7 @@ struct Map_I32_Obj native native void Remove(int key); } -struct MapIterator_I32_Obj native +struct MapIterator_I32_Obj native internal { native bool Init(Map_I32_Obj other); native bool ReInit(); @@ -191,7 +191,7 @@ struct MapIterator_I32_Obj native native void SetValue(Object value); } -struct Map_I32_Ptr native +struct Map_I32_Ptr native internal { native void Copy(Map_I32_Ptr other); native void Move(Map_I32_Ptr other); @@ -210,7 +210,7 @@ struct Map_I32_Ptr native native void Remove(int key); } -struct MapIterator_I32_Ptr native +struct MapIterator_I32_Ptr native internal { native bool Init(Map_I32_Ptr other); native bool Next(); @@ -220,7 +220,7 @@ struct MapIterator_I32_Ptr native native void SetValue(voidptr value); } -struct Map_I32_Str native +struct Map_I32_Str native internal { native void Copy(Map_I32_Str other); native void Move(Map_I32_Str other); @@ -239,7 +239,7 @@ struct Map_I32_Str native native void Remove(int key); } -struct MapIterator_I32_Str native +struct MapIterator_I32_Str native internal { native bool Init(Map_I32_Str other); native bool ReInit(); @@ -254,7 +254,7 @@ struct MapIterator_I32_Str native // --------------- -struct Map_Str_I8 native +struct Map_Str_I8 native internal { native void Copy(Map_Str_I8 other); native void Move(Map_Str_I8 other); @@ -273,7 +273,7 @@ struct Map_Str_I8 native native void Remove(String key); } -struct MapIterator_Str_I8 native +struct MapIterator_Str_I8 native internal { native bool Init(Map_Str_I8 other); native bool ReInit(); @@ -286,7 +286,7 @@ struct MapIterator_Str_I8 native native void SetValue(int value); } -struct Map_Str_I16 native +struct Map_Str_I16 native internal { native void Copy(Map_Str_I16 other); native void Move(Map_Str_I16 other); @@ -305,7 +305,7 @@ struct Map_Str_I16 native native void Remove(String key); } -struct MapIterator_Str_I16 native +struct MapIterator_Str_I16 native internal { native bool Init(Map_Str_I16 other); native bool ReInit(); @@ -318,7 +318,7 @@ struct MapIterator_Str_I16 native native void SetValue(int value); } -struct Map_Str_I32 native +struct Map_Str_I32 native internal { native void Copy(Map_Str_I32 other); native void Move(Map_Str_I32 other); @@ -337,7 +337,7 @@ struct Map_Str_I32 native native void Remove(String key); } -struct MapIterator_Str_I32 native +struct MapIterator_Str_I32 native internal { native bool Init(Map_Str_I32 other); native bool ReInit(); @@ -350,7 +350,7 @@ struct MapIterator_Str_I32 native native void SetValue(int value); } -struct Map_Str_F32 native +struct Map_Str_F32 native internal { native void Copy(Map_Str_F32 other); native void Move(Map_Str_F32 other); @@ -369,7 +369,7 @@ struct Map_Str_F32 native native void Remove(String key); } -struct MapIterator_Str_F32 native +struct MapIterator_Str_F32 native internal { native bool Init(Map_Str_F32 other); native bool ReInit(); @@ -382,7 +382,7 @@ struct MapIterator_Str_F32 native native void SetValue(double value); } -struct Map_Str_F64 native +struct Map_Str_F64 native internal { native void Copy(Map_Str_F64 other); native void Move(Map_Str_F64 other); @@ -401,7 +401,7 @@ struct Map_Str_F64 native native void Remove(String key); } -struct MapIterator_Str_F64 native +struct MapIterator_Str_F64 native internal { native bool Init(Map_Str_F64 other); native bool ReInit(); @@ -414,7 +414,7 @@ struct MapIterator_Str_F64 native native void SetValue(double value); } -struct Map_Str_Obj native +struct Map_Str_Obj native internal { native void Copy(Map_Str_Obj other); native void Move(Map_Str_Obj other); @@ -433,7 +433,7 @@ struct Map_Str_Obj native native void Remove(String key); } -struct MapIterator_Str_Obj native +struct MapIterator_Str_Obj native internal { native bool Init(Map_Str_Obj other); native bool ReInit(); @@ -446,7 +446,7 @@ struct MapIterator_Str_Obj native native void SetValue(Object value); } -struct Map_Str_Ptr native +struct Map_Str_Ptr native internal { native void Copy(Map_Str_Ptr other); native void Move(Map_Str_Ptr other); @@ -465,7 +465,7 @@ struct Map_Str_Ptr native native void Remove(String key); } -struct MapIterator_Str_Ptr native +struct MapIterator_Str_Ptr native internal { native bool Init(Map_Str_Ptr other); native bool ReInit(); @@ -478,7 +478,7 @@ struct MapIterator_Str_Ptr native native void SetValue(voidptr value); } -struct Map_Str_Str native +struct Map_Str_Str native internal { native void Copy(Map_Str_Str other); native void Move(Map_Str_Str other); @@ -497,7 +497,7 @@ struct Map_Str_Str native native void Remove(String key); } -struct MapIterator_Str_Str native +struct MapIterator_Str_Str native internal { native bool Init(Map_Str_Str other); native bool ReInit();