f7e20640694f42d50bee152d7102c4573eda8a33
[gcc.git] / gcc / cp / decl2.c
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22
23 /* Process declarations and symbol lookup for C front end.
24 Also constructs types; the standard scalar types at initialization,
25 and structure, union, array and enum types when they are declared. */
26
27 /* ??? not all decl nodes are given the most useful possible
28 line numbers. For example, the CONST_DECLs for enum values. */
29
30 #include "config.h"
31 #include <stdio.h>
32 #include "tree.h"
33 #include "rtl.h"
34 #include "flags.h"
35 #include "cp-tree.h"
36 #include "decl.h"
37 #include "lex.h"
38 #include "output.h"
39
40 extern tree get_file_function_name ();
41 static void grok_function_init PROTO((tree, tree));
42 void import_export_decl ();
43 extern int current_class_depth;
44
45 /* A list of virtual function tables we must make sure to write out. */
46 tree pending_vtables;
47
48 /* A list of static class variables. This is needed, because a
49 static class variable can be declared inside the class without
50 an initializer, and then initialized, staticly, outside the class. */
51 tree pending_statics;
52
53 /* A list of functions which were declared inline, but which we
54 may need to emit outline anyway. */
55 static tree saved_inlines;
56
57 /* Used to help generate temporary names which are unique within
58 a function. Reset to 0 by start_function. */
59
60 int temp_name_counter;
61
62 /* Same, but not reset. Local temp variables and global temp variables
63 can have the same name. */
64 static int global_temp_name_counter;
65
66 /* Flag used when debugging spew.c */
67
68 extern int spew_debug;
69
70 /* Nonzero if we're done parsing and into end-of-file activities. */
71
72 int at_eof;
73
74 /* Functions called along with real static constructors and destructors. */
75
76 tree static_ctors, static_dtors;
77 \f
78 /* C (and C++) language-specific option variables. */
79
80 /* Nonzero means allow type mismatches in conditional expressions;
81 just make their values `void'. */
82
83 int flag_cond_mismatch;
84
85 /* Nonzero means give `double' the same size as `float'. */
86
87 int flag_short_double;
88
89 /* Nonzero means don't recognize the keyword `asm'. */
90
91 int flag_no_asm;
92
93 /* Nonzero means don't recognize any extension keywords. */
94
95 int flag_no_gnu_keywords;
96
97 /* Nonzero means don't recognize the non-ANSI builtin functions. */
98
99 int flag_no_builtin;
100
101 /* Nonzero means don't recognize the non-ANSI builtin functions.
102 -ansi sets this. */
103
104 int flag_no_nonansi_builtin;
105
106 /* Nonzero means do some things the same way PCC does. Only provided so
107 the compiler will link. */
108
109 int flag_traditional;
110
111 /* Nonzero means to treat bitfields as unsigned unless they say `signed'. */
112
113 int flag_signed_bitfields = 1;
114
115 /* Nonzero means handle `#ident' directives. 0 means ignore them. */
116
117 int flag_no_ident;
118
119 /* Nonzero means enable obscure ANSI features and disable GNU extensions
120 that might cause ANSI-compliant code to be miscompiled. */
121
122 int flag_ansi;
123
124 /* Nonzero means do argument matching for overloading according to the
125 ANSI rules, rather than what g++ used to believe to be correct. */
126
127 int flag_ansi_overloading = 1;
128
129 /* Nonzero means do emit exported implementations of functions even if
130 they can be inlined. */
131
132 int flag_implement_inlines = 1;
133
134 /* Nonzero means do emit exported implementations of templates, instead of
135 multiple static copies in each file that needs a definition. */
136
137 int flag_external_templates;
138
139 /* Nonzero means that the decision to emit or not emit the implementation of a
140 template depends on where the template is instantiated, rather than where
141 it is defined. */
142
143 int flag_alt_external_templates;
144
145 /* Nonzero means that implicit instantiations will be emitted if needed. */
146
147 int flag_implicit_templates = 1;
148
149 /* Nonzero means warn about implicit declarations. */
150
151 int warn_implicit = 1;
152
153 /* Nonzero means warn when all ctors or dtors are private, and the class
154 has no friends. */
155
156 int warn_ctor_dtor_privacy = 1;
157
158 /* True if we want to implement vtables using "thunks".
159 The default is off. */
160
161 #if defined(NEW_OVER) && defined (__i386__)
162 int flag_vtable_thunks = 1;
163 #else
164 int flag_vtable_thunks;
165 #endif
166
167 /* True if we want to deal with repository information. */
168
169 int flag_use_repository;
170
171 /* Nonzero means give string constants the type `const char *'
172 to get extra warnings from them. These warnings will be too numerous
173 to be useful, except in thoroughly ANSIfied programs. */
174
175 int warn_write_strings;
176
177 /* Nonzero means warn about pointer casts that can drop a type qualifier
178 from the pointer target type. */
179
180 int warn_cast_qual;
181
182 /* Nonzero means warn that dbx info for template class methods isn't fully
183 supported yet. */
184
185 int warn_template_debugging;
186
187 /* Nonzero means warn about sizeof(function) or addition/subtraction
188 of function pointers. */
189
190 int warn_pointer_arith = 1;
191
192 /* Nonzero means warn for any function def without prototype decl. */
193
194 int warn_missing_prototypes;
195
196 /* Nonzero means warn about multiple (redundant) decls for the same single
197 variable or function. */
198
199 int warn_redundant_decls;
200
201 /* Warn if initializer is not completely bracketed. */
202
203 int warn_missing_braces;
204
205 /* Warn about comparison of signed and unsigned values. */
206
207 int warn_sign_compare;
208
209 /* Warn about *printf or *scanf format/argument anomalies. */
210
211 int warn_format;
212
213 /* Warn about a subscript that has type char. */
214
215 int warn_char_subscripts;
216
217 /* Warn if a type conversion is done that might have confusing results. */
218
219 int warn_conversion;
220
221 /* Warn if adding () is suggested. */
222
223 int warn_parentheses;
224
225 /* Non-zero means warn in function declared in derived class has the
226 same name as a virtual in the base class, but fails to match the
227 type signature of any virtual function in the base class. */
228 int warn_overloaded_virtual;
229
230 /* Non-zero means warn when declaring a class that has a non virtual
231 destructor, when it really ought to have a virtual one. */
232 int warn_nonvdtor;
233
234 /* Non-zero means warn when a function is declared extern and later inline. */
235 int warn_extern_inline;
236
237 /* Non-zero means warn when the compiler will reorder code. */
238 int warn_reorder;
239
240 /* Non-zero means warn when synthesis behavior differs from Cfront's. */
241 int warn_synth;
242
243 /* Non-zero means warn when we convert a pointer to member function
244 into a pointer to (void or function). */
245 int warn_pmf2ptr = 1;
246
247 /* Nonzero means warn about violation of some Effective C++ style rules. */
248
249 int warn_ecpp = 0;
250
251 /* Nonzero means `$' can be in an identifier.
252 See cccp.c for reasons why this breaks some obscure ANSI C programs. */
253
254 #ifndef DOLLARS_IN_IDENTIFIERS
255 #define DOLLARS_IN_IDENTIFIERS 1
256 #endif
257 int dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
258
259 /* Nonzero for -fno-strict-prototype switch: do not consider empty
260 argument prototype to mean function takes no arguments. */
261
262 int flag_strict_prototype = 2;
263 int strict_prototype = 1;
264 int strict_prototypes_lang_c, strict_prototypes_lang_cplusplus = 1;
265
266 /* Nonzero means that labels can be used as first-class objects */
267
268 int flag_labels_ok;
269
270 /* Non-zero means to collect statistics which might be expensive
271 and to print them when we are done. */
272 int flag_detailed_statistics;
273
274 /* C++ specific flags. */
275 /* Nonzero for -fall-virtual: make every member function (except
276 constructors) lay down in the virtual function table. Calls
277 can then either go through the virtual function table or not,
278 depending. */
279
280 int flag_all_virtual;
281
282 /* Zero means that `this' is a *const. This gives nice behavior in the
283 2.0 world. 1 gives 1.2-compatible behavior. 2 gives Spring behavior.
284 -2 means we're constructing an object and it has fixed type. */
285
286 int flag_this_is_variable;
287
288 /* Nonzero means memoize our member lookups. */
289
290 int flag_memoize_lookups; int flag_save_memoized_contexts;
291
292 /* 3 means write out only virtuals function tables `defined'
293 in this implementation file.
294 2 means write out only specific virtual function tables
295 and give them (C) public access.
296 1 means write out virtual function tables and give them
297 (C) public access.
298 0 means write out virtual function tables and give them
299 (C) static access (default).
300 -1 means declare virtual function tables extern. */
301
302 int write_virtuals;
303
304 /* Nonzero means we should attempt to elide constructors when possible. */
305
306 int flag_elide_constructors;
307
308 /* Nonzero means recognize and handle signature language constructs. */
309
310 int flag_handle_signatures;
311
312 /* Nonzero means that member functions defined in class scope are
313 inline by default. */
314
315 int flag_default_inline = 1;
316
317 /* Controls whether enums and ints freely convert.
318 1 means with complete freedom.
319 0 means enums can convert to ints, but not vice-versa. */
320 int flag_int_enum_equivalence;
321
322 /* Controls whether compiler generates 'type descriptor' that give
323 run-time type information. */
324 int flag_rtti = 1;
325
326 /* Nonzero if we wish to output cross-referencing information
327 for the GNU class browser. */
328 extern int flag_gnu_xref;
329
330 /* Nonzero if compiler can make `reasonable' assumptions about
331 references and objects. For example, the compiler must be
332 conservative about the following and not assume that `a' is nonnull:
333
334 obj &a = g ();
335 a.f (2);
336
337 In general, it is `reasonable' to assume that for many programs,
338 and better code can be generated in that case. */
339
340 int flag_assume_nonnull_objects = 1;
341
342 /* Nonzero if we want to support huge (> 2^(sizeof(short)*8-1) bytes)
343 objects. */
344
345 int flag_huge_objects;
346
347 /* Nonzero if we want to conserve space in the .o files. We do this
348 by putting uninitialized data and runtime initialized data into
349 .common instead of .data at the expense of not flagging multiple
350 definitions. */
351
352 int flag_conserve_space;
353
354 /* Nonzero if we want to obey access control semantics. */
355
356 int flag_access_control = 1;
357
358 /* Nonzero if we want to understand the operator names, i.e. 'bitand'. */
359
360 int flag_operator_names;
361
362 /* Nonzero if we want to check the return value of new and avoid calling
363 constructors if it is a null pointer. */
364
365 int flag_check_new;
366
367 /* Nonzero if we want the new ANSI rules for pushing a new scope for `for'
368 initialization variables.
369 0: Old rules, set by -fno-for-scope.
370 2: New ANSI rules, set by -ffor-scope.
371 1: Try to implement new ANSI rules, but with backup compatibility
372 (and warnings). This is the default, for now. */
373
374 int flag_new_for_scope = 1;
375
376 /* Nonzero if we want to emit defined symbols with common-like linkage as
377 weak symbols where possible, in order to conform to C++ semantics.
378 Otherwise, emit them as local symbols. */
379
380 int flag_weak = 1;
381
382 /* Maximum template instantiation depth. Must be at least 17 for ANSI
383 compliance. */
384
385 int max_tinst_depth = 17;
386
387 /* Table of language-dependent -f options.
388 STRING is the option name. VARIABLE is the address of the variable.
389 ON_VALUE is the value to store in VARIABLE
390 if `-fSTRING' is seen as an option.
391 (If `-fno-STRING' is seen as an option, the opposite value is stored.) */
392
393 static struct { char *string; int *variable; int on_value;} lang_f_options[] =
394 {
395 {"signed-char", &flag_signed_char, 1},
396 {"unsigned-char", &flag_signed_char, 0},
397 {"signed-bitfields", &flag_signed_bitfields, 1},
398 {"unsigned-bitfields", &flag_signed_bitfields, 0},
399 {"short-enums", &flag_short_enums, 1},
400 {"short-double", &flag_short_double, 1},
401 {"cond-mismatch", &flag_cond_mismatch, 1},
402 {"asm", &flag_no_asm, 0},
403 {"builtin", &flag_no_builtin, 0},
404 {"ident", &flag_no_ident, 0},
405 {"labels-ok", &flag_labels_ok, 1},
406 {"stats", &flag_detailed_statistics, 1},
407 {"this-is-variable", &flag_this_is_variable, 1},
408 {"strict-prototype", &flag_strict_prototype, 1},
409 {"all-virtual", &flag_all_virtual, 1},
410 {"memoize-lookups", &flag_memoize_lookups, 1},
411 {"elide-constructors", &flag_elide_constructors, 1},
412 {"handle-signatures", &flag_handle_signatures, 1},
413 {"default-inline", &flag_default_inline, 1},
414 {"dollars-in-identifiers", &dollars_in_ident, 1},
415 {"enum-int-equiv", &flag_int_enum_equivalence, 1},
416 {"rtti", &flag_rtti, 1},
417 {"xref", &flag_gnu_xref, 1},
418 {"nonnull-objects", &flag_assume_nonnull_objects, 1},
419 {"implement-inlines", &flag_implement_inlines, 1},
420 {"external-templates", &flag_external_templates, 1},
421 {"implicit-templates", &flag_implicit_templates, 1},
422 {"ansi-overloading", &flag_ansi_overloading, 1},
423 {"huge-objects", &flag_huge_objects, 1},
424 {"conserve-space", &flag_conserve_space, 1},
425 {"vtable-thunks", &flag_vtable_thunks, 1},
426 {"access-control", &flag_access_control, 1},
427 {"nonansi-builtins", &flag_no_nonansi_builtin, 0},
428 {"gnu-keywords", &flag_no_gnu_keywords, 0},
429 {"operator-names", &flag_operator_names, 1},
430 {"check-new", &flag_check_new, 1},
431 {"repo", &flag_use_repository, 1},
432 {"for-scope", &flag_new_for_scope, 2},
433 {"weak", &flag_weak, 1}
434 };
435
436 /* Decode the string P as a language-specific option.
437 Return 1 if it is recognized (and handle it);
438 return 0 if not recognized. */
439
440 int
441 lang_decode_option (p)
442 char *p;
443 {
444 if (!strcmp (p, "-ftraditional") || !strcmp (p, "-traditional"))
445 dollars_in_ident = 1, flag_writable_strings = 1,
446 flag_this_is_variable = 1, flag_new_for_scope = 0;
447 /* The +e options are for cfront compatibility. They come in as
448 `-+eN', to kludge around gcc.c's argument handling. */
449 else if (p[0] == '-' && p[1] == '+' && p[2] == 'e')
450 {
451 int old_write_virtuals = write_virtuals;
452 if (p[3] == '1')
453 write_virtuals = 1;
454 else if (p[3] == '0')
455 write_virtuals = -1;
456 else if (p[3] == '2')
457 write_virtuals = 2;
458 else error ("invalid +e option");
459 if (old_write_virtuals != 0
460 && write_virtuals != old_write_virtuals)
461 error ("conflicting +e options given");
462 }
463 else if (p[0] == '-' && p[1] == 'f')
464 {
465 /* Some kind of -f option.
466 P's value is the option sans `-f'.
467 Search for it in the table of options. */
468 int found = 0, j;
469
470 p += 2;
471 /* Try special -f options. */
472
473 if (!strcmp (p, "save-memoized"))
474 {
475 flag_memoize_lookups = 1;
476 flag_save_memoized_contexts = 1;
477 found = 1;
478 }
479 if (!strcmp (p, "no-save-memoized"))
480 {
481 flag_memoize_lookups = 0;
482 flag_save_memoized_contexts = 0;
483 found = 1;
484 }
485 else if (! strcmp (p, "alt-external-templates"))
486 {
487 flag_external_templates = 1;
488 flag_alt_external_templates = 1;
489 found = 1;
490 }
491 else if (! strcmp (p, "no-alt-external-templates"))
492 {
493 flag_alt_external_templates = 0;
494 found = 1;
495 }
496 else if (!strcmp (p, "repo"))
497 {
498 flag_use_repository = 1;
499 flag_implicit_templates = 0;
500 found = 1;
501 }
502 else if (!strncmp (p, "template-depth-", 15))
503 {
504 char *endp = p + 15;
505 while (*endp)
506 {
507 if (*endp >= '0' && *endp <= '9')
508 endp++;
509 else
510 {
511 error ("Invalid option `%s'", p - 2);
512 goto template_depth_lose;
513 }
514 }
515 max_tinst_depth = atoi (p + 15);
516 template_depth_lose: ;
517 }
518 else for (j = 0;
519 !found && j < sizeof (lang_f_options) / sizeof (lang_f_options[0]);
520 j++)
521 {
522 if (!strcmp (p, lang_f_options[j].string))
523 {
524 *lang_f_options[j].variable = lang_f_options[j].on_value;
525 /* A goto here would be cleaner,
526 but breaks the vax pcc. */
527 found = 1;
528 }
529 if (p[0] == 'n' && p[1] == 'o' && p[2] == '-'
530 && ! strcmp (p+3, lang_f_options[j].string))
531 {
532 *lang_f_options[j].variable = ! lang_f_options[j].on_value;
533 found = 1;
534 }
535 }
536 return found;
537 }
538 else if (p[0] == '-' && p[1] == 'W')
539 {
540 int setting = 1;
541
542 /* The -W options control the warning behavior of the compiler. */
543 p += 2;
544
545 if (p[0] == 'n' && p[1] == 'o' && p[2] == '-')
546 setting = 0, p += 3;
547
548 if (!strcmp (p, "implicit"))
549 warn_implicit = setting;
550 else if (!strcmp (p, "return-type"))
551 warn_return_type = setting;
552 else if (!strcmp (p, "ctor-dtor-privacy"))
553 warn_ctor_dtor_privacy = setting;
554 else if (!strcmp (p, "write-strings"))
555 warn_write_strings = setting;
556 else if (!strcmp (p, "cast-qual"))
557 warn_cast_qual = setting;
558 else if (!strcmp (p, "char-subscripts"))
559 warn_char_subscripts = setting;
560 else if (!strcmp (p, "pointer-arith"))
561 warn_pointer_arith = setting;
562 else if (!strcmp (p, "missing-prototypes"))
563 warn_missing_prototypes = setting;
564 else if (!strcmp (p, "redundant-decls"))
565 warn_redundant_decls = setting;
566 else if (!strcmp (p, "missing-braces"))
567 warn_missing_braces = setting;
568 else if (!strcmp (p, "sign-compare"))
569 warn_sign_compare = setting;
570 else if (!strcmp (p, "format"))
571 warn_format = setting;
572 else if (!strcmp (p, "conversion"))
573 warn_conversion = setting;
574 else if (!strcmp (p, "parentheses"))
575 warn_parentheses = setting;
576 else if (!strcmp (p, "non-virtual-dtor"))
577 warn_nonvdtor = setting;
578 else if (!strcmp (p, "extern-inline"))
579 warn_extern_inline = setting;
580 else if (!strcmp (p, "reorder"))
581 warn_reorder = setting;
582 else if (!strcmp (p, "synth"))
583 warn_synth = setting;
584 else if (!strcmp (p, "pmf-conversions"))
585 warn_pmf2ptr = setting;
586 else if (!strcmp (p, "effc++"))
587 warn_ecpp = setting;
588 else if (!strcmp (p, "comment"))
589 ; /* cpp handles this one. */
590 else if (!strcmp (p, "comments"))
591 ; /* cpp handles this one. */
592 else if (!strcmp (p, "trigraphs"))
593 ; /* cpp handles this one. */
594 else if (!strcmp (p, "import"))
595 ; /* cpp handles this one. */
596 else if (!strcmp (p, "all"))
597 {
598 warn_return_type = setting;
599 warn_unused = setting;
600 warn_implicit = setting;
601 warn_ctor_dtor_privacy = setting;
602 warn_switch = setting;
603 warn_format = setting;
604 warn_parentheses = setting;
605 warn_missing_braces = setting;
606 warn_sign_compare = setting;
607 warn_extern_inline = setting;
608 warn_nonvdtor = setting;
609 /* We save the value of warn_uninitialized, since if they put
610 -Wuninitialized on the command line, we need to generate a
611 warning about not using it without also specifying -O. */
612 if (warn_uninitialized != 1)
613 warn_uninitialized = (setting ? 2 : 0);
614 warn_template_debugging = setting;
615 warn_reorder = setting;
616 }
617
618 else if (!strcmp (p, "overloaded-virtual"))
619 warn_overloaded_virtual = setting;
620 else return 0;
621 }
622 else if (!strcmp (p, "-ansi"))
623 dollars_in_ident = 0, flag_no_nonansi_builtin = 1, flag_ansi = 1,
624 flag_no_gnu_keywords = 1, flag_operator_names = 1;
625 #ifdef SPEW_DEBUG
626 /* Undocumented, only ever used when you're invoking cc1plus by hand, since
627 it's probably safe to assume no sane person would ever want to use this
628 under normal circumstances. */
629 else if (!strcmp (p, "-spew-debug"))
630 spew_debug = 1;
631 #endif
632 else
633 return 0;
634
635 return 1;
636 }
637 \f
638 /* Incorporate `const' and `volatile' qualifiers for member functions.
639 FUNCTION is a TYPE_DECL or a FUNCTION_DECL.
640 QUALS is a list of qualifiers. */
641
642 tree
643 grok_method_quals (ctype, function, quals)
644 tree ctype, function, quals;
645 {
646 tree fntype = TREE_TYPE (function);
647 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
648
649 do
650 {
651 extern tree ridpointers[];
652
653 if (TREE_VALUE (quals) == ridpointers[(int)RID_CONST])
654 {
655 if (TYPE_READONLY (ctype))
656 error ("duplicate `%s' %s",
657 IDENTIFIER_POINTER (TREE_VALUE (quals)),
658 (TREE_CODE (function) == FUNCTION_DECL
659 ? "for member function" : "in type declaration"));
660 ctype = build_type_variant (ctype, 1, TYPE_VOLATILE (ctype));
661 build_pointer_type (ctype);
662 }
663 else if (TREE_VALUE (quals) == ridpointers[(int)RID_VOLATILE])
664 {
665 if (TYPE_VOLATILE (ctype))
666 error ("duplicate `%s' %s",
667 IDENTIFIER_POINTER (TREE_VALUE (quals)),
668 (TREE_CODE (function) == FUNCTION_DECL
669 ? "for member function" : "in type declaration"));
670 ctype = build_type_variant (ctype, TYPE_READONLY (ctype), 1);
671 build_pointer_type (ctype);
672 }
673 else
674 my_friendly_abort (20);
675 quals = TREE_CHAIN (quals);
676 }
677 while (quals);
678 fntype = build_cplus_method_type (ctype, TREE_TYPE (fntype),
679 (TREE_CODE (fntype) == METHOD_TYPE
680 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
681 : TYPE_ARG_TYPES (fntype)));
682 if (raises)
683 fntype = build_exception_variant (fntype, raises);
684
685 TREE_TYPE (function) = fntype;
686 return ctype;
687 }
688
689 #if 0 /* Not used. */
690 /* This routine replaces cryptic DECL_NAMEs with readable DECL_NAMEs.
691 It leaves DECL_ASSEMBLER_NAMEs with the correct value. */
692 /* This does not yet work with user defined conversion operators
693 It should. */
694
695 static void
696 substitute_nice_name (decl)
697 tree decl;
698 {
699 if (DECL_NAME (decl) && TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE)
700 {
701 char *n = decl_as_string (DECL_NAME (decl), 1);
702 if (n[strlen (n) - 1] == ' ')
703 n[strlen (n) - 1] = 0;
704 DECL_NAME (decl) = get_identifier (n);
705 }
706 }
707 #endif
708
709 /* Warn when -fexternal-templates is used and #pragma
710 interface/implementation is not used all the times it should be,
711 inform the user. */
712
713 void
714 warn_if_unknown_interface (decl)
715 tree decl;
716 {
717 static int already_warned = 0;
718 if (already_warned++)
719 return;
720
721 if (flag_alt_external_templates)
722 {
723 struct tinst_level *til = tinst_for_decl ();
724 int sl = lineno;
725 char *sf = input_filename;
726
727 if (til)
728 {
729 lineno = til->line;
730 input_filename = til->file;
731 }
732 cp_warning ("template `%#D' instantiated in file without #pragma interface",
733 decl);
734 lineno = sl;
735 input_filename = sf;
736 }
737 else
738 cp_warning_at ("template `%#D' defined in file without #pragma interface",
739 decl);
740 }
741
742 /* A subroutine of the parser, to handle a component list. */
743
744 tree
745 grok_x_components (specs, components)
746 tree specs, components;
747 {
748 register tree t, x, tcode;
749
750 /* We just got some friends. They have been recorded elsewhere. */
751 if (components == void_type_node)
752 return NULL_TREE;
753
754 if (components == NULL_TREE)
755 {
756 t = groktypename (build_decl_list (specs, NULL_TREE));
757
758 if (t == NULL_TREE)
759 {
760 error ("error in component specification");
761 return NULL_TREE;
762 }
763
764 switch (TREE_CODE (t))
765 {
766 case VAR_DECL:
767 /* Static anonymous unions come out as VAR_DECLs. */
768 if (TREE_CODE (TREE_TYPE (t)) == UNION_TYPE
769 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (TREE_TYPE (t))))
770 return t;
771
772 /* We return SPECS here, because in the parser it was ending
773 up with not doing anything to $$, which is what SPECS
774 represents. */
775 return specs;
776 break;
777
778 case RECORD_TYPE:
779 /* This code may be needed for UNION_TYPEs as
780 well. */
781 tcode = record_type_node;
782 if (CLASSTYPE_DECLARED_CLASS (t))
783 tcode = class_type_node;
784 else if (IS_SIGNATURE (t))
785 tcode = signature_type_node;
786
787 t = xref_tag (tcode, TYPE_IDENTIFIER (t), NULL_TREE, 0);
788 if (TYPE_CONTEXT (t))
789 CLASSTYPE_NO_GLOBALIZE (t) = 1;
790 return NULL_TREE;
791 break;
792
793 case UNION_TYPE:
794 case ENUMERAL_TYPE:
795 if (TREE_CODE (t) == UNION_TYPE)
796 tcode = union_type_node;
797 else
798 tcode = enum_type_node;
799
800 t = xref_tag (tcode, TYPE_IDENTIFIER (t), NULL_TREE, 0);
801 if (TREE_CODE (t) == UNION_TYPE && TYPE_CONTEXT (t))
802 CLASSTYPE_NO_GLOBALIZE (t) = 1;
803 if (TREE_CODE (t) == UNION_TYPE
804 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
805 {
806 /* See also shadow_tag. */
807
808 struct pending_inline **p;
809 tree *q;
810 x = build_lang_field_decl (FIELD_DECL, NULL_TREE, t);
811
812 /* Wipe out memory of synthesized methods */
813 TYPE_HAS_CONSTRUCTOR (t) = 0;
814 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
815 TYPE_HAS_INIT_REF (t) = 0;
816 TYPE_HAS_CONST_INIT_REF (t) = 0;
817 TYPE_HAS_ASSIGN_REF (t) = 0;
818 TYPE_HAS_ASSIGNMENT (t) = 0;
819 TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
820
821 q = &TYPE_METHODS (t);
822 while (*q)
823 {
824 if (DECL_ARTIFICIAL (*q))
825 *q = TREE_CHAIN (*q);
826 else
827 q = &TREE_CHAIN (*q);
828 }
829 if (TYPE_METHODS (t))
830 error ("an anonymous union cannot have function members");
831
832 p = &pending_inlines;
833 for (; *p; *p = (*p)->next)
834 if (DECL_CONTEXT ((*p)->fndecl) != t)
835 break;
836 }
837 else if (TREE_CODE (t) == ENUMERAL_TYPE)
838 x = grok_enum_decls (t, NULL_TREE);
839 else
840 x = NULL_TREE;
841 return x;
842 break;
843
844 default:
845 if (t != void_type_node)
846 error ("empty component declaration");
847 return NULL_TREE;
848 }
849 }
850 else
851 {
852 t = TREE_TYPE (components);
853 if (TREE_CODE (t) == ENUMERAL_TYPE && TREE_NONLOCAL_FLAG (t))
854 return grok_enum_decls (t, components);
855 else
856 return components;
857 }
858 }
859
860 /* Classes overload their constituent function names automatically.
861 When a function name is declared in a record structure,
862 its name is changed to it overloaded name. Since names for
863 constructors and destructors can conflict, we place a leading
864 '$' for destructors.
865
866 CNAME is the name of the class we are grokking for.
867
868 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
869
870 FLAGS contains bits saying what's special about today's
871 arguments. 1 == DESTRUCTOR. 2 == OPERATOR.
872
873 If FUNCTION is a destructor, then we must add the `auto-delete' field
874 as a second parameter. There is some hair associated with the fact
875 that we must "declare" this variable in the manner consistent with the
876 way the rest of the arguments were declared.
877
878 QUALS are the qualifiers for the this pointer. */
879
880 void
881 grokclassfn (ctype, cname, function, flags, quals)
882 tree ctype, cname, function;
883 enum overload_flags flags;
884 tree quals;
885 {
886 tree fn_name = DECL_NAME (function);
887 tree arg_types;
888 tree parm;
889 tree qualtype;
890 tree fntype = TREE_TYPE (function);
891 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
892
893 if (fn_name == NULL_TREE)
894 {
895 error ("name missing for member function");
896 fn_name = get_identifier ("<anonymous>");
897 DECL_NAME (function) = fn_name;
898 }
899
900 if (quals)
901 qualtype = grok_method_quals (ctype, function, quals);
902 else
903 qualtype = ctype;
904
905 arg_types = TYPE_ARG_TYPES (TREE_TYPE (function));
906 if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
907 {
908 /* Must add the class instance variable up front. */
909 /* Right now we just make this a pointer. But later
910 we may wish to make it special. */
911 tree type = TREE_VALUE (arg_types);
912 int constp = 1;
913
914 if ((flag_this_is_variable > 0)
915 && (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function)))
916 constp = 0;
917
918 if (DECL_CONSTRUCTOR_P (function))
919 {
920 if (TYPE_USES_VIRTUAL_BASECLASSES (ctype))
921 {
922 DECL_CONSTRUCTOR_FOR_VBASE_P (function) = 1;
923 /* In this case we need "in-charge" flag saying whether
924 this constructor is responsible for initialization
925 of virtual baseclasses or not. */
926 parm = build_decl (PARM_DECL, in_charge_identifier, integer_type_node);
927 /* Mark the artificial `__in_chrg' parameter as "artificial". */
928 SET_DECL_ARTIFICIAL (parm);
929 DECL_ARG_TYPE (parm) = integer_type_node;
930 TREE_READONLY (parm) = 1;
931 TREE_CHAIN (parm) = last_function_parms;
932 last_function_parms = parm;
933 }
934 }
935
936 parm = build_decl (PARM_DECL, this_identifier, type);
937 /* Mark the artificial `this' parameter as "artificial". */
938 SET_DECL_ARTIFICIAL (parm);
939 DECL_ARG_TYPE (parm) = type;
940 /* We can make this a register, so long as we don't
941 accidentally complain if someone tries to take its address. */
942 DECL_REGISTER (parm) = 1;
943 if (constp)
944 TREE_READONLY (parm) = 1;
945 TREE_CHAIN (parm) = last_function_parms;
946 last_function_parms = parm;
947 }
948
949 if (flags == DTOR_FLAG)
950 {
951 char *buf, *dbuf;
952 tree const_integer_type = build_type_variant (integer_type_node, 1, 0);
953 int len = sizeof (DESTRUCTOR_DECL_PREFIX)-1;
954
955 arg_types = hash_tree_chain (const_integer_type, void_list_node);
956 TREE_SIDE_EFFECTS (arg_types) = 1;
957 /* Build the overload name. It will look like `7Example'. */
958 if (IDENTIFIER_TYPE_VALUE (cname))
959 dbuf = build_overload_name (IDENTIFIER_TYPE_VALUE (cname), 1, 1);
960 else if (IDENTIFIER_LOCAL_VALUE (cname))
961 dbuf = build_overload_name (TREE_TYPE (IDENTIFIER_LOCAL_VALUE (cname)), 1, 1);
962 else
963 /* Using ctype fixes the `X::Y::~Y()' crash. The cname has no type when
964 it's defined out of the class definition, since poplevel_class wipes
965 it out. This used to be internal error 346. */
966 dbuf = build_overload_name (ctype, 1, 1);
967 buf = (char *) alloca (strlen (dbuf) + sizeof (DESTRUCTOR_DECL_PREFIX));
968 bcopy (DESTRUCTOR_DECL_PREFIX, buf, len);
969 buf[len] = '\0';
970 strcat (buf, dbuf);
971 DECL_ASSEMBLER_NAME (function) = get_identifier (buf);
972 parm = build_decl (PARM_DECL, in_charge_identifier, integer_type_node);
973 /* Mark the artificial `__in_chrg' parameter as "artificial". */
974 SET_DECL_ARTIFICIAL (parm);
975 TREE_READONLY (parm) = 1;
976 DECL_ARG_TYPE (parm) = integer_type_node;
977 /* This is the same chain as DECL_ARGUMENTS (...). */
978 TREE_CHAIN (last_function_parms) = parm;
979
980 fntype = build_cplus_method_type (qualtype, void_type_node,
981 arg_types);
982 if (raises)
983 {
984 fntype = build_exception_variant (fntype, raises);
985 }
986 TREE_TYPE (function) = fntype;
987 TYPE_HAS_DESTRUCTOR (ctype) = 1;
988 }
989 else
990 {
991 tree these_arg_types;
992
993 if (DECL_CONSTRUCTOR_FOR_VBASE_P (function))
994 {
995 arg_types = hash_tree_chain (integer_type_node,
996 TREE_CHAIN (arg_types));
997 fntype = build_cplus_method_type (qualtype,
998 TREE_TYPE (TREE_TYPE (function)),
999 arg_types);
1000 if (raises)
1001 {
1002 fntype = build_exception_variant (fntype, raises);
1003 }
1004 TREE_TYPE (function) = fntype;
1005 arg_types = TYPE_ARG_TYPES (TREE_TYPE (function));
1006 }
1007
1008 these_arg_types = arg_types;
1009
1010 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
1011 /* Only true for static member functions. */
1012 these_arg_types = hash_tree_chain (build_pointer_type (qualtype),
1013 arg_types);
1014
1015 DECL_ASSEMBLER_NAME (function)
1016 = build_decl_overload (fn_name, these_arg_types,
1017 1 + DECL_CONSTRUCTOR_P (function));
1018
1019 #if 0
1020 /* This code is going into the compiler, but currently, it makes
1021 libg++/src/Integer.cc not compile. The problem is that the nice name
1022 winds up going into the symbol table, and conversion operations look
1023 for the manged name. */
1024 substitute_nice_name (function);
1025 #endif
1026 }
1027
1028 DECL_ARGUMENTS (function) = last_function_parms;
1029 /* First approximations. */
1030 DECL_CONTEXT (function) = ctype;
1031 DECL_CLASS_CONTEXT (function) = ctype;
1032 }
1033
1034 /* Work on the expr used by alignof (this is only called by the parser). */
1035
1036 tree
1037 grok_alignof (expr)
1038 tree expr;
1039 {
1040 tree best, t;
1041 int bestalign;
1042
1043 if (TREE_CODE (expr) == COMPONENT_REF
1044 && DECL_BIT_FIELD (TREE_OPERAND (expr, 1)))
1045 error ("`__alignof__' applied to a bit-field");
1046
1047 if (TREE_CODE (expr) == INDIRECT_REF)
1048 {
1049 best = t = TREE_OPERAND (expr, 0);
1050 bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
1051
1052 while (TREE_CODE (t) == NOP_EXPR
1053 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
1054 {
1055 int thisalign;
1056 t = TREE_OPERAND (t, 0);
1057 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
1058 if (thisalign > bestalign)
1059 best = t, bestalign = thisalign;
1060 }
1061 return c_alignof (TREE_TYPE (TREE_TYPE (best)));
1062 }
1063 else
1064 {
1065 /* ANSI says arrays and fns are converted inside comma.
1066 But we can't convert them in build_compound_expr
1067 because that would break commas in lvalues.
1068 So do the conversion here if operand was a comma. */
1069 if (TREE_CODE (expr) == COMPOUND_EXPR
1070 && (TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
1071 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE))
1072 expr = default_conversion (expr);
1073 return c_alignof (TREE_TYPE (expr));
1074 }
1075 }
1076
1077 /* Create an ARRAY_REF, checking for the user doing things backwards
1078 along the way. */
1079
1080 tree
1081 grok_array_decl (array_expr, index_exp)
1082 tree array_expr, index_exp;
1083 {
1084 tree type = TREE_TYPE (array_expr);
1085 tree p1, p2, i1, i2;
1086
1087 if (type == error_mark_node || index_exp == error_mark_node)
1088 return error_mark_node;
1089 if (processing_template_decl)
1090 return build_min (ARRAY_REF, type ? TREE_TYPE (type) : NULL_TREE,
1091 array_expr, index_exp);
1092
1093 if (type == NULL_TREE)
1094 {
1095 /* Something has gone very wrong. Assume we are mistakenly reducing
1096 an expression instead of a declaration. */
1097 error ("parser may be lost: is there a '{' missing somewhere?");
1098 return NULL_TREE;
1099 }
1100
1101 if (TREE_CODE (type) == OFFSET_TYPE
1102 || TREE_CODE (type) == REFERENCE_TYPE)
1103 type = TREE_TYPE (type);
1104
1105 /* If they have an `operator[]', use that. */
1106 if (TYPE_LANG_SPECIFIC (type)
1107 && TYPE_OVERLOADS_ARRAY_REF (complete_type (type)))
1108 return build_opfncall (ARRAY_REF, LOOKUP_NORMAL,
1109 array_expr, index_exp, NULL_TREE);
1110
1111 /* Otherwise, create an ARRAY_REF for a pointer or array type. */
1112
1113 if (TREE_CODE (type) == ARRAY_TYPE)
1114 p1 = array_expr;
1115 else
1116 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, 0);
1117
1118 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
1119 p2 = index_exp;
1120 else
1121 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, 0);
1122
1123 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr, 0);
1124 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp, 0);
1125
1126 if ((p1 && i2) && (i1 && p2))
1127 error ("ambiguous conversion for array subscript");
1128
1129 if (p1 && i2)
1130 array_expr = p1, index_exp = i2;
1131 else if (i1 && p2)
1132 array_expr = p2, index_exp = i1;
1133 else
1134 {
1135 cp_error ("invalid types `%T[%T]' for array subscript",
1136 type, TREE_TYPE (index_exp));
1137 return error_mark_node;
1138 }
1139
1140 if (array_expr == error_mark_node || index_exp == error_mark_node)
1141 error ("ambiguous conversion for array subscript");
1142
1143 return build_array_ref (array_expr, index_exp);
1144 }
1145
1146 /* Given the cast expression EXP, checking out its validity. Either return
1147 an error_mark_node if there was an unavoidable error, return a cast to
1148 void for trying to delete a pointer w/ the value 0, or return the
1149 call to delete. If DOING_VEC is 1, we handle things differently
1150 for doing an array delete. If DOING_VEC is 2, they gave us the
1151 array size as an argument to delete.
1152 Implements ARM $5.3.4. This is called from the parser. */
1153
1154 tree
1155 delete_sanity (exp, size, doing_vec, use_global_delete)
1156 tree exp, size;
1157 int doing_vec, use_global_delete;
1158 {
1159 tree t;
1160 tree type;
1161 enum tree_code code;
1162 /* For a regular vector delete (aka, no size argument) we will pass
1163 this down as a NULL_TREE into build_vec_delete. */
1164 tree maxindex = NULL_TREE;
1165
1166 if (exp == error_mark_node)
1167 return exp;
1168
1169 if (processing_template_decl)
1170 {
1171 t = build_min (DELETE_EXPR, void_type_node, exp, size);
1172 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
1173 DELETE_EXPR_USE_VEC (t) = doing_vec;
1174 return t;
1175 }
1176
1177 t = stabilize_reference (convert_from_reference (exp));
1178 type = TREE_TYPE (t);
1179 code = TREE_CODE (type);
1180
1181 switch (doing_vec)
1182 {
1183 case 2:
1184 maxindex = build_binary_op (MINUS_EXPR, size, integer_one_node, 1);
1185 pedwarn ("anachronistic use of array size in vector delete");
1186 /* Fall through. */
1187 case 1:
1188 break;
1189 default:
1190 if (code != POINTER_TYPE)
1191 {
1192 cp_error ("type `%#T' argument given to `delete', expected pointer",
1193 type);
1194 return error_mark_node;
1195 }
1196
1197 /* Deleting a pointer with the value zero is valid and has no effect. */
1198 if (integer_zerop (t))
1199 return build1 (NOP_EXPR, void_type_node, t);
1200 }
1201
1202 if (code == POINTER_TYPE)
1203 {
1204 #if 0
1205 /* As of Valley Forge, you can delete a pointer to constant. */
1206 /* You can't delete a pointer to constant. */
1207 if (TREE_READONLY (TREE_TYPE (type)))
1208 {
1209 error ("`const *' cannot be deleted");
1210 return error_mark_node;
1211 }
1212 #endif
1213 /* You also can't delete functions. */
1214 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
1215 {
1216 error ("cannot delete a function");
1217 return error_mark_node;
1218 }
1219 }
1220
1221 #if 0
1222 /* If the type has no destructor, then we should build a regular
1223 delete, instead of a vector delete. Otherwise, we would end
1224 up passing a bogus offset into __builtin_delete, which is
1225 not expecting it. */
1226 if (doing_vec
1227 && TREE_CODE (type) == POINTER_TYPE
1228 && !TYPE_HAS_DESTRUCTOR (TREE_TYPE (type)))
1229 {
1230 doing_vec = 0;
1231 use_global_delete = 1;
1232 }
1233 #endif
1234
1235 if (doing_vec)
1236 return build_vec_delete (t, maxindex, integer_one_node,
1237 integer_two_node, use_global_delete);
1238 else
1239 {
1240 if (IS_AGGR_TYPE (TREE_TYPE (type))
1241 && TYPE_GETS_REG_DELETE (TREE_TYPE (type)))
1242 {
1243 /* Only do access checking here; we'll be calling op delete
1244 from the destructor. */
1245 tree tmp = build_opfncall (DELETE_EXPR, LOOKUP_NORMAL, t,
1246 size_zero_node, NULL_TREE);
1247 if (tmp == error_mark_node)
1248 return error_mark_node;
1249 }
1250
1251 return build_delete (type, t, integer_three_node,
1252 LOOKUP_NORMAL, use_global_delete);
1253 }
1254 }
1255
1256 /* Sanity check: report error if this function FUNCTION is not
1257 really a member of the class (CTYPE) it is supposed to belong to.
1258 CNAME is the same here as it is for grokclassfn above. */
1259
1260 tree
1261 check_classfn (ctype, function)
1262 tree ctype, function;
1263 {
1264 tree fn_name = DECL_NAME (function);
1265 tree fndecl;
1266 tree method_vec = CLASSTYPE_METHOD_VEC (complete_type (ctype));
1267 tree *methods = 0;
1268 tree *end = 0;
1269
1270 if (method_vec != 0)
1271 {
1272 methods = &TREE_VEC_ELT (method_vec, 0);
1273 end = TREE_VEC_END (method_vec);
1274
1275 /* First suss out ctors and dtors. */
1276 if (*methods && fn_name == DECL_NAME (*methods)
1277 && DECL_CONSTRUCTOR_P (function))
1278 goto got_it;
1279 if (*++methods && fn_name == DECL_NAME (*methods)
1280 && DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (function)))
1281 goto got_it;
1282
1283 while (++methods != end)
1284 {
1285 if (fn_name == DECL_NAME (*methods))
1286 {
1287 got_it:
1288 fndecl = *methods;
1289 while (fndecl)
1290 {
1291 if (DECL_ASSEMBLER_NAME (function) == DECL_ASSEMBLER_NAME (fndecl))
1292 return fndecl;
1293 #if 0
1294 /* This doesn't work for static member functions that are
1295 pretending to be methods. */
1296 /* We have to do more extensive argument checking here, as
1297 the name may have been changed by asm("new_name"). */
1298 if (decls_match (function, fndecl))
1299 return fndecl;
1300 #else
1301 if (DECL_NAME (function) == DECL_NAME (fndecl))
1302 {
1303 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
1304 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1305
1306 /* Get rid of the this parameter on functions that become
1307 static. */
1308 if (DECL_STATIC_FUNCTION_P (fndecl)
1309 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
1310 p1 = TREE_CHAIN (p1);
1311
1312 if (comptypes (TREE_TYPE (TREE_TYPE (function)),
1313 TREE_TYPE (TREE_TYPE (fndecl)), 1)
1314 && compparms (p1, p2, 3))
1315 return fndecl;
1316 }
1317 #endif
1318 fndecl = DECL_CHAIN (fndecl);
1319 }
1320 break; /* loser */
1321 }
1322 }
1323 }
1324
1325 if (methods != end)
1326 {
1327 tree fndecl = *methods;
1328 cp_error ("prototype for `%#D' does not match any in class `%T'",
1329 function, ctype);
1330 cp_error_at ("candidate%s: %+#D", DECL_CHAIN (fndecl) ? "s are" : " is",
1331 fndecl);
1332 while (fndecl = DECL_CHAIN (fndecl), fndecl)
1333 cp_error_at (" %#D", fndecl);
1334 }
1335 else
1336 {
1337 methods = 0;
1338 cp_error ("no `%#D' member function declared in class `%T'",
1339 function, ctype);
1340 }
1341
1342 /* If we did not find the method in the class, add it to avoid
1343 spurious errors. */
1344 add_method (ctype, methods, function);
1345 return NULL_TREE;
1346 }
1347
1348 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
1349 of a structure component, returning a FIELD_DECL node.
1350 QUALS is a list of type qualifiers for this decl (such as for declaring
1351 const member functions).
1352
1353 This is done during the parsing of the struct declaration.
1354 The FIELD_DECL nodes are chained together and the lot of them
1355 are ultimately passed to `build_struct' to make the RECORD_TYPE node.
1356
1357 C++:
1358
1359 If class A defines that certain functions in class B are friends, then
1360 the way I have set things up, it is B who is interested in permission
1361 granted by A. However, it is in A's context that these declarations
1362 are parsed. By returning a void_type_node, class A does not attempt
1363 to incorporate the declarations of the friends within its structure.
1364
1365 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
1366 CHANGES TO CODE IN `start_method'. */
1367
1368 tree
1369 grokfield (declarator, declspecs, init, asmspec_tree, attrlist)
1370 tree declarator, declspecs, init, asmspec_tree, attrlist;
1371 {
1372 register tree value;
1373 char *asmspec = 0;
1374 int flags = LOOKUP_ONLYCONVERTING;
1375
1376 /* Convert () initializers to = initializers. */
1377 if (init == NULL_TREE && declarator != NULL_TREE
1378 && TREE_CODE (declarator) == CALL_EXPR
1379 && TREE_OPERAND (declarator, 0)
1380 && (TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE
1381 || TREE_CODE (TREE_OPERAND (declarator, 0)) == SCOPE_REF)
1382 && parmlist_is_exprlist (TREE_OPERAND (declarator, 1)))
1383 {
1384 init = TREE_OPERAND (declarator, 1);
1385 declarator = TREE_OPERAND (declarator, 0);
1386 flags = 0;
1387 }
1388
1389 if (declspecs == NULL_TREE
1390 && TREE_CODE (declarator) == SCOPE_REF
1391 && TREE_CODE (TREE_OPERAND (declarator, 1)) == IDENTIFIER_NODE)
1392 {
1393 /* Access declaration */
1394 if (! IS_AGGR_TYPE_CODE (TREE_CODE (TREE_OPERAND (declarator, 0))))
1395 ;
1396 else if (TREE_COMPLEXITY (declarator) == current_class_depth)
1397 pop_nested_class (1);
1398 return do_class_using_decl (declarator);
1399 }
1400
1401 if (init
1402 && TREE_CODE (init) == TREE_LIST
1403 && TREE_VALUE (init) == error_mark_node
1404 && TREE_CHAIN (init) == NULL_TREE)
1405 init = NULL_TREE;
1406
1407 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, NULL_TREE);
1408 if (! value)
1409 return value; /* friend or constructor went bad. */
1410
1411 /* Pass friendly classes back. */
1412 if (TREE_CODE (value) == VOID_TYPE)
1413 return void_type_node;
1414
1415 if (DECL_NAME (value) != NULL_TREE
1416 && IDENTIFIER_POINTER (DECL_NAME (value))[0] == '_'
1417 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (value)), "_vptr"))
1418 cp_error ("member `%D' conflicts with virtual function table field name", value);
1419
1420 /* Stash away type declarations. */
1421 if (TREE_CODE (value) == TYPE_DECL)
1422 {
1423 DECL_NONLOCAL (value) = 1;
1424 DECL_CONTEXT (value) = current_class_type;
1425 DECL_CLASS_CONTEXT (value) = current_class_type;
1426 CLASSTYPE_LOCAL_TYPEDECLS (current_class_type) = 1;
1427
1428 pushdecl_class_level (value);
1429 return value;
1430 }
1431
1432 if (IS_SIGNATURE (current_class_type)
1433 && TREE_CODE (value) != FUNCTION_DECL)
1434 {
1435 error ("field declaration not allowed in signature");
1436 return void_type_node;
1437 }
1438
1439 if (DECL_IN_AGGR_P (value))
1440 {
1441 cp_error ("`%D' is already defined in the class %T", value,
1442 DECL_CONTEXT (value));
1443 return void_type_node;
1444 }
1445
1446 if (asmspec_tree)
1447 asmspec = TREE_STRING_POINTER (asmspec_tree);
1448
1449 if (init)
1450 {
1451 if (IS_SIGNATURE (current_class_type)
1452 && TREE_CODE (value) == FUNCTION_DECL)
1453 {
1454 error ("function declarations cannot have initializers in signature");
1455 init = NULL_TREE;
1456 }
1457 else if (TREE_CODE (value) == FUNCTION_DECL)
1458 {
1459 grok_function_init (value, init);
1460 init = NULL_TREE;
1461 }
1462 else if (pedantic && TREE_CODE (value) != VAR_DECL)
1463 /* Already complained in grokdeclarator. */
1464 init = NULL_TREE;
1465 else
1466 {
1467 /* We allow initializers to become parameters to base
1468 initializers. */
1469 if (TREE_CODE (init) == TREE_LIST)
1470 {
1471 if (TREE_CHAIN (init) == NULL_TREE)
1472 init = TREE_VALUE (init);
1473 else
1474 init = digest_init (TREE_TYPE (value), init, (tree *)0);
1475 }
1476
1477 if (TREE_CODE (init) == CONST_DECL)
1478 init = DECL_INITIAL (init);
1479 else if (TREE_READONLY_DECL_P (init))
1480 init = decl_constant_value (init);
1481 else if (TREE_CODE (init) == CONSTRUCTOR)
1482 init = digest_init (TREE_TYPE (value), init, (tree *)0);
1483 my_friendly_assert (TREE_PERMANENT (init), 192);
1484 if (init == error_mark_node)
1485 /* We must make this look different than `error_mark_node'
1486 because `decl_const_value' would mis-interpret it
1487 as only meaning that this VAR_DECL is defined. */
1488 init = build1 (NOP_EXPR, TREE_TYPE (value), init);
1489 else if (processing_template_decl)
1490 ;
1491 else if (! TREE_CONSTANT (init))
1492 {
1493 /* We can allow references to things that are effectively
1494 static, since references are initialized with the address. */
1495 if (TREE_CODE (TREE_TYPE (value)) != REFERENCE_TYPE
1496 || (TREE_STATIC (init) == 0
1497 && (TREE_CODE_CLASS (TREE_CODE (init)) != 'd'
1498 || DECL_EXTERNAL (init) == 0)))
1499 {
1500 error ("field initializer is not constant");
1501 init = error_mark_node;
1502 }
1503 }
1504 }
1505 }
1506
1507 /* The corresponding pop_obstacks is in cp_finish_decl. */
1508 push_obstacks_nochange ();
1509
1510 if (processing_template_decl && ! current_function_decl
1511 && (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == FUNCTION_DECL))
1512 push_template_decl (value);
1513
1514 if (attrlist)
1515 cplus_decl_attributes (value, TREE_PURPOSE (attrlist),
1516 TREE_VALUE (attrlist));
1517
1518 if (TREE_CODE (value) == VAR_DECL)
1519 {
1520 /* We cannot call pushdecl here, because that would
1521 fill in the value of our TREE_CHAIN. Instead, we
1522 modify cp_finish_decl to do the right thing, namely, to
1523 put this decl out straight away. */
1524 if (TREE_PUBLIC (value))
1525 {
1526 /* current_class_type can be NULL_TREE in case of error. */
1527 if (asmspec == 0 && current_class_type)
1528 {
1529 TREE_PUBLIC (value) = 1;
1530 DECL_INITIAL (value) = error_mark_node;
1531 DECL_ASSEMBLER_NAME (value)
1532 = build_static_name (current_class_type, DECL_NAME (value));
1533 }
1534 if (! processing_template_decl)
1535 pending_statics = perm_tree_cons (NULL_TREE, value, pending_statics);
1536
1537 /* Static consts need not be initialized in the class definition. */
1538 if (init != NULL_TREE && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (value)))
1539 {
1540 static int explanation = 0;
1541
1542 error ("initializer invalid for static member with constructor");
1543 if (explanation++ == 0)
1544 error ("(you really want to initialize it separately)");
1545 init = 0;
1546 }
1547 /* Force the compiler to know when an uninitialized static
1548 const member is being used. */
1549 if (TYPE_READONLY (value) && init == 0)
1550 TREE_USED (value) = 1;
1551 }
1552 DECL_INITIAL (value) = init;
1553 DECL_IN_AGGR_P (value) = 1;
1554 DECL_CONTEXT (value) = current_class_type;
1555 DECL_CLASS_CONTEXT (value) = current_class_type;
1556
1557 cp_finish_decl (value, init, asmspec_tree, 1, flags);
1558 pushdecl_class_level (value);
1559 return value;
1560 }
1561 if (TREE_CODE (value) == FIELD_DECL)
1562 {
1563 if (asmspec)
1564 {
1565 /* This must override the asm specifier which was placed
1566 by grokclassfn. Lay this out fresh. */
1567 DECL_RTL (value) = NULL_RTX;
1568 DECL_ASSEMBLER_NAME (value) = get_identifier (asmspec);
1569 }
1570 if (DECL_INITIAL (value) == error_mark_node)
1571 init = error_mark_node;
1572 cp_finish_decl (value, init, asmspec_tree, 1, flags);
1573 DECL_INITIAL (value) = init;
1574 DECL_IN_AGGR_P (value) = 1;
1575 return value;
1576 }
1577 if (TREE_CODE (value) == FUNCTION_DECL)
1578 {
1579 check_default_args (value);
1580 if (DECL_CHAIN (value) != NULL_TREE)
1581 {
1582 /* Need a fresh node here so that we don't get circularity
1583 when we link these together. */
1584 value = copy_node (value);
1585 /* When does this happen? */
1586 my_friendly_assert (init == NULL_TREE, 193);
1587 }
1588 if (asmspec)
1589 {
1590 /* This must override the asm specifier which was placed
1591 by grokclassfn. Lay this out fresh. */
1592 DECL_RTL (value) = NULL_RTX;
1593 DECL_ASSEMBLER_NAME (value) = get_identifier (asmspec);
1594 }
1595 cp_finish_decl (value, init, asmspec_tree, 1, flags);
1596
1597 /* Pass friends back this way. */
1598 if (DECL_FRIEND_P (value))
1599 return void_type_node;
1600
1601 #if 0 /* Just because a fn is declared doesn't mean we'll try to define it. */
1602 if (current_function_decl && ! IS_SIGNATURE (current_class_type))
1603 cp_error ("method `%#D' of local class must be defined in class body",
1604 value);
1605 #endif
1606
1607 DECL_IN_AGGR_P (value) = 1;
1608 return value;
1609 }
1610 my_friendly_abort (21);
1611 /* NOTREACHED */
1612 return NULL_TREE;
1613 }
1614
1615 /* Like `grokfield', but for bitfields.
1616 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1617
1618 tree
1619 grokbitfield (declarator, declspecs, width)
1620 tree declarator, declspecs, width;
1621 {
1622 register tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1623 0, NULL_TREE);
1624
1625 if (! value) return NULL_TREE; /* friends went bad. */
1626
1627 /* Pass friendly classes back. */
1628 if (TREE_CODE (value) == VOID_TYPE)
1629 return void_type_node;
1630
1631 if (TREE_CODE (value) == TYPE_DECL)
1632 {
1633 cp_error ("cannot declare `%D' to be a bitfield type", value);
1634 return NULL_TREE;
1635 }
1636
1637 if (IS_SIGNATURE (current_class_type))
1638 {
1639 error ("field declaration not allowed in signature");
1640 return void_type_node;
1641 }
1642
1643 if (DECL_IN_AGGR_P (value))
1644 {
1645 cp_error ("`%D' is already defined in the class %T", value,
1646 DECL_CONTEXT (value));
1647 return void_type_node;
1648 }
1649
1650 GNU_xref_member (current_class_name, value);
1651
1652 if (TREE_STATIC (value))
1653 {
1654 cp_error ("static member `%D' cannot be a bitfield", value);
1655 return NULL_TREE;
1656 }
1657 cp_finish_decl (value, NULL_TREE, NULL_TREE, 0, 0);
1658
1659 if (width != error_mark_node)
1660 {
1661 constant_expression_warning (width);
1662 DECL_INITIAL (value) = width;
1663 DECL_BIT_FIELD (value) = 1;
1664 }
1665
1666 DECL_IN_AGGR_P (value) = 1;
1667 return value;
1668 }
1669
1670 tree
1671 grokoptypename (declspecs, declarator)
1672 tree declspecs, declarator;
1673 {
1674 tree t = grokdeclarator (declarator, declspecs, TYPENAME, 0, NULL_TREE);
1675 return build_typename_overload (t);
1676 }
1677
1678 /* When a function is declared with an initializer,
1679 do the right thing. Currently, there are two possibilities:
1680
1681 class B
1682 {
1683 public:
1684 // initialization possibility #1.
1685 virtual void f () = 0;
1686 int g ();
1687 };
1688
1689 class D1 : B
1690 {
1691 public:
1692 int d1;
1693 // error, no f ();
1694 };
1695
1696 class D2 : B
1697 {
1698 public:
1699 int d2;
1700 void f ();
1701 };
1702
1703 class D3 : B
1704 {
1705 public:
1706 int d3;
1707 // initialization possibility #2
1708 void f () = B::f;
1709 };
1710
1711 */
1712
1713 int
1714 copy_assignment_arg_p (parmtype, virtualp)
1715 tree parmtype;
1716 int virtualp;
1717 {
1718 if (current_class_type == NULL_TREE)
1719 return 0;
1720
1721 if (TREE_CODE (parmtype) == REFERENCE_TYPE)
1722 parmtype = TREE_TYPE (parmtype);
1723
1724 if ((TYPE_MAIN_VARIANT (parmtype) == current_class_type)
1725 #if 0
1726 /* Non-standard hack to support old Booch components. */
1727 || (! virtualp && DERIVED_FROM_P (parmtype, current_class_type))
1728 #endif
1729 )
1730 return 1;
1731
1732 return 0;
1733 }
1734
1735 static void
1736 grok_function_init (decl, init)
1737 tree decl;
1738 tree init;
1739 {
1740 /* An initializer for a function tells how this function should
1741 be inherited. */
1742 tree type = TREE_TYPE (decl);
1743
1744 if (TREE_CODE (type) == FUNCTION_TYPE)
1745 cp_error ("initializer specified for non-member function `%D'", decl);
1746 #if 0
1747 /* We'll check for this in finish_struct_1. */
1748 else if (DECL_VINDEX (decl) == NULL_TREE)
1749 cp_error ("initializer specified for non-virtual method `%D'", decl);
1750 #endif
1751 else if (integer_zerop (init))
1752 {
1753 #if 0
1754 /* Mark this function as being "defined". */
1755 DECL_INITIAL (decl) = error_mark_node;
1756 /* pure virtual destructors must be defined. */
1757 /* pure virtual needs to be defined (as abort) only when put in
1758 vtbl. For wellformed call, it should be itself. pr4737 */
1759 if (!DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)))
1760 {
1761 extern tree abort_fndecl;
1762 /* Give this node rtl from `abort'. */
1763 DECL_RTL (decl) = DECL_RTL (abort_fndecl);
1764 }
1765 #endif
1766 DECL_ABSTRACT_VIRTUAL_P (decl) = 1;
1767 if (DECL_NAME (decl) == ansi_opname [(int) MODIFY_EXPR])
1768 {
1769 tree parmtype
1770 = TREE_VALUE (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl))));
1771
1772 if (copy_assignment_arg_p (parmtype, 1))
1773 TYPE_HAS_ABSTRACT_ASSIGN_REF (current_class_type) = 1;
1774 }
1775 }
1776 else if (TREE_CODE (init) == OFFSET_REF
1777 && TREE_OPERAND (init, 0) == NULL_TREE
1778 && TREE_CODE (TREE_TYPE (init)) == METHOD_TYPE)
1779 {
1780 tree basetype = DECL_CLASS_CONTEXT (init);
1781 tree basefn = TREE_OPERAND (init, 1);
1782 if (TREE_CODE (basefn) != FUNCTION_DECL)
1783 cp_error ("non-method initializer invalid for method `%D'", decl);
1784 else if (! BINFO_OFFSET_ZEROP (TYPE_BINFO (DECL_CLASS_CONTEXT (basefn))))
1785 sorry ("base member function from other than first base class");
1786 else
1787 {
1788 tree binfo = get_binfo (basetype, TYPE_METHOD_BASETYPE (type), 1);
1789 if (binfo == error_mark_node)
1790 ;
1791 else if (binfo == 0)
1792 error_not_base_type (TYPE_METHOD_BASETYPE (TREE_TYPE (init)),
1793 TYPE_METHOD_BASETYPE (type));
1794 else
1795 {
1796 /* Mark this function as being defined,
1797 and give it new rtl. */
1798 DECL_INITIAL (decl) = error_mark_node;
1799 DECL_RTL (decl) = DECL_RTL (basefn);
1800 }
1801 }
1802 }
1803 else
1804 cp_error ("invalid initializer for virtual method `%D'", decl);
1805 }
1806 \f
1807 void
1808 cplus_decl_attributes (decl, attributes, prefix_attributes)
1809 tree decl, attributes, prefix_attributes;
1810 {
1811 if (decl == NULL_TREE || decl == void_type_node)
1812 return;
1813
1814 if (TREE_CODE (decl) == TEMPLATE_DECL)
1815 decl = DECL_TEMPLATE_RESULT (decl);
1816
1817 decl_attributes (decl, attributes, prefix_attributes);
1818
1819 if (TREE_CODE (decl) == TYPE_DECL)
1820 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (decl), TREE_TYPE (decl));
1821 }
1822 \f
1823 /* CONSTRUCTOR_NAME:
1824 Return the name for the constructor (or destructor) for the
1825 specified class. Argument can be RECORD_TYPE, TYPE_DECL, or
1826 IDENTIFIER_NODE. When given a template, this routine doesn't
1827 lose the specialization. */
1828
1829 tree
1830 constructor_name_full (thing)
1831 tree thing;
1832 {
1833 if (TREE_CODE (thing) == TEMPLATE_TYPE_PARM)
1834 thing = TYPE_NAME (thing);
1835 else if (IS_AGGR_TYPE_CODE (TREE_CODE (thing)))
1836 {
1837 if (TYPE_WAS_ANONYMOUS (thing) && TYPE_HAS_CONSTRUCTOR (thing))
1838 thing = DECL_NAME (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (thing), 0));
1839 else
1840 thing = TYPE_NAME (thing);
1841 }
1842 if (TREE_CODE (thing) == TYPE_DECL
1843 || (TREE_CODE (thing) == TEMPLATE_DECL
1844 && TREE_CODE (DECL_TEMPLATE_RESULT (thing)) == TYPE_DECL))
1845 thing = DECL_NAME (thing);
1846 my_friendly_assert (TREE_CODE (thing) == IDENTIFIER_NODE, 197);
1847 return thing;
1848 }
1849
1850 /* CONSTRUCTOR_NAME:
1851 Return the name for the constructor (or destructor) for the
1852 specified class. Argument can be RECORD_TYPE, TYPE_DECL, or
1853 IDENTIFIER_NODE. When given a template, return the plain
1854 unspecialized name. */
1855
1856 tree
1857 constructor_name (thing)
1858 tree thing;
1859 {
1860 tree t;
1861 thing = constructor_name_full (thing);
1862 t = IDENTIFIER_TEMPLATE (thing);
1863 if (!t)
1864 return thing;
1865 return t;
1866 }
1867 \f
1868 /* Cache the value of this class's main virtual function table pointer
1869 in a register variable. This will save one indirection if a
1870 more than one virtual function call is made this function. */
1871
1872 void
1873 setup_vtbl_ptr ()
1874 {
1875 extern tree base_init_expr;
1876
1877 if (base_init_expr == 0
1878 && DECL_CONSTRUCTOR_P (current_function_decl))
1879 {
1880 if (processing_template_decl)
1881 add_tree (build_min_nt
1882 (CTOR_INITIALIZER,
1883 current_member_init_list, current_base_init_list));
1884 else
1885 emit_base_init (current_class_type, 0);
1886 }
1887 }
1888
1889 /* Record the existence of an addressable inline function. */
1890
1891 void
1892 mark_inline_for_output (decl)
1893 tree decl;
1894 {
1895 decl = DECL_MAIN_VARIANT (decl);
1896 if (DECL_SAVED_INLINE (decl))
1897 return;
1898 my_friendly_assert (TREE_PERMANENT (decl), 363);
1899 DECL_SAVED_INLINE (decl) = 1;
1900 #if 0
1901 if (DECL_PENDING_INLINE_INFO (decl) != 0
1902 && ! DECL_PENDING_INLINE_INFO (decl)->deja_vu)
1903 {
1904 struct pending_inline *t = pending_inlines;
1905 my_friendly_assert (DECL_SAVED_INSNS (decl) == 0, 198);
1906 while (t)
1907 {
1908 if (t == DECL_PENDING_INLINE_INFO (decl))
1909 break;
1910 t = t->next;
1911 }
1912 if (t == 0)
1913 {
1914 t = DECL_PENDING_INLINE_INFO (decl);
1915 t->next = pending_inlines;
1916 pending_inlines = t;
1917 }
1918 DECL_PENDING_INLINE_INFO (decl) = 0;
1919 }
1920 #endif
1921 saved_inlines = perm_tree_cons (NULL_TREE, decl, saved_inlines);
1922 }
1923
1924 void
1925 clear_temp_name ()
1926 {
1927 temp_name_counter = 0;
1928 }
1929
1930 /* Hand off a unique name which can be used for variable we don't really
1931 want to know about anyway, for example, the anonymous variables which
1932 are needed to make references work. Declare this thing so we can use it.
1933 The variable created will be of type TYPE.
1934
1935 STATICP is nonzero if this variable should be static. */
1936
1937 tree
1938 get_temp_name (type, staticp)
1939 tree type;
1940 int staticp;
1941 {
1942 char buf[sizeof (AUTO_TEMP_FORMAT) + 20];
1943 tree decl;
1944 int toplev = toplevel_bindings_p ();
1945
1946 push_obstacks_nochange ();
1947 if (toplev || staticp)
1948 {
1949 end_temporary_allocation ();
1950 sprintf (buf, AUTO_TEMP_FORMAT, global_temp_name_counter++);
1951 decl = pushdecl_top_level (build_decl (VAR_DECL, get_identifier (buf), type));
1952 }
1953 else
1954 {
1955 sprintf (buf, AUTO_TEMP_FORMAT, temp_name_counter++);
1956 decl = pushdecl (build_decl (VAR_DECL, get_identifier (buf), type));
1957 }
1958 TREE_USED (decl) = 1;
1959 TREE_STATIC (decl) = staticp;
1960 DECL_ARTIFICIAL (decl) = 1;
1961
1962 /* If this is a local variable, then lay out its rtl now.
1963 Otherwise, callers of this function are responsible for dealing
1964 with this variable's rtl. */
1965 if (! toplev)
1966 {
1967 expand_decl (decl);
1968 expand_decl_init (decl);
1969 }
1970 pop_obstacks ();
1971
1972 return decl;
1973 }
1974
1975 /* Get a variable which we can use for multiple assignments.
1976 It is not entered into current_binding_level, because
1977 that breaks things when it comes time to do final cleanups
1978 (which take place "outside" the binding contour of the function). */
1979
1980 tree
1981 get_temp_regvar (type, init)
1982 tree type, init;
1983 {
1984 static char buf[sizeof (AUTO_TEMP_FORMAT) + 20] = { '_' };
1985 tree decl;
1986
1987 sprintf (buf+1, AUTO_TEMP_FORMAT, temp_name_counter++);
1988 decl = build_decl (VAR_DECL, get_identifier (buf), type);
1989 TREE_USED (decl) = 1;
1990 DECL_REGISTER (decl) = 1;
1991
1992 if (init)
1993 store_init_value (decl, init);
1994
1995 /* We can expand these without fear, since they cannot need
1996 constructors or destructors. */
1997 expand_decl (decl);
1998 expand_decl_init (decl);
1999
2000 return decl;
2001 }
2002
2003 /* Finish off the processing of a UNION_TYPE structure.
2004 If there are static members, then all members are
2005 static, and must be laid out together. If the
2006 union is an anonymous union, we arrange for that
2007 as well. PUBLIC_P is nonzero if this union is
2008 not declared static. */
2009
2010 void
2011 finish_anon_union (anon_union_decl)
2012 tree anon_union_decl;
2013 {
2014 tree type = TREE_TYPE (anon_union_decl);
2015 tree field, main_decl = NULL_TREE;
2016 tree elems = NULL_TREE;
2017 int public_p = TREE_PUBLIC (anon_union_decl);
2018 int static_p = TREE_STATIC (anon_union_decl);
2019 int external_p = DECL_EXTERNAL (anon_union_decl);
2020
2021 if ((field = TYPE_FIELDS (type)) == NULL_TREE)
2022 return;
2023
2024 if (public_p)
2025 {
2026 error ("global anonymous unions must be declared static");
2027 return;
2028 }
2029
2030 for (; field; field = TREE_CHAIN (field))
2031 {
2032 tree decl;
2033 if (TREE_CODE (field) != FIELD_DECL)
2034 continue;
2035
2036 if (TREE_PRIVATE (field))
2037 cp_pedwarn_at ("private member `%#D' in anonymous union", field);
2038 else if (TREE_PROTECTED (field))
2039 cp_pedwarn_at ("protected member `%#D' in anonymous union", field);
2040
2041 decl = build_decl (VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
2042 /* tell `pushdecl' that this is not tentative. */
2043 DECL_INITIAL (decl) = error_mark_node;
2044 TREE_PUBLIC (decl) = public_p;
2045 TREE_STATIC (decl) = static_p;
2046 DECL_EXTERNAL (decl) = external_p;
2047 decl = pushdecl (decl);
2048
2049 /* Only write out one anon union element--choose the one that
2050 can hold them all. */
2051 if (main_decl == NULL_TREE
2052 && 1 == simple_cst_equal (DECL_SIZE (decl),
2053 DECL_SIZE (anon_union_decl)))
2054 {
2055 main_decl = decl;
2056 }
2057 else
2058 {
2059 /* ??? This causes there to be no debug info written out
2060 about this decl. */
2061 TREE_ASM_WRITTEN (decl) = 1;
2062 }
2063
2064 DECL_INITIAL (decl) = NULL_TREE;
2065 /* If there's a cleanup to do, it belongs in the
2066 TREE_PURPOSE of the following TREE_LIST. */
2067 elems = tree_cons (NULL_TREE, decl, elems);
2068 TREE_TYPE (elems) = type;
2069 }
2070 if (static_p)
2071 {
2072 if (main_decl)
2073 {
2074 make_decl_rtl (main_decl, 0, toplevel_bindings_p ());
2075 DECL_RTL (anon_union_decl) = DECL_RTL (main_decl);
2076 }
2077 else
2078 {
2079 warning ("anonymous union with no members");
2080 return;
2081 }
2082 }
2083
2084 /* The following call assumes that there are never any cleanups
2085 for anonymous unions--a reasonable assumption. */
2086 expand_anon_union_decl (anon_union_decl, NULL_TREE, elems);
2087 }
2088
2089 /* Finish and output a table which is generated by the compiler.
2090 NAME is the name to give the table.
2091 TYPE is the type of the table entry.
2092 INIT is all the elements in the table.
2093 PUBLICP is non-zero if this table should be given external access. */
2094
2095 tree
2096 finish_table (name, type, init, publicp)
2097 tree name, type, init;
2098 int publicp;
2099 {
2100 tree itype, atype, decl;
2101 static tree empty_table;
2102 int is_empty = 0;
2103 tree asmspec;
2104
2105 itype = build_index_type (size_int (list_length (init) - 1));
2106 atype = build_cplus_array_type (type, itype);
2107 layout_type (atype);
2108
2109 if (TREE_VALUE (init) == integer_zero_node
2110 && TREE_CHAIN (init) == NULL_TREE)
2111 {
2112 #if 0
2113 if (empty_table == NULL_TREE)
2114 #endif
2115 {
2116 empty_table = get_temp_name (atype, 1);
2117 init = build (CONSTRUCTOR, atype, NULL_TREE, init);
2118 TREE_CONSTANT (init) = 1;
2119 TREE_STATIC (init) = 1;
2120 DECL_INITIAL (empty_table) = init;
2121 asmspec = build_string (IDENTIFIER_LENGTH (DECL_NAME (empty_table)),
2122 IDENTIFIER_POINTER (DECL_NAME (empty_table)));
2123 cp_finish_decl (empty_table, NULL_TREE, asmspec, 0, 0);
2124 }
2125 is_empty = 1;
2126 }
2127
2128 if (name == NULL_TREE)
2129 {
2130 if (is_empty)
2131 return empty_table;
2132 decl = get_temp_name (atype, 1);
2133 }
2134 else
2135 {
2136 decl = build_decl (VAR_DECL, name, atype);
2137 decl = pushdecl (decl);
2138 TREE_STATIC (decl) = 1;
2139 }
2140
2141 if (is_empty == 0)
2142 {
2143 TREE_PUBLIC (decl) = publicp;
2144 init = build (CONSTRUCTOR, atype, NULL_TREE, init);
2145 TREE_CONSTANT (init) = 1;
2146 TREE_STATIC (init) = 1;
2147 DECL_INITIAL (decl) = init;
2148 asmspec = build_string (IDENTIFIER_LENGTH (DECL_NAME (decl)),
2149 IDENTIFIER_POINTER (DECL_NAME (decl)));
2150 }
2151 else
2152 {
2153 /* This will cause DECL to point to EMPTY_TABLE in rtl-land. */
2154 DECL_EXTERNAL (decl) = 1;
2155 TREE_STATIC (decl) = 0;
2156 init = 0;
2157 asmspec = build_string (IDENTIFIER_LENGTH (DECL_NAME (empty_table)),
2158 IDENTIFIER_POINTER (DECL_NAME (empty_table)));
2159 }
2160
2161 cp_finish_decl (decl, NULL_TREE, asmspec, 0, 0);
2162 return decl;
2163 }
2164
2165 /* Finish processing a builtin type TYPE. It's name is NAME,
2166 its fields are in the array FIELDS. LEN is the number of elements
2167 in FIELDS minus one, or put another way, it is the maximum subscript
2168 used in FIELDS.
2169
2170 It is given the same alignment as ALIGN_TYPE. */
2171
2172 void
2173 finish_builtin_type (type, name, fields, len, align_type)
2174 tree type;
2175 char *name;
2176 tree fields[];
2177 int len;
2178 tree align_type;
2179 {
2180 register int i;
2181
2182 TYPE_FIELDS (type) = fields[0];
2183 for (i = 0; i < len; i++)
2184 {
2185 layout_type (TREE_TYPE (fields[i]));
2186 DECL_FIELD_CONTEXT (fields[i]) = type;
2187 TREE_CHAIN (fields[i]) = fields[i+1];
2188 }
2189 DECL_FIELD_CONTEXT (fields[i]) = type;
2190 DECL_CLASS_CONTEXT (fields[i]) = type;
2191 TYPE_ALIGN (type) = TYPE_ALIGN (align_type);
2192 layout_type (type);
2193 #if 0 /* not yet, should get fixed properly later */
2194 TYPE_NAME (type) = make_type_decl (get_identifier (name), type);
2195 #else
2196 TYPE_NAME (type) = build_decl (TYPE_DECL, get_identifier (name), type);
2197 #endif
2198 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2199 layout_decl (TYPE_NAME (type), 0);
2200 }
2201 \f
2202 /* Auxiliary functions to make type signatures for
2203 `operator new' and `operator delete' correspond to
2204 what compiler will be expecting. */
2205
2206 extern tree sizetype;
2207
2208 tree
2209 coerce_new_type (type)
2210 tree type;
2211 {
2212 int e1 = 0, e2 = 0;
2213
2214 if (TREE_CODE (type) == METHOD_TYPE)
2215 type = build_function_type (TREE_TYPE (type), TREE_CHAIN (TYPE_ARG_TYPES (type)));
2216 if (TREE_TYPE (type) != ptr_type_node)
2217 e1 = 1, error ("`operator new' must return type `void *'");
2218
2219 /* Technically the type must be `size_t', but we may not know
2220 what that is. */
2221 if (TYPE_ARG_TYPES (type) == NULL_TREE)
2222 e1 = 1, error ("`operator new' takes type `size_t' parameter");
2223 else if (TREE_CODE (TREE_VALUE (TYPE_ARG_TYPES (type))) != INTEGER_TYPE
2224 || TYPE_PRECISION (TREE_VALUE (TYPE_ARG_TYPES (type))) != TYPE_PRECISION (sizetype))
2225 e2 = 1, error ("`operator new' takes type `size_t' as first parameter");
2226 if (e2)
2227 type = build_function_type (ptr_type_node, tree_cons (NULL_TREE, sizetype, TREE_CHAIN (TYPE_ARG_TYPES (type))));
2228 else if (e1)
2229 type = build_function_type (ptr_type_node, TYPE_ARG_TYPES (type));
2230 return type;
2231 }
2232
2233 tree
2234 coerce_delete_type (type)
2235 tree type;
2236 {
2237 int e1 = 0, e2 = 0, e3 = 0;
2238 tree arg_types = TYPE_ARG_TYPES (type);
2239
2240 if (TREE_CODE (type) == METHOD_TYPE)
2241 {
2242 type = build_function_type (TREE_TYPE (type), TREE_CHAIN (arg_types));
2243 arg_types = TREE_CHAIN (arg_types);
2244 }
2245
2246 if (TREE_TYPE (type) != void_type_node)
2247 e1 = 1, error ("`operator delete' must return type `void'");
2248
2249 if (arg_types == NULL_TREE
2250 || TREE_VALUE (arg_types) != ptr_type_node)
2251 e2 = 1, error ("`operator delete' takes type `void *' as first parameter");
2252
2253 if (arg_types
2254 && TREE_CHAIN (arg_types)
2255 && TREE_CHAIN (arg_types) != void_list_node)
2256 {
2257 /* Again, technically this argument must be `size_t', but again
2258 we may not know what that is. */
2259 tree t2 = TREE_VALUE (TREE_CHAIN (arg_types));
2260 if (TREE_CODE (t2) != INTEGER_TYPE
2261 || TYPE_PRECISION (t2) != TYPE_PRECISION (sizetype))
2262 e3 = 1, error ("second argument to `operator delete' must be of type `size_t'");
2263 else if (TREE_CHAIN (TREE_CHAIN (arg_types)) != void_list_node)
2264 {
2265 e3 = 1;
2266 if (TREE_CHAIN (TREE_CHAIN (arg_types)))
2267 error ("too many arguments in declaration of `operator delete'");
2268 else
2269 error ("`...' invalid in specification of `operator delete'");
2270 }
2271 }
2272
2273 if (e3)
2274 arg_types = tree_cons (NULL_TREE, ptr_type_node,
2275 build_tree_list (NULL_TREE, sizetype));
2276 else if (e3 |= e2)
2277 {
2278 if (arg_types == NULL_TREE)
2279 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
2280 else
2281 arg_types = tree_cons (NULL_TREE, ptr_type_node, TREE_CHAIN (arg_types));
2282 }
2283 else e3 |= e1;
2284
2285 if (e3)
2286 type = build_function_type (void_type_node, arg_types);
2287
2288 return type;
2289 }
2290 \f
2291 extern tree abort_fndecl;
2292
2293 static void
2294 mark_vtable_entries (decl)
2295 tree decl;
2296 {
2297 tree entries = CONSTRUCTOR_ELTS (DECL_INITIAL (decl));
2298
2299 if (flag_rtti)
2300 {
2301 tree fnaddr = (flag_vtable_thunks ? TREE_VALUE (TREE_CHAIN (entries))
2302 : FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (entries)));
2303 tree fn = TREE_OPERAND (fnaddr, 0);
2304 TREE_ADDRESSABLE (fn) = 1;
2305 mark_used (fn);
2306 }
2307 skip_rtti_stuff (&entries);
2308
2309 for (; entries; entries = TREE_CHAIN (entries))
2310 {
2311 tree fnaddr = (flag_vtable_thunks ? TREE_VALUE (entries)
2312 : FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (entries)));
2313 tree fn = TREE_OPERAND (fnaddr, 0);
2314 TREE_ADDRESSABLE (fn) = 1;
2315 if (DECL_LANG_SPECIFIC (fn) && DECL_ABSTRACT_VIRTUAL_P (fn))
2316 TREE_OPERAND (fnaddr, 0) = fn = abort_fndecl;
2317 if (TREE_CODE (fn) == THUNK_DECL && DECL_EXTERNAL (fn))
2318 {
2319 DECL_EXTERNAL (fn) = 0;
2320 emit_thunk (fn);
2321 }
2322 mark_used (fn);
2323 }
2324 }
2325
2326 /* Set DECL up to have the closest approximation of "initialized common"
2327 linkage available. */
2328
2329 void
2330 comdat_linkage (decl)
2331 tree decl;
2332 {
2333 if (flag_weak)
2334 make_decl_one_only (decl);
2335 else
2336 TREE_PUBLIC (decl) = 0;
2337 }
2338
2339 /* Set TREE_PUBLIC and/or DECL_EXTERN on the vtable DECL,
2340 based on TYPE and other static flags.
2341
2342 Note that anything public is tagged TREE_PUBLIC, whether
2343 it's public in this file or in another one. */
2344
2345 void
2346 import_export_vtable (decl, type, final)
2347 tree decl, type;
2348 int final;
2349 {
2350 if (DECL_INTERFACE_KNOWN (decl))
2351 return;
2352
2353 /* +e0 or +e1 */
2354 if (write_virtuals < 2 && write_virtuals != 0)
2355 {
2356 TREE_PUBLIC (decl) = 1;
2357 if (write_virtuals < 0)
2358 DECL_EXTERNAL (decl) = 1;
2359 DECL_INTERFACE_KNOWN (decl) = 1;
2360 }
2361 else if (CLASSTYPE_INTERFACE_KNOWN (type))
2362 {
2363 TREE_PUBLIC (decl) = 1;
2364 DECL_EXTERNAL (decl) = ! CLASSTYPE_VTABLE_NEEDS_WRITING (type);
2365 DECL_INTERFACE_KNOWN (decl) = 1;
2366 }
2367 else
2368 {
2369 /* We can only wait to decide if we have real non-inline virtual
2370 functions in our class, or if we come from a template. */
2371
2372 int found = CLASSTYPE_TEMPLATE_INSTANTIATION (type);
2373
2374 if (! found && ! final)
2375 {
2376 tree method;
2377 for (method = TYPE_METHODS (type); method != NULL_TREE;
2378 method = TREE_CHAIN (method))
2379 if (DECL_VINDEX (method) != NULL_TREE
2380 && ! DECL_THIS_INLINE (method)
2381 && ! DECL_ABSTRACT_VIRTUAL_P (method))
2382 {
2383 found = 1;
2384 break;
2385 }
2386 }
2387
2388 if (final || ! found)
2389 {
2390 comdat_linkage (decl);
2391 DECL_EXTERNAL (decl) = 0;
2392 }
2393 else
2394 {
2395 TREE_PUBLIC (decl) = 1;
2396 DECL_EXTERNAL (decl) = 1;
2397 }
2398 }
2399 }
2400
2401 static void
2402 import_export_template (type)
2403 tree type;
2404 {
2405 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
2406 && ! flag_implicit_templates
2407 && CLASSTYPE_INTERFACE_UNKNOWN (type))
2408 {
2409 SET_CLASSTYPE_INTERFACE_KNOWN (type);
2410 CLASSTYPE_INTERFACE_ONLY (type) = 1;
2411 CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 0;
2412 }
2413 }
2414
2415 int
2416 finish_prevtable_vardecl (prev, vars)
2417 tree prev, vars;
2418 {
2419 tree ctype = DECL_CONTEXT (vars);
2420 import_export_template (ctype);
2421
2422 #ifndef MULTIPLE_SYMBOL_SPACES
2423 if (CLASSTYPE_INTERFACE_UNKNOWN (ctype) && TYPE_VIRTUAL_P (ctype)
2424 && ! CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2425 {
2426 tree method;
2427 for (method = TYPE_METHODS (ctype); method != NULL_TREE;
2428 method = TREE_CHAIN (method))
2429 {
2430 if (DECL_VINDEX (method) != NULL_TREE
2431 && !DECL_THIS_INLINE (method)
2432 && !DECL_ABSTRACT_VIRTUAL_P (method))
2433 {
2434 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
2435 CLASSTYPE_VTABLE_NEEDS_WRITING (ctype) = ! DECL_EXTERNAL (method);
2436 CLASSTYPE_INTERFACE_ONLY (ctype) = DECL_EXTERNAL (method);
2437 break;
2438 }
2439 }
2440 }
2441 #endif
2442
2443 import_export_vtable (vars, ctype, 1);
2444 return 1;
2445 }
2446
2447 static int
2448 finish_vtable_vardecl (prev, vars)
2449 tree prev, vars;
2450 {
2451 if (write_virtuals >= 0
2452 && ! DECL_EXTERNAL (vars)
2453 && ((TREE_PUBLIC (vars) && ! DECL_WEAK (vars) && ! DECL_ONE_ONLY (vars))
2454 || TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (vars))
2455 || (hack_decl_function_context (vars) && TREE_USED (vars)))
2456 && ! TREE_ASM_WRITTEN (vars))
2457 {
2458 /* Write it out. */
2459 mark_vtable_entries (vars);
2460 if (TREE_TYPE (DECL_INITIAL (vars)) == 0)
2461 store_init_value (vars, DECL_INITIAL (vars));
2462
2463 if (write_symbols == DWARF_DEBUG || write_symbols == DWARF2_DEBUG)
2464 {
2465 /* Mark the VAR_DECL node representing the vtable itself as a
2466 "gratuitous" one, thereby forcing dwarfout.c to ignore it.
2467 It is rather important that such things be ignored because
2468 any effort to actually generate DWARF for them will run
2469 into trouble when/if we encounter code like:
2470
2471 #pragma interface
2472 struct S { virtual void member (); };
2473
2474 because the artificial declaration of the vtable itself (as
2475 manufactured by the g++ front end) will say that the vtable
2476 is a static member of `S' but only *after* the debug output
2477 for the definition of `S' has already been output. This causes
2478 grief because the DWARF entry for the definition of the vtable
2479 will try to refer back to an earlier *declaration* of the
2480 vtable as a static member of `S' and there won't be one.
2481 We might be able to arrange to have the "vtable static member"
2482 attached to the member list for `S' before the debug info for
2483 `S' get written (which would solve the problem) but that would
2484 require more intrusive changes to the g++ front end. */
2485
2486 DECL_IGNORED_P (vars) = 1;
2487 }
2488
2489 rest_of_decl_compilation (vars, NULL_PTR, 1, 1);
2490 return 1;
2491 }
2492 else if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (vars)))
2493 /* We don't know what to do with this one yet. */
2494 return 0;
2495
2496 /* We know that PREV must be non-zero here. */
2497 TREE_CHAIN (prev) = TREE_CHAIN (vars);
2498 return 0;
2499 }
2500
2501 static int
2502 prune_vtable_vardecl (prev, vars)
2503 tree prev, vars;
2504 {
2505 /* We know that PREV must be non-zero here. */
2506 TREE_CHAIN (prev) = TREE_CHAIN (vars);
2507 return 1;
2508 }
2509
2510 int
2511 walk_vtables (typedecl_fn, vardecl_fn)
2512 register void (*typedecl_fn)();
2513 register int (*vardecl_fn)();
2514 {
2515 tree prev, vars;
2516 int flag = 0;
2517
2518 for (prev = 0, vars = getdecls (); vars; vars = TREE_CHAIN (vars))
2519 {
2520 register tree type = TREE_TYPE (vars);
2521
2522 if (TREE_CODE (vars) == VAR_DECL && DECL_VIRTUAL_P (vars))
2523 {
2524 if (vardecl_fn)
2525 flag |= (*vardecl_fn) (prev, vars);
2526
2527 if (prev && TREE_CHAIN (prev) != vars)
2528 continue;
2529 }
2530 else if (TREE_CODE (vars) == TYPE_DECL
2531 && type != error_mark_node
2532 && TYPE_LANG_SPECIFIC (type)
2533 && CLASSTYPE_VSIZE (type))
2534 {
2535 if (typedecl_fn) (*typedecl_fn) (prev, vars);
2536 }
2537
2538 prev = vars;
2539 }
2540
2541 return flag;
2542 }
2543
2544 static void
2545 finish_sigtable_vardecl (prev, vars)
2546 tree prev, vars;
2547 {
2548 /* We don't need to mark sigtable entries as addressable here as is done
2549 for vtables. Since sigtables, unlike vtables, are always written out,
2550 that was already done in build_signature_table_constructor. */
2551
2552 rest_of_decl_compilation (vars, NULL_PTR, 1, 1);
2553
2554 /* We know that PREV must be non-zero here. */
2555 TREE_CHAIN (prev) = TREE_CHAIN (vars);
2556 }
2557
2558 void
2559 walk_sigtables (typedecl_fn, vardecl_fn)
2560 register void (*typedecl_fn)();
2561 register void (*vardecl_fn)();
2562 {
2563 tree prev, vars;
2564
2565 for (prev = 0, vars = getdecls (); vars; vars = TREE_CHAIN (vars))
2566 {
2567 register tree type = TREE_TYPE (vars);
2568
2569 if (TREE_CODE (vars) == TYPE_DECL
2570 && type != error_mark_node
2571 && IS_SIGNATURE (type))
2572 {
2573 if (typedecl_fn) (*typedecl_fn) (prev, vars);
2574 }
2575 else if (TREE_CODE (vars) == VAR_DECL
2576 && TREE_TYPE (vars) != error_mark_node
2577 && IS_SIGNATURE (TREE_TYPE (vars)))
2578 {
2579 if (vardecl_fn) (*vardecl_fn) (prev, vars);
2580 }
2581 else
2582 prev = vars;
2583 }
2584 }
2585
2586 /* Determines the proper settings of TREE_PUBLIC and DECL_EXTERNAL for an
2587 inline function or template instantiation at end-of-file. */
2588
2589 void
2590 import_export_decl (decl)
2591 tree decl;
2592 {
2593 if (DECL_INTERFACE_KNOWN (decl))
2594 return;
2595
2596 if (DECL_TEMPLATE_INSTANTIATION (decl))
2597 {
2598 DECL_NOT_REALLY_EXTERN (decl) = 1;
2599 if (DECL_IMPLICIT_INSTANTIATION (decl)
2600 && (flag_implicit_templates || DECL_THIS_INLINE (decl)))
2601 {
2602 if (TREE_CODE (decl) == FUNCTION_DECL)
2603 comdat_linkage (decl);
2604 /* Dynamically initialized vars go into common. */
2605 else if (DECL_INITIAL (decl) == NULL_TREE
2606 || DECL_INITIAL (decl) == error_mark_node)
2607 DECL_COMMON (decl) = 1;
2608 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
2609 {
2610 DECL_COMMON (decl) = 1;
2611 DECL_INITIAL (decl) = error_mark_node;
2612 }
2613 else
2614 {
2615 /* Statically initialized vars are weak or comdat, if
2616 supported. */
2617 if (flag_weak)
2618 make_decl_one_only (decl);
2619 /* else leave vars public so multiple defs will break. */
2620 }
2621 }
2622 else
2623 DECL_NOT_REALLY_EXTERN (decl) = 0;
2624 }
2625 else if (DECL_FUNCTION_MEMBER_P (decl))
2626 {
2627 tree ctype = DECL_CLASS_CONTEXT (decl);
2628 if (CLASSTYPE_INTERFACE_KNOWN (ctype) && ! DECL_ARTIFICIAL (decl))
2629 {
2630 DECL_NOT_REALLY_EXTERN (decl)
2631 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2632 || (DECL_THIS_INLINE (decl) && ! flag_implement_inlines));
2633 }
2634 else
2635 comdat_linkage (decl);
2636 }
2637 /* tinfo function */
2638 else if (DECL_ARTIFICIAL (decl) && DECL_MUTABLE_P (decl))
2639 {
2640 tree ctype = TREE_TYPE (DECL_NAME (decl));
2641 if (IS_AGGR_TYPE (ctype) && CLASSTYPE_INTERFACE_KNOWN (ctype)
2642 && TYPE_VIRTUAL_P (ctype))
2643 {
2644 DECL_NOT_REALLY_EXTERN (decl)
2645 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2646 || (DECL_THIS_INLINE (decl) && ! flag_implement_inlines));
2647 }
2648 else if (TYPE_BUILT_IN (ctype) && ctype == TYPE_MAIN_VARIANT (ctype))
2649 DECL_NOT_REALLY_EXTERN (decl) = 0;
2650 else
2651 comdat_linkage (decl);
2652 }
2653 else if (DECL_C_STATIC (decl))
2654 TREE_PUBLIC (decl) = 0;
2655 else
2656 comdat_linkage (decl);
2657
2658 DECL_INTERFACE_KNOWN (decl) = 1;
2659 }
2660
2661 tree
2662 build_cleanup (decl)
2663 tree decl;
2664 {
2665 tree temp;
2666 tree type = TREE_TYPE (decl);
2667
2668 if (TREE_CODE (type) == ARRAY_TYPE)
2669 temp = decl;
2670 else
2671 {
2672 mark_addressable (decl);
2673 temp = build1 (ADDR_EXPR, build_pointer_type (type), decl);
2674 }
2675 temp = build_delete (TREE_TYPE (temp), temp,
2676 integer_two_node,
2677 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
2678 return temp;
2679 }
2680
2681 extern int parse_time, varconst_time;
2682 extern tree pending_templates;
2683 extern tree maybe_templates;
2684
2685 extern struct obstack permanent_obstack;
2686 extern tree get_id_2 ();
2687
2688 static tree
2689 get_sentry (base)
2690 tree base;
2691 {
2692 tree sname = get_id_2 ("__sn", base);
2693 tree sentry = IDENTIFIER_GLOBAL_VALUE (sname);
2694 if (! sentry)
2695 {
2696 push_obstacks (&permanent_obstack, &permanent_obstack);
2697 sentry = build_decl (VAR_DECL, sname, integer_type_node);
2698 TREE_PUBLIC (sentry) = 1;
2699 DECL_ARTIFICIAL (sentry) = 1;
2700 TREE_STATIC (sentry) = 1;
2701 TREE_USED (sentry) = 1;
2702 DECL_COMMON (sentry) = 1;
2703 pushdecl_top_level (sentry);
2704 cp_finish_decl (sentry, NULL_TREE, NULL_TREE, 0, 0);
2705 pop_obstacks ();
2706 }
2707 return sentry;
2708 }
2709
2710 /* This routine is called from the last rule in yyparse ().
2711 Its job is to create all the code needed to initialize and
2712 destroy the global aggregates. We do the destruction
2713 first, since that way we only need to reverse the decls once. */
2714
2715 void
2716 finish_file ()
2717 {
2718 extern int lineno;
2719 int start_time, this_time;
2720
2721 tree fnname;
2722 tree vars;
2723 int needs_cleaning = 0, needs_messing_up = 0;
2724
2725 at_eof = 1;
2726
2727 if (flag_detailed_statistics)
2728 dump_tree_statistics ();
2729
2730 /* Bad parse errors. Just forget about it. */
2731 if (! global_bindings_p () || current_class_type)
2732 return;
2733
2734 start_time = get_run_time ();
2735
2736 /* Otherwise, GDB can get confused, because in only knows
2737 about source for LINENO-1 lines. */
2738 lineno -= 1;
2739
2740 interface_unknown = 1;
2741 interface_only = 0;
2742
2743 for (fnname = pending_templates; fnname; fnname = TREE_CHAIN (fnname))
2744 {
2745 tree decl = TREE_VALUE (fnname);
2746 if (TREE_CODE_CLASS (TREE_CODE (decl)) == 't')
2747 {
2748 instantiate_class_template (decl);
2749 if (CLASSTYPE_TEMPLATE_INSTANTIATION (decl))
2750 for (vars = TYPE_METHODS (decl); vars; vars = TREE_CHAIN (vars))
2751 if (! DECL_ARTIFICIAL (vars))
2752 instantiate_decl (vars);
2753 }
2754 else
2755 instantiate_decl (decl);
2756 }
2757
2758 for (fnname = maybe_templates; fnname; fnname = TREE_CHAIN (fnname))
2759 {
2760 tree *args, fn, decl = TREE_VALUE (fnname);
2761
2762 if (DECL_INITIAL (decl))
2763 continue;
2764
2765 fn = TREE_PURPOSE (fnname);
2766 args = get_bindings (fn, decl);
2767 fn = instantiate_template (fn, args);
2768 free (args);
2769 instantiate_decl (fn);
2770 }
2771
2772 /* Push into C language context, because that's all
2773 we'll need here. */
2774 push_lang_context (lang_name_c);
2775
2776 #if 1
2777 /* The reason for pushing garbage onto the global_binding_level is to
2778 ensure that we can slice out _DECLs which pertain to virtual function
2779 tables. If the last thing pushed onto the global_binding_level was a
2780 virtual function table, then slicing it out would slice away all the
2781 decls (i.e., we lose the head of the chain).
2782
2783 There are several ways of getting the same effect, from changing the
2784 way that iterators over the chain treat the elements that pertain to
2785 virtual function tables, moving the implementation of this code to
2786 decl.c (where we can manipulate global_binding_level directly),
2787 popping the garbage after pushing it and slicing away the vtable
2788 stuff, or just leaving it alone. */
2789
2790 /* Make last thing in global scope not be a virtual function table. */
2791 #if 0 /* not yet, should get fixed properly later */
2792 vars = make_type_decl (get_identifier (" @%$#@!"), integer_type_node);
2793 #else
2794 vars = build_decl (TYPE_DECL, get_identifier (" @%$#@!"), integer_type_node);
2795 #endif
2796 DECL_IGNORED_P (vars) = 1;
2797 SET_DECL_ARTIFICIAL (vars);
2798 pushdecl (vars);
2799 #endif
2800
2801 /* Walk to mark the inline functions we need, then output them so
2802 that we can pick up any other tdecls that those routines need. */
2803 walk_vtables ((void (*)())0, finish_prevtable_vardecl);
2804
2805 for (vars = pending_statics; vars; vars = TREE_CHAIN (vars))
2806 {
2807 tree decl = TREE_VALUE (vars);
2808
2809 if (DECL_TEMPLATE_INSTANTIATION (decl)
2810 && ! DECL_IN_AGGR_P (decl))
2811 {
2812 import_export_decl (decl);
2813 DECL_EXTERNAL (decl) = ! DECL_NOT_REALLY_EXTERN (decl);
2814 }
2815 }
2816
2817 for (vars = static_aggregates; vars; vars = TREE_CHAIN (vars))
2818 if (! TREE_ASM_WRITTEN (TREE_VALUE (vars)))
2819 rest_of_decl_compilation (TREE_VALUE (vars), 0, 1, 1);
2820 vars = static_aggregates;
2821
2822 if (static_ctors || vars || exception_table_p ())
2823 needs_messing_up = 1;
2824 if (static_dtors)
2825 needs_cleaning = 1;
2826
2827 /* See if we really need the hassle. */
2828 while (vars && needs_cleaning == 0)
2829 {
2830 tree decl = TREE_VALUE (vars);
2831 tree type = TREE_TYPE (decl);
2832 if (TYPE_NEEDS_DESTRUCTOR (type) && ! TREE_STATIC (vars))
2833 {
2834 needs_cleaning = 1;
2835 break;
2836 }
2837
2838 vars = TREE_CHAIN (vars);
2839 }
2840
2841 if (needs_cleaning == 0)
2842 goto mess_up;
2843
2844 fnname = get_file_function_name ('D');
2845 start_function (void_list_node,
2846 make_call_declarator (fnname, void_list_node, NULL_TREE,
2847 NULL_TREE),
2848 NULL_TREE, 0);
2849 fnname = DECL_ASSEMBLER_NAME (current_function_decl);
2850 store_parm_decls ();
2851
2852 pushlevel (0);
2853 clear_last_expr ();
2854 push_momentary ();
2855 expand_start_bindings (0);
2856
2857 /* These must be done in backward order to destroy,
2858 in which they happen to be! */
2859 for (vars = static_aggregates; vars; vars = TREE_CHAIN (vars))
2860 {
2861 tree decl = TREE_VALUE (vars);
2862 tree type = TREE_TYPE (decl);
2863 tree temp = TREE_PURPOSE (vars);
2864
2865 if (TYPE_NEEDS_DESTRUCTOR (type) && ! TREE_STATIC (vars)
2866 && ! DECL_EXTERNAL (decl))
2867 {
2868 int protect = (TREE_PUBLIC (decl) && (DECL_COMMON (decl)
2869 || DECL_ONE_ONLY (decl)
2870 || DECL_WEAK (decl)));
2871
2872 temp = build_cleanup (decl);
2873
2874 if (protect)
2875 {
2876 tree sentry = get_sentry (DECL_ASSEMBLER_NAME (decl));
2877 sentry = build_unary_op (PREDECREMENT_EXPR, sentry, 0);
2878 sentry = build_binary_op (EQ_EXPR, sentry, integer_zero_node, 1);
2879 expand_start_cond (sentry, 0);
2880 }
2881
2882 expand_expr_stmt (temp);
2883
2884 if (protect)
2885 expand_end_cond ();
2886 }
2887 }
2888
2889 for (; static_dtors; static_dtors = TREE_CHAIN (static_dtors))
2890 expand_expr_stmt (build_function_call (TREE_VALUE (static_dtors),
2891 NULL_TREE));
2892
2893 expand_end_bindings (getdecls (), 1, 0);
2894 poplevel (1, 0, 0);
2895 pop_momentary ();
2896
2897 finish_function (lineno, 0, 0);
2898
2899 assemble_destructor (IDENTIFIER_POINTER (fnname));
2900
2901 /* if it needed cleaning, then it will need messing up: drop through */
2902
2903 mess_up:
2904 /* Must do this while we think we are at the top level. */
2905 vars = nreverse (static_aggregates);
2906 if (needs_messing_up)
2907 {
2908 fnname = get_file_function_name ('I');
2909 start_function (void_list_node,
2910 make_call_declarator (fnname, void_list_node, NULL_TREE,
2911 NULL_TREE),
2912 NULL_TREE, 0);
2913 fnname = DECL_ASSEMBLER_NAME (current_function_decl);
2914 store_parm_decls ();
2915
2916 pushlevel (0);
2917 clear_last_expr ();
2918 push_momentary ();
2919 expand_start_bindings (0);
2920
2921 if (exception_table_p ())
2922 register_exception_table ();
2923
2924 while (vars)
2925 {
2926 tree decl = TREE_VALUE (vars);
2927 tree init = TREE_PURPOSE (vars);
2928
2929 /* If this was a static attribute within some function's scope,
2930 then don't initialize it here. Also, don't bother
2931 with initializers that contain errors. */
2932 if (TREE_STATIC (vars)
2933 || DECL_EXTERNAL (decl)
2934 || (init && TREE_CODE (init) == TREE_LIST
2935 && value_member (error_mark_node, init)))
2936 goto next_mess;
2937
2938 if (TREE_CODE (decl) == VAR_DECL)
2939 {
2940 int protect = (TREE_PUBLIC (decl) && (DECL_COMMON (decl)
2941 || DECL_ONE_ONLY (decl)
2942 || DECL_WEAK (decl)));
2943
2944 /* Set these global variables so that GDB at least puts
2945 us near the declaration which required the initialization. */
2946 input_filename = DECL_SOURCE_FILE (decl);
2947 lineno = DECL_SOURCE_LINE (decl);
2948 emit_note (input_filename, lineno);
2949
2950 /* 9.5p5: The initializer of a static member of a class has
2951 the same access rights as a member function. */
2952 DECL_CLASS_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
2953 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
2954
2955 if (protect)
2956 {
2957 tree sentry = get_sentry (DECL_ASSEMBLER_NAME (decl));
2958 sentry = build_unary_op (PREINCREMENT_EXPR, sentry, 0);
2959 sentry = build_binary_op
2960 (EQ_EXPR, sentry, integer_one_node, 1);
2961 expand_start_cond (sentry, 0);
2962 }
2963
2964 expand_start_target_temps ();
2965
2966 if (IS_AGGR_TYPE (TREE_TYPE (decl))
2967 || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
2968 expand_aggr_init (decl, init, 0, 0);
2969 else if (TREE_CODE (init) == TREE_VEC)
2970 {
2971 expand_expr (expand_vec_init (decl, TREE_VEC_ELT (init, 0),
2972 TREE_VEC_ELT (init, 1),
2973 TREE_VEC_ELT (init, 2), 0),
2974 const0_rtx, VOIDmode, 0);
2975 }
2976 else
2977 expand_assignment (decl, init, 0, 0);
2978
2979 /* Cleanup any temporaries needed for the initial value. */
2980 expand_end_target_temps ();
2981
2982 if (protect)
2983 expand_end_cond ();
2984
2985 DECL_CLASS_CONTEXT (current_function_decl) = NULL_TREE;
2986 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
2987 }
2988 else if (decl == error_mark_node)
2989 ;
2990 else my_friendly_abort (22);
2991
2992 next_mess:
2993 vars = TREE_CHAIN (vars);
2994 }
2995
2996 for (; static_ctors; static_ctors = TREE_CHAIN (static_ctors))
2997 expand_expr_stmt (build_function_call (TREE_VALUE (static_ctors),
2998 NULL_TREE));
2999
3000 expand_end_bindings (getdecls (), 1, 0);
3001 poplevel (1, 0, 0);
3002 pop_momentary ();
3003
3004 finish_function (lineno, 0, 0);
3005 assemble_constructor (IDENTIFIER_POINTER (fnname));
3006 }
3007
3008 expand_builtin_throw ();
3009
3010 permanent_allocation (1);
3011
3012 /* Done with C language context needs. */
3013 pop_lang_context ();
3014
3015 /* Now write out any static class variables (which may have since
3016 learned how to be initialized). */
3017 while (pending_statics)
3018 {
3019 tree decl = TREE_VALUE (pending_statics);
3020
3021 /* Output DWARF debug information. */
3022 #ifdef DWARF_DEBUGGING_INFO
3023 if (write_symbols == DWARF_DEBUG)
3024 dwarfout_file_scope_decl (decl, 1);
3025 #endif
3026 #ifdef DWARF2_DEBUGGING_INFO
3027 if (write_symbols == DWARF2_DEBUG)
3028 dwarf2out_decl (decl);
3029 #endif
3030
3031 DECL_DEFER_OUTPUT (decl) = 0;
3032 rest_of_decl_compilation
3033 (decl, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), 1, 1);
3034
3035 pending_statics = TREE_CHAIN (pending_statics);
3036 }
3037
3038 this_time = get_run_time ();
3039 parse_time -= this_time - start_time;
3040 varconst_time += this_time - start_time;
3041
3042 start_time = get_run_time ();
3043
3044 if (flag_handle_signatures)
3045 walk_sigtables ((void (*)())0, finish_sigtable_vardecl);
3046
3047 for (fnname = saved_inlines; fnname; fnname = TREE_CHAIN (fnname))
3048 {
3049 tree decl = TREE_VALUE (fnname);
3050 import_export_decl (decl);
3051 }
3052
3053 /* Now write out inline functions which had their addresses taken and
3054 which were not declared virtual and which were not declared `extern
3055 inline'. */
3056 {
3057 int reconsider = 1; /* More may be referenced; check again */
3058
3059 while (reconsider)
3060 {
3061 tree *p = &saved_inlines;
3062 reconsider = 0;
3063
3064 /* We need to do this each time so that newly completed template
3065 types don't wind up at the front of the list. Sigh. */
3066 vars = build_decl (TYPE_DECL, make_anon_name (), integer_type_node);
3067 DECL_IGNORED_P (vars) = 1;
3068 SET_DECL_ARTIFICIAL (vars);
3069 pushdecl (vars);
3070
3071 reconsider |= walk_vtables ((void (*)())0, finish_vtable_vardecl);
3072
3073 while (*p)
3074 {
3075 tree decl = TREE_VALUE (*p);
3076
3077 if (DECL_ARTIFICIAL (decl) && ! DECL_INITIAL (decl)
3078 && TREE_USED (decl)
3079 && (! DECL_REALLY_EXTERN (decl) || DECL_INLINE (decl)))
3080 {
3081 if (DECL_MUTABLE_P (decl))
3082 synthesize_tinfo_fn (decl);
3083 else
3084 synthesize_method (decl);
3085 reconsider = 1;
3086 }
3087
3088 /* Catch new template instantiations. */
3089 if (decl != TREE_VALUE (*p))
3090 continue;
3091
3092 if (TREE_ASM_WRITTEN (decl)
3093 || (DECL_SAVED_INSNS (decl) == 0 && ! DECL_ARTIFICIAL (decl)))
3094 *p = TREE_CHAIN (*p);
3095 else if (DECL_INITIAL (decl) == 0)
3096 p = &TREE_CHAIN (*p);
3097 else if ((TREE_PUBLIC (decl) && ! DECL_WEAK (decl)
3098 && ! DECL_ONE_ONLY (decl))
3099 || TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
3100 || flag_keep_inline_functions)
3101 {
3102 if (DECL_NOT_REALLY_EXTERN (decl))
3103 {
3104 DECL_EXTERNAL (decl) = 0;
3105 reconsider = 1;
3106 /* We can't inline this function after it's been
3107 emitted, so just disable inlining. We want a
3108 variant of output_inline_function that doesn't
3109 prevent subsequent integration... */
3110 flag_no_inline = 1;
3111 temporary_allocation ();
3112 output_inline_function (decl);
3113 permanent_allocation (1);
3114 }
3115
3116 *p = TREE_CHAIN (*p);
3117 }
3118 else
3119 p = &TREE_CHAIN (*p);
3120 }
3121 }
3122 }
3123
3124 /* Now delete from the chain of variables all virtual function tables.
3125 We output them all ourselves, because each will be treated specially. */
3126
3127 walk_vtables ((void (*)())0, prune_vtable_vardecl);
3128
3129 for (vars = getdecls (); vars; vars = TREE_CHAIN (vars))
3130 {
3131 if (TREE_CODE (vars) == FUNCTION_DECL
3132 && ! DECL_INTERFACE_KNOWN (vars)
3133 && DECL_C_STATIC (vars))
3134 TREE_PUBLIC (vars) = 0;
3135 }
3136
3137 if (write_virtuals == 2)
3138 {
3139 /* Now complain about an virtual function tables promised
3140 but not delivered. */
3141 while (pending_vtables)
3142 {
3143 if (TREE_PURPOSE (pending_vtables) == NULL_TREE)
3144 error ("virtual function table for `%s' not defined",
3145 IDENTIFIER_POINTER (TREE_VALUE (pending_vtables)));
3146 pending_vtables = TREE_CHAIN (pending_vtables);
3147 }
3148 }
3149
3150 finish_repo ();
3151
3152 this_time = get_run_time ();
3153 parse_time -= this_time - start_time;
3154 varconst_time += this_time - start_time;
3155
3156 if (flag_detailed_statistics)
3157 dump_time_statistics ();
3158 }
3159
3160 /* This is something of the form 'A()()()()()+1' that has turned out to be an
3161 expr. Since it was parsed like a type, we need to wade through and fix
3162 that. Unfortunately, since operator() is left-associative, we can't use
3163 tail recursion. In the above example, TYPE is `A', and DECL is
3164 `()()()()()'.
3165
3166 Maybe this shouldn't be recursive, but how often will it actually be
3167 used? (jason) */
3168
3169 tree
3170 reparse_absdcl_as_expr (type, decl)
3171 tree type, decl;
3172 {
3173 /* do build_functional_cast (type, NULL_TREE) at bottom */
3174 if (TREE_OPERAND (decl, 0) == NULL_TREE)
3175 return build_functional_cast (type, NULL_TREE);
3176
3177 /* recurse */
3178 decl = reparse_decl_as_expr (type, TREE_OPERAND (decl, 0));
3179
3180 decl = build_x_function_call (decl, NULL_TREE, current_class_ref);
3181
3182 if (TREE_CODE (decl) == CALL_EXPR && TREE_TYPE (decl) != void_type_node)
3183 decl = require_complete_type (decl);
3184
3185 return decl;
3186 }
3187
3188 /* This is something of the form `int ((int)(int)(int)1)' that has turned
3189 out to be an expr. Since it was parsed like a type, we need to wade
3190 through and fix that. Since casts are right-associative, we are
3191 reversing the order, so we don't have to recurse.
3192
3193 In the above example, DECL is the `(int)(int)(int)', and EXPR is the
3194 `1'. */
3195
3196 tree
3197 reparse_absdcl_as_casts (decl, expr)
3198 tree decl, expr;
3199 {
3200 tree type;
3201
3202 if (TREE_CODE (expr) == CONSTRUCTOR
3203 && TREE_TYPE (expr) == 0)
3204 {
3205 type = groktypename (TREE_VALUE (TREE_OPERAND (decl, 1)));
3206 decl = TREE_OPERAND (decl, 0);
3207
3208 if (IS_SIGNATURE (type))
3209 {
3210 error ("cast specifies signature type");
3211 return error_mark_node;
3212 }
3213
3214 expr = digest_init (type, expr, (tree *) 0);
3215 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_SIZE (type) == 0)
3216 {
3217 int failure = complete_array_type (type, expr, 1);
3218 if (failure)
3219 my_friendly_abort (78);
3220 }
3221 }
3222
3223 while (decl)
3224 {
3225 type = groktypename (TREE_VALUE (TREE_OPERAND (decl, 1)));
3226 decl = TREE_OPERAND (decl, 0);
3227 expr = build_c_cast (type, expr);
3228 }
3229
3230 return expr;
3231 }
3232
3233 /* Given plain tree nodes for an expression, build up the full semantics. */
3234
3235 tree
3236 build_expr_from_tree (t)
3237 tree t;
3238 {
3239 if (t == NULL_TREE || t == error_mark_node)
3240 return t;
3241
3242 switch (TREE_CODE (t))
3243 {
3244 case IDENTIFIER_NODE:
3245 return do_identifier (t, 0);
3246
3247 case LOOKUP_EXPR:
3248 if (LOOKUP_EXPR_GLOBAL (t))
3249 return do_scoped_id (TREE_OPERAND (t, 0), 0);
3250 else
3251 return do_identifier (TREE_OPERAND (t, 0), 0);
3252
3253 case INDIRECT_REF:
3254 return build_x_indirect_ref
3255 (build_expr_from_tree (TREE_OPERAND (t, 0)), "unary *");
3256
3257 case CAST_EXPR:
3258 return build_functional_cast
3259 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3260
3261 case REINTERPRET_CAST_EXPR:
3262 return build_reinterpret_cast
3263 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3264
3265 case CONST_CAST_EXPR:
3266 return build_const_cast
3267 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3268
3269 case DYNAMIC_CAST_EXPR:
3270 return build_dynamic_cast
3271 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3272
3273 case STATIC_CAST_EXPR:
3274 return build_static_cast
3275 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3276
3277 case PREDECREMENT_EXPR:
3278 case PREINCREMENT_EXPR:
3279 case POSTDECREMENT_EXPR:
3280 case POSTINCREMENT_EXPR:
3281 case NEGATE_EXPR:
3282 case BIT_NOT_EXPR:
3283 case ABS_EXPR:
3284 case TRUTH_NOT_EXPR:
3285 case ADDR_EXPR:
3286 case CONVERT_EXPR: /* Unary + */
3287 if (TREE_TYPE (t))
3288 return t;
3289 return build_x_unary_op (TREE_CODE (t),
3290 build_expr_from_tree (TREE_OPERAND (t, 0)));
3291
3292 case PLUS_EXPR:
3293 case MINUS_EXPR:
3294 case MULT_EXPR:
3295 case TRUNC_DIV_EXPR:
3296 case CEIL_DIV_EXPR:
3297 case FLOOR_DIV_EXPR:
3298 case ROUND_DIV_EXPR:
3299 case EXACT_DIV_EXPR:
3300 case BIT_AND_EXPR:
3301 case BIT_ANDTC_EXPR:
3302 case BIT_IOR_EXPR:
3303 case BIT_XOR_EXPR:
3304 case TRUNC_MOD_EXPR:
3305 case FLOOR_MOD_EXPR:
3306 case TRUTH_ANDIF_EXPR:
3307 case TRUTH_ORIF_EXPR:
3308 case TRUTH_AND_EXPR:
3309 case TRUTH_OR_EXPR:
3310 case RSHIFT_EXPR:
3311 case LSHIFT_EXPR:
3312 case RROTATE_EXPR:
3313 case LROTATE_EXPR:
3314 case EQ_EXPR:
3315 case NE_EXPR:
3316 case MAX_EXPR:
3317 case MIN_EXPR:
3318 case LE_EXPR:
3319 case GE_EXPR:
3320 case LT_EXPR:
3321 case GT_EXPR:
3322 case MEMBER_REF:
3323 return build_x_binary_op
3324 (TREE_CODE (t),
3325 build_expr_from_tree (TREE_OPERAND (t, 0)),
3326 build_expr_from_tree (TREE_OPERAND (t, 1)));
3327
3328 case DOTSTAR_EXPR:
3329 return build_m_component_ref
3330 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3331 build_expr_from_tree (TREE_OPERAND (t, 1)));
3332
3333 case SCOPE_REF:
3334 return build_offset_ref (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1));
3335
3336 case ARRAY_REF:
3337 if (TREE_OPERAND (t, 0) == NULL_TREE)
3338 /* new-type-id */
3339 return build_parse_node (ARRAY_REF, NULL_TREE,
3340 build_expr_from_tree (TREE_OPERAND (t, 1)));
3341 return grok_array_decl (build_expr_from_tree (TREE_OPERAND (t, 0)),
3342 build_expr_from_tree (TREE_OPERAND (t, 1)));
3343
3344 case SIZEOF_EXPR:
3345 {
3346 tree r = build_expr_from_tree (TREE_OPERAND (t, 0));
3347 if (TREE_CODE_CLASS (TREE_CODE (r)) != 't')
3348 r = TREE_TYPE (r);
3349 return c_sizeof (r);
3350 }
3351
3352 case MODOP_EXPR:
3353 return build_x_modify_expr
3354 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3355 TREE_CODE (TREE_OPERAND (t, 1)),
3356 build_expr_from_tree (TREE_OPERAND (t, 2)));
3357
3358 case ARROW_EXPR:
3359 return build_x_arrow
3360 (build_expr_from_tree (TREE_OPERAND (t, 0)));
3361
3362 case NEW_EXPR:
3363 return build_new
3364 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3365 build_expr_from_tree (TREE_OPERAND (t, 1)),
3366 build_expr_from_tree (TREE_OPERAND (t, 2)),
3367 NEW_EXPR_USE_GLOBAL (t));
3368
3369 case DELETE_EXPR:
3370 return delete_sanity
3371 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3372 build_expr_from_tree (TREE_OPERAND (t, 1)),
3373 DELETE_EXPR_USE_VEC (t), DELETE_EXPR_USE_GLOBAL (t));
3374
3375 case COMPOUND_EXPR:
3376 if (TREE_OPERAND (t, 1) == NULL_TREE)
3377 return build_x_compound_expr
3378 (build_expr_from_tree (TREE_OPERAND (t, 0)));
3379 else
3380 my_friendly_abort (42);
3381
3382 case METHOD_CALL_EXPR:
3383 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
3384 {
3385 tree ref = TREE_OPERAND (t, 0);
3386 return build_scoped_method_call
3387 (build_expr_from_tree (TREE_OPERAND (t, 1)),
3388 build_expr_from_tree (TREE_OPERAND (ref, 0)),
3389 TREE_OPERAND (ref, 1),
3390 build_expr_from_tree (TREE_OPERAND (t, 2)));
3391 }
3392 return build_method_call
3393 (build_expr_from_tree (TREE_OPERAND (t, 1)),
3394 TREE_OPERAND (t, 0),
3395 build_expr_from_tree (TREE_OPERAND (t, 2)),
3396 NULL_TREE, LOOKUP_NORMAL);
3397
3398 case CALL_EXPR:
3399 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
3400 {
3401 tree ref = TREE_OPERAND (t, 0);
3402 return build_member_call
3403 (build_expr_from_tree (TREE_OPERAND (ref, 0)),
3404 TREE_OPERAND (ref, 1),
3405 build_expr_from_tree (TREE_OPERAND (t, 1)));
3406 }
3407 else
3408 {
3409 tree name = TREE_OPERAND (t, 0);
3410 if (! really_overloaded_fn (name))
3411 name = build_expr_from_tree (name);
3412 return build_x_function_call
3413 (name, build_expr_from_tree (TREE_OPERAND (t, 1)),
3414 current_class_ref);
3415 }
3416
3417 case COND_EXPR:
3418 return build_x_conditional_expr
3419 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3420 build_expr_from_tree (TREE_OPERAND (t, 1)),
3421 build_expr_from_tree (TREE_OPERAND (t, 2)));
3422
3423 case TREE_LIST:
3424 {
3425 tree purpose, value, chain;
3426
3427 if (t == void_list_node)
3428 return t;
3429
3430 purpose = TREE_PURPOSE (t);
3431 if (purpose)
3432 purpose = build_expr_from_tree (purpose);
3433 value = TREE_VALUE (t);
3434 if (value)
3435 value = build_expr_from_tree (value);
3436 chain = TREE_CHAIN (t);
3437 if (chain && chain != void_type_node)
3438 chain = build_expr_from_tree (chain);
3439 return tree_cons (purpose, value, chain);
3440 }
3441
3442 case COMPONENT_REF:
3443 return build_x_component_ref
3444 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3445 TREE_OPERAND (t, 1), NULL_TREE, 1);
3446
3447 case THROW_EXPR:
3448 return build_throw (build_expr_from_tree (TREE_OPERAND (t, 0)));
3449
3450 case CONSTRUCTOR:
3451 {
3452 tree r = build_nt (CONSTRUCTOR, NULL_TREE,
3453 build_expr_from_tree (CONSTRUCTOR_ELTS (t)));
3454
3455 if (TREE_TYPE (t))
3456 return digest_init (TREE_TYPE (t), r, 0);
3457 return r;
3458 }
3459
3460 case TYPEID_EXPR:
3461 if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (t, 0))) == 't')
3462 return get_typeid (TREE_OPERAND (t, 0));
3463 return build_x_typeid (build_expr_from_tree (TREE_OPERAND (t, 0)));
3464
3465 case VAR_DECL:
3466 return convert_from_reference (t);
3467
3468 default:
3469 return t;
3470 }
3471 }
3472
3473 /* This is something of the form `int (*a)++' that has turned out to be an
3474 expr. It was only converted into parse nodes, so we need to go through
3475 and build up the semantics. Most of the work is done by
3476 build_expr_from_tree, above.
3477
3478 In the above example, TYPE is `int' and DECL is `*a'. */
3479
3480 tree
3481 reparse_decl_as_expr (type, decl)
3482 tree type, decl;
3483 {
3484 decl = build_expr_from_tree (decl);
3485 if (type)
3486 return build_functional_cast (type, build_tree_list (NULL_TREE, decl));
3487 else
3488 return decl;
3489 }
3490
3491 /* This is something of the form `int (*a)' that has turned out to be a
3492 decl. It was only converted into parse nodes, so we need to do the
3493 checking that make_{pointer,reference}_declarator do. */
3494
3495 tree
3496 finish_decl_parsing (decl)
3497 tree decl;
3498 {
3499 extern int current_class_depth;
3500
3501 switch (TREE_CODE (decl))
3502 {
3503 case IDENTIFIER_NODE:
3504 return decl;
3505 case INDIRECT_REF:
3506 return make_pointer_declarator
3507 (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
3508 case ADDR_EXPR:
3509 return make_reference_declarator
3510 (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
3511 case BIT_NOT_EXPR:
3512 TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
3513 return decl;
3514 case SCOPE_REF:
3515 push_nested_class (TREE_TYPE (TREE_OPERAND (decl, 0)), 3);
3516 TREE_COMPLEXITY (decl) = current_class_depth;
3517 return decl;
3518 case ARRAY_REF:
3519 TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
3520 return decl;
3521 default:
3522 my_friendly_abort (5);
3523 return NULL_TREE;
3524 }
3525 }
3526
3527 tree
3528 check_cp_case_value (value)
3529 tree value;
3530 {
3531 if (value == NULL_TREE)
3532 return value;
3533
3534 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3535 STRIP_TYPE_NOPS (value);
3536
3537 if (TREE_READONLY_DECL_P (value))
3538 {
3539 value = decl_constant_value (value);
3540 STRIP_TYPE_NOPS (value);
3541 }
3542 value = fold (value);
3543
3544 if (TREE_CODE (value) != INTEGER_CST
3545 && value != error_mark_node)
3546 {
3547 cp_error ("case label `%E' does not reduce to an integer constant",
3548 value);
3549 value = error_mark_node;
3550 }
3551 else
3552 /* Promote char or short to int. */
3553 value = default_conversion (value);
3554
3555 constant_expression_warning (value);
3556
3557 return value;
3558 }
3559
3560 tree current_namespace;
3561
3562 /* Get the inner part of a namespace id. It doesn't have any prefix, nor
3563 postfix. Returns 0 if in global namespace. */
3564
3565 tree
3566 get_namespace_id ()
3567 {
3568 tree x = current_namespace;
3569 if (x)
3570 x = TREE_PURPOSE (x);
3571 return x;
3572 }
3573
3574 /* Build up a DECL_ASSEMBLER_NAME for NAME in the current namespace. */
3575
3576 tree
3577 current_namespace_id (name)
3578 tree name;
3579 {
3580 tree old_id = get_namespace_id ();
3581 char *buf;
3582
3583 /* Global names retain old encoding. */
3584 if (! old_id)
3585 return name;
3586
3587 buf = (char *) alloca (8 + IDENTIFIER_LENGTH (old_id)
3588 + IDENTIFIER_LENGTH (name));
3589 sprintf (buf, "__ns_%s_%s", IDENTIFIER_POINTER (old_id),
3590 IDENTIFIER_POINTER (name));
3591 return get_identifier (buf);
3592 }
3593
3594 void
3595 do_namespace_alias (alias, namespace)
3596 tree alias, namespace;
3597 {
3598 sorry ("namespace alias");
3599 }
3600
3601 void
3602 do_toplevel_using_decl (decl)
3603 tree decl;
3604 {
3605 if (decl == NULL_TREE || decl == error_mark_node)
3606 return;
3607
3608 if (TREE_CODE (decl) == SCOPE_REF)
3609 decl = resolve_scope_to_name (NULL_TREE, decl);
3610
3611 /* Is this the right way to do an id list? */
3612 if (TREE_CODE (decl) != TREE_LIST)
3613 {
3614 pushdecl (decl);
3615 }
3616 else
3617 while (decl)
3618 {
3619 pushdecl (TREE_VALUE (decl));
3620 decl = TREE_CHAIN (decl);
3621 }
3622 }
3623
3624 tree
3625 do_class_using_decl (decl)
3626 tree decl;
3627 {
3628 tree name, value;
3629
3630 if (TREE_CODE (decl) != SCOPE_REF)
3631 {
3632 cp_error ("using-declaration for non-member at class scope");
3633 return NULL_TREE;
3634 }
3635 name = TREE_OPERAND (decl, 1);
3636 if (TREE_CODE (name) == BIT_NOT_EXPR)
3637 {
3638 cp_error ("using-declaration for destructor");
3639 return NULL_TREE;
3640 }
3641
3642 value = build_lang_field_decl (USING_DECL, name, void_type_node);
3643 DECL_INITIAL (value) = TREE_OPERAND (decl, 0);
3644 return value;
3645 }
3646
3647 void
3648 do_using_directive (namespace)
3649 tree namespace;
3650 {
3651 sorry ("using directive");
3652 }
3653
3654 void
3655 check_default_args (x)
3656 tree x;
3657 {
3658 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
3659 int saw_def = 0, i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
3660 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
3661 {
3662 if (TREE_PURPOSE (arg))
3663 saw_def = 1;
3664 else if (saw_def)
3665 {
3666 cp_error ("default argument missing for parameter %P of `%#D'",
3667 i, x);
3668 break;
3669 }
3670 }
3671 }
3672
3673 void
3674 mark_used (decl)
3675 tree decl;
3676 {
3677 TREE_USED (decl) = 1;
3678 if (processing_template_decl)
3679 return;
3680 assemble_external (decl);
3681 /* Is it a synthesized method that needs to be synthesized? */
3682 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_CLASS_CONTEXT (decl)
3683 && DECL_ARTIFICIAL (decl) && ! DECL_INITIAL (decl)
3684 /* Kludge: don't synthesize for default args. */
3685 && current_function_decl)
3686 synthesize_method (decl);
3687 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
3688 instantiate_decl (decl);
3689 }