]>
Commit | Line | Data |
---|---|---|
3fd5abcf | 1 | /* Process declarations and variables for C++ compiler. |
d6a8bdff | 2 | Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, |
86ac0575 | 3 | 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc. |
8d08fdba MS |
4 | Hacked by Michael Tiemann (tiemann@cygnus.com) |
5 | ||
1c313945 | 6 | This file is part of GCC. |
8d08fdba | 7 | |
1c313945 | 8 | GCC is free software; you can redistribute it and/or modify |
8d08fdba MS |
9 | it under the terms of the GNU General Public License as published by |
10 | the Free Software Foundation; either version 2, or (at your option) | |
11 | any later version. | |
12 | ||
1c313945 | 13 | GCC is distributed in the hope that it will be useful, |
8d08fdba MS |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
1c313945 | 19 | along with GCC; see the file COPYING. If not, write to |
e9fa0c7c RK |
20 | the Free Software Foundation, 59 Temple Place - Suite 330, |
21 | Boston, MA 02111-1307, USA. */ | |
8d08fdba MS |
22 | |
23 | ||
1c313945 | 24 | /* Process declarations and symbol lookup for C++ front end. |
8d08fdba MS |
25 | Also constructs types; the standard scalar types at initialization, |
26 | and structure, union, array and enum types when they are declared. */ | |
27 | ||
28 | /* ??? not all decl nodes are given the most useful possible | |
29 | line numbers. For example, the CONST_DECLs for enum values. */ | |
30 | ||
31 | #include "config.h" | |
8d052bc7 | 32 | #include "system.h" |
4977bab6 ZW |
33 | #include "coretypes.h" |
34 | #include "tm.h" | |
8d08fdba MS |
35 | #include "tree.h" |
36 | #include "rtl.h" | |
8f17b5c5 | 37 | #include "expr.h" |
8d08fdba MS |
38 | #include "flags.h" |
39 | #include "cp-tree.h" | |
40 | #include "decl.h" | |
41 | #include "lex.h" | |
e8abc66f | 42 | #include "output.h" |
49c249e1 | 43 | #include "except.h" |
54f92bfb | 44 | #include "toplev.h" |
2a9a326b | 45 | #include "timevar.h" |
297441fd | 46 | #include "cpplib.h" |
672a6f42 | 47 | #include "target.h" |
a5a49440 | 48 | #include "c-common.h" |
cf44ea52 | 49 | extern cpp_reader *parse_in; |
297441fd | 50 | |
0aafb128 MM |
51 | /* This structure contains information about the initializations |
52 | and/or destructions required for a particular priority level. */ | |
53 | typedef struct priority_info_s { | |
838dfd8a | 54 | /* Nonzero if there have been any initializations at this priority |
0352cfc8 MM |
55 | throughout the translation unit. */ |
56 | int initializations_p; | |
838dfd8a | 57 | /* Nonzero if there have been any destructions at this priority |
0352cfc8 MM |
58 | throughout the translation unit. */ |
59 | int destructions_p; | |
0aafb128 MM |
60 | } *priority_info; |
61 | ||
848eed92 GDR |
62 | static void mark_vtable_entries (tree); |
63 | static void grok_function_init (tree, tree); | |
64 | static bool maybe_emit_vtables (tree); | |
848eed92 GDR |
65 | static void add_using_namespace (tree, tree, bool); |
66 | static tree ambiguous_decl (tree, tree, tree,int); | |
67 | static tree build_anon_union_vars (tree); | |
68 | static bool acceptable_java_type (tree); | |
69 | static void output_vtable_inherit (tree); | |
70 | static tree start_objects (int, int); | |
71 | static void finish_objects (int, int, tree); | |
72 | static tree merge_functions (tree, tree); | |
73 | static tree decl_namespace (tree); | |
74 | static tree validate_nonmember_using_decl (tree, tree *, tree *); | |
75 | static void do_nonmember_using_decl (tree, tree, tree, tree, tree *, tree *); | |
76 | static tree start_static_storage_duration_function (void); | |
77 | static void finish_static_storage_duration_function (tree); | |
78 | static priority_info get_priority_info (int); | |
79 | static void do_static_initialization (tree, tree); | |
80 | static void do_static_destruction (tree); | |
81 | static tree start_static_initialization_or_destruction (tree, int); | |
82 | static void finish_static_initialization_or_destruction (tree); | |
83 | static void generate_ctor_or_dtor_function (bool, int); | |
84 | static int generate_ctor_and_dtor_functions_for_priority (splay_tree_node, | |
85 | void *); | |
cec24319 | 86 | static tree prune_vars_needing_no_initialization (tree *); |
848eed92 GDR |
87 | static void write_out_vars (tree); |
88 | static void import_export_class (tree); | |
848eed92 | 89 | static tree get_guard_bits (tree); |
313bc2c2 | 90 | |
8d08fdba MS |
91 | /* A list of static class variables. This is needed, because a |
92 | static class variable can be declared inside the class without | |
306ef644 | 93 | an initializer, and then initialized, statically, outside the class. */ |
e2500fed | 94 | static GTY(()) varray_type pending_statics; |
2c0f17dc MM |
95 | #define pending_statics_used \ |
96 | (pending_statics ? pending_statics->elements_used : 0) | |
8d08fdba | 97 | |
8926095f | 98 | /* A list of functions which were declared inline, but which we |
e92cc029 | 99 | may need to emit outline anyway. */ |
e2500fed | 100 | static GTY(()) varray_type deferred_fns; |
56e770bf MM |
101 | #define deferred_fns_used \ |
102 | (deferred_fns ? deferred_fns->elements_used : 0) | |
8d08fdba | 103 | |
51c184be | 104 | /* Flag used when debugging spew.c */ |
8d08fdba MS |
105 | |
106 | extern int spew_debug; | |
e1cd6e56 | 107 | |
5566b478 MS |
108 | /* Nonzero if we're done parsing and into end-of-file activities. */ |
109 | ||
110 | int at_eof; | |
111 | ||
e1cd6e56 MS |
112 | /* Functions called along with real static constructors and destructors. */ |
113 | ||
f444e36b MM |
114 | tree static_ctors; |
115 | tree static_dtors; | |
30394414 | 116 | |
a1c65f9f | 117 | /* The :: namespace. */ |
30394414 | 118 | |
30394414 | 119 | tree global_namespace; |
8d08fdba | 120 | \f |
8d08fdba MS |
121 | /* Incorporate `const' and `volatile' qualifiers for member functions. |
122 | FUNCTION is a TYPE_DECL or a FUNCTION_DECL. | |
535233a8 NS |
123 | QUALS is a list of qualifiers. Returns any explicit |
124 | top-level qualifiers of the method's this pointer, anything other than | |
125 | TYPE_UNQUALIFIED will be an extension. */ | |
e92cc029 | 126 | |
535233a8 | 127 | int |
848eed92 | 128 | grok_method_quals (tree ctype, tree function, tree quals) |
8d08fdba MS |
129 | { |
130 | tree fntype = TREE_TYPE (function); | |
131 | tree raises = TYPE_RAISES_EXCEPTIONS (fntype); | |
91063b51 MM |
132 | int type_quals = TYPE_UNQUALIFIED; |
133 | int dup_quals = TYPE_UNQUALIFIED; | |
535233a8 | 134 | int this_quals = TYPE_UNQUALIFIED; |
8d08fdba MS |
135 | |
136 | do | |
137 | { | |
91063b51 MM |
138 | int tq = cp_type_qual_from_rid (TREE_VALUE (quals)); |
139 | ||
535233a8 | 140 | if ((type_quals | this_quals) & tq) |
91063b51 | 141 | dup_quals |= tq; |
535233a8 NS |
142 | else if (tq & TYPE_QUAL_RESTRICT) |
143 | this_quals |= tq; | |
8d08fdba | 144 | else |
91063b51 | 145 | type_quals |= tq; |
8d08fdba | 146 | quals = TREE_CHAIN (quals); |
91063b51 | 147 | } |
8d08fdba | 148 | while (quals); |
91063b51 MM |
149 | |
150 | if (dup_quals != TYPE_UNQUALIFIED) | |
33bd39a2 | 151 | error ("duplicate type qualifiers in %s declaration", |
91063b51 MM |
152 | TREE_CODE (function) == FUNCTION_DECL |
153 | ? "member function" : "type"); | |
154 | ||
155 | ctype = cp_build_qualified_type (ctype, type_quals); | |
8d08fdba MS |
156 | fntype = build_cplus_method_type (ctype, TREE_TYPE (fntype), |
157 | (TREE_CODE (fntype) == METHOD_TYPE | |
158 | ? TREE_CHAIN (TYPE_ARG_TYPES (fntype)) | |
159 | : TYPE_ARG_TYPES (fntype))); | |
160 | if (raises) | |
f30432d7 | 161 | fntype = build_exception_variant (fntype, raises); |
8d08fdba MS |
162 | |
163 | TREE_TYPE (function) = fntype; | |
535233a8 | 164 | return this_quals; |
8d08fdba MS |
165 | } |
166 | ||
8d08fdba MS |
167 | /* Warn when -fexternal-templates is used and #pragma |
168 | interface/implementation is not used all the times it should be, | |
169 | inform the user. */ | |
e92cc029 | 170 | |
8d08fdba | 171 | void |
848eed92 | 172 | warn_if_unknown_interface (tree decl) |
8d08fdba MS |
173 | { |
174 | static int already_warned = 0; | |
8ccc31eb MS |
175 | if (already_warned++) |
176 | return; | |
177 | ||
178 | if (flag_alt_external_templates) | |
179 | { | |
3ae18eaf | 180 | tree til = tinst_for_decl (); |
8ccc31eb | 181 | int sl = lineno; |
3b304f5b | 182 | const char *sf = input_filename; |
8ccc31eb | 183 | |
f30432d7 MS |
184 | if (til) |
185 | { | |
3ae18eaf JM |
186 | lineno = TINST_LINE (til); |
187 | input_filename = TINST_FILE (til); | |
f30432d7 | 188 | } |
33bd39a2 | 189 | warning ("template `%#D' instantiated in file without #pragma interface", |
8ccc31eb MS |
190 | decl); |
191 | lineno = sl; | |
192 | input_filename = sf; | |
193 | } | |
194 | else | |
8251199e | 195 | cp_warning_at ("template `%#D' defined in file without #pragma interface", |
8ccc31eb | 196 | decl); |
8d08fdba MS |
197 | } |
198 | ||
199 | /* A subroutine of the parser, to handle a component list. */ | |
e92cc029 | 200 | |
61a127b3 | 201 | void |
848eed92 | 202 | grok_x_components (tree specs) |
8d08fdba | 203 | { |
61a127b3 | 204 | tree t; |
8d08fdba | 205 | |
72a93143 JM |
206 | specs = strip_attrs (specs); |
207 | ||
208 | check_tag_decl (specs); | |
1f8f4a0b | 209 | t = groktypename (build_tree_list (specs, NULL_TREE)); |
36a117a5 | 210 | |
61a127b3 MM |
211 | /* The only case where we need to do anything additional here is an |
212 | anonymous union field, e.g.: `struct S { union { int i; }; };'. */ | |
6bdb8141 | 213 | if (t == NULL_TREE || !ANON_AGGR_TYPE_P (t)) |
61a127b3 | 214 | return; |
8d08fdba | 215 | |
6bdb8141 | 216 | fixup_anonymous_aggr (t); |
721c3b42 | 217 | finish_member_declaration (build_decl (FIELD_DECL, NULL_TREE, t)); |
8d08fdba MS |
218 | } |
219 | ||
8e51619a JM |
220 | /* Build a PARM_DECL with NAME and TYPE, and set DECL_ARG_TYPE |
221 | appropriately. */ | |
222 | ||
223 | tree | |
848eed92 | 224 | cp_build_parm_decl (tree name, tree type) |
8e51619a JM |
225 | { |
226 | tree parm = build_decl (PARM_DECL, name, type); | |
227 | DECL_ARG_TYPE (parm) = type_passed_as (type); | |
228 | return parm; | |
229 | } | |
230 | ||
3ec6bad3 MM |
231 | /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the |
232 | indicated NAME. */ | |
233 | ||
234 | tree | |
848eed92 | 235 | build_artificial_parm (tree name, tree type) |
3ec6bad3 | 236 | { |
8e51619a | 237 | tree parm = cp_build_parm_decl (name, type); |
c727aa5e | 238 | DECL_ARTIFICIAL (parm) = 1; |
a714e5c5 JM |
239 | /* All our artificial parms are implicitly `const'; they cannot be |
240 | assigned to. */ | |
241 | TREE_READONLY (parm) = 1; | |
3ec6bad3 MM |
242 | return parm; |
243 | } | |
244 | ||
711734a9 JM |
245 | /* Constructors for types with virtual baseclasses need an "in-charge" flag |
246 | saying whether this constructor is responsible for initialization of | |
247 | virtual baseclasses or not. All destructors also need this "in-charge" | |
248 | flag, which additionally determines whether or not the destructor should | |
249 | free the memory for the object. | |
250 | ||
251 | This function adds the "in-charge" flag to member function FN if | |
252 | appropriate. It is called from grokclassfn and tsubst. | |
e0fff4b3 JM |
253 | FN must be either a constructor or destructor. |
254 | ||
255 | The in-charge flag follows the 'this' parameter, and is followed by the | |
256 | VTT parm (if any), then the user-written parms. */ | |
711734a9 JM |
257 | |
258 | void | |
848eed92 | 259 | maybe_retrofit_in_chrg (tree fn) |
711734a9 JM |
260 | { |
261 | tree basetype, arg_types, parms, parm, fntype; | |
262 | ||
454fa7a7 MM |
263 | /* If we've already add the in-charge parameter don't do it again. */ |
264 | if (DECL_HAS_IN_CHARGE_PARM_P (fn)) | |
711734a9 JM |
265 | return; |
266 | ||
212e7048 MM |
267 | /* When processing templates we can't know, in general, whether or |
268 | not we're going to have virtual baseclasses. */ | |
269 | if (uses_template_parms (fn)) | |
270 | return; | |
271 | ||
454fa7a7 MM |
272 | /* We don't need an in-charge parameter for constructors that don't |
273 | have virtual bases. */ | |
274 | if (DECL_CONSTRUCTOR_P (fn) | |
275 | && !TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn))) | |
276 | return; | |
711734a9 | 277 | |
711734a9 JM |
278 | arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn)); |
279 | basetype = TREE_TYPE (TREE_VALUE (arg_types)); | |
e0fff4b3 JM |
280 | arg_types = TREE_CHAIN (arg_types); |
281 | ||
282 | parms = TREE_CHAIN (DECL_ARGUMENTS (fn)); | |
283 | ||
284 | /* If this is a subobject constructor or destructor, our caller will | |
285 | pass us a pointer to our VTT. */ | |
286 | if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn))) | |
287 | { | |
288 | parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type); | |
289 | ||
290 | /* First add it to DECL_ARGUMENTS between 'this' and the real args... */ | |
291 | TREE_CHAIN (parm) = parms; | |
292 | parms = parm; | |
293 | ||
294 | /* ...and then to TYPE_ARG_TYPES. */ | |
295 | arg_types = hash_tree_chain (vtt_parm_type, arg_types); | |
296 | ||
297 | DECL_HAS_VTT_PARM_P (fn) = 1; | |
298 | } | |
299 | ||
300 | /* Then add the in-charge parm (before the VTT parm). */ | |
301 | parm = build_artificial_parm (in_charge_identifier, integer_type_node); | |
302 | TREE_CHAIN (parm) = parms; | |
303 | parms = parm; | |
304 | arg_types = hash_tree_chain (integer_type_node, arg_types); | |
305 | ||
306 | /* Insert our new parameter(s) into the list. */ | |
307 | TREE_CHAIN (DECL_ARGUMENTS (fn)) = parms; | |
308 | ||
309 | /* And rebuild the function type. */ | |
711734a9 JM |
310 | fntype = build_cplus_method_type (basetype, TREE_TYPE (TREE_TYPE (fn)), |
311 | arg_types); | |
312 | if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn))) | |
313 | fntype = build_exception_variant (fntype, | |
314 | TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn))); | |
315 | TREE_TYPE (fn) = fntype; | |
454fa7a7 MM |
316 | |
317 | /* Now we've got the in-charge parameter. */ | |
318 | DECL_HAS_IN_CHARGE_PARM_P (fn) = 1; | |
711734a9 JM |
319 | } |
320 | ||
8d08fdba MS |
321 | /* Classes overload their constituent function names automatically. |
322 | When a function name is declared in a record structure, | |
323 | its name is changed to it overloaded name. Since names for | |
324 | constructors and destructors can conflict, we place a leading | |
325 | '$' for destructors. | |
326 | ||
327 | CNAME is the name of the class we are grokking for. | |
328 | ||
329 | FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'. | |
330 | ||
331 | FLAGS contains bits saying what's special about today's | |
332 | arguments. 1 == DESTRUCTOR. 2 == OPERATOR. | |
333 | ||
334 | If FUNCTION is a destructor, then we must add the `auto-delete' field | |
335 | as a second parameter. There is some hair associated with the fact | |
336 | that we must "declare" this variable in the manner consistent with the | |
337 | way the rest of the arguments were declared. | |
338 | ||
339 | QUALS are the qualifiers for the this pointer. */ | |
340 | ||
341 | void | |
848eed92 | 342 | grokclassfn (tree ctype, tree function, enum overload_flags flags, tree quals) |
8d08fdba MS |
343 | { |
344 | tree fn_name = DECL_NAME (function); | |
535233a8 | 345 | int this_quals = TYPE_UNQUALIFIED; |
8d08fdba | 346 | |
cab8bde9 MM |
347 | /* Even within an `extern "C"' block, members get C++ linkage. See |
348 | [dcl.link] for details. */ | |
5d2ed28c | 349 | SET_DECL_LANGUAGE (function, lang_cplusplus); |
cab8bde9 | 350 | |
8d08fdba MS |
351 | if (fn_name == NULL_TREE) |
352 | { | |
8251199e | 353 | error ("name missing for member function"); |
8d08fdba MS |
354 | fn_name = get_identifier ("<anonymous>"); |
355 | DECL_NAME (function) = fn_name; | |
356 | } | |
357 | ||
358 | if (quals) | |
535233a8 | 359 | this_quals = grok_method_quals (ctype, function, quals); |
8d08fdba | 360 | |
8d08fdba MS |
361 | if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE) |
362 | { | |
363 | /* Must add the class instance variable up front. */ | |
364 | /* Right now we just make this a pointer. But later | |
365 | we may wish to make it special. */ | |
535233a8 | 366 | tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (function))); |
bedda2da MM |
367 | tree qual_type; |
368 | tree parm; | |
369 | ||
370 | /* The `this' parameter is implicitly `const'; it cannot be | |
371 | assigned to. */ | |
372 | this_quals |= TYPE_QUAL_CONST; | |
373 | qual_type = cp_build_qualified_type (type, this_quals); | |
3ec6bad3 | 374 | parm = build_artificial_parm (this_identifier, qual_type); |
bedda2da | 375 | c_apply_type_quals_to_decl (this_quals, parm); |
8d08fdba MS |
376 | TREE_CHAIN (parm) = last_function_parms; |
377 | last_function_parms = parm; | |
378 | } | |
379 | ||
711734a9 | 380 | DECL_ARGUMENTS (function) = last_function_parms; |
711734a9 | 381 | DECL_CONTEXT (function) = ctype; |
711734a9 | 382 | |
92643fea MM |
383 | if (flags == DTOR_FLAG) |
384 | DECL_DESTRUCTOR_P (function) = 1; | |
385 | ||
711734a9 | 386 | if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function)) |
535233a8 | 387 | maybe_retrofit_in_chrg (function); |
711734a9 | 388 | |
8d08fdba MS |
389 | if (flags == DTOR_FLAG) |
390 | { | |
0d9eb3ba | 391 | DECL_DESTRUCTOR_P (function) = 1; |
8d08fdba MS |
392 | TYPE_HAS_DESTRUCTOR (ctype) = 1; |
393 | } | |
8d08fdba MS |
394 | } |
395 | ||
8d08fdba MS |
396 | /* Create an ARRAY_REF, checking for the user doing things backwards |
397 | along the way. */ | |
e92cc029 | 398 | |
8d08fdba | 399 | tree |
848eed92 | 400 | grok_array_decl (tree array_expr, tree index_exp) |
8d08fdba MS |
401 | { |
402 | tree type = TREE_TYPE (array_expr); | |
b7484fbe | 403 | tree p1, p2, i1, i2; |
8d08fdba MS |
404 | |
405 | if (type == error_mark_node || index_exp == error_mark_node) | |
406 | return error_mark_node; | |
5156628f | 407 | if (processing_template_decl) |
5566b478 MS |
408 | return build_min (ARRAY_REF, type ? TREE_TYPE (type) : NULL_TREE, |
409 | array_expr, index_exp); | |
410 | ||
8d08fdba MS |
411 | if (type == NULL_TREE) |
412 | { | |
413 | /* Something has gone very wrong. Assume we are mistakenly reducing | |
414 | an expression instead of a declaration. */ | |
8251199e | 415 | error ("parser may be lost: is there a '{' missing somewhere?"); |
8d08fdba MS |
416 | return NULL_TREE; |
417 | } | |
418 | ||
419 | if (TREE_CODE (type) == OFFSET_TYPE | |
420 | || TREE_CODE (type) == REFERENCE_TYPE) | |
421 | type = TREE_TYPE (type); | |
422 | ||
423 | /* If they have an `operator[]', use that. */ | |
3c215895 | 424 | if (IS_AGGR_TYPE (type) || IS_AGGR_TYPE (TREE_TYPE (index_exp))) |
14d22dd6 MM |
425 | return build_new_op (ARRAY_REF, LOOKUP_NORMAL, |
426 | array_expr, index_exp, NULL_TREE); | |
8d08fdba | 427 | |
03d0f4af MM |
428 | /* Otherwise, create an ARRAY_REF for a pointer or array type. It |
429 | is a little-known fact that, if `a' is an array and `i' is an | |
430 | int, you can write `i[a]', which means the same thing as `a[i]'. */ | |
8d08fdba | 431 | |
b7484fbe MS |
432 | if (TREE_CODE (type) == ARRAY_TYPE) |
433 | p1 = array_expr; | |
434 | else | |
b746c5dc | 435 | p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false); |
8d08fdba | 436 | |
b7484fbe MS |
437 | if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE) |
438 | p2 = index_exp; | |
439 | else | |
b746c5dc | 440 | p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false); |
8d08fdba | 441 | |
b746c5dc GDR |
442 | i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr, false); |
443 | i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp, false); | |
8d08fdba | 444 | |
b7484fbe | 445 | if ((p1 && i2) && (i1 && p2)) |
8251199e | 446 | error ("ambiguous conversion for array subscript"); |
8d08fdba | 447 | |
b7484fbe MS |
448 | if (p1 && i2) |
449 | array_expr = p1, index_exp = i2; | |
450 | else if (i1 && p2) | |
451 | array_expr = p2, index_exp = i1; | |
452 | else | |
453 | { | |
33bd39a2 | 454 | error ("invalid types `%T[%T]' for array subscript", |
b7484fbe MS |
455 | type, TREE_TYPE (index_exp)); |
456 | return error_mark_node; | |
457 | } | |
458 | ||
459 | if (array_expr == error_mark_node || index_exp == error_mark_node) | |
8251199e | 460 | error ("ambiguous conversion for array subscript"); |
b7484fbe MS |
461 | |
462 | return build_array_ref (array_expr, index_exp); | |
8d08fdba MS |
463 | } |
464 | ||
465 | /* Given the cast expression EXP, checking out its validity. Either return | |
466 | an error_mark_node if there was an unavoidable error, return a cast to | |
467 | void for trying to delete a pointer w/ the value 0, or return the | |
468 | call to delete. If DOING_VEC is 1, we handle things differently | |
469 | for doing an array delete. If DOING_VEC is 2, they gave us the | |
470 | array size as an argument to delete. | |
471 | Implements ARM $5.3.4. This is called from the parser. */ | |
e92cc029 | 472 | |
8d08fdba | 473 | tree |
848eed92 | 474 | delete_sanity (tree exp, tree size, int doing_vec, int use_global_delete) |
8d08fdba | 475 | { |
02020185 | 476 | tree t, type; |
8d08fdba MS |
477 | /* For a regular vector delete (aka, no size argument) we will pass |
478 | this down as a NULL_TREE into build_vec_delete. */ | |
479 | tree maxindex = NULL_TREE; | |
5566b478 | 480 | |
909e536a MS |
481 | if (exp == error_mark_node) |
482 | return exp; | |
483 | ||
5156628f | 484 | if (processing_template_decl) |
5566b478 MS |
485 | { |
486 | t = build_min (DELETE_EXPR, void_type_node, exp, size); | |
487 | DELETE_EXPR_USE_GLOBAL (t) = use_global_delete; | |
488 | DELETE_EXPR_USE_VEC (t) = doing_vec; | |
489 | return t; | |
490 | } | |
491 | ||
02020185 JM |
492 | if (TREE_CODE (exp) == OFFSET_REF) |
493 | exp = resolve_offset_ref (exp); | |
494 | exp = convert_from_reference (exp); | |
495 | t = stabilize_reference (exp); | |
b746c5dc | 496 | t = build_expr_type_conversion (WANT_POINTER, t, true); |
8d08fdba | 497 | |
02020185 | 498 | if (t == NULL_TREE || t == error_mark_node) |
8d08fdba | 499 | { |
33bd39a2 | 500 | error ("type `%#T' argument given to `delete', expected pointer", |
02020185 JM |
501 | TREE_TYPE (exp)); |
502 | return error_mark_node; | |
8d08fdba MS |
503 | } |
504 | ||
02020185 | 505 | if (doing_vec == 2) |
8d08fdba | 506 | { |
ab76ca54 | 507 | maxindex = cp_build_binary_op (MINUS_EXPR, size, integer_one_node); |
8251199e | 508 | pedwarn ("anachronistic use of array size in vector delete"); |
8d08fdba MS |
509 | } |
510 | ||
02020185 JM |
511 | type = TREE_TYPE (t); |
512 | ||
513 | /* As of Valley Forge, you can delete a pointer to const. */ | |
514 | ||
515 | /* You can't delete functions. */ | |
516 | if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE) | |
8926095f | 517 | { |
cb9a3ff8 | 518 | error ("cannot delete a function. Only pointer-to-objects are valid arguments to `delete'"); |
02020185 | 519 | return error_mark_node; |
8926095f | 520 | } |
02020185 | 521 | |
838dfd8a | 522 | /* Deleting ptr to void is undefined behavior [expr.delete/3]. */ |
7f477e81 | 523 | if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE) |
655dc6ee | 524 | { |
33bd39a2 | 525 | warning ("deleting `%T' is undefined", type); |
655dc6ee JM |
526 | doing_vec = 0; |
527 | } | |
528 | ||
02020185 JM |
529 | /* An array can't have been allocated by new, so complain. */ |
530 | if (TREE_CODE (t) == ADDR_EXPR | |
531 | && TREE_CODE (TREE_OPERAND (t, 0)) == VAR_DECL | |
532 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == ARRAY_TYPE) | |
33bd39a2 | 533 | warning ("deleting array `%#D'", TREE_OPERAND (t, 0)); |
02020185 JM |
534 | |
535 | /* Deleting a pointer with the value zero is valid and has no effect. */ | |
536 | if (integer_zerop (t)) | |
537 | return build1 (NOP_EXPR, void_type_node, t); | |
8d08fdba MS |
538 | |
539 | if (doing_vec) | |
86f45d2c MM |
540 | return build_vec_delete (t, maxindex, sfk_deleting_destructor, |
541 | use_global_delete); | |
8d08fdba | 542 | else |
e3fe84e5 JM |
543 | return build_delete (type, t, sfk_deleting_destructor, |
544 | LOOKUP_NORMAL, use_global_delete); | |
8d08fdba MS |
545 | } |
546 | ||
98c1c668 JM |
547 | /* Report an error if the indicated template declaration is not the |
548 | sort of thing that should be a member template. */ | |
549 | ||
550 | void | |
848eed92 | 551 | check_member_template (tree tmpl) |
98c1c668 JM |
552 | { |
553 | tree decl; | |
554 | ||
555 | my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0); | |
556 | decl = DECL_TEMPLATE_RESULT (tmpl); | |
557 | ||
1701f21e MM |
558 | if (TREE_CODE (decl) == FUNCTION_DECL |
559 | || (TREE_CODE (decl) == TYPE_DECL | |
560 | && IS_AGGR_TYPE (TREE_TYPE (decl)))) | |
98c1c668 JM |
561 | { |
562 | if (current_function_decl) | |
563 | /* 14.5.2.2 [temp.mem] | |
564 | ||
a1c65f9f | 565 | A local class shall not have member templates. */ |
33bd39a2 | 566 | error ("invalid declaration of member template `%#D' in local class", |
98c1c668 JM |
567 | decl); |
568 | ||
1701f21e | 569 | if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl)) |
98c1c668 JM |
570 | { |
571 | /* 14.5.2.3 [temp.mem] | |
572 | ||
573 | A member function template shall not be virtual. */ | |
33bd39a2 | 574 | error |
8251199e | 575 | ("invalid use of `virtual' in template declaration of `%#D'", |
98c1c668 JM |
576 | decl); |
577 | DECL_VIRTUAL_P (decl) = 0; | |
578 | } | |
579 | ||
580 | /* The debug-information generating code doesn't know what to do | |
581 | with member templates. */ | |
582 | DECL_IGNORED_P (tmpl) = 1; | |
583 | } | |
98c1c668 | 584 | else |
33bd39a2 | 585 | error ("template declaration of `%#D'", decl); |
98c1c668 JM |
586 | } |
587 | ||
a1c65f9f | 588 | /* Return true iff TYPE is a valid Java parameter or return type. */ |
eff71ab0 | 589 | |
848eed92 GDR |
590 | static bool |
591 | acceptable_java_type (tree type) | |
eff71ab0 PB |
592 | { |
593 | if (TREE_CODE (type) == VOID_TYPE || TYPE_FOR_JAVA (type)) | |
594 | return 1; | |
23d4e4cc | 595 | if (TREE_CODE (type) == POINTER_TYPE || TREE_CODE (type) == REFERENCE_TYPE) |
eff71ab0 PB |
596 | { |
597 | type = TREE_TYPE (type); | |
598 | if (TREE_CODE (type) == RECORD_TYPE) | |
599 | { | |
824f42ab PB |
600 | tree args; int i; |
601 | if (! TYPE_FOR_JAVA (type)) | |
848eed92 | 602 | return false; |
824f42ab | 603 | if (! CLASSTYPE_TEMPLATE_INFO (type)) |
848eed92 | 604 | return true; |
824f42ab PB |
605 | args = CLASSTYPE_TI_ARGS (type); |
606 | i = TREE_VEC_LENGTH (args); | |
607 | while (--i >= 0) | |
608 | { | |
609 | type = TREE_VEC_ELT (args, i); | |
610 | if (TREE_CODE (type) == POINTER_TYPE) | |
611 | type = TREE_TYPE (type); | |
612 | if (! TYPE_FOR_JAVA (type)) | |
848eed92 | 613 | return false; |
824f42ab | 614 | } |
848eed92 | 615 | return true; |
eff71ab0 PB |
616 | } |
617 | } | |
848eed92 | 618 | return false; |
eff71ab0 PB |
619 | } |
620 | ||
848eed92 | 621 | /* For a METHOD in a Java class CTYPE, return true if |
eff71ab0 | 622 | the parameter and return types are valid Java types. |
848eed92 | 623 | Otherwise, print appropriate error messages, and return false. */ |
eff71ab0 | 624 | |
848eed92 GDR |
625 | bool |
626 | check_java_method (tree method) | |
eff71ab0 | 627 | { |
848eed92 | 628 | bool jerr = false; |
eff71ab0 PB |
629 | tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method)); |
630 | tree ret_type = TREE_TYPE (TREE_TYPE (method)); | |
848eed92 | 631 | if (!acceptable_java_type (ret_type)) |
eff71ab0 | 632 | { |
33bd39a2 | 633 | error ("Java method '%D' has non-Java return type `%T'", |
eff71ab0 | 634 | method, ret_type); |
848eed92 | 635 | jerr = true; |
eff71ab0 PB |
636 | } |
637 | for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types)) | |
638 | { | |
639 | tree type = TREE_VALUE (arg_types); | |
848eed92 | 640 | if (!acceptable_java_type (type)) |
eff71ab0 | 641 | { |
33bd39a2 | 642 | error ("Java method '%D' has non-Java parameter type `%T'", |
eff71ab0 | 643 | method, type); |
848eed92 | 644 | jerr = true; |
eff71ab0 PB |
645 | } |
646 | } | |
848eed92 | 647 | return !jerr; |
eff71ab0 PB |
648 | } |
649 | ||
8d08fdba MS |
650 | /* Sanity check: report error if this function FUNCTION is not |
651 | really a member of the class (CTYPE) it is supposed to belong to. | |
652 | CNAME is the same here as it is for grokclassfn above. */ | |
653 | ||
f30432d7 | 654 | tree |
848eed92 | 655 | check_classfn (tree ctype, tree function) |
8d08fdba | 656 | { |
b9201622 | 657 | int ix; |
03017874 MM |
658 | |
659 | if (DECL_USE_TEMPLATE (function) | |
74b846e0 MM |
660 | && !(TREE_CODE (function) == TEMPLATE_DECL |
661 | && DECL_TEMPLATE_SPECIALIZATION (function)) | |
03017874 MM |
662 | && is_member_template (DECL_TI_TEMPLATE (function))) |
663 | /* Since this is a specialization of a member template, | |
664 | we're not going to find the declaration in the class. | |
665 | For example, in: | |
666 | ||
667 | struct S { template <typename T> void f(T); }; | |
668 | template <> void S::f(int); | |
669 | ||
670 | we're not going to find `S::f(int)', but there's no | |
671 | reason we should, either. We let our callers know we didn't | |
672 | find the method, but we don't complain. */ | |
673 | return NULL_TREE; | |
b9201622 NS |
674 | |
675 | ix = lookup_fnfields_1 (complete_type (ctype), | |
676 | DECL_CONSTRUCTOR_P (function) ? ctor_identifier : | |
677 | DECL_DESTRUCTOR_P (function) ? dtor_identifier : | |
678 | DECL_NAME (function)); | |
679 | ||
680 | if (ix >= 0) | |
8d08fdba | 681 | { |
b9201622 | 682 | tree methods = CLASSTYPE_METHOD_VEC (ctype); |
dd1b7476 | 683 | tree fndecls, fndecl = 0; |
b9201622 NS |
684 | bool is_conv_op; |
685 | const char *format = NULL; | |
686 | ||
14d22dd6 | 687 | push_scope (ctype); |
b9201622 NS |
688 | for (fndecls = TREE_VEC_ELT (methods, ix); |
689 | fndecls; fndecls = OVL_NEXT (fndecls)) | |
690 | { | |
691 | tree p1, p2; | |
692 | ||
693 | fndecl = OVL_CURRENT (fndecls); | |
694 | p1 = TYPE_ARG_TYPES (TREE_TYPE (function)); | |
695 | p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl)); | |
696 | ||
697 | /* We cannot simply call decls_match because this doesn't | |
698 | work for static member functions that are pretending to | |
699 | be methods, and because the name may have been changed by | |
700 | asm("new_name"). */ | |
701 | ||
702 | /* Get rid of the this parameter on functions that become | |
703 | static. */ | |
704 | if (DECL_STATIC_FUNCTION_P (fndecl) | |
705 | && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE) | |
706 | p1 = TREE_CHAIN (p1); | |
707 | ||
708 | if (same_type_p (TREE_TYPE (TREE_TYPE (function)), | |
709 | TREE_TYPE (TREE_TYPE (fndecl))) | |
710 | && compparms (p1, p2) | |
711 | && (DECL_TEMPLATE_SPECIALIZATION (function) | |
712 | == DECL_TEMPLATE_SPECIALIZATION (fndecl)) | |
713 | && (!DECL_TEMPLATE_SPECIALIZATION (function) | |
714 | || (DECL_TI_TEMPLATE (function) | |
715 | == DECL_TI_TEMPLATE (fndecl)))) | |
14d22dd6 | 716 | break; |
b9201622 | 717 | } |
14d22dd6 MM |
718 | pop_scope (ctype); |
719 | if (fndecls) | |
720 | return OVL_CURRENT (fndecls); | |
b9201622 NS |
721 | error ("prototype for `%#D' does not match any in class `%T'", |
722 | function, ctype); | |
723 | is_conv_op = DECL_CONV_FN_P (fndecl); | |
724 | ||
725 | if (is_conv_op) | |
726 | ix = CLASSTYPE_FIRST_CONVERSION_SLOT; | |
727 | fndecls = TREE_VEC_ELT (methods, ix); | |
728 | while (fndecls) | |
8d08fdba | 729 | { |
b9201622 NS |
730 | fndecl = OVL_CURRENT (fndecls); |
731 | fndecls = OVL_NEXT (fndecls); | |
732 | ||
733 | if (!fndecls && is_conv_op) | |
8d08fdba | 734 | { |
b9201622 | 735 | if (TREE_VEC_LENGTH (methods) > ix) |
8d08fdba | 736 | { |
b9201622 NS |
737 | ix++; |
738 | fndecls = TREE_VEC_ELT (methods, ix); | |
739 | if (!DECL_CONV_FN_P (OVL_CURRENT (fndecls))) | |
8145f082 | 740 | { |
b9201622 NS |
741 | fndecls = NULL_TREE; |
742 | is_conv_op = false; | |
8145f082 | 743 | } |
8d08fdba | 744 | } |
b9201622 NS |
745 | else |
746 | is_conv_op = false; | |
8d08fdba | 747 | } |
b9201622 NS |
748 | if (format) |
749 | format = " %#D"; | |
750 | else if (fndecls) | |
751 | format = "candidates are: %#D"; | |
752 | else | |
753 | format = "candidate is: %#D"; | |
754 | cp_error_at (format, fndecl); | |
8d08fdba MS |
755 | } |
756 | } | |
b9201622 NS |
757 | else if (!COMPLETE_TYPE_P (ctype)) |
758 | cxx_incomplete_type_error (function, ctype); | |
8d08fdba | 759 | else |
b9201622 NS |
760 | error ("no `%#D' member function declared in class `%T'", |
761 | function, ctype); | |
8d08fdba | 762 | |
fc378698 | 763 | /* If we did not find the method in the class, add it to avoid |
6b4b3deb MM |
764 | spurious errors (unless the CTYPE is not yet defined, in which |
765 | case we'll only confuse ourselves when the function is declared | |
766 | properly within the class. */ | |
d0f062fb | 767 | if (COMPLETE_TYPE_P (ctype)) |
452a394b | 768 | add_method (ctype, function, /*error_p=*/1); |
f30432d7 | 769 | return NULL_TREE; |
8d08fdba MS |
770 | } |
771 | ||
fa8d6e85 MM |
772 | /* We have just processed the DECL, which is a static data member. |
773 | Its initializer, if present, is INIT. The ASMSPEC_TREE, if | |
774 | present, is the assembly-language name for the data member. | |
cd9f6678 | 775 | FLAGS is as for cp_finish_decl. */ |
fa8d6e85 MM |
776 | |
777 | void | |
848eed92 GDR |
778 | finish_static_data_member_decl (tree decl, tree init, tree asmspec_tree, |
779 | int flags) | |
fa8d6e85 | 780 | { |
fa8d6e85 MM |
781 | my_friendly_assert (TREE_PUBLIC (decl), 0); |
782 | ||
1f6e1acc AS |
783 | DECL_CONTEXT (decl) = current_class_type; |
784 | ||
fa8d6e85 | 785 | /* We cannot call pushdecl here, because that would fill in the |
46ccf50a | 786 | TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do |
fa8d6e85 MM |
787 | the right thing, namely, to put this decl out straight away. */ |
788 | /* current_class_type can be NULL_TREE in case of error. */ | |
92643fea MM |
789 | if (!asmspec_tree && current_class_type) |
790 | DECL_INITIAL (decl) = error_mark_node; | |
791 | ||
fa8d6e85 | 792 | if (! processing_template_decl) |
0aafb128 MM |
793 | { |
794 | if (!pending_statics) | |
795 | VARRAY_TREE_INIT (pending_statics, 32, "pending_statics"); | |
2c0f17dc | 796 | VARRAY_PUSH_TREE (pending_statics, decl); |
0aafb128 MM |
797 | } |
798 | ||
65f36ac8 JM |
799 | if (LOCAL_CLASS_P (current_class_type)) |
800 | pedwarn ("local class `%#T' shall not have static data member `%#D'", | |
801 | current_class_type, decl); | |
802 | ||
fa8d6e85 MM |
803 | /* Static consts need not be initialized in the class definition. */ |
804 | if (init != NULL_TREE && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))) | |
805 | { | |
b1ce3eb2 | 806 | static int explained = 0; |
fa8d6e85 MM |
807 | |
808 | error ("initializer invalid for static member with constructor"); | |
b1ce3eb2 NS |
809 | if (!explained) |
810 | { | |
811 | error ("(an out of class initialization is required)"); | |
812 | explained = 1; | |
813 | } | |
814 | init = NULL_TREE; | |
fa8d6e85 MM |
815 | } |
816 | /* Force the compiler to know when an uninitialized static const | |
817 | member is being used. */ | |
818 | if (CP_TYPE_CONST_P (TREE_TYPE (decl)) && init == 0) | |
819 | TREE_USED (decl) = 1; | |
820 | DECL_INITIAL (decl) = init; | |
821 | DECL_IN_AGGR_P (decl) = 1; | |
fa8d6e85 | 822 | |
cd9f6678 | 823 | cp_finish_decl (decl, init, asmspec_tree, flags); |
fa8d6e85 MM |
824 | } |
825 | ||
8d08fdba | 826 | /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted) |
5c1e5476 | 827 | of a structure component, returning a _DECL node. |
8d08fdba MS |
828 | QUALS is a list of type qualifiers for this decl (such as for declaring |
829 | const member functions). | |
830 | ||
831 | This is done during the parsing of the struct declaration. | |
5c1e5476 | 832 | The _DECL nodes are chained together and the lot of them |
8d08fdba MS |
833 | are ultimately passed to `build_struct' to make the RECORD_TYPE node. |
834 | ||
8d08fdba MS |
835 | If class A defines that certain functions in class B are friends, then |
836 | the way I have set things up, it is B who is interested in permission | |
837 | granted by A. However, it is in A's context that these declarations | |
838 | are parsed. By returning a void_type_node, class A does not attempt | |
839 | to incorporate the declarations of the friends within its structure. | |
840 | ||
841 | DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING | |
842 | CHANGES TO CODE IN `start_method'. */ | |
843 | ||
844 | tree | |
848eed92 GDR |
845 | grokfield (tree declarator, tree declspecs, tree init, tree asmspec_tree, |
846 | tree attrlist) | |
8d08fdba | 847 | { |
59387d2e | 848 | tree value; |
9c0758dd | 849 | const char *asmspec = 0; |
6060a796 | 850 | int flags = LOOKUP_ONLYCONVERTING; |
8d08fdba | 851 | |
cffa8729 | 852 | if (declspecs == NULL_TREE |
7fcdf4c2 MS |
853 | && TREE_CODE (declarator) == SCOPE_REF |
854 | && TREE_CODE (TREE_OPERAND (declarator, 1)) == IDENTIFIER_NODE) | |
cffa8729 MS |
855 | { |
856 | /* Access declaration */ | |
5566b478 MS |
857 | if (! IS_AGGR_TYPE_CODE (TREE_CODE (TREE_OPERAND (declarator, 0)))) |
858 | ; | |
859 | else if (TREE_COMPLEXITY (declarator) == current_class_depth) | |
b74a0560 | 860 | pop_nested_class (); |
cffa8729 MS |
861 | return do_class_using_decl (declarator); |
862 | } | |
863 | ||
8d08fdba MS |
864 | if (init |
865 | && TREE_CODE (init) == TREE_LIST | |
866 | && TREE_VALUE (init) == error_mark_node | |
867 | && TREE_CHAIN (init) == NULL_TREE) | |
cffa8729 | 868 | init = NULL_TREE; |
8d08fdba | 869 | |
91d231cb | 870 | value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist); |
6c30752f | 871 | if (! value || value == error_mark_node) |
3ddfb0e6 MM |
872 | /* friend or constructor went bad. */ |
873 | return value; | |
5add10fd | 874 | if (TREE_TYPE (value) == error_mark_node) |
04d57dd5 ZW |
875 | return error_mark_node; |
876 | ||
877 | if (TREE_CODE (value) == TYPE_DECL && init) | |
878 | { | |
879 | error ("typedef `%D' is initialized (use __typeof__ instead)", value); | |
880 | init = NULL_TREE; | |
881 | } | |
8d08fdba MS |
882 | |
883 | /* Pass friendly classes back. */ | |
884 | if (TREE_CODE (value) == VOID_TYPE) | |
885 | return void_type_node; | |
886 | ||
887 | if (DECL_NAME (value) != NULL_TREE | |
888 | && IDENTIFIER_POINTER (DECL_NAME (value))[0] == '_' | |
889 | && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (value)), "_vptr")) | |
33bd39a2 | 890 | error ("member `%D' conflicts with virtual function table field name", |
3c215895 | 891 | value); |
8d08fdba MS |
892 | |
893 | /* Stash away type declarations. */ | |
894 | if (TREE_CODE (value) == TYPE_DECL) | |
895 | { | |
896 | DECL_NONLOCAL (value) = 1; | |
700f8a87 | 897 | DECL_CONTEXT (value) = current_class_type; |
8145f082 | 898 | |
7a8b1375 NS |
899 | if (CLASS_TYPE_P (TREE_TYPE (value))) |
900 | CLASSTYPE_GOT_SEMICOLON (TREE_TYPE (value)) = 1; | |
901 | ||
9188c363 MM |
902 | if (processing_template_decl) |
903 | value = push_template_decl (value); | |
904 | ||
8d08fdba MS |
905 | return value; |
906 | } | |
907 | ||
8d08fdba MS |
908 | if (DECL_IN_AGGR_P (value)) |
909 | { | |
33bd39a2 | 910 | error ("`%D' is already defined in `%T'", value, |
6b4b3deb | 911 | DECL_CONTEXT (value)); |
8d08fdba MS |
912 | return void_type_node; |
913 | } | |
914 | ||
8d08fdba MS |
915 | if (asmspec_tree) |
916 | asmspec = TREE_STRING_POINTER (asmspec_tree); | |
917 | ||
918 | if (init) | |
919 | { | |
6eabb241 | 920 | if (TREE_CODE (value) == FUNCTION_DECL) |
8d08fdba MS |
921 | { |
922 | grok_function_init (value, init); | |
923 | init = NULL_TREE; | |
924 | } | |
5b605f68 | 925 | else if (pedantic && TREE_CODE (value) != VAR_DECL) |
a0a33927 MS |
926 | /* Already complained in grokdeclarator. */ |
927 | init = NULL_TREE; | |
8d08fdba MS |
928 | else |
929 | { | |
a0a33927 MS |
930 | /* We allow initializers to become parameters to base |
931 | initializers. */ | |
8d08fdba MS |
932 | if (TREE_CODE (init) == TREE_LIST) |
933 | { | |
934 | if (TREE_CHAIN (init) == NULL_TREE) | |
935 | init = TREE_VALUE (init); | |
936 | else | |
937 | init = digest_init (TREE_TYPE (value), init, (tree *)0); | |
938 | } | |
dcba9b0f MM |
939 | |
940 | if (!processing_template_decl) | |
8d08fdba | 941 | { |
dcba9b0f MM |
942 | if (TREE_CODE (init) == CONST_DECL) |
943 | init = DECL_INITIAL (init); | |
944 | else if (TREE_READONLY_DECL_P (init)) | |
945 | init = decl_constant_value (init); | |
946 | else if (TREE_CODE (init) == CONSTRUCTOR) | |
947 | init = digest_init (TREE_TYPE (value), init, (tree *)0); | |
948 | if (init == error_mark_node) | |
949 | /* We must make this look different than `error_mark_node' | |
950 | because `decl_const_value' would mis-interpret it | |
951 | as only meaning that this VAR_DECL is defined. */ | |
952 | init = build1 (NOP_EXPR, TREE_TYPE (value), init); | |
953 | else if (! TREE_CONSTANT (init)) | |
8d08fdba | 954 | { |
dcba9b0f MM |
955 | /* We can allow references to things that are effectively |
956 | static, since references are initialized with the | |
957 | address. */ | |
958 | if (TREE_CODE (TREE_TYPE (value)) != REFERENCE_TYPE | |
959 | || (TREE_STATIC (init) == 0 | |
960 | && (!DECL_P (init) || DECL_EXTERNAL (init) == 0))) | |
961 | { | |
962 | error ("field initializer is not constant"); | |
963 | init = error_mark_node; | |
964 | } | |
8d08fdba MS |
965 | } |
966 | } | |
967 | } | |
968 | } | |
969 | ||
5156628f | 970 | if (processing_template_decl && ! current_function_decl |
5566b478 | 971 | && (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == FUNCTION_DECL)) |
3ac3d9ea | 972 | value = push_template_decl (value); |
5566b478 | 973 | |
45537677 | 974 | if (attrlist) |
91d231cb | 975 | cplus_decl_attributes (&value, attrlist, 0); |
45537677 | 976 | |
8d08fdba MS |
977 | if (TREE_CODE (value) == VAR_DECL) |
978 | { | |
fa8d6e85 | 979 | finish_static_data_member_decl (value, init, asmspec_tree, |
cd9f6678 | 980 | flags); |
8d08fdba MS |
981 | return value; |
982 | } | |
983 | if (TREE_CODE (value) == FIELD_DECL) | |
984 | { | |
985 | if (asmspec) | |
33bd39a2 | 986 | error ("`asm' specifiers are not permitted on non-static data members"); |
8d08fdba MS |
987 | if (DECL_INITIAL (value) == error_mark_node) |
988 | init = error_mark_node; | |
0c58da3e | 989 | cp_finish_decl (value, init, NULL_TREE, flags); |
8d08fdba MS |
990 | DECL_INITIAL (value) = init; |
991 | DECL_IN_AGGR_P (value) = 1; | |
992 | return value; | |
993 | } | |
994 | if (TREE_CODE (value) == FUNCTION_DECL) | |
995 | { | |
6060a796 MS |
996 | if (asmspec) |
997 | { | |
998 | /* This must override the asm specifier which was placed | |
999 | by grokclassfn. Lay this out fresh. */ | |
19e7881c | 1000 | SET_DECL_RTL (value, NULL_RTX); |
92643fea | 1001 | SET_DECL_ASSEMBLER_NAME (value, get_identifier (asmspec)); |
6060a796 | 1002 | } |
271e6f02 NS |
1003 | if (!DECL_FRIEND_P (value)) |
1004 | grok_special_member_properties (value); | |
1005 | ||
cd9f6678 | 1006 | cp_finish_decl (value, init, asmspec_tree, flags); |
8d08fdba MS |
1007 | |
1008 | /* Pass friends back this way. */ | |
1009 | if (DECL_FRIEND_P (value)) | |
1010 | return void_type_node; | |
1011 | ||
1012 | DECL_IN_AGGR_P (value) = 1; | |
1013 | return value; | |
1014 | } | |
a98facb0 | 1015 | abort (); |
8d08fdba MS |
1016 | /* NOTREACHED */ |
1017 | return NULL_TREE; | |
1018 | } | |
1019 | ||
1020 | /* Like `grokfield', but for bitfields. | |
1021 | WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */ | |
1022 | ||
1023 | tree | |
848eed92 | 1024 | grokbitfield (tree declarator, tree declspecs, tree width) |
8d08fdba | 1025 | { |
f30432d7 | 1026 | register tree value = grokdeclarator (declarator, declspecs, BITFIELD, |
91d231cb | 1027 | 0, NULL); |
8d08fdba MS |
1028 | |
1029 | if (! value) return NULL_TREE; /* friends went bad. */ | |
1030 | ||
1031 | /* Pass friendly classes back. */ | |
1032 | if (TREE_CODE (value) == VOID_TYPE) | |
1033 | return void_type_node; | |
1034 | ||
1035 | if (TREE_CODE (value) == TYPE_DECL) | |
1036 | { | |
3855d0d1 | 1037 | error ("cannot declare `%D' to be a bit-field type", value); |
8d08fdba MS |
1038 | return NULL_TREE; |
1039 | } | |
1040 | ||
33bd39a2 | 1041 | /* Usually, finish_struct_1 catches bitfields with invalid types. |
ae58fa02 MM |
1042 | But, in the case of bitfields with function type, we confuse |
1043 | ourselves into thinking they are member functions, so we must | |
1044 | check here. */ | |
1045 | if (TREE_CODE (value) == FUNCTION_DECL) | |
1046 | { | |
3855d0d1 | 1047 | error ("cannot declare bit-field `%D' with function type", |
33bd39a2 | 1048 | DECL_NAME (value)); |
ae58fa02 MM |
1049 | return NULL_TREE; |
1050 | } | |
1051 | ||
8d08fdba MS |
1052 | if (DECL_IN_AGGR_P (value)) |
1053 | { | |
33bd39a2 | 1054 | error ("`%D' is already defined in the class %T", value, |
8d08fdba MS |
1055 | DECL_CONTEXT (value)); |
1056 | return void_type_node; | |
1057 | } | |
1058 | ||
8d08fdba MS |
1059 | if (TREE_STATIC (value)) |
1060 | { | |
3855d0d1 | 1061 | error ("static member `%D' cannot be a bit-field", value); |
8d08fdba MS |
1062 | return NULL_TREE; |
1063 | } | |
cd9f6678 | 1064 | cp_finish_decl (value, NULL_TREE, NULL_TREE, 0); |
8d08fdba MS |
1065 | |
1066 | if (width != error_mark_node) | |
1067 | { | |
5566b478 MS |
1068 | constant_expression_warning (width); |
1069 | DECL_INITIAL (value) = width; | |
162bc98d | 1070 | SET_DECL_C_BIT_FIELD (value); |
8d08fdba MS |
1071 | } |
1072 | ||
1073 | DECL_IN_AGGR_P (value) = 1; | |
1074 | return value; | |
1075 | } | |
1076 | ||
8d08fdba MS |
1077 | /* When a function is declared with an initializer, |
1078 | do the right thing. Currently, there are two possibilities: | |
1079 | ||
1080 | class B | |
1081 | { | |
1082 | public: | |
1083 | // initialization possibility #1. | |
1084 | virtual void f () = 0; | |
1085 | int g (); | |
1086 | }; | |
1087 | ||
1088 | class D1 : B | |
1089 | { | |
1090 | public: | |
1091 | int d1; | |
1092 | // error, no f (); | |
1093 | }; | |
1094 | ||
1095 | class D2 : B | |
1096 | { | |
1097 | public: | |
1098 | int d2; | |
1099 | void f (); | |
1100 | }; | |
1101 | ||
1102 | class D3 : B | |
1103 | { | |
1104 | public: | |
1105 | int d3; | |
1106 | // initialization possibility #2 | |
1107 | void f () = B::f; | |
1108 | }; | |
1109 | ||
1110 | */ | |
1111 | ||
1112 | static void | |
848eed92 | 1113 | grok_function_init (tree decl, tree init) |
8d08fdba MS |
1114 | { |
1115 | /* An initializer for a function tells how this function should | |
1116 | be inherited. */ | |
1117 | tree type = TREE_TYPE (decl); | |
8d08fdba MS |
1118 | |
1119 | if (TREE_CODE (type) == FUNCTION_TYPE) | |
33bd39a2 | 1120 | error ("initializer specified for non-member function `%D'", decl); |
8d08fdba | 1121 | else if (integer_zerop (init)) |
271e6f02 | 1122 | DECL_PURE_VIRTUAL_P (decl) = 1; |
8d08fdba | 1123 | else |
33bd39a2 | 1124 | error ("invalid initializer for virtual method `%D'", decl); |
8d08fdba MS |
1125 | } |
1126 | \f | |
28cbf42c | 1127 | void |
848eed92 | 1128 | cplus_decl_attributes (tree *decl, tree attributes, int flags) |
8d08fdba | 1129 | { |
59387d2e | 1130 | if (*decl == NULL_TREE || *decl == void_type_node) |
e8abc66f MS |
1131 | return; |
1132 | ||
59387d2e JM |
1133 | if (TREE_CODE (*decl) == TEMPLATE_DECL) |
1134 | decl = &DECL_TEMPLATE_RESULT (*decl); | |
e8abc66f | 1135 | |
91d231cb | 1136 | decl_attributes (decl, attributes, flags); |
863adfc0 | 1137 | |
59387d2e JM |
1138 | if (TREE_CODE (*decl) == TYPE_DECL) |
1139 | SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl)); | |
8d08fdba MS |
1140 | } |
1141 | \f | |
a723baf1 MM |
1142 | /* Return the name for the constructor (or destructor) for the |
1143 | specified class TYPE. When given a template, this routine doesn't | |
8d08fdba | 1144 | lose the specialization. */ |
e92cc029 | 1145 | |
8d08fdba | 1146 | tree |
a723baf1 | 1147 | constructor_name_full (tree type) |
8d08fdba | 1148 | { |
a723baf1 MM |
1149 | type = TYPE_MAIN_VARIANT (type); |
1150 | if (CLASS_TYPE_P (type) && TYPE_WAS_ANONYMOUS (type) | |
1151 | && TYPE_HAS_CONSTRUCTOR (type)) | |
1152 | return DECL_NAME (OVL_CURRENT (CLASSTYPE_CONSTRUCTORS (type))); | |
1153 | else | |
1154 | return TYPE_IDENTIFIER (type); | |
8d08fdba MS |
1155 | } |
1156 | ||
a723baf1 MM |
1157 | /* Return the name for the constructor (or destructor) for the |
1158 | specified class. When given a template, return the plain | |
8d08fdba | 1159 | unspecialized name. */ |
e92cc029 | 1160 | |
8d08fdba | 1161 | tree |
848eed92 | 1162 | constructor_name (tree type) |
8d08fdba | 1163 | { |
a723baf1 MM |
1164 | tree name; |
1165 | name = constructor_name_full (type); | |
1166 | if (IDENTIFIER_TEMPLATE (name)) | |
1167 | name = IDENTIFIER_TEMPLATE (name); | |
1168 | return name; | |
8d08fdba | 1169 | } |
8ba658ee MM |
1170 | |
1171 | /* Returns TRUE if NAME is the name for the constructor for TYPE. */ | |
1172 | ||
1173 | bool | |
1174 | constructor_name_p (tree name, tree type) | |
1175 | { | |
1176 | return (name == constructor_name (type) | |
1177 | || name == constructor_name_full (type)); | |
1178 | } | |
1179 | ||
8d08fdba | 1180 | \f |
56e770bf | 1181 | /* Defer the compilation of the FN until the end of compilation. */ |
e92cc029 | 1182 | |
8d08fdba | 1183 | void |
848eed92 | 1184 | defer_fn (tree fn) |
8d08fdba | 1185 | { |
56e770bf | 1186 | if (DECL_DEFERRED_FN (fn)) |
8926095f | 1187 | return; |
56e770bf MM |
1188 | DECL_DEFERRED_FN (fn) = 1; |
1189 | if (!deferred_fns) | |
1190 | VARRAY_TREE_INIT (deferred_fns, 32, "deferred_fns"); | |
2c0f17dc | 1191 | |
56e770bf | 1192 | VARRAY_PUSH_TREE (deferred_fns, fn); |
8d08fdba MS |
1193 | } |
1194 | ||
0ca7178c JM |
1195 | /* Walks through the namespace- or function-scope anonymous union OBJECT, |
1196 | building appropriate ALIAS_DECLs. Returns one of the fields for use in | |
1197 | the mangled name. */ | |
f237d906 | 1198 | |
0ca7178c | 1199 | static tree |
848eed92 | 1200 | build_anon_union_vars (tree object) |
ce1b9eb9 | 1201 | { |
0ca7178c | 1202 | tree type = TREE_TYPE (object); |
ce1b9eb9 | 1203 | tree main_decl = NULL_TREE; |
cb96daa2 | 1204 | tree field; |
ce1b9eb9 | 1205 | |
6bdb8141 JM |
1206 | /* Rather than write the code to handle the non-union case, |
1207 | just give an error. */ | |
1208 | if (TREE_CODE (type) != UNION_TYPE) | |
1209 | error ("anonymous struct not inside named type"); | |
1210 | ||
cb96daa2 | 1211 | for (field = TYPE_FIELDS (type); |
ce1b9eb9 MM |
1212 | field != NULL_TREE; |
1213 | field = TREE_CHAIN (field)) | |
1214 | { | |
cb96daa2 | 1215 | tree decl; |
0ca7178c | 1216 | tree ref; |
8ebeee52 JM |
1217 | |
1218 | if (DECL_ARTIFICIAL (field)) | |
ce1b9eb9 | 1219 | continue; |
8ebeee52 JM |
1220 | if (TREE_CODE (field) != FIELD_DECL) |
1221 | { | |
0ca7178c JM |
1222 | cp_pedwarn_at ("\ |
1223 | `%#D' invalid; an anonymous union can only have non-static data members", | |
8ebeee52 JM |
1224 | field); |
1225 | continue; | |
1226 | } | |
ce1b9eb9 | 1227 | |
cb96daa2 | 1228 | if (TREE_PRIVATE (field)) |
8251199e | 1229 | cp_pedwarn_at ("private member `%#D' in anonymous union", field); |
cb96daa2 | 1230 | else if (TREE_PROTECTED (field)) |
8251199e | 1231 | cp_pedwarn_at ("protected member `%#D' in anonymous union", field); |
cb96daa2 | 1232 | |
8fbc5ae7 MM |
1233 | if (processing_template_decl) |
1234 | ref = build_min_nt (COMPONENT_REF, object, DECL_NAME (field)); | |
1235 | else | |
1236 | ref = build_class_member_access_expr (object, field, NULL_TREE, | |
1237 | false); | |
0ca7178c JM |
1238 | |
1239 | if (DECL_NAME (field)) | |
cb96daa2 | 1240 | { |
0ca7178c JM |
1241 | decl = build_decl (ALIAS_DECL, DECL_NAME (field), TREE_TYPE (field)); |
1242 | DECL_INITIAL (decl) = ref; | |
cb96daa2 | 1243 | TREE_PUBLIC (decl) = 0; |
0ca7178c JM |
1244 | TREE_STATIC (decl) = 0; |
1245 | DECL_EXTERNAL (decl) = 1; | |
cb96daa2 | 1246 | decl = pushdecl (decl); |
cb96daa2 | 1247 | } |
0ca7178c JM |
1248 | else if (ANON_AGGR_TYPE_P (TREE_TYPE (field))) |
1249 | decl = build_anon_union_vars (ref); | |
dd1b7476 KG |
1250 | else |
1251 | decl = 0; | |
cb96daa2 | 1252 | |
0ca7178c JM |
1253 | if (main_decl == NULL_TREE) |
1254 | main_decl = decl; | |
cb96daa2 | 1255 | } |
0ca7178c | 1256 | |
ce1b9eb9 MM |
1257 | return main_decl; |
1258 | } | |
1259 | ||
c050ec51 JM |
1260 | /* Finish off the processing of a UNION_TYPE structure. If the union is an |
1261 | anonymous union, then all members must be laid out together. PUBLIC_P | |
1262 | is nonzero if this union is not declared static. */ | |
e92cc029 | 1263 | |
8d08fdba | 1264 | void |
848eed92 | 1265 | finish_anon_union (tree anon_union_decl) |
8d08fdba MS |
1266 | { |
1267 | tree type = TREE_TYPE (anon_union_decl); | |
cb96daa2 | 1268 | tree main_decl; |
848eed92 | 1269 | bool public_p = TREE_PUBLIC (anon_union_decl); |
8d08fdba | 1270 | |
a1c65f9f | 1271 | /* The VAR_DECL's context is the same as the TYPE's context. */ |
e5410b32 NS |
1272 | DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type)); |
1273 | ||
cb96daa2 | 1274 | if (TYPE_FIELDS (type) == NULL_TREE) |
8d08fdba MS |
1275 | return; |
1276 | ||
1277 | if (public_p) | |
1278 | { | |
ff9f1a5d | 1279 | error ("namespace-scope anonymous aggregates must be static"); |
8d08fdba MS |
1280 | return; |
1281 | } | |
1282 | ||
8fbc5ae7 MM |
1283 | main_decl = build_anon_union_vars (anon_union_decl); |
1284 | if (main_decl == NULL_TREE) | |
01f4137f | 1285 | { |
8fbc5ae7 MM |
1286 | warning ("anonymous union with no members"); |
1287 | return; | |
1288 | } | |
bd9bb3d2 | 1289 | |
8fbc5ae7 MM |
1290 | if (!processing_template_decl) |
1291 | { | |
0ca7178c JM |
1292 | /* Use main_decl to set the mangled name. */ |
1293 | DECL_NAME (anon_union_decl) = DECL_NAME (main_decl); | |
1294 | mangle_decl (anon_union_decl); | |
1295 | DECL_NAME (anon_union_decl) = NULL_TREE; | |
01f4137f | 1296 | } |
edebf865 | 1297 | |
0ca7178c JM |
1298 | pushdecl (anon_union_decl); |
1299 | if (building_stmt_tree () | |
1300 | && at_function_scope_p ()) | |
1301 | add_decl_stmt (anon_union_decl); | |
1302 | else if (!processing_template_decl) | |
1303 | rest_of_decl_compilation (anon_union_decl, NULL, | |
1304 | toplevel_bindings_p (), at_eof); | |
8d08fdba | 1305 | } |
8d08fdba MS |
1306 | \f |
1307 | /* Auxiliary functions to make type signatures for | |
1308 | `operator new' and `operator delete' correspond to | |
1309 | what compiler will be expecting. */ | |
1310 | ||
8d08fdba | 1311 | tree |
848eed92 | 1312 | coerce_new_type (tree type) |
8d08fdba | 1313 | { |
36791f1e NS |
1314 | int e = 0; |
1315 | tree args = TYPE_ARG_TYPES (type); | |
1316 | ||
1317 | my_friendly_assert (TREE_CODE (type) == FUNCTION_TYPE, 20001107); | |
1318 | ||
1319 | if (!same_type_p (TREE_TYPE (type), ptr_type_node)) | |
33bd39a2 | 1320 | e = 1, error ("`operator new' must return type `%T'", ptr_type_node); |
36791f1e NS |
1321 | |
1322 | if (!args || args == void_list_node | |
c9f8536c | 1323 | || !same_type_p (TREE_VALUE (args), size_type_node)) |
36791f1e NS |
1324 | { |
1325 | e = 2; | |
1326 | if (args && args != void_list_node) | |
1327 | args = TREE_CHAIN (args); | |
c9f8536c | 1328 | pedwarn ("`operator new' takes type `size_t' (`%T') as first parameter", size_type_node); |
36791f1e NS |
1329 | } |
1330 | switch (e) | |
1331 | { | |
1332 | case 2: | |
c9f8536c | 1333 | args = tree_cons (NULL_TREE, size_type_node, args); |
36791f1e NS |
1334 | /* FALLTHROUGH */ |
1335 | case 1: | |
1336 | type = build_exception_variant | |
1337 | (build_function_type (ptr_type_node, args), | |
1338 | TYPE_RAISES_EXCEPTIONS (type)); | |
1339 | /* FALLTHROUGH */ | |
1340 | default:; | |
1341 | } | |
8d08fdba MS |
1342 | return type; |
1343 | } | |
1344 | ||
1345 | tree | |
848eed92 | 1346 | coerce_delete_type (tree type) |
8d08fdba | 1347 | { |
36791f1e NS |
1348 | int e = 0; |
1349 | tree args = TYPE_ARG_TYPES (type); | |
1350 | ||
1351 | my_friendly_assert (TREE_CODE (type) == FUNCTION_TYPE, 20001107); | |
824b9a4c | 1352 | |
36791f1e | 1353 | if (!same_type_p (TREE_TYPE (type), void_type_node)) |
33bd39a2 | 1354 | e = 1, error ("`operator delete' must return type `%T'", void_type_node); |
8d08fdba | 1355 | |
36791f1e NS |
1356 | if (!args || args == void_list_node |
1357 | || !same_type_p (TREE_VALUE (args), ptr_type_node)) | |
8d08fdba | 1358 | { |
36791f1e NS |
1359 | e = 2; |
1360 | if (args && args != void_list_node) | |
1361 | args = TREE_CHAIN (args); | |
33bd39a2 | 1362 | error ("`operator delete' takes type `%T' as first parameter", ptr_type_node); |
8d08fdba | 1363 | } |
36791f1e NS |
1364 | switch (e) |
1365 | { | |
1366 | case 2: | |
1367 | args = tree_cons (NULL_TREE, ptr_type_node, args); | |
1368 | /* FALLTHROUGH */ | |
1369 | case 1: | |
1370 | type = build_exception_variant | |
1371 | (build_function_type (void_type_node, args), | |
1372 | TYPE_RAISES_EXCEPTIONS (type)); | |
1373 | /* FALLTHROUGH */ | |
1374 | default:; | |
1375 | } | |
8d08fdba MS |
1376 | |
1377 | return type; | |
1378 | } | |
1379 | \f | |
1380 | static void | |
848eed92 | 1381 | mark_vtable_entries (tree decl) |
8d08fdba | 1382 | { |
f30432d7 MS |
1383 | tree entries = CONSTRUCTOR_ELTS (DECL_INITIAL (decl)); |
1384 | ||
8d08fdba MS |
1385 | for (; entries; entries = TREE_CHAIN (entries)) |
1386 | { | |
c4372ef4 | 1387 | tree fnaddr = TREE_VALUE (entries); |
aff08c18 | 1388 | tree fn; |
c4372ef4 | 1389 | |
67231816 RH |
1390 | if (TREE_CODE (fnaddr) != ADDR_EXPR |
1391 | && TREE_CODE (fnaddr) != FDESC_EXPR) | |
bbd15aac | 1392 | /* This entry is an offset: a virtual base class offset, a |
46ccf50a | 1393 | virtual call offset, an RTTI offset, etc. */ |
aff08c18 JM |
1394 | continue; |
1395 | ||
aff08c18 | 1396 | fn = TREE_OPERAND (fnaddr, 0); |
8926095f | 1397 | TREE_ADDRESSABLE (fn) = 1; |
31f8e4f3 MM |
1398 | /* When we don't have vcall offsets, we output thunks whenever |
1399 | we output the vtables that contain them. With vcall offsets, | |
1400 | we know all the thunks we'll need when we emit a virtual | |
1401 | function, so we emit the thunks there instead. */ | |
1402 | if (DECL_THUNK_P (fn)) | |
d0cd8b44 | 1403 | use_thunk (fn, /*emit_p=*/0); |
5566b478 | 1404 | mark_used (fn); |
7177d104 MS |
1405 | } |
1406 | } | |
1407 | ||
d11ad92e MS |
1408 | /* Set DECL up to have the closest approximation of "initialized common" |
1409 | linkage available. */ | |
1410 | ||
1411 | void | |
848eed92 | 1412 | comdat_linkage (tree decl) |
d11ad92e | 1413 | { |
d11ad92e | 1414 | if (flag_weak) |
7fcdf4c2 | 1415 | make_decl_one_only (decl); |
97458258 MM |
1416 | else if (TREE_CODE (decl) == FUNCTION_DECL |
1417 | || (TREE_CODE (decl) == VAR_DECL && DECL_ARTIFICIAL (decl))) | |
1418 | /* We can just emit function and compiler-generated variables | |
1419 | statically; having multiple copies is (for the most part) only | |
1420 | a waste of space. | |
1421 | ||
1422 | There are two correctness issues, however: the address of a | |
1423 | template instantiation with external linkage should be the | |
ad50e811 MM |
1424 | same, independent of what translation unit asks for the |
1425 | address, and this will not hold when we emit multiple copies of | |
97458258 MM |
1426 | the function. However, there's little else we can do. |
1427 | ||
3461fba7 NS |
1428 | Also, by default, the typeinfo implementation assumes that |
1429 | there will be only one copy of the string used as the name for | |
1430 | each type. Therefore, if weak symbols are unavailable, the | |
1431 | run-time library should perform a more conservative check; it | |
1432 | should perform a string comparison, rather than an address | |
1433 | comparison. */ | |
7fcdf4c2 | 1434 | TREE_PUBLIC (decl) = 0; |
ea735e02 JM |
1435 | else |
1436 | { | |
2f435bed JM |
1437 | /* Static data member template instantiations, however, cannot |
1438 | have multiple copies. */ | |
ea735e02 JM |
1439 | if (DECL_INITIAL (decl) == 0 |
1440 | || DECL_INITIAL (decl) == error_mark_node) | |
1441 | DECL_COMMON (decl) = 1; | |
1442 | else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))) | |
1443 | { | |
1444 | DECL_COMMON (decl) = 1; | |
1445 | DECL_INITIAL (decl) = error_mark_node; | |
1446 | } | |
1447 | else | |
1448 | { | |
1449 | /* We can't do anything useful; leave vars for explicit | |
1450 | instantiation. */ | |
1451 | DECL_EXTERNAL (decl) = 1; | |
1452 | DECL_NOT_REALLY_EXTERN (decl) = 0; | |
1453 | } | |
1454 | } | |
ab23f787 JM |
1455 | |
1456 | if (DECL_LANG_SPECIFIC (decl)) | |
1457 | DECL_COMDAT (decl) = 1; | |
d11ad92e MS |
1458 | } |
1459 | ||
b385c841 JM |
1460 | /* For win32 we also want to put explicit instantiations in |
1461 | linkonce sections, so that they will be merged with implicit | |
1462 | instantiations; otherwise we get duplicate symbol errors. */ | |
1463 | ||
1464 | void | |
848eed92 | 1465 | maybe_make_one_only (tree decl) |
b385c841 | 1466 | { |
b78121f6 JM |
1467 | /* We used to say that this was not necessary on targets that support weak |
1468 | symbols, because the implicit instantiations will defer to the explicit | |
1469 | one. However, that's not actually the case in SVR4; a strong definition | |
1470 | after a weak one is an error. Also, not making explicit | |
1471 | instantiations one_only means that we can end up with two copies of | |
a1c65f9f | 1472 | some template instantiations. */ |
90ecce3e | 1473 | if (! flag_weak) |
b385c841 JM |
1474 | return; |
1475 | ||
1476 | /* We can't set DECL_COMDAT on functions, or finish_file will think | |
ea735e02 JM |
1477 | we can get away with not emitting them if they aren't used. We need |
1478 | to for variables so that cp_finish_decl will update their linkage, | |
1479 | because their DECL_INITIAL may not have been set properly yet. */ | |
b385c841 | 1480 | |
ea735e02 JM |
1481 | make_decl_one_only (decl); |
1482 | ||
5cc90635 JM |
1483 | if (TREE_CODE (decl) == VAR_DECL) |
1484 | { | |
1485 | DECL_COMDAT (decl) = 1; | |
1486 | /* Mark it needed so we don't forget to emit it. */ | |
1487 | TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)) = 1; | |
1488 | } | |
b385c841 JM |
1489 | } |
1490 | ||
d18c083e | 1491 | /* Set TREE_PUBLIC and/or DECL_EXTERN on the vtable DECL, |
7177d104 MS |
1492 | based on TYPE and other static flags. |
1493 | ||
1494 | Note that anything public is tagged TREE_PUBLIC, whether | |
1495 | it's public in this file or in another one. */ | |
1496 | ||
5b605f68 | 1497 | void |
848eed92 | 1498 | import_export_vtable (tree decl, tree type, int final) |
7177d104 | 1499 | { |
e3417fcd MS |
1500 | if (DECL_INTERFACE_KNOWN (decl)) |
1501 | return; | |
1502 | ||
56ae6d77 | 1503 | if (TYPE_FOR_JAVA (type)) |
7177d104 MS |
1504 | { |
1505 | TREE_PUBLIC (decl) = 1; | |
56ae6d77 | 1506 | DECL_EXTERNAL (decl) = 1; |
e3417fcd MS |
1507 | DECL_INTERFACE_KNOWN (decl) = 1; |
1508 | } | |
1509 | else if (CLASSTYPE_INTERFACE_KNOWN (type)) | |
1510 | { | |
1511 | TREE_PUBLIC (decl) = 1; | |
46ccf50a | 1512 | DECL_EXTERNAL (decl) = CLASSTYPE_INTERFACE_ONLY (type); |
e3417fcd MS |
1513 | DECL_INTERFACE_KNOWN (decl) = 1; |
1514 | } | |
1515 | else | |
1516 | { | |
b7484fbe MS |
1517 | /* We can only wait to decide if we have real non-inline virtual |
1518 | functions in our class, or if we come from a template. */ | |
e3417fcd | 1519 | |
6db20143 | 1520 | int found = (CLASSTYPE_TEMPLATE_INSTANTIATION (type) |
9aad8f83 | 1521 | || CLASSTYPE_KEY_METHOD (type) != NULL_TREE); |
e3417fcd MS |
1522 | |
1523 | if (final || ! found) | |
1524 | { | |
d11ad92e | 1525 | comdat_linkage (decl); |
e3417fcd | 1526 | DECL_EXTERNAL (decl) = 0; |
e3417fcd MS |
1527 | } |
1528 | else | |
1529 | { | |
1530 | TREE_PUBLIC (decl) = 1; | |
1531 | DECL_EXTERNAL (decl) = 1; | |
e3417fcd | 1532 | } |
8d08fdba MS |
1533 | } |
1534 | } | |
1535 | ||
7e776093 JM |
1536 | /* Determine whether or not we want to specifically import or export CTYPE, |
1537 | using various heuristics. */ | |
67f7c391 | 1538 | |
27d26ee7 | 1539 | static void |
848eed92 | 1540 | import_export_class (tree ctype) |
8d08fdba | 1541 | { |
7e776093 JM |
1542 | /* -1 for imported, 1 for exported. */ |
1543 | int import_export = 0; | |
1544 | ||
27d26ee7 MM |
1545 | /* It only makes sense to call this function at EOF. The reason is |
1546 | that this function looks at whether or not the first non-inline | |
1547 | non-abstract virtual member function has been defined in this | |
1548 | translation unit. But, we can't possibly know that until we've | |
1549 | seen the entire translation unit. */ | |
1550 | my_friendly_assert (at_eof, 20000226); | |
1551 | ||
7e776093 JM |
1552 | if (CLASSTYPE_INTERFACE_KNOWN (ctype)) |
1553 | return; | |
1554 | ||
e9659ab0 JM |
1555 | /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma interface, |
1556 | we will have CLASSTYPE_INTERFACE_ONLY set but not | |
1557 | CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this | |
1558 | heuristic because someone will supply a #pragma implementation | |
1559 | elsewhere, and deducing it here would produce a conflict. */ | |
1560 | if (CLASSTYPE_INTERFACE_ONLY (ctype)) | |
1561 | return; | |
1562 | ||
91d231cb | 1563 | if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype))) |
501990bb | 1564 | import_export = -1; |
91d231cb | 1565 | else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype))) |
501990bb | 1566 | import_export = 1; |
7e776093 JM |
1567 | |
1568 | /* If we got -fno-implicit-templates, we import template classes that | |
1569 | weren't explicitly instantiated. */ | |
1570 | if (import_export == 0 | |
1571 | && CLASSTYPE_IMPLICIT_INSTANTIATION (ctype) | |
1572 | && ! flag_implicit_templates) | |
1573 | import_export = -1; | |
7177d104 | 1574 | |
7e776093 | 1575 | /* Base our import/export status on that of the first non-inline, |
6db20143 | 1576 | non-pure virtual function, if any. */ |
7e776093 | 1577 | if (import_export == 0 |
6e9dcc25 | 1578 | && TYPE_POLYMORPHIC_P (ctype)) |
51c184be | 1579 | { |
9aad8f83 | 1580 | tree method = CLASSTYPE_KEY_METHOD (ctype); |
6db20143 JM |
1581 | if (method) |
1582 | import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1); | |
51c184be | 1583 | } |
ad236eab JM |
1584 | |
1585 | #ifdef MULTIPLE_SYMBOL_SPACES | |
1586 | if (import_export == -1) | |
1587 | import_export = 0; | |
c11b6f21 | 1588 | #endif |
7e776093 JM |
1589 | |
1590 | if (import_export) | |
1591 | { | |
1592 | SET_CLASSTYPE_INTERFACE_KNOWN (ctype); | |
7e776093 JM |
1593 | CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0); |
1594 | } | |
67f7c391 JM |
1595 | } |
1596 | ||
a1dd0d36 | 1597 | /* We need to describe to the assembler the relationship between |
59fa060f | 1598 | a vtable and the vtable of the parent class. */ |
a1dd0d36 JM |
1599 | |
1600 | static void | |
848eed92 | 1601 | output_vtable_inherit (tree vars) |
a1dd0d36 JM |
1602 | { |
1603 | tree parent; | |
4a8d0c9c | 1604 | rtx child_rtx, parent_rtx; |
a1dd0d36 | 1605 | |
4a8d0c9c | 1606 | child_rtx = XEXP (DECL_RTL (vars), 0); /* strip the mem ref */ |
a1dd0d36 JM |
1607 | |
1608 | parent = binfo_for_vtable (vars); | |
1609 | ||
1610 | if (parent == TYPE_BINFO (DECL_CONTEXT (vars))) | |
4a8d0c9c | 1611 | parent_rtx = const0_rtx; |
a1dd0d36 JM |
1612 | else if (parent) |
1613 | { | |
c35cce41 | 1614 | parent = get_vtbl_decl_for_binfo (TYPE_BINFO (BINFO_TYPE (parent))); |
4a8d0c9c | 1615 | parent_rtx = XEXP (DECL_RTL (parent), 0); /* strip the mem ref */ |
a1dd0d36 JM |
1616 | } |
1617 | else | |
a98facb0 | 1618 | abort (); |
a1dd0d36 | 1619 | |
4a8d0c9c | 1620 | assemble_vtable_inherit (child_rtx, parent_rtx); |
a1dd0d36 JM |
1621 | } |
1622 | ||
548502d3 | 1623 | /* If necessary, write out the vtables for the dynamic class CTYPE. |
848eed92 | 1624 | Returns true if any vtables were emitted. */ |
548502d3 | 1625 | |
848eed92 | 1626 | static bool |
548502d3 | 1627 | maybe_emit_vtables (tree ctype) |
d18c083e | 1628 | { |
548502d3 MM |
1629 | tree vtbl; |
1630 | tree primary_vtbl; | |
1631 | ||
1632 | /* If the vtables for this class have already been emitted there is | |
1633 | nothing more to do. */ | |
1634 | primary_vtbl = CLASSTYPE_VTABLES (ctype); | |
1635 | if (TREE_ASM_WRITTEN (primary_vtbl)) | |
848eed92 | 1636 | return false; |
548502d3 MM |
1637 | /* Ignore dummy vtables made by get_vtable_decl. */ |
1638 | if (TREE_TYPE (primary_vtbl) == void_type_node) | |
848eed92 | 1639 | return false; |
548502d3 | 1640 | |
2455f26f | 1641 | import_export_class (ctype); |
548502d3 | 1642 | import_export_vtable (primary_vtbl, ctype, 1); |
2455f26f | 1643 | |
548502d3 MM |
1644 | /* See if any of the vtables are needed. */ |
1645 | for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = TREE_CHAIN (vtbl)) | |
1646 | if (!DECL_EXTERNAL (vtbl) && DECL_NEEDED_P (vtbl)) | |
1647 | break; | |
1648 | ||
1649 | if (!vtbl) | |
1650 | { | |
1651 | /* If the references to this class' vtables are optimized away, | |
1652 | still emit the appropriate debugging information. See | |
1653 | dfs_debug_mark. */ | |
1654 | if (DECL_COMDAT (primary_vtbl) | |
1655 | && CLASSTYPE_DEBUG_REQUESTED (ctype)) | |
1656 | note_debug_info_needed (ctype); | |
848eed92 | 1657 | return false; |
548502d3 MM |
1658 | } |
1659 | ||
1660 | /* The ABI requires that we emit all of the vtables if we emit any | |
1661 | of them. */ | |
1662 | for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = TREE_CHAIN (vtbl)) | |
d18c083e | 1663 | { |
8d08fdba | 1664 | /* Write it out. */ |
548502d3 MM |
1665 | import_export_vtable (vtbl, ctype, 1); |
1666 | mark_vtable_entries (vtbl); | |
1667 | if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0) | |
1668 | store_init_value (vtbl, DECL_INITIAL (vtbl)); | |
8d08fdba | 1669 | |
faf5394a | 1670 | if (write_symbols == DWARF_DEBUG || write_symbols == DWARF2_DEBUG) |
8d08fdba MS |
1671 | { |
1672 | /* Mark the VAR_DECL node representing the vtable itself as a | |
1673 | "gratuitous" one, thereby forcing dwarfout.c to ignore it. | |
1674 | It is rather important that such things be ignored because | |
1675 | any effort to actually generate DWARF for them will run | |
1676 | into trouble when/if we encounter code like: | |
1677 | ||
1678 | #pragma interface | |
1679 | struct S { virtual void member (); }; | |
1680 | ||
1681 | because the artificial declaration of the vtable itself (as | |
1682 | manufactured by the g++ front end) will say that the vtable | |
1683 | is a static member of `S' but only *after* the debug output | |
1684 | for the definition of `S' has already been output. This causes | |
1685 | grief because the DWARF entry for the definition of the vtable | |
1686 | will try to refer back to an earlier *declaration* of the | |
1687 | vtable as a static member of `S' and there won't be one. | |
1688 | We might be able to arrange to have the "vtable static member" | |
1689 | attached to the member list for `S' before the debug info for | |
1690 | `S' get written (which would solve the problem) but that would | |
1691 | require more intrusive changes to the g++ front end. */ | |
1692 | ||
548502d3 | 1693 | DECL_IGNORED_P (vtbl) = 1; |
8d08fdba | 1694 | } |
8d08fdba | 1695 | |
e2213efb JM |
1696 | /* Always make vtables weak. */ |
1697 | if (flag_weak) | |
548502d3 | 1698 | comdat_linkage (vtbl); |
e2213efb | 1699 | |
548502d3 | 1700 | rest_of_decl_compilation (vtbl, NULL, 1, 1); |
a1dd0d36 JM |
1701 | |
1702 | if (flag_vtable_gc) | |
548502d3 | 1703 | output_vtable_inherit (vtbl); |
a1dd0d36 | 1704 | |
84df082b MM |
1705 | /* Because we're only doing syntax-checking, we'll never end up |
1706 | actually marking the variable as written. */ | |
1707 | if (flag_syntax_only) | |
548502d3 | 1708 | TREE_ASM_WRITTEN (vtbl) = 1; |
8d08fdba | 1709 | } |
6db20143 | 1710 | |
548502d3 MM |
1711 | /* Since we're writing out the vtable here, also write the debug |
1712 | info. */ | |
1713 | note_debug_info_needed (ctype); | |
b7484fbe | 1714 | |
848eed92 | 1715 | return true; |
b7484fbe MS |
1716 | } |
1717 | ||
00595019 | 1718 | /* Determines the proper settings of TREE_PUBLIC and DECL_EXTERNAL for an |
5566b478 | 1719 | inline function or template instantiation at end-of-file. */ |
00595019 MS |
1720 | |
1721 | void | |
848eed92 | 1722 | import_export_decl (tree decl) |
00595019 | 1723 | { |
db5ae43f | 1724 | if (DECL_INTERFACE_KNOWN (decl)) |
00595019 MS |
1725 | return; |
1726 | ||
61289ca3 MM |
1727 | if (DECL_TEMPLATE_INSTANTIATION (decl) |
1728 | || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)) | |
00595019 | 1729 | { |
5566b478 | 1730 | DECL_NOT_REALLY_EXTERN (decl) = 1; |
61289ca3 MM |
1731 | if ((DECL_IMPLICIT_INSTANTIATION (decl) |
1732 | || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)) | |
5eea678f | 1733 | && (flag_implicit_templates |
79065db2 MM |
1734 | || (flag_implicit_inline_templates |
1735 | && DECL_DECLARED_INLINE_P (decl)))) | |
a9aedbc2 | 1736 | { |
75650646 MM |
1737 | if (!TREE_PUBLIC (decl)) |
1738 | /* Templates are allowed to have internal linkage. See | |
1739 | [basic.link]. */ | |
1740 | ; | |
d11ad92e | 1741 | else |
ea735e02 | 1742 | comdat_linkage (decl); |
a9aedbc2 | 1743 | } |
db5ae43f | 1744 | else |
b41d214e JM |
1745 | { |
1746 | DECL_EXTERNAL (decl) = 1; | |
1747 | DECL_NOT_REALLY_EXTERN (decl) = 0; | |
1748 | } | |
00595019 MS |
1749 | } |
1750 | else if (DECL_FUNCTION_MEMBER_P (decl)) | |
1751 | { | |
79065db2 | 1752 | if (!DECL_DECLARED_INLINE_P (decl)) |
00595019 | 1753 | { |
92788413 MM |
1754 | tree ctype = DECL_CONTEXT (decl); |
1755 | import_export_class (ctype); | |
1756 | if (CLASSTYPE_INTERFACE_KNOWN (ctype)) | |
1757 | { | |
1758 | DECL_NOT_REALLY_EXTERN (decl) | |
1759 | = ! (CLASSTYPE_INTERFACE_ONLY (ctype) | |
79065db2 MM |
1760 | || (DECL_DECLARED_INLINE_P (decl) |
1761 | && ! flag_implement_inlines | |
92788413 MM |
1762 | && !DECL_VINDEX (decl))); |
1763 | ||
b41d214e JM |
1764 | if (!DECL_NOT_REALLY_EXTERN (decl)) |
1765 | DECL_EXTERNAL (decl) = 1; | |
1766 | ||
92788413 MM |
1767 | /* Always make artificials weak. */ |
1768 | if (DECL_ARTIFICIAL (decl) && flag_weak) | |
1769 | comdat_linkage (decl); | |
1770 | else | |
1771 | maybe_make_one_only (decl); | |
1772 | } | |
00595019 | 1773 | } |
db5ae43f | 1774 | else |
d11ad92e | 1775 | comdat_linkage (decl); |
00595019 | 1776 | } |
db5ae43f | 1777 | else |
d11ad92e | 1778 | comdat_linkage (decl); |
e8abc66f MS |
1779 | |
1780 | DECL_INTERFACE_KNOWN (decl) = 1; | |
00595019 | 1781 | } |
db5ae43f | 1782 | |
d689a8f1 | 1783 | /* Here, we only decide whether or not the tinfo node should be |
838dfd8a | 1784 | emitted with the vtable. IS_IN_LIBRARY is nonzero iff the |
c6f553d1 | 1785 | typeinfo for TYPE should be in the runtime library. */ |
d689a8f1 NS |
1786 | |
1787 | void | |
848eed92 | 1788 | import_export_tinfo (tree decl, tree type, bool is_in_library) |
d689a8f1 NS |
1789 | { |
1790 | if (DECL_INTERFACE_KNOWN (decl)) | |
1791 | return; | |
1792 | ||
1793 | if (IS_AGGR_TYPE (type)) | |
1794 | import_export_class (type); | |
1795 | ||
1796 | if (IS_AGGR_TYPE (type) && CLASSTYPE_INTERFACE_KNOWN (type) | |
1797 | && TYPE_POLYMORPHIC_P (type) | |
1798 | /* If -fno-rtti, we're not necessarily emitting this stuff with | |
1799 | the class, so go ahead and emit it now. This can happen when | |
1800 | a class is used in exception handling. */ | |
1801 | && flag_rtti) | |
1802 | { | |
1803 | DECL_NOT_REALLY_EXTERN (decl) = !CLASSTYPE_INTERFACE_ONLY (type); | |
1804 | DECL_COMDAT (decl) = 0; | |
1805 | } | |
1806 | else | |
1807 | { | |
1808 | DECL_NOT_REALLY_EXTERN (decl) = 1; | |
1809 | DECL_COMDAT (decl) = 1; | |
1810 | } | |
c6f553d1 | 1811 | |
a1c65f9f | 1812 | /* Now override some cases. */ |
c6f553d1 NS |
1813 | if (flag_weak) |
1814 | DECL_COMDAT (decl) = 1; | |
1815 | else if (is_in_library) | |
1816 | DECL_COMDAT (decl) = 0; | |
1817 | ||
d689a8f1 NS |
1818 | DECL_INTERFACE_KNOWN (decl) = 1; |
1819 | } | |
1820 | ||
c10cdb3d MM |
1821 | /* Return an expression that performs the destruction of DECL, which |
1822 | must be a VAR_DECL whose type has a non-trivial destructor, or is | |
1823 | an array whose (innermost) elements have a non-trivial destructor. */ | |
1824 | ||
72b7eeff | 1825 | tree |
848eed92 | 1826 | build_cleanup (tree decl) |
72b7eeff MS |
1827 | { |
1828 | tree temp; | |
1829 | tree type = TREE_TYPE (decl); | |
1830 | ||
c10cdb3d MM |
1831 | /* This function should only be called for declarations that really |
1832 | require cleanups. */ | |
1833 | my_friendly_assert (!TYPE_HAS_TRIVIAL_DESTRUCTOR (type), 20030106); | |
1834 | ||
1835 | /* Treat all objects with destructors as used; the destructor may do | |
1836 | something substantive. */ | |
1837 | mark_used (decl); | |
1838 | ||
72b7eeff MS |
1839 | if (TREE_CODE (type) == ARRAY_TYPE) |
1840 | temp = decl; | |
1841 | else | |
1842 | { | |
dffd7eb6 | 1843 | cxx_mark_addressable (decl); |
72b7eeff MS |
1844 | temp = build1 (ADDR_EXPR, build_pointer_type (type), decl); |
1845 | } | |
1846 | temp = build_delete (TREE_TYPE (temp), temp, | |
86f45d2c | 1847 | sfk_complete_destructor, |
72b7eeff MS |
1848 | LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0); |
1849 | return temp; | |
1850 | } | |
1851 | ||
c395453c MM |
1852 | /* Returns the initialization guard variable for the variable DECL, |
1853 | which has static storage duration. */ | |
1f6e1acc | 1854 | |
c395453c | 1855 | tree |
848eed92 | 1856 | get_guard (tree decl) |
67d743fe | 1857 | { |
1f6e1acc | 1858 | tree sname; |
c395453c MM |
1859 | tree guard; |
1860 | ||
1f84ec23 | 1861 | sname = mangle_guard_variable (decl); |
c395453c MM |
1862 | guard = IDENTIFIER_GLOBAL_VALUE (sname); |
1863 | if (! guard) | |
1864 | { | |
1865 | tree guard_type; | |
1866 | ||
3461fba7 NS |
1867 | /* We use a type that is big enough to contain a mutex as well |
1868 | as an integer counter. */ | |
1f84ec23 | 1869 | guard_type = long_long_integer_type_node; |
c395453c | 1870 | guard = build_decl (VAR_DECL, sname, guard_type); |
844511c8 | 1871 | |
a1c65f9f | 1872 | /* The guard should have the same linkage as what it guards. */ |
844511c8 NS |
1873 | TREE_PUBLIC (guard) = TREE_PUBLIC (decl); |
1874 | TREE_STATIC (guard) = TREE_STATIC (decl); | |
1875 | DECL_COMMON (guard) = DECL_COMMON (decl); | |
1876 | DECL_ONE_ONLY (guard) = DECL_ONE_ONLY (decl); | |
1877 | if (TREE_PUBLIC (decl)) | |
1878 | DECL_WEAK (guard) = DECL_WEAK (decl); | |
1879 | ||
c395453c | 1880 | DECL_ARTIFICIAL (guard) = 1; |
c395453c | 1881 | TREE_USED (guard) = 1; |
c395453c MM |
1882 | pushdecl_top_level (guard); |
1883 | cp_finish_decl (guard, NULL_TREE, NULL_TREE, 0); | |
1884 | } | |
1885 | return guard; | |
1886 | } | |
1887 | ||
1888 | /* Return those bits of the GUARD variable that should be set when the | |
1889 | guarded entity is actually initialized. */ | |
1890 | ||
1891 | static tree | |
848eed92 | 1892 | get_guard_bits (tree guard) |
c395453c | 1893 | { |
3461fba7 NS |
1894 | /* We only set the first byte of the guard, in order to leave room |
1895 | for a mutex in the high-order bits. */ | |
c395453c MM |
1896 | guard = build1 (ADDR_EXPR, |
1897 | build_pointer_type (TREE_TYPE (guard)), | |
1898 | guard); | |
1899 | guard = build1 (NOP_EXPR, | |
1900 | build_pointer_type (char_type_node), | |
1901 | guard); | |
1902 | guard = build1 (INDIRECT_REF, char_type_node, guard); | |
1903 | ||
1904 | return guard; | |
1905 | } | |
1906 | ||
1907 | /* Return an expression which determines whether or not the GUARD | |
1908 | variable has already been initialized. */ | |
1909 | ||
1910 | tree | |
848eed92 | 1911 | get_guard_cond (tree guard) |
c395453c MM |
1912 | { |
1913 | tree guard_value; | |
1914 | ||
1915 | /* Check to see if the GUARD is zero. */ | |
1916 | guard = get_guard_bits (guard); | |
1917 | guard_value = integer_zero_node; | |
1918 | if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard))) | |
1919 | guard_value = convert (TREE_TYPE (guard), guard_value); | |
1920 | return cp_build_binary_op (EQ_EXPR, guard, guard_value); | |
1921 | } | |
1922 | ||
1923 | /* Return an expression which sets the GUARD variable, indicating that | |
1924 | the variable being guarded has been initialized. */ | |
1925 | ||
1926 | tree | |
848eed92 | 1927 | set_guard (tree guard) |
c395453c MM |
1928 | { |
1929 | tree guard_init; | |
1930 | ||
1931 | /* Set the GUARD to one. */ | |
1932 | guard = get_guard_bits (guard); | |
1933 | guard_init = integer_one_node; | |
1934 | if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard))) | |
1935 | guard_init = convert (TREE_TYPE (guard), guard_init); | |
1936 | return build_modify_expr (guard, NOP_EXPR, guard_init); | |
67d743fe MS |
1937 | } |
1938 | ||
961ec1a5 JM |
1939 | /* Start the process of running a particular set of global constructors |
1940 | or destructors. Subroutine of do_[cd]tors. */ | |
1941 | ||
914653a2 | 1942 | static tree |
848eed92 | 1943 | start_objects (int method_type, int initp) |
961ec1a5 JM |
1944 | { |
1945 | tree fnname; | |
914653a2 | 1946 | tree body; |
2ce3c6c6 | 1947 | char type[10]; |
961ec1a5 JM |
1948 | |
1949 | /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */ | |
1950 | ||
000ab922 | 1951 | if (initp != DEFAULT_INIT_PRIORITY) |
2ce3c6c6 | 1952 | { |
066d147c MH |
1953 | char joiner; |
1954 | ||
1955 | #ifdef JOINER | |
1956 | joiner = JOINER; | |
1957 | #else | |
1958 | joiner = '_'; | |
1959 | #endif | |
2ce3c6c6 | 1960 | |
066d147c | 1961 | sprintf (type, "%c%c%.5u", method_type, joiner, initp); |
2ce3c6c6 JM |
1962 | } |
1963 | else | |
1964 | sprintf (type, "%c", method_type); | |
1965 | ||
1966 | fnname = get_file_function_name_long (type); | |
961ec1a5 JM |
1967 | |
1968 | start_function (void_list_node, | |
1969 | make_call_declarator (fnname, void_list_node, NULL_TREE, | |
1970 | NULL_TREE), | |
914653a2 | 1971 | NULL_TREE, SF_DEFAULT); |
961ec1a5 | 1972 | |
14686fcd JL |
1973 | /* It can be a static function as long as collect2 does not have |
1974 | to scan the object file to find its ctor/dtor routine. */ | |
2cc07db4 | 1975 | TREE_PUBLIC (current_function_decl) = ! targetm.have_ctors_dtors; |
b4bb92e5 | 1976 | |
5fdaba89 MM |
1977 | /* Mark this declaration as used to avoid spurious warnings. */ |
1978 | TREE_USED (current_function_decl) = 1; | |
1979 | ||
2b76013c MM |
1980 | /* Mark this function as a global constructor or destructor. */ |
1981 | if (method_type == 'I') | |
1982 | DECL_GLOBAL_CTOR_P (current_function_decl) = 1; | |
1983 | else | |
1984 | DECL_GLOBAL_DTOR_P (current_function_decl) = 1; | |
e2500fed | 1985 | DECL_LANG_SPECIFIC (current_function_decl)->decl_flags.u2sel = 1; |
2b76013c | 1986 | |
914653a2 | 1987 | body = begin_compound_stmt (/*has_no_scope=*/0); |
b3f738da MM |
1988 | |
1989 | /* We cannot allow these functions to be elided, even if they do not | |
1990 | have external linkage. And, there's no point in deferring | |
1991 | copmilation of thes functions; they're all going to have to be | |
1992 | out anyhow. */ | |
1993 | current_function_cannot_inline | |
1994 | = "static constructors and destructors cannot be inlined"; | |
914653a2 MM |
1995 | |
1996 | return body; | |
961ec1a5 JM |
1997 | } |
1998 | ||
1999 | /* Finish the process of running a particular set of global constructors | |
2000 | or destructors. Subroutine of do_[cd]tors. */ | |
2001 | ||
2002 | static void | |
848eed92 | 2003 | finish_objects (int method_type, int initp, tree body) |
961ec1a5 | 2004 | { |
914653a2 | 2005 | tree fn; |
961ec1a5 | 2006 | |
c7cb8979 JM |
2007 | /* Finish up. */ |
2008 | finish_compound_stmt (/*has_no_scope=*/0, body); | |
0acf7199 | 2009 | fn = finish_function (0); |
914653a2 | 2010 | expand_body (fn); |
961ec1a5 | 2011 | |
84df082b MM |
2012 | /* When only doing semantic analysis, and no RTL generation, we |
2013 | can't call functions that directly emit assembly code; there is | |
2014 | no assembly file in which to put the code. */ | |
2015 | if (flag_syntax_only) | |
2016 | return; | |
2017 | ||
2cc07db4 RH |
2018 | if (targetm.have_ctors_dtors) |
2019 | { | |
2020 | rtx fnsym = XEXP (DECL_RTL (fn), 0); | |
2021 | if (method_type == 'I') | |
2022 | (* targetm.asm_out.constructor) (fnsym, initp); | |
2023 | else | |
2024 | (* targetm.asm_out.destructor) (fnsym, initp); | |
2025 | } | |
961ec1a5 JM |
2026 | } |
2027 | ||
0aafb128 MM |
2028 | /* The names of the parameters to the function created to handle |
2029 | initializations and destructions for objects with static storage | |
2030 | duration. */ | |
2031 | #define INITIALIZE_P_IDENTIFIER "__initialize_p" | |
2032 | #define PRIORITY_IDENTIFIER "__priority" | |
2033 | ||
2034 | /* The name of the function we create to handle initializations and | |
2035 | destructions for objects with static storage duration. */ | |
2036 | #define SSDF_IDENTIFIER "__static_initialization_and_destruction" | |
2037 | ||
2038 | /* The declaration for the __INITIALIZE_P argument. */ | |
e2500fed | 2039 | static GTY(()) tree initialize_p_decl; |
0aafb128 MM |
2040 | |
2041 | /* The declaration for the __PRIORITY argument. */ | |
e2500fed | 2042 | static GTY(()) tree priority_decl; |
0aafb128 MM |
2043 | |
2044 | /* The declaration for the static storage duration function. */ | |
e2500fed | 2045 | static GTY(()) tree ssdf_decl; |
0aafb128 | 2046 | |
0352cfc8 MM |
2047 | /* All the static storage duration functions created in this |
2048 | translation unit. */ | |
e2500fed | 2049 | static GTY(()) varray_type ssdf_decls; |
0352cfc8 | 2050 | |
0aafb128 MM |
2051 | /* A map from priority levels to information about that priority |
2052 | level. There may be many such levels, so efficient lookup is | |
2053 | important. */ | |
2054 | static splay_tree priority_info_map; | |
2055 | ||
2056 | /* Begins the generation of the function that will handle all | |
2057 | initialization and destruction of objects with static storage | |
2058 | duration. The function generated takes two parameters of type | |
2059 | `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is | |
838dfd8a | 2060 | nonzero, it performs initializations. Otherwise, it performs |
0aafb128 MM |
2061 | destructions. It only performs those initializations or |
2062 | destructions with the indicated __PRIORITY. The generated function | |
2063 | returns no value. | |
2064 | ||
2065 | It is assumed that this function will only be called once per | |
2066 | translation unit. */ | |
961ec1a5 | 2067 | |
313bc2c2 | 2068 | static tree |
848eed92 | 2069 | start_static_storage_duration_function (void) |
961ec1a5 | 2070 | { |
0352cfc8 MM |
2071 | static unsigned ssdf_number; |
2072 | ||
0aafb128 MM |
2073 | tree parm_types; |
2074 | tree type; | |
313bc2c2 | 2075 | tree body; |
0352cfc8 MM |
2076 | char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32]; |
2077 | ||
2078 | /* Create the identifier for this function. It will be of the form | |
2079 | SSDF_IDENTIFIER_<number>. */ | |
2080 | sprintf (id, "%s_%u", SSDF_IDENTIFIER, ssdf_number++); | |
2081 | if (ssdf_number == 0) | |
2082 | { | |
2083 | /* Overflow occurred. That means there are at least 4 billion | |
2084 | initialization functions. */ | |
2085 | sorry ("too many initialization functions required"); | |
a98facb0 | 2086 | abort (); |
0352cfc8 | 2087 | } |
0aafb128 MM |
2088 | |
2089 | /* Create the parameters. */ | |
2090 | parm_types = void_list_node; | |
e1b3e07d MM |
2091 | parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types); |
2092 | parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types); | |
0aafb128 MM |
2093 | type = build_function_type (void_type_node, parm_types); |
2094 | ||
2095 | /* Create the FUNCTION_DECL itself. */ | |
2096 | ssdf_decl = build_lang_decl (FUNCTION_DECL, | |
0352cfc8 | 2097 | get_identifier (id), |
0aafb128 MM |
2098 | type); |
2099 | TREE_PUBLIC (ssdf_decl) = 0; | |
2100 | DECL_ARTIFICIAL (ssdf_decl) = 1; | |
0352cfc8 MM |
2101 | |
2102 | /* Put this function in the list of functions to be called from the | |
2103 | static constructors and destructors. */ | |
2104 | if (!ssdf_decls) | |
2105 | { | |
2106 | VARRAY_TREE_INIT (ssdf_decls, 32, "ssdf_decls"); | |
2107 | ||
2108 | /* Take this opportunity to initialize the map from priority | |
a1c65f9f | 2109 | numbers to information about that priority level. */ |
0352cfc8 MM |
2110 | priority_info_map = splay_tree_new (splay_tree_compare_ints, |
2111 | /*delete_key_fn=*/0, | |
2112 | /*delete_value_fn=*/ | |
2113 | (splay_tree_delete_value_fn) &free); | |
2114 | ||
2115 | /* We always need to generate functions for the | |
2116 | DEFAULT_INIT_PRIORITY so enter it now. That way when we walk | |
2117 | priorities later, we'll be sure to find the | |
2118 | DEFAULT_INIT_PRIORITY. */ | |
2119 | get_priority_info (DEFAULT_INIT_PRIORITY); | |
2120 | } | |
2121 | ||
2c0f17dc | 2122 | VARRAY_PUSH_TREE (ssdf_decls, ssdf_decl); |
0aafb128 MM |
2123 | |
2124 | /* Create the argument list. */ | |
8e51619a JM |
2125 | initialize_p_decl = cp_build_parm_decl |
2126 | (get_identifier (INITIALIZE_P_IDENTIFIER), integer_type_node); | |
0aafb128 | 2127 | DECL_CONTEXT (initialize_p_decl) = ssdf_decl; |
0aafb128 | 2128 | TREE_USED (initialize_p_decl) = 1; |
8e51619a JM |
2129 | priority_decl = cp_build_parm_decl |
2130 | (get_identifier (PRIORITY_IDENTIFIER), integer_type_node); | |
0aafb128 | 2131 | DECL_CONTEXT (priority_decl) = ssdf_decl; |
0aafb128 MM |
2132 | TREE_USED (priority_decl) = 1; |
2133 | ||
2134 | TREE_CHAIN (initialize_p_decl) = priority_decl; | |
2135 | DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl; | |
2136 | ||
13ef1ec5 MM |
2137 | /* Put the function in the global scope. */ |
2138 | pushdecl (ssdf_decl); | |
2139 | ||
0aafb128 MM |
2140 | /* Start the function itself. This is equivalent to declarating the |
2141 | function as: | |
2142 | ||
c472cdfd | 2143 | static void __ssdf (int __initialize_p, init __priority_p); |
0aafb128 MM |
2144 | |
2145 | It is static because we only need to call this function from the | |
2146 | various constructor and destructor functions for this module. */ | |
2147 | start_function (/*specs=*/NULL_TREE, | |
2148 | ssdf_decl, | |
2149 | /*attrs=*/NULL_TREE, | |
313bc2c2 | 2150 | SF_PRE_PARSED); |
0aafb128 MM |
2151 | |
2152 | /* Set up the scope of the outermost block in the function. */ | |
313bc2c2 | 2153 | body = begin_compound_stmt (/*has_no_scope=*/0); |
2ce3c6c6 | 2154 | |
0352cfc8 MM |
2155 | /* This function must not be deferred because we are depending on |
2156 | its compilation to tell us what is TREE_SYMBOL_REFERENCED. */ | |
2157 | current_function_cannot_inline | |
2158 | = "static storage duration functions cannot be inlined"; | |
961ec1a5 | 2159 | |
313bc2c2 | 2160 | return body; |
961ec1a5 JM |
2161 | } |
2162 | ||
0aafb128 MM |
2163 | /* Finish the generation of the function which performs initialization |
2164 | and destruction of objects with static storage duration. After | |
2165 | this point, no more such objects can be created. */ | |
961ec1a5 JM |
2166 | |
2167 | static void | |
848eed92 | 2168 | finish_static_storage_duration_function (tree body) |
961ec1a5 | 2169 | { |
0aafb128 | 2170 | /* Close out the function. */ |
313bc2c2 | 2171 | finish_compound_stmt (/*has_no_scope=*/0, body); |
0acf7199 | 2172 | expand_body (finish_function (0)); |
0aafb128 | 2173 | } |
961ec1a5 | 2174 | |
0aafb128 MM |
2175 | /* Return the information about the indicated PRIORITY level. If no |
2176 | code to handle this level has yet been generated, generate the | |
2177 | appropriate prologue. */ | |
961ec1a5 | 2178 | |
0aafb128 | 2179 | static priority_info |
848eed92 | 2180 | get_priority_info (int priority) |
0aafb128 MM |
2181 | { |
2182 | priority_info pi; | |
2183 | splay_tree_node n; | |
2184 | ||
2185 | n = splay_tree_lookup (priority_info_map, | |
2186 | (splay_tree_key) priority); | |
2187 | if (!n) | |
2188 | { | |
2189 | /* Create a new priority information structure, and insert it | |
2190 | into the map. */ | |
2191 | pi = (priority_info) xmalloc (sizeof (struct priority_info_s)); | |
0352cfc8 MM |
2192 | pi->initializations_p = 0; |
2193 | pi->destructions_p = 0; | |
0aafb128 MM |
2194 | splay_tree_insert (priority_info_map, |
2195 | (splay_tree_key) priority, | |
2196 | (splay_tree_value) pi); | |
2197 | } | |
2198 | else | |
2199 | pi = (priority_info) n->value; | |
961ec1a5 | 2200 | |
0aafb128 MM |
2201 | return pi; |
2202 | } | |
961ec1a5 | 2203 | |
313bc2c2 | 2204 | /* Set up to handle the initialization or destruction of DECL. If |
838dfd8a | 2205 | INITP is nonzero, we are initializing the variable. Otherwise, we |
313bc2c2 MM |
2206 | are destroying it. */ |
2207 | ||
2208 | static tree | |
848eed92 | 2209 | start_static_initialization_or_destruction (tree decl, int initp) |
313bc2c2 | 2210 | { |
c395453c | 2211 | tree guard_if_stmt = NULL_TREE; |
313bc2c2 MM |
2212 | int priority; |
2213 | tree cond; | |
c395453c | 2214 | tree guard; |
313bc2c2 MM |
2215 | tree init_cond; |
2216 | priority_info pi; | |
2217 | ||
2218 | /* Figure out the priority for this declaration. */ | |
2219 | priority = DECL_INIT_PRIORITY (decl); | |
2220 | if (!priority) | |
2221 | priority = DEFAULT_INIT_PRIORITY; | |
2222 | ||
2223 | /* Remember that we had an initialization or finalization at this | |
2224 | priority. */ | |
2225 | pi = get_priority_info (priority); | |
2226 | if (initp) | |
2227 | pi->initializations_p = 1; | |
2228 | else | |
2229 | pi->destructions_p = 1; | |
2230 | ||
2231 | /* Trick the compiler into thinking we are at the file and line | |
2232 | where DECL was declared so that error-messages make sense, and so | |
2233 | that the debugger will show somewhat sensible file and line | |
2234 | information. */ | |
2235 | input_filename = DECL_SOURCE_FILE (decl); | |
2236 | lineno = DECL_SOURCE_LINE (decl); | |
2237 | ||
2238 | /* Because of: | |
2239 | ||
2240 | [class.access.spec] | |
2241 | ||
2242 | Access control for implicit calls to the constructors, | |
2243 | the conversion functions, or the destructor called to | |
2244 | create and destroy a static data member is performed as | |
2245 | if these calls appeared in the scope of the member's | |
2246 | class. | |
2247 | ||
2248 | we pretend we are in a static member function of the class of | |
2249 | which the DECL is a member. */ | |
2250 | if (member_p (decl)) | |
2251 | { | |
4f1c5b7d | 2252 | DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl); |
313bc2c2 MM |
2253 | DECL_STATIC_FUNCTION_P (current_function_decl) = 1; |
2254 | } | |
2255 | ||
2256 | /* Conditionalize this initialization on being in the right priority | |
2257 | and being initializing/finalizing appropriately. */ | |
c395453c | 2258 | guard_if_stmt = begin_if_stmt (); |
ab76ca54 MM |
2259 | cond = cp_build_binary_op (EQ_EXPR, |
2260 | priority_decl, | |
2261 | build_int_2 (priority, 0)); | |
313bc2c2 | 2262 | init_cond = initp ? integer_one_node : integer_zero_node; |
ab76ca54 MM |
2263 | init_cond = cp_build_binary_op (EQ_EXPR, |
2264 | initialize_p_decl, | |
2265 | init_cond); | |
2266 | cond = cp_build_binary_op (TRUTH_ANDIF_EXPR, cond, init_cond); | |
313bc2c2 | 2267 | |
c395453c MM |
2268 | /* Assume we don't need a guard. */ |
2269 | guard = NULL_TREE; | |
2270 | /* We need a guard if this is an object with external linkage that | |
1f6e1acc AS |
2271 | might be initialized in more than one place. (For example, a |
2272 | static data member of a template, when the data member requires | |
2273 | construction.) */ | |
313bc2c2 MM |
2274 | if (TREE_PUBLIC (decl) && (DECL_COMMON (decl) |
2275 | || DECL_ONE_ONLY (decl) | |
2276 | || DECL_WEAK (decl))) | |
2277 | { | |
c395453c | 2278 | tree guard_cond; |
313bc2c2 | 2279 | |
c395453c | 2280 | guard = get_guard (decl); |
313bc2c2 | 2281 | |
c395453c MM |
2282 | /* When using __cxa_atexit, we just check the GUARD as we would |
2283 | for a local static. */ | |
2284 | if (flag_use_cxa_atexit) | |
2285 | { | |
2286 | /* When using __cxa_atexit, we never try to destroy | |
2287 | anything from a static destructor. */ | |
2288 | my_friendly_assert (initp, 20000629); | |
2289 | guard_cond = get_guard_cond (guard); | |
2290 | } | |
5fd893d5 MM |
2291 | /* If we don't have __cxa_atexit, then we will be running |
2292 | destructors from .fini sections, or their equivalents. So, | |
2293 | we need to know how many times we've tried to initialize this | |
2294 | object. We do initializations only if the GUARD is zero, | |
2295 | i.e., if we are the first to initialize the variable. We do | |
2296 | destructions only if the GUARD is one, i.e., if we are the | |
2297 | last to destroy the variable. */ | |
c395453c MM |
2298 | else if (initp) |
2299 | guard_cond | |
ab76ca54 MM |
2300 | = cp_build_binary_op (EQ_EXPR, |
2301 | build_unary_op (PREINCREMENT_EXPR, | |
c395453c | 2302 | guard, |
ab76ca54 MM |
2303 | /*noconvert=*/1), |
2304 | integer_one_node); | |
313bc2c2 | 2305 | else |
c395453c | 2306 | guard_cond |
ab76ca54 MM |
2307 | = cp_build_binary_op (EQ_EXPR, |
2308 | build_unary_op (PREDECREMENT_EXPR, | |
c395453c | 2309 | guard, |
ab76ca54 MM |
2310 | /*noconvert=*/1), |
2311 | integer_zero_node); | |
2312 | ||
c395453c | 2313 | cond = cp_build_binary_op (TRUTH_ANDIF_EXPR, cond, guard_cond); |
313bc2c2 MM |
2314 | } |
2315 | ||
c395453c MM |
2316 | finish_if_stmt_cond (cond, guard_if_stmt); |
2317 | ||
5fd893d5 MM |
2318 | /* If we're using __cxa_atexit, we have not already set the GUARD, |
2319 | so we must do so now. */ | |
2320 | if (guard && initp && flag_use_cxa_atexit) | |
c395453c | 2321 | finish_expr_stmt (set_guard (guard)); |
313bc2c2 | 2322 | |
c395453c | 2323 | return guard_if_stmt; |
313bc2c2 MM |
2324 | } |
2325 | ||
2326 | /* We've just finished generating code to do an initialization or | |
c395453c | 2327 | finalization. GUARD_IF_STMT is the if-statement we used to guard |
313bc2c2 MM |
2328 | the initialization. */ |
2329 | ||
2330 | static void | |
848eed92 | 2331 | finish_static_initialization_or_destruction (tree guard_if_stmt) |
313bc2c2 | 2332 | { |
c395453c | 2333 | finish_then_clause (guard_if_stmt); |
313bc2c2 MM |
2334 | finish_if_stmt (); |
2335 | ||
2336 | /* Now that we're done with DECL we don't need to pretend to be a | |
2337 | member of its class any longer. */ | |
4f1c5b7d | 2338 | DECL_CONTEXT (current_function_decl) = NULL_TREE; |
313bc2c2 MM |
2339 | DECL_STATIC_FUNCTION_P (current_function_decl) = 0; |
2340 | } | |
2341 | ||
8e3df2de MM |
2342 | /* Generate code to do the initialization of DECL, a VAR_DECL with |
2343 | static storage duration. The initialization is INIT. */ | |
961ec1a5 | 2344 | |
0aafb128 | 2345 | static void |
848eed92 | 2346 | do_static_initialization (tree decl, tree init) |
0aafb128 | 2347 | { |
c395453c | 2348 | tree guard_if_stmt; |
961ec1a5 | 2349 | |
313bc2c2 | 2350 | /* Set up for the initialization. */ |
c395453c | 2351 | guard_if_stmt |
313bc2c2 MM |
2352 | = start_static_initialization_or_destruction (decl, |
2353 | /*initp=*/1); | |
8e3df2de MM |
2354 | |
2355 | /* Perform the initialization. */ | |
2356 | if (init) | |
2357 | finish_expr_stmt (init); | |
961ec1a5 | 2358 | |
bf419747 MM |
2359 | /* If we're using __cxa_atexit, register a a function that calls the |
2360 | destructor for the object. */ | |
2361 | if (flag_use_cxa_atexit) | |
2362 | register_dtor_fn (decl); | |
2363 | ||
313bc2c2 | 2364 | /* Finsh up. */ |
c395453c | 2365 | finish_static_initialization_or_destruction (guard_if_stmt); |
0aafb128 | 2366 | } |
961ec1a5 | 2367 | |
0aafb128 | 2368 | /* Generate code to do the static destruction of DECL. If DECL may be |
c395453c | 2369 | initialized more than once in different object files, GUARD is the |
0aafb128 MM |
2370 | guard variable to check. PRIORITY is the priority for the |
2371 | destruction. */ | |
2372 | ||
2373 | static void | |
848eed92 | 2374 | do_static_destruction (tree decl) |
0aafb128 | 2375 | { |
c395453c | 2376 | tree guard_if_stmt; |
961ec1a5 | 2377 | |
bf419747 MM |
2378 | /* If we're using __cxa_atexit, then destructors are registered |
2379 | immediately after objects are initialized. */ | |
2380 | my_friendly_assert (!flag_use_cxa_atexit, 20000121); | |
2381 | ||
0aafb128 | 2382 | /* If we don't need a destructor, there's nothing to do. */ |
834c6dff | 2383 | if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl))) |
0aafb128 | 2384 | return; |
bf419747 | 2385 | |
f1dedc31 | 2386 | /* Actually do the destruction. */ |
c395453c | 2387 | guard_if_stmt = start_static_initialization_or_destruction (decl, |
8e3df2de | 2388 | /*initp=*/0); |
313bc2c2 | 2389 | finish_expr_stmt (build_cleanup (decl)); |
c395453c | 2390 | finish_static_initialization_or_destruction (guard_if_stmt); |
961ec1a5 JM |
2391 | } |
2392 | ||
313bc2c2 MM |
2393 | /* VARS is a list of variables with static storage duration which may |
2394 | need initialization and/or finalization. Remove those variables | |
2395 | that don't really need to be initialized or finalized, and return | |
2396 | the resulting list. The order in which the variables appear in | |
2397 | VARS is in reverse order of the order in which they should actually | |
2398 | be initialized. The list we return is in the unreversed order; | |
2399 | i.e., the first variable should be initialized first. */ | |
8d08fdba | 2400 | |
313bc2c2 | 2401 | static tree |
cec24319 | 2402 | prune_vars_needing_no_initialization (tree *vars) |
8d08fdba | 2403 | { |
cec24319 MM |
2404 | tree *var = vars; |
2405 | tree result = NULL_TREE; | |
8d08fdba | 2406 | |
cec24319 | 2407 | while (*var) |
313bc2c2 | 2408 | { |
cec24319 MM |
2409 | tree t = *var; |
2410 | tree decl = TREE_VALUE (t); | |
2411 | tree init = TREE_PURPOSE (t); | |
8d2733ca | 2412 | |
313bc2c2 MM |
2413 | /* Deal gracefully with error. */ |
2414 | if (decl == error_mark_node) | |
cec24319 MM |
2415 | { |
2416 | var = &TREE_CHAIN (t); | |
2417 | continue; | |
2418 | } | |
5566b478 | 2419 | |
313bc2c2 MM |
2420 | /* The only things that can be initialized are variables. */ |
2421 | my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 19990420); | |
8d08fdba | 2422 | |
313bc2c2 MM |
2423 | /* If this object is not defined, we don't need to do anything |
2424 | here. */ | |
2425 | if (DECL_EXTERNAL (decl)) | |
cec24319 MM |
2426 | { |
2427 | var = &TREE_CHAIN (t); | |
2428 | continue; | |
2429 | } | |
8d08fdba | 2430 | |
313bc2c2 MM |
2431 | /* Also, if the initializer already contains errors, we can bail |
2432 | out now. */ | |
2433 | if (init && TREE_CODE (init) == TREE_LIST | |
2434 | && value_member (error_mark_node, init)) | |
cec24319 MM |
2435 | { |
2436 | var = &TREE_CHAIN (t); | |
2437 | continue; | |
2438 | } | |
d18c083e | 2439 | |
313bc2c2 MM |
2440 | /* This variable is going to need initialization and/or |
2441 | finalization, so we add it to the list. */ | |
cec24319 MM |
2442 | *var = TREE_CHAIN (t); |
2443 | TREE_CHAIN (t) = result; | |
2444 | result = t; | |
313bc2c2 | 2445 | } |
1139b3d8 | 2446 | |
313bc2c2 MM |
2447 | return result; |
2448 | } | |
1139b3d8 | 2449 | |
313bc2c2 MM |
2450 | /* Make sure we have told the back end about all the variables in |
2451 | VARS. */ | |
0aafb128 | 2452 | |
313bc2c2 | 2453 | static void |
848eed92 | 2454 | write_out_vars (tree vars) |
313bc2c2 MM |
2455 | { |
2456 | tree v; | |
0aafb128 | 2457 | |
313bc2c2 MM |
2458 | for (v = vars; v; v = TREE_CHAIN (v)) |
2459 | if (! TREE_ASM_WRITTEN (TREE_VALUE (v))) | |
2460 | rest_of_decl_compilation (TREE_VALUE (v), 0, 1, 1); | |
0aafb128 | 2461 | } |
909e536a | 2462 | |
0aafb128 MM |
2463 | /* Generate a static constructor (if CONSTRUCTOR_P) or destructor |
2464 | (otherwise) that will initialize all gobal objects with static | |
2465 | storage duration having the indicated PRIORITY. */ | |
73aad9b9 | 2466 | |
0aafb128 | 2467 | static void |
848eed92 | 2468 | generate_ctor_or_dtor_function (bool constructor_p, int priority) |
0aafb128 MM |
2469 | { |
2470 | char function_key; | |
2471 | tree arguments; | |
914653a2 | 2472 | tree body; |
0352cfc8 | 2473 | size_t i; |
73aad9b9 | 2474 | |
0aafb128 MM |
2475 | /* We use `I' to indicate initialization and `D' to indicate |
2476 | destruction. */ | |
2477 | if (constructor_p) | |
2478 | function_key = 'I'; | |
2479 | else | |
2480 | function_key = 'D'; | |
73aad9b9 | 2481 | |
0aafb128 | 2482 | /* Begin the function. */ |
914653a2 | 2483 | body = start_objects (function_key, priority); |
2c73f9f5 | 2484 | |
0aafb128 MM |
2485 | /* Call the static storage duration function with appropriate |
2486 | arguments. */ | |
2c0f17dc | 2487 | for (i = 0; i < ssdf_decls->elements_used; ++i) |
0352cfc8 MM |
2488 | { |
2489 | arguments = tree_cons (NULL_TREE, build_int_2 (priority, 0), | |
2490 | NULL_TREE); | |
2491 | arguments = tree_cons (NULL_TREE, build_int_2 (constructor_p, 0), | |
2492 | arguments); | |
914653a2 | 2493 | finish_expr_stmt (build_function_call (VARRAY_TREE (ssdf_decls, i), |
0352cfc8 MM |
2494 | arguments)); |
2495 | } | |
909e536a | 2496 | |
0aafb128 MM |
2497 | /* If we're generating code for the DEFAULT_INIT_PRIORITY, throw in |
2498 | calls to any functions marked with attributes indicating that | |
2499 | they should be called at initialization- or destruction-time. */ | |
2500 | if (priority == DEFAULT_INIT_PRIORITY) | |
2501 | { | |
2502 | tree fns; | |
2503 | ||
2504 | for (fns = constructor_p ? static_ctors : static_dtors; | |
2505 | fns; | |
2506 | fns = TREE_CHAIN (fns)) | |
914653a2 | 2507 | finish_expr_stmt (build_function_call (TREE_VALUE (fns), NULL_TREE)); |
0aafb128 | 2508 | } |
28cbf42c | 2509 | |
0aafb128 | 2510 | /* Close out the function. */ |
914653a2 | 2511 | finish_objects (function_key, priority, body); |
0aafb128 | 2512 | } |
44a8d0b3 | 2513 | |
0aafb128 | 2514 | /* Generate constructor and destructor functions for the priority |
0352cfc8 | 2515 | indicated by N. */ |
44a8d0b3 | 2516 | |
0aafb128 | 2517 | static int |
848eed92 GDR |
2518 | generate_ctor_and_dtor_functions_for_priority (splay_tree_node n, |
2519 | void * data ATTRIBUTE_UNUSED) | |
0aafb128 MM |
2520 | { |
2521 | int priority = (int) n->key; | |
2522 | priority_info pi = (priority_info) n->value; | |
0aafb128 MM |
2523 | |
2524 | /* Generate the functions themselves, but only if they are really | |
2525 | needed. */ | |
0352cfc8 | 2526 | if (pi->initializations_p |
0aafb128 | 2527 | || (priority == DEFAULT_INIT_PRIORITY && static_ctors)) |
848eed92 | 2528 | generate_ctor_or_dtor_function (/*constructor_p=*/true, priority); |
0352cfc8 | 2529 | if (pi->destructions_p |
0aafb128 | 2530 | || (priority == DEFAULT_INIT_PRIORITY && static_dtors)) |
848eed92 | 2531 | generate_ctor_or_dtor_function (/*constructor_p=*/false, priority); |
0aafb128 MM |
2532 | |
2533 | /* Keep iterating. */ | |
2534 | return 0; | |
2535 | } | |
2ce3c6c6 | 2536 | |
0aafb128 MM |
2537 | /* This routine is called from the last rule in yyparse (). |
2538 | Its job is to create all the code needed to initialize and | |
2539 | destroy the global aggregates. We do the destruction | |
2540 | first, since that way we only need to reverse the decls once. */ | |
44a8d0b3 | 2541 | |
0aafb128 MM |
2542 | void |
2543 | finish_file () | |
2544 | { | |
0aafb128 | 2545 | tree vars; |
848eed92 | 2546 | bool reconsider; |
0aafb128 | 2547 | size_t i; |
8d08fdba | 2548 | |
0aafb128 | 2549 | at_eof = 1; |
faae18ab | 2550 | |
0aafb128 MM |
2551 | /* Bad parse errors. Just forget about it. */ |
2552 | if (! global_bindings_p () || current_class_type || decl_namespace_list) | |
2553 | return; | |
8d08fdba | 2554 | |
17211ab5 GK |
2555 | if (pch_file) |
2556 | c_common_write_pch (); | |
2557 | ||
0aafb128 MM |
2558 | /* Otherwise, GDB can get confused, because in only knows |
2559 | about source for LINENO-1 lines. */ | |
2560 | lineno -= 1; | |
5566b478 | 2561 | |
0aafb128 MM |
2562 | interface_unknown = 1; |
2563 | interface_only = 0; | |
ea735e02 | 2564 | |
0aafb128 MM |
2565 | /* We now have to write out all the stuff we put off writing out. |
2566 | These include: | |
d2e5ee5c | 2567 | |
0aafb128 MM |
2568 | o Template specializations that we have not yet instantiated, |
2569 | but which are needed. | |
2570 | o Initialization and destruction for non-local objects with | |
2571 | static storage duration. (Local objects with static storage | |
2572 | duration are initialized when their scope is first entered, | |
2573 | and are cleaned up via atexit.) | |
2574 | o Virtual function tables. | |
ea735e02 | 2575 | |
0aafb128 MM |
2576 | All of these may cause others to be needed. For example, |
2577 | instantiating one function may cause another to be needed, and | |
0a288b9a | 2578 | generating the initializer for an object may cause templates to be |
0aafb128 | 2579 | instantiated, etc., etc. */ |
8d08fdba | 2580 | |
2a9a326b | 2581 | timevar_push (TV_VARCONST); |
8d08fdba | 2582 | |
8fa33dfa | 2583 | emit_support_tinfos (); |
7267d692 | 2584 | |
0aafb128 MM |
2585 | do |
2586 | { | |
548502d3 MM |
2587 | tree t; |
2588 | ||
848eed92 | 2589 | reconsider = false; |
0aafb128 | 2590 | |
1a6580ec MM |
2591 | /* If there are templates that we've put off instantiating, do |
2592 | them now. */ | |
0aafb128 MM |
2593 | instantiate_pending_templates (); |
2594 | ||
6eabb241 | 2595 | /* Write out virtual tables as required. Note that writing out |
9aad8f83 MA |
2596 | the virtual table for a template class may cause the |
2597 | instantiation of members of that class. If we write out | |
2598 | vtables then we remove the class from our list so we don't | |
2599 | have to look at it again. */ | |
2600 | ||
2601 | while (keyed_classes != NULL_TREE | |
2602 | && maybe_emit_vtables (TREE_VALUE (keyed_classes))) | |
2603 | { | |
2604 | reconsider = 1; | |
2605 | keyed_classes = TREE_CHAIN (keyed_classes); | |
2606 | } | |
2607 | ||
2608 | t = keyed_classes; | |
2609 | if (t != NULL_TREE) | |
2610 | { | |
2611 | tree next = TREE_CHAIN (t); | |
2612 | ||
2613 | while (next) | |
2614 | { | |
2615 | if (maybe_emit_vtables (TREE_VALUE (next))) | |
2616 | { | |
2617 | reconsider = 1; | |
2618 | TREE_CHAIN (t) = TREE_CHAIN (next); | |
2619 | } | |
2620 | else | |
2621 | t = next; | |
2622 | ||
2623 | next = TREE_CHAIN (t); | |
2624 | } | |
2625 | } | |
2626 | ||
7267d692 NS |
2627 | /* Write out needed type info variables. Writing out one variable |
2628 | might cause others to be needed. */ | |
d689a8f1 | 2629 | if (walk_globals (unemitted_tinfo_decl_p, emit_tinfo_decl, /*data=*/0)) |
848eed92 | 2630 | reconsider = true; |
7267d692 | 2631 | |
0aafb128 | 2632 | /* The list of objects with static storage duration is built up |
313bc2c2 MM |
2633 | in reverse order. We clear STATIC_AGGREGATES so that any new |
2634 | aggregates added during the initialization of these will be | |
2635 | initialized in the correct order when we next come around the | |
2636 | loop. */ | |
cec24319 | 2637 | vars = prune_vars_needing_no_initialization (&static_aggregates); |
c472cdfd | 2638 | |
313bc2c2 MM |
2639 | if (vars) |
2640 | { | |
2641 | tree v; | |
2642 | ||
2643 | /* We need to start a new initialization function each time | |
2644 | through the loop. That's because we need to know which | |
2645 | vtables have been referenced, and TREE_SYMBOL_REFERENCED | |
2646 | isn't computed until a function is finished, and written | |
2647 | out. That's a deficiency in the back-end. When this is | |
2648 | fixed, these initialization functions could all become | |
2649 | inline, with resulting performance improvements. */ | |
2650 | tree ssdf_body = start_static_storage_duration_function (); | |
2651 | ||
2652 | /* Make sure the back end knows about all the variables. */ | |
2653 | write_out_vars (vars); | |
2654 | ||
2655 | /* First generate code to do all the initializations. */ | |
2656 | for (v = vars; v; v = TREE_CHAIN (v)) | |
2657 | do_static_initialization (TREE_VALUE (v), | |
2658 | TREE_PURPOSE (v)); | |
2659 | ||
2660 | /* Then, generate code to do all the destructions. Do these | |
2661 | in reverse order so that the most recently constructed | |
bf419747 MM |
2662 | variable is the first destroyed. If we're using |
2663 | __cxa_atexit, then we don't need to do this; functions | |
4c0aad2c | 2664 | were registered at initialization time to destroy the |
bf419747 MM |
2665 | local statics. */ |
2666 | if (!flag_use_cxa_atexit) | |
2667 | { | |
2668 | vars = nreverse (vars); | |
2669 | for (v = vars; v; v = TREE_CHAIN (v)) | |
2670 | do_static_destruction (TREE_VALUE (v)); | |
2671 | } | |
2672 | else | |
2673 | vars = NULL_TREE; | |
313bc2c2 MM |
2674 | |
2675 | /* Finish up the static storage duration function for this | |
2676 | round. */ | |
2677 | finish_static_storage_duration_function (ssdf_body); | |
2678 | ||
2679 | /* All those initializations and finalizations might cause | |
2680 | us to need more inline functions, more template | |
2681 | instantiations, etc. */ | |
848eed92 | 2682 | reconsider = true; |
0aafb128 | 2683 | } |
0aafb128 | 2684 | |
56e770bf | 2685 | for (i = 0; i < deferred_fns_used; ++i) |
0aafb128 | 2686 | { |
56e770bf | 2687 | tree decl = VARRAY_TREE (deferred_fns, i); |
eab5474f | 2688 | |
0aafb128 | 2689 | import_export_decl (decl); |
eab5474f NS |
2690 | |
2691 | /* Does it need synthesizing? */ | |
0aafb128 MM |
2692 | if (DECL_ARTIFICIAL (decl) && ! DECL_INITIAL (decl) |
2693 | && TREE_USED (decl) | |
2694 | && (! DECL_REALLY_EXTERN (decl) || DECL_INLINE (decl))) | |
2695 | { | |
2696 | /* Even though we're already at the top-level, we push | |
2697 | there again. That way, when we pop back a few lines | |
2698 | hence, all of our state is restored. Otherwise, | |
2699 | finish_function doesn't clean things up, and we end | |
2700 | up with CURRENT_FUNCTION_DECL set. */ | |
2701 | push_to_top_level (); | |
1f84ec23 | 2702 | synthesize_method (decl); |
0aafb128 | 2703 | pop_from_top_level (); |
848eed92 | 2704 | reconsider = true; |
0aafb128 | 2705 | } |
0aafb128 | 2706 | |
eab5474f NS |
2707 | /* We lie to the back-end, pretending that some functions |
2708 | are not defined when they really are. This keeps these | |
2709 | functions from being put out unnecessarily. But, we must | |
2710 | stop lying when the functions are referenced, or if they | |
2711 | are not comdat since they need to be put out now. This | |
2712 | is done in a separate for cycle, because if some deferred | |
2713 | function is contained in another deferred function later | |
2714 | in deferred_fns varray, rest_of_compilation would skip | |
2715 | this function and we really cannot expand the same | |
2716 | function twice. */ | |
4cb02ea1 MM |
2717 | if (DECL_NOT_REALLY_EXTERN (decl) |
2718 | && DECL_INITIAL (decl) | |
7c913d33 | 2719 | && DECL_NEEDED_P (decl)) |
4cb02ea1 | 2720 | DECL_EXTERNAL (decl) = 0; |
21b0c6dc MM |
2721 | |
2722 | /* If we're going to need to write this function out, and | |
2723 | there's already a body for it, create RTL for it now. | |
2724 | (There might be no body if this is a method we haven't | |
2725 | gotten around to synthesizing yet.) */ | |
2726 | if (!DECL_EXTERNAL (decl) | |
7c913d33 | 2727 | && DECL_NEEDED_P (decl) |
21b0c6dc | 2728 | && DECL_SAVED_TREE (decl) |
21b0c6dc MM |
2729 | && !TREE_ASM_WRITTEN (decl)) |
2730 | { | |
2731 | int saved_not_really_extern; | |
2732 | ||
2733 | /* When we call finish_function in expand_body, it will | |
2734 | try to reset DECL_NOT_REALLY_EXTERN so we save and | |
2735 | restore it here. */ | |
1c1c0761 | 2736 | saved_not_really_extern = DECL_NOT_REALLY_EXTERN (decl); |
21b0c6dc MM |
2737 | /* Generate RTL for this function now that we know we |
2738 | need it. */ | |
2739 | expand_body (decl); | |
2740 | /* Undo the damage done by finish_function. */ | |
2741 | DECL_EXTERNAL (decl) = 0; | |
2742 | DECL_NOT_REALLY_EXTERN (decl) = saved_not_really_extern; | |
2743 | /* If we're compiling -fsyntax-only pretend that this | |
2744 | function has been written out so that we don't try to | |
2745 | expand it again. */ | |
2746 | if (flag_syntax_only) | |
2747 | TREE_ASM_WRITTEN (decl) = 1; | |
848eed92 | 2748 | reconsider = true; |
21b0c6dc | 2749 | } |
4cb02ea1 MM |
2750 | } |
2751 | ||
56e770bf MM |
2752 | if (deferred_fns_used |
2753 | && wrapup_global_declarations (&VARRAY_TREE (deferred_fns, 0), | |
2754 | deferred_fns_used)) | |
848eed92 | 2755 | reconsider = true; |
4cb02ea1 | 2756 | if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0)) |
848eed92 | 2757 | reconsider = true; |
0aafb128 MM |
2758 | |
2759 | /* Static data members are just like namespace-scope globals. */ | |
2760 | for (i = 0; i < pending_statics_used; ++i) | |
2761 | { | |
2762 | tree decl = VARRAY_TREE (pending_statics, i); | |
2763 | if (TREE_ASM_WRITTEN (decl)) | |
2764 | continue; | |
2765 | import_export_decl (decl); | |
2766 | if (DECL_NOT_REALLY_EXTERN (decl) && ! DECL_IN_AGGR_P (decl)) | |
2767 | DECL_EXTERNAL (decl) = 0; | |
2768 | } | |
4cb02ea1 MM |
2769 | if (pending_statics |
2770 | && wrapup_global_declarations (&VARRAY_TREE (pending_statics, 0), | |
2771 | pending_statics_used)) | |
848eed92 | 2772 | reconsider = true; |
0352cfc8 | 2773 | } |
0aafb128 | 2774 | while (reconsider); |
28cbf42c | 2775 | |
eab5474f NS |
2776 | /* All used inline functions must have a definition at this point. */ |
2777 | for (i = 0; i < deferred_fns_used; ++i) | |
2778 | { | |
2779 | tree decl = VARRAY_TREE (deferred_fns, i); | |
2780 | ||
2781 | if (TREE_USED (decl) && DECL_DECLARED_INLINE_P (decl) | |
2782 | && !(TREE_ASM_WRITTEN (decl) || DECL_SAVED_TREE (decl))) | |
3cfabe60 NS |
2783 | { |
2784 | cp_warning_at ("inline function `%D' used but never defined", decl); | |
2785 | /* This symbol is effectively an "extern" declaration now. | |
2786 | This is not strictly necessary, but removes a duplicate | |
2787 | warning. */ | |
2788 | TREE_PUBLIC (decl) = 1; | |
2789 | } | |
2790 | ||
eab5474f NS |
2791 | } |
2792 | ||
0352cfc8 MM |
2793 | /* We give C linkage to static constructors and destructors. */ |
2794 | push_lang_context (lang_name_c); | |
2795 | ||
2796 | /* Generate initialization and destruction functions for all | |
2797 | priorities for which they are required. */ | |
2798 | if (priority_info_map) | |
2799 | splay_tree_foreach (priority_info_map, | |
2800 | generate_ctor_and_dtor_functions_for_priority, | |
2801 | /*data=*/0); | |
2802 | ||
2803 | /* We're done with the splay-tree now. */ | |
2804 | if (priority_info_map) | |
2805 | splay_tree_delete (priority_info_map); | |
2806 | ||
2807 | /* We're done with static constructors, so we can go back to "C++" | |
2808 | linkage now. */ | |
2809 | pop_lang_context (); | |
2810 | ||
0aafb128 | 2811 | /* Now, issue warnings about static, but not defined, functions, |
033ed340 | 2812 | etc., and emit debugging information. */ |
0aafb128 | 2813 | walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider); |
033ed340 JM |
2814 | if (pending_statics) |
2815 | check_global_declarations (&VARRAY_TREE (pending_statics, 0), | |
2816 | pending_statics_used); | |
b7484fbe | 2817 | |
faae18ab MS |
2818 | finish_repo (); |
2819 | ||
f71f87f9 | 2820 | /* The entire file is now complete. If requested, dump everything |
a1c65f9f | 2821 | to a file. */ |
b7442fb5 NS |
2822 | { |
2823 | int flags; | |
2824 | FILE *stream = dump_begin (TDI_all, &flags); | |
2825 | ||
2826 | if (stream) | |
2827 | { | |
2828 | dump_node (global_namespace, flags & ~TDF_SLIM, stream); | |
2829 | dump_end (TDI_all, stream); | |
2830 | } | |
2831 | } | |
2832 | ||
2a9a326b | 2833 | timevar_pop (TV_VARCONST); |
8d08fdba MS |
2834 | |
2835 | if (flag_detailed_statistics) | |
27bb8339 JM |
2836 | { |
2837 | dump_tree_statistics (); | |
2838 | dump_time_statistics (); | |
2839 | } | |
8d08fdba | 2840 | } |
51c184be | 2841 | |
4ba126e4 MM |
2842 | /* T is the parse tree for an expression. Return the expression after |
2843 | performing semantic analysis. */ | |
5566b478 MS |
2844 | |
2845 | tree | |
2846 | build_expr_from_tree (t) | |
2847 | tree t; | |
51c184be | 2848 | { |
5566b478 MS |
2849 | if (t == NULL_TREE || t == error_mark_node) |
2850 | return t; | |
2851 | ||
2852 | switch (TREE_CODE (t)) | |
51c184be MS |
2853 | { |
2854 | case IDENTIFIER_NODE: | |
afd9b9dd | 2855 | return do_identifier (t, NULL_TREE); |
5566b478 MS |
2856 | |
2857 | case LOOKUP_EXPR: | |
2858 | if (LOOKUP_EXPR_GLOBAL (t)) | |
80b1331c MM |
2859 | { |
2860 | tree token = TREE_OPERAND (t, 0); | |
2861 | return do_scoped_id (token, IDENTIFIER_GLOBAL_VALUE (token)); | |
2862 | } | |
5566b478 | 2863 | else |
a723baf1 | 2864 | { |
afd9b9dd | 2865 | t = do_identifier (TREE_OPERAND (t, 0), NULL_TREE); |
a723baf1 MM |
2866 | if (TREE_CODE (t) == ALIAS_DECL) |
2867 | t = DECL_INITIAL (t); | |
2868 | return t; | |
2869 | } | |
5566b478 | 2870 | |
386b8a85 | 2871 | case TEMPLATE_ID_EXPR: |
4ba126e4 MM |
2872 | { |
2873 | tree template; | |
2874 | tree args; | |
2875 | tree object; | |
2876 | ||
2877 | template = build_expr_from_tree (TREE_OPERAND (t, 0)); | |
2878 | args = build_expr_from_tree (TREE_OPERAND (t, 1)); | |
2879 | ||
2880 | if (TREE_CODE (template) == COMPONENT_REF) | |
2881 | { | |
2882 | object = TREE_OPERAND (template, 0); | |
2883 | template = TREE_OPERAND (template, 1); | |
2884 | } | |
2885 | else | |
2886 | object = NULL_TREE; | |
2887 | ||
2888 | template = lookup_template_function (template, args); | |
2889 | if (object) | |
2890 | return build (COMPONENT_REF, TREE_TYPE (template), | |
2891 | object, template); | |
2892 | else | |
2893 | return template; | |
2894 | } | |
386b8a85 | 2895 | |
51c184be MS |
2896 | case INDIRECT_REF: |
2897 | return build_x_indirect_ref | |
5566b478 MS |
2898 | (build_expr_from_tree (TREE_OPERAND (t, 0)), "unary *"); |
2899 | ||
2900 | case CAST_EXPR: | |
2901 | return build_functional_cast | |
2902 | (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0))); | |
2903 | ||
2904 | case REINTERPRET_CAST_EXPR: | |
2905 | return build_reinterpret_cast | |
2906 | (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0))); | |
2907 | ||
e92cc029 MS |
2908 | case CONST_CAST_EXPR: |
2909 | return build_const_cast | |
2910 | (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0))); | |
2911 | ||
2912 | case DYNAMIC_CAST_EXPR: | |
2913 | return build_dynamic_cast | |
2914 | (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0))); | |
2915 | ||
2916 | case STATIC_CAST_EXPR: | |
2917 | return build_static_cast | |
2918 | (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0))); | |
2919 | ||
5566b478 MS |
2920 | case PREDECREMENT_EXPR: |
2921 | case PREINCREMENT_EXPR: | |
2922 | case POSTDECREMENT_EXPR: | |
2923 | case POSTINCREMENT_EXPR: | |
2924 | case NEGATE_EXPR: | |
51c184be | 2925 | case BIT_NOT_EXPR: |
5566b478 MS |
2926 | case ABS_EXPR: |
2927 | case TRUTH_NOT_EXPR: | |
2928 | case ADDR_EXPR: | |
2929 | case CONVERT_EXPR: /* Unary + */ | |
f5733617 SS |
2930 | case REALPART_EXPR: |
2931 | case IMAGPART_EXPR: | |
fc378698 MS |
2932 | if (TREE_TYPE (t)) |
2933 | return t; | |
5566b478 MS |
2934 | return build_x_unary_op (TREE_CODE (t), |
2935 | build_expr_from_tree (TREE_OPERAND (t, 0))); | |
2936 | ||
2937 | case PLUS_EXPR: | |
2938 | case MINUS_EXPR: | |
2939 | case MULT_EXPR: | |
2940 | case TRUNC_DIV_EXPR: | |
2941 | case CEIL_DIV_EXPR: | |
2942 | case FLOOR_DIV_EXPR: | |
2943 | case ROUND_DIV_EXPR: | |
2944 | case EXACT_DIV_EXPR: | |
2945 | case BIT_AND_EXPR: | |
2946 | case BIT_ANDTC_EXPR: | |
2947 | case BIT_IOR_EXPR: | |
2948 | case BIT_XOR_EXPR: | |
2949 | case TRUNC_MOD_EXPR: | |
2950 | case FLOOR_MOD_EXPR: | |
2951 | case TRUTH_ANDIF_EXPR: | |
2952 | case TRUTH_ORIF_EXPR: | |
2953 | case TRUTH_AND_EXPR: | |
2954 | case TRUTH_OR_EXPR: | |
2955 | case RSHIFT_EXPR: | |
2956 | case LSHIFT_EXPR: | |
2957 | case RROTATE_EXPR: | |
2958 | case LROTATE_EXPR: | |
2959 | case EQ_EXPR: | |
2960 | case NE_EXPR: | |
2961 | case MAX_EXPR: | |
2962 | case MIN_EXPR: | |
2963 | case LE_EXPR: | |
2964 | case GE_EXPR: | |
2965 | case LT_EXPR: | |
2966 | case GT_EXPR: | |
2967 | case MEMBER_REF: | |
2968 | return build_x_binary_op | |
2969 | (TREE_CODE (t), | |
2970 | build_expr_from_tree (TREE_OPERAND (t, 0)), | |
2971 | build_expr_from_tree (TREE_OPERAND (t, 1))); | |
2972 | ||
2973 | case DOTSTAR_EXPR: | |
2974 | return build_m_component_ref | |
2975 | (build_expr_from_tree (TREE_OPERAND (t, 0)), | |
2976 | build_expr_from_tree (TREE_OPERAND (t, 1))); | |
2977 | ||
a28e3c7f | 2978 | case SCOPE_REF: |
5566b478 MS |
2979 | return build_offset_ref (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)); |
2980 | ||
a28e3c7f | 2981 | case ARRAY_REF: |
5566b478 MS |
2982 | if (TREE_OPERAND (t, 0) == NULL_TREE) |
2983 | /* new-type-id */ | |
718b8ea5 JM |
2984 | return build_nt (ARRAY_REF, NULL_TREE, |
2985 | build_expr_from_tree (TREE_OPERAND (t, 1))); | |
5566b478 MS |
2986 | return grok_array_decl (build_expr_from_tree (TREE_OPERAND (t, 0)), |
2987 | build_expr_from_tree (TREE_OPERAND (t, 1))); | |
2988 | ||
2989 | case SIZEOF_EXPR: | |
abff8e06 | 2990 | case ALIGNOF_EXPR: |
5566b478 MS |
2991 | { |
2992 | tree r = build_expr_from_tree (TREE_OPERAND (t, 0)); | |
2f939d94 | 2993 | if (!TYPE_P (r)) |
0213a355 JM |
2994 | return TREE_CODE (t) == SIZEOF_EXPR ? expr_sizeof (r) : c_alignof_expr (r); |
2995 | else | |
ea793912 | 2996 | return cxx_sizeof_or_alignof_type (r, TREE_CODE (t), true); |
5566b478 MS |
2997 | } |
2998 | ||
2999 | case MODOP_EXPR: | |
3000 | return build_x_modify_expr | |
3001 | (build_expr_from_tree (TREE_OPERAND (t, 0)), | |
3002 | TREE_CODE (TREE_OPERAND (t, 1)), | |
3003 | build_expr_from_tree (TREE_OPERAND (t, 2))); | |
3004 | ||
3005 | case ARROW_EXPR: | |
3006 | return build_x_arrow | |
3007 | (build_expr_from_tree (TREE_OPERAND (t, 0))); | |
3008 | ||
3009 | case NEW_EXPR: | |
3010 | return build_new | |
3011 | (build_expr_from_tree (TREE_OPERAND (t, 0)), | |
3012 | build_expr_from_tree (TREE_OPERAND (t, 1)), | |
3013 | build_expr_from_tree (TREE_OPERAND (t, 2)), | |
3014 | NEW_EXPR_USE_GLOBAL (t)); | |
3015 | ||
3016 | case DELETE_EXPR: | |
3017 | return delete_sanity | |
3018 | (build_expr_from_tree (TREE_OPERAND (t, 0)), | |
3019 | build_expr_from_tree (TREE_OPERAND (t, 1)), | |
3020 | DELETE_EXPR_USE_VEC (t), DELETE_EXPR_USE_GLOBAL (t)); | |
3021 | ||
3022 | case COMPOUND_EXPR: | |
3023 | if (TREE_OPERAND (t, 1) == NULL_TREE) | |
3024 | return build_x_compound_expr | |
3025 | (build_expr_from_tree (TREE_OPERAND (t, 0))); | |
3026 | else | |
a98facb0 | 3027 | abort (); |
5566b478 MS |
3028 | |
3029 | case METHOD_CALL_EXPR: | |
3030 | if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF) | |
3031 | { | |
3032 | tree ref = TREE_OPERAND (t, 0); | |
fd037e0d NS |
3033 | tree name = TREE_OPERAND (ref, 1); |
3034 | ||
3035 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) | |
3036 | name = build_nt (TEMPLATE_ID_EXPR, | |
3037 | TREE_OPERAND (name, 0), | |
3038 | build_expr_from_tree (TREE_OPERAND (name, 1))); | |
3039 | ||
5566b478 MS |
3040 | return build_scoped_method_call |
3041 | (build_expr_from_tree (TREE_OPERAND (t, 1)), | |
3042 | build_expr_from_tree (TREE_OPERAND (ref, 0)), | |
fd037e0d | 3043 | name, |
5566b478 MS |
3044 | build_expr_from_tree (TREE_OPERAND (t, 2))); |
3045 | } | |
d8f8dca1 MM |
3046 | else |
3047 | { | |
3048 | tree fn = TREE_OPERAND (t, 0); | |
074917ba | 3049 | |
d8f8dca1 MM |
3050 | /* We can get a TEMPLATE_ID_EXPR here on code like: |
3051 | ||
3052 | x->f<2>(); | |
3053 | ||
3054 | so we must resolve that. However, we can also get things | |
3055 | like a BIT_NOT_EXPR here, when referring to a destructor, | |
3056 | and things like that are not correctly resolved by | |
3057 | build_expr_from_tree. So, just use build_expr_from_tree | |
3058 | when we really need it. */ | |
3059 | if (TREE_CODE (fn) == TEMPLATE_ID_EXPR) | |
074917ba JM |
3060 | fn = lookup_template_function |
3061 | (TREE_OPERAND (fn, 0), | |
3062 | build_expr_from_tree (TREE_OPERAND (fn, 1))); | |
d8f8dca1 MM |
3063 | |
3064 | return build_method_call | |
3065 | (build_expr_from_tree (TREE_OPERAND (t, 1)), | |
3066 | fn, | |
3067 | build_expr_from_tree (TREE_OPERAND (t, 2)), | |
3068 | NULL_TREE, LOOKUP_NORMAL); | |
3069 | } | |
5566b478 MS |
3070 | |
3071 | case CALL_EXPR: | |
3072 | if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF) | |
3073 | { | |
3074 | tree ref = TREE_OPERAND (t, 0); | |
fd037e0d | 3075 | tree name = TREE_OPERAND (ref, 1); |
3e14cd30 | 3076 | tree fn, scope, args; |
fd037e0d NS |
3077 | |
3078 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) | |
3079 | name = build_nt (TEMPLATE_ID_EXPR, | |
3080 | TREE_OPERAND (name, 0), | |
3081 | build_expr_from_tree (TREE_OPERAND (name, 1))); | |
3e14cd30 NS |
3082 | |
3083 | scope = build_expr_from_tree (TREE_OPERAND (ref, 0)); | |
3084 | args = build_expr_from_tree (TREE_OPERAND (t, 1)); | |
3085 | fn = resolve_scoped_fn_name (scope, name); | |
3086 | ||
3087 | return build_call_from_tree (fn, args, 1); | |
5566b478 MS |
3088 | } |
3089 | else | |
3090 | { | |
3091 | tree name = TREE_OPERAND (t, 0); | |
a759e627 ML |
3092 | tree id; |
3093 | tree args = build_expr_from_tree (TREE_OPERAND (t, 1)); | |
3094 | if (args != NULL_TREE && TREE_CODE (name) == LOOKUP_EXPR | |
3095 | && !LOOKUP_EXPR_GLOBAL (name) | |
3096 | && TREE_CODE ((id = TREE_OPERAND (name, 0))) == IDENTIFIER_NODE | |
3097 | && (!current_class_type | |
86ac0575 | 3098 | || !lookup_member (current_class_type, id, 0, false))) |
a759e627 | 3099 | { |
a1c65f9f | 3100 | /* Do Koenig lookup if there are no class members. */ |
afd9b9dd | 3101 | name = do_identifier (id, args); |
a759e627 ML |
3102 | } |
3103 | else if (TREE_CODE (name) == TEMPLATE_ID_EXPR | |
4ba126e4 | 3104 | || ! really_overloaded_fn (name)) |
5566b478 | 3105 | name = build_expr_from_tree (name); |
4ba126e4 MM |
3106 | |
3107 | if (TREE_CODE (name) == OFFSET_REF) | |
3108 | return build_offset_ref_call_from_tree (name, args); | |
3109 | if (TREE_CODE (name) == COMPONENT_REF) | |
3110 | return finish_object_call_expr (TREE_OPERAND (name, 1), | |
3111 | TREE_OPERAND (name, 0), | |
3112 | args); | |
3113 | name = convert_from_reference (name); | |
3114 | return build_call_from_tree (name, args, | |
3115 | /*disallow_virtual=*/false); | |
5566b478 MS |
3116 | } |
3117 | ||
3118 | case COND_EXPR: | |
3119 | return build_x_conditional_expr | |
3120 | (build_expr_from_tree (TREE_OPERAND (t, 0)), | |
3121 | build_expr_from_tree (TREE_OPERAND (t, 1)), | |
3122 | build_expr_from_tree (TREE_OPERAND (t, 2))); | |
3123 | ||
40242ccf | 3124 | case PSEUDO_DTOR_EXPR: |
a723baf1 | 3125 | return (finish_pseudo_destructor_expr |
40242ccf MM |
3126 | (build_expr_from_tree (TREE_OPERAND (t, 0)), |
3127 | build_expr_from_tree (TREE_OPERAND (t, 1)), | |
3128 | build_expr_from_tree (TREE_OPERAND (t, 2)))); | |
3129 | ||
5566b478 MS |
3130 | case TREE_LIST: |
3131 | { | |
3132 | tree purpose, value, chain; | |
3133 | ||
3134 | if (t == void_list_node) | |
3135 | return t; | |
3136 | ||
3137 | purpose = TREE_PURPOSE (t); | |
3138 | if (purpose) | |
3139 | purpose = build_expr_from_tree (purpose); | |
3140 | value = TREE_VALUE (t); | |
3141 | if (value) | |
3142 | value = build_expr_from_tree (value); | |
3143 | chain = TREE_CHAIN (t); | |
3144 | if (chain && chain != void_type_node) | |
3145 | chain = build_expr_from_tree (chain); | |
e1b3e07d | 3146 | return tree_cons (purpose, value, chain); |
5566b478 MS |
3147 | } |
3148 | ||
3149 | case COMPONENT_REF: | |
74322f31 MM |
3150 | { |
3151 | tree object = build_expr_from_tree (TREE_OPERAND (t, 0)); | |
a723baf1 MM |
3152 | tree member = TREE_OPERAND (t, 1); |
3153 | ||
3154 | if (!CLASS_TYPE_P (TREE_TYPE (object))) | |
3155 | { | |
3156 | if (TREE_CODE (member) == BIT_NOT_EXPR) | |
3157 | return finish_pseudo_destructor_expr (object, | |
3158 | NULL_TREE, | |
3159 | TREE_TYPE (object)); | |
3160 | else if (TREE_CODE (member) == SCOPE_REF | |
3161 | && (TREE_CODE (TREE_OPERAND (member, 1)) == BIT_NOT_EXPR)) | |
3162 | return finish_pseudo_destructor_expr (object, | |
3163 | TREE_OPERAND (t, 0), | |
3164 | TREE_TYPE (object)); | |
3165 | } | |
3166 | else if (TREE_CODE (member) == SCOPE_REF | |
3167 | && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR) | |
3168 | { | |
3169 | tree tmpl; | |
3170 | tree args; | |
3171 | ||
3172 | /* Lookup the template functions now that we know what the | |
3173 | scope is. */ | |
3174 | tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0); | |
3175 | args = TREE_OPERAND (TREE_OPERAND (member, 1), 1); | |
3176 | member = lookup_qualified_name (TREE_OPERAND (member, 0), | |
3177 | tmpl, | |
3178 | /*is_type=*/0, | |
3179 | /*flags=*/0); | |
3180 | if (BASELINK_P (member)) | |
3181 | BASELINK_FUNCTIONS (member) | |
3182 | = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member), | |
3183 | args); | |
3184 | else | |
3185 | { | |
3186 | error ("`%D' is not a member of `%T'", | |
3187 | tmpl, TREE_TYPE (object)); | |
3188 | return error_mark_node; | |
3189 | } | |
3190 | } | |
3191 | ||
3192 | ||
3193 | return finish_class_member_access_expr (object, member); | |
74322f31 MM |
3194 | } |
3195 | ||
fc378698 MS |
3196 | case THROW_EXPR: |
3197 | return build_throw (build_expr_from_tree (TREE_OPERAND (t, 0))); | |
3198 | ||
5156628f MS |
3199 | case CONSTRUCTOR: |
3200 | { | |
2fcdec57 | 3201 | tree r; |
b8b98c66 NS |
3202 | tree elts; |
3203 | tree type = TREE_TYPE (t); | |
3204 | bool purpose_p; | |
2fcdec57 JM |
3205 | |
3206 | /* digest_init will do the wrong thing if we let it. */ | |
b8b98c66 | 3207 | if (type && TYPE_PTRMEMFUNC_P (type)) |
2fcdec57 JM |
3208 | return t; |
3209 | ||
b8b98c66 NS |
3210 | r = NULL_TREE; |
3211 | /* We do not want to process the purpose of aggregate | |
3212 | initializers as they are identifier nodes which will be | |
3213 | looked up by digest_init. */ | |
3214 | purpose_p = !(type && IS_AGGR_TYPE (type)); | |
3215 | for (elts = CONSTRUCTOR_ELTS (t); elts; elts = TREE_CHAIN (elts)) | |
3216 | { | |
3217 | tree purpose = TREE_PURPOSE (elts); | |
3218 | tree value = TREE_VALUE (elts); | |
3219 | ||
3220 | if (purpose && purpose_p) | |
3221 | purpose = build_expr_from_tree (purpose); | |
3222 | value = build_expr_from_tree (value); | |
3223 | r = tree_cons (purpose, value, r); | |
3224 | } | |
3225 | ||
3226 | r = build_nt (CONSTRUCTOR, NULL_TREE, nreverse (r)); | |
8452b1d3 | 3227 | TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t); |
5156628f | 3228 | |
b8b98c66 NS |
3229 | if (type) |
3230 | return digest_init (type, r, 0); | |
5156628f MS |
3231 | return r; |
3232 | } | |
3233 | ||
3234 | case TYPEID_EXPR: | |
2f939d94 | 3235 | if (TYPE_P (TREE_OPERAND (t, 0))) |
e9f32eb5 | 3236 | return get_typeid (TREE_OPERAND (t, 0)); |
e5f614d7 | 3237 | return build_typeid (build_expr_from_tree (TREE_OPERAND (t, 0))); |
5156628f | 3238 | |
a723baf1 | 3239 | case PARM_DECL: |
9a3b49ac MS |
3240 | case VAR_DECL: |
3241 | return convert_from_reference (t); | |
3242 | ||
371534a9 MM |
3243 | case VA_ARG_EXPR: |
3244 | return build_va_arg (build_expr_from_tree (TREE_OPERAND (t, 0)), | |
3245 | TREE_TYPE (t)); | |
3246 | ||
8926095f | 3247 | default: |
5566b478 | 3248 | return t; |
51c184be | 3249 | } |
51c184be MS |
3250 | } |
3251 | ||
4ba126e4 MM |
3252 | /* FN is an OFFSET_REF indicating the function to call in parse-tree |
3253 | form; it has not yet been semantically analyzed. ARGS are the | |
3254 | arguments to the function. They have already been semantically | |
3255 | analzyed. */ | |
3256 | ||
3257 | tree | |
3258 | build_offset_ref_call_from_tree (tree fn, tree args) | |
3259 | { | |
3260 | tree object_addr; | |
3261 | ||
3262 | my_friendly_assert (TREE_CODE (fn) == OFFSET_REF, 20020725); | |
3263 | ||
3264 | /* A qualified name corresponding to a non-static member | |
3265 | function or a pointer-to-member is represented as an | |
3266 | OFFSET_REF. | |
3267 | ||
3268 | For both of these function calls, FN will be an OFFSET_REF. | |
3269 | ||
3270 | struct A { void f(); }; | |
3271 | void A::f() { (A::f) (); } | |
3272 | ||
3273 | struct B { void g(); }; | |
3274 | void (B::*p)(); | |
3275 | void B::g() { (this->*p)(); } */ | |
3276 | ||
3277 | /* This code is not really correct (for example, it does not | |
3278 | handle the case that `A::f' is overloaded), but it is | |
3279 | historically how we have handled this situation. */ | |
3280 | object_addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (fn, 0), 0); | |
3281 | if (TREE_CODE (TREE_OPERAND (fn, 1)) == FIELD_DECL) | |
3282 | fn = resolve_offset_ref (fn); | |
3283 | else | |
3284 | { | |
3285 | fn = TREE_OPERAND (fn, 1); | |
3286 | fn = get_member_function_from_ptrfunc (&object_addr, fn); | |
3287 | } | |
3288 | args = tree_cons (NULL_TREE, object_addr, args); | |
3289 | return build_function_call (fn, args); | |
3290 | } | |
3291 | ||
3292 | /* FN indicates the function to call. Name resolution has been | |
3293 | performed on FN. ARGS are the arguments to the function. They | |
3294 | have already been semantically analyzed. DISALLOW_VIRTUAL is true | |
3295 | if the function call should be determined at compile time, even if | |
3296 | FN is virtual. */ | |
3297 | ||
3298 | tree | |
3299 | build_call_from_tree (tree fn, tree args, bool disallow_virtual) | |
3300 | { | |
3301 | tree template_args; | |
3302 | tree template_id; | |
3303 | tree f; | |
3304 | ||
3305 | /* Check to see that name lookup has already been performed. */ | |
3306 | my_friendly_assert (TREE_CODE (fn) != OFFSET_REF, 20020725); | |
3307 | my_friendly_assert (TREE_CODE (fn) != SCOPE_REF, 20020725); | |
3308 | ||
3309 | /* In the future all of this should be eliminated. Instead, | |
3310 | name-lookup for a member function should simply return a | |
3311 | baselink, instead of a FUNCTION_DECL, TEMPLATE_DECL, or | |
3312 | TEMPLATE_ID_EXPR. */ | |
3313 | ||
3314 | if (TREE_CODE (fn) == TEMPLATE_ID_EXPR) | |
3315 | { | |
3316 | template_id = fn; | |
3317 | template_args = TREE_OPERAND (fn, 1); | |
3318 | fn = TREE_OPERAND (fn, 0); | |
3319 | } | |
3320 | else | |
e65e6212 KG |
3321 | { |
3322 | template_id = NULL_TREE; | |
3323 | template_args = NULL_TREE; | |
3324 | } | |
4ba126e4 MM |
3325 | |
3326 | f = (TREE_CODE (fn) == OVERLOAD) ? get_first_fn (fn) : fn; | |
3327 | /* Make sure we have a baselink (rather than simply a | |
3328 | FUNCTION_DECL) for a member function. */ | |
3329 | if (current_class_type | |
3330 | && ((TREE_CODE (f) == FUNCTION_DECL | |
3331 | && DECL_FUNCTION_MEMBER_P (f)) | |
3332 | || (DECL_FUNCTION_TEMPLATE_P (f) | |
3333 | && DECL_FUNCTION_MEMBER_P (f)))) | |
3334 | { | |
3335 | f = lookup_member (current_class_type, DECL_NAME (f), | |
86ac0575 | 3336 | /*protect=*/1, /*want_type=*/false); |
4ba126e4 MM |
3337 | if (f) |
3338 | fn = f; | |
3339 | } | |
3340 | ||
3341 | if (template_id) | |
3342 | { | |
3343 | if (BASELINK_P (fn)) | |
3344 | BASELINK_FUNCTIONS (fn) = build_nt (TEMPLATE_ID_EXPR, | |
3345 | BASELINK_FUNCTIONS (fn), | |
3346 | template_args); | |
3347 | else | |
3348 | fn = template_id; | |
3349 | } | |
3350 | ||
3351 | return finish_call_expr (fn, args, disallow_virtual); | |
3352 | } | |
3353 | ||
089d6ea7 MM |
3354 | /* Returns true if ROOT (a namespace, class, or function) encloses |
3355 | CHILD. CHILD may be either a class type or a namespace. */ | |
2c73f9f5 | 3356 | |
089d6ea7 MM |
3357 | bool |
3358 | is_ancestor (tree root, tree child) | |
30394414 | 3359 | { |
089d6ea7 MM |
3360 | my_friendly_assert ((TREE_CODE (root) == NAMESPACE_DECL |
3361 | || TREE_CODE (root) == FUNCTION_DECL | |
3362 | || CLASS_TYPE_P (root)), 20030307); | |
3363 | my_friendly_assert ((TREE_CODE (child) == NAMESPACE_DECL | |
3364 | || CLASS_TYPE_P (child)), | |
3365 | 20030307); | |
3366 | ||
3367 | /* The global namespace encloses everything. */ | |
30394414 | 3368 | if (root == global_namespace) |
848eed92 | 3369 | return true; |
089d6ea7 MM |
3370 | |
3371 | while (true) | |
3372 | { | |
3373 | /* If we've run out of scopes, stop. */ | |
3374 | if (!child) | |
3375 | return false; | |
3376 | /* If we've reached the ROOT, it encloses CHILD. */ | |
3377 | if (root == child) | |
3378 | return true; | |
3379 | /* Go out one level. */ | |
3380 | if (TYPE_P (child)) | |
3381 | child = TYPE_NAME (child); | |
3382 | child = DECL_CONTEXT (child); | |
3383 | } | |
30394414 JM |
3384 | } |
3385 | ||
3386 | ||
2c73f9f5 | 3387 | /* Return the namespace that is the common ancestor |
a1c65f9f | 3388 | of two given namespaces. */ |
2c73f9f5 | 3389 | |
9ed182dc | 3390 | tree |
848eed92 | 3391 | namespace_ancestor (tree ns1, tree ns2) |
30394414 | 3392 | { |
fd295cb2 | 3393 | timevar_push (TV_NAME_LOOKUP); |
089d6ea7 | 3394 | if (is_ancestor (ns1, ns2)) |
fd295cb2 GDR |
3395 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ns1); |
3396 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, | |
3397 | namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2)); | |
30394414 JM |
3398 | } |
3399 | ||
848eed92 | 3400 | /* Insert USED into the using list of USER. Set INDIRECT_flag if this |
30394414 JM |
3401 | directive is not directly from the source. Also find the common |
3402 | ancestor and let our users know about the new namespace */ | |
3403 | static void | |
848eed92 | 3404 | add_using_namespace (tree user, tree used, bool indirect) |
30394414 | 3405 | { |
52c11ef6 | 3406 | tree t; |
fd295cb2 | 3407 | timevar_push (TV_NAME_LOOKUP); |
a1c65f9f | 3408 | /* Using oneself is a no-op. */ |
30394414 | 3409 | if (user == used) |
fd295cb2 | 3410 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (void)0); |
30394414 JM |
3411 | my_friendly_assert (TREE_CODE (user) == NAMESPACE_DECL, 380); |
3412 | my_friendly_assert (TREE_CODE (used) == NAMESPACE_DECL, 380); | |
a1c65f9f | 3413 | /* Check if we already have this. */ |
52c11ef6 JM |
3414 | t = purpose_member (used, DECL_NAMESPACE_USING (user)); |
3415 | if (t != NULL_TREE) | |
3416 | { | |
3417 | if (!indirect) | |
a1c65f9f | 3418 | /* Promote to direct usage. */ |
52c11ef6 | 3419 | TREE_INDIRECT_USING (t) = 0; |
fd295cb2 | 3420 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (void)0); |
52c11ef6 | 3421 | } |
30394414 | 3422 | |
a1c65f9f | 3423 | /* Add used to the user's using list. */ |
30394414 | 3424 | DECL_NAMESPACE_USING (user) |
e1b3e07d MM |
3425 | = tree_cons (used, namespace_ancestor (user, used), |
3426 | DECL_NAMESPACE_USING (user)); | |
30394414 JM |
3427 | |
3428 | TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect; | |
3429 | ||
a1c65f9f | 3430 | /* Add user to the used's users list. */ |
30394414 | 3431 | DECL_NAMESPACE_USERS (used) |
e1b3e07d | 3432 | = tree_cons (user, 0, DECL_NAMESPACE_USERS (used)); |
85c6cbaf | 3433 | |
a1c65f9f | 3434 | /* Recursively add all namespaces used. */ |
52c11ef6 | 3435 | for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t)) |
30394414 | 3436 | /* indirect usage */ |
52c11ef6 | 3437 | add_using_namespace (user, TREE_PURPOSE (t), 1); |
85c6cbaf | 3438 | |
a1c65f9f | 3439 | /* Tell everyone using us about the new used namespaces. */ |
52c11ef6 JM |
3440 | for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t)) |
3441 | add_using_namespace (TREE_PURPOSE (t), used, 1); | |
fd295cb2 | 3442 | timevar_pop (TV_NAME_LOOKUP); |
30394414 JM |
3443 | } |
3444 | ||
5eea678f JM |
3445 | /* Combines two sets of overloaded functions into an OVERLOAD chain, removing |
3446 | duplicates. The first list becomes the tail of the result. | |
3447 | ||
61a127b3 MM |
3448 | The algorithm is O(n^2). We could get this down to O(n log n) by |
3449 | doing a sort on the addresses of the functions, if that becomes | |
3450 | necessary. */ | |
2c73f9f5 ML |
3451 | |
3452 | static tree | |
848eed92 | 3453 | merge_functions (tree s1, tree s2) |
2c73f9f5 | 3454 | { |
5eea678f JM |
3455 | for (; s2; s2 = OVL_NEXT (s2)) |
3456 | { | |
998979e6 MM |
3457 | tree fn2 = OVL_CURRENT (s2); |
3458 | tree fns1; | |
3459 | ||
3460 | for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1)) | |
3461 | { | |
3462 | tree fn1 = OVL_CURRENT (fns1); | |
3463 | ||
3464 | /* If the function from S2 is already in S1, there is no | |
3465 | need to add it again. For `extern "C"' functions, we | |
3466 | might have two FUNCTION_DECLs for the same function, in | |
3467 | different namespaces; again, we only need one of them. */ | |
3468 | if (fn1 == fn2 | |
3469 | || (DECL_EXTERN_C_P (fn1) && DECL_EXTERN_C_P (fn2) | |
3470 | && DECL_NAME (fn1) == DECL_NAME (fn2))) | |
3471 | break; | |
3472 | } | |
3473 | ||
3474 | /* If we exhausted all of the functions in S1, FN2 is new. */ | |
3475 | if (!fns1) | |
3476 | s1 = build_overload (fn2, s1); | |
5eea678f | 3477 | } |
2c73f9f5 ML |
3478 | return s1; |
3479 | } | |
3480 | ||
30394414 JM |
3481 | /* This should return an error not all definitions define functions. |
3482 | It is not an error if we find two functions with exactly the | |
3483 | same signature, only if these are selected in overload resolution. | |
2c73f9f5 | 3484 | old is the current set of bindings, new the freshly-found binding. |
30394414 JM |
3485 | XXX Do we want to give *all* candidates in case of ambiguity? |
3486 | XXX In what way should I treat extern declarations? | |
3487 | XXX I don't want to repeat the entire duplicate_decls here */ | |
2c73f9f5 | 3488 | |
30394414 | 3489 | static tree |
848eed92 | 3490 | ambiguous_decl (tree name, tree old, tree new, int flags) |
30394414 | 3491 | { |
52c11ef6 | 3492 | tree val, type; |
2c73f9f5 | 3493 | my_friendly_assert (old != NULL_TREE, 393); |
a1c65f9f | 3494 | /* Copy the value. */ |
52c11ef6 JM |
3495 | val = BINDING_VALUE (new); |
3496 | if (val) | |
3497 | switch (TREE_CODE (val)) | |
3498 | { | |
3499 | case TEMPLATE_DECL: | |
3500 | /* If we expect types or namespaces, and not templates, | |
a1c65f9f | 3501 | or this is not a template class. */ |
52c11ef6 | 3502 | if (LOOKUP_QUALIFIERS_ONLY (flags) |
c592d5d2 | 3503 | && !DECL_CLASS_TEMPLATE_P (val)) |
52c11ef6 JM |
3504 | val = NULL_TREE; |
3505 | break; | |
3506 | case TYPE_DECL: | |
3507 | if (LOOKUP_NAMESPACES_ONLY (flags)) | |
3508 | val = NULL_TREE; | |
3509 | break; | |
3510 | case NAMESPACE_DECL: | |
3511 | if (LOOKUP_TYPES_ONLY (flags)) | |
3512 | val = NULL_TREE; | |
3513 | break; | |
d52e4867 RS |
3514 | case FUNCTION_DECL: |
3515 | /* Ignore built-in functions that are still anticipated. */ | |
3516 | if (LOOKUP_QUALIFIERS_ONLY (flags) || DECL_ANTICIPATED (val)) | |
3517 | val = NULL_TREE; | |
3518 | break; | |
52c11ef6 JM |
3519 | default: |
3520 | if (LOOKUP_QUALIFIERS_ONLY (flags)) | |
3521 | val = NULL_TREE; | |
3522 | } | |
3523 | ||
2c73f9f5 | 3524 | if (!BINDING_VALUE (old)) |
52c11ef6 JM |
3525 | BINDING_VALUE (old) = val; |
3526 | else if (val && val != BINDING_VALUE (old)) | |
2c73f9f5 ML |
3527 | { |
3528 | if (is_overloaded_fn (BINDING_VALUE (old)) | |
52c11ef6 | 3529 | && is_overloaded_fn (val)) |
2c73f9f5 ML |
3530 | { |
3531 | BINDING_VALUE (old) = merge_functions (BINDING_VALUE (old), | |
52c11ef6 | 3532 | val); |
2c73f9f5 ML |
3533 | } |
3534 | else | |
3535 | { | |
a1c65f9f | 3536 | /* Some declarations are functions, some are not. */ |
52c11ef6 JM |
3537 | if (flags & LOOKUP_COMPLAIN) |
3538 | { | |
2642b9bf JM |
3539 | /* If we've already given this error for this lookup, |
3540 | BINDING_VALUE (old) is error_mark_node, so let's not | |
3541 | repeat ourselves. */ | |
3542 | if (BINDING_VALUE (old) != error_mark_node) | |
3543 | { | |
33bd39a2 | 3544 | error ("use of `%D' is ambiguous", name); |
2642b9bf JM |
3545 | cp_error_at (" first declared as `%#D' here", |
3546 | BINDING_VALUE (old)); | |
3547 | } | |
8251199e | 3548 | cp_error_at (" also declared as `%#D' here", val); |
52c11ef6 | 3549 | } |
d67cdbc3 | 3550 | BINDING_VALUE (old) = error_mark_node; |
2c73f9f5 ML |
3551 | } |
3552 | } | |
a1c65f9f | 3553 | /* ... and copy the type. */ |
52c11ef6 JM |
3554 | type = BINDING_TYPE (new); |
3555 | if (LOOKUP_NAMESPACES_ONLY (flags)) | |
3556 | type = NULL_TREE; | |
2c73f9f5 | 3557 | if (!BINDING_TYPE (old)) |
52c11ef6 | 3558 | BINDING_TYPE (old) = type; |
1c35f5b6 | 3559 | else if (type && BINDING_TYPE (old) != type) |
30394414 | 3560 | { |
52c11ef6 JM |
3561 | if (flags & LOOKUP_COMPLAIN) |
3562 | { | |
33bd39a2 | 3563 | error ("`%D' denotes an ambiguous type",name); |
8251199e JM |
3564 | cp_error_at (" first type here", BINDING_TYPE (old)); |
3565 | cp_error_at (" other type here", type); | |
52c11ef6 | 3566 | } |
30394414 | 3567 | } |
2c73f9f5 | 3568 | return old; |
30394414 JM |
3569 | } |
3570 | ||
2c169bab JM |
3571 | /* Subroutine of unualified_namespace_lookup: |
3572 | Add the bindings of NAME in used namespaces to VAL. | |
3573 | We are currently looking for names in namespace SCOPE, so we | |
3574 | look through USINGS for using-directives of namespaces | |
3575 | which have SCOPE as a common ancestor with the current scope. | |
848eed92 | 3576 | Returns false on errors. */ |
2c73f9f5 | 3577 | |
848eed92 GDR |
3578 | bool |
3579 | lookup_using_namespace (tree name, tree val, tree usings, tree scope, | |
3580 | int flags, tree *spacesp) | |
30394414 JM |
3581 | { |
3582 | tree iter; | |
3583 | tree val1; | |
fd295cb2 | 3584 | timevar_push (TV_NAME_LOOKUP); |
ea9635c7 | 3585 | /* Iterate over all used namespaces in current, searching for using |
a1c65f9f | 3586 | directives of scope. */ |
ea9635c7 ML |
3587 | for (iter = usings; iter; iter = TREE_CHAIN (iter)) |
3588 | if (TREE_VALUE (iter) == scope) | |
3589 | { | |
2c169bab | 3590 | if (spacesp) |
e1b3e07d MM |
3591 | *spacesp = tree_cons (TREE_PURPOSE (iter), NULL_TREE, |
3592 | *spacesp); | |
ea9635c7 | 3593 | val1 = binding_for_name (name, TREE_PURPOSE (iter)); |
a1c65f9f | 3594 | /* Resolve ambiguities. */ |
52c11ef6 | 3595 | val = ambiguous_decl (name, val, val1, flags); |
ea9635c7 | 3596 | } |
fd295cb2 GDR |
3597 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, |
3598 | BINDING_VALUE (val) != error_mark_node); | |
30394414 JM |
3599 | } |
3600 | ||
2c73f9f5 | 3601 | /* [namespace.qual] |
6ad07332 JM |
3602 | Accepts the NAME to lookup and its qualifying SCOPE. |
3603 | Returns the name/type pair found into the CPLUS_BINDING RESULT, | |
848eed92 | 3604 | or false on error. */ |
2c73f9f5 | 3605 | |
848eed92 GDR |
3606 | bool |
3607 | qualified_lookup_using_namespace (tree name, tree scope, tree result, | |
3608 | int flags) | |
30394414 | 3609 | { |
a1c65f9f | 3610 | /* Maintain a list of namespaces visited... */ |
30394414 | 3611 | tree seen = NULL_TREE; |
a1c65f9f | 3612 | /* ... and a list of namespace yet to see. */ |
30394414 JM |
3613 | tree todo = NULL_TREE; |
3614 | tree usings; | |
fd295cb2 | 3615 | timevar_push (TV_NAME_LOOKUP); |
dd4fae80 ML |
3616 | /* Look through namespace aliases. */ |
3617 | scope = ORIGINAL_NAMESPACE (scope); | |
2c73f9f5 | 3618 | while (scope && (result != error_mark_node)) |
30394414 | 3619 | { |
58010b57 | 3620 | seen = tree_cons (scope, NULL_TREE, seen); |
52c11ef6 JM |
3621 | result = ambiguous_decl (name, result, |
3622 | binding_for_name (name, scope), flags); | |
2c73f9f5 | 3623 | if (!BINDING_VALUE (result) && !BINDING_TYPE (result)) |
a1c65f9f | 3624 | /* Consider using directives. */ |
30394414 JM |
3625 | for (usings = DECL_NAMESPACE_USING (scope); usings; |
3626 | usings = TREE_CHAIN (usings)) | |
a1c65f9f | 3627 | /* If this was a real directive, and we have not seen it. */ |
30394414 | 3628 | if (!TREE_INDIRECT_USING (usings) |
2b9dc906 | 3629 | && !purpose_member (TREE_PURPOSE (usings), seen)) |
58010b57 | 3630 | todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo); |
30394414 JM |
3631 | if (todo) |
3632 | { | |
3633 | scope = TREE_PURPOSE (todo); | |
3634 | todo = TREE_CHAIN (todo); | |
3635 | } | |
3636 | else | |
a1c65f9f | 3637 | scope = NULL_TREE; /* If there never was a todo list. */ |
30394414 | 3638 | } |
fd295cb2 | 3639 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result != error_mark_node); |
30394414 | 3640 | } |
6060a796 | 3641 | |
2c73f9f5 ML |
3642 | /* [namespace.memdef]/2 */ |
3643 | ||
3644 | /* Set the context of a declaration to scope. Complain if we are not | |
a1c65f9f | 3645 | outside scope. */ |
2c73f9f5 ML |
3646 | |
3647 | void | |
848eed92 | 3648 | set_decl_namespace (tree decl, tree scope, bool friendp) |
2c73f9f5 ML |
3649 | { |
3650 | tree old; | |
1dbb6023 | 3651 | |
a1c65f9f | 3652 | /* Get rid of namespace aliases. */ |
3e3f722c ML |
3653 | scope = ORIGINAL_NAMESPACE (scope); |
3654 | ||
b262d64c | 3655 | /* It is ok for friends to be qualified in parallel space. */ |
089d6ea7 | 3656 | if (!friendp && !is_ancestor (current_namespace, scope)) |
33bd39a2 | 3657 | error ("declaration of `%D' not in a namespace surrounding `%D'", |
2c73f9f5 | 3658 | decl, scope); |
cb0dbb9a | 3659 | DECL_CONTEXT (decl) = FROB_CONTEXT (scope); |
2c73f9f5 ML |
3660 | if (scope != current_namespace) |
3661 | { | |
a1c65f9f | 3662 | /* See whether this has been declared in the namespace. */ |
2c73f9f5 ML |
3663 | old = namespace_binding (DECL_NAME (decl), scope); |
3664 | if (!old) | |
a1c65f9f | 3665 | /* No old declaration at all. */ |
2c73f9f5 | 3666 | goto complain; |
2c0fc02d MM |
3667 | /* A template can be explicitly specialized in any namespace. */ |
3668 | if (processing_explicit_instantiation) | |
3669 | return; | |
2c73f9f5 ML |
3670 | if (!is_overloaded_fn (decl)) |
3671 | /* Don't compare non-function decls with decls_match here, | |
3672 | since it can't check for the correct constness at this | |
3673 | point. pushdecl will find those errors later. */ | |
3674 | return; | |
a1c65f9f | 3675 | /* Since decl is a function, old should contain a function decl. */ |
2c73f9f5 ML |
3676 | if (!is_overloaded_fn (old)) |
3677 | goto complain; | |
d82d65d8 | 3678 | if (processing_template_decl || processing_specialization) |
a723baf1 | 3679 | /* We have not yet called push_template_decl to turn a |
d82d65d8 ML |
3680 | FUNCTION_DECL into a TEMPLATE_DECL, so the declarations |
3681 | won't match. But, we'll check later, when we construct the | |
3682 | template. */ | |
3683 | return; | |
a723baf1 MM |
3684 | if (is_overloaded_fn (old)) |
3685 | { | |
3686 | for (; old; old = OVL_NEXT (old)) | |
3687 | if (decls_match (decl, OVL_CURRENT (old))) | |
3688 | return; | |
3689 | } | |
3690 | else | |
3691 | if (decls_match (decl, old)) | |
2c73f9f5 ML |
3692 | return; |
3693 | } | |
3694 | else | |
3695 | return; | |
3696 | complain: | |
33bd39a2 | 3697 | error ("`%D' should have been declared inside `%D'", |
2c73f9f5 ML |
3698 | decl, scope); |
3699 | } | |
3700 | ||
a1c65f9f | 3701 | /* Compute the namespace where a declaration is defined. */ |
e92cc029 | 3702 | |
d8e178a0 | 3703 | static tree |
848eed92 | 3704 | decl_namespace (tree decl) |
6060a796 | 3705 | { |
fd295cb2 | 3706 | timevar_push (TV_NAME_LOOKUP); |
2bdb0643 JM |
3707 | if (TYPE_P (decl)) |
3708 | decl = TYPE_STUB_DECL (decl); | |
2c73f9f5 ML |
3709 | while (DECL_CONTEXT (decl)) |
3710 | { | |
3711 | decl = DECL_CONTEXT (decl); | |
3712 | if (TREE_CODE (decl) == NAMESPACE_DECL) | |
fd295cb2 | 3713 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl); |
2f939d94 | 3714 | if (TYPE_P (decl)) |
2c73f9f5 | 3715 | decl = TYPE_STUB_DECL (decl); |
2f939d94 | 3716 | my_friendly_assert (DECL_P (decl), 390); |
2c73f9f5 ML |
3717 | } |
3718 | ||
fd295cb2 | 3719 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, global_namespace); |
6060a796 MS |
3720 | } |
3721 | ||
a1c65f9f | 3722 | /* Return the namespace where the current declaration is declared. */ |
e92cc029 | 3723 | |
6060a796 | 3724 | tree |
848eed92 | 3725 | current_decl_namespace (void) |
6060a796 | 3726 | { |
2c73f9f5 | 3727 | tree result; |
a1c65f9f | 3728 | /* If we have been pushed into a different namespace, use it. */ |
2c73f9f5 ML |
3729 | if (decl_namespace_list) |
3730 | return TREE_PURPOSE (decl_namespace_list); | |
3731 | ||
3732 | if (current_class_type) | |
c3baf4b5 | 3733 | result = decl_namespace (TYPE_STUB_DECL (current_class_type)); |
2c73f9f5 | 3734 | else if (current_function_decl) |
c3baf4b5 | 3735 | result = decl_namespace (current_function_decl); |
2c73f9f5 ML |
3736 | else |
3737 | result = current_namespace; | |
3738 | return result; | |
3739 | } | |
6060a796 | 3740 | |
a1c65f9f | 3741 | /* Temporarily set the namespace for the current declaration. */ |
6060a796 | 3742 | |
2c73f9f5 | 3743 | void |
848eed92 | 3744 | push_decl_namespace (tree decl) |
2c73f9f5 ML |
3745 | { |
3746 | if (TREE_CODE (decl) != NAMESPACE_DECL) | |
3747 | decl = decl_namespace (decl); | |
a1089a8e NS |
3748 | decl_namespace_list = tree_cons (ORIGINAL_NAMESPACE (decl), |
3749 | NULL_TREE, decl_namespace_list); | |
2c73f9f5 ML |
3750 | } |
3751 | ||
3752 | void | |
848eed92 | 3753 | pop_decl_namespace (void) |
2c73f9f5 ML |
3754 | { |
3755 | decl_namespace_list = TREE_CHAIN (decl_namespace_list); | |
3756 | } | |
3757 | ||
a1c65f9f | 3758 | /* Enter a class or namespace scope. */ |
f09bbbed ML |
3759 | |
3760 | void | |
848eed92 | 3761 | push_scope (tree t) |
f09bbbed ML |
3762 | { |
3763 | if (TREE_CODE (t) == NAMESPACE_DECL) | |
3764 | push_decl_namespace (t); | |
a723baf1 | 3765 | else if CLASS_TYPE_P (t) |
14d22dd6 | 3766 | push_nested_class (t); |
f09bbbed ML |
3767 | } |
3768 | ||
a1c65f9f | 3769 | /* Leave scope pushed by push_scope. */ |
f09bbbed ML |
3770 | |
3771 | void | |
848eed92 | 3772 | pop_scope (tree t) |
f09bbbed ML |
3773 | { |
3774 | if (TREE_CODE (t) == NAMESPACE_DECL) | |
3775 | pop_decl_namespace (); | |
a723baf1 MM |
3776 | else if CLASS_TYPE_P (t) |
3777 | pop_nested_class (); | |
f09bbbed ML |
3778 | } |
3779 | ||
2c73f9f5 | 3780 | /* [basic.lookup.koenig] */ |
838dfd8a | 3781 | /* A nonzero return value in the functions below indicates an error. */ |
2c73f9f5 ML |
3782 | |
3783 | struct arg_lookup | |
3784 | { | |
3785 | tree name; | |
3786 | tree namespaces; | |
3787 | tree classes; | |
3788 | tree functions; | |
3789 | }; | |
3790 | ||
848eed92 GDR |
3791 | static bool arg_assoc (struct arg_lookup*, tree); |
3792 | static bool arg_assoc_args (struct arg_lookup*, tree); | |
3793 | static bool arg_assoc_type (struct arg_lookup*, tree); | |
3794 | static bool add_function (struct arg_lookup *, tree); | |
3795 | static bool arg_assoc_namespace (struct arg_lookup *, tree); | |
3796 | static bool arg_assoc_class (struct arg_lookup *, tree); | |
3797 | static bool arg_assoc_template_arg (struct arg_lookup*, tree); | |
2c73f9f5 | 3798 | |
2d390867 | 3799 | /* Add a function to the lookup structure. |
848eed92 | 3800 | Returns true on error. */ |
2c73f9f5 | 3801 | |
848eed92 GDR |
3802 | static bool |
3803 | add_function (struct arg_lookup *k, tree fn) | |
2c73f9f5 | 3804 | { |
2c169bab JM |
3805 | /* We used to check here to see if the function was already in the list, |
3806 | but that's O(n^2), which is just too expensive for function lookup. | |
3807 | Now we deal with the occasional duplicate in joust. In doing this, we | |
3808 | assume that the number of duplicates will be small compared to the | |
3809 | total number of functions being compared, which should usually be the | |
3810 | case. */ | |
3811 | ||
a1c65f9f | 3812 | /* We must find only functions, or exactly one non-function. */ |
9845b52b TP |
3813 | if (!k->functions) |
3814 | k->functions = fn; | |
3815 | else if (is_overloaded_fn (k->functions) && is_overloaded_fn (fn)) | |
5f8ac7d1 | 3816 | k->functions = build_overload (fn, k->functions); |
9845b52b | 3817 | else |
2c169bab JM |
3818 | { |
3819 | tree f1 = OVL_CURRENT (k->functions); | |
3820 | tree f2 = fn; | |
3821 | if (is_overloaded_fn (f1)) | |
3822 | { | |
3823 | fn = f1; f1 = f2; f2 = fn; | |
3824 | } | |
3825 | cp_error_at ("`%D' is not a function,", f1); | |
3826 | cp_error_at (" conflict with `%D'", f2); | |
33bd39a2 | 3827 | error (" in call to `%D'", k->name); |
848eed92 | 3828 | return true; |
2c169bab | 3829 | } |
9845b52b | 3830 | |
848eed92 | 3831 | return false; |
2c73f9f5 ML |
3832 | } |
3833 | ||
2d390867 | 3834 | /* Add functions of a namespace to the lookup structure. |
848eed92 | 3835 | Returns true on error. */ |
2c73f9f5 | 3836 | |
848eed92 GDR |
3837 | static bool |
3838 | arg_assoc_namespace (struct arg_lookup *k, tree scope) | |
2c73f9f5 ML |
3839 | { |
3840 | tree value; | |
3841 | ||
3842 | if (purpose_member (scope, k->namespaces)) | |
3843 | return 0; | |
3844 | k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces); | |
3845 | ||
3846 | value = namespace_binding (k->name, scope); | |
3847 | if (!value) | |
848eed92 | 3848 | return false; |
2c169bab | 3849 | |
2c73f9f5 ML |
3850 | for (; value; value = OVL_NEXT (value)) |
3851 | if (add_function (k, OVL_CURRENT (value))) | |
848eed92 | 3852 | return true; |
2c73f9f5 | 3853 | |
848eed92 | 3854 | return false; |
6060a796 | 3855 | } |
2c73f9f5 | 3856 | |
ec4f972f | 3857 | /* Adds everything associated with a template argument to the lookup |
848eed92 | 3858 | structure. Returns true on error. */ |
ec4f972f | 3859 | |
848eed92 GDR |
3860 | static bool |
3861 | arg_assoc_template_arg (struct arg_lookup *k, tree arg) | |
ec4f972f AS |
3862 | { |
3863 | /* [basic.lookup.koenig] | |
3864 | ||
3865 | If T is a template-id, its associated namespaces and classes are | |
3866 | ... the namespaces and classes associated with the types of the | |
3867 | template arguments provided for template type parameters | |
3868 | (excluding template template parameters); the namespaces in which | |
3869 | any template template arguments are defined; and the classes in | |
3870 | which any member templates used as template template arguments | |
3871 | are defined. [Note: non-type template arguments do not | |
3872 | contribute to the set of associated namespaces. ] */ | |
3873 | ||
3874 | /* Consider first template template arguments. */ | |
b8c6534b KL |
3875 | if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM |
3876 | || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE) | |
848eed92 | 3877 | return false; |
a1281f45 | 3878 | else if (TREE_CODE (arg) == TEMPLATE_DECL) |
ec4f972f AS |
3879 | { |
3880 | tree ctx = CP_DECL_CONTEXT (arg); | |
3881 | ||
3882 | /* It's not a member template. */ | |
3883 | if (TREE_CODE (ctx) == NAMESPACE_DECL) | |
3884 | return arg_assoc_namespace (k, ctx); | |
3885 | /* Otherwise, it must be member template. */ | |
3886 | else | |
3887 | return arg_assoc_class (k, ctx); | |
3888 | } | |
3889 | /* It's not a template template argument, but it is a type template | |
3890 | argument. */ | |
2f939d94 | 3891 | else if (TYPE_P (arg)) |
ec4f972f AS |
3892 | return arg_assoc_type (k, arg); |
3893 | /* It's a non-type template argument. */ | |
3894 | else | |
848eed92 | 3895 | return false; |
ec4f972f AS |
3896 | } |
3897 | ||
2d390867 | 3898 | /* Adds everything associated with class to the lookup structure. |
848eed92 | 3899 | Returns true on error. */ |
2c73f9f5 | 3900 | |
848eed92 GDR |
3901 | static bool |
3902 | arg_assoc_class (struct arg_lookup *k, tree type) | |
2c73f9f5 ML |
3903 | { |
3904 | tree list, friends, context; | |
3905 | int i; | |
3906 | ||
31aa49b7 RH |
3907 | /* Backend build structures, such as __builtin_va_list, aren't |
3908 | affected by all this. */ | |
3909 | if (!CLASS_TYPE_P (type)) | |
848eed92 | 3910 | return false; |
31aa49b7 | 3911 | |
2c73f9f5 | 3912 | if (purpose_member (type, k->classes)) |
848eed92 | 3913 | return false; |
2c73f9f5 ML |
3914 | k->classes = tree_cons (type, NULL_TREE, k->classes); |
3915 | ||
3916 | context = decl_namespace (TYPE_MAIN_DECL (type)); | |
3917 | if (arg_assoc_namespace (k, context)) | |
848eed92 | 3918 | return true; |
2c73f9f5 | 3919 | |
a1c65f9f | 3920 | /* Process baseclasses. */ |
2c73f9f5 ML |
3921 | for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); i++) |
3922 | if (arg_assoc_class (k, TYPE_BINFO_BASETYPE (type, i))) | |
848eed92 | 3923 | return true; |
2c73f9f5 | 3924 | |
a1c65f9f | 3925 | /* Process friends. */ |
2c73f9f5 ML |
3926 | for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list; |
3927 | list = TREE_CHAIN (list)) | |
3928 | if (k->name == TREE_PURPOSE (list)) | |
3929 | for (friends = TREE_VALUE (list); friends; | |
3930 | friends = TREE_CHAIN (friends)) | |
3931 | /* Only interested in global functions with potentially hidden | |
a1c65f9f | 3932 | (i.e. unqualified) declarations. */ |
60696c53 JM |
3933 | if (TREE_PURPOSE (friends) == error_mark_node && TREE_VALUE (friends) |
3934 | && decl_namespace (TREE_VALUE (friends)) == context) | |
3935 | if (add_function (k, TREE_VALUE (friends))) | |
848eed92 | 3936 | return true; |
00dc6358 JM |
3937 | |
3938 | /* Process template arguments. */ | |
3939 | if (CLASSTYPE_TEMPLATE_INFO (type)) | |
3940 | { | |
f9a7ae04 | 3941 | list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)); |
ec4f972f AS |
3942 | for (i = 0; i < TREE_VEC_LENGTH (list); ++i) |
3943 | arg_assoc_template_arg (k, TREE_VEC_ELT (list, i)); | |
00dc6358 JM |
3944 | } |
3945 | ||
848eed92 | 3946 | return false; |
2c73f9f5 ML |
3947 | } |
3948 | ||
2d390867 JM |
3949 | /* Adds everything associated with a given type. |
3950 | Returns 1 on error. */ | |
2c73f9f5 | 3951 | |
848eed92 GDR |
3952 | static bool |
3953 | arg_assoc_type (struct arg_lookup *k, tree type) | |
2c73f9f5 ML |
3954 | { |
3955 | switch (TREE_CODE (type)) | |
3956 | { | |
3957 | case VOID_TYPE: | |
3958 | case INTEGER_TYPE: | |
3959 | case REAL_TYPE: | |
3960 | case COMPLEX_TYPE: | |
c00996a3 | 3961 | case VECTOR_TYPE: |
2c73f9f5 ML |
3962 | case CHAR_TYPE: |
3963 | case BOOLEAN_TYPE: | |
848eed92 | 3964 | return false; |
2c73f9f5 ML |
3965 | case RECORD_TYPE: |
3966 | if (TYPE_PTRMEMFUNC_P (type)) | |
3967 | return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type)); | |
3968 | return arg_assoc_class (k, type); | |
3969 | case POINTER_TYPE: | |
3970 | case REFERENCE_TYPE: | |
3971 | case ARRAY_TYPE: | |
3972 | return arg_assoc_type (k, TREE_TYPE (type)); | |
3973 | case UNION_TYPE: | |
3974 | case ENUMERAL_TYPE: | |
3975 | return arg_assoc_namespace (k, decl_namespace (TYPE_MAIN_DECL (type))); | |
1813dd7b | 3976 | case OFFSET_TYPE: |
a1c65f9f | 3977 | /* Pointer to member: associate class type and value type. */ |
1813dd7b | 3978 | if (arg_assoc_type (k, TYPE_OFFSET_BASETYPE (type))) |
848eed92 | 3979 | return true; |
1813dd7b | 3980 | return arg_assoc_type (k, TREE_TYPE (type)); |
2c73f9f5 | 3981 | case METHOD_TYPE: |
2d390867 JM |
3982 | /* The basetype is referenced in the first arg type, so just |
3983 | fall through. */ | |
2c73f9f5 | 3984 | case FUNCTION_TYPE: |
a1c65f9f | 3985 | /* Associate the parameter types. */ |
2c73f9f5 | 3986 | if (arg_assoc_args (k, TYPE_ARG_TYPES (type))) |
848eed92 | 3987 | return true; |
a1c65f9f | 3988 | /* Associate the return type. */ |
2c73f9f5 | 3989 | return arg_assoc_type (k, TREE_TYPE (type)); |
2d390867 | 3990 | case TEMPLATE_TYPE_PARM: |
a1281f45 | 3991 | case BOUND_TEMPLATE_TEMPLATE_PARM: |
848eed92 | 3992 | return false; |
66081283 | 3993 | case TYPENAME_TYPE: |
848eed92 | 3994 | return false; |
1813dd7b ML |
3995 | case LANG_TYPE: |
3996 | if (type == unknown_type_node) | |
848eed92 | 3997 | return false; |
1813dd7b | 3998 | /* else fall through */ |
2c73f9f5 | 3999 | default: |
a98facb0 | 4000 | abort (); |
2c73f9f5 | 4001 | } |
848eed92 | 4002 | return false; |
2c73f9f5 ML |
4003 | } |
4004 | ||
848eed92 | 4005 | /* Adds everything associated with arguments. Returns true on error. */ |
2c73f9f5 | 4006 | |
848eed92 GDR |
4007 | static bool |
4008 | arg_assoc_args (struct arg_lookup *k, tree args) | |
2c73f9f5 ML |
4009 | { |
4010 | for (; args; args = TREE_CHAIN (args)) | |
4011 | if (arg_assoc (k, TREE_VALUE (args))) | |
848eed92 GDR |
4012 | return true; |
4013 | return false; | |
2c73f9f5 ML |
4014 | } |
4015 | ||
2d390867 | 4016 | /* Adds everything associated with a given tree_node. Returns 1 on error. */ |
2c73f9f5 | 4017 | |
848eed92 GDR |
4018 | static bool |
4019 | arg_assoc (struct arg_lookup *k, tree n) | |
2c73f9f5 | 4020 | { |
00dc6358 | 4021 | if (n == error_mark_node) |
848eed92 | 4022 | return false; |
00dc6358 | 4023 | |
2f939d94 | 4024 | if (TYPE_P (n)) |
2d390867 JM |
4025 | return arg_assoc_type (k, n); |
4026 | ||
4027 | if (! type_unknown_p (n)) | |
4028 | return arg_assoc_type (k, TREE_TYPE (n)); | |
4029 | ||
4030 | if (TREE_CODE (n) == ADDR_EXPR) | |
4031 | n = TREE_OPERAND (n, 0); | |
51924768 JM |
4032 | if (TREE_CODE (n) == COMPONENT_REF) |
4033 | n = TREE_OPERAND (n, 1); | |
05e0b2f4 JM |
4034 | if (TREE_CODE (n) == OFFSET_REF) |
4035 | n = TREE_OPERAND (n, 1); | |
00dc6358 | 4036 | while (TREE_CODE (n) == TREE_LIST) |
2d390867 | 4037 | n = TREE_VALUE (n); |
50ad9642 MM |
4038 | if (TREE_CODE (n) == BASELINK) |
4039 | n = BASELINK_FUNCTIONS (n); | |
2d390867 | 4040 | |
51924768 JM |
4041 | if (TREE_CODE (n) == FUNCTION_DECL) |
4042 | return arg_assoc_type (k, TREE_TYPE (n)); | |
0c8bfdbc MM |
4043 | if (TREE_CODE (n) == TEMPLATE_ID_EXPR) |
4044 | { | |
4045 | /* [basic.lookup.koenig] | |
4046 | ||
4047 | If T is a template-id, its associated namespaces and classes | |
4048 | are the namespace in which the template is defined; for | |
ec4f972f | 4049 | member templates, the member template's class... */ |
0c8bfdbc MM |
4050 | tree template = TREE_OPERAND (n, 0); |
4051 | tree args = TREE_OPERAND (n, 1); | |
4052 | tree ctx; | |
4053 | tree arg; | |
4054 | ||
3338663b NS |
4055 | if (TREE_CODE (template) == COMPONENT_REF) |
4056 | template = TREE_OPERAND (template, 1); | |
4057 | ||
0c8bfdbc MM |
4058 | /* First, the template. There may actually be more than one if |
4059 | this is an overloaded function template. But, in that case, | |
4060 | we only need the first; all the functions will be in the same | |
4061 | namespace. */ | |
4062 | template = OVL_CURRENT (template); | |
4063 | ||
4064 | ctx = CP_DECL_CONTEXT (template); | |
4065 | ||
4066 | if (TREE_CODE (ctx) == NAMESPACE_DECL) | |
4067 | { | |
4068 | if (arg_assoc_namespace (k, ctx) == 1) | |
848eed92 | 4069 | return true; |
0c8bfdbc MM |
4070 | } |
4071 | /* It must be a member template. */ | |
4072 | else if (arg_assoc_class (k, ctx) == 1) | |
848eed92 | 4073 | return true; |
2d390867 | 4074 | |
0c8bfdbc MM |
4075 | /* Now the arguments. */ |
4076 | for (arg = args; arg != NULL_TREE; arg = TREE_CHAIN (arg)) | |
ec4f972f | 4077 | if (arg_assoc_template_arg (k, TREE_VALUE (arg)) == 1) |
848eed92 | 4078 | return true; |
0c8bfdbc MM |
4079 | } |
4080 | else | |
4081 | { | |
4082 | my_friendly_assert (TREE_CODE (n) == OVERLOAD, 980715); | |
4083 | ||
3add6264 TP |
4084 | for (; n; n = OVL_CHAIN (n)) |
4085 | if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n)))) | |
848eed92 | 4086 | return true; |
0c8bfdbc | 4087 | } |
2c73f9f5 | 4088 | |
848eed92 | 4089 | return false; |
2c73f9f5 ML |
4090 | } |
4091 | ||
4092 | /* Performs Koenig lookup depending on arguments, where fns | |
a1c65f9f | 4093 | are the functions found in normal lookup. */ |
2c73f9f5 ML |
4094 | |
4095 | tree | |
848eed92 | 4096 | lookup_arg_dependent (tree name, tree fns, tree args) |
2c73f9f5 ML |
4097 | { |
4098 | struct arg_lookup k; | |
f0b9bc6c | 4099 | tree fn = NULL_TREE; |
2c169bab | 4100 | |
fd295cb2 | 4101 | timevar_push (TV_NAME_LOOKUP); |
2c73f9f5 ML |
4102 | k.name = name; |
4103 | k.functions = fns; | |
2c73f9f5 | 4104 | k.classes = NULL_TREE; |
2c169bab JM |
4105 | |
4106 | /* Note that we've already looked at some namespaces during normal | |
4107 | unqualified lookup, unless we found a decl in function scope. */ | |
f0b9bc6c NS |
4108 | if (fns) |
4109 | fn = OVL_CURRENT (fns); | |
4110 | if (fn && TREE_CODE (fn) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (fn)) | |
2c169bab JM |
4111 | k.namespaces = NULL_TREE; |
4112 | else | |
4113 | unqualified_namespace_lookup (name, 0, &k.namespaces); | |
4114 | ||
2c73f9f5 | 4115 | arg_assoc_args (&k, args); |
fd295cb2 | 4116 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, k.functions); |
2c73f9f5 ML |
4117 | } |
4118 | ||
a1c65f9f | 4119 | /* Process a namespace-alias declaration. */ |
6060a796 | 4120 | |
6060a796 | 4121 | void |
848eed92 | 4122 | do_namespace_alias (tree alias, tree namespace) |
6060a796 | 4123 | { |
3e3f722c | 4124 | if (TREE_CODE (namespace) != NAMESPACE_DECL) |
30394414 | 4125 | { |
a1c65f9f | 4126 | /* The parser did not find it, so it's not there. */ |
33bd39a2 | 4127 | error ("unknown namespace `%D'", namespace); |
30394414 JM |
4128 | return; |
4129 | } | |
3e3f722c ML |
4130 | |
4131 | namespace = ORIGINAL_NAMESPACE (namespace); | |
4132 | ||
a1c65f9f | 4133 | /* Build the alias. */ |
85c6cbaf ML |
4134 | alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node); |
4135 | DECL_NAMESPACE_ALIAS (alias) = namespace; | |
4e8dca1c | 4136 | DECL_EXTERNAL (alias) = 1; |
85c6cbaf | 4137 | pushdecl (alias); |
6060a796 MS |
4138 | } |
4139 | ||
ea9635c7 | 4140 | /* Check a non-member using-declaration. Return the name and scope |
a1c65f9f | 4141 | being used, and the USING_DECL, or NULL_TREE on failure. */ |
2c73f9f5 | 4142 | |
ea9635c7 | 4143 | static tree |
848eed92 | 4144 | validate_nonmember_using_decl (tree decl, tree *scope, tree *name) |
6060a796 | 4145 | { |
a723baf1 MM |
4146 | *scope = global_namespace; |
4147 | *name = NULL_TREE; | |
066b4a1c | 4148 | |
a723baf1 | 4149 | if (TREE_CODE (decl) == TEMPLATE_ID_EXPR) |
2c73f9f5 | 4150 | { |
a723baf1 MM |
4151 | *name = TREE_OPERAND (decl, 0); |
4152 | /* 7.3.3/5 | |
4153 | A using-declaration shall not name a template-id. */ | |
4154 | error ("a using-declaration cannot specify a template-id. Try `using %D'", *name); | |
4155 | return NULL_TREE; | |
2c73f9f5 | 4156 | } |
a723baf1 MM |
4157 | |
4158 | if (TREE_CODE (decl) == NAMESPACE_DECL) | |
76543000 | 4159 | { |
33bd39a2 | 4160 | error ("namespace `%D' not allowed in using-declaration", decl); |
76543000 KL |
4161 | return NULL_TREE; |
4162 | } | |
a723baf1 MM |
4163 | |
4164 | if (is_overloaded_fn (decl)) | |
4165 | decl = get_first_fn (decl); | |
4166 | ||
4167 | my_friendly_assert (DECL_P (decl), 20020908); | |
4168 | ||
4169 | if (TREE_CODE (decl) == CONST_DECL) | |
4170 | /* Enumeration constants to not have DECL_CONTEXT set. */ | |
4171 | *scope = TYPE_CONTEXT (TREE_TYPE (decl)); | |
2c73f9f5 | 4172 | else |
a723baf1 MM |
4173 | *scope = DECL_CONTEXT (decl); |
4174 | if (!*scope) | |
4175 | *scope = global_namespace; | |
4176 | ||
4177 | /* [namespace.udecl] | |
4178 | A using-declaration for a class member shall be a | |
4179 | member-declaration. */ | |
4180 | if (TYPE_P (*scope)) | |
4181 | { | |
4182 | error ("`%T' is not a namespace", *scope); | |
4183 | return NULL_TREE; | |
4184 | } | |
4185 | *name = DECL_NAME (decl); | |
4186 | /* Make a USING_DECL. */ | |
ea9635c7 ML |
4187 | return push_using_decl (*scope, *name); |
4188 | } | |
4189 | ||
a1c65f9f | 4190 | /* Process local and global using-declarations. */ |
ea9635c7 ML |
4191 | |
4192 | static void | |
848eed92 GDR |
4193 | do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype, |
4194 | tree *newval, tree *newtype) | |
ea9635c7 ML |
4195 | { |
4196 | tree decls; | |
ea9635c7 ML |
4197 | |
4198 | *newval = *newtype = NULL_TREE; | |
87e3dbc9 | 4199 | decls = make_node (CPLUS_BINDING); |
52c11ef6 | 4200 | if (!qualified_lookup_using_namespace (name, scope, decls, 0)) |
2c73f9f5 | 4201 | /* Lookup error */ |
a9aedbc2 | 4202 | return; |
6060a796 | 4203 | |
2c73f9f5 ML |
4204 | if (!BINDING_VALUE (decls) && !BINDING_TYPE (decls)) |
4205 | { | |
33bd39a2 | 4206 | error ("`%D' not declared", name); |
2c73f9f5 ML |
4207 | return; |
4208 | } | |
2c73f9f5 | 4209 | |
a1c65f9f | 4210 | /* Check for using functions. */ |
2c73f9f5 ML |
4211 | if (BINDING_VALUE (decls) && is_overloaded_fn (BINDING_VALUE (decls))) |
4212 | { | |
2c73f9f5 | 4213 | tree tmp, tmp1; |
9ed182dc JM |
4214 | |
4215 | if (oldval && !is_overloaded_fn (oldval)) | |
4216 | { | |
4217 | duplicate_decls (OVL_CURRENT (BINDING_VALUE (decls)), oldval); | |
4218 | oldval = NULL_TREE; | |
4219 | } | |
4220 | ||
ea9635c7 | 4221 | *newval = oldval; |
2c73f9f5 ML |
4222 | for (tmp = BINDING_VALUE (decls); tmp; tmp = OVL_NEXT (tmp)) |
4223 | { | |
7bdbfa05 MM |
4224 | tree new_fn = OVL_CURRENT (tmp); |
4225 | ||
4226 | /* [namespace.udecl] | |
4227 | ||
4228 | If a function declaration in namespace scope or block | |
4229 | scope has the same name and the same parameter types as a | |
4230 | function introduced by a using declaration the program is | |
4231 | ill-formed. */ | |
2c73f9f5 | 4232 | for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1)) |
7bdbfa05 MM |
4233 | { |
4234 | tree old_fn = OVL_CURRENT (tmp1); | |
a9aedbc2 | 4235 | |
4d24a889 NS |
4236 | if (new_fn == old_fn) |
4237 | /* The function already exists in the current namespace. */ | |
4238 | break; | |
4239 | else if (OVL_USED (tmp1)) | |
4240 | continue; /* this is a using decl */ | |
4241 | else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)), | |
4242 | TYPE_ARG_TYPES (TREE_TYPE (old_fn)))) | |
7bdbfa05 | 4243 | { |
d52e4867 RS |
4244 | /* If this using declaration introduces a function |
4245 | recognized as a built-in, no longer mark it as | |
4246 | anticipated in this scope. */ | |
4247 | if (DECL_ANTICIPATED (old_fn)) | |
4248 | { | |
4249 | DECL_ANTICIPATED (old_fn) = 0; | |
4250 | break; | |
4251 | } | |
4252 | ||
4d24a889 NS |
4253 | /* There was already a non-using declaration in |
4254 | this scope with the same parameter types. If both | |
4255 | are the same extern "C" functions, that's ok. */ | |
4256 | if (!decls_match (new_fn, old_fn)) | |
33bd39a2 | 4257 | error ("`%D' is already declared in this scope", name); |
7bdbfa05 MM |
4258 | break; |
4259 | } | |
7bdbfa05 MM |
4260 | } |
4261 | ||
4262 | /* If we broke out of the loop, there's no reason to add | |
4263 | this function to the using declarations for this | |
4264 | scope. */ | |
2c73f9f5 ML |
4265 | if (tmp1) |
4266 | continue; | |
4267 | ||
ea9635c7 ML |
4268 | *newval = build_overload (OVL_CURRENT (tmp), *newval); |
4269 | if (TREE_CODE (*newval) != OVERLOAD) | |
4270 | *newval = ovl_cons (*newval, NULL_TREE); | |
4271 | OVL_USED (*newval) = 1; | |
2c73f9f5 ML |
4272 | } |
4273 | } | |
4274 | else | |
a9aedbc2 | 4275 | { |
ea9635c7 | 4276 | *newval = BINDING_VALUE (decls); |
9ed182dc JM |
4277 | if (oldval) |
4278 | duplicate_decls (*newval, oldval); | |
2c73f9f5 ML |
4279 | } |
4280 | ||
ea9635c7 ML |
4281 | *newtype = BINDING_TYPE (decls); |
4282 | if (oldtype && *newtype && oldtype != *newtype) | |
2c73f9f5 | 4283 | { |
33bd39a2 | 4284 | error ("using declaration `%D' introduced ambiguous type `%T'", |
ea9635c7 | 4285 | name, oldtype); |
2c73f9f5 | 4286 | return; |
a9aedbc2 | 4287 | } |
ea9635c7 ML |
4288 | } |
4289 | ||
a1c65f9f | 4290 | /* Process a using-declaration not appearing in class or local scope. */ |
ea9635c7 ML |
4291 | |
4292 | void | |
848eed92 | 4293 | do_toplevel_using_decl (tree decl) |
ea9635c7 ML |
4294 | { |
4295 | tree scope, name, binding; | |
4296 | tree oldval, oldtype, newval, newtype; | |
4297 | ||
4298 | decl = validate_nonmember_using_decl (decl, &scope, &name); | |
4299 | if (decl == NULL_TREE) | |
4300 | return; | |
4301 | ||
4302 | binding = binding_for_name (name, current_namespace); | |
4303 | ||
4304 | oldval = BINDING_VALUE (binding); | |
4305 | oldtype = BINDING_TYPE (binding); | |
4306 | ||
4307 | do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype); | |
4308 | ||
a1c65f9f | 4309 | /* Copy declarations found. */ |
2c73f9f5 ML |
4310 | if (newval) |
4311 | BINDING_VALUE (binding) = newval; | |
4312 | if (newtype) | |
4313 | BINDING_TYPE (binding) = newtype; | |
4314 | return; | |
6060a796 MS |
4315 | } |
4316 | ||
9ed182dc JM |
4317 | /* Process a using-declaration at function scope. */ |
4318 | ||
ea9635c7 | 4319 | void |
848eed92 | 4320 | do_local_using_decl (tree decl) |
ea9635c7 ML |
4321 | { |
4322 | tree scope, name; | |
4323 | tree oldval, oldtype, newval, newtype; | |
9ed182dc | 4324 | |
ea9635c7 ML |
4325 | decl = validate_nonmember_using_decl (decl, &scope, &name); |
4326 | if (decl == NULL_TREE) | |
4327 | return; | |
4328 | ||
fbfe8c9e NS |
4329 | if (building_stmt_tree () |
4330 | && at_function_scope_p ()) | |
4331 | add_decl_stmt (decl); | |
fbfe8c9e | 4332 | |
9ed182dc JM |
4333 | oldval = lookup_name_current_level (name); |
4334 | oldtype = lookup_type_current_level (name); | |
ea9635c7 ML |
4335 | |
4336 | do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype); | |
4337 | ||
4338 | if (newval) | |
7bdbfa05 MM |
4339 | { |
4340 | if (is_overloaded_fn (newval)) | |
4341 | { | |
0580c9aa | 4342 | tree fn, term; |
7bdbfa05 MM |
4343 | |
4344 | /* We only need to push declarations for those functions | |
0580c9aa ML |
4345 | that were not already bound in the current level. |
4346 | The old value might be NULL_TREE, it might be a single | |
4347 | function, or an OVERLOAD. */ | |
4348 | if (oldval && TREE_CODE (oldval) == OVERLOAD) | |
4349 | term = OVL_FUNCTION (oldval); | |
4350 | else | |
4351 | term = oldval; | |
4352 | for (fn = newval; fn && OVL_CURRENT (fn) != term; | |
4353 | fn = OVL_NEXT (fn)) | |
7bdbfa05 MM |
4354 | push_overloaded_decl (OVL_CURRENT (fn), |
4355 | PUSH_LOCAL | PUSH_USING); | |
4356 | } | |
4357 | else | |
0034cf72 | 4358 | push_local_binding (name, newval, PUSH_USING); |
7bdbfa05 | 4359 | } |
9ed182dc JM |
4360 | if (newtype) |
4361 | set_identifier_type_value (name, newtype); | |
ea9635c7 ML |
4362 | } |
4363 | ||
6060a796 | 4364 | tree |
848eed92 | 4365 | do_class_using_decl (tree decl) |
6060a796 | 4366 | { |
cffa8729 | 4367 | tree name, value; |
f30432d7 | 4368 | |
2c73f9f5 | 4369 | if (TREE_CODE (decl) != SCOPE_REF |
2f939d94 | 4370 | || !TYPE_P (TREE_OPERAND (decl, 0))) |
cffa8729 | 4371 | { |
33bd39a2 | 4372 | error ("using-declaration for non-member at class scope"); |
cffa8729 MS |
4373 | return NULL_TREE; |
4374 | } | |
4375 | name = TREE_OPERAND (decl, 1); | |
4376 | if (TREE_CODE (name) == BIT_NOT_EXPR) | |
4377 | { | |
33bd39a2 | 4378 | error ("using-declaration for destructor"); |
cffa8729 MS |
4379 | return NULL_TREE; |
4380 | } | |
eac69b8a GDR |
4381 | else if (TREE_CODE (name) == TEMPLATE_ID_EXPR) |
4382 | { | |
4383 | name = TREE_OPERAND (name, 0); | |
33bd39a2 | 4384 | error ("a using-declaration cannot specify a template-id. Try `using %T::%D'", TREE_OPERAND (decl, 0), name); |
eac69b8a GDR |
4385 | return NULL_TREE; |
4386 | } | |
a723baf1 MM |
4387 | if (TREE_CODE (name) == TYPE_DECL) |
4388 | { | |
4389 | tree type = TREE_TYPE (name); | |
4390 | if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (name))) | |
4391 | { | |
4392 | name = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)); | |
4393 | error ("a using-declaration cannot specify a template-id."); | |
4394 | return NULL_TREE; | |
4395 | } | |
4396 | name = DECL_NAME (name); | |
4397 | } | |
4398 | else if (TREE_CODE (name) == TEMPLATE_DECL) | |
4399 | name = DECL_NAME (name); | |
50ad9642 MM |
4400 | else if (BASELINK_P (name)) |
4401 | { | |
a723baf1 MM |
4402 | tree fns; |
4403 | ||
4404 | fns = BASELINK_FUNCTIONS (name); | |
4405 | if (TREE_CODE (fns) == TEMPLATE_ID_EXPR) | |
4406 | { | |
4407 | fns = TREE_OPERAND (fns, 0); | |
4408 | error ("a using-declaration cannot specify a template-id. Try `using %T::%D'", | |
4409 | BASELINK_ACCESS_BINFO (name), | |
4410 | DECL_NAME (get_first_fn (fns))); | |
4411 | } | |
4412 | name = DECL_NAME (get_first_fn (fns)); | |
50ad9642 | 4413 | } |
1c35f5b6 JM |
4414 | |
4415 | my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 980716); | |
cffa8729 | 4416 | |
4ce3d537 | 4417 | value = build_lang_decl (USING_DECL, name, void_type_node); |
cffa8729 MS |
4418 | DECL_INITIAL (value) = TREE_OPERAND (decl, 0); |
4419 | return value; | |
6060a796 MS |
4420 | } |
4421 | ||
a1c65f9f | 4422 | /* Process a using-directive. */ |
2c73f9f5 | 4423 | |
6060a796 | 4424 | void |
848eed92 | 4425 | do_using_directive (tree namespace) |
6060a796 | 4426 | { |
5158d7ee NS |
4427 | if (building_stmt_tree ()) |
4428 | add_stmt (build_stmt (USING_STMT, namespace)); | |
9da99f7d | 4429 | |
3e3f722c | 4430 | /* using namespace A::B::C; */ |
30394414 JM |
4431 | if (TREE_CODE (namespace) == SCOPE_REF) |
4432 | namespace = TREE_OPERAND (namespace, 1); | |
2c73f9f5 ML |
4433 | if (TREE_CODE (namespace) == IDENTIFIER_NODE) |
4434 | { | |
a1c65f9f | 4435 | /* Lookup in lexer did not find a namespace. */ |
9da99f7d | 4436 | if (!processing_template_decl) |
33bd39a2 | 4437 | error ("namespace `%T' undeclared", namespace); |
2c73f9f5 ML |
4438 | return; |
4439 | } | |
4440 | if (TREE_CODE (namespace) != NAMESPACE_DECL) | |
4441 | { | |
9da99f7d | 4442 | if (!processing_template_decl) |
33bd39a2 | 4443 | error ("`%T' is not a namespace", namespace); |
2c73f9f5 ML |
4444 | return; |
4445 | } | |
3e3f722c | 4446 | namespace = ORIGINAL_NAMESPACE (namespace); |
ea9635c7 | 4447 | if (!toplevel_bindings_p ()) |
9ed182dc | 4448 | push_using_directive (namespace); |
ea9635c7 ML |
4449 | else |
4450 | /* direct usage */ | |
4451 | add_using_namespace (current_namespace, namespace, 0); | |
6060a796 | 4452 | } |
f30432d7 MS |
4453 | |
4454 | void | |
848eed92 | 4455 | check_default_args (tree x) |
f30432d7 MS |
4456 | { |
4457 | tree arg = TYPE_ARG_TYPES (TREE_TYPE (x)); | |
848eed92 GDR |
4458 | bool saw_def = false; |
4459 | int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE); | |
f30432d7 MS |
4460 | for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i) |
4461 | { | |
4462 | if (TREE_PURPOSE (arg)) | |
848eed92 | 4463 | saw_def = true; |
f30432d7 MS |
4464 | else if (saw_def) |
4465 | { | |
8251199e | 4466 | cp_error_at ("default argument missing for parameter %P of `%+#D'", |
b7698cf0 | 4467 | i, x); |
f30432d7 MS |
4468 | break; |
4469 | } | |
4470 | } | |
4471 | } | |
72b7eeff MS |
4472 | |
4473 | void | |
848eed92 | 4474 | mark_used (tree decl) |
72b7eeff MS |
4475 | { |
4476 | TREE_USED (decl) = 1; | |
5156628f | 4477 | if (processing_template_decl) |
5566b478 | 4478 | return; |
eab5474f NS |
4479 | |
4480 | if (TREE_CODE (decl) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (decl) | |
4481 | && !TREE_ASM_WRITTEN (decl)) | |
4482 | /* Remember it, so we can check it was defined. */ | |
4483 | defer_fn (decl); | |
4484 | ||
c01b9ec8 AO |
4485 | if (!skip_evaluation) |
4486 | assemble_external (decl); | |
36a117a5 | 4487 | |
73aad9b9 | 4488 | /* Is it a synthesized method that needs to be synthesized? */ |
4f1c5b7d MM |
4489 | if (TREE_CODE (decl) == FUNCTION_DECL |
4490 | && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl) | |
4491 | && DECL_ARTIFICIAL (decl) | |
4c4646b5 | 4492 | && !DECL_THUNK_P (decl) |
4f1c5b7d | 4493 | && ! DECL_INITIAL (decl) |
73aad9b9 JM |
4494 | /* Kludge: don't synthesize for default args. */ |
4495 | && current_function_decl) | |
db9b2174 MM |
4496 | { |
4497 | synthesize_method (decl); | |
4498 | /* If we've already synthesized the method we don't need to | |
4499 | instantiate it, so we can return right away. */ | |
4500 | return; | |
4501 | } | |
36a117a5 MM |
4502 | |
4503 | /* If this is a function or variable that is an instance of some | |
4504 | template, we now know that we will need to actually do the | |
7c355bca | 4505 | instantiation. We check that DECL is not an explicit |
03d0f4af | 4506 | instantiation because that is not checked in instantiate_decl. */ |
872f37f9 | 4507 | if ((DECL_NON_THUNK_FUNCTION_P (decl) || TREE_CODE (decl) == VAR_DECL) |
03d0f4af | 4508 | && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl) |
7610f2ce JM |
4509 | && (!DECL_EXPLICIT_INSTANTIATION (decl) |
4510 | || (TREE_CODE (decl) == FUNCTION_DECL && DECL_INLINE (decl)))) | |
16d53b64 | 4511 | instantiate_decl (decl, /*defer_ok=*/1); |
72b7eeff | 4512 | } |
f62dbf03 | 4513 | |
089d6ea7 MM |
4514 | /* Called when a class-head is encountered. TAG_KIND is the class-key |
4515 | for the class. SCOPE, if non-NULL, is the type or namespace | |
4516 | indicated in the nested-name-specifier for the declaration of the | |
4517 | class. ID is the name of the class, if any; it may be a TYPE_DECL, | |
4518 | or an IDENTIFIER_NODE. ATTRIBUTES are attributes that apply to the | |
4519 | class. | |
4520 | ||
4521 | Return a TYPE_DECL for the class being defined. */ | |
f62dbf03 JM |
4522 | |
4523 | tree | |
848eed92 | 4524 | handle_class_head (enum tag_types tag_kind, tree scope, tree id, |
089d6ea7 | 4525 | tree attributes) |
f62dbf03 | 4526 | { |
f2ae0c45 | 4527 | tree decl = NULL_TREE; |
d46a33b3 NS |
4528 | tree current = current_scope (); |
4529 | bool xrefd_p = false; | |
089d6ea7 MM |
4530 | bool new_type_p; |
4531 | tree context; | |
4532 | ||
d46a33b3 NS |
4533 | if (current == NULL_TREE) |
4534 | current = current_namespace; | |
f2ae0c45 | 4535 | |
d46a33b3 NS |
4536 | if (scope) |
4537 | { | |
4538 | if (TREE_CODE (id) == TYPE_DECL) | |
4539 | /* We must bash typedefs back to the main decl of the | |
4540 | type. Otherwise we become confused about scopes. */ | |
4541 | decl = TYPE_MAIN_DECL (TREE_TYPE (id)); | |
4542 | else if (DECL_CLASS_TEMPLATE_P (id)) | |
4543 | decl = DECL_TEMPLATE_RESULT (id); | |
4544 | else | |
f2ae0c45 | 4545 | { |
d46a33b3 NS |
4546 | if (TYPE_P (scope)) |
4547 | { | |
4548 | /* According to the suggested resolution of core issue | |
4549 | 180, 'typename' is assumed after a class-key. */ | |
4f2b0fb2 | 4550 | decl = make_typename_type (scope, id, tf_error); |
d46a33b3 NS |
4551 | if (decl != error_mark_node) |
4552 | decl = TYPE_MAIN_DECL (decl); | |
4553 | else | |
4554 | decl = NULL_TREE; | |
4555 | } | |
4556 | else if (scope == current) | |
4557 | { | |
4558 | /* We've been given AGGR SCOPE::ID, when we're already | |
4559 | inside SCOPE. Be nice about it. */ | |
4560 | if (pedantic) | |
4561 | pedwarn ("extra qualification `%T::' on member `%D' ignored", | |
4562 | scope, id); | |
4563 | } | |
fc928afe | 4564 | else |
d46a33b3 NS |
4565 | error ("`%T' does not have a class or union named `%D'", |
4566 | scope, id); | |
f2ae0c45 | 4567 | } |
3d7de1fa | 4568 | } |
d46a33b3 NS |
4569 | |
4570 | if (!decl) | |
4571 | { | |
089d6ea7 | 4572 | decl = TYPE_MAIN_DECL (xref_tag (tag_kind, id, attributes, false)); |
d46a33b3 NS |
4573 | xrefd_p = true; |
4574 | } | |
f62dbf03 | 4575 | |
d46a33b3 NS |
4576 | if (!TYPE_BINFO (TREE_TYPE (decl))) |
4577 | { | |
4578 | error ("`%T' is not a class or union type", decl); | |
4579 | return error_mark_node; | |
4580 | } | |
4581 | ||
089d6ea7 MM |
4582 | /* For a definition, we want to enter the containing scope before |
4583 | looking up any base classes etc. Only do so, if this is different | |
4584 | to the current scope. */ | |
4585 | context = CP_DECL_CONTEXT (decl); | |
4586 | ||
4587 | new_type_p = (current != context | |
4588 | && TREE_CODE (context) != TEMPLATE_TYPE_PARM | |
4589 | && TREE_CODE (context) != BOUND_TEMPLATE_TEMPLATE_PARM); | |
4590 | if (new_type_p) | |
4591 | push_scope (context); | |
4592 | ||
4593 | if (!xrefd_p | |
4594 | && PROCESSING_REAL_TEMPLATE_DECL_P () | |
4595 | && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))) | |
4596 | decl = push_template_decl (decl); | |
3d7de1fa MM |
4597 | |
4598 | return decl; | |
f62dbf03 | 4599 | } |
fc6af6e3 | 4600 | |
e2500fed | 4601 | #include "gt-cp-decl2.h" |