blob: adb370086b70ebd91e178fa38c032eab9fcfeb89 [file] [log] [blame]
Shinichiro Hamaji776ca302015-06-06 03:52:48 +09001#ifndef EVAL_H_
2#define EVAL_H_
3
Shinichiro Hamaji0562c302015-06-19 15:30:49 +09004#include <memory>
Shinichiro Hamaji776ca302015-06-06 03:52:48 +09005#include <unordered_map>
6#include <vector>
7
8#include "loc.h"
9#include "string_piece.h"
10
11using namespace std;
12
13class AssignAST;
14class CommandAST;
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +090015class ExportAST;
16class IfAST;
17class IncludeAST;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090018class Makefile;
19class Rule;
20class RuleAST;
21class Var;
22class Vars;
23
24struct EvalResult {
25 ~EvalResult();
Shinichiro Hamaji0562c302015-06-19 15:30:49 +090026 vector<shared_ptr<Rule>> rules;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090027 Vars* vars;
28 unordered_map<StringPiece, Vars*> rule_vars;
29 // TODO: read_mks
30 unordered_map<StringPiece, bool> exports;
31};
32
33class Evaluator {
34 public:
35 Evaluator(const Vars* vars);
36 ~Evaluator();
37
38 void EvalAssign(const AssignAST* ast);
39 void EvalRule(const RuleAST* ast);
40 void EvalCommand(const CommandAST* ast);
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +090041 void EvalIf(const IfAST* ast);
42 void EvalInclude(const IncludeAST* ast);
43 void EvalExport(const ExportAST* ast);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090044
Shinichiro Hamaji6e6de8d2015-06-18 11:12:58 +090045
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090046 Var* LookupVar(StringPiece name);
47 // For target specific variables.
48 Var* LookupVarInCurrentScope(StringPiece name);
49
50 EvalResult* GetEvalResult();
51
Shinichiro Hamaji9619b362015-06-16 16:13:25 +090052 const Loc& loc() const { return loc_; }
53
Shinichiro Hamajicf0cd682015-06-18 16:18:13 +090054 Vars* mutable_vars() { return vars_; }
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090055
Shinichiro Hamaji9619b362015-06-16 16:13:25 +090056 void Error(const string& msg);
57
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090058 private:
Shinichiro Hamaji6e6de8d2015-06-18 11:12:58 +090059 void DoInclude(const char* fname, bool should_exist);
60
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090061 const Vars* in_vars_;
62 Vars* vars_;
63 unordered_map<StringPiece, Vars*> rule_vars_;
Shinichiro Hamaji0562c302015-06-19 15:30:49 +090064 vector<shared_ptr<Rule>> rules_;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090065 Rule* last_rule_;
66
67 Loc loc_;
68};
69
70#endif // EVAL_H_