Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All rights reserved |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | // +build ignore |
| 16 | |
| 17 | #include "symtab.h" |
| 18 | |
| 19 | #include <string.h> |
| 20 | |
| 21 | #include <unordered_map> |
| 22 | |
| 23 | #include "log.h" |
| 24 | #include "strutil.h" |
| 25 | |
Shinichiro Hamaji | ba2ccdb | 2015-07-17 05:55:42 +0900 | [diff] [blame] | 26 | vector<string*>* g_symbols; |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 27 | |
Shinichiro Hamaji | 43defe0 | 2015-07-11 07:06:43 +0900 | [diff] [blame] | 28 | Symbol kEmptySym = Symbol(Symbol::IsUninitialized()); |
Shinichiro Hamaji | 94d6f2a | 2015-07-05 05:32:25 +0900 | [diff] [blame] | 29 | Symbol kShellSym = Symbol(Symbol::IsUninitialized()); |
| 30 | |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 31 | Symbol::Symbol(int v) |
| 32 | : v_(v) { |
| 33 | } |
| 34 | |
| 35 | class Symtab { |
| 36 | public: |
| 37 | Symtab() { |
| 38 | CHECK(g_symbols == NULL); |
| 39 | g_symbols = &symbols_; |
Shinichiro Hamaji | 82aeb27 | 2015-06-30 02:24:36 +0900 | [diff] [blame] | 40 | |
| 41 | Symbol s = InternImpl(""); |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 42 | CHECK(s.v_ == 0); |
| 43 | CHECK(Intern("") == s); |
Shinichiro Hamaji | 82aeb27 | 2015-06-30 02:24:36 +0900 | [diff] [blame] | 44 | char b[2]; |
| 45 | b[1] = 0; |
| 46 | for (int i = 1; i < 256; i++) { |
| 47 | b[0] = i; |
| 48 | s = InternImpl(b); |
| 49 | CHECK(s.val() == i); |
| 50 | } |
Shinichiro Hamaji | 94d6f2a | 2015-07-05 05:32:25 +0900 | [diff] [blame] | 51 | |
Shinichiro Hamaji | 43defe0 | 2015-07-11 07:06:43 +0900 | [diff] [blame] | 52 | kEmptySym = Intern(""); |
Shinichiro Hamaji | 94d6f2a | 2015-07-05 05:32:25 +0900 | [diff] [blame] | 53 | kShellSym = Intern("SHELL"); |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 54 | } |
| 55 | |
Shinichiro Hamaji | ba2ccdb | 2015-07-17 05:55:42 +0900 | [diff] [blame] | 56 | ~Symtab() { |
Shinichiro Hamaji | 9f127b2 | 2015-10-28 18:08:47 +0900 | [diff] [blame] | 57 | LOG_STAT("%zu symbols", symbols_.size()); |
Shinichiro Hamaji | ba2ccdb | 2015-07-17 05:55:42 +0900 | [diff] [blame] | 58 | for (string* s : symbols_) |
| 59 | delete s; |
| 60 | } |
| 61 | |
Shinichiro Hamaji | 82aeb27 | 2015-06-30 02:24:36 +0900 | [diff] [blame] | 62 | Symbol InternImpl(StringPiece s) { |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 63 | auto found = symtab_.find(s); |
| 64 | if (found != symtab_.end()) { |
| 65 | return found->second; |
| 66 | } |
Shinichiro Hamaji | ba2ccdb | 2015-07-17 05:55:42 +0900 | [diff] [blame] | 67 | symbols_.push_back(new string(s.data(), s.size())); |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 68 | Symbol sym = Symbol(symtab_.size()); |
Shinichiro Hamaji | ba2ccdb | 2015-07-17 05:55:42 +0900 | [diff] [blame] | 69 | bool ok = symtab_.emplace(*symbols_.back(), sym).second; |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 70 | CHECK(ok); |
| 71 | return sym; |
| 72 | } |
| 73 | |
Shinichiro Hamaji | 82aeb27 | 2015-06-30 02:24:36 +0900 | [diff] [blame] | 74 | Symbol Intern(StringPiece s) { |
| 75 | if (s.size() <= 1) { |
| 76 | return Symbol(s.empty() ? 0 : (unsigned char)s[0]); |
| 77 | } |
| 78 | return InternImpl(s); |
| 79 | } |
| 80 | |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 81 | private: |
| 82 | unordered_map<StringPiece, Symbol> symtab_; |
Shinichiro Hamaji | ba2ccdb | 2015-07-17 05:55:42 +0900 | [diff] [blame] | 83 | vector<string*> symbols_; |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | static Symtab* g_symtab; |
| 87 | |
| 88 | void InitSymtab() { |
| 89 | g_symtab = new Symtab; |
| 90 | } |
| 91 | |
| 92 | void QuitSymtab() { |
| 93 | delete g_symtab; |
| 94 | } |
| 95 | |
| 96 | Symbol Intern(StringPiece s) { |
| 97 | return g_symtab->Intern(s); |
| 98 | } |
| 99 | |
| 100 | string JoinSymbols(const vector<Symbol>& syms, const char* sep) { |
| 101 | vector<string> strs; |
| 102 | for (Symbol s : syms) { |
| 103 | strs.push_back(s.str()); |
| 104 | } |
| 105 | return JoinStrings(strs, sep); |
| 106 | } |