Home | History | Annotate | Download | only in kati
      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 
     25 extern vector<string*>* g_symbols;
     26 
     27 class Symtab;
     28 class Var;
     29 
     30 class Symbol {
     31  public:
     32   struct IsUninitialized {};
     33   explicit Symbol(IsUninitialized)
     34     : v_(-1) {
     35   }
     36 
     37   const string& str() const {
     38     return *((*g_symbols)[v_]);
     39   }
     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 
     56   bool IsValid() const { return v_ >= 0; }
     57 
     58   Var* GetGlobalVar() const;
     59   void SetGlobalVar(Var* v, bool is_override = false, bool* readonly = nullptr) const;
     60 
     61  private:
     62   explicit Symbol(int v);
     63 
     64   int v_;
     65 
     66   friend class Symtab;
     67 };
     68 
     69 class ScopedGlobalVar {
     70  public:
     71   ScopedGlobalVar(Symbol name, Var* var);
     72   ~ScopedGlobalVar();
     73 
     74  private:
     75   Symbol name_;
     76   Var* orig_;
     77 };
     78 
     79 inline bool operator==(const Symbol& x, const Symbol& y) {
     80   return x.val() == y.val();
     81 }
     82 
     83 inline bool operator<(const Symbol& x, const Symbol& y) {
     84   return x.val() < y.val();
     85 }
     86 
     87 namespace std {
     88 template<> struct hash<Symbol> {
     89   size_t operator()(const Symbol& s) const {
     90     return s.val();
     91   }
     92 };
     93 }
     94 
     95 extern Symbol kEmptySym;
     96 extern Symbol kShellSym;
     97 
     98 void InitSymtab();
     99 void QuitSymtab();
    100 Symbol Intern(StringPiece s);
    101 
    102 string JoinSymbols(const vector<Symbol>& syms, const char* sep);
    103 
    104 #endif  // SYMTAB_H_
    105