• 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 #include "ast_java.h"
18 #include "code_writer.h"
19 
20 using std::vector;
21 using std::string;
22 
23 template <class... Ts>
24 struct overloaded : Ts... {
25   using Ts::operator()...;
26 };
27 template <class... Ts>
28 overloaded(Ts...)->overloaded<Ts...>;
29 
30 namespace android {
31 namespace aidl {
32 namespace java {
33 
ToString()34 std::string AstNode::ToString() {
35   std::string str;
36   Write(CodeWriter::ForString(&str).get());
37   return str;
38 }
39 
WriteModifiers(CodeWriter * to,int mod,int mask)40 void WriteModifiers(CodeWriter* to, int mod, int mask) {
41   int m = mod & mask;
42 
43   if (m & OVERRIDE) {
44     to->Write("@Override ");
45   }
46 
47   if ((m & SCOPE_MASK) == PUBLIC) {
48     to->Write("public ");
49   } else if ((m & SCOPE_MASK) == PRIVATE) {
50     to->Write("private ");
51   } else if ((m & SCOPE_MASK) == PROTECTED) {
52     to->Write("protected ");
53   }
54 
55   if (m & STATIC) {
56     to->Write("static ");
57   }
58 
59   if (m & FINAL) {
60     to->Write("final ");
61   }
62 
63   if (m & ABSTRACT) {
64     to->Write("abstract ");
65   }
66 }
67 
WriteArgumentList(CodeWriter * to,const vector<Expression * > & arguments)68 void WriteArgumentList(CodeWriter* to, const vector<Expression*>& arguments) {
69   size_t N = arguments.size();
70   for (size_t i = 0; i < N; i++) {
71     arguments[i]->Write(to);
72     if (i != N - 1) {
73       to->Write(", ");
74     }
75   }
76 }
77 
Field(int m,Variable * v)78 Field::Field(int m, Variable* v) : ClassElement(), modifiers(m), variable(v) {}
79 
Write(CodeWriter * to) const80 void Field::Write(CodeWriter* to) const {
81   if (this->comment.length() != 0) {
82     to->Write("%s\n", this->comment.c_str());
83   }
84   for (const auto& a : this->annotations) {
85     to->Write("%s\n", a.c_str());
86   }
87   WriteModifiers(to, this->modifiers, SCOPE_MASK | STATIC | FINAL | OVERRIDE);
88   this->variable->WriteDeclaration(to);
89 
90   if (this->value.length() != 0) {
91     to->Write(" = %s", this->value.c_str());
92   }
93   to->Write(";\n");
94 }
95 
LiteralExpression(const string & v)96 LiteralExpression::LiteralExpression(const string& v) : value(v) {}
97 
Write(CodeWriter * to) const98 void LiteralExpression::Write(CodeWriter* to) const {
99   to->Write("%s", this->value.c_str());
100 }
101 
StringLiteralExpression(const string & v)102 StringLiteralExpression::StringLiteralExpression(const string& v) : value(v) {}
103 
Write(CodeWriter * to) const104 void StringLiteralExpression::Write(CodeWriter* to) const {
105   to->Write("\"%s\"", this->value.c_str());
106 }
107 
Variable(const string & t,const string & n)108 Variable::Variable(const string& t, const string& n) : type(t), name(n), dimension(0) {}
109 
Variable(const string & t,const string & n,int d)110 Variable::Variable(const string& t, const string& n, int d) : type(t), name(n), dimension(d) {}
111 
WriteDeclaration(CodeWriter * to) const112 void Variable::WriteDeclaration(CodeWriter* to) const {
113   string dim;
114   for (int i = 0; i < this->dimension; i++) {
115     dim += "[]";
116   }
117   to->Write("%s%s %s", this->type.c_str(), dim.c_str(), this->name.c_str());
118 }
119 
Write(CodeWriter * to) const120 void Variable::Write(CodeWriter* to) const { to->Write("%s", name.c_str()); }
121 
FieldVariable(Expression * o,const string & n)122 FieldVariable::FieldVariable(Expression* o, const string& n) : receiver(o), name(n) {}
123 
FieldVariable(const string & c,const string & n)124 FieldVariable::FieldVariable(const string& c, const string& n) : receiver(c), name(n) {}
125 
Write(CodeWriter * to) const126 void FieldVariable::Write(CodeWriter* to) const {
127   visit(
128       overloaded{[&](Expression* e) { e->Write(to); },
129                  [&](const std::string& s) { to->Write("%s", s.c_str()); }, [](std::monostate) {}},
130       this->receiver);
131   to->Write(".%s", name.c_str());
132 }
133 
LiteralStatement(const std::string & value)134 LiteralStatement::LiteralStatement(const std::string& value) : value_(value) {}
135 
Write(CodeWriter * to) const136 void LiteralStatement::Write(CodeWriter* to) const {
137   to->Write("%s", value_.c_str());
138 }
139 
Write(CodeWriter * to) const140 void StatementBlock::Write(CodeWriter* to) const {
141   to->Write("{\n");
142   to->Indent();
143   int N = this->statements.size();
144   for (int i = 0; i < N; i++) {
145     this->statements[i]->Write(to);
146   }
147   to->Dedent();
148   to->Write("}\n");
149 }
150 
Add(Statement * statement)151 void StatementBlock::Add(Statement* statement) {
152   this->statements.push_back(statement);
153 }
154 
Add(Expression * expression)155 void StatementBlock::Add(Expression* expression) {
156   this->statements.push_back(new ExpressionStatement(expression));
157 }
158 
ExpressionStatement(Expression * e)159 ExpressionStatement::ExpressionStatement(Expression* e) : expression(e) {}
160 
Write(CodeWriter * to) const161 void ExpressionStatement::Write(CodeWriter* to) const {
162   this->expression->Write(to);
163   to->Write(";\n");
164 }
165 
Assignment(Variable * l,Expression * r)166 Assignment::Assignment(Variable* l, Expression* r) : lvalue(l), rvalue(r) {}
167 
Assignment(Variable * l,Expression * r,string c)168 Assignment::Assignment(Variable* l, Expression* r, string c) : lvalue(l), rvalue(r), cast(c) {}
169 
Write(CodeWriter * to) const170 void Assignment::Write(CodeWriter* to) const {
171   this->lvalue->Write(to);
172   to->Write(" = ");
173   if (this->cast) {
174     to->Write("(%s)", this->cast->c_str());
175   }
176   this->rvalue->Write(to);
177 }
178 
MethodCall(const string & n)179 MethodCall::MethodCall(const string& n) : name(n) {}
180 
MethodCall(const string & n,int argc=0,...)181 MethodCall::MethodCall(const string& n, int argc = 0, ...) : name(n) {
182   va_list args;
183   va_start(args, argc);
184   init(argc, args);
185   va_end(args);
186 }
187 
MethodCall(Expression * o,const string & n)188 MethodCall::MethodCall(Expression* o, const string& n) : receiver(o), name(n) {}
189 
MethodCall(const std::string & t,const string & n)190 MethodCall::MethodCall(const std::string& t, const string& n) : receiver(t), name(n) {}
191 
MethodCall(Expression * o,const string & n,int argc=0,...)192 MethodCall::MethodCall(Expression* o, const string& n, int argc = 0, ...) : receiver(o), name(n) {
193   va_list args;
194   va_start(args, argc);
195   init(argc, args);
196   va_end(args);
197 }
198 
MethodCall(const std::string & t,const string & n,int argc=0,...)199 MethodCall::MethodCall(const std::string& t, const string& n, int argc = 0, ...)
200     : receiver(t), name(n) {
201   va_list args;
202   va_start(args, argc);
203   init(argc, args);
204   va_end(args);
205 }
206 
init(int n,va_list args)207 void MethodCall::init(int n, va_list args) {
208   for (int i = 0; i < n; i++) {
209     Expression* expression = (Expression*)va_arg(args, void*);
210     this->arguments.push_back(expression);
211   }
212 }
213 
Write(CodeWriter * to) const214 void MethodCall::Write(CodeWriter* to) const {
215   visit(
216       overloaded{[&](Expression* e) {
217                    e->Write(to);
218                    to->Write(".");
219                  },
220                  [&](const std::string& s) { to->Write("%s.", s.c_str()); }, [](std::monostate) {}},
221       this->receiver);
222   to->Write("%s(", this->name.c_str());
223   WriteArgumentList(to, this->arguments);
224   to->Write(")");
225 }
226 
Comparison(Expression * l,const string & o,Expression * r)227 Comparison::Comparison(Expression* l, const string& o, Expression* r)
228     : lvalue(l), op(o), rvalue(r) {}
229 
Write(CodeWriter * to) const230 void Comparison::Write(CodeWriter* to) const {
231   to->Write("(");
232   this->lvalue->Write(to);
233   to->Write("%s", this->op.c_str());
234   this->rvalue->Write(to);
235   to->Write(")");
236 }
237 
NewExpression(const std::string & n)238 NewExpression::NewExpression(const std::string& n) : instantiableName(n) {}
239 
NewExpression(const std::string & n,int argc=0,...)240 NewExpression::NewExpression(const std::string& n, int argc = 0, ...) : instantiableName(n) {
241   va_list args;
242   va_start(args, argc);
243   init(argc, args);
244   va_end(args);
245 }
246 
init(int n,va_list args)247 void NewExpression::init(int n, va_list args) {
248   for (int i = 0; i < n; i++) {
249     Expression* expression = (Expression*)va_arg(args, void*);
250     this->arguments.push_back(expression);
251   }
252 }
253 
Write(CodeWriter * to) const254 void NewExpression::Write(CodeWriter* to) const {
255   to->Write("new %s(", this->instantiableName.c_str());
256   WriteArgumentList(to, this->arguments);
257   to->Write(")");
258 }
259 
NewArrayExpression(const std::string & t,Expression * s)260 NewArrayExpression::NewArrayExpression(const std::string& t, Expression* s) : type(t), size(s) {}
261 
Write(CodeWriter * to) const262 void NewArrayExpression::Write(CodeWriter* to) const {
263   to->Write("new %s[", this->type.c_str());
264   size->Write(to);
265   to->Write("]");
266 }
267 
Cast(const std::string & t,Expression * e)268 Cast::Cast(const std::string& t, Expression* e) : type(t), expression(e) {}
269 
Write(CodeWriter * to) const270 void Cast::Write(CodeWriter* to) const {
271   to->Write("((%s)", this->type.c_str());
272   expression->Write(to);
273   to->Write(")");
274 }
275 
VariableDeclaration(Variable * l,Expression * r)276 VariableDeclaration::VariableDeclaration(Variable* l, Expression* r) : lvalue(l), rvalue(r) {}
277 
VariableDeclaration(Variable * l)278 VariableDeclaration::VariableDeclaration(Variable* l) : lvalue(l) {}
279 
Write(CodeWriter * to) const280 void VariableDeclaration::Write(CodeWriter* to) const {
281   this->lvalue->WriteDeclaration(to);
282   if (this->rvalue != nullptr) {
283     to->Write(" = ");
284     this->rvalue->Write(to);
285   }
286   to->Write(";\n");
287 }
288 
Write(CodeWriter * to) const289 void IfStatement::Write(CodeWriter* to) const {
290   if (this->expression != nullptr) {
291     to->Write("if (");
292     this->expression->Write(to);
293     to->Write(") ");
294   }
295   this->statements->Write(to);
296   if (this->elseif != nullptr) {
297     to->Write("else ");
298     this->elseif->Write(to);
299   }
300 }
301 
ReturnStatement(Expression * e)302 ReturnStatement::ReturnStatement(Expression* e) : expression(e) {}
303 
Write(CodeWriter * to) const304 void ReturnStatement::Write(CodeWriter* to) const {
305   to->Write("return ");
306   this->expression->Write(to);
307   to->Write(";\n");
308 }
309 
Write(CodeWriter * to) const310 void TryStatement::Write(CodeWriter* to) const {
311   to->Write("try ");
312   this->statements->Write(to);
313 }
314 
Write(CodeWriter * to) const315 void FinallyStatement::Write(CodeWriter* to) const {
316   to->Write("finally ");
317   this->statements->Write(to);
318 }
319 
Case(const string & c)320 Case::Case(const string& c) { cases.push_back(c); }
321 
Write(CodeWriter * to) const322 void Case::Write(CodeWriter* to) const {
323   int N = this->cases.size();
324   if (N > 0) {
325     for (int i = 0; i < N; i++) {
326       string s = this->cases[i];
327       if (s.length() != 0) {
328         to->Write("case %s:\n", s.c_str());
329       } else {
330         to->Write("default:\n");
331       }
332     }
333   } else {
334     to->Write("default:\n");
335   }
336   statements->Write(to);
337 }
338 
SwitchStatement(Expression * e)339 SwitchStatement::SwitchStatement(Expression* e) : expression(e) {}
340 
Write(CodeWriter * to) const341 void SwitchStatement::Write(CodeWriter* to) const {
342   to->Write("switch (");
343   this->expression->Write(to);
344   to->Write(")\n{\n");
345   to->Indent();
346   int N = this->cases.size();
347   for (int i = 0; i < N; i++) {
348     this->cases[i]->Write(to);
349   }
350   to->Dedent();
351   to->Write("}\n");
352 }
353 
Write(CodeWriter * to) const354 void Method::Write(CodeWriter* to) const {
355   size_t N, i;
356 
357   if (this->comment.length() != 0) {
358     to->Write("%s\n", this->comment.c_str());
359   }
360 
361   for (const auto& a : this->annotations) {
362     to->Write("%s\n", a.c_str());
363   }
364 
365   WriteModifiers(to, this->modifiers,
366                  SCOPE_MASK | STATIC | ABSTRACT | FINAL | OVERRIDE);
367 
368   if (this->returnType) {
369     string dim;
370     for (i = 0; i < this->returnTypeDimension; i++) {
371       dim += "[]";
372     }
373     to->Write("%s%s ", this->returnType->c_str(), dim.c_str());
374   }
375 
376   to->Write("%s(", this->name.c_str());
377 
378   N = this->parameters.size();
379   for (i = 0; i < N; i++) {
380     this->parameters[i]->WriteDeclaration(to);
381     if (i != N - 1) {
382       to->Write(", ");
383     }
384   }
385 
386   to->Write(")");
387 
388   N = this->exceptions.size();
389   for (i = 0; i < N; i++) {
390     if (i == 0) {
391       to->Write(" throws ");
392     } else {
393       to->Write(", ");
394     }
395     to->Write("%s", this->exceptions[i].c_str());
396   }
397 
398   if (this->statements == nullptr) {
399     to->Write(";\n");
400   } else {
401     to->Write("\n");
402     this->statements->Write(to);
403   }
404 }
405 
Write(CodeWriter * to) const406 void LiteralClassElement::Write(CodeWriter* to) const {
407   to->Write("%s", element.c_str());
408 }
409 
Write(CodeWriter * to) const410 void Class::Write(CodeWriter* to) const {
411   size_t N, i;
412 
413   if (this->comment.length() != 0) {
414     to->Write("%s\n", this->comment.c_str());
415   }
416   for (const auto& a : this->annotations) {
417     to->Write("%s\n", a.c_str());
418   }
419 
420   WriteModifiers(to, this->modifiers, ALL_MODIFIERS);
421 
422   if (this->what == Class::CLASS) {
423     to->Write("class ");
424   } else {
425     to->Write("interface ");
426   }
427 
428   string name = this->type;
429   size_t pos = name.rfind('.');
430   if (pos != string::npos) {
431     name = name.c_str() + pos + 1;
432   }
433 
434   to->Write("%s", name.c_str());
435 
436   if (this->extends) {
437     to->Write(" extends %s", this->extends->c_str());
438   }
439 
440   N = this->interfaces.size();
441   if (N != 0) {
442     if (this->what == Class::CLASS) {
443       to->Write(" implements");
444     } else {
445       to->Write(" extends");
446     }
447     for (i = 0; i < N; i++) {
448       to->Write(" %s", this->interfaces[i].c_str());
449     }
450   }
451 
452   to->Write("\n");
453   to->Write("{\n");
454   to->Indent();
455 
456   N = this->elements.size();
457   for (i = 0; i < N; i++) {
458     this->elements[i]->Write(to);
459   }
460 
461   to->Dedent();
462   to->Write("}\n");
463 }
464 
Document(const std::string & comment,const std::string & package,std::unique_ptr<Class> clazz)465 Document::Document(const std::string& comment,
466                    const std::string& package,
467                    std::unique_ptr<Class> clazz)
468     : comment_(comment),
469       package_(package),
470       clazz_(std::move(clazz)) {
471 }
472 
Write(CodeWriter * to) const473 void Document::Write(CodeWriter* to) const {
474   if (!comment_.empty()) {
475     to->Write("%s\n", comment_.c_str());
476   }
477   to->Write(
478       "/*\n"
479       " * This file is auto-generated.  DO NOT MODIFY.\n"
480       " */\n");
481   if (!package_.empty()) {
482     to->Write("package %s;\n", package_.c_str());
483   }
484 
485   if (clazz_) {
486     clazz_->Write(to);
487   }
488 }
489 
490 }  // namespace java
491 }  // namespace aidl
492 }  // namespace android
493