Ninja
eval_env.h
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 #ifndef NINJA_EVAL_ENV_H_
16 #define NINJA_EVAL_ENV_H_
17 
18 #include <map>
19 #include <string>
20 #include <vector>
21 using namespace std;
22 
23 #include "string_piece.h"
24 
25 struct Rule;
26 
27 /// An interface for a scope for variable (e.g. "$foo") lookups.
28 struct Env {
29  virtual ~Env() {}
30  virtual string LookupVariable(const string& var) = 0;
31 };
32 
33 /// A tokenized string that contains variable references.
34 /// Can be evaluated relative to an Env.
35 struct EvalString {
36  string Evaluate(Env* env) const;
37 
38  void Clear() { parsed_.clear(); }
39  bool empty() const { return parsed_.empty(); }
40 
41  void AddText(StringPiece text);
42  void AddSpecial(StringPiece text);
43 
44  /// Construct a human-readable representation of the parsed state,
45  /// for use in tests.
46  string Serialize() const;
47 
48 private:
49  enum TokenType { RAW, SPECIAL };
50  typedef vector<pair<string, TokenType> > TokenList;
52 };
53 
54 /// An invokable build command and associated metadata (description, etc.).
55 struct Rule {
56  explicit Rule(const string& name) : name_(name) {}
57 
58  const string& name() const { return name_; }
59 
60  void AddBinding(const string& key, const EvalString& val);
61 
62  static bool IsReservedBinding(const string& var);
63 
64  const EvalString* GetBinding(const string& key) const;
65 
66  private:
67  // Allow the parsers to reach into this object and fill out its fields.
68  friend struct ManifestParser;
69 
70  string name_;
71  typedef map<string, EvalString> Bindings;
73 };
74 
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) {}
80 
81  virtual ~BindingEnv() {}
82  virtual string LookupVariable(const string& var);
83 
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;
88 
89  void AddBinding(const string& key, const string& val);
90 
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,
97  Env* env);
98 
99 private:
100  map<string, string> bindings_;
101  map<string, const Rule*> rules_;
103 };
104 
105 #endif // NINJA_EVAL_ENV_H_
BindingEnv(BindingEnv *parent)
Definition: eval_env.h:79
vector< pair< string, TokenType > > TokenList
Definition: eval_env.h:50
map< string, const Rule * > rules_
Definition: eval_env.h:101
map< string, string > bindings_
Definition: eval_env.h:100
BindingEnv()
Definition: eval_env.h:78
void Clear()
Definition: eval_env.h:38
StringPiece represents a slice of a string whose memory is managed externally.
Definition: string_piece.h:27
bool empty() const
Definition: eval_env.h:39
virtual ~BindingEnv()
Definition: eval_env.h:81
Parses .ninja files.
An Env which contains a mapping of variables to values as well as a pointer to a parent scope...
Definition: eval_env.h:77
An invokable build command and associated metadata (description, etc.).
Definition: eval_env.h:55
virtual ~Env()
Definition: eval_env.h:29
BindingEnv * parent_
Definition: eval_env.h:102
map< string, EvalString > Bindings
Definition: eval_env.h:71
string name_
Definition: eval_env.h:70
const string & name() const
Definition: eval_env.h:58
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
TokenList parsed_
Definition: eval_env.h:51
Rule(const string &name)
Definition: eval_env.h:56