header.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844
  1. /*
  2. * IDL Compiler
  3. *
  4. * Copyright 2002 Ove Kaaven
  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 <stdarg.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 "expr.h"
  34. #include "typetree.h"
  35. #include "typelib.h"
  36. static int indentation = 0;
  37. static int is_object_interface = 0;
  38. user_type_list_t user_type_list = LIST_INIT(user_type_list);
  39. context_handle_list_t context_handle_list = LIST_INIT(context_handle_list);
  40. generic_handle_list_t generic_handle_list = LIST_INIT(generic_handle_list);
  41. static void write_type_v(FILE *f, const decl_spec_t *t, int is_field, int declonly, const char *name);
  42. static void indent(FILE *h, int delta)
  43. {
  44. int c;
  45. if (delta < 0) indentation += delta;
  46. for (c=0; c<indentation; c++) fprintf(h, " ");
  47. if (delta > 0) indentation += delta;
  48. }
  49. static void write_line(FILE *f, int delta, const char *fmt, ...)
  50. {
  51. va_list ap;
  52. indent(f, delta);
  53. va_start(ap, fmt);
  54. vfprintf(f, fmt, ap);
  55. va_end(ap);
  56. fprintf(f, "\n");
  57. }
  58. int is_ptrchain_attr(const var_t *var, enum attr_type t)
  59. {
  60. if (is_attr(var->attrs, t))
  61. return 1;
  62. else
  63. {
  64. type_t *type = var->declspec.type;
  65. for (;;)
  66. {
  67. if (is_attr(type->attrs, t))
  68. return 1;
  69. else if (type_is_alias(type))
  70. type = type_alias_get_aliasee_type(type);
  71. else if (is_ptr(type))
  72. type = type_pointer_get_ref_type(type);
  73. else return 0;
  74. }
  75. }
  76. }
  77. int is_aliaschain_attr(const type_t *type, enum attr_type attr)
  78. {
  79. const type_t *t = type;
  80. for (;;)
  81. {
  82. if (is_attr(t->attrs, attr))
  83. return 1;
  84. else if (type_is_alias(t))
  85. t = type_alias_get_aliasee_type(t);
  86. else return 0;
  87. }
  88. }
  89. int is_attr(const attr_list_t *list, enum attr_type t)
  90. {
  91. const attr_t *attr;
  92. if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
  93. if (attr->type == t) return 1;
  94. return 0;
  95. }
  96. void *get_attrp(const attr_list_t *list, enum attr_type t)
  97. {
  98. const attr_t *attr;
  99. if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
  100. if (attr->type == t) return attr->u.pval;
  101. return NULL;
  102. }
  103. unsigned int get_attrv(const attr_list_t *list, enum attr_type t)
  104. {
  105. const attr_t *attr;
  106. if (list) LIST_FOR_EACH_ENTRY( attr, list, const attr_t, entry )
  107. if (attr->type == t) return attr->u.ival;
  108. return 0;
  109. }
  110. static void write_guid(FILE *f, const char *guid_prefix, const char *name, const UUID *uuid)
  111. {
  112. if (!uuid) return;
  113. fprintf(f, "DEFINE_GUID(%s_%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
  114. "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
  115. guid_prefix, name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0],
  116. uuid->Data4[1], uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5],
  117. uuid->Data4[6], uuid->Data4[7]);
  118. }
  119. static void write_uuid_decl(FILE *f, type_t *type, const UUID *uuid)
  120. {
  121. char *name = format_namespace(type->namespace, "", "::", type->name);
  122. fprintf(f, "#ifdef __CRT_UUID_DECL\n");
  123. fprintf(f, "__CRT_UUID_DECL(%s, 0x%08x, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,"
  124. "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x)\n",
  125. name, uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1],
  126. uuid->Data4[2], uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6],
  127. uuid->Data4[7]);
  128. fprintf(f, "#endif\n");
  129. free(name);
  130. }
  131. static const char *uuid_string(const UUID *uuid)
  132. {
  133. static char buf[37];
  134. sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
  135. uuid->Data1, uuid->Data2, uuid->Data3, uuid->Data4[0], uuid->Data4[1], uuid->Data4[2],
  136. uuid->Data4[3], uuid->Data4[4], uuid->Data4[5], uuid->Data4[6], uuid->Data4[7]);
  137. return buf;
  138. }
  139. static void write_namespace_start(FILE *header, struct namespace *namespace)
  140. {
  141. if(is_global_namespace(namespace)) {
  142. if(use_abi_namespace)
  143. write_line(header, 1, "namespace ABI {");
  144. return;
  145. }
  146. write_namespace_start(header, namespace->parent);
  147. write_line(header, 1, "namespace %s {", namespace->name);
  148. }
  149. static void write_namespace_end(FILE *header, struct namespace *namespace)
  150. {
  151. if(is_global_namespace(namespace)) {
  152. if(use_abi_namespace)
  153. write_line(header, -1, "}", namespace->name);
  154. return;
  155. }
  156. write_line(header, -1, "}", namespace->name);
  157. write_namespace_end(header, namespace->parent);
  158. }
  159. const char *get_name(const var_t *v)
  160. {
  161. static char *buffer;
  162. free( buffer );
  163. if (is_attr( v->attrs, ATTR_PROPGET ))
  164. return buffer = strmake( "get_%s", v->name );
  165. if (is_attr( v->attrs, ATTR_PROPPUT ))
  166. return buffer = strmake( "put_%s", v->name );
  167. if (is_attr( v->attrs, ATTR_PROPPUTREF ))
  168. return buffer = strmake( "putref_%s", v->name );
  169. buffer = NULL;
  170. return v->name;
  171. }
  172. static void write_fields(FILE *h, var_list_t *fields)
  173. {
  174. unsigned nameless_struct_cnt = 0, nameless_struct_i = 0, nameless_union_cnt = 0, nameless_union_i = 0;
  175. const char *name;
  176. char buf[32];
  177. var_t *v;
  178. if (!fields) return;
  179. LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) {
  180. if (!v || !v->declspec.type) continue;
  181. switch(type_get_type_detect_alias(v->declspec.type)) {
  182. case TYPE_STRUCT:
  183. case TYPE_ENCAPSULATED_UNION:
  184. nameless_struct_cnt++;
  185. break;
  186. case TYPE_UNION:
  187. nameless_union_cnt++;
  188. break;
  189. default:
  190. ;
  191. }
  192. }
  193. LIST_FOR_EACH_ENTRY( v, fields, var_t, entry ) {
  194. if (!v || !v->declspec.type) continue;
  195. indent(h, 0);
  196. name = v->name;
  197. switch(type_get_type_detect_alias(v->declspec.type)) {
  198. case TYPE_STRUCT:
  199. case TYPE_ENCAPSULATED_UNION:
  200. if(!v->name) {
  201. fprintf(h, "__C89_NAMELESS ");
  202. if(nameless_struct_cnt == 1) {
  203. name = "__C89_NAMELESSSTRUCTNAME";
  204. }else if(nameless_struct_i < 5 /* # of supporting macros */) {
  205. sprintf(buf, "__C89_NAMELESSSTRUCTNAME%d", ++nameless_struct_i);
  206. name = buf;
  207. }
  208. }
  209. break;
  210. case TYPE_UNION:
  211. if(!v->name) {
  212. fprintf(h, "__C89_NAMELESS ");
  213. if(nameless_union_cnt == 1) {
  214. name = "__C89_NAMELESSUNIONNAME";
  215. }else if(nameless_union_i < 8 /* # of supporting macros */ ) {
  216. sprintf(buf, "__C89_NAMELESSUNIONNAME%d", ++nameless_union_i);
  217. name = buf;
  218. }
  219. }
  220. break;
  221. default:
  222. ;
  223. }
  224. write_type_v(h, &v->declspec, TRUE, v->declonly, name);
  225. fprintf(h, ";\n");
  226. }
  227. }
  228. static void write_enums(FILE *h, var_list_t *enums, const char *enum_name)
  229. {
  230. var_t *v;
  231. if (!enums) return;
  232. LIST_FOR_EACH_ENTRY( v, enums, var_t, entry )
  233. {
  234. if (v->name) {
  235. indent(h, 0);
  236. if(!enum_name)
  237. fprintf(h, "%s", get_name(v));
  238. else
  239. fprintf(h, "%s_%s", enum_name, get_name(v));
  240. if (v->eval) {
  241. fprintf(h, " = ");
  242. write_expr(h, v->eval, 0, 1, NULL, NULL, "");
  243. }
  244. }
  245. if (list_next( enums, &v->entry )) fprintf(h, ",\n");
  246. }
  247. fprintf(h, "\n");
  248. }
  249. int needs_space_after(type_t *t)
  250. {
  251. return (type_is_alias(t) ||
  252. (!is_ptr(t) && (!is_array(t) || !type_array_is_decl_as_ptr(t) || t->name)));
  253. }
  254. int decl_needs_parens(const type_t *t)
  255. {
  256. if (type_is_alias(t))
  257. return FALSE;
  258. if (is_array(t) && !type_array_is_decl_as_ptr(t))
  259. return TRUE;
  260. return is_func(t);
  261. }
  262. static void write_pointer_left(FILE *h, type_t *ref)
  263. {
  264. if (needs_space_after(ref))
  265. fprintf(h, " ");
  266. if (decl_needs_parens(ref))
  267. fprintf(h, "(");
  268. if (type_get_type_detect_alias(ref) == TYPE_FUNCTION)
  269. {
  270. const char *callconv = get_attrp(ref->attrs, ATTR_CALLCONV);
  271. if (!callconv && is_object_interface) callconv = "STDMETHODCALLTYPE";
  272. if (callconv) fprintf(h, "%s ", callconv);
  273. }
  274. fprintf(h, "*");
  275. }
  276. void write_type_left(FILE *h, const decl_spec_t *ds, enum name_type name_type, int declonly, int write_callconv)
  277. {
  278. type_t *t = ds->type;
  279. const char *name;
  280. if (!h) return;
  281. name = type_get_name(t, name_type);
  282. if (ds->func_specifier & FUNCTION_SPECIFIER_INLINE)
  283. fprintf(h, "inline ");
  284. if ((ds->qualifier & TYPE_QUALIFIER_CONST) && (type_is_alias(t) || !is_ptr(t)))
  285. fprintf(h, "const ");
  286. if (type_is_alias(t)) fprintf(h, "%s", t->name);
  287. else {
  288. switch (type_get_type_detect_alias(t)) {
  289. case TYPE_ENUM:
  290. if (!declonly && !t->written) {
  291. assert(t->defined);
  292. if (name) fprintf(h, "enum %s {\n", name);
  293. else fprintf(h, "enum {\n");
  294. t->written = TRUE;
  295. indentation++;
  296. write_enums(h, type_enum_get_values(t), is_global_namespace(t->namespace) ? NULL : t->name);
  297. indent(h, -1);
  298. fprintf(h, "}");
  299. }
  300. else fprintf(h, "enum %s", name ? name : "");
  301. break;
  302. case TYPE_STRUCT:
  303. case TYPE_ENCAPSULATED_UNION:
  304. if (!declonly && !t->written) {
  305. assert(t->defined);
  306. if (name) fprintf(h, "struct %s {\n", name);
  307. else fprintf(h, "struct {\n");
  308. t->written = TRUE;
  309. indentation++;
  310. if (type_get_type(t) != TYPE_STRUCT)
  311. write_fields(h, type_encapsulated_union_get_fields(t));
  312. else
  313. write_fields(h, type_struct_get_fields(t));
  314. indent(h, -1);
  315. fprintf(h, "}");
  316. }
  317. else fprintf(h, "struct %s", name ? name : "");
  318. break;
  319. case TYPE_UNION:
  320. if (!declonly && !t->written) {
  321. assert(t->defined);
  322. if (t->name) fprintf(h, "union %s {\n", t->name);
  323. else fprintf(h, "union {\n");
  324. t->written = TRUE;
  325. indentation++;
  326. write_fields(h, type_union_get_cases(t));
  327. indent(h, -1);
  328. fprintf(h, "}");
  329. }
  330. else fprintf(h, "union %s", t->name ? t->name : "");
  331. break;
  332. case TYPE_POINTER:
  333. {
  334. write_type_left(h, type_pointer_get_ref(t), name_type, declonly, FALSE);
  335. write_pointer_left(h, type_pointer_get_ref_type(t));
  336. if (ds->qualifier & TYPE_QUALIFIER_CONST) fprintf(h, "const ");
  337. break;
  338. }
  339. case TYPE_ARRAY:
  340. if (t->name && type_array_is_decl_as_ptr(t))
  341. fprintf(h, "%s", t->name);
  342. else
  343. {
  344. write_type_left(h, type_array_get_element(t), name_type, declonly, !type_array_is_decl_as_ptr(t));
  345. if (type_array_is_decl_as_ptr(t))
  346. write_pointer_left(h, type_array_get_element_type(t));
  347. }
  348. break;
  349. case TYPE_FUNCTION:
  350. {
  351. write_type_left(h, type_function_get_ret(t), name_type, declonly, TRUE);
  352. /* A pointer to a function has to write the calling convention inside
  353. * the parentheses. There's no way to handle that here, so we have to
  354. * use an extra parameter to tell us whether to write the calling
  355. * convention or not. */
  356. if (write_callconv)
  357. {
  358. const char *callconv = get_attrp(t->attrs, ATTR_CALLCONV);
  359. if (!callconv && is_object_interface) callconv = "STDMETHODCALLTYPE";
  360. if (callconv) fprintf(h, " %s ", callconv);
  361. }
  362. break;
  363. }
  364. case TYPE_BASIC:
  365. if (type_basic_get_type(t) != TYPE_BASIC_INT32 &&
  366. type_basic_get_type(t) != TYPE_BASIC_INT64 &&
  367. type_basic_get_type(t) != TYPE_BASIC_LONG &&
  368. type_basic_get_type(t) != TYPE_BASIC_HYPER)
  369. {
  370. if (type_basic_get_sign(t) < 0) fprintf(h, "signed ");
  371. else if (type_basic_get_sign(t) > 0) fprintf(h, "unsigned ");
  372. }
  373. switch (type_basic_get_type(t))
  374. {
  375. case TYPE_BASIC_INT8: fprintf(h, "small"); break;
  376. case TYPE_BASIC_INT16: fprintf(h, "short"); break;
  377. case TYPE_BASIC_INT: fprintf(h, "int"); break;
  378. case TYPE_BASIC_INT3264: fprintf(h, "__int3264"); break;
  379. case TYPE_BASIC_BYTE: fprintf(h, "byte"); break;
  380. case TYPE_BASIC_CHAR: fprintf(h, "char"); break;
  381. case TYPE_BASIC_WCHAR: fprintf(h, "wchar_t"); break;
  382. case TYPE_BASIC_FLOAT: fprintf(h, "float"); break;
  383. case TYPE_BASIC_DOUBLE: fprintf(h, "double"); break;
  384. case TYPE_BASIC_ERROR_STATUS_T: fprintf(h, "error_status_t"); break;
  385. case TYPE_BASIC_HANDLE: fprintf(h, "handle_t"); break;
  386. case TYPE_BASIC_INT32:
  387. if (type_basic_get_sign(t) > 0)
  388. fprintf(h, "UINT32");
  389. else
  390. fprintf(h, "INT32");
  391. break;
  392. case TYPE_BASIC_LONG:
  393. if (type_basic_get_sign(t) > 0)
  394. fprintf(h, "ULONG");
  395. else
  396. fprintf(h, "LONG");
  397. break;
  398. case TYPE_BASIC_INT64:
  399. if (type_basic_get_sign(t) > 0)
  400. fprintf(h, "UINT64");
  401. else
  402. fprintf(h, "INT64");
  403. break;
  404. case TYPE_BASIC_HYPER:
  405. if (type_basic_get_sign(t) > 0)
  406. fprintf(h, "MIDL_uhyper");
  407. else
  408. fprintf(h, "hyper");
  409. break;
  410. }
  411. break;
  412. case TYPE_INTERFACE:
  413. case TYPE_MODULE:
  414. case TYPE_COCLASS:
  415. fprintf(h, "%s", t->name);
  416. break;
  417. case TYPE_VOID:
  418. fprintf(h, "void");
  419. break;
  420. case TYPE_BITFIELD:
  421. {
  422. const decl_spec_t ds = {.type = type_bitfield_get_field(t)};
  423. write_type_left(h, &ds, name_type, declonly, TRUE);
  424. break;
  425. }
  426. case TYPE_ALIAS:
  427. /* handled elsewhere */
  428. assert(0);
  429. break;
  430. }
  431. }
  432. }
  433. void write_type_right(FILE *h, type_t *t, int is_field)
  434. {
  435. if (!h) return;
  436. if (type_is_alias(t)) return;
  437. switch (type_get_type(t))
  438. {
  439. case TYPE_ARRAY:
  440. {
  441. type_t *elem = type_array_get_element_type(t);
  442. if (type_array_is_decl_as_ptr(t))
  443. {
  444. if (decl_needs_parens(elem))
  445. fprintf(h, ")");
  446. }
  447. else
  448. {
  449. if (is_conformant_array(t))
  450. fprintf(h, "[%s]", is_field ? "1" : "");
  451. else
  452. fprintf(h, "[%u]", type_array_get_dim(t));
  453. }
  454. write_type_right(h, elem, FALSE);
  455. break;
  456. }
  457. case TYPE_FUNCTION:
  458. {
  459. const var_list_t *args = type_function_get_args(t);
  460. fputc('(', h);
  461. if (args)
  462. write_args(h, args, NULL, 0, FALSE);
  463. else
  464. fprintf(h, "void");
  465. fputc(')', h);
  466. write_type_right(h, type_function_get_rettype(t), FALSE);
  467. break;
  468. }
  469. case TYPE_POINTER:
  470. {
  471. type_t *ref = type_pointer_get_ref_type(t);
  472. if (decl_needs_parens(ref))
  473. fprintf(h, ")");
  474. write_type_right(h, ref, FALSE);
  475. break;
  476. }
  477. case TYPE_BITFIELD:
  478. fprintf(h, " : %u", type_bitfield_get_bits(t)->cval);
  479. break;
  480. case TYPE_VOID:
  481. case TYPE_BASIC:
  482. case TYPE_ENUM:
  483. case TYPE_STRUCT:
  484. case TYPE_ENCAPSULATED_UNION:
  485. case TYPE_UNION:
  486. case TYPE_ALIAS:
  487. case TYPE_MODULE:
  488. case TYPE_COCLASS:
  489. case TYPE_INTERFACE:
  490. break;
  491. }
  492. }
  493. static void write_type_v(FILE *h, const decl_spec_t *ds, int is_field, int declonly, const char *name)
  494. {
  495. type_t *t = ds->type;
  496. if (!h) return;
  497. if (t)
  498. write_type_left(h, ds, NAME_DEFAULT, declonly, TRUE);
  499. if (name) fprintf(h, "%s%s", !t || needs_space_after(t) ? " " : "", name );
  500. if (t)
  501. write_type_right(h, t, is_field);
  502. }
  503. static void write_type_definition(FILE *f, type_t *t, int declonly)
  504. {
  505. int in_namespace = t->namespace && !is_global_namespace(t->namespace);
  506. int save_written = t->written;
  507. decl_spec_t ds = {.type = t};
  508. if(in_namespace) {
  509. fprintf(f, "#ifdef __cplusplus\n");
  510. fprintf(f, "} /* extern \"C\" */\n");
  511. write_namespace_start(f, t->namespace);
  512. }
  513. indent(f, 0);
  514. write_type_left(f, &ds, NAME_DEFAULT, declonly, TRUE);
  515. fprintf(f, ";\n");
  516. if(in_namespace) {
  517. t->written = save_written;
  518. write_namespace_end(f, t->namespace);
  519. fprintf(f, "extern \"C\" {\n");
  520. fprintf(f, "#else\n");
  521. write_type_left(f, &ds, NAME_C, declonly, TRUE);
  522. fprintf(f, ";\n");
  523. fprintf(f, "#endif\n\n");
  524. }
  525. }
  526. void write_type_decl(FILE *f, const decl_spec_t *t, const char *name)
  527. {
  528. write_type_v(f, t, FALSE, TRUE, name);
  529. }
  530. void write_type_decl_left(FILE *f, const decl_spec_t *ds)
  531. {
  532. write_type_left(f, ds, NAME_DEFAULT, TRUE, TRUE);
  533. }
  534. static int user_type_registered(const char *name)
  535. {
  536. user_type_t *ut;
  537. LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
  538. if (!strcmp(name, ut->name))
  539. return 1;
  540. return 0;
  541. }
  542. static int context_handle_registered(const char *name)
  543. {
  544. context_handle_t *ch;
  545. LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
  546. if (!strcmp(name, ch->name))
  547. return 1;
  548. return 0;
  549. }
  550. static int generic_handle_registered(const char *name)
  551. {
  552. generic_handle_t *gh;
  553. LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
  554. if (!strcmp(name, gh->name))
  555. return 1;
  556. return 0;
  557. }
  558. unsigned int get_context_handle_offset( const type_t *type )
  559. {
  560. context_handle_t *ch;
  561. unsigned int index = 0;
  562. while (!is_attr( type->attrs, ATTR_CONTEXTHANDLE ))
  563. {
  564. if (type_is_alias( type )) type = type_alias_get_aliasee_type( type );
  565. else if (is_ptr( type )) type = type_pointer_get_ref_type( type );
  566. else error( "internal error: %s is not a context handle\n", type->name );
  567. }
  568. LIST_FOR_EACH_ENTRY( ch, &context_handle_list, context_handle_t, entry )
  569. {
  570. if (!strcmp( type->name, ch->name )) return index;
  571. index++;
  572. }
  573. error( "internal error: %s is not registered as a context handle\n", type->name );
  574. return index;
  575. }
  576. unsigned int get_generic_handle_offset( const type_t *type )
  577. {
  578. generic_handle_t *gh;
  579. unsigned int index = 0;
  580. while (!is_attr( type->attrs, ATTR_HANDLE ))
  581. {
  582. if (type_is_alias( type )) type = type_alias_get_aliasee_type( type );
  583. else if (is_ptr( type )) type = type_pointer_get_ref_type( type );
  584. else error( "internal error: %s is not a generic handle\n", type->name );
  585. }
  586. LIST_FOR_EACH_ENTRY( gh, &generic_handle_list, generic_handle_t, entry )
  587. {
  588. if (!strcmp( type->name, gh->name )) return index;
  589. index++;
  590. }
  591. error( "internal error: %s is not registered as a generic handle\n", type->name );
  592. return index;
  593. }
  594. /* check for types which require additional prototypes to be generated in the
  595. * header */
  596. void check_for_additional_prototype_types(type_t *type)
  597. {
  598. if (!type) return;
  599. for (;;) {
  600. const char *name = type->name;
  601. if (type->user_types_registered) break;
  602. type->user_types_registered = 1;
  603. if (is_attr(type->attrs, ATTR_CONTEXTHANDLE)) {
  604. if (!context_handle_registered(name))
  605. {
  606. context_handle_t *ch = xmalloc(sizeof(*ch));
  607. ch->name = xstrdup(name);
  608. list_add_tail(&context_handle_list, &ch->entry);
  609. }
  610. /* don't carry on parsing fields within this type */
  611. break;
  612. }
  613. if ((type_get_type(type) != TYPE_BASIC ||
  614. type_basic_get_type(type) != TYPE_BASIC_HANDLE) &&
  615. is_attr(type->attrs, ATTR_HANDLE)) {
  616. if (!generic_handle_registered(name))
  617. {
  618. generic_handle_t *gh = xmalloc(sizeof(*gh));
  619. gh->name = xstrdup(name);
  620. list_add_tail(&generic_handle_list, &gh->entry);
  621. }
  622. /* don't carry on parsing fields within this type */
  623. break;
  624. }
  625. if (is_attr(type->attrs, ATTR_WIREMARSHAL)) {
  626. if (!user_type_registered(name))
  627. {
  628. user_type_t *ut = xmalloc(sizeof *ut);
  629. ut->name = xstrdup(name);
  630. list_add_tail(&user_type_list, &ut->entry);
  631. }
  632. /* don't carry on parsing fields within this type as we are already
  633. * using a wire marshaled type */
  634. break;
  635. }
  636. else if (type_is_complete(type))
  637. {
  638. var_list_t *vars;
  639. const var_t *v;
  640. switch (type_get_type_detect_alias(type))
  641. {
  642. case TYPE_ENUM:
  643. vars = type_enum_get_values(type);
  644. break;
  645. case TYPE_STRUCT:
  646. vars = type_struct_get_fields(type);
  647. break;
  648. case TYPE_UNION:
  649. vars = type_union_get_cases(type);
  650. break;
  651. default:
  652. vars = NULL;
  653. break;
  654. }
  655. if (vars) LIST_FOR_EACH_ENTRY( v, vars, const var_t, entry )
  656. check_for_additional_prototype_types(v->declspec.type);
  657. }
  658. if (type_is_alias(type))
  659. type = type_alias_get_aliasee_type(type);
  660. else if (is_ptr(type))
  661. type = type_pointer_get_ref_type(type);
  662. else if (is_array(type))
  663. type = type_array_get_element_type(type);
  664. else
  665. break;
  666. }
  667. }
  668. static int write_serialize_function_decl(FILE *header, const type_t *type)
  669. {
  670. write_serialize_functions(header, type, NULL);
  671. return 1;
  672. }
  673. static int serializable_exists(FILE *header, const type_t *type)
  674. {
  675. return 0;
  676. }
  677. static int for_each_serializable(const statement_list_t *stmts, FILE *header,
  678. int (*proc)(FILE*, const type_t*))
  679. {
  680. statement_t *stmt, *iface_stmt;
  681. statement_list_t *iface_stmts;
  682. const type_list_t *type_entry;
  683. if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, statement_t, entry )
  684. {
  685. if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
  686. continue;
  687. iface_stmts = type_iface_get_stmts(stmt->u.type);
  688. if (iface_stmts) LIST_FOR_EACH_ENTRY( iface_stmt, iface_stmts, statement_t, entry )
  689. {
  690. if (iface_stmt->type != STMT_TYPEDEF) continue;
  691. for (type_entry = iface_stmt->u.type_list; type_entry; type_entry = type_entry->next)
  692. {
  693. if (!is_attr(type_entry->type->attrs, ATTR_ENCODE)
  694. && !is_attr(type_entry->type->attrs, ATTR_DECODE))
  695. continue;
  696. if (!proc(header, type_entry->type))
  697. return 0;
  698. }
  699. }
  700. }
  701. return 1;
  702. }
  703. static void write_user_types(FILE *header)
  704. {
  705. user_type_t *ut;
  706. LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
  707. {
  708. const char *name = ut->name;
  709. fprintf(header, "ULONG __RPC_USER %s_UserSize (ULONG *, ULONG, %s *);\n", name, name);
  710. fprintf(header, "unsigned char * __RPC_USER %s_UserMarshal (ULONG *, unsigned char *, %s *);\n", name, name);
  711. fprintf(header, "unsigned char * __RPC_USER %s_UserUnmarshal(ULONG *, unsigned char *, %s *);\n", name, name);
  712. fprintf(header, "void __RPC_USER %s_UserFree (ULONG *, %s *);\n", name, name);
  713. }
  714. }
  715. static void write_context_handle_rundowns(FILE *header)
  716. {
  717. context_handle_t *ch;
  718. LIST_FOR_EACH_ENTRY(ch, &context_handle_list, context_handle_t, entry)
  719. {
  720. const char *name = ch->name;
  721. fprintf(header, "void __RPC_USER %s_rundown(%s);\n", name, name);
  722. }
  723. }
  724. static void write_generic_handle_routines(FILE *header)
  725. {
  726. generic_handle_t *gh;
  727. LIST_FOR_EACH_ENTRY(gh, &generic_handle_list, generic_handle_t, entry)
  728. {
  729. const char *name = gh->name;
  730. fprintf(header, "handle_t __RPC_USER %s_bind(%s);\n", name, name);
  731. fprintf(header, "void __RPC_USER %s_unbind(%s, handle_t);\n", name, name);
  732. }
  733. }
  734. static void write_typedef(FILE *header, type_t *type, int declonly)
  735. {
  736. fprintf(header, "typedef ");
  737. write_type_v(header, type_alias_get_aliasee(type), FALSE, declonly, type->name);
  738. fprintf(header, ";\n");
  739. }
  740. int is_const_decl(const var_t *var)
  741. {
  742. const decl_spec_t *t;
  743. /* strangely, MIDL accepts a const attribute on any pointer in the
  744. * declaration to mean that data isn't being instantiated. this appears
  745. * to be a bug, but there is no benefit to being incompatible with MIDL,
  746. * so we'll do the same thing */
  747. for (t = &var->declspec; ; )
  748. {
  749. if (t->qualifier & TYPE_QUALIFIER_CONST)
  750. return TRUE;
  751. else if (is_ptr(t->type))
  752. t = type_pointer_get_ref(t->type);
  753. else break;
  754. }
  755. return FALSE;
  756. }
  757. static void write_declaration(FILE *header, const var_t *v)
  758. {
  759. if (is_const_decl(v) && v->eval)
  760. {
  761. fprintf(header, "#define %s (", v->name);
  762. write_expr(header, v->eval, 0, 1, NULL, NULL, "");
  763. fprintf(header, ")\n\n");
  764. }
  765. else
  766. {
  767. switch (v->declspec.stgclass)
  768. {
  769. case STG_NONE:
  770. case STG_REGISTER: /* ignored */
  771. break;
  772. case STG_STATIC:
  773. fprintf(header, "static ");
  774. break;
  775. case STG_EXTERN:
  776. fprintf(header, "extern ");
  777. break;
  778. }
  779. write_type_v(header, &v->declspec, FALSE, v->declonly, v->name);
  780. fprintf(header, ";\n\n");
  781. }
  782. }
  783. static void write_library(FILE *header, const typelib_t *typelib)
  784. {
  785. const UUID *uuid = get_attrp(typelib->attrs, ATTR_UUID);
  786. fprintf(header, "\n");
  787. write_guid(header, "LIBID", typelib->name, uuid);
  788. fprintf(header, "\n");
  789. }
  790. const type_t* get_explicit_generic_handle_type(const var_t* var)
  791. {
  792. const type_t *t;
  793. for (t = var->declspec.type;
  794. is_ptr(t) || type_is_alias(t);
  795. t = type_is_alias(t) ? type_alias_get_aliasee_type(t) : type_pointer_get_ref_type(t))
  796. if ((type_get_type_detect_alias(t) != TYPE_BASIC || type_basic_get_type(t) != TYPE_BASIC_HANDLE) &&
  797. is_attr(t->attrs, ATTR_HANDLE))
  798. return t;
  799. return NULL;
  800. }
  801. const var_t *get_func_handle_var( const type_t *iface, const var_t *func,
  802. unsigned char *explicit_fc, unsigned char *implicit_fc )
  803. {
  804. const var_t *var;
  805. const var_list_t *args = type_function_get_args( func->declspec.type );
  806. *explicit_fc = *implicit_fc = 0;
  807. if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
  808. {
  809. if (!is_attr( var->attrs, ATTR_IN ) && is_attr( var->attrs, ATTR_OUT )) continue;
  810. if (type_get_type( var->declspec.type ) == TYPE_BASIC && type_basic_get_type( var->declspec.type ) == TYPE_BASIC_HANDLE)
  811. {
  812. *explicit_fc = FC_BIND_PRIMITIVE;
  813. return var;
  814. }
  815. if (get_explicit_generic_handle_type( var ))
  816. {
  817. *explicit_fc = FC_BIND_GENERIC;
  818. return var;
  819. }
  820. if (is_context_handle( var->declspec.type ))
  821. {
  822. *explicit_fc = FC_BIND_CONTEXT;
  823. return var;
  824. }
  825. }
  826. if ((var = get_attrp( iface->attrs, ATTR_IMPLICIT_HANDLE )))
  827. {
  828. if (type_get_type( var->declspec.type ) == TYPE_BASIC &&
  829. type_basic_get_type( var->declspec.type ) == TYPE_BASIC_HANDLE)
  830. *implicit_fc = FC_BIND_PRIMITIVE;
  831. else
  832. *implicit_fc = FC_BIND_GENERIC;
  833. return var;
  834. }
  835. *implicit_fc = FC_AUTO_HANDLE;
  836. return NULL;
  837. }
  838. int has_out_arg_or_return(const var_t *func)
  839. {
  840. const var_t *var;
  841. if (!is_void(type_function_get_rettype(func->declspec.type)))
  842. return 1;
  843. if (!type_function_get_args(func->declspec.type))
  844. return 0;
  845. LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
  846. if (is_attr(var->attrs, ATTR_OUT))
  847. return 1;
  848. return 0;
  849. }
  850. /********** INTERFACES **********/
  851. int is_object(const type_t *iface)
  852. {
  853. const attr_t *attr;
  854. if (type_is_defined(iface) && type_iface_get_inherit(iface))
  855. return 1;
  856. if (iface->attrs) LIST_FOR_EACH_ENTRY( attr, iface->attrs, const attr_t, entry )
  857. if (attr->type == ATTR_OBJECT || attr->type == ATTR_ODL) return 1;
  858. return 0;
  859. }
  860. int is_local(const attr_list_t *a)
  861. {
  862. return is_attr(a, ATTR_LOCAL);
  863. }
  864. const var_t *is_callas(const attr_list_t *a)
  865. {
  866. return get_attrp(a, ATTR_CALLAS);
  867. }
  868. static int is_inherited_method(const type_t *iface, const var_t *func)
  869. {
  870. while ((iface = type_iface_get_inherit(iface)))
  871. {
  872. const statement_t *stmt;
  873. STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
  874. {
  875. const var_t *funccmp = stmt->u.var;
  876. if (!is_callas(func->attrs))
  877. {
  878. char inherit_name[256];
  879. /* compare full name including property prefix */
  880. strcpy(inherit_name, get_name(funccmp));
  881. if (!strcmp(inherit_name, get_name(func))) return 1;
  882. }
  883. }
  884. }
  885. return 0;
  886. }
  887. static int is_override_method(const type_t *iface, const type_t *child, const var_t *func)
  888. {
  889. if (iface == child)
  890. return 0;
  891. do
  892. {
  893. const statement_t *stmt;
  894. STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(child))
  895. {
  896. const var_t *funccmp = stmt->u.var;
  897. if (!is_callas(func->attrs))
  898. {
  899. char inherit_name[256];
  900. /* compare full name including property prefix */
  901. strcpy(inherit_name, get_name(funccmp));
  902. if (!strcmp(inherit_name, get_name(func))) return 1;
  903. }
  904. }
  905. }
  906. while ((child = type_iface_get_inherit(child)) && child != iface);
  907. return 0;
  908. }
  909. static int is_aggregate_return(const var_t *func)
  910. {
  911. enum type_type type = type_get_type(type_function_get_rettype(func->declspec.type));
  912. return type == TYPE_STRUCT || type == TYPE_UNION ||
  913. type == TYPE_COCLASS || type == TYPE_INTERFACE;
  914. }
  915. static char *get_vtbl_entry_name(const type_t *iface, const var_t *func)
  916. {
  917. static char buff[255];
  918. if (is_inherited_method(iface, func))
  919. sprintf(buff, "%s_%s", iface->name, get_name(func));
  920. else
  921. sprintf(buff, "%s", get_name(func));
  922. return buff;
  923. }
  924. static void write_method_macro(FILE *header, const type_t *iface, const type_t *child, const char *name)
  925. {
  926. const statement_t *stmt;
  927. int first_iface = 1;
  928. if (type_iface_get_inherit(iface))
  929. write_method_macro(header, type_iface_get_inherit(iface), child, name);
  930. STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
  931. {
  932. const var_t *func = stmt->u.var;
  933. if (first_iface)
  934. {
  935. fprintf(header, "/*** %s methods ***/\n", iface->name);
  936. first_iface = 0;
  937. }
  938. if (is_override_method(iface, child, func))
  939. continue;
  940. if (!is_callas(func->attrs)) {
  941. const var_t *arg;
  942. fprintf(header, "#define %s_%s(This", name, get_name(func));
  943. if (type_function_get_args(func->declspec.type))
  944. LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), const var_t, entry )
  945. fprintf(header, ",%s", arg->name);
  946. fprintf(header, ") ");
  947. if (is_aggregate_return(func))
  948. {
  949. fprintf(header, "%s_%s_define_WIDL_C_INLINE_WRAPPERS_for_aggregate_return_support\n", name, get_name(func));
  950. continue;
  951. }
  952. fprintf(header, "(This)->lpVtbl->%s(This", get_vtbl_entry_name(iface, func));
  953. if (type_function_get_args(func->declspec.type))
  954. LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), const var_t, entry )
  955. fprintf(header, ",%s", arg->name);
  956. fprintf(header, ")\n");
  957. }
  958. }
  959. }
  960. void write_args(FILE *h, const var_list_t *args, const char *name, int method, int do_indent)
  961. {
  962. const var_t *arg;
  963. int count = 0;
  964. if (do_indent)
  965. {
  966. indentation++;
  967. indent(h, 0);
  968. }
  969. if (method == 1) {
  970. fprintf(h, "%s* This", name);
  971. count++;
  972. }
  973. if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry ) {
  974. if (count) {
  975. if (do_indent)
  976. {
  977. fprintf(h, ",\n");
  978. indent(h, 0);
  979. }
  980. else fprintf(h, ",");
  981. }
  982. /* In theory we should be writing the definition using write_type_v(..., arg->declonly),
  983. * but that causes redefinition in e.g. proxy files. In fact MIDL disallows
  984. * defining UDTs inside of an argument list. */
  985. write_type_decl(h, &arg->declspec, arg->name);
  986. if (method == 2) {
  987. const expr_t *expr = get_attrp(arg->attrs, ATTR_DEFAULTVALUE);
  988. if (expr) {
  989. const var_t *tail_arg;
  990. /* Output default value only if all following arguments also have default value. */
  991. LIST_FOR_EACH_ENTRY_REV( tail_arg, args, const var_t, entry ) {
  992. if(tail_arg == arg) {
  993. expr_t bstr;
  994. /* Fixup the expression type for a BSTR like midl does. */
  995. if (get_type_vt(arg->declspec.type) == VT_BSTR && expr->type == EXPR_STRLIT)
  996. {
  997. bstr = *expr;
  998. bstr.type = EXPR_WSTRLIT;
  999. expr = &bstr;
  1000. }
  1001. fprintf(h, " = ");
  1002. write_expr( h, expr, 0, 1, NULL, NULL, "" );
  1003. break;
  1004. }
  1005. if(!get_attrp(tail_arg->attrs, ATTR_DEFAULTVALUE))
  1006. break;
  1007. }
  1008. }
  1009. }
  1010. count++;
  1011. }
  1012. if (do_indent) indentation--;
  1013. }
  1014. static void write_cpp_method_def(FILE *header, const type_t *iface)
  1015. {
  1016. const statement_t *stmt;
  1017. STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
  1018. {
  1019. const var_t *func = stmt->u.var;
  1020. if (!is_callas(func->attrs)) {
  1021. const decl_spec_t *ret = type_function_get_ret(func->declspec.type);
  1022. const char *callconv = get_attrp(func->declspec.type->attrs, ATTR_CALLCONV);
  1023. const var_list_t *args = type_function_get_args(func->declspec.type);
  1024. const var_t *arg;
  1025. if (!callconv) callconv = "STDMETHODCALLTYPE";
  1026. if (is_aggregate_return(func)) {
  1027. fprintf(header, "#ifdef WIDL_EXPLICIT_AGGREGATE_RETURNS\n");
  1028. indent(header, 0);
  1029. fprintf(header, "virtual ");
  1030. write_type_decl_left(header, ret);
  1031. fprintf(header, "* %s %s(\n", callconv, get_name(func));
  1032. ++indentation;
  1033. indent(header, 0);
  1034. write_type_decl_left(header, ret);
  1035. fprintf(header, " *__ret");
  1036. --indentation;
  1037. if (args) {
  1038. fprintf(header, ",\n");
  1039. write_args(header, args, iface->name, 2, TRUE);
  1040. }
  1041. fprintf(header, ") = 0;\n");
  1042. indent(header, 0);
  1043. write_type_decl_left(header, ret);
  1044. fprintf(header, " %s %s(\n", callconv, get_name(func));
  1045. write_args(header, args, iface->name, 2, TRUE);
  1046. fprintf(header, ")\n");
  1047. indent(header, 0);
  1048. fprintf(header, "{\n");
  1049. ++indentation;
  1050. indent(header, 0);
  1051. write_type_decl_left(header, ret);
  1052. fprintf(header, " __ret;\n");
  1053. indent(header, 0);
  1054. fprintf(header, "return *%s(&__ret", get_name(func));
  1055. if (args)
  1056. LIST_FOR_EACH_ENTRY(arg, args, const var_t, entry)
  1057. fprintf(header, ", %s", arg->name);
  1058. fprintf(header, ");\n");
  1059. --indentation;
  1060. indent(header, 0);
  1061. fprintf(header, "}\n");
  1062. fprintf(header, "#else\n");
  1063. }
  1064. indent(header, 0);
  1065. fprintf(header, "virtual ");
  1066. write_type_decl_left(header, ret);
  1067. fprintf(header, " %s %s(\n", callconv, get_name(func));
  1068. write_args(header, args, iface->name, 2, TRUE);
  1069. fprintf(header, ") = 0;\n");
  1070. if (is_aggregate_return(func))
  1071. fprintf(header, "#endif\n");
  1072. fprintf(header, "\n");
  1073. }
  1074. }
  1075. }
  1076. static void write_inline_wrappers(FILE *header, const type_t *iface, const type_t *child, const char *name)
  1077. {
  1078. const statement_t *stmt;
  1079. int first_iface = 1;
  1080. if (type_iface_get_inherit(iface))
  1081. write_inline_wrappers(header, type_iface_get_inherit(iface), child, name);
  1082. STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
  1083. {
  1084. const var_t *func = stmt->u.var;
  1085. if (first_iface)
  1086. {
  1087. fprintf(header, "/*** %s methods ***/\n", iface->name);
  1088. first_iface = 0;
  1089. }
  1090. if (is_override_method(iface, child, func))
  1091. continue;
  1092. if (!is_callas(func->attrs)) {
  1093. const var_t *arg;
  1094. fprintf(header, "static FORCEINLINE ");
  1095. write_type_decl_left(header, type_function_get_ret(func->declspec.type));
  1096. fprintf(header, " %s_%s(", name, get_name(func));
  1097. write_args(header, type_function_get_args(func->declspec.type), name, 1, FALSE);
  1098. fprintf(header, ") {\n");
  1099. ++indentation;
  1100. if (!is_aggregate_return(func)) {
  1101. indent(header, 0);
  1102. fprintf(header, "%sThis->lpVtbl->%s(This",
  1103. is_void(type_function_get_rettype(func->declspec.type)) ? "" : "return ",
  1104. get_vtbl_entry_name(iface, func));
  1105. } else {
  1106. indent(header, 0);
  1107. write_type_decl_left(header, type_function_get_ret(func->declspec.type));
  1108. fprintf(header, " __ret;\n");
  1109. indent(header, 0);
  1110. fprintf(header, "return *This->lpVtbl->%s(This,&__ret", get_vtbl_entry_name(iface, func));
  1111. }
  1112. if (type_function_get_args(func->declspec.type))
  1113. LIST_FOR_EACH_ENTRY( arg, type_function_get_args(func->declspec.type), const var_t, entry )
  1114. fprintf(header, ",%s", arg->name);
  1115. fprintf(header, ");\n");
  1116. --indentation;
  1117. fprintf(header, "}\n");
  1118. }
  1119. }
  1120. }
  1121. static void do_write_c_method_def(FILE *header, const type_t *iface, const char *name)
  1122. {
  1123. const statement_t *stmt;
  1124. int first_iface = 1;
  1125. if (type_iface_get_inherit(iface))
  1126. do_write_c_method_def(header, type_iface_get_inherit(iface), name);
  1127. STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
  1128. {
  1129. const var_t *func = stmt->u.var;
  1130. if (first_iface) {
  1131. indent(header, 0);
  1132. fprintf(header, "/*** %s methods ***/\n", iface->name);
  1133. first_iface = 0;
  1134. }
  1135. if (!is_callas(func->attrs)) {
  1136. const char *callconv = get_attrp(func->declspec.type->attrs, ATTR_CALLCONV);
  1137. if (!callconv) callconv = "STDMETHODCALLTYPE";
  1138. indent(header, 0);
  1139. write_type_decl_left(header, type_function_get_ret(func->declspec.type));
  1140. if (is_aggregate_return(func))
  1141. fprintf(header, " *");
  1142. if (is_inherited_method(iface, func))
  1143. fprintf(header, " (%s *%s_%s)(\n", callconv, iface->name, func->name);
  1144. else
  1145. fprintf(header, " (%s *%s)(\n", callconv, get_name(func));
  1146. ++indentation;
  1147. indent(header, 0);
  1148. fprintf(header, "%s *This", name);
  1149. if (is_aggregate_return(func)) {
  1150. fprintf(header, ",\n");
  1151. indent(header, 0);
  1152. write_type_decl_left(header, type_function_get_ret(func->declspec.type));
  1153. fprintf(header, " *__ret");
  1154. }
  1155. --indentation;
  1156. if (type_function_get_args(func->declspec.type)) {
  1157. fprintf(header, ",\n");
  1158. write_args(header, type_function_get_args(func->declspec.type), name, 0, TRUE);
  1159. }
  1160. fprintf(header, ");\n");
  1161. fprintf(header, "\n");
  1162. }
  1163. }
  1164. }
  1165. static void write_c_method_def(FILE *header, const type_t *iface)
  1166. {
  1167. do_write_c_method_def(header, iface, iface->c_name);
  1168. }
  1169. static void write_c_disp_method_def(FILE *header, const type_t *iface)
  1170. {
  1171. do_write_c_method_def(header, type_iface_get_inherit(iface), iface->c_name);
  1172. }
  1173. static void write_method_proto(FILE *header, const type_t *iface)
  1174. {
  1175. const statement_t *stmt;
  1176. STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
  1177. {
  1178. const var_t *func = stmt->u.var;
  1179. if (is_callas(func->attrs)) {
  1180. const char *callconv = get_attrp(func->declspec.type->attrs, ATTR_CALLCONV);
  1181. if (!callconv) callconv = "STDMETHODCALLTYPE";
  1182. /* proxy prototype */
  1183. write_type_decl_left(header, type_function_get_ret(func->declspec.type));
  1184. fprintf(header, " %s %s_%s_Proxy(\n", callconv, iface->name, get_name(func));
  1185. write_args(header, type_function_get_args(func->declspec.type), iface->name, 1, TRUE);
  1186. fprintf(header, ");\n");
  1187. /* stub prototype */
  1188. fprintf(header, "void __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(func));
  1189. fprintf(header, " IRpcStubBuffer* This,\n");
  1190. fprintf(header, " IRpcChannelBuffer* pRpcChannelBuffer,\n");
  1191. fprintf(header, " PRPC_MESSAGE pRpcMessage,\n");
  1192. fprintf(header, " DWORD* pdwStubPhase);\n");
  1193. }
  1194. }
  1195. }
  1196. static void write_locals(FILE *fp, const type_t *iface, int body)
  1197. {
  1198. static const char comment[]
  1199. = "/* WIDL-generated stub. You must provide an implementation for this. */";
  1200. const statement_t *stmt;
  1201. if (!is_object(iface))
  1202. return;
  1203. STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface)) {
  1204. const var_t *func = stmt->u.var;
  1205. const var_t *cas = is_callas(func->attrs);
  1206. if (cas) {
  1207. const statement_t *stmt2 = NULL;
  1208. STATEMENTS_FOR_EACH_FUNC(stmt2, type_iface_get_stmts(iface))
  1209. if (!strcmp(get_name(stmt2->u.var), cas->name))
  1210. break;
  1211. if (&stmt2->entry != type_iface_get_stmts(iface)) {
  1212. const var_t *m = stmt2->u.var;
  1213. /* proxy prototype - use local prototype */
  1214. write_type_decl_left(fp, type_function_get_ret(m->declspec.type));
  1215. fprintf(fp, " CALLBACK %s_%s_Proxy(\n", iface->name, get_name(m));
  1216. write_args(fp, type_function_get_args(m->declspec.type), iface->name, 1, TRUE);
  1217. fprintf(fp, ")");
  1218. if (body) {
  1219. const decl_spec_t *rt = type_function_get_ret(m->declspec.type);
  1220. fprintf(fp, "\n{\n");
  1221. fprintf(fp, " %s\n", comment);
  1222. if (rt->type->name && strcmp(rt->type->name, "HRESULT") == 0)
  1223. fprintf(fp, " return E_NOTIMPL;\n");
  1224. else if (type_get_type(rt->type) != TYPE_VOID) {
  1225. fprintf(fp, " ");
  1226. write_type_decl(fp, rt, "rv");
  1227. fprintf(fp, ";\n");
  1228. fprintf(fp, " memset(&rv, 0, sizeof rv);\n");
  1229. fprintf(fp, " return rv;\n");
  1230. }
  1231. fprintf(fp, "}\n\n");
  1232. }
  1233. else
  1234. fprintf(fp, ";\n");
  1235. /* stub prototype - use remotable prototype */
  1236. write_type_decl_left(fp, type_function_get_ret(func->declspec.type));
  1237. fprintf(fp, " __RPC_STUB %s_%s_Stub(\n", iface->name, get_name(m));
  1238. write_args(fp, type_function_get_args(func->declspec.type), iface->name, 1, TRUE);
  1239. fprintf(fp, ")");
  1240. if (body)
  1241. /* Remotable methods must all return HRESULTs. */
  1242. fprintf(fp, "\n{\n %s\n return E_NOTIMPL;\n}\n\n", comment);
  1243. else
  1244. fprintf(fp, ";\n");
  1245. }
  1246. else
  1247. error_loc("invalid call_as attribute (%s -> %s)\n", func->name, cas->name);
  1248. }
  1249. }
  1250. }
  1251. static void write_local_stubs_stmts(FILE *local_stubs, const statement_list_t *stmts)
  1252. {
  1253. const statement_t *stmt;
  1254. if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
  1255. {
  1256. if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
  1257. write_locals(local_stubs, stmt->u.type, TRUE);
  1258. }
  1259. }
  1260. void write_local_stubs(const statement_list_t *stmts)
  1261. {
  1262. FILE *local_stubs;
  1263. if (!local_stubs_name) return;
  1264. local_stubs = fopen(local_stubs_name, "w");
  1265. if (!local_stubs) {
  1266. error("Could not open %s for output\n", local_stubs_name);
  1267. return;
  1268. }
  1269. fprintf(local_stubs, "/* call_as/local stubs for %s */\n\n", input_name);
  1270. fprintf(local_stubs, "#include <objbase.h>\n");
  1271. fprintf(local_stubs, "#include \"%s\"\n\n", header_name);
  1272. write_local_stubs_stmts(local_stubs, stmts);
  1273. fclose(local_stubs);
  1274. }
  1275. static void write_function_proto(FILE *header, const type_t *iface, const var_t *fun, const char *prefix)
  1276. {
  1277. const char *callconv = get_attrp(fun->declspec.type->attrs, ATTR_CALLCONV);
  1278. if (!callconv) callconv = "__cdecl";
  1279. /* FIXME: do we need to handle call_as? */
  1280. write_type_decl_left(header, type_function_get_ret(fun->declspec.type));
  1281. fprintf(header, " %s ", callconv);
  1282. fprintf(header, "%s%s(\n", prefix, get_name(fun));
  1283. if (type_function_get_args(fun->declspec.type))
  1284. write_args(header, type_function_get_args(fun->declspec.type), iface->name, 0, TRUE);
  1285. else
  1286. fprintf(header, " void");
  1287. fprintf(header, ");\n\n");
  1288. }
  1289. static void write_forward(FILE *header, type_t *iface)
  1290. {
  1291. fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", iface->c_name);
  1292. fprintf(header, "#define __%s_FWD_DEFINED__\n", iface->c_name);
  1293. fprintf(header, "typedef interface %s %s;\n", iface->c_name, iface->c_name);
  1294. fprintf(header, "#ifdef __cplusplus\n");
  1295. write_namespace_start(header, iface->namespace);
  1296. write_line(header, 0, "interface %s;", iface->name);
  1297. write_namespace_end(header, iface->namespace);
  1298. fprintf(header, "#endif /* __cplusplus */\n");
  1299. fprintf(header, "#endif\n\n" );
  1300. }
  1301. static void write_com_interface_start(FILE *header, const type_t *iface)
  1302. {
  1303. int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
  1304. fprintf(header, "/*****************************************************************************\n");
  1305. fprintf(header, " * %s %sinterface\n", iface->name, dispinterface ? "disp" : "");
  1306. fprintf(header, " */\n");
  1307. fprintf(header,"#ifndef __%s_%sINTERFACE_DEFINED__\n", iface->c_name, dispinterface ? "DISP" : "");
  1308. fprintf(header,"#define __%s_%sINTERFACE_DEFINED__\n\n", iface->c_name, dispinterface ? "DISP" : "");
  1309. }
  1310. static void write_com_interface_end(FILE *header, type_t *iface)
  1311. {
  1312. int dispinterface = is_attr(iface->attrs, ATTR_DISPINTERFACE);
  1313. const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
  1314. type_t *type;
  1315. if (uuid)
  1316. write_guid(header, dispinterface ? "DIID" : "IID", iface->c_name, uuid);
  1317. /* C++ interface */
  1318. fprintf(header, "#if defined(__cplusplus) && !defined(CINTERFACE)\n");
  1319. if (!is_global_namespace(iface->namespace)) {
  1320. write_line(header, 0, "} /* extern \"C\" */");
  1321. write_namespace_start(header, iface->namespace);
  1322. }
  1323. if (uuid) {
  1324. write_line(header, 0, "MIDL_INTERFACE(\"%s\")", uuid_string(uuid));
  1325. indent(header, 0);
  1326. }else {
  1327. indent(header, 0);
  1328. fprintf(header, "interface ");
  1329. }
  1330. if (type_iface_get_inherit(iface))
  1331. {
  1332. fprintf(header, "%s : public %s\n", iface->name,
  1333. type_iface_get_inherit(iface)->name);
  1334. write_line(header, 1, "{");
  1335. }
  1336. else
  1337. {
  1338. fprintf(header, "%s\n", iface->name);
  1339. write_line(header, 1, "{\n");
  1340. write_line(header, 0, "BEGIN_INTERFACE\n");
  1341. }
  1342. /* dispinterfaces don't have real functions, so don't write C++ functions for
  1343. * them */
  1344. if (!dispinterface)
  1345. write_cpp_method_def(header, iface);
  1346. if (!type_iface_get_inherit(iface))
  1347. write_line(header, 0, "END_INTERFACE\n");
  1348. write_line(header, -1, "};");
  1349. if (!is_global_namespace(iface->namespace)) {
  1350. write_namespace_end(header, iface->namespace);
  1351. write_line(header, 0, "extern \"C\" {");
  1352. }
  1353. if (uuid)
  1354. write_uuid_decl(header, iface, uuid);
  1355. fprintf(header, "#else\n");
  1356. /* C interface */
  1357. write_line(header, 1, "typedef struct %sVtbl {", iface->c_name);
  1358. write_line(header, 0, "BEGIN_INTERFACE\n");
  1359. if (dispinterface)
  1360. write_c_disp_method_def(header, iface);
  1361. else
  1362. write_c_method_def(header, iface);
  1363. write_line(header, 0, "END_INTERFACE");
  1364. write_line(header, -1, "} %sVtbl;\n", iface->c_name);
  1365. fprintf(header, "interface %s {\n", iface->c_name);
  1366. fprintf(header, " CONST_VTBL %sVtbl* lpVtbl;\n", iface->c_name);
  1367. fprintf(header, "};\n\n");
  1368. fprintf(header, "#ifdef COBJMACROS\n");
  1369. /* dispinterfaces don't have real functions, so don't write macros for them,
  1370. * only for the interface this interface inherits from, i.e. IDispatch */
  1371. fprintf(header, "#ifndef WIDL_C_INLINE_WRAPPERS\n");
  1372. type = dispinterface ? type_iface_get_inherit(iface) : iface;
  1373. write_method_macro(header, type, type, iface->c_name);
  1374. fprintf(header, "#else\n");
  1375. write_inline_wrappers(header, type, type, iface->c_name);
  1376. fprintf(header, "#endif\n");
  1377. fprintf(header, "#endif\n");
  1378. fprintf(header, "\n");
  1379. fprintf(header, "#endif\n");
  1380. fprintf(header, "\n");
  1381. /* dispinterfaces don't have real functions, so don't write prototypes for
  1382. * them */
  1383. if (!dispinterface && !winrt_mode)
  1384. {
  1385. write_method_proto(header, iface);
  1386. write_locals(header, iface, FALSE);
  1387. fprintf(header, "\n");
  1388. }
  1389. fprintf(header,"#endif /* __%s_%sINTERFACE_DEFINED__ */\n\n", iface->c_name, dispinterface ? "DISP" : "");
  1390. }
  1391. static void write_rpc_interface_start(FILE *header, const type_t *iface)
  1392. {
  1393. unsigned int ver = get_attrv(iface->attrs, ATTR_VERSION);
  1394. const var_t *var = get_attrp(iface->attrs, ATTR_IMPLICIT_HANDLE);
  1395. fprintf(header, "/*****************************************************************************\n");
  1396. fprintf(header, " * %s interface (v%d.%d)\n", iface->name, MAJORVERSION(ver), MINORVERSION(ver));
  1397. fprintf(header, " */\n");
  1398. fprintf(header,"#ifndef __%s_INTERFACE_DEFINED__\n", iface->name);
  1399. fprintf(header,"#define __%s_INTERFACE_DEFINED__\n\n", iface->name);
  1400. if (var)
  1401. {
  1402. fprintf(header, "extern ");
  1403. write_type_decl( header, &var->declspec, var->name );
  1404. fprintf(header, ";\n");
  1405. }
  1406. if (old_names)
  1407. {
  1408. fprintf(header, "extern RPC_IF_HANDLE %s%s_ClientIfHandle;\n", prefix_client, iface->name);
  1409. fprintf(header, "extern RPC_IF_HANDLE %s%s_ServerIfHandle;\n", prefix_server, iface->name);
  1410. }
  1411. else
  1412. {
  1413. fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_c_ifspec;\n",
  1414. prefix_client, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
  1415. fprintf(header, "extern RPC_IF_HANDLE %s%s_v%d_%d_s_ifspec;\n",
  1416. prefix_server, iface->name, MAJORVERSION(ver), MINORVERSION(ver));
  1417. }
  1418. }
  1419. static void write_rpc_interface_end(FILE *header, const type_t *iface)
  1420. {
  1421. fprintf(header,"\n#endif /* __%s_INTERFACE_DEFINED__ */\n\n", iface->name);
  1422. }
  1423. static void write_coclass(FILE *header, type_t *cocl)
  1424. {
  1425. const UUID *uuid = get_attrp(cocl->attrs, ATTR_UUID);
  1426. fprintf(header, "/*****************************************************************************\n");
  1427. fprintf(header, " * %s coclass\n", cocl->name);
  1428. fprintf(header, " */\n\n");
  1429. if (uuid)
  1430. write_guid(header, "CLSID", cocl->name, uuid);
  1431. fprintf(header, "\n#ifdef __cplusplus\n");
  1432. if (uuid)
  1433. {
  1434. fprintf(header, "class DECLSPEC_UUID(\"%s\") %s;\n", uuid_string(uuid), cocl->name);
  1435. write_uuid_decl(header, cocl, uuid);
  1436. }
  1437. else
  1438. {
  1439. fprintf(header, "class %s;\n", cocl->name);
  1440. }
  1441. fprintf(header, "#endif\n");
  1442. fprintf(header, "\n");
  1443. }
  1444. static void write_coclass_forward(FILE *header, type_t *cocl)
  1445. {
  1446. fprintf(header, "#ifndef __%s_FWD_DEFINED__\n", cocl->name);
  1447. fprintf(header, "#define __%s_FWD_DEFINED__\n", cocl->name);
  1448. fprintf(header, "#ifdef __cplusplus\n");
  1449. fprintf(header, "typedef class %s %s;\n", cocl->name, cocl->name);
  1450. fprintf(header, "#else\n");
  1451. fprintf(header, "typedef struct %s %s;\n", cocl->name, cocl->name);
  1452. fprintf(header, "#endif /* defined __cplusplus */\n");
  1453. fprintf(header, "#endif /* defined __%s_FWD_DEFINED__ */\n\n", cocl->name );
  1454. }
  1455. static void write_import(FILE *header, const char *fname)
  1456. {
  1457. char *hname, *p;
  1458. hname = dup_basename(fname, ".idl");
  1459. p = hname + strlen(hname) - 2;
  1460. if (p <= hname || strcmp( p, ".h" )) strcat(hname, ".h");
  1461. fprintf(header, "#include <%s>\n", hname);
  1462. free(hname);
  1463. }
  1464. static void write_imports(FILE *header, const statement_list_t *stmts)
  1465. {
  1466. const statement_t *stmt;
  1467. if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
  1468. {
  1469. switch (stmt->type)
  1470. {
  1471. case STMT_TYPE:
  1472. if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
  1473. write_imports(header, type_iface_get_stmts(stmt->u.type));
  1474. break;
  1475. case STMT_TYPEREF:
  1476. case STMT_IMPORTLIB:
  1477. /* not included in header */
  1478. break;
  1479. case STMT_IMPORT:
  1480. write_import(header, stmt->u.str);
  1481. break;
  1482. case STMT_TYPEDEF:
  1483. case STMT_MODULE:
  1484. case STMT_CPPQUOTE:
  1485. case STMT_PRAGMA:
  1486. case STMT_DECLARATION:
  1487. /* not processed here */
  1488. break;
  1489. case STMT_LIBRARY:
  1490. write_imports(header, stmt->u.lib->stmts);
  1491. break;
  1492. }
  1493. }
  1494. }
  1495. static void write_forward_decls(FILE *header, const statement_list_t *stmts)
  1496. {
  1497. const statement_t *stmt;
  1498. if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
  1499. {
  1500. switch (stmt->type)
  1501. {
  1502. case STMT_TYPE:
  1503. if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
  1504. {
  1505. type_t *iface = stmt->u.type;
  1506. if (is_object(iface) || is_attr(iface->attrs, ATTR_DISPINTERFACE))
  1507. {
  1508. write_forward(header, iface);
  1509. if (type_iface_get_async_iface(iface))
  1510. write_forward(header, type_iface_get_async_iface(iface));
  1511. }
  1512. }
  1513. else if (type_get_type(stmt->u.type) == TYPE_COCLASS)
  1514. write_coclass_forward(header, stmt->u.type);
  1515. break;
  1516. case STMT_TYPEREF:
  1517. case STMT_IMPORTLIB:
  1518. /* not included in header */
  1519. break;
  1520. case STMT_IMPORT:
  1521. case STMT_TYPEDEF:
  1522. case STMT_MODULE:
  1523. case STMT_CPPQUOTE:
  1524. case STMT_PRAGMA:
  1525. case STMT_DECLARATION:
  1526. /* not processed here */
  1527. break;
  1528. case STMT_LIBRARY:
  1529. write_forward_decls(header, stmt->u.lib->stmts);
  1530. break;
  1531. }
  1532. }
  1533. }
  1534. static void write_header_stmts(FILE *header, const statement_list_t *stmts, const type_t *iface, int ignore_funcs)
  1535. {
  1536. const statement_t *stmt;
  1537. if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
  1538. {
  1539. switch (stmt->type)
  1540. {
  1541. case STMT_TYPE:
  1542. if (type_get_type(stmt->u.type) == TYPE_INTERFACE)
  1543. {
  1544. type_t *iface = stmt->u.type;
  1545. type_t *async_iface = type_iface_get_async_iface(iface);
  1546. if (is_object(iface)) is_object_interface++;
  1547. if (is_attr(stmt->u.type->attrs, ATTR_DISPINTERFACE) || is_object(stmt->u.type))
  1548. {
  1549. write_com_interface_start(header, iface);
  1550. write_header_stmts(header, type_iface_get_stmts(iface), stmt->u.type, TRUE);
  1551. write_com_interface_end(header, iface);
  1552. if (async_iface)
  1553. {
  1554. write_com_interface_start(header, async_iface);
  1555. write_com_interface_end(header, async_iface);
  1556. }
  1557. }
  1558. else
  1559. {
  1560. write_rpc_interface_start(header, iface);
  1561. write_header_stmts(header, type_iface_get_stmts(iface), iface, FALSE);
  1562. write_rpc_interface_end(header, iface);
  1563. }
  1564. if (is_object(iface)) is_object_interface--;
  1565. }
  1566. else if (type_get_type(stmt->u.type) == TYPE_COCLASS)
  1567. write_coclass(header, stmt->u.type);
  1568. else
  1569. {
  1570. write_type_definition(header, stmt->u.type, stmt->declonly);
  1571. }
  1572. break;
  1573. case STMT_TYPEREF:
  1574. /* FIXME: shouldn't write out forward declarations for undefined
  1575. * interfaces but a number of our IDL files depend on this */
  1576. if (type_get_type(stmt->u.type) == TYPE_INTERFACE && !stmt->u.type->written)
  1577. write_forward(header, stmt->u.type);
  1578. break;
  1579. case STMT_IMPORTLIB:
  1580. case STMT_MODULE:
  1581. case STMT_PRAGMA:
  1582. /* not included in header */
  1583. break;
  1584. case STMT_IMPORT:
  1585. /* not processed here */
  1586. break;
  1587. case STMT_TYPEDEF:
  1588. {
  1589. const type_list_t *type_entry = stmt->u.type_list;
  1590. for (; type_entry; type_entry = type_entry->next)
  1591. write_typedef(header, type_entry->type, stmt->declonly);
  1592. break;
  1593. }
  1594. case STMT_LIBRARY:
  1595. write_library(header, stmt->u.lib);
  1596. write_header_stmts(header, stmt->u.lib->stmts, NULL, FALSE);
  1597. break;
  1598. case STMT_CPPQUOTE:
  1599. fprintf(header, "%s\n", stmt->u.str);
  1600. break;
  1601. case STMT_DECLARATION:
  1602. if (iface && type_get_type(stmt->u.var->declspec.type) == TYPE_FUNCTION)
  1603. {
  1604. if (!ignore_funcs)
  1605. {
  1606. int prefixes_differ = strcmp(prefix_client, prefix_server);
  1607. if (prefixes_differ)
  1608. {
  1609. fprintf(header, "/* client prototype */\n");
  1610. write_function_proto(header, iface, stmt->u.var, prefix_client);
  1611. fprintf(header, "/* server prototype */\n");
  1612. }
  1613. write_function_proto(header, iface, stmt->u.var, prefix_server);
  1614. }
  1615. }
  1616. else
  1617. write_declaration(header, stmt->u.var);
  1618. break;
  1619. }
  1620. }
  1621. }
  1622. void write_header(const statement_list_t *stmts)
  1623. {
  1624. FILE *header;
  1625. if (!do_header) return;
  1626. if(!(header = fopen(header_name, "w"))) {
  1627. error("Could not open %s for output\n", header_name);
  1628. return;
  1629. }
  1630. fprintf(header, "/*** Autogenerated by WIDL %s from %s - Do not edit ***/\n\n", PACKAGE_VERSION, input_name);
  1631. fprintf(header, "#ifdef _WIN32\n");
  1632. fprintf(header, "#ifndef __REQUIRED_RPCNDR_H_VERSION__\n");
  1633. fprintf(header, "#define __REQUIRED_RPCNDR_H_VERSION__ 475\n");
  1634. fprintf(header, "#endif\n");
  1635. fprintf(header, "#include <rpc.h>\n" );
  1636. fprintf(header, "#include <rpcndr.h>\n" );
  1637. if (!for_each_serializable(stmts, NULL, serializable_exists))
  1638. fprintf(header, "#include <midles.h>\n" );
  1639. fprintf(header, "#endif\n\n");
  1640. fprintf(header, "#ifndef COM_NO_WINDOWS_H\n");
  1641. fprintf(header, "#include <windows.h>\n");
  1642. fprintf(header, "#include <ole2.h>\n");
  1643. fprintf(header, "#endif\n\n");
  1644. fprintf(header, "#ifndef __%s__\n", header_token);
  1645. fprintf(header, "#define __%s__\n\n", header_token);
  1646. fprintf(header, "/* Forward declarations */\n\n");
  1647. write_forward_decls(header, stmts);
  1648. fprintf(header, "/* Headers for imported files */\n\n");
  1649. write_imports(header, stmts);
  1650. fprintf(header, "\n");
  1651. start_cplusplus_guard(header);
  1652. write_header_stmts(header, stmts, NULL, FALSE);
  1653. fprintf(header, "/* Begin additional prototypes for all interfaces */\n");
  1654. fprintf(header, "\n");
  1655. for_each_serializable(stmts, header, write_serialize_function_decl);
  1656. write_user_types(header);
  1657. write_generic_handle_routines(header);
  1658. write_context_handle_rundowns(header);
  1659. fprintf(header, "\n");
  1660. fprintf(header, "/* End additional prototypes */\n");
  1661. fprintf(header, "\n");
  1662. end_cplusplus_guard(header);
  1663. fprintf(header, "#endif /* __%s__ */\n", header_token);
  1664. fclose(header);
  1665. }