1 // Copyright 2015 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 EVAL_H_
16 #define EVAL_H_
17 
18 #include <unordered_map>
19 #include <unordered_set>
20 #include <vector>
21 
22 #include "loc.h"
23 #include "stmt.h"
24 #include "string_piece.h"
25 #include "symtab.h"
26 
27 using namespace std;
28 
29 class Makefile;
30 class Rule;
31 class Var;
32 class Vars;
33 
34 class Evaluator {
35  public:
36   Evaluator();
37   ~Evaluator();
38 
39   void EvalAssign(const AssignStmt* stmt);
40   void EvalRule(const RuleStmt* stmt);
41   void EvalCommand(const CommandStmt* stmt);
42   void EvalIf(const IfStmt* stmt);
43   void EvalInclude(const IncludeStmt* stmt);
44   void EvalExport(const ExportStmt* stmt);
45 
46   Var* LookupVar(Symbol name);
47   // For target specific variables.
48   Var* LookupVarInCurrentScope(Symbol name);
49 
50   string EvalVar(Symbol name);
51 
loc()52   const Loc& loc() const { return loc_; }
set_loc(const Loc & loc)53   void set_loc(const Loc& loc) { loc_ = loc; }
54 
rules()55   const vector<const Rule*>& rules() const { return rules_; }
rule_vars()56   const unordered_map<Symbol, Vars*>& rule_vars() const {
57     return rule_vars_;
58   }
exports()59   const unordered_map<Symbol, bool>& exports() const { return exports_; }
60 
61   void Error(const string& msg);
62 
set_is_bootstrap(bool b)63   void set_is_bootstrap(bool b) { is_bootstrap_ = b; }
64 
set_current_scope(Vars * v)65   void set_current_scope(Vars* v) { current_scope_ = v; }
66 
avoid_io()67   bool avoid_io() const { return avoid_io_; }
set_avoid_io(bool a)68   void set_avoid_io(bool a) { avoid_io_ = a; }
69 
delayed_output_commands()70   const vector<string>& delayed_output_commands() const {
71     return delayed_output_commands_;
72   }
add_delayed_output_command(const string & c)73   void add_delayed_output_command(const string& c) {
74     delayed_output_commands_.push_back(c);
75   }
clear_delayed_output_commands()76   void clear_delayed_output_commands() {
77     delayed_output_commands_.clear();
78   }
79 
used_undefined_vars()80   static const unordered_set<Symbol>& used_undefined_vars() {
81     return used_undefined_vars_;
82   }
83 
eval_depth()84   int eval_depth() const { return eval_depth_; }
IncrementEvalDepth()85   void IncrementEvalDepth() {
86     eval_depth_++;
87   }
DecrementEvalDepth()88   void DecrementEvalDepth() {
89     eval_depth_--;
90   }
91 
92  private:
93   Var* EvalRHS(Symbol lhs, Value* rhs, StringPiece orig_rhs, AssignOp op,
94                bool is_override = false);
95   void DoInclude(const string& fname);
96 
97   Var* LookupVarGlobal(Symbol name);
98 
99   unordered_map<Symbol, Vars*> rule_vars_;
100   vector<const Rule*> rules_;
101   unordered_map<Symbol, bool> exports_;
102 
103   Rule* last_rule_;
104   Vars* current_scope_;
105 
106   Loc loc_;
107   bool is_bootstrap_;
108 
109   bool avoid_io_;
110   // This value tracks the nest level of make expressions. For
111   // example, $(YYY) in $(XXX $(YYY)) is evaluated with depth==2.
112   // This will be used to disallow $(shell) in other make constructs.
113   int eval_depth_;
114   // Commands which should run at ninja-time (i.e., info, warning, and
115   // error).
116   vector<string> delayed_output_commands_;
117 
118   static unordered_set<Symbol> used_undefined_vars_;
119 };
120 
121 #endif  // EVAL_H_
122