Imported Upstream version 1.7.1
[platform/upstream/ninja.git] / src / eval_env.h
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;
51   TokenList parsed_;
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;
72   Bindings 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_;
102   BindingEnv* parent_;
103 };
104
105 #endif  // NINJA_EVAL_ENV_H_