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