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