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()
     90       : lhs_sym_cache_(Symbol::IsUninitialized{}) {
     91   }
     92   virtual ~AssignStmt();
     93 
     94   virtual void Eval(Evaluator* ev) const;
     95 
     96   virtual string DebugString() const;
     97 
     98   Symbol GetLhsSymbol(Evaluator* ev) const;
     99 
    100  private:
    101   mutable Symbol lhs_sym_cache_;
    102 };
    103 
    104 struct CommandStmt : public Stmt {
    105   Value* expr;
    106   StringPiece orig;
    107 
    108   virtual ~CommandStmt();
    109 
    110   virtual void Eval(Evaluator* ev) const;
    111 
    112   virtual string DebugString() const;
    113 };
    114 
    115 struct IfStmt : public Stmt {
    116   CondOp op;
    117   Value* lhs;
    118   Value* rhs;
    119   vector<Stmt*> true_stmts;
    120   vector<Stmt*> false_stmts;
    121 
    122   virtual ~IfStmt();
    123 
    124   virtual void Eval(Evaluator* ev) const;
    125 
    126   virtual string DebugString() const;
    127 };
    128 
    129 struct IncludeStmt : public Stmt {
    130   Value* expr;
    131   bool should_exist;
    132 
    133   virtual ~IncludeStmt();
    134 
    135   virtual void Eval(Evaluator* ev) const;
    136 
    137   virtual string DebugString() const;
    138 };
    139 
    140 struct ExportStmt : public Stmt {
    141   Value* expr;
    142   bool is_export;
    143 
    144   virtual ~ExportStmt();
    145 
    146   virtual void Eval(Evaluator* ev) const;
    147 
    148   virtual string DebugString() const;
    149 };
    150 
    151 struct ParseErrorStmt : public Stmt {
    152   string msg;
    153 
    154   virtual ~ParseErrorStmt();
    155 
    156   virtual void Eval(Evaluator* ev) const;
    157 
    158   virtual string DebugString() const;
    159 };
    160 
    161 #endif  // STMT_H_
    162