• 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 
Cast(const std::string & t,std::shared_ptr<Expression> e)236 Cast::Cast(const std::string& t, std::shared_ptr<Expression> e) : type(t), expression(e) {}
237 
Write(CodeWriter * to) const238 void Cast::Write(CodeWriter* to) const {
239   to->Write("((%s)", this->type.c_str());
240   expression->Write(to);
241   to->Write(")");
242 }
243 
VariableDeclaration(std::shared_ptr<Variable> l,std::shared_ptr<Expression> r)244 VariableDeclaration::VariableDeclaration(std::shared_ptr<Variable> l, std::shared_ptr<Expression> r)
245     : lvalue(l), rvalue(r) {}
246 
VariableDeclaration(std::shared_ptr<Variable> l)247 VariableDeclaration::VariableDeclaration(std::shared_ptr<Variable> l) : lvalue(l) {}
248 
Write(CodeWriter * to) const249 void VariableDeclaration::Write(CodeWriter* to) const {
250   this->lvalue->WriteDeclaration(to);
251   if (this->rvalue != nullptr) {
252     to->Write(" = ");
253     this->rvalue->Write(to);
254   }
255   to->Write(";\n");
256 }
257 
Write(CodeWriter * to) const258 void IfStatement::Write(CodeWriter* to) const {
259   if (this->expression != nullptr) {
260     to->Write("if (");
261     this->expression->Write(to);
262     to->Write(") ");
263   }
264   this->statements->Write(to);
265   if (this->elseif != nullptr) {
266     to->Write("else ");
267     this->elseif->Write(to);
268   }
269 }
270 
ReturnStatement(std::shared_ptr<Expression> e)271 ReturnStatement::ReturnStatement(std::shared_ptr<Expression> e) : expression(e) {}
272 
Write(CodeWriter * to) const273 void ReturnStatement::Write(CodeWriter* to) const {
274   to->Write("return ");
275   this->expression->Write(to);
276   to->Write(";\n");
277 }
278 
Write(CodeWriter * to) const279 void BreakStatement::Write(CodeWriter* to) const {
280   to->Write("break;\n");
281 }
282 
Write(CodeWriter * to) const283 void TryStatement::Write(CodeWriter* to) const {
284   to->Write("try ");
285   this->statements->Write(to);
286 }
287 
Write(CodeWriter * to) const288 void FinallyStatement::Write(CodeWriter* to) const {
289   to->Write("finally ");
290   this->statements->Write(to);
291 }
292 
Case(const string & c)293 Case::Case(const string& c) { cases.push_back(c); }
294 
Write(CodeWriter * to) const295 void Case::Write(CodeWriter* to) const {
296   int N = this->cases.size();
297   if (N > 0) {
298     for (int i = 0; i < N; i++) {
299       string s = this->cases[i];
300       if (s.length() != 0) {
301         to->Write("case %s:\n", s.c_str());
302       } else {
303         to->Write("default:\n");
304       }
305     }
306   } else {
307     to->Write("default:\n");
308   }
309   statements->Write(to);
310 }
311 
SwitchStatement(std::shared_ptr<Expression> e)312 SwitchStatement::SwitchStatement(std::shared_ptr<Expression> e) : expression(e) {}
313 
Write(CodeWriter * to) const314 void SwitchStatement::Write(CodeWriter* to) const {
315   to->Write("switch (");
316   this->expression->Write(to);
317   to->Write(")\n{\n");
318   to->Indent();
319   int N = this->cases.size();
320   for (int i = 0; i < N; i++) {
321     this->cases[i]->Write(to);
322   }
323   to->Dedent();
324   to->Write("}\n");
325 }
326 
Write(CodeWriter * to) const327 void Method::Write(CodeWriter* to) const {
328   size_t N, i;
329 
330   WriteComment(to, comment);
331 
332   for (const auto& a : this->annotations) {
333     to->Write("%s\n", a.c_str());
334   }
335 
336   WriteModifiers(to, this->modifiers,
337                  SCOPE_MASK | STATIC | ABSTRACT | FINAL | OVERRIDE);
338 
339   if (this->returnType) {
340     to->Write("%s ", this->returnType->c_str());
341   }
342 
343   to->Write("%s(", this->name.c_str());
344 
345   N = this->parameters.size();
346   for (i = 0; i < N; i++) {
347     this->parameters[i]->WriteDeclaration(to);
348     if (i != N - 1) {
349       to->Write(", ");
350     }
351   }
352 
353   to->Write(")");
354 
355   N = this->exceptions.size();
356   for (i = 0; i < N; i++) {
357     if (i == 0) {
358       to->Write(" throws ");
359     } else {
360       to->Write(", ");
361     }
362     to->Write("%s", this->exceptions[i].c_str());
363   }
364 
365   if (this->statements == nullptr) {
366     to->Write(";\n");
367   } else {
368     to->Write("\n");
369     this->statements->Write(to);
370   }
371 }
372 
Write(CodeWriter * to) const373 void LiteralClassElement::Write(CodeWriter* to) const {
374   to->Write("%s", element.c_str());
375 }
376 
Write(CodeWriter * to) const377 void Class::Write(CodeWriter* to) const {
378   size_t N, i;
379 
380   WriteComment(to, comment);
381   for (const auto& a : this->annotations) {
382     to->Write("%s\n", a.c_str());
383   }
384 
385   WriteModifiers(to, this->modifiers, ALL_MODIFIERS);
386 
387   if (this->what == Class::CLASS) {
388     to->Write("class ");
389   } else {
390     to->Write("interface ");
391   }
392 
393   string name = this->type;
394   size_t pos = name.rfind('.');
395   if (pos != string::npos) {
396     name = name.c_str() + pos + 1;
397   }
398 
399   to->Write("%s", name.c_str());
400 
401   if (this->extends) {
402     to->Write(" extends %s", this->extends->c_str());
403   }
404 
405   N = this->interfaces.size();
406   if (N != 0) {
407     if (this->what == Class::CLASS) {
408       to->Write(" implements");
409     } else {
410       to->Write(" extends");
411     }
412     for (i = 0; i < N; i++) {
413       to->Write(" %s", this->interfaces[i].c_str());
414     }
415   }
416 
417   to->Write("\n");
418   to->Write("{\n");
419   to->Indent();
420 
421   N = this->elements.size();
422   for (i = 0; i < N; i++) {
423     this->elements[i]->Write(to);
424   }
425 
426   to->Dedent();
427   to->Write("}\n");
428 }
429 
430 std::shared_ptr<Expression> NULL_VALUE = std::make_shared<LiteralExpression>("null");
431 std::shared_ptr<Expression> THIS_VALUE = std::make_shared<LiteralExpression>("this");
432 std::shared_ptr<Expression> SUPER_VALUE = std::make_shared<LiteralExpression>("super");
433 std::shared_ptr<Expression> TRUE_VALUE = std::make_shared<LiteralExpression>("true");
434 std::shared_ptr<Expression> FALSE_VALUE = std::make_shared<LiteralExpression>("false");
435 }  // namespace java
436 }  // namespace aidl
437 }  // namespace android
438