header.c 71 KB

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