1 //
2 // Copyright (c) 2016 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 // IntermNodePatternMatcher is a helper class for matching node trees to given patterns.
7 // It can be used whenever the same checks for certain node structures are common to multiple AST
8 // traversers.
9 //
10 
11 #ifndef COMPILER_TRANSLATOR_INTERMNODEPATTERNMATCHER_H_
12 #define COMPILER_TRANSLATOR_INTERMNODEPATTERNMATCHER_H_
13 
14 namespace sh
15 {
16 
17 class TIntermAggregate;
18 class TIntermBinary;
19 class TIntermDeclaration;
20 class TIntermNode;
21 class TIntermTernary;
22 class TIntermUnary;
23 
24 class IntermNodePatternMatcher
25 {
26   public:
27     static bool IsDynamicIndexingOfVectorOrMatrix(TIntermBinary *node);
28 
29     enum PatternType
30     {
31         // Matches expressions that are unfolded to if statements by UnfoldShortCircuitToIf
32         kUnfoldedShortCircuitExpression = 0x0001,
33 
34         // Matches expressions that return arrays with the exception of simple statements where a
35         // constructor or function call result is assigned.
36         kExpressionReturningArray = 0x0001 << 1,
37 
38         // Matches dynamic indexing of vectors or matrices in l-values.
39         kDynamicIndexingOfVectorOrMatrixInLValue = 0x0001 << 2,
40 
41         // Matches declarations with more than one declared variables.
42         kMultiDeclaration = 0x0001 << 3,
43 
44         // Matches declarations of arrays.
45         kArrayDeclaration = 0x0001 << 4,
46 
47         // Matches declarations of structs where the struct type does not have a name.
48         kNamelessStructDeclaration = 0x0001 << 5,
49 
50         // Matches array length() method.
51         kArrayLengthMethod = 0x0001 << 6
52     };
53     IntermNodePatternMatcher(const unsigned int mask);
54 
55     bool match(TIntermUnary *node);
56 
57     bool match(TIntermBinary *node, TIntermNode *parentNode);
58 
59     // Use this version for checking binary node matches in case you're using flag
60     // kDynamicIndexingOfVectorOrMatrixInLValue.
61     bool match(TIntermBinary *node, TIntermNode *parentNode, bool isLValueRequiredHere);
62 
63     bool match(TIntermAggregate *node, TIntermNode *parentNode);
64     bool match(TIntermTernary *node);
65     bool match(TIntermDeclaration *node);
66 
67   private:
68     const unsigned int mMask;
69 
70     bool matchInternal(TIntermBinary *node, TIntermNode *parentNode);
71 };
72 
73 }  // namespace sh
74 
75 #endif
76