Shinichiro Hamaji | 1d545aa | 2015-06-23 15:29:13 +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 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 15 | #ifndef VAR_H_ |
| 16 | #define VAR_H_ |
| 17 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 18 | #include <string> |
| 19 | #include <unordered_map> |
Shinichiro Hamaji | 5163e04 | 2015-07-14 03:51:44 +0900 | [diff] [blame] | 20 | #include <unordered_set> |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 21 | |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 22 | #include "expr.h" |
| 23 | #include "stmt.h" |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 24 | #include "string_piece.h" |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 25 | #include "symtab.h" |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 26 | |
| 27 | using namespace std; |
| 28 | |
| 29 | class Evaluator; |
| 30 | class Value; |
| 31 | |
Shinichiro Hamaji | f62e9a7 | 2015-06-26 04:18:21 +0900 | [diff] [blame] | 32 | enum struct VarOrigin { |
| 33 | UNDEFINED, |
| 34 | DEFAULT, |
| 35 | ENVIRONMENT, |
| 36 | ENVIRONMENT_OVERRIDE, |
| 37 | FILE, |
| 38 | COMMAND_LINE, |
| 39 | OVERRIDE, |
| 40 | AUTOMATIC, |
| 41 | }; |
| 42 | |
| 43 | const char* GetOriginStr(VarOrigin origin); |
| 44 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 45 | class Var : public Evaluable { |
| 46 | public: |
| 47 | virtual ~Var(); |
| 48 | |
| 49 | virtual const char* Flavor() const = 0; |
Shinichiro Hamaji | f62e9a7 | 2015-06-26 04:18:21 +0900 | [diff] [blame] | 50 | virtual VarOrigin Origin() const = 0; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 51 | virtual bool IsDefined() const { return true; } |
| 52 | |
Shinichiro Hamaji | 4c469b3 | 2015-06-15 19:53:36 +0900 | [diff] [blame] | 53 | virtual void AppendVar(Evaluator* ev, Value* v); |
| 54 | |
Shinichiro Hamaji | 81699be | 2015-06-22 18:07:38 +0900 | [diff] [blame] | 55 | virtual StringPiece String() const = 0; |
| 56 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 57 | virtual string DebugString() const = 0; |
| 58 | |
| 59 | protected: |
| 60 | Var(); |
| 61 | }; |
| 62 | |
| 63 | class SimpleVar : public Var { |
| 64 | public: |
Shinichiro Hamaji | 5081c71 | 2015-08-14 16:49:20 +0900 | [diff] [blame] | 65 | SimpleVar(const string& v, VarOrigin origin); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 66 | |
| 67 | virtual const char* Flavor() const { |
| 68 | return "simple"; |
| 69 | } |
Shinichiro Hamaji | f62e9a7 | 2015-06-26 04:18:21 +0900 | [diff] [blame] | 70 | virtual VarOrigin Origin() const { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 71 | return origin_; |
| 72 | } |
| 73 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 74 | virtual void Eval(Evaluator* ev, string* s) const override; |
| 75 | |
Shinichiro Hamaji | 4c469b3 | 2015-06-15 19:53:36 +0900 | [diff] [blame] | 76 | virtual void AppendVar(Evaluator* ev, Value* v); |
| 77 | |
Shinichiro Hamaji | 81699be | 2015-06-22 18:07:38 +0900 | [diff] [blame] | 78 | virtual StringPiece String() const override; |
| 79 | |
Shinichiro Hamaji | cf0cd68 | 2015-06-18 16:18:13 +0900 | [diff] [blame] | 80 | virtual string DebugString() const override; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 81 | |
| 82 | private: |
Shinichiro Hamaji | 5081c71 | 2015-08-14 16:49:20 +0900 | [diff] [blame] | 83 | string v_; |
Shinichiro Hamaji | f62e9a7 | 2015-06-26 04:18:21 +0900 | [diff] [blame] | 84 | VarOrigin origin_; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | class RecursiveVar : public Var { |
| 88 | public: |
Shinichiro Hamaji | f62e9a7 | 2015-06-26 04:18:21 +0900 | [diff] [blame] | 89 | RecursiveVar(Value* v, VarOrigin origin, StringPiece orig); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 90 | |
| 91 | virtual const char* Flavor() const { |
| 92 | return "recursive"; |
| 93 | } |
Shinichiro Hamaji | f62e9a7 | 2015-06-26 04:18:21 +0900 | [diff] [blame] | 94 | virtual VarOrigin Origin() const { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 95 | return origin_; |
| 96 | } |
| 97 | |
| 98 | virtual void Eval(Evaluator* ev, string* s) const override; |
| 99 | |
Shinichiro Hamaji | 4c469b3 | 2015-06-15 19:53:36 +0900 | [diff] [blame] | 100 | virtual void AppendVar(Evaluator* ev, Value* v); |
| 101 | |
Shinichiro Hamaji | 81699be | 2015-06-22 18:07:38 +0900 | [diff] [blame] | 102 | virtual StringPiece String() const override; |
| 103 | |
Shinichiro Hamaji | cf0cd68 | 2015-06-18 16:18:13 +0900 | [diff] [blame] | 104 | virtual string DebugString() const override; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 105 | |
| 106 | private: |
| 107 | Value* v_; |
Shinichiro Hamaji | f62e9a7 | 2015-06-26 04:18:21 +0900 | [diff] [blame] | 108 | VarOrigin origin_; |
Shinichiro Hamaji | 81699be | 2015-06-22 18:07:38 +0900 | [diff] [blame] | 109 | StringPiece orig_; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | class UndefinedVar : public Var { |
| 113 | public: |
| 114 | UndefinedVar(); |
| 115 | |
| 116 | virtual const char* Flavor() const { |
| 117 | return "undefined"; |
| 118 | } |
Shinichiro Hamaji | f62e9a7 | 2015-06-26 04:18:21 +0900 | [diff] [blame] | 119 | virtual VarOrigin Origin() const { |
| 120 | return VarOrigin::UNDEFINED; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 121 | } |
| 122 | virtual bool IsDefined() const { return false; } |
| 123 | |
| 124 | virtual void Eval(Evaluator* ev, string* s) const override; |
| 125 | |
Shinichiro Hamaji | 81699be | 2015-06-22 18:07:38 +0900 | [diff] [blame] | 126 | virtual StringPiece String() const override; |
| 127 | |
Shinichiro Hamaji | cf0cd68 | 2015-06-18 16:18:13 +0900 | [diff] [blame] | 128 | virtual string DebugString() const override; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | extern UndefinedVar* kUndefined; |
| 132 | |
Shinichiro Hamaji | ffc52c3 | 2015-06-23 16:51:07 +0900 | [diff] [blame] | 133 | class RuleVar : public Var { |
| 134 | public: |
| 135 | RuleVar(Var* v, AssignOp op) |
| 136 | : v_(v), op_(op) {} |
| 137 | virtual ~RuleVar() { |
| 138 | delete v_; |
| 139 | } |
| 140 | |
| 141 | virtual const char* Flavor() const { |
| 142 | return v_->Flavor(); |
| 143 | } |
Shinichiro Hamaji | f62e9a7 | 2015-06-26 04:18:21 +0900 | [diff] [blame] | 144 | virtual VarOrigin Origin() const { |
Shinichiro Hamaji | ffc52c3 | 2015-06-23 16:51:07 +0900 | [diff] [blame] | 145 | return v_->Origin(); |
| 146 | } |
| 147 | virtual bool IsDefined() const { |
| 148 | return v_->IsDefined(); |
| 149 | } |
| 150 | virtual void Eval(Evaluator* ev, string* s) const { |
| 151 | v_->Eval(ev, s); |
| 152 | } |
| 153 | virtual void AppendVar(Evaluator* ev, Value* v) { |
| 154 | v_->AppendVar(ev, v); |
| 155 | } |
| 156 | virtual StringPiece String() const override { |
| 157 | return v_->String(); |
| 158 | } |
| 159 | virtual string DebugString() const override { |
| 160 | return v_->DebugString(); |
| 161 | } |
| 162 | |
| 163 | Var* v() const { return v_; } |
| 164 | AssignOp op() const { return op_; } |
| 165 | |
| 166 | private: |
| 167 | Var* v_; |
| 168 | AssignOp op_; |
| 169 | }; |
| 170 | |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 171 | class Vars : public unordered_map<Symbol, Var*> { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 172 | public: |
| 173 | ~Vars(); |
| 174 | |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 175 | Var* Lookup(Symbol name) const; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 176 | |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 177 | void Assign(Symbol name, Var* v); |
Shinichiro Hamaji | 5163e04 | 2015-07-14 03:51:44 +0900 | [diff] [blame] | 178 | |
| 179 | static const unordered_set<Symbol>& used_env_vars() { |
| 180 | return used_env_vars_; |
| 181 | } |
| 182 | |
| 183 | private: |
| 184 | static unordered_set<Symbol> used_env_vars_; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 185 | }; |
| 186 | |
Shinichiro Hamaji | cf0cd68 | 2015-06-18 16:18:13 +0900 | [diff] [blame] | 187 | class ScopedVar { |
| 188 | public: |
| 189 | // Does not take ownerships of arguments. |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 190 | ScopedVar(Vars* vars, Symbol name, Var* var); |
Shinichiro Hamaji | cf0cd68 | 2015-06-18 16:18:13 +0900 | [diff] [blame] | 191 | ~ScopedVar(); |
| 192 | |
| 193 | private: |
| 194 | Vars* vars_; |
| 195 | Var* orig_; |
| 196 | Vars::iterator iter_; |
| 197 | }; |
| 198 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 199 | #endif // VAR_H_ |