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 | 5081c71 | 2015-08-14 16:49:20 +0900 | [diff] [blame] | 63 | v_.push_back(' '); |
| 64 | v->Eval(ev, &v_); |
Shinichiro Hamaji | 4c469b3 | 2015-06-15 19:53:36 +0900 | [diff] [blame] | 65 | } |
| 66 | |
Shinichiro Hamaji | 81699be | 2015-06-22 18:07:38 +0900 | [diff] [blame] | 67 | StringPiece SimpleVar::String() const { |
Shinichiro Hamaji | 5081c71 | 2015-08-14 16:49:20 +0900 | [diff] [blame] | 68 | return v_; |
Shinichiro Hamaji | 81699be | 2015-06-22 18:07:38 +0900 | [diff] [blame] | 69 | } |
| 70 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 71 | string SimpleVar::DebugString() const { |
Shinichiro Hamaji | 5081c71 | 2015-08-14 16:49:20 +0900 | [diff] [blame] | 72 | return v_; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 73 | } |
| 74 | |
Shinichiro Hamaji | f62e9a7 | 2015-06-26 04:18:21 +0900 | [diff] [blame] | 75 | RecursiveVar::RecursiveVar(Value* v, VarOrigin origin, StringPiece orig) |
Shinichiro Hamaji | 81699be | 2015-06-22 18:07:38 +0900 | [diff] [blame] | 76 | : v_(v), origin_(origin), orig_(orig) { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void RecursiveVar::Eval(Evaluator* ev, string* s) const { |
| 80 | v_->Eval(ev, s); |
| 81 | } |
| 82 | |
Shinichiro Hamaji | 4c469b3 | 2015-06-15 19:53:36 +0900 | [diff] [blame] | 83 | void RecursiveVar::AppendVar(Evaluator*, Value* v) { |
| 84 | v_ = NewExpr3(v_, NewLiteral(" "), v); |
| 85 | } |
| 86 | |
Shinichiro Hamaji | 81699be | 2015-06-22 18:07:38 +0900 | [diff] [blame] | 87 | StringPiece RecursiveVar::String() const { |
| 88 | return orig_; |
| 89 | } |
| 90 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 91 | string RecursiveVar::DebugString() const { |
| 92 | return v_->DebugString(); |
| 93 | } |
| 94 | |
| 95 | UndefinedVar::UndefinedVar() {} |
| 96 | |
| 97 | void UndefinedVar::Eval(Evaluator*, string*) const { |
| 98 | // Nothing to do. |
| 99 | } |
| 100 | |
Shinichiro Hamaji | 81699be | 2015-06-22 18:07:38 +0900 | [diff] [blame] | 101 | StringPiece UndefinedVar::String() const { |
Shinichiro Hamaji | 388e858 | 2015-07-03 16:51:46 +0900 | [diff] [blame] | 102 | return StringPiece(""); |
Shinichiro Hamaji | 81699be | 2015-06-22 18:07:38 +0900 | [diff] [blame] | 103 | } |
| 104 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 105 | string UndefinedVar::DebugString() const { |
| 106 | return "*undefined*"; |
| 107 | } |
| 108 | |
| 109 | Vars::~Vars() { |
| 110 | for (auto p : *this) { |
| 111 | delete p.second; |
| 112 | } |
| 113 | } |
| 114 | |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 115 | Var* Vars::Lookup(Symbol name) const { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 116 | auto found = find(name); |
| 117 | if (found == end()) |
| 118 | return kUndefined; |
Shinichiro Hamaji | 5163e04 | 2015-07-14 03:51:44 +0900 | [diff] [blame] | 119 | Var* v = found->second; |
| 120 | if (v->Origin() == VarOrigin::ENVIRONMENT || |
| 121 | v->Origin() == VarOrigin::ENVIRONMENT_OVERRIDE) { |
| 122 | used_env_vars_.insert(name); |
| 123 | } |
| 124 | return v; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 125 | } |
| 126 | |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 127 | void Vars::Assign(Symbol name, Var* v) { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 128 | auto p = insert(make_pair(name, v)); |
| 129 | if (!p.second) { |
Shinichiro Hamaji | 0ea9e91 | 2015-06-26 04:24:38 +0900 | [diff] [blame] | 130 | Var* orig = p.first->second; |
| 131 | if (orig->Origin() == VarOrigin::OVERRIDE || |
| 132 | orig->Origin() == VarOrigin::ENVIRONMENT_OVERRIDE) { |
| 133 | return; |
| 134 | } |
Shinichiro Hamaji | 2bb8f37 | 2015-06-26 06:58:13 +0900 | [diff] [blame] | 135 | if (orig->Origin() == VarOrigin::AUTOMATIC) { |
| 136 | ERROR("overriding automatic variable is not implemented yet"); |
| 137 | } |
Shinichiro Hamaji | 0ea9e91 | 2015-06-26 04:24:38 +0900 | [diff] [blame] | 138 | if (orig->IsDefined()) |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 139 | delete p.first->second; |
| 140 | p.first->second = v; |
| 141 | } |
| 142 | } |
Shinichiro Hamaji | cf0cd68 | 2015-06-18 16:18:13 +0900 | [diff] [blame] | 143 | |
Shinichiro Hamaji | 5163e04 | 2015-07-14 03:51:44 +0900 | [diff] [blame] | 144 | unordered_set<Symbol> Vars::used_env_vars_; |
| 145 | |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 146 | ScopedVar::ScopedVar(Vars* vars, Symbol name, Var* var) |
Shinichiro Hamaji | cf0cd68 | 2015-06-18 16:18:13 +0900 | [diff] [blame] | 147 | : vars_(vars), orig_(NULL) { |
| 148 | auto p = vars->insert(make_pair(name, var)); |
| 149 | iter_ = p.first; |
| 150 | if (!p.second) { |
| 151 | orig_ = iter_->second; |
| 152 | iter_->second = var; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | ScopedVar::~ScopedVar() { |
| 157 | if (orig_) { |
| 158 | iter_->second = orig_; |
| 159 | } else { |
| 160 | vars_->erase(iter_); |
| 161 | } |
| 162 | } |