blob: 4b67c4fb19e31a4e485224576181fed1a0aa2341 [file] [log] [blame]
Shinichiro Hamaji1d545aa2015-06-23 15:29:13 +09001// 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 Ukai744bb2b2015-06-25 00:10:52 +090015// +build ignore
16
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090017#include "var.h"
18
Shinichiro Hamaji645cca72015-09-24 17:04:21 +090019#include "expr.h"
Shinichiro Hamaji4c469b32015-06-15 19:53:36 +090020#include "log.h"
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090021
22UndefinedVar kUndefinedBuf;
23UndefinedVar* kUndefined = &kUndefinedBuf;
24
Shinichiro Hamajif62e9a72015-06-26 04:18:21 +090025const 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 Hamaji776ca302015-06-06 03:52:48 +090040Var::Var() {
41}
42
43Var::~Var() {
44}
45
Shinichiro Hamaji4c469b32015-06-15 19:53:36 +090046void Var::AppendVar(Evaluator*, Value*) {
47 CHECK(false);
48}
49
Shinichiro Hamaji90e52ce2016-02-10 13:53:41 +090050SimpleVar::SimpleVar(VarOrigin origin)
51 : origin_(origin) {
52}
53
Shinichiro Hamaji5081c712015-08-14 16:49:20 +090054SimpleVar::SimpleVar(const string& v, VarOrigin origin)
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090055 : v_(v), origin_(origin) {
56}
57
58void SimpleVar::Eval(Evaluator*, string* s) const {
Shinichiro Hamaji5081c712015-08-14 16:49:20 +090059 *s += v_;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090060}
61
Shinichiro Hamaji4c469b32015-06-15 19:53:36 +090062void SimpleVar::AppendVar(Evaluator* ev, Value* v) {
Shinichiro Hamajie6a4d172016-03-10 14:47:34 +090063 string buf;
64 v->Eval(ev, &buf);
Shinichiro Hamaji5081c712015-08-14 16:49:20 +090065 v_.push_back(' ');
Shinichiro Hamajie6a4d172016-03-10 14:47:34 +090066 v_ += buf;
Shinichiro Hamaji4c469b32015-06-15 19:53:36 +090067}
68
Shinichiro Hamaji81699be2015-06-22 18:07:38 +090069StringPiece SimpleVar::String() const {
Shinichiro Hamaji5081c712015-08-14 16:49:20 +090070 return v_;
Shinichiro Hamaji81699be2015-06-22 18:07:38 +090071}
72
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090073string SimpleVar::DebugString() const {
Shinichiro Hamaji5081c712015-08-14 16:49:20 +090074 return v_;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090075}
76
Shinichiro Hamajif62e9a72015-06-26 04:18:21 +090077RecursiveVar::RecursiveVar(Value* v, VarOrigin origin, StringPiece orig)
Shinichiro Hamaji81699be2015-06-22 18:07:38 +090078 : v_(v), origin_(origin), orig_(orig) {
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090079}
80
81void RecursiveVar::Eval(Evaluator* ev, string* s) const {
82 v_->Eval(ev, s);
83}
84
Shinichiro Hamaji4c469b32015-06-15 19:53:36 +090085void RecursiveVar::AppendVar(Evaluator*, Value* v) {
86 v_ = NewExpr3(v_, NewLiteral(" "), v);
87}
88
Shinichiro Hamaji81699be2015-06-22 18:07:38 +090089StringPiece RecursiveVar::String() const {
90 return orig_;
91}
92
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090093string RecursiveVar::DebugString() const {
94 return v_->DebugString();
95}
96
97UndefinedVar::UndefinedVar() {}
98
99void UndefinedVar::Eval(Evaluator*, string*) const {
100 // Nothing to do.
101}
102
Shinichiro Hamaji81699be2015-06-22 18:07:38 +0900103StringPiece UndefinedVar::String() const {
Shinichiro Hamaji388e8582015-07-03 16:51:46 +0900104 return StringPiece("");
Shinichiro Hamaji81699be2015-06-22 18:07:38 +0900105}
106
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900107string UndefinedVar::DebugString() const {
108 return "*undefined*";
109}
110
111Vars::~Vars() {
112 for (auto p : *this) {
113 delete p.second;
114 }
115}
116
Shinichiro Hamajic9b9e5e2016-02-18 18:18:54 +0900117void Vars::add_used_env_vars(Symbol v) {
118 used_env_vars_.insert(v);
119}
120
Shinichiro Hamajie7992752015-06-29 18:38:35 +0900121Var* Vars::Lookup(Symbol name) const {
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900122 auto found = find(name);
123 if (found == end())
124 return kUndefined;
Shinichiro Hamaji5163e042015-07-14 03:51:44 +0900125 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 Hamaji776ca302015-06-06 03:52:48 +0900131}
132
Shinichiro Hamajie7992752015-06-29 18:38:35 +0900133void Vars::Assign(Symbol name, Var* v) {
Shinichiro Hamaji3deff5b2016-02-17 17:50:06 +0900134 auto p = emplace(name, v);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900135 if (!p.second) {
Shinichiro Hamaji0ea9e912015-06-26 04:24:38 +0900136 Var* orig = p.first->second;
137 if (orig->Origin() == VarOrigin::OVERRIDE ||
138 orig->Origin() == VarOrigin::ENVIRONMENT_OVERRIDE) {
139 return;
140 }
Shinichiro Hamaji2bb8f372015-06-26 06:58:13 +0900141 if (orig->Origin() == VarOrigin::AUTOMATIC) {
142 ERROR("overriding automatic variable is not implemented yet");
143 }
Shinichiro Hamaji0ea9e912015-06-26 04:24:38 +0900144 if (orig->IsDefined())
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900145 delete p.first->second;
146 p.first->second = v;
147 }
148}
Shinichiro Hamajicf0cd682015-06-18 16:18:13 +0900149
Shinichiro Hamaji5163e042015-07-14 03:51:44 +0900150unordered_set<Symbol> Vars::used_env_vars_;
151
Shinichiro Hamajie7992752015-06-29 18:38:35 +0900152ScopedVar::ScopedVar(Vars* vars, Symbol name, Var* var)
Shinichiro Hamajicf0cd682015-06-18 16:18:13 +0900153 : vars_(vars), orig_(NULL) {
Shinichiro Hamaji3deff5b2016-02-17 17:50:06 +0900154 auto p = vars->emplace(name, var);
Shinichiro Hamajicf0cd682015-06-18 16:18:13 +0900155 iter_ = p.first;
156 if (!p.second) {
157 orig_ = iter_->second;
158 iter_->second = var;
159 }
160}
161
162ScopedVar::~ScopedVar() {
163 if (orig_) {
164 iter_->second = orig_;
165 } else {
166 vars_->erase(iter_);
167 }
168}