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