summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/angle/src/compiler/translator/MapLongVariableNames.cpp
blob: ef629c26b1a464580c8e2b3de80d7244c0d5329c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//
// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//

#include "compiler/translator/MapLongVariableNames.h"

namespace {

TString mapLongName(size_t id, const TString& name, bool isGlobal)
{
    ASSERT(name.size() > MAX_SHORTENED_IDENTIFIER_SIZE);
    TStringStream stream;
    stream << "webgl_";
    if (isGlobal)
        stream << "g";
    stream << id;
    if (name[0] != '_')
        stream << "_";
    stream << name.substr(0, MAX_SHORTENED_IDENTIFIER_SIZE - stream.str().size());
    return stream.str();
}

LongNameMap* gLongNameMapInstance = NULL;

}  // anonymous namespace

LongNameMap::LongNameMap()
    : refCount(0)
{
}

LongNameMap::~LongNameMap()
{
}

// static
LongNameMap* LongNameMap::GetInstance()
{
    if (gLongNameMapInstance == NULL)
        gLongNameMapInstance = new LongNameMap;
    gLongNameMapInstance->refCount++;
    return gLongNameMapInstance;
}

void LongNameMap::Release()
{
    ASSERT(gLongNameMapInstance == this);
    ASSERT(refCount > 0);
    refCount--;
    if (refCount == 0) {
        delete gLongNameMapInstance;
        gLongNameMapInstance = NULL;
    }
}

const char* LongNameMap::Find(const char* originalName) const
{
    std::map<std::string, std::string>::const_iterator it = mLongNameMap.find(
        originalName);
    if (it != mLongNameMap.end())
        return (*it).second.c_str();
    return NULL;
}

void LongNameMap::Insert(const char* originalName, const char* mappedName)
{
    mLongNameMap.insert(std::map<std::string, std::string>::value_type(
        originalName, mappedName));
}

size_t LongNameMap::Size() const
{
    return mLongNameMap.size();
}

MapLongVariableNames::MapLongVariableNames(LongNameMap* globalMap)
{
    ASSERT(globalMap);
    mGlobalMap = globalMap;
}

void MapLongVariableNames::visitSymbol(TIntermSymbol* symbol)
{
    ASSERT(symbol != NULL);
    if (symbol->getSymbol().size() > MAX_SHORTENED_IDENTIFIER_SIZE) {
        switch (symbol->getQualifier()) {
          case EvqVaryingIn:
          case EvqVaryingOut:
          case EvqInvariantVaryingIn:
          case EvqInvariantVaryingOut:
          case EvqUniform:
            symbol->setSymbol(
                mapGlobalLongName(symbol->getSymbol()));
            break;
          default:
            symbol->setSymbol(
                mapLongName(symbol->getId(), symbol->getSymbol(), false));
            break;
        };
    }
}

TString MapLongVariableNames::mapGlobalLongName(const TString& name)
{
    ASSERT(mGlobalMap);
    const char* mappedName = mGlobalMap->Find(name.c_str());
    if (mappedName != NULL)
        return mappedName;
    size_t id = mGlobalMap->Size();
    TString rt = mapLongName(id, name, true);
    mGlobalMap->Insert(name.c_str(), rt.c_str());
    return rt;
}