Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/tools/widl/proxy.c
4389 views
1
/*
2
* IDL Compiler
3
*
4
* Copyright 2002 Ove Kaaven
5
* Copyright 2004 Mike McCormack
6
*
7
* This library is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public
9
* License as published by the Free Software Foundation; either
10
* version 2.1 of the License, or (at your option) any later version.
11
*
12
* This library is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
16
*
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this library; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20
*/
21
22
#include "config.h"
23
24
#include <stdio.h>
25
#include <stdlib.h>
26
#include <string.h>
27
#include <ctype.h>
28
29
#include "widl.h"
30
#include "utils.h"
31
#include "parser.h"
32
#include "header.h"
33
#include "typegen.h"
34
#include "expr.h"
35
36
static FILE* proxy;
37
static int indent = 0;
38
39
static void print_proxy( const char *format, ... ) __attribute__((format (printf, 1, 2)));
40
static void print_proxy( const char *format, ... )
41
{
42
va_list va;
43
va_start( va, format );
44
print( proxy, indent, format, va );
45
va_end( va );
46
}
47
48
static void write_stubdescproto(void)
49
{
50
print_proxy( "static const MIDL_STUB_DESC Object_StubDesc;\n");
51
print_proxy( "\n");
52
}
53
54
static void write_stubdesc(int expr_eval_routines)
55
{
56
print_proxy( "static const MIDL_STUB_DESC Object_StubDesc =\n{\n");
57
indent++;
58
print_proxy( "0,\n");
59
print_proxy( "NdrOleAllocate,\n");
60
print_proxy( "NdrOleFree,\n");
61
print_proxy( "{0}, 0, 0, %s, 0,\n", expr_eval_routines ? "ExprEvalRoutines" : "0");
62
print_proxy( "__MIDL_TypeFormatString.Format,\n");
63
print_proxy( "1, /* -error bounds_check flag */\n");
64
print_proxy( "0x%x, /* Ndr library version */\n", interpreted_mode ? 0x50002 : 0x10001);
65
print_proxy( "0,\n");
66
print_proxy( "0x50200ca, /* MIDL Version 5.2.202 */\n");
67
print_proxy( "0,\n");
68
print_proxy("%s,\n", list_empty(&user_type_list) ? "0" : "UserMarshalRoutines");
69
print_proxy( "0, /* notify & notify_flag routine table */\n");
70
print_proxy( "1, /* Flags */\n");
71
print_proxy( "0, /* Reserved3 */\n");
72
print_proxy( "0, /* Reserved4 */\n");
73
print_proxy( "0 /* Reserved5 */\n");
74
indent--;
75
print_proxy( "};\n");
76
print_proxy( "\n");
77
}
78
79
static void init_proxy(const statement_list_t *stmts)
80
{
81
if (proxy) return;
82
if(!(proxy = fopen(proxy_name, "w")))
83
error("Could not open %s for output\n", proxy_name);
84
print_proxy( "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name);
85
print_proxy( "\n");
86
print_proxy( "#define __midl_proxy\n");
87
print_proxy( "#include \"objbase.h\"\n");
88
print_proxy( "\n");
89
}
90
91
static void clear_output_vars( const var_list_t *args )
92
{
93
const var_t *arg;
94
95
if (!args) return;
96
LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
97
{
98
if (is_attr(arg->attrs, ATTR_IN)) continue;
99
if (!is_attr(arg->attrs, ATTR_OUT)) continue;
100
if (is_ptr(arg->declspec.type))
101
{
102
if (type_get_type(type_pointer_get_ref_type(arg->declspec.type)) == TYPE_BASIC) continue;
103
if (type_get_type(type_pointer_get_ref_type(arg->declspec.type)) == TYPE_ENUM) continue;
104
}
105
print_proxy( "if (%s) MIDL_memset( %s, 0, ", arg->name, arg->name );
106
if (is_array(arg->declspec.type) && type_array_has_conformance(arg->declspec.type))
107
{
108
write_expr( proxy, type_array_get_conformance(arg->declspec.type), 1, 1, NULL, NULL, "" );
109
fprintf( proxy, " * " );
110
}
111
fprintf( proxy, "sizeof( *%s ));\n", arg->name );
112
}
113
}
114
115
static int need_delegation(const type_t *iface)
116
{
117
const type_t *parent = type_iface_get_inherit( iface );
118
return parent && type_iface_get_inherit(parent) && (parent->ignore || is_local( parent->attrs ));
119
}
120
121
static int get_delegation_indirect(const type_t *iface, const type_t ** delegate_to)
122
{
123
const type_t * cur_iface;
124
for (cur_iface = iface; cur_iface != NULL; cur_iface = type_iface_get_inherit(cur_iface))
125
if (need_delegation(cur_iface))
126
{
127
if(delegate_to)
128
*delegate_to = type_iface_get_inherit(cur_iface);
129
return 1;
130
}
131
return 0;
132
}
133
134
static int need_delegation_indirect(const type_t *iface)
135
{
136
return get_delegation_indirect(iface, NULL);
137
}
138
139
static void free_variable( const var_t *arg, const char *local_var_prefix )
140
{
141
unsigned int type_offset = arg->typestring_offset;
142
type_t *type = arg->declspec.type;
143
144
write_parameter_conf_or_var_exprs(proxy, indent, local_var_prefix, PHASE_FREE, arg, FALSE);
145
146
switch (typegen_detect_type(type, arg->attrs, TDT_IGNORE_STRINGS))
147
{
148
case TGT_ENUM:
149
case TGT_BASIC:
150
break;
151
152
case TGT_STRUCT:
153
if (get_struct_fc(type) != FC_STRUCT)
154
print_proxy("/* FIXME: %s code for %s struct type 0x%x missing */\n", __FUNCTION__, arg->name, get_struct_fc(type) );
155
break;
156
157
case TGT_IFACE_POINTER:
158
case TGT_POINTER:
159
case TGT_ARRAY:
160
print_proxy( "NdrClearOutParameters( &__frame->_StubMsg, ");
161
fprintf(proxy, "&__MIDL_TypeFormatString.Format[%u], ", type_offset );
162
fprintf(proxy, "(void *)%s );\n", arg->name );
163
break;
164
165
default:
166
print_proxy("/* FIXME: %s code for %s type %d missing */\n", __FUNCTION__, arg->name, type_get_type(type) );
167
}
168
}
169
170
static void proxy_free_variables( var_list_t *args, const char *local_var_prefix )
171
{
172
const var_t *arg;
173
174
if (!args) return;
175
LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
176
if (is_attr(arg->attrs, ATTR_OUT))
177
{
178
free_variable( arg, local_var_prefix );
179
fprintf(proxy, "\n");
180
}
181
}
182
183
static void gen_proxy(type_t *iface, const var_t *func, int idx,
184
unsigned int proc_offset)
185
{
186
var_t *retval = type_function_get_retval(func->declspec.type);
187
int has_ret = !is_void(retval->declspec.type);
188
int has_full_pointer = is_full_pointer_function(func);
189
const char *callconv = get_attrp(func->declspec.type->attrs, ATTR_CALLCONV);
190
const var_list_t *args = type_function_get_args(func->declspec.type);
191
if (!callconv) callconv = "STDMETHODCALLTYPE";
192
193
indent = 0;
194
if (is_interpreted_func( iface, func ))
195
{
196
if (!is_callas( func->attrs )) return;
197
write_type_decl_left(proxy, &retval->declspec);
198
print_proxy( " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
199
write_args(proxy, args, iface->name, 1, TRUE, NAME_DEFAULT);
200
print_proxy( ")\n");
201
write_client_call_routine( proxy, iface, func, "Object", proc_offset );
202
return;
203
}
204
print_proxy( "static void __finally_%s_%s_Proxy( struct __proxy_frame *__frame )\n",
205
iface->name, get_name(func) );
206
print_proxy( "{\n");
207
indent++;
208
if (has_full_pointer) write_full_pointer_free(proxy, indent, func);
209
print_proxy( "NdrProxyFreeBuffer( __frame->This, &__frame->_StubMsg );\n" );
210
indent--;
211
print_proxy( "}\n");
212
print_proxy( "\n");
213
214
write_type_decl_left(proxy, &retval->declspec);
215
print_proxy( " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
216
write_args(proxy, args, iface->name, 1, TRUE, NAME_DEFAULT);
217
print_proxy( ")\n");
218
print_proxy( "{\n");
219
indent ++;
220
print_proxy( "struct __proxy_frame __f, * const __frame = &__f;\n" );
221
/* local variables */
222
if (has_ret) {
223
print_proxy( "%s", "" );
224
write_type_decl(proxy, &retval->declspec, retval->name);
225
fprintf( proxy, ";\n" );
226
}
227
print_proxy( "RPC_MESSAGE _RpcMessage;\n" );
228
if (has_ret) {
229
if (decl_indirect(retval->declspec.type))
230
print_proxy("void *_p_%s = &%s;\n", retval->name, retval->name);
231
}
232
print_proxy( "\n");
233
234
print_proxy( "RpcExceptionInit( __proxy_filter, __finally_%s_%s_Proxy );\n", iface->name, get_name(func) );
235
print_proxy( "__frame->This = This;\n" );
236
237
if (has_full_pointer)
238
write_full_pointer_init(proxy, indent, func, FALSE);
239
240
/* FIXME: trace */
241
clear_output_vars( type_function_get_args(func->declspec.type) );
242
243
print_proxy( "RpcTryExcept\n" );
244
print_proxy( "{\n" );
245
indent++;
246
print_proxy( "NdrProxyInitialize(This, &_RpcMessage, &__frame->_StubMsg, &Object_StubDesc, %d);\n", idx);
247
write_pointer_checks( proxy, indent, func );
248
249
print_proxy( "RpcTryFinally\n" );
250
print_proxy( "{\n" );
251
indent++;
252
253
write_remoting_arguments(proxy, indent, func, "", PASS_IN, PHASE_BUFFERSIZE);
254
255
print_proxy( "NdrProxyGetBuffer(This, &__frame->_StubMsg);\n" );
256
257
write_remoting_arguments(proxy, indent, func, "", PASS_IN, PHASE_MARSHAL);
258
259
print_proxy( "NdrProxySendReceive(This, &__frame->_StubMsg);\n" );
260
fprintf(proxy, "\n");
261
print_proxy( "__frame->_StubMsg.BufferStart = _RpcMessage.Buffer;\n" );
262
print_proxy( "__frame->_StubMsg.BufferEnd = __frame->_StubMsg.BufferStart + _RpcMessage.BufferLength;\n\n" );
263
264
print_proxy("if ((_RpcMessage.DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
265
indent++;
266
print_proxy("NdrConvert( &__frame->_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset );
267
indent--;
268
fprintf(proxy, "\n");
269
270
write_remoting_arguments(proxy, indent, func, "", PASS_OUT, PHASE_UNMARSHAL);
271
272
if (has_ret)
273
{
274
if (decl_indirect(retval->declspec.type))
275
print_proxy("MIDL_memset(&%s, 0, sizeof(%s));\n", retval->name, retval->name);
276
else if (is_ptr(retval->declspec.type) || is_array(retval->declspec.type))
277
print_proxy("%s = 0;\n", retval->name);
278
write_remoting_arguments(proxy, indent, func, "", PASS_RETURN, PHASE_UNMARSHAL);
279
}
280
281
indent--;
282
print_proxy( "}\n");
283
print_proxy( "RpcFinally\n" );
284
print_proxy( "{\n" );
285
indent++;
286
print_proxy( "__finally_%s_%s_Proxy( __frame );\n", iface->name, get_name(func) );
287
indent--;
288
print_proxy( "}\n");
289
print_proxy( "RpcEndFinally\n" );
290
indent--;
291
print_proxy( "}\n" );
292
print_proxy( "RpcExcept(__frame->_StubMsg.dwStubPhase != PROXY_SENDRECEIVE)\n" );
293
print_proxy( "{\n" );
294
if (has_ret) {
295
indent++;
296
proxy_free_variables( type_function_get_args(func->declspec.type), "" );
297
print_proxy( "/* coverity[uninit_use_in_call:SUPPRESS] */\n" );
298
print_proxy( "_RetVal = NdrProxyErrorHandler(RpcExceptionCode());\n" );
299
indent--;
300
}
301
print_proxy( "}\n" );
302
print_proxy( "RpcEndExcept\n" );
303
304
if (has_ret) {
305
print_proxy( "return _RetVal;\n" );
306
}
307
indent--;
308
print_proxy( "}\n");
309
print_proxy( "\n");
310
}
311
312
static void gen_stub(type_t *iface, const var_t *func, const char *cas,
313
unsigned int proc_offset)
314
{
315
const var_t *arg;
316
int has_ret = !is_void(type_function_get_rettype(func->declspec.type));
317
int has_full_pointer = is_full_pointer_function(func);
318
319
if (is_interpreted_func( iface, func )) return;
320
321
indent = 0;
322
print_proxy( "struct __frame_%s_%s_Stub\n{\n", iface->name, get_name(func));
323
indent++;
324
print_proxy( "__DECL_EXCEPTION_FRAME\n" );
325
print_proxy( "MIDL_STUB_MESSAGE _StubMsg;\n");
326
print_proxy( "%s * _This;\n", iface->name );
327
declare_stub_args( proxy, indent, func );
328
indent--;
329
print_proxy( "};\n\n" );
330
331
print_proxy( "static void __finally_%s_%s_Stub(", iface->name, get_name(func) );
332
print_proxy( " struct __frame_%s_%s_Stub *__frame )\n{\n", iface->name, get_name(func) );
333
indent++;
334
write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_FREE);
335
if (has_full_pointer)
336
write_full_pointer_free(proxy, indent, func);
337
indent--;
338
print_proxy( "}\n\n" );
339
340
print_proxy( "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(func));
341
indent++;
342
print_proxy( "IRpcStubBuffer* This,\n");
343
print_proxy( "IRpcChannelBuffer *_pRpcChannelBuffer,\n");
344
print_proxy( "PRPC_MESSAGE _pRpcMessage,\n");
345
print_proxy( "DWORD* _pdwStubPhase)\n");
346
indent--;
347
print_proxy( "{\n");
348
indent++;
349
print_proxy( "struct __frame_%s_%s_Stub __f, * const __frame = &__f;\n\n",
350
iface->name, get_name(func) );
351
352
print_proxy("__frame->_This = (%s*)((CStdStubBuffer*)This)->pvServerObject;\n\n", iface->name);
353
354
/* FIXME: trace */
355
356
print_proxy("NdrStubInitialize(_pRpcMessage, &__frame->_StubMsg, &Object_StubDesc, _pRpcChannelBuffer);\n");
357
fprintf(proxy, "\n");
358
print_proxy( "RpcExceptionInit( 0, __finally_%s_%s_Stub );\n", iface->name, get_name(func) );
359
360
write_parameters_init(proxy, indent, func, "__frame->");
361
362
print_proxy("RpcTryFinally\n");
363
print_proxy("{\n");
364
indent++;
365
if (has_full_pointer)
366
write_full_pointer_init(proxy, indent, func, TRUE);
367
print_proxy("if ((_pRpcMessage->DataRepresentation & 0xffff) != NDR_LOCAL_DATA_REPRESENTATION)\n");
368
indent++;
369
print_proxy("NdrConvert( &__frame->_StubMsg, &__MIDL_ProcFormatString.Format[%u]);\n", proc_offset );
370
indent--;
371
fprintf(proxy, "\n");
372
373
write_remoting_arguments(proxy, indent, func, "__frame->", PASS_IN, PHASE_UNMARSHAL);
374
fprintf(proxy, "\n");
375
376
assign_stub_out_args( proxy, indent, func, "__frame->" );
377
378
print_proxy("*_pdwStubPhase = STUB_CALL_SERVER;\n");
379
fprintf(proxy, "\n");
380
print_proxy( "%s", has_ret ? "__frame->_RetVal = " : "" );
381
if (cas) fprintf(proxy, "%s_%s_Stub", iface->name, cas);
382
else fprintf(proxy, "__frame->_This->lpVtbl->%s", get_name(func));
383
fprintf(proxy, "(__frame->_This");
384
385
if (type_function_get_args(func->declspec.type))
386
{
387
LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), const var_t, entry )
388
fprintf(proxy, ", %s__frame->%s", is_array(arg->declspec.type) && !type_array_is_decl_as_ptr(arg->declspec.type) ? "*" :"" , arg->name);
389
}
390
fprintf(proxy, ");\n");
391
fprintf(proxy, "\n");
392
print_proxy("*_pdwStubPhase = STUB_MARSHAL;\n");
393
fprintf(proxy, "\n");
394
395
write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_BUFFERSIZE);
396
397
if (!is_void(type_function_get_rettype(func->declspec.type)))
398
write_remoting_arguments(proxy, indent, func, "__frame->", PASS_RETURN, PHASE_BUFFERSIZE);
399
400
print_proxy("NdrStubGetBuffer(This, _pRpcChannelBuffer, &__frame->_StubMsg);\n");
401
402
write_remoting_arguments(proxy, indent, func, "__frame->", PASS_OUT, PHASE_MARSHAL);
403
fprintf(proxy, "\n");
404
405
/* marshall the return value */
406
if (!is_void(type_function_get_rettype(func->declspec.type)))
407
write_remoting_arguments(proxy, indent, func, "__frame->", PASS_RETURN, PHASE_MARSHAL);
408
409
indent--;
410
print_proxy("}\n");
411
print_proxy("RpcFinally\n");
412
print_proxy("{\n");
413
indent++;
414
print_proxy( "__finally_%s_%s_Stub( __frame );\n", iface->name, get_name(func) );
415
indent--;
416
print_proxy("}\n");
417
print_proxy("RpcEndFinally\n");
418
419
print_proxy("_pRpcMessage->BufferLength = __frame->_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer;\n");
420
indent--;
421
422
print_proxy("}\n");
423
print_proxy("\n");
424
}
425
426
static void gen_stub_thunk( type_t *iface, const var_t *func, unsigned int proc_offset )
427
{
428
int has_ret = !is_void( type_function_get_rettype( func->declspec.type ));
429
const var_t *arg, *callas = is_callas( func->attrs );
430
const var_list_t *args = type_function_get_args( func->declspec.type );
431
432
indent = 0;
433
print_proxy( "void __RPC_API %s_%s_Thunk( PMIDL_STUB_MESSAGE pStubMsg )\n",
434
iface->name, get_name(func) );
435
print_proxy( "{\n");
436
indent++;
437
write_func_param_struct( proxy, iface, func->declspec.type,
438
"*pParamStruct = (struct _PARAM_STRUCT *)pStubMsg->StackTop", has_ret );
439
print_proxy( "%s%s_%s_Stub( pParamStruct->This",
440
has_ret ? "pParamStruct->_RetVal = " : "", iface->name, callas->name );
441
indent++;
442
if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
443
{
444
fprintf( proxy, ",\n%*spParamStruct->%s", 4 * indent, "", arg->name );
445
}
446
fprintf( proxy, " );\n" );
447
indent--;
448
indent--;
449
print_proxy( "}\n\n");
450
}
451
452
int count_methods(const type_t *iface)
453
{
454
const statement_t *stmt;
455
int count = 0;
456
457
if (type_iface_get_inherit(iface))
458
count = count_methods(type_iface_get_inherit(iface));
459
460
STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
461
const var_t *func = stmt->u.var;
462
if (!is_callas(func->attrs)) count++;
463
}
464
return count;
465
}
466
467
const statement_t *get_callas_source(const type_t *iface, const var_t *def)
468
{
469
const statement_t * source;
470
STATEMENTS_FOR_EACH_FUNC( source, type_iface_get_stmts(iface)) {
471
const var_t * cas = is_callas(source->u.var->attrs );
472
if (cas && !strcmp(def->name, cas->name))
473
return source;
474
}
475
return NULL;
476
}
477
478
static void write_proxy_procformatstring_offsets( const type_t *iface, int skip )
479
{
480
const statement_t *stmt;
481
482
if (type_iface_get_inherit(iface))
483
write_proxy_procformatstring_offsets( type_iface_get_inherit(iface), need_delegation(iface));
484
else
485
return;
486
487
STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
488
{
489
const var_t *func = stmt->u.var;
490
int missing = 0;
491
492
if (is_callas(func->attrs)) continue;
493
if (is_local(func->attrs))
494
{
495
const statement_t * callas_source = get_callas_source(iface, func);
496
if (!callas_source)
497
missing = 1;
498
else
499
func = callas_source->u.var;
500
}
501
if (skip || missing)
502
print_proxy( "(unsigned short)-1, /* %s::%s */\n", iface->name, get_name(func));
503
else
504
print_proxy( "%u, /* %s::%s */\n", func->procstring_offset, iface->name, get_name(func));
505
}
506
}
507
508
static int write_proxy_methods(type_t *iface, int skip)
509
{
510
const statement_t *stmt;
511
int i = 0;
512
513
if (type_iface_get_inherit(iface))
514
i = write_proxy_methods(type_iface_get_inherit(iface),
515
need_delegation(iface));
516
STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
517
const var_t *func = stmt->u.var;
518
if (!is_callas(func->attrs)) {
519
if (skip || (is_local(func->attrs) && !get_callas_source(iface, func)))
520
print_proxy( "0, /* %s::%s */\n", iface->name, get_name(func));
521
else if (is_interpreted_func( iface, func ) &&
522
!is_local( func->attrs ) &&
523
type_iface_get_inherit(iface))
524
print_proxy( "(void *)-1, /* %s::%s */\n", iface->name, get_name(func));
525
else
526
print_proxy( "%s_%s_Proxy,\n", iface->name, get_name(func));
527
i++;
528
}
529
}
530
return i;
531
}
532
533
static int write_stub_methods(type_t *iface, int skip)
534
{
535
const statement_t *stmt;
536
int i = 0;
537
538
if (type_iface_get_inherit(iface))
539
i = write_stub_methods(type_iface_get_inherit(iface), need_delegation(iface));
540
else
541
return i; /* skip IUnknown */
542
543
STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
544
const var_t *func = stmt->u.var;
545
if (!is_callas(func->attrs)) {
546
int missing = 0;
547
const char * fname = get_name(func);
548
if(is_local(func->attrs)) {
549
const statement_t * callas_source = get_callas_source(iface, func);
550
if(!callas_source)
551
missing = 1;
552
else
553
fname = get_name(callas_source->u.var);
554
}
555
if (i) fprintf(proxy,",\n");
556
if (skip || missing) print_proxy("STUB_FORWARDING_FUNCTION");
557
else if (is_interpreted_func( iface, func ))
558
print_proxy( "(PRPC_STUB_FUNCTION)%s", interpreted_mode ? "NdrStubCall2" : "NdrStubCall" );
559
else print_proxy( "%s_%s_Stub", iface->name, fname);
560
i++;
561
}
562
}
563
return i;
564
}
565
566
static void write_thunk_methods( type_t *iface, int skip )
567
{
568
const statement_t *stmt;
569
570
if (type_iface_get_inherit( iface ))
571
write_thunk_methods( type_iface_get_inherit(iface), need_delegation(iface) );
572
else
573
return; /* skip IUnknown */
574
575
STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
576
{
577
var_t *func = stmt->u.var;
578
const statement_t * callas_source = NULL;
579
580
if (is_callas(func->attrs)) continue;
581
if (is_local(func->attrs)) callas_source = get_callas_source(iface, func);
582
583
if (!skip && callas_source && is_interpreted_func( iface, func ))
584
print_proxy( "%s_%s_Thunk,\n", iface->name, get_name(callas_source->u.var) );
585
else
586
print_proxy( "0, /* %s::%s */\n", iface->name, get_name(func) );
587
}
588
}
589
590
static void write_proxy(type_t *iface, unsigned int *proc_offset)
591
{
592
int count;
593
const statement_t *stmt;
594
int first_func = 1;
595
int needs_stub_thunks = 0;
596
int needs_inline_stubs = need_inline_stubs( iface ) || need_delegation( iface );
597
598
STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
599
var_t *func = stmt->u.var;
600
if (first_func) {
601
fprintf(proxy, "/*****************************************************************************\n");
602
fprintf(proxy, " * %s interface\n", iface->name);
603
fprintf(proxy, " */\n");
604
first_func = 0;
605
}
606
if (!is_local(func->attrs)) {
607
const var_t *cas = is_callas(func->attrs);
608
const char *cname = cas ? cas->name : NULL;
609
int idx = func->func_idx;
610
if (cname) {
611
const statement_t *stmt2;
612
STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface)) {
613
const var_t *m = stmt2->u.var;
614
if (!strcmp(m->name, cname))
615
{
616
idx = m->func_idx;
617
break;
618
}
619
}
620
}
621
func->procstring_offset = *proc_offset;
622
gen_proxy(iface, func, idx, *proc_offset);
623
gen_stub(iface, func, cname, *proc_offset);
624
if (cas && is_interpreted_func( iface, func ))
625
{
626
needs_stub_thunks = 1;
627
gen_stub_thunk(iface, func, *proc_offset);
628
}
629
*proc_offset += get_size_procformatstring_func( iface, func );
630
}
631
}
632
633
count = count_methods(iface);
634
635
print_proxy( "static const unsigned short %s_FormatStringOffsetTable[] =\n", iface->name );
636
print_proxy( "{\n" );
637
indent++;
638
write_proxy_procformatstring_offsets( iface, 0 );
639
indent--;
640
print_proxy( "};\n\n" );
641
642
/* proxy info */
643
if (interpreted_mode)
644
{
645
print_proxy( "static const MIDL_STUBLESS_PROXY_INFO %s_ProxyInfo =\n", iface->name );
646
print_proxy( "{\n" );
647
indent++;
648
print_proxy( "&Object_StubDesc,\n" );
649
print_proxy( "__MIDL_ProcFormatString.Format,\n" );
650
print_proxy( "&%s_FormatStringOffsetTable[-3],\n", iface->name );
651
print_proxy( "0,\n" );
652
print_proxy( "0,\n" );
653
print_proxy( "0\n" );
654
indent--;
655
print_proxy( "};\n\n" );
656
}
657
658
/* proxy vtable */
659
print_proxy( "static %sCINTERFACE_PROXY_VTABLE(%d) _%sProxyVtbl =\n",
660
(interpreted_mode || need_delegation_indirect(iface)) ? "" : "const ",
661
count, iface->name);
662
print_proxy( "{\n");
663
indent++;
664
print_proxy( "{\n");
665
indent++;
666
if (interpreted_mode) print_proxy( "&%s_ProxyInfo,\n", iface->name );
667
print_proxy( "&IID_%s,\n", iface->name);
668
indent--;
669
print_proxy( "},\n");
670
print_proxy( "{\n");
671
indent++;
672
write_proxy_methods(iface, FALSE);
673
indent--;
674
print_proxy( "}\n");
675
indent--;
676
print_proxy( "};\n\n");
677
678
/* stub thunk table */
679
if (needs_stub_thunks)
680
{
681
print_proxy( "static const STUB_THUNK %s_StubThunkTable[] =\n", iface->name);
682
print_proxy( "{\n");
683
indent++;
684
write_thunk_methods( iface, 0 );
685
indent--;
686
print_proxy( "};\n\n");
687
}
688
689
/* server info */
690
print_proxy( "static const MIDL_SERVER_INFO %s_ServerInfo =\n", iface->name );
691
print_proxy( "{\n" );
692
indent++;
693
print_proxy( "&Object_StubDesc,\n" );
694
print_proxy( "0,\n" );
695
print_proxy( "__MIDL_ProcFormatString.Format,\n" );
696
print_proxy( "&%s_FormatStringOffsetTable[-3],\n", iface->name );
697
if (needs_stub_thunks)
698
print_proxy( "&%s_StubThunkTable[-3],\n", iface->name );
699
else
700
print_proxy( "0,\n" );
701
print_proxy( "0,\n" );
702
print_proxy( "0,\n" );
703
print_proxy( "0\n" );
704
indent--;
705
print_proxy( "};\n\n" );
706
707
/* stub vtable */
708
if (needs_inline_stubs)
709
{
710
print_proxy( "static const PRPC_STUB_FUNCTION %s_table[] =\n", iface->name);
711
print_proxy( "{\n");
712
indent++;
713
write_stub_methods(iface, FALSE);
714
fprintf(proxy, "\n");
715
indent--;
716
fprintf(proxy, "};\n\n");
717
}
718
print_proxy( "static %sCInterfaceStubVtbl _%sStubVtbl =\n",
719
need_delegation_indirect(iface) ? "" : "const ", iface->name);
720
print_proxy( "{\n");
721
indent++;
722
print_proxy( "{\n");
723
indent++;
724
print_proxy( "&IID_%s,\n", iface->name);
725
print_proxy( "&%s_ServerInfo,\n", iface->name );
726
print_proxy( "%d,\n", count);
727
if (needs_inline_stubs) print_proxy( "&%s_table[-3]\n", iface->name );
728
else print_proxy( "0\n" );
729
indent--;
730
print_proxy( "},\n");
731
print_proxy( "{\n");
732
indent++;
733
print_proxy( "%s_%s\n",
734
type_iface_get_async_iface(iface) == iface ? "CStdAsyncStubBuffer" : "CStdStubBuffer",
735
need_delegation_indirect(iface) ? "DELEGATING_METHODS" : "METHODS");
736
indent--;
737
print_proxy( "}\n");
738
indent--;
739
print_proxy( "};\n");
740
print_proxy( "\n");
741
}
742
743
static int does_any_iface(const statement_list_t *stmts, type_pred_t pred)
744
{
745
const statement_t *stmt;
746
747
if (stmts)
748
LIST_FOR_EACH_ENTRY(stmt, stmts, const statement_t, entry)
749
{
750
if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
751
{
752
if (pred(stmt->u.type))
753
return TRUE;
754
}
755
}
756
757
return FALSE;
758
}
759
760
int need_proxy(const type_t *iface)
761
{
762
if (!is_object( iface )) return 0;
763
if (is_local( iface->attrs )) return 0;
764
if (is_attr( iface->attrs, ATTR_DISPINTERFACE )) return 0;
765
return 1;
766
}
767
768
int need_stub(const type_t *iface)
769
{
770
return !is_object(iface) && !is_local(iface->attrs);
771
}
772
773
int need_proxy_file(const statement_list_t *stmts)
774
{
775
return does_any_iface(stmts, need_proxy);
776
}
777
778
int need_proxy_delegation(const statement_list_t *stmts)
779
{
780
return does_any_iface(stmts, need_delegation);
781
}
782
783
int need_inline_stubs(const type_t *iface)
784
{
785
const statement_t *stmt;
786
787
if (!interpreted_mode) return 1;
788
789
STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
790
{
791
const var_t *func = stmt->u.var;
792
if (is_local( func->attrs )) continue;
793
if (!is_interpreted_func( iface, func )) return 1;
794
}
795
return 0;
796
}
797
798
static int need_proxy_and_inline_stubs(const type_t *iface)
799
{
800
const statement_t *stmt;
801
802
if (!need_proxy( iface )) return 0;
803
if (!interpreted_mode) return 1;
804
805
STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
806
{
807
const var_t *func = stmt->u.var;
808
if (is_local( func->attrs )) continue;
809
if (!is_interpreted_func( iface, func )) return 1;
810
}
811
return 0;
812
}
813
814
int need_stub_files(const statement_list_t *stmts)
815
{
816
return does_any_iface(stmts, need_stub);
817
}
818
819
int need_inline_stubs_file(const statement_list_t *stmts)
820
{
821
return does_any_iface(stmts, need_inline_stubs);
822
}
823
824
static void write_proxy_stmts(const statement_list_t *stmts, unsigned int *proc_offset)
825
{
826
const statement_t *stmt;
827
if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
828
{
829
if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
830
{
831
type_t *iface = stmt->u.type;
832
if (need_proxy(iface))
833
{
834
write_proxy(iface, proc_offset);
835
if (type_iface_get_async_iface(iface))
836
write_proxy(type_iface_get_async_iface(iface), proc_offset);
837
}
838
}
839
}
840
}
841
842
static int cmp_iid( const void *ptr1, const void *ptr2 )
843
{
844
const type_t * const *iface1 = ptr1;
845
const type_t * const *iface2 = ptr2;
846
const struct uuid *uuid1 = get_attrp( (*iface1)->attrs, ATTR_UUID );
847
const struct uuid *uuid2 = get_attrp( (*iface2)->attrs, ATTR_UUID );
848
return memcmp( uuid1, uuid2, sizeof(*uuid1) );
849
}
850
851
static void build_iface_list( const statement_list_t *stmts, type_t **ifaces[], int *count )
852
{
853
const statement_t *stmt;
854
855
if (!stmts) return;
856
LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
857
{
858
if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
859
{
860
type_t *iface = stmt->u.type;
861
if (type_iface_get_inherit(iface) && need_proxy(iface))
862
{
863
*ifaces = xrealloc( *ifaces, (*count + 1) * sizeof(**ifaces) );
864
(*ifaces)[(*count)++] = iface;
865
if (type_iface_get_async_iface(iface))
866
{
867
iface = type_iface_get_async_iface(iface);
868
*ifaces = xrealloc( *ifaces, (*count + 1) * sizeof(**ifaces) );
869
(*ifaces)[(*count)++] = iface;
870
}
871
}
872
}
873
}
874
}
875
876
static type_t **sort_interfaces( const statement_list_t *stmts, int *count )
877
{
878
type_t **ifaces = NULL;
879
880
*count = 0;
881
build_iface_list( stmts, &ifaces, count );
882
qsort( ifaces, *count, sizeof(*ifaces), cmp_iid );
883
return ifaces;
884
}
885
886
static void write_proxy_routines(const statement_list_t *stmts)
887
{
888
int expr_eval_routines;
889
unsigned int proc_offset = 0;
890
char *file_id = proxy_token;
891
int i, count, have_baseiid = 0;
892
unsigned int table_version;
893
type_t **interfaces;
894
const type_t * delegate_to;
895
896
print_proxy( "#ifndef __REDQ_RPCPROXY_H_VERSION__\n");
897
print_proxy( "#define __REQUIRED_RPCPROXY_H_VERSION__ %u\n", interpreted_mode ? 475 : 440);
898
print_proxy( "#endif\n");
899
print_proxy( "\n");
900
if (interpreted_mode) print_proxy( "#define USE_STUBLESS_PROXY\n");
901
print_proxy( "#include \"rpcproxy.h\"\n");
902
print_proxy( "#ifndef __RPCPROXY_H_VERSION__\n");
903
print_proxy( "#error This code needs a newer version of rpcproxy.h\n");
904
print_proxy( "#endif /* __RPCPROXY_H_VERSION__ */\n");
905
print_proxy( "\n");
906
print_proxy( "#include \"%s\"\n", header_name);
907
print_proxy( "\n");
908
909
if (does_any_iface(stmts, need_proxy_and_inline_stubs))
910
{
911
write_exceptions( proxy );
912
print_proxy( "\n");
913
print_proxy( "struct __proxy_frame\n");
914
print_proxy( "{\n");
915
print_proxy( " __DECL_EXCEPTION_FRAME\n");
916
print_proxy( " MIDL_STUB_MESSAGE _StubMsg;\n");
917
print_proxy( " void *This;\n");
918
print_proxy( "};\n");
919
print_proxy( "\n");
920
print_proxy("static int __proxy_filter( struct __proxy_frame *__frame )\n");
921
print_proxy( "{\n");
922
print_proxy( " return (__frame->_StubMsg.dwStubPhase != PROXY_SENDRECEIVE);\n");
923
print_proxy( "}\n");
924
print_proxy( "\n");
925
}
926
927
write_formatstringsdecl(proxy, indent, stmts, need_proxy);
928
write_stubdescproto();
929
write_proxy_stmts(stmts, &proc_offset);
930
931
expr_eval_routines = write_expr_eval_routines(proxy, proxy_token);
932
if (expr_eval_routines)
933
write_expr_eval_routine_list(proxy, proxy_token);
934
write_user_quad_list(proxy);
935
write_stubdesc(expr_eval_routines);
936
937
print_proxy( "#if !defined(__RPC_WIN%u__)\n", pointer_size == 8 ? 64 : 32);
938
print_proxy( "#error Invalid build platform for this proxy.\n");
939
print_proxy( "#endif\n");
940
print_proxy( "\n");
941
write_procformatstring(proxy, stmts, need_proxy);
942
write_typeformatstring(proxy, stmts, need_proxy);
943
944
interfaces = sort_interfaces(stmts, &count);
945
fprintf(proxy, "static const CInterfaceProxyVtbl* const _%s_ProxyVtblList[] =\n", file_id);
946
fprintf(proxy, "{\n");
947
for (i = 0; i < count; i++)
948
fprintf(proxy, " (const CInterfaceProxyVtbl*)&_%sProxyVtbl,\n", interfaces[i]->name);
949
fprintf(proxy, " 0\n");
950
fprintf(proxy, "};\n");
951
fprintf(proxy, "\n");
952
953
fprintf(proxy, "static const CInterfaceStubVtbl* const _%s_StubVtblList[] =\n", file_id);
954
fprintf(proxy, "{\n");
955
for (i = 0; i < count; i++)
956
fprintf(proxy, " &_%sStubVtbl,\n", interfaces[i]->name);
957
fprintf(proxy, " 0\n");
958
fprintf(proxy, "};\n");
959
fprintf(proxy, "\n");
960
961
fprintf(proxy, "static PCInterfaceName const _%s_InterfaceNamesList[] =\n", file_id);
962
fprintf(proxy, "{\n");
963
for (i = 0; i < count; i++)
964
fprintf(proxy, " \"%s\",\n", interfaces[i]->name);
965
fprintf(proxy, " 0\n");
966
fprintf(proxy, "};\n");
967
fprintf(proxy, "\n");
968
969
for (i = 0; i < count; i++)
970
if ((have_baseiid = get_delegation_indirect( interfaces[i], NULL ))) break;
971
972
if (have_baseiid)
973
{
974
fprintf(proxy, "static const IID * _%s_BaseIIDList[] =\n", file_id);
975
fprintf(proxy, "{\n");
976
for (i = 0; i < count; i++)
977
{
978
if (get_delegation_indirect(interfaces[i], &delegate_to))
979
fprintf( proxy, " &IID_%s, /* %s */\n", delegate_to->name, interfaces[i]->name );
980
else
981
fprintf( proxy, " 0,\n" );
982
}
983
fprintf(proxy, " 0\n");
984
fprintf(proxy, "};\n");
985
fprintf(proxy, "\n");
986
}
987
988
fprintf(proxy, "static int __stdcall _%s_IID_Lookup(const IID* pIID, int* pIndex)\n", file_id);
989
fprintf(proxy, "{\n");
990
fprintf(proxy, " int low = 0, high = %d;\n", count - 1);
991
fprintf(proxy, "\n");
992
fprintf(proxy, " while (low <= high)\n");
993
fprintf(proxy, " {\n");
994
fprintf(proxy, " int pos = (low + high) / 2;\n");
995
fprintf(proxy, " int res = IID_GENERIC_CHECK_IID(_%s, pIID, pos);\n", file_id);
996
fprintf(proxy, " if (!res) { *pIndex = pos; return 1; }\n");
997
fprintf(proxy, " if (res > 0) low = pos + 1;\n");
998
fprintf(proxy, " else high = pos - 1;\n");
999
fprintf(proxy, " }\n");
1000
fprintf(proxy, " return 0;\n");
1001
fprintf(proxy, "}\n");
1002
fprintf(proxy, "\n");
1003
1004
table_version = interpreted_mode ? 2 : 1;
1005
for (i = 0; i < count; i++)
1006
{
1007
if (type_iface_get_async_iface(interfaces[i]) != interfaces[i]) continue;
1008
if (table_version != 6)
1009
{
1010
fprintf(proxy, "static const IID *_AsyncInterfaceTable[] =\n");
1011
fprintf(proxy, "{\n");
1012
table_version = 6;
1013
}
1014
fprintf(proxy, " &IID_%s,\n", interfaces[i]->name);
1015
fprintf(proxy, " (IID*)(LONG_PTR)-1,\n");
1016
}
1017
if (table_version == 6)
1018
{
1019
fprintf(proxy, " 0\n");
1020
fprintf(proxy, "};\n");
1021
fprintf(proxy, "\n");
1022
}
1023
1024
fprintf(proxy, "const ExtendedProxyFileInfo %s_ProxyFileInfo =\n", file_id);
1025
fprintf(proxy, "{\n");
1026
fprintf(proxy, " (const PCInterfaceProxyVtblList*)_%s_ProxyVtblList,\n", file_id);
1027
fprintf(proxy, " (const PCInterfaceStubVtblList*)_%s_StubVtblList,\n", file_id);
1028
fprintf(proxy, " _%s_InterfaceNamesList,\n", file_id);
1029
if (have_baseiid) fprintf(proxy, " _%s_BaseIIDList,\n", file_id);
1030
else fprintf(proxy, " 0,\n");
1031
fprintf(proxy, " _%s_IID_Lookup,\n", file_id);
1032
fprintf(proxy, " %d,\n", count);
1033
fprintf(proxy, " %u,\n", table_version);
1034
fprintf(proxy, " %s,\n", table_version == 6 ? "_AsyncInterfaceTable" : "0");
1035
fprintf(proxy, " 0,\n");
1036
fprintf(proxy, " 0,\n");
1037
fprintf(proxy, " 0\n");
1038
fprintf(proxy, "};\n");
1039
}
1040
1041
void write_proxies(const statement_list_t *stmts)
1042
{
1043
if (!do_proxies) return;
1044
if (do_everything && !need_proxy_file(stmts)) return;
1045
1046
init_proxy(stmts);
1047
if(!proxy) return;
1048
1049
write_proxy_routines( stmts );
1050
fclose(proxy);
1051
}
1052
1053