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 | |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 15 | // +build ignore |
| 16 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 17 | #include "var.h" |
| 18 | |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 19 | #include "expr.h" |
Shinichiro Hamaji | 4c469b3 | 2015-06-15 19:53:36 +0900 | [diff] [blame] | 20 | #include "log.h" |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 21 | |
| 22 | UndefinedVar kUndefinedBuf; |
| 23 | UndefinedVar* kUndefined = &kUndefinedBuf; |
| 24 | |
Shinichiro Hamaji | f62e9a7 | 2015-06-26 04:18:21 +0900 | [diff] [blame] | 25 | const char* GetOriginStr(VarOrigin origin) { |
| 26 | switch (origin) { |
| 27 | case VarOrigin::UNDEFINED: return "undefined"; |
| 28 | case VarOrigin::DEFAULT: return "default"; |
| 29 | case VarOrigin::ENVIRONMENT: return "environment"; |
| 30 | case VarOrigin::ENVIRONMENT_OVERRIDE: return "environment override"; |
| 31 | case VarOrigin::FILE: return "file"; |
| 32 | case VarOrigin::COMMAND_LINE: return "command line"; |
| 33 | case VarOrigin::OVERRIDE: return "override"; |
| 34 | case VarOrigin::AUTOMATIC: return "automatic"; |
| 35 | } |
| 36 | CHECK(false); |
| 37 | return "*** broken origin ***"; |
| 38 | } |
| 39 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 40 | Var::Var() { |
| 41 | } |
| 42 | |
| 43 | Var::~Var() { |
| 44 | } |
| 45 | |
Shinichiro Hamaji | 4c469b3 | 2015-06-15 19:53:36 +0900 | [diff] [blame] | 46 | void Var::AppendVar(Evaluator*, Value*) { |
| 47 | CHECK(false); |
| 48 | } |
| 49 | |
Shinichiro Hamaji | 90e52ce | 2016-02-10 13:53:41 +0900 | [diff] [blame] | 50 | SimpleVar::SimpleVar(VarOrigin origin) |
| 51 | : origin_(origin) { |
| 52 | } |
| 53 | |
Shinichiro Hamaji | 5081c71 | 2015-08-14 16:49:20 +0900 | [diff] [blame] | 54 | SimpleVar::SimpleVar(const string& v, VarOrigin origin) |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 55 | : v_(v), origin_(origin) { |
| 56 | } |
| 57 | |
| 58 | void SimpleVar::Eval(Evaluator*, string* s) const { |
Shinichiro Hamaji | 5081c71 | 2015-08-14 16:49:20 +0900 | [diff] [blame] | 59 | *s += v_; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 60 | } |
| 61 | |
Shinichiro Hamaji | 4c469b3 | 2015-06-15 19:53:36 +0900 | [diff] [blame] | 62 | void SimpleVar::AppendVar(Evaluator* ev, Value* v) { |
Shinichiro Hamaji | e6a4d17 | 2016-03-10 14:47:34 +0900 | [diff] [blame] | 63 | string buf; |
| 64 | v->Eval(ev, &buf); |
Shinichiro Hamaji | 5081c71 | 2015-08-14 16:49:20 +0900 | [diff] [blame] | 65 | v_.push_back(' '); |
Shinichiro Hamaji | e6a4d17 | 2016-03-10 14:47:34 +0900 | [diff] [blame] | 66 | v_ += buf; |
Shinichiro Hamaji | 4c469b3 | 2015-06-15 19:53:36 +0900 | [diff] [blame] | 67 | } |
| 68 | |
Shinichiro Hamaji | 81699be | 2015-06-22 18:07:38 +0900 | [diff] [blame] | 69 | StringPiece SimpleVar::String() const { |
Shinichiro Hamaji | 5081c71 | 2015-08-14 16:49:20 +0900 | [diff] [blame] | 70 | return v_; |
Shinichiro Hamaji | 81699be | 2015-06-22 18:07:38 +0900 | [diff] [blame] | 71 | } |
| 72 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 73 | string SimpleVar::DebugString() const { |
Shinichiro Hamaji | 5081c71 | 2015-08-14 16:49:20 +0900 | [diff] [blame] | 74 | return v_; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 75 | } |
| 76 | |
Shinichiro Hamaji | f62e9a7 | 2015-06-26 04:18:21 +0900 | [diff] [blame] | 77 | RecursiveVar::RecursiveVar(Value* v, VarOrigin origin, StringPiece orig) |
Shinichiro Hamaji | 81699be | 2015-06-22 18:07:38 +0900 | [diff] [blame] | 78 | : v_(v), origin_(origin), orig_(orig) { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void RecursiveVar::Eval(Evaluator* ev, string* s) const { |
| 82 | v_->Eval(ev, s); |
| 83 | } |
| 84 | |
Shinichiro Hamaji | 4c469b3 | 2015-06-15 19:53:36 +0900 | [diff] [blame] | 85 | void RecursiveVar::AppendVar(Evaluator*, Value* v) { |
| 86 | v_ = NewExpr3(v_, NewLiteral(" "), v); |
| 87 | } |
| 88 | |
Shinichiro Hamaji | 81699be | 2015-06-22 18:07:38 +0900 | [diff] [blame] | 89 | StringPiece RecursiveVar::String() const { |
| 90 | return orig_; |
| 91 | } |
| 92 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 93 | string RecursiveVar::DebugString() const { |
| 94 | return v_->DebugString(); |
| 95 | } |
| 96 | |
| 97 | UndefinedVar::UndefinedVar() {} |
| 98 | |
| 99 | void UndefinedVar::Eval(Evaluator*, string*) const { |
| 100 | // Nothing to do. |
| 101 | } |
| 102 | |
Shinichiro Hamaji | 81699be | 2015-06-22 18:07:38 +0900 | [diff] [blame] | 103 | StringPiece UndefinedVar::String() const { |
Shinichiro Hamaji | 388e858 | 2015-07-03 16:51:46 +0900 | [diff] [blame] | 104 | return StringPiece(""); |
Shinichiro Hamaji | 81699be | 2015-06-22 18:07:38 +0900 | [diff] [blame] | 105 | } |
| 106 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 107 | string UndefinedVar::DebugString() const { |
| 108 | return "*undefined*"; |
| 109 | } |
| 110 | |
| 111 | Vars::~Vars() { |
| 112 | for (auto p : *this) { |
| 113 | delete p.second; |
| 114 | } |
| 115 | } |
| 116 | |
Shinichiro Hamaji | c9b9e5e | 2016-02-18 18:18:54 +0900 | [diff] [blame] | 117 | void Vars::add_used_env_vars(Symbol v) { |
| 118 | used_env_vars_.insert(v); |
| 119 | } |
| 120 | |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 121 | Var* Vars::Lookup(Symbol name) const { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 122 | auto found = find(name); |
| 123 | if (found == end()) |
| 124 | return kUndefined; |
Shinichiro Hamaji | 5163e04 | 2015-07-14 03:51:44 +0900 | [diff] [blame] | 125 | Var* v = found->second; |
| 126 | if (v->Origin() == VarOrigin::ENVIRONMENT || |
| 127 | v->Origin() == VarOrigin::ENVIRONMENT_OVERRIDE) { |
| 128 | used_env_vars_.insert(name); |
| 129 | } |
| 130 | return v; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 131 | } |
| 132 | |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 133 | void Vars::Assign(Symbol name, Var* v) { |
Shinichiro Hamaji | 3deff5b | 2016-02-17 17:50:06 +0900 | [diff] [blame] | 134 | auto p = emplace(name, v); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 135 | if (!p.second) { |
Shinichiro Hamaji | 0ea9e91 | 2015-06-26 04:24:38 +0900 | [diff] [blame] | 136 | Var* orig = p.first->second; |
| 137 | if (orig->Origin() == VarOrigin::OVERRIDE || |
| 138 | orig->Origin() == VarOrigin::ENVIRONMENT_OVERRIDE) { |
| 139 | return; |
| 140 | } |
Shinichiro Hamaji | 2bb8f37 | 2015-06-26 06:58:13 +0900 | [diff] [blame] | 141 | if (orig->Origin() == VarOrigin::AUTOMATIC) { |
| 142 | ERROR("overriding automatic variable is not implemented yet"); |
| 143 | } |
Shinichiro Hamaji | 0ea9e91 | 2015-06-26 04:24:38 +0900 | [diff] [blame] | 144 | if (orig->IsDefined()) |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 145 | delete p.first->second; |
| 146 | p.first->second = v; |
| 147 | } |
| 148 | } |
Shinichiro Hamaji | cf0cd68 | 2015-06-18 16:18:13 +0900 | [diff] [blame] | 149 | |
Shinichiro Hamaji | 5163e04 | 2015-07-14 03:51:44 +0900 | [diff] [blame] | 150 | unordered_set<Symbol> Vars::used_env_vars_; |
| 151 | |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 152 | ScopedVar::ScopedVar(Vars* vars, Symbol name, Var* var) |
Shinichiro Hamaji | cf0cd68 | 2015-06-18 16:18:13 +0900 | [diff] [blame] | 153 | : vars_(vars), orig_(NULL) { |
Shinichiro Hamaji | 3deff5b | 2016-02-17 17:50:06 +0900 | [diff] [blame] | 154 | auto p = vars->emplace(name, var); |
Shinichiro Hamaji | cf0cd68 | 2015-06-18 16:18:13 +0900 | [diff] [blame] | 155 | iter_ = p.first; |
| 156 | if (!p.second) { |
| 157 | orig_ = iter_->second; |
| 158 | iter_->second = var; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | ScopedVar::~ScopedVar() { |
| 163 | if (orig_) { |
| 164 | iter_->second = orig_; |
| 165 | } else { |
| 166 | vars_->erase(iter_); |
| 167 | } |
| 168 | } |