This repository has been archived by the owner on Dec 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
type_java.cpp
908 lines (732 loc) · 35.5 KB
/
type_java.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
/*
* Copyright (C) 2015, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "type_java.h"
#include <sys/types.h>
#include <android-base/strings.h>
#include "aidl_language.h"
#include "logging.h"
using std::string;
using android::base::Split;
using android::base::Join;
using android::base::Trim;
namespace android {
namespace aidl {
namespace java {
Expression* NULL_VALUE;
Expression* THIS_VALUE;
Expression* SUPER_VALUE;
Expression* TRUE_VALUE;
Expression* FALSE_VALUE;
// ================================================================
Type::Type(const JavaTypeNamespace* types, const string& name, int kind,
bool canWriteToParcel, bool canBeOut)
: Type(types, "", name, kind, canWriteToParcel, canBeOut, "", -1) {}
Type::Type(const JavaTypeNamespace* types, const string& package,
const string& name, int kind, bool canWriteToParcel,
bool canBeOut, const string& declFile, int declLine)
: ValidatableType(kind, package, name, declFile, declLine),
m_types(types),
m_javaType((package.empty()) ? name : package + "." + name),
m_canWriteToParcel(canWriteToParcel),
m_canBeOut(canBeOut) {
}
string Type::CreatorName() const { return ""; }
string Type::InstantiableName() const { return JavaType(); }
void Type::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel,
int flags) const {
fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%sn", __FILE__,
__LINE__, m_javaType.c_str());
addTo->Add(new LiteralExpression("/* WriteToParcel error " + m_javaType +
" */"));
}
void Type::CreateFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable**) const {
fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n", __FILE__,
__LINE__, m_javaType.c_str());
addTo->Add(new LiteralExpression("/* CreateFromParcel error " +
m_javaType + " */"));
}
void Type::ReadFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel,
Variable**) const {
fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n", __FILE__,
__LINE__, m_javaType.c_str());
addTo->Add(new LiteralExpression("/* ReadFromParcel error " +
m_javaType + " */"));
}
Expression* Type::BuildWriteToParcelFlags(int flags) const {
if (flags == 0) {
return new LiteralExpression("0");
}
if ((flags & PARCELABLE_WRITE_RETURN_VALUE) != 0) {
return new FieldVariable(m_types->ParcelableInterfaceType(),
"PARCELABLE_WRITE_RETURN_VALUE");
}
return new LiteralExpression("0");
}
// ================================================================
BasicType::BasicType(const JavaTypeNamespace* types, const string& name,
const string& marshallParcel,
const string& unmarshallParcel,
const string& writeArrayParcel,
const string& createArrayParcel,
const string& readArrayParcel)
: Type(types, name, ValidatableType::KIND_BUILT_IN, true, false),
m_marshallParcel(marshallParcel),
m_unmarshallParcel(unmarshallParcel) {
m_array_type.reset(new BasicArrayType(types, name, writeArrayParcel,
createArrayParcel, readArrayParcel));
}
void BasicType::WriteToParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, int flags) const {
addTo->Add(new MethodCall(parcel, m_marshallParcel, 1, v));
}
void BasicType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable**) const {
addTo->Add(new Assignment(v, new MethodCall(parcel, m_unmarshallParcel)));
}
BasicArrayType::BasicArrayType(const JavaTypeNamespace* types,
const string& name,
const string& writeArrayParcel,
const string& createArrayParcel,
const string& readArrayParcel)
: Type(types, name, ValidatableType::KIND_BUILT_IN, true, true),
m_writeArrayParcel(writeArrayParcel),
m_createArrayParcel(createArrayParcel),
m_readArrayParcel(readArrayParcel) {}
void BasicArrayType::WriteToParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, int flags) const {
addTo->Add(new MethodCall(parcel, m_writeArrayParcel, 1, v));
}
void BasicArrayType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable**) const {
addTo->Add(new Assignment(v, new MethodCall(parcel, m_createArrayParcel)));
}
void BasicArrayType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable**) const {
addTo->Add(new MethodCall(parcel, m_readArrayParcel, 1, v));
}
// ================================================================
FileDescriptorType::FileDescriptorType(const JavaTypeNamespace* types)
: Type(types, "java.io", "FileDescriptor", ValidatableType::KIND_BUILT_IN,
true, false) {
m_array_type.reset(new FileDescriptorArrayType(types));
}
void FileDescriptorType::WriteToParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, int flags) const {
addTo->Add(new MethodCall(parcel, "writeRawFileDescriptor", 1, v));
}
void FileDescriptorType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable**) const {
addTo->Add(new Assignment(v, new MethodCall(parcel, "readRawFileDescriptor")));
}
FileDescriptorArrayType::FileDescriptorArrayType(const JavaTypeNamespace* types)
: Type(types, "java.io", "FileDescriptor", ValidatableType::KIND_BUILT_IN,
true, true) {}
void FileDescriptorArrayType::WriteToParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, int flags) const {
addTo->Add(new MethodCall(parcel, "writeRawFileDescriptorArray", 1, v));
}
void FileDescriptorArrayType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable**) const {
addTo->Add(new Assignment(v, new MethodCall(parcel, "createRawFileDescriptorArray")));
}
void FileDescriptorArrayType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable**) const {
addTo->Add(new MethodCall(parcel, "readRawFileDescriptorArray", 1, v));
}
// ================================================================
BooleanType::BooleanType(const JavaTypeNamespace* types)
: Type(types, "boolean", ValidatableType::KIND_BUILT_IN, true, false) {
m_array_type.reset(new BooleanArrayType(types));
}
void BooleanType::WriteToParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, int flags) const {
addTo->Add(new MethodCall(
parcel, "writeInt", 1,
new Ternary(v, new LiteralExpression("1"), new LiteralExpression("0"))));
}
void BooleanType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable**) const {
addTo->Add(
new Assignment(v, new Comparison(new LiteralExpression("0"), "!=",
new MethodCall(parcel, "readInt"))));
}
BooleanArrayType::BooleanArrayType(const JavaTypeNamespace* types)
: Type(types, "boolean", ValidatableType::KIND_BUILT_IN, true, true) {}
void BooleanArrayType::WriteToParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, int flags) const {
addTo->Add(new MethodCall(parcel, "writeBooleanArray", 1, v));
}
void BooleanArrayType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable**) const {
addTo->Add(new Assignment(v, new MethodCall(parcel, "createBooleanArray")));
}
void BooleanArrayType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable**) const {
addTo->Add(new MethodCall(parcel, "readBooleanArray", 1, v));
}
// ================================================================
CharType::CharType(const JavaTypeNamespace* types)
: Type(types, "char", ValidatableType::KIND_BUILT_IN, true, false) {
m_array_type.reset(new CharArrayType(types));
}
void CharType::WriteToParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, int flags) const {
addTo->Add(
new MethodCall(parcel, "writeInt", 1, new Cast(m_types->IntType(), v)));
}
void CharType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable**) const {
addTo->Add(new Assignment(v, new MethodCall(parcel, "readInt"), this));
}
CharArrayType::CharArrayType(const JavaTypeNamespace* types)
: Type(types, "char", ValidatableType::KIND_BUILT_IN, true, true) {}
void CharArrayType::WriteToParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, int flags) const {
addTo->Add(new MethodCall(parcel, "writeCharArray", 1, v));
}
void CharArrayType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable**) const {
addTo->Add(new Assignment(v, new MethodCall(parcel, "createCharArray")));
}
void CharArrayType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable**) const {
addTo->Add(new MethodCall(parcel, "readCharArray", 1, v));
}
// ================================================================
StringType::StringType(const JavaTypeNamespace* types,
const std::string& package,
const std::string& class_name)
: Type(types, package, class_name,
ValidatableType::KIND_BUILT_IN, true, false) {
m_array_type.reset(new StringArrayType(types));
}
string StringType::CreatorName() const {
return "android.os.Parcel.STRING_CREATOR";
}
void StringType::WriteToParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, int flags) const {
addTo->Add(new MethodCall(parcel, "writeString", 1, v));
}
void StringType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable**) const {
addTo->Add(new Assignment(v, new MethodCall(parcel, "readString")));
}
StringArrayType::StringArrayType(const JavaTypeNamespace* types)
: Type(types, "java.lang", "String", ValidatableType::KIND_BUILT_IN,
true, true) {}
string StringArrayType::CreatorName() const {
return "android.os.Parcel.STRING_CREATOR";
}
void StringArrayType::WriteToParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, int flags) const {
addTo->Add(new MethodCall(parcel, "writeStringArray", 1, v));
}
void StringArrayType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable**) const {
addTo->Add(new Assignment(v, new MethodCall(parcel, "createStringArray")));
}
void StringArrayType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable**) const {
addTo->Add(new MethodCall(parcel, "readStringArray", 1, v));
}
// ================================================================
CharSequenceType::CharSequenceType(const JavaTypeNamespace* types)
: Type(types, "java.lang", "CharSequence", ValidatableType::KIND_BUILT_IN,
true, false) {}
string CharSequenceType::CreatorName() const {
return "android.os.Parcel.STRING_CREATOR";
}
void CharSequenceType::WriteToParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, int flags) const {
// if (v != null) {
// parcel.writeInt(1);
// v.writeToParcel(parcel);
// } else {
// parcel.writeInt(0);
// }
IfStatement* elsepart = new IfStatement();
elsepart->statements->Add(
new MethodCall(parcel, "writeInt", 1, new LiteralExpression("0")));
IfStatement* ifpart = new IfStatement;
ifpart->expression = new Comparison(v, "!=", NULL_VALUE);
ifpart->elseif = elsepart;
ifpart->statements->Add(
new MethodCall(parcel, "writeInt", 1, new LiteralExpression("1")));
ifpart->statements->Add(new MethodCall(m_types->TextUtilsType(),
"writeToParcel", 3, v, parcel,
BuildWriteToParcelFlags(flags)));
addTo->Add(ifpart);
}
void CharSequenceType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable**) const {
// if (0 != parcel.readInt()) {
// v = TextUtils.createFromParcel(parcel)
// } else {
// v = null;
// }
IfStatement* elsepart = new IfStatement();
elsepart->statements->Add(new Assignment(v, NULL_VALUE));
IfStatement* ifpart = new IfStatement();
ifpart->expression = new Comparison(new LiteralExpression("0"), "!=",
new MethodCall(parcel, "readInt"));
ifpart->elseif = elsepart;
ifpart->statements->Add(new Assignment(
v, new MethodCall(m_types->TextUtilsType(),
"CHAR_SEQUENCE_CREATOR.createFromParcel", 1, parcel)));
addTo->Add(ifpart);
}
// ================================================================
RemoteExceptionType::RemoteExceptionType(const JavaTypeNamespace* types)
: Type(types, "android.os", "RemoteException",
ValidatableType::KIND_BUILT_IN, false, false) {}
void RemoteExceptionType::WriteToParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, int flags) const {
fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
}
void RemoteExceptionType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable**) const {
fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
}
// ================================================================
RuntimeExceptionType::RuntimeExceptionType(const JavaTypeNamespace* types)
: Type(types, "java.lang", "RuntimeException",
ValidatableType::KIND_BUILT_IN, false, false) {}
void RuntimeExceptionType::WriteToParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, int flags) const {
fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
}
void RuntimeExceptionType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel,
Variable**) const {
fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
}
// ================================================================
IBinderType::IBinderType(const JavaTypeNamespace* types)
: Type(types, "android.os", "IBinder", ValidatableType::KIND_BUILT_IN,
true, false) {
m_array_type.reset(new IBinderArrayType(types));
}
void IBinderType::WriteToParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, int flags) const {
addTo->Add(new MethodCall(parcel, "writeStrongBinder", 1, v));
}
void IBinderType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable**) const {
addTo->Add(new Assignment(v, new MethodCall(parcel, "readStrongBinder")));
}
IBinderArrayType::IBinderArrayType(const JavaTypeNamespace* types)
: Type(types, "android.os", "IBinder", ValidatableType::KIND_BUILT_IN,
true, true) {}
void IBinderArrayType::WriteToParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, int flags) const {
addTo->Add(new MethodCall(parcel, "writeBinderArray", 1, v));
}
void IBinderArrayType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable**) const {
addTo->Add(new Assignment(v, new MethodCall(parcel, "createBinderArray")));
}
void IBinderArrayType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable**) const {
addTo->Add(new MethodCall(parcel, "readBinderArray", 1, v));
}
// ================================================================
IInterfaceType::IInterfaceType(const JavaTypeNamespace* types)
: Type(types, "android.os", "IInterface", ValidatableType::KIND_BUILT_IN,
false, false) {}
void IInterfaceType::WriteToParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, int flags) const {
fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
}
void IInterfaceType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable**) const {
fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
}
// ================================================================
BinderType::BinderType(const JavaTypeNamespace* types)
: Type(types, "android.os", "Binder", ValidatableType::KIND_BUILT_IN,
false, false) {}
void BinderType::WriteToParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, int flags) const {
fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
}
void BinderType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable**) const {
fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
}
// ================================================================
BinderProxyType::BinderProxyType(const JavaTypeNamespace* types)
: Type(types, "android.os", "BinderProxy", ValidatableType::KIND_BUILT_IN,
false, false) {}
void BinderProxyType::WriteToParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, int flags) const {
fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
}
void BinderProxyType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable**) const {
fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
}
// ================================================================
ParcelType::ParcelType(const JavaTypeNamespace* types)
: Type(types, "android.os", "Parcel", ValidatableType::KIND_BUILT_IN,
false, false) {}
void ParcelType::WriteToParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, int flags) const {
fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
}
void ParcelType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable**) const {
fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
}
// ================================================================
ParcelableInterfaceType::ParcelableInterfaceType(const JavaTypeNamespace* types)
: Type(types, "android.os", "Parcelable", ValidatableType::KIND_BUILT_IN,
false, false) {}
void ParcelableInterfaceType::WriteToParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, int flags) const {
fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
}
void ParcelableInterfaceType::CreateFromParcel(StatementBlock* addTo,
Variable* v, Variable* parcel,
Variable**) const {
fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
}
// ================================================================
MapType::MapType(const JavaTypeNamespace* types)
: Type(types, "java.util", "Map", ValidatableType::KIND_BUILT_IN,
true, true) {}
void MapType::WriteToParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, int flags) const {
addTo->Add(new MethodCall(parcel, "writeMap", 1, v));
}
static void EnsureClassLoader(StatementBlock* addTo, Variable** cl,
const JavaTypeNamespace* types) {
// We don't want to look up the class loader once for every
// collection argument, so ensure we do it at most once per method.
if (*cl == NULL) {
*cl = new Variable(types->ClassLoaderType(), "cl");
addTo->Add(new VariableDeclaration(
*cl, new LiteralExpression("this.getClass().getClassLoader()"),
types->ClassLoaderType()));
}
}
void MapType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable** cl) const {
EnsureClassLoader(addTo, cl, m_types);
addTo->Add(new Assignment(v, new MethodCall(parcel, "readHashMap", 1, *cl)));
}
void MapType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable** cl) const {
EnsureClassLoader(addTo, cl, m_types);
addTo->Add(new MethodCall(parcel, "readMap", 2, v, *cl));
}
// ================================================================
ListType::ListType(const JavaTypeNamespace* types)
: Type(types, "java.util", "List", ValidatableType::KIND_BUILT_IN,
true, true) {}
string ListType::InstantiableName() const { return "java.util.ArrayList"; }
void ListType::WriteToParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, int flags) const {
addTo->Add(new MethodCall(parcel, "writeList", 1, v));
}
void ListType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable** cl) const {
EnsureClassLoader(addTo, cl, m_types);
addTo->Add(
new Assignment(v, new MethodCall(parcel, "readArrayList", 1, *cl)));
}
void ListType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable** cl) const {
EnsureClassLoader(addTo, cl, m_types);
addTo->Add(new MethodCall(parcel, "readList", 2, v, *cl));
}
// ================================================================
UserDataType::UserDataType(const JavaTypeNamespace* types,
const string& package, const string& name,
bool builtIn, bool canWriteToParcel,
const string& declFile, int declLine)
: Type(types, package, name,
builtIn ? ValidatableType::KIND_BUILT_IN
: ValidatableType::KIND_PARCELABLE,
canWriteToParcel, true, declFile, declLine) {
m_array_type.reset(new UserDataArrayType(types, package, name, builtIn,
canWriteToParcel, declFile,
declLine));
}
string UserDataType::CreatorName() const {
return JavaType() + ".CREATOR";
}
void UserDataType::WriteToParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, int flags) const {
// if (v != null) {
// parcel.writeInt(1);
// v.writeToParcel(parcel);
// } else {
// parcel.writeInt(0);
// }
IfStatement* elsepart = new IfStatement();
elsepart->statements->Add(
new MethodCall(parcel, "writeInt", 1, new LiteralExpression("0")));
IfStatement* ifpart = new IfStatement;
ifpart->expression = new Comparison(v, "!=", NULL_VALUE);
ifpart->elseif = elsepart;
ifpart->statements->Add(
new MethodCall(parcel, "writeInt", 1, new LiteralExpression("1")));
ifpart->statements->Add(new MethodCall(v, "writeToParcel", 2, parcel,
BuildWriteToParcelFlags(flags)));
addTo->Add(ifpart);
}
void UserDataType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable**) const {
// if (0 != parcel.readInt()) {
// v = CLASS.CREATOR.createFromParcel(parcel)
// } else {
// v = null;
// }
IfStatement* elsepart = new IfStatement();
elsepart->statements->Add(new Assignment(v, NULL_VALUE));
IfStatement* ifpart = new IfStatement();
ifpart->expression = new Comparison(new LiteralExpression("0"), "!=",
new MethodCall(parcel, "readInt"));
ifpart->elseif = elsepart;
ifpart->statements->Add(new Assignment(
v, new MethodCall(v->type, "CREATOR.createFromParcel", 1, parcel)));
addTo->Add(ifpart);
}
void UserDataType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable**) const {
// TODO: really, we don't need to have this extra check, but we
// don't have two separate marshalling code paths
// if (0 != parcel.readInt()) {
// v.readFromParcel(parcel)
// }
IfStatement* ifpart = new IfStatement();
ifpart->expression = new Comparison(new LiteralExpression("0"), "!=",
new MethodCall(parcel, "readInt"));
ifpart->statements->Add(new MethodCall(v, "readFromParcel", 1, parcel));
addTo->Add(ifpart);
}
UserDataArrayType::UserDataArrayType(const JavaTypeNamespace* types,
const string& package, const string& name,
bool builtIn, bool canWriteToParcel,
const string& declFile, int declLine)
: Type(types, package, name,
builtIn ? ValidatableType::KIND_BUILT_IN
: ValidatableType::KIND_PARCELABLE,
canWriteToParcel, true, declFile, declLine) {}
string UserDataArrayType::CreatorName() const {
return JavaType() + ".CREATOR";
}
void UserDataArrayType::WriteToParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, int flags) const {
addTo->Add(new MethodCall(parcel, "writeTypedArray", 2, v,
BuildWriteToParcelFlags(flags)));
}
void UserDataArrayType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable**) const {
string creator = v->type->JavaType() + ".CREATOR";
addTo->Add(new Assignment(v, new MethodCall(parcel, "createTypedArray", 1,
new LiteralExpression(creator))));
}
void UserDataArrayType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable**) const {
string creator = v->type->JavaType() + ".CREATOR";
addTo->Add(new MethodCall(parcel, "readTypedArray", 2, v,
new LiteralExpression(creator)));
}
// ================================================================
InterfaceType::InterfaceType(const JavaTypeNamespace* types,
const string& package, const string& name,
bool builtIn, bool oneway, const string& declFile,
int declLine, const Type* stub, const Type* proxy)
: Type(types, package, name, builtIn ? ValidatableType::KIND_BUILT_IN
: ValidatableType::KIND_INTERFACE,
true, false, declFile, declLine),
m_oneway(oneway),
stub_(stub),
proxy_(proxy) {}
bool InterfaceType::OneWay() const { return m_oneway; }
void InterfaceType::WriteToParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, int flags) const {
// parcel.writeStrongBinder(v != null ? v.asBinder() : null);
addTo->Add(
new MethodCall(parcel, "writeStrongBinder", 1,
new Ternary(new Comparison(v, "!=", NULL_VALUE),
new MethodCall(v, "asBinder"), NULL_VALUE)));
}
void InterfaceType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable**) const {
// v = Interface.asInterface(parcel.readStrongBinder());
addTo->Add(new Assignment(
v, new MethodCall(stub_, "asInterface", 1,
new MethodCall(parcel, "readStrongBinder"))));
}
// ================================================================
GenericListType::GenericListType(const JavaTypeNamespace* types,
const Type* contained_type)
: Type(types, "java.util", "List<" + contained_type->CanonicalName() + ">",
ValidatableType::KIND_BUILT_IN, true, true),
m_contained_type(contained_type),
m_creator(contained_type->CreatorName()) {}
string GenericListType::CreatorName() const {
return "android.os.Parcel.arrayListCreator";
}
string GenericListType::InstantiableName() const {
return "java.util.ArrayList<" + m_contained_type->JavaType() + ">";
}
void GenericListType::WriteToParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, int flags) const {
if (m_creator == m_types->StringType()->CreatorName()) {
addTo->Add(new MethodCall(parcel, "writeStringList", 1, v));
} else if (m_creator == m_types->IBinderType()->CreatorName()) {
addTo->Add(new MethodCall(parcel, "writeBinderList", 1, v));
} else {
// parcel.writeTypedListXX(arg);
addTo->Add(new MethodCall(parcel, "writeTypedList", 1, v));
}
}
void GenericListType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable**) const {
if (m_creator == m_types->StringType()->CreatorName()) {
addTo->Add(
new Assignment(v, new MethodCall(parcel, "createStringArrayList", 0)));
} else if (m_creator == m_types->IBinderType()->CreatorName()) {
addTo->Add(
new Assignment(v, new MethodCall(parcel, "createBinderArrayList", 0)));
} else {
// v = _data.readTypedArrayList(XXX.creator);
addTo->Add(
new Assignment(v, new MethodCall(parcel, "createTypedArrayList", 1,
new LiteralExpression(m_creator))));
}
}
void GenericListType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Variable* parcel, Variable**) const {
if (m_creator == m_types->StringType()->CreatorName()) {
addTo->Add(new MethodCall(parcel, "readStringList", 1, v));
} else if (m_creator == m_types->IBinderType()->CreatorName()) {
addTo->Add(new MethodCall(parcel, "readBinderList", 1, v));
} else {
// v = _data.readTypedList(v, XXX.creator);
addTo->Add(new MethodCall(parcel, "readTypedList", 2, v,
new LiteralExpression(m_creator)));
}
}
// ================================================================
ClassLoaderType::ClassLoaderType(const JavaTypeNamespace* types)
: Type(types, "java.lang", "ClassLoader", ValidatableType::KIND_BUILT_IN,
false, false) {}
// ================================================================
void JavaTypeNamespace::Init() {
Add(new BasicType(this, "void", "XXX", "XXX", "XXX", "XXX", "XXX"));
m_bool_type = new BooleanType(this);
Add(m_bool_type);
Add(new BasicType(this, "byte", "writeByte", "readByte", "writeByteArray",
"createByteArray", "readByteArray"));
Add(new CharType(this));
m_int_type = new BasicType(this, "int", "writeInt", "readInt",
"writeIntArray", "createIntArray", "readIntArray");
Add(m_int_type);
Add(new BasicType(this, "long", "writeLong", "readLong", "writeLongArray",
"createLongArray", "readLongArray"));
Add(new BasicType(this, "float", "writeFloat", "readFloat", "writeFloatArray",
"createFloatArray", "readFloatArray"));
Add(new BasicType(this, "double", "writeDouble", "readDouble",
"writeDoubleArray", "createDoubleArray",
"readDoubleArray"));
m_string_type = new class StringType(this, "java.lang", "String");
Add(m_string_type);
Add(new class StringType(this, ::android::aidl::kAidlReservedTypePackage,
::android::aidl::kUtf8InCppStringClass));
Add(new Type(this, "java.lang", "Object", ValidatableType::KIND_BUILT_IN,
false, false));
Add(new FileDescriptorType(this));
Add(new CharSequenceType(this));
Add(new MapType(this));
Add(new ListType(this));
m_text_utils_type =
new Type(this, "android.text", "TextUtils",
ValidatableType::KIND_BUILT_IN, false, false);
Add(m_text_utils_type);
m_remote_exception_type = new class RemoteExceptionType(this);
Add(m_remote_exception_type);
m_runtime_exception_type = new class RuntimeExceptionType(this);
Add(m_runtime_exception_type);
m_ibinder_type = new class IBinderType(this);
Add(m_ibinder_type);
m_iinterface_type = new class IInterfaceType(this);
Add(m_iinterface_type);
m_binder_native_type = new class BinderType(this);
Add(m_binder_native_type);
m_binder_proxy_type = new class BinderProxyType(this);
Add(m_binder_proxy_type);
m_parcel_type = new class ParcelType(this);
Add(m_parcel_type);
m_parcelable_interface_type = new class ParcelableInterfaceType(this);
Add(m_parcelable_interface_type);
m_context_type = new class Type(this, "android.content", "Context",
ValidatableType::KIND_BUILT_IN, false, false);
Add(m_context_type);
m_classloader_type = new class ClassLoaderType(this);
Add(m_classloader_type);
NULL_VALUE = new LiteralExpression("null");
THIS_VALUE = new LiteralExpression("this");
SUPER_VALUE = new LiteralExpression("super");
TRUE_VALUE = new LiteralExpression("true");
FALSE_VALUE = new LiteralExpression("false");
}
bool JavaTypeNamespace::AddParcelableType(const AidlParcelable& p,
const std::string& filename) {
Type* type =
new UserDataType(this, p.GetPackage(), p.GetName(), false,
true, filename, p.GetLine());
return Add(type);
}
bool JavaTypeNamespace::AddBinderType(const AidlInterface& b,
const std::string& filename) {
// for interfaces, add the stub, proxy, and interface types.
Type* stub = new Type(this, b.GetPackage(),
b.GetName() + ".Stub", ValidatableType::KIND_GENERATED,
false, false, filename, b.GetLine());
Type* proxy = new Type(this, b.GetPackage(),
b.GetName() + ".Stub.Proxy",
ValidatableType::KIND_GENERATED,
false, false, filename, b.GetLine());
Type* type =
new InterfaceType(this, b.GetPackage(), b.GetName(), false,
b.IsOneway(), filename, b.GetLine(), stub, proxy);
bool success = true;
success &= Add(type);
success &= Add(stub);
success &= Add(proxy);
return success;
}
bool JavaTypeNamespace::AddListType(const std::string& contained_type_name) {
const Type* contained_type = FindTypeByCanonicalName(contained_type_name);
if (!contained_type) {
return false;
}
Add(new GenericListType(this, contained_type));
return true;
}
bool JavaTypeNamespace::AddMapType(const string& key_type_name,
const string& value_type_name) {
LOG(ERROR) << "Don't know how to create a Map<K,V> container.";
return false;
}
} // namespace java
} // namespace aidl
} // namespace android