Olli Etuaho | c683311 | 2015-04-22 15:15:54 +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 | // |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 6 | // The PruneEmptyDeclarations function prunes unnecessary empty declarations and declarators from |
| 7 | // the AST. |
Olli Etuaho | c683311 | 2015-04-22 15:15:54 +0300 | [diff] [blame] | 8 | |
| 9 | #include "compiler/translator/PruneEmptyDeclarations.h" |
| 10 | |
Olli Etuaho | cccf2b0 | 2017-07-05 14:50:54 +0300 | [diff] [blame] | 11 | #include "compiler/translator/IntermTraverse.h" |
Olli Etuaho | c683311 | 2015-04-22 15:15:54 +0300 | [diff] [blame] | 12 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 13 | namespace sh |
| 14 | { |
| 15 | |
Olli Etuaho | c683311 | 2015-04-22 15:15:54 +0300 | [diff] [blame] | 16 | namespace |
| 17 | { |
| 18 | |
| 19 | class PruneEmptyDeclarationsTraverser : private TIntermTraverser |
| 20 | { |
| 21 | public: |
Olli Etuaho | 923ecef | 2017-10-11 12:01:38 +0300 | [diff] [blame^] | 22 | static void apply(TIntermBlock *root); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 23 | |
Olli Etuaho | c683311 | 2015-04-22 15:15:54 +0300 | [diff] [blame] | 24 | private: |
| 25 | PruneEmptyDeclarationsTraverser(); |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 26 | bool visitDeclaration(Visit, TIntermDeclaration *node) override; |
Olli Etuaho | c683311 | 2015-04-22 15:15:54 +0300 | [diff] [blame] | 27 | }; |
| 28 | |
Olli Etuaho | 923ecef | 2017-10-11 12:01:38 +0300 | [diff] [blame^] | 29 | void PruneEmptyDeclarationsTraverser::apply(TIntermBlock *root) |
Olli Etuaho | c683311 | 2015-04-22 15:15:54 +0300 | [diff] [blame] | 30 | { |
| 31 | PruneEmptyDeclarationsTraverser prune; |
| 32 | root->traverse(&prune); |
| 33 | prune.updateTree(); |
| 34 | } |
| 35 | |
| 36 | PruneEmptyDeclarationsTraverser::PruneEmptyDeclarationsTraverser() |
| 37 | : TIntermTraverser(true, false, false) |
| 38 | { |
| 39 | } |
| 40 | |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 41 | bool PruneEmptyDeclarationsTraverser::visitDeclaration(Visit, TIntermDeclaration *node) |
Olli Etuaho | c683311 | 2015-04-22 15:15:54 +0300 | [diff] [blame] | 42 | { |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 43 | TIntermSequence *sequence = node->getSequence(); |
| 44 | if (sequence->size() >= 1) |
Olli Etuaho | c683311 | 2015-04-22 15:15:54 +0300 | [diff] [blame] | 45 | { |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 46 | 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 Etuaho | c683311 | 2015-04-22 15:15:54 +0300 | [diff] [blame] | 49 | { |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 50 | if (sequence->size() > 1) |
Olli Etuaho | c683311 | 2015-04-22 15:15:54 +0300 | [diff] [blame] | 51 | { |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 52 | // 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 |
Olli Etuaho | 923ecef | 2017-10-11 12:01:38 +0300 | [diff] [blame^] | 65 | // they should not be pruned. If there are entirely empty non-struct declarations, |
| 66 | // they result in TIntermDeclaration nodes without any children in the parsing |
| 67 | // stage. This will be handled further down in the code. |
| 68 | UNREACHABLE(); |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 69 | } |
| 70 | else if (sym->getType().getQualifier() != EvqGlobal && |
| 71 | sym->getType().getQualifier() != EvqTemporary) |
| 72 | { |
| 73 | // We've hit an empty struct declaration with a qualifier, for example like |
| 74 | // this: |
| 75 | // const struct a { int i; }; |
| 76 | // NVIDIA GL driver version 367.27 doesn't accept this kind of declarations, so |
| 77 | // we convert the declaration to a regular struct declaration. This is okay, |
| 78 | // since ESSL 1.00 spec section 4.1.8 says about structs that "The optional |
| 79 | // qualifiers only apply to any declarators, and are not part of the type being |
| 80 | // defined for name." |
Olli Etuaho | 474a08c0 | 2016-06-28 10:49:46 +0300 | [diff] [blame] | 81 | |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 82 | if (mInGlobalScope) |
| 83 | { |
| 84 | sym->getTypePointer()->setQualifier(EvqGlobal); |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | sym->getTypePointer()->setQualifier(EvqTemporary); |
Olli Etuaho | 474a08c0 | 2016-06-28 10:49:46 +0300 | [diff] [blame] | 89 | } |
Olli Etuaho | c683311 | 2015-04-22 15:15:54 +0300 | [diff] [blame] | 90 | } |
| 91 | } |
Olli Etuaho | c683311 | 2015-04-22 15:15:54 +0300 | [diff] [blame] | 92 | } |
Olli Etuaho | 923ecef | 2017-10-11 12:01:38 +0300 | [diff] [blame^] | 93 | else |
| 94 | { |
| 95 | // We have a declaration with no declarators. |
| 96 | // The declaration may be either inside a block or in a loop init expression. |
| 97 | TIntermBlock *parentAsBlock = getParentNode()->getAsBlock(); |
| 98 | ASSERT(parentAsBlock != nullptr || getParentNode()->getAsLoopNode() != nullptr); |
| 99 | if (parentAsBlock) |
| 100 | { |
| 101 | TIntermSequence emptyReplacement; |
| 102 | mMultiReplacements.push_back( |
| 103 | NodeReplaceWithMultipleEntry(parentAsBlock, node, emptyReplacement)); |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | queueReplacement(nullptr, OriginalNode::IS_DROPPED); |
| 108 | } |
| 109 | } |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 110 | return false; |
Olli Etuaho | c683311 | 2015-04-22 15:15:54 +0300 | [diff] [blame] | 111 | } |
| 112 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 113 | } // namespace |
Olli Etuaho | c683311 | 2015-04-22 15:15:54 +0300 | [diff] [blame] | 114 | |
Olli Etuaho | 923ecef | 2017-10-11 12:01:38 +0300 | [diff] [blame^] | 115 | void PruneEmptyDeclarations(TIntermBlock *root) |
Olli Etuaho | c683311 | 2015-04-22 15:15:54 +0300 | [diff] [blame] | 116 | { |
| 117 | PruneEmptyDeclarationsTraverser::apply(root); |
| 118 | } |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 119 | |
| 120 | } // namespace sh |