blob: 8ec04c88b6850e6b69a9705ac5556cd18607c4e6 [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
Fumitoshi Ukai744bb2b2015-06-25 00:10:52 +090015// +build ignore
16
Shinichiro Hamaji645cca72015-09-24 17:04:21 +090017#include "stmt.h"
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090018
19#include "eval.h"
Shinichiro Hamaji645cca72015-09-24 17:04:21 +090020#include "expr.h"
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090021#include "stringprintf.h"
Shinichiro Hamajic195f402015-06-24 14:44:04 +090022#include "strutil.h"
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090023
Shinichiro Hamaji645cca72015-09-24 17:04:21 +090024Stmt::Stmt() {}
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090025
Shinichiro Hamaji645cca72015-09-24 17:04:21 +090026Stmt::~Stmt() {}
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090027
Shinichiro Hamaji645cca72015-09-24 17:04:21 +090028string RuleStmt::DebugString() const {
Sasha Smundakce812f52018-08-15 14:49:39 -070029 return StringPrintf("RuleStmt(lhs=%s sep=%d rhs=%s loc=%s:%d)",
30 Value::DebugString(lhs).c_str(), sep,
31 Value::DebugString(rhs).c_str(), LOCF(loc()));
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090032}
33
Shinichiro Hamaji645cca72015-09-24 17:04:21 +090034string AssignStmt::DebugString() const {
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090035 const char* opstr = "???";
36 switch (op) {
Dan Willemsen3ce083f2017-10-11 22:17:48 -070037 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 Hamaji776ca302015-06-06 03:52:48 +090049 }
50 const char* dirstr = "???";
51 switch (directive) {
Dan Willemsen3ce083f2017-10-11 22:17:48 -070052 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 Hamaji776ca302015-06-06 03:52:48 +090061 }
Dan Willemsen3ce083f2017-10-11 22:17:48 -070062 return StringPrintf(
63 "AssignStmt(lhs=%s rhs=%s (%s) "
64 "opstr=%s dir=%s loc=%s:%d)",
Sasha Smundak8174f9b2018-08-13 11:07:30 -070065 Value::DebugString(lhs).c_str(), Value::DebugString(rhs).c_str(),
Dan Willemsen3ce083f2017-10-11 22:17:48 -070066 NoLineBreak(orig_rhs.as_string()).c_str(), opstr, dirstr, LOCF(loc()));
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090067}
68
Shinichiro Hamaji92a47382016-02-17 17:19:21 +090069Symbol 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 Hamaji645cca72015-09-24 17:04:21 +090082string CommandStmt::DebugString() const {
Dan Willemsenee57a3f2018-11-05 16:18:44 -080083 return StringPrintf("CommandStmt(%s, loc=%s:%d)",
84 Value::DebugString(expr).c_str(), LOCF(loc()));
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090085}
86
Shinichiro Hamaji645cca72015-09-24 17:04:21 +090087string IfStmt::DebugString() const {
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +090088 const char* opstr = "???";
89 switch (op) {
Dan Willemsen3ce083f2017-10-11 22:17:48 -070090 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 Hamaji7e256df2015-06-17 15:33:11 +0900102 }
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900103 return StringPrintf("IfStmt(op=%s, lhs=%s, rhs=%s t=%zu f=%zu loc=%s:%d)",
Sasha Smundak8174f9b2018-08-13 11:07:30 -0700104 opstr, Value::DebugString(lhs).c_str(),
105 Value::DebugString(rhs).c_str(), true_stmts.size(),
Dan Willemsen3ce083f2017-10-11 22:17:48 -0700106 false_stmts.size(), LOCF(loc()));
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900107}
108
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900109string IncludeStmt::DebugString() const {
Dan Willemsenee57a3f2018-11-05 16:18:44 -0800110 return StringPrintf("IncludeStmt(%s, loc=%s:%d)",
111 Value::DebugString(expr).c_str(), LOCF(loc()));
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900112}
113
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900114string ExportStmt::DebugString() const {
115 return StringPrintf("ExportStmt(%s, %d, loc=%s:%d)",
Sasha Smundak8174f9b2018-08-13 11:07:30 -0700116 Value::DebugString(expr).c_str(), is_export, LOCF(loc()));
Shinichiro Hamaji45a0c762015-06-26 06:47:10 +0900117}
118
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900119string ParseErrorStmt::DebugString() const {
Dan Willemsen3ce083f2017-10-11 22:17:48 -0700120 return StringPrintf("ParseErrorStmt(%s, loc=%s:%d)", msg.c_str(),
Shinichiro Hamaji56227862015-08-05 16:53:37 +0900121 LOCF(loc()));
122}
123
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900124RuleStmt::~RuleStmt() {
Sasha Smundakce812f52018-08-15 14:49:39 -0700125 delete lhs;
126 delete rhs;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900127}
128
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900129void RuleStmt::Eval(Evaluator* ev) const {
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900130 ev->EvalRule(this);
131}
132
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900133AssignStmt::~AssignStmt() {
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900134 delete lhs;
135 delete rhs;
136}
137
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900138void AssignStmt::Eval(Evaluator* ev) const {
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900139 ev->EvalAssign(this);
140}
141
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900142CommandStmt::~CommandStmt() {
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900143 delete expr;
144}
145
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900146void CommandStmt::Eval(Evaluator* ev) const {
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900147 ev->EvalCommand(this);
148}
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900149
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900150IfStmt::~IfStmt() {
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900151 delete lhs;
152 delete rhs;
153}
154
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900155void IfStmt::Eval(Evaluator* ev) const {
Shinichiro Hamaji7e256df2015-06-17 15:33:11 +0900156 ev->EvalIf(this);
157}
158
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900159IncludeStmt::~IncludeStmt() {
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900160 delete expr;
161}
162
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900163void IncludeStmt::Eval(Evaluator* ev) const {
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900164 ev->EvalInclude(this);
165}
Shinichiro Hamaji45a0c762015-06-26 06:47:10 +0900166
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900167ExportStmt::~ExportStmt() {
Shinichiro Hamaji45a0c762015-06-26 06:47:10 +0900168 delete expr;
169}
170
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900171void ExportStmt::Eval(Evaluator* ev) const {
Shinichiro Hamaji45a0c762015-06-26 06:47:10 +0900172 ev->EvalExport(this);
173}
Shinichiro Hamaji56227862015-08-05 16:53:37 +0900174
Dan Willemsen3ce083f2017-10-11 22:17:48 -0700175ParseErrorStmt::~ParseErrorStmt() {}
Shinichiro Hamaji56227862015-08-05 16:53:37 +0900176
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900177void ParseErrorStmt::Eval(Evaluator* ev) const {
Shinichiro Hamaji56227862015-08-05 16:53:37 +0900178 ev->set_loc(loc());
179 ev->Error(msg);
180}