server.c 19 KB

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