blob: b9ec983b5ba048513f96e596ebf2fb17ea8889f2 [file] [log] [blame]
zmo@google.comfd747b82011-04-23 01:30:07 +00001//
zmo@google.com89c98132012-01-06 02:17:03 +00002// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
zmo@google.comfd747b82011-04-23 01:30:07 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Geoff Lang17732822013-08-29 13:46:49 -04007#include "compiler/translator/MapLongVariableNames.h"
zmo@google.comfd747b82011-04-23 01:30:07 +00008
9namespace {
10
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +000011TString mapLongName(size_t id, const TString& name, bool isGlobal)
zmo@google.comfd747b82011-04-23 01:30:07 +000012{
kbr@chromium.org22152112011-10-26 01:18:28 +000013 ASSERT(name.size() > MAX_SHORTENED_IDENTIFIER_SIZE);
zmo@google.comfd747b82011-04-23 01:30:07 +000014 TStringStream stream;
zmo@google.com24c08c42011-05-27 17:40:48 +000015 stream << "webgl_";
zmo@google.comb9f64aa2012-01-20 00:35:15 +000016 if (isGlobal)
17 stream << "g";
zmo@google.com571fe342012-04-17 17:40:29 +000018 stream << id;
19 if (name[0] != '_')
20 stream << "_";
kbr@chromium.org22152112011-10-26 01:18:28 +000021 stream << name.substr(0, MAX_SHORTENED_IDENTIFIER_SIZE - stream.str().size());
zmo@google.comfd747b82011-04-23 01:30:07 +000022 return stream.str();
23}
24
zmo@google.comb9f64aa2012-01-20 00:35:15 +000025LongNameMap* gLongNameMapInstance = NULL;
26
zmo@google.comfd747b82011-04-23 01:30:07 +000027} // anonymous namespace
28
zmo@google.comb9f64aa2012-01-20 00:35:15 +000029LongNameMap::LongNameMap()
30 : refCount(0)
zmo@google.com24c08c42011-05-27 17:40:48 +000031{
32}
33
zmo@google.comb9f64aa2012-01-20 00:35:15 +000034LongNameMap::~LongNameMap()
35{
36}
37
38// static
39LongNameMap* LongNameMap::GetInstance()
40{
41 if (gLongNameMapInstance == NULL)
42 gLongNameMapInstance = new LongNameMap;
43 gLongNameMapInstance->refCount++;
44 return gLongNameMapInstance;
45}
46
47void 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
58const 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
67void 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
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +000073size_t LongNameMap::Size() const
zmo@google.comb9f64aa2012-01-20 00:35:15 +000074{
75 return mLongNameMap.size();
76}
77
78MapLongVariableNames::MapLongVariableNames(LongNameMap* globalMap)
79{
80 ASSERT(globalMap);
81 mGlobalMap = globalMap;
82}
83
zmo@google.comfd747b82011-04-23 01:30:07 +000084void MapLongVariableNames::visitSymbol(TIntermSymbol* symbol)
85{
86 ASSERT(symbol != NULL);
kbr@chromium.org22152112011-10-26 01:18:28 +000087 if (symbol->getSymbol().size() > MAX_SHORTENED_IDENTIFIER_SIZE) {
zmo@google.com24c08c42011-05-27 17:40:48 +000088 switch (symbol->getQualifier()) {
89 case EvqVaryingIn:
90 case EvqVaryingOut:
91 case EvqInvariantVaryingIn:
92 case EvqInvariantVaryingOut:
zmo@google.com89c98132012-01-06 02:17:03 +000093 case EvqUniform:
zmo@google.com24c08c42011-05-27 17:40:48 +000094 symbol->setSymbol(
zmo@google.comb9f64aa2012-01-20 00:35:15 +000095 mapGlobalLongName(symbol->getSymbol()));
zmo@google.com24c08c42011-05-27 17:40:48 +000096 break;
97 default:
98 symbol->setSymbol(
99 mapLongName(symbol->getId(), symbol->getSymbol(), false));
100 break;
101 };
102 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000103}
104
zmo@google.com216aa5e2011-06-17 22:31:32 +0000105bool MapLongVariableNames::visitLoop(Visit, TIntermLoop* node)
zmo@google.comfd747b82011-04-23 01:30:07 +0000106{
zmo@google.com216aa5e2011-06-17 22:31:32 +0000107 if (node->getInit())
108 node->getInit()->traverse(this);
zmo@google.comfd747b82011-04-23 01:30:07 +0000109 return true;
110}
111
zmo@google.comb9f64aa2012-01-20 00:35:15 +0000112TString MapLongVariableNames::mapGlobalLongName(const TString& name)
zmo@google.com24c08c42011-05-27 17:40:48 +0000113{
zmo@google.comb9f64aa2012-01-20 00:35:15 +0000114 ASSERT(mGlobalMap);
115 const char* mappedName = mGlobalMap->Find(name.c_str());
116 if (mappedName != NULL)
117 return mappedName;
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000118 size_t id = mGlobalMap->Size();
zmo@google.comb9f64aa2012-01-20 00:35:15 +0000119 TString rt = mapLongName(id, name, true);
120 mGlobalMap->Insert(name.c_str(), rt.c_str());
121 return rt;
zmo@google.com24c08c42011-05-27 17:40:48 +0000122}