Reactos
at master 554 lines 19 kB view raw
1/* 2 * IDL Compiler 3 * 4 * Copyright 2005-2006 Eric Kohl 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA 19 */ 20 21#include "config.h" 22 23#include <stdio.h> 24#include <stdlib.h> 25#include <string.h> 26#include <ctype.h> 27 28#include "widl.h" 29#include "utils.h" 30#include "parser.h" 31#include "header.h" 32 33#include "typegen.h" 34 35static FILE* server; 36static int indent = 0; 37 38 39static void print_server(const char *format, ...) __attribute__((format (printf, 1, 2))); 40static void print_server(const char *format, ...) 41{ 42 va_list va; 43 va_start(va, format); 44 print(server, indent, format, va); 45 va_end(va); 46} 47 48static void write_function_stub(const type_t *iface, const var_t *func, unsigned int proc_offset) 49{ 50 const var_t *var; 51 unsigned char explicit_fc, implicit_fc; 52 int has_full_pointer = is_full_pointer_function(func); 53 const var_t *handle_var = get_func_handle_var( iface, func, &explicit_fc, &implicit_fc ); 54 type_t *ret_type = type_function_get_rettype(func->declspec.type); 55 56 if (is_interpreted_func( iface, func )) return; 57 58 print_server("struct __frame_%s_%s\n{\n", iface->name, get_name(func)); 59 indent++; 60 print_server("__DECL_EXCEPTION_FRAME\n"); 61 print_server("MIDL_STUB_MESSAGE _StubMsg;\n"); 62 63 /* Declare arguments */ 64 declare_stub_args(server, indent, func); 65 66 indent--; 67 print_server("};\n\n"); 68 69 print_server("static void __finally_%s_%s(", iface->name, get_name(func)); 70 fprintf(server," struct __frame_%s_%s *__frame )\n{\n", iface->name, get_name(func)); 71 72 if (interpreted_mode) print_server("NdrCorrelationFree(&__frame->_StubMsg);\n"); 73 74 indent++; 75 write_remoting_arguments(server, indent, func, "__frame->", PASS_OUT, PHASE_FREE); 76 77 if (!is_void(ret_type)) 78 write_remoting_arguments(server, indent, func, "__frame->", PASS_RETURN, PHASE_FREE); 79 80 if (has_full_pointer) 81 write_full_pointer_free(server, indent, func); 82 83 indent--; 84 print_server("}\n\n"); 85 86 print_server("void __RPC_STUB %s_%s( PRPC_MESSAGE _pRpcMessage )\n", iface->name, get_name(func)); 87 88 /* write the functions body */ 89 fprintf(server, "{\n"); 90 indent++; 91 print_server("struct __frame_%s_%s __f, * const __frame = &__f;\n", iface->name, get_name(func)); 92 if (interpreted_mode) print_server("ULONG _NdrCorrCache[256];\n"); 93 if (has_out_arg_or_return(func)) print_server("RPC_STATUS _Status;\n"); 94 fprintf(server, "\n"); 95 96 print_server("NdrServerInitializeNew(\n"); 97 indent++; 98 print_server("_pRpcMessage,\n"); 99 print_server("&__frame->_StubMsg,\n"); 100 print_server("&%s_StubDesc);\n", iface->name); 101 indent--; 102 fprintf(server, "\n"); 103 print_server( "RpcExceptionInit( __server_filter, __finally_%s_%s );\n", iface->name, get_name(func)); 104 105 write_parameters_init(server, indent, func, "__frame->"); 106 107 if (explicit_fc == FC_BIND_PRIMITIVE) 108 { 109 print_server("__frame->%s = _pRpcMessage->Handle;\n", handle_var->name); 110 fprintf(server, "\n"); 111 } 112 113 print_server("RpcTryFinally\n"); 114 print_server("{\n"); 115 indent++; 116 print_server("RpcTryExcept\n"); 117 print_server("{\n"); 118 indent++; 119 if (interpreted_mode) 120 print_server("NdrCorrelationInitialize(&__frame->_StubMsg, _NdrCorrCache, sizeof(_NdrCorrCache), 0);\n" ); 121 122 if (has_full_pointer) 123 write_full_pointer_init(server, indent, func, TRUE); 124 125 if (type_function_get_args(func->declspec.type)) 126 { 127 print_server("if ((_pRpcMessage->DataRepresentation & 0x0000FFFFUL) != NDR_LOCAL_DATA_REPRESENTATION)\n"); 128 indent++; 129 print_server("NdrConvert(&__frame->_StubMsg, (PFORMAT_STRING)&__MIDL_ProcFormatString.Format[%u]);\n", 130 proc_offset); 131 indent--; 132 fprintf(server, "\n"); 133 134 /* unmarshall arguments */ 135 write_remoting_arguments(server, indent, func, "__frame->", PASS_IN, PHASE_UNMARSHAL); 136 } 137 138 print_server("if (__frame->_StubMsg.Buffer > __frame->_StubMsg.BufferEnd)\n"); 139 print_server("{\n"); 140 indent++; 141 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n"); 142 indent--; 143 print_server("}\n"); 144 indent--; 145 print_server("}\n"); 146 print_server("RpcExcept(RPC_BAD_STUB_DATA_EXCEPTION_FILTER)\n"); 147 print_server("{\n"); 148 indent++; 149 print_server("RpcRaiseException(RPC_X_BAD_STUB_DATA);\n"); 150 indent--; 151 print_server("}\n"); 152 print_server("RpcEndExcept\n"); 153 fprintf(server, "\n"); 154 155 /* Assign 'out' arguments */ 156 assign_stub_out_args(server, indent, func, "__frame->"); 157 158 /* Call the real server function */ 159 if (is_context_handle(ret_type)) 160 { 161 print_server("__frame->_RetVal = NDRSContextUnmarshall((char*)0, _pRpcMessage->DataRepresentation);\n"); 162 print_server("*(("); 163 write_type_decl(server, type_function_get_ret(func->declspec.type), NULL); 164 fprintf(server, "*)NDRSContextValue(__frame->_RetVal)) = "); 165 } 166 else 167 print_server("%s", is_void(ret_type) ? "" : "__frame->_RetVal = "); 168 fprintf(server, "%s%s", prefix_server, get_name(func)); 169 170 if (type_function_get_args(func->declspec.type)) 171 { 172 int first_arg = 1; 173 174 fprintf(server, "(\n"); 175 indent++; 176 LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry ) 177 { 178 if (first_arg) 179 first_arg = 0; 180 else 181 fprintf(server, ",\n"); 182 if (is_context_handle(var->declspec.type)) 183 { 184 /* if the context_handle attribute appears in the chain of types 185 * without pointers being followed, then the context handle must 186 * be direct, otherwise it is a pointer */ 187 const char *ch_ptr = is_aliaschain_attr(var->declspec.type, ATTR_CONTEXTHANDLE) ? "*" : ""; 188 print_server("("); 189 write_type_decl_left(server, &var->declspec); 190 fprintf(server, ")%sNDRSContextValue(__frame->%s)", ch_ptr, var->name); 191 } 192 else 193 { 194 print_server("%s__frame->%s", is_array(var->declspec.type) && !type_array_is_decl_as_ptr(var->declspec.type) ? "*" : "", var->name); 195 } 196 } 197 fprintf(server, ");\n"); 198 indent--; 199 } 200 else 201 { 202 fprintf(server, "();\n"); 203 } 204 205 if (has_out_arg_or_return(func)) 206 { 207 write_remoting_arguments(server, indent, func, "__frame->", PASS_OUT, PHASE_BUFFERSIZE); 208 209 if (!is_void(ret_type)) 210 write_remoting_arguments(server, indent, func, "__frame->", PASS_RETURN, PHASE_BUFFERSIZE); 211 212 print_server("_pRpcMessage->BufferLength = __frame->_StubMsg.BufferLength;\n"); 213 fprintf(server, "\n"); 214 print_server("_Status = I_RpcGetBuffer(_pRpcMessage);\n"); 215 print_server("if (_Status)\n"); 216 indent++; 217 print_server("RpcRaiseException(_Status);\n"); 218 indent--; 219 fprintf(server, "\n"); 220 print_server("__frame->_StubMsg.Buffer = _pRpcMessage->Buffer;\n"); 221 fprintf(server, "\n"); 222 } 223 224 /* marshall arguments */ 225 write_remoting_arguments(server, indent, func, "__frame->", PASS_OUT, PHASE_MARSHAL); 226 227 /* marshall the return value */ 228 if (!is_void(ret_type)) 229 write_remoting_arguments(server, indent, func, "__frame->", PASS_RETURN, PHASE_MARSHAL); 230 231 indent--; 232 print_server("}\n"); 233 print_server("RpcFinally\n"); 234 print_server("{\n"); 235 indent++; 236 print_server("__finally_%s_%s( __frame );\n", iface->name, get_name(func)); 237 indent--; 238 print_server("}\n"); 239 print_server("RpcEndFinally\n"); 240 241 /* calculate buffer length */ 242 fprintf(server, "\n"); 243 print_server("_pRpcMessage->BufferLength = __frame->_StubMsg.Buffer - (unsigned char *)_pRpcMessage->Buffer;\n"); 244 indent--; 245 fprintf(server, "}\n"); 246 fprintf(server, "\n"); 247} 248 249 250static void write_function_stubs(type_t *iface, unsigned int *proc_offset) 251{ 252 const statement_t *stmt; 253 254 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) ) 255 { 256 var_t *func = stmt->u.var; 257 258 write_function_stub( iface, func, *proc_offset ); 259 260 /* update proc_offset */ 261 func->procstring_offset = *proc_offset; 262 *proc_offset += get_size_procformatstring_func( iface, func ); 263 } 264} 265 266 267static void write_dispatchtable(type_t *iface) 268{ 269 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION); 270 unsigned int method_count = 0; 271 const statement_t *stmt; 272 273 print_server("static RPC_DISPATCH_FUNCTION %s_table[] =\n", iface->name); 274 print_server("{\n"); 275 indent++; 276 277 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) ) 278 { 279 var_t *func = stmt->u.var; 280 if (is_interpreted_func( iface, func )) 281 print_server("NdrServerCall2,\n"); 282 else 283 print_server("%s_%s,\n", iface->name, get_name(func)); 284 method_count++; 285 } 286 print_server("0\n"); 287 indent--; 288 print_server("};\n"); 289 print_server("static RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable =\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver)); 290 print_server("{\n"); 291 indent++; 292 print_server("%u,\n", method_count); 293 print_server("%s_table\n", iface->name); 294 indent--; 295 print_server("};\n"); 296 fprintf(server, "\n"); 297} 298 299 300static void write_routinetable(type_t *iface) 301{ 302 const statement_t *stmt; 303 304 print_server( "static const SERVER_ROUTINE %s_ServerRoutineTable[] =\n", iface->name ); 305 print_server( "{\n" ); 306 indent++; 307 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) ) 308 { 309 var_t *func = stmt->u.var; 310 if (is_local( func->attrs )) continue; 311 print_server( "(void *)%s%s,\n", prefix_server, get_name(func)); 312 } 313 indent--; 314 print_server( "};\n\n" ); 315} 316 317 318static void write_rundown_routines(void) 319{ 320 context_handle_t *ch; 321 int count = list_count( &context_handle_list ); 322 323 if (!count) return; 324 print_server( "static const NDR_RUNDOWN RundownRoutines[] =\n" ); 325 print_server( "{\n" ); 326 indent++; 327 LIST_FOR_EACH_ENTRY( ch, &context_handle_list, context_handle_t, entry ) 328 { 329 print_server( "%s_rundown", ch->name ); 330 if (--count) fputc( ',', server ); 331 fputc( '\n', server ); 332 } 333 indent--; 334 print_server( "};\n\n" ); 335} 336 337 338static void write_serverinfo(type_t *iface) 339{ 340 print_server( "static const MIDL_SERVER_INFO %s_ServerInfo =\n", iface->name ); 341 print_server( "{\n" ); 342 indent++; 343 print_server( "&%s_StubDesc,\n", iface->name ); 344 print_server( "%s_ServerRoutineTable,\n", iface->name ); 345 print_server( "__MIDL_ProcFormatString.Format,\n" ); 346 print_server( "%s_FormatStringOffsetTable,\n", iface->name ); 347 print_server( "0,\n" ); 348 print_server( "0,\n" ); 349 print_server( "0,\n" ); 350 print_server( "0\n" ); 351 indent--; 352 print_server( "};\n\n" ); 353} 354 355 356static void write_stubdescdecl(type_t *iface) 357{ 358 print_server("static const MIDL_STUB_DESC %s_StubDesc;\n", iface->name); 359 fprintf(server, "\n"); 360} 361 362 363static void write_stubdescriptor(type_t *iface, int expr_eval_routines) 364{ 365 print_server("static const MIDL_STUB_DESC %s_StubDesc =\n", iface->name); 366 print_server("{\n"); 367 indent++; 368 print_server("(void *)& %s___RpcServerInterface,\n", iface->name); 369 print_server("MIDL_user_allocate,\n"); 370 print_server("MIDL_user_free,\n"); 371 print_server("{\n"); 372 indent++; 373 print_server("0,\n"); 374 indent--; 375 print_server("},\n"); 376 if (!list_empty( &context_handle_list )) 377 print_server("RundownRoutines,\n"); 378 else 379 print_server("0,\n"); 380 print_server("0,\n"); 381 if (expr_eval_routines) 382 print_server("ExprEvalRoutines,\n"); 383 else 384 print_server("0,\n"); 385 print_server("0,\n"); 386 print_server("__MIDL_TypeFormatString.Format,\n"); 387 print_server("1, /* -error bounds_check flag */\n"); 388 print_server("0x%x, /* Ndr library version */\n", interpreted_mode ? 0x50002 : 0x10001); 389 print_server("0,\n"); 390 print_server("0x50200ca, /* MIDL Version 5.2.202 */\n"); 391 print_server("0,\n"); 392 print_server("%s,\n", list_empty(&user_type_list) ? "0" : "UserMarshalRoutines"); 393 print_server("0, /* notify & notify_flag routine table */\n"); 394 print_server("1, /* Flags */\n"); 395 print_server("0, /* Reserved3 */\n"); 396 print_server("0, /* Reserved4 */\n"); 397 print_server("0 /* Reserved5 */\n"); 398 indent--; 399 print_server("};\n"); 400 fprintf(server, "\n"); 401} 402 403 404static void write_serverinterfacedecl(type_t *iface) 405{ 406 unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION); 407 struct uuid *uuid = get_attrp(iface->attrs, ATTR_UUID); 408 const str_list_t *endpoints = get_attrp(iface->attrs, ATTR_ENDPOINT); 409 410 if (endpoints) write_endpoints( server, iface->name, endpoints ); 411 412 print_server("static RPC_DISPATCH_TABLE %s_v%d_%d_DispatchTable;\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver)); 413 print_server( "static const MIDL_SERVER_INFO %s_ServerInfo;\n", iface->name ); 414 fprintf(server, "\n"); 415 print_server("static const RPC_SERVER_INTERFACE %s___RpcServerInterface =\n", iface->name ); 416 print_server("{\n"); 417 indent++; 418 print_server("sizeof(RPC_SERVER_INTERFACE),\n"); 419 print_server("{{0x%08x,0x%04x,0x%04x,{0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x}},{%d,%d}},\n", 420 uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1], 421 uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6], 422 uuid->Data4[7], MAJORVERSION(ver), MINORVERSION(ver)); 423 print_server("{{0x8a885d04,0x1ceb,0x11c9,{0x9f,0xe8,0x08,0x00,0x2b,0x10,0x48,0x60}},{2,0}},\n"); /* FIXME */ 424 print_server("&%s_v%d_%d_DispatchTable,\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver)); 425 if (endpoints) 426 { 427 print_server("%u,\n", list_count(endpoints)); 428 print_server("(PRPC_PROTSEQ_ENDPOINT)%s__RpcProtseqEndpoint,\n", iface->name); 429 } 430 else 431 { 432 print_server("0,\n"); 433 print_server("0,\n"); 434 } 435 print_server("0,\n"); 436 print_server("&%s_ServerInfo,\n", iface->name); 437 print_server("0,\n"); 438 indent--; 439 print_server("};\n"); 440 if (old_names) 441 print_server("RPC_IF_HANDLE %s_ServerIfHandle = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n", 442 iface->name, iface->name); 443 else 444 print_server("RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec = (RPC_IF_HANDLE)& %s___RpcServerInterface;\n", 445 prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver), iface->name); 446 fprintf(server, "\n"); 447} 448 449 450static void init_server(void) 451{ 452 if (server) 453 return; 454 if (!(server = fopen(server_name, "w"))) 455 error("Could not open %s for output\n", server_name); 456 457 print_server("/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n", PACKAGE_VERSION, input_name); 458 print_server("#include <string.h>\n"); 459 fprintf(server, "\n"); 460 print_server("#include \"%s\"\n", header_name); 461 print_server( "\n"); 462} 463 464 465static void write_server_stmts(const statement_list_t *stmts, int expr_eval_routines, unsigned int *proc_offset) 466{ 467 const statement_t *stmt; 468 if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry ) 469 { 470 if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE) 471 { 472 type_t *iface = stmt->u.type; 473 if (!need_stub(iface)) 474 continue; 475 476 fprintf(server, "/*****************************************************************************\n"); 477 fprintf(server, " * %s interface\n", iface->name); 478 fprintf(server, " */\n"); 479 fprintf(server, "\n"); 480 481 if (statements_has_func(type_iface_get_stmts(iface))) 482 { 483 write_serverinterfacedecl(iface); 484 write_stubdescdecl(iface); 485 486 write_function_stubs(iface, proc_offset); 487 488 print_server("#if !defined(__RPC_WIN%u__)\n", pointer_size == 8 ? 64 : 32); 489 print_server("#error Invalid build platform for this stub.\n"); 490 print_server("#endif\n"); 491 492 fprintf(server, "\n"); 493 write_procformatstring_offsets( server, iface ); 494 write_stubdescriptor(iface, expr_eval_routines); 495 write_dispatchtable(iface); 496 write_routinetable(iface); 497 write_serverinfo(iface); 498 } 499 } 500 } 501} 502 503static void write_server_routines(const statement_list_t *stmts) 504{ 505 unsigned int proc_offset = 0; 506 int expr_eval_routines; 507 508 if (need_inline_stubs_file( stmts )) 509 { 510 write_exceptions( server ); 511 print_server("\n"); 512 print_server("struct __server_frame\n"); 513 print_server("{\n"); 514 print_server(" __DECL_EXCEPTION_FRAME\n"); 515 print_server(" MIDL_STUB_MESSAGE _StubMsg;\n"); 516 print_server("};\n"); 517 print_server("\n"); 518 print_server("static int __server_filter( struct __server_frame *__frame )\n"); 519 print_server( "{\n"); 520 print_server( " return (__frame->code == STATUS_ACCESS_VIOLATION) ||\n"); 521 print_server( " (__frame->code == STATUS_DATATYPE_MISALIGNMENT) ||\n"); 522 print_server( " (__frame->code == RPC_X_BAD_STUB_DATA) ||\n"); 523 print_server( " (__frame->code == RPC_S_INVALID_BOUND);\n"); 524 print_server( "}\n"); 525 print_server( "\n"); 526 } 527 528 write_formatstringsdecl(server, indent, stmts, need_stub); 529 expr_eval_routines = write_expr_eval_routines(server, server_token); 530 if (expr_eval_routines) 531 write_expr_eval_routine_list(server, server_token); 532 write_user_quad_list(server); 533 write_rundown_routines(); 534 535 write_server_stmts(stmts, expr_eval_routines, &proc_offset); 536 537 write_procformatstring(server, stmts, need_stub); 538 write_typeformatstring(server, stmts, need_stub); 539} 540 541void write_server(const statement_list_t *stmts) 542{ 543 if (!do_server) 544 return; 545 if (do_everything && !need_stub_files(stmts)) 546 return; 547 548 init_server(); 549 if (!server) 550 return; 551 552 write_server_routines( stmts ); 553 fclose(server); 554}