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