]>
Commit | Line | Data |
---|---|---|
3fd5abcf | 1 | /* Process declarations and variables for C++ compiler. |
d363e7bf | 2 | Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, |
3fd5abcf | 3 | 2001, 2002, 2003 Free Software Foundation, Inc. |
e5e809f4 | 4 | Contributed by Michael Tiemann (tiemann@cygnus.com) |
8d08fdba | 5 | |
1c313945 | 6 | This file is part of GCC. |
8d08fdba | 7 | |
1c313945 | 8 | GCC is free software; you can redistribute it and/or modify |
8d08fdba MS |
9 | it under the terms of the GNU General Public License as published by |
10 | the Free Software Foundation; either version 2, or (at your option) | |
11 | any later version. | |
12 | ||
1c313945 | 13 | GCC is distributed in the hope that it will be useful, |
8d08fdba MS |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
1c313945 | 19 | along with GCC; see the file COPYING. If not, write to |
e9fa0c7c RK |
20 | the Free Software Foundation, 59 Temple Place - Suite 330, |
21 | Boston, MA 02111-1307, USA. */ | |
8d08fdba MS |
22 | |
23 | ||
1c313945 | 24 | /* Process declarations and symbol lookup for C++ front end. |
8d08fdba MS |
25 | Also constructs types; the standard scalar types at initialization, |
26 | and structure, union, array and enum types when they are declared. */ | |
27 | ||
28 | /* ??? not all decl nodes are given the most useful possible | |
29 | line numbers. For example, the CONST_DECLs for enum values. */ | |
30 | ||
8d08fdba | 31 | #include "config.h" |
8d052bc7 | 32 | #include "system.h" |
4977bab6 ZW |
33 | #include "coretypes.h" |
34 | #include "tm.h" | |
8d08fdba MS |
35 | #include "tree.h" |
36 | #include "rtl.h" | |
3bdf5ad1 | 37 | #include "expr.h" |
8d08fdba MS |
38 | #include "flags.h" |
39 | #include "cp-tree.h" | |
25af8512 | 40 | #include "tree-inline.h" |
8d08fdba MS |
41 | #include "decl.h" |
42 | #include "lex.h" | |
49c249e1 JM |
43 | #include "output.h" |
44 | #include "except.h" | |
54f92bfb | 45 | #include "toplev.h" |
e2500fed | 46 | #include "hashtab.h" |
0e9295cf | 47 | #include "tm_p.h" |
672a6f42 | 48 | #include "target.h" |
26f943fd | 49 | #include "c-common.h" |
ecb0eece | 50 | #include "c-pragma.h" |
7437519c | 51 | #include "diagnostic.h" |
e2500fed | 52 | #include "debug.h" |
22ffcc6f | 53 | #include "timevar.h" |
8d08fdba | 54 | |
11f6b451 NN |
55 | static tree grokparms (tree); |
56 | static const char *redeclaration_error_message (tree, tree); | |
57 | ||
82a2669e | 58 | static void push_binding_level (cxx_scope *); |
11f6b451 NN |
59 | static void pop_binding_level (void); |
60 | static void suspend_binding_level (void); | |
61 | static void resume_binding_level (struct cp_binding_level *); | |
11f6b451 NN |
62 | static int decl_jump_unsafe (tree); |
63 | static void storedecls (tree); | |
64 | static void require_complete_types_for_parms (tree); | |
65 | static int ambi_op_p (enum tree_code); | |
66 | static int unary_op_p (enum tree_code); | |
000d38ea | 67 | static cxx_saved_binding *store_bindings (tree, cxx_saved_binding *); |
11f6b451 NN |
68 | static tree lookup_tag_reverse (tree, tree); |
69 | static void push_local_name (tree); | |
70 | static void warn_extern_redeclared_static (tree, tree); | |
71 | static tree grok_reference_init (tree, tree, tree); | |
72 | static tree grokfndecl (tree, tree, tree, tree, int, | |
73 | enum overload_flags, tree, | |
74 | tree, int, int, int, int, int, int, tree); | |
75 | static tree grokvardecl (tree, tree, RID_BIT_TYPE *, int, int, tree); | |
76 | static tree follow_tag_typedef (tree); | |
77 | static tree lookup_tag (enum tree_code, tree, | |
78 | struct cp_binding_level *, int); | |
49c249e1 | 79 | static void set_identifier_type_value_with_scope |
11f6b451 NN |
80 | (tree, tree, struct cp_binding_level *); |
81 | static void record_unknown_type (tree, const char *); | |
82 | static tree builtin_function_1 (const char *, tree, tree, int, | |
83 | enum built_in_class, const char *, | |
84 | tree); | |
85 | static tree build_library_fn_1 (tree, enum tree_code, tree); | |
86 | static int member_function_or_else (tree, tree, enum overload_flags); | |
87 | static void bad_specifiers (tree, const char *, int, int, int, int, | |
88 | int); | |
89 | static tree maybe_process_template_type_declaration | |
90 | (tree, int, struct cp_binding_level*); | |
91 | static void check_for_uninitialized_const_var (tree); | |
92 | static hashval_t typename_hash (const void *); | |
93 | static int typename_compare (const void *, const void *); | |
94 | static void push_binding (tree, tree, struct cp_binding_level*); | |
95 | static int add_binding (tree, tree); | |
96 | static void pop_binding (tree, tree); | |
97 | static tree local_variable_p_walkfn (tree *, int *, void *); | |
af6fd53f | 98 | static tree select_decl (cxx_binding *, int); |
11f6b451 NN |
99 | static int lookup_flags (int, int); |
100 | static tree qualify_lookup (tree, int); | |
101 | static tree record_builtin_java_type (const char *, int); | |
102 | static const char *tag_name (enum tag_types code); | |
103 | static void find_class_binding_level (void); | |
104 | static struct cp_binding_level *innermost_nonclass_level (void); | |
11f6b451 NN |
105 | static int walk_namespaces_r (tree, walk_namespaces_fn, void *); |
106 | static int walk_globals_r (tree, void*); | |
107 | static int walk_vtables_r (tree, void*); | |
108 | static void add_decl_to_level (tree, struct cp_binding_level *); | |
109 | static tree make_label_decl (tree, int); | |
110 | static void use_label (tree); | |
111 | static void check_previous_goto_1 (tree, struct cp_binding_level *, tree, | |
152c16a9 | 112 | const location_t *); |
11f6b451 NN |
113 | static void check_previous_goto (struct named_label_use_list *); |
114 | static void check_switch_goto (struct cp_binding_level *); | |
115 | static void check_previous_gotos (tree); | |
116 | static void pop_label (tree, tree); | |
117 | static void pop_labels (tree); | |
118 | static void maybe_deduce_size_from_array_init (tree, tree); | |
119 | static void layout_var_decl (tree); | |
120 | static void maybe_commonize_var (tree); | |
8e3df2de | 121 | static tree check_initializer (tree, tree, int); |
11f6b451 NN |
122 | static void make_rtl_for_nonlocal_decl (tree, tree, const char *); |
123 | static void save_function_data (tree); | |
124 | static void check_function_type (tree, tree); | |
125 | static void begin_constructor_body (void); | |
126 | static void finish_constructor_body (void); | |
127 | static void begin_destructor_body (void); | |
128 | static void finish_destructor_body (void); | |
129 | static tree create_array_type_for_decl (tree, tree, tree); | |
130 | static tree get_atexit_node (void); | |
131 | static tree get_dso_handle_node (void); | |
132 | static tree start_cleanup_fn (void); | |
133 | static void end_cleanup_fn (void); | |
134 | static tree cp_make_fname_decl (tree, int); | |
135 | static void initialize_predefined_identifiers (void); | |
136 | static tree check_special_function_return_type | |
137 | (special_function_kind, tree, tree); | |
138 | static tree push_cp_library_fn (enum tree_code, tree); | |
139 | static tree build_cp_library_fn (tree, enum tree_code, tree); | |
140 | static void store_parm_decls (tree); | |
141 | static int cp_missing_noreturn_ok_p (tree); | |
8e3df2de MM |
142 | static void initialize_local_var (tree, tree); |
143 | static void expand_static_init (tree, tree); | |
144 | static tree next_initializable_field (tree); | |
145 | static tree reshape_init (tree, tree *); | |
8fbc5ae7 | 146 | static tree build_typename_type (tree, tree, tree); |
8d08fdba | 147 | |
8d08fdba MS |
148 | /* Erroneous argument lists can use this *IFF* they do not modify it. */ |
149 | tree error_mark_list; | |
150 | ||
7f4edbcb | 151 | /* The following symbols are subsumed in the cp_global_trees array, and |
68642fb6 | 152 | listed here individually for documentation purposes. |
8d08fdba | 153 | |
7f4edbcb BS |
154 | C++ extensions |
155 | tree wchar_decl_node; | |
37c46b43 | 156 | |
7f4edbcb BS |
157 | tree vtable_entry_type; |
158 | tree delta_type_node; | |
7f4edbcb | 159 | tree __t_desc_type_node; |
db1147b2 | 160 | tree ti_desc_type_node; |
1f4cb92b | 161 | tree bltn_desc_type_node, ptr_desc_type_node; |
db1147b2 NS |
162 | tree ary_desc_type_node, func_desc_type_node, enum_desc_type_node; |
163 | tree class_desc_type_node, si_class_desc_type_node, vmi_class_desc_type_node; | |
387769ed | 164 | tree ptm_desc_type_node; |
db1147b2 | 165 | tree base_desc_type_node; |
8d08fdba | 166 | |
7f4edbcb BS |
167 | tree class_type_node, record_type_node, union_type_node, enum_type_node; |
168 | tree unknown_type_node; | |
8d08fdba | 169 | |
7f4edbcb | 170 | Array type `vtable_entry_type[]' |
8d08fdba | 171 | |
7f4edbcb BS |
172 | tree vtbl_type_node; |
173 | tree vtbl_ptr_type_node; | |
8d08fdba | 174 | |
2854d3c6 | 175 | Namespaces, |
8d08fdba | 176 | |
7f4edbcb | 177 | tree std_node; |
2854d3c6 | 178 | tree abi_node; |
8d08fdba | 179 | |
7f4edbcb BS |
180 | A FUNCTION_DECL which can call `abort'. Not necessarily the |
181 | one that the user will declare, but sufficient to be called | |
182 | by routines that want to abort the program. | |
8d08fdba | 183 | |
7f4edbcb | 184 | tree abort_fndecl; |
8d08fdba | 185 | |
7f4edbcb | 186 | The FUNCTION_DECL for the default `::operator delete'. |
2986ae00 | 187 | |
7f4edbcb | 188 | tree global_delete_fndecl; |
8d08fdba | 189 | |
7f4edbcb | 190 | Used by RTTI |
669ec2b4 JM |
191 | tree type_info_type_node, tinfo_decl_id, tinfo_decl_type; |
192 | tree tinfo_var_id; | |
193 | ||
7f4edbcb | 194 | */ |
8d08fdba | 195 | |
7f4edbcb | 196 | tree cp_global_trees[CPTI_MAX]; |
8d08fdba | 197 | |
2c73f9f5 | 198 | /* Indicates that there is a type value in some namespace, although |
7f4edbcb | 199 | that is not necessarily in scope at the moment. */ |
2c73f9f5 | 200 | |
e2500fed | 201 | static GTY(()) tree global_type_node; |
2c73f9f5 | 202 | |
82a2669e GDR |
203 | /* The node that holds the "name" of the global scope. */ |
204 | static GTY(()) tree global_scope_name; | |
205 | ||
6625cdb5 JM |
206 | /* Used only for jumps to as-yet undefined labels, since jumps to |
207 | defined labels can have their validity checked immediately. */ | |
208 | ||
e2500fed | 209 | struct named_label_use_list GTY(()) |
e349ee73 | 210 | { |
e2500fed | 211 | struct cp_binding_level *binding_level; |
e349ee73 MS |
212 | tree names_in_scope; |
213 | tree label_decl; | |
152c16a9 | 214 | location_t o_goto_locus; |
6625cdb5 | 215 | struct named_label_use_list *next; |
e349ee73 MS |
216 | }; |
217 | ||
ed5511d9 | 218 | #define named_label_uses cp_function_chain->x_named_label_uses |
8d08fdba | 219 | |
8e4ce833 JJ |
220 | #define local_names cp_function_chain->x_local_names |
221 | ||
8d08fdba MS |
222 | /* A list of objects which have constructors or destructors |
223 | which reside in the global scope. The decl is stored in | |
224 | the TREE_VALUE slot and the initializer is stored | |
225 | in the TREE_PURPOSE slot. */ | |
226 | tree static_aggregates; | |
227 | ||
8d08fdba MS |
228 | /* -- end of C++ */ |
229 | ||
81b3411c | 230 | /* A node for the integer constants 2, and 3. */ |
d11ad92e | 231 | |
81b3411c | 232 | tree integer_two_node, integer_three_node; |
8d08fdba | 233 | |
8d08fdba | 234 | /* Similar, for last_function_parm_tags. */ |
9cd64686 | 235 | tree last_function_parms; |
8d08fdba | 236 | |
6625cdb5 JM |
237 | /* A list of all LABEL_DECLs in the function that have names. Here so |
238 | we can clear out their names' definitions at the end of the | |
239 | function, and so we can check the validity of jumps to these labels. */ | |
240 | ||
e2500fed | 241 | struct named_label_list GTY(()) |
6625cdb5 | 242 | { |
e2500fed | 243 | struct cp_binding_level *binding_level; |
6625cdb5 JM |
244 | tree names_in_scope; |
245 | tree old_value; | |
246 | tree label_decl; | |
247 | tree bad_decls; | |
6625cdb5 | 248 | struct named_label_list *next; |
826840d9 RH |
249 | unsigned int in_try_scope : 1; |
250 | unsigned int in_catch_scope : 1; | |
6625cdb5 | 251 | }; |
8d08fdba | 252 | |
4519c0a8 | 253 | #define named_labels cp_function_chain->x_named_labels |
8d08fdba | 254 | \f |
0c8feefe MM |
255 | /* The name of the anonymous namespace, throughout this translation |
256 | unit. */ | |
257 | tree anonymous_namespace_name; | |
258 | ||
b2244c65 MM |
259 | /* The number of function bodies which we are currently processing. |
260 | (Zero if we are at namespace scope, one inside the body of a | |
261 | function, two inside the body of a function in a local class, etc.) */ | |
262 | int function_depth; | |
e23bd218 IR |
263 | |
264 | /* States indicating how grokdeclarator() should handle declspecs marked | |
265 | with __attribute__((deprecated)). An object declared as | |
266 | __attribute__((deprecated)) suppresses warnings of uses of other | |
267 | deprecated items. */ | |
268 | ||
269 | enum deprecated_states { | |
270 | DEPRECATED_NORMAL, | |
271 | DEPRECATED_SUPPRESS | |
272 | }; | |
273 | ||
274 | static enum deprecated_states deprecated_state = DEPRECATED_NORMAL; | |
275 | ||
276 | /* Set by add_implicitly_declared_members() to keep those members from | |
277 | being flagged as deprecated or reported as using deprecated | |
278 | types. */ | |
279 | int adding_implicit_members = 0; | |
594bb0e7 MM |
280 | |
281 | /* True if a declaration with an `extern' linkage specifier is being | |
282 | processed. */ | |
283 | bool have_extern_spec; | |
284 | ||
8d08fdba MS |
285 | \f |
286 | /* For each binding contour we allocate a binding_level structure | |
e92cc029 MS |
287 | which records the names defined in that contour. |
288 | Contours include: | |
289 | 0) the global one | |
290 | 1) one for each function definition, | |
291 | where internal declarations of the parameters appear. | |
292 | 2) one for each compound statement, | |
293 | to record its declarations. | |
294 | ||
295 | The current meaning of a name can be found by searching the levels | |
296 | from the current one out to the global one. | |
297 | ||
298 | Off to the side, may be the class_binding_level. This exists only | |
299 | to catch class-local declarations. It is otherwise nonexistent. | |
300 | ||
301 | Also there may be binding levels that catch cleanups that must be | |
d8f8dca1 MM |
302 | run when exceptions occur. Thus, to see whether a name is bound in |
303 | the current scope, it is not enough to look in the | |
304 | CURRENT_BINDING_LEVEL. You should use lookup_name_current_level | |
305 | instead. */ | |
8d08fdba MS |
306 | |
307 | /* Note that the information in the `names' component of the global contour | |
308 | is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */ | |
309 | ||
e2500fed | 310 | struct cp_binding_level GTY(()) |
8d08fdba MS |
311 | { |
312 | /* A chain of _DECL nodes for all variables, constants, functions, | |
e92cc029 | 313 | and typedef types. These are in the reverse of the order |
f181d4ae MM |
314 | supplied. There may be OVERLOADs on this list, too, but they |
315 | are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD. */ | |
8d08fdba MS |
316 | tree names; |
317 | ||
70b76b34 DP |
318 | /* Count of elements in names chain. */ |
319 | size_t names_size; | |
320 | ||
321 | /* A chain of NAMESPACE_DECL nodes. */ | |
322 | tree namespaces; | |
323 | ||
1d555f7a MA |
324 | /* An array of static functions and variables (for namespaces only) */ |
325 | varray_type static_decls; | |
326 | ||
70b76b34 DP |
327 | /* A chain of VTABLE_DECL nodes. */ |
328 | tree vtables; | |
329 | ||
5e0c54e5 GDR |
330 | /* A dictionary for looking up user-defined-types. */ |
331 | binding_table type_decls; | |
8d08fdba | 332 | |
a1c65f9f | 333 | /* A list of USING_DECL nodes. */ |
2c73f9f5 ML |
334 | tree usings; |
335 | ||
ea9635c7 | 336 | /* A list of used namespaces. PURPOSE is the namespace, |
a1c65f9f | 337 | VALUE the common ancestor with this binding_level's namespace. */ |
ea9635c7 ML |
338 | tree using_directives; |
339 | ||
f181d4ae MM |
340 | /* If this binding level is the binding level for a class, then |
341 | class_shadowed is a TREE_LIST. The TREE_PURPOSE of each node | |
76191fdd | 342 | is the name of an entity bound in the class. The TREE_TYPE is |
d8f8dca1 | 343 | the DECL bound by this name in the class. */ |
8d08fdba MS |
344 | tree class_shadowed; |
345 | ||
f181d4ae | 346 | /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and |
76191fdd NS |
347 | is used for all binding levels. In addition the TREE_VALUE is the |
348 | IDENTIFIER_TYPE_VALUE before we entered the class. */ | |
8d08fdba MS |
349 | tree type_shadowed; |
350 | ||
acef433b MM |
351 | /* A TREE_LIST. Each TREE_VALUE is the LABEL_DECL for a local |
352 | label in this scope. The TREE_PURPOSE is the previous value of | |
353 | the IDENTIFIER_LABEL VALUE. */ | |
354 | tree shadowed_labels; | |
355 | ||
8d08fdba MS |
356 | /* For each level (except not the global one), |
357 | a chain of BLOCK nodes for all the levels | |
358 | that were entered and exited one level down. */ | |
359 | tree blocks; | |
360 | ||
82a2669e GDR |
361 | /* The entity (namespace, class, function) the scope of which this |
362 | binding contour corresponds to. Otherwise NULL. */ | |
363 | tree this_entity; | |
70adf8a9 | 364 | |
8d08fdba | 365 | /* The binding level which this one is contained in (inherits from). */ |
e2500fed | 366 | struct cp_binding_level *level_chain; |
8d08fdba | 367 | |
8d6e462b | 368 | /* List of VAR_DECLS saved from a previous for statement. |
cab1f180 | 369 | These would be dead in ISO-conforming code, but might |
f181d4ae MM |
370 | be referenced in ARM-era code. These are stored in a |
371 | TREE_LIST; the TREE_VALUE is the actual declaration. */ | |
8d6e462b PB |
372 | tree dead_vars_from_for; |
373 | ||
8d08fdba | 374 | /* 1 for the level that holds the parameters of a function. |
74b846e0 MM |
375 | 2 for the level that holds a class declaration. */ |
376 | unsigned parm_flag : 2; | |
8d08fdba MS |
377 | |
378 | /* 1 means make a BLOCK for this level regardless of all else. | |
379 | 2 for temporary binding contours created by the compiler. */ | |
74b846e0 | 380 | unsigned keep : 2; |
8d08fdba MS |
381 | |
382 | /* Nonzero if this level "doesn't exist" for tags. */ | |
383 | unsigned tag_transparent : 1; | |
384 | ||
385 | /* Nonzero if this level can safely have additional | |
386 | cleanup-needing variables added to it. */ | |
387 | unsigned more_cleanups_ok : 1; | |
388 | unsigned have_cleanups : 1; | |
389 | ||
74b846e0 | 390 | /* Nonzero if this scope is for storing the decls for template |
5566b478 MS |
391 | parameters and generic decls; these decls will be discarded and |
392 | replaced with a TEMPLATE_DECL. */ | |
74b846e0 MM |
393 | unsigned template_parms_p : 1; |
394 | ||
5362b086 | 395 | /* Nonzero if this scope corresponds to the `<>' in a |
74b846e0 MM |
396 | `template <>' clause. Whenever this flag is set, |
397 | TEMPLATE_PARMS_P will be set as well. */ | |
398 | unsigned template_spec_p : 1; | |
8d08fdba | 399 | |
a9aedbc2 MS |
400 | /* This is set for a namespace binding level. */ |
401 | unsigned namespace_p : 1; | |
402 | ||
2ee887f2 | 403 | /* True if this level is that of a for-statement where we need to |
cab1f180 | 404 | worry about ambiguous (ARM or ISO) scope rules. */ |
8d6e462b PB |
405 | unsigned is_for_scope : 1; |
406 | ||
826840d9 RH |
407 | /* True if this level corresponds to a TRY block. Currently this |
408 | information is only available while building the tree structure. */ | |
409 | unsigned is_try_scope : 1; | |
227cf171 | 410 | |
826840d9 RH |
411 | /* True if this level corresponds to a CATCH block. Currently this |
412 | information is only available while building the tree structure. */ | |
413 | unsigned is_catch_scope : 1; | |
414 | ||
415 | /* Three bits left for this word. */ | |
8d08fdba | 416 | |
8d08fdba MS |
417 | /* Binding depth at which this level began. */ |
418 | unsigned binding_depth; | |
8d08fdba MS |
419 | }; |
420 | ||
e2500fed | 421 | #define NULL_BINDING_LEVEL ((struct cp_binding_level *) NULL) |
68642fb6 | 422 | |
8f032717 | 423 | /* The binding level currently in effect. */ |
8d08fdba | 424 | |
a8f73d4b | 425 | #define current_binding_level \ |
ff955512 KG |
426 | (*(cfun && cp_function_chain->bindings \ |
427 | ? &cp_function_chain->bindings \ | |
428 | : &scope_chain->bindings)) | |
de94b46c | 429 | |
8d08fdba MS |
430 | /* The binding level of the current class, if any. */ |
431 | ||
9cd64686 | 432 | #define class_binding_level scope_chain->class_bindings |
8d08fdba | 433 | |
8d08fdba MS |
434 | /* A chain of binding_level structures awaiting reuse. */ |
435 | ||
e2500fed | 436 | static GTY((deletable (""))) struct cp_binding_level *free_binding_level; |
8d08fdba | 437 | |
8d08fdba MS |
438 | /* Nonzero means unconditionally make a BLOCK for the next level pushed. */ |
439 | ||
440 | static int keep_next_level_flag; | |
441 | ||
0154eaa8 MM |
442 | /* A TREE_LIST of VAR_DECLs. The TREE_PURPOSE is a RECORD_TYPE or |
443 | UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type. At the | |
444 | time the VAR_DECL was declared, the type was incomplete. */ | |
445 | ||
e2500fed | 446 | static GTY(()) tree incomplete_vars; |
0154eaa8 | 447 | |
f37e2274 GDR |
448 | #ifndef ENABLE_SCOPE_CHECKING |
449 | # define ENABLE_SCOPE_CHECKING 0 | |
450 | #else | |
451 | # define ENABLE_SCOPE_CHECKING 1 | |
452 | #endif | |
453 | ||
8d08fdba MS |
454 | static int binding_depth = 0; |
455 | static int is_class_level = 0; | |
456 | ||
457 | static void | |
f37e2274 | 458 | indent (int depth) |
8d08fdba | 459 | { |
f37e2274 | 460 | int i; |
8d08fdba | 461 | |
f37e2274 | 462 | for (i = 0; i < depth * 2; i++) |
8d08fdba MS |
463 | putc (' ', stderr); |
464 | } | |
8d08fdba | 465 | |
11f6b451 | 466 | static tree pushdecl_with_scope (tree, struct cp_binding_level *); |
8d08fdba | 467 | |
82a2669e GDR |
468 | /* Return a string describing the kind of SCOPE we have. */ |
469 | static const char * | |
470 | cxx_scope_descriptor (cxx_scope *scope) | |
471 | { | |
472 | const char *desc; | |
473 | ||
474 | if (scope->namespace_p) | |
475 | desc = "namespace-scope"; | |
476 | else if (scope->parm_flag == 1) | |
477 | desc = "function-prototype-scope"; | |
478 | else if (scope->parm_flag == 2) | |
479 | desc = "class-scope"; | |
480 | else if (scope->is_for_scope) | |
481 | desc = "for-scope"; | |
482 | else if (scope->is_try_scope) | |
483 | desc = "try-scope"; | |
484 | else if (scope->is_catch_scope) | |
485 | desc = "catch-scope"; | |
486 | else if (scope->template_spec_p) | |
487 | desc = "template-explicit-spec-scope"; | |
488 | else if (scope->template_parms_p) | |
489 | desc = "template-prototype-scope"; | |
490 | else | |
491 | desc = "block-scope"; | |
492 | ||
493 | return desc; | |
494 | } | |
495 | ||
496 | /* Output a debugging information about SCOPE when performning | |
497 | ACTION at LINE. */ | |
8d08fdba | 498 | static void |
82a2669e GDR |
499 | cxx_scope_debug (cxx_scope *scope, int line, const char *action) |
500 | { | |
501 | const char *desc = cxx_scope_descriptor (scope); | |
502 | if (scope->this_entity) | |
503 | verbatim ("%s %s(%E) %p %d\n", action, desc, | |
504 | scope->this_entity, (void *) scope, line); | |
505 | else | |
506 | verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line); | |
507 | } | |
508 | ||
509 | /* Construct a scope that may be TAG-TRANSPARENT, the sub-blocks of | |
510 | which may be KEPT. */ | |
511 | static inline cxx_scope * | |
512 | make_cxx_scope (bool tag_transparent, int keep) | |
513 | { | |
514 | cxx_scope *scope; | |
515 | ||
516 | /* Reuse or create a struct for this binding level. */ | |
517 | if (!ENABLE_SCOPE_CHECKING && free_binding_level) | |
518 | { | |
519 | scope = free_binding_level; | |
520 | free_binding_level = scope->level_chain; | |
521 | } | |
522 | else | |
523 | scope = ggc_alloc (sizeof (cxx_scope)); | |
524 | ||
525 | memset (scope, 0, sizeof (cxx_scope)); | |
526 | scope->tag_transparent = tag_transparent; | |
527 | scope->keep = keep; | |
528 | scope->more_cleanups_ok = true; | |
529 | ||
530 | return scope; | |
531 | } | |
532 | ||
533 | static void | |
534 | push_binding_level (cxx_scope *newlevel) | |
8d08fdba MS |
535 | { |
536 | /* Add this level to the front of the chain (stack) of levels that | |
537 | are active. */ | |
8f032717 | 538 | newlevel->level_chain = current_binding_level; |
ff955512 | 539 | current_binding_level = newlevel; |
227cf171 | 540 | |
f37e2274 GDR |
541 | if (ENABLE_SCOPE_CHECKING) |
542 | { | |
543 | newlevel->binding_depth = binding_depth; | |
544 | indent (binding_depth); | |
82a2669e | 545 | cxx_scope_debug (newlevel, input_location.line, "push"); |
f37e2274 GDR |
546 | is_class_level = 0; |
547 | binding_depth++; | |
548 | } | |
8d08fdba MS |
549 | } |
550 | ||
8f032717 MM |
551 | /* Find the innermost enclosing class scope, and reset |
552 | CLASS_BINDING_LEVEL appropriately. */ | |
553 | ||
554 | static void | |
11f6b451 | 555 | find_class_binding_level (void) |
8f032717 | 556 | { |
e2500fed | 557 | struct cp_binding_level *level = current_binding_level; |
8f032717 MM |
558 | |
559 | while (level && level->parm_flag != 2) | |
560 | level = level->level_chain; | |
561 | if (level && level->parm_flag == 2) | |
562 | class_binding_level = level; | |
563 | else | |
564 | class_binding_level = 0; | |
565 | } | |
566 | ||
8d08fdba | 567 | static void |
11f6b451 | 568 | pop_binding_level (void) |
8d08fdba | 569 | { |
5f52c0e0 GDR |
570 | if (NAMESPACE_LEVEL (global_namespace)) |
571 | /* Cannot pop a level, if there are none left to pop. */ | |
572 | my_friendly_assert (!global_scope_p (current_binding_level), 20030527); | |
8d08fdba | 573 | /* Pop the current level, and free the structure for reuse. */ |
f37e2274 | 574 | if (ENABLE_SCOPE_CHECKING) |
9e9ff709 | 575 | { |
f37e2274 | 576 | indent (--binding_depth); |
82a2669e | 577 | cxx_scope_debug (current_binding_level, input_location.line, "pop"); |
f37e2274 GDR |
578 | if (is_class_level != (current_binding_level == class_binding_level)) |
579 | { | |
580 | indent (binding_depth); | |
581 | verbatim ("XXX is_class_level != (current_binding_level " | |
582 | "== class_binding_level)\n"); | |
583 | } | |
584 | is_class_level = 0; | |
9e9ff709 | 585 | } |
8d08fdba | 586 | { |
e2500fed | 587 | register struct cp_binding_level *level = current_binding_level; |
ff955512 | 588 | current_binding_level = current_binding_level->level_chain; |
8d08fdba | 589 | level->level_chain = free_binding_level; |
5e0c54e5 GDR |
590 | if (level->parm_flag == 2) |
591 | level->type_decls = NULL; | |
592 | else | |
593 | binding_table_free (level->type_decls); | |
f37e2274 GDR |
594 | my_friendly_assert (!ENABLE_SCOPE_CHECKING |
595 | || level->binding_depth == binding_depth, | |
596 | 20030529); | |
8f032717 MM |
597 | free_binding_level = level; |
598 | find_class_binding_level (); | |
8d08fdba MS |
599 | } |
600 | } | |
a9aedbc2 MS |
601 | |
602 | static void | |
11f6b451 | 603 | suspend_binding_level (void) |
a9aedbc2 MS |
604 | { |
605 | if (class_binding_level) | |
ff955512 | 606 | current_binding_level = class_binding_level; |
a9aedbc2 | 607 | |
5f52c0e0 GDR |
608 | if (NAMESPACE_LEVEL (global_namespace)) |
609 | /* Cannot suspend a level, if there are none left to suspend. */ | |
610 | my_friendly_assert (!global_scope_p (current_binding_level), 20030527); | |
a9aedbc2 | 611 | /* Suspend the current level. */ |
f37e2274 | 612 | if (ENABLE_SCOPE_CHECKING) |
9e9ff709 | 613 | { |
f37e2274 | 614 | indent (--binding_depth); |
82a2669e | 615 | cxx_scope_debug (current_binding_level, input_location.line, "suspend"); |
f37e2274 GDR |
616 | if (is_class_level != (current_binding_level == class_binding_level)) |
617 | { | |
618 | indent (binding_depth); | |
619 | verbatim ("XXX is_class_level != (current_binding_level " | |
620 | "== class_binding_level)\n"); | |
621 | } | |
622 | is_class_level = 0; | |
9e9ff709 | 623 | } |
ff955512 | 624 | current_binding_level = current_binding_level->level_chain; |
8f032717 | 625 | find_class_binding_level (); |
a9aedbc2 MS |
626 | } |
627 | ||
824b9a4c | 628 | static void |
11f6b451 | 629 | resume_binding_level (struct cp_binding_level* b) |
a9aedbc2 | 630 | { |
2c73f9f5 | 631 | /* Resuming binding levels is meant only for namespaces, |
a1c65f9f | 632 | and those cannot nest into classes. */ |
2c73f9f5 ML |
633 | my_friendly_assert(!class_binding_level, 386); |
634 | /* Also, resuming a non-directly nested namespace is a no-no. */ | |
635 | my_friendly_assert(b->level_chain == current_binding_level, 386); | |
ff955512 | 636 | current_binding_level = b; |
f37e2274 GDR |
637 | if (ENABLE_SCOPE_CHECKING) |
638 | { | |
639 | b->binding_depth = binding_depth; | |
640 | indent (binding_depth); | |
82a2669e | 641 | cxx_scope_debug (b, input_location.line, "resume"); |
f37e2274 GDR |
642 | is_class_level = 0; |
643 | binding_depth++; | |
644 | } | |
a9aedbc2 | 645 | } |
8d08fdba MS |
646 | \f |
647 | /* Nonzero if we are currently in the global binding level. */ | |
648 | ||
649 | int | |
11f6b451 | 650 | global_bindings_p (void) |
8d08fdba | 651 | { |
5f52c0e0 | 652 | return global_scope_p (current_binding_level); |
8d08fdba MS |
653 | } |
654 | ||
8f032717 MM |
655 | /* Return the innermost binding level that is not for a class scope. */ |
656 | ||
e2500fed | 657 | static struct cp_binding_level * |
11f6b451 | 658 | innermost_nonclass_level (void) |
8f032717 | 659 | { |
e2500fed | 660 | struct cp_binding_level *b; |
8f032717 MM |
661 | |
662 | b = current_binding_level; | |
663 | while (b->parm_flag == 2) | |
664 | b = b->level_chain; | |
665 | ||
666 | return b; | |
667 | } | |
668 | ||
a9aedbc2 MS |
669 | /* Nonzero if we are currently in a toplevel binding level. This |
670 | means either the global binding level or a namespace in a toplevel | |
8f032717 | 671 | binding level. Since there are no non-toplevel namespace levels, |
74b846e0 MM |
672 | this really means any namespace or template parameter level. We |
673 | also include a class whose context is toplevel. */ | |
a9aedbc2 MS |
674 | |
675 | int | |
11f6b451 | 676 | toplevel_bindings_p (void) |
a9aedbc2 | 677 | { |
e2500fed | 678 | struct cp_binding_level *b = innermost_nonclass_level (); |
8f032717 | 679 | |
74b846e0 | 680 | return b->namespace_p || b->template_parms_p; |
a9aedbc2 MS |
681 | } |
682 | ||
8f032717 MM |
683 | /* Nonzero if this is a namespace scope, or if we are defining a class |
684 | which is itself at namespace scope, or whose enclosing class is | |
685 | such a class, etc. */ | |
a9aedbc2 | 686 | |
7bdbfa05 | 687 | int |
11f6b451 | 688 | namespace_bindings_p (void) |
a9aedbc2 | 689 | { |
e2500fed | 690 | struct cp_binding_level *b = innermost_nonclass_level (); |
8f032717 MM |
691 | |
692 | return b->namespace_p; | |
a9aedbc2 MS |
693 | } |
694 | ||
838dfd8a | 695 | /* If KEEP is nonzero, make a BLOCK node for the next binding level, |
f1dedc31 MM |
696 | unconditionally. Otherwise, use the normal logic to decide whether |
697 | or not to create a BLOCK. */ | |
698 | ||
8d08fdba | 699 | void |
11f6b451 | 700 | keep_next_level (int keep) |
8d08fdba | 701 | { |
f1dedc31 | 702 | keep_next_level_flag = keep; |
8d08fdba MS |
703 | } |
704 | ||
705 | /* Nonzero if the current level needs to have a BLOCK made. */ | |
706 | ||
707 | int | |
11f6b451 | 708 | kept_level_p (void) |
8d08fdba MS |
709 | { |
710 | return (current_binding_level->blocks != NULL_TREE | |
711 | || current_binding_level->keep | |
712 | || current_binding_level->names != NULL_TREE | |
5e0c54e5 | 713 | || (current_binding_level->type_decls != NULL |
8d08fdba MS |
714 | && !current_binding_level->tag_transparent)); |
715 | } | |
716 | ||
838dfd8a | 717 | /* Returns nonzero if this scope was created to store template |
74b846e0 MM |
718 | parameters. */ |
719 | ||
8d08fdba | 720 | int |
11f6b451 | 721 | template_parm_scope_p (void) |
8d08fdba | 722 | { |
74b846e0 MM |
723 | return current_binding_level->template_parms_p; |
724 | } | |
725 | ||
726 | /* Returns the kind of template specialization we are currently | |
727 | processing, given that it's declaration contained N_CLASS_SCOPES | |
728 | explicit scope qualifications. */ | |
729 | ||
730 | tmpl_spec_kind | |
11f6b451 | 731 | current_tmpl_spec_kind (int n_class_scopes) |
74b846e0 MM |
732 | { |
733 | int n_template_parm_scopes = 0; | |
734 | int seen_specialization_p = 0; | |
735 | int innermost_specialization_p = 0; | |
e2500fed | 736 | struct cp_binding_level *b; |
74b846e0 MM |
737 | |
738 | /* Scan through the template parameter scopes. */ | |
739 | for (b = current_binding_level; b->template_parms_p; b = b->level_chain) | |
740 | { | |
741 | /* If we see a specialization scope inside a parameter scope, | |
742 | then something is wrong. That corresponds to a declaration | |
743 | like: | |
744 | ||
745 | template <class T> template <> ... | |
746 | ||
0e339752 | 747 | which is always invalid since [temp.expl.spec] forbids the |
74b846e0 MM |
748 | specialization of a class member template if the enclosing |
749 | class templates are not explicitly specialized as well. */ | |
750 | if (b->template_spec_p) | |
751 | { | |
752 | if (n_template_parm_scopes == 0) | |
753 | innermost_specialization_p = 1; | |
754 | else | |
755 | seen_specialization_p = 1; | |
756 | } | |
757 | else if (seen_specialization_p == 1) | |
758 | return tsk_invalid_member_spec; | |
759 | ||
760 | ++n_template_parm_scopes; | |
761 | } | |
762 | ||
763 | /* Handle explicit instantiations. */ | |
764 | if (processing_explicit_instantiation) | |
765 | { | |
766 | if (n_template_parm_scopes != 0) | |
767 | /* We've seen a template parameter list during an explicit | |
768 | instantiation. For example: | |
769 | ||
770 | template <class T> template void f(int); | |
771 | ||
772 | This is erroneous. */ | |
773 | return tsk_invalid_expl_inst; | |
774 | else | |
775 | return tsk_expl_inst; | |
776 | } | |
777 | ||
778 | if (n_template_parm_scopes < n_class_scopes) | |
779 | /* We've not seen enough template headers to match all the | |
780 | specialized classes present. For example: | |
781 | ||
782 | template <class T> void R<T>::S<T>::f(int); | |
783 | ||
0e339752 | 784 | This is invalid; there needs to be one set of template |
74b846e0 MM |
785 | parameters for each class. */ |
786 | return tsk_insufficient_parms; | |
787 | else if (n_template_parm_scopes == n_class_scopes) | |
788 | /* We're processing a non-template declaration (even though it may | |
789 | be a member of a template class.) For example: | |
790 | ||
791 | template <class T> void S<T>::f(int); | |
792 | ||
793 | The `class T' maches the `S<T>', leaving no template headers | |
794 | corresponding to the `f'. */ | |
795 | return tsk_none; | |
796 | else if (n_template_parm_scopes > n_class_scopes + 1) | |
797 | /* We've got too many template headers. For example: | |
798 | ||
799 | template <> template <class T> void f (T); | |
800 | ||
801 | There need to be more enclosing classes. */ | |
802 | return tsk_excessive_parms; | |
803 | else | |
804 | /* This must be a template. It's of the form: | |
805 | ||
806 | template <class T> template <class U> void S<T>::f(U); | |
807 | ||
808 | This is a specialization if the innermost level was a | |
809 | specialization; otherwise it's just a definition of the | |
810 | template. */ | |
811 | return innermost_specialization_p ? tsk_expl_spec : tsk_template; | |
8d08fdba MS |
812 | } |
813 | ||
814 | void | |
11f6b451 | 815 | set_class_shadows (tree shadows) |
8d08fdba MS |
816 | { |
817 | class_binding_level->class_shadowed = shadows; | |
818 | } | |
819 | ||
820 | /* Enter a new binding level. | |
821 | If TAG_TRANSPARENT is nonzero, do so only for the name space of variables, | |
822 | not for that of tags. */ | |
823 | ||
824 | void | |
11f6b451 | 825 | pushlevel (int tag_transparent) |
8d08fdba | 826 | { |
01d939e8 | 827 | if (cfun && !doing_semantic_analysis_p ()) |
b35d4555 | 828 | return; |
8d08fdba | 829 | |
82a2669e | 830 | push_binding_level (make_cxx_scope (tag_transparent, keep_next_level_flag)); |
8d08fdba MS |
831 | keep_next_level_flag = 0; |
832 | } | |
833 | ||
bd0d5d4a JM |
834 | /* We're defining an object of type TYPE. If it needs a cleanup, but |
835 | we're not allowed to add any more objects with cleanups to the current | |
836 | scope, create a new binding level. */ | |
837 | ||
838 | void | |
11f6b451 | 839 | maybe_push_cleanup_level (tree type) |
bd0d5d4a JM |
840 | { |
841 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) | |
842 | && current_binding_level->more_cleanups_ok == 0) | |
843 | { | |
844 | keep_next_level (2); | |
845 | pushlevel (1); | |
846 | clear_last_expr (); | |
847 | add_scope_stmt (/*begin_p=*/1, /*partial_p=*/1); | |
848 | } | |
849 | } | |
850 | ||
74b846e0 MM |
851 | /* Enter a new scope. The KIND indicates what kind of scope is being |
852 | created. */ | |
853 | ||
854 | void | |
11f6b451 | 855 | begin_scope (scope_kind sk) |
74b846e0 MM |
856 | { |
857 | pushlevel (0); | |
858 | ||
859 | switch (sk) | |
860 | { | |
92bc1323 MM |
861 | case sk_block: |
862 | break; | |
863 | ||
864 | case sk_try: | |
865 | current_binding_level->is_try_scope = 1; | |
866 | break; | |
867 | ||
868 | case sk_catch: | |
869 | current_binding_level->is_catch_scope = 1; | |
870 | break; | |
871 | ||
872 | case sk_for: | |
873 | current_binding_level->is_for_scope = 1; | |
874 | break; | |
875 | ||
74b846e0 MM |
876 | case sk_template_spec: |
877 | current_binding_level->template_spec_p = 1; | |
878 | /* Fall through. */ | |
879 | ||
880 | case sk_template_parms: | |
881 | current_binding_level->template_parms_p = 1; | |
882 | break; | |
883 | ||
884 | default: | |
a98facb0 | 885 | abort (); |
74b846e0 MM |
886 | } |
887 | } | |
888 | ||
889 | /* Exit the current scope. */ | |
890 | ||
891 | void | |
11f6b451 | 892 | finish_scope (void) |
74b846e0 MM |
893 | { |
894 | poplevel (0, 0, 0); | |
895 | } | |
896 | ||
f181d4ae MM |
897 | /* Make DECL the innermost binding for ID. The LEVEL is the binding |
898 | level at which this declaration is being bound. */ | |
899 | ||
900 | static void | |
ed3cf953 | 901 | push_binding (tree id, tree decl, cxx_scope* level) |
f181d4ae | 902 | { |
aed81407 | 903 | cxx_binding *binding = cxx_binding_make (decl, NULL); |
f181d4ae MM |
904 | |
905 | /* Now, fill in the binding information. */ | |
af6fd53f | 906 | binding->previous = IDENTIFIER_BINDING (id); |
ed3cf953 | 907 | BINDING_SCOPE (binding) = level; |
8f032717 | 908 | INHERITED_VALUE_BINDING_P (binding) = 0; |
f181d4ae MM |
909 | LOCAL_BINDING_P (binding) = (level != class_binding_level); |
910 | ||
c45df9c1 | 911 | /* And put it on the front of the list of bindings for ID. */ |
f181d4ae MM |
912 | IDENTIFIER_BINDING (id) = binding; |
913 | } | |
914 | ||
d8f8dca1 MM |
915 | /* ID is already bound in the current scope. But, DECL is an |
916 | additional binding for ID in the same scope. This is the `struct | |
917 | stat' hack whereby a non-typedef class-name or enum-name can be | |
918 | bound at the same level as some other kind of entity. It's the | |
919 | responsibility of the caller to check that inserting this name is | |
0e339752 | 920 | valid here. Returns nonzero if the new binding was successful. */ |
8f032717 | 921 | static int |
11f6b451 | 922 | add_binding (tree id, tree decl) |
d8f8dca1 | 923 | { |
af6fd53f | 924 | cxx_binding *binding = IDENTIFIER_BINDING (id); |
8f032717 | 925 | int ok = 1; |
d8f8dca1 | 926 | |
7d9f2df2 | 927 | timevar_push (TV_NAME_LOOKUP); |
d8f8dca1 MM |
928 | if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl)) |
929 | /* The new name is the type name. */ | |
930 | BINDING_TYPE (binding) = decl; | |
8f032717 MM |
931 | else if (!BINDING_VALUE (binding)) |
932 | /* This situation arises when push_class_level_binding moves an | |
933 | inherited type-binding out of the way to make room for a new | |
934 | value binding. */ | |
935 | BINDING_VALUE (binding) = decl; | |
936 | else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL | |
937 | && DECL_ARTIFICIAL (BINDING_VALUE (binding))) | |
938 | { | |
939 | /* The old binding was a type name. It was placed in | |
940 | BINDING_VALUE because it was thought, at the point it was | |
941 | declared, to be the only entity with such a name. Move the | |
942 | type name into the type slot; it is now hidden by the new | |
943 | binding. */ | |
d8f8dca1 MM |
944 | BINDING_TYPE (binding) = BINDING_VALUE (binding); |
945 | BINDING_VALUE (binding) = decl; | |
8f032717 | 946 | INHERITED_VALUE_BINDING_P (binding) = 0; |
d8f8dca1 | 947 | } |
263505af MM |
948 | else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL |
949 | && TREE_CODE (decl) == TYPE_DECL | |
950 | && DECL_NAME (decl) == DECL_NAME (BINDING_VALUE (binding)) | |
9aaceb4b NS |
951 | && (same_type_p (TREE_TYPE (decl), |
952 | TREE_TYPE (BINDING_VALUE (binding))) | |
953 | /* If either type involves template parameters, we must | |
954 | wait until instantiation. */ | |
955 | || uses_template_parms (TREE_TYPE (decl)) | |
956 | || uses_template_parms (TREE_TYPE (BINDING_VALUE (binding))))) | |
263505af MM |
957 | /* We have two typedef-names, both naming the same type to have |
958 | the same name. This is OK because of: | |
959 | ||
960 | [dcl.typedef] | |
961 | ||
962 | In a given scope, a typedef specifier can be used to redefine | |
963 | the name of any type declared in that scope to refer to the | |
964 | type to which it already refers. */ | |
965 | ok = 0; | |
7b176381 | 966 | /* There can be two block-scope declarations of the same variable, |
f139561c MM |
967 | so long as they are `extern' declarations. However, there cannot |
968 | be two declarations of the same static data member: | |
969 | ||
970 | [class.mem] | |
971 | ||
972 | A member shall not be declared twice in the | |
973 | member-specification. */ | |
7b176381 MM |
974 | else if (TREE_CODE (decl) == VAR_DECL |
975 | && TREE_CODE (BINDING_VALUE (binding)) == VAR_DECL | |
976 | && DECL_EXTERNAL (decl) | |
f139561c MM |
977 | && DECL_EXTERNAL (BINDING_VALUE (binding)) |
978 | && !DECL_CLASS_SCOPE_P (decl)) | |
7b176381 MM |
979 | { |
980 | duplicate_decls (decl, BINDING_VALUE (binding)); | |
981 | ok = 0; | |
982 | } | |
8f032717 MM |
983 | else |
984 | { | |
33bd39a2 | 985 | error ("declaration of `%#D'", decl); |
8f032717 MM |
986 | cp_error_at ("conflicts with previous declaration `%#D'", |
987 | BINDING_VALUE (binding)); | |
988 | ok = 0; | |
989 | } | |
990 | ||
7d9f2df2 | 991 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok); |
d8f8dca1 MM |
992 | } |
993 | ||
cfe507be MM |
994 | /* Add DECL to the list of things declared in B. */ |
995 | ||
996 | static void | |
11f6b451 NN |
997 | add_decl_to_level (tree decl, |
998 | struct cp_binding_level* b) | |
cfe507be | 999 | { |
70b76b34 DP |
1000 | if (TREE_CODE (decl) == NAMESPACE_DECL |
1001 | && !DECL_NAMESPACE_ALIAS (decl)) | |
1002 | { | |
1003 | TREE_CHAIN (decl) = b->namespaces; | |
1004 | b->namespaces = decl; | |
1005 | } | |
1006 | else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl)) | |
1007 | { | |
1008 | TREE_CHAIN (decl) = b->vtables; | |
1009 | b->vtables = decl; | |
1010 | } | |
1011 | else | |
1012 | { | |
1013 | /* We build up the list in reverse order, and reverse it later if | |
1014 | necessary. */ | |
1015 | TREE_CHAIN (decl) = b->names; | |
1016 | b->names = decl; | |
1017 | b->names_size++; | |
1d555f7a MA |
1018 | |
1019 | /* If appropriate, add decl to separate list of statics */ | |
1020 | if (b->namespace_p) | |
1021 | if ((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl)) | |
1022 | || (TREE_CODE (decl) == FUNCTION_DECL | |
1023 | && (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl)))) | |
1024 | VARRAY_PUSH_TREE (b->static_decls, decl); | |
70b76b34 | 1025 | } |
cfe507be MM |
1026 | } |
1027 | ||
1028 | /* Bind DECL to ID in the current_binding_level, assumed to be a local | |
1029 | binding level. If PUSH_USING is set in FLAGS, we know that DECL | |
1030 | doesn't really belong to this binding level, that it got here | |
1031 | through a using-declaration. */ | |
f181d4ae MM |
1032 | |
1033 | void | |
11f6b451 | 1034 | push_local_binding (tree id, tree decl, int flags) |
f181d4ae | 1035 | { |
e2500fed | 1036 | struct cp_binding_level *b; |
8f032717 MM |
1037 | |
1038 | /* Skip over any local classes. This makes sense if we call | |
1039 | push_local_binding with a friend decl of a local class. */ | |
1040 | b = current_binding_level; | |
1041 | while (b->parm_flag == 2) | |
1042 | b = b->level_chain; | |
f181d4ae | 1043 | |
d8f8dca1 | 1044 | if (lookup_name_current_level (id)) |
8f032717 MM |
1045 | { |
1046 | /* Supplement the existing binding. */ | |
1047 | if (!add_binding (id, decl)) | |
1048 | /* It didn't work. Something else must be bound at this | |
1049 | level. Do not add DECL to the list of things to pop | |
1050 | later. */ | |
1051 | return; | |
1052 | } | |
d8f8dca1 MM |
1053 | else |
1054 | /* Create a new binding. */ | |
8f032717 | 1055 | push_binding (id, decl, b); |
f181d4ae | 1056 | |
0034cf72 | 1057 | if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING)) |
a06d48ef JM |
1058 | /* We must put the OVERLOAD into a TREE_LIST since the |
1059 | TREE_CHAIN of an OVERLOAD is already used. Similarly for | |
1060 | decls that got here through a using-declaration. */ | |
1061 | decl = build_tree_list (NULL_TREE, decl); | |
1062 | ||
f181d4ae MM |
1063 | /* And put DECL on the list of things declared by the current |
1064 | binding level. */ | |
cfe507be | 1065 | add_decl_to_level (decl, b); |
f181d4ae MM |
1066 | } |
1067 | ||
8f032717 MM |
1068 | /* Bind DECL to ID in the class_binding_level. Returns nonzero if the |
1069 | binding was successful. */ | |
f181d4ae | 1070 | |
8f032717 | 1071 | int |
11f6b451 | 1072 | push_class_binding (tree id, tree decl) |
f181d4ae | 1073 | { |
8f032717 | 1074 | int result = 1; |
af6fd53f | 1075 | cxx_binding *binding = IDENTIFIER_BINDING (id); |
8f032717 MM |
1076 | tree context; |
1077 | ||
7d9f2df2 | 1078 | timevar_push (TV_NAME_LOOKUP); |
8f032717 | 1079 | /* Note that we declared this value so that we can issue an error if |
0e339752 | 1080 | this is an invalid redeclaration of a name already used for some |
8f032717 MM |
1081 | other purpose. */ |
1082 | note_name_declared_in_class (id, decl); | |
1083 | ||
ed3cf953 | 1084 | if (binding && BINDING_SCOPE (binding) == class_binding_level) |
d8f8dca1 | 1085 | /* Supplement the existing binding. */ |
8f032717 | 1086 | result = add_binding (id, decl); |
d8f8dca1 MM |
1087 | else |
1088 | /* Create a new binding. */ | |
1089 | push_binding (id, decl, class_binding_level); | |
1090 | ||
1091 | /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the | |
1092 | class-level declaration. Note that we do not use DECL here | |
1093 | because of the possibility of the `struct stat' hack; if DECL is | |
1094 | a class-name or enum-name we might prefer a field-name, or some | |
1095 | such. */ | |
1096 | IDENTIFIER_CLASS_VALUE (id) = BINDING_VALUE (IDENTIFIER_BINDING (id)); | |
8f032717 MM |
1097 | |
1098 | /* If this is a binding from a base class, mark it as such. */ | |
1099 | binding = IDENTIFIER_BINDING (id); | |
1100 | if (BINDING_VALUE (binding) == decl && TREE_CODE (decl) != TREE_LIST) | |
1101 | { | |
8fbc5ae7 MM |
1102 | if (TREE_CODE (decl) == OVERLOAD) |
1103 | context = CP_DECL_CONTEXT (OVL_CURRENT (decl)); | |
8f032717 MM |
1104 | else |
1105 | { | |
8fbc5ae7 MM |
1106 | my_friendly_assert (DECL_P (decl), 0); |
1107 | context = context_for_name_lookup (decl); | |
280f9385 | 1108 | } |
8fbc5ae7 MM |
1109 | |
1110 | if (is_properly_derived_from (current_class_type, context)) | |
1111 | INHERITED_VALUE_BINDING_P (binding) = 1; | |
1112 | else | |
1113 | INHERITED_VALUE_BINDING_P (binding) = 0; | |
8f032717 MM |
1114 | } |
1115 | else if (BINDING_VALUE (binding) == decl) | |
1116 | /* We only encounter a TREE_LIST when push_class_decls detects an | |
1117 | ambiguity. Such an ambiguity can be overridden by a definition | |
1118 | in this class. */ | |
1119 | INHERITED_VALUE_BINDING_P (binding) = 1; | |
1120 | ||
7d9f2df2 | 1121 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result); |
f181d4ae MM |
1122 | } |
1123 | ||
d8f8dca1 MM |
1124 | /* Remove the binding for DECL which should be the innermost binding |
1125 | for ID. */ | |
f181d4ae | 1126 | |
68642fb6 | 1127 | static void |
11f6b451 | 1128 | pop_binding (tree id, tree decl) |
f181d4ae | 1129 | { |
af6fd53f | 1130 | cxx_binding *binding; |
68642fb6 | 1131 | |
f181d4ae MM |
1132 | if (id == NULL_TREE) |
1133 | /* It's easiest to write the loops that call this function without | |
1134 | checking whether or not the entities involved have names. We | |
1135 | get here for such an entity. */ | |
1136 | return; | |
1137 | ||
d8f8dca1 | 1138 | /* Get the innermost binding for ID. */ |
f181d4ae | 1139 | binding = IDENTIFIER_BINDING (id); |
f181d4ae | 1140 | |
d8f8dca1 | 1141 | /* The name should be bound. */ |
af6fd53f | 1142 | my_friendly_assert (binding != NULL, 0); |
d8f8dca1 MM |
1143 | |
1144 | /* The DECL will be either the ordinary binding or the type | |
1145 | binding for this identifier. Remove that binding. */ | |
1146 | if (BINDING_VALUE (binding) == decl) | |
1147 | BINDING_VALUE (binding) = NULL_TREE; | |
1148 | else if (BINDING_TYPE (binding) == decl) | |
1149 | BINDING_TYPE (binding) = NULL_TREE; | |
1150 | else | |
a98facb0 | 1151 | abort (); |
d8f8dca1 MM |
1152 | |
1153 | if (!BINDING_VALUE (binding) && !BINDING_TYPE (binding)) | |
4890c2f4 MM |
1154 | { |
1155 | /* We're completely done with the innermost binding for this | |
1156 | identifier. Unhook it from the list of bindings. */ | |
af6fd53f | 1157 | IDENTIFIER_BINDING (id) = binding->previous; |
4890c2f4 MM |
1158 | |
1159 | /* Add it to the free list. */ | |
aed81407 | 1160 | cxx_binding_free (binding); |
4890c2f4 | 1161 | |
ed3cf953 | 1162 | /* Clear the BINDING_SCOPE so the garbage collector doesn't walk |
4890c2f4 | 1163 | it. */ |
ed3cf953 | 1164 | BINDING_SCOPE (binding) = NULL; |
4890c2f4 | 1165 | } |
f181d4ae MM |
1166 | } |
1167 | ||
acef433b MM |
1168 | /* When a label goes out of scope, check to see if that label was used |
1169 | in a valid manner, and issue any appropriate warnings or errors. */ | |
1170 | ||
1171 | static void | |
11f6b451 | 1172 | pop_label (tree label, tree old_value) |
acef433b | 1173 | { |
88848bde | 1174 | if (!processing_template_decl && doing_semantic_analysis_p ()) |
acef433b | 1175 | { |
88848bde MM |
1176 | if (DECL_INITIAL (label) == NULL_TREE) |
1177 | { | |
1178 | cp_error_at ("label `%D' used but not defined", label); | |
1179 | /* Avoid crashing later. */ | |
1180 | define_label (input_filename, 1, DECL_NAME (label)); | |
1181 | } | |
078721e1 | 1182 | else if (warn_unused_label && !TREE_USED (label)) |
88848bde | 1183 | cp_warning_at ("label `%D' defined but not used", label); |
acef433b | 1184 | } |
acef433b | 1185 | |
6625cdb5 | 1186 | SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value); |
acef433b MM |
1187 | } |
1188 | ||
0811ea8f | 1189 | /* At the end of a function, all labels declared within the function |
68642fb6 | 1190 | go out of scope. BLOCK is the top-level block for the |
acef433b MM |
1191 | function. */ |
1192 | ||
1193 | static void | |
11f6b451 | 1194 | pop_labels (tree block) |
acef433b | 1195 | { |
6625cdb5 | 1196 | struct named_label_list *link; |
acef433b MM |
1197 | |
1198 | /* Clear out the definitions of all label names, since their scopes | |
1199 | end here. */ | |
6625cdb5 | 1200 | for (link = named_labels; link; link = link->next) |
acef433b | 1201 | { |
6625cdb5 | 1202 | pop_label (link->label_decl, link->old_value); |
acef433b MM |
1203 | /* Put the labels into the "variables" of the top-level block, |
1204 | so debugger can see them. */ | |
6625cdb5 JM |
1205 | TREE_CHAIN (link->label_decl) = BLOCK_VARS (block); |
1206 | BLOCK_VARS (block) = link->label_decl; | |
acef433b MM |
1207 | } |
1208 | ||
6625cdb5 | 1209 | named_labels = NULL; |
acef433b MM |
1210 | } |
1211 | ||
8d08fdba MS |
1212 | /* Exit a binding level. |
1213 | Pop the level off, and restore the state of the identifier-decl mappings | |
1214 | that were in effect when this level was entered. | |
1215 | ||
1216 | If KEEP == 1, this level had explicit declarations, so | |
1217 | and create a "block" (a BLOCK node) for the level | |
1218 | to record its declarations and subblocks for symbol table output. | |
1219 | ||
8d08fdba MS |
1220 | If FUNCTIONBODY is nonzero, this level is the body of a function, |
1221 | so create a block as if KEEP were set and also clear out all | |
1222 | label names. | |
1223 | ||
1224 | If REVERSE is nonzero, reverse the order of decls before putting | |
1225 | them into the BLOCK. */ | |
1226 | ||
1227 | tree | |
11f6b451 | 1228 | poplevel (int keep, int reverse, int functionbody) |
8d08fdba MS |
1229 | { |
1230 | register tree link; | |
1231 | /* The chain of decls was accumulated in reverse order. | |
1232 | Put it into forward order, just for cleanliness. */ | |
1233 | tree decls; | |
1234 | int tmp = functionbody; | |
b35d4555 | 1235 | int real_functionbody; |
b35d4555 | 1236 | tree subblocks; |
8d08fdba MS |
1237 | tree block = NULL_TREE; |
1238 | tree decl; | |
f181d4ae MM |
1239 | int leaving_for_scope; |
1240 | ||
22ffcc6f | 1241 | timevar_push (TV_NAME_LOOKUP); |
01d939e8 | 1242 | if (cfun && !doing_semantic_analysis_p ()) |
22ffcc6f | 1243 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); |
b35d4555 | 1244 | |
273a708f MM |
1245 | my_friendly_assert (current_binding_level->parm_flag != 2, |
1246 | 19990916); | |
b74a0560 | 1247 | |
b35d4555 MM |
1248 | real_functionbody = (current_binding_level->keep == 2 |
1249 | ? ((functionbody = 0), tmp) : functionbody); | |
b35d4555 MM |
1250 | subblocks = functionbody >= 0 ? current_binding_level->blocks : 0; |
1251 | ||
b74a0560 MM |
1252 | my_friendly_assert (!current_binding_level->class_shadowed, |
1253 | 19990414); | |
8d08fdba | 1254 | |
536333d4 MM |
1255 | /* We used to use KEEP == 2 to indicate that the new block should go |
1256 | at the beginning of the list of blocks at this binding level, | |
1257 | rather than the end. This hack is no longer used. */ | |
1258 | my_friendly_assert (keep == 0 || keep == 1, 0); | |
1259 | ||
8d08fdba MS |
1260 | if (current_binding_level->keep == 1) |
1261 | keep = 1; | |
1262 | ||
6625cdb5 JM |
1263 | /* Any uses of undefined labels, and any defined labels, now operate |
1264 | under constraints of next binding contour. */ | |
1265 | if (cfun && !functionbody) | |
1266 | { | |
e2500fed | 1267 | struct cp_binding_level *level_chain; |
6625cdb5 JM |
1268 | level_chain = current_binding_level->level_chain; |
1269 | if (level_chain) | |
1270 | { | |
1271 | struct named_label_use_list *uses; | |
1272 | struct named_label_list *labels; | |
1273 | for (labels = named_labels; labels; labels = labels->next) | |
1274 | if (labels->binding_level == current_binding_level) | |
1275 | { | |
1276 | tree decl; | |
826840d9 RH |
1277 | if (current_binding_level->is_try_scope) |
1278 | labels->in_try_scope = 1; | |
1279 | if (current_binding_level->is_catch_scope) | |
1280 | labels->in_catch_scope = 1; | |
6625cdb5 JM |
1281 | for (decl = labels->names_in_scope; decl; |
1282 | decl = TREE_CHAIN (decl)) | |
1283 | if (decl_jump_unsafe (decl)) | |
1284 | labels->bad_decls = tree_cons (NULL_TREE, decl, | |
1285 | labels->bad_decls); | |
1286 | labels->binding_level = level_chain; | |
1287 | labels->names_in_scope = level_chain->names; | |
1288 | } | |
1289 | ||
1290 | for (uses = named_label_uses; uses; uses = uses->next) | |
1291 | if (uses->binding_level == current_binding_level) | |
1292 | { | |
1293 | uses->binding_level = level_chain; | |
1294 | uses->names_in_scope = level_chain->names; | |
1295 | } | |
1296 | } | |
1297 | } | |
1298 | ||
8d08fdba MS |
1299 | /* Get the decls in the order they were written. |
1300 | Usually current_binding_level->names is in reverse order. | |
1301 | But parameter decls were previously put in forward order. */ | |
1302 | ||
1303 | if (reverse) | |
1304 | current_binding_level->names | |
1305 | = decls = nreverse (current_binding_level->names); | |
1306 | else | |
1307 | decls = current_binding_level->names; | |
1308 | ||
1309 | /* Output any nested inline functions within this block | |
1310 | if they weren't already output. */ | |
8d08fdba MS |
1311 | for (decl = decls; decl; decl = TREE_CHAIN (decl)) |
1312 | if (TREE_CODE (decl) == FUNCTION_DECL | |
1313 | && ! TREE_ASM_WRITTEN (decl) | |
1314 | && DECL_INITIAL (decl) != NULL_TREE | |
6060a796 MS |
1315 | && TREE_ADDRESSABLE (decl) |
1316 | && decl_function_context (decl) == current_function_decl) | |
8d08fdba MS |
1317 | { |
1318 | /* If this decl was copied from a file-scope decl | |
1319 | on account of a block-scope extern decl, | |
1320 | propagate TREE_ADDRESSABLE to the file-scope decl. */ | |
1321 | if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE) | |
1322 | TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1; | |
1323 | else | |
1324 | { | |
1325 | push_function_context (); | |
1326 | output_inline_function (decl); | |
1327 | pop_function_context (); | |
1328 | } | |
1329 | } | |
1330 | ||
d9b2d9da MM |
1331 | /* When not in function-at-a-time mode, expand_end_bindings will |
1332 | warn about unused variables. But, in function-at-a-time mode | |
1333 | expand_end_bindings is not passed the list of variables in the | |
1334 | current scope, and therefore no warning is emitted. So, we | |
1335 | explicitly warn here. */ | |
1336 | if (!processing_template_decl) | |
1337 | warn_about_unused_variables (getdecls ()); | |
1338 | ||
8d08fdba MS |
1339 | /* If there were any declarations or structure tags in that level, |
1340 | or if this level is a function body, | |
1341 | create a BLOCK to record them for the life of this function. */ | |
8d08fdba | 1342 | block = NULL_TREE; |
f444e36b | 1343 | if (keep == 1 || functionbody) |
8d08fdba MS |
1344 | block = make_node (BLOCK); |
1345 | if (block != NULL_TREE) | |
1346 | { | |
f444e36b MM |
1347 | BLOCK_VARS (block) = decls; |
1348 | BLOCK_SUBBLOCKS (block) = subblocks; | |
8d08fdba MS |
1349 | } |
1350 | ||
1351 | /* In each subblock, record that this is its superior. */ | |
8d08fdba MS |
1352 | if (keep >= 0) |
1353 | for (link = subblocks; link; link = TREE_CHAIN (link)) | |
1354 | BLOCK_SUPERCONTEXT (link) = block; | |
1355 | ||
f181d4ae MM |
1356 | /* We still support the old for-scope rules, whereby the variables |
1357 | in a for-init statement were in scope after the for-statement | |
92bc1323 | 1358 | ended. We only use the new rules if flag_new_for_scope is |
f181d4ae | 1359 | nonzero. */ |
68642fb6 | 1360 | leaving_for_scope |
f181d4ae MM |
1361 | = current_binding_level->is_for_scope && flag_new_for_scope == 1; |
1362 | ||
1363 | /* Remove declarations for all the DECLs in this level. */ | |
1364 | for (link = decls; link; link = TREE_CHAIN (link)) | |
1365 | { | |
c3783399 NS |
1366 | if (leaving_for_scope && TREE_CODE (link) == VAR_DECL |
1367 | && DECL_NAME (link)) | |
f181d4ae | 1368 | { |
af6fd53f GDR |
1369 | cxx_binding *outer_binding |
1370 | = IDENTIFIER_BINDING (DECL_NAME (link))->previous; | |
f181d4ae MM |
1371 | tree ns_binding; |
1372 | ||
1373 | if (!outer_binding) | |
1374 | ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link)); | |
c7dda1e3 MM |
1375 | else |
1376 | ns_binding = NULL_TREE; | |
1377 | ||
68642fb6 | 1378 | if (outer_binding |
ed3cf953 | 1379 | && (BINDING_SCOPE (outer_binding) |
f181d4ae MM |
1380 | == current_binding_level->level_chain)) |
1381 | /* We have something like: | |
68642fb6 | 1382 | |
f181d4ae MM |
1383 | int i; |
1384 | for (int i; ;); | |
68642fb6 | 1385 | |
f181d4ae MM |
1386 | and we are leaving the `for' scope. There's no reason to |
1387 | keep the binding of the inner `i' in this case. */ | |
d8f8dca1 | 1388 | pop_binding (DECL_NAME (link), link); |
68642fb6 UD |
1389 | else if ((outer_binding |
1390 | && (TREE_CODE (BINDING_VALUE (outer_binding)) | |
f181d4ae | 1391 | == TYPE_DECL)) |
68642fb6 | 1392 | || (ns_binding |
f181d4ae MM |
1393 | && TREE_CODE (ns_binding) == TYPE_DECL)) |
1394 | /* Here, we have something like: | |
1395 | ||
1396 | typedef int I; | |
1397 | ||
1398 | void f () { | |
1399 | for (int I; ;); | |
1400 | } | |
1401 | ||
1402 | We must pop the for-scope binding so we know what's a | |
1403 | type and what isn't. */ | |
d8f8dca1 | 1404 | pop_binding (DECL_NAME (link), link); |
e76a2646 | 1405 | else |
e76a2646 | 1406 | { |
f181d4ae MM |
1407 | /* Mark this VAR_DECL as dead so that we can tell we left it |
1408 | there only for backward compatibility. */ | |
1409 | DECL_DEAD_FOR_LOCAL (link) = 1; | |
68642fb6 | 1410 | |
517f3315 | 1411 | /* Keep track of what should have happened when we |
f181d4ae MM |
1412 | popped the binding. */ |
1413 | if (outer_binding && BINDING_VALUE (outer_binding)) | |
68642fb6 | 1414 | DECL_SHADOWED_FOR_VAR (link) |
f181d4ae MM |
1415 | = BINDING_VALUE (outer_binding); |
1416 | ||
1417 | /* Add it to the list of dead variables in the next | |
1418 | outermost binding to that we can remove these when we | |
1419 | leave that binding. */ | |
1420 | current_binding_level->level_chain->dead_vars_from_for | |
1421 | = tree_cons (NULL_TREE, link, | |
1422 | current_binding_level->level_chain-> | |
1423 | dead_vars_from_for); | |
1424 | ||
af6fd53f | 1425 | /* Although we don't pop the cxx_binding, we do clear |
ed3cf953 GDR |
1426 | its BINDING_SCOPE since the level is going away now. */ |
1427 | BINDING_SCOPE (IDENTIFIER_BINDING (DECL_NAME (link))) = 0; | |
e76a2646 | 1428 | } |
2ee887f2 | 1429 | } |
68642fb6 | 1430 | else |
8d6e462b | 1431 | { |
f181d4ae | 1432 | /* Remove the binding. */ |
0034cf72 JM |
1433 | decl = link; |
1434 | if (TREE_CODE (decl) == TREE_LIST) | |
1435 | decl = TREE_VALUE (decl); | |
2f939d94 | 1436 | if (DECL_P (decl)) |
0034cf72 JM |
1437 | pop_binding (DECL_NAME (decl), decl); |
1438 | else if (TREE_CODE (decl) == OVERLOAD) | |
1439 | pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl); | |
68642fb6 | 1440 | else |
a98facb0 | 1441 | abort (); |
8d08fdba | 1442 | } |
f181d4ae | 1443 | } |
8d08fdba | 1444 | |
f181d4ae MM |
1445 | /* Remove declarations for any `for' variables from inner scopes |
1446 | that we kept around. */ | |
1447 | for (link = current_binding_level->dead_vars_from_for; | |
1448 | link; link = TREE_CHAIN (link)) | |
d8f8dca1 | 1449 | pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link)); |
2ee887f2 | 1450 | |
f181d4ae MM |
1451 | /* Restore the IDENTIFIER_TYPE_VALUEs. */ |
1452 | for (link = current_binding_level->type_shadowed; | |
1453 | link; link = TREE_CHAIN (link)) | |
1454 | SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link)); | |
acef433b MM |
1455 | |
1456 | /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */ | |
1457 | for (link = current_binding_level->shadowed_labels; | |
68642fb6 | 1458 | link; |
acef433b | 1459 | link = TREE_CHAIN (link)) |
6625cdb5 | 1460 | pop_label (TREE_VALUE (link), TREE_PURPOSE (link)); |
acef433b | 1461 | |
f181d4ae MM |
1462 | /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs |
1463 | list if a `using' declaration put them there. The debugging | |
1464 | back-ends won't understand OVERLOAD, so we remove them here. | |
1465 | Because the BLOCK_VARS are (temporarily) shared with | |
1466 | CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have | |
1467 | popped all the bindings. */ | |
1468 | if (block) | |
1469 | { | |
1470 | tree* d; | |
8d6e462b | 1471 | |
a06d48ef JM |
1472 | for (d = &BLOCK_VARS (block); *d; ) |
1473 | { | |
1474 | if (TREE_CODE (*d) == TREE_LIST) | |
1475 | *d = TREE_CHAIN (*d); | |
1476 | else | |
1477 | d = &TREE_CHAIN (*d); | |
1478 | } | |
8d6e462b | 1479 | } |
8d08fdba MS |
1480 | |
1481 | /* If the level being exited is the top level of a function, | |
1482 | check over all the labels. */ | |
8d08fdba MS |
1483 | if (functionbody) |
1484 | { | |
acef433b MM |
1485 | /* Since this is the top level block of a function, the vars are |
1486 | the function's parameters. Don't leave them in the BLOCK | |
1487 | because they are found in the FUNCTION_DECL instead. */ | |
8d08fdba | 1488 | BLOCK_VARS (block) = 0; |
acef433b | 1489 | pop_labels (block); |
8d08fdba MS |
1490 | } |
1491 | ||
8d08fdba MS |
1492 | tmp = current_binding_level->keep; |
1493 | ||
1494 | pop_binding_level (); | |
1495 | if (functionbody) | |
1496 | DECL_INITIAL (current_function_decl) = block; | |
1497 | else if (block) | |
f444e36b MM |
1498 | current_binding_level->blocks |
1499 | = chainon (current_binding_level->blocks, block); | |
1500 | ||
8d08fdba MS |
1501 | /* If we did not make a block for the level just exited, |
1502 | any blocks made for inner levels | |
1503 | (since they cannot be recorded as subblocks in that level) | |
1504 | must be carried forward so they will later become subblocks | |
1505 | of something else. */ | |
1506 | else if (subblocks) | |
536333d4 MM |
1507 | current_binding_level->blocks |
1508 | = chainon (current_binding_level->blocks, subblocks); | |
8d08fdba | 1509 | |
d9b2d9da MM |
1510 | /* Each and every BLOCK node created here in `poplevel' is important |
1511 | (e.g. for proper debugging information) so if we created one | |
1512 | earlier, mark it as "used". */ | |
1513 | if (block) | |
1514 | TREE_USED (block) = 1; | |
1515 | ||
8d08fdba | 1516 | /* Take care of compiler's internal binding structures. */ |
a4443a08 | 1517 | if (tmp == 2) |
8d08fdba | 1518 | { |
d9b2d9da MM |
1519 | tree scope_stmts; |
1520 | ||
68642fb6 | 1521 | scope_stmts |
d9b2d9da | 1522 | = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/1); |
8d08fdba | 1523 | if (block) |
d9b2d9da MM |
1524 | { |
1525 | SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block; | |
1526 | SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block; | |
1527 | } | |
1528 | ||
8f471b0d | 1529 | block = poplevel (keep, reverse, functionbody); |
8d08fdba MS |
1530 | } |
1531 | ||
22ffcc6f | 1532 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, block); |
8d08fdba MS |
1533 | } |
1534 | ||
1535 | /* Delete the node BLOCK from the current binding level. | |
1536 | This is used for the block inside a stmt expr ({...}) | |
1537 | so that the block can be reinserted where appropriate. */ | |
1538 | ||
1539 | void | |
11f6b451 | 1540 | delete_block (tree block) |
8d08fdba MS |
1541 | { |
1542 | tree t; | |
1543 | if (current_binding_level->blocks == block) | |
1544 | current_binding_level->blocks = TREE_CHAIN (block); | |
1545 | for (t = current_binding_level->blocks; t;) | |
1546 | { | |
1547 | if (TREE_CHAIN (t) == block) | |
1548 | TREE_CHAIN (t) = TREE_CHAIN (block); | |
1549 | else | |
1550 | t = TREE_CHAIN (t); | |
1551 | } | |
1552 | TREE_CHAIN (block) = NULL_TREE; | |
1553 | /* Clear TREE_USED which is always set by poplevel. | |
1554 | The flag is set again if insert_block is called. */ | |
1555 | TREE_USED (block) = 0; | |
1556 | } | |
1557 | ||
1558 | /* Insert BLOCK at the end of the list of subblocks of the | |
1559 | current binding level. This is used when a BIND_EXPR is expanded, | |
1560 | to handle the BLOCK node inside the BIND_EXPR. */ | |
1561 | ||
1562 | void | |
11f6b451 | 1563 | insert_block (tree block) |
8d08fdba MS |
1564 | { |
1565 | TREE_USED (block) = 1; | |
1566 | current_binding_level->blocks | |
1567 | = chainon (current_binding_level->blocks, block); | |
1568 | } | |
1569 | ||
8d08fdba MS |
1570 | /* Set the BLOCK node for the innermost scope |
1571 | (the one we are currently in). */ | |
1572 | ||
1573 | void | |
11f6b451 | 1574 | set_block (tree block ATTRIBUTE_UNUSED ) |
8d08fdba | 1575 | { |
f444e36b MM |
1576 | /* The RTL expansion machinery requires us to provide this callback, |
1577 | but it is not applicable in function-at-a-time mode. */ | |
1578 | my_friendly_assert (cfun && !doing_semantic_analysis_p (), 20000911); | |
8d08fdba MS |
1579 | } |
1580 | ||
1581 | /* Do a pushlevel for class declarations. */ | |
e92cc029 | 1582 | |
8d08fdba | 1583 | void |
11f6b451 | 1584 | pushlevel_class (void) |
8d08fdba | 1585 | { |
f37e2274 GDR |
1586 | if (ENABLE_SCOPE_CHECKING) |
1587 | is_class_level = 1; | |
8d08fdba | 1588 | |
82a2669e | 1589 | push_binding_level (make_cxx_scope (false, 0)); |
8d08fdba | 1590 | |
8d08fdba MS |
1591 | class_binding_level = current_binding_level; |
1592 | class_binding_level->parm_flag = 2; | |
82a2669e | 1593 | class_binding_level->this_entity = current_class_type; |
8d08fdba MS |
1594 | } |
1595 | ||
b74a0560 | 1596 | /* ...and a poplevel for class declarations. */ |
e92cc029 | 1597 | |
273a708f | 1598 | void |
11f6b451 | 1599 | poplevel_class (void) |
8d08fdba | 1600 | { |
e2500fed | 1601 | register struct cp_binding_level *level = class_binding_level; |
8d08fdba MS |
1602 | tree shadowed; |
1603 | ||
22ffcc6f | 1604 | timevar_push (TV_NAME_LOOKUP); |
8d08fdba | 1605 | my_friendly_assert (level != 0, 354); |
68642fb6 | 1606 | |
8d08fdba | 1607 | /* If we're leaving a toplevel class, don't bother to do the setting |
ddd5a7c1 | 1608 | of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot |
8d08fdba | 1609 | shouldn't even be used when current_class_type isn't set, and second, |
ddd5a7c1 | 1610 | if we don't touch it here, we're able to use the cache effect if the |
8d08fdba | 1611 | next time we're entering a class scope, it is the same class. */ |
b74a0560 | 1612 | if (current_class_depth != 1) |
8f032717 | 1613 | { |
e2500fed | 1614 | struct cp_binding_level* b; |
8f032717 MM |
1615 | |
1616 | /* Clear out our IDENTIFIER_CLASS_VALUEs. */ | |
1617 | for (shadowed = level->class_shadowed; | |
1618 | shadowed; | |
1619 | shadowed = TREE_CHAIN (shadowed)) | |
1620 | IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE; | |
68642fb6 | 1621 | |
8f032717 MM |
1622 | /* Find the next enclosing class, and recreate |
1623 | IDENTIFIER_CLASS_VALUEs appropriate for that class. */ | |
1624 | b = level->level_chain; | |
1625 | while (b && b->parm_flag != 2) | |
1626 | b = b->level_chain; | |
1627 | ||
1628 | if (b) | |
68642fb6 UD |
1629 | for (shadowed = b->class_shadowed; |
1630 | shadowed; | |
8f032717 MM |
1631 | shadowed = TREE_CHAIN (shadowed)) |
1632 | { | |
af6fd53f GDR |
1633 | cxx_binding *binding; |
1634 | ||
1635 | binding = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed)); | |
ed3cf953 | 1636 | while (binding && BINDING_SCOPE (binding) != b) |
af6fd53f | 1637 | binding = binding->previous; |
68642fb6 | 1638 | |
af6fd53f | 1639 | if (binding) |
68642fb6 | 1640 | IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) |
af6fd53f | 1641 | = BINDING_VALUE (binding); |
8f032717 MM |
1642 | } |
1643 | } | |
8d08fdba MS |
1644 | else |
1645 | /* Remember to save what IDENTIFIER's were bound in this scope so we | |
1646 | can recover from cache misses. */ | |
e76a2646 MS |
1647 | { |
1648 | previous_class_type = current_class_type; | |
1649 | previous_class_values = class_binding_level->class_shadowed; | |
1650 | } | |
8d08fdba MS |
1651 | for (shadowed = level->type_shadowed; |
1652 | shadowed; | |
1653 | shadowed = TREE_CHAIN (shadowed)) | |
2c73f9f5 | 1654 | SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed)); |
8d08fdba | 1655 | |
f181d4ae | 1656 | /* Remove the bindings for all of the class-level declarations. */ |
68642fb6 UD |
1657 | for (shadowed = level->class_shadowed; |
1658 | shadowed; | |
f181d4ae | 1659 | shadowed = TREE_CHAIN (shadowed)) |
d8f8dca1 | 1660 | pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed)); |
f181d4ae | 1661 | |
38e01259 | 1662 | /* Now, pop out of the binding level which we created up in the |
8d08fdba | 1663 | `pushlevel_class' routine. */ |
f37e2274 GDR |
1664 | if (ENABLE_SCOPE_CHECKING) |
1665 | is_class_level = 1; | |
8d08fdba MS |
1666 | |
1667 | pop_binding_level (); | |
22ffcc6f | 1668 | timevar_pop (TV_NAME_LOOKUP); |
8d08fdba | 1669 | } |
8f032717 MM |
1670 | |
1671 | /* We are entering the scope of a class. Clear IDENTIFIER_CLASS_VALUE | |
1672 | for any names in enclosing classes. */ | |
1673 | ||
1674 | void | |
11f6b451 | 1675 | clear_identifier_class_values (void) |
8f032717 MM |
1676 | { |
1677 | tree t; | |
1678 | ||
1679 | if (!class_binding_level) | |
1680 | return; | |
1681 | ||
1682 | for (t = class_binding_level->class_shadowed; | |
1683 | t; | |
1684 | t = TREE_CHAIN (t)) | |
1685 | IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE; | |
1686 | } | |
1687 | ||
838dfd8a | 1688 | /* Returns nonzero if T is a virtual function table. */ |
0aafb128 MM |
1689 | |
1690 | int | |
11f6b451 | 1691 | vtable_decl_p (tree t, void* data ATTRIBUTE_UNUSED ) |
0aafb128 MM |
1692 | { |
1693 | return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t)); | |
1694 | } | |
1695 | ||
838dfd8a | 1696 | /* Returns nonzero if T is a TYPE_DECL for a type with virtual |
0aafb128 MM |
1697 | functions. */ |
1698 | ||
1699 | int | |
11f6b451 | 1700 | vtype_decl_p (tree t, void *data ATTRIBUTE_UNUSED ) |
0aafb128 MM |
1701 | { |
1702 | return (TREE_CODE (t) == TYPE_DECL | |
34249844 NS |
1703 | && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE |
1704 | && TYPE_POLYMORPHIC_P (TREE_TYPE (t))); | |
0aafb128 MM |
1705 | } |
1706 | ||
f71f87f9 MM |
1707 | /* Return the declarations that are members of the namespace NS. */ |
1708 | ||
1709 | tree | |
11f6b451 | 1710 | cp_namespace_decls (tree ns) |
f71f87f9 MM |
1711 | { |
1712 | return NAMESPACE_LEVEL (ns)->names; | |
1713 | } | |
1714 | ||
70b76b34 DP |
1715 | struct walk_globals_data { |
1716 | walk_globals_pred p; | |
1717 | walk_globals_fn f; | |
1718 | void *data; | |
1719 | }; | |
1720 | ||
1721 | /* Walk the vtable declarations in NAMESPACE. Whenever one is found | |
838dfd8a KH |
1722 | for which P returns nonzero, call F with its address. If any call |
1723 | to F returns a nonzero value, return a nonzero value. */ | |
70b76b34 DP |
1724 | |
1725 | static int | |
11f6b451 | 1726 | walk_vtables_r (tree namespace, void* data) |
70b76b34 DP |
1727 | { |
1728 | struct walk_globals_data* wgd = (struct walk_globals_data *) data; | |
1729 | walk_globals_fn f = wgd->f; | |
1730 | void *d = wgd->data; | |
1731 | tree decl = NAMESPACE_LEVEL (namespace)->vtables; | |
1732 | int result = 0; | |
1733 | ||
1734 | for (; decl ; decl = TREE_CHAIN (decl)) | |
0246d49e | 1735 | result |= (*f) (&decl, d); |
70b76b34 DP |
1736 | |
1737 | return result; | |
1738 | } | |
1739 | ||
1740 | /* Walk the vtable declarations. Whenever one is found for which P | |
838dfd8a KH |
1741 | returns nonzero, call F with its address. If any call to F |
1742 | returns a nonzero value, return a nonzero value. */ | |
848eed92 GDR |
1743 | bool |
1744 | walk_vtables (walk_globals_pred p, walk_globals_fn f, void *data) | |
70b76b34 DP |
1745 | { |
1746 | struct walk_globals_data wgd; | |
1747 | wgd.p = p; | |
1748 | wgd.f = f; | |
1749 | wgd.data = data; | |
1750 | ||
1751 | return walk_namespaces (walk_vtables_r, &wgd); | |
1752 | } | |
1753 | ||
0aafb128 MM |
1754 | /* Walk all the namespaces contained NAMESPACE, including NAMESPACE |
1755 | itself, calling F for each. The DATA is passed to F as well. */ | |
1756 | ||
1757 | static int | |
11f6b451 | 1758 | walk_namespaces_r (tree namespace, walk_namespaces_fn f, void* data) |
0aafb128 | 1759 | { |
0aafb128 | 1760 | int result = 0; |
70b76b34 | 1761 | tree current = NAMESPACE_LEVEL (namespace)->namespaces; |
0aafb128 MM |
1762 | |
1763 | result |= (*f) (namespace, data); | |
1764 | ||
70b76b34 DP |
1765 | for (; current; current = TREE_CHAIN (current)) |
1766 | result |= walk_namespaces_r (current, f, data); | |
0aafb128 MM |
1767 | |
1768 | return result; | |
1769 | } | |
1770 | ||
1771 | /* Walk all the namespaces, calling F for each. The DATA is passed to | |
1772 | F as well. */ | |
1773 | ||
1774 | int | |
11f6b451 | 1775 | walk_namespaces (walk_namespaces_fn f, void* data) |
0aafb128 MM |
1776 | { |
1777 | return walk_namespaces_r (global_namespace, f, data); | |
1778 | } | |
1779 | ||
0aafb128 | 1780 | /* Walk the global declarations in NAMESPACE. Whenever one is found |
838dfd8a KH |
1781 | for which P returns nonzero, call F with its address. If any call |
1782 | to F returns a nonzero value, return a nonzero value. */ | |
0aafb128 | 1783 | |
68642fb6 | 1784 | static int |
11f6b451 | 1785 | walk_globals_r (tree namespace, void* data) |
0aafb128 MM |
1786 | { |
1787 | struct walk_globals_data* wgd = (struct walk_globals_data *) data; | |
1788 | walk_globals_pred p = wgd->p; | |
1789 | walk_globals_fn f = wgd->f; | |
1790 | void *d = wgd->data; | |
1791 | tree *t; | |
1792 | int result = 0; | |
1793 | ||
1794 | t = &NAMESPACE_LEVEL (namespace)->names; | |
1795 | ||
1796 | while (*t) | |
1797 | { | |
1798 | tree glbl = *t; | |
1799 | ||
1800 | if ((*p) (glbl, d)) | |
1801 | result |= (*f) (t, d); | |
1802 | ||
1803 | /* If F changed *T, then *T still points at the next item to | |
1804 | examine. */ | |
1805 | if (*t == glbl) | |
1806 | t = &TREE_CHAIN (*t); | |
1807 | } | |
1808 | ||
1809 | return result; | |
1810 | } | |
1811 | ||
1812 | /* Walk the global declarations. Whenever one is found for which P | |
848eed92 GDR |
1813 | returns true, call F with its address. If any call to F |
1814 | returns true, return true. */ | |
0aafb128 | 1815 | |
848eed92 GDR |
1816 | bool |
1817 | walk_globals (walk_globals_pred p, walk_globals_fn f, void *data) | |
0aafb128 MM |
1818 | { |
1819 | struct walk_globals_data wgd; | |
1820 | wgd.p = p; | |
1821 | wgd.f = f; | |
1822 | wgd.data = data; | |
1823 | ||
1824 | return walk_namespaces (walk_globals_r, &wgd); | |
1825 | } | |
1826 | ||
1827 | /* Call wrapup_globals_declarations for the globals in NAMESPACE. If | |
1828 | DATA is non-NULL, this is the last time we will call | |
1829 | wrapup_global_declarations for this NAMESPACE. */ | |
1830 | ||
1831 | int | |
11f6b451 | 1832 | wrapup_globals_for_namespace (tree namespace, void* data) |
0aafb128 | 1833 | { |
1d555f7a MA |
1834 | struct cp_binding_level *level = NAMESPACE_LEVEL (namespace); |
1835 | varray_type statics = level->static_decls; | |
1836 | tree *vec = &VARRAY_TREE (statics, 0); | |
1837 | int len = VARRAY_ACTIVE_SIZE (statics); | |
0aafb128 MM |
1838 | int last_time = (data != 0); |
1839 | ||
0352cfc8 MM |
1840 | if (last_time) |
1841 | { | |
1842 | check_global_declarations (vec, len); | |
1843 | return 0; | |
1844 | } | |
1845 | ||
0352cfc8 | 1846 | /* Write out any globals that need to be output. */ |
1d555f7a | 1847 | return wrapup_global_declarations (vec, len); |
0aafb128 MM |
1848 | } |
1849 | ||
8d08fdba MS |
1850 | \f |
1851 | /* For debugging. */ | |
5566b478 MS |
1852 | static int no_print_functions = 0; |
1853 | static int no_print_builtins = 0; | |
8d08fdba | 1854 | |
5e0c54e5 GDR |
1855 | /* Called from print_binding_level through binding_table_foreach to |
1856 | print the content of binding ENTRY. DATA is a pointer to line offset | |
1857 | marker. */ | |
1858 | static void | |
1859 | bt_print_entry (binding_entry entry, void *data) | |
1860 | { | |
1861 | int *p = (int *) data; | |
1862 | int len; | |
1863 | ||
1864 | if (entry->name == NULL) | |
1865 | len = 3; | |
1866 | else if (entry->name == TYPE_IDENTIFIER (entry->type)) | |
1867 | len = 2; | |
1868 | else | |
1869 | len = 4; | |
1870 | len = 4; | |
1871 | ||
1872 | *p += len; | |
1873 | ||
1874 | if (*p > 5) | |
1875 | { | |
1876 | fprintf (stderr, "\n\t"); | |
1877 | *p = len; | |
1878 | } | |
1879 | if (entry->name == NULL) | |
1880 | { | |
1881 | print_node_brief (stderr, "<unnamed-typedef", entry->type, 0); | |
1882 | fprintf (stderr, ">"); | |
1883 | } | |
1884 | else if (entry->name == TYPE_IDENTIFIER (entry->type)) | |
1885 | print_node_brief (stderr, "", entry->type, 0); | |
1886 | else | |
1887 | { | |
1888 | print_node_brief (stderr, "<typedef", entry->name, 0); | |
1889 | print_node_brief (stderr, "", entry->type, 0); | |
1890 | fprintf (stderr, ">"); | |
1891 | } | |
1892 | } | |
1893 | ||
8d08fdba | 1894 | void |
11f6b451 | 1895 | print_binding_level (struct cp_binding_level* lvl) |
8d08fdba MS |
1896 | { |
1897 | tree t; | |
1898 | int i = 0, len; | |
1899 | fprintf (stderr, " blocks="); | |
de94b46c | 1900 | fprintf (stderr, HOST_PTR_PRINTF, (void *) lvl->blocks); |
8d08fdba MS |
1901 | if (lvl->tag_transparent) |
1902 | fprintf (stderr, " tag-transparent"); | |
1903 | if (lvl->more_cleanups_ok) | |
1904 | fprintf (stderr, " more-cleanups-ok"); | |
1905 | if (lvl->have_cleanups) | |
1906 | fprintf (stderr, " have-cleanups"); | |
8d08fdba MS |
1907 | fprintf (stderr, "\n"); |
1908 | if (lvl->names) | |
1909 | { | |
1910 | fprintf (stderr, " names:\t"); | |
1911 | /* We can probably fit 3 names to a line? */ | |
1912 | for (t = lvl->names; t; t = TREE_CHAIN (t)) | |
1913 | { | |
68642fb6 | 1914 | if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL)) |
8d08fdba MS |
1915 | continue; |
1916 | if (no_print_builtins | |
fc378698 MS |
1917 | && (TREE_CODE (t) == TYPE_DECL) |
1918 | && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>"))) | |
8d08fdba MS |
1919 | continue; |
1920 | ||
1921 | /* Function decls tend to have longer names. */ | |
1922 | if (TREE_CODE (t) == FUNCTION_DECL) | |
1923 | len = 3; | |
1924 | else | |
1925 | len = 2; | |
1926 | i += len; | |
1927 | if (i > 6) | |
1928 | { | |
1929 | fprintf (stderr, "\n\t"); | |
1930 | i = len; | |
1931 | } | |
1932 | print_node_brief (stderr, "", t, 0); | |
bd6dd845 | 1933 | if (t == error_mark_node) |
8d08fdba MS |
1934 | break; |
1935 | } | |
1936 | if (i) | |
1937 | fprintf (stderr, "\n"); | |
1938 | } | |
5e0c54e5 | 1939 | if (lvl->type_decls) |
8d08fdba MS |
1940 | { |
1941 | fprintf (stderr, " tags:\t"); | |
1942 | i = 0; | |
5e0c54e5 | 1943 | binding_table_foreach (lvl->type_decls, bt_print_entry, &i); |
8d08fdba MS |
1944 | if (i) |
1945 | fprintf (stderr, "\n"); | |
1946 | } | |
8d08fdba MS |
1947 | if (lvl->class_shadowed) |
1948 | { | |
1949 | fprintf (stderr, " class-shadowed:"); | |
1950 | for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t)) | |
1951 | { | |
1952 | fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t))); | |
1953 | } | |
1954 | fprintf (stderr, "\n"); | |
1955 | } | |
1956 | if (lvl->type_shadowed) | |
1957 | { | |
1958 | fprintf (stderr, " type-shadowed:"); | |
1959 | for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t)) | |
1960 | { | |
8d08fdba | 1961 | fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t))); |
8d08fdba MS |
1962 | } |
1963 | fprintf (stderr, "\n"); | |
1964 | } | |
1965 | } | |
1966 | ||
1967 | void | |
11f6b451 | 1968 | print_other_binding_stack (struct cp_binding_level *stack) |
8d08fdba | 1969 | { |
e2500fed | 1970 | struct cp_binding_level *level; |
5f52c0e0 | 1971 | for (level = stack; !global_scope_p (level); level = level->level_chain) |
8d08fdba MS |
1972 | { |
1973 | fprintf (stderr, "binding level "); | |
de94b46c | 1974 | fprintf (stderr, HOST_PTR_PRINTF, (void *) level); |
8d08fdba MS |
1975 | fprintf (stderr, "\n"); |
1976 | print_binding_level (level); | |
1977 | } | |
1978 | } | |
1979 | ||
1980 | void | |
11f6b451 | 1981 | print_binding_stack (void) |
8d08fdba | 1982 | { |
e2500fed | 1983 | struct cp_binding_level *b; |
8d08fdba | 1984 | fprintf (stderr, "current_binding_level="); |
de94b46c | 1985 | fprintf (stderr, HOST_PTR_PRINTF, (void *) current_binding_level); |
8d08fdba | 1986 | fprintf (stderr, "\nclass_binding_level="); |
de94b46c | 1987 | fprintf (stderr, HOST_PTR_PRINTF, (void *) class_binding_level); |
5f52c0e0 GDR |
1988 | fprintf (stderr, "\nNAMESPACE_LEVEL (global_namespace)="); |
1989 | fprintf (stderr, HOST_PTR_PRINTF, | |
1990 | (void *) NAMESPACE_LEVEL (global_namespace)); | |
8d08fdba MS |
1991 | fprintf (stderr, "\n"); |
1992 | if (class_binding_level) | |
1993 | { | |
1994 | for (b = class_binding_level; b; b = b->level_chain) | |
1995 | if (b == current_binding_level) | |
1996 | break; | |
1997 | if (b) | |
1998 | b = class_binding_level; | |
1999 | else | |
2000 | b = current_binding_level; | |
2001 | } | |
2002 | else | |
2003 | b = current_binding_level; | |
2004 | print_other_binding_stack (b); | |
2005 | fprintf (stderr, "global:\n"); | |
5f52c0e0 | 2006 | print_binding_level (NAMESPACE_LEVEL (global_namespace)); |
8d08fdba | 2007 | } |
a9aedbc2 | 2008 | |
2c73f9f5 ML |
2009 | /* Namespace binding access routines: The namespace_bindings field of |
2010 | the identifier is polymorphic, with three possible values: | |
af6fd53f | 2011 | NULL_TREE, a list of "cxx_binding"s. */ |
30394414 | 2012 | |
82a2669e GDR |
2013 | |
2014 | /* Push the initial binding contour of NAMESPACE-scope. Any subsequent | |
2015 | push of NS is actually a resume. */ | |
2016 | static void | |
2017 | initial_push_namespace_scope (tree ns) | |
2018 | { | |
2019 | tree name = DECL_NAME (ns); | |
2020 | cxx_scope *scope; | |
2021 | ||
2022 | pushlevel (0); | |
2023 | scope = current_binding_level; | |
2024 | scope->namespace_p = true; | |
2025 | scope->type_decls = binding_table_new (name == std_identifier | |
2026 | ? NAMESPACE_STD_HT_SIZE | |
2027 | : (name == global_scope_name | |
2028 | ? GLOBAL_SCOPE_HT_SIZE | |
2029 | : NAMESPACE_ORDINARY_HT_SIZE)); | |
2030 | VARRAY_TREE_INIT (scope->static_decls, | |
2031 | name == std_identifier || name == global_scope_name | |
2032 | ? 200 : 10, | |
2033 | "Static declarations"); | |
2034 | scope->this_entity = ns; | |
2035 | NAMESPACE_LEVEL (ns) = scope; | |
2036 | } | |
2037 | ||
dff6b454 RK |
2038 | /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we |
2039 | select a name that is unique to this compilation unit. */ | |
e92cc029 | 2040 | |
a9aedbc2 | 2041 | void |
11f6b451 | 2042 | push_namespace (tree name) |
a9aedbc2 | 2043 | { |
b370501f | 2044 | tree d = NULL_TREE; |
30394414 JM |
2045 | int need_new = 1; |
2046 | int implicit_use = 0; | |
22ffcc6f GDR |
2047 | |
2048 | timevar_push (TV_NAME_LOOKUP); | |
2049 | ||
82a2669e GDR |
2050 | /* We should not get here if the global_namespace is not yet constructed |
2051 | nor if NAME designates the global namespace: The global scope is | |
2052 | constructed elsewhere. */ | |
2053 | my_friendly_assert (global_namespace != NULL && name != global_scope_name, | |
2054 | 20030531); | |
2055 | ||
2056 | if (!name) | |
30394414 | 2057 | { |
3ab52652 ML |
2058 | /* The name of anonymous namespace is unique for the translation |
2059 | unit. */ | |
0c8feefe MM |
2060 | if (!anonymous_namespace_name) |
2061 | anonymous_namespace_name = get_file_function_name ('N'); | |
2062 | name = anonymous_namespace_name; | |
3ab52652 ML |
2063 | d = IDENTIFIER_NAMESPACE_VALUE (name); |
2064 | if (d) | |
2065 | /* Reopening anonymous namespace. */ | |
2066 | need_new = 0; | |
30394414 | 2067 | implicit_use = 1; |
2c73f9f5 | 2068 | } |
30394414 JM |
2069 | else |
2070 | { | |
a1c65f9f | 2071 | /* Check whether this is an extended namespace definition. */ |
30394414 JM |
2072 | d = IDENTIFIER_NAMESPACE_VALUE (name); |
2073 | if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL) | |
3e3f722c ML |
2074 | { |
2075 | need_new = 0; | |
2076 | if (DECL_NAMESPACE_ALIAS (d)) | |
2077 | { | |
33bd39a2 | 2078 | error ("namespace alias `%D' not allowed here, assuming `%D'", |
3e3f722c ML |
2079 | d, DECL_NAMESPACE_ALIAS (d)); |
2080 | d = DECL_NAMESPACE_ALIAS (d); | |
2081 | } | |
2082 | } | |
30394414 | 2083 | } |
68642fb6 | 2084 | |
30394414 JM |
2085 | if (need_new) |
2086 | { | |
a1c65f9f | 2087 | /* Make a new namespace, binding the name to it. */ |
6b57ac29 | 2088 | d = build_lang_decl (NAMESPACE_DECL, name, void_type_node); |
82a2669e GDR |
2089 | DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace); |
2090 | d = pushdecl (d); | |
2091 | initial_push_namespace_scope (d); | |
30394414 | 2092 | } |
2c73f9f5 ML |
2093 | else |
2094 | resume_binding_level (NAMESPACE_LEVEL (d)); | |
2095 | ||
30394414 JM |
2096 | if (implicit_use) |
2097 | do_using_directive (d); | |
a1c65f9f | 2098 | /* Enter the name space. */ |
30394414 | 2099 | current_namespace = d; |
22ffcc6f GDR |
2100 | |
2101 | timevar_pop (TV_NAME_LOOKUP); | |
a9aedbc2 MS |
2102 | } |
2103 | ||
2104 | /* Pop from the scope of the current namespace. */ | |
e92cc029 | 2105 | |
a9aedbc2 | 2106 | void |
11f6b451 | 2107 | pop_namespace (void) |
a9aedbc2 | 2108 | { |
1dbb6023 | 2109 | my_friendly_assert (current_namespace != global_namespace, 20010801); |
cb0dbb9a | 2110 | current_namespace = CP_DECL_CONTEXT (current_namespace); |
2c73f9f5 ML |
2111 | /* The binding level is not popped, as it might be re-opened later. */ |
2112 | suspend_binding_level (); | |
2113 | } | |
a9aedbc2 | 2114 | |
61fc8c9e MM |
2115 | /* Push into the scope of the namespace NS, even if it is deeply |
2116 | nested within another namespace. */ | |
2117 | ||
2118 | void | |
11f6b451 | 2119 | push_nested_namespace (tree ns) |
61fc8c9e MM |
2120 | { |
2121 | if (ns == global_namespace) | |
2122 | push_to_top_level (); | |
2123 | else | |
2124 | { | |
2125 | push_nested_namespace (CP_DECL_CONTEXT (ns)); | |
2126 | push_namespace (DECL_NAME (ns)); | |
2127 | } | |
2128 | } | |
2129 | ||
2130 | /* Pop back from the scope of the namespace NS, which was previously | |
2131 | entered with push_nested_namespace. */ | |
68642fb6 | 2132 | |
61fc8c9e | 2133 | void |
11f6b451 | 2134 | pop_nested_namespace (tree ns) |
61fc8c9e | 2135 | { |
22ffcc6f | 2136 | timevar_push (TV_NAME_LOOKUP); |
61fc8c9e MM |
2137 | while (ns != global_namespace) |
2138 | { | |
2139 | pop_namespace (); | |
2140 | ns = CP_DECL_CONTEXT (ns); | |
2141 | } | |
2142 | ||
2143 | pop_from_top_level (); | |
22ffcc6f | 2144 | timevar_pop (TV_NAME_LOOKUP); |
61fc8c9e MM |
2145 | } |
2146 | ||
8d08fdba | 2147 | \f |
000d38ea GDR |
2148 | /* Allocate storage for saving a C++ binding. */ |
2149 | #define cxx_saved_binding_make() \ | |
2150 | (ggc_alloc (sizeof (cxx_saved_binding))) | |
2151 | ||
2152 | struct cxx_saved_binding GTY(()) | |
2153 | { | |
2154 | /* Link that chains saved C++ bindings for a given name into a stack. */ | |
2155 | cxx_saved_binding *previous; | |
2156 | /* The name of the current binding. */ | |
2157 | tree identifier; | |
2158 | /* The binding we're saving. */ | |
af6fd53f | 2159 | cxx_binding *binding; |
000d38ea GDR |
2160 | tree class_value; |
2161 | tree real_type_value; | |
2162 | }; | |
2163 | ||
8d08fdba MS |
2164 | /* Subroutines for reverting temporarily to top-level for instantiation |
2165 | of templates and such. We actually need to clear out the class- and | |
2166 | local-value slots of all identifiers, so that only the global values | |
2167 | are at all visible. Simply setting current_binding_level to the global | |
2168 | scope isn't enough, because more binding levels may be pushed. */ | |
9cd64686 | 2169 | struct saved_scope *scope_chain; |
8d08fdba | 2170 | |
000d38ea GDR |
2171 | static cxx_saved_binding * |
2172 | store_bindings (tree names, cxx_saved_binding *old_bindings) | |
45537677 MS |
2173 | { |
2174 | tree t; | |
000d38ea | 2175 | cxx_saved_binding *search_bindings = old_bindings; |
c00996a3 | 2176 | |
22ffcc6f | 2177 | timevar_push (TV_NAME_LOOKUP); |
45537677 MS |
2178 | for (t = names; t; t = TREE_CHAIN (t)) |
2179 | { | |
000d38ea GDR |
2180 | tree id; |
2181 | cxx_saved_binding *saved; | |
2182 | cxx_saved_binding *t1; | |
45537677 MS |
2183 | |
2184 | if (TREE_CODE (t) == TREE_LIST) | |
2185 | id = TREE_PURPOSE (t); | |
2186 | else | |
2187 | id = DECL_NAME (t); | |
2188 | ||
68642fb6 | 2189 | if (!id |
f181d4ae MM |
2190 | /* Note that we may have an IDENTIFIER_CLASS_VALUE even when |
2191 | we have no IDENTIFIER_BINDING if we have left the class | |
2192 | scope, but cached the class-level declarations. */ | |
2193 | || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id))) | |
45537677 MS |
2194 | continue; |
2195 | ||
000d38ea GDR |
2196 | for (t1 = search_bindings; t1; t1 = t1->previous) |
2197 | if (t1->identifier == id) | |
45537677 | 2198 | goto skip_it; |
78957a2a | 2199 | |
31a714f6 | 2200 | my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135); |
000d38ea GDR |
2201 | saved = cxx_saved_binding_make (); |
2202 | saved->previous = old_bindings; | |
2203 | saved->identifier = id; | |
2204 | saved->binding = IDENTIFIER_BINDING (id); | |
2205 | saved->class_value = IDENTIFIER_CLASS_VALUE (id);; | |
2206 | saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id); | |
af6fd53f | 2207 | IDENTIFIER_BINDING (id) = NULL; |
31a714f6 | 2208 | IDENTIFIER_CLASS_VALUE (id) = NULL_TREE; |
000d38ea | 2209 | old_bindings = saved; |
45537677 MS |
2210 | skip_it: |
2211 | ; | |
2212 | } | |
22ffcc6f | 2213 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old_bindings); |
45537677 MS |
2214 | } |
2215 | ||
8d08fdba | 2216 | void |
11f6b451 | 2217 | maybe_push_to_top_level (int pseudo) |
8d08fdba | 2218 | { |
a8f73d4b | 2219 | struct saved_scope *s; |
e2500fed | 2220 | struct cp_binding_level *b; |
000d38ea | 2221 | cxx_saved_binding *old_bindings; |
a8f73d4b MM |
2222 | int need_pop; |
2223 | ||
22ffcc6f | 2224 | timevar_push (TV_NAME_LOOKUP); |
e2500fed | 2225 | s = (struct saved_scope *) ggc_alloc_cleared (sizeof (struct saved_scope)); |
8d08fdba | 2226 | |
9cd64686 MM |
2227 | b = scope_chain ? current_binding_level : 0; |
2228 | ||
a8f73d4b | 2229 | /* If we're in the middle of some function, save our state. */ |
01d939e8 | 2230 | if (cfun) |
a8f73d4b MM |
2231 | { |
2232 | need_pop = 1; | |
2233 | push_function_context_to (NULL_TREE); | |
2234 | } | |
2235 | else | |
2236 | need_pop = 0; | |
e349ee73 | 2237 | |
000d38ea | 2238 | old_bindings = NULL; |
9cd64686 | 2239 | if (scope_chain && previous_class_type) |
a50f0918 MS |
2240 | old_bindings = store_bindings (previous_class_values, old_bindings); |
2241 | ||
5f52c0e0 | 2242 | /* Have to include the global scope, because class-scope decls |
8d08fdba MS |
2243 | aren't listed anywhere useful. */ |
2244 | for (; b; b = b->level_chain) | |
2245 | { | |
2246 | tree t; | |
2247 | ||
2c73f9f5 ML |
2248 | /* Template IDs are inserted into the global level. If they were |
2249 | inserted into namespace level, finish_file wouldn't find them | |
2250 | when doing pending instantiations. Therefore, don't stop at | |
2251 | namespace level, but continue until :: . */ | |
5f52c0e0 | 2252 | if (global_scope_p (b) || (pseudo && b->template_parms_p)) |
5566b478 | 2253 | break; |
8d08fdba | 2254 | |
45537677 | 2255 | old_bindings = store_bindings (b->names, old_bindings); |
cffa8729 | 2256 | /* We also need to check class_shadowed to save class-level type |
45537677 MS |
2257 | bindings, since pushclass doesn't fill in b->names. */ |
2258 | if (b->parm_flag == 2) | |
cffa8729 | 2259 | old_bindings = store_bindings (b->class_shadowed, old_bindings); |
8d08fdba | 2260 | |
8d08fdba MS |
2261 | /* Unwind type-value slots back to top level. */ |
2262 | for (t = b->type_shadowed; t; t = TREE_CHAIN (t)) | |
2263 | SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t)); | |
2264 | } | |
fc0e7bf5 MM |
2265 | s->prev = scope_chain; |
2266 | s->old_bindings = old_bindings; | |
a8f73d4b MM |
2267 | s->bindings = b; |
2268 | s->need_pop_function_context = need_pop; | |
2269 | s->function_decl = current_function_decl; | |
0f4237c2 | 2270 | s->last_parms = last_function_parms; |
fc0e7bf5 MM |
2271 | |
2272 | scope_chain = s; | |
8d08fdba | 2273 | current_function_decl = NULL_TREE; |
9cd64686 | 2274 | VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base"); |
51c184be | 2275 | current_lang_name = lang_name_cplusplus; |
2c73f9f5 | 2276 | current_namespace = global_namespace; |
22ffcc6f | 2277 | timevar_pop (TV_NAME_LOOKUP); |
5566b478 MS |
2278 | } |
2279 | ||
2280 | void | |
11f6b451 | 2281 | push_to_top_level (void) |
5566b478 MS |
2282 | { |
2283 | maybe_push_to_top_level (0); | |
8d08fdba MS |
2284 | } |
2285 | ||
2286 | void | |
11f6b451 | 2287 | pop_from_top_level (void) |
8d08fdba | 2288 | { |
9cd64686 | 2289 | struct saved_scope *s = scope_chain; |
000d38ea | 2290 | cxx_saved_binding *saved; |
8d08fdba | 2291 | |
22ffcc6f | 2292 | timevar_push (TV_NAME_LOOKUP); |
e76a2646 | 2293 | /* Clear out class-level bindings cache. */ |
8d08fdba | 2294 | if (previous_class_type) |
8f032717 | 2295 | invalidate_class_lookup_cache (); |
8d08fdba | 2296 | |
e2500fed | 2297 | current_lang_base = 0; |
9cd64686 MM |
2298 | |
2299 | scope_chain = s->prev; | |
000d38ea | 2300 | for (saved = s->old_bindings; saved; saved = saved->previous) |
8d08fdba | 2301 | { |
000d38ea | 2302 | tree id = saved->identifier; |
31a714f6 | 2303 | |
000d38ea GDR |
2304 | IDENTIFIER_BINDING (id) = saved->binding; |
2305 | IDENTIFIER_CLASS_VALUE (id) = saved->class_value; | |
2306 | SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value); | |
8d08fdba | 2307 | } |
9cd64686 | 2308 | |
a8f73d4b MM |
2309 | /* If we were in the middle of compiling a function, restore our |
2310 | state. */ | |
2311 | if (s->need_pop_function_context) | |
2312 | pop_function_context_from (NULL_TREE); | |
2313 | current_function_decl = s->function_decl; | |
0f4237c2 | 2314 | last_function_parms = s->last_parms; |
22ffcc6f | 2315 | timevar_pop (TV_NAME_LOOKUP); |
8d08fdba MS |
2316 | } |
2317 | \f | |
2318 | /* Push a definition of struct, union or enum tag "name". | |
68642fb6 | 2319 | into binding_level "b". "type" should be the type node, |
8d08fdba MS |
2320 | We assume that the tag "name" is not already defined. |
2321 | ||
2322 | Note that the definition may really be just a forward reference. | |
2323 | In that case, the TYPE_SIZE will be a NULL_TREE. | |
2324 | ||
e92cc029 | 2325 | C++ gratuitously puts all these tags in the name space. */ |
8d08fdba MS |
2326 | |
2327 | /* When setting the IDENTIFIER_TYPE_VALUE field of an identifier ID, | |
2328 | record the shadowed value for this binding contour. TYPE is | |
2329 | the type that ID maps to. */ | |
2330 | ||
2331 | static void | |
11f6b451 NN |
2332 | set_identifier_type_value_with_scope (tree id, |
2333 | tree type, | |
2334 | struct cp_binding_level* b) | |
8d08fdba | 2335 | { |
2c73f9f5 | 2336 | if (!b->namespace_p) |
8d08fdba | 2337 | { |
2c73f9f5 | 2338 | /* Shadow the marker, not the real thing, so that the marker |
a1c65f9f | 2339 | gets restored later. */ |
2c73f9f5 | 2340 | tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id); |
8d08fdba MS |
2341 | b->type_shadowed |
2342 | = tree_cons (id, old_type_value, b->type_shadowed); | |
2343 | } | |
2c73f9f5 ML |
2344 | else |
2345 | { | |
ed3cf953 GDR |
2346 | cxx_binding *binding = |
2347 | binding_for_name (NAMESPACE_LEVEL (current_namespace), id); | |
2c73f9f5 | 2348 | BINDING_TYPE (binding) = type; |
a1c65f9f | 2349 | /* Store marker instead of real type. */ |
2c73f9f5 ML |
2350 | type = global_type_node; |
2351 | } | |
8d08fdba MS |
2352 | SET_IDENTIFIER_TYPE_VALUE (id, type); |
2353 | } | |
2354 | ||
8f032717 | 2355 | /* As set_identifier_type_value_with_scope, but using current_binding_level. */ |
8d08fdba MS |
2356 | |
2357 | void | |
11f6b451 | 2358 | set_identifier_type_value (tree id, tree type) |
8d08fdba | 2359 | { |
8f032717 | 2360 | set_identifier_type_value_with_scope (id, type, current_binding_level); |
8d08fdba MS |
2361 | } |
2362 | ||
a1c65f9f | 2363 | /* Return the type associated with id. */ |
2c73f9f5 ML |
2364 | |
2365 | tree | |
11f6b451 | 2366 | identifier_type_value (tree id) |
2c73f9f5 | 2367 | { |
22ffcc6f | 2368 | timevar_push (TV_NAME_LOOKUP); |
a1c65f9f | 2369 | /* There is no type with that name, anywhere. */ |
2c73f9f5 | 2370 | if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE) |
22ffcc6f | 2371 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); |
a1c65f9f | 2372 | /* This is not the type marker, but the real thing. */ |
2c73f9f5 | 2373 | if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node) |
22ffcc6f | 2374 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id)); |
2c73f9f5 | 2375 | /* Have to search for it. It must be on the global level, now. |
a723baf1 MM |
2376 | Ask lookup_name not to return non-types. */ |
2377 | id = lookup_name_real (id, 2, 1, 0, LOOKUP_COMPLAIN); | |
2c73f9f5 | 2378 | if (id) |
22ffcc6f GDR |
2379 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id)); |
2380 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); | |
2c73f9f5 ML |
2381 | } |
2382 | ||
a9aedbc2 MS |
2383 | /* Pop off extraneous binding levels left over due to syntax errors. |
2384 | ||
2385 | We don't pop past namespaces, as they might be valid. */ | |
e92cc029 | 2386 | |
8926095f | 2387 | void |
11f6b451 | 2388 | pop_everything (void) |
8926095f | 2389 | { |
f37e2274 GDR |
2390 | if (ENABLE_SCOPE_CHECKING) |
2391 | verbatim ("XXX entering pop_everything ()\n"); | |
8f032717 | 2392 | while (!toplevel_bindings_p ()) |
8926095f | 2393 | { |
8f032717 | 2394 | if (current_binding_level->parm_flag == 2) |
b74a0560 | 2395 | pop_nested_class (); |
8926095f MS |
2396 | else |
2397 | poplevel (0, 0, 0); | |
2398 | } | |
f37e2274 GDR |
2399 | if (ENABLE_SCOPE_CHECKING) |
2400 | verbatim ("XXX leaving pop_everything ()\n"); | |
8926095f MS |
2401 | } |
2402 | ||
39c01e4c MM |
2403 | /* The type TYPE is being declared. If it is a class template, or a |
2404 | specialization of a class template, do any processing required and | |
838dfd8a | 2405 | perform error-checking. If IS_FRIEND is nonzero, this TYPE is |
39c01e4c MM |
2406 | being declared a friend. B is the binding level at which this TYPE |
2407 | should be bound. | |
2408 | ||
2409 | Returns the TYPE_DECL for TYPE, which may have been altered by this | |
2410 | processing. */ | |
2411 | ||
68642fb6 | 2412 | static tree |
11f6b451 NN |
2413 | maybe_process_template_type_declaration (tree type, |
2414 | int globalize, | |
2415 | struct cp_binding_level* b) | |
39c01e4c MM |
2416 | { |
2417 | tree decl = TYPE_NAME (type); | |
68642fb6 | 2418 | |
39c01e4c MM |
2419 | if (processing_template_parmlist) |
2420 | /* You can't declare a new template type in a template parameter | |
2421 | list. But, you can declare a non-template type: | |
68642fb6 | 2422 | |
39c01e4c | 2423 | template <class A*> struct S; |
68642fb6 | 2424 | |
39c01e4c MM |
2425 | is a forward-declaration of `A'. */ |
2426 | ; | |
68642fb6 | 2427 | else |
39c01e4c MM |
2428 | { |
2429 | maybe_check_template_type (type); | |
2430 | ||
68642fb6 | 2431 | my_friendly_assert (IS_AGGR_TYPE (type) |
ed44da02 | 2432 | || TREE_CODE (type) == ENUMERAL_TYPE, 0); |
68642fb6 UD |
2433 | |
2434 | ||
3ebc5c52 | 2435 | if (processing_template_decl) |
39c01e4c MM |
2436 | { |
2437 | /* This may change after the call to | |
2438 | push_template_decl_real, but we want the original value. */ | |
2439 | tree name = DECL_NAME (decl); | |
2440 | ||
2441 | decl = push_template_decl_real (decl, globalize); | |
2442 | /* If the current binding level is the binding level for the | |
2443 | template parameters (see the comment in | |
2444 | begin_template_parm_list) and the enclosing level is a class | |
2445 | scope, and we're not looking at a friend, push the | |
2446 | declaration of the member class into the class scope. In the | |
2447 | friend case, push_template_decl will already have put the | |
2448 | friend into global scope, if appropriate. */ | |
ed44da02 | 2449 | if (TREE_CODE (type) != ENUMERAL_TYPE |
74b846e0 | 2450 | && !globalize && b->template_parms_p |
39c01e4c MM |
2451 | && b->level_chain->parm_flag == 2) |
2452 | { | |
61a127b3 | 2453 | finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type)); |
5e0c54e5 | 2454 | /* Put this UDT in the table of UDTs for the class, since |
39c01e4c MM |
2455 | that won't happen below because B is not the class |
2456 | binding level, but is instead the pseudo-global level. */ | |
5e0c54e5 GDR |
2457 | if (b->level_chain->type_decls == NULL) |
2458 | b->level_chain->type_decls = | |
2459 | binding_table_new (SCOPE_DEFAULT_HT_SIZE); | |
2460 | binding_table_insert (b->level_chain->type_decls, name, type); | |
d0f062fb | 2461 | if (!COMPLETE_TYPE_P (current_class_type)) |
7088fca9 KL |
2462 | { |
2463 | maybe_add_class_template_decl_list (current_class_type, | |
2464 | type, /*friend_p=*/0); | |
5e0c54e5 GDR |
2465 | CLASSTYPE_NESTED_UTDS (current_class_type) = |
2466 | b->level_chain->type_decls; | |
7088fca9 | 2467 | } |
39c01e4c MM |
2468 | } |
2469 | } | |
2470 | } | |
2471 | ||
2472 | return decl; | |
2473 | } | |
2474 | ||
9188c363 MM |
2475 | /* In C++, you don't have to write `struct S' to refer to `S'; you |
2476 | can just use `S'. We accomplish this by creating a TYPE_DECL as | |
2477 | if the user had written `typedef struct S S'. Create and return | |
2478 | the TYPE_DECL for TYPE. */ | |
2479 | ||
2480 | tree | |
11f6b451 | 2481 | create_implicit_typedef (tree name, tree type) |
9188c363 MM |
2482 | { |
2483 | tree decl; | |
2484 | ||
2485 | decl = build_decl (TYPE_DECL, name, type); | |
c727aa5e | 2486 | DECL_ARTIFICIAL (decl) = 1; |
9188c363 MM |
2487 | /* There are other implicit type declarations, like the one *within* |
2488 | a class that allows you to write `S::S'. We must distinguish | |
2489 | amongst these. */ | |
2490 | SET_DECL_IMPLICIT_TYPEDEF_P (decl); | |
2491 | TYPE_NAME (type) = decl; | |
2492 | ||
2493 | return decl; | |
2494 | } | |
2495 | ||
8e4ce833 JJ |
2496 | /* Remember a local name for name-mangling purposes. */ |
2497 | ||
2498 | static void | |
11f6b451 | 2499 | push_local_name (tree decl) |
8e4ce833 JJ |
2500 | { |
2501 | size_t i, nelts; | |
2502 | tree t, name; | |
2503 | ||
22ffcc6f | 2504 | timevar_push (TV_NAME_LOOKUP); |
8e4ce833 JJ |
2505 | if (!local_names) |
2506 | VARRAY_TREE_INIT (local_names, 8, "local_names"); | |
2507 | ||
2508 | name = DECL_NAME (decl); | |
2509 | ||
2510 | nelts = VARRAY_ACTIVE_SIZE (local_names); | |
2511 | for (i = 0; i < nelts; i++) | |
2512 | { | |
2513 | t = VARRAY_TREE (local_names, i); | |
2514 | if (DECL_NAME (t) == name) | |
2515 | { | |
2516 | if (!DECL_LANG_SPECIFIC (decl)) | |
2517 | retrofit_lang_decl (decl); | |
e2500fed | 2518 | DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1; |
8e4ce833 JJ |
2519 | if (DECL_LANG_SPECIFIC (t)) |
2520 | DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1; | |
2521 | else | |
2522 | DECL_DISCRIMINATOR (decl) = 1; | |
2523 | ||
2524 | VARRAY_TREE (local_names, i) = decl; | |
de94b46c KG |
2525 | timevar_pop (TV_NAME_LOOKUP); |
2526 | return; | |
8e4ce833 JJ |
2527 | } |
2528 | } | |
2529 | ||
2530 | VARRAY_PUSH_TREE (local_names, decl); | |
22ffcc6f | 2531 | timevar_pop (TV_NAME_LOOKUP); |
8e4ce833 JJ |
2532 | } |
2533 | ||
8d08fdba | 2534 | /* Push a tag name NAME for struct/class/union/enum type TYPE. |
6757edfe | 2535 | Normally put it into the inner-most non-tag-transparent scope, |
8d08fdba | 2536 | but if GLOBALIZE is true, put it in the inner-most non-class scope. |
e92cc029 | 2537 | The latter is needed for implicit declarations. */ |
8d08fdba MS |
2538 | |
2539 | void | |
11f6b451 | 2540 | pushtag (tree name, tree type, int globalize) |
8d08fdba | 2541 | { |
e2500fed | 2542 | register struct cp_binding_level *b; |
8d08fdba | 2543 | |
22ffcc6f | 2544 | timevar_push (TV_NAME_LOOKUP); |
8f032717 | 2545 | b = current_binding_level; |
8d08fdba | 2546 | while (b->tag_transparent |
fcebb2dd NS |
2547 | || (b->parm_flag == 2 |
2548 | && (globalize | |
2549 | /* We may be defining a new type in the initializer | |
2550 | of a static member variable. We allow this when | |
2551 | not pedantic, and it is particularly useful for | |
a1c65f9f | 2552 | type punning via an anonymous union. */ |
82a2669e | 2553 | || COMPLETE_TYPE_P (b->this_entity)))) |
8d08fdba MS |
2554 | b = b->level_chain; |
2555 | ||
5e0c54e5 GDR |
2556 | if (b->type_decls == NULL) |
2557 | b->type_decls = binding_table_new (SCOPE_DEFAULT_HT_SIZE); | |
2558 | binding_table_insert (b->type_decls, name, type); | |
8d08fdba MS |
2559 | |
2560 | if (name) | |
2561 | { | |
8d08fdba | 2562 | /* Do C++ gratuitous typedefing. */ |
db5ae43f | 2563 | if (IDENTIFIER_TYPE_VALUE (name) != type) |
8d08fdba | 2564 | { |
93cdc044 | 2565 | register tree d = NULL_TREE; |
848cf1e0 | 2566 | int in_class = 0; |
9188c363 | 2567 | tree context = TYPE_CONTEXT (type); |
280f9385 | 2568 | |
280f9385 MM |
2569 | if (! context) |
2570 | { | |
2571 | tree cs = current_scope (); | |
2572 | ||
2573 | if (! globalize) | |
2574 | context = cs; | |
2f939d94 | 2575 | else if (cs != NULL_TREE && TYPE_P (cs)) |
280f9385 MM |
2576 | /* When declaring a friend class of a local class, we want |
2577 | to inject the newly named class into the scope | |
2578 | containing the local class, not the namespace scope. */ | |
4f1c5b7d | 2579 | context = decl_function_context (get_type_decl (cs)); |
280f9385 | 2580 | } |
280f9385 MM |
2581 | if (!context) |
2582 | context = current_namespace; | |
8d08fdba | 2583 | |
74b846e0 | 2584 | if ((b->template_parms_p && b->level_chain->parm_flag == 2) |
93cdc044 JM |
2585 | || b->parm_flag == 2) |
2586 | in_class = 1; | |
93cdc044 | 2587 | |
848cf1e0 MM |
2588 | if (current_lang_name == lang_name_java) |
2589 | TYPE_FOR_JAVA (type) = 1; | |
93cdc044 | 2590 | |
9188c363 | 2591 | d = create_implicit_typedef (name, type); |
cb0dbb9a | 2592 | DECL_CONTEXT (d) = FROB_CONTEXT (context); |
9188c363 MM |
2593 | if (! in_class) |
2594 | set_identifier_type_value_with_scope (name, type, b); | |
e1cd6e56 | 2595 | |
39c01e4c MM |
2596 | d = maybe_process_template_type_declaration (type, |
2597 | globalize, b); | |
93cdc044 JM |
2598 | |
2599 | if (b->parm_flag == 2) | |
61a127b3 | 2600 | { |
848cf1e0 | 2601 | if (!PROCESSING_REAL_TEMPLATE_DECL_P ()) |
61a127b3 MM |
2602 | /* Put this TYPE_DECL on the TYPE_FIELDS list for the |
2603 | class. But if it's a member template class, we | |
2604 | want the TEMPLATE_DECL, not the TYPE_DECL, so this | |
2605 | is done later. */ | |
2606 | finish_member_declaration (d); | |
8f032717 MM |
2607 | else |
2608 | pushdecl_class_level (d); | |
61a127b3 | 2609 | } |
93cdc044 JM |
2610 | else |
2611 | d = pushdecl_with_scope (d, b); | |
2612 | ||
1951a1b6 | 2613 | /* FIXME what if it gets a name from typedef? */ |
848cf1e0 MM |
2614 | if (ANON_AGGRNAME_P (name)) |
2615 | DECL_IGNORED_P (d) = 1; | |
2616 | ||
2617 | TYPE_CONTEXT (type) = DECL_CONTEXT (d); | |
1f6e1acc AS |
2618 | |
2619 | /* If this is a local class, keep track of it. We need this | |
2620 | information for name-mangling, and so that it is possible to find | |
2621 | all function definitions in a translation unit in a convenient | |
2622 | way. (It's otherwise tricky to find a member function definition | |
2623 | it's only pointed to from within a local class.) */ | |
5362b086 | 2624 | if (TYPE_CONTEXT (type) |
089acd57 MM |
2625 | && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL |
2626 | && !processing_template_decl) | |
1f6e1acc | 2627 | VARRAY_PUSH_TREE (local_classes, type); |
8d08fdba MS |
2628 | } |
2629 | if (b->parm_flag == 2) | |
2630 | { | |
d0f062fb | 2631 | if (!COMPLETE_TYPE_P (current_class_type)) |
7088fca9 KL |
2632 | { |
2633 | maybe_add_class_template_decl_list (current_class_type, | |
2634 | type, /*friend_p=*/0); | |
5e0c54e5 | 2635 | CLASSTYPE_NESTED_UTDS (current_class_type) = b->type_decls; |
7088fca9 | 2636 | } |
8d08fdba MS |
2637 | } |
2638 | } | |
2639 | ||
2640 | if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL) | |
2641 | /* Use the canonical TYPE_DECL for this node. */ | |
2642 | TYPE_STUB_DECL (type) = TYPE_NAME (type); | |
2643 | else | |
2644 | { | |
2645 | /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE | |
2646 | will be the tagged type we just added to the current | |
2647 | binding level. This fake NULL-named TYPE_DECL node helps | |
2648 | dwarfout.c to know when it needs to output a | |
2649 | representation of a tagged type, and it also gives us a | |
2650 | convenient place to record the "scope start" address for | |
2651 | the tagged type. */ | |
2652 | ||
8d08fdba | 2653 | tree d = build_decl (TYPE_DECL, NULL_TREE, type); |
8d08fdba MS |
2654 | TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b); |
2655 | } | |
22ffcc6f | 2656 | timevar_pop (TV_NAME_LOOKUP); |
8d08fdba MS |
2657 | } |
2658 | ||
2659 | /* Counter used to create anonymous type names. */ | |
e92cc029 | 2660 | |
17211ab5 | 2661 | static GTY(()) int anon_cnt; |
8d08fdba MS |
2662 | |
2663 | /* Return an IDENTIFIER which can be used as a name for | |
2664 | anonymous structs and unions. */ | |
e92cc029 | 2665 | |
8d08fdba | 2666 | tree |
11f6b451 | 2667 | make_anon_name (void) |
8d08fdba MS |
2668 | { |
2669 | char buf[32]; | |
2670 | ||
2671 | sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++); | |
2672 | return get_identifier (buf); | |
2673 | } | |
2674 | ||
5e0c54e5 | 2675 | /* Clear the TREE_PURPOSE slot of UTDs which have anonymous typenames. |
8d08fdba | 2676 | This keeps dbxout from getting confused. */ |
e92cc029 | 2677 | |
8d08fdba | 2678 | void |
11f6b451 | 2679 | clear_anon_tags (void) |
8d08fdba | 2680 | { |
e2500fed | 2681 | register struct cp_binding_level *b; |
8d08fdba MS |
2682 | static int last_cnt = 0; |
2683 | ||
2684 | /* Fast out if no new anon names were declared. */ | |
2685 | if (last_cnt == anon_cnt) | |
2686 | return; | |
2687 | ||
2688 | b = current_binding_level; | |
2689 | while (b->tag_transparent) | |
2690 | b = b->level_chain; | |
5e0c54e5 GDR |
2691 | if (b->type_decls != NULL) |
2692 | binding_table_remove_anonymous_types (b->type_decls); | |
8d08fdba MS |
2693 | last_cnt = anon_cnt; |
2694 | } | |
2695 | \f | |
2696 | /* Subroutine of duplicate_decls: return truthvalue of whether | |
2697 | or not types of these decls match. | |
2698 | ||
2699 | For C++, we must compare the parameter list so that `int' can match | |
2700 | `int&' in a parameter position, but `int&' is not confused with | |
2701 | `const int&'. */ | |
e92cc029 | 2702 | |
6060a796 | 2703 | int |
11f6b451 | 2704 | decls_match (tree newdecl, tree olddecl) |
8d08fdba MS |
2705 | { |
2706 | int types_match; | |
2707 | ||
347d73d7 ML |
2708 | if (newdecl == olddecl) |
2709 | return 1; | |
2710 | ||
6b4b3deb MM |
2711 | if (TREE_CODE (newdecl) != TREE_CODE (olddecl)) |
2712 | /* If the two DECLs are not even the same kind of thing, we're not | |
2713 | interested in their types. */ | |
2714 | return 0; | |
2715 | ||
2716 | if (TREE_CODE (newdecl) == FUNCTION_DECL) | |
8d08fdba MS |
2717 | { |
2718 | tree f1 = TREE_TYPE (newdecl); | |
2719 | tree f2 = TREE_TYPE (olddecl); | |
2720 | tree p1 = TYPE_ARG_TYPES (f1); | |
2721 | tree p2 = TYPE_ARG_TYPES (f2); | |
2722 | ||
4f1c5b7d | 2723 | if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl) |
eb68cb58 MM |
2724 | && ! (DECL_EXTERN_C_P (newdecl) |
2725 | && DECL_EXTERN_C_P (olddecl))) | |
c5a6fc45 JM |
2726 | return 0; |
2727 | ||
8d08fdba | 2728 | if (TREE_CODE (f1) != TREE_CODE (f2)) |
e867a179 | 2729 | return 0; |
8d08fdba | 2730 | |
3bfdc719 | 2731 | if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2))) |
8926095f | 2732 | { |
8b27e9ef NS |
2733 | if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl) |
2734 | && (DECL_BUILT_IN (olddecl) | |
2735 | #ifndef NO_IMPLICIT_EXTERN_C | |
2736 | || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl)) | |
2737 | || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl)) | |
2738 | #endif | |
2739 | )) | |
a28e3c7f MS |
2740 | { |
2741 | types_match = self_promoting_args_p (p1); | |
2742 | if (p1 == void_list_node) | |
2743 | TREE_TYPE (newdecl) = TREE_TYPE (olddecl); | |
2744 | } | |
8b27e9ef NS |
2745 | #ifndef NO_IMPLICIT_EXTERN_C |
2746 | else if (p1 == NULL_TREE | |
2747 | && (DECL_EXTERN_C_P (olddecl) | |
2748 | && DECL_IN_SYSTEM_HEADER (olddecl) | |
2749 | && !DECL_CLASS_SCOPE_P (olddecl)) | |
2750 | && (DECL_EXTERN_C_P (newdecl) | |
2751 | && DECL_IN_SYSTEM_HEADER (newdecl) | |
2752 | && !DECL_CLASS_SCOPE_P (newdecl))) | |
a28e3c7f MS |
2753 | { |
2754 | types_match = self_promoting_args_p (p2); | |
2755 | TREE_TYPE (newdecl) = TREE_TYPE (olddecl); | |
2756 | } | |
8b27e9ef | 2757 | #endif |
8926095f | 2758 | else |
91063b51 | 2759 | types_match = compparms (p1, p2); |
8926095f | 2760 | } |
8d08fdba MS |
2761 | else |
2762 | types_match = 0; | |
2763 | } | |
6b4b3deb | 2764 | else if (TREE_CODE (newdecl) == TEMPLATE_DECL) |
51c184be | 2765 | { |
f84b4be9 JM |
2766 | if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl), |
2767 | DECL_TEMPLATE_PARMS (olddecl))) | |
2768 | return 0; | |
68642fb6 | 2769 | |
2bb5d995 JM |
2770 | if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) |
2771 | != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))) | |
2772 | return 0; | |
2773 | ||
f84b4be9 JM |
2774 | if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL) |
2775 | types_match = 1; | |
2776 | else | |
2777 | types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl), | |
2778 | DECL_TEMPLATE_RESULT (newdecl)); | |
51c184be | 2779 | } |
8d08fdba MS |
2780 | else |
2781 | { | |
2782 | if (TREE_TYPE (newdecl) == error_mark_node) | |
2783 | types_match = TREE_TYPE (olddecl) == error_mark_node; | |
2784 | else if (TREE_TYPE (olddecl) == NULL_TREE) | |
2785 | types_match = TREE_TYPE (newdecl) == NULL_TREE; | |
a0a33927 MS |
2786 | else if (TREE_TYPE (newdecl) == NULL_TREE) |
2787 | types_match = 0; | |
8d08fdba | 2788 | else |
01240200 | 2789 | types_match = comptypes (TREE_TYPE (newdecl), |
3bfdc719 MM |
2790 | TREE_TYPE (olddecl), |
2791 | COMPARE_REDECLARATION); | |
8d08fdba MS |
2792 | } |
2793 | ||
2794 | return types_match; | |
2795 | } | |
2796 | ||
2797 | /* If NEWDECL is `static' and an `extern' was seen previously, | |
24f30ed4 | 2798 | warn about it. OLDDECL is the previous declaration. |
8d08fdba MS |
2799 | |
2800 | Note that this does not apply to the C++ case of declaring | |
2801 | a variable `extern const' and then later `const'. | |
2802 | ||
8d08fdba MS |
2803 | Don't complain about built-in functions, since they are beyond |
2804 | the user's control. */ | |
2805 | ||
2806 | static void | |
11f6b451 | 2807 | warn_extern_redeclared_static (tree newdecl, tree olddecl) |
8d08fdba | 2808 | { |
83182544 | 2809 | static const char *const explicit_extern_static_warning |
8251199e | 2810 | = "`%D' was declared `extern' and later `static'"; |
83182544 | 2811 | static const char *const implicit_extern_static_warning |
8251199e JM |
2812 | = "`%D' was declared implicitly `extern' and later `static'"; |
2813 | ||
24f30ed4 MM |
2814 | tree name; |
2815 | ||
68642fb6 | 2816 | if (TREE_CODE (newdecl) == TYPE_DECL |
655dc6ee JM |
2817 | || TREE_CODE (newdecl) == TEMPLATE_DECL |
2818 | || TREE_CODE (newdecl) == CONST_DECL) | |
8d08fdba | 2819 | return; |
68642fb6 | 2820 | |
963d5758 MM |
2821 | /* Don't get confused by static member functions; that's a different |
2822 | use of `static'. */ | |
2823 | if (TREE_CODE (newdecl) == FUNCTION_DECL | |
2824 | && DECL_STATIC_FUNCTION_P (newdecl)) | |
2825 | return; | |
8d08fdba | 2826 | |
24f30ed4 MM |
2827 | /* If the old declaration was `static', or the new one isn't, then |
2828 | then everything is OK. */ | |
2829 | if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl)) | |
2830 | return; | |
2831 | ||
2832 | /* It's OK to declare a builtin function as `static'. */ | |
2833 | if (TREE_CODE (olddecl) == FUNCTION_DECL | |
2834 | && DECL_ARTIFICIAL (olddecl)) | |
2835 | return; | |
2836 | ||
8d08fdba | 2837 | name = DECL_ASSEMBLER_NAME (newdecl); |
33bd39a2 | 2838 | pedwarn (IDENTIFIER_IMPLICIT_DECL (name) |
24f30ed4 MM |
2839 | ? implicit_extern_static_warning |
2840 | : explicit_extern_static_warning, newdecl); | |
2841 | cp_pedwarn_at ("previous declaration of `%D'", olddecl); | |
8d08fdba MS |
2842 | } |
2843 | ||
2844 | /* Handle when a new declaration NEWDECL has the same name as an old | |
2845 | one OLDDECL in the same binding contour. Prints an error message | |
2846 | if appropriate. | |
2847 | ||
2848 | If safely possible, alter OLDDECL to look like NEWDECL, and return 1. | |
2849 | Otherwise, return 0. */ | |
2850 | ||
51c184be | 2851 | int |
11f6b451 | 2852 | duplicate_decls (tree newdecl, tree olddecl) |
8d08fdba | 2853 | { |
8d08fdba MS |
2854 | unsigned olddecl_uid = DECL_UID (olddecl); |
2855 | int olddecl_friend = 0, types_match = 0; | |
0b60dfe3 | 2856 | int new_defines_function = 0; |
5566b478 MS |
2857 | |
2858 | if (newdecl == olddecl) | |
2859 | return 1; | |
8d08fdba | 2860 | |
8926095f | 2861 | types_match = decls_match (newdecl, olddecl); |
8d08fdba | 2862 | |
8d08fdba MS |
2863 | /* If either the type of the new decl or the type of the old decl is an |
2864 | error_mark_node, then that implies that we have already issued an | |
2865 | error (earlier) for some bogus type specification, and in that case, | |
2866 | it is rather pointless to harass the user with yet more error message | |
0b60dfe3 | 2867 | about the same declaration, so just pretend the types match here. */ |
bd6dd845 MS |
2868 | if (TREE_TYPE (newdecl) == error_mark_node |
2869 | || TREE_TYPE (olddecl) == error_mark_node) | |
8d08fdba | 2870 | types_match = 1; |
68642fb6 | 2871 | |
97055d5c AO |
2872 | if (DECL_P (olddecl) |
2873 | && TREE_CODE (newdecl) == FUNCTION_DECL | |
2874 | && TREE_CODE (olddecl) == FUNCTION_DECL | |
2875 | && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl))) | |
2876 | { | |
2877 | if (DECL_DECLARED_INLINE_P (newdecl) | |
2878 | && DECL_UNINLINABLE (newdecl) | |
2879 | && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))) | |
2880 | /* Already warned elsewhere. */; | |
2881 | else if (DECL_DECLARED_INLINE_P (olddecl) | |
2882 | && DECL_UNINLINABLE (olddecl) | |
2883 | && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl))) | |
2884 | /* Already warned. */; | |
2885 | else if (DECL_DECLARED_INLINE_P (newdecl) | |
2886 | && DECL_UNINLINABLE (olddecl) | |
2887 | && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl))) | |
2888 | { | |
456d8864 GDR |
2889 | warning ("%Hfunction '%D' redeclared as inline", |
2890 | &DECL_SOURCE_LOCATION (newdecl), newdecl); | |
2891 | warning ("%Hprevious declaration of '%D' with attribute noinline", | |
2892 | &DECL_SOURCE_LOCATION (olddecl), olddecl); | |
97055d5c AO |
2893 | } |
2894 | else if (DECL_DECLARED_INLINE_P (olddecl) | |
2895 | && DECL_UNINLINABLE (newdecl) | |
2896 | && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))) | |
2897 | { | |
456d8864 GDR |
2898 | warning ("%Hfunction '%D' redeclared with attribute noinline", |
2899 | &DECL_SOURCE_LOCATION (newdecl), newdecl); | |
2900 | warning ("%Hprevious declaration of '%D' was inline", | |
2901 | &DECL_SOURCE_LOCATION (olddecl), olddecl); | |
97055d5c AO |
2902 | } |
2903 | } | |
2904 | ||
a1c65f9f | 2905 | /* Check for redeclaration and other discrepancies. */ |
d22c8596 | 2906 | if (TREE_CODE (olddecl) == FUNCTION_DECL |
9f33663b JM |
2907 | && DECL_ARTIFICIAL (olddecl)) |
2908 | { | |
2909 | if (TREE_CODE (newdecl) != FUNCTION_DECL) | |
2910 | { | |
d52e4867 RS |
2911 | /* Avoid warnings redeclaring anticipated built-ins. */ |
2912 | if (DECL_ANTICIPATED (olddecl)) | |
2913 | return 0; | |
2914 | ||
9f33663b JM |
2915 | /* If you declare a built-in or predefined function name as static, |
2916 | the old definition is overridden, but optionally warn this was a | |
2917 | bad choice of name. */ | |
2918 | if (! TREE_PUBLIC (newdecl)) | |
2919 | { | |
2920 | if (warn_shadow) | |
33bd39a2 | 2921 | warning ("shadowing %s function `%#D'", |
9f33663b JM |
2922 | DECL_BUILT_IN (olddecl) ? "built-in" : "library", |
2923 | olddecl); | |
2924 | /* Discard the old built-in function. */ | |
2925 | return 0; | |
2926 | } | |
2927 | /* If the built-in is not ansi, then programs can override | |
2928 | it even globally without an error. */ | |
2929 | else if (! DECL_BUILT_IN (olddecl)) | |
33bd39a2 | 2930 | warning ("library function `%#D' redeclared as non-function `%#D'", |
9f33663b JM |
2931 | olddecl, newdecl); |
2932 | else | |
2933 | { | |
33bd39a2 ZW |
2934 | error ("declaration of `%#D'", newdecl); |
2935 | error ("conflicts with built-in declaration `%#D'", | |
a4443a08 | 2936 | olddecl); |
9f33663b | 2937 | } |
a4443a08 MS |
2938 | return 0; |
2939 | } | |
9f33663b | 2940 | else if (!types_match) |
8d08fdba | 2941 | { |
d52e4867 RS |
2942 | /* Avoid warnings redeclaring anticipated built-ins. */ |
2943 | if (DECL_ANTICIPATED (olddecl)) | |
2944 | ; /* Do nothing yet. */ | |
2945 | else if ((DECL_EXTERN_C_P (newdecl) | |
eb68cb58 | 2946 | && DECL_EXTERN_C_P (olddecl)) |
9f33663b JM |
2947 | || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)), |
2948 | TYPE_ARG_TYPES (TREE_TYPE (olddecl)))) | |
a4443a08 | 2949 | { |
9f33663b JM |
2950 | /* A near match; override the builtin. */ |
2951 | ||
2952 | if (TREE_PUBLIC (newdecl)) | |
a4443a08 | 2953 | { |
33bd39a2 ZW |
2954 | warning ("new declaration `%#D'", newdecl); |
2955 | warning ("ambiguates built-in declaration `%#D'", | |
9f33663b | 2956 | olddecl); |
a4443a08 | 2957 | } |
9f33663b | 2958 | else if (warn_shadow) |
33bd39a2 | 2959 | warning ("shadowing %s function `%#D'", |
9f33663b JM |
2960 | DECL_BUILT_IN (olddecl) ? "built-in" : "library", |
2961 | olddecl); | |
a4443a08 | 2962 | } |
9f33663b JM |
2963 | else |
2964 | /* Discard the old built-in function. */ | |
2965 | return 0; | |
04a9d080 RS |
2966 | |
2967 | /* Replace the old RTL to avoid problems with inlining. */ | |
2968 | SET_DECL_RTL (olddecl, DECL_RTL (newdecl)); | |
8d08fdba | 2969 | } |
10326855 | 2970 | /* Even if the types match, prefer the new declarations type |
7df87825 | 2971 | for anticipated built-ins, for exception lists, etc... */ |
10326855 RS |
2972 | else if (DECL_ANTICIPATED (olddecl)) |
2973 | TREE_TYPE (olddecl) = TREE_TYPE (newdecl); | |
68642fb6 | 2974 | |
24f30ed4 MM |
2975 | if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl)) |
2976 | { | |
2977 | /* If a builtin function is redeclared as `static', merge | |
2978 | the declarations, but make the original one static. */ | |
2979 | DECL_THIS_STATIC (olddecl) = 1; | |
2980 | TREE_PUBLIC (olddecl) = 0; | |
9bfadf57 | 2981 | |
421844e7 MM |
2982 | /* Make the old declaration consistent with the new one so |
2983 | that all remnants of the builtin-ness of this function | |
2984 | will be banished. */ | |
5d2ed28c | 2985 | SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl)); |
19e7881c | 2986 | SET_DECL_RTL (olddecl, DECL_RTL (newdecl)); |
24f30ed4 | 2987 | } |
39211cd5 MS |
2988 | } |
2989 | else if (TREE_CODE (olddecl) != TREE_CODE (newdecl)) | |
2990 | { | |
9ed182dc JM |
2991 | if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl) |
2992 | && TREE_CODE (newdecl) != TYPE_DECL | |
2993 | && ! (TREE_CODE (newdecl) == TEMPLATE_DECL | |
2994 | && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)) | |
2995 | || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl) | |
2996 | && TREE_CODE (olddecl) != TYPE_DECL | |
2997 | && ! (TREE_CODE (olddecl) == TEMPLATE_DECL | |
2998 | && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) | |
2999 | == TYPE_DECL)))) | |
3000 | { | |
3001 | /* We do nothing special here, because C++ does such nasty | |
3002 | things with TYPE_DECLs. Instead, just let the TYPE_DECL | |
3003 | get shadowed, and know that if we need to find a TYPE_DECL | |
3004 | for a given name, we can look in the IDENTIFIER_TYPE_VALUE | |
3005 | slot of the identifier. */ | |
3006 | return 0; | |
3007 | } | |
3008 | ||
39211cd5 | 3009 | if ((TREE_CODE (newdecl) == FUNCTION_DECL |
5566b478 | 3010 | && DECL_FUNCTION_TEMPLATE_P (olddecl)) |
39211cd5 | 3011 | || (TREE_CODE (olddecl) == FUNCTION_DECL |
5566b478 | 3012 | && DECL_FUNCTION_TEMPLATE_P (newdecl))) |
39211cd5 | 3013 | return 0; |
9ed182dc | 3014 | |
33bd39a2 | 3015 | error ("`%#D' redeclared as different kind of symbol", newdecl); |
39211cd5 MS |
3016 | if (TREE_CODE (olddecl) == TREE_LIST) |
3017 | olddecl = TREE_VALUE (olddecl); | |
8251199e | 3018 | cp_error_at ("previous declaration of `%#D'", olddecl); |
39211cd5 MS |
3019 | |
3020 | /* New decl is completely inconsistent with the old one => | |
3021 | tell caller to replace the old one. */ | |
3022 | ||
3023 | return 0; | |
8d08fdba | 3024 | } |
8d08fdba MS |
3025 | else if (!types_match) |
3026 | { | |
4f1c5b7d | 3027 | if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)) |
7bdbfa05 MM |
3028 | /* These are certainly not duplicate declarations; they're |
3029 | from different scopes. */ | |
3030 | return 0; | |
3031 | ||
8926095f | 3032 | if (TREE_CODE (newdecl) == TEMPLATE_DECL) |
f0e01782 MS |
3033 | { |
3034 | /* The name of a class template may not be declared to refer to | |
3035 | any other template, class, function, object, namespace, value, | |
e92cc029 | 3036 | or type in the same scope. */ |
5566b478 MS |
3037 | if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL |
3038 | || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL) | |
f0e01782 | 3039 | { |
33bd39a2 | 3040 | error ("declaration of template `%#D'", newdecl); |
8251199e | 3041 | cp_error_at ("conflicts with previous declaration `%#D'", |
f0e01782 MS |
3042 | olddecl); |
3043 | } | |
ec255269 MS |
3044 | else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL |
3045 | && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL | |
3046 | && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))), | |
91063b51 | 3047 | TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl)))) |
75650646 | 3048 | && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl), |
b9201622 NS |
3049 | DECL_TEMPLATE_PARMS (olddecl)) |
3050 | /* Template functions can be disambiguated by | |
3051 | return type. */ | |
3052 | && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)), | |
3053 | TREE_TYPE (TREE_TYPE (olddecl)))) | |
ec255269 | 3054 | { |
33bd39a2 | 3055 | error ("new declaration `%#D'", newdecl); |
8251199e | 3056 | cp_error_at ("ambiguates old declaration `%#D'", olddecl); |
ec255269 | 3057 | } |
f0e01782 MS |
3058 | return 0; |
3059 | } | |
8926095f MS |
3060 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
3061 | { | |
eb68cb58 | 3062 | if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl)) |
8926095f | 3063 | { |
33bd39a2 | 3064 | error ("declaration of C function `%#D' conflicts with", |
8926095f | 3065 | newdecl); |
8251199e | 3066 | cp_error_at ("previous declaration `%#D' here", olddecl); |
8926095f | 3067 | } |
00595019 | 3068 | else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)), |
91063b51 | 3069 | TYPE_ARG_TYPES (TREE_TYPE (olddecl)))) |
700f8a87 | 3070 | { |
33bd39a2 | 3071 | error ("new declaration `%#D'", newdecl); |
8251199e | 3072 | cp_error_at ("ambiguates old declaration `%#D'", olddecl); |
700f8a87 MS |
3073 | } |
3074 | else | |
3075 | return 0; | |
8926095f | 3076 | } |
8d08fdba MS |
3077 | |
3078 | /* Already complained about this, so don't do so again. */ | |
a4443a08 | 3079 | else if (current_class_type == NULL_TREE |
8d08fdba MS |
3080 | || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type) |
3081 | { | |
33bd39a2 | 3082 | error ("conflicting types for `%#D'", newdecl); |
8251199e | 3083 | cp_error_at ("previous declaration as `%#D'", olddecl); |
8d08fdba MS |
3084 | } |
3085 | } | |
68642fb6 | 3086 | else if (TREE_CODE (newdecl) == FUNCTION_DECL |
75650646 MM |
3087 | && ((DECL_TEMPLATE_SPECIALIZATION (olddecl) |
3088 | && (!DECL_TEMPLATE_INFO (newdecl) | |
68642fb6 | 3089 | || (DECL_TI_TEMPLATE (newdecl) |
75650646 MM |
3090 | != DECL_TI_TEMPLATE (olddecl)))) |
3091 | || (DECL_TEMPLATE_SPECIALIZATION (newdecl) | |
3092 | && (!DECL_TEMPLATE_INFO (olddecl) | |
68642fb6 | 3093 | || (DECL_TI_TEMPLATE (olddecl) |
75650646 | 3094 | != DECL_TI_TEMPLATE (newdecl)))))) |
386b8a85 JM |
3095 | /* It's OK to have a template specialization and a non-template |
3096 | with the same type, or to have specializations of two | |
75650646 MM |
3097 | different templates with the same type. Note that if one is a |
3098 | specialization, and the other is an instantiation of the same | |
3099 | template, that we do not exit at this point. That situation | |
3100 | can occur if we instantiate a template class, and then | |
0e339752 | 3101 | specialize one of its methods. This situation is valid, but |
75650646 MM |
3102 | the declarations must be merged in the usual way. */ |
3103 | return 0; | |
68642fb6 UD |
3104 | else if (TREE_CODE (newdecl) == FUNCTION_DECL |
3105 | && ((DECL_TEMPLATE_INSTANTIATION (olddecl) | |
75650646 MM |
3106 | && !DECL_USE_TEMPLATE (newdecl)) |
3107 | || (DECL_TEMPLATE_INSTANTIATION (newdecl) | |
3108 | && !DECL_USE_TEMPLATE (olddecl)))) | |
3109 | /* One of the declarations is a template instantiation, and the | |
3110 | other is not a template at all. That's OK. */ | |
386b8a85 | 3111 | return 0; |
85c6cbaf ML |
3112 | else if (TREE_CODE (newdecl) == NAMESPACE_DECL |
3113 | && DECL_NAMESPACE_ALIAS (newdecl) | |
3114 | && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl)) | |
a1c65f9f | 3115 | /* Redeclaration of namespace alias, ignore it. */ |
85c6cbaf | 3116 | return 1; |
8d08fdba MS |
3117 | else |
3118 | { | |
d8e178a0 | 3119 | const char *errmsg = redeclaration_error_message (newdecl, olddecl); |
8251199e | 3120 | if (errmsg) |
8d08fdba | 3121 | { |
33bd39a2 | 3122 | error (errmsg, newdecl); |
8d08fdba MS |
3123 | if (DECL_NAME (olddecl) != NULL_TREE) |
3124 | cp_error_at ((DECL_INITIAL (olddecl) | |
2c73f9f5 | 3125 | && namespace_bindings_p ()) |
8251199e JM |
3126 | ? "`%#D' previously defined here" |
3127 | : "`%#D' previously declared here", olddecl); | |
00fae85d | 3128 | return 0; |
8d08fdba MS |
3129 | } |
3130 | else if (TREE_CODE (olddecl) == FUNCTION_DECL | |
3131 | && DECL_INITIAL (olddecl) != NULL_TREE | |
3132 | && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE | |
3133 | && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE) | |
3134 | { | |
3135 | /* Prototype decl follows defn w/o prototype. */ | |
8251199e JM |
3136 | cp_warning_at ("prototype for `%#D'", newdecl); |
3137 | cp_warning_at ("follows non-prototype definition here", olddecl); | |
8d08fdba MS |
3138 | } |
3139 | else if (TREE_CODE (olddecl) == FUNCTION_DECL | |
3140 | && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl)) | |
8926095f MS |
3141 | { |
3142 | /* extern "C" int foo (); | |
3143 | int foo () { bar (); } | |
3144 | is OK. */ | |
46ccf50a | 3145 | if (current_lang_depth () == 0) |
5d2ed28c | 3146 | SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl)); |
8926095f MS |
3147 | else |
3148 | { | |
8251199e | 3149 | cp_error_at ("previous declaration of `%#D' with %L linkage", |
8926095f | 3150 | olddecl, DECL_LANGUAGE (olddecl)); |
33bd39a2 | 3151 | error ("conflicts with new declaration with %L linkage", |
8926095f MS |
3152 | DECL_LANGUAGE (newdecl)); |
3153 | } | |
3154 | } | |
e1cd6e56 | 3155 | |
a6f02587 | 3156 | if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl)) |
c32381b1 JM |
3157 | ; |
3158 | else if (TREE_CODE (olddecl) == FUNCTION_DECL) | |
e1cd6e56 MS |
3159 | { |
3160 | tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl)); | |
3161 | tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)); | |
3162 | int i = 1; | |
3163 | ||
3164 | if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE) | |
3165 | t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2); | |
68642fb6 | 3166 | |
e1cd6e56 MS |
3167 | for (; t1 && t1 != void_list_node; |
3168 | t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++) | |
3169 | if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2)) | |
3170 | { | |
2507f3b5 RK |
3171 | if (1 == simple_cst_equal (TREE_PURPOSE (t1), |
3172 | TREE_PURPOSE (t2))) | |
e1cd6e56 | 3173 | { |
624a5800 CR |
3174 | pedwarn ("default argument given for parameter %d of `%#D'", |
3175 | i, newdecl); | |
3176 | cp_pedwarn_at ("after previous specification in `%#D'", | |
3177 | olddecl); | |
e1cd6e56 MS |
3178 | } |
3179 | else | |
3180 | { | |
33bd39a2 | 3181 | error ("default argument given for parameter %d of `%#D'", |
e1cd6e56 | 3182 | i, newdecl); |
8251199e | 3183 | cp_error_at ("after previous specification in `%#D'", |
e1cd6e56 MS |
3184 | olddecl); |
3185 | } | |
3186 | } | |
a5894242 | 3187 | |
79065db2 MM |
3188 | if (DECL_DECLARED_INLINE_P (newdecl) |
3189 | && ! DECL_DECLARED_INLINE_P (olddecl) | |
7fcdf4c2 | 3190 | && TREE_ADDRESSABLE (olddecl) && warn_inline) |
dff6b454 | 3191 | { |
33bd39a2 | 3192 | warning ("`%#D' was used before it was declared inline", |
7fcdf4c2 | 3193 | newdecl); |
8251199e | 3194 | cp_warning_at ("previous non-inline declaration here", |
7fcdf4c2 | 3195 | olddecl); |
dff6b454 | 3196 | } |
e1cd6e56 | 3197 | } |
8d08fdba MS |
3198 | } |
3199 | ||
4782bd5b RS |
3200 | /* Do not merge an implicit typedef with an explicit one. In: |
3201 | ||
3202 | class A; | |
3203 | ... | |
3204 | typedef class A A __attribute__ ((foo)); | |
3205 | ||
3206 | the attribute should apply only to the typedef. */ | |
3207 | if (TREE_CODE (olddecl) == TYPE_DECL | |
3208 | && (DECL_IMPLICIT_TYPEDEF_P (olddecl) | |
3209 | || DECL_IMPLICIT_TYPEDEF_P (newdecl))) | |
3210 | return 0; | |
3211 | ||
8d08fdba MS |
3212 | /* If new decl is `static' and an `extern' was seen previously, |
3213 | warn about it. */ | |
3214 | warn_extern_redeclared_static (newdecl, olddecl); | |
3215 | ||
e92cc029 | 3216 | /* We have committed to returning 1 at this point. */ |
8d08fdba MS |
3217 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
3218 | { | |
3219 | /* Now that functions must hold information normally held | |
3220 | by field decls, there is extra work to do so that | |
3221 | declaration information does not get destroyed during | |
3222 | definition. */ | |
3223 | if (DECL_VINDEX (olddecl)) | |
3224 | DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl); | |
3225 | if (DECL_CONTEXT (olddecl)) | |
3226 | DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl); | |
e1cd6e56 MS |
3227 | DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl); |
3228 | DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl); | |
fee7654e | 3229 | DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl); |
864b83b9 | 3230 | DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl); |
4a67c9e9 | 3231 | DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl); |
963d5758 | 3232 | DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl); |
e0fff4b3 JM |
3233 | if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK) |
3234 | SET_OVERLOADED_OPERATOR_CODE | |
3235 | (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl)); | |
0b60dfe3 | 3236 | new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE; |
68642fb6 | 3237 | |
0b60dfe3 BK |
3238 | /* Optionally warn about more than one declaration for the same |
3239 | name, but don't warn about a function declaration followed by a | |
3240 | definition. */ | |
3241 | if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl) | |
3242 | && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE) | |
a1c65f9f | 3243 | /* Don't warn about extern decl followed by definition. */ |
0b60dfe3 | 3244 | && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl)) |
a1c65f9f | 3245 | /* Don't warn about friends, let add_friend take care of it. */ |
3581fae0 | 3246 | && ! (DECL_FRIEND_P (newdecl) || DECL_FRIEND_P (olddecl))) |
0b60dfe3 | 3247 | { |
33bd39a2 | 3248 | warning ("redundant redeclaration of `%D' in same scope", newdecl); |
8251199e | 3249 | cp_warning_at ("previous declaration of `%D'", olddecl); |
0b60dfe3 | 3250 | } |
8d08fdba MS |
3251 | } |
3252 | ||
3253 | /* Deal with C++: must preserve virtual function table size. */ | |
3254 | if (TREE_CODE (olddecl) == TYPE_DECL) | |
3255 | { | |
3256 | register tree newtype = TREE_TYPE (newdecl); | |
3257 | register tree oldtype = TREE_TYPE (olddecl); | |
3258 | ||
3259 | if (newtype != error_mark_node && oldtype != error_mark_node | |
3260 | && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype)) | |
e93ee644 MM |
3261 | CLASSTYPE_FRIEND_CLASSES (newtype) |
3262 | = CLASSTYPE_FRIEND_CLASSES (oldtype); | |
7df87825 | 3263 | |
970d6386 | 3264 | DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl); |
8d08fdba MS |
3265 | } |
3266 | ||
8d08fdba MS |
3267 | /* Copy all the DECL_... slots specified in the new decl |
3268 | except for any that we copy here from the old type. */ | |
91d231cb | 3269 | DECL_ATTRIBUTES (newdecl) |
f6897b10 | 3270 | = (*targetm.merge_decl_attributes) (olddecl, newdecl); |
8d08fdba | 3271 | |
5566b478 MS |
3272 | if (TREE_CODE (newdecl) == TEMPLATE_DECL) |
3273 | { | |
4d85e00e | 3274 | TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl)); |
68642fb6 | 3275 | DECL_TEMPLATE_SPECIALIZATIONS (olddecl) |
4d85e00e MM |
3276 | = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl), |
3277 | DECL_TEMPLATE_SPECIALIZATIONS (newdecl)); | |
68642fb6 | 3278 | |
daa8df65 MM |
3279 | /* If the new declaration is a definition, update the file and |
3280 | line information on the declaration. */ | |
06d5e633 NS |
3281 | if (DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)) == NULL_TREE |
3282 | && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl)) != NULL_TREE) | |
daa8df65 | 3283 | { |
3e72ec9a GDR |
3284 | DECL_SOURCE_LOCATION (olddecl) |
3285 | = DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (olddecl)) | |
3286 | = DECL_SOURCE_LOCATION (newdecl); | |
daa8df65 MM |
3287 | } |
3288 | ||
5566b478 MS |
3289 | return 1; |
3290 | } | |
68642fb6 | 3291 | |
8d08fdba MS |
3292 | if (types_match) |
3293 | { | |
3294 | /* Automatically handles default parameters. */ | |
3295 | tree oldtype = TREE_TYPE (olddecl); | |
e1cd6e56 | 3296 | tree newtype; |
8d08fdba | 3297 | |
e1cd6e56 | 3298 | /* Merge the data types specified in the two decls. */ |
6da794e8 | 3299 | newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl)); |
e1cd6e56 | 3300 | |
6da794e8 | 3301 | /* If merge_types produces a non-typedef type, just use the old type. */ |
9076e292 JM |
3302 | if (TREE_CODE (newdecl) == TYPE_DECL |
3303 | && newtype == DECL_ORIGINAL_TYPE (newdecl)) | |
3304 | newtype = oldtype; | |
3305 | ||
8d08fdba | 3306 | if (TREE_CODE (newdecl) == VAR_DECL) |
17bbb839 MM |
3307 | { |
3308 | DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl); | |
3309 | DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl); | |
3310 | } | |
3311 | ||
6da794e8 | 3312 | /* Do this after calling `merge_types' so that default |
8d08fdba MS |
3313 | parameters don't confuse us. */ |
3314 | else if (TREE_CODE (newdecl) == FUNCTION_DECL | |
3315 | && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)) | |
3316 | != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)))) | |
3317 | { | |
f30432d7 | 3318 | TREE_TYPE (newdecl) = build_exception_variant (newtype, |
8d08fdba | 3319 | TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))); |
f30432d7 | 3320 | TREE_TYPE (olddecl) = build_exception_variant (newtype, |
8d08fdba MS |
3321 | TYPE_RAISES_EXCEPTIONS (oldtype)); |
3322 | ||
9a224b4a JM |
3323 | if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl)) |
3324 | && DECL_SOURCE_LINE (olddecl) != 0 | |
da20811c | 3325 | && flag_exceptions |
4cc1d462 NS |
3326 | && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)), |
3327 | TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1)) | |
8d08fdba | 3328 | { |
33bd39a2 | 3329 | error ("declaration of `%F' throws different exceptions", |
a28e3c7f | 3330 | newdecl); |
a09ba2e0 | 3331 | cp_error_at ("than previous declaration `%F'", olddecl); |
8d08fdba MS |
3332 | } |
3333 | } | |
3334 | TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype; | |
3335 | ||
3336 | /* Lay the type out, unless already done. */ | |
407f03b8 | 3337 | if (! same_type_p (newtype, oldtype) |
5566b478 | 3338 | && TREE_TYPE (newdecl) != error_mark_node |
5156628f | 3339 | && !(processing_template_decl && uses_template_parms (newdecl))) |
b7484fbe MS |
3340 | layout_type (TREE_TYPE (newdecl)); |
3341 | ||
5566b478 MS |
3342 | if ((TREE_CODE (newdecl) == VAR_DECL |
3343 | || TREE_CODE (newdecl) == PARM_DECL | |
3344 | || TREE_CODE (newdecl) == RESULT_DECL | |
3345 | || TREE_CODE (newdecl) == FIELD_DECL | |
3346 | || TREE_CODE (newdecl) == TYPE_DECL) | |
5156628f | 3347 | && !(processing_template_decl && uses_template_parms (newdecl))) |
b7484fbe | 3348 | layout_decl (newdecl, 0); |
8d08fdba MS |
3349 | |
3350 | /* Merge the type qualifiers. */ | |
3351 | if (TREE_READONLY (newdecl)) | |
3352 | TREE_READONLY (olddecl) = 1; | |
3353 | if (TREE_THIS_VOLATILE (newdecl)) | |
3354 | TREE_THIS_VOLATILE (olddecl) = 1; | |
3355 | ||
3356 | /* Merge the initialization information. */ | |
8926095f MS |
3357 | if (DECL_INITIAL (newdecl) == NULL_TREE |
3358 | && DECL_INITIAL (olddecl) != NULL_TREE) | |
3359 | { | |
3360 | DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl); | |
3e72ec9a | 3361 | DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl); |
b0d06515 MM |
3362 | if (CAN_HAVE_FULL_LANG_DECL_P (newdecl) |
3363 | && DECL_LANG_SPECIFIC (newdecl) | |
4d85e00e MM |
3364 | && DECL_LANG_SPECIFIC (olddecl)) |
3365 | DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl); | |
8926095f | 3366 | } |
39211cd5 MS |
3367 | |
3368 | /* Merge the section attribute. | |
3369 | We want to issue an error if the sections conflict but that must be | |
3370 | done later in decl_attributes since we are called before attributes | |
3371 | are assigned. */ | |
3372 | if (DECL_SECTION_NAME (newdecl) == NULL_TREE) | |
3373 | DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl); | |
3374 | ||
a157febd GK |
3375 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
3376 | { | |
3377 | DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl) | |
3378 | |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl); | |
1331d16f | 3379 | DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl); |
220a35cc RS |
3380 | TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl); |
3381 | TREE_READONLY (newdecl) |= TREE_READONLY (olddecl); | |
3382 | TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl); | |
1331d16f RS |
3383 | DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl); |
3384 | DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl); | |
c740732f MM |
3385 | /* Keep the old RTL. */ |
3386 | COPY_DECL_RTL (olddecl, newdecl); | |
3387 | } | |
3388 | else if (TREE_CODE (newdecl) == VAR_DECL | |
3389 | && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl))) | |
3390 | { | |
3391 | /* Keep the old RTL. We cannot keep the old RTL if the old | |
3392 | declaration was for an incomplete object and the new | |
3393 | declaration is not since many attributes of the RTL will | |
3394 | change. */ | |
3395 | COPY_DECL_RTL (olddecl, newdecl); | |
a157febd | 3396 | } |
8d08fdba MS |
3397 | } |
3398 | /* If cannot merge, then use the new type and qualifiers, | |
3399 | and don't preserve the old rtl. */ | |
3400 | else | |
3401 | { | |
3402 | /* Clean out any memory we had of the old declaration. */ | |
3403 | tree oldstatic = value_member (olddecl, static_aggregates); | |
3404 | if (oldstatic) | |
3405 | TREE_VALUE (oldstatic) = error_mark_node; | |
3406 | ||
3407 | TREE_TYPE (olddecl) = TREE_TYPE (newdecl); | |
3408 | TREE_READONLY (olddecl) = TREE_READONLY (newdecl); | |
3409 | TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl); | |
3410 | TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl); | |
3411 | } | |
3412 | ||
3413 | /* Merge the storage class information. */ | |
45806a3f FS |
3414 | merge_weak (newdecl, olddecl); |
3415 | ||
e92cc029 | 3416 | DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl); |
794d4a61 | 3417 | DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl); |
a9aedbc2 MS |
3418 | TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl); |
3419 | TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl); | |
3420 | if (! DECL_EXTERNAL (olddecl)) | |
3421 | DECL_EXTERNAL (newdecl) = 0; | |
68642fb6 | 3422 | |
0b60dfe3 | 3423 | if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl)) |
8d08fdba | 3424 | { |
a9aedbc2 MS |
3425 | DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl); |
3426 | DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl); | |
77be6f82 | 3427 | DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl); |
68642fb6 | 3428 | DECL_TEMPLATE_INSTANTIATED (newdecl) |
2b0a63a3 | 3429 | |= DECL_TEMPLATE_INSTANTIATED (olddecl); |
0b60dfe3 BK |
3430 | /* Don't really know how much of the language-specific |
3431 | values we should copy from old to new. */ | |
3432 | DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl); | |
e2500fed GK |
3433 | DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 = |
3434 | DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2; | |
0b60dfe3 | 3435 | DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl); |
fbf1c34b | 3436 | DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl); |
3febd123 NS |
3437 | DECL_INITIALIZED_IN_CLASS_P (newdecl) |
3438 | |= DECL_INITIALIZED_IN_CLASS_P (olddecl); | |
0b60dfe3 | 3439 | olddecl_friend = DECL_FRIEND_P (olddecl); |
6a629cac MM |
3440 | |
3441 | /* Only functions have DECL_BEFRIENDING_CLASSES. */ | |
3442 | if (TREE_CODE (newdecl) == FUNCTION_DECL | |
3443 | || DECL_FUNCTION_TEMPLATE_P (newdecl)) | |
bb5e8a7f MM |
3444 | { |
3445 | DECL_BEFRIENDING_CLASSES (newdecl) | |
3446 | = chainon (DECL_BEFRIENDING_CLASSES (newdecl), | |
3447 | DECL_BEFRIENDING_CLASSES (olddecl)); | |
ca9e1382 KL |
3448 | /* DECL_THUNKS is only valid for virtual functions, |
3449 | otherwise it is a DECL_FRIEND_CONTEXT. */ | |
3450 | if (DECL_VIRTUAL_P (newdecl)) | |
3451 | DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl); | |
bb5e8a7f | 3452 | } |
8d08fdba MS |
3453 | } |
3454 | ||
8d08fdba MS |
3455 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
3456 | { | |
68642fb6 UD |
3457 | if (DECL_TEMPLATE_INSTANTIATION (olddecl) |
3458 | && !DECL_TEMPLATE_INSTANTIATION (newdecl)) | |
75650646 MM |
3459 | { |
3460 | /* If newdecl is not a specialization, then it is not a | |
3461 | template-related function at all. And that means that we | |
bf7c02dd | 3462 | should have exited above, returning 0. */ |
75650646 MM |
3463 | my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl), |
3464 | 0); | |
3465 | ||
68642fb6 | 3466 | if (TREE_USED (olddecl)) |
75650646 | 3467 | /* From [temp.expl.spec]: |
68642fb6 | 3468 | |
75650646 MM |
3469 | If a template, a member template or the member of a class |
3470 | template is explicitly specialized then that | |
3471 | specialization shall be declared before the first use of | |
3472 | that specialization that would cause an implicit | |
3473 | instantiation to take place, in every translation unit in | |
3474 | which such a use occurs. */ | |
33bd39a2 | 3475 | error ("explicit specialization of %D after first use", |
75650646 MM |
3476 | olddecl); |
3477 | ||
3478 | SET_DECL_TEMPLATE_SPECIALIZATION (olddecl); | |
d03d18e8 KL |
3479 | |
3480 | /* [temp.expl.spec/14] We don't inline explicit specialization | |
3481 | just because the primary template says so. */ | |
75650646 | 3482 | } |
d03d18e8 KL |
3483 | else |
3484 | { | |
3485 | if (DECL_PENDING_INLINE_INFO (newdecl) == 0) | |
3486 | DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl); | |
faae18ab | 3487 | |
d03d18e8 | 3488 | DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl); |
faae18ab | 3489 | |
d03d18e8 KL |
3490 | /* If either decl says `inline', this fn is inline, unless |
3491 | its definition was passed already. */ | |
3492 | if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE) | |
3493 | DECL_INLINE (olddecl) = 1; | |
3494 | DECL_INLINE (newdecl) = DECL_INLINE (olddecl); | |
3495 | ||
3496 | DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl) | |
3497 | = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)); | |
3498 | } | |
97055d5c | 3499 | |
5daf7c0a JM |
3500 | /* Preserve abstractness on cloned [cd]tors. */ |
3501 | DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl); | |
3502 | ||
700f8a87 MS |
3503 | if (! types_match) |
3504 | { | |
5d2ed28c | 3505 | SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl)); |
92643fea | 3506 | COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl); |
19e7881c | 3507 | SET_DECL_RTL (olddecl, DECL_RTL (newdecl)); |
5566b478 MS |
3508 | } |
3509 | if (! types_match || new_defines_function) | |
3510 | { | |
6f1b4c42 JM |
3511 | /* These need to be copied so that the names are available. |
3512 | Note that if the types do match, we'll preserve inline | |
3513 | info and other bits, but if not, we won't. */ | |
700f8a87 MS |
3514 | DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl); |
3515 | DECL_RESULT (olddecl) = DECL_RESULT (newdecl); | |
700f8a87 | 3516 | } |
8d08fdba MS |
3517 | if (new_defines_function) |
3518 | /* If defining a function declared with other language | |
3519 | linkage, use the previously declared language linkage. */ | |
5d2ed28c | 3520 | SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl)); |
6f1b4c42 | 3521 | else if (types_match) |
8d08fdba MS |
3522 | { |
3523 | /* If redeclaring a builtin function, and not a definition, | |
3524 | it stays built in. */ | |
3525 | if (DECL_BUILT_IN (olddecl)) | |
3526 | { | |
26db82d8 | 3527 | DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl); |
39211cd5 | 3528 | DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl); |
8d08fdba MS |
3529 | /* If we're keeping the built-in definition, keep the rtl, |
3530 | regardless of declaration matches. */ | |
19e7881c | 3531 | SET_DECL_RTL (newdecl, DECL_RTL (olddecl)); |
8d08fdba MS |
3532 | } |
3533 | else | |
b850de4f | 3534 | DECL_NUM_STMTS (newdecl) = DECL_NUM_STMTS (olddecl); |
8d08fdba MS |
3535 | |
3536 | DECL_RESULT (newdecl) = DECL_RESULT (olddecl); | |
8d08fdba MS |
3537 | /* Don't clear out the arguments if we're redefining a function. */ |
3538 | if (DECL_ARGUMENTS (olddecl)) | |
3539 | DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl); | |
3540 | } | |
3541 | } | |
0b1161fc MM |
3542 | else if (TREE_CODE (newdecl) == NAMESPACE_DECL) |
3543 | NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl); | |
a9aedbc2 | 3544 | |
8d08fdba MS |
3545 | /* Now preserve various other info from the definition. */ |
3546 | TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl); | |
3547 | TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl); | |
3548 | DECL_COMMON (newdecl) = DECL_COMMON (olddecl); | |
92643fea | 3549 | COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl); |
8d08fdba | 3550 | |
8d08fdba MS |
3551 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
3552 | { | |
3553 | int function_size; | |
8d08fdba MS |
3554 | |
3555 | function_size = sizeof (struct tree_decl); | |
3556 | ||
730e1556 KG |
3557 | memcpy ((char *) olddecl + sizeof (struct tree_common), |
3558 | (char *) newdecl + sizeof (struct tree_common), | |
3559 | function_size - sizeof (struct tree_common)); | |
8d08fdba | 3560 | |
75650646 MM |
3561 | if (DECL_TEMPLATE_INSTANTIATION (newdecl)) |
3562 | { | |
3563 | /* If newdecl is a template instantiation, it is possible that | |
3564 | the following sequence of events has occurred: | |
3565 | ||
3566 | o A friend function was declared in a class template. The | |
68642fb6 | 3567 | class template was instantiated. |
75650646 | 3568 | |
68642fb6 UD |
3569 | o The instantiation of the friend declaration was |
3570 | recorded on the instantiation list, and is newdecl. | |
75650646 MM |
3571 | |
3572 | o Later, however, instantiate_class_template called pushdecl | |
3573 | on the newdecl to perform name injection. But, pushdecl in | |
3574 | turn called duplicate_decls when it discovered that another | |
3575 | declaration of a global function with the same name already | |
68642fb6 | 3576 | existed. |
75650646 MM |
3577 | |
3578 | o Here, in duplicate_decls, we decided to clobber newdecl. | |
3579 | ||
3580 | If we're going to do that, we'd better make sure that | |
3581 | olddecl, and not newdecl, is on the list of | |
3582 | instantiations so that if we try to do the instantiation | |
3583 | again we won't get the clobbered declaration. */ | |
3584 | ||
68642fb6 UD |
3585 | tree tmpl = DECL_TI_TEMPLATE (newdecl); |
3586 | tree decls = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); | |
75650646 MM |
3587 | |
3588 | for (; decls; decls = TREE_CHAIN (decls)) | |
3589 | if (TREE_VALUE (decls) == newdecl) | |
3590 | TREE_VALUE (decls) = olddecl; | |
3591 | } | |
8d08fdba MS |
3592 | } |
3593 | else | |
3594 | { | |
730e1556 KG |
3595 | memcpy ((char *) olddecl + sizeof (struct tree_common), |
3596 | (char *) newdecl + sizeof (struct tree_common), | |
3597 | sizeof (struct tree_decl) - sizeof (struct tree_common) | |
2f9834e8 | 3598 | + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *)); |
8d08fdba MS |
3599 | } |
3600 | ||
3601 | DECL_UID (olddecl) = olddecl_uid; | |
3602 | if (olddecl_friend) | |
3603 | DECL_FRIEND_P (olddecl) = 1; | |
3604 | ||
d9525bec BK |
3605 | /* NEWDECL contains the merged attribute lists. |
3606 | Update OLDDECL to be the same. */ | |
91d231cb | 3607 | DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl); |
d9525bec | 3608 | |
16e31964 DS |
3609 | /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl |
3610 | so that encode_section_info has a chance to look at the new decl | |
3611 | flags and attributes. */ | |
3612 | if (DECL_RTL_SET_P (olddecl) | |
3613 | && (TREE_CODE (olddecl) == FUNCTION_DECL | |
3614 | || (TREE_CODE (olddecl) == VAR_DECL | |
3615 | && TREE_STATIC (olddecl)))) | |
3616 | make_decl_rtl (olddecl, NULL); | |
3617 | ||
8d08fdba MS |
3618 | return 1; |
3619 | } | |
3620 | ||
3621 | /* Record a decl-node X as belonging to the current lexical scope. | |
3622 | Check for errors (such as an incompatible declaration for the same | |
3623 | name already seen in the same scope). | |
3624 | ||
3625 | Returns either X or an old decl for the same name. | |
3626 | If an old decl is returned, it may have been smashed | |
3627 | to agree with what X says. */ | |
3628 | ||
3629 | tree | |
11f6b451 | 3630 | pushdecl (tree x) |
8d08fdba MS |
3631 | { |
3632 | register tree t; | |
b35d4555 MM |
3633 | register tree name; |
3634 | int need_new_binding; | |
3635 | ||
22ffcc6f | 3636 | timevar_push (TV_NAME_LOOKUP); |
b35d4555 MM |
3637 | /* We shouldn't be calling pushdecl when we're generating RTL for a |
3638 | function that we already did semantic analysis on previously. */ | |
01d939e8 | 3639 | my_friendly_assert (!cfun || doing_semantic_analysis_p (), |
b35d4555 MM |
3640 | 19990913); |
3641 | ||
b35d4555 | 3642 | need_new_binding = 1; |
8d08fdba | 3643 | |
50714e79 MM |
3644 | if (DECL_TEMPLATE_PARM_P (x)) |
3645 | /* Template parameters have no context; they are not X::T even | |
3646 | when declared within a class or namespace. */ | |
3647 | ; | |
3648 | else | |
3649 | { | |
3650 | if (current_function_decl && x != current_function_decl | |
3651 | /* A local declaration for a function doesn't constitute | |
3652 | nesting. */ | |
f39ee884 MM |
3653 | && !(TREE_CODE (x) == FUNCTION_DECL && !DECL_INITIAL (x)) |
3654 | /* A local declaration for an `extern' variable is in the | |
94dfccd1 | 3655 | scope of the current namespace, not the current |
f39ee884 MM |
3656 | function. */ |
3657 | && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x)) | |
50714e79 MM |
3658 | && !DECL_CONTEXT (x)) |
3659 | DECL_CONTEXT (x) = current_function_decl; | |
cd9f6678 MM |
3660 | |
3661 | /* If this is the declaration for a namespace-scope function, | |
3662 | but the declaration itself is in a local scope, mark the | |
3663 | declaration. */ | |
68642fb6 | 3664 | if (TREE_CODE (x) == FUNCTION_DECL |
cd9f6678 MM |
3665 | && DECL_NAMESPACE_SCOPE_P (x) |
3666 | && current_function_decl | |
3667 | && x != current_function_decl) | |
3668 | DECL_LOCAL_FUNCTION_P (x) = 1; | |
50714e79 | 3669 | } |
8d08fdba | 3670 | |
fe8fadc1 | 3671 | name = DECL_NAME (x); |
8d08fdba MS |
3672 | if (name) |
3673 | { | |
94dfccd1 JM |
3674 | int different_binding_level = 0; |
3675 | ||
386b8a85 JM |
3676 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) |
3677 | name = TREE_OPERAND (name, 0); | |
68642fb6 | 3678 | |
94dfccd1 JM |
3679 | /* In case this decl was explicitly namespace-qualified, look it |
3680 | up in its namespace context. */ | |
3681 | if (TREE_CODE (x) == VAR_DECL && DECL_NAMESPACE_SCOPE_P (x) | |
3682 | && namespace_bindings_p ()) | |
2c73f9f5 ML |
3683 | t = namespace_binding (name, DECL_CONTEXT (x)); |
3684 | else | |
3685 | t = lookup_name_current_level (name); | |
fe8fadc1 | 3686 | |
94dfccd1 JM |
3687 | /* [basic.link] If there is a visible declaration of an entity |
3688 | with linkage having the same name and type, ignoring entities | |
3689 | declared outside the innermost enclosing namespace scope, the | |
3690 | block scope declaration declares that same entity and | |
3691 | receives the linkage of the previous declaration. */ | |
3692 | if (! t && current_function_decl && x != current_function_decl | |
3693 | && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL) | |
3694 | && DECL_EXTERNAL (x)) | |
3695 | { | |
3696 | /* Look in block scope. */ | |
3697 | t = IDENTIFIER_VALUE (name); | |
3698 | /* Or in the innermost namespace. */ | |
3699 | if (! t) | |
3700 | t = namespace_binding (name, DECL_CONTEXT (x)); | |
fab09a24 JM |
3701 | /* Does it have linkage? Note that if this isn't a DECL, it's an |
3702 | OVERLOAD, which is OK. */ | |
3703 | if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t))) | |
94dfccd1 JM |
3704 | t = NULL_TREE; |
3705 | if (t) | |
3706 | different_binding_level = 1; | |
3707 | } | |
3708 | ||
fe8fadc1 MM |
3709 | /* If we are declaring a function, and the result of name-lookup |
3710 | was an OVERLOAD, look for an overloaded instance that is | |
3711 | actually the same as the function we are declaring. (If | |
3712 | there is one, we have to merge our declaration with the | |
3713 | previous declaration.) */ | |
655dc6ee | 3714 | if (t && TREE_CODE (t) == OVERLOAD) |
fe8fadc1 MM |
3715 | { |
3716 | tree match; | |
3717 | ||
655dc6ee JM |
3718 | if (TREE_CODE (x) == FUNCTION_DECL) |
3719 | for (match = t; match; match = OVL_NEXT (match)) | |
3720 | { | |
92643fea | 3721 | if (decls_match (OVL_CURRENT (match), x)) |
655dc6ee JM |
3722 | break; |
3723 | } | |
3724 | else | |
3725 | /* Just choose one. */ | |
3726 | match = t; | |
fe8fadc1 MM |
3727 | |
3728 | if (match) | |
3729 | t = OVL_CURRENT (match); | |
3730 | else | |
3731 | t = NULL_TREE; | |
3732 | } | |
3733 | ||
8d08fdba MS |
3734 | if (t == error_mark_node) |
3735 | { | |
3736 | /* error_mark_node is 0 for a while during initialization! */ | |
3737 | t = NULL_TREE; | |
8251199e | 3738 | cp_error_at ("`%#D' used prior to declaration", x); |
8d08fdba | 3739 | } |
51c184be | 3740 | else if (t != NULL_TREE) |
8d08fdba | 3741 | { |
94dfccd1 JM |
3742 | if (different_binding_level) |
3743 | { | |
3744 | if (decls_match (x, t)) | |
3745 | /* The standard only says that the local extern | |
3746 | inherits linkage from the previous decl; in | |
3747 | particular, default args are not shared. It would | |
3748 | be nice to propagate inlining info, though. FIXME. */ | |
3749 | TREE_PUBLIC (x) = TREE_PUBLIC (t); | |
3750 | } | |
3751 | else if (TREE_CODE (t) == PARM_DECL) | |
8d08fdba MS |
3752 | { |
3753 | if (DECL_CONTEXT (t) == NULL_TREE) | |
400500c4 RK |
3754 | /* This is probaby caused by too many errors, but calling |
3755 | abort will say that if errors have occurred. */ | |
3756 | abort (); | |
be99da77 | 3757 | |
e92cc029 | 3758 | /* Check for duplicate params. */ |
be99da77 | 3759 | if (duplicate_decls (x, t)) |
22ffcc6f | 3760 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); |
8d08fdba | 3761 | } |
eb68cb58 | 3762 | else if ((DECL_EXTERN_C_FUNCTION_P (x) |
5566b478 MS |
3763 | || DECL_FUNCTION_TEMPLATE_P (x)) |
3764 | && is_overloaded_fn (t)) | |
a1c65f9f | 3765 | /* Don't do anything just yet. */; |
e1cd6e56 MS |
3766 | else if (t == wchar_decl_node) |
3767 | { | |
3768 | if (pedantic && ! DECL_IN_SYSTEM_HEADER (x)) | |
33bd39a2 | 3769 | pedwarn ("redeclaration of `wchar_t' as `%T'", |
2b0cbc5d | 3770 | TREE_TYPE (x)); |
e1cd6e56 MS |
3771 | |
3772 | /* Throw away the redeclaration. */ | |
22ffcc6f | 3773 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); |
e1cd6e56 | 3774 | } |
8926095f | 3775 | else if (TREE_CODE (t) != TREE_CODE (x)) |
8d08fdba | 3776 | { |
9ed182dc | 3777 | if (duplicate_decls (x, t)) |
22ffcc6f | 3778 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); |
8d08fdba MS |
3779 | } |
3780 | else if (duplicate_decls (x, t)) | |
51c184be | 3781 | { |
7177d104 MS |
3782 | if (TREE_CODE (t) == TYPE_DECL) |
3783 | SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t)); | |
f30432d7 MS |
3784 | else if (TREE_CODE (t) == FUNCTION_DECL) |
3785 | check_default_args (t); | |
7177d104 | 3786 | |
22ffcc6f | 3787 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); |
51c184be | 3788 | } |
35680744 MM |
3789 | else if (DECL_MAIN_P (x)) |
3790 | { | |
3791 | /* A redeclaration of main, but not a duplicate of the | |
68642fb6 | 3792 | previous one. |
35680744 MM |
3793 | |
3794 | [basic.start.main] | |
3795 | ||
3796 | This function shall not be overloaded. */ | |
8251199e | 3797 | cp_error_at ("invalid redeclaration of `%D'", t); |
33bd39a2 | 3798 | error ("as `%D'", x); |
35680744 MM |
3799 | /* We don't try to push this declaration since that |
3800 | causes a crash. */ | |
22ffcc6f | 3801 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x); |
35680744 | 3802 | } |
8d08fdba | 3803 | } |
8926095f | 3804 | |
f3400fe2 JM |
3805 | check_template_shadow (x); |
3806 | ||
fcfcdfc8 JM |
3807 | /* If this is a function conjured up by the backend, massage it |
3808 | so it looks friendly. */ | |
eb68cb58 | 3809 | if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x)) |
fcfcdfc8 JM |
3810 | { |
3811 | retrofit_lang_decl (x); | |
5d2ed28c | 3812 | SET_DECL_LANGUAGE (x, lang_c); |
fcfcdfc8 JM |
3813 | } |
3814 | ||
eb68cb58 | 3815 | if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x)) |
8926095f | 3816 | { |
7bdbfa05 | 3817 | t = push_overloaded_decl (x, PUSH_LOCAL); |
5fdaba89 | 3818 | if (t != x) |
22ffcc6f | 3819 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); |
f181d4ae MM |
3820 | if (!namespace_bindings_p ()) |
3821 | /* We do not need to create a binding for this name; | |
3822 | push_overloaded_decl will have already done so if | |
3823 | necessary. */ | |
3824 | need_new_binding = 0; | |
8926095f | 3825 | } |
6eb3bb27 | 3826 | else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x)) |
cfe507be MM |
3827 | { |
3828 | t = push_overloaded_decl (x, PUSH_GLOBAL); | |
3829 | if (t == x) | |
3830 | add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t))); | |
22ffcc6f | 3831 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); |
cfe507be | 3832 | } |
8d08fdba | 3833 | |
a1774733 BK |
3834 | /* If declaring a type as a typedef, copy the type (unless we're |
3835 | at line 0), and install this TYPE_DECL as the new type's typedef | |
a1c65f9f | 3836 | name. See the extensive comment in ../c-decl.c (pushdecl). */ |
8d08fdba MS |
3837 | if (TREE_CODE (x) == TYPE_DECL) |
3838 | { | |
3839 | tree type = TREE_TYPE (x); | |
655dc6ee | 3840 | if (DECL_SOURCE_LINE (x) == 0) |
a1774733 BK |
3841 | { |
3842 | if (TYPE_NAME (type) == 0) | |
3843 | TYPE_NAME (type) = x; | |
3844 | } | |
1c80fb65 MM |
3845 | else if (type != error_mark_node && TYPE_NAME (type) != x |
3846 | /* We don't want to copy the type when all we're | |
3847 | doing is making a TYPE_DECL for the purposes of | |
3848 | inlining. */ | |
68642fb6 | 3849 | && (!TYPE_NAME (type) |
1c80fb65 | 3850 | || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x))) |
a1774733 BK |
3851 | { |
3852 | DECL_ORIGINAL_TYPE (x) = type; | |
3853 | type = build_type_copy (type); | |
3854 | TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x)); | |
3855 | TYPE_NAME (type) = x; | |
3856 | TREE_TYPE (x) = type; | |
3857 | } | |
8d08fdba | 3858 | |
8d08fdba MS |
3859 | if (type != error_mark_node |
3860 | && TYPE_NAME (type) | |
3861 | && TYPE_IDENTIFIER (type)) | |
68642fb6 | 3862 | set_identifier_type_value_with_scope (DECL_NAME (x), type, |
f181d4ae MM |
3863 | current_binding_level); |
3864 | ||
8d08fdba MS |
3865 | } |
3866 | ||
3867 | /* Multiple external decls of the same identifier ought to match. | |
3868 | ||
3869 | We get warnings about inline functions where they are defined. | |
39211cd5 | 3870 | We get warnings about other functions from push_overloaded_decl. |
68642fb6 | 3871 | |
8d08fdba | 3872 | Avoid duplicate warnings where they are used. */ |
39211cd5 | 3873 | if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL) |
8d08fdba MS |
3874 | { |
3875 | tree decl; | |
3876 | ||
f49fad00 JM |
3877 | decl = IDENTIFIER_NAMESPACE_VALUE (name); |
3878 | if (decl && TREE_CODE (decl) == OVERLOAD) | |
3879 | decl = OVL_FUNCTION (decl); | |
8d08fdba | 3880 | |
f49fad00 JM |
3881 | if (decl && decl != error_mark_node |
3882 | && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl)) | |
8d08fdba MS |
3883 | /* If different sort of thing, we already gave an error. */ |
3884 | && TREE_CODE (decl) == TREE_CODE (x) | |
3bfdc719 | 3885 | && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl))) |
8d08fdba | 3886 | { |
33bd39a2 | 3887 | pedwarn ("type mismatch with previous external decl", x); |
8251199e | 3888 | cp_pedwarn_at ("previous external decl of `%#D'", decl); |
8d08fdba MS |
3889 | } |
3890 | } | |
3891 | ||
8d08fdba MS |
3892 | /* This name is new in its binding level. |
3893 | Install the new declaration and return it. */ | |
2c73f9f5 | 3894 | if (namespace_bindings_p ()) |
8d08fdba MS |
3895 | { |
3896 | /* Install a global value. */ | |
3897 | ||
8d08fdba MS |
3898 | /* If the first global decl has external linkage, |
3899 | warn if we later see static one. */ | |
31928556 | 3900 | if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x)) |
8d08fdba | 3901 | TREE_PUBLIC (name) = 1; |
5362b086 | 3902 | |
92643fea MM |
3903 | /* Bind the name for the entity. */ |
3904 | if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x) | |
3905 | && t != NULL_TREE) | |
3906 | && (TREE_CODE (x) == TYPE_DECL | |
3907 | || TREE_CODE (x) == VAR_DECL | |
0ca7178c | 3908 | || TREE_CODE (x) == ALIAS_DECL |
92643fea MM |
3909 | || TREE_CODE (x) == NAMESPACE_DECL |
3910 | || TREE_CODE (x) == CONST_DECL | |
3911 | || TREE_CODE (x) == TEMPLATE_DECL)) | |
3912 | SET_IDENTIFIER_NAMESPACE_VALUE (name, x); | |
8d08fdba MS |
3913 | |
3914 | /* Don't forget if the function was used via an implicit decl. */ | |
3915 | if (IDENTIFIER_IMPLICIT_DECL (name) | |
3916 | && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name))) | |
3917 | TREE_USED (x) = 1; | |
3918 | ||
3919 | /* Don't forget if its address was taken in that way. */ | |
3920 | if (IDENTIFIER_IMPLICIT_DECL (name) | |
3921 | && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name))) | |
3922 | TREE_ADDRESSABLE (x) = 1; | |
3923 | ||
3924 | /* Warn about mismatches against previous implicit decl. */ | |
3925 | if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE | |
3926 | /* If this real decl matches the implicit, don't complain. */ | |
3927 | && ! (TREE_CODE (x) == FUNCTION_DECL | |
3928 | && TREE_TYPE (TREE_TYPE (x)) == integer_type_node)) | |
33bd39a2 | 3929 | warning |
8251199e | 3930 | ("`%D' was previously implicitly declared to return `int'", x); |
8d08fdba MS |
3931 | |
3932 | /* If new decl is `static' and an `extern' was seen previously, | |
3933 | warn about it. */ | |
a0a33927 MS |
3934 | if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t)) |
3935 | warn_extern_redeclared_static (x, t); | |
8d08fdba MS |
3936 | } |
3937 | else | |
3938 | { | |
3939 | /* Here to install a non-global value. */ | |
f181d4ae | 3940 | tree oldlocal = IDENTIFIER_VALUE (name); |
31928556 | 3941 | tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name); |
8d08fdba | 3942 | |
f181d4ae MM |
3943 | if (need_new_binding) |
3944 | { | |
0034cf72 | 3945 | push_local_binding (name, x, 0); |
f181d4ae MM |
3946 | /* Because push_local_binding will hook X on to the |
3947 | current_binding_level's name list, we don't want to | |
3948 | do that again below. */ | |
3949 | need_new_binding = 0; | |
3950 | } | |
8d08fdba MS |
3951 | |
3952 | /* If this is a TYPE_DECL, push it into the type value slot. */ | |
3953 | if (TREE_CODE (x) == TYPE_DECL) | |
68642fb6 | 3954 | set_identifier_type_value_with_scope (name, TREE_TYPE (x), |
f181d4ae | 3955 | current_binding_level); |
8d08fdba | 3956 | |
a9aedbc2 MS |
3957 | /* Clear out any TYPE_DECL shadowed by a namespace so that |
3958 | we won't think this is a type. The C struct hack doesn't | |
3959 | go through namespaces. */ | |
3960 | if (TREE_CODE (x) == NAMESPACE_DECL) | |
68642fb6 | 3961 | set_identifier_type_value_with_scope (name, NULL_TREE, |
f181d4ae | 3962 | current_binding_level); |
a9aedbc2 | 3963 | |
e905ac8a MS |
3964 | if (oldlocal) |
3965 | { | |
3966 | tree d = oldlocal; | |
17aec3eb | 3967 | |
74dc0d8c MS |
3968 | while (oldlocal |
3969 | && TREE_CODE (oldlocal) == VAR_DECL | |
3970 | && DECL_DEAD_FOR_LOCAL (oldlocal)) | |
17aec3eb RK |
3971 | oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal); |
3972 | ||
e905ac8a MS |
3973 | if (oldlocal == NULL_TREE) |
3974 | oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d)); | |
3975 | } | |
3976 | ||
8d08fdba MS |
3977 | /* If this is an extern function declaration, see if we |
3978 | have a global definition or declaration for the function. */ | |
3979 | if (oldlocal == NULL_TREE | |
faae18ab | 3980 | && DECL_EXTERNAL (x) |
31928556 | 3981 | && oldglobal != NULL_TREE |
8d08fdba | 3982 | && TREE_CODE (x) == FUNCTION_DECL |
31928556 | 3983 | && TREE_CODE (oldglobal) == FUNCTION_DECL) |
8d08fdba MS |
3984 | { |
3985 | /* We have one. Their types must agree. */ | |
31928556 | 3986 | if (decls_match (x, oldglobal)) |
6060a796 MS |
3987 | /* OK */; |
3988 | else | |
8d08fdba | 3989 | { |
33bd39a2 | 3990 | warning ("extern declaration of `%#D' doesn't match", x); |
8251199e | 3991 | cp_warning_at ("global declaration `%#D'", oldglobal); |
8d08fdba | 3992 | } |
8d08fdba MS |
3993 | } |
3994 | /* If we have a local external declaration, | |
3995 | and no file-scope declaration has yet been seen, | |
3996 | then if we later have a file-scope decl it must not be static. */ | |
3997 | if (oldlocal == NULL_TREE | |
31928556 | 3998 | && oldglobal == NULL_TREE |
8d08fdba MS |
3999 | && DECL_EXTERNAL (x) |
4000 | && TREE_PUBLIC (x)) | |
f181d4ae | 4001 | TREE_PUBLIC (name) = 1; |
8d08fdba | 4002 | |
8d08fdba | 4003 | /* Warn if shadowing an argument at the top level of the body. */ |
e905ac8a MS |
4004 | if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x) |
4005 | /* Inline decls shadow nothing. */ | |
4006 | && !DECL_FROM_INLINE (x) | |
07681121 MM |
4007 | && TREE_CODE (oldlocal) == PARM_DECL |
4008 | /* Don't check the `this' parameter. */ | |
4009 | && !DECL_ARTIFICIAL (oldlocal)) | |
8d08fdba | 4010 | { |
8ac9ea61 | 4011 | bool err = false; |
8d08fdba | 4012 | |
8ac9ea61 NB |
4013 | /* Don't complain if it's from an enclosing function. */ |
4014 | if (DECL_CONTEXT (oldlocal) == current_function_decl | |
4015 | && TREE_CODE (x) != PARM_DECL) | |
4016 | { | |
4017 | /* Go to where the parms should be and see if we find | |
4018 | them there. */ | |
e2500fed | 4019 | struct cp_binding_level *b = current_binding_level->level_chain; |
8ac9ea61 | 4020 | |
9f175208 JM |
4021 | /* Skip the ctor/dtor cleanup level. */ |
4022 | b = b->level_chain; | |
4023 | ||
8ac9ea61 NB |
4024 | /* ARM $8.3 */ |
4025 | if (b->parm_flag == 1) | |
4026 | { | |
33bd39a2 | 4027 | error ("declaration of `%#D' shadows a parameter", |
8ac9ea61 NB |
4028 | name); |
4029 | err = true; | |
4030 | } | |
4031 | } | |
8d08fdba | 4032 | |
8ac9ea61 | 4033 | if (warn_shadow && !err) |
339a28b9 | 4034 | shadow_warning (SW_PARAM, |
d9b2742a | 4035 | IDENTIFIER_POINTER (name), oldlocal); |
8d08fdba | 4036 | } |
e905ac8a | 4037 | |
8d08fdba | 4038 | /* Maybe warn if shadowing something else. */ |
8ac9ea61 | 4039 | else if (warn_shadow && !DECL_EXTERNAL (x) |
e905ac8a MS |
4040 | /* No shadow warnings for internally generated vars. */ |
4041 | && ! DECL_ARTIFICIAL (x) | |
4042 | /* No shadow warnings for vars made for inlining. */ | |
4043 | && ! DECL_FROM_INLINE (x)) | |
8d08fdba | 4044 | { |
8ac9ea61 | 4045 | if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE |
4ac14744 | 4046 | && current_class_ptr |
8d08fdba | 4047 | && !TREE_STATIC (name)) |
33bd39a2 | 4048 | warning ("declaration of `%s' shadows a member of `this'", |
8ac9ea61 NB |
4049 | IDENTIFIER_POINTER (name)); |
4050 | else if (oldlocal != NULL_TREE | |
4051 | && TREE_CODE (oldlocal) == VAR_DECL) | |
339a28b9 | 4052 | shadow_warning (SW_LOCAL, |
d9b2742a | 4053 | IDENTIFIER_POINTER (name), oldlocal); |
8ac9ea61 NB |
4054 | else if (oldglobal != NULL_TREE |
4055 | && TREE_CODE (oldglobal) == VAR_DECL) | |
30394414 | 4056 | /* XXX shadow warnings in outer-more namespaces */ |
339a28b9 | 4057 | shadow_warning (SW_GLOBAL, |
d9b2742a | 4058 | IDENTIFIER_POINTER (name), oldglobal); |
8d08fdba | 4059 | } |
e1cd6e56 | 4060 | } |
8d08fdba | 4061 | |
e1cd6e56 | 4062 | if (TREE_CODE (x) == FUNCTION_DECL) |
f30432d7 | 4063 | check_default_args (x); |
8145f082 | 4064 | |
0154eaa8 MM |
4065 | if (TREE_CODE (x) == VAR_DECL) |
4066 | maybe_register_incomplete_var (x); | |
8d08fdba MS |
4067 | } |
4068 | ||
f181d4ae | 4069 | if (need_new_binding) |
68642fb6 | 4070 | add_decl_to_level (x, |
efee38a9 MM |
4071 | DECL_NAMESPACE_SCOPE_P (x) |
4072 | ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x)) | |
4073 | : current_binding_level); | |
8d08fdba | 4074 | |
22ffcc6f | 4075 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x); |
8d08fdba MS |
4076 | } |
4077 | ||
5566b478 MS |
4078 | /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the |
4079 | caller to set DECL_CONTEXT properly. */ | |
8d08fdba MS |
4080 | |
4081 | static tree | |
11f6b451 | 4082 | pushdecl_with_scope (tree x, struct cp_binding_level* level) |
8d08fdba | 4083 | { |
e2500fed | 4084 | register struct cp_binding_level *b; |
5566b478 | 4085 | tree function_decl = current_function_decl; |
8d08fdba | 4086 | |
22ffcc6f | 4087 | timevar_push (TV_NAME_LOOKUP); |
5566b478 | 4088 | current_function_decl = NULL_TREE; |
8d019cef JM |
4089 | if (level->parm_flag == 2) |
4090 | { | |
4091 | b = class_binding_level; | |
4092 | class_binding_level = level; | |
4093 | pushdecl_class_level (x); | |
4094 | class_binding_level = b; | |
4095 | } | |
4096 | else | |
4097 | { | |
4098 | b = current_binding_level; | |
ff955512 | 4099 | current_binding_level = level; |
8d019cef | 4100 | x = pushdecl (x); |
ff955512 | 4101 | current_binding_level = b; |
8d019cef | 4102 | } |
5566b478 | 4103 | current_function_decl = function_decl; |
22ffcc6f | 4104 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x); |
8d08fdba MS |
4105 | } |
4106 | ||
2c73f9f5 | 4107 | /* Like pushdecl, only it places X in the current namespace, |
8d08fdba | 4108 | if appropriate. */ |
e92cc029 | 4109 | |
8d08fdba | 4110 | tree |
11f6b451 | 4111 | pushdecl_namespace_level (tree x) |
8d08fdba | 4112 | { |
e2500fed | 4113 | register struct cp_binding_level *b = current_binding_level; |
2c73f9f5 ML |
4114 | register tree t; |
4115 | ||
22ffcc6f | 4116 | timevar_push (TV_NAME_LOOKUP); |
2c73f9f5 | 4117 | t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace)); |
8d08fdba MS |
4118 | |
4119 | /* Now, the type_shadowed stack may screw us. Munge it so it does | |
4120 | what we want. */ | |
4121 | if (TREE_CODE (x) == TYPE_DECL) | |
4122 | { | |
4123 | tree name = DECL_NAME (x); | |
4124 | tree newval; | |
4125 | tree *ptr = (tree *)0; | |
5f52c0e0 | 4126 | for (; !global_scope_p (b); b = b->level_chain) |
8d08fdba MS |
4127 | { |
4128 | tree shadowed = b->type_shadowed; | |
4129 | for (; shadowed; shadowed = TREE_CHAIN (shadowed)) | |
4130 | if (TREE_PURPOSE (shadowed) == name) | |
4131 | { | |
4132 | ptr = &TREE_VALUE (shadowed); | |
4133 | /* Can't break out of the loop here because sometimes | |
4134 | a binding level will have duplicate bindings for | |
4135 | PT names. It's gross, but I haven't time to fix it. */ | |
4136 | } | |
4137 | } | |
4138 | newval = TREE_TYPE (x); | |
4139 | if (ptr == (tree *)0) | |
4140 | { | |
4141 | /* @@ This shouldn't be needed. My test case "zstring.cc" trips | |
4142 | up here if this is changed to an assertion. --KR */ | |
4143 | SET_IDENTIFIER_TYPE_VALUE (name, newval); | |
4144 | } | |
4145 | else | |
4146 | { | |
8d08fdba MS |
4147 | *ptr = newval; |
4148 | } | |
4149 | } | |
22ffcc6f | 4150 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); |
8d08fdba MS |
4151 | } |
4152 | ||
170b020f MM |
4153 | /* Like pushdecl, only it places X in the global scope if appropriate. |
4154 | Calls cp_finish_decl to register the variable, initializing it with | |
4155 | *INIT, if INIT is non-NULL. */ | |
2c73f9f5 | 4156 | |
170b020f MM |
4157 | static tree |
4158 | pushdecl_top_level_1 (tree x, tree *init) | |
2c73f9f5 | 4159 | { |
22ffcc6f | 4160 | timevar_push (TV_NAME_LOOKUP); |
b35d4555 | 4161 | push_to_top_level (); |
2c73f9f5 | 4162 | x = pushdecl_namespace_level (x); |
170b020f MM |
4163 | if (init) |
4164 | cp_finish_decl (x, *init, NULL_TREE, 0); | |
b35d4555 | 4165 | pop_from_top_level (); |
22ffcc6f | 4166 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x); |
2c73f9f5 ML |
4167 | } |
4168 | ||
170b020f MM |
4169 | /* Like pushdecl, only it places X in the global scope if appropriate. */ |
4170 | ||
4171 | tree | |
4172 | pushdecl_top_level (tree x) | |
4173 | { | |
4174 | return pushdecl_top_level_1 (x, NULL); | |
4175 | } | |
4176 | ||
4177 | /* Like pushdecl, only it places X in the global scope if | |
4178 | appropriate. Calls cp_finish_decl to register the variable, | |
4179 | initializing it with INIT. */ | |
4180 | ||
4181 | tree | |
4182 | pushdecl_top_level_and_finish (tree x, tree init) | |
4183 | { | |
4184 | return pushdecl_top_level_1 (x, &init); | |
4185 | } | |
4186 | ||
8d08fdba | 4187 | /* Make the declaration of X appear in CLASS scope. */ |
e92cc029 | 4188 | |
f139561c | 4189 | bool |
11f6b451 | 4190 | pushdecl_class_level (tree x) |
8d08fdba | 4191 | { |
22eadedb | 4192 | tree name; |
f139561c | 4193 | bool is_valid = true; |
8f032717 | 4194 | |
22ffcc6f | 4195 | timevar_push (TV_NAME_LOOKUP); |
22eadedb | 4196 | /* Get the name of X. */ |
8f032717 | 4197 | if (TREE_CODE (x) == OVERLOAD) |
22eadedb MM |
4198 | name = DECL_NAME (get_first_fn (x)); |
4199 | else | |
4200 | name = DECL_NAME (x); | |
8d08fdba MS |
4201 | |
4202 | if (name) | |
4203 | { | |
f139561c | 4204 | is_valid = push_class_level_binding (name, x); |
8d08fdba | 4205 | if (TREE_CODE (x) == TYPE_DECL) |
f181d4ae | 4206 | set_identifier_type_value (name, TREE_TYPE (x)); |
8d08fdba | 4207 | } |
6bdb8141 | 4208 | else if (ANON_AGGR_TYPE_P (TREE_TYPE (x))) |
8f032717 | 4209 | { |
22eadedb MM |
4210 | /* If X is an anonymous aggregate, all of its members are |
4211 | treated as if they were members of the class containing the | |
4212 | aggregate, for naming purposes. */ | |
8f032717 MM |
4213 | tree f; |
4214 | ||
22eadedb | 4215 | for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f)) |
f139561c MM |
4216 | { |
4217 | push_srcloc (DECL_SOURCE_FILE (f), DECL_SOURCE_LINE (f)); | |
4218 | if (!pushdecl_class_level (f)) | |
4219 | is_valid = false; | |
4220 | pop_srcloc (); | |
4221 | } | |
8f032717 | 4222 | } |
22ffcc6f | 4223 | timevar_pop (TV_NAME_LOOKUP); |
f139561c MM |
4224 | |
4225 | return is_valid; | |
8d08fdba MS |
4226 | } |
4227 | ||
9188c363 MM |
4228 | /* Enter DECL into the symbol table, if that's appropriate. Returns |
4229 | DECL, or a modified version thereof. */ | |
4230 | ||
4231 | tree | |
11f6b451 | 4232 | maybe_push_decl (tree decl) |
9188c363 MM |
4233 | { |
4234 | tree type = TREE_TYPE (decl); | |
4235 | ||
4236 | /* Add this decl to the current binding level, but not if it comes | |
4237 | from another scope, e.g. a static member variable. TEM may equal | |
4238 | DECL or it may be a previous decl of the same name. */ | |
07c88314 MM |
4239 | if (decl == error_mark_node |
4240 | || (TREE_CODE (decl) != PARM_DECL | |
4241 | && DECL_CONTEXT (decl) != NULL_TREE | |
4242 | /* Definitions of namespace members outside their namespace are | |
a1c65f9f | 4243 | possible. */ |
07c88314 | 4244 | && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL) |
9188c363 MM |
4245 | || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ()) |
4246 | || TREE_CODE (type) == UNKNOWN_TYPE | |
558475f0 | 4247 | /* The declaration of a template specialization does not affect |
9188c363 MM |
4248 | the functions available for overload resolution, so we do not |
4249 | call pushdecl. */ | |
4250 | || (TREE_CODE (decl) == FUNCTION_DECL | |
4251 | && DECL_TEMPLATE_SPECIALIZATION (decl))) | |
4252 | return decl; | |
4253 | else | |
4254 | return pushdecl (decl); | |
4255 | } | |
4256 | ||
f139561c MM |
4257 | /* Make the declaration(s) of X appear in CLASS scope under the name |
4258 | NAME. Returns true if the binding is valid. */ | |
e92cc029 | 4259 | |
f139561c | 4260 | bool |
11f6b451 | 4261 | push_class_level_binding (tree name, tree x) |
8d08fdba | 4262 | { |
af6fd53f | 4263 | cxx_binding *binding; |
f139561c | 4264 | |
22ffcc6f | 4265 | timevar_push (TV_NAME_LOOKUP); |
68642fb6 | 4266 | /* The class_binding_level will be NULL if x is a template |
98c1c668 JM |
4267 | parameter name in a member template. */ |
4268 | if (!class_binding_level) | |
f139561c | 4269 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true); |
98c1c668 | 4270 | |
908c4e83 MM |
4271 | /* Make sure that this new member does not have the same name |
4272 | as a template parameter. */ | |
4273 | if (TYPE_BEING_DEFINED (current_class_type)) | |
4274 | check_template_shadow (x); | |
4275 | ||
f181d4ae MM |
4276 | /* If this declaration shadows a declaration from an enclosing |
4277 | class, then we will need to restore IDENTIFIER_CLASS_VALUE when | |
4278 | we leave this class. Record the shadowed declaration here. */ | |
8f032717 | 4279 | binding = IDENTIFIER_BINDING (name); |
68642fb6 | 4280 | if (binding |
8f032717 MM |
4281 | && ((TREE_CODE (x) == OVERLOAD |
4282 | && BINDING_VALUE (binding) | |
4283 | && is_overloaded_fn (BINDING_VALUE (binding))) | |
4284 | || INHERITED_VALUE_BINDING_P (binding))) | |
4285 | { | |
4286 | tree shadow; | |
4287 | tree old_decl; | |
4288 | ||
4289 | /* If the old binding was from a base class, and was for a tag | |
4290 | name, slide it over to make room for the new binding. The | |
4291 | old binding is still visible if explicitly qualified with a | |
4292 | class-key. */ | |
4293 | if (INHERITED_VALUE_BINDING_P (binding) | |
4294 | && BINDING_VALUE (binding) | |
4295 | && TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL | |
4296 | && DECL_ARTIFICIAL (BINDING_VALUE (binding)) | |
4297 | && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x))) | |
4298 | { | |
4299 | old_decl = BINDING_TYPE (binding); | |
4300 | BINDING_TYPE (binding) = BINDING_VALUE (binding); | |
4301 | BINDING_VALUE (binding) = NULL_TREE; | |
4302 | INHERITED_VALUE_BINDING_P (binding) = 0; | |
4303 | } | |
4304 | else | |
4305 | old_decl = BINDING_VALUE (binding); | |
4306 | ||
76191fdd NS |
4307 | /* Find the previous binding of name on the class-shadowed |
4308 | list, and update it. */ | |
8f032717 MM |
4309 | for (shadow = class_binding_level->class_shadowed; |
4310 | shadow; | |
4311 | shadow = TREE_CHAIN (shadow)) | |
4312 | if (TREE_PURPOSE (shadow) == name | |
4313 | && TREE_TYPE (shadow) == old_decl) | |
4314 | { | |
4315 | BINDING_VALUE (binding) = x; | |
4316 | INHERITED_VALUE_BINDING_P (binding) = 0; | |
4317 | TREE_TYPE (shadow) = x; | |
76191fdd | 4318 | IDENTIFIER_CLASS_VALUE (name) = x; |
f139561c | 4319 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true); |
8f032717 MM |
4320 | } |
4321 | } | |
f181d4ae | 4322 | |
8f032717 | 4323 | /* If we didn't replace an existing binding, put the binding on the |
76191fdd | 4324 | stack of bindings for the identifier, and update the shadowed list. */ |
8f032717 MM |
4325 | if (push_class_binding (name, x)) |
4326 | { | |
8f032717 | 4327 | class_binding_level->class_shadowed |
76191fdd | 4328 | = tree_cons (name, NULL, |
8f032717 | 4329 | class_binding_level->class_shadowed); |
8f032717 MM |
4330 | /* Record the value we are binding NAME to so that we can know |
4331 | what to pop later. */ | |
4332 | TREE_TYPE (class_binding_level->class_shadowed) = x; | |
f139561c | 4333 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true); |
8f032717 | 4334 | } |
f139561c MM |
4335 | |
4336 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false); | |
8d08fdba MS |
4337 | } |
4338 | ||
dd4fae80 ML |
4339 | /* Insert another USING_DECL into the current binding level, returning |
4340 | this declaration. If this is a redeclaration, do nothing, and | |
4341 | return NULL_TREE if this not in namespace scope (in namespace | |
4342 | scope, a using decl might extend any previous bindings). */ | |
e92cc029 | 4343 | |
2c73f9f5 | 4344 | tree |
11f6b451 | 4345 | push_using_decl (tree scope, tree name) |
8d08fdba | 4346 | { |
2c73f9f5 | 4347 | tree decl; |
68642fb6 | 4348 | |
22ffcc6f | 4349 | timevar_push (TV_NAME_LOOKUP); |
2c73f9f5 ML |
4350 | my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383); |
4351 | my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384); | |
4352 | for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl)) | |
4353 | if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name) | |
4354 | break; | |
4355 | if (decl) | |
22ffcc6f GDR |
4356 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, |
4357 | namespace_bindings_p () ? decl : NULL_TREE); | |
4ce3d537 | 4358 | decl = build_lang_decl (USING_DECL, name, void_type_node); |
2c73f9f5 ML |
4359 | DECL_INITIAL (decl) = scope; |
4360 | TREE_CHAIN (decl) = current_binding_level->usings; | |
4361 | current_binding_level->usings = decl; | |
22ffcc6f | 4362 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl); |
8d08fdba MS |
4363 | } |
4364 | ||
ea9635c7 ML |
4365 | /* Add namespace to using_directives. Return NULL_TREE if nothing was |
4366 | changed (i.e. there was already a directive), or the fresh | |
4367 | TREE_LIST otherwise. */ | |
4368 | ||
4369 | tree | |
11f6b451 | 4370 | push_using_directive (tree used) |
ea9635c7 ML |
4371 | { |
4372 | tree ud = current_binding_level->using_directives; | |
9ed182dc | 4373 | tree iter, ancestor; |
68642fb6 | 4374 | |
22ffcc6f | 4375 | timevar_push (TV_NAME_LOOKUP); |
a1c65f9f | 4376 | /* Check if we already have this. */ |
ea9635c7 | 4377 | if (purpose_member (used, ud) != NULL_TREE) |
22ffcc6f | 4378 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); |
00dc6358 | 4379 | |
9ed182dc | 4380 | ancestor = namespace_ancestor (current_decl_namespace (), used); |
00dc6358 | 4381 | ud = current_binding_level->using_directives; |
e1b3e07d | 4382 | ud = tree_cons (used, ancestor, ud); |
ea9635c7 | 4383 | current_binding_level->using_directives = ud; |
70f0e288 JM |
4384 | |
4385 | /* Recursively add all namespaces used. */ | |
4386 | for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter)) | |
4387 | push_using_directive (TREE_PURPOSE (iter)); | |
4388 | ||
22ffcc6f | 4389 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ud); |
ea9635c7 ML |
4390 | } |
4391 | ||
f181d4ae MM |
4392 | /* DECL is a FUNCTION_DECL for a non-member function, which may have |
4393 | other definitions already in place. We get around this by making | |
4394 | the value of the identifier point to a list of all the things that | |
4395 | want to be referenced by that name. It is then up to the users of | |
4396 | that name to decide what to do with that list. | |
8d08fdba | 4397 | |
17aec3eb RK |
4398 | DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its |
4399 | DECL_TEMPLATE_RESULT. It is dealt with the same way. | |
8d08fdba | 4400 | |
7bdbfa05 MM |
4401 | FLAGS is a bitwise-or of the following values: |
4402 | PUSH_LOCAL: Bind DECL in the current scope, rather than at | |
4403 | namespace scope. | |
4404 | PUSH_USING: DECL is being pushed as the result of a using | |
68642fb6 | 4405 | declaration. |
7bdbfa05 | 4406 | |
8d08fdba MS |
4407 | The value returned may be a previous declaration if we guessed wrong |
4408 | about what language DECL should belong to (C or C++). Otherwise, | |
4409 | it's always DECL (and never something that's not a _DECL). */ | |
e92cc029 | 4410 | |
7bdbfa05 | 4411 | tree |
11f6b451 | 4412 | push_overloaded_decl (tree decl, int flags) |
8d08fdba | 4413 | { |
f181d4ae | 4414 | tree name = DECL_NAME (decl); |
700f8a87 | 4415 | tree old; |
f181d4ae | 4416 | tree new_binding; |
7bdbfa05 | 4417 | int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL)); |
700f8a87 | 4418 | |
22ffcc6f | 4419 | timevar_push (TV_NAME_LOOKUP); |
700f8a87 | 4420 | if (doing_global) |
9f33663b | 4421 | old = namespace_binding (name, DECL_CONTEXT (decl)); |
700f8a87 | 4422 | else |
f181d4ae | 4423 | old = lookup_name_current_level (name); |
8d08fdba | 4424 | |
700f8a87 | 4425 | if (old) |
8d08fdba | 4426 | { |
e1cd6e56 | 4427 | if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old)) |
8926095f | 4428 | { |
700f8a87 | 4429 | tree t = TREE_TYPE (old); |
cdf5b885 MS |
4430 | if (IS_AGGR_TYPE (t) && warn_shadow |
4431 | && (! DECL_IN_SYSTEM_HEADER (decl) | |
4432 | || ! DECL_IN_SYSTEM_HEADER (old))) | |
33bd39a2 | 4433 | warning ("`%#D' hides constructor for `%#T'", decl, t); |
5b605f68 | 4434 | old = NULL_TREE; |
8926095f | 4435 | } |
700f8a87 | 4436 | else if (is_overloaded_fn (old)) |
8d08fdba | 4437 | { |
8d08fdba | 4438 | tree tmp; |
68642fb6 | 4439 | |
2c73f9f5 | 4440 | for (tmp = old; tmp; tmp = OVL_NEXT (tmp)) |
7bdbfa05 MM |
4441 | { |
4442 | tree fn = OVL_CURRENT (tmp); | |
4443 | ||
4444 | if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp) | |
4445 | && !(flags & PUSH_USING) | |
4446 | && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)), | |
4447 | TYPE_ARG_TYPES (TREE_TYPE (decl)))) | |
33bd39a2 | 4448 | error ("`%#D' conflicts with previous using declaration `%#D'", |
7bdbfa05 | 4449 | decl, fn); |
68642fb6 | 4450 | |
7bdbfa05 | 4451 | if (duplicate_decls (decl, fn)) |
22ffcc6f | 4452 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fn); |
7bdbfa05 | 4453 | } |
8d08fdba | 4454 | } |
655dc6ee JM |
4455 | else if (old == error_mark_node) |
4456 | /* Ignore the undefined symbol marker. */ | |
4457 | old = NULL_TREE; | |
e1cd6e56 MS |
4458 | else |
4459 | { | |
8251199e | 4460 | cp_error_at ("previous non-function declaration `%#D'", old); |
33bd39a2 | 4461 | error ("conflicts with function declaration `%#D'", decl); |
22ffcc6f | 4462 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl); |
e1cd6e56 | 4463 | } |
8d08fdba | 4464 | } |
7177d104 | 4465 | |
700f8a87 | 4466 | if (old || TREE_CODE (decl) == TEMPLATE_DECL) |
8d08fdba | 4467 | { |
2c73f9f5 | 4468 | if (old && TREE_CODE (old) != OVERLOAD) |
f181d4ae MM |
4469 | new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE)); |
4470 | else | |
4471 | new_binding = ovl_cons (decl, old); | |
347d73d7 ML |
4472 | if (flags & PUSH_USING) |
4473 | OVL_USED (new_binding) = 1; | |
8d08fdba MS |
4474 | } |
4475 | else | |
f181d4ae MM |
4476 | /* NAME is not ambiguous. */ |
4477 | new_binding = decl; | |
700f8a87 MS |
4478 | |
4479 | if (doing_global) | |
f181d4ae | 4480 | set_namespace_binding (name, current_namespace, new_binding); |
700f8a87 | 4481 | else |
f181d4ae MM |
4482 | { |
4483 | /* We only create an OVERLOAD if there was a previous binding at | |
0580c9aa ML |
4484 | this level, or if decl is a template. In the former case, we |
4485 | need to remove the old binding and replace it with the new | |
4486 | binding. We must also run through the NAMES on the binding | |
4487 | level where the name was bound to update the chain. */ | |
4488 | ||
4489 | if (TREE_CODE (new_binding) == OVERLOAD && old) | |
f181d4ae MM |
4490 | { |
4491 | tree *d; | |
68642fb6 | 4492 | |
ed3cf953 | 4493 | for (d = &BINDING_SCOPE (IDENTIFIER_BINDING (name))->names; |
f181d4ae MM |
4494 | *d; |
4495 | d = &TREE_CHAIN (*d)) | |
4496 | if (*d == old | |
4497 | || (TREE_CODE (*d) == TREE_LIST | |
4498 | && TREE_VALUE (*d) == old)) | |
4499 | { | |
d8f8dca1 MM |
4500 | if (TREE_CODE (*d) == TREE_LIST) |
4501 | /* Just replace the old binding with the new. */ | |
4502 | TREE_VALUE (*d) = new_binding; | |
4503 | else | |
4504 | /* Build a TREE_LIST to wrap the OVERLOAD. */ | |
68642fb6 | 4505 | *d = tree_cons (NULL_TREE, new_binding, |
ed5511d9 | 4506 | TREE_CHAIN (*d)); |
d8f8dca1 | 4507 | |
af6fd53f | 4508 | /* And update the cxx_binding node. */ |
d8f8dca1 MM |
4509 | BINDING_VALUE (IDENTIFIER_BINDING (name)) |
4510 | = new_binding; | |
22ffcc6f | 4511 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl); |
f181d4ae MM |
4512 | } |
4513 | ||
d8f8dca1 | 4514 | /* We should always find a previous binding in this case. */ |
a98facb0 | 4515 | abort (); |
f181d4ae MM |
4516 | } |
4517 | ||
4518 | /* Install the new binding. */ | |
0034cf72 | 4519 | push_local_binding (name, new_binding, flags); |
f181d4ae | 4520 | } |
700f8a87 | 4521 | |
22ffcc6f | 4522 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl); |
8d08fdba MS |
4523 | } |
4524 | \f | |
4525 | /* Generate an implicit declaration for identifier FUNCTIONID | |
4526 | as a function of type int (). Print a warning if appropriate. */ | |
4527 | ||
4528 | tree | |
11f6b451 | 4529 | implicitly_declare (tree functionid) |
8d08fdba MS |
4530 | { |
4531 | register tree decl; | |
8d08fdba MS |
4532 | |
4533 | /* We used to reuse an old implicit decl here, | |
4534 | but this loses with inline functions because it can clobber | |
4535 | the saved decl chains. */ | |
4536 | decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type); | |
4537 | ||
4538 | DECL_EXTERNAL (decl) = 1; | |
4539 | TREE_PUBLIC (decl) = 1; | |
4540 | ||
cab1f180 | 4541 | /* ISO standard says implicit declarations are in the innermost block. |
d22c8596 | 4542 | So we record the decl in the standard fashion. */ |
8d08fdba | 4543 | pushdecl (decl); |
3e411c3f | 4544 | rest_of_decl_compilation (decl, NULL, 0, 0); |
8d08fdba MS |
4545 | |
4546 | if (warn_implicit | |
4547 | /* Only one warning per identifier. */ | |
4548 | && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE) | |
4549 | { | |
33bd39a2 | 4550 | pedwarn ("implicit declaration of function `%#D'", decl); |
8d08fdba MS |
4551 | } |
4552 | ||
4553 | SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl); | |
4554 | ||
8d08fdba MS |
4555 | return decl; |
4556 | } | |
4557 | ||
8251199e | 4558 | /* Return zero if the declaration NEWDECL is valid |
8d08fdba MS |
4559 | when the declaration OLDDECL (assumed to be for the same name) |
4560 | has already been seen. | |
4561 | Otherwise return an error message format string with a %s | |
4562 | where the identifier should go. */ | |
4563 | ||
d8e178a0 | 4564 | static const char * |
11f6b451 | 4565 | redeclaration_error_message (tree newdecl, tree olddecl) |
8d08fdba MS |
4566 | { |
4567 | if (TREE_CODE (newdecl) == TYPE_DECL) | |
4568 | { | |
4569 | /* Because C++ can put things into name space for free, | |
4570 | constructs like "typedef struct foo { ... } foo" | |
4571 | would look like an erroneous redeclaration. */ | |
3bfdc719 | 4572 | if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl))) |
8251199e | 4573 | return 0; |
8d08fdba | 4574 | else |
8251199e | 4575 | return "redefinition of `%#D'"; |
8d08fdba MS |
4576 | } |
4577 | else if (TREE_CODE (newdecl) == FUNCTION_DECL) | |
4578 | { | |
4579 | /* If this is a pure function, its olddecl will actually be | |
4580 | the original initialization to `0' (which we force to call | |
4581 | abort()). Don't complain about redefinition in this case. */ | |
fee7654e | 4582 | if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)) |
8251199e | 4583 | return 0; |
8d08fdba | 4584 | |
2c73f9f5 | 4585 | /* If both functions come from different namespaces, this is not |
a1c65f9f | 4586 | a redeclaration - this is a conflict with a used function. */ |
6eb3bb27 | 4587 | if (DECL_NAMESPACE_SCOPE_P (olddecl) |
2c73f9f5 | 4588 | && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)) |
8251199e | 4589 | return "`%D' conflicts with used function"; |
2c73f9f5 | 4590 | |
db5ae43f MS |
4591 | /* We'll complain about linkage mismatches in |
4592 | warn_extern_redeclared_static. */ | |
4593 | ||
2c73f9f5 | 4594 | /* Defining the same name twice is no good. */ |
8d08fdba | 4595 | if (DECL_INITIAL (olddecl) != NULL_TREE |
faae18ab | 4596 | && DECL_INITIAL (newdecl) != NULL_TREE) |
8d08fdba MS |
4597 | { |
4598 | if (DECL_NAME (olddecl) == NULL_TREE) | |
8251199e | 4599 | return "`%#D' not declared in class"; |
8d08fdba | 4600 | else |
8251199e | 4601 | return "redefinition of `%#D'"; |
8d08fdba | 4602 | } |
8251199e | 4603 | return 0; |
8d08fdba | 4604 | } |
51c184be MS |
4605 | else if (TREE_CODE (newdecl) == TEMPLATE_DECL) |
4606 | { | |
ec255269 | 4607 | if ((TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL |
5362b086 | 4608 | && (DECL_TEMPLATE_RESULT (newdecl) |
f4381d62 | 4609 | != DECL_TEMPLATE_RESULT (olddecl)) |
ec255269 MS |
4610 | && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl)) |
4611 | && DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl))) | |
4612 | || (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL | |
d0f062fb NS |
4613 | && COMPLETE_TYPE_P (TREE_TYPE (newdecl)) |
4614 | && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))) | |
8251199e JM |
4615 | return "redefinition of `%#D'"; |
4616 | return 0; | |
51c184be | 4617 | } |
1f51a992 | 4618 | else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl)) |
8d08fdba MS |
4619 | { |
4620 | /* Objects declared at top level: */ | |
4621 | /* If at least one is a reference, it's ok. */ | |
4622 | if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl)) | |
8251199e | 4623 | return 0; |
8926095f | 4624 | /* Reject two definitions. */ |
8251199e | 4625 | return "redefinition of `%#D'"; |
8d08fdba MS |
4626 | } |
4627 | else | |
4628 | { | |
4629 | /* Objects declared with block scope: */ | |
4630 | /* Reject two definitions, and reject a definition | |
4631 | together with an external reference. */ | |
4632 | if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl))) | |
8251199e JM |
4633 | return "redeclaration of `%#D'"; |
4634 | return 0; | |
8d08fdba MS |
4635 | } |
4636 | } | |
4637 | \f | |
acef433b | 4638 | /* Create a new label, named ID. */ |
8d08fdba | 4639 | |
acef433b | 4640 | static tree |
11f6b451 | 4641 | make_label_decl (tree id, int local_p) |
8d08fdba | 4642 | { |
acef433b | 4643 | tree decl; |
8d08fdba | 4644 | |
acef433b | 4645 | decl = build_decl (LABEL_DECL, id, void_type_node); |
b35d4555 | 4646 | if (expanding_p) |
acef433b MM |
4647 | /* Make sure every label has an rtx. */ |
4648 | label_rtx (decl); | |
4649 | ||
4650 | DECL_CONTEXT (decl) = current_function_decl; | |
4651 | DECL_MODE (decl) = VOIDmode; | |
4652 | C_DECLARED_LABEL_FLAG (decl) = local_p; | |
8d08fdba | 4653 | |
acef433b MM |
4654 | /* Say where one reference is to the label, for the sake of the |
4655 | error if it is not defined. */ | |
82a98427 | 4656 | DECL_SOURCE_LOCATION (decl) = input_location; |
acef433b MM |
4657 | |
4658 | /* Record the fact that this identifier is bound to this label. */ | |
4659 | SET_IDENTIFIER_LABEL_VALUE (id, decl); | |
4660 | ||
6625cdb5 JM |
4661 | return decl; |
4662 | } | |
4663 | ||
4664 | /* Record this label on the list of used labels so that we can check | |
4665 | at the end of the function to see whether or not the label was | |
4666 | actually defined, and so we can check when the label is defined whether | |
4667 | this use is valid. */ | |
4668 | ||
4669 | static void | |
11f6b451 | 4670 | use_label (tree decl) |
6625cdb5 JM |
4671 | { |
4672 | if (named_label_uses == NULL | |
4673 | || named_label_uses->names_in_scope != current_binding_level->names | |
4674 | || named_label_uses->label_decl != decl) | |
4675 | { | |
4676 | struct named_label_use_list *new_ent; | |
4677 | new_ent = ((struct named_label_use_list *) | |
4678 | ggc_alloc (sizeof (struct named_label_use_list))); | |
e349ee73 MS |
4679 | new_ent->label_decl = decl; |
4680 | new_ent->names_in_scope = current_binding_level->names; | |
4681 | new_ent->binding_level = current_binding_level; | |
82a98427 | 4682 | new_ent->o_goto_locus = input_location; |
e349ee73 MS |
4683 | new_ent->next = named_label_uses; |
4684 | named_label_uses = new_ent; | |
8d08fdba | 4685 | } |
acef433b | 4686 | } |
8d08fdba | 4687 | |
acef433b MM |
4688 | /* Look for a label named ID in the current function. If one cannot |
4689 | be found, create one. (We keep track of used, but undefined, | |
4690 | labels, and complain about them at the end of a function.) */ | |
8d08fdba | 4691 | |
68642fb6 | 4692 | tree |
11f6b451 | 4693 | lookup_label (tree id) |
acef433b MM |
4694 | { |
4695 | tree decl; | |
6625cdb5 | 4696 | struct named_label_list *ent; |
8d08fdba | 4697 | |
22ffcc6f | 4698 | timevar_push (TV_NAME_LOOKUP); |
acef433b MM |
4699 | /* You can't use labels at global scope. */ |
4700 | if (current_function_decl == NULL_TREE) | |
4701 | { | |
4702 | error ("label `%s' referenced outside of any function", | |
4703 | IDENTIFIER_POINTER (id)); | |
22ffcc6f | 4704 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); |
acef433b | 4705 | } |
68642fb6 | 4706 | |
acef433b MM |
4707 | /* See if we've already got this label. */ |
4708 | decl = IDENTIFIER_LABEL_VALUE (id); | |
4709 | if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl) | |
22ffcc6f | 4710 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl); |
8d08fdba | 4711 | |
acef433b MM |
4712 | /* Record this label on the list of labels used in this function. |
4713 | We do this before calling make_label_decl so that we get the | |
4714 | IDENTIFIER_LABEL_VALUE before the new label is declared. */ | |
6625cdb5 | 4715 | ent = ((struct named_label_list *) |
f8a83ee3 | 4716 | ggc_alloc_cleared (sizeof (struct named_label_list))); |
6625cdb5 JM |
4717 | ent->old_value = IDENTIFIER_LABEL_VALUE (id); |
4718 | ent->next = named_labels; | |
4719 | named_labels = ent; | |
4720 | ||
acef433b MM |
4721 | /* We need a new label. */ |
4722 | decl = make_label_decl (id, /*local_p=*/0); | |
6625cdb5 | 4723 | |
acef433b | 4724 | /* Now fill in the information we didn't have before. */ |
6625cdb5 | 4725 | ent->label_decl = decl; |
8d08fdba | 4726 | |
22ffcc6f | 4727 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl); |
8d08fdba MS |
4728 | } |
4729 | ||
acef433b | 4730 | /* Declare a local label named ID. */ |
8d08fdba MS |
4731 | |
4732 | tree | |
11f6b451 | 4733 | declare_local_label (tree id) |
8d08fdba | 4734 | { |
acef433b | 4735 | tree decl; |
8d08fdba | 4736 | |
acef433b MM |
4737 | /* Add a new entry to the SHADOWED_LABELS list so that when we leave |
4738 | this scope we can restore the old value of | |
4739 | IDENTIFIER_TYPE_VALUE. */ | |
68642fb6 | 4740 | current_binding_level->shadowed_labels |
acef433b MM |
4741 | = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE, |
4742 | current_binding_level->shadowed_labels); | |
4743 | /* Look for the label. */ | |
4744 | decl = make_label_decl (id, /*local_p=*/1); | |
4745 | /* Now fill in the information we didn't have before. */ | |
4746 | TREE_VALUE (current_binding_level->shadowed_labels) = decl; | |
68642fb6 | 4747 | |
acef433b | 4748 | return decl; |
8d08fdba MS |
4749 | } |
4750 | ||
6625cdb5 JM |
4751 | /* Returns nonzero if it is ill-formed to jump past the declaration of |
4752 | DECL. Returns 2 if it's also a real problem. */ | |
4753 | ||
4754 | static int | |
11f6b451 | 4755 | decl_jump_unsafe (tree decl) |
6625cdb5 JM |
4756 | { |
4757 | if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl)) | |
4758 | return 0; | |
4759 | ||
4760 | if (DECL_INITIAL (decl) == NULL_TREE | |
4761 | && pod_type_p (TREE_TYPE (decl))) | |
4762 | return 0; | |
4763 | ||
4764 | /* This is really only important if we're crossing an initialization. | |
4765 | The POD stuff is just pedantry; why should it matter if the class | |
4766 | contains a field of pointer to member type? */ | |
4767 | if (DECL_INITIAL (decl) | |
4768 | || (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))) | |
4769 | return 2; | |
4770 | return 1; | |
4771 | } | |
4772 | ||
4773 | /* Check that a single previously seen jump to a newly defined label | |
4774 | is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for | |
4775 | the jump context; NAMES are the names in scope in LEVEL at the jump | |
4776 | context; FILE and LINE are the source position of the jump or 0. */ | |
4777 | ||
4778 | static void | |
11f6b451 NN |
4779 | check_previous_goto_1 (tree decl, |
4780 | struct cp_binding_level* level, | |
152c16a9 | 4781 | tree names, const location_t *locus) |
6625cdb5 JM |
4782 | { |
4783 | int identified = 0; | |
4784 | int saw_eh = 0; | |
e2500fed | 4785 | struct cp_binding_level *b = current_binding_level; |
6625cdb5 JM |
4786 | for (; b; b = b->level_chain) |
4787 | { | |
4788 | tree new_decls = b->names; | |
4789 | tree old_decls = (b == level ? names : NULL_TREE); | |
4790 | for (; new_decls != old_decls; | |
4791 | new_decls = TREE_CHAIN (new_decls)) | |
4792 | { | |
4793 | int problem = decl_jump_unsafe (new_decls); | |
4794 | if (! problem) | |
4795 | continue; | |
4796 | ||
4797 | if (! identified) | |
4798 | { | |
4799 | if (decl) | |
33bd39a2 | 4800 | pedwarn ("jump to label `%D'", decl); |
6625cdb5 JM |
4801 | else |
4802 | pedwarn ("jump to case label"); | |
4803 | ||
152c16a9 GDR |
4804 | if (locus) |
4805 | pedwarn ("%H from here", locus); | |
6625cdb5 JM |
4806 | identified = 1; |
4807 | } | |
4808 | ||
826840d9 | 4809 | if (problem > 1) |
6625cdb5 JM |
4810 | cp_error_at (" crosses initialization of `%#D'", |
4811 | new_decls); | |
4812 | else | |
4813 | cp_pedwarn_at (" enters scope of non-POD `%#D'", | |
4814 | new_decls); | |
4815 | } | |
4816 | ||
4817 | if (b == level) | |
4818 | break; | |
826840d9 | 4819 | if ((b->is_try_scope || b->is_catch_scope) && ! saw_eh) |
6625cdb5 JM |
4820 | { |
4821 | if (! identified) | |
4822 | { | |
4823 | if (decl) | |
33bd39a2 | 4824 | pedwarn ("jump to label `%D'", decl); |
6625cdb5 JM |
4825 | else |
4826 | pedwarn ("jump to case label"); | |
4827 | ||
152c16a9 GDR |
4828 | if (locus) |
4829 | pedwarn ("%H from here", locus); | |
6625cdb5 JM |
4830 | identified = 1; |
4831 | } | |
826840d9 RH |
4832 | if (b->is_try_scope) |
4833 | error (" enters try block"); | |
4834 | else | |
4835 | error (" enters catch block"); | |
6625cdb5 JM |
4836 | saw_eh = 1; |
4837 | } | |
4838 | } | |
4839 | } | |
4840 | ||
4841 | static void | |
11f6b451 | 4842 | check_previous_goto (struct named_label_use_list* use) |
6625cdb5 JM |
4843 | { |
4844 | check_previous_goto_1 (use->label_decl, use->binding_level, | |
152c16a9 | 4845 | use->names_in_scope, &use->o_goto_locus); |
6625cdb5 JM |
4846 | } |
4847 | ||
4848 | static void | |
11f6b451 | 4849 | check_switch_goto (struct cp_binding_level* level) |
6625cdb5 | 4850 | { |
152c16a9 | 4851 | check_previous_goto_1 (NULL_TREE, level, level->names, NULL); |
6625cdb5 JM |
4852 | } |
4853 | ||
4854 | /* Check that any previously seen jumps to a newly defined label DECL | |
4855 | are OK. Called by define_label. */ | |
4856 | ||
4857 | static void | |
11f6b451 | 4858 | check_previous_gotos (tree decl) |
6625cdb5 JM |
4859 | { |
4860 | struct named_label_use_list **usep; | |
4861 | ||
4862 | if (! TREE_USED (decl)) | |
4863 | return; | |
4864 | ||
4865 | for (usep = &named_label_uses; *usep; ) | |
4866 | { | |
4867 | struct named_label_use_list *use = *usep; | |
4868 | if (use->label_decl == decl) | |
4869 | { | |
4870 | check_previous_goto (use); | |
4871 | *usep = use->next; | |
4872 | } | |
4873 | else | |
4874 | usep = &(use->next); | |
4875 | } | |
4876 | } | |
4877 | ||
4878 | /* Check that a new jump to a label DECL is OK. Called by | |
4879 | finish_goto_stmt. */ | |
4880 | ||
4881 | void | |
11f6b451 | 4882 | check_goto (tree decl) |
6625cdb5 JM |
4883 | { |
4884 | int identified = 0; | |
4885 | tree bad; | |
4886 | struct named_label_list *lab; | |
4887 | ||
e3cd9945 APB |
4888 | /* We can't know where a computed goto is jumping. So we assume |
4889 | that it's OK. */ | |
4890 | if (! DECL_P (decl)) | |
4891 | return; | |
4892 | ||
6625cdb5 JM |
4893 | /* If the label hasn't been defined yet, defer checking. */ |
4894 | if (! DECL_INITIAL (decl)) | |
4895 | { | |
4896 | use_label (decl); | |
4897 | return; | |
4898 | } | |
4899 | ||
4900 | for (lab = named_labels; lab; lab = lab->next) | |
4901 | if (decl == lab->label_decl) | |
4902 | break; | |
4903 | ||
4904 | /* If the label is not on named_labels it's a gcc local label, so | |
4905 | it must be in an outer scope, so jumping to it is always OK. */ | |
4906 | if (lab == 0) | |
4907 | return; | |
4908 | ||
826840d9 RH |
4909 | if ((lab->in_try_scope || lab->in_catch_scope || lab->bad_decls) |
4910 | && !identified) | |
6625cdb5 JM |
4911 | { |
4912 | cp_pedwarn_at ("jump to label `%D'", decl); | |
4913 | pedwarn (" from here"); | |
4914 | identified = 1; | |
4915 | } | |
4916 | ||
4917 | for (bad = lab->bad_decls; bad; bad = TREE_CHAIN (bad)) | |
4918 | { | |
4919 | tree b = TREE_VALUE (bad); | |
4920 | int u = decl_jump_unsafe (b); | |
4921 | ||
4922 | if (u > 1 && DECL_ARTIFICIAL (b)) | |
4923 | /* Can't skip init of __exception_info. */ | |
4924 | cp_error_at (" enters catch block", b); | |
4925 | else if (u > 1) | |
4926 | cp_error_at (" skips initialization of `%#D'", b); | |
4927 | else | |
4928 | cp_pedwarn_at (" enters scope of non-POD `%#D'", b); | |
4929 | } | |
4930 | ||
826840d9 | 4931 | if (lab->in_try_scope) |
6625cdb5 | 4932 | error (" enters try block"); |
826840d9 RH |
4933 | else if (lab->in_catch_scope) |
4934 | error (" enters catch block"); | |
6625cdb5 JM |
4935 | } |
4936 | ||
8d08fdba MS |
4937 | /* Define a label, specifying the location in the source file. |
4938 | Return the LABEL_DECL node for the label, if the definition is valid. | |
4939 | Otherwise return 0. */ | |
4940 | ||
4941 | tree | |
11f6b451 | 4942 | define_label (const char* filename, int line, tree name) |
8d08fdba | 4943 | { |
f01b0acb | 4944 | tree decl = lookup_label (name); |
6625cdb5 | 4945 | struct named_label_list *ent; |
e2500fed | 4946 | register struct cp_binding_level *p; |
6625cdb5 | 4947 | |
22ffcc6f | 4948 | timevar_push (TV_NAME_LOOKUP); |
6625cdb5 JM |
4949 | for (ent = named_labels; ent; ent = ent->next) |
4950 | if (ent->label_decl == decl) | |
4951 | break; | |
8d08fdba | 4952 | |
3dc9aec6 | 4953 | /* After labels, make any new cleanups in the function go into their |
8d08fdba | 4954 | own new (temporary) binding contour. */ |
3dc9aec6 WC |
4955 | for (p = current_binding_level; !(p->parm_flag); p = p->level_chain) |
4956 | p->more_cleanups_ok = 0; | |
8d08fdba | 4957 | |
e1cd6e56 | 4958 | if (name == get_identifier ("wchar_t")) |
33bd39a2 | 4959 | pedwarn ("label named wchar_t"); |
e1cd6e56 | 4960 | |
8d08fdba MS |
4961 | if (DECL_INITIAL (decl) != NULL_TREE) |
4962 | { | |
33bd39a2 | 4963 | error ("duplicate label `%D'", decl); |
e8298892 | 4964 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); |
8d08fdba MS |
4965 | } |
4966 | else | |
4967 | { | |
8d08fdba MS |
4968 | /* Mark label as having been defined. */ |
4969 | DECL_INITIAL (decl) = error_mark_node; | |
4970 | /* Say where in the source. */ | |
4971 | DECL_SOURCE_FILE (decl) = filename; | |
4972 | DECL_SOURCE_LINE (decl) = line; | |
6625cdb5 JM |
4973 | if (ent) |
4974 | { | |
4975 | ent->names_in_scope = current_binding_level->names; | |
4976 | ent->binding_level = current_binding_level; | |
4977 | } | |
4978 | check_previous_gotos (decl); | |
22ffcc6f | 4979 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl); |
8d08fdba | 4980 | } |
22ffcc6f | 4981 | timevar_pop (TV_NAME_LOOKUP); |
8d08fdba MS |
4982 | } |
4983 | ||
a5894242 MS |
4984 | struct cp_switch |
4985 | { | |
e2500fed | 4986 | struct cp_binding_level *level; |
a5894242 | 4987 | struct cp_switch *next; |
56cb9733 MM |
4988 | /* The SWITCH_STMT being built. */ |
4989 | tree switch_stmt; | |
4990 | /* A splay-tree mapping the low element of a case range to the high | |
4991 | element, or NULL_TREE if there is no high element. Used to | |
4992 | determine whether or not a new case label duplicates an old case | |
4993 | label. We need a tree, rather than simply a hash table, because | |
4994 | of the GNU case range extension. */ | |
4995 | splay_tree cases; | |
a5894242 MS |
4996 | }; |
4997 | ||
56cb9733 MM |
4998 | /* A stack of the currently active switch statements. The innermost |
4999 | switch statement is on the top of the stack. There is no need to | |
5000 | mark the stack for garbage collection because it is only active | |
5001 | during the processing of the body of a function, and we never | |
5002 | collect at that point. */ | |
5362b086 | 5003 | |
a5894242 MS |
5004 | static struct cp_switch *switch_stack; |
5005 | ||
56cb9733 MM |
5006 | /* Called right after a switch-statement condition is parsed. |
5007 | SWITCH_STMT is the switch statement being parsed. */ | |
5008 | ||
a5894242 | 5009 | void |
11f6b451 | 5010 | push_switch (tree switch_stmt) |
a5894242 MS |
5011 | { |
5012 | struct cp_switch *p | |
bedda2da | 5013 | = (struct cp_switch *) xmalloc (sizeof (struct cp_switch)); |
a5894242 MS |
5014 | p->level = current_binding_level; |
5015 | p->next = switch_stack; | |
56cb9733 MM |
5016 | p->switch_stmt = switch_stmt; |
5017 | p->cases = splay_tree_new (case_compare, NULL, NULL); | |
a5894242 MS |
5018 | switch_stack = p; |
5019 | } | |
5020 | ||
5021 | void | |
11f6b451 | 5022 | pop_switch (void) |
a5894242 | 5023 | { |
bedda2da | 5024 | struct cp_switch *cs; |
5362b086 | 5025 | |
bedda2da | 5026 | cs = switch_stack; |
56cb9733 | 5027 | splay_tree_delete (cs->cases); |
a5894242 | 5028 | switch_stack = switch_stack->next; |
bedda2da | 5029 | free (cs); |
a5894242 MS |
5030 | } |
5031 | ||
b0a1da19 JM |
5032 | /* Note that we've seen a definition of a case label, and complain if this |
5033 | is a bad place for one. */ | |
e92cc029 | 5034 | |
3e4d04a1 | 5035 | tree |
11f6b451 | 5036 | finish_case_label (tree low_value, tree high_value) |
8d08fdba | 5037 | { |
3e4d04a1 | 5038 | tree cond, r; |
e2500fed | 5039 | register struct cp_binding_level *p; |
a5894242 | 5040 | |
b0a1da19 | 5041 | if (! switch_stack) |
56cb9733 MM |
5042 | { |
5043 | if (high_value) | |
5044 | error ("case label not within a switch statement"); | |
5045 | else if (low_value) | |
33bd39a2 | 5046 | error ("case label `%E' not within a switch statement", |
56cb9733 MM |
5047 | low_value); |
5048 | else | |
5049 | error ("`default' label not within a switch statement"); | |
3e4d04a1 | 5050 | return NULL_TREE; |
56cb9733 MM |
5051 | } |
5052 | ||
56cb9733 MM |
5053 | if (processing_template_decl) |
5054 | { | |
8f17b5c5 MM |
5055 | tree label; |
5056 | ||
56cb9733 MM |
5057 | /* For templates, just add the case label; we'll do semantic |
5058 | analysis at instantiation-time. */ | |
8f17b5c5 | 5059 | label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE); |
3e4d04a1 | 5060 | return add_stmt (build_case_label (low_value, high_value, label)); |
56cb9733 MM |
5061 | } |
5062 | ||
5063 | /* Find the condition on which this switch statement depends. */ | |
5064 | cond = SWITCH_COND (switch_stack->switch_stmt); | |
5065 | if (cond && TREE_CODE (cond) == TREE_LIST) | |
5066 | cond = TREE_VALUE (cond); | |
56cb9733 | 5067 | |
3e4d04a1 | 5068 | r = c_add_case_label (switch_stack->cases, cond, low_value, high_value); |
8d08fdba | 5069 | |
6625cdb5 | 5070 | check_switch_goto (switch_stack->level); |
a5894242 | 5071 | |
3dc9aec6 | 5072 | /* After labels, make any new cleanups in the function go into their |
8d08fdba | 5073 | own new (temporary) binding contour. */ |
3dc9aec6 WC |
5074 | for (p = current_binding_level; !(p->parm_flag); p = p->level_chain) |
5075 | p->more_cleanups_ok = 0; | |
3e4d04a1 RH |
5076 | |
5077 | return r; | |
8d08fdba MS |
5078 | } |
5079 | \f | |
5080 | /* Return the list of declarations of the current level. | |
5081 | Note that this list is in reverse order unless/until | |
5082 | you nreverse it; and when you do nreverse it, you must | |
5083 | store the result back using `storedecls' or you will lose. */ | |
5084 | ||
5085 | tree | |
11f6b451 | 5086 | getdecls (void) |
8d08fdba MS |
5087 | { |
5088 | return current_binding_level->names; | |
5089 | } | |
5090 | ||
8d08fdba MS |
5091 | /* Store the list of declarations of the current level. |
5092 | This is done for the parameter declarations of a function being defined, | |
5093 | after they are modified in the light of any missing parameters. */ | |
5094 | ||
5095 | static void | |
11f6b451 | 5096 | storedecls (tree decls) |
8d08fdba MS |
5097 | { |
5098 | current_binding_level->names = decls; | |
5099 | } | |
5100 | ||
5e0c54e5 GDR |
5101 | /* Set the current binding TABLE for type declarations.. This is a |
5102 | temporary workaround of the fact that the data structure classtypes | |
5103 | does not currently carry its allocated cxx_scope structure. */ | |
280f9385 | 5104 | void |
5e0c54e5 | 5105 | cxx_remember_type_decls (binding_table table) |
8d08fdba | 5106 | { |
5e0c54e5 | 5107 | current_binding_level->type_decls = table; |
8d08fdba MS |
5108 | } |
5109 | \f | |
4782bd5b RS |
5110 | /* Return the type that should be used when TYPE's name is preceded |
5111 | by a tag such as 'struct' or 'union', or null if the name cannot | |
5112 | be used in this way. | |
5113 | ||
5114 | For example, when processing the third line of: | |
5115 | ||
5116 | struct A; | |
5117 | typedef struct A A; | |
5118 | struct A; | |
5119 | ||
5120 | lookup of A will find the typedef. Given A's typedef, this function | |
5121 | will return the type associated with "struct A". For the tag to be | |
5122 | anything other than TYPE, TYPE must be a typedef whose original type | |
5123 | has the same name and context as TYPE itself. | |
5124 | ||
5125 | It is not valid for a typedef of an anonymous type to be used with | |
5126 | an explicit tag: | |
5127 | ||
5128 | typedef struct { ... } B; | |
5129 | struct B; | |
5130 | ||
5131 | Return null for this case. */ | |
5132 | ||
5133 | static tree | |
11f6b451 | 5134 | follow_tag_typedef (tree type) |
4782bd5b RS |
5135 | { |
5136 | tree original; | |
5137 | ||
5138 | original = original_type (type); | |
3a73bffb JJ |
5139 | if (! TYPE_NAME (original)) |
5140 | return NULL_TREE; | |
4782bd5b RS |
5141 | if (TYPE_IDENTIFIER (original) == TYPE_IDENTIFIER (type) |
5142 | && (CP_DECL_CONTEXT (TYPE_NAME (original)) | |
5143 | == CP_DECL_CONTEXT (TYPE_NAME (type))) | |
5144 | && !(CLASS_TYPE_P (original) && TYPE_WAS_ANONYMOUS (original))) | |
5145 | return original; | |
5146 | else | |
5147 | return NULL_TREE; | |
5148 | } | |
5149 | ||
8d08fdba MS |
5150 | /* Given NAME, an IDENTIFIER_NODE, |
5151 | return the structure (or union or enum) definition for that name. | |
ed3cf953 | 5152 | Searches binding levels from BINDING_SCOPE up to the global level. |
8d08fdba MS |
5153 | If THISLEVEL_ONLY is nonzero, searches only the specified context |
5154 | (but skips any tag-transparent contexts to find one that is | |
5155 | meaningful for tags). | |
5156 | FORM says which kind of type the caller wants; | |
5157 | it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE. | |
5158 | If the wrong kind of type is found, and it's not a template, an error is | |
5159 | reported. */ | |
5160 | ||
5161 | static tree | |
af6fd53f GDR |
5162 | lookup_tag (enum tree_code form, tree name, |
5163 | struct cp_binding_level* binding_level, int thislevel_only) | |
8d08fdba | 5164 | { |
e2500fed | 5165 | register struct cp_binding_level *level; |
838dfd8a | 5166 | /* Nonzero if, we should look past a template parameter level, even |
74b846e0 MM |
5167 | if THISLEVEL_ONLY. */ |
5168 | int allow_template_parms_p = 1; | |
5e0c54e5 | 5169 | bool type_is_anonymous = ANON_AGGRNAME_P (name); |
8d08fdba | 5170 | |
22ffcc6f | 5171 | timevar_push (TV_NAME_LOOKUP); |
8d08fdba MS |
5172 | for (level = binding_level; level; level = level->level_chain) |
5173 | { | |
5174 | register tree tail; | |
5e0c54e5 GDR |
5175 | if (type_is_anonymous && level->type_decls != NULL) |
5176 | { | |
5177 | tree type = binding_table_find_anon_type (level->type_decls, name); | |
5178 | /* There is no need for error checking here, because | |
5179 | anon names are unique throughout the compilation. */ | |
5180 | if (type != NULL) | |
5181 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, type); | |
5182 | } | |
2c73f9f5 | 5183 | else if (level->namespace_p) |
a1c65f9f | 5184 | /* Do namespace lookup. */ |
6c011b01 | 5185 | for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail)) |
2c73f9f5 | 5186 | { |
af6fd53f | 5187 | cxx_binding *binding = |
ed3cf953 | 5188 | cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (tail), name); |
af6fd53f | 5189 | tree old; |
36a117a5 | 5190 | |
74b846e0 MM |
5191 | /* If we just skipped past a template parameter level, |
5192 | even though THISLEVEL_ONLY, and we find a template | |
5193 | class declaration, then we use the _TYPE node for the | |
36a117a5 | 5194 | template. See the example below. */ |
74b846e0 | 5195 | if (thislevel_only && !allow_template_parms_p |
af6fd53f GDR |
5196 | && binding && BINDING_VALUE (binding) |
5197 | && DECL_CLASS_TEMPLATE_P (BINDING_VALUE (binding))) | |
5198 | old = TREE_TYPE (BINDING_VALUE (binding)); | |
5199 | else if (binding) | |
5200 | old = BINDING_TYPE (binding); | |
5201 | else | |
5202 | old = NULL_TREE; | |
36a117a5 | 5203 | |
4782bd5b | 5204 | if (old) |
2c73f9f5 | 5205 | { |
4782bd5b RS |
5206 | /* We've found something at this binding level. If it is |
5207 | a typedef, extract the tag it refers to. Lookup fails | |
5208 | if the typedef doesn't refer to a taggable type. */ | |
5209 | old = follow_tag_typedef (old); | |
5210 | if (!old) | |
22ffcc6f | 5211 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); |
4782bd5b RS |
5212 | if (TREE_CODE (old) != form |
5213 | && (form == ENUMERAL_TYPE | |
5214 | || TREE_CODE (old) == ENUMERAL_TYPE)) | |
5215 | { | |
5216 | error ("`%#D' redeclared as %C", old, form); | |
22ffcc6f | 5217 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); |
4782bd5b | 5218 | } |
22ffcc6f | 5219 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old); |
2c73f9f5 | 5220 | } |
2c73f9f5 | 5221 | if (thislevel_only || tail == global_namespace) |
22ffcc6f | 5222 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); |
2c73f9f5 | 5223 | } |
5e0c54e5 GDR |
5224 | else if (level->type_decls != NULL) |
5225 | { | |
5226 | binding_entry entry = binding_table_find (level->type_decls, name); | |
5227 | if (entry != NULL) | |
5228 | { | |
5229 | enum tree_code code = TREE_CODE (entry->type); | |
fd87b7a7 | 5230 | |
5e0c54e5 GDR |
5231 | if (code != form |
5232 | && (form == ENUMERAL_TYPE || code == ENUMERAL_TYPE)) | |
5233 | { | |
5234 | /* Definition isn't the kind we were looking for. */ | |
5235 | error ("`%#D' redeclared as %C", entry->type, form); | |
5236 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); | |
5237 | } | |
5238 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->type); | |
5239 | } | |
8d08fdba MS |
5240 | } |
5241 | if (thislevel_only && ! level->tag_transparent) | |
5566b478 | 5242 | { |
74b846e0 | 5243 | if (level->template_parms_p && allow_template_parms_p) |
5566b478 | 5244 | { |
36a117a5 | 5245 | /* We must deal with cases like this: |
68642fb6 | 5246 | |
36a117a5 MM |
5247 | template <class T> struct S; |
5248 | template <class T> struct S {}; | |
68642fb6 | 5249 | |
36a117a5 MM |
5250 | When looking up `S', for the second declaration, we |
5251 | would like to find the first declaration. But, we | |
5252 | are in the pseudo-global level created for the | |
5253 | template parameters, rather than the (surrounding) | |
5254 | namespace level. Thus, we keep going one more level, | |
838dfd8a | 5255 | even though THISLEVEL_ONLY is nonzero. */ |
74b846e0 | 5256 | allow_template_parms_p = 0; |
36a117a5 | 5257 | continue; |
5566b478 | 5258 | } |
36a117a5 | 5259 | else |
22ffcc6f | 5260 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); |
5566b478 | 5261 | } |
8d08fdba | 5262 | } |
22ffcc6f | 5263 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); |
8d08fdba MS |
5264 | } |
5265 | ||
8d08fdba MS |
5266 | /* Given a type, find the tag that was defined for it and return the tag name. |
5267 | Otherwise return 0. However, the value can never be 0 | |
5268 | in the cases in which this is used. | |
5269 | ||
838dfd8a | 5270 | C++: If NAME is nonzero, this is the new name to install. This is |
8d08fdba MS |
5271 | done when replacing anonymous tags with real tag names. */ |
5272 | ||
5273 | static tree | |
11f6b451 | 5274 | lookup_tag_reverse (tree type, tree name) |
8d08fdba | 5275 | { |
e2500fed | 5276 | register struct cp_binding_level *level; |
8d08fdba | 5277 | |
22ffcc6f | 5278 | timevar_push (TV_NAME_LOOKUP); |
8d08fdba MS |
5279 | for (level = current_binding_level; level; level = level->level_chain) |
5280 | { | |
5e0c54e5 GDR |
5281 | binding_entry entry = level->type_decls == NULL |
5282 | ? NULL | |
5283 | : binding_table_reverse_maybe_remap (level->type_decls, type, name); | |
5284 | if (entry) | |
5285 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->name); | |
8d08fdba | 5286 | } |
22ffcc6f | 5287 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); |
8d08fdba | 5288 | } |
8d08fdba | 5289 | \f |
a9aedbc2 | 5290 | /* Look up NAME in the NAMESPACE. */ |
e92cc029 | 5291 | |
a9aedbc2 | 5292 | tree |
11f6b451 | 5293 | lookup_namespace_name (tree namespace, tree name) |
a9aedbc2 | 5294 | { |
30394414 | 5295 | tree val; |
f30c84c9 | 5296 | tree template_id = NULL_TREE; |
af6fd53f | 5297 | cxx_binding binding; |
2c73f9f5 | 5298 | |
22ffcc6f | 5299 | timevar_push (TV_NAME_LOOKUP); |
30394414 | 5300 | my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370); |
1231fb96 | 5301 | |
1231fb96 | 5302 | if (TREE_CODE (name) == NAMESPACE_DECL) |
a1c65f9f | 5303 | /* This happens for A::B<int> when B is a namespace. */ |
22ffcc6f | 5304 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, name); |
d8f8dca1 MM |
5305 | else if (TREE_CODE (name) == TEMPLATE_DECL) |
5306 | { | |
5307 | /* This happens for A::B where B is a template, and there are no | |
5308 | template arguments. */ | |
33bd39a2 | 5309 | error ("invalid use of `%D'", name); |
22ffcc6f | 5310 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
d8f8dca1 | 5311 | } |
1231fb96 | 5312 | |
b262d64c JM |
5313 | namespace = ORIGINAL_NAMESPACE (namespace); |
5314 | ||
f30c84c9 MM |
5315 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) |
5316 | { | |
5317 | template_id = name; | |
5318 | name = TREE_OPERAND (name, 0); | |
5319 | if (TREE_CODE (name) == OVERLOAD) | |
5320 | name = DECL_NAME (OVL_CURRENT (name)); | |
2f939d94 | 5321 | else if (DECL_P (name)) |
f30c84c9 MM |
5322 | name = DECL_NAME (name); |
5323 | } | |
5324 | ||
30394414 | 5325 | my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373); |
68642fb6 | 5326 | |
af6fd53f GDR |
5327 | cxx_binding_clear (&binding); |
5328 | if (!qualified_lookup_using_namespace (name, namespace, &binding, 0)) | |
22ffcc6f | 5329 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
2c73f9f5 | 5330 | |
af6fd53f | 5331 | if (binding.value) |
1c35f5b6 | 5332 | { |
af6fd53f | 5333 | val = binding.value; |
1c35f5b6 | 5334 | |
f30c84c9 MM |
5335 | if (template_id) |
5336 | { | |
5337 | if (DECL_CLASS_TEMPLATE_P (val)) | |
68642fb6 | 5338 | val = lookup_template_class (val, |
f30c84c9 MM |
5339 | TREE_OPERAND (template_id, 1), |
5340 | /*in_decl=*/NULL_TREE, | |
5341 | /*context=*/NULL_TREE, | |
f9c244b8 | 5342 | /*entering_scope=*/0, |
c2ea3a40 | 5343 | tf_error | tf_warning); |
f30c84c9 MM |
5344 | else if (DECL_FUNCTION_TEMPLATE_P (val) |
5345 | || TREE_CODE (val) == OVERLOAD) | |
68642fb6 | 5346 | val = lookup_template_function (val, |
f30c84c9 MM |
5347 | TREE_OPERAND (template_id, 1)); |
5348 | else | |
5349 | { | |
33bd39a2 | 5350 | error ("`%D::%D' is not a template", |
f30c84c9 | 5351 | namespace, name); |
22ffcc6f | 5352 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
f30c84c9 MM |
5353 | } |
5354 | } | |
5355 | ||
1c35f5b6 JM |
5356 | /* If we have a single function from a using decl, pull it out. */ |
5357 | if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val)) | |
5358 | val = OVL_FUNCTION (val); | |
d52e4867 RS |
5359 | |
5360 | /* Ignore built-in functions that haven't been prototyped yet. */ | |
5361 | if (!val || !DECL_P(val) | |
5362 | || !DECL_LANG_SPECIFIC(val) | |
5363 | || !DECL_ANTICIPATED (val)) | |
22ffcc6f | 5364 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val); |
1c35f5b6 JM |
5365 | } |
5366 | ||
33bd39a2 | 5367 | error ("`%D' undeclared in namespace `%D'", name, namespace); |
22ffcc6f | 5368 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
a9aedbc2 MS |
5369 | } |
5370 | ||
7ddedda4 MM |
5371 | /* Hash a TYPENAME_TYPE. K is really of type `tree'. */ |
5372 | ||
e2500fed | 5373 | static hashval_t |
11f6b451 | 5374 | typename_hash (const void* k) |
7ddedda4 | 5375 | { |
e2500fed GK |
5376 | hashval_t hash; |
5377 | tree t = (tree) k; | |
7ddedda4 | 5378 | |
7bdfd72e KG |
5379 | hash = (htab_hash_pointer (TYPE_CONTEXT (t)) |
5380 | ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t)))); | |
7ddedda4 MM |
5381 | |
5382 | return hash; | |
5383 | } | |
5384 | ||
5385 | /* Compare two TYPENAME_TYPEs. K1 and K2 are really of type `tree'. */ | |
5386 | ||
e2500fed | 5387 | static int |
11f6b451 | 5388 | typename_compare (const void * k1, const void * k2) |
7ddedda4 MM |
5389 | { |
5390 | tree t1; | |
5391 | tree t2; | |
5392 | tree d1; | |
5393 | tree d2; | |
5394 | ||
5395 | t1 = (tree) k1; | |
5396 | t2 = (tree) k2; | |
5397 | d1 = TYPE_NAME (t1); | |
5398 | d2 = TYPE_NAME (t2); | |
68642fb6 | 5399 | |
7ddedda4 | 5400 | return (DECL_NAME (d1) == DECL_NAME (d2) |
e2500fed | 5401 | && TYPE_CONTEXT (t1) == TYPE_CONTEXT (t2) |
68642fb6 | 5402 | && ((TREE_TYPE (t1) != NULL_TREE) |
7ddedda4 MM |
5403 | == (TREE_TYPE (t2) != NULL_TREE)) |
5404 | && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)) | |
5405 | && TYPENAME_TYPE_FULLNAME (t1) == TYPENAME_TYPE_FULLNAME (t2)); | |
5406 | } | |
5407 | ||
45869a6c MM |
5408 | /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is |
5409 | the type of `T', NAME is the IDENTIFIER_NODE for `t'. If BASE_TYPE | |
5410 | is non-NULL, this type is being created by the implicit typename | |
5411 | extension, and BASE_TYPE is a type named `t' in some base class of | |
68642fb6 | 5412 | `T' which depends on template parameters. |
45869a6c MM |
5413 | |
5414 | Returns the new TYPENAME_TYPE. */ | |
5415 | ||
e2500fed GK |
5416 | static GTY ((param_is (union tree_node))) htab_t typename_htab; |
5417 | ||
45869a6c | 5418 | tree |
8fbc5ae7 | 5419 | build_typename_type (tree context, tree name, tree fullname) |
45869a6c MM |
5420 | { |
5421 | tree t; | |
5422 | tree d; | |
fad205ff | 5423 | void **e; |
45869a6c | 5424 | |
e2500fed | 5425 | if (typename_htab == NULL) |
9cd64686 | 5426 | { |
e2500fed GK |
5427 | typename_htab = htab_create_ggc (61, &typename_hash, |
5428 | &typename_compare, NULL); | |
9cd64686 | 5429 | } |
45869a6c MM |
5430 | |
5431 | /* Build the TYPENAME_TYPE. */ | |
33848bb0 | 5432 | t = make_aggr_type (TYPENAME_TYPE); |
45869a6c MM |
5433 | TYPE_CONTEXT (t) = FROB_CONTEXT (context); |
5434 | TYPENAME_TYPE_FULLNAME (t) = fullname; | |
45869a6c MM |
5435 | |
5436 | /* Build the corresponding TYPE_DECL. */ | |
5437 | d = build_decl (TYPE_DECL, name, t); | |
5438 | TYPE_NAME (TREE_TYPE (d)) = d; | |
5439 | TYPE_STUB_DECL (TREE_TYPE (d)) = d; | |
5440 | DECL_CONTEXT (d) = FROB_CONTEXT (context); | |
2642b9bf | 5441 | DECL_ARTIFICIAL (d) = 1; |
45869a6c | 5442 | |
7ddedda4 | 5443 | /* See if we already have this type. */ |
e2500fed GK |
5444 | e = htab_find_slot (typename_htab, t, INSERT); |
5445 | if (*e) | |
5446 | t = (tree) *e; | |
7ddedda4 | 5447 | else |
e2500fed | 5448 | *e = t; |
7ddedda4 | 5449 | |
45869a6c MM |
5450 | return t; |
5451 | } | |
5452 | ||
3baa501d MM |
5453 | /* Resolve `typename CONTEXT::NAME'. Returns an appropriate type, |
5454 | unless an error occurs, in which case error_mark_node is returned. | |
4f2b0fb2 NS |
5455 | If we locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is |
5456 | set, we return that, rather than the _TYPE it corresponds to, in | |
5457 | other cases we look through the type decl. If TF_ERROR is set, | |
5458 | complain about errors, otherwise be quiet. */ | |
3baa501d | 5459 | |
5566b478 | 5460 | tree |
11f6b451 | 5461 | make_typename_type (tree context, tree name, tsubst_flags_t complain) |
5566b478 | 5462 | { |
b2b7d40a | 5463 | tree fullname; |
a80e4195 | 5464 | |
5dc5d13c KL |
5465 | if (name == error_mark_node |
5466 | || context == NULL_TREE | |
5467 | || context == error_mark_node) | |
5468 | return error_mark_node; | |
5469 | ||
2f939d94 | 5470 | if (TYPE_P (name)) |
78638e24 | 5471 | { |
68642fb6 UD |
5472 | if (!(TYPE_LANG_SPECIFIC (name) |
5473 | && (CLASSTYPE_IS_TEMPLATE (name) | |
78638e24 MM |
5474 | || CLASSTYPE_USE_TEMPLATE (name)))) |
5475 | name = TYPE_IDENTIFIER (name); | |
5476 | else | |
5477 | /* Create a TEMPLATE_ID_EXPR for the type. */ | |
5478 | name = build_nt (TEMPLATE_ID_EXPR, | |
5479 | CLASSTYPE_TI_TEMPLATE (name), | |
5480 | CLASSTYPE_TI_ARGS (name)); | |
5481 | } | |
653cc74a | 5482 | else if (TREE_CODE (name) == TYPE_DECL) |
a80e4195 | 5483 | name = DECL_NAME (name); |
b2b7d40a JM |
5484 | |
5485 | fullname = name; | |
5486 | ||
5487 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) | |
11686454 JM |
5488 | { |
5489 | name = TREE_OPERAND (name, 0); | |
5490 | if (TREE_CODE (name) == TEMPLATE_DECL) | |
5491 | name = TREE_OPERAND (fullname, 0) = DECL_NAME (name); | |
5492 | } | |
8a2b77e7 JM |
5493 | if (TREE_CODE (name) == TEMPLATE_DECL) |
5494 | { | |
33bd39a2 | 5495 | error ("`%D' used without template parameters", name); |
8a2b77e7 JM |
5496 | return error_mark_node; |
5497 | } | |
b2b7d40a | 5498 | if (TREE_CODE (name) != IDENTIFIER_NODE) |
a98facb0 | 5499 | abort (); |
5566b478 | 5500 | |
04ddee1b BK |
5501 | if (TREE_CODE (context) == NAMESPACE_DECL) |
5502 | { | |
5503 | /* We can get here from typename_sub0 in the explicit_template_type | |
5504 | expansion. Just fail. */ | |
4f2b0fb2 | 5505 | if (complain & tf_error) |
33bd39a2 | 5506 | error ("no class template named `%#T' in `%#T'", |
3baa501d | 5507 | name, context); |
04ddee1b BK |
5508 | return error_mark_node; |
5509 | } | |
5510 | ||
85b71cf2 | 5511 | if (! uses_template_parms (context) |
b77ead33 | 5512 | || currently_open_class (context)) |
5566b478 | 5513 | { |
b2b7d40a JM |
5514 | if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR) |
5515 | { | |
ad810b22 | 5516 | tree tmpl = NULL_TREE; |
b2b7d40a | 5517 | if (IS_AGGR_TYPE (context)) |
86ac0575 | 5518 | tmpl = lookup_field (context, name, 0, false); |
ad810b22 | 5519 | if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl)) |
b2b7d40a | 5520 | { |
4f2b0fb2 | 5521 | if (complain & tf_error) |
33bd39a2 | 5522 | error ("no class template named `%#T' in `%#T'", |
3baa501d | 5523 | name, context); |
b2b7d40a JM |
5524 | return error_mark_node; |
5525 | } | |
ffb690bd | 5526 | |
2b59fc25 | 5527 | if (complain & tf_error) |
78757caa | 5528 | perform_or_defer_access_check (context, tmpl); |
ba59df78 | 5529 | |
68642fb6 | 5530 | return lookup_template_class (tmpl, |
ad810b22 | 5531 | TREE_OPERAND (fullname, 1), |
68642fb6 | 5532 | NULL_TREE, context, |
f9c244b8 | 5533 | /*entering_scope=*/0, |
c2ea3a40 | 5534 | tf_error | tf_warning); |
b2b7d40a JM |
5535 | } |
5536 | else | |
5566b478 | 5537 | { |
b4f70b3d | 5538 | tree t; |
68642fb6 | 5539 | |
b4f70b3d | 5540 | if (!IS_AGGR_TYPE (context)) |
b2b7d40a | 5541 | { |
4f2b0fb2 | 5542 | if (complain & tf_error) |
33bd39a2 | 5543 | error ("no type named `%#T' in `%#T'", name, context); |
b2b7d40a JM |
5544 | return error_mark_node; |
5545 | } | |
1107c4b3 | 5546 | |
86ac0575 | 5547 | t = lookup_field (context, name, 0, true); |
7d4bdeed | 5548 | if (t) |
4f2b0fb2 | 5549 | { |
8c081e84 MM |
5550 | if (TREE_CODE (t) != TYPE_DECL) |
5551 | { | |
5552 | if (complain & tf_error) | |
5553 | error ("no type named `%#T' in `%#T'", name, context); | |
5554 | return error_mark_node; | |
5555 | } | |
5556 | ||
2b59fc25 | 5557 | if (complain & tf_error) |
78757caa | 5558 | perform_or_defer_access_check (context, t); |
ba59df78 | 5559 | |
4f2b0fb2 NS |
5560 | if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl)) |
5561 | t = TREE_TYPE (t); | |
ba3307c0 | 5562 | |
4f2b0fb2 NS |
5563 | return t; |
5564 | } | |
5566b478 | 5565 | } |
5566b478 | 5566 | } |
11249cf0 MM |
5567 | |
5568 | /* If the CONTEXT is not a template type, then either the field is | |
5569 | there now or its never going to be. */ | |
b4f70b3d | 5570 | if (!uses_template_parms (context)) |
11249cf0 | 5571 | { |
4f2b0fb2 | 5572 | if (complain & tf_error) |
33bd39a2 | 5573 | error ("no type named `%#T' in `%#T'", name, context); |
11249cf0 MM |
5574 | return error_mark_node; |
5575 | } | |
68642fb6 | 5576 | |
8fbc5ae7 | 5577 | return build_typename_type (context, name, fullname); |
5566b478 MS |
5578 | } |
5579 | ||
b8c6534b KL |
5580 | /* Resolve `CONTEXT::template NAME'. Returns an appropriate type, |
5581 | unless an error occurs, in which case error_mark_node is returned. | |
4f2b0fb2 NS |
5582 | If we locate a TYPE_DECL, we return that, rather than the _TYPE it |
5583 | corresponds to. If COMPLAIN zero, don't complain about any errors | |
5584 | that occur. */ | |
b8c6534b KL |
5585 | |
5586 | tree | |
11f6b451 | 5587 | make_unbound_class_template (tree context, tree name, tsubst_flags_t complain) |
b8c6534b KL |
5588 | { |
5589 | tree t; | |
5590 | tree d; | |
5591 | ||
5592 | if (TYPE_P (name)) | |
5593 | name = TYPE_IDENTIFIER (name); | |
5594 | else if (DECL_P (name)) | |
5595 | name = DECL_NAME (name); | |
5596 | if (TREE_CODE (name) != IDENTIFIER_NODE) | |
a98facb0 | 5597 | abort (); |
b8c6534b KL |
5598 | |
5599 | if (!uses_template_parms (context) | |
5600 | || currently_open_class (context)) | |
5601 | { | |
5602 | tree tmpl = NULL_TREE; | |
5603 | ||
5604 | if (IS_AGGR_TYPE (context)) | |
86ac0575 | 5605 | tmpl = lookup_field (context, name, 0, false); |
b8c6534b KL |
5606 | |
5607 | if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl)) | |
5608 | { | |
4f09be91 | 5609 | if (complain & tf_error) |
33bd39a2 | 5610 | error ("no class template named `%#T' in `%#T'", name, context); |
b8c6534b KL |
5611 | return error_mark_node; |
5612 | } | |
5613 | ||
2b59fc25 | 5614 | if (complain & tf_error) |
78757caa | 5615 | perform_or_defer_access_check (context, tmpl); |
b8c6534b KL |
5616 | |
5617 | return tmpl; | |
5618 | } | |
5619 | ||
5620 | /* Build the UNBOUND_CLASS_TEMPLATE. */ | |
5621 | t = make_aggr_type (UNBOUND_CLASS_TEMPLATE); | |
5622 | TYPE_CONTEXT (t) = FROB_CONTEXT (context); | |
5623 | TREE_TYPE (t) = NULL_TREE; | |
5624 | ||
5625 | /* Build the corresponding TEMPLATE_DECL. */ | |
5626 | d = build_decl (TEMPLATE_DECL, name, t); | |
5627 | TYPE_NAME (TREE_TYPE (d)) = d; | |
5628 | TYPE_STUB_DECL (TREE_TYPE (d)) = d; | |
5629 | DECL_CONTEXT (d) = FROB_CONTEXT (context); | |
5630 | DECL_ARTIFICIAL (d) = 1; | |
5631 | ||
5632 | return t; | |
5633 | } | |
5634 | ||
a1c65f9f | 5635 | /* Select the right _DECL from multiple choices. */ |
2c73f9f5 ML |
5636 | |
5637 | static tree | |
af6fd53f | 5638 | select_decl (cxx_binding *binding, int flags) |
2c73f9f5 ML |
5639 | { |
5640 | tree val; | |
5641 | val = BINDING_VALUE (binding); | |
97ba1e3c | 5642 | |
22ffcc6f | 5643 | timevar_push (TV_NAME_LOOKUP); |
52c11ef6 | 5644 | if (LOOKUP_NAMESPACES_ONLY (flags)) |
3e3f722c | 5645 | { |
a1c65f9f | 5646 | /* We are not interested in types. */ |
3e3f722c | 5647 | if (val && TREE_CODE (val) == NAMESPACE_DECL) |
22ffcc6f GDR |
5648 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val); |
5649 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); | |
3e3f722c | 5650 | } |
68642fb6 | 5651 | |
2c73f9f5 ML |
5652 | /* If we could have a type and |
5653 | we have nothing or we need a type and have none. */ | |
5654 | if (BINDING_TYPE (binding) | |
52c11ef6 JM |
5655 | && (!val || ((flags & LOOKUP_PREFER_TYPES) |
5656 | && TREE_CODE (val) != TYPE_DECL))) | |
2c73f9f5 | 5657 | val = TYPE_STUB_DECL (BINDING_TYPE (binding)); |
a1c65f9f | 5658 | /* Don't return non-types if we really prefer types. */ |
52c11ef6 | 5659 | else if (val && LOOKUP_TYPES_ONLY (flags) && TREE_CODE (val) != TYPE_DECL |
c592d5d2 ML |
5660 | && (TREE_CODE (val) != TEMPLATE_DECL |
5661 | || !DECL_CLASS_TEMPLATE_P (val))) | |
2c73f9f5 | 5662 | val = NULL_TREE; |
1c35f5b6 | 5663 | |
22ffcc6f | 5664 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val); |
2c73f9f5 ML |
5665 | } |
5666 | ||
2c169bab JM |
5667 | /* Unscoped lookup of a global: iterate over current namespaces, |
5668 | considering using-directives. If SPACESP is non-NULL, store a list | |
5669 | of the namespaces we've considered in it. */ | |
ea9635c7 | 5670 | |
2c169bab | 5671 | tree |
11f6b451 | 5672 | unqualified_namespace_lookup (tree name, int flags, tree* spacesp) |
ea9635c7 | 5673 | { |
ee8fc32b | 5674 | tree initial = current_decl_namespace (); |
ea9635c7 ML |
5675 | tree scope = initial; |
5676 | tree siter; | |
e2500fed | 5677 | struct cp_binding_level *level; |
ea9635c7 | 5678 | tree val = NULL_TREE; |
af6fd53f | 5679 | cxx_binding binding; |
ea9635c7 | 5680 | |
22ffcc6f | 5681 | timevar_push (TV_NAME_LOOKUP); |
af6fd53f | 5682 | cxx_binding_clear (&binding); |
2c169bab JM |
5683 | if (spacesp) |
5684 | *spacesp = NULL_TREE; | |
5685 | ||
5686 | for (; !val; scope = CP_DECL_CONTEXT (scope)) | |
ea9635c7 | 5687 | { |
ed3cf953 GDR |
5688 | cxx_binding *b = |
5689 | cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name); | |
2c169bab | 5690 | if (spacesp) |
e1b3e07d | 5691 | *spacesp = tree_cons (scope, NULL_TREE, *spacesp); |
ea9635c7 | 5692 | |
d52e4867 | 5693 | /* Ignore anticipated built-in functions. */ |
af6fd53f GDR |
5694 | if (b && BINDING_VALUE (b) |
5695 | && DECL_P (BINDING_VALUE (b)) | |
5696 | && DECL_LANG_SPECIFIC (BINDING_VALUE (b)) | |
5697 | && DECL_ANTICIPATED (BINDING_VALUE (b))) | |
5698 | /* Keep binding cleared. */; | |
5699 | else if (b) | |
d52e4867 | 5700 | { |
a1c65f9f | 5701 | /* Initialize binding for this context. */ |
af6fd53f GDR |
5702 | binding.value = BINDING_VALUE (b); |
5703 | binding.type = BINDING_TYPE (b); | |
d52e4867 | 5704 | } |
ea9635c7 | 5705 | |
a1c65f9f | 5706 | /* Add all _DECLs seen through local using-directives. */ |
68642fb6 | 5707 | for (level = current_binding_level; |
ea9635c7 ML |
5708 | !level->namespace_p; |
5709 | level = level->level_chain) | |
af6fd53f | 5710 | if (!lookup_using_namespace (name, &binding, level->using_directives, |
2c169bab | 5711 | scope, flags, spacesp)) |
a1c65f9f | 5712 | /* Give up because of error. */ |
22ffcc6f | 5713 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
ea9635c7 | 5714 | |
a1c65f9f KH |
5715 | /* Add all _DECLs seen through global using-directives. */ |
5716 | /* XXX local and global using lists should work equally. */ | |
ea9635c7 ML |
5717 | siter = initial; |
5718 | while (1) | |
5719 | { | |
af6fd53f GDR |
5720 | if (!lookup_using_namespace (name, &binding, |
5721 | DECL_NAMESPACE_USING (siter), | |
2c169bab | 5722 | scope, flags, spacesp)) |
a1c65f9f | 5723 | /* Give up because of error. */ |
22ffcc6f | 5724 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); |
ea9635c7 ML |
5725 | if (siter == scope) break; |
5726 | siter = CP_DECL_CONTEXT (siter); | |
5727 | } | |
5728 | ||
af6fd53f | 5729 | val = select_decl (&binding, flags); |
ea9635c7 ML |
5730 | if (scope == global_namespace) |
5731 | break; | |
ea9635c7 | 5732 | } |
22ffcc6f | 5733 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val); |
ea9635c7 ML |
5734 | } |
5735 | ||
52c11ef6 JM |
5736 | /* Combine prefer_type and namespaces_only into flags. */ |
5737 | ||
5738 | static int | |
11f6b451 | 5739 | lookup_flags (int prefer_type, int namespaces_only) |
52c11ef6 JM |
5740 | { |
5741 | if (namespaces_only) | |
5742 | return LOOKUP_PREFER_NAMESPACES; | |
5743 | if (prefer_type > 1) | |
5744 | return LOOKUP_PREFER_TYPES; | |
5745 | if (prefer_type > 0) | |
5746 | return LOOKUP_PREFER_BOTH; | |
5747 | return 0; | |
5748 | } | |
5749 | ||
5750 | /* Given a lookup that returned VAL, use FLAGS to decide if we want to | |
5751 | ignore it or not. Subroutine of lookup_name_real. */ | |
5752 | ||
5753 | static tree | |
11f6b451 | 5754 | qualify_lookup (tree val, int flags) |
52c11ef6 JM |
5755 | { |
5756 | if (val == NULL_TREE) | |
5757 | return val; | |
6b945830 JM |
5758 | if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL) |
5759 | return val; | |
eea9800f | 5760 | if ((flags & LOOKUP_PREFER_TYPES) && TREE_CODE (val) == TYPE_DECL) |
6b945830 JM |
5761 | return val; |
5762 | if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES)) | |
52c11ef6 JM |
5763 | return NULL_TREE; |
5764 | return val; | |
5765 | } | |
5766 | ||
a723baf1 MM |
5767 | /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL |
5768 | or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type | |
5769 | bindings. | |
5770 | ||
5771 | Returns a DECL (or OVERLOAD, or BASELINK) representing the | |
5772 | declaration found. */ | |
5773 | ||
5774 | tree | |
5775 | lookup_qualified_name (tree scope, tree name, bool is_type_p, int flags) | |
5776 | { | |
5777 | if (TREE_CODE (scope) == NAMESPACE_DECL) | |
5778 | { | |
af6fd53f | 5779 | cxx_binding binding; |
a723baf1 | 5780 | |
af6fd53f | 5781 | cxx_binding_clear (&binding); |
a723baf1 MM |
5782 | flags |= LOOKUP_COMPLAIN; |
5783 | if (is_type_p) | |
5784 | flags |= LOOKUP_PREFER_TYPES; | |
af6fd53f | 5785 | if (!qualified_lookup_using_namespace (name, scope, &binding, flags)) |
a723baf1 | 5786 | return NULL_TREE; |
af6fd53f | 5787 | return select_decl (&binding, flags); |
a723baf1 MM |
5788 | } |
5789 | else | |
5790 | return lookup_member (scope, name, 0, is_type_p); | |
5791 | } | |
5792 | ||
5f261ba9 MM |
5793 | /* Check to see whether or not DECL is a variable that would have been |
5794 | in scope under the ARM, but is not in scope under the ANSI/ISO | |
5795 | standard. If so, issue an error message. If name lookup would | |
5796 | work in both cases, but return a different result, this function | |
5797 | returns the result of ANSI/ISO lookup. Otherwise, it returns | |
5798 | DECL. */ | |
5799 | ||
5800 | tree | |
5801 | check_for_out_of_scope_variable (tree decl) | |
5802 | { | |
5803 | tree shadowed; | |
5804 | ||
5805 | /* We only care about out of scope variables. */ | |
5806 | if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl))) | |
5807 | return decl; | |
5808 | ||
5809 | shadowed = DECL_SHADOWED_FOR_VAR (decl); | |
5810 | while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL | |
5811 | && DECL_DEAD_FOR_LOCAL (shadowed)) | |
5812 | shadowed = DECL_SHADOWED_FOR_VAR (shadowed); | |
5813 | if (!shadowed) | |
5814 | shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl)); | |
5815 | if (shadowed) | |
5816 | { | |
5817 | if (!DECL_ERROR_REPORTED (decl)) | |
5818 | { | |
5819 | warning ("name lookup of `%D' changed", | |
5820 | DECL_NAME (decl)); | |
5821 | cp_warning_at (" matches this `%D' under ISO standard rules", | |
5822 | shadowed); | |
5823 | cp_warning_at (" matches this `%D' under old rules", decl); | |
5824 | DECL_ERROR_REPORTED (decl) = 1; | |
5825 | } | |
5826 | return shadowed; | |
5827 | } | |
5828 | ||
5829 | /* If we have already complained about this declaration, there's no | |
5830 | need to do it again. */ | |
5831 | if (DECL_ERROR_REPORTED (decl)) | |
5832 | return decl; | |
5833 | ||
5834 | DECL_ERROR_REPORTED (decl) = 1; | |
5835 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))) | |
5836 | { | |
5837 | error ("name lookup of `%D' changed for new ISO `for' scoping", | |
5838 | DECL_NAME (decl)); | |
5839 | cp_error_at (" cannot use obsolete binding at `%D' because it has a destructor", decl); | |
5840 | return error_mark_node; | |
5841 | } | |
5842 | else | |
5843 | { | |
5844 | pedwarn ("name lookup of `%D' changed for new ISO `for' scoping", | |
5845 | DECL_NAME (decl)); | |
5846 | cp_pedwarn_at (" using obsolete binding at `%D'", decl); | |
5847 | } | |
5848 | ||
5849 | return decl; | |
5850 | } | |
5851 | ||
8d08fdba MS |
5852 | /* Look up NAME in the current binding level and its superiors in the |
5853 | namespace of variables, functions and typedefs. Return a ..._DECL | |
5854 | node of some kind representing its definition if there is only one | |
5855 | such declaration, or return a TREE_LIST with all the overloaded | |
5856 | definitions if there are many, or return 0 if it is undefined. | |
5857 | ||
2c73f9f5 ML |
5858 | If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces. |
5859 | If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces). | |
68642fb6 | 5860 | Otherwise we prefer non-TYPE_DECLs. |
f84b4be9 | 5861 | |
838dfd8a | 5862 | If NONCLASS is nonzero, we don't look for the NAME in class scope, |
f84b4be9 | 5863 | using IDENTIFIER_CLASS_VALUE. */ |
8d08fdba | 5864 | |
a723baf1 | 5865 | tree |
af6fd53f GDR |
5866 | lookup_name_real (tree name, int prefer_type, int nonclass, |
5867 | int namespaces_only, int flags) | |
8d08fdba | 5868 | { |
af6fd53f | 5869 | cxx_binding *iter; |
235f734d | 5870 | tree val = NULL_TREE; |
8d08fdba | 5871 | |
22ffcc6f | 5872 | timevar_push (TV_NAME_LOOKUP); |
a723baf1 MM |
5873 | /* Conversion operators are handled specially because ordinary |
5874 | unqualified name lookup will not find template conversion | |
5875 | operators. */ | |
5876 | if (IDENTIFIER_TYPENAME_P (name)) | |
a28e3c7f | 5877 | { |
a723baf1 | 5878 | struct cp_binding_level *level; |
68642fb6 | 5879 | |
a723baf1 MM |
5880 | for (level = current_binding_level; |
5881 | level && !level->namespace_p; | |
5882 | level = level->level_chain) | |
a28e3c7f | 5883 | { |
a723baf1 MM |
5884 | tree class_type; |
5885 | tree operators; | |
5886 | ||
5887 | /* A conversion operator can only be declared in a class | |
5888 | scope. */ | |
5889 | if (level->parm_flag != 2) | |
5890 | continue; | |
5891 | ||
5892 | /* Lookup the conversion operator in the class. */ | |
82a2669e | 5893 | class_type = level->this_entity; |
a723baf1 MM |
5894 | operators = lookup_fnfields (class_type, name, /*protect=*/0); |
5895 | if (operators) | |
22ffcc6f | 5896 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, operators); |
a28e3c7f | 5897 | } |
e1cd6e56 | 5898 | |
22ffcc6f | 5899 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); |
5b163de4 | 5900 | } |
e76a2646 | 5901 | |
a723baf1 MM |
5902 | flags |= lookup_flags (prefer_type, namespaces_only); |
5903 | ||
d8f8dca1 | 5904 | /* First, look in non-namespace scopes. */ |
6f1b4c42 JM |
5905 | |
5906 | if (current_class_type == NULL_TREE) | |
5907 | nonclass = 1; | |
5908 | ||
af6fd53f | 5909 | for (iter = IDENTIFIER_BINDING (name); iter; iter = iter->previous) |
d8f8dca1 | 5910 | { |
235f734d MM |
5911 | tree binding; |
5912 | ||
af6fd53f | 5913 | if (!LOCAL_BINDING_P (iter) && nonclass) |
d8f8dca1 MM |
5914 | /* We're not looking for class-scoped bindings, so keep going. */ |
5915 | continue; | |
68642fb6 | 5916 | |
d8f8dca1 | 5917 | /* If this is the kind of thing we're looking for, we're done. */ |
af6fd53f GDR |
5918 | if (qualify_lookup (BINDING_VALUE (iter), flags)) |
5919 | binding = BINDING_VALUE (iter); | |
68642fb6 | 5920 | else if ((flags & LOOKUP_PREFER_TYPES) |
af6fd53f GDR |
5921 | && qualify_lookup (BINDING_TYPE (iter), flags)) |
5922 | binding = BINDING_TYPE (iter); | |
235f734d MM |
5923 | else |
5924 | binding = NULL_TREE; | |
5925 | ||
8fbc5ae7 | 5926 | if (binding) |
d8f8dca1 | 5927 | { |
235f734d | 5928 | val = binding; |
8fbc5ae7 | 5929 | break; |
d8f8dca1 MM |
5930 | } |
5931 | } | |
f181d4ae | 5932 | |
235f734d | 5933 | /* Now lookup in namespace scopes. */ |
8fbc5ae7 | 5934 | if (!val) |
e76a2646 | 5935 | { |
af6fd53f | 5936 | tree t = unqualified_namespace_lookup (name, flags, 0); |
235f734d | 5937 | if (t) |
8fbc5ae7 | 5938 | val = t; |
c1def683 JM |
5939 | } |
5940 | ||
8d08fdba MS |
5941 | if (val) |
5942 | { | |
0c64a9ca JM |
5943 | /* If we have a single function from a using decl, pull it out. */ |
5944 | if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val)) | |
5945 | val = OVL_FUNCTION (val); | |
8d08fdba | 5946 | } |
0ca7178c | 5947 | |
22ffcc6f | 5948 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val); |
8d08fdba MS |
5949 | } |
5950 | ||
700f8a87 | 5951 | tree |
11f6b451 | 5952 | lookup_name_nonclass (tree name) |
700f8a87 | 5953 | { |
a723baf1 | 5954 | return lookup_name_real (name, 0, 1, 0, LOOKUP_COMPLAIN); |
700f8a87 MS |
5955 | } |
5956 | ||
2c73f9f5 | 5957 | tree |
11f6b451 | 5958 | lookup_function_nonclass (tree name, tree args) |
2c73f9f5 ML |
5959 | { |
5960 | return lookup_arg_dependent (name, lookup_name_nonclass (name), args); | |
5961 | } | |
5962 | ||
700f8a87 | 5963 | tree |
11f6b451 | 5964 | lookup_name (tree name, int prefer_type) |
700f8a87 | 5965 | { |
a723baf1 | 5966 | return lookup_name_real (name, prefer_type, 0, 0, LOOKUP_COMPLAIN); |
700f8a87 MS |
5967 | } |
5968 | ||
a7d2d407 MM |
5969 | /* Similar to `lookup_name' but look only in the innermost non-class |
5970 | binding level. */ | |
8d08fdba MS |
5971 | |
5972 | tree | |
11f6b451 | 5973 | lookup_name_current_level (tree name) |
8d08fdba | 5974 | { |
e2500fed | 5975 | struct cp_binding_level *b; |
a7d2d407 | 5976 | tree t = NULL_TREE; |
8d08fdba | 5977 | |
22ffcc6f | 5978 | timevar_push (TV_NAME_LOOKUP); |
a7d2d407 MM |
5979 | b = current_binding_level; |
5980 | while (b->parm_flag == 2) | |
5981 | b = b->level_chain; | |
5982 | ||
5983 | if (b->namespace_p) | |
8d08fdba | 5984 | { |
5fdaba89 | 5985 | t = IDENTIFIER_NAMESPACE_VALUE (name); |
8d08fdba MS |
5986 | |
5987 | /* extern "C" function() */ | |
5988 | if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST) | |
5989 | t = TREE_VALUE (t); | |
5990 | } | |
68642fb6 | 5991 | else if (IDENTIFIER_BINDING (name) |
f181d4ae | 5992 | && LOCAL_BINDING_P (IDENTIFIER_BINDING (name))) |
8d08fdba | 5993 | { |
a4443a08 MS |
5994 | while (1) |
5995 | { | |
ed3cf953 | 5996 | if (BINDING_SCOPE (IDENTIFIER_BINDING (name)) == b) |
22ffcc6f | 5997 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, IDENTIFIER_VALUE (name)); |
68642fb6 | 5998 | |
9ed182dc JM |
5999 | if (b->keep == 2) |
6000 | b = b->level_chain; | |
6001 | else | |
6002 | break; | |
6003 | } | |
6004 | } | |
6005 | ||
22ffcc6f | 6006 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); |
9ed182dc JM |
6007 | } |
6008 | ||
6009 | /* Like lookup_name_current_level, but for types. */ | |
6010 | ||
6011 | tree | |
11f6b451 | 6012 | lookup_type_current_level (tree name) |
9ed182dc JM |
6013 | { |
6014 | register tree t = NULL_TREE; | |
6015 | ||
22ffcc6f | 6016 | timevar_push (TV_NAME_LOOKUP); |
9ed182dc JM |
6017 | my_friendly_assert (! current_binding_level->namespace_p, 980716); |
6018 | ||
6019 | if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE | |
6020 | && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node) | |
6021 | { | |
e2500fed | 6022 | struct cp_binding_level *b = current_binding_level; |
9ed182dc JM |
6023 | while (1) |
6024 | { | |
6025 | if (purpose_member (name, b->type_shadowed)) | |
22ffcc6f GDR |
6026 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, |
6027 | REAL_IDENTIFIER_TYPE_VALUE (name)); | |
a4443a08 MS |
6028 | if (b->keep == 2) |
6029 | b = b->level_chain; | |
6030 | else | |
6031 | break; | |
6032 | } | |
8d08fdba MS |
6033 | } |
6034 | ||
22ffcc6f | 6035 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); |
8d08fdba | 6036 | } |
3e3f722c | 6037 | |
8d08fdba | 6038 | \f |
8d08fdba | 6039 | /* Push the declarations of builtin types into the namespace. |
0e5921e8 | 6040 | RID_INDEX is the index of the builtin type |
8d08fdba MS |
6041 | in the array RID_POINTERS. NAME is the name used when looking |
6042 | up the builtin type. TYPE is the _TYPE node for the builtin type. */ | |
6043 | ||
eaa7c03f | 6044 | void |
11f6b451 NN |
6045 | record_builtin_type (enum rid rid_index, |
6046 | const char* name, | |
6047 | tree type) | |
8d08fdba MS |
6048 | { |
6049 | tree rname = NULL_TREE, tname = NULL_TREE; | |
a703fb38 | 6050 | tree tdecl = NULL_TREE; |
8d08fdba | 6051 | |
0e5921e8 | 6052 | if ((int) rid_index < (int) RID_MAX) |
8d08fdba MS |
6053 | rname = ridpointers[(int) rid_index]; |
6054 | if (name) | |
6055 | tname = get_identifier (name); | |
6056 | ||
8d08fdba MS |
6057 | if (tname) |
6058 | { | |
8d08fdba | 6059 | tdecl = pushdecl (build_decl (TYPE_DECL, tname, type)); |
8d08fdba | 6060 | set_identifier_type_value (tname, NULL_TREE); |
0e5921e8 | 6061 | if ((int) rid_index < (int) RID_MAX) |
a1c65f9f | 6062 | /* Built-in types live in the global namespace. */ |
2c73f9f5 | 6063 | SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl); |
8d08fdba MS |
6064 | } |
6065 | if (rname != NULL_TREE) | |
6066 | { | |
6067 | if (tname != NULL_TREE) | |
6068 | { | |
6069 | set_identifier_type_value (rname, NULL_TREE); | |
2c73f9f5 | 6070 | SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl); |
8d08fdba MS |
6071 | } |
6072 | else | |
6073 | { | |
8d08fdba | 6074 | tdecl = pushdecl (build_decl (TYPE_DECL, rname, type)); |
8d08fdba MS |
6075 | set_identifier_type_value (rname, NULL_TREE); |
6076 | } | |
6077 | } | |
8d08fdba MS |
6078 | } |
6079 | ||
eff71ab0 | 6080 | /* Record one of the standard Java types. |
4d8a1dd6 MM |
6081 | * Declare it as having the given NAME. |
6082 | * If SIZE > 0, it is the size of one of the integral types; | |
6083 | * otherwise it is the negative of the size of one of the other types. */ | |
eff71ab0 PB |
6084 | |
6085 | static tree | |
11f6b451 | 6086 | record_builtin_java_type (const char* name, int size) |
eff71ab0 PB |
6087 | { |
6088 | tree type, decl; | |
6089 | if (size > 0) | |
6090 | type = make_signed_type (size); | |
6091 | else if (size > -32) | |
a1c65f9f | 6092 | { /* "__java_char" or ""__java_boolean". */ |
eff71ab0 PB |
6093 | type = make_unsigned_type (-size); |
6094 | /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/ | |
6095 | } | |
6096 | else | |
a1c65f9f | 6097 | { /* "__java_float" or ""__java_double". */ |
eff71ab0 PB |
6098 | type = make_node (REAL_TYPE); |
6099 | TYPE_PRECISION (type) = - size; | |
6100 | layout_type (type); | |
6101 | } | |
0e5921e8 | 6102 | record_builtin_type (RID_MAX, name, type); |
eff71ab0 | 6103 | decl = TYPE_NAME (type); |
e229f2cd PB |
6104 | |
6105 | /* Suppress generate debug symbol entries for these types, | |
6106 | since for normal C++ they are just clutter. | |
a1c65f9f | 6107 | However, push_lang_context undoes this if extern "Java" is seen. */ |
eff71ab0 | 6108 | DECL_IGNORED_P (decl) = 1; |
e229f2cd | 6109 | |
eff71ab0 PB |
6110 | TYPE_FOR_JAVA (type) = 1; |
6111 | return type; | |
6112 | } | |
6113 | ||
a1c65f9f | 6114 | /* Push a type into the namespace so that the back-ends ignore it. */ |
036407f7 ML |
6115 | |
6116 | static void | |
11f6b451 | 6117 | record_unknown_type (tree type, const char* name) |
036407f7 ML |
6118 | { |
6119 | tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type)); | |
6120 | /* Make sure the "unknown type" typedecl gets ignored for debug info. */ | |
6121 | DECL_IGNORED_P (decl) = 1; | |
6122 | TYPE_DECL_SUPPRESS_DEBUG (decl) = 1; | |
6123 | TYPE_SIZE (type) = TYPE_SIZE (void_type_node); | |
6124 | TYPE_ALIGN (type) = 1; | |
11cf4d18 | 6125 | TYPE_USER_ALIGN (type) = 0; |
036407f7 | 6126 | TYPE_MODE (type) = TYPE_MODE (void_type_node); |
68642fb6 | 6127 | } |
036407f7 | 6128 | |
d43829f9 MM |
6129 | /* An string for which we should create an IDENTIFIER_NODE at |
6130 | startup. */ | |
6131 | ||
6132 | typedef struct predefined_identifier | |
6133 | { | |
6134 | /* The name of the identifier. */ | |
8b60264b | 6135 | const char *const name; |
d43829f9 | 6136 | /* The place where the IDENTIFIER_NODE should be stored. */ |
8b60264b | 6137 | tree *const node; |
838dfd8a | 6138 | /* Nonzero if this is the name of a constructor or destructor. */ |
8b60264b | 6139 | const int ctor_or_dtor_p; |
d43829f9 MM |
6140 | } predefined_identifier; |
6141 | ||
6142 | /* Create all the predefined identifiers. */ | |
6143 | ||
6144 | static void | |
11f6b451 | 6145 | initialize_predefined_identifiers (void) |
d43829f9 | 6146 | { |
8b60264b | 6147 | const predefined_identifier *pid; |
d43829f9 MM |
6148 | |
6149 | /* A table of identifiers to create at startup. */ | |
8b60264b | 6150 | static const predefined_identifier predefined_identifiers[] = { |
298d6f60 MM |
6151 | { "C++", &lang_name_cplusplus, 0 }, |
6152 | { "C", &lang_name_c, 0 }, | |
6153 | { "Java", &lang_name_java, 0 }, | |
6154 | { CTOR_NAME, &ctor_identifier, 1 }, | |
6155 | { "__base_ctor", &base_ctor_identifier, 1 }, | |
6156 | { "__comp_ctor", &complete_ctor_identifier, 1 }, | |
6157 | { DTOR_NAME, &dtor_identifier, 1 }, | |
6158 | { "__comp_dtor", &complete_dtor_identifier, 1 }, | |
6159 | { "__base_dtor", &base_dtor_identifier, 1 }, | |
6160 | { "__deleting_dtor", &deleting_dtor_identifier, 1 }, | |
298d6f60 | 6161 | { IN_CHARGE_NAME, &in_charge_identifier, 0 }, |
298d6f60 MM |
6162 | { "nelts", &nelts_identifier, 0 }, |
6163 | { THIS_NAME, &this_identifier, 0 }, | |
c4372ef4 | 6164 | { VTABLE_DELTA_NAME, &delta_identifier, 0 }, |
298d6f60 | 6165 | { VTABLE_PFN_NAME, &pfn_identifier, 0 }, |
298d6f60 | 6166 | { "_vptr", &vptr_identifier, 0 }, |
3ec6bad3 | 6167 | { "__vtt_parm", &vtt_parm_identifier, 0 }, |
82a2669e | 6168 | { "::", &global_scope_name, 0 }, |
1f6e1acc | 6169 | { "std", &std_identifier, 0 }, |
298d6f60 | 6170 | { NULL, NULL, 0 } |
d43829f9 MM |
6171 | }; |
6172 | ||
6173 | for (pid = predefined_identifiers; pid->name; ++pid) | |
298d6f60 MM |
6174 | { |
6175 | *pid->node = get_identifier (pid->name); | |
6176 | if (pid->ctor_or_dtor_p) | |
6177 | IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1; | |
6178 | } | |
d43829f9 MM |
6179 | } |
6180 | ||
8d08fdba MS |
6181 | /* Create the predefined scalar types of C, |
6182 | and some nodes representing standard constants (0, 1, (void *)0). | |
6183 | Initialize the global binding level. | |
6184 | Make definitions for built-in primitive functions. */ | |
6185 | ||
6186 | void | |
11f6b451 | 6187 | cxx_init_decl_processing (void) |
8d08fdba | 6188 | { |
10841285 MM |
6189 | tree void_ftype; |
6190 | tree void_ftype_ptr; | |
8d08fdba | 6191 | |
d43829f9 MM |
6192 | /* Create all the identifiers we need. */ |
6193 | initialize_predefined_identifiers (); | |
8d08fdba | 6194 | |
8f17b5c5 | 6195 | /* Fill in back-end hooks. */ |
d363e7bf | 6196 | lang_missing_noreturn_ok_p = &cp_missing_noreturn_ok_p; |
99dccabc | 6197 | |
9cd64686 MM |
6198 | /* Create the global variables. */ |
6199 | push_to_top_level (); | |
8012c983 | 6200 | |
82a2669e GDR |
6201 | current_function_decl = NULL_TREE; |
6202 | current_binding_level = NULL_BINDING_LEVEL; | |
6203 | free_binding_level = NULL_BINDING_LEVEL; | |
a1c65f9f | 6204 | /* Enter the global namespace. */ |
30394414 | 6205 | my_friendly_assert (global_namespace == NULL_TREE, 375); |
82a2669e GDR |
6206 | global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name, |
6207 | void_type_node); | |
6208 | initial_push_namespace_scope (global_namespace); | |
6209 | ||
30394414 JM |
6210 | current_lang_name = NULL_TREE; |
6211 | ||
bccd95ae | 6212 | /* Adjust various flags based on command-line settings. */ |
2642b9bf JM |
6213 | if (! flag_permissive && ! pedantic) |
6214 | flag_pedantic_errors = 1; | |
bccd95ae | 6215 | if (!flag_no_inline) |
b850de4f MM |
6216 | { |
6217 | flag_inline_trees = 1; | |
6218 | flag_no_inline = 1; | |
6219 | } | |
acc72c37 MM |
6220 | if (flag_inline_functions) |
6221 | { | |
6222 | flag_inline_trees = 2; | |
6223 | flag_inline_functions = 0; | |
6224 | } | |
830fcda8 | 6225 | |
f963b5d9 RS |
6226 | /* Force minimum function alignment if using the least significant |
6227 | bit of function pointers to store the virtual bit. */ | |
6228 | if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn | |
6229 | && force_align_functions_log < 1) | |
6230 | force_align_functions_log = 1; | |
6231 | ||
8d08fdba MS |
6232 | /* Initially, C. */ |
6233 | current_lang_name = lang_name_c; | |
6234 | ||
81b3411c BS |
6235 | build_common_tree_nodes (flag_signed_char); |
6236 | ||
8d08fdba MS |
6237 | error_mark_list = build_tree_list (error_mark_node, error_mark_node); |
6238 | TREE_TYPE (error_mark_list) = error_mark_node; | |
6239 | ||
6bcedb4e | 6240 | /* Create the `std' namespace. */ |
1dbb6023 NS |
6241 | push_namespace (std_identifier); |
6242 | std_node = current_namespace; | |
6243 | pop_namespace (); | |
5362b086 | 6244 | |
eaa7c03f | 6245 | c_common_nodes_and_builtins (); |
37c46b43 | 6246 | |
4d8a1dd6 MM |
6247 | java_byte_type_node = record_builtin_java_type ("__java_byte", 8); |
6248 | java_short_type_node = record_builtin_java_type ("__java_short", 16); | |
6249 | java_int_type_node = record_builtin_java_type ("__java_int", 32); | |
6250 | java_long_type_node = record_builtin_java_type ("__java_long", 64); | |
6251 | java_float_type_node = record_builtin_java_type ("__java_float", -32); | |
6252 | java_double_type_node = record_builtin_java_type ("__java_double", -64); | |
6253 | java_char_type_node = record_builtin_java_type ("__java_char", -16); | |
6254 | java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1); | |
eff71ab0 | 6255 | |
8d08fdba MS |
6256 | integer_two_node = build_int_2 (2, 0); |
6257 | TREE_TYPE (integer_two_node) = integer_type_node; | |
6258 | integer_three_node = build_int_2 (3, 0); | |
6259 | TREE_TYPE (integer_three_node) = integer_type_node; | |
8d08fdba | 6260 | |
255512c1 JM |
6261 | boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE); |
6262 | TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE); | |
c1ea6a0b BK |
6263 | TYPE_MAX_VALUE (boolean_type_node) = build_int_2 (1, 0); |
6264 | TREE_TYPE (TYPE_MAX_VALUE (boolean_type_node)) = boolean_type_node; | |
6265 | TYPE_PRECISION (boolean_type_node) = 1; | |
255512c1 JM |
6266 | record_builtin_type (RID_BOOL, "bool", boolean_type_node); |
6267 | boolean_false_node = build_int_2 (0, 0); | |
6268 | TREE_TYPE (boolean_false_node) = boolean_type_node; | |
6269 | boolean_true_node = build_int_2 (1, 0); | |
6270 | TREE_TYPE (boolean_true_node) = boolean_type_node; | |
2986ae00 | 6271 | |
4cc1d462 | 6272 | empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE); |
5fd8e536 JM |
6273 | |
6274 | #if 0 | |
3e411c3f | 6275 | record_builtin_type (RID_MAX, NULL, string_type_node); |
5fd8e536 JM |
6276 | #endif |
6277 | ||
1f84ec23 MM |
6278 | delta_type_node = ptrdiff_type_node; |
6279 | vtable_index_type = ptrdiff_type_node; | |
c7e266a6 | 6280 | |
3ec6bad3 | 6281 | vtt_parm_type = build_pointer_type (const_ptr_type_node); |
10841285 MM |
6282 | void_ftype = build_function_type (void_type_node, void_list_node); |
6283 | void_ftype_ptr = build_function_type (void_type_node, | |
6284 | tree_cons (NULL_TREE, | |
6285 | ptr_type_node, | |
6286 | void_list_node)); | |
824b9a4c | 6287 | void_ftype_ptr |
4cc1d462 | 6288 | = build_exception_variant (void_ftype_ptr, empty_except_spec); |
8d08fdba | 6289 | |
8d08fdba MS |
6290 | /* C++ extensions */ |
6291 | ||
6292 | unknown_type_node = make_node (UNKNOWN_TYPE); | |
036407f7 ML |
6293 | record_unknown_type (unknown_type_node, "unknown type"); |
6294 | ||
8d08fdba MS |
6295 | /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */ |
6296 | TREE_TYPE (unknown_type_node) = unknown_type_node; | |
a6967cc0 | 6297 | |
a6967cc0 JM |
6298 | /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same |
6299 | result. */ | |
8d08fdba MS |
6300 | TYPE_POINTER_TO (unknown_type_node) = unknown_type_node; |
6301 | TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node; | |
6302 | ||
c4372ef4 NS |
6303 | { |
6304 | /* Make sure we get a unique function type, so we can give | |
6305 | its pointer type a name. (This wins for gdb.) */ | |
6306 | tree vfunc_type = make_node (FUNCTION_TYPE); | |
6307 | TREE_TYPE (vfunc_type) = integer_type_node; | |
6308 | TYPE_ARG_TYPES (vfunc_type) = NULL_TREE; | |
6309 | layout_type (vfunc_type); | |
6310 | ||
6311 | vtable_entry_type = build_pointer_type (vfunc_type); | |
6312 | } | |
0e5921e8 | 6313 | record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type); |
8d08fdba | 6314 | |
8d08fdba | 6315 | vtbl_type_node |
52bf7d5d | 6316 | = build_cplus_array_type (vtable_entry_type, NULL_TREE); |
8d08fdba | 6317 | layout_type (vtbl_type_node); |
91063b51 | 6318 | vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST); |
3e411c3f | 6319 | record_builtin_type (RID_MAX, NULL, vtbl_type_node); |
849da744 MM |
6320 | vtbl_ptr_type_node = build_pointer_type (vtable_entry_type); |
6321 | layout_type (vtbl_ptr_type_node); | |
3e411c3f | 6322 | record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node); |
8d08fdba | 6323 | |
1f84ec23 MM |
6324 | push_namespace (get_identifier ("__cxxabiv1")); |
6325 | abi_node = current_namespace; | |
6326 | pop_namespace (); | |
6633d636 | 6327 | |
2c73f9f5 | 6328 | global_type_node = make_node (LANG_TYPE); |
036407f7 | 6329 | record_unknown_type (global_type_node, "global type"); |
2c73f9f5 | 6330 | |
db5ae43f MS |
6331 | /* Now, C++. */ |
6332 | current_lang_name = lang_name_cplusplus; | |
8d08fdba | 6333 | |
ced78d8b | 6334 | { |
2c73f9f5 | 6335 | tree bad_alloc_type_node, newtype, deltype; |
10841285 MM |
6336 | tree ptr_ftype_sizetype; |
6337 | ||
1dbb6023 | 6338 | push_namespace (std_identifier); |
88e5899c MM |
6339 | bad_alloc_type_node |
6340 | = xref_tag (class_type, get_identifier ("bad_alloc"), | |
6341 | /*attributes=*/NULL_TREE, 1); | |
1dbb6023 | 6342 | pop_namespace (); |
10841285 MM |
6343 | ptr_ftype_sizetype |
6344 | = build_function_type (ptr_type_node, | |
6345 | tree_cons (NULL_TREE, | |
c9f8536c | 6346 | size_type_node, |
10841285 | 6347 | void_list_node)); |
2c73f9f5 | 6348 | newtype = build_exception_variant |
0ba8a114 NS |
6349 | (ptr_ftype_sizetype, add_exception_specifier |
6350 | (NULL_TREE, bad_alloc_type_node, -1)); | |
4cc1d462 | 6351 | deltype = build_exception_variant (void_ftype_ptr, empty_except_spec); |
596ea4e5 AS |
6352 | push_cp_library_fn (NEW_EXPR, newtype); |
6353 | push_cp_library_fn (VEC_NEW_EXPR, newtype); | |
6354 | global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype); | |
6355 | push_cp_library_fn (VEC_DELETE_EXPR, deltype); | |
ced78d8b | 6356 | } |
8d08fdba MS |
6357 | |
6358 | abort_fndecl | |
1f84ec23 | 6359 | = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype); |
8d08fdba | 6360 | |
8d08fdba MS |
6361 | /* Perform other language dependent initializations. */ |
6362 | init_class_processing (); | |
8d08fdba | 6363 | init_search_processing (); |
db48b831 | 6364 | init_rtti_processing (); |
8d08fdba | 6365 | |
6467930b | 6366 | if (flag_exceptions) |
8d2733ca | 6367 | init_exception_processing (); |
9e9ff709 | 6368 | |
7fcdf4c2 | 6369 | if (! supports_one_only ()) |
72b7eeff | 6370 | flag_weak = 0; |
8d08fdba | 6371 | |
2ce07e2d | 6372 | make_fname_decl = cp_make_fname_decl; |
0ba8a114 | 6373 | start_fname_decls (); |
8d08fdba | 6374 | |
e9a25f70 JL |
6375 | /* Show we use EH for cleanups. */ |
6376 | using_eh_for_cleanups (); | |
62c154ed | 6377 | |
d9cf7c82 JM |
6378 | /* Maintain consistency. Perhaps we should just complain if they |
6379 | say -fwritable-strings? */ | |
6380 | if (flag_writable_strings) | |
6381 | flag_const_strings = 0; | |
62c154ed JM |
6382 | } |
6383 | ||
0ba8a114 NS |
6384 | /* Generate an initializer for a function naming variable from |
6385 | NAME. NAME may be NULL, in which case we generate a special | |
a1c65f9f | 6386 | ERROR_MARK node which should be replaced later. */ |
0ba8a114 NS |
6387 | |
6388 | tree | |
11f6b451 | 6389 | cp_fname_init (const char* name) |
0ba8a114 NS |
6390 | { |
6391 | tree domain = NULL_TREE; | |
6392 | tree type; | |
6393 | tree init = NULL_TREE; | |
6394 | size_t length = 0; | |
6395 | ||
6396 | if (name) | |
6397 | { | |
6398 | length = strlen (name); | |
6399 | domain = build_index_type (size_int (length)); | |
6400 | init = build_string (length + 1, name); | |
6401 | } | |
6402 | ||
6403 | type = build_qualified_type (char_type_node, TYPE_QUAL_CONST); | |
6404 | type = build_cplus_array_type (type, domain); | |
6405 | ||
6406 | if (init) | |
6407 | TREE_TYPE (init) = type; | |
6408 | else | |
6409 | /* We don't know the value until instantiation time. Make | |
a1c65f9f | 6410 | something which will be digested now, but replaced later. */ |
0ba8a114 NS |
6411 | init = build (ERROR_MARK, type); |
6412 | ||
6413 | return init; | |
6414 | } | |
6415 | ||
2ce07e2d NS |
6416 | /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the |
6417 | decl, NAME is the initialization string and TYPE_DEP indicates whether | |
6418 | NAME depended on the type of the function. We make use of that to detect | |
0ba8a114 NS |
6419 | __PRETTY_FUNCTION__ inside a template fn. This is being done |
6420 | lazily at the point of first use, so we musn't push the decl now. */ | |
2ce07e2d NS |
6421 | |
6422 | static tree | |
11f6b451 | 6423 | cp_make_fname_decl (tree id, int type_dep) |
2ce07e2d | 6424 | { |
83182544 | 6425 | const char *const name = (type_dep && processing_template_decl |
0ba8a114 NS |
6426 | ? NULL : fname_as_string (type_dep)); |
6427 | tree init = cp_fname_init (name); | |
6428 | tree decl = build_decl (VAR_DECL, id, TREE_TYPE (init)); | |
2ce07e2d | 6429 | |
6cce57b0 | 6430 | /* As we're using pushdecl_with_scope, we must set the context. */ |
0ba8a114 NS |
6431 | DECL_CONTEXT (decl) = current_function_decl; |
6432 | DECL_PRETTY_FUNCTION_P (decl) = type_dep; | |
6433 | ||
2ce07e2d NS |
6434 | TREE_STATIC (decl) = 1; |
6435 | TREE_READONLY (decl) = 1; | |
2ce07e2d | 6436 | DECL_ARTIFICIAL (decl) = 1; |
2ce07e2d | 6437 | DECL_INITIAL (decl) = init; |
0ba8a114 NS |
6438 | |
6439 | TREE_USED (decl) = 1; | |
5362b086 | 6440 | |
6cce57b0 JM |
6441 | if (current_function_decl) |
6442 | { | |
6443 | struct cp_binding_level *b = current_binding_level; | |
6444 | while (b->level_chain->parm_flag == 0) | |
6445 | b = b->level_chain; | |
6446 | pushdecl_with_scope (decl, b); | |
6447 | } | |
6448 | ||
0ba8a114 NS |
6449 | cp_finish_decl (decl, init, NULL_TREE, LOOKUP_ONLYCONVERTING); |
6450 | ||
2ce07e2d NS |
6451 | return decl; |
6452 | } | |
6453 | ||
d52e4867 RS |
6454 | /* Make a definition for a builtin function named NAME in the current |
6455 | namespace, whose data type is TYPE and whose context is CONTEXT. | |
6456 | TYPE should be a function type with argument types. | |
8d08fdba | 6457 | |
0c11ada6 JM |
6458 | CLASS and CODE tell later passes how to compile calls to this function. |
6459 | See tree.h for possible values. | |
6460 | ||
6461 | If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME, | |
6a2dd09a RS |
6462 | the name to be called if we can't opencode the function. |
6463 | If ATTRS is nonzero, use that for the function's attribute | |
6464 | list. */ | |
8d08fdba | 6465 | |
d52e4867 | 6466 | static tree |
11f6b451 NN |
6467 | builtin_function_1 (const char* name, |
6468 | tree type, | |
6469 | tree context, | |
6470 | int code, | |
6471 | enum built_in_class class, | |
6472 | const char* libname, | |
6473 | tree attrs) | |
8d08fdba | 6474 | { |
596ea4e5 | 6475 | tree decl = build_library_fn_1 (get_identifier (name), ERROR_MARK, type); |
0c11ada6 JM |
6476 | DECL_BUILT_IN_CLASS (decl) = class; |
6477 | DECL_FUNCTION_CODE (decl) = code; | |
d52e4867 | 6478 | DECL_CONTEXT (decl) = context; |
12a22e76 | 6479 | |
6bcedb4e | 6480 | pushdecl (decl); |
6bcedb4e | 6481 | |
8d08fdba MS |
6482 | /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME, |
6483 | we cannot change DECL_ASSEMBLER_NAME until we have installed this | |
6484 | function in the namespace. */ | |
0c11ada6 | 6485 | if (libname) |
92643fea MM |
6486 | SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname)); |
6487 | make_decl_rtl (decl, NULL); | |
935d1834 ZW |
6488 | |
6489 | /* Warn if a function in the namespace for users | |
6490 | is used without an occasion to consider it declared. */ | |
6491 | if (name[0] != '_' || name[1] != '_') | |
6492 | DECL_ANTICIPATED (decl) = 1; | |
6493 | ||
6431177a | 6494 | /* Possibly apply some default attributes to this built-in function. */ |
6a2dd09a RS |
6495 | if (attrs) |
6496 | decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN); | |
6497 | else | |
6498 | decl_attributes (&decl, NULL_TREE, 0); | |
6431177a | 6499 | |
8d08fdba MS |
6500 | return decl; |
6501 | } | |
7f4edbcb | 6502 | |
d52e4867 RS |
6503 | /* Entry point for the benefit of c_common_nodes_and_builtins. |
6504 | ||
6505 | Make a defintion for a builtin function named NAME and whose data type | |
6506 | is TYPE. TYPE should be a function type with argument types. This | |
6507 | function places the anticipated declaration in the global namespace | |
6508 | and additionally in the std namespace if appropriate. | |
6509 | ||
6510 | CLASS and CODE tell later passes how to compile calls to this function. | |
6511 | See tree.h for possible values. | |
6512 | ||
6513 | If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME, | |
6a2dd09a RS |
6514 | the name to be called if we can't opencode the function. |
6515 | ||
6516 | If ATTRS is nonzero, use that for the function's attribute | |
6517 | list. */ | |
d52e4867 RS |
6518 | |
6519 | tree | |
11f6b451 NN |
6520 | builtin_function (const char* name, |
6521 | tree type, | |
6522 | int code, | |
6523 | enum built_in_class class, | |
6524 | const char* libname, | |
6525 | tree attrs) | |
d52e4867 RS |
6526 | { |
6527 | /* All builtins that don't begin with an '_' should additionally | |
6528 | go in the 'std' namespace. */ | |
6529 | if (name[0] != '_') | |
6530 | { | |
6531 | push_namespace (std_identifier); | |
6a2dd09a | 6532 | builtin_function_1 (name, type, std_node, code, class, libname, attrs); |
d52e4867 RS |
6533 | pop_namespace (); |
6534 | } | |
6535 | ||
6a2dd09a RS |
6536 | return builtin_function_1 (name, type, NULL_TREE, code, |
6537 | class, libname, attrs); | |
d52e4867 RS |
6538 | } |
6539 | ||
0c11ada6 JM |
6540 | /* Generate a FUNCTION_DECL with the typical flags for a runtime library |
6541 | function. Not called directly. */ | |
6542 | ||
6543 | static tree | |
11f6b451 | 6544 | build_library_fn_1 (tree name, enum tree_code operator_code, tree type) |
0c11ada6 JM |
6545 | { |
6546 | tree fn = build_lang_decl (FUNCTION_DECL, name, type); | |
6547 | DECL_EXTERNAL (fn) = 1; | |
6548 | TREE_PUBLIC (fn) = 1; | |
6549 | DECL_ARTIFICIAL (fn) = 1; | |
6550 | TREE_NOTHROW (fn) = 1; | |
596ea4e5 | 6551 | SET_OVERLOADED_OPERATOR_CODE (fn, operator_code); |
5d2ed28c | 6552 | SET_DECL_LANGUAGE (fn, lang_c); |
0c11ada6 JM |
6553 | return fn; |
6554 | } | |
c2a37c55 | 6555 | |
0c11ada6 JM |
6556 | /* Returns the _DECL for a library function with C linkage. |
6557 | We assume that such functions never throw; if this is incorrect, | |
6558 | callers should unset TREE_NOTHROW. */ | |
c2a37c55 | 6559 | |
7f4edbcb | 6560 | tree |
11f6b451 | 6561 | build_library_fn (tree name, tree type) |
0c11ada6 | 6562 | { |
19e7881c | 6563 | return build_library_fn_1 (name, ERROR_MARK, type); |
0c11ada6 JM |
6564 | } |
6565 | ||
6566 | /* Returns the _DECL for a library function with C++ linkage. */ | |
6567 | ||
596ea4e5 | 6568 | static tree |
11f6b451 | 6569 | build_cp_library_fn (tree name, enum tree_code operator_code, tree type) |
0c11ada6 | 6570 | { |
596ea4e5 | 6571 | tree fn = build_library_fn_1 (name, operator_code, type); |
0c11ada6 | 6572 | TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type); |
6bbf1598 | 6573 | DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace); |
5d2ed28c | 6574 | SET_DECL_LANGUAGE (fn, lang_cplusplus); |
0c11ada6 | 6575 | set_mangled_name_for_decl (fn); |
0c11ada6 JM |
6576 | return fn; |
6577 | } | |
6578 | ||
6579 | /* Like build_library_fn, but takes a C string instead of an | |
6580 | IDENTIFIER_NODE. */ | |
6581 | ||
6582 | tree | |
11f6b451 | 6583 | build_library_fn_ptr (const char* name, tree type) |
7f4edbcb | 6584 | { |
0c11ada6 JM |
6585 | return build_library_fn (get_identifier (name), type); |
6586 | } | |
6587 | ||
6588 | /* Like build_cp_library_fn, but takes a C string instead of an | |
6589 | IDENTIFIER_NODE. */ | |
6590 | ||
6591 | tree | |
11f6b451 | 6592 | build_cp_library_fn_ptr (const char* name, tree type) |
0c11ada6 | 6593 | { |
596ea4e5 | 6594 | return build_cp_library_fn (get_identifier (name), ERROR_MARK, type); |
0c11ada6 JM |
6595 | } |
6596 | ||
6597 | /* Like build_library_fn, but also pushes the function so that we will | |
6598 | be able to find it via IDENTIFIER_GLOBAL_VALUE. */ | |
6599 | ||
6600 | tree | |
11f6b451 | 6601 | push_library_fn (tree name, tree type) |
0c11ada6 JM |
6602 | { |
6603 | tree fn = build_library_fn (name, type); | |
6604 | pushdecl_top_level (fn); | |
6605 | return fn; | |
6606 | } | |
6607 | ||
6608 | /* Like build_cp_library_fn, but also pushes the function so that it | |
6609 | will be found by normal lookup. */ | |
6610 | ||
596ea4e5 | 6611 | static tree |
11f6b451 | 6612 | push_cp_library_fn (enum tree_code operator_code, tree type) |
0c11ada6 | 6613 | { |
5362b086 | 6614 | tree fn = build_cp_library_fn (ansi_opname (operator_code), |
596ea4e5 AS |
6615 | operator_code, |
6616 | type); | |
0c11ada6 JM |
6617 | pushdecl (fn); |
6618 | return fn; | |
6619 | } | |
6620 | ||
6621 | /* Like push_library_fn, but takes a TREE_LIST of parm types rather than | |
6622 | a FUNCTION_TYPE. */ | |
6623 | ||
6624 | tree | |
11f6b451 | 6625 | push_void_library_fn (tree name, tree parmtypes) |
0c11ada6 JM |
6626 | { |
6627 | tree type = build_function_type (void_type_node, parmtypes); | |
6628 | return push_library_fn (name, type); | |
6629 | } | |
6630 | ||
cf74fb86 | 6631 | /* Like push_library_fn, but also note that this function throws |
0c11ada6 JM |
6632 | and does not return. Used for __throw_foo and the like. */ |
6633 | ||
6634 | tree | |
11f6b451 | 6635 | push_throw_library_fn (tree name, tree type) |
0c11ada6 | 6636 | { |
cf74fb86 | 6637 | tree fn = push_library_fn (name, type); |
0c11ada6 JM |
6638 | TREE_THIS_VOLATILE (fn) = 1; |
6639 | TREE_NOTHROW (fn) = 0; | |
6640 | return fn; | |
7f4edbcb | 6641 | } |
6431177a JM |
6642 | |
6643 | /* Apply default attributes to a function, if a system function with default | |
6644 | attributes. */ | |
6645 | ||
6646 | void | |
11f6b451 | 6647 | cxx_insert_default_attributes (tree decl) |
6431177a JM |
6648 | { |
6649 | if (!DECL_EXTERN_C_FUNCTION_P (decl)) | |
6650 | return; | |
6651 | if (!TREE_PUBLIC (decl)) | |
6652 | return; | |
6653 | c_common_insert_default_attributes (decl); | |
6654 | } | |
8d08fdba | 6655 | \f |
61a127b3 MM |
6656 | /* When we call finish_struct for an anonymous union, we create |
6657 | default copy constructors and such. But, an anonymous union | |
6658 | shouldn't have such things; this function undoes the damage to the | |
6659 | anonymous union type T. | |
6660 | ||
6661 | (The reason that we create the synthesized methods is that we don't | |
6662 | distinguish `union { int i; }' from `typedef union { int i; } U'. | |
6663 | The first is an anonymous union; the second is just an ordinary | |
6664 | union type.) */ | |
6665 | ||
6666 | void | |
11f6b451 | 6667 | fixup_anonymous_aggr (tree t) |
61a127b3 MM |
6668 | { |
6669 | tree *q; | |
6670 | ||
6671 | /* Wipe out memory of synthesized methods */ | |
6672 | TYPE_HAS_CONSTRUCTOR (t) = 0; | |
6673 | TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0; | |
6674 | TYPE_HAS_INIT_REF (t) = 0; | |
6675 | TYPE_HAS_CONST_INIT_REF (t) = 0; | |
6676 | TYPE_HAS_ASSIGN_REF (t) = 0; | |
61a127b3 MM |
6677 | TYPE_HAS_CONST_ASSIGN_REF (t) = 0; |
6678 | ||
6679 | /* Splice the implicitly generated functions out of the TYPE_METHODS | |
6680 | list. */ | |
6681 | q = &TYPE_METHODS (t); | |
6682 | while (*q) | |
6683 | { | |
6684 | if (DECL_ARTIFICIAL (*q)) | |
6685 | *q = TREE_CHAIN (*q); | |
6686 | else | |
6687 | q = &TREE_CHAIN (*q); | |
6688 | } | |
6689 | ||
cab1f180 | 6690 | /* ISO C++ 9.5.3. Anonymous unions may not have function members. */ |
61a127b3 | 6691 | if (TYPE_METHODS (t)) |
1f0d71c5 | 6692 | cp_error_at ("an anonymous union cannot have function members", t); |
a1c2b86d JJ |
6693 | |
6694 | /* Anonymous aggregates cannot have fields with ctors, dtors or complex | |
6695 | assignment operators (because they cannot have these methods themselves). | |
6696 | For anonymous unions this is already checked because they are not allowed | |
6697 | in any union, otherwise we have to check it. */ | |
6698 | if (TREE_CODE (t) != UNION_TYPE) | |
6699 | { | |
6700 | tree field, type; | |
6701 | ||
6702 | for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field)) | |
6703 | if (TREE_CODE (field) == FIELD_DECL) | |
6704 | { | |
6705 | type = TREE_TYPE (field); | |
6706 | if (CLASS_TYPE_P (type)) | |
6707 | { | |
6708 | if (TYPE_NEEDS_CONSTRUCTING (type)) | |
6709 | cp_error_at ("member %#D' with constructor not allowed in anonymous aggregate", | |
6710 | field); | |
6711 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) | |
6712 | cp_error_at ("member %#D' with destructor not allowed in anonymous aggregate", | |
6713 | field); | |
6714 | if (TYPE_HAS_COMPLEX_ASSIGN_REF (type)) | |
6715 | cp_error_at ("member %#D' with copy assignment operator not allowed in anonymous aggregate", | |
6716 | field); | |
6717 | } | |
6718 | } | |
6719 | } | |
61a127b3 MM |
6720 | } |
6721 | ||
72a93143 | 6722 | /* Make sure that a declaration with no declarator is well-formed, i.e. |
a723baf1 | 6723 | just declares a tagged type or anonymous union. |
8d08fdba | 6724 | |
a723baf1 | 6725 | Returns the type declared; or NULL_TREE if none. */ |
8d08fdba | 6726 | |
72a93143 | 6727 | tree |
11f6b451 | 6728 | check_tag_decl (tree declspecs) |
8d08fdba | 6729 | { |
72a93143 | 6730 | int found_type = 0; |
2bdb0643 | 6731 | int saw_friend = 0; |
e8186ecf | 6732 | int saw_typedef = 0; |
2986ae00 | 6733 | tree ob_modifier = NULL_TREE; |
8d08fdba | 6734 | register tree link; |
a723baf1 MM |
6735 | /* If a class, struct, or enum type is declared by the DECLSPECS |
6736 | (i.e, if a class-specifier, enum-specifier, or non-typename | |
6737 | elaborated-type-specifier appears in the DECLSPECS), | |
6738 | DECLARED_TYPE is set to the corresponding type. */ | |
6739 | tree declared_type = NULL_TREE; | |
6740 | bool error_p = false; | |
8d08fdba MS |
6741 | |
6742 | for (link = declspecs; link; link = TREE_CHAIN (link)) | |
6743 | { | |
a723baf1 | 6744 | tree value = TREE_VALUE (link); |
8d08fdba | 6745 | |
2bdb0643 | 6746 | if (TYPE_P (value) |
bd0d5d4a | 6747 | || TREE_CODE (value) == TYPE_DECL |
2bdb0643 JM |
6748 | || (TREE_CODE (value) == IDENTIFIER_NODE |
6749 | && IDENTIFIER_GLOBAL_VALUE (value) | |
bd0d5d4a | 6750 | && TREE_CODE (IDENTIFIER_GLOBAL_VALUE (value)) == TYPE_DECL)) |
8d08fdba | 6751 | { |
72a93143 | 6752 | ++found_type; |
5566b478 | 6753 | |
bd0d5d4a JM |
6754 | if (found_type == 2 && TREE_CODE (value) == IDENTIFIER_NODE) |
6755 | { | |
6756 | if (! in_system_header) | |
33bd39a2 | 6757 | pedwarn ("redeclaration of C++ built-in type `%T'", value); |
bd0d5d4a JM |
6758 | return NULL_TREE; |
6759 | } | |
6760 | ||
6761 | if (TYPE_P (value) | |
6762 | && ((TREE_CODE (value) != TYPENAME_TYPE && IS_AGGR_TYPE (value)) | |
6763 | || TREE_CODE (value) == ENUMERAL_TYPE)) | |
72a93143 JM |
6764 | { |
6765 | my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261); | |
a723baf1 | 6766 | declared_type = value; |
72a93143 | 6767 | } |
8d08fdba | 6768 | } |
e8186ecf GDR |
6769 | else if (value == ridpointers[(int) RID_TYPEDEF]) |
6770 | saw_typedef = 1; | |
83f660b7 JM |
6771 | else if (value == ridpointers[(int) RID_FRIEND]) |
6772 | { | |
83f660b7 JM |
6773 | if (current_class_type == NULL_TREE |
6774 | || current_scope () != current_class_type) | |
6775 | ob_modifier = value; | |
2bdb0643 JM |
6776 | else |
6777 | saw_friend = 1; | |
83f660b7 | 6778 | } |
8d08fdba | 6779 | else if (value == ridpointers[(int) RID_STATIC] |
2986ae00 MS |
6780 | || value == ridpointers[(int) RID_EXTERN] |
6781 | || value == ridpointers[(int) RID_AUTO] | |
28cbf42c MS |
6782 | || value == ridpointers[(int) RID_REGISTER] |
6783 | || value == ridpointers[(int) RID_INLINE] | |
6784 | || value == ridpointers[(int) RID_VIRTUAL] | |
72a93143 JM |
6785 | || value == ridpointers[(int) RID_CONST] |
6786 | || value == ridpointers[(int) RID_VOLATILE] | |
7a1f3f5f RH |
6787 | || value == ridpointers[(int) RID_EXPLICIT] |
6788 | || value == ridpointers[(int) RID_THREAD]) | |
2986ae00 | 6789 | ob_modifier = value; |
a723baf1 MM |
6790 | else if (value == error_mark_node) |
6791 | error_p = true; | |
8d08fdba MS |
6792 | } |
6793 | ||
72a93143 JM |
6794 | if (found_type > 1) |
6795 | error ("multiple types in one declaration"); | |
7e2067ca | 6796 | |
a723baf1 | 6797 | if (declared_type == NULL_TREE && ! saw_friend && !error_p) |
7e2067ca | 6798 | pedwarn ("declaration does not declare anything"); |
1951a1b6 | 6799 | /* Check for an anonymous union. */ |
a723baf1 MM |
6800 | else if (declared_type && IS_AGGR_TYPE_CODE (TREE_CODE (declared_type)) |
6801 | && TYPE_ANONYMOUS_P (declared_type)) | |
0dd3962d | 6802 | { |
e8186ecf GDR |
6803 | /* 7/3 In a simple-declaration, the optional init-declarator-list |
6804 | can be omitted only when declaring a class (clause 9) or | |
6805 | enumeration (7.2), that is, when the decl-specifier-seq contains | |
6806 | either a class-specifier, an elaborated-type-specifier with | |
6807 | a class-key (9.1), or an enum-specifier. In these cases and | |
6808 | whenever a class-specifier or enum-specifier is present in the | |
6809 | decl-specifier-seq, the identifiers in these specifiers are among | |
6810 | the names being declared by the declaration (as class-name, | |
6811 | enum-names, or enumerators, depending on the syntax). In such | |
6812 | cases, and except for the declaration of an unnamed bit-field (9.6), | |
6813 | the decl-specifier-seq shall introduce one or more names into the | |
6814 | program, or shall redeclare a name introduced by a previous | |
6815 | declaration. [Example: | |
6816 | enum { }; // ill-formed | |
6817 | typedef class { }; // ill-formed | |
6818 | --end example] */ | |
6819 | if (saw_typedef) | |
6820 | { | |
357351e5 | 6821 | error ("missing type-name in typedef-declaration"); |
e8186ecf GDR |
6822 | return NULL_TREE; |
6823 | } | |
0dd3962d | 6824 | /* Anonymous unions are objects, so they can have specifiers. */; |
a723baf1 | 6825 | SET_ANON_AGGR_TYPE_P (declared_type); |
6bdb8141 | 6826 | |
a723baf1 MM |
6827 | if (TREE_CODE (declared_type) != UNION_TYPE && pedantic |
6828 | && !in_system_header) | |
6bdb8141 | 6829 | pedwarn ("ISO C++ prohibits anonymous structs"); |
0dd3962d JM |
6830 | } |
6831 | ||
83f660b7 | 6832 | else if (ob_modifier) |
8d08fdba | 6833 | { |
83f660b7 JM |
6834 | if (ob_modifier == ridpointers[(int) RID_INLINE] |
6835 | || ob_modifier == ridpointers[(int) RID_VIRTUAL]) | |
33bd39a2 | 6836 | error ("`%D' can only be specified for functions", ob_modifier); |
83f660b7 | 6837 | else if (ob_modifier == ridpointers[(int) RID_FRIEND]) |
33bd39a2 | 6838 | error ("`%D' can only be specified inside a class", ob_modifier); |
83f660b7 | 6839 | else if (ob_modifier == ridpointers[(int) RID_EXPLICIT]) |
33bd39a2 | 6840 | error ("`%D' can only be specified for constructors", |
83f660b7 JM |
6841 | ob_modifier); |
6842 | else | |
33bd39a2 | 6843 | error ("`%D' can only be specified for objects and functions", |
83f660b7 | 6844 | ob_modifier); |
72a93143 | 6845 | } |
8d08fdba | 6846 | |
a723baf1 | 6847 | return declared_type; |
72a93143 JM |
6848 | } |
6849 | ||
6850 | /* Called when a declaration is seen that contains no names to declare. | |
6851 | If its type is a reference to a structure, union or enum inherited | |
6852 | from a containing scope, shadow that tag name for the current scope | |
6853 | with a forward reference. | |
6854 | If its type defines a new named structure or union | |
6855 | or defines an enum, it is valid but we need not do anything here. | |
6856 | Otherwise, it is an error. | |
6857 | ||
6858 | C++: may have to grok the declspecs to learn about static, | |
a723baf1 | 6859 | complain for anonymous unions. |
72a93143 | 6860 | |
a723baf1 MM |
6861 | Returns the TYPE declared -- or NULL_TREE if none. */ |
6862 | ||
6863 | tree | |
11f6b451 | 6864 | shadow_tag (tree declspecs) |
72a93143 JM |
6865 | { |
6866 | tree t = check_tag_decl (declspecs); | |
6867 | ||
a723baf1 MM |
6868 | if (!t) |
6869 | return NULL_TREE; | |
6870 | ||
6871 | maybe_process_partial_specialization (t); | |
72a93143 JM |
6872 | |
6873 | /* This is where the variables in an anonymous union are | |
6874 | declared. An anonymous union declaration looks like: | |
6875 | union { ... } ; | |
6876 | because there is no declarator after the union, the parser | |
6877 | sends that declaration here. */ | |
a723baf1 | 6878 | if (ANON_AGGR_TYPE_P (t)) |
72a93143 | 6879 | { |
6bdb8141 | 6880 | fixup_anonymous_aggr (t); |
72a93143 JM |
6881 | |
6882 | if (TYPE_FIELDS (t)) | |
6883 | { | |
6884 | tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0, | |
91d231cb | 6885 | NULL); |
72a93143 JM |
6886 | finish_anon_union (decl); |
6887 | } | |
8d08fdba | 6888 | } |
a723baf1 MM |
6889 | |
6890 | return t; | |
8d08fdba MS |
6891 | } |
6892 | \f | |
6893 | /* Decode a "typename", such as "int **", returning a ..._TYPE node. */ | |
6894 | ||
6895 | tree | |
11f6b451 | 6896 | groktypename (tree typename) |
8d08fdba | 6897 | { |
98884b26 JM |
6898 | tree specs, attrs; |
6899 | tree type; | |
8d08fdba MS |
6900 | if (TREE_CODE (typename) != TREE_LIST) |
6901 | return typename; | |
98884b26 JM |
6902 | split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs); |
6903 | type = grokdeclarator (TREE_VALUE (typename), specs, | |
6904 | TYPENAME, 0, &attrs); | |
6905 | if (attrs) | |
6906 | cplus_decl_attributes (&type, attrs, 0); | |
6907 | return type; | |
8d08fdba MS |
6908 | } |
6909 | ||
6910 | /* Decode a declarator in an ordinary declaration or data definition. | |
6911 | This is called as soon as the type information and variable name | |
6912 | have been parsed, before parsing the initializer if any. | |
6913 | Here we create the ..._DECL node, fill in its type, | |
6914 | and put it on the list of decls for the current context. | |
6915 | The ..._DECL node is returned as the value. | |
6916 | ||
6917 | Exception: for arrays where the length is not specified, | |
82580166 | 6918 | the type is left null, to be filled in by `cp_finish_decl'. |
8d08fdba MS |
6919 | |
6920 | Function definitions do not come here; they go to start_function | |
6921 | instead. However, external and forward declarations of functions | |
6922 | do go through here. Structure field declarations are done by | |
6923 | grokfield and not through here. */ | |
6924 | ||
8d08fdba | 6925 | tree |
11f6b451 NN |
6926 | start_decl (tree declarator, |
6927 | tree declspecs, | |
6928 | int initialized, | |
6929 | tree attributes, | |
6930 | tree prefix_attributes) | |
8d08fdba | 6931 | { |
59387d2e | 6932 | tree decl; |
8d08fdba MS |
6933 | register tree type, tem; |
6934 | tree context; | |
8d08fdba | 6935 | |
e92cc029 | 6936 | /* This should only be done once on the top most decl. */ |
594bb0e7 | 6937 | if (have_extern_spec) |
8d08fdba | 6938 | { |
1f8f4a0b MM |
6939 | declspecs = tree_cons (NULL_TREE, get_identifier ("extern"), |
6940 | declspecs); | |
594bb0e7 | 6941 | have_extern_spec = false; |
8d08fdba MS |
6942 | } |
6943 | ||
e23bd218 IR |
6944 | /* An object declared as __attribute__((deprecated)) suppresses |
6945 | warnings of uses of other deprecated items. */ | |
6946 | if (lookup_attribute ("deprecated", attributes)) | |
6947 | deprecated_state = DEPRECATED_SUPPRESS; | |
6948 | ||
91d231cb | 6949 | attributes = chainon (attributes, prefix_attributes); |
b17e2870 | 6950 | |
c11b6f21 | 6951 | decl = grokdeclarator (declarator, declspecs, NORMAL, initialized, |
91d231cb | 6952 | &attributes); |
68642fb6 | 6953 | |
e23bd218 IR |
6954 | deprecated_state = DEPRECATED_NORMAL; |
6955 | ||
a1774733 | 6956 | if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE) |
8d08fdba MS |
6957 | return NULL_TREE; |
6958 | ||
6959 | type = TREE_TYPE (decl); | |
6960 | ||
44689c12 ML |
6961 | if (type == error_mark_node) |
6962 | return NULL_TREE; | |
6963 | ||
4f1c5b7d | 6964 | context = DECL_CONTEXT (decl); |
8d08fdba | 6965 | |
9a68c51f JM |
6966 | if (initialized && context && TREE_CODE (context) == NAMESPACE_DECL |
6967 | && context != current_namespace && TREE_CODE (decl) == VAR_DECL) | |
6968 | { | |
6969 | /* When parsing the initializer, lookup should use the object's | |
a1c65f9f | 6970 | namespace. */ |
9a68c51f JM |
6971 | push_decl_namespace (context); |
6972 | } | |
6973 | ||
a1c65f9f | 6974 | /* We are only interested in class contexts, later. */ |
2c73f9f5 ML |
6975 | if (context && TREE_CODE (context) == NAMESPACE_DECL) |
6976 | context = NULL_TREE; | |
6977 | ||
8d08fdba MS |
6978 | if (initialized) |
6979 | /* Is it valid for this decl to have an initializer at all? | |
6980 | If not, set INITIALIZED to zero, which will indirectly | |
82580166 | 6981 | tell `cp_finish_decl' to ignore the initializer once it is parsed. */ |
8d08fdba MS |
6982 | switch (TREE_CODE (decl)) |
6983 | { | |
6984 | case TYPE_DECL: | |
4a7510cb | 6985 | error ("typedef `%D' is initialized (use __typeof__ instead)", decl); |
95f79357 | 6986 | initialized = 0; |
8d08fdba MS |
6987 | break; |
6988 | ||
6989 | case FUNCTION_DECL: | |
33bd39a2 | 6990 | error ("function `%#D' is initialized like a variable", decl); |
8d08fdba MS |
6991 | initialized = 0; |
6992 | break; | |
6993 | ||
6994 | default: | |
3e41d13b | 6995 | break; |
8d08fdba MS |
6996 | } |
6997 | ||
8d08fdba MS |
6998 | if (initialized) |
6999 | { | |
a9aedbc2 | 7000 | if (! toplevel_bindings_p () |
8d08fdba | 7001 | && DECL_EXTERNAL (decl)) |
33bd39a2 | 7002 | warning ("declaration of `%#D' has `extern' and is initialized", |
8d08fdba MS |
7003 | decl); |
7004 | DECL_EXTERNAL (decl) = 0; | |
5566b478 | 7005 | if (toplevel_bindings_p ()) |
8d08fdba MS |
7006 | TREE_STATIC (decl) = 1; |
7007 | ||
7008 | /* Tell `pushdecl' this is an initialized decl | |
7009 | even though we don't yet have the initializer expression. | |
82580166 | 7010 | Also tell `cp_finish_decl' it may store the real initializer. */ |
8d08fdba MS |
7011 | DECL_INITIAL (decl) = error_mark_node; |
7012 | } | |
7013 | ||
fa20888b | 7014 | /* Set attributes here so if duplicate decl, will have proper attributes. */ |
91d231cb | 7015 | cplus_decl_attributes (&decl, attributes, 0); |
fa20888b | 7016 | |
ecb0eece | 7017 | /* If #pragma weak was used, mark the decl weak now. */ |
5f52c0e0 | 7018 | if (global_scope_p (current_binding_level)) |
ecb0eece RH |
7019 | maybe_apply_pragma_weak (decl); |
7020 | ||
97055d5c AO |
7021 | if (TREE_CODE (decl) == FUNCTION_DECL |
7022 | && DECL_DECLARED_INLINE_P (decl) | |
7023 | && DECL_UNINLINABLE (decl) | |
7024 | && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl))) | |
456d8864 GDR |
7025 | warning ("%Hinline function '%D' given attribute noinline", |
7026 | &DECL_SOURCE_LOCATION (decl), decl); | |
97055d5c | 7027 | |
d0f062fb | 7028 | if (context && COMPLETE_TYPE_P (complete_type (context))) |
5b605f68 | 7029 | { |
14d22dd6 | 7030 | push_nested_class (context); |
e97e5263 | 7031 | |
5b605f68 MS |
7032 | if (TREE_CODE (decl) == VAR_DECL) |
7033 | { | |
86ac0575 | 7034 | tree field = lookup_field (context, DECL_NAME (decl), 0, false); |
5b605f68 | 7035 | if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL) |
33bd39a2 | 7036 | error ("`%#D' is not a static member of `%#T'", decl, context); |
e349ee73 MS |
7037 | else |
7038 | { | |
7039 | if (DECL_CONTEXT (field) != context) | |
f2d773a2 | 7040 | { |
a723baf1 MM |
7041 | if (!same_type_p (DECL_CONTEXT (field), context)) |
7042 | pedwarn ("ISO C++ does not permit `%T::%D' to be defined as `%T::%D'", | |
7043 | DECL_CONTEXT (field), DECL_NAME (decl), | |
7044 | context, DECL_NAME (decl)); | |
f2d773a2 JM |
7045 | DECL_CONTEXT (decl) = DECL_CONTEXT (field); |
7046 | } | |
75650646 MM |
7047 | /* Static data member are tricky; an in-class initialization |
7048 | still doesn't provide a definition, so the in-class | |
7049 | declaration will have DECL_EXTERNAL set, but will have an | |
7050 | initialization. Thus, duplicate_decls won't warn | |
7051 | about this situation, and so we check here. */ | |
7052 | if (DECL_INITIAL (decl) && DECL_INITIAL (field)) | |
33bd39a2 | 7053 | error ("duplicate initialization of %D", decl); |
e349ee73 MS |
7054 | if (duplicate_decls (decl, field)) |
7055 | decl = field; | |
7056 | } | |
5b605f68 | 7057 | } |
f30432d7 MS |
7058 | else |
7059 | { | |
5566b478 | 7060 | tree field = check_classfn (context, decl); |
f30432d7 MS |
7061 | if (field && duplicate_decls (decl, field)) |
7062 | decl = field; | |
7063 | } | |
7064 | ||
7065 | /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */ | |
dff3e828 | 7066 | DECL_IN_AGGR_P (decl) = 0; |
68642fb6 | 7067 | if ((DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl)) |
1f6e1acc | 7068 | || CLASSTYPE_TEMPLATE_INSTANTIATION (context)) |
84e6233f JM |
7069 | { |
7070 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
7071 | /* [temp.expl.spec] An explicit specialization of a static data | |
7072 | member of a template is a definition if the declaration | |
7073 | includes an initializer; otherwise, it is a declaration. | |
7074 | ||
7075 | We check for processing_specialization so this only applies | |
7076 | to the new specialization syntax. */ | |
7077 | if (DECL_INITIAL (decl) == NULL_TREE && processing_specialization) | |
7078 | DECL_EXTERNAL (decl) = 1; | |
7079 | } | |
f30432d7 | 7080 | |
b7698cf0 | 7081 | if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)) |
33bd39a2 | 7082 | pedwarn ("declaration of `%#D' outside of class is not definition", |
f30432d7 | 7083 | decl); |
5b605f68 MS |
7084 | } |
7085 | ||
9188c363 MM |
7086 | /* Enter this declaration into the symbol table. */ |
7087 | tem = maybe_push_decl (decl); | |
2ee887f2 | 7088 | |
5156628f | 7089 | if (processing_template_decl) |
cd9f6678 | 7090 | tem = push_template_decl (tem); |
5566b478 | 7091 | |
2ee887f2 | 7092 | #if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS) |
a3203465 | 7093 | /* Tell the back-end to use or not use .common as appropriate. If we say |
a50f0918 MS |
7094 | -fconserve-space, we want this to save .data space, at the expense of |
7095 | wrong semantics. If we say -fno-conserve-space, we want this to | |
7096 | produce errors about redefs; to do this we force variables into the | |
7097 | data segment. */ | |
ca5dd8b7 JJ |
7098 | DECL_COMMON (tem) = ((TREE_CODE (tem) != VAR_DECL |
7099 | || !DECL_THREAD_LOCAL (tem)) | |
c456a45a | 7100 | && (flag_conserve_space || ! TREE_PUBLIC (tem))); |
2ee887f2 | 7101 | #endif |
68642fb6 | 7102 | |
5156628f | 7103 | if (! processing_template_decl) |
5566b478 | 7104 | start_decl_1 (tem); |
8d08fdba | 7105 | |
8d08fdba MS |
7106 | return tem; |
7107 | } | |
7108 | ||
5566b478 | 7109 | void |
11f6b451 | 7110 | start_decl_1 (tree decl) |
8d08fdba | 7111 | { |
5566b478 MS |
7112 | tree type = TREE_TYPE (decl); |
7113 | int initialized = (DECL_INITIAL (decl) != NULL_TREE); | |
8d08fdba | 7114 | |
44689c12 ML |
7115 | if (type == error_mark_node) |
7116 | return; | |
7117 | ||
bd0d5d4a | 7118 | maybe_push_cleanup_level (type); |
5566b478 MS |
7119 | |
7120 | if (initialized) | |
7121 | /* Is it valid for this decl to have an initializer at all? | |
7122 | If not, set INITIALIZED to zero, which will indirectly | |
7123 | tell `cp_finish_decl' to ignore the initializer once it is parsed. */ | |
8d08fdba | 7124 | { |
5566b478 MS |
7125 | /* Don't allow initializations for incomplete types except for |
7126 | arrays which might be completed by the initialization. */ | |
d0f062fb | 7127 | if (COMPLETE_TYPE_P (complete_type (type))) |
5566b478 MS |
7128 | ; /* A complete type is ok. */ |
7129 | else if (TREE_CODE (type) != ARRAY_TYPE) | |
8d08fdba | 7130 | { |
33bd39a2 | 7131 | error ("variable `%#D' has initializer but incomplete type", |
5566b478 MS |
7132 | decl); |
7133 | initialized = 0; | |
25eb19ff | 7134 | type = TREE_TYPE (decl) = error_mark_node; |
5566b478 | 7135 | } |
d0f062fb | 7136 | else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type)))) |
5566b478 MS |
7137 | { |
7138 | if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)) | |
33bd39a2 | 7139 | error ("elements of array `%#D' have incomplete type", decl); |
5566b478 MS |
7140 | /* else we already gave an error in start_decl. */ |
7141 | initialized = 0; | |
8d08fdba | 7142 | } |
8d08fdba MS |
7143 | } |
7144 | ||
5566b478 MS |
7145 | if (!initialized |
7146 | && TREE_CODE (decl) != TYPE_DECL | |
7147 | && TREE_CODE (decl) != TEMPLATE_DECL | |
07c88314 | 7148 | && type != error_mark_node |
5362b086 | 7149 | && IS_AGGR_TYPE (type) |
07c88314 | 7150 | && ! DECL_EXTERNAL (decl)) |
8d08fdba | 7151 | { |
5156628f | 7152 | if ((! processing_template_decl || ! uses_template_parms (type)) |
d0f062fb | 7153 | && !COMPLETE_TYPE_P (complete_type (type))) |
5566b478 | 7154 | { |
0154eaa8 | 7155 | error ("aggregate `%#D' has incomplete type and cannot be defined", |
5566b478 MS |
7156 | decl); |
7157 | /* Change the type so that assemble_variable will give | |
7158 | DECL an rtl we can live with: (mem (const_int 0)). */ | |
25eb19ff | 7159 | type = TREE_TYPE (decl) = error_mark_node; |
5566b478 MS |
7160 | } |
7161 | else | |
7162 | { | |
7163 | /* If any base type in the hierarchy of TYPE needs a constructor, | |
7164 | then we set initialized to 1. This way any nodes which are | |
7165 | created for the purposes of initializing this aggregate | |
7166 | will live as long as it does. This is necessary for global | |
7167 | aggregates which do not have their initializers processed until | |
7168 | the end of the file. */ | |
7169 | initialized = TYPE_NEEDS_CONSTRUCTING (type); | |
7170 | } | |
7171 | } | |
7172 | ||
5566b478 MS |
7173 | if (! initialized) |
7174 | DECL_INITIAL (decl) = NULL_TREE; | |
7175 | } | |
7176 | ||
7177 | /* Handle initialization of references. | |
38e01259 | 7178 | These three arguments are from `cp_finish_decl', and have the |
e92cc029 MS |
7179 | same meaning here that they do there. |
7180 | ||
7181 | Quotes on semantics can be found in ARM 8.4.3. */ | |
7182 | ||
8e4ce833 | 7183 | static tree |
11f6b451 | 7184 | grok_reference_init (tree decl, tree type, tree init) |
5566b478 MS |
7185 | { |
7186 | tree tmp; | |
7187 | ||
7188 | if (init == NULL_TREE) | |
7189 | { | |
7190 | if ((DECL_LANG_SPECIFIC (decl) == 0 | |
7191 | || DECL_IN_AGGR_P (decl) == 0) | |
7192 | && ! DECL_THIS_EXTERN (decl)) | |
33bd39a2 | 7193 | error ("`%D' declared as reference but not initialized", decl); |
8e4ce833 | 7194 | return NULL_TREE; |
5566b478 MS |
7195 | } |
7196 | ||
ed5511d9 | 7197 | if (TREE_CODE (init) == CONSTRUCTOR) |
5566b478 | 7198 | { |
33bd39a2 | 7199 | error ("ISO C++ forbids use of initializer list to initialize reference `%D'", decl); |
8e4ce833 | 7200 | return NULL_TREE; |
8d08fdba MS |
7201 | } |
7202 | ||
7203 | if (TREE_CODE (init) == TREE_LIST) | |
7204 | init = build_compound_expr (init); | |
8d08fdba | 7205 | |
8ccc31eb MS |
7206 | if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE) |
7207 | init = convert_from_reference (init); | |
7208 | ||
8d08fdba MS |
7209 | if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE |
7210 | && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE) | |
7211 | { | |
a3203465 | 7212 | /* Note: default conversion is only called in very special cases. */ |
8d08fdba MS |
7213 | init = default_conversion (init); |
7214 | } | |
68642fb6 | 7215 | |
24bef158 MM |
7216 | /* Convert INIT to the reference type TYPE. This may involve the |
7217 | creation of a temporary, whose lifetime must be the same as that | |
7218 | of the reference. If so, a DECL_STMT for the temporary will be | |
7219 | added just after the DECL_STMT for DECL. That's why we don't set | |
7220 | DECL_INITIAL for local references (instead assigning to them | |
7221 | explicitly); we need to allow the temporary to be initialized | |
7222 | first. */ | |
7993382e | 7223 | tmp = initialize_reference (type, init, decl); |
8d08fdba | 7224 | |
a3203465 | 7225 | if (tmp == error_mark_node) |
8e4ce833 JJ |
7226 | return NULL_TREE; |
7227 | else if (tmp == NULL_TREE) | |
8d08fdba | 7228 | { |
33bd39a2 | 7229 | error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init)); |
8e4ce833 | 7230 | return NULL_TREE; |
8d08fdba | 7231 | } |
8d08fdba | 7232 | |
8e4ce833 JJ |
7233 | if (TREE_STATIC (decl) && !TREE_CONSTANT (tmp)) |
7234 | return tmp; | |
7235 | ||
08ac397c | 7236 | DECL_INITIAL (decl) = tmp; |
8e4ce833 JJ |
7237 | |
7238 | return NULL_TREE; | |
8d08fdba MS |
7239 | } |
7240 | ||
27778b73 MM |
7241 | /* When parsing `int a[] = {1, 2};' we don't know the size of the |
7242 | array until we finish parsing the initializer. If that's the | |
7243 | situation we're in, update DECL accordingly. */ | |
7244 | ||
7245 | static void | |
11f6b451 | 7246 | maybe_deduce_size_from_array_init (tree decl, tree init) |
27778b73 MM |
7247 | { |
7248 | tree type = TREE_TYPE (decl); | |
7249 | ||
7250 | if (TREE_CODE (type) == ARRAY_TYPE | |
7251 | && TYPE_DOMAIN (type) == NULL_TREE | |
7252 | && TREE_CODE (decl) != TYPE_DECL) | |
7253 | { | |
f2ae0c45 JM |
7254 | /* do_default is really a C-ism to deal with tentative definitions. |
7255 | But let's leave it here to ease the eventual merge. */ | |
7256 | int do_default = !DECL_EXTERNAL (decl); | |
27778b73 MM |
7257 | tree initializer = init ? init : DECL_INITIAL (decl); |
7258 | int failure = complete_array_type (type, initializer, do_default); | |
7259 | ||
7260 | if (failure == 1) | |
33bd39a2 | 7261 | error ("initializer fails to determine size of `%D'", decl); |
27778b73 MM |
7262 | |
7263 | if (failure == 2) | |
7264 | { | |
7265 | if (do_default) | |
33bd39a2 | 7266 | error ("array size missing in `%D'", decl); |
27778b73 MM |
7267 | /* If a `static' var's size isn't known, make it extern as |
7268 | well as static, so it does not get allocated. If it's not | |
7269 | `static', then don't mark it extern; finish_incomplete_decl | |
7270 | will give it a default size and it will get allocated. */ | |
7271 | else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl)) | |
7272 | DECL_EXTERNAL (decl) = 1; | |
7273 | } | |
7274 | ||
7275 | if (pedantic && TYPE_DOMAIN (type) != NULL_TREE | |
7276 | && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)), | |
7277 | integer_zero_node)) | |
33bd39a2 | 7278 | error ("zero-size array `%D'", decl); |
27778b73 MM |
7279 | |
7280 | layout_decl (decl, 0); | |
7281 | } | |
7282 | } | |
7283 | ||
7284 | /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue | |
57b52417 | 7285 | any appropriate error messages regarding the layout. */ |
27778b73 | 7286 | |
57b52417 | 7287 | static void |
11f6b451 | 7288 | layout_var_decl (tree decl) |
27778b73 | 7289 | { |
57b52417 | 7290 | tree type = TREE_TYPE (decl); |
c95cd22e | 7291 | #if 0 |
57b52417 | 7292 | tree ttype = target_type (type); |
c95cd22e | 7293 | #endif |
57b52417 MM |
7294 | |
7295 | /* If we haven't already layed out this declaration, do so now. | |
7296 | Note that we must not call complete type for an external object | |
7297 | because it's type might involve templates that we are not | |
0e339752 | 7298 | supposed to isntantiate yet. (And it's perfectly valid to say |
57b52417 MM |
7299 | `extern X x' for some incomplete type `X'.) */ |
7300 | if (!DECL_EXTERNAL (decl)) | |
7301 | complete_type (type); | |
7de85f7e | 7302 | if (!DECL_SIZE (decl) |
328de7c2 | 7303 | && TREE_TYPE (decl) != error_mark_node |
7de85f7e MM |
7304 | && (COMPLETE_TYPE_P (type) |
7305 | || (TREE_CODE (type) == ARRAY_TYPE | |
7306 | && !TYPE_DOMAIN (type) | |
7307 | && COMPLETE_TYPE_P (TREE_TYPE (type))))) | |
27778b73 MM |
7308 | layout_decl (decl, 0); |
7309 | ||
c82dbd95 | 7310 | if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE) |
27778b73 MM |
7311 | { |
7312 | /* An automatic variable with an incomplete type: that is an error. | |
7313 | Don't talk about array types here, since we took care of that | |
7314 | message in grokdeclarator. */ | |
33bd39a2 | 7315 | error ("storage size of `%D' isn't known", decl); |
27778b73 MM |
7316 | TREE_TYPE (decl) = error_mark_node; |
7317 | } | |
ae673f14 JM |
7318 | #if 0 |
7319 | /* Keep this code around in case we later want to control debug info | |
7320 | based on whether a type is "used". (jason 1999-11-11) */ | |
7321 | ||
27778b73 MM |
7322 | else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype)) |
7323 | /* Let debugger know it should output info for this type. */ | |
7324 | note_debug_info_needed (ttype); | |
7325 | ||
7326 | if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl)) | |
7327 | note_debug_info_needed (DECL_CONTEXT (decl)); | |
ae673f14 | 7328 | #endif |
27778b73 MM |
7329 | |
7330 | if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl)) | |
7331 | && DECL_SIZE (decl) != NULL_TREE | |
7332 | && ! TREE_CONSTANT (DECL_SIZE (decl))) | |
7333 | { | |
7334 | if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST) | |
7335 | constant_expression_warning (DECL_SIZE (decl)); | |
7336 | else | |
33bd39a2 | 7337 | error ("storage size of `%D' isn't constant", decl); |
27778b73 | 7338 | } |
8e4ce833 JJ |
7339 | |
7340 | if (TREE_STATIC (decl) | |
7341 | && !DECL_ARTIFICIAL (decl) | |
7342 | && current_function_decl | |
7343 | && DECL_CONTEXT (decl) == current_function_decl) | |
7344 | push_local_name (decl); | |
27778b73 MM |
7345 | } |
7346 | ||
27778b73 MM |
7347 | /* If a local static variable is declared in an inline function, or if |
7348 | we have a weak definition, we must endeavor to create only one | |
7349 | instance of the variable at link-time. */ | |
7350 | ||
7351 | static void | |
11f6b451 | 7352 | maybe_commonize_var (tree decl) |
27778b73 MM |
7353 | { |
7354 | /* Static data in a function with comdat linkage also has comdat | |
7355 | linkage. */ | |
7356 | if (TREE_STATIC (decl) | |
7357 | /* Don't mess with __FUNCTION__. */ | |
cf74fb86 | 7358 | && ! DECL_ARTIFICIAL (decl) |
0b50d7f1 MM |
7359 | && DECL_FUNCTION_SCOPE_P (decl) |
7360 | /* Unfortunately, import_export_decl has not always been called | |
7361 | before the function is processed, so we cannot simply check | |
7362 | DECL_COMDAT. */ | |
9e6aaf5e | 7363 | && (DECL_COMDAT (DECL_CONTEXT (decl)) |
0b50d7f1 MM |
7364 | || ((DECL_DECLARED_INLINE_P (DECL_CONTEXT (decl)) |
7365 | || DECL_TEMPLATE_INSTANTIATION (DECL_CONTEXT (decl))) | |
7366 | && TREE_PUBLIC (DECL_CONTEXT (decl))))) | |
27778b73 | 7367 | { |
0dbc5cd3 | 7368 | if (flag_weak) |
27778b73 | 7369 | { |
0dbc5cd3 MM |
7370 | /* With weak symbols, we simply make the variable COMDAT; |
7371 | that will cause copies in multiple translations units to | |
7372 | be merged. */ | |
7373 | comdat_linkage (decl); | |
7374 | } | |
7375 | else | |
7376 | { | |
7377 | if (DECL_INITIAL (decl) == NULL_TREE | |
7378 | || DECL_INITIAL (decl) == error_mark_node) | |
27778b73 | 7379 | { |
0dbc5cd3 MM |
7380 | /* Without weak symbols, we can use COMMON to merge |
7381 | uninitialized variables. */ | |
27778b73 MM |
7382 | TREE_PUBLIC (decl) = 1; |
7383 | DECL_COMMON (decl) = 1; | |
7384 | } | |
0dbc5cd3 | 7385 | else |
27778b73 | 7386 | { |
0dbc5cd3 MM |
7387 | /* While for initialized variables, we must use internal |
7388 | linkage -- which means that multiple copies will not | |
7389 | be merged. */ | |
7390 | TREE_PUBLIC (decl) = 0; | |
7391 | DECL_COMMON (decl) = 0; | |
27778b73 MM |
7392 | cp_warning_at ("sorry: semantics of inline function static data `%#D' are wrong (you'll wind up with multiple copies)", decl); |
7393 | cp_warning_at (" you can work around this by removing the initializer", decl); | |
7394 | } | |
7395 | } | |
7396 | } | |
7397 | else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl)) | |
7398 | /* Set it up again; we might have set DECL_INITIAL since the last | |
7399 | time. */ | |
7400 | comdat_linkage (decl); | |
7401 | } | |
7402 | ||
91063b51 MM |
7403 | /* Issue an error message if DECL is an uninitialized const variable. */ |
7404 | ||
7405 | static void | |
11f6b451 | 7406 | check_for_uninitialized_const_var (tree decl) |
91063b51 MM |
7407 | { |
7408 | tree type = TREE_TYPE (decl); | |
7409 | ||
7410 | /* ``Unless explicitly declared extern, a const object does not have | |
7411 | external linkage and must be initialized. ($8.4; $12.1)'' ARM | |
7412 | 7.1.6 */ | |
7413 | if (TREE_CODE (decl) == VAR_DECL | |
7414 | && TREE_CODE (type) != REFERENCE_TYPE | |
7415 | && CP_TYPE_CONST_P (type) | |
7416 | && !TYPE_NEEDS_CONSTRUCTING (type) | |
7417 | && !DECL_INITIAL (decl)) | |
33bd39a2 | 7418 | error ("uninitialized const `%D'", decl); |
91063b51 MM |
7419 | } |
7420 | ||
8e3df2de MM |
7421 | /* FIELD is a FIELD_DECL or NULL. In the former case, the value |
7422 | returned is the next FIELD_DECL (possibly FIELD itself) that can be | |
7423 | initialized. If there are no more such fields, the return value | |
7424 | will be NULL. */ | |
7425 | ||
7426 | static tree | |
7427 | next_initializable_field (tree field) | |
7428 | { | |
7429 | while (field | |
7430 | && (TREE_CODE (field) != FIELD_DECL | |
7431 | || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field)) | |
7432 | || DECL_ARTIFICIAL (field))) | |
7433 | field = TREE_CHAIN (field); | |
7434 | ||
7435 | return field; | |
7436 | } | |
7437 | ||
7438 | /* Undo the brace-elision allowed by [dcl.init.aggr] in a | |
7439 | brace-enclosed aggregate initializer. | |
7440 | ||
7441 | *INITP is one of a list of initializers describing a brace-enclosed | |
7442 | initializer for an entity of the indicated aggregate TYPE. It may | |
7443 | not presently match the shape of the TYPE; for example: | |
7444 | ||
7445 | struct S { int a; int b; }; | |
7446 | struct S a[] = { 1, 2, 3, 4 }; | |
7447 | ||
7448 | Here *INITP will point to TREE_LIST of four elements, rather than a | |
7449 | list of two elements, each itself a list of two elements. This | |
7450 | routine transforms INIT from the former form into the latter. The | |
7451 | revised initializer is returned. */ | |
7452 | ||
7453 | static tree | |
7454 | reshape_init (tree type, tree *initp) | |
7455 | { | |
7456 | tree inits; | |
7457 | tree old_init; | |
7458 | tree old_init_value; | |
7459 | tree new_init; | |
7460 | bool brace_enclosed_p; | |
7461 | ||
7462 | old_init = *initp; | |
7463 | old_init_value = (TREE_CODE (*initp) == TREE_LIST | |
7464 | ? TREE_VALUE (*initp) : old_init); | |
7465 | ||
ee7ecb29 MM |
7466 | /* For some parse errors, OLD_INIT_VALUE may be NULL. */ |
7467 | if (!old_init_value) | |
7468 | { | |
7469 | my_friendly_assert (TREE_CODE (old_init) == TREE_LIST, 20021202); | |
7470 | TREE_VALUE (old_init) = error_mark_node; | |
7471 | return old_init; | |
7472 | } | |
7473 | ||
8e3df2de MM |
7474 | /* If the initializer is brace-enclosed, pull initializers from the |
7475 | enclosed elements. Advance past the brace-enclosed initializer | |
7476 | now. */ | |
dbe85b80 JM |
7477 | if (TREE_CODE (old_init_value) == CONSTRUCTOR |
7478 | && TREE_TYPE (old_init_value) == NULL_TREE | |
8e3df2de MM |
7479 | && TREE_HAS_CONSTRUCTOR (old_init_value)) |
7480 | { | |
7481 | *initp = TREE_CHAIN (old_init); | |
7482 | TREE_CHAIN (old_init) = NULL_TREE; | |
7483 | inits = CONSTRUCTOR_ELTS (old_init_value); | |
7484 | initp = &inits; | |
7485 | brace_enclosed_p = true; | |
7486 | } | |
7487 | else | |
7488 | { | |
7489 | inits = NULL_TREE; | |
7490 | brace_enclosed_p = false; | |
7491 | } | |
7492 | ||
7493 | /* A non-aggregate type is always initialized with a single | |
7494 | initializer. */ | |
7495 | if (!CP_AGGREGATE_TYPE_P (type)) | |
7496 | { | |
7497 | *initp = TREE_CHAIN (old_init); | |
7498 | TREE_CHAIN (old_init) = NULL_TREE; | |
7499 | /* It is invalid to initialize a non-aggregate type with a | |
7500 | brace-enclosed initializer. */ | |
7501 | if (brace_enclosed_p) | |
7502 | { | |
7503 | error ("brace-enclosed initializer used to initialize `%T'", | |
7504 | type); | |
7505 | if (TREE_CODE (old_init) == TREE_LIST) | |
7506 | TREE_VALUE (old_init) = error_mark_node; | |
7507 | else | |
7508 | old_init = error_mark_node; | |
7509 | } | |
7510 | ||
7511 | return old_init; | |
7512 | } | |
7513 | ||
7514 | /* [dcl.init.aggr] | |
7515 | ||
7516 | All implicit type conversions (clause _conv_) are considered when | |
7517 | initializing the aggregate member with an initializer from an | |
7518 | initializer-list. If the initializer can initialize a member, | |
7519 | the member is initialized. Otherwise, if the member is itself a | |
7520 | non-empty subaggregate, brace elision is assumed and the | |
7521 | initializer is considered for the initialization of the first | |
7522 | member of the subaggregate. */ | |
dbe85b80 | 7523 | if (!brace_enclosed_p |
8e3df2de MM |
7524 | && can_convert_arg (type, TREE_TYPE (old_init_value), old_init_value)) |
7525 | { | |
7526 | *initp = TREE_CHAIN (old_init); | |
7527 | TREE_CHAIN (old_init) = NULL_TREE; | |
7528 | return old_init; | |
7529 | } | |
7530 | ||
3fa3c4bd | 7531 | if (TREE_CODE (old_init_value) == STRING_CST |
8e3df2de | 7532 | && TREE_CODE (type) == ARRAY_TYPE |
42328048 | 7533 | && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))) |
8e3df2de MM |
7534 | { |
7535 | /* [dcl.init.string] | |
7536 | ||
7537 | A char array (whether plain char, signed char, or unsigned char) | |
7538 | can be initialized by a string-literal (optionally enclosed in | |
7539 | braces); a wchar_t array can be initialized by a wide | |
7540 | string-literal (optionally enclosed in braces). */ | |
7541 | new_init = old_init; | |
7542 | /* Move past the initializer. */ | |
7543 | *initp = TREE_CHAIN (old_init); | |
7544 | TREE_CHAIN (old_init) = NULL_TREE; | |
7545 | } | |
7546 | else | |
7547 | { | |
7548 | /* Build a CONSTRUCTOR to hold the contents of the aggregate. */ | |
dcf92453 | 7549 | new_init = build_constructor (type, NULL_TREE); |
8e3df2de MM |
7550 | TREE_HAS_CONSTRUCTOR (new_init) = 1; |
7551 | ||
7552 | if (CLASS_TYPE_P (type)) | |
7553 | { | |
7554 | tree field; | |
7555 | ||
7556 | field = next_initializable_field (TYPE_FIELDS (type)); | |
7557 | ||
7558 | if (!field) | |
7559 | { | |
7560 | /* [dcl.init.aggr] | |
7561 | ||
7562 | An initializer for an aggregate member that is an | |
7563 | empty class shall have the form of an empty | |
7564 | initializer-list {}. */ | |
7565 | if (!brace_enclosed_p) | |
7566 | error ("initializer for `%T' must be brace-enclosed", | |
7567 | type); | |
7568 | } | |
7569 | else | |
7570 | { | |
7571 | /* Loop through the initializable fields, gathering | |
7572 | initializers. */ | |
446d5e47 | 7573 | while (*initp) |
8e3df2de MM |
7574 | { |
7575 | tree field_init; | |
7576 | ||
446d5e47 MM |
7577 | /* Handle designated initializers, as an extension. */ |
7578 | if (TREE_PURPOSE (*initp)) | |
7579 | { | |
7580 | if (pedantic) | |
7581 | pedwarn ("ISO C++ does not allow designated initializers"); | |
7582 | field = lookup_field_1 (type, TREE_PURPOSE (*initp), | |
7583 | /*want_type=*/false); | |
7584 | if (!field || TREE_CODE (field) != FIELD_DECL) | |
7585 | error ("`%T' has no non-static data member named `%D'", | |
7586 | type, TREE_PURPOSE (*initp)); | |
7587 | } | |
7588 | if (!field) | |
7589 | break; | |
7590 | ||
8e3df2de MM |
7591 | field_init = reshape_init (TREE_TYPE (field), initp); |
7592 | TREE_CHAIN (field_init) = CONSTRUCTOR_ELTS (new_init); | |
7593 | CONSTRUCTOR_ELTS (new_init) = field_init; | |
7594 | /* [dcl.init.aggr] | |
7595 | ||
7596 | When a union is initialized with a brace-enclosed | |
7597 | initializer, the braces shall only contain an | |
7598 | initializer for the first member of the union. */ | |
7599 | if (TREE_CODE (type) == UNION_TYPE) | |
7600 | break; | |
8e3df2de MM |
7601 | field = next_initializable_field (TREE_CHAIN (field)); |
7602 | } | |
7603 | } | |
7604 | } | |
7605 | else if (TREE_CODE (type) == ARRAY_TYPE) | |
7606 | { | |
7607 | tree index; | |
7608 | tree max_index; | |
7609 | ||
7610 | /* If the bound of the array is known, take no more initializers | |
7611 | than are allowed. */ | |
7612 | max_index = (TYPE_DOMAIN (type) | |
7613 | ? array_type_nelts (type) : NULL_TREE); | |
7614 | /* Loop through the array elements, gathering initializers. */ | |
7615 | for (index = size_zero_node; | |
7616 | *initp && (!max_index || !tree_int_cst_lt (max_index, index)); | |
7617 | index = size_binop (PLUS_EXPR, index, size_one_node)) | |
7618 | { | |
7619 | tree element_init; | |
7620 | ||
7621 | element_init = reshape_init (TREE_TYPE (type), initp); | |
7622 | TREE_CHAIN (element_init) = CONSTRUCTOR_ELTS (new_init); | |
7623 | CONSTRUCTOR_ELTS (new_init) = element_init; | |
7624 | if (TREE_PURPOSE (element_init)) | |
7625 | index = TREE_PURPOSE (element_init); | |
7626 | } | |
7627 | } | |
7628 | else | |
7629 | abort (); | |
7630 | ||
7631 | /* The initializers were placed in reverse order in the | |
7632 | CONSTRUCTOR. */ | |
7633 | CONSTRUCTOR_ELTS (new_init) = nreverse (CONSTRUCTOR_ELTS (new_init)); | |
7634 | ||
7635 | if (TREE_CODE (old_init) == TREE_LIST) | |
7636 | new_init = build_tree_list (TREE_PURPOSE (old_init), new_init); | |
7637 | } | |
7638 | ||
7639 | /* If this was a brace-enclosed initializer and all of the | |
7640 | initializers were not used up, there is a problem. */ | |
7641 | if (brace_enclosed_p && *initp) | |
7642 | error ("too many initializers for `%T'", type); | |
7643 | ||
7644 | return new_init; | |
7645 | } | |
7646 | ||
c82dbd95 | 7647 | /* Verify INIT (the initializer for DECL), and record the |
8e3df2de MM |
7648 | initialization in DECL_INITIAL, if appropriate. |
7649 | ||
7650 | If the return value is non-NULL, it is an expression that must be | |
7651 | evaluated dynamically to initialize DECL. */ | |
27778b73 | 7652 | |
c82dbd95 | 7653 | static tree |
8e3df2de | 7654 | check_initializer (tree decl, tree init, int flags) |
27778b73 | 7655 | { |
17bbb839 | 7656 | tree type = TREE_TYPE (decl); |
fc0e7bf5 | 7657 | |
27778b73 MM |
7658 | /* If `start_decl' didn't like having an initialization, ignore it now. */ |
7659 | if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE) | |
7660 | init = NULL_TREE; | |
27778b73 | 7661 | |
17bbb839 MM |
7662 | /* If an initializer is present, DECL_INITIAL has been |
7663 | error_mark_node, to indicate that an as-of-yet unevaluated | |
7664 | initialization will occur. From now on, DECL_INITIAL reflects | |
7665 | the static initialization -- if any -- of DECL. */ | |
7666 | DECL_INITIAL (decl) = NULL_TREE; | |
7667 | ||
328de7c2 MM |
7668 | /* Things that are going to be initialized need to have complete |
7669 | type. */ | |
7670 | TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl)); | |
c82dbd95 | 7671 | |
328de7c2 MM |
7672 | if (type == error_mark_node) |
7673 | /* We will have already complained. */ | |
7674 | init = NULL_TREE; | |
7675 | else if (init && COMPLETE_TYPE_P (type) | |
7676 | && !TREE_CONSTANT (TYPE_SIZE (type))) | |
7677 | { | |
7678 | error ("variable-sized object `%D' may not be initialized", decl); | |
7679 | init = NULL_TREE; | |
7680 | } | |
7681 | else if (TREE_CODE (type) == ARRAY_TYPE | |
7682 | && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (type)))) | |
7683 | { | |
7684 | error ("elements of array `%#D' have incomplete type", decl); | |
7685 | init = NULL_TREE; | |
7686 | } | |
7687 | else if (TREE_CODE (type) != ARRAY_TYPE && !COMPLETE_TYPE_P (type)) | |
7688 | { | |
7689 | error ("`%D' has incomplete type", decl); | |
7690 | TREE_TYPE (decl) = error_mark_node; | |
7691 | init = NULL_TREE; | |
27778b73 MM |
7692 | } |
7693 | ||
7694 | if (TREE_CODE (decl) == CONST_DECL) | |
7695 | { | |
7696 | my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148); | |
7697 | ||
7698 | DECL_INITIAL (decl) = init; | |
7699 | ||
27778b73 MM |
7700 | my_friendly_assert (init != NULL_TREE, 149); |
7701 | init = NULL_TREE; | |
7702 | } | |
c82dbd95 | 7703 | else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE) |
17bbb839 | 7704 | init = grok_reference_init (decl, type, init); |
27778b73 MM |
7705 | else if (init) |
7706 | { | |
8e3df2de | 7707 | if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init)) |
a16f2357 GK |
7708 | { |
7709 | /* [dcl.init] paragraph 13, | |
7710 | If T is a scalar type, then a declaration of the form | |
7711 | T x = { a }; | |
7712 | is equivalent to | |
7713 | T x = a; | |
7714 | ||
7715 | reshape_init will complain about the extra braces, | |
7716 | and doesn't do anything useful in the case where TYPE is | |
7717 | scalar, so just don't call it. */ | |
7718 | if (CP_AGGREGATE_TYPE_P (type)) | |
7719 | init = reshape_init (type, &init); | |
6e1b3a7c AH |
7720 | |
7721 | if ((*targetm.vector_opaque_p) (type)) | |
7722 | { | |
7723 | error ("opaque vector types cannot be initialized"); | |
7724 | init = error_mark_node; | |
7725 | } | |
a16f2357 | 7726 | } |
8e3df2de MM |
7727 | |
7728 | /* If DECL has an array type without a specific bound, deduce the | |
7729 | array size from the initializer. */ | |
7730 | maybe_deduce_size_from_array_init (decl, init); | |
7731 | type = TREE_TYPE (decl); | |
7732 | if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init)) | |
7733 | TREE_TYPE (init) = type; | |
7734 | ||
27778b73 MM |
7735 | if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type)) |
7736 | { | |
7737 | if (TREE_CODE (type) == ARRAY_TYPE) | |
8e3df2de | 7738 | goto initialize_aggr; |
27778b73 MM |
7739 | else if (TREE_CODE (init) == CONSTRUCTOR |
7740 | && TREE_HAS_CONSTRUCTOR (init)) | |
7741 | { | |
7742 | if (TYPE_NON_AGGREGATE_CLASS (type)) | |
7743 | { | |
33bd39a2 | 7744 | error ("`%D' must be initialized by constructor, not by `{...}'", |
8e3df2de | 7745 | decl); |
27778b73 MM |
7746 | init = error_mark_node; |
7747 | } | |
7748 | else | |
7749 | goto dont_use_constructor; | |
7750 | } | |
8e3df2de MM |
7751 | else |
7752 | { | |
7753 | int saved_stmts_are_full_exprs_p; | |
7754 | ||
7755 | initialize_aggr: | |
7756 | saved_stmts_are_full_exprs_p = 0; | |
7757 | if (building_stmt_tree ()) | |
7758 | { | |
7759 | saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p (); | |
7760 | current_stmt_tree ()->stmts_are_full_exprs_p = 1; | |
7761 | } | |
7762 | init = build_aggr_init (decl, init, flags); | |
7763 | if (building_stmt_tree ()) | |
7764 | current_stmt_tree ()->stmts_are_full_exprs_p = | |
7765 | saved_stmts_are_full_exprs_p; | |
7766 | return init; | |
7767 | } | |
27778b73 MM |
7768 | } |
7769 | else | |
7770 | { | |
7771 | dont_use_constructor: | |
7772 | if (TREE_CODE (init) != TREE_VEC) | |
7773 | init = store_init_value (decl, init); | |
7774 | } | |
27778b73 MM |
7775 | } |
7776 | else if (DECL_EXTERNAL (decl)) | |
7777 | ; | |
8e3df2de MM |
7778 | else if (TYPE_P (type) && TYPE_NEEDS_CONSTRUCTING (type)) |
7779 | goto initialize_aggr; | |
7780 | else if (IS_AGGR_TYPE (type)) | |
27778b73 MM |
7781 | { |
7782 | tree core_type = strip_array_types (type); | |
7783 | ||
8e3df2de MM |
7784 | if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)) |
7785 | error ("structure `%D' with uninitialized const members", decl); | |
7786 | if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)) | |
7787 | error ("structure `%D' with uninitialized reference members", | |
7788 | decl); | |
27778b73 MM |
7789 | |
7790 | check_for_uninitialized_const_var (decl); | |
27778b73 MM |
7791 | } |
7792 | else | |
7793 | check_for_uninitialized_const_var (decl); | |
68642fb6 | 7794 | |
8e3df2de MM |
7795 | if (init && init != error_mark_node) |
7796 | init = build (INIT_EXPR, type, decl, init); | |
7797 | ||
c82dbd95 | 7798 | return init; |
27778b73 MM |
7799 | } |
7800 | ||
7801 | /* If DECL is not a local variable, give it RTL. */ | |
7802 | ||
7803 | static void | |
11f6b451 | 7804 | make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec) |
27778b73 | 7805 | { |
95ee998c MM |
7806 | int toplev = toplevel_bindings_p (); |
7807 | int defer_p; | |
27778b73 | 7808 | |
f39ee884 MM |
7809 | /* Handle non-variables up front. */ |
7810 | if (TREE_CODE (decl) != VAR_DECL) | |
7811 | { | |
7812 | rest_of_decl_compilation (decl, asmspec, toplev, at_eof); | |
7813 | return; | |
7814 | } | |
7815 | ||
95ee998c MM |
7816 | /* If we see a class member here, it should be a static data |
7817 | member. */ | |
7818 | if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl)) | |
7819 | { | |
7820 | my_friendly_assert (TREE_STATIC (decl), 19990828); | |
7821 | /* An in-class declaration of a static data member should be | |
7822 | external; it is only a declaration, and not a definition. */ | |
7823 | if (init == NULL_TREE) | |
7824 | my_friendly_assert (DECL_EXTERNAL (decl), 20000723); | |
7825 | } | |
7826 | ||
f39ee884 MM |
7827 | /* Set the DECL_ASSEMBLER_NAME for the variable. */ |
7828 | if (asmspec) | |
7ed47c04 MM |
7829 | { |
7830 | SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec)); | |
7831 | /* The `register' keyword, when used together with an | |
7832 | asm-specification, indicates that the variable should be | |
7833 | placed in a particular register. */ | |
7834 | if (DECL_REGISTER (decl)) | |
7835 | DECL_C_HARD_REGISTER (decl) = 1; | |
7836 | } | |
f39ee884 | 7837 | |
95ee998c MM |
7838 | /* We don't create any RTL for local variables. */ |
7839 | if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl)) | |
7840 | return; | |
27778b73 | 7841 | |
95ee998c MM |
7842 | /* We defer emission of local statics until the corresponding |
7843 | DECL_STMT is expanded. */ | |
7844 | defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl); | |
7845 | ||
b602511f JM |
7846 | /* We try to defer namespace-scope static constants so that they are |
7847 | not emitted into the object file unnecessarily. */ | |
7848 | if (!DECL_VIRTUAL_P (decl) | |
7849 | && TREE_READONLY (decl) | |
7850 | && DECL_INITIAL (decl) != NULL_TREE | |
7851 | && DECL_INITIAL (decl) != error_mark_node | |
7852 | && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)) | |
7853 | && toplev | |
7854 | && !TREE_PUBLIC (decl)) | |
5cc90635 JM |
7855 | { |
7856 | /* Fool with the linkage of static consts according to #pragma | |
7857 | interface. */ | |
7858 | if (!interface_unknown && !TREE_PUBLIC (decl)) | |
27778b73 | 7859 | { |
95ee998c MM |
7860 | TREE_PUBLIC (decl) = 1; |
7861 | DECL_EXTERNAL (decl) = interface_only; | |
27778b73 | 7862 | } |
27778b73 | 7863 | |
95ee998c | 7864 | defer_p = 1; |
27778b73 | 7865 | } |
b602511f JM |
7866 | /* Likewise for template instantiations. */ |
7867 | else if (DECL_COMDAT (decl)) | |
7868 | defer_p = 1; | |
95ee998c | 7869 | |
92643fea MM |
7870 | /* If we're deferring the variable, we only need to make RTL if |
7871 | there's an ASMSPEC. Otherwise, we'll lazily create it later when | |
7872 | we need it. (There's no way to lazily create RTL for things that | |
7873 | have assembly specs because the information about the specifier | |
7874 | isn't stored in the tree, yet) */ | |
7875 | if (defer_p && asmspec) | |
6c418184 | 7876 | make_decl_rtl (decl, asmspec); |
95ee998c | 7877 | /* If we're not deferring, go ahead and assemble the variable. */ |
92643fea | 7878 | else if (!defer_p) |
27778b73 | 7879 | rest_of_decl_compilation (decl, asmspec, toplev, at_eof); |
27778b73 MM |
7880 | } |
7881 | ||
7882 | /* The old ARM scoping rules injected variables declared in the | |
7883 | initialization statement of a for-statement into the surrounding | |
7884 | scope. We support this usage, in order to be backward-compatible. | |
7885 | DECL is a just-declared VAR_DECL; if necessary inject its | |
7886 | declaration into the surrounding scope. */ | |
7887 | ||
b7b8bcd2 | 7888 | void |
11f6b451 | 7889 | maybe_inject_for_scope_var (tree decl) |
27778b73 | 7890 | { |
22ffcc6f | 7891 | timevar_push (TV_NAME_LOOKUP); |
c3783399 | 7892 | if (!DECL_NAME (decl)) |
de94b46c KG |
7893 | { |
7894 | timevar_pop (TV_NAME_LOOKUP); | |
7895 | return; | |
7896 | } | |
6b607ffd MM |
7897 | |
7898 | /* Declarations of __FUNCTION__ and its ilk appear magically when | |
7899 | the variable is first used. If that happens to be inside a | |
7900 | for-loop, we don't want to do anything special. */ | |
7901 | if (DECL_PRETTY_FUNCTION_P (decl)) | |
de94b46c KG |
7902 | { |
7903 | timevar_pop (TV_NAME_LOOKUP); | |
7904 | return; | |
7905 | } | |
5362b086 | 7906 | |
27778b73 MM |
7907 | if (current_binding_level->is_for_scope) |
7908 | { | |
e2500fed | 7909 | struct cp_binding_level *outer |
27778b73 MM |
7910 | = current_binding_level->level_chain; |
7911 | ||
7912 | /* Check to see if the same name is already bound at the outer | |
7913 | level, either because it was directly declared, or because a | |
7914 | dead for-decl got preserved. In either case, the code would | |
7915 | not have been valid under the ARM scope rules, so clear | |
7916 | is_for_scope for the current_binding_level. | |
7917 | ||
7918 | Otherwise, we need to preserve the temp slot for decl to last | |
7919 | into the outer binding level. */ | |
7920 | ||
af6fd53f GDR |
7921 | cxx_binding *outer_binding |
7922 | = IDENTIFIER_BINDING (DECL_NAME (decl))->previous; | |
68642fb6 | 7923 | |
ed3cf953 | 7924 | if (outer_binding && BINDING_SCOPE (outer_binding) == outer |
af6fd53f | 7925 | && (TREE_CODE (BINDING_VALUE (outer_binding)) == VAR_DECL) |
27778b73 MM |
7926 | && DECL_DEAD_FOR_LOCAL (BINDING_VALUE (outer_binding))) |
7927 | { | |
7928 | BINDING_VALUE (outer_binding) | |
7929 | = DECL_SHADOWED_FOR_VAR (BINDING_VALUE (outer_binding)); | |
7930 | current_binding_level->is_for_scope = 0; | |
7931 | } | |
27778b73 | 7932 | } |
22ffcc6f | 7933 | timevar_pop (TV_NAME_LOOKUP); |
27778b73 MM |
7934 | } |
7935 | ||
ed5511d9 | 7936 | /* Generate code to initialize DECL (a local variable). */ |
27778b73 | 7937 | |
8e3df2de | 7938 | static void |
11f6b451 | 7939 | initialize_local_var (tree decl, tree init) |
27778b73 | 7940 | { |
9ed9e79a | 7941 | tree type = TREE_TYPE (decl); |
170b020f | 7942 | tree cleanup; |
27778b73 | 7943 | |
8e3df2de MM |
7944 | my_friendly_assert (TREE_CODE (decl) == VAR_DECL |
7945 | || TREE_CODE (decl) == RESULT_DECL, | |
7946 | 20021010); | |
7947 | my_friendly_assert (!TREE_STATIC (decl), 20021010); | |
b7b8bcd2 | 7948 | |
8e3df2de | 7949 | if (DECL_SIZE (decl) == NULL_TREE) |
b7b8bcd2 MM |
7950 | { |
7951 | /* If we used it already as memory, it must stay in memory. */ | |
7952 | DECL_INITIAL (decl) = NULL_TREE; | |
7953 | TREE_ADDRESSABLE (decl) = TREE_USED (decl); | |
7954 | } | |
7955 | ||
27778b73 MM |
7956 | if (DECL_SIZE (decl) && type != error_mark_node) |
7957 | { | |
7958 | int already_used; | |
68642fb6 | 7959 | |
27778b73 | 7960 | /* Compute and store the initial value. */ |
27778b73 MM |
7961 | already_used = TREE_USED (decl) || TREE_USED (type); |
7962 | ||
8e3df2de MM |
7963 | /* Perform the initialization. */ |
7964 | if (init) | |
27778b73 | 7965 | { |
24bef158 MM |
7966 | int saved_stmts_are_full_exprs_p; |
7967 | ||
3a0d3e1e | 7968 | my_friendly_assert (building_stmt_tree (), 20000906); |
f2c5f623 | 7969 | saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p (); |
ae499cce | 7970 | current_stmt_tree ()->stmts_are_full_exprs_p = 1; |
8e3df2de | 7971 | finish_expr_stmt (init); |
5362b086 | 7972 | current_stmt_tree ()->stmts_are_full_exprs_p = |
ae499cce | 7973 | saved_stmts_are_full_exprs_p; |
27778b73 MM |
7974 | } |
7975 | ||
7976 | /* Set this to 0 so we can tell whether an aggregate which was | |
7977 | initialized was ever used. Don't do this if it has a | |
7978 | destructor, so we don't complain about the 'resource | |
b7b8bcd2 MM |
7979 | allocation is initialization' idiom. Now set |
7980 | attribute((unused)) on types so decls of that type will be | |
7981 | marked used. (see TREE_USED, above.) */ | |
27778b73 MM |
7982 | if (TYPE_NEEDS_CONSTRUCTING (type) |
7983 | && ! already_used | |
834c6dff | 7984 | && TYPE_HAS_TRIVIAL_DESTRUCTOR (type) |
27778b73 MM |
7985 | && DECL_NAME (decl)) |
7986 | TREE_USED (decl) = 0; | |
b7b8bcd2 | 7987 | else if (already_used) |
27778b73 MM |
7988 | TREE_USED (decl) = 1; |
7989 | } | |
24bef158 | 7990 | |
8e3df2de | 7991 | /* Generate a cleanup, if necessary. */ |
170b020f MM |
7992 | cleanup = cxx_maybe_build_cleanup (decl); |
7993 | if (DECL_SIZE (decl) && cleanup) | |
7994 | finish_decl_cleanup (decl, cleanup); | |
24bef158 MM |
7995 | } |
7996 | ||
8d08fdba MS |
7997 | /* Finish processing of a declaration; |
7998 | install its line number and initial value. | |
7999 | If the length of an array type is not known before, | |
8000 | it must be determined now, from the initial value, or it is an error. | |
8001 | ||
8b27e9ef | 8002 | INIT holds the value of an initializer that should be allowed to escape |
8d08fdba MS |
8003 | the normal rules. |
8004 | ||
920f9474 | 8005 | FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0 |
f78c7bc6 | 8006 | if the (init) syntax was used. */ |
8d08fdba MS |
8007 | |
8008 | void | |
11f6b451 | 8009 | cp_finish_decl (tree decl, tree init, tree asmspec_tree, int flags) |
8d08fdba MS |
8010 | { |
8011 | register tree type; | |
27778b73 | 8012 | tree ttype = NULL_TREE; |
9c0758dd | 8013 | const char *asmspec = NULL; |
8d08fdba MS |
8014 | int was_readonly = 0; |
8015 | ||
8d08fdba MS |
8016 | if (! decl) |
8017 | { | |
8018 | if (init) | |
8251199e | 8019 | error ("assignment (not initialization) in declaration"); |
8d08fdba MS |
8020 | return; |
8021 | } | |
8022 | ||
170b020f MM |
8023 | my_friendly_assert (TREE_CODE (decl) != RESULT_DECL, 20030619); |
8024 | ||
a4443a08 | 8025 | /* If a name was specified, get the string. */ |
5f52c0e0 | 8026 | if (global_scope_p (current_binding_level)) |
41c64394 | 8027 | asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree); |
8d08fdba | 8028 | if (asmspec_tree) |
5cc90635 | 8029 | asmspec = TREE_STRING_POINTER (asmspec_tree); |
8d08fdba | 8030 | |
2c73f9f5 ML |
8031 | if (init && TREE_CODE (init) == NAMESPACE_DECL) |
8032 | { | |
33bd39a2 | 8033 | error ("cannot initialize `%D' to namespace `%D'", |
2c73f9f5 ML |
8034 | decl, init); |
8035 | init = NULL_TREE; | |
8036 | } | |
8037 | ||
6ba89f8e | 8038 | if (current_class_type |
4f1c5b7d | 8039 | && CP_DECL_CONTEXT (decl) == current_class_type |
6ba89f8e MM |
8040 | && TYPE_BEING_DEFINED (current_class_type) |
8041 | && (DECL_INITIAL (decl) || init)) | |
3febd123 | 8042 | DECL_INITIALIZED_IN_CLASS_P (decl) = 1; |
6ba89f8e | 8043 | |
68642fb6 | 8044 | if (TREE_CODE (decl) == VAR_DECL |
9a68c51f JM |
8045 | && DECL_CONTEXT (decl) |
8046 | && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL | |
8047 | && DECL_CONTEXT (decl) != current_namespace | |
8048 | && init) | |
8049 | { | |
a1c65f9f | 8050 | /* Leave the namespace of the object. */ |
9a68c51f JM |
8051 | pop_decl_namespace (); |
8052 | } | |
8053 | ||
c82dbd95 | 8054 | type = TREE_TYPE (decl); |
8d08fdba | 8055 | |
f376e137 | 8056 | if (type == error_mark_node) |
df7f7b9c | 8057 | goto finish_end0; |
5362b086 | 8058 | |
a7a7710d NS |
8059 | if (TYPE_HAS_MUTABLE_P (type)) |
8060 | TREE_READONLY (decl) = 0; | |
24bef158 | 8061 | |
5156628f | 8062 | if (processing_template_decl) |
5566b478 | 8063 | { |
08ac397c | 8064 | /* Add this declaration to the statement-tree. */ |
170b020f | 8065 | if (at_function_scope_p ()) |
08ac397c JM |
8066 | add_decl_stmt (decl); |
8067 | ||
5566b478 | 8068 | if (init && DECL_INITIAL (decl)) |
2a1e9fdd | 8069 | DECL_INITIAL (decl) = init; |
5566b478 MS |
8070 | goto finish_end0; |
8071 | } | |
3e41d13b | 8072 | |
27778b73 MM |
8073 | /* Parameters are handled by store_parm_decls, not cp_finish_decl. */ |
8074 | my_friendly_assert (TREE_CODE (decl) != PARM_DECL, 19990828); | |
8075 | ||
8d08fdba MS |
8076 | /* Take care of TYPE_DECLs up front. */ |
8077 | if (TREE_CODE (decl) == TYPE_DECL) | |
8078 | { | |
a0a33927 MS |
8079 | if (type != error_mark_node |
8080 | && IS_AGGR_TYPE (type) && DECL_NAME (decl)) | |
8d08fdba MS |
8081 | { |
8082 | if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type) | |
33bd39a2 | 8083 | warning ("shadowing previous type declaration of `%#D'", decl); |
8d08fdba MS |
8084 | set_identifier_type_value (DECL_NAME (decl), type); |
8085 | CLASSTYPE_GOT_SEMICOLON (type) = 1; | |
8086 | } | |
cffa8729 MS |
8087 | |
8088 | /* If we have installed this as the canonical typedef for this | |
8089 | type, and that type has not been defined yet, delay emitting | |
956d6950 | 8090 | the debug information for it, as we will emit it later. */ |
d2e5ee5c | 8091 | if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl |
d0f062fb | 8092 | && !COMPLETE_TYPE_P (TREE_TYPE (decl))) |
cffa8729 MS |
8093 | TYPE_DECL_SUPPRESS_DEBUG (decl) = 1; |
8094 | ||
3e411c3f | 8095 | rest_of_decl_compilation (decl, NULL, |
5566b478 | 8096 | DECL_CONTEXT (decl) == NULL_TREE, at_eof); |
8d08fdba MS |
8097 | goto finish_end; |
8098 | } | |
3e41d13b | 8099 | |
8d08fdba | 8100 | if (TREE_CODE (decl) != FUNCTION_DECL) |
3e41d13b | 8101 | ttype = target_type (type); |
8d08fdba MS |
8102 | |
8103 | if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl) | |
8104 | && TYPE_NEEDS_CONSTRUCTING (type)) | |
8105 | { | |
8d08fdba MS |
8106 | /* Currently, GNU C++ puts constants in text space, making them |
8107 | impossible to initialize. In the future, one would hope for | |
8108 | an operating system which understood the difference between | |
8109 | initialization and the running of a program. */ | |
8110 | was_readonly = 1; | |
8111 | TREE_READONLY (decl) = 0; | |
8112 | } | |
8113 | ||
27778b73 | 8114 | if (TREE_CODE (decl) == FIELD_DECL && asmspec) |
8d08fdba | 8115 | { |
27778b73 MM |
8116 | /* This must override the asm specifier which was placed by |
8117 | grokclassfn. Lay this out fresh. */ | |
19e7881c | 8118 | SET_DECL_RTL (TREE_TYPE (decl), NULL_RTX); |
92643fea | 8119 | SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec)); |
6c418184 | 8120 | make_decl_rtl (decl, asmspec); |
8d08fdba | 8121 | } |
17bbb839 MM |
8122 | else if (TREE_CODE (decl) == VAR_DECL) |
8123 | { | |
8124 | /* Only PODs can have thread-local storage. Other types may require | |
8125 | various kinds of non-trivial initialization. */ | |
8126 | if (DECL_THREAD_LOCAL (decl) && !pod_type_p (TREE_TYPE (decl))) | |
8127 | error ("`%D' cannot be thread-local because it has non-POD type `%T'", | |
8128 | decl, TREE_TYPE (decl)); | |
8129 | /* Convert the initializer to the type of DECL, if we have not | |
8130 | already initialized DECL. */ | |
8131 | if (!DECL_INITIALIZED_P (decl) | |
8132 | /* If !DECL_EXTERNAL then DECL is being defined. In the | |
8e3df2de | 8133 | case of a static data member initialized inside the |
17bbb839 MM |
8134 | class-specifier, there can be an initializer even if DECL |
8135 | is *not* defined. */ | |
8136 | && (!DECL_EXTERNAL (decl) || init)) | |
8137 | { | |
8e3df2de | 8138 | init = check_initializer (decl, init, flags); |
b4c20e52 MM |
8139 | /* Thread-local storage cannot be dynamically initialized. */ |
8140 | if (DECL_THREAD_LOCAL (decl) && init) | |
8141 | { | |
8142 | error ("`%D' is thread-local and so cannot be dynamically " | |
8143 | "initialized", decl); | |
8144 | init = NULL_TREE; | |
8145 | } | |
17bbb839 MM |
8146 | /* Handle: |
8147 | ||
8148 | [dcl.init] | |
8149 | ||
8150 | The memory occupied by any object of static storage | |
8151 | duration is zero-initialized at program startup before | |
8152 | any other initialization takes place. | |
8153 | ||
8154 | We cannot create an appropriate initializer until after | |
8155 | the type of DECL is finalized. If DECL_INITIAL is set, | |
8156 | then the DECL is statically initialized, and any | |
8157 | necessary zero-initialization has already been performed. */ | |
8158 | if (TREE_STATIC (decl) && !DECL_INITIAL (decl)) | |
8159 | DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl), | |
1cb8292f | 8160 | /*nelts=*/NULL_TREE, |
17bbb839 MM |
8161 | /*static_storage_p=*/true); |
8162 | /* Remember that the initialization for this variable has | |
8163 | taken place. */ | |
8164 | DECL_INITIALIZED_P (decl) = 1; | |
8165 | } | |
d881ff25 MM |
8166 | /* If the variable has an array type, lay out the type, even if |
8167 | there is no initializer. It is valid to index through the | |
8168 | array, and we must get TYPE_ALIGN set correctly on the array | |
8169 | type. */ | |
8170 | else if (TREE_CODE (type) == ARRAY_TYPE) | |
8171 | layout_type (type); | |
17bbb839 | 8172 | } |
3e41d13b | 8173 | |
57cf57fb JM |
8174 | /* Add this declaration to the statement-tree. This needs to happen |
8175 | after the call to check_initializer so that the DECL_STMT for a | |
8176 | reference temp is added before the DECL_STMT for the reference itself. */ | |
170b020f | 8177 | if (at_function_scope_p ()) |
08ac397c JM |
8178 | add_decl_stmt (decl); |
8179 | ||
8d08fdba | 8180 | if (TREE_CODE (decl) == VAR_DECL) |
57b52417 | 8181 | layout_var_decl (decl); |
8d08fdba MS |
8182 | |
8183 | /* Output the assembler code and/or RTL code for variables and functions, | |
8184 | unless the type is an undefined structure or union. | |
8185 | If not, it will get done when the type is completed. */ | |
170b020f | 8186 | if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL) |
8d08fdba | 8187 | { |
27778b73 MM |
8188 | if (TREE_CODE (decl) == VAR_DECL) |
8189 | maybe_commonize_var (decl); | |
8d08fdba | 8190 | |
27778b73 | 8191 | make_rtl_for_nonlocal_decl (decl, init, asmspec); |
8d08fdba | 8192 | |
68642fb6 | 8193 | if (TREE_CODE (type) == FUNCTION_TYPE |
27778b73 | 8194 | || TREE_CODE (type) == METHOD_TYPE) |
68642fb6 | 8195 | abstract_virtuals_error (decl, |
27778b73 | 8196 | strip_array_types (TREE_TYPE (type))); |
68642fb6 | 8197 | else |
27778b73 | 8198 | abstract_virtuals_error (decl, strip_array_types (type)); |
8d08fdba | 8199 | |
8e3df2de MM |
8200 | if (TREE_CODE (decl) == FUNCTION_DECL |
8201 | || TREE_TYPE (decl) == error_mark_node) | |
8202 | /* No initialization required. */ | |
faae18ab | 8203 | ; |
67d743fe MS |
8204 | else if (DECL_EXTERNAL (decl) |
8205 | && ! (DECL_LANG_SPECIFIC (decl) | |
8206 | && DECL_NOT_REALLY_EXTERN (decl))) | |
5566b478 MS |
8207 | { |
8208 | if (init) | |
8209 | DECL_INITIAL (decl) = init; | |
8210 | } | |
8e3df2de | 8211 | else |
8d08fdba | 8212 | { |
8e3df2de MM |
8213 | /* A variable definition. */ |
8214 | if (DECL_FUNCTION_SCOPE_P (decl)) | |
24bef158 | 8215 | { |
8e3df2de MM |
8216 | /* This is a local declaration. */ |
8217 | if (doing_semantic_analysis_p ()) | |
8218 | maybe_inject_for_scope_var (decl); | |
8219 | /* Initialize the local variable. */ | |
8220 | if (processing_template_decl) | |
8221 | { | |
8222 | if (init || DECL_INITIAL (decl) == error_mark_node) | |
8223 | DECL_INITIAL (decl) = init; | |
8224 | } | |
8225 | else if (!TREE_STATIC (decl)) | |
8226 | initialize_local_var (decl, init); | |
24bef158 | 8227 | } |
8e3df2de MM |
8228 | |
8229 | if (TREE_STATIC (decl)) | |
9ed9e79a MM |
8230 | expand_static_init (decl, init); |
8231 | } | |
8d08fdba MS |
8232 | finish_end0: |
8233 | ||
8234 | /* Undo call to `pushclass' that was done in `start_decl' | |
8235 | due to initialization of qualified member variable. | |
8236 | I.e., Foo::x = 10; */ | |
8237 | { | |
4f1c5b7d | 8238 | tree context = CP_DECL_CONTEXT (decl); |
8d08fdba | 8239 | if (context |
2f939d94 | 8240 | && TYPE_P (context) |
8d08fdba MS |
8241 | && (TREE_CODE (decl) == VAR_DECL |
8242 | /* We also have a pushclass done that we need to undo here | |
8243 | if we're at top level and declare a method. */ | |
5566b478 MS |
8244 | || TREE_CODE (decl) == FUNCTION_DECL) |
8245 | /* If size hasn't been set, we're still defining it, | |
8246 | and therefore inside the class body; don't pop | |
8247 | the binding level.. */ | |
d0f062fb | 8248 | && COMPLETE_TYPE_P (context) |
5566b478 | 8249 | && context == current_class_type) |
6b400b21 | 8250 | pop_nested_class (); |
8d08fdba MS |
8251 | } |
8252 | } | |
8253 | ||
8254 | finish_end: | |
8255 | ||
8d08fdba MS |
8256 | if (was_readonly) |
8257 | TREE_READONLY (decl) = 1; | |
8d08fdba MS |
8258 | } |
8259 | ||
82580166 | 8260 | /* This is here for a midend callback from c-common.c */ |
e92cc029 | 8261 | |
82580166 | 8262 | void |
11f6b451 | 8263 | finish_decl (tree decl, tree init, tree asmspec_tree) |
82580166 | 8264 | { |
cd9f6678 | 8265 | cp_finish_decl (decl, init, asmspec_tree, 0); |
82580166 MS |
8266 | } |
8267 | ||
db4283a0 MM |
8268 | /* Returns a declaration for a VAR_DECL as if: |
8269 | ||
8270 | extern "C" TYPE NAME; | |
8271 | ||
8272 | had been seen. Used to create compiler-generated global | |
8273 | variables. */ | |
8274 | ||
8275 | tree | |
11f6b451 | 8276 | declare_global_var (tree name, tree type) |
db4283a0 MM |
8277 | { |
8278 | tree decl; | |
8279 | ||
8280 | push_to_top_level (); | |
8281 | decl = build_decl (VAR_DECL, name, type); | |
8282 | TREE_PUBLIC (decl) = 1; | |
8283 | DECL_EXTERNAL (decl) = 1; | |
8284 | DECL_ARTIFICIAL (decl) = 1; | |
8285 | pushdecl (decl); | |
8286 | cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0); | |
8287 | pop_from_top_level (); | |
8288 | ||
8289 | return decl; | |
8290 | } | |
8291 | ||
8292 | /* Returns a pointer to the `atexit' function. Note that if | |
838dfd8a | 8293 | FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new |
db4283a0 MM |
8294 | `__cxa_atexit' function specified in the IA64 C++ ABI. */ |
8295 | ||
8296 | static tree | |
11f6b451 | 8297 | get_atexit_node (void) |
db4283a0 MM |
8298 | { |
8299 | tree atexit_fndecl; | |
8300 | tree arg_types; | |
8301 | tree fn_type; | |
8302 | tree fn_ptr_type; | |
8303 | const char *name; | |
8304 | ||
8305 | if (atexit_node) | |
8306 | return atexit_node; | |
8307 | ||
8308 | if (flag_use_cxa_atexit) | |
8309 | { | |
8310 | /* The declaration for `__cxa_atexit' is: | |
8311 | ||
8312 | int __cxa_atexit (void (*)(void *), void *, void *) | |
8313 | ||
8314 | We build up the argument types and then then function type | |
8315 | itself. */ | |
68642fb6 | 8316 | |
db4283a0 MM |
8317 | /* First, build the pointer-to-function type for the first |
8318 | argument. */ | |
8319 | arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node); | |
8320 | fn_type = build_function_type (void_type_node, arg_types); | |
8321 | fn_ptr_type = build_pointer_type (fn_type); | |
8322 | /* Then, build the rest of the argument types. */ | |
8323 | arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node); | |
8324 | arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types); | |
8325 | arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types); | |
8326 | /* And the final __cxa_atexit type. */ | |
8327 | fn_type = build_function_type (integer_type_node, arg_types); | |
8328 | fn_ptr_type = build_pointer_type (fn_type); | |
8329 | name = "__cxa_atexit"; | |
8330 | } | |
8331 | else | |
8332 | { | |
8333 | /* The declaration for `atexit' is: | |
68642fb6 | 8334 | |
db4283a0 MM |
8335 | int atexit (void (*)()); |
8336 | ||
8337 | We build up the argument types and then then function type | |
8338 | itself. */ | |
8339 | fn_type = build_function_type (void_type_node, void_list_node); | |
8340 | fn_ptr_type = build_pointer_type (fn_type); | |
8341 | arg_types = tree_cons (NULL_TREE, fn_ptr_type, void_list_node); | |
8342 | /* Build the final atexit type. */ | |
8343 | fn_type = build_function_type (integer_type_node, arg_types); | |
8344 | name = "atexit"; | |
8345 | } | |
8346 | ||
8347 | /* Now, build the function declaration. */ | |
8348 | push_lang_context (lang_name_c); | |
0c11ada6 | 8349 | atexit_fndecl = build_library_fn_ptr (name, fn_type); |
db4283a0 MM |
8350 | mark_used (atexit_fndecl); |
8351 | pop_lang_context (); | |
8352 | atexit_node = default_conversion (atexit_fndecl); | |
8353 | ||
8354 | return atexit_node; | |
8355 | } | |
8356 | ||
8357 | /* Returns the __dso_handle VAR_DECL. */ | |
8358 | ||
8359 | static tree | |
11f6b451 | 8360 | get_dso_handle_node (void) |
db4283a0 MM |
8361 | { |
8362 | if (dso_handle_node) | |
8363 | return dso_handle_node; | |
8364 | ||
8365 | /* Declare the variable. */ | |
8366 | dso_handle_node = declare_global_var (get_identifier ("__dso_handle"), | |
8367 | ptr_type_node); | |
8368 | ||
8369 | return dso_handle_node; | |
8370 | } | |
8371 | ||
8372 | /* Begin a new function with internal linkage whose job will be simply | |
8373 | to destroy some particular variable. */ | |
8374 | ||
d192828a GK |
8375 | static GTY(()) int start_cleanup_cnt; |
8376 | ||
db4283a0 | 8377 | static tree |
11f6b451 | 8378 | start_cleanup_fn (void) |
db4283a0 | 8379 | { |
23ccd1f3 | 8380 | int old_interface_only = interface_only; |
db4283a0 MM |
8381 | int old_interface_unknown = interface_unknown; |
8382 | char name[32]; | |
8383 | tree parmtypes; | |
8384 | tree fntype; | |
8385 | tree fndecl; | |
8386 | ||
8387 | push_to_top_level (); | |
8388 | ||
8389 | /* No need to mangle this. */ | |
8390 | push_lang_context (lang_name_c); | |
8391 | ||
23ccd1f3 | 8392 | interface_only = 0; |
db4283a0 MM |
8393 | interface_unknown = 1; |
8394 | ||
8395 | /* Build the parameter-types. */ | |
8396 | parmtypes = void_list_node; | |
8397 | /* Functions passed to __cxa_atexit take an additional parameter. | |
8398 | We'll just ignore it. After we implement the new calling | |
8399 | convention for destructors, we can eliminate the use of | |
8400 | additional cleanup functions entirely in the -fnew-abi case. */ | |
8401 | if (flag_use_cxa_atexit) | |
8402 | parmtypes = tree_cons (NULL_TREE, ptr_type_node, parmtypes); | |
8403 | /* Build the function type itself. */ | |
8404 | fntype = build_function_type (void_type_node, parmtypes); | |
8405 | /* Build the name of the function. */ | |
d192828a | 8406 | sprintf (name, "__tcf_%d", start_cleanup_cnt++); |
db4283a0 MM |
8407 | /* Build the function declaration. */ |
8408 | fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype); | |
8409 | /* It's a function with internal linkage, generated by the | |
8410 | compiler. */ | |
8411 | TREE_PUBLIC (fndecl) = 0; | |
8412 | DECL_ARTIFICIAL (fndecl) = 1; | |
5a728aca MM |
8413 | /* Make the function `inline' so that it is only emitted if it is |
8414 | actually needed. It is unlikely that it will be inlined, since | |
aba649ba | 8415 | it is only called via a function pointer, but we avoid unnecessary |
5a728aca MM |
8416 | emissions this way. */ |
8417 | DECL_INLINE (fndecl) = 1; | |
db4283a0 MM |
8418 | /* Build the parameter. */ |
8419 | if (flag_use_cxa_atexit) | |
8420 | { | |
8421 | tree parmdecl; | |
8422 | ||
8e51619a | 8423 | parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node); |
db4283a0 | 8424 | DECL_CONTEXT (parmdecl) = fndecl; |
db4283a0 MM |
8425 | TREE_USED (parmdecl) = 1; |
8426 | DECL_ARGUMENTS (fndecl) = parmdecl; | |
8427 | } | |
8428 | ||
09ed39ad | 8429 | pushdecl (fndecl); |
db4283a0 | 8430 | start_function (/*specs=*/NULL_TREE, fndecl, NULL_TREE, SF_PRE_PARSED); |
db4283a0 MM |
8431 | |
8432 | interface_unknown = old_interface_unknown; | |
23ccd1f3 | 8433 | interface_only = old_interface_only; |
db4283a0 MM |
8434 | |
8435 | pop_lang_context (); | |
8436 | ||
8437 | return current_function_decl; | |
8438 | } | |
8439 | ||
8440 | /* Finish the cleanup function begun by start_cleanup_fn. */ | |
8441 | ||
8442 | static void | |
11f6b451 | 8443 | end_cleanup_fn (void) |
db4283a0 | 8444 | { |
0acf7199 | 8445 | expand_body (finish_function (0)); |
db4283a0 MM |
8446 | |
8447 | pop_from_top_level (); | |
8448 | } | |
8449 | ||
bf419747 MM |
8450 | /* Generate code to handle the destruction of DECL, an object with |
8451 | static storage duration. */ | |
f0105ed3 | 8452 | |
bf419747 | 8453 | void |
11f6b451 | 8454 | register_dtor_fn (tree decl) |
f0105ed3 | 8455 | { |
db4283a0 | 8456 | tree cleanup; |
f0105ed3 | 8457 | tree compound_stmt; |
db4283a0 MM |
8458 | tree args; |
8459 | tree fcall; | |
f0105ed3 | 8460 | |
834c6dff | 8461 | if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl))) |
bf419747 MM |
8462 | return; |
8463 | ||
f0105ed3 MM |
8464 | /* Call build_cleanup before we enter the anonymous function so that |
8465 | any access checks will be done relative to the current scope, | |
8466 | rather than the scope of the anonymous function. */ | |
8467 | build_cleanup (decl); | |
8468 | ||
8469 | /* Now start the function. */ | |
db4283a0 | 8470 | cleanup = start_cleanup_fn (); |
f0105ed3 MM |
8471 | |
8472 | /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer | |
8473 | to the original function, rather than the anonymous one. That | |
8474 | will make the back-end think that nested functions are in use, | |
8475 | which causes confusion. */ | |
78757caa KL |
8476 | |
8477 | push_deferring_access_checks (dk_no_check); | |
f0105ed3 | 8478 | fcall = build_cleanup (decl); |
78757caa | 8479 | pop_deferring_access_checks (); |
f0105ed3 MM |
8480 | |
8481 | /* Create the body of the anonymous function. */ | |
8482 | compound_stmt = begin_compound_stmt (/*has_no_scope=*/0); | |
8483 | finish_expr_stmt (fcall); | |
8484 | finish_compound_stmt (/*has_no_scope=*/0, compound_stmt); | |
db4283a0 | 8485 | end_cleanup_fn (); |
f0105ed3 MM |
8486 | |
8487 | /* Call atexit with the cleanup function. */ | |
dffd7eb6 | 8488 | cxx_mark_addressable (cleanup); |
bb4f6e6b | 8489 | mark_used (cleanup); |
f0105ed3 | 8490 | cleanup = build_unary_op (ADDR_EXPR, cleanup, 0); |
db4283a0 MM |
8491 | if (flag_use_cxa_atexit) |
8492 | { | |
0f268005 MM |
8493 | args = tree_cons (NULL_TREE, |
8494 | build_unary_op (ADDR_EXPR, get_dso_handle_node (), 0), | |
8495 | NULL_TREE); | |
db4283a0 MM |
8496 | args = tree_cons (NULL_TREE, null_pointer_node, args); |
8497 | args = tree_cons (NULL_TREE, cleanup, args); | |
8498 | } | |
8499 | else | |
8500 | args = tree_cons (NULL_TREE, cleanup, NULL_TREE); | |
8501 | finish_expr_stmt (build_function_call (get_atexit_node (), args)); | |
f0105ed3 MM |
8502 | } |
8503 | ||
8e3df2de MM |
8504 | /* DECL is a VAR_DECL with static storage duration. INIT, if present, |
8505 | is its initializer. Generate code to handle the construction | |
8506 | and destruction of DECL. */ | |
8507 | ||
8508 | static void | |
11f6b451 | 8509 | expand_static_init (tree decl, tree init) |
8d08fdba | 8510 | { |
8e3df2de MM |
8511 | tree oldstatic; |
8512 | ||
8513 | my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20021010); | |
8514 | my_friendly_assert (TREE_STATIC (decl), 20021010); | |
8515 | ||
8516 | /* Some variables require no initialization. */ | |
8517 | if (!init | |
8518 | && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)) | |
8519 | && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl))) | |
8520 | return; | |
8521 | ||
8522 | oldstatic = value_member (decl, static_aggregates); | |
a4443a08 | 8523 | |
8d08fdba MS |
8524 | if (oldstatic) |
8525 | { | |
8526 | if (TREE_PURPOSE (oldstatic) && init != NULL_TREE) | |
33bd39a2 | 8527 | error ("multiple initializations given for `%D'", decl); |
8d08fdba | 8528 | } |
0aafb128 | 8529 | else if (! toplevel_bindings_p ()) |
8d08fdba MS |
8530 | { |
8531 | /* Emit code to perform this initialization but once. */ | |
b7b8bcd2 | 8532 | tree if_stmt; |
f0105ed3 | 8533 | tree then_clause; |
f1dedc31 | 8534 | tree assignment; |
c395453c MM |
8535 | tree guard; |
8536 | tree guard_init; | |
8d08fdba | 8537 | |
2036a15c MM |
8538 | /* Emit code to perform this initialization but once. This code |
8539 | looks like: | |
8540 | ||
c395453c MM |
8541 | static int guard = 0; |
8542 | if (!guard) { | |
2036a15c | 8543 | // Do initialization. |
c395453c | 8544 | guard = 1; |
2036a15c MM |
8545 | // Register variable for destruction at end of program. |
8546 | } | |
8547 | ||
8548 | Note that the `temp' variable is only set to 1 *after* the | |
8549 | initialization is complete. This ensures that an exception, | |
8550 | thrown during the construction, will cause the variable to | |
8551 | reinitialized when we pass through this code again, as per: | |
68642fb6 | 8552 | |
2036a15c MM |
8553 | [stmt.dcl] |
8554 | ||
8555 | If the initialization exits by throwing an exception, the | |
8556 | initialization is not complete, so it will be tried again | |
8557 | the next time control enters the declaration. | |
8558 | ||
8559 | In theory, this process should be thread-safe, too; multiple | |
8560 | threads should not be able to initialize the variable more | |
8561 | than once. We don't yet attempt to ensure thread-safety. */ | |
c395453c MM |
8562 | |
8563 | /* Create the guard variable. */ | |
8564 | guard = get_guard (decl); | |
2036a15c MM |
8565 | |
8566 | /* Begin the conditional initialization. */ | |
b7b8bcd2 | 8567 | if_stmt = begin_if_stmt (); |
c395453c | 8568 | finish_if_stmt_cond (get_guard_cond (guard), if_stmt); |
f0105ed3 | 8569 | then_clause = begin_compound_stmt (/*has_no_scope=*/0); |
72b7eeff | 8570 | |
2036a15c | 8571 | /* Do the initialization itself. */ |
8e3df2de | 8572 | assignment = init ? init : NULL_TREE; |
f1dedc31 MM |
8573 | |
8574 | /* Once the assignment is complete, set TEMP to 1. Since the | |
8575 | construction of the static object is complete at this point, | |
8576 | we want to make sure TEMP is set to 1 even if a temporary | |
8577 | constructed during the initialization throws an exception | |
8578 | when it is destroyed. So, we combine the initialization and | |
8579 | the assignment to TEMP into a single expression, ensuring | |
8580 | that when we call finish_expr_stmt the cleanups will not be | |
8581 | run until after TEMP is set to 1. */ | |
c395453c | 8582 | guard_init = set_guard (guard); |
f1dedc31 MM |
8583 | if (assignment) |
8584 | { | |
8585 | assignment = tree_cons (NULL_TREE, assignment, | |
68642fb6 | 8586 | build_tree_list (NULL_TREE, |
c395453c | 8587 | guard_init)); |
f1dedc31 MM |
8588 | assignment = build_compound_expr (assignment); |
8589 | } | |
8590 | else | |
c395453c | 8591 | assignment = guard_init; |
f1dedc31 | 8592 | finish_expr_stmt (assignment); |
72b7eeff | 8593 | |
2036a15c MM |
8594 | /* Use atexit to register a function for destroying this static |
8595 | variable. */ | |
bf419747 | 8596 | register_dtor_fn (decl); |
72b7eeff | 8597 | |
f0105ed3 | 8598 | finish_compound_stmt (/*has_no_scope=*/0, then_clause); |
b7b8bcd2 MM |
8599 | finish_then_clause (if_stmt); |
8600 | finish_if_stmt (); | |
8d08fdba MS |
8601 | } |
8602 | else | |
bbd15aac | 8603 | static_aggregates = tree_cons (init, decl, static_aggregates); |
8d08fdba | 8604 | } |
3c5c0849 MM |
8605 | |
8606 | /* Finish the declaration of a catch-parameter. */ | |
8607 | ||
b35d4555 | 8608 | tree |
11f6b451 | 8609 | start_handler_parms (tree declspecs, tree declarator) |
3c5c0849 MM |
8610 | { |
8611 | tree decl; | |
8612 | if (declspecs) | |
8613 | { | |
8614 | decl = grokdeclarator (declarator, declspecs, CATCHPARM, | |
91d231cb | 8615 | 1, NULL); |
3c5c0849 MM |
8616 | if (decl == NULL_TREE) |
8617 | error ("invalid catch parameter"); | |
8618 | } | |
8619 | else | |
8620 | decl = NULL_TREE; | |
b35d4555 MM |
8621 | |
8622 | return decl; | |
3c5c0849 MM |
8623 | } |
8624 | ||
8d08fdba MS |
8625 | \f |
8626 | /* Make TYPE a complete type based on INITIAL_VALUE. | |
8627 | Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered, | |
6ab5c740 | 8628 | 2 if there was no information (in which case assume 0 if DO_DEFAULT). */ |
8d08fdba MS |
8629 | |
8630 | int | |
11f6b451 | 8631 | complete_array_type (tree type, tree initial_value, int do_default) |
8d08fdba MS |
8632 | { |
8633 | register tree maxindex = NULL_TREE; | |
8634 | int value = 0; | |
68642fb6 | 8635 | |
8d08fdba MS |
8636 | if (initial_value) |
8637 | { | |
7b019c19 MM |
8638 | /* An array of character type can be initialized from a |
8639 | brace-enclosed string constant. */ | |
8640 | if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))) | |
8641 | && TREE_CODE (initial_value) == CONSTRUCTOR | |
8642 | && CONSTRUCTOR_ELTS (initial_value) | |
8643 | && (TREE_CODE (TREE_VALUE (CONSTRUCTOR_ELTS (initial_value))) | |
8644 | == STRING_CST) | |
8645 | && TREE_CHAIN (CONSTRUCTOR_ELTS (initial_value)) == NULL_TREE) | |
8646 | initial_value = TREE_VALUE (CONSTRUCTOR_ELTS (initial_value)); | |
8647 | ||
8648 | /* Note MAXINDEX is really the maximum index, one less than the | |
8649 | size. */ | |
8d08fdba | 8650 | if (TREE_CODE (initial_value) == STRING_CST) |
e1cd6e56 MS |
8651 | { |
8652 | int eltsize | |
8653 | = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value))); | |
8654 | maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value) | |
8655 | / eltsize) - 1, 0); | |
8656 | } | |
8d08fdba MS |
8657 | else if (TREE_CODE (initial_value) == CONSTRUCTOR) |
8658 | { | |
e1cd6e56 | 8659 | tree elts = CONSTRUCTOR_ELTS (initial_value); |
fed3cef0 RK |
8660 | |
8661 | maxindex = ssize_int (-1); | |
e1cd6e56 MS |
8662 | for (; elts; elts = TREE_CHAIN (elts)) |
8663 | { | |
8664 | if (TREE_PURPOSE (elts)) | |
8665 | maxindex = TREE_PURPOSE (elts); | |
8666 | else | |
fed3cef0 | 8667 | maxindex = size_binop (PLUS_EXPR, maxindex, ssize_int (1)); |
e1cd6e56 MS |
8668 | } |
8669 | maxindex = copy_node (maxindex); | |
8d08fdba MS |
8670 | } |
8671 | else | |
8672 | { | |
8673 | /* Make an error message unless that happened already. */ | |
8674 | if (initial_value != error_mark_node) | |
8675 | value = 1; | |
0db982be ML |
8676 | else |
8677 | initial_value = NULL_TREE; | |
8d08fdba MS |
8678 | |
8679 | /* Prevent further error messages. */ | |
8680 | maxindex = build_int_2 (0, 0); | |
8681 | } | |
8682 | } | |
8683 | ||
8684 | if (!maxindex) | |
8685 | { | |
8686 | if (do_default) | |
8687 | maxindex = build_int_2 (0, 0); | |
8688 | value = 2; | |
8689 | } | |
8690 | ||
8691 | if (maxindex) | |
8692 | { | |
51c184be | 8693 | tree itype; |
6ab5c740 NS |
8694 | tree domain; |
8695 | ||
8696 | domain = build_index_type (maxindex); | |
8697 | TYPE_DOMAIN (type) = domain; | |
51c184be | 8698 | |
dff6b454 | 8699 | if (! TREE_TYPE (maxindex)) |
6ab5c740 | 8700 | TREE_TYPE (maxindex) = domain; |
51c184be MS |
8701 | if (initial_value) |
8702 | itype = TREE_TYPE (initial_value); | |
8703 | else | |
8704 | itype = NULL; | |
8705 | if (itype && !TYPE_DOMAIN (itype)) | |
6ab5c740 | 8706 | TYPE_DOMAIN (itype) = domain; |
dff6b454 RK |
8707 | /* The type of the main variant should never be used for arrays |
8708 | of different sizes. It should only ever be completed with the | |
8709 | size of the array. */ | |
8710 | if (! TYPE_DOMAIN (TYPE_MAIN_VARIANT (type))) | |
6ab5c740 | 8711 | TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)) = domain; |
8d08fdba MS |
8712 | } |
8713 | ||
8714 | /* Lay out the type now that we can get the real answer. */ | |
8715 | ||
8716 | layout_type (type); | |
8717 | ||
8718 | return value; | |
8719 | } | |
8720 | \f | |
8721 | /* Return zero if something is declared to be a member of type | |
8722 | CTYPE when in the context of CUR_TYPE. STRING is the error | |
8723 | message to print in that case. Otherwise, quietly return 1. */ | |
e92cc029 | 8724 | |
8d08fdba | 8725 | static int |
11f6b451 | 8726 | member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags) |
8d08fdba MS |
8727 | { |
8728 | if (ctype && ctype != cur_type) | |
8729 | { | |
4dacf5bd | 8730 | if (flags == DTOR_FLAG) |
33bd39a2 | 8731 | error ("destructor for alien class `%T' cannot be a member", |
2ae7bada | 8732 | ctype); |
4dacf5bd | 8733 | else |
33bd39a2 | 8734 | error ("constructor for alien class `%T' cannot be a member", |
2ae7bada | 8735 | ctype); |
8d08fdba MS |
8736 | return 0; |
8737 | } | |
8738 | return 1; | |
8739 | } | |
8740 | \f | |
8741 | /* Subroutine of `grokdeclarator'. */ | |
8742 | ||
8743 | /* Generate errors possibly applicable for a given set of specifiers. | |
8744 | This is for ARM $7.1.2. */ | |
e92cc029 | 8745 | |
8d08fdba | 8746 | static void |
11f6b451 NN |
8747 | bad_specifiers (tree object, |
8748 | const char* type, | |
8749 | int virtualp, | |
8750 | int quals, | |
45225759 | 8751 | int inlinep, |
11f6b451 | 8752 | int friendp, |
45225759 | 8753 | int raises) |
8d08fdba MS |
8754 | { |
8755 | if (virtualp) | |
33bd39a2 | 8756 | error ("`%D' declared as a `virtual' %s", object, type); |
8d08fdba | 8757 | if (inlinep) |
33bd39a2 | 8758 | error ("`%D' declared as an `inline' %s", object, type); |
8d08fdba | 8759 | if (quals) |
33bd39a2 | 8760 | error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration", |
a28e3c7f | 8761 | object, type); |
8d08fdba | 8762 | if (friendp) |
f8e55f34 | 8763 | cp_error_at ("`%D' declared as a friend", object); |
e1be26f4 RS |
8764 | if (raises |
8765 | && (TREE_CODE (object) == TYPE_DECL | |
8766 | || (!TYPE_PTRFN_P (TREE_TYPE (object)) | |
742a37d5 | 8767 | && !TYPE_REFFN_P (TREE_TYPE (object)) |
e1be26f4 | 8768 | && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object))))) |
f8e55f34 | 8769 | cp_error_at ("`%D' declared with an exception specification", object); |
8d08fdba MS |
8770 | } |
8771 | ||
8772 | /* CTYPE is class type, or null if non-class. | |
8773 | TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE | |
8774 | or METHOD_TYPE. | |
8775 | DECLARATOR is the function's name. | |
8776 | VIRTUALP is truthvalue of whether the function is virtual or not. | |
8777 | FLAGS are to be passed through to `grokclassfn'. | |
8778 | QUALS are qualifiers indicating whether the function is `const' | |
8779 | or `volatile'. | |
8780 | RAISES is a list of exceptions that this function can raise. | |
8781 | CHECK is 1 if we must find this method in CTYPE, 0 if we should | |
68642fb6 | 8782 | not look, and -1 if we should not call `grokclassfn' at all. |
3ddfb0e6 | 8783 | |
20496fa2 | 8784 | Returns `NULL_TREE' if something goes wrong, after issuing |
3ddfb0e6 | 8785 | applicable error messages. */ |
e92cc029 | 8786 | |
8d08fdba | 8787 | static tree |
11f6b451 NN |
8788 | grokfndecl (tree ctype, |
8789 | tree type, | |
8790 | tree declarator, | |
8791 | tree orig_declarator, | |
8792 | int virtualp, | |
8793 | enum overload_flags flags, | |
8794 | tree quals, | |
8795 | tree raises, | |
8796 | int check, | |
8797 | int friendp, | |
8798 | int publicp, | |
8799 | int inlinep, | |
8800 | int funcdef_flag, | |
8801 | int template_count, | |
8802 | tree in_namespace) | |
8d08fdba | 8803 | { |
1951a1b6 | 8804 | tree decl; |
8d08fdba | 8805 | int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE; |
856216bb | 8806 | int has_default_arg = 0; |
42976354 | 8807 | tree t; |
8d08fdba | 8808 | |
8d08fdba | 8809 | if (raises) |
271e6f02 | 8810 | type = build_exception_variant (type, raises); |
c11b6f21 | 8811 | |
8d08fdba | 8812 | decl = build_lang_decl (FUNCTION_DECL, declarator, type); |
a1c65f9f | 8813 | /* Propagate volatile out from type to decl. */ |
8d08fdba | 8814 | if (TYPE_VOLATILE (type)) |
893de33c | 8815 | TREE_THIS_VOLATILE (decl) = 1; |
8d08fdba | 8816 | |
79c4d4b7 | 8817 | /* If this decl has namespace scope, set that up. */ |
2c73f9f5 | 8818 | if (in_namespace) |
b262d64c | 8819 | set_decl_namespace (decl, in_namespace, friendp); |
adae082f | 8820 | else if (!ctype) |
79c4d4b7 | 8821 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); |
2c73f9f5 | 8822 | |
0f8766b8 JM |
8823 | /* `main' and builtins have implicit 'C' linkage. */ |
8824 | if ((MAIN_NAME_P (declarator) | |
8825 | || (IDENTIFIER_LENGTH (declarator) > 10 | |
8826 | && IDENTIFIER_POINTER (declarator)[0] == '_' | |
8827 | && IDENTIFIER_POINTER (declarator)[1] == '_' | |
8828 | && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0)) | |
8829 | && current_lang_name == lang_name_cplusplus | |
94706a5c | 8830 | && ctype == NULL_TREE |
79c4d4b7 JM |
8831 | /* NULL_TREE means global namespace. */ |
8832 | && DECL_CONTEXT (decl) == NULL_TREE) | |
5d2ed28c | 8833 | SET_DECL_LANGUAGE (decl, lang_c); |
0f8766b8 | 8834 | |
8d08fdba MS |
8835 | /* Should probably propagate const out from type to decl I bet (mrs). */ |
8836 | if (staticp) | |
8837 | { | |
8838 | DECL_STATIC_FUNCTION_P (decl) = 1; | |
8839 | DECL_CONTEXT (decl) = ctype; | |
8d08fdba MS |
8840 | } |
8841 | ||
e76a2646 | 8842 | if (ctype) |
4f1c5b7d | 8843 | DECL_CONTEXT (decl) = ctype; |
e76a2646 | 8844 | |
0f8766b8 | 8845 | if (ctype == NULL_TREE && DECL_MAIN_P (decl)) |
faae18ab | 8846 | { |
848b92e1 | 8847 | if (processing_template_decl) |
cb9a3ff8 | 8848 | error ("cannot declare `::main' to be a template"); |
faae18ab | 8849 | if (inlinep) |
cb9a3ff8 | 8850 | error ("cannot declare `::main' to be inline"); |
f22967f3 | 8851 | if (!publicp) |
cb9a3ff8 | 8852 | error ("cannot declare `::main' to be static"); |
f22967f3 MM |
8853 | if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)), |
8854 | integer_type_node)) | |
8855 | error ("`main' must return `int'"); | |
faae18ab MS |
8856 | inlinep = 0; |
8857 | publicp = 1; | |
8858 | } | |
50a6dbd7 | 8859 | |
59e76fc6 JM |
8860 | /* Members of anonymous types and local classes have no linkage; make |
8861 | them internal. */ | |
1951a1b6 JM |
8862 | /* FIXME what if it gets a name from typedef? */ |
8863 | if (ctype && (TYPE_ANONYMOUS_P (ctype) | |
4f1c5b7d | 8864 | || decl_function_context (TYPE_MAIN_DECL (ctype)))) |
50a6dbd7 JM |
8865 | publicp = 0; |
8866 | ||
8867 | if (publicp) | |
8868 | { | |
8869 | /* [basic.link]: A name with no linkage (notably, the name of a class | |
8870 | or enumeration declared in a local scope) shall not be used to | |
8871 | declare an entity with linkage. | |
8872 | ||
50b97e0f | 8873 | Only check this for public decls for now. See core 319, 389. */ |
50a6dbd7 JM |
8874 | t = no_linkage_check (TREE_TYPE (decl)); |
8875 | if (t) | |
8876 | { | |
1951a1b6 | 8877 | if (TYPE_ANONYMOUS_P (t)) |
7f7c930e | 8878 | { |
eb68cb58 | 8879 | if (DECL_EXTERN_C_P (decl)) |
7f7c930e JM |
8880 | /* Allow this; it's pretty common in C. */; |
8881 | else | |
1951a1b6 | 8882 | { |
33bd39a2 | 8883 | pedwarn ("non-local function `%#D' uses anonymous type", |
1951a1b6 JM |
8884 | decl); |
8885 | if (DECL_ORIGINAL_TYPE (TYPE_NAME (t))) | |
8886 | cp_pedwarn_at ("\ | |
8887 | `%#D' does not refer to the unqualified type, so it is not used for linkage", | |
8888 | TYPE_NAME (t)); | |
8889 | } | |
7f7c930e | 8890 | } |
50a6dbd7 | 8891 | else |
33bd39a2 | 8892 | pedwarn ("non-local function `%#D' uses local type `%T'", |
cce2be43 | 8893 | decl, t); |
50a6dbd7 JM |
8894 | } |
8895 | } | |
8896 | ||
893de33c | 8897 | TREE_PUBLIC (decl) = publicp; |
faae18ab | 8898 | if (! publicp) |
893de33c JM |
8899 | { |
8900 | DECL_INTERFACE_KNOWN (decl) = 1; | |
8901 | DECL_NOT_REALLY_EXTERN (decl) = 1; | |
8902 | } | |
faae18ab | 8903 | |
bc522472 | 8904 | DID_INLINE_FUNC (decl) = 0; |
acc72c37 | 8905 | /* If the declaration was declared inline, mark it as such. */ |
faae18ab | 8906 | if (inlinep) |
acc72c37 MM |
8907 | DECL_DECLARED_INLINE_P (decl) = 1; |
8908 | /* We inline functions that are explicitly declared inline, or, when | |
8909 | the user explicitly asks us to, all functions. */ | |
bc522472 | 8910 | if (DECL_DECLARED_INLINE_P (decl)) |
acc72c37 | 8911 | DECL_INLINE (decl) = 1; |
9da32fe1 | 8912 | if (flag_inline_trees == 2 && !DECL_INLINE (decl) && funcdef_flag) |
bc522472 KG |
8913 | { |
8914 | DID_INLINE_FUNC (decl) = 1; | |
8915 | DECL_INLINE (decl) = 1; | |
8916 | } | |
8d08fdba MS |
8917 | |
8918 | DECL_EXTERNAL (decl) = 1; | |
8919 | if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE) | |
8920 | { | |
33bd39a2 | 8921 | error ("%smember function `%D' cannot have `%T' method qualifier", |
8d08fdba MS |
8922 | (ctype ? "static " : "non-"), decl, TREE_VALUE (quals)); |
8923 | quals = NULL_TREE; | |
8924 | } | |
8925 | ||
596ea4e5 | 8926 | if (IDENTIFIER_OPNAME_P (DECL_NAME (decl))) |
271e6f02 | 8927 | grok_op_properties (decl, friendp); |
8d08fdba | 8928 | |
4f1c5b7d | 8929 | if (ctype && decl_function_context (decl)) |
893de33c | 8930 | DECL_NO_STATIC_CHAIN (decl) = 1; |
e76a2646 | 8931 | |
42976354 BK |
8932 | for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t)) |
8933 | if (TREE_PURPOSE (t) | |
8934 | && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG) | |
8935 | { | |
856216bb | 8936 | has_default_arg = 1; |
42976354 BK |
8937 | break; |
8938 | } | |
8939 | ||
f9d94ea4 JM |
8940 | if (friendp |
8941 | && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR) | |
8942 | { | |
8943 | if (funcdef_flag) | |
33bd39a2 | 8944 | error |
8251199e | 8945 | ("defining explicit specialization `%D' in friend declaration", |
f9d94ea4 JM |
8946 | orig_declarator); |
8947 | else | |
8948 | { | |
76e57b45 NS |
8949 | tree fns = TREE_OPERAND (orig_declarator, 0); |
8950 | tree args = TREE_OPERAND (orig_declarator, 1); | |
d363e7bf | 8951 | |
7e2421f7 MM |
8952 | if (PROCESSING_REAL_TEMPLATE_DECL_P ()) |
8953 | { | |
8954 | /* Something like `template <class T> friend void f<T>()'. */ | |
33bd39a2 | 8955 | error ("invalid use of template-id `%D' in declaration of primary template", |
7e2421f7 | 8956 | orig_declarator); |
20496fa2 | 8957 | return NULL_TREE; |
7e2421f7 MM |
8958 | } |
8959 | ||
856216bb | 8960 | |
f9d94ea4 JM |
8961 | /* A friend declaration of the form friend void f<>(). Record |
8962 | the information in the TEMPLATE_ID_EXPR. */ | |
8963 | SET_DECL_IMPLICIT_INSTANTIATION (decl); | |
76e57b45 NS |
8964 | |
8965 | if (TREE_CODE (fns) == COMPONENT_REF) | |
8966 | { | |
8967 | /* Due to bison parser ickiness, we will have already looked | |
8968 | up an operator_name or PFUNCNAME within the current class | |
8969 | (see template_id in parse.y). If the current class contains | |
a1c65f9f | 8970 | such a name, we'll get a COMPONENT_REF here. Undo that. */ |
d363e7bf | 8971 | |
76e57b45 NS |
8972 | my_friendly_assert (TREE_TYPE (TREE_OPERAND (fns, 0)) |
8973 | == current_class_type, 20001120); | |
8974 | fns = TREE_OPERAND (fns, 1); | |
8975 | } | |
8976 | my_friendly_assert (TREE_CODE (fns) == IDENTIFIER_NODE | |
8977 | || TREE_CODE (fns) == LOOKUP_EXPR | |
8978 | || TREE_CODE (fns) == OVERLOAD, 20001120); | |
8979 | DECL_TEMPLATE_INFO (decl) = tree_cons (fns, args, NULL_TREE); | |
856216bb MM |
8980 | |
8981 | if (has_default_arg) | |
8982 | { | |
33bd39a2 | 8983 | error ("default arguments are not allowed in declaration of friend template specialization `%D'", |
856216bb MM |
8984 | decl); |
8985 | return NULL_TREE; | |
8986 | } | |
8987 | ||
8988 | if (inlinep) | |
8989 | { | |
33bd39a2 | 8990 | error ("`inline' is not allowed in declaration of friend template specialization `%D'", |
856216bb MM |
8991 | decl); |
8992 | return NULL_TREE; | |
8993 | } | |
f9d94ea4 | 8994 | } |
f84b4be9 | 8995 | } |
386b8a85 | 8996 | |
1eb0072d JM |
8997 | if (funcdef_flag) |
8998 | /* Make the init_value nonzero so pushdecl knows this is not | |
8999 | tentative. error_mark_node is replaced later with the BLOCK. */ | |
9000 | DECL_INITIAL (decl) = error_mark_node; | |
9001 | ||
93ca4ba7 | 9002 | if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl)) |
1660cb3a JM |
9003 | TREE_NOTHROW (decl) = 1; |
9004 | ||
75650646 | 9005 | /* Caller will do the rest of this. */ |
8d08fdba MS |
9006 | if (check < 0) |
9007 | return decl; | |
9008 | ||
a723baf1 | 9009 | if (flags == NO_SPECIAL && ctype && constructor_name_p (declarator, ctype)) |
74b846e0 MM |
9010 | DECL_CONSTRUCTOR_P (decl) = 1; |
9011 | ||
9012 | /* Function gets the ugly name, field gets the nice one. This call | |
9013 | may change the type of the function (because of default | |
9014 | parameters)! */ | |
9015 | if (ctype != NULL_TREE) | |
9016 | grokclassfn (ctype, decl, flags, quals); | |
9017 | ||
9018 | decl = check_explicit_specialization (orig_declarator, decl, | |
9019 | template_count, | |
9020 | 2 * (funcdef_flag != 0) + | |
9021 | 4 * (friendp != 0)); | |
9022 | if (decl == error_mark_node) | |
9023 | return NULL_TREE; | |
98c1c668 | 9024 | |
74b846e0 MM |
9025 | if (ctype != NULL_TREE |
9026 | && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl)) | |
9027 | && check) | |
8d08fdba | 9028 | { |
74b846e0 | 9029 | tree old_decl; |
8d08fdba | 9030 | |
74b846e0 | 9031 | old_decl = check_classfn (ctype, decl); |
8d08fdba | 9032 | |
74b846e0 MM |
9033 | if (old_decl && TREE_CODE (old_decl) == TEMPLATE_DECL) |
9034 | /* Because grokfndecl is always supposed to return a | |
9035 | FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT | |
9036 | here. We depend on our callers to figure out that its | |
9037 | really a template that's being returned. */ | |
9038 | old_decl = DECL_TEMPLATE_RESULT (old_decl); | |
6c30752f | 9039 | |
74b846e0 MM |
9040 | if (old_decl && DECL_STATIC_FUNCTION_P (old_decl) |
9041 | && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE) | |
5566b478 | 9042 | { |
74b846e0 MM |
9043 | /* Remove the `this' parm added by grokclassfn. |
9044 | XXX Isn't this done in start_function, too? */ | |
3afb32a4 | 9045 | revert_static_member_fn (decl); |
74b846e0 | 9046 | last_function_parms = TREE_CHAIN (last_function_parms); |
5566b478 | 9047 | } |
74b846e0 | 9048 | if (old_decl && DECL_ARTIFICIAL (old_decl)) |
33bd39a2 | 9049 | error ("definition of implicitly-declared `%D'", old_decl); |
8d08fdba | 9050 | |
74b846e0 | 9051 | if (old_decl) |
8d08fdba | 9052 | { |
316a2456 MM |
9053 | bool ok; |
9054 | ||
74b846e0 MM |
9055 | /* Since we've smashed OLD_DECL to its |
9056 | DECL_TEMPLATE_RESULT, we must do the same to DECL. */ | |
9057 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
9058 | decl = DECL_TEMPLATE_RESULT (decl); | |
9059 | ||
9060 | /* Attempt to merge the declarations. This can fail, in | |
0e339752 | 9061 | the case of some invalid specialization declarations. */ |
14d22dd6 | 9062 | push_scope (ctype); |
316a2456 | 9063 | ok = duplicate_decls (decl, old_decl); |
14d22dd6 | 9064 | pop_scope (ctype); |
316a2456 MM |
9065 | if (!ok) |
9066 | { | |
9067 | error ("no `%#D' member function declared in class `%T'", | |
9068 | decl, ctype); | |
9069 | return NULL_TREE; | |
9070 | } | |
74b846e0 | 9071 | return old_decl; |
8d08fdba MS |
9072 | } |
9073 | } | |
74b846e0 MM |
9074 | |
9075 | if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl)) | |
9076 | return NULL_TREE; | |
9077 | ||
9078 | if (ctype == NULL_TREE || check) | |
9079 | return decl; | |
9080 | ||
9081 | if (virtualp) | |
cbb40945 | 9082 | DECL_VIRTUAL_P (decl) = 1; |
74b846e0 | 9083 | |
8d08fdba MS |
9084 | return decl; |
9085 | } | |
9086 | ||
9e259dd1 MM |
9087 | /* Create a VAR_DECL named NAME with the indicated TYPE. |
9088 | ||
9089 | If SCOPE is non-NULL, it is the class type or namespace containing | |
9090 | the variable. If SCOPE is NULL, the variable should is created in | |
9091 | the innermost enclosings scope. */ | |
9092 | ||
8d08fdba | 9093 | static tree |
11f6b451 NN |
9094 | grokvardecl (tree type, |
9095 | tree name, | |
9096 | RID_BIT_TYPE * specbits_in, | |
9097 | int initialized, | |
9098 | int constp, | |
9099 | tree scope) | |
8d08fdba MS |
9100 | { |
9101 | tree decl; | |
f7da6097 MS |
9102 | RID_BIT_TYPE specbits; |
9103 | ||
9e259dd1 MM |
9104 | my_friendly_assert (!name || TREE_CODE (name) == IDENTIFIER_NODE, |
9105 | 20020808); | |
9106 | ||
f7da6097 | 9107 | specbits = *specbits_in; |
8d08fdba | 9108 | |
9e259dd1 MM |
9109 | /* Compute the scope in which to place the variable. */ |
9110 | if (!scope) | |
8d08fdba | 9111 | { |
9e259dd1 MM |
9112 | /* An explicit "extern" specifier indicates a namespace-scope |
9113 | variable. */ | |
9114 | if (RIDBIT_SETP (RID_EXTERN, specbits)) | |
9115 | scope = current_namespace; | |
9116 | else if (!at_function_scope_p ()) | |
9117 | { | |
9118 | scope = current_scope (); | |
9119 | if (!scope) | |
9120 | scope = current_namespace; | |
9121 | } | |
9122 | } | |
9123 | ||
9124 | if (scope | |
9125 | && (/* If the variable is a namespace-scope variable declared in a | |
9126 | template, we need DECL_LANG_SPECIFIC. */ | |
9127 | (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl) | |
9128 | /* Similarly for namespace-scope variables with language linkage | |
9129 | other than C++. */ | |
9130 | || (TREE_CODE (scope) == NAMESPACE_DECL | |
9131 | && current_lang_name != lang_name_cplusplus) | |
9132 | /* Similarly for static data members. */ | |
9133 | || TYPE_P (scope))) | |
9134 | decl = build_lang_decl (VAR_DECL, name, type); | |
8d08fdba | 9135 | else |
9e259dd1 | 9136 | decl = build_decl (VAR_DECL, name, type); |
79c4d4b7 | 9137 | |
9e259dd1 MM |
9138 | if (scope && TREE_CODE (scope) == NAMESPACE_DECL) |
9139 | set_decl_namespace (decl, scope, 0); | |
9140 | else | |
9141 | DECL_CONTEXT (decl) = scope; | |
6060a796 | 9142 | |
9e259dd1 MM |
9143 | if (name && scope && current_lang_name != lang_name_c) |
9144 | /* We can't mangle lazily here because we don't have any | |
9145 | way to recover whether or not a variable was `extern | |
9146 | "C"' later. */ | |
9147 | mangle_decl (decl); | |
2c73f9f5 | 9148 | |
8d08fdba MS |
9149 | if (RIDBIT_SETP (RID_EXTERN, specbits)) |
9150 | { | |
9151 | DECL_THIS_EXTERN (decl) = 1; | |
9152 | DECL_EXTERNAL (decl) = !initialized; | |
9153 | } | |
9154 | ||
9155 | /* In class context, static means one per class, | |
9156 | public access, and static storage. */ | |
2b9dc906 | 9157 | if (DECL_CLASS_SCOPE_P (decl)) |
8d08fdba MS |
9158 | { |
9159 | TREE_PUBLIC (decl) = 1; | |
9160 | TREE_STATIC (decl) = 1; | |
5b605f68 | 9161 | DECL_EXTERNAL (decl) = 0; |
8d08fdba MS |
9162 | } |
9163 | /* At top level, either `static' or no s.c. makes a definition | |
9164 | (perhaps tentative), and absence of `static' makes it public. */ | |
a9aedbc2 | 9165 | else if (toplevel_bindings_p ()) |
8d08fdba | 9166 | { |
a9aedbc2 | 9167 | TREE_PUBLIC (decl) = (RIDBIT_NOTSETP (RID_STATIC, specbits) |
9fffd093 | 9168 | && (DECL_THIS_EXTERN (decl) || ! constp)); |
8d08fdba MS |
9169 | TREE_STATIC (decl) = ! DECL_EXTERNAL (decl); |
9170 | } | |
9171 | /* Not at top level, only `static' makes a static definition. */ | |
9172 | else | |
9173 | { | |
9174 | TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits); | |
9175 | TREE_PUBLIC (decl) = DECL_EXTERNAL (decl); | |
9176 | } | |
50a6dbd7 | 9177 | |
7a1f3f5f RH |
9178 | if (RIDBIT_SETP (RID_THREAD, specbits)) |
9179 | { | |
9180 | if (targetm.have_tls) | |
9181 | DECL_THREAD_LOCAL (decl) = 1; | |
9182 | else | |
9183 | /* A mere warning is sure to result in improper semantics | |
9184 | at runtime. Don't bother to allow this to compile. */ | |
9185 | error ("thread-local storage not supported for this target"); | |
9186 | } | |
9187 | ||
50a6dbd7 JM |
9188 | if (TREE_PUBLIC (decl)) |
9189 | { | |
9190 | /* [basic.link]: A name with no linkage (notably, the name of a class | |
9191 | or enumeration declared in a local scope) shall not be used to | |
9192 | declare an entity with linkage. | |
9193 | ||
9194 | Only check this for public decls for now. */ | |
9195 | tree t = no_linkage_check (TREE_TYPE (decl)); | |
9196 | if (t) | |
9197 | { | |
1951a1b6 | 9198 | if (TYPE_ANONYMOUS_P (t)) |
50a6dbd7 JM |
9199 | /* Ignore for now; `enum { foo } e' is pretty common. */; |
9200 | else | |
33bd39a2 | 9201 | pedwarn ("non-local variable `%#D' uses local type `%T'", |
50a6dbd7 JM |
9202 | decl, t); |
9203 | } | |
9204 | } | |
9205 | ||
8d08fdba MS |
9206 | return decl; |
9207 | } | |
9208 | ||
d8f8dca1 MM |
9209 | /* Create and return a canonical pointer to member function type, for |
9210 | TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */ | |
8d08fdba MS |
9211 | |
9212 | tree | |
11f6b451 | 9213 | build_ptrmemfunc_type (tree type) |
8d08fdba | 9214 | { |
4977bab6 | 9215 | tree field, fields; |
8d08fdba | 9216 | tree t; |
46cbda4a | 9217 | tree unqualified_variant = NULL_TREE; |
8d08fdba | 9218 | |
d48ebde1 NS |
9219 | if (type == error_mark_node) |
9220 | return type; | |
d363e7bf | 9221 | |
8d08fdba MS |
9222 | /* If a canonical type already exists for this type, use it. We use |
9223 | this method instead of type_hash_canon, because it only does a | |
9224 | simple equality check on the list of field members. */ | |
9225 | ||
9226 | if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type))) | |
9227 | return t; | |
9228 | ||
46cbda4a MM |
9229 | /* Make sure that we always have the unqualified pointer-to-member |
9230 | type first. */ | |
89d684bb | 9231 | if (cp_type_quals (type) != TYPE_UNQUALIFIED) |
68642fb6 | 9232 | unqualified_variant |
46cbda4a MM |
9233 | = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type)); |
9234 | ||
33848bb0 | 9235 | t = make_aggr_type (RECORD_TYPE); |
2c73f9f5 | 9236 | /* Let the front-end know this is a pointer to member function... */ |
db5ae43f | 9237 | TYPE_PTRMEMFUNC_FLAG (t) = 1; |
2c73f9f5 | 9238 | /* ... and not really an aggregate. */ |
7ddedda4 | 9239 | SET_IS_AGGR_TYPE (t, 0); |
8d08fdba | 9240 | |
4977bab6 ZW |
9241 | field = build_decl (FIELD_DECL, pfn_identifier, type); |
9242 | fields = field; | |
9243 | ||
9244 | field = build_decl (FIELD_DECL, delta_identifier, delta_type_node); | |
9245 | TREE_CHAIN (field) = fields; | |
9246 | fields = field; | |
9247 | ||
9248 | finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node); | |
8d08fdba | 9249 | |
8d08fdba MS |
9250 | /* Zap out the name so that the back-end will give us the debugging |
9251 | information for this anonymous RECORD_TYPE. */ | |
9252 | TYPE_NAME (t) = NULL_TREE; | |
9253 | ||
46cbda4a MM |
9254 | /* If this is not the unqualified form of this pointer-to-member |
9255 | type, set the TYPE_MAIN_VARIANT for this type to be the | |
9256 | unqualified type. Since they are actually RECORD_TYPEs that are | |
9257 | not variants of each other, we must do this manually. */ | |
89d684bb | 9258 | if (cp_type_quals (type) != TYPE_UNQUALIFIED) |
46cbda4a | 9259 | { |
89d684bb | 9260 | t = build_qualified_type (t, cp_type_quals (type)); |
46cbda4a MM |
9261 | TYPE_MAIN_VARIANT (t) = unqualified_variant; |
9262 | TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant); | |
9263 | TYPE_NEXT_VARIANT (unqualified_variant) = t; | |
9264 | } | |
9265 | ||
9266 | /* Cache this pointer-to-member type so that we can find it again | |
9267 | later. */ | |
8d08fdba MS |
9268 | TYPE_SET_PTRMEMFUNC_TYPE (type, t); |
9269 | ||
e92cc029 | 9270 | /* Seems to be wanted. */ |
8d08fdba | 9271 | CLASSTYPE_GOT_SEMICOLON (t) = 1; |
46cbda4a | 9272 | |
8d08fdba MS |
9273 | return t; |
9274 | } | |
9275 | ||
9e259dd1 MM |
9276 | /* Create and return a pointer to data member type. */ |
9277 | ||
9278 | tree | |
9279 | build_ptrmem_type (tree class_type, tree member_type) | |
9280 | { | |
9281 | return build_pointer_type (build_offset_type (class_type, member_type)); | |
9282 | } | |
9283 | ||
b17e2870 JM |
9284 | /* DECL is a VAR_DECL defined in-class, whose TYPE is also given. |
9285 | Check to see that the definition is valid. Issue appropriate error | |
9286 | messages. Return 1 if the definition is particularly bad, or 0 | |
9287 | otherwise. */ | |
9288 | ||
9289 | int | |
11f6b451 | 9290 | check_static_variable_definition (tree decl, tree type) |
b17e2870 JM |
9291 | { |
9292 | /* Motion 10 at San Diego: If a static const integral data member is | |
9293 | initialized with an integral constant expression, the initializer | |
9294 | may appear either in the declaration (within the class), or in | |
9295 | the definition, but not both. If it appears in the class, the | |
9296 | member is a member constant. The file-scope definition is always | |
9297 | required. */ | |
dcba9b0f | 9298 | if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE) |
b17e2870 | 9299 | { |
33bd39a2 | 9300 | error ("invalid in-class initialization of static data member of non-integral type `%T'", |
dcba9b0f | 9301 | type); |
b17e2870 JM |
9302 | /* If we just return the declaration, crashes will sometimes |
9303 | occur. We therefore return void_type_node, as if this was a | |
9304 | friend declaration, to cause callers to completely ignore | |
9305 | this declaration. */ | |
9306 | return 1; | |
9307 | } | |
9308 | else if (!CP_TYPE_CONST_P (type)) | |
33bd39a2 | 9309 | error ("ISO C++ forbids in-class initialization of non-const static member `%D'", |
b17e2870 JM |
9310 | decl); |
9311 | else if (pedantic && !INTEGRAL_TYPE_P (type)) | |
33bd39a2 | 9312 | pedwarn ("ISO C++ forbids initialization of member constant `%D' of non-integral type `%T'", decl, type); |
b17e2870 JM |
9313 | |
9314 | return 0; | |
9315 | } | |
9316 | ||
2848ad0f MM |
9317 | /* Given the SIZE (i.e., number of elements) in an array, compute an |
9318 | appropriate index type for the array. If non-NULL, NAME is the | |
9319 | name of the thing being declared. */ | |
9320 | ||
c95cd22e | 9321 | tree |
11f6b451 | 9322 | compute_array_index_type (tree name, tree size) |
2848ad0f MM |
9323 | { |
9324 | tree itype; | |
9325 | ||
2848ad0f MM |
9326 | /* If this involves a template parameter, it will be a constant at |
9327 | instantiation time, but we don't know what the value is yet. | |
9328 | Even if no template parameters are involved, we may an expression | |
9329 | that is not a constant; we don't even simplify `1 + 2' when | |
9330 | processing a template. */ | |
9331 | if (processing_template_decl) | |
9332 | { | |
9333 | /* Resolve a qualified reference to an enumerator or static | |
9334 | const data member of ours. */ | |
9335 | if (TREE_CODE (size) == SCOPE_REF | |
9336 | && TREE_OPERAND (size, 0) == current_class_type) | |
9337 | { | |
9338 | tree t = lookup_field (current_class_type, | |
86ac0575 | 9339 | TREE_OPERAND (size, 1), 0, false); |
2848ad0f MM |
9340 | if (t) |
9341 | size = t; | |
9342 | } | |
9343 | ||
9344 | return build_index_type (build_min (MINUS_EXPR, sizetype, | |
9345 | size, integer_one_node)); | |
9346 | } | |
9347 | ||
a1c65f9f | 9348 | /* The size might be the result of a cast. */ |
80f5bb34 MM |
9349 | STRIP_TYPE_NOPS (size); |
9350 | ||
9351 | /* It might be a const variable or enumeration constant. */ | |
9352 | size = decl_constant_value (size); | |
9353 | ||
2848ad0f MM |
9354 | /* The array bound must be an integer type. */ |
9355 | if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE | |
9356 | && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE | |
9357 | && TREE_CODE (TREE_TYPE (size)) != BOOLEAN_TYPE) | |
9358 | { | |
d67cdbc3 | 9359 | if (name) |
33bd39a2 | 9360 | error ("size of array `%D' has non-integer type", name); |
d67cdbc3 | 9361 | else |
33bd39a2 | 9362 | error ("size of array has non-integer type"); |
2848ad0f MM |
9363 | size = integer_one_node; |
9364 | } | |
9365 | ||
9366 | /* Normally, the array-bound will be a constant. */ | |
2bb5d995 | 9367 | if (TREE_CODE (size) == INTEGER_CST) |
2848ad0f MM |
9368 | { |
9369 | /* Check to see if the array bound overflowed. Make that an | |
9370 | error, no matter how generous we're being. */ | |
9371 | int old_flag_pedantic_errors = flag_pedantic_errors; | |
9372 | int old_pedantic = pedantic; | |
9373 | pedantic = flag_pedantic_errors = 1; | |
9374 | constant_expression_warning (size); | |
9375 | pedantic = old_pedantic; | |
9376 | flag_pedantic_errors = old_flag_pedantic_errors; | |
9377 | ||
9378 | /* An array must have a positive number of elements. */ | |
9379 | if (INT_CST_LT (size, integer_zero_node)) | |
9380 | { | |
d67cdbc3 | 9381 | if (name) |
33bd39a2 | 9382 | error ("size of array `%D' is negative", name); |
d67cdbc3 | 9383 | else |
33bd39a2 | 9384 | error ("size of array is negative"); |
2848ad0f MM |
9385 | size = integer_one_node; |
9386 | } | |
9387 | /* Except that an extension we allow zero-sized arrays. We | |
68642fb6 | 9388 | always allow them in system headers because glibc uses |
2848ad0f MM |
9389 | them. */ |
9390 | else if (integer_zerop (size) && pedantic && !in_system_header) | |
d67cdbc3 JM |
9391 | { |
9392 | if (name) | |
33bd39a2 | 9393 | pedwarn ("ISO C++ forbids zero-size array `%D'", name); |
d67cdbc3 | 9394 | else |
33bd39a2 | 9395 | pedwarn ("ISO C++ forbids zero-size array"); |
d67cdbc3 | 9396 | } |
2848ad0f | 9397 | } |
2bb5d995 JM |
9398 | else if (TREE_CONSTANT (size)) |
9399 | { | |
9400 | /* `(int) &fn' is not a valid array bound. */ | |
9401 | if (name) | |
33bd39a2 | 9402 | error ("size of array `%D' is not an integral constant-expression", |
2bb5d995 JM |
9403 | name); |
9404 | else | |
33bd39a2 | 9405 | error ("size of array is not an integral constant-expression"); |
2bb5d995 | 9406 | } |
2848ad0f MM |
9407 | |
9408 | /* Compute the index of the largest element in the array. It is | |
9409 | one less than the number of elements in the array. */ | |
9410 | itype | |
ab76ca54 MM |
9411 | = fold (cp_build_binary_op (MINUS_EXPR, |
9412 | cp_convert (ssizetype, size), | |
9413 | cp_convert (ssizetype, | |
9414 | integer_one_node))); | |
68642fb6 | 9415 | |
2848ad0f MM |
9416 | /* Check for variable-sized arrays. We allow such things as an |
9417 | extension, even though they are not allowed in ANSI/ISO C++. */ | |
9418 | if (!TREE_CONSTANT (itype)) | |
9419 | { | |
9420 | if (pedantic) | |
9421 | { | |
9422 | if (name) | |
33bd39a2 | 9423 | pedwarn ("ISO C++ forbids variable-size array `%D'", |
2848ad0f MM |
9424 | name); |
9425 | else | |
33bd39a2 | 9426 | pedwarn ("ISO C++ forbids variable-size array"); |
2848ad0f MM |
9427 | } |
9428 | ||
9429 | /* Create a variable-sized array index type. */ | |
9430 | itype = variable_size (itype); | |
9431 | } | |
9432 | /* Make sure that there was no overflow when creating to a signed | |
9433 | index type. (For example, on a 32-bit machine, an array with | |
9434 | size 2^32 - 1 is too big.) */ | |
9435 | else if (TREE_OVERFLOW (itype)) | |
9436 | { | |
9437 | error ("overflow in array dimension"); | |
9438 | TREE_OVERFLOW (itype) = 0; | |
9439 | } | |
68642fb6 | 9440 | |
2848ad0f MM |
9441 | /* Create and return the appropriate index type. */ |
9442 | return build_index_type (itype); | |
9443 | } | |
9444 | ||
a723baf1 MM |
9445 | /* Returns the scope (if any) in which the entity declared by |
9446 | DECLARATOR will be located. If the entity was declared with an | |
9447 | unqualified name, NULL_TREE is returned. */ | |
9448 | ||
9449 | tree | |
11f6b451 | 9450 | get_scope_of_declarator (tree declarator) |
a723baf1 MM |
9451 | { |
9452 | if (!declarator) | |
9453 | return NULL_TREE; | |
9454 | ||
9455 | switch (TREE_CODE (declarator)) | |
9456 | { | |
9457 | case CALL_EXPR: | |
9458 | case ARRAY_REF: | |
9459 | case INDIRECT_REF: | |
9460 | case ADDR_EXPR: | |
9461 | /* For any of these, the main declarator is the first operand. */ | |
9462 | return get_scope_of_declarator (TREE_OPERAND | |
9463 | (declarator, 0)); | |
9464 | ||
9465 | case SCOPE_REF: | |
9466 | /* For a pointer-to-member, continue descending. */ | |
9467 | if (TREE_CODE (TREE_OPERAND (declarator, 1)) | |
9468 | == INDIRECT_REF) | |
9469 | return get_scope_of_declarator (TREE_OPERAND | |
9470 | (declarator, 1)); | |
9471 | /* Otherwise, if the declarator-id is a SCOPE_REF, the scope in | |
9472 | which the declaration occurs is the first operand. */ | |
9473 | return TREE_OPERAND (declarator, 0); | |
9474 | ||
9475 | case TREE_LIST: | |
9476 | /* Attributes to be applied. The declarator is TREE_VALUE. */ | |
9477 | return get_scope_of_declarator (TREE_VALUE (declarator)); | |
9478 | ||
9479 | default: | |
9480 | /* Otherwise, we have a declarator-id which is not a qualified | |
9481 | name; the entity will be declared in the current scope. */ | |
9482 | return NULL_TREE; | |
9483 | } | |
9484 | } | |
9485 | ||
2848ad0f MM |
9486 | /* Returns an ARRAY_TYPE for an array with SIZE elements of the |
9487 | indicated TYPE. If non-NULL, NAME is the NAME of the declaration | |
9488 | with this type. */ | |
9489 | ||
9490 | static tree | |
11f6b451 | 9491 | create_array_type_for_decl (tree name, tree type, tree size) |
2848ad0f MM |
9492 | { |
9493 | tree itype = NULL_TREE; | |
9494 | const char* error_msg; | |
9495 | ||
9496 | /* If things have already gone awry, bail now. */ | |
9497 | if (type == error_mark_node || size == error_mark_node) | |
9498 | return error_mark_node; | |
9499 | ||
9500 | /* Assume that everything will go OK. */ | |
9501 | error_msg = NULL; | |
9502 | ||
9503 | /* There are some types which cannot be array elements. */ | |
9504 | switch (TREE_CODE (type)) | |
9505 | { | |
9506 | case VOID_TYPE: | |
9507 | error_msg = "array of void"; | |
9508 | break; | |
9509 | ||
9510 | case FUNCTION_TYPE: | |
9511 | error_msg = "array of functions"; | |
9512 | break; | |
9513 | ||
9514 | case REFERENCE_TYPE: | |
9515 | error_msg = "array of references"; | |
9516 | break; | |
9517 | ||
9518 | case OFFSET_TYPE: | |
9519 | error_msg = "array of data members"; | |
9520 | break; | |
9521 | ||
9522 | case METHOD_TYPE: | |
9523 | error_msg = "array of function members"; | |
9524 | break; | |
9525 | ||
9526 | default: | |
9527 | break; | |
9528 | } | |
9529 | ||
9530 | /* If something went wrong, issue an error-message and return. */ | |
9531 | if (error_msg) | |
9532 | { | |
9533 | if (name) | |
33bd39a2 | 9534 | error ("declaration of `%D' as %s", name, error_msg); |
2848ad0f | 9535 | else |
33bd39a2 | 9536 | error ("creating %s", error_msg); |
2848ad0f MM |
9537 | |
9538 | return error_mark_node; | |
9539 | } | |
9540 | ||
9541 | /* [dcl.array] | |
68642fb6 | 9542 | |
2848ad0f MM |
9543 | The constant expressions that specify the bounds of the arrays |
9544 | can be omitted only for the first member of the sequence. */ | |
9545 | if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)) | |
9546 | { | |
b3faacfd | 9547 | if (name) |
33bd39a2 | 9548 | error ("declaration of `%D' as multidimensional array must have bounds for all dimensions except the first", |
b3faacfd KL |
9549 | name); |
9550 | else | |
33bd39a2 | 9551 | error ("multidimensional array must have bounds for all dimensions except the first"); |
2848ad0f MM |
9552 | |
9553 | return error_mark_node; | |
9554 | } | |
9555 | ||
9556 | /* Figure out the index type for the array. */ | |
9557 | if (size) | |
9558 | itype = compute_array_index_type (name, size); | |
9559 | ||
9560 | return build_cplus_array_type (type, itype); | |
9561 | } | |
9562 | ||
3dbc07b6 MM |
9563 | /* Check that it's OK to declare a function with the indicated TYPE. |
9564 | SFK indicates the kind of special function (if any) that this | |
1f84ec23 | 9565 | function is. OPTYPE is the type given in a conversion operator |
3dbc07b6 MM |
9566 | declaration. Returns the actual return type of the function; that |
9567 | may be different than TYPE if an error occurs, or for certain | |
9568 | special functions. */ | |
9569 | ||
9570 | static tree | |
11f6b451 NN |
9571 | check_special_function_return_type (special_function_kind sfk, |
9572 | tree type, | |
9573 | tree optype) | |
3dbc07b6 MM |
9574 | { |
9575 | switch (sfk) | |
9576 | { | |
9577 | case sfk_constructor: | |
9578 | if (type) | |
33bd39a2 | 9579 | error ("return type specification for constructor invalid"); |
5362b086 | 9580 | |
1f84ec23 | 9581 | type = void_type_node; |
3dbc07b6 MM |
9582 | break; |
9583 | ||
9584 | case sfk_destructor: | |
9585 | if (type) | |
33bd39a2 | 9586 | error ("return type specification for destructor invalid"); |
3dbc07b6 MM |
9587 | type = void_type_node; |
9588 | break; | |
9589 | ||
9590 | case sfk_conversion: | |
9591 | if (type && !same_type_p (type, optype)) | |
33bd39a2 | 9592 | error ("operator `%T' declared to return `%T'", optype, type); |
3dbc07b6 | 9593 | else if (type) |
33bd39a2 | 9594 | pedwarn ("return type specified for `operator %T'", optype); |
3dbc07b6 MM |
9595 | type = optype; |
9596 | break; | |
9597 | ||
9598 | default: | |
a98facb0 | 9599 | abort (); |
3dbc07b6 MM |
9600 | break; |
9601 | } | |
9602 | ||
9603 | return type; | |
9604 | } | |
9605 | ||
a723baf1 MM |
9606 | /* Given declspecs and a declarator (abstract or otherwise), determine |
9607 | the name and type of the object declared and construct a DECL node | |
9608 | for it. | |
8d08fdba MS |
9609 | |
9610 | DECLSPECS is a chain of tree_list nodes whose value fields | |
9611 | are the storage classes and type specifiers. | |
9612 | ||
9613 | DECL_CONTEXT says which syntactic context this declaration is in: | |
9614 | NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL. | |
9615 | FUNCDEF for a function definition. Like NORMAL but a few different | |
9616 | error messages in each case. Return value may be zero meaning | |
9617 | this definition is too screwy to try to parse. | |
9618 | MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to | |
9619 | handle member functions (which have FIELD context). | |
9620 | Return value may be zero meaning this definition is too screwy to | |
9621 | try to parse. | |
9622 | PARM for a parameter declaration (either within a function prototype | |
9623 | or before a function body). Make a PARM_DECL, or return void_type_node. | |
db5ae43f | 9624 | CATCHPARM for a parameter declaration before a catch clause. |
8d08fdba MS |
9625 | TYPENAME if for a typename (in a cast or sizeof). |
9626 | Don't make a DECL node; just return the ..._TYPE node. | |
9627 | FIELD for a struct or union field; make a FIELD_DECL. | |
9628 | BITFIELD for a field with specified width. | |
9629 | INITIALIZED is 1 if the decl has an initializer. | |
9630 | ||
91d231cb JM |
9631 | ATTRLIST is a pointer to the list of attributes, which may be NULL |
9632 | if there are none; *ATTRLIST may be modified if attributes from inside | |
9633 | the declarator should be applied to the declaration. | |
b17e2870 | 9634 | |
a723baf1 MM |
9635 | When this function is called, scoping variables (such as |
9636 | CURRENT_CLASS_TYPE) should reflect the scope in which the | |
9637 | declaration occurs, not the scope in which the new declaration will | |
9638 | be placed. For example, on: | |
8d08fdba | 9639 | |
a723baf1 | 9640 | void S::f() { ... } |
8d08fdba | 9641 | |
a723baf1 MM |
9642 | when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE |
9643 | should not be `S'. */ | |
8d08fdba | 9644 | |
8d08fdba | 9645 | tree |
11f6b451 NN |
9646 | grokdeclarator (tree declarator, |
9647 | tree declspecs, | |
9648 | enum decl_context decl_context, | |
9649 | int initialized, | |
9650 | tree* attrlist) | |
8d08fdba MS |
9651 | { |
9652 | RID_BIT_TYPE specbits; | |
9653 | int nclasses = 0; | |
9654 | tree spec; | |
9655 | tree type = NULL_TREE; | |
9656 | int longlong = 0; | |
91063b51 | 9657 | int type_quals; |
db5ae43f | 9658 | int virtualp, explicitp, friendp, inlinep, staticp; |
8d08fdba MS |
9659 | int explicit_int = 0; |
9660 | int explicit_char = 0; | |
37c46b43 | 9661 | int defaulted_int = 0; |
270d8c65 | 9662 | int extern_langp = 0; |
d5614afb | 9663 | tree dependant_name = NULL_TREE; |
270d8c65 | 9664 | |
8d08fdba | 9665 | tree typedef_decl = NULL_TREE; |
9c0758dd | 9666 | const char *name; |
8d08fdba MS |
9667 | tree typedef_type = NULL_TREE; |
9668 | int funcdef_flag = 0; | |
9669 | enum tree_code innermost_code = ERROR_MARK; | |
9670 | int bitfield = 0; | |
6125f3be DE |
9671 | #if 0 |
9672 | /* See the code below that used this. */ | |
91d231cb | 9673 | tree decl_attr = NULL_TREE; |
6125f3be | 9674 | #endif |
8d08fdba MS |
9675 | |
9676 | /* Keep track of what sort of function is being processed | |
9677 | so that we can warn about default return values, or explicit | |
9678 | return values which do not match prescribed defaults. */ | |
3dbc07b6 | 9679 | special_function_kind sfk = sfk_none; |
8d08fdba MS |
9680 | |
9681 | tree dname = NULL_TREE; | |
9682 | tree ctype = current_class_type; | |
9683 | tree ctor_return_type = NULL_TREE; | |
9684 | enum overload_flags flags = NO_SPECIAL; | |
8d08fdba | 9685 | tree quals = NULL_TREE; |
c11b6f21 | 9686 | tree raises = NULL_TREE; |
386b8a85 | 9687 | int template_count = 0; |
2c73f9f5 | 9688 | tree in_namespace = NULL_TREE; |
91d231cb | 9689 | tree returned_attrs = NULL_TREE; |
a723baf1 | 9690 | tree scope = NULL_TREE; |
8d08fdba MS |
9691 | |
9692 | RIDBIT_RESET_ALL (specbits); | |
9693 | if (decl_context == FUNCDEF) | |
9694 | funcdef_flag = 1, decl_context = NORMAL; | |
9695 | else if (decl_context == MEMFUNCDEF) | |
9696 | funcdef_flag = -1, decl_context = FIELD; | |
9697 | else if (decl_context == BITFIELD) | |
9698 | bitfield = 1, decl_context = FIELD; | |
9699 | ||
8d08fdba MS |
9700 | /* Look inside a declarator for the name being declared |
9701 | and get it as a string, for an error message. */ | |
9702 | { | |
be99da77 MS |
9703 | tree *next = &declarator; |
9704 | register tree decl; | |
8d08fdba MS |
9705 | name = NULL; |
9706 | ||
be99da77 MS |
9707 | while (next && *next) |
9708 | { | |
9709 | decl = *next; | |
9710 | switch (TREE_CODE (decl)) | |
8d08fdba | 9711 | { |
52fbc847 JM |
9712 | case TREE_LIST: |
9713 | /* For attributes. */ | |
9714 | next = &TREE_VALUE (decl); | |
9715 | break; | |
9716 | ||
be99da77 MS |
9717 | case COND_EXPR: |
9718 | ctype = NULL_TREE; | |
9719 | next = &TREE_OPERAND (decl, 0); | |
9720 | break; | |
8d08fdba | 9721 | |
2c73f9f5 | 9722 | case BIT_NOT_EXPR: /* For C++ destructors! */ |
8d08fdba | 9723 | { |
be99da77 MS |
9724 | tree name = TREE_OPERAND (decl, 0); |
9725 | tree rename = NULL_TREE; | |
9726 | ||
9727 | my_friendly_assert (flags == NO_SPECIAL, 152); | |
9728 | flags = DTOR_FLAG; | |
3dbc07b6 | 9729 | sfk = sfk_destructor; |
a723baf1 | 9730 | if (TYPE_P (name)) |
5566b478 | 9731 | TREE_OPERAND (decl, 0) = name = constructor_name (name); |
be99da77 MS |
9732 | my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153); |
9733 | if (ctype == NULL_TREE) | |
9734 | { | |
9735 | if (current_class_type == NULL_TREE) | |
9736 | { | |
8251199e | 9737 | error ("destructors must be member functions"); |
be99da77 MS |
9738 | flags = NO_SPECIAL; |
9739 | } | |
9740 | else | |
9741 | { | |
a723baf1 | 9742 | tree t = constructor_name (current_class_type); |
be99da77 MS |
9743 | if (t != name) |
9744 | rename = t; | |
9745 | } | |
9746 | } | |
8d08fdba | 9747 | else |
be99da77 MS |
9748 | { |
9749 | tree t = constructor_name (ctype); | |
9750 | if (t != name) | |
9751 | rename = t; | |
9752 | } | |
51c184be | 9753 | |
be99da77 | 9754 | if (rename) |
39211cd5 | 9755 | { |
33bd39a2 | 9756 | error ("destructor `%T' must match class name `%T'", |
5566b478 | 9757 | name, rename); |
be99da77 | 9758 | TREE_OPERAND (decl, 0) = rename; |
39211cd5 | 9759 | } |
be99da77 | 9760 | next = &name; |
51c184be | 9761 | } |
be99da77 | 9762 | break; |
8d08fdba | 9763 | |
be99da77 | 9764 | case ADDR_EXPR: /* C++ reference declaration */ |
a1c65f9f | 9765 | /* Fall through. */ |
be99da77 MS |
9766 | case ARRAY_REF: |
9767 | case INDIRECT_REF: | |
9768 | ctype = NULL_TREE; | |
9769 | innermost_code = TREE_CODE (decl); | |
9770 | next = &TREE_OPERAND (decl, 0); | |
9771 | break; | |
8d08fdba | 9772 | |
be99da77 | 9773 | case CALL_EXPR: |
be99da77 MS |
9774 | innermost_code = TREE_CODE (decl); |
9775 | if (decl_context == FIELD && ctype == NULL_TREE) | |
9776 | ctype = current_class_type; | |
45537677 | 9777 | if (ctype |
c11b6f21 | 9778 | && TREE_OPERAND (decl, 0) |
45537677 | 9779 | && (TREE_CODE (TREE_OPERAND (decl, 0)) == TYPE_DECL |
8ba658ee MM |
9780 | && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 0)), |
9781 | ctype))) | |
be99da77 MS |
9782 | TREE_OPERAND (decl, 0) = constructor_name (ctype); |
9783 | next = &TREE_OPERAND (decl, 0); | |
9784 | decl = *next; | |
9785 | if (ctype != NULL_TREE | |
9786 | && decl != NULL_TREE && flags != DTOR_FLAG | |
a723baf1 | 9787 | && constructor_name_p (decl, ctype)) |
8d08fdba | 9788 | { |
3dbc07b6 | 9789 | sfk = sfk_constructor; |
be99da77 | 9790 | ctor_return_type = ctype; |
8d08fdba | 9791 | } |
be99da77 MS |
9792 | ctype = NULL_TREE; |
9793 | break; | |
68642fb6 | 9794 | |
386b8a85 JM |
9795 | case TEMPLATE_ID_EXPR: |
9796 | { | |
9797 | tree fns = TREE_OPERAND (decl, 0); | |
9798 | ||
9799 | if (TREE_CODE (fns) == LOOKUP_EXPR) | |
9800 | fns = TREE_OPERAND (fns, 0); | |
9801 | ||
8f032717 MM |
9802 | dname = fns; |
9803 | if (TREE_CODE (dname) == COMPONENT_REF) | |
9804 | dname = TREE_OPERAND (dname, 1); | |
9805 | if (TREE_CODE (dname) != IDENTIFIER_NODE) | |
9806 | { | |
9807 | my_friendly_assert (is_overloaded_fn (dname), | |
9808 | 19990331); | |
9809 | dname = DECL_NAME (get_first_fn (dname)); | |
9810 | } | |
386b8a85 | 9811 | } |
a1c65f9f | 9812 | /* Fall through. */ |
be99da77 MS |
9813 | |
9814 | case IDENTIFIER_NODE: | |
386b8a85 JM |
9815 | if (TREE_CODE (decl) == IDENTIFIER_NODE) |
9816 | dname = decl; | |
9817 | ||
be99da77 MS |
9818 | next = 0; |
9819 | ||
0e5921e8 | 9820 | if (C_IS_RESERVED_WORD (dname)) |
8d08fdba | 9821 | { |
33bd39a2 | 9822 | error ("declarator-id missing; using reserved word `%D'", |
be99da77 MS |
9823 | dname); |
9824 | name = IDENTIFIER_POINTER (dname); | |
8d08fdba | 9825 | } |
596ea4e5 | 9826 | else if (!IDENTIFIER_TYPENAME_P (dname)) |
be99da77 | 9827 | name = IDENTIFIER_POINTER (dname); |
8d08fdba MS |
9828 | else |
9829 | { | |
596ea4e5 AS |
9830 | my_friendly_assert (flags == NO_SPECIAL, 154); |
9831 | flags = TYPENAME_FLAG; | |
9832 | ctor_return_type = TREE_TYPE (dname); | |
9833 | sfk = sfk_conversion; | |
9834 | if (IDENTIFIER_GLOBAL_VALUE (dname) | |
5362b086 | 9835 | && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (dname)) |
596ea4e5 AS |
9836 | == TYPE_DECL)) |
9837 | name = IDENTIFIER_POINTER (dname); | |
9838 | else | |
9839 | name = "<invalid operator>"; | |
8d08fdba | 9840 | } |
be99da77 | 9841 | break; |
8d08fdba | 9842 | |
be99da77 MS |
9843 | /* C++ extension */ |
9844 | case SCOPE_REF: | |
9845 | { | |
9846 | /* Perform error checking, and decide on a ctype. */ | |
9847 | tree cname = TREE_OPERAND (decl, 0); | |
9848 | if (cname == NULL_TREE) | |
9849 | ctype = NULL_TREE; | |
2c73f9f5 ML |
9850 | else if (TREE_CODE (cname) == NAMESPACE_DECL) |
9851 | { | |
9852 | ctype = NULL_TREE; | |
9853 | in_namespace = TREE_OPERAND (decl, 0); | |
2c73f9f5 | 9854 | } |
be99da77 | 9855 | else if (! is_aggr_type (cname, 1)) |
a723baf1 | 9856 | ctype = NULL_TREE; |
be99da77 MS |
9857 | /* Must test TREE_OPERAND (decl, 1), in case user gives |
9858 | us `typedef (class::memfunc)(int); memfunc *memfuncptr;' */ | |
9859 | else if (TREE_OPERAND (decl, 1) | |
9860 | && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF) | |
9861 | ctype = cname; | |
73b0fce8 | 9862 | else if (TREE_CODE (cname) == TEMPLATE_TYPE_PARM |
a1281f45 | 9863 | || TREE_CODE (cname) == BOUND_TEMPLATE_TEMPLATE_PARM) |
5566b478 | 9864 | { |
d5614afb NS |
9865 | /* This might be declaring a member of a template |
9866 | parm to be a friend. */ | |
9867 | ctype = cname; | |
9868 | dependant_name = TREE_OPERAND (decl, 1); | |
5566b478 | 9869 | } |
be99da77 MS |
9870 | else if (ctype == NULL_TREE) |
9871 | ctype = cname; | |
9872 | else if (TREE_COMPLEXITY (decl) == current_class_depth) | |
a723baf1 | 9873 | ; |
be99da77 MS |
9874 | else |
9875 | { | |
9876 | if (! UNIQUELY_DERIVED_FROM_P (cname, ctype)) | |
9877 | { | |
33bd39a2 | 9878 | error ("type `%T' is not derived from type `%T'", |
be99da77 | 9879 | cname, ctype); |
a723baf1 | 9880 | ctype = NULL_TREE; |
be99da77 MS |
9881 | } |
9882 | else | |
9883 | ctype = cname; | |
9884 | } | |
9885 | ||
a723baf1 MM |
9886 | /* It is valid to write: |
9887 | ||
9888 | class C { void f(); }; | |
9889 | typedef C D; | |
9890 | void D::f(); | |
9891 | ||
9892 | The standard is not clear about whether `typedef const C D' is | |
9893 | legal; as of 2002-09-15 the committee is considering | |
9894 | that question. EDG 3.0 allows that syntax. | |
9895 | Therefore, we do as well. */ | |
9896 | if (ctype) | |
9897 | ctype = TYPE_MAIN_VARIANT (ctype); | |
9898 | /* Update the declarator so that when we process it | |
9899 | again the correct type is present. */ | |
9900 | TREE_OPERAND (decl, 0) = ctype; | |
9901 | ||
c91a56d2 | 9902 | if (ctype && TREE_CODE (TREE_OPERAND (decl, 1)) == TYPE_DECL |
8ba658ee MM |
9903 | && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 1)), |
9904 | ctype)) | |
be99da77 MS |
9905 | TREE_OPERAND (decl, 1) = constructor_name (ctype); |
9906 | next = &TREE_OPERAND (decl, 1); | |
9907 | decl = *next; | |
9908 | if (ctype) | |
9909 | { | |
9910 | if (TREE_CODE (decl) == IDENTIFIER_NODE | |
a723baf1 | 9911 | && constructor_name_p (decl, ctype)) |
be99da77 | 9912 | { |
3dbc07b6 | 9913 | sfk = sfk_constructor; |
be99da77 MS |
9914 | ctor_return_type = ctype; |
9915 | } | |
9916 | else if (TREE_CODE (decl) == BIT_NOT_EXPR | |
9917 | && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE | |
8ba658ee MM |
9918 | && constructor_name_p (TREE_OPERAND (decl, 0), |
9919 | ctype)) | |
be99da77 | 9920 | { |
3dbc07b6 | 9921 | sfk = sfk_destructor; |
be99da77 MS |
9922 | ctor_return_type = ctype; |
9923 | flags = DTOR_FLAG; | |
9924 | TREE_OPERAND (decl, 0) = constructor_name (ctype); | |
9925 | next = &TREE_OPERAND (decl, 0); | |
9926 | } | |
9927 | } | |
9928 | } | |
9929 | break; | |
9930 | ||
9931 | case ERROR_MARK: | |
9932 | next = 0; | |
9933 | break; | |
9934 | ||
45537677 MS |
9935 | case TYPE_DECL: |
9936 | /* Parse error puts this typespec where | |
9937 | a declarator should go. */ | |
33bd39a2 | 9938 | error ("`%T' specified as declarator-id", DECL_NAME (decl)); |
45537677 | 9939 | if (TREE_TYPE (decl) == current_class_type) |
33bd39a2 | 9940 | error (" perhaps you want `%T' for a constructor", |
45537677 MS |
9941 | current_class_name); |
9942 | dname = DECL_NAME (decl); | |
9943 | name = IDENTIFIER_POINTER (dname); | |
9944 | ||
e92cc029 | 9945 | /* Avoid giving two errors for this. */ |
45537677 MS |
9946 | IDENTIFIER_CLASS_VALUE (dname) = NULL_TREE; |
9947 | ||
58010b57 | 9948 | declspecs = tree_cons (NULL_TREE, integer_type_node, declspecs); |
45537677 MS |
9949 | *next = dname; |
9950 | next = 0; | |
9951 | break; | |
9952 | ||
50ad9642 MM |
9953 | case BASELINK: |
9954 | next = &BASELINK_FUNCTIONS (decl); | |
9955 | break; | |
2ee366b5 GDR |
9956 | |
9957 | case TEMPLATE_DECL: | |
9958 | /* Sometimes, we see a template-name used as part of a | |
9959 | decl-specifier like in | |
9960 | std::allocator alloc; | |
9961 | Handle that gracefully. */ | |
9962 | error ("invalid use of template-name '%E' in a declarator", decl); | |
9963 | return error_mark_node; | |
9964 | break; | |
50ad9642 | 9965 | |
be99da77 | 9966 | default: |
dd29188b | 9967 | my_friendly_assert (0, 20020917); |
8d08fdba | 9968 | } |
be99da77 | 9969 | } |
8d08fdba MS |
9970 | } |
9971 | ||
9972 | /* A function definition's declarator must have the form of | |
9973 | a function declarator. */ | |
9974 | ||
9975 | if (funcdef_flag && innermost_code != CALL_EXPR) | |
9976 | return 0; | |
9977 | ||
e1cd6e56 MS |
9978 | if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG) |
9979 | && innermost_code != CALL_EXPR | |
9980 | && ! (ctype && declspecs == NULL_TREE)) | |
9981 | { | |
33bd39a2 | 9982 | error ("declaration of `%D' as non-function", dname); |
e1cd6e56 MS |
9983 | return void_type_node; |
9984 | } | |
9985 | ||
8d08fdba MS |
9986 | /* Anything declared one level down from the top level |
9987 | must be one of the parameters of a function | |
9988 | (because the body is at least two levels down). */ | |
9989 | ||
9990 | /* This heuristic cannot be applied to C++ nodes! Fixed, however, | |
9991 | by not allowing C++ class definitions to specify their parameters | |
9992 | with xdecls (must be spec.d in the parmlist). | |
9993 | ||
9994 | Since we now wait to push a class scope until we are sure that | |
9995 | we are in a legitimate method context, we must set oldcname | |
a9aedbc2 MS |
9996 | explicitly (since current_class_name is not yet alive). |
9997 | ||
9998 | We also want to avoid calling this a PARM if it is in a namespace. */ | |
8d08fdba | 9999 | |
9188c363 | 10000 | if (decl_context == NORMAL && !toplevel_bindings_p ()) |
a9aedbc2 | 10001 | { |
e2500fed | 10002 | struct cp_binding_level *b = current_binding_level; |
ff955512 | 10003 | current_binding_level = b->level_chain; |
a9aedbc2 MS |
10004 | if (current_binding_level != 0 && toplevel_bindings_p ()) |
10005 | decl_context = PARM; | |
ff955512 | 10006 | current_binding_level = b; |
a9aedbc2 | 10007 | } |
8d08fdba | 10008 | |
34fc957d NS |
10009 | if (name == NULL) |
10010 | name = decl_context == PARM ? "parameter" : "type name"; | |
68642fb6 | 10011 | |
8d08fdba MS |
10012 | /* Look through the decl specs and record which ones appear. |
10013 | Some typespecs are defined as built-in typenames. | |
10014 | Others, the ones that are modifiers of other types, | |
10015 | are represented by bits in SPECBITS: set the bits for | |
10016 | the modifiers that appear. Storage class keywords are also in SPECBITS. | |
10017 | ||
10018 | If there is a typedef name or a type, store the type in TYPE. | |
10019 | This includes builtin typedefs such as `int'. | |
10020 | ||
10021 | Set EXPLICIT_INT if the type is `int' or `char' and did not | |
10022 | come from a user typedef. | |
10023 | ||
10024 | Set LONGLONG if `long' is mentioned twice. | |
10025 | ||
10026 | For C++, constructors and destructors have their own fast treatment. */ | |
10027 | ||
10028 | for (spec = declspecs; spec; spec = TREE_CHAIN (spec)) | |
10029 | { | |
10030 | register int i; | |
10031 | register tree id; | |
10032 | ||
10033 | /* Certain parse errors slip through. For example, | |
10034 | `int class;' is not caught by the parser. Try | |
10035 | weakly to recover here. */ | |
10036 | if (TREE_CODE (spec) != TREE_LIST) | |
10037 | return 0; | |
10038 | ||
10039 | id = TREE_VALUE (spec); | |
10040 | ||
e23bd218 IR |
10041 | /* If the entire declaration is itself tagged as deprecated then |
10042 | suppress reports of deprecated items. */ | |
10043 | if (!adding_implicit_members && id && TREE_DEPRECATED (id)) | |
10044 | { | |
10045 | if (deprecated_state != DEPRECATED_SUPPRESS) | |
10046 | warn_deprecated_use (id); | |
10047 | } | |
10048 | ||
8d08fdba MS |
10049 | if (TREE_CODE (id) == IDENTIFIER_NODE) |
10050 | { | |
a3203465 MS |
10051 | if (id == ridpointers[(int) RID_INT] |
10052 | || id == ridpointers[(int) RID_CHAR] | |
10053 | || id == ridpointers[(int) RID_BOOL] | |
10054 | || id == ridpointers[(int) RID_WCHAR]) | |
8d08fdba MS |
10055 | { |
10056 | if (type) | |
8ccc31eb MS |
10057 | { |
10058 | if (id == ridpointers[(int) RID_BOOL]) | |
8251199e | 10059 | error ("`bool' is now a keyword"); |
8ccc31eb | 10060 | else |
33bd39a2 | 10061 | error ("extraneous `%T' ignored", id); |
8ccc31eb | 10062 | } |
8d08fdba MS |
10063 | else |
10064 | { | |
a3203465 MS |
10065 | if (id == ridpointers[(int) RID_INT]) |
10066 | explicit_int = 1; | |
10067 | else if (id == ridpointers[(int) RID_CHAR]) | |
10068 | explicit_char = 1; | |
8d08fdba MS |
10069 | type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id)); |
10070 | } | |
10071 | goto found; | |
10072 | } | |
e92cc029 | 10073 | /* C++ aggregate types. */ |
8d08fdba MS |
10074 | if (IDENTIFIER_HAS_TYPE_VALUE (id)) |
10075 | { | |
10076 | if (type) | |
33bd39a2 | 10077 | error ("multiple declarations `%T' and `%T'", type, id); |
8d08fdba MS |
10078 | else |
10079 | type = IDENTIFIER_TYPE_VALUE (id); | |
10080 | goto found; | |
10081 | } | |
10082 | ||
f376e137 | 10083 | for (i = (int) RID_FIRST_MODIFIER; i <= (int) RID_LAST_MODIFIER; i++) |
8d08fdba MS |
10084 | { |
10085 | if (ridpointers[i] == id) | |
10086 | { | |
10087 | if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits)) | |
10088 | { | |
795add94 | 10089 | if (pedantic && ! in_system_header && warn_long_long) |
cb9a3ff8 | 10090 | pedwarn ("ISO C++ does not support `long long'"); |
9a3b49ac | 10091 | if (longlong) |
8251199e | 10092 | error ("`long long long' is too long for GCC"); |
8d08fdba MS |
10093 | else |
10094 | longlong = 1; | |
10095 | } | |
10096 | else if (RIDBIT_SETP (i, specbits)) | |
8251199e | 10097 | pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id)); |
7a1f3f5f | 10098 | |
946d5e45 MM |
10099 | /* Diagnose "__thread extern" or "__thread static". */ |
10100 | if (RIDBIT_SETP (RID_THREAD, specbits)) | |
7a1f3f5f | 10101 | { |
946d5e45 | 10102 | if (i == (int)RID_EXTERN) |
7a1f3f5f | 10103 | error ("`__thread' before `extern'"); |
946d5e45 | 10104 | else if (i == (int)RID_STATIC) |
7a1f3f5f RH |
10105 | error ("`__thread' before `static'"); |
10106 | } | |
10107 | ||
270d8c65 NS |
10108 | if (i == (int)RID_EXTERN |
10109 | && TREE_PURPOSE (spec) == error_mark_node) | |
10110 | /* This extern was part of a language linkage. */ | |
10111 | extern_langp = 1; | |
7a1f3f5f | 10112 | |
8d08fdba MS |
10113 | RIDBIT_SET (i, specbits); |
10114 | goto found; | |
10115 | } | |
10116 | } | |
10117 | } | |
1899c3a4 | 10118 | else if (TREE_CODE (id) == TYPE_DECL) |
45537677 MS |
10119 | { |
10120 | if (type) | |
33bd39a2 | 10121 | error ("multiple declarations `%T' and `%T'", type, |
45537677 MS |
10122 | TREE_TYPE (id)); |
10123 | else | |
5566b478 MS |
10124 | { |
10125 | type = TREE_TYPE (id); | |
10126 | TREE_VALUE (spec) = type; | |
8d6e459d | 10127 | typedef_decl = id; |
5566b478 | 10128 | } |
45537677 MS |
10129 | goto found; |
10130 | } | |
8d08fdba | 10131 | if (type) |
8251199e | 10132 | error ("two or more data types in declaration of `%s'", name); |
8d08fdba MS |
10133 | else if (TREE_CODE (id) == IDENTIFIER_NODE) |
10134 | { | |
10135 | register tree t = lookup_name (id, 1); | |
10136 | if (!t || TREE_CODE (t) != TYPE_DECL) | |
8251199e | 10137 | error ("`%s' fails to be a typedef or built in type", |
8d08fdba MS |
10138 | IDENTIFIER_POINTER (id)); |
10139 | else | |
10140 | { | |
10141 | type = TREE_TYPE (t); | |
10142 | typedef_decl = t; | |
10143 | } | |
10144 | } | |
bd6dd845 | 10145 | else if (id != error_mark_node) |
8d08fdba MS |
10146 | /* Can't change CLASS nodes into RECORD nodes here! */ |
10147 | type = id; | |
10148 | ||
10149 | found: ; | |
10150 | } | |
10151 | ||
8d6e459d NS |
10152 | #if 0 |
10153 | /* See the code below that used this. */ | |
10154 | if (typedef_decl) | |
10155 | decl_attr = DECL_ATTRIBUTES (typedef_decl); | |
10156 | #endif | |
8d08fdba MS |
10157 | typedef_type = type; |
10158 | ||
37c46b43 | 10159 | /* No type at all: default to `int', and set DEFAULTED_INT |
6eabb241 | 10160 | because it was not a user-defined typedef. */ |
8d08fdba | 10161 | |
a3203465 MS |
10162 | if (type == NULL_TREE |
10163 | && (RIDBIT_SETP (RID_SIGNED, specbits) | |
10164 | || RIDBIT_SETP (RID_UNSIGNED, specbits) | |
10165 | || RIDBIT_SETP (RID_LONG, specbits) | |
10166 | || RIDBIT_SETP (RID_SHORT, specbits))) | |
10167 | { | |
10168 | /* These imply 'int'. */ | |
10169 | type = integer_type_node; | |
37c46b43 | 10170 | defaulted_int = 1; |
a3203465 MS |
10171 | } |
10172 | ||
3dbc07b6 MM |
10173 | if (sfk != sfk_none) |
10174 | type = check_special_function_return_type (sfk, type, | |
3dbc07b6 MM |
10175 | ctor_return_type); |
10176 | else if (type == NULL_TREE) | |
8d08fdba | 10177 | { |
3dbc07b6 MM |
10178 | int is_main; |
10179 | ||
8d08fdba | 10180 | explicit_int = -1; |
3dbc07b6 | 10181 | |
0fd0b7ee JM |
10182 | /* We handle `main' specially here, because 'main () { }' is so |
10183 | common. With no options, it is allowed. With -Wreturn-type, | |
10184 | it is a warning. It is only an error with -pedantic-errors. */ | |
3dbc07b6 | 10185 | is_main = (funcdef_flag |
7437519c | 10186 | && dname && MAIN_NAME_P (dname) |
3dbc07b6 MM |
10187 | && ctype == NULL_TREE |
10188 | && in_namespace == NULL_TREE | |
10189 | && current_namespace == global_namespace); | |
10190 | ||
10191 | if (in_system_header || flag_ms_extensions) | |
10192 | /* Allow it, sigh. */; | |
10193 | else if (pedantic || ! is_main) | |
33bd39a2 | 10194 | pedwarn ("ISO C++ forbids declaration of `%s' with no type", |
3dbc07b6 MM |
10195 | name); |
10196 | else if (warn_return_type) | |
33bd39a2 | 10197 | warning ("ISO C++ forbids declaration of `%s' with no type", |
3dbc07b6 | 10198 | name); |
41eff652 | 10199 | |
3dbc07b6 | 10200 | type = integer_type_node; |
51c184be | 10201 | } |
ad1a6d45 | 10202 | |
8d08fdba MS |
10203 | ctype = NULL_TREE; |
10204 | ||
10205 | /* Now process the modifiers that were specified | |
10206 | and check for invalid combinations. */ | |
10207 | ||
10208 | /* Long double is a special combination. */ | |
10209 | ||
10210 | if (RIDBIT_SETP (RID_LONG, specbits) | |
10211 | && TYPE_MAIN_VARIANT (type) == double_type_node) | |
10212 | { | |
10213 | RIDBIT_RESET (RID_LONG, specbits); | |
68642fb6 | 10214 | type = build_qualified_type (long_double_type_node, |
89d684bb | 10215 | cp_type_quals (type)); |
8d08fdba MS |
10216 | } |
10217 | ||
10218 | /* Check all other uses of type modifiers. */ | |
10219 | ||
10220 | if (RIDBIT_SETP (RID_UNSIGNED, specbits) | |
10221 | || RIDBIT_SETP (RID_SIGNED, specbits) | |
10222 | || RIDBIT_SETP (RID_LONG, specbits) | |
10223 | || RIDBIT_SETP (RID_SHORT, specbits)) | |
10224 | { | |
10225 | int ok = 0; | |
10226 | ||
10227 | if (TREE_CODE (type) == REAL_TYPE) | |
8251199e | 10228 | error ("short, signed or unsigned invalid for `%s'", name); |
b7484fbe | 10229 | else if (TREE_CODE (type) != INTEGER_TYPE) |
8251199e | 10230 | error ("long, short, signed or unsigned invalid for `%s'", name); |
8d08fdba MS |
10231 | else if (RIDBIT_SETP (RID_LONG, specbits) |
10232 | && RIDBIT_SETP (RID_SHORT, specbits)) | |
8251199e | 10233 | error ("long and short specified together for `%s'", name); |
8d08fdba MS |
10234 | else if ((RIDBIT_SETP (RID_LONG, specbits) |
10235 | || RIDBIT_SETP (RID_SHORT, specbits)) | |
10236 | && explicit_char) | |
8251199e | 10237 | error ("long or short specified with char for `%s'", name); |
8d08fdba MS |
10238 | else if ((RIDBIT_SETP (RID_LONG, specbits) |
10239 | || RIDBIT_SETP (RID_SHORT, specbits)) | |
10240 | && TREE_CODE (type) == REAL_TYPE) | |
8251199e | 10241 | error ("long or short specified with floating type for `%s'", name); |
8d08fdba MS |
10242 | else if (RIDBIT_SETP (RID_SIGNED, specbits) |
10243 | && RIDBIT_SETP (RID_UNSIGNED, specbits)) | |
8251199e | 10244 | error ("signed and unsigned given together for `%s'", name); |
8d08fdba MS |
10245 | else |
10246 | { | |
10247 | ok = 1; | |
37c46b43 | 10248 | if (!explicit_int && !defaulted_int && !explicit_char && pedantic) |
8d08fdba | 10249 | { |
8251199e | 10250 | pedwarn ("long, short, signed or unsigned used invalidly for `%s'", |
8d08fdba MS |
10251 | name); |
10252 | if (flag_pedantic_errors) | |
10253 | ok = 0; | |
10254 | } | |
10255 | } | |
10256 | ||
10257 | /* Discard the type modifiers if they are invalid. */ | |
10258 | if (! ok) | |
10259 | { | |
10260 | RIDBIT_RESET (RID_UNSIGNED, specbits); | |
10261 | RIDBIT_RESET (RID_SIGNED, specbits); | |
10262 | RIDBIT_RESET (RID_LONG, specbits); | |
10263 | RIDBIT_RESET (RID_SHORT, specbits); | |
10264 | longlong = 0; | |
10265 | } | |
10266 | } | |
10267 | ||
37c46b43 MS |
10268 | if (RIDBIT_SETP (RID_COMPLEX, specbits) |
10269 | && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE) | |
10270 | { | |
8251199e | 10271 | error ("complex invalid for `%s'", name); |
37c46b43 MS |
10272 | RIDBIT_RESET (RID_COMPLEX, specbits); |
10273 | } | |
10274 | ||
8d08fdba MS |
10275 | /* Decide whether an integer type is signed or not. |
10276 | Optionally treat bitfields as signed by default. */ | |
10277 | if (RIDBIT_SETP (RID_UNSIGNED, specbits) | |
b89c5a7b MM |
10278 | /* [class.bit] |
10279 | ||
10280 | It is implementation-defined whether a plain (neither | |
10281 | explicitly signed or unsigned) char, short, int, or long | |
10282 | bit-field is signed or unsigned. | |
68642fb6 | 10283 | |
b89c5a7b MM |
10284 | Naturally, we extend this to long long as well. Note that |
10285 | this does not include wchar_t. */ | |
10286 | || (bitfield && !flag_signed_bitfields | |
10287 | && RIDBIT_NOTSETP (RID_SIGNED, specbits) | |
10288 | /* A typedef for plain `int' without `signed' can be | |
10289 | controlled just like plain `int', but a typedef for | |
10290 | `signed int' cannot be so controlled. */ | |
68642fb6 | 10291 | && !(typedef_decl |
29bbeb1c | 10292 | && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)) |
b89c5a7b MM |
10293 | && (TREE_CODE (type) == INTEGER_TYPE |
10294 | || TREE_CODE (type) == CHAR_TYPE) | |
29bbeb1c | 10295 | && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node))) |
8d08fdba MS |
10296 | { |
10297 | if (longlong) | |
10298 | type = long_long_unsigned_type_node; | |
10299 | else if (RIDBIT_SETP (RID_LONG, specbits)) | |
10300 | type = long_unsigned_type_node; | |
10301 | else if (RIDBIT_SETP (RID_SHORT, specbits)) | |
10302 | type = short_unsigned_type_node; | |
10303 | else if (type == char_type_node) | |
10304 | type = unsigned_char_type_node; | |
10305 | else if (typedef_decl) | |
ceef8ce4 | 10306 | type = c_common_unsigned_type (type); |
8d08fdba MS |
10307 | else |
10308 | type = unsigned_type_node; | |
10309 | } | |
10310 | else if (RIDBIT_SETP (RID_SIGNED, specbits) | |
10311 | && type == char_type_node) | |
10312 | type = signed_char_type_node; | |
10313 | else if (longlong) | |
10314 | type = long_long_integer_type_node; | |
10315 | else if (RIDBIT_SETP (RID_LONG, specbits)) | |
10316 | type = long_integer_type_node; | |
10317 | else if (RIDBIT_SETP (RID_SHORT, specbits)) | |
10318 | type = short_integer_type_node; | |
10319 | ||
37c46b43 MS |
10320 | if (RIDBIT_SETP (RID_COMPLEX, specbits)) |
10321 | { | |
10322 | /* If we just have "complex", it is equivalent to | |
10323 | "complex double", but if any modifiers at all are specified it is | |
10324 | the complex form of TYPE. E.g, "complex short" is | |
10325 | "complex short int". */ | |
10326 | ||
10327 | if (defaulted_int && ! longlong | |
10328 | && ! (RIDBIT_SETP (RID_LONG, specbits) | |
10329 | || RIDBIT_SETP (RID_SHORT, specbits) | |
10330 | || RIDBIT_SETP (RID_SIGNED, specbits) | |
10331 | || RIDBIT_SETP (RID_UNSIGNED, specbits))) | |
10332 | type = complex_double_type_node; | |
10333 | else if (type == integer_type_node) | |
10334 | type = complex_integer_type_node; | |
10335 | else if (type == float_type_node) | |
10336 | type = complex_float_type_node; | |
10337 | else if (type == double_type_node) | |
10338 | type = complex_double_type_node; | |
10339 | else if (type == long_double_type_node) | |
10340 | type = complex_long_double_type_node; | |
10341 | else | |
10342 | type = build_complex_type (type); | |
10343 | } | |
10344 | ||
4f2b0fb2 NS |
10345 | type_quals = TYPE_UNQUALIFIED; |
10346 | if (RIDBIT_SETP (RID_CONST, specbits)) | |
10347 | type_quals |= TYPE_QUAL_CONST; | |
10348 | if (RIDBIT_SETP (RID_VOLATILE, specbits)) | |
10349 | type_quals |= TYPE_QUAL_VOLATILE; | |
10350 | if (RIDBIT_SETP (RID_RESTRICT, specbits)) | |
10351 | type_quals |= TYPE_QUAL_RESTRICT; | |
10352 | if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED) | |
33bd39a2 | 10353 | error ("qualifiers are not allowed on declaration of `operator %T'", |
3ac3d9ea MM |
10354 | ctor_return_type); |
10355 | ||
4f2b0fb2 NS |
10356 | type_quals |= cp_type_quals (type); |
10357 | type = cp_build_qualified_type_real | |
10358 | (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl) | |
10359 | ? tf_ignore_bad_quals : 0) | tf_error | tf_warning)); | |
10360 | /* We might have ignored or rejected some of the qualifiers. */ | |
10361 | type_quals = cp_type_quals (type); | |
10362 | ||
8d08fdba MS |
10363 | staticp = 0; |
10364 | inlinep = !! RIDBIT_SETP (RID_INLINE, specbits); | |
8d08fdba | 10365 | virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits); |
db5ae43f MS |
10366 | RIDBIT_RESET (RID_VIRTUAL, specbits); |
10367 | explicitp = RIDBIT_SETP (RID_EXPLICIT, specbits) != 0; | |
10368 | RIDBIT_RESET (RID_EXPLICIT, specbits); | |
8d08fdba | 10369 | |
8d08fdba MS |
10370 | if (RIDBIT_SETP (RID_STATIC, specbits)) |
10371 | staticp = 1 + (decl_context == FIELD); | |
10372 | ||
10373 | if (virtualp && staticp == 2) | |
10374 | { | |
33bd39a2 | 10375 | error ("member `%D' cannot be declared both virtual and static", |
8d08fdba MS |
10376 | dname); |
10377 | staticp = 0; | |
10378 | } | |
10379 | friendp = RIDBIT_SETP (RID_FRIEND, specbits); | |
8d08fdba MS |
10380 | RIDBIT_RESET (RID_FRIEND, specbits); |
10381 | ||
d5614afb NS |
10382 | if (dependant_name && !friendp) |
10383 | { | |
10384 | error ("`%T::%D' is not a valid declarator", ctype, dependant_name); | |
10385 | return void_type_node; | |
10386 | } | |
10387 | ||
8d08fdba MS |
10388 | /* Warn if two storage classes are given. Default to `auto'. */ |
10389 | ||
10390 | if (RIDBIT_ANY_SET (specbits)) | |
10391 | { | |
10392 | if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++; | |
270d8c65 | 10393 | if (RIDBIT_SETP (RID_EXTERN, specbits) && !extern_langp) nclasses++; |
7a1f3f5f | 10394 | if (RIDBIT_SETP (RID_THREAD, specbits)) nclasses++; |
8d08fdba | 10395 | if (decl_context == PARM && nclasses > 0) |
8251199e | 10396 | error ("storage class specifiers invalid in parameter declarations"); |
8d08fdba MS |
10397 | if (RIDBIT_SETP (RID_TYPEDEF, specbits)) |
10398 | { | |
10399 | if (decl_context == PARM) | |
8251199e | 10400 | error ("typedef declaration invalid in parameter declaration"); |
8d08fdba MS |
10401 | nclasses++; |
10402 | } | |
10403 | if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++; | |
10404 | if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++; | |
270d8c65 NS |
10405 | if (!nclasses && !friendp && extern_langp) |
10406 | nclasses++; | |
8d08fdba MS |
10407 | } |
10408 | ||
10409 | /* Give error if `virtual' is used outside of class declaration. */ | |
b7484fbe MS |
10410 | if (virtualp |
10411 | && (current_class_name == NULL_TREE || decl_context != FIELD)) | |
8d08fdba | 10412 | { |
8251199e | 10413 | error ("virtual outside class declaration"); |
8d08fdba MS |
10414 | virtualp = 0; |
10415 | } | |
8d08fdba MS |
10416 | |
10417 | /* Static anonymous unions are dealt with here. */ | |
10418 | if (staticp && decl_context == TYPENAME | |
10419 | && TREE_CODE (declspecs) == TREE_LIST | |
6bdb8141 | 10420 | && ANON_AGGR_TYPE_P (TREE_VALUE (declspecs))) |
8d08fdba MS |
10421 | decl_context = FIELD; |
10422 | ||
8d08fdba MS |
10423 | /* Warn about storage classes that are invalid for certain |
10424 | kinds of declarations (parameters, typenames, etc.). */ | |
10425 | ||
7a1f3f5f RH |
10426 | /* "static __thread" and "extern __thread" are allowed. */ |
10427 | if (nclasses == 2 | |
10428 | && RIDBIT_SETP (RID_THREAD, specbits) | |
10429 | && (RIDBIT_SETP (RID_EXTERN, specbits) | |
10430 | || RIDBIT_SETP (RID_STATIC, specbits))) | |
10431 | nclasses = 1; | |
10432 | ||
8d08fdba | 10433 | if (nclasses > 1) |
8251199e | 10434 | error ("multiple storage classes in declaration of `%s'", name); |
8d08fdba MS |
10435 | else if (decl_context != NORMAL && nclasses > 0) |
10436 | { | |
db5ae43f | 10437 | if ((decl_context == PARM || decl_context == CATCHPARM) |
8d08fdba MS |
10438 | && (RIDBIT_SETP (RID_REGISTER, specbits) |
10439 | || RIDBIT_SETP (RID_AUTO, specbits))) | |
10440 | ; | |
fc378698 MS |
10441 | else if (RIDBIT_SETP (RID_TYPEDEF, specbits)) |
10442 | ; | |
8d08fdba | 10443 | else if (decl_context == FIELD |
d363e7bf AJ |
10444 | /* C++ allows static class elements */ |
10445 | && RIDBIT_SETP (RID_STATIC, specbits)) | |
10446 | /* C++ also allows inlines and signed and unsigned elements, | |
10447 | but in those cases we don't come in here. */ | |
8d08fdba MS |
10448 | ; |
10449 | else | |
10450 | { | |
10451 | if (decl_context == FIELD) | |
10452 | { | |
b7484fbe MS |
10453 | tree tmp = NULL_TREE; |
10454 | register int op = 0; | |
10455 | ||
10456 | if (declarator) | |
10457 | { | |
68642fb6 | 10458 | /* Avoid trying to get an operand off an identifier node. */ |
9e9ff709 MS |
10459 | if (TREE_CODE (declarator) == IDENTIFIER_NODE) |
10460 | tmp = declarator; | |
10461 | else | |
10462 | tmp = TREE_OPERAND (declarator, 0); | |
b7484fbe | 10463 | op = IDENTIFIER_OPNAME_P (tmp); |
596ea4e5 AS |
10464 | if (IDENTIFIER_TYPENAME_P (tmp)) |
10465 | { | |
10466 | if (IDENTIFIER_GLOBAL_VALUE (tmp) | |
5362b086 | 10467 | && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (tmp)) |
596ea4e5 AS |
10468 | == TYPE_DECL)) |
10469 | name = IDENTIFIER_POINTER (tmp); | |
10470 | else | |
10471 | name = "<invalid operator>"; | |
10472 | } | |
b7484fbe | 10473 | } |
8251199e | 10474 | error ("storage class specified for %s `%s'", |
6eabb241 | 10475 | op ? "member operator" : "field", |
596ea4e5 | 10476 | name); |
8d08fdba MS |
10477 | } |
10478 | else | |
b9d12519 KG |
10479 | { |
10480 | if (decl_context == PARM || decl_context == CATCHPARM) | |
10481 | error ("storage class specified for parameter `%s'", name); | |
10482 | else | |
10483 | error ("storage class specified for typename"); | |
10484 | } | |
8d08fdba MS |
10485 | RIDBIT_RESET (RID_REGISTER, specbits); |
10486 | RIDBIT_RESET (RID_AUTO, specbits); | |
10487 | RIDBIT_RESET (RID_EXTERN, specbits); | |
7a1f3f5f | 10488 | RIDBIT_RESET (RID_THREAD, specbits); |
8d08fdba MS |
10489 | } |
10490 | } | |
10491 | else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag) | |
10492 | { | |
a9aedbc2 | 10493 | if (toplevel_bindings_p ()) |
8d08fdba | 10494 | { |
59be0cdd | 10495 | /* It's common practice (and completely valid) to have a const |
8d08fdba | 10496 | be initialized and declared extern. */ |
950ad3c3 | 10497 | if (!(type_quals & TYPE_QUAL_CONST)) |
8251199e | 10498 | warning ("`%s' initialized and declared `extern'", name); |
8d08fdba MS |
10499 | } |
10500 | else | |
8251199e | 10501 | error ("`%s' has both `extern' and initializer", name); |
8d08fdba MS |
10502 | } |
10503 | else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag | |
a9aedbc2 | 10504 | && ! toplevel_bindings_p ()) |
8251199e | 10505 | error ("nested function `%s' declared `extern'", name); |
a9aedbc2 | 10506 | else if (toplevel_bindings_p ()) |
8d08fdba MS |
10507 | { |
10508 | if (RIDBIT_SETP (RID_AUTO, specbits)) | |
8251199e | 10509 | error ("top-level declaration of `%s' specifies `auto'", name); |
8d08fdba | 10510 | } |
7a1f3f5f RH |
10511 | else if (RIDBIT_SETP (RID_THREAD, specbits) |
10512 | && !RIDBIT_SETP (RID_EXTERN, specbits) | |
10513 | && !RIDBIT_SETP (RID_STATIC, specbits)) | |
10514 | { | |
10515 | error ("function-scope `%s' implicitly auto and declared `__thread'", | |
10516 | name); | |
10517 | RIDBIT_RESET (RID_THREAD, specbits); | |
10518 | } | |
8d08fdba | 10519 | |
909e536a | 10520 | if (nclasses > 0 && friendp) |
8251199e | 10521 | error ("storage class specifiers invalid in friend function declarations"); |
909e536a | 10522 | |
a723baf1 MM |
10523 | scope = get_scope_of_declarator (declarator); |
10524 | ||
8d08fdba MS |
10525 | /* Now figure out the structure of the declarator proper. |
10526 | Descend through it, creating more complex types, until we reach | |
9e259dd1 | 10527 | the declared identifier (or NULL_TREE, in an abstract declarator). */ |
8d08fdba | 10528 | |
386b8a85 JM |
10529 | while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE |
10530 | && TREE_CODE (declarator) != TEMPLATE_ID_EXPR) | |
8d08fdba MS |
10531 | { |
10532 | /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]), | |
10533 | an INDIRECT_REF (for *...), | |
10534 | a CALL_EXPR (for ...(...)), | |
10535 | an identifier (for the name being declared) | |
10536 | or a null pointer (for the place in an absolute declarator | |
10537 | where the name was omitted). | |
10538 | For the last two cases, we have just exited the loop. | |
10539 | ||
10540 | For C++ it could also be | |
10541 | a SCOPE_REF (for class :: ...). In this case, we have converted | |
10542 | sensible names to types, and those are the values we use to | |
10543 | qualify the member name. | |
10544 | an ADDR_EXPR (for &...), | |
10545 | a BIT_NOT_EXPR (for destructors) | |
8d08fdba MS |
10546 | |
10547 | At this point, TYPE is the type of elements of an array, | |
10548 | or for a function to return, or for a pointer to point to. | |
10549 | After this sequence of ifs, TYPE is the type of the | |
10550 | array or function or pointer, and DECLARATOR has had its | |
10551 | outermost layer removed. */ | |
10552 | ||
bd6dd845 | 10553 | if (type == error_mark_node) |
8d08fdba | 10554 | { |
88d5a16e R |
10555 | if (declarator == error_mark_node) |
10556 | return error_mark_node; | |
10557 | else if (TREE_CODE (declarator) == SCOPE_REF) | |
8d08fdba MS |
10558 | declarator = TREE_OPERAND (declarator, 1); |
10559 | else | |
10560 | declarator = TREE_OPERAND (declarator, 0); | |
10561 | continue; | |
10562 | } | |
10563 | if (quals != NULL_TREE | |
10564 | && (declarator == NULL_TREE | |
10565 | || TREE_CODE (declarator) != SCOPE_REF)) | |
10566 | { | |
10567 | if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE) | |
10568 | ctype = TYPE_METHOD_BASETYPE (type); | |
10569 | if (ctype != NULL_TREE) | |
10570 | { | |
8d08fdba | 10571 | tree dummy = build_decl (TYPE_DECL, NULL_TREE, type); |
535233a8 | 10572 | grok_method_quals (ctype, dummy, quals); |
8d08fdba MS |
10573 | type = TREE_TYPE (dummy); |
10574 | quals = NULL_TREE; | |
10575 | } | |
10576 | } | |
1eab9b56 | 10577 | |
8d08fdba MS |
10578 | switch (TREE_CODE (declarator)) |
10579 | { | |
52fbc847 JM |
10580 | case TREE_LIST: |
10581 | { | |
10582 | /* We encode a declarator with embedded attributes using | |
91d231cb JM |
10583 | a TREE_LIST. */ |
10584 | tree attrs = TREE_PURPOSE (declarator); | |
10585 | tree inner_decl; | |
c9574c9f R |
10586 | int attr_flags; |
10587 | ||
52fbc847 | 10588 | declarator = TREE_VALUE (declarator); |
91d231cb JM |
10589 | inner_decl = declarator; |
10590 | while (inner_decl != NULL_TREE | |
10591 | && TREE_CODE (inner_decl) == TREE_LIST) | |
10592 | inner_decl = TREE_VALUE (inner_decl); | |
c9574c9f | 10593 | attr_flags = 0; |
91d231cb JM |
10594 | if (inner_decl == NULL_TREE |
10595 | || TREE_CODE (inner_decl) == IDENTIFIER_NODE) | |
10596 | attr_flags |= (int) ATTR_FLAG_DECL_NEXT; | |
10597 | if (TREE_CODE (inner_decl) == CALL_EXPR) | |
10598 | attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT; | |
10599 | if (TREE_CODE (inner_decl) == ARRAY_REF) | |
10600 | attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT; | |
10601 | returned_attrs = decl_attributes (&type, | |
10602 | chainon (returned_attrs, attrs), | |
10603 | attr_flags); | |
52fbc847 JM |
10604 | } |
10605 | break; | |
10606 | ||
8d08fdba MS |
10607 | case ARRAY_REF: |
10608 | { | |
2848ad0f | 10609 | register tree size; |
8d08fdba | 10610 | |
2848ad0f | 10611 | size = TREE_OPERAND (declarator, 1); |
8d08fdba | 10612 | |
f2e2cbd4 | 10613 | /* VC++ spells a zero-sized array with []. */ |
e8ea7036 MM |
10614 | if (size == NULL_TREE && decl_context == FIELD && ! staticp |
10615 | && ! RIDBIT_SETP (RID_TYPEDEF, specbits)) | |
f2e2cbd4 JM |
10616 | size = integer_zero_node; |
10617 | ||
2848ad0f | 10618 | declarator = TREE_OPERAND (declarator, 0); |
8d08fdba | 10619 | |
2848ad0f | 10620 | type = create_array_type_for_decl (dname, type, size); |
c1b177ec | 10621 | |
8d08fdba MS |
10622 | ctype = NULL_TREE; |
10623 | } | |
10624 | break; | |
10625 | ||
10626 | case CALL_EXPR: | |
10627 | { | |
10628 | tree arg_types; | |
f376e137 | 10629 | int funcdecl_p; |
43f887f9 | 10630 | tree inner_parms = CALL_DECLARATOR_PARMS (declarator); |
f376e137 | 10631 | tree inner_decl = TREE_OPERAND (declarator, 0); |
8d08fdba MS |
10632 | |
10633 | /* Declaring a function type. | |
10634 | Make sure we have a valid type for the function to return. */ | |
8d08fdba | 10635 | |
91063b51 | 10636 | /* We now know that the TYPE_QUALS don't apply to the |
14ae7e7d | 10637 | decl, but to its return type. */ |
91063b51 | 10638 | type_quals = TYPE_UNQUALIFIED; |
8d08fdba MS |
10639 | |
10640 | /* Warn about some types functions can't return. */ | |
10641 | ||
10642 | if (TREE_CODE (type) == FUNCTION_TYPE) | |
10643 | { | |
8251199e | 10644 | error ("`%s' declared as function returning a function", name); |
8d08fdba MS |
10645 | type = integer_type_node; |
10646 | } | |
10647 | if (TREE_CODE (type) == ARRAY_TYPE) | |
10648 | { | |
8251199e | 10649 | error ("`%s' declared as function returning an array", name); |
8d08fdba MS |
10650 | type = integer_type_node; |
10651 | } | |
10652 | ||
f376e137 MS |
10653 | if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF) |
10654 | inner_decl = TREE_OPERAND (inner_decl, 1); | |
10655 | ||
68642fb6 | 10656 | if (inner_decl && TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR) |
386b8a85 JM |
10657 | inner_decl = dname; |
10658 | ||
b7484fbe | 10659 | /* Pick up type qualifiers which should be applied to `this'. */ |
43f887f9 | 10660 | quals = CALL_DECLARATOR_QUALS (declarator); |
b7484fbe | 10661 | |
c11b6f21 | 10662 | /* Pick up the exception specifications. */ |
43f887f9 | 10663 | raises = CALL_DECLARATOR_EXCEPTION_SPEC (declarator); |
c11b6f21 | 10664 | |
f376e137 MS |
10665 | /* Say it's a definition only for the CALL_EXPR |
10666 | closest to the identifier. */ | |
beb53fb8 | 10667 | funcdecl_p |
68642fb6 | 10668 | = inner_decl |
386b8a85 | 10669 | && (TREE_CODE (inner_decl) == IDENTIFIER_NODE |
68642fb6 | 10670 | || TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR |
386b8a85 | 10671 | || TREE_CODE (inner_decl) == BIT_NOT_EXPR); |
68642fb6 | 10672 | |
8d08fdba MS |
10673 | if (ctype == NULL_TREE |
10674 | && decl_context == FIELD | |
f376e137 | 10675 | && funcdecl_p |
8d08fdba MS |
10676 | && (friendp == 0 || dname == current_class_name)) |
10677 | ctype = current_class_type; | |
10678 | ||
3dbc07b6 | 10679 | if (ctype && sfk == sfk_conversion) |
8d08fdba | 10680 | TYPE_HAS_CONVERSION (ctype) = 1; |
a723baf1 | 10681 | if (ctype && constructor_name_p (dname, ctype)) |
8d08fdba MS |
10682 | { |
10683 | /* We are within a class's scope. If our declarator name | |
10684 | is the same as the class name, and we are defining | |
10685 | a function, then it is a constructor/destructor, and | |
10686 | therefore returns a void type. */ | |
10687 | ||
10688 | if (flags == DTOR_FLAG) | |
10689 | { | |
cab1f180 ML |
10690 | /* ISO C++ 12.4/2. A destructor may not be |
10691 | declared const or volatile. A destructor may | |
10692 | not be static. */ | |
8d08fdba | 10693 | if (staticp == 2) |
8251199e | 10694 | error ("destructor cannot be static member function"); |
b7484fbe | 10695 | if (quals) |
8d08fdba | 10696 | { |
33bd39a2 | 10697 | error ("destructors may not be `%s'", |
4f70a846 MM |
10698 | IDENTIFIER_POINTER (TREE_VALUE (quals))); |
10699 | quals = NULL_TREE; | |
8d08fdba MS |
10700 | } |
10701 | if (decl_context == FIELD) | |
10702 | { | |
4dacf5bd KG |
10703 | if (! member_function_or_else (ctype, |
10704 | current_class_type, | |
10705 | flags)) | |
8d08fdba MS |
10706 | return void_type_node; |
10707 | } | |
10708 | } | |
2c73f9f5 | 10709 | else /* It's a constructor. */ |
8d08fdba | 10710 | { |
db5ae43f MS |
10711 | if (explicitp == 1) |
10712 | explicitp = 2; | |
cab1f180 ML |
10713 | /* ISO C++ 12.1. A constructor may not be |
10714 | declared const or volatile. A constructor may | |
10715 | not be virtual. A constructor may not be | |
10716 | static. */ | |
8d08fdba | 10717 | if (staticp == 2) |
8251199e | 10718 | error ("constructor cannot be static member function"); |
8d08fdba MS |
10719 | if (virtualp) |
10720 | { | |
8251199e | 10721 | pedwarn ("constructors cannot be declared virtual"); |
8d08fdba MS |
10722 | virtualp = 0; |
10723 | } | |
b7484fbe | 10724 | if (quals) |
8d08fdba | 10725 | { |
33bd39a2 | 10726 | error ("constructors may not be `%s'", |
4f70a846 MM |
10727 | IDENTIFIER_POINTER (TREE_VALUE (quals))); |
10728 | quals = NULL_TREE; | |
d363e7bf | 10729 | } |
8d08fdba | 10730 | { |
51c184be | 10731 | RID_BIT_TYPE tmp_bits; |
4e135bdd | 10732 | memcpy (&tmp_bits, &specbits, sizeof (RID_BIT_TYPE)); |
51c184be MS |
10733 | RIDBIT_RESET (RID_INLINE, tmp_bits); |
10734 | RIDBIT_RESET (RID_STATIC, tmp_bits); | |
10735 | if (RIDBIT_ANY_SET (tmp_bits)) | |
8251199e | 10736 | error ("return value type specifier for constructor ignored"); |
8d08fdba | 10737 | } |
6eabb241 | 10738 | if (decl_context == FIELD) |
8d08fdba | 10739 | { |
4dacf5bd KG |
10740 | if (! member_function_or_else (ctype, |
10741 | current_class_type, | |
10742 | flags)) | |
8d08fdba MS |
10743 | return void_type_node; |
10744 | TYPE_HAS_CONSTRUCTOR (ctype) = 1; | |
3dbc07b6 | 10745 | if (sfk != sfk_constructor) |
8d08fdba MS |
10746 | return NULL_TREE; |
10747 | } | |
10748 | } | |
10749 | if (decl_context == FIELD) | |
10750 | staticp = 0; | |
10751 | } | |
b7484fbe | 10752 | else if (friendp) |
8d08fdba | 10753 | { |
b7484fbe | 10754 | if (initialized) |
8251199e | 10755 | error ("can't initialize friend function `%s'", name); |
b7484fbe MS |
10756 | if (virtualp) |
10757 | { | |
10758 | /* Cannot be both friend and virtual. */ | |
8251199e | 10759 | error ("virtual functions cannot be friends"); |
b7484fbe MS |
10760 | RIDBIT_RESET (RID_FRIEND, specbits); |
10761 | friendp = 0; | |
10762 | } | |
28cbf42c | 10763 | if (decl_context == NORMAL) |
8251199e | 10764 | error ("friend declaration not in class definition"); |
28cbf42c | 10765 | if (current_function_decl && funcdef_flag) |
33bd39a2 | 10766 | error ("can't define friend function `%s' in a local class definition", |
28cbf42c | 10767 | name); |
8d08fdba MS |
10768 | } |
10769 | ||
8d08fdba MS |
10770 | /* Construct the function type and go to the next |
10771 | inner layer of declarator. */ | |
10772 | ||
f376e137 | 10773 | declarator = TREE_OPERAND (declarator, 0); |
8d08fdba | 10774 | |
f376e137 MS |
10775 | /* FIXME: This is where default args should be fully |
10776 | processed. */ | |
8d08fdba | 10777 | |
5cce22b6 | 10778 | arg_types = grokparms (inner_parms); |
8d08fdba | 10779 | |
4f70a846 | 10780 | if (declarator && flags == DTOR_FLAG) |
8d08fdba | 10781 | { |
4f70a846 MM |
10782 | /* A destructor declared in the body of a class will |
10783 | be represented as a BIT_NOT_EXPR. But, we just | |
10784 | want the underlying IDENTIFIER. */ | |
8d08fdba | 10785 | if (TREE_CODE (declarator) == BIT_NOT_EXPR) |
4f70a846 | 10786 | declarator = TREE_OPERAND (declarator, 0); |
68642fb6 | 10787 | |
8b27e9ef | 10788 | if (arg_types != void_list_node) |
8d08fdba | 10789 | { |
33bd39a2 | 10790 | error ("destructors may not have parameters"); |
4f70a846 MM |
10791 | arg_types = void_list_node; |
10792 | last_function_parms = NULL_TREE; | |
8d08fdba MS |
10793 | } |
10794 | } | |
10795 | ||
d22c8596 | 10796 | /* ANSI says that `const int foo ();' |
8d08fdba | 10797 | does not make the function foo const. */ |
d22c8596 | 10798 | type = build_function_type (type, arg_types); |
8d08fdba MS |
10799 | } |
10800 | break; | |
10801 | ||
10802 | case ADDR_EXPR: | |
10803 | case INDIRECT_REF: | |
10804 | /* Filter out pointers-to-references and references-to-references. | |
10805 | We can get these if a TYPE_DECL is used. */ | |
10806 | ||
10807 | if (TREE_CODE (type) == REFERENCE_TYPE) | |
10808 | { | |
dd29188b NS |
10809 | error (TREE_CODE (declarator) == ADDR_EXPR |
10810 | ? "cannot declare reference to `%#T'" | |
10811 | : "cannot declare pointer to `%#T'", type); | |
a5894242 MS |
10812 | type = TREE_TYPE (type); |
10813 | } | |
dd29188b NS |
10814 | else if (VOID_TYPE_P (type) |
10815 | && (ctype || TREE_CODE (declarator) == ADDR_EXPR)) | |
10816 | error (ctype ? "cannot declare pointer to `%#T' member" | |
10817 | : "cannot declare reference to `%#T'", type); | |
a5894242 | 10818 | |
61a127b3 MM |
10819 | /* Merge any constancy or volatility into the target type |
10820 | for the pointer. */ | |
10821 | ||
91063b51 MM |
10822 | /* We now know that the TYPE_QUALS don't apply to the decl, |
10823 | but to the target of the pointer. */ | |
10824 | type_quals = TYPE_UNQUALIFIED; | |
8d08fdba | 10825 | |
6eabb241 | 10826 | if (TREE_CODE (declarator) == ADDR_EXPR) |
8d08fdba | 10827 | { |
dd29188b | 10828 | if (!VOID_TYPE_P (type)) |
69851283 | 10829 | type = build_reference_type (type); |
8d08fdba MS |
10830 | } |
10831 | else if (TREE_CODE (type) == METHOD_TYPE) | |
d8f8dca1 | 10832 | type = build_ptrmemfunc_type (build_pointer_type (type)); |
9e259dd1 MM |
10833 | else if (ctype) |
10834 | type = build_ptrmem_type (ctype, type); | |
8d08fdba MS |
10835 | else |
10836 | type = build_pointer_type (type); | |
10837 | ||
10838 | /* Process a list of type modifier keywords (such as | |
10839 | const or volatile) that were given inside the `*' or `&'. */ | |
10840 | ||
10841 | if (TREE_TYPE (declarator)) | |
10842 | { | |
10843 | register tree typemodlist; | |
10844 | int erred = 0; | |
4f2b0fb2 NS |
10845 | int constp = 0; |
10846 | int volatilep = 0; | |
10847 | int restrictp = 0; | |
10848 | ||
8d08fdba MS |
10849 | for (typemodlist = TREE_TYPE (declarator); typemodlist; |
10850 | typemodlist = TREE_CHAIN (typemodlist)) | |
10851 | { | |
91063b51 MM |
10852 | tree qualifier = TREE_VALUE (typemodlist); |
10853 | ||
10854 | if (qualifier == ridpointers[(int) RID_CONST]) | |
4f2b0fb2 NS |
10855 | { |
10856 | constp++; | |
10857 | type_quals |= TYPE_QUAL_CONST; | |
10858 | } | |
91063b51 | 10859 | else if (qualifier == ridpointers[(int) RID_VOLATILE]) |
4f2b0fb2 NS |
10860 | { |
10861 | volatilep++; | |
10862 | type_quals |= TYPE_QUAL_VOLATILE; | |
10863 | } | |
91063b51 | 10864 | else if (qualifier == ridpointers[(int) RID_RESTRICT]) |
4f2b0fb2 NS |
10865 | { |
10866 | restrictp++; | |
10867 | type_quals |= TYPE_QUAL_RESTRICT; | |
10868 | } | |
8d08fdba MS |
10869 | else if (!erred) |
10870 | { | |
10871 | erred = 1; | |
91063b51 | 10872 | error ("invalid type modifier within pointer declarator"); |
8d08fdba MS |
10873 | } |
10874 | } | |
10875 | if (constp > 1) | |
8251199e | 10876 | pedwarn ("duplicate `const'"); |
8d08fdba | 10877 | if (volatilep > 1) |
8251199e | 10878 | pedwarn ("duplicate `volatile'"); |
91063b51 MM |
10879 | if (restrictp > 1) |
10880 | pedwarn ("duplicate `restrict'"); | |
91063b51 | 10881 | type = cp_build_qualified_type (type, type_quals); |
4f2b0fb2 | 10882 | type_quals = cp_type_quals (type); |
8d08fdba MS |
10883 | } |
10884 | declarator = TREE_OPERAND (declarator, 0); | |
10885 | ctype = NULL_TREE; | |
10886 | break; | |
10887 | ||
10888 | case SCOPE_REF: | |
10889 | { | |
10890 | /* We have converted type names to NULL_TREE if the | |
10891 | name was bogus, or to a _TYPE node, if not. | |
10892 | ||
10893 | The variable CTYPE holds the type we will ultimately | |
10894 | resolve to. The code here just needs to build | |
10895 | up appropriate member types. */ | |
10896 | tree sname = TREE_OPERAND (declarator, 1); | |
386b8a85 JM |
10897 | tree t; |
10898 | ||
8d08fdba MS |
10899 | /* Destructors can have their visibilities changed as well. */ |
10900 | if (TREE_CODE (sname) == BIT_NOT_EXPR) | |
10901 | sname = TREE_OPERAND (sname, 0); | |
10902 | ||
8d08fdba MS |
10903 | if (TREE_OPERAND (declarator, 0) == NULL_TREE) |
10904 | { | |
10905 | /* We had a reference to a global decl, or | |
10906 | perhaps we were given a non-aggregate typedef, | |
10907 | in which case we cleared this out, and should just | |
10908 | keep going as though it wasn't there. */ | |
10909 | declarator = sname; | |
10910 | continue; | |
10911 | } | |
10912 | ctype = TREE_OPERAND (declarator, 0); | |
10913 | ||
386b8a85 | 10914 | t = ctype; |
68642fb6 | 10915 | while (t != NULL_TREE && CLASS_TYPE_P (t)) |
386b8a85 | 10916 | { |
5362b086 | 10917 | /* You're supposed to have one `template <...>' |
da8a7456 MM |
10918 | for every template class, but you don't need one |
10919 | for a full specialization. For example: | |
10920 | ||
10921 | template <class T> struct S{}; | |
10922 | template <> struct S<int> { void f(); }; | |
10923 | void S<int>::f () {} | |
10924 | ||
10925 | is correct; there shouldn't be a `template <>' for | |
10926 | the definition of `S<int>::f'. */ | |
10927 | if (CLASSTYPE_TEMPLATE_INFO (t) | |
10928 | && (CLASSTYPE_TEMPLATE_INSTANTIATION (t) | |
92ecdfb7 NS |
10929 | || uses_template_parms (CLASSTYPE_TI_ARGS (t))) |
10930 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t))) | |
386b8a85 | 10931 | template_count += 1; |
da8a7456 | 10932 | |
386b8a85 | 10933 | t = TYPE_MAIN_DECL (t); |
92ecdfb7 | 10934 | t = DECL_CONTEXT (t); |
386b8a85 JM |
10935 | } |
10936 | ||
8d08fdba MS |
10937 | if (sname == NULL_TREE) |
10938 | goto done_scoping; | |
10939 | ||
10940 | if (TREE_CODE (sname) == IDENTIFIER_NODE) | |
10941 | { | |
10942 | /* This is the `standard' use of the scoping operator: | |
10943 | basetype :: member . */ | |
10944 | ||
db5ae43f | 10945 | if (ctype == current_class_type) |
28cbf42c MS |
10946 | { |
10947 | /* class A { | |
10948 | void A::f (); | |
10949 | }; | |
10950 | ||
10951 | Is this ill-formed? */ | |
10952 | ||
10953 | if (pedantic) | |
33bd39a2 | 10954 | pedwarn ("extra qualification `%T::' on member `%s' ignored", |
28cbf42c MS |
10955 | ctype, name); |
10956 | } | |
db5ae43f | 10957 | else if (TREE_CODE (type) == FUNCTION_TYPE) |
8d08fdba | 10958 | { |
eeb75383 | 10959 | if (current_class_type == NULL_TREE || friendp) |
14ae7e7d JM |
10960 | type = build_cplus_method_type (ctype, TREE_TYPE (type), |
10961 | TYPE_ARG_TYPES (type)); | |
8d08fdba MS |
10962 | else |
10963 | { | |
33bd39a2 | 10964 | error ("cannot declare member function `%T::%s' within `%T'", |
a3203465 | 10965 | ctype, name, current_class_type); |
a723baf1 | 10966 | return error_mark_node; |
8d08fdba MS |
10967 | } |
10968 | } | |
5566b478 | 10969 | else if (RIDBIT_SETP (RID_TYPEDEF, specbits) |
d0f062fb | 10970 | || COMPLETE_TYPE_P (complete_type (ctype))) |
8d08fdba | 10971 | { |
2c73f9f5 | 10972 | /* Have to move this code elsewhere in this function. |
db5ae43f MS |
10973 | this code is used for i.e., typedef int A::M; M *pm; |
10974 | ||
10975 | It is? How? jason 10/2/94 */ | |
8d08fdba | 10976 | |
8d08fdba MS |
10977 | if (current_class_type) |
10978 | { | |
33bd39a2 | 10979 | error ("cannot declare member `%T::%s' within `%T'", |
db5ae43f MS |
10980 | ctype, name, current_class_type); |
10981 | return void_type_node; | |
8d08fdba | 10982 | } |
8d08fdba | 10983 | } |
8d08fdba | 10984 | else |
eeb75383 | 10985 | { |
7a228918 | 10986 | cxx_incomplete_type_error (NULL_TREE, ctype); |
eeb75383 | 10987 | return error_mark_node; |
d363e7bf | 10988 | } |
8d08fdba MS |
10989 | |
10990 | declarator = sname; | |
10991 | } | |
8d08fdba | 10992 | else if (TREE_CODE (sname) == SCOPE_REF) |
a98facb0 | 10993 | abort (); |
8d08fdba MS |
10994 | else |
10995 | { | |
10996 | done_scoping: | |
10997 | declarator = TREE_OPERAND (declarator, 1); | |
10998 | if (declarator && TREE_CODE (declarator) == CALL_EXPR) | |
10999 | /* In this case, we will deal with it later. */ | |
11000 | ; | |
9e259dd1 MM |
11001 | else if (TREE_CODE (type) == FUNCTION_TYPE) |
11002 | type = build_cplus_method_type (ctype, TREE_TYPE (type), | |
11003 | TYPE_ARG_TYPES (type)); | |
8d08fdba MS |
11004 | } |
11005 | } | |
11006 | break; | |
11007 | ||
11008 | case BIT_NOT_EXPR: | |
11009 | declarator = TREE_OPERAND (declarator, 0); | |
11010 | break; | |
11011 | ||
50ad9642 MM |
11012 | case BASELINK: |
11013 | declarator = BASELINK_FUNCTIONS (declarator); | |
11014 | break; | |
11015 | ||
8d08fdba MS |
11016 | case RECORD_TYPE: |
11017 | case UNION_TYPE: | |
11018 | case ENUMERAL_TYPE: | |
11019 | declarator = NULL_TREE; | |
11020 | break; | |
11021 | ||
11022 | case ERROR_MARK: | |
11023 | declarator = NULL_TREE; | |
11024 | break; | |
11025 | ||
11026 | default: | |
a98facb0 | 11027 | abort (); |
8d08fdba MS |
11028 | } |
11029 | } | |
11030 | ||
91d231cb | 11031 | if (returned_attrs) |
1eab9b56 | 11032 | { |
91d231cb JM |
11033 | if (attrlist) |
11034 | *attrlist = chainon (returned_attrs, *attrlist); | |
1eab9b56 | 11035 | else |
91d231cb | 11036 | attrlist = &returned_attrs; |
1eab9b56 JM |
11037 | } |
11038 | ||
419c6212 JM |
11039 | /* Now TYPE has the actual type. */ |
11040 | ||
55b3d665 JM |
11041 | /* Did array size calculations overflow? */ |
11042 | ||
11043 | if (TREE_CODE (type) == ARRAY_TYPE | |
11044 | && COMPLETE_TYPE_P (type) | |
11045 | && TREE_OVERFLOW (TYPE_SIZE (type))) | |
11046 | { | |
11047 | error ("size of array `%s' is too large", name); | |
aba649ba | 11048 | /* If we proceed with the array type as it is, we'll eventually |
55b3d665 JM |
11049 | crash in tree_low_cst(). */ |
11050 | type = error_mark_node; | |
11051 | } | |
11052 | ||
2fff6d71 | 11053 | if ((decl_context == FIELD || decl_context == PARM) |
dac45b5c MM |
11054 | && !processing_template_decl |
11055 | && variably_modified_type_p (type)) | |
11056 | { | |
2fff6d71 MM |
11057 | if (decl_context == FIELD) |
11058 | error ("data member may not have variably modified type `%T'", type); | |
11059 | else | |
11060 | error ("parameter may not have variably modified type `%T'", type); | |
dac45b5c MM |
11061 | type = error_mark_node; |
11062 | } | |
11063 | ||
34fc957d | 11064 | if (explicitp == 1 || (explicitp && friendp)) |
db5ae43f | 11065 | { |
34fc957d NS |
11066 | /* [dcl.fct.spec] The explicit specifier shall only be used in |
11067 | declarations of constructors within a class definition. */ | |
11068 | error ("only declarations of constructors can be `explicit'"); | |
db5ae43f MS |
11069 | explicitp = 0; |
11070 | } | |
11071 | ||
f30432d7 MS |
11072 | if (RIDBIT_SETP (RID_MUTABLE, specbits)) |
11073 | { | |
4223f82f | 11074 | if (decl_context != FIELD || friendp) |
34fc957d NS |
11075 | { |
11076 | error ("non-member `%s' cannot be declared `mutable'", name); | |
11077 | RIDBIT_RESET (RID_MUTABLE, specbits); | |
11078 | } | |
11079 | else if (decl_context == TYPENAME || RIDBIT_SETP (RID_TYPEDEF, specbits)) | |
f30432d7 | 11080 | { |
34fc957d | 11081 | error ("non-object member `%s' cannot be declared `mutable'", name); |
f30432d7 MS |
11082 | RIDBIT_RESET (RID_MUTABLE, specbits); |
11083 | } | |
34fc957d NS |
11084 | else if (TREE_CODE (type) == FUNCTION_TYPE |
11085 | || TREE_CODE (type) == METHOD_TYPE) | |
11086 | { | |
11087 | error ("function `%s' cannot be declared `mutable'", name); | |
11088 | RIDBIT_RESET (RID_MUTABLE, specbits); | |
11089 | } | |
f30432d7 MS |
11090 | else if (staticp) |
11091 | { | |
8251199e | 11092 | error ("static `%s' cannot be declared `mutable'", name); |
f30432d7 MS |
11093 | RIDBIT_RESET (RID_MUTABLE, specbits); |
11094 | } | |
34fc957d NS |
11095 | else if (type_quals & TYPE_QUAL_CONST) |
11096 | { | |
11097 | error ("const `%s' cannot be declared `mutable'", name); | |
4f2b0fb2 | 11098 | RIDBIT_RESET (RID_MUTABLE, specbits); |
34fc957d | 11099 | } |
f30432d7 MS |
11100 | } |
11101 | ||
efe38fab JM |
11102 | if (declarator == NULL_TREE |
11103 | || TREE_CODE (declarator) == IDENTIFIER_NODE | |
11104 | || (TREE_CODE (declarator) == TEMPLATE_ID_EXPR | |
11105 | && (TREE_CODE (type) == FUNCTION_TYPE | |
11106 | || TREE_CODE (type) == METHOD_TYPE))) | |
11107 | /* OK */; | |
11108 | else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) | |
11109 | { | |
33bd39a2 | 11110 | error ("template-id `%D' used as a declarator", declarator); |
efe38fab JM |
11111 | declarator = dname; |
11112 | } | |
11113 | else | |
419c6212 | 11114 | /* Unexpected declarator format. */ |
a98facb0 | 11115 | abort (); |
efe38fab | 11116 | |
419c6212 JM |
11117 | /* If this is declaring a typedef name, return a TYPE_DECL. */ |
11118 | ||
fc378698 | 11119 | if (RIDBIT_SETP (RID_TYPEDEF, specbits) && decl_context != TYPENAME) |
8d08fdba MS |
11120 | { |
11121 | tree decl; | |
11122 | ||
11123 | /* Note that the grammar rejects storage classes | |
11124 | in typenames, fields or parameters. */ | |
eff71ab0 PB |
11125 | if (current_lang_name == lang_name_java) |
11126 | TYPE_FOR_JAVA (type) = 1; | |
8d08fdba | 11127 | |
d2e5ee5c MS |
11128 | if (decl_context == FIELD) |
11129 | { | |
a723baf1 | 11130 | if (constructor_name_p (declarator, current_class_type)) |
33bd39a2 | 11131 | pedwarn ("ISO C++ forbids nested type `%D' with same name as enclosing class", |
d2e5ee5c MS |
11132 | declarator); |
11133 | decl = build_lang_decl (TYPE_DECL, declarator, type); | |
d2e5ee5c MS |
11134 | } |
11135 | else | |
6bda7a5e NS |
11136 | { |
11137 | decl = build_decl (TYPE_DECL, declarator, type); | |
a723baf1 MM |
11138 | if (in_namespace || ctype) |
11139 | cp_error_at ("typedef name may not be a nested-name-specifier", | |
11140 | decl); | |
6bda7a5e NS |
11141 | if (!current_function_decl) |
11142 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); | |
11143 | } | |
11144 | ||
9188c363 MM |
11145 | /* If the user declares "typedef struct {...} foo" then the |
11146 | struct will have an anonymous name. Fill that name in now. | |
11147 | Nothing can refer to it, so nothing needs know about the name | |
11148 | change. */ | |
8d08fdba | 11149 | if (type != error_mark_node |
2bdb0643 | 11150 | && declarator |
8d08fdba MS |
11151 | && TYPE_NAME (type) |
11152 | && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL | |
1951a1b6 | 11153 | && TYPE_ANONYMOUS_P (type) |
ac905924 JM |
11154 | /* Don't do this if there are attributes. */ |
11155 | && (!attrlist || !*attrlist) | |
89d684bb | 11156 | && cp_type_quals (type) == TYPE_UNQUALIFIED) |
8d08fdba | 11157 | { |
dcd08efc JM |
11158 | tree oldname = TYPE_NAME (type); |
11159 | tree t; | |
11160 | ||
2c73f9f5 | 11161 | /* Replace the anonymous name with the real name everywhere. */ |
8d08fdba | 11162 | lookup_tag_reverse (type, declarator); |
dcd08efc JM |
11163 | for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) |
11164 | if (TYPE_NAME (t) == oldname) | |
11165 | TYPE_NAME (t) = decl; | |
8d08fdba MS |
11166 | |
11167 | if (TYPE_LANG_SPECIFIC (type)) | |
11168 | TYPE_WAS_ANONYMOUS (type) = 1; | |
11169 | ||
33964bf4 MM |
11170 | /* If this is a typedef within a template class, the nested |
11171 | type is a (non-primary) template. The name for the | |
11172 | template needs updating as well. */ | |
11173 | if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type)) | |
68642fb6 | 11174 | DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)) |
33964bf4 MM |
11175 | = TYPE_IDENTIFIER (type); |
11176 | ||
50a6dbd7 JM |
11177 | /* FIXME remangle member functions; member functions of a |
11178 | type with external linkage have external linkage. */ | |
fc378698 | 11179 | } |
fc378698 | 11180 | |
a723baf1 | 11181 | if (quals) |
8d08fdba MS |
11182 | { |
11183 | if (ctype == NULL_TREE) | |
11184 | { | |
11185 | if (TREE_CODE (type) != METHOD_TYPE) | |
cb9a3ff8 | 11186 | cp_error_at ("invalid type qualifier for non-member function type", decl); |
8d08fdba MS |
11187 | else |
11188 | ctype = TYPE_METHOD_BASETYPE (type); | |
11189 | } | |
11190 | if (ctype != NULL_TREE) | |
11191 | grok_method_quals (ctype, decl, quals); | |
11192 | } | |
11193 | ||
11194 | if (RIDBIT_SETP (RID_SIGNED, specbits) | |
11195 | || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))) | |
11196 | C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1; | |
11197 | ||
c91a56d2 MS |
11198 | bad_specifiers (decl, "type", virtualp, quals != NULL_TREE, |
11199 | inlinep, friendp, raises != NULL_TREE); | |
11200 | ||
8d08fdba MS |
11201 | return decl; |
11202 | } | |
11203 | ||
11204 | /* Detect the case of an array type of unspecified size | |
11205 | which came, as such, direct from a typedef name. | |
8d6e459d NS |
11206 | We must copy the type, so that the array's domain can be |
11207 | individually set by the object's initializer. */ | |
8d08fdba | 11208 | |
8d6e459d NS |
11209 | if (type && typedef_type |
11210 | && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type) | |
55b3d665 | 11211 | && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type)) |
8d6e459d | 11212 | type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE); |
8d08fdba | 11213 | |
969fd501 NS |
11214 | /* Detect where we're using a typedef of function type to declare a |
11215 | function. last_function_parms will not be set, so we must create | |
11216 | it now. */ | |
11217 | ||
11218 | if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE) | |
11219 | { | |
11220 | tree decls = NULL_TREE; | |
11221 | tree args; | |
11222 | ||
11223 | for (args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args)) | |
11224 | { | |
8e51619a | 11225 | tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args)); |
969fd501 NS |
11226 | |
11227 | TREE_CHAIN (decl) = decls; | |
11228 | decls = decl; | |
11229 | } | |
11230 | ||
11231 | last_function_parms = nreverse (decls); | |
11232 | } | |
11233 | ||
8d08fdba MS |
11234 | /* If this is a type name (such as, in a cast or sizeof), |
11235 | compute the type and return it now. */ | |
11236 | ||
11237 | if (decl_context == TYPENAME) | |
11238 | { | |
11239 | /* Note that the grammar rejects storage classes | |
11240 | in typenames, fields or parameters. */ | |
91063b51 | 11241 | if (type_quals != TYPE_UNQUALIFIED) |
6eabb241 | 11242 | type_quals = TYPE_UNQUALIFIED; |
8d08fdba MS |
11243 | |
11244 | /* Special case: "friend class foo" looks like a TYPENAME context. */ | |
11245 | if (friendp) | |
11246 | { | |
91063b51 | 11247 | if (type_quals != TYPE_UNQUALIFIED) |
b7484fbe | 11248 | { |
33bd39a2 | 11249 | error ("type qualifiers specified for friend class declaration"); |
91063b51 | 11250 | type_quals = TYPE_UNQUALIFIED; |
b7484fbe MS |
11251 | } |
11252 | if (inlinep) | |
11253 | { | |
33bd39a2 | 11254 | error ("`inline' specified for friend class declaration"); |
b7484fbe MS |
11255 | inlinep = 0; |
11256 | } | |
f2ae0c45 | 11257 | |
218e0eb6 | 11258 | if (!current_aggr) |
4b054b80 | 11259 | { |
218e0eb6 | 11260 | /* Don't allow friend declaration without a class-key. */ |
4b054b80 | 11261 | if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) |
218e0eb6 | 11262 | pedwarn ("template parameters cannot be friends"); |
f2ae0c45 | 11263 | else if (TREE_CODE (type) == TYPENAME_TYPE) |
218e0eb6 KL |
11264 | pedwarn ("friend declaration requires class-key, " |
11265 | "i.e. `friend class %T::%D'", | |
11266 | TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type)); | |
4b054b80 | 11267 | else |
218e0eb6 KL |
11268 | pedwarn ("friend declaration requires class-key, " |
11269 | "i.e. `friend %#T'", | |
11270 | type); | |
4b054b80 | 11271 | } |
b7484fbe MS |
11272 | |
11273 | /* Only try to do this stuff if we didn't already give up. */ | |
11274 | if (type != integer_type_node) | |
11275 | { | |
11276 | /* A friendly class? */ | |
11277 | if (current_class_type) | |
11278 | make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type)); | |
11279 | else | |
33bd39a2 | 11280 | error ("trying to make class `%T' a friend of global scope", |
2ae7bada | 11281 | type); |
d363e7bf | 11282 | |
b7484fbe MS |
11283 | type = void_type_node; |
11284 | } | |
8d08fdba MS |
11285 | } |
11286 | else if (quals) | |
11287 | { | |
8d08fdba MS |
11288 | if (ctype == NULL_TREE) |
11289 | { | |
41cbc04c | 11290 | if (TREE_CODE (type) != METHOD_TYPE) |
33bd39a2 | 11291 | error ("invalid qualifiers on non-member function type"); |
41cbc04c NS |
11292 | else |
11293 | ctype = TYPE_METHOD_BASETYPE (type); | |
11294 | } | |
11295 | if (ctype) | |
11296 | { | |
11297 | tree dummy = build_decl (TYPE_DECL, declarator, type); | |
11298 | grok_method_quals (ctype, dummy, quals); | |
11299 | type = TREE_TYPE (dummy); | |
8d08fdba | 11300 | } |
8d08fdba MS |
11301 | } |
11302 | ||
11303 | return type; | |
11304 | } | |
11305 | else if (declarator == NULL_TREE && decl_context != PARM | |
db5ae43f | 11306 | && decl_context != CATCHPARM |
8d08fdba MS |
11307 | && TREE_CODE (type) != UNION_TYPE |
11308 | && ! bitfield) | |
11309 | { | |
33bd39a2 | 11310 | error ("abstract declarator `%T' used as declaration", type); |
8d08fdba MS |
11311 | declarator = make_anon_name (); |
11312 | } | |
11313 | ||
11314 | /* `void' at top level (not within pointer) | |
11315 | is allowed only in typedefs or type names. | |
11316 | We don't complain about parms either, but that is because | |
11317 | a better error message can be made later. */ | |
11318 | ||
a1774733 | 11319 | if (TREE_CODE (type) == VOID_TYPE && decl_context != PARM) |
8d08fdba | 11320 | { |
b7484fbe | 11321 | if (! declarator) |
8251199e | 11322 | error ("unnamed variable or field declared void"); |
b7484fbe | 11323 | else if (TREE_CODE (declarator) == IDENTIFIER_NODE) |
8d08fdba MS |
11324 | { |
11325 | if (IDENTIFIER_OPNAME_P (declarator)) | |
a98facb0 | 11326 | abort (); |
8d08fdba | 11327 | else |
8251199e | 11328 | error ("variable or field `%s' declared void", name); |
8d08fdba MS |
11329 | } |
11330 | else | |
8251199e | 11331 | error ("variable or field declared void"); |
8d08fdba MS |
11332 | type = integer_type_node; |
11333 | } | |
11334 | ||
11335 | /* Now create the decl, which may be a VAR_DECL, a PARM_DECL | |
11336 | or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */ | |
11337 | ||
14ae7e7d JM |
11338 | if (decl_context == PARM || decl_context == CATCHPARM) |
11339 | { | |
11340 | if (ctype || in_namespace) | |
11341 | error ("cannot use `::' in parameter declaration"); | |
11342 | ||
11343 | /* A parameter declared as an array of T is really a pointer to T. | |
11344 | One declared as a function is really a pointer to a function. | |
11345 | One declared as a member is really a pointer to member. */ | |
11346 | ||
11347 | if (TREE_CODE (type) == ARRAY_TYPE) | |
11348 | { | |
11349 | /* Transfer const-ness of array into that of type pointed to. */ | |
11350 | type = build_pointer_type (TREE_TYPE (type)); | |
91063b51 | 11351 | type_quals = TYPE_UNQUALIFIED; |
14ae7e7d JM |
11352 | } |
11353 | else if (TREE_CODE (type) == FUNCTION_TYPE) | |
11354 | type = build_pointer_type (type); | |
11355 | else if (TREE_CODE (type) == OFFSET_TYPE) | |
11356 | type = build_pointer_type (type); | |
14ae7e7d | 11357 | } |
68642fb6 | 11358 | |
8d08fdba MS |
11359 | { |
11360 | register tree decl; | |
11361 | ||
11362 | if (decl_context == PARM) | |
11363 | { | |
8e51619a | 11364 | decl = cp_build_parm_decl (declarator, type); |
8d08fdba MS |
11365 | |
11366 | bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE, | |
11367 | inlinep, friendp, raises != NULL_TREE); | |
8d08fdba MS |
11368 | } |
11369 | else if (decl_context == FIELD) | |
11370 | { | |
11371 | if (type == error_mark_node) | |
11372 | { | |
11373 | /* Happens when declaring arrays of sizes which | |
11374 | are error_mark_node, for example. */ | |
11375 | decl = NULL_TREE; | |
11376 | } | |
997a088c | 11377 | else if (in_namespace && !friendp) |
05008fb9 MM |
11378 | { |
11379 | /* Something like struct S { int N::j; }; */ | |
33bd39a2 | 11380 | error ("invalid use of `::'"); |
05008fb9 MM |
11381 | decl = NULL_TREE; |
11382 | } | |
8d08fdba MS |
11383 | else if (TREE_CODE (type) == FUNCTION_TYPE) |
11384 | { | |
11385 | int publicp = 0; | |
e1467ff2 | 11386 | tree function_context; |
8d08fdba | 11387 | |
72b7eeff MS |
11388 | /* We catch the others as conflicts with the builtin |
11389 | typedefs. */ | |
11390 | if (friendp && declarator == ridpointers[(int) RID_SIGNED]) | |
11391 | { | |
33bd39a2 | 11392 | error ("function `%D' cannot be declared friend", |
72b7eeff MS |
11393 | declarator); |
11394 | friendp = 0; | |
11395 | } | |
11396 | ||
8d08fdba MS |
11397 | if (friendp == 0) |
11398 | { | |
11399 | if (ctype == NULL_TREE) | |
11400 | ctype = current_class_type; | |
11401 | ||
11402 | if (ctype == NULL_TREE) | |
11403 | { | |
33bd39a2 | 11404 | error ("can't make `%D' into a method -- not in a class", |
8d08fdba MS |
11405 | declarator); |
11406 | return void_type_node; | |
11407 | } | |
11408 | ||
11409 | /* ``A union may [ ... ] not [ have ] virtual functions.'' | |
11410 | ARM 9.5 */ | |
11411 | if (virtualp && TREE_CODE (ctype) == UNION_TYPE) | |
11412 | { | |
33bd39a2 | 11413 | error ("function `%D' declared virtual inside a union", |
8d08fdba MS |
11414 | declarator); |
11415 | return void_type_node; | |
11416 | } | |
11417 | ||
596ea4e5 AS |
11418 | if (declarator == ansi_opname (NEW_EXPR) |
11419 | || declarator == ansi_opname (VEC_NEW_EXPR) | |
11420 | || declarator == ansi_opname (DELETE_EXPR) | |
11421 | || declarator == ansi_opname (VEC_DELETE_EXPR)) | |
8d08fdba MS |
11422 | { |
11423 | if (virtualp) | |
11424 | { | |
33bd39a2 | 11425 | error ("`%D' cannot be declared virtual, since it is always static", |
8d08fdba MS |
11426 | declarator); |
11427 | virtualp = 0; | |
11428 | } | |
11429 | } | |
11430 | else if (staticp < 2) | |
14ae7e7d JM |
11431 | type = build_cplus_method_type (ctype, TREE_TYPE (type), |
11432 | TYPE_ARG_TYPES (type)); | |
8d08fdba MS |
11433 | } |
11434 | ||
11435 | /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */ | |
68642fb6 | 11436 | function_context = (ctype != NULL_TREE) ? |
4f1c5b7d | 11437 | decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE; |
e1467ff2 MM |
11438 | publicp = (! friendp || ! staticp) |
11439 | && function_context == NULL_TREE; | |
68642fb6 | 11440 | decl = grokfndecl (ctype, type, |
386b8a85 JM |
11441 | TREE_CODE (declarator) != TEMPLATE_ID_EXPR |
11442 | ? declarator : dname, | |
11443 | declarator, | |
7a8f9fa9 | 11444 | virtualp, flags, quals, raises, |
386b8a85 | 11445 | friendp ? -1 : 0, friendp, publicp, inlinep, |
2c73f9f5 | 11446 | funcdef_flag, template_count, in_namespace); |
20496fa2 | 11447 | if (decl == NULL_TREE) |
3ddfb0e6 | 11448 | return decl; |
6125f3be DE |
11449 | #if 0 |
11450 | /* This clobbers the attrs stored in `decl' from `attrlist'. */ | |
91d231cb JM |
11451 | /* The decl and setting of decl_attr is also turned off. */ |
11452 | decl = build_decl_attribute_variant (decl, decl_attr); | |
6125f3be | 11453 | #endif |
f0e01782 | 11454 | |
cc804e51 MM |
11455 | /* [class.conv.ctor] |
11456 | ||
11457 | A constructor declared without the function-specifier | |
11458 | explicit that can be called with a single parameter | |
11459 | specifies a conversion from the type of its first | |
11460 | parameter to the type of its class. Such a constructor | |
11461 | is called a converting constructor. */ | |
db5ae43f MS |
11462 | if (explicitp == 2) |
11463 | DECL_NONCONVERTING_P (decl) = 1; | |
cc804e51 MM |
11464 | else if (DECL_CONSTRUCTOR_P (decl)) |
11465 | { | |
11466 | /* The constructor can be called with exactly one | |
11467 | parameter if there is at least one parameter, and | |
11468 | any subsequent parameters have default arguments. | |
e0fff4b3 JM |
11469 | Ignore any compiler-added parms. */ |
11470 | tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (decl); | |
cc804e51 MM |
11471 | |
11472 | if (arg_types == void_list_node | |
68642fb6 UD |
11473 | || (arg_types |
11474 | && TREE_CHAIN (arg_types) | |
cc804e51 MM |
11475 | && TREE_CHAIN (arg_types) != void_list_node |
11476 | && !TREE_PURPOSE (TREE_CHAIN (arg_types)))) | |
11477 | DECL_NONCONVERTING_P (decl) = 1; | |
11478 | } | |
8d08fdba MS |
11479 | } |
11480 | else if (TREE_CODE (type) == METHOD_TYPE) | |
11481 | { | |
faae18ab MS |
11482 | /* We only get here for friend declarations of |
11483 | members of other classes. */ | |
8d08fdba MS |
11484 | /* All method decls are public, so tell grokfndecl to set |
11485 | TREE_PUBLIC, also. */ | |
386b8a85 | 11486 | decl = grokfndecl (ctype, type, declarator, declarator, |
7a8f9fa9 | 11487 | virtualp, flags, quals, raises, |
386b8a85 | 11488 | friendp ? -1 : 0, friendp, 1, 0, funcdef_flag, |
2c73f9f5 | 11489 | template_count, in_namespace); |
f0e01782 MS |
11490 | if (decl == NULL_TREE) |
11491 | return NULL_TREE; | |
8d08fdba | 11492 | } |
5566b478 | 11493 | else if (!staticp && ! processing_template_decl |
d0f062fb | 11494 | && !COMPLETE_TYPE_P (complete_type (type)) |
8d08fdba MS |
11495 | && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0)) |
11496 | { | |
b7484fbe | 11497 | if (declarator) |
33bd39a2 | 11498 | error ("field `%D' has incomplete type", declarator); |
b7484fbe | 11499 | else |
33bd39a2 | 11500 | error ("name `%T' has incomplete type", type); |
8d08fdba MS |
11501 | |
11502 | /* If we're instantiating a template, tell them which | |
11503 | instantiation made the field's type be incomplete. */ | |
11504 | if (current_class_type | |
11505 | && TYPE_NAME (current_class_type) | |
d2e5ee5c | 11506 | && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type)) |
8d08fdba MS |
11507 | && declspecs && TREE_VALUE (declspecs) |
11508 | && TREE_TYPE (TREE_VALUE (declspecs)) == type) | |
33bd39a2 | 11509 | error (" in instantiation of template `%T'", |
db5ae43f MS |
11510 | current_class_type); |
11511 | ||
8d08fdba MS |
11512 | type = error_mark_node; |
11513 | decl = NULL_TREE; | |
11514 | } | |
11515 | else | |
11516 | { | |
11517 | if (friendp) | |
11518 | { | |
cb9a3ff8 | 11519 | error ("`%s' is neither function nor member function; cannot be declared friend", |
8d08fdba MS |
11520 | IDENTIFIER_POINTER (declarator)); |
11521 | friendp = 0; | |
11522 | } | |
11523 | decl = NULL_TREE; | |
11524 | } | |
11525 | ||
11526 | if (friendp) | |
11527 | { | |
11528 | /* Friends are treated specially. */ | |
11529 | if (ctype == current_class_type) | |
8251199e | 11530 | warning ("member functions are implicitly friends of their class"); |
ab04e34b NS |
11531 | else |
11532 | { | |
11533 | tree t = NULL_TREE; | |
11534 | if (decl && DECL_NAME (decl)) | |
11535 | { | |
11536 | if (template_class_depth (current_class_type) == 0) | |
11537 | { | |
11538 | decl | |
11539 | = check_explicit_specialization | |
11540 | (declarator, decl, | |
11541 | template_count, 2 * (funcdef_flag != 0) + 4); | |
11542 | if (decl == error_mark_node) | |
11543 | return error_mark_node; | |
11544 | } | |
11545 | ||
11546 | t = do_friend (ctype, declarator, decl, | |
11547 | last_function_parms, *attrlist, | |
11548 | flags, quals, funcdef_flag); | |
11549 | } | |
11550 | if (t && funcdef_flag) | |
11551 | return t; | |
11552 | ||
11553 | return void_type_node; | |
11554 | } | |
8d08fdba MS |
11555 | } |
11556 | ||
11557 | /* Structure field. It may not be a function, except for C++ */ | |
11558 | ||
11559 | if (decl == NULL_TREE) | |
11560 | { | |
8d08fdba MS |
11561 | if (initialized) |
11562 | { | |
3ac3d9ea MM |
11563 | if (!staticp) |
11564 | { | |
11565 | /* An attempt is being made to initialize a non-static | |
11566 | member. But, from [class.mem]: | |
68642fb6 | 11567 | |
3ac3d9ea MM |
11568 | 4 A member-declarator can contain a |
11569 | constant-initializer only if it declares a static | |
11570 | member (_class.static_) of integral or enumeration | |
68642fb6 | 11571 | type, see _class.static.data_. |
3ac3d9ea MM |
11572 | |
11573 | This used to be relatively common practice, but | |
11574 | the rest of the compiler does not correctly | |
11575 | handle the initialization unless the member is | |
11576 | static so we make it static below. */ | |
33bd39a2 | 11577 | pedwarn ("ISO C++ forbids initialization of member `%D'", |
3ac3d9ea | 11578 | declarator); |
33bd39a2 | 11579 | pedwarn ("making `%D' static", declarator); |
3ac3d9ea MM |
11580 | staticp = 1; |
11581 | } | |
11582 | ||
6ba89f8e MM |
11583 | if (uses_template_parms (type)) |
11584 | /* We'll check at instantiation time. */ | |
11585 | ; | |
11586 | else if (check_static_variable_definition (declarator, | |
11587 | type)) | |
11588 | /* If we just return the declaration, crashes | |
11589 | will sometimes occur. We therefore return | |
72a93143 JM |
11590 | void_type_node, as if this was a friend |
11591 | declaration, to cause callers to completely | |
11592 | ignore this declaration. */ | |
6ba89f8e | 11593 | return void_type_node; |
8d08fdba MS |
11594 | } |
11595 | ||
3ac3d9ea | 11596 | if (staticp) |
8d08fdba | 11597 | { |
c717c5af MM |
11598 | /* [class.mem] forbids static data members with the |
11599 | same name as the enclosing class. Non-static data | |
11600 | members are checked in check_field_decls. */ | |
11601 | if (constructor_name_p (declarator, current_class_type)) | |
11602 | pedwarn ("ISO C++ forbids static data member `%D' with same name as enclosing class", | |
11603 | declarator); | |
11604 | ||
f18a14bc MM |
11605 | /* C++ allows static class members. All other work |
11606 | for this is done by grokfield. */ | |
4ce3d537 | 11607 | decl = build_lang_decl (VAR_DECL, declarator, type); |
5b605f68 MS |
11608 | TREE_STATIC (decl) = 1; |
11609 | /* In class context, 'static' means public access. */ | |
3ac3d9ea | 11610 | TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = 1; |
8d08fdba MS |
11611 | } |
11612 | else | |
11613 | { | |
721c3b42 | 11614 | decl = build_decl (FIELD_DECL, declarator, type); |
2bf105ab | 11615 | DECL_NONADDRESSABLE_P (decl) = bitfield; |
8d08fdba MS |
11616 | if (RIDBIT_SETP (RID_MUTABLE, specbits)) |
11617 | { | |
11618 | DECL_MUTABLE_P (decl) = 1; | |
11619 | RIDBIT_RESET (RID_MUTABLE, specbits); | |
11620 | } | |
11621 | } | |
11622 | ||
11623 | bad_specifiers (decl, "field", virtualp, quals != NULL_TREE, | |
11624 | inlinep, friendp, raises != NULL_TREE); | |
11625 | } | |
11626 | } | |
11627 | else if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE) | |
11628 | { | |
386b8a85 | 11629 | tree original_name; |
8d08fdba MS |
11630 | int publicp = 0; |
11631 | ||
11632 | if (! declarator) | |
11633 | return NULL_TREE; | |
11634 | ||
386b8a85 JM |
11635 | if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) |
11636 | original_name = dname; | |
11637 | else | |
11638 | original_name = declarator; | |
11639 | ||
8926095f | 11640 | if (RIDBIT_SETP (RID_AUTO, specbits)) |
8251199e | 11641 | error ("storage class `auto' invalid for function `%s'", name); |
8926095f | 11642 | else if (RIDBIT_SETP (RID_REGISTER, specbits)) |
8251199e | 11643 | error ("storage class `register' invalid for function `%s'", name); |
7a1f3f5f RH |
11644 | else if (RIDBIT_SETP (RID_THREAD, specbits)) |
11645 | error ("storage class `__thread' invalid for function `%s'", name); | |
8d08fdba MS |
11646 | |
11647 | /* Function declaration not at top level. | |
11648 | Storage classes other than `extern' are not allowed | |
11649 | and `extern' makes no difference. */ | |
a9aedbc2 | 11650 | if (! toplevel_bindings_p () |
8926095f MS |
11651 | && (RIDBIT_SETP (RID_STATIC, specbits) |
11652 | || RIDBIT_SETP (RID_INLINE, specbits)) | |
8d08fdba | 11653 | && pedantic) |
8926095f MS |
11654 | { |
11655 | if (RIDBIT_SETP (RID_STATIC, specbits)) | |
8251199e | 11656 | pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name); |
8926095f | 11657 | else |
8251199e | 11658 | pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name); |
8926095f | 11659 | } |
68642fb6 | 11660 | |
8d08fdba MS |
11661 | if (ctype == NULL_TREE) |
11662 | { | |
11663 | if (virtualp) | |
11664 | { | |
8251199e | 11665 | error ("virtual non-class function `%s'", name); |
8d08fdba MS |
11666 | virtualp = 0; |
11667 | } | |
8d08fdba MS |
11668 | } |
11669 | else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2) | |
14ae7e7d JM |
11670 | type = build_cplus_method_type (ctype, TREE_TYPE (type), |
11671 | TYPE_ARG_TYPES (type)); | |
8d08fdba | 11672 | |
eb66be0e | 11673 | /* Record presence of `static'. */ |
faae18ab | 11674 | publicp = (ctype != NULL_TREE |
a9aedbc2 | 11675 | || RIDBIT_SETP (RID_EXTERN, specbits) |
eb66be0e | 11676 | || !RIDBIT_SETP (RID_STATIC, specbits)); |
8d08fdba | 11677 | |
386b8a85 | 11678 | decl = grokfndecl (ctype, type, original_name, declarator, |
7a8f9fa9 | 11679 | virtualp, flags, quals, raises, |
75650646 | 11680 | 1, friendp, |
68642fb6 | 11681 | publicp, inlinep, funcdef_flag, |
2c73f9f5 | 11682 | template_count, in_namespace); |
f0e01782 MS |
11683 | if (decl == NULL_TREE) |
11684 | return NULL_TREE; | |
8d08fdba | 11685 | |
8d08fdba MS |
11686 | if (staticp == 1) |
11687 | { | |
0e339752 | 11688 | int invalid_static = 0; |
8d08fdba MS |
11689 | |
11690 | /* Don't allow a static member function in a class, and forbid | |
11691 | declaring main to be static. */ | |
11692 | if (TREE_CODE (type) == METHOD_TYPE) | |
11693 | { | |
33bd39a2 | 11694 | pedwarn ("cannot declare member function `%D' to have static linkage", decl); |
0e339752 | 11695 | invalid_static = 1; |
8d08fdba | 11696 | } |
8d08fdba MS |
11697 | else if (current_function_decl) |
11698 | { | |
11699 | /* FIXME need arm citation */ | |
8251199e | 11700 | error ("cannot declare static function inside another function"); |
0e339752 | 11701 | invalid_static = 1; |
8d08fdba MS |
11702 | } |
11703 | ||
0e339752 | 11704 | if (invalid_static) |
8d08fdba MS |
11705 | { |
11706 | staticp = 0; | |
11707 | RIDBIT_RESET (RID_STATIC, specbits); | |
11708 | } | |
11709 | } | |
8d08fdba MS |
11710 | } |
11711 | else | |
11712 | { | |
11713 | /* It's a variable. */ | |
11714 | ||
11715 | /* An uninitialized decl with `extern' is a reference. */ | |
68642fb6 UD |
11716 | decl = grokvardecl (type, declarator, &specbits, |
11717 | initialized, | |
11718 | (type_quals & TYPE_QUAL_CONST) != 0, | |
9e259dd1 | 11719 | ctype ? ctype : in_namespace); |
8d08fdba MS |
11720 | bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE, |
11721 | inlinep, friendp, raises != NULL_TREE); | |
11722 | ||
11723 | if (ctype) | |
11724 | { | |
f0e01782 | 11725 | DECL_CONTEXT (decl) = ctype; |
8d08fdba MS |
11726 | if (staticp == 1) |
11727 | { | |
33bd39a2 | 11728 | pedwarn ("`static' may not be used when defining (as opposed to declaring) a static data member"); |
8d08fdba MS |
11729 | staticp = 0; |
11730 | RIDBIT_RESET (RID_STATIC, specbits); | |
11731 | } | |
b7484fbe MS |
11732 | if (RIDBIT_SETP (RID_REGISTER, specbits) && TREE_STATIC (decl)) |
11733 | { | |
33bd39a2 | 11734 | error ("static member `%D' declared `register'", decl); |
b7484fbe MS |
11735 | RIDBIT_RESET (RID_REGISTER, specbits); |
11736 | } | |
f30432d7 | 11737 | if (RIDBIT_SETP (RID_EXTERN, specbits) && pedantic) |
8d08fdba | 11738 | { |
33bd39a2 | 11739 | pedwarn ("cannot explicitly declare member `%#D' to have extern linkage", |
f30432d7 | 11740 | decl); |
8d08fdba MS |
11741 | RIDBIT_RESET (RID_EXTERN, specbits); |
11742 | } | |
11743 | } | |
11744 | } | |
11745 | ||
34fc957d | 11746 | my_friendly_assert (!RIDBIT_SETP (RID_MUTABLE, specbits), 19990927); |
8d08fdba MS |
11747 | |
11748 | /* Record `register' declaration for warnings on & | |
11749 | and in case doing stupid register allocation. */ | |
11750 | ||
11751 | if (RIDBIT_SETP (RID_REGISTER, specbits)) | |
11752 | DECL_REGISTER (decl) = 1; | |
11753 | ||
8926095f MS |
11754 | if (RIDBIT_SETP (RID_EXTERN, specbits)) |
11755 | DECL_THIS_EXTERN (decl) = 1; | |
11756 | ||
faae18ab MS |
11757 | if (RIDBIT_SETP (RID_STATIC, specbits)) |
11758 | DECL_THIS_STATIC (decl) = 1; | |
11759 | ||
adecb3f4 MM |
11760 | /* Record constancy and volatility. There's no need to do this |
11761 | when processing a template; we'll do this for the instantiated | |
11762 | declaration based on the type of DECL. */ | |
11763 | if (!processing_template_decl) | |
11764 | c_apply_type_quals_to_decl (type_quals, decl); | |
8d08fdba MS |
11765 | |
11766 | return decl; | |
11767 | } | |
11768 | } | |
11769 | \f | |
f181d4ae MM |
11770 | /* Subroutine of start_function. Ensure that each of the parameter |
11771 | types (as listed in PARMS) is complete, as is required for a | |
11772 | function definition. */ | |
e92cc029 | 11773 | |
8d08fdba | 11774 | static void |
11f6b451 | 11775 | require_complete_types_for_parms (tree parms) |
8d08fdba | 11776 | { |
07c88314 | 11777 | for (; parms; parms = TREE_CHAIN (parms)) |
8d08fdba | 11778 | { |
5cce22b6 NS |
11779 | if (VOID_TYPE_P (TREE_TYPE (parms))) |
11780 | /* grokparms will have already issued an error */ | |
11781 | TREE_TYPE (parms) = error_mark_node; | |
11782 | else if (complete_type_or_else (TREE_TYPE (parms), parms)) | |
753225c1 JM |
11783 | { |
11784 | layout_decl (parms, 0); | |
11785 | DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms)); | |
11786 | } | |
5cce22b6 NS |
11787 | else |
11788 | TREE_TYPE (parms) = error_mark_node; | |
8d08fdba MS |
11789 | } |
11790 | } | |
11791 | ||
838dfd8a | 11792 | /* Returns nonzero if T is a local variable. */ |
297e73d8 | 11793 | |
46e8c075 | 11794 | int |
11f6b451 | 11795 | local_variable_p (tree t) |
297e73d8 | 11796 | { |
68642fb6 | 11797 | if ((TREE_CODE (t) == VAR_DECL |
297e73d8 MM |
11798 | /* A VAR_DECL with a context that is a _TYPE is a static data |
11799 | member. */ | |
11800 | && !TYPE_P (CP_DECL_CONTEXT (t)) | |
11801 | /* Any other non-local variable must be at namespace scope. */ | |
46e8c075 | 11802 | && !DECL_NAMESPACE_SCOPE_P (t)) |
297e73d8 | 11803 | || (TREE_CODE (t) == PARM_DECL)) |
46e8c075 | 11804 | return 1; |
297e73d8 | 11805 | |
46e8c075 MM |
11806 | return 0; |
11807 | } | |
11808 | ||
838dfd8a | 11809 | /* Returns nonzero if T is an automatic local variable or a label. |
46e8c075 MM |
11810 | (These are the declarations that need to be remapped when the code |
11811 | containing them is duplicated.) */ | |
11812 | ||
11813 | int | |
11f6b451 | 11814 | nonstatic_local_decl_p (tree t) |
46e8c075 MM |
11815 | { |
11816 | return ((local_variable_p (t) && !TREE_STATIC (t)) | |
11817 | || TREE_CODE (t) == LABEL_DECL | |
11818 | || TREE_CODE (t) == RESULT_DECL); | |
11819 | } | |
11820 | ||
11821 | /* Like local_variable_p, but suitable for use as a tree-walking | |
11822 | function. */ | |
11823 | ||
11824 | static tree | |
11f6b451 NN |
11825 | local_variable_p_walkfn (tree* tp, |
11826 | int* walk_subtrees ATTRIBUTE_UNUSED , | |
11827 | void* data ATTRIBUTE_UNUSED ) | |
46e8c075 | 11828 | { |
68642fb6 | 11829 | return ((local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp)) |
46e8c075 | 11830 | ? *tp : NULL_TREE); |
297e73d8 MM |
11831 | } |
11832 | ||
11833 | /* Check that ARG, which is a default-argument expression for a | |
0e339752 | 11834 | parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if |
297e73d8 MM |
11835 | something goes wrong. DECL may also be a _TYPE node, rather than a |
11836 | DECL, if there is no DECL available. */ | |
11837 | ||
11838 | tree | |
11f6b451 | 11839 | check_default_argument (tree decl, tree arg) |
297e73d8 MM |
11840 | { |
11841 | tree var; | |
11842 | tree decl_type; | |
11843 | ||
11844 | if (TREE_CODE (arg) == DEFAULT_ARG) | |
11845 | /* We get a DEFAULT_ARG when looking at an in-class declaration | |
11846 | with a default argument. Ignore the argument for now; we'll | |
11847 | deal with it after the class is complete. */ | |
11848 | return arg; | |
11849 | ||
11850 | if (processing_template_decl || uses_template_parms (arg)) | |
11851 | /* We don't do anything checking until instantiation-time. Note | |
11852 | that there may be uninstantiated arguments even for an | |
11853 | instantiated function, since default arguments are not | |
11854 | instantiated until they are needed. */ | |
11855 | return arg; | |
11856 | ||
11857 | if (TYPE_P (decl)) | |
11858 | { | |
11859 | decl_type = decl; | |
11860 | decl = NULL_TREE; | |
11861 | } | |
11862 | else | |
11863 | decl_type = TREE_TYPE (decl); | |
11864 | ||
68642fb6 | 11865 | if (arg == error_mark_node |
297e73d8 MM |
11866 | || decl == error_mark_node |
11867 | || TREE_TYPE (arg) == error_mark_node | |
11868 | || decl_type == error_mark_node) | |
11869 | /* Something already went wrong. There's no need to check | |
11870 | further. */ | |
11871 | return error_mark_node; | |
11872 | ||
11873 | /* [dcl.fct.default] | |
68642fb6 | 11874 | |
297e73d8 MM |
11875 | A default argument expression is implicitly converted to the |
11876 | parameter type. */ | |
11877 | if (!TREE_TYPE (arg) | |
11878 | || !can_convert_arg (decl_type, TREE_TYPE (arg), arg)) | |
11879 | { | |
11880 | if (decl) | |
33bd39a2 | 11881 | error ("default argument for `%#D' has type `%T'", |
297e73d8 MM |
11882 | decl, TREE_TYPE (arg)); |
11883 | else | |
33bd39a2 | 11884 | error ("default argument for parameter of type `%T' has type `%T'", |
297e73d8 MM |
11885 | decl_type, TREE_TYPE (arg)); |
11886 | ||
11887 | return error_mark_node; | |
11888 | } | |
11889 | ||
11890 | /* [dcl.fct.default] | |
11891 | ||
11892 | Local variables shall not be used in default argument | |
68642fb6 | 11893 | expressions. |
297e73d8 MM |
11894 | |
11895 | The keyword `this' shall not be used in a default argument of a | |
11896 | member function. */ | |
5362b086 | 11897 | var = walk_tree_without_duplicates (&arg, local_variable_p_walkfn, |
ee94fce6 | 11898 | NULL); |
297e73d8 MM |
11899 | if (var) |
11900 | { | |
33bd39a2 | 11901 | error ("default argument `%E' uses local variable `%D'", |
297e73d8 MM |
11902 | arg, var); |
11903 | return error_mark_node; | |
11904 | } | |
11905 | ||
11906 | /* All is well. */ | |
11907 | return arg; | |
11908 | } | |
11909 | ||
8d08fdba MS |
11910 | /* Decode the list of parameter types for a function type. |
11911 | Given the list of things declared inside the parens, | |
11912 | return a list of types. | |
11913 | ||
5cce22b6 NS |
11914 | We determine whether ellipsis parms are used by PARMLIST_ELLIPSIS_P |
11915 | flag. If unset, we append void_list_node. A parmlist declared | |
11916 | as `(void)' is accepted as the empty parmlist. | |
11917 | ||
11918 | Also set last_function_parms to the chain of PARM_DECLs. */ | |
8d08fdba MS |
11919 | |
11920 | static tree | |
11f6b451 | 11921 | grokparms (tree first_parm) |
8d08fdba MS |
11922 | { |
11923 | tree result = NULL_TREE; | |
11924 | tree decls = NULL_TREE; | |
5cce22b6 NS |
11925 | int ellipsis = !first_parm || PARMLIST_ELLIPSIS_P (first_parm); |
11926 | tree parm, chain; | |
11927 | int any_error = 0; | |
8d08fdba | 11928 | |
5cce22b6 NS |
11929 | my_friendly_assert (!first_parm || TREE_PARMLIST (first_parm), 20001115); |
11930 | ||
11931 | for (parm = first_parm; parm != NULL_TREE; parm = chain) | |
8d08fdba | 11932 | { |
0657c69c | 11933 | tree type = NULL_TREE; |
98884b26 | 11934 | tree decl = TREE_VALUE (parm); |
5cce22b6 | 11935 | tree init = TREE_PURPOSE (parm); |
98884b26 | 11936 | tree specs, attrs; |
8d08fdba | 11937 | |
5cce22b6 NS |
11938 | chain = TREE_CHAIN (parm); |
11939 | /* @@ weak defense against parse errors. */ | |
11940 | if (TREE_CODE (decl) != VOID_TYPE | |
11941 | && TREE_CODE (decl) != TREE_LIST) | |
8d08fdba | 11942 | { |
5cce22b6 NS |
11943 | /* Give various messages as the need arises. */ |
11944 | if (TREE_CODE (decl) == STRING_CST) | |
33bd39a2 | 11945 | error ("invalid string constant `%E'", decl); |
5cce22b6 NS |
11946 | else if (TREE_CODE (decl) == INTEGER_CST) |
11947 | error ("invalid integer constant in parameter list, did you forget to give parameter name?"); | |
11948 | continue; | |
11949 | } | |
8d08fdba | 11950 | |
5cce22b6 NS |
11951 | if (parm == void_list_node) |
11952 | break; | |
8d08fdba | 11953 | |
98884b26 JM |
11954 | split_specs_attrs (TREE_PURPOSE (decl), &specs, &attrs); |
11955 | decl = grokdeclarator (TREE_VALUE (decl), specs, | |
11956 | PARM, init != NULL_TREE, &attrs); | |
5cce22b6 NS |
11957 | if (! decl || TREE_TYPE (decl) == error_mark_node) |
11958 | continue; | |
d363e7bf | 11959 | |
98884b26 JM |
11960 | if (attrs) |
11961 | cplus_decl_attributes (&decl, attrs, 0); | |
11962 | ||
5cce22b6 NS |
11963 | type = TREE_TYPE (decl); |
11964 | if (VOID_TYPE_P (type)) | |
11965 | { | |
11966 | if (same_type_p (type, void_type_node) | |
11967 | && !DECL_NAME (decl) && !result && !chain && !ellipsis) | |
11968 | /* this is a parmlist of `(void)', which is ok. */ | |
11969 | break; | |
7a228918 | 11970 | cxx_incomplete_type_error (decl, type); |
0657c69c MM |
11971 | /* It's not a good idea to actually create parameters of |
11972 | type `void'; other parts of the compiler assume that a | |
11973 | void type terminates the parameter list. */ | |
04f3dc2b | 11974 | type = error_mark_node; |
0657c69c | 11975 | TREE_TYPE (decl) = error_mark_node; |
5cce22b6 | 11976 | } |
8d08fdba | 11977 | |
d363e7bf | 11978 | if (type != error_mark_node) |
04f3dc2b MM |
11979 | { |
11980 | /* Top-level qualifiers on the parameters are | |
11981 | ignored for function types. */ | |
11982 | type = TYPE_MAIN_VARIANT (type); | |
11983 | if (TREE_CODE (type) == METHOD_TYPE) | |
11984 | { | |
33bd39a2 | 11985 | error ("parameter `%D' invalidly declared method type", decl); |
04f3dc2b MM |
11986 | type = build_pointer_type (type); |
11987 | TREE_TYPE (decl) = type; | |
11988 | } | |
11989 | else if (TREE_CODE (type) == OFFSET_TYPE) | |
11990 | { | |
33bd39a2 | 11991 | error ("parameter `%D' invalidly declared offset type", decl); |
04f3dc2b MM |
11992 | type = build_pointer_type (type); |
11993 | TREE_TYPE (decl) = type; | |
11994 | } | |
11995 | else if (abstract_virtuals_error (decl, type)) | |
a1c65f9f | 11996 | any_error = 1; /* Seems like a good idea. */ |
04f3dc2b MM |
11997 | else if (POINTER_TYPE_P (type)) |
11998 | { | |
11999 | /* [dcl.fct]/6, parameter types cannot contain pointers | |
12000 | (references) to arrays of unknown bound. */ | |
98979fe0 NS |
12001 | tree t = TREE_TYPE (type); |
12002 | int ptr = TYPE_PTR_P (type); | |
12003 | ||
12004 | while (1) | |
12005 | { | |
12006 | if (TYPE_PTR_P (t)) | |
12007 | ptr = 1; | |
12008 | else if (TREE_CODE (t) != ARRAY_TYPE) | |
12009 | break; | |
12010 | else if (!TYPE_DOMAIN (t)) | |
12011 | break; | |
12012 | t = TREE_TYPE (t); | |
12013 | } | |
04f3dc2b | 12014 | if (TREE_CODE (t) == ARRAY_TYPE) |
33bd39a2 | 12015 | error ("parameter `%D' includes %s to array of unknown bound `%T'", |
98979fe0 | 12016 | decl, ptr ? "pointer" : "reference", t); |
04f3dc2b MM |
12017 | } |
12018 | ||
04f3dc2b MM |
12019 | if (!any_error && init) |
12020 | init = check_default_argument (decl, init); | |
12021 | else | |
12022 | init = NULL_TREE; | |
12023 | } | |
8d08fdba | 12024 | |
5cce22b6 NS |
12025 | TREE_CHAIN (decl) = decls; |
12026 | decls = decl; | |
0657c69c | 12027 | result = tree_cons (init, type, result); |
8d08fdba | 12028 | } |
5cce22b6 NS |
12029 | decls = nreverse (decls); |
12030 | result = nreverse (result); | |
12031 | if (!ellipsis) | |
12032 | result = chainon (result, void_list_node); | |
8d08fdba MS |
12033 | last_function_parms = decls; |
12034 | ||
8d08fdba MS |
12035 | return result; |
12036 | } | |
42976354 | 12037 | |
8d08fdba | 12038 | \f |
271e6f02 NS |
12039 | /* D is a constructor or overloaded `operator='. |
12040 | ||
12041 | Let T be the class in which D is declared. Then, this function | |
12042 | returns: | |
12043 | ||
12044 | -1 if D's is an ill-formed constructor or copy assignment operator | |
12045 | whose first parameter is of type `T'. | |
12046 | 0 if D is not a copy constructor or copy assignment | |
12047 | operator. | |
12048 | 1 if D is a copy constructor or copy assignment operator whose | |
12049 | first parameter is a reference to const qualified T. | |
12050 | 2 if D is a copy constructor or copy assignment operator whose | |
12051 | first parameter is a reference to non-const qualified T. | |
12052 | ||
12053 | This function can be used as a predicate. Positive values indicate | |
838dfd8a | 12054 | a copy constructor and nonzero values indicate a copy assignment |
4f1c5b7d MM |
12055 | operator. */ |
12056 | ||
c11b6f21 | 12057 | int |
11f6b451 | 12058 | copy_fn_p (tree d) |
c11b6f21 | 12059 | { |
271e6f02 NS |
12060 | tree args; |
12061 | tree arg_type; | |
12062 | int result = 1; | |
12063 | ||
12064 | my_friendly_assert (DECL_FUNCTION_MEMBER_P (d), 20011208); | |
4f1c5b7d | 12065 | |
271e6f02 NS |
12066 | if (DECL_TEMPLATE_INFO (d) && is_member_template (DECL_TI_TEMPLATE (d))) |
12067 | /* Instantiations of template member functions are never copy | |
12068 | functions. Note that member functions of templated classes are | |
12069 | represented as template functions internally, and we must | |
12070 | accept those as copy functions. */ | |
12071 | return 0; | |
12072 | ||
12073 | args = FUNCTION_FIRST_USER_PARMTYPE (d); | |
12074 | if (!args) | |
4f1c5b7d MM |
12075 | return 0; |
12076 | ||
271e6f02 NS |
12077 | arg_type = TREE_VALUE (args); |
12078 | ||
12079 | if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d)) | |
12080 | { | |
12081 | /* Pass by value copy assignment operator. */ | |
12082 | result = -1; | |
12083 | } | |
12084 | else if (TREE_CODE (arg_type) == REFERENCE_TYPE | |
12085 | && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d)) | |
12086 | { | |
12087 | if (CP_TYPE_CONST_P (TREE_TYPE (arg_type))) | |
12088 | result = 2; | |
12089 | } | |
12090 | else | |
12091 | return 0; | |
12092 | ||
12093 | args = TREE_CHAIN (args); | |
12094 | ||
12095 | if (args && args != void_list_node && !TREE_PURPOSE (args)) | |
12096 | /* There are more non-optional args. */ | |
12097 | return 0; | |
12098 | ||
12099 | return result; | |
12100 | } | |
12101 | ||
12102 | /* Remember any special properties of member function DECL. */ | |
12103 | ||
11f6b451 | 12104 | void grok_special_member_properties (tree decl) |
271e6f02 NS |
12105 | { |
12106 | if (!DECL_NONSTATIC_MEMBER_FUNCTION_P(decl)) | |
12107 | ; /* Not special. */ | |
12108 | else if (DECL_CONSTRUCTOR_P (decl)) | |
12109 | { | |
12110 | int ctor = copy_fn_p (decl); | |
12111 | ||
12112 | if (ctor > 0) | |
12113 | { | |
12114 | /* [class.copy] | |
12115 | ||
12116 | A non-template constructor for class X is a copy | |
12117 | constructor if its first parameter is of type X&, const | |
12118 | X&, volatile X& or const volatile X&, and either there | |
12119 | are no other parameters or else all other parameters have | |
12120 | default arguments. */ | |
12121 | TYPE_HAS_INIT_REF (DECL_CONTEXT (decl)) = 1; | |
12122 | if (ctor > 1) | |
12123 | TYPE_HAS_CONST_INIT_REF (DECL_CONTEXT (decl)) = 1; | |
12124 | } | |
12125 | else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl))) | |
12126 | TYPE_HAS_DEFAULT_CONSTRUCTOR (DECL_CONTEXT (decl)) = 1; | |
12127 | } | |
12128 | else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR) | |
12129 | { | |
12130 | /* [class.copy] | |
12131 | ||
12132 | A non-template assignment operator for class X is a copy | |
12133 | assignment operator if its parameter is of type X, X&, const | |
12134 | X&, volatile X& or const volatile X&. */ | |
12135 | ||
12136 | int assop = copy_fn_p (decl); | |
12137 | ||
12138 | if (assop) | |
12139 | { | |
12140 | TYPE_HAS_ASSIGN_REF (DECL_CONTEXT (decl)) = 1; | |
12141 | if (assop != 1) | |
12142 | TYPE_HAS_CONST_ASSIGN_REF (DECL_CONTEXT (decl)) = 1; | |
12143 | if (DECL_PURE_VIRTUAL_P (decl)) | |
12144 | TYPE_HAS_ABSTRACT_ASSIGN_REF (DECL_CONTEXT (decl)) = 1; | |
12145 | } | |
12146 | } | |
c11b6f21 MS |
12147 | } |
12148 | ||
271e6f02 NS |
12149 | /* Check a constructor DECL has the correct form. Complains |
12150 | if the class has a constructor of the form X(X). */ | |
e92cc029 | 12151 | |
a0a33927 | 12152 | int |
11f6b451 | 12153 | grok_ctor_properties (tree ctype, tree decl) |
8d08fdba | 12154 | { |
271e6f02 NS |
12155 | int ctor_parm = copy_fn_p (decl); |
12156 | ||
12157 | if (ctor_parm < 0) | |
12158 | { | |
12159 | /* [class.copy] | |
12160 | ||
12161 | A declaration of a constructor for a class X is ill-formed if | |
12162 | its first parameter is of type (optionally cv-qualified) X | |
12163 | and either there are no other parameters or else all other | |
12164 | parameters have default arguments. | |
12165 | ||
12166 | We *don't* complain about member template instantiations that | |
12167 | have this form, though; they can occur as we try to decide | |
12168 | what constructor to use during overload resolution. Since | |
12169 | overload resolution will never prefer such a constructor to | |
12170 | the non-template copy constructor (which is either explicitly | |
12171 | or implicitly defined), there's no need to worry about their | |
12172 | existence. Theoretically, they should never even be | |
12173 | instantiated, but that's hard to forestall. */ | |
33bd39a2 | 12174 | error ("invalid constructor; you probably meant `%T (const %T&)'", |
0b41abe6 JM |
12175 | ctype, ctype); |
12176 | SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype); | |
12177 | return 0; | |
8d08fdba | 12178 | } |
271e6f02 | 12179 | |
a0a33927 | 12180 | return 1; |
8d08fdba MS |
12181 | } |
12182 | ||
596ea4e5 | 12183 | /* An operator with this code is unary, but can also be binary. */ |
e92cc029 | 12184 | |
a28e3c7f | 12185 | static int |
11f6b451 | 12186 | ambi_op_p (enum tree_code code) |
8d08fdba | 12187 | { |
596ea4e5 AS |
12188 | return (code == INDIRECT_REF |
12189 | || code == ADDR_EXPR | |
12190 | || code == CONVERT_EXPR | |
12191 | || code == NEGATE_EXPR | |
12192 | || code == PREINCREMENT_EXPR | |
12193 | || code == PREDECREMENT_EXPR); | |
8d08fdba MS |
12194 | } |
12195 | ||
12196 | /* An operator with this name can only be unary. */ | |
e92cc029 | 12197 | |
a28e3c7f | 12198 | static int |
11f6b451 | 12199 | unary_op_p (enum tree_code code) |
8d08fdba | 12200 | { |
596ea4e5 AS |
12201 | return (code == TRUTH_NOT_EXPR |
12202 | || code == BIT_NOT_EXPR | |
12203 | || code == COMPONENT_REF | |
12204 | || code == TYPE_EXPR); | |
8d08fdba MS |
12205 | } |
12206 | ||
12207 | /* Do a little sanity-checking on how they declared their operator. */ | |
e92cc029 | 12208 | |
5566b478 | 12209 | void |
11f6b451 | 12210 | grok_op_properties (tree decl, int friendp) |
8d08fdba MS |
12211 | { |
12212 | tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
596ea4e5 | 12213 | tree argtype; |
8d08fdba MS |
12214 | int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE); |
12215 | tree name = DECL_NAME (decl); | |
596ea4e5 AS |
12216 | enum tree_code operator_code; |
12217 | int arity; | |
12218 | ||
12219 | /* Count the number of arguments. */ | |
12220 | for (argtype = argtypes, arity = 0; | |
12221 | argtype && argtype != void_list_node; | |
12222 | argtype = TREE_CHAIN (argtype)) | |
12223 | ++arity; | |
8d08fdba | 12224 | |
a28e3c7f MS |
12225 | if (current_class_type == NULL_TREE) |
12226 | friendp = 1; | |
8d08fdba | 12227 | |
596ea4e5 AS |
12228 | if (DECL_CONV_FN_P (decl)) |
12229 | operator_code = TYPE_EXPR; | |
12230 | else | |
12231 | do | |
12232 | { | |
0c918ce5 MM |
12233 | #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \ |
12234 | if (ansi_opname (CODE) == name) \ | |
12235 | { \ | |
75ac8dec | 12236 | operator_code = (CODE); \ |
0c918ce5 MM |
12237 | break; \ |
12238 | } \ | |
12239 | else if (ansi_assopname (CODE) == name) \ | |
12240 | { \ | |
75ac8dec | 12241 | operator_code = (CODE); \ |
0c918ce5 MM |
12242 | DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \ |
12243 | break; \ | |
596ea4e5 AS |
12244 | } |
12245 | ||
12246 | #include "operators.def" | |
12247 | #undef DEF_OPERATOR | |
12248 | ||
a98facb0 | 12249 | abort (); |
596ea4e5 AS |
12250 | } |
12251 | while (0); | |
12252 | my_friendly_assert (operator_code != LAST_CPLUS_TREE_CODE, 20000526); | |
12253 | SET_OVERLOADED_OPERATOR_CODE (decl, operator_code); | |
12254 | ||
a28e3c7f MS |
12255 | if (! friendp) |
12256 | { | |
596ea4e5 AS |
12257 | switch (operator_code) |
12258 | { | |
12259 | case CALL_EXPR: | |
12260 | TYPE_OVERLOADS_CALL_EXPR (current_class_type) = 1; | |
12261 | break; | |
5362b086 | 12262 | |
596ea4e5 AS |
12263 | case ARRAY_REF: |
12264 | TYPE_OVERLOADS_ARRAY_REF (current_class_type) = 1; | |
12265 | break; | |
f181d4ae | 12266 | |
596ea4e5 AS |
12267 | case COMPONENT_REF: |
12268 | case MEMBER_REF: | |
12269 | TYPE_OVERLOADS_ARROW (current_class_type) = 1; | |
12270 | break; | |
5362b086 | 12271 | |
596ea4e5 AS |
12272 | case NEW_EXPR: |
12273 | TYPE_HAS_NEW_OPERATOR (current_class_type) = 1; | |
12274 | break; | |
5362b086 | 12275 | |
596ea4e5 AS |
12276 | case DELETE_EXPR: |
12277 | TYPE_GETS_DELETE (current_class_type) |= 1; | |
12278 | break; | |
5362b086 | 12279 | |
596ea4e5 AS |
12280 | case VEC_NEW_EXPR: |
12281 | TYPE_HAS_ARRAY_NEW_OPERATOR (current_class_type) = 1; | |
12282 | break; | |
5362b086 | 12283 | |
596ea4e5 AS |
12284 | case VEC_DELETE_EXPR: |
12285 | TYPE_GETS_DELETE (current_class_type) |= 2; | |
12286 | break; | |
12287 | ||
12288 | default: | |
12289 | break; | |
12290 | } | |
12291 | } | |
12292 | ||
12293 | if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR) | |
8d08fdba | 12294 | { |
8d08fdba MS |
12295 | /* When the compiler encounters the definition of A::operator new, it |
12296 | doesn't look at the class declaration to find out if it's static. */ | |
a28e3c7f | 12297 | if (methodp) |
3afb32a4 | 12298 | revert_static_member_fn (decl); |
68642fb6 | 12299 | |
36791f1e | 12300 | TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl)); |
8d08fdba | 12301 | } |
596ea4e5 | 12302 | else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR) |
8d08fdba | 12303 | { |
a28e3c7f | 12304 | if (methodp) |
3afb32a4 | 12305 | revert_static_member_fn (decl); |
68642fb6 | 12306 | |
36791f1e | 12307 | TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl)); |
8d08fdba | 12308 | } |
8d08fdba MS |
12309 | else |
12310 | { | |
12311 | /* An operator function must either be a non-static member function | |
12312 | or have at least one parameter of a class, a reference to a class, | |
12313 | an enumeration, or a reference to an enumeration. 13.4.0.6 */ | |
700f8a87 | 12314 | if (! methodp || DECL_STATIC_FUNCTION_P (decl)) |
8d08fdba | 12315 | { |
596ea4e5 AS |
12316 | if (operator_code == TYPE_EXPR |
12317 | || operator_code == CALL_EXPR | |
12318 | || operator_code == COMPONENT_REF | |
12319 | || operator_code == ARRAY_REF | |
12320 | || operator_code == NOP_EXPR) | |
33bd39a2 | 12321 | error ("`%D' must be a nonstatic member function", decl); |
8d08fdba MS |
12322 | else |
12323 | { | |
12324 | tree p = argtypes; | |
12325 | ||
700f8a87 | 12326 | if (DECL_STATIC_FUNCTION_P (decl)) |
33bd39a2 | 12327 | error ("`%D' must be either a non-static member function or a non-member function", decl); |
700f8a87 | 12328 | |
8d08fdba | 12329 | if (p) |
a1774733 | 12330 | for (; TREE_CODE (TREE_VALUE (p)) != VOID_TYPE ; p = TREE_CHAIN (p)) |
8d08fdba MS |
12331 | { |
12332 | tree arg = TREE_VALUE (p); | |
12333 | if (TREE_CODE (arg) == REFERENCE_TYPE) | |
12334 | arg = TREE_TYPE (arg); | |
12335 | ||
12336 | /* This lets bad template code slip through. */ | |
12337 | if (IS_AGGR_TYPE (arg) | |
12338 | || TREE_CODE (arg) == ENUMERAL_TYPE | |
73b0fce8 | 12339 | || TREE_CODE (arg) == TEMPLATE_TYPE_PARM |
a1281f45 | 12340 | || TREE_CODE (arg) == BOUND_TEMPLATE_TEMPLATE_PARM) |
8d08fdba MS |
12341 | goto foundaggr; |
12342 | } | |
33bd39a2 | 12343 | error |
8251199e | 12344 | ("`%D' must have an argument of class or enumerated type", |
8d08fdba MS |
12345 | decl); |
12346 | foundaggr: | |
12347 | ; | |
12348 | } | |
12349 | } | |
68642fb6 | 12350 | |
596ea4e5 | 12351 | if (operator_code == CALL_EXPR) |
a1c65f9f | 12352 | return; /* No restrictions on args. */ |
8d08fdba | 12353 | |
9a3b49ac | 12354 | if (IDENTIFIER_TYPENAME_P (name) && ! DECL_TEMPLATE_INFO (decl)) |
a0a33927 MS |
12355 | { |
12356 | tree t = TREE_TYPE (name); | |
17708e90 | 12357 | if (! friendp) |
a0a33927 MS |
12358 | { |
12359 | int ref = (TREE_CODE (t) == REFERENCE_TYPE); | |
d8e178a0 | 12360 | const char *what = 0; |
5362b086 | 12361 | |
a0a33927 MS |
12362 | if (ref) |
12363 | t = TYPE_MAIN_VARIANT (TREE_TYPE (t)); | |
12364 | ||
17708e90 NS |
12365 | if (TREE_CODE (t) == VOID_TYPE) |
12366 | what = "void"; | |
12367 | else if (t == current_class_type) | |
a0a33927 | 12368 | what = "the same type"; |
9a3b49ac | 12369 | /* Don't force t to be complete here. */ |
a0a33927 | 12370 | else if (IS_AGGR_TYPE (t) |
d0f062fb | 12371 | && COMPLETE_TYPE_P (t) |
a0a33927 MS |
12372 | && DERIVED_FROM_P (t, current_class_type)) |
12373 | what = "a base class"; | |
12374 | ||
dfbeb061 | 12375 | if (what && warn_conversion) |
8251199e | 12376 | warning ("conversion to %s%s will never use a type conversion operator", |
a0a33927 MS |
12377 | ref ? "a reference to " : "", what); |
12378 | } | |
12379 | } | |
271e6f02 | 12380 | if (operator_code == COND_EXPR) |
8d08fdba MS |
12381 | { |
12382 | /* 13.4.0.3 */ | |
33bd39a2 | 12383 | error ("ISO C++ prohibits overloading operator ?:"); |
68642fb6 | 12384 | } |
596ea4e5 | 12385 | else if (ambi_op_p (operator_code)) |
8d08fdba | 12386 | { |
596ea4e5 AS |
12387 | if (arity == 1) |
12388 | /* We pick the one-argument operator codes by default, so | |
12389 | we don't have to change anything. */ | |
12390 | ; | |
12391 | else if (arity == 2) | |
8d08fdba | 12392 | { |
596ea4e5 AS |
12393 | /* If we thought this was a unary operator, we now know |
12394 | it to be a binary operator. */ | |
12395 | switch (operator_code) | |
12396 | { | |
12397 | case INDIRECT_REF: | |
12398 | operator_code = MULT_EXPR; | |
12399 | break; | |
12400 | ||
12401 | case ADDR_EXPR: | |
12402 | operator_code = BIT_AND_EXPR; | |
12403 | break; | |
12404 | ||
12405 | case CONVERT_EXPR: | |
12406 | operator_code = PLUS_EXPR; | |
12407 | break; | |
12408 | ||
12409 | case NEGATE_EXPR: | |
12410 | operator_code = MINUS_EXPR; | |
12411 | break; | |
12412 | ||
12413 | case PREINCREMENT_EXPR: | |
12414 | operator_code = POSTINCREMENT_EXPR; | |
12415 | break; | |
12416 | ||
12417 | case PREDECREMENT_EXPR: | |
655dc6ee | 12418 | operator_code = POSTDECREMENT_EXPR; |
596ea4e5 AS |
12419 | break; |
12420 | ||
12421 | default: | |
a98facb0 | 12422 | abort (); |
596ea4e5 AS |
12423 | } |
12424 | ||
12425 | SET_OVERLOADED_OPERATOR_CODE (decl, operator_code); | |
12426 | ||
12427 | if ((operator_code == POSTINCREMENT_EXPR | |
12428 | || operator_code == POSTDECREMENT_EXPR) | |
5156628f | 12429 | && ! processing_template_decl |
007e5fea | 12430 | && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node)) |
8d08fdba MS |
12431 | { |
12432 | if (methodp) | |
33bd39a2 | 12433 | error ("postfix `%D' must take `int' as its argument", |
8d08fdba MS |
12434 | decl); |
12435 | else | |
33bd39a2 | 12436 | error |
8251199e JM |
12437 | ("postfix `%D' must take `int' as its second argument", |
12438 | decl); | |
8d08fdba MS |
12439 | } |
12440 | } | |
12441 | else | |
12442 | { | |
12443 | if (methodp) | |
33bd39a2 | 12444 | error ("`%D' must take either zero or one argument", decl); |
8d08fdba | 12445 | else |
33bd39a2 | 12446 | error ("`%D' must take either one or two arguments", decl); |
8d08fdba | 12447 | } |
824b9a4c MS |
12448 | |
12449 | /* More Effective C++ rule 6. */ | |
eb448459 | 12450 | if (warn_ecpp |
596ea4e5 AS |
12451 | && (operator_code == POSTINCREMENT_EXPR |
12452 | || operator_code == POSTDECREMENT_EXPR | |
12453 | || operator_code == PREINCREMENT_EXPR | |
12454 | || operator_code == PREDECREMENT_EXPR)) | |
824b9a4c MS |
12455 | { |
12456 | tree arg = TREE_VALUE (argtypes); | |
12457 | tree ret = TREE_TYPE (TREE_TYPE (decl)); | |
12458 | if (methodp || TREE_CODE (arg) == REFERENCE_TYPE) | |
12459 | arg = TREE_TYPE (arg); | |
12460 | arg = TYPE_MAIN_VARIANT (arg); | |
596ea4e5 AS |
12461 | if (operator_code == PREINCREMENT_EXPR |
12462 | || operator_code == PREDECREMENT_EXPR) | |
824b9a4c MS |
12463 | { |
12464 | if (TREE_CODE (ret) != REFERENCE_TYPE | |
3bfdc719 MM |
12465 | || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), |
12466 | arg)) | |
33bd39a2 | 12467 | warning ("prefix `%D' should return `%T'", decl, |
824b9a4c MS |
12468 | build_reference_type (arg)); |
12469 | } | |
12470 | else | |
12471 | { | |
3bfdc719 | 12472 | if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg)) |
33bd39a2 | 12473 | warning ("postfix `%D' should return `%T'", decl, arg); |
824b9a4c MS |
12474 | } |
12475 | } | |
8d08fdba | 12476 | } |
596ea4e5 | 12477 | else if (unary_op_p (operator_code)) |
8d08fdba | 12478 | { |
596ea4e5 | 12479 | if (arity != 1) |
8d08fdba MS |
12480 | { |
12481 | if (methodp) | |
33bd39a2 | 12482 | error ("`%D' must take `void'", decl); |
8d08fdba | 12483 | else |
33bd39a2 | 12484 | error ("`%D' must take exactly one argument", decl); |
8d08fdba MS |
12485 | } |
12486 | } | |
596ea4e5 | 12487 | else /* if (binary_op_p (operator_code)) */ |
8d08fdba | 12488 | { |
596ea4e5 | 12489 | if (arity != 2) |
8d08fdba MS |
12490 | { |
12491 | if (methodp) | |
33bd39a2 | 12492 | error ("`%D' must take exactly one argument", decl); |
8d08fdba | 12493 | else |
33bd39a2 | 12494 | error ("`%D' must take exactly two arguments", decl); |
8d08fdba | 12495 | } |
824b9a4c MS |
12496 | |
12497 | /* More Effective C++ rule 7. */ | |
eb448459 | 12498 | if (warn_ecpp |
596ea4e5 AS |
12499 | && (operator_code == TRUTH_ANDIF_EXPR |
12500 | || operator_code == TRUTH_ORIF_EXPR | |
12501 | || operator_code == COMPOUND_EXPR)) | |
33bd39a2 | 12502 | warning ("user-defined `%D' always evaluates both arguments", |
824b9a4c MS |
12503 | decl); |
12504 | } | |
12505 | ||
12506 | /* Effective C++ rule 23. */ | |
eb448459 | 12507 | if (warn_ecpp |
596ea4e5 | 12508 | && arity == 2 |
4bd7c270 | 12509 | && !DECL_ASSIGNMENT_OPERATOR_P (decl) |
596ea4e5 AS |
12510 | && (operator_code == PLUS_EXPR |
12511 | || operator_code == MINUS_EXPR | |
12512 | || operator_code == TRUNC_DIV_EXPR | |
4bd7c270 NS |
12513 | || operator_code == MULT_EXPR |
12514 | || operator_code == TRUNC_MOD_EXPR) | |
824b9a4c | 12515 | && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE) |
33bd39a2 | 12516 | warning ("`%D' should return by value", decl); |
8d08fdba | 12517 | |
271e6f02 | 12518 | /* [over.oper]/8 */ |
34332678 CT |
12519 | for (; argtypes && argtypes != void_list_node; |
12520 | argtypes = TREE_CHAIN (argtypes)) | |
12521 | if (TREE_PURPOSE (argtypes)) | |
12522 | { | |
12523 | TREE_PURPOSE (argtypes) = NULL_TREE; | |
596ea4e5 AS |
12524 | if (operator_code == POSTINCREMENT_EXPR |
12525 | || operator_code == POSTDECREMENT_EXPR) | |
34332678 CT |
12526 | { |
12527 | if (pedantic) | |
33bd39a2 | 12528 | pedwarn ("`%D' cannot have default arguments", decl); |
34332678 CT |
12529 | } |
12530 | else | |
33bd39a2 | 12531 | error ("`%D' cannot have default arguments", decl); |
34332678 CT |
12532 | } |
12533 | ||
8d08fdba MS |
12534 | } |
12535 | } | |
12536 | \f | |
d8e178a0 | 12537 | static const char * |
11f6b451 | 12538 | tag_name (enum tag_types code) |
094fe153 JM |
12539 | { |
12540 | switch (code) | |
12541 | { | |
12542 | case record_type: | |
12543 | return "struct"; | |
12544 | case class_type: | |
12545 | return "class"; | |
12546 | case union_type: | |
12547 | return "union "; | |
12548 | case enum_type: | |
12549 | return "enum"; | |
094fe153 | 12550 | default: |
a98facb0 | 12551 | abort (); |
094fe153 JM |
12552 | } |
12553 | } | |
12554 | ||
befcd99b MM |
12555 | /* Name lookup in an elaborated-type-specifier (after the keyword |
12556 | indicated by TAG_CODE) has found TYPE. If the | |
12557 | elaborated-type-specifier is invalid, issue a diagnostic and return | |
12558 | error_mark_node; otherwise, return TYPE itself. */ | |
12559 | ||
12560 | static tree | |
12561 | check_elaborated_type_specifier (enum tag_types tag_code, | |
12562 | tree type) | |
12563 | { | |
12564 | tree t; | |
12565 | ||
12566 | t = follow_tag_typedef (type); | |
12567 | ||
12568 | /* [dcl.type.elab] If the identifier resolves to a typedef-name or a | |
12569 | template type-parameter, the elaborated-type-specifier is | |
12570 | ill-formed. */ | |
12571 | if (!t) | |
12572 | { | |
12573 | error ("using typedef-name `%D' after `%s'", | |
12574 | TYPE_NAME (type), tag_name (tag_code)); | |
12575 | t = error_mark_node; | |
12576 | } | |
12577 | else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) | |
12578 | { | |
12579 | error ("using template type parameter `%T' after `%s'", | |
12580 | type, tag_name (tag_code)); | |
12581 | t = error_mark_node; | |
12582 | } | |
12583 | ||
12584 | return t; | |
12585 | } | |
12586 | ||
8d08fdba MS |
12587 | /* Get the struct, enum or union (CODE says which) with tag NAME. |
12588 | Define the tag as a forward-reference if it is not defined. | |
12589 | ||
12590 | C++: If a class derivation is given, process it here, and report | |
12591 | an error if multiple derivation declarations are not identical. | |
12592 | ||
12593 | If this is a definition, come in through xref_tag and only look in | |
12594 | the current frame for the name (since C++ allows new names in any | |
12595 | scope.) */ | |
12596 | ||
8d08fdba | 12597 | tree |
88e5899c MM |
12598 | xref_tag (enum tag_types tag_code, tree name, tree attributes, |
12599 | bool globalize) | |
8d08fdba | 12600 | { |
8d08fdba | 12601 | enum tree_code code; |
8d08fdba | 12602 | register tree ref, t; |
e2500fed | 12603 | struct cp_binding_level *b = current_binding_level; |
25aab5d0 | 12604 | tree context = NULL_TREE; |
dc8263bc | 12605 | |
22ffcc6f | 12606 | timevar_push (TV_NAME_LOOKUP); |
8d08fdba MS |
12607 | switch (tag_code) |
12608 | { | |
12609 | case record_type: | |
12610 | case class_type: | |
8d08fdba | 12611 | code = RECORD_TYPE; |
8d08fdba MS |
12612 | break; |
12613 | case union_type: | |
12614 | code = UNION_TYPE; | |
8d08fdba MS |
12615 | break; |
12616 | case enum_type: | |
12617 | code = ENUMERAL_TYPE; | |
12618 | break; | |
12619 | default: | |
a98facb0 | 12620 | abort (); |
8d08fdba MS |
12621 | } |
12622 | ||
12623 | /* If a cross reference is requested, look up the type | |
12624 | already defined for this tag and return it. */ | |
2f939d94 | 12625 | if (TYPE_P (name)) |
be99da77 MS |
12626 | { |
12627 | t = name; | |
a80e4195 | 12628 | name = TYPE_IDENTIFIER (t); |
be99da77 MS |
12629 | } |
12630 | else | |
12631 | t = IDENTIFIER_TYPE_VALUE (name); | |
61a127b3 | 12632 | |
f2ae0c45 JM |
12633 | /* Warn about 'friend struct Inherited;' doing the wrong thing. */ |
12634 | if (t && globalize && TREE_CODE (t) == TYPENAME_TYPE) | |
12635 | { | |
12636 | static int explained; | |
2b0cbc5d NS |
12637 | tree shadowed; |
12638 | ||
33bd39a2 | 12639 | warning ("`%s %T' declares a new type at namespace scope", |
2b0cbc5d NS |
12640 | tag_name (tag_code), name); |
12641 | if (!explained++) | |
33bd39a2 | 12642 | warning (" names from dependent base classes are not visible to unqualified name lookup - to refer to the inherited type, say `%s %T::%T'", |
2b0cbc5d NS |
12643 | tag_name (tag_code), |
12644 | constructor_name (current_class_type), | |
12645 | TYPE_IDENTIFIER (t)); | |
12646 | ||
12647 | /* We need to remove the class scope binding for the | |
a1c65f9f | 12648 | TYPENAME_TYPE as otherwise poplevel_class gets confused. */ |
2b0cbc5d NS |
12649 | for (shadowed = b->class_shadowed; |
12650 | shadowed; | |
12651 | shadowed = TREE_CHAIN (shadowed)) | |
12652 | if (TREE_TYPE (shadowed) == TYPE_NAME (t)) | |
12653 | { | |
12654 | TREE_PURPOSE (shadowed) = NULL_TREE; | |
12655 | break; | |
12656 | } | |
f2ae0c45 JM |
12657 | } |
12658 | ||
73b0fce8 | 12659 | if (t && TREE_CODE (t) != code && TREE_CODE (t) != TEMPLATE_TYPE_PARM |
a1281f45 | 12660 | && TREE_CODE (t) != BOUND_TEMPLATE_TEMPLATE_PARM) |
a0a33927 MS |
12661 | t = NULL_TREE; |
12662 | ||
8ccc31eb | 12663 | if (! globalize) |
8d08fdba | 12664 | { |
f3400fe2 JM |
12665 | /* If we know we are defining this tag, only look it up in |
12666 | this scope and don't try to find it as a type. */ | |
12667 | ref = lookup_tag (code, name, b, 1); | |
8d08fdba MS |
12668 | } |
12669 | else | |
12670 | { | |
25aab5d0 | 12671 | if (t) |
36a117a5 | 12672 | { |
befcd99b MM |
12673 | ref = check_elaborated_type_specifier (tag_code, t); |
12674 | if (ref == error_mark_node) | |
12675 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); | |
25aab5d0 MM |
12676 | } |
12677 | else | |
12678 | ref = lookup_tag (code, name, b, 0); | |
68642fb6 | 12679 | |
25aab5d0 MM |
12680 | if (! ref) |
12681 | { | |
12682 | /* Try finding it as a type declaration. If that wins, | |
68642fb6 | 12683 | use it. */ |
25aab5d0 MM |
12684 | ref = lookup_name (name, 1); |
12685 | ||
12686 | if (ref != NULL_TREE | |
12687 | && processing_template_decl | |
12688 | && DECL_CLASS_TEMPLATE_P (ref) | |
12689 | && template_class_depth (current_class_type) == 0) | |
12690 | /* Since GLOBALIZE is true, we're declaring a global | |
6757edfe | 12691 | template, so we want this type. */ |
17aec3eb | 12692 | ref = DECL_TEMPLATE_RESULT (ref); |
6757edfe | 12693 | |
befcd99b MM |
12694 | if (ref && TREE_CODE (ref) == TYPE_DECL) |
12695 | { | |
12696 | ref = check_elaborated_type_specifier (tag_code, | |
12697 | TREE_TYPE (ref)); | |
12698 | if (ref == error_mark_node) | |
12699 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); | |
12700 | if (ref && TREE_CODE (ref) != code) | |
12701 | ref = NULL_TREE; | |
12702 | } | |
25aab5d0 MM |
12703 | else |
12704 | ref = NULL_TREE; | |
12705 | } | |
12706 | ||
68642fb6 UD |
12707 | if (ref && current_class_type |
12708 | && template_class_depth (current_class_type) | |
12709 | && PROCESSING_REAL_TEMPLATE_DECL_P ()) | |
25aab5d0 | 12710 | { |
838dfd8a | 12711 | /* Since GLOBALIZE is nonzero, we are not looking at a |
25aab5d0 MM |
12712 | definition of this tag. Since, in addition, we are currently |
12713 | processing a (member) template declaration of a template | |
12714 | class, we must be very careful; consider: | |
12715 | ||
12716 | template <class X> | |
12717 | struct S1 | |
12718 | ||
12719 | template <class U> | |
12720 | struct S2 | |
12721 | { template <class V> | |
12722 | friend struct S1; }; | |
12723 | ||
12724 | Here, the S2::S1 declaration should not be confused with the | |
12725 | outer declaration. In particular, the inner version should | |
12726 | have a template parameter of level 2, not level 1. This | |
12727 | would be particularly important if the member declaration | |
12728 | were instead: | |
12729 | ||
12730 | template <class V = U> friend struct S1; | |
12731 | ||
12732 | say, when we should tsubst into `U' when instantiating | |
12733 | S2. On the other hand, when presented with: | |
12734 | ||
12735 | template <class T> | |
12736 | struct S1 { | |
12737 | template <class U> | |
12738 | struct S2 {}; | |
12739 | template <class U> | |
12740 | friend struct S2; | |
12741 | }; | |
12742 | ||
12743 | we must find the inner binding eventually. We | |
12744 | accomplish this by making sure that the new type we | |
12745 | create to represent this declaration has the right | |
12746 | TYPE_CONTEXT. */ | |
12747 | context = TYPE_CONTEXT (ref); | |
12748 | ref = NULL_TREE; | |
8d08fdba MS |
12749 | } |
12750 | } | |
12751 | ||
8d08fdba MS |
12752 | if (! ref) |
12753 | { | |
12754 | /* If no such tag is yet defined, create a forward-reference node | |
12755 | and record it as the "definition". | |
12756 | When a real declaration of this type is found, | |
12757 | the forward-reference will be altered into a real type. */ | |
8d08fdba MS |
12758 | if (code == ENUMERAL_TYPE) |
12759 | { | |
33bd39a2 | 12760 | error ("use of enum `%#D' without previous declaration", name); |
fc378698 | 12761 | |
8d08fdba MS |
12762 | ref = make_node (ENUMERAL_TYPE); |
12763 | ||
12764 | /* Give the type a default layout like unsigned int | |
12765 | to avoid crashing if it does not get defined. */ | |
12766 | TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node); | |
12767 | TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node); | |
11cf4d18 | 12768 | TYPE_USER_ALIGN (ref) = 0; |
8d08fdba MS |
12769 | TREE_UNSIGNED (ref) = 1; |
12770 | TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node); | |
12771 | TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node); | |
12772 | TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node); | |
12773 | ||
12774 | /* Enable us to recognize when a type is created in class context. | |
12775 | To do nested classes correctly, this should probably be cleared | |
12776 | out when we leave this classes scope. Currently this in only | |
12777 | done in `start_enum'. */ | |
12778 | ||
12779 | pushtag (name, ref, globalize); | |
8d08fdba | 12780 | } |
8d08fdba MS |
12781 | else |
12782 | { | |
e2500fed | 12783 | struct cp_binding_level *old_b = class_binding_level; |
8d08fdba | 12784 | |
33848bb0 | 12785 | ref = make_aggr_type (code); |
25aab5d0 | 12786 | TYPE_CONTEXT (ref) = context; |
8d08fdba MS |
12787 | pushtag (name, ref, globalize); |
12788 | class_binding_level = old_b; | |
8d08fdba MS |
12789 | } |
12790 | } | |
12791 | else | |
12792 | { | |
7fe6899f | 12793 | if (!globalize && processing_template_decl && IS_AGGR_TYPE (ref)) |
6757edfe | 12794 | redeclare_class_template (ref, current_template_parms); |
8d08fdba MS |
12795 | } |
12796 | ||
a588fe25 | 12797 | TYPE_ATTRIBUTES (ref) = attributes; |
dc8263bc | 12798 | |
22ffcc6f | 12799 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ref); |
8d08fdba | 12800 | } |
8ccc31eb | 12801 | |
fc378698 | 12802 | tree |
11f6b451 | 12803 | xref_tag_from_type (tree old, tree id, int globalize) |
fc378698 | 12804 | { |
88e5899c | 12805 | enum tag_types tag_kind; |
fc378698 MS |
12806 | |
12807 | if (TREE_CODE (old) == RECORD_TYPE) | |
88e5899c | 12808 | tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type); |
fc378698 | 12809 | else |
88e5899c | 12810 | tag_kind = union_type; |
fc378698 MS |
12811 | |
12812 | if (id == NULL_TREE) | |
12813 | id = TYPE_IDENTIFIER (old); | |
12814 | ||
88e5899c | 12815 | return xref_tag (tag_kind, id, /*attributes=*/NULL_TREE, globalize); |
fc378698 MS |
12816 | } |
12817 | ||
3fd71a52 | 12818 | /* REF is a type (named NAME), for which we have just seen some |
dbbf88d1 | 12819 | baseclasses. BASE_LIST is a list of those baseclasses; the |
3fd71a52 | 12820 | TREE_PURPOSE is an access_* node, and the TREE_VALUE is the type of |
dbbf88d1 NS |
12821 | the base-class. TREE_VIA_VIRTUAL indicates virtual |
12822 | inheritance. CODE_TYPE_NODE indicates whether REF is a class, | |
3fd71a52 MM |
12823 | struct, or union. */ |
12824 | ||
8ccc31eb | 12825 | void |
dbbf88d1 | 12826 | xref_basetypes (tree ref, tree base_list) |
8ccc31eb MS |
12827 | { |
12828 | /* In the declaration `A : X, Y, ... Z' we mark all the types | |
12829 | (A, X, Y, ..., Z) so we can check for duplicates. */ | |
8fbc5ae7 | 12830 | tree *basep; |
d6479fe7 | 12831 | |
8fbc5ae7 | 12832 | int i; |
591382c4 | 12833 | enum tag_types tag_code; |
a5d7c4a3 | 12834 | |
da15dae6 | 12835 | if (TREE_CODE (ref) == UNION_TYPE) |
8ccc31eb | 12836 | { |
33bd39a2 | 12837 | error ("derived union `%T' invalid", ref); |
8ccc31eb MS |
12838 | return; |
12839 | } | |
12840 | ||
da15dae6 MM |
12841 | tag_code = (CLASSTYPE_DECLARED_CLASS (ref) ? class_type : record_type); |
12842 | ||
d6479fe7 MM |
12843 | /* First, make sure that any templates in base-classes are |
12844 | instantiated. This ensures that if we call ourselves recursively | |
12845 | we do not get confused about which classes are marked and which | |
12846 | are not. */ | |
dbbf88d1 | 12847 | basep = &base_list; |
8fbc5ae7 MM |
12848 | while (*basep) |
12849 | { | |
12850 | tree basetype = TREE_VALUE (*basep); | |
12851 | if (!(processing_template_decl && uses_template_parms (basetype)) | |
12852 | && !complete_type_or_else (basetype, NULL)) | |
dbbf88d1 | 12853 | /* An incomplete type. Remove it from the list. */ |
8fbc5ae7 MM |
12854 | *basep = TREE_CHAIN (*basep); |
12855 | else | |
12856 | basep = &TREE_CHAIN (*basep); | |
12857 | } | |
d6479fe7 | 12858 | |
8ccc31eb | 12859 | SET_CLASSTYPE_MARKED (ref); |
dbbf88d1 NS |
12860 | i = list_length (base_list); |
12861 | if (i) | |
12862 | { | |
12863 | tree binfo = TYPE_BINFO (ref); | |
12864 | tree binfos = make_tree_vec (i); | |
12865 | tree accesses = make_tree_vec (i); | |
12866 | ||
12867 | BINFO_BASETYPES (binfo) = binfos; | |
12868 | BINFO_BASEACCESSES (binfo) = accesses; | |
12869 | ||
12870 | for (i = 0; base_list; base_list = TREE_CHAIN (base_list)) | |
8ccc31eb | 12871 | { |
dbbf88d1 NS |
12872 | tree access = TREE_PURPOSE (base_list); |
12873 | int via_virtual = TREE_VIA_VIRTUAL (base_list); | |
12874 | tree basetype = TREE_VALUE (base_list); | |
12875 | tree base_binfo; | |
12876 | ||
12877 | if (access == access_default_node) | |
12878 | /* The base of a derived struct is public by default. */ | |
12879 | access = (tag_code == class_type | |
12880 | ? access_private_node : access_public_node); | |
12881 | ||
12882 | if (basetype && TREE_CODE (basetype) == TYPE_DECL) | |
12883 | basetype = TREE_TYPE (basetype); | |
12884 | if (!basetype | |
12885 | || (TREE_CODE (basetype) != RECORD_TYPE | |
12886 | && TREE_CODE (basetype) != TYPENAME_TYPE | |
12887 | && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM | |
12888 | && TREE_CODE (basetype) != BOUND_TEMPLATE_TEMPLATE_PARM)) | |
12889 | { | |
12890 | error ("base type `%T' fails to be a struct or class type", | |
12891 | basetype); | |
12892 | continue; | |
12893 | } | |
12894 | ||
12895 | if (CLASSTYPE_MARKED (basetype)) | |
12896 | { | |
12897 | if (basetype == ref) | |
12898 | error ("recursive type `%T' undefined", basetype); | |
12899 | else | |
12900 | error ("duplicate base type `%T' invalid", basetype); | |
12901 | continue; | |
12902 | } | |
12903 | ||
12904 | if (TYPE_FOR_JAVA (basetype) | |
12905 | && (current_lang_depth () == 0)) | |
12906 | TYPE_FOR_JAVA (ref) = 1; | |
12907 | ||
12908 | if (CLASS_TYPE_P (basetype)) | |
12909 | { | |
12910 | base_binfo = TYPE_BINFO (basetype); | |
12911 | /* This flag will be in the binfo of the base type, we must | |
12912 | clear it after copying the base binfos. */ | |
12913 | BINFO_DEPENDENT_BASE_P (base_binfo) | |
12914 | = dependent_type_p (basetype); | |
12915 | } | |
8fbc5ae7 | 12916 | else |
dbbf88d1 NS |
12917 | base_binfo = make_binfo (size_zero_node, basetype, |
12918 | NULL_TREE, NULL_TREE); | |
12919 | ||
12920 | TREE_VEC_ELT (binfos, i) = base_binfo; | |
12921 | TREE_VEC_ELT (accesses, i) = access; | |
12922 | /* This flag will be in the binfo of the base type, we must | |
12923 | clear it after copying the base binfos. */ | |
12924 | TREE_VIA_VIRTUAL (base_binfo) = via_virtual; | |
12925 | ||
12926 | SET_CLASSTYPE_MARKED (basetype); | |
12927 | ||
12928 | /* We are free to modify these bits because they are meaningless | |
12929 | at top level, and BASETYPE is a top-level type. */ | |
12930 | if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype)) | |
12931 | { | |
12932 | TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1; | |
12933 | /* Converting to a virtual base class requires looking | |
12934 | up the offset of the virtual base. */ | |
12935 | TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1; | |
12936 | } | |
12937 | ||
12938 | if (CLASS_TYPE_P (basetype)) | |
12939 | { | |
12940 | TYPE_HAS_NEW_OPERATOR (ref) | |
12941 | |= TYPE_HAS_NEW_OPERATOR (basetype); | |
12942 | TYPE_HAS_ARRAY_NEW_OPERATOR (ref) | |
12943 | |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype); | |
12944 | TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype); | |
12945 | /* If the base-class uses multiple inheritance, so do we. */ | |
12946 | TYPE_USES_MULTIPLE_INHERITANCE (ref) | |
12947 | |= TYPE_USES_MULTIPLE_INHERITANCE (basetype); | |
12948 | /* Likewise, if converting to a base of the base may require | |
12949 | code, then we may need to generate code to convert to a | |
12950 | base as well. */ | |
12951 | TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) | |
12952 | |= TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype); | |
12953 | } | |
12954 | i++; | |
8ccc31eb | 12955 | } |
dbbf88d1 NS |
12956 | if (i) |
12957 | TREE_VEC_LENGTH (accesses) = TREE_VEC_LENGTH (binfos) = i; | |
12958 | else | |
12959 | BINFO_BASEACCESSES (binfo) = BINFO_BASETYPES (binfo) = NULL_TREE; | |
12960 | ||
12961 | if (i > 1) | |
12962 | { | |
12963 | TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1; | |
12964 | /* If there is more than one non-empty they cannot be at the same | |
12965 | address. */ | |
8fbc5ae7 MM |
12966 | TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1; |
12967 | } | |
dbbf88d1 NS |
12968 | } |
12969 | ||
12970 | /* Copy the base binfos, collect the virtual bases and set the | |
12971 | inheritance order chain. */ | |
12972 | copy_base_binfos (TYPE_BINFO (ref), ref, NULL_TREE); | |
12973 | CLASSTYPE_VBASECLASSES (ref) = nreverse (CLASSTYPE_VBASECLASSES (ref)); | |
12974 | ||
12975 | /* Unmark all the types. */ | |
12976 | while (i--) | |
12977 | { | |
12978 | tree basetype = BINFO_TYPE (BINFO_BASETYPE (TYPE_BINFO (ref), i)); | |
12979 | ||
12980 | CLEAR_CLASSTYPE_MARKED (basetype); | |
8fbc5ae7 MM |
12981 | if (CLASS_TYPE_P (basetype)) |
12982 | { | |
dbbf88d1 NS |
12983 | TREE_VIA_VIRTUAL (TYPE_BINFO (basetype)) = 0; |
12984 | BINFO_DEPENDENT_BASE_P (TYPE_BINFO (basetype)) = 0; | |
8fbc5ae7 | 12985 | } |
7ddedda4 | 12986 | } |
8ccc31eb | 12987 | CLEAR_CLASSTYPE_MARKED (ref); |
8ccc31eb | 12988 | } |
68642fb6 | 12989 | |
8d08fdba | 12990 | \f |
8d08fdba MS |
12991 | /* Begin compiling the definition of an enumeration type. |
12992 | NAME is its name (or null if anonymous). | |
12993 | Returns the type object, as yet incomplete. | |
12994 | Also records info about it so that build_enumerator | |
12995 | may be used to declare the individual values as they are read. */ | |
12996 | ||
12997 | tree | |
11f6b451 | 12998 | start_enum (tree name) |
8d08fdba MS |
12999 | { |
13000 | register tree enumtype = NULL_TREE; | |
e2500fed | 13001 | struct cp_binding_level *b = current_binding_level; |
8d08fdba MS |
13002 | |
13003 | /* If this is the real definition for a previous forward reference, | |
13004 | fill in the contents in the same object that used to be the | |
13005 | forward reference. */ | |
13006 | ||
13007 | if (name != NULL_TREE) | |
13008 | enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1); | |
13009 | ||
13010 | if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE) | |
30ff8252 | 13011 | { |
33bd39a2 | 13012 | error ("multiple definition of `%#T'", enumtype); |
30ff8252 | 13013 | cp_error_at ("previous definition here", enumtype); |
58595203 MM |
13014 | /* Clear out TYPE_VALUES, and start again. */ |
13015 | TYPE_VALUES (enumtype) = NULL_TREE; | |
30ff8252 | 13016 | } |
8d08fdba MS |
13017 | else |
13018 | { | |
13019 | enumtype = make_node (ENUMERAL_TYPE); | |
13020 | pushtag (name, enumtype, 0); | |
13021 | } | |
13022 | ||
8d08fdba MS |
13023 | return enumtype; |
13024 | } | |
13025 | ||
13026 | /* After processing and defining all the values of an enumeration type, | |
13027 | install their decls in the enumeration type and finish it off. | |
968b956a | 13028 | ENUMTYPE is the type object and VALUES a list of name-value pairs. */ |
8d08fdba | 13029 | |
968b956a | 13030 | void |
11f6b451 | 13031 | finish_enum (tree enumtype) |
8d08fdba | 13032 | { |
968b956a MM |
13033 | tree pair; |
13034 | tree minnode; | |
13035 | tree maxnode; | |
13036 | tree t; | |
13037 | bool unsignedp; | |
13038 | int lowprec; | |
13039 | int highprec; | |
13040 | int precision; | |
13041 | ||
13042 | /* We built up the VALUES in reverse order. */ | |
13043 | TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype)); | |
13044 | ||
f5d70cc0 JM |
13045 | /* For an enum defined in a template, just set the type of the values; |
13046 | all further processing is postponed until the template is | |
13047 | instantiated. We need to set the type so that tsubst of a CONST_DECL | |
13048 | works. */ | |
968b956a MM |
13049 | if (processing_template_decl) |
13050 | { | |
f5d70cc0 JM |
13051 | for (pair = TYPE_VALUES (enumtype); pair; pair = TREE_CHAIN (pair)) |
13052 | TREE_TYPE (TREE_VALUE (pair)) = enumtype; | |
5f261ba9 | 13053 | if (at_function_scope_p ()) |
968b956a | 13054 | add_stmt (build_min (TAG_DEFN, enumtype)); |
968b956a MM |
13055 | return; |
13056 | } | |
13057 | ||
968b956a | 13058 | if (TYPE_VALUES (enumtype)) |
8d08fdba | 13059 | { |
968b956a | 13060 | minnode = maxnode = NULL_TREE; |
5566b478 | 13061 | |
fdedded1 | 13062 | for (pair = TYPE_VALUES (enumtype); pair; pair = TREE_CHAIN (pair)) |
8d08fdba | 13063 | { |
f5d70cc0 JM |
13064 | tree decl = TREE_VALUE (pair); |
13065 | tree value = DECL_INITIAL (decl); | |
13066 | ||
13067 | /* [dcl.enum]: Following the closing brace of an enum-specifier, | |
13068 | each enumerator has the type of its enumeration. Prior to the | |
13069 | closing brace, the type of each enumerator is the type of its | |
13070 | initializing value. */ | |
13071 | TREE_TYPE (decl) = enumtype; | |
13072 | ||
13073 | /* Figure out what the minimum and maximum values of the | |
13074 | enumerators are. */ | |
13075 | if (!minnode) | |
13076 | minnode = maxnode = value; | |
13077 | else if (tree_int_cst_lt (maxnode, value)) | |
13078 | maxnode = value; | |
13079 | else if (tree_int_cst_lt (value, minnode)) | |
13080 | minnode = value; | |
13081 | ||
13082 | /* Set the TREE_TYPE for the values as well. That's so that when | |
13083 | we call decl_constant_value we get an entity of the right type | |
13084 | (but with the constant value). But first make a copy so we | |
13085 | don't clobber shared INTEGER_CSTs. */ | |
13086 | if (TREE_TYPE (value) != enumtype) | |
fdedded1 | 13087 | { |
f5d70cc0 JM |
13088 | value = DECL_INITIAL (decl) = copy_node (value); |
13089 | TREE_TYPE (value) = enumtype; | |
fdedded1 | 13090 | } |
ed44da02 | 13091 | |
f5d70cc0 JM |
13092 | /* In addition, transform the TYPE_VALUES list to contain the |
13093 | values, rather than the CONST_DECLs for them. */ | |
13094 | TREE_VALUE (pair) = value; | |
8d08fdba MS |
13095 | } |
13096 | } | |
f376e137 | 13097 | else |
968b956a MM |
13098 | minnode = maxnode = integer_zero_node; |
13099 | ||
13100 | /* Compute the number of bits require to represent all values of the | |
13101 | enumeration. We must do this before the type of MINNODE and | |
13102 | MAXNODE are transformed, since min_precision relies on the | |
13103 | TREE_TYPE of the value it is passed. */ | |
13104 | unsignedp = tree_int_cst_sgn (minnode) >= 0; | |
13105 | lowprec = min_precision (minnode, unsignedp); | |
13106 | highprec = min_precision (maxnode, unsignedp); | |
13107 | precision = MAX (lowprec, highprec); | |
13108 | ||
968b956a MM |
13109 | /* Set TYPE_MIN_VALUE and TYPE_MAX_VALUE according to `precision'. */ |
13110 | TYPE_SIZE (enumtype) = NULL_TREE; | |
13111 | TYPE_PRECISION (enumtype) = precision; | |
13112 | if (unsignedp) | |
13113 | fixup_unsigned_type (enumtype); | |
cbf882af | 13114 | else |
968b956a | 13115 | fixup_signed_type (enumtype); |
8d08fdba | 13116 | |
219670f1 BE |
13117 | if (flag_short_enums || (precision > TYPE_PRECISION (integer_type_node))) |
13118 | /* Use the width of the narrowest normal C type which is wide | |
13119 | enough. */ | |
b0c48229 | 13120 | TYPE_PRECISION (enumtype) = TYPE_PRECISION (c_common_type_for_size |
219670f1 | 13121 | (precision, 1)); |
968b956a MM |
13122 | else |
13123 | TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node); | |
68642fb6 | 13124 | |
968b956a MM |
13125 | TYPE_SIZE (enumtype) = NULL_TREE; |
13126 | layout_type (enumtype); | |
8d08fdba | 13127 | |
968b956a MM |
13128 | /* Fix up all variant types of this enum type. */ |
13129 | for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t)) | |
13130 | { | |
13131 | TYPE_VALUES (t) = TYPE_VALUES (enumtype); | |
13132 | TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (enumtype); | |
13133 | TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (enumtype); | |
13134 | TYPE_SIZE (t) = TYPE_SIZE (enumtype); | |
13135 | TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (enumtype); | |
13136 | TYPE_MODE (t) = TYPE_MODE (enumtype); | |
13137 | TYPE_PRECISION (t) = TYPE_PRECISION (enumtype); | |
13138 | TYPE_ALIGN (t) = TYPE_ALIGN (enumtype); | |
13139 | TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (enumtype); | |
13140 | TREE_UNSIGNED (t) = TREE_UNSIGNED (enumtype); | |
cbf882af MM |
13141 | } |
13142 | ||
968b956a MM |
13143 | /* Finish debugging output for this type. */ |
13144 | rest_of_type_compilation (enumtype, namespace_bindings_p ()); | |
8d08fdba MS |
13145 | } |
13146 | ||
079e1098 | 13147 | /* Build and install a CONST_DECL for an enumeration constant of the |
58595203 | 13148 | enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided. |
8d08fdba MS |
13149 | Assignment of sequential values by default is handled here. */ |
13150 | ||
58595203 | 13151 | void |
11f6b451 | 13152 | build_enumerator (tree name, tree value, tree enumtype) |
8d08fdba | 13153 | { |
58595203 | 13154 | tree decl; |
e8bd800e | 13155 | tree context; |
58595203 | 13156 | tree type; |
8d08fdba MS |
13157 | |
13158 | /* Remove no-op casts from the value. */ | |
13159 | if (value) | |
13160 | STRIP_TYPE_NOPS (value); | |
13161 | ||
58595203 MM |
13162 | if (! processing_template_decl) |
13163 | { | |
13164 | /* Validate and default VALUE. */ | |
13165 | if (value != NULL_TREE) | |
13166 | { | |
fc611ce0 | 13167 | value = decl_constant_value (value); |
58595203 MM |
13168 | |
13169 | if (TREE_CODE (value) == INTEGER_CST) | |
13170 | { | |
13171 | value = default_conversion (value); | |
13172 | constant_expression_warning (value); | |
13173 | } | |
13174 | else | |
13175 | { | |
33bd39a2 | 13176 | error ("enumerator value for `%D' not integer constant", name); |
58595203 MM |
13177 | value = NULL_TREE; |
13178 | } | |
13179 | } | |
13180 | ||
13181 | /* Default based on previous value. */ | |
6a540f3c | 13182 | if (value == NULL_TREE) |
58595203 MM |
13183 | { |
13184 | tree prev_value; | |
13185 | ||
13186 | if (TYPE_VALUES (enumtype)) | |
13187 | { | |
a1c65f9f | 13188 | /* The next value is the previous value ... */ |
58595203 MM |
13189 | prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype))); |
13190 | /* ... plus one. */ | |
ab76ca54 MM |
13191 | value = cp_build_binary_op (PLUS_EXPR, |
13192 | prev_value, | |
13193 | integer_one_node); | |
68642fb6 | 13194 | |
58595203 | 13195 | if (tree_int_cst_lt (value, prev_value)) |
33bd39a2 | 13196 | error ("overflow in enumeration values at `%D'", name); |
58595203 MM |
13197 | } |
13198 | else | |
13199 | value = integer_zero_node; | |
13200 | } | |
13201 | ||
13202 | /* Remove no-op casts from the value. */ | |
f5d70cc0 | 13203 | STRIP_TYPE_NOPS (value); |
58595203 | 13204 | } |
8d08fdba | 13205 | |
8d08fdba | 13206 | /* C++ associates enums with global, function, or class declarations. */ |
58595203 MM |
13207 | context = current_scope (); |
13208 | ||
13209 | /* Build the actual enumeration constant. Note that the enumeration | |
13210 | constants have the type of their initializers until the | |
13211 | enumeration is complete: | |
13212 | ||
13213 | [ dcl.enum ] | |
13214 | ||
13215 | Following the closing brace of an enum-specifier, each enumer- | |
13216 | ator has the type of its enumeration. Prior to the closing | |
13217 | brace, the type of each enumerator is the type of its | |
13218 | initializing value. | |
13219 | ||
13220 | In finish_enum we will reset the type. Of course, if we're | |
a1c65f9f | 13221 | processing a template, there may be no value. */ |
58595203 MM |
13222 | type = value ? TREE_TYPE (value) : NULL_TREE; |
13223 | ||
13224 | if (context && context == current_class_type) | |
13225 | /* This enum declaration is local to the class. We need the full | |
8f17b5c5 | 13226 | lang_decl so that we can record DECL_CLASS_CONTEXT, for example. */ |
58595203 MM |
13227 | decl = build_lang_decl (CONST_DECL, name, type); |
13228 | else | |
13229 | /* It's a global enum, or it's local to a function. (Note local to | |
e8bd800e | 13230 | a function could mean local to a class method. */ |
58595203 | 13231 | decl = build_decl (CONST_DECL, name, type); |
e8bd800e | 13232 | |
58595203 | 13233 | DECL_CONTEXT (decl) = FROB_CONTEXT (context); |
3e4a3562 | 13234 | TREE_CONSTANT (decl) = TREE_READONLY (decl) = 1; |
58595203 | 13235 | DECL_INITIAL (decl) = value; |
e8bd800e | 13236 | |
58595203 MM |
13237 | if (context && context == current_class_type) |
13238 | /* In something like `struct S { enum E { i = 7 }; };' we put `i' | |
8f032717 MM |
13239 | on the TYPE_FIELDS list for `S'. (That's so that you can say |
13240 | things like `S::i' later.) */ | |
58595203 MM |
13241 | finish_member_declaration (decl); |
13242 | else | |
9780c24f | 13243 | pushdecl (decl); |
58595203 MM |
13244 | |
13245 | /* Add this enumeration constant to the list for this type. */ | |
13246 | TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype)); | |
8d08fdba MS |
13247 | } |
13248 | ||
8d08fdba | 13249 | \f |
a8f73d4b MM |
13250 | /* We're defining DECL. Make sure that it's type is OK. */ |
13251 | ||
13252 | static void | |
11f6b451 | 13253 | check_function_type (tree decl, tree current_function_parms) |
a8f73d4b MM |
13254 | { |
13255 | tree fntype = TREE_TYPE (decl); | |
d0f062fb | 13256 | tree return_type = complete_type (TREE_TYPE (fntype)); |
a8f73d4b MM |
13257 | |
13258 | /* In a function definition, arg types must be complete. */ | |
13259 | require_complete_types_for_parms (current_function_parms); | |
13260 | ||
d0f062fb | 13261 | if (!COMPLETE_OR_VOID_TYPE_P (return_type)) |
a8f73d4b | 13262 | { |
33bd39a2 | 13263 | error ("return type `%#T' is incomplete", TREE_TYPE (fntype)); |
a8f73d4b MM |
13264 | |
13265 | /* Make it return void instead, but don't change the | |
13266 | type of the DECL_RESULT, in case we have a named return value. */ | |
13267 | if (TREE_CODE (fntype) == METHOD_TYPE) | |
13268 | { | |
13269 | tree ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype))); | |
13270 | TREE_TYPE (decl) | |
13271 | = build_cplus_method_type (ctype, | |
13272 | void_type_node, | |
13273 | FUNCTION_ARG_CHAIN (decl)); | |
13274 | } | |
13275 | else | |
13276 | TREE_TYPE (decl) | |
13277 | = build_function_type (void_type_node, | |
13278 | TYPE_ARG_TYPES (TREE_TYPE (decl))); | |
68642fb6 | 13279 | TREE_TYPE (decl) |
a8f73d4b MM |
13280 | = build_exception_variant (fntype, |
13281 | TYPE_RAISES_EXCEPTIONS (fntype)); | |
13282 | } | |
13283 | else | |
13284 | abstract_virtuals_error (decl, TREE_TYPE (fntype)); | |
13285 | } | |
13286 | ||
8d08fdba MS |
13287 | /* Create the FUNCTION_DECL for a function definition. |
13288 | DECLSPECS and DECLARATOR are the parts of the declaration; | |
13289 | they describe the function's name and the type it returns, | |
13290 | but twisted together in a fashion that parallels the syntax of C. | |
13291 | ||
a8f73d4b MM |
13292 | FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the |
13293 | DECLARATOR is really the DECL for the function we are about to | |
13294 | process and that DECLSPECS should be ignored), SF_INCLASS_INLINE | |
f444e36b | 13295 | indicating that the function is an inline defined in-class. |
68642fb6 | 13296 | |
8d08fdba MS |
13297 | This function creates a binding context for the function body |
13298 | as well as setting up the FUNCTION_DECL in current_function_decl. | |
13299 | ||
13300 | Returns 1 on success. If the DECLARATOR is not suitable for a function | |
13301 | (it defines a datum instead), we return 0, which tells | |
13302 | yyparse to report a parse error. | |
13303 | ||
13304 | For C++, we must first check whether that datum makes any sense. | |
13305 | For example, "class A local_a(1,2);" means that variable local_a | |
13306 | is an aggregate of type A, which should have a constructor | |
87e3dbc9 | 13307 | applied to it with the argument list [1, 2]. */ |
8d08fdba MS |
13308 | |
13309 | int | |
11f6b451 | 13310 | start_function (tree declspecs, tree declarator, tree attrs, int flags) |
8d08fdba | 13311 | { |
5566b478 | 13312 | tree decl1; |
8d08fdba MS |
13313 | tree ctype = NULL_TREE; |
13314 | tree fntype; | |
13315 | tree restype; | |
8d08fdba | 13316 | int doing_friend = 0; |
e2500fed | 13317 | struct cp_binding_level *bl; |
f444e36b | 13318 | tree current_function_parms; |
8d08fdba | 13319 | |
8d08fdba | 13320 | /* Sanity check. */ |
a1774733 | 13321 | my_friendly_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE, 160); |
8d08fdba MS |
13322 | my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161); |
13323 | ||
e92cc029 | 13324 | /* This should only be done once on the top most decl. */ |
594bb0e7 | 13325 | if (have_extern_spec) |
8d08fdba | 13326 | { |
1f8f4a0b | 13327 | declspecs = tree_cons (NULL_TREE, get_identifier ("extern"), declspecs); |
594bb0e7 | 13328 | have_extern_spec = false; |
8d08fdba MS |
13329 | } |
13330 | ||
a8f73d4b | 13331 | if (flags & SF_PRE_PARSED) |
8d08fdba MS |
13332 | { |
13333 | decl1 = declarator; | |
13334 | ||
8d08fdba MS |
13335 | fntype = TREE_TYPE (decl1); |
13336 | if (TREE_CODE (fntype) == METHOD_TYPE) | |
13337 | ctype = TYPE_METHOD_BASETYPE (fntype); | |
13338 | ||
cab1f180 ML |
13339 | /* ISO C++ 11.4/5. A friend function defined in a class is in |
13340 | the (lexical) scope of the class in which it is defined. */ | |
8d08fdba MS |
13341 | if (!ctype && DECL_FRIEND_P (decl1)) |
13342 | { | |
4f1c5b7d | 13343 | ctype = DECL_FRIEND_CONTEXT (decl1); |
8d08fdba MS |
13344 | |
13345 | /* CTYPE could be null here if we're dealing with a template; | |
13346 | for example, `inline friend float foo()' inside a template | |
13347 | will have no CTYPE set. */ | |
13348 | if (ctype && TREE_CODE (ctype) != RECORD_TYPE) | |
13349 | ctype = NULL_TREE; | |
13350 | else | |
13351 | doing_friend = 1; | |
13352 | } | |
13353 | ||
5566b478 | 13354 | last_function_parms = DECL_ARGUMENTS (decl1); |
8d08fdba MS |
13355 | } |
13356 | else | |
13357 | { | |
91d231cb | 13358 | decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL); |
8d08fdba MS |
13359 | /* If the declarator is not suitable for a function definition, |
13360 | cause a syntax error. */ | |
e89a6075 JM |
13361 | if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL) |
13362 | return 0; | |
13363 | ||
13364 | cplus_decl_attributes (&decl1, attrs, 0); | |
8d08fdba | 13365 | |
ecb0eece | 13366 | /* If #pragma weak was used, mark the decl weak now. */ |
5f52c0e0 | 13367 | if (global_scope_p (current_binding_level)) |
ecb0eece RH |
13368 | maybe_apply_pragma_weak (decl1); |
13369 | ||
8d08fdba MS |
13370 | fntype = TREE_TYPE (decl1); |
13371 | ||
13372 | restype = TREE_TYPE (fntype); | |
7ddedda4 | 13373 | if (CLASS_TYPE_P (restype) && !CLASSTYPE_GOT_SEMICOLON (restype)) |
8d08fdba | 13374 | { |
33bd39a2 | 13375 | error ("semicolon missing after declaration of `%#T'", restype); |
051e6fd7 | 13376 | shadow_tag (build_tree_list (NULL_TREE, restype)); |
8d08fdba MS |
13377 | CLASSTYPE_GOT_SEMICOLON (restype) = 1; |
13378 | if (TREE_CODE (fntype) == FUNCTION_TYPE) | |
13379 | fntype = build_function_type (integer_type_node, | |
13380 | TYPE_ARG_TYPES (fntype)); | |
13381 | else | |
13382 | fntype = build_cplus_method_type (build_type_variant (TYPE_METHOD_BASETYPE (fntype), TREE_READONLY (decl1), TREE_SIDE_EFFECTS (decl1)), | |
13383 | integer_type_node, | |
13384 | TYPE_ARG_TYPES (fntype)); | |
13385 | TREE_TYPE (decl1) = fntype; | |
13386 | } | |
13387 | ||
13388 | if (TREE_CODE (fntype) == METHOD_TYPE) | |
13389 | ctype = TYPE_METHOD_BASETYPE (fntype); | |
35680744 | 13390 | else if (DECL_MAIN_P (decl1)) |
8d08fdba | 13391 | { |
4b41d9eb KL |
13392 | /* If this doesn't return integer_type, or a typedef to |
13393 | integer_type, complain. */ | |
13394 | if (!same_type_p (TREE_TYPE (TREE_TYPE (decl1)), integer_type_node)) | |
8d08fdba | 13395 | { |
a28e3c7f | 13396 | if (pedantic || warn_return_type) |
8251199e | 13397 | pedwarn ("return type for `main' changed to `int'"); |
8d08fdba MS |
13398 | TREE_TYPE (decl1) = fntype = default_function_type; |
13399 | } | |
8d08fdba MS |
13400 | } |
13401 | } | |
68642fb6 | 13402 | |
97055d5c AO |
13403 | if (DECL_DECLARED_INLINE_P (decl1) |
13404 | && lookup_attribute ("noinline", attrs)) | |
456d8864 GDR |
13405 | warning ("%Hinline function '%D' given attribute noinline", |
13406 | &DECL_SOURCE_LOCATION (decl1), decl1); | |
97055d5c | 13407 | |
5f6eeeb3 NS |
13408 | if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1)) |
13409 | /* This is a constructor, we must ensure that any default args | |
13410 | introduced by this definition are propagated to the clones | |
13411 | now. The clones are used directly in overload resolution. */ | |
13412 | adjust_clone_args (decl1); | |
13413 | ||
b35d4555 MM |
13414 | /* Sometimes we don't notice that a function is a static member, and |
13415 | build a METHOD_TYPE for it. Fix that up now. */ | |
13416 | if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1) | |
13417 | && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE) | |
13418 | { | |
3afb32a4 | 13419 | revert_static_member_fn (decl1); |
b35d4555 MM |
13420 | last_function_parms = TREE_CHAIN (last_function_parms); |
13421 | ctype = NULL_TREE; | |
13422 | } | |
8d08fdba MS |
13423 | |
13424 | /* Warn if function was previously implicitly declared | |
13425 | (but not if we warned then). */ | |
13426 | if (! warn_implicit | |
13427 | && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE) | |
8251199e | 13428 | cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1))); |
8d08fdba | 13429 | |
f181d4ae MM |
13430 | /* Set up current_class_type, and enter the scope of the class, if |
13431 | appropriate. */ | |
13432 | if (ctype) | |
14d22dd6 | 13433 | push_nested_class (ctype); |
f181d4ae | 13434 | else if (DECL_STATIC_FUNCTION_P (decl1)) |
14d22dd6 | 13435 | push_nested_class (DECL_CONTEXT (decl1)); |
f181d4ae MM |
13436 | |
13437 | /* Now that we have entered the scope of the class, we must restore | |
13438 | the bindings for any template parameters surrounding DECL1, if it | |
13439 | is an inline member template. (Order is important; consider the | |
13440 | case where a template parameter has the same name as a field of | |
13441 | the class.) It is not until after this point that | |
13442 | PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */ | |
a8f73d4b | 13443 | if (flags & SF_INCLASS_INLINE) |
f181d4ae MM |
13444 | maybe_begin_member_template_processing (decl1); |
13445 | ||
56cb9733 | 13446 | /* Effective C++ rule 15. */ |
9188c363 | 13447 | if (warn_ecpp |
596ea4e5 | 13448 | && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR |
9188c363 | 13449 | && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE) |
33bd39a2 | 13450 | warning ("`operator=' should return a reference to `*this'"); |
9188c363 MM |
13451 | |
13452 | /* Make the init_value nonzero so pushdecl knows this is not tentative. | |
13453 | error_mark_node is replaced below (in poplevel) with the BLOCK. */ | |
b35d4555 MM |
13454 | if (!DECL_INITIAL (decl1)) |
13455 | DECL_INITIAL (decl1) = error_mark_node; | |
9188c363 | 13456 | |
9188c363 MM |
13457 | /* This function exists in static storage. |
13458 | (This does not mean `static' in the C sense!) */ | |
13459 | TREE_STATIC (decl1) = 1; | |
13460 | ||
13461 | /* We must call push_template_decl after current_class_type is set | |
13462 | up. (If we are processing inline definitions after exiting a | |
13463 | class scope, current_class_type will be NULL_TREE until set above | |
13464 | by push_nested_class.) */ | |
13465 | if (processing_template_decl) | |
13466 | decl1 = push_template_decl (decl1); | |
13467 | ||
f181d4ae | 13468 | /* We are now in the scope of the function being defined. */ |
8d08fdba | 13469 | current_function_decl = decl1; |
f181d4ae | 13470 | |
5566b478 MS |
13471 | /* Save the parm names or decls from this function's declarator |
13472 | where store_parm_decls will find them. */ | |
13473 | current_function_parms = last_function_parms; | |
8d08fdba | 13474 | |
a8f73d4b MM |
13475 | /* Make sure the parameter and return types are reasonable. When |
13476 | you declare a function, these types can be incomplete, but they | |
13477 | must be complete when you define the function. */ | |
5156628f | 13478 | if (! processing_template_decl) |
f444e36b | 13479 | check_function_type (decl1, current_function_parms); |
f181d4ae | 13480 | |
a8f73d4b MM |
13481 | /* Build the return declaration for the function. */ |
13482 | restype = TREE_TYPE (fntype); | |
e89a6075 JM |
13483 | /* Promote the value to int before returning it. */ |
13484 | if (c_promoting_integer_type_p (restype)) | |
13485 | restype = type_promotes_to (restype); | |
13486 | if (DECL_RESULT (decl1) == NULL_TREE) | |
a8f73d4b | 13487 | { |
e89a6075 JM |
13488 | DECL_RESULT (decl1) |
13489 | = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype)); | |
13490 | c_apply_type_quals_to_decl (cp_type_quals (restype), | |
13491 | DECL_RESULT (decl1)); | |
5566b478 | 13492 | } |
a8f73d4b MM |
13493 | |
13494 | /* Initialize RTL machinery. We cannot do this until | |
13495 | CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this | |
13496 | even when processing a template; this is how we get | |
c00996a3 JM |
13497 | CFUN set up, and our per-function variables initialized. |
13498 | FIXME factor out the non-RTL stuff. */ | |
a8f73d4b | 13499 | bl = current_binding_level; |
ee6b0296 | 13500 | init_function_start (decl1); |
ff955512 | 13501 | current_binding_level = bl; |
a8f73d4b MM |
13502 | |
13503 | /* Even though we're inside a function body, we still don't want to | |
13504 | call expand_expr to calculate the size of a variable-sized array. | |
13505 | We haven't necessarily assigned RTL to all variables yet, so it's | |
13506 | not safe to try to expand expressions involving them. */ | |
13507 | immediate_size_expand = 0; | |
01d939e8 | 13508 | cfun->x_dont_save_pending_sizes_p = 1; |
a8f73d4b | 13509 | |
f444e36b MM |
13510 | /* Start the statement-tree, start the tree now. */ |
13511 | begin_stmt_tree (&DECL_SAVED_TREE (decl1)); | |
6f80451c | 13512 | |
d1a74aa7 JM |
13513 | /* Don't double-count statements in templates. */ |
13514 | DECL_NUM_STMTS (decl1) = 0; | |
13515 | ||
a8f73d4b | 13516 | /* Let the user know we're compiling this function. */ |
ea11ca7e | 13517 | announce_function (decl1); |
b7484fbe | 13518 | |
878cd289 MS |
13519 | /* Record the decl so that the function name is defined. |
13520 | If we already have a decl for this name, and it is a FUNCTION_DECL, | |
13521 | use the old decl. */ | |
a8f73d4b | 13522 | if (!processing_template_decl && !(flags & SF_PRE_PARSED)) |
878cd289 | 13523 | { |
75650646 | 13524 | /* A specialization is not used to guide overload resolution. */ |
2228d450 | 13525 | if (!DECL_TEMPLATE_SPECIALIZATION (decl1) |
959d8796 | 13526 | && ! DECL_FUNCTION_MEMBER_P (decl1)) |
75650646 | 13527 | decl1 = pushdecl (decl1); |
2c73f9f5 | 13528 | else |
b7698cf0 | 13529 | { |
a1c65f9f | 13530 | /* We need to set the DECL_CONTEXT. */ |
b7698cf0 JM |
13531 | if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1)) |
13532 | DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1)); | |
13533 | /* And make sure we have enough default args. */ | |
13534 | check_default_args (decl1); | |
13535 | } | |
878cd289 MS |
13536 | fntype = TREE_TYPE (decl1); |
13537 | } | |
5566b478 | 13538 | |
a8f73d4b | 13539 | /* Reset these in case the call to pushdecl changed them. */ |
5566b478 | 13540 | current_function_decl = decl1; |
01d939e8 | 13541 | cfun->decl = decl1; |
878cd289 | 13542 | |
78c120b5 MM |
13543 | /* If we are (erroneously) defining a function that we have already |
13544 | defined before, wipe out what we knew before. */ | |
e2500fed GK |
13545 | if (!DECL_PENDING_INLINE_P (decl1)) |
13546 | DECL_SAVED_FUNCTION_DATA (decl1) = NULL; | |
b35d4555 | 13547 | |
f444e36b | 13548 | if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1)) |
b35d4555 MM |
13549 | { |
13550 | /* We know that this was set up by `grokclassfn'. We do not | |
13551 | wait until `store_parm_decls', since evil parse errors may | |
13552 | never get us to that point. Here we keep the consistency | |
13553 | between `current_class_type' and `current_class_ptr'. */ | |
13554 | tree t = DECL_ARGUMENTS (decl1); | |
68642fb6 UD |
13555 | |
13556 | my_friendly_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL, | |
b35d4555 MM |
13557 | 162); |
13558 | my_friendly_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE, | |
13559 | 19990811); | |
68642fb6 UD |
13560 | |
13561 | cp_function_chain->x_current_class_ref | |
3e411c3f | 13562 | = build_indirect_ref (t, NULL); |
b35d4555 MM |
13563 | cp_function_chain->x_current_class_ptr = t; |
13564 | ||
018fc244 MM |
13565 | /* Constructors and destructors need to know whether they're "in |
13566 | charge" of initializing virtual base classes. */ | |
e0fff4b3 | 13567 | t = TREE_CHAIN (t); |
454fa7a7 | 13568 | if (DECL_HAS_IN_CHARGE_PARM_P (decl1)) |
e0fff4b3 JM |
13569 | { |
13570 | current_in_charge_parm = t; | |
13571 | t = TREE_CHAIN (t); | |
13572 | } | |
13573 | if (DECL_HAS_VTT_PARM_P (decl1)) | |
13574 | { | |
13575 | if (DECL_NAME (t) != vtt_parm_identifier) | |
13576 | abort (); | |
13577 | current_vtt_parm = t; | |
13578 | } | |
b35d4555 MM |
13579 | } |
13580 | ||
db5ae43f | 13581 | if (DECL_INTERFACE_KNOWN (decl1)) |
faae18ab | 13582 | { |
4f1c5b7d | 13583 | tree ctx = decl_function_context (decl1); |
86052cc3 | 13584 | |
faae18ab MS |
13585 | if (DECL_NOT_REALLY_EXTERN (decl1)) |
13586 | DECL_EXTERNAL (decl1) = 0; | |
86052cc3 | 13587 | |
79065db2 | 13588 | if (ctx != NULL_TREE && DECL_DECLARED_INLINE_P (ctx) |
86052cc3 JM |
13589 | && TREE_PUBLIC (ctx)) |
13590 | /* This is a function in a local class in an extern inline | |
13591 | function. */ | |
13592 | comdat_linkage (decl1); | |
faae18ab | 13593 | } |
8d08fdba MS |
13594 | /* If this function belongs to an interface, it is public. |
13595 | If it belongs to someone else's interface, it is also external. | |
1f901793 | 13596 | This only affects inlines and template instantiations. */ |
5566b478 MS |
13597 | else if (interface_unknown == 0 |
13598 | && (! DECL_TEMPLATE_INSTANTIATION (decl1) | |
d11ad92e | 13599 | || flag_alt_external_templates)) |
8d08fdba | 13600 | { |
79065db2 MM |
13601 | if (DECL_DECLARED_INLINE_P (decl1) |
13602 | || DECL_TEMPLATE_INSTANTIATION (decl1) | |
5156628f | 13603 | || processing_template_decl) |
1f901793 JM |
13604 | { |
13605 | DECL_EXTERNAL (decl1) | |
13606 | = (interface_only | |
79065db2 MM |
13607 | || (DECL_DECLARED_INLINE_P (decl1) |
13608 | && ! flag_implement_inlines | |
9c73ec84 | 13609 | && !DECL_VINDEX (decl1))); |
1f901793 JM |
13610 | |
13611 | /* For WIN32 we also want to put these in linkonce sections. */ | |
13612 | maybe_make_one_only (decl1); | |
13613 | } | |
db5ae43f | 13614 | else |
893de33c | 13615 | DECL_EXTERNAL (decl1) = 0; |
e8abc66f | 13616 | DECL_NOT_REALLY_EXTERN (decl1) = 0; |
db5ae43f | 13617 | DECL_INTERFACE_KNOWN (decl1) = 1; |
8d08fdba | 13618 | } |
c16c47fb JM |
13619 | else if (interface_unknown && interface_only |
13620 | && (! DECL_TEMPLATE_INSTANTIATION (decl1) | |
13621 | || flag_alt_external_templates)) | |
13622 | { | |
13623 | /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma | |
13624 | interface, we will have interface_only set but not | |
13625 | interface_known. In that case, we don't want to use the normal | |
13626 | heuristics because someone will supply a #pragma implementation | |
13627 | elsewhere, and deducing it here would produce a conflict. */ | |
13628 | comdat_linkage (decl1); | |
13629 | DECL_EXTERNAL (decl1) = 0; | |
13630 | DECL_INTERFACE_KNOWN (decl1) = 1; | |
13631 | DECL_DEFER_OUTPUT (decl1) = 1; | |
13632 | } | |
8d08fdba | 13633 | else |
a0a33927 MS |
13634 | { |
13635 | /* This is a definition, not a reference. | |
b7484fbe MS |
13636 | So clear DECL_EXTERNAL. */ |
13637 | DECL_EXTERNAL (decl1) = 0; | |
faae18ab | 13638 | |
79065db2 MM |
13639 | if ((DECL_DECLARED_INLINE_P (decl1) |
13640 | || DECL_TEMPLATE_INSTANTIATION (decl1)) | |
5566b478 MS |
13641 | && ! DECL_INTERFACE_KNOWN (decl1) |
13642 | /* Don't try to defer nested functions for now. */ | |
4f1c5b7d | 13643 | && ! decl_function_context (decl1)) |
878cd289 MS |
13644 | DECL_DEFER_OUTPUT (decl1) = 1; |
13645 | else | |
893de33c | 13646 | DECL_INTERFACE_KNOWN (decl1) = 1; |
db5ae43f | 13647 | } |
a9aedbc2 | 13648 | |
f444e36b MM |
13649 | pushlevel (0); |
13650 | current_binding_level->parm_flag = 1; | |
8d08fdba | 13651 | |
5566b478 MS |
13652 | ++function_depth; |
13653 | ||
0d9eb3ba | 13654 | if (DECL_DESTRUCTOR_P (decl1)) |
46e8c075 MM |
13655 | { |
13656 | dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE); | |
13657 | DECL_CONTEXT (dtor_label) = current_function_decl; | |
13658 | } | |
8d08fdba | 13659 | |
0ba8a114 NS |
13660 | start_fname_decls (); |
13661 | ||
f444e36b MM |
13662 | store_parm_decls (current_function_parms); |
13663 | ||
8d08fdba MS |
13664 | return 1; |
13665 | } | |
13666 | \f | |
13667 | /* Store the parameter declarations into the current function declaration. | |
13668 | This is called after parsing the parameter declarations, before | |
13669 | digesting the body of the function. | |
13670 | ||
13671 | Also install to binding contour return value identifier, if any. */ | |
13672 | ||
f444e36b | 13673 | static void |
11f6b451 | 13674 | store_parm_decls (tree current_function_parms) |
8d08fdba MS |
13675 | { |
13676 | register tree fndecl = current_function_decl; | |
13677 | register tree parm; | |
8d08fdba | 13678 | |
8d08fdba MS |
13679 | /* This is a chain of any other decls that came in among the parm |
13680 | declarations. If a parm is declared with enum {foo, bar} x; | |
13681 | then CONST_DECLs for foo and bar are put here. */ | |
13682 | tree nonparms = NULL_TREE; | |
13683 | ||
b35d4555 | 13684 | if (current_function_parms) |
8d08fdba MS |
13685 | { |
13686 | /* This case is when the function was defined with an ANSI prototype. | |
13687 | The parms already have decls, so we need not do anything here | |
13688 | except record them as in effect | |
13689 | and complain if any redundant old-style parm decls were written. */ | |
13690 | ||
b35d4555 MM |
13691 | tree specparms = current_function_parms; |
13692 | tree next; | |
13693 | ||
f444e36b | 13694 | /* Must clear this because it might contain TYPE_DECLs declared |
b35d4555 | 13695 | at class level. */ |
f444e36b | 13696 | storedecls (NULL_TREE); |
8d08fdba | 13697 | |
f444e36b | 13698 | /* If we're doing semantic analysis, then we'll call pushdecl |
b35d4555 MM |
13699 | for each of these. We must do them in reverse order so that |
13700 | they end in the correct forward order. */ | |
f444e36b | 13701 | specparms = nreverse (specparms); |
5566b478 | 13702 | |
b35d4555 | 13703 | for (parm = specparms; parm; parm = next) |
8d08fdba MS |
13704 | { |
13705 | next = TREE_CHAIN (parm); | |
13706 | if (TREE_CODE (parm) == PARM_DECL) | |
13707 | { | |
f444e36b MM |
13708 | if (DECL_NAME (parm) == NULL_TREE |
13709 | || TREE_CODE (parm) != VOID_TYPE) | |
13710 | pushdecl (parm); | |
13711 | else | |
33bd39a2 | 13712 | error ("parameter `%D' declared void", parm); |
8d08fdba MS |
13713 | } |
13714 | else | |
13715 | { | |
13716 | /* If we find an enum constant or a type tag, | |
13717 | put it aside for the moment. */ | |
13718 | TREE_CHAIN (parm) = NULL_TREE; | |
13719 | nonparms = chainon (nonparms, parm); | |
13720 | } | |
13721 | } | |
13722 | ||
f444e36b MM |
13723 | /* Get the decls in their original chain order and record in the |
13724 | function. This is all and only the PARM_DECLs that were | |
13725 | pushed into scope by the loop above. */ | |
13726 | DECL_ARGUMENTS (fndecl) = getdecls (); | |
8d08fdba MS |
13727 | } |
13728 | else | |
13729 | DECL_ARGUMENTS (fndecl) = NULL_TREE; | |
13730 | ||
13731 | /* Now store the final chain of decls for the arguments | |
13732 | as the decl-chain of the current lexical scope. | |
13733 | Put the enumerators in as well, at the front so that | |
13734 | DECL_ARGUMENTS is not modified. */ | |
f444e36b | 13735 | storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl))); |
eb448459 | 13736 | |
b35d4555 | 13737 | /* Do the starting of the exception specifications, if we have any. */ |
68642fb6 | 13738 | if (flag_exceptions && !processing_template_decl |
1660cb3a | 13739 | && flag_enforce_eh_specs |
b35d4555 | 13740 | && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl))) |
52a11cbf | 13741 | current_eh_spec_block = begin_eh_spec_block (); |
8d08fdba MS |
13742 | } |
13743 | ||
8d08fdba | 13744 | \f |
59026e79 MM |
13745 | /* We have finished doing semantic analysis on DECL, but have not yet |
13746 | generated RTL for its body. Save away our current state, so that | |
13747 | when we want to generate RTL later we know what to do. */ | |
13748 | ||
13749 | static void | |
11f6b451 | 13750 | save_function_data (tree decl) |
59026e79 | 13751 | { |
e2500fed | 13752 | struct language_function *f; |
59026e79 MM |
13753 | |
13754 | /* Save the language-specific per-function data so that we can | |
13755 | get it back when we really expand this function. */ | |
13756 | my_friendly_assert (!DECL_PENDING_INLINE_P (decl), | |
13757 | 19990908); | |
68642fb6 | 13758 | |
59026e79 | 13759 | /* Make a copy. */ |
e2500fed GK |
13760 | f = ((struct language_function *) |
13761 | ggc_alloc (sizeof (struct language_function))); | |
13762 | memcpy (f, cp_function_chain, sizeof (struct language_function)); | |
59026e79 MM |
13763 | DECL_SAVED_FUNCTION_DATA (decl) = f; |
13764 | ||
13765 | /* Clear out the bits we don't need. */ | |
ae499cce MM |
13766 | f->base.x_stmt_tree.x_last_stmt = NULL_TREE; |
13767 | f->base.x_stmt_tree.x_last_expr_type = NULL_TREE; | |
59026e79 MM |
13768 | f->x_named_label_uses = NULL; |
13769 | f->bindings = NULL; | |
8e4ce833 | 13770 | f->x_local_names = NULL; |
59026e79 MM |
13771 | |
13772 | /* When we get back here again, we will be expanding. */ | |
13773 | f->x_expanding_p = 1; | |
914653a2 MM |
13774 | |
13775 | /* If we've already decided that we cannot inline this function, we | |
13776 | must remember that fact when we actually go to expand the | |
13777 | function. */ | |
acc72c37 MM |
13778 | if (current_function_cannot_inline) |
13779 | { | |
13780 | f->cannot_inline = current_function_cannot_inline; | |
13781 | DECL_INLINE (decl) = 0; | |
13782 | } | |
59026e79 MM |
13783 | } |
13784 | ||
cdd2559c JM |
13785 | /* Add a note to mark the beginning of the main body of the constructor. |
13786 | This is used to set up the data structures for the cleanup regions for | |
13787 | fully-constructed bases and members. */ | |
13788 | ||
13789 | static void | |
11f6b451 | 13790 | begin_constructor_body (void) |
cdd2559c | 13791 | { |
cdd2559c JM |
13792 | } |
13793 | ||
ade3dc07 JM |
13794 | /* Add a note to mark the end of the main body of the constructor. This is |
13795 | used to end the cleanup regions for fully-constructed bases and | |
13796 | members. */ | |
efee38a9 MM |
13797 | |
13798 | static void | |
11f6b451 | 13799 | finish_constructor_body (void) |
efee38a9 | 13800 | { |
efee38a9 MM |
13801 | } |
13802 | ||
cdd2559c JM |
13803 | /* Do all the processing for the beginning of a destructor; set up the |
13804 | vtable pointers and cleanups for bases and members. */ | |
13805 | ||
13806 | static void | |
11f6b451 | 13807 | begin_destructor_body (void) |
cdd2559c JM |
13808 | { |
13809 | tree if_stmt; | |
13810 | tree compound_stmt; | |
13811 | ||
13812 | /* If the dtor is empty, and we know there is not any possible | |
13813 | way we could use any vtable entries, before they are possibly | |
13814 | set by a base class dtor, we don't have to setup the vtables, | |
13815 | as we know that any base class dtor will set up any vtables | |
13816 | it needs. We avoid MI, because one base class dtor can do a | |
13817 | virtual dispatch to an overridden function that would need to | |
13818 | have a non-related vtable set up, we cannot avoid setting up | |
13819 | vtables in that case. We could change this to see if there | |
13820 | is just one vtable. | |
13821 | ||
13822 | ??? In the destructor for a class, the vtables are set | |
13823 | appropriately for that class. There will be no non-related | |
13824 | vtables. jason 2001-12-11. */ | |
13825 | if_stmt = begin_if_stmt (); | |
13826 | ||
13827 | /* If it is not safe to avoid setting up the vtables, then | |
13828 | someone will change the condition to be boolean_true_node. | |
13829 | (Actually, for now, we do not have code to set the condition | |
13830 | appropriately, so we just assume that we always need to | |
13831 | initialize the vtables.) */ | |
13832 | finish_if_stmt_cond (boolean_true_node, if_stmt); | |
cdd2559c JM |
13833 | |
13834 | compound_stmt = begin_compound_stmt (/*has_no_scope=*/0); | |
13835 | ||
13836 | /* Make all virtual function table pointers in non-virtual base | |
13837 | classes point to CURRENT_CLASS_TYPE's virtual function | |
13838 | tables. */ | |
13839 | initialize_vtbl_ptrs (current_class_ptr); | |
13840 | ||
13841 | finish_compound_stmt (/*has_no_scope=*/0, compound_stmt); | |
13842 | finish_then_clause (if_stmt); | |
13843 | finish_if_stmt (); | |
13844 | ||
13845 | /* And insert cleanups for our bases and members so that they | |
13846 | will be properly destroyed if we throw. */ | |
13847 | push_base_cleanups (); | |
13848 | } | |
13849 | ||
ade3dc07 JM |
13850 | /* At the end of every destructor we generate code to delete the object if |
13851 | necessary. Do that now. */ | |
9bfadf57 MM |
13852 | |
13853 | static void | |
11f6b451 | 13854 | finish_destructor_body (void) |
9bfadf57 | 13855 | { |
9bfadf57 MM |
13856 | tree exprstmt; |
13857 | ||
5633b37c MM |
13858 | /* Any return from a destructor will end up here; that way all base |
13859 | and member cleanups will be run when the function returns. */ | |
13860 | add_stmt (build_stmt (LABEL_STMT, dtor_label)); | |
13861 | ||
52682a1b MM |
13862 | /* In a virtual destructor, we must call delete. */ |
13863 | if (DECL_VIRTUAL_P (current_function_decl)) | |
13864 | { | |
13865 | tree if_stmt; | |
fa72b064 | 13866 | tree virtual_size = cxx_sizeof (current_class_type); |
68642fb6 | 13867 | |
52682a1b | 13868 | /* [class.dtor] |
68642fb6 | 13869 | |
ade3dc07 JM |
13870 | At the point of definition of a virtual destructor (including |
13871 | an implicit definition), non-placement operator delete shall | |
13872 | be looked up in the scope of the destructor's class and if | |
13873 | found shall be accessible and unambiguous. */ | |
52682a1b MM |
13874 | exprstmt = build_op_delete_call |
13875 | (DELETE_EXPR, current_class_ptr, virtual_size, | |
13876 | LOOKUP_NORMAL | LOOKUP_SPECULATIVELY, NULL_TREE); | |
298d6f60 | 13877 | |
52682a1b MM |
13878 | if_stmt = begin_if_stmt (); |
13879 | finish_if_stmt_cond (build (BIT_AND_EXPR, integer_type_node, | |
13880 | current_in_charge_parm, | |
13881 | integer_one_node), | |
13882 | if_stmt); | |
13883 | finish_expr_stmt (exprstmt); | |
13884 | finish_then_clause (if_stmt); | |
13885 | finish_if_stmt (); | |
13886 | } | |
ade3dc07 | 13887 | } |
9bfadf57 | 13888 | |
ade3dc07 JM |
13889 | /* Do the necessary processing for the beginning of a function body, which |
13890 | in this case includes member-initializers, but not the catch clauses of | |
13891 | a function-try-block. Currently, this means opening a binding level | |
13892 | for the member-initializers (in a ctor) and member cleanups (in a dtor). | |
13893 | In other functions, this isn't necessary, but it doesn't hurt. */ | |
13894 | ||
13895 | tree | |
11f6b451 | 13896 | begin_function_body (void) |
ade3dc07 | 13897 | { |
cdd2559c JM |
13898 | tree stmt; |
13899 | ||
b5856475 JM |
13900 | if (processing_template_decl) |
13901 | /* Do nothing now. */; | |
13902 | else | |
13903 | /* Always keep the BLOCK node associated with the outermost pair of | |
13904 | curly braces of a function. These are needed for correct | |
13905 | operation of dwarfout.c. */ | |
13906 | keep_next_level (1); | |
13907 | ||
cdd2559c | 13908 | stmt = begin_compound_stmt (0); |
ade3dc07 | 13909 | COMPOUND_STMT_BODY_BLOCK (stmt) = 1; |
cdd2559c JM |
13910 | |
13911 | if (processing_template_decl) | |
13912 | /* Do nothing now. */; | |
13913 | else if (DECL_CONSTRUCTOR_P (current_function_decl)) | |
13914 | begin_constructor_body (); | |
13915 | else if (DECL_DESTRUCTOR_P (current_function_decl)) | |
13916 | begin_destructor_body (); | |
13917 | ||
ade3dc07 | 13918 | return stmt; |
9bfadf57 MM |
13919 | } |
13920 | ||
ade3dc07 JM |
13921 | /* Do the processing for the end of a function body. Currently, this means |
13922 | closing out the cleanups for fully-constructed bases and members, and in | |
13923 | the case of the destructor, deleting the object if desired. Again, this | |
13924 | is only meaningful for [cd]tors, since they are the only functions where | |
13925 | there is a significant distinction between the main body and any | |
13926 | function catch clauses. Handling, say, main() return semantics here | |
13927 | would be wrong, as flowing off the end of a function catch clause for | |
13928 | main() would also need to return 0. */ | |
13929 | ||
13930 | void | |
11f6b451 | 13931 | finish_function_body (tree compstmt) |
ade3dc07 | 13932 | { |
5633b37c | 13933 | /* Close the block. */ |
ade3dc07 JM |
13934 | finish_compound_stmt (0, compstmt); |
13935 | ||
13936 | if (processing_template_decl) | |
13937 | /* Do nothing now. */; | |
13938 | else if (DECL_CONSTRUCTOR_P (current_function_decl)) | |
13939 | finish_constructor_body (); | |
13940 | else if (DECL_DESTRUCTOR_P (current_function_decl)) | |
13941 | finish_destructor_body (); | |
13942 | } | |
13943 | ||
8d08fdba MS |
13944 | /* Finish up a function declaration and compile that function |
13945 | all the way to assembler language output. The free the storage | |
13946 | for the function definition. | |
13947 | ||
68642fb6 | 13948 | FLAGS is a bitwise or of the following values: |
f181d4ae MM |
13949 | 2 - INCLASS_INLINE |
13950 | We just finished processing the body of an in-class inline | |
13951 | function definition. (This processing will have taken place | |
87e3dbc9 | 13952 | after the class definition is complete.) */ |
8d08fdba | 13953 | |
4d6abc1c | 13954 | tree |
11f6b451 | 13955 | finish_function (int flags) |
8d08fdba MS |
13956 | { |
13957 | register tree fndecl = current_function_decl; | |
13958 | tree fntype, ctype = NULL_TREE; | |
f181d4ae | 13959 | int inclass_inline = (flags & 2) != 0; |
87e3dbc9 | 13960 | int nested; |
8d08fdba MS |
13961 | |
13962 | /* When we get some parse errors, we can end up without a | |
13963 | current_function_decl, so cope. */ | |
13964 | if (fndecl == NULL_TREE) | |
4d6abc1c | 13965 | return error_mark_node; |
8d08fdba | 13966 | |
9aad8f83 MA |
13967 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl) |
13968 | && DECL_VIRTUAL_P (fndecl) | |
13969 | && !processing_template_decl) | |
13970 | { | |
13971 | tree fnclass = DECL_CONTEXT (fndecl); | |
13972 | if (fndecl == CLASSTYPE_KEY_METHOD (fnclass)) | |
13973 | keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes); | |
13974 | } | |
13975 | ||
87e3dbc9 | 13976 | nested = function_depth > 1; |
8d08fdba MS |
13977 | fntype = TREE_TYPE (fndecl); |
13978 | ||
9bfadf57 MM |
13979 | /* TREE_READONLY (fndecl) = 1; |
13980 | This caused &foo to be of type ptr-to-const-function | |
13981 | which then got a warning when stored in a ptr-to-function variable. */ | |
8d08fdba | 13982 | |
f444e36b | 13983 | my_friendly_assert (building_stmt_tree (), 20000911); |
8d08fdba | 13984 | |
0ba8a114 NS |
13985 | finish_fname_decls (); |
13986 | ||
db9b2174 MM |
13987 | /* For a cloned function, we've already got all the code we need; |
13988 | there's no need to add any extra bits. */ | |
f444e36b | 13989 | if (!DECL_CLONED_FUNCTION_P (fndecl)) |
8d08fdba | 13990 | { |
ade3dc07 | 13991 | if (DECL_MAIN_P (current_function_decl)) |
efee38a9 MM |
13992 | { |
13993 | /* Make it so that `main' always returns 0 by default. */ | |
08c7ae5a | 13994 | #if VMS_TARGET |
efee38a9 MM |
13995 | finish_return_stmt (integer_one_node); |
13996 | #else | |
13997 | finish_return_stmt (integer_zero_node); | |
13998 | #endif | |
13999 | } | |
87e3dbc9 | 14000 | |
b35d4555 MM |
14001 | /* Finish dealing with exception specifiers. */ |
14002 | if (flag_exceptions && !processing_template_decl | |
1660cb3a | 14003 | && flag_enforce_eh_specs |
b35d4555 | 14004 | && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl))) |
52a11cbf RH |
14005 | finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS |
14006 | (TREE_TYPE (current_function_decl)), | |
14007 | current_eh_spec_block); | |
5566b478 | 14008 | } |
68642fb6 | 14009 | |
558475f0 | 14010 | /* If we're saving up tree structure, tie off the function now. */ |
f444e36b | 14011 | finish_stmt_tree (&DECL_SAVED_TREE (fndecl)); |
8d2733ca | 14012 | |
b2dd096b MM |
14013 | /* If this function can't throw any exceptions, remember that. */ |
14014 | if (!processing_template_decl | |
14015 | && !cp_function_chain->can_throw | |
14016 | && !flag_non_call_exceptions) | |
14017 | TREE_NOTHROW (fndecl) = 1; | |
14018 | ||
8d08fdba MS |
14019 | /* This must come after expand_function_end because cleanups might |
14020 | have declarations (from inline functions) that need to go into | |
14021 | this function's blocks. */ | |
7437519c ZW |
14022 | |
14023 | /* If the current binding level isn't the outermost binding level | |
14024 | for this function, either there is a bug, or we have experienced | |
14025 | syntax errors and the statement tree is malformed. */ | |
f444e36b | 14026 | if (current_binding_level->parm_flag != 1) |
7437519c ZW |
14027 | { |
14028 | /* Make sure we have already experienced errors. */ | |
14029 | if (errorcount == 0) | |
14030 | abort (); | |
14031 | ||
14032 | /* Throw away the broken statement tree and extra binding | |
14033 | levels. */ | |
14034 | DECL_SAVED_TREE (fndecl) = build_stmt (COMPOUND_STMT, NULL_TREE); | |
14035 | ||
14036 | while (current_binding_level->parm_flag != 1) | |
14037 | { | |
14038 | if (current_binding_level->parm_flag == 2) | |
14039 | pop_nested_class (); | |
14040 | else | |
14041 | poplevel (0, 0, 0); | |
14042 | } | |
14043 | } | |
f444e36b | 14044 | poplevel (1, 0, 1); |
8d08fdba | 14045 | |
07b2f2fd JM |
14046 | /* Set up the named return value optimization, if we can. Here, we |
14047 | eliminate the copy from the nrv into the RESULT_DECL and any cleanup | |
14048 | for the nrv. genrtl_start_function and declare_return_variable | |
14049 | handle making the nrv and RESULT_DECL share space. */ | |
14050 | if (current_function_return_value) | |
14051 | { | |
14052 | tree r = current_function_return_value; | |
35e939ae JM |
14053 | tree outer; |
14054 | ||
07b2f2fd | 14055 | if (r != error_mark_node |
35e939ae JM |
14056 | /* This is only worth doing for fns that return in memory--and |
14057 | simpler, since we don't have to worry about promoted modes. */ | |
14058 | && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl))) | |
14059 | /* Only allow this for variables declared in the outer scope of | |
14060 | the function so we know that their lifetime always ends with a | |
14061 | return; see g++.dg/opt/nrv6.C. We could be more flexible if | |
14062 | we were to do this optimization in tree-ssa. */ | |
14063 | /* Skip the artificial function body block. */ | |
14064 | && (outer = BLOCK_SUBBLOCKS (BLOCK_SUBBLOCKS (DECL_INITIAL (fndecl))), | |
14065 | chain_member (r, BLOCK_VARS (outer)))) | |
07b2f2fd | 14066 | { |
35e939ae | 14067 | |
07b2f2fd JM |
14068 | DECL_ALIGN (r) = DECL_ALIGN (DECL_RESULT (fndecl)); |
14069 | walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl), | |
14070 | nullify_returns_r, r); | |
14071 | } | |
14072 | else | |
14073 | /* Clear it so genrtl_start_function and declare_return_variable | |
14074 | know we're not optimizing. */ | |
14075 | current_function_return_value = NULL_TREE; | |
14076 | } | |
14077 | ||
a8f73d4b | 14078 | /* Remember that we were in class scope. */ |
db5ae43f | 14079 | if (current_class_name) |
a8f73d4b | 14080 | ctype = current_class_type; |
db5ae43f | 14081 | |
1caa11d3 MM |
14082 | /* Must mark the RESULT_DECL as being in this function. */ |
14083 | DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl; | |
14084 | ||
14085 | /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point | |
14086 | to the FUNCTION_DECL node itself. */ | |
14087 | BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl; | |
14088 | ||
59026e79 | 14089 | /* Save away current state, if appropriate. */ |
f444e36b | 14090 | if (!processing_template_decl) |
59026e79 MM |
14091 | save_function_data (fndecl); |
14092 | ||
95fabfd3 MM |
14093 | /* If this function calls `setjmp' it cannot be inlined. When |
14094 | `longjmp' is called it is not guaranteed to restore the value of | |
14095 | local variables that have been modified since the call to | |
14096 | `setjmp'. So, if were to inline this function into some caller | |
14097 | `c', then when we `longjmp', we might not restore all variables | |
14098 | in `c'. (It might seem, at first blush, that there's no way for | |
14099 | this function to modify local variables in `c', but their | |
14100 | addresses may have been stored somewhere accessible to this | |
14101 | function.) */ | |
f444e36b | 14102 | if (!processing_template_decl && calls_setjmp_p (fndecl)) |
95fabfd3 MM |
14103 | DECL_UNINLINABLE (fndecl) = 1; |
14104 | ||
efe49da0 | 14105 | /* Complain if there's just no return statement. */ |
46cfb101 | 14106 | if (warn_return_type |
efe49da0 | 14107 | && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE |
57d5032b | 14108 | && !current_function_returns_value && !current_function_returns_null |
efe49da0 JM |
14109 | /* Don't complain if we abort or throw. */ |
14110 | && !current_function_returns_abnormally | |
46cfb101 JM |
14111 | && !DECL_NAME (DECL_RESULT (fndecl)) |
14112 | /* Normally, with -Wreturn-type, flow will complain. Unless we're an | |
efe49da0 | 14113 | inline function, as we might never be compiled separately. */ |
efc7052d | 14114 | && (DECL_INLINE (fndecl) || processing_template_decl)) |
efe49da0 JM |
14115 | warning ("no return statement in function returning non-void"); |
14116 | ||
f444e36b MM |
14117 | /* Clear out memory we no longer need. */ |
14118 | free_after_parsing (cfun); | |
14119 | /* Since we never call rest_of_compilation, we never clear | |
14120 | CFUN. Do so explicitly. */ | |
14121 | free_after_compilation (cfun); | |
14122 | cfun = NULL; | |
a8f73d4b | 14123 | |
27631dae | 14124 | /* If this is an in-class inline definition, we may have to pop the |
a8f73d4b MM |
14125 | bindings for the template parameters that we added in |
14126 | maybe_begin_member_template_processing when start_function was | |
14127 | called. */ | |
14128 | if (inclass_inline) | |
14129 | maybe_end_member_template_processing (); | |
14130 | ||
14131 | /* Leave the scope of the class. */ | |
14132 | if (ctype) | |
14133 | pop_nested_class (); | |
5566b478 MS |
14134 | |
14135 | --function_depth; | |
8d08fdba | 14136 | |
4d6abc1c | 14137 | /* Clean up. */ |
28cbf42c | 14138 | if (! nested) |
1f8f4a0b MM |
14139 | /* Let the error reporting routines know that we're outside a |
14140 | function. For a nested function, this value is used in | |
b03e38e1 | 14141 | cxx_pop_function_context and then reset via pop_function_context. */ |
1f8f4a0b | 14142 | current_function_decl = NULL_TREE; |
4d6abc1c MM |
14143 | |
14144 | return fndecl; | |
8d08fdba MS |
14145 | } |
14146 | \f | |
14147 | /* Create the FUNCTION_DECL for a function definition. | |
8d08fdba MS |
14148 | DECLSPECS and DECLARATOR are the parts of the declaration; |
14149 | they describe the return type and the name of the function, | |
14150 | but twisted together in a fashion that parallels the syntax of C. | |
14151 | ||
14152 | This function creates a binding context for the function body | |
14153 | as well as setting up the FUNCTION_DECL in current_function_decl. | |
14154 | ||
14155 | Returns a FUNCTION_DECL on success. | |
14156 | ||
14157 | If the DECLARATOR is not suitable for a function (it defines a datum | |
14158 | instead), we return 0, which tells yyparse to report a parse error. | |
14159 | ||
14160 | May return void_type_node indicating that this method is actually | |
14161 | a friend. See grokfield for more details. | |
14162 | ||
14163 | Came here with a `.pushlevel' . | |
14164 | ||
14165 | DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING | |
14166 | CHANGES TO CODE IN `grokfield'. */ | |
e92cc029 | 14167 | |
8d08fdba | 14168 | tree |
11f6b451 | 14169 | start_method (tree declspecs, tree declarator, tree attrlist) |
8d08fdba | 14170 | { |
c11b6f21 | 14171 | tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0, |
91d231cb | 14172 | &attrlist); |
8d08fdba | 14173 | |
a723baf1 MM |
14174 | if (fndecl == error_mark_node) |
14175 | return error_mark_node; | |
14176 | ||
14177 | if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL) | |
14178 | { | |
14179 | error ("invalid member function declaration"); | |
14180 | return error_mark_node; | |
14181 | } | |
8d08fdba | 14182 | |
195a5def IR |
14183 | if (attrlist) |
14184 | cplus_decl_attributes (&fndecl, attrlist, 0); | |
14185 | ||
8d08fdba | 14186 | /* Pass friends other than inline friend functions back. */ |
a1774733 | 14187 | if (fndecl == void_type_node) |
8d08fdba MS |
14188 | return fndecl; |
14189 | ||
8d08fdba MS |
14190 | if (DECL_IN_AGGR_P (fndecl)) |
14191 | { | |
14192 | if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type) | |
14193 | { | |
68642fb6 | 14194 | if (DECL_CONTEXT (fndecl) |
2c73f9f5 | 14195 | && TREE_CODE( DECL_CONTEXT (fndecl)) != NAMESPACE_DECL) |
33bd39a2 | 14196 | error ("`%D' is already defined in class `%T'", fndecl, |
2ae7bada | 14197 | DECL_CONTEXT (fndecl)); |
8d08fdba MS |
14198 | } |
14199 | return void_type_node; | |
14200 | } | |
14201 | ||
f3400fe2 JM |
14202 | check_template_shadow (fndecl); |
14203 | ||
79065db2 | 14204 | DECL_DECLARED_INLINE_P (fndecl) = 1; |
faae18ab | 14205 | |
bc522472 | 14206 | DID_INLINE_FUNC (fndecl) = 0; |
8926095f | 14207 | if (flag_default_inline) |
8d08fdba MS |
14208 | DECL_INLINE (fndecl) = 1; |
14209 | ||
36a117a5 MM |
14210 | /* We process method specializations in finish_struct_1. */ |
14211 | if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl)) | |
3ac3d9ea | 14212 | fndecl = push_template_decl (fndecl); |
a0a33927 | 14213 | |
8d08fdba MS |
14214 | if (! DECL_FRIEND_P (fndecl)) |
14215 | { | |
8d08fdba MS |
14216 | if (TREE_CHAIN (fndecl)) |
14217 | { | |
14218 | fndecl = copy_node (fndecl); | |
14219 | TREE_CHAIN (fndecl) = NULL_TREE; | |
14220 | } | |
271e6f02 | 14221 | grok_special_member_properties (fndecl); |
8d08fdba MS |
14222 | } |
14223 | ||
cd9f6678 | 14224 | cp_finish_decl (fndecl, NULL_TREE, NULL_TREE, 0); |
8d08fdba MS |
14225 | |
14226 | /* Make a place for the parms */ | |
14227 | pushlevel (0); | |
14228 | current_binding_level->parm_flag = 1; | |
68642fb6 | 14229 | |
8d08fdba MS |
14230 | DECL_IN_AGGR_P (fndecl) = 1; |
14231 | return fndecl; | |
14232 | } | |
14233 | ||
14234 | /* Go through the motions of finishing a function definition. | |
14235 | We don't compile this method until after the whole class has | |
14236 | been processed. | |
14237 | ||
14238 | FINISH_METHOD must return something that looks as though it | |
14239 | came from GROKFIELD (since we are defining a method, after all). | |
14240 | ||
14241 | This is called after parsing the body of the function definition. | |
14242 | STMTS is the chain of statements that makes up the function body. | |
14243 | ||
14244 | DECL is the ..._DECL that `start_method' provided. */ | |
14245 | ||
14246 | tree | |
11f6b451 | 14247 | finish_method (tree decl) |
8d08fdba MS |
14248 | { |
14249 | register tree fndecl = decl; | |
14250 | tree old_initial; | |
8d08fdba MS |
14251 | |
14252 | register tree link; | |
14253 | ||
a1774733 | 14254 | if (decl == void_type_node) |
8d08fdba MS |
14255 | return decl; |
14256 | ||
14257 | old_initial = DECL_INITIAL (fndecl); | |
14258 | ||
14259 | /* Undo the level for the parms (from start_method). | |
14260 | This is like poplevel, but it causes nothing to be | |
14261 | saved. Saving information here confuses symbol-table | |
14262 | output routines. Besides, this information will | |
14263 | be correctly output when this method is actually | |
14264 | compiled. */ | |
14265 | ||
14266 | /* Clear out the meanings of the local variables of this level; | |
14267 | also record in each decl which block it belongs to. */ | |
14268 | ||
14269 | for (link = current_binding_level->names; link; link = TREE_CHAIN (link)) | |
14270 | { | |
14271 | if (DECL_NAME (link) != NULL_TREE) | |
d8f8dca1 | 14272 | pop_binding (DECL_NAME (link), link); |
8d08fdba MS |
14273 | my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163); |
14274 | DECL_CONTEXT (link) = NULL_TREE; | |
14275 | } | |
14276 | ||
8d08fdba MS |
14277 | poplevel (0, 0, 0); |
14278 | ||
14279 | DECL_INITIAL (fndecl) = old_initial; | |
14280 | ||
14281 | /* We used to check if the context of FNDECL was different from | |
14282 | current_class_type as another way to get inside here. This didn't work | |
14283 | for String.cc in libg++. */ | |
14284 | if (DECL_FRIEND_P (fndecl)) | |
14285 | { | |
14286 | CLASSTYPE_INLINE_FRIENDS (current_class_type) | |
14287 | = tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type)); | |
14288 | decl = void_type_node; | |
14289 | } | |
14290 | ||
14291 | return decl; | |
14292 | } | |
14293 | \f | |
0154eaa8 MM |
14294 | |
14295 | /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that | |
14296 | we can lay it out later, when and if its type becomes complete. */ | |
8d08fdba MS |
14297 | |
14298 | void | |
11f6b451 | 14299 | maybe_register_incomplete_var (tree var) |
8d08fdba | 14300 | { |
0154eaa8 | 14301 | my_friendly_assert (TREE_CODE (var) == VAR_DECL, 20020406); |
8d08fdba | 14302 | |
0154eaa8 MM |
14303 | /* Keep track of variables with incomplete types. */ |
14304 | if (!processing_template_decl && TREE_TYPE (var) != error_mark_node | |
14305 | && DECL_EXTERNAL (var)) | |
70adf8a9 | 14306 | { |
0154eaa8 MM |
14307 | tree inner_type = TREE_TYPE (var); |
14308 | ||
14309 | while (TREE_CODE (inner_type) == ARRAY_TYPE) | |
14310 | inner_type = TREE_TYPE (inner_type); | |
14311 | inner_type = TYPE_MAIN_VARIANT (inner_type); | |
14312 | ||
14313 | if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type)) | |
14314 | /* RTTI TD entries are created while defining the type_info. */ | |
14315 | || (TYPE_LANG_SPECIFIC (inner_type) | |
14316 | && TYPE_BEING_DEFINED (inner_type))) | |
14317 | incomplete_vars = tree_cons (inner_type, var, incomplete_vars); | |
70adf8a9 | 14318 | } |
0154eaa8 | 14319 | } |
70adf8a9 | 14320 | |
0154eaa8 MM |
14321 | /* Called when a class type (given by TYPE) is defined. If there are |
14322 | any existing VAR_DECLs whose type hsa been completed by this | |
14323 | declaration, update them now. */ | |
70adf8a9 | 14324 | |
0154eaa8 | 14325 | void |
11f6b451 | 14326 | complete_vars (tree type) |
0154eaa8 MM |
14327 | { |
14328 | tree *list = &incomplete_vars; | |
14329 | ||
14330 | my_friendly_assert (CLASS_TYPE_P (type), 20020406); | |
14331 | while (*list) | |
14332 | { | |
14333 | if (same_type_p (type, TREE_PURPOSE (*list))) | |
70adf8a9 | 14334 | { |
0154eaa8 | 14335 | tree var = TREE_VALUE (*list); |
c740732f MM |
14336 | /* Complete the type of the variable. The VAR_DECL itself |
14337 | will be laid out in expand_expr. */ | |
14338 | complete_type (TREE_TYPE (var)); | |
0154eaa8 MM |
14339 | /* Remove this entry from the list. */ |
14340 | *list = TREE_CHAIN (*list); | |
f30432d7 MS |
14341 | } |
14342 | else | |
0154eaa8 | 14343 | list = &TREE_CHAIN (*list); |
f30432d7 | 14344 | } |
8d08fdba MS |
14345 | } |
14346 | ||
86f45d2c MM |
14347 | /* If DECL is of a type which needs a cleanup, build that cleanup |
14348 | here. */ | |
e92cc029 | 14349 | |
86f45d2c | 14350 | tree |
11f6b451 | 14351 | cxx_maybe_build_cleanup (tree decl) |
8d08fdba MS |
14352 | { |
14353 | tree type = TREE_TYPE (decl); | |
86f45d2c | 14354 | |
834c6dff | 14355 | if (type != error_mark_node && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) |
8d08fdba | 14356 | { |
80048418 | 14357 | int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR; |
8d08fdba | 14358 | tree rval; |
8d08fdba | 14359 | |
8d08fdba MS |
14360 | if (TREE_CODE (type) == ARRAY_TYPE) |
14361 | rval = decl; | |
14362 | else | |
14363 | { | |
dffd7eb6 | 14364 | cxx_mark_addressable (decl); |
8d08fdba MS |
14365 | rval = build_unary_op (ADDR_EXPR, decl, 0); |
14366 | } | |
14367 | ||
14368 | /* Optimize for space over speed here. */ | |
14369 | if (! TYPE_USES_VIRTUAL_BASECLASSES (type) | |
14370 | || flag_expensive_optimizations) | |
14371 | flags |= LOOKUP_NONVIRTUAL; | |
14372 | ||
86f45d2c MM |
14373 | rval = build_delete (TREE_TYPE (rval), rval, |
14374 | sfk_complete_destructor, flags, 0); | |
8d08fdba MS |
14375 | |
14376 | if (TYPE_USES_VIRTUAL_BASECLASSES (type) | |
14377 | && ! TYPE_HAS_DESTRUCTOR (type)) | |
e1b3e07d | 14378 | rval = build_compound_expr (tree_cons (NULL_TREE, rval, |
051e6fd7 | 14379 | build_tree_list (NULL_TREE, build_vbase_delete (type, decl)))); |
8d08fdba | 14380 | |
8d08fdba MS |
14381 | return rval; |
14382 | } | |
6e4ae815 | 14383 | return NULL_TREE; |
8d08fdba MS |
14384 | } |
14385 | \f | |
558475f0 | 14386 | /* When a stmt has been parsed, this function is called. */ |
8d08fdba MS |
14387 | |
14388 | void | |
11f6b451 | 14389 | finish_stmt (void) |
8d08fdba | 14390 | { |
558475f0 MM |
14391 | /* Always assume this statement was not an expression statement. If |
14392 | it actually was an expression statement, its our callers | |
14393 | responsibility to fix this up. */ | |
14394 | last_expr_type = NULL_TREE; | |
8d08fdba MS |
14395 | } |
14396 | ||
3afb32a4 MM |
14397 | /* DECL was originally constructed as a non-static member function, |
14398 | but turned out to be static. Update it accordingly. */ | |
700f8a87 | 14399 | |
8857f91e | 14400 | void |
11f6b451 | 14401 | revert_static_member_fn (tree decl) |
8d08fdba | 14402 | { |
700f8a87 | 14403 | tree tmp; |
3afb32a4 MM |
14404 | tree function = TREE_TYPE (decl); |
14405 | tree args = TYPE_ARG_TYPES (function); | |
8d08fdba | 14406 | |
89d684bb | 14407 | if (cp_type_quals (TREE_TYPE (TREE_VALUE (args))) |
91063b51 | 14408 | != TYPE_UNQUALIFIED) |
33bd39a2 | 14409 | error ("static member function `%#D' declared with type qualifiers", |
11306230 | 14410 | decl); |
f30432d7 | 14411 | |
700f8a87 MS |
14412 | args = TREE_CHAIN (args); |
14413 | tmp = build_function_type (TREE_TYPE (function), args); | |
89d684bb | 14414 | tmp = build_qualified_type (tmp, cp_type_quals (function)); |
f30432d7 | 14415 | tmp = build_exception_variant (tmp, |
8d08fdba | 14416 | TYPE_RAISES_EXCEPTIONS (function)); |
3afb32a4 MM |
14417 | TREE_TYPE (decl) = tmp; |
14418 | if (DECL_ARGUMENTS (decl)) | |
14419 | DECL_ARGUMENTS (decl) = TREE_CHAIN (DECL_ARGUMENTS (decl)); | |
14420 | DECL_STATIC_FUNCTION_P (decl) = 1; | |
8d08fdba | 14421 | } |
a4443a08 | 14422 | |
68642fb6 UD |
14423 | /* Initialize the variables used during compilation of a C++ |
14424 | function. */ | |
db5ae43f | 14425 | |
b03e38e1 | 14426 | void |
11f6b451 | 14427 | cxx_push_function_context (struct function * f) |
99dccabc | 14428 | { |
e2500fed GK |
14429 | struct language_function *p |
14430 | = ((struct language_function *) | |
14431 | ggc_alloc_cleared (sizeof (struct language_function))); | |
14432 | f->language = p; | |
db5ae43f | 14433 | |
b35d4555 MM |
14434 | /* It takes an explicit call to expand_body to generate RTL for a |
14435 | function. */ | |
14436 | expanding_p = 0; | |
f1dedc31 MM |
14437 | |
14438 | /* Whenever we start a new function, we destroy temporaries in the | |
14439 | usual way. */ | |
ae499cce | 14440 | current_stmt_tree ()->stmts_are_full_exprs_p = 1; |
db5ae43f MS |
14441 | } |
14442 | ||
a8f73d4b MM |
14443 | /* Free the language-specific parts of F, now that we've finished |
14444 | compiling the function. */ | |
db5ae43f | 14445 | |
b03e38e1 | 14446 | void |
11f6b451 | 14447 | cxx_pop_function_context (struct function * f) |
db5ae43f | 14448 | { |
99dccabc | 14449 | f->language = 0; |
db5ae43f | 14450 | } |
ebfc180f | 14451 | |
e2500fed GK |
14452 | /* Return which tree structure is used by T, or TS_CP_GENERIC if T is |
14453 | one of the language-independent trees. */ | |
4519c0a8 | 14454 | |
e2500fed | 14455 | enum cp_tree_node_structure_enum |
11f6b451 | 14456 | cp_tree_node_structure (union lang_tree_node * t) |
4519c0a8 | 14457 | { |
e2500fed | 14458 | switch (TREE_CODE (&t->generic)) |
4519c0a8 | 14459 | { |
a723baf1 | 14460 | case DEFAULT_ARG: return TS_CP_DEFAULT_ARG; |
e2500fed | 14461 | case IDENTIFIER_NODE: return TS_CP_IDENTIFIER; |
e2500fed GK |
14462 | case OVERLOAD: return TS_CP_OVERLOAD; |
14463 | case TEMPLATE_PARM_INDEX: return TS_CP_TPI; | |
14464 | case PTRMEM_CST: return TS_CP_PTRMEM; | |
5dae1114 | 14465 | case BASELINK: return TS_CP_BASELINK; |
e2500fed | 14466 | case WRAPPER: return TS_CP_WRAPPER; |
e2500fed | 14467 | default: return TS_CP_GENERIC; |
4519c0a8 MM |
14468 | } |
14469 | } | |
5fd8e536 JM |
14470 | |
14471 | /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since | |
14472 | the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */ | |
14473 | ||
14474 | tree | |
11f6b451 | 14475 | identifier_global_value (tree t) |
5fd8e536 JM |
14476 | { |
14477 | return IDENTIFIER_GLOBAL_VALUE (t); | |
14478 | } | |
d363e7bf | 14479 | |
eaa7c03f JM |
14480 | /* Build the void_list_node (void_type_node having been created). */ |
14481 | tree | |
11f6b451 | 14482 | build_void_list_node (void) |
eaa7c03f JM |
14483 | { |
14484 | tree t = build_tree_list (NULL_TREE, void_type_node); | |
14485 | TREE_PARMLIST (t) = 1; | |
14486 | return t; | |
14487 | } | |
14488 | ||
d363e7bf | 14489 | static int |
11f6b451 | 14490 | cp_missing_noreturn_ok_p (tree decl) |
d363e7bf AJ |
14491 | { |
14492 | /* A missing noreturn is ok for the `main' function. */ | |
92643fea | 14493 | return DECL_MAIN_P (decl); |
d363e7bf | 14494 | } |
e2500fed GK |
14495 | |
14496 | #include "gt-cp-decl.h" | |
14497 | #include "gtype-cp.h" |