1 //
2 // Copyright 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_TREEUTIL_INTERMNODEPATTERNMATCHER_H_
12 #define COMPILER_TRANSLATOR_TREEUTIL_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 IsDynamicIndexingOfNonSSBOVectorOrMatrix(TIntermBinary *node);
28     static bool IsDynamicIndexingOfVectorOrMatrix(TIntermBinary *node);
29     static bool IsDynamicIndexingOfSwizzledVector(TIntermBinary *node);
30 
31     enum PatternType : unsigned int
32     {
33         // Matches expressions that are unfolded to if statements by UnfoldShortCircuitToIf
34         kUnfoldedShortCircuitExpression = 1u << 0u,
35 
36         // Matches expressions that return arrays with the exception of simple statements where a
37         // constructor or function call result is assigned.
38         kExpressionReturningArray = 1u << 1u,
39 
40         // Matches dynamic indexing of vectors or matrices in l-values.
41         kDynamicIndexingOfVectorOrMatrixInLValue = 1u << 2u,
42 
43         // Matches declarations with more than one declared variables.
44         kMultiDeclaration = 1u << 3u,
45 
46         // Matches declarations of arrays.
47         kArrayDeclaration = 1u << 4u,
48 
49         // Matches declarations of structs where the struct type does not have a name.
50         kNamelessStructDeclaration = 1u << 5u,
51 
52         // Matches array length() method.
53         kArrayLengthMethod = 1u << 6u,
54 
55         // Matches a vector or matrix constructor whose arguments are scalarized by the
56         // SH_SCALARIZE_VEC_OR_MAT_CONSTRUCTOR_ARGUMENTS workaround.
57         kScalarizedVecOrMatConstructor = 1u << 7u,
58     };
59     IntermNodePatternMatcher(const unsigned int mask);
60 
61     bool match(TIntermUnary *node) const;
62 
63     bool match(TIntermBinary *node, TIntermNode *parentNode) const;
64 
65     // Use this version for checking binary node matches in case you're using flag
66     // kDynamicIndexingOfVectorOrMatrixInLValue.
67     bool match(TIntermBinary *node, TIntermNode *parentNode, bool isLValueRequiredHere) const;
68 
69     bool match(TIntermAggregate *node, TIntermNode *parentNode) const;
70     bool match(TIntermTernary *node) const;
71     bool match(TIntermDeclaration *node) const;
72 
73   private:
74     const unsigned int mMask;
75 
76     bool matchInternal(TIntermBinary *node, TIntermNode *parentNode) const;
77 };
78 
79 }  // namespace sh
80 
81 #endif  // COMPILER_TRANSLATOR_TREEUTIL_INTERMNODEPATTERNMATCHER_H_
82