Ninja
eval_env.cc
Go to the documentation of this file.
1// Copyright 2011 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
15#include <assert.h>
16
17#include "eval_env.h"
18
19using namespace std;
20
21string BindingEnv::LookupVariable(const string& var) {
22 map<string, string>::iterator i = bindings_.find(var);
23 if (i != bindings_.end())
24 return i->second;
25 if (parent_)
26 return parent_->LookupVariable(var);
27 return "";
28}
29
30void BindingEnv::AddBinding(const string& key, const string& val) {
31 bindings_[key] = val;
32}
33
34void BindingEnv::AddRule(std::unique_ptr<const Rule> rule) {
35 assert(LookupRuleCurrentScope(rule->name()) == NULL);
36 rules_[rule->name()] = std::move(rule);
37}
38
39const Rule* BindingEnv::LookupRuleCurrentScope(const string& rule_name) {
40 auto i = rules_.find(rule_name);
41 if (i == rules_.end())
42 return NULL;
43 return i->second.get();
44}
45
46const Rule* BindingEnv::LookupRule(const string& rule_name) {
47 auto i = rules_.find(rule_name);
48 if (i != rules_.end())
49 return i->second.get();
50 if (parent_)
51 return parent_->LookupRule(rule_name);
52 return NULL;
53}
54
55void Rule::AddBinding(const string& key, const EvalString& val) {
56 bindings_[key] = val;
57}
58
59const EvalString* Rule::GetBinding(const string& key) const {
60 Bindings::const_iterator i = bindings_.find(key);
61 if (i == bindings_.end())
62 return NULL;
63 return &i->second;
64}
65
66std::unique_ptr<Rule> Rule::Phony() {
67 auto rule = std::unique_ptr<Rule>(new Rule("phony"));
68 rule->phony_ = true;
69 return rule;
70}
71
72bool Rule::IsPhony() const {
73 return phony_;
74}
75
76// static
77bool Rule::IsReservedBinding(const string& var) {
78 return var == "command" ||
79 var == "depfile" ||
80 var == "dyndep" ||
81 var == "description" ||
82 var == "deps" ||
83 var == "generator" ||
84 var == "pool" ||
85 var == "restat" ||
86 var == "rspfile" ||
87 var == "rspfile_content" ||
88 var == "msvc_deps_prefix";
89}
90
91const map<string, std::unique_ptr<const Rule>>& BindingEnv::GetRules() const {
92 return rules_;
93}
94
95string BindingEnv::LookupWithFallback(const string& var,
96 const EvalString* eval,
97 Env* env) {
98 map<string, string>::iterator i = bindings_.find(var);
99 if (i != bindings_.end())
100 return i->second;
101
102 if (eval)
103 return eval->Evaluate(env);
104
105 if (parent_)
106 return parent_->LookupVariable(var);
107
108 return "";
109}
110
111string EvalString::Evaluate(Env* env) const {
112 if (parsed_.empty()) {
113 return single_token_;
114 }
115
116 string result;
117 for (TokenList::const_iterator i = parsed_.begin(); i != parsed_.end(); ++i) {
118 if (i->second == RAW)
119 result.append(i->first);
120 else
121 result.append(env->LookupVariable(i->first));
122 }
123 return result;
124}
125
127 if (parsed_.empty()) {
128 single_token_.append(text.begin(), text.end());
129 } else if (!parsed_.empty() && parsed_.back().second == RAW) {
130 parsed_.back().first.append(text.begin(), text.end());
131 } else {
132 parsed_.push_back(std::make_pair(text.AsString(), RAW));
133 }
134}
135
137 if (parsed_.empty() && !single_token_.empty()) {
138 // Going from one to two tokens, so we can no longer apply
139 // our single_token_ optimization and need to push everything
140 // onto the vector.
141 parsed_.push_back(std::make_pair(std::move(single_token_), RAW));
142 }
143 parsed_.push_back(std::make_pair(text.AsString(), SPECIAL));
144}
145
146string EvalString::Serialize() const {
147 string result;
148 if (parsed_.empty() && !single_token_.empty()) {
149 result.append("[");
150 result.append(single_token_);
151 result.append("]");
152 } else {
153 for (const auto& pair : parsed_) {
154 result.append("[");
155 if (pair.second == SPECIAL)
156 result.append("$");
157 result.append(pair.first.begin(), pair.first.end());
158 result.append("]");
159 }
160 }
161 return result;
162}
163
164string EvalString::Unparse() const {
165 string result;
166 if (parsed_.empty() && !single_token_.empty()) {
167 result.append(single_token_.begin(), single_token_.end());
168 } else {
169 for (TokenList::const_iterator i = parsed_.begin();
170 i != parsed_.end(); ++i) {
171 bool special = (i->second == SPECIAL);
172 if (special)
173 result.append("${");
174 result.append(i->first.begin(), i->first.end());
175 if (special)
176 result.append("}");
177 }
178 }
179 return result;
180}
Definition hash_map.h:26
std::string LookupWithFallback(const std::string &var, const EvalString *eval, Env *env)
This is tricky.
Definition eval_env.cc:95
BindingEnv * parent_
Definition eval_env.h:118
void AddBinding(const std::string &key, const std::string &val)
Definition eval_env.cc:30
const Rule * LookupRule(const std::string &rule_name)
Definition eval_env.cc:46
const std::map< std::string, std::unique_ptr< const Rule > > & GetRules() const
Definition eval_env.cc:91
std::map< std::string, std::string > bindings_
Definition eval_env.h:116
std::map< std::string, std::unique_ptr< const Rule > > rules_
Definition eval_env.h:117
const Rule * LookupRuleCurrentScope(const std::string &rule_name)
Definition eval_env.cc:39
virtual std::string LookupVariable(const std::string &var)
Definition eval_env.cc:21
void AddRule(std::unique_ptr< const Rule > rule)
Definition eval_env.cc:34
An interface for a scope for variable (e.g. "$foo") lookups.
Definition eval_env.h:28
virtual std::string LookupVariable(const std::string &var)=0
A tokenized string that contains variable references.
Definition eval_env.h:35
std::string Evaluate(Env *env) const
Definition eval_env.cc:111
std::string Unparse() const
Definition eval_env.cc:164
void AddSpecial(StringPiece text)
Definition eval_env.cc:136
std::string single_token_
Definition eval_env.h:62
std::string Serialize() const
Construct a human-readable representation of the parsed state, for use in tests.
Definition eval_env.cc:146
TokenList parsed_
Definition eval_env.h:56
void AddText(StringPiece text)
Definition eval_env.cc:126
An invocable build command and associated metadata (description, etc.).
Definition eval_env.h:66
const EvalString * GetBinding(const std::string &key) const
Definition eval_env.cc:59
void AddBinding(const std::string &key, const EvalString &val)
Definition eval_env.cc:55
bool phony_
Definition eval_env.h:88
static std::unique_ptr< Rule > Phony()
Definition eval_env.cc:66
Rule(const std::string &name)
Definition eval_env.h:67
bool IsPhony() const
Definition eval_env.cc:72
Bindings bindings_
Definition eval_env.h:87
static bool IsReservedBinding(const std::string &var)
Definition eval_env.cc:77
StringPiece represents a slice of a string whose memory is managed externally.
const_iterator end() const
std::string AsString() const
Convert the slice into a full-fledged std::string, copying the data into a new string.
const_iterator begin() const