| 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" |
| Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 18 | |
| Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 19 | #include <cctype> |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 20 | #include <cstring> |
| 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> |
| Casey Dahlin | ce776cf | 2015-10-15 18:45:54 -0700 | [diff] [blame] | 23 | #include <set> |
| Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 24 | #include <string> |
| 25 | |
| Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 26 | #include <base/stringprintf.h> |
| 27 | |
| Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 28 | #include "aidl_language.h" |
| Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 29 | #include "ast_cpp.h" |
| 30 | #include "code_writer.h" |
| Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 31 | #include "logging.h" |
| Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 32 | #include "os.h" |
| Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 33 | |
| Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 34 | using android::base::StringPrintf; |
| Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 35 | using std::string; |
| Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 36 | using std::unique_ptr; |
| Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 37 | using std::vector; |
| Casey Dahlin | ce776cf | 2015-10-15 18:45:54 -0700 | [diff] [blame] | 38 | using std::set; |
| Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 39 | |
| Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 40 | namespace android { |
| 41 | namespace aidl { |
| Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 42 | namespace cpp { |
| Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 43 | namespace internals { |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 44 | namespace { |
| Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 45 | |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 46 | const char kStatusOkOrBreakCheck[] = "if (status != android::OK) { break; }"; |
| Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 47 | const char kStatusOkOrReturnLiteral[] = |
| 48 | "if (status != android::OK) { return status; }"; |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 49 | const char kReturnVarName[] = "_aidl_return"; |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 50 | const char kAndroidStatusLiteral[] = "android::status_t"; |
| Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 51 | const char kAndroidParcelLiteral[] = "android::Parcel"; |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 52 | const char kIBinderHeader[] = "binder/IBinder.h"; |
| 53 | const char kIInterfaceHeader[] = "binder/IInterface.h"; |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 54 | const char kParcelHeader[] = "binder/Parcel.h"; |
| Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 55 | |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 56 | string UpperCase(const std::string& s) { |
| 57 | string result = s; |
| 58 | for (char& c : result) |
| 59 | c = toupper(c); |
| 60 | return result; |
| Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 61 | } |
| Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 62 | |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 63 | string BuildVarName(const AidlArgument& a) { |
| 64 | string prefix = "out_"; |
| 65 | if (a.GetDirection() & AidlArgument::IN_DIR) { |
| 66 | prefix = "in_"; |
| Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 67 | } |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 68 | return prefix + a.GetName(); |
| 69 | } |
| 70 | |
| Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 71 | ArgList BuildArgList(const TypeNamespace& types, |
| 72 | const AidlMethod& method, |
| 73 | bool for_declaration) { |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 74 | // Build up the argument list for the server method call. |
| 75 | vector<string> method_arguments; |
| 76 | for (const unique_ptr<AidlArgument>& a : method.GetArguments()) { |
| 77 | string literal; |
| 78 | if (for_declaration) { |
| 79 | // Method declarations need types, pointers to out params, and variable |
| 80 | // names that match the .aidl specification. |
| 81 | const Type* type = types.Find(a->GetType().GetName()); |
| Casey Dahlin | ce776cf | 2015-10-15 18:45:54 -0700 | [diff] [blame] | 82 | |
| Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 83 | literal = type->CppType(a->GetType().IsArray()); |
| 84 | |
| 85 | if (a->IsOut()) |
| 86 | literal += "*"; |
| 87 | else if (a->GetType().IsArray()) |
| 88 | literal = "const " + literal + "&"; |
| 89 | |
| 90 | literal += " " + a->GetName(); |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 91 | } else { |
| 92 | if (a->IsOut()) { literal = "&"; } |
| 93 | literal += BuildVarName(*a); |
| 94 | } |
| 95 | method_arguments.push_back(literal); |
| 96 | } |
| 97 | |
| 98 | const Type* return_type = types.Find(method.GetType().GetName()); |
| Casey Dahlin | ce776cf | 2015-10-15 18:45:54 -0700 | [diff] [blame] | 99 | |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 100 | if (return_type != types.VoidType()) { |
| Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 101 | string literal; |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 102 | if (for_declaration) { |
| Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 103 | literal = StringPrintf( |
| Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 104 | "%s* %s", return_type->CppType(false /* not array */).c_str(), |
| 105 | kReturnVarName); |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 106 | } else { |
| Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 107 | literal = string{"&"} + kReturnVarName; |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 108 | } |
| Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 109 | method_arguments.push_back(literal); |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 112 | return ArgList(method_arguments); |
| Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 115 | unique_ptr<Declaration> BuildMethodDecl(const AidlMethod& method, |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 116 | const TypeNamespace& types, |
| 117 | bool for_interface) { |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 118 | uint32_t modifiers = 0; |
| 119 | if (for_interface) { |
| 120 | modifiers |= MethodDecl::IS_VIRTUAL; |
| 121 | modifiers |= MethodDecl::IS_PURE_VIRTUAL; |
| 122 | } else { |
| 123 | modifiers |= MethodDecl::IS_OVERRIDE; |
| 124 | } |
| 125 | |
| 126 | return unique_ptr<Declaration>{ |
| 127 | new MethodDecl{kAndroidStatusLiteral, |
| Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 128 | method.GetName(), |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 129 | BuildArgList(types, method, true /* for method decl */), |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 130 | modifiers}}; |
| 131 | } |
| 132 | |
| Christopher Wiley | b656a3b | 2015-10-16 11:11:09 -0700 | [diff] [blame] | 133 | unique_ptr<CppNamespace> NestInNamespaces( |
| 134 | vector<unique_ptr<Declaration>> decls, |
| 135 | const vector<string>& package) { |
| 136 | if (package.empty()) { |
| 137 | // We should also be checking this before we get this far, but do it again |
| 138 | // for the sake of unit tests and meaningful errors. |
| 139 | LOG(FATAL) << "C++ generation requires a package declaration " |
| 140 | "for namespacing"; |
| 141 | } |
| 142 | auto it = package.crbegin(); // Iterate over the namespaces inner to outer |
| 143 | unique_ptr<CppNamespace> inner{new CppNamespace{*it, std::move(decls)}}; |
| 144 | ++it; |
| 145 | for (; it != package.crend(); ++it) { |
| 146 | inner.reset(new CppNamespace{*it, std::move(inner)}); |
| 147 | } |
| 148 | return inner; |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| Christopher Wiley | b656a3b | 2015-10-16 11:11:09 -0700 | [diff] [blame] | 151 | unique_ptr<CppNamespace> NestInNamespaces(unique_ptr<Declaration> decl, |
| 152 | const vector<string>& package) { |
| 153 | vector<unique_ptr<Declaration>> decls; |
| 154 | decls.push_back(std::move(decl)); |
| 155 | return NestInNamespaces(std::move(decls), package); |
| Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 158 | bool DeclareLocalVariable(const TypeNamespace& types, const AidlArgument& a, |
| 159 | StatementBlock* b) { |
| 160 | const Type* cpp_type = types.Find(a.GetType().GetName()); |
| 161 | if (!cpp_type) { return false; } |
| 162 | |
| Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 163 | string type = cpp_type->CppType(a.GetType().IsArray()); |
| 164 | |
| 165 | b->AddLiteral(type + " " + BuildVarName(a)); |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 166 | return true; |
| 167 | } |
| 168 | |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 169 | enum class ClassNames { BASE, CLIENT, SERVER, INTERFACE }; |
| 170 | |
| Casey Dahlin | 1ae2bc5 | 2015-10-07 18:49:10 -0700 | [diff] [blame] | 171 | string ClassName(const AidlInterface& interface, ClassNames type) { |
| Casey Dahlin | fb7da2e | 2015-10-08 17:26:09 -0700 | [diff] [blame] | 172 | string c_name = interface.GetName(); |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 173 | |
| 174 | if (c_name.length() >= 2 && c_name[0] == 'I' && isupper(c_name[1])) |
| 175 | c_name = c_name.substr(1); |
| 176 | |
| 177 | switch (type) { |
| 178 | case ClassNames::CLIENT: |
| 179 | c_name = "Bp" + c_name; |
| 180 | break; |
| 181 | case ClassNames::SERVER: |
| 182 | c_name = "Bn" + c_name; |
| 183 | break; |
| 184 | case ClassNames::INTERFACE: |
| 185 | c_name = "I" + c_name; |
| 186 | break; |
| 187 | case ClassNames::BASE: |
| 188 | break; |
| 189 | } |
| 190 | return c_name; |
| 191 | } |
| 192 | |
| Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 193 | string HeaderFile(const AidlInterface& interface, |
| 194 | ClassNames class_type, |
| 195 | bool use_os_sep = true) { |
| 196 | string file_path = interface.GetPackage(); |
| 197 | for (char& c: file_path) { |
| 198 | if (c == '.') { |
| 199 | c = (use_os_sep) ? OS_PATH_SEPARATOR : '/'; |
| 200 | } |
| 201 | } |
| 202 | if (!file_path.empty()) { |
| 203 | file_path += (use_os_sep) ? OS_PATH_SEPARATOR : '/'; |
| 204 | } |
| 205 | file_path += ClassName(interface, class_type); |
| 206 | file_path += ".h"; |
| 207 | |
| 208 | return file_path; |
| 209 | } |
| 210 | |
| Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 211 | string BuildHeaderGuard(const AidlInterface& interface, |
| 212 | ClassNames header_type) { |
| 213 | string class_name = ClassName(interface, header_type); |
| 214 | for (size_t i = 1; i < class_name.size(); ++i) { |
| 215 | if (isupper(class_name[i])) { |
| 216 | class_name.insert(i, "_"); |
| 217 | ++i; |
| 218 | } |
| 219 | } |
| 220 | string ret = StringPrintf("AIDL_GENERATED_%s_%s_H_", |
| 221 | interface.GetPackage().c_str(), |
| 222 | class_name.c_str()); |
| 223 | for (char& c : ret) { |
| 224 | if (c == '.') { |
| 225 | c = '_'; |
| 226 | } |
| 227 | c = toupper(c); |
| 228 | } |
| 229 | return ret; |
| 230 | } |
| Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 231 | |
| 232 | unique_ptr<Declaration> DefineClientTransaction(const TypeNamespace& types, |
| 233 | const AidlInterface& interface, |
| 234 | const AidlMethod& method) { |
| 235 | const string i_name = ClassName(interface, ClassNames::INTERFACE); |
| 236 | const string bp_name = ClassName(interface, ClassNames::CLIENT); |
| 237 | unique_ptr<MethodImpl> ret{new MethodImpl{ |
| 238 | kAndroidStatusLiteral, bp_name, method.GetName(), |
| 239 | ArgList{BuildArgList(types, method, true /* for method decl */)}}}; |
| 240 | StatementBlock* b = ret->GetStatementBlock(); |
| 241 | |
| 242 | // Declare parcels to hold our query and the response. |
| 243 | b->AddLiteral(StringPrintf("%s data", kAndroidParcelLiteral)); |
| Casey Dahlin | 0dd08af | 2015-10-20 18:45:50 -0700 | [diff] [blame] | 244 | |
| 245 | if (!interface.IsOneway() && !method.IsOneway()) { |
| 246 | b->AddLiteral(StringPrintf("%s reply", kAndroidParcelLiteral)); |
| 247 | } |
| 248 | |
| Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 249 | // And declare the status variable we need for error handling. |
| 250 | b->AddLiteral(StringPrintf("%s status", kAndroidStatusLiteral)); |
| 251 | |
| 252 | // Serialization looks roughly like: |
| 253 | // status = data.WriteInt32(in_param_name); |
| 254 | // if (status != android::OK) { return status; } |
| 255 | for (const AidlArgument* a : method.GetInArguments()) { |
| Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 256 | string method = |
| 257 | types.Find(a->GetType().GetName()) |
| 258 | ->WriteToParcelMethod(a->GetType().IsArray()); |
| 259 | |
| Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 260 | string var_name = ((a->IsOut()) ? "*" : "") + a->GetName(); |
| 261 | b->AddStatement(new Assignment( |
| 262 | "status", |
| 263 | new MethodCall("data." + method, ArgList(var_name)))); |
| 264 | b->AddLiteral(kStatusOkOrReturnLiteral, false /* no semicolon */); |
| 265 | } |
| 266 | |
| 267 | // Invoke the transaction on the remote binder and confirm status. |
| 268 | string transaction_code = StringPrintf( |
| 269 | "%s::%s", i_name.c_str(), UpperCase(method.GetName()).c_str()); |
| Casey Dahlin | 0dd08af | 2015-10-20 18:45:50 -0700 | [diff] [blame] | 270 | |
| 271 | vector<string> args = {transaction_code, "data", "&reply"}; |
| 272 | |
| 273 | if (interface.IsOneway() || method.IsOneway()) { |
| 274 | args.push_back("android::IBinder::FLAG_ONEWAY"); |
| 275 | } |
| 276 | |
| 277 | // Type checking should guarantee that nothing below emits code until "return |
| 278 | // status" if we are a oneway method, so no more fear of accessing reply. |
| 279 | |
| Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 280 | b->AddStatement(new Assignment( |
| 281 | "status", |
| 282 | new MethodCall("remote()->transact", |
| Casey Dahlin | 0dd08af | 2015-10-20 18:45:50 -0700 | [diff] [blame] | 283 | ArgList(args)))); |
| Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 284 | b->AddLiteral(kStatusOkOrReturnLiteral, false /* no semicolon */); |
| 285 | |
| 286 | // If the method is expected to return something, read it first by convention. |
| 287 | const Type* return_type = types.Find(method.GetType().GetName()); |
| 288 | if (return_type != types.VoidType()) { |
| Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 289 | string method = return_type->ReadFromParcelMethod(false); |
| Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 290 | b->AddStatement(new Assignment( |
| 291 | "status", |
| 292 | new MethodCall("reply." + method, ArgList(kReturnVarName)))); |
| 293 | b->AddLiteral(kStatusOkOrReturnLiteral, false /* no semicolon */); |
| 294 | } |
| 295 | |
| 296 | for (const AidlArgument* a : method.GetOutArguments()) { |
| 297 | // Deserialization looks roughly like: |
| 298 | // status = reply.ReadInt32(out_param_name); |
| 299 | // if (status != android::OK) { return status; } |
| Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 300 | string method = |
| 301 | types.Find(a->GetType().GetName()) |
| 302 | ->ReadFromParcelMethod(a->GetType().IsArray()); |
| 303 | |
| Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 304 | b->AddStatement(new Assignment( |
| 305 | "status", |
| 306 | new MethodCall("reply." + method, ArgList(a->GetName())))); |
| 307 | b->AddLiteral(kStatusOkOrReturnLiteral, false /* no semicolon */); |
| 308 | } |
| 309 | |
| 310 | b->AddLiteral("return status"); |
| 311 | |
| 312 | return unique_ptr<Declaration>(ret.release()); |
| 313 | } |
| 314 | |
| 315 | } // namespace |
| 316 | |
| Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 317 | unique_ptr<Document> BuildClientSource(const TypeNamespace& types, |
| Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 318 | const AidlInterface& interface) { |
| Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 319 | vector<string> include_list = { |
| 320 | HeaderFile(interface, ClassNames::CLIENT, false), |
| 321 | kParcelHeader |
| 322 | }; |
| Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 323 | vector<unique_ptr<Declaration>> file_decls; |
| 324 | |
| 325 | // The constructor just passes the IBinder instance up to the super |
| 326 | // class. |
| 327 | file_decls.push_back(unique_ptr<Declaration>{new ConstructorImpl{ |
| Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 328 | ClassName(interface, ClassNames::CLIENT), |
| Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 329 | ArgList{"const android::sp<android::IBinder>& impl"}, |
| 330 | { "BpInterface<IPingResponder>(impl)" }}}); |
| 331 | |
| 332 | // Clients define a method per transaction. |
| 333 | for (const auto& method : interface.GetMethods()) { |
| 334 | unique_ptr<Declaration> m = DefineClientTransaction( |
| 335 | types, interface, *method); |
| 336 | if (!m) { return nullptr; } |
| 337 | file_decls.push_back(std::move(m)); |
| 338 | } |
| 339 | return unique_ptr<Document>{new CppSource{ |
| 340 | include_list, |
| Christopher Wiley | b656a3b | 2015-10-16 11:11:09 -0700 | [diff] [blame] | 341 | NestInNamespaces(std::move(file_decls), interface.GetSplitPackage())}}; |
| Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 344 | namespace { |
| 345 | |
| 346 | bool HandleServerTransaction(const TypeNamespace& types, |
| 347 | const AidlMethod& method, |
| 348 | StatementBlock* b) { |
| 349 | // Declare all the parameters now. In the common case, we expect no errors |
| 350 | // in serialization. |
| 351 | for (const unique_ptr<AidlArgument>& a : method.GetArguments()) { |
| 352 | if (!DeclareLocalVariable(types, *a, b)) { return false; } |
| 353 | } |
| 354 | |
| 355 | // Declare a variable to hold the return value. |
| 356 | const Type* return_type = types.Find(method.GetType().GetName()); |
| 357 | if (return_type != types.VoidType()) { |
| 358 | b->AddLiteral(StringPrintf( |
| Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 359 | "%s %s", return_type->CppType(false /* not array */).c_str(), |
| 360 | kReturnVarName)); |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 363 | // Deserialize each "in" parameter to the transaction. |
| 364 | for (const AidlArgument* a : method.GetInArguments()) { |
| 365 | // Deserialization looks roughly like: |
| 366 | // status = data.ReadInt32(&in_param_name); |
| 367 | // if (status != android::OK) { break; } |
| 368 | const Type* type = types.Find(a->GetType().GetName()); |
| Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 369 | string readMethod = type->ReadFromParcelMethod(a->GetType().IsArray()); |
| 370 | |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 371 | b->AddStatement(new Assignment{ |
| 372 | "status", |
| Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 373 | new MethodCall{"data." + readMethod, |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 374 | "&" + BuildVarName(*a)}}); |
| 375 | b->AddLiteral(kStatusOkOrBreakCheck, false /* no semicolon */); |
| 376 | } |
| 377 | |
| 378 | // Call the actual method. This is implemented by the subclass. |
| 379 | b->AddStatement(new Assignment{ |
| 380 | "status", new MethodCall{ |
| 381 | method.GetName(), |
| Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 382 | BuildArgList(types, method, false /* not for method decl */)}}); |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 383 | b->AddLiteral(kStatusOkOrBreakCheck, false /* no semicolon */); |
| 384 | |
| Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 385 | string writeMethod = |
| 386 | return_type->WriteToParcelMethod(method.GetType().IsArray()); |
| 387 | |
| Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 388 | // If we have a return value, write it first. |
| 389 | if (return_type != types.VoidType()) { |
| Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 390 | string method = "reply->" + writeMethod; |
| Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 391 | b->AddStatement(new Assignment{ |
| 392 | "status", new MethodCall{method, ArgList{kReturnVarName}}}); |
| 393 | b->AddLiteral(kStatusOkOrBreakCheck, false /* no semicolon */); |
| 394 | } |
| 395 | |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 396 | // Write each out parameter to the reply parcel. |
| 397 | for (const AidlArgument* a : method.GetOutArguments()) { |
| 398 | // Serialization looks roughly like: |
| 399 | // status = data.WriteInt32(out_param_name); |
| 400 | // if (status != android::OK) { break; } |
| 401 | const Type* type = types.Find(a->GetType().GetName()); |
| Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 402 | string writeMethod = type->WriteToParcelMethod(a->GetType().IsArray()); |
| 403 | |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 404 | b->AddStatement(new Assignment{ |
| 405 | "status", |
| Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 406 | new MethodCall{"reply->" + writeMethod, |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 407 | BuildVarName(*a)}}); |
| 408 | b->AddLiteral(kStatusOkOrBreakCheck, false /* no semicolon */); |
| 409 | } |
| 410 | |
| 411 | return true; |
| 412 | } |
| 413 | |
| 414 | } // namespace |
| 415 | |
| Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 416 | unique_ptr<Document> BuildServerSource(const TypeNamespace& types, |
| Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 417 | const AidlInterface& interface) { |
| 418 | const string bn_name = ClassName(interface, ClassNames::SERVER); |
| 419 | vector<string> include_list{ |
| 420 | HeaderFile(interface, ClassNames::SERVER, false), |
| 421 | kParcelHeader |
| 422 | }; |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 423 | unique_ptr<MethodImpl> on_transact{new MethodImpl{ |
| 424 | kAndroidStatusLiteral, bn_name, "onTransact", |
| Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 425 | ArgList{{"uint32_t code", |
| Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 426 | StringPrintf("const %s& data", kAndroidParcelLiteral), |
| 427 | StringPrintf("%s* reply", kAndroidParcelLiteral), |
| 428 | "uint32_t flags"}} |
| 429 | }}; |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 430 | |
| Christopher Wiley | 05f4f89 | 2015-10-14 13:30:43 -0700 | [diff] [blame] | 431 | // Declare the status variable |
| 432 | on_transact->GetStatementBlock()->AddLiteral( |
| 433 | StringPrintf("%s status", kAndroidStatusLiteral)); |
| 434 | |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 435 | // Add the all important switch statement, but retain a pointer to it. |
| 436 | SwitchStatement* s = new SwitchStatement{"code"}; |
| Christopher Wiley | f9688b0 | 2015-10-08 17:17:50 -0700 | [diff] [blame] | 437 | on_transact->GetStatementBlock()->AddStatement(s); |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 438 | |
| 439 | // The switch statement has a case statement for each transaction code. |
| Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 440 | for (const auto& method : interface.GetMethods()) { |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 441 | StatementBlock* b = s->AddCase("Call::" + UpperCase(method->GetName())); |
| 442 | if (!b) { return nullptr; } |
| 443 | |
| 444 | if (!HandleServerTransaction(types, *method, b)) { return nullptr; } |
| 445 | } |
| 446 | |
| 447 | // The switch statement has a default case which defers to the super class. |
| 448 | // The superclass handles a few pre-defined transactions. |
| 449 | StatementBlock* b = s->AddCase(""); |
| 450 | b->AddLiteral( |
| 451 | "status = android::BBinder::onTransact(code, data, reply, flags)"); |
| 452 | |
| 453 | // Finally, the server's onTransact method just returns a status code. |
| Christopher Wiley | f9688b0 | 2015-10-08 17:17:50 -0700 | [diff] [blame] | 454 | on_transact->GetStatementBlock()->AddLiteral("return status"); |
| Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 455 | |
| 456 | return unique_ptr<Document>{new CppSource{ |
| 457 | include_list, |
| Christopher Wiley | b656a3b | 2015-10-16 11:11:09 -0700 | [diff] [blame] | 458 | NestInNamespaces(std::move(on_transact), interface.GetSplitPackage())}}; |
| Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| Christopher Wiley | f59c499 | 2015-10-08 13:12:44 -0700 | [diff] [blame] | 461 | unique_ptr<Document> BuildInterfaceSource(const TypeNamespace& /* types */, |
| Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 462 | const AidlInterface& interface) { |
| 463 | vector<string> include_list{ |
| 464 | HeaderFile(interface, ClassNames::INTERFACE, false), |
| 465 | HeaderFile(interface, ClassNames::CLIENT, false), |
| 466 | }; |
| Christopher Wiley | 1dd458d | 2015-09-30 11:05:52 -0700 | [diff] [blame] | 467 | |
| Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 468 | string fq_name = ClassName(interface, ClassNames::INTERFACE); |
| 469 | if (!interface.GetPackage().empty()) { |
| 470 | fq_name = interface.GetPackage() + "." + fq_name; |
| Christopher Wiley | 1dd458d | 2015-09-30 11:05:52 -0700 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | unique_ptr<ConstructorDecl> meta_if{new ConstructorDecl{ |
| 474 | "IMPLEMENT_META_INTERFACE", |
| Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 475 | ArgList{vector<string>{ClassName(interface, ClassNames::BASE), |
| Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 476 | '"' + fq_name + '"'}}}}; |
| Christopher Wiley | 1dd458d | 2015-09-30 11:05:52 -0700 | [diff] [blame] | 477 | |
| 478 | return unique_ptr<Document>{new CppSource{ |
| 479 | include_list, |
| Christopher Wiley | b656a3b | 2015-10-16 11:11:09 -0700 | [diff] [blame] | 480 | NestInNamespaces(std::move(meta_if), interface.GetSplitPackage())}}; |
| Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 483 | unique_ptr<Document> BuildClientHeader(const TypeNamespace& types, |
| Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 484 | const AidlInterface& interface) { |
| 485 | const string i_name = ClassName(interface, ClassNames::INTERFACE); |
| 486 | const string bp_name = ClassName(interface, ClassNames::CLIENT); |
| Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 487 | |
| Christopher Wiley | b23149d | 2015-10-14 13:52:21 -0700 | [diff] [blame] | 488 | unique_ptr<ConstructorDecl> constructor{new ConstructorDecl{ |
| 489 | bp_name, |
| 490 | ArgList{"const android::sp<android::IBinder>& impl"}, |
| 491 | ConstructorDecl::IS_EXPLICIT |
| 492 | }}; |
| 493 | unique_ptr<ConstructorDecl> destructor{new ConstructorDecl{ |
| 494 | "~" + bp_name, |
| 495 | ArgList{}, |
| 496 | ConstructorDecl::IS_VIRTUAL | ConstructorDecl::IS_DEFAULT}}; |
| Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 497 | |
| Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 498 | vector<unique_ptr<Declaration>> publics; |
| Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 499 | publics.push_back(std::move(constructor)); |
| 500 | publics.push_back(std::move(destructor)); |
| 501 | |
| Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 502 | for (const auto& method: interface.GetMethods()) { |
| Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 503 | publics.push_back(BuildMethodDecl(*method, types, false)); |
| Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 506 | unique_ptr<ClassDecl> bp_class{ |
| 507 | new ClassDecl{bp_name, |
| Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 508 | "android::BpInterface<" + i_name + ">", |
| Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 509 | std::move(publics), |
| 510 | {} |
| Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 511 | }}; |
| 512 | |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 513 | return unique_ptr<Document>{new CppHeader{ |
| Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 514 | BuildHeaderGuard(interface, ClassNames::CLIENT), |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 515 | {kIBinderHeader, |
| 516 | kIInterfaceHeader, |
| 517 | "utils/Errors.h", |
| Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 518 | HeaderFile(interface, ClassNames::INTERFACE, false)}, |
| Christopher Wiley | b656a3b | 2015-10-16 11:11:09 -0700 | [diff] [blame] | 519 | NestInNamespaces(std::move(bp_class), interface.GetSplitPackage())}}; |
| Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 520 | } |
| 521 | |
| Christopher Wiley | f59c499 | 2015-10-08 13:12:44 -0700 | [diff] [blame] | 522 | unique_ptr<Document> BuildServerHeader(const TypeNamespace& /* types */, |
| Christopher Wiley | fd51d60 | 2015-10-14 13:04:48 -0700 | [diff] [blame] | 523 | const AidlInterface& interface) { |
| 524 | const string i_name = ClassName(interface, ClassNames::INTERFACE); |
| 525 | const string bn_name = ClassName(interface, ClassNames::SERVER); |
| Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 526 | |
| Christopher Wiley | fd51d60 | 2015-10-14 13:04:48 -0700 | [diff] [blame] | 527 | unique_ptr<Declaration> on_transact{new MethodDecl{ |
| Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 528 | kAndroidStatusLiteral, "onTransact", |
| 529 | ArgList{{"uint32_t code", |
| Christopher Wiley | 36570f4 | 2015-10-08 17:20:11 -0700 | [diff] [blame] | 530 | StringPrintf("const %s& data", kAndroidParcelLiteral), |
| 531 | StringPrintf("%s* reply", kAndroidParcelLiteral), |
| Christopher Wiley | fd51d60 | 2015-10-14 13:04:48 -0700 | [diff] [blame] | 532 | "uint32_t flags = 0"}}, |
| 533 | MethodDecl::IS_OVERRIDE |
| 534 | }}; |
| Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 535 | |
| Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 536 | std::vector<unique_ptr<Declaration>> publics; |
| Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 537 | publics.push_back(std::move(on_transact)); |
| 538 | |
| Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 539 | unique_ptr<ClassDecl> bn_class{ |
| 540 | new ClassDecl{bn_name, |
| Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 541 | "android::BnInterface<" + i_name + ">", |
| Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 542 | std::move(publics), |
| 543 | {} |
| Casey Dahlin | b7d0f7f | 2015-09-22 17:21:08 -0700 | [diff] [blame] | 544 | }}; |
| Casey Dahlin | 082f1d1 | 2015-09-21 14:06:25 -0700 | [diff] [blame] | 545 | |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 546 | return unique_ptr<Document>{new CppHeader{ |
| Christopher Wiley | fd51d60 | 2015-10-14 13:04:48 -0700 | [diff] [blame] | 547 | BuildHeaderGuard(interface, ClassNames::SERVER), |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 548 | {"binder/IInterface.h", |
| Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 549 | HeaderFile(interface, ClassNames::INTERFACE, false)}, |
| Christopher Wiley | b656a3b | 2015-10-16 11:11:09 -0700 | [diff] [blame] | 550 | NestInNamespaces(std::move(bn_class), interface.GetSplitPackage())}}; |
| Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 551 | } |
| 552 | |
| Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 553 | unique_ptr<Document> BuildInterfaceHeader(const TypeNamespace& types, |
| Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 554 | const AidlInterface& interface) { |
| Casey Dahlin | ce776cf | 2015-10-15 18:45:54 -0700 | [diff] [blame] | 555 | set<string> includes = { kIBinderHeader, kIInterfaceHeader }; |
| 556 | |
| 557 | for (const auto& method : interface.GetMethods()) { |
| 558 | for (const auto& argument : method->GetArguments()) { |
| 559 | const Type* type = types.Find(argument->GetType().GetName()); |
| 560 | const std::string& header = type->Header(); |
| 561 | if (! header.empty()) |
| 562 | includes.insert(header); |
| Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 563 | if (argument->GetType().IsArray()) |
| 564 | includes.insert("vector"); |
| Casey Dahlin | ce776cf | 2015-10-15 18:45:54 -0700 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | const Type* type = types.Find(method->GetType().GetName()); |
| 568 | const std::string& header = type->Header(); |
| 569 | if (! header.empty()) |
| 570 | includes.insert(header); |
| 571 | } |
| 572 | |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 573 | unique_ptr<ClassDecl> if_class{ |
| Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 574 | new ClassDecl{ClassName(interface, ClassNames::INTERFACE), |
| 575 | "android::IInterface"}}; |
| Christopher Wiley | ade4b45 | 2015-10-10 11:06:03 -0700 | [diff] [blame] | 576 | if_class->AddPublic(unique_ptr<Declaration>{new ConstructorDecl{ |
| 577 | "DECLARE_META_INTERFACE", |
| Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 578 | ArgList{vector<string>{ClassName(interface, ClassNames::BASE)}}}}); |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 579 | |
| 580 | unique_ptr<Enum> call_enum{new Enum{"Call"}}; |
| Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 581 | for (const auto& method : interface.GetMethods()) { |
| 582 | // Each method gets an enum entry and pure virtual declaration. |
| Casey Dahlin | 5c69deb | 2015-10-01 14:44:12 -0700 | [diff] [blame] | 583 | if_class->AddPublic(BuildMethodDecl(*method, types, true)); |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 584 | call_enum->AddValue( |
| Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 585 | UpperCase(method->GetName()), |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 586 | StringPrintf("android::IBinder::FIRST_CALL_TRANSACTION + %d", |
| Casey Dahlin | f4a9311 | 2015-10-05 16:58:09 -0700 | [diff] [blame] | 587 | method->GetId())); |
| Christopher Wiley | 0c732db | 2015-09-29 14:36:44 -0700 | [diff] [blame] | 588 | } |
| 589 | if_class->AddPublic(std::move(call_enum)); |
| 590 | |
| Christopher Wiley | 3bb6bc1 | 2015-10-14 10:58:27 -0700 | [diff] [blame] | 591 | return unique_ptr<Document>{new CppHeader{ |
| 592 | BuildHeaderGuard(interface, ClassNames::INTERFACE), |
| Casey Dahlin | ce776cf | 2015-10-15 18:45:54 -0700 | [diff] [blame] | 593 | vector<string>(includes.begin(), includes.end()), |
| Christopher Wiley | b656a3b | 2015-10-16 11:11:09 -0700 | [diff] [blame] | 594 | NestInNamespaces(std::move(if_class), interface.GetSplitPackage())}}; |
| Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 595 | } |
| 596 | |
| Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 597 | bool WriteHeader(const CppOptions& options, |
| 598 | const TypeNamespace& types, |
| 599 | const AidlInterface& interface, |
| 600 | const IoDelegate& io_delegate, |
| 601 | ClassNames header_type) { |
| 602 | unique_ptr<Document> header; |
| 603 | switch (header_type) { |
| 604 | case ClassNames::INTERFACE: |
| 605 | header = BuildInterfaceHeader(types, interface); |
| 606 | break; |
| 607 | case ClassNames::CLIENT: |
| 608 | header = BuildClientHeader(types, interface); |
| 609 | break; |
| 610 | case ClassNames::SERVER: |
| 611 | header = BuildServerHeader(types, interface); |
| 612 | break; |
| 613 | default: |
| 614 | LOG(FATAL) << "aidl internal error"; |
| 615 | } |
| 616 | if (!header) { |
| 617 | LOG(ERROR) << "aidl internal error: Failed to generate header."; |
| Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 618 | return false; |
| 619 | } |
| Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 620 | |
| 621 | // TODO(wiley): b/25026025 error checking for file I/O |
| 622 | header->Write(io_delegate.GetCodeWriter( |
| 623 | options.OutputHeaderDir() + OS_PATH_SEPARATOR + |
| 624 | HeaderFile(interface, header_type)).get()); |
| 625 | |
| Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 626 | return true; |
| 627 | } |
| 628 | |
| Casey Dahlin | a834dd4 | 2015-09-23 11:52:15 -0700 | [diff] [blame] | 629 | } // namespace internals |
| 630 | |
| 631 | using namespace internals; |
| Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 632 | |
| Christopher Wiley | e3550c6 | 2015-09-29 13:26:10 -0700 | [diff] [blame] | 633 | bool GenerateCpp(const CppOptions& options, |
| 634 | const TypeNamespace& types, |
| Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 635 | const AidlInterface& interface, |
| 636 | const IoDelegate& io_delegate) { |
| 637 | auto interface_src = BuildInterfaceSource(types, interface); |
| 638 | auto client_src = BuildClientSource(types, interface); |
| 639 | auto server_src = BuildServerSource(types, interface); |
| Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 640 | |
| Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 641 | if (!interface_src || !client_src || !server_src) { |
| 642 | return false; |
| 643 | } |
| Christopher Wiley | 9a8e1d9 | 2015-09-19 10:34:33 -0700 | [diff] [blame] | 644 | |
| Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 645 | if (!io_delegate.CreatedNestedDirs(options.OutputHeaderDir(), |
| Christopher Wiley | b656a3b | 2015-10-16 11:11:09 -0700 | [diff] [blame] | 646 | interface.GetSplitPackage())) { |
| Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 647 | LOG(ERROR) << "Failed to create directory structure for headers."; |
| 648 | return false; |
| 649 | } |
| 650 | |
| 651 | if (!WriteHeader(options, types, interface, io_delegate, |
| 652 | ClassNames::INTERFACE) || |
| 653 | !WriteHeader(options, types, interface, io_delegate, |
| 654 | ClassNames::CLIENT) || |
| 655 | !WriteHeader(options, types, interface, io_delegate, |
| 656 | ClassNames::SERVER)) { |
| 657 | return false; |
| 658 | } |
| 659 | |
| 660 | // TODO(wiley): b/25026025 error checking for file I/O. |
| 661 | // If it fails, we should remove all the partial results. |
| 662 | unique_ptr<CodeWriter> writer = io_delegate.GetCodeWriter( |
| 663 | options.OutputCppFilePath()); |
| 664 | interface_src->Write(writer.get()); |
| 665 | client_src->Write(writer.get()); |
| 666 | server_src->Write(writer.get()); |
| 667 | |
| 668 | return true; |
| Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 669 | } |
| 670 | |
| Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 671 | } // namespace cpp |
| Christopher Wiley | eb1acc1 | 2015-09-16 11:25:13 -0700 | [diff] [blame] | 672 | } // namespace aidl |
| Christopher Wiley | f944e79 | 2015-09-29 10:00:46 -0700 | [diff] [blame] | 673 | } // namespace android |