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