annotate gcc/cp/except.c @ 111:04ced10e8804

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