except.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  1. /* Handle exceptional things in C++.
  2. Copyright (C) 1989-2015 Free Software Foundation, Inc.
  3. Contributed by Michael Tiemann <tiemann@cygnus.com>
  4. Rewritten by Mike Stump <mrs@cygnus.com>, based upon an
  5. initial re-implementation courtesy Tad Hunt.
  6. This file is part of GCC.
  7. GCC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 3, or (at your option)
  10. any later version.
  11. GCC 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
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with GCC; see the file COPYING3. If not see
  17. <http://www.gnu.org/licenses/>. */
  18. #include "config.h"
  19. #include "system.h"
  20. #include "coretypes.h"
  21. #include "tm.h"
  22. #include "hash-set.h"
  23. #include "machmode.h"
  24. #include "vec.h"
  25. #include "double-int.h"
  26. #include "input.h"
  27. #include "alias.h"
  28. #include "symtab.h"
  29. #include "wide-int.h"
  30. #include "inchash.h"
  31. #include "tree.h"
  32. #include "stringpool.h"
  33. #include "trans-mem.h"
  34. #include "attribs.h"
  35. #include "cp-tree.h"
  36. #include "flags.h"
  37. #include "tree-inline.h"
  38. #include "tree-iterator.h"
  39. #include "target.h"
  40. static void push_eh_cleanup (tree);
  41. static tree prepare_eh_type (tree);
  42. static tree do_begin_catch (void);
  43. static int dtor_nothrow (tree);
  44. static tree do_end_catch (tree);
  45. static bool decl_is_java_type (tree decl, int err);
  46. static void initialize_handler_parm (tree, tree);
  47. static tree do_allocate_exception (tree);
  48. static tree wrap_cleanups_r (tree *, int *, void *);
  49. static int complete_ptr_ref_or_void_ptr_p (tree, tree);
  50. static bool is_admissible_throw_operand_or_catch_parameter (tree, bool);
  51. static int can_convert_eh (tree, tree);
  52. /* Sets up all the global eh stuff that needs to be initialized at the
  53. start of compilation. */
  54. void
  55. init_exception_processing (void)
  56. {
  57. tree tmp;
  58. /* void std::terminate (); */
  59. push_namespace (std_identifier);
  60. tmp = build_function_type_list (void_type_node, NULL_TREE);
  61. terminate_node = build_cp_library_fn_ptr ("terminate", tmp,
  62. ECF_NOTHROW | ECF_NORETURN);
  63. TREE_THIS_VOLATILE (terminate_node) = 1;
  64. TREE_NOTHROW (terminate_node) = 1;
  65. pop_namespace ();
  66. /* void __cxa_call_unexpected(void *); */
  67. tmp = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
  68. call_unexpected_node
  69. = push_throw_library_fn (get_identifier ("__cxa_call_unexpected"), tmp);
  70. }
  71. /* Returns an expression to be executed if an unhandled exception is
  72. propagated out of a cleanup region. */
  73. tree
  74. cp_protect_cleanup_actions (void)
  75. {
  76. /* [except.terminate]
  77. When the destruction of an object during stack unwinding exits
  78. using an exception ... void terminate(); is called. */
  79. return terminate_node;
  80. }
  81. static tree
  82. prepare_eh_type (tree type)
  83. {
  84. if (type == NULL_TREE)
  85. return type;
  86. if (type == error_mark_node)
  87. return error_mark_node;
  88. /* peel back references, so they match. */
  89. type = non_reference (type);
  90. /* Peel off cv qualifiers. */
  91. type = TYPE_MAIN_VARIANT (type);
  92. /* Functions and arrays decay to pointers. */
  93. type = type_decays_to (type);
  94. return type;
  95. }
  96. /* Return the type info for TYPE as used by EH machinery. */
  97. tree
  98. eh_type_info (tree type)
  99. {
  100. tree exp;
  101. if (type == NULL_TREE || type == error_mark_node)
  102. return type;
  103. if (decl_is_java_type (type, 0))
  104. exp = build_java_class_ref (TREE_TYPE (type));
  105. else
  106. exp = get_tinfo_decl (type);
  107. return exp;
  108. }
  109. /* Build the address of a typeinfo decl for use in the runtime
  110. matching field of the exception model. */
  111. tree
  112. build_eh_type_type (tree type)
  113. {
  114. tree exp = eh_type_info (type);
  115. if (!exp)
  116. return NULL;
  117. mark_used (exp);
  118. return convert (ptr_type_node, build_address (exp));
  119. }
  120. tree
  121. build_exc_ptr (void)
  122. {
  123. return build_call_n (builtin_decl_explicit (BUILT_IN_EH_POINTER),
  124. 1, integer_zero_node);
  125. }
  126. /* Declare a function NAME, returning RETURN_TYPE, taking a single
  127. parameter PARM_TYPE, with an empty exception specification.
  128. Note that the C++ ABI document does not have a throw-specifier on
  129. the routines declared below via this function. The declarations
  130. are consistent with the actual implementations in libsupc++. */
  131. static tree
  132. declare_library_fn (tree name, tree return_type, tree parm_type, int ecf_flags)
  133. {
  134. return push_library_fn (name, build_function_type_list (return_type,
  135. parm_type,
  136. NULL_TREE),
  137. empty_except_spec,
  138. ecf_flags);
  139. }
  140. /* Build up a call to __cxa_get_exception_ptr so that we can build a
  141. copy constructor for the thrown object. */
  142. static tree
  143. do_get_exception_ptr (void)
  144. {
  145. tree fn;
  146. fn = get_identifier ("__cxa_get_exception_ptr");
  147. if (!get_global_value_if_present (fn, &fn))
  148. {
  149. /* Declare void* __cxa_get_exception_ptr (void *) throw(). */
  150. fn = declare_library_fn (fn, ptr_type_node, ptr_type_node,
  151. ECF_NOTHROW | ECF_PURE | ECF_LEAF | ECF_TM_PURE);
  152. }
  153. return cp_build_function_call_nary (fn, tf_warning_or_error,
  154. build_exc_ptr (), NULL_TREE);
  155. }
  156. /* Build up a call to __cxa_begin_catch, to tell the runtime that the
  157. exception has been handled. */
  158. static tree
  159. do_begin_catch (void)
  160. {
  161. tree fn;
  162. fn = get_identifier ("__cxa_begin_catch");
  163. if (!get_global_value_if_present (fn, &fn))
  164. {
  165. /* Declare void* __cxa_begin_catch (void *) throw(). */
  166. fn = declare_library_fn (fn, ptr_type_node, ptr_type_node, ECF_NOTHROW);
  167. /* Create its transactional-memory equivalent. */
  168. if (flag_tm)
  169. {
  170. tree fn2 = get_identifier ("_ITM_cxa_begin_catch");
  171. if (!get_global_value_if_present (fn2, &fn2))
  172. fn2 = declare_library_fn (fn2, ptr_type_node,
  173. ptr_type_node, ECF_NOTHROW | ECF_TM_PURE);
  174. record_tm_replacement (fn, fn2);
  175. }
  176. }
  177. return cp_build_function_call_nary (fn, tf_warning_or_error,
  178. build_exc_ptr (), NULL_TREE);
  179. }
  180. /* Returns nonzero if cleaning up an exception of type TYPE (which can be
  181. NULL_TREE for a ... handler) will not throw an exception. */
  182. static int
  183. dtor_nothrow (tree type)
  184. {
  185. if (type == NULL_TREE || type == error_mark_node)
  186. return 0;
  187. if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
  188. return 1;
  189. if (CLASSTYPE_LAZY_DESTRUCTOR (type))
  190. lazily_declare_fn (sfk_destructor, type);
  191. return TREE_NOTHROW (CLASSTYPE_DESTRUCTORS (type));
  192. }
  193. /* Build up a call to __cxa_end_catch, to destroy the exception object
  194. for the current catch block if no others are currently using it. */
  195. static tree
  196. do_end_catch (tree type)
  197. {
  198. tree fn, cleanup;
  199. fn = get_identifier ("__cxa_end_catch");
  200. if (!get_global_value_if_present (fn, &fn))
  201. {
  202. /* Declare void __cxa_end_catch ().
  203. This can throw if the destructor for the exception throws. */
  204. fn = push_void_library_fn (fn, void_list_node, 0);
  205. /* Create its transactional-memory equivalent. */
  206. if (flag_tm)
  207. {
  208. tree fn2 = get_identifier ("_ITM_cxa_end_catch");
  209. if (!get_global_value_if_present (fn2, &fn2))
  210. fn2 = push_void_library_fn (fn2, void_list_node, ECF_TM_PURE);
  211. record_tm_replacement (fn, fn2);
  212. }
  213. }
  214. cleanup = cp_build_function_call_vec (fn, NULL, tf_warning_or_error);
  215. TREE_NOTHROW (cleanup) = dtor_nothrow (type);
  216. return cleanup;
  217. }
  218. /* This routine creates the cleanup for the current exception. */
  219. static void
  220. push_eh_cleanup (tree type)
  221. {
  222. finish_decl_cleanup (NULL_TREE, do_end_catch (type));
  223. }
  224. /* Return nonzero value if DECL is a Java type suitable for catch or
  225. throw. */
  226. static bool
  227. decl_is_java_type (tree decl, int err)
  228. {
  229. bool r = (TYPE_PTR_P (decl)
  230. && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
  231. && TYPE_FOR_JAVA (TREE_TYPE (decl)));
  232. if (err)
  233. {
  234. if (TREE_CODE (decl) == REFERENCE_TYPE
  235. && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
  236. && TYPE_FOR_JAVA (TREE_TYPE (decl)))
  237. {
  238. /* Can't throw a reference. */
  239. error ("type %qT is disallowed in Java %<throw%> or %<catch%>",
  240. decl);
  241. }
  242. if (r)
  243. {
  244. tree jthrow_node
  245. = IDENTIFIER_GLOBAL_VALUE (get_identifier ("jthrowable"));
  246. if (jthrow_node == NULL_TREE)
  247. fatal_error
  248. (input_location,
  249. "call to Java %<catch%> or %<throw%> with %<jthrowable%> undefined");
  250. jthrow_node = TREE_TYPE (TREE_TYPE (jthrow_node));
  251. if (! DERIVED_FROM_P (jthrow_node, TREE_TYPE (decl)))
  252. {
  253. /* Thrown object must be a Throwable. */
  254. error ("type %qT is not derived from %<java::lang::Throwable%>",
  255. TREE_TYPE (decl));
  256. }
  257. }
  258. }
  259. return r;
  260. }
  261. /* Select the personality routine to be used for exception handling,
  262. or issue an error if we need two different ones in the same
  263. translation unit.
  264. ??? At present DECL_FUNCTION_PERSONALITY is set via
  265. LANG_HOOKS_EH_PERSONALITY. Should it be done here instead? */
  266. void
  267. choose_personality_routine (enum languages lang)
  268. {
  269. static enum {
  270. chose_none,
  271. chose_cpp,
  272. chose_java,
  273. gave_error
  274. } state;
  275. switch (state)
  276. {
  277. case gave_error:
  278. return;
  279. case chose_cpp:
  280. if (lang != lang_cplusplus)
  281. goto give_error;
  282. return;
  283. case chose_java:
  284. if (lang != lang_java)
  285. goto give_error;
  286. return;
  287. case chose_none:
  288. ; /* Proceed to language selection. */
  289. }
  290. switch (lang)
  291. {
  292. case lang_cplusplus:
  293. state = chose_cpp;
  294. break;
  295. case lang_java:
  296. state = chose_java;
  297. terminate_node = builtin_decl_explicit (BUILT_IN_ABORT);
  298. pragma_java_exceptions = true;
  299. break;
  300. default:
  301. gcc_unreachable ();
  302. }
  303. return;
  304. give_error:
  305. error ("mixing C++ and Java catches in a single translation unit");
  306. state = gave_error;
  307. }
  308. /* Wrap EXPR in a MUST_NOT_THROW_EXPR expressing that EXPR must
  309. not throw any exceptions if COND is true. A condition of
  310. NULL_TREE is treated as 'true'. */
  311. tree
  312. build_must_not_throw_expr (tree body, tree cond)
  313. {
  314. tree type = body ? TREE_TYPE (body) : void_type_node;
  315. if (!flag_exceptions)
  316. return body;
  317. if (cond && !value_dependent_expression_p (cond))
  318. {
  319. cond = cxx_constant_value (cond);
  320. if (integer_zerop (cond))
  321. return body;
  322. else if (integer_onep (cond))
  323. cond = NULL_TREE;
  324. }
  325. return build2 (MUST_NOT_THROW_EXPR, type, body, cond);
  326. }
  327. /* Initialize the catch parameter DECL. */
  328. static void
  329. initialize_handler_parm (tree decl, tree exp)
  330. {
  331. tree init;
  332. tree init_type;
  333. /* Make sure we mark the catch param as used, otherwise we'll get a
  334. warning about an unused ((anonymous)). */
  335. TREE_USED (decl) = 1;
  336. DECL_READ_P (decl) = 1;
  337. /* Figure out the type that the initializer is. Pointers are returned
  338. adjusted by value from __cxa_begin_catch. Others are returned by
  339. reference. */
  340. init_type = TREE_TYPE (decl);
  341. if (!POINTER_TYPE_P (init_type))
  342. init_type = build_reference_type (init_type);
  343. choose_personality_routine (decl_is_java_type (init_type, 0)
  344. ? lang_java : lang_cplusplus);
  345. /* Since pointers are passed by value, initialize a reference to
  346. pointer catch parm with the address of the temporary. */
  347. if (TREE_CODE (init_type) == REFERENCE_TYPE
  348. && TYPE_PTR_P (TREE_TYPE (init_type)))
  349. exp = cp_build_addr_expr (exp, tf_warning_or_error);
  350. exp = ocp_convert (init_type, exp, CONV_IMPLICIT|CONV_FORCE_TEMP, 0,
  351. tf_warning_or_error);
  352. init = convert_from_reference (exp);
  353. /* If the constructor for the catch parm exits via an exception, we
  354. must call terminate. See eh23.C. */
  355. if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
  356. {
  357. /* Generate the copy constructor call directly so we can wrap it.
  358. See also expand_default_init. */
  359. init = ocp_convert (TREE_TYPE (decl), init,
  360. CONV_IMPLICIT|CONV_FORCE_TEMP, 0,
  361. tf_warning_or_error);
  362. /* Force cleanups now to avoid nesting problems with the
  363. MUST_NOT_THROW_EXPR. */
  364. init = fold_build_cleanup_point_expr (TREE_TYPE (init), init);
  365. init = build_must_not_throw_expr (init, NULL_TREE);
  366. }
  367. decl = pushdecl (decl);
  368. start_decl_1 (decl, true);
  369. cp_finish_decl (decl, init, /*init_const_expr_p=*/false, NULL_TREE,
  370. LOOKUP_ONLYCONVERTING|DIRECT_BIND);
  371. }
  372. /* Routine to see if exception handling is turned on.
  373. DO_WARN is nonzero if we want to inform the user that exception
  374. handling is turned off.
  375. This is used to ensure that -fexceptions has been specified if the
  376. compiler tries to use any exception-specific functions. */
  377. static inline int
  378. doing_eh (void)
  379. {
  380. if (! flag_exceptions)
  381. {
  382. static int warned = 0;
  383. if (! warned)
  384. {
  385. error ("exception handling disabled, use -fexceptions to enable");
  386. warned = 1;
  387. }
  388. return 0;
  389. }
  390. return 1;
  391. }
  392. /* Call this to start a catch block. DECL is the catch parameter. */
  393. tree
  394. expand_start_catch_block (tree decl)
  395. {
  396. tree exp;
  397. tree type, init;
  398. if (! doing_eh ())
  399. return NULL_TREE;
  400. if (decl)
  401. {
  402. if (!is_admissible_throw_operand_or_catch_parameter (decl, false))
  403. decl = error_mark_node;
  404. type = prepare_eh_type (TREE_TYPE (decl));
  405. mark_used (eh_type_info (type));
  406. }
  407. else
  408. type = NULL_TREE;
  409. if (decl && decl_is_java_type (type, 1))
  410. {
  411. /* Java only passes object via pointer and doesn't require
  412. adjusting. The java object is immediately before the
  413. generic exception header. */
  414. exp = build_exc_ptr ();
  415. exp = build1 (NOP_EXPR, build_pointer_type (type), exp);
  416. exp = fold_build_pointer_plus (exp,
  417. fold_build1_loc (input_location,
  418. NEGATE_EXPR, sizetype,
  419. TYPE_SIZE_UNIT (TREE_TYPE (exp))));
  420. exp = cp_build_indirect_ref (exp, RO_NULL, tf_warning_or_error);
  421. initialize_handler_parm (decl, exp);
  422. return type;
  423. }
  424. /* Call __cxa_end_catch at the end of processing the exception. */
  425. push_eh_cleanup (type);
  426. init = do_begin_catch ();
  427. /* If there's no decl at all, then all we need to do is make sure
  428. to tell the runtime that we've begun handling the exception. */
  429. if (decl == NULL || decl == error_mark_node || init == error_mark_node)
  430. finish_expr_stmt (init);
  431. /* If the C++ object needs constructing, we need to do that before
  432. calling __cxa_begin_catch, so that std::uncaught_exception gets
  433. the right value during the copy constructor. */
  434. else if (flag_use_cxa_get_exception_ptr
  435. && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
  436. {
  437. exp = do_get_exception_ptr ();
  438. initialize_handler_parm (decl, exp);
  439. finish_expr_stmt (init);
  440. }
  441. /* Otherwise the type uses a bitwise copy, and we don't have to worry
  442. about the value of std::uncaught_exception and therefore can do the
  443. copy with the return value of __cxa_end_catch instead. */
  444. else
  445. {
  446. tree init_type = type;
  447. /* Pointers are passed by values, everything else by reference. */
  448. if (!TYPE_PTR_P (type))
  449. init_type = build_pointer_type (type);
  450. if (init_type != TREE_TYPE (init))
  451. init = build1 (NOP_EXPR, init_type, init);
  452. exp = create_temporary_var (init_type);
  453. DECL_REGISTER (exp) = 1;
  454. cp_finish_decl (exp, init, /*init_const_expr=*/false,
  455. NULL_TREE, LOOKUP_ONLYCONVERTING);
  456. initialize_handler_parm (decl, exp);
  457. }
  458. return type;
  459. }
  460. /* Call this to end a catch block. Its responsible for emitting the
  461. code to handle jumping back to the correct place, and for emitting
  462. the label to jump to if this catch block didn't match. */
  463. void
  464. expand_end_catch_block (void)
  465. {
  466. if (! doing_eh ())
  467. return;
  468. /* The exception being handled is rethrown if control reaches the end of
  469. a handler of the function-try-block of a constructor or destructor. */
  470. if (in_function_try_handler
  471. && (DECL_CONSTRUCTOR_P (current_function_decl)
  472. || DECL_DESTRUCTOR_P (current_function_decl)))
  473. finish_expr_stmt (build_throw (NULL_TREE));
  474. }
  475. tree
  476. begin_eh_spec_block (void)
  477. {
  478. tree r;
  479. location_t spec_location = DECL_SOURCE_LOCATION (current_function_decl);
  480. /* A noexcept specification (or throw() with -fnothrow-opt) is a
  481. MUST_NOT_THROW_EXPR. */
  482. if (TYPE_NOEXCEPT_P (TREE_TYPE (current_function_decl)))
  483. {
  484. r = build_stmt (spec_location, MUST_NOT_THROW_EXPR,
  485. NULL_TREE, NULL_TREE);
  486. TREE_SIDE_EFFECTS (r) = 1;
  487. }
  488. else
  489. r = build_stmt (spec_location, EH_SPEC_BLOCK, NULL_TREE, NULL_TREE);
  490. add_stmt (r);
  491. TREE_OPERAND (r, 0) = push_stmt_list ();
  492. return r;
  493. }
  494. void
  495. finish_eh_spec_block (tree raw_raises, tree eh_spec_block)
  496. {
  497. tree raises;
  498. TREE_OPERAND (eh_spec_block, 0)
  499. = pop_stmt_list (TREE_OPERAND (eh_spec_block, 0));
  500. if (TREE_CODE (eh_spec_block) == MUST_NOT_THROW_EXPR)
  501. return;
  502. /* Strip cv quals, etc, from the specification types. */
  503. for (raises = NULL_TREE;
  504. raw_raises && TREE_VALUE (raw_raises);
  505. raw_raises = TREE_CHAIN (raw_raises))
  506. {
  507. tree type = prepare_eh_type (TREE_VALUE (raw_raises));
  508. tree tinfo = eh_type_info (type);
  509. mark_used (tinfo);
  510. raises = tree_cons (NULL_TREE, type, raises);
  511. }
  512. EH_SPEC_RAISES (eh_spec_block) = raises;
  513. }
  514. /* Return a pointer to a buffer for an exception object of type TYPE. */
  515. static tree
  516. do_allocate_exception (tree type)
  517. {
  518. tree fn;
  519. fn = get_identifier ("__cxa_allocate_exception");
  520. if (!get_global_value_if_present (fn, &fn))
  521. {
  522. /* Declare void *__cxa_allocate_exception(size_t) throw(). */
  523. fn = declare_library_fn (fn, ptr_type_node, size_type_node,
  524. ECF_NOTHROW | ECF_MALLOC);
  525. if (flag_tm)
  526. {
  527. tree fn2 = get_identifier ("_ITM_cxa_allocate_exception");
  528. if (!get_global_value_if_present (fn2, &fn2))
  529. fn2 = declare_library_fn (fn2, ptr_type_node,
  530. size_type_node,
  531. ECF_NOTHROW | ECF_MALLOC | ECF_TM_PURE);
  532. record_tm_replacement (fn, fn2);
  533. }
  534. }
  535. return cp_build_function_call_nary (fn, tf_warning_or_error,
  536. size_in_bytes (type), NULL_TREE);
  537. }
  538. /* Call __cxa_free_exception from a cleanup. This is never invoked
  539. directly, but see the comment for stabilize_throw_expr. */
  540. static tree
  541. do_free_exception (tree ptr)
  542. {
  543. tree fn;
  544. fn = get_identifier ("__cxa_free_exception");
  545. if (!get_global_value_if_present (fn, &fn))
  546. {
  547. /* Declare void __cxa_free_exception (void *) throw(). */
  548. fn = declare_library_fn (fn, void_type_node, ptr_type_node,
  549. ECF_NOTHROW | ECF_LEAF);
  550. }
  551. return cp_build_function_call_nary (fn, tf_warning_or_error, ptr, NULL_TREE);
  552. }
  553. /* Wrap all cleanups for TARGET_EXPRs in MUST_NOT_THROW_EXPR.
  554. Called from build_throw via walk_tree_without_duplicates. */
  555. static tree
  556. wrap_cleanups_r (tree *tp, int *walk_subtrees, void * /*data*/)
  557. {
  558. tree exp = *tp;
  559. tree cleanup;
  560. /* Don't walk into types. */
  561. if (TYPE_P (exp))
  562. {
  563. *walk_subtrees = 0;
  564. return NULL_TREE;
  565. }
  566. if (TREE_CODE (exp) != TARGET_EXPR)
  567. return NULL_TREE;
  568. cleanup = TARGET_EXPR_CLEANUP (exp);
  569. if (cleanup)
  570. {
  571. cleanup = build2 (MUST_NOT_THROW_EXPR, void_type_node, cleanup,
  572. NULL_TREE);
  573. TARGET_EXPR_CLEANUP (exp) = cleanup;
  574. }
  575. /* Keep iterating. */
  576. return NULL_TREE;
  577. }
  578. /* Build a throw expression. */
  579. tree
  580. build_throw (tree exp)
  581. {
  582. tree fn;
  583. if (exp == error_mark_node)
  584. return exp;
  585. if (processing_template_decl)
  586. {
  587. if (cfun)
  588. current_function_returns_abnormally = 1;
  589. exp = build_min (THROW_EXPR, void_type_node, exp);
  590. SET_EXPR_LOCATION (exp, input_location);
  591. return exp;
  592. }
  593. if (exp == null_node)
  594. warning (0, "throwing NULL, which has integral, not pointer type");
  595. if (exp != NULL_TREE)
  596. {
  597. if (!is_admissible_throw_operand_or_catch_parameter (exp, true))
  598. return error_mark_node;
  599. }
  600. if (! doing_eh ())
  601. return error_mark_node;
  602. if (exp && decl_is_java_type (TREE_TYPE (exp), 1))
  603. {
  604. tree fn = get_identifier ("_Jv_Throw");
  605. if (!get_global_value_if_present (fn, &fn))
  606. {
  607. /* Declare void _Jv_Throw (void *). */
  608. tree tmp;
  609. tmp = build_function_type_list (ptr_type_node,
  610. ptr_type_node, NULL_TREE);
  611. fn = push_throw_library_fn (fn, tmp);
  612. }
  613. else if (really_overloaded_fn (fn))
  614. {
  615. error ("%qD should never be overloaded", fn);
  616. return error_mark_node;
  617. }
  618. fn = OVL_CURRENT (fn);
  619. exp = cp_build_function_call_nary (fn, tf_warning_or_error,
  620. exp, NULL_TREE);
  621. }
  622. else if (exp)
  623. {
  624. tree throw_type;
  625. tree temp_type;
  626. tree cleanup;
  627. tree object, ptr;
  628. tree tmp;
  629. tree allocate_expr;
  630. /* The CLEANUP_TYPE is the internal type of a destructor. */
  631. if (!cleanup_type)
  632. {
  633. tmp = build_function_type_list (void_type_node,
  634. ptr_type_node, NULL_TREE);
  635. cleanup_type = build_pointer_type (tmp);
  636. }
  637. fn = get_identifier ("__cxa_throw");
  638. if (!get_global_value_if_present (fn, &fn))
  639. {
  640. /* Declare void __cxa_throw (void*, void*, void (*)(void*)). */
  641. /* ??? Second argument is supposed to be "std::type_info*". */
  642. tmp = build_function_type_list (void_type_node,
  643. ptr_type_node, ptr_type_node,
  644. cleanup_type, NULL_TREE);
  645. fn = push_throw_library_fn (fn, tmp);
  646. if (flag_tm)
  647. {
  648. tree fn2 = get_identifier ("_ITM_cxa_throw");
  649. if (!get_global_value_if_present (fn2, &fn2))
  650. fn2 = push_throw_library_fn (fn2, tmp);
  651. apply_tm_attr (fn2, get_identifier ("transaction_pure"));
  652. record_tm_replacement (fn, fn2);
  653. }
  654. }
  655. /* [except.throw]
  656. A throw-expression initializes a temporary object, the type
  657. of which is determined by removing any top-level
  658. cv-qualifiers from the static type of the operand of throw
  659. and adjusting the type from "array of T" or "function return
  660. T" to "pointer to T" or "pointer to function returning T"
  661. respectively. */
  662. temp_type = is_bitfield_expr_with_lowered_type (exp);
  663. if (!temp_type)
  664. temp_type = cv_unqualified (type_decays_to (TREE_TYPE (exp)));
  665. /* OK, this is kind of wacky. The standard says that we call
  666. terminate when the exception handling mechanism, after
  667. completing evaluation of the expression to be thrown but
  668. before the exception is caught (_except.throw_), calls a
  669. user function that exits via an uncaught exception.
  670. So we have to protect the actual initialization of the
  671. exception object with terminate(), but evaluate the
  672. expression first. Since there could be temps in the
  673. expression, we need to handle that, too. We also expand
  674. the call to __cxa_allocate_exception first (which doesn't
  675. matter, since it can't throw). */
  676. /* Allocate the space for the exception. */
  677. allocate_expr = do_allocate_exception (temp_type);
  678. allocate_expr = get_target_expr (allocate_expr);
  679. ptr = TARGET_EXPR_SLOT (allocate_expr);
  680. TARGET_EXPR_CLEANUP (allocate_expr) = do_free_exception (ptr);
  681. CLEANUP_EH_ONLY (allocate_expr) = 1;
  682. object = build_nop (build_pointer_type (temp_type), ptr);
  683. object = cp_build_indirect_ref (object, RO_NULL, tf_warning_or_error);
  684. /* And initialize the exception object. */
  685. if (CLASS_TYPE_P (temp_type))
  686. {
  687. int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
  688. vec<tree, va_gc> *exp_vec;
  689. /* Under C++0x [12.8/16 class.copy], a thrown lvalue is sometimes
  690. treated as an rvalue for the purposes of overload resolution
  691. to favor move constructors over copy constructors. */
  692. if (/* Must be a local, automatic variable. */
  693. VAR_P (exp)
  694. && DECL_CONTEXT (exp) == current_function_decl
  695. && ! TREE_STATIC (exp)
  696. /* The variable must not have the `volatile' qualifier. */
  697. && !(cp_type_quals (TREE_TYPE (exp)) & TYPE_QUAL_VOLATILE))
  698. flags = flags | LOOKUP_PREFER_RVALUE;
  699. /* Call the copy constructor. */
  700. exp_vec = make_tree_vector_single (exp);
  701. exp = (build_special_member_call
  702. (object, complete_ctor_identifier, &exp_vec,
  703. TREE_TYPE (object), flags, tf_warning_or_error));
  704. release_tree_vector (exp_vec);
  705. if (exp == error_mark_node)
  706. {
  707. error (" in thrown expression");
  708. return error_mark_node;
  709. }
  710. }
  711. else
  712. {
  713. tmp = decay_conversion (exp, tf_warning_or_error);
  714. if (tmp == error_mark_node)
  715. return error_mark_node;
  716. exp = build2 (INIT_EXPR, temp_type, object, tmp);
  717. }
  718. /* Mark any cleanups from the initialization as MUST_NOT_THROW, since
  719. they are run after the exception object is initialized. */
  720. cp_walk_tree_without_duplicates (&exp, wrap_cleanups_r, 0);
  721. /* Prepend the allocation. */
  722. exp = build2 (COMPOUND_EXPR, TREE_TYPE (exp), allocate_expr, exp);
  723. /* Force all the cleanups to be evaluated here so that we don't have
  724. to do them during unwinding. */
  725. exp = build1 (CLEANUP_POINT_EXPR, void_type_node, exp);
  726. throw_type = build_eh_type_type (prepare_eh_type (TREE_TYPE (object)));
  727. cleanup = NULL_TREE;
  728. if (type_build_dtor_call (TREE_TYPE (object)))
  729. {
  730. tree fn = lookup_fnfields (TYPE_BINFO (TREE_TYPE (object)),
  731. complete_dtor_identifier, 0);
  732. fn = BASELINK_FUNCTIONS (fn);
  733. mark_used (fn);
  734. if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (object)))
  735. {
  736. cxx_mark_addressable (fn);
  737. /* Pretend it's a normal function. */
  738. cleanup = build1 (ADDR_EXPR, cleanup_type, fn);
  739. }
  740. }
  741. if (cleanup == NULL_TREE)
  742. cleanup = build_int_cst (cleanup_type, 0);
  743. /* ??? Indicate that this function call throws throw_type. */
  744. tmp = cp_build_function_call_nary (fn, tf_warning_or_error,
  745. ptr, throw_type, cleanup, NULL_TREE);
  746. /* Tack on the initialization stuff. */
  747. exp = build2 (COMPOUND_EXPR, TREE_TYPE (tmp), exp, tmp);
  748. }
  749. else
  750. {
  751. /* Rethrow current exception. */
  752. tree fn = get_identifier ("__cxa_rethrow");
  753. if (!get_global_value_if_present (fn, &fn))
  754. {
  755. /* Declare void __cxa_rethrow (void). */
  756. fn = push_throw_library_fn
  757. (fn, build_function_type_list (void_type_node, NULL_TREE));
  758. }
  759. if (flag_tm)
  760. apply_tm_attr (fn, get_identifier ("transaction_pure"));
  761. /* ??? Indicate that this function call allows exceptions of the type
  762. of the enclosing catch block (if known). */
  763. exp = cp_build_function_call_vec (fn, NULL, tf_warning_or_error);
  764. }
  765. exp = build1 (THROW_EXPR, void_type_node, exp);
  766. SET_EXPR_LOCATION (exp, input_location);
  767. return exp;
  768. }
  769. /* Make sure TYPE is complete, pointer to complete, reference to
  770. complete, or pointer to cv void. Issue diagnostic on failure.
  771. Return the zero on failure and nonzero on success. FROM can be
  772. the expr or decl from whence TYPE came, if available. */
  773. static int
  774. complete_ptr_ref_or_void_ptr_p (tree type, tree from)
  775. {
  776. int is_ptr;
  777. /* Check complete. */
  778. type = complete_type_or_else (type, from);
  779. if (!type)
  780. return 0;
  781. /* Or a pointer or ref to one, or cv void *. */
  782. is_ptr = TYPE_PTR_P (type);
  783. if (is_ptr || TREE_CODE (type) == REFERENCE_TYPE)
  784. {
  785. tree core = TREE_TYPE (type);
  786. if (is_ptr && VOID_TYPE_P (core))
  787. /* OK */;
  788. else if (!complete_type_or_else (core, from))
  789. return 0;
  790. }
  791. return 1;
  792. }
  793. /* If IS_THROW is true return truth-value if T is an expression admissible
  794. in throw-expression, i.e. if it is not of incomplete type or a pointer/
  795. reference to such a type or of an abstract class type.
  796. If IS_THROW is false, likewise for a catch parameter, same requirements
  797. for its type plus rvalue reference type is also not admissible. */
  798. static bool
  799. is_admissible_throw_operand_or_catch_parameter (tree t, bool is_throw)
  800. {
  801. tree expr = is_throw ? t : NULL_TREE;
  802. tree type = TREE_TYPE (t);
  803. /* C++11 [except.handle] The exception-declaration shall not denote
  804. an incomplete type, an abstract class type, or an rvalue reference
  805. type. */
  806. /* 15.1/4 [...] The type of the throw-expression shall not be an
  807. incomplete type, or a pointer or a reference to an incomplete
  808. type, other than void*, const void*, volatile void*, or
  809. const volatile void*. Except for these restriction and the
  810. restrictions on type matching mentioned in 15.3, the operand
  811. of throw is treated exactly as a function argument in a call
  812. (5.2.2) or the operand of a return statement. */
  813. if (!complete_ptr_ref_or_void_ptr_p (type, expr))
  814. return false;
  815. /* 10.4/3 An abstract class shall not be used as a parameter type,
  816. as a function return type or as type of an explicit
  817. conversion. */
  818. else if (abstract_virtuals_error (is_throw ? ACU_THROW : ACU_CATCH, type))
  819. return false;
  820. else if (!is_throw
  821. && TREE_CODE (type) == REFERENCE_TYPE
  822. && TYPE_REF_IS_RVALUE (type))
  823. {
  824. error ("cannot declare catch parameter to be of rvalue "
  825. "reference type %qT", type);
  826. return false;
  827. }
  828. else if (variably_modified_type_p (type, NULL_TREE))
  829. {
  830. if (is_throw)
  831. error ("cannot throw expression of type %qT because it involves "
  832. "types of variable size", type);
  833. else
  834. error ("cannot catch type %qT because it involves types of "
  835. "variable size", type);
  836. return false;
  837. }
  838. return true;
  839. }
  840. /* Returns nonzero if FN is a declaration of a standard C library
  841. function which is known not to throw.
  842. [lib.res.on.exception.handling]: None of the functions from the
  843. Standard C library shall report an error by throwing an
  844. exception, unless it calls a program-supplied function that
  845. throws an exception. */
  846. #include "cfns.h"
  847. int
  848. nothrow_libfn_p (const_tree fn)
  849. {
  850. tree id;
  851. if (TREE_PUBLIC (fn)
  852. && DECL_EXTERNAL (fn)
  853. && DECL_NAMESPACE_SCOPE_P (fn)
  854. && DECL_EXTERN_C_P (fn))
  855. /* OK */;
  856. else
  857. /* Can't be a C library function. */
  858. return 0;
  859. /* Being a C library function, DECL_ASSEMBLER_NAME == DECL_NAME
  860. unless the system headers are playing rename tricks, and if
  861. they are, we don't want to be confused by them. */
  862. id = DECL_NAME (fn);
  863. return !!libc_name_p (IDENTIFIER_POINTER (id), IDENTIFIER_LENGTH (id));
  864. }
  865. /* Returns nonzero if an exception of type FROM will be caught by a
  866. handler for type TO, as per [except.handle]. */
  867. static int
  868. can_convert_eh (tree to, tree from)
  869. {
  870. to = non_reference (to);
  871. from = non_reference (from);
  872. if (TYPE_PTR_P (to) && TYPE_PTR_P (from))
  873. {
  874. to = TREE_TYPE (to);
  875. from = TREE_TYPE (from);
  876. if (! at_least_as_qualified_p (to, from))
  877. return 0;
  878. if (VOID_TYPE_P (to))
  879. return 1;
  880. /* Else fall through. */
  881. }
  882. if (CLASS_TYPE_P (to) && CLASS_TYPE_P (from)
  883. && publicly_uniquely_derived_p (to, from))
  884. return 1;
  885. return 0;
  886. }
  887. /* Check whether any of the handlers in I are shadowed by another handler
  888. accepting TYPE. Note that the shadowing may not be complete; even if
  889. an exception of type B would be caught by a handler for A, there could
  890. be a derived class C for which A is an ambiguous base but B is not, so
  891. the handler for B would catch an exception of type C. */
  892. static void
  893. check_handlers_1 (tree master, tree_stmt_iterator i)
  894. {
  895. tree type = TREE_TYPE (master);
  896. for (; !tsi_end_p (i); tsi_next (&i))
  897. {
  898. tree handler = tsi_stmt (i);
  899. if (TREE_TYPE (handler) && can_convert_eh (type, TREE_TYPE (handler)))
  900. {
  901. warning_at (EXPR_LOCATION (handler), 0,
  902. "exception of type %qT will be caught",
  903. TREE_TYPE (handler));
  904. warning_at (EXPR_LOCATION (master), 0,
  905. " by earlier handler for %qT", type);
  906. break;
  907. }
  908. }
  909. }
  910. /* Given a STATEMENT_LIST of HANDLERs, make sure that they're OK. */
  911. void
  912. check_handlers (tree handlers)
  913. {
  914. tree_stmt_iterator i;
  915. /* If we don't have a STATEMENT_LIST, then we've just got one
  916. handler, and thus nothing to warn about. */
  917. if (TREE_CODE (handlers) != STATEMENT_LIST)
  918. return;
  919. i = tsi_start (handlers);
  920. if (!tsi_end_p (i))
  921. while (1)
  922. {
  923. tree handler = tsi_stmt (i);
  924. tsi_next (&i);
  925. /* No more handlers; nothing to shadow. */
  926. if (tsi_end_p (i))
  927. break;
  928. if (TREE_TYPE (handler) == NULL_TREE)
  929. permerror (EXPR_LOCATION (handler), "%<...%>"
  930. " handler must be the last handler for its try block");
  931. else
  932. check_handlers_1 (handler, i);
  933. }
  934. }
  935. /* walk_tree helper for finish_noexcept_expr. Returns non-null if the
  936. expression *TP causes the noexcept operator to evaluate to false.
  937. 5.3.7 [expr.noexcept]: The result of the noexcept operator is false if
  938. in a potentially-evaluated context the expression would contain
  939. * a potentially evaluated call to a function, member function,
  940. function pointer, or member function pointer that does not have a
  941. non-throwing exception-specification (15.4),
  942. * a potentially evaluated throw-expression (15.1),
  943. * a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
  944. where T is a reference type, that requires a run-time check (5.2.7), or
  945. * a potentially evaluated typeid expression (5.2.8) applied to a glvalue
  946. expression whose type is a polymorphic class type (10.3). */
  947. static tree
  948. check_noexcept_r (tree *tp, int * /*walk_subtrees*/, void * /*data*/)
  949. {
  950. tree t = *tp;
  951. enum tree_code code = TREE_CODE (t);
  952. if ((code == CALL_EXPR && CALL_EXPR_FN (t))
  953. || code == AGGR_INIT_EXPR)
  954. {
  955. /* We can only use the exception specification of the called function
  956. for determining the value of a noexcept expression; we can't use
  957. TREE_NOTHROW, as it might have a different value in another
  958. translation unit, creating ODR problems.
  959. We could use TREE_NOTHROW (t) for !TREE_PUBLIC fns, though... */
  960. tree fn = (code == AGGR_INIT_EXPR
  961. ? AGGR_INIT_EXPR_FN (t) : CALL_EXPR_FN (t));
  962. tree type = TREE_TYPE (TREE_TYPE (fn));
  963. STRIP_NOPS (fn);
  964. if (TREE_CODE (fn) == ADDR_EXPR)
  965. fn = TREE_OPERAND (fn, 0);
  966. if (TREE_CODE (fn) == FUNCTION_DECL)
  967. {
  968. /* We do use TREE_NOTHROW for ABI internals like __dynamic_cast,
  969. and for C library functions known not to throw. */
  970. if (DECL_EXTERN_C_P (fn)
  971. && (DECL_ARTIFICIAL (fn)
  972. || nothrow_libfn_p (fn)))
  973. return TREE_NOTHROW (fn) ? NULL_TREE : fn;
  974. /* A call to a constexpr function is noexcept if the call
  975. is a constant expression. */
  976. if (DECL_DECLARED_CONSTEXPR_P (fn)
  977. && is_sub_constant_expr (t))
  978. return NULL_TREE;
  979. }
  980. if (!TYPE_NOTHROW_P (type))
  981. return fn;
  982. }
  983. return NULL_TREE;
  984. }
  985. /* If a function that causes a noexcept-expression to be false isn't
  986. defined yet, remember it and check it for TREE_NOTHROW again at EOF. */
  987. typedef struct GTY(()) pending_noexcept {
  988. tree fn;
  989. location_t loc;
  990. } pending_noexcept;
  991. static GTY(()) vec<pending_noexcept, va_gc> *pending_noexcept_checks;
  992. /* FN is a FUNCTION_DECL that caused a noexcept-expr to be false. Warn if
  993. it can't throw. */
  994. static void
  995. maybe_noexcept_warning (tree fn)
  996. {
  997. if (TREE_NOTHROW (fn))
  998. {
  999. warning (OPT_Wnoexcept, "noexcept-expression evaluates to %<false%> "
  1000. "because of a call to %qD", fn);
  1001. warning (OPT_Wnoexcept, "but %q+D does not throw; perhaps "
  1002. "it should be declared %<noexcept%>", fn);
  1003. }
  1004. }
  1005. /* Check any functions that weren't defined earlier when they caused a
  1006. noexcept expression to evaluate to false. */
  1007. void
  1008. perform_deferred_noexcept_checks (void)
  1009. {
  1010. int i;
  1011. pending_noexcept *p;
  1012. location_t saved_loc = input_location;
  1013. FOR_EACH_VEC_SAFE_ELT (pending_noexcept_checks, i, p)
  1014. {
  1015. input_location = p->loc;
  1016. maybe_noexcept_warning (p->fn);
  1017. }
  1018. input_location = saved_loc;
  1019. }
  1020. /* Evaluate noexcept ( EXPR ). */
  1021. tree
  1022. finish_noexcept_expr (tree expr, tsubst_flags_t complain)
  1023. {
  1024. if (expr == error_mark_node)
  1025. return error_mark_node;
  1026. if (processing_template_decl)
  1027. return build_min (NOEXCEPT_EXPR, boolean_type_node, expr);
  1028. return (expr_noexcept_p (expr, complain)
  1029. ? boolean_true_node : boolean_false_node);
  1030. }
  1031. /* Returns whether EXPR is noexcept, possibly warning if allowed by
  1032. COMPLAIN. */
  1033. bool
  1034. expr_noexcept_p (tree expr, tsubst_flags_t complain)
  1035. {
  1036. tree fn;
  1037. if (expr == error_mark_node)
  1038. return false;
  1039. fn = cp_walk_tree_without_duplicates (&expr, check_noexcept_r, 0);
  1040. if (fn)
  1041. {
  1042. if ((complain & tf_warning) && warn_noexcept
  1043. && TREE_CODE (fn) == FUNCTION_DECL)
  1044. {
  1045. if (!DECL_INITIAL (fn))
  1046. {
  1047. /* Not defined yet; check again at EOF. */
  1048. pending_noexcept p = {fn, input_location};
  1049. vec_safe_push (pending_noexcept_checks, p);
  1050. }
  1051. else
  1052. maybe_noexcept_warning (fn);
  1053. }
  1054. return false;
  1055. }
  1056. else
  1057. return true;
  1058. }
  1059. /* Return true iff SPEC is throw() or noexcept(true). */
  1060. bool
  1061. nothrow_spec_p (const_tree spec)
  1062. {
  1063. gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (spec));
  1064. if (spec == NULL_TREE
  1065. || TREE_VALUE (spec) != NULL_TREE
  1066. || spec == noexcept_false_spec)
  1067. return false;
  1068. if (TREE_PURPOSE (spec) == NULL_TREE
  1069. || spec == noexcept_true_spec)
  1070. return true;
  1071. gcc_assert (processing_template_decl
  1072. || TREE_PURPOSE (spec) == error_mark_node);
  1073. return false;
  1074. }
  1075. /* For FUNCTION_TYPE or METHOD_TYPE, true if NODE is noexcept. This is the
  1076. case for things declared noexcept(true) and, with -fnothrow-opt, for
  1077. throw() functions. */
  1078. bool
  1079. type_noexcept_p (const_tree type)
  1080. {
  1081. tree spec = TYPE_RAISES_EXCEPTIONS (type);
  1082. gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (spec));
  1083. if (flag_nothrow_opt)
  1084. return nothrow_spec_p (spec);
  1085. else
  1086. return spec == noexcept_true_spec;
  1087. }
  1088. /* For FUNCTION_TYPE or METHOD_TYPE, true if NODE can throw any type,
  1089. i.e. no exception-specification or noexcept(false). */
  1090. bool
  1091. type_throw_all_p (const_tree type)
  1092. {
  1093. tree spec = TYPE_RAISES_EXCEPTIONS (type);
  1094. gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (spec));
  1095. return spec == NULL_TREE || spec == noexcept_false_spec;
  1096. }
  1097. /* Create a representation of the noexcept-specification with
  1098. constant-expression of EXPR. COMPLAIN is as for tsubst. */
  1099. tree
  1100. build_noexcept_spec (tree expr, int complain)
  1101. {
  1102. /* This isn't part of the signature, so don't bother trying to evaluate
  1103. it until instantiation. */
  1104. if (!processing_template_decl && TREE_CODE (expr) != DEFERRED_NOEXCEPT)
  1105. {
  1106. expr = perform_implicit_conversion_flags (boolean_type_node, expr,
  1107. complain,
  1108. LOOKUP_NORMAL);
  1109. expr = cxx_constant_value (expr);
  1110. }
  1111. if (TREE_CODE (expr) == INTEGER_CST)
  1112. {
  1113. if (operand_equal_p (expr, boolean_true_node, 0))
  1114. return noexcept_true_spec;
  1115. else
  1116. {
  1117. gcc_checking_assert (operand_equal_p (expr, boolean_false_node, 0));
  1118. return noexcept_false_spec;
  1119. }
  1120. }
  1121. else if (expr == error_mark_node)
  1122. return error_mark_node;
  1123. else
  1124. {
  1125. gcc_assert (processing_template_decl
  1126. || TREE_CODE (expr) == DEFERRED_NOEXCEPT);
  1127. return build_tree_list (expr, NULL_TREE);
  1128. }
  1129. }
  1130. /* Returns a noexcept-specifier to be evaluated later, for an
  1131. implicitly-declared or explicitly defaulted special member function. */
  1132. tree
  1133. unevaluated_noexcept_spec (void)
  1134. {
  1135. static tree spec;
  1136. if (spec == NULL_TREE)
  1137. spec = build_noexcept_spec (make_node (DEFERRED_NOEXCEPT), tf_none);
  1138. return spec;
  1139. }
  1140. /* Returns a TRY_CATCH_EXPR that will put TRY_LIST and CATCH_LIST in the
  1141. TRY and CATCH locations. CATCH_LIST must be a STATEMENT_LIST */
  1142. tree
  1143. create_try_catch_expr (tree try_expr, tree catch_list)
  1144. {
  1145. location_t loc = EXPR_LOCATION (try_expr);
  1146. append_to_statement_list (do_begin_catch (), &catch_list);
  1147. append_to_statement_list (build_throw (NULL_TREE), &catch_list);
  1148. tree catch_tf_expr = build_stmt (loc, TRY_FINALLY_EXPR, catch_list,
  1149. do_end_catch (NULL_TREE));
  1150. catch_list = build2 (CATCH_EXPR, void_type_node, NULL_TREE,
  1151. catch_tf_expr);
  1152. tree try_catch_expr = build_stmt (loc, TRY_CATCH_EXPR, try_expr, catch_list);
  1153. return try_catch_expr;
  1154. }
  1155. #include "gt-cp-except.h"