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