1 /*
2 * Copyright (C) 2019 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 <hidl-util/FQName.h>
18 #include <string>
19
20 #include "AidlHelper.h"
21 #include "ArrayType.h"
22 #include "EnumType.h"
23 #include "FmqType.h"
24 #include "NamedType.h"
25 #include "Type.h"
26 #include "VectorType.h"
27
28 namespace android {
29
getPlaceholderType(const std::string & type)30 static std::string getPlaceholderType(const std::string& type) {
31 return "IBinder /* FIXME: " + type + " */";
32 }
33
34 static const std::map<std::string, ReplacedTypeInfo> kReplacedTypes{
35 {"android.hidl.safe_union@1.0::Monostate",
36 ReplacedTypeInfo{
37 "boolean", std::nullopt,
__anon8f90c5730102() 38 [](Formatter& out) { out << "// Nothing to translate for Monostate.\n"; }}},
39 };
40
getAidlReplacedType(const FQName & fqName)41 std::optional<const ReplacedTypeInfo> AidlHelper::getAidlReplacedType(const FQName& fqName) {
42 const auto& it = kReplacedTypes.find(fqName.string());
43 if (it != kReplacedTypes.end()) {
44 return it->second;
45 }
46 return std::nullopt;
47 }
48
getAidlType(const Type & type,const FQName & relativeTo)49 std::string AidlHelper::getAidlType(const Type& type, const FQName& relativeTo) {
50 if (type.isVector()) {
51 const VectorType& vec = static_cast<const VectorType&>(type);
52 return getAidlType(*vec.getElementType(), relativeTo) + "[]";
53 } else if (type.isArray()) {
54 const ArrayType& arr = static_cast<const ArrayType&>(type);
55 return getAidlType(*arr.getElementType(), relativeTo) + "[]";
56 } else if (type.isNamedType()) {
57 const NamedType& namedType = static_cast<const NamedType&>(type);
58 if (getAidlPackage(relativeTo) == getAidlPackage(namedType.fqName())) {
59 return getAidlName(namedType.fqName());
60 } else {
61 std::optional<const ReplacedTypeInfo> type = getAidlReplacedType(namedType.fqName());
62 if (type) {
63 notes() << "Replacing type " << namedType.fqName().string() << " with "
64 << type.value().aidlReplacedType << ".\n";
65 return type.value().aidlReplacedType;
66 }
67 std::optional<std::string> name = getAidlFQName(namedType.fqName()).value();
68 if (name) {
69 return name.value();
70 } else {
71 LOG(FATAL) << "Failed to resolve Aidl FQName: " << namedType.fqName().string();
72 return "";
73 }
74 }
75 } else if (type.isMemory()) {
76 return getPlaceholderType("memory");
77 } else if (type.isFmq()) {
78 const FmqType& fmq = static_cast<const FmqType&>(type);
79 return getPlaceholderType(fmq.templatedTypeName() + "<" +
80 getAidlType(*fmq.getElementType(), relativeTo) + ">");
81 } else if (type.isPointer()) {
82 return getPlaceholderType("pointer");
83 } else if (type.isEnum()) {
84 // enum type goes to the primitive java type in HIDL, but AIDL should use
85 // the enum type name itself
86 return type.definedName();
87 } else if (type.isBitField()) {
88 const BitFieldType& bitfield = static_cast<const BitFieldType&>(type);
89 return getAidlType(*bitfield.getElementType(), relativeTo);
90 } else if (type.getJavaType() == "short") {
91 notes() << relativeTo.name()
92 << ": Consider replacing char with int if signed integer is desired\n";
93 return "char";
94 } else {
95 return type.getJavaType();
96 }
97 }
98
99 } // namespace android
100