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 <memory>
20#include <string>
21#include <vector>
22
23#include "string_piece.h"
24
25struct Rule;
26
27/// An interface for a scope for variable (e.g. "$foo") lookups.
28struct Env {
29 virtual ~Env() {}
30 virtual std::string LookupVariable(const std::string& var) = 0;
31};
32
33/// A tokenized string that contains variable references.
34/// Can be evaluated relative to an Env.
35struct EvalString {
36 /// @return The evaluated string with variable expanded using value found in
37 /// environment @a env.
38 std::string Evaluate(Env* env) const;
39
40 /// @return The string with variables not expanded.
41 std::string Unparse() const;
42
43 void Clear() { parsed_.clear(); single_token_.clear(); }
44 bool empty() const { return parsed_.empty() && single_token_.empty(); }
45
46 void AddText(StringPiece text);
47 void AddSpecial(StringPiece text);
48
49 /// Construct a human-readable representation of the parsed state,
50 /// for use in tests.
51 std::string Serialize() const;
52
53private:
55 typedef std::vector<std::pair<std::string, TokenType> > TokenList;
57
58 // If we hold only a single RAW token, then we keep it here instead of
59 // pushing it on TokenList. This saves a bunch of allocations for
60 // what is a common case. If parsed_ is nonempty, then this value
61 // must be ignored.
62 std::string single_token_;
63};
64
65/// An invocable build command and associated metadata (description, etc.).
66struct Rule {
67 explicit Rule(const std::string& name) : name_(name) {}
68
69 static std::unique_ptr<Rule> Phony();
70
71 bool IsPhony() const;
72
73 const std::string& name() const { return name_; }
74
75 void AddBinding(const std::string& key, const EvalString& val);
76
77 static bool IsReservedBinding(const std::string& var);
78
79 const EvalString* GetBinding(const std::string& key) const;
80
81 private:
82 // Allow the parsers to reach into this object and fill out its fields.
83 friend struct ManifestParser;
84
85 std::string name_;
86 typedef std::map<std::string, EvalString> Bindings;
88 bool phony_ = false;
89};
90
91/// An Env which contains a mapping of variables to values
92/// as well as a pointer to a parent scope.
93struct BindingEnv : public Env {
94 BindingEnv() : parent_(NULL) {}
95 explicit BindingEnv(BindingEnv* parent) : parent_(parent) {}
96
97 virtual ~BindingEnv() {}
98 virtual std::string LookupVariable(const std::string& var);
99
100 void AddRule(std::unique_ptr<const Rule> rule);
101 const Rule* LookupRule(const std::string& rule_name);
102 const Rule* LookupRuleCurrentScope(const std::string& rule_name);
103 const std::map<std::string, std::unique_ptr<const Rule>>& GetRules() const;
104
105 void AddBinding(const std::string& key, const std::string& val);
106
107 /// This is tricky. Edges want lookup scope to go in this order:
108 /// 1) value set on edge itself (edge_->env_)
109 /// 2) value set on rule, with expansion in the edge's scope
110 /// 3) value set on enclosing scope of edge (edge_->env_->parent_)
111 /// This function takes as parameters the necessary info to do (2).
112 std::string LookupWithFallback(const std::string& var, const EvalString* eval,
113 Env* env);
114
115private:
116 std::map<std::string, std::string> bindings_;
117 std::map<std::string, std::unique_ptr<const Rule>> rules_;
119};
120
121#endif // NINJA_EVAL_ENV_H_
std::string LookupWithFallback(const std::string &var, const EvalString *eval, Env *env)
This is tricky.
Definition eval_env.cc:95
BindingEnv * parent_
Definition eval_env.h:118
void AddBinding(const std::string &key, const std::string &val)
Definition eval_env.cc:30
BindingEnv(BindingEnv *parent)
Definition eval_env.h:95
const Rule * LookupRule(const std::string &rule_name)
Definition eval_env.cc:46
const std::map< std::string, std::unique_ptr< const Rule > > & GetRules() const
Definition eval_env.cc:91
std::map< std::string, std::string > bindings_
Definition eval_env.h:116
std::map< std::string, std::unique_ptr< const Rule > > rules_
Definition eval_env.h:117
const Rule * LookupRuleCurrentScope(const std::string &rule_name)
Definition eval_env.cc:39
virtual std::string LookupVariable(const std::string &var)
Definition eval_env.cc:21
virtual ~BindingEnv()
Definition eval_env.h:97
void AddRule(std::unique_ptr< const Rule > rule)
Definition eval_env.cc:34
An interface for a scope for variable (e.g. "$foo") lookups.
Definition eval_env.h:28
virtual ~Env()
Definition eval_env.h:29
virtual std::string LookupVariable(const std::string &var)=0
A tokenized string that contains variable references.
Definition eval_env.h:35
void Clear()
Definition eval_env.h:43
bool empty() const
Definition eval_env.h:44
std::string Evaluate(Env *env) const
Definition eval_env.cc:111
std::string Unparse() const
Definition eval_env.cc:164
void AddSpecial(StringPiece text)
Definition eval_env.cc:136
std::string single_token_
Definition eval_env.h:62
std::string Serialize() const
Construct a human-readable representation of the parsed state, for use in tests.
Definition eval_env.cc:146
std::vector< std::pair< std::string, TokenType > > TokenList
Definition eval_env.h:55
TokenList parsed_
Definition eval_env.h:56
void AddText(StringPiece text)
Definition eval_env.cc:126
An invocable build command and associated metadata (description, etc.).
Definition eval_env.h:66
std::map< std::string, EvalString > Bindings
Definition eval_env.h:86
const EvalString * GetBinding(const std::string &key) const
Definition eval_env.cc:59
void AddBinding(const std::string &key, const EvalString &val)
Definition eval_env.cc:55
bool phony_
Definition eval_env.h:88
static std::unique_ptr< Rule > Phony()
Definition eval_env.cc:66
std::string name_
Definition eval_env.h:85
Rule(const std::string &name)
Definition eval_env.h:67
bool IsPhony() const
Definition eval_env.cc:72
Bindings bindings_
Definition eval_env.h:87
friend struct ManifestParser
Definition eval_env.h:83
static bool IsReservedBinding(const std::string &var)
Definition eval_env.cc:77
const std::string & name() const
Definition eval_env.h:73
StringPiece represents a slice of a string whose memory is managed externally.