blob: 31a8926e0c2d71f33e85ad1270dcfed10fa827f0 [file] [log] [blame]
The Android Open Source Project46c012c2008-10-21 07:00:00 -07001#include "Type.h"
2
David 'Digit' Turnera3372982014-03-04 16:43:41 +01003#include <sys/types.h>
4
Christopher Wileyfdeb0f42015-09-11 15:38:22 -07005namespace android {
6namespace aidl {
7
The Android Open Source Project46c012c2008-10-21 07:00:00 -07008Namespace NAMES;
9
10Type* VOID_TYPE;
11Type* BOOLEAN_TYPE;
12Type* BYTE_TYPE;
13Type* CHAR_TYPE;
14Type* INT_TYPE;
15Type* LONG_TYPE;
16Type* FLOAT_TYPE;
17Type* DOUBLE_TYPE;
18Type* STRING_TYPE;
Joe Onoratoc596cfe2011-08-30 17:24:17 -070019Type* OBJECT_TYPE;
The Android Open Source Project46c012c2008-10-21 07:00:00 -070020Type* CHAR_SEQUENCE_TYPE;
21Type* TEXT_UTILS_TYPE;
22Type* REMOTE_EXCEPTION_TYPE;
23Type* RUNTIME_EXCEPTION_TYPE;
24Type* IBINDER_TYPE;
25Type* IINTERFACE_TYPE;
26Type* BINDER_NATIVE_TYPE;
27Type* BINDER_PROXY_TYPE;
28Type* PARCEL_TYPE;
29Type* PARCELABLE_INTERFACE_TYPE;
Joe Onoratoc596cfe2011-08-30 17:24:17 -070030Type* CONTEXT_TYPE;
The Android Open Source Project46c012c2008-10-21 07:00:00 -070031Type* MAP_TYPE;
32Type* LIST_TYPE;
33Type* CLASSLOADER_TYPE;
34
35Expression* NULL_VALUE;
36Expression* THIS_VALUE;
37Expression* SUPER_VALUE;
38Expression* TRUE_VALUE;
39Expression* FALSE_VALUE;
40
41void
42register_base_types()
43{
Joe Onorato3d0e06f2011-09-02 15:28:36 -070044 VOID_TYPE = new BasicType("void",
Joe Onorato3d0e06f2011-09-02 15:28:36 -070045 "XXX", "XXX", "XXX", "XXX", "XXX");
The Android Open Source Project46c012c2008-10-21 07:00:00 -070046 NAMES.Add(VOID_TYPE);
47
48 BOOLEAN_TYPE = new BooleanType();
49 NAMES.Add(BOOLEAN_TYPE);
50
Joe Onorato3d0e06f2011-09-02 15:28:36 -070051 BYTE_TYPE = new BasicType("byte",
Casey Dahlin88868fc2015-09-01 13:21:26 -070052 "writeByte", "readByte", "writeByteArray", "createByteArray",
53 "readByteArray");
The Android Open Source Project46c012c2008-10-21 07:00:00 -070054 NAMES.Add(BYTE_TYPE);
55
56 CHAR_TYPE = new CharType();
57 NAMES.Add(CHAR_TYPE);
58
Joe Onorato3d0e06f2011-09-02 15:28:36 -070059 INT_TYPE = new BasicType("int",
Casey Dahlin88868fc2015-09-01 13:21:26 -070060 "writeInt", "readInt", "writeIntArray", "createIntArray",
61 "readIntArray");
The Android Open Source Project46c012c2008-10-21 07:00:00 -070062 NAMES.Add(INT_TYPE);
63
Joe Onorato3d0e06f2011-09-02 15:28:36 -070064 LONG_TYPE = new BasicType("long",
Casey Dahlin88868fc2015-09-01 13:21:26 -070065 "writeLong", "readLong", "writeLongArray", "createLongArray",
66 "readLongArray");
The Android Open Source Project46c012c2008-10-21 07:00:00 -070067 NAMES.Add(LONG_TYPE);
68
Joe Onorato3d0e06f2011-09-02 15:28:36 -070069 FLOAT_TYPE = new BasicType("float",
Casey Dahlin88868fc2015-09-01 13:21:26 -070070 "writeFloat", "readFloat", "writeFloatArray", "createFloatArray",
71 "readFloatArray");
The Android Open Source Project46c012c2008-10-21 07:00:00 -070072 NAMES.Add(FLOAT_TYPE);
73
Joe Onorato3d0e06f2011-09-02 15:28:36 -070074 DOUBLE_TYPE = new BasicType("double",
Casey Dahlin88868fc2015-09-01 13:21:26 -070075 "writeDouble", "readDouble", "writeDoubleArray",
76 "createDoubleArray", "readDoubleArray");
The Android Open Source Project46c012c2008-10-21 07:00:00 -070077 NAMES.Add(DOUBLE_TYPE);
78
79 STRING_TYPE = new StringType();
80 NAMES.Add(STRING_TYPE);
81
Casey Dahlin88868fc2015-09-01 13:21:26 -070082 OBJECT_TYPE = new Type("java.lang", "Object", Type::BUILT_IN, false, false);
Joe Onoratoc596cfe2011-08-30 17:24:17 -070083 NAMES.Add(OBJECT_TYPE);
84
The Android Open Source Project46c012c2008-10-21 07:00:00 -070085 CHAR_SEQUENCE_TYPE = new CharSequenceType();
86 NAMES.Add(CHAR_SEQUENCE_TYPE);
87
88 MAP_TYPE = new MapType();
89 NAMES.Add(MAP_TYPE);
90
91 LIST_TYPE = new ListType();
92 NAMES.Add(LIST_TYPE);
93
Casey Dahlin88868fc2015-09-01 13:21:26 -070094 TEXT_UTILS_TYPE = new Type("android.text", "TextUtils", Type::BUILT_IN, false, false);
The Android Open Source Project46c012c2008-10-21 07:00:00 -070095 NAMES.Add(TEXT_UTILS_TYPE);
96
97 REMOTE_EXCEPTION_TYPE = new RemoteExceptionType();
98 NAMES.Add(REMOTE_EXCEPTION_TYPE);
99
100 RUNTIME_EXCEPTION_TYPE = new RuntimeExceptionType();
101 NAMES.Add(RUNTIME_EXCEPTION_TYPE);
102
103 IBINDER_TYPE = new IBinderType();
104 NAMES.Add(IBINDER_TYPE);
105
106 IINTERFACE_TYPE = new IInterfaceType();
107 NAMES.Add(IINTERFACE_TYPE);
108
109 BINDER_NATIVE_TYPE = new BinderType();
110 NAMES.Add(BINDER_NATIVE_TYPE);
111
112 BINDER_PROXY_TYPE = new BinderProxyType();
113 NAMES.Add(BINDER_PROXY_TYPE);
114
115 PARCEL_TYPE = new ParcelType();
116 NAMES.Add(PARCEL_TYPE);
117
118 PARCELABLE_INTERFACE_TYPE = new ParcelableInterfaceType();
119 NAMES.Add(PARCELABLE_INTERFACE_TYPE);
120
Casey Dahlin88868fc2015-09-01 13:21:26 -0700121 CONTEXT_TYPE = new Type("android.content", "Context", Type::BUILT_IN, false, false);
Joe Onoratoc596cfe2011-08-30 17:24:17 -0700122 NAMES.Add(CONTEXT_TYPE);
123
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700124 CLASSLOADER_TYPE = new ClassLoaderType();
125 NAMES.Add(CLASSLOADER_TYPE);
126
127 NULL_VALUE = new LiteralExpression("null");
128 THIS_VALUE = new LiteralExpression("this");
129 SUPER_VALUE = new LiteralExpression("super");
130 TRUE_VALUE = new LiteralExpression("true");
131 FALSE_VALUE = new LiteralExpression("false");
132
133 NAMES.AddGenericType("java.util", "List", 1);
134 NAMES.AddGenericType("java.util", "Map", 2);
135}
136
137static Type*
138make_generic_type(const string& package, const string& name,
139 const vector<Type*>& args)
140{
141 if (package == "java.util" && name == "List") {
142 return new GenericListType("java.util", "List", args);
143 }
144 return NULL;
145 //return new GenericType(package, name, args);
146}
147
148// ================================================================
149
Casey Dahlin88868fc2015-09-01 13:21:26 -0700150Type::Type(const string& name, int kind, bool canWriteToParcel, bool canBeOut)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700151 :m_package(),
152 m_name(name),
153 m_declFile(""),
154 m_declLine(-1),
155 m_kind(kind),
156 m_canWriteToParcel(canWriteToParcel),
157 m_canBeOut(canBeOut)
158{
159 m_qualifiedName = name;
160}
161
162Type::Type(const string& package, const string& name,
Casey Dahlin88868fc2015-09-01 13:21:26 -0700163 int kind, bool canWriteToParcel, bool canBeOut,
164 const string& declFile, int declLine)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700165 :m_package(package),
166 m_name(name),
167 m_declFile(declFile),
168 m_declLine(declLine),
169 m_kind(kind),
170 m_canWriteToParcel(canWriteToParcel),
171 m_canBeOut(canBeOut)
172{
173 if (package.length() > 0) {
174 m_qualifiedName = package;
175 m_qualifiedName += '.';
176 }
177 m_qualifiedName += name;
178}
179
180Type::~Type()
181{
182}
183
Christopher Wileyf690be52015-09-14 15:19:10 -0700184string
185Type::HumanReadableKind() const {
186 switch (Kind())
187 {
188 case INTERFACE:
189 return "an interface";
190 case USERDATA:
191 return "a user data";
192 default:
193 return "ERROR";
194 }
195}
196
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700197bool
198Type::CanBeArray() const
199{
200 return false;
201}
202
203string
204Type::ImportType() const
205{
206 return m_qualifiedName;
207}
208
209string
210Type::CreatorName() const
211{
212 return "";
213}
214
215string
216Type::InstantiableName() const
217{
218 return QualifiedName();
219}
220
221
222void
223Type::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
224{
225 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%sn",
226 __FILE__, __LINE__, m_qualifiedName.c_str());
227 addTo->Add(new LiteralExpression("/* WriteToParcel error "
228 + m_qualifiedName + " */"));
229}
230
231void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700232Type::CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700233{
234 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n",
235 __FILE__, __LINE__, m_qualifiedName.c_str());
236 addTo->Add(new LiteralExpression("/* CreateFromParcel error "
237 + m_qualifiedName + " */"));
238}
239
240void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700241Type::ReadFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700242{
243 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n",
244 __FILE__, __LINE__, m_qualifiedName.c_str());
245 addTo->Add(new LiteralExpression("/* ReadFromParcel error "
246 + m_qualifiedName + " */"));
247}
248
249void
250Type::WriteArrayToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
251{
252 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n",
253 __FILE__, __LINE__, m_qualifiedName.c_str());
254 addTo->Add(new LiteralExpression("/* WriteArrayToParcel error "
255 + m_qualifiedName + " */"));
256}
257
258void
259Type::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Elliott Hughes15f8da22011-07-13 12:10:30 -0700260 Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700261{
262 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n",
263 __FILE__, __LINE__, m_qualifiedName.c_str());
264 addTo->Add(new LiteralExpression("/* CreateArrayFromParcel error "
265 + m_qualifiedName + " */"));
266}
267
268void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700269Type::ReadArrayFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700270{
271 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n",
272 __FILE__, __LINE__, m_qualifiedName.c_str());
273 addTo->Add(new LiteralExpression("/* ReadArrayFromParcel error "
274 + m_qualifiedName + " */"));
275}
276
277void
278Type::SetQualifiedName(const string& qualified)
279{
280 m_qualifiedName = qualified;
281}
282
283Expression*
284Type::BuildWriteToParcelFlags(int flags)
285{
286 if (flags == 0) {
287 return new LiteralExpression("0");
288 }
289 if ((flags&PARCELABLE_WRITE_RETURN_VALUE) != 0) {
290 return new FieldVariable(PARCELABLE_INTERFACE_TYPE,
291 "PARCELABLE_WRITE_RETURN_VALUE");
292 }
293 return new LiteralExpression("0");
294}
295
296// ================================================================
297
Joe Onorato3d0e06f2011-09-02 15:28:36 -0700298BasicType::BasicType(const string& name, const string& marshallParcel,
299 const string& unmarshallParcel, const string& writeArrayParcel,
Casey Dahlin88868fc2015-09-01 13:21:26 -0700300 const string& createArrayParcel, const string& readArrayParcel)
301 :Type(name, BUILT_IN, true, false),
Joe Onorato3d0e06f2011-09-02 15:28:36 -0700302 m_marshallParcel(marshallParcel),
303 m_unmarshallParcel(unmarshallParcel),
304 m_writeArrayParcel(writeArrayParcel),
305 m_createArrayParcel(createArrayParcel),
Casey Dahlin88868fc2015-09-01 13:21:26 -0700306 m_readArrayParcel(readArrayParcel)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700307{
308}
309
310void
311BasicType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
312{
Joe Onorato3d0e06f2011-09-02 15:28:36 -0700313 addTo->Add(new MethodCall(parcel, m_marshallParcel, 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700314}
315
316void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700317BasicType::CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700318{
Joe Onorato3d0e06f2011-09-02 15:28:36 -0700319 addTo->Add(new Assignment(v, new MethodCall(parcel, m_unmarshallParcel)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700320}
321
322bool
323BasicType::CanBeArray() const
324{
325 return true;
326}
327
328void
329BasicType::WriteArrayToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
330{
Joe Onorato3d0e06f2011-09-02 15:28:36 -0700331 addTo->Add(new MethodCall(parcel, m_writeArrayParcel, 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700332}
333
334void
335BasicType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Elliott Hughes15f8da22011-07-13 12:10:30 -0700336 Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700337{
Joe Onorato3d0e06f2011-09-02 15:28:36 -0700338 addTo->Add(new Assignment(v, new MethodCall(parcel, m_createArrayParcel)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700339}
340
341void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700342BasicType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700343{
Joe Onorato3d0e06f2011-09-02 15:28:36 -0700344 addTo->Add(new MethodCall(parcel, m_readArrayParcel, 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700345}
346
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700347// ================================================================
348
349BooleanType::BooleanType()
Casey Dahlin88868fc2015-09-01 13:21:26 -0700350 :Type("boolean", BUILT_IN, true, false)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700351{
352}
353
354void
355BooleanType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
356{
357 addTo->Add(new MethodCall(parcel, "writeInt", 1,
358 new Ternary(v, new LiteralExpression("1"),
359 new LiteralExpression("0"))));
360}
361
362void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700363BooleanType::CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700364{
365 addTo->Add(new Assignment(v, new Comparison(new LiteralExpression("0"),
366 "!=", new MethodCall(parcel, "readInt"))));
367}
368
369bool
370BooleanType::CanBeArray() const
371{
372 return true;
373}
374
375void
376BooleanType::WriteArrayToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
377{
378 addTo->Add(new MethodCall(parcel, "writeBooleanArray", 1, v));
379}
380
381void
382BooleanType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Elliott Hughes15f8da22011-07-13 12:10:30 -0700383 Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700384{
385 addTo->Add(new Assignment(v, new MethodCall(parcel, "createBooleanArray")));
386}
387
388void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700389BooleanType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700390{
391 addTo->Add(new MethodCall(parcel, "readBooleanArray", 1, v));
392}
393
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700394// ================================================================
395
396CharType::CharType()
Casey Dahlin88868fc2015-09-01 13:21:26 -0700397 :Type("char", BUILT_IN, true, false)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700398{
399}
400
401void
402CharType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
403{
404 addTo->Add(new MethodCall(parcel, "writeInt", 1,
405 new Cast(INT_TYPE, v)));
406}
407
408void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700409CharType::CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700410{
411 addTo->Add(new Assignment(v, new MethodCall(parcel, "readInt"), this));
412}
413
414bool
415CharType::CanBeArray() const
416{
417 return true;
418}
419
420void
421CharType::WriteArrayToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
422{
423 addTo->Add(new MethodCall(parcel, "writeCharArray", 1, v));
424}
425
426void
427CharType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Elliott Hughes15f8da22011-07-13 12:10:30 -0700428 Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700429{
430 addTo->Add(new Assignment(v, new MethodCall(parcel, "createCharArray")));
431}
432
433void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700434CharType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700435{
436 addTo->Add(new MethodCall(parcel, "readCharArray", 1, v));
437}
438
439// ================================================================
440
441StringType::StringType()
Casey Dahlin88868fc2015-09-01 13:21:26 -0700442 :Type("java.lang", "String", BUILT_IN, true, false)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700443{
444}
445
446string
447StringType::CreatorName() const
448{
449 return "android.os.Parcel.STRING_CREATOR";
450}
451
452void
453StringType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
454{
455 addTo->Add(new MethodCall(parcel, "writeString", 1, v));
456}
457
458void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700459StringType::CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700460{
461 addTo->Add(new Assignment(v, new MethodCall(parcel, "readString")));
462}
463
464bool
465StringType::CanBeArray() const
466{
467 return true;
468}
469
470void
471StringType::WriteArrayToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
472{
473 addTo->Add(new MethodCall(parcel, "writeStringArray", 1, v));
474}
475
476void
477StringType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Elliott Hughes15f8da22011-07-13 12:10:30 -0700478 Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700479{
480 addTo->Add(new Assignment(v, new MethodCall(parcel, "createStringArray")));
481}
482
483void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700484StringType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700485{
486 addTo->Add(new MethodCall(parcel, "readStringArray", 1, v));
487}
488
489// ================================================================
490
491CharSequenceType::CharSequenceType()
Casey Dahlin88868fc2015-09-01 13:21:26 -0700492 :Type("java.lang", "CharSequence", BUILT_IN, true, false)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700493{
494}
495
496string
497CharSequenceType::CreatorName() const
498{
499 return "android.os.Parcel.STRING_CREATOR";
500}
501
502void
503CharSequenceType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
504{
505 // if (v != null) {
506 // parcel.writeInt(1);
507 // v.writeToParcel(parcel);
508 // } else {
509 // parcel.writeInt(0);
510 // }
511 IfStatement* elsepart = new IfStatement();
512 elsepart->statements->Add(new MethodCall(parcel, "writeInt", 1,
513 new LiteralExpression("0")));
514 IfStatement* ifpart = new IfStatement;
515 ifpart->expression = new Comparison(v, "!=", NULL_VALUE);
516 ifpart->elseif = elsepart;
517 ifpart->statements->Add(new MethodCall(parcel, "writeInt", 1,
518 new LiteralExpression("1")));
519 ifpart->statements->Add(new MethodCall(TEXT_UTILS_TYPE, "writeToParcel",
520 3, v, parcel, BuildWriteToParcelFlags(flags)));
521
522 addTo->Add(ifpart);
523}
524
525void
526CharSequenceType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Elliott Hughes15f8da22011-07-13 12:10:30 -0700527 Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700528{
529 // if (0 != parcel.readInt()) {
530 // v = TextUtils.createFromParcel(parcel)
531 // } else {
532 // v = null;
533 // }
534 IfStatement* elsepart = new IfStatement();
535 elsepart->statements->Add(new Assignment(v, NULL_VALUE));
536
537 IfStatement* ifpart = new IfStatement();
538 ifpart->expression = new Comparison(new LiteralExpression("0"), "!=",
539 new MethodCall(parcel, "readInt"));
540 ifpart->elseif = elsepart;
541 ifpart->statements->Add(new Assignment(v,
542 new MethodCall(TEXT_UTILS_TYPE,
543 "CHAR_SEQUENCE_CREATOR.createFromParcel", 1, parcel)));
544
545 addTo->Add(ifpart);
546}
547
548
549// ================================================================
550
551RemoteExceptionType::RemoteExceptionType()
Casey Dahlin88868fc2015-09-01 13:21:26 -0700552 :Type("android.os", "RemoteException", BUILT_IN, false, false)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700553{
554}
555
556void
557RemoteExceptionType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
558{
559 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
560}
561
562void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700563RemoteExceptionType::CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700564{
565 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
566}
567
568// ================================================================
569
570RuntimeExceptionType::RuntimeExceptionType()
Casey Dahlin88868fc2015-09-01 13:21:26 -0700571 :Type("java.lang", "RuntimeException", BUILT_IN, false, false)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700572{
573}
574
575void
576RuntimeExceptionType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
577{
578 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
579}
580
581void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700582RuntimeExceptionType::CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700583{
584 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
585}
586
587
588// ================================================================
589
590IBinderType::IBinderType()
Casey Dahlin88868fc2015-09-01 13:21:26 -0700591 :Type("android.os", "IBinder", BUILT_IN, true, false)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700592{
593}
594
595void
596IBinderType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
597{
598 addTo->Add(new MethodCall(parcel, "writeStrongBinder", 1, v));
599}
600
601void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700602IBinderType::CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700603{
604 addTo->Add(new Assignment(v, new MethodCall(parcel, "readStrongBinder")));
605}
606
607void
608IBinderType::WriteArrayToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
609{
610 addTo->Add(new MethodCall(parcel, "writeBinderArray", 1, v));
611}
612
613void
614IBinderType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Elliott Hughes15f8da22011-07-13 12:10:30 -0700615 Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700616{
617 addTo->Add(new Assignment(v, new MethodCall(parcel, "createBinderArray")));
618}
619
620void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700621IBinderType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700622{
623 addTo->Add(new MethodCall(parcel, "readBinderArray", 1, v));
624}
625
626
627// ================================================================
628
629IInterfaceType::IInterfaceType()
Casey Dahlin88868fc2015-09-01 13:21:26 -0700630 :Type("android.os", "IInterface", BUILT_IN, false, false)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700631{
632}
633
634void
635IInterfaceType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
636{
637 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
638}
639
640void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700641IInterfaceType::CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700642{
643 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
644}
645
646
647// ================================================================
648
649BinderType::BinderType()
Casey Dahlin88868fc2015-09-01 13:21:26 -0700650 :Type("android.os", "Binder", BUILT_IN, false, false)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700651{
652}
653
654void
655BinderType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
656{
657 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
658}
659
660void
661BinderType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Elliott Hughes15f8da22011-07-13 12:10:30 -0700662 Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700663{
664 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
665}
666
667
668// ================================================================
669
670BinderProxyType::BinderProxyType()
Casey Dahlin88868fc2015-09-01 13:21:26 -0700671 :Type("android.os", "BinderProxy", BUILT_IN, false, false)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700672{
673}
674
675void
676BinderProxyType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
677{
678 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
679}
680
681void
682BinderProxyType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Elliott Hughes15f8da22011-07-13 12:10:30 -0700683 Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700684{
685 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
686}
687
688
689// ================================================================
690
691ParcelType::ParcelType()
Casey Dahlin88868fc2015-09-01 13:21:26 -0700692 :Type("android.os", "Parcel", BUILT_IN, false, false)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700693{
694}
695
696void
697ParcelType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
698{
699 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
700}
701
702void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700703ParcelType::CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700704{
705 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
706}
707
708// ================================================================
709
710ParcelableInterfaceType::ParcelableInterfaceType()
Casey Dahlin88868fc2015-09-01 13:21:26 -0700711 :Type("android.os", "Parcelable", BUILT_IN, false, false)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700712{
713}
714
715void
716ParcelableInterfaceType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
717{
718 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
719}
720
721void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700722ParcelableInterfaceType::CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700723{
724 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
725}
726
727// ================================================================
728
729MapType::MapType()
Casey Dahlin88868fc2015-09-01 13:21:26 -0700730 :Type("java.util", "Map", BUILT_IN, true, true)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700731{
732}
733
734void
735MapType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
736{
737 addTo->Add(new MethodCall(parcel, "writeMap", 1, v));
738}
739
Elliott Hughes15f8da22011-07-13 12:10:30 -0700740static void EnsureClassLoader(StatementBlock* addTo, Variable** cl)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700741{
Elliott Hughes15f8da22011-07-13 12:10:30 -0700742 // We don't want to look up the class loader once for every
743 // collection argument, so ensure we do it at most once per method.
744 if (*cl == NULL) {
745 *cl = new Variable(CLASSLOADER_TYPE, "cl");
746 addTo->Add(new VariableDeclaration(*cl,
747 new LiteralExpression("this.getClass().getClassLoader()"),
748 CLASSLOADER_TYPE));
749 }
750}
751
752void
753MapType::CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable** cl)
754{
755 EnsureClassLoader(addTo, cl);
756 addTo->Add(new Assignment(v, new MethodCall(parcel, "readHashMap", 1, *cl)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700757}
758
759void
Joe Onorato3d0e06f2011-09-02 15:28:36 -0700760MapType::ReadFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable** cl)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700761{
Elliott Hughes15f8da22011-07-13 12:10:30 -0700762 EnsureClassLoader(addTo, cl);
763 addTo->Add(new MethodCall(parcel, "readMap", 2, v, *cl));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700764}
765
766
767// ================================================================
768
769ListType::ListType()
Casey Dahlin88868fc2015-09-01 13:21:26 -0700770 :Type("java.util", "List", BUILT_IN, true, true)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700771{
772}
773
774string
775ListType::InstantiableName() const
776{
777 return "java.util.ArrayList";
778}
779
780void
781ListType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
782{
783 addTo->Add(new MethodCall(parcel, "writeList", 1, v));
784}
785
786void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700787ListType::CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable** cl)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700788{
Elliott Hughes15f8da22011-07-13 12:10:30 -0700789 EnsureClassLoader(addTo, cl);
790 addTo->Add(new Assignment(v, new MethodCall(parcel, "readArrayList", 1, *cl)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700791}
792
793void
794ListType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Elliott Hughes15f8da22011-07-13 12:10:30 -0700795 Variable* parcel, Variable** cl)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700796{
Elliott Hughes15f8da22011-07-13 12:10:30 -0700797 EnsureClassLoader(addTo, cl);
798 addTo->Add(new MethodCall(parcel, "readList", 2, v, *cl));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700799}
800
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700801// ================================================================
802
Joe Onoratobaaf9c82011-10-09 22:31:16 -0700803UserDataType::UserDataType(const string& package, const string& name,
Casey Dahlin88868fc2015-09-01 13:21:26 -0700804 bool builtIn, bool canWriteToParcel,
Joe Onoratobaaf9c82011-10-09 22:31:16 -0700805 const string& declFile, int declLine)
Casey Dahlin88868fc2015-09-01 13:21:26 -0700806 :Type(package, name, builtIn ? BUILT_IN : USERDATA, canWriteToParcel,
Joe Onoratobaaf9c82011-10-09 22:31:16 -0700807 true, declFile, declLine)
Joe Onorato44050522011-10-09 17:38:20 -0700808{
809}
810
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700811string
Joe Onoratobaaf9c82011-10-09 22:31:16 -0700812UserDataType::CreatorName() const
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700813{
814 return QualifiedName() + ".CREATOR";
815}
816
817void
Joe Onoratobaaf9c82011-10-09 22:31:16 -0700818UserDataType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700819{
820 // if (v != null) {
821 // parcel.writeInt(1);
822 // v.writeToParcel(parcel);
823 // } else {
824 // parcel.writeInt(0);
825 // }
826 IfStatement* elsepart = new IfStatement();
827 elsepart->statements->Add(new MethodCall(parcel, "writeInt", 1,
828 new LiteralExpression("0")));
829 IfStatement* ifpart = new IfStatement;
830 ifpart->expression = new Comparison(v, "!=", NULL_VALUE);
831 ifpart->elseif = elsepart;
832 ifpart->statements->Add(new MethodCall(parcel, "writeInt", 1,
833 new LiteralExpression("1")));
834 ifpart->statements->Add(new MethodCall(v, "writeToParcel", 2,
835 parcel, BuildWriteToParcelFlags(flags)));
836
837 addTo->Add(ifpart);
838}
839
840void
Joe Onoratobaaf9c82011-10-09 22:31:16 -0700841UserDataType::CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700842{
843 // if (0 != parcel.readInt()) {
844 // v = CLASS.CREATOR.createFromParcel(parcel)
845 // } else {
846 // v = null;
847 // }
848 IfStatement* elsepart = new IfStatement();
849 elsepart->statements->Add(new Assignment(v, NULL_VALUE));
850
851 IfStatement* ifpart = new IfStatement();
852 ifpart->expression = new Comparison(new LiteralExpression("0"), "!=",
853 new MethodCall(parcel, "readInt"));
854 ifpart->elseif = elsepart;
855 ifpart->statements->Add(new Assignment(v,
856 new MethodCall(v->type, "CREATOR.createFromParcel", 1, parcel)));
857
858 addTo->Add(ifpart);
859}
860
861void
Joe Onoratobaaf9c82011-10-09 22:31:16 -0700862UserDataType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Elliott Hughes15f8da22011-07-13 12:10:30 -0700863 Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700864{
865 // TODO: really, we don't need to have this extra check, but we
866 // don't have two separate marshalling code paths
867 // if (0 != parcel.readInt()) {
868 // v.readFromParcel(parcel)
869 // }
870 IfStatement* ifpart = new IfStatement();
871 ifpart->expression = new Comparison(new LiteralExpression("0"), "!=",
872 new MethodCall(parcel, "readInt"));
873 ifpart->statements->Add(new MethodCall(v, "readFromParcel", 1, parcel));
874 addTo->Add(ifpart);
875}
876
877bool
Joe Onoratobaaf9c82011-10-09 22:31:16 -0700878UserDataType::CanBeArray() const
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700879{
880 return true;
881}
882
883void
Joe Onoratobaaf9c82011-10-09 22:31:16 -0700884UserDataType::WriteArrayToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700885{
886 addTo->Add(new MethodCall(parcel, "writeTypedArray", 2, v,
887 BuildWriteToParcelFlags(flags)));
888}
889
890void
Joe Onoratobaaf9c82011-10-09 22:31:16 -0700891UserDataType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Elliott Hughes15f8da22011-07-13 12:10:30 -0700892 Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700893{
894 string creator = v->type->QualifiedName() + ".CREATOR";
895 addTo->Add(new Assignment(v, new MethodCall(parcel,
896 "createTypedArray", 1, new LiteralExpression(creator))));
897}
898
899void
Joe Onoratobaaf9c82011-10-09 22:31:16 -0700900UserDataType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700901{
902 string creator = v->type->QualifiedName() + ".CREATOR";
903 addTo->Add(new MethodCall(parcel, "readTypedArray", 2,
904 v, new LiteralExpression(creator)));
905}
906
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700907// ================================================================
908
909InterfaceType::InterfaceType(const string& package, const string& name,
910 bool builtIn, bool oneway,
911 const string& declFile, int declLine)
Casey Dahlin88868fc2015-09-01 13:21:26 -0700912 :Type(package, name, builtIn ? BUILT_IN : INTERFACE, true, false,
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700913 declFile, declLine)
914 ,m_oneway(oneway)
915{
916}
917
918bool
919InterfaceType::OneWay() const
920{
921 return m_oneway;
922}
923
924void
925InterfaceType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
926{
927 // parcel.writeStrongBinder(v != null ? v.asBinder() : null);
928 addTo->Add(new MethodCall(parcel, "writeStrongBinder", 1,
929 new Ternary(
930 new Comparison(v, "!=", NULL_VALUE),
931 new MethodCall(v, "asBinder"),
932 NULL_VALUE)));
933}
934
935void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700936InterfaceType::CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700937{
938 // v = Interface.asInterface(parcel.readStrongBinder());
939 string type = v->type->QualifiedName();
940 type += ".Stub";
941 addTo->Add(new Assignment(v,
942 new MethodCall( NAMES.Find(type), "asInterface", 1,
943 new MethodCall(parcel, "readStrongBinder"))));
944}
945
946
947// ================================================================
948
949GenericType::GenericType(const string& package, const string& name,
950 const vector<Type*>& args)
Casey Dahlin88868fc2015-09-01 13:21:26 -0700951 :Type(package, name, BUILT_IN, true, true)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700952{
953 m_args = args;
954
955 m_importName = package + '.' + name;
956
957 string gen = "<";
958 int N = args.size();
959 for (int i=0; i<N; i++) {
960 Type* t = args[i];
961 gen += t->QualifiedName();
962 if (i != N-1) {
963 gen += ',';
964 }
965 }
966 gen += '>';
967 m_genericArguments = gen;
968 SetQualifiedName(m_importName + gen);
969}
970
Joe Onorato3d0e06f2011-09-02 15:28:36 -0700971const vector<Type*>&
972GenericType::GenericArgumentTypes() const
973{
974 return m_args;
975}
976
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700977string
978GenericType::GenericArguments() const
979{
980 return m_genericArguments;
981}
982
983string
984GenericType::ImportType() const
985{
986 return m_importName;
987}
988
989void
990GenericType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
991{
992 fprintf(stderr, "implement GenericType::WriteToParcel\n");
993}
994
995void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700996GenericType::CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700997{
998 fprintf(stderr, "implement GenericType::CreateFromParcel\n");
999}
1000
1001void
1002GenericType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Elliott Hughes15f8da22011-07-13 12:10:30 -07001003 Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -07001004{
1005 fprintf(stderr, "implement GenericType::ReadFromParcel\n");
1006}
1007
1008
1009// ================================================================
1010
1011GenericListType::GenericListType(const string& package, const string& name,
1012 const vector<Type*>& args)
1013 :GenericType(package, name, args),
1014 m_creator(args[0]->CreatorName())
1015{
1016}
1017
1018string
1019GenericListType::CreatorName() const
1020{
1021 return "android.os.Parcel.arrayListCreator";
1022}
1023
1024string
1025GenericListType::InstantiableName() const
1026{
1027 return "java.util.ArrayList" + GenericArguments();
1028}
1029
1030void
1031GenericListType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
1032{
1033 if (m_creator == STRING_TYPE->CreatorName()) {
1034 addTo->Add(new MethodCall(parcel, "writeStringList", 1, v));
1035 } else if (m_creator == IBINDER_TYPE->CreatorName()) {
1036 addTo->Add(new MethodCall(parcel, "writeBinderList", 1, v));
1037 } else {
1038 // parcel.writeTypedListXX(arg);
1039 addTo->Add(new MethodCall(parcel, "writeTypedList", 1, v));
1040 }
1041}
1042
1043void
Elliott Hughes15f8da22011-07-13 12:10:30 -07001044GenericListType::CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -07001045{
1046 if (m_creator == STRING_TYPE->CreatorName()) {
1047 addTo->Add(new Assignment(v,
1048 new MethodCall(parcel, "createStringArrayList", 0)));
1049 } else if (m_creator == IBINDER_TYPE->CreatorName()) {
1050 addTo->Add(new Assignment(v,
1051 new MethodCall(parcel, "createBinderArrayList", 0)));
1052 } else {
1053 // v = _data.readTypedArrayList(XXX.creator);
1054 addTo->Add(new Assignment(v,
1055 new MethodCall(parcel, "createTypedArrayList", 1,
1056 new LiteralExpression(m_creator))));
1057 }
1058}
1059
1060void
1061GenericListType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Elliott Hughes15f8da22011-07-13 12:10:30 -07001062 Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -07001063{
1064 if (m_creator == STRING_TYPE->CreatorName()) {
1065 addTo->Add(new MethodCall(parcel, "readStringList", 1, v));
1066 } else if (m_creator == IBINDER_TYPE->CreatorName()) {
1067 addTo->Add(new MethodCall(parcel, "readBinderList", 1, v));
1068 } else {
1069 // v = _data.readTypedList(v, XXX.creator);
1070 addTo->Add(new MethodCall(parcel, "readTypedList", 2,
1071 v,
1072 new LiteralExpression(m_creator)));
1073 }
1074}
1075
Joe Onorato1c5b7922011-09-06 11:01:17 -07001076
The Android Open Source Project46c012c2008-10-21 07:00:00 -07001077// ================================================================
1078
1079ClassLoaderType::ClassLoaderType()
Casey Dahlin88868fc2015-09-01 13:21:26 -07001080 :Type("java.lang", "ClassLoader", BUILT_IN, false, false)
The Android Open Source Project46c012c2008-10-21 07:00:00 -07001081{
1082}
1083
1084
1085// ================================================================
1086
1087Namespace::Namespace()
1088{
1089}
1090
1091Namespace::~Namespace()
1092{
1093 int N = m_types.size();
1094 for (int i=0; i<N; i++) {
1095 delete m_types[i];
1096 }
1097}
1098
1099void
1100Namespace::Add(Type* type)
1101{
1102 Type* t = Find(type->QualifiedName());
1103 if (t == NULL) {
1104 m_types.push_back(type);
1105 }
1106}
1107
1108void
1109Namespace::AddGenericType(const string& package, const string& name, int args)
1110{
1111 Generic g;
1112 g.package = package;
1113 g.name = name;
1114 g.qualified = package + '.' + name;
1115 g.args = args;
1116 m_generics.push_back(g);
1117}
1118
1119Type*
1120Namespace::Find(const string& name) const
1121{
1122 int N = m_types.size();
1123 for (int i=0; i<N; i++) {
1124 if (m_types[i]->QualifiedName() == name) {
1125 return m_types[i];
1126 }
1127 }
1128 return NULL;
1129}
1130
1131Type*
1132Namespace::Find(const char* package, const char* name) const
1133{
1134 string s;
1135 if (package != NULL) {
1136 s += package;
1137 s += '.';
1138 }
1139 s += name;
1140 return Find(s);
1141}
1142
1143static string
1144normalize_generic(const string& s)
1145{
1146 string r;
1147 int N = s.size();
1148 for (int i=0; i<N; i++) {
1149 char c = s[i];
1150 if (!isspace(c)) {
1151 r += c;
1152 }
1153 }
1154 return r;
1155}
1156
1157Type*
1158Namespace::Search(const string& name)
1159{
1160 // an exact match wins
1161 Type* result = Find(name);
1162 if (result != NULL) {
1163 return result;
1164 }
1165
1166 // try the class names
1167 // our language doesn't allow you to not specify outer classes
1168 // when referencing an inner class. that could be changed, and this
1169 // would be the place to do it, but I don't think the complexity in
1170 // scoping rules is worth it.
1171 int N = m_types.size();
Laurent Tu8b15e482013-02-15 16:07:33 -08001172 for (int i=0; i<N; i++) {
The Android Open Source Project46c012c2008-10-21 07:00:00 -07001173 if (m_types[i]->Name() == name) {
1174 return m_types[i];
1175 }
1176 }
1177
1178 // we got to here and it's not a generic, give up
1179 if (name.find('<') == name.npos) {
1180 return NULL;
1181 }
1182
1183 // remove any whitespace
1184 string normalized = normalize_generic(name);
1185
1186 // find the part before the '<', find a generic for it
1187 ssize_t baseIndex = normalized.find('<');
1188 string base(normalized.c_str(), baseIndex);
1189 const Generic* g = search_generic(base);
1190 if (g == NULL) {
1191 return NULL;
1192 }
1193
1194 // For each of the args, do a recursive search on it. We don't allow
1195 // generics within generics like Java does, because we're really limiting
1196 // them to just built-in container classes, at least for now. Our syntax
1197 // ensures this right now as well.
1198 vector<Type*> args;
1199 size_t start = baseIndex + 1;
1200 size_t end = start;
1201 while (normalized[start] != '\0') {
1202 end = normalized.find(',', start);
1203 if (end == normalized.npos) {
1204 end = normalized.find('>', start);
1205 }
1206 string s(normalized.c_str()+start, end-start);
1207 Type* t = this->Search(s);
1208 if (t == NULL) {
1209 // maybe we should print a warning here?
1210 return NULL;
1211 }
1212 args.push_back(t);
1213 start = end+1;
1214 }
1215
1216 // construct a GenericType, add it to our name set so they always get
1217 // the same object, and return it.
1218 result = make_generic_type(g->package, g->name, args);
1219 if (result == NULL) {
1220 return NULL;
1221 }
1222
1223 this->Add(result);
1224 return this->Find(result->QualifiedName());
1225}
1226
1227const Namespace::Generic*
1228Namespace::search_generic(const string& name) const
1229{
1230 int N = m_generics.size();
1231
1232 // first exact match
1233 for (int i=0; i<N; i++) {
1234 const Generic& g = m_generics[i];
1235 if (g.qualified == name) {
1236 return &g;
1237 }
1238 }
1239
1240 // then name match
1241 for (int i=0; i<N; i++) {
1242 const Generic& g = m_generics[i];
1243 if (g.name == name) {
1244 return &g;
1245 }
1246 }
1247
1248 return NULL;
1249}
1250
1251void
1252Namespace::Dump() const
1253{
1254 int n = m_types.size();
1255 for (int i=0; i<n; i++) {
1256 Type* t = m_types[i];
1257 printf("type: package=%s name=%s qualifiedName=%s\n",
1258 t->Package().c_str(), t->Name().c_str(),
1259 t->QualifiedName().c_str());
1260 }
1261}
Christopher Wileyfdeb0f42015-09-11 15:38:22 -07001262
1263} // namespace aidl
1264} // namespace android