Home | History | Annotate | Download | only in kati
      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 {
     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 
     54   Loc loc() const { return loc_; }
     55   void set_loc(Loc loc) { loc_ = loc; }
     56   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 struct RuleStmt : public Stmt {
     71   Value* expr;
     72   char term;
     73   Value* after_term;
     74 
     75   virtual ~RuleStmt();
     76 
     77   virtual void Eval(Evaluator* ev) const;
     78 
     79   virtual string DebugString() const;
     80 };
     81 
     82 struct AssignStmt : public Stmt {
     83   Value* lhs;
     84   Value* rhs;
     85   StringPiece orig_rhs;
     86   AssignOp op;
     87   AssignDirective directive;
     88 
     89   AssignStmt() : lhs_sym_cache_(Symbol::IsUninitialized{}) {}
     90   virtual ~AssignStmt();
     91 
     92   virtual void Eval(Evaluator* ev) const;
     93 
     94   virtual string DebugString() const;
     95 
     96   Symbol GetLhsSymbol(Evaluator* ev) const;
     97 
     98  private:
     99   mutable Symbol lhs_sym_cache_;
    100 };
    101 
    102 struct CommandStmt : public Stmt {
    103   Value* expr;
    104   StringPiece orig;
    105 
    106   virtual ~CommandStmt();
    107 
    108   virtual void Eval(Evaluator* ev) const;
    109 
    110   virtual string DebugString() const;
    111 };
    112 
    113 struct IfStmt : public Stmt {
    114   CondOp op;
    115   Value* lhs;
    116   Value* rhs;
    117   vector<Stmt*> true_stmts;
    118   vector<Stmt*> false_stmts;
    119 
    120   virtual ~IfStmt();
    121 
    122   virtual void Eval(Evaluator* ev) const;
    123 
    124   virtual string DebugString() const;
    125 };
    126 
    127 struct IncludeStmt : public Stmt {
    128   Value* expr;
    129   bool should_exist;
    130 
    131   virtual ~IncludeStmt();
    132 
    133   virtual void Eval(Evaluator* ev) const;
    134 
    135   virtual string DebugString() const;
    136 };
    137 
    138 struct ExportStmt : public Stmt {
    139   Value* expr;
    140   bool is_export;
    141 
    142   virtual ~ExportStmt();
    143 
    144   virtual void Eval(Evaluator* ev) const;
    145 
    146   virtual string DebugString() const;
    147 };
    148 
    149 struct ParseErrorStmt : public Stmt {
    150   string msg;
    151 
    152   virtual ~ParseErrorStmt();
    153 
    154   virtual void Eval(Evaluator* ev) const;
    155 
    156   virtual string DebugString() const;
    157 };
    158 
    159 #endif  // STMT_H_
    160