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 | |
Shinichiro Hamaji | 0562c30 | 2015-06-19 15:30:49 +0900 | [diff] [blame] | 18 | #include <memory> |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 19 | #include <unordered_map> |
| 20 | #include <vector> |
| 21 | |
Shinichiro Hamaji | 784b995 | 2015-06-23 14:29:32 +0900 | [diff] [blame] | 22 | #include "ast.h" |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 23 | #include "loc.h" |
| 24 | #include "string_piece.h" |
| 25 | |
| 26 | using namespace std; |
| 27 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 28 | class Makefile; |
| 29 | class Rule; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 30 | class Var; |
| 31 | class Vars; |
| 32 | |
| 33 | struct EvalResult { |
| 34 | ~EvalResult(); |
Shinichiro Hamaji | 0562c30 | 2015-06-19 15:30:49 +0900 | [diff] [blame] | 35 | vector<shared_ptr<Rule>> rules; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 36 | Vars* vars; |
| 37 | unordered_map<StringPiece, Vars*> rule_vars; |
| 38 | // TODO: read_mks |
| 39 | unordered_map<StringPiece, bool> exports; |
| 40 | }; |
| 41 | |
| 42 | class 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 Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 50 | void EvalIf(const IfAST* ast); |
| 51 | void EvalInclude(const IncludeAST* ast); |
| 52 | void EvalExport(const ExportAST* ast); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 53 | |
| 54 | Var* LookupVar(StringPiece name); |
| 55 | // For target specific variables. |
| 56 | Var* LookupVarInCurrentScope(StringPiece name); |
| 57 | |
| 58 | EvalResult* GetEvalResult(); |
| 59 | |
Shinichiro Hamaji | 9619b36 | 2015-06-16 16:13:25 +0900 | [diff] [blame] | 60 | const Loc& loc() const { return loc_; } |
| 61 | |
Shinichiro Hamaji | cf0cd68 | 2015-06-18 16:18:13 +0900 | [diff] [blame] | 62 | Vars* mutable_vars() { return vars_; } |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 63 | |
Shinichiro Hamaji | 9619b36 | 2015-06-16 16:13:25 +0900 | [diff] [blame] | 64 | void Error(const string& msg); |
| 65 | |
Shinichiro Hamaji | 0e74c54 | 2015-06-22 16:17:08 +0900 | [diff] [blame] | 66 | void set_is_bootstrap(bool b) { is_bootstrap_ = b; } |
| 67 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 68 | private: |
Shinichiro Hamaji | 784b995 | 2015-06-23 14:29:32 +0900 | [diff] [blame] | 69 | void DoAssign(StringPiece lhs, Value* rhs, StringPiece orig_rhs, AssignOp op); |
Shinichiro Hamaji | 6e6de8d | 2015-06-18 11:12:58 +0900 | [diff] [blame] | 70 | void DoInclude(const char* fname, bool should_exist); |
| 71 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 72 | const Vars* in_vars_; |
| 73 | Vars* vars_; |
| 74 | unordered_map<StringPiece, Vars*> rule_vars_; |
Shinichiro Hamaji | 0562c30 | 2015-06-19 15:30:49 +0900 | [diff] [blame] | 75 | vector<shared_ptr<Rule>> rules_; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 76 | Rule* last_rule_; |
Shinichiro Hamaji | 784b995 | 2015-06-23 14:29:32 +0900 | [diff] [blame] | 77 | Vars* current_scope_; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 78 | |
| 79 | Loc loc_; |
Shinichiro Hamaji | 0e74c54 | 2015-06-22 16:17:08 +0900 | [diff] [blame] | 80 | bool is_bootstrap_; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | #endif // EVAL_H_ |