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<std::shared_ptr<Expression>> & arguments)68 void WriteArgumentList(CodeWriter* to, const vector<std::shared_ptr<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,std::shared_ptr<Variable> v)78 Field::Field(int m, std::shared_ptr<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) {}
109 
WriteDeclaration(CodeWriter * to) const110 void Variable::WriteDeclaration(CodeWriter* to) const {
111   for (const auto& a : this->annotations) {
112     to->Write("%s ", a.c_str());
113   }
114   to->Write("%s %s", this->type.c_str(), this->name.c_str());
115 }
116 
Write(CodeWriter * to) const117 void Variable::Write(CodeWriter* to) const { to->Write("%s", name.c_str()); }
118 
FieldVariable(std::shared_ptr<Expression> o,const string & n)119 FieldVariable::FieldVariable(std::shared_ptr<Expression> o, const string& n)
120     : receiver(o), name(n) {}
121 
FieldVariable(const string & c,const string & n)122 FieldVariable::FieldVariable(const string& c, const string& n) : receiver(c), name(n) {}
123 
Write(CodeWriter * to) const124 void FieldVariable::Write(CodeWriter* to) const {
125   visit(
126       overloaded{[&](std::shared_ptr<Expression> e) { e->Write(to); },
127                  [&](const std::string& s) { to->Write("%s", s.c_str()); }, [](std::monostate) {}},
128       this->receiver);
129   to->Write(".%s", name.c_str());
130 }
131 
LiteralStatement(const std::string & value)132 LiteralStatement::LiteralStatement(const std::string& value) : value_(value) {}
133 
Write(CodeWriter * to) const134 void LiteralStatement::Write(CodeWriter* to) const {
135   to->Write("%s", value_.c_str());
136 }
137 
Write(CodeWriter * to) const138 void StatementBlock::Write(CodeWriter* to) const {
139   to->Write("{\n");
140   to->Indent();
141   int N = this->statements.size();
142   for (int i = 0; i < N; i++) {
143     this->statements[i]->Write(to);
144   }
145   to->Dedent();
146   to->Write("}\n");
147 }
148 
Add(std::shared_ptr<Statement> statement)149 void StatementBlock::Add(std::shared_ptr<Statement> statement) {
150   this->statements.push_back(statement);
151 }
152 
Add(std::shared_ptr<Expression> expression)153 void StatementBlock::Add(std::shared_ptr<Expression> expression) {
154   this->statements.push_back(std::make_shared<ExpressionStatement>(expression));
155 }
156 
ExpressionStatement(std::shared_ptr<Expression> e)157 ExpressionStatement::ExpressionStatement(std::shared_ptr<Expression> e) : expression(e) {}
158 
Write(CodeWriter * to) const159 void ExpressionStatement::Write(CodeWriter* to) const {
160   this->expression->Write(to);
161   to->Write(";\n");
162 }
163 
Assignment(std::shared_ptr<Variable> l,std::shared_ptr<Expression> r)164 Assignment::Assignment(std::shared_ptr<Variable> l, std::shared_ptr<Expression> r)
165     : lvalue(l), rvalue(r) {}
166 
Assignment(std::shared_ptr<Variable> l,std::shared_ptr<Expression> r,string c)167 Assignment::Assignment(std::shared_ptr<Variable> l, std::shared_ptr<Expression> r, string c)
168     : 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,const std::vector<std::shared_ptr<Expression>> & args)181 MethodCall::MethodCall(const string& n, const std::vector<std::shared_ptr<Expression>>& args)
182     : name(n), arguments(args) {}
183 
MethodCall(std::shared_ptr<Expression> o,const string & n)184 MethodCall::MethodCall(std::shared_ptr<Expression> o, const string& n) : receiver(o), name(n) {}
185 
MethodCall(const std::string & t,const string & n)186 MethodCall::MethodCall(const std::string& t, const string& n) : receiver(t), name(n) {}
187 
MethodCall(std::shared_ptr<Expression> o,const string & n,const std::vector<std::shared_ptr<Expression>> & args)188 MethodCall::MethodCall(std::shared_ptr<Expression> o, const string& n,
189                        const std::vector<std::shared_ptr<Expression>>& args)
190     : receiver(o), name(n), arguments(args) {}
191 
MethodCall(const std::string & t,const string & n,const std::vector<std::shared_ptr<Expression>> & args)192 MethodCall::MethodCall(const std::string& t, const string& n,
193                        const std::vector<std::shared_ptr<Expression>>& args)
194     : receiver(t), name(n), arguments(args) {}
195 
Write(CodeWriter * to) const196 void MethodCall::Write(CodeWriter* to) const {
197   visit(
198       overloaded{[&](std::shared_ptr<Expression> e) {
199                    e->Write(to);
200                    to->Write(".");
201                  },
202                  [&](const std::string& s) { to->Write("%s.", s.c_str()); }, [](std::monostate) {}},
203       this->receiver);
204   to->Write("%s(", this->name.c_str());
205   WriteArgumentList(to, this->arguments);
206   to->Write(")");
207 }
208 
Comparison(std::shared_ptr<Expression> l,const string & o,std::shared_ptr<Expression> r)209 Comparison::Comparison(std::shared_ptr<Expression> l, const string& o,
210                        std::shared_ptr<Expression> r)
211     : lvalue(l), op(o), rvalue(r) {}
212 
Write(CodeWriter * to) const213 void Comparison::Write(CodeWriter* to) const {
214   to->Write("(");
215   this->lvalue->Write(to);
216   to->Write("%s", this->op.c_str());
217   this->rvalue->Write(to);
218   to->Write(")");
219 }
220 
NewExpression(const std::string & n)221 NewExpression::NewExpression(const std::string& n) : instantiableName(n) {}
222 
NewExpression(const std::string & n,const std::vector<std::shared_ptr<Expression>> & args)223 NewExpression::NewExpression(const std::string& n,
224                              const std::vector<std::shared_ptr<Expression>>& args)
225     : instantiableName(n), arguments(args) {}
226 
Write(CodeWriter * to) const227 void NewExpression::Write(CodeWriter* to) const {
228   to->Write("new %s(", this->instantiableName.c_str());
229   WriteArgumentList(to, this->arguments);
230   to->Write(")");
231 }
232 
NewArrayExpression(const std::string & t,std::shared_ptr<Expression> s)233 NewArrayExpression::NewArrayExpression(const std::string& t, std::shared_ptr<Expression> s)
234     : type(t), size(s) {}
235 
Write(CodeWriter * to) const236 void NewArrayExpression::Write(CodeWriter* to) const {
237   to->Write("new %s[", this->type.c_str());
238   size->Write(to);
239   to->Write("]");
240 }
241 
Cast(const std::string & t,std::shared_ptr<Expression> e)242 Cast::Cast(const std::string& t, std::shared_ptr<Expression> e) : type(t), expression(e) {}
243 
Write(CodeWriter * to) const244 void Cast::Write(CodeWriter* to) const {
245   to->Write("((%s)", this->type.c_str());
246   expression->Write(to);
247   to->Write(")");
248 }
249 
VariableDeclaration(std::shared_ptr<Variable> l,std::shared_ptr<Expression> r)250 VariableDeclaration::VariableDeclaration(std::shared_ptr<Variable> l, std::shared_ptr<Expression> r)
251     : lvalue(l), rvalue(r) {}
252 
VariableDeclaration(std::shared_ptr<Variable> l)253 VariableDeclaration::VariableDeclaration(std::shared_ptr<Variable> l) : lvalue(l) {}
254 
Write(CodeWriter * to) const255 void VariableDeclaration::Write(CodeWriter* to) const {
256   this->lvalue->WriteDeclaration(to);
257   if (this->rvalue != nullptr) {
258     to->Write(" = ");
259     this->rvalue->Write(to);
260   }
261   to->Write(";\n");
262 }
263 
Write(CodeWriter * to) const264 void IfStatement::Write(CodeWriter* to) const {
265   if (this->expression != nullptr) {
266     to->Write("if (");
267     this->expression->Write(to);
268     to->Write(") ");
269   }
270   this->statements->Write(to);
271   if (this->elseif != nullptr) {
272     to->Write("else ");
273     this->elseif->Write(to);
274   }
275 }
276 
ReturnStatement(std::shared_ptr<Expression> e)277 ReturnStatement::ReturnStatement(std::shared_ptr<Expression> e) : expression(e) {}
278 
Write(CodeWriter * to) const279 void ReturnStatement::Write(CodeWriter* to) const {
280   to->Write("return ");
281   this->expression->Write(to);
282   to->Write(";\n");
283 }
284 
Write(CodeWriter * to) const285 void TryStatement::Write(CodeWriter* to) const {
286   to->Write("try ");
287   this->statements->Write(to);
288 }
289 
Write(CodeWriter * to) const290 void FinallyStatement::Write(CodeWriter* to) const {
291   to->Write("finally ");
292   this->statements->Write(to);
293 }
294 
Case(const string & c)295 Case::Case(const string& c) { cases.push_back(c); }
296 
Write(CodeWriter * to) const297 void Case::Write(CodeWriter* to) const {
298   int N = this->cases.size();
299   if (N > 0) {
300     for (int i = 0; i < N; i++) {
301       string s = this->cases[i];
302       if (s.length() != 0) {
303         to->Write("case %s:\n", s.c_str());
304       } else {
305         to->Write("default:\n");
306       }
307     }
308   } else {
309     to->Write("default:\n");
310   }
311   statements->Write(to);
312 }
313 
SwitchStatement(std::shared_ptr<Expression> e)314 SwitchStatement::SwitchStatement(std::shared_ptr<Expression> e) : expression(e) {}
315 
Write(CodeWriter * to) const316 void SwitchStatement::Write(CodeWriter* to) const {
317   to->Write("switch (");
318   this->expression->Write(to);
319   to->Write(")\n{\n");
320   to->Indent();
321   int N = this->cases.size();
322   for (int i = 0; i < N; i++) {
323     this->cases[i]->Write(to);
324   }
325   to->Dedent();
326   to->Write("}\n");
327 }
328 
Write(CodeWriter * to) const329 void Method::Write(CodeWriter* to) const {
330   size_t N, i;
331 
332   if (this->comment.length() != 0) {
333     to->Write("%s\n", this->comment.c_str());
334   }
335 
336   for (const auto& a : this->annotations) {
337     to->Write("%s\n", a.c_str());
338   }
339 
340   WriteModifiers(to, this->modifiers,
341                  SCOPE_MASK | STATIC | ABSTRACT | FINAL | OVERRIDE);
342 
343   if (this->returnType) {
344     to->Write("%s ", this->returnType->c_str());
345   }
346 
347   to->Write("%s(", this->name.c_str());
348 
349   N = this->parameters.size();
350   for (i = 0; i < N; i++) {
351     this->parameters[i]->WriteDeclaration(to);
352     if (i != N - 1) {
353       to->Write(", ");
354     }
355   }
356 
357   to->Write(")");
358 
359   N = this->exceptions.size();
360   for (i = 0; i < N; i++) {
361     if (i == 0) {
362       to->Write(" throws ");
363     } else {
364       to->Write(", ");
365     }
366     to->Write("%s", this->exceptions[i].c_str());
367   }
368 
369   if (this->statements == nullptr) {
370     to->Write(";\n");
371   } else {
372     to->Write("\n");
373     this->statements->Write(to);
374   }
375 }
376 
Write(CodeWriter * to) const377 void LiteralClassElement::Write(CodeWriter* to) const {
378   to->Write("%s", element.c_str());
379 }
380 
Write(CodeWriter * to) const381 void Class::Write(CodeWriter* to) const {
382   size_t N, i;
383 
384   if (this->comment.length() != 0) {
385     to->Write("%s\n", this->comment.c_str());
386   }
387   for (const auto& a : this->annotations) {
388     to->Write("%s\n", a.c_str());
389   }
390 
391   WriteModifiers(to, this->modifiers, ALL_MODIFIERS);
392 
393   if (this->what == Class::CLASS) {
394     to->Write("class ");
395   } else {
396     to->Write("interface ");
397   }
398 
399   string name = this->type;
400   size_t pos = name.rfind('.');
401   if (pos != string::npos) {
402     name = name.c_str() + pos + 1;
403   }
404 
405   to->Write("%s", name.c_str());
406 
407   if (this->extends) {
408     to->Write(" extends %s", this->extends->c_str());
409   }
410 
411   N = this->interfaces.size();
412   if (N != 0) {
413     if (this->what == Class::CLASS) {
414       to->Write(" implements");
415     } else {
416       to->Write(" extends");
417     }
418     for (i = 0; i < N; i++) {
419       to->Write(" %s", this->interfaces[i].c_str());
420     }
421   }
422 
423   to->Write("\n");
424   to->Write("{\n");
425   to->Indent();
426 
427   N = this->elements.size();
428   for (i = 0; i < N; i++) {
429     this->elements[i]->Write(to);
430   }
431 
432   to->Dedent();
433   to->Write("}\n");
434 }
435 
Document(const std::string & comment,const std::string & package,std::unique_ptr<Class> clazz)436 Document::Document(const std::string& comment,
437                    const std::string& package,
438                    std::unique_ptr<Class> clazz)
439     : comment_(comment),
440       package_(package),
441       clazz_(std::move(clazz)) {
442 }
443 
Write(CodeWriter * to) const444 void Document::Write(CodeWriter* to) const {
445   if (!comment_.empty()) {
446     to->Write("%s\n", comment_.c_str());
447   }
448   to->Write(
449       "/*\n"
450       " * This file is auto-generated.  DO NOT MODIFY.\n"
451       " */\n");
452   if (!package_.empty()) {
453     to->Write("package %s;\n", package_.c_str());
454   }
455 
456   if (clazz_) {
457     clazz_->Write(to);
458   }
459 }
460 
461 std::shared_ptr<Expression> NULL_VALUE = std::make_shared<LiteralExpression>("null");
462 std::shared_ptr<Expression> THIS_VALUE = std::make_shared<LiteralExpression>("this");
463 std::shared_ptr<Expression> SUPER_VALUE = std::make_shared<LiteralExpression>("super");
464 std::shared_ptr<Expression> TRUE_VALUE = std::make_shared<LiteralExpression>("true");
465 std::shared_ptr<Expression> FALSE_VALUE = std::make_shared<LiteralExpression>("false");
466 }  // namespace java
467 }  // namespace aidl
468 }  // namespace android
469