Olli Etuaho | fc0e2bc | 2015-04-16 13:39:56 +0300 | [diff] [blame] | 1 | // |
| 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 Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 6 | // 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 Etuaho | fc0e2bc | 2015-04-16 13:39:56 +0300 | [diff] [blame] | 10 | // 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 | |
| 20 | namespace |
| 21 | { |
| 22 | |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 23 | class SeparateDeclarationsTraverser : private TIntermTraverser |
Olli Etuaho | fc0e2bc | 2015-04-16 13:39:56 +0300 | [diff] [blame] | 24 | { |
| 25 | public: |
| 26 | static void apply(TIntermNode *root); |
| 27 | private: |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 28 | SeparateDeclarationsTraverser(); |
Olli Etuaho | fc0e2bc | 2015-04-16 13:39:56 +0300 | [diff] [blame] | 29 | bool visitAggregate(Visit, TIntermAggregate *node) override; |
| 30 | }; |
| 31 | |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 32 | void SeparateDeclarationsTraverser::apply(TIntermNode *root) |
Olli Etuaho | fc0e2bc | 2015-04-16 13:39:56 +0300 | [diff] [blame] | 33 | { |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 34 | SeparateDeclarationsTraverser separateDecl; |
Olli Etuaho | fc0e2bc | 2015-04-16 13:39:56 +0300 | [diff] [blame] | 35 | root->traverse(&separateDecl); |
| 36 | separateDecl.updateTree(); |
| 37 | } |
| 38 | |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 39 | SeparateDeclarationsTraverser::SeparateDeclarationsTraverser() |
Olli Etuaho | fc0e2bc | 2015-04-16 13:39:56 +0300 | [diff] [blame] | 40 | : TIntermTraverser(true, false, false) |
| 41 | { |
| 42 | } |
| 43 | |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 44 | bool SeparateDeclarationsTraverser::visitAggregate(Visit, TIntermAggregate *node) |
Olli Etuaho | fc0e2bc | 2015-04-16 13:39:56 +0300 | [diff] [blame] | 45 | { |
| 46 | if (node->getOp() == EOpDeclaration) |
| 47 | { |
| 48 | TIntermSequence *sequence = node->getSequence(); |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 49 | if (sequence->size() > 1) |
Olli Etuaho | fc0e2bc | 2015-04-16 13:39:56 +0300 | [diff] [blame] | 50 | { |
| 51 | TIntermAggregate *parentAgg = getParentNode()->getAsAggregate(); |
| 52 | ASSERT(parentAgg != nullptr); |
| 53 | |
| 54 | TIntermSequence replacementDeclarations; |
| 55 | for (size_t ii = 0; ii < sequence->size(); ++ii) |
| 56 | { |
| 57 | TIntermAggregate *replacementDeclaration = new TIntermAggregate; |
| 58 | |
| 59 | replacementDeclaration->setOp(EOpDeclaration); |
| 60 | replacementDeclaration->getSequence()->push_back(sequence->at(ii)); |
| 61 | replacementDeclaration->setLine(sequence->at(ii)->getLine()); |
| 62 | replacementDeclarations.push_back(replacementDeclaration); |
| 63 | } |
| 64 | |
| 65 | mMultiReplacements.push_back(NodeReplaceWithMultipleEntry(parentAgg, node, replacementDeclarations)); |
| 66 | } |
| 67 | return false; |
| 68 | } |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | } // namespace |
| 73 | |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 74 | void SeparateDeclarations(TIntermNode *root) |
Olli Etuaho | fc0e2bc | 2015-04-16 13:39:56 +0300 | [diff] [blame] | 75 | { |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 76 | SeparateDeclarationsTraverser::apply(root); |
Olli Etuaho | fc0e2bc | 2015-04-16 13:39:56 +0300 | [diff] [blame] | 77 | } |