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