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 "FmqType.h"
22 #include "NamedType.h"
23 #include "Type.h"
24 #include "VectorType.h"
25 
26 namespace android {
27 
getPlaceholderType(const std::string & type)28 static std::string getPlaceholderType(const std::string& type) {
29     return "IBinder /* FIXME: " + type + " */";
30 }
31 
getAidlType(const Type & type,const FQName & relativeTo)32 std::string AidlHelper::getAidlType(const Type& type, const FQName& relativeTo) {
33     if (type.isVector()) {
34         const VectorType& vec = static_cast<const VectorType&>(type);
35         const Type* elementType = vec.getElementType();
36 
37         // Aidl doesn't support List<*> for C++ and NDK backends
38         return getAidlType(*elementType, relativeTo) + "[]";
39     } else if (type.isNamedType()) {
40         const NamedType& namedType = static_cast<const NamedType&>(type);
41         if (getAidlPackage(relativeTo) == getAidlPackage(namedType.fqName())) {
42             return getAidlName(namedType.fqName());
43         } else {
44             return getAidlFQName(namedType.fqName());
45         }
46     } else if (type.isMemory()) {
47         return getPlaceholderType("memory");
48     } else if (type.isFmq()) {
49         const FmqType& fmq = static_cast<const FmqType&>(type);
50         return getPlaceholderType(fmq.templatedTypeName() + "<" +
51                                   getAidlType(*fmq.getElementType(), relativeTo) + ">");
52     } else if (type.isPointer()) {
53         return getPlaceholderType("pointer");
54     } else if (type.isEnum()) {
55         // enum type goes to the primitive java type in HIDL, but AIDL should use
56         // the enum type name itself
57         return type.definedName();
58     } else {
59         return type.getJavaType();
60     }
61 }
62 
63 }  // namespace android
64