• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
StandardRead(const std::string & name)71 static std::function<void(const CodeGeneratorContext& c)> StandardRead(const std::string& name) {
72   return [name](const CodeGeneratorContext& c) {
73     c.writer << name << "(" << c.parcel << ", " << c.var << ")";
74   };
75 }
StandardWrite(const std::string & name)76 static std::function<void(const CodeGeneratorContext& c)> StandardWrite(const std::string& name) {
77   return [name](const CodeGeneratorContext& c) {
78     c.writer << name << "(" << c.parcel << ", " << c.var << ")";
79   };
80 }
81 
PrimitiveType(const std::string & cpp_name,const std::string & pretty_name)82 TypeInfo PrimitiveType(const std::string& cpp_name, const std::string& pretty_name) {
83   return TypeInfo{
84       .raw =
85           TypeInfo::Aspect{
86               .cpp_name = cpp_name,
87               .value_is_cheap = true,
88               .read_func = StandardRead("AParcel_read" + pretty_name),
89               .write_func = StandardWrite("AParcel_write" + pretty_name),
90           },
91       .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
92           .cpp_name = "std::vector<" + cpp_name + ">",
93           .value_is_cheap = false,
94           .read_func = StandardRead("::ndk::AParcel_readVector"),
95           .write_func = StandardWrite("::ndk::AParcel_writeVector"),
96       }),
97       .nullable = nullptr,
98       .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
99           .cpp_name = "std::optional<std::vector<" + cpp_name + ">>",
100           .value_is_cheap = false,
101           .read_func = StandardRead("::ndk::AParcel_readVector"),
102           .write_func = StandardWrite("::ndk::AParcel_writeVector"),
103       }),
104   };
105 }
106 
InterfaceTypeInfo(const AidlInterface & type)107 TypeInfo InterfaceTypeInfo(const AidlInterface& type) {
108   const std::string clazz = NdkFullClassName(type, cpp::ClassNames::INTERFACE);
109 
110   return TypeInfo{
111       .raw =
112           TypeInfo::Aspect{
113               .cpp_name = "std::shared_ptr<" + clazz + ">",
114               .value_is_cheap = false,
115               // TODO(b/111445392): these should be non-null
116               .read_func = StandardRead(clazz + "::readFromParcel"),
117               .write_func = StandardWrite(clazz + "::writeToParcel"),
118           },
119       .array = nullptr,
120       .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
121           .cpp_name = "std::shared_ptr<" + clazz + ">",
122           .value_is_cheap = false,
123           .read_func = StandardRead(clazz + "::readFromParcel"),
124           .write_func = StandardWrite(clazz + "::writeToParcel"),
125       }),
126       .nullable_array = nullptr,
127   };
128 }
129 
ParcelableTypeInfo(const AidlParcelable & type)130 TypeInfo ParcelableTypeInfo(const AidlParcelable& type) {
131   const std::string clazz = NdkFullClassName(type, cpp::ClassNames::BASE);
132 
133   return TypeInfo{
134       .raw =
135           TypeInfo::Aspect{
136               .cpp_name = clazz,
137               .value_is_cheap = false,
138               .read_func =
139                   [](const CodeGeneratorContext& c) {
140                     c.writer << "(" << c.var << ")->readFromParcel(" << c.parcel << ")";
141                   },
142               .write_func =
143                   [](const CodeGeneratorContext& c) {
144                     c.writer << "(" << c.var << ").writeToParcel(" << c.parcel << ")";
145                   },
146           },
147       .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
148           .cpp_name = "std::vector<" + clazz + ">",
149           .value_is_cheap = false,
150           .read_func = StandardRead("::ndk::AParcel_readVector"),
151           .write_func = StandardWrite("::ndk::AParcel_writeVector"),
152       }),
153       .nullable = nullptr,
154       .nullable_array = nullptr,
155   };
156 }
157 
158 // map from AIDL built-in type name to the corresponding Ndk type name
159 static map<std::string, TypeInfo> kNdkTypeInfoMap = {
160     {"void", TypeInfo{{"void", true, nullptr, nullptr}, nullptr, nullptr, nullptr}},
161     {"boolean", PrimitiveType("bool", "Bool")},
162     {"byte", PrimitiveType("int8_t", "Byte")},
163     {"char", PrimitiveType("char16_t", "Char")},
164     {"int", PrimitiveType("int32_t", "Int32")},
165     {"long", PrimitiveType("int64_t", "Int64")},
166     {"float", PrimitiveType("float", "Float")},
167     {"double", PrimitiveType("double", "Double")},
168     {"String",
169      TypeInfo{
170          .raw =
171              TypeInfo::Aspect{
172                  .cpp_name = "std::string",
173                  .value_is_cheap = false,
174                  .read_func = StandardRead("::ndk::AParcel_readString"),
175                  .write_func = StandardWrite("::ndk::AParcel_writeString"),
176              },
177          .array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
178              .cpp_name = "std::vector<std::string>",
179              .value_is_cheap = false,
180              .read_func = StandardRead("::ndk::AParcel_readVector"),
181              .write_func = StandardWrite("::ndk::AParcel_writeVector"),
182          }),
183          .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
184              .cpp_name = "std::optional<std::string>",
185              .value_is_cheap = false,
186              .read_func = StandardRead("::ndk::AParcel_readString"),
187              .write_func = StandardWrite("::ndk::AParcel_writeString"),
188          }),
189          .nullable_array = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
190              .cpp_name = "std::optional<std::vector<std::optional<std::string>>>",
191              .value_is_cheap = false,
192              .read_func = StandardRead("::ndk::AParcel_readVector"),
193              .write_func = StandardWrite("::ndk::AParcel_writeVector"),
194          }),
195      }},
196     // TODO(b/111445392) {"List", ""},
197     // TODO(b/111445392) {"Map", ""},
198     {"IBinder",
199      TypeInfo{
200          .raw =
201              TypeInfo::Aspect{
202                  .cpp_name = "::ndk::SpAIBinder",
203                  .value_is_cheap = false,
204                  .read_func = StandardRead("::ndk::AParcel_readRequiredStrongBinder"),
205                  .write_func = StandardRead("::ndk::AParcel_writeRequiredStrongBinder"),
206              },
207          .array = nullptr,
208          .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
209              .cpp_name = "::ndk::SpAIBinder",
210              .value_is_cheap = false,
211              .read_func = StandardRead("::ndk::AParcel_readNullableStrongBinder"),
212              .write_func = StandardRead("::ndk::AParcel_writeNullableStrongBinder"),
213          }),
214          .nullable_array = nullptr,
215      }},
216     // TODO(b/111445392) {"FileDescriptor", ""},
217     {"ParcelFileDescriptor",
218      TypeInfo{
219          .raw =
220              TypeInfo::Aspect{
221                  .cpp_name = "::ndk::ScopedFileDescriptor",
222                  .value_is_cheap = false,
223                  .read_func = StandardRead("::ndk::AParcel_readRequiredParcelFileDescriptor"),
224                  .write_func = StandardRead("::ndk::AParcel_writeRequiredParcelFileDescriptor"),
225              },
226          .array = nullptr,
227          .nullable = std::shared_ptr<TypeInfo::Aspect>(new TypeInfo::Aspect{
228              .cpp_name = "::ndk::ScopedFileDescriptor",
229              .value_is_cheap = false,
230              .read_func = StandardRead("::ndk::AParcel_readNullableParcelFileDescriptor"),
231              .write_func = StandardRead("::ndk::AParcel_writeNullableParcelFileDescriptor"),
232          }),
233          .nullable_array = nullptr,
234      }},
235     // TODO(b/111445392) {"CharSequence", ""},
236 };
237 
GetTypeAspect(const AidlTypenames & types,const AidlTypeSpecifier & aidl)238 static TypeInfo::Aspect GetTypeAspect(const AidlTypenames& types, const AidlTypeSpecifier& aidl) {
239   CHECK(aidl.IsResolved()) << aidl.ToString();
240 
241   const string aidl_name = aidl.GetName();
242 
243   // TODO(b/112664205): this is okay for some types
244   AIDL_FATAL_IF(aidl.IsGeneric(), aidl) << aidl.ToString();
245 
246   TypeInfo info;
247   if (AidlTypenames::IsBuiltinTypename(aidl_name)) {
248     auto it = kNdkTypeInfoMap.find(aidl_name);
249     CHECK(it != kNdkTypeInfoMap.end());
250     info = it->second;
251   } else {
252     const AidlDefinedType* type = types.TryGetDefinedType(aidl_name);
253     AIDL_FATAL_IF(type == nullptr, aidl_name) << "Unrecognized type.";
254 
255     if (type->AsInterface() != nullptr) {
256       info = InterfaceTypeInfo(*type->AsInterface());
257     } else if (type->AsParcelable() != nullptr) {
258       info = ParcelableTypeInfo(*type->AsParcelable());
259     } else {
260       AIDL_FATAL(aidl_name) << "Unrecognized type";
261     }
262   }
263 
264   if (aidl.IsArray()) {
265     if (aidl.IsNullable()) {
266       AIDL_FATAL_IF(info.nullable_array == nullptr, aidl) << "Unsupported type in NDK Backend.";
267       return *info.nullable_array;
268     }
269     AIDL_FATAL_IF(info.array == nullptr, aidl) << "Unsupported type in NDK Backend.";
270     return *info.array;
271   }
272 
273   if (aidl.IsNullable()) {
274     AIDL_FATAL_IF(info.nullable == nullptr, aidl) << "Unsupported type in NDK Backend.";
275     return *info.nullable;
276   }
277 
278   return info.raw;
279 }
280 
NdkFullClassName(const AidlDefinedType & type,cpp::ClassNames name)281 std::string NdkFullClassName(const AidlDefinedType& type, cpp::ClassNames name) {
282   std::vector<std::string> pieces = {"::aidl"};
283   std::vector<std::string> package = type.GetSplitPackage();
284   pieces.insert(pieces.end(), package.begin(), package.end());
285   pieces.push_back(cpp::ClassName(type, name));
286 
287   return Join(pieces, "::");
288 }
289 
NdkNameOf(const AidlTypenames & types,const AidlTypeSpecifier & aidl,StorageMode mode)290 std::string NdkNameOf(const AidlTypenames& types, const AidlTypeSpecifier& aidl, StorageMode mode) {
291   TypeInfo::Aspect aspect = GetTypeAspect(types, aidl);
292 
293   switch (mode) {
294     case StorageMode::STACK:
295       return aspect.cpp_name;
296     case StorageMode::ARGUMENT:
297       if (aspect.value_is_cheap) {
298         return aspect.cpp_name;
299       } else {
300         return "const " + aspect.cpp_name + "&";
301       }
302     case StorageMode::OUT_ARGUMENT:
303       return aspect.cpp_name + "*";
304     default:
305       AIDL_FATAL(aidl.GetName()) << "Unrecognized mode type: " << static_cast<int>(mode);
306   }
307 }
308 
WriteToParcelFor(const CodeGeneratorContext & c)309 void WriteToParcelFor(const CodeGeneratorContext& c) {
310   TypeInfo::Aspect aspect = GetTypeAspect(c.types, c.type);
311   aspect.write_func(c);
312 }
313 
ReadFromParcelFor(const CodeGeneratorContext & c)314 void ReadFromParcelFor(const CodeGeneratorContext& c) {
315   TypeInfo::Aspect aspect = GetTypeAspect(c.types, c.type);
316   aspect.read_func(c);
317 }
318 
NdkArgList(const AidlTypenames & types,const AidlMethod & method,std::function<std::string (const std::string & type,const std::string & name,bool isOut)> formatter)319 std::string NdkArgList(
320     const AidlTypenames& types, const AidlMethod& method,
321     std::function<std::string(const std::string& type, const std::string& name, bool isOut)>
322         formatter) {
323   std::vector<std::string> method_arguments;
324   for (const auto& a : method.GetArguments()) {
325     StorageMode mode = a->IsOut() ? StorageMode::OUT_ARGUMENT : StorageMode::ARGUMENT;
326     std::string type = NdkNameOf(types, a->GetType(), mode);
327     std::string name = cpp::BuildVarName(*a);
328     method_arguments.emplace_back(formatter(type, name, a->IsOut()));
329   }
330 
331   if (method.GetType().GetName() != "void") {
332     std::string type = NdkNameOf(types, method.GetType(), StorageMode::OUT_ARGUMENT);
333     std::string name = "_aidl_return";
334     method_arguments.emplace_back(formatter(type, name, true));
335   }
336 
337   return Join(method_arguments, ", ");
338 }
339 
NdkMethodDecl(const AidlTypenames & types,const AidlMethod & method,const std::string & clazz)340 std::string NdkMethodDecl(const AidlTypenames& types, const AidlMethod& method,
341                           const std::string& clazz) {
342   std::string class_prefix = clazz.empty() ? "" : (clazz + "::");
343   return "::ndk::ScopedAStatus " + class_prefix + method.GetName() + "(" +
344          NdkArgList(types, method, FormatArgForDecl) + ")";
345 }
346 
347 }  // namespace ndk
348 }  // namespace aidl
349 }  // namespace android
350