• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
LookupVariable(const string & var)19 string BindingEnv::LookupVariable(const string& var) {
20   map<string, string>::iterator i = bindings_.find(var);
21   if (i != bindings_.end())
22     return i->second;
23   if (parent_)
24     return parent_->LookupVariable(var);
25   return "";
26 }
27 
AddBinding(const string & key,const string & val)28 void BindingEnv::AddBinding(const string& key, const string& val) {
29   bindings_[key] = val;
30 }
31 
AddRule(const Rule * rule)32 void BindingEnv::AddRule(const Rule* rule) {
33   assert(LookupRuleCurrentScope(rule->name()) == NULL);
34   rules_[rule->name()] = rule;
35 }
36 
LookupRuleCurrentScope(const string & rule_name)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())
40     return NULL;
41   return i->second;
42 }
43 
LookupRule(const string & rule_name)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())
47     return i->second;
48   if (parent_)
49     return parent_->LookupRule(rule_name);
50   return NULL;
51 }
52 
AddBinding(const string & key,const EvalString & val)53 void Rule::AddBinding(const string& key, const EvalString& val) {
54   bindings_[key] = val;
55 }
56 
GetBinding(const string & key) const57 const EvalString* Rule::GetBinding(const string& key) const {
58   Bindings::const_iterator i = bindings_.find(key);
59   if (i == bindings_.end())
60     return NULL;
61   return &i->second;
62 }
63 
64 // static
IsReservedBinding(const string & var)65 bool Rule::IsReservedBinding(const string& var) {
66   return var == "command" ||
67       var == "depfile" ||
68       var == "dyndep" ||
69       var == "description" ||
70       var == "deps" ||
71       var == "generator" ||
72       var == "pool" ||
73       var == "restat" ||
74       var == "rspfile" ||
75       var == "rspfile_content" ||
76       var == "msvc_deps_prefix";
77 }
78 
GetRules() const79 const map<string, const Rule*>& BindingEnv::GetRules() const {
80   return rules_;
81 }
82 
LookupWithFallback(const string & var,const EvalString * eval,Env * env)83 string BindingEnv::LookupWithFallback(const string& var,
84                                       const EvalString* eval,
85                                       Env* env) {
86   map<string, string>::iterator i = bindings_.find(var);
87   if (i != bindings_.end())
88     return i->second;
89 
90   if (eval)
91     return eval->Evaluate(env);
92 
93   if (parent_)
94     return parent_->LookupVariable(var);
95 
96   return "";
97 }
98 
Evaluate(Env * env) const99 string EvalString::Evaluate(Env* env) const {
100   string result;
101   for (TokenList::const_iterator i = parsed_.begin(); i != parsed_.end(); ++i) {
102     if (i->second == RAW)
103       result.append(i->first);
104     else
105       result.append(env->LookupVariable(i->first));
106   }
107   return result;
108 }
109 
AddText(StringPiece text)110 void EvalString::AddText(StringPiece text) {
111   // Add it to the end of an existing RAW token if possible.
112   if (!parsed_.empty() && parsed_.back().second == RAW) {
113     parsed_.back().first.append(text.str_, text.len_);
114   } else {
115     parsed_.push_back(make_pair(text.AsString(), RAW));
116   }
117 }
AddSpecial(StringPiece text)118 void EvalString::AddSpecial(StringPiece text) {
119   parsed_.push_back(make_pair(text.AsString(), SPECIAL));
120 }
121 
Serialize() const122 string EvalString::Serialize() const {
123   string result;
124   for (TokenList::const_iterator i = parsed_.begin();
125        i != parsed_.end(); ++i) {
126     result.append("[");
127     if (i->second == SPECIAL)
128       result.append("$");
129     result.append(i->first);
130     result.append("]");
131   }
132   return result;
133 }
134 
Unparse() const135 string EvalString::Unparse() const {
136   string result;
137   for (TokenList::const_iterator i = parsed_.begin();
138        i != parsed_.end(); ++i) {
139     bool special = (i->second == SPECIAL);
140     if (special)
141       result.append("${");
142     result.append(i->first);
143     if (special)
144       result.append("}");
145   }
146   return result;
147 }
148