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