blob: d1de4e11b89c6228416239211e6c8e1d493601d4 [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 {};
33 explicit Symbol(IsUninitialized)
34 : v_(-1) {
35 }
36
Shinichiro Hamajie7992752015-06-29 18:38:35 +090037 const string& str() const {
Shinichiro Hamajiba2ccdb2015-07-17 05:55:42 +090038 return *((*g_symbols)[v_]);
Shinichiro Hamajie7992752015-06-29 18:38:35 +090039 }
40
41 const char* c_str() const {
42 return str().c_str();
43 }
44
45 bool empty() const { return !v_; }
46
47 int val() const { return v_; }
48
49 char get(size_t i) const {
50 const string& s = str();
51 if (i >= s.size())
52 return 0;
53 return s[i];
54 }
55
Shinichiro Hamajia7984ad2015-09-11 16:33:16 +090056 bool IsValid() const { return v_ >= 0; }
57
Shinichiro Hamajic9b9e5e2016-02-18 18:18:54 +090058 Var* GetGlobalVar() const;
59 void SetGlobalVar(Var* v) const;
60
Shinichiro Hamajie7992752015-06-29 18:38:35 +090061 private:
62 explicit Symbol(int v);
63
64 int v_;
65
66 friend class Symtab;
67};
68
Shinichiro Hamajic9b9e5e2016-02-18 18:18:54 +090069class ScopedGlobalVar {
70 public:
71 ScopedGlobalVar(Symbol name, Var* var);
72 ~ScopedGlobalVar();
73
74 private:
75 Symbol name_;
76 Var* orig_;
77};
78
Shinichiro Hamajie7992752015-06-29 18:38:35 +090079inline bool operator==(const Symbol& x, const Symbol& y) {
80 return x.val() == y.val();
81}
82
Dan Willemsenb248caa2015-10-01 16:07:48 -070083inline bool operator<(const Symbol& x, const Symbol& y) {
Shinichiro Hamaji675ecf32015-10-03 10:57:31 +090084 return x.val() < y.val();
Dan Willemsenb248caa2015-10-01 16:07:48 -070085}
86
Shinichiro Hamajie7992752015-06-29 18:38:35 +090087namespace std {
88template<> struct hash<Symbol> {
89 size_t operator()(const Symbol& s) const {
90 return s.val();
91 }
92};
93}
94
Shinichiro Hamaji43defe02015-07-11 07:06:43 +090095extern Symbol kEmptySym;
Shinichiro Hamaji94d6f2a2015-07-05 05:32:25 +090096extern Symbol kShellSym;
97
Shinichiro Hamajie7992752015-06-29 18:38:35 +090098void InitSymtab();
99void QuitSymtab();
100Symbol Intern(StringPiece s);
101
102string JoinSymbols(const vector<Symbol>& syms, const char* sep);
103
104#endif // SYMTAB_H_