• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (c) 2002-2012 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/MapLongVariableNames.h"
8 
9 namespace {
10 
mapLongName(size_t id,const TString & name,bool isGlobal)11 TString mapLongName(size_t id, const TString& name, bool isGlobal)
12 {
13     ASSERT(name.size() > MAX_SHORTENED_IDENTIFIER_SIZE);
14     TStringStream stream;
15     stream << "webgl_";
16     if (isGlobal)
17         stream << "g";
18     stream << id;
19     if (name[0] != '_')
20         stream << "_";
21     stream << name.substr(0, MAX_SHORTENED_IDENTIFIER_SIZE - stream.str().size());
22     return stream.str();
23 }
24 
25 LongNameMap* gLongNameMapInstance = NULL;
26 
27 }  // anonymous namespace
28 
LongNameMap()29 LongNameMap::LongNameMap()
30     : refCount(0)
31 {
32 }
33 
~LongNameMap()34 LongNameMap::~LongNameMap()
35 {
36 }
37 
38 // static
GetInstance()39 LongNameMap* LongNameMap::GetInstance()
40 {
41     if (gLongNameMapInstance == NULL)
42         gLongNameMapInstance = new LongNameMap;
43     gLongNameMapInstance->refCount++;
44     return gLongNameMapInstance;
45 }
46 
Release()47 void LongNameMap::Release()
48 {
49     ASSERT(gLongNameMapInstance == this);
50     ASSERT(refCount > 0);
51     refCount--;
52     if (refCount == 0) {
53         delete gLongNameMapInstance;
54         gLongNameMapInstance = NULL;
55     }
56 }
57 
Find(const char * originalName) const58 const char* LongNameMap::Find(const char* originalName) const
59 {
60     std::map<std::string, std::string>::const_iterator it = mLongNameMap.find(
61         originalName);
62     if (it != mLongNameMap.end())
63         return (*it).second.c_str();
64     return NULL;
65 }
66 
Insert(const char * originalName,const char * mappedName)67 void LongNameMap::Insert(const char* originalName, const char* mappedName)
68 {
69     mLongNameMap.insert(std::map<std::string, std::string>::value_type(
70         originalName, mappedName));
71 }
72 
Size() const73 size_t LongNameMap::Size() const
74 {
75     return mLongNameMap.size();
76 }
77 
MapLongVariableNames(LongNameMap * globalMap)78 MapLongVariableNames::MapLongVariableNames(LongNameMap* globalMap)
79 {
80     ASSERT(globalMap);
81     mGlobalMap = globalMap;
82 }
83 
visitSymbol(TIntermSymbol * symbol)84 void MapLongVariableNames::visitSymbol(TIntermSymbol* symbol)
85 {
86     ASSERT(symbol != NULL);
87     if (symbol->getSymbol().size() > MAX_SHORTENED_IDENTIFIER_SIZE) {
88         switch (symbol->getQualifier()) {
89           case EvqVaryingIn:
90           case EvqVaryingOut:
91           case EvqInvariantVaryingIn:
92           case EvqInvariantVaryingOut:
93           case EvqUniform:
94             symbol->setSymbol(
95                 mapGlobalLongName(symbol->getSymbol()));
96             break;
97           default:
98             symbol->setSymbol(
99                 mapLongName(symbol->getId(), symbol->getSymbol(), false));
100             break;
101         };
102     }
103 }
104 
mapGlobalLongName(const TString & name)105 TString MapLongVariableNames::mapGlobalLongName(const TString& name)
106 {
107     ASSERT(mGlobalMap);
108     const char* mappedName = mGlobalMap->Find(name.c_str());
109     if (mappedName != NULL)
110         return mappedName;
111     size_t id = mGlobalMap->Size();
112     TString rt = mapLongName(id, name, true);
113     mGlobalMap->Insert(name.c_str(), rt.c_str());
114     return rt;
115 }
116