IntermNode_util.h (6097B)
1 // 2 // Copyright 2017 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 // IntermNode_util.h: High-level utilities for creating AST nodes and node hierarchies. Mostly meant 7 // to be used in AST transforms. 8 9 #ifndef COMPILER_TRANSLATOR_INTERMNODEUTIL_H_ 10 #define COMPILER_TRANSLATOR_INTERMNODEUTIL_H_ 11 12 #include "compiler/translator/IntermNode.h" 13 #include "compiler/translator/tree_util/FindFunction.h" 14 15 namespace sh 16 { 17 18 class TSymbolTable; 19 class TVariable; 20 21 TIntermFunctionPrototype *CreateInternalFunctionPrototypeNode(const TFunction &func); 22 TIntermFunctionDefinition *CreateInternalFunctionDefinitionNode(const TFunction &func, 23 TIntermBlock *functionBody); 24 25 TIntermTyped *CreateZeroNode(const TType &type); 26 TIntermConstantUnion *CreateFloatNode(float value, TPrecision precision); 27 TIntermConstantUnion *CreateVecNode(const float values[], 28 unsigned int vecSize, 29 TPrecision precision); 30 TIntermConstantUnion *CreateUVecNode(const unsigned int values[], 31 unsigned int vecSize, 32 TPrecision precision); 33 TIntermConstantUnion *CreateIndexNode(int index); 34 TIntermConstantUnion *CreateUIntNode(unsigned int value); 35 TIntermConstantUnion *CreateBoolNode(bool value); 36 37 TVariable *CreateTempVariable(TSymbolTable *symbolTable, const TType *type); 38 TVariable *CreateTempVariable(TSymbolTable *symbolTable, const TType *type, TQualifier qualifier); 39 40 TIntermSymbol *CreateTempSymbolNode(const TVariable *tempVariable); 41 TIntermDeclaration *CreateTempDeclarationNode(const TVariable *tempVariable); 42 TIntermDeclaration *CreateTempInitDeclarationNode(const TVariable *tempVariable, 43 TIntermTyped *initializer); 44 TIntermBinary *CreateTempAssignmentNode(const TVariable *tempVariable, TIntermTyped *rightNode); 45 46 TVariable *DeclareTempVariable(TSymbolTable *symbolTable, 47 const TType *type, 48 TQualifier qualifier, 49 TIntermDeclaration **declarationOut); 50 TVariable *DeclareTempVariable(TSymbolTable *symbolTable, 51 TIntermTyped *initializer, 52 TQualifier qualifier, 53 TIntermDeclaration **declarationOut); 54 std::pair<const TVariable *, const TVariable *> DeclareStructure( 55 TIntermBlock *root, 56 TSymbolTable *symbolTable, 57 TFieldList *fieldList, 58 TQualifier qualifier, 59 const TMemoryQualifier &memoryQualifier, 60 uint32_t arraySize, 61 const ImmutableString &structTypeName, 62 const ImmutableString *structInstanceName); 63 const TVariable *DeclareInterfaceBlock(TIntermBlock *root, 64 TSymbolTable *symbolTable, 65 TFieldList *fieldList, 66 TQualifier qualifier, 67 const TLayoutQualifier &layoutQualifier, 68 const TMemoryQualifier &memoryQualifier, 69 uint32_t arraySize, 70 const ImmutableString &blockTypeName, 71 const ImmutableString &blockVariableName); 72 73 // If the input node is nullptr, return nullptr. 74 // If the input node is a block node, return it. 75 // If the input node is not a block node, put it inside a block node and return that. 76 TIntermBlock *EnsureBlock(TIntermNode *node); 77 78 // Should be called from inside Compiler::compileTreeImpl() where the global level is in scope. 79 TIntermSymbol *ReferenceGlobalVariable(const ImmutableString &name, 80 const TSymbolTable &symbolTable); 81 82 // Note: this can't access desktop GLSL built-ins. Those can only be accessed directly through 83 // BuiltIn.h. 84 TIntermSymbol *ReferenceBuiltInVariable(const ImmutableString &name, 85 const TSymbolTable &symbolTable, 86 int shaderVersion); 87 88 TIntermTyped *CreateBuiltInFunctionCallNode(const char *name, 89 TIntermSequence *arguments, 90 const TSymbolTable &symbolTable, 91 int shaderVersion); 92 TIntermTyped *CreateBuiltInFunctionCallNode(const char *name, 93 const std::initializer_list<TIntermNode *> &arguments, 94 const TSymbolTable &symbolTable, 95 int shaderVersion); 96 TIntermTyped *CreateBuiltInUnaryFunctionCallNode(const char *name, 97 TIntermTyped *argument, 98 const TSymbolTable &symbolTable, 99 int shaderVersion); 100 101 int GetESSLOrGLSLVersion(ShShaderSpec spec, int esslVersion, int glslVersion); 102 103 inline void GetSwizzleIndex(TVector<int> *indexOut) {} 104 105 template <typename T, typename... ArgsT> 106 void GetSwizzleIndex(TVector<int> *indexOut, T arg, ArgsT... args) 107 { 108 indexOut->push_back(arg); 109 GetSwizzleIndex(indexOut, args...); 110 } 111 112 template <typename... ArgsT> 113 TIntermSwizzle *CreateSwizzle(TIntermTyped *reference, ArgsT... args) 114 { 115 TVector<int> swizzleIndex; 116 GetSwizzleIndex(&swizzleIndex, args...); 117 return new TIntermSwizzle(reference, swizzleIndex); 118 } 119 120 // Returns true if a block ends in a branch (break, continue, return, etc). This is only correct 121 // after PruneNoOps, because it expects empty blocks after a branch to have been already pruned, 122 // i.e. a block can only end in a branch if its last statement is a branch or is a block ending in 123 // branch. 124 bool EndsInBranch(TIntermBlock *block); 125 126 } // namespace sh 127 128 #endif // COMPILER_TRANSLATOR_INTERMNODEUTIL_H_