blob: 4fb5f0519210d67d02a47a13e0f81619462ccb36 [file] [log] [blame]
Shinichiro Hamaji1d545aa2015-06-23 15:29:13 +09001// 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 Hamaji776ca302015-06-06 03:52:48 +090015#ifndef EVAL_H_
16#define EVAL_H_
17
Dan Willemsenc3f6a972018-02-27 00:25:01 -080018#include <memory>
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090019#include <unordered_map>
Shinichiro Hamaji7e708012015-07-31 13:07:34 +090020#include <unordered_set>
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090021#include <vector>
22
23#include "loc.h"
Shinichiro Hamaji645cca72015-09-24 17:04:21 +090024#include "stmt.h"
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090025#include "string_piece.h"
Shinichiro Hamajie7992752015-06-29 18:38:35 +090026#include "symtab.h"
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090027
28using namespace std;
29
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090030class Makefile;
31class Rule;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090032class Var;
33class Vars;
34
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090035class Evaluator {
36 public:
Shinichiro Hamajic9b9e5e2016-02-18 18:18:54 +090037 Evaluator();
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090038 ~Evaluator();
39
Shinichiro Hamaji645cca72015-09-24 17:04:21 +090040 void EvalAssign(const AssignStmt* stmt);
41 void EvalRule(const RuleStmt* stmt);
42 void EvalCommand(const CommandStmt* stmt);
43 void EvalIf(const IfStmt* stmt);
44 void EvalInclude(const IncludeStmt* stmt);
45 void EvalExport(const ExportStmt* stmt);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090046
Shinichiro Hamajie7992752015-06-29 18:38:35 +090047 Var* LookupVar(Symbol name);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090048 // For target specific variables.
Shinichiro Hamajie7992752015-06-29 18:38:35 +090049 Var* LookupVarInCurrentScope(Symbol name);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090050
Dan Willemsen74197412017-12-27 16:50:09 -080051 // Equivalent to LookupVar, but doesn't mark as used.
52 Var* PeekVar(Symbol name);
53
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +090054 string EvalVar(Symbol name);
Shinichiro Hamaji94d6f2a2015-07-05 05:32:25 +090055
Shinichiro Hamaji9619b362015-06-16 16:13:25 +090056 const Loc& loc() const { return loc_; }
Shinichiro Hamaji86e11332015-07-28 15:24:17 +090057 void set_loc(const Loc& loc) { loc_ = loc; }
Shinichiro Hamaji9619b362015-06-16 16:13:25 +090058
Shinichiro Hamaji7a2659e2016-02-08 14:32:56 +090059 const vector<const Rule*>& rules() const { return rules_; }
Dan Willemsen3ce083f2017-10-11 22:17:48 -070060 const unordered_map<Symbol, Vars*>& rule_vars() const { return rule_vars_; }
Shinichiro Hamajie7992752015-06-29 18:38:35 +090061 const unordered_map<Symbol, bool>& exports() const { return exports_; }
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090062
Shinichiro Hamaji9619b362015-06-16 16:13:25 +090063 void Error(const string& msg);
64
Shinichiro Hamaji0e74c542015-06-22 16:17:08 +090065 void set_is_bootstrap(bool b) { is_bootstrap_ = b; }
Shinichiro Hamaji1c3a6952016-05-20 16:35:35 +090066 void set_is_commandline(bool c) { is_commandline_ = c; }
Shinichiro Hamaji0e74c542015-06-22 16:17:08 +090067
Shinichiro Hamajiffc52c32015-06-23 16:51:07 +090068 void set_current_scope(Vars* v) { current_scope_ = v; }
69
Shinichiro Hamajidf1fc8b2015-06-29 15:45:21 +090070 bool avoid_io() const { return avoid_io_; }
71 void set_avoid_io(bool a) { avoid_io_ = a; }
72
Shinichiro Hamaji86e11332015-07-28 15:24:17 +090073 const vector<string>& delayed_output_commands() const {
74 return delayed_output_commands_;
75 }
76 void add_delayed_output_command(const string& c) {
77 delayed_output_commands_.push_back(c);
78 }
Dan Willemsen3ce083f2017-10-11 22:17:48 -070079 void clear_delayed_output_commands() { delayed_output_commands_.clear(); }
Shinichiro Hamaji86e11332015-07-28 15:24:17 +090080
Shinichiro Hamaji7e708012015-07-31 13:07:34 +090081 static const unordered_set<Symbol>& used_undefined_vars() {
82 return used_undefined_vars_;
83 }
84
Shinichiro Hamaji28da2372015-11-30 19:03:53 +090085 int eval_depth() const { return eval_depth_; }
Dan Willemsen3ce083f2017-10-11 22:17:48 -070086 void IncrementEvalDepth() { eval_depth_++; }
87 void DecrementEvalDepth() { eval_depth_--; }
Shinichiro Hamaji28da2372015-11-30 19:03:53 +090088
Shinichiro Hamaji2941ea02016-04-27 17:26:21 +090089 string GetShell();
90 string GetShellFlag();
91 string GetShellAndFlag();
92
Dan Willemsen36e57292017-10-09 11:23:32 -070093 void CheckStack() {
94 void* addr = __builtin_frame_address(0);
95 if (__builtin_expect(addr < lowest_stack_ && addr >= stack_addr_, 0)) {
96 lowest_stack_ = addr;
97 lowest_loc_ = loc_;
98 }
99 }
100 void DumpStackStats() const;
101
Dan Willemsenc3f6a972018-02-27 00:25:01 -0800102 bool ExportDeprecated() const { return export_message_ && !export_error_; };
103 bool ExportObsolete() const { return export_error_; };
104 void SetExportDeprecated(StringPiece msg) {
105 export_message_.reset(new string(msg.as_string()));
106 }
107 void SetExportObsolete(StringPiece msg) {
108 export_message_.reset(new string(msg.as_string()));
109 export_error_ = true;
110 }
111
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900112 private:
Dan Willemsen3ce083f2017-10-11 22:17:48 -0700113 Var* EvalRHS(Symbol lhs,
114 Value* rhs,
115 StringPiece orig_rhs,
116 AssignOp op,
Shinichiro Hamaji420f7752015-06-26 04:02:02 +0900117 bool is_override = false);
Shinichiro Hamaji0e3873a2015-07-05 15:48:28 +0900118 void DoInclude(const string& fname);
Shinichiro Hamaji6e6de8d2015-06-18 11:12:58 +0900119
Shinichiro Hamaji7e708012015-07-31 13:07:34 +0900120 Var* LookupVarGlobal(Symbol name);
121
Dan Willemsenff90ea32017-11-21 13:22:26 -0800122 // Equivalent to LookupVarInCurrentScope, but doesn't mark as used.
123 Var* PeekVarInCurrentScope(Symbol name);
124
Shinichiro Hamajie7992752015-06-29 18:38:35 +0900125 unordered_map<Symbol, Vars*> rule_vars_;
Shinichiro Hamaji7a2659e2016-02-08 14:32:56 +0900126 vector<const Rule*> rules_;
Shinichiro Hamajie7992752015-06-29 18:38:35 +0900127 unordered_map<Symbol, bool> exports_;
Shinichiro Hamaji45a0c762015-06-26 06:47:10 +0900128
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900129 Rule* last_rule_;
Shinichiro Hamaji784b9952015-06-23 14:29:32 +0900130 Vars* current_scope_;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900131
132 Loc loc_;
Shinichiro Hamaji0e74c542015-06-22 16:17:08 +0900133 bool is_bootstrap_;
Shinichiro Hamaji1c3a6952016-05-20 16:35:35 +0900134 bool is_commandline_;
Shinichiro Hamaji86e11332015-07-28 15:24:17 +0900135
Shinichiro Hamajidf1fc8b2015-06-29 15:45:21 +0900136 bool avoid_io_;
Shinichiro Hamaji28da2372015-11-30 19:03:53 +0900137 // This value tracks the nest level of make expressions. For
138 // example, $(YYY) in $(XXX $(YYY)) is evaluated with depth==2.
139 // This will be used to disallow $(shell) in other make constructs.
140 int eval_depth_;
Shinichiro Hamaji86e11332015-07-28 15:24:17 +0900141 // Commands which should run at ninja-time (i.e., info, warning, and
142 // error).
143 vector<string> delayed_output_commands_;
Shinichiro Hamaji7e708012015-07-31 13:07:34 +0900144
Shinichiro Hamaji2941ea02016-04-27 17:26:21 +0900145 Symbol posix_sym_;
146 bool is_posix_;
147
Dan Willemsen36e57292017-10-09 11:23:32 -0700148 void* stack_addr_;
149 size_t stack_size_;
150 void* lowest_stack_;
151 Loc lowest_loc_;
152
Dan Willemsenc3f6a972018-02-27 00:25:01 -0800153 unique_ptr<string> export_message_;
154 bool export_error_;
155
Shinichiro Hamaji7e708012015-07-31 13:07:34 +0900156 static unordered_set<Symbol> used_undefined_vars_;
Dan Willemsenf87d49e2016-09-29 20:09:47 -0700157
158 Symbol kati_readonly_;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900159};
160
161#endif // EVAL_H_