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 | |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 15 | // +build ignore |
| 16 | |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 17 | #include "stmt.h" |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 18 | |
| 19 | #include "eval.h" |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 20 | #include "expr.h" |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 21 | #include "stringprintf.h" |
Shinichiro Hamaji | c195f40 | 2015-06-24 14:44:04 +0900 | [diff] [blame] | 22 | #include "strutil.h" |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 23 | |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 24 | Stmt::Stmt() {} |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 25 | |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 26 | Stmt::~Stmt() {} |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 27 | |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 28 | string RuleStmt::DebugString() const { |
| 29 | return StringPrintf("RuleStmt(expr=%s term=%d after_term=%s loc=%s:%d)", |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 30 | expr->DebugString().c_str(), |
| 31 | term, |
Shinichiro Hamaji | 8ee8c37 | 2015-06-16 16:19:40 +0900 | [diff] [blame] | 32 | after_term->DebugString().c_str(), |
| 33 | LOCF(loc())); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 34 | } |
| 35 | |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 36 | string AssignStmt::DebugString() const { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 37 | const char* opstr = "???"; |
| 38 | switch (op) { |
| 39 | case AssignOp::EQ: opstr = "EQ"; break; |
| 40 | case AssignOp::COLON_EQ: opstr = "COLON_EQ"; break; |
| 41 | case AssignOp::PLUS_EQ: opstr = "PLUS_EQ"; break; |
| 42 | case AssignOp::QUESTION_EQ: opstr = "QUESTION_EQ"; break; |
| 43 | } |
| 44 | const char* dirstr = "???"; |
| 45 | switch (directive) { |
| 46 | case AssignDirective::NONE: dirstr = ""; break; |
| 47 | case AssignDirective::OVERRIDE: dirstr = "override"; break; |
| 48 | case AssignDirective::EXPORT: dirstr = "export"; break; |
| 49 | } |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 50 | return StringPrintf("AssignStmt(lhs=%s rhs=%s (%s) " |
Shinichiro Hamaji | 81699be | 2015-06-22 18:07:38 +0900 | [diff] [blame] | 51 | "opstr=%s dir=%s loc=%s:%d)", |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 52 | lhs->DebugString().c_str(), |
| 53 | rhs->DebugString().c_str(), |
Shinichiro Hamaji | c195f40 | 2015-06-24 14:44:04 +0900 | [diff] [blame] | 54 | NoLineBreak(orig_rhs.as_string()).c_str(), |
Shinichiro Hamaji | 8ee8c37 | 2015-06-16 16:19:40 +0900 | [diff] [blame] | 55 | opstr, dirstr, LOCF(loc())); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 56 | } |
| 57 | |
Shinichiro Hamaji | 92a4738 | 2016-02-17 17:19:21 +0900 | [diff] [blame] | 58 | Symbol AssignStmt::GetLhsSymbol(Evaluator* ev) const { |
| 59 | if (!lhs->IsLiteral()) { |
| 60 | string buf; |
| 61 | lhs->Eval(ev, &buf); |
| 62 | return Intern(buf); |
| 63 | } |
| 64 | |
| 65 | if (!lhs_sym_cache_.IsValid()) { |
| 66 | lhs_sym_cache_ = Intern(lhs->GetLiteralValueUnsafe()); |
| 67 | } |
| 68 | return lhs_sym_cache_; |
| 69 | } |
| 70 | |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 71 | string CommandStmt::DebugString() const { |
| 72 | return StringPrintf("CommandStmt(%s, loc=%s:%d)", |
Shinichiro Hamaji | 8ee8c37 | 2015-06-16 16:19:40 +0900 | [diff] [blame] | 73 | expr->DebugString().c_str(), LOCF(loc())); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 74 | } |
| 75 | |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 76 | string IfStmt::DebugString() const { |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 77 | const char* opstr = "???"; |
| 78 | switch (op) { |
| 79 | case CondOp::IFEQ: opstr = "ifeq"; break; |
| 80 | case CondOp::IFNEQ: opstr = "ifneq"; break; |
| 81 | case CondOp::IFDEF: opstr = "ifdef"; break; |
| 82 | case CondOp::IFNDEF: opstr = "ifndef"; break; |
| 83 | } |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 84 | return StringPrintf("IfStmt(op=%s, lhs=%s, rhs=%s t=%zu f=%zu loc=%s:%d)", |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 85 | opstr, |
| 86 | lhs->DebugString().c_str(), |
| 87 | rhs->DebugString().c_str(), |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 88 | true_stmts.size(), |
| 89 | false_stmts.size(), |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 90 | LOCF(loc())); |
| 91 | } |
| 92 | |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 93 | string IncludeStmt::DebugString() const { |
| 94 | return StringPrintf("IncludeStmt(%s, loc=%s:%d)", |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 95 | expr->DebugString().c_str(), LOCF(loc())); |
| 96 | } |
| 97 | |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 98 | string ExportStmt::DebugString() const { |
| 99 | return StringPrintf("ExportStmt(%s, %d, loc=%s:%d)", |
Shinichiro Hamaji | 45a0c76 | 2015-06-26 06:47:10 +0900 | [diff] [blame] | 100 | expr->DebugString().c_str(), |
| 101 | is_export, |
| 102 | LOCF(loc())); |
| 103 | } |
| 104 | |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 105 | string ParseErrorStmt::DebugString() const { |
| 106 | return StringPrintf("ParseErrorStmt(%s, loc=%s:%d)", |
Shinichiro Hamaji | 5622786 | 2015-08-05 16:53:37 +0900 | [diff] [blame] | 107 | msg.c_str(), |
| 108 | LOCF(loc())); |
| 109 | } |
| 110 | |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 111 | RuleStmt::~RuleStmt() { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 112 | delete expr; |
| 113 | delete after_term; |
| 114 | } |
| 115 | |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 116 | void RuleStmt::Eval(Evaluator* ev) const { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 117 | ev->EvalRule(this); |
| 118 | } |
| 119 | |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 120 | AssignStmt::~AssignStmt() { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 121 | delete lhs; |
| 122 | delete rhs; |
| 123 | } |
| 124 | |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 125 | void AssignStmt::Eval(Evaluator* ev) const { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 126 | ev->EvalAssign(this); |
| 127 | } |
| 128 | |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 129 | CommandStmt::~CommandStmt() { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 130 | delete expr; |
| 131 | } |
| 132 | |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 133 | void CommandStmt::Eval(Evaluator* ev) const { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 134 | ev->EvalCommand(this); |
| 135 | } |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 136 | |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 137 | IfStmt::~IfStmt() { |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 138 | delete lhs; |
| 139 | delete rhs; |
| 140 | } |
| 141 | |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 142 | void IfStmt::Eval(Evaluator* ev) const { |
Shinichiro Hamaji | 7e256df | 2015-06-17 15:33:11 +0900 | [diff] [blame] | 143 | ev->EvalIf(this); |
| 144 | } |
| 145 | |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 146 | IncludeStmt::~IncludeStmt() { |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 147 | delete expr; |
| 148 | } |
| 149 | |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 150 | void IncludeStmt::Eval(Evaluator* ev) const { |
Shinichiro Hamaji | 42b625f | 2015-06-16 23:07:21 +0900 | [diff] [blame] | 151 | ev->EvalInclude(this); |
| 152 | } |
Shinichiro Hamaji | 45a0c76 | 2015-06-26 06:47:10 +0900 | [diff] [blame] | 153 | |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 154 | ExportStmt::~ExportStmt() { |
Shinichiro Hamaji | 45a0c76 | 2015-06-26 06:47:10 +0900 | [diff] [blame] | 155 | delete expr; |
| 156 | } |
| 157 | |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 158 | void ExportStmt::Eval(Evaluator* ev) const { |
Shinichiro Hamaji | 45a0c76 | 2015-06-26 06:47:10 +0900 | [diff] [blame] | 159 | ev->EvalExport(this); |
| 160 | } |
Shinichiro Hamaji | 5622786 | 2015-08-05 16:53:37 +0900 | [diff] [blame] | 161 | |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 162 | ParseErrorStmt::~ParseErrorStmt() { |
Shinichiro Hamaji | 5622786 | 2015-08-05 16:53:37 +0900 | [diff] [blame] | 163 | } |
| 164 | |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 165 | void ParseErrorStmt::Eval(Evaluator* ev) const { |
Shinichiro Hamaji | 5622786 | 2015-08-05 16:53:37 +0900 | [diff] [blame] | 166 | ev->set_loc(loc()); |
| 167 | ev->Error(msg); |
| 168 | } |