proxy.c 34 KB

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