-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast_java.h
388 lines (306 loc) · 9.79 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
* 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.
*/
#ifndef AIDL_AST_JAVA_H_
#define AIDL_AST_JAVA_H_
#include <memory>
#include <stdarg.h>
#include <stdio.h>
#include <string>
#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 {
class Type;
// Write the modifiers that are set in both mod and mask
void WriteModifiers(CodeWriter* to, int mod, int mask);
struct ClassElement {
ClassElement() = default;
virtual ~ClassElement() = default;
virtual void Write(CodeWriter* to) const = 0;
};
struct Expression {
virtual ~Expression() = default;
virtual void Write(CodeWriter* to) const = 0;
};
struct LiteralExpression : public Expression {
std::string value;
explicit LiteralExpression(const std::string& value);
virtual ~LiteralExpression() = default;
void Write(CodeWriter* to) const override;
};
// TODO: also escape the contents. not needed for now
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 {
const Type* type = nullptr;
std::string name;
int dimension = 0;
Variable() = default;
Variable(const Type* type, const std::string& name);
Variable(const Type* type, const std::string& name, int dimension);
virtual ~Variable() = default;
void WriteDeclaration(CodeWriter* to) const;
void Write(CodeWriter* to) const;
};
struct FieldVariable : public Expression {
Expression* object;
const Type* clazz;
std::string name;
FieldVariable(Expression* object, const std::string& name);
FieldVariable(const Type* clazz, const std::string& name);
virtual ~FieldVariable() = default;
void Write(CodeWriter* to) const;
};
struct Field : public ClassElement {
std::string comment;
int modifiers = 0;
Variable* variable = nullptr;
std::string value;
Field() = default;
Field(int modifiers, Variable* variable);
virtual ~Field() = default;
void Write(CodeWriter* to) const override;
};
struct Statement {
virtual ~Statement() = default;
virtual void Write(CodeWriter* to) const = 0;
};
struct StatementBlock : public Statement {
std::vector<Statement*> statements;
StatementBlock() = default;
virtual ~StatementBlock() = default;
void Write(CodeWriter* to) const override;
void Add(Statement* statement);
void Add(Expression* expression);
};
struct ExpressionStatement : public Statement {
Expression* expression;
explicit ExpressionStatement(Expression* expression);
virtual ~ExpressionStatement() = default;
void Write(CodeWriter* to) const override;
};
struct Assignment : public Expression {
Variable* lvalue;
Expression* rvalue;
const Type* cast;
Assignment(Variable* lvalue, Expression* rvalue);
Assignment(Variable* lvalue, Expression* rvalue, const Type* cast);
virtual ~Assignment() = default;
void Write(CodeWriter* to) const override;
};
struct MethodCall : public Expression {
Expression* obj = nullptr;
const Type* clazz = nullptr;
std::string name;
std::vector<Expression*> arguments;
std::vector<std::string> exceptions;
explicit MethodCall(const std::string& name);
MethodCall(const std::string& name, int argc, ...);
MethodCall(Expression* obj, const std::string& name);
MethodCall(const Type* clazz, const std::string& name);
MethodCall(Expression* obj, const std::string& name, int argc, ...);
MethodCall(const Type* clazz, const std::string& name, int argc, ...);
virtual ~MethodCall() = default;
void Write(CodeWriter* to) const override;
private:
void init(int n, va_list args);
};
struct Comparison : public Expression {
Expression* lvalue;
std::string op;
Expression* rvalue;
Comparison(Expression* lvalue, const std::string& op, Expression* rvalue);
virtual ~Comparison() = default;
void Write(CodeWriter* to) const override;
};
struct NewExpression : public Expression {
const Type* type;
std::vector<Expression*> arguments;
explicit NewExpression(const Type* type);
NewExpression(const Type* type, int argc, ...);
virtual ~NewExpression() = default;
void Write(CodeWriter* to) const override;
private:
void init(int n, va_list args);
};
struct NewArrayExpression : public Expression {
const Type* type;
Expression* size;
NewArrayExpression(const Type* type, Expression* size);
virtual ~NewArrayExpression() = default;
void Write(CodeWriter* to) const override;
};
struct Ternary : public Expression {
Expression* condition = nullptr;
Expression* ifpart = nullptr;
Expression* elsepart = nullptr;
Ternary() = default;
Ternary(Expression* condition, Expression* ifpart, Expression* elsepart);
virtual ~Ternary() = default;
void Write(CodeWriter* to) const override;
};
struct Cast : public Expression {
const Type* type = nullptr;
Expression* expression = nullptr;
Cast() = default;
Cast(const Type* type, Expression* expression);
virtual ~Cast() = default;
void Write(CodeWriter* to) const override;
};
struct VariableDeclaration : public Statement {
Variable* lvalue = nullptr;
const Type* cast = nullptr;
Expression* rvalue = nullptr;
explicit VariableDeclaration(Variable* lvalue);
VariableDeclaration(Variable* lvalue, Expression* rvalue,
const Type* cast = NULL);
virtual ~VariableDeclaration() = default;
void Write(CodeWriter* to) const override;
};
struct IfStatement : public Statement {
Expression* expression = nullptr;
StatementBlock* statements = new StatementBlock;
IfStatement* elseif = nullptr;
IfStatement() = default;
virtual ~IfStatement() = default;
void Write(CodeWriter* to) const override;
};
struct ReturnStatement : public Statement {
Expression* expression;
explicit ReturnStatement(Expression* expression);
virtual ~ReturnStatement() = default;
void Write(CodeWriter* to) const override;
};
struct TryStatement : public Statement {
StatementBlock* statements = new StatementBlock;
TryStatement() = default;
virtual ~TryStatement() = default;
void Write(CodeWriter* to) const override;
};
struct CatchStatement : public Statement {
StatementBlock* statements;
Variable* exception;
explicit CatchStatement(Variable* exception);
virtual ~CatchStatement() = default;
void Write(CodeWriter* to) const override;
};
struct FinallyStatement : public Statement {
StatementBlock* statements = new StatementBlock;
FinallyStatement() = default;
virtual ~FinallyStatement() = default;
void Write(CodeWriter* to) const override;
};
struct Case {
std::vector<std::string> cases;
StatementBlock* statements = new StatementBlock;
Case() = default;
explicit Case(const std::string& c);
virtual ~Case() = default;
virtual void Write(CodeWriter* to) const;
};
struct SwitchStatement : public Statement {
Expression* expression;
std::vector<Case*> cases;
explicit SwitchStatement(Expression* expression);
virtual ~SwitchStatement() = default;
void Write(CodeWriter* to) const override;
};
struct Break : public Statement {
Break() = default;
virtual ~Break() = default;
void Write(CodeWriter* to) const override;
};
struct Method : public ClassElement {
std::string comment;
int modifiers = 0;
const Type* returnType = nullptr; // nullptr means constructor
size_t returnTypeDimension = 0;
std::string name;
std::vector<Variable*> parameters;
std::vector<const Type*> exceptions;
StatementBlock* statements = nullptr;
Method() = default;
virtual ~Method() = default;
void Write(CodeWriter* to) const override;
};
struct IntConstant : public ClassElement {
const std::string name;
const int value;
IntConstant(std::string name, int value)
: name(name), value(value) {}
virtual ~IntConstant() = default;
void Write(CodeWriter* to) const override;
};
struct StringConstant : public ClassElement {
const std::string name;
const std::string value;
StringConstant(std::string name, std::string value)
: name(name), value(value) {}
~StringConstant() override = default;
void Write(CodeWriter* to) const override;
};
struct Class : public ClassElement {
enum { CLASS, INTERFACE };
std::string comment;
int modifiers = 0;
int what = CLASS; // CLASS or INTERFACE
const Type* type = nullptr;
const Type* extends = nullptr;
std::vector<const Type*> interfaces;
std::vector<ClassElement*> elements;
Class() = default;
virtual ~Class() = default;
void Write(CodeWriter* to) const override;
};
class Document {
public:
Document(const std::string& comment,
const std::string& package,
const std::string& original_src,
std::unique_ptr<Class> clazz);
virtual ~Document() = default;
virtual void Write(CodeWriter* to) const;
private:
std::string comment_;
std::string package_;
std::string original_src_;
std::unique_ptr<Class> clazz_;
};
} // namespace java
} // namespace aidl
} // namespace android
#endif // AIDL_AST_JAVA_H_