blob: 76a94db4179ccf2263dd27aee88aae5a7cc0d269 [file] [log] [blame]
Than McIntoshb8518b32018-12-14 08:16:25 -05001//===-- namegen.cpp -------------------------------------------------------===//
2//
3// Copyright 2018 The Go Authors. All rights reserved.
4// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file.
6//
7//===----------------------------------------------------------------------===//
8
9// Implementation of NameGen helper class.
10
11#include "namegen.h"
12
13 // For creating useful type, inst and block names.
14std::string NameGen::namegen(const std::string &tag,
15 unsigned expl)
16{
17 auto it = nametags_.find(tag);
18 unsigned count = 0;
19 if (it != nametags_.end())
20 count = it->second + 1;
21 if (expl != ChooseVer)
22 count = expl;
23 std::stringstream ss;
24 ss << tag << "." << count;
25 if (expl == ChooseVer)
26 nametags_[tag] = count;
27 return ss.str();
28}
29
30static std::string chopNumericSuffix(const std::string &tag)
31{
32 std::string res;
33 auto dot = tag.rfind('.');
34 if (dot == std::string::npos || dot == tag.size()-1)
35 return tag;
36 dot += 1;
37 if (!std::isdigit(tag.at(dot)))
38 return tag;
39 auto found = tag.find_first_not_of("0123456789", dot);
40 if (found != std::string::npos)
41 return tag;
42 return tag.substr(0, dot-1);
43}
44
45std::string NameGen::combineTags(const std::string &baseTag,
46 const std::string &suffix)
47{
48 std::string res(chopNumericSuffix(baseTag));
49 res += suffix;
50 return res;
51}