header.c 71 KB

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