diff options
Diffstat (limited to 'source/slang/lower.cpp')
| -rw-r--r-- | source/slang/lower.cpp | 245 |
1 files changed, 129 insertions, 116 deletions
diff --git a/source/slang/lower.cpp b/source/slang/lower.cpp index 391221f47..85f19f14c 100644 --- a/source/slang/lower.cpp +++ b/source/slang/lower.cpp @@ -36,6 +36,103 @@ struct CloneVisitor // +// + +class TupleExpr; +class TupleVarDecl; +class VaryingTupleExpr; +class VaryingTupleVarDecl; + + +// The result of lowering a declaration will usually be a declaration, +// but it might also be a "tuple" declaration, in cases where we needed +// to sclarize (or partially scalarize) things to guarantee validity. +struct LoweredDecl +{ + enum class Flavor + { + Decl, // A single declaration (the default case) + Tuple, // A `TupleVarDecl` representing multiple decls + VaryingTuple, // A `VaryingTupleVarDecl` representing multiple decls + }; + + LoweredDecl() + : flavor(Flavor::Decl) + {} + + LoweredDecl(Decl* decl) + : value(decl) + , flavor(Flavor::Decl) + {} + + LoweredDecl(TupleVarDecl* decl) + : value((RefObject*) decl) + , flavor(Flavor::Tuple) + {} + + LoweredDecl(VaryingTupleVarDecl* decl) + : value((RefObject*) decl) + , flavor(Flavor::VaryingTuple) + {} + + Flavor getFlavor() const { return flavor; } + RefObject* getValue() const { return value; } + + Decl* getDecl() const + { + SLANG_ASSERT(getFlavor() == Flavor::Decl); + return (Decl*) value.Ptr(); + } + + TupleVarDecl* getTupleDecl() const + { + SLANG_ASSERT(getFlavor() == Flavor::Tuple); + return (TupleVarDecl*) value.Ptr(); + } + + VaryingTupleVarDecl* getVaryingTupleDecl() const + { + SLANG_ASSERT(getFlavor() == Flavor::VaryingTuple); + return (VaryingTupleVarDecl*) value.Ptr(); + } + + Decl* asDecl() const + { + return (getFlavor() == Flavor::Decl) ? getDecl() : nullptr; + } + + TupleVarDecl* asTupleDecl() const + { + return (getFlavor() == Flavor::Tuple) ? getTupleDecl() : nullptr; + } + + VaryingTupleVarDecl* asVaryingTupleDecl() const + { + return (getFlavor() == Flavor::VaryingTuple) ? getVaryingTupleDecl() : nullptr; + } + +private: + RefPtr<RefObject> value; + Flavor flavor; +}; + +struct LoweredDeclRef +{ +public: + LoweredDecl decl; + RefPtr<Substitutions> substitutions; + + LoweredDecl getDecl() { return decl; } + + template<typename T> + DeclRef<T> As() + { + return DeclRef<Decl>(decl.getDecl(), substitutions).As<T>(); + } +}; + +// + template<typename V> struct StructuralTransformVisitorBase { @@ -54,7 +151,8 @@ struct StructuralTransformVisitorBase template<typename T> DeclRef<T> transformDeclField(DeclRef<T> const& decl) { - return visitor->translateDeclRef(decl).As<T>(); + LoweredDeclRef declRef = visitor->translateDeclRef(decl); + return declRef.As<T>(); } TypeExp transformSyntaxField(TypeExp const& typeExp) @@ -89,7 +187,8 @@ struct StructuralTransformVisitorBase RefPtr<ScopeDecl> transformSyntaxField(ScopeDecl* decl) { if(!decl) return nullptr; - return visitor->transformSyntaxField(decl).As<ScopeDecl>(); + RefPtr<Decl> transformed = visitor->transformSyntaxField(decl); + return transformed.As<ScopeDecl>(); } template<typename T> @@ -104,6 +203,7 @@ struct StructuralTransformVisitorBase } }; +#if 0 template<typename V> RefPtr<Stmt> structuralTransform( Stmt* stmt, @@ -113,6 +213,7 @@ RefPtr<Stmt> structuralTransform( transformer.visitor = visitor; return transformer.dispatch(stmt); } +#endif template<typename V> struct StructuralTransformExprVisitor @@ -121,21 +222,26 @@ struct StructuralTransformExprVisitor { void transformFields(Expr* result, Expr* obj) { - result->type = transformSyntaxField(obj->type); + result->type = this->transformSyntaxField(obj->type); } +#define ABSTRACT_SYNTAX_CLASS(NAME, BASE, ...) \ + void transformFields(NAME* result, NAME* obj) { \ + this->transformFields((BASE*) result, (BASE*) obj); \ + /* end */ + #define SYNTAX_CLASS(NAME, BASE, ...) \ - RefPtr<Expr> visit##NAME(NAME* obj) { \ + RefPtr<Expr> visit##NAME(NAME* obj) { \ RefPtr<NAME> result = new NAME(*obj); \ transformFields(result, obj); \ return result; \ } \ - void transformFields(NAME* result, NAME* obj) { \ - transformFields((BASE*) result, (BASE*) obj); \ + ABSTRACT_SYNTAX_CLASS(NAME, BASE) \ + /* end */ -#define SYNTAX_FIELD(TYPE, NAME) result->NAME = transformSyntaxField(obj->NAME); -#define DECL_FIELD(TYPE, NAME) result->NAME = transformDeclField(obj->NAME); +#define SYNTAX_FIELD(TYPE, NAME) result->NAME = this->transformSyntaxField(obj->NAME); +#define DECL_FIELD(TYPE, NAME) result->NAME = this->transformDeclField(obj->NAME); #define FIELD(TYPE, NAME) /* empty */ @@ -158,100 +264,6 @@ RefPtr<Expr> structuralTransform( return transformer.dispatch(expr); } -// - -class TupleExpr; -class TupleVarDecl; -class VaryingTupleExpr; -class VaryingTupleVarDecl; - - -// The result of lowering a declaration will usually be a declaration, -// but it might also be a "tuple" declaration, in cases where we needed -// to sclarize (or partially scalarize) things to guarantee validity. -struct LoweredDecl -{ - enum class Flavor - { - Decl, // A single declaration (the default case) - Tuple, // A `TupleVarDecl` representing multiple decls - VaryingTuple, // A `VaryingTupleVarDecl` representing multiple decls - }; - - LoweredDecl() - : flavor(Flavor::Decl) - {} - - LoweredDecl(Decl* decl) - : value(decl) - , flavor(Flavor::Decl) - {} - - LoweredDecl(TupleVarDecl* decl) - : value((RefObject*) decl) - , flavor(Flavor::Tuple) - {} - - LoweredDecl(VaryingTupleVarDecl* decl) - : value((RefObject*) decl) - , flavor(Flavor::VaryingTuple) - {} - - Flavor getFlavor() const { return flavor; } - RefObject* getValue() const { return value; } - - Decl* getDecl() const - { - SLANG_ASSERT(getFlavor() == Flavor::Decl); - return (Decl*) value.Ptr(); - } - - TupleVarDecl* getTupleDecl() const - { - SLANG_ASSERT(getFlavor() == Flavor::Tuple); - return (TupleVarDecl*) value.Ptr(); - } - - VaryingTupleVarDecl* getVaryingTupleDecl() const - { - SLANG_ASSERT(getFlavor() == Flavor::VaryingTuple); - return (VaryingTupleVarDecl*) value.Ptr(); - } - - Decl* asDecl() const - { - return (getFlavor() == Flavor::Decl) ? getDecl() : nullptr; - } - - TupleVarDecl* asTupleDecl() const - { - return (getFlavor() == Flavor::Tuple) ? getTupleDecl() : nullptr; - } - - VaryingTupleVarDecl* asVaryingTupleDecl() const - { - return (getFlavor() == Flavor::VaryingTuple) ? getVaryingTupleDecl() : nullptr; - } - -private: - RefPtr<RefObject> value; - Flavor flavor; -}; - -struct LoweredDeclRef -{ -public: - LoweredDecl decl; - RefPtr<Substitutions> substitutions; - - LoweredDecl getDecl() { return decl; } - - template<typename T> - DeclRef<T> As() - { - return DeclRef<Decl>(decl.getDecl(), substitutions).As<T>(); - } -}; // The result of lowering an exrpession will usually be just a single @@ -321,9 +333,10 @@ struct LoweredExpr return (getFlavor() == Flavor::VaryingTuple) ? getVaryingTupleExpr() : nullptr; } - bool operator!() + // Allow use in boolean contexts + operator void*() { - return !value; + return value.Ptr(); } private: @@ -754,7 +767,7 @@ struct LoweringVisitor RefPtr<Type> visitArrayExpressionType(ArrayExpressionType* type) { RefPtr<ArrayExpressionType> loweredType = Slang::getArrayType( - lowerType(type->BaseType), + lowerType(type->baseType), lowerVal(type->ArrayLength).As<IntVal>()); return loweredType; } @@ -1074,7 +1087,7 @@ struct LoweringVisitor if (auto rightVecType = rightType->As<VectorExpressionType>()) { // RHS type was a vector - if (auto leftElemVecType = leftArrayType->BaseType->As<VectorExpressionType>()) + if (auto leftElemVecType = leftArrayType->baseType->As<VectorExpressionType>()) { // LHS element type was also a vector, so this is a "scalar splat // to array" case. @@ -1102,7 +1115,7 @@ struct LoweringVisitor swizzleExpr->elementIndices[0] = ee; auto convertedArgExpr = convertExprForAssignmentWithFixups( - leftArrayType->BaseType, + leftArrayType->baseType, swizzleExpr); ctorExpr->Arguments.Add(convertedArgExpr); @@ -1222,7 +1235,7 @@ struct LoweringVisitor if (auto rightVecType = rightType->As<VectorExpressionType>()) { // RHS type was a vector - if (auto leftElemVecType = leftArrayType->BaseType->As<VectorExpressionType>()) + if (auto leftElemVecType = leftArrayType->baseType->As<VectorExpressionType>()) { // LHS element type was also a vector, so this is a "scalar splat // to array" case. @@ -1243,7 +1256,7 @@ struct LoweringVisitor // LHS array element RefPtr<IndexExpr> arrayElemExpr = new IndexExpr(); arrayElemExpr->loc = leftExpr->loc; - arrayElemExpr->type.type = leftArrayType->BaseType; + arrayElemExpr->type.type = leftArrayType->baseType; arrayElemExpr->BaseExpression = leftExpr; arrayElemExpr->IndexExpression = createConstIntExpr(ee); @@ -1495,7 +1508,7 @@ struct LoweringVisitor { if (auto arrayType = type->As<ArrayExpressionType>()) { - return arrayType->BaseType; + return arrayType->baseType; } return nullptr; } @@ -1702,7 +1715,7 @@ struct LoweringVisitor while (auto arrayType = varType->As<ArrayExpressionType>()) { - varType = arrayType->BaseType; + varType = arrayType->baseType; } if (auto constantBufferType = varType->As<ConstantBufferType>()) @@ -2834,7 +2847,7 @@ struct LoweringVisitor auto type = inType; while (auto arrayType = type->As<ArrayExpressionType>()) { - type = arrayType->BaseType; + type = arrayType->baseType; } return type; } @@ -2848,7 +2861,7 @@ struct LoweringVisitor { while (auto arrayType = type->As<ArrayExpressionType>()) { - type = arrayType->BaseType; + type = arrayType->baseType; } if (auto textureTypeBase = type->As<TextureTypeBase>()) @@ -3065,7 +3078,7 @@ struct LoweringVisitor arraySpec.elementCount = arrayType->ArrayLength; TupleSecondaryVarInfo subInfo = info; - subInfo.tupleType = arrayType->BaseType; + subInfo.tupleType = arrayType->baseType; subInfo.arraySpecs = &arraySpec; createTupleTypeSecondaryVarDecls(subInfo); return; @@ -3685,7 +3698,7 @@ struct LoweringVisitor { if (auto baseType = type->As<BasicExpressionType>()) { - switch (baseType->BaseType) + switch (baseType->baseType) { default: return false; @@ -4108,7 +4121,7 @@ struct LoweringVisitor // heterogeneous stuff... return lowerShaderParameterToGLSLGLobalsRec( arrayInfo, - arrayType->BaseType, + arrayType->baseType, varLayout); } else if (auto declRefType = varType->As<DeclRefType>()) |
