From 3726194fbe3da234eb30b6371e5b4ab1ea388f93 Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Sat, 2 Feb 2019 11:58:54 -0500 Subject: Feature/as refactor review (#821) * Replace dynamicCast with as where does not change behavior (ie not Type derived). Use free function where scoping is clear. * Replace uses of dynamicCast with as when there is no difference in behavior. * Remove the IsXXXX methods from Type. * Don't have separate smart pointer to store canonicalType on Type. * Simplify Slang.FilteredMemberRefList.Adjust, such does the cast directly. * Use free as where appropriate. * Use free function version of casts where appropriate. * Fix text in casting.md * Fix typos in decl-refs.md * Remove the uses of free function as on RefDecl. Add 'canAs' to RefDecl as a way to test if a cast is possible. Moved 'as' into RefDeclBase. * Use 'is' to test for as cast on smart pointers. Fix small scope issue. * * Cache stringType and enumTypeType on the Session * Make DeclRefType::Create return a RefPtr * Make casting of result use the *method* .as (cos using free function would mean objects being wrongly destroyed) * Make results from createInstance ref'd to avoid possible leaks. * Fix typo in template parameter for is on RefPtr. --- source/slang/check.cpp | 112 +++++++++++++++++++++++++------------------------ 1 file changed, 57 insertions(+), 55 deletions(-) (limited to 'source/slang/check.cpp') diff --git a/source/slang/check.cpp b/source/slang/check.cpp index 199e733ce..d1294841c 100644 --- a/source/slang/check.cpp +++ b/source/slang/check.cpp @@ -126,7 +126,7 @@ namespace Slang } else if (auto vectorType = as(typeIn)) { - if (auto elemCount = vectorType->elementCount.dynamicCast()) + if (auto elemCount = as(vectorType->elementCount)) { data.dim1 = elemCount->value - 1; auto elementBasicType = as(vectorType->elementType); @@ -583,7 +583,7 @@ namespace Slang // to the chosen interface decl must be the first substitution on // the list (which is a linked list from the "inside" out). // - auto thisTypeSubst = interfaceDeclRef.substitutions.substitutions.dynamicCast(); + auto thisTypeSubst = interfaceDeclRef.substitutions.substitutions.as(); if(thisTypeSubst && thisTypeSubst->interfaceDecl == interfaceDeclRef.decl) { // This isn't really an existential type, because somebody @@ -885,16 +885,16 @@ namespace Slang RefPtr ExpectATypeRepr(RefPtr expr) { - if (auto overloadedExpr = expr.dynamicCast()) + if (auto overloadedExpr = as(expr)) { expr = ResolveOverloadedExpr(overloadedExpr, LookupMask::type); } - if (auto typeType = as(expr->type.type)) + if (auto typeType = as(expr->type)) { return expr; } - else if (auto errorType = as(expr->type.type)) + else if (auto errorType = as(expr->type)) { return expr; } @@ -925,7 +925,7 @@ namespace Slang RefPtr ExtractGenericArgVal(RefPtr exp) { - if (auto overloadedExpr = exp.dynamicCast()) + if (auto overloadedExpr = as(exp)) { // assume that if it is overloaded, we want a type exp = ResolveOverloadedExpr(overloadedExpr, LookupMask::type); @@ -1043,7 +1043,7 @@ namespace Slang // this is a quick fix that at least alerts the user to how we are // interpreting their code. // - if (auto varDecl = decl.dynamicCast()) + if (auto varDecl = as(decl)) { if (auto parenScope = as(varDecl->ParentDecl)) { @@ -1076,7 +1076,7 @@ namespace Slang void EnusreAllDeclsRec(RefPtr decl) { checkDecl(decl); - if (auto containerDecl = decl.dynamicCast()) + if (auto containerDecl = as(decl)) { for (auto m : containerDecl->Members) { @@ -1111,7 +1111,7 @@ namespace Slang Type* type = typeExp.type.Ptr(); if(!type && typeExp.exp) { - if(auto typeType = typeExp.exp->type.type.dynamicCast()) + if(auto typeType = as(typeExp.exp->type)) { type = typeType->type; } @@ -1141,7 +1141,7 @@ namespace Slang List> args; for (RefPtr member : genericDeclRef.getDecl()->Members) { - if (auto typeParam = member.dynamicCast()) + if (auto typeParam = as(member)) { if (!typeParam->initType.exp) { @@ -1157,7 +1157,7 @@ namespace Slang if (outProperType) args.Add(typeParam->initType.exp); } - else if (auto valParam = member.dynamicCast()) + else if (auto valParam = as(member)) { if (!valParam->initExpr) { @@ -1274,11 +1274,11 @@ namespace Slang // Capture the "base" expression in case this is a member reference RefPtr GetBaseExpr(RefPtr expr) { - if (auto memberExpr = expr.dynamicCast()) + if (auto memberExpr = as(expr)) { return memberExpr->BaseExpression; } - else if(auto overloadedExpr = expr.dynamicCast()) + else if(auto overloadedExpr = as(expr)) { return overloadedExpr->base; } @@ -1293,17 +1293,17 @@ namespace Slang { if(left == right) return true; - if(auto leftConst = left.dynamicCast()) + if(auto leftConst = as(left)) { - if(auto rightConst = right.dynamicCast()) + if(auto rightConst = as(right)) { return leftConst->value == rightConst->value; } } - if(auto leftVar = left.dynamicCast()) + if(auto leftVar = as(left)) { - if(auto rightVar = right.dynamicCast()) + if(auto rightVar = as(right)) { return leftVar->declRef.Equals(rightVar->declRef); } @@ -1352,7 +1352,7 @@ namespace Slang if(auto declRefType = as(type)) { - if(declRefType->declRef.as()) + if(as(declRefType->declRef)) return false; } @@ -1366,7 +1366,7 @@ namespace Slang { // A nested initializer list should always be used directly. // - if(fromExpr.dynamicCast()) + if(as(fromExpr)) { return true; } @@ -1500,7 +1500,7 @@ namespace Slang auto toElementType = toVecType->elementType; UInt elementCount = 0; - if (auto constElementCount = toElementCount.dynamicCast()) + if (auto constElementCount = as(toElementCount)) { elementCount = (UInt) constElementCount->value; } @@ -1548,7 +1548,7 @@ namespace Slang // of elements being initialized matches what was declared. // UInt elementCount = 0; - if (auto constElementCount = toElementCount.dynamicCast()) + if (auto constElementCount = as(toElementCount)) { elementCount = (UInt) constElementCount->value; } @@ -1780,7 +1780,7 @@ namespace Slang } // Coercion from an initializer list is allowed for many types - if( auto fromInitializerListExpr = fromExpr.dynamicCast()) + if( auto fromInitializerListExpr = as(fromExpr)) { if(!tryCoerceInitializerList(toType, outToExpr, fromInitializerListExpr)) return false; @@ -2210,14 +2210,15 @@ namespace Slang } // Fill in default substitutions for the 'subtype' part of a type constraint decl - void CheckConstraintSubType(TypeExp & typeExp) + void CheckConstraintSubType(TypeExp& typeExp) { - if (auto sharedTypeExpr = typeExp.exp.dynamicCast()) + if (auto sharedTypeExpr = as(typeExp.exp)) { if (auto declRefType = as(sharedTypeExpr->base)) { declRefType->declRef.substitutions = createDefaultSubstitutions(getSession(), declRefType->declRef.getDecl()); - if (auto typetype = typeExp.exp->type.type.dynamicCast()) + + if (auto typetype = as(typeExp.exp->type)) typetype->type = declRefType; } } @@ -2707,8 +2708,9 @@ namespace Slang return uncheckedAttr; } - RefPtr attrObj = attrDecl->syntaxClass.createInstance(); - auto attr = attrObj.dynamicCast(); + // Manage scope + RefPtr attrInstance = attrDecl->syntaxClass.createInstance(); + auto attr = attrInstance.as(); if(!attr) { SLANG_DIAGNOSE_UNEXPECTED(getSink(), attrDecl, "attribute class did not yield an attribute object"); @@ -2802,7 +2804,7 @@ namespace Slang RefPtr m, ModifiableSyntaxNode* syntaxNode) { - if(auto hlslUncheckedAttribute = m.dynamicCast()) + if(auto hlslUncheckedAttribute = as(m)) { // We have an HLSL `[name(arg,...)]` attribute, and we'd like // to check that it is provides all the expected arguments @@ -3036,17 +3038,17 @@ namespace Slang { auto genMbr = genDecl.getDecl()->Members[i]; auto requiredGenMbr = genDecl.getDecl()->Members[i]; - if (auto genTypeMbr = genMbr.dynamicCast()) + if (auto genTypeMbr = as(genMbr)) { - if (auto requiredGenTypeMbr = requiredGenMbr.dynamicCast()) + if (auto requiredGenTypeMbr = as(requiredGenMbr)) { } else return false; } - else if (auto genValMbr = genMbr.dynamicCast()) + else if (auto genValMbr = as(genMbr)) { - if (auto requiredGenValMbr = requiredGenMbr.dynamicCast()) + if (auto requiredGenValMbr = as(requiredGenMbr)) { if (!genValMbr->type->Equals(requiredGenValMbr->type)) return false; @@ -3054,9 +3056,9 @@ namespace Slang else return false; } - else if (auto genTypeConstraintMbr = genMbr.dynamicCast()) + else if (auto genTypeConstraintMbr = as(genMbr)) { - if (auto requiredTypeConstraintMbr = requiredGenMbr.dynamicCast()) + if (auto requiredTypeConstraintMbr = as(requiredGenMbr)) { if (!genTypeConstraintMbr->sup->Equals(requiredTypeConstraintMbr->sup)) { @@ -3844,7 +3846,7 @@ namespace Slang RefPtr explicitTagVal = TryConstantFoldExpr(explicitTagValExpr); if(explicitTagVal) { - if(auto constIntVal = explicitTagVal.dynamicCast()) + if(auto constIntVal = as(explicitTagVal)) { defaultTag = constIntVal->value; } @@ -4895,7 +4897,7 @@ namespace Slang // // For right now we will look for calls to intrinsic functions, and then inspect // their names (this is bad and slow). - auto funcDeclRefExpr = invokeExpr->FunctionExpr.dynamicCast(); + auto funcDeclRefExpr = invokeExpr->FunctionExpr.as(); if (!funcDeclRefExpr) return nullptr; auto funcDeclRef = funcDeclRefExpr->declRef; @@ -5197,7 +5199,7 @@ namespace Slang { auto session = getSession(); auto vectorGenericDecl = findMagicDecl( - session, "Vector").dynamicCast(); + session, "Vector").as(); auto vectorTypeDecl = vectorGenericDecl->inner; auto substitutions = new GenericSubstitution(); @@ -5207,9 +5209,9 @@ namespace Slang auto declRef = DeclRef(vectorTypeDecl.Ptr(), substitutions); - return as(DeclRefType::Create( + return DeclRefType::Create( session, - declRef)); + declRef).as(); } RefPtr visitIndexExpr(IndexExpr* subscriptExpr) @@ -6258,8 +6260,8 @@ namespace Slang if (c.decl != typeParam.getDecl()) continue; - auto cType = c.val.dynamicCast(); - SLANG_RELEASE_ASSERT(cType.Ptr()); + auto cType = as(c.val); + SLANG_RELEASE_ASSERT(cType); if (!type) { @@ -6297,8 +6299,8 @@ namespace Slang if (c.decl != valParam.getDecl()) continue; - auto cVal = c.val.dynamicCast(); - SLANG_RELEASE_ASSERT(cVal.Ptr()); + auto cVal = as(c.val); + SLANG_RELEASE_ASSERT(cVal); if (!val) { @@ -6306,7 +6308,7 @@ namespace Slang } else { - if(!val->EqualsVal(cVal.Ptr())) + if(!val->EqualsVal(cVal)) { // failure! return SubstitutionSet(); @@ -6781,7 +6783,7 @@ namespace Slang // We should have the existing arguments to the generic // handy, so that we can construct a substitution list. - RefPtr subst = candidate.subst.dynamicCast(); + RefPtr subst = candidate.subst.as(); SLANG_ASSERT(subst); subst->genericDecl = genericDeclRef.getDecl(); @@ -6851,7 +6853,7 @@ namespace Slang RefPtr originalExpr, RefPtr subst) { - auto baseDeclRefExpr = baseExpr.dynamicCast(); + auto baseDeclRefExpr = as(baseExpr); if (!baseDeclRefExpr) { SLANG_DIAGNOSE_UNEXPECTED(getSink(), baseExpr, "expected a reference to a generic declaration"); @@ -6928,7 +6930,7 @@ namespace Slang { case OverloadCandidate::Flavor::Func: { - RefPtr callExpr = context.originalExpr.as(); + RefPtr callExpr = as(context.originalExpr); if(!callExpr) { callExpr = new InvokeExpr(); @@ -7209,18 +7211,18 @@ namespace Slang RefPtr snd) { // if both values are types, then unify types - if (auto fstType = dynamicCast(fst)) + if (auto fstType = as(fst)) { - if (auto sndType = dynamicCast(snd)) + if (auto sndType = as(snd)) { return TryUnifyTypes(constraints, fstType, sndType); } } // if both values are constant integers, then compare them - if (auto fstIntVal = dynamicCast(fst)) + if (auto fstIntVal = as(fst)) { - if (auto sndIntVal = dynamicCast(snd)) + if (auto sndIntVal = as(snd)) { return fstIntVal->value == sndIntVal->value; } @@ -7735,7 +7737,7 @@ namespace Slang // for (auto genericDeclRef : getMembersOfType(aggTypeDeclRef)) { - if (auto ctorDecl = genericDeclRef.getDecl()->inner.as()) + if (auto ctorDecl = as(genericDeclRef.getDecl()->inner)) { DeclRef innerRef = SpecializeGenericForOverload(genericDeclRef, context); if (!innerRef) @@ -9742,16 +9744,16 @@ namespace Slang isLValue = false; // Variables declared with `let` are always immutable. - if(varDeclRef.as()) + if(as(varDeclRef.getDecl())) isLValue = false; // Generic value parameters are always immutable - if(varDeclRef.as()) + if(as(varDeclRef.getDecl())) isLValue = false; // Function parameters declared in the "modern" style // are immutable unless they have an `out` or `inout` modifier. - if( varDeclRef.as() ) + if(as(varDeclRef.getDecl()) ) { // Note: the `inout` modifier AST class inherits from // the class for the `out` modifier so that we can -- cgit v1.2.3