typetree.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326
  1. /*
  2. * IDL Type Tree
  3. *
  4. * Copyright 2008 Robert Shearman
  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 <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include "widl.h"
  25. #include "utils.h"
  26. #include "parser.h"
  27. #include "typetree.h"
  28. #include "header.h"
  29. #include "hash.h"
  30. type_t *duptype(type_t *t, int dupname)
  31. {
  32. type_t *d = alloc_type();
  33. *d = *t;
  34. if (dupname && t->name)
  35. d->name = xstrdup(t->name);
  36. return d;
  37. }
  38. type_t *make_type(enum type_type type)
  39. {
  40. type_t *t = alloc_type();
  41. t->name = NULL;
  42. t->namespace = NULL;
  43. t->type_type = type;
  44. t->attrs = NULL;
  45. t->c_name = NULL;
  46. t->signature = NULL;
  47. t->qualified_name = NULL;
  48. t->impl_name = NULL;
  49. t->short_name = NULL;
  50. memset(&t->details, 0, sizeof(t->details));
  51. t->typestring_offset = 0;
  52. t->ptrdesc = 0;
  53. t->ignore = (parse_only != 0);
  54. t->defined = FALSE;
  55. t->written = FALSE;
  56. t->user_types_registered = FALSE;
  57. t->tfswrite = FALSE;
  58. t->checked = FALSE;
  59. t->typelib_idx = -1;
  60. init_loc_info(&t->loc_info);
  61. return t;
  62. }
  63. static const var_t *find_arg(const var_list_t *args, const char *name)
  64. {
  65. const var_t *arg;
  66. if (args) LIST_FOR_EACH_ENTRY(arg, args, const var_t, entry)
  67. {
  68. if (arg->name && !strcmp(name, arg->name))
  69. return arg;
  70. }
  71. return NULL;
  72. }
  73. const char *type_get_name(const type_t *type, enum name_type name_type)
  74. {
  75. switch(name_type) {
  76. case NAME_DEFAULT:
  77. return type->name;
  78. case NAME_C:
  79. return type->c_name ? type->c_name : type->name;
  80. }
  81. assert(0);
  82. return NULL;
  83. }
  84. const char *type_get_qualified_name(const type_t *type, enum name_type name_type)
  85. {
  86. switch(name_type) {
  87. case NAME_DEFAULT:
  88. return type->qualified_name;
  89. case NAME_C:
  90. return type->c_name;
  91. }
  92. assert(0);
  93. return NULL;
  94. }
  95. static size_t append_namespace(char **buf, size_t *len, size_t pos, struct namespace *namespace, const char *separator, const char *abi_prefix)
  96. {
  97. int nested = namespace && !is_global_namespace(namespace);
  98. const char *name = nested ? namespace->name : abi_prefix;
  99. size_t n = 0;
  100. if (!name) return 0;
  101. if (nested) n += append_namespace(buf, len, pos + n, namespace->parent, separator, abi_prefix);
  102. n += strappend(buf, len, pos + n, "%s%s", name, separator);
  103. return n;
  104. }
  105. static size_t append_namespaces(char **buf, size_t *len, size_t pos, struct namespace *namespace, const char *prefix,
  106. const char *separator, const char *suffix, const char *abi_prefix)
  107. {
  108. int nested = namespace && !is_global_namespace(namespace);
  109. size_t n = 0;
  110. n += strappend(buf, len, pos + n, "%s", prefix);
  111. if (nested) n += append_namespace(buf, len, pos + n, namespace, separator, abi_prefix);
  112. if (suffix) n += strappend(buf, len, pos + n, "%s", suffix);
  113. else if (nested)
  114. {
  115. n -= strlen(separator);
  116. (*buf)[n] = 0;
  117. }
  118. return n;
  119. }
  120. static size_t append_pointer_stars(char **buf, size_t *len, size_t pos, type_t *type)
  121. {
  122. size_t n = 0;
  123. for (; type && type->type_type == TYPE_POINTER; type = type_pointer_get_ref_type(type)) n += strappend(buf, len, pos + n, "*");
  124. return n;
  125. }
  126. static size_t append_type_signature(char **buf, size_t *len, size_t pos, type_t *type);
  127. static size_t append_var_list_signature(char **buf, size_t *len, size_t pos, var_list_t *var_list)
  128. {
  129. var_t *var;
  130. size_t n = 0;
  131. if (!var_list) n += strappend(buf, len, pos + n, ";");
  132. else LIST_FOR_EACH_ENTRY(var, var_list, var_t, entry)
  133. {
  134. n += strappend(buf, len, pos + n, ";");
  135. n += append_type_signature(buf, len, pos + n, var->declspec.type);
  136. }
  137. return n;
  138. }
  139. static size_t append_type_signature(char **buf, size_t *len, size_t pos, type_t *type)
  140. {
  141. const GUID *uuid;
  142. size_t n = 0;
  143. if (!type) return 0;
  144. switch (type->type_type)
  145. {
  146. case TYPE_INTERFACE:
  147. if (type->signature) n += strappend(buf, len, pos + n, "%s", type->signature);
  148. else
  149. {
  150. if (!(uuid = get_attrp(type->attrs, ATTR_UUID)))
  151. error_loc_info(&type->loc_info, "cannot compute type signature, no uuid found for type %s.\n", type->name);
  152. n += strappend(buf, len, pos + n, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
  153. uuid->Data1, uuid->Data2, uuid->Data3,
  154. uuid->Data4[0], uuid->Data4[1], uuid->Data4[2], uuid->Data4[3],
  155. uuid->Data4[4], uuid->Data4[5], uuid->Data4[6], uuid->Data4[7]);
  156. }
  157. return n;
  158. case TYPE_DELEGATE:
  159. n += strappend(buf, len, pos + n, "delegate(");
  160. n += append_type_signature(buf, len, pos + n, type_delegate_get_iface(type));
  161. n += strappend(buf, len, pos + n, ")");
  162. return n;
  163. case TYPE_RUNTIMECLASS:
  164. n += strappend(buf, len, pos + n, "rc(");
  165. n += append_namespaces(buf, len, pos + n, type->namespace, "", ".", type->name, NULL);
  166. n += strappend(buf, len, pos + n, ";");
  167. n += append_type_signature(buf, len, pos + n, type_runtimeclass_get_default_iface(type, TRUE));
  168. n += strappend(buf, len, pos + n, ")");
  169. return n;
  170. case TYPE_POINTER:
  171. n += append_type_signature(buf, len, pos + n, type->details.pointer.ref.type);
  172. return n;
  173. case TYPE_ALIAS:
  174. if (!strcmp(type->name, "boolean")) n += strappend(buf, len, pos + n, "b1");
  175. else n += append_type_signature(buf, len, pos + n, type->details.alias.aliasee.type);
  176. return n;
  177. case TYPE_STRUCT:
  178. n += strappend(buf, len, pos + n, "struct(");
  179. n += append_namespaces(buf, len, pos + n, type->namespace, "", ".", type->name, NULL);
  180. n += append_var_list_signature(buf, len, pos + n, type->details.structure->fields);
  181. n += strappend(buf, len, pos + n, ")");
  182. return n;
  183. case TYPE_BASIC:
  184. switch (type_basic_get_type(type))
  185. {
  186. case TYPE_BASIC_INT:
  187. case TYPE_BASIC_INT32:
  188. n += strappend(buf, len, pos + n, type_basic_get_sign(type) < 0 ? "i4" : "u4");
  189. return n;
  190. case TYPE_BASIC_INT64:
  191. n += strappend(buf, len, pos + n, type_basic_get_sign(type) < 0 ? "i8" : "u8");
  192. return n;
  193. case TYPE_BASIC_INT8:
  194. assert(type_basic_get_sign(type) >= 0); /* signature string for signed char isn't specified */
  195. n += strappend(buf, len, pos + n, "u1");
  196. return n;
  197. case TYPE_BASIC_FLOAT:
  198. n += strappend(buf, len, pos + n, "f4");
  199. return n;
  200. case TYPE_BASIC_DOUBLE:
  201. n += strappend(buf, len, pos + n, "f8");
  202. return n;
  203. case TYPE_BASIC_INT16:
  204. case TYPE_BASIC_INT3264:
  205. case TYPE_BASIC_LONG:
  206. case TYPE_BASIC_CHAR:
  207. case TYPE_BASIC_HYPER:
  208. case TYPE_BASIC_BYTE:
  209. case TYPE_BASIC_WCHAR:
  210. case TYPE_BASIC_ERROR_STATUS_T:
  211. case TYPE_BASIC_HANDLE:
  212. error_loc_info(&type->loc_info, "unimplemented type signature for basic type %d.\n", type_basic_get_type(type));
  213. break;
  214. }
  215. case TYPE_ENUM:
  216. n += strappend(buf, len, pos + n, "enum(");
  217. n += append_namespaces(buf, len, pos + n, type->namespace, "", ".", type->name, NULL);
  218. if (is_attr(type->attrs, ATTR_FLAGS)) n += strappend(buf, len, pos + n, ";u4");
  219. else n += strappend(buf, len, pos + n, ";i4");
  220. n += strappend(buf, len, pos + n, ")");
  221. return n;
  222. case TYPE_ARRAY:
  223. case TYPE_ENCAPSULATED_UNION:
  224. case TYPE_UNION:
  225. case TYPE_COCLASS:
  226. case TYPE_VOID:
  227. case TYPE_FUNCTION:
  228. case TYPE_BITFIELD:
  229. case TYPE_MODULE:
  230. case TYPE_APICONTRACT:
  231. error_loc_info(&type->loc_info, "unimplemented type signature for type %s of type %d.\n", type->name, type->type_type);
  232. break;
  233. case TYPE_PARAMETERIZED_TYPE:
  234. case TYPE_PARAMETER:
  235. assert(0); /* should not be there */
  236. break;
  237. }
  238. return n;
  239. }
  240. char *format_namespace(struct namespace *namespace, const char *prefix, const char *separator, const char *suffix, const char *abi_prefix)
  241. {
  242. size_t len = 0;
  243. char *buf = NULL;
  244. append_namespaces(&buf, &len, 0, namespace, prefix, separator, suffix, abi_prefix);
  245. return buf;
  246. }
  247. char *format_parameterized_type_name(type_t *type, typeref_list_t *params)
  248. {
  249. size_t len = 0, pos = 0;
  250. char *buf = NULL;
  251. typeref_t *ref;
  252. pos += strappend(&buf, &len, pos, "%s<", type->name);
  253. if (params) LIST_FOR_EACH_ENTRY(ref, params, typeref_t, entry)
  254. {
  255. type = type_pointer_get_root_type(ref->type);
  256. pos += strappend(&buf, &len, pos, "%s", type->qualified_name);
  257. pos += append_pointer_stars(&buf, &len, pos, ref->type);
  258. if (list_next(params, &ref->entry)) pos += strappend(&buf, &len, pos, ",");
  259. }
  260. pos += strappend(&buf, &len, pos, " >");
  261. return buf;
  262. }
  263. static char const *parameterized_type_shorthands[][2] = {
  264. {"Windows_CFoundation_CCollections_C", "__F"},
  265. {"Windows_CFoundation_C", "__F"},
  266. };
  267. static char *format_parameterized_type_c_name(type_t *type, typeref_list_t *params, const char *prefix)
  268. {
  269. size_t len = 0, pos = 0;
  270. char *buf = NULL, *tmp;
  271. int i, count = params ? list_count(params) : 0;
  272. typeref_t *ref;
  273. pos += append_namespaces(&buf, &len, pos, type->namespace, "__x_", "_C", "", use_abi_namespace ? "ABI" : NULL);
  274. pos += strappend(&buf, &len, pos, "%s%s_%d", prefix, type->name, count);
  275. if (params) LIST_FOR_EACH_ENTRY(ref, params, typeref_t, entry)
  276. {
  277. type = type_pointer_get_root_type(ref->type);
  278. pos += append_namespaces(&buf, &len, pos, type->namespace, "_", "__C", type->name, NULL);
  279. }
  280. for (i = 0; i < ARRAY_SIZE(parameterized_type_shorthands); ++i)
  281. {
  282. if ((tmp = strstr(buf, parameterized_type_shorthands[i][0])) &&
  283. (tmp - buf) == strlen(use_abi_namespace ? "__x_ABI_C" : "__x_C"))
  284. {
  285. tmp += strlen(parameterized_type_shorthands[i][0]);
  286. strcpy(buf, parameterized_type_shorthands[i][1]);
  287. memmove(buf + 3, tmp, len - (tmp - buf));
  288. }
  289. }
  290. return buf;
  291. }
  292. static char *format_parameterized_type_signature(type_t *type, typeref_list_t *params)
  293. {
  294. size_t len = 0, pos = 0;
  295. char *buf = NULL;
  296. typeref_t *ref;
  297. const GUID *uuid;
  298. if (!(uuid = get_attrp(type->attrs, ATTR_UUID)))
  299. error_loc_info(&type->loc_info, "cannot compute type signature, no uuid found for type %s.\n", type->name);
  300. pos += strappend(&buf, &len, pos, "pinterface({%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
  301. uuid->Data1, uuid->Data2, uuid->Data3,
  302. uuid->Data4[0], uuid->Data4[1], uuid->Data4[2], uuid->Data4[3],
  303. uuid->Data4[4], uuid->Data4[5], uuid->Data4[6], uuid->Data4[7]);
  304. if (params) LIST_FOR_EACH_ENTRY(ref, params, typeref_t, entry)
  305. {
  306. pos += strappend(&buf, &len, pos, ";");
  307. pos += append_type_signature(&buf, &len, pos, ref->type);
  308. }
  309. pos += strappend(&buf, &len, pos, ")");
  310. return buf;
  311. }
  312. static char *format_parameterized_type_short_name(type_t *type, typeref_list_t *params, const char *prefix)
  313. {
  314. size_t len = 0, pos = 0;
  315. char *buf = NULL;
  316. typeref_t *ref;
  317. pos += strappend(&buf, &len, pos, "%s%s", prefix, type->name);
  318. if (params) LIST_FOR_EACH_ENTRY(ref, params, typeref_t, entry)
  319. {
  320. type = type_pointer_get_root_type(ref->type);
  321. pos += strappend(&buf, &len, pos, "_%s", type->name);
  322. }
  323. return buf;
  324. }
  325. static char *format_parameterized_type_impl_name(type_t *type, typeref_list_t *params, const char *prefix)
  326. {
  327. size_t len = 0, pos = 0;
  328. char *buf = NULL;
  329. typeref_t *ref;
  330. type_t *iface;
  331. pos += strappend(&buf, &len, pos, "%s%s_impl<", prefix, type->name);
  332. if (params) LIST_FOR_EACH_ENTRY(ref, params, typeref_t, entry)
  333. {
  334. type = type_pointer_get_root_type(ref->type);
  335. if (type->type_type == TYPE_RUNTIMECLASS)
  336. {
  337. pos += strappend(&buf, &len, pos, "ABI::Windows::Foundation::Internal::AggregateType<%s", type->qualified_name);
  338. pos += append_pointer_stars(&buf, &len, pos, ref->type);
  339. iface = type_runtimeclass_get_default_iface(type, TRUE);
  340. pos += strappend(&buf, &len, pos, ", %s", iface->qualified_name);
  341. pos += append_pointer_stars(&buf, &len, pos, ref->type);
  342. pos += strappend(&buf, &len, pos, " >");
  343. }
  344. else
  345. {
  346. pos += strappend(&buf, &len, pos, "%s", type->qualified_name);
  347. pos += append_pointer_stars(&buf, &len, pos, ref->type);
  348. }
  349. if (list_next(params, &ref->entry)) pos += strappend(&buf, &len, pos, ", ");
  350. }
  351. pos += strappend(&buf, &len, pos, " >");
  352. return buf;
  353. }
  354. type_t *type_new_function(var_list_t *args)
  355. {
  356. var_t *arg;
  357. type_t *t;
  358. unsigned int i = 0;
  359. if (args)
  360. {
  361. arg = LIST_ENTRY(list_head(args), var_t, entry);
  362. if (list_count(args) == 1 && !arg->name && arg->declspec.type && type_get_type(arg->declspec.type) == TYPE_VOID)
  363. {
  364. list_remove(&arg->entry);
  365. free(arg);
  366. free(args);
  367. args = NULL;
  368. }
  369. }
  370. if (args) LIST_FOR_EACH_ENTRY(arg, args, var_t, entry)
  371. {
  372. if (arg->declspec.type && type_get_type(arg->declspec.type) == TYPE_VOID)
  373. error_loc("argument '%s' has void type\n", arg->name);
  374. if (!arg->name)
  375. {
  376. if (i > 26 * 26)
  377. error_loc("too many unnamed arguments\n");
  378. else
  379. {
  380. int unique;
  381. do
  382. {
  383. char name[3];
  384. name[0] = i > 26 ? 'a' + i / 26 : 'a' + i;
  385. name[1] = i > 26 ? 'a' + i % 26 : 0;
  386. name[2] = 0;
  387. unique = !find_arg(args, name);
  388. if (unique)
  389. arg->name = xstrdup(name);
  390. i++;
  391. } while (!unique);
  392. }
  393. }
  394. }
  395. t = make_type(TYPE_FUNCTION);
  396. t->details.function = xmalloc(sizeof(*t->details.function));
  397. t->details.function->args = args;
  398. t->details.function->retval = make_var(xstrdup("_RetVal"));
  399. return t;
  400. }
  401. type_t *type_new_pointer(type_t *ref)
  402. {
  403. type_t *t = make_type(TYPE_POINTER);
  404. t->details.pointer.ref.type = ref;
  405. return t;
  406. }
  407. type_t *type_new_alias(const decl_spec_t *t, const char *name)
  408. {
  409. type_t *a = make_type(TYPE_ALIAS);
  410. a->name = xstrdup(name);
  411. a->attrs = NULL;
  412. a->details.alias.aliasee = *t;
  413. init_loc_info(&a->loc_info);
  414. return a;
  415. }
  416. type_t *type_new_array(const char *name, const decl_spec_t *element, int declptr,
  417. unsigned int dim, expr_t *size_is, expr_t *length_is)
  418. {
  419. type_t *t = make_type(TYPE_ARRAY);
  420. if (name) t->name = xstrdup(name);
  421. t->details.array.declptr = declptr;
  422. t->details.array.length_is = length_is;
  423. if (size_is)
  424. t->details.array.size_is = size_is;
  425. else
  426. t->details.array.dim = dim;
  427. if (element)
  428. t->details.array.elem = *element;
  429. return t;
  430. }
  431. type_t *type_new_basic(enum type_basic_type basic_type)
  432. {
  433. type_t *t = make_type(TYPE_BASIC);
  434. t->details.basic.type = basic_type;
  435. t->details.basic.sign = 0;
  436. return t;
  437. }
  438. type_t *type_new_int(enum type_basic_type basic_type, int sign)
  439. {
  440. static type_t *int_types[TYPE_BASIC_INT_MAX+1][3];
  441. assert(basic_type <= TYPE_BASIC_INT_MAX);
  442. /* map sign { -1, 0, 1 } -> { 0, 1, 2 } */
  443. if (!int_types[basic_type][sign + 1])
  444. {
  445. int_types[basic_type][sign + 1] = type_new_basic(basic_type);
  446. int_types[basic_type][sign + 1]->details.basic.sign = sign;
  447. }
  448. return int_types[basic_type][sign + 1];
  449. }
  450. type_t *type_new_void(void)
  451. {
  452. static type_t *void_type = NULL;
  453. if (!void_type)
  454. void_type = make_type(TYPE_VOID);
  455. return void_type;
  456. }
  457. type_t *type_new_enum(const char *name, struct namespace *namespace, int defined, var_list_t *enums)
  458. {
  459. type_t *t = NULL;
  460. if (name)
  461. t = find_type(name, namespace,tsENUM);
  462. if (!t)
  463. {
  464. t = make_type(TYPE_ENUM);
  465. t->name = name;
  466. t->namespace = namespace;
  467. if (name)
  468. reg_type(t, name, namespace, tsENUM);
  469. }
  470. if (!t->defined && defined)
  471. {
  472. t->details.enumeration = xmalloc(sizeof(*t->details.enumeration));
  473. t->details.enumeration->enums = enums;
  474. t->defined = TRUE;
  475. }
  476. else if (defined)
  477. error_loc("redefinition of enum %s\n", name);
  478. return t;
  479. }
  480. type_t *type_new_struct(char *name, struct namespace *namespace, int defined, var_list_t *fields)
  481. {
  482. type_t *t = NULL;
  483. if (name)
  484. t = find_type(name, namespace, tsSTRUCT);
  485. if (!t)
  486. {
  487. t = make_type(TYPE_STRUCT);
  488. t->name = name;
  489. t->namespace = namespace;
  490. if (name)
  491. reg_type(t, name, namespace, tsSTRUCT);
  492. }
  493. if (!t->defined && defined)
  494. {
  495. t->details.structure = xmalloc(sizeof(*t->details.structure));
  496. t->details.structure->fields = fields;
  497. t->defined = TRUE;
  498. }
  499. else if (defined)
  500. error_loc("redefinition of struct %s\n", name);
  501. return t;
  502. }
  503. type_t *type_new_nonencapsulated_union(const char *name, int defined, var_list_t *fields)
  504. {
  505. type_t *t = NULL;
  506. if (name)
  507. t = find_type(name, NULL, tsUNION);
  508. if (!t)
  509. {
  510. t = make_type(TYPE_UNION);
  511. t->name = name;
  512. if (name)
  513. reg_type(t, name, NULL, tsUNION);
  514. }
  515. if (!t->defined && defined)
  516. {
  517. t->details.structure = xmalloc(sizeof(*t->details.structure));
  518. t->details.structure->fields = fields;
  519. t->defined = TRUE;
  520. }
  521. else if (defined)
  522. error_loc("redefinition of union %s\n", name);
  523. return t;
  524. }
  525. type_t *type_new_encapsulated_union(char *name, var_t *switch_field, var_t *union_field, var_list_t *cases)
  526. {
  527. type_t *t = NULL;
  528. if (name)
  529. t = find_type(name, NULL, tsUNION);
  530. if (!t)
  531. {
  532. t = make_type(TYPE_ENCAPSULATED_UNION);
  533. t->name = name;
  534. if (name)
  535. reg_type(t, name, NULL, tsUNION);
  536. }
  537. t->type_type = TYPE_ENCAPSULATED_UNION;
  538. if (!t->defined)
  539. {
  540. if (!union_field)
  541. union_field = make_var(xstrdup("tagged_union"));
  542. union_field->declspec.type = type_new_nonencapsulated_union(gen_name(), TRUE, cases);
  543. t->details.structure = xmalloc(sizeof(*t->details.structure));
  544. t->details.structure->fields = append_var(NULL, switch_field);
  545. t->details.structure->fields = append_var(t->details.structure->fields, union_field);
  546. t->defined = TRUE;
  547. }
  548. else
  549. error_loc("redefinition of union %s\n", name);
  550. return t;
  551. }
  552. static int is_valid_bitfield_type(const type_t *type)
  553. {
  554. switch (type_get_type(type))
  555. {
  556. case TYPE_ENUM:
  557. return TRUE;
  558. case TYPE_BASIC:
  559. switch (type_basic_get_type(type))
  560. {
  561. case TYPE_BASIC_INT8:
  562. case TYPE_BASIC_INT16:
  563. case TYPE_BASIC_INT32:
  564. case TYPE_BASIC_INT64:
  565. case TYPE_BASIC_INT:
  566. case TYPE_BASIC_INT3264:
  567. case TYPE_BASIC_LONG:
  568. case TYPE_BASIC_CHAR:
  569. case TYPE_BASIC_HYPER:
  570. case TYPE_BASIC_BYTE:
  571. case TYPE_BASIC_WCHAR:
  572. case TYPE_BASIC_ERROR_STATUS_T:
  573. return TRUE;
  574. case TYPE_BASIC_FLOAT:
  575. case TYPE_BASIC_DOUBLE:
  576. case TYPE_BASIC_HANDLE:
  577. return FALSE;
  578. }
  579. return FALSE;
  580. default:
  581. return FALSE;
  582. }
  583. }
  584. type_t *type_new_bitfield(type_t *field, const expr_t *bits)
  585. {
  586. type_t *t;
  587. if (!is_valid_bitfield_type(field))
  588. error_loc("bit-field has invalid type\n");
  589. if (bits->cval < 0)
  590. error_loc("negative width for bit-field\n");
  591. /* FIXME: validate bits->cval <= memsize(field) * 8 */
  592. t = make_type(TYPE_BITFIELD);
  593. t->details.bitfield.field = field;
  594. t->details.bitfield.bits = bits;
  595. return t;
  596. }
  597. static unsigned int compute_method_indexes(type_t *iface)
  598. {
  599. unsigned int idx;
  600. statement_t *stmt;
  601. if (!iface->details.iface)
  602. return 0;
  603. if (type_iface_get_inherit(iface))
  604. idx = compute_method_indexes(type_iface_get_inherit(iface));
  605. else
  606. idx = 0;
  607. STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
  608. {
  609. var_t *func = stmt->u.var;
  610. if (!is_callas(func->attrs))
  611. func->func_idx = idx++;
  612. }
  613. return idx;
  614. }
  615. type_t *type_interface_declare(char *name, struct namespace *namespace)
  616. {
  617. type_t *type = get_type(TYPE_INTERFACE, name, namespace, 0);
  618. if (type_get_type_detect_alias(type) != TYPE_INTERFACE)
  619. error_loc("interface %s previously not declared an interface at %s:%d\n",
  620. type->name, type->loc_info.input_name, type->loc_info.line_number);
  621. return type;
  622. }
  623. type_t *type_interface_define(type_t *iface, attr_list_t *attrs, type_t *inherit, statement_list_t *stmts, typeref_list_t *requires)
  624. {
  625. if (iface->defined)
  626. error_loc("interface %s already defined at %s:%d\n",
  627. iface->name, iface->loc_info.input_name, iface->loc_info.line_number);
  628. if (iface == inherit)
  629. error_loc("interface %s can't inherit from itself\n",
  630. iface->name);
  631. iface->attrs = check_interface_attrs(iface->name, attrs);
  632. iface->details.iface = xmalloc(sizeof(*iface->details.iface));
  633. iface->details.iface->disp_props = NULL;
  634. iface->details.iface->disp_methods = NULL;
  635. iface->details.iface->stmts = stmts;
  636. iface->details.iface->inherit = inherit;
  637. iface->details.iface->disp_inherit = NULL;
  638. iface->details.iface->async_iface = NULL;
  639. iface->details.iface->requires = requires;
  640. iface->defined = TRUE;
  641. compute_method_indexes(iface);
  642. return iface;
  643. }
  644. type_t *type_dispinterface_declare(char *name)
  645. {
  646. type_t *type = get_type(TYPE_INTERFACE, name, NULL, 0);
  647. if (type_get_type_detect_alias(type) != TYPE_INTERFACE)
  648. error_loc("dispinterface %s previously not declared a dispinterface at %s:%d\n",
  649. type->name, type->loc_info.input_name, type->loc_info.line_number);
  650. return type;
  651. }
  652. type_t *type_dispinterface_define(type_t *iface, attr_list_t *attrs, var_list_t *props, var_list_t *methods)
  653. {
  654. if (iface->defined)
  655. error_loc("dispinterface %s already defined at %s:%d\n",
  656. iface->name, iface->loc_info.input_name, iface->loc_info.line_number);
  657. iface->attrs = check_dispiface_attrs(iface->name, attrs);
  658. iface->details.iface = xmalloc(sizeof(*iface->details.iface));
  659. iface->details.iface->disp_props = props;
  660. iface->details.iface->disp_methods = methods;
  661. iface->details.iface->stmts = NULL;
  662. iface->details.iface->inherit = find_type("IDispatch", NULL, 0);
  663. if (!iface->details.iface->inherit) error_loc("IDispatch is undefined\n");
  664. iface->details.iface->disp_inherit = NULL;
  665. iface->details.iface->async_iface = NULL;
  666. iface->details.iface->requires = NULL;
  667. iface->defined = TRUE;
  668. compute_method_indexes(iface);
  669. return iface;
  670. }
  671. type_t *type_dispinterface_define_from_iface(type_t *dispiface, attr_list_t *attrs, type_t *iface)
  672. {
  673. if (dispiface->defined)
  674. error_loc("dispinterface %s already defined at %s:%d\n",
  675. dispiface->name, dispiface->loc_info.input_name, dispiface->loc_info.line_number);
  676. dispiface->attrs = check_dispiface_attrs(dispiface->name, attrs);
  677. dispiface->details.iface = xmalloc(sizeof(*dispiface->details.iface));
  678. dispiface->details.iface->disp_props = NULL;
  679. dispiface->details.iface->disp_methods = NULL;
  680. dispiface->details.iface->stmts = NULL;
  681. dispiface->details.iface->inherit = find_type("IDispatch", NULL, 0);
  682. if (!dispiface->details.iface->inherit) error_loc("IDispatch is undefined\n");
  683. dispiface->details.iface->disp_inherit = iface;
  684. dispiface->details.iface->async_iface = NULL;
  685. dispiface->details.iface->requires = NULL;
  686. dispiface->defined = TRUE;
  687. compute_method_indexes(dispiface);
  688. return dispiface;
  689. }
  690. type_t *type_module_declare(char *name)
  691. {
  692. type_t *type = get_type(TYPE_MODULE, name, NULL, 0);
  693. if (type_get_type_detect_alias(type) != TYPE_MODULE)
  694. error_loc("module %s previously not declared a module at %s:%d\n",
  695. type->name, type->loc_info.input_name, type->loc_info.line_number);
  696. return type;
  697. }
  698. type_t *type_module_define(type_t* module, attr_list_t *attrs, statement_list_t *stmts)
  699. {
  700. if (module->defined)
  701. error_loc("module %s already defined at %s:%d\n",
  702. module->name, module->loc_info.input_name, module->loc_info.line_number);
  703. module->attrs = check_module_attrs(module->name, attrs);
  704. module->details.module = xmalloc(sizeof(*module->details.module));
  705. module->details.module->stmts = stmts;
  706. module->defined = TRUE;
  707. return module;
  708. }
  709. type_t *type_coclass_declare(char *name)
  710. {
  711. type_t *type = get_type(TYPE_COCLASS, name, NULL, 0);
  712. if (type_get_type_detect_alias(type) != TYPE_COCLASS)
  713. error_loc("coclass %s previously not declared a coclass at %s:%d\n",
  714. type->name, type->loc_info.input_name, type->loc_info.line_number);
  715. return type;
  716. }
  717. type_t *type_coclass_define(type_t *coclass, attr_list_t *attrs, typeref_list_t *ifaces)
  718. {
  719. if (coclass->defined)
  720. error_loc("coclass %s already defined at %s:%d\n",
  721. coclass->name, coclass->loc_info.input_name, coclass->loc_info.line_number);
  722. coclass->attrs = check_coclass_attrs(coclass->name, attrs);
  723. coclass->details.coclass.ifaces = ifaces;
  724. coclass->defined = TRUE;
  725. return coclass;
  726. }
  727. type_t *type_runtimeclass_declare(char *name, struct namespace *namespace)
  728. {
  729. type_t *type = get_type(TYPE_RUNTIMECLASS, name, namespace, 0);
  730. if (type_get_type_detect_alias(type) != TYPE_RUNTIMECLASS)
  731. error_loc("runtimeclass %s previously not declared a runtimeclass at %s:%d\n",
  732. type->name, type->loc_info.input_name, type->loc_info.line_number);
  733. return type;
  734. }
  735. type_t *type_runtimeclass_define(type_t *runtimeclass, attr_list_t *attrs, typeref_list_t *ifaces)
  736. {
  737. typeref_t *ref, *required, *tmp;
  738. typeref_list_t *requires;
  739. if (runtimeclass->defined)
  740. error_loc("runtimeclass %s already defined at %s:%d\n",
  741. runtimeclass->name, runtimeclass->loc_info.input_name, runtimeclass->loc_info.line_number);
  742. runtimeclass->attrs = check_runtimeclass_attrs(runtimeclass->name, attrs);
  743. runtimeclass->details.runtimeclass.ifaces = ifaces;
  744. runtimeclass->defined = TRUE;
  745. if (!type_runtimeclass_get_default_iface(runtimeclass, FALSE) &&
  746. !get_attrp(runtimeclass->attrs, ATTR_STATIC))
  747. error_loc("runtimeclass %s must have a default interface or static factory\n", runtimeclass->name);
  748. if (ifaces) LIST_FOR_EACH_ENTRY(ref, ifaces, typeref_t, entry)
  749. {
  750. /* FIXME: this should probably not be allowed, here or in coclass, */
  751. /* but for now there's too many places in Wine IDL where it is to */
  752. /* even print a warning. */
  753. if (!(ref->type->defined)) continue;
  754. if (!(requires = type_iface_get_requires(ref->type))) continue;
  755. LIST_FOR_EACH_ENTRY(required, requires, typeref_t, entry)
  756. {
  757. int found = 0;
  758. LIST_FOR_EACH_ENTRY(tmp, ifaces, typeref_t, entry)
  759. if ((found = type_is_equal(tmp->type, required->type))) break;
  760. if (!found)
  761. error_loc("interface '%s' also requires interface '%s', "
  762. "but runtimeclass '%s' does not implement it.\n",
  763. ref->type->name, required->type->name, runtimeclass->name);
  764. }
  765. }
  766. return runtimeclass;
  767. }
  768. type_t *type_apicontract_declare(char *name, struct namespace *namespace)
  769. {
  770. type_t *type = get_type(TYPE_APICONTRACT, name, namespace, 0);
  771. if (type_get_type_detect_alias(type) != TYPE_APICONTRACT)
  772. error_loc("apicontract %s previously not declared a apicontract at %s:%d\n",
  773. type->name, type->loc_info.input_name, type->loc_info.line_number);
  774. return type;
  775. }
  776. type_t *type_apicontract_define(type_t *apicontract, attr_list_t *attrs)
  777. {
  778. if (apicontract->defined)
  779. error_loc("apicontract %s already defined at %s:%d\n",
  780. apicontract->name, apicontract->loc_info.input_name, apicontract->loc_info.line_number);
  781. apicontract->attrs = check_apicontract_attrs(apicontract->name, attrs);
  782. apicontract->defined = TRUE;
  783. return apicontract;
  784. }
  785. static void compute_delegate_iface_names(type_t *delegate, type_t *type, typeref_list_t *params)
  786. {
  787. type_t *iface = delegate->details.delegate.iface;
  788. iface->namespace = delegate->namespace;
  789. iface->name = strmake("I%s", delegate->name);
  790. if (type) iface->c_name = format_parameterized_type_c_name(type, params, "I");
  791. else iface->c_name = format_namespace(delegate->namespace, "__x_", "_C", iface->name, use_abi_namespace ? "ABI" : NULL);
  792. iface->qualified_name = format_namespace(delegate->namespace, "", "::", iface->name, use_abi_namespace ? "ABI" : NULL);
  793. }
  794. type_t *type_delegate_declare(char *name, struct namespace *namespace)
  795. {
  796. type_t *type = get_type(TYPE_DELEGATE, name, namespace, 0);
  797. if (type_get_type_detect_alias(type) != TYPE_DELEGATE)
  798. error_loc("delegate %s previously not declared a delegate at %s:%d\n",
  799. type->name, type->loc_info.input_name, type->loc_info.line_number);
  800. return type;
  801. }
  802. type_t *type_delegate_define(type_t *delegate, attr_list_t *attrs, statement_list_t *stmts)
  803. {
  804. type_t *iface;
  805. if (delegate->defined)
  806. error_loc("delegate %s already defined at %s:%d\n",
  807. delegate->name, delegate->loc_info.input_name, delegate->loc_info.line_number);
  808. delegate->attrs = check_interface_attrs(delegate->name, attrs);
  809. iface = make_type(TYPE_INTERFACE);
  810. iface->attrs = delegate->attrs;
  811. iface->details.iface = xmalloc(sizeof(*iface->details.iface));
  812. iface->details.iface->disp_props = NULL;
  813. iface->details.iface->disp_methods = NULL;
  814. iface->details.iface->stmts = stmts;
  815. iface->details.iface->inherit = find_type("IUnknown", NULL, 0);
  816. if (!iface->details.iface->inherit) error_loc("IUnknown is undefined\n");
  817. iface->details.iface->disp_inherit = NULL;
  818. iface->details.iface->async_iface = NULL;
  819. iface->details.iface->requires = NULL;
  820. iface->defined = TRUE;
  821. compute_method_indexes(iface);
  822. delegate->details.delegate.iface = iface;
  823. delegate->defined = TRUE;
  824. compute_delegate_iface_names(delegate, NULL, NULL);
  825. return delegate;
  826. }
  827. type_t *type_parameterized_interface_declare(char *name, struct namespace *namespace, typeref_list_t *params)
  828. {
  829. type_t *type = get_type(TYPE_PARAMETERIZED_TYPE, name, namespace, 0);
  830. if (type_get_type_detect_alias(type) != TYPE_PARAMETERIZED_TYPE)
  831. error_loc("pinterface %s previously not declared a pinterface at %s:%d\n",
  832. type->name, type->loc_info.input_name, type->loc_info.line_number);
  833. type->details.parameterized.type = make_type(TYPE_INTERFACE);
  834. type->details.parameterized.params = params;
  835. return type;
  836. }
  837. type_t *type_parameterized_interface_define(type_t *type, attr_list_t *attrs, type_t *inherit, statement_list_t *stmts, typeref_list_t *requires)
  838. {
  839. type_t *iface;
  840. if (type->defined)
  841. error_loc("pinterface %s already defined at %s:%d\n",
  842. type->name, type->loc_info.input_name, type->loc_info.line_number);
  843. /* The parameterized type UUID is actually a PIID that is then used as a seed to generate
  844. * a new type GUID with the rules described in:
  845. * https://docs.microsoft.com/en-us/uwp/winrt-cref/winrt-type-system#parameterized-types
  846. * TODO: store type signatures for generated interfaces, and generate their GUIDs
  847. */
  848. type->attrs = check_interface_attrs(type->name, attrs);
  849. iface = type->details.parameterized.type;
  850. iface->details.iface = xmalloc(sizeof(*iface->details.iface));
  851. iface->details.iface->disp_props = NULL;
  852. iface->details.iface->disp_methods = NULL;
  853. iface->details.iface->stmts = stmts;
  854. iface->details.iface->inherit = inherit;
  855. iface->details.iface->disp_inherit = NULL;
  856. iface->details.iface->async_iface = NULL;
  857. iface->details.iface->requires = requires;
  858. iface->name = type->name;
  859. type->defined = TRUE;
  860. return type;
  861. }
  862. type_t *type_parameterized_delegate_declare(char *name, struct namespace *namespace, typeref_list_t *params)
  863. {
  864. type_t *type = get_type(TYPE_PARAMETERIZED_TYPE, name, namespace, 0);
  865. if (type_get_type_detect_alias(type) != TYPE_PARAMETERIZED_TYPE)
  866. error_loc("pdelegate %s previously not declared a pdelegate at %s:%d\n",
  867. type->name, type->loc_info.input_name, type->loc_info.line_number);
  868. type->details.parameterized.type = make_type(TYPE_DELEGATE);
  869. type->details.parameterized.params = params;
  870. return type;
  871. }
  872. type_t *type_parameterized_delegate_define(type_t *type, attr_list_t *attrs, statement_list_t *stmts)
  873. {
  874. type_t *iface, *delegate;
  875. if (type->defined)
  876. error_loc("pdelegate %s already defined at %s:%d\n",
  877. type->name, type->loc_info.input_name, type->loc_info.line_number);
  878. type->attrs = check_interface_attrs(type->name, attrs);
  879. delegate = type->details.parameterized.type;
  880. delegate->attrs = type->attrs;
  881. delegate->details.delegate.iface = make_type(TYPE_INTERFACE);
  882. iface = delegate->details.delegate.iface;
  883. iface->details.iface = xmalloc(sizeof(*iface->details.iface));
  884. iface->details.iface->disp_props = NULL;
  885. iface->details.iface->disp_methods = NULL;
  886. iface->details.iface->stmts = stmts;
  887. iface->details.iface->inherit = find_type("IUnknown", NULL, 0);
  888. if (!iface->details.iface->inherit) error_loc("IUnknown is undefined\n");
  889. iface->details.iface->disp_inherit = NULL;
  890. iface->details.iface->async_iface = NULL;
  891. iface->details.iface->requires = NULL;
  892. delegate->name = type->name;
  893. compute_delegate_iface_names(delegate, type, type->details.parameterized.params);
  894. type->defined = TRUE;
  895. return type;
  896. }
  897. type_t *type_parameterized_type_specialize_partial(type_t *type, typeref_list_t *params)
  898. {
  899. type_t *new_type = duptype(type, 0);
  900. new_type->details.parameterized.type = type;
  901. new_type->details.parameterized.params = params;
  902. return new_type;
  903. }
  904. static type_t *replace_type_parameters_in_type(type_t *type, typeref_list_t *orig, typeref_list_t *repl);
  905. static typeref_list_t *replace_type_parameters_in_type_list(typeref_list_t *list, typeref_list_t *orig, typeref_list_t *repl)
  906. {
  907. typeref_list_t *new_list = NULL;
  908. typeref_t *ref;
  909. if (!list) return list;
  910. LIST_FOR_EACH_ENTRY(ref, list, typeref_t, entry)
  911. {
  912. type_t *new_type = replace_type_parameters_in_type(ref->type, orig, repl);
  913. new_list = append_typeref(new_list, make_typeref(new_type));
  914. }
  915. return new_list;
  916. }
  917. static var_t *replace_type_parameters_in_var(var_t *var, typeref_list_t *orig, typeref_list_t *repl)
  918. {
  919. var_t *new_var = xmalloc(sizeof(*new_var));
  920. *new_var = *var;
  921. list_init(&new_var->entry);
  922. new_var->declspec.type = replace_type_parameters_in_type(var->declspec.type, orig, repl);
  923. return new_var;
  924. }
  925. static var_list_t *replace_type_parameters_in_var_list(var_list_t *var_list, typeref_list_t *orig, typeref_list_t *repl)
  926. {
  927. var_list_t *new_var_list;
  928. var_t *var, *new_var;
  929. if (!var_list) return var_list;
  930. new_var_list = xmalloc(sizeof(*new_var_list));
  931. list_init(new_var_list);
  932. LIST_FOR_EACH_ENTRY(var, var_list, var_t, entry)
  933. {
  934. new_var = replace_type_parameters_in_var(var, orig, repl);
  935. list_add_tail(new_var_list, &new_var->entry);
  936. }
  937. return new_var_list;
  938. }
  939. static statement_t *replace_type_parameters_in_statement(statement_t *stmt, typeref_list_t *orig, typeref_list_t *repl, loc_info_t *loc)
  940. {
  941. statement_t *new_stmt = xmalloc(sizeof(*new_stmt));
  942. *new_stmt = *stmt;
  943. list_init(&new_stmt->entry);
  944. switch (stmt->type)
  945. {
  946. case STMT_DECLARATION:
  947. new_stmt->u.var = replace_type_parameters_in_var(stmt->u.var, orig, repl);
  948. break;
  949. case STMT_TYPE:
  950. case STMT_TYPEREF:
  951. new_stmt->u.type = replace_type_parameters_in_type(stmt->u.type, orig, repl);
  952. break;
  953. case STMT_TYPEDEF:
  954. new_stmt->u.type_list = replace_type_parameters_in_type_list(stmt->u.type_list, orig, repl);
  955. break;
  956. case STMT_MODULE:
  957. case STMT_LIBRARY:
  958. case STMT_IMPORT:
  959. case STMT_IMPORTLIB:
  960. case STMT_PRAGMA:
  961. case STMT_CPPQUOTE:
  962. error_loc_info(loc, "unimplemented parameterized type replacement for statement type %d.\n", stmt->type);
  963. break;
  964. }
  965. return new_stmt;
  966. }
  967. static statement_list_t *replace_type_parameters_in_statement_list(statement_list_t *stmt_list, typeref_list_t *orig, typeref_list_t *repl, loc_info_t *loc)
  968. {
  969. statement_list_t *new_stmt_list;
  970. statement_t *stmt, *new_stmt;
  971. if (!stmt_list) return stmt_list;
  972. new_stmt_list = xmalloc(sizeof(*new_stmt_list));
  973. list_init(new_stmt_list);
  974. LIST_FOR_EACH_ENTRY(stmt, stmt_list, statement_t, entry)
  975. {
  976. new_stmt = replace_type_parameters_in_statement(stmt, orig, repl, loc);
  977. list_add_tail(new_stmt_list, &new_stmt->entry);
  978. }
  979. return new_stmt_list;
  980. }
  981. static type_t *replace_type_parameters_in_type(type_t *type, typeref_list_t *orig, typeref_list_t *repl)
  982. {
  983. struct list *o, *r;
  984. type_t *t;
  985. if (!type) return type;
  986. switch (type->type_type)
  987. {
  988. case TYPE_VOID:
  989. case TYPE_BASIC:
  990. case TYPE_ENUM:
  991. case TYPE_BITFIELD:
  992. case TYPE_INTERFACE:
  993. case TYPE_RUNTIMECLASS:
  994. case TYPE_DELEGATE:
  995. return type;
  996. case TYPE_PARAMETER:
  997. if (!orig || !repl) return NULL;
  998. for (o = list_head(orig), r = list_head(repl); o && r;
  999. o = list_next(orig, o), r = list_next(repl, r))
  1000. if (type == LIST_ENTRY(o, typeref_t, entry)->type)
  1001. return LIST_ENTRY(r, typeref_t, entry)->type;
  1002. return type;
  1003. case TYPE_POINTER:
  1004. t = replace_type_parameters_in_type(type->details.pointer.ref.type, orig, repl);
  1005. if (t == type->details.pointer.ref.type) return type;
  1006. type = duptype(type, 0);
  1007. type->details.pointer.ref.type = t;
  1008. return type;
  1009. case TYPE_ALIAS:
  1010. t = replace_type_parameters_in_type(type->details.alias.aliasee.type, orig, repl);
  1011. if (t == type->details.alias.aliasee.type) return type;
  1012. type = duptype(type, 0);
  1013. type->details.alias.aliasee.type = t;
  1014. return type;
  1015. case TYPE_ARRAY:
  1016. t = replace_type_parameters_in_type(type->details.array.elem.type, orig, repl);
  1017. if (t == t->details.array.elem.type) return type;
  1018. type = duptype(type, 0);
  1019. t->details.array.elem.type = t;
  1020. return type;
  1021. case TYPE_FUNCTION:
  1022. t = duptype(type, 0);
  1023. t->details.function = xmalloc(sizeof(*t->details.function));
  1024. t->details.function->args = replace_type_parameters_in_var_list(type->details.function->args, orig, repl);
  1025. t->details.function->retval = replace_type_parameters_in_var(type->details.function->retval, orig, repl);
  1026. return t;
  1027. case TYPE_PARAMETERIZED_TYPE:
  1028. t = type->details.parameterized.type;
  1029. if (t->type_type != TYPE_PARAMETERIZED_TYPE) return find_parameterized_type(type, repl);
  1030. repl = replace_type_parameters_in_type_list(type->details.parameterized.params, orig, repl);
  1031. return replace_type_parameters_in_type(t, t->details.parameterized.params, repl);
  1032. case TYPE_STRUCT:
  1033. case TYPE_ENCAPSULATED_UNION:
  1034. case TYPE_UNION:
  1035. case TYPE_MODULE:
  1036. case TYPE_COCLASS:
  1037. case TYPE_APICONTRACT:
  1038. error_loc_info(&type->loc_info, "unimplemented parameterized type replacement for type %s of type %d.\n", type->name, type->type_type);
  1039. break;
  1040. }
  1041. return type;
  1042. }
  1043. static void type_parameterized_interface_specialize(type_t *tmpl, type_t *iface, typeref_list_t *orig, typeref_list_t *repl)
  1044. {
  1045. iface->details.iface = xmalloc(sizeof(*iface->details.iface));
  1046. iface->details.iface->disp_methods = NULL;
  1047. iface->details.iface->disp_props = NULL;
  1048. iface->details.iface->stmts = replace_type_parameters_in_statement_list(tmpl->details.iface->stmts, orig, repl, &tmpl->loc_info);
  1049. iface->details.iface->inherit = replace_type_parameters_in_type(tmpl->details.iface->inherit, orig, repl);
  1050. iface->details.iface->disp_inherit = NULL;
  1051. iface->details.iface->async_iface = NULL;
  1052. iface->details.iface->requires = NULL;
  1053. }
  1054. static void type_parameterized_delegate_specialize(type_t *tmpl, type_t *delegate, typeref_list_t *orig, typeref_list_t *repl)
  1055. {
  1056. type_parameterized_interface_specialize(tmpl->details.delegate.iface, delegate->details.delegate.iface, orig, repl);
  1057. }
  1058. type_t *type_parameterized_type_specialize_declare(type_t *type, typeref_list_t *params)
  1059. {
  1060. type_t *tmpl = type->details.parameterized.type;
  1061. type_t *new_type = duptype(tmpl, 0);
  1062. new_type->namespace = type->namespace;
  1063. new_type->name = format_parameterized_type_name(type, params);
  1064. reg_type(new_type, new_type->name, new_type->namespace, 0);
  1065. new_type->c_name = format_parameterized_type_c_name(type, params, "");
  1066. new_type->short_name = format_parameterized_type_short_name(type, params, "");
  1067. if (new_type->type_type == TYPE_DELEGATE)
  1068. {
  1069. new_type->details.delegate.iface = duptype(tmpl->details.delegate.iface, 0);
  1070. compute_delegate_iface_names(new_type, type, params);
  1071. new_type->details.delegate.iface->short_name = format_parameterized_type_short_name(type, params, "I");
  1072. }
  1073. return new_type;
  1074. }
  1075. static void compute_interface_signature_uuid(type_t *iface)
  1076. {
  1077. static const char winrt_pinterface_namespace[] = {0x11,0xf4,0x7a,0xd5,0x7b,0x73,0x42,0xc0,0xab,0xae,0x87,0x8b,0x1e,0x16,0xad,0xee};
  1078. static const int version = 5;
  1079. struct sha1_context ctx;
  1080. unsigned char hash[20];
  1081. GUID *uuid;
  1082. if (!(uuid = get_attrp(iface->attrs, ATTR_UUID)))
  1083. {
  1084. uuid = xmalloc(sizeof(GUID));
  1085. iface->attrs = append_attr(iface->attrs, make_attrp(ATTR_UUID, uuid));
  1086. }
  1087. sha1_init(&ctx);
  1088. sha1_update(&ctx, winrt_pinterface_namespace, sizeof(winrt_pinterface_namespace));
  1089. sha1_update(&ctx, iface->signature, strlen(iface->signature));
  1090. sha1_finalize(&ctx, (ULONG *)hash);
  1091. /* https://tools.ietf.org/html/rfc4122:
  1092. * Set the four most significant bits (bits 12 through 15) of the
  1093. time_hi_and_version field to the appropriate 4-bit version number
  1094. from Section 4.1.3.
  1095. * Set the two most significant bits (bits 6 and 7) of the
  1096. clock_seq_hi_and_reserved to zero and one, respectively.
  1097. */
  1098. hash[6] = ((hash[6] & 0x0f) | (version << 4));
  1099. hash[8] = ((hash[8] & 0x3f) | 0x80);
  1100. uuid->Data1 = ((DWORD)hash[0] << 24)|((DWORD)hash[1] << 16)|((DWORD)hash[2] << 8)|(DWORD)hash[3];
  1101. uuid->Data2 = ((WORD)hash[4] << 8)|(WORD)hash[5];
  1102. uuid->Data3 = ((WORD)hash[6] << 8)|(WORD)hash[7];
  1103. memcpy(&uuid->Data4, hash + 8, sizeof(*uuid) - offsetof(GUID, Data4));
  1104. }
  1105. type_t *type_parameterized_type_specialize_define(type_t *type)
  1106. {
  1107. type_t *tmpl = type->details.parameterized.type;
  1108. typeref_list_t *orig = tmpl->details.parameterized.params;
  1109. typeref_list_t *repl = type->details.parameterized.params;
  1110. type_t *iface = find_parameterized_type(tmpl, repl);
  1111. if (type_get_type_detect_alias(type) != TYPE_PARAMETERIZED_TYPE ||
  1112. type_get_type_detect_alias(tmpl) != TYPE_PARAMETERIZED_TYPE)
  1113. error_loc("cannot define non-parameterized type %s, declared at %s:%d\n",
  1114. type->name, type->loc_info.input_name, type->loc_info.line_number);
  1115. if (type_get_type_detect_alias(tmpl->details.parameterized.type) == TYPE_INTERFACE &&
  1116. type_get_type_detect_alias(iface) == TYPE_INTERFACE)
  1117. type_parameterized_interface_specialize(tmpl->details.parameterized.type, iface, orig, repl);
  1118. else if (type_get_type_detect_alias(tmpl->details.parameterized.type) == TYPE_DELEGATE &&
  1119. type_get_type_detect_alias(iface) == TYPE_DELEGATE)
  1120. type_parameterized_delegate_specialize(tmpl->details.parameterized.type, iface, orig, repl);
  1121. else
  1122. error_loc("pinterface/pdelegate %s previously not declared a pinterface/pdelegate at %s:%d\n",
  1123. iface->name, iface->loc_info.input_name, iface->loc_info.line_number);
  1124. iface->impl_name = format_parameterized_type_impl_name(type, repl, "");
  1125. iface->signature = format_parameterized_type_signature(type, repl);
  1126. iface->defined = TRUE;
  1127. if (iface->type_type == TYPE_DELEGATE)
  1128. {
  1129. iface = iface->details.delegate.iface;
  1130. iface->impl_name = format_parameterized_type_impl_name(type, repl, "I");
  1131. iface->signature = format_parameterized_type_signature(type, repl);
  1132. iface->defined = TRUE;
  1133. }
  1134. compute_interface_signature_uuid(iface);
  1135. compute_method_indexes(iface);
  1136. return iface;
  1137. }
  1138. int type_is_equal(const type_t *type1, const type_t *type2)
  1139. {
  1140. if (type1 == type2)
  1141. return TRUE;
  1142. if (type_get_type_detect_alias(type1) != type_get_type_detect_alias(type2))
  1143. return FALSE;
  1144. if (type1->namespace != type2->namespace)
  1145. return FALSE;
  1146. if (type1->name && type2->name)
  1147. return !strcmp(type1->name, type2->name);
  1148. else if ((!type1->name && type2->name) || (type1->name && !type2->name))
  1149. return FALSE;
  1150. /* FIXME: do deep inspection of types to determine if they are equal */
  1151. return FALSE;
  1152. }