| Christopher Wiley | 3a9da17 | 2016-01-29 11:10:49 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016, 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 | |
| Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 17 | #include "aidl.h" |
| Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 18 | #include "aidl_to_java.h" |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 19 | #include "generate_java.h" |
| Jeongik Cha | 047c5ee | 2019-08-07 23:16:49 +0900 | [diff] [blame] | 20 | #include "logging.h" |
| Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 21 | #include "options.h" |
| Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 22 | |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | |
| Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 27 | #include <algorithm> |
| 28 | #include <unordered_set> |
| Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 29 | #include <utility> |
| 30 | #include <vector> |
| Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 31 | |
| Elliott Hughes | 0a62067 | 2015-12-04 13:53:18 -0800 | [diff] [blame] | 32 | #include <android-base/macros.h> |
| Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 33 | #include <android-base/stringprintf.h> |
| Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 34 | |
| Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 35 | using android::base::Join; |
| Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 36 | using android::base::StringPrintf; |
| Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 37 | |
| Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 38 | using std::string; |
| 39 | using std::unique_ptr; |
| 40 | using std::vector; |
| Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 41 | |
| Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 42 | namespace android { |
| 43 | namespace aidl { |
| Christopher Wiley | db154a5 | 2015-09-28 16:32:25 -0700 | [diff] [blame] | 44 | namespace java { |
| Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 45 | |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 46 | // ================================================= |
| Jiyong Park | 2c44f07 | 2018-07-30 21:52:21 +0900 | [diff] [blame] | 47 | class VariableFactory { |
| 48 | public: |
| 49 | using Variable = ::android::aidl::java::Variable; |
| Jiyong Park | 2c44f07 | 2018-07-30 21:52:21 +0900 | [diff] [blame] | 50 | |
| 51 | explicit VariableFactory(const std::string& base) : base_(base), index_(0) {} |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 52 | std::shared_ptr<Variable> Get(const AidlTypeSpecifier& type) { |
| 53 | auto v = std::make_shared<Variable>(JavaSignatureOf(type), |
| 54 | StringPrintf("%s%d", base_.c_str(), index_)); |
| Jiyong Park | 2c44f07 | 2018-07-30 21:52:21 +0900 | [diff] [blame] | 55 | vars_.push_back(v); |
| 56 | index_++; |
| 57 | return v; |
| 58 | } |
| 59 | |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 60 | std::shared_ptr<Variable> Get(int index) { return vars_[index]; } |
| Jiyong Park | 2c44f07 | 2018-07-30 21:52:21 +0900 | [diff] [blame] | 61 | |
| 62 | private: |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 63 | std::vector<std::shared_ptr<Variable>> vars_; |
| Jiyong Park | 2c44f07 | 2018-07-30 21:52:21 +0900 | [diff] [blame] | 64 | std::string base_; |
| 65 | int index_; |
| 66 | |
| 67 | DISALLOW_COPY_AND_ASSIGN(VariableFactory); |
| 68 | }; |
| 69 | |
| 70 | // ================================================= |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 71 | class StubClass : public Class { |
| 72 | public: |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 73 | StubClass(const AidlInterface* interfaceType, const Options& options); |
| Yi Kong | de13891 | 2019-03-30 01:38:17 -0700 | [diff] [blame] | 74 | ~StubClass() override = default; |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 75 | |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 76 | std::shared_ptr<Variable> transact_code; |
| 77 | std::shared_ptr<Variable> transact_data; |
| 78 | std::shared_ptr<Variable> transact_reply; |
| 79 | std::shared_ptr<Variable> transact_flags; |
| 80 | std::shared_ptr<SwitchStatement> transact_switch; |
| 81 | std::shared_ptr<StatementBlock> transact_statements; |
| 82 | std::shared_ptr<SwitchStatement> code_to_method_name_switch; |
| Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 83 | |
| Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 84 | // Where onTransact cases should be generated as separate methods. |
| 85 | bool transact_outline; |
| Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 86 | // Specific methods that should be outlined when transact_outline is true. |
| 87 | std::unordered_set<const AidlMethod*> outline_methods; |
| 88 | // Number of all methods. |
| 89 | size_t all_method_count; |
| Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 90 | |
| Andreas Gampe | a8a66fe | 2017-11-22 12:17:00 -0800 | [diff] [blame] | 91 | // Finish generation. This will add a default case to the switch. |
| 92 | void finish(); |
| 93 | |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 94 | std::shared_ptr<Expression> get_transact_descriptor(const AidlMethod* method); |
| Andreas Gampe | 7fab0d1 | 2017-11-22 17:50:17 -0800 | [diff] [blame] | 95 | |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 96 | private: |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 97 | void make_as_interface(const AidlInterface* interfaceType); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 98 | |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 99 | std::shared_ptr<Variable> transact_descriptor; |
| Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 100 | const Options& options_; |
| Andreas Gampe | 7fab0d1 | 2017-11-22 17:50:17 -0800 | [diff] [blame] | 101 | |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 102 | DISALLOW_COPY_AND_ASSIGN(StubClass); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 103 | }; |
| 104 | |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 105 | StubClass::StubClass(const AidlInterface* interfaceType, const Options& options) |
| Olivier Gaillard | 1140140 | 2018-07-05 15:01:34 +0100 | [diff] [blame] | 106 | : Class(), options_(options) { |
| Andreas Gampe | 7fab0d1 | 2017-11-22 17:50:17 -0800 | [diff] [blame] | 107 | transact_descriptor = nullptr; |
| Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 108 | transact_outline = false; |
| Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 109 | all_method_count = 0; // Will be set when outlining may be enabled. |
| Andreas Gampe | 7fab0d1 | 2017-11-22 17:50:17 -0800 | [diff] [blame] | 110 | |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 111 | this->comment = "/** Local-side IPC implementation stub class. */"; |
| 112 | this->modifiers = PUBLIC | ABSTRACT | STATIC; |
| 113 | this->what = Class::CLASS; |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 114 | this->type = interfaceType->GetCanonicalName() + ".Stub"; |
| 115 | this->extends = "android.os.Binder"; |
| 116 | this->interfaces.push_back(interfaceType->GetCanonicalName()); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 117 | |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 118 | // descriptor |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 119 | auto descriptor = std::make_shared<Field>( |
| 120 | STATIC | FINAL | PRIVATE, std::make_shared<Variable>("java.lang.String", "DESCRIPTOR")); |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 121 | descriptor->value = "\"" + interfaceType->GetCanonicalName() + "\""; |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 122 | this->elements.push_back(descriptor); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 123 | |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 124 | // ctor |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 125 | auto ctor = std::make_shared<Method>(); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 126 | ctor->modifiers = PUBLIC; |
| 127 | ctor->comment = |
| 128 | "/** Construct the stub at attach it to the " |
| 129 | "interface. */"; |
| 130 | ctor->name = "Stub"; |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 131 | ctor->statements = std::make_shared<StatementBlock>(); |
| 132 | auto attach = std::make_shared<MethodCall>( |
| 133 | THIS_VALUE, "attachInterface", |
| 134 | std::vector<std::shared_ptr<Expression>>{THIS_VALUE, |
| 135 | std::make_shared<LiteralExpression>("DESCRIPTOR")}); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 136 | ctor->statements->Add(attach); |
| 137 | this->elements.push_back(ctor); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 138 | |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 139 | // asInterface |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 140 | make_as_interface(interfaceType); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 141 | |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 142 | // asBinder |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 143 | auto asBinder = std::make_shared<Method>(); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 144 | asBinder->modifiers = PUBLIC | OVERRIDE; |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 145 | asBinder->returnType = "android.os.IBinder"; |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 146 | asBinder->name = "asBinder"; |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 147 | asBinder->statements = std::make_shared<StatementBlock>(); |
| 148 | asBinder->statements->Add(std::make_shared<ReturnStatement>(THIS_VALUE)); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 149 | this->elements.push_back(asBinder); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 150 | |
| Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 151 | if (options_.GenTransactionNames()) { |
| Olivier Gaillard | 83d7cd3 | 2018-07-30 16:20:57 +0100 | [diff] [blame] | 152 | // getDefaultTransactionName |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 153 | auto getDefaultTransactionName = std::make_shared<Method>(); |
| Jiyong Park | e0b2803 | 2019-04-10 03:08:41 +0900 | [diff] [blame] | 154 | getDefaultTransactionName->comment = "/** @hide */"; |
| Olivier Gaillard | 83d7cd3 | 2018-07-30 16:20:57 +0100 | [diff] [blame] | 155 | getDefaultTransactionName->modifiers = PUBLIC | STATIC; |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 156 | getDefaultTransactionName->returnType = "java.lang.String"; |
| Olivier Gaillard | 83d7cd3 | 2018-07-30 16:20:57 +0100 | [diff] [blame] | 157 | getDefaultTransactionName->name = "getDefaultTransactionName"; |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 158 | auto code = std::make_shared<Variable>("int", "transactionCode"); |
| Olivier Gaillard | 83d7cd3 | 2018-07-30 16:20:57 +0100 | [diff] [blame] | 159 | getDefaultTransactionName->parameters.push_back(code); |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 160 | getDefaultTransactionName->statements = std::make_shared<StatementBlock>(); |
| 161 | this->code_to_method_name_switch = std::make_shared<SwitchStatement>(code); |
| Olivier Gaillard | 83d7cd3 | 2018-07-30 16:20:57 +0100 | [diff] [blame] | 162 | getDefaultTransactionName->statements->Add(this->code_to_method_name_switch); |
| 163 | this->elements.push_back(getDefaultTransactionName); |
| 164 | |
| 165 | // getTransactionName |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 166 | auto getTransactionName = std::make_shared<Method>(); |
| Jiyong Park | e0b2803 | 2019-04-10 03:08:41 +0900 | [diff] [blame] | 167 | getTransactionName->comment = "/** @hide */"; |
| Olivier Gaillard | 1140140 | 2018-07-05 15:01:34 +0100 | [diff] [blame] | 168 | getTransactionName->modifiers = PUBLIC; |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 169 | getTransactionName->returnType = "java.lang.String"; |
| Olivier Gaillard | 1140140 | 2018-07-05 15:01:34 +0100 | [diff] [blame] | 170 | getTransactionName->name = "getTransactionName"; |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 171 | auto code2 = std::make_shared<Variable>("int", "transactionCode"); |
| Olivier Gaillard | 83d7cd3 | 2018-07-30 16:20:57 +0100 | [diff] [blame] | 172 | getTransactionName->parameters.push_back(code2); |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 173 | getTransactionName->statements = std::make_shared<StatementBlock>(); |
| 174 | getTransactionName->statements->Add(std::make_shared<ReturnStatement>( |
| 175 | std::make_shared<MethodCall>(THIS_VALUE, "getDefaultTransactionName", |
| 176 | std::vector<std::shared_ptr<Expression>>{code2}))); |
| Olivier Gaillard | 1140140 | 2018-07-05 15:01:34 +0100 | [diff] [blame] | 177 | this->elements.push_back(getTransactionName); |
| 178 | } |
| 179 | |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 180 | // onTransact |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 181 | this->transact_code = std::make_shared<Variable>("int", "code"); |
| 182 | this->transact_data = std::make_shared<Variable>("android.os.Parcel", "data"); |
| 183 | this->transact_reply = std::make_shared<Variable>("android.os.Parcel", "reply"); |
| 184 | this->transact_flags = std::make_shared<Variable>("int", "flags"); |
| 185 | auto onTransact = std::make_shared<Method>(); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 186 | onTransact->modifiers = PUBLIC | OVERRIDE; |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 187 | onTransact->returnType = "boolean"; |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 188 | onTransact->name = "onTransact"; |
| 189 | onTransact->parameters.push_back(this->transact_code); |
| 190 | onTransact->parameters.push_back(this->transact_data); |
| 191 | onTransact->parameters.push_back(this->transact_reply); |
| 192 | onTransact->parameters.push_back(this->transact_flags); |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 193 | onTransact->statements = std::make_shared<StatementBlock>(); |
| Andreas Gampe | 7fab0d1 | 2017-11-22 17:50:17 -0800 | [diff] [blame] | 194 | transact_statements = onTransact->statements; |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 195 | onTransact->exceptions.push_back("android.os.RemoteException"); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 196 | this->elements.push_back(onTransact); |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 197 | this->transact_switch = std::make_shared<SwitchStatement>(this->transact_code); |
| Andreas Gampe | a8a66fe | 2017-11-22 12:17:00 -0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | void StubClass::finish() { |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 201 | auto default_case = std::make_shared<Case>(); |
| Andreas Gampe | a8a66fe | 2017-11-22 12:17:00 -0800 | [diff] [blame] | 202 | |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 203 | auto superCall = std::make_shared<MethodCall>( |
| 204 | SUPER_VALUE, "onTransact", |
| 205 | std::vector<std::shared_ptr<Expression>>{this->transact_code, this->transact_data, |
| 206 | this->transact_reply, this->transact_flags}); |
| 207 | default_case->statements->Add(std::make_shared<ReturnStatement>(superCall)); |
| Andreas Gampe | a8a66fe | 2017-11-22 12:17:00 -0800 | [diff] [blame] | 208 | transact_switch->cases.push_back(default_case); |
| Andreas Gampe | 7fab0d1 | 2017-11-22 17:50:17 -0800 | [diff] [blame] | 209 | |
| 210 | transact_statements->Add(this->transact_switch); |
| Olivier Gaillard | 1140140 | 2018-07-05 15:01:34 +0100 | [diff] [blame] | 211 | |
| 212 | // getTransactionName |
| Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 213 | if (options_.GenTransactionNames()) { |
| Olivier Gaillard | 1140140 | 2018-07-05 15:01:34 +0100 | [diff] [blame] | 214 | // Some transaction codes are common, e.g. INTERFACE_TRANSACTION or DUMP_TRANSACTION. |
| 215 | // Common transaction codes will not be resolved to a string by getTransactionName. The method |
| 216 | // will return NULL in this case. |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 217 | auto code_switch_default_case = std::make_shared<Case>(); |
| 218 | code_switch_default_case->statements->Add(std::make_shared<ReturnStatement>(NULL_VALUE)); |
| Olivier Gaillard | 1140140 | 2018-07-05 15:01:34 +0100 | [diff] [blame] | 219 | this->code_to_method_name_switch->cases.push_back(code_switch_default_case); |
| 220 | } |
| Andreas Gampe | 7fab0d1 | 2017-11-22 17:50:17 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 223 | // The the expression for the interface's descriptor to be used when |
| 224 | // generating code for the given method. Null is acceptable for method |
| 225 | // and stands for synthetic cases. |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 226 | std::shared_ptr<Expression> StubClass::get_transact_descriptor(const AidlMethod* method) { |
| Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 227 | if (transact_outline) { |
| Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 228 | if (method != nullptr) { |
| 229 | // When outlining, each outlined method needs its own literal. |
| 230 | if (outline_methods.count(method) != 0) { |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 231 | return std::make_shared<LiteralExpression>("DESCRIPTOR"); |
| Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 232 | } |
| 233 | } else { |
| 234 | // Synthetic case. A small number is assumed. Use its own descriptor |
| 235 | // if there are only synthetic cases. |
| 236 | if (outline_methods.size() == all_method_count) { |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 237 | return std::make_shared<LiteralExpression>("DESCRIPTOR"); |
| Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 238 | } |
| 239 | } |
| Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 240 | } |
| 241 | |
| Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 242 | // When not outlining, store the descriptor literal into a local variable, in |
| 243 | // an effort to save const-string instructions in each switch case. |
| Andreas Gampe | 7fab0d1 | 2017-11-22 17:50:17 -0800 | [diff] [blame] | 244 | if (transact_descriptor == nullptr) { |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 245 | transact_descriptor = std::make_shared<Variable>("java.lang.String", "descriptor"); |
| 246 | transact_statements->Add(std::make_shared<VariableDeclaration>( |
| 247 | transact_descriptor, std::make_shared<LiteralExpression>("DESCRIPTOR"))); |
| Andreas Gampe | 7fab0d1 | 2017-11-22 17:50:17 -0800 | [diff] [blame] | 248 | } |
| 249 | return transact_descriptor; |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 252 | void StubClass::make_as_interface(const AidlInterface* interfaceType) { |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 253 | auto obj = std::make_shared<Variable>("android.os.IBinder", "obj"); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 254 | |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 255 | auto m = std::make_shared<Method>(); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 256 | m->comment = "/**\n * Cast an IBinder object into an "; |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 257 | m->comment += interfaceType->GetCanonicalName(); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 258 | m->comment += " interface,\n"; |
| 259 | m->comment += " * generating a proxy if needed.\n */"; |
| 260 | m->modifiers = PUBLIC | STATIC; |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 261 | m->returnType = interfaceType->GetCanonicalName(); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 262 | m->name = "asInterface"; |
| 263 | m->parameters.push_back(obj); |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 264 | m->statements = std::make_shared<StatementBlock>(); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 265 | |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 266 | auto ifstatement = std::make_shared<IfStatement>(); |
| 267 | ifstatement->expression = std::make_shared<Comparison>(obj, "==", NULL_VALUE); |
| 268 | ifstatement->statements = std::make_shared<StatementBlock>(); |
| 269 | ifstatement->statements->Add(std::make_shared<ReturnStatement>(NULL_VALUE)); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 270 | m->statements->Add(ifstatement); |
| 271 | |
| 272 | // IInterface iin = obj.queryLocalInterface(DESCRIPTOR) |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 273 | auto queryLocalInterface = std::make_shared<MethodCall>(obj, "queryLocalInterface"); |
| 274 | queryLocalInterface->arguments.push_back(std::make_shared<LiteralExpression>("DESCRIPTOR")); |
| 275 | auto iin = std::make_shared<Variable>("android.os.IInterface", "iin"); |
| 276 | auto iinVd = std::make_shared<VariableDeclaration>(iin, queryLocalInterface); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 277 | m->statements->Add(iinVd); |
| 278 | |
| 279 | // Ensure the instance type of the local object is as expected. |
| 280 | // One scenario where this is needed is if another package (with a |
| 281 | // different class loader) runs in the same process as the service. |
| 282 | |
| 283 | // if (iin != null && iin instanceof <interfaceType>) return (<interfaceType>) |
| 284 | // iin; |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 285 | auto iinNotNull = std::make_shared<Comparison>(iin, "!=", NULL_VALUE); |
| 286 | auto instOfCheck = std::make_shared<Comparison>( |
| 287 | iin, " instanceof ", std::make_shared<LiteralExpression>(interfaceType->GetCanonicalName())); |
| 288 | auto instOfStatement = std::make_shared<IfStatement>(); |
| 289 | instOfStatement->expression = std::make_shared<Comparison>(iinNotNull, "&&", instOfCheck); |
| 290 | instOfStatement->statements = std::make_shared<StatementBlock>(); |
| 291 | instOfStatement->statements->Add(std::make_shared<ReturnStatement>( |
| 292 | std::make_shared<Cast>(interfaceType->GetCanonicalName(), iin))); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 293 | m->statements->Add(instOfStatement); |
| 294 | |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 295 | auto ne = std::make_shared<NewExpression>(interfaceType->GetCanonicalName() + ".Stub.Proxy"); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 296 | ne->arguments.push_back(obj); |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 297 | m->statements->Add(std::make_shared<ReturnStatement>(ne)); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 298 | |
| 299 | this->elements.push_back(m); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 300 | } |
| 301 | |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 302 | // ================================================= |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 303 | class ProxyClass : public Class { |
| 304 | public: |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 305 | ProxyClass(const AidlInterface* interfaceType, const Options& options); |
| Yi Kong | de13891 | 2019-03-30 01:38:17 -0700 | [diff] [blame] | 306 | ~ProxyClass() override; |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 307 | |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 308 | std::shared_ptr<Variable> mRemote; |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 309 | }; |
| 310 | |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 311 | ProxyClass::ProxyClass(const AidlInterface* interfaceType, const Options& options) : Class() { |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 312 | this->modifiers = PRIVATE | STATIC; |
| 313 | this->what = Class::CLASS; |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 314 | this->type = interfaceType->GetCanonicalName() + ".Stub.Proxy"; |
| 315 | this->interfaces.push_back(interfaceType->GetCanonicalName()); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 316 | |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 317 | // IBinder mRemote |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 318 | mRemote = std::make_shared<Variable>("android.os.IBinder", "mRemote"); |
| 319 | this->elements.push_back(std::make_shared<Field>(PRIVATE, mRemote)); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 320 | |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 321 | // Proxy() |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 322 | auto remote = std::make_shared<Variable>("android.os.IBinder", "remote"); |
| 323 | auto ctor = std::make_shared<Method>(); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 324 | ctor->name = "Proxy"; |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 325 | ctor->statements = std::make_shared<StatementBlock>(); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 326 | ctor->parameters.push_back(remote); |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 327 | ctor->statements->Add(std::make_shared<Assignment>(mRemote, remote)); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 328 | this->elements.push_back(ctor); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 329 | |
| Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 330 | if (options.Version() > 0) { |
| 331 | std::ostringstream code; |
| 332 | code << "private int mCachedVersion = -1;\n"; |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 333 | this->elements.emplace_back(std::make_shared<LiteralClassElement>(code.str())); |
| Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 334 | } |
| 335 | |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 336 | // IBinder asBinder() |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 337 | auto asBinder = std::make_shared<Method>(); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 338 | asBinder->modifiers = PUBLIC | OVERRIDE; |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 339 | asBinder->returnType = "android.os.IBinder"; |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 340 | asBinder->name = "asBinder"; |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 341 | asBinder->statements = std::make_shared<StatementBlock>(); |
| 342 | asBinder->statements->Add(std::make_shared<ReturnStatement>(mRemote)); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 343 | this->elements.push_back(asBinder); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 344 | } |
| 345 | |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 346 | ProxyClass::~ProxyClass() {} |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 347 | |
| 348 | // ================================================= |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 349 | static void generate_new_array(const AidlTypeSpecifier& type, std::shared_ptr<StatementBlock> addTo, |
| 350 | std::shared_ptr<Variable> v, std::shared_ptr<Variable> parcel) { |
| 351 | auto len = std::make_shared<Variable>("int", v->name + "_length"); |
| 352 | addTo->Add( |
| 353 | std::make_shared<VariableDeclaration>(len, std::make_shared<MethodCall>(parcel, "readInt"))); |
| 354 | auto lencheck = std::make_shared<IfStatement>(); |
| 355 | lencheck->expression = |
| 356 | std::make_shared<Comparison>(len, "<", std::make_shared<LiteralExpression>("0")); |
| 357 | lencheck->statements->Add(std::make_shared<Assignment>(v, NULL_VALUE)); |
| 358 | lencheck->elseif = std::make_shared<IfStatement>(); |
| 359 | lencheck->elseif->statements->Add(std::make_shared<Assignment>( |
| 360 | v, std::make_shared<NewArrayExpression>(InstantiableJavaSignatureOf(type), len))); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 361 | addTo->Add(lencheck); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 362 | } |
| 363 | |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 364 | static void generate_write_to_parcel(const AidlTypeSpecifier& type, |
| 365 | std::shared_ptr<StatementBlock> addTo, |
| 366 | std::shared_ptr<Variable> v, std::shared_ptr<Variable> parcel, |
| 367 | bool is_return_value, const AidlTypenames& typenames) { |
| Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 368 | string code; |
| 369 | CodeWriterPtr writer = CodeWriter::ForString(&code); |
| 370 | CodeGeneratorContext context{ |
| 371 | .writer = *(writer.get()), |
| 372 | .typenames = typenames, |
| 373 | .type = type, |
| 374 | .var = v->name, |
| 375 | .parcel = parcel->name, |
| 376 | .is_return_value = is_return_value, |
| 377 | }; |
| 378 | WriteToParcelFor(context); |
| 379 | writer->Close(); |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 380 | addTo->Add(std::make_shared<LiteralStatement>(code)); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 381 | } |
| 382 | |
| Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 383 | static void generate_int_constant(Class* interface, const std::string& name, |
| 384 | const std::string& value) { |
| Jeongik Cha | de157cc | 2019-02-12 12:41:27 +0900 | [diff] [blame] | 385 | auto code = StringPrintf("public static final int %s = %s;\n", name.c_str(), value.c_str()); |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 386 | interface->elements.push_back(std::make_shared<LiteralClassElement>(code)); |
| Christopher Wiley | 69b44cf | 2016-05-03 13:43:33 -0700 | [diff] [blame] | 387 | } |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 388 | |
| Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 389 | static void generate_string_constant(Class* interface, const std::string& name, |
| 390 | const std::string& value) { |
| Jeongik Cha | de157cc | 2019-02-12 12:41:27 +0900 | [diff] [blame] | 391 | auto code = StringPrintf("public static final String %s = %s;\n", name.c_str(), value.c_str()); |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 392 | interface->elements.push_back(std::make_shared<LiteralClassElement>(code)); |
| Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 393 | } |
| 394 | |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 395 | static std::shared_ptr<Method> generate_interface_method(const AidlMethod& method) { |
| 396 | auto decl = std::make_shared<Method>(); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 397 | decl->comment = method.GetComments(); |
| 398 | decl->modifiers = PUBLIC; |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 399 | decl->returnType = JavaSignatureOf(method.GetType()); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 400 | decl->name = method.GetName(); |
| Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 401 | decl->annotations = generate_java_annotations(method.GetType()); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 402 | |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 403 | for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) { |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 404 | decl->parameters.push_back( |
| 405 | std::make_shared<Variable>(JavaSignatureOf(arg->GetType()), arg->GetName())); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 406 | } |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 407 | |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 408 | decl->exceptions.push_back("android.os.RemoteException"); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 409 | |
| Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 410 | return decl; |
| 411 | } |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 412 | |
| Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 413 | static void generate_stub_code(const AidlInterface& iface, const AidlMethod& method, bool oneway, |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 414 | std::shared_ptr<Variable> transact_data, |
| 415 | std::shared_ptr<Variable> transact_reply, |
| 416 | const AidlTypenames& typenames, |
| 417 | std::shared_ptr<StatementBlock> statements, |
| 418 | std::shared_ptr<StubClass> stubClass, const Options& options) { |
| 419 | std::shared_ptr<TryStatement> tryStatement; |
| 420 | std::shared_ptr<FinallyStatement> finallyStatement; |
| 421 | auto realCall = std::make_shared<MethodCall>(THIS_VALUE, method.GetName()); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 422 | |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 423 | // interface token validation is the very first thing we do |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 424 | statements->Add(std::make_shared<MethodCall>( |
| 425 | transact_data, "enforceInterface", |
| 426 | std::vector<std::shared_ptr<Expression>>{stubClass->get_transact_descriptor(&method)})); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 427 | |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 428 | // args |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 429 | VariableFactory stubArgs("_arg"); |
| Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 430 | { |
| Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 431 | // keep this across different args in order to create the classloader |
| 432 | // at most once. |
| 433 | bool is_classloader_created = false; |
| Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 434 | for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) { |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 435 | std::shared_ptr<Variable> v = stubArgs.Get(arg->GetType()); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 436 | |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 437 | statements->Add(std::make_shared<VariableDeclaration>(v)); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 438 | |
| Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 439 | if (arg->GetDirection() & AidlArgument::IN_DIR) { |
| Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 440 | string code; |
| 441 | CodeWriterPtr writer = CodeWriter::ForString(&code); |
| 442 | CodeGeneratorContext context{.writer = *(writer.get()), |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 443 | .typenames = typenames, |
| Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 444 | .type = arg->GetType(), |
| 445 | .var = v->name, |
| 446 | .parcel = transact_data->name, |
| 447 | .is_classloader_created = &is_classloader_created}; |
| 448 | CreateFromParcelFor(context); |
| 449 | writer->Close(); |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 450 | statements->Add(std::make_shared<LiteralStatement>(code)); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 451 | } else { |
| Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 452 | if (!arg->GetType().IsArray()) { |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 453 | statements->Add(std::make_shared<Assignment>( |
| 454 | v, std::make_shared<NewExpression>(InstantiableJavaSignatureOf(arg->GetType())))); |
| Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 455 | } else { |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 456 | generate_new_array(arg->GetType(), statements, v, transact_data); |
| Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 457 | } |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 458 | } |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 459 | |
| Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 460 | realCall->arguments.push_back(v); |
| 461 | } |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 462 | } |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 463 | |
| Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 464 | if (options.GenTraces()) { |
| Martijn Coenen | f1b5078 | 2018-02-21 21:06:23 +0100 | [diff] [blame] | 465 | // try and finally, but only when generating trace code |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 466 | tryStatement = std::make_shared<TryStatement>(); |
| 467 | finallyStatement = std::make_shared<FinallyStatement>(); |
| Martijn Coenen | f1b5078 | 2018-02-21 21:06:23 +0100 | [diff] [blame] | 468 | |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 469 | tryStatement->statements->Add(std::make_shared<MethodCall>( |
| 470 | std::make_shared<LiteralExpression>("android.os.Trace"), "traceBegin", |
| 471 | std::vector<std::shared_ptr<Expression>>{ |
| 472 | std::make_shared<LiteralExpression>("android.os.Trace.TRACE_TAG_AIDL"), |
| 473 | std::make_shared<StringLiteralExpression>(iface.GetName() + "::" + method.GetName() + |
| 474 | "::server")})); |
| Martijn Coenen | f1b5078 | 2018-02-21 21:06:23 +0100 | [diff] [blame] | 475 | |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 476 | finallyStatement->statements->Add(std::make_shared<MethodCall>( |
| 477 | std::make_shared<LiteralExpression>("android.os.Trace"), "traceEnd", |
| 478 | std::vector<std::shared_ptr<Expression>>{ |
| 479 | std::make_shared<LiteralExpression>("android.os.Trace.TRACE_TAG_AIDL")})); |
| Martijn Coenen | f1b5078 | 2018-02-21 21:06:23 +0100 | [diff] [blame] | 480 | } |
| 481 | |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 482 | // the real call |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 483 | if (method.GetType().GetName() == "void") { |
| Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 484 | if (options.GenTraces()) { |
| Martijn Coenen | f1b5078 | 2018-02-21 21:06:23 +0100 | [diff] [blame] | 485 | statements->Add(tryStatement); |
| 486 | tryStatement->statements->Add(realCall); |
| 487 | statements->Add(finallyStatement); |
| 488 | } else { |
| 489 | statements->Add(realCall); |
| 490 | } |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 491 | |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 492 | if (!oneway) { |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 493 | // report that there were no exceptions |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 494 | auto ex = std::make_shared<MethodCall>(transact_reply, "writeNoException"); |
| Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 495 | statements->Add(ex); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 496 | } |
| 497 | } else { |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 498 | auto _result = std::make_shared<Variable>(JavaSignatureOf(method.GetType()), "_result"); |
| Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 499 | if (options.GenTraces()) { |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 500 | statements->Add(std::make_shared<VariableDeclaration>(_result)); |
| Martijn Coenen | f1b5078 | 2018-02-21 21:06:23 +0100 | [diff] [blame] | 501 | statements->Add(tryStatement); |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 502 | tryStatement->statements->Add(std::make_shared<Assignment>(_result, realCall)); |
| Martijn Coenen | f1b5078 | 2018-02-21 21:06:23 +0100 | [diff] [blame] | 503 | statements->Add(finallyStatement); |
| 504 | } else { |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 505 | statements->Add(std::make_shared<VariableDeclaration>(_result, realCall)); |
| Martijn Coenen | f1b5078 | 2018-02-21 21:06:23 +0100 | [diff] [blame] | 506 | } |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 507 | |
| 508 | if (!oneway) { |
| 509 | // report that there were no exceptions |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 510 | auto ex = std::make_shared<MethodCall>(transact_reply, "writeNoException"); |
| Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 511 | statements->Add(ex); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 512 | } |
| 513 | |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 514 | // marshall the return value |
| Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 515 | generate_write_to_parcel(method.GetType(), statements, _result, transact_reply, true, |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 516 | typenames); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | // out parameters |
| Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 520 | int i = 0; |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 521 | for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) { |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 522 | std::shared_ptr<Variable> v = stubArgs.Get(i++); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 523 | |
| 524 | if (arg->GetDirection() & AidlArgument::OUT_DIR) { |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 525 | generate_write_to_parcel(arg->GetType(), statements, v, transact_reply, true, typenames); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 526 | } |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 527 | } |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 528 | |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 529 | // return true |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 530 | statements->Add(std::make_shared<ReturnStatement>(TRUE_VALUE)); |
| Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 531 | } |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 532 | |
| Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 533 | static void generate_stub_case(const AidlInterface& iface, const AidlMethod& method, |
| 534 | const std::string& transactCodeName, bool oneway, |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 535 | std::shared_ptr<StubClass> stubClass, const AidlTypenames& typenames, |
| Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 536 | const Options& options) { |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 537 | auto c = std::make_shared<Case>(transactCodeName); |
| Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 538 | |
| Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 539 | generate_stub_code(iface, method, oneway, stubClass->transact_data, stubClass->transact_reply, |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 540 | typenames, c->statements, stubClass, options); |
| Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 541 | |
| 542 | stubClass->transact_switch->cases.push_back(c); |
| 543 | } |
| 544 | |
| Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 545 | static void generate_stub_case_outline(const AidlInterface& iface, const AidlMethod& method, |
| 546 | const std::string& transactCodeName, bool oneway, |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 547 | std::shared_ptr<StubClass> stubClass, |
| 548 | const AidlTypenames& typenames, const Options& options) { |
| Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 549 | std::string outline_name = "onTransact$" + method.GetName() + "$"; |
| 550 | // Generate an "outlined" method with the actual code. |
| 551 | { |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 552 | auto transact_data = std::make_shared<Variable>("android.os.Parcel", "data"); |
| 553 | auto transact_reply = std::make_shared<Variable>("android.os.Parcel", "reply"); |
| 554 | auto onTransact_case = std::make_shared<Method>(); |
| Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 555 | onTransact_case->modifiers = PRIVATE; |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 556 | onTransact_case->returnType = "boolean"; |
| Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 557 | onTransact_case->name = outline_name; |
| 558 | onTransact_case->parameters.push_back(transact_data); |
| 559 | onTransact_case->parameters.push_back(transact_reply); |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 560 | onTransact_case->statements = std::make_shared<StatementBlock>(); |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 561 | onTransact_case->exceptions.push_back("android.os.RemoteException"); |
| Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 562 | stubClass->elements.push_back(onTransact_case); |
| 563 | |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 564 | generate_stub_code(iface, method, oneway, transact_data, transact_reply, typenames, |
| Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 565 | onTransact_case->statements, stubClass, options); |
| Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | // Generate the case dispatch. |
| 569 | { |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 570 | auto c = std::make_shared<Case>(transactCodeName); |
| Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 571 | |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 572 | auto helper_call = |
| 573 | std::make_shared<MethodCall>(THIS_VALUE, outline_name, |
| 574 | std::vector<std::shared_ptr<Expression>>{ |
| 575 | stubClass->transact_data, stubClass->transact_reply}); |
| 576 | c->statements->Add(std::make_shared<ReturnStatement>(helper_call)); |
| Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 577 | |
| 578 | stubClass->transact_switch->cases.push_back(c); |
| 579 | } |
| 580 | } |
| 581 | |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 582 | static std::shared_ptr<Method> generate_proxy_method( |
| Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 583 | const AidlInterface& iface, const AidlMethod& method, const std::string& transactCodeName, |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 584 | bool oneway, std::shared_ptr<ProxyClass> proxyClass, const AidlTypenames& typenames, |
| 585 | const Options& options) { |
| 586 | auto proxy = std::make_shared<Method>(); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 587 | proxy->comment = method.GetComments(); |
| 588 | proxy->modifiers = PUBLIC | OVERRIDE; |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 589 | proxy->returnType = JavaSignatureOf(method.GetType()); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 590 | proxy->name = method.GetName(); |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 591 | proxy->statements = std::make_shared<StatementBlock>(); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 592 | for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) { |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 593 | proxy->parameters.push_back( |
| 594 | std::make_shared<Variable>(JavaSignatureOf(arg->GetType()), arg->GetName())); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 595 | } |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 596 | proxy->exceptions.push_back("android.os.RemoteException"); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 597 | |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 598 | // the parcels |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 599 | auto _data = std::make_shared<Variable>("android.os.Parcel", "_data"); |
| 600 | proxy->statements->Add(std::make_shared<VariableDeclaration>( |
| 601 | _data, std::make_shared<MethodCall>("android.os.Parcel", "obtain"))); |
| 602 | std::shared_ptr<Variable> _reply = nullptr; |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 603 | if (!oneway) { |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 604 | _reply = std::make_shared<Variable>("android.os.Parcel", "_reply"); |
| 605 | proxy->statements->Add(std::make_shared<VariableDeclaration>( |
| 606 | _reply, std::make_shared<MethodCall>("android.os.Parcel", "obtain"))); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | // the return value |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 610 | std::shared_ptr<Variable> _result = nullptr; |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 611 | if (method.GetType().GetName() != "void") { |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 612 | _result = std::make_shared<Variable>(*proxy->returnType, "_result"); |
| 613 | proxy->statements->Add(std::make_shared<VariableDeclaration>(_result)); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | // try and finally |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 617 | auto tryStatement = std::make_shared<TryStatement>(); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 618 | proxy->statements->Add(tryStatement); |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 619 | auto finallyStatement = std::make_shared<FinallyStatement>(); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 620 | proxy->statements->Add(finallyStatement); |
| 621 | |
| Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 622 | if (options.GenTraces()) { |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 623 | tryStatement->statements->Add(std::make_shared<MethodCall>( |
| 624 | std::make_shared<LiteralExpression>("android.os.Trace"), "traceBegin", |
| 625 | std::vector<std::shared_ptr<Expression>>{ |
| 626 | std::make_shared<LiteralExpression>("android.os.Trace.TRACE_TAG_AIDL"), |
| 627 | std::make_shared<StringLiteralExpression>(iface.GetName() + "::" + method.GetName() + |
| 628 | "::client")})); |
| Martijn Coenen | f1b5078 | 2018-02-21 21:06:23 +0100 | [diff] [blame] | 629 | } |
| 630 | |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 631 | // the interface identifier token: the DESCRIPTOR constant, marshalled as a |
| 632 | // string |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 633 | tryStatement->statements->Add(std::make_shared<MethodCall>( |
| 634 | _data, "writeInterfaceToken", |
| 635 | std::vector<std::shared_ptr<Expression>>{std::make_shared<LiteralExpression>("DESCRIPTOR")})); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 636 | |
| 637 | // the parameters |
| 638 | for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) { |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 639 | auto v = std::make_shared<Variable>(JavaSignatureOf(arg->GetType()), arg->GetName()); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 640 | AidlArgument::Direction dir = arg->GetDirection(); |
| 641 | if (dir == AidlArgument::OUT_DIR && arg->GetType().IsArray()) { |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 642 | auto checklen = std::make_shared<IfStatement>(); |
| 643 | checklen->expression = std::make_shared<Comparison>(v, "==", NULL_VALUE); |
| 644 | checklen->statements->Add(std::make_shared<MethodCall>( |
| 645 | _data, "writeInt", |
| 646 | std::vector<std::shared_ptr<Expression>>{std::make_shared<LiteralExpression>("-1")})); |
| 647 | checklen->elseif = std::make_shared<IfStatement>(); |
| 648 | checklen->elseif->statements->Add(std::make_shared<MethodCall>( |
| 649 | _data, "writeInt", |
| 650 | std::vector<std::shared_ptr<Expression>>{std::make_shared<FieldVariable>(v, "length")})); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 651 | tryStatement->statements->Add(checklen); |
| 652 | } else if (dir & AidlArgument::IN_DIR) { |
| Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 653 | generate_write_to_parcel(arg->GetType(), tryStatement->statements, v, _data, false, |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 654 | typenames); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 655 | } |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 656 | } |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 657 | |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 658 | // the transact call |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 659 | auto call = std::make_shared<MethodCall>( |
| 660 | proxyClass->mRemote, "transact", |
| 661 | std::vector<std::shared_ptr<Expression>>{ |
| 662 | std::make_shared<LiteralExpression>("Stub." + transactCodeName), _data, |
| 663 | _reply ? _reply : NULL_VALUE, |
| 664 | std::make_shared<LiteralExpression>(oneway ? "android.os.IBinder.FLAG_ONEWAY" : "0")}); |
| 665 | auto _status = std::make_shared<Variable>("boolean", "_status"); |
| 666 | tryStatement->statements->Add(std::make_shared<VariableDeclaration>(_status, call)); |
| Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 667 | |
| 668 | // If the transaction returns false, which means UNKNOWN_TRANSACTION, fall |
| 669 | // back to the local method in the default impl, if set before. |
| 670 | vector<string> arg_names; |
| 671 | for (const auto& arg : method.GetArguments()) { |
| 672 | arg_names.emplace_back(arg->GetName()); |
| 673 | } |
| 674 | bool has_return_type = method.GetType().GetName() != "void"; |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 675 | tryStatement->statements->Add(std::make_shared<LiteralStatement>( |
| Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 676 | android::base::StringPrintf(has_return_type ? "if (!_status && getDefaultImpl() != null) {\n" |
| 677 | " return getDefaultImpl().%s(%s);\n" |
| 678 | "}\n" |
| 679 | : "if (!_status && getDefaultImpl() != null) {\n" |
| 680 | " getDefaultImpl().%s(%s);\n" |
| 681 | " return;\n" |
| 682 | "}\n", |
| 683 | method.GetName().c_str(), Join(arg_names, ", ").c_str()))); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 684 | |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 685 | // throw back exceptions. |
| 686 | if (_reply) { |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 687 | auto ex = std::make_shared<MethodCall>(_reply, "readException"); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 688 | tryStatement->statements->Add(ex); |
| 689 | } |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 690 | |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 691 | // returning and cleanup |
| Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 692 | if (_reply != nullptr) { |
| Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 693 | // keep this across return value and arguments in order to create the |
| 694 | // classloader at most once. |
| 695 | bool is_classloader_created = false; |
| Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 696 | if (_result != nullptr) { |
| Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 697 | string code; |
| 698 | CodeWriterPtr writer = CodeWriter::ForString(&code); |
| 699 | CodeGeneratorContext context{.writer = *(writer.get()), |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 700 | .typenames = typenames, |
| Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 701 | .type = method.GetType(), |
| 702 | .var = _result->name, |
| 703 | .parcel = _reply->name, |
| 704 | .is_classloader_created = &is_classloader_created}; |
| 705 | CreateFromParcelFor(context); |
| 706 | writer->Close(); |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 707 | tryStatement->statements->Add(std::make_shared<LiteralStatement>(code)); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 708 | } |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 709 | |
| 710 | // the out/inout parameters |
| 711 | for (const std::unique_ptr<AidlArgument>& arg : method.GetArguments()) { |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 712 | if (arg->GetDirection() & AidlArgument::OUT_DIR) { |
| Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 713 | string code; |
| 714 | CodeWriterPtr writer = CodeWriter::ForString(&code); |
| 715 | CodeGeneratorContext context{.writer = *(writer.get()), |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 716 | .typenames = typenames, |
| Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 717 | .type = arg->GetType(), |
| 718 | .var = arg->GetName(), |
| 719 | .parcel = _reply->name, |
| 720 | .is_classloader_created = &is_classloader_created}; |
| 721 | ReadFromParcelFor(context); |
| 722 | writer->Close(); |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 723 | tryStatement->statements->Add(std::make_shared<LiteralStatement>(code)); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 724 | } |
| 725 | } |
| 726 | |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 727 | finallyStatement->statements->Add(std::make_shared<MethodCall>(_reply, "recycle")); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 728 | } |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 729 | finallyStatement->statements->Add(std::make_shared<MethodCall>(_data, "recycle")); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 730 | |
| Jiyong Park | fbbfa93 | 2018-07-30 21:44:10 +0900 | [diff] [blame] | 731 | if (options.GenTraces()) { |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 732 | finallyStatement->statements->Add(std::make_shared<MethodCall>( |
| 733 | std::make_shared<LiteralExpression>("android.os.Trace"), "traceEnd", |
| 734 | std::vector<std::shared_ptr<Expression>>{ |
| 735 | std::make_shared<LiteralExpression>("android.os.Trace.TRACE_TAG_AIDL")})); |
| Martijn Coenen | f1b5078 | 2018-02-21 21:06:23 +0100 | [diff] [blame] | 736 | } |
| 737 | |
| Yi Kong | 894d6ba | 2018-07-24 11:27:38 -0700 | [diff] [blame] | 738 | if (_result != nullptr) { |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 739 | proxy->statements->Add(std::make_shared<ReturnStatement>(_result)); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 740 | } |
| Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 741 | |
| 742 | return proxy; |
| 743 | } |
| 744 | |
| Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 745 | static void generate_methods(const AidlInterface& iface, const AidlMethod& method, Class* interface, |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 746 | std::shared_ptr<StubClass> stubClass, |
| 747 | std::shared_ptr<ProxyClass> proxyClass, int index, |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 748 | const AidlTypenames& typenames, const Options& options) { |
| Steven Moreland | acd5347 | 2018-12-14 10:17:26 -0800 | [diff] [blame] | 749 | const bool oneway = method.IsOneway(); |
| Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 750 | |
| 751 | // == the TRANSACT_ constant ============================================= |
| 752 | string transactCodeName = "TRANSACTION_"; |
| 753 | transactCodeName += method.GetName(); |
| 754 | |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 755 | auto transactCode = |
| 756 | std::make_shared<Field>(STATIC | FINAL, std::make_shared<Variable>("int", transactCodeName)); |
| Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 757 | transactCode->value = |
| 758 | StringPrintf("(android.os.IBinder.FIRST_CALL_TRANSACTION + %d)", index); |
| 759 | stubClass->elements.push_back(transactCode); |
| 760 | |
| Olivier Gaillard | 1140140 | 2018-07-05 15:01:34 +0100 | [diff] [blame] | 761 | // getTransactionName |
| Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 762 | if (options.GenTransactionNames()) { |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 763 | auto c = std::make_shared<Case>(transactCodeName); |
| 764 | c->statements->Add(std::make_shared<ReturnStatement>( |
| 765 | std::make_shared<StringLiteralExpression>(method.GetName()))); |
| Olivier Gaillard | 1140140 | 2018-07-05 15:01:34 +0100 | [diff] [blame] | 766 | stubClass->code_to_method_name_switch->cases.push_back(c); |
| 767 | } |
| 768 | |
| Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 769 | // == the declaration in the interface =================================== |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 770 | std::shared_ptr<ClassElement> decl; |
| Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 771 | if (method.IsUserDefined()) { |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 772 | decl = generate_interface_method(method); |
| Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 773 | } else { |
| 774 | if (method.GetName() == kGetInterfaceVersion && options.Version() > 0) { |
| 775 | std::ostringstream code; |
| 776 | code << "public int " << kGetInterfaceVersion << "() " |
| 777 | << "throws android.os.RemoteException;\n"; |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 778 | decl = std::make_shared<LiteralClassElement>(code.str()); |
| Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 779 | } |
| 780 | } |
| Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 781 | interface->elements.push_back(decl); |
| 782 | |
| 783 | // == the stub method ==================================================== |
| Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 784 | if (method.IsUserDefined()) { |
| 785 | bool outline_stub = |
| 786 | stubClass->transact_outline && stubClass->outline_methods.count(&method) != 0; |
| 787 | if (outline_stub) { |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 788 | generate_stub_case_outline(iface, method, transactCodeName, oneway, stubClass, typenames, |
| Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 789 | options); |
| 790 | } else { |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 791 | generate_stub_case(iface, method, transactCodeName, oneway, stubClass, typenames, options); |
| Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 792 | } |
| Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 793 | } else { |
| Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 794 | if (method.GetName() == kGetInterfaceVersion && options.Version() > 0) { |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 795 | auto c = std::make_shared<Case>(transactCodeName); |
| Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 796 | std::ostringstream code; |
| Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 797 | code << "data.enforceInterface(descriptor);\n" |
| 798 | << "reply.writeNoException();\n" |
| 799 | << "reply.writeInt(" << kGetInterfaceVersion << "());\n" |
| Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 800 | << "return true;\n"; |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 801 | c->statements->Add(std::make_shared<LiteralStatement>(code.str())); |
| Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 802 | stubClass->transact_switch->cases.push_back(c); |
| 803 | } |
| Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 804 | } |
| Andreas Gampe | 7d7fa60 | 2017-11-22 10:50:03 -0800 | [diff] [blame] | 805 | |
| 806 | // == the proxy method =================================================== |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 807 | std::shared_ptr<ClassElement> proxy = nullptr; |
| Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 808 | if (method.IsUserDefined()) { |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 809 | proxy = generate_proxy_method(iface, method, transactCodeName, oneway, proxyClass, typenames, |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 810 | options); |
| Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 811 | |
| 812 | } else { |
| 813 | if (method.GetName() == kGetInterfaceVersion && options.Version() > 0) { |
| 814 | std::ostringstream code; |
| 815 | code << "@Override\n" |
| 816 | << "public int " << kGetInterfaceVersion << "()" |
| 817 | << " throws " |
| 818 | << "android.os.RemoteException {\n" |
| 819 | << " if (mCachedVersion == -1) {\n" |
| 820 | << " android.os.Parcel data = android.os.Parcel.obtain();\n" |
| 821 | << " android.os.Parcel reply = android.os.Parcel.obtain();\n" |
| 822 | << " try {\n" |
| Jiyong Park | 965c5b9 | 2018-11-21 13:37:15 +0900 | [diff] [blame] | 823 | << " data.writeInterfaceToken(DESCRIPTOR);\n" |
| Jiyong Park | 6b39de4 | 2019-08-27 13:04:57 +0900 | [diff] [blame] | 824 | << " boolean _status = mRemote.transact(Stub." << transactCodeName << ", " |
| Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 825 | << "data, reply, 0);\n" |
| Jiyong Park | 6b39de4 | 2019-08-27 13:04:57 +0900 | [diff] [blame] | 826 | << " if (!_status) {\n" |
| 827 | << " if (getDefaultImpl() != null) {\n" |
| 828 | << " return getDefaultImpl().getInterfaceVersion();\n" |
| 829 | << " }\n" |
| 830 | << " }\n" |
| Jeongik Cha | f1470e2 | 2019-05-20 18:45:05 +0900 | [diff] [blame] | 831 | << " reply.readException();\n" |
| Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 832 | << " mCachedVersion = reply.readInt();\n" |
| 833 | << " } finally {\n" |
| 834 | << " reply.recycle();\n" |
| 835 | << " data.recycle();\n" |
| 836 | << " }\n" |
| 837 | << " }\n" |
| 838 | << " return mCachedVersion;\n" |
| 839 | << "}\n"; |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 840 | proxy = std::make_shared<LiteralClassElement>(code.str()); |
| Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 841 | } |
| 842 | } |
| 843 | if (proxy != nullptr) { |
| 844 | proxyClass->elements.push_back(proxy); |
| 845 | } |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 846 | } |
| 847 | |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 848 | static void generate_interface_descriptors(std::shared_ptr<StubClass> stub, |
| 849 | std::shared_ptr<ProxyClass> proxy) { |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 850 | // the interface descriptor transaction handler |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 851 | auto c = std::make_shared<Case>("INTERFACE_TRANSACTION"); |
| 852 | c->statements->Add(std::make_shared<MethodCall>( |
| 853 | stub->transact_reply, "writeString", |
| 854 | std::vector<std::shared_ptr<Expression>>{stub->get_transact_descriptor(nullptr)})); |
| 855 | c->statements->Add(std::make_shared<ReturnStatement>(TRUE_VALUE)); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 856 | stub->transact_switch->cases.push_back(c); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 857 | |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 858 | // and the proxy-side method returning the descriptor directly |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 859 | auto getDesc = std::make_shared<Method>(); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 860 | getDesc->modifiers = PUBLIC; |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 861 | getDesc->returnType = "java.lang.String"; |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 862 | getDesc->name = "getInterfaceDescriptor"; |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 863 | getDesc->statements = std::make_shared<StatementBlock>(); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 864 | getDesc->statements->Add( |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 865 | std::make_shared<ReturnStatement>(std::make_shared<LiteralExpression>("DESCRIPTOR"))); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 866 | proxy->elements.push_back(getDesc); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 867 | } |
| 868 | |
| Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 869 | // Check whether (some) methods in this interface should be "outlined," that |
| 870 | // is, have specific onTransact methods for certain cases. Set up StubClass |
| 871 | // metadata accordingly. |
| 872 | // |
| 873 | // Outlining will be enabled if the interface has more than outline_threshold |
| 874 | // methods. In that case, the methods are sorted by number of arguments |
| 875 | // (so that more "complex" methods come later), and the first non_outline_count |
| 876 | // number of methods not outlined (are kept in the onTransact() method). |
| 877 | // |
| 878 | // Requirements: non_outline_count <= outline_threshold. |
| 879 | static void compute_outline_methods(const AidlInterface* iface, |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 880 | const std::shared_ptr<StubClass> stub, size_t outline_threshold, |
| Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 881 | size_t non_outline_count) { |
| 882 | CHECK_LE(non_outline_count, outline_threshold); |
| 883 | // We'll outline (create sub methods) if there are more than min_methods |
| 884 | // cases. |
| 885 | stub->transact_outline = iface->GetMethods().size() > outline_threshold; |
| 886 | if (stub->transact_outline) { |
| 887 | stub->all_method_count = iface->GetMethods().size(); |
| 888 | std::vector<const AidlMethod*> methods; |
| 889 | methods.reserve(iface->GetMethods().size()); |
| 890 | for (const std::unique_ptr<AidlMethod>& ptr : iface->GetMethods()) { |
| 891 | methods.push_back(ptr.get()); |
| 892 | } |
| 893 | |
| 894 | std::stable_sort( |
| 895 | methods.begin(), |
| 896 | methods.end(), |
| 897 | [](const AidlMethod* m1, const AidlMethod* m2) { |
| 898 | return m1->GetArguments().size() < m2->GetArguments().size(); |
| 899 | }); |
| 900 | |
| 901 | stub->outline_methods.insert(methods.begin() + non_outline_count, |
| 902 | methods.end()); |
| 903 | } |
| 904 | } |
| 905 | |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 906 | static shared_ptr<ClassElement> generate_default_impl_method(const AidlMethod& method) { |
| 907 | auto default_method = std::make_shared<Method>(); |
| Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 908 | default_method->comment = method.GetComments(); |
| 909 | default_method->modifiers = PUBLIC | OVERRIDE; |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 910 | default_method->returnType = JavaSignatureOf(method.GetType()); |
| Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 911 | default_method->name = method.GetName(); |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 912 | default_method->statements = std::make_shared<StatementBlock>(); |
| Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 913 | for (const auto& arg : method.GetArguments()) { |
| Steven Moreland | 3dc29d8 | 2019-08-21 17:23:11 -0700 | [diff] [blame] | 914 | default_method->parameters.push_back( |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 915 | std::make_shared<Variable>(JavaSignatureOf(arg->GetType()), arg->GetName())); |
| Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 916 | } |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 917 | default_method->exceptions.push_back("android.os.RemoteException"); |
| Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 918 | |
| 919 | if (method.GetType().GetName() != "void") { |
| Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 920 | const string& defaultValue = DefaultJavaValueOf(method.GetType()); |
| Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 921 | default_method->statements->Add( |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 922 | std::make_shared<LiteralStatement>(StringPrintf("return %s;\n", defaultValue.c_str()))); |
| Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 923 | } |
| 924 | return default_method; |
| 925 | } |
| 926 | |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 927 | static shared_ptr<Class> generate_default_impl_class(const AidlInterface& iface, |
| Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 928 | const Options& options) { |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 929 | auto default_class = std::make_shared<Class>(); |
| Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 930 | default_class->comment = "/** Default implementation for " + iface.GetName() + ". */"; |
| 931 | default_class->modifiers = PUBLIC | STATIC; |
| 932 | default_class->what = Class::CLASS; |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 933 | default_class->type = iface.GetCanonicalName() + ".Default"; |
| 934 | default_class->interfaces.emplace_back(iface.GetCanonicalName()); |
| Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 935 | |
| 936 | for (const auto& m : iface.GetMethods()) { |
| Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 937 | if (m->IsUserDefined()) { |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 938 | default_class->elements.emplace_back(generate_default_impl_method(*m.get())); |
| Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 939 | } else { |
| 940 | if (m->GetName() == kGetInterfaceVersion && options.Version() > 0) { |
| Jiyong Park | a2f9ca3 | 2018-08-01 21:04:11 +0900 | [diff] [blame] | 941 | // This is called only when the remote side is not implementing this |
| 942 | // method, which is impossible in normal case, because this method is |
| 943 | // automatically declared in the interface class and not implementing |
| 944 | // it in the remote side is causing compilation error. But if the remote |
| 945 | // side somehow managed to not implement it, that's an error and we |
| 946 | // report the case by returning -1 here. |
| Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 947 | std::ostringstream code; |
| 948 | code << "@Override\n" |
| 949 | << "public int " << kGetInterfaceVersion << "() {\n" |
| Jiyong Park | a2f9ca3 | 2018-08-01 21:04:11 +0900 | [diff] [blame] | 950 | << " return -1;\n" |
| Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 951 | << "}\n"; |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 952 | default_class->elements.emplace_back(std::make_shared<LiteralClassElement>(code.str())); |
| Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 953 | } |
| 954 | } |
| Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | default_class->elements.emplace_back( |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 958 | std::make_shared<LiteralClassElement>("@Override\n" |
| 959 | "public android.os.IBinder asBinder() {\n" |
| 960 | " return null;\n" |
| 961 | "}\n")); |
| Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 962 | |
| 963 | return default_class; |
| 964 | } |
| 965 | |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 966 | std::unique_ptr<Class> generate_binder_interface_class(const AidlInterface* iface, |
| 967 | const AidlTypenames& typenames, |
| 968 | const Options& options) { |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 969 | // the interface class |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 970 | auto interface = std::make_unique<Class>(); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 971 | interface->comment = iface->GetComments(); |
| 972 | interface->modifiers = PUBLIC; |
| 973 | interface->what = Class::INTERFACE; |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 974 | interface->type = iface->GetCanonicalName(); |
| 975 | interface->interfaces.push_back("android.os.IInterface"); |
| Jiyong Park | a6605ab | 2018-11-11 14:30:21 +0900 | [diff] [blame] | 976 | interface->annotations = generate_java_annotations(*iface); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 977 | |
| Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 978 | if (options.Version()) { |
| 979 | std::ostringstream code; |
| 980 | code << "/**\n" |
| 981 | << " * The version of this interface that the caller is built against.\n" |
| 982 | << " * This might be different from what {@link #getInterfaceVersion()\n" |
| 983 | << " * getInterfaceVersion} returns as that is the version of the interface\n" |
| 984 | << " * that the remote object is implementing.\n" |
| 985 | << " */\n" |
| 986 | << "public static final int VERSION = " << options.Version() << ";\n"; |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 987 | interface->elements.emplace_back(std::make_shared<LiteralClassElement>(code.str())); |
| Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 988 | } |
| 989 | |
| Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 990 | // the default impl class |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 991 | auto default_impl = generate_default_impl_class(*iface, options); |
| Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 992 | interface->elements.emplace_back(default_impl); |
| Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 993 | |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 994 | // the stub inner class |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 995 | auto stub = std::make_shared<StubClass>(iface, options); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 996 | interface->elements.push_back(stub); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 997 | |
| Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 998 | compute_outline_methods(iface, |
| 999 | stub, |
| 1000 | options.onTransact_outline_threshold_, |
| 1001 | options.onTransact_non_outline_count_); |
| Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 1002 | |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1003 | // the proxy inner class |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 1004 | auto proxy = std::make_shared<ProxyClass>(iface, options); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1005 | stub->elements.push_back(proxy); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1006 | |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1007 | // stub and proxy support for getInterfaceDescriptor() |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 1008 | generate_interface_descriptors(stub, proxy); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1009 | |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1010 | // all the declared constants of the interface |
| Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 1011 | for (const auto& constant : iface->GetConstantDeclarations()) { |
| 1012 | const AidlConstantValue& value = constant->GetValue(); |
| 1013 | |
| 1014 | switch (value.GetType()) { |
| 1015 | case AidlConstantValue::Type::STRING: { |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 1016 | generate_string_constant(interface.get(), constant->GetName(), |
| Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 1017 | constant->ValueString(ConstantValueDecorator)); |
| Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 1018 | break; |
| 1019 | } |
| Steven Moreland | 2529432 | 2018-08-07 18:13:55 -0700 | [diff] [blame] | 1020 | case AidlConstantValue::Type::INTEGRAL: |
| 1021 | case AidlConstantValue::Type::HEXIDECIMAL: { |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 1022 | generate_int_constant(interface.get(), constant->GetName(), |
| Steven Moreland | 860b194 | 2018-08-16 14:59:28 -0700 | [diff] [blame] | 1023 | constant->ValueString(ConstantValueDecorator)); |
| Steven Moreland | 693640b | 2018-07-19 13:46:27 -0700 | [diff] [blame] | 1024 | break; |
| 1025 | } |
| 1026 | default: { |
| 1027 | LOG(FATAL) << "Unrecognized constant type: " << static_cast<int>(value.GetType()); |
| 1028 | } |
| 1029 | } |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1030 | } |
| Casey Dahlin | d40e2fe | 2015-11-24 14:06:52 -0800 | [diff] [blame] | 1031 | |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1032 | // all the declared methods of the interface |
| Andreas Gampe | 1b865af | 2017-11-22 11:31:47 -0800 | [diff] [blame] | 1033 | |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1034 | for (const auto& item : iface->GetMethods()) { |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 1035 | generate_methods(*iface, *item, interface.get(), stub, proxy, item->GetId(), typenames, |
| 1036 | options); |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1037 | } |
| Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 1038 | |
| 1039 | // additional static methods for the default impl set/get to the |
| 1040 | // stub class. Can't add them to the interface as the generated java files |
| 1041 | // may be compiled with Java < 1.7 where static interface method isn't |
| 1042 | // supported. |
| 1043 | // TODO(b/111417145) make this conditional depending on the Java language |
| 1044 | // version requested |
| Jeongik Cha | a2080bf | 2019-06-18 16:44:29 +0900 | [diff] [blame] | 1045 | const string i_name = iface->GetCanonicalName(); |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 1046 | stub->elements.emplace_back(std::make_shared<LiteralClassElement>( |
| Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 1047 | StringPrintf("public static boolean setDefaultImpl(%s impl) {\n" |
| 1048 | " if (Stub.Proxy.sDefaultImpl == null && impl != null) {\n" |
| 1049 | " Stub.Proxy.sDefaultImpl = impl;\n" |
| 1050 | " return true;\n" |
| 1051 | " }\n" |
| 1052 | " return false;\n" |
| 1053 | "}\n", |
| 1054 | i_name.c_str()))); |
| 1055 | stub->elements.emplace_back( |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 1056 | std::make_shared<LiteralClassElement>(StringPrintf("public static %s getDefaultImpl() {\n" |
| 1057 | " return Stub.Proxy.sDefaultImpl;\n" |
| 1058 | "}\n", |
| 1059 | i_name.c_str()))); |
| Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 1060 | |
| 1061 | // the static field is defined in the proxy class, not in the interface class |
| 1062 | // because all fields in an interface class are by default final. |
| Steven Moreland | 48548e0 | 2019-09-18 15:10:22 -0700 | [diff] [blame] | 1063 | proxy->elements.emplace_back(std::make_shared<LiteralClassElement>( |
| Jiyong Park | 47fb0d6 | 2018-11-17 10:12:15 +0900 | [diff] [blame] | 1064 | StringPrintf("public static %s sDefaultImpl;\n", i_name.c_str()))); |
| Jiyong Park | 75e1a74 | 2018-07-04 12:31:23 +0900 | [diff] [blame] | 1065 | |
| Andreas Gampe | a8a66fe | 2017-11-22 12:17:00 -0800 | [diff] [blame] | 1066 | stub->finish(); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1067 | |
| Christopher Wiley | 67502f1 | 2016-01-29 10:57:00 -0800 | [diff] [blame] | 1068 | return interface; |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1069 | } |
| 1070 | |
| Christopher Wiley | db154a5 | 2015-09-28 16:32:25 -0700 | [diff] [blame] | 1071 | } // namespace java |
| Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 1072 | } // namespace aidl |
| Steven Moreland | f4c64df | 2019-07-29 19:54:04 -0700 | [diff] [blame] | 1073 | } // namespace android |