| Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 1 | /* |
| 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 Park | ce50e26 | 2018-10-29 09:54:20 +0900 | [diff] [blame] | 19 | #include "logging.h" |
| 20 | |
| 21 | #include <functional> |
| 22 | #include <unordered_map> |
| Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 23 | |
| Jeongik Cha | b5d962f | 2018-11-17 09:12:28 +0900 | [diff] [blame] | 24 | using std::ostringstream; |
| 25 | |
| Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 26 | namespace android { |
| 27 | namespace aidl { |
| 28 | namespace cpp { |
| 29 | |
| 30 | std::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 Park | ce50e26 | 2018-10-29 09:54:20 +0900 | [diff] [blame] | 38 | struct 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 | |
| 47 | const 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 | |
| 108 | TypeInfo 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 | |
| 123 | void 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 Cha | b5d962f | 2018-11-17 09:12:28 +0900 | [diff] [blame] | 143 | std::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 Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 152 | } // namespace cpp |
| 153 | } // namespace aidl |
| 154 | } // namespace android |