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