]>
git.proxmox.com Git - rustc.git/blob - src/binaryen/src/support/name.h
2 * Copyright 2017 WebAssembly Community Group participants
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef wasm_support_string_h
18 #define wasm_support_string_h
22 #include "emscripten-optimizer/istring.h"
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.
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) {}
42 friend std::ostream
& operator<<(std::ostream
& o
, Name name
) {
44 return o
<< '$' << name
.str
; // reference interpreter requires we prefix all names
47 static Name
fromInt(size_t i
) {
48 return cashew::IString(std::to_string(i
).c_str(), false);
51 bool hasSubstring(cashew::IString substring
) {
52 return strstr(c_str(), substring
.c_str()) != nullptr;
60 template <> struct hash
<wasm::Name
> : hash
<cashew::IString
> {};
65 #endif // wasm_support_string_h