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 *CreateIndexNode(int index);
31 TIntermConstantUnion *CreateUIntNode(unsigned int value);
32 TIntermConstantUnion *CreateBoolNode(bool value);
33
34 TVariable *CreateTempVariable(TSymbolTable *symbolTable, const TType *type);
35 TVariable *CreateTempVariable(TSymbolTable *symbolTable, const TType *type, TQualifier qualifier);
36
37 TIntermSymbol *CreateTempSymbolNode(const TVariable *tempVariable);
38 TIntermDeclaration *CreateTempDeclarationNode(const TVariable *tempVariable);
39 TIntermDeclaration *CreateTempInitDeclarationNode(const TVariable *tempVariable,
40 TIntermTyped *initializer);
41 TIntermBinary *CreateTempAssignmentNode(const TVariable *tempVariable, TIntermTyped *rightNode);
42
43 TVariable *DeclareTempVariable(TSymbolTable *symbolTable,
44 const TType *type,
45 TQualifier qualifier,
46 TIntermDeclaration **declarationOut);
47 TVariable *DeclareTempVariable(TSymbolTable *symbolTable,
48 TIntermTyped *initializer,
49 TQualifier qualifier,
50 TIntermDeclaration **declarationOut);
51 std::pair<const TVariable *, const TVariable *> DeclareStructure(
52 TIntermBlock *root,
53 TSymbolTable *symbolTable,
54 TFieldList *fieldList,
55 TQualifier qualifier,
56 const TMemoryQualifier &memoryQualifier,
57 uint32_t arraySize,
58 const ImmutableString &structTypeName,
59 const ImmutableString *structInstanceName);
60 const TVariable *DeclareInterfaceBlock(TIntermBlock *root,
61 TSymbolTable *symbolTable,
62 TFieldList *fieldList,
63 TQualifier qualifier,
64 const TLayoutQualifier &layoutQualifier,
65 const TMemoryQualifier &memoryQualifier,
66 uint32_t arraySize,
67 const ImmutableString &blockTypeName,
68 const ImmutableString &blockVariableName);
69
70 // If the input node is nullptr, return nullptr.
71 // If the input node is a block node, return it.
72 // If the input node is not a block node, put it inside a block node and return that.
73 TIntermBlock *EnsureBlock(TIntermNode *node);
74
75 // Should be called from inside Compiler::compileTreeImpl() where the global level is in scope.
76 TIntermSymbol *ReferenceGlobalVariable(const ImmutableString &name,
77 const TSymbolTable &symbolTable);
78
79 // Note: this can't access desktop GLSL built-ins. Those can only be accessed directly through
80 // BuiltIn.h.
81 TIntermSymbol *ReferenceBuiltInVariable(const ImmutableString &name,
82 const TSymbolTable &symbolTable,
83 int shaderVersion);
84
85 TIntermTyped *CreateBuiltInFunctionCallNode(const char *name,
86 TIntermSequence *arguments,
87 const TSymbolTable &symbolTable,
88 int shaderVersion);
89 TIntermTyped *CreateBuiltInUnaryFunctionCallNode(const char *name,
90 TIntermTyped *argument,
91 const TSymbolTable &symbolTable,
92 int shaderVersion);
93
GetSwizzleIndex(TVector<int> * indexOut)94 inline void GetSwizzleIndex(TVector<int> *indexOut) {}
95
96 template <typename T, typename... ArgsT>
GetSwizzleIndex(TVector<int> * indexOut,T arg,ArgsT...args)97 void GetSwizzleIndex(TVector<int> *indexOut, T arg, ArgsT... args)
98 {
99 indexOut->push_back(arg);
100 GetSwizzleIndex(indexOut, args...);
101 }
102
103 template <typename... ArgsT>
CreateSwizzle(TIntermTyped * reference,ArgsT...args)104 TIntermSwizzle *CreateSwizzle(TIntermTyped *reference, ArgsT... args)
105 {
106 TVector<int> swizzleIndex;
107 GetSwizzleIndex(&swizzleIndex, args...);
108 return new TIntermSwizzle(reference, swizzleIndex);
109 }
110
111 // Returns true if a block ends in a branch (break, continue, return, etc). This is only correct
112 // after PruneNoOps, because it expects empty blocks after a branch to have been already pruned,
113 // i.e. a block can only end in a branch if its last statement is a branch or is a block ending in
114 // branch.
115 bool EndsInBranch(TIntermBlock *block);
116
117 } // namespace sh
118
119 #endif // COMPILER_TRANSLATOR_INTERMNODEUTIL_H_
120