blob: 8c948753c3b3576b57f0054024cdb329b04b8f2f [file] [log] [blame]
Joe Onorato7add83b2011-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
Joe Onorato9a782242011-10-23 15:15:27 -07008Type* ANDROID_CONTEXT_TYPE = new Type("android.content",
Manuel Roman2c5eb262011-10-06 10:28:35 -07009 "Context", Type::BUILT_IN, false, false, false);
Joe Onorato0c861962011-12-11 20:42:33 -080010Type* PRESENTER_BASE_TYPE = new Type("android.support.place.connector",
Joe Onoratoc7ec1dc2011-10-12 23:22:42 -070011 "EventListener", Type::BUILT_IN, false, false, false);
Joe Onorato0c861962011-12-11 20:42:33 -080012Type* PRESENTER_LISTENER_BASE_TYPE = new Type("android.support.place.connector",
Joe Onoratoc7ec1dc2011-10-12 23:22:42 -070013 "EventListener.Listener", Type::BUILT_IN, false, false, false);
Joe Onorato0c861962011-12-11 20:42:33 -080014Type* RPC_BROKER_TYPE = new Type("android.support.place.connector", "Broker",
Joe Onoratoe415ecb2011-09-23 15:17:52 -070015 Type::BUILT_IN, false, false, false);
Joe Onorato0d2a6b62011-10-09 21:51:46 -070016// TODO: Just use Endpoint, so this works for all endpoints.
Joe Onorato0c861962011-12-11 20:42:33 -080017Type* RPC_CONNECTOR_TYPE = new Type("android.support.place.connector", "Connector",
Joe Onorato0d2a6b62011-10-09 21:51:46 -070018 Type::BUILT_IN, false, false, false);
Joe Onorato0c861962011-12-11 20:42:33 -080019Type* RPC_ENDPOINT_INFO_TYPE = new UserDataType("android.support.place.rpc",
Joe Onorato0d2a6b62011-10-09 21:51:46 -070020 "EndpointInfo", true, __FILE__, __LINE__);
Joe Onorato0c861962011-12-11 20:42:33 -080021Type* RPC_RESULT_HANDLER_TYPE = new UserDataType("android.support.place.rpc", "RpcResultHandler",
Joe Onoratoe415ecb2011-09-23 15:17:52 -070022 true, __FILE__, __LINE__);
Joe Onorato0c861962011-12-11 20:42:33 -080023Type* RPC_ERROR_LISTENER_TYPE = new Type("android.support.place.rpc", "RpcErrorHandler",
Joe Onoratoe415ecb2011-09-23 15:17:52 -070024 Type::BUILT_IN, false, false, false);
Joe Onorato0c861962011-12-11 20:42:33 -080025Type* RPC_CONTEXT_TYPE = new UserDataType("android.support.place.rpc", "RpcContext", true,
Manuel Roman2c5eb262011-10-06 10:28:35 -070026 __FILE__, __LINE__);
Joe Onoratoe415ecb2011-09-23 15:17:52 -070027
28static void generate_create_from_data(Type* t, StatementBlock* addTo, const string& key,
29 Variable* v, Variable* data, Variable** cl);
30static void generate_new_array(Type* t, StatementBlock* addTo, Variable* v, Variable* from);
31static void generate_write_to_data(Type* t, StatementBlock* addTo, Expression* k, Variable* v,
32 Variable* data);
Joe Onorato7add83b2011-08-30 17:24:17 -070033
34static string
35format_int(int n)
36{
37 char str[20];
38 sprintf(str, "%d", n);
39 return string(str);
40}
41
42static string
43class_name_leaf(const string& str)
44{
45 string::size_type pos = str.rfind('.');
46 if (pos == string::npos) {
47 return str;
48 } else {
49 return string(str, pos+1);
50 }
51}
52
Joe Onoratoe415ecb2011-09-23 15:17:52 -070053static string
54results_class_name(const string& n)
55{
56 string str = n;
57 str[0] = toupper(str[0]);
58 str.insert(0, "On");
59 return str;
60}
61
62static string
63results_method_name(const string& n)
64{
65 string str = n;
66 str[0] = toupper(str[0]);
67 str.insert(0, "on");
68 return str;
69}
70
71static string
72push_method_name(const string& n)
73{
74 string str = n;
75 str[0] = toupper(str[0]);
76 str.insert(0, "push");
77 return str;
78}
79
80// =================================================
81class DispatcherClass : public Class
82{
83public:
84 DispatcherClass(const interface_type* iface, Expression* target);
85 virtual ~DispatcherClass();
86
87 void AddMethod(const method_type* method);
88 void DoneWithMethods();
89
90 Method* processMethod;
91 Variable* actionParam;
92 Variable* requestParam;
Manuel Roman2c5eb262011-10-06 10:28:35 -070093 Variable* rpcContextParam;
Joe Onoratoe415ecb2011-09-23 15:17:52 -070094 Variable* errorParam;
95 Variable* requestData;
96 Variable* resultData;
97 IfStatement* dispatchIfStatement;
98 Expression* targetExpression;
99
100private:
101 void generate_process();
102};
103
104DispatcherClass::DispatcherClass(const interface_type* iface, Expression* target)
105 :Class(),
106 dispatchIfStatement(NULL),
107 targetExpression(target)
108{
109 generate_process();
110}
111
112DispatcherClass::~DispatcherClass()
113{
114}
115
116void
117DispatcherClass::generate_process()
118{
Manuel Roman2c5eb262011-10-06 10:28:35 -0700119 // byte[] process(String action, byte[] params, RpcContext context, RpcError status)
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700120 this->processMethod = new Method;
121 this->processMethod->modifiers = PUBLIC;
122 this->processMethod->returnType = BYTE_TYPE;
123 this->processMethod->returnTypeDimension = 1;
124 this->processMethod->name = "process";
125 this->processMethod->statements = new StatementBlock;
126
127 this->actionParam = new Variable(STRING_TYPE, "action");
128 this->processMethod->parameters.push_back(this->actionParam);
129
130 this->requestParam = new Variable(BYTE_TYPE, "requestParam", 1);
131 this->processMethod->parameters.push_back(this->requestParam);
132
Manuel Roman2c5eb262011-10-06 10:28:35 -0700133 this->rpcContextParam = new Variable(RPC_CONTEXT_TYPE, "context", 0);
134 this->processMethod->parameters.push_back(this->rpcContextParam);
135
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700136 this->errorParam = new Variable(RPC_ERROR_TYPE, "errorParam", 0);
137 this->processMethod->parameters.push_back(this->errorParam);
138
139 this->requestData = new Variable(RPC_DATA_TYPE, "request");
140 this->processMethod->statements->Add(new VariableDeclaration(requestData,
141 new NewExpression(RPC_DATA_TYPE, 1, this->requestParam)));
142
143 this->resultData = new Variable(RPC_DATA_TYPE, "resultData");
144 this->processMethod->statements->Add(new VariableDeclaration(this->resultData,
145 NULL_VALUE));
146}
147
148void
149DispatcherClass::AddMethod(const method_type* method)
150{
151 arg_type* arg;
152
153 // The if/switch statement
154 IfStatement* ifs = new IfStatement();
155 ifs->expression = new MethodCall(new StringLiteralExpression(method->name.data), "equals",
156 1, this->actionParam);
157 StatementBlock* block = ifs->statements = new StatementBlock;
158 if (this->dispatchIfStatement == NULL) {
159 this->dispatchIfStatement = ifs;
160 this->processMethod->statements->Add(dispatchIfStatement);
161 } else {
162 this->dispatchIfStatement->elseif = ifs;
163 this->dispatchIfStatement = ifs;
164 }
165
166 // The call to decl (from above)
167 MethodCall* realCall = new MethodCall(this->targetExpression, method->name.data);
168
169 // args
170 Variable* classLoader = NULL;
171 VariableFactory stubArgs("_arg");
172 arg = method->args;
173 while (arg != NULL) {
174 Type* t = NAMES.Search(arg->type.type.data);
175 Variable* v = stubArgs.Get(t);
176 v->dimension = arg->type.dimension;
177
178 // Unmarshall the parameter
179 block->Add(new VariableDeclaration(v));
180 if (convert_direction(arg->direction.data) & IN_PARAMETER) {
181 generate_create_from_data(t, block, arg->name.data, v,
182 this->requestData, &classLoader);
183 } else {
184 if (arg->type.dimension == 0) {
185 block->Add(new Assignment(v, new NewExpression(v->type)));
186 }
187 else if (arg->type.dimension == 1) {
188 generate_new_array(v->type, block, v, this->requestData);
189 }
190 else {
191 fprintf(stderr, "aidl:internal error %s:%d\n", __FILE__,
192 __LINE__);
193 }
194 }
195
196 // Add that parameter to the method call
197 realCall->arguments.push_back(v);
198
199 arg = arg->next;
200 }
201
Manuel Roman2c5eb262011-10-06 10:28:35 -0700202 // Add a final parameter: RpcContext. Contains data about
203 // incoming request (e.g., certificate)
204 realCall->arguments.push_back(new Variable(RPC_CONTEXT_TYPE, "context", 0));
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700205
206 Type* returnType = NAMES.Search(method->type.type.data);
207 if (returnType == EVENT_FAKE_TYPE) {
208 returnType = VOID_TYPE;
209 }
210
211 // the real call
212 bool first = true;
213 Variable* _result = NULL;
214 if (returnType == VOID_TYPE) {
215 block->Add(realCall);
216 } else {
217 _result = new Variable(returnType, "_result",
218 method->type.dimension);
219 block->Add(new VariableDeclaration(_result, realCall));
220
221 // need the result RpcData
222 if (first) {
223 block->Add(new Assignment(this->resultData,
224 new NewExpression(RPC_DATA_TYPE)));
225 first = false;
226 }
227
228 // marshall the return value
229 generate_write_to_data(returnType, block,
230 new StringLiteralExpression("_result"), _result, this->resultData);
231 }
232
233 // out parameters
234 int i = 0;
235 arg = method->args;
236 while (arg != NULL) {
237 Type* t = NAMES.Search(arg->type.type.data);
238 Variable* v = stubArgs.Get(i++);
239
240 if (convert_direction(arg->direction.data) & OUT_PARAMETER) {
241 // need the result RpcData
242 if (first) {
243 block->Add(new Assignment(this->resultData, new NewExpression(RPC_DATA_TYPE)));
244 first = false;
245 }
246
247 generate_write_to_data(t, block, new StringLiteralExpression(arg->name.data),
248 v, this->resultData);
249 }
250
251 arg = arg->next;
252 }
253}
254
255void
256DispatcherClass::DoneWithMethods()
257{
258 if (this->dispatchIfStatement == NULL) {
259 return;
260 }
261
262 this->elements.push_back(this->processMethod);
263
264 IfStatement* fallthrough = new IfStatement();
265 fallthrough->statements = new StatementBlock;
266 fallthrough->statements->Add(new ReturnStatement(
Manuel Roman2c5eb262011-10-06 10:28:35 -0700267 new MethodCall(SUPER_VALUE, "process", 4,
268 this->actionParam, this->requestParam,
269 this->rpcContextParam,
270 this->errorParam)));
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700271 this->dispatchIfStatement->elseif = fallthrough;
272 IfStatement* s = new IfStatement;
273 s->statements = new StatementBlock;
274 this->processMethod->statements->Add(s);
275 s->expression = new Comparison(this->resultData, "!=", NULL_VALUE);
276 s->statements->Add(new ReturnStatement(new MethodCall(this->resultData, "serialize")));
277 s->elseif = new IfStatement;
278 s = s->elseif;
279 s->statements->Add(new ReturnStatement(NULL_VALUE));
280}
281
Joe Onorato7add83b2011-08-30 17:24:17 -0700282// =================================================
283class RpcProxyClass : public Class
284{
285public:
286 RpcProxyClass(const interface_type* iface, InterfaceType* interfaceType);
287 virtual ~RpcProxyClass();
288
289 Variable* endpoint;
Joe Onorato0d2a6b62011-10-09 21:51:46 -0700290 Variable* broker;
Joe Onorato7add83b2011-08-30 17:24:17 -0700291
292private:
293 void generate_ctor();
294};
295
296RpcProxyClass::RpcProxyClass(const interface_type* iface, InterfaceType* interfaceType)
297 :Class()
298{
299 this->comment = gather_comments(iface->comments_token->extra);
300 this->modifiers = PUBLIC;
301 this->what = Class::CLASS;
302 this->type = interfaceType;
303
Joe Onorato0d2a6b62011-10-09 21:51:46 -0700304 // broker
305 this->broker = new Variable(RPC_BROKER_TYPE, "_broker");
306 this->elements.push_back(new Field(PRIVATE, this->broker));
Joe Onorato7add83b2011-08-30 17:24:17 -0700307 // endpoint
Joe Onorato0d2a6b62011-10-09 21:51:46 -0700308 this->endpoint = new Variable(RPC_ENDPOINT_INFO_TYPE, "_endpoint");
Joe Onorato7add83b2011-08-30 17:24:17 -0700309 this->elements.push_back(new Field(PRIVATE, this->endpoint));
310
311 // methods
312 generate_ctor();
313}
314
315RpcProxyClass::~RpcProxyClass()
316{
317}
318
319void
320RpcProxyClass::generate_ctor()
321{
Joe Onorato0d2a6b62011-10-09 21:51:46 -0700322 Variable* broker = new Variable(RPC_BROKER_TYPE, "broker");
323 Variable* endpoint = new Variable(RPC_ENDPOINT_INFO_TYPE, "endpoint");
Joe Onorato7add83b2011-08-30 17:24:17 -0700324 Method* ctor = new Method;
325 ctor->modifiers = PUBLIC;
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700326 ctor->name = class_name_leaf(this->type->Name());
Joe Onorato7add83b2011-08-30 17:24:17 -0700327 ctor->statements = new StatementBlock;
Joe Onorato0d2a6b62011-10-09 21:51:46 -0700328 ctor->parameters.push_back(broker);
Joe Onorato7add83b2011-08-30 17:24:17 -0700329 ctor->parameters.push_back(endpoint);
330 this->elements.push_back(ctor);
331
Joe Onorato0d2a6b62011-10-09 21:51:46 -0700332 ctor->statements->Add(new Assignment(this->broker, broker));
Joe Onorato7add83b2011-08-30 17:24:17 -0700333 ctor->statements->Add(new Assignment(this->endpoint, endpoint));
334}
335
336// =================================================
Joe Onoratoc7ec1dc2011-10-12 23:22:42 -0700337class EventListenerClass : public DispatcherClass
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700338{
339public:
Joe Onoratoc7ec1dc2011-10-12 23:22:42 -0700340 EventListenerClass(const interface_type* iface, Type* listenerType);
341 virtual ~EventListenerClass();
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700342
343 Variable* _listener;
344
345private:
346 void generate_ctor();
347};
348
349Expression*
350generate_get_listener_expression(Type* cast)
351{
352 return new Cast(cast, new MethodCall(THIS_VALUE, "getView"));
353}
354
Joe Onoratoc7ec1dc2011-10-12 23:22:42 -0700355EventListenerClass::EventListenerClass(const interface_type* iface, Type* listenerType)
356 :DispatcherClass(iface, new FieldVariable(THIS_VALUE, "_listener"))
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700357{
358 this->modifiers = PRIVATE;
359 this->what = Class::CLASS;
360 this->type = new Type(iface->package ? iface->package : "",
361 append(iface->name.data, ".Presenter"),
362 Type::GENERATED, false, false, false);
363 this->extends = PRESENTER_BASE_TYPE;
364
365 this->_listener = new Variable(listenerType, "_listener");
Tim Kilbournb8a6f222011-09-27 10:30:53 -0700366 this->elements.push_back(new Field(PRIVATE, this->_listener));
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700367
368 // methods
369 generate_ctor();
370}
371
Joe Onoratoc7ec1dc2011-10-12 23:22:42 -0700372EventListenerClass::~EventListenerClass()
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700373{
374}
375
376void
Joe Onoratoc7ec1dc2011-10-12 23:22:42 -0700377EventListenerClass::generate_ctor()
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700378{
Joe Onorato0d2a6b62011-10-09 21:51:46 -0700379 Variable* broker = new Variable(RPC_BROKER_TYPE, "broker");
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700380 Variable* listener = new Variable(this->_listener->type, "listener");
381 Method* ctor = new Method;
382 ctor->modifiers = PUBLIC;
383 ctor->name = class_name_leaf(this->type->Name());
384 ctor->statements = new StatementBlock;
Joe Onorato0d2a6b62011-10-09 21:51:46 -0700385 ctor->parameters.push_back(broker);
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700386 ctor->parameters.push_back(listener);
387 this->elements.push_back(ctor);
388
Joe Onoratoc7ec1dc2011-10-12 23:22:42 -0700389 ctor->statements->Add(new MethodCall("super", 2, broker, listener));
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700390 ctor->statements->Add(new Assignment(this->_listener, listener));
391}
392
393// =================================================
394class ListenerClass : public Class
395{
396public:
397 ListenerClass(const interface_type* iface);
398 virtual ~ListenerClass();
399
400 bool needed;
401
402private:
403 void generate_ctor();
404};
405
406ListenerClass::ListenerClass(const interface_type* iface)
407 :Class(),
408 needed(false)
409{
410 this->comment = "/** Extend this to listen to the events from this class. */";
411 this->modifiers = STATIC | PUBLIC ;
412 this->what = Class::CLASS;
413 this->type = new Type(iface->package ? iface->package : "",
414 append(iface->name.data, ".Listener"),
415 Type::GENERATED, false, false, false);
416 this->extends = PRESENTER_LISTENER_BASE_TYPE;
417}
418
419ListenerClass::~ListenerClass()
420{
421}
422
423// =================================================
Joe Onorato0d2a6b62011-10-09 21:51:46 -0700424class EndpointBaseClass : public DispatcherClass
Joe Onorato7add83b2011-08-30 17:24:17 -0700425{
426public:
Joe Onorato0d2a6b62011-10-09 21:51:46 -0700427 EndpointBaseClass(const interface_type* iface);
428 virtual ~EndpointBaseClass();
Joe Onorato7add83b2011-08-30 17:24:17 -0700429
Joe Onorato7add83b2011-08-30 17:24:17 -0700430 bool needed;
Joe Onorato7add83b2011-08-30 17:24:17 -0700431
432private:
433 void generate_ctor();
Joe Onorato7add83b2011-08-30 17:24:17 -0700434};
435
Joe Onorato0d2a6b62011-10-09 21:51:46 -0700436EndpointBaseClass::EndpointBaseClass(const interface_type* iface)
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700437 :DispatcherClass(iface, THIS_VALUE),
438 needed(false)
Joe Onorato7add83b2011-08-30 17:24:17 -0700439{
440 this->comment = "/** Extend this to implement a link service. */";
441 this->modifiers = STATIC | PUBLIC | ABSTRACT;
442 this->what = Class::CLASS;
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700443 this->type = new Type(iface->package ? iface->package : "",
Joe Onorato0d2a6b62011-10-09 21:51:46 -0700444 append(iface->name.data, ".EndpointBase"),
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700445 Type::GENERATED, false, false, false);
Joe Onorato0d2a6b62011-10-09 21:51:46 -0700446 this->extends = RPC_CONNECTOR_TYPE;
Joe Onorato7add83b2011-08-30 17:24:17 -0700447
448 // methods
449 generate_ctor();
Joe Onorato7add83b2011-08-30 17:24:17 -0700450}
451
Joe Onorato0d2a6b62011-10-09 21:51:46 -0700452EndpointBaseClass::~EndpointBaseClass()
Joe Onorato7add83b2011-08-30 17:24:17 -0700453{
454}
455
456void
Joe Onorato0d2a6b62011-10-09 21:51:46 -0700457EndpointBaseClass::generate_ctor()
Joe Onorato7add83b2011-08-30 17:24:17 -0700458{
Joe Onorato9a782242011-10-23 15:15:27 -0700459 Variable* container = new Variable(ANDROID_CONTEXT_TYPE, "context");
Joe Onorato0d2a6b62011-10-09 21:51:46 -0700460 Variable* broker = new Variable(RPC_BROKER_TYPE, "broker");
Joe Onorato7add83b2011-08-30 17:24:17 -0700461 Method* ctor = new Method;
462 ctor->modifiers = PUBLIC;
463 ctor->name = class_name_leaf(this->type->Name());
464 ctor->statements = new StatementBlock;
Joe Onorato0d2a6b62011-10-09 21:51:46 -0700465 ctor->parameters.push_back(container);
466 ctor->parameters.push_back(broker);
Joe Onorato7add83b2011-08-30 17:24:17 -0700467 this->elements.push_back(ctor);
468
Joe Onorato0d2a6b62011-10-09 21:51:46 -0700469 ctor->statements->Add(new MethodCall("super", 2, container, broker));
Joe Onorato7add83b2011-08-30 17:24:17 -0700470}
471
Joe Onorato7add83b2011-08-30 17:24:17 -0700472// =================================================
473class ResultDispatcherClass : public Class
474{
475public:
476 ResultDispatcherClass();
477 virtual ~ResultDispatcherClass();
478
479 void AddMethod(int index, const string& name, Method** method, Variable** param);
480
481 bool needed;
482 Variable* methodId;
483 Variable* callback;
484 Method* onResultMethod;
485 Variable* resultParam;
486 SwitchStatement* methodSwitch;
487
488private:
489 void generate_ctor();
490 void generate_onResult();
491};
492
493ResultDispatcherClass::ResultDispatcherClass()
494 :Class(),
495 needed(false)
496{
497 this->modifiers = PRIVATE | FINAL;
498 this->what = Class::CLASS;
Joe Onorato0ca2a36d2011-09-15 21:31:15 -0700499 this->type = new Type("_ResultDispatcher", Type::GENERATED, false, false, false);
Joe Onorato7add83b2011-08-30 17:24:17 -0700500 this->interfaces.push_back(RPC_RESULT_HANDLER_TYPE);
501
502 // methodId
503 this->methodId = new Variable(INT_TYPE, "methodId");
504 this->elements.push_back(new Field(PRIVATE, this->methodId));
505 this->callback = new Variable(OBJECT_TYPE, "callback");
506 this->elements.push_back(new Field(PRIVATE, this->callback));
507
508 // methods
509 generate_ctor();
510 generate_onResult();
511}
512
513ResultDispatcherClass::~ResultDispatcherClass()
514{
515}
516
517void
518ResultDispatcherClass::generate_ctor()
519{
520 Variable* methodIdParam = new Variable(INT_TYPE, "methId");
521 Variable* callbackParam = new Variable(OBJECT_TYPE, "cbObj");
522 Method* ctor = new Method;
523 ctor->modifiers = PUBLIC;
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700524 ctor->name = class_name_leaf(this->type->Name());
Joe Onorato7add83b2011-08-30 17:24:17 -0700525 ctor->statements = new StatementBlock;
526 ctor->parameters.push_back(methodIdParam);
527 ctor->parameters.push_back(callbackParam);
528 this->elements.push_back(ctor);
529
530 ctor->statements->Add(new Assignment(this->methodId, methodIdParam));
531 ctor->statements->Add(new Assignment(this->callback, callbackParam));
532}
533
534void
535ResultDispatcherClass::generate_onResult()
536{
537 this->onResultMethod = new Method;
538 this->onResultMethod->modifiers = PUBLIC;
539 this->onResultMethod->returnType = VOID_TYPE;
540 this->onResultMethod->returnTypeDimension = 0;
541 this->onResultMethod->name = "onResult";
542 this->onResultMethod->statements = new StatementBlock;
543 this->elements.push_back(this->onResultMethod);
544
545 this->resultParam = new Variable(BYTE_TYPE, "result", 1);
546 this->onResultMethod->parameters.push_back(this->resultParam);
547
548 this->methodSwitch = new SwitchStatement(this->methodId);
549 this->onResultMethod->statements->Add(this->methodSwitch);
550}
551
552void
553ResultDispatcherClass::AddMethod(int index, const string& name, Method** method, Variable** param)
554{
555 Method* m = new Method;
556 m->modifiers = PUBLIC;
557 m->returnType = VOID_TYPE;
558 m->returnTypeDimension = 0;
559 m->name = name;
560 m->statements = new StatementBlock;
561 *param = new Variable(BYTE_TYPE, "result", 1);
562 m->parameters.push_back(*param);
563 this->elements.push_back(m);
564 *method = m;
565
566 Case* c = new Case(format_int(index));
567 c->statements->Add(new MethodCall(new LiteralExpression("this"), name, 1, this->resultParam));
Joe Onoratoa1426e62011-09-02 15:28:36 -0700568 c->statements->Add(new Break());
Joe Onorato7add83b2011-08-30 17:24:17 -0700569
570 this->methodSwitch->cases.push_back(c);
571}
572
573// =================================================
574static void
575generate_new_array(Type* t, StatementBlock* addTo, Variable* v, Variable* from)
576{
577 fprintf(stderr, "aidl: implement generate_new_array %s:%d\n", __FILE__, __LINE__);
578 exit(1);
579}
580
581static void
582generate_create_from_data(Type* t, StatementBlock* addTo, const string& key, Variable* v,
583 Variable* data, Variable** cl)
584{
585 Expression* k = new StringLiteralExpression(key);
586 if (v->dimension == 0) {
587 t->CreateFromRpcData(addTo, k, v, data, cl);
588 }
589 if (v->dimension == 1) {
590 //t->ReadArrayFromRpcData(addTo, v, data, cl);
591 fprintf(stderr, "aidl: implement generate_create_from_data for arrays%s:%d\n",
592 __FILE__, __LINE__);
593 }
594}
595
596static void
597generate_write_to_data(Type* t, StatementBlock* addTo, Expression* k, Variable* v, Variable* data)
598{
599 if (v->dimension == 0) {
600 t->WriteToRpcData(addTo, k, v, data, 0);
601 }
602 if (v->dimension == 1) {
603 //t->WriteArrayToParcel(addTo, v, data);
604 fprintf(stderr, "aidl: implement generate_write_to_data for arrays%s:%d\n",
605 __FILE__, __LINE__);
606 }
607}
608
609// =================================================
Joe Onorato7add83b2011-08-30 17:24:17 -0700610static Type*
611generate_results_method(const method_type* method, RpcProxyClass* proxyClass)
612{
613 arg_type* arg;
614
615 string resultsMethodName = results_method_name(method->name.data);
616 Type* resultsInterfaceType = new Type(results_class_name(method->name.data),
Joe Onorato0ca2a36d2011-09-15 21:31:15 -0700617 Type::GENERATED, false, false, false);
Joe Onorato7add83b2011-08-30 17:24:17 -0700618
619 if (!method->oneway) {
620 Class* resultsClass = new Class;
621 resultsClass->modifiers = STATIC | PUBLIC;
622 resultsClass->what = Class::INTERFACE;
623 resultsClass->type = resultsInterfaceType;
624
625 Method* resultMethod = new Method;
626 resultMethod->comment = gather_comments(method->comments_token->extra);
627 resultMethod->modifiers = PUBLIC;
628 resultMethod->returnType = VOID_TYPE;
629 resultMethod->returnTypeDimension = 0;
630 resultMethod->name = resultsMethodName;
631 if (0 != strcmp("void", method->type.type.data)) {
632 resultMethod->parameters.push_back(new Variable(NAMES.Search(method->type.type.data),
633 "_result", method->type.dimension));
634 }
635 arg = method->args;
636 while (arg != NULL) {
637 if (convert_direction(arg->direction.data) & OUT_PARAMETER) {
638 resultMethod->parameters.push_back(new Variable(
639 NAMES.Search(arg->type.type.data), arg->name.data,
640 arg->type.dimension));
641 }
642 arg = arg->next;
643 }
644 resultsClass->elements.push_back(resultMethod);
645
646 if (resultMethod->parameters.size() > 0) {
647 proxyClass->elements.push_back(resultsClass);
648 return resultsInterfaceType;
649 }
650 }
651 //delete resultsInterfaceType;
652 return NULL;
653}
654
655static void
656generate_proxy_method(const method_type* method, RpcProxyClass* proxyClass,
657 ResultDispatcherClass* resultsDispatcherClass, Type* resultsInterfaceType, int index)
658{
659 arg_type* arg;
660 Method* proxyMethod = new Method;
661 proxyMethod->comment = gather_comments(method->comments_token->extra);
662 proxyMethod->modifiers = PUBLIC;
663 proxyMethod->returnType = VOID_TYPE;
664 proxyMethod->returnTypeDimension = 0;
665 proxyMethod->name = method->name.data;
666 proxyMethod->statements = new StatementBlock;
667 proxyClass->elements.push_back(proxyMethod);
668
669 // The local variables
670 Variable* _data = new Variable(RPC_DATA_TYPE, "_data");
671 proxyMethod->statements->Add(new VariableDeclaration(_data, new NewExpression(RPC_DATA_TYPE)));
672
673 // Add the arguments
674 arg = method->args;
675 while (arg != NULL) {
676 if (convert_direction(arg->direction.data) & IN_PARAMETER) {
677 // Function signature
678 Type* t = NAMES.Search(arg->type.type.data);
679 Variable* v = new Variable(t, arg->name.data, arg->type.dimension);
680 proxyMethod->parameters.push_back(v);
681
682 // Input parameter marshalling
683 generate_write_to_data(t, proxyMethod->statements,
684 new StringLiteralExpression(arg->name.data), v, _data);
685 }
686 arg = arg->next;
687 }
688
689 // If there is a results interface for this class
690 Expression* resultParameter;
691 if (resultsInterfaceType != NULL) {
692 // Result interface parameter
693 Variable* resultListener = new Variable(resultsInterfaceType, "_result");
694 proxyMethod->parameters.push_back(resultListener);
695
696 // Add the results dispatcher callback
697 resultsDispatcherClass->needed = true;
698 resultParameter = new NewExpression(resultsDispatcherClass->type, 2,
699 new LiteralExpression(format_int(index)), resultListener);
700 } else {
701 resultParameter = NULL_VALUE;
702 }
703
704 // All proxy methods take an error parameter
705 Variable* errorListener = new Variable(RPC_ERROR_LISTENER_TYPE, "_errors");
706 proxyMethod->parameters.push_back(errorListener);
707
708 // Call the broker
Joe Onorato0d2a6b62011-10-09 21:51:46 -0700709 proxyMethod->statements->Add(new MethodCall(new FieldVariable(THIS_VALUE, "_broker"),
710 "sendRpc", 5,
Joe Onorato7add83b2011-08-30 17:24:17 -0700711 proxyClass->endpoint,
Joe Onorato0d2a6b62011-10-09 21:51:46 -0700712 new StringLiteralExpression(method->name.data),
Joe Onorato7add83b2011-08-30 17:24:17 -0700713 new MethodCall(_data, "serialize"),
714 resultParameter,
715 errorListener));
716}
717
718static void
719generate_result_dispatcher_method(const method_type* method,
720 ResultDispatcherClass* resultsDispatcherClass, Type* resultsInterfaceType, int index)
721{
722 arg_type* arg;
723 Method* dispatchMethod;
724 Variable* dispatchParam;
725 resultsDispatcherClass->AddMethod(index, method->name.data, &dispatchMethod, &dispatchParam);
726
727 Variable* classLoader = NULL;
728 Variable* resultData = new Variable(RPC_DATA_TYPE, "resultData");
729 dispatchMethod->statements->Add(new VariableDeclaration(resultData,
730 new NewExpression(RPC_DATA_TYPE, 1, dispatchParam)));
731
732 // The callback method itself
733 MethodCall* realCall = new MethodCall(
734 new Cast(resultsInterfaceType, new FieldVariable(THIS_VALUE, "callback")),
735 results_method_name(method->name.data));
736
737 // The return value
738 {
739 Type* t = NAMES.Search(method->type.type.data);
Joe Onorato6980de42011-11-17 16:13:49 -0800740 if (t != VOID_TYPE) {
741 Variable* rv = new Variable(t, "rv");
742 dispatchMethod->statements->Add(new VariableDeclaration(rv));
743 generate_create_from_data(t, dispatchMethod->statements, "_result", rv,
744 resultData, &classLoader);
745 realCall->arguments.push_back(rv);
746 }
Joe Onorato7add83b2011-08-30 17:24:17 -0700747 }
748
749 VariableFactory stubArgs("arg");
750 arg = method->args;
751 while (arg != NULL) {
752 if (convert_direction(arg->direction.data) & OUT_PARAMETER) {
753 // Unmarshall the results
754 Type* t = NAMES.Search(arg->type.type.data);
755 Variable* v = stubArgs.Get(t);
756 dispatchMethod->statements->Add(new VariableDeclaration(v));
757
758 generate_create_from_data(t, dispatchMethod->statements, arg->name.data, v,
759 resultData, &classLoader);
760
761 // Add the argument to the callback
762 realCall->arguments.push_back(v);
763 }
764 arg = arg->next;
765 }
766
767 // Call the callback method
768 dispatchMethod->statements->Add(realCall);
769}
770
771static void
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700772generate_regular_method(const method_type* method, RpcProxyClass* proxyClass,
Joe Onorato0d2a6b62011-10-09 21:51:46 -0700773 EndpointBaseClass* serviceBaseClass, ResultDispatcherClass* resultsDispatcherClass,
Joe Onorato7add83b2011-08-30 17:24:17 -0700774 int index)
775{
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700776 arg_type* arg;
777
778 // == the callback interface for results ================================
Joe Onorato7add83b2011-08-30 17:24:17 -0700779 // the service base class
780 Type* resultsInterfaceType = generate_results_method(method, proxyClass);
781
782 // == the method in the proxy class =====================================
783 generate_proxy_method(method, proxyClass, resultsDispatcherClass, resultsInterfaceType, index);
784
785 // == the method in the result dispatcher class =========================
786 if (resultsInterfaceType != NULL) {
787 generate_result_dispatcher_method(method, resultsDispatcherClass, resultsInterfaceType,
788 index);
789 }
790
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700791 // == The abstract method that the service developers implement ==========
792 Method* decl = new Method;
793 decl->comment = gather_comments(method->comments_token->extra);
794 decl->modifiers = PUBLIC | ABSTRACT;
795 decl->returnType = NAMES.Search(method->type.type.data);
796 decl->returnTypeDimension = method->type.dimension;
797 decl->name = method->name.data;
798 arg = method->args;
799 while (arg != NULL) {
800 decl->parameters.push_back(new Variable(
801 NAMES.Search(arg->type.type.data), arg->name.data,
802 arg->type.dimension));
803 arg = arg->next;
804 }
Manuel Roman2c5eb262011-10-06 10:28:35 -0700805
806 // Add the default RpcContext param to all methods
807 decl->parameters.push_back(new Variable(RPC_CONTEXT_TYPE, "context", 0));
808
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700809 serviceBaseClass->elements.push_back(decl);
810
811
Joe Onorato7add83b2011-08-30 17:24:17 -0700812 // == the dispatch method in the service base class ======================
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700813 serviceBaseClass->AddMethod(method);
814}
815
816static void
817generate_event_method(const method_type* method, RpcProxyClass* proxyClass,
Joe Onorato0d2a6b62011-10-09 21:51:46 -0700818 EndpointBaseClass* serviceBaseClass, ListenerClass* listenerClass,
Joe Onoratoc7ec1dc2011-10-12 23:22:42 -0700819 EventListenerClass* presenterClass, int index)
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700820{
821 arg_type* arg;
822 listenerClass->needed = true;
823
824 // == the push method in the service base class =========================
825 Method* push = new Method;
826 push->modifiers = PUBLIC;
827 push->name = push_method_name(method->name.data);
828 push->statements = new StatementBlock;
829 push->returnType = VOID_TYPE;
830 serviceBaseClass->elements.push_back(push);
831
832 // The local variables
833 Variable* _data = new Variable(RPC_DATA_TYPE, "_data");
834 push->statements->Add(new VariableDeclaration(_data, new NewExpression(RPC_DATA_TYPE)));
835
836 // Add the arguments
837 arg = method->args;
838 while (arg != NULL) {
839 // Function signature
840 Type* t = NAMES.Search(arg->type.type.data);
841 Variable* v = new Variable(t, arg->name.data, arg->type.dimension);
842 push->parameters.push_back(v);
843
844 // Input parameter marshalling
845 generate_write_to_data(t, push->statements,
846 new StringLiteralExpression(arg->name.data), v, _data);
847
848 arg = arg->next;
849 }
850
851 // Send the notifications
852 push->statements->Add(new MethodCall("pushEvent", 2,
853 new StringLiteralExpression(method->name.data),
854 new MethodCall(_data, "serialize")));
855
856 // == the event callback dispatcher method ====================================
857 presenterClass->AddMethod(method);
858
859 // == the event method in the listener base class =====================
860 Method* event = new Method;
861 event->modifiers = PUBLIC;
862 event->name = method->name.data;
863 event->statements = new StatementBlock;
864 event->returnType = VOID_TYPE;
865 listenerClass->elements.push_back(event);
866 arg = method->args;
867 while (arg != NULL) {
868 event->parameters.push_back(new Variable(
869 NAMES.Search(arg->type.type.data), arg->name.data,
870 arg->type.dimension));
871 arg = arg->next;
872 }
Manuel Roman2c5eb262011-10-06 10:28:35 -0700873
874 // Add a final parameter: RpcContext. Contains data about
875 // incoming request (e.g., certificate)
876 event->parameters.push_back(new Variable(RPC_CONTEXT_TYPE, "context", 0));
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700877}
878
879static void
880generate_listener_methods(RpcProxyClass* proxyClass, Type* presenterType, Type* listenerType)
881{
882 // AndroidAtHomePresenter _presenter;
Joe Onoratoc7ec1dc2011-10-12 23:22:42 -0700883 // void startListening(Listener listener) {
884 // stopListening();
885 // _presenter = new Presenter(_broker, listener);
886 // _presenter.startListening(_endpoint);
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700887 // }
Joe Onoratoc7ec1dc2011-10-12 23:22:42 -0700888 // void stopListening() {
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700889 // if (_presenter != null) {
Joe Onoratoc7ec1dc2011-10-12 23:22:42 -0700890 // _presenter.stopListening();
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700891 // }
892 // }
893
894 Variable* _presenter = new Variable(presenterType, "_presenter");
895 proxyClass->elements.push_back(new Field(PRIVATE, _presenter));
896
897 Variable* listener = new Variable(listenerType, "listener");
898
Joe Onoratoc7ec1dc2011-10-12 23:22:42 -0700899 Method* startListeningMethod = new Method;
900 startListeningMethod->modifiers = PUBLIC;
901 startListeningMethod->returnType = VOID_TYPE;
902 startListeningMethod->name = "startListening";
903 startListeningMethod->statements = new StatementBlock;
904 startListeningMethod->parameters.push_back(listener);
905 proxyClass->elements.push_back(startListeningMethod);
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700906
Joe Onoratoc7ec1dc2011-10-12 23:22:42 -0700907 startListeningMethod->statements->Add(new MethodCall(THIS_VALUE, "stopListening"));
908 startListeningMethod->statements->Add(new Assignment(_presenter,
909 new NewExpression(presenterType, 2, proxyClass->broker, listener)));
910 startListeningMethod->statements->Add(new MethodCall(_presenter,
911 "startListening", 1, proxyClass->endpoint));
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700912
Joe Onoratoc7ec1dc2011-10-12 23:22:42 -0700913 Method* stopListeningMethod = new Method;
914 stopListeningMethod->modifiers = PUBLIC;
915 stopListeningMethod->returnType = VOID_TYPE;
916 stopListeningMethod->name = "stopListening";
917 stopListeningMethod->statements = new StatementBlock;
918 proxyClass->elements.push_back(stopListeningMethod);
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700919
920 IfStatement* ifst = new IfStatement;
921 ifst->expression = new Comparison(_presenter, "!=", NULL_VALUE);
Joe Onoratoc7ec1dc2011-10-12 23:22:42 -0700922 stopListeningMethod->statements->Add(ifst);
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700923
Joe Onoratoc7ec1dc2011-10-12 23:22:42 -0700924 ifst->statements->Add(new MethodCall(_presenter, "stopListening"));
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700925 ifst->statements->Add(new Assignment(_presenter, NULL_VALUE));
Joe Onorato7add83b2011-08-30 17:24:17 -0700926}
927
928Class*
929generate_rpc_interface_class(const interface_type* iface)
930{
931 // the proxy class
932 InterfaceType* interfaceType = static_cast<InterfaceType*>(
933 NAMES.Find(iface->package, iface->name.data));
934 RpcProxyClass* proxy = new RpcProxyClass(iface, interfaceType);
935
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700936 // the listener class
937 ListenerClass* listener = new ListenerClass(iface);
938
939 // the presenter class
Joe Onoratoc7ec1dc2011-10-12 23:22:42 -0700940 EventListenerClass* presenter = new EventListenerClass(iface, listener->type);
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700941
Joe Onorato7add83b2011-08-30 17:24:17 -0700942 // the service base class
Joe Onorato0d2a6b62011-10-09 21:51:46 -0700943 EndpointBaseClass* base = new EndpointBaseClass(iface);
Joe Onorato7add83b2011-08-30 17:24:17 -0700944 proxy->elements.push_back(base);
945
946 // the result dispatcher
947 ResultDispatcherClass* results = new ResultDispatcherClass();
948
949 // all the declared methods of the proxy
950 int index = 0;
951 interface_item_type* item = iface->interface_items;
952 while (item != NULL) {
953 if (item->item_type == METHOD_TYPE) {
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700954 if (NAMES.Search(((method_type*)item)->type.type.data) == EVENT_FAKE_TYPE) {
955 generate_event_method((method_type*)item, proxy, base, listener, presenter, index);
956 } else {
957 generate_regular_method((method_type*)item, proxy, base, results, index);
958 }
Joe Onorato7add83b2011-08-30 17:24:17 -0700959 }
960 item = item->next;
961 index++;
962 }
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700963 presenter->DoneWithMethods();
Joe Onorato7add83b2011-08-30 17:24:17 -0700964 base->DoneWithMethods();
965
966 // only add this if there are methods with results / out parameters
967 if (results->needed) {
968 proxy->elements.push_back(results);
969 }
Joe Onoratoe415ecb2011-09-23 15:17:52 -0700970 if (listener->needed) {
971 proxy->elements.push_back(listener);
972 proxy->elements.push_back(presenter);
973 generate_listener_methods(proxy, presenter->type, listener->type);
974 }
Joe Onorato7add83b2011-08-30 17:24:17 -0700975
976 return proxy;
977}