blob: bd1b7000ddc8a28ad0f770ea3d150a80e40c551e [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 {
Christopher Wileydb154a52015-09-28 16:32:25 -070032namespace java {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070033
The Android Open Source Project46c012c2008-10-21 07:00:00 -070034Expression* NULL_VALUE;
35Expression* THIS_VALUE;
36Expression* SUPER_VALUE;
37Expression* TRUE_VALUE;
38Expression* FALSE_VALUE;
39
The Android Open Source Project46c012c2008-10-21 07:00:00 -070040// ================================================================
41
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -070042Type::Type(const JavaTypeNamespace* types, const string& name, int kind,
43 bool canWriteToParcel, bool canBeOut)
44 : m_types(types),
45 m_package(),
Christopher Wileye6dee912015-09-22 14:50:23 -070046 m_name(name),
47 m_declFile(""),
48 m_declLine(-1),
49 m_kind(kind),
50 m_canWriteToParcel(canWriteToParcel),
51 m_canBeOut(canBeOut) {
52 m_qualifiedName = name;
The Android Open Source Project46c012c2008-10-21 07:00:00 -070053}
54
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -070055Type::Type(const JavaTypeNamespace* types, const string& package,
56 const string& name, int kind, bool canWriteToParcel, bool canBeOut,
57 const string& declFile, int declLine)
58 : m_types(types),
59 m_package(package),
Christopher Wileye6dee912015-09-22 14:50:23 -070060 m_name(name),
61 m_declFile(declFile),
62 m_declLine(declLine),
63 m_kind(kind),
64 m_canWriteToParcel(canWriteToParcel),
65 m_canBeOut(canBeOut) {
66 if (package.length() > 0) {
67 m_qualifiedName = package;
68 m_qualifiedName += '.';
69 }
70 m_qualifiedName += name;
The Android Open Source Project46c012c2008-10-21 07:00:00 -070071}
72
Christopher Wileye6dee912015-09-22 14:50:23 -070073Type::~Type() {}
74
75string Type::HumanReadableKind() const {
76 switch (Kind()) {
77 case INTERFACE:
78 return "an interface";
79 case USERDATA:
80 return "a user data";
81 default:
82 return "ERROR";
83 }
The Android Open Source Project46c012c2008-10-21 07:00:00 -070084}
85
Christopher Wileye6dee912015-09-22 14:50:23 -070086bool Type::CanBeArray() const { return false; }
87
88string Type::ImportType() const { return m_qualifiedName; }
89
90string Type::CreatorName() const { return ""; }
91
92string Type::InstantiableName() const { return QualifiedName(); }
93
94void Type::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel,
Christopher Wiley3637a4d2015-09-22 15:37:59 -070095 int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -070096 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%sn", __FILE__,
97 __LINE__, m_qualifiedName.c_str());
98 addTo->Add(new LiteralExpression("/* WriteToParcel error " + m_qualifiedName +
99 " */"));
Christopher Wileyf690be52015-09-14 15:19:10 -0700100}
101
Christopher Wileye6dee912015-09-22 14:50:23 -0700102void Type::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700103 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700104 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n", __FILE__,
105 __LINE__, m_qualifiedName.c_str());
106 addTo->Add(new LiteralExpression("/* CreateFromParcel error " +
107 m_qualifiedName + " */"));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700108}
109
Christopher Wileye6dee912015-09-22 14:50:23 -0700110void Type::ReadFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700111 Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700112 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n", __FILE__,
113 __LINE__, m_qualifiedName.c_str());
114 addTo->Add(new LiteralExpression("/* ReadFromParcel error " +
115 m_qualifiedName + " */"));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700116}
117
Christopher Wileye6dee912015-09-22 14:50:23 -0700118void Type::WriteArrayToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700119 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700120 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n", __FILE__,
121 __LINE__, m_qualifiedName.c_str());
122 addTo->Add(new LiteralExpression("/* WriteArrayToParcel error " +
123 m_qualifiedName + " */"));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700124}
125
Christopher Wileye6dee912015-09-22 14:50:23 -0700126void Type::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700127 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700128 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n", __FILE__,
129 __LINE__, m_qualifiedName.c_str());
130 addTo->Add(new LiteralExpression("/* CreateArrayFromParcel error " +
131 m_qualifiedName + " */"));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700132}
133
Christopher Wileye6dee912015-09-22 14:50:23 -0700134void Type::ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700135 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700136 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n", __FILE__,
137 __LINE__, m_qualifiedName.c_str());
138 addTo->Add(new LiteralExpression("/* ReadArrayFromParcel error " +
139 m_qualifiedName + " */"));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700140}
141
Christopher Wileye6dee912015-09-22 14:50:23 -0700142void Type::SetQualifiedName(const string& qualified) {
143 m_qualifiedName = qualified;
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700144}
145
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700146Expression* Type::BuildWriteToParcelFlags(int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700147 if (flags == 0) {
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700148 return new LiteralExpression("0");
Christopher Wileye6dee912015-09-22 14:50:23 -0700149 }
150 if ((flags & PARCELABLE_WRITE_RETURN_VALUE) != 0) {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700151 return new FieldVariable(m_types->ParcelableInterfaceType(),
Christopher Wileye6dee912015-09-22 14:50:23 -0700152 "PARCELABLE_WRITE_RETURN_VALUE");
153 }
154 return new LiteralExpression("0");
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700155}
156
157// ================================================================
158
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700159BasicType::BasicType(const JavaTypeNamespace* types, const string& name,
160 const string& marshallParcel,
Christopher Wileye6dee912015-09-22 14:50:23 -0700161 const string& unmarshallParcel,
162 const string& writeArrayParcel,
163 const string& createArrayParcel,
164 const string& readArrayParcel)
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700165 : Type(types, name, BUILT_IN, true, false),
Christopher Wileye6dee912015-09-22 14:50:23 -0700166 m_marshallParcel(marshallParcel),
167 m_unmarshallParcel(unmarshallParcel),
168 m_writeArrayParcel(writeArrayParcel),
169 m_createArrayParcel(createArrayParcel),
170 m_readArrayParcel(readArrayParcel) {}
171
172void BasicType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700173 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700174 addTo->Add(new MethodCall(parcel, m_marshallParcel, 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700175}
176
Christopher Wileye6dee912015-09-22 14:50:23 -0700177void BasicType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700178 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700179 addTo->Add(new Assignment(v, new MethodCall(parcel, m_unmarshallParcel)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700180}
181
Christopher Wileye6dee912015-09-22 14:50:23 -0700182bool BasicType::CanBeArray() const { return true; }
183
184void BasicType::WriteArrayToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700185 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700186 addTo->Add(new MethodCall(parcel, m_writeArrayParcel, 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700187}
188
Christopher Wileye6dee912015-09-22 14:50:23 -0700189void BasicType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700190 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700191 addTo->Add(new Assignment(v, new MethodCall(parcel, m_createArrayParcel)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700192}
193
Christopher Wileye6dee912015-09-22 14:50:23 -0700194void BasicType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700195 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700196 addTo->Add(new MethodCall(parcel, m_readArrayParcel, 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700197}
198
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700199// ================================================================
200
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700201BooleanType::BooleanType(const JavaTypeNamespace* types)
202 : Type(types, "boolean", BUILT_IN, true, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700203
204void BooleanType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700205 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700206 addTo->Add(new MethodCall(
207 parcel, "writeInt", 1,
208 new Ternary(v, new LiteralExpression("1"), new LiteralExpression("0"))));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700209}
210
Christopher Wileye6dee912015-09-22 14:50:23 -0700211void BooleanType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700212 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700213 addTo->Add(
214 new Assignment(v, new Comparison(new LiteralExpression("0"), "!=",
215 new MethodCall(parcel, "readInt"))));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700216}
217
Christopher Wileye6dee912015-09-22 14:50:23 -0700218bool BooleanType::CanBeArray() const { return true; }
219
220void BooleanType::WriteArrayToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700221 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700222 addTo->Add(new MethodCall(parcel, "writeBooleanArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700223}
224
Christopher Wileye6dee912015-09-22 14:50:23 -0700225void BooleanType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700226 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700227 addTo->Add(new Assignment(v, new MethodCall(parcel, "createBooleanArray")));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700228}
229
Christopher Wileye6dee912015-09-22 14:50:23 -0700230void BooleanType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700231 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700232 addTo->Add(new MethodCall(parcel, "readBooleanArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700233}
234
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700235// ================================================================
236
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700237CharType::CharType(const JavaTypeNamespace* types)
238 : Type(types, "char", BUILT_IN, true, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700239
240void CharType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700241 Variable* parcel, int flags) const {
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700242 addTo->Add(
243 new MethodCall(parcel, "writeInt", 1, new Cast(m_types->IntType(), v)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700244}
245
Christopher Wileye6dee912015-09-22 14:50:23 -0700246void CharType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700247 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700248 addTo->Add(new Assignment(v, new MethodCall(parcel, "readInt"), this));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700249}
250
Christopher Wileye6dee912015-09-22 14:50:23 -0700251bool CharType::CanBeArray() const { return true; }
252
253void CharType::WriteArrayToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700254 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700255 addTo->Add(new MethodCall(parcel, "writeCharArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700256}
257
Christopher Wileye6dee912015-09-22 14:50:23 -0700258void CharType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700259 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700260 addTo->Add(new Assignment(v, new MethodCall(parcel, "createCharArray")));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700261}
262
Christopher Wileye6dee912015-09-22 14:50:23 -0700263void CharType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700264 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700265 addTo->Add(new MethodCall(parcel, "readCharArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700266}
267
268// ================================================================
269
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700270StringType::StringType(const JavaTypeNamespace* types)
271 : Type(types, "java.lang", "String", BUILT_IN, true, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700272
273string StringType::CreatorName() const {
274 return "android.os.Parcel.STRING_CREATOR";
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700275}
276
Christopher Wileye6dee912015-09-22 14:50:23 -0700277void StringType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700278 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700279 addTo->Add(new MethodCall(parcel, "writeString", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700280}
281
Christopher Wileye6dee912015-09-22 14:50:23 -0700282void StringType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700283 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700284 addTo->Add(new Assignment(v, new MethodCall(parcel, "readString")));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700285}
286
Christopher Wileye6dee912015-09-22 14:50:23 -0700287bool StringType::CanBeArray() const { return true; }
288
289void StringType::WriteArrayToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700290 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700291 addTo->Add(new MethodCall(parcel, "writeStringArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700292}
293
Christopher Wileye6dee912015-09-22 14:50:23 -0700294void StringType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700295 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700296 addTo->Add(new Assignment(v, new MethodCall(parcel, "createStringArray")));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700297}
298
Christopher Wileye6dee912015-09-22 14:50:23 -0700299void StringType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700300 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700301 addTo->Add(new MethodCall(parcel, "readStringArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700302}
303
304// ================================================================
305
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700306CharSequenceType::CharSequenceType(const JavaTypeNamespace* types)
307 : Type(types, "java.lang", "CharSequence", BUILT_IN, true, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700308
309string CharSequenceType::CreatorName() const {
310 return "android.os.Parcel.STRING_CREATOR";
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700311}
312
Christopher Wileye6dee912015-09-22 14:50:23 -0700313void CharSequenceType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700314 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700315 // if (v != null) {
316 // parcel.writeInt(1);
317 // v.writeToParcel(parcel);
318 // } else {
319 // parcel.writeInt(0);
320 // }
321 IfStatement* elsepart = new IfStatement();
322 elsepart->statements->Add(
323 new MethodCall(parcel, "writeInt", 1, new LiteralExpression("0")));
324 IfStatement* ifpart = new IfStatement;
325 ifpart->expression = new Comparison(v, "!=", NULL_VALUE);
326 ifpart->elseif = elsepart;
327 ifpart->statements->Add(
328 new MethodCall(parcel, "writeInt", 1, new LiteralExpression("1")));
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700329 ifpart->statements->Add(new MethodCall(m_types->TextUtilsType(),
330 "writeToParcel", 3, v, parcel,
Christopher Wileye6dee912015-09-22 14:50:23 -0700331 BuildWriteToParcelFlags(flags)));
332
333 addTo->Add(ifpart);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700334}
335
Christopher Wileye6dee912015-09-22 14:50:23 -0700336void CharSequenceType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700337 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700338 // if (0 != parcel.readInt()) {
339 // v = TextUtils.createFromParcel(parcel)
340 // } else {
341 // v = null;
342 // }
343 IfStatement* elsepart = new IfStatement();
344 elsepart->statements->Add(new Assignment(v, NULL_VALUE));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700345
Christopher Wileye6dee912015-09-22 14:50:23 -0700346 IfStatement* ifpart = new IfStatement();
347 ifpart->expression = new Comparison(new LiteralExpression("0"), "!=",
348 new MethodCall(parcel, "readInt"));
349 ifpart->elseif = elsepart;
350 ifpart->statements->Add(new Assignment(
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700351 v, new MethodCall(m_types->TextUtilsType(),
Christopher Wileye6dee912015-09-22 14:50:23 -0700352 "CHAR_SEQUENCE_CREATOR.createFromParcel", 1, parcel)));
353
354 addTo->Add(ifpart);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700355}
356
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700357// ================================================================
358
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700359RemoteExceptionType::RemoteExceptionType(const JavaTypeNamespace* types)
360 : Type(types, "android.os", "RemoteException", BUILT_IN, false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700361
362void RemoteExceptionType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700363 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700364 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700365}
366
Christopher Wileye6dee912015-09-22 14:50:23 -0700367void RemoteExceptionType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700368 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700369 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700370}
371
372// ================================================================
373
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700374RuntimeExceptionType::RuntimeExceptionType(const JavaTypeNamespace* types)
375 : Type(types, "java.lang", "RuntimeException", BUILT_IN, false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700376
377void RuntimeExceptionType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700378 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700379 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700380}
381
Christopher Wileye6dee912015-09-22 14:50:23 -0700382void RuntimeExceptionType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700383 Variable* parcel,
384 Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700385 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700386}
387
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700388// ================================================================
389
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700390IBinderType::IBinderType(const JavaTypeNamespace* types)
391 : Type(types, "android.os", "IBinder", BUILT_IN, true, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700392
393void IBinderType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700394 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700395 addTo->Add(new MethodCall(parcel, "writeStrongBinder", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700396}
397
Christopher Wileye6dee912015-09-22 14:50:23 -0700398void IBinderType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700399 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700400 addTo->Add(new Assignment(v, new MethodCall(parcel, "readStrongBinder")));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700401}
402
Christopher Wileye6dee912015-09-22 14:50:23 -0700403void IBinderType::WriteArrayToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700404 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700405 addTo->Add(new MethodCall(parcel, "writeBinderArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700406}
407
Christopher Wileye6dee912015-09-22 14:50:23 -0700408void IBinderType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700409 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700410 addTo->Add(new Assignment(v, new MethodCall(parcel, "createBinderArray")));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700411}
412
Christopher Wileye6dee912015-09-22 14:50:23 -0700413void IBinderType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700414 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700415 addTo->Add(new MethodCall(parcel, "readBinderArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700416}
417
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700418// ================================================================
419
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700420IInterfaceType::IInterfaceType(const JavaTypeNamespace* types)
421 : Type(types, "android.os", "IInterface", BUILT_IN, false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700422
423void IInterfaceType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700424 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700425 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700426}
427
Christopher Wileye6dee912015-09-22 14:50:23 -0700428void IInterfaceType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700429 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700430 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700431}
432
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700433// ================================================================
434
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700435BinderType::BinderType(const JavaTypeNamespace* types)
436 : Type(types, "android.os", "Binder", BUILT_IN, false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700437
438void BinderType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700439 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700440 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700441}
442
Christopher Wileye6dee912015-09-22 14:50:23 -0700443void BinderType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700444 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700445 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700446}
447
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700448// ================================================================
449
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700450BinderProxyType::BinderProxyType(const JavaTypeNamespace* types)
451 : Type(types, "android.os", "BinderProxy", BUILT_IN, false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700452
453void BinderProxyType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700454 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700455 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700456}
457
Christopher Wileye6dee912015-09-22 14:50:23 -0700458void BinderProxyType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700459 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700460 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700461}
462
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700463// ================================================================
464
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700465ParcelType::ParcelType(const JavaTypeNamespace* types)
466 : Type(types, "android.os", "Parcel", BUILT_IN, false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700467
468void ParcelType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700469 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700470 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700471}
472
Christopher Wileye6dee912015-09-22 14:50:23 -0700473void ParcelType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700474 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700475 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700476}
477
478// ================================================================
479
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700480ParcelableInterfaceType::ParcelableInterfaceType(const JavaTypeNamespace* types)
481 : Type(types, "android.os", "Parcelable", BUILT_IN, false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700482
483void ParcelableInterfaceType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700484 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700485 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700486}
487
Christopher Wileye6dee912015-09-22 14:50:23 -0700488void ParcelableInterfaceType::CreateFromParcel(StatementBlock* addTo,
489 Variable* v, Variable* parcel,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700490 Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700491 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700492}
493
494// ================================================================
495
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700496MapType::MapType(const JavaTypeNamespace* types)
497 : Type(types, "java.util", "Map", BUILT_IN, true, true) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700498
499void MapType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700500 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700501 addTo->Add(new MethodCall(parcel, "writeMap", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700502}
503
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700504static void EnsureClassLoader(StatementBlock* addTo, Variable** cl,
505 const JavaTypeNamespace* types) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700506 // We don't want to look up the class loader once for every
507 // collection argument, so ensure we do it at most once per method.
508 if (*cl == NULL) {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700509 *cl = new Variable(types->ClassLoaderType(), "cl");
Christopher Wileye6dee912015-09-22 14:50:23 -0700510 addTo->Add(new VariableDeclaration(
511 *cl, new LiteralExpression("this.getClass().getClassLoader()"),
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700512 types->ClassLoaderType()));
Christopher Wileye6dee912015-09-22 14:50:23 -0700513 }
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700514}
515
Christopher Wileye6dee912015-09-22 14:50:23 -0700516void MapType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700517 Variable* parcel, Variable** cl) const {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700518 EnsureClassLoader(addTo, cl, m_types);
Christopher Wileye6dee912015-09-22 14:50:23 -0700519 addTo->Add(new Assignment(v, new MethodCall(parcel, "readHashMap", 1, *cl)));
Elliott Hughes15f8da22011-07-13 12:10:30 -0700520}
521
Christopher Wileye6dee912015-09-22 14:50:23 -0700522void MapType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700523 Variable* parcel, Variable** cl) const {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700524 EnsureClassLoader(addTo, cl, m_types);
Christopher Wileye6dee912015-09-22 14:50:23 -0700525 addTo->Add(new MethodCall(parcel, "readMap", 2, v, *cl));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700526}
527
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700528// ================================================================
529
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700530ListType::ListType(const JavaTypeNamespace* types)
531 : Type(types, "java.util", "List", BUILT_IN, true, true) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700532
533string ListType::InstantiableName() const { return "java.util.ArrayList"; }
534
535void ListType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700536 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700537 addTo->Add(new MethodCall(parcel, "writeList", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700538}
539
Christopher Wileye6dee912015-09-22 14:50:23 -0700540void ListType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700541 Variable* parcel, Variable** cl) const {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700542 EnsureClassLoader(addTo, cl, m_types);
Christopher Wileye6dee912015-09-22 14:50:23 -0700543 addTo->Add(
544 new Assignment(v, new MethodCall(parcel, "readArrayList", 1, *cl)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700545}
546
Christopher Wileye6dee912015-09-22 14:50:23 -0700547void ListType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700548 Variable* parcel, Variable** cl) const {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700549 EnsureClassLoader(addTo, cl, m_types);
Christopher Wileye6dee912015-09-22 14:50:23 -0700550 addTo->Add(new MethodCall(parcel, "readList", 2, v, *cl));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700551}
552
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700553// ================================================================
554
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700555UserDataType::UserDataType(const JavaTypeNamespace* types,
556 const string& package, const string& name,
Christopher Wileye6dee912015-09-22 14:50:23 -0700557 bool builtIn, bool canWriteToParcel,
558 const string& declFile, int declLine)
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700559 : Type(types, package, name, builtIn ? BUILT_IN : USERDATA,
560 canWriteToParcel, true, declFile, declLine) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700561
562string UserDataType::CreatorName() const {
563 return QualifiedName() + ".CREATOR";
Joe Onorato44050522011-10-09 17:38:20 -0700564}
565
Christopher Wileye6dee912015-09-22 14:50:23 -0700566void UserDataType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700567 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700568 // if (v != null) {
569 // parcel.writeInt(1);
570 // v.writeToParcel(parcel);
571 // } else {
572 // parcel.writeInt(0);
573 // }
574 IfStatement* elsepart = new IfStatement();
575 elsepart->statements->Add(
576 new MethodCall(parcel, "writeInt", 1, new LiteralExpression("0")));
577 IfStatement* ifpart = new IfStatement;
578 ifpart->expression = new Comparison(v, "!=", NULL_VALUE);
579 ifpart->elseif = elsepart;
580 ifpart->statements->Add(
581 new MethodCall(parcel, "writeInt", 1, new LiteralExpression("1")));
582 ifpart->statements->Add(new MethodCall(v, "writeToParcel", 2, parcel,
583 BuildWriteToParcelFlags(flags)));
584
585 addTo->Add(ifpart);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700586}
587
Christopher Wileye6dee912015-09-22 14:50:23 -0700588void UserDataType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700589 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700590 // if (0 != parcel.readInt()) {
591 // v = CLASS.CREATOR.createFromParcel(parcel)
592 // } else {
593 // v = null;
594 // }
595 IfStatement* elsepart = new IfStatement();
596 elsepart->statements->Add(new Assignment(v, NULL_VALUE));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700597
Christopher Wileye6dee912015-09-22 14:50:23 -0700598 IfStatement* ifpart = new IfStatement();
599 ifpart->expression = new Comparison(new LiteralExpression("0"), "!=",
600 new MethodCall(parcel, "readInt"));
601 ifpart->elseif = elsepart;
602 ifpart->statements->Add(new Assignment(
603 v, new MethodCall(v->type, "CREATOR.createFromParcel", 1, parcel)));
604
605 addTo->Add(ifpart);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700606}
607
Christopher Wileye6dee912015-09-22 14:50:23 -0700608void UserDataType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700609 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700610 // TODO: really, we don't need to have this extra check, but we
611 // don't have two separate marshalling code paths
612 // if (0 != parcel.readInt()) {
613 // v.readFromParcel(parcel)
614 // }
615 IfStatement* ifpart = new IfStatement();
616 ifpart->expression = new Comparison(new LiteralExpression("0"), "!=",
617 new MethodCall(parcel, "readInt"));
618 ifpart->statements->Add(new MethodCall(v, "readFromParcel", 1, parcel));
619 addTo->Add(ifpart);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700620}
621
Christopher Wileye6dee912015-09-22 14:50:23 -0700622bool UserDataType::CanBeArray() const { return true; }
623
624void UserDataType::WriteArrayToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700625 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700626 addTo->Add(new MethodCall(parcel, "writeTypedArray", 2, v,
627 BuildWriteToParcelFlags(flags)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700628}
629
Christopher Wileye6dee912015-09-22 14:50:23 -0700630void UserDataType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700631 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700632 string creator = v->type->QualifiedName() + ".CREATOR";
633 addTo->Add(new Assignment(v, new MethodCall(parcel, "createTypedArray", 1,
634 new LiteralExpression(creator))));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700635}
636
Christopher Wileye6dee912015-09-22 14:50:23 -0700637void UserDataType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700638 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700639 string creator = v->type->QualifiedName() + ".CREATOR";
640 addTo->Add(new MethodCall(parcel, "readTypedArray", 2, v,
641 new LiteralExpression(creator)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700642}
643
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700644// ================================================================
645
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700646InterfaceType::InterfaceType(const JavaTypeNamespace* types,
647 const string& package, const string& name,
Christopher Wileye6dee912015-09-22 14:50:23 -0700648 bool builtIn, bool oneway, const string& declFile,
649 int declLine)
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700650 : Type(types, package, name, builtIn ? BUILT_IN : INTERFACE, true, false,
651 declFile, declLine),
Christopher Wileye6dee912015-09-22 14:50:23 -0700652 m_oneway(oneway) {}
653
654bool InterfaceType::OneWay() const { return m_oneway; }
655
656void InterfaceType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700657 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700658 // parcel.writeStrongBinder(v != null ? v.asBinder() : null);
659 addTo->Add(
660 new MethodCall(parcel, "writeStrongBinder", 1,
661 new Ternary(new Comparison(v, "!=", NULL_VALUE),
662 new MethodCall(v, "asBinder"), NULL_VALUE)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700663}
664
Christopher Wileye6dee912015-09-22 14:50:23 -0700665void InterfaceType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700666 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700667 // v = Interface.asInterface(parcel.readStrongBinder());
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700668 string stub_type = v->type->QualifiedName() + ".Stub";
Christopher Wileye6dee912015-09-22 14:50:23 -0700669 addTo->Add(new Assignment(
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700670 v, new MethodCall(m_types->Find(stub_type), "asInterface", 1,
Christopher Wileye6dee912015-09-22 14:50:23 -0700671 new MethodCall(parcel, "readStrongBinder"))));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700672}
673
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700674// ================================================================
675
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700676GenericType::GenericType(const JavaTypeNamespace* types, const string& package,
677 const string& name, const vector<const Type*>& args)
678 : Type(types, package, name, BUILT_IN, true, true) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700679 m_args = args;
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700680
Christopher Wileye6dee912015-09-22 14:50:23 -0700681 m_importName = package + '.' + name;
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700682
Christopher Wileye6dee912015-09-22 14:50:23 -0700683 string gen = "<";
684 int N = args.size();
685 for (int i = 0; i < N; i++) {
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700686 const Type* t = args[i];
Christopher Wileye6dee912015-09-22 14:50:23 -0700687 gen += t->QualifiedName();
688 if (i != N - 1) {
689 gen += ',';
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700690 }
Christopher Wileye6dee912015-09-22 14:50:23 -0700691 }
692 gen += '>';
693 m_genericArguments = gen;
694 SetQualifiedName(m_importName + gen);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700695}
696
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700697const vector<const Type*>& GenericType::GenericArgumentTypes() const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700698 return m_args;
Joe Onorato3d0e06f2011-09-02 15:28:36 -0700699}
700
Christopher Wileye6dee912015-09-22 14:50:23 -0700701string GenericType::GenericArguments() const { return m_genericArguments; }
702
703string GenericType::ImportType() const { return m_importName; }
704
705void GenericType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700706 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700707 fprintf(stderr, "implement GenericType::WriteToParcel\n");
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700708}
709
Christopher Wileye6dee912015-09-22 14:50:23 -0700710void GenericType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700711 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700712 fprintf(stderr, "implement GenericType::CreateFromParcel\n");
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700713}
714
Christopher Wileye6dee912015-09-22 14:50:23 -0700715void GenericType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700716 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700717 fprintf(stderr, "implement GenericType::ReadFromParcel\n");
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700718}
719
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700720// ================================================================
721
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700722GenericListType::GenericListType(const JavaTypeNamespace* types,
723 const string& package, const string& name,
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700724 const vector<const Type*>& args)
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700725 : GenericType(types, package, name, args),
726 m_creator(args[0]->CreatorName()) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700727
728string GenericListType::CreatorName() const {
729 return "android.os.Parcel.arrayListCreator";
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700730}
731
Christopher Wileye6dee912015-09-22 14:50:23 -0700732string GenericListType::InstantiableName() const {
733 return "java.util.ArrayList" + GenericArguments();
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700734}
735
Christopher Wileye6dee912015-09-22 14:50:23 -0700736void GenericListType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700737 Variable* parcel, int flags) const {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700738 if (m_creator == m_types->StringType()->CreatorName()) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700739 addTo->Add(new MethodCall(parcel, "writeStringList", 1, v));
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700740 } else if (m_creator == m_types->IBinderType()->CreatorName()) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700741 addTo->Add(new MethodCall(parcel, "writeBinderList", 1, v));
742 } else {
743 // parcel.writeTypedListXX(arg);
744 addTo->Add(new MethodCall(parcel, "writeTypedList", 1, v));
745 }
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700746}
747
Christopher Wileye6dee912015-09-22 14:50:23 -0700748void GenericListType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700749 Variable* parcel, Variable**) const {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700750 if (m_creator == m_types->StringType()->CreatorName()) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700751 addTo->Add(
752 new Assignment(v, new MethodCall(parcel, "createStringArrayList", 0)));
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700753 } else if (m_creator == m_types->IBinderType()->CreatorName()) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700754 addTo->Add(
755 new Assignment(v, new MethodCall(parcel, "createBinderArrayList", 0)));
756 } else {
757 // v = _data.readTypedArrayList(XXX.creator);
758 addTo->Add(
759 new Assignment(v, new MethodCall(parcel, "createTypedArrayList", 1,
760 new LiteralExpression(m_creator))));
761 }
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700762}
763
Christopher Wileye6dee912015-09-22 14:50:23 -0700764void GenericListType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700765 Variable* parcel, Variable**) const {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700766 if (m_creator == m_types->StringType()->CreatorName()) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700767 addTo->Add(new MethodCall(parcel, "readStringList", 1, v));
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700768 } else if (m_creator == m_types->IBinderType()->CreatorName()) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700769 addTo->Add(new MethodCall(parcel, "readBinderList", 1, v));
770 } else {
771 // v = _data.readTypedList(v, XXX.creator);
772 addTo->Add(new MethodCall(parcel, "readTypedList", 2, v,
773 new LiteralExpression(m_creator)));
774 }
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700775}
776
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700777// ================================================================
778
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700779ClassLoaderType::ClassLoaderType(const JavaTypeNamespace* types)
780 : Type(types, "java.lang", "ClassLoader", BUILT_IN, false, false) {}
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700781
782// ================================================================
783
Christopher Wiley4582ecb2015-09-25 13:27:45 -0700784JavaTypeNamespace::ContainerClass::ContainerClass(const string& package,
785 const string& class_name,
786 size_t nargs)
787 : package(package),
788 class_name(class_name),
789 canonical_name(package + "." + class_name),
790 args(nargs) {
791}
792
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700793JavaTypeNamespace::JavaTypeNamespace() {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700794 Add(new BasicType(this, "void", "XXX", "XXX", "XXX", "XXX", "XXX"));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700795
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700796 m_bool_type = new BooleanType(this);
797 Add(m_bool_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700798
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700799 Add(new BasicType(this, "byte", "writeByte", "readByte", "writeByteArray",
800 "createByteArray", "readByteArray"));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700801
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700802 Add(new CharType(this));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700803
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700804 m_int_type = new BasicType(this, "int", "writeInt", "readInt",
805 "writeIntArray", "createIntArray", "readIntArray");
806 Add(m_int_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700807
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700808 Add(new BasicType(this, "long", "writeLong", "readLong", "writeLongArray",
809 "createLongArray", "readLongArray"));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700810
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700811 Add(new BasicType(this, "float", "writeFloat", "readFloat", "writeFloatArray",
812 "createFloatArray", "readFloatArray"));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700813
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700814 Add(new BasicType(this, "double", "writeDouble", "readDouble",
815 "writeDoubleArray", "createDoubleArray",
816 "readDoubleArray"));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700817
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700818 m_string_type = new class StringType(this);
819 Add(m_string_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700820
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700821 Add(new Type(this, "java.lang", "Object", Type::BUILT_IN, false, false));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700822
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700823 Add(new CharSequenceType(this));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700824
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700825 Add(new MapType(this));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700826
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700827 Add(new ListType(this));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700828
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700829 m_text_utils_type =
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700830 new Type(this, "android.text", "TextUtils", Type::BUILT_IN, false, false);
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700831 Add(m_text_utils_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700832
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700833 m_remote_exception_type = new class RemoteExceptionType(this);
834 Add(m_remote_exception_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700835
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700836 m_runtime_exception_type = new class RuntimeExceptionType(this);
837 Add(m_runtime_exception_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700838
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700839 m_ibinder_type = new class IBinderType(this);
840 Add(m_ibinder_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700841
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700842 m_iinterface_type = new class IInterfaceType(this);
843 Add(m_iinterface_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700844
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700845 m_binder_native_type = new class BinderType(this);
846 Add(m_binder_native_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700847
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700848 m_binder_proxy_type = new class BinderProxyType(this);
849 Add(m_binder_proxy_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700850
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700851 m_parcel_type = new class ParcelType(this);
852 Add(m_parcel_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700853
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700854 m_parcelable_interface_type = new class ParcelableInterfaceType(this);
855 Add(m_parcelable_interface_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700856
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700857 m_context_type = new class Type(this, "android.content", "Context",
858 Type::BUILT_IN, false, false);
859 Add(m_context_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700860
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700861 m_classloader_type = new class ClassLoaderType(this);
862 Add(m_classloader_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700863
864 NULL_VALUE = new LiteralExpression("null");
865 THIS_VALUE = new LiteralExpression("this");
866 SUPER_VALUE = new LiteralExpression("super");
867 TRUE_VALUE = new LiteralExpression("true");
868 FALSE_VALUE = new LiteralExpression("false");
869
Christopher Wiley4582ecb2015-09-25 13:27:45 -0700870 m_containers.emplace_back("java.util", "List", 1);
871 m_containers.emplace_back("java.util", "Map", 2);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700872}
Christopher Wileye6dee912015-09-22 14:50:23 -0700873
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700874JavaTypeNamespace::~JavaTypeNamespace() {
Christopher Wileye6dee912015-09-22 14:50:23 -0700875 int N = m_types.size();
876 for (int i = 0; i < N; i++) {
877 delete m_types[i];
878 }
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700879}
880
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700881bool JavaTypeNamespace::Add(const Type* type) {
882 const Type* existing = Find(type->QualifiedName());
883 if (!existing) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700884 m_types.push_back(type);
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700885 return true;
Christopher Wileye6dee912015-09-22 14:50:23 -0700886 }
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700887
888 if (existing->Kind() == Type::BUILT_IN) {
889 fprintf(stderr, "%s:%d attempt to redefine built in class %s\n",
890 type->DeclFile().c_str(), type->DeclLine(),
891 type->QualifiedName().c_str());
892 return false;
893 }
894
895 if (type->Kind() != existing->Kind()) {
896 fprintf(stderr, "%s:%d attempt to redefine %s as %s,\n",
897 type->DeclFile().c_str(), type->DeclLine(),
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700898 type->QualifiedName().c_str(), type->HumanReadableKind().c_str());
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700899 fprintf(stderr, "%s:%d previously defined here as %s.\n",
900 existing->DeclFile().c_str(), existing->DeclLine(),
901 existing->HumanReadableKind().c_str());
902 return false;
903 }
904
905 return true;
Christopher Wileye6dee912015-09-22 14:50:23 -0700906}
907
Christopher Wiley4582ecb2015-09-25 13:27:45 -0700908const Type* JavaTypeNamespace::Find(const string& unstripped_name) const {
909 const ContainerClass* g = nullptr;
910 vector<const Type*> template_arg_types;
911 if (!CanonicalizeContainerClass(unstripped_name, &g, &template_arg_types)) {
912 LOG(ERROR) << "Error canonicalizing type '" << unstripped_name << "'";
913 return nullptr;
914 }
915
916 string name = Trim(unstripped_name);
917 if (g != nullptr) {
918 vector<string> template_args;
919 for (const Type* type : template_arg_types) {
920 template_args.push_back(type->QualifiedName());
921 }
922 name = g->canonical_name + "<" + Join(template_args, ',') + ">";
923 }
924
925 // Always prefer a exact match if possible.
926 // This works for primitives and class names qualified with a package.
927 for (const Type* type : m_types) {
928 if (type->QualifiedName() == name) {
929 return type;
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700930 }
Christopher Wileye6dee912015-09-22 14:50:23 -0700931 }
Christopher Wiley4582ecb2015-09-25 13:27:45 -0700932
933 // We allow authors to drop packages when refering to a class name.
934 // our language doesn't allow you to not specify outer classes
935 // when referencing an inner class. that could be changed, and this
936 // would be the place to do it, but I don't think the complexity in
937 // scoping rules is worth it.
938 for (const Type* type : m_types) {
939 if (type->Name() == name) {
940 return type;
941 }
942 }
943
944 return nullptr;
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700945}
946
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700947const Type* JavaTypeNamespace::Find(const char* package,
948 const char* name) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700949 string s;
950 if (package != nullptr && *package != '\0') {
951 s += package;
952 s += '.';
953 }
954 s += name;
955 return Find(s);
956}
957
Christopher Wiley4582ecb2015-09-25 13:27:45 -0700958bool JavaTypeNamespace::AddParcelableType(const user_data_type* p,
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700959 const std::string& filename) {
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700960 Type* type =
961 new UserDataType(this, p->package ? p->package : "", p->name.data, false,
962 p->parcelable, filename, p->name.lineno);
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700963 return Add(type);
964}
965
Christopher Wiley4582ecb2015-09-25 13:27:45 -0700966bool JavaTypeNamespace::AddBinderType(const interface_type* b,
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700967 const std::string& filename) {
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700968 // for interfaces, add the stub, proxy, and interface types.
969 Type* type =
970 new InterfaceType(this, b->package ? b->package : "", b->name.data, false,
971 b->oneway, filename, b->name.lineno);
972 Type* stub = new Type(this, b->package ? b->package : "",
973 string{b->name.data} + ".Stub", Type::GENERATED, false,
974 false, filename, b->name.lineno);
975 Type* proxy = new Type(this, b->package ? b->package : "",
976 string{b->name.data} + ".Stub.Proxy", Type::GENERATED,
977 false, false, filename, b->name.lineno);
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700978
979 bool success = true;
980 success &= Add(type);
981 success &= Add(stub);
982 success &= Add(proxy);
983 return success;
984}
985
Christopher Wiley4582ecb2015-09-25 13:27:45 -0700986bool JavaTypeNamespace::AddContainerType(const string& type_name) {
987 if (Find(type_name) != nullptr) {
988 return true; // Don't add duplicates of the same templated type.
Christopher Wileye6dee912015-09-22 14:50:23 -0700989 }
990
Christopher Wiley4582ecb2015-09-25 13:27:45 -0700991 const ContainerClass* g = nullptr;
992 vector<const Type*> template_arg_types;
993 if (!CanonicalizeContainerClass(type_name, &g, &template_arg_types)) {
994 LOG(ERROR) << "Error canonicalizing type '" << type_name << "'";
995 return false;
Christopher Wileye6dee912015-09-22 14:50:23 -0700996 }
997
Christopher Wiley4582ecb2015-09-25 13:27:45 -0700998 if (g == nullptr) {
999 // We parsed the type correctly, but it wasn't a container type. No error.
1000 return true;
Christopher Wileye6dee912015-09-22 14:50:23 -07001001 }
1002
Christopher Wiley4582ecb2015-09-25 13:27:45 -07001003 // construct an instance of a container type, add it to our name set so they
1004 // always get the same object, and return it.
1005 Type* result = nullptr;
1006 if (g->canonical_name == "java.util.List" &&
1007 template_arg_types.size() == 1u) {
1008 result = new GenericListType(this, g->package, g->class_name,
1009 template_arg_types);
1010 } else {
1011 LOG(ERROR) << "Don't know how to create a container of type "
1012 << g->canonical_name << " with " << template_arg_types.size()
1013 << " arguments.";
1014 return false;
Christopher Wileye6dee912015-09-22 14:50:23 -07001015 }
1016
Christopher Wiley60b02ae2015-09-23 16:35:18 -07001017 Add(result);
Christopher Wiley4582ecb2015-09-25 13:27:45 -07001018 return true;
The Android Open Source Project46c012c2008-10-21 07:00:00 -07001019}
1020
Christopher Wileyfb4b22d2015-09-25 15:16:13 -07001021const ValidatableType* JavaTypeNamespace::GetValidatableType(
1022 const string& name) const {
1023 return Find(name);
1024}
1025
Christopher Wiley4582ecb2015-09-25 13:27:45 -07001026const JavaTypeNamespace::ContainerClass* JavaTypeNamespace::FindContainerClass(
1027 const string& name,
1028 size_t nargs) const {
1029 // first check fully qualified class names (with packages).
1030 for (const ContainerClass& container : m_containers) {
1031 if (container.canonical_name == name && nargs == container.args) {
1032 return &container;
The Android Open Source Project46c012c2008-10-21 07:00:00 -07001033 }
Christopher Wileye6dee912015-09-22 14:50:23 -07001034 }
1035
Christopher Wiley4582ecb2015-09-25 13:27:45 -07001036 // then match on the class name alone (no package).
1037 for (const ContainerClass& container : m_containers) {
1038 if (container.class_name == name && nargs == container.args) {
1039 return &container;
Christopher Wileye6dee912015-09-22 14:50:23 -07001040 }
1041 }
1042
Christopher Wiley4582ecb2015-09-25 13:27:45 -07001043 return nullptr;
1044}
1045
1046bool JavaTypeNamespace::CanonicalizeContainerClass(
1047 const string& raw_name,
1048 const ContainerClass** container_class,
1049 vector<const Type*>* arg_types) const {
1050 string name = Trim(raw_name);
1051 const size_t opening_brace = name.find('<');
1052 if (opening_brace == name.npos) {
1053 // Not a template type and not an error.
1054 *container_class = nullptr;
1055 arg_types->clear();
1056 return true;
1057 }
1058
1059 const size_t closing_brace = name.find('>');
1060 if (opening_brace != name.rfind('<') ||
1061 closing_brace != name.rfind('>') ||
1062 closing_brace != name.length() - 1) {
1063 LOG(ERROR) << "Invalid template type '" << name << "'";
1064 // Nested/invalid templates are forbidden.
1065 return false;
1066 }
1067
1068 string container_class_name = Trim(name.substr(0, opening_brace));
1069 string remainder = name.substr(opening_brace + 1,
1070 (closing_brace - opening_brace) - 1);
1071 vector<string> template_args = Split(remainder, ",");
1072
1073 const ContainerClass* g =
1074 FindContainerClass(container_class_name, template_args.size());
1075 if (g == nullptr) {
1076 LOG(ERROR) << "Failed to find templated container '"
1077 << container_class_name << "'";
1078 return false;
1079 }
1080
1081 vector<const Type*> template_arg_types;
1082 for (auto& template_arg : template_args) {
1083 // Recursively search for the contained types.
1084 const Type* template_arg_type = Find(Trim(template_arg));
1085 if (template_arg_type == nullptr) {
1086 LOG(ERROR) << "Failed to find formal type of '"
1087 << template_arg << "'";
1088 return false;
1089 }
1090 template_arg_types.push_back(template_arg_type);
1091 }
1092
1093 *arg_types = template_arg_types;
1094 *container_class = g;
1095 return true;
The Android Open Source Project46c012c2008-10-21 07:00:00 -07001096}
1097
Christopher Wiley84c1eac2015-09-23 13:29:28 -07001098void JavaTypeNamespace::Dump() const {
Christopher Wileye6dee912015-09-22 14:50:23 -07001099 int n = m_types.size();
1100 for (int i = 0; i < n; i++) {
Christopher Wiley8f6816e2015-09-22 17:03:47 -07001101 const Type* t = m_types[i];
Christopher Wileye6dee912015-09-22 14:50:23 -07001102 printf("type: package=%s name=%s qualifiedName=%s\n", t->Package().c_str(),
1103 t->Name().c_str(), t->QualifiedName().c_str());
1104 }
The Android Open Source Project46c012c2008-10-21 07:00:00 -07001105}
Christopher Wileyfdeb0f42015-09-11 15:38:22 -07001106
Christopher Wileydb154a52015-09-28 16:32:25 -07001107} // namespace java
Christopher Wileyfdeb0f42015-09-11 15:38:22 -07001108} // namespace aidl
1109} // namespace android