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 15 // +build ignore 16 17 #include "stmt.h" 18 19 #include "eval.h" 20 #include "expr.h" 21 #include "stringprintf.h" 22 #include "strutil.h" 23 24 Stmt::Stmt() {} 25 26 Stmt::~Stmt() {} 27 28 string RuleStmt::DebugString() const { 29 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())); 32 } 33 34 string AssignStmt::DebugString() const { 35 const char* opstr = "???"; 36 switch (op) { 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; 49 } 50 const char* dirstr = "???"; 51 switch (directive) { 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; 61 } 62 return StringPrintf( 63 "AssignStmt(lhs=%s rhs=%s (%s) " 64 "opstr=%s dir=%s loc=%s:%d)", 65 Value::DebugString(lhs).c_str(), Value::DebugString(rhs).c_str(), 66 NoLineBreak(orig_rhs.as_string()).c_str(), opstr, dirstr, LOCF(loc())); 67 } 68 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 82 string CommandStmt::DebugString() const { 83 return StringPrintf("CommandStmt(%s, loc=%s:%d)", 84 Value::DebugString(expr).c_str(), LOCF(loc())); 85 } 86 87 string IfStmt::DebugString() const { 88 const char* opstr = "???"; 89 switch (op) { 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; 102 } 103 return StringPrintf("IfStmt(op=%s, lhs=%s, rhs=%s t=%zu f=%zu loc=%s:%d)", 104 opstr, Value::DebugString(lhs).c_str(), 105 Value::DebugString(rhs).c_str(), true_stmts.size(), 106 false_stmts.size(), LOCF(loc())); 107 } 108 109 string IncludeStmt::DebugString() const { 110 return StringPrintf("IncludeStmt(%s, loc=%s:%d)", 111 Value::DebugString(expr).c_str(), LOCF(loc())); 112 } 113 114 string ExportStmt::DebugString() const { 115 return StringPrintf("ExportStmt(%s, %d, loc=%s:%d)", 116 Value::DebugString(expr).c_str(), is_export, LOCF(loc())); 117 } 118 119 string ParseErrorStmt::DebugString() const { 120 return StringPrintf("ParseErrorStmt(%s, loc=%s:%d)", msg.c_str(), 121 LOCF(loc())); 122 } 123 124 RuleStmt::~RuleStmt() { 125 delete lhs; 126 delete rhs; 127 } 128 129 void RuleStmt::Eval(Evaluator* ev) const { 130 ev->EvalRule(this); 131 } 132 133 AssignStmt::~AssignStmt() { 134 delete lhs; 135 delete rhs; 136 } 137 138 void AssignStmt::Eval(Evaluator* ev) const { 139 ev->EvalAssign(this); 140 } 141 142 CommandStmt::~CommandStmt() { 143 delete expr; 144 } 145 146 void CommandStmt::Eval(Evaluator* ev) const { 147 ev->EvalCommand(this); 148 } 149 150 IfStmt::~IfStmt() { 151 delete lhs; 152 delete rhs; 153 } 154 155 void IfStmt::Eval(Evaluator* ev) const { 156 ev->EvalIf(this); 157 } 158 159 IncludeStmt::~IncludeStmt() { 160 delete expr; 161 } 162 163 void IncludeStmt::Eval(Evaluator* ev) const { 164 ev->EvalInclude(this); 165 } 166 167 ExportStmt::~ExportStmt() { 168 delete expr; 169 } 170 171 void ExportStmt::Eval(Evaluator* ev) const { 172 ev->EvalExport(this); 173 } 174 175 ParseErrorStmt::~ParseErrorStmt() {} 176 177 void ParseErrorStmt::Eval(Evaluator* ev) const { 178 ev->set_loc(loc()); 179 ev->Error(msg); 180 } 181