blob: e922f9f18f714a51e01cf89feee61e03e75c53a5 [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)
Christopher Wiley09af4692015-10-30 11:48:25 -070044 : Type(types, "", name, kind, canWriteToParcel, canBeOut, "", -1) {}
The Android Open Source Project46c012c2008-10-21 07:00:00 -070045
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -070046Type::Type(const JavaTypeNamespace* types, const string& package,
Christopher Wiley09af4692015-10-30 11:48:25 -070047 const string& name, int kind, bool canWriteToParcel,
48 bool canBeOut, const string& declFile, int declLine)
49 : ValidatableType(kind, package, name, declFile, declLine),
50 m_types(types),
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -070051 m_package(package),
Christopher Wileye6dee912015-09-22 14:50:23 -070052 m_name(name),
Christopher Wileye6dee912015-09-22 14:50:23 -070053 m_canWriteToParcel(canWriteToParcel),
54 m_canBeOut(canBeOut) {
The Android Open Source Project46c012c2008-10-21 07:00:00 -070055}
56
Christopher Wileye6dee912015-09-22 14:50:23 -070057string Type::CreatorName() const { return ""; }
58
59string Type::InstantiableName() const { return QualifiedName(); }
60
61void Type::WriteToParcel(StatementBlock* addTo, Variable* v, Variable* parcel,
Christopher Wiley3637a4d2015-09-22 15:37:59 -070062 int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -070063 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%sn", __FILE__,
64 __LINE__, m_qualifiedName.c_str());
65 addTo->Add(new LiteralExpression("/* WriteToParcel error " + m_qualifiedName +
66 " */"));
Christopher Wileyf690be52015-09-14 15:19:10 -070067}
68
Christopher Wileye6dee912015-09-22 14:50:23 -070069void Type::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -070070 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -070071 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n", __FILE__,
72 __LINE__, m_qualifiedName.c_str());
73 addTo->Add(new LiteralExpression("/* CreateFromParcel error " +
74 m_qualifiedName + " */"));
The Android Open Source Project46c012c2008-10-21 07:00:00 -070075}
76
Christopher Wileye6dee912015-09-22 14:50:23 -070077void Type::ReadFromParcel(StatementBlock* addTo, Variable* v, Variable* parcel,
Christopher Wiley3637a4d2015-09-22 15:37:59 -070078 Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -070079 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n", __FILE__,
80 __LINE__, m_qualifiedName.c_str());
81 addTo->Add(new LiteralExpression("/* ReadFromParcel error " +
82 m_qualifiedName + " */"));
The Android Open Source Project46c012c2008-10-21 07:00:00 -070083}
84
Christopher Wileye6dee912015-09-22 14:50:23 -070085void Type::WriteArrayToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -070086 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -070087 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n", __FILE__,
88 __LINE__, m_qualifiedName.c_str());
89 addTo->Add(new LiteralExpression("/* WriteArrayToParcel error " +
90 m_qualifiedName + " */"));
The Android Open Source Project46c012c2008-10-21 07:00:00 -070091}
92
Christopher Wileye6dee912015-09-22 14:50:23 -070093void Type::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -070094 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -070095 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n", __FILE__,
96 __LINE__, m_qualifiedName.c_str());
97 addTo->Add(new LiteralExpression("/* CreateArrayFromParcel error " +
98 m_qualifiedName + " */"));
The Android Open Source Project46c012c2008-10-21 07:00:00 -070099}
100
Christopher Wileye6dee912015-09-22 14:50:23 -0700101void Type::ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700102 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700103 fprintf(stderr, "aidl:internal error %s:%d qualifiedName=%s\n", __FILE__,
104 __LINE__, m_qualifiedName.c_str());
105 addTo->Add(new LiteralExpression("/* ReadArrayFromParcel error " +
106 m_qualifiedName + " */"));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700107}
108
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700109Expression* Type::BuildWriteToParcelFlags(int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700110 if (flags == 0) {
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700111 return new LiteralExpression("0");
Christopher Wileye6dee912015-09-22 14:50:23 -0700112 }
113 if ((flags & PARCELABLE_WRITE_RETURN_VALUE) != 0) {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700114 return new FieldVariable(m_types->ParcelableInterfaceType(),
Christopher Wileye6dee912015-09-22 14:50:23 -0700115 "PARCELABLE_WRITE_RETURN_VALUE");
116 }
117 return new LiteralExpression("0");
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700118}
119
120// ================================================================
121
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700122BasicType::BasicType(const JavaTypeNamespace* types, const string& name,
123 const string& marshallParcel,
Christopher Wileye6dee912015-09-22 14:50:23 -0700124 const string& unmarshallParcel,
125 const string& writeArrayParcel,
126 const string& createArrayParcel,
127 const string& readArrayParcel)
Christopher Wiley09af4692015-10-30 11:48:25 -0700128 : Type(types, name, ValidatableType::KIND_BUILT_IN, true, false),
Christopher Wileye6dee912015-09-22 14:50:23 -0700129 m_marshallParcel(marshallParcel),
130 m_unmarshallParcel(unmarshallParcel),
131 m_writeArrayParcel(writeArrayParcel),
132 m_createArrayParcel(createArrayParcel),
133 m_readArrayParcel(readArrayParcel) {}
134
135void BasicType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700136 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700137 addTo->Add(new MethodCall(parcel, m_marshallParcel, 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700138}
139
Christopher Wileye6dee912015-09-22 14:50:23 -0700140void BasicType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700141 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700142 addTo->Add(new Assignment(v, new MethodCall(parcel, m_unmarshallParcel)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700143}
144
Christopher Wileye6dee912015-09-22 14:50:23 -0700145void BasicType::WriteArrayToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700146 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700147 addTo->Add(new MethodCall(parcel, m_writeArrayParcel, 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700148}
149
Christopher Wileye6dee912015-09-22 14:50:23 -0700150void BasicType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700151 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700152 addTo->Add(new Assignment(v, new MethodCall(parcel, m_createArrayParcel)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700153}
154
Christopher Wileye6dee912015-09-22 14:50:23 -0700155void BasicType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700156 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700157 addTo->Add(new MethodCall(parcel, m_readArrayParcel, 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700158}
159
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700160// ================================================================
161
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700162BooleanType::BooleanType(const JavaTypeNamespace* types)
Christopher Wiley09af4692015-10-30 11:48:25 -0700163 : Type(types, "boolean", ValidatableType::KIND_BUILT_IN, true, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700164
165void BooleanType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700166 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700167 addTo->Add(new MethodCall(
168 parcel, "writeInt", 1,
169 new Ternary(v, new LiteralExpression("1"), new LiteralExpression("0"))));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700170}
171
Christopher Wileye6dee912015-09-22 14:50:23 -0700172void BooleanType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700173 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700174 addTo->Add(
175 new Assignment(v, new Comparison(new LiteralExpression("0"), "!=",
176 new MethodCall(parcel, "readInt"))));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700177}
178
Christopher Wileye6dee912015-09-22 14:50:23 -0700179void BooleanType::WriteArrayToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700180 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700181 addTo->Add(new MethodCall(parcel, "writeBooleanArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700182}
183
Christopher Wileye6dee912015-09-22 14:50:23 -0700184void BooleanType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700185 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700186 addTo->Add(new Assignment(v, new MethodCall(parcel, "createBooleanArray")));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700187}
188
Christopher Wileye6dee912015-09-22 14:50:23 -0700189void BooleanType::ReadArrayFromParcel(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 MethodCall(parcel, "readBooleanArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700192}
193
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700194// ================================================================
195
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700196CharType::CharType(const JavaTypeNamespace* types)
Christopher Wiley09af4692015-10-30 11:48:25 -0700197 : Type(types, "char", ValidatableType::KIND_BUILT_IN, true, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700198
199void CharType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700200 Variable* parcel, int flags) const {
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700201 addTo->Add(
202 new MethodCall(parcel, "writeInt", 1, new Cast(m_types->IntType(), v)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700203}
204
Christopher Wileye6dee912015-09-22 14:50:23 -0700205void CharType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700206 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700207 addTo->Add(new Assignment(v, new MethodCall(parcel, "readInt"), this));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700208}
209
Christopher Wileye6dee912015-09-22 14:50:23 -0700210void CharType::WriteArrayToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700211 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700212 addTo->Add(new MethodCall(parcel, "writeCharArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700213}
214
Christopher Wileye6dee912015-09-22 14:50:23 -0700215void CharType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700216 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700217 addTo->Add(new Assignment(v, new MethodCall(parcel, "createCharArray")));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700218}
219
Christopher Wileye6dee912015-09-22 14:50:23 -0700220void CharType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700221 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700222 addTo->Add(new MethodCall(parcel, "readCharArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700223}
224
225// ================================================================
226
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700227StringType::StringType(const JavaTypeNamespace* types)
Christopher Wiley09af4692015-10-30 11:48:25 -0700228 : Type(types, "java.lang", "String", ValidatableType::KIND_BUILT_IN,
229 true, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700230
231string StringType::CreatorName() const {
232 return "android.os.Parcel.STRING_CREATOR";
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700233}
234
Christopher Wileye6dee912015-09-22 14:50:23 -0700235void StringType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700236 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700237 addTo->Add(new MethodCall(parcel, "writeString", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700238}
239
Christopher Wileye6dee912015-09-22 14:50:23 -0700240void StringType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700241 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700242 addTo->Add(new Assignment(v, new MethodCall(parcel, "readString")));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700243}
244
Christopher Wileye6dee912015-09-22 14:50:23 -0700245void StringType::WriteArrayToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700246 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700247 addTo->Add(new MethodCall(parcel, "writeStringArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700248}
249
Christopher Wileye6dee912015-09-22 14:50:23 -0700250void StringType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700251 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700252 addTo->Add(new Assignment(v, new MethodCall(parcel, "createStringArray")));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700253}
254
Christopher Wileye6dee912015-09-22 14:50:23 -0700255void StringType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700256 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700257 addTo->Add(new MethodCall(parcel, "readStringArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700258}
259
260// ================================================================
261
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700262CharSequenceType::CharSequenceType(const JavaTypeNamespace* types)
Christopher Wiley09af4692015-10-30 11:48:25 -0700263 : Type(types, "java.lang", "CharSequence", ValidatableType::KIND_BUILT_IN,
264 true, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700265
266string CharSequenceType::CreatorName() const {
267 return "android.os.Parcel.STRING_CREATOR";
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700268}
269
Christopher Wileye6dee912015-09-22 14:50:23 -0700270void CharSequenceType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700271 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700272 // if (v != null) {
273 // parcel.writeInt(1);
274 // v.writeToParcel(parcel);
275 // } else {
276 // parcel.writeInt(0);
277 // }
278 IfStatement* elsepart = new IfStatement();
279 elsepart->statements->Add(
280 new MethodCall(parcel, "writeInt", 1, new LiteralExpression("0")));
281 IfStatement* ifpart = new IfStatement;
282 ifpart->expression = new Comparison(v, "!=", NULL_VALUE);
283 ifpart->elseif = elsepart;
284 ifpart->statements->Add(
285 new MethodCall(parcel, "writeInt", 1, new LiteralExpression("1")));
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700286 ifpart->statements->Add(new MethodCall(m_types->TextUtilsType(),
287 "writeToParcel", 3, v, parcel,
Christopher Wileye6dee912015-09-22 14:50:23 -0700288 BuildWriteToParcelFlags(flags)));
289
290 addTo->Add(ifpart);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700291}
292
Christopher Wileye6dee912015-09-22 14:50:23 -0700293void CharSequenceType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700294 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700295 // if (0 != parcel.readInt()) {
296 // v = TextUtils.createFromParcel(parcel)
297 // } else {
298 // v = null;
299 // }
300 IfStatement* elsepart = new IfStatement();
301 elsepart->statements->Add(new Assignment(v, NULL_VALUE));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700302
Christopher Wileye6dee912015-09-22 14:50:23 -0700303 IfStatement* ifpart = new IfStatement();
304 ifpart->expression = new Comparison(new LiteralExpression("0"), "!=",
305 new MethodCall(parcel, "readInt"));
306 ifpart->elseif = elsepart;
307 ifpart->statements->Add(new Assignment(
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700308 v, new MethodCall(m_types->TextUtilsType(),
Christopher Wileye6dee912015-09-22 14:50:23 -0700309 "CHAR_SEQUENCE_CREATOR.createFromParcel", 1, parcel)));
310
311 addTo->Add(ifpart);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700312}
313
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700314// ================================================================
315
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700316RemoteExceptionType::RemoteExceptionType(const JavaTypeNamespace* types)
Christopher Wiley09af4692015-10-30 11:48:25 -0700317 : Type(types, "android.os", "RemoteException",
318 ValidatableType::KIND_BUILT_IN, false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700319
320void RemoteExceptionType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700321 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700322 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700323}
324
Christopher Wileye6dee912015-09-22 14:50:23 -0700325void RemoteExceptionType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700326 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700327 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700328}
329
330// ================================================================
331
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700332RuntimeExceptionType::RuntimeExceptionType(const JavaTypeNamespace* types)
Christopher Wiley09af4692015-10-30 11:48:25 -0700333 : Type(types, "java.lang", "RuntimeException",
334 ValidatableType::KIND_BUILT_IN, false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700335
336void RuntimeExceptionType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700337 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700338 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700339}
340
Christopher Wileye6dee912015-09-22 14:50:23 -0700341void RuntimeExceptionType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700342 Variable* parcel,
343 Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700344 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700345}
346
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700347// ================================================================
348
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700349IBinderType::IBinderType(const JavaTypeNamespace* types)
Christopher Wiley09af4692015-10-30 11:48:25 -0700350 : Type(types, "android.os", "IBinder", ValidatableType::KIND_BUILT_IN,
351 true, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700352
353void IBinderType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700354 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700355 addTo->Add(new MethodCall(parcel, "writeStrongBinder", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700356}
357
Christopher Wileye6dee912015-09-22 14:50:23 -0700358void IBinderType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700359 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700360 addTo->Add(new Assignment(v, new MethodCall(parcel, "readStrongBinder")));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700361}
362
Christopher Wileye6dee912015-09-22 14:50:23 -0700363void IBinderType::WriteArrayToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700364 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700365 addTo->Add(new MethodCall(parcel, "writeBinderArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700366}
367
Christopher Wileye6dee912015-09-22 14:50:23 -0700368void IBinderType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700369 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700370 addTo->Add(new Assignment(v, new MethodCall(parcel, "createBinderArray")));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700371}
372
Christopher Wileye6dee912015-09-22 14:50:23 -0700373void IBinderType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700374 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700375 addTo->Add(new MethodCall(parcel, "readBinderArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700376}
377
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700378// ================================================================
379
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700380IInterfaceType::IInterfaceType(const JavaTypeNamespace* types)
Christopher Wiley09af4692015-10-30 11:48:25 -0700381 : Type(types, "android.os", "IInterface", ValidatableType::KIND_BUILT_IN,
382 false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700383
384void IInterfaceType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700385 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700386 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700387}
388
Christopher Wileye6dee912015-09-22 14:50:23 -0700389void IInterfaceType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700390 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700391 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700392}
393
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700394// ================================================================
395
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700396BinderType::BinderType(const JavaTypeNamespace* types)
Christopher Wiley09af4692015-10-30 11:48:25 -0700397 : Type(types, "android.os", "Binder", ValidatableType::KIND_BUILT_IN,
398 false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700399
400void BinderType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700401 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700402 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700403}
404
Christopher Wileye6dee912015-09-22 14:50:23 -0700405void BinderType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700406 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700407 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700408}
409
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700410// ================================================================
411
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700412BinderProxyType::BinderProxyType(const JavaTypeNamespace* types)
Christopher Wiley09af4692015-10-30 11:48:25 -0700413 : Type(types, "android.os", "BinderProxy", ValidatableType::KIND_BUILT_IN,
414 false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700415
416void BinderProxyType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700417 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700418 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700419}
420
Christopher Wileye6dee912015-09-22 14:50:23 -0700421void BinderProxyType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700422 Variable* parcel, Variable**) 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
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700426// ================================================================
427
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700428ParcelType::ParcelType(const JavaTypeNamespace* types)
Christopher Wiley09af4692015-10-30 11:48:25 -0700429 : Type(types, "android.os", "Parcel", ValidatableType::KIND_BUILT_IN,
430 false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700431
432void ParcelType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700433 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700434 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700435}
436
Christopher Wileye6dee912015-09-22 14:50:23 -0700437void ParcelType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700438 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700439 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700440}
441
442// ================================================================
443
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700444ParcelableInterfaceType::ParcelableInterfaceType(const JavaTypeNamespace* types)
Christopher Wiley09af4692015-10-30 11:48:25 -0700445 : Type(types, "android.os", "Parcelable", ValidatableType::KIND_BUILT_IN,
446 false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700447
448void ParcelableInterfaceType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700449 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700450 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700451}
452
Christopher Wileye6dee912015-09-22 14:50:23 -0700453void ParcelableInterfaceType::CreateFromParcel(StatementBlock* addTo,
454 Variable* v, Variable* parcel,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700455 Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700456 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700457}
458
459// ================================================================
460
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700461MapType::MapType(const JavaTypeNamespace* types)
Christopher Wiley09af4692015-10-30 11:48:25 -0700462 : Type(types, "java.util", "Map", ValidatableType::KIND_BUILT_IN,
463 true, true) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700464
465void MapType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700466 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700467 addTo->Add(new MethodCall(parcel, "writeMap", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700468}
469
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700470static void EnsureClassLoader(StatementBlock* addTo, Variable** cl,
471 const JavaTypeNamespace* types) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700472 // We don't want to look up the class loader once for every
473 // collection argument, so ensure we do it at most once per method.
474 if (*cl == NULL) {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700475 *cl = new Variable(types->ClassLoaderType(), "cl");
Christopher Wileye6dee912015-09-22 14:50:23 -0700476 addTo->Add(new VariableDeclaration(
477 *cl, new LiteralExpression("this.getClass().getClassLoader()"),
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700478 types->ClassLoaderType()));
Christopher Wileye6dee912015-09-22 14:50:23 -0700479 }
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700480}
481
Christopher Wileye6dee912015-09-22 14:50:23 -0700482void MapType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700483 Variable* parcel, Variable** cl) const {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700484 EnsureClassLoader(addTo, cl, m_types);
Christopher Wileye6dee912015-09-22 14:50:23 -0700485 addTo->Add(new Assignment(v, new MethodCall(parcel, "readHashMap", 1, *cl)));
Elliott Hughes15f8da22011-07-13 12:10:30 -0700486}
487
Christopher Wileye6dee912015-09-22 14:50:23 -0700488void MapType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700489 Variable* parcel, Variable** cl) const {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700490 EnsureClassLoader(addTo, cl, m_types);
Christopher Wileye6dee912015-09-22 14:50:23 -0700491 addTo->Add(new MethodCall(parcel, "readMap", 2, v, *cl));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700492}
493
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700494// ================================================================
495
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700496ListType::ListType(const JavaTypeNamespace* types)
Christopher Wiley09af4692015-10-30 11:48:25 -0700497 : Type(types, "java.util", "List", ValidatableType::KIND_BUILT_IN,
498 true, true) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700499
500string ListType::InstantiableName() const { return "java.util.ArrayList"; }
501
502void ListType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700503 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700504 addTo->Add(new MethodCall(parcel, "writeList", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700505}
506
Christopher Wileye6dee912015-09-22 14:50:23 -0700507void ListType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700508 Variable* parcel, Variable** cl) const {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700509 EnsureClassLoader(addTo, cl, m_types);
Christopher Wileye6dee912015-09-22 14:50:23 -0700510 addTo->Add(
511 new Assignment(v, new MethodCall(parcel, "readArrayList", 1, *cl)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700512}
513
Christopher Wileye6dee912015-09-22 14:50:23 -0700514void ListType::ReadFromParcel(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 MethodCall(parcel, "readList", 2, v, *cl));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700518}
519
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700520// ================================================================
521
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700522UserDataType::UserDataType(const JavaTypeNamespace* types,
523 const string& package, const string& name,
Christopher Wileye6dee912015-09-22 14:50:23 -0700524 bool builtIn, bool canWriteToParcel,
525 const string& declFile, int declLine)
Christopher Wiley09af4692015-10-30 11:48:25 -0700526 : Type(types, package, name,
527 builtIn ? ValidatableType::KIND_BUILT_IN
528 : ValidatableType::KIND_PARCELABLE,
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700529 canWriteToParcel, true, declFile, declLine) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700530
531string UserDataType::CreatorName() const {
532 return QualifiedName() + ".CREATOR";
Joe Onorato44050522011-10-09 17:38:20 -0700533}
534
Christopher Wileye6dee912015-09-22 14:50:23 -0700535void UserDataType::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 // if (v != null) {
538 // parcel.writeInt(1);
539 // v.writeToParcel(parcel);
540 // } else {
541 // parcel.writeInt(0);
542 // }
543 IfStatement* elsepart = new IfStatement();
544 elsepart->statements->Add(
545 new MethodCall(parcel, "writeInt", 1, new LiteralExpression("0")));
546 IfStatement* ifpart = new IfStatement;
547 ifpart->expression = new Comparison(v, "!=", NULL_VALUE);
548 ifpart->elseif = elsepart;
549 ifpart->statements->Add(
550 new MethodCall(parcel, "writeInt", 1, new LiteralExpression("1")));
551 ifpart->statements->Add(new MethodCall(v, "writeToParcel", 2, parcel,
552 BuildWriteToParcelFlags(flags)));
553
554 addTo->Add(ifpart);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700555}
556
Christopher Wileye6dee912015-09-22 14:50:23 -0700557void UserDataType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700558 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700559 // if (0 != parcel.readInt()) {
560 // v = CLASS.CREATOR.createFromParcel(parcel)
561 // } else {
562 // v = null;
563 // }
564 IfStatement* elsepart = new IfStatement();
565 elsepart->statements->Add(new Assignment(v, NULL_VALUE));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700566
Christopher Wileye6dee912015-09-22 14:50:23 -0700567 IfStatement* ifpart = new IfStatement();
568 ifpart->expression = new Comparison(new LiteralExpression("0"), "!=",
569 new MethodCall(parcel, "readInt"));
570 ifpart->elseif = elsepart;
571 ifpart->statements->Add(new Assignment(
572 v, new MethodCall(v->type, "CREATOR.createFromParcel", 1, parcel)));
573
574 addTo->Add(ifpart);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700575}
576
Christopher Wileye6dee912015-09-22 14:50:23 -0700577void UserDataType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700578 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700579 // TODO: really, we don't need to have this extra check, but we
580 // don't have two separate marshalling code paths
581 // if (0 != parcel.readInt()) {
582 // v.readFromParcel(parcel)
583 // }
584 IfStatement* ifpart = new IfStatement();
585 ifpart->expression = new Comparison(new LiteralExpression("0"), "!=",
586 new MethodCall(parcel, "readInt"));
587 ifpart->statements->Add(new MethodCall(v, "readFromParcel", 1, parcel));
588 addTo->Add(ifpart);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700589}
590
Christopher Wileye6dee912015-09-22 14:50:23 -0700591void UserDataType::WriteArrayToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700592 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700593 addTo->Add(new MethodCall(parcel, "writeTypedArray", 2, v,
594 BuildWriteToParcelFlags(flags)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700595}
596
Christopher Wileye6dee912015-09-22 14:50:23 -0700597void UserDataType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700598 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700599 string creator = v->type->QualifiedName() + ".CREATOR";
600 addTo->Add(new Assignment(v, new MethodCall(parcel, "createTypedArray", 1,
601 new LiteralExpression(creator))));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700602}
603
Christopher Wileye6dee912015-09-22 14:50:23 -0700604void UserDataType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700605 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700606 string creator = v->type->QualifiedName() + ".CREATOR";
607 addTo->Add(new MethodCall(parcel, "readTypedArray", 2, v,
608 new LiteralExpression(creator)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700609}
610
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700611// ================================================================
612
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700613InterfaceType::InterfaceType(const JavaTypeNamespace* types,
614 const string& package, const string& name,
Christopher Wileye6dee912015-09-22 14:50:23 -0700615 bool builtIn, bool oneway, const string& declFile,
616 int declLine)
Christopher Wiley09af4692015-10-30 11:48:25 -0700617 : Type(types, package, name, builtIn ? ValidatableType::KIND_BUILT_IN
618 : ValidatableType::KIND_INTERFACE,
619 true, false, declFile, declLine),
Christopher Wileye6dee912015-09-22 14:50:23 -0700620 m_oneway(oneway) {}
621
622bool InterfaceType::OneWay() const { return m_oneway; }
623
624void InterfaceType::WriteToParcel(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 // parcel.writeStrongBinder(v != null ? v.asBinder() : null);
627 addTo->Add(
628 new MethodCall(parcel, "writeStrongBinder", 1,
629 new Ternary(new Comparison(v, "!=", NULL_VALUE),
630 new MethodCall(v, "asBinder"), NULL_VALUE)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700631}
632
Christopher Wileye6dee912015-09-22 14:50:23 -0700633void InterfaceType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700634 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700635 // v = Interface.asInterface(parcel.readStrongBinder());
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700636 string stub_type = v->type->QualifiedName() + ".Stub";
Christopher Wileye6dee912015-09-22 14:50:23 -0700637 addTo->Add(new Assignment(
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700638 v, new MethodCall(m_types->Find(stub_type), "asInterface", 1,
Christopher Wileye6dee912015-09-22 14:50:23 -0700639 new MethodCall(parcel, "readStrongBinder"))));
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 -0700644GenericListType::GenericListType(const JavaTypeNamespace* types,
Christopher Wiley09af4692015-10-30 11:48:25 -0700645 const Type* contained_type)
646 : Type(types, "java.util", "List<" + contained_type->QualifiedName() + ">",
647 ValidatableType::KIND_BUILT_IN, true, true),
648 m_contained_type(contained_type),
649 m_creator(contained_type->CreatorName()) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700650
651string GenericListType::CreatorName() const {
652 return "android.os.Parcel.arrayListCreator";
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700653}
654
Christopher Wileye6dee912015-09-22 14:50:23 -0700655string GenericListType::InstantiableName() const {
Christopher Wiley09af4692015-10-30 11:48:25 -0700656 return "java.util.ArrayList<" + m_contained_type->QualifiedName() + ">";
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700657}
658
Christopher Wileye6dee912015-09-22 14:50:23 -0700659void GenericListType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700660 Variable* parcel, int flags) const {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700661 if (m_creator == m_types->StringType()->CreatorName()) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700662 addTo->Add(new MethodCall(parcel, "writeStringList", 1, v));
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700663 } else if (m_creator == m_types->IBinderType()->CreatorName()) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700664 addTo->Add(new MethodCall(parcel, "writeBinderList", 1, v));
665 } else {
666 // parcel.writeTypedListXX(arg);
667 addTo->Add(new MethodCall(parcel, "writeTypedList", 1, v));
668 }
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700669}
670
Christopher Wileye6dee912015-09-22 14:50:23 -0700671void GenericListType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700672 Variable* parcel, Variable**) const {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700673 if (m_creator == m_types->StringType()->CreatorName()) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700674 addTo->Add(
675 new Assignment(v, new MethodCall(parcel, "createStringArrayList", 0)));
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700676 } else if (m_creator == m_types->IBinderType()->CreatorName()) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700677 addTo->Add(
678 new Assignment(v, new MethodCall(parcel, "createBinderArrayList", 0)));
679 } else {
680 // v = _data.readTypedArrayList(XXX.creator);
681 addTo->Add(
682 new Assignment(v, new MethodCall(parcel, "createTypedArrayList", 1,
683 new LiteralExpression(m_creator))));
684 }
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700685}
686
Christopher Wileye6dee912015-09-22 14:50:23 -0700687void GenericListType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700688 Variable* parcel, Variable**) const {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700689 if (m_creator == m_types->StringType()->CreatorName()) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700690 addTo->Add(new MethodCall(parcel, "readStringList", 1, v));
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700691 } else if (m_creator == m_types->IBinderType()->CreatorName()) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700692 addTo->Add(new MethodCall(parcel, "readBinderList", 1, v));
693 } else {
694 // v = _data.readTypedList(v, XXX.creator);
695 addTo->Add(new MethodCall(parcel, "readTypedList", 2, v,
696 new LiteralExpression(m_creator)));
697 }
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700698}
699
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700700// ================================================================
701
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700702ClassLoaderType::ClassLoaderType(const JavaTypeNamespace* types)
Christopher Wiley09af4692015-10-30 11:48:25 -0700703 : Type(types, "java.lang", "ClassLoader", ValidatableType::KIND_BUILT_IN,
704 false, false) {}
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700705
706// ================================================================
707
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700708JavaTypeNamespace::JavaTypeNamespace() {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700709 Add(new BasicType(this, "void", "XXX", "XXX", "XXX", "XXX", "XXX"));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700710
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700711 m_bool_type = new BooleanType(this);
712 Add(m_bool_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700713
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700714 Add(new BasicType(this, "byte", "writeByte", "readByte", "writeByteArray",
715 "createByteArray", "readByteArray"));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700716
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700717 Add(new CharType(this));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700718
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700719 m_int_type = new BasicType(this, "int", "writeInt", "readInt",
720 "writeIntArray", "createIntArray", "readIntArray");
721 Add(m_int_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700722
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700723 Add(new BasicType(this, "long", "writeLong", "readLong", "writeLongArray",
724 "createLongArray", "readLongArray"));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700725
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700726 Add(new BasicType(this, "float", "writeFloat", "readFloat", "writeFloatArray",
727 "createFloatArray", "readFloatArray"));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700728
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700729 Add(new BasicType(this, "double", "writeDouble", "readDouble",
730 "writeDoubleArray", "createDoubleArray",
731 "readDoubleArray"));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700732
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700733 m_string_type = new class StringType(this);
734 Add(m_string_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700735
Christopher Wiley09af4692015-10-30 11:48:25 -0700736 Add(new Type(this, "java.lang", "Object", ValidatableType::KIND_BUILT_IN,
737 false, false));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700738
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700739 Add(new CharSequenceType(this));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700740
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700741 Add(new MapType(this));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700742
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700743 Add(new ListType(this));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700744
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700745 m_text_utils_type =
Christopher Wiley09af4692015-10-30 11:48:25 -0700746 new Type(this, "android.text", "TextUtils",
747 ValidatableType::KIND_BUILT_IN, false, false);
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700748 Add(m_text_utils_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700749
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700750 m_remote_exception_type = new class RemoteExceptionType(this);
751 Add(m_remote_exception_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700752
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700753 m_runtime_exception_type = new class RuntimeExceptionType(this);
754 Add(m_runtime_exception_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700755
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700756 m_ibinder_type = new class IBinderType(this);
757 Add(m_ibinder_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700758
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700759 m_iinterface_type = new class IInterfaceType(this);
760 Add(m_iinterface_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700761
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700762 m_binder_native_type = new class BinderType(this);
763 Add(m_binder_native_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700764
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700765 m_binder_proxy_type = new class BinderProxyType(this);
766 Add(m_binder_proxy_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700767
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700768 m_parcel_type = new class ParcelType(this);
769 Add(m_parcel_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700770
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700771 m_parcelable_interface_type = new class ParcelableInterfaceType(this);
772 Add(m_parcelable_interface_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700773
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700774 m_context_type = new class Type(this, "android.content", "Context",
Christopher Wiley09af4692015-10-30 11:48:25 -0700775 ValidatableType::KIND_BUILT_IN, false, false);
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700776 Add(m_context_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700777
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700778 m_classloader_type = new class ClassLoaderType(this);
779 Add(m_classloader_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700780
781 NULL_VALUE = new LiteralExpression("null");
782 THIS_VALUE = new LiteralExpression("this");
783 SUPER_VALUE = new LiteralExpression("super");
784 TRUE_VALUE = new LiteralExpression("true");
785 FALSE_VALUE = new LiteralExpression("false");
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700786}
Christopher Wileye6dee912015-09-22 14:50:23 -0700787
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700788const Type* JavaTypeNamespace::Find(const char* package,
789 const char* name) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700790 string s;
791 if (package != nullptr && *package != '\0') {
792 s += package;
793 s += '.';
794 }
795 s += name;
796 return Find(s);
797}
798
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700799bool JavaTypeNamespace::AddParcelableType(const AidlParcelable* p,
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700800 const std::string& filename) {
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700801 Type* type =
Casey Dahlin59401da2015-10-09 18:16:45 -0700802 new UserDataType(this, p->GetPackage(), p->GetName(), false,
803 true, filename, p->GetLine());
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700804 return Add(type);
805}
806
Casey Dahlin1ae2bc52015-10-07 18:49:10 -0700807bool JavaTypeNamespace::AddBinderType(const AidlInterface* b,
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700808 const std::string& filename) {
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700809 // for interfaces, add the stub, proxy, and interface types.
810 Type* type =
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700811 new InterfaceType(this, b->GetPackage(), b->GetName(), false,
812 b->IsOneway(), filename, b->GetLine());
813 Type* stub = new Type(this, b->GetPackage(),
Christopher Wiley09af4692015-10-30 11:48:25 -0700814 b->GetName() + ".Stub", ValidatableType::KIND_GENERATED,
815 false, false, filename, b->GetLine());
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700816 Type* proxy = new Type(this, b->GetPackage(),
Christopher Wiley09af4692015-10-30 11:48:25 -0700817 b->GetName() + ".Stub.Proxy",
818 ValidatableType::KIND_GENERATED,
Casey Dahlinfb7da2e2015-10-08 17:26:09 -0700819 false, false, filename, b->GetLine());
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700820
821 bool success = true;
822 success &= Add(type);
823 success &= Add(stub);
824 success &= Add(proxy);
825 return success;
826}
827
Christopher Wileycd8e8972015-10-26 10:24:35 -0700828bool JavaTypeNamespace::AddListType(const std::string& contained_type_name) {
829 const Type* contained_type = Find(contained_type_name);
830 if (!contained_type) {
Christopher Wiley4582ecb2015-09-25 13:27:45 -0700831 return false;
Christopher Wileye6dee912015-09-22 14:50:23 -0700832 }
Christopher Wiley09af4692015-10-30 11:48:25 -0700833 Add(new GenericListType(this, contained_type));
Christopher Wiley4582ecb2015-09-25 13:27:45 -0700834 return true;
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700835}
836
Christopher Wileycd8e8972015-10-26 10:24:35 -0700837bool JavaTypeNamespace::AddMapType(const string& key_type_name,
838 const string& value_type_name) {
839 LOG(ERROR) << "Don't know how to create a Map<K,V> container.";
840 return false;
841}
842
Christopher Wileydb154a52015-09-28 16:32:25 -0700843} // namespace java
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700844} // namespace aidl
845} // namespace android