blob: 3ce93fe16d3fdea7c7091b28efe5f3446faa3ba7 [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
184bool
185Type::CanBeArray() const
186{
187 return false;
188}
189
190string
191Type::ImportType() const
192{
193 return m_qualifiedName;
194}
195
196string
197Type::CreatorName() const
198{
199 return "";
200}
201
202string
203Type::InstantiableName() const
204{
205 return QualifiedName();
206}
207
208
209void
210Type::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
211{
212 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%sn",
213 __FILE__, __LINE__, m_qualifiedName.c_str());
214 addTo->Add(new LiteralExpression("/* WriteToParcel error "
215 + m_qualifiedName + " */"));
216}
217
218void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700219Type::CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700220{
221 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n",
222 __FILE__, __LINE__, m_qualifiedName.c_str());
223 addTo->Add(new LiteralExpression("/* CreateFromParcel error "
224 + m_qualifiedName + " */"));
225}
226
227void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700228Type::ReadFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700229{
230 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n",
231 __FILE__, __LINE__, m_qualifiedName.c_str());
232 addTo->Add(new LiteralExpression("/* ReadFromParcel error "
233 + m_qualifiedName + " */"));
234}
235
236void
237Type::WriteArrayToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
238{
239 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n",
240 __FILE__, __LINE__, m_qualifiedName.c_str());
241 addTo->Add(new LiteralExpression("/* WriteArrayToParcel error "
242 + m_qualifiedName + " */"));
243}
244
245void
246Type::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Elliott Hughes15f8da22011-07-13 12:10:30 -0700247 Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700248{
249 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n",
250 __FILE__, __LINE__, m_qualifiedName.c_str());
251 addTo->Add(new LiteralExpression("/* CreateArrayFromParcel error "
252 + m_qualifiedName + " */"));
253}
254
255void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700256Type::ReadArrayFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700257{
258 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n",
259 __FILE__, __LINE__, m_qualifiedName.c_str());
260 addTo->Add(new LiteralExpression("/* ReadArrayFromParcel error "
261 + m_qualifiedName + " */"));
262}
263
264void
265Type::SetQualifiedName(const string& qualified)
266{
267 m_qualifiedName = qualified;
268}
269
270Expression*
271Type::BuildWriteToParcelFlags(int flags)
272{
273 if (flags == 0) {
274 return new LiteralExpression("0");
275 }
276 if ((flags&PARCELABLE_WRITE_RETURN_VALUE) != 0) {
277 return new FieldVariable(PARCELABLE_INTERFACE_TYPE,
278 "PARCELABLE_WRITE_RETURN_VALUE");
279 }
280 return new LiteralExpression("0");
281}
282
283// ================================================================
284
Joe Onorato3d0e06f2011-09-02 15:28:36 -0700285BasicType::BasicType(const string& name, const string& marshallParcel,
286 const string& unmarshallParcel, const string& writeArrayParcel,
Casey Dahlin88868fc2015-09-01 13:21:26 -0700287 const string& createArrayParcel, const string& readArrayParcel)
288 :Type(name, BUILT_IN, true, false),
Joe Onorato3d0e06f2011-09-02 15:28:36 -0700289 m_marshallParcel(marshallParcel),
290 m_unmarshallParcel(unmarshallParcel),
291 m_writeArrayParcel(writeArrayParcel),
292 m_createArrayParcel(createArrayParcel),
Casey Dahlin88868fc2015-09-01 13:21:26 -0700293 m_readArrayParcel(readArrayParcel)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700294{
295}
296
297void
298BasicType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
299{
Joe Onorato3d0e06f2011-09-02 15:28:36 -0700300 addTo->Add(new MethodCall(parcel, m_marshallParcel, 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700301}
302
303void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700304BasicType::CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700305{
Joe Onorato3d0e06f2011-09-02 15:28:36 -0700306 addTo->Add(new Assignment(v, new MethodCall(parcel, m_unmarshallParcel)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700307}
308
309bool
310BasicType::CanBeArray() const
311{
312 return true;
313}
314
315void
316BasicType::WriteArrayToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
317{
Joe Onorato3d0e06f2011-09-02 15:28:36 -0700318 addTo->Add(new MethodCall(parcel, m_writeArrayParcel, 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700319}
320
321void
322BasicType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Elliott Hughes15f8da22011-07-13 12:10:30 -0700323 Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700324{
Joe Onorato3d0e06f2011-09-02 15:28:36 -0700325 addTo->Add(new Assignment(v, new MethodCall(parcel, m_createArrayParcel)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700326}
327
328void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700329BasicType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700330{
Joe Onorato3d0e06f2011-09-02 15:28:36 -0700331 addTo->Add(new MethodCall(parcel, m_readArrayParcel, 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700332}
333
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700334// ================================================================
335
336BooleanType::BooleanType()
Casey Dahlin88868fc2015-09-01 13:21:26 -0700337 :Type("boolean", BUILT_IN, true, false)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700338{
339}
340
341void
342BooleanType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
343{
344 addTo->Add(new MethodCall(parcel, "writeInt", 1,
345 new Ternary(v, new LiteralExpression("1"),
346 new LiteralExpression("0"))));
347}
348
349void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700350BooleanType::CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700351{
352 addTo->Add(new Assignment(v, new Comparison(new LiteralExpression("0"),
353 "!=", new MethodCall(parcel, "readInt"))));
354}
355
356bool
357BooleanType::CanBeArray() const
358{
359 return true;
360}
361
362void
363BooleanType::WriteArrayToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
364{
365 addTo->Add(new MethodCall(parcel, "writeBooleanArray", 1, v));
366}
367
368void
369BooleanType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Elliott Hughes15f8da22011-07-13 12:10:30 -0700370 Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700371{
372 addTo->Add(new Assignment(v, new MethodCall(parcel, "createBooleanArray")));
373}
374
375void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700376BooleanType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700377{
378 addTo->Add(new MethodCall(parcel, "readBooleanArray", 1, v));
379}
380
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700381// ================================================================
382
383CharType::CharType()
Casey Dahlin88868fc2015-09-01 13:21:26 -0700384 :Type("char", BUILT_IN, true, false)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700385{
386}
387
388void
389CharType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
390{
391 addTo->Add(new MethodCall(parcel, "writeInt", 1,
392 new Cast(INT_TYPE, v)));
393}
394
395void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700396CharType::CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700397{
398 addTo->Add(new Assignment(v, new MethodCall(parcel, "readInt"), this));
399}
400
401bool
402CharType::CanBeArray() const
403{
404 return true;
405}
406
407void
408CharType::WriteArrayToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
409{
410 addTo->Add(new MethodCall(parcel, "writeCharArray", 1, v));
411}
412
413void
414CharType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Elliott Hughes15f8da22011-07-13 12:10:30 -0700415 Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700416{
417 addTo->Add(new Assignment(v, new MethodCall(parcel, "createCharArray")));
418}
419
420void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700421CharType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700422{
423 addTo->Add(new MethodCall(parcel, "readCharArray", 1, v));
424}
425
426// ================================================================
427
428StringType::StringType()
Casey Dahlin88868fc2015-09-01 13:21:26 -0700429 :Type("java.lang", "String", BUILT_IN, true, false)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700430{
431}
432
433string
434StringType::CreatorName() const
435{
436 return "android.os.Parcel.STRING_CREATOR";
437}
438
439void
440StringType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
441{
442 addTo->Add(new MethodCall(parcel, "writeString", 1, v));
443}
444
445void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700446StringType::CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700447{
448 addTo->Add(new Assignment(v, new MethodCall(parcel, "readString")));
449}
450
451bool
452StringType::CanBeArray() const
453{
454 return true;
455}
456
457void
458StringType::WriteArrayToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
459{
460 addTo->Add(new MethodCall(parcel, "writeStringArray", 1, v));
461}
462
463void
464StringType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Elliott Hughes15f8da22011-07-13 12:10:30 -0700465 Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700466{
467 addTo->Add(new Assignment(v, new MethodCall(parcel, "createStringArray")));
468}
469
470void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700471StringType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700472{
473 addTo->Add(new MethodCall(parcel, "readStringArray", 1, v));
474}
475
476// ================================================================
477
478CharSequenceType::CharSequenceType()
Casey Dahlin88868fc2015-09-01 13:21:26 -0700479 :Type("java.lang", "CharSequence", BUILT_IN, true, false)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700480{
481}
482
483string
484CharSequenceType::CreatorName() const
485{
486 return "android.os.Parcel.STRING_CREATOR";
487}
488
489void
490CharSequenceType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
491{
492 // if (v != null) {
493 // parcel.writeInt(1);
494 // v.writeToParcel(parcel);
495 // } else {
496 // parcel.writeInt(0);
497 // }
498 IfStatement* elsepart = new IfStatement();
499 elsepart->statements->Add(new MethodCall(parcel, "writeInt", 1,
500 new LiteralExpression("0")));
501 IfStatement* ifpart = new IfStatement;
502 ifpart->expression = new Comparison(v, "!=", NULL_VALUE);
503 ifpart->elseif = elsepart;
504 ifpart->statements->Add(new MethodCall(parcel, "writeInt", 1,
505 new LiteralExpression("1")));
506 ifpart->statements->Add(new MethodCall(TEXT_UTILS_TYPE, "writeToParcel",
507 3, v, parcel, BuildWriteToParcelFlags(flags)));
508
509 addTo->Add(ifpart);
510}
511
512void
513CharSequenceType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Elliott Hughes15f8da22011-07-13 12:10:30 -0700514 Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700515{
516 // if (0 != parcel.readInt()) {
517 // v = TextUtils.createFromParcel(parcel)
518 // } else {
519 // v = null;
520 // }
521 IfStatement* elsepart = new IfStatement();
522 elsepart->statements->Add(new Assignment(v, NULL_VALUE));
523
524 IfStatement* ifpart = new IfStatement();
525 ifpart->expression = new Comparison(new LiteralExpression("0"), "!=",
526 new MethodCall(parcel, "readInt"));
527 ifpart->elseif = elsepart;
528 ifpart->statements->Add(new Assignment(v,
529 new MethodCall(TEXT_UTILS_TYPE,
530 "CHAR_SEQUENCE_CREATOR.createFromParcel", 1, parcel)));
531
532 addTo->Add(ifpart);
533}
534
535
536// ================================================================
537
538RemoteExceptionType::RemoteExceptionType()
Casey Dahlin88868fc2015-09-01 13:21:26 -0700539 :Type("android.os", "RemoteException", BUILT_IN, false, false)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700540{
541}
542
543void
544RemoteExceptionType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
545{
546 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
547}
548
549void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700550RemoteExceptionType::CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700551{
552 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
553}
554
555// ================================================================
556
557RuntimeExceptionType::RuntimeExceptionType()
Casey Dahlin88868fc2015-09-01 13:21:26 -0700558 :Type("java.lang", "RuntimeException", BUILT_IN, false, false)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700559{
560}
561
562void
563RuntimeExceptionType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
564{
565 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
566}
567
568void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700569RuntimeExceptionType::CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700570{
571 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
572}
573
574
575// ================================================================
576
577IBinderType::IBinderType()
Casey Dahlin88868fc2015-09-01 13:21:26 -0700578 :Type("android.os", "IBinder", BUILT_IN, true, false)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700579{
580}
581
582void
583IBinderType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
584{
585 addTo->Add(new MethodCall(parcel, "writeStrongBinder", 1, v));
586}
587
588void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700589IBinderType::CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700590{
591 addTo->Add(new Assignment(v, new MethodCall(parcel, "readStrongBinder")));
592}
593
594void
595IBinderType::WriteArrayToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
596{
597 addTo->Add(new MethodCall(parcel, "writeBinderArray", 1, v));
598}
599
600void
601IBinderType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Elliott Hughes15f8da22011-07-13 12:10:30 -0700602 Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700603{
604 addTo->Add(new Assignment(v, new MethodCall(parcel, "createBinderArray")));
605}
606
607void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700608IBinderType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700609{
610 addTo->Add(new MethodCall(parcel, "readBinderArray", 1, v));
611}
612
613
614// ================================================================
615
616IInterfaceType::IInterfaceType()
Casey Dahlin88868fc2015-09-01 13:21:26 -0700617 :Type("android.os", "IInterface", BUILT_IN, false, false)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700618{
619}
620
621void
622IInterfaceType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
623{
624 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
625}
626
627void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700628IInterfaceType::CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700629{
630 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
631}
632
633
634// ================================================================
635
636BinderType::BinderType()
Casey Dahlin88868fc2015-09-01 13:21:26 -0700637 :Type("android.os", "Binder", BUILT_IN, false, false)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700638{
639}
640
641void
642BinderType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
643{
644 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
645}
646
647void
648BinderType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Elliott Hughes15f8da22011-07-13 12:10:30 -0700649 Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700650{
651 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
652}
653
654
655// ================================================================
656
657BinderProxyType::BinderProxyType()
Casey Dahlin88868fc2015-09-01 13:21:26 -0700658 :Type("android.os", "BinderProxy", BUILT_IN, false, false)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700659{
660}
661
662void
663BinderProxyType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
664{
665 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
666}
667
668void
669BinderProxyType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Elliott Hughes15f8da22011-07-13 12:10:30 -0700670 Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700671{
672 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
673}
674
675
676// ================================================================
677
678ParcelType::ParcelType()
Casey Dahlin88868fc2015-09-01 13:21:26 -0700679 :Type("android.os", "Parcel", BUILT_IN, false, false)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700680{
681}
682
683void
684ParcelType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
685{
686 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
687}
688
689void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700690ParcelType::CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700691{
692 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
693}
694
695// ================================================================
696
697ParcelableInterfaceType::ParcelableInterfaceType()
Casey Dahlin88868fc2015-09-01 13:21:26 -0700698 :Type("android.os", "Parcelable", BUILT_IN, false, false)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700699{
700}
701
702void
703ParcelableInterfaceType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
704{
705 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
706}
707
708void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700709ParcelableInterfaceType::CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700710{
711 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
712}
713
714// ================================================================
715
716MapType::MapType()
Casey Dahlin88868fc2015-09-01 13:21:26 -0700717 :Type("java.util", "Map", BUILT_IN, true, true)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700718{
719}
720
721void
722MapType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
723{
724 addTo->Add(new MethodCall(parcel, "writeMap", 1, v));
725}
726
Elliott Hughes15f8da22011-07-13 12:10:30 -0700727static void EnsureClassLoader(StatementBlock* addTo, Variable** cl)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700728{
Elliott Hughes15f8da22011-07-13 12:10:30 -0700729 // We don't want to look up the class loader once for every
730 // collection argument, so ensure we do it at most once per method.
731 if (*cl == NULL) {
732 *cl = new Variable(CLASSLOADER_TYPE, "cl");
733 addTo->Add(new VariableDeclaration(*cl,
734 new LiteralExpression("this.getClass().getClassLoader()"),
735 CLASSLOADER_TYPE));
736 }
737}
738
739void
740MapType::CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable** cl)
741{
742 EnsureClassLoader(addTo, cl);
743 addTo->Add(new Assignment(v, new MethodCall(parcel, "readHashMap", 1, *cl)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700744}
745
746void
Joe Onorato3d0e06f2011-09-02 15:28:36 -0700747MapType::ReadFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable** cl)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700748{
Elliott Hughes15f8da22011-07-13 12:10:30 -0700749 EnsureClassLoader(addTo, cl);
750 addTo->Add(new MethodCall(parcel, "readMap", 2, v, *cl));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700751}
752
753
754// ================================================================
755
756ListType::ListType()
Casey Dahlin88868fc2015-09-01 13:21:26 -0700757 :Type("java.util", "List", BUILT_IN, true, true)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700758{
759}
760
761string
762ListType::InstantiableName() const
763{
764 return "java.util.ArrayList";
765}
766
767void
768ListType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
769{
770 addTo->Add(new MethodCall(parcel, "writeList", 1, v));
771}
772
773void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700774ListType::CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable** cl)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700775{
Elliott Hughes15f8da22011-07-13 12:10:30 -0700776 EnsureClassLoader(addTo, cl);
777 addTo->Add(new Assignment(v, new MethodCall(parcel, "readArrayList", 1, *cl)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700778}
779
780void
781ListType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Elliott Hughes15f8da22011-07-13 12:10:30 -0700782 Variable* parcel, Variable** cl)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700783{
Elliott Hughes15f8da22011-07-13 12:10:30 -0700784 EnsureClassLoader(addTo, cl);
785 addTo->Add(new MethodCall(parcel, "readList", 2, v, *cl));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700786}
787
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700788// ================================================================
789
Joe Onoratobaaf9c82011-10-09 22:31:16 -0700790UserDataType::UserDataType(const string& package, const string& name,
Casey Dahlin88868fc2015-09-01 13:21:26 -0700791 bool builtIn, bool canWriteToParcel,
Joe Onoratobaaf9c82011-10-09 22:31:16 -0700792 const string& declFile, int declLine)
Casey Dahlin88868fc2015-09-01 13:21:26 -0700793 :Type(package, name, builtIn ? BUILT_IN : USERDATA, canWriteToParcel,
Joe Onoratobaaf9c82011-10-09 22:31:16 -0700794 true, declFile, declLine)
Joe Onorato44050522011-10-09 17:38:20 -0700795{
796}
797
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700798string
Joe Onoratobaaf9c82011-10-09 22:31:16 -0700799UserDataType::CreatorName() const
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700800{
801 return QualifiedName() + ".CREATOR";
802}
803
804void
Joe Onoratobaaf9c82011-10-09 22:31:16 -0700805UserDataType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700806{
807 // if (v != null) {
808 // parcel.writeInt(1);
809 // v.writeToParcel(parcel);
810 // } else {
811 // parcel.writeInt(0);
812 // }
813 IfStatement* elsepart = new IfStatement();
814 elsepart->statements->Add(new MethodCall(parcel, "writeInt", 1,
815 new LiteralExpression("0")));
816 IfStatement* ifpart = new IfStatement;
817 ifpart->expression = new Comparison(v, "!=", NULL_VALUE);
818 ifpart->elseif = elsepart;
819 ifpart->statements->Add(new MethodCall(parcel, "writeInt", 1,
820 new LiteralExpression("1")));
821 ifpart->statements->Add(new MethodCall(v, "writeToParcel", 2,
822 parcel, BuildWriteToParcelFlags(flags)));
823
824 addTo->Add(ifpart);
825}
826
827void
Joe Onoratobaaf9c82011-10-09 22:31:16 -0700828UserDataType::CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700829{
830 // if (0 != parcel.readInt()) {
831 // v = CLASS.CREATOR.createFromParcel(parcel)
832 // } else {
833 // v = null;
834 // }
835 IfStatement* elsepart = new IfStatement();
836 elsepart->statements->Add(new Assignment(v, NULL_VALUE));
837
838 IfStatement* ifpart = new IfStatement();
839 ifpart->expression = new Comparison(new LiteralExpression("0"), "!=",
840 new MethodCall(parcel, "readInt"));
841 ifpart->elseif = elsepart;
842 ifpart->statements->Add(new Assignment(v,
843 new MethodCall(v->type, "CREATOR.createFromParcel", 1, parcel)));
844
845 addTo->Add(ifpart);
846}
847
848void
Joe Onoratobaaf9c82011-10-09 22:31:16 -0700849UserDataType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Elliott Hughes15f8da22011-07-13 12:10:30 -0700850 Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700851{
852 // TODO: really, we don't need to have this extra check, but we
853 // don't have two separate marshalling code paths
854 // if (0 != parcel.readInt()) {
855 // v.readFromParcel(parcel)
856 // }
857 IfStatement* ifpart = new IfStatement();
858 ifpart->expression = new Comparison(new LiteralExpression("0"), "!=",
859 new MethodCall(parcel, "readInt"));
860 ifpart->statements->Add(new MethodCall(v, "readFromParcel", 1, parcel));
861 addTo->Add(ifpart);
862}
863
864bool
Joe Onoratobaaf9c82011-10-09 22:31:16 -0700865UserDataType::CanBeArray() const
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700866{
867 return true;
868}
869
870void
Joe Onoratobaaf9c82011-10-09 22:31:16 -0700871UserDataType::WriteArrayToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700872{
873 addTo->Add(new MethodCall(parcel, "writeTypedArray", 2, v,
874 BuildWriteToParcelFlags(flags)));
875}
876
877void
Joe Onoratobaaf9c82011-10-09 22:31:16 -0700878UserDataType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Elliott Hughes15f8da22011-07-13 12:10:30 -0700879 Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700880{
881 string creator = v->type->QualifiedName() + ".CREATOR";
882 addTo->Add(new Assignment(v, new MethodCall(parcel,
883 "createTypedArray", 1, new LiteralExpression(creator))));
884}
885
886void
Joe Onoratobaaf9c82011-10-09 22:31:16 -0700887UserDataType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700888{
889 string creator = v->type->QualifiedName() + ".CREATOR";
890 addTo->Add(new MethodCall(parcel, "readTypedArray", 2,
891 v, new LiteralExpression(creator)));
892}
893
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700894// ================================================================
895
896InterfaceType::InterfaceType(const string& package, const string& name,
897 bool builtIn, bool oneway,
898 const string& declFile, int declLine)
Casey Dahlin88868fc2015-09-01 13:21:26 -0700899 :Type(package, name, builtIn ? BUILT_IN : INTERFACE, true, false,
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700900 declFile, declLine)
901 ,m_oneway(oneway)
902{
903}
904
905bool
906InterfaceType::OneWay() const
907{
908 return m_oneway;
909}
910
911void
912InterfaceType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
913{
914 // parcel.writeStrongBinder(v != null ? v.asBinder() : null);
915 addTo->Add(new MethodCall(parcel, "writeStrongBinder", 1,
916 new Ternary(
917 new Comparison(v, "!=", NULL_VALUE),
918 new MethodCall(v, "asBinder"),
919 NULL_VALUE)));
920}
921
922void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700923InterfaceType::CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700924{
925 // v = Interface.asInterface(parcel.readStrongBinder());
926 string type = v->type->QualifiedName();
927 type += ".Stub";
928 addTo->Add(new Assignment(v,
929 new MethodCall( NAMES.Find(type), "asInterface", 1,
930 new MethodCall(parcel, "readStrongBinder"))));
931}
932
933
934// ================================================================
935
936GenericType::GenericType(const string& package, const string& name,
937 const vector<Type*>& args)
Casey Dahlin88868fc2015-09-01 13:21:26 -0700938 :Type(package, name, BUILT_IN, true, true)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700939{
940 m_args = args;
941
942 m_importName = package + '.' + name;
943
944 string gen = "<";
945 int N = args.size();
946 for (int i=0; i<N; i++) {
947 Type* t = args[i];
948 gen += t->QualifiedName();
949 if (i != N-1) {
950 gen += ',';
951 }
952 }
953 gen += '>';
954 m_genericArguments = gen;
955 SetQualifiedName(m_importName + gen);
956}
957
Joe Onorato3d0e06f2011-09-02 15:28:36 -0700958const vector<Type*>&
959GenericType::GenericArgumentTypes() const
960{
961 return m_args;
962}
963
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700964string
965GenericType::GenericArguments() const
966{
967 return m_genericArguments;
968}
969
970string
971GenericType::ImportType() const
972{
973 return m_importName;
974}
975
976void
977GenericType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
978{
979 fprintf(stderr, "implement GenericType::WriteToParcel\n");
980}
981
982void
Elliott Hughes15f8da22011-07-13 12:10:30 -0700983GenericType::CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700984{
985 fprintf(stderr, "implement GenericType::CreateFromParcel\n");
986}
987
988void
989GenericType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Elliott Hughes15f8da22011-07-13 12:10:30 -0700990 Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700991{
992 fprintf(stderr, "implement GenericType::ReadFromParcel\n");
993}
994
995
996// ================================================================
997
998GenericListType::GenericListType(const string& package, const string& name,
999 const vector<Type*>& args)
1000 :GenericType(package, name, args),
1001 m_creator(args[0]->CreatorName())
1002{
1003}
1004
1005string
1006GenericListType::CreatorName() const
1007{
1008 return "android.os.Parcel.arrayListCreator";
1009}
1010
1011string
1012GenericListType::InstantiableName() const
1013{
1014 return "java.util.ArrayList" + GenericArguments();
1015}
1016
1017void
1018GenericListType::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel, int flags)
1019{
1020 if (m_creator == STRING_TYPE->CreatorName()) {
1021 addTo->Add(new MethodCall(parcel, "writeStringList", 1, v));
1022 } else if (m_creator == IBINDER_TYPE->CreatorName()) {
1023 addTo->Add(new MethodCall(parcel, "writeBinderList", 1, v));
1024 } else {
1025 // parcel.writeTypedListXX(arg);
1026 addTo->Add(new MethodCall(parcel, "writeTypedList", 1, v));
1027 }
1028}
1029
1030void
Elliott Hughes15f8da22011-07-13 12:10:30 -07001031GenericListType::CreateFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -07001032{
1033 if (m_creator == STRING_TYPE->CreatorName()) {
1034 addTo->Add(new Assignment(v,
1035 new MethodCall(parcel, "createStringArrayList", 0)));
1036 } else if (m_creator == IBINDER_TYPE->CreatorName()) {
1037 addTo->Add(new Assignment(v,
1038 new MethodCall(parcel, "createBinderArrayList", 0)));
1039 } else {
1040 // v = _data.readTypedArrayList(XXX.creator);
1041 addTo->Add(new Assignment(v,
1042 new MethodCall(parcel, "createTypedArrayList", 1,
1043 new LiteralExpression(m_creator))));
1044 }
1045}
1046
1047void
1048GenericListType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Elliott Hughes15f8da22011-07-13 12:10:30 -07001049 Variable* parcel, Variable**)
The Android Open Source Project46c012c2008-10-21 07:00:00 -07001050{
1051 if (m_creator == STRING_TYPE->CreatorName()) {
1052 addTo->Add(new MethodCall(parcel, "readStringList", 1, v));
1053 } else if (m_creator == IBINDER_TYPE->CreatorName()) {
1054 addTo->Add(new MethodCall(parcel, "readBinderList", 1, v));
1055 } else {
1056 // v = _data.readTypedList(v, XXX.creator);
1057 addTo->Add(new MethodCall(parcel, "readTypedList", 2,
1058 v,
1059 new LiteralExpression(m_creator)));
1060 }
1061}
1062
Joe Onorato1c5b7922011-09-06 11:01:17 -07001063
The Android Open Source Project46c012c2008-10-21 07:00:00 -07001064// ================================================================
1065
1066ClassLoaderType::ClassLoaderType()
Casey Dahlin88868fc2015-09-01 13:21:26 -07001067 :Type("java.lang", "ClassLoader", BUILT_IN, false, false)
The Android Open Source Project46c012c2008-10-21 07:00:00 -07001068{
1069}
1070
1071
1072// ================================================================
1073
1074Namespace::Namespace()
1075{
1076}
1077
1078Namespace::~Namespace()
1079{
1080 int N = m_types.size();
1081 for (int i=0; i<N; i++) {
1082 delete m_types[i];
1083 }
1084}
1085
1086void
1087Namespace::Add(Type* type)
1088{
1089 Type* t = Find(type->QualifiedName());
1090 if (t == NULL) {
1091 m_types.push_back(type);
1092 }
1093}
1094
1095void
1096Namespace::AddGenericType(const string& package, const string& name, int args)
1097{
1098 Generic g;
1099 g.package = package;
1100 g.name = name;
1101 g.qualified = package + '.' + name;
1102 g.args = args;
1103 m_generics.push_back(g);
1104}
1105
1106Type*
1107Namespace::Find(const string& name) const
1108{
1109 int N = m_types.size();
1110 for (int i=0; i<N; i++) {
1111 if (m_types[i]->QualifiedName() == name) {
1112 return m_types[i];
1113 }
1114 }
1115 return NULL;
1116}
1117
1118Type*
1119Namespace::Find(const char* package, const char* name) const
1120{
1121 string s;
1122 if (package != NULL) {
1123 s += package;
1124 s += '.';
1125 }
1126 s += name;
1127 return Find(s);
1128}
1129
1130static string
1131normalize_generic(const string& s)
1132{
1133 string r;
1134 int N = s.size();
1135 for (int i=0; i<N; i++) {
1136 char c = s[i];
1137 if (!isspace(c)) {
1138 r += c;
1139 }
1140 }
1141 return r;
1142}
1143
1144Type*
1145Namespace::Search(const string& name)
1146{
1147 // an exact match wins
1148 Type* result = Find(name);
1149 if (result != NULL) {
1150 return result;
1151 }
1152
1153 // try the class names
1154 // our language doesn't allow you to not specify outer classes
1155 // when referencing an inner class. that could be changed, and this
1156 // would be the place to do it, but I don't think the complexity in
1157 // scoping rules is worth it.
1158 int N = m_types.size();
Laurent Tu8b15e482013-02-15 16:07:33 -08001159 for (int i=0; i<N; i++) {
The Android Open Source Project46c012c2008-10-21 07:00:00 -07001160 if (m_types[i]->Name() == name) {
1161 return m_types[i];
1162 }
1163 }
1164
1165 // we got to here and it's not a generic, give up
1166 if (name.find('<') == name.npos) {
1167 return NULL;
1168 }
1169
1170 // remove any whitespace
1171 string normalized = normalize_generic(name);
1172
1173 // find the part before the '<', find a generic for it
1174 ssize_t baseIndex = normalized.find('<');
1175 string base(normalized.c_str(), baseIndex);
1176 const Generic* g = search_generic(base);
1177 if (g == NULL) {
1178 return NULL;
1179 }
1180
1181 // For each of the args, do a recursive search on it. We don't allow
1182 // generics within generics like Java does, because we're really limiting
1183 // them to just built-in container classes, at least for now. Our syntax
1184 // ensures this right now as well.
1185 vector<Type*> args;
1186 size_t start = baseIndex + 1;
1187 size_t end = start;
1188 while (normalized[start] != '\0') {
1189 end = normalized.find(',', start);
1190 if (end == normalized.npos) {
1191 end = normalized.find('>', start);
1192 }
1193 string s(normalized.c_str()+start, end-start);
1194 Type* t = this->Search(s);
1195 if (t == NULL) {
1196 // maybe we should print a warning here?
1197 return NULL;
1198 }
1199 args.push_back(t);
1200 start = end+1;
1201 }
1202
1203 // construct a GenericType, add it to our name set so they always get
1204 // the same object, and return it.
1205 result = make_generic_type(g->package, g->name, args);
1206 if (result == NULL) {
1207 return NULL;
1208 }
1209
1210 this->Add(result);
1211 return this->Find(result->QualifiedName());
1212}
1213
1214const Namespace::Generic*
1215Namespace::search_generic(const string& name) const
1216{
1217 int N = m_generics.size();
1218
1219 // first exact match
1220 for (int i=0; i<N; i++) {
1221 const Generic& g = m_generics[i];
1222 if (g.qualified == name) {
1223 return &g;
1224 }
1225 }
1226
1227 // then name match
1228 for (int i=0; i<N; i++) {
1229 const Generic& g = m_generics[i];
1230 if (g.name == name) {
1231 return &g;
1232 }
1233 }
1234
1235 return NULL;
1236}
1237
1238void
1239Namespace::Dump() const
1240{
1241 int n = m_types.size();
1242 for (int i=0; i<n; i++) {
1243 Type* t = m_types[i];
1244 printf("type: package=%s name=%s qualifiedName=%s\n",
1245 t->Package().c_str(), t->Name().c_str(),
1246 t->QualifiedName().c_str());
1247 }
1248}
Christopher Wileyfdeb0f42015-09-11 15:38:22 -07001249
1250} // namespace aidl
1251} // namespace android