blob: efd5b6b3614ccca4f8b5fe8d975033d92b87a901 [file] [log] [blame]
Steven Moreland860b1942018-08-16 14:59:28 -07001/*
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 * 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 "aidl_to_cpp.h"
18#include "aidl_language.h"
Jiyong Parkce50e262018-10-29 09:54:20 +090019#include "logging.h"
20
21#include <functional>
22#include <unordered_map>
Steven Moreland860b1942018-08-16 14:59:28 -070023
Jeongik Chab5d962f2018-11-17 09:12:28 +090024using std::ostringstream;
25
Steven Moreland860b1942018-08-16 14:59:28 -070026namespace android {
27namespace aidl {
28namespace cpp {
29
30std::string ConstantValueDecorator(const AidlTypeSpecifier& type, const std::string& raw_value) {
31 if (type.GetName() == "String" && !type.IsArray() && !type.IsUtf8InCpp()) {
32 return "::android::String16(" + raw_value + ")";
33 }
34
35 return raw_value;
36};
37
Jiyong Parkce50e262018-10-29 09:54:20 +090038struct TypeInfo {
39 // name of the type in C++ output
40 std::string cpp_name;
41
42 // function that writes an expression to convert a variable to a Json::Value
43 // object
44 std::function<void(const CodeGeneratorContext& c, const string& var_name)> toJsonValueExpr;
45};
46
47const static std::unordered_map<std::string, TypeInfo> kTypeInfoMap = {
48 {"void", {"void", nullptr}},
49 {"boolean",
50 {
51 "bool",
52 [](const CodeGeneratorContext& c, const string& var_name) {
53 c.writer << "Json::Value(" << var_name << "? \"true\" : \"false\")";
54 },
55 }},
56 {"byte",
57 {
58 "int8_t",
59 [](const CodeGeneratorContext& c, const string& var_name) {
60 c.writer << "Json::Value(" << var_name << ")";
61 },
62 }},
63 {"char",
64 {
65 "char16_t",
66 [](const CodeGeneratorContext& c, const string& var_name) {
67 c.writer << "Json::Value(std::string(android::String8(&" << var_name << ", 1)))";
68 },
69 }},
70 {"int",
71 {
72 "int32_t",
73 [](const CodeGeneratorContext& c, const string& var_name) {
74 c.writer << "Json::Value(" << var_name << ")";
75 },
76 }},
77 {"long",
78 {
79 "int64_t",
80 [](const CodeGeneratorContext& c, const string& var_name) {
81 c.writer << "Json::Value(static_cast<Json::Int64>(" << var_name << "))";
82 },
83 }},
84 {"float",
85 {
86 "float",
87 [](const CodeGeneratorContext& c, const string& var_name) {
88 c.writer << "Json::Value(" << var_name << ")";
89 },
90 }},
91 {"double",
92 {
93 "double",
94 [](const CodeGeneratorContext& c, const string& var_name) {
95 c.writer << "Json::Value(" << var_name << ")";
96 },
97 }},
98 {"String",
99 {
100 "std::string",
101 [](const CodeGeneratorContext& c, const string& var_name) {
102 c.writer << "Json::Value(" << var_name << ")";
103 },
104 }}
105 // missing List, Map, ParcelFileDescriptor, IBinder
106};
107
108TypeInfo GetTypeInfo(const AidlTypenames&, const AidlTypeSpecifier& aidl) {
109 CHECK(aidl.IsResolved()) << aidl.ToString();
110 const string& aidl_name = aidl.GetName();
111
112 TypeInfo info;
113 if (AidlTypenames::IsBuiltinTypename(aidl_name)) {
114 auto it = kTypeInfoMap.find(aidl_name);
115 if (it != kTypeInfoMap.end()) {
116 info = it->second;
117 }
118 }
119 // Missing interface and parcelable type
120 return info;
121}
122
123void WriteLogFor(const CodeGeneratorContext& c) {
124 const TypeInfo info = GetTypeInfo(c.types, c.type);
125 if (info.cpp_name == "") {
126 return;
127 }
128
129 const string var_object_expr = ((c.isPointer ? "*" : "")) + c.name;
130 if (c.type.IsArray()) {
131 c.writer << "for (const auto& v: " << var_object_expr << ") " << c.log << "[\"" << c.name
132 << "\"] = ";
133 info.toJsonValueExpr(c, "v");
134 c.writer << ";";
135 } else {
136 c.writer << c.log << "[\"" << c.name << "\"] = ";
137 info.toJsonValueExpr(c, var_object_expr);
138 c.writer << ";";
139 }
140 c.writer << "\n";
141}
142
Jeongik Chab5d962f2018-11-17 09:12:28 +0900143std::string GetTransactionIdFor(const AidlMethod& method) {
144 ostringstream output;
145
146 if (method.IsUserDefined()) {
147 output << "::android::IBinder::FIRST_CALL_TRANSACTION + ";
148 }
149 output << method.GetId() << " /* " << method.GetName() << " */";
150 return output.str();
151}
Steven Moreland860b1942018-08-16 14:59:28 -0700152} // namespace cpp
153} // namespace aidl
154} // namespace android