Imported Upstream version 1.7.1
[platform/upstream/ninja.git] / src / eval_env.cc
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
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_);
113   } else {
114     parsed_.push_back(make_pair(text.AsString(), RAW));
115   }
116 }
117 void EvalString::AddSpecial(StringPiece text) {
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 }