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
27using namespace std;
28
29class Makefile;
30class Rule;
31class Var;
32class Vars;
33
34class 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
52  const Loc& loc() const { return loc_; }
53  void set_loc(const Loc& loc) { loc_ = loc; }
54
55  const vector<const Rule*>& rules() const { return rules_; }
56  const unordered_map<Symbol, Vars*>& rule_vars() const {
57    return rule_vars_;
58  }
59  const unordered_map<Symbol, bool>& exports() const { return exports_; }
60
61  void Error(const string& msg);
62
63  void set_is_bootstrap(bool b) { is_bootstrap_ = b; }
64  void set_is_commandline(bool c) { is_commandline_ = c; }
65
66  void set_current_scope(Vars* v) { current_scope_ = v; }
67
68  bool avoid_io() const { return avoid_io_; }
69  void set_avoid_io(bool a) { avoid_io_ = a; }
70
71  const vector<string>& delayed_output_commands() const {
72    return delayed_output_commands_;
73  }
74  void add_delayed_output_command(const string& c) {
75    delayed_output_commands_.push_back(c);
76  }
77  void clear_delayed_output_commands() {
78    delayed_output_commands_.clear();
79  }
80
81  static const unordered_set<Symbol>& used_undefined_vars() {
82    return used_undefined_vars_;
83  }
84
85  int eval_depth() const { return eval_depth_; }
86  void IncrementEvalDepth() {
87    eval_depth_++;
88  }
89  void DecrementEvalDepth() {
90    eval_depth_--;
91  }
92
93  string GetShell();
94  string GetShellFlag();
95  string GetShellAndFlag();
96
97 private:
98  Var* EvalRHS(Symbol lhs, Value* rhs, StringPiece orig_rhs, AssignOp op,
99               bool is_override = false);
100  void DoInclude(const string& fname);
101
102  Var* LookupVarGlobal(Symbol name);
103
104  unordered_map<Symbol, Vars*> rule_vars_;
105  vector<const Rule*> rules_;
106  unordered_map<Symbol, bool> exports_;
107
108  Rule* last_rule_;
109  Vars* current_scope_;
110
111  Loc loc_;
112  bool is_bootstrap_;
113  bool is_commandline_;
114
115  bool avoid_io_;
116  // This value tracks the nest level of make expressions. For
117  // example, $(YYY) in $(XXX $(YYY)) is evaluated with depth==2.
118  // This will be used to disallow $(shell) in other make constructs.
119  int eval_depth_;
120  // Commands which should run at ninja-time (i.e., info, warning, and
121  // error).
122  vector<string> delayed_output_commands_;
123
124  Symbol posix_sym_;
125  bool is_posix_;
126
127  static unordered_set<Symbol> used_undefined_vars_;
128
129  Symbol kati_readonly_;
130};
131
132#endif  // EVAL_H_
133