blob: a953dd98f3f0a381f9e033f77c33ff36ce2ed344 [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
Christopher Wileye6dee912015-09-22 14:50:23 -070088string Type::CreatorName() const { return ""; }
89
90string Type::InstantiableName() const { return QualifiedName(); }
91
92void Type::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel,
Christopher Wiley3637a4d2015-09-22 15:37:59 -070093 int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -070094 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%sn", __FILE__,
95 __LINE__, m_qualifiedName.c_str());
96 addTo->Add(new LiteralExpression("/* WriteToParcel error " + m_qualifiedName +
97 " */"));
Christopher Wileyf690be52015-09-14 15:19:10 -070098}
99
Christopher Wileye6dee912015-09-22 14:50:23 -0700100void Type::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700101 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700102 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n", __FILE__,
103 __LINE__, m_qualifiedName.c_str());
104 addTo->Add(new LiteralExpression("/* CreateFromParcel error " +
105 m_qualifiedName + " */"));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700106}
107
Christopher Wileye6dee912015-09-22 14:50:23 -0700108void Type::ReadFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700109 Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700110 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n", __FILE__,
111 __LINE__, m_qualifiedName.c_str());
112 addTo->Add(new LiteralExpression("/* ReadFromParcel error " +
113 m_qualifiedName + " */"));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700114}
115
Christopher Wileye6dee912015-09-22 14:50:23 -0700116void Type::WriteArrayToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700117 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700118 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n", __FILE__,
119 __LINE__, m_qualifiedName.c_str());
120 addTo->Add(new LiteralExpression("/* WriteArrayToParcel error " +
121 m_qualifiedName + " */"));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700122}
123
Christopher Wileye6dee912015-09-22 14:50:23 -0700124void Type::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700125 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700126 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n", __FILE__,
127 __LINE__, m_qualifiedName.c_str());
128 addTo->Add(new LiteralExpression("/* CreateArrayFromParcel error " +
129 m_qualifiedName + " */"));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700130}
131
Christopher Wileye6dee912015-09-22 14:50:23 -0700132void Type::ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700133 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700134 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n", __FILE__,
135 __LINE__, m_qualifiedName.c_str());
136 addTo->Add(new LiteralExpression("/* ReadArrayFromParcel error " +
137 m_qualifiedName + " */"));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700138}
139
Christopher Wileye6dee912015-09-22 14:50:23 -0700140void Type::SetQualifiedName(const string& qualified) {
141 m_qualifiedName = qualified;
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700142}
143
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700144Expression* Type::BuildWriteToParcelFlags(int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700145 if (flags == 0) {
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700146 return new LiteralExpression("0");
Christopher Wileye6dee912015-09-22 14:50:23 -0700147 }
148 if ((flags & PARCELABLE_WRITE_RETURN_VALUE) != 0) {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700149 return new FieldVariable(m_types->ParcelableInterfaceType(),
Christopher Wileye6dee912015-09-22 14:50:23 -0700150 "PARCELABLE_WRITE_RETURN_VALUE");
151 }
152 return new LiteralExpression("0");
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700153}
154
155// ================================================================
156
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700157BasicType::BasicType(const JavaTypeNamespace* types, const string& name,
158 const string& marshallParcel,
Christopher Wileye6dee912015-09-22 14:50:23 -0700159 const string& unmarshallParcel,
160 const string& writeArrayParcel,
161 const string& createArrayParcel,
162 const string& readArrayParcel)
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700163 : Type(types, name, BUILT_IN, true, false),
Christopher Wileye6dee912015-09-22 14:50:23 -0700164 m_marshallParcel(marshallParcel),
165 m_unmarshallParcel(unmarshallParcel),
166 m_writeArrayParcel(writeArrayParcel),
167 m_createArrayParcel(createArrayParcel),
168 m_readArrayParcel(readArrayParcel) {}
169
170void BasicType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700171 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700172 addTo->Add(new MethodCall(parcel, m_marshallParcel, 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700173}
174
Christopher Wileye6dee912015-09-22 14:50:23 -0700175void BasicType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700176 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700177 addTo->Add(new Assignment(v, new MethodCall(parcel, m_unmarshallParcel)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700178}
179
Christopher Wileye6dee912015-09-22 14:50:23 -0700180bool BasicType::CanBeArray() const { return true; }
181
182void BasicType::WriteArrayToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700183 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700184 addTo->Add(new MethodCall(parcel, m_writeArrayParcel, 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700185}
186
Christopher Wileye6dee912015-09-22 14:50:23 -0700187void BasicType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700188 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700189 addTo->Add(new Assignment(v, new MethodCall(parcel, m_createArrayParcel)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700190}
191
Christopher Wileye6dee912015-09-22 14:50:23 -0700192void BasicType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700193 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700194 addTo->Add(new MethodCall(parcel, m_readArrayParcel, 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700195}
196
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700197// ================================================================
198
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700199BooleanType::BooleanType(const JavaTypeNamespace* types)
200 : Type(types, "boolean", BUILT_IN, true, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700201
202void BooleanType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700203 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700204 addTo->Add(new MethodCall(
205 parcel, "writeInt", 1,
206 new Ternary(v, new LiteralExpression("1"), new LiteralExpression("0"))));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700207}
208
Christopher Wileye6dee912015-09-22 14:50:23 -0700209void BooleanType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700210 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700211 addTo->Add(
212 new Assignment(v, new Comparison(new LiteralExpression("0"), "!=",
213 new MethodCall(parcel, "readInt"))));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700214}
215
Christopher Wileye6dee912015-09-22 14:50:23 -0700216bool BooleanType::CanBeArray() const { return true; }
217
218void BooleanType::WriteArrayToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700219 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700220 addTo->Add(new MethodCall(parcel, "writeBooleanArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700221}
222
Christopher Wileye6dee912015-09-22 14:50:23 -0700223void BooleanType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700224 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700225 addTo->Add(new Assignment(v, new MethodCall(parcel, "createBooleanArray")));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700226}
227
Christopher Wileye6dee912015-09-22 14:50:23 -0700228void BooleanType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700229 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700230 addTo->Add(new MethodCall(parcel, "readBooleanArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700231}
232
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700233// ================================================================
234
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700235CharType::CharType(const JavaTypeNamespace* types)
236 : Type(types, "char", BUILT_IN, true, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700237
238void CharType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700239 Variable* parcel, int flags) const {
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700240 addTo->Add(
241 new MethodCall(parcel, "writeInt", 1, new Cast(m_types->IntType(), v)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700242}
243
Christopher Wileye6dee912015-09-22 14:50:23 -0700244void CharType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700245 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700246 addTo->Add(new Assignment(v, new MethodCall(parcel, "readInt"), this));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700247}
248
Christopher Wileye6dee912015-09-22 14:50:23 -0700249bool CharType::CanBeArray() const { return true; }
250
251void CharType::WriteArrayToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700252 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700253 addTo->Add(new MethodCall(parcel, "writeCharArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700254}
255
Christopher Wileye6dee912015-09-22 14:50:23 -0700256void CharType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700257 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700258 addTo->Add(new Assignment(v, new MethodCall(parcel, "createCharArray")));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700259}
260
Christopher Wileye6dee912015-09-22 14:50:23 -0700261void CharType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700262 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700263 addTo->Add(new MethodCall(parcel, "readCharArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700264}
265
266// ================================================================
267
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700268StringType::StringType(const JavaTypeNamespace* types)
269 : Type(types, "java.lang", "String", BUILT_IN, true, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700270
271string StringType::CreatorName() const {
272 return "android.os.Parcel.STRING_CREATOR";
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700273}
274
Christopher Wileye6dee912015-09-22 14:50:23 -0700275void StringType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700276 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700277 addTo->Add(new MethodCall(parcel, "writeString", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700278}
279
Christopher Wileye6dee912015-09-22 14:50:23 -0700280void StringType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700281 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700282 addTo->Add(new Assignment(v, new MethodCall(parcel, "readString")));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700283}
284
Christopher Wileye6dee912015-09-22 14:50:23 -0700285bool StringType::CanBeArray() const { return true; }
286
287void StringType::WriteArrayToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700288 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700289 addTo->Add(new MethodCall(parcel, "writeStringArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700290}
291
Christopher Wileye6dee912015-09-22 14:50:23 -0700292void StringType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700293 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700294 addTo->Add(new Assignment(v, new MethodCall(parcel, "createStringArray")));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700295}
296
Christopher Wileye6dee912015-09-22 14:50:23 -0700297void StringType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700298 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700299 addTo->Add(new MethodCall(parcel, "readStringArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700300}
301
302// ================================================================
303
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700304CharSequenceType::CharSequenceType(const JavaTypeNamespace* types)
305 : Type(types, "java.lang", "CharSequence", BUILT_IN, true, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700306
307string CharSequenceType::CreatorName() const {
308 return "android.os.Parcel.STRING_CREATOR";
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700309}
310
Christopher Wileye6dee912015-09-22 14:50:23 -0700311void CharSequenceType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700312 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700313 // if (v != null) {
314 // parcel.writeInt(1);
315 // v.writeToParcel(parcel);
316 // } else {
317 // parcel.writeInt(0);
318 // }
319 IfStatement* elsepart = new IfStatement();
320 elsepart->statements->Add(
321 new MethodCall(parcel, "writeInt", 1, new LiteralExpression("0")));
322 IfStatement* ifpart = new IfStatement;
323 ifpart->expression = new Comparison(v, "!=", NULL_VALUE);
324 ifpart->elseif = elsepart;
325 ifpart->statements->Add(
326 new MethodCall(parcel, "writeInt", 1, new LiteralExpression("1")));
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700327 ifpart->statements->Add(new MethodCall(m_types->TextUtilsType(),
328 "writeToParcel", 3, v, parcel,
Christopher Wileye6dee912015-09-22 14:50:23 -0700329 BuildWriteToParcelFlags(flags)));
330
331 addTo->Add(ifpart);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700332}
333
Christopher Wileye6dee912015-09-22 14:50:23 -0700334void CharSequenceType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700335 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700336 // if (0 != parcel.readInt()) {
337 // v = TextUtils.createFromParcel(parcel)
338 // } else {
339 // v = null;
340 // }
341 IfStatement* elsepart = new IfStatement();
342 elsepart->statements->Add(new Assignment(v, NULL_VALUE));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700343
Christopher Wileye6dee912015-09-22 14:50:23 -0700344 IfStatement* ifpart = new IfStatement();
345 ifpart->expression = new Comparison(new LiteralExpression("0"), "!=",
346 new MethodCall(parcel, "readInt"));
347 ifpart->elseif = elsepart;
348 ifpart->statements->Add(new Assignment(
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700349 v, new MethodCall(m_types->TextUtilsType(),
Christopher Wileye6dee912015-09-22 14:50:23 -0700350 "CHAR_SEQUENCE_CREATOR.createFromParcel", 1, parcel)));
351
352 addTo->Add(ifpart);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700353}
354
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700355// ================================================================
356
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700357RemoteExceptionType::RemoteExceptionType(const JavaTypeNamespace* types)
358 : Type(types, "android.os", "RemoteException", BUILT_IN, false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700359
360void RemoteExceptionType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700361 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700362 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700363}
364
Christopher Wileye6dee912015-09-22 14:50:23 -0700365void RemoteExceptionType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700366 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700367 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700368}
369
370// ================================================================
371
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700372RuntimeExceptionType::RuntimeExceptionType(const JavaTypeNamespace* types)
373 : Type(types, "java.lang", "RuntimeException", BUILT_IN, false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700374
375void RuntimeExceptionType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700376 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700377 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700378}
379
Christopher Wileye6dee912015-09-22 14:50:23 -0700380void RuntimeExceptionType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700381 Variable* parcel,
382 Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700383 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700384}
385
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700386// ================================================================
387
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700388IBinderType::IBinderType(const JavaTypeNamespace* types)
389 : Type(types, "android.os", "IBinder", BUILT_IN, true, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700390
391void IBinderType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700392 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700393 addTo->Add(new MethodCall(parcel, "writeStrongBinder", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700394}
395
Christopher Wileye6dee912015-09-22 14:50:23 -0700396void IBinderType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700397 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700398 addTo->Add(new Assignment(v, new MethodCall(parcel, "readStrongBinder")));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700399}
400
Christopher Wileye6dee912015-09-22 14:50:23 -0700401void IBinderType::WriteArrayToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700402 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700403 addTo->Add(new MethodCall(parcel, "writeBinderArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700404}
405
Christopher Wileye6dee912015-09-22 14:50:23 -0700406void IBinderType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700407 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700408 addTo->Add(new Assignment(v, new MethodCall(parcel, "createBinderArray")));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700409}
410
Christopher Wileye6dee912015-09-22 14:50:23 -0700411void IBinderType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700412 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700413 addTo->Add(new MethodCall(parcel, "readBinderArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700414}
415
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700416// ================================================================
417
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700418IInterfaceType::IInterfaceType(const JavaTypeNamespace* types)
419 : Type(types, "android.os", "IInterface", BUILT_IN, false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700420
421void IInterfaceType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700422 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700423 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700424}
425
Christopher Wileye6dee912015-09-22 14:50:23 -0700426void IInterfaceType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700427 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700428 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700429}
430
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700431// ================================================================
432
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700433BinderType::BinderType(const JavaTypeNamespace* types)
434 : Type(types, "android.os", "Binder", BUILT_IN, false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700435
436void BinderType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700437 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700438 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700439}
440
Christopher Wileye6dee912015-09-22 14:50:23 -0700441void BinderType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700442 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700443 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700444}
445
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700446// ================================================================
447
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700448BinderProxyType::BinderProxyType(const JavaTypeNamespace* types)
449 : Type(types, "android.os", "BinderProxy", BUILT_IN, false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700450
451void BinderProxyType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700452 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700453 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700454}
455
Christopher Wileye6dee912015-09-22 14:50:23 -0700456void BinderProxyType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700457 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700458 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700459}
460
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700461// ================================================================
462
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700463ParcelType::ParcelType(const JavaTypeNamespace* types)
464 : Type(types, "android.os", "Parcel", BUILT_IN, false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700465
466void ParcelType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700467 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700468 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700469}
470
Christopher Wileye6dee912015-09-22 14:50:23 -0700471void ParcelType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700472 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700473 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700474}
475
476// ================================================================
477
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700478ParcelableInterfaceType::ParcelableInterfaceType(const JavaTypeNamespace* types)
479 : Type(types, "android.os", "Parcelable", BUILT_IN, false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700480
481void ParcelableInterfaceType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700482 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700483 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700484}
485
Christopher Wileye6dee912015-09-22 14:50:23 -0700486void ParcelableInterfaceType::CreateFromParcel(StatementBlock* addTo,
487 Variable* v, Variable* parcel,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700488 Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700489 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700490}
491
492// ================================================================
493
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700494MapType::MapType(const JavaTypeNamespace* types)
495 : Type(types, "java.util", "Map", BUILT_IN, true, true) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700496
497void MapType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700498 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700499 addTo->Add(new MethodCall(parcel, "writeMap", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700500}
501
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700502static void EnsureClassLoader(StatementBlock* addTo, Variable** cl,
503 const JavaTypeNamespace* types) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700504 // We don't want to look up the class loader once for every
505 // collection argument, so ensure we do it at most once per method.
506 if (*cl == NULL) {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700507 *cl = new Variable(types->ClassLoaderType(), "cl");
Christopher Wileye6dee912015-09-22 14:50:23 -0700508 addTo->Add(new VariableDeclaration(
509 *cl, new LiteralExpression("this.getClass().getClassLoader()"),
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700510 types->ClassLoaderType()));
Christopher Wileye6dee912015-09-22 14:50:23 -0700511 }
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700512}
513
Christopher Wileye6dee912015-09-22 14:50:23 -0700514void MapType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700515 Variable* parcel, Variable** cl) const {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700516 EnsureClassLoader(addTo, cl, m_types);
Christopher Wileye6dee912015-09-22 14:50:23 -0700517 addTo->Add(new Assignment(v, new MethodCall(parcel, "readHashMap", 1, *cl)));
Elliott Hughes15f8da22011-07-13 12:10:30 -0700518}
519
Christopher Wileye6dee912015-09-22 14:50:23 -0700520void MapType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700521 Variable* parcel, Variable** cl) const {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700522 EnsureClassLoader(addTo, cl, m_types);
Christopher Wileye6dee912015-09-22 14:50:23 -0700523 addTo->Add(new MethodCall(parcel, "readMap", 2, v, *cl));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700524}
525
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700526// ================================================================
527
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700528ListType::ListType(const JavaTypeNamespace* types)
529 : Type(types, "java.util", "List", BUILT_IN, true, true) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700530
531string ListType::InstantiableName() const { return "java.util.ArrayList"; }
532
533void ListType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700534 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700535 addTo->Add(new MethodCall(parcel, "writeList", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700536}
537
Christopher Wileye6dee912015-09-22 14:50:23 -0700538void ListType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700539 Variable* parcel, Variable** cl) const {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700540 EnsureClassLoader(addTo, cl, m_types);
Christopher Wileye6dee912015-09-22 14:50:23 -0700541 addTo->Add(
542 new Assignment(v, new MethodCall(parcel, "readArrayList", 1, *cl)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700543}
544
Christopher Wileye6dee912015-09-22 14:50:23 -0700545void ListType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700546 Variable* parcel, Variable** cl) const {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700547 EnsureClassLoader(addTo, cl, m_types);
Christopher Wileye6dee912015-09-22 14:50:23 -0700548 addTo->Add(new MethodCall(parcel, "readList", 2, v, *cl));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700549}
550
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700551// ================================================================
552
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700553UserDataType::UserDataType(const JavaTypeNamespace* types,
554 const string& package, const string& name,
Christopher Wileye6dee912015-09-22 14:50:23 -0700555 bool builtIn, bool canWriteToParcel,
556 const string& declFile, int declLine)
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700557 : Type(types, package, name, builtIn ? BUILT_IN : USERDATA,
558 canWriteToParcel, true, declFile, declLine) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700559
560string UserDataType::CreatorName() const {
561 return QualifiedName() + ".CREATOR";
Joe Onorato44050522011-10-09 17:38:20 -0700562}
563
Christopher Wileye6dee912015-09-22 14:50:23 -0700564void UserDataType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700565 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700566 // if (v != null) {
567 // parcel.writeInt(1);
568 // v.writeToParcel(parcel);
569 // } else {
570 // parcel.writeInt(0);
571 // }
572 IfStatement* elsepart = new IfStatement();
573 elsepart->statements->Add(
574 new MethodCall(parcel, "writeInt", 1, new LiteralExpression("0")));
575 IfStatement* ifpart = new IfStatement;
576 ifpart->expression = new Comparison(v, "!=", NULL_VALUE);
577 ifpart->elseif = elsepart;
578 ifpart->statements->Add(
579 new MethodCall(parcel, "writeInt", 1, new LiteralExpression("1")));
580 ifpart->statements->Add(new MethodCall(v, "writeToParcel", 2, parcel,
581 BuildWriteToParcelFlags(flags)));
582
583 addTo->Add(ifpart);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700584}
585
Christopher Wileye6dee912015-09-22 14:50:23 -0700586void UserDataType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700587 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700588 // if (0 != parcel.readInt()) {
589 // v = CLASS.CREATOR.createFromParcel(parcel)
590 // } else {
591 // v = null;
592 // }
593 IfStatement* elsepart = new IfStatement();
594 elsepart->statements->Add(new Assignment(v, NULL_VALUE));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700595
Christopher Wileye6dee912015-09-22 14:50:23 -0700596 IfStatement* ifpart = new IfStatement();
597 ifpart->expression = new Comparison(new LiteralExpression("0"), "!=",
598 new MethodCall(parcel, "readInt"));
599 ifpart->elseif = elsepart;
600 ifpart->statements->Add(new Assignment(
601 v, new MethodCall(v->type, "CREATOR.createFromParcel", 1, parcel)));
602
603 addTo->Add(ifpart);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700604}
605
Christopher Wileye6dee912015-09-22 14:50:23 -0700606void UserDataType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700607 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700608 // TODO: really, we don't need to have this extra check, but we
609 // don't have two separate marshalling code paths
610 // if (0 != parcel.readInt()) {
611 // v.readFromParcel(parcel)
612 // }
613 IfStatement* ifpart = new IfStatement();
614 ifpart->expression = new Comparison(new LiteralExpression("0"), "!=",
615 new MethodCall(parcel, "readInt"));
616 ifpart->statements->Add(new MethodCall(v, "readFromParcel", 1, parcel));
617 addTo->Add(ifpart);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700618}
619
Christopher Wileye6dee912015-09-22 14:50:23 -0700620bool UserDataType::CanBeArray() const { return true; }
621
622void UserDataType::WriteArrayToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700623 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700624 addTo->Add(new MethodCall(parcel, "writeTypedArray", 2, v,
625 BuildWriteToParcelFlags(flags)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700626}
627
Christopher Wileye6dee912015-09-22 14:50:23 -0700628void UserDataType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700629 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700630 string creator = v->type->QualifiedName() + ".CREATOR";
631 addTo->Add(new Assignment(v, new MethodCall(parcel, "createTypedArray", 1,
632 new LiteralExpression(creator))));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700633}
634
Christopher Wileye6dee912015-09-22 14:50:23 -0700635void UserDataType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700636 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700637 string creator = v->type->QualifiedName() + ".CREATOR";
638 addTo->Add(new MethodCall(parcel, "readTypedArray", 2, v,
639 new LiteralExpression(creator)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700640}
641
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700642// ================================================================
643
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700644InterfaceType::InterfaceType(const JavaTypeNamespace* types,
645 const string& package, const string& name,
Christopher Wileye6dee912015-09-22 14:50:23 -0700646 bool builtIn, bool oneway, const string& declFile,
647 int declLine)
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700648 : Type(types, package, name, builtIn ? BUILT_IN : INTERFACE, true, false,
649 declFile, declLine),
Christopher Wileye6dee912015-09-22 14:50:23 -0700650 m_oneway(oneway) {}
651
652bool InterfaceType::OneWay() const { return m_oneway; }
653
654void InterfaceType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700655 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700656 // parcel.writeStrongBinder(v != null ? v.asBinder() : null);
657 addTo->Add(
658 new MethodCall(parcel, "writeStrongBinder", 1,
659 new Ternary(new Comparison(v, "!=", NULL_VALUE),
660 new MethodCall(v, "asBinder"), NULL_VALUE)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700661}
662
Christopher Wileye6dee912015-09-22 14:50:23 -0700663void InterfaceType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700664 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700665 // v = Interface.asInterface(parcel.readStrongBinder());
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700666 string stub_type = v->type->QualifiedName() + ".Stub";
Christopher Wileye6dee912015-09-22 14:50:23 -0700667 addTo->Add(new Assignment(
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700668 v, new MethodCall(m_types->Find(stub_type), "asInterface", 1,
Christopher Wileye6dee912015-09-22 14:50:23 -0700669 new MethodCall(parcel, "readStrongBinder"))));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700670}
671
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700672// ================================================================
673
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700674GenericType::GenericType(const JavaTypeNamespace* types, const string& package,
675 const string& name, const vector<const Type*>& args)
676 : Type(types, package, name, BUILT_IN, true, true) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700677 m_args = args;
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700678
Christopher Wileye6dee912015-09-22 14:50:23 -0700679 m_importName = package + '.' + name;
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700680
Christopher Wileye6dee912015-09-22 14:50:23 -0700681 string gen = "<";
682 int N = args.size();
683 for (int i = 0; i < N; i++) {
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700684 const Type* t = args[i];
Christopher Wileye6dee912015-09-22 14:50:23 -0700685 gen += t->QualifiedName();
686 if (i != N - 1) {
687 gen += ',';
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700688 }
Christopher Wileye6dee912015-09-22 14:50:23 -0700689 }
690 gen += '>';
691 m_genericArguments = gen;
692 SetQualifiedName(m_importName + gen);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700693}
694
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700695const vector<const Type*>& GenericType::GenericArgumentTypes() const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700696 return m_args;
Joe Onorato3d0e06f2011-09-02 15:28:36 -0700697}
698
Christopher Wileye6dee912015-09-22 14:50:23 -0700699string GenericType::GenericArguments() const { return m_genericArguments; }
700
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700701// ================================================================
702
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700703GenericListType::GenericListType(const JavaTypeNamespace* types,
704 const string& package, const string& name,
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700705 const vector<const Type*>& args)
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700706 : GenericType(types, package, name, args),
707 m_creator(args[0]->CreatorName()) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700708
709string GenericListType::CreatorName() const {
710 return "android.os.Parcel.arrayListCreator";
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700711}
712
Christopher Wileye6dee912015-09-22 14:50:23 -0700713string GenericListType::InstantiableName() const {
714 return "java.util.ArrayList" + GenericArguments();
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700715}
716
Christopher Wileye6dee912015-09-22 14:50:23 -0700717void GenericListType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700718 Variable* parcel, int flags) const {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700719 if (m_creator == m_types->StringType()->CreatorName()) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700720 addTo->Add(new MethodCall(parcel, "writeStringList", 1, v));
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700721 } else if (m_creator == m_types->IBinderType()->CreatorName()) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700722 addTo->Add(new MethodCall(parcel, "writeBinderList", 1, v));
723 } else {
724 // parcel.writeTypedListXX(arg);
725 addTo->Add(new MethodCall(parcel, "writeTypedList", 1, v));
726 }
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700727}
728
Christopher Wileye6dee912015-09-22 14:50:23 -0700729void GenericListType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700730 Variable* parcel, Variable**) const {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700731 if (m_creator == m_types->StringType()->CreatorName()) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700732 addTo->Add(
733 new Assignment(v, new MethodCall(parcel, "createStringArrayList", 0)));
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700734 } else if (m_creator == m_types->IBinderType()->CreatorName()) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700735 addTo->Add(
736 new Assignment(v, new MethodCall(parcel, "createBinderArrayList", 0)));
737 } else {
738 // v = _data.readTypedArrayList(XXX.creator);
739 addTo->Add(
740 new Assignment(v, new MethodCall(parcel, "createTypedArrayList", 1,
741 new LiteralExpression(m_creator))));
742 }
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700743}
744
Christopher Wileye6dee912015-09-22 14:50:23 -0700745void GenericListType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700746 Variable* parcel, Variable**) const {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700747 if (m_creator == m_types->StringType()->CreatorName()) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700748 addTo->Add(new MethodCall(parcel, "readStringList", 1, v));
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700749 } else if (m_creator == m_types->IBinderType()->CreatorName()) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700750 addTo->Add(new MethodCall(parcel, "readBinderList", 1, v));
751 } else {
752 // v = _data.readTypedList(v, XXX.creator);
753 addTo->Add(new MethodCall(parcel, "readTypedList", 2, v,
754 new LiteralExpression(m_creator)));
755 }
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700756}
757
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700758// ================================================================
759
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700760ClassLoaderType::ClassLoaderType(const JavaTypeNamespace* types)
761 : Type(types, "java.lang", "ClassLoader", BUILT_IN, false, false) {}
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700762
763// ================================================================
764
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700765JavaTypeNamespace::JavaTypeNamespace() {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700766 Add(new BasicType(this, "void", "XXX", "XXX", "XXX", "XXX", "XXX"));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700767
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700768 m_bool_type = new BooleanType(this);
769 Add(m_bool_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700770
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700771 Add(new BasicType(this, "byte", "writeByte", "readByte", "writeByteArray",
772 "createByteArray", "readByteArray"));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700773
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700774 Add(new CharType(this));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700775
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700776 m_int_type = new BasicType(this, "int", "writeInt", "readInt",
777 "writeIntArray", "createIntArray", "readIntArray");
778 Add(m_int_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700779
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700780 Add(new BasicType(this, "long", "writeLong", "readLong", "writeLongArray",
781 "createLongArray", "readLongArray"));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700782
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700783 Add(new BasicType(this, "float", "writeFloat", "readFloat", "writeFloatArray",
784 "createFloatArray", "readFloatArray"));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700785
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700786 Add(new BasicType(this, "double", "writeDouble", "readDouble",
787 "writeDoubleArray", "createDoubleArray",
788 "readDoubleArray"));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700789
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700790 m_string_type = new class StringType(this);
791 Add(m_string_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700792
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700793 Add(new Type(this, "java.lang", "Object", Type::BUILT_IN, false, false));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700794
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700795 Add(new CharSequenceType(this));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700796
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700797 Add(new MapType(this));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700798
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700799 Add(new ListType(this));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700800
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700801 m_text_utils_type =
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700802 new Type(this, "android.text", "TextUtils", Type::BUILT_IN, false, false);
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700803 Add(m_text_utils_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700804
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700805 m_remote_exception_type = new class RemoteExceptionType(this);
806 Add(m_remote_exception_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700807
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700808 m_runtime_exception_type = new class RuntimeExceptionType(this);
809 Add(m_runtime_exception_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700810
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700811 m_ibinder_type = new class IBinderType(this);
812 Add(m_ibinder_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700813
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700814 m_iinterface_type = new class IInterfaceType(this);
815 Add(m_iinterface_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700816
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700817 m_binder_native_type = new class BinderType(this);
818 Add(m_binder_native_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700819
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700820 m_binder_proxy_type = new class BinderProxyType(this);
821 Add(m_binder_proxy_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700822
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700823 m_parcel_type = new class ParcelType(this);
824 Add(m_parcel_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700825
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700826 m_parcelable_interface_type = new class ParcelableInterfaceType(this);
827 Add(m_parcelable_interface_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700828
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700829 m_context_type = new class Type(this, "android.content", "Context",
830 Type::BUILT_IN, false, false);
831 Add(m_context_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700832
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700833 m_classloader_type = new class ClassLoaderType(this);
834 Add(m_classloader_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700835
836 NULL_VALUE = new LiteralExpression("null");
837 THIS_VALUE = new LiteralExpression("this");
838 SUPER_VALUE = new LiteralExpression("super");
839 TRUE_VALUE = new LiteralExpression("true");
840 FALSE_VALUE = new LiteralExpression("false");
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700841}
Christopher Wileye6dee912015-09-22 14:50:23 -0700842
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700843JavaTypeNamespace::~JavaTypeNamespace() {
Christopher Wileye6dee912015-09-22 14:50:23 -0700844 int N = m_types.size();
845 for (int i = 0; i < N; i++) {
846 delete m_types[i];
847 }
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700848}
849
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700850bool JavaTypeNamespace::Add(const Type* type) {
851 const Type* existing = Find(type->QualifiedName());
852 if (!existing) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700853 m_types.push_back(type);
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700854 return true;
Christopher Wileye6dee912015-09-22 14:50:23 -0700855 }
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700856
857 if (existing->Kind() == Type::BUILT_IN) {
858 fprintf(stderr, "%s:%d attempt to redefine built in class %s\n",
859 type->DeclFile().c_str(), type->DeclLine(),
860 type->QualifiedName().c_str());
861 return false;
862 }
863
864 if (type->Kind() != existing->Kind()) {
865 fprintf(stderr, "%s:%d attempt to redefine %s as %s,\n",
866 type->DeclFile().c_str(), type->DeclLine(),
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700867 type->QualifiedName().c_str(), type->HumanReadableKind().c_str());
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700868 fprintf(stderr, "%s:%d previously defined here as %s.\n",
869 existing->DeclFile().c_str(), existing->DeclLine(),
870 existing->HumanReadableKind().c_str());
871 return false;
872 }
873
874 return true;
Christopher Wileye6dee912015-09-22 14:50:23 -0700875}
876
Christopher Wileycd8e8972015-10-26 10:24:35 -0700877const Type* JavaTypeNamespace::Find(const string& raw_name) const {
878 string name = Trim(raw_name);
879 if (IsContainerType(name)) {
880 vector<string> container_class;
881 vector<string> contained_type_names;
882 if (!CanonicalizeContainerType(name, &container_class,
883 &contained_type_names)) {
884 return nullptr;
885 }
886 for (string& contained_type_name : contained_type_names) {
887 const Type* contained_type = Find(contained_type_name);
888 if (!contained_type) {
889 return nullptr;
890 }
891 contained_type_name = contained_type->QualifiedName();
892 }
893 name = Join(container_class, '.') +
894 "<" + Join(contained_type_names, ',') + ">";
Christopher Wiley4582ecb2015-09-25 13:27:45 -0700895 }
896
Christopher Wiley4582ecb2015-09-25 13:27:45 -0700897 // Always prefer a exact match if possible.
898 // This works for primitives and class names qualified with a package.
899 for (const Type* type : m_types) {
900 if (type->QualifiedName() == name) {
901 return type;
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700902 }
Christopher Wileye6dee912015-09-22 14:50:23 -0700903 }
Christopher Wiley4582ecb2015-09-25 13:27:45 -0700904
905 // We allow authors to drop packages when refering to a class name.
906 // our language doesn't allow you to not specify outer classes
907 // when referencing an inner class. that could be changed, and this
908 // would be the place to do it, but I don't think the complexity in
909 // scoping rules is worth it.
910 for (const Type* type : m_types) {
911 if (type->Name() == name) {
912 return type;
913 }
914 }
915
916 return nullptr;
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700917}
918
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700919const Type* JavaTypeNamespace::Find(const char* package,
920 const char* name) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700921 string s;
922 if (package != nullptr && *package != '\0') {
923 s += package;
924 s += '.';
925 }
926 s += name;
927 return Find(s);
928}
929
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700930bool JavaTypeNamespace::AddParcelableType(const AidlParcelable* p,
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700931 const std::string& filename) {
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700932 Type* type =
Casey Dahlin59401da2015-10-09 18:16:45 -0700933 new UserDataType(this, p->GetPackage(), p->GetName(), false,
934 true, filename, p->GetLine());
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700935 return Add(type);
936}
937
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700938bool JavaTypeNamespace::AddBinderType(const AidlInterface* b,
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700939 const std::string& filename) {
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700940 // for interfaces, add the stub, proxy, and interface types.
941 Type* type =
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700942 new InterfaceType(this, b->GetPackage(), b->GetName(), false,
943 b->IsOneway(), filename, b->GetLine());
944 Type* stub = new Type(this, b->GetPackage(),
945 b->GetName() + ".Stub", Type::GENERATED, false,
946 false, filename, b->GetLine());
947 Type* proxy = new Type(this, b->GetPackage(),
948 b->GetName() + ".Stub.Proxy", Type::GENERATED,
949 false, false, filename, b->GetLine());
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700950
951 bool success = true;
952 success &= Add(type);
953 success &= Add(stub);
954 success &= Add(proxy);
955 return success;
956}
957
Christopher Wileycd8e8972015-10-26 10:24:35 -0700958bool JavaTypeNamespace::AddListType(const std::string& contained_type_name) {
959 const Type* contained_type = Find(contained_type_name);
960 if (!contained_type) {
Christopher Wiley4582ecb2015-09-25 13:27:45 -0700961 return false;
Christopher Wileye6dee912015-09-22 14:50:23 -0700962 }
Christopher Wileycd8e8972015-10-26 10:24:35 -0700963 Type* result = new GenericListType(
964 this, "java.util", "List", {contained_type});
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700965 Add(result);
Christopher Wiley4582ecb2015-09-25 13:27:45 -0700966 return true;
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700967}
968
Christopher Wileycd8e8972015-10-26 10:24:35 -0700969bool JavaTypeNamespace::AddMapType(const string& key_type_name,
970 const string& value_type_name) {
971 LOG(ERROR) << "Don't know how to create a Map<K,V> container.";
972 return false;
973}
974
Christopher Wileyfb4b22d2015-09-25 15:16:13 -0700975const ValidatableType* JavaTypeNamespace::GetValidatableType(
976 const string& name) const {
977 return Find(name);
978}
979
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700980void JavaTypeNamespace::Dump() const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700981 int n = m_types.size();
982 for (int i = 0; i < n; i++) {
Christopher Wiley8f6816e2015-09-22 17:03:47 -0700983 const Type* t = m_types[i];
Christopher Wileye6dee912015-09-22 14:50:23 -0700984 printf("type: package=%s name=%s qualifiedName=%s\n", t->Package().c_str(),
985 t->Name().c_str(), t->QualifiedName().c_str());
986 }
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700987}
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700988
Christopher Wileydb154a52015-09-28 16:32:25 -0700989} // namespace java
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700990} // namespace aidl
991} // namespace android