OSDN Git Service

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