blob: 60517e8fb5cf159ec43fea9834ad97bd69a15933 [file] [log] [blame]
Joe Onoratofdfe2ff2011-08-30 17:24:17 -07001#include "generate_java.h"
2#include "Type.h"
3#include <string.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7
Manuel Romanb71287f2011-10-06 10:28:35 -07008Type* SERVICE_CONTEXT_TYPE = new Type("android.content",
9 "Context", Type::BUILT_IN, false, false, false);
Joe Onorato28087c62011-10-12 23:22:42 -070010Type* PRESENTER_BASE_TYPE = new Type("com.android.athome.connector",
11 "EventListener", Type::BUILT_IN, false, false, false);
12Type* PRESENTER_LISTENER_BASE_TYPE = new Type("com.android.athome.connector",
13 "EventListener.Listener", Type::BUILT_IN, false, false, false);
Joe Onorato95a766d2011-10-09 21:51:46 -070014Type* RPC_BROKER_TYPE = new Type("com.android.athome.connector", "Broker",
Joe Onoratoe24dbea2011-09-23 15:17:52 -070015 Type::BUILT_IN, false, false, false);
Joe Onorato95a766d2011-10-09 21:51:46 -070016Type* RPC_CONTAINER_TYPE = new Type("com.android.athome.connector", "ConnectorContainer",
Joe Onoratoe24dbea2011-09-23 15:17:52 -070017 Type::BUILT_IN, false, false, false);
Joe Onorato95a766d2011-10-09 21:51:46 -070018// TODO: Just use Endpoint, so this works for all endpoints.
19Type* RPC_CONNECTOR_TYPE = new Type("com.android.athome.connector", "Connector",
20 Type::BUILT_IN, false, false, false);
Joe Onoratoa1c6d902011-10-09 22:31:16 -070021Type* RPC_ENDPOINT_INFO_TYPE = new UserDataType("com.android.athome.rpc",
Joe Onorato95a766d2011-10-09 21:51:46 -070022 "EndpointInfo", true, __FILE__, __LINE__);
Joe Onoratoa1c6d902011-10-09 22:31:16 -070023Type* RPC_RESULT_HANDLER_TYPE = new UserDataType("com.android.athome.rpc", "RpcResultHandler",
Joe Onoratoe24dbea2011-09-23 15:17:52 -070024 true, __FILE__, __LINE__);
25Type* RPC_ERROR_LISTENER_TYPE = new Type("com.android.athome.rpc", "RpcErrorHandler",
26 Type::BUILT_IN, false, false, false);
Joe Onoratoa1c6d902011-10-09 22:31:16 -070027Type* RPC_CONTEXT_TYPE = new UserDataType("com.android.athome.rpc", "RpcContext", true,
Manuel Romanb71287f2011-10-06 10:28:35 -070028 __FILE__, __LINE__);
Joe Onoratoe24dbea2011-09-23 15:17:52 -070029
30static void generate_create_from_data(Type* t, StatementBlock* addTo, const string& key,
31 Variable* v, Variable* data, Variable** cl);
32static void generate_new_array(Type* t, StatementBlock* addTo, Variable* v, Variable* from);
33static void generate_write_to_data(Type* t, StatementBlock* addTo, Expression* k, Variable* v,
34 Variable* data);
Joe Onoratofdfe2ff2011-08-30 17:24:17 -070035
36static string
37format_int(int n)
38{
39 char str[20];
40 sprintf(str, "%d", n);
41 return string(str);
42}
43
44static string
45class_name_leaf(const string& str)
46{
47 string::size_type pos = str.rfind('.');
48 if (pos == string::npos) {
49 return str;
50 } else {
51 return string(str, pos+1);
52 }
53}
54
Joe Onoratoe24dbea2011-09-23 15:17:52 -070055static string
56results_class_name(const string& n)
57{
58 string str = n;
59 str[0] = toupper(str[0]);
60 str.insert(0, "On");
61 return str;
62}
63
64static string
65results_method_name(const string& n)
66{
67 string str = n;
68 str[0] = toupper(str[0]);
69 str.insert(0, "on");
70 return str;
71}
72
73static string
74push_method_name(const string& n)
75{
76 string str = n;
77 str[0] = toupper(str[0]);
78 str.insert(0, "push");
79 return str;
80}
81
82// =================================================
83class DispatcherClass : public Class
84{
85public:
86 DispatcherClass(const interface_type* iface, Expression* target);
87 virtual ~DispatcherClass();
88
89 void AddMethod(const method_type* method);
90 void DoneWithMethods();
91
92 Method* processMethod;
93 Variable* actionParam;
94 Variable* requestParam;
Manuel Romanb71287f2011-10-06 10:28:35 -070095 Variable* rpcContextParam;
Joe Onoratoe24dbea2011-09-23 15:17:52 -070096 Variable* errorParam;
97 Variable* requestData;
98 Variable* resultData;
99 IfStatement* dispatchIfStatement;
100 Expression* targetExpression;
101
102private:
103 void generate_process();
104};
105
106DispatcherClass::DispatcherClass(const interface_type* iface, Expression* target)
107 :Class(),
108 dispatchIfStatement(NULL),
109 targetExpression(target)
110{
111 generate_process();
112}
113
114DispatcherClass::~DispatcherClass()
115{
116}
117
118void
119DispatcherClass::generate_process()
120{
Manuel Romanb71287f2011-10-06 10:28:35 -0700121 // byte[] process(String action, byte[] params, RpcContext context, RpcError status)
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700122 this->processMethod = new Method;
123 this->processMethod->modifiers = PUBLIC;
124 this->processMethod->returnType = BYTE_TYPE;
125 this->processMethod->returnTypeDimension = 1;
126 this->processMethod->name = "process";
127 this->processMethod->statements = new StatementBlock;
128
129 this->actionParam = new Variable(STRING_TYPE, "action");
130 this->processMethod->parameters.push_back(this->actionParam);
131
132 this->requestParam = new Variable(BYTE_TYPE, "requestParam", 1);
133 this->processMethod->parameters.push_back(this->requestParam);
134
Manuel Romanb71287f2011-10-06 10:28:35 -0700135 this->rpcContextParam = new Variable(RPC_CONTEXT_TYPE, "context", 0);
136 this->processMethod->parameters.push_back(this->rpcContextParam);
137
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700138 this->errorParam = new Variable(RPC_ERROR_TYPE, "errorParam", 0);
139 this->processMethod->parameters.push_back(this->errorParam);
140
141 this->requestData = new Variable(RPC_DATA_TYPE, "request");
142 this->processMethod->statements->Add(new VariableDeclaration(requestData,
143 new NewExpression(RPC_DATA_TYPE, 1, this->requestParam)));
144
145 this->resultData = new Variable(RPC_DATA_TYPE, "resultData");
146 this->processMethod->statements->Add(new VariableDeclaration(this->resultData,
147 NULL_VALUE));
148}
149
150void
151DispatcherClass::AddMethod(const method_type* method)
152{
153 arg_type* arg;
154
155 // The if/switch statement
156 IfStatement* ifs = new IfStatement();
157 ifs->expression = new MethodCall(new StringLiteralExpression(method->name.data), "equals",
158 1, this->actionParam);
159 StatementBlock* block = ifs->statements = new StatementBlock;
160 if (this->dispatchIfStatement == NULL) {
161 this->dispatchIfStatement = ifs;
162 this->processMethod->statements->Add(dispatchIfStatement);
163 } else {
164 this->dispatchIfStatement->elseif = ifs;
165 this->dispatchIfStatement = ifs;
166 }
167
168 // The call to decl (from above)
169 MethodCall* realCall = new MethodCall(this->targetExpression, method->name.data);
170
171 // args
172 Variable* classLoader = NULL;
173 VariableFactory stubArgs("_arg");
174 arg = method->args;
175 while (arg != NULL) {
176 Type* t = NAMES.Search(arg->type.type.data);
177 Variable* v = stubArgs.Get(t);
178 v->dimension = arg->type.dimension;
179
180 // Unmarshall the parameter
181 block->Add(new VariableDeclaration(v));
182 if (convert_direction(arg->direction.data) & IN_PARAMETER) {
183 generate_create_from_data(t, block, arg->name.data, v,
184 this->requestData, &classLoader);
185 } else {
186 if (arg->type.dimension == 0) {
187 block->Add(new Assignment(v, new NewExpression(v->type)));
188 }
189 else if (arg->type.dimension == 1) {
190 generate_new_array(v->type, block, v, this->requestData);
191 }
192 else {
193 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__,
194 __LINE__);
195 }
196 }
197
198 // Add that parameter to the method call
199 realCall->arguments.push_back(v);
200
201 arg = arg->next;
202 }
203
Manuel Romanb71287f2011-10-06 10:28:35 -0700204 // Add a final parameter: RpcContext. Contains data about
205 // incoming request (e.g., certificate)
206 realCall->arguments.push_back(new Variable(RPC_CONTEXT_TYPE, "context", 0));
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700207
208 Type* returnType = NAMES.Search(method->type.type.data);
209 if (returnType == EVENT_FAKE_TYPE) {
210 returnType = VOID_TYPE;
211 }
212
213 // the real call
214 bool first = true;
215 Variable* _result = NULL;
216 if (returnType == VOID_TYPE) {
217 block->Add(realCall);
218 } else {
219 _result = new Variable(returnType, "_result",
220 method->type.dimension);
221 block->Add(new VariableDeclaration(_result, realCall));
222
223 // need the result RpcData
224 if (first) {
225 block->Add(new Assignment(this->resultData,
226 new NewExpression(RPC_DATA_TYPE)));
227 first = false;
228 }
229
230 // marshall the return value
231 generate_write_to_data(returnType, block,
232 new StringLiteralExpression("_result"), _result, this->resultData);
233 }
234
235 // out parameters
236 int i = 0;
237 arg = method->args;
238 while (arg != NULL) {
239 Type* t = NAMES.Search(arg->type.type.data);
240 Variable* v = stubArgs.Get(i++);
241
242 if (convert_direction(arg->direction.data) & OUT_PARAMETER) {
243 // need the result RpcData
244 if (first) {
245 block->Add(new Assignment(this->resultData, new NewExpression(RPC_DATA_TYPE)));
246 first = false;
247 }
248
249 generate_write_to_data(t, block, new StringLiteralExpression(arg->name.data),
250 v, this->resultData);
251 }
252
253 arg = arg->next;
254 }
255}
256
257void
258DispatcherClass::DoneWithMethods()
259{
260 if (this->dispatchIfStatement == NULL) {
261 return;
262 }
263
264 this->elements.push_back(this->processMethod);
265
266 IfStatement* fallthrough = new IfStatement();
267 fallthrough->statements = new StatementBlock;
268 fallthrough->statements->Add(new ReturnStatement(
Manuel Romanb71287f2011-10-06 10:28:35 -0700269 new MethodCall(SUPER_VALUE, "process", 4,
270 this->actionParam, this->requestParam,
271 this->rpcContextParam,
272 this->errorParam)));
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700273 this->dispatchIfStatement->elseif = fallthrough;
274 IfStatement* s = new IfStatement;
275 s->statements = new StatementBlock;
276 this->processMethod->statements->Add(s);
277 s->expression = new Comparison(this->resultData, "!=", NULL_VALUE);
278 s->statements->Add(new ReturnStatement(new MethodCall(this->resultData, "serialize")));
279 s->elseif = new IfStatement;
280 s = s->elseif;
281 s->statements->Add(new ReturnStatement(NULL_VALUE));
282}
283
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700284// =================================================
285class RpcProxyClass : public Class
286{
287public:
288 RpcProxyClass(const interface_type* iface, InterfaceType* interfaceType);
289 virtual ~RpcProxyClass();
290
291 Variable* endpoint;
Joe Onorato95a766d2011-10-09 21:51:46 -0700292 Variable* broker;
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700293
294private:
295 void generate_ctor();
Jason Simmons7dc29732011-12-16 16:14:17 -0800296 void generate_get_endpoint_info();
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700297};
298
299RpcProxyClass::RpcProxyClass(const interface_type* iface, InterfaceType* interfaceType)
300 :Class()
301{
302 this->comment = gather_comments(iface->comments_token->extra);
303 this->modifiers = PUBLIC;
304 this->what = Class::CLASS;
305 this->type = interfaceType;
306
Joe Onorato95a766d2011-10-09 21:51:46 -0700307 // broker
308 this->broker = new Variable(RPC_BROKER_TYPE, "_broker");
309 this->elements.push_back(new Field(PRIVATE, this->broker));
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700310 // endpoint
Joe Onorato95a766d2011-10-09 21:51:46 -0700311 this->endpoint = new Variable(RPC_ENDPOINT_INFO_TYPE, "_endpoint");
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700312 this->elements.push_back(new Field(PRIVATE, this->endpoint));
313
314 // methods
315 generate_ctor();
Jason Simmons7dc29732011-12-16 16:14:17 -0800316 generate_get_endpoint_info();
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700317}
318
319RpcProxyClass::~RpcProxyClass()
320{
321}
322
323void
324RpcProxyClass::generate_ctor()
325{
Joe Onorato95a766d2011-10-09 21:51:46 -0700326 Variable* broker = new Variable(RPC_BROKER_TYPE, "broker");
327 Variable* endpoint = new Variable(RPC_ENDPOINT_INFO_TYPE, "endpoint");
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700328 Method* ctor = new Method;
329 ctor->modifiers = PUBLIC;
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700330 ctor->name = class_name_leaf(this->type->Name());
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700331 ctor->statements = new StatementBlock;
Joe Onorato95a766d2011-10-09 21:51:46 -0700332 ctor->parameters.push_back(broker);
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700333 ctor->parameters.push_back(endpoint);
334 this->elements.push_back(ctor);
335
Joe Onorato95a766d2011-10-09 21:51:46 -0700336 ctor->statements->Add(new Assignment(this->broker, broker));
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700337 ctor->statements->Add(new Assignment(this->endpoint, endpoint));
338}
339
Jason Simmons7dc29732011-12-16 16:14:17 -0800340void
341RpcProxyClass::generate_get_endpoint_info()
342{
343 Method* get = new Method;
344 get->modifiers = PUBLIC;
345 get->returnType = RPC_ENDPOINT_INFO_TYPE;
346 get->name = "getEndpointInfo";
347 get->statements = new StatementBlock;
348 this->elements.push_back(get);
349
350 get->statements->Add(new ReturnStatement(this->endpoint));
351}
352
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700353// =================================================
Joe Onorato28087c62011-10-12 23:22:42 -0700354class EventListenerClass : public DispatcherClass
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700355{
356public:
Joe Onorato28087c62011-10-12 23:22:42 -0700357 EventListenerClass(const interface_type* iface, Type* listenerType);
358 virtual ~EventListenerClass();
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700359
360 Variable* _listener;
361
362private:
363 void generate_ctor();
364};
365
366Expression*
367generate_get_listener_expression(Type* cast)
368{
369 return new Cast(cast, new MethodCall(THIS_VALUE, "getView"));
370}
371
Joe Onorato28087c62011-10-12 23:22:42 -0700372EventListenerClass::EventListenerClass(const interface_type* iface, Type* listenerType)
373 :DispatcherClass(iface, new FieldVariable(THIS_VALUE, "_listener"))
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700374{
375 this->modifiers = PRIVATE;
376 this->what = Class::CLASS;
377 this->type = new Type(iface->package ? iface->package : "",
378 append(iface->name.data, ".Presenter"),
379 Type::GENERATED, false, false, false);
380 this->extends = PRESENTER_BASE_TYPE;
381
382 this->_listener = new Variable(listenerType, "_listener");
Tim Kilbourn22a7cb82011-09-27 10:30:53 -0700383 this->elements.push_back(new Field(PRIVATE, this->_listener));
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700384
385 // methods
386 generate_ctor();
387}
388
Joe Onorato28087c62011-10-12 23:22:42 -0700389EventListenerClass::~EventListenerClass()
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700390{
391}
392
393void
Joe Onorato28087c62011-10-12 23:22:42 -0700394EventListenerClass::generate_ctor()
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700395{
Joe Onorato95a766d2011-10-09 21:51:46 -0700396 Variable* broker = new Variable(RPC_BROKER_TYPE, "broker");
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700397 Variable* listener = new Variable(this->_listener->type, "listener");
398 Method* ctor = new Method;
399 ctor->modifiers = PUBLIC;
400 ctor->name = class_name_leaf(this->type->Name());
401 ctor->statements = new StatementBlock;
Joe Onorato95a766d2011-10-09 21:51:46 -0700402 ctor->parameters.push_back(broker);
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700403 ctor->parameters.push_back(listener);
404 this->elements.push_back(ctor);
405
Joe Onorato28087c62011-10-12 23:22:42 -0700406 ctor->statements->Add(new MethodCall("super", 2, broker, listener));
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700407 ctor->statements->Add(new Assignment(this->_listener, listener));
408}
409
410// =================================================
411class ListenerClass : public Class
412{
413public:
414 ListenerClass(const interface_type* iface);
415 virtual ~ListenerClass();
416
417 bool needed;
418
419private:
420 void generate_ctor();
421};
422
423ListenerClass::ListenerClass(const interface_type* iface)
424 :Class(),
425 needed(false)
426{
427 this->comment = "/** Extend this to listen to the events from this class. */";
428 this->modifiers = STATIC | PUBLIC ;
429 this->what = Class::CLASS;
430 this->type = new Type(iface->package ? iface->package : "",
431 append(iface->name.data, ".Listener"),
432 Type::GENERATED, false, false, false);
433 this->extends = PRESENTER_LISTENER_BASE_TYPE;
434}
435
436ListenerClass::~ListenerClass()
437{
438}
439
440// =================================================
Joe Onorato95a766d2011-10-09 21:51:46 -0700441class EndpointBaseClass : public DispatcherClass
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700442{
443public:
Joe Onorato95a766d2011-10-09 21:51:46 -0700444 EndpointBaseClass(const interface_type* iface);
445 virtual ~EndpointBaseClass();
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700446
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700447 bool needed;
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700448
449private:
450 void generate_ctor();
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700451};
452
Joe Onorato95a766d2011-10-09 21:51:46 -0700453EndpointBaseClass::EndpointBaseClass(const interface_type* iface)
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700454 :DispatcherClass(iface, THIS_VALUE),
455 needed(false)
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700456{
457 this->comment = "/** Extend this to implement a link service. */";
458 this->modifiers = STATIC | PUBLIC | ABSTRACT;
459 this->what = Class::CLASS;
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700460 this->type = new Type(iface->package ? iface->package : "",
Joe Onorato95a766d2011-10-09 21:51:46 -0700461 append(iface->name.data, ".EndpointBase"),
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700462 Type::GENERATED, false, false, false);
Joe Onorato95a766d2011-10-09 21:51:46 -0700463 this->extends = RPC_CONNECTOR_TYPE;
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700464
465 // methods
466 generate_ctor();
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700467}
468
Joe Onorato95a766d2011-10-09 21:51:46 -0700469EndpointBaseClass::~EndpointBaseClass()
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700470{
471}
472
473void
Joe Onorato95a766d2011-10-09 21:51:46 -0700474EndpointBaseClass::generate_ctor()
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700475{
Joe Onorato95a766d2011-10-09 21:51:46 -0700476 Variable* container = new Variable(RPC_CONTAINER_TYPE, "container");
477 Variable* broker = new Variable(RPC_BROKER_TYPE, "broker");
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700478 Method* ctor = new Method;
479 ctor->modifiers = PUBLIC;
480 ctor->name = class_name_leaf(this->type->Name());
481 ctor->statements = new StatementBlock;
Joe Onorato95a766d2011-10-09 21:51:46 -0700482 ctor->parameters.push_back(container);
483 ctor->parameters.push_back(broker);
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700484 this->elements.push_back(ctor);
485
Joe Onorato95a766d2011-10-09 21:51:46 -0700486 ctor->statements->Add(new MethodCall("super", 2, container, broker));
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700487}
488
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700489// =================================================
490class ResultDispatcherClass : public Class
491{
492public:
493 ResultDispatcherClass();
494 virtual ~ResultDispatcherClass();
495
496 void AddMethod(int index, const string& name, Method** method, Variable** param);
497
498 bool needed;
499 Variable* methodId;
500 Variable* callback;
501 Method* onResultMethod;
502 Variable* resultParam;
503 SwitchStatement* methodSwitch;
504
505private:
506 void generate_ctor();
507 void generate_onResult();
508};
509
510ResultDispatcherClass::ResultDispatcherClass()
511 :Class(),
512 needed(false)
513{
514 this->modifiers = PRIVATE | FINAL;
515 this->what = Class::CLASS;
Joe Onorato7db766c2011-09-15 21:31:15 -0700516 this->type = new Type("_ResultDispatcher", Type::GENERATED, false, false, false);
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700517 this->interfaces.push_back(RPC_RESULT_HANDLER_TYPE);
518
519 // methodId
520 this->methodId = new Variable(INT_TYPE, "methodId");
521 this->elements.push_back(new Field(PRIVATE, this->methodId));
522 this->callback = new Variable(OBJECT_TYPE, "callback");
523 this->elements.push_back(new Field(PRIVATE, this->callback));
524
525 // methods
526 generate_ctor();
527 generate_onResult();
528}
529
530ResultDispatcherClass::~ResultDispatcherClass()
531{
532}
533
534void
535ResultDispatcherClass::generate_ctor()
536{
537 Variable* methodIdParam = new Variable(INT_TYPE, "methId");
538 Variable* callbackParam = new Variable(OBJECT_TYPE, "cbObj");
539 Method* ctor = new Method;
540 ctor->modifiers = PUBLIC;
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700541 ctor->name = class_name_leaf(this->type->Name());
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700542 ctor->statements = new StatementBlock;
543 ctor->parameters.push_back(methodIdParam);
544 ctor->parameters.push_back(callbackParam);
545 this->elements.push_back(ctor);
546
547 ctor->statements->Add(new Assignment(this->methodId, methodIdParam));
548 ctor->statements->Add(new Assignment(this->callback, callbackParam));
549}
550
551void
552ResultDispatcherClass::generate_onResult()
553{
554 this->onResultMethod = new Method;
555 this->onResultMethod->modifiers = PUBLIC;
556 this->onResultMethod->returnType = VOID_TYPE;
557 this->onResultMethod->returnTypeDimension = 0;
558 this->onResultMethod->name = "onResult";
559 this->onResultMethod->statements = new StatementBlock;
560 this->elements.push_back(this->onResultMethod);
561
562 this->resultParam = new Variable(BYTE_TYPE, "result", 1);
563 this->onResultMethod->parameters.push_back(this->resultParam);
564
565 this->methodSwitch = new SwitchStatement(this->methodId);
566 this->onResultMethod->statements->Add(this->methodSwitch);
567}
568
569void
570ResultDispatcherClass::AddMethod(int index, const string& name, Method** method, Variable** param)
571{
572 Method* m = new Method;
573 m->modifiers = PUBLIC;
574 m->returnType = VOID_TYPE;
575 m->returnTypeDimension = 0;
576 m->name = name;
577 m->statements = new StatementBlock;
578 *param = new Variable(BYTE_TYPE, "result", 1);
579 m->parameters.push_back(*param);
580 this->elements.push_back(m);
581 *method = m;
582
583 Case* c = new Case(format_int(index));
584 c->statements->Add(new MethodCall(new LiteralExpression("this"), name, 1, this->resultParam));
Joe Onorato05ffbe72011-09-02 15:28:36 -0700585 c->statements->Add(new Break());
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700586
587 this->methodSwitch->cases.push_back(c);
588}
589
590// =================================================
591static void
592generate_new_array(Type* t, StatementBlock* addTo, Variable* v, Variable* from)
593{
594 fprintf(stderr, "aidl: implement generate_new_array %s:%d\n", __FILE__, __LINE__);
595 exit(1);
596}
597
598static void
599generate_create_from_data(Type* t, StatementBlock* addTo, const string& key, Variable* v,
600 Variable* data, Variable** cl)
601{
602 Expression* k = new StringLiteralExpression(key);
603 if (v->dimension == 0) {
604 t->CreateFromRpcData(addTo, k, v, data, cl);
605 }
606 if (v->dimension == 1) {
607 //t->ReadArrayFromRpcData(addTo, v, data, cl);
608 fprintf(stderr, "aidl: implement generate_create_from_data for arrays%s:%d\n",
609 __FILE__, __LINE__);
610 }
611}
612
613static void
614generate_write_to_data(Type* t, StatementBlock* addTo, Expression* k, Variable* v, Variable* data)
615{
616 if (v->dimension == 0) {
617 t->WriteToRpcData(addTo, k, v, data, 0);
618 }
619 if (v->dimension == 1) {
620 //t->WriteArrayToParcel(addTo, v, data);
621 fprintf(stderr, "aidl: implement generate_write_to_data for arrays%s:%d\n",
622 __FILE__, __LINE__);
623 }
624}
625
626// =================================================
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700627static Type*
628generate_results_method(const method_type* method, RpcProxyClass* proxyClass)
629{
630 arg_type* arg;
631
632 string resultsMethodName = results_method_name(method->name.data);
633 Type* resultsInterfaceType = new Type(results_class_name(method->name.data),
Joe Onorato7db766c2011-09-15 21:31:15 -0700634 Type::GENERATED, false, false, false);
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700635
636 if (!method->oneway) {
637 Class* resultsClass = new Class;
638 resultsClass->modifiers = STATIC | PUBLIC;
639 resultsClass->what = Class::INTERFACE;
640 resultsClass->type = resultsInterfaceType;
641
642 Method* resultMethod = new Method;
643 resultMethod->comment = gather_comments(method->comments_token->extra);
644 resultMethod->modifiers = PUBLIC;
645 resultMethod->returnType = VOID_TYPE;
646 resultMethod->returnTypeDimension = 0;
647 resultMethod->name = resultsMethodName;
648 if (0 != strcmp("void", method->type.type.data)) {
649 resultMethod->parameters.push_back(new Variable(NAMES.Search(method->type.type.data),
650 "_result", method->type.dimension));
651 }
652 arg = method->args;
653 while (arg != NULL) {
654 if (convert_direction(arg->direction.data) & OUT_PARAMETER) {
655 resultMethod->parameters.push_back(new Variable(
656 NAMES.Search(arg->type.type.data), arg->name.data,
657 arg->type.dimension));
658 }
659 arg = arg->next;
660 }
661 resultsClass->elements.push_back(resultMethod);
662
663 if (resultMethod->parameters.size() > 0) {
664 proxyClass->elements.push_back(resultsClass);
665 return resultsInterfaceType;
666 }
667 }
668 //delete resultsInterfaceType;
669 return NULL;
670}
671
672static void
673generate_proxy_method(const method_type* method, RpcProxyClass* proxyClass,
674 ResultDispatcherClass* resultsDispatcherClass, Type* resultsInterfaceType, int index)
675{
676 arg_type* arg;
677 Method* proxyMethod = new Method;
678 proxyMethod->comment = gather_comments(method->comments_token->extra);
679 proxyMethod->modifiers = PUBLIC;
680 proxyMethod->returnType = VOID_TYPE;
681 proxyMethod->returnTypeDimension = 0;
682 proxyMethod->name = method->name.data;
683 proxyMethod->statements = new StatementBlock;
684 proxyClass->elements.push_back(proxyMethod);
685
686 // The local variables
687 Variable* _data = new Variable(RPC_DATA_TYPE, "_data");
688 proxyMethod->statements->Add(new VariableDeclaration(_data, new NewExpression(RPC_DATA_TYPE)));
689
690 // Add the arguments
691 arg = method->args;
692 while (arg != NULL) {
693 if (convert_direction(arg->direction.data) & IN_PARAMETER) {
694 // Function signature
695 Type* t = NAMES.Search(arg->type.type.data);
696 Variable* v = new Variable(t, arg->name.data, arg->type.dimension);
697 proxyMethod->parameters.push_back(v);
698
699 // Input parameter marshalling
700 generate_write_to_data(t, proxyMethod->statements,
701 new StringLiteralExpression(arg->name.data), v, _data);
702 }
703 arg = arg->next;
704 }
705
706 // If there is a results interface for this class
707 Expression* resultParameter;
708 if (resultsInterfaceType != NULL) {
709 // Result interface parameter
710 Variable* resultListener = new Variable(resultsInterfaceType, "_result");
711 proxyMethod->parameters.push_back(resultListener);
712
713 // Add the results dispatcher callback
714 resultsDispatcherClass->needed = true;
715 resultParameter = new NewExpression(resultsDispatcherClass->type, 2,
716 new LiteralExpression(format_int(index)), resultListener);
717 } else {
718 resultParameter = NULL_VALUE;
719 }
720
721 // All proxy methods take an error parameter
722 Variable* errorListener = new Variable(RPC_ERROR_LISTENER_TYPE, "_errors");
723 proxyMethod->parameters.push_back(errorListener);
724
725 // Call the broker
Joe Onorato95a766d2011-10-09 21:51:46 -0700726 proxyMethod->statements->Add(new MethodCall(new FieldVariable(THIS_VALUE, "_broker"),
727 "sendRpc", 5,
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700728 proxyClass->endpoint,
Joe Onorato95a766d2011-10-09 21:51:46 -0700729 new StringLiteralExpression(method->name.data),
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700730 new MethodCall(_data, "serialize"),
731 resultParameter,
732 errorListener));
733}
734
735static void
736generate_result_dispatcher_method(const method_type* method,
737 ResultDispatcherClass* resultsDispatcherClass, Type* resultsInterfaceType, int index)
738{
739 arg_type* arg;
740 Method* dispatchMethod;
741 Variable* dispatchParam;
742 resultsDispatcherClass->AddMethod(index, method->name.data, &dispatchMethod, &dispatchParam);
743
744 Variable* classLoader = NULL;
745 Variable* resultData = new Variable(RPC_DATA_TYPE, "resultData");
746 dispatchMethod->statements->Add(new VariableDeclaration(resultData,
747 new NewExpression(RPC_DATA_TYPE, 1, dispatchParam)));
748
749 // The callback method itself
750 MethodCall* realCall = new MethodCall(
751 new Cast(resultsInterfaceType, new FieldVariable(THIS_VALUE, "callback")),
752 results_method_name(method->name.data));
753
754 // The return value
755 {
756 Type* t = NAMES.Search(method->type.type.data);
Joe Onorato0a7eaec2011-11-17 16:13:49 -0800757 if (t != VOID_TYPE) {
758 Variable* rv = new Variable(t, "rv");
759 dispatchMethod->statements->Add(new VariableDeclaration(rv));
760 generate_create_from_data(t, dispatchMethod->statements, "_result", rv,
761 resultData, &classLoader);
762 realCall->arguments.push_back(rv);
763 }
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700764 }
765
766 VariableFactory stubArgs("arg");
767 arg = method->args;
768 while (arg != NULL) {
769 if (convert_direction(arg->direction.data) & OUT_PARAMETER) {
770 // Unmarshall the results
771 Type* t = NAMES.Search(arg->type.type.data);
772 Variable* v = stubArgs.Get(t);
773 dispatchMethod->statements->Add(new VariableDeclaration(v));
774
775 generate_create_from_data(t, dispatchMethod->statements, arg->name.data, v,
776 resultData, &classLoader);
777
778 // Add the argument to the callback
779 realCall->arguments.push_back(v);
780 }
781 arg = arg->next;
782 }
783
784 // Call the callback method
785 dispatchMethod->statements->Add(realCall);
786}
787
788static void
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700789generate_regular_method(const method_type* method, RpcProxyClass* proxyClass,
Joe Onorato95a766d2011-10-09 21:51:46 -0700790 EndpointBaseClass* serviceBaseClass, ResultDispatcherClass* resultsDispatcherClass,
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700791 int index)
792{
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700793 arg_type* arg;
794
795 // == the callback interface for results ================================
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700796 // the service base class
797 Type* resultsInterfaceType = generate_results_method(method, proxyClass);
798
799 // == the method in the proxy class =====================================
800 generate_proxy_method(method, proxyClass, resultsDispatcherClass, resultsInterfaceType, index);
801
802 // == the method in the result dispatcher class =========================
803 if (resultsInterfaceType != NULL) {
804 generate_result_dispatcher_method(method, resultsDispatcherClass, resultsInterfaceType,
805 index);
806 }
807
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700808 // == The abstract method that the service developers implement ==========
809 Method* decl = new Method;
810 decl->comment = gather_comments(method->comments_token->extra);
811 decl->modifiers = PUBLIC | ABSTRACT;
812 decl->returnType = NAMES.Search(method->type.type.data);
813 decl->returnTypeDimension = method->type.dimension;
814 decl->name = method->name.data;
815 arg = method->args;
816 while (arg != NULL) {
817 decl->parameters.push_back(new Variable(
818 NAMES.Search(arg->type.type.data), arg->name.data,
819 arg->type.dimension));
820 arg = arg->next;
821 }
Manuel Romanb71287f2011-10-06 10:28:35 -0700822
823 // Add the default RpcContext param to all methods
824 decl->parameters.push_back(new Variable(RPC_CONTEXT_TYPE, "context", 0));
825
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700826 serviceBaseClass->elements.push_back(decl);
827
828
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700829 // == the dispatch method in the service base class ======================
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700830 serviceBaseClass->AddMethod(method);
831}
832
833static void
834generate_event_method(const method_type* method, RpcProxyClass* proxyClass,
Joe Onorato95a766d2011-10-09 21:51:46 -0700835 EndpointBaseClass* serviceBaseClass, ListenerClass* listenerClass,
Joe Onorato28087c62011-10-12 23:22:42 -0700836 EventListenerClass* presenterClass, int index)
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700837{
838 arg_type* arg;
839 listenerClass->needed = true;
840
841 // == the push method in the service base class =========================
842 Method* push = new Method;
843 push->modifiers = PUBLIC;
844 push->name = push_method_name(method->name.data);
845 push->statements = new StatementBlock;
846 push->returnType = VOID_TYPE;
847 serviceBaseClass->elements.push_back(push);
848
849 // The local variables
850 Variable* _data = new Variable(RPC_DATA_TYPE, "_data");
851 push->statements->Add(new VariableDeclaration(_data, new NewExpression(RPC_DATA_TYPE)));
852
853 // Add the arguments
854 arg = method->args;
855 while (arg != NULL) {
856 // Function signature
857 Type* t = NAMES.Search(arg->type.type.data);
858 Variable* v = new Variable(t, arg->name.data, arg->type.dimension);
859 push->parameters.push_back(v);
860
861 // Input parameter marshalling
862 generate_write_to_data(t, push->statements,
863 new StringLiteralExpression(arg->name.data), v, _data);
864
865 arg = arg->next;
866 }
867
868 // Send the notifications
869 push->statements->Add(new MethodCall("pushEvent", 2,
870 new StringLiteralExpression(method->name.data),
871 new MethodCall(_data, "serialize")));
872
873 // == the event callback dispatcher method ====================================
874 presenterClass->AddMethod(method);
875
876 // == the event method in the listener base class =====================
877 Method* event = new Method;
878 event->modifiers = PUBLIC;
879 event->name = method->name.data;
880 event->statements = new StatementBlock;
881 event->returnType = VOID_TYPE;
882 listenerClass->elements.push_back(event);
883 arg = method->args;
884 while (arg != NULL) {
885 event->parameters.push_back(new Variable(
886 NAMES.Search(arg->type.type.data), arg->name.data,
887 arg->type.dimension));
888 arg = arg->next;
889 }
Manuel Romanb71287f2011-10-06 10:28:35 -0700890
891 // Add a final parameter: RpcContext. Contains data about
892 // incoming request (e.g., certificate)
893 event->parameters.push_back(new Variable(RPC_CONTEXT_TYPE, "context", 0));
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700894}
895
896static void
897generate_listener_methods(RpcProxyClass* proxyClass, Type* presenterType, Type* listenerType)
898{
899 // AndroidAtHomePresenter _presenter;
Joe Onorato28087c62011-10-12 23:22:42 -0700900 // void startListening(Listener listener) {
901 // stopListening();
902 // _presenter = new Presenter(_broker, listener);
903 // _presenter.startListening(_endpoint);
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700904 // }
Joe Onorato28087c62011-10-12 23:22:42 -0700905 // void stopListening() {
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700906 // if (_presenter != null) {
Joe Onorato28087c62011-10-12 23:22:42 -0700907 // _presenter.stopListening();
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700908 // }
909 // }
910
911 Variable* _presenter = new Variable(presenterType, "_presenter");
912 proxyClass->elements.push_back(new Field(PRIVATE, _presenter));
913
914 Variable* listener = new Variable(listenerType, "listener");
915
Joe Onorato28087c62011-10-12 23:22:42 -0700916 Method* startListeningMethod = new Method;
917 startListeningMethod->modifiers = PUBLIC;
918 startListeningMethod->returnType = VOID_TYPE;
919 startListeningMethod->name = "startListening";
920 startListeningMethod->statements = new StatementBlock;
921 startListeningMethod->parameters.push_back(listener);
922 proxyClass->elements.push_back(startListeningMethod);
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700923
Joe Onorato28087c62011-10-12 23:22:42 -0700924 startListeningMethod->statements->Add(new MethodCall(THIS_VALUE, "stopListening"));
925 startListeningMethod->statements->Add(new Assignment(_presenter,
926 new NewExpression(presenterType, 2, proxyClass->broker, listener)));
927 startListeningMethod->statements->Add(new MethodCall(_presenter,
928 "startListening", 1, proxyClass->endpoint));
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700929
Joe Onorato28087c62011-10-12 23:22:42 -0700930 Method* stopListeningMethod = new Method;
931 stopListeningMethod->modifiers = PUBLIC;
932 stopListeningMethod->returnType = VOID_TYPE;
933 stopListeningMethod->name = "stopListening";
934 stopListeningMethod->statements = new StatementBlock;
935 proxyClass->elements.push_back(stopListeningMethod);
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700936
937 IfStatement* ifst = new IfStatement;
938 ifst->expression = new Comparison(_presenter, "!=", NULL_VALUE);
Joe Onorato28087c62011-10-12 23:22:42 -0700939 stopListeningMethod->statements->Add(ifst);
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700940
Joe Onorato28087c62011-10-12 23:22:42 -0700941 ifst->statements->Add(new MethodCall(_presenter, "stopListening"));
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700942 ifst->statements->Add(new Assignment(_presenter, NULL_VALUE));
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700943}
944
945Class*
946generate_rpc_interface_class(const interface_type* iface)
947{
948 // the proxy class
949 InterfaceType* interfaceType = static_cast<InterfaceType*>(
950 NAMES.Find(iface->package, iface->name.data));
951 RpcProxyClass* proxy = new RpcProxyClass(iface, interfaceType);
952
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700953 // the listener class
954 ListenerClass* listener = new ListenerClass(iface);
955
956 // the presenter class
Joe Onorato28087c62011-10-12 23:22:42 -0700957 EventListenerClass* presenter = new EventListenerClass(iface, listener->type);
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700958
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700959 // the service base class
Joe Onorato95a766d2011-10-09 21:51:46 -0700960 EndpointBaseClass* base = new EndpointBaseClass(iface);
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700961 proxy->elements.push_back(base);
962
963 // the result dispatcher
964 ResultDispatcherClass* results = new ResultDispatcherClass();
965
966 // all the declared methods of the proxy
967 int index = 0;
968 interface_item_type* item = iface->interface_items;
969 while (item != NULL) {
970 if (item->item_type == METHOD_TYPE) {
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700971 if (NAMES.Search(((method_type*)item)->type.type.data) == EVENT_FAKE_TYPE) {
972 generate_event_method((method_type*)item, proxy, base, listener, presenter, index);
973 } else {
974 generate_regular_method((method_type*)item, proxy, base, results, index);
975 }
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700976 }
977 item = item->next;
978 index++;
979 }
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700980 presenter->DoneWithMethods();
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700981 base->DoneWithMethods();
982
983 // only add this if there are methods with results / out parameters
984 if (results->needed) {
985 proxy->elements.push_back(results);
986 }
Joe Onoratoe24dbea2011-09-23 15:17:52 -0700987 if (listener->needed) {
988 proxy->elements.push_back(listener);
989 proxy->elements.push_back(presenter);
990 generate_listener_methods(proxy, presenter->type, listener->type);
991 }
Joe Onoratofdfe2ff2011-08-30 17:24:17 -0700992
993 return proxy;
994}