| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 1 | #include "generate_java.h" |
| Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 2 | |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 3 | #include <string.h> |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | |
| Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 8 | #include <base/macros.h> |
| 9 | |
| Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 10 | #include "parse_helpers.h" |
| Christopher Wiley | 775fa1f | 2015-09-22 15:00:12 -0700 | [diff] [blame] | 11 | #include "type_java.h" |
| Christopher Wiley | f690be5 | 2015-09-14 15:19:10 -0700 | [diff] [blame] | 12 | |
| Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 13 | namespace android { |
| 14 | namespace aidl { |
| Christopher Wiley | db154a5 | 2015-09-28 16:32:25 -0700 | [diff] [blame] | 15 | namespace java { |
| Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 16 | |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 17 | // ================================================= |
| 18 | class StubClass : public Class |
| 19 | { |
| 20 | public: |
| Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 21 | StubClass(const Type* type, const Type* interfaceType, |
| 22 | JavaTypeNamespace* types); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 23 | virtual ~StubClass(); |
| 24 | |
| 25 | Variable* transact_code; |
| 26 | Variable* transact_data; |
| 27 | Variable* transact_reply; |
| 28 | Variable* transact_flags; |
| 29 | SwitchStatement* transact_switch; |
| 30 | private: |
| Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 31 | void make_as_interface(const Type* interfaceType, JavaTypeNamespace* types); |
| 32 | |
| 33 | DISALLOW_COPY_AND_ASSIGN(StubClass); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 34 | }; |
| 35 | |
| Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 36 | StubClass::StubClass(const Type* type, const Type* interfaceType, |
| 37 | JavaTypeNamespace* types) |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 38 | :Class() |
| 39 | { |
| 40 | this->comment = "/** Local-side IPC implementation stub class. */"; |
| 41 | this->modifiers = PUBLIC | ABSTRACT | STATIC; |
| 42 | this->what = Class::CLASS; |
| 43 | this->type = type; |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 44 | this->extends = types->BinderNativeType(); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 45 | this->interfaces.push_back(interfaceType); |
| 46 | |
| 47 | // descriptor |
| 48 | Field* descriptor = new Field(STATIC | FINAL | PRIVATE, |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 49 | new Variable(types->StringType(), "DESCRIPTOR")); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 50 | descriptor->value = "\"" + interfaceType->QualifiedName() + "\""; |
| 51 | this->elements.push_back(descriptor); |
| 52 | |
| 53 | // ctor |
| 54 | Method* ctor = new Method; |
| 55 | ctor->modifiers = PUBLIC; |
| 56 | ctor->comment = "/** Construct the stub at attach it to the " |
| 57 | "interface. */"; |
| 58 | ctor->name = "Stub"; |
| 59 | ctor->statements = new StatementBlock; |
| 60 | MethodCall* attach = new MethodCall(THIS_VALUE, "attachInterface", |
| 61 | 2, THIS_VALUE, new LiteralExpression("DESCRIPTOR")); |
| 62 | ctor->statements->Add(attach); |
| 63 | this->elements.push_back(ctor); |
| 64 | |
| 65 | // asInterface |
| Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 66 | make_as_interface(interfaceType, types); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 67 | |
| 68 | // asBinder |
| 69 | Method* asBinder = new Method; |
| 70 | asBinder->modifiers = PUBLIC | OVERRIDE; |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 71 | asBinder->returnType = types->IBinderType(); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 72 | asBinder->name = "asBinder"; |
| 73 | asBinder->statements = new StatementBlock; |
| 74 | asBinder->statements->Add(new ReturnStatement(THIS_VALUE)); |
| 75 | this->elements.push_back(asBinder); |
| 76 | |
| 77 | // onTransact |
| Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 78 | this->transact_code = new Variable(types->IntType(), "code"); |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 79 | this->transact_data = new Variable(types->ParcelType(), "data"); |
| 80 | this->transact_reply = new Variable(types->ParcelType(), "reply"); |
| Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 81 | this->transact_flags = new Variable(types->IntType(), "flags"); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 82 | Method* onTransact = new Method; |
| 83 | onTransact->modifiers = PUBLIC | OVERRIDE; |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 84 | onTransact->returnType = types->BoolType(); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 85 | onTransact->name = "onTransact"; |
| 86 | onTransact->parameters.push_back(this->transact_code); |
| 87 | onTransact->parameters.push_back(this->transact_data); |
| 88 | onTransact->parameters.push_back(this->transact_reply); |
| 89 | onTransact->parameters.push_back(this->transact_flags); |
| 90 | onTransact->statements = new StatementBlock; |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 91 | onTransact->exceptions.push_back(types->RemoteExceptionType()); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 92 | this->elements.push_back(onTransact); |
| 93 | this->transact_switch = new SwitchStatement(this->transact_code); |
| 94 | |
| 95 | onTransact->statements->Add(this->transact_switch); |
| 96 | MethodCall* superCall = new MethodCall(SUPER_VALUE, "onTransact", 4, |
| 97 | this->transact_code, this->transact_data, |
| 98 | this->transact_reply, this->transact_flags); |
| 99 | onTransact->statements->Add(new ReturnStatement(superCall)); |
| 100 | } |
| 101 | |
| 102 | StubClass::~StubClass() |
| 103 | { |
| 104 | } |
| 105 | |
| 106 | void |
| Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 107 | StubClass::make_as_interface(const Type *interfaceType, |
| 108 | JavaTypeNamespace* types) |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 109 | { |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 110 | Variable* obj = new Variable(types->IBinderType(), "obj"); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 111 | |
| 112 | Method* m = new Method; |
| 113 | m->comment = "/**\n * Cast an IBinder object into an "; |
| 114 | m->comment += interfaceType->QualifiedName(); |
| 115 | m->comment += " interface,\n"; |
| 116 | m->comment += " * generating a proxy if needed.\n */"; |
| 117 | m->modifiers = PUBLIC | STATIC; |
| 118 | m->returnType = interfaceType; |
| 119 | m->name = "asInterface"; |
| 120 | m->parameters.push_back(obj); |
| 121 | m->statements = new StatementBlock; |
| 122 | |
| 123 | IfStatement* ifstatement = new IfStatement(); |
| 124 | ifstatement->expression = new Comparison(obj, "==", NULL_VALUE); |
| 125 | ifstatement->statements = new StatementBlock; |
| 126 | ifstatement->statements->Add(new ReturnStatement(NULL_VALUE)); |
| 127 | m->statements->Add(ifstatement); |
| 128 | |
| 129 | // IInterface iin = obj.queryLocalInterface(DESCRIPTOR) |
| 130 | MethodCall* queryLocalInterface = new MethodCall(obj, "queryLocalInterface"); |
| 131 | queryLocalInterface->arguments.push_back(new LiteralExpression("DESCRIPTOR")); |
| Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 132 | IInterfaceType* iinType = new IInterfaceType(types); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 133 | Variable *iin = new Variable(iinType, "iin"); |
| 134 | VariableDeclaration* iinVd = new VariableDeclaration(iin, queryLocalInterface, NULL); |
| 135 | m->statements->Add(iinVd); |
| 136 | |
| 137 | // Ensure the instance type of the local object is as expected. |
| 138 | // One scenario where this is needed is if another package (with a |
| 139 | // different class loader) runs in the same process as the service. |
| 140 | |
| 141 | // if (iin != null && iin instanceof <interfaceType>) return (<interfaceType>) iin; |
| 142 | Comparison* iinNotNull = new Comparison(iin, "!=", NULL_VALUE); |
| 143 | Comparison* instOfCheck = new Comparison(iin, " instanceof ", |
| 144 | new LiteralExpression(interfaceType->QualifiedName())); |
| 145 | IfStatement* instOfStatement = new IfStatement(); |
| 146 | instOfStatement->expression = new Comparison(iinNotNull, "&&", instOfCheck); |
| 147 | instOfStatement->statements = new StatementBlock; |
| 148 | instOfStatement->statements->Add(new ReturnStatement(new Cast(interfaceType, iin))); |
| 149 | m->statements->Add(instOfStatement); |
| 150 | |
| 151 | string proxyType = interfaceType->QualifiedName(); |
| 152 | proxyType += ".Stub.Proxy"; |
| Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 153 | NewExpression* ne = new NewExpression(types->Find(proxyType)); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 154 | ne->arguments.push_back(obj); |
| 155 | m->statements->Add(new ReturnStatement(ne)); |
| 156 | |
| 157 | this->elements.push_back(m); |
| 158 | } |
| 159 | |
| 160 | |
| 161 | |
| 162 | // ================================================= |
| 163 | class ProxyClass : public Class |
| 164 | { |
| 165 | public: |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 166 | ProxyClass(const JavaTypeNamespace* types, const Type* type, |
| 167 | const InterfaceType* interfaceType); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 168 | virtual ~ProxyClass(); |
| 169 | |
| 170 | Variable* mRemote; |
| 171 | bool mOneWay; |
| 172 | }; |
| 173 | |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 174 | ProxyClass::ProxyClass(const JavaTypeNamespace* types, |
| 175 | const Type* type, const InterfaceType* interfaceType) |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 176 | :Class() |
| 177 | { |
| 178 | this->modifiers = PRIVATE | STATIC; |
| 179 | this->what = Class::CLASS; |
| 180 | this->type = type; |
| 181 | this->interfaces.push_back(interfaceType); |
| 182 | |
| 183 | mOneWay = interfaceType->OneWay(); |
| 184 | |
| 185 | // IBinder mRemote |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 186 | mRemote = new Variable(types->IBinderType(), "mRemote"); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 187 | this->elements.push_back(new Field(PRIVATE, mRemote)); |
| 188 | |
| 189 | // Proxy() |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 190 | Variable* remote = new Variable(types->IBinderType(), "remote"); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 191 | Method* ctor = new Method; |
| 192 | ctor->name = "Proxy"; |
| 193 | ctor->statements = new StatementBlock; |
| 194 | ctor->parameters.push_back(remote); |
| 195 | ctor->statements->Add(new Assignment(mRemote, remote)); |
| 196 | this->elements.push_back(ctor); |
| 197 | |
| 198 | // IBinder asBinder() |
| 199 | Method* asBinder = new Method; |
| 200 | asBinder->modifiers = PUBLIC | OVERRIDE; |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 201 | asBinder->returnType = types->IBinderType(); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 202 | asBinder->name = "asBinder"; |
| 203 | asBinder->statements = new StatementBlock; |
| 204 | asBinder->statements->Add(new ReturnStatement(mRemote)); |
| 205 | this->elements.push_back(asBinder); |
| 206 | } |
| 207 | |
| 208 | ProxyClass::~ProxyClass() |
| 209 | { |
| 210 | } |
| 211 | |
| 212 | // ================================================= |
| 213 | static void |
| Christopher Wiley | 8f6816e | 2015-09-22 17:03:47 -0700 | [diff] [blame] | 214 | generate_new_array(const Type* t, StatementBlock* addTo, Variable* v, |
| Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 215 | Variable* parcel, JavaTypeNamespace* types) |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 216 | { |
| Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 217 | Variable* len = new Variable(types->IntType(), v->name + "_length"); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 218 | addTo->Add(new VariableDeclaration(len, new MethodCall(parcel, "readInt"))); |
| 219 | IfStatement* lencheck = new IfStatement(); |
| 220 | lencheck->expression = new Comparison(len, "<", new LiteralExpression("0")); |
| 221 | lencheck->statements->Add(new Assignment(v, NULL_VALUE)); |
| 222 | lencheck->elseif = new IfStatement(); |
| 223 | lencheck->elseif->statements->Add(new Assignment(v, |
| 224 | new NewArrayExpression(t, len))); |
| 225 | addTo->Add(lencheck); |
| 226 | } |
| 227 | |
| 228 | static void |
| Christopher Wiley | 8f6816e | 2015-09-22 17:03:47 -0700 | [diff] [blame] | 229 | generate_write_to_parcel(const Type* t, StatementBlock* addTo, Variable* v, |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 230 | Variable* parcel, int flags) |
| 231 | { |
| 232 | if (v->dimension == 0) { |
| 233 | t->WriteToParcel(addTo, v, parcel, flags); |
| 234 | } |
| 235 | if (v->dimension == 1) { |
| 236 | t->WriteArrayToParcel(addTo, v, parcel, flags); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | static void |
| Christopher Wiley | 8f6816e | 2015-09-22 17:03:47 -0700 | [diff] [blame] | 241 | generate_create_from_parcel(const Type* t, StatementBlock* addTo, Variable* v, |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 242 | Variable* parcel, Variable** cl) |
| 243 | { |
| 244 | if (v->dimension == 0) { |
| 245 | t->CreateFromParcel(addTo, v, parcel, cl); |
| 246 | } |
| 247 | if (v->dimension == 1) { |
| 248 | t->CreateArrayFromParcel(addTo, v, parcel, cl); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | static void |
| Christopher Wiley | 8f6816e | 2015-09-22 17:03:47 -0700 | [diff] [blame] | 253 | generate_read_from_parcel(const Type* t, StatementBlock* addTo, Variable* v, |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 254 | Variable* parcel, Variable** cl) |
| 255 | { |
| 256 | if (v->dimension == 0) { |
| 257 | t->ReadFromParcel(addTo, v, parcel, cl); |
| 258 | } |
| 259 | if (v->dimension == 1) { |
| 260 | t->ReadArrayFromParcel(addTo, v, parcel, cl); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | |
| 265 | static void |
| 266 | generate_method(const method_type* method, Class* interface, |
| Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 267 | StubClass* stubClass, ProxyClass* proxyClass, int index, |
| 268 | JavaTypeNamespace* types) |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 269 | { |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 270 | int i; |
| 271 | bool hasOutParams = false; |
| 272 | |
| 273 | const bool oneway = proxyClass->mOneWay || method->oneway; |
| 274 | |
| 275 | // == the TRANSACT_ constant ============================================= |
| 276 | string transactCodeName = "TRANSACTION_"; |
| 277 | transactCodeName += method->name.data; |
| 278 | |
| 279 | char transactCodeValue[60]; |
| 280 | sprintf(transactCodeValue, "(android.os.IBinder.FIRST_CALL_TRANSACTION + %d)", index); |
| 281 | |
| 282 | Field* transactCode = new Field(STATIC | FINAL, |
| Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 283 | new Variable(types->IntType(), transactCodeName)); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 284 | transactCode->value = transactCodeValue; |
| 285 | stubClass->elements.push_back(transactCode); |
| 286 | |
| 287 | // == the declaration in the interface =================================== |
| 288 | Method* decl = new Method; |
| 289 | decl->comment = gather_comments(method->comments_token->extra); |
| 290 | decl->modifiers = PUBLIC; |
| Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 291 | decl->returnType = types->Find(method->type.type.data); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 292 | decl->returnTypeDimension = method->type.dimension; |
| 293 | decl->name = method->name.data; |
| 294 | |
| Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 295 | for (const std::unique_ptr<AidlArgument>& arg : *method->args) { |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 296 | decl->parameters.push_back(new Variable( |
| Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 297 | types->Find(arg->type.type.data), arg->name.data, |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 298 | arg->type.dimension)); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 299 | } |
| 300 | |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 301 | decl->exceptions.push_back(types->RemoteExceptionType()); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 302 | |
| 303 | interface->elements.push_back(decl); |
| 304 | |
| 305 | // == the stub method ==================================================== |
| 306 | |
| 307 | Case* c = new Case(transactCodeName); |
| 308 | |
| 309 | MethodCall* realCall = new MethodCall(THIS_VALUE, method->name.data); |
| 310 | |
| 311 | // interface token validation is the very first thing we do |
| 312 | c->statements->Add(new MethodCall(stubClass->transact_data, |
| 313 | "enforceInterface", 1, new LiteralExpression("DESCRIPTOR"))); |
| 314 | |
| 315 | // args |
| 316 | Variable* cl = NULL; |
| 317 | VariableFactory stubArgs("_arg"); |
| Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 318 | for (const std::unique_ptr<AidlArgument>& arg : *method->args) { |
| Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 319 | const Type* t = types->Find(arg->type.type.data); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 320 | Variable* v = stubArgs.Get(t); |
| 321 | v->dimension = arg->type.dimension; |
| 322 | |
| 323 | c->statements->Add(new VariableDeclaration(v)); |
| 324 | |
| 325 | if (convert_direction(arg->direction.data) & IN_PARAMETER) { |
| 326 | generate_create_from_parcel(t, c->statements, v, |
| 327 | stubClass->transact_data, &cl); |
| 328 | } else { |
| 329 | if (arg->type.dimension == 0) { |
| 330 | c->statements->Add(new Assignment(v, new NewExpression(v->type))); |
| 331 | } |
| 332 | else if (arg->type.dimension == 1) { |
| 333 | generate_new_array(v->type, c->statements, v, |
| Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 334 | stubClass->transact_data, types); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 335 | } |
| 336 | else { |
| 337 | fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__, |
| 338 | __LINE__); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | realCall->arguments.push_back(v); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | // the real call |
| 346 | Variable* _result = NULL; |
| 347 | if (0 == strcmp(method->type.type.data, "void")) { |
| 348 | c->statements->Add(realCall); |
| 349 | |
| 350 | if (!oneway) { |
| 351 | // report that there were no exceptions |
| 352 | MethodCall* ex = new MethodCall(stubClass->transact_reply, |
| 353 | "writeNoException", 0); |
| 354 | c->statements->Add(ex); |
| 355 | } |
| 356 | } else { |
| 357 | _result = new Variable(decl->returnType, "_result", |
| 358 | decl->returnTypeDimension); |
| 359 | c->statements->Add(new VariableDeclaration(_result, realCall)); |
| 360 | |
| 361 | if (!oneway) { |
| 362 | // report that there were no exceptions |
| 363 | MethodCall* ex = new MethodCall(stubClass->transact_reply, |
| 364 | "writeNoException", 0); |
| 365 | c->statements->Add(ex); |
| 366 | } |
| 367 | |
| 368 | // marshall the return value |
| 369 | generate_write_to_parcel(decl->returnType, c->statements, _result, |
| 370 | stubClass->transact_reply, |
| 371 | Type::PARCELABLE_WRITE_RETURN_VALUE); |
| 372 | } |
| 373 | |
| 374 | // out parameters |
| 375 | i = 0; |
| Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 376 | for (const std::unique_ptr<AidlArgument>& arg : *method->args) { |
| Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 377 | const Type* t = types->Find(arg->type.type.data); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 378 | Variable* v = stubArgs.Get(i++); |
| 379 | |
| 380 | if (convert_direction(arg->direction.data) & OUT_PARAMETER) { |
| 381 | generate_write_to_parcel(t, c->statements, v, |
| 382 | stubClass->transact_reply, |
| 383 | Type::PARCELABLE_WRITE_RETURN_VALUE); |
| 384 | hasOutParams = true; |
| 385 | } |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | // return true |
| 389 | c->statements->Add(new ReturnStatement(TRUE_VALUE)); |
| 390 | stubClass->transact_switch->cases.push_back(c); |
| 391 | |
| 392 | // == the proxy method =================================================== |
| 393 | Method* proxy = new Method; |
| 394 | proxy->comment = gather_comments(method->comments_token->extra); |
| 395 | proxy->modifiers = PUBLIC | OVERRIDE; |
| Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 396 | proxy->returnType = types->Find(method->type.type.data); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 397 | proxy->returnTypeDimension = method->type.dimension; |
| 398 | proxy->name = method->name.data; |
| 399 | proxy->statements = new StatementBlock; |
| Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 400 | for (const std::unique_ptr<AidlArgument>& arg : *method->args) { |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 401 | proxy->parameters.push_back(new Variable( |
| Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 402 | types->Find(arg->type.type.data), arg->name.data, |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 403 | arg->type.dimension)); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 404 | } |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 405 | proxy->exceptions.push_back(types->RemoteExceptionType()); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 406 | proxyClass->elements.push_back(proxy); |
| 407 | |
| 408 | // the parcels |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 409 | Variable* _data = new Variable(types->ParcelType(), "_data"); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 410 | proxy->statements->Add(new VariableDeclaration(_data, |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 411 | new MethodCall(types->ParcelType(), "obtain"))); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 412 | Variable* _reply = NULL; |
| 413 | if (!oneway) { |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 414 | _reply = new Variable(types->ParcelType(), "_reply"); |
| 415 | proxy->statements->Add( |
| 416 | new VariableDeclaration( |
| 417 | _reply, |
| 418 | new MethodCall(types->ParcelType(), "obtain"))); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | // the return value |
| 422 | _result = NULL; |
| 423 | if (0 != strcmp(method->type.type.data, "void")) { |
| 424 | _result = new Variable(proxy->returnType, "_result", |
| 425 | method->type.dimension); |
| 426 | proxy->statements->Add(new VariableDeclaration(_result)); |
| 427 | } |
| 428 | |
| 429 | // try and finally |
| 430 | TryStatement* tryStatement = new TryStatement(); |
| 431 | proxy->statements->Add(tryStatement); |
| 432 | FinallyStatement* finallyStatement = new FinallyStatement(); |
| 433 | proxy->statements->Add(finallyStatement); |
| 434 | |
| 435 | // the interface identifier token: the DESCRIPTOR constant, marshalled as a string |
| 436 | tryStatement->statements->Add(new MethodCall(_data, "writeInterfaceToken", |
| 437 | 1, new LiteralExpression("DESCRIPTOR"))); |
| 438 | |
| 439 | // the parameters |
| Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 440 | for (const std::unique_ptr<AidlArgument>& arg : *method->args) { |
| Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 441 | const Type* t = types->Find(arg->type.type.data); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 442 | Variable* v = new Variable(t, arg->name.data, arg->type.dimension); |
| 443 | int dir = convert_direction(arg->direction.data); |
| 444 | if (dir == OUT_PARAMETER && arg->type.dimension != 0) { |
| 445 | IfStatement* checklen = new IfStatement(); |
| 446 | checklen->expression = new Comparison(v, "==", NULL_VALUE); |
| 447 | checklen->statements->Add(new MethodCall(_data, "writeInt", 1, |
| 448 | new LiteralExpression("-1"))); |
| 449 | checklen->elseif = new IfStatement(); |
| 450 | checklen->elseif->statements->Add(new MethodCall(_data, "writeInt", |
| 451 | 1, new FieldVariable(v, "length"))); |
| 452 | tryStatement->statements->Add(checklen); |
| 453 | } |
| 454 | else if (dir & IN_PARAMETER) { |
| 455 | generate_write_to_parcel(t, tryStatement->statements, v, _data, 0); |
| 456 | } |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | // the transact call |
| 460 | MethodCall* call = new MethodCall(proxyClass->mRemote, "transact", 4, |
| 461 | new LiteralExpression("Stub." + transactCodeName), |
| 462 | _data, _reply ? _reply : NULL_VALUE, |
| 463 | new LiteralExpression( |
| 464 | oneway ? "android.os.IBinder.FLAG_ONEWAY" : "0")); |
| 465 | tryStatement->statements->Add(call); |
| 466 | |
| 467 | // throw back exceptions. |
| 468 | if (_reply) { |
| 469 | MethodCall* ex = new MethodCall(_reply, "readException", 0); |
| 470 | tryStatement->statements->Add(ex); |
| 471 | } |
| 472 | |
| 473 | // returning and cleanup |
| 474 | if (_reply != NULL) { |
| 475 | if (_result != NULL) { |
| 476 | generate_create_from_parcel(proxy->returnType, |
| 477 | tryStatement->statements, _result, _reply, &cl); |
| 478 | } |
| 479 | |
| 480 | // the out/inout parameters |
| Casey Dahlin | bc7a50a | 2015-09-28 19:20:50 -0700 | [diff] [blame] | 481 | for (const std::unique_ptr<AidlArgument>& arg : *method->args) { |
| Christopher Wiley | fb4b22d | 2015-09-25 15:16:13 -0700 | [diff] [blame] | 482 | const Type* t = types->Find(arg->type.type.data); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 483 | Variable* v = new Variable(t, arg->name.data, arg->type.dimension); |
| 484 | if (convert_direction(arg->direction.data) & OUT_PARAMETER) { |
| 485 | generate_read_from_parcel(t, tryStatement->statements, |
| 486 | v, _reply, &cl); |
| 487 | } |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | finallyStatement->statements->Add(new MethodCall(_reply, "recycle")); |
| 491 | } |
| 492 | finallyStatement->statements->Add(new MethodCall(_data, "recycle")); |
| 493 | |
| 494 | if (_result != NULL) { |
| 495 | proxy->statements->Add(new ReturnStatement(_result)); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | static void |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 500 | generate_interface_descriptors(StubClass* stub, ProxyClass* proxy, |
| 501 | const JavaTypeNamespace* types) |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 502 | { |
| 503 | // the interface descriptor transaction handler |
| 504 | Case* c = new Case("INTERFACE_TRANSACTION"); |
| 505 | c->statements->Add(new MethodCall(stub->transact_reply, "writeString", |
| 506 | 1, new LiteralExpression("DESCRIPTOR"))); |
| 507 | c->statements->Add(new ReturnStatement(TRUE_VALUE)); |
| 508 | stub->transact_switch->cases.push_back(c); |
| 509 | |
| 510 | // and the proxy-side method returning the descriptor directly |
| 511 | Method* getDesc = new Method; |
| 512 | getDesc->modifiers = PUBLIC; |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 513 | getDesc->returnType = types->StringType(); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 514 | getDesc->returnTypeDimension = 0; |
| 515 | getDesc->name = "getInterfaceDescriptor"; |
| 516 | getDesc->statements = new StatementBlock; |
| 517 | getDesc->statements->Add(new ReturnStatement(new LiteralExpression("DESCRIPTOR"))); |
| 518 | proxy->elements.push_back(getDesc); |
| 519 | } |
| 520 | |
| 521 | Class* |
| Christopher Wiley | 84c1eac | 2015-09-23 13:29:28 -0700 | [diff] [blame] | 522 | generate_binder_interface_class(const interface_type* iface, |
| 523 | JavaTypeNamespace* types) |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 524 | { |
| Christopher Wiley | 8f6816e | 2015-09-22 17:03:47 -0700 | [diff] [blame] | 525 | const InterfaceType* interfaceType = static_cast<const InterfaceType*>( |
| Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 526 | types->Find(iface->package, iface->name.data)); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 527 | |
| 528 | // the interface class |
| 529 | Class* interface = new Class; |
| 530 | interface->comment = gather_comments(iface->comments_token->extra); |
| 531 | interface->modifiers = PUBLIC; |
| 532 | interface->what = Class::INTERFACE; |
| 533 | interface->type = interfaceType; |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 534 | interface->interfaces.push_back(types->IInterfaceType()); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 535 | |
| 536 | // the stub inner class |
| 537 | StubClass* stub = new StubClass( |
| Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 538 | types->Find(iface->package, append(iface->name.data, ".Stub").c_str()), |
| 539 | interfaceType, types); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 540 | interface->elements.push_back(stub); |
| 541 | |
| 542 | // the proxy inner class |
| 543 | ProxyClass* proxy = new ProxyClass( |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 544 | types, |
| Christopher Wiley | 8b2d3ee | 2015-09-23 15:43:01 -0700 | [diff] [blame] | 545 | types->Find(iface->package, |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 546 | append(iface->name.data, ".Stub.Proxy").c_str()), |
| 547 | interfaceType); |
| 548 | stub->elements.push_back(proxy); |
| 549 | |
| 550 | // stub and proxy support for getInterfaceDescriptor() |
| Christopher Wiley | 60b02ae | 2015-09-23 16:35:18 -0700 | [diff] [blame] | 551 | generate_interface_descriptors(stub, proxy, types); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 552 | |
| 553 | // all the declared methods of the interface |
| 554 | int index = 0; |
| Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 555 | method_type* item = iface->interface_items; |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 556 | while (item != NULL) { |
| Casey Dahlin | dff80e5 | 2015-09-29 13:57:06 -0700 | [diff] [blame] | 557 | generate_method(item, interface, stub, proxy, |
| 558 | item->assigned_id, types); |
| Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 559 | item = item->next; |
| 560 | index++; |
| 561 | } |
| 562 | |
| 563 | return interface; |
| 564 | } |
| 565 | |
| Christopher Wiley | db154a5 | 2015-09-28 16:32:25 -0700 | [diff] [blame] | 566 | } // namespace java |
| Christopher Wiley | fdeb0f4 | 2015-09-11 15:38:22 -0700 | [diff] [blame] | 567 | } // namespace android |
| 568 | } // namespace aidl |