blob: 0ebab3aa60f3101284cb57614663040394f9cc91 [file] [log] [blame]
John Bauman66b8ab22014-05-06 15:57:45 -04001//
John Baumand4ae8632014-05-06 16:18:33 -04002// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
John Bauman66b8ab22014-05-06 15:57:45 -04003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7//
8// Build the intermediate representation.
9//
10
11#include <float.h>
12#include <limits.h>
13#include <algorithm>
14
Nicolas Capenscc863da2015-01-21 15:50:55 -050015#include "localintermediate.h"
16#include "SymbolTable.h"
John Bauman66b8ab22014-05-06 15:57:45 -040017
18bool CompareStructure(const TType& leftNodeType, ConstantUnion* rightUnionArray, ConstantUnion* leftUnionArray);
19
20static TPrecision GetHigherPrecision( TPrecision left, TPrecision right ){
21 return left > right ? left : right;
22}
23
24const char* getOperatorString(TOperator op) {
25 switch (op) {
26 case EOpInitialize: return "=";
27 case EOpAssign: return "=";
28 case EOpAddAssign: return "+=";
29 case EOpSubAssign: return "-=";
30 case EOpDivAssign: return "/=";
31
32 // Fall-through.
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -040033 case EOpMulAssign:
John Bauman66b8ab22014-05-06 15:57:45 -040034 case EOpVectorTimesMatrixAssign:
35 case EOpVectorTimesScalarAssign:
36 case EOpMatrixTimesScalarAssign:
37 case EOpMatrixTimesMatrixAssign: return "*=";
38
39 // Fall-through.
40 case EOpIndexDirect:
41 case EOpIndexIndirect: return "[]";
42
43 case EOpIndexDirectStruct: return ".";
44 case EOpVectorSwizzle: return ".";
45 case EOpAdd: return "+";
46 case EOpSub: return "-";
47 case EOpMul: return "*";
48 case EOpDiv: return "/";
49 case EOpMod: UNIMPLEMENTED(); break;
50 case EOpEqual: return "==";
51 case EOpNotEqual: return "!=";
52 case EOpLessThan: return "<";
53 case EOpGreaterThan: return ">";
54 case EOpLessThanEqual: return "<=";
55 case EOpGreaterThanEqual: return ">=";
56
57 // Fall-through.
58 case EOpVectorTimesScalar:
59 case EOpVectorTimesMatrix:
60 case EOpMatrixTimesVector:
61 case EOpMatrixTimesScalar:
62 case EOpMatrixTimesMatrix: return "*";
63
64 case EOpLogicalOr: return "||";
65 case EOpLogicalXor: return "^^";
66 case EOpLogicalAnd: return "&&";
67 case EOpNegative: return "-";
68 case EOpVectorLogicalNot: return "not";
69 case EOpLogicalNot: return "!";
70 case EOpPostIncrement: return "++";
71 case EOpPostDecrement: return "--";
72 case EOpPreIncrement: return "++";
73 case EOpPreDecrement: return "--";
74
John Bauman66b8ab22014-05-06 15:57:45 -040075 case EOpRadians: return "radians";
76 case EOpDegrees: return "degrees";
77 case EOpSin: return "sin";
78 case EOpCos: return "cos";
79 case EOpTan: return "tan";
80 case EOpAsin: return "asin";
81 case EOpAcos: return "acos";
82 case EOpAtan: return "atan";
Alexis Hetuaf1970c2015-04-17 14:26:07 -040083 case EOpSinh: return "sinh";
84 case EOpCosh: return "cosh";
85 case EOpTanh: return "tanh";
86 case EOpAsinh: return "asinh";
87 case EOpAcosh: return "acosh";
88 case EOpAtanh: return "atanh";
John Bauman66b8ab22014-05-06 15:57:45 -040089 case EOpExp: return "exp";
90 case EOpLog: return "log";
91 case EOpExp2: return "exp2";
92 case EOpLog2: return "log2";
93 case EOpSqrt: return "sqrt";
94 case EOpInverseSqrt: return "inversesqrt";
95 case EOpAbs: return "abs";
96 case EOpSign: return "sign";
97 case EOpFloor: return "floor";
Alexis Hetuaf1970c2015-04-17 14:26:07 -040098 case EOpTrunc: return "trunc";
99 case EOpRound: return "round";
100 case EOpRoundEven: return "roundEven";
John Bauman66b8ab22014-05-06 15:57:45 -0400101 case EOpCeil: return "ceil";
102 case EOpFract: return "fract";
103 case EOpLength: return "length";
104 case EOpNormalize: return "normalize";
105 case EOpDFdx: return "dFdx";
106 case EOpDFdy: return "dFdy";
107 case EOpFwidth: return "fwidth";
108 case EOpAny: return "any";
109 case EOpAll: return "all";
Alexis Hetuaf1970c2015-04-17 14:26:07 -0400110 case EOpIsNan: return "isnan";
111 case EOpIsInf: return "isinf";
John Bauman66b8ab22014-05-06 15:57:45 -0400112
113 default: break;
114 }
115 return "";
116}
117
118////////////////////////////////////////////////////////////////////////////
119//
120// First set of functions are to help build the intermediate representation.
121// These functions are not member functions of the nodes.
122// They are called from parser productions.
123//
124/////////////////////////////////////////////////////////////////////////////
125
126//
127// Add a terminal node for an identifier in an expression.
128//
129// Returns the added node.
130//
131TIntermSymbol* TIntermediate::addSymbol(int id, const TString& name, const TType& type, TSourceLoc line)
132{
133 TIntermSymbol* node = new TIntermSymbol(id, name, type);
134 node->setLine(line);
135
136 return node;
137}
138
139//
140// Connect two nodes with a new parent that does a binary operation on the nodes.
141//
142// Returns the added node.
143//
John Baumand4ae8632014-05-06 16:18:33 -0400144TIntermTyped* TIntermediate::addBinaryMath(TOperator op, TIntermTyped* left, TIntermTyped* right, TSourceLoc line)
John Bauman66b8ab22014-05-06 15:57:45 -0400145{
146 switch (op) {
147 case EOpEqual:
148 case EOpNotEqual:
149 if (left->isArray())
150 return 0;
151 break;
152 case EOpLessThan:
153 case EOpGreaterThan:
154 case EOpLessThanEqual:
155 case EOpGreaterThanEqual:
156 if (left->isMatrix() || left->isArray() || left->isVector() || left->getBasicType() == EbtStruct) {
157 return 0;
158 }
159 break;
160 case EOpLogicalOr:
161 case EOpLogicalXor:
162 case EOpLogicalAnd:
163 if (left->getBasicType() != EbtBool || left->isMatrix() || left->isArray() || left->isVector()) {
164 return 0;
165 }
166 break;
167 case EOpAdd:
168 case EOpSub:
169 case EOpDiv:
170 case EOpMul:
171 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
172 return 0;
173 default: break;
174 }
175
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400176 if (left->getBasicType() != right->getBasicType())
177 {
178 return 0;
John Bauman66b8ab22014-05-06 15:57:45 -0400179 }
180
181 //
182 // Need a new node holding things together then. Make
183 // one and promote it to the right type.
184 //
185 TIntermBinary* node = new TIntermBinary(op);
186 if (line == 0)
187 line = right->getLine();
188 node->setLine(line);
189
190 node->setLeft(left);
191 node->setRight(right);
192 if (!node->promote(infoSink))
193 return 0;
194
195 //
196 // See if we can fold constants.
197 //
John Bauman66b8ab22014-05-06 15:57:45 -0400198 TIntermConstantUnion *leftTempConstant = left->getAsConstantUnion();
199 TIntermConstantUnion *rightTempConstant = right->getAsConstantUnion();
200 if (leftTempConstant && rightTempConstant) {
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400201 TIntermTyped *typedReturnNode = leftTempConstant->fold(node->getOp(), rightTempConstant, infoSink);
John Bauman66b8ab22014-05-06 15:57:45 -0400202
203 if (typedReturnNode)
204 return typedReturnNode;
205 }
206
207 return node;
208}
209
210//
211// Connect two nodes through an assignment.
212//
213// Returns the added node.
214//
215TIntermTyped* TIntermediate::addAssign(TOperator op, TIntermTyped* left, TIntermTyped* right, TSourceLoc line)
216{
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400217 if (left->getType().getStruct() || right->getType().getStruct())
218 {
219 if (left->getType() != right->getType())
220 {
221 return 0;
222 }
223 }
224
John Bauman66b8ab22014-05-06 15:57:45 -0400225 TIntermBinary* node = new TIntermBinary(op);
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400226 if(line == 0)
John Bauman66b8ab22014-05-06 15:57:45 -0400227 line = left->getLine();
228 node->setLine(line);
229
John Bauman66b8ab22014-05-06 15:57:45 -0400230 node->setLeft(left);
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400231 node->setRight(right);
John Bauman66b8ab22014-05-06 15:57:45 -0400232 if (! node->promote(infoSink))
233 return 0;
234
235 return node;
236}
237
238//
239// Connect two nodes through an index operator, where the left node is the base
240// of an array or struct, and the right node is a direct or indirect offset.
241//
242// Returns the added node.
243// The caller should set the type of the returned node.
244//
245TIntermTyped* TIntermediate::addIndex(TOperator op, TIntermTyped* base, TIntermTyped* index, TSourceLoc line)
246{
247 TIntermBinary* node = new TIntermBinary(op);
248 if (line == 0)
249 line = index->getLine();
250 node->setLine(line);
251 node->setLeft(base);
252 node->setRight(index);
253
254 // caller should set the type
255
256 return node;
257}
258
259//
260// Add one node as the parent of another that it operates on.
261//
262// Returns the added node.
263//
John Baumand4ae8632014-05-06 16:18:33 -0400264TIntermTyped* TIntermediate::addUnaryMath(TOperator op, TIntermNode* childNode, TSourceLoc line)
John Bauman66b8ab22014-05-06 15:57:45 -0400265{
266 TIntermUnary* node;
267 TIntermTyped* child = childNode->getAsTyped();
268
269 if (child == 0) {
270 infoSink.info.message(EPrefixInternalError, "Bad type in AddUnaryMath", line);
271 return 0;
272 }
273
274 switch (op) {
275 case EOpLogicalNot:
276 if (child->getType().getBasicType() != EbtBool || child->getType().isMatrix() || child->getType().isArray() || child->getType().isVector()) {
277 return 0;
278 }
279 break;
280
281 case EOpPostIncrement:
282 case EOpPreIncrement:
283 case EOpPostDecrement:
284 case EOpPreDecrement:
285 case EOpNegative:
286 if (child->getType().getBasicType() == EbtStruct || child->getType().isArray())
287 return 0;
288 default: break;
289 }
290
John Bauman66b8ab22014-05-06 15:57:45 -0400291 TIntermConstantUnion *childTempConstant = 0;
292 if (child->getAsConstantUnion())
293 childTempConstant = child->getAsConstantUnion();
294
295 //
296 // Make a new node for the operator.
297 //
298 node = new TIntermUnary(op);
299 if (line == 0)
300 line = child->getLine();
301 node->setLine(line);
302 node->setOperand(child);
303
304 if (! node->promote(infoSink))
305 return 0;
306
307 if (childTempConstant) {
308 TIntermTyped* newChild = childTempConstant->fold(op, 0, infoSink);
309
310 if (newChild)
311 return newChild;
312 }
313
314 return node;
315}
316
317//
318// This is the safe way to change the operator on an aggregate, as it
319// does lots of error checking and fixing. Especially for establishing
320// a function call's operation on it's set of parameters. Sequences
321// of instructions are also aggregates, but they just direnctly set
322// their operator to EOpSequence.
323//
324// Returns an aggregate node, which could be the one passed in if
325// it was already an aggregate but no operator was set.
326//
327TIntermAggregate* TIntermediate::setAggregateOperator(TIntermNode* node, TOperator op, TSourceLoc line)
328{
329 TIntermAggregate* aggNode;
330
331 //
332 // Make sure we have an aggregate. If not turn it into one.
333 //
334 if (node) {
335 aggNode = node->getAsAggregate();
336 if (aggNode == 0 || aggNode->getOp() != EOpNull) {
337 //
338 // Make an aggregate containing this node.
339 //
340 aggNode = new TIntermAggregate();
341 aggNode->getSequence().push_back(node);
342 if (line == 0)
343 line = node->getLine();
344 }
345 } else
346 aggNode = new TIntermAggregate();
347
348 //
349 // Set the operator.
350 //
351 aggNode->setOp(op);
352 if (line != 0)
353 aggNode->setLine(line);
354
355 return aggNode;
356}
357
358//
John Bauman66b8ab22014-05-06 15:57:45 -0400359// Safe way to combine two nodes into an aggregate. Works with null pointers,
360// a node that's not a aggregate yet, etc.
361//
362// Returns the resulting aggregate, unless 0 was passed in for
363// both existing nodes.
364//
365TIntermAggregate* TIntermediate::growAggregate(TIntermNode* left, TIntermNode* right, TSourceLoc line)
366{
367 if (left == 0 && right == 0)
368 return 0;
369
370 TIntermAggregate* aggNode = 0;
371 if (left)
372 aggNode = left->getAsAggregate();
373 if (!aggNode || aggNode->getOp() != EOpNull) {
374 aggNode = new TIntermAggregate;
375 if (left)
376 aggNode->getSequence().push_back(left);
377 }
378
379 if (right)
380 aggNode->getSequence().push_back(right);
381
382 if (line != 0)
383 aggNode->setLine(line);
384
385 return aggNode;
386}
387
388//
389// Turn an existing node into an aggregate.
390//
391// Returns an aggregate, unless 0 was passed in for the existing node.
392//
393TIntermAggregate* TIntermediate::makeAggregate(TIntermNode* node, TSourceLoc line)
394{
395 if (node == 0)
396 return 0;
397
398 TIntermAggregate* aggNode = new TIntermAggregate;
399 aggNode->getSequence().push_back(node);
400
401 if (line != 0)
402 aggNode->setLine(line);
403 else
404 aggNode->setLine(node->getLine());
405
406 return aggNode;
407}
408
409//
410// For "if" test nodes. There are three children; a condition,
411// a true path, and a false path. The two paths are in the
412// nodePair.
413//
414// Returns the selection node created.
415//
416TIntermNode* TIntermediate::addSelection(TIntermTyped* cond, TIntermNodePair nodePair, TSourceLoc line)
417{
418 //
419 // For compile time constant selections, prune the code and
420 // test now.
421 //
422
423 if (cond->getAsTyped() && cond->getAsTyped()->getAsConstantUnion()) {
Nicolas Capens198529d2015-02-10 13:54:19 -0500424 if (cond->getAsConstantUnion()->getBConst(0) == true)
John Bauman66b8ab22014-05-06 15:57:45 -0400425 return nodePair.node1 ? setAggregateOperator(nodePair.node1, EOpSequence, nodePair.node1->getLine()) : NULL;
426 else
427 return nodePair.node2 ? setAggregateOperator(nodePair.node2, EOpSequence, nodePair.node2->getLine()) : NULL;
428 }
429
430 TIntermSelection* node = new TIntermSelection(cond, nodePair.node1, nodePair.node2);
431 node->setLine(line);
432
433 return node;
434}
435
436
437TIntermTyped* TIntermediate::addComma(TIntermTyped* left, TIntermTyped* right, TSourceLoc line)
438{
Nicolas Capens31ad2aa2015-02-26 13:14:27 -0500439 if (left->getType().getQualifier() == EvqConstExpr && right->getType().getQualifier() == EvqConstExpr) {
John Bauman66b8ab22014-05-06 15:57:45 -0400440 return right;
441 } else {
442 TIntermTyped *commaAggregate = growAggregate(left, right, line);
443 commaAggregate->getAsAggregate()->setOp(EOpComma);
444 commaAggregate->setType(right->getType());
445 commaAggregate->getTypePointer()->setQualifier(EvqTemporary);
446 return commaAggregate;
447 }
448}
449
450//
451// For "?:" test nodes. There are three children; a condition,
452// a true path, and a false path. The two paths are specified
453// as separate parameters.
454//
455// Returns the selection node created, or 0 if one could not be.
456//
457TIntermTyped* TIntermediate::addSelection(TIntermTyped* cond, TIntermTyped* trueBlock, TIntermTyped* falseBlock, TSourceLoc line)
458{
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400459 if (trueBlock->getType() != falseBlock->getType())
460 {
461 return 0;
John Bauman66b8ab22014-05-06 15:57:45 -0400462 }
463
464 //
465 // See if all the operands are constant, then fold it otherwise not.
466 //
467
468 if (cond->getAsConstantUnion() && trueBlock->getAsConstantUnion() && falseBlock->getAsConstantUnion()) {
Nicolas Capens198529d2015-02-10 13:54:19 -0500469 if (cond->getAsConstantUnion()->getBConst(0))
John Bauman66b8ab22014-05-06 15:57:45 -0400470 return trueBlock;
471 else
472 return falseBlock;
473 }
474
475 //
476 // Make a selection node.
477 //
478 TIntermSelection* node = new TIntermSelection(cond, trueBlock, falseBlock, trueBlock->getType());
479 node->getTypePointer()->setQualifier(EvqTemporary);
480 node->setLine(line);
481
482 return node;
483}
484
485//
486// Constant terminal nodes. Has a union that contains bool, float or int constants
487//
488// Returns the constant union node created.
489//
490
491TIntermConstantUnion* TIntermediate::addConstantUnion(ConstantUnion* unionArrayPointer, const TType& t, TSourceLoc line)
492{
493 TIntermConstantUnion* node = new TIntermConstantUnion(unionArrayPointer, t);
494 node->setLine(line);
495
496 return node;
497}
498
499TIntermTyped* TIntermediate::addSwizzle(TVectorFields& fields, TSourceLoc line)
500{
501
502 TIntermAggregate* node = new TIntermAggregate(EOpSequence);
503
504 node->setLine(line);
505 TIntermConstantUnion* constIntNode;
506 TIntermSequence &sequenceVector = node->getSequence();
507 ConstantUnion* unionArray;
508
509 for (int i = 0; i < fields.num; i++) {
510 unionArray = new ConstantUnion[1];
511 unionArray->setIConst(fields.offsets[i]);
Nicolas Capens31ad2aa2015-02-26 13:14:27 -0500512 constIntNode = addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConstExpr), line);
John Bauman66b8ab22014-05-06 15:57:45 -0400513 sequenceVector.push_back(constIntNode);
514 }
515
516 return node;
517}
518
519//
520// Create loop nodes.
521//
522TIntermNode* TIntermediate::addLoop(TLoopType type, TIntermNode* init, TIntermTyped* cond, TIntermTyped* expr, TIntermNode* body, TSourceLoc line)
523{
524 TIntermNode* node = new TIntermLoop(type, init, cond, expr, body);
525 node->setLine(line);
526
527 return node;
528}
529
530//
531// Add branches.
532//
533TIntermBranch* TIntermediate::addBranch(TOperator branchOp, TSourceLoc line)
534{
535 return addBranch(branchOp, 0, line);
536}
537
538TIntermBranch* TIntermediate::addBranch(TOperator branchOp, TIntermTyped* expression, TSourceLoc line)
539{
540 TIntermBranch* node = new TIntermBranch(branchOp, expression);
541 node->setLine(line);
542
543 return node;
544}
545
546//
547// This is to be executed once the final root is put on top by the parsing
548// process.
549//
550bool TIntermediate::postProcess(TIntermNode* root)
551{
552 if (root == 0)
553 return true;
554
555 //
556 // First, finish off the top level sequence, if any
557 //
558 TIntermAggregate* aggRoot = root->getAsAggregate();
559 if (aggRoot && aggRoot->getOp() == EOpNull)
560 aggRoot->setOp(EOpSequence);
561
562 return true;
563}
564
John Bauman66b8ab22014-05-06 15:57:45 -0400565////////////////////////////////////////////////////////////////
566//
567// Member functions of the nodes used for building the tree.
568//
569////////////////////////////////////////////////////////////////
570
571//
572// Say whether or not an operation node changes the value of a variable.
573//
574// Returns true if state is modified.
575//
576bool TIntermOperator::modifiesState() const
577{
578 switch (op) {
579 case EOpPostIncrement:
580 case EOpPostDecrement:
581 case EOpPreIncrement:
582 case EOpPreDecrement:
583 case EOpAssign:
584 case EOpAddAssign:
585 case EOpSubAssign:
586 case EOpMulAssign:
587 case EOpVectorTimesMatrixAssign:
588 case EOpVectorTimesScalarAssign:
589 case EOpMatrixTimesScalarAssign:
590 case EOpMatrixTimesMatrixAssign:
591 case EOpDivAssign:
592 return true;
593 default:
594 return false;
595 }
596}
597
598//
599// returns true if the operator is for one of the constructors
600//
601bool TIntermOperator::isConstructor() const
602{
603 switch (op) {
604 case EOpConstructVec2:
605 case EOpConstructVec3:
606 case EOpConstructVec4:
607 case EOpConstructMat2:
608 case EOpConstructMat3:
609 case EOpConstructMat4:
610 case EOpConstructFloat:
611 case EOpConstructIVec2:
612 case EOpConstructIVec3:
613 case EOpConstructIVec4:
614 case EOpConstructInt:
Nicolas Capense4b1b1d2015-02-17 17:26:01 -0500615 case EOpConstructUVec2:
616 case EOpConstructUVec3:
617 case EOpConstructUVec4:
Nicolas Capens3c20f802015-02-17 17:17:20 -0500618 case EOpConstructUInt:
John Bauman66b8ab22014-05-06 15:57:45 -0400619 case EOpConstructBVec2:
620 case EOpConstructBVec3:
621 case EOpConstructBVec4:
622 case EOpConstructBool:
623 case EOpConstructStruct:
624 return true;
625 default:
626 return false;
627 }
628}
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400629
John Bauman66b8ab22014-05-06 15:57:45 -0400630//
631// Make sure the type of a unary operator is appropriate for its
632// combination of operation and operand type.
633//
634// Returns false in nothing makes sense.
635//
636bool TIntermUnary::promote(TInfoSink&)
637{
638 switch (op) {
639 case EOpLogicalNot:
640 if (operand->getBasicType() != EbtBool)
641 return false;
642 break;
643 case EOpNegative:
644 case EOpPostIncrement:
645 case EOpPostDecrement:
646 case EOpPreIncrement:
647 case EOpPreDecrement:
648 if (operand->getBasicType() == EbtBool)
649 return false;
650 break;
651
652 // operators for built-ins are already type checked against their prototype
653 case EOpAny:
654 case EOpAll:
655 case EOpVectorLogicalNot:
656 return true;
657
658 default:
659 if (operand->getBasicType() != EbtFloat)
660 return false;
661 }
662
663 setType(operand->getType());
664
665 // Unary operations results in temporary variables unless const.
Nicolas Capens31ad2aa2015-02-26 13:14:27 -0500666 if (operand->getQualifier() != EvqConstExpr) {
John Bauman66b8ab22014-05-06 15:57:45 -0400667 getTypePointer()->setQualifier(EvqTemporary);
668 }
669
670 return true;
671}
672
673//
674// Establishes the type of the resultant operation, as well as
675// makes the operator the correct one for the operands.
676//
677// Returns false if operator can't work on operands.
678//
679bool TIntermBinary::promote(TInfoSink& infoSink)
680{
681 // This function only handles scalars, vectors, and matrices.
682 if (left->isArray() || right->isArray()) {
683 infoSink.info.message(EPrefixInternalError, "Invalid operation for arrays", getLine());
684 return false;
685 }
686
687 // GLSL ES 2.0 does not support implicit type casting.
688 // So the basic type should always match.
689 if (left->getBasicType() != right->getBasicType())
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400690 {
John Bauman66b8ab22014-05-06 15:57:45 -0400691 return false;
Nicolas Capens7c0ec1e2014-06-12 12:18:44 -0400692 }
John Bauman66b8ab22014-05-06 15:57:45 -0400693
694 //
695 // Base assumption: just make the type the same as the left
696 // operand. Then only deviations from this need be coded.
697 //
698 setType(left->getType());
699
700 // The result gets promoted to the highest precision.
701 TPrecision higherPrecision = GetHigherPrecision(left->getPrecision(), right->getPrecision());
702 getTypePointer()->setPrecision(higherPrecision);
703
704 // Binary operations results in temporary variables unless both
705 // operands are const.
Nicolas Capens31ad2aa2015-02-26 13:14:27 -0500706 if (left->getQualifier() != EvqConstExpr || right->getQualifier() != EvqConstExpr) {
John Bauman66b8ab22014-05-06 15:57:45 -0400707 getTypePointer()->setQualifier(EvqTemporary);
708 }
709
710 int size = std::max(left->getNominalSize(), right->getNominalSize());
Alexis Hetub14178b2015-04-13 13:23:20 -0400711 int matrixSize = std::max(left->getSecondarySize(), right->getSecondarySize()); // FIXME: This will have to change for NxM matrices
John Bauman66b8ab22014-05-06 15:57:45 -0400712
713 //
714 // All scalars. Code after this test assumes this case is removed!
715 //
716 if (size == 1) {
717 switch (op) {
718 //
719 // Promote to conditional
720 //
721 case EOpEqual:
722 case EOpNotEqual:
723 case EOpLessThan:
724 case EOpGreaterThan:
725 case EOpLessThanEqual:
726 case EOpGreaterThanEqual:
727 setType(TType(EbtBool, EbpUndefined));
728 break;
729
730 //
731 // And and Or operate on conditionals
732 //
733 case EOpLogicalAnd:
734 case EOpLogicalOr:
735 // Both operands must be of type bool.
736 if (left->getBasicType() != EbtBool || right->getBasicType() != EbtBool)
737 return false;
738 setType(TType(EbtBool, EbpUndefined));
739 break;
740
741 default:
742 break;
743 }
744 return true;
745 }
746
747 // If we reach here, at least one of the operands is vector or matrix.
748 // The other operand could be a scalar, vector, or matrix.
749 // Are the sizes compatible?
750 //
751 if (left->getNominalSize() != right->getNominalSize()) {
752 // If the nominal size of operands do not match:
753 // One of them must be scalar.
754 if (left->getNominalSize() != 1 && right->getNominalSize() != 1)
755 return false;
756 // Operator cannot be of type pure assignment.
757 if (op == EOpAssign || op == EOpInitialize)
758 return false;
759 }
760
Alexis Hetub14178b2015-04-13 13:23:20 -0400761 if (left->getSecondarySize() != right->getSecondarySize()) {
762 // Operator cannot be of type pure assignment.
763 if (op == EOpAssign || op == EOpInitialize)
764 return false;
765 }
766
John Bauman66b8ab22014-05-06 15:57:45 -0400767 //
768 // Can these two operands be combined?
769 //
770 TBasicType basicType = left->getBasicType();
771 switch (op) {
772 case EOpMul:
773 if (!left->isMatrix() && right->isMatrix()) {
774 if (left->isVector())
775 op = EOpVectorTimesMatrix;
776 else {
777 op = EOpMatrixTimesScalar;
Alexis Hetub14178b2015-04-13 13:23:20 -0400778 setType(TType(basicType, higherPrecision, EvqTemporary, size, matrixSize));
John Bauman66b8ab22014-05-06 15:57:45 -0400779 }
780 } else if (left->isMatrix() && !right->isMatrix()) {
781 if (right->isVector()) {
782 op = EOpMatrixTimesVector;
Alexis Hetub14178b2015-04-13 13:23:20 -0400783 setType(TType(basicType, higherPrecision, EvqTemporary, size, 1));
John Bauman66b8ab22014-05-06 15:57:45 -0400784 } else {
785 op = EOpMatrixTimesScalar;
786 }
787 } else if (left->isMatrix() && right->isMatrix()) {
788 op = EOpMatrixTimesMatrix;
789 } else if (!left->isMatrix() && !right->isMatrix()) {
790 if (left->isVector() && right->isVector()) {
791 // leave as component product
792 } else if (left->isVector() || right->isVector()) {
793 op = EOpVectorTimesScalar;
Alexis Hetub14178b2015-04-13 13:23:20 -0400794 setType(TType(basicType, higherPrecision, EvqTemporary, size, 1));
John Bauman66b8ab22014-05-06 15:57:45 -0400795 }
796 } else {
797 infoSink.info.message(EPrefixInternalError, "Missing elses", getLine());
798 return false;
799 }
800 break;
801 case EOpMulAssign:
802 if (!left->isMatrix() && right->isMatrix()) {
803 if (left->isVector())
804 op = EOpVectorTimesMatrixAssign;
805 else {
806 return false;
807 }
808 } else if (left->isMatrix() && !right->isMatrix()) {
809 if (right->isVector()) {
810 return false;
811 } else {
812 op = EOpMatrixTimesScalarAssign;
813 }
814 } else if (left->isMatrix() && right->isMatrix()) {
815 op = EOpMatrixTimesMatrixAssign;
816 } else if (!left->isMatrix() && !right->isMatrix()) {
817 if (left->isVector() && right->isVector()) {
818 // leave as component product
819 } else if (left->isVector() || right->isVector()) {
820 if (! left->isVector())
821 return false;
822 op = EOpVectorTimesScalarAssign;
Alexis Hetub14178b2015-04-13 13:23:20 -0400823 setType(TType(basicType, higherPrecision, EvqTemporary, size, 1));
John Bauman66b8ab22014-05-06 15:57:45 -0400824 }
825 } else {
826 infoSink.info.message(EPrefixInternalError, "Missing elses", getLine());
827 return false;
828 }
829 break;
830
831 case EOpAssign:
832 case EOpInitialize:
833 case EOpAdd:
834 case EOpSub:
835 case EOpDiv:
836 case EOpAddAssign:
837 case EOpSubAssign:
838 case EOpDivAssign:
839 if ((left->isMatrix() && right->isVector()) ||
840 (left->isVector() && right->isMatrix()))
841 return false;
Alexis Hetub14178b2015-04-13 13:23:20 -0400842 setType(TType(basicType, higherPrecision, EvqTemporary, size, matrixSize));
John Bauman66b8ab22014-05-06 15:57:45 -0400843 break;
844
845 case EOpEqual:
846 case EOpNotEqual:
847 case EOpLessThan:
848 case EOpGreaterThan:
849 case EOpLessThanEqual:
850 case EOpGreaterThanEqual:
851 if ((left->isMatrix() && right->isVector()) ||
852 (left->isVector() && right->isMatrix()))
853 return false;
854 setType(TType(EbtBool, EbpUndefined));
855 break;
856
857 default:
858 return false;
859 }
860
861 return true;
862}
863
864bool CompareStruct(const TType& leftNodeType, ConstantUnion* rightUnionArray, ConstantUnion* leftUnionArray)
865{
866 const TTypeList* fields = leftNodeType.getStruct();
867
868 size_t structSize = fields->size();
869 int index = 0;
870
871 for (size_t j = 0; j < structSize; j++) {
872 int size = (*fields)[j].type->getObjectSize();
873 for (int i = 0; i < size; i++) {
874 if ((*fields)[j].type->getBasicType() == EbtStruct) {
875 if (!CompareStructure(*(*fields)[j].type, &rightUnionArray[index], &leftUnionArray[index]))
876 return false;
877 } else {
878 if (leftUnionArray[index] != rightUnionArray[index])
879 return false;
880 index++;
881 }
882
883 }
884 }
885 return true;
886}
887
888bool CompareStructure(const TType& leftNodeType, ConstantUnion* rightUnionArray, ConstantUnion* leftUnionArray)
889{
890 if (leftNodeType.isArray()) {
891 TType typeWithoutArrayness = leftNodeType;
892 typeWithoutArrayness.clearArrayness();
893
894 int arraySize = leftNodeType.getArraySize();
895
896 for (int i = 0; i < arraySize; ++i) {
897 int offset = typeWithoutArrayness.getObjectSize() * i;
898 if (!CompareStruct(typeWithoutArrayness, &rightUnionArray[offset], &leftUnionArray[offset]))
899 return false;
900 }
901 } else
902 return CompareStruct(leftNodeType, rightUnionArray, leftUnionArray);
903
904 return true;
905}
906
907//
908// The fold functions see if an operation on a constant can be done in place,
909// without generating run-time code.
910//
911// Returns the node to keep using, which may or may not be the node passed in.
912//
913
914TIntermTyped* TIntermConstantUnion::fold(TOperator op, TIntermTyped* constantNode, TInfoSink& infoSink)
915{
916 ConstantUnion *unionArray = getUnionArrayPointer();
917 int objectSize = getType().getObjectSize();
918
919 if (constantNode) { // binary operations
920 TIntermConstantUnion *node = constantNode->getAsConstantUnion();
921 ConstantUnion *rightUnionArray = node->getUnionArrayPointer();
922 TType returnType = getType();
923
924 // for a case like float f = 1.2 + vec4(2,3,4,5);
925 if (constantNode->getType().getObjectSize() == 1 && objectSize > 1) {
926 rightUnionArray = new ConstantUnion[objectSize];
927 for (int i = 0; i < objectSize; ++i)
928 rightUnionArray[i] = *node->getUnionArrayPointer();
929 returnType = getType();
930 } else if (constantNode->getType().getObjectSize() > 1 && objectSize == 1) {
931 // for a case like float f = vec4(2,3,4,5) + 1.2;
932 unionArray = new ConstantUnion[constantNode->getType().getObjectSize()];
933 for (int i = 0; i < constantNode->getType().getObjectSize(); ++i)
934 unionArray[i] = *getUnionArrayPointer();
935 returnType = node->getType();
936 objectSize = constantNode->getType().getObjectSize();
937 }
938
939 ConstantUnion* tempConstArray = 0;
940 TIntermConstantUnion *tempNode;
941
942 bool boolNodeFlag = false;
943 switch(op) {
944 case EOpAdd:
945 tempConstArray = new ConstantUnion[objectSize];
946 {// support MSVC++6.0
947 for (int i = 0; i < objectSize; i++)
948 tempConstArray[i] = unionArray[i] + rightUnionArray[i];
949 }
950 break;
951 case EOpSub:
952 tempConstArray = new ConstantUnion[objectSize];
953 {// support MSVC++6.0
954 for (int i = 0; i < objectSize; i++)
955 tempConstArray[i] = unionArray[i] - rightUnionArray[i];
956 }
957 break;
958
959 case EOpMul:
960 case EOpVectorTimesScalar:
961 case EOpMatrixTimesScalar:
962 tempConstArray = new ConstantUnion[objectSize];
963 {// support MSVC++6.0
964 for (int i = 0; i < objectSize; i++)
965 tempConstArray[i] = unionArray[i] * rightUnionArray[i];
966 }
967 break;
968 case EOpMatrixTimesMatrix:
969 if (getType().getBasicType() != EbtFloat || node->getBasicType() != EbtFloat) {
970 infoSink.info.message(EPrefixInternalError, "Constant Folding cannot be done for matrix multiply", getLine());
971 return 0;
972 }
973 {// support MSVC++6.0
974 int size = getNominalSize();
975 tempConstArray = new ConstantUnion[size*size];
976 for (int row = 0; row < size; row++) {
977 for (int column = 0; column < size; column++) {
978 tempConstArray[size * column + row].setFConst(0.0f);
979 for (int i = 0; i < size; i++) {
980 tempConstArray[size * column + row].setFConst(tempConstArray[size * column + row].getFConst() + unionArray[i * size + row].getFConst() * (rightUnionArray[column * size + i].getFConst()));
981 }
982 }
983 }
984 }
985 break;
986 case EOpDiv:
987 tempConstArray = new ConstantUnion[objectSize];
988 {// support MSVC++6.0
989 for (int i = 0; i < objectSize; i++) {
990 switch (getType().getBasicType()) {
991 case EbtFloat:
992 if (rightUnionArray[i] == 0.0f) {
993 infoSink.info.message(EPrefixWarning, "Divide by zero error during constant folding", getLine());
994 tempConstArray[i].setFConst(FLT_MAX);
995 } else
996 tempConstArray[i].setFConst(unionArray[i].getFConst() / rightUnionArray[i].getFConst());
997 break;
998
999 case EbtInt:
1000 if (rightUnionArray[i] == 0) {
1001 infoSink.info.message(EPrefixWarning, "Divide by zero error during constant folding", getLine());
1002 tempConstArray[i].setIConst(INT_MAX);
1003 } else
1004 tempConstArray[i].setIConst(unionArray[i].getIConst() / rightUnionArray[i].getIConst());
1005 break;
Nicolas Capens3c20f802015-02-17 17:17:20 -05001006 case EbtUInt:
1007 if (rightUnionArray[i] == 0) {
1008 infoSink.info.message(EPrefixWarning, "Divide by zero error during constant folding", getLine());
1009 tempConstArray[i].setUConst(UINT_MAX);
1010 } else
1011 tempConstArray[i].setUConst(unionArray[i].getUConst() / rightUnionArray[i].getUConst());
1012 break;
John Bauman66b8ab22014-05-06 15:57:45 -04001013 default:
1014 infoSink.info.message(EPrefixInternalError, "Constant folding cannot be done for \"/\"", getLine());
1015 return 0;
1016 }
1017 }
1018 }
1019 break;
1020
1021 case EOpMatrixTimesVector:
1022 if (node->getBasicType() != EbtFloat) {
1023 infoSink.info.message(EPrefixInternalError, "Constant Folding cannot be done for matrix times vector", getLine());
1024 return 0;
1025 }
1026 tempConstArray = new ConstantUnion[getNominalSize()];
1027
1028 {// support MSVC++6.0
1029 for (int size = getNominalSize(), i = 0; i < size; i++) {
1030 tempConstArray[i].setFConst(0.0f);
1031 for (int j = 0; j < size; j++) {
1032 tempConstArray[i].setFConst(tempConstArray[i].getFConst() + ((unionArray[j*size + i].getFConst()) * rightUnionArray[j].getFConst()));
1033 }
1034 }
1035 }
1036
1037 tempNode = new TIntermConstantUnion(tempConstArray, node->getType());
1038 tempNode->setLine(getLine());
1039
1040 return tempNode;
1041
1042 case EOpVectorTimesMatrix:
1043 if (getType().getBasicType() != EbtFloat) {
1044 infoSink.info.message(EPrefixInternalError, "Constant Folding cannot be done for vector times matrix", getLine());
1045 return 0;
1046 }
1047
1048 tempConstArray = new ConstantUnion[getNominalSize()];
1049 {// support MSVC++6.0
1050 for (int size = getNominalSize(), i = 0; i < size; i++) {
1051 tempConstArray[i].setFConst(0.0f);
1052 for (int j = 0; j < size; j++) {
1053 tempConstArray[i].setFConst(tempConstArray[i].getFConst() + ((unionArray[j].getFConst()) * rightUnionArray[i*size + j].getFConst()));
1054 }
1055 }
1056 }
1057 break;
1058
1059 case EOpLogicalAnd: // this code is written for possible future use, will not get executed currently
1060 tempConstArray = new ConstantUnion[objectSize];
1061 {// support MSVC++6.0
1062 for (int i = 0; i < objectSize; i++)
1063 tempConstArray[i] = unionArray[i] && rightUnionArray[i];
1064 }
1065 break;
1066
1067 case EOpLogicalOr: // this code is written for possible future use, will not get executed currently
1068 tempConstArray = new ConstantUnion[objectSize];
1069 {// support MSVC++6.0
1070 for (int i = 0; i < objectSize; i++)
1071 tempConstArray[i] = unionArray[i] || rightUnionArray[i];
1072 }
1073 break;
1074
1075 case EOpLogicalXor:
1076 tempConstArray = new ConstantUnion[objectSize];
1077 {// support MSVC++6.0
1078 for (int i = 0; i < objectSize; i++)
1079 switch (getType().getBasicType()) {
1080 case EbtBool: tempConstArray[i].setBConst((unionArray[i] == rightUnionArray[i]) ? false : true); break;
1081 default: assert(false && "Default missing");
1082 }
1083 }
1084 break;
1085
1086 case EOpLessThan:
1087 assert(objectSize == 1);
1088 tempConstArray = new ConstantUnion[1];
1089 tempConstArray->setBConst(*unionArray < *rightUnionArray);
Nicolas Capens31ad2aa2015-02-26 13:14:27 -05001090 returnType = TType(EbtBool, EbpUndefined, EvqConstExpr);
John Bauman66b8ab22014-05-06 15:57:45 -04001091 break;
1092 case EOpGreaterThan:
1093 assert(objectSize == 1);
1094 tempConstArray = new ConstantUnion[1];
1095 tempConstArray->setBConst(*unionArray > *rightUnionArray);
Nicolas Capens31ad2aa2015-02-26 13:14:27 -05001096 returnType = TType(EbtBool, EbpUndefined, EvqConstExpr);
John Bauman66b8ab22014-05-06 15:57:45 -04001097 break;
1098 case EOpLessThanEqual:
1099 {
1100 assert(objectSize == 1);
1101 ConstantUnion constant;
1102 constant.setBConst(*unionArray > *rightUnionArray);
1103 tempConstArray = new ConstantUnion[1];
1104 tempConstArray->setBConst(!constant.getBConst());
Nicolas Capens31ad2aa2015-02-26 13:14:27 -05001105 returnType = TType(EbtBool, EbpUndefined, EvqConstExpr);
John Bauman66b8ab22014-05-06 15:57:45 -04001106 break;
1107 }
1108 case EOpGreaterThanEqual:
1109 {
1110 assert(objectSize == 1);
1111 ConstantUnion constant;
1112 constant.setBConst(*unionArray < *rightUnionArray);
1113 tempConstArray = new ConstantUnion[1];
1114 tempConstArray->setBConst(!constant.getBConst());
Nicolas Capens31ad2aa2015-02-26 13:14:27 -05001115 returnType = TType(EbtBool, EbpUndefined, EvqConstExpr);
John Bauman66b8ab22014-05-06 15:57:45 -04001116 break;
1117 }
1118
1119 case EOpEqual:
1120 if (getType().getBasicType() == EbtStruct) {
1121 if (!CompareStructure(node->getType(), node->getUnionArrayPointer(), unionArray))
1122 boolNodeFlag = true;
1123 } else {
1124 for (int i = 0; i < objectSize; i++) {
1125 if (unionArray[i] != rightUnionArray[i]) {
1126 boolNodeFlag = true;
1127 break; // break out of for loop
1128 }
1129 }
1130 }
1131
1132 tempConstArray = new ConstantUnion[1];
1133 if (!boolNodeFlag) {
1134 tempConstArray->setBConst(true);
1135 }
1136 else {
1137 tempConstArray->setBConst(false);
1138 }
1139
Nicolas Capens31ad2aa2015-02-26 13:14:27 -05001140 tempNode = new TIntermConstantUnion(tempConstArray, TType(EbtBool, EbpUndefined, EvqConstExpr));
John Bauman66b8ab22014-05-06 15:57:45 -04001141 tempNode->setLine(getLine());
1142
1143 return tempNode;
1144
1145 case EOpNotEqual:
1146 if (getType().getBasicType() == EbtStruct) {
1147 if (CompareStructure(node->getType(), node->getUnionArrayPointer(), unionArray))
1148 boolNodeFlag = true;
1149 } else {
1150 for (int i = 0; i < objectSize; i++) {
1151 if (unionArray[i] == rightUnionArray[i]) {
1152 boolNodeFlag = true;
1153 break; // break out of for loop
1154 }
1155 }
1156 }
1157
1158 tempConstArray = new ConstantUnion[1];
1159 if (!boolNodeFlag) {
1160 tempConstArray->setBConst(true);
1161 }
1162 else {
1163 tempConstArray->setBConst(false);
1164 }
1165
Nicolas Capens31ad2aa2015-02-26 13:14:27 -05001166 tempNode = new TIntermConstantUnion(tempConstArray, TType(EbtBool, EbpUndefined, EvqConstExpr));
John Bauman66b8ab22014-05-06 15:57:45 -04001167 tempNode->setLine(getLine());
1168
1169 return tempNode;
1170
1171 default:
1172 infoSink.info.message(EPrefixInternalError, "Invalid operator for constant folding", getLine());
1173 return 0;
1174 }
1175 tempNode = new TIntermConstantUnion(tempConstArray, returnType);
1176 tempNode->setLine(getLine());
1177
1178 return tempNode;
1179 } else {
1180 //
1181 // Do unary operations
1182 //
1183 TIntermConstantUnion *newNode = 0;
1184 ConstantUnion* tempConstArray = new ConstantUnion[objectSize];
1185 for (int i = 0; i < objectSize; i++) {
1186 switch(op) {
1187 case EOpNegative:
1188 switch (getType().getBasicType()) {
1189 case EbtFloat: tempConstArray[i].setFConst(-unionArray[i].getFConst()); break;
1190 case EbtInt: tempConstArray[i].setIConst(-unionArray[i].getIConst()); break;
1191 default:
1192 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1193 return 0;
1194 }
1195 break;
1196 case EOpLogicalNot: // this code is written for possible future use, will not get executed currently
1197 switch (getType().getBasicType()) {
1198 case EbtBool: tempConstArray[i].setBConst(!unionArray[i].getBConst()); break;
1199 default:
1200 infoSink.info.message(EPrefixInternalError, "Unary operation not folded into constant", getLine());
1201 return 0;
1202 }
1203 break;
1204 default:
1205 return 0;
1206 }
1207 }
1208 newNode = new TIntermConstantUnion(tempConstArray, getType());
1209 newNode->setLine(getLine());
1210 return newNode;
1211 }
1212}
1213
1214TIntermTyped* TIntermediate::promoteConstantUnion(TBasicType promoteTo, TIntermConstantUnion* node)
1215{
John Bauman66b8ab22014-05-06 15:57:45 -04001216 int size = node->getType().getObjectSize();
1217
1218 ConstantUnion *leftUnionArray = new ConstantUnion[size];
1219
1220 for (int i=0; i < size; i++) {
1221
1222 switch (promoteTo) {
1223 case EbtFloat:
1224 switch (node->getType().getBasicType()) {
1225 case EbtInt:
Nicolas Capens55b22d62015-02-10 13:58:40 -05001226 leftUnionArray[i].setFConst(static_cast<float>(node->getIConst(i)));
John Bauman66b8ab22014-05-06 15:57:45 -04001227 break;
Nicolas Capens3c20f802015-02-17 17:17:20 -05001228 case EbtUInt:
1229 leftUnionArray[i].setFConst(static_cast<float>(node->getUConst(i)));
1230 break;
John Bauman66b8ab22014-05-06 15:57:45 -04001231 case EbtBool:
Nicolas Capens55b22d62015-02-10 13:58:40 -05001232 leftUnionArray[i].setFConst(static_cast<float>(node->getBConst(i)));
John Bauman66b8ab22014-05-06 15:57:45 -04001233 break;
1234 case EbtFloat:
Nicolas Capens55b22d62015-02-10 13:58:40 -05001235 leftUnionArray[i].setFConst(static_cast<float>(node->getFConst(i)));
John Bauman66b8ab22014-05-06 15:57:45 -04001236 break;
1237 default:
1238 infoSink.info.message(EPrefixInternalError, "Cannot promote", node->getLine());
1239 return 0;
1240 }
1241 break;
1242 case EbtInt:
1243 switch (node->getType().getBasicType()) {
1244 case EbtInt:
Nicolas Capens55b22d62015-02-10 13:58:40 -05001245 leftUnionArray[i].setIConst(static_cast<int>(node->getIConst(i)));
John Bauman66b8ab22014-05-06 15:57:45 -04001246 break;
Nicolas Capens3c20f802015-02-17 17:17:20 -05001247 case EbtUInt:
1248 leftUnionArray[i].setIConst(static_cast<int>(node->getUConst(i)));
1249 break;
John Bauman66b8ab22014-05-06 15:57:45 -04001250 case EbtBool:
Nicolas Capens55b22d62015-02-10 13:58:40 -05001251 leftUnionArray[i].setIConst(static_cast<int>(node->getBConst(i)));
John Bauman66b8ab22014-05-06 15:57:45 -04001252 break;
1253 case EbtFloat:
Nicolas Capens55b22d62015-02-10 13:58:40 -05001254 leftUnionArray[i].setIConst(static_cast<int>(node->getFConst(i)));
John Bauman66b8ab22014-05-06 15:57:45 -04001255 break;
1256 default:
1257 infoSink.info.message(EPrefixInternalError, "Cannot promote", node->getLine());
1258 return 0;
1259 }
1260 break;
Nicolas Capens3c20f802015-02-17 17:17:20 -05001261 case EbtUInt:
1262 switch (node->getType().getBasicType()) {
1263 case EbtInt:
1264 leftUnionArray[i].setUConst(static_cast<unsigned int>(node->getIConst(i)));
1265 break;
1266 case EbtUInt:
1267 leftUnionArray[i].setUConst(static_cast<unsigned int>(node->getUConst(i)));
1268 break;
1269 case EbtBool:
1270 leftUnionArray[i].setUConst(static_cast<unsigned int>(node->getBConst(i)));
1271 break;
1272 case EbtFloat:
1273 leftUnionArray[i].setUConst(static_cast<unsigned int>(node->getFConst(i)));
1274 break;
1275 default:
1276 infoSink.info.message(EPrefixInternalError, "Cannot promote", node->getLine());
1277 return 0;
1278 }
1279 break;
John Bauman66b8ab22014-05-06 15:57:45 -04001280 case EbtBool:
1281 switch (node->getType().getBasicType()) {
1282 case EbtInt:
Nicolas Capens55b22d62015-02-10 13:58:40 -05001283 leftUnionArray[i].setBConst(node->getIConst(i) != 0);
John Bauman66b8ab22014-05-06 15:57:45 -04001284 break;
Nicolas Capens3c20f802015-02-17 17:17:20 -05001285 case EbtUInt:
1286 leftUnionArray[i].setBConst(node->getUConst(i) != 0);
1287 break;
John Bauman66b8ab22014-05-06 15:57:45 -04001288 case EbtBool:
Nicolas Capens55b22d62015-02-10 13:58:40 -05001289 leftUnionArray[i].setBConst(node->getBConst(i));
John Bauman66b8ab22014-05-06 15:57:45 -04001290 break;
1291 case EbtFloat:
Nicolas Capens55b22d62015-02-10 13:58:40 -05001292 leftUnionArray[i].setBConst(node->getFConst(i) != 0.0f);
John Bauman66b8ab22014-05-06 15:57:45 -04001293 break;
1294 default:
1295 infoSink.info.message(EPrefixInternalError, "Cannot promote", node->getLine());
1296 return 0;
1297 }
1298
1299 break;
1300 default:
1301 infoSink.info.message(EPrefixInternalError, "Incorrect data type found", node->getLine());
1302 return 0;
1303 }
1304
1305 }
1306
1307 const TType& t = node->getType();
1308
Alexis Hetub14178b2015-04-13 13:23:20 -04001309 return addConstantUnion(leftUnionArray, TType(promoteTo, t.getPrecision(), t.getQualifier(), t.getNominalSize(), t.getSecondarySize(), t.isArray()), node->getLine());
John Bauman66b8ab22014-05-06 15:57:45 -04001310}
1311