ast_java.cpp revision 8f6816ea1fb532cff9ce0ecc1449926553b4e1da
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
19#include "code_writer.h"
20#include "type_java.h"
21
22namespace android {
23namespace aidl {
24
25void
26WriteModifiers(CodeWriter* to, int mod, int mask)
27{
28    int m = mod & mask;
29
30    if (m & OVERRIDE) {
31        to->Write("@Override ");
32    }
33
34    if ((m & SCOPE_MASK) == PUBLIC) {
35        to->Write("public ");
36    }
37    else if ((m & SCOPE_MASK) == PRIVATE) {
38        to->Write("private ");
39    }
40    else if ((m & SCOPE_MASK) == PROTECTED) {
41        to->Write("protected ");
42    }
43
44    if (m & STATIC) {
45        to->Write("static ");
46    }
47
48    if (m & FINAL) {
49        to->Write("final ");
50    }
51
52    if (m & ABSTRACT) {
53        to->Write("abstract ");
54    }
55}
56
57void
58WriteArgumentList(CodeWriter* to, const vector<Expression*>& arguments)
59{
60    size_t N = arguments.size();
61    for (size_t i=0; i<N; i++) {
62        arguments[i]->Write(to);
63        if (i != N-1) {
64            to->Write(", ");
65        }
66    }
67}
68
69ClassElement::ClassElement()
70{
71}
72
73ClassElement::~ClassElement()
74{
75}
76
77Field::Field()
78    :ClassElement(),
79     modifiers(0),
80     variable(NULL)
81{
82}
83
84Field::Field(int m, Variable* v)
85    :ClassElement(),
86     modifiers(m),
87     variable(v)
88{
89}
90
91Field::~Field()
92{
93}
94
95void
96Field::GatherTypes(set<const Type*>* types) const
97{
98    types->insert(this->variable->type);
99}
100
101void
102Field::Write(CodeWriter* to) const
103{
104    if (this->comment.length() != 0) {
105        to->Write("%s\n", this->comment.c_str());
106    }
107    WriteModifiers(to, this->modifiers, SCOPE_MASK | STATIC | FINAL | OVERRIDE);
108    to->Write("%s %s", this->variable->type->QualifiedName().c_str(),
109            this->variable->name.c_str());
110    if (this->value.length() != 0) {
111        to->Write(" = %s", this->value.c_str());
112    }
113    to->Write(";\n");
114}
115
116Expression::~Expression()
117{
118}
119
120LiteralExpression::LiteralExpression(const string& v)
121    :value(v)
122{
123}
124
125LiteralExpression::~LiteralExpression()
126{
127}
128
129void
130LiteralExpression::Write(CodeWriter* to) const
131{
132    to->Write("%s", this->value.c_str());
133}
134
135StringLiteralExpression::StringLiteralExpression(const string& v)
136    :value(v)
137{
138}
139
140StringLiteralExpression::~StringLiteralExpression()
141{
142}
143
144void
145StringLiteralExpression::Write(CodeWriter* to) const
146{
147    to->Write("\"%s\"", this->value.c_str());
148}
149
150Variable::Variable()
151    :type(NULL),
152     name(),
153     dimension(0)
154{
155}
156
157Variable::Variable(const Type* t, const string& n)
158    :type(t),
159     name(n),
160     dimension(0)
161{
162}
163
164Variable::Variable(const Type* t, const string& n, int d)
165    :type(t),
166     name(n),
167     dimension(d)
168{
169}
170
171Variable::~Variable()
172{
173}
174
175void
176Variable::GatherTypes(set<const Type*>* types) const
177{
178    types->insert(this->type);
179}
180
181void
182Variable::WriteDeclaration(CodeWriter* to) const
183{
184    string dim;
185    for (int i=0; i<this->dimension; i++) {
186        dim += "[]";
187    }
188    to->Write("%s%s %s", this->type->QualifiedName().c_str(), dim.c_str(),
189            this->name.c_str());
190}
191
192void
193Variable::Write(CodeWriter* to) const
194{
195    to->Write("%s", name.c_str());
196}
197
198FieldVariable::FieldVariable(Expression* o, const string& n)
199    :object(o),
200     clazz(NULL),
201     name(n)
202{
203}
204
205FieldVariable::FieldVariable(const Type* c, const string& n)
206    :object(NULL),
207     clazz(c),
208     name(n)
209{
210}
211
212FieldVariable::~FieldVariable()
213{
214}
215
216void
217FieldVariable::Write(CodeWriter* to) const
218{
219    if (this->object != NULL) {
220        this->object->Write(to);
221    }
222    else if (this->clazz != NULL) {
223        to->Write("%s", this->clazz->QualifiedName().c_str());
224    }
225    to->Write(".%s", name.c_str());
226}
227
228
229Statement::~Statement()
230{
231}
232
233StatementBlock::StatementBlock()
234{
235}
236
237StatementBlock::~StatementBlock()
238{
239}
240
241void
242StatementBlock::Write(CodeWriter* to) const
243{
244    to->Write("{\n");
245    int N = this->statements.size();
246    for (int i=0; i<N; i++) {
247        this->statements[i]->Write(to);
248    }
249    to->Write("}\n");
250}
251
252void
253StatementBlock::Add(Statement* statement)
254{
255    this->statements.push_back(statement);
256}
257
258void
259StatementBlock::Add(Expression* expression)
260{
261    this->statements.push_back(new ExpressionStatement(expression));
262}
263
264ExpressionStatement::ExpressionStatement(Expression* e)
265    :expression(e)
266{
267}
268
269ExpressionStatement::~ExpressionStatement()
270{
271}
272
273void
274ExpressionStatement::Write(CodeWriter* to) const
275{
276    this->expression->Write(to);
277    to->Write(";\n");
278}
279
280Assignment::Assignment(Variable* l, Expression* r)
281    :lvalue(l),
282     rvalue(r),
283     cast(NULL)
284{
285}
286
287Assignment::Assignment(Variable* l, Expression* r, const Type* c)
288    :lvalue(l),
289     rvalue(r),
290     cast(c)
291{
292}
293
294Assignment::~Assignment()
295{
296}
297
298void
299Assignment::Write(CodeWriter* to) const
300{
301    this->lvalue->Write(to);
302    to->Write(" = ");
303    if (this->cast != NULL) {
304        to->Write("(%s)", this->cast->QualifiedName().c_str());
305    }
306    this->rvalue->Write(to);
307}
308
309MethodCall::MethodCall(const string& n)
310    :obj(NULL),
311     clazz(NULL),
312     name(n)
313{
314}
315
316MethodCall::MethodCall(const string& n, int argc = 0, ...)
317    :obj(NULL),
318     clazz(NULL),
319     name(n)
320{
321  va_list args;
322  va_start(args, argc);
323  init(argc, args);
324  va_end(args);
325}
326
327MethodCall::MethodCall(Expression* o, const string& n)
328    :obj(o),
329     clazz(NULL),
330     name(n)
331{
332}
333
334MethodCall::MethodCall(const Type* t, const string& n)
335    :obj(NULL),
336     clazz(t),
337     name(n)
338{
339}
340
341MethodCall::MethodCall(Expression* o, const string& n, int argc = 0, ...)
342    :obj(o),
343     clazz(NULL),
344     name(n)
345{
346  va_list args;
347  va_start(args, argc);
348  init(argc, args);
349  va_end(args);
350}
351
352MethodCall::MethodCall(const Type* t, const string& n, int argc = 0, ...)
353    :obj(NULL),
354     clazz(t),
355     name(n)
356{
357  va_list args;
358  va_start(args, argc);
359  init(argc, args);
360  va_end(args);
361}
362
363MethodCall::~MethodCall()
364{
365}
366
367void
368MethodCall::init(int n, va_list args)
369{
370    for (int i=0; i<n; i++) {
371        Expression* expression = (Expression*)va_arg(args, void*);
372        this->arguments.push_back(expression);
373    }
374}
375
376void
377MethodCall::Write(CodeWriter* to) const
378{
379    if (this->obj != NULL) {
380        this->obj->Write(to);
381        to->Write(".");
382    }
383    else if (this->clazz != NULL) {
384        to->Write("%s.", this->clazz->QualifiedName().c_str());
385    }
386    to->Write("%s(", this->name.c_str());
387    WriteArgumentList(to, this->arguments);
388    to->Write(")");
389}
390
391Comparison::Comparison(Expression* l, const string& o, Expression* r)
392    :lvalue(l),
393     op(o),
394     rvalue(r)
395{
396}
397
398Comparison::~Comparison()
399{
400}
401
402void
403Comparison::Write(CodeWriter* to) const
404{
405    to->Write("(");
406    this->lvalue->Write(to);
407    to->Write("%s", this->op.c_str());
408    this->rvalue->Write(to);
409    to->Write(")");
410}
411
412NewExpression::NewExpression(const Type* t)
413    :type(t)
414{
415}
416
417NewExpression::NewExpression(const Type* t, int argc = 0, ...)
418    :type(t)
419{
420  va_list args;
421  va_start(args, argc);
422  init(argc, args);
423  va_end(args);
424}
425
426NewExpression::~NewExpression()
427{
428}
429
430void
431NewExpression::init(int n, va_list args)
432{
433    for (int i=0; i<n; i++) {
434        Expression* expression = (Expression*)va_arg(args, void*);
435        this->arguments.push_back(expression);
436    }
437}
438
439void
440NewExpression::Write(CodeWriter* to) const
441{
442    to->Write("new %s(", this->type->InstantiableName().c_str());
443    WriteArgumentList(to, this->arguments);
444    to->Write(")");
445}
446
447NewArrayExpression::NewArrayExpression(const Type* t, Expression* s)
448    :type(t),
449     size(s)
450{
451}
452
453NewArrayExpression::~NewArrayExpression()
454{
455}
456
457void
458NewArrayExpression::Write(CodeWriter* to) const
459{
460    to->Write("new %s[", this->type->QualifiedName().c_str());
461    size->Write(to);
462    to->Write("]");
463}
464
465Ternary::Ternary()
466    :condition(NULL),
467     ifpart(NULL),
468     elsepart(NULL)
469{
470}
471
472Ternary::Ternary(Expression* a, Expression* b, Expression* c)
473    :condition(a),
474     ifpart(b),
475     elsepart(c)
476{
477}
478
479Ternary::~Ternary()
480{
481}
482
483void
484Ternary::Write(CodeWriter* to) const
485{
486    to->Write("((");
487    this->condition->Write(to);
488    to->Write(")?(");
489    this->ifpart->Write(to);
490    to->Write("):(");
491    this->elsepart->Write(to);
492    to->Write("))");
493}
494
495Cast::Cast()
496    :type(NULL),
497     expression(NULL)
498{
499}
500
501Cast::Cast(const Type* t, Expression* e)
502    :type(t),
503     expression(e)
504{
505}
506
507Cast::~Cast()
508{
509}
510
511void
512Cast::Write(CodeWriter* to) const
513{
514    to->Write("((%s)", this->type->QualifiedName().c_str());
515    expression->Write(to);
516    to->Write(")");
517}
518
519VariableDeclaration::VariableDeclaration(Variable* l, Expression* r, const Type* c)
520    :lvalue(l),
521     cast(c),
522     rvalue(r)
523{
524}
525
526VariableDeclaration::VariableDeclaration(Variable* l)
527    :lvalue(l),
528     cast(NULL),
529     rvalue(NULL)
530{
531}
532
533VariableDeclaration::~VariableDeclaration()
534{
535}
536
537void
538VariableDeclaration::Write(CodeWriter* to) const
539{
540    this->lvalue->WriteDeclaration(to);
541    if (this->rvalue != NULL) {
542        to->Write(" = ");
543        if (this->cast != NULL) {
544            to->Write("(%s)", this->cast->QualifiedName().c_str());
545        }
546        this->rvalue->Write(to);
547    }
548    to->Write(";\n");
549}
550
551IfStatement::IfStatement()
552    :expression(NULL),
553     statements(new StatementBlock),
554     elseif(NULL)
555{
556}
557
558IfStatement::~IfStatement()
559{
560}
561
562void
563IfStatement::Write(CodeWriter* to) const
564{
565    if (this->expression != NULL) {
566        to->Write("if (");
567        this->expression->Write(to);
568        to->Write(") ");
569    }
570    this->statements->Write(to);
571    if (this->elseif != NULL) {
572        to->Write("else ");
573        this->elseif->Write(to);
574    }
575}
576
577ReturnStatement::ReturnStatement(Expression* e)
578    :expression(e)
579{
580}
581
582ReturnStatement::~ReturnStatement()
583{
584}
585
586void
587ReturnStatement::Write(CodeWriter* to) const
588{
589    to->Write("return ");
590    this->expression->Write(to);
591    to->Write(";\n");
592}
593
594TryStatement::TryStatement()
595    :statements(new StatementBlock)
596{
597}
598
599TryStatement::~TryStatement()
600{
601}
602
603void
604TryStatement::Write(CodeWriter* to) const
605{
606    to->Write("try ");
607    this->statements->Write(to);
608}
609
610CatchStatement::CatchStatement(Variable* e)
611    :statements(new StatementBlock),
612     exception(e)
613{
614}
615
616CatchStatement::~CatchStatement()
617{
618}
619
620void
621CatchStatement::Write(CodeWriter* to) const
622{
623    to->Write("catch ");
624    if (this->exception != NULL) {
625        to->Write("(");
626        this->exception->WriteDeclaration(to);
627        to->Write(") ");
628    }
629    this->statements->Write(to);
630}
631
632FinallyStatement::FinallyStatement()
633    :statements(new StatementBlock)
634{
635}
636
637FinallyStatement::~FinallyStatement()
638{
639}
640
641void
642FinallyStatement::Write(CodeWriter* to) const
643{
644    to->Write("finally ");
645    this->statements->Write(to);
646}
647
648Case::Case()
649    :statements(new StatementBlock)
650{
651}
652
653Case::Case(const string& c)
654    :statements(new StatementBlock)
655{
656    cases.push_back(c);
657}
658
659Case::~Case()
660{
661}
662
663void
664Case::Write(CodeWriter* to) const
665{
666    int N = this->cases.size();
667    if (N > 0) {
668        for (int i=0; i<N; i++) {
669            string s = this->cases[i];
670            if (s.length() != 0) {
671                to->Write("case %s:\n", s.c_str());
672            } else {
673                to->Write("default:\n");
674            }
675        }
676    } else {
677        to->Write("default:\n");
678    }
679    statements->Write(to);
680}
681
682SwitchStatement::SwitchStatement(Expression* e)
683    :expression(e)
684{
685}
686
687SwitchStatement::~SwitchStatement()
688{
689}
690
691void
692SwitchStatement::Write(CodeWriter* to) const
693{
694    to->Write("switch (");
695    this->expression->Write(to);
696    to->Write(")\n{\n");
697    int N = this->cases.size();
698    for (int i=0; i<N; i++) {
699        this->cases[i]->Write(to);
700    }
701    to->Write("}\n");
702}
703
704Break::Break()
705{
706}
707
708Break::~Break()
709{
710}
711
712void
713Break::Write(CodeWriter* to) const
714{
715    to->Write("break;\n");
716}
717
718Method::Method()
719    :ClassElement(),
720     modifiers(0),
721     returnType(NULL), // (NULL means constructor)
722     returnTypeDimension(0),
723     statements(NULL)
724{
725}
726
727Method::~Method()
728{
729}
730
731void
732Method::GatherTypes(set<const Type*>* types) const
733{
734    size_t N, i;
735
736    if (this->returnType) {
737        types->insert(this->returnType);
738    }
739
740    N = this->parameters.size();
741    for (i=0; i<N; i++) {
742        this->parameters[i]->GatherTypes(types);
743    }
744
745    N = this->exceptions.size();
746    for (i=0; i<N; i++) {
747        types->insert(this->exceptions[i]);
748    }
749}
750
751void
752Method::Write(CodeWriter* to) const
753{
754    size_t N, i;
755
756    if (this->comment.length() != 0) {
757        to->Write("%s\n", this->comment.c_str());
758    }
759
760    WriteModifiers(to, this->modifiers, SCOPE_MASK | STATIC | ABSTRACT | FINAL | OVERRIDE);
761
762    if (this->returnType != NULL) {
763        string dim;
764        for (i=0; i<this->returnTypeDimension; i++) {
765            dim += "[]";
766        }
767        to->Write("%s%s ", this->returnType->QualifiedName().c_str(),
768                dim.c_str());
769    }
770
771    to->Write("%s(", this->name.c_str());
772
773    N = this->parameters.size();
774    for (i=0; i<N; i++) {
775        this->parameters[i]->WriteDeclaration(to);
776        if (i != N-1) {
777            to->Write(", ");
778        }
779    }
780
781    to->Write(")");
782
783    N = this->exceptions.size();
784    for (i=0; i<N; i++) {
785        if (i == 0) {
786            to->Write(" throws ");
787        } else {
788            to->Write(", ");
789        }
790        to->Write("%s", this->exceptions[i]->QualifiedName().c_str());
791    }
792
793    if (this->statements == NULL) {
794        to->Write(";\n");
795    } else {
796        to->Write("\n");
797        this->statements->Write(to);
798    }
799}
800
801Class::Class()
802    :modifiers(0),
803     what(CLASS),
804     type(NULL),
805     extends(NULL)
806{
807}
808
809Class::~Class()
810{
811}
812
813void
814Class::GatherTypes(set<const Type*>* types) const
815{
816    int N, i;
817
818    types->insert(this->type);
819    if (this->extends != NULL) {
820        types->insert(this->extends);
821    }
822
823    N = this->interfaces.size();
824    for (i=0; i<N; i++) {
825        types->insert(this->interfaces[i]);
826    }
827
828    N = this->elements.size();
829    for (i=0; i<N; i++) {
830        this->elements[i]->GatherTypes(types);
831    }
832}
833
834void
835Class::Write(CodeWriter* to) const
836{
837    size_t N, i;
838
839    if (this->comment.length() != 0) {
840        to->Write("%s\n", this->comment.c_str());
841    }
842
843    WriteModifiers(to, this->modifiers, ALL_MODIFIERS);
844
845    if (this->what == Class::CLASS) {
846        to->Write("class ");
847    } else {
848        to->Write("interface ");
849    }
850
851    string name = this->type->Name();
852    size_t pos = name.rfind('.');
853    if (pos != string::npos) {
854        name = name.c_str() + pos + 1;
855    }
856
857    to->Write("%s", name.c_str());
858
859    if (this->extends != NULL) {
860        to->Write(" extends %s", this->extends->QualifiedName().c_str());
861    }
862
863    N = this->interfaces.size();
864    if (N != 0) {
865        if (this->what == Class::CLASS) {
866            to->Write(" implements");
867        } else {
868            to->Write(" extends");
869        }
870        for (i=0; i<N; i++) {
871            to->Write(" %s", this->interfaces[i]->QualifiedName().c_str());
872        }
873    }
874
875    to->Write("\n");
876    to->Write("{\n");
877
878    N = this->elements.size();
879    for (i=0; i<N; i++) {
880        this->elements[i]->Write(to);
881    }
882
883    to->Write("}\n");
884
885}
886
887Document::Document()
888{
889}
890
891Document::~Document()
892{
893}
894
895static string
896escape_backslashes(const string& str)
897{
898    string result;
899    const size_t I=str.length();
900    for (size_t i=0; i<I; i++) {
901        char c = str[i];
902        if (c == '\\') {
903            result += "\\\\";
904        } else {
905            result += c;
906        }
907    }
908    return result;
909}
910
911void
912Document::Write(CodeWriter* to) const
913{
914    size_t N, i;
915
916    if (this->comment.length() != 0) {
917        to->Write("%s\n", this->comment.c_str());
918    }
919    to->Write("/*\n"
920                " * This file is auto-generated.  DO NOT MODIFY.\n"
921                " * Original file: %s\n"
922                " */\n", escape_backslashes(this->originalSrc).c_str());
923    if (this->package.length() != 0) {
924        to->Write("package %s;\n", this->package.c_str());
925    }
926
927    N = this->classes.size();
928    for (i=0; i<N; i++) {
929        Class* c = this->classes[i];
930        c->Write(to);
931    }
932}
933
934}  // namespace aidl
935}  // namespace android
936