fix NDK List<T>
The NDK backend supports List<T> by treating it like an array. But
it's been ignoring annotations attached to List.
For example, `@nullable List<String>` was converted to `String[]` and
then translated to `vector<string>`, which should be
`optional<vector<optional<string>>>`.
Fixes: 203144721
Test: aidl_unittests
Test: aidl_integration_test
Change-Id: I5b8099db30fbbf41936aad7f58da25b74dcc0191
diff --git a/aidl_to_ndk.cpp b/aidl_to_ndk.cpp
index 0df4a6a..4bf1c1f 100644
--- a/aidl_to_ndk.cpp
+++ b/aidl_to_ndk.cpp
@@ -360,6 +360,29 @@
}},
};
+static TypeInfo GetTypeInfo(const AidlTypenames& types, const AidlTypeSpecifier& aidl) {
+ auto& aidl_name = aidl.GetName();
+
+ if (AidlTypenames::IsBuiltinTypename(aidl_name)) {
+ auto it = kNdkTypeInfoMap.find(aidl_name);
+ AIDL_FATAL_IF(it == kNdkTypeInfoMap.end(), aidl_name);
+ return it->second;
+ }
+ const AidlDefinedType* type = types.TryGetDefinedType(aidl_name);
+ AIDL_FATAL_IF(type == nullptr, aidl_name) << "Unrecognized type.";
+
+ if (const AidlInterface* intf = type->AsInterface(); intf != nullptr) {
+ return InterfaceTypeInfo(*intf);
+ } else if (const AidlParcelable* parcelable = type->AsParcelable(); parcelable != nullptr) {
+ return ParcelableTypeInfo(*parcelable, aidl, types);
+ } else if (const AidlEnumDeclaration* enum_decl = type->AsEnumDeclaration();
+ enum_decl != nullptr) {
+ return EnumDeclarationTypeInfo(*enum_decl);
+ } else {
+ AIDL_FATAL(aidl_name) << "Unrecognized type";
+ }
+}
+
static TypeInfo::Aspect GetTypeAspect(const AidlTypenames& types, const AidlTypeSpecifier& aidl) {
AIDL_FATAL_IF(!aidl.IsResolved(), aidl) << aidl.ToString();
auto& aidl_name = aidl.GetName();
@@ -371,36 +394,16 @@
AIDL_FATAL_IF(!aidl.IsGeneric(), aidl) << "List must be generic type.";
AIDL_FATAL_IF(aidl.GetTypeParameters().size() != 1, aidl)
<< "List can accept only one type parameter.";
- const auto& type_param = aidl.GetTypeParameters()[0];
+ const auto& type_param = *aidl.GetTypeParameters()[0];
// TODO(b/136048684) AIDL doesn't support nested type parameter yet.
- AIDL_FATAL_IF(type_param->IsGeneric(), aidl) << "AIDL doesn't support nested type parameter";
+ AIDL_FATAL_IF(type_param.IsGeneric(), aidl) << "AIDL doesn't support nested type parameter";
- auto array_type =
- types.MakeResolvedType(AIDL_LOCATION_HERE, type_param->GetName(), /*isArray=*/true);
- return GetTypeAspect(types, *array_type);
- }
-
- if (AidlTypenames::IsBuiltinTypename(aidl_name)) {
- auto it = kNdkTypeInfoMap.find(aidl_name);
- AIDL_FATAL_IF(it == kNdkTypeInfoMap.end(), aidl_name);
- info = it->second;
+ info = GetTypeInfo(types, type_param);
} else {
- const AidlDefinedType* type = types.TryGetDefinedType(aidl_name);
- AIDL_FATAL_IF(type == nullptr, aidl_name) << "Unrecognized type.";
-
- if (const AidlInterface* intf = type->AsInterface(); intf != nullptr) {
- info = InterfaceTypeInfo(*intf);
- } else if (const AidlParcelable* parcelable = type->AsParcelable(); parcelable != nullptr) {
- info = ParcelableTypeInfo(*parcelable, aidl, types);
- } else if (const AidlEnumDeclaration* enum_decl = type->AsEnumDeclaration();
- enum_decl != nullptr) {
- info = EnumDeclarationTypeInfo(*enum_decl);
- } else {
- AIDL_FATAL(aidl_name) << "Unrecognized type";
- }
+ info = GetTypeInfo(types, aidl);
}
- if (aidl.IsArray()) {
+ if (aidl.IsArray() || aidl_name == "List") {
if (aidl.IsNullable()) {
AIDL_FATAL_IF(info.nullable_array == nullptr, aidl)
<< "Unsupported type in NDK Backend: " << aidl.ToString();