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