• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "compiler/translator/TranslatorMetalDirect/Name.h"
8 #include "common/debug.h"
9 #include "compiler/translator/tree_util/IntermTraverse.h"
10 
11 using namespace sh;
12 
13 ////////////////////////////////////////////////////////////////////////////////
14 
15 template <typename T>
GetName(T const & object)16 static ImmutableString GetName(T const &object)
17 {
18     if (object.symbolType() == SymbolType::Empty)
19     {
20         return kEmptyImmutableString;
21     }
22     return object.name();
23 }
24 
Name(const TField & field)25 Name::Name(const TField &field) : Name(GetName(field), field.symbolType()) {}
26 
Name(const TSymbol & symbol)27 Name::Name(const TSymbol &symbol) : Name(GetName(symbol), symbol.symbolType()) {}
28 
operator ==(const Name & other) const29 bool Name::operator==(const Name &other) const
30 {
31     return mRawName == other.mRawName && mSymbolType == other.mSymbolType;
32 }
33 
operator !=(const Name & other) const34 bool Name::operator!=(const Name &other) const
35 {
36     return !(*this == other);
37 }
38 
operator <(const Name & other) const39 bool Name::operator<(const Name &other) const
40 {
41     if (mRawName < other.mRawName)
42     {
43         return true;
44     }
45     if (other.mRawName < mRawName)
46     {
47         return false;
48     }
49     return mSymbolType < other.mSymbolType;
50 }
51 
empty() const52 bool Name::empty() const
53 {
54     return mSymbolType == SymbolType::Empty;
55 }
56 
beginsWith(const Name & prefix) const57 bool Name::beginsWith(const Name &prefix) const
58 {
59     if (mSymbolType != prefix.mSymbolType)
60     {
61         return false;
62     }
63     return mRawName.beginsWith(prefix.mRawName);
64 }
65 
emit(TInfoSinkBase & out) const66 void Name::emit(TInfoSinkBase &out) const
67 {
68     switch (mSymbolType)
69     {
70         case SymbolType::BuiltIn:
71         case SymbolType::UserDefined:
72             ASSERT(!mRawName.empty());
73             out << mRawName;
74             break;
75 
76         case SymbolType::AngleInternal:
77             ASSERT(!mRawName.empty());
78             if (mRawName.beginsWith(kAngleInternalPrefix))
79             {
80                 out << mRawName;
81             }
82             else if (mRawName[0] != '_')
83             {
84                 out << kAngleInternalPrefix << '_' << mRawName;
85             }
86             else
87             {
88                 out << kAngleInternalPrefix << mRawName;
89             }
90             break;
91 
92         case SymbolType::Empty:
93             UNREACHABLE();
94             break;
95     }
96 }
97 
98 ////////////////////////////////////////////////////////////////////////////////
99 
100 namespace
101 {
102 
103 // NOTE: This matches more things than FindSymbolNode.
104 class ExpressionContainsNameVisitor : public TIntermTraverser
105 {
106     Name mName;
107     bool mFoundName = false;
108 
109   public:
ExpressionContainsNameVisitor(const Name & name)110     ExpressionContainsNameVisitor(const Name &name)
111         : TIntermTraverser(true, false, false), mName(name)
112     {}
113 
foundName() const114     bool foundName() const { return mFoundName; }
115 
visitSymbol(TIntermSymbol * node)116     void visitSymbol(TIntermSymbol *node) override
117     {
118         if (Name(node->variable()) == mName)
119         {
120             mFoundName = true;
121         }
122     }
123 
visitSwizzle(Visit,TIntermSwizzle *)124     bool visitSwizzle(Visit, TIntermSwizzle *) override { return !mFoundName; }
125 
visitBinary(Visit visit,TIntermBinary * node)126     bool visitBinary(Visit visit, TIntermBinary *node) override { return !mFoundName; }
127 
visitUnary(Visit visit,TIntermUnary * node)128     bool visitUnary(Visit visit, TIntermUnary *node) override { return !mFoundName; }
129 
visitTernary(Visit visit,TIntermTernary * node)130     bool visitTernary(Visit visit, TIntermTernary *node) override { return !mFoundName; }
131 
visitAggregate(Visit visit,TIntermAggregate * node)132     bool visitAggregate(Visit visit, TIntermAggregate *node) override
133     {
134         if (node->isConstructor())
135         {
136             const TType &type           = node->getType();
137             const TStructure *structure = type.getStruct();
138             if (structure && Name(*structure) == mName)
139             {
140                 mFoundName = true;
141             }
142         }
143         else
144         {
145             const TFunction *func = node->getFunction();
146             if (func && Name(*func) == mName)
147             {
148                 mFoundName = true;
149             }
150         }
151         return !mFoundName;
152     }
153 
visitIfElse(Visit visit,TIntermIfElse * node)154     bool visitIfElse(Visit visit, TIntermIfElse *node) override
155     {
156         UNREACHABLE();
157         return false;
158     }
159 
visitSwitch(Visit,TIntermSwitch *)160     bool visitSwitch(Visit, TIntermSwitch *) override
161     {
162         UNREACHABLE();
163         return false;
164     }
visitCase(Visit,TIntermCase *)165     bool visitCase(Visit, TIntermCase *) override
166     {
167         UNREACHABLE();
168         return false;
169     }
170 
visitFunctionPrototype(TIntermFunctionPrototype *)171     void visitFunctionPrototype(TIntermFunctionPrototype *) override { UNREACHABLE(); }
172 
visitFunctionDefinition(Visit,TIntermFunctionDefinition *)173     bool visitFunctionDefinition(Visit, TIntermFunctionDefinition *) override
174     {
175         UNREACHABLE();
176         return false;
177     }
178 
visitBlock(Visit,TIntermBlock *)179     bool visitBlock(Visit, TIntermBlock *) override
180     {
181         UNREACHABLE();
182         return false;
183     }
184 
visitGlobalQualifierDeclaration(Visit,TIntermGlobalQualifierDeclaration *)185     bool visitGlobalQualifierDeclaration(Visit, TIntermGlobalQualifierDeclaration *) override
186     {
187         UNREACHABLE();
188         return false;
189     }
190 
visitDeclaration(Visit,TIntermDeclaration *)191     bool visitDeclaration(Visit, TIntermDeclaration *) override
192     {
193         UNREACHABLE();
194         return false;
195     }
196 
visitLoop(Visit,TIntermLoop *)197     bool visitLoop(Visit, TIntermLoop *) override
198     {
199         UNREACHABLE();
200         return false;
201     }
202 
visitBranch(Visit,TIntermBranch *)203     bool visitBranch(Visit, TIntermBranch *) override
204     {
205         UNREACHABLE();
206         return false;
207     }
208 
visitPreprocessorDirective(TIntermPreprocessorDirective *)209     void visitPreprocessorDirective(TIntermPreprocessorDirective *) override { UNREACHABLE(); }
210 };
211 
212 }  // anonymous namespace
213 
ExpressionContainsName(const Name & name,TIntermTyped & node)214 bool sh::ExpressionContainsName(const Name &name, TIntermTyped &node)
215 {
216     ExpressionContainsNameVisitor visitor(name);
217     node.traverse(&visitor);
218     return visitor.foundName();
219 }
220