blob: 0469b65e28d5ad4b9e12cf4cfb54f4e5651ddc45 [file] [log] [blame]
Shinichiro Hamajie7992752015-06-29 18:38:35 +09001// 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
23using namespace std;
24
Shinichiro Hamajiba2ccdb2015-07-17 05:55:42 +090025extern vector<string*>* g_symbols;
Shinichiro Hamajie7992752015-06-29 18:38:35 +090026
27class Symtab;
Shinichiro Hamajic9b9e5e2016-02-18 18:18:54 +090028class Var;
Shinichiro Hamajie7992752015-06-29 18:38:35 +090029
30class Symbol {
31 public:
Shinichiro Hamaji94d6f2a2015-07-05 05:32:25 +090032 struct IsUninitialized {};
Dan Willemsen3ce083f2017-10-11 22:17:48 -070033 explicit Symbol(IsUninitialized) : v_(-1) {}
Shinichiro Hamaji94d6f2a2015-07-05 05:32:25 +090034
Dan Willemsen3ce083f2017-10-11 22:17:48 -070035 const string& str() const { return *((*g_symbols)[v_]); }
Shinichiro Hamajie7992752015-06-29 18:38:35 +090036
Dan Willemsen3ce083f2017-10-11 22:17:48 -070037 const char* c_str() const { return str().c_str(); }
Shinichiro Hamajie7992752015-06-29 18:38:35 +090038
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 Hamajia7984ad2015-09-11 16:33:16 +090050 bool IsValid() const { return v_ >= 0; }
51
Shinichiro Hamajic9b9e5e2016-02-18 18:18:54 +090052 Var* GetGlobalVar() const;
Dan Willemsen3ce083f2017-10-11 22:17:48 -070053 void SetGlobalVar(Var* v,
54 bool is_override = false,
55 bool* readonly = nullptr) const;
Shinichiro Hamajic9b9e5e2016-02-18 18:18:54 +090056
Shinichiro Hamajie7992752015-06-29 18:38:35 +090057 private:
58 explicit Symbol(int v);
59
60 int v_;
61
62 friend class Symtab;
63};
64
Shinichiro Hamajic9b9e5e2016-02-18 18:18:54 +090065class ScopedGlobalVar {
66 public:
67 ScopedGlobalVar(Symbol name, Var* var);
68 ~ScopedGlobalVar();
69
70 private:
71 Symbol name_;
72 Var* orig_;
73};
74
Shinichiro Hamajie7992752015-06-29 18:38:35 +090075inline bool operator==(const Symbol& x, const Symbol& y) {
76 return x.val() == y.val();
77}
78
Dan Willemsenb248caa2015-10-01 16:07:48 -070079inline bool operator<(const Symbol& x, const Symbol& y) {
Shinichiro Hamaji675ecf32015-10-03 10:57:31 +090080 return x.val() < y.val();
Dan Willemsenb248caa2015-10-01 16:07:48 -070081}
82
Shinichiro Hamajie7992752015-06-29 18:38:35 +090083namespace std {
Dan Willemsen3ce083f2017-10-11 22:17:48 -070084template <>
85struct hash<Symbol> {
86 size_t operator()(const Symbol& s) const { return s.val(); }
Shinichiro Hamajie7992752015-06-29 18:38:35 +090087};
Dan Willemsen3ce083f2017-10-11 22:17:48 -070088} // namespace std
Shinichiro Hamajie7992752015-06-29 18:38:35 +090089
Shinichiro Hamaji43defe02015-07-11 07:06:43 +090090extern Symbol kEmptySym;
Shinichiro Hamaji94d6f2a2015-07-05 05:32:25 +090091extern Symbol kShellSym;
92
Shinichiro Hamajie7992752015-06-29 18:38:35 +090093void InitSymtab();
94void QuitSymtab();
95Symbol Intern(StringPiece s);
96
97string JoinSymbols(const vector<Symbol>& syms, const char* sep);
98
99#endif // SYMTAB_H_