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