blob: 9fac956e5ebbd5567905eacada7766b9d6b25f1d [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 Hamaji5081c712015-08-14 16:49:20 +090063 v_.push_back(' ');
64 v->Eval(ev, &v_);
Shinichiro Hamaji4c469b32015-06-15 19:53:36 +090065}
66
Shinichiro Hamaji81699be2015-06-22 18:07:38 +090067StringPiece SimpleVar::String() const {
Shinichiro Hamaji5081c712015-08-14 16:49:20 +090068 return v_;
Shinichiro Hamaji81699be2015-06-22 18:07:38 +090069}
70
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090071string SimpleVar::DebugString() const {
Shinichiro Hamaji5081c712015-08-14 16:49:20 +090072 return v_;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090073}
74
Shinichiro Hamajif62e9a72015-06-26 04:18:21 +090075RecursiveVar::RecursiveVar(Value* v, VarOrigin origin, StringPiece orig)
Shinichiro Hamaji81699be2015-06-22 18:07:38 +090076 : v_(v), origin_(origin), orig_(orig) {
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090077}
78
79void RecursiveVar::Eval(Evaluator* ev, string* s) const {
80 v_->Eval(ev, s);
81}
82
Shinichiro Hamaji4c469b32015-06-15 19:53:36 +090083void RecursiveVar::AppendVar(Evaluator*, Value* v) {
84 v_ = NewExpr3(v_, NewLiteral(" "), v);
85}
86
Shinichiro Hamaji81699be2015-06-22 18:07:38 +090087StringPiece RecursiveVar::String() const {
88 return orig_;
89}
90
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090091string RecursiveVar::DebugString() const {
92 return v_->DebugString();
93}
94
95UndefinedVar::UndefinedVar() {}
96
97void UndefinedVar::Eval(Evaluator*, string*) const {
98 // Nothing to do.
99}
100
Shinichiro Hamaji81699be2015-06-22 18:07:38 +0900101StringPiece UndefinedVar::String() const {
Shinichiro Hamaji388e8582015-07-03 16:51:46 +0900102 return StringPiece("");
Shinichiro Hamaji81699be2015-06-22 18:07:38 +0900103}
104
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900105string UndefinedVar::DebugString() const {
106 return "*undefined*";
107}
108
109Vars::~Vars() {
110 for (auto p : *this) {
111 delete p.second;
112 }
113}
114
Shinichiro Hamajie7992752015-06-29 18:38:35 +0900115Var* Vars::Lookup(Symbol name) const {
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900116 auto found = find(name);
117 if (found == end())
118 return kUndefined;
Shinichiro Hamaji5163e042015-07-14 03:51:44 +0900119 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 Hamaji776ca302015-06-06 03:52:48 +0900125}
126
Shinichiro Hamajie7992752015-06-29 18:38:35 +0900127void Vars::Assign(Symbol name, Var* v) {
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900128 auto p = insert(make_pair(name, v));
129 if (!p.second) {
Shinichiro Hamaji0ea9e912015-06-26 04:24:38 +0900130 Var* orig = p.first->second;
131 if (orig->Origin() == VarOrigin::OVERRIDE ||
132 orig->Origin() == VarOrigin::ENVIRONMENT_OVERRIDE) {
133 return;
134 }
Shinichiro Hamaji2bb8f372015-06-26 06:58:13 +0900135 if (orig->Origin() == VarOrigin::AUTOMATIC) {
136 ERROR("overriding automatic variable is not implemented yet");
137 }
Shinichiro Hamaji0ea9e912015-06-26 04:24:38 +0900138 if (orig->IsDefined())
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900139 delete p.first->second;
140 p.first->second = v;
141 }
142}
Shinichiro Hamajicf0cd682015-06-18 16:18:13 +0900143
Shinichiro Hamaji5163e042015-07-14 03:51:44 +0900144unordered_set<Symbol> Vars::used_env_vars_;
145
Shinichiro Hamajie7992752015-06-29 18:38:35 +0900146ScopedVar::ScopedVar(Vars* vars, Symbol name, Var* var)
Shinichiro Hamajicf0cd682015-06-18 16:18:13 +0900147 : 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
156ScopedVar::~ScopedVar() {
157 if (orig_) {
158 iter_->second = orig_;
159 } else {
160 vars_->erase(iter_);
161 }
162}