From 11c547d1e94fa620f527c3590174e6e25ab21883 Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Thu, 31 Jan 2019 10:14:26 -0500 Subject: Feature/as refactor (#817) * Made dynamicCast a free function. * Replace As with as or dynamicCast depending on if it is a type. * Fix problem with using non smart pointer cast. * Removed legacy asXXXX methods. * Remove As from Type. * Removed As from Qual type -> made coercable into Type*, such that can just use free 'as'. * Remove left over QualType::As() impl. * Remove As from SyntaxNodeBase. * Made as for instructions implemented by dynamicCast. * Replace As on DeclRef. Use the global as<> to do the cast. * Add const safe versions of dynamicCast and as for IRInst --- source/slang/reflection.cpp | 118 ++++++++++++++++++++++---------------------- 1 file changed, 59 insertions(+), 59 deletions(-) (limited to 'source/slang/reflection.cpp') diff --git a/source/slang/reflection.cpp b/source/slang/reflection.cpp index 9a5a5faf9..76e687483 100644 --- a/source/slang/reflection.cpp +++ b/source/slang/reflection.cpp @@ -159,7 +159,7 @@ SLANG_API SlangResult spReflectionUserAttribute_GetArgumentValueInt(SlangReflect RefPtr val; if (userAttr->intArgVals.TryGetValue(index, val)) { - *rs = (int)val.As()->value; + *rs = (int)as(val)->value; return 0; } return SLANG_ERROR_INVALID_PARAMETER; @@ -169,7 +169,7 @@ SLANG_API SlangResult spReflectionUserAttribute_GetArgumentValueFloat(SlangRefle auto userAttr = convert(attrib); if (!userAttr) return SLANG_ERROR_INVALID_PARAMETER; if (index >= userAttr->args.Count()) return SLANG_ERROR_INVALID_PARAMETER; - if (auto cexpr = userAttr->args[index].As()) + if (auto cexpr = as(userAttr->args[index])) { *rs = (float)cexpr->value; return 0; @@ -181,7 +181,7 @@ SLANG_API const char* spReflectionUserAttribute_GetArgumentValueString(SlangRefl auto userAttr = convert(attrib); if (!userAttr) return nullptr; if (index >= userAttr->args.Count()) return nullptr; - if (auto cexpr = userAttr->args[index].As()) + if (auto cexpr = as(userAttr->args[index])) { if (bufLen) *bufLen = cexpr->token.Content.size(); @@ -202,49 +202,49 @@ SLANG_API SlangTypeKind spReflectionType_GetKind(SlangReflectionType* inType) // TODO(tfoley: Don't emit the same type more than once... - if (auto basicType = type->As()) + if (auto basicType = as(type)) { return SLANG_TYPE_KIND_SCALAR; } - else if (auto vectorType = type->As()) + else if (auto vectorType = as(type)) { return SLANG_TYPE_KIND_VECTOR; } - else if (auto matrixType = type->As()) + else if (auto matrixType = as(type)) { return SLANG_TYPE_KIND_MATRIX; } - else if (auto parameterBlockType = type->As()) + else if (auto parameterBlockType = as(type)) { return SLANG_TYPE_KIND_PARAMETER_BLOCK; } - else if (auto constantBufferType = type->As()) + else if (auto constantBufferType = as(type)) { return SLANG_TYPE_KIND_CONSTANT_BUFFER; } - else if( auto streamOutputType = type->As() ) + else if( auto streamOutputType = as(type) ) { return SLANG_TYPE_KIND_OUTPUT_STREAM; } - else if (type->As()) + else if (as(type)) { return SLANG_TYPE_KIND_TEXTURE_BUFFER; } - else if (type->As()) + else if (as(type)) { return SLANG_TYPE_KIND_SHADER_STORAGE_BUFFER; } - else if (auto samplerStateType = type->As()) + else if (auto samplerStateType = as(type)) { return SLANG_TYPE_KIND_SAMPLER_STATE; } - else if (auto textureType = type->As()) + else if (auto textureType = as(type)) { return SLANG_TYPE_KIND_RESOURCE; } // TODO: need a better way to handle this stuff... #define CASE(TYPE) \ - else if(type->As()) do { \ + else if(as(type)) do { \ return SLANG_TYPE_KIND_RESOURCE; \ } while(0) @@ -259,27 +259,27 @@ SLANG_API SlangTypeKind spReflectionType_GetKind(SlangReflectionType* inType) CASE(UntypedBufferResourceType); #undef CASE - else if (auto arrayType = type->As()) + else if (auto arrayType = as(type)) { return SLANG_TYPE_KIND_ARRAY; } - else if( auto declRefType = type->As() ) + else if( auto declRefType = as(type) ) { auto declRef = declRefType->declRef; - if( auto structDeclRef = declRef.As() ) + if( auto structDeclRef = declRef.as() ) { return SLANG_TYPE_KIND_STRUCT; } - else if (auto genericParamType = declRef.As()) + else if (auto genericParamType = declRef.as()) { return SLANG_TYPE_KIND_GENERIC_TYPE_PARAMETER; } - else if (auto interfaceType = declRef.As()) + else if (auto interfaceType = declRef.as()) { return SLANG_TYPE_KIND_INTERFACE; } } - else if (auto errorType = type->As()) + else if (auto errorType = as(type)) { // This means we saw a type we didn't understand in the user's code return SLANG_TYPE_KIND_NONE; @@ -296,10 +296,10 @@ SLANG_API unsigned int spReflectionType_GetFieldCount(SlangReflectionType* inTyp // TODO: maybe filter based on kind - if(auto declRefType = type->As()) + if(auto declRefType = as(type)) { auto declRef = declRefType->declRef; - if( auto structDeclRef = declRef.As()) + if( auto structDeclRef = declRef.as()) { return GetFields(structDeclRef).Count(); } @@ -315,10 +315,10 @@ SLANG_API SlangReflectionVariable* spReflectionType_GetFieldByIndex(SlangReflect // TODO: maybe filter based on kind - if(auto declRefType = type->As()) + if(auto declRefType = as(type)) { auto declRef = declRefType->declRef; - if( auto structDeclRef = declRef.As()) + if( auto structDeclRef = declRef.as()) { auto fieldDeclRef = GetFields(structDeclRef).ToArray()[index]; return (SlangReflectionVariable*) fieldDeclRef.getDecl(); @@ -333,11 +333,11 @@ SLANG_API size_t spReflectionType_GetElementCount(SlangReflectionType* inType) auto type = convert(inType); if(!type) return 0; - if(auto arrayType = type->As()) + if(auto arrayType = as(type)) { return arrayType->ArrayLength ? (size_t) GetIntVal(arrayType->ArrayLength) : 0; } - else if( auto vectorType = type->As()) + else if( auto vectorType = as(type)) { return (size_t) GetIntVal(vectorType->elementCount); } @@ -350,19 +350,19 @@ SLANG_API SlangReflectionType* spReflectionType_GetElementType(SlangReflectionTy auto type = convert(inType); if(!type) return nullptr; - if(auto arrayType = type->As()) + if(auto arrayType = as(type)) { return (SlangReflectionType*) arrayType->baseType.Ptr(); } - else if( auto constantBufferType = type->As()) + else if( auto constantBufferType = as(type)) { return convert(constantBufferType->elementType.Ptr()); } - else if( auto vectorType = type->As()) + else if( auto vectorType = as(type)) { return convert(vectorType->elementType.Ptr()); } - else if( auto matrixType = type->As()) + else if( auto matrixType = as(type)) { return convert(matrixType->getElementType()); } @@ -375,15 +375,15 @@ SLANG_API unsigned int spReflectionType_GetRowCount(SlangReflectionType* inType) auto type = convert(inType); if(!type) return 0; - if(auto matrixType = type->As()) + if(auto matrixType = as(type)) { return (unsigned int) GetIntVal(matrixType->getRowCount()); } - else if(auto vectorType = type->As()) + else if(auto vectorType = as(type)) { return 1; } - else if( auto basicType = type->As() ) + else if( auto basicType = as(type) ) { return 1; } @@ -396,15 +396,15 @@ SLANG_API unsigned int spReflectionType_GetColumnCount(SlangReflectionType* inTy auto type = convert(inType); if(!type) return 0; - if(auto matrixType = type->As()) + if(auto matrixType = as(type)) { return (unsigned int) GetIntVal(matrixType->getColumnCount()); } - else if(auto vectorType = type->As()) + else if(auto vectorType = as(type)) { return (unsigned int) GetIntVal(vectorType->elementCount); } - else if( auto basicType = type->As() ) + else if( auto basicType = as(type) ) { return 1; } @@ -417,16 +417,16 @@ SLANG_API SlangScalarType spReflectionType_GetScalarType(SlangReflectionType* in auto type = convert(inType); if(!type) return 0; - if(auto matrixType = type->As()) + if(auto matrixType = as(type)) { type = matrixType->getElementType(); } - else if(auto vectorType = type->As()) + else if(auto vectorType = as(type)) { type = vectorType->elementType.Ptr(); } - if(auto basicType = type->As()) + if(auto basicType = as(type)) { switch (basicType->baseType) { @@ -463,7 +463,7 @@ SLANG_API unsigned int spReflectionType_GetUserAttributeCount(SlangReflectionTyp { auto type = convert(inType); if (!type) return 0; - if (auto declRefType = type->AsDeclRefType()) + if (auto declRefType = as(type)) { return getUserAttributeCount(declRefType->declRef.getDecl()); } @@ -473,7 +473,7 @@ SLANG_API SlangReflectionUserAttribute* spReflectionType_GetUserAttribute(SlangR { auto type = convert(inType); if (!type) return 0; - if (auto declRefType = type->AsDeclRefType()) + if (auto declRefType = as(type)) { return getUserAttributeByIndex(declRefType->declRef.getDecl(), index); } @@ -483,7 +483,7 @@ SLANG_API SlangReflectionUserAttribute* spReflectionType_FindUserAttributeByName { auto type = convert(inType); if (!type) return 0; - if (auto declRefType = type->AsDeclRefType()) + if (auto declRefType = as(type)) { return findUserAttributeByName(declRefType->getSession(), declRefType->declRef.getDecl(), name); } @@ -495,19 +495,19 @@ SLANG_API SlangResourceShape spReflectionType_GetResourceShape(SlangReflectionTy auto type = convert(inType); if(!type) return 0; - while(auto arrayType = type->As()) + while(auto arrayType = as(type)) { type = arrayType->baseType.Ptr(); } - if(auto textureType = type->As()) + if(auto textureType = as(type)) { return textureType->getShape(); } // TODO: need a better way to handle this stuff... #define CASE(TYPE, SHAPE, ACCESS) \ - else if(type->As()) do { \ + else if(as(type)) do { \ return SHAPE; \ } while(0) @@ -530,19 +530,19 @@ SLANG_API SlangResourceAccess spReflectionType_GetResourceAccess(SlangReflection auto type = convert(inType); if(!type) return 0; - while(auto arrayType = type->As()) + while(auto arrayType = as(type)) { type = arrayType->baseType.Ptr(); } - if(auto textureType = type->As()) + if(auto textureType = as(type)) { return textureType->getAccess(); } // TODO: need a better way to handle this stuff... #define CASE(TYPE, SHAPE, ACCESS) \ - else if(type->As()) do { \ + else if(as(type)) do { \ return ACCESS; \ } while(0) @@ -567,7 +567,7 @@ SLANG_API char const* spReflectionType_GetName(SlangReflectionType* inType) { auto type = convert(inType); - if( auto declRefType = type->As() ) + if( auto declRefType = as(type) ) { auto declRef = declRefType->declRef; @@ -613,20 +613,20 @@ SLANG_API SlangReflectionType* spReflectionType_GetResourceResultType(SlangRefle auto type = convert(inType); if(!type) return nullptr; - while(auto arrayType = type->As()) + while(auto arrayType = as(type)) { type = arrayType->baseType.Ptr(); } - if (auto textureType = type->As()) + if (auto textureType = as(type)) { return convert(textureType->elementType.Ptr()); } // TODO: need a better way to handle this stuff... #define CASE(TYPE, SHAPE, ACCESS) \ - else if(type->As()) do { \ - return convert(type->As()->elementType.Ptr()); \ + else if(as(type)) do { \ + return convert(as(type)->elementType.Ptr()); \ } while(0) // TODO: structured buffer needs to expose type layout! @@ -695,7 +695,7 @@ SLANG_API size_t spReflectionTypeLayout_GetElementStride(SlangReflectionTypeLayo { switch (category) { - // We store the stride explictly for the uniform case + // We store the stride explicitly for the uniform case case SLANG_PARAMETER_CATEGORY_UNIFORM: return arrayTypeLayout->uniformStride; @@ -950,7 +950,7 @@ namespace Slang // Is the category they were asking about one that makes sense for the type // of this variable? Type* type = typeLayout->getType(); - while (auto arrayType = type->As()) + while (auto arrayType = as(type)) type = arrayType->baseType; switch (spReflectionType_GetKind(convert(type))) { @@ -1109,12 +1109,12 @@ namespace Slang { static unsigned getParameterCount(RefPtr typeLayout) { - if(auto parameterGroupLayout = typeLayout.As()) + if(auto parameterGroupLayout = as(typeLayout)) { typeLayout = parameterGroupLayout->offsetElementTypeLayout; } - if(auto structLayout = typeLayout.As()) + if(auto structLayout = as(typeLayout)) { return (unsigned) structLayout->fields.Count(); } @@ -1124,12 +1124,12 @@ namespace Slang static VarLayout* getParameterByIndex(RefPtr typeLayout, unsigned index) { - if(auto parameterGroupLayout = typeLayout.As()) + if(auto parameterGroupLayout = as(typeLayout)) { typeLayout = parameterGroupLayout->offsetElementTypeLayout; } - if(auto structLayout = typeLayout.As()) + if(auto structLayout = as(typeLayout)) { return structLayout->fields[index]; } -- cgit v1.2.3