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 #ifndef STMT_H_ 16 #define STMT_H_ 17 18 #include <string> 19 #include <vector> 20 21 #include "loc.h" 22 #include "string_piece.h" 23 #include "symtab.h" 24 25 using namespace std; 26 27 class Evaluator; 28 class Value; 29 30 enum struct AssignOp : char { 31 EQ, 32 COLON_EQ, 33 PLUS_EQ, 34 QUESTION_EQ, 35 }; 36 37 enum struct AssignDirective { 38 NONE = 0, 39 OVERRIDE = 1, 40 EXPORT = 2, 41 }; 42 43 enum struct CondOp { 44 IFEQ, 45 IFNEQ, 46 IFDEF, 47 IFNDEF, 48 }; 49 50 struct Stmt { 51 public: 52 virtual ~Stmt(); 53 locStmt54 Loc loc() const { return loc_; } set_locStmt55 void set_loc(Loc loc) { loc_ = loc; } origStmt56 StringPiece orig() const { return orig_; } 57 58 virtual void Eval(Evaluator* ev) const = 0; 59 60 virtual string DebugString() const = 0; 61 62 protected: 63 Stmt(); 64 65 private: 66 Loc loc_; 67 StringPiece orig_; 68 }; 69 70 /* Parsed "rule statement" before evaluation is kept as 71 * <lhs> <sep> <rhs> 72 * where <lhs> and <rhs> as Value instances. <sep> is either command 73 * separator (';') or an assignment ('=' or '=$='). 74 * Until we evaluate <lhs>, we don't know whether it is a rule or 75 * a rule-specific variable assignment. 76 */ 77 struct RuleStmt : public Stmt { 78 Value* lhs; 79 enum { SEP_NULL, SEP_SEMICOLON, SEP_EQ, SEP_FINALEQ } sep; 80 Value* rhs; 81 82 virtual ~RuleStmt(); 83 84 virtual void Eval(Evaluator* ev) const; 85 86 virtual string DebugString() const; 87 }; 88 89 struct AssignStmt : public Stmt { 90 Value* lhs; 91 Value* rhs; 92 StringPiece orig_rhs; 93 AssignOp op; 94 AssignDirective directive; 95 bool is_final; 96 AssignStmtAssignStmt97 AssignStmt() : is_final(false) {} 98 virtual ~AssignStmt(); 99 100 virtual void Eval(Evaluator* ev) const; 101 102 virtual string DebugString() const; 103 104 Symbol GetLhsSymbol(Evaluator* ev) const; 105 106 private: 107 mutable Symbol lhs_sym_cache_; 108 }; 109 110 struct CommandStmt : public Stmt { 111 Value* expr; 112 StringPiece orig; 113 114 virtual ~CommandStmt(); 115 116 virtual void Eval(Evaluator* ev) const; 117 118 virtual string DebugString() const; 119 }; 120 121 struct IfStmt : public Stmt { 122 CondOp op; 123 Value* lhs; 124 Value* rhs; 125 vector<Stmt*> true_stmts; 126 vector<Stmt*> false_stmts; 127 128 virtual ~IfStmt(); 129 130 virtual void Eval(Evaluator* ev) const; 131 132 virtual string DebugString() const; 133 }; 134 135 struct IncludeStmt : public Stmt { 136 Value* expr; 137 bool should_exist; 138 139 virtual ~IncludeStmt(); 140 141 virtual void Eval(Evaluator* ev) const; 142 143 virtual string DebugString() const; 144 }; 145 146 struct ExportStmt : public Stmt { 147 Value* expr; 148 bool is_export; 149 150 virtual ~ExportStmt(); 151 152 virtual void Eval(Evaluator* ev) const; 153 154 virtual string DebugString() const; 155 }; 156 157 struct ParseErrorStmt : public Stmt { 158 string msg; 159 160 virtual ~ParseErrorStmt(); 161 162 virtual void Eval(Evaluator* ev) const; 163 164 virtual string DebugString() const; 165 }; 166 167 #endif // STMT_H_ 168