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 | |
| 9 | #include "compiler/translator/ParseContext.h" |
| 10 | |
| 11 | bool ValidateSwitch::validate(TBasicType switchType, TParseContext *context, |
| 12 | TIntermAggregate *statementList, const TSourceLoc &loc) |
| 13 | { |
| 14 | ValidateSwitch validate(switchType, context); |
| 15 | ASSERT(statementList); |
| 16 | statementList->traverse(&validate); |
| 17 | return validate.validateInternal(loc); |
| 18 | } |
| 19 | |
| 20 | ValidateSwitch::ValidateSwitch(TBasicType switchType, TParseContext *context) |
| 21 | : TIntermTraverser(true, false, true), |
| 22 | mSwitchType(switchType), |
| 23 | mContext(context), |
| 24 | mCaseTypeMismatch(false), |
| 25 | mFirstCaseFound(false), |
| 26 | mStatementBeforeCase(false), |
| 27 | mLastStatementWasCase(false), |
| 28 | mControlFlowDepth(0), |
| 29 | mCaseInsideControlFlow(false), |
| 30 | mDefaultCount(0), |
| 31 | mDuplicateCases(false) |
| 32 | {} |
| 33 | |
| 34 | void ValidateSwitch::visitSymbol(TIntermSymbol *) |
| 35 | { |
| 36 | if (!mFirstCaseFound) |
| 37 | mStatementBeforeCase = true; |
| 38 | mLastStatementWasCase = false; |
| 39 | } |
| 40 | |
| 41 | void ValidateSwitch::visitConstantUnion(TIntermConstantUnion *) |
| 42 | { |
| 43 | // Conditions of case labels are not traversed, so this is some other constant |
| 44 | // Could be just a statement like "0;" |
| 45 | if (!mFirstCaseFound) |
| 46 | mStatementBeforeCase = true; |
| 47 | mLastStatementWasCase = false; |
| 48 | } |
| 49 | |
| 50 | bool ValidateSwitch::visitBinary(Visit, TIntermBinary *) |
| 51 | { |
| 52 | if (!mFirstCaseFound) |
| 53 | mStatementBeforeCase = true; |
| 54 | mLastStatementWasCase = false; |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | bool ValidateSwitch::visitUnary(Visit, TIntermUnary *) |
| 59 | { |
| 60 | if (!mFirstCaseFound) |
| 61 | mStatementBeforeCase = true; |
Olli Etuaho | d0bad2c | 2016-09-09 18:01:16 +0300 | [diff] [blame] | 62 | mLastStatementWasCase = false; |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | bool ValidateSwitch::visitTernary(Visit, TIntermTernary *) |
| 67 | { |
| 68 | if (!mFirstCaseFound) |
| 69 | mStatementBeforeCase = true; |
Olli Etuaho | ac5274d | 2015-02-20 10:19:08 +0200 | [diff] [blame] | 70 | mLastStatementWasCase = false; |
| 71 | return true; |
| 72 | } |
| 73 | |
Olli Etuaho | 5796127 | 2016-09-14 13:57:46 +0300 | [diff] [blame] | 74 | bool ValidateSwitch::visitIfElse(Visit visit, TIntermIfElse *) |
Olli Etuaho | ac5274d | 2015-02-20 10:19:08 +0200 | [diff] [blame] | 75 | { |
| 76 | if (visit == PreVisit) |
| 77 | ++mControlFlowDepth; |
| 78 | if (visit == PostVisit) |
| 79 | --mControlFlowDepth; |
| 80 | if (!mFirstCaseFound) |
| 81 | mStatementBeforeCase = true; |
| 82 | mLastStatementWasCase = false; |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | bool ValidateSwitch::visitSwitch(Visit, TIntermSwitch *) |
| 87 | { |
| 88 | if (!mFirstCaseFound) |
| 89 | mStatementBeforeCase = true; |
| 90 | mLastStatementWasCase = false; |
| 91 | // Don't go into nested switch statements |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | bool ValidateSwitch::visitCase(Visit, TIntermCase *node) |
| 96 | { |
| 97 | const char *nodeStr = node->hasCondition() ? "case" : "default"; |
| 98 | if (mControlFlowDepth > 0) |
| 99 | { |
| 100 | mContext->error(node->getLine(), "label statement nested inside control flow", nodeStr); |
| 101 | mCaseInsideControlFlow = true; |
| 102 | } |
| 103 | mFirstCaseFound = true; |
| 104 | mLastStatementWasCase = true; |
| 105 | if (!node->hasCondition()) |
| 106 | { |
| 107 | ++mDefaultCount; |
| 108 | if (mDefaultCount > 1) |
| 109 | { |
| 110 | mContext->error(node->getLine(), "duplicate default label", nodeStr); |
| 111 | } |
| 112 | } |
| 113 | else |
| 114 | { |
| 115 | TIntermConstantUnion *condition = node->getCondition()->getAsConstantUnion(); |
| 116 | if (condition == nullptr) |
| 117 | { |
| 118 | // This can happen in error cases. |
| 119 | return false; |
| 120 | } |
| 121 | TBasicType conditionType = condition->getBasicType(); |
| 122 | if (conditionType != mSwitchType) |
| 123 | { |
| 124 | mContext->error(condition->getLine(), |
| 125 | "case label type does not match switch init-expression type", nodeStr); |
| 126 | mCaseTypeMismatch = true; |
| 127 | } |
| 128 | |
| 129 | if (conditionType == EbtInt) |
| 130 | { |
| 131 | int iConst = condition->getIConst(0); |
| 132 | if (mCasesSigned.find(iConst) != mCasesSigned.end()) |
| 133 | { |
| 134 | mContext->error(condition->getLine(), "duplicate case label", nodeStr); |
| 135 | mDuplicateCases = true; |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | mCasesSigned.insert(iConst); |
| 140 | } |
| 141 | } |
Olli Etuaho | c8716df | 2015-02-26 17:17:22 +0200 | [diff] [blame] | 142 | else if (conditionType == EbtUInt) |
Olli Etuaho | ac5274d | 2015-02-20 10:19:08 +0200 | [diff] [blame] | 143 | { |
Olli Etuaho | ac5274d | 2015-02-20 10:19:08 +0200 | [diff] [blame] | 144 | unsigned int uConst = condition->getUConst(0); |
| 145 | if (mCasesUnsigned.find(uConst) != mCasesUnsigned.end()) |
| 146 | { |
| 147 | mContext->error(condition->getLine(), "duplicate case label", nodeStr); |
| 148 | mDuplicateCases = true; |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | mCasesUnsigned.insert(uConst); |
| 153 | } |
Olli Etuaho | ac5274d | 2015-02-20 10:19:08 +0200 | [diff] [blame] | 154 | } |
Olli Etuaho | c8716df | 2015-02-26 17:17:22 +0200 | [diff] [blame] | 155 | // Other types are possible only in error cases, where the error has already been generated |
| 156 | // when parsing the case statement. |
Olli Etuaho | ac5274d | 2015-02-20 10:19:08 +0200 | [diff] [blame] | 157 | } |
| 158 | // Don't traverse the condition of the case statement |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | bool ValidateSwitch::visitAggregate(Visit visit, TIntermAggregate *) |
| 163 | { |
| 164 | if (getParentNode() != nullptr) |
| 165 | { |
| 166 | // This is not the statementList node, but some other node. |
| 167 | if (!mFirstCaseFound) |
| 168 | mStatementBeforeCase = true; |
| 169 | mLastStatementWasCase = false; |
| 170 | } |
| 171 | return true; |
| 172 | } |
| 173 | |
| 174 | bool ValidateSwitch::visitLoop(Visit visit, TIntermLoop *) |
| 175 | { |
| 176 | if (visit == PreVisit) |
| 177 | ++mControlFlowDepth; |
| 178 | if (visit == PostVisit) |
| 179 | --mControlFlowDepth; |
| 180 | if (!mFirstCaseFound) |
| 181 | mStatementBeforeCase = true; |
| 182 | mLastStatementWasCase = false; |
| 183 | return true; |
| 184 | } |
| 185 | |
| 186 | bool ValidateSwitch::visitBranch(Visit, TIntermBranch *) |
| 187 | { |
| 188 | if (!mFirstCaseFound) |
| 189 | mStatementBeforeCase = true; |
| 190 | mLastStatementWasCase = false; |
| 191 | return true; |
| 192 | } |
| 193 | |
| 194 | bool ValidateSwitch::validateInternal(const TSourceLoc &loc) |
| 195 | { |
| 196 | if (mStatementBeforeCase) |
| 197 | { |
| 198 | mContext->error(loc, |
| 199 | "statement before the first label", "switch"); |
| 200 | } |
| 201 | if (mLastStatementWasCase) |
| 202 | { |
| 203 | mContext->error(loc, |
| 204 | "no statement between the last label and the end of the switch statement", "switch"); |
| 205 | } |
| 206 | return !mStatementBeforeCase && !mLastStatementWasCase && !mCaseInsideControlFlow && |
| 207 | !mCaseTypeMismatch && mDefaultCount <= 1 && !mDuplicateCases; |
| 208 | } |