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