cp-objcp-common.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /* Some code common to C++ and ObjC++ front ends.
  2. Copyright (C) 2004-2015 Free Software Foundation, Inc.
  3. Contributed by Ziemowit Laski <zlaski@apple.com>
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "config.h"
  17. #include "system.h"
  18. #include "coretypes.h"
  19. #include "tm.h"
  20. #include "hash-set.h"
  21. #include "machmode.h"
  22. #include "vec.h"
  23. #include "double-int.h"
  24. #include "input.h"
  25. #include "alias.h"
  26. #include "symtab.h"
  27. #include "wide-int.h"
  28. #include "inchash.h"
  29. #include "tree.h"
  30. #include "cp-tree.h"
  31. #include "c-family/c-common.h"
  32. #include "langhooks.h"
  33. #include "langhooks-def.h"
  34. #include "diagnostic.h"
  35. #include "debug.h"
  36. #include "cxx-pretty-print.h"
  37. #include "cp-objcp-common.h"
  38. /* Special routine to get the alias set for C++. */
  39. alias_set_type
  40. cxx_get_alias_set (tree t)
  41. {
  42. if (IS_FAKE_BASE_TYPE (t))
  43. /* The base variant of a type must be in the same alias set as the
  44. complete type. */
  45. return get_alias_set (TYPE_CONTEXT (t));
  46. /* Punt on PMFs until we canonicalize functions properly. */
  47. if (TYPE_PTRMEMFUNC_P (t)
  48. || (POINTER_TYPE_P (t)
  49. && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))))
  50. return 0;
  51. return c_common_get_alias_set (t);
  52. }
  53. /* Called from check_global_declarations. */
  54. bool
  55. cxx_warn_unused_global_decl (const_tree decl)
  56. {
  57. if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl))
  58. return false;
  59. if (DECL_IN_SYSTEM_HEADER (decl))
  60. return false;
  61. /* Const variables take the place of #defines in C++. */
  62. if (VAR_P (decl) && TREE_READONLY (decl))
  63. return false;
  64. return true;
  65. }
  66. /* Langhook for tree_size: determine size of our 'x' and 'c' nodes. */
  67. size_t
  68. cp_tree_size (enum tree_code code)
  69. {
  70. switch (code)
  71. {
  72. case PTRMEM_CST: return sizeof (struct ptrmem_cst);
  73. case BASELINK: return sizeof (struct tree_baselink);
  74. case TEMPLATE_PARM_INDEX: return sizeof (template_parm_index);
  75. case DEFAULT_ARG: return sizeof (struct tree_default_arg);
  76. case DEFERRED_NOEXCEPT: return sizeof (struct tree_deferred_noexcept);
  77. case OVERLOAD: return sizeof (struct tree_overload);
  78. case STATIC_ASSERT: return sizeof (struct tree_static_assert);
  79. case TYPE_ARGUMENT_PACK:
  80. case TYPE_PACK_EXPANSION:
  81. return sizeof (struct tree_common);
  82. case NONTYPE_ARGUMENT_PACK:
  83. case EXPR_PACK_EXPANSION:
  84. return sizeof (struct tree_exp);
  85. case ARGUMENT_PACK_SELECT:
  86. return sizeof (struct tree_argument_pack_select);
  87. case TRAIT_EXPR:
  88. return sizeof (struct tree_trait_expr);
  89. case LAMBDA_EXPR: return sizeof (struct tree_lambda_expr);
  90. case TEMPLATE_INFO: return sizeof (struct tree_template_info);
  91. case USERDEF_LITERAL: return sizeof (struct tree_userdef_literal);
  92. case TEMPLATE_DECL: return sizeof (struct tree_template_decl);
  93. default:
  94. if (TREE_CODE_CLASS (code) == tcc_declaration)
  95. return sizeof (struct tree_decl_non_common);
  96. gcc_unreachable ();
  97. }
  98. /* NOTREACHED */
  99. }
  100. /* Returns true if T is a variably modified type, in the sense of C99.
  101. FN is as passed to variably_modified_p.
  102. This routine needs only check cases that cannot be handled by the
  103. language-independent logic in tree.c. */
  104. bool
  105. cp_var_mod_type_p (tree type, tree fn)
  106. {
  107. /* If TYPE is a pointer-to-member, it is variably modified if either
  108. the class or the member are variably modified. */
  109. if (TYPE_PTRMEM_P (type))
  110. return (variably_modified_type_p (TYPE_PTRMEM_CLASS_TYPE (type), fn)
  111. || variably_modified_type_p (TYPE_PTRMEM_POINTED_TO_TYPE (type),
  112. fn));
  113. /* All other types are not variably modified. */
  114. return false;
  115. }
  116. /* This compares two types for equivalence ("compatible" in C-based languages).
  117. This routine should only return 1 if it is sure. It should not be used
  118. in contexts where erroneously returning 0 causes problems. */
  119. int
  120. cxx_types_compatible_p (tree x, tree y)
  121. {
  122. return same_type_ignoring_top_level_qualifiers_p (x, y);
  123. }
  124. /* Return true if DECL is explicit member function. */
  125. bool
  126. cp_function_decl_explicit_p (tree decl)
  127. {
  128. return (decl
  129. && DECL_LANG_SPECIFIC (STRIP_TEMPLATE (decl))
  130. && DECL_NONCONVERTING_P (decl));
  131. }
  132. /* Return true if DECL is deleted special member function. */
  133. bool
  134. cp_function_decl_deleted_p (tree decl)
  135. {
  136. return (decl
  137. && DECL_LANG_SPECIFIC (STRIP_TEMPLATE (decl))
  138. && DECL_DELETED_FN (decl));
  139. }
  140. /* Stubs to keep c-opts.c happy. */
  141. void
  142. push_file_scope (void)
  143. {
  144. }
  145. void
  146. pop_file_scope (void)
  147. {
  148. }
  149. /* c-pragma.c needs to query whether a decl has extern "C" linkage. */
  150. bool
  151. has_c_linkage (const_tree decl)
  152. {
  153. return DECL_EXTERN_C_P (decl);
  154. }
  155. static GTY ((cache))
  156. hash_table<tree_decl_map_cache_hasher> *shadowed_var_for_decl;
  157. /* Lookup a shadowed var for FROM, and return it if we find one. */
  158. tree
  159. decl_shadowed_for_var_lookup (tree from)
  160. {
  161. struct tree_decl_map *h, in;
  162. in.base.from = from;
  163. h = shadowed_var_for_decl->find_with_hash (&in, DECL_UID (from));
  164. if (h)
  165. return h->to;
  166. return NULL_TREE;
  167. }
  168. /* Insert a mapping FROM->TO in the shadowed var hashtable. */
  169. void
  170. decl_shadowed_for_var_insert (tree from, tree to)
  171. {
  172. struct tree_decl_map *h;
  173. h = ggc_alloc<tree_decl_map> ();
  174. h->base.from = from;
  175. h->to = to;
  176. *shadowed_var_for_decl->find_slot_with_hash (h, DECL_UID (from), INSERT) = h;
  177. }
  178. void
  179. init_shadowed_var_for_decl (void)
  180. {
  181. shadowed_var_for_decl
  182. = hash_table<tree_decl_map_cache_hasher>::create_ggc (512);
  183. }
  184. /* Return true if stmt can fall through. Used by block_may_fallthru
  185. default case. */
  186. bool
  187. cxx_block_may_fallthru (const_tree stmt)
  188. {
  189. switch (TREE_CODE (stmt))
  190. {
  191. case EXPR_STMT:
  192. return block_may_fallthru (EXPR_STMT_EXPR (stmt));
  193. case THROW_EXPR:
  194. return false;
  195. default:
  196. return true;
  197. }
  198. }
  199. void
  200. cp_common_init_ts (void)
  201. {
  202. MARK_TS_DECL_NON_COMMON (USING_DECL);
  203. MARK_TS_DECL_COMMON (TEMPLATE_DECL);
  204. MARK_TS_COMMON (TEMPLATE_TEMPLATE_PARM);
  205. MARK_TS_COMMON (TEMPLATE_TYPE_PARM);
  206. MARK_TS_COMMON (TEMPLATE_PARM_INDEX);
  207. MARK_TS_COMMON (OVERLOAD);
  208. MARK_TS_COMMON (TEMPLATE_INFO);
  209. MARK_TS_COMMON (TYPENAME_TYPE);
  210. MARK_TS_COMMON (TYPEOF_TYPE);
  211. MARK_TS_COMMON (UNDERLYING_TYPE);
  212. MARK_TS_COMMON (BASELINK);
  213. MARK_TS_COMMON (TYPE_PACK_EXPANSION);
  214. MARK_TS_COMMON (TYPE_ARGUMENT_PACK);
  215. MARK_TS_COMMON (DECLTYPE_TYPE);
  216. MARK_TS_COMMON (BOUND_TEMPLATE_TEMPLATE_PARM);
  217. MARK_TS_COMMON (UNBOUND_CLASS_TEMPLATE);
  218. MARK_TS_TYPED (EXPR_PACK_EXPANSION);
  219. MARK_TS_TYPED (SWITCH_STMT);
  220. MARK_TS_TYPED (IF_STMT);
  221. MARK_TS_TYPED (FOR_STMT);
  222. MARK_TS_TYPED (RANGE_FOR_STMT);
  223. MARK_TS_TYPED (AGGR_INIT_EXPR);
  224. MARK_TS_TYPED (EXPR_STMT);
  225. MARK_TS_TYPED (EH_SPEC_BLOCK);
  226. MARK_TS_TYPED (CLEANUP_STMT);
  227. MARK_TS_TYPED (SCOPE_REF);
  228. MARK_TS_TYPED (CAST_EXPR);
  229. MARK_TS_TYPED (NON_DEPENDENT_EXPR);
  230. MARK_TS_TYPED (MODOP_EXPR);
  231. MARK_TS_TYPED (TRY_BLOCK);
  232. MARK_TS_TYPED (THROW_EXPR);
  233. MARK_TS_TYPED (HANDLER);
  234. MARK_TS_TYPED (REINTERPRET_CAST_EXPR);
  235. MARK_TS_TYPED (CONST_CAST_EXPR);
  236. MARK_TS_TYPED (STATIC_CAST_EXPR);
  237. MARK_TS_TYPED (DYNAMIC_CAST_EXPR);
  238. MARK_TS_TYPED (IMPLICIT_CONV_EXPR);
  239. MARK_TS_TYPED (TEMPLATE_ID_EXPR);
  240. MARK_TS_TYPED (ARROW_EXPR);
  241. MARK_TS_TYPED (SIZEOF_EXPR);
  242. MARK_TS_TYPED (ALIGNOF_EXPR);
  243. MARK_TS_TYPED (AT_ENCODE_EXPR);
  244. MARK_TS_TYPED (UNARY_PLUS_EXPR);
  245. MARK_TS_TYPED (TRAIT_EXPR);
  246. MARK_TS_TYPED (TYPE_ARGUMENT_PACK);
  247. MARK_TS_TYPED (NOEXCEPT_EXPR);
  248. MARK_TS_TYPED (NONTYPE_ARGUMENT_PACK);
  249. MARK_TS_TYPED (WHILE_STMT);
  250. MARK_TS_TYPED (NEW_EXPR);
  251. MARK_TS_TYPED (VEC_NEW_EXPR);
  252. MARK_TS_TYPED (BREAK_STMT);
  253. MARK_TS_TYPED (MEMBER_REF);
  254. MARK_TS_TYPED (DOTSTAR_EXPR);
  255. MARK_TS_TYPED (DO_STMT);
  256. MARK_TS_TYPED (DELETE_EXPR);
  257. MARK_TS_TYPED (VEC_DELETE_EXPR);
  258. MARK_TS_TYPED (CONTINUE_STMT);
  259. MARK_TS_TYPED (TAG_DEFN);
  260. MARK_TS_TYPED (PSEUDO_DTOR_EXPR);
  261. MARK_TS_TYPED (TYPEID_EXPR);
  262. MARK_TS_TYPED (MUST_NOT_THROW_EXPR);
  263. MARK_TS_TYPED (STMT_EXPR);
  264. MARK_TS_TYPED (OFFSET_REF);
  265. MARK_TS_TYPED (OFFSETOF_EXPR);
  266. MARK_TS_TYPED (PTRMEM_CST);
  267. MARK_TS_TYPED (EMPTY_CLASS_EXPR);
  268. MARK_TS_TYPED (VEC_INIT_EXPR);
  269. MARK_TS_TYPED (USING_STMT);
  270. MARK_TS_TYPED (LAMBDA_EXPR);
  271. MARK_TS_TYPED (CTOR_INITIALIZER);
  272. MARK_TS_TYPED (ARRAY_NOTATION_REF);
  273. }
  274. #include "gt-cp-cp-objcp-common.h"