blob: 0d0129fc658e359c1cd87b14065b5e93dc11ad71 [file] [log] [blame]
Christopher Wiley775fa1f2015-09-22 15:00:12 -07001/*
2 * Copyright (C) 2015, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "type_java.h"
The Android Open Source Project46c012c2008-10-21 07:00:00 -070018
David 'Digit' Turnera3372982014-03-04 16:43:41 +010019#include <sys/types.h>
20
Christopher Wiley4582ecb2015-09-25 13:27:45 -070021#include <base/strings.h>
22
Christopher Wiley84c1eac2015-09-23 13:29:28 -070023#include "aidl_language.h"
24#include "logging.h"
25
Christopher Wiley4582ecb2015-09-25 13:27:45 -070026using android::base::Split;
27using android::base::Join;
28using android::base::Trim;
29
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070030namespace android {
31namespace aidl {
32
The Android Open Source Project46c012c2008-10-21 07:00:00 -070033Expression* NULL_VALUE;
34Expression* THIS_VALUE;
35Expression* SUPER_VALUE;
36Expression* TRUE_VALUE;
37Expression* FALSE_VALUE;
38
The Android Open Source Project46c012c2008-10-21 07:00:00 -070039// ================================================================
40
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -070041Type::Type(const JavaTypeNamespace* types, const string& name, int kind,
42 bool canWriteToParcel, bool canBeOut)
43 : m_types(types),
44 m_package(),
Christopher Wileye6dee912015-09-22 14:50:23 -070045 m_name(name),
46 m_declFile(""),
47 m_declLine(-1),
48 m_kind(kind),
49 m_canWriteToParcel(canWriteToParcel),
50 m_canBeOut(canBeOut) {
51 m_qualifiedName = name;
The Android Open Source Project46c012c2008-10-21 07:00:00 -070052}
53
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -070054Type::Type(const JavaTypeNamespace* types, const string& package,
55 const string& name, int kind, bool canWriteToParcel, bool canBeOut,
56 const string& declFile, int declLine)
57 : m_types(types),
58 m_package(package),
Christopher Wileye6dee912015-09-22 14:50:23 -070059 m_name(name),
60 m_declFile(declFile),
61 m_declLine(declLine),
62 m_kind(kind),
63 m_canWriteToParcel(canWriteToParcel),
64 m_canBeOut(canBeOut) {
65 if (package.length() > 0) {
66 m_qualifiedName = package;
67 m_qualifiedName += '.';
68 }
69 m_qualifiedName += name;
The Android Open Source Project46c012c2008-10-21 07:00:00 -070070}
71
Christopher Wileye6dee912015-09-22 14:50:23 -070072Type::~Type() {}
73
74string Type::HumanReadableKind() const {
75 switch (Kind()) {
76 case INTERFACE:
77 return "an interface";
78 case USERDATA:
79 return "a user data";
80 default:
81 return "ERROR";
82 }
The Android Open Source Project46c012c2008-10-21 07:00:00 -070083}
84
Christopher Wileye6dee912015-09-22 14:50:23 -070085bool Type::CanBeArray() const { return false; }
86
87string Type::ImportType() const { return m_qualifiedName; }
88
89string Type::CreatorName() const { return ""; }
90
91string Type::InstantiableName() const { return QualifiedName(); }
92
93void Type::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel,
Christopher Wiley3637a4d2015-09-22 15:37:59 -070094 int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -070095 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%sn", __FILE__,
96 __LINE__, m_qualifiedName.c_str());
97 addTo->Add(new LiteralExpression("/* WriteToParcel error " + m_qualifiedName +
98 " */"));
Christopher Wileyf690be52015-09-14 15:19:10 -070099}
100
Christopher Wileye6dee912015-09-22 14:50:23 -0700101void Type::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700102 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700103 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n", __FILE__,
104 __LINE__, m_qualifiedName.c_str());
105 addTo->Add(new LiteralExpression("/* CreateFromParcel error " +
106 m_qualifiedName + " */"));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700107}
108
Christopher Wileye6dee912015-09-22 14:50:23 -0700109void Type::ReadFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700110 Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700111 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n", __FILE__,
112 __LINE__, m_qualifiedName.c_str());
113 addTo->Add(new LiteralExpression("/* ReadFromParcel error " +
114 m_qualifiedName + " */"));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700115}
116
Christopher Wileye6dee912015-09-22 14:50:23 -0700117void Type::WriteArrayToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700118 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700119 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n", __FILE__,
120 __LINE__, m_qualifiedName.c_str());
121 addTo->Add(new LiteralExpression("/* WriteArrayToParcel error " +
122 m_qualifiedName + " */"));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700123}
124
Christopher Wileye6dee912015-09-22 14:50:23 -0700125void Type::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700126 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700127 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n", __FILE__,
128 __LINE__, m_qualifiedName.c_str());
129 addTo->Add(new LiteralExpression("/* CreateArrayFromParcel error " +
130 m_qualifiedName + " */"));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700131}
132
Christopher Wileye6dee912015-09-22 14:50:23 -0700133void Type::ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700134 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700135 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n", __FILE__,
136 __LINE__, m_qualifiedName.c_str());
137 addTo->Add(new LiteralExpression("/* ReadArrayFromParcel error " +
138 m_qualifiedName + " */"));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700139}
140
Christopher Wileye6dee912015-09-22 14:50:23 -0700141void Type::SetQualifiedName(const string& qualified) {
142 m_qualifiedName = qualified;
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700143}
144
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700145Expression* Type::BuildWriteToParcelFlags(int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700146 if (flags == 0) {
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700147 return new LiteralExpression("0");
Christopher Wileye6dee912015-09-22 14:50:23 -0700148 }
149 if ((flags & PARCELABLE_WRITE_RETURN_VALUE) != 0) {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700150 return new FieldVariable(m_types->ParcelableInterfaceType(),
Christopher Wileye6dee912015-09-22 14:50:23 -0700151 "PARCELABLE_WRITE_RETURN_VALUE");
152 }
153 return new LiteralExpression("0");
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700154}
155
156// ================================================================
157
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700158BasicType::BasicType(const JavaTypeNamespace* types, const string& name,
159 const string& marshallParcel,
Christopher Wileye6dee912015-09-22 14:50:23 -0700160 const string& unmarshallParcel,
161 const string& writeArrayParcel,
162 const string& createArrayParcel,
163 const string& readArrayParcel)
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700164 : Type(types, name, BUILT_IN, true, false),
Christopher Wileye6dee912015-09-22 14:50:23 -0700165 m_marshallParcel(marshallParcel),
166 m_unmarshallParcel(unmarshallParcel),
167 m_writeArrayParcel(writeArrayParcel),
168 m_createArrayParcel(createArrayParcel),
169 m_readArrayParcel(readArrayParcel) {}
170
171void BasicType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700172 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700173 addTo->Add(new MethodCall(parcel, m_marshallParcel, 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700174}
175
Christopher Wileye6dee912015-09-22 14:50:23 -0700176void BasicType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700177 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700178 addTo->Add(new Assignment(v, new MethodCall(parcel, m_unmarshallParcel)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700179}
180
Christopher Wileye6dee912015-09-22 14:50:23 -0700181bool BasicType::CanBeArray() const { return true; }
182
183void BasicType::WriteArrayToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700184 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700185 addTo->Add(new MethodCall(parcel, m_writeArrayParcel, 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700186}
187
Christopher Wileye6dee912015-09-22 14:50:23 -0700188void BasicType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700189 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700190 addTo->Add(new Assignment(v, new MethodCall(parcel, m_createArrayParcel)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700191}
192
Christopher Wileye6dee912015-09-22 14:50:23 -0700193void BasicType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700194 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700195 addTo->Add(new MethodCall(parcel, m_readArrayParcel, 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700196}
197
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700198// ================================================================
199
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700200BooleanType::BooleanType(const JavaTypeNamespace* types)
201 : Type(types, "boolean", BUILT_IN, true, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700202
203void BooleanType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700204 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700205 addTo->Add(new MethodCall(
206 parcel, "writeInt", 1,
207 new Ternary(v, new LiteralExpression("1"), new LiteralExpression("0"))));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700208}
209
Christopher Wileye6dee912015-09-22 14:50:23 -0700210void BooleanType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700211 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700212 addTo->Add(
213 new Assignment(v, new Comparison(new LiteralExpression("0"), "!=",
214 new MethodCall(parcel, "readInt"))));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700215}
216
Christopher Wileye6dee912015-09-22 14:50:23 -0700217bool BooleanType::CanBeArray() const { return true; }
218
219void BooleanType::WriteArrayToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700220 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700221 addTo->Add(new MethodCall(parcel, "writeBooleanArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700222}
223
Christopher Wileye6dee912015-09-22 14:50:23 -0700224void BooleanType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700225 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700226 addTo->Add(new Assignment(v, new MethodCall(parcel, "createBooleanArray")));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700227}
228
Christopher Wileye6dee912015-09-22 14:50:23 -0700229void BooleanType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700230 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700231 addTo->Add(new MethodCall(parcel, "readBooleanArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700232}
233
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700234// ================================================================
235
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700236CharType::CharType(const JavaTypeNamespace* types)
237 : Type(types, "char", BUILT_IN, true, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700238
239void CharType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700240 Variable* parcel, int flags) const {
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700241 addTo->Add(
242 new MethodCall(parcel, "writeInt", 1, new Cast(m_types->IntType(), v)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700243}
244
Christopher Wileye6dee912015-09-22 14:50:23 -0700245void CharType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700246 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700247 addTo->Add(new Assignment(v, new MethodCall(parcel, "readInt"), this));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700248}
249
Christopher Wileye6dee912015-09-22 14:50:23 -0700250bool CharType::CanBeArray() const { return true; }
251
252void CharType::WriteArrayToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700253 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700254 addTo->Add(new MethodCall(parcel, "writeCharArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700255}
256
Christopher Wileye6dee912015-09-22 14:50:23 -0700257void CharType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700258 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700259 addTo->Add(new Assignment(v, new MethodCall(parcel, "createCharArray")));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700260}
261
Christopher Wileye6dee912015-09-22 14:50:23 -0700262void CharType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700263 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700264 addTo->Add(new MethodCall(parcel, "readCharArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700265}
266
267// ================================================================
268
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700269StringType::StringType(const JavaTypeNamespace* types)
270 : Type(types, "java.lang", "String", BUILT_IN, true, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700271
272string StringType::CreatorName() const {
273 return "android.os.Parcel.STRING_CREATOR";
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700274}
275
Christopher Wileye6dee912015-09-22 14:50:23 -0700276void StringType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700277 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700278 addTo->Add(new MethodCall(parcel, "writeString", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700279}
280
Christopher Wileye6dee912015-09-22 14:50:23 -0700281void StringType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700282 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700283 addTo->Add(new Assignment(v, new MethodCall(parcel, "readString")));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700284}
285
Christopher Wileye6dee912015-09-22 14:50:23 -0700286bool StringType::CanBeArray() const { return true; }
287
288void StringType::WriteArrayToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700289 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700290 addTo->Add(new MethodCall(parcel, "writeStringArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700291}
292
Christopher Wileye6dee912015-09-22 14:50:23 -0700293void StringType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700294 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700295 addTo->Add(new Assignment(v, new MethodCall(parcel, "createStringArray")));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700296}
297
Christopher Wileye6dee912015-09-22 14:50:23 -0700298void StringType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700299 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700300 addTo->Add(new MethodCall(parcel, "readStringArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700301}
302
303// ================================================================
304
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700305CharSequenceType::CharSequenceType(const JavaTypeNamespace* types)
306 : Type(types, "java.lang", "CharSequence", BUILT_IN, true, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700307
308string CharSequenceType::CreatorName() const {
309 return "android.os.Parcel.STRING_CREATOR";
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700310}
311
Christopher Wileye6dee912015-09-22 14:50:23 -0700312void CharSequenceType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700313 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700314 // if (v != null) {
315 // parcel.writeInt(1);
316 // v.writeToParcel(parcel);
317 // } else {
318 // parcel.writeInt(0);
319 // }
320 IfStatement* elsepart = new IfStatement();
321 elsepart->statements->Add(
322 new MethodCall(parcel, "writeInt", 1, new LiteralExpression("0")));
323 IfStatement* ifpart = new IfStatement;
324 ifpart->expression = new Comparison(v, "!=", NULL_VALUE);
325 ifpart->elseif = elsepart;
326 ifpart->statements->Add(
327 new MethodCall(parcel, "writeInt", 1, new LiteralExpression("1")));
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700328 ifpart->statements->Add(new MethodCall(m_types->TextUtilsType(),
329 "writeToParcel", 3, v, parcel,
Christopher Wileye6dee912015-09-22 14:50:23 -0700330 BuildWriteToParcelFlags(flags)));
331
332 addTo->Add(ifpart);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700333}
334
Christopher Wileye6dee912015-09-22 14:50:23 -0700335void CharSequenceType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700336 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700337 // if (0 != parcel.readInt()) {
338 // v = TextUtils.createFromParcel(parcel)
339 // } else {
340 // v = null;
341 // }
342 IfStatement* elsepart = new IfStatement();
343 elsepart->statements->Add(new Assignment(v, NULL_VALUE));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700344
Christopher Wileye6dee912015-09-22 14:50:23 -0700345 IfStatement* ifpart = new IfStatement();
346 ifpart->expression = new Comparison(new LiteralExpression("0"), "!=",
347 new MethodCall(parcel, "readInt"));
348 ifpart->elseif = elsepart;
349 ifpart->statements->Add(new Assignment(
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700350 v, new MethodCall(m_types->TextUtilsType(),
Christopher Wileye6dee912015-09-22 14:50:23 -0700351 "CHAR_SEQUENCE_CREATOR.createFromParcel", 1, parcel)));
352
353 addTo->Add(ifpart);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700354}
355
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700356// ================================================================
357
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700358RemoteExceptionType::RemoteExceptionType(const JavaTypeNamespace* types)
359 : Type(types, "android.os", "RemoteException", BUILT_IN, false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700360
361void RemoteExceptionType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700362 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700363 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700364}
365
Christopher Wileye6dee912015-09-22 14:50:23 -0700366void RemoteExceptionType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700367 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700368 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700369}
370
371// ================================================================
372
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700373RuntimeExceptionType::RuntimeExceptionType(const JavaTypeNamespace* types)
374 : Type(types, "java.lang", "RuntimeException", BUILT_IN, false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700375
376void RuntimeExceptionType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700377 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700378 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700379}
380
Christopher Wileye6dee912015-09-22 14:50:23 -0700381void RuntimeExceptionType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700382 Variable* parcel,
383 Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700384 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700385}
386
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700387// ================================================================
388
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700389IBinderType::IBinderType(const JavaTypeNamespace* types)
390 : Type(types, "android.os", "IBinder", BUILT_IN, true, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700391
392void IBinderType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700393 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700394 addTo->Add(new MethodCall(parcel, "writeStrongBinder", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700395}
396
Christopher Wileye6dee912015-09-22 14:50:23 -0700397void IBinderType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700398 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700399 addTo->Add(new Assignment(v, new MethodCall(parcel, "readStrongBinder")));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700400}
401
Christopher Wileye6dee912015-09-22 14:50:23 -0700402void IBinderType::WriteArrayToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700403 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700404 addTo->Add(new MethodCall(parcel, "writeBinderArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700405}
406
Christopher Wileye6dee912015-09-22 14:50:23 -0700407void IBinderType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700408 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700409 addTo->Add(new Assignment(v, new MethodCall(parcel, "createBinderArray")));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700410}
411
Christopher Wileye6dee912015-09-22 14:50:23 -0700412void IBinderType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700413 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700414 addTo->Add(new MethodCall(parcel, "readBinderArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700415}
416
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700417// ================================================================
418
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700419IInterfaceType::IInterfaceType(const JavaTypeNamespace* types)
420 : Type(types, "android.os", "IInterface", BUILT_IN, false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700421
422void IInterfaceType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700423 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700424 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700425}
426
Christopher Wileye6dee912015-09-22 14:50:23 -0700427void IInterfaceType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700428 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700429 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700430}
431
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700432// ================================================================
433
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700434BinderType::BinderType(const JavaTypeNamespace* types)
435 : Type(types, "android.os", "Binder", BUILT_IN, false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700436
437void BinderType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700438 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700439 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700440}
441
Christopher Wileye6dee912015-09-22 14:50:23 -0700442void BinderType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700443 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700444 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700445}
446
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700447// ================================================================
448
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700449BinderProxyType::BinderProxyType(const JavaTypeNamespace* types)
450 : Type(types, "android.os", "BinderProxy", BUILT_IN, false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700451
452void BinderProxyType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700453 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700454 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700455}
456
Christopher Wileye6dee912015-09-22 14:50:23 -0700457void BinderProxyType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700458 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700459 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700460}
461
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700462// ================================================================
463
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700464ParcelType::ParcelType(const JavaTypeNamespace* types)
465 : Type(types, "android.os", "Parcel", BUILT_IN, false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700466
467void ParcelType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700468 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700469 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700470}
471
Christopher Wileye6dee912015-09-22 14:50:23 -0700472void ParcelType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700473 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700474 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700475}
476
477// ================================================================
478
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700479ParcelableInterfaceType::ParcelableInterfaceType(const JavaTypeNamespace* types)
480 : Type(types, "android.os", "Parcelable", BUILT_IN, false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700481
482void ParcelableInterfaceType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700483 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700484 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700485}
486
Christopher Wileye6dee912015-09-22 14:50:23 -0700487void ParcelableInterfaceType::CreateFromParcel(StatementBlock* addTo,
488 Variable* v, Variable* parcel,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700489 Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700490 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700491}
492
493// ================================================================
494
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700495MapType::MapType(const JavaTypeNamespace* types)
496 : Type(types, "java.util", "Map", BUILT_IN, true, true) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700497
498void MapType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700499 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700500 addTo->Add(new MethodCall(parcel, "writeMap", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700501}
502
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700503static void EnsureClassLoader(StatementBlock* addTo, Variable** cl,
504 const JavaTypeNamespace* types) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700505 // We don't want to look up the class loader once for every
506 // collection argument, so ensure we do it at most once per method.
507 if (*cl == NULL) {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700508 *cl = new Variable(types->ClassLoaderType(), "cl");
Christopher Wileye6dee912015-09-22 14:50:23 -0700509 addTo->Add(new VariableDeclaration(
510 *cl, new LiteralExpression("this.getClass().getClassLoader()"),
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700511 types->ClassLoaderType()));
Christopher Wileye6dee912015-09-22 14:50:23 -0700512 }
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700513}
514
Christopher Wileye6dee912015-09-22 14:50:23 -0700515void MapType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700516 Variable* parcel, Variable** cl) const {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700517 EnsureClassLoader(addTo, cl, m_types);
Christopher Wileye6dee912015-09-22 14:50:23 -0700518 addTo->Add(new Assignment(v, new MethodCall(parcel, "readHashMap", 1, *cl)));
Elliott Hughes15f8da22011-07-13 12:10:30 -0700519}
520
Christopher Wileye6dee912015-09-22 14:50:23 -0700521void MapType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700522 Variable* parcel, Variable** cl) const {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700523 EnsureClassLoader(addTo, cl, m_types);
Christopher Wileye6dee912015-09-22 14:50:23 -0700524 addTo->Add(new MethodCall(parcel, "readMap", 2, v, *cl));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700525}
526
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700527// ================================================================
528
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700529ListType::ListType(const JavaTypeNamespace* types)
530 : Type(types, "java.util", "List", BUILT_IN, true, true) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700531
532string ListType::InstantiableName() const { return "java.util.ArrayList"; }
533
534void ListType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700535 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700536 addTo->Add(new MethodCall(parcel, "writeList", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700537}
538
Christopher Wileye6dee912015-09-22 14:50:23 -0700539void ListType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700540 Variable* parcel, Variable** cl) const {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700541 EnsureClassLoader(addTo, cl, m_types);
Christopher Wileye6dee912015-09-22 14:50:23 -0700542 addTo->Add(
543 new Assignment(v, new MethodCall(parcel, "readArrayList", 1, *cl)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700544}
545
Christopher Wileye6dee912015-09-22 14:50:23 -0700546void ListType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700547 Variable* parcel, Variable** cl) const {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700548 EnsureClassLoader(addTo, cl, m_types);
Christopher Wileye6dee912015-09-22 14:50:23 -0700549 addTo->Add(new MethodCall(parcel, "readList", 2, v, *cl));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700550}
551
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700552// ================================================================
553
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700554UserDataType::UserDataType(const JavaTypeNamespace* types,
555 const string& package, const string& name,
Christopher Wileye6dee912015-09-22 14:50:23 -0700556 bool builtIn, bool canWriteToParcel,
557 const string& declFile, int declLine)
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700558 : Type(types, package, name, builtIn ? BUILT_IN : USERDATA,
559 canWriteToParcel, true, declFile, declLine) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700560
561string UserDataType::CreatorName() const {
562 return QualifiedName() + ".CREATOR";
Joe Onorato44050522011-10-09 17:38:20 -0700563}
564
Christopher Wileye6dee912015-09-22 14:50:23 -0700565void UserDataType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700566 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700567 // if (v != null) {
568 // parcel.writeInt(1);
569 // v.writeToParcel(parcel);
570 // } else {
571 // parcel.writeInt(0);
572 // }
573 IfStatement* elsepart = new IfStatement();
574 elsepart->statements->Add(
575 new MethodCall(parcel, "writeInt", 1, new LiteralExpression("0")));
576 IfStatement* ifpart = new IfStatement;
577 ifpart->expression = new Comparison(v, "!=", NULL_VALUE);
578 ifpart->elseif = elsepart;
579 ifpart->statements->Add(
580 new MethodCall(parcel, "writeInt", 1, new LiteralExpression("1")));
581 ifpart->statements->Add(new MethodCall(v, "writeToParcel", 2, parcel,
582 BuildWriteToParcelFlags(flags)));
583
584 addTo->Add(ifpart);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700585}
586
Christopher Wileye6dee912015-09-22 14:50:23 -0700587void UserDataType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700588 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700589 // if (0 != parcel.readInt()) {
590 // v = CLASS.CREATOR.createFromParcel(parcel)
591 // } else {
592 // v = null;
593 // }
594 IfStatement* elsepart = new IfStatement();
595 elsepart->statements->Add(new Assignment(v, NULL_VALUE));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700596
Christopher Wileye6dee912015-09-22 14:50:23 -0700597 IfStatement* ifpart = new IfStatement();
598 ifpart->expression = new Comparison(new LiteralExpression("0"), "!=",
599 new MethodCall(parcel, "readInt"));
600 ifpart->elseif = elsepart;
601 ifpart->statements->Add(new Assignment(
602 v, new MethodCall(v->type, "CREATOR.createFromParcel", 1, parcel)));
603
604 addTo->Add(ifpart);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700605}
606
Christopher Wileye6dee912015-09-22 14:50:23 -0700607void UserDataType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700608 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700609 // TODO: really, we don't need to have this extra check, but we
610 // don't have two separate marshalling code paths
611 // if (0 != parcel.readInt()) {
612 // v.readFromParcel(parcel)
613 // }
614 IfStatement* ifpart = new IfStatement();
615 ifpart->expression = new Comparison(new LiteralExpression("0"), "!=",
616 new MethodCall(parcel, "readInt"));
617 ifpart->statements->Add(new MethodCall(v, "readFromParcel", 1, parcel));
618 addTo->Add(ifpart);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700619}
620
Christopher Wileye6dee912015-09-22 14:50:23 -0700621bool UserDataType::CanBeArray() const { return true; }
622
623void UserDataType::WriteArrayToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700624 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700625 addTo->Add(new MethodCall(parcel, "writeTypedArray", 2, v,
626 BuildWriteToParcelFlags(flags)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700627}
628
Christopher Wileye6dee912015-09-22 14:50:23 -0700629void UserDataType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700630 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700631 string creator = v->type->QualifiedName() + ".CREATOR";
632 addTo->Add(new Assignment(v, new MethodCall(parcel, "createTypedArray", 1,
633 new LiteralExpression(creator))));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700634}
635
Christopher Wileye6dee912015-09-22 14:50:23 -0700636void UserDataType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700637 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700638 string creator = v->type->QualifiedName() + ".CREATOR";
639 addTo->Add(new MethodCall(parcel, "readTypedArray", 2, v,
640 new LiteralExpression(creator)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700641}
642
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700643// ================================================================
644
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700645InterfaceType::InterfaceType(const JavaTypeNamespace* types,
646 const string& package, const string& name,
Christopher Wileye6dee912015-09-22 14:50:23 -0700647 bool builtIn, bool oneway, const string& declFile,
648 int declLine)
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700649 : Type(types, package, name, builtIn ? BUILT_IN : INTERFACE, true, false,
650 declFile, declLine),
Christopher Wileye6dee912015-09-22 14:50:23 -0700651 m_oneway(oneway) {}
652
653bool InterfaceType::OneWay() const { return m_oneway; }
654
655void InterfaceType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700656 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700657 // parcel.writeStrongBinder(v != null ? v.asBinder() : null);
658 addTo->Add(
659 new MethodCall(parcel, "writeStrongBinder", 1,
660 new Ternary(new Comparison(v, "!=", NULL_VALUE),
661 new MethodCall(v, "asBinder"), NULL_VALUE)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700662}
663
Christopher Wileye6dee912015-09-22 14:50:23 -0700664void InterfaceType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700665 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700666 // v = Interface.asInterface(parcel.readStrongBinder());
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700667 string stub_type = v->type->QualifiedName() + ".Stub";
Christopher Wileye6dee912015-09-22 14:50:23 -0700668 addTo->Add(new Assignment(
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700669 v, new MethodCall(m_types->Find(stub_type), "asInterface", 1,
Christopher Wileye6dee912015-09-22 14:50:23 -0700670 new MethodCall(parcel, "readStrongBinder"))));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700671}
672
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700673// ================================================================
674
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700675GenericType::GenericType(const JavaTypeNamespace* types, const string& package,
676 const string& name, const vector<const Type*>& args)
677 : Type(types, package, name, BUILT_IN, true, true) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700678 m_args = args;
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700679
Christopher Wileye6dee912015-09-22 14:50:23 -0700680 m_importName = package + '.' + name;
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700681
Christopher Wileye6dee912015-09-22 14:50:23 -0700682 string gen = "<";
683 int N = args.size();
684 for (int i = 0; i < N; i++) {
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700685 const Type* t = args[i];
Christopher Wileye6dee912015-09-22 14:50:23 -0700686 gen += t->QualifiedName();
687 if (i != N - 1) {
688 gen += ',';
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700689 }
Christopher Wileye6dee912015-09-22 14:50:23 -0700690 }
691 gen += '>';
692 m_genericArguments = gen;
693 SetQualifiedName(m_importName + gen);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700694}
695
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700696const vector<const Type*>& GenericType::GenericArgumentTypes() const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700697 return m_args;
Joe Onorato3d0e06f2011-09-02 15:28:36 -0700698}
699
Christopher Wileye6dee912015-09-22 14:50:23 -0700700string GenericType::GenericArguments() const { return m_genericArguments; }
701
702string GenericType::ImportType() const { return m_importName; }
703
704void GenericType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700705 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700706 fprintf(stderr, "implement GenericType::WriteToParcel\n");
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700707}
708
Christopher Wileye6dee912015-09-22 14:50:23 -0700709void GenericType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700710 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700711 fprintf(stderr, "implement GenericType::CreateFromParcel\n");
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700712}
713
Christopher Wileye6dee912015-09-22 14:50:23 -0700714void GenericType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700715 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700716 fprintf(stderr, "implement GenericType::ReadFromParcel\n");
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700717}
718
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700719// ================================================================
720
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700721GenericListType::GenericListType(const JavaTypeNamespace* types,
722 const string& package, const string& name,
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700723 const vector<const Type*>& args)
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700724 : GenericType(types, package, name, args),
725 m_creator(args[0]->CreatorName()) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700726
727string GenericListType::CreatorName() const {
728 return "android.os.Parcel.arrayListCreator";
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700729}
730
Christopher Wileye6dee912015-09-22 14:50:23 -0700731string GenericListType::InstantiableName() const {
732 return "java.util.ArrayList" + GenericArguments();
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700733}
734
Christopher Wileye6dee912015-09-22 14:50:23 -0700735void GenericListType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700736 Variable* parcel, int flags) const {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700737 if (m_creator == m_types->StringType()->CreatorName()) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700738 addTo->Add(new MethodCall(parcel, "writeStringList", 1, v));
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700739 } else if (m_creator == m_types->IBinderType()->CreatorName()) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700740 addTo->Add(new MethodCall(parcel, "writeBinderList", 1, v));
741 } else {
742 // parcel.writeTypedListXX(arg);
743 addTo->Add(new MethodCall(parcel, "writeTypedList", 1, v));
744 }
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700745}
746
Christopher Wileye6dee912015-09-22 14:50:23 -0700747void GenericListType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700748 Variable* parcel, Variable**) const {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700749 if (m_creator == m_types->StringType()->CreatorName()) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700750 addTo->Add(
751 new Assignment(v, new MethodCall(parcel, "createStringArrayList", 0)));
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700752 } else if (m_creator == m_types->IBinderType()->CreatorName()) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700753 addTo->Add(
754 new Assignment(v, new MethodCall(parcel, "createBinderArrayList", 0)));
755 } else {
756 // v = _data.readTypedArrayList(XXX.creator);
757 addTo->Add(
758 new Assignment(v, new MethodCall(parcel, "createTypedArrayList", 1,
759 new LiteralExpression(m_creator))));
760 }
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700761}
762
Christopher Wileye6dee912015-09-22 14:50:23 -0700763void GenericListType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700764 Variable* parcel, Variable**) const {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700765 if (m_creator == m_types->StringType()->CreatorName()) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700766 addTo->Add(new MethodCall(parcel, "readStringList", 1, v));
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700767 } else if (m_creator == m_types->IBinderType()->CreatorName()) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700768 addTo->Add(new MethodCall(parcel, "readBinderList", 1, v));
769 } else {
770 // v = _data.readTypedList(v, XXX.creator);
771 addTo->Add(new MethodCall(parcel, "readTypedList", 2, v,
772 new LiteralExpression(m_creator)));
773 }
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700774}
775
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700776// ================================================================
777
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700778ClassLoaderType::ClassLoaderType(const JavaTypeNamespace* types)
779 : Type(types, "java.lang", "ClassLoader", BUILT_IN, false, false) {}
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700780
781// ================================================================
782
Christopher Wiley4582ecb2015-09-25 13:27:45 -0700783JavaTypeNamespace::ContainerClass::ContainerClass(const string& package,
784 const string& class_name,
785 size_t nargs)
786 : package(package),
787 class_name(class_name),
788 canonical_name(package + "." + class_name),
789 args(nargs) {
790}
791
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700792JavaTypeNamespace::JavaTypeNamespace() {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700793 Add(new BasicType(this, "void", "XXX", "XXX", "XXX", "XXX", "XXX"));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700794
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700795 m_bool_type = new BooleanType(this);
796 Add(m_bool_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700797
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700798 Add(new BasicType(this, "byte", "writeByte", "readByte", "writeByteArray",
799 "createByteArray", "readByteArray"));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700800
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700801 Add(new CharType(this));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700802
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700803 m_int_type = new BasicType(this, "int", "writeInt", "readInt",
804 "writeIntArray", "createIntArray", "readIntArray");
805 Add(m_int_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700806
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700807 Add(new BasicType(this, "long", "writeLong", "readLong", "writeLongArray",
808 "createLongArray", "readLongArray"));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700809
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700810 Add(new BasicType(this, "float", "writeFloat", "readFloat", "writeFloatArray",
811 "createFloatArray", "readFloatArray"));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700812
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700813 Add(new BasicType(this, "double", "writeDouble", "readDouble",
814 "writeDoubleArray", "createDoubleArray",
815 "readDoubleArray"));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700816
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700817 m_string_type = new class StringType(this);
818 Add(m_string_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700819
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700820 Add(new Type(this, "java.lang", "Object", Type::BUILT_IN, false, false));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700821
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700822 Add(new CharSequenceType(this));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700823
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700824 Add(new MapType(this));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700825
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700826 Add(new ListType(this));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700827
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700828 m_text_utils_type =
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700829 new Type(this, "android.text", "TextUtils", Type::BUILT_IN, false, false);
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700830 Add(m_text_utils_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700831
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700832 m_remote_exception_type = new class RemoteExceptionType(this);
833 Add(m_remote_exception_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700834
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700835 m_runtime_exception_type = new class RuntimeExceptionType(this);
836 Add(m_runtime_exception_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700837
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700838 m_ibinder_type = new class IBinderType(this);
839 Add(m_ibinder_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700840
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700841 m_iinterface_type = new class IInterfaceType(this);
842 Add(m_iinterface_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700843
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700844 m_binder_native_type = new class BinderType(this);
845 Add(m_binder_native_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700846
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700847 m_binder_proxy_type = new class BinderProxyType(this);
848 Add(m_binder_proxy_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700849
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700850 m_parcel_type = new class ParcelType(this);
851 Add(m_parcel_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700852
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700853 m_parcelable_interface_type = new class ParcelableInterfaceType(this);
854 Add(m_parcelable_interface_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700855
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700856 m_context_type = new class Type(this, "android.content", "Context",
857 Type::BUILT_IN, false, false);
858 Add(m_context_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700859
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700860 m_classloader_type = new class ClassLoaderType(this);
861 Add(m_classloader_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700862
863 NULL_VALUE = new LiteralExpression("null");
864 THIS_VALUE = new LiteralExpression("this");
865 SUPER_VALUE = new LiteralExpression("super");
866 TRUE_VALUE = new LiteralExpression("true");
867 FALSE_VALUE = new LiteralExpression("false");
868
Christopher Wiley4582ecb2015-09-25 13:27:45 -0700869 m_containers.emplace_back("java.util", "List", 1);
870 m_containers.emplace_back("java.util", "Map", 2);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700871}
Christopher Wileye6dee912015-09-22 14:50:23 -0700872
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700873JavaTypeNamespace::~JavaTypeNamespace() {
Christopher Wileye6dee912015-09-22 14:50:23 -0700874 int N = m_types.size();
875 for (int i = 0; i < N; i++) {
876 delete m_types[i];
877 }
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700878}
879
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700880bool JavaTypeNamespace::Add(const Type* type) {
881 const Type* existing = Find(type->QualifiedName());
882 if (!existing) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700883 m_types.push_back(type);
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700884 return true;
Christopher Wileye6dee912015-09-22 14:50:23 -0700885 }
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700886
887 if (existing->Kind() == Type::BUILT_IN) {
888 fprintf(stderr, "%s:%d attempt to redefine built in class %s\n",
889 type->DeclFile().c_str(), type->DeclLine(),
890 type->QualifiedName().c_str());
891 return false;
892 }
893
894 if (type->Kind() != existing->Kind()) {
895 fprintf(stderr, "%s:%d attempt to redefine %s as %s,\n",
896 type->DeclFile().c_str(), type->DeclLine(),
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700897 type->QualifiedName().c_str(), type->HumanReadableKind().c_str());
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700898 fprintf(stderr, "%s:%d previously defined here as %s.\n",
899 existing->DeclFile().c_str(), existing->DeclLine(),
900 existing->HumanReadableKind().c_str());
901 return false;
902 }
903
904 return true;
Christopher Wileye6dee912015-09-22 14:50:23 -0700905}
906
Christopher Wiley4582ecb2015-09-25 13:27:45 -0700907const Type* JavaTypeNamespace::Search(const string& name) {
908 const Type* result = Find(name);
909 if (result != nullptr) {
910 return result;
911 }
912
913 if (!AddContainerType(name)) {
914 return nullptr;
915 }
916
917 return Find(name);
Christopher Wileye6dee912015-09-22 14:50:23 -0700918}
919
Christopher Wiley4582ecb2015-09-25 13:27:45 -0700920const Type* JavaTypeNamespace::Find(const string& unstripped_name) const {
921 const ContainerClass* g = nullptr;
922 vector<const Type*> template_arg_types;
923 if (!CanonicalizeContainerClass(unstripped_name, &g, &template_arg_types)) {
924 LOG(ERROR) << "Error canonicalizing type '" << unstripped_name << "'";
925 return nullptr;
926 }
927
928 string name = Trim(unstripped_name);
929 if (g != nullptr) {
930 vector<string> template_args;
931 for (const Type* type : template_arg_types) {
932 template_args.push_back(type->QualifiedName());
933 }
934 name = g->canonical_name + "<" + Join(template_args, ',') + ">";
935 }
936
937 // Always prefer a exact match if possible.
938 // This works for primitives and class names qualified with a package.
939 for (const Type* type : m_types) {
940 if (type->QualifiedName() == name) {
941 return type;
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700942 }
Christopher Wileye6dee912015-09-22 14:50:23 -0700943 }
Christopher Wiley4582ecb2015-09-25 13:27:45 -0700944
945 // We allow authors to drop packages when refering to a class name.
946 // our language doesn't allow you to not specify outer classes
947 // when referencing an inner class. that could be changed, and this
948 // would be the place to do it, but I don't think the complexity in
949 // scoping rules is worth it.
950 for (const Type* type : m_types) {
951 if (type->Name() == name) {
952 return type;
953 }
954 }
955
956 return nullptr;
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700957}
958
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700959const Type* JavaTypeNamespace::Find(const char* package,
960 const char* name) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700961 string s;
962 if (package != nullptr && *package != '\0') {
963 s += package;
964 s += '.';
965 }
966 s += name;
967 return Find(s);
968}
969
Christopher Wiley4582ecb2015-09-25 13:27:45 -0700970bool JavaTypeNamespace::AddParcelableType(const user_data_type* p,
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700971 const std::string& filename) {
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700972 Type* type =
973 new UserDataType(this, p->package ? p->package : "", p->name.data, false,
974 p->parcelable, filename, p->name.lineno);
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700975 return Add(type);
976}
977
Christopher Wiley4582ecb2015-09-25 13:27:45 -0700978bool JavaTypeNamespace::AddBinderType(const interface_type* b,
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700979 const std::string& filename) {
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700980 // for interfaces, add the stub, proxy, and interface types.
981 Type* type =
982 new InterfaceType(this, b->package ? b->package : "", b->name.data, false,
983 b->oneway, filename, b->name.lineno);
984 Type* stub = new Type(this, b->package ? b->package : "",
985 string{b->name.data} + ".Stub", Type::GENERATED, false,
986 false, filename, b->name.lineno);
987 Type* proxy = new Type(this, b->package ? b->package : "",
988 string{b->name.data} + ".Stub.Proxy", Type::GENERATED,
989 false, false, filename, b->name.lineno);
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700990
991 bool success = true;
992 success &= Add(type);
993 success &= Add(stub);
994 success &= Add(proxy);
995 return success;
996}
997
Christopher Wiley4582ecb2015-09-25 13:27:45 -0700998bool JavaTypeNamespace::AddContainerType(const string& type_name) {
999 if (Find(type_name) != nullptr) {
1000 return true; // Don't add duplicates of the same templated type.
Christopher Wileye6dee912015-09-22 14:50:23 -07001001 }
1002
Christopher Wiley4582ecb2015-09-25 13:27:45 -07001003 const ContainerClass* g = nullptr;
1004 vector<const Type*> template_arg_types;
1005 if (!CanonicalizeContainerClass(type_name, &g, &template_arg_types)) {
1006 LOG(ERROR) << "Error canonicalizing type '" << type_name << "'";
1007 return false;
Christopher Wileye6dee912015-09-22 14:50:23 -07001008 }
1009
Christopher Wiley4582ecb2015-09-25 13:27:45 -07001010 if (g == nullptr) {
1011 // We parsed the type correctly, but it wasn't a container type. No error.
1012 return true;
Christopher Wileye6dee912015-09-22 14:50:23 -07001013 }
1014
Christopher Wiley4582ecb2015-09-25 13:27:45 -07001015 // construct an instance of a container type, add it to our name set so they
1016 // always get the same object, and return it.
1017 Type* result = nullptr;
1018 if (g->canonical_name == "java.util.List" &&
1019 template_arg_types.size() == 1u) {
1020 result = new GenericListType(this, g->package, g->class_name,
1021 template_arg_types);
1022 } else {
1023 LOG(ERROR) << "Don't know how to create a container of type "
1024 << g->canonical_name << " with " << template_arg_types.size()
1025 << " arguments.";
1026 return false;
Christopher Wileye6dee912015-09-22 14:50:23 -07001027 }
1028
Christopher Wiley60b02ae2015-09-23 16:35:18 -07001029 Add(result);
Christopher Wiley4582ecb2015-09-25 13:27:45 -07001030 return true;
The Android Open Source Project46c012c2008-10-21 07:00:00 -07001031}
1032
Christopher Wiley4582ecb2015-09-25 13:27:45 -07001033const JavaTypeNamespace::ContainerClass* JavaTypeNamespace::FindContainerClass(
1034 const string& name,
1035 size_t nargs) const {
1036 // first check fully qualified class names (with packages).
1037 for (const ContainerClass& container : m_containers) {
1038 if (container.canonical_name == name && nargs == container.args) {
1039 return &container;
The Android Open Source Project46c012c2008-10-21 07:00:00 -07001040 }
Christopher Wileye6dee912015-09-22 14:50:23 -07001041 }
1042
Christopher Wiley4582ecb2015-09-25 13:27:45 -07001043 // then match on the class name alone (no package).
1044 for (const ContainerClass& container : m_containers) {
1045 if (container.class_name == name && nargs == container.args) {
1046 return &container;
Christopher Wileye6dee912015-09-22 14:50:23 -07001047 }
1048 }
1049
Christopher Wiley4582ecb2015-09-25 13:27:45 -07001050 return nullptr;
1051}
1052
1053bool JavaTypeNamespace::CanonicalizeContainerClass(
1054 const string& raw_name,
1055 const ContainerClass** container_class,
1056 vector<const Type*>* arg_types) const {
1057 string name = Trim(raw_name);
1058 const size_t opening_brace = name.find('<');
1059 if (opening_brace == name.npos) {
1060 // Not a template type and not an error.
1061 *container_class = nullptr;
1062 arg_types->clear();
1063 return true;
1064 }
1065
1066 const size_t closing_brace = name.find('>');
1067 if (opening_brace != name.rfind('<') ||
1068 closing_brace != name.rfind('>') ||
1069 closing_brace != name.length() - 1) {
1070 LOG(ERROR) << "Invalid template type '" << name << "'";
1071 // Nested/invalid templates are forbidden.
1072 return false;
1073 }
1074
1075 string container_class_name = Trim(name.substr(0, opening_brace));
1076 string remainder = name.substr(opening_brace + 1,
1077 (closing_brace - opening_brace) - 1);
1078 vector<string> template_args = Split(remainder, ",");
1079
1080 const ContainerClass* g =
1081 FindContainerClass(container_class_name, template_args.size());
1082 if (g == nullptr) {
1083 LOG(ERROR) << "Failed to find templated container '"
1084 << container_class_name << "'";
1085 return false;
1086 }
1087
1088 vector<const Type*> template_arg_types;
1089 for (auto& template_arg : template_args) {
1090 // Recursively search for the contained types.
1091 const Type* template_arg_type = Find(Trim(template_arg));
1092 if (template_arg_type == nullptr) {
1093 LOG(ERROR) << "Failed to find formal type of '"
1094 << template_arg << "'";
1095 return false;
1096 }
1097 template_arg_types.push_back(template_arg_type);
1098 }
1099
1100 *arg_types = template_arg_types;
1101 *container_class = g;
1102 return true;
The Android Open Source Project46c012c2008-10-21 07:00:00 -07001103}
1104
Christopher Wiley84c1eac2015-09-23 13:29:28 -07001105void JavaTypeNamespace::Dump() const {
Christopher Wileye6dee912015-09-22 14:50:23 -07001106 int n = m_types.size();
1107 for (int i = 0; i < n; i++) {
Christopher Wiley8f6816e2015-09-22 17:03:47 -07001108 const Type* t = m_types[i];
Christopher Wileye6dee912015-09-22 14:50:23 -07001109 printf("type: package=%s name=%s qualifiedName=%s\n", t->Package().c_str(),
1110 t->Name().c_str(), t->QualifiedName().c_str());
1111 }
The Android Open Source Project46c012c2008-10-21 07:00:00 -07001112}
Christopher Wileyfdeb0f42015-09-11 15:38:22 -07001113
1114} // namespace aidl
1115} // namespace android