typetree.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. type_t *duptype(type_t *t, int dupname)
  30. {
  31. type_t *d = alloc_type();
  32. *d = *t;
  33. if (dupname && t->name)
  34. d->name = xstrdup(t->name);
  35. return d;
  36. }
  37. type_t *make_type(enum type_type type)
  38. {
  39. type_t *t = alloc_type();
  40. t->name = NULL;
  41. t->namespace = NULL;
  42. t->type_type = type;
  43. t->attrs = NULL;
  44. t->c_name = NULL;
  45. memset(&t->details, 0, sizeof(t->details));
  46. t->typestring_offset = 0;
  47. t->ptrdesc = 0;
  48. t->ignore = (parse_only != 0);
  49. t->defined = FALSE;
  50. t->written = FALSE;
  51. t->user_types_registered = FALSE;
  52. t->tfswrite = FALSE;
  53. t->checked = FALSE;
  54. t->typelib_idx = -1;
  55. init_loc_info(&t->loc_info);
  56. return t;
  57. }
  58. static const var_t *find_arg(const var_list_t *args, const char *name)
  59. {
  60. const var_t *arg;
  61. if (args) LIST_FOR_EACH_ENTRY(arg, args, const var_t, entry)
  62. {
  63. if (arg->name && !strcmp(name, arg->name))
  64. return arg;
  65. }
  66. return NULL;
  67. }
  68. const char *type_get_name(const type_t *type, enum name_type name_type)
  69. {
  70. switch(name_type) {
  71. case NAME_DEFAULT:
  72. return type->name;
  73. case NAME_C:
  74. return type->c_name;
  75. }
  76. assert(0);
  77. return NULL;
  78. }
  79. static char *append_namespace(char *ptr, struct namespace *namespace, const char *separator)
  80. {
  81. if(is_global_namespace(namespace)) {
  82. if(!use_abi_namespace)
  83. return ptr;
  84. strcpy(ptr, "ABI");
  85. strcat(ptr, separator);
  86. return ptr + strlen(ptr);
  87. }
  88. ptr = append_namespace(ptr, namespace->parent, separator);
  89. strcpy(ptr, namespace->name);
  90. strcat(ptr, separator);
  91. return ptr + strlen(ptr);
  92. }
  93. char *format_namespace(struct namespace *namespace, const char *prefix, const char *separator, const char *suffix)
  94. {
  95. unsigned len = strlen(prefix) + strlen(suffix);
  96. unsigned sep_len = strlen(separator);
  97. struct namespace *iter;
  98. char *ret, *ptr;
  99. if(use_abi_namespace && !is_global_namespace(namespace))
  100. len += 3 /* strlen("ABI") */ + sep_len;
  101. for(iter = namespace; !is_global_namespace(iter); iter = iter->parent)
  102. len += strlen(iter->name) + sep_len;
  103. ret = xmalloc(len+1);
  104. strcpy(ret, prefix);
  105. ptr = append_namespace(ret + strlen(ret), namespace, separator);
  106. strcpy(ptr, suffix);
  107. return ret;
  108. }
  109. type_t *type_new_function(var_list_t *args)
  110. {
  111. var_t *arg;
  112. type_t *t;
  113. unsigned int i = 0;
  114. if (args)
  115. {
  116. arg = LIST_ENTRY(list_head(args), var_t, entry);
  117. if (list_count(args) == 1 && !arg->name && arg->declspec.type && type_get_type(arg->declspec.type) == TYPE_VOID)
  118. {
  119. list_remove(&arg->entry);
  120. free(arg);
  121. free(args);
  122. args = NULL;
  123. }
  124. }
  125. if (args) LIST_FOR_EACH_ENTRY(arg, args, var_t, entry)
  126. {
  127. if (arg->declspec.type && type_get_type(arg->declspec.type) == TYPE_VOID)
  128. error_loc("argument '%s' has void type\n", arg->name);
  129. if (!arg->name)
  130. {
  131. if (i > 26 * 26)
  132. error_loc("too many unnamed arguments\n");
  133. else
  134. {
  135. int unique;
  136. do
  137. {
  138. char name[3];
  139. name[0] = i > 26 ? 'a' + i / 26 : 'a' + i;
  140. name[1] = i > 26 ? 'a' + i % 26 : 0;
  141. name[2] = 0;
  142. unique = !find_arg(args, name);
  143. if (unique)
  144. arg->name = xstrdup(name);
  145. i++;
  146. } while (!unique);
  147. }
  148. }
  149. }
  150. t = make_type(TYPE_FUNCTION);
  151. t->details.function = xmalloc(sizeof(*t->details.function));
  152. t->details.function->args = args;
  153. t->details.function->retval = make_var(xstrdup("_RetVal"));
  154. return t;
  155. }
  156. type_t *type_new_pointer(type_t *ref)
  157. {
  158. type_t *t = make_type(TYPE_POINTER);
  159. t->details.pointer.ref.type = ref;
  160. return t;
  161. }
  162. type_t *type_new_alias(const decl_spec_t *t, const char *name)
  163. {
  164. type_t *a = make_type(TYPE_ALIAS);
  165. a->name = xstrdup(name);
  166. a->attrs = NULL;
  167. a->details.alias.aliasee = *t;
  168. init_loc_info(&a->loc_info);
  169. return a;
  170. }
  171. type_t *type_new_module(char *name)
  172. {
  173. type_t *type = get_type(TYPE_MODULE, name, NULL, 0);
  174. if (type->type_type != TYPE_MODULE || type->defined)
  175. error_loc("%s: redefinition error; original definition was at %s:%d\n",
  176. type->name, type->loc_info.input_name, type->loc_info.line_number);
  177. type->name = name;
  178. return type;
  179. }
  180. type_t *type_new_coclass(char *name)
  181. {
  182. type_t *type = get_type(TYPE_COCLASS, name, NULL, 0);
  183. if (type->type_type != TYPE_COCLASS || type->defined)
  184. error_loc("%s: redefinition error; original definition was at %s:%d\n",
  185. type->name, type->loc_info.input_name, type->loc_info.line_number);
  186. type->name = name;
  187. return type;
  188. }
  189. type_t *type_new_array(const char *name, const decl_spec_t *element, int declptr,
  190. unsigned int dim, expr_t *size_is, expr_t *length_is)
  191. {
  192. type_t *t = make_type(TYPE_ARRAY);
  193. if (name) t->name = xstrdup(name);
  194. t->details.array.declptr = declptr;
  195. t->details.array.length_is = length_is;
  196. if (size_is)
  197. t->details.array.size_is = size_is;
  198. else
  199. t->details.array.dim = dim;
  200. if (element)
  201. t->details.array.elem = *element;
  202. return t;
  203. }
  204. type_t *type_new_basic(enum type_basic_type basic_type)
  205. {
  206. type_t *t = make_type(TYPE_BASIC);
  207. t->details.basic.type = basic_type;
  208. t->details.basic.sign = 0;
  209. return t;
  210. }
  211. type_t *type_new_int(enum type_basic_type basic_type, int sign)
  212. {
  213. static type_t *int_types[TYPE_BASIC_INT_MAX+1][3];
  214. assert(basic_type <= TYPE_BASIC_INT_MAX);
  215. /* map sign { -1, 0, 1 } -> { 0, 1, 2 } */
  216. if (!int_types[basic_type][sign + 1])
  217. {
  218. int_types[basic_type][sign + 1] = type_new_basic(basic_type);
  219. int_types[basic_type][sign + 1]->details.basic.sign = sign;
  220. }
  221. return int_types[basic_type][sign + 1];
  222. }
  223. type_t *type_new_void(void)
  224. {
  225. static type_t *void_type = NULL;
  226. if (!void_type)
  227. void_type = make_type(TYPE_VOID);
  228. return void_type;
  229. }
  230. type_t *type_new_enum(const char *name, struct namespace *namespace, int defined, var_list_t *enums)
  231. {
  232. type_t *t = NULL;
  233. if (name)
  234. t = find_type(name, namespace,tsENUM);
  235. if (!t)
  236. {
  237. t = make_type(TYPE_ENUM);
  238. t->name = name;
  239. t->namespace = namespace;
  240. if (name)
  241. reg_type(t, name, namespace, tsENUM);
  242. }
  243. if (!t->defined && defined)
  244. {
  245. t->details.enumeration = xmalloc(sizeof(*t->details.enumeration));
  246. t->details.enumeration->enums = enums;
  247. t->defined = TRUE;
  248. }
  249. else if (defined)
  250. error_loc("redefinition of enum %s\n", name);
  251. return t;
  252. }
  253. type_t *type_new_struct(char *name, struct namespace *namespace, int defined, var_list_t *fields)
  254. {
  255. type_t *t = NULL;
  256. if (name)
  257. t = find_type(name, namespace, tsSTRUCT);
  258. if (!t)
  259. {
  260. t = make_type(TYPE_STRUCT);
  261. t->name = name;
  262. t->namespace = namespace;
  263. if (name)
  264. reg_type(t, name, namespace, tsSTRUCT);
  265. }
  266. if (!t->defined && defined)
  267. {
  268. t->details.structure = xmalloc(sizeof(*t->details.structure));
  269. t->details.structure->fields = fields;
  270. t->defined = TRUE;
  271. }
  272. else if (defined)
  273. error_loc("redefinition of struct %s\n", name);
  274. return t;
  275. }
  276. type_t *type_new_nonencapsulated_union(const char *name, int defined, var_list_t *fields)
  277. {
  278. type_t *t = NULL;
  279. if (name)
  280. t = find_type(name, NULL, tsUNION);
  281. if (!t)
  282. {
  283. t = make_type(TYPE_UNION);
  284. t->name = name;
  285. if (name)
  286. reg_type(t, name, NULL, tsUNION);
  287. }
  288. if (!t->defined && defined)
  289. {
  290. t->details.structure = xmalloc(sizeof(*t->details.structure));
  291. t->details.structure->fields = fields;
  292. t->defined = TRUE;
  293. }
  294. else if (defined)
  295. error_loc("redefinition of union %s\n", name);
  296. return t;
  297. }
  298. type_t *type_new_encapsulated_union(char *name, var_t *switch_field, var_t *union_field, var_list_t *cases)
  299. {
  300. type_t *t = NULL;
  301. if (name)
  302. t = find_type(name, NULL, tsUNION);
  303. if (!t)
  304. {
  305. t = make_type(TYPE_ENCAPSULATED_UNION);
  306. t->name = name;
  307. if (name)
  308. reg_type(t, name, NULL, tsUNION);
  309. }
  310. t->type_type = TYPE_ENCAPSULATED_UNION;
  311. if (!t->defined)
  312. {
  313. if (!union_field)
  314. union_field = make_var(xstrdup("tagged_union"));
  315. union_field->declspec.type = type_new_nonencapsulated_union(gen_name(), TRUE, cases);
  316. t->details.structure = xmalloc(sizeof(*t->details.structure));
  317. t->details.structure->fields = append_var(NULL, switch_field);
  318. t->details.structure->fields = append_var(t->details.structure->fields, union_field);
  319. t->defined = TRUE;
  320. }
  321. else
  322. error_loc("redefinition of union %s\n", name);
  323. return t;
  324. }
  325. static int is_valid_bitfield_type(const type_t *type)
  326. {
  327. switch (type_get_type(type))
  328. {
  329. case TYPE_ENUM:
  330. return TRUE;
  331. case TYPE_BASIC:
  332. switch (type_basic_get_type(type))
  333. {
  334. case TYPE_BASIC_INT8:
  335. case TYPE_BASIC_INT16:
  336. case TYPE_BASIC_INT32:
  337. case TYPE_BASIC_INT64:
  338. case TYPE_BASIC_INT:
  339. case TYPE_BASIC_INT3264:
  340. case TYPE_BASIC_LONG:
  341. case TYPE_BASIC_CHAR:
  342. case TYPE_BASIC_HYPER:
  343. case TYPE_BASIC_BYTE:
  344. case TYPE_BASIC_WCHAR:
  345. case TYPE_BASIC_ERROR_STATUS_T:
  346. return TRUE;
  347. case TYPE_BASIC_FLOAT:
  348. case TYPE_BASIC_DOUBLE:
  349. case TYPE_BASIC_HANDLE:
  350. return FALSE;
  351. }
  352. return FALSE;
  353. default:
  354. return FALSE;
  355. }
  356. }
  357. type_t *type_new_bitfield(type_t *field, const expr_t *bits)
  358. {
  359. type_t *t;
  360. if (!is_valid_bitfield_type(field))
  361. error_loc("bit-field has invalid type\n");
  362. if (bits->cval < 0)
  363. error_loc("negative width for bit-field\n");
  364. /* FIXME: validate bits->cval <= memsize(field) * 8 */
  365. t = make_type(TYPE_BITFIELD);
  366. t->details.bitfield.field = field;
  367. t->details.bitfield.bits = bits;
  368. return t;
  369. }
  370. static unsigned int compute_method_indexes(type_t *iface)
  371. {
  372. unsigned int idx;
  373. statement_t *stmt;
  374. if (!iface->details.iface)
  375. return 0;
  376. if (type_iface_get_inherit(iface))
  377. idx = compute_method_indexes(type_iface_get_inherit(iface));
  378. else
  379. idx = 0;
  380. STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
  381. {
  382. var_t *func = stmt->u.var;
  383. if (!is_callas(func->attrs))
  384. func->func_idx = idx++;
  385. }
  386. return idx;
  387. }
  388. void type_interface_define(type_t *iface, type_t *inherit, statement_list_t *stmts)
  389. {
  390. iface->details.iface = xmalloc(sizeof(*iface->details.iface));
  391. iface->details.iface->disp_props = NULL;
  392. iface->details.iface->disp_methods = NULL;
  393. iface->details.iface->stmts = stmts;
  394. iface->details.iface->inherit = inherit;
  395. iface->details.iface->disp_inherit = NULL;
  396. iface->details.iface->async_iface = NULL;
  397. iface->defined = TRUE;
  398. compute_method_indexes(iface);
  399. }
  400. void type_dispinterface_define(type_t *iface, var_list_t *props, var_list_t *methods)
  401. {
  402. iface->details.iface = xmalloc(sizeof(*iface->details.iface));
  403. iface->details.iface->disp_props = props;
  404. iface->details.iface->disp_methods = methods;
  405. iface->details.iface->stmts = NULL;
  406. iface->details.iface->inherit = find_type("IDispatch", NULL, 0);
  407. if (!iface->details.iface->inherit) error_loc("IDispatch is undefined\n");
  408. iface->details.iface->disp_inherit = NULL;
  409. iface->details.iface->async_iface = NULL;
  410. iface->defined = TRUE;
  411. compute_method_indexes(iface);
  412. }
  413. void type_dispinterface_define_from_iface(type_t *dispiface, type_t *iface)
  414. {
  415. dispiface->details.iface = xmalloc(sizeof(*dispiface->details.iface));
  416. dispiface->details.iface->disp_props = NULL;
  417. dispiface->details.iface->disp_methods = NULL;
  418. dispiface->details.iface->stmts = NULL;
  419. dispiface->details.iface->inherit = find_type("IDispatch", NULL, 0);
  420. if (!dispiface->details.iface->inherit) error_loc("IDispatch is undefined\n");
  421. dispiface->details.iface->disp_inherit = iface;
  422. dispiface->details.iface->async_iface = NULL;
  423. dispiface->defined = TRUE;
  424. compute_method_indexes(dispiface);
  425. }
  426. void type_module_define(type_t *module, statement_list_t *stmts)
  427. {
  428. if (module->details.module) error_loc("multiple definition error\n");
  429. module->details.module = xmalloc(sizeof(*module->details.module));
  430. module->details.module->stmts = stmts;
  431. module->defined = TRUE;
  432. }
  433. type_t *type_coclass_define(type_t *coclass, ifref_list_t *ifaces)
  434. {
  435. coclass->details.coclass.ifaces = ifaces;
  436. coclass->defined = TRUE;
  437. return coclass;
  438. }
  439. int type_is_equal(const type_t *type1, const type_t *type2)
  440. {
  441. if (type_get_type_detect_alias(type1) != type_get_type_detect_alias(type2))
  442. return FALSE;
  443. if (type1->name && type2->name)
  444. return !strcmp(type1->name, type2->name);
  445. else if ((!type1->name && type2->name) || (type1->name && !type2->name))
  446. return FALSE;
  447. /* FIXME: do deep inspection of types to determine if they are equal */
  448. return FALSE;
  449. }