source: icGREP/icgrep-devel/icgrep/pablo/symbol_generator.cpp @ 5202

Last change on this file since 5202 was 5202, checked in by nmedfort, 2 years ago

Initial work on adding types to PabloAST and mutable Var objects.

File size: 1.5 KB
Line 
1/*
2 *  Copyright (c) 2014 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 *  icgrep is a trademark of International Characters.
5 */
6
7#include <pablo/symbol_generator.h>
8#include <pablo/pe_string.h>
9#include <pablo/pe_integer.h>
10
11namespace pablo {
12
13String * SymbolGenerator::get(const std::string name) {
14    if (LLVM_UNLIKELY(name.length() == 0)) {
15        throw std::runtime_error("symbol name cannot be 0-length");
16    }
17    auto f = mStringMap.find(name);
18    String * result = nullptr;
19    if (f == mStringMap.end()) {
20        result = new String(name);
21        assert (result);
22        mStringMap.insert(std::make_pair(std::move(name), result));
23    }
24    else {
25        result = f->second;
26    }
27    return result;
28}
29
30Integer * SymbolGenerator::getInteger(const integer_t value) {
31    auto f = mIntegerMap.find(value);
32    Integer * result;
33    if (f == mIntegerMap.end()) {
34        result = new Integer(value);
35        assert (result->value() == value);
36        mIntegerMap.insert(std::make_pair(value, result));
37    } else {
38        result = f->second;
39    }
40    return result;
41}
42
43String * SymbolGenerator::make(const std::string prefix) {
44    auto f = mPrefixMap.find(prefix);
45    if (f == mPrefixMap.end()) {
46        mPrefixMap.insert(std::make_pair(prefix, 1));
47        return get(prefix);
48    } else {
49        const unsigned count = f->second++;
50        return get(prefix + '_' + std::to_string(count));
51    }
52}
53
54}
Note: See TracBrowser for help on using the repository browser.