blob: 4d38353700c1eeb37fad97d5be1384939159f06d [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
Jamie Madill45bcc782016-11-07 13:58:48 -050020namespace sh
21{
22
Olli Etuahofc0e2bc2015-04-16 13:39:56 +030023namespace
24{
25
Olli Etuahoa6f22092015-05-08 18:31:10 +030026class SeparateDeclarationsTraverser : private TIntermTraverser
Olli Etuahofc0e2bc2015-04-16 13:39:56 +030027{
28 public:
29 static void apply(TIntermNode *root);
30 private:
Olli Etuahoa6f22092015-05-08 18:31:10 +030031 SeparateDeclarationsTraverser();
Olli Etuaho13389b62016-10-16 11:48:18 +010032 bool visitDeclaration(Visit, TIntermDeclaration *node) override;
Olli Etuahofc0e2bc2015-04-16 13:39:56 +030033};
34
Olli Etuahoa6f22092015-05-08 18:31:10 +030035void SeparateDeclarationsTraverser::apply(TIntermNode *root)
Olli Etuahofc0e2bc2015-04-16 13:39:56 +030036{
Olli Etuahoa6f22092015-05-08 18:31:10 +030037 SeparateDeclarationsTraverser separateDecl;
Olli Etuahofc0e2bc2015-04-16 13:39:56 +030038 root->traverse(&separateDecl);
39 separateDecl.updateTree();
40}
41
Olli Etuahoa6f22092015-05-08 18:31:10 +030042SeparateDeclarationsTraverser::SeparateDeclarationsTraverser()
Olli Etuahofc0e2bc2015-04-16 13:39:56 +030043 : TIntermTraverser(true, false, false)
44{
45}
46
Olli Etuaho13389b62016-10-16 11:48:18 +010047bool SeparateDeclarationsTraverser::visitDeclaration(Visit, TIntermDeclaration *node)
Olli Etuahofc0e2bc2015-04-16 13:39:56 +030048{
Olli Etuaho13389b62016-10-16 11:48:18 +010049 TIntermSequence *sequence = node->getSequence();
50 if (sequence->size() > 1)
Olli Etuahofc0e2bc2015-04-16 13:39:56 +030051 {
Olli Etuaho13389b62016-10-16 11:48:18 +010052 TIntermBlock *parentBlock = getParentNode()->getAsBlock();
53 ASSERT(parentBlock != nullptr);
54
55 TIntermSequence replacementDeclarations;
56 for (size_t ii = 0; ii < sequence->size(); ++ii)
Olli Etuahofc0e2bc2015-04-16 13:39:56 +030057 {
Olli Etuaho13389b62016-10-16 11:48:18 +010058 TIntermDeclaration *replacementDeclaration = new TIntermDeclaration();
Olli Etuahofc0e2bc2015-04-16 13:39:56 +030059
Olli Etuaho13389b62016-10-16 11:48:18 +010060 replacementDeclaration->appendDeclarator(sequence->at(ii)->getAsTyped());
61 replacementDeclaration->setLine(sequence->at(ii)->getLine());
62 replacementDeclarations.push_back(replacementDeclaration);
Olli Etuahofc0e2bc2015-04-16 13:39:56 +030063 }
Olli Etuaho13389b62016-10-16 11:48:18 +010064
65 mMultiReplacements.push_back(
66 NodeReplaceWithMultipleEntry(parentBlock, node, replacementDeclarations));
Olli Etuahofc0e2bc2015-04-16 13:39:56 +030067 }
Olli Etuaho13389b62016-10-16 11:48:18 +010068 return false;
Olli Etuahofc0e2bc2015-04-16 13:39:56 +030069}
70
71} // namespace
72
Olli Etuahoa6f22092015-05-08 18:31:10 +030073void SeparateDeclarations(TIntermNode *root)
Olli Etuahofc0e2bc2015-04-16 13:39:56 +030074{
Olli Etuahoa6f22092015-05-08 18:31:10 +030075 SeparateDeclarationsTraverser::apply(root);
Olli Etuahofc0e2bc2015-04-16 13:39:56 +030076}
Jamie Madill45bcc782016-11-07 13:58:48 -050077
78} // namespace sh