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 | #ifndef SYMTAB_H_ |
| 16 | #define SYMTAB_H_ |
| 17 | |
| 18 | #include <string> |
| 19 | #include <vector> |
| 20 | |
| 21 | #include "string_piece.h" |
| 22 | |
| 23 | using namespace std; |
| 24 | |
Shinichiro Hamaji | ba2ccdb | 2015-07-17 05:55:42 +0900 | [diff] [blame] | 25 | extern vector<string*>* g_symbols; |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 26 | |
| 27 | class Symtab; |
| 28 | |
| 29 | class Symbol { |
| 30 | public: |
Shinichiro Hamaji | 94d6f2a | 2015-07-05 05:32:25 +0900 | [diff] [blame] | 31 | struct IsUninitialized {}; |
| 32 | explicit Symbol(IsUninitialized) |
| 33 | : v_(-1) { |
| 34 | } |
| 35 | |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 36 | const string& str() const { |
Shinichiro Hamaji | ba2ccdb | 2015-07-17 05:55:42 +0900 | [diff] [blame] | 37 | return *((*g_symbols)[v_]); |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | const char* c_str() const { |
| 41 | return str().c_str(); |
| 42 | } |
| 43 | |
| 44 | bool empty() const { return !v_; } |
| 45 | |
| 46 | int val() const { return v_; } |
| 47 | |
| 48 | char get(size_t i) const { |
| 49 | const string& s = str(); |
| 50 | if (i >= s.size()) |
| 51 | return 0; |
| 52 | return s[i]; |
| 53 | } |
| 54 | |
Shinichiro Hamaji | a7984ad | 2015-09-11 16:33:16 +0900 | [diff] [blame] | 55 | bool IsValid() const { return v_ >= 0; } |
| 56 | |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 57 | private: |
| 58 | explicit Symbol(int v); |
| 59 | |
| 60 | int v_; |
| 61 | |
| 62 | friend class Symtab; |
| 63 | }; |
| 64 | |
| 65 | inline bool operator==(const Symbol& x, const Symbol& y) { |
| 66 | return x.val() == y.val(); |
| 67 | } |
| 68 | |
Dan Willemsen | b248caa | 2015-10-01 16:07:48 -0700 | [diff] [blame] | 69 | inline bool operator<(const Symbol& x, const Symbol& y) { |
Shinichiro Hamaji | 675ecf3 | 2015-10-03 10:57:31 +0900 | [diff] [blame] | 70 | return x.val() < y.val(); |
Dan Willemsen | b248caa | 2015-10-01 16:07:48 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 73 | namespace std { |
| 74 | template<> struct hash<Symbol> { |
| 75 | size_t operator()(const Symbol& s) const { |
| 76 | return s.val(); |
| 77 | } |
| 78 | }; |
| 79 | } |
| 80 | |
Shinichiro Hamaji | 43defe0 | 2015-07-11 07:06:43 +0900 | [diff] [blame] | 81 | extern Symbol kEmptySym; |
Shinichiro Hamaji | 94d6f2a | 2015-07-05 05:32:25 +0900 | [diff] [blame] | 82 | extern Symbol kShellSym; |
| 83 | |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 84 | void InitSymtab(); |
| 85 | void QuitSymtab(); |
| 86 | Symbol Intern(StringPiece s); |
| 87 | |
| 88 | string JoinSymbols(const vector<Symbol>& syms, const char* sep); |
| 89 | |
| 90 | #endif // SYMTAB_H_ |