blob: 0b91f8cba3e5f3841009ffda8449be4815f9ef01 [file] [log] [blame]
zmo@google.comfd747b82011-04-23 01:30:07 +00001//
2// Copyright (c) 2002-2011 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
9namespace {
10
zmo@google.com24c08c42011-05-27 17:40:48 +000011TString mapLongName(int id, const TString& name, bool isVarying)
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_";
16 if (isVarying)
17 stream << "v";
18 stream << id << "_";
kbr@chromium.org22152112011-10-26 01:18:28 +000019 stream << name.substr(0, MAX_SHORTENED_IDENTIFIER_SIZE - stream.str().size());
zmo@google.comfd747b82011-04-23 01:30:07 +000020 return stream.str();
21}
22
23} // anonymous namespace
24
zmo@google.com24c08c42011-05-27 17:40:48 +000025MapLongVariableNames::MapLongVariableNames(
zmo@google.com46974d22011-09-27 21:26:19 +000026 std::map<std::string, std::string>& varyingLongNameMap)
zmo@google.com24c08c42011-05-27 17:40:48 +000027 : mVaryingLongNameMap(varyingLongNameMap)
28{
29}
30
zmo@google.comfd747b82011-04-23 01:30:07 +000031void MapLongVariableNames::visitSymbol(TIntermSymbol* symbol)
32{
33 ASSERT(symbol != NULL);
kbr@chromium.org22152112011-10-26 01:18:28 +000034 if (symbol->getSymbol().size() > MAX_SHORTENED_IDENTIFIER_SIZE) {
zmo@google.com24c08c42011-05-27 17:40:48 +000035 switch (symbol->getQualifier()) {
36 case EvqVaryingIn:
37 case EvqVaryingOut:
38 case EvqInvariantVaryingIn:
39 case EvqInvariantVaryingOut:
40 symbol->setSymbol(
41 mapVaryingLongName(symbol->getSymbol()));
42 break;
43 default:
44 symbol->setSymbol(
45 mapLongName(symbol->getId(), symbol->getSymbol(), false));
46 break;
47 };
48 }
zmo@google.comfd747b82011-04-23 01:30:07 +000049}
50
zmo@google.com216aa5e2011-06-17 22:31:32 +000051bool MapLongVariableNames::visitLoop(Visit, TIntermLoop* node)
zmo@google.comfd747b82011-04-23 01:30:07 +000052{
zmo@google.com216aa5e2011-06-17 22:31:32 +000053 if (node->getInit())
54 node->getInit()->traverse(this);
zmo@google.comfd747b82011-04-23 01:30:07 +000055 return true;
56}
57
zmo@google.com24c08c42011-05-27 17:40:48 +000058TString MapLongVariableNames::mapVaryingLongName(const TString& name)
59{
zmo@google.com46974d22011-09-27 21:26:19 +000060 std::map<std::string, std::string>::const_iterator it = mVaryingLongNameMap.find(name.c_str());
zmo@google.com24c08c42011-05-27 17:40:48 +000061 if (it != mVaryingLongNameMap.end())
zmo@google.com46974d22011-09-27 21:26:19 +000062 return (*it).second.c_str();
zmo@google.com24c08c42011-05-27 17:40:48 +000063
64 int id = mVaryingLongNameMap.size();
65 TString mappedName = mapLongName(id, name, true);
66 mVaryingLongNameMap.insert(
zmo@google.com46974d22011-09-27 21:26:19 +000067 std::map<std::string, std::string>::value_type(name.c_str(), mappedName.c_str()));
zmo@google.com24c08c42011-05-27 17:40:48 +000068 return mappedName;
69}