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; |
Shinichiro Hamaji | c9b9e5e | 2016-02-18 18:18:54 +0900 | [diff] [blame] | 28 | class Var; |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 29 | |
| 30 | class Symbol { |
| 31 | public: |
Shinichiro Hamaji | 94d6f2a | 2015-07-05 05:32:25 +0900 | [diff] [blame] | 32 | struct IsUninitialized {}; |
Dan Willemsen | 3ce083f | 2017-10-11 22:17:48 -0700 | [diff] [blame^] | 33 | explicit Symbol(IsUninitialized) : v_(-1) {} |
Shinichiro Hamaji | 94d6f2a | 2015-07-05 05:32:25 +0900 | [diff] [blame] | 34 | |
Dan Willemsen | 3ce083f | 2017-10-11 22:17:48 -0700 | [diff] [blame^] | 35 | const string& str() const { return *((*g_symbols)[v_]); } |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 36 | |
Dan Willemsen | 3ce083f | 2017-10-11 22:17:48 -0700 | [diff] [blame^] | 37 | const char* c_str() const { return str().c_str(); } |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 38 | |
| 39 | bool empty() const { return !v_; } |
| 40 | |
| 41 | int val() const { return v_; } |
| 42 | |
| 43 | char get(size_t i) const { |
| 44 | const string& s = str(); |
| 45 | if (i >= s.size()) |
| 46 | return 0; |
| 47 | return s[i]; |
| 48 | } |
| 49 | |
Shinichiro Hamaji | a7984ad | 2015-09-11 16:33:16 +0900 | [diff] [blame] | 50 | bool IsValid() const { return v_ >= 0; } |
| 51 | |
Shinichiro Hamaji | c9b9e5e | 2016-02-18 18:18:54 +0900 | [diff] [blame] | 52 | Var* GetGlobalVar() const; |
Dan Willemsen | 3ce083f | 2017-10-11 22:17:48 -0700 | [diff] [blame^] | 53 | void SetGlobalVar(Var* v, |
| 54 | bool is_override = false, |
| 55 | bool* readonly = nullptr) const; |
Shinichiro Hamaji | c9b9e5e | 2016-02-18 18:18:54 +0900 | [diff] [blame] | 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 | |
Shinichiro Hamaji | c9b9e5e | 2016-02-18 18:18:54 +0900 | [diff] [blame] | 65 | class ScopedGlobalVar { |
| 66 | public: |
| 67 | ScopedGlobalVar(Symbol name, Var* var); |
| 68 | ~ScopedGlobalVar(); |
| 69 | |
| 70 | private: |
| 71 | Symbol name_; |
| 72 | Var* orig_; |
| 73 | }; |
| 74 | |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 75 | inline bool operator==(const Symbol& x, const Symbol& y) { |
| 76 | return x.val() == y.val(); |
| 77 | } |
| 78 | |
Dan Willemsen | b248caa | 2015-10-01 16:07:48 -0700 | [diff] [blame] | 79 | inline bool operator<(const Symbol& x, const Symbol& y) { |
Shinichiro Hamaji | 675ecf3 | 2015-10-03 10:57:31 +0900 | [diff] [blame] | 80 | return x.val() < y.val(); |
Dan Willemsen | b248caa | 2015-10-01 16:07:48 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 83 | namespace std { |
Dan Willemsen | 3ce083f | 2017-10-11 22:17:48 -0700 | [diff] [blame^] | 84 | template <> |
| 85 | struct hash<Symbol> { |
| 86 | size_t operator()(const Symbol& s) const { return s.val(); } |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 87 | }; |
Dan Willemsen | 3ce083f | 2017-10-11 22:17:48 -0700 | [diff] [blame^] | 88 | } // namespace std |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 89 | |
Shinichiro Hamaji | 43defe0 | 2015-07-11 07:06:43 +0900 | [diff] [blame] | 90 | extern Symbol kEmptySym; |
Shinichiro Hamaji | 94d6f2a | 2015-07-05 05:32:25 +0900 | [diff] [blame] | 91 | extern Symbol kShellSym; |
| 92 | |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 93 | void InitSymtab(); |
| 94 | void QuitSymtab(); |
| 95 | Symbol Intern(StringPiece s); |
| 96 | |
| 97 | string JoinSymbols(const vector<Symbol>& syms, const char* sep); |
| 98 | |
| 99 | #endif // SYMTAB_H_ |