1 /*
2  * Copyright (C) 2018, 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  * limitations under the License.
13  */
14 
15 #include "aidl_to_ndk.h"
16 #include "aidl_language.h"
17 #include "aidl_to_cpp_common.h"
18 #include "logging.h"
19 #include "os.h"
20 
21 #include <android-base/strings.h>
22 
23 #include <functional>
24 
25 using ::android::base::Join;
26 
27 namespace android {
28 namespace aidl {
29 namespace ndk {
30 
NdkHeaderFile(const AidlDefinedType & defined_type,cpp::ClassNames name,bool use_os_sep)31 std::string NdkHeaderFile(const AidlDefinedType& defined_type, cpp::ClassNames name,
32                           bool use_os_sep) {
33   char seperator = (use_os_sep) ? OS_PATH_SEPARATOR : '/';
34   return std::string("aidl") + seperator + cpp::HeaderFile(defined_type, name, use_os_sep);
35 }
36 
37 // This represents a type in AIDL (e.g. 'String' which can be referenced in multiple ways)
38 struct TypeInfo {
39   struct Aspect {
40     // name of the type in C++ output
41     std::string cpp_name;
42     // whether to prefer 'value type' over 'const&'
43     bool value_is_cheap;
44 
45     std::function<void(const CodeGeneratorContext& c)> read_func;
46     std::function<void(const CodeGeneratorContext& c)> write_func;
47   };
48 
49   // e.g. 'String'
50   Aspect raw;
51 
52   // e.g. 'String[]'
53   std::shared_ptr<Aspect> array;
54 
55   // note: Nullable types do not exist in Java. For most Java types, the type is split into a
56   // nullable and non-nullable variant. This is because C++ types are more usually non-nullable, but
57   // everything in Java is non-nullable. This does mean that some Java interfaces may have to have
58   // '@nullable' added to them in order to function as expected w/ the NDK. It also means that some
59   // transactions will be allowed in Java which are not allowed in C++. However, in Java, if a null
60   // is ignored, it will just result in a NullPointerException and be delivered to the other side.
61   // C++ does not have this same capacity (in Android), and so instead, we distinguish nullability
62   // in the type system.
63 
64   // e.g. '@nullable String'
65   std::shared_ptr<Aspect> nullable;
66 
67   // e.g. '@nullable String[]'
68   std::shared_ptr<Aspect> nullable_array;
69 };
70 
ConstantValueDecorator(const AidlTypeSpecifier & type,const std::string & raw_value)71 std::string ConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value) {
72   if (type.GetName() == "long" && !type.IsArray()) {
73     return raw_value + "L";
74   }
75 
76   return raw_value;
77 };
78 
StandardRead(const std::string & name)79 static std::function<void(const CodeGeneratorContext& c)> StandardRead(const std::string& name) {
80   return [name](const CodeGeneratorContext& c) {
81     c.writer << name << "(" << c.parcel << ", " << c.var << ")";
82   };
83 }
StandardWrite(const std::string & name)84 static std::function<void(const CodeGeneratorContext& c)> StandardWrite(const std::string& name) {
85   return [name](const CodeGeneratorContext& c) {
86     c.writer << name << "(" << c.parcel << ", " << c.var << ")";
87   };
88 }
89 
PrimitiveType(const std::string & cpp_name,const std::string & pretty_name,const std::optional<std::string> & cpp_name_for_array_opt=std::nullopt)90 TypeInfo PrimitiveType(const std::string& cpp_name, const std::string& pretty_name,
91                        const std::optional<std::string>& cpp_name_for_array_opt = std::nullopt) {
92   std::string cpp_name_for_array = cpp_name_for_array_opt.value_or(cpp_name);
93   return TypeInfo{
94       .raw =
95           TypeInfo::Aspect{
96               .cpp_name = cpp_name,
97               .value_is_cheap = true,
98               .read_func = StandardRead("AParcel_read" + pretty_name),
99               .write_func = StandardWrite("AParcel_write" + pretty_name),
100           },
101       .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
102           .cpp_name = "std::vector<" + cpp_name_for_array + ">",
103           .value_is_cheap = false,
104           .read_func = StandardRead("::ndk::AParcel_readVector"),
105           .write_func = StandardWrite("::ndk::AParcel_writeVector"),
106       }),
107       .nullable = nullptr,
108       .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
109           .cpp_name = "std::optional<std::vector<" + cpp_name_for_array + ">>",
110           .value_is_cheap = false,
111           .read_func = StandardRead("::ndk::AParcel_readVector"),
112           .write_func = StandardWrite("::ndk::AParcel_writeVector"),
113       }),
114   };
115 }
116 
InterfaceTypeInfo(const AidlInterface & type)117 TypeInfo InterfaceTypeInfo(const AidlInterface& type) {
118   const std::string clazz = NdkFullClassName(type, cpp::ClassNames::INTERFACE);
119 
120   return TypeInfo{
121       .raw =
122           TypeInfo::Aspect{
123               .cpp_name = "std::shared_ptr<" + clazz + ">",
124               .value_is_cheap = false,
125               .read_func = StandardRead(clazz + "::readFromParcel"),
126               .write_func = StandardWrite(clazz + "::writeToParcel"),
127           },
128       .array = nullptr,
129       .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
130           .cpp_name = "std::shared_ptr<" + clazz + ">",
131           .value_is_cheap = false,
132           .read_func = StandardRead(clazz + "::readFromParcel"),
133           .write_func = StandardWrite(clazz + "::writeToParcel"),
134       }),
135       .nullable_array = nullptr,
136   };
137 }
138 
ParcelableTypeInfo(const AidlParcelable & type)139 TypeInfo ParcelableTypeInfo(const AidlParcelable& type) {
140   const std::string clazz = NdkFullClassName(type, cpp::ClassNames::RAW);
141 
142   return TypeInfo{
143       .raw =
144           TypeInfo::Aspect{
145               .cpp_name = clazz,
146               .value_is_cheap = false,
147               .read_func = StandardRead("::ndk::AParcel_readParcelable"),
148               .write_func = StandardWrite("::ndk::AParcel_writeParcelable"),
149           },
150       .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
151           .cpp_name = "std::vector<" + clazz + ">",
152           .value_is_cheap = false,
153           .read_func = StandardRead("::ndk::AParcel_readVector"),
154           .write_func = StandardWrite("::ndk::AParcel_writeVector"),
155       }),
156       .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
157           .cpp_name = "std::optional<" + clazz + ">",
158           .value_is_cheap = false,
159           .read_func = StandardRead("::ndk::AParcel_readNullableParcelable"),
160           .write_func = StandardWrite("::ndk::AParcel_writeNullableParcelable"),
161       }),
162       .nullable_array = nullptr,
163   };
164 }
165 
EnumDeclarationTypeInfo(const AidlEnumDeclaration & enum_decl)166 TypeInfo EnumDeclarationTypeInfo(const AidlEnumDeclaration& enum_decl) {
167   const std::string clazz = NdkFullClassName(enum_decl, cpp::ClassNames::RAW);
168 
169   static map<std::string, std::string> kAParcelTypeNameMap = {
170       {"byte", "Byte"},
171       {"int", "Int32"},
172       {"long", "Int64"},
173   };
174   auto aparcel_name_it = kAParcelTypeNameMap.find(enum_decl.GetBackingType().GetName());
175   CHECK(aparcel_name_it != kAParcelTypeNameMap.end());
176   const std::string aparcel_name = aparcel_name_it->second;
177 
178   const std::string backing_type_name =
179       NdkNameOf(AidlTypenames(), enum_decl.GetBackingType(), StorageMode::STACK);
180 
181   return TypeInfo{
182       .raw = TypeInfo::Aspect{
183           .cpp_name = clazz,
184           .value_is_cheap = true,
185           .read_func =
186               [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
187                 c.writer << "AParcel_read" << aparcel_name << "(" << c.parcel
188                          << ", reinterpret_cast<" << backing_type_name << "*>(" << c.var << "))";
189               },
190           .write_func =
191               [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
192                 c.writer << "AParcel_write" << aparcel_name << "(" << c.parcel << ", static_cast<"
193                          << backing_type_name << ">(" << c.var << "))";
194               },
195       },
196       .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
197           .cpp_name = "std::vector<" + clazz + ">",
198           .value_is_cheap = false,
199           .read_func =
200               [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
201                 c.writer << "AParcel_read" << aparcel_name << "Array(" << c.parcel
202                          << ", static_cast<void*>(" << c.var
203                          << "), ndk::AParcel_stdVectorAllocator<" << backing_type_name << ">)";
204               },
205           .write_func =
206               [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
207                 c.writer << "AParcel_write" << aparcel_name << "Array(" << c.parcel
208                          << ", reinterpret_cast<const " << backing_type_name << "*>(" << c.var
209                          << ".data()), " << c.var << ".size())";
210               },
211       }),
212       .nullable = nullptr,
213       .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
214           .cpp_name = "std::optional<std::vector<" + clazz + ">>",
215           .value_is_cheap = false,
216           .read_func =
217               [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
218                 c.writer << "AParcel_read" << aparcel_name << "Array(" << c.parcel
219                          << ", static_cast<void*>(" << c.var
220                          << "), ndk::AParcel_nullableStdVectorAllocator<" << backing_type_name
221                          << ">)";
222               },
223           .write_func =
224               [aparcel_name, backing_type_name](const CodeGeneratorContext& c) {
225                 // If the var exists, use writeArray with data() and size().
226                 // Otherwise, use nullptr and -1.
227                 c.writer << "AParcel_write" << aparcel_name << "Array(" << c.parcel << ", ("
228                          << c.var << " ? reinterpret_cast<const " << backing_type_name << "*>("
229                          << c.var << "->data()) : nullptr)"
230                          << ", (" << c.var << " ? " << c.var << "->size() : -1))";
231               },
232       }),
233   };
234 }
235 
236 // map from AIDL built-in type name to the corresponding Ndk type info
237 static map<std::string, TypeInfo> kNdkTypeInfoMap = {
238     {"void", TypeInfo{{"void", true, nullptr, nullptr}, nullptr, nullptr, nullptr}},
239     {"boolean", PrimitiveType("bool", "Bool")},
240     {"byte", PrimitiveType("int8_t", "Byte", "uint8_t")},
241     {"char", PrimitiveType("char16_t", "Char")},
242     {"int", PrimitiveType("int32_t", "Int32")},
243     {"long", PrimitiveType("int64_t", "Int64")},
244     {"float", PrimitiveType("float", "Float")},
245     {"double", PrimitiveType("double", "Double")},
246     {"String",
247      TypeInfo{
248          .raw =
249              TypeInfo::Aspect{
250                  .cpp_name = "std::string",
251                  .value_is_cheap = false,
252                  .read_func = StandardRead("::ndk::AParcel_readString"),
253                  .write_func = StandardWrite("::ndk::AParcel_writeString"),
254              },
255          .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
256              .cpp_name = "std::vector<std::string>",
257              .value_is_cheap = false,
258              .read_func = StandardRead("::ndk::AParcel_readVector"),
259              .write_func = StandardWrite("::ndk::AParcel_writeVector"),
260          }),
261          .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
262              .cpp_name = "std::optional<std::string>",
263              .value_is_cheap = false,
264              .read_func = StandardRead("::ndk::AParcel_readString"),
265              .write_func = StandardWrite("::ndk::AParcel_writeString"),
266          }),
267          .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
268              .cpp_name = "std::optional<std::vector<std::optional<std::string>>>",
269              .value_is_cheap = false,
270              .read_func = StandardRead("::ndk::AParcel_readVector"),
271              .write_func = StandardWrite("::ndk::AParcel_writeVector"),
272          }),
273      }},
274     // TODO(b/136048684) {"Map", ""},
275     {"IBinder",
276      TypeInfo{
277          .raw =
278              TypeInfo::Aspect{
279                  .cpp_name = "::ndk::SpAIBinder",
280                  .value_is_cheap = false,
281                  .read_func = StandardRead("::ndk::AParcel_readRequiredStrongBinder"),
282                  .write_func = StandardRead("::ndk::AParcel_writeRequiredStrongBinder"),
283              },
284          .array = nullptr,
285          .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
286              .cpp_name = "::ndk::SpAIBinder",
287              .value_is_cheap = false,
288              .read_func = StandardRead("::ndk::AParcel_readNullableStrongBinder"),
289              .write_func = StandardRead("::ndk::AParcel_writeNullableStrongBinder"),
290          }),
291          .nullable_array = nullptr,
292      }},
293     {"ParcelFileDescriptor",
294      TypeInfo{
295          .raw =
296              TypeInfo::Aspect{
297                  .cpp_name = "::ndk::ScopedFileDescriptor",
298                  .value_is_cheap = false,
299                  .read_func = StandardRead("::ndk::AParcel_readRequiredParcelFileDescriptor"),
300                  .write_func = StandardRead("::ndk::AParcel_writeRequiredParcelFileDescriptor"),
301              },
302          .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
303              .cpp_name = "std::vector<::ndk::ScopedFileDescriptor>",
304              .value_is_cheap = false,
305              .read_func = StandardRead("::ndk::AParcel_readVector"),
306              .write_func = StandardWrite("::ndk::AParcel_writeVector"),
307          }),
308          .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
309              .cpp_name = "::ndk::ScopedFileDescriptor",
310              .value_is_cheap = false,
311              .read_func = StandardRead("::ndk::AParcel_readNullableParcelFileDescriptor"),
312              .write_func = StandardRead("::ndk::AParcel_writeNullableParcelFileDescriptor"),
313          }),
314          .nullable_array = nullptr,
315      }},
316 };
317 
GetTypeAspect(const AidlTypenames & types,const AidlTypeSpecifier & aidl)318 static TypeInfo::Aspect GetTypeAspect(const AidlTypenames& types, const AidlTypeSpecifier& aidl) {
319   CHECK(aidl.IsResolved()) << aidl.ToString();
320   auto& aidl_name = aidl.GetName();
321 
322   TypeInfo info;
323 
324   // TODO(b/136048684): For now, List<T> is converted to T[].(Both are using vector<T>)
325   if (aidl_name == "List") {
326     AIDL_FATAL_IF(!aidl.IsGeneric(), aidl) << "List must be generic type.";
327     AIDL_FATAL_IF(aidl.GetTypeParameters().size() != 1, aidl)
328         << "List can accept only one type parameter.";
329     auto& type_param = aidl.GetTypeParameters()[0];
330     // TODO(b/136048684) AIDL doesn't support nested type parameter yet.
331     AIDL_FATAL_IF(type_param->IsGeneric(), aidl) << "AIDL doesn't support nested type parameter";
332 
333     AidlTypeSpecifier array_type =
334         AidlTypeSpecifier(AIDL_LOCATION_HERE, type_param->GetUnresolvedName(), true /* isArray */,
335                           nullptr /* type_params */, aidl.GetComments());
336     if (!(array_type.Resolve(types) && array_type.CheckValid(types))) {
337       AIDL_FATAL(aidl) << "The type parameter is wrong.";
338     }
339     return GetTypeAspect(types, array_type);
340   }
341 
342   // All generic types should be handled above.
343   AIDL_FATAL_IF(aidl.IsGeneric(), aidl);
344 
345   if (AidlTypenames::IsBuiltinTypename(aidl_name)) {
346     auto it = kNdkTypeInfoMap.find(aidl_name);
347     CHECK(it != kNdkTypeInfoMap.end());
348     info = it->second;
349   } else {
350     const AidlDefinedType* type = types.TryGetDefinedType(aidl_name);
351     AIDL_FATAL_IF(type == nullptr, aidl_name) << "Unrecognized type.";
352 
353     if (const AidlInterface* intf = type->AsInterface(); intf != nullptr) {
354       info = InterfaceTypeInfo(*intf);
355     } else if (const AidlParcelable* parcelable = type->AsParcelable(); parcelable != nullptr) {
356       info = ParcelableTypeInfo(*parcelable);
357     } else if (const AidlEnumDeclaration* enum_decl = type->AsEnumDeclaration();
358                enum_decl != nullptr) {
359       info = EnumDeclarationTypeInfo(*enum_decl);
360     } else {
361       AIDL_FATAL(aidl_name) << "Unrecognized type";
362     }
363   }
364 
365   if (aidl.IsArray()) {
366     if (aidl.IsNullable()) {
367       AIDL_FATAL_IF(info.nullable_array == nullptr, aidl)
368           << "Unsupported type in NDK Backend: " << aidl.ToString();
369       return *info.nullable_array;
370     }
371     AIDL_FATAL_IF(info.array == nullptr, aidl)
372         << "Unsupported type in NDK Backend: " << aidl.ToString();
373     return *info.array;
374   }
375 
376   if (aidl.IsNullable()) {
377     AIDL_FATAL_IF(info.nullable == nullptr, aidl)
378         << "Unsupported type in NDK Backend: " << aidl.ToString();
379     return *info.nullable;
380   }
381 
382   return info.raw;
383 }
384 
NdkFullClassName(const AidlDefinedType & type,cpp::ClassNames name)385 std::string NdkFullClassName(const AidlDefinedType& type, cpp::ClassNames name) {
386   std::vector<std::string> pieces = {"::aidl"};
387   std::vector<std::string> package = type.GetSplitPackage();
388   pieces.insert(pieces.end(), package.begin(), package.end());
389   pieces.push_back(cpp::ClassName(type, name));
390 
391   return Join(pieces, "::");
392 }
393 
NdkNameOf(const AidlTypenames & types,const AidlTypeSpecifier & aidl,StorageMode mode)394 std::string NdkNameOf(const AidlTypenames& types, const AidlTypeSpecifier& aidl, StorageMode mode) {
395   TypeInfo::Aspect aspect = GetTypeAspect(types, aidl);
396 
397   switch (mode) {
398     case StorageMode::STACK:
399       return aspect.cpp_name;
400     case StorageMode::ARGUMENT:
401       if (aspect.value_is_cheap) {
402         return aspect.cpp_name;
403       } else {
404         return "const " + aspect.cpp_name + "&";
405       }
406     case StorageMode::OUT_ARGUMENT:
407       return aspect.cpp_name + "*";
408     default:
409       AIDL_FATAL(aidl.GetName()) << "Unrecognized mode type: " << static_cast<int>(mode);
410   }
411 }
412 
WriteToParcelFor(const CodeGeneratorContext & c)413 void WriteToParcelFor(const CodeGeneratorContext& c) {
414   TypeInfo::Aspect aspect = GetTypeAspect(c.types, c.type);
415   aspect.write_func(c);
416 }
417 
ReadFromParcelFor(const CodeGeneratorContext & c)418 void ReadFromParcelFor(const CodeGeneratorContext& c) {
419   TypeInfo::Aspect aspect = GetTypeAspect(c.types, c.type);
420   aspect.read_func(c);
421 }
422 
NdkArgList(const AidlTypenames & types,const AidlMethod & method,std::function<std::string (const std::string & type,const std::string & name,bool isOut)> formatter)423 std::string NdkArgList(
424     const AidlTypenames& types, const AidlMethod& method,
425     std::function<std::string(const std::string& type, const std::string& name, bool isOut)>
426         formatter) {
427   std::vector<std::string> method_arguments;
428   for (const auto& a : method.GetArguments()) {
429     StorageMode mode = a->IsOut() ? StorageMode::OUT_ARGUMENT : StorageMode::ARGUMENT;
430     std::string type = NdkNameOf(types, a->GetType(), mode);
431     std::string name = cpp::BuildVarName(*a);
432     method_arguments.emplace_back(formatter(type, name, a->IsOut()));
433   }
434 
435   if (method.GetType().GetName() != "void") {
436     std::string type = NdkNameOf(types, method.GetType(), StorageMode::OUT_ARGUMENT);
437     std::string name = "_aidl_return";
438     method_arguments.emplace_back(formatter(type, name, true));
439   }
440 
441   return Join(method_arguments, ", ");
442 }
443 
NdkMethodDecl(const AidlTypenames & types,const AidlMethod & method,const std::string & clazz)444 std::string NdkMethodDecl(const AidlTypenames& types, const AidlMethod& method,
445                           const std::string& clazz) {
446   std::string class_prefix = clazz.empty() ? "" : (clazz + "::");
447   return "::ndk::ScopedAStatus " + class_prefix + method.GetName() + "(" +
448          NdkArgList(types, method, FormatArgForDecl) + ")";
449 }
450 
451 }  // namespace ndk
452 }  // namespace aidl
453 }  // namespace android
454