Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015, 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 "generate_cpp.h" |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 18 | #include "parse_helpers.h" |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 19 | |
Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 20 | #include <cctype> |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 21 | #include <memory> |
Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 22 | #include <random> |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 23 | #include <string> |
| 24 | |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 25 | #include <base/stringprintf.h> |
| 26 | |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 27 | #include "aidl_language.h" |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 28 | #include "ast_cpp.h" |
| 29 | #include "code_writer.h" |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 30 | #include "logging.h" |
| 31 | |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 32 | using android::base::StringPrintf; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 33 | using std::string; |
Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 34 | using std::unique_ptr; |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 35 | using std::vector; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 36 | |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 37 | namespace android { |
| 38 | namespace aidl { |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 39 | namespace cpp { |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 40 | namespace internals { |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame^] | 41 | namespace { |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 42 | |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame^] | 43 | const char kAndroidStatusLiteral[] = "android::status_t"; |
| 44 | const char kIBinderHeader[] = "binder/IBinder.h"; |
| 45 | const char kIInterfaceHeader[] = "binder/IInterface.h"; |
Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 46 | |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame^] | 47 | string UpperCase(const std::string& s) { |
| 48 | string result = s; |
| 49 | for (char& c : result) |
| 50 | c = toupper(c); |
| 51 | return result; |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 52 | } |
Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 53 | |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 54 | string GetCPPVarDec(const TypeNamespace& types, const type_type* type, |
| 55 | const string& var_name, bool use_pointer) { |
| 56 | const Type* cpp_type = types.Find(type->type.data); |
| 57 | if (cpp_type == nullptr) { |
| 58 | // We should have caught this in type resolution. |
| 59 | LOG(FATAL) << "internal error"; |
| 60 | } |
| 61 | return StringPrintf("%s%s %s%s", |
| 62 | cpp_type->CppType().c_str(), |
| 63 | (use_pointer) ? "*" : "", |
| 64 | var_name.c_str(), |
| 65 | type->Brackets().c_str()); |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame^] | 68 | unique_ptr<Declaration> BuildMethodDecl(const method_type* method, |
| 69 | const TypeNamespace& types, |
| 70 | bool for_interface) { |
| 71 | vector<string> args; |
| 72 | for (const unique_ptr<AidlArgument>& arg : *method->args) { |
| 73 | args.push_back(GetCPPVarDec( |
| 74 | types, &arg->type, arg->name.Literal(), |
| 75 | OUT_PARAMETER & convert_direction(arg->direction.data))); |
| 76 | } |
| 77 | |
| 78 | string return_arg = GetCPPVarDec(types, &method->type, "_aidl_return", true); |
| 79 | args.push_back(return_arg); |
| 80 | |
| 81 | uint32_t modifiers = 0; |
| 82 | if (for_interface) { |
| 83 | modifiers |= MethodDecl::IS_VIRTUAL; |
| 84 | modifiers |= MethodDecl::IS_PURE_VIRTUAL; |
| 85 | } else { |
| 86 | modifiers |= MethodDecl::IS_OVERRIDE; |
| 87 | } |
| 88 | |
| 89 | return unique_ptr<Declaration>{ |
| 90 | new MethodDecl{kAndroidStatusLiteral, |
| 91 | method->name.Literal(), |
| 92 | args, |
| 93 | modifiers}}; |
| 94 | } |
| 95 | |
| 96 | unique_ptr<CppNamespace> NestInNamespaces(unique_ptr<Declaration> decl) { |
| 97 | using N = CppNamespace; |
| 98 | using NPtr = unique_ptr<N>; |
| 99 | return NPtr{new N{"android", NPtr{new N{"generated", std::move(decl)}}}}; |
| 100 | } |
| 101 | |
| 102 | } // namespace |
| 103 | |
| 104 | enum class ClassNames { BASE, CLIENT, SERVER, INTERFACE }; |
| 105 | |
| 106 | string ClassName(const interface_type& interface, ClassNames type) { |
| 107 | string c_name = interface.name.Literal(); |
| 108 | |
| 109 | if (c_name.length() >= 2 && c_name[0] == 'I' && isupper(c_name[1])) |
| 110 | c_name = c_name.substr(1); |
| 111 | |
| 112 | switch (type) { |
| 113 | case ClassNames::CLIENT: |
| 114 | c_name = "Bp" + c_name; |
| 115 | break; |
| 116 | case ClassNames::SERVER: |
| 117 | c_name = "Bn" + c_name; |
| 118 | break; |
| 119 | case ClassNames::INTERFACE: |
| 120 | c_name = "I" + c_name; |
| 121 | break; |
| 122 | case ClassNames::BASE: |
| 123 | break; |
| 124 | } |
| 125 | return c_name; |
| 126 | } |
| 127 | |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 128 | unique_ptr<Document> BuildClientSource(const TypeNamespace& types, |
| 129 | const interface_type& parsed_doc) { |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame^] | 130 | unique_ptr<CppNamespace> ns{new CppNamespace{"android"}}; |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 131 | return unique_ptr<Document>{new CppSource{ {}, std::move(ns)}}; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 134 | unique_ptr<Document> BuildServerSource(const TypeNamespace& types, |
| 135 | const interface_type& parsed_doc) { |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame^] | 136 | unique_ptr<CppNamespace> ns{new CppNamespace{"android"}}; |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 137 | return unique_ptr<Document>{new CppSource{ {}, std::move(ns)}}; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 140 | unique_ptr<Document> BuildInterfaceSource(const TypeNamespace& types, |
| 141 | const interface_type& parsed_doc) { |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame^] | 142 | unique_ptr<CppNamespace> ns{new CppNamespace{"android"}}; |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 143 | return unique_ptr<Document>{new CppSource{ {}, std::move(ns)}}; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 146 | unique_ptr<Document> BuildClientHeader(const TypeNamespace& types, |
| 147 | const interface_type& parsed_doc) { |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame^] | 148 | const string i_name = ClassName(parsed_doc, ClassNames::INTERFACE); |
| 149 | const string bp_name = ClassName(parsed_doc, ClassNames::CLIENT); |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 150 | |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame^] | 151 | unique_ptr<ConstructorDecl> constructor{new ConstructorDecl(bp_name, {})}; |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 152 | unique_ptr<ConstructorDecl> destructor{ |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame^] | 153 | new ConstructorDecl("~" + bp_name, {})}; |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 154 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 155 | vector<unique_ptr<Declaration>> publics; |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 156 | publics.push_back(std::move(constructor)); |
| 157 | publics.push_back(std::move(destructor)); |
| 158 | |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 159 | for (method_type *item = parsed_doc.interface_items; |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 160 | item; |
| 161 | item = item->next) { |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame^] | 162 | publics.push_back(BuildMethodDecl(item, types, false)); |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 165 | unique_ptr<ClassDecl> bp_class{ |
| 166 | new ClassDecl{bp_name, |
| 167 | "public android::BpInterface<" + i_name + ">", |
| 168 | std::move(publics), |
| 169 | {} |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 170 | }}; |
| 171 | |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame^] | 172 | return unique_ptr<Document>{new CppHeader{ |
| 173 | bp_name + "_H", |
| 174 | {kIBinderHeader, |
| 175 | kIInterfaceHeader, |
| 176 | "utils/Errors.h", |
| 177 | i_name + ".h"}, |
| 178 | NestInNamespaces(std::move(bp_class))}}; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 181 | unique_ptr<Document> BuildServerHeader(const TypeNamespace& types, |
| 182 | const interface_type& parsed_doc) { |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame^] | 183 | const string i_name = ClassName(parsed_doc, ClassNames::INTERFACE); |
| 184 | const string bn_name = ClassName(parsed_doc, ClassNames::SERVER); |
Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 185 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 186 | unique_ptr<Declaration> on_transact{ |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame^] | 187 | new MethodDecl(kAndroidStatusLiteral, "onTransact", |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 188 | { "uint32_t code", |
| 189 | "const android::Parcel& data", |
| 190 | "android::Parcel* reply", |
| 191 | "uint32_t flags = 0" |
| 192 | })}; |
Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 193 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 194 | std::vector<unique_ptr<Declaration>> publics; |
Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 195 | publics.push_back(std::move(on_transact)); |
| 196 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 197 | unique_ptr<ClassDecl> bn_class{ |
| 198 | new ClassDecl{bn_name, |
| 199 | "public android::BnInterface<" + i_name + ">", |
| 200 | std::move(publics), |
| 201 | {} |
Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 202 | }}; |
Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 203 | |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame^] | 204 | return unique_ptr<Document>{new CppHeader{ |
| 205 | bn_name + "_H", |
| 206 | {"binder/IInterface.h", |
| 207 | i_name + ".h"}, |
| 208 | NestInNamespaces(std::move(bn_class))}}; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 211 | unique_ptr<Document> BuildInterfaceHeader(const TypeNamespace& types, |
| 212 | const interface_type& parsed_doc) { |
Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame^] | 213 | unique_ptr<ClassDecl> if_class{ |
| 214 | new ClassDecl{ClassName(parsed_doc, ClassNames::INTERFACE), |
| 215 | "public android::IInterface"}}; |
| 216 | if_class->AddPublic(unique_ptr<Declaration>{ |
| 217 | new ConstructorDecl{ |
| 218 | "DECLARE_META_INTERFACE", |
| 219 | {ClassName(parsed_doc, ClassNames::BASE)}}}); |
| 220 | |
| 221 | unique_ptr<Enum> call_enum{new Enum{"Call"}}; |
| 222 | for (const method_type* method = parsed_doc.interface_items; |
| 223 | method; |
| 224 | method = method->next) { |
| 225 | if_class->AddPublic(BuildMethodDecl(method, types, true)); |
| 226 | call_enum->AddValue( |
| 227 | UpperCase(method->name.data), |
| 228 | StringPrintf("android::IBinder::FIRST_CALL_TRANSACTION + %d", |
| 229 | method->assigned_id)); |
| 230 | } |
| 231 | if_class->AddPublic(std::move(call_enum)); |
| 232 | |
| 233 | return unique_ptr<Document>{new CppSource{ |
| 234 | {kIBinderHeader, |
| 235 | kIInterfaceHeader}, |
| 236 | NestInNamespaces(std::move(if_class))}}; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 239 | bool GenerateCppForFile(const std::string& name, unique_ptr<Document> doc) { |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 240 | if (!doc) { |
| 241 | return false; |
| 242 | } |
| 243 | unique_ptr<CodeWriter> writer = GetFileWriter(name); |
| 244 | doc->Write(writer.get()); |
| 245 | return true; |
| 246 | } |
| 247 | |
Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 248 | } // namespace internals |
| 249 | |
| 250 | using namespace internals; |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 251 | |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 252 | bool GenerateCpp(const CppOptions& options, |
| 253 | const TypeNamespace& types, |
| 254 | const interface_type& parsed_doc) { |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 255 | bool success = true; |
| 256 | |
| 257 | success &= GenerateCppForFile(options.ClientCppFileName(), |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 258 | BuildClientSource(types, parsed_doc)); |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 259 | success &= GenerateCppForFile(options.ClientHeaderFileName(), |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 260 | BuildClientHeader(types, parsed_doc)); |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 261 | success &= GenerateCppForFile(options.ServerCppFileName(), |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 262 | BuildServerSource(types, parsed_doc)); |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 263 | success &= GenerateCppForFile(options.ServerHeaderFileName(), |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 264 | BuildServerHeader(types, parsed_doc)); |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 265 | success &= GenerateCppForFile(options.InterfaceCppFileName(), |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 266 | BuildInterfaceSource(types, parsed_doc)); |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 267 | success &= GenerateCppForFile(options.InterfaceHeaderFileName(), |
Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 268 | BuildInterfaceHeader(types, parsed_doc)); |
Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 269 | |
| 270 | return success; |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 273 | } // namespace cpp |
Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 274 | } // namespace aidl |
Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 275 | } // namespace android |