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