1//
2// Copyright (c) 2002-2014 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
7#include "compiler/translator/tree_ops/RegenerateStructNames.h"
8
9#include "common/debug.h"
10#include "compiler/translator/ImmutableStringBuilder.h"
11
12namespace sh
13{
14
15namespace
16{
17constexpr const ImmutableString kPrefix("_webgl_struct_");
18} // anonymous namespace
19
20void RegenerateStructNames::visitSymbol(TIntermSymbol *symbol)
21{
22 ASSERT(symbol);
23 const TType &type = symbol->getType();
24 const TStructure *userType = type.getStruct();
25 if (!userType)
26 return;
27
28 if (userType->symbolType() == SymbolType::BuiltIn ||
29 userType->symbolType() == SymbolType::Empty)
30 {
31 // Built-in struct or nameless struct, do not touch it.
32 return;
33 }
34
35 int uniqueId = userType->uniqueId().get();
36
37 ASSERT(mScopeDepth > 0);
38 if (mScopeDepth == 1)
39 {
40 // If a struct is defined at global scope, we don't map its name.
41 // This is because at global level, the struct might be used to
42 // declare a uniform, so the same name needs to stay the same for
43 // vertex/fragment shaders. However, our mapping uses internal ID,
44 // which will be different for the same struct in vertex/fragment
45 // shaders.
46 // This is OK because names for any structs defined in other scopes
47 // will begin with "_webgl", which is reserved. So there will be
48 // no conflicts among unmapped struct names from global scope and
49 // mapped struct names from other scopes.
50 // However, we need to keep track of these global structs, so if a
51 // variable is used in a local scope, we don't try to modify the
52 // struct name through that variable.
53 mDeclaredGlobalStructs.insert(uniqueId);
54 return;
55 }
56 if (mDeclaredGlobalStructs.count(uniqueId) > 0)
57 return;
58 // Map {name} to _webgl_struct_{uniqueId}_{name}.
59 if (userType->name().beginsWith(kPrefix))
60 {
61 // The name has already been regenerated.
62 return;
63 }
64 ImmutableStringBuilder tmp(kPrefix.length() + sizeof(uniqueId) * 2u + 1u +
65 userType->name().length());
66 tmp << kPrefix;
67 tmp.appendHex(uniqueId);
68 tmp << '_' << userType->name();
69
70 // TODO(oetuaho): Add another mechanism to change symbol names so that the const_cast is not
71 // needed.
72 const_cast<TStructure *>(userType)->setName(tmp);
73}
74
75bool RegenerateStructNames::visitBlock(Visit, TIntermBlock *block)
76{
77 ++mScopeDepth;
78 TIntermSequence &sequence = *(block->getSequence());
79 for (TIntermNode *node : sequence)
80 {
81 node->traverse(this);
82 }
83 --mScopeDepth;
84 return false;
85}
86
87} // namespace sh
88