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,
__anon4050bd8c0102() 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,AidlBackend backend)49 std::string AidlHelper::getAidlType(const Type& type, const FQName& relativeTo,
50                                     AidlBackend backend) {
51     if (type.isVector()) {
52         const VectorType& vec = static_cast<const VectorType&>(type);
53         return getAidlType(*vec.getElementType(), relativeTo) + "[]";
54     } else if (type.isArray()) {
55         const ArrayType& arr = static_cast<const ArrayType&>(type);
56         auto sizes = arr.getConstantExpressions();
57         CHECK(sizes.size() > 0) << "Failed to get array dimensions for " << arr.definedName();
58         std::string typeStr = getAidlType(*arr.getElementType(), relativeTo);
59         for (const auto& size : sizes) {
60             typeStr += "[" + size->value() + "]";
61         }
62         return typeStr;
63     } else if (type.isNamedType()) {
64         const NamedType& namedType = static_cast<const NamedType&>(type);
65         if (getAidlPackage(relativeTo) == getAidlPackage(namedType.fqName())) {
66             return getAidlName(namedType.fqName(), backend);
67         } else {
68             std::optional<const ReplacedTypeInfo> type = getAidlReplacedType(namedType.fqName());
69             if (type) {
70                 notes() << "Replacing type " << namedType.fqName().string() << " with "
71                         << type.value().aidlReplacedType << ".\n";
72                 return type.value().aidlReplacedType;
73             }
74             std::optional<std::string> name = getAidlFQName(namedType.fqName()).value();
75             if (name) {
76                 return name.value();
77             } else {
78                 LOG(FATAL) << "Failed to resolve Aidl FQName: " << namedType.fqName().string();
79                 return "";
80             }
81         }
82     } else if (type.isMemory()) {
83         return getPlaceholderType("memory");
84     } else if (type.isFmq()) {
85         const FmqType& fmq = static_cast<const FmqType&>(type);
86         return getPlaceholderType(fmq.templatedTypeName() + "<" +
87                                   getAidlType(*fmq.getElementType(), relativeTo) + ">");
88     } else if (type.isPointer()) {
89         return getPlaceholderType("pointer");
90     } else if (type.isEnum()) {
91         // enum type goes to the primitive java type in HIDL, but AIDL should use
92         // the enum type name itself
93         return type.definedName();
94     } else if (type.isBitField()) {
95         const BitFieldType& bitfield = static_cast<const BitFieldType&>(type);
96         return getAidlType(*bitfield.getElementType(), relativeTo);
97     } else if (type.getJavaType() == "short") {
98         notes() << relativeTo.name()
99                 << ": Consider replacing char with int if signed integer is desired\n";
100         return "char";
101     } else {
102         return type.getJavaType();
103     }
104 }
105 
106 }  // namespace android
107