-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast_java.h
339 lines (270 loc) · 9.75 KB
/
ast_java.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
* Copyright (C) 2015, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <stdarg.h>
#include <stdio.h>
#include <memory>
#include <optional>
#include <string>
#include <variant>
#include <vector>
enum {
PACKAGE_PRIVATE = 0x00000000,
PUBLIC = 0x00000001,
PRIVATE = 0x00000002,
PROTECTED = 0x00000003,
SCOPE_MASK = 0x00000003,
STATIC = 0x00000010,
FINAL = 0x00000020,
ABSTRACT = 0x00000040,
OVERRIDE = 0x00000100,
ALL_MODIFIERS = 0xffffffff
};
namespace android {
namespace aidl {
class CodeWriter;
} // namespace aidl
} // namespace android
namespace android {
namespace aidl {
namespace java {
// Write the modifiers that are set in both mod and mask
void WriteModifiers(CodeWriter* to, int mod, int mask);
struct AstNode {
AstNode() = default;
virtual ~AstNode() = default;
virtual void Write(CodeWriter* to) const = 0;
std::string ToString();
};
struct ClassElement : public AstNode {
ClassElement() = default;
virtual ~ClassElement() = default;
};
struct Expression : public AstNode {
virtual ~Expression() = default;
};
struct LiteralExpression : public Expression {
std::string value;
explicit LiteralExpression(const std::string& value);
virtual ~LiteralExpression() = default;
void Write(CodeWriter* to) const override;
};
struct StringLiteralExpression : public Expression {
std::string value;
explicit StringLiteralExpression(const std::string& value);
virtual ~StringLiteralExpression() = default;
void Write(CodeWriter* to) const override;
};
struct Variable : public Expression {
std::vector<std::string> annotations;
const std::string type;
std::string name;
Variable() = default;
Variable(const std::string& type, const std::string& name);
virtual ~Variable() = default;
void WriteDeclaration(CodeWriter* to) const;
void Write(CodeWriter* to) const;
};
struct FieldVariable : public Expression {
std::variant<std::shared_ptr<Expression>, std::string> receiver;
std::string name;
FieldVariable(std::shared_ptr<Expression> object, const std::string& name);
FieldVariable(const std::string& clazz, const std::string& name);
virtual ~FieldVariable() = default;
void Write(CodeWriter* to) const;
};
struct Field : public ClassElement {
std::string comment;
std::vector<std::string> annotations;
int modifiers = 0;
std::shared_ptr<Variable> variable = nullptr;
std::string value;
Field() = default;
Field(int modifiers, std::shared_ptr<Variable> variable);
virtual ~Field() = default;
void Write(CodeWriter* to) const override;
};
struct Statement : public AstNode {
virtual ~Statement() = default;
};
struct LiteralStatement : public Statement {
public:
LiteralStatement(const std::string& value);
virtual ~LiteralStatement() = default;
void Write(CodeWriter* to) const override;
private:
const std::string value_;
};
struct StatementBlock : public Statement {
std::vector<std::shared_ptr<Statement>> statements;
StatementBlock() = default;
virtual ~StatementBlock() = default;
void Write(CodeWriter* to) const override;
void Add(std::shared_ptr<Statement> statement);
void Add(std::shared_ptr<Expression> expression);
};
struct ExpressionStatement : public Statement {
std::shared_ptr<Expression> expression;
explicit ExpressionStatement(std::shared_ptr<Expression> expression);
virtual ~ExpressionStatement() = default;
void Write(CodeWriter* to) const override;
};
struct Assignment : public Expression {
std::shared_ptr<Variable> lvalue;
std::shared_ptr<Expression> rvalue;
std::optional<std::string> cast = std::nullopt;
Assignment(std::shared_ptr<Variable> lvalue, std::shared_ptr<Expression> rvalue);
Assignment(std::shared_ptr<Variable> lvalue, std::shared_ptr<Expression> rvalue,
std::string cast);
virtual ~Assignment() = default;
void Write(CodeWriter* to) const override;
};
struct MethodCall : public Expression {
std::variant<std::monostate, std::shared_ptr<Expression>, std::string> receiver;
std::string name;
std::vector<std::shared_ptr<Expression>> arguments;
std::vector<std::string> exceptions;
explicit MethodCall(const std::string& name);
MethodCall(const std::string& name, const std::vector<std::shared_ptr<Expression>>& args);
MethodCall(std::shared_ptr<Expression> obj, const std::string& name);
MethodCall(const std::string& clazz, const std::string& name);
MethodCall(std::shared_ptr<Expression> obj, const std::string& name,
const std::vector<std::shared_ptr<Expression>>& args);
MethodCall(const std::string&, const std::string& name,
const std::vector<std::shared_ptr<Expression>>& args);
virtual ~MethodCall() = default;
void Write(CodeWriter* to) const override;
};
struct Comparison : public Expression {
std::shared_ptr<Expression> lvalue;
std::string op;
std::shared_ptr<Expression> rvalue;
Comparison(std::shared_ptr<Expression> lvalue, const std::string& op,
std::shared_ptr<Expression> rvalue);
virtual ~Comparison() = default;
void Write(CodeWriter* to) const override;
};
struct NewExpression : public Expression {
const std::string instantiableName;
std::vector<std::shared_ptr<Expression>> arguments;
explicit NewExpression(const std::string& name);
NewExpression(const std::string& name, const std::vector<std::shared_ptr<Expression>>& args);
virtual ~NewExpression() = default;
void Write(CodeWriter* to) const override;
};
struct Cast : public Expression {
const std::string type;
std::shared_ptr<Expression> expression = nullptr;
Cast() = default;
Cast(const std::string& type, std::shared_ptr<Expression> expression);
virtual ~Cast() = default;
void Write(CodeWriter* to) const override;
};
struct VariableDeclaration : public Statement {
std::shared_ptr<Variable> lvalue = nullptr;
std::shared_ptr<Expression> rvalue = nullptr;
explicit VariableDeclaration(std::shared_ptr<Variable> lvalue);
VariableDeclaration(std::shared_ptr<Variable> lvalue, std::shared_ptr<Expression> rvalue);
virtual ~VariableDeclaration() = default;
void Write(CodeWriter* to) const override;
};
struct IfStatement : public Statement {
std::shared_ptr<Expression> expression = nullptr;
std::shared_ptr<StatementBlock> statements = std::make_shared<StatementBlock>();
std::shared_ptr<IfStatement> elseif = nullptr;
IfStatement() = default;
virtual ~IfStatement() = default;
void Write(CodeWriter* to) const override;
};
struct ReturnStatement : public Statement {
std::shared_ptr<Expression> expression;
explicit ReturnStatement(std::shared_ptr<Expression> expression);
virtual ~ReturnStatement() = default;
void Write(CodeWriter* to) const override;
};
struct BreakStatement : public Statement {
BreakStatement() = default;
virtual ~BreakStatement() = default;
void Write(CodeWriter* to) const override;
};
struct TryStatement : public Statement {
std::shared_ptr<StatementBlock> statements = std::make_shared<StatementBlock>();
TryStatement() = default;
virtual ~TryStatement() = default;
void Write(CodeWriter* to) const override;
};
struct FinallyStatement : public Statement {
std::shared_ptr<StatementBlock> statements = std::make_shared<StatementBlock>();
FinallyStatement() = default;
virtual ~FinallyStatement() = default;
void Write(CodeWriter* to) const override;
};
struct Case : public AstNode {
std::vector<std::string> cases;
std::shared_ptr<StatementBlock> statements = std::make_shared<StatementBlock>();
Case() = default;
explicit Case(const std::string& c);
virtual ~Case() = default;
void Write(CodeWriter* to) const override;
};
struct SwitchStatement : public Statement {
std::shared_ptr<Expression> expression;
std::vector<std::shared_ptr<Case>> cases;
explicit SwitchStatement(std::shared_ptr<Expression> expression);
virtual ~SwitchStatement() = default;
void Write(CodeWriter* to) const override;
};
struct Method : public ClassElement {
std::string comment;
std::vector<std::string> annotations;
int modifiers = 0;
std::optional<std::string> returnType = std::nullopt; // nullopt means constructor
std::string name;
std::vector<std::shared_ptr<Variable>> parameters;
std::vector<std::string> exceptions;
std::shared_ptr<StatementBlock> statements = nullptr;
Method() = default;
virtual ~Method() = default;
void Write(CodeWriter* to) const override;
};
struct LiteralClassElement : public ClassElement {
std::string element;
LiteralClassElement(std::string e) : element(e) {}
virtual ~LiteralClassElement() = default;
void Write(CodeWriter* to) const override;
};
struct Class : public ClassElement {
enum { CLASS, INTERFACE };
std::string comment;
std::vector<std::string> annotations;
int modifiers = 0;
int what = CLASS; // CLASS or INTERFACE
std::string type;
std::optional<std::string> extends = std::nullopt;
std::vector<std::string> interfaces;
std::vector<std::shared_ptr<ClassElement>> elements;
Class() = default;
virtual ~Class() = default;
void Write(CodeWriter* to) const override;
};
extern std::shared_ptr<Expression> NULL_VALUE;
extern std::shared_ptr<Expression> THIS_VALUE;
extern std::shared_ptr<Expression> SUPER_VALUE;
extern std::shared_ptr<Expression> TRUE_VALUE;
extern std::shared_ptr<Expression> FALSE_VALUE;
} // namespace java
} // namespace aidl
} // namespace android