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_TRANSLATORMETALDIRECT_NODETYPE_H_
8 #define COMPILER_TRANSLATOR_TRANSLATORMETALDIRECT_NODETYPE_H_
9 
10 #include "compiler/translator/tree_util/IntermTraverse.h"
11 
12 namespace sh
13 {
14 
15 enum class NodeType
16 {
17     Unknown,
18     Symbol,
19     ConstantUnion,
20     FunctionPrototype,
21     PreprocessorDirective,
22     Unary,
23     Binary,
24     Ternary,
25     Swizzle,
26     IfElse,
27     Switch,
28     Case,
29     FunctionDefinition,
30     Aggregate,
31     Block,
32     GlobalQualifierDeclaration,
33     Declaration,
34     Loop,
35     Branch,
36 };
37 
38 // This is a function like object instead of a function that stack allocates this because
39 // TIntermTraverser is a heavy object to construct.
40 class GetNodeType : private TIntermTraverser
41 {
42     NodeType nodeType;
43 
44   public:
GetNodeType()45     GetNodeType() : TIntermTraverser(true, false, false) {}
46 
operator()47     NodeType operator()(TIntermNode &node)
48     {
49         node.visit(Visit::PreVisit, this);
50         return nodeType;
51     }
52 
53   private:
visitSymbol(TIntermSymbol *)54     void visitSymbol(TIntermSymbol *) override { nodeType = NodeType::Symbol; }
55 
visitConstantUnion(TIntermConstantUnion *)56     void visitConstantUnion(TIntermConstantUnion *) override { nodeType = NodeType::ConstantUnion; }
57 
visitFunctionPrototype(TIntermFunctionPrototype *)58     void visitFunctionPrototype(TIntermFunctionPrototype *) override
59     {
60         nodeType = NodeType::FunctionPrototype;
61     }
62 
visitPreprocessorDirective(TIntermPreprocessorDirective *)63     void visitPreprocessorDirective(TIntermPreprocessorDirective *) override
64     {
65         nodeType = NodeType::PreprocessorDirective;
66     }
67 
visitSwizzle(Visit,TIntermSwizzle *)68     bool visitSwizzle(Visit, TIntermSwizzle *) override
69     {
70         nodeType = NodeType::Swizzle;
71         return false;
72     }
73 
visitBinary(Visit,TIntermBinary *)74     bool visitBinary(Visit, TIntermBinary *) override
75     {
76         nodeType = NodeType::Binary;
77         return false;
78     }
79 
visitUnary(Visit,TIntermUnary *)80     bool visitUnary(Visit, TIntermUnary *) override
81     {
82         nodeType = NodeType::Unary;
83         return false;
84     }
85 
visitTernary(Visit,TIntermTernary *)86     bool visitTernary(Visit, TIntermTernary *) override
87     {
88         nodeType = NodeType::Ternary;
89         return false;
90     }
91 
visitIfElse(Visit,TIntermIfElse *)92     bool visitIfElse(Visit, TIntermIfElse *) override
93     {
94         nodeType = NodeType::IfElse;
95         return false;
96     }
97 
visitSwitch(Visit,TIntermSwitch *)98     bool visitSwitch(Visit, TIntermSwitch *) override
99     {
100         nodeType = NodeType::Switch;
101         return false;
102     }
103 
visitCase(Visit,TIntermCase *)104     bool visitCase(Visit, TIntermCase *) override
105     {
106         nodeType = NodeType::Case;
107         return false;
108     }
109 
visitFunctionDefinition(Visit,TIntermFunctionDefinition *)110     bool visitFunctionDefinition(Visit, TIntermFunctionDefinition *) override
111     {
112         nodeType = NodeType::FunctionDefinition;
113         return false;
114     }
115 
visitAggregate(Visit,TIntermAggregate *)116     bool visitAggregate(Visit, TIntermAggregate *) override
117     {
118         nodeType = NodeType::Aggregate;
119         return false;
120     }
121 
visitBlock(Visit,TIntermBlock *)122     bool visitBlock(Visit, TIntermBlock *) override
123     {
124         nodeType = NodeType::Block;
125         return false;
126     }
127 
visitGlobalQualifierDeclaration(Visit,TIntermGlobalQualifierDeclaration *)128     bool visitGlobalQualifierDeclaration(Visit, TIntermGlobalQualifierDeclaration *) override
129     {
130         nodeType = NodeType::GlobalQualifierDeclaration;
131         return false;
132     }
133 
visitDeclaration(Visit,TIntermDeclaration *)134     bool visitDeclaration(Visit, TIntermDeclaration *) override
135     {
136         nodeType = NodeType::Declaration;
137         return false;
138     }
139 
visitLoop(Visit,TIntermLoop *)140     bool visitLoop(Visit, TIntermLoop *) override
141     {
142         nodeType = NodeType::Loop;
143         return false;
144     }
145 
visitBranch(Visit,TIntermBranch *)146     bool visitBranch(Visit, TIntermBranch *) override
147     {
148         nodeType = NodeType::Branch;
149         return false;
150     }
151 };
152 
153 }  // namespace sh
154 
155 #endif  // COMPILER_TRANSLATOR_TRANSLATORMETALDIRECT_NODETYPE_H_
156