xref: /aosp_15_r20/external/angle/src/compiler/translator/ValidateSwitch.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1*8975f5c5SAndroid Build Coastguard Worker //
2*8975f5c5SAndroid Build Coastguard Worker // Copyright 2002 The ANGLE Project Authors. All rights reserved.
3*8975f5c5SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
4*8975f5c5SAndroid Build Coastguard Worker // found in the LICENSE file.
5*8975f5c5SAndroid Build Coastguard Worker //
6*8975f5c5SAndroid Build Coastguard Worker 
7*8975f5c5SAndroid Build Coastguard Worker #include "compiler/translator/ValidateSwitch.h"
8*8975f5c5SAndroid Build Coastguard Worker 
9*8975f5c5SAndroid Build Coastguard Worker #include "compiler/translator/Diagnostics.h"
10*8975f5c5SAndroid Build Coastguard Worker #include "compiler/translator/tree_util/IntermTraverse.h"
11*8975f5c5SAndroid Build Coastguard Worker 
12*8975f5c5SAndroid Build Coastguard Worker namespace sh
13*8975f5c5SAndroid Build Coastguard Worker {
14*8975f5c5SAndroid Build Coastguard Worker 
15*8975f5c5SAndroid Build Coastguard Worker namespace
16*8975f5c5SAndroid Build Coastguard Worker {
17*8975f5c5SAndroid Build Coastguard Worker 
18*8975f5c5SAndroid Build Coastguard Worker const int kMaxAllowedTraversalDepth = 256;
19*8975f5c5SAndroid Build Coastguard Worker 
20*8975f5c5SAndroid Build Coastguard Worker class ValidateSwitch : public TIntermTraverser
21*8975f5c5SAndroid Build Coastguard Worker {
22*8975f5c5SAndroid Build Coastguard Worker   public:
23*8975f5c5SAndroid Build Coastguard Worker     static bool validate(TBasicType switchType,
24*8975f5c5SAndroid Build Coastguard Worker                          TDiagnostics *diagnostics,
25*8975f5c5SAndroid Build Coastguard Worker                          TIntermBlock *statementList,
26*8975f5c5SAndroid Build Coastguard Worker                          const TSourceLoc &loc);
27*8975f5c5SAndroid Build Coastguard Worker 
28*8975f5c5SAndroid Build Coastguard Worker     void visitSymbol(TIntermSymbol *) override;
29*8975f5c5SAndroid Build Coastguard Worker     void visitConstantUnion(TIntermConstantUnion *) override;
30*8975f5c5SAndroid Build Coastguard Worker     bool visitDeclaration(Visit, TIntermDeclaration *) override;
31*8975f5c5SAndroid Build Coastguard Worker     bool visitBlock(Visit visit, TIntermBlock *) override;
32*8975f5c5SAndroid Build Coastguard Worker     bool visitBinary(Visit, TIntermBinary *) override;
33*8975f5c5SAndroid Build Coastguard Worker     bool visitUnary(Visit, TIntermUnary *) override;
34*8975f5c5SAndroid Build Coastguard Worker     bool visitTernary(Visit, TIntermTernary *) override;
35*8975f5c5SAndroid Build Coastguard Worker     bool visitSwizzle(Visit, TIntermSwizzle *) override;
36*8975f5c5SAndroid Build Coastguard Worker     bool visitIfElse(Visit visit, TIntermIfElse *) override;
37*8975f5c5SAndroid Build Coastguard Worker     bool visitSwitch(Visit, TIntermSwitch *) override;
38*8975f5c5SAndroid Build Coastguard Worker     bool visitCase(Visit, TIntermCase *node) override;
39*8975f5c5SAndroid Build Coastguard Worker     bool visitAggregate(Visit, TIntermAggregate *) override;
40*8975f5c5SAndroid Build Coastguard Worker     bool visitLoop(Visit visit, TIntermLoop *) override;
41*8975f5c5SAndroid Build Coastguard Worker     bool visitBranch(Visit, TIntermBranch *) override;
42*8975f5c5SAndroid Build Coastguard Worker 
43*8975f5c5SAndroid Build Coastguard Worker   private:
44*8975f5c5SAndroid Build Coastguard Worker     ValidateSwitch(TBasicType switchType, TDiagnostics *context);
45*8975f5c5SAndroid Build Coastguard Worker 
46*8975f5c5SAndroid Build Coastguard Worker     bool validateInternal(const TSourceLoc &loc);
47*8975f5c5SAndroid Build Coastguard Worker 
48*8975f5c5SAndroid Build Coastguard Worker     TBasicType mSwitchType;
49*8975f5c5SAndroid Build Coastguard Worker     TDiagnostics *mDiagnostics;
50*8975f5c5SAndroid Build Coastguard Worker     bool mCaseTypeMismatch;
51*8975f5c5SAndroid Build Coastguard Worker     bool mFirstCaseFound;
52*8975f5c5SAndroid Build Coastguard Worker     bool mStatementBeforeCase;
53*8975f5c5SAndroid Build Coastguard Worker     bool mLastStatementWasCase;
54*8975f5c5SAndroid Build Coastguard Worker     int mControlFlowDepth;
55*8975f5c5SAndroid Build Coastguard Worker     bool mCaseInsideControlFlow;
56*8975f5c5SAndroid Build Coastguard Worker     int mDefaultCount;
57*8975f5c5SAndroid Build Coastguard Worker     std::set<int> mCasesSigned;
58*8975f5c5SAndroid Build Coastguard Worker     std::set<unsigned int> mCasesUnsigned;
59*8975f5c5SAndroid Build Coastguard Worker     bool mDuplicateCases;
60*8975f5c5SAndroid Build Coastguard Worker };
61*8975f5c5SAndroid Build Coastguard Worker 
validate(TBasicType switchType,TDiagnostics * diagnostics,TIntermBlock * statementList,const TSourceLoc & loc)62*8975f5c5SAndroid Build Coastguard Worker bool ValidateSwitch::validate(TBasicType switchType,
63*8975f5c5SAndroid Build Coastguard Worker                               TDiagnostics *diagnostics,
64*8975f5c5SAndroid Build Coastguard Worker                               TIntermBlock *statementList,
65*8975f5c5SAndroid Build Coastguard Worker                               const TSourceLoc &loc)
66*8975f5c5SAndroid Build Coastguard Worker {
67*8975f5c5SAndroid Build Coastguard Worker     ValidateSwitch validate(switchType, diagnostics);
68*8975f5c5SAndroid Build Coastguard Worker     ASSERT(statementList);
69*8975f5c5SAndroid Build Coastguard Worker     statementList->traverse(&validate);
70*8975f5c5SAndroid Build Coastguard Worker     return validate.validateInternal(loc);
71*8975f5c5SAndroid Build Coastguard Worker }
72*8975f5c5SAndroid Build Coastguard Worker 
ValidateSwitch(TBasicType switchType,TDiagnostics * diagnostics)73*8975f5c5SAndroid Build Coastguard Worker ValidateSwitch::ValidateSwitch(TBasicType switchType, TDiagnostics *diagnostics)
74*8975f5c5SAndroid Build Coastguard Worker     : TIntermTraverser(true, false, true, nullptr),
75*8975f5c5SAndroid Build Coastguard Worker       mSwitchType(switchType),
76*8975f5c5SAndroid Build Coastguard Worker       mDiagnostics(diagnostics),
77*8975f5c5SAndroid Build Coastguard Worker       mCaseTypeMismatch(false),
78*8975f5c5SAndroid Build Coastguard Worker       mFirstCaseFound(false),
79*8975f5c5SAndroid Build Coastguard Worker       mStatementBeforeCase(false),
80*8975f5c5SAndroid Build Coastguard Worker       mLastStatementWasCase(false),
81*8975f5c5SAndroid Build Coastguard Worker       mControlFlowDepth(0),
82*8975f5c5SAndroid Build Coastguard Worker       mCaseInsideControlFlow(false),
83*8975f5c5SAndroid Build Coastguard Worker       mDefaultCount(0),
84*8975f5c5SAndroid Build Coastguard Worker       mDuplicateCases(false)
85*8975f5c5SAndroid Build Coastguard Worker {
86*8975f5c5SAndroid Build Coastguard Worker     setMaxAllowedDepth(kMaxAllowedTraversalDepth);
87*8975f5c5SAndroid Build Coastguard Worker }
88*8975f5c5SAndroid Build Coastguard Worker 
visitSymbol(TIntermSymbol *)89*8975f5c5SAndroid Build Coastguard Worker void ValidateSwitch::visitSymbol(TIntermSymbol *)
90*8975f5c5SAndroid Build Coastguard Worker {
91*8975f5c5SAndroid Build Coastguard Worker     if (!mFirstCaseFound)
92*8975f5c5SAndroid Build Coastguard Worker         mStatementBeforeCase = true;
93*8975f5c5SAndroid Build Coastguard Worker     mLastStatementWasCase = false;
94*8975f5c5SAndroid Build Coastguard Worker }
95*8975f5c5SAndroid Build Coastguard Worker 
visitConstantUnion(TIntermConstantUnion *)96*8975f5c5SAndroid Build Coastguard Worker void ValidateSwitch::visitConstantUnion(TIntermConstantUnion *)
97*8975f5c5SAndroid Build Coastguard Worker {
98*8975f5c5SAndroid Build Coastguard Worker     // Conditions of case labels are not traversed, so this is some other constant
99*8975f5c5SAndroid Build Coastguard Worker     // Could be just a statement like "0;"
100*8975f5c5SAndroid Build Coastguard Worker     if (!mFirstCaseFound)
101*8975f5c5SAndroid Build Coastguard Worker         mStatementBeforeCase = true;
102*8975f5c5SAndroid Build Coastguard Worker     mLastStatementWasCase = false;
103*8975f5c5SAndroid Build Coastguard Worker }
104*8975f5c5SAndroid Build Coastguard Worker 
visitDeclaration(Visit,TIntermDeclaration *)105*8975f5c5SAndroid Build Coastguard Worker bool ValidateSwitch::visitDeclaration(Visit, TIntermDeclaration *)
106*8975f5c5SAndroid Build Coastguard Worker {
107*8975f5c5SAndroid Build Coastguard Worker     if (!mFirstCaseFound)
108*8975f5c5SAndroid Build Coastguard Worker         mStatementBeforeCase = true;
109*8975f5c5SAndroid Build Coastguard Worker     mLastStatementWasCase = false;
110*8975f5c5SAndroid Build Coastguard Worker     return true;
111*8975f5c5SAndroid Build Coastguard Worker }
112*8975f5c5SAndroid Build Coastguard Worker 
visitBlock(Visit visit,TIntermBlock *)113*8975f5c5SAndroid Build Coastguard Worker bool ValidateSwitch::visitBlock(Visit visit, TIntermBlock *)
114*8975f5c5SAndroid Build Coastguard Worker {
115*8975f5c5SAndroid Build Coastguard Worker     if (getParentNode() != nullptr)
116*8975f5c5SAndroid Build Coastguard Worker     {
117*8975f5c5SAndroid Build Coastguard Worker         if (!mFirstCaseFound)
118*8975f5c5SAndroid Build Coastguard Worker             mStatementBeforeCase = true;
119*8975f5c5SAndroid Build Coastguard Worker         mLastStatementWasCase = false;
120*8975f5c5SAndroid Build Coastguard Worker         if (visit == PreVisit)
121*8975f5c5SAndroid Build Coastguard Worker             ++mControlFlowDepth;
122*8975f5c5SAndroid Build Coastguard Worker         if (visit == PostVisit)
123*8975f5c5SAndroid Build Coastguard Worker             --mControlFlowDepth;
124*8975f5c5SAndroid Build Coastguard Worker     }
125*8975f5c5SAndroid Build Coastguard Worker     return true;
126*8975f5c5SAndroid Build Coastguard Worker }
127*8975f5c5SAndroid Build Coastguard Worker 
visitBinary(Visit,TIntermBinary *)128*8975f5c5SAndroid Build Coastguard Worker bool ValidateSwitch::visitBinary(Visit, TIntermBinary *)
129*8975f5c5SAndroid Build Coastguard Worker {
130*8975f5c5SAndroid Build Coastguard Worker     if (!mFirstCaseFound)
131*8975f5c5SAndroid Build Coastguard Worker         mStatementBeforeCase = true;
132*8975f5c5SAndroid Build Coastguard Worker     mLastStatementWasCase = false;
133*8975f5c5SAndroid Build Coastguard Worker     return true;
134*8975f5c5SAndroid Build Coastguard Worker }
135*8975f5c5SAndroid Build Coastguard Worker 
visitUnary(Visit,TIntermUnary *)136*8975f5c5SAndroid Build Coastguard Worker bool ValidateSwitch::visitUnary(Visit, TIntermUnary *)
137*8975f5c5SAndroid Build Coastguard Worker {
138*8975f5c5SAndroid Build Coastguard Worker     if (!mFirstCaseFound)
139*8975f5c5SAndroid Build Coastguard Worker         mStatementBeforeCase = true;
140*8975f5c5SAndroid Build Coastguard Worker     mLastStatementWasCase = false;
141*8975f5c5SAndroid Build Coastguard Worker     return true;
142*8975f5c5SAndroid Build Coastguard Worker }
143*8975f5c5SAndroid Build Coastguard Worker 
visitTernary(Visit,TIntermTernary *)144*8975f5c5SAndroid Build Coastguard Worker bool ValidateSwitch::visitTernary(Visit, TIntermTernary *)
145*8975f5c5SAndroid Build Coastguard Worker {
146*8975f5c5SAndroid Build Coastguard Worker     if (!mFirstCaseFound)
147*8975f5c5SAndroid Build Coastguard Worker         mStatementBeforeCase = true;
148*8975f5c5SAndroid Build Coastguard Worker     mLastStatementWasCase = false;
149*8975f5c5SAndroid Build Coastguard Worker     return true;
150*8975f5c5SAndroid Build Coastguard Worker }
151*8975f5c5SAndroid Build Coastguard Worker 
visitSwizzle(Visit,TIntermSwizzle *)152*8975f5c5SAndroid Build Coastguard Worker bool ValidateSwitch::visitSwizzle(Visit, TIntermSwizzle *)
153*8975f5c5SAndroid Build Coastguard Worker {
154*8975f5c5SAndroid Build Coastguard Worker     if (!mFirstCaseFound)
155*8975f5c5SAndroid Build Coastguard Worker         mStatementBeforeCase = true;
156*8975f5c5SAndroid Build Coastguard Worker     mLastStatementWasCase = false;
157*8975f5c5SAndroid Build Coastguard Worker     return true;
158*8975f5c5SAndroid Build Coastguard Worker }
159*8975f5c5SAndroid Build Coastguard Worker 
visitIfElse(Visit visit,TIntermIfElse *)160*8975f5c5SAndroid Build Coastguard Worker bool ValidateSwitch::visitIfElse(Visit visit, TIntermIfElse *)
161*8975f5c5SAndroid Build Coastguard Worker {
162*8975f5c5SAndroid Build Coastguard Worker     if (visit == PreVisit)
163*8975f5c5SAndroid Build Coastguard Worker         ++mControlFlowDepth;
164*8975f5c5SAndroid Build Coastguard Worker     if (visit == PostVisit)
165*8975f5c5SAndroid Build Coastguard Worker         --mControlFlowDepth;
166*8975f5c5SAndroid Build Coastguard Worker     if (!mFirstCaseFound)
167*8975f5c5SAndroid Build Coastguard Worker         mStatementBeforeCase = true;
168*8975f5c5SAndroid Build Coastguard Worker     mLastStatementWasCase = false;
169*8975f5c5SAndroid Build Coastguard Worker     return true;
170*8975f5c5SAndroid Build Coastguard Worker }
171*8975f5c5SAndroid Build Coastguard Worker 
visitSwitch(Visit,TIntermSwitch *)172*8975f5c5SAndroid Build Coastguard Worker bool ValidateSwitch::visitSwitch(Visit, TIntermSwitch *)
173*8975f5c5SAndroid Build Coastguard Worker {
174*8975f5c5SAndroid Build Coastguard Worker     if (!mFirstCaseFound)
175*8975f5c5SAndroid Build Coastguard Worker         mStatementBeforeCase = true;
176*8975f5c5SAndroid Build Coastguard Worker     mLastStatementWasCase = false;
177*8975f5c5SAndroid Build Coastguard Worker     // Don't go into nested switch statements
178*8975f5c5SAndroid Build Coastguard Worker     return false;
179*8975f5c5SAndroid Build Coastguard Worker }
180*8975f5c5SAndroid Build Coastguard Worker 
visitCase(Visit,TIntermCase * node)181*8975f5c5SAndroid Build Coastguard Worker bool ValidateSwitch::visitCase(Visit, TIntermCase *node)
182*8975f5c5SAndroid Build Coastguard Worker {
183*8975f5c5SAndroid Build Coastguard Worker     const char *nodeStr = node->hasCondition() ? "case" : "default";
184*8975f5c5SAndroid Build Coastguard Worker     if (mControlFlowDepth > 0)
185*8975f5c5SAndroid Build Coastguard Worker     {
186*8975f5c5SAndroid Build Coastguard Worker         mDiagnostics->error(node->getLine(), "label statement nested inside control flow", nodeStr);
187*8975f5c5SAndroid Build Coastguard Worker         mCaseInsideControlFlow = true;
188*8975f5c5SAndroid Build Coastguard Worker     }
189*8975f5c5SAndroid Build Coastguard Worker     mFirstCaseFound       = true;
190*8975f5c5SAndroid Build Coastguard Worker     mLastStatementWasCase = true;
191*8975f5c5SAndroid Build Coastguard Worker     if (!node->hasCondition())
192*8975f5c5SAndroid Build Coastguard Worker     {
193*8975f5c5SAndroid Build Coastguard Worker         ++mDefaultCount;
194*8975f5c5SAndroid Build Coastguard Worker         if (mDefaultCount > 1)
195*8975f5c5SAndroid Build Coastguard Worker         {
196*8975f5c5SAndroid Build Coastguard Worker             mDiagnostics->error(node->getLine(), "duplicate default label", nodeStr);
197*8975f5c5SAndroid Build Coastguard Worker         }
198*8975f5c5SAndroid Build Coastguard Worker     }
199*8975f5c5SAndroid Build Coastguard Worker     else
200*8975f5c5SAndroid Build Coastguard Worker     {
201*8975f5c5SAndroid Build Coastguard Worker         TIntermConstantUnion *condition = node->getCondition()->getAsConstantUnion();
202*8975f5c5SAndroid Build Coastguard Worker         if (condition == nullptr)
203*8975f5c5SAndroid Build Coastguard Worker         {
204*8975f5c5SAndroid Build Coastguard Worker             // This can happen in error cases.
205*8975f5c5SAndroid Build Coastguard Worker             return false;
206*8975f5c5SAndroid Build Coastguard Worker         }
207*8975f5c5SAndroid Build Coastguard Worker         TBasicType conditionType = condition->getBasicType();
208*8975f5c5SAndroid Build Coastguard Worker         if (conditionType != mSwitchType)
209*8975f5c5SAndroid Build Coastguard Worker         {
210*8975f5c5SAndroid Build Coastguard Worker             mDiagnostics->error(condition->getLine(),
211*8975f5c5SAndroid Build Coastguard Worker                                 "case label type does not match switch init-expression type",
212*8975f5c5SAndroid Build Coastguard Worker                                 nodeStr);
213*8975f5c5SAndroid Build Coastguard Worker             mCaseTypeMismatch = true;
214*8975f5c5SAndroid Build Coastguard Worker         }
215*8975f5c5SAndroid Build Coastguard Worker 
216*8975f5c5SAndroid Build Coastguard Worker         if (conditionType == EbtInt)
217*8975f5c5SAndroid Build Coastguard Worker         {
218*8975f5c5SAndroid Build Coastguard Worker             int iConst = condition->getIConst(0);
219*8975f5c5SAndroid Build Coastguard Worker             if (mCasesSigned.find(iConst) != mCasesSigned.end())
220*8975f5c5SAndroid Build Coastguard Worker             {
221*8975f5c5SAndroid Build Coastguard Worker                 mDiagnostics->error(condition->getLine(), "duplicate case label", nodeStr);
222*8975f5c5SAndroid Build Coastguard Worker                 mDuplicateCases = true;
223*8975f5c5SAndroid Build Coastguard Worker             }
224*8975f5c5SAndroid Build Coastguard Worker             else
225*8975f5c5SAndroid Build Coastguard Worker             {
226*8975f5c5SAndroid Build Coastguard Worker                 mCasesSigned.insert(iConst);
227*8975f5c5SAndroid Build Coastguard Worker             }
228*8975f5c5SAndroid Build Coastguard Worker         }
229*8975f5c5SAndroid Build Coastguard Worker         else if (conditionType == EbtUInt)
230*8975f5c5SAndroid Build Coastguard Worker         {
231*8975f5c5SAndroid Build Coastguard Worker             unsigned int uConst = condition->getUConst(0);
232*8975f5c5SAndroid Build Coastguard Worker             if (mCasesUnsigned.find(uConst) != mCasesUnsigned.end())
233*8975f5c5SAndroid Build Coastguard Worker             {
234*8975f5c5SAndroid Build Coastguard Worker                 mDiagnostics->error(condition->getLine(), "duplicate case label", nodeStr);
235*8975f5c5SAndroid Build Coastguard Worker                 mDuplicateCases = true;
236*8975f5c5SAndroid Build Coastguard Worker             }
237*8975f5c5SAndroid Build Coastguard Worker             else
238*8975f5c5SAndroid Build Coastguard Worker             {
239*8975f5c5SAndroid Build Coastguard Worker                 mCasesUnsigned.insert(uConst);
240*8975f5c5SAndroid Build Coastguard Worker             }
241*8975f5c5SAndroid Build Coastguard Worker         }
242*8975f5c5SAndroid Build Coastguard Worker         // Other types are possible only in error cases, where the error has already been generated
243*8975f5c5SAndroid Build Coastguard Worker         // when parsing the case statement.
244*8975f5c5SAndroid Build Coastguard Worker     }
245*8975f5c5SAndroid Build Coastguard Worker     // Don't traverse the condition of the case statement
246*8975f5c5SAndroid Build Coastguard Worker     return false;
247*8975f5c5SAndroid Build Coastguard Worker }
248*8975f5c5SAndroid Build Coastguard Worker 
visitAggregate(Visit visit,TIntermAggregate *)249*8975f5c5SAndroid Build Coastguard Worker bool ValidateSwitch::visitAggregate(Visit visit, TIntermAggregate *)
250*8975f5c5SAndroid Build Coastguard Worker {
251*8975f5c5SAndroid Build Coastguard Worker     if (getParentNode() != nullptr)
252*8975f5c5SAndroid Build Coastguard Worker     {
253*8975f5c5SAndroid Build Coastguard Worker         // This is not the statementList node, but some other node.
254*8975f5c5SAndroid Build Coastguard Worker         if (!mFirstCaseFound)
255*8975f5c5SAndroid Build Coastguard Worker             mStatementBeforeCase = true;
256*8975f5c5SAndroid Build Coastguard Worker         mLastStatementWasCase = false;
257*8975f5c5SAndroid Build Coastguard Worker     }
258*8975f5c5SAndroid Build Coastguard Worker     return true;
259*8975f5c5SAndroid Build Coastguard Worker }
260*8975f5c5SAndroid Build Coastguard Worker 
visitLoop(Visit visit,TIntermLoop *)261*8975f5c5SAndroid Build Coastguard Worker bool ValidateSwitch::visitLoop(Visit visit, TIntermLoop *)
262*8975f5c5SAndroid Build Coastguard Worker {
263*8975f5c5SAndroid Build Coastguard Worker     if (visit == PreVisit)
264*8975f5c5SAndroid Build Coastguard Worker         ++mControlFlowDepth;
265*8975f5c5SAndroid Build Coastguard Worker     if (visit == PostVisit)
266*8975f5c5SAndroid Build Coastguard Worker         --mControlFlowDepth;
267*8975f5c5SAndroid Build Coastguard Worker     if (!mFirstCaseFound)
268*8975f5c5SAndroid Build Coastguard Worker         mStatementBeforeCase = true;
269*8975f5c5SAndroid Build Coastguard Worker     mLastStatementWasCase = false;
270*8975f5c5SAndroid Build Coastguard Worker     return true;
271*8975f5c5SAndroid Build Coastguard Worker }
272*8975f5c5SAndroid Build Coastguard Worker 
visitBranch(Visit,TIntermBranch *)273*8975f5c5SAndroid Build Coastguard Worker bool ValidateSwitch::visitBranch(Visit, TIntermBranch *)
274*8975f5c5SAndroid Build Coastguard Worker {
275*8975f5c5SAndroid Build Coastguard Worker     if (!mFirstCaseFound)
276*8975f5c5SAndroid Build Coastguard Worker         mStatementBeforeCase = true;
277*8975f5c5SAndroid Build Coastguard Worker     mLastStatementWasCase = false;
278*8975f5c5SAndroid Build Coastguard Worker     return true;
279*8975f5c5SAndroid Build Coastguard Worker }
280*8975f5c5SAndroid Build Coastguard Worker 
validateInternal(const TSourceLoc & loc)281*8975f5c5SAndroid Build Coastguard Worker bool ValidateSwitch::validateInternal(const TSourceLoc &loc)
282*8975f5c5SAndroid Build Coastguard Worker {
283*8975f5c5SAndroid Build Coastguard Worker     if (mStatementBeforeCase)
284*8975f5c5SAndroid Build Coastguard Worker     {
285*8975f5c5SAndroid Build Coastguard Worker         mDiagnostics->error(loc, "statement before the first label", "switch");
286*8975f5c5SAndroid Build Coastguard Worker     }
287*8975f5c5SAndroid Build Coastguard Worker     if (mLastStatementWasCase)
288*8975f5c5SAndroid Build Coastguard Worker     {
289*8975f5c5SAndroid Build Coastguard Worker         // There have been some differences between versions of GLSL ES specs on whether this should
290*8975f5c5SAndroid Build Coastguard Worker         // be an error or not, but as of early 2018 the latest discussion is that this is an error
291*8975f5c5SAndroid Build Coastguard Worker         // also on GLSL ES versions newer than 3.00.
292*8975f5c5SAndroid Build Coastguard Worker         mDiagnostics->error(
293*8975f5c5SAndroid Build Coastguard Worker             loc, "no statement between the last label and the end of the switch statement",
294*8975f5c5SAndroid Build Coastguard Worker             "switch");
295*8975f5c5SAndroid Build Coastguard Worker     }
296*8975f5c5SAndroid Build Coastguard Worker     if (getMaxDepth() >= kMaxAllowedTraversalDepth)
297*8975f5c5SAndroid Build Coastguard Worker     {
298*8975f5c5SAndroid Build Coastguard Worker         mDiagnostics->error(loc, "too complex expressions inside a switch statement", "switch");
299*8975f5c5SAndroid Build Coastguard Worker     }
300*8975f5c5SAndroid Build Coastguard Worker     return !mStatementBeforeCase && !mLastStatementWasCase && !mCaseInsideControlFlow &&
301*8975f5c5SAndroid Build Coastguard Worker            !mCaseTypeMismatch && mDefaultCount <= 1 && !mDuplicateCases &&
302*8975f5c5SAndroid Build Coastguard Worker            getMaxDepth() < kMaxAllowedTraversalDepth;
303*8975f5c5SAndroid Build Coastguard Worker }
304*8975f5c5SAndroid Build Coastguard Worker 
305*8975f5c5SAndroid Build Coastguard Worker }  // anonymous namespace
306*8975f5c5SAndroid Build Coastguard Worker 
ValidateSwitchStatementList(TBasicType switchType,TDiagnostics * diagnostics,TIntermBlock * statementList,const TSourceLoc & loc)307*8975f5c5SAndroid Build Coastguard Worker bool ValidateSwitchStatementList(TBasicType switchType,
308*8975f5c5SAndroid Build Coastguard Worker                                  TDiagnostics *diagnostics,
309*8975f5c5SAndroid Build Coastguard Worker                                  TIntermBlock *statementList,
310*8975f5c5SAndroid Build Coastguard Worker                                  const TSourceLoc &loc)
311*8975f5c5SAndroid Build Coastguard Worker {
312*8975f5c5SAndroid Build Coastguard Worker     return ValidateSwitch::validate(switchType, diagnostics, statementList, loc);
313*8975f5c5SAndroid Build Coastguard Worker }
314*8975f5c5SAndroid Build Coastguard Worker 
315*8975f5c5SAndroid Build Coastguard Worker }  // namespace sh
316