blob: ef62dbfce71594cc2f362fdd22aa08ca290d7527 [file] [log] [blame]
Olli Etuahoc6833112015-04-22 15:15:54 +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//
6// The PruneEmptyDeclarations function prunes unnecessary empty declarations and declarators from the AST.
7
8#include "compiler/translator/PruneEmptyDeclarations.h"
9
10#include "compiler/translator/IntermNode.h"
11
12namespace
13{
14
15class PruneEmptyDeclarationsTraverser : private TIntermTraverser
16{
17 public:
18 static void apply(TIntermNode *root);
19 private:
20 PruneEmptyDeclarationsTraverser();
21 bool visitAggregate(Visit, TIntermAggregate *node) override;
22};
23
24void PruneEmptyDeclarationsTraverser::apply(TIntermNode *root)
25{
26 PruneEmptyDeclarationsTraverser prune;
27 root->traverse(&prune);
28 prune.updateTree();
29}
30
31PruneEmptyDeclarationsTraverser::PruneEmptyDeclarationsTraverser()
32 : TIntermTraverser(true, false, false)
33{
34}
35
36bool PruneEmptyDeclarationsTraverser::visitAggregate(Visit, TIntermAggregate *node)
37{
38 if (node->getOp() == EOpDeclaration)
39 {
40 TIntermSequence *sequence = node->getSequence();
41 if (sequence->size() >= 1)
42 {
43 TIntermSymbol *sym = sequence->front()->getAsSymbolNode();
44 // Prune declarations without a variable name, unless it's an interface block declaration.
45 if (sym != nullptr && sym->getSymbol() == "" && !sym->isInterfaceBlock())
46 {
47 if (sequence->size() > 1)
48 {
49 // Generate a replacement that will remove the empty declarator in the beginning of a declarator
50 // list. Example of a declaration that will be changed:
51 // float, a;
52 // will be changed to
53 // float a;
54 // This applies also to struct declarations.
55 TIntermSequence emptyReplacement;
56 mMultiReplacements.push_back(NodeReplaceWithMultipleEntry(node, sym, emptyReplacement));
57 }
58 else if (sym->getBasicType() != EbtStruct)
59 {
60 // Single struct declarations may just declare the struct type and no variables, so they should
61 // not be pruned. All other single empty declarations can be pruned entirely. Example of an empty
62 // declaration that will be pruned:
63 // float;
64 TIntermSequence emptyReplacement;
65 TIntermAggregate *parentAgg = getParentNode()->getAsAggregate();
66 ASSERT(parentAgg != nullptr);
67 mMultiReplacements.push_back(NodeReplaceWithMultipleEntry(parentAgg, node, emptyReplacement));
68 }
69 }
70 }
71 return false;
72 }
73 return true;
74}
75
76} // namespace
77
78void PruneEmptyDeclarations(TIntermNode *root)
79{
80 PruneEmptyDeclarationsTraverser::apply(root);
81}