• 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 
WriteComment(CodeWriter * to,const std::string & comment)40 void WriteComment(CodeWriter* to, const std::string& comment) {
41   to->Write("%s", comment.c_str());
42   if (!comment.empty() && comment.back() != '\n') to->Write("\n");
43 }
44 
WriteModifiers(CodeWriter * to,int mod,int mask)45 void WriteModifiers(CodeWriter* to, int mod, int mask) {
46   int m = mod & mask;
47 
48   if (m & OVERRIDE) {
49     to->Write("@Override ");
50   }
51 
52   if ((m & SCOPE_MASK) == PUBLIC) {
53     to->Write("public ");
54   } else if ((m & SCOPE_MASK) == PRIVATE) {
55     to->Write("private ");
56   } else if ((m & SCOPE_MASK) == PROTECTED) {
57     to->Write("protected ");
58   }
59 
60   if (m & STATIC) {
61     to->Write("static ");
62   }
63 
64   if (m & FINAL) {
65     to->Write("final ");
66   }
67 
68   if (m & ABSTRACT) {
69     to->Write("abstract ");
70   }
71 }
72 
WriteArgumentList(CodeWriter * to,const vector<std::shared_ptr<Expression>> & arguments)73 void WriteArgumentList(CodeWriter* to, const vector<std::shared_ptr<Expression>>& arguments) {
74   size_t N = arguments.size();
75   for (size_t i = 0; i < N; i++) {
76     arguments[i]->Write(to);
77     if (i != N - 1) {
78       to->Write(", ");
79     }
80   }
81 }
82 
Field(int m,std::shared_ptr<Variable> v)83 Field::Field(int m, std::shared_ptr<Variable> v) : ClassElement(), modifiers(m), variable(v) {}
84 
Write(CodeWriter * to) const85 void Field::Write(CodeWriter* to) const {
86   WriteComment(to, comment);
87   for (const auto& a : this->annotations) {
88     to->Write("%s\n", a.c_str());
89   }
90   WriteModifiers(to, this->modifiers, SCOPE_MASK | STATIC | FINAL | OVERRIDE);
91   this->variable->WriteDeclaration(to);
92 
93   if (this->value.length() != 0) {
94     to->Write(" = %s", this->value.c_str());
95   }
96   to->Write(";\n");
97 }
98 
LiteralExpression(const string & v)99 LiteralExpression::LiteralExpression(const string& v) : value(v) {}
100 
Write(CodeWriter * to) const101 void LiteralExpression::Write(CodeWriter* to) const {
102   to->Write("%s", this->value.c_str());
103 }
104 
StringLiteralExpression(const string & v)105 StringLiteralExpression::StringLiteralExpression(const string& v) : value(v) {}
106 
Write(CodeWriter * to) const107 void StringLiteralExpression::Write(CodeWriter* to) const {
108   to->Write("\"%s\"", this->value.c_str());
109 }
110 
Variable(const string & t,const string & n)111 Variable::Variable(const string& t, const string& n) : type(t), name(n) {}
112 
WriteDeclaration(CodeWriter * to) const113 void Variable::WriteDeclaration(CodeWriter* to) const {
114   for (const auto& a : this->annotations) {
115     to->Write("%s ", a.c_str());
116   }
117   to->Write("%s %s", this->type.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(std::shared_ptr<Expression> o,const string & n)122 FieldVariable::FieldVariable(std::shared_ptr<Expression> o, const string& n)
123     : receiver(o), name(n) {}
124 
FieldVariable(const string & c,const string & n)125 FieldVariable::FieldVariable(const string& c, const string& n) : receiver(c), name(n) {}
126 
Write(CodeWriter * to) const127 void FieldVariable::Write(CodeWriter* to) const {
128   visit(
129       overloaded{[&](std::shared_ptr<Expression> e) { e->Write(to); },
130                  [&](const std::string& s) { to->Write("%s", s.c_str()); }, [](std::monostate) {}},
131       this->receiver);
132   to->Write(".%s", name.c_str());
133 }
134 
LiteralStatement(const std::string & value)135 LiteralStatement::LiteralStatement(const std::string& value) : value_(value) {}
136 
Write(CodeWriter * to) const137 void LiteralStatement::Write(CodeWriter* to) const {
138   to->Write("%s", value_.c_str());
139 }
140 
Write(CodeWriter * to) const141 void StatementBlock::Write(CodeWriter* to) const {
142   to->Write("{\n");
143   to->Indent();
144   int N = this->statements.size();
145   for (int i = 0; i < N; i++) {
146     this->statements[i]->Write(to);
147   }
148   to->Dedent();
149   to->Write("}\n");
150 }
151 
Add(std::shared_ptr<Statement> statement)152 void StatementBlock::Add(std::shared_ptr<Statement> statement) {
153   this->statements.push_back(statement);
154 }
155 
Add(std::shared_ptr<Expression> expression)156 void StatementBlock::Add(std::shared_ptr<Expression> expression) {
157   this->statements.push_back(std::make_shared<ExpressionStatement>(expression));
158 }
159 
ExpressionStatement(std::shared_ptr<Expression> e)160 ExpressionStatement::ExpressionStatement(std::shared_ptr<Expression> e) : expression(e) {}
161 
Write(CodeWriter * to) const162 void ExpressionStatement::Write(CodeWriter* to) const {
163   this->expression->Write(to);
164   to->Write(";\n");
165 }
166 
Assignment(std::shared_ptr<Variable> l,std::shared_ptr<Expression> r)167 Assignment::Assignment(std::shared_ptr<Variable> l, std::shared_ptr<Expression> r)
168     : lvalue(l), rvalue(r) {}
169 
Assignment(std::shared_ptr<Variable> l,std::shared_ptr<Expression> r,string c)170 Assignment::Assignment(std::shared_ptr<Variable> l, std::shared_ptr<Expression> r, string c)
171     : lvalue(l), rvalue(r), cast(c) {}
172 
Write(CodeWriter * to) const173 void Assignment::Write(CodeWriter* to) const {
174   this->lvalue->Write(to);
175   to->Write(" = ");
176   if (this->cast) {
177     to->Write("(%s)", this->cast->c_str());
178   }
179   this->rvalue->Write(to);
180 }
181 
MethodCall(const string & n)182 MethodCall::MethodCall(const string& n) : name(n) {}
183 
MethodCall(const string & n,const std::vector<std::shared_ptr<Expression>> & args)184 MethodCall::MethodCall(const string& n, const std::vector<std::shared_ptr<Expression>>& args)
185     : name(n), arguments(args) {}
186 
MethodCall(std::shared_ptr<Expression> o,const string & n)187 MethodCall::MethodCall(std::shared_ptr<Expression> o, const string& n) : receiver(o), name(n) {}
188 
MethodCall(const std::string & t,const string & n)189 MethodCall::MethodCall(const std::string& t, const string& n) : receiver(t), name(n) {}
190 
MethodCall(std::shared_ptr<Expression> o,const string & n,const std::vector<std::shared_ptr<Expression>> & args)191 MethodCall::MethodCall(std::shared_ptr<Expression> o, const string& n,
192                        const std::vector<std::shared_ptr<Expression>>& args)
193     : receiver(o), name(n), arguments(args) {}
194 
MethodCall(const std::string & t,const string & n,const std::vector<std::shared_ptr<Expression>> & args)195 MethodCall::MethodCall(const std::string& t, const string& n,
196                        const std::vector<std::shared_ptr<Expression>>& args)
197     : receiver(t), name(n), arguments(args) {}
198 
Write(CodeWriter * to) const199 void MethodCall::Write(CodeWriter* to) const {
200   visit(
201       overloaded{[&](std::shared_ptr<Expression> e) {
202                    e->Write(to);
203                    to->Write(".");
204                  },
205                  [&](const std::string& s) { to->Write("%s.", s.c_str()); }, [](std::monostate) {}},
206       this->receiver);
207   to->Write("%s(", this->name.c_str());
208   WriteArgumentList(to, this->arguments);
209   to->Write(")");
210 }
211 
Comparison(std::shared_ptr<Expression> l,const string & o,std::shared_ptr<Expression> r)212 Comparison::Comparison(std::shared_ptr<Expression> l, const string& o,
213                        std::shared_ptr<Expression> r)
214     : lvalue(l), op(o), rvalue(r) {}
215 
Write(CodeWriter * to) const216 void Comparison::Write(CodeWriter* to) const {
217   to->Write("(");
218   this->lvalue->Write(to);
219   to->Write("%s", this->op.c_str());
220   this->rvalue->Write(to);
221   to->Write(")");
222 }
223 
NewExpression(const std::string & n)224 NewExpression::NewExpression(const std::string& n) : instantiableName(n) {}
225 
NewExpression(const std::string & n,const std::vector<std::shared_ptr<Expression>> & args)226 NewExpression::NewExpression(const std::string& n,
227                              const std::vector<std::shared_ptr<Expression>>& args)
228     : instantiableName(n), arguments(args) {}
229 
Write(CodeWriter * to) const230 void NewExpression::Write(CodeWriter* to) const {
231   to->Write("new %s(", this->instantiableName.c_str());
232   WriteArgumentList(to, this->arguments);
233   to->Write(")");
234 }
235 
NewArrayExpression(const std::string & t,std::shared_ptr<Expression> s)236 NewArrayExpression::NewArrayExpression(const std::string& t, std::shared_ptr<Expression> s)
237     : type(t), size(s) {}
238 
Write(CodeWriter * to) const239 void NewArrayExpression::Write(CodeWriter* to) const {
240   to->Write("new %s[", this->type.c_str());
241   size->Write(to);
242   to->Write("]");
243 }
244 
Cast(const std::string & t,std::shared_ptr<Expression> e)245 Cast::Cast(const std::string& t, std::shared_ptr<Expression> e) : type(t), expression(e) {}
246 
Write(CodeWriter * to) const247 void Cast::Write(CodeWriter* to) const {
248   to->Write("((%s)", this->type.c_str());
249   expression->Write(to);
250   to->Write(")");
251 }
252 
VariableDeclaration(std::shared_ptr<Variable> l,std::shared_ptr<Expression> r)253 VariableDeclaration::VariableDeclaration(std::shared_ptr<Variable> l, std::shared_ptr<Expression> r)
254     : lvalue(l), rvalue(r) {}
255 
VariableDeclaration(std::shared_ptr<Variable> l)256 VariableDeclaration::VariableDeclaration(std::shared_ptr<Variable> l) : lvalue(l) {}
257 
Write(CodeWriter * to) const258 void VariableDeclaration::Write(CodeWriter* to) const {
259   this->lvalue->WriteDeclaration(to);
260   if (this->rvalue != nullptr) {
261     to->Write(" = ");
262     this->rvalue->Write(to);
263   }
264   to->Write(";\n");
265 }
266 
Write(CodeWriter * to) const267 void IfStatement::Write(CodeWriter* to) const {
268   if (this->expression != nullptr) {
269     to->Write("if (");
270     this->expression->Write(to);
271     to->Write(") ");
272   }
273   this->statements->Write(to);
274   if (this->elseif != nullptr) {
275     to->Write("else ");
276     this->elseif->Write(to);
277   }
278 }
279 
ReturnStatement(std::shared_ptr<Expression> e)280 ReturnStatement::ReturnStatement(std::shared_ptr<Expression> e) : expression(e) {}
281 
Write(CodeWriter * to) const282 void ReturnStatement::Write(CodeWriter* to) const {
283   to->Write("return ");
284   this->expression->Write(to);
285   to->Write(";\n");
286 }
287 
Write(CodeWriter * to) const288 void TryStatement::Write(CodeWriter* to) const {
289   to->Write("try ");
290   this->statements->Write(to);
291 }
292 
Write(CodeWriter * to) const293 void FinallyStatement::Write(CodeWriter* to) const {
294   to->Write("finally ");
295   this->statements->Write(to);
296 }
297 
Case(const string & c)298 Case::Case(const string& c) { cases.push_back(c); }
299 
Write(CodeWriter * to) const300 void Case::Write(CodeWriter* to) const {
301   int N = this->cases.size();
302   if (N > 0) {
303     for (int i = 0; i < N; i++) {
304       string s = this->cases[i];
305       if (s.length() != 0) {
306         to->Write("case %s:\n", s.c_str());
307       } else {
308         to->Write("default:\n");
309       }
310     }
311   } else {
312     to->Write("default:\n");
313   }
314   statements->Write(to);
315 }
316 
SwitchStatement(std::shared_ptr<Expression> e)317 SwitchStatement::SwitchStatement(std::shared_ptr<Expression> e) : expression(e) {}
318 
Write(CodeWriter * to) const319 void SwitchStatement::Write(CodeWriter* to) const {
320   to->Write("switch (");
321   this->expression->Write(to);
322   to->Write(")\n{\n");
323   to->Indent();
324   int N = this->cases.size();
325   for (int i = 0; i < N; i++) {
326     this->cases[i]->Write(to);
327   }
328   to->Dedent();
329   to->Write("}\n");
330 }
331 
Write(CodeWriter * to) const332 void Method::Write(CodeWriter* to) const {
333   size_t N, i;
334 
335   WriteComment(to, comment);
336 
337   for (const auto& a : this->annotations) {
338     to->Write("%s\n", a.c_str());
339   }
340 
341   WriteModifiers(to, this->modifiers,
342                  SCOPE_MASK | STATIC | ABSTRACT | FINAL | OVERRIDE);
343 
344   if (this->returnType) {
345     to->Write("%s ", this->returnType->c_str());
346   }
347 
348   to->Write("%s(", this->name.c_str());
349 
350   N = this->parameters.size();
351   for (i = 0; i < N; i++) {
352     this->parameters[i]->WriteDeclaration(to);
353     if (i != N - 1) {
354       to->Write(", ");
355     }
356   }
357 
358   to->Write(")");
359 
360   N = this->exceptions.size();
361   for (i = 0; i < N; i++) {
362     if (i == 0) {
363       to->Write(" throws ");
364     } else {
365       to->Write(", ");
366     }
367     to->Write("%s", this->exceptions[i].c_str());
368   }
369 
370   if (this->statements == nullptr) {
371     to->Write(";\n");
372   } else {
373     to->Write("\n");
374     this->statements->Write(to);
375   }
376 }
377 
Write(CodeWriter * to) const378 void LiteralClassElement::Write(CodeWriter* to) const {
379   to->Write("%s", element.c_str());
380 }
381 
Write(CodeWriter * to) const382 void Class::Write(CodeWriter* to) const {
383   size_t N, i;
384 
385   WriteComment(to, comment);
386   for (const auto& a : this->annotations) {
387     to->Write("%s\n", a.c_str());
388   }
389 
390   WriteModifiers(to, this->modifiers, ALL_MODIFIERS);
391 
392   if (this->what == Class::CLASS) {
393     to->Write("class ");
394   } else {
395     to->Write("interface ");
396   }
397 
398   string name = this->type;
399   size_t pos = name.rfind('.');
400   if (pos != string::npos) {
401     name = name.c_str() + pos + 1;
402   }
403 
404   to->Write("%s", name.c_str());
405 
406   if (this->extends) {
407     to->Write(" extends %s", this->extends->c_str());
408   }
409 
410   N = this->interfaces.size();
411   if (N != 0) {
412     if (this->what == Class::CLASS) {
413       to->Write(" implements");
414     } else {
415       to->Write(" extends");
416     }
417     for (i = 0; i < N; i++) {
418       to->Write(" %s", this->interfaces[i].c_str());
419     }
420   }
421 
422   to->Write("\n");
423   to->Write("{\n");
424   to->Indent();
425 
426   N = this->elements.size();
427   for (i = 0; i < N; i++) {
428     this->elements[i]->Write(to);
429   }
430 
431   to->Dedent();
432   to->Write("}\n");
433 }
434 
Document(const std::string & comment,const std::string & package,std::unique_ptr<Class> clazz)435 Document::Document(const std::string& comment,
436                    const std::string& package,
437                    std::unique_ptr<Class> clazz)
438     : comment_(comment),
439       package_(package),
440       clazz_(std::move(clazz)) {
441 }
442 
Write(CodeWriter * to) const443 void Document::Write(CodeWriter* to) const {
444   if (!comment_.empty()) {
445     to->Write("%s\n", comment_.c_str());
446   }
447   to->Write(
448       "/*\n"
449       " * This file is auto-generated.  DO NOT MODIFY.\n"
450       " */\n");
451   if (!package_.empty()) {
452     to->Write("package %s;\n", package_.c_str());
453   }
454 
455   if (clazz_) {
456     clazz_->Write(to);
457   }
458 }
459 
460 std::shared_ptr<Expression> NULL_VALUE = std::make_shared<LiteralExpression>("null");
461 std::shared_ptr<Expression> THIS_VALUE = std::make_shared<LiteralExpression>("this");
462 std::shared_ptr<Expression> SUPER_VALUE = std::make_shared<LiteralExpression>("super");
463 std::shared_ptr<Expression> TRUE_VALUE = std::make_shared<LiteralExpression>("true");
464 std::shared_ptr<Expression> FALSE_VALUE = std::make_shared<LiteralExpression>("false");
465 }  // namespace java
466 }  // namespace aidl
467 }  // namespace android
468