daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
shannonwoods@chromium.org | 298f907 | 2013-05-30 00:21:17 +0000 | [diff] [blame] | 2 | // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [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 | |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 7 | #include "compiler/ParseHelper.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [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: |
shannonwoods@chromium.org | 298f907 | 2013-05-30 00:21:17 +0000 | [diff] [blame] | 15 | TConstTraverser(ConstantUnion* cUnion, bool singleConstParam, TOperator constructType, TInfoSink& sink, TType& t) |
alokp@chromium.org | b2dfd8e | 2010-08-09 22:28:19 +0000 | [diff] [blame] | 16 | : error(false), |
| 17 | index(0), |
| 18 | unionArray(cUnion), |
| 19 | type(t), |
| 20 | constructorType(constructType), |
| 21 | singleConstantParam(singleConstParam), |
| 22 | infoSink(sink), |
alokp@chromium.org | b2dfd8e | 2010-08-09 22:28:19 +0000 | [diff] [blame] | 23 | size(0), |
shannonwoods@chromium.org | 9bd22fa | 2013-05-30 00:18:47 +0000 | [diff] [blame] | 24 | isDiagonalMatrixInit(false), |
| 25 | matrixCols(0), |
| 26 | matrixRows(0) { |
alokp@chromium.org | b2dfd8e | 2010-08-09 22:28:19 +0000 | [diff] [blame] | 27 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 28 | |
alokp@chromium.org | b2dfd8e | 2010-08-09 22:28:19 +0000 | [diff] [blame] | 29 | bool error; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 30 | |
| 31 | protected: |
alokp@chromium.org | b2dfd8e | 2010-08-09 22:28:19 +0000 | [diff] [blame] | 32 | void visitSymbol(TIntermSymbol*); |
| 33 | void visitConstantUnion(TIntermConstantUnion*); |
| 34 | bool visitBinary(Visit visit, TIntermBinary*); |
| 35 | bool visitUnary(Visit visit, TIntermUnary*); |
| 36 | bool visitSelection(Visit visit, TIntermSelection*); |
| 37 | bool visitAggregate(Visit visit, TIntermAggregate*); |
| 38 | bool visitLoop(Visit visit, TIntermLoop*); |
| 39 | bool visitBranch(Visit visit, TIntermBranch*); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 40 | |
Jamie Madill | 94bf7f2 | 2013-07-08 13:31:15 -0400 | [diff] [blame^] | 41 | size_t index; |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 42 | ConstantUnion *unionArray; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 43 | TType type; |
| 44 | TOperator constructorType; |
| 45 | bool singleConstantParam; |
| 46 | TInfoSink& infoSink; |
Jamie Madill | 94bf7f2 | 2013-07-08 13:31:15 -0400 | [diff] [blame^] | 47 | size_t size; // size of the constructor ( 4 for vec4) |
shannonwoods@chromium.org | 9bd22fa | 2013-05-30 00:18:47 +0000 | [diff] [blame] | 48 | bool isDiagonalMatrixInit; |
| 49 | int matrixCols; // columns of the matrix |
| 50 | int matrixRows; // rows of the matrix |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | // |
| 54 | // The rest of the file are the traversal functions. The last one |
| 55 | // is the one that starts the traversal. |
| 56 | // |
| 57 | // Return true from interior nodes to have the external traversal |
| 58 | // continue on to children. If you process children yourself, |
| 59 | // return false. |
| 60 | // |
| 61 | |
| 62 | void TConstTraverser::visitSymbol(TIntermSymbol* node) |
| 63 | { |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 64 | infoSink.info.message(EPrefixInternalError, node->getLine(), "Symbol Node found in constant constructor"); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 65 | return; |
| 66 | |
| 67 | } |
| 68 | |
| 69 | bool TConstTraverser::visitBinary(Visit visit, TIntermBinary* node) |
| 70 | { |
| 71 | TQualifier qualifier = node->getType().getQualifier(); |
| 72 | |
| 73 | if (qualifier != EvqConst) { |
alokp@chromium.org | 4e4facd | 2010-06-02 15:21:22 +0000 | [diff] [blame] | 74 | TString buf; |
| 75 | buf.append("'constructor' : assigning non-constant to "); |
| 76 | buf.append(type.getCompleteString()); |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 77 | infoSink.info.message(EPrefixError, node->getLine(), buf.c_str()); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 78 | error = true; |
| 79 | return false; |
| 80 | } |
| 81 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 82 | infoSink.info.message(EPrefixInternalError, node->getLine(), "Binary Node found in constant constructor"); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 83 | |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | bool TConstTraverser::visitUnary(Visit visit, TIntermUnary* node) |
| 88 | { |
alokp@chromium.org | 4e4facd | 2010-06-02 15:21:22 +0000 | [diff] [blame] | 89 | TString buf; |
| 90 | buf.append("'constructor' : assigning non-constant to "); |
| 91 | buf.append(type.getCompleteString()); |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 92 | infoSink.info.message(EPrefixError, node->getLine(), buf.c_str()); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 93 | error = true; |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | bool TConstTraverser::visitAggregate(Visit visit, TIntermAggregate* node) |
| 98 | { |
| 99 | if (!node->isConstructor() && node->getOp() != EOpComma) { |
alokp@chromium.org | 4e4facd | 2010-06-02 15:21:22 +0000 | [diff] [blame] | 100 | TString buf; |
| 101 | buf.append("'constructor' : assigning non-constant to "); |
| 102 | buf.append(type.getCompleteString()); |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 103 | infoSink.info.message(EPrefixError, node->getLine(), buf.c_str()); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 104 | error = true; |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | if (node->getSequence().size() == 0) { |
| 109 | error = true; |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | bool flag = node->getSequence().size() == 1 && node->getSequence()[0]->getAsTyped()->getAsConstantUnion(); |
| 114 | if (flag) |
| 115 | { |
| 116 | singleConstantParam = true; |
| 117 | constructorType = node->getOp(); |
| 118 | size = node->getType().getObjectSize(); |
| 119 | |
| 120 | if (node->getType().isMatrix()) { |
shannonwoods@chromium.org | 9bd22fa | 2013-05-30 00:18:47 +0000 | [diff] [blame] | 121 | isDiagonalMatrixInit = true; |
| 122 | matrixCols = node->getType().getCols(); |
| 123 | matrixRows = node->getType().getRows(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| 127 | for (TIntermSequence::iterator p = node->getSequence().begin(); |
| 128 | p != node->getSequence().end(); p++) { |
| 129 | |
| 130 | if (node->getOp() == EOpComma) |
| 131 | index = 0; |
| 132 | |
| 133 | (*p)->traverse(this); |
| 134 | } |
| 135 | if (flag) |
| 136 | { |
| 137 | singleConstantParam = false; |
| 138 | constructorType = EOpNull; |
| 139 | size = 0; |
shannonwoods@chromium.org | 9bd22fa | 2013-05-30 00:18:47 +0000 | [diff] [blame] | 140 | isDiagonalMatrixInit = false; |
| 141 | matrixCols = 0; |
| 142 | matrixRows = 0; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 143 | } |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | bool TConstTraverser::visitSelection(Visit visit, TIntermSelection* node) |
| 148 | { |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 149 | infoSink.info.message(EPrefixInternalError, node->getLine(), "Selection Node found in constant constructor"); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 150 | error = true; |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | void TConstTraverser::visitConstantUnion(TIntermConstantUnion* node) |
| 155 | { |
shannon.woods%transgaming.com@gtempaccount.com | 6df256c | 2013-04-13 03:29:46 +0000 | [diff] [blame] | 156 | if (!node->getUnionArrayPointer()) |
| 157 | { |
| 158 | // The constant was not initialized, this should already have been logged |
| 159 | assert(infoSink.info.size() != 0); |
| 160 | return; |
| 161 | } |
| 162 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 163 | ConstantUnion* leftUnionArray = unionArray; |
Jamie Madill | 94bf7f2 | 2013-07-08 13:31:15 -0400 | [diff] [blame^] | 164 | size_t instanceSize = type.getObjectSize(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 165 | |
| 166 | if (index >= instanceSize) |
| 167 | return; |
| 168 | |
| 169 | if (!singleConstantParam) { |
Jamie Madill | 94bf7f2 | 2013-07-08 13:31:15 -0400 | [diff] [blame^] | 170 | size_t objectSize = node->getType().getObjectSize(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 171 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 172 | ConstantUnion *rightUnionArray = node->getUnionArrayPointer(); |
Jamie Madill | 94bf7f2 | 2013-07-08 13:31:15 -0400 | [diff] [blame^] | 173 | for (size_t i=0; i < objectSize; i++) { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 174 | if (index >= instanceSize) |
| 175 | return; |
| 176 | leftUnionArray[index] = rightUnionArray[i]; |
| 177 | |
| 178 | (index)++; |
| 179 | } |
| 180 | } else { |
Jamie Madill | 94bf7f2 | 2013-07-08 13:31:15 -0400 | [diff] [blame^] | 181 | size_t totalSize = index + size; |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 182 | ConstantUnion *rightUnionArray = node->getUnionArrayPointer(); |
shannonwoods@chromium.org | 9bd22fa | 2013-05-30 00:18:47 +0000 | [diff] [blame] | 183 | if (!isDiagonalMatrixInit) { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 184 | int count = 0; |
Jamie Madill | 94bf7f2 | 2013-07-08 13:31:15 -0400 | [diff] [blame^] | 185 | for (size_t i = index; i < totalSize; i++) { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 186 | if (i >= instanceSize) |
| 187 | return; |
| 188 | |
| 189 | leftUnionArray[i] = rightUnionArray[count]; |
| 190 | |
| 191 | (index)++; |
| 192 | |
| 193 | if (node->getType().getObjectSize() > 1) |
| 194 | count++; |
| 195 | } |
shannonwoods@chromium.org | 9bd22fa | 2013-05-30 00:18:47 +0000 | [diff] [blame] | 196 | } |
| 197 | else |
| 198 | { |
| 199 | // for matrix diagonal constructors from a single scalar |
| 200 | for (int i = 0, col = 0; col < matrixCols; col++) |
| 201 | { |
| 202 | for (int row = 0; row < matrixRows; row++, i++) |
| 203 | { |
| 204 | if (col == row) |
| 205 | { |
| 206 | leftUnionArray[i] = rightUnionArray[0]; |
| 207 | } |
| 208 | else |
| 209 | { |
| 210 | leftUnionArray[i].setFConst(0.0f); |
| 211 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 212 | |
shannonwoods@chromium.org | 9bd22fa | 2013-05-30 00:18:47 +0000 | [diff] [blame] | 213 | (index)++; |
| 214 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | bool TConstTraverser::visitLoop(Visit visit, TIntermLoop* node) |
| 221 | { |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 222 | infoSink.info.message(EPrefixInternalError, node->getLine(), "Loop Node found in constant constructor"); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 223 | error = true; |
| 224 | return false; |
| 225 | } |
| 226 | |
| 227 | bool TConstTraverser::visitBranch(Visit visit, TIntermBranch* node) |
| 228 | { |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 229 | infoSink.info.message(EPrefixInternalError, node->getLine(), "Branch Node found in constant constructor"); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 230 | error = true; |
| 231 | return false; |
| 232 | } |
| 233 | |
| 234 | // |
| 235 | // This function is the one to call externally to start the traversal. |
| 236 | // Individual functions can be initialized to 0 to skip processing of that |
| 237 | // type of node. It's children will still be processed. |
| 238 | // |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 239 | bool TIntermediate::parseConstTree(const TSourceLoc& line, TIntermNode* root, ConstantUnion* unionArray, TOperator constructorType, TType t, bool singleConstantParam) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 240 | { |
| 241 | if (root == 0) |
| 242 | return false; |
| 243 | |
shannonwoods@chromium.org | 298f907 | 2013-05-30 00:21:17 +0000 | [diff] [blame] | 244 | TConstTraverser it(unionArray, singleConstantParam, constructorType, infoSink, t); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 245 | |
| 246 | root->traverse(&it); |
| 247 | if (it.error) |
| 248 | return true; |
| 249 | else |
| 250 | return false; |
| 251 | } |