-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaidl_to_rust.cpp
More file actions
302 lines (259 loc) · 9.34 KB
/
Copy pathaidl_to_rust.cpp
File metadata and controls
302 lines (259 loc) · 9.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
* Copyright (C) 2020, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "aidl_to_rust.h"
#include "aidl_language.h"
#include "aidl_typenames.h"
#include "logging.h"
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using android::base::Join;
using android::base::Split;
using android::base::StringPrintf;
namespace android {
namespace aidl {
namespace rust {
namespace {
std::string GetRawRustName(const AidlTypeSpecifier& type);
std::string ConstantValueDecoratorInternal(const AidlTypeSpecifier& type,
const std::string& raw_value, bool by_ref) {
if (type.IsArray()) {
// Convert `{ ... }` to `vec!{ ... }`
return "vec!" + raw_value;
}
const auto& aidl_name = type.GetName();
if (aidl_name == "char") {
return raw_value + " as u16";
}
if (aidl_name == "float") {
// raw_value already ends in `f`, so just add `32`
return raw_value + "32";
}
if (aidl_name == "double") {
return raw_value + "f64";
}
if (aidl_name == "String" && !by_ref) {
// The actual type might be String or &str,
// and .into() transparently converts into either one
return raw_value + ".into()";
}
if (auto defined_type = type.GetDefinedType(); defined_type) {
auto enum_type = defined_type->AsEnumDeclaration();
AIDL_FATAL_IF(!enum_type, type) << "Invalid type for \"" << raw_value << "\"";
return GetRawRustName(type) + "::" + raw_value.substr(raw_value.find_last_of('.') + 1);
}
return raw_value;
}
std::string GetRawRustName(const AidlTypeSpecifier& type) {
// Each Rust type is defined in a file with the same name,
// e.g., IFoo is in IFoo.rs
auto split_name = type.GetSplitName();
std::string rust_name{"crate::mangled::"};
for (const auto& component : split_name) {
rust_name += StringPrintf("_%zd_%s", component.size(), component.c_str());
}
return rust_name;
}
std::string GetRustName(const AidlTypeSpecifier& type, const AidlTypenames& typenames,
StorageMode mode) {
// map from AIDL built-in type name to the corresponding Rust type name
static map<string, string> m = {
{"void", "()"},
{"boolean", "bool"},
{"byte", "i8"},
{"char", "u16"},
{"int", "i32"},
{"long", "i64"},
{"float", "f32"},
{"double", "f64"},
{"String", "String"},
{"IBinder", "binder::SpIBinder"},
{"ParcelFileDescriptor", "binder::parcel::ParcelFileDescriptor"},
};
// If the type is an array/List<T>, get the inner element type
AIDL_FATAL_IF(typenames.IsList(type) && type.GetTypeParameters().size() != 1, type);
const auto& element_type = type.IsGeneric() ? (*type.GetTypeParameters().at(0)) : type;
const string& element_type_name = element_type.GetName();
if (m.find(element_type_name) != m.end()) {
AIDL_FATAL_IF(!AidlTypenames::IsBuiltinTypename(element_type_name), type);
if (element_type_name == "byte" && type.IsArray()) {
return "u8";
} else if (element_type_name == "String" && mode == StorageMode::UNSIZED_ARGUMENT) {
return "str";
} else if (element_type_name == "ParcelFileDescriptor") {
if (type.IsArray() && mode == StorageMode::DEFAULT_VALUE) {
// Out-arguments of ParcelFileDescriptors arrays need to
// be Vec<Option<ParcelFileDescriptor>> so resize_out_vec
// can initialize all elements to None (it requires Default
// and ParcelFileDescriptor doesn't implement that)
return "Option<" + m[element_type_name] + ">";
} else {
return m[element_type_name];
}
}
return m[element_type_name];
}
if (TypeIsInterface(element_type, typenames)) {
return "binder::Strong<dyn " + GetRawRustName(element_type) + ">";
}
return GetRawRustName(element_type);
}
} // namespace
std::string ConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value) {
auto rust_value = ConstantValueDecoratorInternal(type, raw_value, false);
if (type.IsNullable()) {
return "Some(" + rust_value + ")";
}
return rust_value;
}
std::string ConstantValueDecoratorRef(const AidlTypeSpecifier& type, const std::string& raw_value) {
auto rust_value = ConstantValueDecoratorInternal(type, raw_value, true);
if (type.IsNullable()) {
return "Some(" + rust_value + ")";
}
return rust_value;
}
std::string RustNameOf(const AidlTypeSpecifier& type, const AidlTypenames& typenames,
StorageMode mode) {
std::string rust_name;
if (type.IsArray() || typenames.IsList(type)) {
StorageMode element_mode;
if (mode == StorageMode::OUT_ARGUMENT || mode == StorageMode::DEFAULT_VALUE) {
// Elements need to have Default for resize_out_vec()
element_mode = StorageMode::DEFAULT_VALUE;
} else {
element_mode = StorageMode::VALUE;
}
rust_name = GetRustName(type, typenames, element_mode);
if (type.IsNullable() && rust_name == "String") {
// The mapping for nullable string arrays is
// optional<vector<optional<string>>> in the NDK,
// so we do the same
rust_name = "Option<" + rust_name + ">";
}
if (mode == StorageMode::UNSIZED_ARGUMENT) {
rust_name = "[" + rust_name + "]";
} else {
rust_name = "Vec<" + rust_name + ">";
}
} else {
rust_name = GetRustName(type, typenames, mode);
}
if (mode == StorageMode::IN_ARGUMENT || mode == StorageMode::UNSIZED_ARGUMENT) {
// If this is a nullable input argument, put the reference inside the option,
// e.g., `Option<&str>` instead of `&Option<str>`
rust_name = "&" + rust_name;
}
if (type.IsNullable() ||
// Some types don't implement Default, so we wrap them
// in Option, which defaults to None
(!TypeHasDefault(type, typenames) &&
(mode == StorageMode::DEFAULT_VALUE || mode == StorageMode::OUT_ARGUMENT ||
mode == StorageMode::PARCELABLE_FIELD))) {
rust_name = "Option<" + rust_name + ">";
}
if (mode == StorageMode::OUT_ARGUMENT || mode == StorageMode::INOUT_ARGUMENT) {
rust_name = "&mut " + rust_name;
}
return rust_name;
}
StorageMode ArgumentStorageMode(const AidlArgument& arg, const AidlTypenames& typenames) {
if (arg.IsOut()) {
return arg.IsIn() ? StorageMode::INOUT_ARGUMENT : StorageMode::OUT_ARGUMENT;
}
const auto typeName = arg.GetType().GetName();
const auto definedType = typenames.TryGetDefinedType(typeName);
const bool isEnum = definedType && definedType->AsEnumDeclaration() != nullptr;
const bool isPrimitive = AidlTypenames::IsPrimitiveTypename(typeName);
if (typeName == "String" || arg.GetType().IsArray() || typenames.IsList(arg.GetType())) {
return StorageMode::UNSIZED_ARGUMENT;
} else if (!(isPrimitive || isEnum)) {
return StorageMode::IN_ARGUMENT;
} else {
return StorageMode::VALUE;
}
}
ReferenceMode ArgumentReferenceMode(const AidlArgument& arg, const AidlTypenames& typenames) {
auto arg_mode = ArgumentStorageMode(arg, typenames);
switch (arg_mode) {
case StorageMode::IN_ARGUMENT:
if (arg.GetType().IsNullable()) {
// &Option<T> => Option<&T>
return ReferenceMode::AS_REF;
} else {
return ReferenceMode::REF;
}
case StorageMode::OUT_ARGUMENT:
case StorageMode::INOUT_ARGUMENT:
return ReferenceMode::MUT_REF;
case StorageMode::UNSIZED_ARGUMENT:
if (arg.GetType().IsNullable()) {
// &Option<String> => Option<&str>
// &Option<Vec<T>> => Option<&[T]>
return ReferenceMode::AS_DEREF;
} else {
return ReferenceMode::REF;
}
default:
return ReferenceMode::VALUE;
}
}
std::string TakeReference(ReferenceMode ref_mode, const std::string& name) {
switch (ref_mode) {
case ReferenceMode::REF:
return "&" + name;
case ReferenceMode::MUT_REF:
return "&mut " + name;
case ReferenceMode::AS_REF:
return name + ".as_ref()";
case ReferenceMode::AS_DEREF:
return name + ".as_deref()";
default:
return name;
}
}
bool TypeIsInterface(const AidlTypeSpecifier& type, const AidlTypenames& typenames) {
const auto definedType = typenames.TryGetDefinedType(type.GetName());
return definedType != nullptr && definedType->AsInterface() != nullptr;
}
bool TypeHasDefault(const AidlTypeSpecifier& type, const AidlTypenames& typenames) {
if (type.IsArray() || typenames.IsList(type)) {
return true;
}
// Already an Option<T>
if (type.IsNullable()) {
return true;
}
const string& aidl_name = type.GetName();
if (aidl_name == "IBinder") {
return false;
}
if (aidl_name == "ParcelFileDescriptor") {
return false;
}
// Strong<dyn IFoo> values don't implement Default
if (TypeIsInterface(type, typenames)) {
return false;
}
return true;
}
} // namespace rust
} // namespace aidl
} // namespace android