proxy.c 34 KB

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