blob: a9be28f88c16022652445a7ec7e5dd920ff1675e [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
Shinichiro Hamaji0562c302015-06-19 15:30:49 +090018#include <memory>
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090019#include <unordered_map>
20#include <vector>
21
Shinichiro Hamaji784b9952015-06-23 14:29:32 +090022#include "ast.h"
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090023#include "loc.h"
24#include "string_piece.h"
25
26using namespace std;
27
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090028class Makefile;
29class Rule;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090030class Var;
31class Vars;
32
33struct EvalResult {
34 ~EvalResult();
Shinichiro Hamaji0562c302015-06-19 15:30:49 +090035 vector<shared_ptr<Rule>> rules;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090036 Vars* vars;
37 unordered_map<StringPiece, Vars*> rule_vars;
38 // TODO: read_mks
39 unordered_map<StringPiece, bool> exports;
40};
41
42class Evaluator {
43 public:
44 Evaluator(const Vars* vars);
45 ~Evaluator();
46
47 void EvalAssign(const AssignAST* ast);
48 void EvalRule(const RuleAST* ast);
49 void EvalCommand(const CommandAST* ast);
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +090050 void EvalIf(const IfAST* ast);
51 void EvalInclude(const IncludeAST* ast);
52 void EvalExport(const ExportAST* ast);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090053
54 Var* LookupVar(StringPiece name);
55 // For target specific variables.
56 Var* LookupVarInCurrentScope(StringPiece name);
57
58 EvalResult* GetEvalResult();
59
Shinichiro Hamaji9619b362015-06-16 16:13:25 +090060 const Loc& loc() const { return loc_; }
61
Shinichiro Hamajicf0cd682015-06-18 16:18:13 +090062 Vars* mutable_vars() { return vars_; }
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090063
Shinichiro Hamaji9619b362015-06-16 16:13:25 +090064 void Error(const string& msg);
65
Shinichiro Hamaji0e74c542015-06-22 16:17:08 +090066 void set_is_bootstrap(bool b) { is_bootstrap_ = b; }
67
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090068 private:
Shinichiro Hamaji784b9952015-06-23 14:29:32 +090069 void DoAssign(StringPiece lhs, Value* rhs, StringPiece orig_rhs, AssignOp op);
Shinichiro Hamaji6e6de8d2015-06-18 11:12:58 +090070 void DoInclude(const char* fname, bool should_exist);
71
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090072 const Vars* in_vars_;
73 Vars* vars_;
74 unordered_map<StringPiece, Vars*> rule_vars_;
Shinichiro Hamaji0562c302015-06-19 15:30:49 +090075 vector<shared_ptr<Rule>> rules_;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090076 Rule* last_rule_;
Shinichiro Hamaji784b9952015-06-23 14:29:32 +090077 Vars* current_scope_;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090078
79 Loc loc_;
Shinichiro Hamaji0e74c542015-06-22 16:17:08 +090080 bool is_bootstrap_;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090081};
82
83#endif // EVAL_H_