• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef AIDL_AST_JAVA_H_
18 #define AIDL_AST_JAVA_H_
19 
20 #include <memory>
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <string>
24 #include <vector>
25 
26 enum {
27   PACKAGE_PRIVATE = 0x00000000,
28   PUBLIC = 0x00000001,
29   PRIVATE = 0x00000002,
30   PROTECTED = 0x00000003,
31   SCOPE_MASK = 0x00000003,
32 
33   STATIC = 0x00000010,
34   FINAL = 0x00000020,
35   ABSTRACT = 0x00000040,
36 
37   OVERRIDE = 0x00000100,
38 
39   ALL_MODIFIERS = 0xffffffff
40 };
41 
42 namespace android {
43 namespace aidl {
44 class CodeWriter;
45 }  // namespace aidl
46 }  // namespace android
47 
48 namespace android {
49 namespace aidl {
50 namespace java {
51 
52 class Type;
53 
54 // Write the modifiers that are set in both mod and mask
55 void WriteModifiers(CodeWriter* to, int mod, int mask);
56 
57 struct ClassElement {
58   ClassElement() = default;
59   virtual ~ClassElement() = default;
60 
61   virtual void Write(CodeWriter* to) const = 0;
62 };
63 
64 struct Expression {
65   virtual ~Expression() = default;
66   virtual void Write(CodeWriter* to) const = 0;
67 };
68 
69 struct LiteralExpression : public Expression {
70   std::string value;
71 
72   explicit LiteralExpression(const std::string& value);
73   virtual ~LiteralExpression() = default;
74   void Write(CodeWriter* to) const override;
75 };
76 
77 // TODO: also escape the contents.  not needed for now
78 struct StringLiteralExpression : public Expression {
79   std::string value;
80 
81   explicit StringLiteralExpression(const std::string& value);
82   virtual ~StringLiteralExpression() = default;
83   void Write(CodeWriter* to) const override;
84 };
85 
86 struct Variable : public Expression {
87   const Type* type = nullptr;
88   std::string name;
89   int dimension = 0;
90 
91   Variable() = default;
92   Variable(const Type* type, const std::string& name);
93   Variable(const Type* type, const std::string& name, int dimension);
94   virtual ~Variable() = default;
95 
96   void WriteDeclaration(CodeWriter* to) const;
97   void Write(CodeWriter* to) const;
98 };
99 
100 struct FieldVariable : public Expression {
101   Expression* object;
102   const Type* clazz;
103   std::string name;
104 
105   FieldVariable(Expression* object, const std::string& name);
106   FieldVariable(const Type* clazz, const std::string& name);
107   virtual ~FieldVariable() = default;
108 
109   void Write(CodeWriter* to) const;
110 };
111 
112 struct Field : public ClassElement {
113   std::string comment;
114   int modifiers = 0;
115   Variable* variable = nullptr;
116   std::string value;
117 
118   Field() = default;
119   Field(int modifiers, Variable* variable);
120   virtual ~Field() = default;
121 
122   void Write(CodeWriter* to) const override;
123 };
124 
125 struct Statement {
126   virtual ~Statement() = default;
127   virtual void Write(CodeWriter* to) const = 0;
128 };
129 
130 struct StatementBlock : public Statement {
131   std::vector<Statement*> statements;
132 
133   StatementBlock() = default;
134   virtual ~StatementBlock() = default;
135   void Write(CodeWriter* to) const override;
136 
137   void Add(Statement* statement);
138   void Add(Expression* expression);
139 };
140 
141 struct ExpressionStatement : public Statement {
142   Expression* expression;
143 
144   explicit ExpressionStatement(Expression* expression);
145   virtual ~ExpressionStatement() = default;
146   void Write(CodeWriter* to) const override;
147 };
148 
149 struct Assignment : public Expression {
150   Variable* lvalue;
151   Expression* rvalue;
152   const Type* cast;
153 
154   Assignment(Variable* lvalue, Expression* rvalue);
155   Assignment(Variable* lvalue, Expression* rvalue, const Type* cast);
156   virtual ~Assignment() = default;
157   void Write(CodeWriter* to) const override;
158 };
159 
160 struct MethodCall : public Expression {
161   Expression* obj = nullptr;
162   const Type* clazz = nullptr;
163   std::string name;
164   std::vector<Expression*> arguments;
165   std::vector<std::string> exceptions;
166 
167   explicit MethodCall(const std::string& name);
168   MethodCall(const std::string& name, int argc, ...);
169   MethodCall(Expression* obj, const std::string& name);
170   MethodCall(const Type* clazz, const std::string& name);
171   MethodCall(Expression* obj, const std::string& name, int argc, ...);
172   MethodCall(const Type* clazz, const std::string& name, int argc, ...);
173   virtual ~MethodCall() = default;
174   void Write(CodeWriter* to) const override;
175 
176  private:
177   void init(int n, va_list args);
178 };
179 
180 struct Comparison : public Expression {
181   Expression* lvalue;
182   std::string op;
183   Expression* rvalue;
184 
185   Comparison(Expression* lvalue, const std::string& op, Expression* rvalue);
186   virtual ~Comparison() = default;
187   void Write(CodeWriter* to) const override;
188 };
189 
190 struct NewExpression : public Expression {
191   const Type* type;
192   std::vector<Expression*> arguments;
193 
194   explicit NewExpression(const Type* type);
195   NewExpression(const Type* type, int argc, ...);
196   virtual ~NewExpression() = default;
197   void Write(CodeWriter* to) const override;
198 
199  private:
200   void init(int n, va_list args);
201 };
202 
203 struct NewArrayExpression : public Expression {
204   const Type* type;
205   Expression* size;
206 
207   NewArrayExpression(const Type* type, Expression* size);
208   virtual ~NewArrayExpression() = default;
209   void Write(CodeWriter* to) const override;
210 };
211 
212 struct Ternary : public Expression {
213   Expression* condition = nullptr;
214   Expression* ifpart = nullptr;
215   Expression* elsepart = nullptr;
216 
217   Ternary() = default;
218   Ternary(Expression* condition, Expression* ifpart, Expression* elsepart);
219   virtual ~Ternary() = default;
220   void Write(CodeWriter* to) const override;
221 };
222 
223 struct Cast : public Expression {
224   const Type* type = nullptr;
225   Expression* expression = nullptr;
226 
227   Cast() = default;
228   Cast(const Type* type, Expression* expression);
229   virtual ~Cast() = default;
230   void Write(CodeWriter* to) const override;
231 };
232 
233 struct VariableDeclaration : public Statement {
234   Variable* lvalue = nullptr;
235   const Type* cast = nullptr;
236   Expression* rvalue = nullptr;
237 
238   explicit VariableDeclaration(Variable* lvalue);
239   VariableDeclaration(Variable* lvalue, Expression* rvalue,
240                       const Type* cast = NULL);
241   virtual ~VariableDeclaration() = default;
242   void Write(CodeWriter* to) const override;
243 };
244 
245 struct IfStatement : public Statement {
246   Expression* expression = nullptr;
247   StatementBlock* statements = new StatementBlock;
248   IfStatement* elseif = nullptr;
249 
250   IfStatement() = default;
251   virtual ~IfStatement() = default;
252   void Write(CodeWriter* to) const override;
253 };
254 
255 struct ReturnStatement : public Statement {
256   Expression* expression;
257 
258   explicit ReturnStatement(Expression* expression);
259   virtual ~ReturnStatement() = default;
260   void Write(CodeWriter* to) const override;
261 };
262 
263 struct TryStatement : public Statement {
264   StatementBlock* statements = new StatementBlock;
265 
266   TryStatement() = default;
267   virtual ~TryStatement() = default;
268   void Write(CodeWriter* to) const override;
269 };
270 
271 struct CatchStatement : public Statement {
272   StatementBlock* statements;
273   Variable* exception;
274 
275   explicit CatchStatement(Variable* exception);
276   virtual ~CatchStatement() = default;
277   void Write(CodeWriter* to) const override;
278 };
279 
280 struct FinallyStatement : public Statement {
281   StatementBlock* statements = new StatementBlock;
282 
283   FinallyStatement() = default;
284   virtual ~FinallyStatement() = default;
285   void Write(CodeWriter* to) const override;
286 };
287 
288 struct Case {
289   std::vector<std::string> cases;
290   StatementBlock* statements = new StatementBlock;
291 
292   Case() = default;
293   explicit Case(const std::string& c);
294   virtual ~Case() = default;
295   virtual void Write(CodeWriter* to) const;
296 };
297 
298 struct SwitchStatement : public Statement {
299   Expression* expression;
300   std::vector<Case*> cases;
301 
302   explicit SwitchStatement(Expression* expression);
303   virtual ~SwitchStatement() = default;
304   void Write(CodeWriter* to) const override;
305 };
306 
307 struct Break : public Statement {
308   Break() = default;
309   virtual ~Break() = default;
310   void Write(CodeWriter* to) const override;
311 };
312 
313 struct Method : public ClassElement {
314   std::string comment;
315   int modifiers = 0;
316   const Type* returnType = nullptr;  // nullptr means constructor
317   size_t returnTypeDimension = 0;
318   std::string name;
319   std::vector<Variable*> parameters;
320   std::vector<const Type*> exceptions;
321   StatementBlock* statements = nullptr;
322 
323   Method() = default;
324   virtual ~Method() = default;
325 
326   void Write(CodeWriter* to) const override;
327 };
328 
329 struct IntConstant : public ClassElement {
330   const std::string name;
331   const int value;
332 
IntConstantIntConstant333   IntConstant(std::string name, int value)
334       : name(name), value(value) {}
335   virtual ~IntConstant() = default;
336 
337   void Write(CodeWriter* to) const override;
338 };
339 
340 struct StringConstant : public ClassElement {
341   const std::string name;
342   const std::string value;
343 
StringConstantStringConstant344   StringConstant(std::string name, std::string value)
345       : name(name), value(value) {}
346   ~StringConstant() override = default;
347 
348   void Write(CodeWriter* to) const override;
349 };
350 
351 struct Class : public ClassElement {
352   enum { CLASS, INTERFACE };
353 
354   std::string comment;
355   int modifiers = 0;
356   int what = CLASS;  // CLASS or INTERFACE
357   const Type* type = nullptr;
358   const Type* extends = nullptr;
359   std::vector<const Type*> interfaces;
360   std::vector<ClassElement*> elements;
361 
362   Class() = default;
363   virtual ~Class() = default;
364 
365   void Write(CodeWriter* to) const override;
366 };
367 
368 class Document {
369  public:
370   Document(const std::string& comment,
371            const std::string& package,
372            const std::string& original_src,
373            std::unique_ptr<Class> clazz);
374   virtual ~Document() = default;
375   virtual void Write(CodeWriter* to) const;
376 
377  private:
378   std::string comment_;
379   std::string package_;
380   std::string original_src_;
381   std::unique_ptr<Class> clazz_;
382 };
383 
384 }  // namespace java
385 }  // namespace aidl
386 }  // namespace android
387 
388 #endif  // AIDL_AST_JAVA_H_
389