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