Than McIntosh | b8518b3 | 2018-12-14 08:16:25 -0500 | [diff] [blame] | 1 | //===-- 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. |
| 14 | std::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 | |
| 30 | static 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 | |
| 45 | std::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 | } |