blob: a6aab2207d233b943402d67e3c2177cf5e9779a9 [file] [log] [blame]
Olli Etuahofc0e2bc2015-04-16 13:39:56 +03001//
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//
Olli Etuahoa6f22092015-05-08 18:31:10 +03006// The SeparateDeclarations function processes declarations, so that in the end each declaration
7// contains only one declarator.
8// This is useful as an intermediate step when initialization needs to be separated from declaration,
9// or when things need to be unfolded out of the initializer.
Olli Etuahofc0e2bc2015-04-16 13:39:56 +030010// Example:
11// int a[1] = int[1](1), b[1] = int[1](2);
12// gets transformed when run through this class into the AST equivalent of:
13// int a[1] = int[1](1);
14// int b[1] = int[1](2);
15
16#include "compiler/translator/SeparateDeclarations.h"
17
18#include "compiler/translator/IntermNode.h"
19
20namespace
21{
22
Olli Etuahoa6f22092015-05-08 18:31:10 +030023class SeparateDeclarationsTraverser : private TIntermTraverser
Olli Etuahofc0e2bc2015-04-16 13:39:56 +030024{
25 public:
26 static void apply(TIntermNode *root);
27 private:
Olli Etuahoa6f22092015-05-08 18:31:10 +030028 SeparateDeclarationsTraverser();
Olli Etuaho13389b62016-10-16 11:48:18 +010029 bool visitDeclaration(Visit, TIntermDeclaration *node) override;
Olli Etuahofc0e2bc2015-04-16 13:39:56 +030030};
31
Olli Etuahoa6f22092015-05-08 18:31:10 +030032void SeparateDeclarationsTraverser::apply(TIntermNode *root)
Olli Etuahofc0e2bc2015-04-16 13:39:56 +030033{
Olli Etuahoa6f22092015-05-08 18:31:10 +030034 SeparateDeclarationsTraverser separateDecl;
Olli Etuahofc0e2bc2015-04-16 13:39:56 +030035 root->traverse(&separateDecl);
36 separateDecl.updateTree();
37}
38
Olli Etuahoa6f22092015-05-08 18:31:10 +030039SeparateDeclarationsTraverser::SeparateDeclarationsTraverser()
Olli Etuahofc0e2bc2015-04-16 13:39:56 +030040 : TIntermTraverser(true, false, false)
41{
42}
43
Olli Etuaho13389b62016-10-16 11:48:18 +010044bool SeparateDeclarationsTraverser::visitDeclaration(Visit, TIntermDeclaration *node)
Olli Etuahofc0e2bc2015-04-16 13:39:56 +030045{
Olli Etuaho13389b62016-10-16 11:48:18 +010046 TIntermSequence *sequence = node->getSequence();
47 if (sequence->size() > 1)
Olli Etuahofc0e2bc2015-04-16 13:39:56 +030048 {
Olli Etuaho13389b62016-10-16 11:48:18 +010049 TIntermBlock *parentBlock = getParentNode()->getAsBlock();
50 ASSERT(parentBlock != nullptr);
51
52 TIntermSequence replacementDeclarations;
53 for (size_t ii = 0; ii < sequence->size(); ++ii)
Olli Etuahofc0e2bc2015-04-16 13:39:56 +030054 {
Olli Etuaho13389b62016-10-16 11:48:18 +010055 TIntermDeclaration *replacementDeclaration = new TIntermDeclaration();
Olli Etuahofc0e2bc2015-04-16 13:39:56 +030056
Olli Etuaho13389b62016-10-16 11:48:18 +010057 replacementDeclaration->appendDeclarator(sequence->at(ii)->getAsTyped());
58 replacementDeclaration->setLine(sequence->at(ii)->getLine());
59 replacementDeclarations.push_back(replacementDeclaration);
Olli Etuahofc0e2bc2015-04-16 13:39:56 +030060 }
Olli Etuaho13389b62016-10-16 11:48:18 +010061
62 mMultiReplacements.push_back(
63 NodeReplaceWithMultipleEntry(parentBlock, node, replacementDeclarations));
Olli Etuahofc0e2bc2015-04-16 13:39:56 +030064 }
Olli Etuaho13389b62016-10-16 11:48:18 +010065 return false;
Olli Etuahofc0e2bc2015-04-16 13:39:56 +030066}
67
68} // namespace
69
Olli Etuahoa6f22092015-05-08 18:31:10 +030070void SeparateDeclarations(TIntermNode *root)
Olli Etuahofc0e2bc2015-04-16 13:39:56 +030071{
Olli Etuahoa6f22092015-05-08 18:31:10 +030072 SeparateDeclarationsTraverser::apply(root);
Olli Etuahofc0e2bc2015-04-16 13:39:56 +030073}