blob: 2dbc69229d9f60d8257315e0b435110418257666 [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
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800162FileDescriptorType::FileDescriptorType(const JavaTypeNamespace* types)
163 : Type(types, "java.io", "FileDescriptor", ValidatableType::KIND_BUILT_IN,
164 true, false) {}
165
166void FileDescriptorType::WriteToParcel(StatementBlock* addTo, Variable* v,
167 Variable* parcel, int flags) const {
168 addTo->Add(new MethodCall(parcel, "writeRawFileDescriptor", 1, v));
169}
170
171void FileDescriptorType::CreateFromParcel(StatementBlock* addTo, Variable* v,
172 Variable* parcel, Variable**) const {
173 addTo->Add(new Assignment(v, new MethodCall(parcel, "readRawFileDescriptor")));
174}
175
176void FileDescriptorType::WriteArrayToParcel(StatementBlock* addTo, Variable* v,
177 Variable* parcel, int flags) const {
178 addTo->Add(new MethodCall(parcel, "writeRawFileDescriptorArray", 1, v));
179}
180
181void FileDescriptorType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
182 Variable* parcel, Variable**) const {
183 addTo->Add(new Assignment(v, new MethodCall(parcel, "createRawFileDescriptorArray")));
184}
185
186void FileDescriptorType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
187 Variable* parcel, Variable**) const {
188 addTo->Add(new MethodCall(parcel, "readRawFileDescriptorArray", 1, v));
189}
190
191// ================================================================
192
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700193BooleanType::BooleanType(const JavaTypeNamespace* types)
Christopher Wiley09af4692015-10-30 11:48:25 -0700194 : Type(types, "boolean", ValidatableType::KIND_BUILT_IN, true, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700195
196void BooleanType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700197 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700198 addTo->Add(new MethodCall(
199 parcel, "writeInt", 1,
200 new Ternary(v, new LiteralExpression("1"), new LiteralExpression("0"))));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700201}
202
Christopher Wileye6dee912015-09-22 14:50:23 -0700203void BooleanType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700204 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700205 addTo->Add(
206 new Assignment(v, new Comparison(new LiteralExpression("0"), "!=",
207 new MethodCall(parcel, "readInt"))));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700208}
209
Christopher Wileye6dee912015-09-22 14:50:23 -0700210void BooleanType::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, "writeBooleanArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700213}
214
Christopher Wileye6dee912015-09-22 14:50:23 -0700215void BooleanType::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, "createBooleanArray")));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700218}
219
Christopher Wileye6dee912015-09-22 14:50:23 -0700220void BooleanType::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, "readBooleanArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700223}
224
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700225// ================================================================
226
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700227CharType::CharType(const JavaTypeNamespace* types)
Christopher Wiley09af4692015-10-30 11:48:25 -0700228 : Type(types, "char", ValidatableType::KIND_BUILT_IN, true, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700229
230void CharType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700231 Variable* parcel, int flags) const {
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700232 addTo->Add(
233 new MethodCall(parcel, "writeInt", 1, new Cast(m_types->IntType(), v)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700234}
235
Christopher Wileye6dee912015-09-22 14:50:23 -0700236void CharType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700237 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700238 addTo->Add(new Assignment(v, new MethodCall(parcel, "readInt"), this));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700239}
240
Christopher Wileye6dee912015-09-22 14:50:23 -0700241void CharType::WriteArrayToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700242 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700243 addTo->Add(new MethodCall(parcel, "writeCharArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700244}
245
Christopher Wileye6dee912015-09-22 14:50:23 -0700246void CharType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700247 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700248 addTo->Add(new Assignment(v, new MethodCall(parcel, "createCharArray")));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700249}
250
Christopher Wileye6dee912015-09-22 14:50:23 -0700251void CharType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700252 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700253 addTo->Add(new MethodCall(parcel, "readCharArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700254}
255
256// ================================================================
257
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700258StringType::StringType(const JavaTypeNamespace* types)
Christopher Wiley09af4692015-10-30 11:48:25 -0700259 : Type(types, "java.lang", "String", ValidatableType::KIND_BUILT_IN,
260 true, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700261
262string StringType::CreatorName() const {
263 return "android.os.Parcel.STRING_CREATOR";
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700264}
265
Christopher Wileye6dee912015-09-22 14:50:23 -0700266void StringType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700267 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700268 addTo->Add(new MethodCall(parcel, "writeString", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700269}
270
Christopher Wileye6dee912015-09-22 14:50:23 -0700271void StringType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700272 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700273 addTo->Add(new Assignment(v, new MethodCall(parcel, "readString")));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700274}
275
Christopher Wileye6dee912015-09-22 14:50:23 -0700276void StringType::WriteArrayToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700277 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700278 addTo->Add(new MethodCall(parcel, "writeStringArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700279}
280
Christopher Wileye6dee912015-09-22 14:50:23 -0700281void StringType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700282 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700283 addTo->Add(new Assignment(v, new MethodCall(parcel, "createStringArray")));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700284}
285
Christopher Wileye6dee912015-09-22 14:50:23 -0700286void StringType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700287 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700288 addTo->Add(new MethodCall(parcel, "readStringArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700289}
290
291// ================================================================
292
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700293CharSequenceType::CharSequenceType(const JavaTypeNamespace* types)
Christopher Wiley09af4692015-10-30 11:48:25 -0700294 : Type(types, "java.lang", "CharSequence", ValidatableType::KIND_BUILT_IN,
295 true, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700296
297string CharSequenceType::CreatorName() const {
298 return "android.os.Parcel.STRING_CREATOR";
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700299}
300
Christopher Wileye6dee912015-09-22 14:50:23 -0700301void CharSequenceType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700302 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700303 // if (v != null) {
304 // parcel.writeInt(1);
305 // v.writeToParcel(parcel);
306 // } else {
307 // parcel.writeInt(0);
308 // }
309 IfStatement* elsepart = new IfStatement();
310 elsepart->statements->Add(
311 new MethodCall(parcel, "writeInt", 1, new LiteralExpression("0")));
312 IfStatement* ifpart = new IfStatement;
313 ifpart->expression = new Comparison(v, "!=", NULL_VALUE);
314 ifpart->elseif = elsepart;
315 ifpart->statements->Add(
316 new MethodCall(parcel, "writeInt", 1, new LiteralExpression("1")));
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700317 ifpart->statements->Add(new MethodCall(m_types->TextUtilsType(),
318 "writeToParcel", 3, v, parcel,
Christopher Wileye6dee912015-09-22 14:50:23 -0700319 BuildWriteToParcelFlags(flags)));
320
321 addTo->Add(ifpart);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700322}
323
Christopher Wileye6dee912015-09-22 14:50:23 -0700324void CharSequenceType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700325 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700326 // if (0 != parcel.readInt()) {
327 // v = TextUtils.createFromParcel(parcel)
328 // } else {
329 // v = null;
330 // }
331 IfStatement* elsepart = new IfStatement();
332 elsepart->statements->Add(new Assignment(v, NULL_VALUE));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700333
Christopher Wileye6dee912015-09-22 14:50:23 -0700334 IfStatement* ifpart = new IfStatement();
335 ifpart->expression = new Comparison(new LiteralExpression("0"), "!=",
336 new MethodCall(parcel, "readInt"));
337 ifpart->elseif = elsepart;
338 ifpart->statements->Add(new Assignment(
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700339 v, new MethodCall(m_types->TextUtilsType(),
Christopher Wileye6dee912015-09-22 14:50:23 -0700340 "CHAR_SEQUENCE_CREATOR.createFromParcel", 1, parcel)));
341
342 addTo->Add(ifpart);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700343}
344
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700345// ================================================================
346
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700347RemoteExceptionType::RemoteExceptionType(const JavaTypeNamespace* types)
Christopher Wiley09af4692015-10-30 11:48:25 -0700348 : Type(types, "android.os", "RemoteException",
349 ValidatableType::KIND_BUILT_IN, false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700350
351void RemoteExceptionType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700352 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700353 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700354}
355
Christopher Wileye6dee912015-09-22 14:50:23 -0700356void RemoteExceptionType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700357 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700358 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700359}
360
361// ================================================================
362
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700363RuntimeExceptionType::RuntimeExceptionType(const JavaTypeNamespace* types)
Christopher Wiley09af4692015-10-30 11:48:25 -0700364 : Type(types, "java.lang", "RuntimeException",
365 ValidatableType::KIND_BUILT_IN, false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700366
367void RuntimeExceptionType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700368 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700369 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700370}
371
Christopher Wileye6dee912015-09-22 14:50:23 -0700372void RuntimeExceptionType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700373 Variable* parcel,
374 Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700375 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
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 -0700380IBinderType::IBinderType(const JavaTypeNamespace* types)
Christopher Wiley09af4692015-10-30 11:48:25 -0700381 : Type(types, "android.os", "IBinder", ValidatableType::KIND_BUILT_IN,
382 true, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700383
384void IBinderType::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 addTo->Add(new MethodCall(parcel, "writeStrongBinder", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700387}
388
Christopher Wileye6dee912015-09-22 14:50:23 -0700389void IBinderType::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 addTo->Add(new Assignment(v, new MethodCall(parcel, "readStrongBinder")));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700392}
393
Christopher Wileye6dee912015-09-22 14:50:23 -0700394void IBinderType::WriteArrayToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700395 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700396 addTo->Add(new MethodCall(parcel, "writeBinderArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700397}
398
Christopher Wileye6dee912015-09-22 14:50:23 -0700399void IBinderType::CreateArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700400 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700401 addTo->Add(new Assignment(v, new MethodCall(parcel, "createBinderArray")));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700402}
403
Christopher Wileye6dee912015-09-22 14:50:23 -0700404void IBinderType::ReadArrayFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700405 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700406 addTo->Add(new MethodCall(parcel, "readBinderArray", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700407}
408
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700409// ================================================================
410
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700411IInterfaceType::IInterfaceType(const JavaTypeNamespace* types)
Christopher Wiley09af4692015-10-30 11:48:25 -0700412 : Type(types, "android.os", "IInterface", ValidatableType::KIND_BUILT_IN,
413 false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700414
415void IInterfaceType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700416 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700417 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700418}
419
Christopher Wileye6dee912015-09-22 14:50:23 -0700420void IInterfaceType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700421 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700422 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700423}
424
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700425// ================================================================
426
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700427BinderType::BinderType(const JavaTypeNamespace* types)
Christopher Wiley09af4692015-10-30 11:48:25 -0700428 : Type(types, "android.os", "Binder", ValidatableType::KIND_BUILT_IN,
429 false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700430
431void BinderType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700432 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700433 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700434}
435
Christopher Wileye6dee912015-09-22 14:50:23 -0700436void BinderType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700437 Variable* parcel, Variable**) 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
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700441// ================================================================
442
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700443BinderProxyType::BinderProxyType(const JavaTypeNamespace* types)
Christopher Wiley09af4692015-10-30 11:48:25 -0700444 : Type(types, "android.os", "BinderProxy", ValidatableType::KIND_BUILT_IN,
445 false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700446
447void BinderProxyType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700448 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700449 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700450}
451
Christopher Wileye6dee912015-09-22 14:50:23 -0700452void BinderProxyType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700453 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700454 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700455}
456
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700457// ================================================================
458
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700459ParcelType::ParcelType(const JavaTypeNamespace* types)
Christopher Wiley09af4692015-10-30 11:48:25 -0700460 : Type(types, "android.os", "Parcel", ValidatableType::KIND_BUILT_IN,
461 false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700462
463void ParcelType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700464 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700465 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700466}
467
Christopher Wileye6dee912015-09-22 14:50:23 -0700468void ParcelType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700469 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700470 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700471}
472
473// ================================================================
474
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700475ParcelableInterfaceType::ParcelableInterfaceType(const JavaTypeNamespace* types)
Christopher Wiley09af4692015-10-30 11:48:25 -0700476 : Type(types, "android.os", "Parcelable", ValidatableType::KIND_BUILT_IN,
477 false, false) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700478
479void ParcelableInterfaceType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700480 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700481 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700482}
483
Christopher Wileye6dee912015-09-22 14:50:23 -0700484void ParcelableInterfaceType::CreateFromParcel(StatementBlock* addTo,
485 Variable* v, Variable* parcel,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700486 Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700487 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, __LINE__);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700488}
489
490// ================================================================
491
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700492MapType::MapType(const JavaTypeNamespace* types)
Christopher Wiley09af4692015-10-30 11:48:25 -0700493 : Type(types, "java.util", "Map", ValidatableType::KIND_BUILT_IN,
494 true, true) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700495
496void MapType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700497 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700498 addTo->Add(new MethodCall(parcel, "writeMap", 1, v));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700499}
500
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700501static void EnsureClassLoader(StatementBlock* addTo, Variable** cl,
502 const JavaTypeNamespace* types) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700503 // We don't want to look up the class loader once for every
504 // collection argument, so ensure we do it at most once per method.
505 if (*cl == NULL) {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700506 *cl = new Variable(types->ClassLoaderType(), "cl");
Christopher Wileye6dee912015-09-22 14:50:23 -0700507 addTo->Add(new VariableDeclaration(
508 *cl, new LiteralExpression("this.getClass().getClassLoader()"),
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700509 types->ClassLoaderType()));
Christopher Wileye6dee912015-09-22 14:50:23 -0700510 }
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700511}
512
Christopher Wileye6dee912015-09-22 14:50:23 -0700513void MapType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700514 Variable* parcel, Variable** cl) const {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700515 EnsureClassLoader(addTo, cl, m_types);
Christopher Wileye6dee912015-09-22 14:50:23 -0700516 addTo->Add(new Assignment(v, new MethodCall(parcel, "readHashMap", 1, *cl)));
Elliott Hughes15f8da22011-07-13 12:10:30 -0700517}
518
Christopher Wileye6dee912015-09-22 14:50:23 -0700519void MapType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700520 Variable* parcel, Variable** cl) const {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700521 EnsureClassLoader(addTo, cl, m_types);
Christopher Wileye6dee912015-09-22 14:50:23 -0700522 addTo->Add(new MethodCall(parcel, "readMap", 2, v, *cl));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700523}
524
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700525// ================================================================
526
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700527ListType::ListType(const JavaTypeNamespace* types)
Christopher Wiley09af4692015-10-30 11:48:25 -0700528 : Type(types, "java.util", "List", ValidatableType::KIND_BUILT_IN,
529 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 Wiley09af4692015-10-30 11:48:25 -0700557 : Type(types, package, name,
558 builtIn ? ValidatableType::KIND_BUILT_IN
559 : ValidatableType::KIND_PARCELABLE,
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700560 canWriteToParcel, true, declFile, declLine) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700561
562string UserDataType::CreatorName() const {
563 return QualifiedName() + ".CREATOR";
Joe Onorato44050522011-10-09 17:38:20 -0700564}
565
Christopher Wileye6dee912015-09-22 14:50:23 -0700566void UserDataType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700567 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700568 // if (v != null) {
569 // parcel.writeInt(1);
570 // v.writeToParcel(parcel);
571 // } else {
572 // parcel.writeInt(0);
573 // }
574 IfStatement* elsepart = new IfStatement();
575 elsepart->statements->Add(
576 new MethodCall(parcel, "writeInt", 1, new LiteralExpression("0")));
577 IfStatement* ifpart = new IfStatement;
578 ifpart->expression = new Comparison(v, "!=", NULL_VALUE);
579 ifpart->elseif = elsepart;
580 ifpart->statements->Add(
581 new MethodCall(parcel, "writeInt", 1, new LiteralExpression("1")));
582 ifpart->statements->Add(new MethodCall(v, "writeToParcel", 2, parcel,
583 BuildWriteToParcelFlags(flags)));
584
585 addTo->Add(ifpart);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700586}
587
Christopher Wileye6dee912015-09-22 14:50:23 -0700588void UserDataType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700589 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700590 // if (0 != parcel.readInt()) {
591 // v = CLASS.CREATOR.createFromParcel(parcel)
592 // } else {
593 // v = null;
594 // }
595 IfStatement* elsepart = new IfStatement();
596 elsepart->statements->Add(new Assignment(v, NULL_VALUE));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700597
Christopher Wileye6dee912015-09-22 14:50:23 -0700598 IfStatement* ifpart = new IfStatement();
599 ifpart->expression = new Comparison(new LiteralExpression("0"), "!=",
600 new MethodCall(parcel, "readInt"));
601 ifpart->elseif = elsepart;
602 ifpart->statements->Add(new Assignment(
603 v, new MethodCall(v->type, "CREATOR.createFromParcel", 1, parcel)));
604
605 addTo->Add(ifpart);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700606}
607
Christopher Wileye6dee912015-09-22 14:50:23 -0700608void UserDataType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700609 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700610 // TODO: really, we don't need to have this extra check, but we
611 // don't have two separate marshalling code paths
612 // if (0 != parcel.readInt()) {
613 // v.readFromParcel(parcel)
614 // }
615 IfStatement* ifpart = new IfStatement();
616 ifpart->expression = new Comparison(new LiteralExpression("0"), "!=",
617 new MethodCall(parcel, "readInt"));
618 ifpart->statements->Add(new MethodCall(v, "readFromParcel", 1, parcel));
619 addTo->Add(ifpart);
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700620}
621
Christopher Wileye6dee912015-09-22 14:50:23 -0700622void 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 Wiley09af4692015-10-30 11:48:25 -0700648 : Type(types, package, name, builtIn ? ValidatableType::KIND_BUILT_IN
649 : ValidatableType::KIND_INTERFACE,
650 true, false, declFile, declLine),
Christopher Wileye6dee912015-09-22 14:50:23 -0700651 m_oneway(oneway) {}
652
653bool InterfaceType::OneWay() const { return m_oneway; }
654
655void InterfaceType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700656 Variable* parcel, int flags) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700657 // parcel.writeStrongBinder(v != null ? v.asBinder() : null);
658 addTo->Add(
659 new MethodCall(parcel, "writeStrongBinder", 1,
660 new Ternary(new Comparison(v, "!=", NULL_VALUE),
661 new MethodCall(v, "asBinder"), NULL_VALUE)));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700662}
663
Christopher Wileye6dee912015-09-22 14:50:23 -0700664void InterfaceType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700665 Variable* parcel, Variable**) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700666 // v = Interface.asInterface(parcel.readStrongBinder());
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700667 string stub_type = v->type->QualifiedName() + ".Stub";
Christopher Wileye6dee912015-09-22 14:50:23 -0700668 addTo->Add(new Assignment(
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700669 v, new MethodCall(m_types->Find(stub_type), "asInterface", 1,
Christopher Wileye6dee912015-09-22 14:50:23 -0700670 new MethodCall(parcel, "readStrongBinder"))));
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700671}
672
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700673// ================================================================
674
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700675GenericListType::GenericListType(const JavaTypeNamespace* types,
Christopher Wiley09af4692015-10-30 11:48:25 -0700676 const Type* contained_type)
677 : Type(types, "java.util", "List<" + contained_type->QualifiedName() + ">",
678 ValidatableType::KIND_BUILT_IN, true, true),
679 m_contained_type(contained_type),
680 m_creator(contained_type->CreatorName()) {}
Christopher Wileye6dee912015-09-22 14:50:23 -0700681
682string GenericListType::CreatorName() const {
683 return "android.os.Parcel.arrayListCreator";
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700684}
685
Christopher Wileye6dee912015-09-22 14:50:23 -0700686string GenericListType::InstantiableName() const {
Christopher Wiley09af4692015-10-30 11:48:25 -0700687 return "java.util.ArrayList<" + m_contained_type->QualifiedName() + ">";
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700688}
689
Christopher Wileye6dee912015-09-22 14:50:23 -0700690void GenericListType::WriteToParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700691 Variable* parcel, int flags) const {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700692 if (m_creator == m_types->StringType()->CreatorName()) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700693 addTo->Add(new MethodCall(parcel, "writeStringList", 1, v));
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700694 } else if (m_creator == m_types->IBinderType()->CreatorName()) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700695 addTo->Add(new MethodCall(parcel, "writeBinderList", 1, v));
696 } else {
697 // parcel.writeTypedListXX(arg);
698 addTo->Add(new MethodCall(parcel, "writeTypedList", 1, v));
699 }
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700700}
701
Christopher Wileye6dee912015-09-22 14:50:23 -0700702void GenericListType::CreateFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700703 Variable* parcel, Variable**) const {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700704 if (m_creator == m_types->StringType()->CreatorName()) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700705 addTo->Add(
706 new Assignment(v, new MethodCall(parcel, "createStringArrayList", 0)));
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700707 } else if (m_creator == m_types->IBinderType()->CreatorName()) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700708 addTo->Add(
709 new Assignment(v, new MethodCall(parcel, "createBinderArrayList", 0)));
710 } else {
711 // v = _data.readTypedArrayList(XXX.creator);
712 addTo->Add(
713 new Assignment(v, new MethodCall(parcel, "createTypedArrayList", 1,
714 new LiteralExpression(m_creator))));
715 }
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700716}
717
Christopher Wileye6dee912015-09-22 14:50:23 -0700718void GenericListType::ReadFromParcel(StatementBlock* addTo, Variable* v,
Christopher Wiley3637a4d2015-09-22 15:37:59 -0700719 Variable* parcel, Variable**) const {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700720 if (m_creator == m_types->StringType()->CreatorName()) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700721 addTo->Add(new MethodCall(parcel, "readStringList", 1, v));
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700722 } else if (m_creator == m_types->IBinderType()->CreatorName()) {
Christopher Wileye6dee912015-09-22 14:50:23 -0700723 addTo->Add(new MethodCall(parcel, "readBinderList", 1, v));
724 } else {
725 // v = _data.readTypedList(v, XXX.creator);
726 addTo->Add(new MethodCall(parcel, "readTypedList", 2, v,
727 new LiteralExpression(m_creator)));
728 }
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700729}
730
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700731// ================================================================
732
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700733ClassLoaderType::ClassLoaderType(const JavaTypeNamespace* types)
Christopher Wiley09af4692015-10-30 11:48:25 -0700734 : Type(types, "java.lang", "ClassLoader", ValidatableType::KIND_BUILT_IN,
735 false, false) {}
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700736
737// ================================================================
738
Christopher Wiley56799522015-10-31 10:17:04 -0700739void JavaTypeNamespace::Init() {
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700740 Add(new BasicType(this, "void", "XXX", "XXX", "XXX", "XXX", "XXX"));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700741
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700742 m_bool_type = new BooleanType(this);
743 Add(m_bool_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700744
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700745 Add(new BasicType(this, "byte", "writeByte", "readByte", "writeByteArray",
746 "createByteArray", "readByteArray"));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700747
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700748 Add(new CharType(this));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700749
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700750 m_int_type = new BasicType(this, "int", "writeInt", "readInt",
751 "writeIntArray", "createIntArray", "readIntArray");
752 Add(m_int_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700753
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700754 Add(new BasicType(this, "long", "writeLong", "readLong", "writeLongArray",
755 "createLongArray", "readLongArray"));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700756
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700757 Add(new BasicType(this, "float", "writeFloat", "readFloat", "writeFloatArray",
758 "createFloatArray", "readFloatArray"));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700759
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700760 Add(new BasicType(this, "double", "writeDouble", "readDouble",
761 "writeDoubleArray", "createDoubleArray",
762 "readDoubleArray"));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700763
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700764 m_string_type = new class StringType(this);
765 Add(m_string_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700766
Christopher Wiley09af4692015-10-30 11:48:25 -0700767 Add(new Type(this, "java.lang", "Object", ValidatableType::KIND_BUILT_IN,
768 false, false));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700769
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800770 Add(new FileDescriptorType(this));
771
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700772 Add(new CharSequenceType(this));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700773
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700774 Add(new MapType(this));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700775
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700776 Add(new ListType(this));
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700777
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700778 m_text_utils_type =
Christopher Wiley09af4692015-10-30 11:48:25 -0700779 new Type(this, "android.text", "TextUtils",
780 ValidatableType::KIND_BUILT_IN, false, false);
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700781 Add(m_text_utils_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700782
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700783 m_remote_exception_type = new class RemoteExceptionType(this);
784 Add(m_remote_exception_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700785
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700786 m_runtime_exception_type = new class RuntimeExceptionType(this);
787 Add(m_runtime_exception_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700788
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700789 m_ibinder_type = new class IBinderType(this);
790 Add(m_ibinder_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700791
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700792 m_iinterface_type = new class IInterfaceType(this);
793 Add(m_iinterface_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700794
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700795 m_binder_native_type = new class BinderType(this);
796 Add(m_binder_native_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700797
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700798 m_binder_proxy_type = new class BinderProxyType(this);
799 Add(m_binder_proxy_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700800
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700801 m_parcel_type = new class ParcelType(this);
802 Add(m_parcel_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700803
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700804 m_parcelable_interface_type = new class ParcelableInterfaceType(this);
805 Add(m_parcelable_interface_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700806
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700807 m_context_type = new class Type(this, "android.content", "Context",
Christopher Wiley09af4692015-10-30 11:48:25 -0700808 ValidatableType::KIND_BUILT_IN, false, false);
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700809 Add(m_context_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700810
Christopher Wiley60b02ae2015-09-23 16:35:18 -0700811 m_classloader_type = new class ClassLoaderType(this);
812 Add(m_classloader_type);
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700813
814 NULL_VALUE = new LiteralExpression("null");
815 THIS_VALUE = new LiteralExpression("this");
816 SUPER_VALUE = new LiteralExpression("super");
817 TRUE_VALUE = new LiteralExpression("true");
818 FALSE_VALUE = new LiteralExpression("false");
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700819}
Christopher Wileye6dee912015-09-22 14:50:23 -0700820
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700821const Type* JavaTypeNamespace::Find(const char* package,
822 const char* name) const {
Christopher Wileye6dee912015-09-22 14:50:23 -0700823 string s;
824 if (package != nullptr && *package != '\0') {
825 s += package;
826 s += '.';
827 }
828 s += name;
829 return Find(s);
830}
831
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800832bool JavaTypeNamespace::AddParcelableType(const AidlParcelable& p,
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700833 const std::string& filename) {
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700834 Type* type =
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800835 new UserDataType(this, p.GetPackage(), p.GetName(), false,
836 true, filename, p.GetLine());
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700837 return Add(type);
838}
839
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800840bool JavaTypeNamespace::AddBinderType(const AidlInterface& b,
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700841 const std::string& filename) {
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -0700842 // for interfaces, add the stub, proxy, and interface types.
843 Type* type =
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800844 new InterfaceType(this, b.GetPackage(), b.GetName(), false,
845 b.IsOneway(), filename, b.GetLine());
846 Type* stub = new Type(this, b.GetPackage(),
847 b.GetName() + ".Stub", ValidatableType::KIND_GENERATED,
848 false, false, filename, b.GetLine());
849 Type* proxy = new Type(this, b.GetPackage(),
850 b.GetName() + ".Stub.Proxy",
Christopher Wiley09af4692015-10-30 11:48:25 -0700851 ValidatableType::KIND_GENERATED,
Casey Dahlinc1f39b42015-11-24 10:34:34 -0800852 false, false, filename, b.GetLine());
Christopher Wiley84c1eac2015-09-23 13:29:28 -0700853
854 bool success = true;
855 success &= Add(type);
856 success &= Add(stub);
857 success &= Add(proxy);
858 return success;
859}
860
Christopher Wileycd8e8972015-10-26 10:24:35 -0700861bool JavaTypeNamespace::AddListType(const std::string& contained_type_name) {
862 const Type* contained_type = Find(contained_type_name);
863 if (!contained_type) {
Christopher Wiley4582ecb2015-09-25 13:27:45 -0700864 return false;
Christopher Wileye6dee912015-09-22 14:50:23 -0700865 }
Christopher Wiley09af4692015-10-30 11:48:25 -0700866 Add(new GenericListType(this, contained_type));
Christopher Wiley4582ecb2015-09-25 13:27:45 -0700867 return true;
The Android Open Source Project46c012c2008-10-21 07:00:00 -0700868}
869
Christopher Wileycd8e8972015-10-26 10:24:35 -0700870bool JavaTypeNamespace::AddMapType(const string& key_type_name,
871 const string& value_type_name) {
872 LOG(ERROR) << "Don't know how to create a Map<K,V> container.";
873 return false;
874}
875
Christopher Wileydb154a52015-09-28 16:32:25 -0700876} // namespace java
Christopher Wileyfdeb0f42015-09-11 15:38:22 -0700877} // namespace aidl
878} // namespace android