1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
|
/* RPC call and callback templates
Copyright (C) 2014 Free Software Foundation, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#ifndef CC1_PLUGIN_RPC_HH
#define CC1_PLUGIN_RPC_HH
#include "status.hh"
#include "marshall.hh"
#include "connection.hh"
namespace cc1_plugin
{
// The plugin API may contain some "const" method parameters.
// However, when unmarshalling we cannot unmarshall into a const
// object; and furthermore we want to be able to deallocate pointers
// when finished with them. This wrapper class lets us properly
// remove the "const" and handle deallocation from pointer types.
template<typename T>
class argument_wrapper
{
public:
argument_wrapper () { }
~argument_wrapper () { }
operator T () const { return m_object; }
status unmarshall (connection *conn)
{
return ::cc1_plugin::unmarshall (conn, &m_object);
}
private:
T m_object;
// No copying or assignment allowed.
argument_wrapper (const argument_wrapper &);
argument_wrapper &operator= (const argument_wrapper &);
};
// Specialization for any kind of pointer. This is declared but not
// defined to avoid bugs if a new pointer type is introduced into
// the API. Instead you will just get a compilation error.
template<typename T>
class argument_wrapper<const T *>;
// Specialization for string types.
template<>
class argument_wrapper<const char *>
{
public:
argument_wrapper () : m_object (NULL) { }
~argument_wrapper ()
{
delete[] m_object;
}
operator const char * () const
{
return m_object;
}
status unmarshall (connection *conn)
{
return ::cc1_plugin::unmarshall (conn, &m_object);
}
private:
char *m_object;
// No copying or assignment allowed.
argument_wrapper (const argument_wrapper &);
argument_wrapper &operator= (const argument_wrapper &);
};
// Specialization for gcc_type_array.
template<>
class argument_wrapper<const gcc_type_array *>
{
public:
argument_wrapper () : m_object (NULL) { }
~argument_wrapper ()
{
// It would be nicer if gcc_type_array could have a destructor.
// But, it is in code shared with gdb and cannot.
if (m_object != NULL)
delete[] m_object->elements;
delete m_object;
}
operator const gcc_type_array * () const
{
return m_object;
}
status unmarshall (connection *conn)
{
return ::cc1_plugin::unmarshall (conn, &m_object);
}
private:
gcc_type_array *m_object;
// No copying or assignment allowed.
argument_wrapper (const argument_wrapper &);
argument_wrapper &operator= (const argument_wrapper &);
};
// There are two kinds of template functions here: "call" and
// "callback". They are each repeated multiple times to handle
// different numbers of arguments. (This would be improved with
// C++11, though applying a call is still tricky until C++14 can be
// used.)
// The "call" template is used for making a remote procedure call.
// It starts a query ('Q') packet, marshalls its arguments, waits
// for a result, and finally reads and returns the result via an
// "out" parameter.
// The "callback" template is used when receiving a remote procedure
// call. This template function is suitable for use with the
// "callbacks" and "connection" classes. It decodes incoming
// arguments, passes them to the wrapped function, and finally
// marshalls a reply packet.
template<typename R>
status
call (connection *conn, const char *method, R *result)
{
if (!conn->send ('Q'))
return FAIL;
if (!marshall (conn, method))
return FAIL;
if (!marshall (conn, 0))
return FAIL;
if (!conn->wait_for_result ())
return FAIL;
if (!unmarshall (conn, result))
return FAIL;
return OK;
}
template<typename R, R (*func) (connection *)>
status
callback (connection *conn)
{
R result;
if (!unmarshall_check (conn, 0))
return FAIL;
result = func (conn);
if (!conn->send ('R'))
return FAIL;
return marshall (conn, result);
}
template<typename R, typename A>
status
call (connection *conn, const char *method, R *result, A arg)
{
if (!conn->send ('Q'))
return FAIL;
if (!marshall (conn, method))
return FAIL;
if (!marshall (conn, 1))
return FAIL;
if (!marshall (conn, arg))
return FAIL;
if (!conn->wait_for_result ())
return FAIL;
if (!unmarshall (conn, result))
return FAIL;
return OK;
}
template<typename R, typename A, R (*func) (connection *, A)>
status
callback (connection *conn)
{
argument_wrapper<A> arg;
R result;
if (!unmarshall_check (conn, 1))
return FAIL;
if (!arg.unmarshall (conn))
return FAIL;
result = func (conn, arg);
if (!conn->send ('R'))
return FAIL;
return marshall (conn, result);
}
template<typename R, typename A1, typename A2>
status
call (connection *conn, const char *method, R *result, A1 arg1, A2 arg2)
{
if (!conn->send ('Q'))
return FAIL;
if (!marshall (conn, method))
return FAIL;
if (!marshall (conn, 2))
return FAIL;
if (!marshall (conn, arg1))
return FAIL;
if (!marshall (conn, arg2))
return FAIL;
if (!conn->wait_for_result ())
return FAIL;
if (!unmarshall (conn, result))
return FAIL;
return OK;
}
template<typename R, typename A1, typename A2, R (*func) (connection *,
A1, A2)>
status
callback (connection *conn)
{
argument_wrapper<A1> arg1;
argument_wrapper<A2> arg2;
R result;
if (!unmarshall_check (conn, 2))
return FAIL;
if (!arg1.unmarshall (conn))
return FAIL;
if (!arg2.unmarshall (conn))
return FAIL;
result = func (conn, arg1, arg2);
if (!conn->send ('R'))
return FAIL;
return marshall (conn, result);
}
template<typename R, typename A1, typename A2, typename A3>
status
call (connection *conn, const char *method, R *result, A1 arg1, A2 arg2,
A3 arg3)
{
if (!conn->send ('Q'))
return FAIL;
if (!marshall (conn, method))
return FAIL;
if (!marshall (conn, 3))
return FAIL;
if (!marshall (conn, arg1))
return FAIL;
if (!marshall (conn, arg2))
return FAIL;
if (!marshall (conn, arg3))
return FAIL;
if (!conn->wait_for_result ())
return FAIL;
if (!unmarshall (conn, result))
return FAIL;
return OK;
}
template<typename R, typename A1, typename A2, typename A3,
R (*func) (connection *, A1, A2, A3)>
status
callback (connection *conn)
{
argument_wrapper<A1> arg1;
argument_wrapper<A2> arg2;
argument_wrapper<A3> arg3;
R result;
if (!unmarshall_check (conn, 3))
return FAIL;
if (!arg1.unmarshall (conn))
return FAIL;
if (!arg2.unmarshall (conn))
return FAIL;
if (!arg3.unmarshall (conn))
return FAIL;
result = func (conn, arg1, arg2, arg3);
if (!conn->send ('R'))
return FAIL;
return marshall (conn, result);
}
template<typename R, typename A1, typename A2, typename A3, typename A4>
status
call (connection *conn, const char *method, R *result, A1 arg1, A2 arg2,
A3 arg3, A4 arg4)
{
if (!conn->send ('Q'))
return FAIL;
if (!marshall (conn, method))
return FAIL;
if (!marshall (conn, 4))
return FAIL;
if (!marshall (conn, arg1))
return FAIL;
if (!marshall (conn, arg2))
return FAIL;
if (!marshall (conn, arg3))
return FAIL;
if (!marshall (conn, arg4))
return FAIL;
if (!conn->wait_for_result ())
return FAIL;
if (!unmarshall (conn, result))
return FAIL;
return OK;
}
template<typename R, typename A1, typename A2, typename A3, typename A4,
R (*func) (connection *, A1, A2, A3, A4)>
status
callback (connection *conn)
{
argument_wrapper<A1> arg1;
argument_wrapper<A2> arg2;
argument_wrapper<A3> arg3;
argument_wrapper<A4> arg4;
R result;
if (!unmarshall_check (conn, 4))
return FAIL;
if (!arg1.unmarshall (conn))
return FAIL;
if (!arg2.unmarshall (conn))
return FAIL;
if (!arg3.unmarshall (conn))
return FAIL;
if (!arg4.unmarshall (conn))
return FAIL;
result = func (conn, arg1, arg2, arg3, arg4);
if (!conn->send ('R'))
return FAIL;
return marshall (conn, result);
}
template<typename R, typename A1, typename A2, typename A3, typename A4,
typename A5>
status
call (connection *conn, const char *method, R *result, A1 arg1, A2 arg2,
A3 arg3, A4 arg4, A5 arg5)
{
if (!conn->send ('Q'))
return FAIL;
if (!marshall (conn, method))
return FAIL;
if (!marshall (conn, 5))
return FAIL;
if (!marshall (conn, arg1))
return FAIL;
if (!marshall (conn, arg2))
return FAIL;
if (!marshall (conn, arg3))
return FAIL;
if (!marshall (conn, arg4))
return FAIL;
if (!marshall (conn, arg5))
return FAIL;
if (!conn->wait_for_result ())
return FAIL;
if (!unmarshall (conn, result))
return FAIL;
return OK;
}
template<typename R, typename A1, typename A2, typename A3, typename A4,
typename A5, R (*func) (connection *, A1, A2, A3, A4, A5)>
status
callback (connection *conn)
{
argument_wrapper<A1> arg1;
argument_wrapper<A2> arg2;
argument_wrapper<A3> arg3;
argument_wrapper<A4> arg4;
argument_wrapper<A5> arg5;
R result;
if (!unmarshall_check (conn, 5))
return FAIL;
if (!arg1.unmarshall (conn))
return FAIL;
if (!arg2.unmarshall (conn))
return FAIL;
if (!arg3.unmarshall (conn))
return FAIL;
if (!arg4.unmarshall (conn))
return FAIL;
if (!arg5.unmarshall (conn))
return FAIL;
result = func (conn, arg1, arg2, arg3, arg4, arg5);
if (!conn->send ('R'))
return FAIL;
return marshall (conn, result);
}
template<typename R, typename A1, typename A2, typename A3, typename A4,
typename A5, typename A6, typename A7>
status
call (connection *conn, const char *method, R *result, A1 arg1, A2 arg2,
A3 arg3, A4 arg4, A5 arg5, A6 arg6, A7 arg7)
{
if (!conn->send ('Q'))
return FAIL;
if (!marshall (conn, method))
return FAIL;
if (!marshall (conn, 7))
return FAIL;
if (!marshall (conn, arg1))
return FAIL;
if (!marshall (conn, arg2))
return FAIL;
if (!marshall (conn, arg3))
return FAIL;
if (!marshall (conn, arg4))
return FAIL;
if (!marshall (conn, arg5))
return FAIL;
if (!marshall (conn, arg6))
return FAIL;
if (!marshall (conn, arg7))
return FAIL;
if (!conn->wait_for_result ())
return FAIL;
if (!unmarshall (conn, result))
return FAIL;
return OK;
}
template<typename R, typename A1, typename A2, typename A3, typename A4,
typename A5, typename A6, typename A7,
R (*func) (connection *, A1, A2, A3, A4, A5, A6, A7)>
status
callback (connection *conn)
{
argument_wrapper<A1> arg1;
argument_wrapper<A2> arg2;
argument_wrapper<A3> arg3;
argument_wrapper<A4> arg4;
argument_wrapper<A5> arg5;
argument_wrapper<A6> arg6;
argument_wrapper<A7> arg7;
R result;
if (!unmarshall_check (conn, 7))
return FAIL;
if (!arg1.unmarshall (conn))
return FAIL;
if (!arg2.unmarshall (conn))
return FAIL;
if (!arg3.unmarshall (conn))
return FAIL;
if (!arg4.unmarshall (conn))
return FAIL;
if (!arg5.unmarshall (conn))
return FAIL;
if (!arg6.unmarshall (conn))
return FAIL;
if (!arg7.unmarshall (conn))
return FAIL;
result = func (conn, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
if (!conn->send ('R'))
return FAIL;
return marshall (conn, result);
}
};
#endif // CC1_PLUGIN_RPC_HH
|