John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 1 | // |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 2 | // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved. |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
Nicolas Capens | cc863da | 2015-01-21 15:50:55 -0500 | [diff] [blame^] | 7 | #include "ParseHelper.h" |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 8 | |
| 9 | // |
| 10 | // Use this class to carry along data from node to node in |
| 11 | // the traversal |
| 12 | // |
| 13 | class TConstTraverser : public TIntermTraverser { |
| 14 | public: |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 15 | TConstTraverser(ConstantUnion* cUnion, bool singleConstParam, TOperator constructType, TInfoSink& sink, TType& t) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 16 | : error(false), |
| 17 | index(0), |
| 18 | unionArray(cUnion), |
| 19 | type(t), |
| 20 | constructorType(constructType), |
| 21 | singleConstantParam(singleConstParam), |
| 22 | infoSink(sink), |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 23 | size(0), |
| 24 | isMatrix(false), |
| 25 | matrixSize(0) { |
| 26 | } |
| 27 | |
| 28 | bool error; |
| 29 | |
| 30 | protected: |
| 31 | void visitSymbol(TIntermSymbol*); |
| 32 | void visitConstantUnion(TIntermConstantUnion*); |
| 33 | bool visitBinary(Visit visit, TIntermBinary*); |
| 34 | bool visitUnary(Visit visit, TIntermUnary*); |
| 35 | bool visitSelection(Visit visit, TIntermSelection*); |
| 36 | bool visitAggregate(Visit visit, TIntermAggregate*); |
| 37 | bool visitLoop(Visit visit, TIntermLoop*); |
| 38 | bool visitBranch(Visit visit, TIntermBranch*); |
| 39 | |
| 40 | int index; |
| 41 | ConstantUnion *unionArray; |
| 42 | TType type; |
| 43 | TOperator constructorType; |
| 44 | bool singleConstantParam; |
| 45 | TInfoSink& infoSink; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 46 | int size; // size of the constructor ( 4 for vec4) |
| 47 | bool isMatrix; |
| 48 | int matrixSize; // dimension of the matrix (nominal size and not the instance size) |
| 49 | }; |
| 50 | |
| 51 | // |
| 52 | // The rest of the file are the traversal functions. The last one |
| 53 | // is the one that starts the traversal. |
| 54 | // |
| 55 | // Return true from interior nodes to have the external traversal |
| 56 | // continue on to children. If you process children yourself, |
| 57 | // return false. |
| 58 | // |
| 59 | |
| 60 | void TConstTraverser::visitSymbol(TIntermSymbol* node) |
| 61 | { |
| 62 | infoSink.info.message(EPrefixInternalError, "Symbol Node found in constant constructor", node->getLine()); |
| 63 | return; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | bool TConstTraverser::visitBinary(Visit visit, TIntermBinary* node) |
| 67 | { |
| 68 | TQualifier qualifier = node->getType().getQualifier(); |
Nicolas Capens | c3bfb40 | 2014-06-12 11:45:17 -0400 | [diff] [blame] | 69 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 70 | if (qualifier != EvqConst) { |
| 71 | TString buf; |
| 72 | buf.append("'constructor' : assigning non-constant to "); |
| 73 | buf.append(type.getCompleteString()); |
| 74 | infoSink.info.message(EPrefixError, buf.c_str(), node->getLine()); |
| 75 | error = true; |
Nicolas Capens | c3bfb40 | 2014-06-12 11:45:17 -0400 | [diff] [blame] | 76 | return false; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 77 | } |
| 78 | |
Nicolas Capens | c3bfb40 | 2014-06-12 11:45:17 -0400 | [diff] [blame] | 79 | infoSink.info.message(EPrefixInternalError, "Binary Node found in constant constructor", node->getLine()); |
| 80 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 81 | return false; |
| 82 | } |
| 83 | |
| 84 | bool TConstTraverser::visitUnary(Visit visit, TIntermUnary* node) |
| 85 | { |
| 86 | TString buf; |
| 87 | buf.append("'constructor' : assigning non-constant to "); |
| 88 | buf.append(type.getCompleteString()); |
| 89 | infoSink.info.message(EPrefixError, buf.c_str(), node->getLine()); |
| 90 | error = true; |
Nicolas Capens | c3bfb40 | 2014-06-12 11:45:17 -0400 | [diff] [blame] | 91 | return false; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | bool TConstTraverser::visitAggregate(Visit visit, TIntermAggregate* node) |
| 95 | { |
| 96 | if (!node->isConstructor() && node->getOp() != EOpComma) { |
| 97 | TString buf; |
| 98 | buf.append("'constructor' : assigning non-constant to "); |
| 99 | buf.append(type.getCompleteString()); |
| 100 | infoSink.info.message(EPrefixError, buf.c_str(), node->getLine()); |
| 101 | error = true; |
Nicolas Capens | c3bfb40 | 2014-06-12 11:45:17 -0400 | [diff] [blame] | 102 | return false; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | if (node->getSequence().size() == 0) { |
| 106 | error = true; |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | bool flag = node->getSequence().size() == 1 && node->getSequence()[0]->getAsTyped()->getAsConstantUnion(); |
Nicolas Capens | c3bfb40 | 2014-06-12 11:45:17 -0400 | [diff] [blame] | 111 | if (flag) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 112 | { |
Nicolas Capens | c3bfb40 | 2014-06-12 11:45:17 -0400 | [diff] [blame] | 113 | singleConstantParam = true; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 114 | constructorType = node->getOp(); |
| 115 | size = node->getType().getObjectSize(); |
| 116 | |
| 117 | if (node->getType().isMatrix()) { |
| 118 | isMatrix = true; |
| 119 | matrixSize = node->getType().getNominalSize(); |
| 120 | } |
Nicolas Capens | c3bfb40 | 2014-06-12 11:45:17 -0400 | [diff] [blame] | 121 | } |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 122 | |
Nicolas Capens | c3bfb40 | 2014-06-12 11:45:17 -0400 | [diff] [blame] | 123 | for (TIntermSequence::iterator p = node->getSequence().begin(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 124 | p != node->getSequence().end(); p++) { |
| 125 | |
| 126 | if (node->getOp() == EOpComma) |
Nicolas Capens | c3bfb40 | 2014-06-12 11:45:17 -0400 | [diff] [blame] | 127 | index = 0; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 128 | |
| 129 | (*p)->traverse(this); |
Nicolas Capens | c3bfb40 | 2014-06-12 11:45:17 -0400 | [diff] [blame] | 130 | } |
| 131 | if (flag) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 132 | { |
Nicolas Capens | c3bfb40 | 2014-06-12 11:45:17 -0400 | [diff] [blame] | 133 | singleConstantParam = false; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 134 | constructorType = EOpNull; |
| 135 | size = 0; |
| 136 | isMatrix = false; |
| 137 | matrixSize = 0; |
| 138 | } |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | bool TConstTraverser::visitSelection(Visit visit, TIntermSelection* node) |
| 143 | { |
| 144 | infoSink.info.message(EPrefixInternalError, "Selection Node found in constant constructor", node->getLine()); |
| 145 | error = true; |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | void TConstTraverser::visitConstantUnion(TIntermConstantUnion* node) |
| 150 | { |
| 151 | ConstantUnion* leftUnionArray = unionArray; |
| 152 | int instanceSize = type.getObjectSize(); |
Nicolas Capens | c3bfb40 | 2014-06-12 11:45:17 -0400 | [diff] [blame] | 153 | TBasicType basicType = type.getBasicType(); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 154 | |
| 155 | if (index >= instanceSize) |
| 156 | return; |
| 157 | |
| 158 | if (!singleConstantParam) { |
| 159 | int size = node->getType().getObjectSize(); |
Nicolas Capens | c3bfb40 | 2014-06-12 11:45:17 -0400 | [diff] [blame] | 160 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 161 | ConstantUnion *rightUnionArray = node->getUnionArrayPointer(); |
| 162 | for (int i=0; i < size; i++) { |
| 163 | if (index >= instanceSize) |
| 164 | return; |
Nicolas Capens | c3bfb40 | 2014-06-12 11:45:17 -0400 | [diff] [blame] | 165 | leftUnionArray[index].cast(basicType, rightUnionArray[i]); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 166 | |
| 167 | (index)++; |
| 168 | } |
| 169 | } else { |
| 170 | int totalSize = index + size; |
| 171 | ConstantUnion *rightUnionArray = node->getUnionArrayPointer(); |
| 172 | if (!isMatrix) { |
| 173 | int count = 0; |
| 174 | for (int i = index; i < totalSize; i++) { |
| 175 | if (i >= instanceSize) |
| 176 | return; |
| 177 | |
Nicolas Capens | c3bfb40 | 2014-06-12 11:45:17 -0400 | [diff] [blame] | 178 | leftUnionArray[i].cast(basicType, rightUnionArray[count]); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 179 | |
| 180 | (index)++; |
Nicolas Capens | c3bfb40 | 2014-06-12 11:45:17 -0400 | [diff] [blame] | 181 | |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 182 | if (node->getType().getObjectSize() > 1) |
| 183 | count++; |
| 184 | } |
| 185 | } else { // for matrix constructors |
| 186 | int count = 0; |
| 187 | int element = index; |
| 188 | for (int i = index; i < totalSize; i++) { |
| 189 | if (i >= instanceSize) |
| 190 | return; |
| 191 | if (element - i == 0 || (i - element) % (matrixSize + 1) == 0 ) |
Nicolas Capens | c3bfb40 | 2014-06-12 11:45:17 -0400 | [diff] [blame] | 192 | leftUnionArray[i].cast(basicType, rightUnionArray[0]); |
| 193 | else |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 194 | leftUnionArray[i].setFConst(0.0f); |
| 195 | |
| 196 | (index)++; |
| 197 | |
| 198 | if (node->getType().getObjectSize() > 1) |
Nicolas Capens | c3bfb40 | 2014-06-12 11:45:17 -0400 | [diff] [blame] | 199 | count++; |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | bool TConstTraverser::visitLoop(Visit visit, TIntermLoop* node) |
| 206 | { |
| 207 | infoSink.info.message(EPrefixInternalError, "Loop Node found in constant constructor", node->getLine()); |
| 208 | error = true; |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | bool TConstTraverser::visitBranch(Visit visit, TIntermBranch* node) |
| 213 | { |
| 214 | infoSink.info.message(EPrefixInternalError, "Branch Node found in constant constructor", node->getLine()); |
| 215 | error = true; |
| 216 | return false; |
| 217 | } |
| 218 | |
| 219 | // |
| 220 | // This function is the one to call externally to start the traversal. |
| 221 | // Individual functions can be initialized to 0 to skip processing of that |
| 222 | // type of node. It's children will still be processed. |
| 223 | // |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 224 | bool TIntermediate::parseConstTree(TSourceLoc line, TIntermNode* root, ConstantUnion* unionArray, TOperator constructorType, TType t, bool singleConstantParam) |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 225 | { |
| 226 | if (root == 0) |
| 227 | return false; |
| 228 | |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 229 | TConstTraverser it(unionArray, singleConstantParam, constructorType, infoSink, t); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 230 | |
| 231 | root->traverse(&it); |
| 232 | if (it.error) |
| 233 | return true; |
| 234 | else |
| 235 | return false; |
| 236 | } |