typetree.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 "widltypes.h"
  21. #include <assert.h>
  22. #include <stdio.h>
  23. #include "utils.h"
  24. #ifndef WIDL_TYPE_TREE_H
  25. #define WIDL_TYPE_TREE_H
  26. enum name_type {
  27. NAME_DEFAULT,
  28. NAME_C
  29. };
  30. attr_list_t *check_apicontract_attrs(const char *name, attr_list_t *attrs);
  31. attr_list_t *check_coclass_attrs(const char *name, attr_list_t *attrs);
  32. attr_list_t *check_dispiface_attrs(const char *name, attr_list_t *attrs);
  33. attr_list_t *check_interface_attrs(const char *name, attr_list_t *attrs);
  34. attr_list_t *check_module_attrs(const char *name, attr_list_t *attrs);
  35. attr_list_t *check_runtimeclass_attrs(const char *name, attr_list_t *attrs);
  36. type_t *find_parameterized_type(type_t *type, typeref_list_t *params);
  37. type_t *type_new_function(var_list_t *args);
  38. type_t *type_new_pointer(type_t *ref);
  39. type_t *type_new_alias(const decl_spec_t *t, const char *name);
  40. type_t *type_module_declare(char *name);
  41. type_t *type_new_array(const char *name, const decl_spec_t *element, int declptr,
  42. unsigned int dim, expr_t *size_is, expr_t *length_is);
  43. type_t *type_new_basic(enum type_basic_type basic_type);
  44. type_t *type_new_int(enum type_basic_type basic_type, int sign);
  45. type_t *type_new_void(void);
  46. type_t *type_coclass_declare(char *name);
  47. type_t *type_new_enum(const char *name, struct namespace *namespace, int defined, var_list_t *enums);
  48. type_t *type_new_struct(char *name, struct namespace *namespace, int defined, var_list_t *fields);
  49. type_t *type_new_nonencapsulated_union(const char *name, int defined, var_list_t *fields);
  50. type_t *type_new_encapsulated_union(char *name, var_t *switch_field, var_t *union_field, var_list_t *cases);
  51. type_t *type_new_bitfield(type_t *field_type, const expr_t *bits);
  52. type_t *type_runtimeclass_declare(char *name, struct namespace *namespace);
  53. type_t *type_interface_declare(char *name, struct namespace *namespace);
  54. type_t *type_interface_define(type_t *iface, attr_list_t *attrs, type_t *inherit, statement_list_t *stmts, typeref_list_t *requires);
  55. type_t *type_dispinterface_declare(char *name);
  56. type_t *type_dispinterface_define(type_t *iface, attr_list_t *attrs, var_list_t *props, var_list_t *methods);
  57. type_t *type_dispinterface_define_from_iface(type_t *dispiface, attr_list_t *attrs, type_t *iface);
  58. type_t *type_module_define(type_t* module, attr_list_t *attrs, statement_list_t *stmts);
  59. type_t *type_coclass_define(type_t *coclass, attr_list_t *attrs, typeref_list_t *ifaces);
  60. type_t *type_runtimeclass_define(type_t *runtimeclass, attr_list_t *attrs, typeref_list_t *ifaces);
  61. type_t *type_apicontract_declare(char *name, struct namespace *namespace);
  62. type_t *type_apicontract_define(type_t *apicontract, attr_list_t *attrs);
  63. type_t *type_delegate_declare(char *name, struct namespace *namespace);
  64. type_t *type_delegate_define(type_t *delegate, attr_list_t *attrs, statement_list_t *stmts);
  65. type_t *type_parameterized_interface_declare(char *name, struct namespace *namespace, typeref_list_t *params);
  66. type_t *type_parameterized_interface_define(type_t *type, attr_list_t *attrs, type_t *inherit, statement_list_t *stmts, typeref_list_t *requires);
  67. type_t *type_parameterized_delegate_declare(char *name, struct namespace *namespace, typeref_list_t *params);
  68. type_t *type_parameterized_delegate_define(type_t *type, attr_list_t *attrs, statement_list_t *stmts);
  69. type_t *type_parameterized_type_specialize_partial(type_t *type, typeref_list_t *params);
  70. type_t *type_parameterized_type_specialize_declare(type_t *type, typeref_list_t *params);
  71. type_t *type_parameterized_type_specialize_define(type_t *type);
  72. int type_is_equal(const type_t *type1, const type_t *type2);
  73. const char *type_get_name(const type_t *type, enum name_type name_type);
  74. const char *type_get_qualified_name(const type_t *type, enum name_type name_type);
  75. char *gen_name(void);
  76. extern int is_attr(const attr_list_t *list, enum attr_type t);
  77. typeref_t *make_typeref(type_t *type);
  78. typeref_list_t *append_typeref(typeref_list_t *list, typeref_t *ref);
  79. attr_t *make_attrp(enum attr_type type, void *val);
  80. attr_list_t *append_attr(attr_list_t *list, attr_t *attr);
  81. /* FIXME: shouldn't need to export this */
  82. type_t *duptype(type_t *t, int dupname);
  83. /* un-alias the type until finding the non-alias type */
  84. static inline type_t *type_get_real_type(const type_t *type)
  85. {
  86. if (type->type_type == TYPE_ALIAS)
  87. return type_get_real_type(type->details.alias.aliasee.type);
  88. else
  89. return (type_t *)type;
  90. }
  91. static inline type_t *type_parameterized_type_get_real_type(const type_t *type)
  92. {
  93. if (type->type_type == TYPE_PARAMETERIZED_TYPE)
  94. return type_parameterized_type_get_real_type(type->details.parameterized.type);
  95. else
  96. return (type_t *)type;
  97. }
  98. static inline enum type_type type_get_type(const type_t *type)
  99. {
  100. return type_get_type_detect_alias(type_get_real_type(type));
  101. }
  102. static inline enum type_basic_type type_basic_get_type(const type_t *type)
  103. {
  104. type = type_get_real_type(type);
  105. assert(type_get_type(type) == TYPE_BASIC);
  106. return type->details.basic.type;
  107. }
  108. static inline int type_basic_get_sign(const type_t *type)
  109. {
  110. type = type_get_real_type(type);
  111. assert(type_get_type(type) == TYPE_BASIC);
  112. return type->details.basic.sign;
  113. }
  114. static inline var_list_t *type_struct_get_fields(const type_t *type)
  115. {
  116. type = type_get_real_type(type);
  117. assert(type_get_type(type) == TYPE_STRUCT);
  118. return type->details.structure->fields;
  119. }
  120. static inline var_list_t *type_function_get_args(const type_t *type)
  121. {
  122. type = type_get_real_type(type);
  123. assert(type_get_type(type) == TYPE_FUNCTION);
  124. return type->details.function->args;
  125. }
  126. static inline var_t *type_function_get_retval(const type_t *type)
  127. {
  128. type = type_get_real_type(type);
  129. assert(type_get_type(type) == TYPE_FUNCTION);
  130. return type->details.function->retval;
  131. }
  132. static inline const decl_spec_t *type_function_get_ret(const type_t *type)
  133. {
  134. return &type_function_get_retval(type)->declspec;
  135. }
  136. static inline type_t *type_function_get_rettype(const type_t *type)
  137. {
  138. return type_function_get_retval(type)->declspec.type;
  139. }
  140. static inline var_list_t *type_enum_get_values(const type_t *type)
  141. {
  142. type = type_get_real_type(type);
  143. assert(type_get_type(type) == TYPE_ENUM);
  144. return type->details.enumeration->enums;
  145. }
  146. static inline var_t *type_union_get_switch_value(const type_t *type)
  147. {
  148. type = type_get_real_type(type);
  149. assert(type_get_type(type) == TYPE_ENCAPSULATED_UNION);
  150. return LIST_ENTRY(list_head(type->details.structure->fields), var_t, entry);
  151. }
  152. static inline var_list_t *type_encapsulated_union_get_fields(const type_t *type)
  153. {
  154. type = type_get_real_type(type);
  155. assert(type_get_type(type) == TYPE_ENCAPSULATED_UNION);
  156. return type->details.structure->fields;
  157. }
  158. static inline var_list_t *type_union_get_cases(const type_t *type)
  159. {
  160. enum type_type type_type;
  161. type = type_get_real_type(type);
  162. type_type = type_get_type(type);
  163. assert(type_type == TYPE_UNION || type_type == TYPE_ENCAPSULATED_UNION);
  164. if (type_type == TYPE_ENCAPSULATED_UNION)
  165. {
  166. const var_t *uv = LIST_ENTRY(list_tail(type->details.structure->fields), const var_t, entry);
  167. return uv->declspec.type->details.structure->fields;
  168. }
  169. else
  170. return type->details.structure->fields;
  171. }
  172. static inline statement_list_t *type_iface_get_stmts(const type_t *type)
  173. {
  174. type = type_get_real_type(type);
  175. assert(type_get_type(type) == TYPE_INTERFACE);
  176. return type->details.iface->stmts;
  177. }
  178. static inline type_t *type_iface_get_inherit(const type_t *type)
  179. {
  180. type = type_get_real_type(type);
  181. assert(type_get_type(type) == TYPE_INTERFACE);
  182. return type->details.iface->inherit;
  183. }
  184. static inline typeref_list_t *type_iface_get_requires(const type_t *type)
  185. {
  186. type = type_get_real_type(type);
  187. assert(type_get_type(type) == TYPE_INTERFACE);
  188. return type->details.iface->requires;
  189. }
  190. static inline type_t *type_iface_get_async_iface(const type_t *type)
  191. {
  192. type = type_get_real_type(type);
  193. assert(type_get_type(type) == TYPE_INTERFACE);
  194. return type->details.iface->async_iface;
  195. }
  196. static inline var_list_t *type_dispiface_get_props(const type_t *type)
  197. {
  198. type = type_get_real_type(type);
  199. assert(type_get_type(type) == TYPE_INTERFACE);
  200. return type->details.iface->disp_props;
  201. }
  202. static inline var_list_t *type_dispiface_get_methods(const type_t *type)
  203. {
  204. type = type_get_real_type(type);
  205. assert(type_get_type(type) == TYPE_INTERFACE);
  206. return type->details.iface->disp_methods;
  207. }
  208. static inline type_t *type_dispiface_get_inherit(const type_t *type)
  209. {
  210. type = type_get_real_type(type);
  211. assert(type_get_type(type) == TYPE_INTERFACE);
  212. return type->details.iface->disp_inherit;
  213. }
  214. static inline int type_is_defined(const type_t *type)
  215. {
  216. return type->defined;
  217. }
  218. static inline int type_is_complete(const type_t *type)
  219. {
  220. switch (type_get_type_detect_alias(type))
  221. {
  222. case TYPE_FUNCTION:
  223. return (type->details.function != NULL);
  224. case TYPE_INTERFACE:
  225. return (type->details.iface != NULL);
  226. case TYPE_ENUM:
  227. return (type->details.enumeration != NULL);
  228. case TYPE_UNION:
  229. case TYPE_ENCAPSULATED_UNION:
  230. case TYPE_STRUCT:
  231. return (type->details.structure != NULL);
  232. case TYPE_VOID:
  233. case TYPE_BASIC:
  234. case TYPE_ALIAS:
  235. case TYPE_MODULE:
  236. case TYPE_COCLASS:
  237. case TYPE_POINTER:
  238. case TYPE_ARRAY:
  239. case TYPE_BITFIELD:
  240. case TYPE_RUNTIMECLASS:
  241. case TYPE_DELEGATE:
  242. return TRUE;
  243. case TYPE_APICONTRACT:
  244. case TYPE_PARAMETERIZED_TYPE:
  245. case TYPE_PARAMETER:
  246. assert(0);
  247. break;
  248. }
  249. return FALSE;
  250. }
  251. static inline int type_array_has_conformance(const type_t *type)
  252. {
  253. type = type_get_real_type(type);
  254. assert(type_get_type(type) == TYPE_ARRAY);
  255. return (type->details.array.size_is != NULL);
  256. }
  257. static inline int type_array_has_variance(const type_t *type)
  258. {
  259. type = type_get_real_type(type);
  260. assert(type_get_type(type) == TYPE_ARRAY);
  261. return (type->details.array.length_is != NULL);
  262. }
  263. static inline unsigned int type_array_get_dim(const type_t *type)
  264. {
  265. type = type_get_real_type(type);
  266. assert(type_get_type(type) == TYPE_ARRAY);
  267. return type->details.array.dim;
  268. }
  269. static inline expr_t *type_array_get_conformance(const type_t *type)
  270. {
  271. type = type_get_real_type(type);
  272. assert(type_get_type(type) == TYPE_ARRAY);
  273. return type->details.array.size_is;
  274. }
  275. static inline expr_t *type_array_get_variance(const type_t *type)
  276. {
  277. type = type_get_real_type(type);
  278. assert(type_get_type(type) == TYPE_ARRAY);
  279. return type->details.array.length_is;
  280. }
  281. static inline unsigned short type_array_get_ptr_tfsoff(const type_t *type)
  282. {
  283. type = type_get_real_type(type);
  284. assert(type_get_type(type) == TYPE_ARRAY);
  285. return type->details.array.ptr_tfsoff;
  286. }
  287. static inline void type_array_set_ptr_tfsoff(type_t *type, unsigned short ptr_tfsoff)
  288. {
  289. type = type_get_real_type(type);
  290. assert(type_get_type(type) == TYPE_ARRAY);
  291. type->details.array.ptr_tfsoff = ptr_tfsoff;
  292. }
  293. static inline const decl_spec_t *type_array_get_element(const type_t *type)
  294. {
  295. type = type_get_real_type(type);
  296. assert(type_get_type(type) == TYPE_ARRAY);
  297. return &type->details.array.elem;
  298. }
  299. static inline type_t *type_array_get_element_type(const type_t *type)
  300. {
  301. return type_array_get_element(type)->type;
  302. }
  303. static inline int type_array_is_decl_as_ptr(const type_t *type)
  304. {
  305. type = type_get_real_type(type);
  306. assert(type_get_type(type) == TYPE_ARRAY);
  307. return type->details.array.declptr;
  308. }
  309. static inline int type_is_alias(const type_t *type)
  310. {
  311. return type->type_type == TYPE_ALIAS;
  312. }
  313. static inline const decl_spec_t *type_alias_get_aliasee(const type_t *type)
  314. {
  315. assert(type_is_alias(type));
  316. return &type->details.alias.aliasee;
  317. }
  318. static inline type_t *type_alias_get_aliasee_type(const type_t *type)
  319. {
  320. assert(type_is_alias(type));
  321. return type->details.alias.aliasee.type;
  322. }
  323. static inline typeref_list_t *type_coclass_get_ifaces(const type_t *type)
  324. {
  325. type = type_get_real_type(type);
  326. assert(type_get_type(type) == TYPE_COCLASS);
  327. return type->details.coclass.ifaces;
  328. }
  329. static inline typeref_list_t *type_runtimeclass_get_ifaces(const type_t *type)
  330. {
  331. type = type_get_real_type(type);
  332. assert(type_get_type(type) == TYPE_RUNTIMECLASS);
  333. return type->details.runtimeclass.ifaces;
  334. }
  335. static inline type_t *type_runtimeclass_get_default_iface(const type_t *type, int check)
  336. {
  337. typeref_list_t *ifaces = type_runtimeclass_get_ifaces(type);
  338. typeref_t *ref;
  339. if (ifaces) LIST_FOR_EACH_ENTRY(ref, ifaces, typeref_t, entry)
  340. if (is_attr(ref->attrs, ATTR_DEFAULT))
  341. return ref->type;
  342. if (!check) return NULL;
  343. error_loc_info(&type->loc_info, "runtimeclass %s needs a default interface\n", type->name);
  344. }
  345. static inline type_t *type_delegate_get_iface(const type_t *type)
  346. {
  347. type = type_get_real_type(type);
  348. assert(type_get_type(type) == TYPE_DELEGATE);
  349. return type->details.delegate.iface;
  350. }
  351. static inline const decl_spec_t *type_pointer_get_ref(const type_t *type)
  352. {
  353. type = type_get_real_type(type);
  354. assert(type_get_type(type) == TYPE_POINTER);
  355. return &type->details.pointer.ref;
  356. }
  357. static inline type_t *type_pointer_get_ref_type(const type_t *type)
  358. {
  359. return type_pointer_get_ref(type)->type;
  360. }
  361. static inline type_t *type_pointer_get_root_type(type_t *type)
  362. {
  363. for (; type && type->type_type == TYPE_POINTER; type = type_pointer_get_ref_type(type)) {}
  364. return type;
  365. }
  366. static inline type_t *type_bitfield_get_field(const type_t *type)
  367. {
  368. type = type_get_real_type(type);
  369. assert(type_get_type(type) == TYPE_BITFIELD);
  370. return type->details.bitfield.field;
  371. }
  372. static inline const expr_t *type_bitfield_get_bits(const type_t *type)
  373. {
  374. type = type_get_real_type(type);
  375. assert(type_get_type(type) == TYPE_BITFIELD);
  376. return type->details.bitfield.bits;
  377. }
  378. #endif /* WIDL_TYPE_TREE_H */