From c12dfd7e4dc533dd0d13167452dfa6185c276c56 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Tue, 24 Jan 2017 10:04:46 +0100 Subject: [PATCH] - fixed: only explicit class type casts must obey strict namespace rules, i.e. only '(class)(variable_to_cast)' The general rule is as follows: A class name as a string will always be looked up fully, even if the class name gets shadows by another variable because strings are not identifiers. It is only class names as identifiers that must obey the rule that if it is not known yet or hidden by something else that it may not be found to ensure that the older variable does not take over the name if it gets reused. --- src/scripting/codegeneration/codegen.cpp | 15 +++++++++------ src/scripting/codegeneration/codegen.h | 3 ++- src/scripting/decorate/thingdef_parse.cpp | 2 +- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/scripting/codegeneration/codegen.cpp b/src/scripting/codegeneration/codegen.cpp index 1d16b182ff..d090fa6e42 100644 --- a/src/scripting/codegeneration/codegen.cpp +++ b/src/scripting/codegeneration/codegen.cpp @@ -1613,7 +1613,7 @@ FxExpression *FxTypeCast::Resolve(FCompileContext &ctx) } else if (ValueType->IsKindOf(RUNTIME_CLASS(PClassPointer))) { - FxExpression *x = new FxClassTypeCast(static_cast(ValueType), basex); + FxExpression *x = new FxClassTypeCast(static_cast(ValueType), basex, Explicit); x = x->Resolve(ctx); basex = nullptr; delete this; @@ -4412,7 +4412,7 @@ FxExpression *FxTypeCheck::Resolve(FCompileContext& ctx) if (left->ValueType->IsKindOf(RUNTIME_CLASS(PClassPointer))) { - left = new FxClassTypeCast(NewClassPointer(RUNTIME_CLASS(DObject)), left); + left = new FxClassTypeCast(NewClassPointer(RUNTIME_CLASS(DObject)), left, false); ClassCheck = true; } else @@ -4420,7 +4420,7 @@ FxExpression *FxTypeCheck::Resolve(FCompileContext& ctx) left = new FxTypeCast(left, NewPointer(RUNTIME_CLASS(DObject)), false); ClassCheck = false; } - right = new FxClassTypeCast(NewClassPointer(RUNTIME_CLASS(DObject)), right); + right = new FxClassTypeCast(NewClassPointer(RUNTIME_CLASS(DObject)), right, false); RESOLVE(left, ctx); RESOLVE(right, ctx); @@ -9922,12 +9922,13 @@ VMFunction *FxReturnStatement::GetDirectFunction() // //========================================================================== -FxClassTypeCast::FxClassTypeCast(PClassPointer *dtype, FxExpression *x) +FxClassTypeCast::FxClassTypeCast(PClassPointer *dtype, FxExpression *x, bool explicitily) : FxExpression(EFX_ClassTypeCast, x->ScriptPosition) { ValueType = dtype; desttype = dtype->ClassRestriction; basex=x; + Explicit = explicitily; } //========================================================================== @@ -9991,7 +9992,9 @@ FxExpression *FxClassTypeCast::Resolve(FCompileContext &ctx) if (clsname != NAME_None) { - cls = FindClassType(clsname, ctx); + if (Explicit) cls = FindClassType(clsname, ctx); + else cls = PClass::FindClass(clsname); + if (cls == nullptr) { /* lax */ @@ -10141,7 +10144,7 @@ FxExpression *FxClassPtrCast::Resolve(FCompileContext &ctx) } else if (basex->ValueType == TypeString || basex->ValueType == TypeName) { - FxExpression *x = new FxClassTypeCast(to, basex); + FxExpression *x = new FxClassTypeCast(to, basex, true); basex = nullptr; delete this; return x->Resolve(ctx); diff --git a/src/scripting/codegeneration/codegen.h b/src/scripting/codegeneration/codegen.h index 7c18589c51..42d5b0ccfd 100644 --- a/src/scripting/codegeneration/codegen.h +++ b/src/scripting/codegeneration/codegen.h @@ -1913,10 +1913,11 @@ class FxClassTypeCast : public FxExpression { PClass *desttype; FxExpression *basex; + bool Explicit; public: - FxClassTypeCast(PClassPointer *dtype, FxExpression *x); + FxClassTypeCast(PClassPointer *dtype, FxExpression *x, bool explicitly); ~FxClassTypeCast(); FxExpression *Resolve(FCompileContext&); ExpEmit Emit(VMFunctionBuilder *build); diff --git a/src/scripting/decorate/thingdef_parse.cpp b/src/scripting/decorate/thingdef_parse.cpp index 81ab765f5d..9b78ef3d57 100644 --- a/src/scripting/decorate/thingdef_parse.cpp +++ b/src/scripting/decorate/thingdef_parse.cpp @@ -197,7 +197,7 @@ FxExpression *ParseParameter(FScanner &sc, PClassActor *cls, PType *type) sc.SetEscape(true); sc.MustGetString(); sc.SetEscape(false); - x = new FxClassTypeCast(static_cast(type), new FxConstant(FName(sc.String), sc)); + x = new FxClassTypeCast(static_cast(type), new FxConstant(FName(sc.String), sc), false); } else {