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.
15 #ifndef NINJA_EVAL_ENV_H_
16 #define NINJA_EVAL_ENV_H_
23 #include "string_piece.h"
27 /// An interface for a scope for variable (e.g. "$foo") lookups.
30 virtual string LookupVariable(const string& var) = 0;
33 /// A tokenized string that contains variable references.
34 /// Can be evaluated relative to an Env.
36 string Evaluate(Env* env) const;
38 void Clear() { parsed_.clear(); }
39 bool empty() const { return parsed_.empty(); }
41 void AddText(StringPiece text);
42 void AddSpecial(StringPiece text);
44 /// Construct a human-readable representation of the parsed state,
46 string Serialize() const;
49 enum TokenType { RAW, SPECIAL };
50 typedef vector<pair<string, TokenType> > TokenList;
54 /// An invokable build command and associated metadata (description, etc.).
56 explicit Rule(const string& name) : name_(name) {}
58 const string& name() const { return name_; }
60 void AddBinding(const string& key, const EvalString& val);
62 static bool IsReservedBinding(const string& var);
64 const EvalString* GetBinding(const string& key) const;
67 // Allow the parsers to reach into this object and fill out its fields.
68 friend struct ManifestParser;
71 typedef map<string, EvalString> Bindings;
75 /// An Env which contains a mapping of variables to values
76 /// as well as a pointer to a parent scope.
77 struct BindingEnv : public Env {
78 BindingEnv() : parent_(NULL) {}
79 explicit BindingEnv(BindingEnv* parent) : parent_(parent) {}
81 virtual ~BindingEnv() {}
82 virtual string LookupVariable(const string& var);
84 void AddRule(const Rule* rule);
85 const Rule* LookupRule(const string& rule_name);
86 const Rule* LookupRuleCurrentScope(const string& rule_name);
87 const map<string, const Rule*>& GetRules() const;
89 void AddBinding(const string& key, const string& val);
91 /// This is tricky. Edges want lookup scope to go in this order:
92 /// 1) value set on edge itself (edge_->env_)
93 /// 2) value set on rule, with expansion in the edge's scope
94 /// 3) value set on enclosing scope of edge (edge_->env_->parent_)
95 /// This function takes as parameters the necessary info to do (2).
96 string LookupWithFallback(const string& var, const EvalString* eval,
100 map<string, string> bindings_;
101 map<string, const Rule*> rules_;
105 #endif // NINJA_EVAL_ENV_H_