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