1 // 2 // Copyright 2020 The ANGLE Project Authors. All rights reserved. 3 // Use of this source code is governed by a BSD-style license that can be 4 // found in the LICENSE file. 5 // 6 7 #ifndef COMPILER_TRANSLATOR_MSL_ASTHELPERS_H_ 8 #define COMPILER_TRANSLATOR_MSL_ASTHELPERS_H_ 9 10 #include <cstring> 11 #include <unordered_map> 12 #include <unordered_set> 13 14 #include "compiler/translator/msl/IdGen.h" 15 #include "compiler/translator/msl/Name.h" 16 #include "compiler/translator/msl/SymbolEnv.h" 17 18 namespace sh 19 { 20 21 // A convenience view of a TIntermDeclaration node's children. 22 struct Declaration 23 { 24 TIntermSymbol &symbol; 25 TIntermTyped *initExpr; // Non-null iff declaration is initialized. 26 }; 27 28 // Returns a `Declaration` view of the given node. 29 Declaration ViewDeclaration(TIntermDeclaration &declNode); 30 31 // Creates a variable for a struct type. 32 const TVariable &CreateStructTypeVariable(TSymbolTable &symbolTable, const TStructure &structure); 33 34 // Creates a variable for a struct instance. 35 const TVariable &CreateInstanceVariable(TSymbolTable &symbolTable, 36 const TStructure &structure, 37 const Name &name, 38 TQualifier qualifier = TQualifier::EvqTemporary, 39 const TSpan<const unsigned int> *arraySizes = nullptr); 40 41 // The input sequence should be discarded from AST after this is called. 42 TIntermSequence &CloneSequenceAndPrepend(const TIntermSequence &seq, TIntermNode &node); 43 44 // Appends parameters from `src` function to `dest` function. 45 void AddParametersFrom(TFunction &dest, const TFunction &src); 46 47 // Clones a function. 48 const TFunction &CloneFunction(TSymbolTable &symbolTable, IdGen &idGen, const TFunction &oldFunc); 49 50 // Clones a function and prepends the provided extr parameter. 51 // If `idGen` is null, the original function must be discarded from the AST. 52 const TFunction &CloneFunctionAndPrependParam(TSymbolTable &symbolTable, 53 IdGen *idGen, 54 const TFunction &oldFunc, 55 const TVariable &newParam); 56 57 // Clones a function and prepends the provided two parameters. 58 // If `idGen` is null, the original function must be discarded from the AST. 59 const TFunction &CloneFunctionAndPrependTwoParams(TSymbolTable &symbolTable, 60 IdGen *idGen, 61 const TFunction &oldFunc, 62 const TVariable &newParam1, 63 const TVariable &newParam2); 64 65 // Clones a function and appends the provided extra parameters. 66 // If `idGen` is null, the original function must be discarded from the AST. 67 const TFunction &CloneFunctionAndAppendParams(TSymbolTable &symbolTable, 68 IdGen *idGen, 69 const TFunction &oldFunc, 70 const std::vector<const TVariable *> &newParam); 71 72 // Clones a function and changes its return type. 73 // If `idGen` is null, the original function must be discarded from the AST. 74 const TFunction &CloneFunctionAndChangeReturnType(TSymbolTable &symbolTable, 75 IdGen *idGen, 76 const TFunction &oldFunc, 77 const TStructure &newReturn); 78 79 // Gets the argument of a function call at the given index. 80 TIntermTyped &GetArg(const TIntermAggregate &call, size_t index); 81 82 // Sets the argument of a function call at the given index. 83 void SetArg(TIntermAggregate &call, size_t index, TIntermTyped &arg); 84 85 // Returns the field index within the given struct for the given field name. 86 // Returns -1 if the struct has no field with the given name. 87 int GetFieldIndex(const TStructure &structure, const ImmutableString &fieldName); 88 89 // Accesses a field for the given variable with the given field name. 90 // The variable must be a struct instance. 91 TIntermBinary &AccessField(const TVariable &structInstanceVar, const ImmutableString &fieldName); 92 93 // Accesses a field for the given node with the given field name. 94 // The node must be a struct instance. 95 TIntermBinary &AccessField(TIntermTyped &object, const ImmutableString &fieldName); 96 97 // Accesses a field for the given node by its field index. 98 // The node must be a struct instance. 99 TIntermBinary &AccessFieldByIndex(TIntermTyped &object, int index); 100 101 // Accesses an element by index for the given node. 102 // The node must be an array, vector, or matrix. 103 TIntermBinary &AccessIndex(TIntermTyped &indexableNode, int index); 104 105 // Accesses an element by index for the given node if `index` is non-null. 106 // Returns the original node if `index` is null. 107 // The node must be an array, vector, or matrix if `index` is non-null. 108 TIntermTyped &AccessIndex(TIntermTyped &node, const int *index); 109 110 // Returns a subvector based on the input slice range. 111 // This returns the original node if the slice is an identity for the node. 112 TIntermTyped &SubVector(TIntermTyped &vectorNode, int begin, int end); 113 114 // Matches scalar bool, int, uint32_t, float, double. 115 bool IsScalarBasicType(const TType &type); 116 117 // Matches vector bool, int, uint32_t, float, double. 118 bool IsVectorBasicType(const TType &type); 119 120 // Matches bool, int, uint32_t, float, double. 121 // Type does not need to be a scalar. 122 bool HasScalarBasicType(const TType &type); 123 124 // Matches bool, int, uint32_t, float, double. 125 bool HasScalarBasicType(TBasicType type); 126 127 // Clones a type. 128 TType &CloneType(const TType &type); 129 130 // Clones a type and drops all array dimensions. 131 TType &InnermostType(const TType &type); 132 133 // Creates a vector type by dropping the columns off of a matrix type. 134 TType &DropColumns(const TType &matrixType); 135 136 // Creates a type by dropping the outer dimension off of an array type. 137 TType &DropOuterDimension(const TType &arrayType); 138 139 // Creates a scalar or vector type by changing the dimensions of a vector type. 140 TType &SetVectorDim(const TType &type, int newDim); 141 142 // Creates a matrix type by changing the row dimensions of a matrix type. 143 TType &SetMatrixRowDim(const TType &matrixType, int newDim); 144 145 // Returns true iff the structure directly contains a field with matrix type. 146 bool HasMatrixField(const TStructure &structure); 147 148 // Returns true iff the structure directly contains a field with array type. 149 bool HasArrayField(const TStructure &structure); 150 151 // Coerces `fromNode` to `toType` by a constructor call of `toType` if their types differ. 152 // Vector and matrix dimensions are retained. 153 // Array types are not allowed. 154 TIntermTyped &CoerceSimple(TBasicType toBasicType, 155 TIntermTyped &fromNode, 156 bool needsExplicitBoolCast); 157 158 // Coerces `fromNode` to `toType` by a constructor call of `toType` if their types differ. 159 // Vector and matrix dimensions must coincide between to and from. 160 // Array types are not allowed. 161 TIntermTyped &CoerceSimple(const TType &toType, TIntermTyped &fromNode, bool needsExplicitBoolCast); 162 163 TIntermTyped &AsType(SymbolEnv &symbolEnv, const TType &toType, TIntermTyped &fromNode); 164 165 } // namespace sh 166 167 #endif // COMPILER_TRANSLATOR_MSL_ASTHELPERS_H_ 168