2 // Copyright (c) 2002-2013 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.
7 #include "compiler/translator/InitializeVariables.h"
8 #include "compiler/translator/compilerdebug.h"
13 TIntermConstantUnion* constructFloatConstUnionNode(const TType& type)
16 unsigned char size = myType.getNominalSize();
17 if (myType.isMatrix())
19 ConstantUnion *u = new ConstantUnion[size];
20 for (int ii = 0; ii < size; ++ii)
21 u[ii].setFConst(0.0f);
23 myType.clearArrayness();
24 myType.setQualifier(EvqConst);
25 TIntermConstantUnion *node = new TIntermConstantUnion(u, myType);
29 TIntermConstantUnion* constructIndexNode(int index)
31 ConstantUnion *u = new ConstantUnion[1];
32 u[0].setIConst(index);
34 TType type(EbtInt, EbpUndefined, EvqConst, 1);
35 TIntermConstantUnion *node = new TIntermConstantUnion(u, type);
39 } // namespace anonymous
41 bool InitializeVariables::visitAggregate(Visit visit, TIntermAggregate* node)
43 bool visitChildren = !mCodeInserted;
44 switch (node->getOp())
50 // Function definition.
51 ASSERT(visit == PreVisit);
52 if (node->getName() == "main(")
54 TIntermSequence &sequence = node->getSequence();
55 ASSERT((sequence.size() == 1) || (sequence.size() == 2));
56 TIntermAggregate *body = NULL;
57 if (sequence.size() == 1)
59 body = new TIntermAggregate(EOpSequence);
60 sequence.push_back(body);
64 body = sequence[1]->getAsAggregate();
67 insertInitCode(body->getSequence());
73 visitChildren = false;
79 void InitializeVariables::insertInitCode(TIntermSequence& sequence)
81 for (size_t ii = 0; ii < mVariables.size(); ++ii)
83 const InitVariableInfo& varInfo = mVariables[ii];
85 if (varInfo.type.isArray())
87 for (int index = varInfo.type.getArraySize() - 1; index >= 0; --index)
89 TIntermBinary *assign = new TIntermBinary(EOpAssign);
90 sequence.insert(sequence.begin(), assign);
92 TIntermBinary *indexDirect = new TIntermBinary(EOpIndexDirect);
93 TIntermSymbol *symbol = new TIntermSymbol(0, varInfo.name, varInfo.type);
94 indexDirect->setLeft(symbol);
95 TIntermConstantUnion *indexNode = constructIndexNode(index);
96 indexDirect->setRight(indexNode);
98 assign->setLeft(indexDirect);
100 TIntermConstantUnion *zeroConst = constructFloatConstUnionNode(varInfo.type);
101 assign->setRight(zeroConst);
106 TIntermBinary *assign = new TIntermBinary(EOpAssign);
107 sequence.insert(sequence.begin(), assign);
108 TIntermSymbol *symbol = new TIntermSymbol(0, varInfo.name, varInfo.type);
109 assign->setLeft(symbol);
110 TIntermConstantUnion *zeroConst = constructFloatConstUnionNode(varInfo.type);
111 assign->setRight(zeroConst);