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 
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 
28 void BindingEnv::AddBinding(const string& key, const string& val) {
29  bindings_[key] = val;
30 }
31 
32 void BindingEnv::AddRule(const Rule* rule) {
33  assert(LookupRuleCurrentScope(rule->name()) == NULL);
34  rules_[rule->name()] = rule;
35 }
36 
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 
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 
53 void Rule::AddBinding(const string& key, const EvalString& val) {
54  bindings_[key] = val;
55 }
56 
57 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
65 bool Rule::IsReservedBinding(const string& var) {
66  return var == "command" ||
67  var == "depfile" ||
68  var == "description" ||
69  var == "deps" ||
70  var == "generator" ||
71  var == "pool" ||
72  var == "restat" ||
73  var == "rspfile" ||
74  var == "rspfile_content" ||
75  var == "msvc_deps_prefix";
76 }
77 
78 const map<string, const Rule*>& BindingEnv::GetRules() const {
79  return rules_;
80 }
81 
82 string BindingEnv::LookupWithFallback(const string& var,
83  const EvalString* eval,
84  Env* env) {
85  map<string, string>::iterator i = bindings_.find(var);
86  if (i != bindings_.end())
87  return i->second;
88 
89  if (eval)
90  return eval->Evaluate(env);
91 
92  if (parent_)
93  return parent_->LookupVariable(var);
94 
95  return "";
96 }
97 
98 string EvalString::Evaluate(Env* env) const {
99  string result;
100  for (TokenList::const_iterator i = parsed_.begin(); i != parsed_.end(); ++i) {
101  if (i->second == RAW)
102  result.append(i->first);
103  else
104  result.append(env->LookupVariable(i->first));
105  }
106  return result;
107 }
108 
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_);
113  } else {
114  parsed_.push_back(make_pair(text.AsString(), RAW));
115  }
116 }
118  parsed_.push_back(make_pair(text.AsString(), SPECIAL));
119 }
120 
121 string EvalString::Serialize() const {
122  string result;
123  for (TokenList::const_iterator i = parsed_.begin();
124  i != parsed_.end(); ++i) {
125  result.append("[");
126  if (i->second == SPECIAL)
127  result.append("$");
128  result.append(i->first);
129  result.append("]");
130  }
131  return result;
132 }
static bool IsReservedBinding(const string &var)
Definition: eval_env.cc:65
string Evaluate(Env *env) const
Definition: eval_env.cc:98
const char * str_
Definition: string_piece.h:67
const map< string, const Rule * > & GetRules() const
Definition: eval_env.cc:78
map< string, const Rule * > rules_
Definition: eval_env.h:101
map< string, string > bindings_
Definition: eval_env.h:100
const Rule * LookupRule(const string &rule_name)
Definition: eval_env.cc:44
StringPiece represents a slice of a string whose memory is managed externally.
Definition: string_piece.h:27
const Rule * LookupRuleCurrentScope(const string &rule_name)
Definition: eval_env.cc:37
void AddSpecial(StringPiece text)
Definition: eval_env.cc:117
void AddRule(const Rule *rule)
Definition: eval_env.cc:32
virtual string LookupVariable(const string &var)=0
An invokable build command and associated metadata (description, etc.).
Definition: eval_env.h:55
virtual string LookupVariable(const string &var)
Definition: eval_env.cc:19
string LookupWithFallback(const string &var, const EvalString *eval, Env *env)
This is tricky.
Definition: eval_env.cc:82
const EvalString * GetBinding(const string &key) const
Definition: eval_env.cc:57
BindingEnv * parent_
Definition: eval_env.h:102
void AddBinding(const string &key, const EvalString &val)
Definition: eval_env.cc:53
string Serialize() const
Construct a human-readable representation of the parsed state, for use in tests.
Definition: eval_env.cc:121
const string & name() const
Definition: eval_env.h:58
void AddBinding(const string &key, const string &val)
Definition: eval_env.cc:28
void AddText(StringPiece text)
Definition: eval_env.cc:109
size_t len_
Definition: string_piece.h:68
Bindings bindings_
Definition: eval_env.h:72
A tokenized string that contains variable references.
Definition: eval_env.h:35
An interface for a scope for variable (e.g. "$foo") lookups.
Definition: eval_env.h:28
string AsString() const
Convert the slice into a full-fledged std::string, copying the data into a new string.
Definition: string_piece.h:47
TokenList parsed_
Definition: eval_env.h:51