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