blob: fd0eb41f3bca8dfc50f2af51b3fd4ab10a98bff0 [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//
Jamie Madilld7b1ab52016-12-12 14:42:19 -05006// The PruneEmptyDeclarations function prunes unnecessary empty declarations and declarators from
7// the AST.
Olli Etuahoc6833112015-04-22 15:15:54 +03008
9#include "compiler/translator/PruneEmptyDeclarations.h"
10
11#include "compiler/translator/IntermNode.h"
12
Jamie Madill45bcc782016-11-07 13:58:48 -050013namespace sh
14{
15
Olli Etuahoc6833112015-04-22 15:15:54 +030016namespace
17{
18
19class PruneEmptyDeclarationsTraverser : private TIntermTraverser
20{
21 public:
22 static void apply(TIntermNode *root);
Jamie Madilld7b1ab52016-12-12 14:42:19 -050023
Olli Etuahoc6833112015-04-22 15:15:54 +030024 private:
25 PruneEmptyDeclarationsTraverser();
Olli Etuaho13389b62016-10-16 11:48:18 +010026 bool visitDeclaration(Visit, TIntermDeclaration *node) override;
Olli Etuahoc6833112015-04-22 15:15:54 +030027};
28
29void PruneEmptyDeclarationsTraverser::apply(TIntermNode *root)
30{
31 PruneEmptyDeclarationsTraverser prune;
32 root->traverse(&prune);
33 prune.updateTree();
34}
35
36PruneEmptyDeclarationsTraverser::PruneEmptyDeclarationsTraverser()
37 : TIntermTraverser(true, false, false)
38{
39}
40
Olli Etuaho13389b62016-10-16 11:48:18 +010041bool PruneEmptyDeclarationsTraverser::visitDeclaration(Visit, TIntermDeclaration *node)
Olli Etuahoc6833112015-04-22 15:15:54 +030042{
Olli Etuaho13389b62016-10-16 11:48:18 +010043 TIntermSequence *sequence = node->getSequence();
44 if (sequence->size() >= 1)
Olli Etuahoc6833112015-04-22 15:15:54 +030045 {
Olli Etuaho13389b62016-10-16 11:48:18 +010046 TIntermSymbol *sym = sequence->front()->getAsSymbolNode();
47 // Prune declarations without a variable name, unless it's an interface block declaration.
48 if (sym != nullptr && sym->getSymbol() == "" && !sym->isInterfaceBlock())
Olli Etuahoc6833112015-04-22 15:15:54 +030049 {
Olli Etuaho13389b62016-10-16 11:48:18 +010050 if (sequence->size() > 1)
Olli Etuahoc6833112015-04-22 15:15:54 +030051 {
Olli Etuaho13389b62016-10-16 11:48:18 +010052 // Generate a replacement that will remove the empty declarator in the beginning of
53 // a declarator list. Example of a declaration that will be changed:
54 // float, a;
55 // will be changed to
56 // float a;
57 // This applies also to struct declarations.
58 TIntermSequence emptyReplacement;
59 mMultiReplacements.push_back(
60 NodeReplaceWithMultipleEntry(node, sym, emptyReplacement));
61 }
62 else if (sym->getBasicType() != EbtStruct)
63 {
64 // Single struct declarations may just declare the struct type and no variables, so
65 // they should not be pruned. All other single empty declarations can be pruned
66 // entirely. Example of an empty declaration that will be pruned:
67 // float;
68 TIntermSequence emptyReplacement;
69 TIntermBlock *parentAsBlock = getParentNode()->getAsBlock();
70 // The declaration may be inside a block or in a loop init expression.
71 ASSERT(parentAsBlock != nullptr || getParentNode()->getAsLoopNode() != nullptr);
72 if (parentAsBlock)
Olli Etuahoc6833112015-04-22 15:15:54 +030073 {
Olli Etuaho13389b62016-10-16 11:48:18 +010074 mMultiReplacements.push_back(
75 NodeReplaceWithMultipleEntry(parentAsBlock, node, emptyReplacement));
Olli Etuahoc6833112015-04-22 15:15:54 +030076 }
Olli Etuaho13389b62016-10-16 11:48:18 +010077 else
Olli Etuahoc6833112015-04-22 15:15:54 +030078 {
Olli Etuaho13389b62016-10-16 11:48:18 +010079 queueReplacement(node, nullptr, OriginalNode::IS_DROPPED);
Olli Etuahoc6833112015-04-22 15:15:54 +030080 }
Olli Etuaho13389b62016-10-16 11:48:18 +010081 }
82 else if (sym->getType().getQualifier() != EvqGlobal &&
83 sym->getType().getQualifier() != EvqTemporary)
84 {
85 // We've hit an empty struct declaration with a qualifier, for example like
86 // this:
87 // const struct a { int i; };
88 // NVIDIA GL driver version 367.27 doesn't accept this kind of declarations, so
89 // we convert the declaration to a regular struct declaration. This is okay,
90 // since ESSL 1.00 spec section 4.1.8 says about structs that "The optional
91 // qualifiers only apply to any declarators, and are not part of the type being
92 // defined for name."
Olli Etuaho474a08c02016-06-28 10:49:46 +030093
Olli Etuaho13389b62016-10-16 11:48:18 +010094 if (mInGlobalScope)
95 {
96 sym->getTypePointer()->setQualifier(EvqGlobal);
97 }
98 else
99 {
100 sym->getTypePointer()->setQualifier(EvqTemporary);
Olli Etuaho474a08c02016-06-28 10:49:46 +0300101 }
Olli Etuahoc6833112015-04-22 15:15:54 +0300102 }
103 }
Olli Etuahoc6833112015-04-22 15:15:54 +0300104 }
Olli Etuaho13389b62016-10-16 11:48:18 +0100105 return false;
Olli Etuahoc6833112015-04-22 15:15:54 +0300106}
107
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500108} // namespace
Olli Etuahoc6833112015-04-22 15:15:54 +0300109
110void PruneEmptyDeclarations(TIntermNode *root)
111{
112 PruneEmptyDeclarationsTraverser::apply(root);
113}
Jamie Madill45bcc782016-11-07 13:58:48 -0500114
115} // namespace sh