1 // Copyright 2011 Google Inc. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 string BindingEnv::LookupVariable(const string& var) {
20 map<string, string>::iterator i = bindings_.find(var);
21 if (i != bindings_.end())
24 return parent_->LookupVariable(var);
28 void BindingEnv::AddBinding(const string& key, const string& val) {
32 void BindingEnv::AddRule(const Rule* rule) {
33 assert(LookupRuleCurrentScope(rule->name()) == NULL);
34 rules_[rule->name()] = rule;
37 const Rule* BindingEnv::LookupRuleCurrentScope(const string& rule_name) {
38 map<string, const Rule*>::iterator i = rules_.find(rule_name);
39 if (i == rules_.end())
44 const Rule* BindingEnv::LookupRule(const string& rule_name) {
45 map<string, const Rule*>::iterator i = rules_.find(rule_name);
46 if (i != rules_.end())
49 return parent_->LookupRule(rule_name);
53 void Rule::AddBinding(const string& key, const EvalString& val) {
57 const EvalString* Rule::GetBinding(const string& key) const {
58 Bindings::const_iterator i = bindings_.find(key);
59 if (i == bindings_.end())
65 bool Rule::IsReservedBinding(const string& var) {
66 return var == "command" ||
68 var == "description" ||
74 var == "rspfile_content" ||
75 var == "msvc_deps_prefix";
78 const map<string, const Rule*>& BindingEnv::GetRules() const {
82 string BindingEnv::LookupWithFallback(const string& var,
83 const EvalString* eval,
85 map<string, string>::iterator i = bindings_.find(var);
86 if (i != bindings_.end())
90 return eval->Evaluate(env);
93 return parent_->LookupVariable(var);
98 string EvalString::Evaluate(Env* env) const {
100 for (TokenList::const_iterator i = parsed_.begin(); i != parsed_.end(); ++i) {
101 if (i->second == RAW)
102 result.append(i->first);
104 result.append(env->LookupVariable(i->first));
109 void EvalString::AddText(StringPiece text) {
110 // Add it to the end of an existing RAW token if possible.
111 if (!parsed_.empty() && parsed_.back().second == RAW) {
112 parsed_.back().first.append(text.str_, text.len_);
114 parsed_.push_back(make_pair(text.AsString(), RAW));
117 void EvalString::AddSpecial(StringPiece text) {
118 parsed_.push_back(make_pair(text.AsString(), SPECIAL));
121 string EvalString::Serialize() const {
123 for (TokenList::const_iterator i = parsed_.begin();
124 i != parsed_.end(); ++i) {
126 if (i->second == SPECIAL)
128 result.append(i->first);