blob: d7fd067a5253cca12b0274c426604c10af79a7f0 [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
Shinichiro Hamaji645cca72015-09-24 17:04:21 +090015#ifndef STMT_H_
16#define STMT_H_
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090017
18#include <string>
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +090019#include <vector>
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090020
21#include "loc.h"
22#include "string_piece.h"
Shinichiro Hamaji92a47382016-02-17 17:19:21 +090023#include "symtab.h"
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090024
25using namespace std;
26
27class Evaluator;
Shinichiro Hamajif24ed142015-07-13 20:57:42 -070028class Value;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090029
Sasha Smundakae1d58c2018-08-22 09:39:42 -070030enum struct AssignOp : char {
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090031 EQ,
32 COLON_EQ,
33 PLUS_EQ,
34 QUESTION_EQ,
35};
36
Dan Willemsen3ce083f2017-10-11 22:17:48 -070037enum struct AssignDirective {
Shinichiro Hamaji420f7752015-06-26 04:02:02 +090038 NONE = 0,
39 OVERRIDE = 1,
40 EXPORT = 2,
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090041};
42
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +090043enum struct CondOp {
44 IFEQ,
45 IFNEQ,
46 IFDEF,
47 IFNDEF,
48};
49
Shinichiro Hamaji645cca72015-09-24 17:04:21 +090050struct Stmt {
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090051 public:
Shinichiro Hamaji645cca72015-09-24 17:04:21 +090052 virtual ~Stmt();
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090053
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:
Shinichiro Hamaji645cca72015-09-24 17:04:21 +090063 Stmt();
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090064
65 private:
66 Loc loc_;
67 StringPiece orig_;
68};
69
Sasha Smundakce812f52018-08-15 14:49:39 -070070/* 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 */
Shinichiro Hamaji645cca72015-09-24 17:04:21 +090077struct RuleStmt : public Stmt {
Sasha Smundakce812f52018-08-15 14:49:39 -070078 Value* lhs;
79 enum { SEP_NULL, SEP_SEMICOLON, SEP_EQ, SEP_FINALEQ } sep;
80 Value* rhs;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090081
Shinichiro Hamaji645cca72015-09-24 17:04:21 +090082 virtual ~RuleStmt();
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090083
84 virtual void Eval(Evaluator* ev) const;
85
86 virtual string DebugString() const;
87};
88
Shinichiro Hamaji645cca72015-09-24 17:04:21 +090089struct AssignStmt : public Stmt {
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090090 Value* lhs;
91 Value* rhs;
Shinichiro Hamaji81699be2015-06-22 18:07:38 +090092 StringPiece orig_rhs;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090093 AssignOp op;
94 AssignDirective directive;
Sasha Smundakce812f52018-08-15 14:49:39 -070095 bool is_final;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090096
Sasha Smundakce812f52018-08-15 14:49:39 -070097 AssignStmt() : is_final(false) {}
Shinichiro Hamaji645cca72015-09-24 17:04:21 +090098 virtual ~AssignStmt();
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090099
100 virtual void Eval(Evaluator* ev) const;
101
102 virtual string DebugString() const;
Shinichiro Hamaji92a47382016-02-17 17:19:21 +0900103
104 Symbol GetLhsSymbol(Evaluator* ev) const;
105
106 private:
107 mutable Symbol lhs_sym_cache_;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900108};
109
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900110struct CommandStmt : public Stmt {
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900111 Value* expr;
Shinichiro Hamaji631a9f82015-07-05 14:18:15 +0900112 StringPiece orig;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900113
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900114 virtual ~CommandStmt();
Shinichiro Hamaji776ca302015-06-06 03:52:48 +0900115
116 virtual void Eval(Evaluator* ev) const;
117
118 virtual string DebugString() const;
119};
120
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900121struct IfStmt : public Stmt {
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900122 CondOp op;
123 Value* lhs;
124 Value* rhs;
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900125 vector<Stmt*> true_stmts;
126 vector<Stmt*> false_stmts;
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900127
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900128 virtual ~IfStmt();
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900129
130 virtual void Eval(Evaluator* ev) const;
131
132 virtual string DebugString() const;
133};
134
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900135struct IncludeStmt : public Stmt {
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900136 Value* expr;
Shinichiro Hamajiefad2dd2015-06-17 03:08:02 +0900137 bool should_exist;
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900138
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900139 virtual ~IncludeStmt();
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900140
141 virtual void Eval(Evaluator* ev) const;
142
143 virtual string DebugString() const;
144};
145
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900146struct ExportStmt : public Stmt {
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900147 Value* expr;
148 bool is_export;
149
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900150 virtual ~ExportStmt();
Shinichiro Hamaji42b625f2015-06-16 23:07:21 +0900151
152 virtual void Eval(Evaluator* ev) const;
153
154 virtual string DebugString() const;
155};
156
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900157struct ParseErrorStmt : public Stmt {
Shinichiro Hamaji56227862015-08-05 16:53:37 +0900158 string msg;
159
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900160 virtual ~ParseErrorStmt();
Shinichiro Hamaji56227862015-08-05 16:53:37 +0900161
162 virtual void Eval(Evaluator* ev) const;
163
164 virtual string DebugString() const;
165};
166
Shinichiro Hamaji645cca72015-09-24 17:04:21 +0900167#endif // STMT_H_