blob: 64385c02bfe40cf4baf4cd8f46818c0ec2431a78 [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 Hamaji4c469b32015-06-15 19:53:36 +090019#include "log.h"
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090020#include "value.h"
21
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 Hamajif62e9a72015-06-26 04:18:21 +090050SimpleVar::SimpleVar(shared_ptr<string> v, VarOrigin origin)
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090051 : v_(v), origin_(origin) {
52}
53
54void SimpleVar::Eval(Evaluator*, string* s) const {
55 *s += *v_;
56}
57
Shinichiro Hamaji4c469b32015-06-15 19:53:36 +090058void 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 Hamaji81699be2015-06-22 18:07:38 +090065StringPiece SimpleVar::String() const {
66 return *v_;
67}
68
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090069string SimpleVar::DebugString() const {
70 return *v_;
71}
72
Shinichiro Hamajif62e9a72015-06-26 04:18:21 +090073RecursiveVar::RecursiveVar(Value* v, VarOrigin origin, StringPiece orig)
Shinichiro Hamaji81699be2015-06-22 18:07:38 +090074 : v_(v), origin_(origin), orig_(orig) {
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090075}
76
77void RecursiveVar::Eval(Evaluator* ev, string* s) const {
78 v_->Eval(ev, s);
79}
80
Shinichiro Hamaji4c469b32015-06-15 19:53:36 +090081void RecursiveVar::AppendVar(Evaluator*, Value* v) {
82 v_ = NewExpr3(v_, NewLiteral(" "), v);
83}
84
Shinichiro Hamaji81699be2015-06-22 18:07:38 +090085StringPiece RecursiveVar::String() const {
86 return orig_;
87}
88
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090089string RecursiveVar::DebugString() const {
90 return v_->DebugString();
91}
92
93UndefinedVar::UndefinedVar() {}
94
95void UndefinedVar::Eval(Evaluator*, string*) const {
96 // Nothing to do.
97}
98
Shinichiro Hamaji81699be2015-06-22 18:07:38 +090099StringPiece UndefinedVar::String() const {
100 return STRING_PIECE("");
101}
102
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900103string UndefinedVar::DebugString() const {
104 return "*undefined*";
105}
106
107Vars::~Vars() {
108 for (auto p : *this) {
109 delete p.second;
110 }
111}
112
113Var* Vars::Lookup(StringPiece name) const {
114 auto found = find(name);
115 if (found == end())
116 return kUndefined;
117 return found->second;
118}
119
120void Vars::Assign(StringPiece name, Var* v) {
121 auto p = insert(make_pair(name, v));
122 if (!p.second) {
Shinichiro Hamaji0ea9e912015-06-26 04:24:38 +0900123 Var* orig = p.first->second;
124 if (orig->Origin() == VarOrigin::OVERRIDE ||
125 orig->Origin() == VarOrigin::ENVIRONMENT_OVERRIDE) {
126 return;
127 }
Shinichiro Hamaji2bb8f372015-06-26 06:58:13 +0900128 if (orig->Origin() == VarOrigin::AUTOMATIC) {
129 ERROR("overriding automatic variable is not implemented yet");
130 }
Shinichiro Hamaji0ea9e912015-06-26 04:24:38 +0900131 if (orig->IsDefined())
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900132 delete p.first->second;
133 p.first->second = v;
134 }
135}
Shinichiro Hamajicf0cd682015-06-18 16:18:13 +0900136
137ScopedVar::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
147ScopedVar::~ScopedVar() {
148 if (orig_) {
149 iter_->second = orig_;
150 } else {
151 vars_->erase(iter_);
152 }
153}