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