Shinichiro Hamaji | 1d545aa | 2015-06-23 15:29:13 +0900 | [diff] [blame] | 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 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 15 | #ifndef EVAL_H_ |
| 16 | #define EVAL_H_ |
| 17 | |
| 18 | #include <unordered_map> |
Shinichiro Hamaji | 7e70801 | 2015-07-31 13:07:34 +0900 | [diff] [blame] | 19 | #include <unordered_set> |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 20 | #include <vector> |
| 21 | |
| 22 | #include "loc.h" |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 23 | #include "stmt.h" |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 24 | #include "string_piece.h" |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 25 | #include "symtab.h" |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 26 | |
| 27 | using namespace std; |
| 28 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 29 | class Makefile; |
| 30 | class Rule; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 31 | class Var; |
| 32 | class Vars; |
| 33 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 34 | class Evaluator { |
| 35 | public: |
Shinichiro Hamaji | c9b9e5e | 2016-02-18 18:18:54 +0900 | [diff] [blame] | 36 | Evaluator(); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 37 | ~Evaluator(); |
| 38 | |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 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); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 45 | |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 46 | Var* LookupVar(Symbol name); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 47 | // For target specific variables. |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 48 | Var* LookupVarInCurrentScope(Symbol name); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 49 | |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 50 | string EvalVar(Symbol name); |
Shinichiro Hamaji | 94d6f2a | 2015-07-05 05:32:25 +0900 | [diff] [blame] | 51 | |
Shinichiro Hamaji | 9619b36 | 2015-06-16 16:13:25 +0900 | [diff] [blame] | 52 | const Loc& loc() const { return loc_; } |
Shinichiro Hamaji | 86e1133 | 2015-07-28 15:24:17 +0900 | [diff] [blame] | 53 | void set_loc(const Loc& loc) { loc_ = loc; } |
Shinichiro Hamaji | 9619b36 | 2015-06-16 16:13:25 +0900 | [diff] [blame] | 54 | |
Shinichiro Hamaji | 7a2659e | 2016-02-08 14:32:56 +0900 | [diff] [blame] | 55 | const vector<const Rule*>& rules() const { return rules_; } |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 56 | const unordered_map<Symbol, Vars*>& rule_vars() const { |
Shinichiro Hamaji | ffc52c3 | 2015-06-23 16:51:07 +0900 | [diff] [blame] | 57 | return rule_vars_; |
| 58 | } |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 59 | const unordered_map<Symbol, bool>& exports() const { return exports_; } |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 60 | |
Shinichiro Hamaji | 9619b36 | 2015-06-16 16:13:25 +0900 | [diff] [blame] | 61 | void Error(const string& msg); |
| 62 | |
Shinichiro Hamaji | 0e74c54 | 2015-06-22 16:17:08 +0900 | [diff] [blame] | 63 | void set_is_bootstrap(bool b) { is_bootstrap_ = b; } |
Shinichiro Hamaji | 1c3a695 | 2016-05-20 16:35:35 +0900 | [diff] [blame] | 64 | void set_is_commandline(bool c) { is_commandline_ = c; } |
Shinichiro Hamaji | 0e74c54 | 2015-06-22 16:17:08 +0900 | [diff] [blame] | 65 | |
Shinichiro Hamaji | ffc52c3 | 2015-06-23 16:51:07 +0900 | [diff] [blame] | 66 | void set_current_scope(Vars* v) { current_scope_ = v; } |
| 67 | |
Shinichiro Hamaji | df1fc8b | 2015-06-29 15:45:21 +0900 | [diff] [blame] | 68 | bool avoid_io() const { return avoid_io_; } |
| 69 | void set_avoid_io(bool a) { avoid_io_ = a; } |
| 70 | |
Shinichiro Hamaji | 86e1133 | 2015-07-28 15:24:17 +0900 | [diff] [blame] | 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 | |
Shinichiro Hamaji | 7e70801 | 2015-07-31 13:07:34 +0900 | [diff] [blame] | 81 | static const unordered_set<Symbol>& used_undefined_vars() { |
| 82 | return used_undefined_vars_; |
| 83 | } |
| 84 | |
Shinichiro Hamaji | 28da237 | 2015-11-30 19:03:53 +0900 | [diff] [blame] | 85 | int eval_depth() const { return eval_depth_; } |
| 86 | void IncrementEvalDepth() { |
| 87 | eval_depth_++; |
| 88 | } |
| 89 | void DecrementEvalDepth() { |
| 90 | eval_depth_--; |
| 91 | } |
| 92 | |
Shinichiro Hamaji | 2941ea0 | 2016-04-27 17:26:21 +0900 | [diff] [blame] | 93 | string GetShell(); |
| 94 | string GetShellFlag(); |
| 95 | string GetShellAndFlag(); |
| 96 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 97 | private: |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 98 | Var* EvalRHS(Symbol lhs, Value* rhs, StringPiece orig_rhs, AssignOp op, |
Shinichiro Hamaji | 420f775 | 2015-06-26 04:02:02 +0900 | [diff] [blame] | 99 | bool is_override = false); |
Shinichiro Hamaji | 0e3873a | 2015-07-05 15:48:28 +0900 | [diff] [blame] | 100 | void DoInclude(const string& fname); |
Shinichiro Hamaji | 6e6de8d | 2015-06-18 11:12:58 +0900 | [diff] [blame] | 101 | |
Shinichiro Hamaji | 7e70801 | 2015-07-31 13:07:34 +0900 | [diff] [blame] | 102 | Var* LookupVarGlobal(Symbol name); |
| 103 | |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 104 | unordered_map<Symbol, Vars*> rule_vars_; |
Shinichiro Hamaji | 7a2659e | 2016-02-08 14:32:56 +0900 | [diff] [blame] | 105 | vector<const Rule*> rules_; |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 106 | unordered_map<Symbol, bool> exports_; |
Shinichiro Hamaji | 45a0c76 | 2015-06-26 06:47:10 +0900 | [diff] [blame] | 107 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 108 | Rule* last_rule_; |
Shinichiro Hamaji | 784b995 | 2015-06-23 14:29:32 +0900 | [diff] [blame] | 109 | Vars* current_scope_; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 110 | |
| 111 | Loc loc_; |
Shinichiro Hamaji | 0e74c54 | 2015-06-22 16:17:08 +0900 | [diff] [blame] | 112 | bool is_bootstrap_; |
Shinichiro Hamaji | 1c3a695 | 2016-05-20 16:35:35 +0900 | [diff] [blame] | 113 | bool is_commandline_; |
Shinichiro Hamaji | 86e1133 | 2015-07-28 15:24:17 +0900 | [diff] [blame] | 114 | |
Shinichiro Hamaji | df1fc8b | 2015-06-29 15:45:21 +0900 | [diff] [blame] | 115 | bool avoid_io_; |
Shinichiro Hamaji | 28da237 | 2015-11-30 19:03:53 +0900 | [diff] [blame] | 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_; |
Shinichiro Hamaji | 86e1133 | 2015-07-28 15:24:17 +0900 | [diff] [blame] | 120 | // Commands which should run at ninja-time (i.e., info, warning, and |
| 121 | // error). |
| 122 | vector<string> delayed_output_commands_; |
Shinichiro Hamaji | 7e70801 | 2015-07-31 13:07:34 +0900 | [diff] [blame] | 123 | |
Shinichiro Hamaji | 2941ea0 | 2016-04-27 17:26:21 +0900 | [diff] [blame] | 124 | Symbol posix_sym_; |
| 125 | bool is_posix_; |
| 126 | |
Shinichiro Hamaji | 7e70801 | 2015-07-31 13:07:34 +0900 | [diff] [blame] | 127 | static unordered_set<Symbol> used_undefined_vars_; |
Dan Willemsen | f87d49e | 2016-09-29 20:09:47 -0700 | [diff] [blame] | 128 | |
| 129 | Symbol kati_readonly_; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | #endif // EVAL_H_ |