]> git.proxmox.com Git - rustc.git/blob - src/binaryen/src/support/name.h
New upstream version 1.23.0+dfsg1
[rustc.git] / src / binaryen / src / support / name.h
1 /*
2 * Copyright 2017 WebAssembly Community Group participants
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef wasm_support_string_h
18 #define wasm_support_string_h
19
20 #include <cstring>
21
22 #include "emscripten-optimizer/istring.h"
23
24 namespace wasm {
25
26 // We use a Name for all of the identifiers. These are IStrings, so they are
27 // all interned - comparisons etc are just pointer comparisons, so there is no
28 // perf loss. Having names everywhere makes using the AST much nicer (for
29 // example, block names are strings and not offsets, which makes composition
30 // - adding blocks, removing blocks - easy). One exception is local variables,
31 // where we do use indices, as they are a large proportion of the AST,
32 // perf matters a lot there, and compositionality is not a problem.
33 // TODO: as an optimization, IString values < some threshold could be considered
34 // numerical indices directly.
35
36 struct Name : public cashew::IString {
37 Name() : cashew::IString() {}
38 Name(const char* str) : cashew::IString(str, false) {}
39 Name(cashew::IString str) : cashew::IString(str) {}
40 Name(const std::string& str) : cashew::IString(str.c_str(), false) {}
41
42 friend std::ostream& operator<<(std::ostream& o, Name name) {
43 assert(name.str);
44 return o << '$' << name.str; // reference interpreter requires we prefix all names
45 }
46
47 static Name fromInt(size_t i) {
48 return cashew::IString(std::to_string(i).c_str(), false);
49 }
50
51 bool hasSubstring(cashew::IString substring) {
52 return strstr(c_str(), substring.c_str()) != nullptr;
53 }
54 };
55
56 } // namespace wasm
57
58 namespace std {
59
60 template <> struct hash<wasm::Name> : hash<cashew::IString> {};
61
62 } // namespace std
63
64
65 #endif // wasm_support_string_h