]>
Commit | Line | Data |
---|---|---|
3fd5abcf | 1 | /* Process declarations and variables for C++ compiler. |
cbe34bb5 | 2 | Copyright (C) 1988-2017 Free Software Foundation, Inc. |
8d08fdba MS |
3 | Hacked by Michael Tiemann (tiemann@cygnus.com) |
4 | ||
1c313945 | 5 | This file is part of GCC. |
8d08fdba | 6 | |
1c313945 | 7 | GCC is free software; you can redistribute it and/or modify |
8d08fdba | 8 | it under the terms of the GNU General Public License as published by |
e77f031d | 9 | the Free Software Foundation; either version 3, or (at your option) |
8d08fdba MS |
10 | any later version. |
11 | ||
1c313945 | 12 | GCC is distributed in the hope that it will be useful, |
8d08fdba MS |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
e77f031d NC |
18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
8d08fdba MS |
20 | |
21 | ||
1c313945 | 22 | /* Process declarations and symbol lookup for C++ front end. |
8d08fdba MS |
23 | Also constructs types; the standard scalar types at initialization, |
24 | and structure, union, array and enum types when they are declared. */ | |
25 | ||
26 | /* ??? not all decl nodes are given the most useful possible | |
27 | line numbers. For example, the CONST_DECLs for enum values. */ | |
28 | ||
29 | #include "config.h" | |
8d052bc7 | 30 | #include "system.h" |
4977bab6 | 31 | #include "coretypes.h" |
4d0cdd0c | 32 | #include "memmodel.h" |
2adfab87 | 33 | #include "target.h" |
2adfab87 AM |
34 | #include "cp-tree.h" |
35 | #include "c-family/c-common.h" | |
36 | #include "timevar.h" | |
d8a2d370 | 37 | #include "stringpool.h" |
2adfab87 | 38 | #include "cgraph.h" |
d8a2d370 DN |
39 | #include "varasm.h" |
40 | #include "attribs.h" | |
41 | #include "stor-layout.h" | |
42 | #include "calls.h" | |
8d08fdba | 43 | #include "decl.h" |
54f92bfb | 44 | #include "toplev.h" |
61d3ce20 | 45 | #include "c-family/c-objc.h" |
39dabefd | 46 | #include "c-family/c-pragma.h" |
7ee2468b | 47 | #include "dumpfile.h" |
dee15844 | 48 | #include "intl.h" |
39dabefd | 49 | #include "c-family/c-ada-spec.h" |
59b36ecf | 50 | #include "asan.h" |
6de9cd9a | 51 | |
58aca9d9 NS |
52 | /* Id for dumping the raw trees. */ |
53 | int raw_dump_id; | |
54 | ||
cf44ea52 | 55 | extern cpp_reader *parse_in; |
297441fd | 56 | |
0aafb128 MM |
57 | /* This structure contains information about the initializations |
58 | and/or destructions required for a particular priority level. */ | |
59 | typedef struct priority_info_s { | |
838dfd8a | 60 | /* Nonzero if there have been any initializations at this priority |
0352cfc8 MM |
61 | throughout the translation unit. */ |
62 | int initializations_p; | |
838dfd8a | 63 | /* Nonzero if there have been any destructions at this priority |
0352cfc8 MM |
64 | throughout the translation unit. */ |
65 | int destructions_p; | |
0aafb128 MM |
66 | } *priority_info; |
67 | ||
848eed92 | 68 | static void mark_vtable_entries (tree); |
848eed92 | 69 | static bool maybe_emit_vtables (tree); |
848eed92 GDR |
70 | static tree start_objects (int, int); |
71 | static void finish_objects (int, int, tree); | |
299f79b5 | 72 | static tree start_static_storage_duration_function (unsigned); |
848eed92 GDR |
73 | static void finish_static_storage_duration_function (tree); |
74 | static priority_info get_priority_info (int); | |
6cec5cb5 RG |
75 | static void do_static_initialization_or_destruction (tree, bool); |
76 | static void one_static_initialization_or_destruction (tree, tree, bool); | |
299f79b5 | 77 | static void generate_ctor_or_dtor_function (bool, int, location_t *); |
848eed92 | 78 | static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node, |
0cbd7506 | 79 | void *); |
cec24319 | 80 | static tree prune_vars_needing_no_initialization (tree *); |
848eed92 GDR |
81 | static void write_out_vars (tree); |
82 | static void import_export_class (tree); | |
848eed92 | 83 | static tree get_guard_bits (tree); |
0ed5edac | 84 | static void determine_visibility_from_class (tree, tree); |
fb9120e3 | 85 | static bool determine_hidden_inline (tree); |
94ced5b4 | 86 | static void maybe_instantiate_decl (tree); |
313bc2c2 | 87 | |
8d08fdba MS |
88 | /* A list of static class variables. This is needed, because a |
89 | static class variable can be declared inside the class without | |
306ef644 | 90 | an initializer, and then initialized, statically, outside the class. */ |
9771b263 | 91 | static GTY(()) vec<tree, va_gc> *pending_statics; |
8d08fdba | 92 | |
8926095f | 93 | /* A list of functions which were declared inline, but which we |
e92cc029 | 94 | may need to emit outline anyway. */ |
9771b263 | 95 | static GTY(()) vec<tree, va_gc> *deferred_fns; |
8d08fdba | 96 | |
e8f43da6 JM |
97 | /* A list of decls that use types with no linkage, which we need to make |
98 | sure are defined. */ | |
9771b263 | 99 | static GTY(()) vec<tree, va_gc> *no_linkage_decls; |
e8f43da6 | 100 | |
613aba20 JM |
101 | /* A vector of alternating decls and identifiers, where the latter |
102 | is to be an alias for the former if the former is defined. */ | |
103 | static GTY(()) vec<tree, va_gc> *mangling_aliases; | |
104 | ||
5566b478 MS |
105 | /* Nonzero if we're done parsing and into end-of-file activities. */ |
106 | ||
107 | int at_eof; | |
ec5a0fe0 JJ |
108 | |
109 | /* True if note_mangling_alias should enqueue mangling aliases for | |
110 | later generation, rather than emitting them right away. */ | |
111 | ||
112 | bool defer_mangling_aliases = true; | |
8d08fdba | 113 | \f |
e2537f2c MM |
114 | |
115 | /* Return a member function type (a METHOD_TYPE), given FNTYPE (a | |
116 | FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers | |
117 | that apply to the function). */ | |
118 | ||
119 | tree | |
2eed8e37 BK |
120 | build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals, |
121 | cp_ref_qualifier rqual) | |
8d08fdba | 122 | { |
e2537f2c | 123 | tree raises; |
3c3905fc | 124 | tree attrs; |
e2537f2c | 125 | int type_quals; |
cab421f4 | 126 | bool late_return_type_p; |
8d08fdba | 127 | |
15218346 VR |
128 | if (fntype == error_mark_node || ctype == error_mark_node) |
129 | return error_mark_node; | |
130 | ||
3c3905fc JM |
131 | gcc_assert (TREE_CODE (fntype) == FUNCTION_TYPE |
132 | || TREE_CODE (fntype) == METHOD_TYPE); | |
133 | ||
3c01e5df | 134 | type_quals = quals & ~TYPE_QUAL_RESTRICT; |
91063b51 | 135 | ctype = cp_build_qualified_type (ctype, type_quals); |
3c3905fc JM |
136 | raises = TYPE_RAISES_EXCEPTIONS (fntype); |
137 | attrs = TYPE_ATTRIBUTES (fntype); | |
cab421f4 | 138 | late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype); |
43dc123f MM |
139 | fntype = build_method_type_directly (ctype, TREE_TYPE (fntype), |
140 | (TREE_CODE (fntype) == METHOD_TYPE | |
141 | ? TREE_CHAIN (TYPE_ARG_TYPES (fntype)) | |
142 | : TYPE_ARG_TYPES (fntype))); | |
3c3905fc JM |
143 | if (attrs) |
144 | fntype = cp_build_type_attribute_variant (fntype, attrs); | |
2eed8e37 BK |
145 | if (rqual) |
146 | fntype = build_ref_qualified_type (fntype, rqual); | |
147 | if (raises) | |
148 | fntype = build_exception_variant (fntype, raises); | |
cab421f4 PC |
149 | if (late_return_type_p) |
150 | TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1; | |
8d08fdba | 151 | |
e2537f2c | 152 | return fntype; |
8d08fdba MS |
153 | } |
154 | ||
643d4cd6 JM |
155 | /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its |
156 | return type changed to NEW_RET. */ | |
157 | ||
158 | tree | |
159 | change_return_type (tree new_ret, tree fntype) | |
160 | { | |
161 | tree newtype; | |
162 | tree args = TYPE_ARG_TYPES (fntype); | |
163 | tree raises = TYPE_RAISES_EXCEPTIONS (fntype); | |
164 | tree attrs = TYPE_ATTRIBUTES (fntype); | |
cab421f4 | 165 | bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (fntype); |
643d4cd6 | 166 | |
852497a3 JM |
167 | if (new_ret == error_mark_node) |
168 | return fntype; | |
169 | ||
643d4cd6 JM |
170 | if (same_type_p (new_ret, TREE_TYPE (fntype))) |
171 | return fntype; | |
172 | ||
173 | if (TREE_CODE (fntype) == FUNCTION_TYPE) | |
2872152c JM |
174 | { |
175 | newtype = build_function_type (new_ret, args); | |
2eed8e37 BK |
176 | newtype = apply_memfn_quals (newtype, |
177 | type_memfn_quals (fntype), | |
178 | type_memfn_rqual (fntype)); | |
2872152c | 179 | } |
643d4cd6 | 180 | else |
c6be04ad | 181 | newtype = build_method_type_directly |
7e1352fe | 182 | (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args)); |
9ab2fcc1 NS |
183 | if (FUNCTION_REF_QUALIFIED (fntype)) |
184 | newtype = build_ref_qualified_type (newtype, type_memfn_rqual (fntype)); | |
643d4cd6 JM |
185 | if (raises) |
186 | newtype = build_exception_variant (newtype, raises); | |
187 | if (attrs) | |
188 | newtype = cp_build_type_attribute_variant (newtype, attrs); | |
cab421f4 PC |
189 | if (late_return_type_p) |
190 | TYPE_HAS_LATE_RETURN_TYPE (newtype) = 1; | |
643d4cd6 JM |
191 | |
192 | return newtype; | |
193 | } | |
194 | ||
8e51619a JM |
195 | /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE |
196 | appropriately. */ | |
197 | ||
198 | tree | |
848eed92 | 199 | cp_build_parm_decl (tree name, tree type) |
8e51619a | 200 | { |
c2255bc4 AH |
201 | tree parm = build_decl (input_location, |
202 | PARM_DECL, name, type); | |
560ad596 MM |
203 | /* DECL_ARG_TYPE is only used by the back end and the back end never |
204 | sees templates. */ | |
205 | if (!processing_template_decl) | |
206 | DECL_ARG_TYPE (parm) = type_passed_as (type); | |
5d80a306 | 207 | |
8e51619a JM |
208 | return parm; |
209 | } | |
210 | ||
3ec6bad3 MM |
211 | /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the |
212 | indicated NAME. */ | |
213 | ||
e2537f2c | 214 | tree |
848eed92 | 215 | build_artificial_parm (tree name, tree type) |
3ec6bad3 | 216 | { |
8e51619a | 217 | tree parm = cp_build_parm_decl (name, type); |
c727aa5e | 218 | DECL_ARTIFICIAL (parm) = 1; |
a714e5c5 JM |
219 | /* All our artificial parms are implicitly `const'; they cannot be |
220 | assigned to. */ | |
221 | TREE_READONLY (parm) = 1; | |
3ec6bad3 MM |
222 | return parm; |
223 | } | |
224 | ||
711734a9 JM |
225 | /* Constructors for types with virtual baseclasses need an "in-charge" flag |
226 | saying whether this constructor is responsible for initialization of | |
227 | virtual baseclasses or not. All destructors also need this "in-charge" | |
228 | flag, which additionally determines whether or not the destructor should | |
229 | free the memory for the object. | |
230 | ||
231 | This function adds the "in-charge" flag to member function FN if | |
232 | appropriate. It is called from grokclassfn and tsubst. | |
e0fff4b3 JM |
233 | FN must be either a constructor or destructor. |
234 | ||
235 | The in-charge flag follows the 'this' parameter, and is followed by the | |
236 | VTT parm (if any), then the user-written parms. */ | |
711734a9 JM |
237 | |
238 | void | |
848eed92 | 239 | maybe_retrofit_in_chrg (tree fn) |
711734a9 JM |
240 | { |
241 | tree basetype, arg_types, parms, parm, fntype; | |
242 | ||
454fa7a7 MM |
243 | /* If we've already add the in-charge parameter don't do it again. */ |
244 | if (DECL_HAS_IN_CHARGE_PARM_P (fn)) | |
711734a9 JM |
245 | return; |
246 | ||
212e7048 MM |
247 | /* When processing templates we can't know, in general, whether or |
248 | not we're going to have virtual baseclasses. */ | |
c353b8e3 | 249 | if (processing_template_decl) |
212e7048 MM |
250 | return; |
251 | ||
454fa7a7 MM |
252 | /* We don't need an in-charge parameter for constructors that don't |
253 | have virtual bases. */ | |
254 | if (DECL_CONSTRUCTOR_P (fn) | |
5775a06a | 255 | && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn))) |
454fa7a7 | 256 | return; |
711734a9 | 257 | |
711734a9 JM |
258 | arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn)); |
259 | basetype = TREE_TYPE (TREE_VALUE (arg_types)); | |
e0fff4b3 JM |
260 | arg_types = TREE_CHAIN (arg_types); |
261 | ||
910ad8de | 262 | parms = DECL_CHAIN (DECL_ARGUMENTS (fn)); |
e0fff4b3 JM |
263 | |
264 | /* If this is a subobject constructor or destructor, our caller will | |
265 | pass us a pointer to our VTT. */ | |
5775a06a | 266 | if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn))) |
e0fff4b3 JM |
267 | { |
268 | parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type); | |
269 | ||
270 | /* First add it to DECL_ARGUMENTS between 'this' and the real args... */ | |
910ad8de | 271 | DECL_CHAIN (parm) = parms; |
e0fff4b3 JM |
272 | parms = parm; |
273 | ||
274 | /* ...and then to TYPE_ARG_TYPES. */ | |
275 | arg_types = hash_tree_chain (vtt_parm_type, arg_types); | |
276 | ||
277 | DECL_HAS_VTT_PARM_P (fn) = 1; | |
278 | } | |
279 | ||
280 | /* Then add the in-charge parm (before the VTT parm). */ | |
281 | parm = build_artificial_parm (in_charge_identifier, integer_type_node); | |
910ad8de | 282 | DECL_CHAIN (parm) = parms; |
e0fff4b3 JM |
283 | parms = parm; |
284 | arg_types = hash_tree_chain (integer_type_node, arg_types); | |
285 | ||
286 | /* Insert our new parameter(s) into the list. */ | |
910ad8de | 287 | DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms; |
e0fff4b3 JM |
288 | |
289 | /* And rebuild the function type. */ | |
43dc123f MM |
290 | fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)), |
291 | arg_types); | |
711734a9 JM |
292 | if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn))) |
293 | fntype = build_exception_variant (fntype, | |
294 | TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn))); | |
3c3905fc JM |
295 | if (TYPE_ATTRIBUTES (TREE_TYPE (fn))) |
296 | fntype = (cp_build_type_attribute_variant | |
297 | (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn)))); | |
711734a9 | 298 | TREE_TYPE (fn) = fntype; |
454fa7a7 MM |
299 | |
300 | /* Now we've got the in-charge parameter. */ | |
301 | DECL_HAS_IN_CHARGE_PARM_P (fn) = 1; | |
711734a9 JM |
302 | } |
303 | ||
8d08fdba MS |
304 | /* Classes overload their constituent function names automatically. |
305 | When a function name is declared in a record structure, | |
306 | its name is changed to it overloaded name. Since names for | |
307 | constructors and destructors can conflict, we place a leading | |
308 | '$' for destructors. | |
309 | ||
310 | CNAME is the name of the class we are grokking for. | |
311 | ||
312 | FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'. | |
313 | ||
314 | FLAGS contains bits saying what's special about today's | |
ee18fe39 | 315 | arguments. DTOR_FLAG == DESTRUCTOR. |
8d08fdba MS |
316 | |
317 | If FUNCTION is a destructor, then we must add the `auto-delete' field | |
318 | as a second parameter. There is some hair associated with the fact | |
319 | that we must "declare" this variable in the manner consistent with the | |
320 | way the rest of the arguments were declared. | |
321 | ||
322 | QUALS are the qualifiers for the this pointer. */ | |
323 | ||
324 | void | |
e2537f2c | 325 | grokclassfn (tree ctype, tree function, enum overload_flags flags) |
8d08fdba MS |
326 | { |
327 | tree fn_name = DECL_NAME (function); | |
8d08fdba | 328 | |
cab8bde9 MM |
329 | /* Even within an `extern "C"' block, members get C++ linkage. See |
330 | [dcl.link] for details. */ | |
5d2ed28c | 331 | SET_DECL_LANGUAGE (function, lang_cplusplus); |
cab8bde9 | 332 | |
8d08fdba MS |
333 | if (fn_name == NULL_TREE) |
334 | { | |
8251199e | 335 | error ("name missing for member function"); |
8d08fdba MS |
336 | fn_name = get_identifier ("<anonymous>"); |
337 | DECL_NAME (function) = fn_name; | |
338 | } | |
339 | ||
711734a9 | 340 | DECL_CONTEXT (function) = ctype; |
711734a9 | 341 | |
92643fea MM |
342 | if (flags == DTOR_FLAG) |
343 | DECL_DESTRUCTOR_P (function) = 1; | |
344 | ||
711734a9 | 345 | if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function)) |
535233a8 | 346 | maybe_retrofit_in_chrg (function); |
8d08fdba MS |
347 | } |
348 | ||
8d08fdba | 349 | /* Create an ARRAY_REF, checking for the user doing things backwards |
e59baf05 | 350 | along the way. DECLTYPE_P is for N3276, as in the parser. */ |
e92cc029 | 351 | |
8d08fdba | 352 | tree |
e59baf05 JM |
353 | grok_array_decl (location_t loc, tree array_expr, tree index_exp, |
354 | bool decltype_p) | |
8d08fdba | 355 | { |
d17811fd MM |
356 | tree type; |
357 | tree expr; | |
358 | tree orig_array_expr = array_expr; | |
359 | tree orig_index_exp = index_exp; | |
fcb9363e | 360 | tree overload = NULL_TREE; |
8d08fdba | 361 | |
d17811fd | 362 | if (error_operand_p (array_expr) || error_operand_p (index_exp)) |
8d08fdba | 363 | return error_mark_node; |
d17811fd | 364 | |
5156628f | 365 | if (processing_template_decl) |
d17811fd MM |
366 | { |
367 | if (type_dependent_expression_p (array_expr) | |
368 | || type_dependent_expression_p (index_exp)) | |
f330f599 PC |
369 | return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp, |
370 | NULL_TREE, NULL_TREE); | |
d17811fd MM |
371 | array_expr = build_non_dependent_expr (array_expr); |
372 | index_exp = build_non_dependent_expr (index_exp); | |
373 | } | |
5566b478 | 374 | |
d17811fd | 375 | type = TREE_TYPE (array_expr); |
50bc768d | 376 | gcc_assert (type); |
ee76b931 | 377 | type = non_reference (type); |
8d08fdba MS |
378 | |
379 | /* If they have an `operator[]', use that. */ | |
9e1e64ec | 380 | if (MAYBE_CLASS_TYPE_P (type) || MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp))) |
e59baf05 JM |
381 | { |
382 | tsubst_flags_t complain = tf_warning_or_error; | |
383 | if (decltype_p) | |
384 | complain |= tf_decltype; | |
385 | expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr, | |
fcb9363e | 386 | index_exp, NULL_TREE, &overload, complain); |
e59baf05 | 387 | } |
b7484fbe | 388 | else |
d17811fd MM |
389 | { |
390 | tree p1, p2, i1, i2; | |
8d08fdba | 391 | |
d17811fd MM |
392 | /* Otherwise, create an ARRAY_REF for a pointer or array type. |
393 | It is a little-known fact that, if `a' is an array and `i' is | |
394 | an int, you can write `i[a]', which means the same thing as | |
395 | `a[i]'. */ | |
b55b02ea | 396 | if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type)) |
d17811fd MM |
397 | p1 = array_expr; |
398 | else | |
399 | p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false); | |
400 | ||
401 | if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE) | |
402 | p2 = index_exp; | |
403 | else | |
404 | p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false); | |
8d08fdba | 405 | |
c8094d83 | 406 | i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr, |
d17811fd | 407 | false); |
c8094d83 | 408 | i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp, |
d17811fd | 409 | false); |
8d08fdba | 410 | |
d17811fd MM |
411 | if ((p1 && i2) && (i1 && p2)) |
412 | error ("ambiguous conversion for array subscript"); | |
8d08fdba | 413 | |
d17811fd MM |
414 | if (p1 && i2) |
415 | array_expr = p1, index_exp = i2; | |
416 | else if (i1 && p2) | |
417 | array_expr = p2, index_exp = i1; | |
418 | else | |
419 | { | |
2d01edd7 | 420 | error ("invalid types %<%T[%T]%> for array subscript", |
0cbd7506 | 421 | type, TREE_TYPE (index_exp)); |
d17811fd MM |
422 | return error_mark_node; |
423 | } | |
b7484fbe | 424 | |
d17811fd MM |
425 | if (array_expr == error_mark_node || index_exp == error_mark_node) |
426 | error ("ambiguous conversion for array subscript"); | |
b7484fbe | 427 | |
3a11c665 | 428 | expr = build_array_ref (input_location, array_expr, index_exp); |
d17811fd MM |
429 | } |
430 | if (processing_template_decl && expr != error_mark_node) | |
fcb9363e PP |
431 | { |
432 | if (overload != NULL_TREE) | |
433 | return (build_min_non_dep_op_overload | |
434 | (ARRAY_REF, expr, overload, orig_array_expr, orig_index_exp)); | |
435 | ||
436 | return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, orig_index_exp, | |
437 | NULL_TREE, NULL_TREE); | |
438 | } | |
d17811fd | 439 | return expr; |
8d08fdba MS |
440 | } |
441 | ||
442 | /* Given the cast expression EXP, checking out its validity. Either return | |
443 | an error_mark_node if there was an unavoidable error, return a cast to | |
444 | void for trying to delete a pointer w/ the value 0, or return the | |
0e8c9b28 MM |
445 | call to delete. If DOING_VEC is true, we handle things differently |
446 | for doing an array delete. | |
8d08fdba | 447 | Implements ARM $5.3.4. This is called from the parser. */ |
e92cc029 | 448 | |
8d08fdba | 449 | tree |
574cfaa4 JM |
450 | delete_sanity (tree exp, tree size, bool doing_vec, int use_global_delete, |
451 | tsubst_flags_t complain) | |
8d08fdba | 452 | { |
02020185 | 453 | tree t, type; |
5566b478 | 454 | |
909e536a MS |
455 | if (exp == error_mark_node) |
456 | return exp; | |
457 | ||
5156628f | 458 | if (processing_template_decl) |
5566b478 MS |
459 | { |
460 | t = build_min (DELETE_EXPR, void_type_node, exp, size); | |
461 | DELETE_EXPR_USE_GLOBAL (t) = use_global_delete; | |
462 | DELETE_EXPR_USE_VEC (t) = doing_vec; | |
c1cca8d4 | 463 | TREE_SIDE_EFFECTS (t) = 1; |
5566b478 MS |
464 | return t; |
465 | } | |
466 | ||
0e8c9b28 | 467 | /* An array can't have been allocated by new, so complain. */ |
0a9696f0 PC |
468 | if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE) |
469 | warning (0, "deleting array %q#E", exp); | |
0e8c9b28 | 470 | |
49b7aacb | 471 | t = build_expr_type_conversion (WANT_POINTER, exp, true); |
8d08fdba | 472 | |
02020185 | 473 | if (t == NULL_TREE || t == error_mark_node) |
8d08fdba | 474 | { |
2d01edd7 | 475 | error ("type %q#T argument given to %<delete%>, expected pointer", |
0cbd7506 | 476 | TREE_TYPE (exp)); |
02020185 | 477 | return error_mark_node; |
8d08fdba MS |
478 | } |
479 | ||
02020185 JM |
480 | type = TREE_TYPE (t); |
481 | ||
482 | /* As of Valley Forge, you can delete a pointer to const. */ | |
483 | ||
484 | /* You can't delete functions. */ | |
485 | if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE) | |
8926095f | 486 | { |
2d01edd7 | 487 | error ("cannot delete a function. Only pointer-to-objects are " |
0cbd7506 | 488 | "valid arguments to %<delete%>"); |
02020185 | 489 | return error_mark_node; |
8926095f | 490 | } |
02020185 | 491 | |
838dfd8a | 492 | /* Deleting ptr to void is undefined behavior [expr.delete/3]. */ |
50e10fa8 | 493 | if (VOID_TYPE_P (TREE_TYPE (type))) |
655dc6ee | 494 | { |
53b0f747 | 495 | warning (OPT_Wdelete_incomplete, "deleting %qT is undefined", type); |
655dc6ee JM |
496 | doing_vec = 0; |
497 | } | |
498 | ||
02020185 JM |
499 | /* Deleting a pointer with the value zero is valid and has no effect. */ |
500 | if (integer_zerop (t)) | |
501 | return build1 (NOP_EXPR, void_type_node, t); | |
8d08fdba MS |
502 | |
503 | if (doing_vec) | |
c8094d83 | 504 | return build_vec_delete (t, /*maxindex=*/NULL_TREE, |
0e8c9b28 | 505 | sfk_deleting_destructor, |
574cfaa4 | 506 | use_global_delete, complain); |
8d08fdba | 507 | else |
e3fe84e5 | 508 | return build_delete (type, t, sfk_deleting_destructor, |
574cfaa4 JM |
509 | LOOKUP_NORMAL, use_global_delete, |
510 | complain); | |
8d08fdba MS |
511 | } |
512 | ||
98c1c668 JM |
513 | /* Report an error if the indicated template declaration is not the |
514 | sort of thing that should be a member template. */ | |
515 | ||
516 | void | |
848eed92 | 517 | check_member_template (tree tmpl) |
98c1c668 JM |
518 | { |
519 | tree decl; | |
520 | ||
50bc768d | 521 | gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); |
98c1c668 JM |
522 | decl = DECL_TEMPLATE_RESULT (tmpl); |
523 | ||
1701f21e | 524 | if (TREE_CODE (decl) == FUNCTION_DECL |
acfdadb4 | 525 | || DECL_ALIAS_TEMPLATE_P (tmpl) |
1701f21e | 526 | || (TREE_CODE (decl) == TYPE_DECL |
9e1e64ec | 527 | && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))) |
98c1c668 | 528 | { |
6a8b219b AB |
529 | /* The parser rejects template declarations in local classes |
530 | (with the exception of generic lambdas). */ | |
531 | gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl)); | |
ceacde63 MM |
532 | /* The parser rejects any use of virtual in a function template. */ |
533 | gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL | |
534 | && DECL_VIRTUAL_P (decl))); | |
98c1c668 JM |
535 | |
536 | /* The debug-information generating code doesn't know what to do | |
c8094d83 | 537 | with member templates. */ |
98c1c668 | 538 | DECL_IGNORED_P (tmpl) = 1; |
c8094d83 | 539 | } |
4a4f287d BO |
540 | else if (variable_template_p (tmpl)) |
541 | /* OK */; | |
98c1c668 | 542 | else |
2d01edd7 | 543 | error ("template declaration of %q#D", decl); |
98c1c668 JM |
544 | } |
545 | ||
8d08fdba MS |
546 | /* Sanity check: report error if this function FUNCTION is not |
547 | really a member of the class (CTYPE) it is supposed to belong to. | |
77880ae4 | 548 | TEMPLATE_PARMS is used to specify the template parameters of a member |
c8094d83 | 549 | template passed as FUNCTION_DECL. If the member template is passed as a |
44021471 GB |
550 | TEMPLATE_DECL, it can be NULL since the parameters can be extracted |
551 | from the declaration. If the function is not a function template, it | |
552 | must be NULL. | |
f8c3b097 PC |
553 | It returns the original declaration for the function, NULL_TREE if |
554 | no declaration was found, error_mark_node if an error was emitted. */ | |
8d08fdba | 555 | |
f30432d7 | 556 | tree |
44021471 | 557 | check_classfn (tree ctype, tree function, tree template_parms) |
8d08fdba | 558 | { |
b9201622 | 559 | int ix; |
44021471 | 560 | bool is_template; |
7d3bec9d NS |
561 | tree pushed_scope; |
562 | ||
03017874 | 563 | if (DECL_USE_TEMPLATE (function) |
74b846e0 MM |
564 | && !(TREE_CODE (function) == TEMPLATE_DECL |
565 | && DECL_TEMPLATE_SPECIALIZATION (function)) | |
c7222c02 | 566 | && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function))) |
03017874 MM |
567 | /* Since this is a specialization of a member template, |
568 | we're not going to find the declaration in the class. | |
569 | For example, in: | |
c8094d83 | 570 | |
0cbd7506 MS |
571 | struct S { template <typename T> void f(T); }; |
572 | template <> void S::f(int); | |
c8094d83 | 573 | |
03017874 MM |
574 | we're not going to find `S::f(int)', but there's no |
575 | reason we should, either. We let our callers know we didn't | |
576 | find the method, but we don't complain. */ | |
577 | return NULL_TREE; | |
b9201622 | 578 | |
44021471 GB |
579 | /* Basic sanity check: for a template function, the template parameters |
580 | either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */ | |
581 | if (TREE_CODE (function) == TEMPLATE_DECL) | |
582 | { | |
1dbc1ae7 JM |
583 | if (template_parms |
584 | && !comp_template_parms (template_parms, | |
585 | DECL_TEMPLATE_PARMS (function))) | |
586 | { | |
d8a07487 | 587 | error ("template parameter lists provided don%'t match the " |
1dbc1ae7 JM |
588 | "template parameters of %qD", function); |
589 | return error_mark_node; | |
590 | } | |
44021471 GB |
591 | template_parms = DECL_TEMPLATE_PARMS (function); |
592 | } | |
593 | ||
5b8e011c | 594 | /* OK, is this a definition of a member template? */ |
44021471 | 595 | is_template = (template_parms != NULL_TREE); |
5b8e011c | 596 | |
a544998e PC |
597 | /* [temp.mem] |
598 | ||
599 | A destructor shall not be a member template. */ | |
600 | if (DECL_DESTRUCTOR_P (function) && is_template) | |
601 | { | |
602 | error ("destructor %qD declared as member template", function); | |
603 | return error_mark_node; | |
604 | } | |
605 | ||
7d3bec9d NS |
606 | /* We must enter the scope here, because conversion operators are |
607 | named by target type, and type equivalence relies on typenames | |
608 | resolving within the scope of CTYPE. */ | |
609 | pushed_scope = push_scope (ctype); | |
c7222c02 | 610 | ix = class_method_index_for_fn (complete_type (ctype), function); |
b9201622 | 611 | if (ix >= 0) |
8d08fdba | 612 | { |
9771b263 | 613 | vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (ctype); |
c8094d83 | 614 | |
a736411a | 615 | for (ovl_iterator iter ((*methods)[ix]); iter; ++iter) |
b9201622 | 616 | { |
a736411a NS |
617 | tree fndecl = *iter; |
618 | tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function)); | |
619 | tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl)); | |
b9201622 NS |
620 | |
621 | /* We cannot simply call decls_match because this doesn't | |
622 | work for static member functions that are pretending to | |
623 | be methods, and because the name may have been changed by | |
c8094d83 MS |
624 | asm("new_name"). */ |
625 | ||
b9201622 NS |
626 | /* Get rid of the this parameter on functions that become |
627 | static. */ | |
628 | if (DECL_STATIC_FUNCTION_P (fndecl) | |
629 | && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE) | |
630 | p1 = TREE_CHAIN (p1); | |
5b8e011c JM |
631 | |
632 | /* A member template definition only matches a member template | |
633 | declaration. */ | |
634 | if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL)) | |
635 | continue; | |
c8094d83 | 636 | |
2eed8e37 BK |
637 | /* ref-qualifier or absence of same must match. */ |
638 | if (type_memfn_rqual (TREE_TYPE (function)) | |
639 | != type_memfn_rqual (TREE_TYPE (fndecl))) | |
640 | continue; | |
641 | ||
971e17ff AS |
642 | // Include constraints in the match. |
643 | tree c1 = get_constraints (function); | |
644 | tree c2 = get_constraints (fndecl); | |
645 | ||
3649b9b7 ST |
646 | /* While finding a match, same types and params are not enough |
647 | if the function is versioned. Also check version ("target") | |
648 | attributes. */ | |
b9201622 NS |
649 | if (same_type_p (TREE_TYPE (TREE_TYPE (function)), |
650 | TREE_TYPE (TREE_TYPE (fndecl))) | |
651 | && compparms (p1, p2) | |
3649b9b7 | 652 | && !targetm.target_option.function_versions (function, fndecl) |
44021471 | 653 | && (!is_template |
c8094d83 | 654 | || comp_template_parms (template_parms, |
44021471 | 655 | DECL_TEMPLATE_PARMS (fndecl))) |
971e17ff | 656 | && equivalent_constraints (c1, c2) |
b9201622 NS |
657 | && (DECL_TEMPLATE_SPECIALIZATION (function) |
658 | == DECL_TEMPLATE_SPECIALIZATION (fndecl)) | |
659 | && (!DECL_TEMPLATE_SPECIALIZATION (function) | |
c8094d83 | 660 | || (DECL_TI_TEMPLATE (function) |
b9201622 | 661 | == DECL_TI_TEMPLATE (fndecl)))) |
a736411a NS |
662 | { |
663 | if (pushed_scope) | |
664 | pop_scope (pushed_scope); | |
665 | return fndecl; | |
666 | } | |
7d3bec9d | 667 | } |
a736411a | 668 | |
402b8cf6 AH |
669 | error_at (DECL_SOURCE_LOCATION (function), |
670 | "prototype for %q#D does not match any in class %qT", | |
671 | function, ctype); | |
a736411a NS |
672 | |
673 | const char *format = NULL; | |
674 | tree first = OVL_FIRST ((*methods)[ix]); | |
675 | bool is_conv_op = DECL_CONV_FN_P (first); | |
676 | tree prev = NULL_TREE; | |
b9201622 NS |
677 | |
678 | if (is_conv_op) | |
679 | ix = CLASSTYPE_FIRST_CONVERSION_SLOT; | |
a736411a | 680 | do |
8d08fdba | 681 | { |
a736411a NS |
682 | ovl_iterator iter ((*methods)[ix++]); |
683 | if (is_conv_op && !DECL_CONV_FN_P (*iter)) | |
684 | break; | |
685 | for (; iter; ++iter) | |
8d08fdba | 686 | { |
a736411a | 687 | if (prev) |
8d08fdba | 688 | { |
a736411a NS |
689 | if (!format) |
690 | format = N_("candidates are: %+#D"); | |
691 | error (format, prev); | |
692 | format = " %+#D"; | |
8d08fdba | 693 | } |
a736411a | 694 | prev = *iter; |
8d08fdba | 695 | } |
a736411a NS |
696 | } |
697 | while (is_conv_op && size_t (ix) < methods->length ()); | |
698 | if (prev) | |
699 | { | |
700 | if (!format) | |
dee15844 | 701 | format = N_("candidate is: %+#D"); |
a736411a | 702 | error (format, prev); |
8d08fdba MS |
703 | } |
704 | } | |
b9201622 NS |
705 | else if (!COMPLETE_TYPE_P (ctype)) |
706 | cxx_incomplete_type_error (function, ctype); | |
8d08fdba | 707 | else |
a82f93ac SE |
708 | error ("no %q#D member function declared in class %qT", |
709 | function, ctype); | |
8d08fdba | 710 | |
7d3bec9d NS |
711 | if (pushed_scope) |
712 | pop_scope (pushed_scope); | |
f8c3b097 | 713 | return error_mark_node; |
8d08fdba MS |
714 | } |
715 | ||
4684cd27 MM |
716 | /* DECL is a function with vague linkage. Remember it so that at the |
717 | end of the translation unit we can decide whether or not to emit | |
718 | it. */ | |
719 | ||
720 | void | |
721 | note_vague_linkage_fn (tree decl) | |
722 | { | |
783a452c | 723 | DECL_DEFER_OUTPUT (decl) = 1; |
9771b263 | 724 | vec_safe_push (deferred_fns, decl); |
4684cd27 MM |
725 | } |
726 | ||
5e0231c2 JM |
727 | /* As above, but for variable template instantiations. */ |
728 | ||
729 | void | |
730 | note_variable_template_instantiation (tree decl) | |
731 | { | |
732 | vec_safe_push (pending_statics, decl); | |
733 | } | |
734 | ||
fa8d6e85 | 735 | /* We have just processed the DECL, which is a static data member. |
d174af6c | 736 | The other parameters are as for cp_finish_decl. */ |
fa8d6e85 MM |
737 | |
738 | void | |
3db45ab5 | 739 | finish_static_data_member_decl (tree decl, |
d174af6c MM |
740 | tree init, bool init_const_expr_p, |
741 | tree asmspec_tree, | |
0cbd7506 | 742 | int flags) |
fa8d6e85 | 743 | { |
1f6e1acc AS |
744 | DECL_CONTEXT (decl) = current_class_type; |
745 | ||
fa8d6e85 | 746 | /* We cannot call pushdecl here, because that would fill in the |
46ccf50a | 747 | TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do |
fa8d6e85 | 748 | the right thing, namely, to put this decl out straight away. */ |
92643fea | 749 | |
42ccbf3d | 750 | if (! processing_template_decl) |
9771b263 | 751 | vec_safe_push (pending_statics, decl); |
0aafb128 | 752 | |
637f68e8 JM |
753 | if (LOCAL_CLASS_P (current_class_type) |
754 | /* We already complained about the template definition. */ | |
755 | && !DECL_TEMPLATE_INSTANTIATION (decl)) | |
cbe5f3b3 | 756 | permerror (input_location, "local class %q#T shall not have static data member %q#D", |
37ec60ed | 757 | current_class_type, decl); |
a6659b55 JM |
758 | else |
759 | for (tree t = current_class_type; TYPE_P (t); | |
760 | t = CP_TYPE_CONTEXT (t)) | |
6a7b9203 | 761 | if (TYPE_UNNAMED_P (t)) |
a6659b55 JM |
762 | { |
763 | if (permerror (DECL_SOURCE_LOCATION (decl), | |
764 | "static data member %qD in unnamed class", decl)) | |
765 | inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), | |
766 | "unnamed class defined here"); | |
767 | break; | |
768 | } | |
65f36ac8 | 769 | |
fa8d6e85 | 770 | DECL_IN_AGGR_P (decl) = 1; |
fa8d6e85 | 771 | |
48326487 JM |
772 | if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE |
773 | && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE) | |
774 | SET_VAR_HAD_UNKNOWN_BOUND (decl); | |
775 | ||
d174af6c | 776 | cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags); |
fa8d6e85 MM |
777 | } |
778 | ||
84dc00e8 | 779 | /* DECLARATOR and DECLSPECS correspond to a class member. The other |
d174af6c | 780 | parameters are as for cp_finish_decl. Return the DECL for the |
3db45ab5 | 781 | class member declared. */ |
8d08fdba MS |
782 | |
783 | tree | |
c8094d83 MS |
784 | grokfield (const cp_declarator *declarator, |
785 | cp_decl_specifier_seq *declspecs, | |
d174af6c MM |
786 | tree init, bool init_const_expr_p, |
787 | tree asmspec_tree, | |
0cbd7506 | 788 | tree attrlist) |
8d08fdba | 789 | { |
59387d2e | 790 | tree value; |
9c0758dd | 791 | const char *asmspec = 0; |
0e5f8a59 | 792 | int flags; |
3937f036 | 793 | tree name; |
8d08fdba | 794 | |
8d08fdba MS |
795 | if (init |
796 | && TREE_CODE (init) == TREE_LIST | |
797 | && TREE_VALUE (init) == error_mark_node | |
798 | && TREE_CHAIN (init) == NULL_TREE) | |
cffa8729 | 799 | init = NULL_TREE; |
8d08fdba | 800 | |
91d231cb | 801 | value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist); |
e765a228 | 802 | if (! value || value == error_mark_node) |
3ddfb0e6 | 803 | /* friend or constructor went bad. */ |
04d57dd5 | 804 | return error_mark_node; |
e765a228 JM |
805 | if (TREE_TYPE (value) == error_mark_node) |
806 | return value; | |
04d57dd5 ZW |
807 | |
808 | if (TREE_CODE (value) == TYPE_DECL && init) | |
809 | { | |
67dc1f24 | 810 | error ("typedef %qD is initialized (use decltype instead)", value); |
04d57dd5 ZW |
811 | init = NULL_TREE; |
812 | } | |
8d08fdba MS |
813 | |
814 | /* Pass friendly classes back. */ | |
8db1028e NS |
815 | if (value == void_type_node) |
816 | return value; | |
817 | ||
8d08fdba | 818 | |
3937f036 PC |
819 | name = DECL_NAME (value); |
820 | ||
821 | if (name != NULL_TREE) | |
822 | { | |
823 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) | |
824 | { | |
825 | error ("explicit template argument list not allowed"); | |
826 | return error_mark_node; | |
827 | } | |
828 | ||
829 | if (IDENTIFIER_POINTER (name)[0] == '_' | |
a01f151f | 830 | && id_equal (name, "_vptr")) |
3937f036 PC |
831 | error ("member %qD conflicts with virtual function table field name", |
832 | value); | |
833 | } | |
8d08fdba MS |
834 | |
835 | /* Stash away type declarations. */ | |
836 | if (TREE_CODE (value) == TYPE_DECL) | |
837 | { | |
838 | DECL_NONLOCAL (value) = 1; | |
700f8a87 | 839 | DECL_CONTEXT (value) = current_class_type; |
8145f082 | 840 | |
9596e064 | 841 | if (attrlist) |
01d685cc JM |
842 | { |
843 | int attrflags = 0; | |
844 | ||
845 | /* If this is a typedef that names the class for linkage purposes | |
846 | (7.1.3p8), apply any attributes directly to the type. */ | |
73243d63 | 847 | if (OVERLOAD_TYPE_P (TREE_TYPE (value)) |
01d685cc JM |
848 | && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value)))) |
849 | attrflags = ATTR_FLAG_TYPE_IN_PLACE; | |
850 | ||
851 | cplus_decl_attributes (&value, attrlist, attrflags); | |
852 | } | |
9596e064 | 853 | |
0b2c4be5 | 854 | if (decl_spec_seq_has_spec_p (declspecs, ds_typedef) |
4fdaccea DS |
855 | && TREE_TYPE (value) != error_mark_node |
856 | && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value) | |
6f1abb06 | 857 | set_underlying_type (value); |
d0940d56 | 858 | |
28704289 DS |
859 | /* It's important that push_template_decl below follows |
860 | set_underlying_type above so that the created template | |
861 | carries the properly set type of VALUE. */ | |
862 | if (processing_template_decl) | |
863 | value = push_template_decl (value); | |
864 | ||
3797cb21 | 865 | record_locally_defined_typedef (value); |
8d08fdba MS |
866 | return value; |
867 | } | |
868 | ||
5d9607f0 VV |
869 | int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend); |
870 | ||
871 | if (!friendp && DECL_IN_AGGR_P (value)) | |
8d08fdba | 872 | { |
2d01edd7 | 873 | error ("%qD is already defined in %qT", value, DECL_CONTEXT (value)); |
8d08fdba MS |
874 | return void_type_node; |
875 | } | |
876 | ||
4f543d15 | 877 | if (asmspec_tree && asmspec_tree != error_mark_node) |
8d08fdba MS |
878 | asmspec = TREE_STRING_POINTER (asmspec_tree); |
879 | ||
880 | if (init) | |
881 | { | |
6eabb241 | 882 | if (TREE_CODE (value) == FUNCTION_DECL) |
8d08fdba | 883 | { |
b87d79e6 JM |
884 | if (init == ridpointers[(int)RID_DELETE]) |
885 | { | |
886 | DECL_DELETED_FN (value) = 1; | |
887 | DECL_DECLARED_INLINE_P (value) = 1; | |
888 | DECL_INITIAL (value) = error_mark_node; | |
889 | } | |
890 | else if (init == ridpointers[(int)RID_DEFAULT]) | |
891 | { | |
20f2653e | 892 | if (defaultable_fn_check (value)) |
b87d79e6 JM |
893 | { |
894 | DECL_DEFAULTED_FN (value) = 1; | |
895 | DECL_INITIALIZED_IN_CLASS_P (value) = 1; | |
896 | DECL_DECLARED_INLINE_P (value) = 1; | |
b87d79e6 JM |
897 | } |
898 | } | |
eb026338 JM |
899 | else if (TREE_CODE (init) == DEFAULT_ARG) |
900 | error ("invalid initializer for member function %qD", value); | |
b87d79e6 | 901 | else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE) |
ea7ce50b | 902 | { |
7801b86a SM |
903 | if (integer_zerop (init)) |
904 | DECL_PURE_VIRTUAL_P (value) = 1; | |
905 | else if (error_operand_p (init)) | |
906 | ; /* An error has already been reported. */ | |
907 | else | |
908 | error ("invalid initializer for member function %qD", | |
909 | value); | |
ea7ce50b VR |
910 | } |
911 | else | |
912 | { | |
913 | gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE); | |
5d9607f0 VV |
914 | if (friendp) |
915 | error ("initializer specified for friend function %qD", | |
916 | value); | |
917 | else | |
918 | error ("initializer specified for static member function %qD", | |
919 | value); | |
ea7ce50b | 920 | } |
8d08fdba | 921 | } |
0e5f8a59 JM |
922 | else if (TREE_CODE (value) == FIELD_DECL) |
923 | /* C++11 NSDMI, keep going. */; | |
5a6ccc94 | 924 | else if (!VAR_P (value)) |
0e5f8a59 | 925 | gcc_unreachable (); |
8d08fdba MS |
926 | } |
927 | ||
5d9607f0 VV |
928 | /* Pass friend decls back. */ |
929 | if ((TREE_CODE (value) == FUNCTION_DECL | |
930 | || TREE_CODE (value) == TEMPLATE_DECL) | |
931 | && DECL_CONTEXT (value) != current_class_type) | |
932 | return value; | |
933 | ||
37491340 | 934 | /* Need to set this before push_template_decl. */ |
56a6f1d3 | 935 | if (VAR_P (value)) |
37491340 JM |
936 | DECL_CONTEXT (value) = current_class_type; |
937 | ||
cb6da767 | 938 | if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value)) |
89f1a6ec MM |
939 | { |
940 | value = push_template_decl (value); | |
941 | if (error_operand_p (value)) | |
942 | return error_mark_node; | |
943 | } | |
5566b478 | 944 | |
45537677 | 945 | if (attrlist) |
91d231cb | 946 | cplus_decl_attributes (&value, attrlist, 0); |
45537677 | 947 | |
014397c2 | 948 | if (init && DIRECT_LIST_INIT_P (init)) |
0e5f8a59 JM |
949 | flags = LOOKUP_NORMAL; |
950 | else | |
951 | flags = LOOKUP_IMPLICIT; | |
952 | ||
8dc2b103 | 953 | switch (TREE_CODE (value)) |
8d08fdba | 954 | { |
8dc2b103 | 955 | case VAR_DECL: |
d174af6c MM |
956 | finish_static_data_member_decl (value, init, init_const_expr_p, |
957 | asmspec_tree, flags); | |
8d08fdba | 958 | return value; |
8dc2b103 NS |
959 | |
960 | case FIELD_DECL: | |
8d08fdba | 961 | if (asmspec) |
9e637a26 | 962 | error ("%<asm%> specifiers are not permitted on non-static data members"); |
8d08fdba MS |
963 | if (DECL_INITIAL (value) == error_mark_node) |
964 | init = error_mark_node; | |
3db45ab5 | 965 | cp_finish_decl (value, init, /*init_const_expr_p=*/false, |
d174af6c | 966 | NULL_TREE, flags); |
8d08fdba MS |
967 | DECL_IN_AGGR_P (value) = 1; |
968 | return value; | |
8dc2b103 NS |
969 | |
970 | case FUNCTION_DECL: | |
6060a796 | 971 | if (asmspec) |
0e6df31e | 972 | set_user_assembler_name (value, asmspec); |
c8094d83 | 973 | |
3db45ab5 MS |
974 | cp_finish_decl (value, |
975 | /*init=*/NULL_TREE, | |
976 | /*init_const_expr_p=*/false, | |
d174af6c | 977 | asmspec_tree, flags); |
8d08fdba MS |
978 | |
979 | /* Pass friends back this way. */ | |
980 | if (DECL_FRIEND_P (value)) | |
981 | return void_type_node; | |
982 | ||
983 | DECL_IN_AGGR_P (value) = 1; | |
984 | return value; | |
c8094d83 | 985 | |
8dc2b103 NS |
986 | default: |
987 | gcc_unreachable (); | |
8d08fdba | 988 | } |
8d08fdba MS |
989 | return NULL_TREE; |
990 | } | |
991 | ||
992 | /* Like `grokfield', but for bitfields. | |
993 | WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */ | |
994 | ||
995 | tree | |
c8094d83 | 996 | grokbitfield (const cp_declarator *declarator, |
a6d9bc9d SM |
997 | cp_decl_specifier_seq *declspecs, tree width, |
998 | tree attrlist) | |
8d08fdba | 999 | { |
a6d9bc9d | 1000 | tree value = grokdeclarator (declarator, declspecs, BITFIELD, 0, &attrlist); |
8d08fdba | 1001 | |
e765a228 | 1002 | if (value == error_mark_node) |
344f237b | 1003 | return NULL_TREE; /* friends went bad. */ |
e765a228 JM |
1004 | if (TREE_TYPE (value) == error_mark_node) |
1005 | return value; | |
8d08fdba MS |
1006 | |
1007 | /* Pass friendly classes back. */ | |
50e10fa8 | 1008 | if (VOID_TYPE_P (value)) |
8d08fdba MS |
1009 | return void_type_node; |
1010 | ||
4d0648ac | 1011 | if (!INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value)) |
4c9fb870 LM |
1012 | && (POINTER_TYPE_P (value) |
1013 | || !dependent_type_p (TREE_TYPE (value)))) | |
1014 | { | |
1015 | error ("bit-field %qD with non-integral type", value); | |
1016 | return error_mark_node; | |
1017 | } | |
1018 | ||
8d08fdba MS |
1019 | if (TREE_CODE (value) == TYPE_DECL) |
1020 | { | |
2d01edd7 | 1021 | error ("cannot declare %qD to be a bit-field type", value); |
8d08fdba MS |
1022 | return NULL_TREE; |
1023 | } | |
1024 | ||
33bd39a2 | 1025 | /* Usually, finish_struct_1 catches bitfields with invalid types. |
ae58fa02 MM |
1026 | But, in the case of bitfields with function type, we confuse |
1027 | ourselves into thinking they are member functions, so we must | |
1028 | check here. */ | |
1029 | if (TREE_CODE (value) == FUNCTION_DECL) | |
1030 | { | |
2d01edd7 | 1031 | error ("cannot declare bit-field %qD with function type", |
33bd39a2 | 1032 | DECL_NAME (value)); |
ae58fa02 MM |
1033 | return NULL_TREE; |
1034 | } | |
1035 | ||
8d08fdba MS |
1036 | if (DECL_IN_AGGR_P (value)) |
1037 | { | |
2d01edd7 | 1038 | error ("%qD is already defined in the class %qT", value, |
0cbd7506 | 1039 | DECL_CONTEXT (value)); |
8d08fdba MS |
1040 | return void_type_node; |
1041 | } | |
1042 | ||
8d08fdba MS |
1043 | if (TREE_STATIC (value)) |
1044 | { | |
2d01edd7 | 1045 | error ("static member %qD cannot be a bit-field", value); |
8d08fdba MS |
1046 | return NULL_TREE; |
1047 | } | |
3600f678 | 1048 | cp_finish_decl (value, NULL_TREE, false, NULL_TREE, 0); |
8d08fdba MS |
1049 | |
1050 | if (width != error_mark_node) | |
1051 | { | |
441b624e | 1052 | /* The width must be an integer type. */ |
faa9e9bf JM |
1053 | if (!type_dependent_expression_p (width) |
1054 | && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width))) | |
441b624e JM |
1055 | error ("width of bit-field %qD has non-integral type %qT", value, |
1056 | TREE_TYPE (width)); | |
d8d58893 PC |
1057 | else |
1058 | { | |
1059 | DECL_INITIAL (value) = width; | |
1060 | SET_DECL_C_BIT_FIELD (value); | |
1061 | } | |
8d08fdba MS |
1062 | } |
1063 | ||
1064 | DECL_IN_AGGR_P (value) = 1; | |
a6d9bc9d SM |
1065 | |
1066 | if (attrlist) | |
1067 | cplus_decl_attributes (&value, attrlist, /*flags=*/0); | |
1068 | ||
8d08fdba MS |
1069 | return value; |
1070 | } | |
1071 | ||
8d08fdba | 1072 | \f |
4f4141ff JM |
1073 | /* Returns true iff ATTR is an attribute which needs to be applied at |
1074 | instantiation time rather than template definition time. */ | |
1075 | ||
a7f6bc8c JM |
1076 | static bool |
1077 | is_late_template_attribute (tree attr, tree decl) | |
4f4141ff | 1078 | { |
e28d52cf | 1079 | tree name = get_attribute_name (attr); |
5044ab0e | 1080 | tree args = TREE_VALUE (attr); |
a7f6bc8c | 1081 | const struct attribute_spec *spec = lookup_attribute_spec (name); |
160b8b80 | 1082 | tree arg; |
a7f6bc8c | 1083 | |
533007c1 JM |
1084 | if (!spec) |
1085 | /* Unknown attribute. */ | |
1086 | return false; | |
1087 | ||
5dc11954 JJ |
1088 | /* Attribute weak handling wants to write out assembly right away. */ |
1089 | if (is_attribute_p ("weak", name)) | |
8d2eb304 JM |
1090 | return true; |
1091 | ||
d10402b4 DS |
1092 | /* Attribute unused is applied directly, as it appertains to |
1093 | decls. */ | |
1094 | if (is_attribute_p ("unused", name)) | |
1095 | return false; | |
1096 | ||
47297e51 JM |
1097 | /* Attribute tls_model wants to modify the symtab. */ |
1098 | if (is_attribute_p ("tls_model", name)) | |
1099 | return true; | |
1100 | ||
acf0174b JJ |
1101 | /* #pragma omp declare simd attribute needs to be always deferred. */ |
1102 | if (flag_openmp | |
1103 | && is_attribute_p ("omp declare simd", name)) | |
1104 | return true; | |
1105 | ||
90f11cf9 JM |
1106 | /* An attribute pack is clearly dependent. */ |
1107 | if (args && PACK_EXPANSION_P (args)) | |
1108 | return true; | |
1109 | ||
160b8b80 JM |
1110 | /* If any of the arguments are dependent expressions, we can't evaluate |
1111 | the attribute until instantiation time. */ | |
1112 | for (arg = args; arg; arg = TREE_CHAIN (arg)) | |
1113 | { | |
1114 | tree t = TREE_VALUE (arg); | |
b2febff9 JJ |
1115 | |
1116 | /* If the first attribute argument is an identifier, only consider | |
1117 | second and following arguments. Attributes like mode, format, | |
1118 | cleanup and several target specific attributes aren't late | |
1119 | just because they have an IDENTIFIER_NODE as first argument. */ | |
69da7802 MS |
1120 | if (arg == args && attribute_takes_identifier_p (name) |
1121 | && identifier_p (t)) | |
b2febff9 JJ |
1122 | continue; |
1123 | ||
160b8b80 JM |
1124 | if (value_dependent_expression_p (t) |
1125 | || type_dependent_expression_p (t)) | |
1126 | return true; | |
1127 | } | |
1128 | ||
1129 | if (TREE_CODE (decl) == TYPE_DECL | |
1130 | || TYPE_P (decl) | |
1131 | || spec->type_required) | |
a7f6bc8c JM |
1132 | { |
1133 | tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl); | |
1134 | ||
1135 | /* We can't apply any attributes to a completely unknown type until | |
1136 | instantiation time. */ | |
1137 | enum tree_code code = TREE_CODE (type); | |
1138 | if (code == TEMPLATE_TYPE_PARM | |
1139 | || code == BOUND_TEMPLATE_TEMPLATE_PARM | |
1140 | || code == TYPENAME_TYPE) | |
1141 | return true; | |
1d555e26 JM |
1142 | /* Also defer most attributes on dependent types. This is not |
1143 | necessary in all cases, but is the better default. */ | |
1144 | else if (dependent_type_p (type) | |
5f7282e2 | 1145 | /* But some attributes specifically apply to templates. */ |
2982147e | 1146 | && !is_attribute_p ("abi_tag", name) |
5f7282e2 | 1147 | && !is_attribute_p ("deprecated", name) |
1d555e26 | 1148 | && !is_attribute_p ("visibility", name)) |
160b8b80 | 1149 | return true; |
a7f6bc8c JM |
1150 | else |
1151 | return false; | |
1152 | } | |
4f4141ff JM |
1153 | else |
1154 | return false; | |
1155 | } | |
1156 | ||
1157 | /* ATTR_P is a list of attributes. Remove any attributes which need to be | |
a7f6bc8c JM |
1158 | applied at instantiation time and return them. If IS_DEPENDENT is true, |
1159 | the declaration itself is dependent, so all attributes should be applied | |
1160 | at instantiation time. */ | |
4f4141ff JM |
1161 | |
1162 | static tree | |
a7f6bc8c | 1163 | splice_template_attributes (tree *attr_p, tree decl) |
4f4141ff JM |
1164 | { |
1165 | tree *p = attr_p; | |
1166 | tree late_attrs = NULL_TREE; | |
1167 | tree *q = &late_attrs; | |
1168 | ||
1169 | if (!p) | |
1170 | return NULL_TREE; | |
1171 | ||
1172 | for (; *p; ) | |
1173 | { | |
a7f6bc8c | 1174 | if (is_late_template_attribute (*p, decl)) |
4f4141ff | 1175 | { |
a7f6bc8c | 1176 | ATTR_IS_DEPENDENT (*p) = 1; |
4f4141ff JM |
1177 | *q = *p; |
1178 | *p = TREE_CHAIN (*p); | |
1179 | q = &TREE_CHAIN (*q); | |
1180 | *q = NULL_TREE; | |
1181 | } | |
1182 | else | |
1183 | p = &TREE_CHAIN (*p); | |
1184 | } | |
1185 | ||
1186 | return late_attrs; | |
1187 | } | |
1188 | ||
1189 | /* Remove any late attributes from the list in ATTR_P and attach them to | |
1190 | DECL_P. */ | |
1191 | ||
1192 | static void | |
1193 | save_template_attributes (tree *attr_p, tree *decl_p) | |
1194 | { | |
4f4141ff JM |
1195 | tree *q; |
1196 | ||
2765f897 PC |
1197 | if (attr_p && *attr_p == error_mark_node) |
1198 | return; | |
1199 | ||
1200 | tree late_attrs = splice_template_attributes (attr_p, *decl_p); | |
4f4141ff JM |
1201 | if (!late_attrs) |
1202 | return; | |
1203 | ||
1204 | if (DECL_P (*decl_p)) | |
1205 | q = &DECL_ATTRIBUTES (*decl_p); | |
1206 | else | |
1207 | q = &TYPE_ATTRIBUTES (*decl_p); | |
1208 | ||
2765f897 | 1209 | tree old_attrs = *q; |
1db54f4e | 1210 | |
54a9f5ea | 1211 | /* Merge the late attributes at the beginning with the attribute |
1db54f4e | 1212 | list. */ |
54a9f5ea | 1213 | late_attrs = merge_attributes (late_attrs, *q); |
4f4141ff | 1214 | *q = late_attrs; |
1db54f4e DG |
1215 | |
1216 | if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p)) | |
1217 | { | |
1218 | /* We've added new attributes directly to the main variant, so | |
1219 | now we need to update all of the other variants to include | |
1220 | these new attributes. */ | |
1221 | tree variant; | |
1222 | for (variant = TYPE_NEXT_VARIANT (*decl_p); variant; | |
1223 | variant = TYPE_NEXT_VARIANT (variant)) | |
1224 | { | |
1225 | gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs); | |
1226 | TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p); | |
1227 | } | |
1228 | } | |
4f4141ff JM |
1229 | } |
1230 | ||
f3365c12 JM |
1231 | /* True if ATTRS contains any dependent attributes that affect type |
1232 | identity. */ | |
1233 | ||
1234 | bool | |
1235 | any_dependent_type_attributes_p (tree attrs) | |
1236 | { | |
1237 | for (tree a = attrs; a; a = TREE_CHAIN (a)) | |
1238 | if (ATTR_IS_DEPENDENT (a)) | |
1239 | { | |
1240 | const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (a)); | |
1241 | if (as && as->affects_type_identity) | |
1242 | return true; | |
1243 | } | |
1244 | return false; | |
1245 | } | |
1246 | ||
404c2aea | 1247 | /* Return true iff ATTRS are acceptable attributes to be applied in-place |
6a7b9203 | 1248 | to a typedef which gives a previously unnamed class or enum a name for |
404c2aea JM |
1249 | linkage purposes. */ |
1250 | ||
1251 | bool | |
1252 | attributes_naming_typedef_ok (tree attrs) | |
1253 | { | |
1254 | for (; attrs; attrs = TREE_CHAIN (attrs)) | |
1255 | { | |
1256 | tree name = get_attribute_name (attrs); | |
1257 | if (is_attribute_p ("vector_size", name)) | |
1258 | return false; | |
1259 | } | |
1260 | return true; | |
1261 | } | |
1262 | ||
5dc11954 JJ |
1263 | /* Like reconstruct_complex_type, but handle also template trees. */ |
1264 | ||
1265 | tree | |
1266 | cp_reconstruct_complex_type (tree type, tree bottom) | |
1267 | { | |
1268 | tree inner, outer; | |
cab421f4 | 1269 | bool late_return_type_p = false; |
5dc11954 | 1270 | |
50e10fa8 | 1271 | if (TYPE_PTR_P (type)) |
5dc11954 JJ |
1272 | { |
1273 | inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom); | |
1274 | outer = build_pointer_type_for_mode (inner, TYPE_MODE (type), | |
1275 | TYPE_REF_CAN_ALIAS_ALL (type)); | |
1276 | } | |
1277 | else if (TREE_CODE (type) == REFERENCE_TYPE) | |
1278 | { | |
1279 | inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom); | |
1280 | outer = build_reference_type_for_mode (inner, TYPE_MODE (type), | |
1281 | TYPE_REF_CAN_ALIAS_ALL (type)); | |
1282 | } | |
1283 | else if (TREE_CODE (type) == ARRAY_TYPE) | |
1284 | { | |
1285 | inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom); | |
1286 | outer = build_cplus_array_type (inner, TYPE_DOMAIN (type)); | |
1287 | /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the | |
1288 | element type qualification will be handled by the recursive | |
1289 | cp_reconstruct_complex_type call and cp_build_qualified_type | |
1290 | for ARRAY_TYPEs changes the element type. */ | |
1291 | return outer; | |
1292 | } | |
1293 | else if (TREE_CODE (type) == FUNCTION_TYPE) | |
1294 | { | |
cab421f4 | 1295 | late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type); |
5dc11954 JJ |
1296 | inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom); |
1297 | outer = build_function_type (inner, TYPE_ARG_TYPES (type)); | |
2eed8e37 BK |
1298 | outer = apply_memfn_quals (outer, |
1299 | type_memfn_quals (type), | |
1300 | type_memfn_rqual (type)); | |
5dc11954 JJ |
1301 | } |
1302 | else if (TREE_CODE (type) == METHOD_TYPE) | |
1303 | { | |
cab421f4 | 1304 | late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (type); |
5dc11954 JJ |
1305 | inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom); |
1306 | /* The build_method_type_directly() routine prepends 'this' to argument list, | |
1307 | so we must compensate by getting rid of it. */ | |
1308 | outer | |
1309 | = build_method_type_directly | |
7e1352fe | 1310 | (class_of_this_parm (type), inner, |
5dc11954 JJ |
1311 | TREE_CHAIN (TYPE_ARG_TYPES (type))); |
1312 | } | |
1313 | else if (TREE_CODE (type) == OFFSET_TYPE) | |
1314 | { | |
1315 | inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom); | |
1316 | outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner); | |
1317 | } | |
1318 | else | |
1319 | return bottom; | |
1320 | ||
3c3905fc JM |
1321 | if (TYPE_ATTRIBUTES (type)) |
1322 | outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type)); | |
cab421f4 PC |
1323 | outer = cp_build_qualified_type (outer, cp_type_quals (type)); |
1324 | ||
1325 | if (late_return_type_p) | |
1326 | TYPE_HAS_LATE_RETURN_TYPE (outer) = 1; | |
1327 | ||
1328 | return outer; | |
5dc11954 JJ |
1329 | } |
1330 | ||
437697b8 RRC |
1331 | /* Replaces any constexpr expression that may be into the attributes |
1332 | arguments with their reduced value. */ | |
1333 | ||
1334 | static void | |
1335 | cp_check_const_attributes (tree attributes) | |
1336 | { | |
2765f897 PC |
1337 | if (attributes == error_mark_node) |
1338 | return; | |
1339 | ||
437697b8 RRC |
1340 | tree attr; |
1341 | for (attr = attributes; attr; attr = TREE_CHAIN (attr)) | |
1342 | { | |
1343 | tree arg; | |
1344 | for (arg = TREE_VALUE (attr); arg; arg = TREE_CHAIN (arg)) | |
1345 | { | |
1346 | tree expr = TREE_VALUE (arg); | |
1347 | if (EXPR_P (expr)) | |
1348 | TREE_VALUE (arg) = maybe_constant_value (expr); | |
1349 | } | |
1350 | } | |
1351 | } | |
1352 | ||
acf0174b JJ |
1353 | /* Return true if TYPE is an OpenMP mappable type. */ |
1354 | bool | |
1355 | cp_omp_mappable_type (tree type) | |
1356 | { | |
1357 | /* Mappable type has to be complete. */ | |
1358 | if (type == error_mark_node || !COMPLETE_TYPE_P (type)) | |
1359 | return false; | |
1360 | /* Arrays have mappable type if the elements have mappable type. */ | |
1361 | while (TREE_CODE (type) == ARRAY_TYPE) | |
1362 | type = TREE_TYPE (type); | |
1363 | /* A mappable type cannot contain virtual members. */ | |
1364 | if (CLASS_TYPE_P (type) && CLASSTYPE_VTABLES (type)) | |
1365 | return false; | |
1366 | /* All data members must be non-static. */ | |
1367 | if (CLASS_TYPE_P (type)) | |
1368 | { | |
1369 | tree field; | |
1370 | for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) | |
56a6f1d3 | 1371 | if (VAR_P (field)) |
acf0174b JJ |
1372 | return false; |
1373 | /* All fields must have mappable types. */ | |
1374 | else if (TREE_CODE (field) == FIELD_DECL | |
1375 | && !cp_omp_mappable_type (TREE_TYPE (field))) | |
1376 | return false; | |
1377 | } | |
1378 | return true; | |
1379 | } | |
1380 | ||
4f4141ff JM |
1381 | /* Like decl_attributes, but handle C++ complexity. */ |
1382 | ||
28cbf42c | 1383 | void |
848eed92 | 1384 | cplus_decl_attributes (tree *decl, tree attributes, int flags) |
8d08fdba | 1385 | { |
6ae9b875 | 1386 | if (*decl == NULL_TREE || *decl == void_type_node |
0065c7eb | 1387 | || *decl == error_mark_node) |
e8abc66f MS |
1388 | return; |
1389 | ||
acf0174b JJ |
1390 | /* Add implicit "omp declare target" attribute if requested. */ |
1391 | if (scope_chain->omp_declare_target_attribute | |
56a6f1d3 | 1392 | && ((VAR_P (*decl) |
9b65e171 | 1393 | && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl))) |
acf0174b JJ |
1394 | || TREE_CODE (*decl) == FUNCTION_DECL)) |
1395 | { | |
56a6f1d3 | 1396 | if (VAR_P (*decl) |
acf0174b JJ |
1397 | && DECL_CLASS_SCOPE_P (*decl)) |
1398 | error ("%q+D static data member inside of declare target directive", | |
1399 | *decl); | |
acf0174b | 1400 | else if (!processing_template_decl |
56a6f1d3 | 1401 | && VAR_P (*decl) |
acf0174b JJ |
1402 | && !cp_omp_mappable_type (TREE_TYPE (*decl))) |
1403 | error ("%q+D in declare target directive does not have mappable type", | |
1404 | *decl); | |
1405 | else | |
1406 | attributes = tree_cons (get_identifier ("omp declare target"), | |
1407 | NULL_TREE, attributes); | |
1408 | } | |
1409 | ||
4f4141ff JM |
1410 | if (processing_template_decl) |
1411 | { | |
7b3e2d46 DG |
1412 | if (check_for_bare_parameter_packs (attributes)) |
1413 | return; | |
1414 | ||
4f4141ff | 1415 | save_template_attributes (&attributes, decl); |
4f4141ff JM |
1416 | } |
1417 | ||
437697b8 RRC |
1418 | cp_check_const_attributes (attributes); |
1419 | ||
59387d2e JM |
1420 | if (TREE_CODE (*decl) == TEMPLATE_DECL) |
1421 | decl = &DECL_TEMPLATE_RESULT (*decl); | |
e8abc66f | 1422 | |
1dbdb756 PP |
1423 | if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl))) |
1424 | { | |
1425 | attributes | |
1426 | = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT); | |
35abb8ed | 1427 | decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)), |
1dbdb756 PP |
1428 | attributes, flags); |
1429 | } | |
1430 | else | |
1431 | decl_attributes (decl, attributes, flags); | |
863adfc0 | 1432 | |
59387d2e JM |
1433 | if (TREE_CODE (*decl) == TYPE_DECL) |
1434 | SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl)); | |
5f7282e2 JM |
1435 | |
1436 | /* Propagate deprecation out to the template. */ | |
1437 | if (TREE_DEPRECATED (*decl)) | |
1438 | if (tree ti = get_template_info (*decl)) | |
1439 | { | |
1440 | tree tmpl = TI_TEMPLATE (ti); | |
1441 | tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl) | |
1442 | : DECL_TEMPLATE_RESULT (tmpl)); | |
1443 | if (*decl == pattern) | |
1444 | TREE_DEPRECATED (tmpl) = true; | |
1445 | } | |
8d08fdba | 1446 | } |
8d08fdba | 1447 | \f |
744b12b6 | 1448 | /* Walks through the namespace- or function-scope anonymous union |
9bfa80fb | 1449 | OBJECT, with the indicated TYPE, building appropriate VAR_DECLs. |
744b12b6 | 1450 | Returns one of the fields for use in the mangled name. */ |
f237d906 | 1451 | |
0ca7178c | 1452 | static tree |
744b12b6 | 1453 | build_anon_union_vars (tree type, tree object) |
ce1b9eb9 | 1454 | { |
ce1b9eb9 | 1455 | tree main_decl = NULL_TREE; |
cb96daa2 | 1456 | tree field; |
ce1b9eb9 | 1457 | |
6bdb8141 JM |
1458 | /* Rather than write the code to handle the non-union case, |
1459 | just give an error. */ | |
1460 | if (TREE_CODE (type) != UNION_TYPE) | |
165d997d PC |
1461 | { |
1462 | error ("anonymous struct not inside named type"); | |
1463 | return error_mark_node; | |
1464 | } | |
6bdb8141 | 1465 | |
c8094d83 MS |
1466 | for (field = TYPE_FIELDS (type); |
1467 | field != NULL_TREE; | |
910ad8de | 1468 | field = DECL_CHAIN (field)) |
ce1b9eb9 | 1469 | { |
cb96daa2 | 1470 | tree decl; |
0ca7178c | 1471 | tree ref; |
8ebeee52 JM |
1472 | |
1473 | if (DECL_ARTIFICIAL (field)) | |
ce1b9eb9 | 1474 | continue; |
8ebeee52 JM |
1475 | if (TREE_CODE (field) != FIELD_DECL) |
1476 | { | |
af718670 PC |
1477 | permerror (DECL_SOURCE_LOCATION (field), |
1478 | "%q#D invalid; an anonymous union can only " | |
37ec60ed | 1479 | "have non-static data members", field); |
8ebeee52 JM |
1480 | continue; |
1481 | } | |
ce1b9eb9 | 1482 | |
cb96daa2 | 1483 | if (TREE_PRIVATE (field)) |
af718670 PC |
1484 | permerror (DECL_SOURCE_LOCATION (field), |
1485 | "private member %q#D in anonymous union", field); | |
cb96daa2 | 1486 | else if (TREE_PROTECTED (field)) |
af718670 PC |
1487 | permerror (DECL_SOURCE_LOCATION (field), |
1488 | "protected member %q#D in anonymous union", field); | |
cb96daa2 | 1489 | |
8fbc5ae7 | 1490 | if (processing_template_decl) |
f330f599 PC |
1491 | ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object, |
1492 | DECL_NAME (field), NULL_TREE); | |
8fbc5ae7 MM |
1493 | else |
1494 | ref = build_class_member_access_expr (object, field, NULL_TREE, | |
5ade1ed2 | 1495 | false, tf_warning_or_error); |
0ca7178c JM |
1496 | |
1497 | if (DECL_NAME (field)) | |
cb96daa2 | 1498 | { |
56b4ea3d RH |
1499 | tree base; |
1500 | ||
c2255bc4 AH |
1501 | decl = build_decl (input_location, |
1502 | VAR_DECL, DECL_NAME (field), TREE_TYPE (field)); | |
555551c2 | 1503 | DECL_ANON_UNION_VAR_P (decl) = 1; |
f208b768 | 1504 | DECL_ARTIFICIAL (decl) = 1; |
56b4ea3d RH |
1505 | |
1506 | base = get_base_address (object); | |
1507 | TREE_PUBLIC (decl) = TREE_PUBLIC (base); | |
1508 | TREE_STATIC (decl) = TREE_STATIC (base); | |
1509 | DECL_EXTERNAL (decl) = DECL_EXTERNAL (base); | |
1510 | ||
9bfa80fb RH |
1511 | SET_DECL_VALUE_EXPR (decl, ref); |
1512 | DECL_HAS_VALUE_EXPR_P (decl) = 1; | |
56b4ea3d | 1513 | |
cb96daa2 | 1514 | decl = pushdecl (decl); |
cb96daa2 | 1515 | } |
0ca7178c | 1516 | else if (ANON_AGGR_TYPE_P (TREE_TYPE (field))) |
744b12b6 | 1517 | decl = build_anon_union_vars (TREE_TYPE (field), ref); |
dd1b7476 KG |
1518 | else |
1519 | decl = 0; | |
cb96daa2 | 1520 | |
0ca7178c JM |
1521 | if (main_decl == NULL_TREE) |
1522 | main_decl = decl; | |
cb96daa2 | 1523 | } |
0ca7178c | 1524 | |
ce1b9eb9 MM |
1525 | return main_decl; |
1526 | } | |
1527 | ||
c050ec51 JM |
1528 | /* Finish off the processing of a UNION_TYPE structure. If the union is an |
1529 | anonymous union, then all members must be laid out together. PUBLIC_P | |
1530 | is nonzero if this union is not declared static. */ | |
e92cc029 | 1531 | |
8d08fdba | 1532 | void |
848eed92 | 1533 | finish_anon_union (tree anon_union_decl) |
8d08fdba | 1534 | { |
06c00c70 | 1535 | tree type; |
cb96daa2 | 1536 | tree main_decl; |
06c00c70 MM |
1537 | bool public_p; |
1538 | ||
1539 | if (anon_union_decl == error_mark_node) | |
1540 | return; | |
1541 | ||
1542 | type = TREE_TYPE (anon_union_decl); | |
1543 | public_p = TREE_PUBLIC (anon_union_decl); | |
8d08fdba | 1544 | |
a1c65f9f | 1545 | /* The VAR_DECL's context is the same as the TYPE's context. */ |
e5410b32 | 1546 | DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type)); |
c8094d83 | 1547 | |
cb96daa2 | 1548 | if (TYPE_FIELDS (type) == NULL_TREE) |
8d08fdba MS |
1549 | return; |
1550 | ||
1551 | if (public_p) | |
1552 | { | |
ff9f1a5d | 1553 | error ("namespace-scope anonymous aggregates must be static"); |
8d08fdba MS |
1554 | return; |
1555 | } | |
1556 | ||
744b12b6 | 1557 | main_decl = build_anon_union_vars (type, anon_union_decl); |
8bdb09cb VR |
1558 | if (main_decl == error_mark_node) |
1559 | return; | |
8fbc5ae7 | 1560 | if (main_decl == NULL_TREE) |
01f4137f | 1561 | { |
d4ee4d25 | 1562 | warning (0, "anonymous union with no members"); |
8fbc5ae7 MM |
1563 | return; |
1564 | } | |
bd9bb3d2 | 1565 | |
8fbc5ae7 MM |
1566 | if (!processing_template_decl) |
1567 | { | |
0ca7178c JM |
1568 | /* Use main_decl to set the mangled name. */ |
1569 | DECL_NAME (anon_union_decl) = DECL_NAME (main_decl); | |
68017cb4 | 1570 | maybe_commonize_var (anon_union_decl); |
cfea9968 JJ |
1571 | if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl)) |
1572 | mangle_decl (anon_union_decl); | |
0ca7178c | 1573 | DECL_NAME (anon_union_decl) = NULL_TREE; |
01f4137f | 1574 | } |
edebf865 | 1575 | |
0ca7178c | 1576 | pushdecl (anon_union_decl); |
7996b727 | 1577 | cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0); |
8d08fdba | 1578 | } |
8d08fdba MS |
1579 | \f |
1580 | /* Auxiliary functions to make type signatures for | |
1581 | `operator new' and `operator delete' correspond to | |
1582 | what compiler will be expecting. */ | |
1583 | ||
8d08fdba | 1584 | tree |
848eed92 | 1585 | coerce_new_type (tree type) |
8d08fdba | 1586 | { |
36791f1e NS |
1587 | int e = 0; |
1588 | tree args = TYPE_ARG_TYPES (type); | |
1589 | ||
50bc768d | 1590 | gcc_assert (TREE_CODE (type) == FUNCTION_TYPE); |
c8094d83 | 1591 | |
36791f1e | 1592 | if (!same_type_p (TREE_TYPE (type), ptr_type_node)) |
2d01edd7 GDR |
1593 | { |
1594 | e = 1; | |
1595 | error ("%<operator new%> must return type %qT", ptr_type_node); | |
1596 | } | |
36791f1e | 1597 | |
07021f8c | 1598 | if (args && args != void_list_node) |
36791f1e | 1599 | { |
07021f8c PC |
1600 | if (TREE_PURPOSE (args)) |
1601 | { | |
1602 | /* [basic.stc.dynamic.allocation] | |
1603 | ||
1604 | The first parameter shall not have an associated default | |
1605 | argument. */ | |
1606 | error ("the first parameter of %<operator new%> cannot " | |
1607 | "have a default argument"); | |
1608 | /* Throw away the default argument. */ | |
1609 | TREE_PURPOSE (args) = NULL_TREE; | |
1610 | } | |
1611 | ||
1612 | if (!same_type_p (TREE_VALUE (args), size_type_node)) | |
1613 | { | |
1614 | e = 2; | |
1615 | args = TREE_CHAIN (args); | |
1616 | } | |
36791f1e | 1617 | } |
07021f8c PC |
1618 | else |
1619 | e = 2; | |
1620 | ||
1621 | if (e == 2) | |
cbe5f3b3 | 1622 | permerror (input_location, "%<operator new%> takes type %<size_t%> (%qT) " |
37ec60ed | 1623 | "as first parameter", size_type_node); |
07021f8c | 1624 | |
36791f1e NS |
1625 | switch (e) |
1626 | { | |
1627 | case 2: | |
c9f8536c | 1628 | args = tree_cons (NULL_TREE, size_type_node, args); |
f4f206f4 | 1629 | /* Fall through. */ |
36791f1e NS |
1630 | case 1: |
1631 | type = build_exception_variant | |
0cbd7506 MS |
1632 | (build_function_type (ptr_type_node, args), |
1633 | TYPE_RAISES_EXCEPTIONS (type)); | |
f4f206f4 | 1634 | /* Fall through. */ |
36791f1e NS |
1635 | default:; |
1636 | } | |
8d08fdba MS |
1637 | return type; |
1638 | } | |
1639 | ||
1640 | tree | |
848eed92 | 1641 | coerce_delete_type (tree type) |
8d08fdba | 1642 | { |
36791f1e NS |
1643 | int e = 0; |
1644 | tree args = TYPE_ARG_TYPES (type); | |
c8094d83 | 1645 | |
50bc768d | 1646 | gcc_assert (TREE_CODE (type) == FUNCTION_TYPE); |
824b9a4c | 1647 | |
36791f1e | 1648 | if (!same_type_p (TREE_TYPE (type), void_type_node)) |
2d01edd7 GDR |
1649 | { |
1650 | e = 1; | |
1651 | error ("%<operator delete%> must return type %qT", void_type_node); | |
1652 | } | |
8d08fdba | 1653 | |
36791f1e NS |
1654 | if (!args || args == void_list_node |
1655 | || !same_type_p (TREE_VALUE (args), ptr_type_node)) | |
8d08fdba | 1656 | { |
36791f1e NS |
1657 | e = 2; |
1658 | if (args && args != void_list_node) | |
0cbd7506 | 1659 | args = TREE_CHAIN (args); |
2d01edd7 | 1660 | error ("%<operator delete%> takes type %qT as first parameter", |
0cbd7506 | 1661 | ptr_type_node); |
8d08fdba | 1662 | } |
36791f1e NS |
1663 | switch (e) |
1664 | { | |
1665 | case 2: | |
1666 | args = tree_cons (NULL_TREE, ptr_type_node, args); | |
f4f206f4 | 1667 | /* Fall through. */ |
36791f1e NS |
1668 | case 1: |
1669 | type = build_exception_variant | |
0cbd7506 MS |
1670 | (build_function_type (void_type_node, args), |
1671 | TYPE_RAISES_EXCEPTIONS (type)); | |
f4f206f4 | 1672 | /* Fall through. */ |
36791f1e NS |
1673 | default:; |
1674 | } | |
8d08fdba MS |
1675 | |
1676 | return type; | |
1677 | } | |
1678 | \f | |
dc472c59 JM |
1679 | /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable |
1680 | and mark them as needed. */ | |
1681 | ||
8d08fdba | 1682 | static void |
848eed92 | 1683 | mark_vtable_entries (tree decl) |
8d08fdba | 1684 | { |
4038c495 GB |
1685 | tree fnaddr; |
1686 | unsigned HOST_WIDE_INT idx; | |
f30432d7 | 1687 | |
60349f15 JM |
1688 | /* It's OK for the vtable to refer to deprecated virtual functions. */ |
1689 | warning_sentinel w(warn_deprecated_decl); | |
1690 | ||
4038c495 GB |
1691 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)), |
1692 | idx, fnaddr) | |
8d08fdba | 1693 | { |
aff08c18 | 1694 | tree fn; |
bb4f6e6b JH |
1695 | |
1696 | STRIP_NOPS (fnaddr); | |
1697 | ||
67231816 RH |
1698 | if (TREE_CODE (fnaddr) != ADDR_EXPR |
1699 | && TREE_CODE (fnaddr) != FDESC_EXPR) | |
bbd15aac | 1700 | /* This entry is an offset: a virtual base class offset, a |
46ccf50a | 1701 | virtual call offset, an RTTI offset, etc. */ |
aff08c18 JM |
1702 | continue; |
1703 | ||
aff08c18 | 1704 | fn = TREE_OPERAND (fnaddr, 0); |
8926095f | 1705 | TREE_ADDRESSABLE (fn) = 1; |
31f8e4f3 MM |
1706 | /* When we don't have vcall offsets, we output thunks whenever |
1707 | we output the vtables that contain them. With vcall offsets, | |
1708 | we know all the thunks we'll need when we emit a virtual | |
1709 | function, so we emit the thunks there instead. */ | |
c8094d83 | 1710 | if (DECL_THUNK_P (fn)) |
d0cd8b44 | 1711 | use_thunk (fn, /*emit_p=*/0); |
5566b478 | 1712 | mark_used (fn); |
7177d104 MS |
1713 | } |
1714 | } | |
1715 | ||
d11ad92e MS |
1716 | /* Set DECL up to have the closest approximation of "initialized common" |
1717 | linkage available. */ | |
1718 | ||
1719 | void | |
848eed92 | 1720 | comdat_linkage (tree decl) |
d11ad92e | 1721 | { |
d11ad92e | 1722 | if (flag_weak) |
fc26fae3 | 1723 | make_decl_one_only (decl, cxx_comdat_group (decl)); |
c8094d83 | 1724 | else if (TREE_CODE (decl) == FUNCTION_DECL |
5a6ccc94 | 1725 | || (VAR_P (decl) && DECL_ARTIFICIAL (decl))) |
97458258 MM |
1726 | /* We can just emit function and compiler-generated variables |
1727 | statically; having multiple copies is (for the most part) only | |
c8094d83 | 1728 | a waste of space. |
97458258 MM |
1729 | |
1730 | There are two correctness issues, however: the address of a | |
1731 | template instantiation with external linkage should be the | |
ad50e811 MM |
1732 | same, independent of what translation unit asks for the |
1733 | address, and this will not hold when we emit multiple copies of | |
c8094d83 | 1734 | the function. However, there's little else we can do. |
97458258 | 1735 | |
3461fba7 NS |
1736 | Also, by default, the typeinfo implementation assumes that |
1737 | there will be only one copy of the string used as the name for | |
1738 | each type. Therefore, if weak symbols are unavailable, the | |
1739 | run-time library should perform a more conservative check; it | |
1740 | should perform a string comparison, rather than an address | |
1741 | comparison. */ | |
7fcdf4c2 | 1742 | TREE_PUBLIC (decl) = 0; |
ea735e02 JM |
1743 | else |
1744 | { | |
2f435bed JM |
1745 | /* Static data member template instantiations, however, cannot |
1746 | have multiple copies. */ | |
ea735e02 JM |
1747 | if (DECL_INITIAL (decl) == 0 |
1748 | || DECL_INITIAL (decl) == error_mark_node) | |
1749 | DECL_COMMON (decl) = 1; | |
1750 | else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))) | |
1751 | { | |
1752 | DECL_COMMON (decl) = 1; | |
1753 | DECL_INITIAL (decl) = error_mark_node; | |
1754 | } | |
49bf4577 | 1755 | else if (!DECL_EXPLICIT_INSTANTIATION (decl)) |
ea735e02 JM |
1756 | { |
1757 | /* We can't do anything useful; leave vars for explicit | |
1758 | instantiation. */ | |
1759 | DECL_EXTERNAL (decl) = 1; | |
1760 | DECL_NOT_REALLY_EXTERN (decl) = 0; | |
1761 | } | |
1762 | } | |
ab23f787 | 1763 | |
73f0dae2 MP |
1764 | if (TREE_PUBLIC (decl)) |
1765 | DECL_COMDAT (decl) = 1; | |
d11ad92e MS |
1766 | } |
1767 | ||
b385c841 JM |
1768 | /* For win32 we also want to put explicit instantiations in |
1769 | linkonce sections, so that they will be merged with implicit | |
c8094d83 MS |
1770 | instantiations; otherwise we get duplicate symbol errors. |
1771 | For Darwin we do not want explicit instantiations to be | |
324f9dfb | 1772 | linkonce. */ |
b385c841 JM |
1773 | |
1774 | void | |
848eed92 | 1775 | maybe_make_one_only (tree decl) |
b385c841 | 1776 | { |
b78121f6 JM |
1777 | /* We used to say that this was not necessary on targets that support weak |
1778 | symbols, because the implicit instantiations will defer to the explicit | |
1779 | one. However, that's not actually the case in SVR4; a strong definition | |
1780 | after a weak one is an error. Also, not making explicit | |
1781 | instantiations one_only means that we can end up with two copies of | |
a1c65f9f | 1782 | some template instantiations. */ |
90ecce3e | 1783 | if (! flag_weak) |
b385c841 JM |
1784 | return; |
1785 | ||
11bb4b27 | 1786 | /* We can't set DECL_COMDAT on functions, or cp_finish_file will think |
ea735e02 JM |
1787 | we can get away with not emitting them if they aren't used. We need |
1788 | to for variables so that cp_finish_decl will update their linkage, | |
1789 | because their DECL_INITIAL may not have been set properly yet. */ | |
b385c841 | 1790 | |
0524c91d | 1791 | if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC |
4746cf84 MA |
1792 | || (! DECL_EXPLICIT_INSTANTIATION (decl) |
1793 | && ! DECL_TEMPLATE_SPECIALIZATION (decl))) | |
5cc90635 | 1794 | { |
fc26fae3 | 1795 | make_decl_one_only (decl, cxx_comdat_group (decl)); |
4746cf84 | 1796 | |
5a6ccc94 | 1797 | if (VAR_P (decl)) |
4746cf84 | 1798 | { |
9041d2e6 | 1799 | varpool_node *node = varpool_node::get_create (decl); |
4746cf84 MA |
1800 | DECL_COMDAT (decl) = 1; |
1801 | /* Mark it needed so we don't forget to emit it. */ | |
67348ccc | 1802 | node->forced_by_abi = true; |
66058468 | 1803 | TREE_USED (decl) = 1; |
4746cf84 | 1804 | } |
5cc90635 | 1805 | } |
b385c841 JM |
1806 | } |
1807 | ||
d6dcdbd5 JM |
1808 | /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage. |
1809 | This predicate will give the right answer during parsing of the | |
1810 | function, which other tests may not. */ | |
ecc607fc JM |
1811 | |
1812 | bool | |
d6dcdbd5 | 1813 | vague_linkage_p (tree decl) |
ecc607fc | 1814 | { |
efb5e817 JM |
1815 | if (!TREE_PUBLIC (decl)) |
1816 | { | |
effdaefe JM |
1817 | /* maybe_thunk_body clears TREE_PUBLIC on the maybe-in-charge 'tor |
1818 | variants, check one of the "clones" for the real linkage. */ | |
1819 | if ((DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl) | |
1820 | || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)) | |
1821 | && DECL_CHAIN (decl) | |
1822 | && DECL_CLONED_FUNCTION (DECL_CHAIN (decl))) | |
1823 | return vague_linkage_p (DECL_CHAIN (decl)); | |
1824 | ||
efb5e817 JM |
1825 | gcc_checking_assert (!DECL_COMDAT (decl)); |
1826 | return false; | |
1827 | } | |
ecc607fc JM |
1828 | /* Unfortunately, import_export_decl has not always been called |
1829 | before the function is processed, so we cannot simply check | |
1830 | DECL_COMDAT. */ | |
c4fa3572 | 1831 | if (DECL_COMDAT (decl) |
efb5e817 JM |
1832 | || (TREE_CODE (decl) == FUNCTION_DECL |
1833 | && DECL_DECLARED_INLINE_P (decl)) | |
1834 | || (DECL_LANG_SPECIFIC (decl) | |
14a2c9aa JM |
1835 | && DECL_TEMPLATE_INSTANTIATION (decl)) |
1836 | || (VAR_P (decl) && DECL_INLINE_VAR_P (decl))) | |
c4fa3572 JM |
1837 | return true; |
1838 | else if (DECL_FUNCTION_SCOPE_P (decl)) | |
1839 | /* A local static in an inline effectively has vague linkage. */ | |
1840 | return (TREE_STATIC (decl) | |
1841 | && vague_linkage_p (DECL_CONTEXT (decl))); | |
1842 | else | |
1843 | return false; | |
ecc607fc JM |
1844 | } |
1845 | ||
7e776093 JM |
1846 | /* Determine whether or not we want to specifically import or export CTYPE, |
1847 | using various heuristics. */ | |
67f7c391 | 1848 | |
27d26ee7 | 1849 | static void |
848eed92 | 1850 | import_export_class (tree ctype) |
8d08fdba | 1851 | { |
7e776093 JM |
1852 | /* -1 for imported, 1 for exported. */ |
1853 | int import_export = 0; | |
1854 | ||
27d26ee7 MM |
1855 | /* It only makes sense to call this function at EOF. The reason is |
1856 | that this function looks at whether or not the first non-inline | |
1857 | non-abstract virtual member function has been defined in this | |
1858 | translation unit. But, we can't possibly know that until we've | |
1859 | seen the entire translation unit. */ | |
50bc768d | 1860 | gcc_assert (at_eof); |
27d26ee7 | 1861 | |
7e776093 JM |
1862 | if (CLASSTYPE_INTERFACE_KNOWN (ctype)) |
1863 | return; | |
1864 | ||
15072eb1 | 1865 | /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface, |
e9659ab0 JM |
1866 | we will have CLASSTYPE_INTERFACE_ONLY set but not |
1867 | CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this | |
1868 | heuristic because someone will supply a #pragma implementation | |
1869 | elsewhere, and deducing it here would produce a conflict. */ | |
1870 | if (CLASSTYPE_INTERFACE_ONLY (ctype)) | |
1871 | return; | |
1872 | ||
91d231cb | 1873 | if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype))) |
501990bb | 1874 | import_export = -1; |
91d231cb | 1875 | else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype))) |
501990bb | 1876 | import_export = 1; |
4684cd27 MM |
1877 | else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype) |
1878 | && !flag_implicit_templates) | |
1879 | /* For a template class, without -fimplicit-templates, check the | |
1880 | repository. If the virtual table is assigned to this | |
1881 | translation unit, then export the class; otherwise, import | |
1882 | it. */ | |
1883 | import_export = repo_export_class_p (ctype) ? 1 : -1; | |
1884 | else if (TYPE_POLYMORPHIC_P (ctype)) | |
1885 | { | |
1886 | /* The ABI specifies that the virtual table and associated | |
1887 | information are emitted with the key method, if any. */ | |
9aad8f83 | 1888 | tree method = CLASSTYPE_KEY_METHOD (ctype); |
30bf22a0 MS |
1889 | /* If weak symbol support is not available, then we must be |
1890 | careful not to emit the vtable when the key function is | |
1891 | inline. An inline function can be defined in multiple | |
1892 | translation units. If we were to emit the vtable in each | |
1893 | translation unit containing a definition, we would get | |
1894 | multiple definition errors at link-time. */ | |
1895 | if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method))) | |
6db20143 | 1896 | import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1); |
51c184be | 1897 | } |
ad236eab | 1898 | |
15072eb1 ZW |
1899 | /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing |
1900 | a definition anywhere else. */ | |
1901 | if (MULTIPLE_SYMBOL_SPACES && import_export == -1) | |
ad236eab | 1902 | import_export = 0; |
7e776093 | 1903 | |
3b426391 | 1904 | /* Allow back ends the chance to overrule the decision. */ |
d59c7b4b NC |
1905 | if (targetm.cxx.import_export_class) |
1906 | import_export = targetm.cxx.import_export_class (ctype, import_export); | |
1907 | ||
7e776093 JM |
1908 | if (import_export) |
1909 | { | |
1910 | SET_CLASSTYPE_INTERFACE_KNOWN (ctype); | |
7e776093 JM |
1911 | CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0); |
1912 | } | |
67f7c391 | 1913 | } |
a1dd0d36 | 1914 | |
c4d2313d JH |
1915 | /* Return true if VAR has already been provided to the back end; in that |
1916 | case VAR should not be modified further by the front end. */ | |
1917 | static bool | |
1918 | var_finalized_p (tree var) | |
1919 | { | |
9041d2e6 | 1920 | return varpool_node::get_create (var)->definition; |
4684cd27 MM |
1921 | } |
1922 | ||
1923 | /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason, | |
1924 | must be emitted in this translation unit. Mark it as such. */ | |
1925 | ||
1926 | void | |
1927 | mark_needed (tree decl) | |
1928 | { | |
7ca8f486 | 1929 | TREE_USED (decl) = 1; |
edb983b2 JH |
1930 | if (TREE_CODE (decl) == FUNCTION_DECL) |
1931 | { | |
1932 | /* Extern inline functions don't become needed when referenced. | |
1933 | If we know a method will be emitted in other TU and no new | |
1934 | functions can be marked reachable, just use the external | |
1935 | definition. */ | |
d52f5295 | 1936 | struct cgraph_node *node = cgraph_node::get_create (decl); |
67348ccc | 1937 | node->forced_by_abi = true; |
d5d0ed2d JM |
1938 | |
1939 | /* #pragma interface and -frepo code can call mark_needed for | |
1940 | maybe-in-charge 'tors; mark the clones as well. */ | |
1941 | tree clone; | |
1942 | FOR_EACH_CLONE (clone, decl) | |
1943 | mark_needed (clone); | |
edb983b2 | 1944 | } |
56a6f1d3 | 1945 | else if (VAR_P (decl)) |
edb983b2 | 1946 | { |
9041d2e6 | 1947 | varpool_node *node = varpool_node::get_create (decl); |
edb983b2 JH |
1948 | /* C++ frontend use mark_decl_references to force COMDAT variables |
1949 | to be output that might appear dead otherwise. */ | |
67348ccc | 1950 | node->forced_by_abi = true; |
edb983b2 | 1951 | } |
4684cd27 MM |
1952 | } |
1953 | ||
1954 | /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function | |
1955 | returns true if a definition of this entity should be provided in | |
1956 | this object file. Callers use this function to determine whether | |
1957 | or not to let the back end know that a definition of DECL is | |
1958 | available in this translation unit. */ | |
1959 | ||
1960 | bool | |
1961 | decl_needed_p (tree decl) | |
1962 | { | |
cb6da767 | 1963 | gcc_assert (VAR_OR_FUNCTION_DECL_P (decl)); |
4684cd27 MM |
1964 | /* This function should only be called at the end of the translation |
1965 | unit. We cannot be sure of whether or not something will be | |
1966 | COMDAT until that point. */ | |
50bc768d | 1967 | gcc_assert (at_eof); |
4684cd27 | 1968 | |
2e118f3c | 1969 | /* All entities with external linkage that are not COMDAT/EXTERN should be |
4684cd27 | 1970 | emitted; they may be referred to from other object files. */ |
2e118f3c | 1971 | if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl)) |
4684cd27 | 1972 | return true; |
fe2978fb MM |
1973 | /* Functions marked "dllexport" must be emitted so that they are |
1974 | visible to other DLLs. */ | |
47ea1edf DK |
1975 | if (flag_keep_inline_dllexport |
1976 | && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl))) | |
fe2978fb | 1977 | return true; |
2e118f3c JH |
1978 | |
1979 | /* When not optimizing, do not bother to produce definitions for extern | |
1980 | symbols. */ | |
1981 | if (DECL_REALLY_EXTERN (decl) | |
1982 | && ((TREE_CODE (decl) != FUNCTION_DECL | |
1983 | && !optimize) | |
1984 | || (TREE_CODE (decl) == FUNCTION_DECL | |
1985 | && !opt_for_fn (decl, optimize))) | |
1986 | && !lookup_attribute ("always_inline", decl)) | |
1987 | return false; | |
1988 | ||
1989 | /* If this entity was used, let the back end see it; it will decide | |
1990 | whether or not to emit it into the object file. */ | |
1991 | if (TREE_USED (decl)) | |
1992 | return true; | |
27093ee4 JM |
1993 | /* Virtual functions might be needed for devirtualization. */ |
1994 | if (flag_devirtualize | |
1995 | && TREE_CODE (decl) == FUNCTION_DECL | |
1996 | && DECL_VIRTUAL_P (decl)) | |
1997 | return true; | |
4684cd27 MM |
1998 | /* Otherwise, DECL does not need to be emitted -- yet. A subsequent |
1999 | reference to DECL might cause it to be emitted later. */ | |
2000 | return false; | |
c4d2313d JH |
2001 | } |
2002 | ||
548502d3 | 2003 | /* If necessary, write out the vtables for the dynamic class CTYPE. |
848eed92 | 2004 | Returns true if any vtables were emitted. */ |
548502d3 | 2005 | |
848eed92 | 2006 | static bool |
548502d3 | 2007 | maybe_emit_vtables (tree ctype) |
d18c083e | 2008 | { |
548502d3 MM |
2009 | tree vtbl; |
2010 | tree primary_vtbl; | |
4684cd27 | 2011 | int needed = 0; |
2c8326a5 | 2012 | varpool_node *current = NULL, *last = NULL; |
548502d3 MM |
2013 | |
2014 | /* If the vtables for this class have already been emitted there is | |
2015 | nothing more to do. */ | |
2016 | primary_vtbl = CLASSTYPE_VTABLES (ctype); | |
c4d2313d | 2017 | if (var_finalized_p (primary_vtbl)) |
848eed92 | 2018 | return false; |
548502d3 MM |
2019 | /* Ignore dummy vtables made by get_vtable_decl. */ |
2020 | if (TREE_TYPE (primary_vtbl) == void_type_node) | |
848eed92 | 2021 | return false; |
548502d3 | 2022 | |
af287697 MM |
2023 | /* On some targets, we cannot determine the key method until the end |
2024 | of the translation unit -- which is when this function is | |
2025 | called. */ | |
2026 | if (!targetm.cxx.key_method_may_be_inline ()) | |
2027 | determine_key_method (ctype); | |
2028 | ||
548502d3 | 2029 | /* See if any of the vtables are needed. */ |
910ad8de | 2030 | for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl)) |
e9e4d4ee | 2031 | { |
4684cd27 MM |
2032 | import_export_decl (vtbl); |
2033 | if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl)) | |
2034 | needed = 1; | |
e9e4d4ee | 2035 | } |
4684cd27 | 2036 | if (!needed) |
548502d3 MM |
2037 | { |
2038 | /* If the references to this class' vtables are optimized away, | |
2039 | still emit the appropriate debugging information. See | |
2040 | dfs_debug_mark. */ | |
c8094d83 | 2041 | if (DECL_COMDAT (primary_vtbl) |
548502d3 MM |
2042 | && CLASSTYPE_DEBUG_REQUESTED (ctype)) |
2043 | note_debug_info_needed (ctype); | |
848eed92 | 2044 | return false; |
548502d3 MM |
2045 | } |
2046 | ||
2047 | /* The ABI requires that we emit all of the vtables if we emit any | |
2048 | of them. */ | |
910ad8de | 2049 | for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl)) |
d18c083e | 2050 | { |
4684cd27 | 2051 | /* Mark entities references from the virtual table as used. */ |
548502d3 | 2052 | mark_vtable_entries (vtbl); |
8cd2462c | 2053 | |
548502d3 | 2054 | if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0) |
25ebb82a | 2055 | { |
9771b263 | 2056 | vec<tree, va_gc> *cleanups = NULL; |
b25dd954 JM |
2057 | tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups, |
2058 | LOOKUP_NORMAL); | |
c8094d83 | 2059 | |
25ebb82a | 2060 | /* It had better be all done at compile-time. */ |
b25dd954 | 2061 | gcc_assert (!expr && !cleanups); |
25ebb82a | 2062 | } |
8d08fdba | 2063 | |
4684cd27 MM |
2064 | /* Write it out. */ |
2065 | DECL_EXTERNAL (vtbl) = 0; | |
0e6df31e | 2066 | rest_of_decl_compilation (vtbl, 1, 1); |
a1dd0d36 | 2067 | |
84df082b MM |
2068 | /* Because we're only doing syntax-checking, we'll never end up |
2069 | actually marking the variable as written. */ | |
2070 | if (flag_syntax_only) | |
548502d3 | 2071 | TREE_ASM_WRITTEN (vtbl) = 1; |
b7262828 | 2072 | else if (DECL_ONE_ONLY (vtbl)) |
9f90e80a | 2073 | { |
9041d2e6 | 2074 | current = varpool_node::get_create (vtbl); |
9f90e80a | 2075 | if (last) |
d52f5295 | 2076 | current->add_to_same_comdat_group (last); |
9f90e80a | 2077 | last = current; |
9f90e80a | 2078 | } |
8d08fdba | 2079 | } |
6db20143 | 2080 | |
548502d3 MM |
2081 | /* Since we're writing out the vtable here, also write the debug |
2082 | info. */ | |
2083 | note_debug_info_needed (ctype); | |
b7484fbe | 2084 | |
848eed92 | 2085 | return true; |
b7484fbe MS |
2086 | } |
2087 | ||
b9e75696 JM |
2088 | /* A special return value from type_visibility meaning internal |
2089 | linkage. */ | |
2090 | ||
b70f0f48 | 2091 | enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 }; |
b9e75696 JM |
2092 | |
2093 | /* walk_tree helper function for type_visibility. */ | |
2094 | ||
2095 | static tree | |
2096 | min_vis_r (tree *tp, int *walk_subtrees, void *data) | |
2097 | { | |
2098 | int *vis_p = (int *)data; | |
2099 | if (! TYPE_P (*tp)) | |
2100 | { | |
2101 | *walk_subtrees = 0; | |
2102 | } | |
73243d63 | 2103 | else if (OVERLOAD_TYPE_P (*tp) |
efa6d698 | 2104 | && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp))) |
b9e75696 | 2105 | { |
efa6d698 JM |
2106 | *vis_p = VISIBILITY_ANON; |
2107 | return *tp; | |
b9e75696 | 2108 | } |
efa6d698 JM |
2109 | else if (CLASS_TYPE_P (*tp) |
2110 | && CLASSTYPE_VISIBILITY (*tp) > *vis_p) | |
2111 | *vis_p = CLASSTYPE_VISIBILITY (*tp); | |
b9e75696 JM |
2112 | return NULL; |
2113 | } | |
2114 | ||
2115 | /* Returns the visibility of TYPE, which is the minimum visibility of its | |
2116 | component types. */ | |
2117 | ||
2118 | static int | |
2119 | type_visibility (tree type) | |
2120 | { | |
2121 | int vis = VISIBILITY_DEFAULT; | |
14588106 | 2122 | cp_walk_tree_without_duplicates (&type, min_vis_r, &vis); |
b9e75696 JM |
2123 | return vis; |
2124 | } | |
2125 | ||
b70f0f48 | 2126 | /* Limit the visibility of DECL to VISIBILITY, if not explicitly |
6d87092d JM |
2127 | specified (or if VISIBILITY is static). If TMPL is true, this |
2128 | constraint is for a template argument, and takes precedence | |
2129 | over explicitly-specified visibility on the template. */ | |
b9e75696 | 2130 | |
6d87092d JM |
2131 | static void |
2132 | constrain_visibility (tree decl, int visibility, bool tmpl) | |
b9e75696 | 2133 | { |
b70f0f48 | 2134 | if (visibility == VISIBILITY_ANON) |
b9e75696 | 2135 | { |
b70f0f48 JM |
2136 | /* extern "C" declarations aren't affected by the anonymous |
2137 | namespace. */ | |
2138 | if (!DECL_EXTERN_C_P (decl)) | |
2139 | { | |
2140 | TREE_PUBLIC (decl) = 0; | |
fcc993f6 JH |
2141 | DECL_WEAK (decl) = 0; |
2142 | DECL_COMMON (decl) = 0; | |
2b8f9c8f | 2143 | DECL_COMDAT (decl) = false; |
56a6f1d3 | 2144 | if (VAR_OR_FUNCTION_DECL_P (decl)) |
aede2c10 | 2145 | { |
d52f5295 | 2146 | struct symtab_node *snode = symtab_node::get (decl); |
aede2c10 JH |
2147 | |
2148 | if (snode) | |
2149 | snode->set_comdat_group (NULL); | |
2150 | } | |
b70f0f48 JM |
2151 | DECL_INTERFACE_KNOWN (decl) = 1; |
2152 | if (DECL_LANG_SPECIFIC (decl)) | |
2153 | DECL_NOT_REALLY_EXTERN (decl) = 1; | |
2154 | } | |
b9e75696 | 2155 | } |
b70f0f48 | 2156 | else if (visibility > DECL_VISIBILITY (decl) |
6d87092d | 2157 | && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl))) |
b9e75696 | 2158 | { |
32e8bb8e | 2159 | DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility; |
fc970532 JM |
2160 | /* This visibility was not specified. */ |
2161 | DECL_VISIBILITY_SPECIFIED (decl) = false; | |
b9e75696 | 2162 | } |
b9e75696 JM |
2163 | } |
2164 | ||
e7d35a88 | 2165 | /* Constrain the visibility of DECL based on the visibility of its template |
b9e75696 JM |
2166 | arguments. */ |
2167 | ||
2168 | static void | |
2169 | constrain_visibility_for_template (tree decl, tree targs) | |
2170 | { | |
2171 | /* If this is a template instantiation, check the innermost | |
2172 | template args for visibility constraints. The outer template | |
2173 | args are covered by the class check. */ | |
2174 | tree args = INNERMOST_TEMPLATE_ARGS (targs); | |
2175 | int i; | |
2176 | for (i = TREE_VEC_LENGTH (args); i > 0; --i) | |
2177 | { | |
2178 | int vis = 0; | |
2179 | ||
2180 | tree arg = TREE_VEC_ELT (args, i-1); | |
2181 | if (TYPE_P (arg)) | |
2182 | vis = type_visibility (arg); | |
2b8f9c8f | 2183 | else |
b9e75696 | 2184 | { |
2b8f9c8f JM |
2185 | if (REFERENCE_REF_P (arg)) |
2186 | arg = TREE_OPERAND (arg, 0); | |
2187 | if (TREE_TYPE (arg)) | |
2188 | STRIP_NOPS (arg); | |
b9e75696 JM |
2189 | if (TREE_CODE (arg) == ADDR_EXPR) |
2190 | arg = TREE_OPERAND (arg, 0); | |
cb6da767 | 2191 | if (VAR_OR_FUNCTION_DECL_P (arg)) |
b9e75696 JM |
2192 | { |
2193 | if (! TREE_PUBLIC (arg)) | |
b70f0f48 | 2194 | vis = VISIBILITY_ANON; |
b9e75696 JM |
2195 | else |
2196 | vis = DECL_VISIBILITY (arg); | |
2197 | } | |
2198 | } | |
2199 | if (vis) | |
6d87092d | 2200 | constrain_visibility (decl, vis, true); |
b9e75696 JM |
2201 | } |
2202 | } | |
2203 | ||
b2ca3702 | 2204 | /* Like c_determine_visibility, but with additional C++-specific |
0ed5edac JM |
2205 | behavior. |
2206 | ||
2207 | Function-scope entities can rely on the function's visibility because | |
2208 | it is set in start_preparsed_function. | |
2209 | ||
2210 | Class-scope entities cannot rely on the class's visibility until the end | |
2211 | of the enclosing class definition. | |
2212 | ||
2213 | Note that because namespaces have multiple independent definitions, | |
2214 | namespace visibility is handled elsewhere using the #pragma visibility | |
b9e75696 JM |
2215 | machinery rather than by decorating the namespace declaration. |
2216 | ||
2217 | The goal is for constraints from the type to give a diagnostic, and | |
2218 | other constraints to be applied silently. */ | |
73a8adb6 MM |
2219 | |
2220 | void | |
2221 | determine_visibility (tree decl) | |
2222 | { | |
b9e75696 | 2223 | /* Remember that all decls get VISIBILITY_DEFAULT when built. */ |
73a8adb6 | 2224 | |
0ed5edac JM |
2225 | /* Only relevant for names with external linkage. */ |
2226 | if (!TREE_PUBLIC (decl)) | |
2227 | return; | |
2228 | ||
73a8adb6 MM |
2229 | /* Cloned constructors and destructors get the same visibility as |
2230 | the underlying function. That should be set up in | |
2231 | maybe_clone_body. */ | |
50bc768d | 2232 | gcc_assert (!DECL_CLONED_FUNCTION_P (decl)); |
b2ca3702 | 2233 | |
b9a161ff NS |
2234 | bool orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl); |
2235 | enum symbol_visibility orig_visibility = DECL_VISIBILITY (decl); | |
09812622 | 2236 | |
b9a161ff NS |
2237 | /* The decl may be a template instantiation, which could influence |
2238 | visibilty. */ | |
2239 | tree template_decl = NULL_TREE; | |
b9e75696 JM |
2240 | if (TREE_CODE (decl) == TYPE_DECL) |
2241 | { | |
2242 | if (CLASS_TYPE_P (TREE_TYPE (decl))) | |
b9a161ff NS |
2243 | { |
2244 | if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl))) | |
2245 | template_decl = decl; | |
2246 | } | |
b9e75696 | 2247 | else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl))) |
b9a161ff | 2248 | template_decl = decl; |
b9e75696 | 2249 | } |
b9a161ff NS |
2250 | else if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl)) |
2251 | template_decl = decl; | |
b9e75696 | 2252 | |
b2ca3702 MM |
2253 | /* If DECL is a member of a class, visibility specifiers on the |
2254 | class can influence the visibility of the DECL. */ | |
b9a161ff | 2255 | tree class_type = NULL_TREE; |
73a8adb6 MM |
2256 | if (DECL_CLASS_SCOPE_P (decl)) |
2257 | class_type = DECL_CONTEXT (decl); | |
73a8adb6 MM |
2258 | else |
2259 | { | |
b9e75696 JM |
2260 | /* Not a class member. */ |
2261 | ||
73a8adb6 MM |
2262 | /* Virtual tables have DECL_CONTEXT set to their associated class, |
2263 | so they are automatically handled above. */ | |
5a6ccc94 | 2264 | gcc_assert (!VAR_P (decl) |
50bc768d | 2265 | || !DECL_VTABLE_OR_VTT_P (decl)); |
0ed5edac | 2266 | |
7c0de6a5 | 2267 | if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl)) |
0ed5edac | 2268 | { |
b9e75696 | 2269 | /* Local statics and classes get the visibility of their |
7c0de6a5 GK |
2270 | containing function by default, except that |
2271 | -fvisibility-inlines-hidden doesn't affect them. */ | |
0ed5edac | 2272 | tree fn = DECL_CONTEXT (decl); |
fb9120e3 | 2273 | if (DECL_VISIBILITY_SPECIFIED (fn)) |
7c0de6a5 GK |
2274 | { |
2275 | DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn); | |
2276 | DECL_VISIBILITY_SPECIFIED (decl) = | |
2277 | DECL_VISIBILITY_SPECIFIED (fn); | |
2278 | } | |
2279 | else | |
fb9120e3 RAV |
2280 | { |
2281 | if (DECL_CLASS_SCOPE_P (fn)) | |
2282 | determine_visibility_from_class (decl, DECL_CONTEXT (fn)); | |
2283 | else if (determine_hidden_inline (fn)) | |
2284 | { | |
2285 | DECL_VISIBILITY (decl) = default_visibility; | |
2286 | DECL_VISIBILITY_SPECIFIED (decl) = | |
2287 | visibility_options.inpragma; | |
2288 | } | |
2289 | else | |
2290 | { | |
2291 | DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn); | |
2292 | DECL_VISIBILITY_SPECIFIED (decl) = | |
2293 | DECL_VISIBILITY_SPECIFIED (fn); | |
2294 | } | |
2295 | } | |
0ed5edac | 2296 | |
b9e75696 | 2297 | /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set, |
b9a161ff NS |
2298 | but have no TEMPLATE_INFO. Their containing template |
2299 | function does, and the local class could be constrained | |
2300 | by that. */ | |
2301 | if (template_decl) | |
2302 | template_decl = fn; | |
b9e75696 | 2303 | } |
5a6ccc94 | 2304 | else if (VAR_P (decl) && DECL_TINFO_P (decl) |
ddbfd28d GK |
2305 | && flag_visibility_ms_compat) |
2306 | { | |
2307 | /* Under -fvisibility-ms-compat, types are visible by default, | |
2308 | even though their contents aren't. */ | |
2309 | tree underlying_type = TREE_TYPE (DECL_NAME (decl)); | |
2310 | int underlying_vis = type_visibility (underlying_type); | |
2311 | if (underlying_vis == VISIBILITY_ANON | |
a2b63a20 KT |
2312 | || (CLASS_TYPE_P (underlying_type) |
2313 | && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type))) | |
6d87092d | 2314 | constrain_visibility (decl, underlying_vis, false); |
ddbfd28d GK |
2315 | else |
2316 | DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT; | |
2317 | } | |
5a6ccc94 | 2318 | else if (VAR_P (decl) && DECL_TINFO_P (decl)) |
b9e75696 JM |
2319 | { |
2320 | /* tinfo visibility is based on the type it's for. */ | |
2321 | constrain_visibility | |
6d87092d | 2322 | (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false); |
711b2998 JB |
2323 | |
2324 | /* Give the target a chance to override the visibility associated | |
2325 | with DECL. */ | |
2326 | if (TREE_PUBLIC (decl) | |
2327 | && !DECL_REALLY_EXTERN (decl) | |
2328 | && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl))) | |
2329 | && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl)))) | |
2330 | targetm.cxx.determine_class_data_visibility (decl); | |
b9e75696 | 2331 | } |
b9a161ff | 2332 | else if (template_decl) |
b9e75696 JM |
2333 | /* Template instantiations and specializations get visibility based |
2334 | on their template unless they override it with an attribute. */; | |
2335 | else if (! DECL_VISIBILITY_SPECIFIED (decl)) | |
2336 | { | |
fb9120e3 RAV |
2337 | if (determine_hidden_inline (decl)) |
2338 | DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN; | |
2339 | else | |
2340 | { | |
2341 | /* Set default visibility to whatever the user supplied with | |
2342 | #pragma GCC visibility or a namespace visibility attribute. */ | |
2343 | DECL_VISIBILITY (decl) = default_visibility; | |
2344 | DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma; | |
2345 | } | |
b9e75696 | 2346 | } |
73a8adb6 MM |
2347 | } |
2348 | ||
b9a161ff | 2349 | if (template_decl) |
73a8adb6 | 2350 | { |
b70f0f48 JM |
2351 | /* If the specialization doesn't specify visibility, use the |
2352 | visibility from the template. */ | |
b9a161ff | 2353 | tree tinfo = get_template_info (template_decl); |
b9e75696 | 2354 | tree args = TI_ARGS (tinfo); |
22197246 JM |
2355 | tree attribs = (TREE_CODE (decl) == TYPE_DECL |
2356 | ? TYPE_ATTRIBUTES (TREE_TYPE (decl)) | |
2357 | : DECL_ATTRIBUTES (decl)); | |
653109bd | 2358 | |
b5053024 | 2359 | if (args != error_mark_node) |
b9e75696 | 2360 | { |
653109bd LM |
2361 | tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo)); |
2362 | ||
2363 | if (!DECL_VISIBILITY_SPECIFIED (decl)) | |
2364 | { | |
fb9120e3 RAV |
2365 | if (!DECL_VISIBILITY_SPECIFIED (pattern) |
2366 | && determine_hidden_inline (decl)) | |
2367 | DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN; | |
2368 | else | |
2369 | { | |
2370 | DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern); | |
2371 | DECL_VISIBILITY_SPECIFIED (decl) | |
2372 | = DECL_VISIBILITY_SPECIFIED (pattern); | |
2373 | } | |
653109bd | 2374 | } |
b9e75696 | 2375 | |
b5053024 JM |
2376 | if (args |
2377 | /* Template argument visibility outweighs #pragma or namespace | |
2378 | visibility, but not an explicit attribute. */ | |
2379 | && !lookup_attribute ("visibility", attribs)) | |
2380 | { | |
2381 | int depth = TMPL_ARGS_DEPTH (args); | |
b5053024 JM |
2382 | if (DECL_VISIBILITY_SPECIFIED (decl)) |
2383 | { | |
2384 | /* A class template member with explicit visibility | |
2385 | overrides the class visibility, so we need to apply | |
2386 | all the levels of template args directly. */ | |
2387 | int i; | |
2388 | for (i = 1; i <= depth; ++i) | |
2389 | { | |
2390 | tree lev = TMPL_ARGS_LEVEL (args, i); | |
2391 | constrain_visibility_for_template (decl, lev); | |
2392 | } | |
2393 | } | |
8f1352c4 | 2394 | else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))) |
b5053024 JM |
2395 | /* Limit visibility based on its template arguments. */ |
2396 | constrain_visibility_for_template (decl, args); | |
2397 | } | |
653109bd | 2398 | } |
3db45ab5 | 2399 | } |
b70f0f48 | 2400 | |
b9e75696 JM |
2401 | if (class_type) |
2402 | determine_visibility_from_class (decl, class_type); | |
2403 | ||
b9e75696 JM |
2404 | if (decl_anon_ns_mem_p (decl)) |
2405 | /* Names in an anonymous namespace get internal linkage. | |
2406 | This might change once we implement export. */ | |
6d87092d | 2407 | constrain_visibility (decl, VISIBILITY_ANON, false); |
bd741f34 JM |
2408 | else if (TREE_CODE (decl) != TYPE_DECL) |
2409 | { | |
2410 | /* Propagate anonymity from type to decl. */ | |
2411 | int tvis = type_visibility (TREE_TYPE (decl)); | |
c96f0001 GK |
2412 | if (tvis == VISIBILITY_ANON |
2413 | || ! DECL_VISIBILITY_SPECIFIED (decl)) | |
6d87092d | 2414 | constrain_visibility (decl, tvis, false); |
bd741f34 | 2415 | } |
ecc607fc JM |
2416 | else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true)) |
2417 | /* DR 757: A type without linkage shall not be used as the type of a | |
2418 | variable or function with linkage, unless | |
2419 | o the variable or function has extern "C" linkage (7.5 [dcl.link]), or | |
2420 | o the variable or function is not used (3.2 [basic.def.odr]) or is | |
2421 | defined in the same translation unit. | |
2422 | ||
2423 | Since non-extern "C" decls need to be defined in the same | |
2424 | translation unit, we can make the type internal. */ | |
6d87092d | 2425 | constrain_visibility (decl, VISIBILITY_ANON, false); |
09812622 JJ |
2426 | |
2427 | /* If visibility changed and DECL already has DECL_RTL, ensure | |
2428 | symbol flags are updated. */ | |
2429 | if ((DECL_VISIBILITY (decl) != orig_visibility | |
2430 | || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified) | |
5a6ccc94 | 2431 | && ((VAR_P (decl) && TREE_STATIC (decl)) |
09812622 JJ |
2432 | || TREE_CODE (decl) == FUNCTION_DECL) |
2433 | && DECL_RTL_SET_P (decl)) | |
2434 | make_decl_rtl (decl); | |
0ed5edac JM |
2435 | } |
2436 | ||
b9e75696 JM |
2437 | /* By default, static data members and function members receive |
2438 | the visibility of their containing class. */ | |
2439 | ||
0ed5edac JM |
2440 | static void |
2441 | determine_visibility_from_class (tree decl, tree class_type) | |
2442 | { | |
3a687f8b MM |
2443 | if (DECL_VISIBILITY_SPECIFIED (decl)) |
2444 | return; | |
2445 | ||
fb9120e3 | 2446 | if (determine_hidden_inline (decl)) |
b9e75696 | 2447 | DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN; |
3a687f8b | 2448 | else |
b70f0f48 JM |
2449 | { |
2450 | /* Default to the class visibility. */ | |
2451 | DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type); | |
2452 | DECL_VISIBILITY_SPECIFIED (decl) | |
2453 | = CLASSTYPE_VISIBILITY_SPECIFIED (class_type); | |
2454 | } | |
b9e75696 JM |
2455 | |
2456 | /* Give the target a chance to override the visibility associated | |
2457 | with DECL. */ | |
5a6ccc94 | 2458 | if (VAR_P (decl) |
b9e75696 JM |
2459 | && (DECL_TINFO_P (decl) |
2460 | || (DECL_VTABLE_OR_VTT_P (decl) | |
2461 | /* Construction virtual tables are not exported because | |
2462 | they cannot be referred to from other object files; | |
2463 | their name is not standardized by the ABI. */ | |
2464 | && !DECL_CONSTRUCTION_VTABLE_P (decl))) | |
2465 | && TREE_PUBLIC (decl) | |
2466 | && !DECL_REALLY_EXTERN (decl) | |
b70f0f48 | 2467 | && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type)) |
b9e75696 JM |
2468 | targetm.cxx.determine_class_data_visibility (decl); |
2469 | } | |
2470 | ||
fb9120e3 RAV |
2471 | /* Returns true iff DECL is an inline that should get hidden visibility |
2472 | because of -fvisibility-inlines-hidden. */ | |
2473 | ||
2474 | static bool | |
2475 | determine_hidden_inline (tree decl) | |
2476 | { | |
2477 | return (visibility_options.inlines_hidden | |
2478 | /* Don't do this for inline templates; specializations might not be | |
2479 | inline, and we don't want them to inherit the hidden | |
2480 | visibility. We'll set it here for all inline instantiations. */ | |
2481 | && !processing_template_decl | |
2482 | && TREE_CODE (decl) == FUNCTION_DECL | |
2483 | && DECL_DECLARED_INLINE_P (decl) | |
2484 | && (! DECL_LANG_SPECIFIC (decl) | |
2485 | || ! DECL_EXPLICIT_INSTANTIATION (decl))); | |
2486 | } | |
2487 | ||
b9e75696 JM |
2488 | /* Constrain the visibility of a class TYPE based on the visibility of its |
2489 | field types. Warn if any fields require lesser visibility. */ | |
2490 | ||
2491 | void | |
2492 | constrain_class_visibility (tree type) | |
2493 | { | |
b70f0f48 | 2494 | tree binfo; |
b9e75696 JM |
2495 | tree t; |
2496 | int i; | |
2497 | ||
b70f0f48 JM |
2498 | int vis = type_visibility (type); |
2499 | ||
bd741f34 JM |
2500 | if (vis == VISIBILITY_ANON |
2501 | || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type))) | |
b70f0f48 JM |
2502 | return; |
2503 | ||
2504 | /* Don't warn about visibility if the class has explicit visibility. */ | |
2505 | if (CLASSTYPE_VISIBILITY_SPECIFIED (type)) | |
2506 | vis = VISIBILITY_INTERNAL; | |
2507 | ||
910ad8de | 2508 | for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t)) |
b70f0f48 JM |
2509 | if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node) |
2510 | { | |
ba992967 | 2511 | tree ftype = strip_pointer_or_array_types (TREE_TYPE (t)); |
bd741f34 | 2512 | int subvis = type_visibility (ftype); |
b70f0f48 JM |
2513 | |
2514 | if (subvis == VISIBILITY_ANON) | |
8fff2060 | 2515 | { |
273aa49e PC |
2516 | if (!in_main_input_context()) |
2517 | { | |
2518 | tree nlt = no_linkage_check (ftype, /*relaxed_p=*/false); | |
2519 | if (nlt) | |
2520 | { | |
2521 | if (same_type_p (TREE_TYPE (t), nlt)) | |
2522 | warning (OPT_Wsubobject_linkage, "\ | |
2523 | %qT has a field %qD whose type has no linkage", | |
2524 | type, t); | |
2525 | else | |
2526 | warning (OPT_Wsubobject_linkage, "\ | |
2527 | %qT has a field %qD whose type depends on the type %qT which has no linkage", | |
2528 | type, t, nlt); | |
2529 | } | |
2530 | else | |
2531 | warning (OPT_Wsubobject_linkage, "\ | |
b70f0f48 | 2532 | %qT has a field %qD whose type uses the anonymous namespace", |
273aa49e PC |
2533 | type, t); |
2534 | } | |
8fff2060 | 2535 | } |
9e1e64ec | 2536 | else if (MAYBE_CLASS_TYPE_P (ftype) |
bd741f34 | 2537 | && vis < VISIBILITY_HIDDEN |
b70f0f48 JM |
2538 | && subvis >= VISIBILITY_HIDDEN) |
2539 | warning (OPT_Wattributes, "\ | |
b9e75696 | 2540 | %qT declared with greater visibility than the type of its field %qD", |
b70f0f48 JM |
2541 | type, t); |
2542 | } | |
b9e75696 | 2543 | |
b70f0f48 | 2544 | binfo = TYPE_BINFO (type); |
b9e75696 | 2545 | for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i) |
b70f0f48 JM |
2546 | { |
2547 | int subvis = type_visibility (TREE_TYPE (t)); | |
2548 | ||
2549 | if (subvis == VISIBILITY_ANON) | |
8fff2060 | 2550 | { |
61172206 | 2551 | if (!in_main_input_context()) |
273aa49e PC |
2552 | { |
2553 | tree nlt = no_linkage_check (TREE_TYPE (t), /*relaxed_p=*/false); | |
2554 | if (nlt) | |
2555 | { | |
2556 | if (same_type_p (TREE_TYPE (t), nlt)) | |
2557 | warning (OPT_Wsubobject_linkage, "\ | |
2558 | %qT has a base %qT whose type has no linkage", | |
2559 | type, TREE_TYPE (t)); | |
2560 | else | |
2561 | warning (OPT_Wsubobject_linkage, "\ | |
2562 | %qT has a base %qT whose type depends on the type %qT which has no linkage", | |
2563 | type, TREE_TYPE (t), nlt); | |
2564 | } | |
2565 | else | |
2566 | warning (OPT_Wsubobject_linkage, "\ | |
b70f0f48 | 2567 | %qT has a base %qT whose type uses the anonymous namespace", |
273aa49e PC |
2568 | type, TREE_TYPE (t)); |
2569 | } | |
8fff2060 | 2570 | } |
b70f0f48 JM |
2571 | else if (vis < VISIBILITY_HIDDEN |
2572 | && subvis >= VISIBILITY_HIDDEN) | |
2573 | warning (OPT_Wattributes, "\ | |
b9e75696 | 2574 | %qT declared with greater visibility than its base %qT", |
b70f0f48 JM |
2575 | type, TREE_TYPE (t)); |
2576 | } | |
73a8adb6 MM |
2577 | } |
2578 | ||
944b63db JM |
2579 | /* Functions for adjusting the visibility of a tagged type and its nested |
2580 | types and declarations when it gets a name for linkage purposes from a | |
2581 | typedef. */ | |
2582 | ||
2583 | static void bt_reset_linkage_1 (binding_entry, void *); | |
2584 | static void bt_reset_linkage_2 (binding_entry, void *); | |
2585 | ||
2586 | /* First reset the visibility of all the types. */ | |
2587 | ||
2588 | static void | |
2589 | reset_type_linkage_1 (tree type) | |
2590 | { | |
2591 | set_linkage_according_to_type (type, TYPE_MAIN_DECL (type)); | |
2592 | if (CLASS_TYPE_P (type)) | |
2593 | binding_table_foreach (CLASSTYPE_NESTED_UTDS (type), | |
2594 | bt_reset_linkage_1, NULL); | |
2595 | } | |
2596 | static void | |
2597 | bt_reset_linkage_1 (binding_entry b, void */*data*/) | |
2598 | { | |
2599 | reset_type_linkage_1 (b->type); | |
2600 | } | |
2601 | ||
2602 | /* Then reset the visibility of any static data members or member | |
2603 | functions that use those types. */ | |
2604 | ||
2605 | static void | |
2606 | reset_decl_linkage (tree decl) | |
2607 | { | |
2608 | if (TREE_PUBLIC (decl)) | |
2609 | return; | |
2610 | if (DECL_CLONED_FUNCTION_P (decl)) | |
2611 | return; | |
2612 | TREE_PUBLIC (decl) = true; | |
2613 | DECL_INTERFACE_KNOWN (decl) = false; | |
2614 | determine_visibility (decl); | |
2615 | tentative_decl_linkage (decl); | |
2616 | } | |
2617 | static void | |
2618 | reset_type_linkage_2 (tree type) | |
2619 | { | |
2620 | if (CLASS_TYPE_P (type)) | |
2621 | { | |
2622 | if (tree vt = CLASSTYPE_VTABLES (type)) | |
2623 | { | |
2624 | tree name = mangle_vtbl_for_type (type); | |
2625 | DECL_NAME (vt) = name; | |
2626 | SET_DECL_ASSEMBLER_NAME (vt, name); | |
2627 | reset_decl_linkage (vt); | |
2628 | } | |
2629 | if (tree ti = CLASSTYPE_TYPEINFO_VAR (type)) | |
2630 | { | |
2631 | tree name = mangle_typeinfo_for_type (type); | |
2632 | DECL_NAME (ti) = name; | |
2633 | SET_DECL_ASSEMBLER_NAME (ti, name); | |
2634 | TREE_TYPE (name) = type; | |
2635 | reset_decl_linkage (ti); | |
2636 | } | |
2637 | for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m)) | |
50ccdf5b JM |
2638 | { |
2639 | tree mem = STRIP_TEMPLATE (m); | |
2640 | if (VAR_P (mem)) | |
2641 | reset_decl_linkage (mem); | |
2642 | } | |
944b63db | 2643 | for (tree m = TYPE_METHODS (type); m; m = DECL_CHAIN (m)) |
15ed2619 | 2644 | { |
50ccdf5b JM |
2645 | tree mem = STRIP_TEMPLATE (m); |
2646 | reset_decl_linkage (mem); | |
2647 | if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (mem)) | |
2648 | { | |
2649 | /* Also update its name, for cxx_dwarf_name. */ | |
2650 | DECL_NAME (mem) = TYPE_IDENTIFIER (type); | |
2651 | if (m != mem) | |
2652 | DECL_NAME (m) = TYPE_IDENTIFIER (type); | |
2653 | } | |
15ed2619 | 2654 | } |
944b63db JM |
2655 | binding_table_foreach (CLASSTYPE_NESTED_UTDS (type), |
2656 | bt_reset_linkage_2, NULL); | |
2657 | } | |
2658 | } | |
2659 | static void | |
2660 | bt_reset_linkage_2 (binding_entry b, void */*data*/) | |
2661 | { | |
2662 | reset_type_linkage_2 (b->type); | |
2663 | } | |
2664 | void | |
2665 | reset_type_linkage (tree type) | |
2666 | { | |
2667 | reset_type_linkage_1 (type); | |
2668 | reset_type_linkage_2 (type); | |
2669 | } | |
2670 | ||
2671 | /* Set up our initial idea of what the linkage of DECL should be. */ | |
2672 | ||
2673 | void | |
2674 | tentative_decl_linkage (tree decl) | |
2675 | { | |
2676 | if (DECL_INTERFACE_KNOWN (decl)) | |
2677 | /* We've already made a decision as to how this function will | |
2678 | be handled. */; | |
2679 | else if (vague_linkage_p (decl)) | |
2680 | { | |
7fce82f5 JM |
2681 | if (TREE_CODE (decl) == FUNCTION_DECL |
2682 | && decl_defined_p (decl)) | |
944b63db JM |
2683 | { |
2684 | DECL_EXTERNAL (decl) = 1; | |
2685 | DECL_NOT_REALLY_EXTERN (decl) = 1; | |
2686 | note_vague_linkage_fn (decl); | |
2687 | /* A non-template inline function with external linkage will | |
2688 | always be COMDAT. As we must eventually determine the | |
2689 | linkage of all functions, and as that causes writes to | |
2690 | the data mapped in from the PCH file, it's advantageous | |
2691 | to mark the functions at this point. */ | |
2692 | if (DECL_DECLARED_INLINE_P (decl) | |
2693 | && (!DECL_IMPLICIT_INSTANTIATION (decl) | |
2694 | || DECL_DEFAULTED_FN (decl))) | |
2695 | { | |
2696 | /* This function must have external linkage, as | |
2697 | otherwise DECL_INTERFACE_KNOWN would have been | |
2698 | set. */ | |
2699 | gcc_assert (TREE_PUBLIC (decl)); | |
2700 | comdat_linkage (decl); | |
2701 | DECL_INTERFACE_KNOWN (decl) = 1; | |
2702 | } | |
2703 | } | |
56a6f1d3 | 2704 | else if (VAR_P (decl)) |
7fce82f5 | 2705 | maybe_commonize_var (decl); |
944b63db JM |
2706 | } |
2707 | } | |
2708 | ||
4684cd27 MM |
2709 | /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage |
2710 | for DECL has not already been determined, do so now by setting | |
2711 | DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this | |
2712 | function is called entities with vague linkage whose definitions | |
2713 | are available must have TREE_PUBLIC set. | |
2714 | ||
2715 | If this function decides to place DECL in COMDAT, it will set | |
2716 | appropriate flags -- but will not clear DECL_EXTERNAL. It is up to | |
2717 | the caller to decide whether or not to clear DECL_EXTERNAL. Some | |
2718 | callers defer that decision until it is clear that DECL is actually | |
2719 | required. */ | |
00595019 MS |
2720 | |
2721 | void | |
848eed92 | 2722 | import_export_decl (tree decl) |
00595019 | 2723 | { |
4684cd27 MM |
2724 | int emit_p; |
2725 | bool comdat_p; | |
2726 | bool import_p; | |
1e731102 | 2727 | tree class_type = NULL_TREE; |
4684cd27 | 2728 | |
db5ae43f | 2729 | if (DECL_INTERFACE_KNOWN (decl)) |
00595019 MS |
2730 | return; |
2731 | ||
4684cd27 MM |
2732 | /* We cannot determine what linkage to give to an entity with vague |
2733 | linkage until the end of the file. For example, a virtual table | |
2734 | for a class will be defined if and only if the key method is | |
2735 | defined in this translation unit. As a further example, consider | |
2736 | that when compiling a translation unit that uses PCH file with | |
2737 | "-frepo" it would be incorrect to make decisions about what | |
2738 | entities to emit when building the PCH; those decisions must be | |
2739 | delayed until the repository information has been processed. */ | |
50bc768d | 2740 | gcc_assert (at_eof); |
4684cd27 MM |
2741 | /* Object file linkage for explicit instantiations is handled in |
2742 | mark_decl_instantiated. For static variables in functions with | |
2743 | vague linkage, maybe_commonize_var is used. | |
2744 | ||
2745 | Therefore, the only declarations that should be provided to this | |
44370687 | 2746 | function are those with external linkage that are: |
4684cd27 MM |
2747 | |
2748 | * implicit instantiations of function templates | |
2749 | ||
2750 | * inline function | |
2751 | ||
2752 | * implicit instantiations of static data members of class | |
2753 | templates | |
2754 | ||
2755 | * virtual tables | |
2756 | ||
2757 | * typeinfo objects | |
2758 | ||
2759 | Furthermore, all entities that reach this point must have a | |
2760 | definition available in this translation unit. | |
2761 | ||
2762 | The following assertions check these conditions. */ | |
cb6da767 | 2763 | gcc_assert (VAR_OR_FUNCTION_DECL_P (decl)); |
4684cd27 MM |
2764 | /* Any code that creates entities with TREE_PUBLIC cleared should |
2765 | also set DECL_INTERFACE_KNOWN. */ | |
50bc768d | 2766 | gcc_assert (TREE_PUBLIC (decl)); |
4684cd27 | 2767 | if (TREE_CODE (decl) == FUNCTION_DECL) |
50bc768d NS |
2768 | gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl) |
2769 | || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl) | |
2770 | || DECL_DECLARED_INLINE_P (decl)); | |
4684cd27 | 2771 | else |
50bc768d NS |
2772 | gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl) |
2773 | || DECL_VTABLE_OR_VTT_P (decl) | |
2774 | || DECL_TINFO_P (decl)); | |
4684cd27 MM |
2775 | /* Check that a definition of DECL is available in this translation |
2776 | unit. */ | |
50bc768d | 2777 | gcc_assert (!DECL_REALLY_EXTERN (decl)); |
4684cd27 MM |
2778 | |
2779 | /* Assume that DECL will not have COMDAT linkage. */ | |
2780 | comdat_p = false; | |
2781 | /* Assume that DECL will not be imported into this translation | |
2782 | unit. */ | |
2783 | import_p = false; | |
2784 | ||
2785 | /* See if the repository tells us whether or not to emit DECL in | |
2786 | this translation unit. */ | |
2787 | emit_p = repo_emit_p (decl); | |
2788 | if (emit_p == 0) | |
2789 | import_p = true; | |
2790 | else if (emit_p == 1) | |
2791 | { | |
2792 | /* The repository indicates that this entity should be defined | |
2793 | here. Make sure the back end honors that request. */ | |
d5d0ed2d | 2794 | mark_needed (decl); |
4684cd27 MM |
2795 | /* Output the definition as an ordinary strong definition. */ |
2796 | DECL_EXTERNAL (decl) = 0; | |
2797 | DECL_INTERFACE_KNOWN (decl) = 1; | |
2798 | return; | |
2799 | } | |
2800 | ||
2801 | if (import_p) | |
2802 | /* We have already decided what to do with this DECL; there is no | |
2803 | need to check anything further. */ | |
2804 | ; | |
5a6ccc94 | 2805 | else if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl)) |
4684cd27 | 2806 | { |
1e731102 MM |
2807 | class_type = DECL_CONTEXT (decl); |
2808 | import_export_class (class_type); | |
bfecd57c JJ |
2809 | if (CLASSTYPE_INTERFACE_KNOWN (class_type) |
2810 | && CLASSTYPE_INTERFACE_ONLY (class_type)) | |
4684cd27 | 2811 | import_p = true; |
e72f3fa1 | 2812 | else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC) |
1e731102 MM |
2813 | && !CLASSTYPE_USE_TEMPLATE (class_type) |
2814 | && CLASSTYPE_KEY_METHOD (class_type) | |
2815 | && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))) | |
4684cd27 MM |
2816 | /* The ABI requires that all virtual tables be emitted with |
2817 | COMDAT linkage. However, on systems where COMDAT symbols | |
2818 | don't show up in the table of contents for a static | |
e72f3fa1 MM |
2819 | archive, or on systems without weak symbols (where we |
2820 | approximate COMDAT linkage by using internal linkage), the | |
2821 | linker will report errors about undefined symbols because | |
2822 | it will not see the virtual table definition. Therefore, | |
2823 | in the case that we know that the virtual table will be | |
2824 | emitted in only one translation unit, we make the virtual | |
2825 | table an ordinary definition with external linkage. */ | |
4684cd27 | 2826 | DECL_EXTERNAL (decl) = 0; |
1e731102 | 2827 | else if (CLASSTYPE_INTERFACE_KNOWN (class_type)) |
b41d214e | 2828 | { |
1e731102 | 2829 | /* CLASS_TYPE is being exported from this translation unit, |
c8094d83 | 2830 | so DECL should be defined here. */ |
1e731102 | 2831 | if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type)) |
e72f3fa1 MM |
2832 | /* If a class is declared in a header with the "extern |
2833 | template" extension, then it will not be instantiated, | |
2834 | even in translation units that would normally require | |
2835 | it. Often such classes are explicitly instantiated in | |
2836 | one translation unit. Therefore, the explicit | |
2837 | instantiation must be made visible to other translation | |
2838 | units. */ | |
2839 | DECL_EXTERNAL (decl) = 0; | |
2840 | else | |
2841 | { | |
1e731102 MM |
2842 | /* The generic C++ ABI says that class data is always |
2843 | COMDAT, even if there is a key function. Some | |
2844 | variants (e.g., the ARM EABI) says that class data | |
1a1d0917 MS |
2845 | only has COMDAT linkage if the class data might be |
2846 | emitted in more than one translation unit. When the | |
2847 | key method can be inline and is inline, we still have | |
2848 | to arrange for comdat even though | |
2849 | class_data_always_comdat is false. */ | |
1e731102 | 2850 | if (!CLASSTYPE_KEY_METHOD (class_type) |
67a70df6 | 2851 | || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)) |
1e731102 MM |
2852 | || targetm.cxx.class_data_always_comdat ()) |
2853 | { | |
2854 | /* The ABI requires COMDAT linkage. Normally, we | |
2855 | only emit COMDAT things when they are needed; | |
2856 | make sure that we realize that this entity is | |
2857 | indeed needed. */ | |
2858 | comdat_p = true; | |
2859 | mark_needed (decl); | |
2860 | } | |
e72f3fa1 | 2861 | } |
b41d214e | 2862 | } |
4684cd27 | 2863 | else if (!flag_implicit_templates |
1e731102 | 2864 | && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type)) |
4684cd27 MM |
2865 | import_p = true; |
2866 | else | |
2867 | comdat_p = true; | |
2868 | } | |
5a6ccc94 | 2869 | else if (VAR_P (decl) && DECL_TINFO_P (decl)) |
4684cd27 MM |
2870 | { |
2871 | tree type = TREE_TYPE (DECL_NAME (decl)); | |
2872 | if (CLASS_TYPE_P (type)) | |
2873 | { | |
1e731102 | 2874 | class_type = type; |
4684cd27 MM |
2875 | import_export_class (type); |
2876 | if (CLASSTYPE_INTERFACE_KNOWN (type) | |
2877 | && TYPE_POLYMORPHIC_P (type) | |
2878 | && CLASSTYPE_INTERFACE_ONLY (type) | |
2879 | /* If -fno-rtti was specified, then we cannot be sure | |
2880 | that RTTI information will be emitted with the | |
2881 | virtual table of the class, so we must emit it | |
2882 | wherever it is used. */ | |
2883 | && flag_rtti) | |
2884 | import_p = true; | |
c8094d83 | 2885 | else |
4684cd27 | 2886 | { |
4684cd27 MM |
2887 | if (CLASSTYPE_INTERFACE_KNOWN (type) |
2888 | && !CLASSTYPE_INTERFACE_ONLY (type)) | |
e72f3fa1 | 2889 | { |
1a1d0917 MS |
2890 | comdat_p = (targetm.cxx.class_data_always_comdat () |
2891 | || (CLASSTYPE_KEY_METHOD (type) | |
67a70df6 | 2892 | && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type)))); |
cd282a6d | 2893 | mark_needed (decl); |
e72f3fa1 MM |
2894 | if (!flag_weak) |
2895 | { | |
2896 | comdat_p = false; | |
2897 | DECL_EXTERNAL (decl) = 0; | |
2898 | } | |
2899 | } | |
1e731102 MM |
2900 | else |
2901 | comdat_p = true; | |
4684cd27 MM |
2902 | } |
2903 | } | |
2904 | else | |
2905 | comdat_p = true; | |
2906 | } | |
5bc08e85 | 2907 | else if (DECL_TEMPLOID_INSTANTIATION (decl)) |
4684cd27 MM |
2908 | { |
2909 | /* DECL is an implicit instantiation of a function or static | |
2910 | data member. */ | |
23372b3f JJ |
2911 | if ((flag_implicit_templates |
2912 | && !flag_use_repository) | |
4684cd27 | 2913 | || (flag_implicit_inline_templates |
c8094d83 | 2914 | && TREE_CODE (decl) == FUNCTION_DECL |
4684cd27 MM |
2915 | && DECL_DECLARED_INLINE_P (decl))) |
2916 | comdat_p = true; | |
2917 | else | |
2918 | /* If we are not implicitly generating templates, then mark | |
2919 | this entity as undefined in this translation unit. */ | |
2920 | import_p = true; | |
00595019 MS |
2921 | } |
2922 | else if (DECL_FUNCTION_MEMBER_P (decl)) | |
2923 | { | |
79065db2 | 2924 | if (!DECL_DECLARED_INLINE_P (decl)) |
00595019 | 2925 | { |
92788413 MM |
2926 | tree ctype = DECL_CONTEXT (decl); |
2927 | import_export_class (ctype); | |
2928 | if (CLASSTYPE_INTERFACE_KNOWN (ctype)) | |
2929 | { | |
2930 | DECL_NOT_REALLY_EXTERN (decl) | |
2931 | = ! (CLASSTYPE_INTERFACE_ONLY (ctype) | |
c8094d83 | 2932 | || (DECL_DECLARED_INLINE_P (decl) |
79065db2 | 2933 | && ! flag_implement_inlines |
92788413 MM |
2934 | && !DECL_VINDEX (decl))); |
2935 | ||
b41d214e JM |
2936 | if (!DECL_NOT_REALLY_EXTERN (decl)) |
2937 | DECL_EXTERNAL (decl) = 1; | |
2938 | ||
92788413 MM |
2939 | /* Always make artificials weak. */ |
2940 | if (DECL_ARTIFICIAL (decl) && flag_weak) | |
4684cd27 | 2941 | comdat_p = true; |
92788413 MM |
2942 | else |
2943 | maybe_make_one_only (decl); | |
2944 | } | |
00595019 | 2945 | } |
db5ae43f | 2946 | else |
4684cd27 | 2947 | comdat_p = true; |
00595019 | 2948 | } |
db5ae43f | 2949 | else |
4684cd27 | 2950 | comdat_p = true; |
d689a8f1 | 2951 | |
4684cd27 | 2952 | if (import_p) |
d689a8f1 | 2953 | { |
4684cd27 MM |
2954 | /* If we are importing DECL into this translation unit, mark is |
2955 | an undefined here. */ | |
2956 | DECL_EXTERNAL (decl) = 1; | |
2957 | DECL_NOT_REALLY_EXTERN (decl) = 0; | |
d689a8f1 | 2958 | } |
4684cd27 | 2959 | else if (comdat_p) |
d689a8f1 | 2960 | { |
4684cd27 MM |
2961 | /* If we decided to put DECL in COMDAT, mark it accordingly at |
2962 | this point. */ | |
2963 | comdat_linkage (decl); | |
d689a8f1 | 2964 | } |
c6f553d1 | 2965 | |
d689a8f1 NS |
2966 | DECL_INTERFACE_KNOWN (decl) = 1; |
2967 | } | |
2968 | ||
c10cdb3d MM |
2969 | /* Return an expression that performs the destruction of DECL, which |
2970 | must be a VAR_DECL whose type has a non-trivial destructor, or is | |
2971 | an array whose (innermost) elements have a non-trivial destructor. */ | |
2972 | ||
72b7eeff | 2973 | tree |
848eed92 | 2974 | build_cleanup (tree decl) |
72b7eeff | 2975 | { |
b8aaa1a3 JM |
2976 | tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error); |
2977 | gcc_assert (clean != NULL_TREE); | |
2978 | return clean; | |
72b7eeff MS |
2979 | } |
2980 | ||
c395453c MM |
2981 | /* Returns the initialization guard variable for the variable DECL, |
2982 | which has static storage duration. */ | |
1f6e1acc | 2983 | |
c395453c | 2984 | tree |
848eed92 | 2985 | get_guard (tree decl) |
67d743fe | 2986 | { |
1f6e1acc | 2987 | tree sname; |
c395453c MM |
2988 | tree guard; |
2989 | ||
1f84ec23 | 2990 | sname = mangle_guard_variable (decl); |
c395453c MM |
2991 | guard = IDENTIFIER_GLOBAL_VALUE (sname); |
2992 | if (! guard) | |
2993 | { | |
2994 | tree guard_type; | |
2995 | ||
3461fba7 NS |
2996 | /* We use a type that is big enough to contain a mutex as well |
2997 | as an integer counter. */ | |
4185ae53 | 2998 | guard_type = targetm.cxx.guard_type (); |
c2255bc4 AH |
2999 | guard = build_decl (DECL_SOURCE_LOCATION (decl), |
3000 | VAR_DECL, sname, guard_type); | |
c8094d83 | 3001 | |
a1c65f9f | 3002 | /* The guard should have the same linkage as what it guards. */ |
844511c8 NS |
3003 | TREE_PUBLIC (guard) = TREE_PUBLIC (decl); |
3004 | TREE_STATIC (guard) = TREE_STATIC (decl); | |
3005 | DECL_COMMON (guard) = DECL_COMMON (decl); | |
07357341 | 3006 | DECL_COMDAT (guard) = DECL_COMDAT (decl); |
3048c0c7 | 3007 | CP_DECL_THREAD_LOCAL_P (guard) = CP_DECL_THREAD_LOCAL_P (decl); |
56363ffd | 3008 | set_decl_tls_model (guard, DECL_TLS_MODEL (decl)); |
d35311a2 JM |
3009 | if (DECL_ONE_ONLY (decl)) |
3010 | make_decl_one_only (guard, cxx_comdat_group (guard)); | |
844511c8 | 3011 | if (TREE_PUBLIC (decl)) |
0cbd7506 | 3012 | DECL_WEAK (guard) = DECL_WEAK (decl); |
3fd40684 JM |
3013 | DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl); |
3014 | DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl); | |
c8094d83 | 3015 | |
c395453c | 3016 | DECL_ARTIFICIAL (guard) = 1; |
78e0d62b | 3017 | DECL_IGNORED_P (guard) = 1; |
c395453c | 3018 | TREE_USED (guard) = 1; |
2e3757e7 | 3019 | pushdecl_top_level_and_finish (guard, NULL_TREE); |
c395453c MM |
3020 | } |
3021 | return guard; | |
3022 | } | |
3023 | ||
e93ca5ca RR |
3024 | /* Return an atomic load of src with the appropriate memory model. */ |
3025 | ||
3026 | static tree | |
3027 | build_atomic_load_byte (tree src, HOST_WIDE_INT model) | |
3028 | { | |
3029 | tree ptr_type = build_pointer_type (char_type_node); | |
3030 | tree mem_model = build_int_cst (integer_type_node, model); | |
3031 | tree t, addr, val; | |
3032 | unsigned int size; | |
3033 | int fncode; | |
3034 | ||
3035 | size = tree_to_uhwi (TYPE_SIZE_UNIT (char_type_node)); | |
3036 | ||
3037 | fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1; | |
3038 | t = builtin_decl_implicit ((enum built_in_function) fncode); | |
3039 | ||
3040 | addr = build1 (ADDR_EXPR, ptr_type, src); | |
3041 | val = build_call_expr (t, 2, addr, mem_model); | |
3042 | return val; | |
3043 | } | |
3044 | ||
c395453c MM |
3045 | /* Return those bits of the GUARD variable that should be set when the |
3046 | guarded entity is actually initialized. */ | |
3047 | ||
3048 | static tree | |
848eed92 | 3049 | get_guard_bits (tree guard) |
c395453c | 3050 | { |
4185ae53 PB |
3051 | if (!targetm.cxx.guard_mask_bit ()) |
3052 | { | |
3053 | /* We only set the first byte of the guard, in order to leave room | |
3054 | for a mutex in the high-order bits. */ | |
c8094d83 | 3055 | guard = build1 (ADDR_EXPR, |
4185ae53 PB |
3056 | build_pointer_type (TREE_TYPE (guard)), |
3057 | guard); | |
c8094d83 MS |
3058 | guard = build1 (NOP_EXPR, |
3059 | build_pointer_type (char_type_node), | |
4185ae53 PB |
3060 | guard); |
3061 | guard = build1 (INDIRECT_REF, char_type_node, guard); | |
3062 | } | |
c395453c MM |
3063 | |
3064 | return guard; | |
3065 | } | |
3066 | ||
3067 | /* Return an expression which determines whether or not the GUARD | |
3068 | variable has already been initialized. */ | |
3069 | ||
3070 | tree | |
e93ca5ca | 3071 | get_guard_cond (tree guard, bool thread_safe) |
c395453c MM |
3072 | { |
3073 | tree guard_value; | |
3074 | ||
e93ca5ca RR |
3075 | if (!thread_safe) |
3076 | guard = get_guard_bits (guard); | |
3077 | else | |
3078 | guard = build_atomic_load_byte (guard, MEMMODEL_ACQUIRE); | |
4185ae53 PB |
3079 | |
3080 | /* Mask off all but the low bit. */ | |
3081 | if (targetm.cxx.guard_mask_bit ()) | |
3082 | { | |
3083 | guard_value = integer_one_node; | |
3084 | if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard))) | |
cda0a029 | 3085 | guard_value = fold_convert (TREE_TYPE (guard), guard_value); |
ba47d38d AH |
3086 | guard = cp_build_binary_op (input_location, |
3087 | BIT_AND_EXPR, guard, guard_value, | |
5ade1ed2 | 3088 | tf_warning_or_error); |
4185ae53 PB |
3089 | } |
3090 | ||
c395453c MM |
3091 | guard_value = integer_zero_node; |
3092 | if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard))) | |
cda0a029 | 3093 | guard_value = fold_convert (TREE_TYPE (guard), guard_value); |
ba47d38d AH |
3094 | return cp_build_binary_op (input_location, |
3095 | EQ_EXPR, guard, guard_value, | |
5ade1ed2 | 3096 | tf_warning_or_error); |
c395453c MM |
3097 | } |
3098 | ||
3099 | /* Return an expression which sets the GUARD variable, indicating that | |
3100 | the variable being guarded has been initialized. */ | |
3101 | ||
3102 | tree | |
848eed92 | 3103 | set_guard (tree guard) |
c395453c MM |
3104 | { |
3105 | tree guard_init; | |
3106 | ||
3107 | /* Set the GUARD to one. */ | |
3108 | guard = get_guard_bits (guard); | |
3109 | guard_init = integer_one_node; | |
3110 | if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard))) | |
cda0a029 | 3111 | guard_init = fold_convert (TREE_TYPE (guard), guard_init); |
4f2e1536 | 3112 | return cp_build_modify_expr (input_location, guard, NOP_EXPR, guard_init, |
5ade1ed2 | 3113 | tf_warning_or_error); |
67d743fe MS |
3114 | } |
3115 | ||
7c424acd JM |
3116 | /* Returns true iff we can tell that VAR does not have a dynamic |
3117 | initializer. */ | |
3118 | ||
3119 | static bool | |
3120 | var_defined_without_dynamic_init (tree var) | |
3121 | { | |
3122 | /* If it's defined in another TU, we can't tell. */ | |
3123 | if (DECL_EXTERNAL (var)) | |
3124 | return false; | |
3125 | /* If it has a non-trivial destructor, registering the destructor | |
3126 | counts as dynamic initialization. */ | |
3127 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var))) | |
3128 | return false; | |
36b218ee PC |
3129 | /* If it's in this TU, its initializer has been processed, unless |
3130 | it's a case of self-initialization, then DECL_INITIALIZED_P is | |
3131 | false while the initializer is handled by finish_id_expression. */ | |
3132 | if (!DECL_INITIALIZED_P (var)) | |
3133 | return false; | |
7c424acd JM |
3134 | /* If it has no initializer or a constant one, it's not dynamic. */ |
3135 | return (!DECL_NONTRIVIALLY_INITIALIZED_P (var) | |
3136 | || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var)); | |
3137 | } | |
3138 | ||
3139 | /* Returns true iff VAR is a variable that needs uses to be | |
3140 | wrapped for possible dynamic initialization. */ | |
3141 | ||
3142 | static bool | |
3143 | var_needs_tls_wrapper (tree var) | |
3144 | { | |
fdbff37f | 3145 | return (!error_operand_p (var) |
3048c0c7 | 3146 | && CP_DECL_THREAD_LOCAL_P (var) |
7c424acd JM |
3147 | && !DECL_GNU_TLS_P (var) |
3148 | && !DECL_FUNCTION_SCOPE_P (var) | |
3149 | && !var_defined_without_dynamic_init (var)); | |
3150 | } | |
3151 | ||
5af057d8 JM |
3152 | /* Get the FUNCTION_DECL for the shared TLS init function for this |
3153 | translation unit. */ | |
3154 | ||
3155 | static tree | |
3156 | get_local_tls_init_fn (void) | |
3157 | { | |
3158 | tree sname = get_identifier ("__tls_init"); | |
3159 | tree fn = IDENTIFIER_GLOBAL_VALUE (sname); | |
3160 | if (!fn) | |
3161 | { | |
3162 | fn = build_lang_decl (FUNCTION_DECL, sname, | |
3163 | build_function_type (void_type_node, | |
3164 | void_list_node)); | |
3165 | SET_DECL_LANGUAGE (fn, lang_c); | |
3166 | TREE_PUBLIC (fn) = false; | |
3167 | DECL_ARTIFICIAL (fn) = true; | |
3168 | mark_used (fn); | |
3169 | SET_IDENTIFIER_GLOBAL_VALUE (sname, fn); | |
3170 | } | |
3171 | return fn; | |
3172 | } | |
3173 | ||
7c424acd JM |
3174 | /* Get a FUNCTION_DECL for the init function for the thread_local |
3175 | variable VAR. The init function will be an alias to the function | |
3176 | that initializes all the non-local TLS variables in the translation | |
3177 | unit. The init function is only used by the wrapper function. */ | |
3178 | ||
3179 | static tree | |
3180 | get_tls_init_fn (tree var) | |
3181 | { | |
3182 | /* Only C++11 TLS vars need this init fn. */ | |
3183 | if (!var_needs_tls_wrapper (var)) | |
3184 | return NULL_TREE; | |
3185 | ||
5af057d8 JM |
3186 | /* If -fno-extern-tls-init, assume that we don't need to call |
3187 | a tls init function for a variable defined in another TU. */ | |
3188 | if (!flag_extern_tls_init && DECL_EXTERNAL (var)) | |
3189 | return NULL_TREE; | |
3190 | ||
3191 | #ifdef ASM_OUTPUT_DEF | |
3192 | /* If the variable is internal, or if we can't generate aliases, | |
3193 | call the local init function directly. */ | |
3194 | if (!TREE_PUBLIC (var)) | |
3195 | #endif | |
3196 | return get_local_tls_init_fn (); | |
3197 | ||
7c424acd JM |
3198 | tree sname = mangle_tls_init_fn (var); |
3199 | tree fn = IDENTIFIER_GLOBAL_VALUE (sname); | |
3200 | if (!fn) | |
3201 | { | |
3202 | fn = build_lang_decl (FUNCTION_DECL, sname, | |
3203 | build_function_type (void_type_node, | |
3204 | void_list_node)); | |
3205 | SET_DECL_LANGUAGE (fn, lang_c); | |
3206 | TREE_PUBLIC (fn) = TREE_PUBLIC (var); | |
3207 | DECL_ARTIFICIAL (fn) = true; | |
3208 | DECL_COMDAT (fn) = DECL_COMDAT (var); | |
99a82a1e | 3209 | DECL_EXTERNAL (fn) = DECL_EXTERNAL (var); |
7c424acd JM |
3210 | if (DECL_ONE_ONLY (var)) |
3211 | make_decl_one_only (fn, cxx_comdat_group (fn)); | |
3212 | if (TREE_PUBLIC (var)) | |
3213 | { | |
3214 | tree obtype = strip_array_types (non_reference (TREE_TYPE (var))); | |
5af057d8 JM |
3215 | /* If the variable is defined somewhere else and might have static |
3216 | initialization, make the init function a weak reference. */ | |
7c424acd JM |
3217 | if ((!TYPE_NEEDS_CONSTRUCTING (obtype) |
3218 | || TYPE_HAS_CONSTEXPR_CTOR (obtype)) | |
5af057d8 JM |
3219 | && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype) |
3220 | && DECL_EXTERNAL (var)) | |
7c424acd JM |
3221 | declare_weak (fn); |
3222 | else | |
3223 | DECL_WEAK (fn) = DECL_WEAK (var); | |
3224 | } | |
3225 | DECL_VISIBILITY (fn) = DECL_VISIBILITY (var); | |
3226 | DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var); | |
3227 | DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var); | |
3228 | DECL_IGNORED_P (fn) = 1; | |
3229 | mark_used (fn); | |
3230 | ||
3231 | DECL_BEFRIENDING_CLASSES (fn) = var; | |
3232 | ||
3233 | SET_IDENTIFIER_GLOBAL_VALUE (sname, fn); | |
3234 | } | |
3235 | return fn; | |
3236 | } | |
3237 | ||
3238 | /* Get a FUNCTION_DECL for the init wrapper function for the thread_local | |
3239 | variable VAR. The wrapper function calls the init function (if any) for | |
3240 | VAR and then returns a reference to VAR. The wrapper function is used | |
3241 | in place of VAR everywhere VAR is mentioned. */ | |
3242 | ||
3243 | tree | |
3244 | get_tls_wrapper_fn (tree var) | |
3245 | { | |
3246 | /* Only C++11 TLS vars need this wrapper fn. */ | |
3247 | if (!var_needs_tls_wrapper (var)) | |
3248 | return NULL_TREE; | |
3249 | ||
3250 | tree sname = mangle_tls_wrapper_fn (var); | |
3251 | tree fn = IDENTIFIER_GLOBAL_VALUE (sname); | |
3252 | if (!fn) | |
3253 | { | |
3254 | /* A named rvalue reference is an lvalue, so the wrapper should | |
3255 | always return an lvalue reference. */ | |
3256 | tree type = non_reference (TREE_TYPE (var)); | |
3257 | type = build_reference_type (type); | |
3258 | tree fntype = build_function_type (type, void_list_node); | |
3259 | fn = build_lang_decl (FUNCTION_DECL, sname, fntype); | |
3260 | SET_DECL_LANGUAGE (fn, lang_c); | |
3261 | TREE_PUBLIC (fn) = TREE_PUBLIC (var); | |
3262 | DECL_ARTIFICIAL (fn) = true; | |
3263 | DECL_IGNORED_P (fn) = 1; | |
3264 | /* The wrapper is inline and emitted everywhere var is used. */ | |
3265 | DECL_DECLARED_INLINE_P (fn) = true; | |
3266 | if (TREE_PUBLIC (var)) | |
3267 | { | |
3268 | comdat_linkage (fn); | |
3269 | #ifdef HAVE_GAS_HIDDEN | |
3270 | /* Make the wrapper bind locally; there's no reason to share | |
3271 | the wrapper between multiple shared objects. */ | |
3272 | DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL; | |
3273 | DECL_VISIBILITY_SPECIFIED (fn) = true; | |
3274 | #endif | |
3275 | } | |
3276 | if (!TREE_PUBLIC (fn)) | |
3277 | DECL_INTERFACE_KNOWN (fn) = true; | |
3278 | mark_used (fn); | |
3279 | note_vague_linkage_fn (fn); | |
3280 | ||
3281 | #if 0 | |
3282 | /* We want CSE to commonize calls to the wrapper, but marking it as | |
3283 | pure is unsafe since it has side-effects. I guess we need a new | |
3284 | ECF flag even weaker than ECF_PURE. FIXME! */ | |
3285 | DECL_PURE_P (fn) = true; | |
3286 | #endif | |
3287 | ||
3288 | DECL_BEFRIENDING_CLASSES (fn) = var; | |
3289 | ||
3290 | SET_IDENTIFIER_GLOBAL_VALUE (sname, fn); | |
3291 | } | |
3292 | return fn; | |
3293 | } | |
3294 | ||
3295 | /* At EOF, generate the definition for the TLS wrapper function FN: | |
3296 | ||
3297 | T& var_wrapper() { | |
3298 | if (init_fn) init_fn(); | |
3299 | return var; | |
3300 | } */ | |
3301 | ||
3302 | static void | |
3303 | generate_tls_wrapper (tree fn) | |
3304 | { | |
3305 | tree var = DECL_BEFRIENDING_CLASSES (fn); | |
3306 | ||
3307 | start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED); | |
3308 | tree body = begin_function_body (); | |
3309 | /* Only call the init fn if there might be one. */ | |
3310 | if (tree init_fn = get_tls_init_fn (var)) | |
3311 | { | |
3312 | tree if_stmt = NULL_TREE; | |
3313 | /* If init_fn is a weakref, make sure it exists before calling. */ | |
3314 | if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn))) | |
3315 | { | |
3316 | if_stmt = begin_if_stmt (); | |
3317 | tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error); | |
3318 | tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var), | |
3319 | NE_EXPR, addr, nullptr_node, | |
3320 | tf_warning_or_error); | |
3321 | finish_if_stmt_cond (cond, if_stmt); | |
3322 | } | |
3323 | finish_expr_stmt (build_cxx_call | |
3324 | (init_fn, 0, NULL, tf_warning_or_error)); | |
3325 | if (if_stmt) | |
3326 | { | |
3327 | finish_then_clause (if_stmt); | |
3328 | finish_if_stmt (if_stmt); | |
3329 | } | |
3330 | } | |
5af057d8 JM |
3331 | else |
3332 | /* If there's no initialization, the wrapper is a constant function. */ | |
3333 | TREE_READONLY (fn) = true; | |
7c424acd JM |
3334 | finish_return_stmt (convert_from_reference (var)); |
3335 | finish_function_body (body); | |
3336 | expand_or_defer_fn (finish_function (0)); | |
3337 | } | |
3338 | ||
961ec1a5 | 3339 | /* Start the process of running a particular set of global constructors |
2077db1b CT |
3340 | or destructors. Subroutine of do_[cd]tors. Also called from |
3341 | vtv_start_verification_constructor_init_function. */ | |
961ec1a5 | 3342 | |
914653a2 | 3343 | static tree |
848eed92 | 3344 | start_objects (int method_type, int initp) |
961ec1a5 | 3345 | { |
914653a2 | 3346 | tree body; |
058b15c1 | 3347 | tree fndecl; |
3a9ed12a | 3348 | char type[14]; |
961ec1a5 JM |
3349 | |
3350 | /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */ | |
3351 | ||
000ab922 | 3352 | if (initp != DEFAULT_INIT_PRIORITY) |
2ce3c6c6 | 3353 | { |
066d147c MH |
3354 | char joiner; |
3355 | ||
3356 | #ifdef JOINER | |
3357 | joiner = JOINER; | |
3358 | #else | |
3359 | joiner = '_'; | |
3360 | #endif | |
2ce3c6c6 | 3361 | |
3a9ed12a | 3362 | sprintf (type, "sub_%c%c%.5u", method_type, joiner, initp); |
2ce3c6c6 JM |
3363 | } |
3364 | else | |
3a9ed12a | 3365 | sprintf (type, "sub_%c", method_type); |
2ce3c6c6 | 3366 | |
c8094d83 | 3367 | fndecl = build_lang_decl (FUNCTION_DECL, |
5880f14f | 3368 | get_file_function_name (type), |
0244e6f7 NF |
3369 | build_function_type_list (void_type_node, |
3370 | NULL_TREE)); | |
058b15c1 | 3371 | start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED); |
961ec1a5 | 3372 | |
7be82279 | 3373 | TREE_PUBLIC (current_function_decl) = 0; |
b4bb92e5 | 3374 | |
32d49134 GK |
3375 | /* Mark as artificial because it's not explicitly in the user's |
3376 | source code. */ | |
3377 | DECL_ARTIFICIAL (current_function_decl) = 1; | |
3378 | ||
5fdaba89 MM |
3379 | /* Mark this declaration as used to avoid spurious warnings. */ |
3380 | TREE_USED (current_function_decl) = 1; | |
3381 | ||
2b76013c MM |
3382 | /* Mark this function as a global constructor or destructor. */ |
3383 | if (method_type == 'I') | |
3384 | DECL_GLOBAL_CTOR_P (current_function_decl) = 1; | |
3385 | else | |
3386 | DECL_GLOBAL_DTOR_P (current_function_decl) = 1; | |
2b76013c | 3387 | |
325c3691 | 3388 | body = begin_compound_stmt (BCS_FN_BODY); |
b3f738da | 3389 | |
914653a2 | 3390 | return body; |
961ec1a5 JM |
3391 | } |
3392 | ||
3393 | /* Finish the process of running a particular set of global constructors | |
3394 | or destructors. Subroutine of do_[cd]tors. */ | |
3395 | ||
3396 | static void | |
848eed92 | 3397 | finish_objects (int method_type, int initp, tree body) |
961ec1a5 | 3398 | { |
914653a2 | 3399 | tree fn; |
961ec1a5 | 3400 | |
c7cb8979 | 3401 | /* Finish up. */ |
7a3397c7 | 3402 | finish_compound_stmt (body); |
0acf7199 | 3403 | fn = finish_function (0); |
84df082b | 3404 | |
09191b7e | 3405 | if (method_type == 'I') |
2cc07db4 | 3406 | { |
09191b7e JH |
3407 | DECL_STATIC_CONSTRUCTOR (fn) = 1; |
3408 | decl_init_priority_insert (fn, initp); | |
2cc07db4 | 3409 | } |
09191b7e JH |
3410 | else |
3411 | { | |
3412 | DECL_STATIC_DESTRUCTOR (fn) = 1; | |
3413 | decl_fini_priority_insert (fn, initp); | |
3414 | } | |
3415 | ||
3416 | expand_or_defer_fn (fn); | |
961ec1a5 JM |
3417 | } |
3418 | ||
0aafb128 MM |
3419 | /* The names of the parameters to the function created to handle |
3420 | initializations and destructions for objects with static storage | |
3421 | duration. */ | |
3422 | #define INITIALIZE_P_IDENTIFIER "__initialize_p" | |
3423 | #define PRIORITY_IDENTIFIER "__priority" | |
3424 | ||
3425 | /* The name of the function we create to handle initializations and | |
3426 | destructions for objects with static storage duration. */ | |
3427 | #define SSDF_IDENTIFIER "__static_initialization_and_destruction" | |
3428 | ||
3429 | /* The declaration for the __INITIALIZE_P argument. */ | |
e2500fed | 3430 | static GTY(()) tree initialize_p_decl; |
0aafb128 MM |
3431 | |
3432 | /* The declaration for the __PRIORITY argument. */ | |
e2500fed | 3433 | static GTY(()) tree priority_decl; |
0aafb128 MM |
3434 | |
3435 | /* The declaration for the static storage duration function. */ | |
e2500fed | 3436 | static GTY(()) tree ssdf_decl; |
0aafb128 | 3437 | |
0352cfc8 MM |
3438 | /* All the static storage duration functions created in this |
3439 | translation unit. */ | |
9771b263 | 3440 | static GTY(()) vec<tree, va_gc> *ssdf_decls; |
0352cfc8 | 3441 | |
0aafb128 MM |
3442 | /* A map from priority levels to information about that priority |
3443 | level. There may be many such levels, so efficient lookup is | |
3444 | important. */ | |
3445 | static splay_tree priority_info_map; | |
3446 | ||
3447 | /* Begins the generation of the function that will handle all | |
3448 | initialization and destruction of objects with static storage | |
3449 | duration. The function generated takes two parameters of type | |
3450 | `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is | |
838dfd8a | 3451 | nonzero, it performs initializations. Otherwise, it performs |
0aafb128 MM |
3452 | destructions. It only performs those initializations or |
3453 | destructions with the indicated __PRIORITY. The generated function | |
c8094d83 | 3454 | returns no value. |
0aafb128 MM |
3455 | |
3456 | It is assumed that this function will only be called once per | |
3457 | translation unit. */ | |
961ec1a5 | 3458 | |
313bc2c2 | 3459 | static tree |
299f79b5 | 3460 | start_static_storage_duration_function (unsigned count) |
961ec1a5 | 3461 | { |
0aafb128 | 3462 | tree type; |
313bc2c2 | 3463 | tree body; |
0352cfc8 MM |
3464 | char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32]; |
3465 | ||
3466 | /* Create the identifier for this function. It will be of the form | |
3467 | SSDF_IDENTIFIER_<number>. */ | |
299f79b5 | 3468 | sprintf (id, "%s_%u", SSDF_IDENTIFIER, count); |
0aafb128 | 3469 | |
0244e6f7 NF |
3470 | type = build_function_type_list (void_type_node, |
3471 | integer_type_node, integer_type_node, | |
3472 | NULL_TREE); | |
0aafb128 MM |
3473 | |
3474 | /* Create the FUNCTION_DECL itself. */ | |
c8094d83 | 3475 | ssdf_decl = build_lang_decl (FUNCTION_DECL, |
0352cfc8 | 3476 | get_identifier (id), |
0aafb128 MM |
3477 | type); |
3478 | TREE_PUBLIC (ssdf_decl) = 0; | |
3479 | DECL_ARTIFICIAL (ssdf_decl) = 1; | |
0352cfc8 MM |
3480 | |
3481 | /* Put this function in the list of functions to be called from the | |
3482 | static constructors and destructors. */ | |
3483 | if (!ssdf_decls) | |
3484 | { | |
9771b263 | 3485 | vec_alloc (ssdf_decls, 32); |
0352cfc8 MM |
3486 | |
3487 | /* Take this opportunity to initialize the map from priority | |
a1c65f9f | 3488 | numbers to information about that priority level. */ |
0352cfc8 MM |
3489 | priority_info_map = splay_tree_new (splay_tree_compare_ints, |
3490 | /*delete_key_fn=*/0, | |
3491 | /*delete_value_fn=*/ | |
3492 | (splay_tree_delete_value_fn) &free); | |
3493 | ||
3494 | /* We always need to generate functions for the | |
3495 | DEFAULT_INIT_PRIORITY so enter it now. That way when we walk | |
3496 | priorities later, we'll be sure to find the | |
3497 | DEFAULT_INIT_PRIORITY. */ | |
3498 | get_priority_info (DEFAULT_INIT_PRIORITY); | |
3499 | } | |
3500 | ||
9771b263 | 3501 | vec_safe_push (ssdf_decls, ssdf_decl); |
0aafb128 MM |
3502 | |
3503 | /* Create the argument list. */ | |
8e51619a JM |
3504 | initialize_p_decl = cp_build_parm_decl |
3505 | (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node); | |
0aafb128 | 3506 | DECL_CONTEXT (initialize_p_decl) = ssdf_decl; |
0aafb128 | 3507 | TREE_USED (initialize_p_decl) = 1; |
8e51619a JM |
3508 | priority_decl = cp_build_parm_decl |
3509 | (get_identifier (PRIORITY_IDENTIFIER), integer_type_node); | |
0aafb128 | 3510 | DECL_CONTEXT (priority_decl) = ssdf_decl; |
0aafb128 MM |
3511 | TREE_USED (priority_decl) = 1; |
3512 | ||
910ad8de | 3513 | DECL_CHAIN (initialize_p_decl) = priority_decl; |
0aafb128 MM |
3514 | DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl; |
3515 | ||
13ef1ec5 MM |
3516 | /* Put the function in the global scope. */ |
3517 | pushdecl (ssdf_decl); | |
3518 | ||
cd0be382 | 3519 | /* Start the function itself. This is equivalent to declaring the |
0aafb128 MM |
3520 | function as: |
3521 | ||
c472cdfd | 3522 | static void __ssdf (int __initialize_p, init __priority_p); |
c8094d83 | 3523 | |
0aafb128 MM |
3524 | It is static because we only need to call this function from the |
3525 | various constructor and destructor functions for this module. */ | |
058b15c1 MM |
3526 | start_preparsed_function (ssdf_decl, |
3527 | /*attrs=*/NULL_TREE, | |
3528 | SF_PRE_PARSED); | |
0aafb128 MM |
3529 | |
3530 | /* Set up the scope of the outermost block in the function. */ | |
325c3691 | 3531 | body = begin_compound_stmt (BCS_FN_BODY); |
2ce3c6c6 | 3532 | |
313bc2c2 | 3533 | return body; |
961ec1a5 JM |
3534 | } |
3535 | ||
0aafb128 MM |
3536 | /* Finish the generation of the function which performs initialization |
3537 | and destruction of objects with static storage duration. After | |
3538 | this point, no more such objects can be created. */ | |
961ec1a5 JM |
3539 | |
3540 | static void | |
848eed92 | 3541 | finish_static_storage_duration_function (tree body) |
961ec1a5 | 3542 | { |
0aafb128 | 3543 | /* Close out the function. */ |
7a3397c7 | 3544 | finish_compound_stmt (body); |
8cd2462c | 3545 | expand_or_defer_fn (finish_function (0)); |
0aafb128 | 3546 | } |
961ec1a5 | 3547 | |
0aafb128 MM |
3548 | /* Return the information about the indicated PRIORITY level. If no |
3549 | code to handle this level has yet been generated, generate the | |
3550 | appropriate prologue. */ | |
961ec1a5 | 3551 | |
0aafb128 | 3552 | static priority_info |
848eed92 | 3553 | get_priority_info (int priority) |
0aafb128 MM |
3554 | { |
3555 | priority_info pi; | |
3556 | splay_tree_node n; | |
3557 | ||
c8094d83 | 3558 | n = splay_tree_lookup (priority_info_map, |
0aafb128 MM |
3559 | (splay_tree_key) priority); |
3560 | if (!n) | |
3561 | { | |
3562 | /* Create a new priority information structure, and insert it | |
3563 | into the map. */ | |
0ac1b889 | 3564 | pi = XNEW (struct priority_info_s); |
0352cfc8 MM |
3565 | pi->initializations_p = 0; |
3566 | pi->destructions_p = 0; | |
0aafb128 MM |
3567 | splay_tree_insert (priority_info_map, |
3568 | (splay_tree_key) priority, | |
3569 | (splay_tree_value) pi); | |
3570 | } | |
3571 | else | |
3572 | pi = (priority_info) n->value; | |
961ec1a5 | 3573 | |
0aafb128 MM |
3574 | return pi; |
3575 | } | |
961ec1a5 | 3576 | |
6cec5cb5 RG |
3577 | /* The effective initialization priority of a DECL. */ |
3578 | ||
3579 | #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \ | |
3580 | ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \ | |
3581 | ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl)) | |
3582 | ||
dd36d4e1 | 3583 | /* Whether a DECL needs a guard to protect it against multiple |
6cec5cb5 RG |
3584 | initialization. */ |
3585 | ||
3586 | #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \ | |
3587 | || DECL_ONE_ONLY (decl) \ | |
3588 | || DECL_WEAK (decl))) | |
3589 | ||
e44c800e DS |
3590 | /* Called from one_static_initialization_or_destruction(), |
3591 | via walk_tree. | |
3592 | Walks the initializer list of a global variable and looks for | |
3593 | temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0) | |
3594 | and that have their DECL_CONTEXT() == NULL. | |
3595 | For each such temporary variable, set their DECL_CONTEXT() to | |
3596 | the current function. This is necessary because otherwise | |
3597 | some optimizers (enabled by -O2 -fprofile-arcs) might crash | |
3598 | when trying to refer to a temporary variable that does not have | |
3599 | it's DECL_CONTECT() properly set. */ | |
3600 | static tree | |
3601 | fix_temporary_vars_context_r (tree *node, | |
12308bc6 PC |
3602 | int * /*unused*/, |
3603 | void * /*unused1*/) | |
e44c800e DS |
3604 | { |
3605 | gcc_assert (current_function_decl); | |
3606 | ||
3607 | if (TREE_CODE (*node) == BIND_EXPR) | |
3608 | { | |
3609 | tree var; | |
3610 | ||
910ad8de | 3611 | for (var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var)) |
5a6ccc94 | 3612 | if (VAR_P (var) |
e44c800e DS |
3613 | && !DECL_NAME (var) |
3614 | && DECL_ARTIFICIAL (var) | |
3615 | && !DECL_CONTEXT (var)) | |
3616 | DECL_CONTEXT (var) = current_function_decl; | |
3617 | } | |
3618 | ||
3619 | return NULL_TREE; | |
3620 | } | |
3621 | ||
313bc2c2 | 3622 | /* Set up to handle the initialization or destruction of DECL. If |
838dfd8a | 3623 | INITP is nonzero, we are initializing the variable. Otherwise, we |
313bc2c2 MM |
3624 | are destroying it. */ |
3625 | ||
6cec5cb5 RG |
3626 | static void |
3627 | one_static_initialization_or_destruction (tree decl, tree init, bool initp) | |
313bc2c2 | 3628 | { |
c395453c | 3629 | tree guard_if_stmt = NULL_TREE; |
c395453c | 3630 | tree guard; |
313bc2c2 | 3631 | |
6cec5cb5 RG |
3632 | /* If we are supposed to destruct and there's a trivial destructor, |
3633 | nothing has to be done. */ | |
3634 | if (!initp | |
3635 | && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl))) | |
3636 | return; | |
313bc2c2 MM |
3637 | |
3638 | /* Trick the compiler into thinking we are at the file and line | |
3639 | where DECL was declared so that error-messages make sense, and so | |
3640 | that the debugger will show somewhat sensible file and line | |
3641 | information. */ | |
f31686a3 | 3642 | input_location = DECL_SOURCE_LOCATION (decl); |
313bc2c2 | 3643 | |
e44c800e DS |
3644 | /* Make sure temporary variables in the initialiser all have |
3645 | their DECL_CONTEXT() set to a value different from NULL_TREE. | |
5764ee3c | 3646 | This can happen when global variables initializers are built. |
e44c800e DS |
3647 | In that case, the DECL_CONTEXT() of the global variables _AND_ of all |
3648 | the temporary variables that might have been generated in the | |
5764ee3c | 3649 | accompanying initializers is NULL_TREE, meaning the variables have been |
e44c800e DS |
3650 | declared in the global namespace. |
3651 | What we want to do here is to fix that and make sure the DECL_CONTEXT() | |
3652 | of the temporaries are set to the current function decl. */ | |
3653 | cp_walk_tree_without_duplicates (&init, | |
3654 | fix_temporary_vars_context_r, | |
3655 | NULL); | |
3656 | ||
313bc2c2 MM |
3657 | /* Because of: |
3658 | ||
3659 | [class.access.spec] | |
3660 | ||
3661 | Access control for implicit calls to the constructors, | |
3662 | the conversion functions, or the destructor called to | |
3663 | create and destroy a static data member is performed as | |
3664 | if these calls appeared in the scope of the member's | |
c8094d83 | 3665 | class. |
313bc2c2 MM |
3666 | |
3667 | we pretend we are in a static member function of the class of | |
3668 | which the DECL is a member. */ | |
3669 | if (member_p (decl)) | |
3670 | { | |
4f1c5b7d | 3671 | DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl); |
313bc2c2 MM |
3672 | DECL_STATIC_FUNCTION_P (current_function_decl) = 1; |
3673 | } | |
c8094d83 | 3674 | |
c395453c MM |
3675 | /* Assume we don't need a guard. */ |
3676 | guard = NULL_TREE; | |
3677 | /* We need a guard if this is an object with external linkage that | |
1f6e1acc AS |
3678 | might be initialized in more than one place. (For example, a |
3679 | static data member of a template, when the data member requires | |
3680 | construction.) */ | |
6cec5cb5 | 3681 | if (NEEDS_GUARD_P (decl)) |
313bc2c2 | 3682 | { |
c395453c | 3683 | tree guard_cond; |
313bc2c2 | 3684 | |
c395453c | 3685 | guard = get_guard (decl); |
313bc2c2 | 3686 | |
c395453c MM |
3687 | /* When using __cxa_atexit, we just check the GUARD as we would |
3688 | for a local static. */ | |
3689 | if (flag_use_cxa_atexit) | |
3690 | { | |
3691 | /* When using __cxa_atexit, we never try to destroy | |
3692 | anything from a static destructor. */ | |
50bc768d | 3693 | gcc_assert (initp); |
e93ca5ca | 3694 | guard_cond = get_guard_cond (guard, false); |
c395453c | 3695 | } |
5fd893d5 MM |
3696 | /* If we don't have __cxa_atexit, then we will be running |
3697 | destructors from .fini sections, or their equivalents. So, | |
3698 | we need to know how many times we've tried to initialize this | |
3699 | object. We do initializations only if the GUARD is zero, | |
3700 | i.e., if we are the first to initialize the variable. We do | |
3701 | destructions only if the GUARD is one, i.e., if we are the | |
3702 | last to destroy the variable. */ | |
c395453c | 3703 | else if (initp) |
c8094d83 | 3704 | guard_cond |
ba47d38d AH |
3705 | = cp_build_binary_op (input_location, |
3706 | EQ_EXPR, | |
5ade1ed2 | 3707 | cp_build_unary_op (PREINCREMENT_EXPR, |
ba47d38d | 3708 | guard, |
e51fbec3 | 3709 | /*noconvert=*/true, |
ba47d38d | 3710 | tf_warning_or_error), |
5ade1ed2 DG |
3711 | integer_one_node, |
3712 | tf_warning_or_error); | |
313bc2c2 | 3713 | else |
c8094d83 | 3714 | guard_cond |
ba47d38d AH |
3715 | = cp_build_binary_op (input_location, |
3716 | EQ_EXPR, | |
5ade1ed2 | 3717 | cp_build_unary_op (PREDECREMENT_EXPR, |
ba47d38d | 3718 | guard, |
e51fbec3 | 3719 | /*noconvert=*/true, |
ba47d38d | 3720 | tf_warning_or_error), |
5ade1ed2 DG |
3721 | integer_zero_node, |
3722 | tf_warning_or_error); | |
ab76ca54 | 3723 | |
6cec5cb5 RG |
3724 | guard_if_stmt = begin_if_stmt (); |
3725 | finish_if_stmt_cond (guard_cond, guard_if_stmt); | |
313bc2c2 MM |
3726 | } |
3727 | ||
c395453c | 3728 | |
5fd893d5 MM |
3729 | /* If we're using __cxa_atexit, we have not already set the GUARD, |
3730 | so we must do so now. */ | |
3731 | if (guard && initp && flag_use_cxa_atexit) | |
c395453c | 3732 | finish_expr_stmt (set_guard (guard)); |
313bc2c2 | 3733 | |
6cec5cb5 RG |
3734 | /* Perform the initialization or destruction. */ |
3735 | if (initp) | |
3736 | { | |
3737 | if (init) | |
59b36ecf JJ |
3738 | { |
3739 | finish_expr_stmt (init); | |
3740 | if (flag_sanitize & SANITIZE_ADDRESS) | |
3741 | { | |
9041d2e6 | 3742 | varpool_node *vnode = varpool_node::get (decl); |
59b36ecf JJ |
3743 | if (vnode) |
3744 | vnode->dynamically_initialized = 1; | |
3745 | } | |
3746 | } | |
313bc2c2 | 3747 | |
6cec5cb5 | 3748 | /* If we're using __cxa_atexit, register a function that calls the |
3db45ab5 | 3749 | destructor for the object. */ |
6cec5cb5 | 3750 | if (flag_use_cxa_atexit) |
3db45ab5 | 3751 | finish_expr_stmt (register_dtor_fn (decl)); |
6cec5cb5 RG |
3752 | } |
3753 | else | |
3754 | finish_expr_stmt (build_cleanup (decl)); | |
313bc2c2 | 3755 | |
6cec5cb5 RG |
3756 | /* Finish the guard if-stmt, if necessary. */ |
3757 | if (guard) | |
3758 | { | |
3759 | finish_then_clause (guard_if_stmt); | |
3760 | finish_if_stmt (guard_if_stmt); | |
3761 | } | |
313bc2c2 MM |
3762 | |
3763 | /* Now that we're done with DECL we don't need to pretend to be a | |
3764 | member of its class any longer. */ | |
4f1c5b7d | 3765 | DECL_CONTEXT (current_function_decl) = NULL_TREE; |
313bc2c2 MM |
3766 | DECL_STATIC_FUNCTION_P (current_function_decl) = 0; |
3767 | } | |
3768 | ||
6cec5cb5 RG |
3769 | /* Generate code to do the initialization or destruction of the decls in VARS, |
3770 | a TREE_LIST of VAR_DECL with static storage duration. | |
3771 | Whether initialization or destruction is performed is specified by INITP. */ | |
961ec1a5 | 3772 | |
0aafb128 | 3773 | static void |
6cec5cb5 | 3774 | do_static_initialization_or_destruction (tree vars, bool initp) |
0aafb128 | 3775 | { |
6cec5cb5 | 3776 | tree node, init_if_stmt, cond; |
0aafb128 | 3777 | |
6cec5cb5 RG |
3778 | /* Build the outer if-stmt to check for initialization or destruction. */ |
3779 | init_if_stmt = begin_if_stmt (); | |
3780 | cond = initp ? integer_one_node : integer_zero_node; | |
ba47d38d AH |
3781 | cond = cp_build_binary_op (input_location, |
3782 | EQ_EXPR, | |
5ade1ed2 DG |
3783 | initialize_p_decl, |
3784 | cond, | |
3785 | tf_warning_or_error); | |
6cec5cb5 RG |
3786 | finish_if_stmt_cond (cond, init_if_stmt); |
3787 | ||
59b36ecf JJ |
3788 | /* To make sure dynamic construction doesn't access globals from other |
3789 | compilation units where they might not be yet constructed, for | |
3790 | -fsanitize=address insert __asan_before_dynamic_init call that | |
3791 | prevents access to either all global variables that need construction | |
3792 | in other compilation units, or at least those that haven't been | |
3793 | initialized yet. Variables that need dynamic construction in | |
3794 | the current compilation unit are kept accessible. */ | |
94087e88 | 3795 | if (initp && (flag_sanitize & SANITIZE_ADDRESS)) |
59b36ecf JJ |
3796 | finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false)); |
3797 | ||
6cec5cb5 RG |
3798 | node = vars; |
3799 | do { | |
3800 | tree decl = TREE_VALUE (node); | |
3801 | tree priority_if_stmt; | |
3802 | int priority; | |
3803 | priority_info pi; | |
3804 | ||
3805 | /* If we don't need a destructor, there's nothing to do. Avoid | |
3806 | creating a possibly empty if-stmt. */ | |
3807 | if (!initp && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl))) | |
3808 | { | |
3809 | node = TREE_CHAIN (node); | |
3810 | continue; | |
3811 | } | |
3812 | ||
3813 | /* Remember that we had an initialization or finalization at this | |
3814 | priority. */ | |
3815 | priority = DECL_EFFECTIVE_INIT_PRIORITY (decl); | |
3816 | pi = get_priority_info (priority); | |
3817 | if (initp) | |
3818 | pi->initializations_p = 1; | |
3819 | else | |
3820 | pi->destructions_p = 1; | |
3821 | ||
3822 | /* Conditionalize this initialization on being in the right priority | |
3823 | and being initializing/finalizing appropriately. */ | |
3824 | priority_if_stmt = begin_if_stmt (); | |
ba47d38d AH |
3825 | cond = cp_build_binary_op (input_location, |
3826 | EQ_EXPR, | |
6cec5cb5 | 3827 | priority_decl, |
5ade1ed2 DG |
3828 | build_int_cst (NULL_TREE, priority), |
3829 | tf_warning_or_error); | |
6cec5cb5 RG |
3830 | finish_if_stmt_cond (cond, priority_if_stmt); |
3831 | ||
3832 | /* Process initializers with same priority. */ | |
3833 | for (; node | |
3834 | && DECL_EFFECTIVE_INIT_PRIORITY (TREE_VALUE (node)) == priority; | |
3835 | node = TREE_CHAIN (node)) | |
3836 | /* Do one initialization or destruction. */ | |
3837 | one_static_initialization_or_destruction (TREE_VALUE (node), | |
3db45ab5 | 3838 | TREE_PURPOSE (node), initp); |
6cec5cb5 RG |
3839 | |
3840 | /* Finish up the priority if-stmt body. */ | |
3841 | finish_then_clause (priority_if_stmt); | |
3842 | finish_if_stmt (priority_if_stmt); | |
3843 | ||
3844 | } while (node); | |
3845 | ||
59b36ecf JJ |
3846 | /* Revert what __asan_before_dynamic_init did by calling |
3847 | __asan_after_dynamic_init. */ | |
94087e88 | 3848 | if (initp && (flag_sanitize & SANITIZE_ADDRESS)) |
59b36ecf JJ |
3849 | finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true)); |
3850 | ||
6cec5cb5 RG |
3851 | /* Finish up the init/destruct if-stmt body. */ |
3852 | finish_then_clause (init_if_stmt); | |
3853 | finish_if_stmt (init_if_stmt); | |
961ec1a5 JM |
3854 | } |
3855 | ||
313bc2c2 MM |
3856 | /* VARS is a list of variables with static storage duration which may |
3857 | need initialization and/or finalization. Remove those variables | |
3858 | that don't really need to be initialized or finalized, and return | |
3859 | the resulting list. The order in which the variables appear in | |
3860 | VARS is in reverse order of the order in which they should actually | |
3861 | be initialized. The list we return is in the unreversed order; | |
3862 | i.e., the first variable should be initialized first. */ | |
8d08fdba | 3863 | |
313bc2c2 | 3864 | static tree |
cec24319 | 3865 | prune_vars_needing_no_initialization (tree *vars) |
8d08fdba | 3866 | { |
cec24319 MM |
3867 | tree *var = vars; |
3868 | tree result = NULL_TREE; | |
8d08fdba | 3869 | |
cec24319 | 3870 | while (*var) |
313bc2c2 | 3871 | { |
cec24319 MM |
3872 | tree t = *var; |
3873 | tree decl = TREE_VALUE (t); | |
3874 | tree init = TREE_PURPOSE (t); | |
8d2733ca | 3875 | |
313bc2c2 | 3876 | /* Deal gracefully with error. */ |
b7280ac2 | 3877 | if (error_operand_p (decl)) |
cec24319 MM |
3878 | { |
3879 | var = &TREE_CHAIN (t); | |
3880 | continue; | |
3881 | } | |
5566b478 | 3882 | |
313bc2c2 | 3883 | /* The only things that can be initialized are variables. */ |
5a6ccc94 | 3884 | gcc_assert (VAR_P (decl)); |
8d08fdba | 3885 | |
313bc2c2 MM |
3886 | /* If this object is not defined, we don't need to do anything |
3887 | here. */ | |
3888 | if (DECL_EXTERNAL (decl)) | |
cec24319 MM |
3889 | { |
3890 | var = &TREE_CHAIN (t); | |
3891 | continue; | |
3892 | } | |
8d08fdba | 3893 | |
313bc2c2 MM |
3894 | /* Also, if the initializer already contains errors, we can bail |
3895 | out now. */ | |
c8094d83 | 3896 | if (init && TREE_CODE (init) == TREE_LIST |
313bc2c2 | 3897 | && value_member (error_mark_node, init)) |
cec24319 MM |
3898 | { |
3899 | var = &TREE_CHAIN (t); | |
3900 | continue; | |
3901 | } | |
d18c083e | 3902 | |
313bc2c2 MM |
3903 | /* This variable is going to need initialization and/or |
3904 | finalization, so we add it to the list. */ | |
cec24319 MM |
3905 | *var = TREE_CHAIN (t); |
3906 | TREE_CHAIN (t) = result; | |
3907 | result = t; | |
313bc2c2 | 3908 | } |
1139b3d8 | 3909 | |
313bc2c2 MM |
3910 | return result; |
3911 | } | |
1139b3d8 | 3912 | |
313bc2c2 MM |
3913 | /* Make sure we have told the back end about all the variables in |
3914 | VARS. */ | |
0aafb128 | 3915 | |
313bc2c2 | 3916 | static void |
848eed92 | 3917 | write_out_vars (tree vars) |
313bc2c2 MM |
3918 | { |
3919 | tree v; | |
0aafb128 | 3920 | |
313bc2c2 | 3921 | for (v = vars; v; v = TREE_CHAIN (v)) |
4684cd27 MM |
3922 | { |
3923 | tree var = TREE_VALUE (v); | |
3924 | if (!var_finalized_p (var)) | |
3925 | { | |
3926 | import_export_decl (var); | |
0e6df31e | 3927 | rest_of_decl_compilation (var, 1, 1); |
4684cd27 MM |
3928 | } |
3929 | } | |
0aafb128 | 3930 | } |
909e536a | 3931 | |
0aafb128 | 3932 | /* Generate a static constructor (if CONSTRUCTOR_P) or destructor |
44cbf6e4 | 3933 | (otherwise) that will initialize all global objects with static |
0aafb128 | 3934 | storage duration having the indicated PRIORITY. */ |
73aad9b9 | 3935 | |
0aafb128 | 3936 | static void |
299f79b5 NS |
3937 | generate_ctor_or_dtor_function (bool constructor_p, int priority, |
3938 | location_t *locus) | |
0aafb128 MM |
3939 | { |
3940 | char function_key; | |
35b4a576 | 3941 | tree fndecl; |
914653a2 | 3942 | tree body; |
0352cfc8 | 3943 | size_t i; |
73aad9b9 | 3944 | |
82a98427 | 3945 | input_location = *locus; |
93409b8c | 3946 | /* ??? */ |
2d593c86 | 3947 | /* Was: locus->line++; */ |
c8094d83 | 3948 | |
0aafb128 MM |
3949 | /* We use `I' to indicate initialization and `D' to indicate |
3950 | destruction. */ | |
35b4a576 | 3951 | function_key = constructor_p ? 'I' : 'D'; |
73aad9b9 | 3952 | |
35b4a576 RS |
3953 | /* We emit the function lazily, to avoid generating empty |
3954 | global constructors and destructors. */ | |
3955 | body = NULL_TREE; | |
2c73f9f5 | 3956 | |
e58a9aa1 ZL |
3957 | /* For Objective-C++, we may need to initialize metadata found in this module. |
3958 | This must be done _before_ any other static initializations. */ | |
3959 | if (c_dialect_objc () && (priority == DEFAULT_INIT_PRIORITY) | |
3960 | && constructor_p && objc_static_init_needed_p ()) | |
3961 | { | |
3962 | body = start_objects (function_key, priority); | |
fc8600f9 | 3963 | objc_generate_static_init_call (NULL_TREE); |
e58a9aa1 ZL |
3964 | } |
3965 | ||
0aafb128 MM |
3966 | /* Call the static storage duration function with appropriate |
3967 | arguments. */ | |
9771b263 | 3968 | FOR_EACH_VEC_SAFE_ELT (ssdf_decls, i, fndecl) |
1bc5f355 KH |
3969 | { |
3970 | /* Calls to pure or const functions will expand to nothing. */ | |
3971 | if (! (flags_from_decl_or_type (fndecl) & (ECF_CONST | ECF_PURE))) | |
3972 | { | |
450f4293 NF |
3973 | tree call; |
3974 | ||
1bc5f355 KH |
3975 | if (! body) |
3976 | body = start_objects (function_key, priority); | |
3977 | ||
450f4293 NF |
3978 | call = cp_build_function_call_nary (fndecl, tf_warning_or_error, |
3979 | build_int_cst (NULL_TREE, | |
3980 | constructor_p), | |
3981 | build_int_cst (NULL_TREE, | |
3982 | priority), | |
3983 | NULL_TREE); | |
3984 | finish_expr_stmt (call); | |
1bc5f355 KH |
3985 | } |
3986 | } | |
909e536a | 3987 | |
0aafb128 | 3988 | /* Close out the function. */ |
35b4a576 RS |
3989 | if (body) |
3990 | finish_objects (function_key, priority, body); | |
0aafb128 | 3991 | } |
44a8d0b3 | 3992 | |
0aafb128 | 3993 | /* Generate constructor and destructor functions for the priority |
0352cfc8 | 3994 | indicated by N. */ |
44a8d0b3 | 3995 | |
0aafb128 | 3996 | static int |
299f79b5 | 3997 | generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, void * data) |
0aafb128 | 3998 | { |
67f5655f | 3999 | location_t *locus = (location_t *) data; |
0aafb128 MM |
4000 | int priority = (int) n->key; |
4001 | priority_info pi = (priority_info) n->value; | |
0aafb128 MM |
4002 | |
4003 | /* Generate the functions themselves, but only if they are really | |
4004 | needed. */ | |
fc8600f9 | 4005 | if (pi->initializations_p) |
299f79b5 | 4006 | generate_ctor_or_dtor_function (/*constructor_p=*/true, priority, locus); |
fc8600f9 | 4007 | if (pi->destructions_p) |
299f79b5 | 4008 | generate_ctor_or_dtor_function (/*constructor_p=*/false, priority, locus); |
0aafb128 MM |
4009 | |
4010 | /* Keep iterating. */ | |
4011 | return 0; | |
4012 | } | |
2ce3c6c6 | 4013 | |
36a549b8 AC |
4014 | /* Return C++ property of T, based on given operation OP. */ |
4015 | ||
4016 | static int | |
621955cb | 4017 | cpp_check (tree t, cpp_operation op) |
36a549b8 AC |
4018 | { |
4019 | switch (op) | |
4020 | { | |
c6a2f2d9 PMR |
4021 | case HAS_DEPENDENT_TEMPLATE_ARGS: |
4022 | { | |
4023 | tree ti = CLASSTYPE_TEMPLATE_INFO (t); | |
4024 | if (!ti) | |
4025 | return 0; | |
4026 | ++processing_template_decl; | |
4027 | const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti)); | |
4028 | --processing_template_decl; | |
4029 | return dep; | |
4030 | } | |
36a549b8 AC |
4031 | case IS_ABSTRACT: |
4032 | return DECL_PURE_VIRTUAL_P (t); | |
4033 | case IS_CONSTRUCTOR: | |
4034 | return DECL_CONSTRUCTOR_P (t); | |
4035 | case IS_DESTRUCTOR: | |
4036 | return DECL_DESTRUCTOR_P (t); | |
4037 | case IS_COPY_CONSTRUCTOR: | |
4038 | return DECL_COPY_CONSTRUCTOR_P (t); | |
2a7fb83f EB |
4039 | case IS_MOVE_CONSTRUCTOR: |
4040 | return DECL_MOVE_CONSTRUCTOR_P (t); | |
36a549b8 AC |
4041 | case IS_TEMPLATE: |
4042 | return TREE_CODE (t) == TEMPLATE_DECL; | |
94159ecf EB |
4043 | case IS_TRIVIAL: |
4044 | return trivial_type_p (t); | |
36a549b8 AC |
4045 | default: |
4046 | return 0; | |
4047 | } | |
4048 | } | |
4049 | ||
4050 | /* Collect source file references recursively, starting from NAMESPC. */ | |
4051 | ||
4052 | static void | |
4053 | collect_source_refs (tree namespc) | |
4054 | { | |
36a549b8 | 4055 | /* Iterate over names in this name space. */ |
c957e9c0 NS |
4056 | for (tree t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t)) |
4057 | if (DECL_IS_BUILTIN (t)) | |
4058 | ; | |
4059 | else if (TREE_CODE (t) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (t)) | |
4060 | collect_source_refs (t); | |
4061 | else | |
36a549b8 | 4062 | collect_source_ref (DECL_SOURCE_FILE (t)); |
36a549b8 AC |
4063 | } |
4064 | ||
4065 | /* Collect decls relevant to SOURCE_FILE from all namespaces recursively, | |
4066 | starting from NAMESPC. */ | |
4067 | ||
4068 | static void | |
4069 | collect_ada_namespace (tree namespc, const char *source_file) | |
4070 | { | |
c957e9c0 | 4071 | tree decl = NAMESPACE_LEVEL (namespc)->names; |
36a549b8 | 4072 | |
c957e9c0 NS |
4073 | /* Collect decls from this namespace. This will skip |
4074 | NAMESPACE_DECLs (both aliases and regular, it cannot tell). */ | |
4075 | collect_ada_nodes (decl, source_file); | |
36a549b8 | 4076 | |
c957e9c0 NS |
4077 | /* Now scan for namespace children, and dump them. */ |
4078 | for (; decl; decl = TREE_CHAIN (decl)) | |
4079 | if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl)) | |
4080 | collect_ada_namespace (decl, source_file); | |
36a549b8 AC |
4081 | } |
4082 | ||
e8f43da6 JM |
4083 | /* Returns true iff there is a definition available for variable or |
4084 | function DECL. */ | |
4085 | ||
f065303f | 4086 | bool |
e8f43da6 JM |
4087 | decl_defined_p (tree decl) |
4088 | { | |
4089 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
f065303f JM |
4090 | return (DECL_INITIAL (decl) != NULL_TREE |
4091 | /* A pending instantiation of a friend temploid is defined. */ | |
4092 | || (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl) | |
4093 | && DECL_INITIAL (DECL_TEMPLATE_RESULT | |
4094 | (DECL_TI_TEMPLATE (decl))))); | |
e8f43da6 JM |
4095 | else |
4096 | { | |
5a6ccc94 | 4097 | gcc_assert (VAR_P (decl)); |
e8f43da6 JM |
4098 | return !DECL_EXTERNAL (decl); |
4099 | } | |
4100 | } | |
4101 | ||
aef4a215 JM |
4102 | /* Nonzero for a VAR_DECL whose value can be used in a constant expression. |
4103 | ||
4104 | [expr.const] | |
4105 | ||
4106 | An integral constant-expression can only involve ... const | |
4107 | variables of integral or enumeration types initialized with | |
4108 | constant expressions ... | |
4109 | ||
4110 | C++0x also allows constexpr variables and temporaries initialized | |
4111 | with constant expressions. We handle the former here, but the latter | |
4112 | are just folded away in cxx_eval_constant_expression. | |
4113 | ||
4114 | The standard does not require that the expression be non-volatile. | |
4115 | G++ implements the proposed correction in DR 457. */ | |
4116 | ||
4117 | bool | |
4118 | decl_constant_var_p (tree decl) | |
4119 | { | |
5fb8060d | 4120 | if (!decl_maybe_constant_var_p (decl)) |
aef4a215 | 4121 | return false; |
aef4a215 | 4122 | |
5fb8060d JM |
4123 | /* We don't know if a template static data member is initialized with |
4124 | a constant expression until we instantiate its initializer. Even | |
4125 | in the case of a constexpr variable, we can't treat it as a | |
4126 | constant until its initializer is complete in case it's used in | |
4127 | its own initializer. */ | |
94ced5b4 | 4128 | maybe_instantiate_decl (decl); |
5fb8060d | 4129 | return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl); |
aef4a215 JM |
4130 | } |
4131 | ||
4132 | /* Returns true if DECL could be a symbolic constant variable, depending on | |
4133 | its initializer. */ | |
4134 | ||
4135 | bool | |
4136 | decl_maybe_constant_var_p (tree decl) | |
4137 | { | |
4138 | tree type = TREE_TYPE (decl); | |
5a6ccc94 | 4139 | if (!VAR_P (decl)) |
aef4a215 JM |
4140 | return false; |
4141 | if (DECL_DECLARED_CONSTEXPR_P (decl)) | |
4142 | return true; | |
58688a4e | 4143 | if (DECL_HAS_VALUE_EXPR_P (decl)) |
b6b99021 JM |
4144 | /* A proxy isn't constant. */ |
4145 | return false; | |
fd338b13 JM |
4146 | if (TREE_CODE (type) == REFERENCE_TYPE) |
4147 | /* References can be constant. */ | |
4148 | return true; | |
aef4a215 JM |
4149 | return (CP_TYPE_CONST_NON_VOLATILE_P (type) |
4150 | && INTEGRAL_OR_ENUMERATION_TYPE_P (type)); | |
4151 | } | |
4152 | ||
944b63db JM |
4153 | /* Complain that DECL uses a type with no linkage. In C++98 mode this is |
4154 | called from grokfndecl and grokvardecl; in all modes it is called from | |
4155 | cp_write_global_declarations. */ | |
e8f43da6 | 4156 | |
944b63db | 4157 | void |
e8f43da6 JM |
4158 | no_linkage_error (tree decl) |
4159 | { | |
944b63db JM |
4160 | if (cxx_dialect >= cxx11 && decl_defined_p (decl)) |
4161 | /* In C++11 it's ok if the decl is defined. */ | |
4162 | return; | |
e8f43da6 | 4163 | tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false); |
944b63db JM |
4164 | if (t == NULL_TREE) |
4165 | /* The type that got us on no_linkage_decls must have gotten a name for | |
4166 | linkage purposes. */; | |
4167 | else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t)) | |
4168 | /* The type might end up having a typedef name for linkage purposes. */ | |
4169 | vec_safe_push (no_linkage_decls, decl); | |
6a7b9203 | 4170 | else if (TYPE_UNNAMED_P (t)) |
e8f43da6 | 4171 | { |
944b63db JM |
4172 | bool d = false; |
4173 | if (cxx_dialect >= cxx11) | |
4174 | d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using " | |
6a7b9203 | 4175 | "unnamed type, is used but never defined", decl); |
944b63db JM |
4176 | else if (DECL_EXTERN_C_P (decl)) |
4177 | /* Allow this; it's pretty common in C. */; | |
56a6f1d3 | 4178 | else if (VAR_P (decl)) |
944b63db JM |
4179 | /* DRs 132, 319 and 389 seem to indicate types with |
4180 | no linkage can only be used to declare extern "C" | |
4181 | entities. Since it's not always an error in the | |
4182 | ISO C++ 90 Standard, we only issue a warning. */ | |
6a7b9203 | 4183 | d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "unnamed type " |
944b63db JM |
4184 | "with no linkage used to declare variable %q#D with " |
4185 | "linkage", decl); | |
4186 | else | |
6a7b9203 | 4187 | d = permerror (DECL_SOURCE_LOCATION (decl), "unnamed type with no " |
944b63db JM |
4188 | "linkage used to declare function %q#D with linkage", |
4189 | decl); | |
4190 | if (d && is_typedef_decl (TYPE_NAME (t))) | |
4191 | inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer " | |
4192 | "to the unqualified type, so it is not used for linkage", | |
4193 | TYPE_NAME (t)); | |
e8f43da6 | 4194 | } |
944b63db | 4195 | else if (cxx_dialect >= cxx11) |
f92c7426 | 4196 | { |
56a6f1d3 | 4197 | if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl)) |
f92c7426 PC |
4198 | permerror (DECL_SOURCE_LOCATION (decl), |
4199 | "%q#D, declared using local type " | |
4200 | "%qT, is used but never defined", decl, t); | |
4201 | } | |
56a6f1d3 | 4202 | else if (VAR_P (decl)) |
944b63db JM |
4203 | warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage " |
4204 | "used to declare variable %q#D with linkage", t, decl); | |
e8f43da6 | 4205 | else |
944b63db JM |
4206 | permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used " |
4207 | "to declare function %q#D with linkage", t, decl); | |
e8f43da6 JM |
4208 | } |
4209 | ||
36a549b8 AC |
4210 | /* Collect declarations from all namespaces relevant to SOURCE_FILE. */ |
4211 | ||
4212 | static void | |
4213 | collect_all_refs (const char *source_file) | |
4214 | { | |
4215 | collect_ada_namespace (global_namespace, source_file); | |
4216 | } | |
4217 | ||
e44eb8ff JH |
4218 | /* Clear DECL_EXTERNAL for NODE. */ |
4219 | ||
4220 | static bool | |
12308bc6 | 4221 | clear_decl_external (struct cgraph_node *node, void * /*data*/) |
e44eb8ff | 4222 | { |
67348ccc | 4223 | DECL_EXTERNAL (node->decl) = 0; |
e44eb8ff JH |
4224 | return false; |
4225 | } | |
4226 | ||
7c424acd JM |
4227 | /* Build up the function to run dynamic initializers for thread_local |
4228 | variables in this translation unit and alias the init functions for the | |
4229 | individual variables to it. */ | |
4230 | ||
4231 | static void | |
4232 | handle_tls_init (void) | |
4233 | { | |
4234 | tree vars = prune_vars_needing_no_initialization (&tls_aggregates); | |
4235 | if (vars == NULL_TREE) | |
4236 | return; | |
4237 | ||
4238 | location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars)); | |
4239 | ||
7c424acd JM |
4240 | write_out_vars (vars); |
4241 | ||
4242 | tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"), | |
4243 | boolean_type_node); | |
4244 | TREE_PUBLIC (guard) = false; | |
4245 | TREE_STATIC (guard) = true; | |
4246 | DECL_ARTIFICIAL (guard) = true; | |
4247 | DECL_IGNORED_P (guard) = true; | |
4248 | TREE_USED (guard) = true; | |
3048c0c7 | 4249 | CP_DECL_THREAD_LOCAL_P (guard) = true; |
56363ffd | 4250 | set_decl_tls_model (guard, decl_default_tls_model (guard)); |
2e3757e7 | 4251 | pushdecl_top_level_and_finish (guard, NULL_TREE); |
7c424acd | 4252 | |
5af057d8 | 4253 | tree fn = get_local_tls_init_fn (); |
7c424acd JM |
4254 | start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED); |
4255 | tree body = begin_function_body (); | |
4256 | tree if_stmt = begin_if_stmt (); | |
4257 | tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false, | |
4258 | tf_warning_or_error); | |
4259 | finish_if_stmt_cond (cond, if_stmt); | |
4f2e1536 MP |
4260 | finish_expr_stmt (cp_build_modify_expr (loc, guard, NOP_EXPR, |
4261 | boolean_true_node, | |
7c424acd JM |
4262 | tf_warning_or_error)); |
4263 | for (; vars; vars = TREE_CHAIN (vars)) | |
4264 | { | |
4265 | tree var = TREE_VALUE (vars); | |
4266 | tree init = TREE_PURPOSE (vars); | |
4267 | one_static_initialization_or_destruction (var, init, true); | |
4268 | ||
5af057d8 JM |
4269 | #ifdef ASM_OUTPUT_DEF |
4270 | /* Output init aliases even with -fno-extern-tls-init. */ | |
4271 | if (TREE_PUBLIC (var)) | |
4272 | { | |
4273 | tree single_init_fn = get_tls_init_fn (var); | |
6962711f JM |
4274 | if (single_init_fn == NULL_TREE) |
4275 | continue; | |
5af057d8 | 4276 | cgraph_node *alias |
d52f5295 ML |
4277 | = cgraph_node::get_create (fn)->create_same_body_alias |
4278 | (single_init_fn, fn); | |
5af057d8 JM |
4279 | gcc_assert (alias != NULL); |
4280 | } | |
4281 | #endif | |
7c424acd JM |
4282 | } |
4283 | ||
4284 | finish_then_clause (if_stmt); | |
4285 | finish_if_stmt (if_stmt); | |
4286 | finish_function_body (body); | |
4287 | expand_or_defer_fn (finish_function (0)); | |
4288 | } | |
4289 | ||
613aba20 JM |
4290 | /* We're at the end of compilation, so generate any mangling aliases that |
4291 | we've been saving up, if DECL is going to be output and ID2 isn't | |
4292 | already taken by another declaration. */ | |
4293 | ||
4294 | static void | |
4295 | generate_mangling_alias (tree decl, tree id2) | |
4296 | { | |
4297 | /* If there's a declaration already using this mangled name, | |
4298 | don't create a compatibility alias that conflicts. */ | |
4299 | if (IDENTIFIER_GLOBAL_VALUE (id2)) | |
4300 | return; | |
4301 | ||
4302 | struct cgraph_node *n = NULL; | |
4303 | if (TREE_CODE (decl) == FUNCTION_DECL | |
4304 | && !(n = cgraph_node::get (decl))) | |
4305 | /* Don't create an alias to an unreferenced function. */ | |
4306 | return; | |
4307 | ||
4308 | tree alias = make_alias_for (decl, id2); | |
4309 | SET_IDENTIFIER_GLOBAL_VALUE (id2, alias); | |
4310 | DECL_IGNORED_P (alias) = 1; | |
4311 | TREE_PUBLIC (alias) = TREE_PUBLIC (decl); | |
4312 | DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl); | |
4313 | if (vague_linkage_p (decl)) | |
4314 | DECL_WEAK (alias) = 1; | |
4315 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
4316 | n->create_same_body_alias (alias, decl); | |
4317 | else | |
4318 | varpool_node::create_extra_name_alias (alias, decl); | |
4319 | } | |
4320 | ||
4321 | /* Note that we might want to emit an alias with the symbol ID2 for DECL at | |
4322 | the end of translation, for compatibility across bugs in the mangling | |
4323 | implementation. */ | |
4324 | ||
4325 | void | |
a31765ef | 4326 | note_mangling_alias (tree decl ATTRIBUTE_UNUSED, tree id2 ATTRIBUTE_UNUSED) |
613aba20 JM |
4327 | { |
4328 | #ifdef ASM_OUTPUT_DEF | |
ec5a0fe0 | 4329 | if (!defer_mangling_aliases) |
613aba20 JM |
4330 | generate_mangling_alias (decl, id2); |
4331 | else | |
4332 | { | |
4333 | vec_safe_push (mangling_aliases, decl); | |
4334 | vec_safe_push (mangling_aliases, id2); | |
4335 | } | |
4336 | #endif | |
4337 | } | |
4338 | ||
ec5a0fe0 JJ |
4339 | /* Emit all mangling aliases that were deferred up to this point. */ |
4340 | ||
4341 | void | |
613aba20 JM |
4342 | generate_mangling_aliases () |
4343 | { | |
4344 | while (!vec_safe_is_empty (mangling_aliases)) | |
4345 | { | |
4346 | tree id2 = mangling_aliases->pop(); | |
4347 | tree decl = mangling_aliases->pop(); | |
4348 | generate_mangling_alias (decl, id2); | |
4349 | } | |
ec5a0fe0 | 4350 | defer_mangling_aliases = false; |
613aba20 JM |
4351 | } |
4352 | ||
fac91973 JM |
4353 | /* The entire file is now complete. If requested, dump everything |
4354 | to a file. */ | |
4355 | ||
4356 | static void | |
4357 | dump_tu (void) | |
4358 | { | |
1a817418 | 4359 | dump_flags_t flags; |
58aca9d9 | 4360 | if (FILE *stream = dump_begin (raw_dump_id, &flags)) |
fac91973 JM |
4361 | { |
4362 | dump_node (global_namespace, flags & ~TDF_SLIM, stream); | |
58aca9d9 | 4363 | dump_end (raw_dump_id, stream); |
fac91973 JM |
4364 | } |
4365 | } | |
4366 | ||
d7438551 AH |
4367 | static location_t locus_at_end_of_parsing; |
4368 | ||
20b06add JM |
4369 | /* Check the deallocation functions for CODE to see if we want to warn that |
4370 | only one was defined. */ | |
4371 | ||
4372 | static void | |
4373 | maybe_warn_sized_delete (enum tree_code code) | |
4374 | { | |
4375 | tree sized = NULL_TREE; | |
4376 | tree unsized = NULL_TREE; | |
4377 | ||
3f267553 NS |
4378 | for (ovl_iterator iter (IDENTIFIER_GLOBAL_VALUE (cp_operator_id (code))); |
4379 | iter; ++iter) | |
20b06add | 4380 | { |
3f267553 | 4381 | tree fn = *iter; |
20b06add | 4382 | /* We're only interested in usual deallocation functions. */ |
af63ba4b | 4383 | if (!usual_deallocation_fn_p (fn)) |
20b06add JM |
4384 | continue; |
4385 | if (FUNCTION_ARG_CHAIN (fn) == void_list_node) | |
4386 | unsized = fn; | |
4387 | else | |
4388 | sized = fn; | |
4389 | } | |
4390 | if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized)) | |
4391 | warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation, | |
4392 | "the program should also define %qD", sized); | |
4393 | else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized)) | |
4394 | warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation, | |
4395 | "the program should also define %qD", unsized); | |
4396 | } | |
4397 | ||
4398 | /* Check the global deallocation functions to see if we want to warn about | |
4399 | defining unsized without sized (or vice versa). */ | |
4400 | ||
4401 | static void | |
4402 | maybe_warn_sized_delete () | |
4403 | { | |
4404 | if (!flag_sized_deallocation || !warn_sized_deallocation) | |
4405 | return; | |
4406 | maybe_warn_sized_delete (DELETE_EXPR); | |
4407 | maybe_warn_sized_delete (VEC_DELETE_EXPR); | |
4408 | } | |
4409 | ||
dcdbc004 JM |
4410 | /* Earlier we left PTRMEM_CST in variable initializers alone so that we could |
4411 | look them up when evaluating non-type template parameters. Now we need to | |
4412 | lower them to something the back end can understand. */ | |
4413 | ||
4414 | static void | |
4415 | lower_var_init () | |
4416 | { | |
4417 | varpool_node *node; | |
4418 | FOR_EACH_VARIABLE (node) | |
4419 | { | |
4420 | tree d = node->decl; | |
4421 | if (tree init = DECL_INITIAL (d)) | |
4422 | DECL_INITIAL (d) = cplus_expand_constant (init); | |
4423 | } | |
4424 | } | |
4425 | ||
e4305460 | 4426 | /* This routine is called at the end of compilation. |
0aafb128 MM |
4427 | Its job is to create all the code needed to initialize and |
4428 | destroy the global aggregates. We do the destruction | |
4429 | first, since that way we only need to reverse the decls once. */ | |
44a8d0b3 | 4430 | |
0aafb128 | 4431 | void |
d7438551 | 4432 | c_parse_final_cleanups (void) |
0aafb128 | 4433 | { |
0aafb128 | 4434 | tree vars; |
848eed92 | 4435 | bool reconsider; |
0aafb128 | 4436 | size_t i; |
299f79b5 | 4437 | unsigned ssdf_count = 0; |
35046a54 | 4438 | int retries = 0; |
125121e2 | 4439 | tree decl; |
8d08fdba | 4440 | |
d7438551 | 4441 | locus_at_end_of_parsing = input_location; |
0aafb128 | 4442 | at_eof = 1; |
faae18ab | 4443 | |
0aafb128 | 4444 | /* Bad parse errors. Just forget about it. */ |
2a4449a3 | 4445 | if (! global_bindings_p () || current_class_type |
9771b263 | 4446 | || !vec_safe_is_empty (decl_namespace_list)) |
0aafb128 | 4447 | return; |
8d08fdba | 4448 | |
5726ad84 SB |
4449 | /* This is the point to write out a PCH if we're doing that. |
4450 | In that case we do not want to do anything else. */ | |
17211ab5 | 4451 | if (pch_file) |
5726ad84 | 4452 | { |
b5415a9e RB |
4453 | /* Mangle all symbols at PCH creation time. */ |
4454 | symtab_node *node; | |
4455 | FOR_EACH_SYMBOL (node) | |
4456 | if (! is_a <varpool_node *> (node) | |
4457 | || ! DECL_HARD_REGISTER (node->decl)) | |
4458 | DECL_ASSEMBLER_NAME (node->decl); | |
5726ad84 | 4459 | c_common_write_pch (); |
fac91973 | 4460 | dump_tu (); |
6b5b4e9c JJ |
4461 | /* Ensure even the callers don't try to finalize the CU. */ |
4462 | flag_syntax_only = 1; | |
5726ad84 SB |
4463 | return; |
4464 | } | |
17211ab5 | 4465 | |
d7438551 AH |
4466 | timevar_stop (TV_PHASE_PARSING); |
4467 | timevar_start (TV_PHASE_DEFERRED); | |
4468 | ||
3dafb85c | 4469 | symtab->process_same_body_aliases (); |
39e2db00 | 4470 | |
36a549b8 | 4471 | /* Handle -fdump-ada-spec[-slim] */ |
3b78de56 | 4472 | if (flag_dump_ada_spec || flag_dump_ada_spec_slim) |
36a549b8 | 4473 | { |
3b78de56 | 4474 | if (flag_dump_ada_spec_slim) |
36a549b8 AC |
4475 | collect_source_ref (main_input_filename); |
4476 | else | |
4477 | collect_source_refs (global_namespace); | |
4478 | ||
4479 | dump_ada_specs (collect_all_refs, cpp_check); | |
4480 | } | |
4481 | ||
2d593c86 | 4482 | /* FIXME - huh? was input_line -= 1;*/ |
5566b478 | 4483 | |
0aafb128 MM |
4484 | /* We now have to write out all the stuff we put off writing out. |
4485 | These include: | |
d2e5ee5c | 4486 | |
0aafb128 | 4487 | o Template specializations that we have not yet instantiated, |
0cbd7506 | 4488 | but which are needed. |
0aafb128 | 4489 | o Initialization and destruction for non-local objects with |
0cbd7506 | 4490 | static storage duration. (Local objects with static storage |
0aafb128 MM |
4491 | duration are initialized when their scope is first entered, |
4492 | and are cleaned up via atexit.) | |
c8094d83 | 4493 | o Virtual function tables. |
ea735e02 | 4494 | |
0aafb128 MM |
4495 | All of these may cause others to be needed. For example, |
4496 | instantiating one function may cause another to be needed, and | |
0a288b9a | 4497 | generating the initializer for an object may cause templates to be |
0aafb128 | 4498 | instantiated, etc., etc. */ |
8d08fdba | 4499 | |
8fa33dfa | 4500 | emit_support_tinfos (); |
6de9cd9a | 4501 | |
c8094d83 | 4502 | do |
0aafb128 | 4503 | { |
548502d3 | 4504 | tree t; |
2b41c040 | 4505 | tree decl; |
548502d3 | 4506 | |
848eed92 | 4507 | reconsider = false; |
0aafb128 | 4508 | |
1a6580ec MM |
4509 | /* If there are templates that we've put off instantiating, do |
4510 | them now. */ | |
35046a54 | 4511 | instantiate_pending_templates (retries); |
d34cb6a1 | 4512 | ggc_collect (); |
0aafb128 | 4513 | |
6eabb241 | 4514 | /* Write out virtual tables as required. Note that writing out |
0cbd7506 MS |
4515 | the virtual table for a template class may cause the |
4516 | instantiation of members of that class. If we write out | |
4517 | vtables then we remove the class from our list so we don't | |
4518 | have to look at it again. */ | |
35046a54 | 4519 | |
9aad8f83 | 4520 | while (keyed_classes != NULL_TREE |
0cbd7506 MS |
4521 | && maybe_emit_vtables (TREE_VALUE (keyed_classes))) |
4522 | { | |
4523 | reconsider = true; | |
4524 | keyed_classes = TREE_CHAIN (keyed_classes); | |
4525 | } | |
c8094d83 | 4526 | |
9aad8f83 MA |
4527 | t = keyed_classes; |
4528 | if (t != NULL_TREE) | |
0cbd7506 MS |
4529 | { |
4530 | tree next = TREE_CHAIN (t); | |
4531 | ||
4532 | while (next) | |
4533 | { | |
4534 | if (maybe_emit_vtables (TREE_VALUE (next))) | |
4535 | { | |
4536 | reconsider = true; | |
4537 | TREE_CHAIN (t) = TREE_CHAIN (next); | |
4538 | } | |
4539 | else | |
4540 | t = next; | |
4541 | ||
4542 | next = TREE_CHAIN (t); | |
4543 | } | |
4544 | } | |
35046a54 | 4545 | |
a260bce6 | 4546 | /* Write out needed type info variables. We have to be careful |
0cbd7506 MS |
4547 | looping through unemitted decls, because emit_tinfo_decl may |
4548 | cause other variables to be needed. New elements will be | |
4549 | appended, and we remove from the vector those that actually | |
4550 | get emitted. */ | |
9771b263 DN |
4551 | for (i = unemitted_tinfo_decls->length (); |
4552 | unemitted_tinfo_decls->iterate (--i, &t);) | |
94214953 NS |
4553 | if (emit_tinfo_decl (t)) |
4554 | { | |
4555 | reconsider = true; | |
9771b263 | 4556 | unemitted_tinfo_decls->unordered_remove (i); |
94214953 | 4557 | } |
7267d692 | 4558 | |
0aafb128 | 4559 | /* The list of objects with static storage duration is built up |
313bc2c2 MM |
4560 | in reverse order. We clear STATIC_AGGREGATES so that any new |
4561 | aggregates added during the initialization of these will be | |
4562 | initialized in the correct order when we next come around the | |
4563 | loop. */ | |
cec24319 | 4564 | vars = prune_vars_needing_no_initialization (&static_aggregates); |
c472cdfd | 4565 | |
313bc2c2 MM |
4566 | if (vars) |
4567 | { | |
313bc2c2 MM |
4568 | /* We need to start a new initialization function each time |
4569 | through the loop. That's because we need to know which | |
4570 | vtables have been referenced, and TREE_SYMBOL_REFERENCED | |
4571 | isn't computed until a function is finished, and written | |
3b426391 | 4572 | out. That's a deficiency in the back end. When this is |
313bc2c2 MM |
4573 | fixed, these initialization functions could all become |
4574 | inline, with resulting performance improvements. */ | |
299f79b5 NS |
4575 | tree ssdf_body; |
4576 | ||
4577 | /* Set the line and file, so that it is obviously not from | |
4578 | the source file. */ | |
d7438551 | 4579 | input_location = locus_at_end_of_parsing; |
299f79b5 | 4580 | ssdf_body = start_static_storage_duration_function (ssdf_count); |
313bc2c2 MM |
4581 | |
4582 | /* Make sure the back end knows about all the variables. */ | |
4583 | write_out_vars (vars); | |
4584 | ||
4585 | /* First generate code to do all the initializations. */ | |
6cec5cb5 RG |
4586 | if (vars) |
4587 | do_static_initialization_or_destruction (vars, /*initp=*/true); | |
313bc2c2 MM |
4588 | |
4589 | /* Then, generate code to do all the destructions. Do these | |
4590 | in reverse order so that the most recently constructed | |
bf419747 MM |
4591 | variable is the first destroyed. If we're using |
4592 | __cxa_atexit, then we don't need to do this; functions | |
4c0aad2c | 4593 | were registered at initialization time to destroy the |
bf419747 | 4594 | local statics. */ |
6cec5cb5 | 4595 | if (!flag_use_cxa_atexit && vars) |
bf419747 MM |
4596 | { |
4597 | vars = nreverse (vars); | |
6cec5cb5 | 4598 | do_static_initialization_or_destruction (vars, /*initp=*/false); |
bf419747 MM |
4599 | } |
4600 | else | |
4601 | vars = NULL_TREE; | |
313bc2c2 MM |
4602 | |
4603 | /* Finish up the static storage duration function for this | |
4604 | round. */ | |
d7438551 | 4605 | input_location = locus_at_end_of_parsing; |
313bc2c2 MM |
4606 | finish_static_storage_duration_function (ssdf_body); |
4607 | ||
4608 | /* All those initializations and finalizations might cause | |
4609 | us to need more inline functions, more template | |
4610 | instantiations, etc. */ | |
848eed92 | 4611 | reconsider = true; |
299f79b5 | 4612 | ssdf_count++; |
d7438551 | 4613 | /* ??? was: locus_at_end_of_parsing.line++; */ |
0aafb128 | 4614 | } |
c8094d83 | 4615 | |
7c424acd JM |
4616 | /* Now do the same for thread_local variables. */ |
4617 | handle_tls_init (); | |
4618 | ||
4684cd27 MM |
4619 | /* Go through the set of inline functions whose bodies have not |
4620 | been emitted yet. If out-of-line copies of these functions | |
4621 | are required, emit them. */ | |
9771b263 | 4622 | FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl) |
0aafb128 | 4623 | { |
eab5474f | 4624 | /* Does it need synthesizing? */ |
bff54b19 | 4625 | if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl) |
4847609e | 4626 | && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl))) |
0aafb128 MM |
4627 | { |
4628 | /* Even though we're already at the top-level, we push | |
4629 | there again. That way, when we pop back a few lines | |
4630 | hence, all of our state is restored. Otherwise, | |
4631 | finish_function doesn't clean things up, and we end | |
4632 | up with CURRENT_FUNCTION_DECL set. */ | |
4633 | push_to_top_level (); | |
3e3935a9 | 4634 | /* The decl's location will mark where it was first |
0cbd7506 MS |
4635 | needed. Save that so synthesize method can indicate |
4636 | where it was needed from, in case of error */ | |
3e3935a9 | 4637 | input_location = DECL_SOURCE_LOCATION (decl); |
1f84ec23 | 4638 | synthesize_method (decl); |
0aafb128 | 4639 | pop_from_top_level (); |
848eed92 | 4640 | reconsider = true; |
0aafb128 | 4641 | } |
0aafb128 | 4642 | |
7c424acd JM |
4643 | if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl)) |
4644 | generate_tls_wrapper (decl); | |
4645 | ||
a406865a | 4646 | if (!DECL_SAVED_TREE (decl)) |
2f0c006e | 4647 | continue; |
ea56c40c | 4648 | |
3b426391 | 4649 | /* We lie to the back end, pretending that some functions |
eab5474f NS |
4650 | are not defined when they really are. This keeps these |
4651 | functions from being put out unnecessarily. But, we must | |
4652 | stop lying when the functions are referenced, or if they | |
1ef0df47 MM |
4653 | are not comdat since they need to be put out now. If |
4654 | DECL_INTERFACE_KNOWN, then we have already set | |
4655 | DECL_EXTERNAL appropriately, so there's no need to check | |
4656 | again, and we do not want to clear DECL_EXTERNAL if a | |
4657 | previous call to import_export_decl set it. | |
3db45ab5 | 4658 | |
1ef0df47 MM |
4659 | This is done in a separate for cycle, because if some |
4660 | deferred function is contained in another deferred | |
4661 | function later in deferred_fns varray, | |
4662 | rest_of_compilation would skip this function and we | |
4663 | really cannot expand the same function twice. */ | |
4664 | import_export_decl (decl); | |
4cb02ea1 MM |
4665 | if (DECL_NOT_REALLY_EXTERN (decl) |
4666 | && DECL_INITIAL (decl) | |
4684cd27 | 4667 | && decl_needed_p (decl)) |
b2583345 | 4668 | { |
e44eb8ff | 4669 | struct cgraph_node *node, *next; |
b2583345 | 4670 | |
d52f5295 | 4671 | node = cgraph_node::get (decl); |
67348ccc | 4672 | if (node->cpp_implicit_alias) |
d52f5295 | 4673 | node = node->get_alias_target (); |
e44eb8ff | 4674 | |
d52f5295 ML |
4675 | node->call_for_symbol_thunks_and_aliases (clear_decl_external, |
4676 | NULL, true); | |
b66887e4 JJ |
4677 | /* If we mark !DECL_EXTERNAL one of the symbols in some comdat |
4678 | group, we need to mark all symbols in the same comdat group | |
4679 | that way. */ | |
67348ccc | 4680 | if (node->same_comdat_group) |
d52f5295 | 4681 | for (next = dyn_cast<cgraph_node *> (node->same_comdat_group); |
b66887e4 | 4682 | next != node; |
d52f5295 ML |
4683 | next = dyn_cast<cgraph_node *> (next->same_comdat_group)) |
4684 | next->call_for_symbol_thunks_and_aliases (clear_decl_external, | |
4685 | NULL, true); | |
b2583345 | 4686 | } |
21b0c6dc MM |
4687 | |
4688 | /* If we're going to need to write this function out, and | |
4689 | there's already a body for it, create RTL for it now. | |
4690 | (There might be no body if this is a method we haven't | |
4691 | gotten around to synthesizing yet.) */ | |
4692 | if (!DECL_EXTERNAL (decl) | |
4684cd27 | 4693 | && decl_needed_p (decl) |
8cd2462c | 4694 | && !TREE_ASM_WRITTEN (decl) |
d52f5295 | 4695 | && !cgraph_node::get (decl)->definition) |
21b0c6dc | 4696 | { |
8cd2462c JH |
4697 | /* We will output the function; no longer consider it in this |
4698 | loop. */ | |
4699 | DECL_DEFER_OUTPUT (decl) = 0; | |
21b0c6dc MM |
4700 | /* Generate RTL for this function now that we know we |
4701 | need it. */ | |
8cd2462c | 4702 | expand_or_defer_fn (decl); |
21b0c6dc MM |
4703 | /* If we're compiling -fsyntax-only pretend that this |
4704 | function has been written out so that we don't try to | |
4705 | expand it again. */ | |
4706 | if (flag_syntax_only) | |
4707 | TREE_ASM_WRITTEN (decl) = 1; | |
848eed92 | 4708 | reconsider = true; |
21b0c6dc | 4709 | } |
4cb02ea1 MM |
4710 | } |
4711 | ||
30318029 | 4712 | if (wrapup_namespace_globals ()) |
848eed92 | 4713 | reconsider = true; |
0aafb128 MM |
4714 | |
4715 | /* Static data members are just like namespace-scope globals. */ | |
9771b263 | 4716 | FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl) |
0aafb128 | 4717 | { |
6c5613b0 JM |
4718 | if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl) |
4719 | /* Don't write it out if we haven't seen a definition. */ | |
14a2c9aa | 4720 | || (DECL_IN_AGGR_P (decl) && !DECL_INLINE_VAR_P (decl))) |
0aafb128 MM |
4721 | continue; |
4722 | import_export_decl (decl); | |
4684cd27 MM |
4723 | /* If this static data member is needed, provide it to the |
4724 | back end. */ | |
4725 | if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl)) | |
5b767b9d | 4726 | DECL_EXTERNAL (decl) = 0; |
0aafb128 | 4727 | } |
9771b263 DN |
4728 | if (vec_safe_length (pending_statics) != 0 |
4729 | && wrapup_global_declarations (pending_statics->address (), | |
4730 | pending_statics->length ())) | |
848eed92 | 4731 | reconsider = true; |
f6d1b84a | 4732 | |
35046a54 | 4733 | retries++; |
c8094d83 | 4734 | } |
0aafb128 | 4735 | while (reconsider); |
28cbf42c | 4736 | |
dcdbc004 JM |
4737 | lower_var_init (); |
4738 | ||
613aba20 JM |
4739 | generate_mangling_aliases (); |
4740 | ||
9bcb9aae | 4741 | /* All used inline functions must have a definition at this point. */ |
9771b263 | 4742 | FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl) |
eab5474f | 4743 | { |
4684cd27 | 4744 | if (/* Check online inline functions that were actually used. */ |
3146f36f | 4745 | DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl) |
1ef0df47 MM |
4746 | /* If the definition actually was available here, then the |
4747 | fact that the function was not defined merely represents | |
4748 | that for some reason (use of a template repository, | |
4749 | #pragma interface, etc.) we decided not to emit the | |
4750 | definition here. */ | |
4751 | && !DECL_INITIAL (decl) | |
4904b64b JM |
4752 | /* Don't complain if the template was defined. */ |
4753 | && !(DECL_TEMPLATE_INSTANTIATION (decl) | |
4754 | && DECL_INITIAL (DECL_TEMPLATE_RESULT | |
4755 | (template_for_substitution (decl))))) | |
3cfabe60 | 4756 | { |
af718670 PC |
4757 | warning_at (DECL_SOURCE_LOCATION (decl), 0, |
4758 | "inline function %qD used but never defined", decl); | |
d7438551 | 4759 | /* Avoid a duplicate warning from check_global_declaration. */ |
96c993a8 | 4760 | TREE_NO_WARNING (decl) = 1; |
3cfabe60 | 4761 | } |
eab5474f | 4762 | } |
c8094d83 | 4763 | |
e8f43da6 | 4764 | /* So must decls that use a type with no linkage. */ |
9771b263 | 4765 | FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl) |
944b63db | 4766 | no_linkage_error (decl); |
e8f43da6 | 4767 | |
20b06add JM |
4768 | maybe_warn_sized_delete (); |
4769 | ||
977e30bc NP |
4770 | /* Then, do the Objective-C stuff. This is where all the |
4771 | Objective-C module stuff gets generated (symtab, | |
4772 | class/protocol/selector lists etc). This must be done after C++ | |
4773 | templates, destructors etc. so that selectors used in C++ | |
4774 | templates are properly allocated. */ | |
4775 | if (c_dialect_objc ()) | |
4776 | objc_write_global_declarations (); | |
4777 | ||
0352cfc8 MM |
4778 | /* We give C linkage to static constructors and destructors. */ |
4779 | push_lang_context (lang_name_c); | |
4780 | ||
4781 | /* Generate initialization and destruction functions for all | |
4782 | priorities for which they are required. */ | |
4783 | if (priority_info_map) | |
c8094d83 | 4784 | splay_tree_foreach (priority_info_map, |
0352cfc8 | 4785 | generate_ctor_and_dtor_functions_for_priority, |
d7438551 | 4786 | /*data=*/&locus_at_end_of_parsing); |
fc8600f9 MM |
4787 | else if (c_dialect_objc () && objc_static_init_needed_p ()) |
4788 | /* If this is obj-c++ and we need a static init, call | |
4789 | generate_ctor_or_dtor_function. */ | |
4790 | generate_ctor_or_dtor_function (/*constructor_p=*/true, | |
d7438551 AH |
4791 | DEFAULT_INIT_PRIORITY, |
4792 | &locus_at_end_of_parsing); | |
0352cfc8 MM |
4793 | |
4794 | /* We're done with the splay-tree now. */ | |
4795 | if (priority_info_map) | |
4796 | splay_tree_delete (priority_info_map); | |
4797 | ||
86f029aa JM |
4798 | /* Generate any missing aliases. */ |
4799 | maybe_apply_pending_pragma_weaks (); | |
4800 | ||
0352cfc8 MM |
4801 | /* We're done with static constructors, so we can go back to "C++" |
4802 | linkage now. */ | |
4803 | pop_lang_context (); | |
4804 | ||
2077db1b CT |
4805 | if (flag_vtable_verify) |
4806 | { | |
4807 | vtv_recover_class_info (); | |
4808 | vtv_compute_class_hierarchy_transitive_closure (); | |
4809 | vtv_build_vtable_verify_fndecl (); | |
4810 | } | |
4811 | ||
2c5df20f JM |
4812 | perform_deferred_noexcept_checks (); |
4813 | ||
faae18ab MS |
4814 | finish_repo (); |
4815 | ||
97f3003f JM |
4816 | fini_constexpr (); |
4817 | ||
f71f87f9 | 4818 | /* The entire file is now complete. If requested, dump everything |
a1c65f9f | 4819 | to a file. */ |
fac91973 | 4820 | dump_tu (); |
c8094d83 | 4821 | |
8d08fdba | 4822 | if (flag_detailed_statistics) |
27bb8339 JM |
4823 | { |
4824 | dump_tree_statistics (); | |
4825 | dump_time_statistics (); | |
4826 | } | |
d7438551 AH |
4827 | |
4828 | timevar_stop (TV_PHASE_DEFERRED); | |
4829 | timevar_start (TV_PHASE_PARSING); | |
e5e58dbe JM |
4830 | |
4831 | /* Indicate that we're done with front end processing. */ | |
4832 | at_eof = 2; | |
d7438551 AH |
4833 | } |
4834 | ||
4835 | /* Perform any post compilation-proper cleanups for the C++ front-end. | |
4836 | This should really go away. No front-end should need to do | |
4837 | anything past the compilation process. */ | |
4838 | ||
4839 | void | |
4840 | cxx_post_compilation_parsing_cleanups (void) | |
4841 | { | |
4842 | timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS); | |
4843 | ||
4844 | if (flag_vtable_verify) | |
4845 | { | |
4846 | /* Generate the special constructor initialization function that | |
4847 | calls __VLTRegisterPairs, and give it a very high | |
4848 | initialization priority. This must be done after | |
4849 | finalize_compilation_unit so that we have accurate | |
4850 | information about which vtable will actually be emitted. */ | |
4851 | vtv_generate_init_routine (); | |
4852 | } | |
4853 | ||
4854 | input_location = locus_at_end_of_parsing; | |
5bd61841 | 4855 | |
595278be MM |
4856 | if (flag_checking) |
4857 | validate_conversion_obstack (); | |
575bfb00 | 4858 | |
d7438551 | 4859 | timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS); |
8d08fdba | 4860 | } |
51c184be | 4861 | |
018a5803 NS |
4862 | /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the |
4863 | function to call in parse-tree form; it has not yet been | |
4864 | semantically analyzed. ARGS are the arguments to the function. | |
c166b898 ILT |
4865 | They have already been semantically analyzed. This may change |
4866 | ARGS. */ | |
4ba126e4 MM |
4867 | |
4868 | tree | |
9771b263 | 4869 | build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args, |
18140454 | 4870 | tsubst_flags_t complain) |
4ba126e4 | 4871 | { |
d17811fd | 4872 | tree orig_fn; |
9771b263 | 4873 | vec<tree, va_gc> *orig_args = NULL; |
d17811fd | 4874 | tree expr; |
018a5803 | 4875 | tree object; |
4ba126e4 | 4876 | |
d17811fd | 4877 | orig_fn = fn; |
018a5803 | 4878 | object = TREE_OPERAND (fn, 0); |
d17811fd MM |
4879 | |
4880 | if (processing_template_decl) | |
4881 | { | |
50bc768d NS |
4882 | gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR |
4883 | || TREE_CODE (fn) == MEMBER_REF); | |
d17811fd | 4884 | if (type_dependent_expression_p (fn) |
c166b898 | 4885 | || any_type_dependent_arguments_p (*args)) |
58dec010 | 4886 | return build_min_nt_call_vec (fn, *args); |
c166b898 ILT |
4887 | |
4888 | orig_args = make_tree_vector_copy (*args); | |
d17811fd MM |
4889 | |
4890 | /* Transform the arguments and add the implicit "this" | |
4891 | parameter. That must be done before the FN is transformed | |
4892 | because we depend on the form of FN. */ | |
c166b898 | 4893 | make_args_non_dependent (*args); |
8adaafcf | 4894 | object = build_non_dependent_expr (object); |
516d9427 JM |
4895 | if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) |
4896 | { | |
516d9427 | 4897 | if (TREE_CODE (fn) == DOTSTAR_EXPR) |
18140454 | 4898 | object = cp_build_addr_expr (object, complain); |
9771b263 | 4899 | vec_safe_insert (*args, 0, object); |
516d9427 | 4900 | } |
d17811fd MM |
4901 | /* Now that the arguments are done, transform FN. */ |
4902 | fn = build_non_dependent_expr (fn); | |
4903 | } | |
4ba126e4 | 4904 | |
ee76b931 MM |
4905 | /* A qualified name corresponding to a bound pointer-to-member is |
4906 | represented as an OFFSET_REF: | |
4ba126e4 MM |
4907 | |
4908 | struct B { void g(); }; | |
4909 | void (B::*p)(); | |
4910 | void B::g() { (this->*p)(); } */ | |
d17811fd | 4911 | if (TREE_CODE (fn) == OFFSET_REF) |
4ba126e4 | 4912 | { |
18140454 | 4913 | tree object_addr = cp_build_addr_expr (object, complain); |
4ba126e4 | 4914 | fn = TREE_OPERAND (fn, 1); |
89fcabaf | 4915 | fn = get_member_function_from_ptrfunc (&object_addr, fn, |
18140454 | 4916 | complain); |
9771b263 | 4917 | vec_safe_insert (*args, 0, object_addr); |
4ba126e4 | 4918 | } |
4ba126e4 | 4919 | |
516d9427 | 4920 | if (CLASS_TYPE_P (TREE_TYPE (fn))) |
18140454 | 4921 | expr = build_op_call (fn, args, complain); |
516d9427 | 4922 | else |
18140454 | 4923 | expr = cp_build_function_call_vec (fn, args, complain); |
d17811fd | 4924 | if (processing_template_decl && expr != error_mark_node) |
c166b898 ILT |
4925 | expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args); |
4926 | ||
4927 | if (orig_args != NULL) | |
4928 | release_tree_vector (orig_args); | |
4929 | ||
d17811fd | 4930 | return expr; |
4ba126e4 | 4931 | } |
c8094d83 | 4932 | |
f30432d7 MS |
4933 | |
4934 | void | |
848eed92 | 4935 | check_default_args (tree x) |
f30432d7 MS |
4936 | { |
4937 | tree arg = TYPE_ARG_TYPES (TREE_TYPE (x)); | |
848eed92 GDR |
4938 | bool saw_def = false; |
4939 | int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE); | |
f30432d7 MS |
4940 | for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i) |
4941 | { | |
4942 | if (TREE_PURPOSE (arg)) | |
848eed92 | 4943 | saw_def = true; |
c10f4193 | 4944 | else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg))) |
f30432d7 | 4945 | { |
dee15844 | 4946 | error ("default argument missing for parameter %P of %q+#D", i, x); |
607c855e | 4947 | TREE_PURPOSE (arg) = error_mark_node; |
f30432d7 MS |
4948 | } |
4949 | } | |
4950 | } | |
72b7eeff | 4951 | |
4847609e JH |
4952 | /* Return true if function DECL can be inlined. This is used to force |
4953 | instantiation of methods that might be interesting for inlining. */ | |
4954 | bool | |
4955 | possibly_inlined_p (tree decl) | |
4956 | { | |
4957 | gcc_assert (TREE_CODE (decl) == FUNCTION_DECL); | |
4958 | if (DECL_UNINLINABLE (decl)) | |
4959 | return false; | |
bfecd57c | 4960 | if (!optimize) |
4847609e JH |
4961 | return DECL_DECLARED_INLINE_P (decl); |
4962 | /* When optimizing, we might inline everything when flatten | |
4963 | attribute or heuristics inlining for size or autoinlining | |
4964 | is used. */ | |
4965 | return true; | |
4966 | } | |
4967 | ||
94ced5b4 JM |
4968 | /* Normally, we can wait until instantiation-time to synthesize DECL. |
4969 | However, if DECL is a static data member initialized with a constant | |
4970 | or a constexpr function, we need it right now because a reference to | |
4971 | such a data member or a call to such function is not value-dependent. | |
4972 | For a function that uses auto in the return type, we need to instantiate | |
4973 | it to find out its type. For OpenMP user defined reductions, we need | |
4974 | them instantiated for reduction clauses which inline them by hand | |
4975 | directly. */ | |
4976 | ||
4977 | static void | |
4978 | maybe_instantiate_decl (tree decl) | |
4979 | { | |
4980 | if (DECL_LANG_SPECIFIC (decl) | |
4981 | && DECL_TEMPLATE_INFO (decl) | |
4982 | && (decl_maybe_constant_var_p (decl) | |
4983 | || (TREE_CODE (decl) == FUNCTION_DECL | |
4984 | && DECL_OMP_DECLARE_REDUCTION_P (decl)) | |
4985 | || undeduced_auto_decl (decl)) | |
4986 | && !DECL_DECLARED_CONCEPT_P (decl) | |
4987 | && !uses_template_parms (DECL_TI_ARGS (decl))) | |
4988 | { | |
4989 | /* Instantiating a function will result in garbage collection. We | |
4990 | must treat this situation as if we were within the body of a | |
4991 | function so as to avoid collecting live data only referenced from | |
4992 | the stack (such as overload resolution candidates). */ | |
4993 | ++function_depth; | |
4994 | instantiate_decl (decl, /*defer_ok=*/false, | |
4995 | /*expl_inst_class_mem_p=*/false); | |
4996 | --function_depth; | |
4997 | } | |
4998 | } | |
4999 | ||
520c3081 | 5000 | /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program. |
7eab6e7b | 5001 | If DECL is a specialization or implicitly declared class member, |
9965f21f PC |
5002 | generate the actual definition. Return false if something goes |
5003 | wrong, true otherwise. */ | |
024f1251 | 5004 | |
9965f21f | 5005 | bool |
2e649151 | 5006 | mark_used (tree decl, tsubst_flags_t complain) |
72b7eeff | 5007 | { |
7eab6e7b MM |
5008 | /* If DECL is a BASELINK for a single function, then treat it just |
5009 | like the DECL for the function. Otherwise, if the BASELINK is | |
5010 | for an overloaded function, we don't know which function was | |
5011 | actually used until after overload resolution. */ | |
c5ce25ce | 5012 | if (BASELINK_P (decl)) |
7eab6e7b MM |
5013 | { |
5014 | decl = BASELINK_FUNCTIONS (decl); | |
5015 | if (really_overloaded_fn (decl)) | |
9965f21f | 5016 | return true; |
848bf88d | 5017 | decl = OVL_FIRST (decl); |
7eab6e7b MM |
5018 | } |
5019 | ||
3146f36f JM |
5020 | /* Set TREE_USED for the benefit of -Wunused. */ |
5021 | TREE_USED (decl) = 1; | |
6fc9f7aa JJ |
5022 | /* And for structured bindings also the underlying decl. */ |
5023 | if (DECL_DECOMPOSITION_P (decl) && DECL_DECOMP_BASE (decl)) | |
5024 | TREE_USED (DECL_DECOMP_BASE (decl)) = 1; | |
20a0c6f9 PP |
5025 | |
5026 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
5027 | return true; | |
5028 | ||
3146f36f JM |
5029 | if (DECL_CLONED_FUNCTION_P (decl)) |
5030 | TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1; | |
5031 | ||
8d0d1915 JM |
5032 | /* Mark enumeration types as used. */ |
5033 | if (TREE_CODE (decl) == CONST_DECL) | |
5034 | used_types_insert (DECL_CONTEXT (decl)); | |
5035 | ||
b15ea309 JM |
5036 | if (TREE_CODE (decl) == FUNCTION_DECL) |
5037 | maybe_instantiate_noexcept (decl); | |
5038 | ||
4ad610c9 JM |
5039 | if (TREE_CODE (decl) == FUNCTION_DECL |
5040 | && DECL_DELETED_FN (decl)) | |
5041 | { | |
c6be04ad JM |
5042 | if (DECL_ARTIFICIAL (decl)) |
5043 | { | |
5044 | if (DECL_OVERLOADED_OPERATOR_P (decl) == TYPE_EXPR | |
5045 | && LAMBDA_TYPE_P (DECL_CONTEXT (decl))) | |
5046 | { | |
5047 | /* We mark a lambda conversion op as deleted if we can't | |
5048 | generate it properly; see maybe_add_lambda_conv_op. */ | |
5049 | sorry ("converting lambda which uses %<...%> to " | |
5050 | "function pointer"); | |
9965f21f | 5051 | return false; |
c6be04ad JM |
5052 | } |
5053 | } | |
2e649151 PC |
5054 | if (complain & tf_error) |
5055 | { | |
5056 | error ("use of deleted function %qD", decl); | |
5057 | if (!maybe_explain_implicit_delete (decl)) | |
5058 | inform (DECL_SOURCE_LOCATION (decl), "declared here"); | |
5059 | } | |
9965f21f | 5060 | return false; |
4ad610c9 | 5061 | } |
3368cdd3 | 5062 | |
5f7282e2 JM |
5063 | if (TREE_DEPRECATED (decl) && (complain & tf_warning) |
5064 | && deprecated_state != DEPRECATED_SUPPRESS) | |
5065 | warn_deprecated_use (decl, NULL_TREE); | |
5066 | ||
3146f36f JM |
5067 | /* We can only check DECL_ODR_USED on variables or functions with |
5068 | DECL_LANG_SPECIFIC set, and these are also the only decls that we | |
5069 | might need special handling for. */ | |
cb6da767 | 5070 | if (!VAR_OR_FUNCTION_DECL_P (decl) |
d9fbd03b JM |
5071 | || DECL_LANG_SPECIFIC (decl) == NULL |
5072 | || DECL_THUNK_P (decl)) | |
852497a3 | 5073 | { |
bc61048a JM |
5074 | if (!processing_template_decl |
5075 | && !require_deduced_type (decl, complain)) | |
5076 | return false; | |
852497a3 JM |
5077 | return true; |
5078 | } | |
3146f36f | 5079 | |
15305a73 JM |
5080 | /* We only want to do this processing once. We don't need to keep trying |
5081 | to instantiate inline templates, because unit-at-a-time will make sure | |
5082 | we get them compiled before functions that want to inline them. */ | |
3146f36f | 5083 | if (DECL_ODR_USED (decl)) |
9965f21f | 5084 | return true; |
15305a73 | 5085 | |
aef4a215 JM |
5086 | /* Normally, we can wait until instantiation-time to synthesize DECL. |
5087 | However, if DECL is a static data member initialized with a constant | |
5088 | or a constexpr function, we need it right now because a reference to | |
852497a3 JM |
5089 | such a data member or a call to such function is not value-dependent. |
5090 | For a function that uses auto in the return type, we need to instantiate | |
acf0174b JJ |
5091 | it to find out its type. For OpenMP user defined reductions, we need |
5092 | them instantiated for reduction clauses which inline them by hand | |
5093 | directly. */ | |
94ced5b4 | 5094 | maybe_instantiate_decl (decl); |
3db45ab5 | 5095 | |
1b82e09e | 5096 | if (processing_template_decl || in_template_function ()) |
c8d15a72 JM |
5097 | return true; |
5098 | ||
234bef96 | 5099 | /* Check this too in case we're within instantiate_non_dependent_expr. */ |
c8d15a72 JM |
5100 | if (DECL_TEMPLATE_INFO (decl) |
5101 | && uses_template_parms (DECL_TI_ARGS (decl))) | |
5102 | return true; | |
5103 | ||
bc61048a JM |
5104 | if (!require_deduced_type (decl, complain)) |
5105 | return false; | |
852497a3 | 5106 | |
f0c1ade4 JM |
5107 | if (builtin_pack_fn_p (decl)) |
5108 | { | |
5109 | error ("use of built-in parameter pack %qD outside of a template", | |
5110 | DECL_NAME (decl)); | |
5111 | return false; | |
5112 | } | |
5113 | ||
aef4a215 | 5114 | /* If we don't need a value, then we don't need to synthesize DECL. */ |
38285dd7 | 5115 | if (cp_unevaluated_operand || in_discarded_stmt) |
9965f21f | 5116 | return true; |
aef4a215 | 5117 | |
3146f36f | 5118 | DECL_ODR_USED (decl) = 1; |
15305a73 | 5119 | if (DECL_CLONED_FUNCTION_P (decl)) |
3146f36f | 5120 | DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1; |
15305a73 | 5121 | |
e8f43da6 JM |
5122 | /* DR 757: A type without linkage shall not be used as the type of a |
5123 | variable or function with linkage, unless | |
5124 | o the variable or function has extern "C" linkage (7.5 [dcl.link]), or | |
5125 | o the variable or function is not used (3.2 [basic.def.odr]) or is | |
5126 | defined in the same translation unit. */ | |
1eee69dd JM |
5127 | if (cxx_dialect > cxx98 |
5128 | && decl_linkage (decl) != lk_none | |
d9fbd03b JM |
5129 | && !DECL_EXTERN_C_P (decl) |
5130 | && !DECL_ARTIFICIAL (decl) | |
5131 | && !decl_defined_p (decl) | |
5132 | && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false)) | |
5133 | { | |
5134 | if (is_local_extern (decl)) | |
5135 | /* There's no way to define a local extern, and adding it to | |
5136 | the vector interferes with GC, so give an error now. */ | |
5137 | no_linkage_error (decl); | |
5138 | else | |
9771b263 | 5139 | vec_safe_push (no_linkage_decls, decl); |
e8f43da6 JM |
5140 | } |
5141 | ||
eab5474f | 5142 | if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl) |
783a452c | 5143 | && !DECL_INITIAL (decl) && !DECL_ARTIFICIAL (decl)) |
eab5474f | 5144 | /* Remember it, so we can check it was defined. */ |
783a452c | 5145 | note_vague_linkage_fn (decl); |
c8094d83 | 5146 | |
73aad9b9 | 5147 | /* Is it a synthesized method that needs to be synthesized? */ |
4f1c5b7d MM |
5148 | if (TREE_CODE (decl) == FUNCTION_DECL |
5149 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl) | |
b87d79e6 | 5150 | && DECL_DEFAULTED_FN (decl) |
767de87d JM |
5151 | /* A function defaulted outside the class is synthesized either by |
5152 | cp_finish_decl or instantiate_decl. */ | |
5153 | && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl) | |
b49bd379 | 5154 | && ! DECL_INITIAL (decl)) |
db9b2174 | 5155 | { |
99c18869 JM |
5156 | /* Defer virtual destructors so that thunks get the right |
5157 | linkage. */ | |
5158 | if (DECL_VIRTUAL_P (decl) && !at_eof) | |
5159 | { | |
5160 | note_vague_linkage_fn (decl); | |
5161 | return true; | |
5162 | } | |
5163 | ||
783a452c JM |
5164 | /* Remember the current location for a function we will end up |
5165 | synthesizing. Then we can inform the user where it was | |
5166 | required in the case of error. */ | |
5167 | DECL_SOURCE_LOCATION (decl) = input_location; | |
5168 | ||
595de302 JM |
5169 | /* Synthesizing an implicitly defined member function will result in |
5170 | garbage collection. We must treat this situation as if we were | |
5171 | within the body of a function so as to avoid collecting live data | |
5172 | on the stack (such as overload resolution candidates). | |
5173 | ||
62906aaa | 5174 | We could just let cp_write_global_declarations handle synthesizing |
99c18869 | 5175 | this function by adding it to deferred_fns, but doing |
62906aaa | 5176 | it at the use site produces better error messages. */ |
595de302 | 5177 | ++function_depth; |
db9b2174 | 5178 | synthesize_method (decl); |
595de302 | 5179 | --function_depth; |
62906aaa | 5180 | /* If this is a synthesized method we don't need to |
5a8613b2 MM |
5181 | do the instantiation test below. */ |
5182 | } | |
cb6da767 | 5183 | else if (VAR_OR_FUNCTION_DECL_P (decl) |
d9fbd03b | 5184 | && DECL_TEMPLATE_INFO (decl) |
971e17ff | 5185 | && !DECL_DECLARED_CONCEPT_P (decl) |
5a8613b2 | 5186 | && (!DECL_EXPLICIT_INSTANTIATION (decl) |
15305a73 | 5187 | || always_instantiate_p (decl))) |
5a8613b2 MM |
5188 | /* If this is a function or variable that is an instance of some |
5189 | template, we now know that we will need to actually do the | |
5190 | instantiation. We check that DECL is not an explicit | |
5191 | instantiation because that is not checked in instantiate_decl. | |
3db45ab5 | 5192 | |
5a8613b2 | 5193 | We put off instantiating functions in order to improve compile |
4684cd27 MM |
5194 | times. Maintaining a stack of active functions is expensive, |
5195 | and the inliner knows to instantiate any functions it might | |
5a8613b2 | 5196 | need. Therefore, we always try to defer instantiation. */ |
b6c917ff JM |
5197 | { |
5198 | ++function_depth; | |
5199 | instantiate_decl (decl, /*defer_ok=*/true, | |
5200 | /*expl_inst_class_mem_p=*/false); | |
5201 | --function_depth; | |
5202 | } | |
9965f21f PC |
5203 | |
5204 | return true; | |
72b7eeff | 5205 | } |
f62dbf03 | 5206 | |
2e649151 PC |
5207 | bool |
5208 | mark_used (tree decl) | |
5209 | { | |
5210 | return mark_used (decl, tf_warning_or_error); | |
5211 | } | |
5212 | ||
2077db1b CT |
5213 | tree |
5214 | vtv_start_verification_constructor_init_function (void) | |
5215 | { | |
5216 | return start_objects ('I', MAX_RESERVED_INIT_PRIORITY - 1); | |
5217 | } | |
5218 | ||
5219 | tree | |
5220 | vtv_finish_verification_constructor_init_function (tree function_body) | |
5221 | { | |
5222 | tree fn; | |
5223 | ||
5224 | finish_compound_stmt (function_body); | |
5225 | fn = finish_function (0); | |
5226 | DECL_STATIC_CONSTRUCTOR (fn) = 1; | |
5227 | decl_init_priority_insert (fn, MAX_RESERVED_INIT_PRIORITY - 1); | |
5228 | ||
5229 | return fn; | |
5230 | } | |
5231 | ||
e2500fed | 5232 | #include "gt-cp-decl2.h" |