]>
Commit | Line | Data |
---|---|---|
e5e809f4 | 1 | /* Process declarations and variables for C compiler. |
d363e7bf | 2 | Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, |
75ac8dec | 3 | 2001, 2002 Free Software Foundation, Inc. |
e5e809f4 | 4 | Contributed by Michael Tiemann (tiemann@cygnus.com) |
8d08fdba MS |
5 | |
6 | This file is part of GNU CC. | |
7 | ||
8 | GNU CC is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2, or (at your option) | |
11 | any later version. | |
12 | ||
13 | GNU CC is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with GNU CC; see the file COPYING. If not, write to | |
e9fa0c7c RK |
20 | the Free Software Foundation, 59 Temple Place - Suite 330, |
21 | Boston, MA 02111-1307, USA. */ | |
8d08fdba MS |
22 | |
23 | ||
24 | /* Process declarations and symbol lookup for C front end. | |
25 | Also constructs types; the standard scalar types at initialization, | |
26 | and structure, union, array and enum types when they are declared. */ | |
27 | ||
28 | /* ??? not all decl nodes are given the most useful possible | |
29 | line numbers. For example, the CONST_DECLs for enum values. */ | |
30 | ||
8d08fdba | 31 | #include "config.h" |
8d052bc7 | 32 | #include "system.h" |
8d08fdba MS |
33 | #include "tree.h" |
34 | #include "rtl.h" | |
3bdf5ad1 | 35 | #include "expr.h" |
8d08fdba MS |
36 | #include "flags.h" |
37 | #include "cp-tree.h" | |
25af8512 | 38 | #include "tree-inline.h" |
8d08fdba MS |
39 | #include "decl.h" |
40 | #include "lex.h" | |
49c249e1 JM |
41 | #include "output.h" |
42 | #include "except.h" | |
54f92bfb | 43 | #include "toplev.h" |
e2500fed | 44 | #include "hashtab.h" |
4519c0a8 | 45 | #include "ggc.h" |
0e9295cf | 46 | #include "tm_p.h" |
672a6f42 | 47 | #include "target.h" |
26f943fd | 48 | #include "c-common.h" |
ecb0eece | 49 | #include "c-pragma.h" |
7437519c | 50 | #include "diagnostic.h" |
e2500fed | 51 | #include "debug.h" |
8d08fdba | 52 | |
5cce22b6 | 53 | static tree grokparms PARAMS ((tree)); |
158991b7 | 54 | static const char *redeclaration_error_message PARAMS ((tree, tree)); |
8d08fdba | 55 | |
e2500fed | 56 | static void push_binding_level PARAMS ((struct cp_binding_level *, int, |
49c249e1 | 57 | int)); |
158991b7 KG |
58 | static void pop_binding_level PARAMS ((void)); |
59 | static void suspend_binding_level PARAMS ((void)); | |
e2500fed GK |
60 | static void resume_binding_level PARAMS ((struct cp_binding_level *)); |
61 | static struct cp_binding_level *make_binding_level PARAMS ((void)); | |
158991b7 | 62 | static void declare_namespace_level PARAMS ((void)); |
6625cdb5 | 63 | static int decl_jump_unsafe PARAMS ((tree)); |
158991b7 KG |
64 | static void storedecls PARAMS ((tree)); |
65 | static void require_complete_types_for_parms PARAMS ((tree)); | |
596ea4e5 AS |
66 | static int ambi_op_p PARAMS ((enum tree_code)); |
67 | static int unary_op_p PARAMS ((enum tree_code)); | |
158991b7 KG |
68 | static tree store_bindings PARAMS ((tree, tree)); |
69 | static tree lookup_tag_reverse PARAMS ((tree, tree)); | |
158991b7 | 70 | static tree lookup_name_real PARAMS ((tree, int, int, int)); |
8e4ce833 | 71 | static void push_local_name PARAMS ((tree)); |
158991b7 | 72 | static void warn_extern_redeclared_static PARAMS ((tree, tree)); |
8e4ce833 | 73 | static tree grok_reference_init PARAMS ((tree, tree, tree)); |
158991b7 | 74 | static tree grokfndecl PARAMS ((tree, tree, tree, tree, int, |
2c73f9f5 | 75 | enum overload_flags, tree, |
7a8f9fa9 | 76 | tree, int, int, int, int, int, int, tree)); |
158991b7 | 77 | static tree grokvardecl PARAMS ((tree, tree, RID_BIT_TYPE *, int, int, tree)); |
4782bd5b | 78 | static tree follow_tag_typedef PARAMS ((tree)); |
158991b7 | 79 | static tree lookup_tag PARAMS ((enum tree_code, tree, |
e2500fed | 80 | struct cp_binding_level *, int)); |
49c249e1 | 81 | static void set_identifier_type_value_with_scope |
e2500fed | 82 | PARAMS ((tree, tree, struct cp_binding_level *)); |
158991b7 | 83 | static void record_unknown_type PARAMS ((tree, const char *)); |
d52e4867 | 84 | static tree builtin_function_1 PARAMS ((const char *, tree, tree, int, |
6a2dd09a RS |
85 | enum built_in_class, const char *, |
86 | tree)); | |
596ea4e5 | 87 | static tree build_library_fn_1 PARAMS ((tree, enum tree_code, tree)); |
158991b7 KG |
88 | static int member_function_or_else PARAMS ((tree, tree, enum overload_flags)); |
89 | static void bad_specifiers PARAMS ((tree, const char *, int, int, int, int, | |
49c249e1 | 90 | int)); |
e2500fed | 91 | static tree maybe_process_template_type_declaration PARAMS ((tree, int, struct cp_binding_level*)); |
158991b7 | 92 | static void check_for_uninitialized_const_var PARAMS ((tree)); |
e2500fed GK |
93 | static hashval_t typename_hash PARAMS ((const void *)); |
94 | static int typename_compare PARAMS ((const void *, const void *)); | |
95 | static void push_binding PARAMS ((tree, tree, struct cp_binding_level*)); | |
158991b7 KG |
96 | static int add_binding PARAMS ((tree, tree)); |
97 | static void pop_binding PARAMS ((tree, tree)); | |
98 | static tree local_variable_p_walkfn PARAMS ((tree *, int *, void *)); | |
99 | static tree find_binding PARAMS ((tree, tree)); | |
100 | static tree select_decl PARAMS ((tree, int)); | |
101 | static int lookup_flags PARAMS ((int, int)); | |
102 | static tree qualify_lookup PARAMS ((tree, int)); | |
103 | static tree record_builtin_java_type PARAMS ((const char *, int)); | |
104 | static const char *tag_name PARAMS ((enum tag_types code)); | |
105 | static void find_class_binding_level PARAMS ((void)); | |
e2500fed | 106 | static struct cp_binding_level *innermost_nonclass_level PARAMS ((void)); |
158991b7 KG |
107 | static void warn_about_implicit_typename_lookup PARAMS ((tree, tree)); |
108 | static int walk_namespaces_r PARAMS ((tree, walk_namespaces_fn, void *)); | |
109 | static int walk_globals_r PARAMS ((tree, void *)); | |
70b76b34 | 110 | static int walk_vtables_r PARAMS ((tree, void*)); |
e2500fed | 111 | static void add_decl_to_level PARAMS ((tree, struct cp_binding_level *)); |
158991b7 | 112 | static tree make_label_decl PARAMS ((tree, int)); |
6625cdb5 | 113 | static void use_label PARAMS ((tree)); |
e2500fed | 114 | static void check_previous_goto_1 PARAMS ((tree, struct cp_binding_level *, tree, |
6625cdb5 JM |
115 | const char *, int)); |
116 | static void check_previous_goto PARAMS ((struct named_label_use_list *)); | |
e2500fed | 117 | static void check_switch_goto PARAMS ((struct cp_binding_level *)); |
6625cdb5 JM |
118 | static void check_previous_gotos PARAMS ((tree)); |
119 | static void pop_label PARAMS ((tree, tree)); | |
158991b7 KG |
120 | static void pop_labels PARAMS ((tree)); |
121 | static void maybe_deduce_size_from_array_init PARAMS ((tree, tree)); | |
122 | static void layout_var_decl PARAMS ((tree)); | |
123 | static void maybe_commonize_var PARAMS ((tree)); | |
124 | static tree check_initializer PARAMS ((tree, tree)); | |
125 | static void make_rtl_for_nonlocal_decl PARAMS ((tree, tree, const char *)); | |
158991b7 | 126 | static void save_function_data PARAMS ((tree)); |
f444e36b | 127 | static void check_function_type PARAMS ((tree, tree)); |
158991b7 | 128 | static void destroy_local_var PARAMS ((tree)); |
cdd2559c | 129 | static void begin_constructor_body PARAMS ((void)); |
158991b7 | 130 | static void finish_constructor_body PARAMS ((void)); |
cdd2559c | 131 | static void begin_destructor_body PARAMS ((void)); |
158991b7 KG |
132 | static void finish_destructor_body PARAMS ((void)); |
133 | static tree create_array_type_for_decl PARAMS ((tree, tree, tree)); | |
134 | static tree get_atexit_node PARAMS ((void)); | |
135 | static tree get_dso_handle_node PARAMS ((void)); | |
136 | static tree start_cleanup_fn PARAMS ((void)); | |
137 | static void end_cleanup_fn PARAMS ((void)); | |
0ba8a114 | 138 | static tree cp_make_fname_decl PARAMS ((tree, int)); |
d43829f9 | 139 | static void initialize_predefined_identifiers PARAMS ((void)); |
5362b086 | 140 | static tree check_special_function_return_type |
1f84ec23 | 141 | PARAMS ((special_function_kind, tree, tree)); |
596ea4e5 AS |
142 | static tree push_cp_library_fn PARAMS ((enum tree_code, tree)); |
143 | static tree build_cp_library_fn PARAMS ((tree, enum tree_code, tree)); | |
f444e36b | 144 | static void store_parm_decls PARAMS ((tree)); |
d363e7bf | 145 | static int cp_missing_noreturn_ok_p PARAMS ((tree)); |
8d08fdba | 146 | |
e2500fed | 147 | #if defined (DEBUG_BINDING_LEVELS) |
158991b7 | 148 | static void indent PARAMS ((void)); |
69ac77ce JL |
149 | #endif |
150 | ||
8d08fdba MS |
151 | /* Erroneous argument lists can use this *IFF* they do not modify it. */ |
152 | tree error_mark_list; | |
153 | ||
7f4edbcb | 154 | /* The following symbols are subsumed in the cp_global_trees array, and |
68642fb6 | 155 | listed here individually for documentation purposes. |
8d08fdba | 156 | |
7f4edbcb BS |
157 | C++ extensions |
158 | tree wchar_decl_node; | |
37c46b43 | 159 | |
7f4edbcb BS |
160 | tree vtable_entry_type; |
161 | tree delta_type_node; | |
7f4edbcb | 162 | tree __t_desc_type_node; |
db1147b2 | 163 | tree ti_desc_type_node; |
1f4cb92b | 164 | tree bltn_desc_type_node, ptr_desc_type_node; |
db1147b2 NS |
165 | tree ary_desc_type_node, func_desc_type_node, enum_desc_type_node; |
166 | tree class_desc_type_node, si_class_desc_type_node, vmi_class_desc_type_node; | |
387769ed | 167 | tree ptm_desc_type_node; |
db1147b2 | 168 | tree base_desc_type_node; |
8d08fdba | 169 | |
7f4edbcb BS |
170 | tree class_type_node, record_type_node, union_type_node, enum_type_node; |
171 | tree unknown_type_node; | |
8d08fdba | 172 | |
7f4edbcb | 173 | Array type `vtable_entry_type[]' |
8d08fdba | 174 | |
7f4edbcb BS |
175 | tree vtbl_type_node; |
176 | tree vtbl_ptr_type_node; | |
8d08fdba | 177 | |
2854d3c6 | 178 | Namespaces, |
8d08fdba | 179 | |
7f4edbcb | 180 | tree std_node; |
2854d3c6 | 181 | tree abi_node; |
8d08fdba | 182 | |
7f4edbcb BS |
183 | A FUNCTION_DECL which can call `abort'. Not necessarily the |
184 | one that the user will declare, but sufficient to be called | |
185 | by routines that want to abort the program. | |
8d08fdba | 186 | |
7f4edbcb | 187 | tree abort_fndecl; |
8d08fdba | 188 | |
7f4edbcb | 189 | The FUNCTION_DECL for the default `::operator delete'. |
2986ae00 | 190 | |
7f4edbcb | 191 | tree global_delete_fndecl; |
8d08fdba | 192 | |
7f4edbcb | 193 | Used by RTTI |
669ec2b4 JM |
194 | tree type_info_type_node, tinfo_decl_id, tinfo_decl_type; |
195 | tree tinfo_var_id; | |
196 | ||
7f4edbcb | 197 | */ |
8d08fdba | 198 | |
7f4edbcb | 199 | tree cp_global_trees[CPTI_MAX]; |
8d08fdba | 200 | |
2c73f9f5 | 201 | /* Indicates that there is a type value in some namespace, although |
7f4edbcb | 202 | that is not necessarily in scope at the moment. */ |
2c73f9f5 | 203 | |
e2500fed | 204 | static GTY(()) tree global_type_node; |
2c73f9f5 | 205 | |
a1c65f9f | 206 | /* Expect only namespace names now. */ |
3e3f722c ML |
207 | static int only_namespace_names; |
208 | ||
6625cdb5 JM |
209 | /* Used only for jumps to as-yet undefined labels, since jumps to |
210 | defined labels can have their validity checked immediately. */ | |
211 | ||
e2500fed | 212 | struct named_label_use_list GTY(()) |
e349ee73 | 213 | { |
e2500fed | 214 | struct cp_binding_level *binding_level; |
e349ee73 MS |
215 | tree names_in_scope; |
216 | tree label_decl; | |
9c0758dd | 217 | const char *filename_o_goto; |
e349ee73 | 218 | int lineno_o_goto; |
6625cdb5 | 219 | struct named_label_use_list *next; |
e349ee73 MS |
220 | }; |
221 | ||
ed5511d9 | 222 | #define named_label_uses cp_function_chain->x_named_label_uses |
8d08fdba | 223 | |
8e4ce833 JJ |
224 | #define local_names cp_function_chain->x_local_names |
225 | ||
8d08fdba MS |
226 | /* A list of objects which have constructors or destructors |
227 | which reside in the global scope. The decl is stored in | |
228 | the TREE_VALUE slot and the initializer is stored | |
229 | in the TREE_PURPOSE slot. */ | |
230 | tree static_aggregates; | |
231 | ||
8d08fdba MS |
232 | /* -- end of C++ */ |
233 | ||
81b3411c | 234 | /* A node for the integer constants 2, and 3. */ |
d11ad92e | 235 | |
81b3411c | 236 | tree integer_two_node, integer_three_node; |
8d08fdba | 237 | |
8d08fdba | 238 | /* Similar, for last_function_parm_tags. */ |
9cd64686 | 239 | tree last_function_parms; |
8d08fdba | 240 | |
6625cdb5 JM |
241 | /* A list of all LABEL_DECLs in the function that have names. Here so |
242 | we can clear out their names' definitions at the end of the | |
243 | function, and so we can check the validity of jumps to these labels. */ | |
244 | ||
e2500fed | 245 | struct named_label_list GTY(()) |
6625cdb5 | 246 | { |
e2500fed | 247 | struct cp_binding_level *binding_level; |
6625cdb5 JM |
248 | tree names_in_scope; |
249 | tree old_value; | |
250 | tree label_decl; | |
251 | tree bad_decls; | |
6625cdb5 | 252 | struct named_label_list *next; |
826840d9 RH |
253 | unsigned int in_try_scope : 1; |
254 | unsigned int in_catch_scope : 1; | |
6625cdb5 | 255 | }; |
8d08fdba | 256 | |
4519c0a8 | 257 | #define named_labels cp_function_chain->x_named_labels |
8d08fdba | 258 | \f |
0c8feefe MM |
259 | /* The name of the anonymous namespace, throughout this translation |
260 | unit. */ | |
261 | tree anonymous_namespace_name; | |
262 | ||
b2244c65 MM |
263 | /* The number of function bodies which we are currently processing. |
264 | (Zero if we are at namespace scope, one inside the body of a | |
265 | function, two inside the body of a function in a local class, etc.) */ | |
266 | int function_depth; | |
e23bd218 IR |
267 | |
268 | /* States indicating how grokdeclarator() should handle declspecs marked | |
269 | with __attribute__((deprecated)). An object declared as | |
270 | __attribute__((deprecated)) suppresses warnings of uses of other | |
271 | deprecated items. */ | |
272 | ||
273 | enum deprecated_states { | |
274 | DEPRECATED_NORMAL, | |
275 | DEPRECATED_SUPPRESS | |
276 | }; | |
277 | ||
278 | static enum deprecated_states deprecated_state = DEPRECATED_NORMAL; | |
279 | ||
280 | /* Set by add_implicitly_declared_members() to keep those members from | |
281 | being flagged as deprecated or reported as using deprecated | |
282 | types. */ | |
283 | int adding_implicit_members = 0; | |
594bb0e7 MM |
284 | |
285 | /* True if a declaration with an `extern' linkage specifier is being | |
286 | processed. */ | |
287 | bool have_extern_spec; | |
288 | ||
8d08fdba MS |
289 | \f |
290 | /* For each binding contour we allocate a binding_level structure | |
e92cc029 MS |
291 | which records the names defined in that contour. |
292 | Contours include: | |
293 | 0) the global one | |
294 | 1) one for each function definition, | |
295 | where internal declarations of the parameters appear. | |
296 | 2) one for each compound statement, | |
297 | to record its declarations. | |
298 | ||
299 | The current meaning of a name can be found by searching the levels | |
300 | from the current one out to the global one. | |
301 | ||
302 | Off to the side, may be the class_binding_level. This exists only | |
303 | to catch class-local declarations. It is otherwise nonexistent. | |
304 | ||
305 | Also there may be binding levels that catch cleanups that must be | |
d8f8dca1 MM |
306 | run when exceptions occur. Thus, to see whether a name is bound in |
307 | the current scope, it is not enough to look in the | |
308 | CURRENT_BINDING_LEVEL. You should use lookup_name_current_level | |
309 | instead. */ | |
8d08fdba MS |
310 | |
311 | /* Note that the information in the `names' component of the global contour | |
312 | is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */ | |
313 | ||
e2500fed | 314 | struct cp_binding_level GTY(()) |
8d08fdba MS |
315 | { |
316 | /* A chain of _DECL nodes for all variables, constants, functions, | |
e92cc029 | 317 | and typedef types. These are in the reverse of the order |
f181d4ae MM |
318 | supplied. There may be OVERLOADs on this list, too, but they |
319 | are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD. */ | |
8d08fdba MS |
320 | tree names; |
321 | ||
70b76b34 DP |
322 | /* Count of elements in names chain. */ |
323 | size_t names_size; | |
324 | ||
325 | /* A chain of NAMESPACE_DECL nodes. */ | |
326 | tree namespaces; | |
327 | ||
328 | /* A chain of VTABLE_DECL nodes. */ | |
329 | tree vtables; | |
330 | ||
e92cc029 MS |
331 | /* A list of structure, union and enum definitions, for looking up |
332 | tag names. | |
333 | It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name, | |
334 | or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE, | |
335 | or ENUMERAL_TYPE node. | |
336 | ||
337 | C++: the TREE_VALUE nodes can be simple types for | |
338 | component_bindings. */ | |
8d08fdba MS |
339 | tree tags; |
340 | ||
a1c65f9f | 341 | /* A list of USING_DECL nodes. */ |
2c73f9f5 ML |
342 | tree usings; |
343 | ||
ea9635c7 | 344 | /* A list of used namespaces. PURPOSE is the namespace, |
a1c65f9f | 345 | VALUE the common ancestor with this binding_level's namespace. */ |
ea9635c7 ML |
346 | tree using_directives; |
347 | ||
f181d4ae MM |
348 | /* If this binding level is the binding level for a class, then |
349 | class_shadowed is a TREE_LIST. The TREE_PURPOSE of each node | |
76191fdd | 350 | is the name of an entity bound in the class. The TREE_TYPE is |
d8f8dca1 | 351 | the DECL bound by this name in the class. */ |
8d08fdba MS |
352 | tree class_shadowed; |
353 | ||
f181d4ae | 354 | /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and |
76191fdd NS |
355 | is used for all binding levels. In addition the TREE_VALUE is the |
356 | IDENTIFIER_TYPE_VALUE before we entered the class. */ | |
8d08fdba MS |
357 | tree type_shadowed; |
358 | ||
acef433b MM |
359 | /* A TREE_LIST. Each TREE_VALUE is the LABEL_DECL for a local |
360 | label in this scope. The TREE_PURPOSE is the previous value of | |
361 | the IDENTIFIER_LABEL VALUE. */ | |
362 | tree shadowed_labels; | |
363 | ||
8d08fdba MS |
364 | /* For each level (except not the global one), |
365 | a chain of BLOCK nodes for all the levels | |
366 | that were entered and exited one level down. */ | |
367 | tree blocks; | |
368 | ||
70adf8a9 JM |
369 | /* The _TYPE node for this level, if parm_flag == 2. */ |
370 | tree this_class; | |
371 | ||
8d08fdba | 372 | /* The binding level which this one is contained in (inherits from). */ |
e2500fed | 373 | struct cp_binding_level *level_chain; |
8d08fdba | 374 | |
8d6e462b | 375 | /* List of VAR_DECLS saved from a previous for statement. |
cab1f180 | 376 | These would be dead in ISO-conforming code, but might |
f181d4ae MM |
377 | be referenced in ARM-era code. These are stored in a |
378 | TREE_LIST; the TREE_VALUE is the actual declaration. */ | |
8d6e462b PB |
379 | tree dead_vars_from_for; |
380 | ||
8d08fdba | 381 | /* 1 for the level that holds the parameters of a function. |
74b846e0 MM |
382 | 2 for the level that holds a class declaration. */ |
383 | unsigned parm_flag : 2; | |
8d08fdba MS |
384 | |
385 | /* 1 means make a BLOCK for this level regardless of all else. | |
386 | 2 for temporary binding contours created by the compiler. */ | |
74b846e0 | 387 | unsigned keep : 2; |
8d08fdba MS |
388 | |
389 | /* Nonzero if this level "doesn't exist" for tags. */ | |
390 | unsigned tag_transparent : 1; | |
391 | ||
392 | /* Nonzero if this level can safely have additional | |
393 | cleanup-needing variables added to it. */ | |
394 | unsigned more_cleanups_ok : 1; | |
395 | unsigned have_cleanups : 1; | |
396 | ||
74b846e0 | 397 | /* Nonzero if this scope is for storing the decls for template |
5566b478 MS |
398 | parameters and generic decls; these decls will be discarded and |
399 | replaced with a TEMPLATE_DECL. */ | |
74b846e0 MM |
400 | unsigned template_parms_p : 1; |
401 | ||
5362b086 | 402 | /* Nonzero if this scope corresponds to the `<>' in a |
74b846e0 MM |
403 | `template <>' clause. Whenever this flag is set, |
404 | TEMPLATE_PARMS_P will be set as well. */ | |
405 | unsigned template_spec_p : 1; | |
8d08fdba | 406 | |
a9aedbc2 MS |
407 | /* This is set for a namespace binding level. */ |
408 | unsigned namespace_p : 1; | |
409 | ||
2ee887f2 | 410 | /* True if this level is that of a for-statement where we need to |
cab1f180 | 411 | worry about ambiguous (ARM or ISO) scope rules. */ |
8d6e462b PB |
412 | unsigned is_for_scope : 1; |
413 | ||
826840d9 RH |
414 | /* True if this level corresponds to a TRY block. Currently this |
415 | information is only available while building the tree structure. */ | |
416 | unsigned is_try_scope : 1; | |
227cf171 | 417 | |
826840d9 RH |
418 | /* True if this level corresponds to a CATCH block. Currently this |
419 | information is only available while building the tree structure. */ | |
420 | unsigned is_catch_scope : 1; | |
421 | ||
422 | /* Three bits left for this word. */ | |
8d08fdba | 423 | |
8d08fdba MS |
424 | /* Binding depth at which this level began. */ |
425 | unsigned binding_depth; | |
8d08fdba MS |
426 | }; |
427 | ||
e2500fed | 428 | #define NULL_BINDING_LEVEL ((struct cp_binding_level *) NULL) |
68642fb6 | 429 | |
8f032717 | 430 | /* The binding level currently in effect. */ |
8d08fdba | 431 | |
a8f73d4b | 432 | #define current_binding_level \ |
52a11cbf | 433 | (cfun && cp_function_chain->bindings \ |
d363e7bf | 434 | ? cp_function_chain->bindings \ |
a8f73d4b | 435 | : scope_chain->bindings) |
8d08fdba MS |
436 | |
437 | /* The binding level of the current class, if any. */ | |
438 | ||
9cd64686 | 439 | #define class_binding_level scope_chain->class_bindings |
8d08fdba | 440 | |
8d08fdba MS |
441 | /* A chain of binding_level structures awaiting reuse. */ |
442 | ||
e2500fed | 443 | static GTY((deletable (""))) struct cp_binding_level *free_binding_level; |
8d08fdba MS |
444 | |
445 | /* The outermost binding level, for names of file scope. | |
446 | This is created when the compiler is started and exists | |
447 | through the entire run. */ | |
448 | ||
e2500fed | 449 | static GTY(()) struct cp_binding_level *global_binding_level; |
8d08fdba | 450 | |
8d08fdba MS |
451 | /* Nonzero means unconditionally make a BLOCK for the next level pushed. */ |
452 | ||
453 | static int keep_next_level_flag; | |
454 | ||
0154eaa8 MM |
455 | /* A TREE_LIST of VAR_DECLs. The TREE_PURPOSE is a RECORD_TYPE or |
456 | UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type. At the | |
457 | time the VAR_DECL was declared, the type was incomplete. */ | |
458 | ||
e2500fed | 459 | static GTY(()) tree incomplete_vars; |
0154eaa8 | 460 | |
e2500fed | 461 | #if defined(DEBUG_BINDING_LEVELS) |
8d08fdba MS |
462 | static int binding_depth = 0; |
463 | static int is_class_level = 0; | |
464 | ||
465 | static void | |
466 | indent () | |
467 | { | |
468 | register unsigned i; | |
469 | ||
470 | for (i = 0; i < binding_depth*2; i++) | |
471 | putc (' ', stderr); | |
472 | } | |
e2500fed | 473 | #endif /* defined(DEBUG_BINDING_LEVELS) */ |
8d08fdba | 474 | |
e2500fed | 475 | static tree pushdecl_with_scope PARAMS ((tree, struct cp_binding_level *)); |
8d08fdba MS |
476 | |
477 | static void | |
478 | push_binding_level (newlevel, tag_transparent, keep) | |
e2500fed | 479 | struct cp_binding_level *newlevel; |
8d08fdba MS |
480 | int tag_transparent, keep; |
481 | { | |
482 | /* Add this level to the front of the chain (stack) of levels that | |
483 | are active. */ | |
e2500fed | 484 | memset ((char*) newlevel, 0, sizeof (struct cp_binding_level)); |
8f032717 | 485 | newlevel->level_chain = current_binding_level; |
8d08fdba MS |
486 | current_binding_level = newlevel; |
487 | newlevel->tag_transparent = tag_transparent; | |
488 | newlevel->more_cleanups_ok = 1; | |
227cf171 | 489 | |
8d08fdba | 490 | newlevel->keep = keep; |
e2500fed | 491 | #if defined(DEBUG_BINDING_LEVELS) |
8d08fdba MS |
492 | newlevel->binding_depth = binding_depth; |
493 | indent (); | |
494 | fprintf (stderr, "push %s level 0x%08x line %d\n", | |
495 | (is_class_level) ? "class" : "block", newlevel, lineno); | |
496 | is_class_level = 0; | |
497 | binding_depth++; | |
e2500fed | 498 | #endif /* defined(DEBUG_BINDING_LEVELS) */ |
8d08fdba MS |
499 | } |
500 | ||
8f032717 MM |
501 | /* Find the innermost enclosing class scope, and reset |
502 | CLASS_BINDING_LEVEL appropriately. */ | |
503 | ||
504 | static void | |
505 | find_class_binding_level () | |
506 | { | |
e2500fed | 507 | struct cp_binding_level *level = current_binding_level; |
8f032717 MM |
508 | |
509 | while (level && level->parm_flag != 2) | |
510 | level = level->level_chain; | |
511 | if (level && level->parm_flag == 2) | |
512 | class_binding_level = level; | |
513 | else | |
514 | class_binding_level = 0; | |
515 | } | |
516 | ||
8d08fdba MS |
517 | static void |
518 | pop_binding_level () | |
519 | { | |
8d08fdba MS |
520 | if (global_binding_level) |
521 | { | |
2c73f9f5 | 522 | /* Cannot pop a level, if there are none left to pop. */ |
8d08fdba | 523 | if (current_binding_level == global_binding_level) |
a98facb0 | 524 | abort (); |
8d08fdba MS |
525 | } |
526 | /* Pop the current level, and free the structure for reuse. */ | |
e2500fed | 527 | #if defined(DEBUG_BINDING_LEVELS) |
8d08fdba MS |
528 | binding_depth--; |
529 | indent (); | |
530 | fprintf (stderr, "pop %s level 0x%08x line %d\n", | |
531 | (is_class_level) ? "class" : "block", | |
532 | current_binding_level, lineno); | |
533 | if (is_class_level != (current_binding_level == class_binding_level)) | |
9e9ff709 MS |
534 | { |
535 | indent (); | |
536 | fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n"); | |
537 | } | |
8d08fdba | 538 | is_class_level = 0; |
e2500fed | 539 | #endif /* defined(DEBUG_BINDING_LEVELS) */ |
8d08fdba | 540 | { |
e2500fed | 541 | register struct cp_binding_level *level = current_binding_level; |
8d08fdba MS |
542 | current_binding_level = current_binding_level->level_chain; |
543 | level->level_chain = free_binding_level; | |
e2500fed | 544 | #if 0 /* defined(DEBUG_BINDING_LEVELS) */ |
8d08fdba MS |
545 | if (level->binding_depth != binding_depth) |
546 | abort (); | |
e2500fed | 547 | #endif /* defined(DEBUG_BINDING_LEVELS) */ |
8f032717 MM |
548 | free_binding_level = level; |
549 | find_class_binding_level (); | |
8d08fdba MS |
550 | } |
551 | } | |
a9aedbc2 MS |
552 | |
553 | static void | |
554 | suspend_binding_level () | |
555 | { | |
556 | if (class_binding_level) | |
557 | current_binding_level = class_binding_level; | |
558 | ||
559 | if (global_binding_level) | |
560 | { | |
2c73f9f5 | 561 | /* Cannot suspend a level, if there are none left to suspend. */ |
a9aedbc2 | 562 | if (current_binding_level == global_binding_level) |
a98facb0 | 563 | abort (); |
a9aedbc2 MS |
564 | } |
565 | /* Suspend the current level. */ | |
e2500fed | 566 | #if defined(DEBUG_BINDING_LEVELS) |
a9aedbc2 MS |
567 | binding_depth--; |
568 | indent (); | |
569 | fprintf (stderr, "suspend %s level 0x%08x line %d\n", | |
570 | (is_class_level) ? "class" : "block", | |
571 | current_binding_level, lineno); | |
572 | if (is_class_level != (current_binding_level == class_binding_level)) | |
9e9ff709 MS |
573 | { |
574 | indent (); | |
575 | fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n"); | |
576 | } | |
a9aedbc2 | 577 | is_class_level = 0; |
e2500fed | 578 | #endif /* defined(DEBUG_BINDING_LEVELS) */ |
8f032717 MM |
579 | current_binding_level = current_binding_level->level_chain; |
580 | find_class_binding_level (); | |
a9aedbc2 MS |
581 | } |
582 | ||
824b9a4c | 583 | static void |
a9aedbc2 | 584 | resume_binding_level (b) |
e2500fed | 585 | struct cp_binding_level *b; |
a9aedbc2 | 586 | { |
2c73f9f5 | 587 | /* Resuming binding levels is meant only for namespaces, |
a1c65f9f | 588 | and those cannot nest into classes. */ |
2c73f9f5 ML |
589 | my_friendly_assert(!class_binding_level, 386); |
590 | /* Also, resuming a non-directly nested namespace is a no-no. */ | |
591 | my_friendly_assert(b->level_chain == current_binding_level, 386); | |
a9aedbc2 | 592 | current_binding_level = b; |
e2500fed | 593 | #if defined(DEBUG_BINDING_LEVELS) |
a9aedbc2 MS |
594 | b->binding_depth = binding_depth; |
595 | indent (); | |
596 | fprintf (stderr, "resume %s level 0x%08x line %d\n", | |
597 | (is_class_level) ? "class" : "block", b, lineno); | |
598 | is_class_level = 0; | |
599 | binding_depth++; | |
e2500fed | 600 | #endif /* defined(DEBUG_BINDING_LEVELS) */ |
a9aedbc2 | 601 | } |
8d08fdba | 602 | \f |
e2500fed | 603 | /* Create a new `struct cp_binding_level'. */ |
cffa8729 MS |
604 | |
605 | static | |
e2500fed | 606 | struct cp_binding_level * |
cffa8729 MS |
607 | make_binding_level () |
608 | { | |
609 | /* NOSTRICT */ | |
e2500fed | 610 | return (struct cp_binding_level *) ggc_alloc (sizeof (struct cp_binding_level)); |
cffa8729 MS |
611 | } |
612 | ||
8d08fdba MS |
613 | /* Nonzero if we are currently in the global binding level. */ |
614 | ||
615 | int | |
616 | global_bindings_p () | |
617 | { | |
618 | return current_binding_level == global_binding_level; | |
619 | } | |
620 | ||
8f032717 MM |
621 | /* Return the innermost binding level that is not for a class scope. */ |
622 | ||
e2500fed | 623 | static struct cp_binding_level * |
8f032717 MM |
624 | innermost_nonclass_level () |
625 | { | |
e2500fed | 626 | struct cp_binding_level *b; |
8f032717 MM |
627 | |
628 | b = current_binding_level; | |
629 | while (b->parm_flag == 2) | |
630 | b = b->level_chain; | |
631 | ||
632 | return b; | |
633 | } | |
634 | ||
a9aedbc2 MS |
635 | /* Nonzero if we are currently in a toplevel binding level. This |
636 | means either the global binding level or a namespace in a toplevel | |
8f032717 | 637 | binding level. Since there are no non-toplevel namespace levels, |
74b846e0 MM |
638 | this really means any namespace or template parameter level. We |
639 | also include a class whose context is toplevel. */ | |
a9aedbc2 MS |
640 | |
641 | int | |
642 | toplevel_bindings_p () | |
643 | { | |
e2500fed | 644 | struct cp_binding_level *b = innermost_nonclass_level (); |
8f032717 | 645 | |
74b846e0 | 646 | return b->namespace_p || b->template_parms_p; |
a9aedbc2 MS |
647 | } |
648 | ||
8f032717 MM |
649 | /* Nonzero if this is a namespace scope, or if we are defining a class |
650 | which is itself at namespace scope, or whose enclosing class is | |
651 | such a class, etc. */ | |
a9aedbc2 | 652 | |
7bdbfa05 | 653 | int |
a9aedbc2 MS |
654 | namespace_bindings_p () |
655 | { | |
e2500fed | 656 | struct cp_binding_level *b = innermost_nonclass_level (); |
8f032717 MM |
657 | |
658 | return b->namespace_p; | |
a9aedbc2 MS |
659 | } |
660 | ||
838dfd8a | 661 | /* If KEEP is nonzero, make a BLOCK node for the next binding level, |
f1dedc31 MM |
662 | unconditionally. Otherwise, use the normal logic to decide whether |
663 | or not to create a BLOCK. */ | |
664 | ||
8d08fdba | 665 | void |
f1dedc31 MM |
666 | keep_next_level (keep) |
667 | int keep; | |
8d08fdba | 668 | { |
f1dedc31 | 669 | keep_next_level_flag = keep; |
8d08fdba MS |
670 | } |
671 | ||
672 | /* Nonzero if the current level needs to have a BLOCK made. */ | |
673 | ||
674 | int | |
675 | kept_level_p () | |
676 | { | |
677 | return (current_binding_level->blocks != NULL_TREE | |
678 | || current_binding_level->keep | |
679 | || current_binding_level->names != NULL_TREE | |
680 | || (current_binding_level->tags != NULL_TREE | |
681 | && !current_binding_level->tag_transparent)); | |
682 | } | |
683 | ||
824b9a4c | 684 | static void |
a9aedbc2 MS |
685 | declare_namespace_level () |
686 | { | |
687 | current_binding_level->namespace_p = 1; | |
688 | } | |
689 | ||
838dfd8a | 690 | /* Returns nonzero if this scope was created to store template |
74b846e0 MM |
691 | parameters. */ |
692 | ||
8d08fdba | 693 | int |
74b846e0 | 694 | template_parm_scope_p () |
8d08fdba | 695 | { |
74b846e0 MM |
696 | return current_binding_level->template_parms_p; |
697 | } | |
698 | ||
699 | /* Returns the kind of template specialization we are currently | |
700 | processing, given that it's declaration contained N_CLASS_SCOPES | |
701 | explicit scope qualifications. */ | |
702 | ||
703 | tmpl_spec_kind | |
704 | current_tmpl_spec_kind (n_class_scopes) | |
705 | int n_class_scopes; | |
706 | { | |
707 | int n_template_parm_scopes = 0; | |
708 | int seen_specialization_p = 0; | |
709 | int innermost_specialization_p = 0; | |
e2500fed | 710 | struct cp_binding_level *b; |
74b846e0 MM |
711 | |
712 | /* Scan through the template parameter scopes. */ | |
713 | for (b = current_binding_level; b->template_parms_p; b = b->level_chain) | |
714 | { | |
715 | /* If we see a specialization scope inside a parameter scope, | |
716 | then something is wrong. That corresponds to a declaration | |
717 | like: | |
718 | ||
719 | template <class T> template <> ... | |
720 | ||
0e339752 | 721 | which is always invalid since [temp.expl.spec] forbids the |
74b846e0 MM |
722 | specialization of a class member template if the enclosing |
723 | class templates are not explicitly specialized as well. */ | |
724 | if (b->template_spec_p) | |
725 | { | |
726 | if (n_template_parm_scopes == 0) | |
727 | innermost_specialization_p = 1; | |
728 | else | |
729 | seen_specialization_p = 1; | |
730 | } | |
731 | else if (seen_specialization_p == 1) | |
732 | return tsk_invalid_member_spec; | |
733 | ||
734 | ++n_template_parm_scopes; | |
735 | } | |
736 | ||
737 | /* Handle explicit instantiations. */ | |
738 | if (processing_explicit_instantiation) | |
739 | { | |
740 | if (n_template_parm_scopes != 0) | |
741 | /* We've seen a template parameter list during an explicit | |
742 | instantiation. For example: | |
743 | ||
744 | template <class T> template void f(int); | |
745 | ||
746 | This is erroneous. */ | |
747 | return tsk_invalid_expl_inst; | |
748 | else | |
749 | return tsk_expl_inst; | |
750 | } | |
751 | ||
752 | if (n_template_parm_scopes < n_class_scopes) | |
753 | /* We've not seen enough template headers to match all the | |
754 | specialized classes present. For example: | |
755 | ||
756 | template <class T> void R<T>::S<T>::f(int); | |
757 | ||
0e339752 | 758 | This is invalid; there needs to be one set of template |
74b846e0 MM |
759 | parameters for each class. */ |
760 | return tsk_insufficient_parms; | |
761 | else if (n_template_parm_scopes == n_class_scopes) | |
762 | /* We're processing a non-template declaration (even though it may | |
763 | be a member of a template class.) For example: | |
764 | ||
765 | template <class T> void S<T>::f(int); | |
766 | ||
767 | The `class T' maches the `S<T>', leaving no template headers | |
768 | corresponding to the `f'. */ | |
769 | return tsk_none; | |
770 | else if (n_template_parm_scopes > n_class_scopes + 1) | |
771 | /* We've got too many template headers. For example: | |
772 | ||
773 | template <> template <class T> void f (T); | |
774 | ||
775 | There need to be more enclosing classes. */ | |
776 | return tsk_excessive_parms; | |
777 | else | |
778 | /* This must be a template. It's of the form: | |
779 | ||
780 | template <class T> template <class U> void S<T>::f(U); | |
781 | ||
782 | This is a specialization if the innermost level was a | |
783 | specialization; otherwise it's just a definition of the | |
784 | template. */ | |
785 | return innermost_specialization_p ? tsk_expl_spec : tsk_template; | |
8d08fdba MS |
786 | } |
787 | ||
788 | void | |
789 | set_class_shadows (shadows) | |
790 | tree shadows; | |
791 | { | |
792 | class_binding_level->class_shadowed = shadows; | |
793 | } | |
794 | ||
795 | /* Enter a new binding level. | |
796 | If TAG_TRANSPARENT is nonzero, do so only for the name space of variables, | |
797 | not for that of tags. */ | |
798 | ||
799 | void | |
800 | pushlevel (tag_transparent) | |
801 | int tag_transparent; | |
802 | { | |
e2500fed | 803 | struct cp_binding_level *newlevel; |
8d08fdba | 804 | |
01d939e8 | 805 | if (cfun && !doing_semantic_analysis_p ()) |
b35d4555 | 806 | return; |
8d08fdba | 807 | |
b35d4555 | 808 | /* Reuse or create a struct for this binding level. */ |
e2500fed | 809 | #if defined(DEBUG_BINDING_LEVELS) |
8d08fdba | 810 | if (0) |
e2500fed | 811 | #else /* !defined(DEBUG_BINDING_LEVELS) */ |
8d08fdba | 812 | if (free_binding_level) |
e2500fed | 813 | #endif /* !defined(DEBUG_BINDING_LEVELS) */ |
8d08fdba MS |
814 | { |
815 | newlevel = free_binding_level; | |
816 | free_binding_level = free_binding_level->level_chain; | |
817 | } | |
818 | else | |
acef433b | 819 | newlevel = make_binding_level (); |
cffa8729 | 820 | |
8d08fdba | 821 | push_binding_level (newlevel, tag_transparent, keep_next_level_flag); |
8d08fdba MS |
822 | keep_next_level_flag = 0; |
823 | } | |
824 | ||
bd0d5d4a JM |
825 | /* We're defining an object of type TYPE. If it needs a cleanup, but |
826 | we're not allowed to add any more objects with cleanups to the current | |
827 | scope, create a new binding level. */ | |
828 | ||
829 | void | |
830 | maybe_push_cleanup_level (type) | |
831 | tree type; | |
832 | { | |
833 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) | |
834 | && current_binding_level->more_cleanups_ok == 0) | |
835 | { | |
836 | keep_next_level (2); | |
837 | pushlevel (1); | |
838 | clear_last_expr (); | |
839 | add_scope_stmt (/*begin_p=*/1, /*partial_p=*/1); | |
840 | } | |
841 | } | |
842 | ||
74b846e0 MM |
843 | /* Enter a new scope. The KIND indicates what kind of scope is being |
844 | created. */ | |
845 | ||
846 | void | |
847 | begin_scope (sk) | |
848 | scope_kind sk; | |
849 | { | |
850 | pushlevel (0); | |
851 | ||
852 | switch (sk) | |
853 | { | |
854 | case sk_template_spec: | |
855 | current_binding_level->template_spec_p = 1; | |
856 | /* Fall through. */ | |
857 | ||
858 | case sk_template_parms: | |
859 | current_binding_level->template_parms_p = 1; | |
860 | break; | |
861 | ||
862 | default: | |
a98facb0 | 863 | abort (); |
74b846e0 MM |
864 | } |
865 | } | |
866 | ||
867 | /* Exit the current scope. */ | |
868 | ||
869 | void | |
870 | finish_scope () | |
871 | { | |
872 | poplevel (0, 0, 0); | |
873 | } | |
874 | ||
5566b478 | 875 | void |
8d6e462b PB |
876 | note_level_for_for () |
877 | { | |
878 | current_binding_level->is_for_scope = 1; | |
879 | } | |
880 | ||
6625cdb5 JM |
881 | /* Record that the current binding level represents a try block. */ |
882 | ||
883 | void | |
826840d9 | 884 | note_level_for_try () |
6625cdb5 | 885 | { |
826840d9 RH |
886 | current_binding_level->is_try_scope = 1; |
887 | } | |
888 | ||
889 | /* Record that the current binding level represents a catch block. */ | |
890 | ||
891 | void | |
892 | note_level_for_catch () | |
893 | { | |
894 | current_binding_level->is_catch_scope = 1; | |
6625cdb5 JM |
895 | } |
896 | ||
f181d4ae | 897 | /* For a binding between a name and an entity at a block scope, |
e2500fed | 898 | this is the `struct cp_binding_level' for the block. */ |
f181d4ae | 899 | #define BINDING_LEVEL(NODE) \ |
75ac8dec | 900 | (((struct tree_binding*)(NODE))->scope.level) |
f181d4ae | 901 | |
4890c2f4 MM |
902 | /* A free list of CPLUS_BINDING nodes, connected by their |
903 | TREE_CHAINs. */ | |
904 | ||
e2500fed | 905 | static GTY((deletable (""))) tree free_bindings; |
4890c2f4 | 906 | |
f181d4ae MM |
907 | /* Make DECL the innermost binding for ID. The LEVEL is the binding |
908 | level at which this declaration is being bound. */ | |
909 | ||
910 | static void | |
911 | push_binding (id, decl, level) | |
912 | tree id; | |
913 | tree decl; | |
e2500fed | 914 | struct cp_binding_level* level; |
f181d4ae MM |
915 | { |
916 | tree binding; | |
917 | ||
4890c2f4 MM |
918 | if (free_bindings) |
919 | { | |
920 | binding = free_bindings; | |
921 | free_bindings = TREE_CHAIN (binding); | |
922 | } | |
923 | else | |
924 | binding = make_node (CPLUS_BINDING); | |
f181d4ae MM |
925 | |
926 | /* Now, fill in the binding information. */ | |
927 | BINDING_VALUE (binding) = decl; | |
d8f8dca1 | 928 | BINDING_TYPE (binding) = NULL_TREE; |
f181d4ae | 929 | BINDING_LEVEL (binding) = level; |
8f032717 | 930 | INHERITED_VALUE_BINDING_P (binding) = 0; |
f181d4ae | 931 | LOCAL_BINDING_P (binding) = (level != class_binding_level); |
87e3dbc9 | 932 | BINDING_HAS_LEVEL_P (binding) = 1; |
f181d4ae | 933 | |
c45df9c1 | 934 | /* And put it on the front of the list of bindings for ID. */ |
f181d4ae MM |
935 | TREE_CHAIN (binding) = IDENTIFIER_BINDING (id); |
936 | IDENTIFIER_BINDING (id) = binding; | |
937 | } | |
938 | ||
d8f8dca1 MM |
939 | /* ID is already bound in the current scope. But, DECL is an |
940 | additional binding for ID in the same scope. This is the `struct | |
941 | stat' hack whereby a non-typedef class-name or enum-name can be | |
942 | bound at the same level as some other kind of entity. It's the | |
943 | responsibility of the caller to check that inserting this name is | |
0e339752 | 944 | valid here. Returns nonzero if the new binding was successful. */ |
8f032717 | 945 | static int |
d8f8dca1 MM |
946 | add_binding (id, decl) |
947 | tree id; | |
948 | tree decl; | |
949 | { | |
950 | tree binding = IDENTIFIER_BINDING (id); | |
8f032717 | 951 | int ok = 1; |
d8f8dca1 MM |
952 | |
953 | if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl)) | |
954 | /* The new name is the type name. */ | |
955 | BINDING_TYPE (binding) = decl; | |
8f032717 MM |
956 | else if (!BINDING_VALUE (binding)) |
957 | /* This situation arises when push_class_level_binding moves an | |
958 | inherited type-binding out of the way to make room for a new | |
959 | value binding. */ | |
960 | BINDING_VALUE (binding) = decl; | |
961 | else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL | |
962 | && DECL_ARTIFICIAL (BINDING_VALUE (binding))) | |
963 | { | |
964 | /* The old binding was a type name. It was placed in | |
965 | BINDING_VALUE because it was thought, at the point it was | |
966 | declared, to be the only entity with such a name. Move the | |
967 | type name into the type slot; it is now hidden by the new | |
968 | binding. */ | |
d8f8dca1 MM |
969 | BINDING_TYPE (binding) = BINDING_VALUE (binding); |
970 | BINDING_VALUE (binding) = decl; | |
8f032717 | 971 | INHERITED_VALUE_BINDING_P (binding) = 0; |
d8f8dca1 | 972 | } |
263505af MM |
973 | else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL |
974 | && TREE_CODE (decl) == TYPE_DECL | |
975 | && DECL_NAME (decl) == DECL_NAME (BINDING_VALUE (binding)) | |
9aaceb4b NS |
976 | && (same_type_p (TREE_TYPE (decl), |
977 | TREE_TYPE (BINDING_VALUE (binding))) | |
978 | /* If either type involves template parameters, we must | |
979 | wait until instantiation. */ | |
980 | || uses_template_parms (TREE_TYPE (decl)) | |
981 | || uses_template_parms (TREE_TYPE (BINDING_VALUE (binding))))) | |
263505af MM |
982 | /* We have two typedef-names, both naming the same type to have |
983 | the same name. This is OK because of: | |
984 | ||
985 | [dcl.typedef] | |
986 | ||
987 | In a given scope, a typedef specifier can be used to redefine | |
988 | the name of any type declared in that scope to refer to the | |
989 | type to which it already refers. */ | |
990 | ok = 0; | |
7b176381 MM |
991 | /* There can be two block-scope declarations of the same variable, |
992 | so long as they are `extern' declarations. */ | |
993 | else if (TREE_CODE (decl) == VAR_DECL | |
994 | && TREE_CODE (BINDING_VALUE (binding)) == VAR_DECL | |
995 | && DECL_EXTERNAL (decl) | |
996 | && DECL_EXTERNAL (BINDING_VALUE (binding))) | |
997 | { | |
998 | duplicate_decls (decl, BINDING_VALUE (binding)); | |
999 | ok = 0; | |
1000 | } | |
8f032717 MM |
1001 | else |
1002 | { | |
33bd39a2 | 1003 | error ("declaration of `%#D'", decl); |
8f032717 MM |
1004 | cp_error_at ("conflicts with previous declaration `%#D'", |
1005 | BINDING_VALUE (binding)); | |
1006 | ok = 0; | |
1007 | } | |
1008 | ||
1009 | return ok; | |
d8f8dca1 MM |
1010 | } |
1011 | ||
cfe507be MM |
1012 | /* Add DECL to the list of things declared in B. */ |
1013 | ||
1014 | static void | |
1015 | add_decl_to_level (decl, b) | |
1016 | tree decl; | |
e2500fed | 1017 | struct cp_binding_level *b; |
cfe507be | 1018 | { |
70b76b34 DP |
1019 | if (TREE_CODE (decl) == NAMESPACE_DECL |
1020 | && !DECL_NAMESPACE_ALIAS (decl)) | |
1021 | { | |
1022 | TREE_CHAIN (decl) = b->namespaces; | |
1023 | b->namespaces = decl; | |
1024 | } | |
1025 | else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl)) | |
1026 | { | |
1027 | TREE_CHAIN (decl) = b->vtables; | |
1028 | b->vtables = decl; | |
1029 | } | |
1030 | else | |
1031 | { | |
1032 | /* We build up the list in reverse order, and reverse it later if | |
1033 | necessary. */ | |
1034 | TREE_CHAIN (decl) = b->names; | |
1035 | b->names = decl; | |
1036 | b->names_size++; | |
1037 | } | |
cfe507be MM |
1038 | } |
1039 | ||
1040 | /* Bind DECL to ID in the current_binding_level, assumed to be a local | |
1041 | binding level. If PUSH_USING is set in FLAGS, we know that DECL | |
1042 | doesn't really belong to this binding level, that it got here | |
1043 | through a using-declaration. */ | |
f181d4ae MM |
1044 | |
1045 | void | |
0034cf72 | 1046 | push_local_binding (id, decl, flags) |
f181d4ae MM |
1047 | tree id; |
1048 | tree decl; | |
0034cf72 | 1049 | int flags; |
f181d4ae | 1050 | { |
e2500fed | 1051 | struct cp_binding_level *b; |
8f032717 MM |
1052 | |
1053 | /* Skip over any local classes. This makes sense if we call | |
1054 | push_local_binding with a friend decl of a local class. */ | |
1055 | b = current_binding_level; | |
1056 | while (b->parm_flag == 2) | |
1057 | b = b->level_chain; | |
f181d4ae | 1058 | |
d8f8dca1 | 1059 | if (lookup_name_current_level (id)) |
8f032717 MM |
1060 | { |
1061 | /* Supplement the existing binding. */ | |
1062 | if (!add_binding (id, decl)) | |
1063 | /* It didn't work. Something else must be bound at this | |
1064 | level. Do not add DECL to the list of things to pop | |
1065 | later. */ | |
1066 | return; | |
1067 | } | |
d8f8dca1 MM |
1068 | else |
1069 | /* Create a new binding. */ | |
8f032717 | 1070 | push_binding (id, decl, b); |
f181d4ae | 1071 | |
0034cf72 | 1072 | if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING)) |
a06d48ef JM |
1073 | /* We must put the OVERLOAD into a TREE_LIST since the |
1074 | TREE_CHAIN of an OVERLOAD is already used. Similarly for | |
1075 | decls that got here through a using-declaration. */ | |
1076 | decl = build_tree_list (NULL_TREE, decl); | |
1077 | ||
f181d4ae MM |
1078 | /* And put DECL on the list of things declared by the current |
1079 | binding level. */ | |
cfe507be | 1080 | add_decl_to_level (decl, b); |
f181d4ae MM |
1081 | } |
1082 | ||
8f032717 MM |
1083 | /* Bind DECL to ID in the class_binding_level. Returns nonzero if the |
1084 | binding was successful. */ | |
f181d4ae | 1085 | |
8f032717 | 1086 | int |
f181d4ae MM |
1087 | push_class_binding (id, decl) |
1088 | tree id; | |
1089 | tree decl; | |
1090 | { | |
8f032717 MM |
1091 | int result = 1; |
1092 | tree binding = IDENTIFIER_BINDING (id); | |
1093 | tree context; | |
1094 | ||
1095 | /* Note that we declared this value so that we can issue an error if | |
0e339752 | 1096 | this is an invalid redeclaration of a name already used for some |
8f032717 MM |
1097 | other purpose. */ |
1098 | note_name_declared_in_class (id, decl); | |
1099 | ||
1100 | if (binding && BINDING_LEVEL (binding) == class_binding_level) | |
d8f8dca1 | 1101 | /* Supplement the existing binding. */ |
8f032717 | 1102 | result = add_binding (id, decl); |
d8f8dca1 MM |
1103 | else |
1104 | /* Create a new binding. */ | |
1105 | push_binding (id, decl, class_binding_level); | |
1106 | ||
1107 | /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the | |
1108 | class-level declaration. Note that we do not use DECL here | |
1109 | because of the possibility of the `struct stat' hack; if DECL is | |
1110 | a class-name or enum-name we might prefer a field-name, or some | |
1111 | such. */ | |
1112 | IDENTIFIER_CLASS_VALUE (id) = BINDING_VALUE (IDENTIFIER_BINDING (id)); | |
8f032717 MM |
1113 | |
1114 | /* If this is a binding from a base class, mark it as such. */ | |
1115 | binding = IDENTIFIER_BINDING (id); | |
1116 | if (BINDING_VALUE (binding) == decl && TREE_CODE (decl) != TREE_LIST) | |
1117 | { | |
280f9385 MM |
1118 | /* Any implicit typename must be from a base-class. The |
1119 | context for an implicit typename declaration is always | |
1120 | the derived class in which the lookup was done, so the checks | |
1121 | based on the context of DECL below will not trigger. */ | |
83233dca | 1122 | if (IMPLICIT_TYPENAME_TYPE_DECL_P (decl)) |
280f9385 | 1123 | INHERITED_VALUE_BINDING_P (binding) = 1; |
8f032717 MM |
1124 | else |
1125 | { | |
280f9385 | 1126 | if (TREE_CODE (decl) == OVERLOAD) |
4f1c5b7d | 1127 | context = CP_DECL_CONTEXT (OVL_CURRENT (decl)); |
280f9385 MM |
1128 | else |
1129 | { | |
2f939d94 | 1130 | my_friendly_assert (DECL_P (decl), 0); |
55de1b66 | 1131 | context = context_for_name_lookup (decl); |
280f9385 | 1132 | } |
8f032717 | 1133 | |
280f9385 MM |
1134 | if (is_properly_derived_from (current_class_type, context)) |
1135 | INHERITED_VALUE_BINDING_P (binding) = 1; | |
1136 | else | |
1137 | INHERITED_VALUE_BINDING_P (binding) = 0; | |
1138 | } | |
8f032717 MM |
1139 | } |
1140 | else if (BINDING_VALUE (binding) == decl) | |
1141 | /* We only encounter a TREE_LIST when push_class_decls detects an | |
1142 | ambiguity. Such an ambiguity can be overridden by a definition | |
1143 | in this class. */ | |
1144 | INHERITED_VALUE_BINDING_P (binding) = 1; | |
1145 | ||
1146 | return result; | |
f181d4ae MM |
1147 | } |
1148 | ||
d8f8dca1 MM |
1149 | /* Remove the binding for DECL which should be the innermost binding |
1150 | for ID. */ | |
f181d4ae | 1151 | |
68642fb6 UD |
1152 | static void |
1153 | pop_binding (id, decl) | |
f181d4ae | 1154 | tree id; |
d8f8dca1 | 1155 | tree decl; |
f181d4ae MM |
1156 | { |
1157 | tree binding; | |
68642fb6 | 1158 | |
f181d4ae MM |
1159 | if (id == NULL_TREE) |
1160 | /* It's easiest to write the loops that call this function without | |
1161 | checking whether or not the entities involved have names. We | |
1162 | get here for such an entity. */ | |
1163 | return; | |
1164 | ||
d8f8dca1 | 1165 | /* Get the innermost binding for ID. */ |
f181d4ae | 1166 | binding = IDENTIFIER_BINDING (id); |
f181d4ae | 1167 | |
d8f8dca1 MM |
1168 | /* The name should be bound. */ |
1169 | my_friendly_assert (binding != NULL_TREE, 0); | |
1170 | ||
1171 | /* The DECL will be either the ordinary binding or the type | |
1172 | binding for this identifier. Remove that binding. */ | |
1173 | if (BINDING_VALUE (binding) == decl) | |
1174 | BINDING_VALUE (binding) = NULL_TREE; | |
1175 | else if (BINDING_TYPE (binding) == decl) | |
1176 | BINDING_TYPE (binding) = NULL_TREE; | |
1177 | else | |
a98facb0 | 1178 | abort (); |
d8f8dca1 MM |
1179 | |
1180 | if (!BINDING_VALUE (binding) && !BINDING_TYPE (binding)) | |
4890c2f4 MM |
1181 | { |
1182 | /* We're completely done with the innermost binding for this | |
1183 | identifier. Unhook it from the list of bindings. */ | |
1184 | IDENTIFIER_BINDING (id) = TREE_CHAIN (binding); | |
1185 | ||
1186 | /* Add it to the free list. */ | |
1187 | TREE_CHAIN (binding) = free_bindings; | |
1188 | free_bindings = binding; | |
1189 | ||
1190 | /* Clear the BINDING_LEVEL so the garbage collector doesn't walk | |
1191 | it. */ | |
1192 | BINDING_LEVEL (binding) = NULL; | |
1193 | } | |
f181d4ae MM |
1194 | } |
1195 | ||
acef433b MM |
1196 | /* When a label goes out of scope, check to see if that label was used |
1197 | in a valid manner, and issue any appropriate warnings or errors. */ | |
1198 | ||
1199 | static void | |
6625cdb5 JM |
1200 | pop_label (label, old_value) |
1201 | tree label; | |
1202 | tree old_value; | |
acef433b | 1203 | { |
88848bde | 1204 | if (!processing_template_decl && doing_semantic_analysis_p ()) |
acef433b | 1205 | { |
88848bde MM |
1206 | if (DECL_INITIAL (label) == NULL_TREE) |
1207 | { | |
1208 | cp_error_at ("label `%D' used but not defined", label); | |
1209 | /* Avoid crashing later. */ | |
1210 | define_label (input_filename, 1, DECL_NAME (label)); | |
1211 | } | |
078721e1 | 1212 | else if (warn_unused_label && !TREE_USED (label)) |
88848bde | 1213 | cp_warning_at ("label `%D' defined but not used", label); |
acef433b | 1214 | } |
acef433b | 1215 | |
6625cdb5 | 1216 | SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value); |
acef433b MM |
1217 | } |
1218 | ||
0811ea8f | 1219 | /* At the end of a function, all labels declared within the function |
68642fb6 | 1220 | go out of scope. BLOCK is the top-level block for the |
acef433b MM |
1221 | function. */ |
1222 | ||
1223 | static void | |
1224 | pop_labels (block) | |
1225 | tree block; | |
1226 | { | |
6625cdb5 | 1227 | struct named_label_list *link; |
acef433b MM |
1228 | |
1229 | /* Clear out the definitions of all label names, since their scopes | |
1230 | end here. */ | |
6625cdb5 | 1231 | for (link = named_labels; link; link = link->next) |
acef433b | 1232 | { |
6625cdb5 | 1233 | pop_label (link->label_decl, link->old_value); |
acef433b MM |
1234 | /* Put the labels into the "variables" of the top-level block, |
1235 | so debugger can see them. */ | |
6625cdb5 JM |
1236 | TREE_CHAIN (link->label_decl) = BLOCK_VARS (block); |
1237 | BLOCK_VARS (block) = link->label_decl; | |
acef433b MM |
1238 | } |
1239 | ||
6625cdb5 | 1240 | named_labels = NULL; |
acef433b MM |
1241 | } |
1242 | ||
8d08fdba MS |
1243 | /* Exit a binding level. |
1244 | Pop the level off, and restore the state of the identifier-decl mappings | |
1245 | that were in effect when this level was entered. | |
1246 | ||
1247 | If KEEP == 1, this level had explicit declarations, so | |
1248 | and create a "block" (a BLOCK node) for the level | |
1249 | to record its declarations and subblocks for symbol table output. | |
1250 | ||
8d08fdba MS |
1251 | If FUNCTIONBODY is nonzero, this level is the body of a function, |
1252 | so create a block as if KEEP were set and also clear out all | |
1253 | label names. | |
1254 | ||
1255 | If REVERSE is nonzero, reverse the order of decls before putting | |
1256 | them into the BLOCK. */ | |
1257 | ||
1258 | tree | |
1259 | poplevel (keep, reverse, functionbody) | |
1260 | int keep; | |
1261 | int reverse; | |
1262 | int functionbody; | |
1263 | { | |
1264 | register tree link; | |
1265 | /* The chain of decls was accumulated in reverse order. | |
1266 | Put it into forward order, just for cleanliness. */ | |
1267 | tree decls; | |
1268 | int tmp = functionbody; | |
b35d4555 MM |
1269 | int real_functionbody; |
1270 | tree tags; | |
1271 | tree subblocks; | |
8d08fdba MS |
1272 | tree block = NULL_TREE; |
1273 | tree decl; | |
f181d4ae MM |
1274 | int leaving_for_scope; |
1275 | ||
01d939e8 | 1276 | if (cfun && !doing_semantic_analysis_p ()) |
b35d4555 MM |
1277 | return NULL_TREE; |
1278 | ||
273a708f MM |
1279 | my_friendly_assert (current_binding_level->parm_flag != 2, |
1280 | 19990916); | |
b74a0560 | 1281 | |
b35d4555 MM |
1282 | real_functionbody = (current_binding_level->keep == 2 |
1283 | ? ((functionbody = 0), tmp) : functionbody); | |
1284 | tags = functionbody >= 0 ? current_binding_level->tags : 0; | |
1285 | subblocks = functionbody >= 0 ? current_binding_level->blocks : 0; | |
1286 | ||
b74a0560 MM |
1287 | my_friendly_assert (!current_binding_level->class_shadowed, |
1288 | 19990414); | |
8d08fdba | 1289 | |
536333d4 MM |
1290 | /* We used to use KEEP == 2 to indicate that the new block should go |
1291 | at the beginning of the list of blocks at this binding level, | |
1292 | rather than the end. This hack is no longer used. */ | |
1293 | my_friendly_assert (keep == 0 || keep == 1, 0); | |
1294 | ||
8d08fdba MS |
1295 | if (current_binding_level->keep == 1) |
1296 | keep = 1; | |
1297 | ||
6625cdb5 JM |
1298 | /* Any uses of undefined labels, and any defined labels, now operate |
1299 | under constraints of next binding contour. */ | |
1300 | if (cfun && !functionbody) | |
1301 | { | |
e2500fed | 1302 | struct cp_binding_level *level_chain; |
6625cdb5 JM |
1303 | level_chain = current_binding_level->level_chain; |
1304 | if (level_chain) | |
1305 | { | |
1306 | struct named_label_use_list *uses; | |
1307 | struct named_label_list *labels; | |
1308 | for (labels = named_labels; labels; labels = labels->next) | |
1309 | if (labels->binding_level == current_binding_level) | |
1310 | { | |
1311 | tree decl; | |
826840d9 RH |
1312 | if (current_binding_level->is_try_scope) |
1313 | labels->in_try_scope = 1; | |
1314 | if (current_binding_level->is_catch_scope) | |
1315 | labels->in_catch_scope = 1; | |
6625cdb5 JM |
1316 | for (decl = labels->names_in_scope; decl; |
1317 | decl = TREE_CHAIN (decl)) | |
1318 | if (decl_jump_unsafe (decl)) | |
1319 | labels->bad_decls = tree_cons (NULL_TREE, decl, | |
1320 | labels->bad_decls); | |
1321 | labels->binding_level = level_chain; | |
1322 | labels->names_in_scope = level_chain->names; | |
1323 | } | |
1324 | ||
1325 | for (uses = named_label_uses; uses; uses = uses->next) | |
1326 | if (uses->binding_level == current_binding_level) | |
1327 | { | |
1328 | uses->binding_level = level_chain; | |
1329 | uses->names_in_scope = level_chain->names; | |
1330 | } | |
1331 | } | |
1332 | } | |
1333 | ||
8d08fdba MS |
1334 | /* Get the decls in the order they were written. |
1335 | Usually current_binding_level->names is in reverse order. | |
1336 | But parameter decls were previously put in forward order. */ | |
1337 | ||
1338 | if (reverse) | |
1339 | current_binding_level->names | |
1340 | = decls = nreverse (current_binding_level->names); | |
1341 | else | |
1342 | decls = current_binding_level->names; | |
1343 | ||
1344 | /* Output any nested inline functions within this block | |
1345 | if they weren't already output. */ | |
8d08fdba MS |
1346 | for (decl = decls; decl; decl = TREE_CHAIN (decl)) |
1347 | if (TREE_CODE (decl) == FUNCTION_DECL | |
1348 | && ! TREE_ASM_WRITTEN (decl) | |
1349 | && DECL_INITIAL (decl) != NULL_TREE | |
6060a796 MS |
1350 | && TREE_ADDRESSABLE (decl) |
1351 | && decl_function_context (decl) == current_function_decl) | |
8d08fdba MS |
1352 | { |
1353 | /* If this decl was copied from a file-scope decl | |
1354 | on account of a block-scope extern decl, | |
1355 | propagate TREE_ADDRESSABLE to the file-scope decl. */ | |
1356 | if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE) | |
1357 | TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1; | |
1358 | else | |
1359 | { | |
1360 | push_function_context (); | |
1361 | output_inline_function (decl); | |
1362 | pop_function_context (); | |
1363 | } | |
1364 | } | |
1365 | ||
d9b2d9da MM |
1366 | /* When not in function-at-a-time mode, expand_end_bindings will |
1367 | warn about unused variables. But, in function-at-a-time mode | |
1368 | expand_end_bindings is not passed the list of variables in the | |
1369 | current scope, and therefore no warning is emitted. So, we | |
1370 | explicitly warn here. */ | |
1371 | if (!processing_template_decl) | |
1372 | warn_about_unused_variables (getdecls ()); | |
1373 | ||
8d08fdba MS |
1374 | /* If there were any declarations or structure tags in that level, |
1375 | or if this level is a function body, | |
1376 | create a BLOCK to record them for the life of this function. */ | |
8d08fdba | 1377 | block = NULL_TREE; |
f444e36b | 1378 | if (keep == 1 || functionbody) |
8d08fdba MS |
1379 | block = make_node (BLOCK); |
1380 | if (block != NULL_TREE) | |
1381 | { | |
f444e36b MM |
1382 | BLOCK_VARS (block) = decls; |
1383 | BLOCK_SUBBLOCKS (block) = subblocks; | |
8d08fdba MS |
1384 | } |
1385 | ||
1386 | /* In each subblock, record that this is its superior. */ | |
8d08fdba MS |
1387 | if (keep >= 0) |
1388 | for (link = subblocks; link; link = TREE_CHAIN (link)) | |
1389 | BLOCK_SUPERCONTEXT (link) = block; | |
1390 | ||
f181d4ae MM |
1391 | /* We still support the old for-scope rules, whereby the variables |
1392 | in a for-init statement were in scope after the for-statement | |
1393 | ended. We only use the new rules in flag_new_for_scope is | |
1394 | nonzero. */ | |
68642fb6 | 1395 | leaving_for_scope |
f181d4ae MM |
1396 | = current_binding_level->is_for_scope && flag_new_for_scope == 1; |
1397 | ||
1398 | /* Remove declarations for all the DECLs in this level. */ | |
1399 | for (link = decls; link; link = TREE_CHAIN (link)) | |
1400 | { | |
c3783399 NS |
1401 | if (leaving_for_scope && TREE_CODE (link) == VAR_DECL |
1402 | && DECL_NAME (link)) | |
f181d4ae | 1403 | { |
68642fb6 | 1404 | tree outer_binding |
f181d4ae MM |
1405 | = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (link))); |
1406 | tree ns_binding; | |
1407 | ||
1408 | if (!outer_binding) | |
1409 | ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link)); | |
c7dda1e3 MM |
1410 | else |
1411 | ns_binding = NULL_TREE; | |
1412 | ||
68642fb6 UD |
1413 | if (outer_binding |
1414 | && (BINDING_LEVEL (outer_binding) | |
f181d4ae MM |
1415 | == current_binding_level->level_chain)) |
1416 | /* We have something like: | |
68642fb6 | 1417 | |
f181d4ae MM |
1418 | int i; |
1419 | for (int i; ;); | |
68642fb6 | 1420 | |
f181d4ae MM |
1421 | and we are leaving the `for' scope. There's no reason to |
1422 | keep the binding of the inner `i' in this case. */ | |
d8f8dca1 | 1423 | pop_binding (DECL_NAME (link), link); |
68642fb6 UD |
1424 | else if ((outer_binding |
1425 | && (TREE_CODE (BINDING_VALUE (outer_binding)) | |
f181d4ae | 1426 | == TYPE_DECL)) |
68642fb6 | 1427 | || (ns_binding |
f181d4ae MM |
1428 | && TREE_CODE (ns_binding) == TYPE_DECL)) |
1429 | /* Here, we have something like: | |
1430 | ||
1431 | typedef int I; | |
1432 | ||
1433 | void f () { | |
1434 | for (int I; ;); | |
1435 | } | |
1436 | ||
1437 | We must pop the for-scope binding so we know what's a | |
1438 | type and what isn't. */ | |
d8f8dca1 | 1439 | pop_binding (DECL_NAME (link), link); |
e76a2646 | 1440 | else |
e76a2646 | 1441 | { |
f181d4ae MM |
1442 | /* Mark this VAR_DECL as dead so that we can tell we left it |
1443 | there only for backward compatibility. */ | |
1444 | DECL_DEAD_FOR_LOCAL (link) = 1; | |
68642fb6 | 1445 | |
f181d4ae MM |
1446 | /* Keep track of what should of have happenned when we |
1447 | popped the binding. */ | |
1448 | if (outer_binding && BINDING_VALUE (outer_binding)) | |
68642fb6 | 1449 | DECL_SHADOWED_FOR_VAR (link) |
f181d4ae MM |
1450 | = BINDING_VALUE (outer_binding); |
1451 | ||
1452 | /* Add it to the list of dead variables in the next | |
1453 | outermost binding to that we can remove these when we | |
1454 | leave that binding. */ | |
1455 | current_binding_level->level_chain->dead_vars_from_for | |
1456 | = tree_cons (NULL_TREE, link, | |
1457 | current_binding_level->level_chain-> | |
1458 | dead_vars_from_for); | |
1459 | ||
1460 | /* Although we don't pop the CPLUS_BINDING, we do clear | |
1461 | its BINDING_LEVEL since the level is going away now. */ | |
1462 | BINDING_LEVEL (IDENTIFIER_BINDING (DECL_NAME (link))) | |
1463 | = 0; | |
e76a2646 | 1464 | } |
2ee887f2 | 1465 | } |
68642fb6 | 1466 | else |
8d6e462b | 1467 | { |
f181d4ae | 1468 | /* Remove the binding. */ |
0034cf72 JM |
1469 | decl = link; |
1470 | if (TREE_CODE (decl) == TREE_LIST) | |
1471 | decl = TREE_VALUE (decl); | |
2f939d94 | 1472 | if (DECL_P (decl)) |
0034cf72 JM |
1473 | pop_binding (DECL_NAME (decl), decl); |
1474 | else if (TREE_CODE (decl) == OVERLOAD) | |
1475 | pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl); | |
68642fb6 | 1476 | else |
a98facb0 | 1477 | abort (); |
8d08fdba | 1478 | } |
f181d4ae | 1479 | } |
8d08fdba | 1480 | |
f181d4ae MM |
1481 | /* Remove declarations for any `for' variables from inner scopes |
1482 | that we kept around. */ | |
1483 | for (link = current_binding_level->dead_vars_from_for; | |
1484 | link; link = TREE_CHAIN (link)) | |
d8f8dca1 | 1485 | pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link)); |
2ee887f2 | 1486 | |
f181d4ae MM |
1487 | /* Restore the IDENTIFIER_TYPE_VALUEs. */ |
1488 | for (link = current_binding_level->type_shadowed; | |
1489 | link; link = TREE_CHAIN (link)) | |
1490 | SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link)); | |
acef433b MM |
1491 | |
1492 | /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */ | |
1493 | for (link = current_binding_level->shadowed_labels; | |
68642fb6 | 1494 | link; |
acef433b | 1495 | link = TREE_CHAIN (link)) |
6625cdb5 | 1496 | pop_label (TREE_VALUE (link), TREE_PURPOSE (link)); |
acef433b | 1497 | |
f181d4ae MM |
1498 | /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs |
1499 | list if a `using' declaration put them there. The debugging | |
1500 | back-ends won't understand OVERLOAD, so we remove them here. | |
1501 | Because the BLOCK_VARS are (temporarily) shared with | |
1502 | CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have | |
1503 | popped all the bindings. */ | |
1504 | if (block) | |
1505 | { | |
1506 | tree* d; | |
8d6e462b | 1507 | |
a06d48ef JM |
1508 | for (d = &BLOCK_VARS (block); *d; ) |
1509 | { | |
1510 | if (TREE_CODE (*d) == TREE_LIST) | |
1511 | *d = TREE_CHAIN (*d); | |
1512 | else | |
1513 | d = &TREE_CHAIN (*d); | |
1514 | } | |
8d6e462b | 1515 | } |
8d08fdba MS |
1516 | |
1517 | /* If the level being exited is the top level of a function, | |
1518 | check over all the labels. */ | |
8d08fdba MS |
1519 | if (functionbody) |
1520 | { | |
acef433b MM |
1521 | /* Since this is the top level block of a function, the vars are |
1522 | the function's parameters. Don't leave them in the BLOCK | |
1523 | because they are found in the FUNCTION_DECL instead. */ | |
8d08fdba | 1524 | BLOCK_VARS (block) = 0; |
acef433b | 1525 | pop_labels (block); |
8d08fdba MS |
1526 | } |
1527 | ||
8d08fdba MS |
1528 | tmp = current_binding_level->keep; |
1529 | ||
1530 | pop_binding_level (); | |
1531 | if (functionbody) | |
1532 | DECL_INITIAL (current_function_decl) = block; | |
1533 | else if (block) | |
f444e36b MM |
1534 | current_binding_level->blocks |
1535 | = chainon (current_binding_level->blocks, block); | |
1536 | ||
8d08fdba MS |
1537 | /* If we did not make a block for the level just exited, |
1538 | any blocks made for inner levels | |
1539 | (since they cannot be recorded as subblocks in that level) | |
1540 | must be carried forward so they will later become subblocks | |
1541 | of something else. */ | |
1542 | else if (subblocks) | |
536333d4 MM |
1543 | current_binding_level->blocks |
1544 | = chainon (current_binding_level->blocks, subblocks); | |
8d08fdba | 1545 | |
d9b2d9da MM |
1546 | /* Each and every BLOCK node created here in `poplevel' is important |
1547 | (e.g. for proper debugging information) so if we created one | |
1548 | earlier, mark it as "used". */ | |
1549 | if (block) | |
1550 | TREE_USED (block) = 1; | |
1551 | ||
8d08fdba | 1552 | /* Take care of compiler's internal binding structures. */ |
a4443a08 | 1553 | if (tmp == 2) |
8d08fdba | 1554 | { |
d9b2d9da MM |
1555 | tree scope_stmts; |
1556 | ||
68642fb6 | 1557 | scope_stmts |
d9b2d9da | 1558 | = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/1); |
8d08fdba | 1559 | if (block) |
d9b2d9da MM |
1560 | { |
1561 | SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block; | |
1562 | SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block; | |
1563 | } | |
1564 | ||
8f471b0d | 1565 | block = poplevel (keep, reverse, functionbody); |
8d08fdba MS |
1566 | } |
1567 | ||
8d08fdba MS |
1568 | return block; |
1569 | } | |
1570 | ||
1571 | /* Delete the node BLOCK from the current binding level. | |
1572 | This is used for the block inside a stmt expr ({...}) | |
1573 | so that the block can be reinserted where appropriate. */ | |
1574 | ||
1575 | void | |
1576 | delete_block (block) | |
1577 | tree block; | |
1578 | { | |
1579 | tree t; | |
1580 | if (current_binding_level->blocks == block) | |
1581 | current_binding_level->blocks = TREE_CHAIN (block); | |
1582 | for (t = current_binding_level->blocks; t;) | |
1583 | { | |
1584 | if (TREE_CHAIN (t) == block) | |
1585 | TREE_CHAIN (t) = TREE_CHAIN (block); | |
1586 | else | |
1587 | t = TREE_CHAIN (t); | |
1588 | } | |
1589 | TREE_CHAIN (block) = NULL_TREE; | |
1590 | /* Clear TREE_USED which is always set by poplevel. | |
1591 | The flag is set again if insert_block is called. */ | |
1592 | TREE_USED (block) = 0; | |
1593 | } | |
1594 | ||
1595 | /* Insert BLOCK at the end of the list of subblocks of the | |
1596 | current binding level. This is used when a BIND_EXPR is expanded, | |
1597 | to handle the BLOCK node inside the BIND_EXPR. */ | |
1598 | ||
1599 | void | |
1600 | insert_block (block) | |
1601 | tree block; | |
1602 | { | |
1603 | TREE_USED (block) = 1; | |
1604 | current_binding_level->blocks | |
1605 | = chainon (current_binding_level->blocks, block); | |
1606 | } | |
1607 | ||
8d08fdba MS |
1608 | /* Set the BLOCK node for the innermost scope |
1609 | (the one we are currently in). */ | |
1610 | ||
1611 | void | |
1612 | set_block (block) | |
f444e36b | 1613 | tree block ATTRIBUTE_UNUSED; |
8d08fdba | 1614 | { |
f444e36b MM |
1615 | /* The RTL expansion machinery requires us to provide this callback, |
1616 | but it is not applicable in function-at-a-time mode. */ | |
1617 | my_friendly_assert (cfun && !doing_semantic_analysis_p (), 20000911); | |
8d08fdba MS |
1618 | } |
1619 | ||
1620 | /* Do a pushlevel for class declarations. */ | |
e92cc029 | 1621 | |
8d08fdba MS |
1622 | void |
1623 | pushlevel_class () | |
1624 | { | |
e2500fed | 1625 | register struct cp_binding_level *newlevel; |
8d08fdba MS |
1626 | |
1627 | /* Reuse or create a struct for this binding level. */ | |
e2500fed | 1628 | #if defined(DEBUG_BINDING_LEVELS) |
8d08fdba | 1629 | if (0) |
e2500fed | 1630 | #else /* !defined(DEBUG_BINDING_LEVELS) */ |
8d08fdba | 1631 | if (free_binding_level) |
e2500fed | 1632 | #endif /* !defined(DEBUG_BINDING_LEVELS) */ |
8d08fdba MS |
1633 | { |
1634 | newlevel = free_binding_level; | |
1635 | free_binding_level = free_binding_level->level_chain; | |
1636 | } | |
1637 | else | |
f181d4ae | 1638 | newlevel = make_binding_level (); |
8d08fdba | 1639 | |
e2500fed | 1640 | #if defined(DEBUG_BINDING_LEVELS) |
8d08fdba | 1641 | is_class_level = 1; |
e2500fed | 1642 | #endif /* defined(DEBUG_BINDING_LEVELS) */ |
8d08fdba MS |
1643 | |
1644 | push_binding_level (newlevel, 0, 0); | |
1645 | ||
8d08fdba MS |
1646 | class_binding_level = current_binding_level; |
1647 | class_binding_level->parm_flag = 2; | |
70adf8a9 | 1648 | class_binding_level->this_class = current_class_type; |
8d08fdba MS |
1649 | } |
1650 | ||
b74a0560 | 1651 | /* ...and a poplevel for class declarations. */ |
e92cc029 | 1652 | |
273a708f | 1653 | void |
b74a0560 | 1654 | poplevel_class () |
8d08fdba | 1655 | { |
e2500fed | 1656 | register struct cp_binding_level *level = class_binding_level; |
8d08fdba MS |
1657 | tree shadowed; |
1658 | ||
1659 | my_friendly_assert (level != 0, 354); | |
68642fb6 | 1660 | |
8d08fdba | 1661 | /* If we're leaving a toplevel class, don't bother to do the setting |
ddd5a7c1 | 1662 | of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot |
8d08fdba | 1663 | shouldn't even be used when current_class_type isn't set, and second, |
ddd5a7c1 | 1664 | if we don't touch it here, we're able to use the cache effect if the |
8d08fdba | 1665 | next time we're entering a class scope, it is the same class. */ |
b74a0560 | 1666 | if (current_class_depth != 1) |
8f032717 | 1667 | { |
e2500fed | 1668 | struct cp_binding_level* b; |
8f032717 MM |
1669 | |
1670 | /* Clear out our IDENTIFIER_CLASS_VALUEs. */ | |
1671 | for (shadowed = level->class_shadowed; | |
1672 | shadowed; | |
1673 | shadowed = TREE_CHAIN (shadowed)) | |
1674 | IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE; | |
68642fb6 | 1675 | |
8f032717 MM |
1676 | /* Find the next enclosing class, and recreate |
1677 | IDENTIFIER_CLASS_VALUEs appropriate for that class. */ | |
1678 | b = level->level_chain; | |
1679 | while (b && b->parm_flag != 2) | |
1680 | b = b->level_chain; | |
1681 | ||
1682 | if (b) | |
68642fb6 UD |
1683 | for (shadowed = b->class_shadowed; |
1684 | shadowed; | |
8f032717 MM |
1685 | shadowed = TREE_CHAIN (shadowed)) |
1686 | { | |
1687 | tree t; | |
1688 | ||
1689 | t = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed)); | |
1690 | while (t && BINDING_LEVEL (t) != b) | |
1691 | t = TREE_CHAIN (t); | |
68642fb6 | 1692 | |
8f032717 | 1693 | if (t) |
68642fb6 | 1694 | IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) |
8f032717 MM |
1695 | = BINDING_VALUE (t); |
1696 | } | |
1697 | } | |
8d08fdba MS |
1698 | else |
1699 | /* Remember to save what IDENTIFIER's were bound in this scope so we | |
1700 | can recover from cache misses. */ | |
e76a2646 MS |
1701 | { |
1702 | previous_class_type = current_class_type; | |
1703 | previous_class_values = class_binding_level->class_shadowed; | |
1704 | } | |
8d08fdba MS |
1705 | for (shadowed = level->type_shadowed; |
1706 | shadowed; | |
1707 | shadowed = TREE_CHAIN (shadowed)) | |
2c73f9f5 | 1708 | SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed)); |
8d08fdba | 1709 | |
f181d4ae | 1710 | /* Remove the bindings for all of the class-level declarations. */ |
68642fb6 UD |
1711 | for (shadowed = level->class_shadowed; |
1712 | shadowed; | |
f181d4ae | 1713 | shadowed = TREE_CHAIN (shadowed)) |
d8f8dca1 | 1714 | pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed)); |
f181d4ae | 1715 | |
38e01259 | 1716 | /* Now, pop out of the binding level which we created up in the |
8d08fdba | 1717 | `pushlevel_class' routine. */ |
e2500fed | 1718 | #if defined(DEBUG_BINDING_LEVELS) |
8d08fdba | 1719 | is_class_level = 1; |
e2500fed | 1720 | #endif /* defined(DEBUG_BINDING_LEVELS) */ |
8d08fdba MS |
1721 | |
1722 | pop_binding_level (); | |
8d08fdba | 1723 | } |
8f032717 MM |
1724 | |
1725 | /* We are entering the scope of a class. Clear IDENTIFIER_CLASS_VALUE | |
1726 | for any names in enclosing classes. */ | |
1727 | ||
1728 | void | |
1729 | clear_identifier_class_values () | |
1730 | { | |
1731 | tree t; | |
1732 | ||
1733 | if (!class_binding_level) | |
1734 | return; | |
1735 | ||
1736 | for (t = class_binding_level->class_shadowed; | |
1737 | t; | |
1738 | t = TREE_CHAIN (t)) | |
1739 | IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE; | |
1740 | } | |
1741 | ||
838dfd8a | 1742 | /* Returns nonzero if T is a virtual function table. */ |
0aafb128 MM |
1743 | |
1744 | int | |
1745 | vtable_decl_p (t, data) | |
1746 | tree t; | |
1747 | void *data ATTRIBUTE_UNUSED; | |
1748 | { | |
1749 | return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t)); | |
1750 | } | |
1751 | ||
838dfd8a | 1752 | /* Returns nonzero if T is a TYPE_DECL for a type with virtual |
0aafb128 MM |
1753 | functions. */ |
1754 | ||
1755 | int | |
1756 | vtype_decl_p (t, data) | |
1757 | tree t; | |
1758 | void *data ATTRIBUTE_UNUSED; | |
1759 | { | |
1760 | return (TREE_CODE (t) == TYPE_DECL | |
34249844 NS |
1761 | && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE |
1762 | && TYPE_POLYMORPHIC_P (TREE_TYPE (t))); | |
0aafb128 MM |
1763 | } |
1764 | ||
f71f87f9 MM |
1765 | /* Return the declarations that are members of the namespace NS. */ |
1766 | ||
1767 | tree | |
1768 | cp_namespace_decls (ns) | |
1769 | tree ns; | |
1770 | { | |
1771 | return NAMESPACE_LEVEL (ns)->names; | |
1772 | } | |
1773 | ||
70b76b34 DP |
1774 | struct walk_globals_data { |
1775 | walk_globals_pred p; | |
1776 | walk_globals_fn f; | |
1777 | void *data; | |
1778 | }; | |
1779 | ||
1780 | /* Walk the vtable 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. */ | |
70b76b34 DP |
1783 | |
1784 | static int | |
1785 | walk_vtables_r (namespace, data) | |
1786 | tree namespace; | |
1787 | void *data; | |
1788 | { | |
1789 | struct walk_globals_data* wgd = (struct walk_globals_data *) data; | |
1790 | walk_globals_fn f = wgd->f; | |
1791 | void *d = wgd->data; | |
1792 | tree decl = NAMESPACE_LEVEL (namespace)->vtables; | |
1793 | int result = 0; | |
1794 | ||
1795 | for (; decl ; decl = TREE_CHAIN (decl)) | |
0246d49e | 1796 | result |= (*f) (&decl, d); |
70b76b34 DP |
1797 | |
1798 | return result; | |
1799 | } | |
1800 | ||
1801 | /* Walk the vtable declarations. Whenever one is found for which P | |
838dfd8a KH |
1802 | returns nonzero, call F with its address. If any call to F |
1803 | returns a nonzero value, return a nonzero value. */ | |
70b76b34 DP |
1804 | int |
1805 | walk_vtables (p, f, data) | |
1806 | walk_globals_pred p; | |
1807 | walk_globals_fn f; | |
1808 | void *data; | |
1809 | { | |
1810 | struct walk_globals_data wgd; | |
1811 | wgd.p = p; | |
1812 | wgd.f = f; | |
1813 | wgd.data = data; | |
1814 | ||
1815 | return walk_namespaces (walk_vtables_r, &wgd); | |
1816 | } | |
1817 | ||
0aafb128 MM |
1818 | /* Walk all the namespaces contained NAMESPACE, including NAMESPACE |
1819 | itself, calling F for each. The DATA is passed to F as well. */ | |
1820 | ||
1821 | static int | |
1822 | walk_namespaces_r (namespace, f, data) | |
1823 | tree namespace; | |
1824 | walk_namespaces_fn f; | |
1825 | void *data; | |
1826 | { | |
0aafb128 | 1827 | int result = 0; |
70b76b34 | 1828 | tree current = NAMESPACE_LEVEL (namespace)->namespaces; |
0aafb128 MM |
1829 | |
1830 | result |= (*f) (namespace, data); | |
1831 | ||
70b76b34 DP |
1832 | for (; current; current = TREE_CHAIN (current)) |
1833 | result |= walk_namespaces_r (current, f, data); | |
0aafb128 MM |
1834 | |
1835 | return result; | |
1836 | } | |
1837 | ||
1838 | /* Walk all the namespaces, calling F for each. The DATA is passed to | |
1839 | F as well. */ | |
1840 | ||
1841 | int | |
1842 | walk_namespaces (f, data) | |
1843 | walk_namespaces_fn f; | |
1844 | void *data; | |
1845 | { | |
1846 | return walk_namespaces_r (global_namespace, f, data); | |
1847 | } | |
1848 | ||
0aafb128 | 1849 | /* Walk the global declarations in NAMESPACE. Whenever one is found |
838dfd8a KH |
1850 | for which P returns nonzero, call F with its address. If any call |
1851 | to F returns a nonzero value, return a nonzero value. */ | |
0aafb128 | 1852 | |
68642fb6 | 1853 | static int |
0aafb128 MM |
1854 | walk_globals_r (namespace, data) |
1855 | tree namespace; | |
1856 | void *data; | |
1857 | { | |
1858 | struct walk_globals_data* wgd = (struct walk_globals_data *) data; | |
1859 | walk_globals_pred p = wgd->p; | |
1860 | walk_globals_fn f = wgd->f; | |
1861 | void *d = wgd->data; | |
1862 | tree *t; | |
1863 | int result = 0; | |
1864 | ||
1865 | t = &NAMESPACE_LEVEL (namespace)->names; | |
1866 | ||
1867 | while (*t) | |
1868 | { | |
1869 | tree glbl = *t; | |
1870 | ||
1871 | if ((*p) (glbl, d)) | |
1872 | result |= (*f) (t, d); | |
1873 | ||
1874 | /* If F changed *T, then *T still points at the next item to | |
1875 | examine. */ | |
1876 | if (*t == glbl) | |
1877 | t = &TREE_CHAIN (*t); | |
1878 | } | |
1879 | ||
1880 | return result; | |
1881 | } | |
1882 | ||
1883 | /* Walk the global declarations. Whenever one is found for which P | |
838dfd8a KH |
1884 | returns nonzero, call F with its address. If any call to F |
1885 | returns a nonzero value, return a nonzero value. */ | |
0aafb128 MM |
1886 | |
1887 | int | |
1888 | walk_globals (p, f, data) | |
1889 | walk_globals_pred p; | |
1890 | walk_globals_fn f; | |
1891 | void *data; | |
1892 | { | |
1893 | struct walk_globals_data wgd; | |
1894 | wgd.p = p; | |
1895 | wgd.f = f; | |
1896 | wgd.data = data; | |
1897 | ||
1898 | return walk_namespaces (walk_globals_r, &wgd); | |
1899 | } | |
1900 | ||
1901 | /* Call wrapup_globals_declarations for the globals in NAMESPACE. If | |
1902 | DATA is non-NULL, this is the last time we will call | |
1903 | wrapup_global_declarations for this NAMESPACE. */ | |
1904 | ||
1905 | int | |
1906 | wrapup_globals_for_namespace (namespace, data) | |
1907 | tree namespace; | |
1908 | void *data; | |
1909 | { | |
f71f87f9 | 1910 | tree globals = cp_namespace_decls (namespace); |
70b76b34 | 1911 | int len = NAMESPACE_LEVEL (namespace)->names_size; |
0aafb128 MM |
1912 | tree *vec = (tree *) alloca (sizeof (tree) * len); |
1913 | int i; | |
0352cfc8 | 1914 | int result; |
0aafb128 MM |
1915 | tree decl; |
1916 | int last_time = (data != 0); | |
1917 | ||
1918 | if (last_time && namespace == global_namespace) | |
1919 | /* Let compile_file handle the global namespace. */ | |
1920 | return 0; | |
1921 | ||
1922 | /* Process the decls in reverse order--earliest first. | |
70b76b34 | 1923 | Put them into VEC from back to front, then take out from front. */ |
0aafb128 | 1924 | for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl)) |
62136084 | 1925 | vec[len - i - 1] = decl; |
68642fb6 | 1926 | |
0352cfc8 MM |
1927 | if (last_time) |
1928 | { | |
1929 | check_global_declarations (vec, len); | |
1930 | return 0; | |
1931 | } | |
1932 | ||
0352cfc8 MM |
1933 | /* Write out any globals that need to be output. */ |
1934 | result = wrapup_global_declarations (vec, len); | |
1935 | ||
0352cfc8 | 1936 | return result; |
0aafb128 MM |
1937 | } |
1938 | ||
8d08fdba MS |
1939 | \f |
1940 | /* For debugging. */ | |
5566b478 MS |
1941 | static int no_print_functions = 0; |
1942 | static int no_print_builtins = 0; | |
8d08fdba MS |
1943 | |
1944 | void | |
1945 | print_binding_level (lvl) | |
e2500fed | 1946 | struct cp_binding_level *lvl; |
8d08fdba MS |
1947 | { |
1948 | tree t; | |
1949 | int i = 0, len; | |
1950 | fprintf (stderr, " blocks="); | |
1951 | fprintf (stderr, HOST_PTR_PRINTF, lvl->blocks); | |
8d08fdba MS |
1952 | if (lvl->tag_transparent) |
1953 | fprintf (stderr, " tag-transparent"); | |
1954 | if (lvl->more_cleanups_ok) | |
1955 | fprintf (stderr, " more-cleanups-ok"); | |
1956 | if (lvl->have_cleanups) | |
1957 | fprintf (stderr, " have-cleanups"); | |
8d08fdba MS |
1958 | fprintf (stderr, "\n"); |
1959 | if (lvl->names) | |
1960 | { | |
1961 | fprintf (stderr, " names:\t"); | |
1962 | /* We can probably fit 3 names to a line? */ | |
1963 | for (t = lvl->names; t; t = TREE_CHAIN (t)) | |
1964 | { | |
68642fb6 | 1965 | if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL)) |
8d08fdba MS |
1966 | continue; |
1967 | if (no_print_builtins | |
fc378698 MS |
1968 | && (TREE_CODE (t) == TYPE_DECL) |
1969 | && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>"))) | |
8d08fdba MS |
1970 | continue; |
1971 | ||
1972 | /* Function decls tend to have longer names. */ | |
1973 | if (TREE_CODE (t) == FUNCTION_DECL) | |
1974 | len = 3; | |
1975 | else | |
1976 | len = 2; | |
1977 | i += len; | |
1978 | if (i > 6) | |
1979 | { | |
1980 | fprintf (stderr, "\n\t"); | |
1981 | i = len; | |
1982 | } | |
1983 | print_node_brief (stderr, "", t, 0); | |
bd6dd845 | 1984 | if (t == error_mark_node) |
8d08fdba MS |
1985 | break; |
1986 | } | |
1987 | if (i) | |
1988 | fprintf (stderr, "\n"); | |
1989 | } | |
1990 | if (lvl->tags) | |
1991 | { | |
1992 | fprintf (stderr, " tags:\t"); | |
1993 | i = 0; | |
1994 | for (t = lvl->tags; t; t = TREE_CHAIN (t)) | |
1995 | { | |
1996 | if (TREE_PURPOSE (t) == NULL_TREE) | |
1997 | len = 3; | |
1998 | else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t))) | |
1999 | len = 2; | |
2000 | else | |
2001 | len = 4; | |
2002 | i += len; | |
2003 | if (i > 5) | |
2004 | { | |
2005 | fprintf (stderr, "\n\t"); | |
2006 | i = len; | |
2007 | } | |
2008 | if (TREE_PURPOSE (t) == NULL_TREE) | |
2009 | { | |
2010 | print_node_brief (stderr, "<unnamed-typedef", TREE_VALUE (t), 0); | |
2011 | fprintf (stderr, ">"); | |
2012 | } | |
2013 | else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t))) | |
2014 | print_node_brief (stderr, "", TREE_VALUE (t), 0); | |
2015 | else | |
2016 | { | |
2017 | print_node_brief (stderr, "<typedef", TREE_PURPOSE (t), 0); | |
2018 | print_node_brief (stderr, "", TREE_VALUE (t), 0); | |
2019 | fprintf (stderr, ">"); | |
2020 | } | |
2021 | } | |
2022 | if (i) | |
2023 | fprintf (stderr, "\n"); | |
2024 | } | |
8d08fdba MS |
2025 | if (lvl->class_shadowed) |
2026 | { | |
2027 | fprintf (stderr, " class-shadowed:"); | |
2028 | for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t)) | |
2029 | { | |
2030 | fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t))); | |
2031 | } | |
2032 | fprintf (stderr, "\n"); | |
2033 | } | |
2034 | if (lvl->type_shadowed) | |
2035 | { | |
2036 | fprintf (stderr, " type-shadowed:"); | |
2037 | for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t)) | |
2038 | { | |
8d08fdba | 2039 | fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t))); |
8d08fdba MS |
2040 | } |
2041 | fprintf (stderr, "\n"); | |
2042 | } | |
2043 | } | |
2044 | ||
2045 | void | |
2046 | print_other_binding_stack (stack) | |
e2500fed | 2047 | struct cp_binding_level *stack; |
8d08fdba | 2048 | { |
e2500fed | 2049 | struct cp_binding_level *level; |
8d08fdba MS |
2050 | for (level = stack; level != global_binding_level; level = level->level_chain) |
2051 | { | |
2052 | fprintf (stderr, "binding level "); | |
2053 | fprintf (stderr, HOST_PTR_PRINTF, level); | |
2054 | fprintf (stderr, "\n"); | |
2055 | print_binding_level (level); | |
2056 | } | |
2057 | } | |
2058 | ||
2059 | void | |
2060 | print_binding_stack () | |
2061 | { | |
e2500fed | 2062 | struct cp_binding_level *b; |
8d08fdba MS |
2063 | fprintf (stderr, "current_binding_level="); |
2064 | fprintf (stderr, HOST_PTR_PRINTF, current_binding_level); | |
2065 | fprintf (stderr, "\nclass_binding_level="); | |
2066 | fprintf (stderr, HOST_PTR_PRINTF, class_binding_level); | |
2067 | fprintf (stderr, "\nglobal_binding_level="); | |
2068 | fprintf (stderr, HOST_PTR_PRINTF, global_binding_level); | |
2069 | fprintf (stderr, "\n"); | |
2070 | if (class_binding_level) | |
2071 | { | |
2072 | for (b = class_binding_level; b; b = b->level_chain) | |
2073 | if (b == current_binding_level) | |
2074 | break; | |
2075 | if (b) | |
2076 | b = class_binding_level; | |
2077 | else | |
2078 | b = current_binding_level; | |
2079 | } | |
2080 | else | |
2081 | b = current_binding_level; | |
2082 | print_other_binding_stack (b); | |
2083 | fprintf (stderr, "global:\n"); | |
2084 | print_binding_level (global_binding_level); | |
2085 | } | |
a9aedbc2 | 2086 | |
2c73f9f5 ML |
2087 | /* Namespace binding access routines: The namespace_bindings field of |
2088 | the identifier is polymorphic, with three possible values: | |
2089 | NULL_TREE, a list of CPLUS_BINDINGS, or any other tree_node | |
a1c65f9f | 2090 | indicating the BINDING_VALUE of global_namespace. */ |
30394414 | 2091 | |
2c73f9f5 ML |
2092 | /* Check whether the a binding for the name to scope is known. |
2093 | Assumes that the bindings of the name are already a list | |
a1c65f9f | 2094 | of bindings. Returns the binding found, or NULL_TREE. */ |
2c73f9f5 ML |
2095 | |
2096 | static tree | |
2097 | find_binding (name, scope) | |
30394414 JM |
2098 | tree name; |
2099 | tree scope; | |
2100 | { | |
2c73f9f5 | 2101 | tree iter, prev = NULL_TREE; |
3e3f722c ML |
2102 | |
2103 | scope = ORIGINAL_NAMESPACE (scope); | |
68642fb6 | 2104 | |
30394414 JM |
2105 | for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name); iter; |
2106 | iter = TREE_CHAIN (iter)) | |
2107 | { | |
2108 | my_friendly_assert (TREE_CODE (iter) == CPLUS_BINDING, 374); | |
2109 | if (BINDING_SCOPE (iter) == scope) | |
2c73f9f5 | 2110 | { |
cfe507be | 2111 | /* Move binding found to the front of the list, so |
a1c65f9f | 2112 | subsequent lookups will find it faster. */ |
2c73f9f5 ML |
2113 | if (prev) |
2114 | { | |
2115 | TREE_CHAIN (prev) = TREE_CHAIN (iter); | |
2116 | TREE_CHAIN (iter) = IDENTIFIER_NAMESPACE_BINDINGS (name); | |
2117 | IDENTIFIER_NAMESPACE_BINDINGS (name) = iter; | |
2118 | } | |
2119 | return iter; | |
2120 | } | |
2121 | prev = iter; | |
30394414 | 2122 | } |
2c73f9f5 ML |
2123 | return NULL_TREE; |
2124 | } | |
2125 | ||
2126 | /* Always returns a binding for name in scope. If the | |
2127 | namespace_bindings is not a list, convert it to one first. | |
a1c65f9f | 2128 | If no binding is found, make a new one. */ |
2c73f9f5 ML |
2129 | |
2130 | tree | |
2131 | binding_for_name (name, scope) | |
2132 | tree name; | |
2133 | tree scope; | |
2134 | { | |
2135 | tree b = IDENTIFIER_NAMESPACE_BINDINGS (name); | |
2136 | tree result; | |
3e3f722c ML |
2137 | |
2138 | scope = ORIGINAL_NAMESPACE (scope); | |
68642fb6 | 2139 | |
2c73f9f5 ML |
2140 | if (b && TREE_CODE (b) != CPLUS_BINDING) |
2141 | { | |
a1c65f9f | 2142 | /* Get rid of optimization for global scope. */ |
2c73f9f5 ML |
2143 | IDENTIFIER_NAMESPACE_BINDINGS (name) = NULL_TREE; |
2144 | BINDING_VALUE (binding_for_name (name, global_namespace)) = b; | |
2145 | b = IDENTIFIER_NAMESPACE_BINDINGS (name); | |
2146 | } | |
2147 | if (b && (result = find_binding (name, scope))) | |
2148 | return result; | |
a1c65f9f | 2149 | /* Not found, make a new one. */ |
2c73f9f5 ML |
2150 | result = make_node (CPLUS_BINDING); |
2151 | TREE_CHAIN (result) = b; | |
2152 | IDENTIFIER_NAMESPACE_BINDINGS (name) = result; | |
2153 | BINDING_SCOPE (result) = scope; | |
2154 | BINDING_TYPE (result) = NULL_TREE; | |
2155 | BINDING_VALUE (result) = NULL_TREE; | |
2c73f9f5 ML |
2156 | return result; |
2157 | } | |
2158 | ||
2159 | /* Return the binding value for name in scope, considering that | |
a1c65f9f | 2160 | namespace_binding may or may not be a list of CPLUS_BINDINGS. */ |
2c73f9f5 ML |
2161 | |
2162 | tree | |
2163 | namespace_binding (name, scope) | |
2164 | tree name; | |
2165 | tree scope; | |
2166 | { | |
2167 | tree b = IDENTIFIER_NAMESPACE_BINDINGS (name); | |
2168 | if (b == NULL_TREE) | |
2169 | return NULL_TREE; | |
cb0dbb9a JM |
2170 | if (scope == NULL_TREE) |
2171 | scope = global_namespace; | |
2c73f9f5 ML |
2172 | if (TREE_CODE (b) != CPLUS_BINDING) |
2173 | return (scope == global_namespace) ? b : NULL_TREE; | |
2174 | name = find_binding (name,scope); | |
2175 | if (name == NULL_TREE) | |
2176 | return name; | |
2177 | return BINDING_VALUE (name); | |
2178 | } | |
2179 | ||
2180 | /* Set the binding value for name in scope. If modifying the binding | |
a1c65f9f | 2181 | of global_namespace is attempted, try to optimize it. */ |
2c73f9f5 ML |
2182 | |
2183 | void | |
2184 | set_namespace_binding (name, scope, val) | |
2185 | tree name; | |
2186 | tree scope; | |
2187 | tree val; | |
2188 | { | |
2189 | tree b; | |
cb0dbb9a JM |
2190 | |
2191 | if (scope == NULL_TREE) | |
2192 | scope = global_namespace; | |
68642fb6 | 2193 | |
2c73f9f5 ML |
2194 | if (scope == global_namespace) |
2195 | { | |
2196 | b = IDENTIFIER_NAMESPACE_BINDINGS (name); | |
2197 | if (b == NULL_TREE || TREE_CODE (b) != CPLUS_BINDING) | |
2198 | { | |
2199 | IDENTIFIER_NAMESPACE_BINDINGS (name) = val; | |
2200 | return; | |
2201 | } | |
2202 | } | |
2203 | b = binding_for_name (name, scope); | |
2204 | BINDING_VALUE (b) = val; | |
30394414 JM |
2205 | } |
2206 | ||
dff6b454 RK |
2207 | /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we |
2208 | select a name that is unique to this compilation unit. */ | |
e92cc029 | 2209 | |
a9aedbc2 MS |
2210 | void |
2211 | push_namespace (name) | |
2212 | tree name; | |
2213 | { | |
b370501f | 2214 | tree d = NULL_TREE; |
30394414 JM |
2215 | int need_new = 1; |
2216 | int implicit_use = 0; | |
2c73f9f5 | 2217 | int global = 0; |
30394414 JM |
2218 | if (!global_namespace) |
2219 | { | |
a1c65f9f | 2220 | /* This must be ::. */ |
30394414 | 2221 | my_friendly_assert (name == get_identifier ("::"), 377); |
2c73f9f5 | 2222 | global = 1; |
30394414 JM |
2223 | } |
2224 | else if (!name) | |
2225 | { | |
3ab52652 ML |
2226 | /* The name of anonymous namespace is unique for the translation |
2227 | unit. */ | |
0c8feefe MM |
2228 | if (!anonymous_namespace_name) |
2229 | anonymous_namespace_name = get_file_function_name ('N'); | |
2230 | name = anonymous_namespace_name; | |
3ab52652 ML |
2231 | d = IDENTIFIER_NAMESPACE_VALUE (name); |
2232 | if (d) | |
2233 | /* Reopening anonymous namespace. */ | |
2234 | need_new = 0; | |
30394414 | 2235 | implicit_use = 1; |
2c73f9f5 | 2236 | } |
30394414 JM |
2237 | else |
2238 | { | |
a1c65f9f | 2239 | /* Check whether this is an extended namespace definition. */ |
30394414 JM |
2240 | d = IDENTIFIER_NAMESPACE_VALUE (name); |
2241 | if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL) | |
3e3f722c ML |
2242 | { |
2243 | need_new = 0; | |
2244 | if (DECL_NAMESPACE_ALIAS (d)) | |
2245 | { | |
33bd39a2 | 2246 | error ("namespace alias `%D' not allowed here, assuming `%D'", |
3e3f722c ML |
2247 | d, DECL_NAMESPACE_ALIAS (d)); |
2248 | d = DECL_NAMESPACE_ALIAS (d); | |
2249 | } | |
2250 | } | |
30394414 | 2251 | } |
68642fb6 | 2252 | |
30394414 JM |
2253 | if (need_new) |
2254 | { | |
a1c65f9f | 2255 | /* Make a new namespace, binding the name to it. */ |
6b57ac29 | 2256 | d = build_lang_decl (NAMESPACE_DECL, name, void_type_node); |
2c73f9f5 ML |
2257 | /* The global namespace is not pushed, and the global binding |
2258 | level is set elsewhere. */ | |
2259 | if (!global) | |
2260 | { | |
40f1e342 | 2261 | DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace); |
2c73f9f5 ML |
2262 | d = pushdecl (d); |
2263 | pushlevel (0); | |
2264 | declare_namespace_level (); | |
2265 | NAMESPACE_LEVEL (d) = current_binding_level; | |
2266 | } | |
30394414 | 2267 | } |
2c73f9f5 ML |
2268 | else |
2269 | resume_binding_level (NAMESPACE_LEVEL (d)); | |
2270 | ||
30394414 JM |
2271 | if (implicit_use) |
2272 | do_using_directive (d); | |
a1c65f9f | 2273 | /* Enter the name space. */ |
30394414 | 2274 | current_namespace = d; |
a9aedbc2 MS |
2275 | } |
2276 | ||
2277 | /* Pop from the scope of the current namespace. */ | |
e92cc029 | 2278 | |
a9aedbc2 MS |
2279 | void |
2280 | pop_namespace () | |
2281 | { | |
1dbb6023 | 2282 | my_friendly_assert (current_namespace != global_namespace, 20010801); |
cb0dbb9a | 2283 | current_namespace = CP_DECL_CONTEXT (current_namespace); |
2c73f9f5 ML |
2284 | /* The binding level is not popped, as it might be re-opened later. */ |
2285 | suspend_binding_level (); | |
2286 | } | |
a9aedbc2 | 2287 | |
61fc8c9e MM |
2288 | /* Push into the scope of the namespace NS, even if it is deeply |
2289 | nested within another namespace. */ | |
2290 | ||
2291 | void | |
2292 | push_nested_namespace (ns) | |
2293 | tree ns; | |
2294 | { | |
2295 | if (ns == global_namespace) | |
2296 | push_to_top_level (); | |
2297 | else | |
2298 | { | |
2299 | push_nested_namespace (CP_DECL_CONTEXT (ns)); | |
2300 | push_namespace (DECL_NAME (ns)); | |
2301 | } | |
2302 | } | |
2303 | ||
2304 | /* Pop back from the scope of the namespace NS, which was previously | |
2305 | entered with push_nested_namespace. */ | |
68642fb6 | 2306 | |
61fc8c9e MM |
2307 | void |
2308 | pop_nested_namespace (ns) | |
2309 | tree ns; | |
2310 | { | |
2311 | while (ns != global_namespace) | |
2312 | { | |
2313 | pop_namespace (); | |
2314 | ns = CP_DECL_CONTEXT (ns); | |
2315 | } | |
2316 | ||
2317 | pop_from_top_level (); | |
2318 | } | |
2319 | ||
8d08fdba MS |
2320 | \f |
2321 | /* Subroutines for reverting temporarily to top-level for instantiation | |
2322 | of templates and such. We actually need to clear out the class- and | |
2323 | local-value slots of all identifiers, so that only the global values | |
2324 | are at all visible. Simply setting current_binding_level to the global | |
2325 | scope isn't enough, because more binding levels may be pushed. */ | |
9cd64686 | 2326 | struct saved_scope *scope_chain; |
8d08fdba | 2327 | |
e92cc029 | 2328 | static tree |
45537677 MS |
2329 | store_bindings (names, old_bindings) |
2330 | tree names, old_bindings; | |
2331 | { | |
2332 | tree t; | |
c00996a3 JM |
2333 | tree search_bindings = old_bindings; |
2334 | ||
45537677 MS |
2335 | for (t = names; t; t = TREE_CHAIN (t)) |
2336 | { | |
2337 | tree binding, t1, id; | |
2338 | ||
2339 | if (TREE_CODE (t) == TREE_LIST) | |
2340 | id = TREE_PURPOSE (t); | |
2341 | else | |
2342 | id = DECL_NAME (t); | |
2343 | ||
68642fb6 | 2344 | if (!id |
f181d4ae MM |
2345 | /* Note that we may have an IDENTIFIER_CLASS_VALUE even when |
2346 | we have no IDENTIFIER_BINDING if we have left the class | |
2347 | scope, but cached the class-level declarations. */ | |
2348 | || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id))) | |
45537677 MS |
2349 | continue; |
2350 | ||
c00996a3 | 2351 | for (t1 = search_bindings; t1; t1 = TREE_CHAIN (t1)) |
45537677 MS |
2352 | if (TREE_VEC_ELT (t1, 0) == id) |
2353 | goto skip_it; | |
78957a2a | 2354 | |
31a714f6 | 2355 | my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135); |
87e3dbc9 | 2356 | binding = make_tree_vec (4); |
31a714f6 MM |
2357 | TREE_VEC_ELT (binding, 0) = id; |
2358 | TREE_VEC_ELT (binding, 1) = REAL_IDENTIFIER_TYPE_VALUE (id); | |
2359 | TREE_VEC_ELT (binding, 2) = IDENTIFIER_BINDING (id); | |
2360 | TREE_VEC_ELT (binding, 3) = IDENTIFIER_CLASS_VALUE (id); | |
2361 | IDENTIFIER_BINDING (id) = NULL_TREE; | |
2362 | IDENTIFIER_CLASS_VALUE (id) = NULL_TREE; | |
45537677 MS |
2363 | TREE_CHAIN (binding) = old_bindings; |
2364 | old_bindings = binding; | |
2365 | skip_it: | |
2366 | ; | |
2367 | } | |
2368 | return old_bindings; | |
2369 | } | |
2370 | ||
8d08fdba | 2371 | void |
5566b478 MS |
2372 | maybe_push_to_top_level (pseudo) |
2373 | int pseudo; | |
8d08fdba | 2374 | { |
a8f73d4b | 2375 | struct saved_scope *s; |
e2500fed | 2376 | struct cp_binding_level *b; |
a8f73d4b MM |
2377 | tree old_bindings; |
2378 | int need_pop; | |
2379 | ||
e2500fed | 2380 | s = (struct saved_scope *) ggc_alloc_cleared (sizeof (struct saved_scope)); |
8d08fdba | 2381 | |
9cd64686 MM |
2382 | b = scope_chain ? current_binding_level : 0; |
2383 | ||
a8f73d4b | 2384 | /* If we're in the middle of some function, save our state. */ |
01d939e8 | 2385 | if (cfun) |
a8f73d4b MM |
2386 | { |
2387 | need_pop = 1; | |
2388 | push_function_context_to (NULL_TREE); | |
2389 | } | |
2390 | else | |
2391 | need_pop = 0; | |
e349ee73 | 2392 | |
a8f73d4b | 2393 | old_bindings = NULL_TREE; |
9cd64686 | 2394 | if (scope_chain && previous_class_type) |
a50f0918 MS |
2395 | old_bindings = store_bindings (previous_class_values, old_bindings); |
2396 | ||
8d08fdba MS |
2397 | /* Have to include global_binding_level, because class-level decls |
2398 | aren't listed anywhere useful. */ | |
2399 | for (; b; b = b->level_chain) | |
2400 | { | |
2401 | tree t; | |
2402 | ||
2c73f9f5 ML |
2403 | /* Template IDs are inserted into the global level. If they were |
2404 | inserted into namespace level, finish_file wouldn't find them | |
2405 | when doing pending instantiations. Therefore, don't stop at | |
2406 | namespace level, but continue until :: . */ | |
74b846e0 | 2407 | if (b == global_binding_level || (pseudo && b->template_parms_p)) |
5566b478 | 2408 | break; |
8d08fdba | 2409 | |
45537677 | 2410 | old_bindings = store_bindings (b->names, old_bindings); |
cffa8729 | 2411 | /* We also need to check class_shadowed to save class-level type |
45537677 MS |
2412 | bindings, since pushclass doesn't fill in b->names. */ |
2413 | if (b->parm_flag == 2) | |
cffa8729 | 2414 | old_bindings = store_bindings (b->class_shadowed, old_bindings); |
8d08fdba | 2415 | |
8d08fdba MS |
2416 | /* Unwind type-value slots back to top level. */ |
2417 | for (t = b->type_shadowed; t; t = TREE_CHAIN (t)) | |
2418 | SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t)); | |
2419 | } | |
fc0e7bf5 MM |
2420 | s->prev = scope_chain; |
2421 | s->old_bindings = old_bindings; | |
a8f73d4b MM |
2422 | s->bindings = b; |
2423 | s->need_pop_function_context = need_pop; | |
2424 | s->function_decl = current_function_decl; | |
0f4237c2 | 2425 | s->last_parms = last_function_parms; |
fc0e7bf5 MM |
2426 | |
2427 | scope_chain = s; | |
8d08fdba | 2428 | current_function_decl = NULL_TREE; |
9cd64686 | 2429 | VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base"); |
51c184be | 2430 | current_lang_name = lang_name_cplusplus; |
2c73f9f5 | 2431 | current_namespace = global_namespace; |
5566b478 MS |
2432 | } |
2433 | ||
2434 | void | |
2435 | push_to_top_level () | |
2436 | { | |
2437 | maybe_push_to_top_level (0); | |
8d08fdba MS |
2438 | } |
2439 | ||
2440 | void | |
2441 | pop_from_top_level () | |
2442 | { | |
9cd64686 | 2443 | struct saved_scope *s = scope_chain; |
8d08fdba MS |
2444 | tree t; |
2445 | ||
e76a2646 | 2446 | /* Clear out class-level bindings cache. */ |
8d08fdba | 2447 | if (previous_class_type) |
8f032717 | 2448 | invalidate_class_lookup_cache (); |
8d08fdba | 2449 | |
e2500fed | 2450 | current_lang_base = 0; |
9cd64686 MM |
2451 | |
2452 | scope_chain = s->prev; | |
87e3dbc9 | 2453 | for (t = s->old_bindings; t; t = TREE_CHAIN (t)) |
8d08fdba MS |
2454 | { |
2455 | tree id = TREE_VEC_ELT (t, 0); | |
31a714f6 MM |
2456 | |
2457 | SET_IDENTIFIER_TYPE_VALUE (id, TREE_VEC_ELT (t, 1)); | |
2458 | IDENTIFIER_BINDING (id) = TREE_VEC_ELT (t, 2); | |
2459 | IDENTIFIER_CLASS_VALUE (id) = TREE_VEC_ELT (t, 3); | |
8d08fdba | 2460 | } |
9cd64686 | 2461 | |
a8f73d4b MM |
2462 | /* If we were in the middle of compiling a function, restore our |
2463 | state. */ | |
2464 | if (s->need_pop_function_context) | |
2465 | pop_function_context_from (NULL_TREE); | |
2466 | current_function_decl = s->function_decl; | |
0f4237c2 | 2467 | last_function_parms = s->last_parms; |
8d08fdba MS |
2468 | } |
2469 | \f | |
2470 | /* Push a definition of struct, union or enum tag "name". | |
68642fb6 | 2471 | into binding_level "b". "type" should be the type node, |
8d08fdba MS |
2472 | We assume that the tag "name" is not already defined. |
2473 | ||
2474 | Note that the definition may really be just a forward reference. | |
2475 | In that case, the TYPE_SIZE will be a NULL_TREE. | |
2476 | ||
e92cc029 | 2477 | C++ gratuitously puts all these tags in the name space. */ |
8d08fdba MS |
2478 | |
2479 | /* When setting the IDENTIFIER_TYPE_VALUE field of an identifier ID, | |
2480 | record the shadowed value for this binding contour. TYPE is | |
2481 | the type that ID maps to. */ | |
2482 | ||
2483 | static void | |
2484 | set_identifier_type_value_with_scope (id, type, b) | |
2485 | tree id; | |
2486 | tree type; | |
e2500fed | 2487 | struct cp_binding_level *b; |
8d08fdba | 2488 | { |
2c73f9f5 | 2489 | if (!b->namespace_p) |
8d08fdba | 2490 | { |
2c73f9f5 | 2491 | /* Shadow the marker, not the real thing, so that the marker |
a1c65f9f | 2492 | gets restored later. */ |
2c73f9f5 | 2493 | tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id); |
8d08fdba MS |
2494 | b->type_shadowed |
2495 | = tree_cons (id, old_type_value, b->type_shadowed); | |
2496 | } | |
2c73f9f5 ML |
2497 | else |
2498 | { | |
2499 | tree binding = binding_for_name (id, current_namespace); | |
2500 | BINDING_TYPE (binding) = type; | |
a1c65f9f | 2501 | /* Store marker instead of real type. */ |
2c73f9f5 ML |
2502 | type = global_type_node; |
2503 | } | |
8d08fdba MS |
2504 | SET_IDENTIFIER_TYPE_VALUE (id, type); |
2505 | } | |
2506 | ||
8f032717 | 2507 | /* As set_identifier_type_value_with_scope, but using current_binding_level. */ |
8d08fdba MS |
2508 | |
2509 | void | |
2510 | set_identifier_type_value (id, type) | |
2511 | tree id; | |
2512 | tree type; | |
2513 | { | |
8f032717 | 2514 | set_identifier_type_value_with_scope (id, type, current_binding_level); |
8d08fdba MS |
2515 | } |
2516 | ||
a1c65f9f | 2517 | /* Return the type associated with id. */ |
2c73f9f5 ML |
2518 | |
2519 | tree | |
2520 | identifier_type_value (id) | |
2521 | tree id; | |
2522 | { | |
a1c65f9f | 2523 | /* There is no type with that name, anywhere. */ |
2c73f9f5 ML |
2524 | if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE) |
2525 | return NULL_TREE; | |
a1c65f9f | 2526 | /* This is not the type marker, but the real thing. */ |
2c73f9f5 ML |
2527 | if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node) |
2528 | return REAL_IDENTIFIER_TYPE_VALUE (id); | |
2529 | /* Have to search for it. It must be on the global level, now. | |
a1c65f9f | 2530 | Ask lookup_name not to return non-types. */ |
3e3f722c | 2531 | id = lookup_name_real (id, 2, 1, 0); |
2c73f9f5 ML |
2532 | if (id) |
2533 | return TREE_TYPE (id); | |
2534 | return NULL_TREE; | |
2535 | } | |
2536 | ||
a9aedbc2 MS |
2537 | /* Pop off extraneous binding levels left over due to syntax errors. |
2538 | ||
2539 | We don't pop past namespaces, as they might be valid. */ | |
e92cc029 | 2540 | |
8926095f MS |
2541 | void |
2542 | pop_everything () | |
2543 | { | |
e2500fed | 2544 | #ifdef DEBUG_BINDING_LEVELS |
8926095f MS |
2545 | fprintf (stderr, "XXX entering pop_everything ()\n"); |
2546 | #endif | |
8f032717 | 2547 | while (!toplevel_bindings_p ()) |
8926095f | 2548 | { |
8f032717 | 2549 | if (current_binding_level->parm_flag == 2) |
b74a0560 | 2550 | pop_nested_class (); |
8926095f MS |
2551 | else |
2552 | poplevel (0, 0, 0); | |
2553 | } | |
e2500fed | 2554 | #ifdef DEBUG_BINDING_LEVELS |
8926095f MS |
2555 | fprintf (stderr, "XXX leaving pop_everything ()\n"); |
2556 | #endif | |
2557 | } | |
2558 | ||
39c01e4c MM |
2559 | /* The type TYPE is being declared. If it is a class template, or a |
2560 | specialization of a class template, do any processing required and | |
838dfd8a | 2561 | perform error-checking. If IS_FRIEND is nonzero, this TYPE is |
39c01e4c MM |
2562 | being declared a friend. B is the binding level at which this TYPE |
2563 | should be bound. | |
2564 | ||
2565 | Returns the TYPE_DECL for TYPE, which may have been altered by this | |
2566 | processing. */ | |
2567 | ||
68642fb6 | 2568 | static tree |
39c01e4c MM |
2569 | maybe_process_template_type_declaration (type, globalize, b) |
2570 | tree type; | |
2571 | int globalize; | |
e2500fed | 2572 | struct cp_binding_level* b; |
39c01e4c MM |
2573 | { |
2574 | tree decl = TYPE_NAME (type); | |
68642fb6 | 2575 | |
39c01e4c MM |
2576 | if (processing_template_parmlist) |
2577 | /* You can't declare a new template type in a template parameter | |
2578 | list. But, you can declare a non-template type: | |
68642fb6 | 2579 | |
39c01e4c | 2580 | template <class A*> struct S; |
68642fb6 | 2581 | |
39c01e4c MM |
2582 | is a forward-declaration of `A'. */ |
2583 | ; | |
68642fb6 | 2584 | else |
39c01e4c MM |
2585 | { |
2586 | maybe_check_template_type (type); | |
2587 | ||
68642fb6 | 2588 | my_friendly_assert (IS_AGGR_TYPE (type) |
ed44da02 | 2589 | || TREE_CODE (type) == ENUMERAL_TYPE, 0); |
68642fb6 UD |
2590 | |
2591 | ||
3ebc5c52 | 2592 | if (processing_template_decl) |
39c01e4c MM |
2593 | { |
2594 | /* This may change after the call to | |
2595 | push_template_decl_real, but we want the original value. */ | |
2596 | tree name = DECL_NAME (decl); | |
2597 | ||
2598 | decl = push_template_decl_real (decl, globalize); | |
2599 | /* If the current binding level is the binding level for the | |
2600 | template parameters (see the comment in | |
2601 | begin_template_parm_list) and the enclosing level is a class | |
2602 | scope, and we're not looking at a friend, push the | |
2603 | declaration of the member class into the class scope. In the | |
2604 | friend case, push_template_decl will already have put the | |
2605 | friend into global scope, if appropriate. */ | |
ed44da02 | 2606 | if (TREE_CODE (type) != ENUMERAL_TYPE |
74b846e0 | 2607 | && !globalize && b->template_parms_p |
39c01e4c MM |
2608 | && b->level_chain->parm_flag == 2) |
2609 | { | |
61a127b3 | 2610 | finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type)); |
39c01e4c MM |
2611 | /* Put this tag on the list of tags for the class, since |
2612 | that won't happen below because B is not the class | |
2613 | binding level, but is instead the pseudo-global level. */ | |
68642fb6 | 2614 | b->level_chain->tags = |
e1b3e07d | 2615 | tree_cons (name, type, b->level_chain->tags); |
d0f062fb | 2616 | if (!COMPLETE_TYPE_P (current_class_type)) |
39c01e4c MM |
2617 | CLASSTYPE_TAGS (current_class_type) = b->level_chain->tags; |
2618 | } | |
2619 | } | |
2620 | } | |
2621 | ||
2622 | return decl; | |
2623 | } | |
2624 | ||
9188c363 MM |
2625 | /* In C++, you don't have to write `struct S' to refer to `S'; you |
2626 | can just use `S'. We accomplish this by creating a TYPE_DECL as | |
2627 | if the user had written `typedef struct S S'. Create and return | |
2628 | the TYPE_DECL for TYPE. */ | |
2629 | ||
2630 | tree | |
2631 | create_implicit_typedef (name, type) | |
2632 | tree name; | |
2633 | tree type; | |
2634 | { | |
2635 | tree decl; | |
2636 | ||
2637 | decl = build_decl (TYPE_DECL, name, type); | |
c727aa5e | 2638 | DECL_ARTIFICIAL (decl) = 1; |
9188c363 MM |
2639 | /* There are other implicit type declarations, like the one *within* |
2640 | a class that allows you to write `S::S'. We must distinguish | |
2641 | amongst these. */ | |
2642 | SET_DECL_IMPLICIT_TYPEDEF_P (decl); | |
2643 | TYPE_NAME (type) = decl; | |
2644 | ||
2645 | return decl; | |
2646 | } | |
2647 | ||
8e4ce833 JJ |
2648 | /* Remember a local name for name-mangling purposes. */ |
2649 | ||
2650 | static void | |
2651 | push_local_name (decl) | |
2652 | tree decl; | |
2653 | { | |
2654 | size_t i, nelts; | |
2655 | tree t, name; | |
2656 | ||
2657 | if (!local_names) | |
2658 | VARRAY_TREE_INIT (local_names, 8, "local_names"); | |
2659 | ||
2660 | name = DECL_NAME (decl); | |
2661 | ||
2662 | nelts = VARRAY_ACTIVE_SIZE (local_names); | |
2663 | for (i = 0; i < nelts; i++) | |
2664 | { | |
2665 | t = VARRAY_TREE (local_names, i); | |
2666 | if (DECL_NAME (t) == name) | |
2667 | { | |
2668 | if (!DECL_LANG_SPECIFIC (decl)) | |
2669 | retrofit_lang_decl (decl); | |
e2500fed | 2670 | DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1; |
8e4ce833 JJ |
2671 | if (DECL_LANG_SPECIFIC (t)) |
2672 | DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1; | |
2673 | else | |
2674 | DECL_DISCRIMINATOR (decl) = 1; | |
2675 | ||
2676 | VARRAY_TREE (local_names, i) = decl; | |
2677 | return; | |
2678 | } | |
2679 | } | |
2680 | ||
2681 | VARRAY_PUSH_TREE (local_names, decl); | |
2682 | } | |
2683 | ||
8d08fdba | 2684 | /* Push a tag name NAME for struct/class/union/enum type TYPE. |
6757edfe | 2685 | Normally put it into the inner-most non-tag-transparent scope, |
8d08fdba | 2686 | but if GLOBALIZE is true, put it in the inner-most non-class scope. |
e92cc029 | 2687 | The latter is needed for implicit declarations. */ |
8d08fdba MS |
2688 | |
2689 | void | |
2690 | pushtag (name, type, globalize) | |
2691 | tree name, type; | |
2692 | int globalize; | |
2693 | { | |
e2500fed | 2694 | register struct cp_binding_level *b; |
8d08fdba | 2695 | |
8f032717 | 2696 | b = current_binding_level; |
8d08fdba | 2697 | while (b->tag_transparent |
fcebb2dd NS |
2698 | || (b->parm_flag == 2 |
2699 | && (globalize | |
2700 | /* We may be defining a new type in the initializer | |
2701 | of a static member variable. We allow this when | |
2702 | not pedantic, and it is particularly useful for | |
a1c65f9f | 2703 | type punning via an anonymous union. */ |
fcebb2dd | 2704 | || COMPLETE_TYPE_P (b->this_class)))) |
8d08fdba MS |
2705 | b = b->level_chain; |
2706 | ||
e1b3e07d | 2707 | b->tags = tree_cons (name, type, b->tags); |
8d08fdba MS |
2708 | |
2709 | if (name) | |
2710 | { | |
8d08fdba | 2711 | /* Do C++ gratuitous typedefing. */ |
db5ae43f | 2712 | if (IDENTIFIER_TYPE_VALUE (name) != type) |
8d08fdba | 2713 | { |
93cdc044 | 2714 | register tree d = NULL_TREE; |
848cf1e0 | 2715 | int in_class = 0; |
9188c363 | 2716 | tree context = TYPE_CONTEXT (type); |
280f9385 | 2717 | |
280f9385 MM |
2718 | if (! context) |
2719 | { | |
2720 | tree cs = current_scope (); | |
2721 | ||
2722 | if (! globalize) | |
2723 | context = cs; | |
2f939d94 | 2724 | else if (cs != NULL_TREE && TYPE_P (cs)) |
280f9385 MM |
2725 | /* When declaring a friend class of a local class, we want |
2726 | to inject the newly named class into the scope | |
2727 | containing the local class, not the namespace scope. */ | |
4f1c5b7d | 2728 | context = decl_function_context (get_type_decl (cs)); |
280f9385 | 2729 | } |
280f9385 MM |
2730 | if (!context) |
2731 | context = current_namespace; | |
8d08fdba | 2732 | |
74b846e0 | 2733 | if ((b->template_parms_p && b->level_chain->parm_flag == 2) |
93cdc044 JM |
2734 | || b->parm_flag == 2) |
2735 | in_class = 1; | |
93cdc044 | 2736 | |
848cf1e0 MM |
2737 | if (current_lang_name == lang_name_java) |
2738 | TYPE_FOR_JAVA (type) = 1; | |
93cdc044 | 2739 | |
9188c363 | 2740 | d = create_implicit_typedef (name, type); |
cb0dbb9a | 2741 | DECL_CONTEXT (d) = FROB_CONTEXT (context); |
9188c363 MM |
2742 | if (! in_class) |
2743 | set_identifier_type_value_with_scope (name, type, b); | |
e1cd6e56 | 2744 | |
39c01e4c MM |
2745 | d = maybe_process_template_type_declaration (type, |
2746 | globalize, b); | |
93cdc044 JM |
2747 | |
2748 | if (b->parm_flag == 2) | |
61a127b3 | 2749 | { |
848cf1e0 | 2750 | if (!PROCESSING_REAL_TEMPLATE_DECL_P ()) |
61a127b3 MM |
2751 | /* Put this TYPE_DECL on the TYPE_FIELDS list for the |
2752 | class. But if it's a member template class, we | |
2753 | want the TEMPLATE_DECL, not the TYPE_DECL, so this | |
2754 | is done later. */ | |
2755 | finish_member_declaration (d); | |
8f032717 MM |
2756 | else |
2757 | pushdecl_class_level (d); | |
61a127b3 | 2758 | } |
93cdc044 JM |
2759 | else |
2760 | d = pushdecl_with_scope (d, b); | |
2761 | ||
1951a1b6 | 2762 | /* FIXME what if it gets a name from typedef? */ |
848cf1e0 MM |
2763 | if (ANON_AGGRNAME_P (name)) |
2764 | DECL_IGNORED_P (d) = 1; | |
2765 | ||
2766 | TYPE_CONTEXT (type) = DECL_CONTEXT (d); | |
1f6e1acc AS |
2767 | |
2768 | /* If this is a local class, keep track of it. We need this | |
2769 | information for name-mangling, and so that it is possible to find | |
2770 | all function definitions in a translation unit in a convenient | |
2771 | way. (It's otherwise tricky to find a member function definition | |
2772 | it's only pointed to from within a local class.) */ | |
5362b086 | 2773 | if (TYPE_CONTEXT (type) |
089acd57 MM |
2774 | && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL |
2775 | && !processing_template_decl) | |
1f6e1acc | 2776 | VARRAY_PUSH_TREE (local_classes, type); |
8d08fdba MS |
2777 | } |
2778 | if (b->parm_flag == 2) | |
2779 | { | |
d0f062fb | 2780 | if (!COMPLETE_TYPE_P (current_class_type)) |
8d08fdba MS |
2781 | CLASSTYPE_TAGS (current_class_type) = b->tags; |
2782 | } | |
2783 | } | |
2784 | ||
2785 | if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL) | |
2786 | /* Use the canonical TYPE_DECL for this node. */ | |
2787 | TYPE_STUB_DECL (type) = TYPE_NAME (type); | |
2788 | else | |
2789 | { | |
2790 | /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE | |
2791 | will be the tagged type we just added to the current | |
2792 | binding level. This fake NULL-named TYPE_DECL node helps | |
2793 | dwarfout.c to know when it needs to output a | |
2794 | representation of a tagged type, and it also gives us a | |
2795 | convenient place to record the "scope start" address for | |
2796 | the tagged type. */ | |
2797 | ||
8d08fdba | 2798 | tree d = build_decl (TYPE_DECL, NULL_TREE, type); |
8d08fdba MS |
2799 | TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b); |
2800 | } | |
2801 | } | |
2802 | ||
2803 | /* Counter used to create anonymous type names. */ | |
e92cc029 | 2804 | |
8d08fdba MS |
2805 | static int anon_cnt = 0; |
2806 | ||
2807 | /* Return an IDENTIFIER which can be used as a name for | |
2808 | anonymous structs and unions. */ | |
e92cc029 | 2809 | |
8d08fdba MS |
2810 | tree |
2811 | make_anon_name () | |
2812 | { | |
2813 | char buf[32]; | |
2814 | ||
2815 | sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++); | |
2816 | return get_identifier (buf); | |
2817 | } | |
2818 | ||
2819 | /* Clear the TREE_PURPOSE slot of tags which have anonymous typenames. | |
2820 | This keeps dbxout from getting confused. */ | |
e92cc029 | 2821 | |
8d08fdba MS |
2822 | void |
2823 | clear_anon_tags () | |
2824 | { | |
e2500fed | 2825 | register struct cp_binding_level *b; |
8d08fdba MS |
2826 | register tree tags; |
2827 | static int last_cnt = 0; | |
2828 | ||
2829 | /* Fast out if no new anon names were declared. */ | |
2830 | if (last_cnt == anon_cnt) | |
2831 | return; | |
2832 | ||
2833 | b = current_binding_level; | |
2834 | while (b->tag_transparent) | |
2835 | b = b->level_chain; | |
2836 | tags = b->tags; | |
2837 | while (tags) | |
2838 | { | |
2839 | /* A NULL purpose means we have already processed all tags | |
2840 | from here to the end of the list. */ | |
2841 | if (TREE_PURPOSE (tags) == NULL_TREE) | |
2842 | break; | |
2843 | if (ANON_AGGRNAME_P (TREE_PURPOSE (tags))) | |
2844 | TREE_PURPOSE (tags) = NULL_TREE; | |
2845 | tags = TREE_CHAIN (tags); | |
2846 | } | |
2847 | last_cnt = anon_cnt; | |
2848 | } | |
2849 | \f | |
2850 | /* Subroutine of duplicate_decls: return truthvalue of whether | |
2851 | or not types of these decls match. | |
2852 | ||
2853 | For C++, we must compare the parameter list so that `int' can match | |
2854 | `int&' in a parameter position, but `int&' is not confused with | |
2855 | `const int&'. */ | |
e92cc029 | 2856 | |
6060a796 | 2857 | int |
8d08fdba MS |
2858 | decls_match (newdecl, olddecl) |
2859 | tree newdecl, olddecl; | |
2860 | { | |
2861 | int types_match; | |
2862 | ||
347d73d7 ML |
2863 | if (newdecl == olddecl) |
2864 | return 1; | |
2865 | ||
6b4b3deb MM |
2866 | if (TREE_CODE (newdecl) != TREE_CODE (olddecl)) |
2867 | /* If the two DECLs are not even the same kind of thing, we're not | |
2868 | interested in their types. */ | |
2869 | return 0; | |
2870 | ||
2871 | if (TREE_CODE (newdecl) == FUNCTION_DECL) | |
8d08fdba MS |
2872 | { |
2873 | tree f1 = TREE_TYPE (newdecl); | |
2874 | tree f2 = TREE_TYPE (olddecl); | |
2875 | tree p1 = TYPE_ARG_TYPES (f1); | |
2876 | tree p2 = TYPE_ARG_TYPES (f2); | |
2877 | ||
4f1c5b7d | 2878 | if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl) |
eb68cb58 MM |
2879 | && ! (DECL_EXTERN_C_P (newdecl) |
2880 | && DECL_EXTERN_C_P (olddecl))) | |
c5a6fc45 JM |
2881 | return 0; |
2882 | ||
8d08fdba | 2883 | if (TREE_CODE (f1) != TREE_CODE (f2)) |
e867a179 | 2884 | return 0; |
8d08fdba | 2885 | |
3bfdc719 | 2886 | if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2))) |
8926095f | 2887 | { |
8b27e9ef NS |
2888 | if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl) |
2889 | && (DECL_BUILT_IN (olddecl) | |
2890 | #ifndef NO_IMPLICIT_EXTERN_C | |
2891 | || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl)) | |
2892 | || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl)) | |
2893 | #endif | |
2894 | )) | |
a28e3c7f MS |
2895 | { |
2896 | types_match = self_promoting_args_p (p1); | |
2897 | if (p1 == void_list_node) | |
2898 | TREE_TYPE (newdecl) = TREE_TYPE (olddecl); | |
2899 | } | |
8b27e9ef NS |
2900 | #ifndef NO_IMPLICIT_EXTERN_C |
2901 | else if (p1 == NULL_TREE | |
2902 | && (DECL_EXTERN_C_P (olddecl) | |
2903 | && DECL_IN_SYSTEM_HEADER (olddecl) | |
2904 | && !DECL_CLASS_SCOPE_P (olddecl)) | |
2905 | && (DECL_EXTERN_C_P (newdecl) | |
2906 | && DECL_IN_SYSTEM_HEADER (newdecl) | |
2907 | && !DECL_CLASS_SCOPE_P (newdecl))) | |
a28e3c7f MS |
2908 | { |
2909 | types_match = self_promoting_args_p (p2); | |
2910 | TREE_TYPE (newdecl) = TREE_TYPE (olddecl); | |
2911 | } | |
8b27e9ef | 2912 | #endif |
8926095f | 2913 | else |
91063b51 | 2914 | types_match = compparms (p1, p2); |
8926095f | 2915 | } |
8d08fdba MS |
2916 | else |
2917 | types_match = 0; | |
2918 | } | |
6b4b3deb | 2919 | else if (TREE_CODE (newdecl) == TEMPLATE_DECL) |
51c184be | 2920 | { |
f84b4be9 JM |
2921 | if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl), |
2922 | DECL_TEMPLATE_PARMS (olddecl))) | |
2923 | return 0; | |
68642fb6 | 2924 | |
2bb5d995 JM |
2925 | if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) |
2926 | != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))) | |
2927 | return 0; | |
2928 | ||
f84b4be9 JM |
2929 | if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL) |
2930 | types_match = 1; | |
2931 | else | |
2932 | types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl), | |
2933 | DECL_TEMPLATE_RESULT (newdecl)); | |
51c184be | 2934 | } |
8d08fdba MS |
2935 | else |
2936 | { | |
2937 | if (TREE_TYPE (newdecl) == error_mark_node) | |
2938 | types_match = TREE_TYPE (olddecl) == error_mark_node; | |
2939 | else if (TREE_TYPE (olddecl) == NULL_TREE) | |
2940 | types_match = TREE_TYPE (newdecl) == NULL_TREE; | |
a0a33927 MS |
2941 | else if (TREE_TYPE (newdecl) == NULL_TREE) |
2942 | types_match = 0; | |
8d08fdba | 2943 | else |
01240200 | 2944 | types_match = comptypes (TREE_TYPE (newdecl), |
3bfdc719 MM |
2945 | TREE_TYPE (olddecl), |
2946 | COMPARE_REDECLARATION); | |
8d08fdba MS |
2947 | } |
2948 | ||
2949 | return types_match; | |
2950 | } | |
2951 | ||
2952 | /* If NEWDECL is `static' and an `extern' was seen previously, | |
24f30ed4 | 2953 | warn about it. OLDDECL is the previous declaration. |
8d08fdba MS |
2954 | |
2955 | Note that this does not apply to the C++ case of declaring | |
2956 | a variable `extern const' and then later `const'. | |
2957 | ||
8d08fdba MS |
2958 | Don't complain about built-in functions, since they are beyond |
2959 | the user's control. */ | |
2960 | ||
2961 | static void | |
2962 | warn_extern_redeclared_static (newdecl, olddecl) | |
2963 | tree newdecl, olddecl; | |
2964 | { | |
83182544 | 2965 | static const char *const explicit_extern_static_warning |
8251199e | 2966 | = "`%D' was declared `extern' and later `static'"; |
83182544 | 2967 | static const char *const implicit_extern_static_warning |
8251199e JM |
2968 | = "`%D' was declared implicitly `extern' and later `static'"; |
2969 | ||
24f30ed4 MM |
2970 | tree name; |
2971 | ||
68642fb6 | 2972 | if (TREE_CODE (newdecl) == TYPE_DECL |
655dc6ee JM |
2973 | || TREE_CODE (newdecl) == TEMPLATE_DECL |
2974 | || TREE_CODE (newdecl) == CONST_DECL) | |
8d08fdba | 2975 | return; |
68642fb6 | 2976 | |
963d5758 MM |
2977 | /* Don't get confused by static member functions; that's a different |
2978 | use of `static'. */ | |
2979 | if (TREE_CODE (newdecl) == FUNCTION_DECL | |
2980 | && DECL_STATIC_FUNCTION_P (newdecl)) | |
2981 | return; | |
8d08fdba | 2982 | |
24f30ed4 MM |
2983 | /* If the old declaration was `static', or the new one isn't, then |
2984 | then everything is OK. */ | |
2985 | if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl)) | |
2986 | return; | |
2987 | ||
2988 | /* It's OK to declare a builtin function as `static'. */ | |
2989 | if (TREE_CODE (olddecl) == FUNCTION_DECL | |
2990 | && DECL_ARTIFICIAL (olddecl)) | |
2991 | return; | |
2992 | ||
8d08fdba | 2993 | name = DECL_ASSEMBLER_NAME (newdecl); |
33bd39a2 | 2994 | pedwarn (IDENTIFIER_IMPLICIT_DECL (name) |
24f30ed4 MM |
2995 | ? implicit_extern_static_warning |
2996 | : explicit_extern_static_warning, newdecl); | |
2997 | cp_pedwarn_at ("previous declaration of `%D'", olddecl); | |
8d08fdba MS |
2998 | } |
2999 | ||
3000 | /* Handle when a new declaration NEWDECL has the same name as an old | |
3001 | one OLDDECL in the same binding contour. Prints an error message | |
3002 | if appropriate. | |
3003 | ||
3004 | If safely possible, alter OLDDECL to look like NEWDECL, and return 1. | |
3005 | Otherwise, return 0. */ | |
3006 | ||
51c184be | 3007 | int |
8d08fdba | 3008 | duplicate_decls (newdecl, olddecl) |
824b9a4c | 3009 | tree newdecl, olddecl; |
8d08fdba | 3010 | { |
8d08fdba MS |
3011 | unsigned olddecl_uid = DECL_UID (olddecl); |
3012 | int olddecl_friend = 0, types_match = 0; | |
0b60dfe3 | 3013 | int new_defines_function = 0; |
5566b478 MS |
3014 | |
3015 | if (newdecl == olddecl) | |
3016 | return 1; | |
8d08fdba | 3017 | |
8926095f | 3018 | types_match = decls_match (newdecl, olddecl); |
8d08fdba | 3019 | |
8d08fdba MS |
3020 | /* If either the type of the new decl or the type of the old decl is an |
3021 | error_mark_node, then that implies that we have already issued an | |
3022 | error (earlier) for some bogus type specification, and in that case, | |
3023 | it is rather pointless to harass the user with yet more error message | |
0b60dfe3 | 3024 | about the same declaration, so just pretend the types match here. */ |
bd6dd845 MS |
3025 | if (TREE_TYPE (newdecl) == error_mark_node |
3026 | || TREE_TYPE (olddecl) == error_mark_node) | |
8d08fdba | 3027 | types_match = 1; |
68642fb6 | 3028 | |
97055d5c AO |
3029 | if (DECL_P (olddecl) |
3030 | && TREE_CODE (newdecl) == FUNCTION_DECL | |
3031 | && TREE_CODE (olddecl) == FUNCTION_DECL | |
3032 | && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl))) | |
3033 | { | |
3034 | if (DECL_DECLARED_INLINE_P (newdecl) | |
3035 | && DECL_UNINLINABLE (newdecl) | |
3036 | && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))) | |
3037 | /* Already warned elsewhere. */; | |
3038 | else if (DECL_DECLARED_INLINE_P (olddecl) | |
3039 | && DECL_UNINLINABLE (olddecl) | |
3040 | && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl))) | |
3041 | /* Already warned. */; | |
3042 | else if (DECL_DECLARED_INLINE_P (newdecl) | |
3043 | && DECL_UNINLINABLE (olddecl) | |
3044 | && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl))) | |
3045 | { | |
3046 | warning_with_decl (newdecl, | |
3047 | "function `%s' redeclared as inline"); | |
3048 | warning_with_decl (olddecl, | |
3049 | "previous declaration of function `%s' with attribute noinline"); | |
3050 | } | |
3051 | else if (DECL_DECLARED_INLINE_P (olddecl) | |
3052 | && DECL_UNINLINABLE (newdecl) | |
3053 | && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))) | |
3054 | { | |
3055 | warning_with_decl (newdecl, | |
3056 | "function `%s' redeclared with attribute noinline"); | |
3057 | warning_with_decl (olddecl, | |
3058 | "previous declaration of function `%s' was inline"); | |
3059 | } | |
3060 | } | |
3061 | ||
a1c65f9f | 3062 | /* Check for redeclaration and other discrepancies. */ |
d22c8596 | 3063 | if (TREE_CODE (olddecl) == FUNCTION_DECL |
9f33663b JM |
3064 | && DECL_ARTIFICIAL (olddecl)) |
3065 | { | |
3066 | if (TREE_CODE (newdecl) != FUNCTION_DECL) | |
3067 | { | |
d52e4867 RS |
3068 | /* Avoid warnings redeclaring anticipated built-ins. */ |
3069 | if (DECL_ANTICIPATED (olddecl)) | |
3070 | return 0; | |
3071 | ||
9f33663b JM |
3072 | /* If you declare a built-in or predefined function name as static, |
3073 | the old definition is overridden, but optionally warn this was a | |
3074 | bad choice of name. */ | |
3075 | if (! TREE_PUBLIC (newdecl)) | |
3076 | { | |
3077 | if (warn_shadow) | |
33bd39a2 | 3078 | warning ("shadowing %s function `%#D'", |
9f33663b JM |
3079 | DECL_BUILT_IN (olddecl) ? "built-in" : "library", |
3080 | olddecl); | |
3081 | /* Discard the old built-in function. */ | |
3082 | return 0; | |
3083 | } | |
3084 | /* If the built-in is not ansi, then programs can override | |
3085 | it even globally without an error. */ | |
3086 | else if (! DECL_BUILT_IN (olddecl)) | |
33bd39a2 | 3087 | warning ("library function `%#D' redeclared as non-function `%#D'", |
9f33663b JM |
3088 | olddecl, newdecl); |
3089 | else | |
3090 | { | |
33bd39a2 ZW |
3091 | error ("declaration of `%#D'", newdecl); |
3092 | error ("conflicts with built-in declaration `%#D'", | |
a4443a08 | 3093 | olddecl); |
9f33663b | 3094 | } |
a4443a08 MS |
3095 | return 0; |
3096 | } | |
9f33663b | 3097 | else if (!types_match) |
8d08fdba | 3098 | { |
d52e4867 RS |
3099 | /* Avoid warnings redeclaring anticipated built-ins. */ |
3100 | if (DECL_ANTICIPATED (olddecl)) | |
3101 | ; /* Do nothing yet. */ | |
3102 | else if ((DECL_EXTERN_C_P (newdecl) | |
eb68cb58 | 3103 | && DECL_EXTERN_C_P (olddecl)) |
9f33663b JM |
3104 | || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)), |
3105 | TYPE_ARG_TYPES (TREE_TYPE (olddecl)))) | |
a4443a08 | 3106 | { |
9f33663b JM |
3107 | /* A near match; override the builtin. */ |
3108 | ||
3109 | if (TREE_PUBLIC (newdecl)) | |
a4443a08 | 3110 | { |
33bd39a2 ZW |
3111 | warning ("new declaration `%#D'", newdecl); |
3112 | warning ("ambiguates built-in declaration `%#D'", | |
9f33663b | 3113 | olddecl); |
a4443a08 | 3114 | } |
9f33663b | 3115 | else if (warn_shadow) |
33bd39a2 | 3116 | warning ("shadowing %s function `%#D'", |
9f33663b JM |
3117 | DECL_BUILT_IN (olddecl) ? "built-in" : "library", |
3118 | olddecl); | |
a4443a08 | 3119 | } |
9f33663b JM |
3120 | else |
3121 | /* Discard the old built-in function. */ | |
3122 | return 0; | |
04a9d080 RS |
3123 | |
3124 | /* Replace the old RTL to avoid problems with inlining. */ | |
3125 | SET_DECL_RTL (olddecl, DECL_RTL (newdecl)); | |
8d08fdba | 3126 | } |
68642fb6 | 3127 | |
24f30ed4 MM |
3128 | if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl)) |
3129 | { | |
3130 | /* If a builtin function is redeclared as `static', merge | |
3131 | the declarations, but make the original one static. */ | |
3132 | DECL_THIS_STATIC (olddecl) = 1; | |
3133 | TREE_PUBLIC (olddecl) = 0; | |
9bfadf57 | 3134 | |
421844e7 MM |
3135 | /* Make the old declaration consistent with the new one so |
3136 | that all remnants of the builtin-ness of this function | |
3137 | will be banished. */ | |
5d2ed28c | 3138 | SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl)); |
19e7881c | 3139 | SET_DECL_RTL (olddecl, DECL_RTL (newdecl)); |
24f30ed4 | 3140 | } |
39211cd5 MS |
3141 | } |
3142 | else if (TREE_CODE (olddecl) != TREE_CODE (newdecl)) | |
3143 | { | |
9ed182dc JM |
3144 | if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl) |
3145 | && TREE_CODE (newdecl) != TYPE_DECL | |
3146 | && ! (TREE_CODE (newdecl) == TEMPLATE_DECL | |
3147 | && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)) | |
3148 | || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl) | |
3149 | && TREE_CODE (olddecl) != TYPE_DECL | |
3150 | && ! (TREE_CODE (olddecl) == TEMPLATE_DECL | |
3151 | && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) | |
3152 | == TYPE_DECL)))) | |
3153 | { | |
3154 | /* We do nothing special here, because C++ does such nasty | |
3155 | things with TYPE_DECLs. Instead, just let the TYPE_DECL | |
3156 | get shadowed, and know that if we need to find a TYPE_DECL | |
3157 | for a given name, we can look in the IDENTIFIER_TYPE_VALUE | |
3158 | slot of the identifier. */ | |
3159 | return 0; | |
3160 | } | |
3161 | ||
39211cd5 | 3162 | if ((TREE_CODE (newdecl) == FUNCTION_DECL |
5566b478 | 3163 | && DECL_FUNCTION_TEMPLATE_P (olddecl)) |
39211cd5 | 3164 | || (TREE_CODE (olddecl) == FUNCTION_DECL |
5566b478 | 3165 | && DECL_FUNCTION_TEMPLATE_P (newdecl))) |
39211cd5 | 3166 | return 0; |
9ed182dc | 3167 | |
33bd39a2 | 3168 | error ("`%#D' redeclared as different kind of symbol", newdecl); |
39211cd5 MS |
3169 | if (TREE_CODE (olddecl) == TREE_LIST) |
3170 | olddecl = TREE_VALUE (olddecl); | |
8251199e | 3171 | cp_error_at ("previous declaration of `%#D'", olddecl); |
39211cd5 MS |
3172 | |
3173 | /* New decl is completely inconsistent with the old one => | |
3174 | tell caller to replace the old one. */ | |
3175 | ||
3176 | return 0; | |
8d08fdba | 3177 | } |
8d08fdba MS |
3178 | else if (!types_match) |
3179 | { | |
4f1c5b7d | 3180 | if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)) |
7bdbfa05 MM |
3181 | /* These are certainly not duplicate declarations; they're |
3182 | from different scopes. */ | |
3183 | return 0; | |
3184 | ||
8926095f | 3185 | if (TREE_CODE (newdecl) == TEMPLATE_DECL) |
f0e01782 MS |
3186 | { |
3187 | /* The name of a class template may not be declared to refer to | |
3188 | any other template, class, function, object, namespace, value, | |
e92cc029 | 3189 | or type in the same scope. */ |
5566b478 MS |
3190 | if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL |
3191 | || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL) | |
f0e01782 | 3192 | { |
33bd39a2 | 3193 | error ("declaration of template `%#D'", newdecl); |
8251199e | 3194 | cp_error_at ("conflicts with previous declaration `%#D'", |
f0e01782 MS |
3195 | olddecl); |
3196 | } | |
ec255269 MS |
3197 | else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL |
3198 | && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL | |
3199 | && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))), | |
91063b51 | 3200 | TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl)))) |
75650646 MM |
3201 | && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl), |
3202 | DECL_TEMPLATE_PARMS (olddecl))) | |
ec255269 | 3203 | { |
33bd39a2 | 3204 | error ("new declaration `%#D'", newdecl); |
8251199e | 3205 | cp_error_at ("ambiguates old declaration `%#D'", olddecl); |
ec255269 | 3206 | } |
f0e01782 MS |
3207 | return 0; |
3208 | } | |
8926095f MS |
3209 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
3210 | { | |
eb68cb58 | 3211 | if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl)) |
8926095f | 3212 | { |
33bd39a2 | 3213 | error ("declaration of C function `%#D' conflicts with", |
8926095f | 3214 | newdecl); |
8251199e | 3215 | cp_error_at ("previous declaration `%#D' here", olddecl); |
8926095f | 3216 | } |
00595019 | 3217 | else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)), |
91063b51 | 3218 | TYPE_ARG_TYPES (TREE_TYPE (olddecl)))) |
700f8a87 | 3219 | { |
33bd39a2 | 3220 | error ("new declaration `%#D'", newdecl); |
8251199e | 3221 | cp_error_at ("ambiguates old declaration `%#D'", olddecl); |
700f8a87 MS |
3222 | } |
3223 | else | |
3224 | return 0; | |
8926095f | 3225 | } |
8d08fdba MS |
3226 | |
3227 | /* Already complained about this, so don't do so again. */ | |
a4443a08 | 3228 | else if (current_class_type == NULL_TREE |
8d08fdba MS |
3229 | || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type) |
3230 | { | |
33bd39a2 | 3231 | error ("conflicting types for `%#D'", newdecl); |
8251199e | 3232 | cp_error_at ("previous declaration as `%#D'", olddecl); |
8d08fdba MS |
3233 | } |
3234 | } | |
68642fb6 | 3235 | else if (TREE_CODE (newdecl) == FUNCTION_DECL |
75650646 MM |
3236 | && ((DECL_TEMPLATE_SPECIALIZATION (olddecl) |
3237 | && (!DECL_TEMPLATE_INFO (newdecl) | |
68642fb6 | 3238 | || (DECL_TI_TEMPLATE (newdecl) |
75650646 MM |
3239 | != DECL_TI_TEMPLATE (olddecl)))) |
3240 | || (DECL_TEMPLATE_SPECIALIZATION (newdecl) | |
3241 | && (!DECL_TEMPLATE_INFO (olddecl) | |
68642fb6 | 3242 | || (DECL_TI_TEMPLATE (olddecl) |
75650646 | 3243 | != DECL_TI_TEMPLATE (newdecl)))))) |
386b8a85 JM |
3244 | /* It's OK to have a template specialization and a non-template |
3245 | with the same type, or to have specializations of two | |
75650646 MM |
3246 | different templates with the same type. Note that if one is a |
3247 | specialization, and the other is an instantiation of the same | |
3248 | template, that we do not exit at this point. That situation | |
3249 | can occur if we instantiate a template class, and then | |
0e339752 | 3250 | specialize one of its methods. This situation is valid, but |
75650646 MM |
3251 | the declarations must be merged in the usual way. */ |
3252 | return 0; | |
68642fb6 UD |
3253 | else if (TREE_CODE (newdecl) == FUNCTION_DECL |
3254 | && ((DECL_TEMPLATE_INSTANTIATION (olddecl) | |
75650646 MM |
3255 | && !DECL_USE_TEMPLATE (newdecl)) |
3256 | || (DECL_TEMPLATE_INSTANTIATION (newdecl) | |
3257 | && !DECL_USE_TEMPLATE (olddecl)))) | |
3258 | /* One of the declarations is a template instantiation, and the | |
3259 | other is not a template at all. That's OK. */ | |
386b8a85 | 3260 | return 0; |
85c6cbaf ML |
3261 | else if (TREE_CODE (newdecl) == NAMESPACE_DECL |
3262 | && DECL_NAMESPACE_ALIAS (newdecl) | |
3263 | && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl)) | |
a1c65f9f | 3264 | /* Redeclaration of namespace alias, ignore it. */ |
85c6cbaf | 3265 | return 1; |
8d08fdba MS |
3266 | else |
3267 | { | |
d8e178a0 | 3268 | const char *errmsg = redeclaration_error_message (newdecl, olddecl); |
8251199e | 3269 | if (errmsg) |
8d08fdba | 3270 | { |
33bd39a2 | 3271 | error (errmsg, newdecl); |
8d08fdba MS |
3272 | if (DECL_NAME (olddecl) != NULL_TREE) |
3273 | cp_error_at ((DECL_INITIAL (olddecl) | |
2c73f9f5 | 3274 | && namespace_bindings_p ()) |
8251199e JM |
3275 | ? "`%#D' previously defined here" |
3276 | : "`%#D' previously declared here", olddecl); | |
00fae85d | 3277 | return 0; |
8d08fdba MS |
3278 | } |
3279 | else if (TREE_CODE (olddecl) == FUNCTION_DECL | |
3280 | && DECL_INITIAL (olddecl) != NULL_TREE | |
3281 | && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE | |
3282 | && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE) | |
3283 | { | |
3284 | /* Prototype decl follows defn w/o prototype. */ | |
8251199e JM |
3285 | cp_warning_at ("prototype for `%#D'", newdecl); |
3286 | cp_warning_at ("follows non-prototype definition here", olddecl); | |
8d08fdba MS |
3287 | } |
3288 | else if (TREE_CODE (olddecl) == FUNCTION_DECL | |
3289 | && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl)) | |
8926095f MS |
3290 | { |
3291 | /* extern "C" int foo (); | |
3292 | int foo () { bar (); } | |
3293 | is OK. */ | |
46ccf50a | 3294 | if (current_lang_depth () == 0) |
5d2ed28c | 3295 | SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl)); |
8926095f MS |
3296 | else |
3297 | { | |
8251199e | 3298 | cp_error_at ("previous declaration of `%#D' with %L linkage", |
8926095f | 3299 | olddecl, DECL_LANGUAGE (olddecl)); |
33bd39a2 | 3300 | error ("conflicts with new declaration with %L linkage", |
8926095f MS |
3301 | DECL_LANGUAGE (newdecl)); |
3302 | } | |
3303 | } | |
e1cd6e56 | 3304 | |
a6f02587 | 3305 | if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl)) |
c32381b1 JM |
3306 | ; |
3307 | else if (TREE_CODE (olddecl) == FUNCTION_DECL) | |
e1cd6e56 MS |
3308 | { |
3309 | tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl)); | |
3310 | tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)); | |
3311 | int i = 1; | |
3312 | ||
3313 | if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE) | |
3314 | t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2); | |
68642fb6 | 3315 | |
e1cd6e56 MS |
3316 | for (; t1 && t1 != void_list_node; |
3317 | t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++) | |
3318 | if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2)) | |
3319 | { | |
2507f3b5 RK |
3320 | if (1 == simple_cst_equal (TREE_PURPOSE (t1), |
3321 | TREE_PURPOSE (t2))) | |
e1cd6e56 | 3322 | { |
624a5800 CR |
3323 | pedwarn ("default argument given for parameter %d of `%#D'", |
3324 | i, newdecl); | |
3325 | cp_pedwarn_at ("after previous specification in `%#D'", | |
3326 | olddecl); | |
e1cd6e56 MS |
3327 | } |
3328 | else | |
3329 | { | |
33bd39a2 | 3330 | error ("default argument given for parameter %d of `%#D'", |
e1cd6e56 | 3331 | i, newdecl); |
8251199e | 3332 | cp_error_at ("after previous specification in `%#D'", |
e1cd6e56 MS |
3333 | olddecl); |
3334 | } | |
3335 | } | |
a5894242 | 3336 | |
79065db2 MM |
3337 | if (DECL_DECLARED_INLINE_P (newdecl) |
3338 | && ! DECL_DECLARED_INLINE_P (olddecl) | |
7fcdf4c2 | 3339 | && TREE_ADDRESSABLE (olddecl) && warn_inline) |
dff6b454 | 3340 | { |
33bd39a2 | 3341 | warning ("`%#D' was used before it was declared inline", |
7fcdf4c2 | 3342 | newdecl); |
8251199e | 3343 | cp_warning_at ("previous non-inline declaration here", |
7fcdf4c2 | 3344 | olddecl); |
dff6b454 | 3345 | } |
e1cd6e56 | 3346 | } |
8d08fdba MS |
3347 | } |
3348 | ||
4782bd5b RS |
3349 | /* Do not merge an implicit typedef with an explicit one. In: |
3350 | ||
3351 | class A; | |
3352 | ... | |
3353 | typedef class A A __attribute__ ((foo)); | |
3354 | ||
3355 | the attribute should apply only to the typedef. */ | |
3356 | if (TREE_CODE (olddecl) == TYPE_DECL | |
3357 | && (DECL_IMPLICIT_TYPEDEF_P (olddecl) | |
3358 | || DECL_IMPLICIT_TYPEDEF_P (newdecl))) | |
3359 | return 0; | |
3360 | ||
8d08fdba MS |
3361 | /* If new decl is `static' and an `extern' was seen previously, |
3362 | warn about it. */ | |
3363 | warn_extern_redeclared_static (newdecl, olddecl); | |
3364 | ||
e92cc029 | 3365 | /* We have committed to returning 1 at this point. */ |
8d08fdba MS |
3366 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
3367 | { | |
3368 | /* Now that functions must hold information normally held | |
3369 | by field decls, there is extra work to do so that | |
3370 | declaration information does not get destroyed during | |
3371 | definition. */ | |
3372 | if (DECL_VINDEX (olddecl)) | |
3373 | DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl); | |
4f1c5b7d MM |
3374 | if (DECL_VIRTUAL_CONTEXT (olddecl)) |
3375 | DECL_VIRTUAL_CONTEXT (newdecl) = DECL_VIRTUAL_CONTEXT (olddecl); | |
8d08fdba MS |
3376 | if (DECL_CONTEXT (olddecl)) |
3377 | DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl); | |
e1cd6e56 MS |
3378 | DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl); |
3379 | DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl); | |
fee7654e | 3380 | DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl); |
864b83b9 | 3381 | DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl); |
4a67c9e9 | 3382 | DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl); |
963d5758 | 3383 | DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl); |
e0fff4b3 JM |
3384 | if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK) |
3385 | SET_OVERLOADED_OPERATOR_CODE | |
3386 | (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl)); | |
0b60dfe3 | 3387 | new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE; |
68642fb6 | 3388 | |
0b60dfe3 BK |
3389 | /* Optionally warn about more than one declaration for the same |
3390 | name, but don't warn about a function declaration followed by a | |
3391 | definition. */ | |
3392 | if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl) | |
3393 | && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE) | |
a1c65f9f | 3394 | /* Don't warn about extern decl followed by definition. */ |
0b60dfe3 | 3395 | && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl)) |
a1c65f9f | 3396 | /* Don't warn about friends, let add_friend take care of it. */ |
3581fae0 | 3397 | && ! (DECL_FRIEND_P (newdecl) || DECL_FRIEND_P (olddecl))) |
0b60dfe3 | 3398 | { |
33bd39a2 | 3399 | warning ("redundant redeclaration of `%D' in same scope", newdecl); |
8251199e | 3400 | cp_warning_at ("previous declaration of `%D'", olddecl); |
0b60dfe3 | 3401 | } |
8d08fdba MS |
3402 | } |
3403 | ||
3404 | /* Deal with C++: must preserve virtual function table size. */ | |
3405 | if (TREE_CODE (olddecl) == TYPE_DECL) | |
3406 | { | |
3407 | register tree newtype = TREE_TYPE (newdecl); | |
3408 | register tree oldtype = TREE_TYPE (olddecl); | |
3409 | ||
3410 | if (newtype != error_mark_node && oldtype != error_mark_node | |
3411 | && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype)) | |
3412 | { | |
3413 | CLASSTYPE_VSIZE (newtype) = CLASSTYPE_VSIZE (oldtype); | |
3414 | CLASSTYPE_FRIEND_CLASSES (newtype) | |
3415 | = CLASSTYPE_FRIEND_CLASSES (oldtype); | |
3416 | } | |
970d6386 MM |
3417 | |
3418 | DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl); | |
8d08fdba MS |
3419 | } |
3420 | ||
8d08fdba MS |
3421 | /* Copy all the DECL_... slots specified in the new decl |
3422 | except for any that we copy here from the old type. */ | |
91d231cb | 3423 | DECL_ATTRIBUTES (newdecl) |
f6897b10 | 3424 | = (*targetm.merge_decl_attributes) (olddecl, newdecl); |
8d08fdba | 3425 | |
5566b478 MS |
3426 | if (TREE_CODE (newdecl) == TEMPLATE_DECL) |
3427 | { | |
4d85e00e | 3428 | TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl)); |
68642fb6 | 3429 | DECL_TEMPLATE_SPECIALIZATIONS (olddecl) |
4d85e00e MM |
3430 | = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl), |
3431 | DECL_TEMPLATE_SPECIALIZATIONS (newdecl)); | |
68642fb6 | 3432 | |
daa8df65 MM |
3433 | /* If the new declaration is a definition, update the file and |
3434 | line information on the declaration. */ | |
06d5e633 NS |
3435 | if (DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)) == NULL_TREE |
3436 | && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl)) != NULL_TREE) | |
daa8df65 | 3437 | { |
3e72ec9a GDR |
3438 | DECL_SOURCE_LOCATION (olddecl) |
3439 | = DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (olddecl)) | |
3440 | = DECL_SOURCE_LOCATION (newdecl); | |
daa8df65 MM |
3441 | } |
3442 | ||
5566b478 MS |
3443 | return 1; |
3444 | } | |
68642fb6 | 3445 | |
8d08fdba MS |
3446 | if (types_match) |
3447 | { | |
3448 | /* Automatically handles default parameters. */ | |
3449 | tree oldtype = TREE_TYPE (olddecl); | |
e1cd6e56 | 3450 | tree newtype; |
8d08fdba | 3451 | |
e1cd6e56 | 3452 | /* Merge the data types specified in the two decls. */ |
6da794e8 | 3453 | newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl)); |
e1cd6e56 | 3454 | |
6da794e8 | 3455 | /* If merge_types produces a non-typedef type, just use the old type. */ |
9076e292 JM |
3456 | if (TREE_CODE (newdecl) == TYPE_DECL |
3457 | && newtype == DECL_ORIGINAL_TYPE (newdecl)) | |
3458 | newtype = oldtype; | |
3459 | ||
8d08fdba | 3460 | if (TREE_CODE (newdecl) == VAR_DECL) |
17bbb839 MM |
3461 | { |
3462 | DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl); | |
3463 | DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl); | |
3464 | } | |
3465 | ||
6da794e8 | 3466 | /* Do this after calling `merge_types' so that default |
8d08fdba MS |
3467 | parameters don't confuse us. */ |
3468 | else if (TREE_CODE (newdecl) == FUNCTION_DECL | |
3469 | && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)) | |
3470 | != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)))) | |
3471 | { | |
f30432d7 | 3472 | TREE_TYPE (newdecl) = build_exception_variant (newtype, |
8d08fdba | 3473 | TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))); |
f30432d7 | 3474 | TREE_TYPE (olddecl) = build_exception_variant (newtype, |
8d08fdba MS |
3475 | TYPE_RAISES_EXCEPTIONS (oldtype)); |
3476 | ||
9a224b4a JM |
3477 | if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl)) |
3478 | && DECL_SOURCE_LINE (olddecl) != 0 | |
da20811c | 3479 | && flag_exceptions |
4cc1d462 NS |
3480 | && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)), |
3481 | TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1)) | |
8d08fdba | 3482 | { |
33bd39a2 | 3483 | error ("declaration of `%F' throws different exceptions", |
a28e3c7f | 3484 | newdecl); |
a09ba2e0 | 3485 | cp_error_at ("than previous declaration `%F'", olddecl); |
8d08fdba MS |
3486 | } |
3487 | } | |
3488 | TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype; | |
3489 | ||
3490 | /* Lay the type out, unless already done. */ | |
407f03b8 | 3491 | if (! same_type_p (newtype, oldtype) |
5566b478 | 3492 | && TREE_TYPE (newdecl) != error_mark_node |
5156628f | 3493 | && !(processing_template_decl && uses_template_parms (newdecl))) |
b7484fbe MS |
3494 | layout_type (TREE_TYPE (newdecl)); |
3495 | ||
5566b478 MS |
3496 | if ((TREE_CODE (newdecl) == VAR_DECL |
3497 | || TREE_CODE (newdecl) == PARM_DECL | |
3498 | || TREE_CODE (newdecl) == RESULT_DECL | |
3499 | || TREE_CODE (newdecl) == FIELD_DECL | |
3500 | || TREE_CODE (newdecl) == TYPE_DECL) | |
5156628f | 3501 | && !(processing_template_decl && uses_template_parms (newdecl))) |
b7484fbe | 3502 | layout_decl (newdecl, 0); |
8d08fdba MS |
3503 | |
3504 | /* Merge the type qualifiers. */ | |
3505 | if (TREE_READONLY (newdecl)) | |
3506 | TREE_READONLY (olddecl) = 1; | |
3507 | if (TREE_THIS_VOLATILE (newdecl)) | |
3508 | TREE_THIS_VOLATILE (olddecl) = 1; | |
3509 | ||
3510 | /* Merge the initialization information. */ | |
8926095f MS |
3511 | if (DECL_INITIAL (newdecl) == NULL_TREE |
3512 | && DECL_INITIAL (olddecl) != NULL_TREE) | |
3513 | { | |
3514 | DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl); | |
3e72ec9a | 3515 | DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl); |
b0d06515 MM |
3516 | if (CAN_HAVE_FULL_LANG_DECL_P (newdecl) |
3517 | && DECL_LANG_SPECIFIC (newdecl) | |
4d85e00e MM |
3518 | && DECL_LANG_SPECIFIC (olddecl)) |
3519 | DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl); | |
8926095f | 3520 | } |
39211cd5 MS |
3521 | |
3522 | /* Merge the section attribute. | |
3523 | We want to issue an error if the sections conflict but that must be | |
3524 | done later in decl_attributes since we are called before attributes | |
3525 | are assigned. */ | |
3526 | if (DECL_SECTION_NAME (newdecl) == NULL_TREE) | |
3527 | DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl); | |
3528 | ||
a157febd GK |
3529 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
3530 | { | |
3531 | DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl) | |
3532 | |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl); | |
a157febd GK |
3533 | DECL_NO_LIMIT_STACK (newdecl) |
3534 | |= DECL_NO_LIMIT_STACK (olddecl); | |
c740732f MM |
3535 | /* Keep the old RTL. */ |
3536 | COPY_DECL_RTL (olddecl, newdecl); | |
3537 | } | |
3538 | else if (TREE_CODE (newdecl) == VAR_DECL | |
3539 | && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl))) | |
3540 | { | |
3541 | /* Keep the old RTL. We cannot keep the old RTL if the old | |
3542 | declaration was for an incomplete object and the new | |
3543 | declaration is not since many attributes of the RTL will | |
3544 | change. */ | |
3545 | COPY_DECL_RTL (olddecl, newdecl); | |
a157febd | 3546 | } |
8d08fdba MS |
3547 | } |
3548 | /* If cannot merge, then use the new type and qualifiers, | |
3549 | and don't preserve the old rtl. */ | |
3550 | else | |
3551 | { | |
3552 | /* Clean out any memory we had of the old declaration. */ | |
3553 | tree oldstatic = value_member (olddecl, static_aggregates); | |
3554 | if (oldstatic) | |
3555 | TREE_VALUE (oldstatic) = error_mark_node; | |
3556 | ||
3557 | TREE_TYPE (olddecl) = TREE_TYPE (newdecl); | |
3558 | TREE_READONLY (olddecl) = TREE_READONLY (newdecl); | |
3559 | TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl); | |
3560 | TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl); | |
3561 | } | |
3562 | ||
3563 | /* Merge the storage class information. */ | |
45806a3f FS |
3564 | merge_weak (newdecl, olddecl); |
3565 | ||
e92cc029 | 3566 | DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl); |
794d4a61 | 3567 | DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl); |
a9aedbc2 MS |
3568 | TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl); |
3569 | TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl); | |
3570 | if (! DECL_EXTERNAL (olddecl)) | |
3571 | DECL_EXTERNAL (newdecl) = 0; | |
68642fb6 | 3572 | |
0b60dfe3 | 3573 | if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl)) |
8d08fdba | 3574 | { |
a9aedbc2 MS |
3575 | DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl); |
3576 | DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl); | |
77be6f82 | 3577 | DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl); |
68642fb6 | 3578 | DECL_TEMPLATE_INSTANTIATED (newdecl) |
2b0a63a3 | 3579 | |= DECL_TEMPLATE_INSTANTIATED (olddecl); |
0b60dfe3 BK |
3580 | /* Don't really know how much of the language-specific |
3581 | values we should copy from old to new. */ | |
3582 | DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl); | |
e2500fed GK |
3583 | DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 = |
3584 | DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2; | |
0b60dfe3 | 3585 | DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl); |
fbf1c34b | 3586 | DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl); |
3febd123 NS |
3587 | DECL_INITIALIZED_IN_CLASS_P (newdecl) |
3588 | |= DECL_INITIALIZED_IN_CLASS_P (olddecl); | |
0b60dfe3 | 3589 | olddecl_friend = DECL_FRIEND_P (olddecl); |
6a629cac MM |
3590 | |
3591 | /* Only functions have DECL_BEFRIENDING_CLASSES. */ | |
3592 | if (TREE_CODE (newdecl) == FUNCTION_DECL | |
3593 | || DECL_FUNCTION_TEMPLATE_P (newdecl)) | |
3594 | DECL_BEFRIENDING_CLASSES (newdecl) | |
3595 | = chainon (DECL_BEFRIENDING_CLASSES (newdecl), | |
3596 | DECL_BEFRIENDING_CLASSES (olddecl)); | |
8d08fdba MS |
3597 | } |
3598 | ||
8d08fdba MS |
3599 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
3600 | { | |
68642fb6 UD |
3601 | if (DECL_TEMPLATE_INSTANTIATION (olddecl) |
3602 | && !DECL_TEMPLATE_INSTANTIATION (newdecl)) | |
75650646 MM |
3603 | { |
3604 | /* If newdecl is not a specialization, then it is not a | |
3605 | template-related function at all. And that means that we | |
3606 | shoud have exited above, returning 0. */ | |
3607 | my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl), | |
3608 | 0); | |
3609 | ||
68642fb6 | 3610 | if (TREE_USED (olddecl)) |
75650646 | 3611 | /* From [temp.expl.spec]: |
68642fb6 | 3612 | |
75650646 MM |
3613 | If a template, a member template or the member of a class |
3614 | template is explicitly specialized then that | |
3615 | specialization shall be declared before the first use of | |
3616 | that specialization that would cause an implicit | |
3617 | instantiation to take place, in every translation unit in | |
3618 | which such a use occurs. */ | |
33bd39a2 | 3619 | error ("explicit specialization of %D after first use", |
75650646 MM |
3620 | olddecl); |
3621 | ||
3622 | SET_DECL_TEMPLATE_SPECIALIZATION (olddecl); | |
d03d18e8 KL |
3623 | |
3624 | /* [temp.expl.spec/14] We don't inline explicit specialization | |
3625 | just because the primary template says so. */ | |
75650646 | 3626 | } |
d03d18e8 KL |
3627 | else |
3628 | { | |
3629 | if (DECL_PENDING_INLINE_INFO (newdecl) == 0) | |
3630 | DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl); | |
faae18ab | 3631 | |
d03d18e8 | 3632 | DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl); |
faae18ab | 3633 | |
d03d18e8 KL |
3634 | /* If either decl says `inline', this fn is inline, unless |
3635 | its definition was passed already. */ | |
3636 | if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE) | |
3637 | DECL_INLINE (olddecl) = 1; | |
3638 | DECL_INLINE (newdecl) = DECL_INLINE (olddecl); | |
3639 | ||
3640 | DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl) | |
3641 | = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)); | |
3642 | } | |
97055d5c | 3643 | |
5daf7c0a JM |
3644 | /* Preserve abstractness on cloned [cd]tors. */ |
3645 | DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl); | |
3646 | ||
700f8a87 MS |
3647 | if (! types_match) |
3648 | { | |
5d2ed28c | 3649 | SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl)); |
92643fea | 3650 | COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl); |
19e7881c | 3651 | SET_DECL_RTL (olddecl, DECL_RTL (newdecl)); |
5566b478 MS |
3652 | } |
3653 | if (! types_match || new_defines_function) | |
3654 | { | |
6f1b4c42 JM |
3655 | /* These need to be copied so that the names are available. |
3656 | Note that if the types do match, we'll preserve inline | |
3657 | info and other bits, but if not, we won't. */ | |
700f8a87 MS |
3658 | DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl); |
3659 | DECL_RESULT (olddecl) = DECL_RESULT (newdecl); | |
700f8a87 | 3660 | } |
8d08fdba MS |
3661 | if (new_defines_function) |
3662 | /* If defining a function declared with other language | |
3663 | linkage, use the previously declared language linkage. */ | |
5d2ed28c | 3664 | SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl)); |
6f1b4c42 | 3665 | else if (types_match) |
8d08fdba MS |
3666 | { |
3667 | /* If redeclaring a builtin function, and not a definition, | |
3668 | it stays built in. */ | |
3669 | if (DECL_BUILT_IN (olddecl)) | |
3670 | { | |
26db82d8 | 3671 | DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl); |
39211cd5 | 3672 | DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl); |
8d08fdba MS |
3673 | /* If we're keeping the built-in definition, keep the rtl, |
3674 | regardless of declaration matches. */ | |
19e7881c | 3675 | SET_DECL_RTL (newdecl, DECL_RTL (olddecl)); |
8d08fdba MS |
3676 | } |
3677 | else | |
b850de4f | 3678 | DECL_NUM_STMTS (newdecl) = DECL_NUM_STMTS (olddecl); |
8d08fdba MS |
3679 | |
3680 | DECL_RESULT (newdecl) = DECL_RESULT (olddecl); | |
8d08fdba MS |
3681 | /* Don't clear out the arguments if we're redefining a function. */ |
3682 | if (DECL_ARGUMENTS (olddecl)) | |
3683 | DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl); | |
3684 | } | |
3685 | } | |
0b1161fc MM |
3686 | else if (TREE_CODE (newdecl) == NAMESPACE_DECL) |
3687 | NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl); | |
a9aedbc2 | 3688 | |
8d08fdba MS |
3689 | /* Now preserve various other info from the definition. */ |
3690 | TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl); | |
3691 | TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl); | |
3692 | DECL_COMMON (newdecl) = DECL_COMMON (olddecl); | |
92643fea | 3693 | COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl); |
8d08fdba | 3694 | |
8d08fdba MS |
3695 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
3696 | { | |
3697 | int function_size; | |
8d08fdba MS |
3698 | |
3699 | function_size = sizeof (struct tree_decl); | |
3700 | ||
730e1556 KG |
3701 | memcpy ((char *) olddecl + sizeof (struct tree_common), |
3702 | (char *) newdecl + sizeof (struct tree_common), | |
3703 | function_size - sizeof (struct tree_common)); | |
8d08fdba | 3704 | |
75650646 MM |
3705 | if (DECL_TEMPLATE_INSTANTIATION (newdecl)) |
3706 | { | |
3707 | /* If newdecl is a template instantiation, it is possible that | |
3708 | the following sequence of events has occurred: | |
3709 | ||
3710 | o A friend function was declared in a class template. The | |
68642fb6 | 3711 | class template was instantiated. |
75650646 | 3712 | |
68642fb6 UD |
3713 | o The instantiation of the friend declaration was |
3714 | recorded on the instantiation list, and is newdecl. | |
75650646 MM |
3715 | |
3716 | o Later, however, instantiate_class_template called pushdecl | |
3717 | on the newdecl to perform name injection. But, pushdecl in | |
3718 | turn called duplicate_decls when it discovered that another | |
3719 | declaration of a global function with the same name already | |
68642fb6 | 3720 | existed. |
75650646 MM |
3721 | |
3722 | o Here, in duplicate_decls, we decided to clobber newdecl. | |
3723 | ||
3724 | If we're going to do that, we'd better make sure that | |
3725 | olddecl, and not newdecl, is on the list of | |
3726 | instantiations so that if we try to do the instantiation | |
3727 | again we won't get the clobbered declaration. */ | |
3728 | ||
68642fb6 UD |
3729 | tree tmpl = DECL_TI_TEMPLATE (newdecl); |
3730 | tree decls = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); | |
75650646 MM |
3731 | |
3732 | for (; decls; decls = TREE_CHAIN (decls)) | |
3733 | if (TREE_VALUE (decls) == newdecl) | |
3734 | TREE_VALUE (decls) = olddecl; | |
3735 | } | |
8d08fdba MS |
3736 | } |
3737 | else | |
3738 | { | |
730e1556 KG |
3739 | memcpy ((char *) olddecl + sizeof (struct tree_common), |
3740 | (char *) newdecl + sizeof (struct tree_common), | |
3741 | sizeof (struct tree_decl) - sizeof (struct tree_common) | |
2f9834e8 | 3742 | + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *)); |
8d08fdba MS |
3743 | } |
3744 | ||
3745 | DECL_UID (olddecl) = olddecl_uid; | |
3746 | if (olddecl_friend) | |
3747 | DECL_FRIEND_P (olddecl) = 1; | |
3748 | ||
d9525bec BK |
3749 | /* NEWDECL contains the merged attribute lists. |
3750 | Update OLDDECL to be the same. */ | |
91d231cb | 3751 | DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl); |
d9525bec | 3752 | |
8d08fdba MS |
3753 | return 1; |
3754 | } | |
3755 | ||
3756 | /* Record a decl-node X as belonging to the current lexical scope. | |
3757 | Check for errors (such as an incompatible declaration for the same | |
3758 | name already seen in the same scope). | |
3759 | ||
3760 | Returns either X or an old decl for the same name. | |
3761 | If an old decl is returned, it may have been smashed | |
3762 | to agree with what X says. */ | |
3763 | ||
3764 | tree | |
3765 | pushdecl (x) | |
3766 | tree x; | |
3767 | { | |
3768 | register tree t; | |
b35d4555 MM |
3769 | register tree name; |
3770 | int need_new_binding; | |
3771 | ||
3772 | /* We shouldn't be calling pushdecl when we're generating RTL for a | |
3773 | function that we already did semantic analysis on previously. */ | |
01d939e8 | 3774 | my_friendly_assert (!cfun || doing_semantic_analysis_p (), |
b35d4555 MM |
3775 | 19990913); |
3776 | ||
b35d4555 | 3777 | need_new_binding = 1; |
8d08fdba | 3778 | |
50714e79 MM |
3779 | if (DECL_TEMPLATE_PARM_P (x)) |
3780 | /* Template parameters have no context; they are not X::T even | |
3781 | when declared within a class or namespace. */ | |
3782 | ; | |
3783 | else | |
3784 | { | |
3785 | if (current_function_decl && x != current_function_decl | |
3786 | /* A local declaration for a function doesn't constitute | |
3787 | nesting. */ | |
f39ee884 MM |
3788 | && !(TREE_CODE (x) == FUNCTION_DECL && !DECL_INITIAL (x)) |
3789 | /* A local declaration for an `extern' variable is in the | |
94dfccd1 | 3790 | scope of the current namespace, not the current |
f39ee884 MM |
3791 | function. */ |
3792 | && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x)) | |
50714e79 MM |
3793 | && !DECL_CONTEXT (x)) |
3794 | DECL_CONTEXT (x) = current_function_decl; | |
cd9f6678 MM |
3795 | |
3796 | /* If this is the declaration for a namespace-scope function, | |
3797 | but the declaration itself is in a local scope, mark the | |
3798 | declaration. */ | |
68642fb6 | 3799 | if (TREE_CODE (x) == FUNCTION_DECL |
cd9f6678 MM |
3800 | && DECL_NAMESPACE_SCOPE_P (x) |
3801 | && current_function_decl | |
3802 | && x != current_function_decl) | |
3803 | DECL_LOCAL_FUNCTION_P (x) = 1; | |
50714e79 | 3804 | } |
8d08fdba | 3805 | |
fe8fadc1 | 3806 | name = DECL_NAME (x); |
8d08fdba MS |
3807 | if (name) |
3808 | { | |
94dfccd1 JM |
3809 | int different_binding_level = 0; |
3810 | ||
386b8a85 JM |
3811 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) |
3812 | name = TREE_OPERAND (name, 0); | |
68642fb6 | 3813 | |
94dfccd1 JM |
3814 | /* In case this decl was explicitly namespace-qualified, look it |
3815 | up in its namespace context. */ | |
3816 | if (TREE_CODE (x) == VAR_DECL && DECL_NAMESPACE_SCOPE_P (x) | |
3817 | && namespace_bindings_p ()) | |
2c73f9f5 ML |
3818 | t = namespace_binding (name, DECL_CONTEXT (x)); |
3819 | else | |
3820 | t = lookup_name_current_level (name); | |
fe8fadc1 | 3821 | |
94dfccd1 JM |
3822 | /* [basic.link] If there is a visible declaration of an entity |
3823 | with linkage having the same name and type, ignoring entities | |
3824 | declared outside the innermost enclosing namespace scope, the | |
3825 | block scope declaration declares that same entity and | |
3826 | receives the linkage of the previous declaration. */ | |
3827 | if (! t && current_function_decl && x != current_function_decl | |
3828 | && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL) | |
3829 | && DECL_EXTERNAL (x)) | |
3830 | { | |
3831 | /* Look in block scope. */ | |
3832 | t = IDENTIFIER_VALUE (name); | |
3833 | /* Or in the innermost namespace. */ | |
3834 | if (! t) | |
3835 | t = namespace_binding (name, DECL_CONTEXT (x)); | |
fab09a24 JM |
3836 | /* Does it have linkage? Note that if this isn't a DECL, it's an |
3837 | OVERLOAD, which is OK. */ | |
3838 | if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t))) | |
94dfccd1 JM |
3839 | t = NULL_TREE; |
3840 | if (t) | |
3841 | different_binding_level = 1; | |
3842 | } | |
3843 | ||
fe8fadc1 MM |
3844 | /* If we are declaring a function, and the result of name-lookup |
3845 | was an OVERLOAD, look for an overloaded instance that is | |
3846 | actually the same as the function we are declaring. (If | |
3847 | there is one, we have to merge our declaration with the | |
3848 | previous declaration.) */ | |
655dc6ee | 3849 | if (t && TREE_CODE (t) == OVERLOAD) |
fe8fadc1 MM |
3850 | { |
3851 | tree match; | |
3852 | ||
655dc6ee JM |
3853 | if (TREE_CODE (x) == FUNCTION_DECL) |
3854 | for (match = t; match; match = OVL_NEXT (match)) | |
3855 | { | |
92643fea | 3856 | if (decls_match (OVL_CURRENT (match), x)) |
655dc6ee JM |
3857 | break; |
3858 | } | |
3859 | else | |
3860 | /* Just choose one. */ | |
3861 | match = t; | |
fe8fadc1 MM |
3862 | |
3863 | if (match) | |
3864 | t = OVL_CURRENT (match); | |
3865 | else | |
3866 | t = NULL_TREE; | |
3867 | } | |
3868 | ||
8d08fdba MS |
3869 | if (t == error_mark_node) |
3870 | { | |
3871 | /* error_mark_node is 0 for a while during initialization! */ | |
3872 | t = NULL_TREE; | |
8251199e | 3873 | cp_error_at ("`%#D' used prior to declaration", x); |
8d08fdba | 3874 | } |
51c184be | 3875 | else if (t != NULL_TREE) |
8d08fdba | 3876 | { |
94dfccd1 JM |
3877 | if (different_binding_level) |
3878 | { | |
3879 | if (decls_match (x, t)) | |
3880 | /* The standard only says that the local extern | |
3881 | inherits linkage from the previous decl; in | |
3882 | particular, default args are not shared. It would | |
3883 | be nice to propagate inlining info, though. FIXME. */ | |
3884 | TREE_PUBLIC (x) = TREE_PUBLIC (t); | |
3885 | } | |
3886 | else if (TREE_CODE (t) == PARM_DECL) | |
8d08fdba MS |
3887 | { |
3888 | if (DECL_CONTEXT (t) == NULL_TREE) | |
400500c4 RK |
3889 | /* This is probaby caused by too many errors, but calling |
3890 | abort will say that if errors have occurred. */ | |
3891 | abort (); | |
be99da77 | 3892 | |
e92cc029 | 3893 | /* Check for duplicate params. */ |
be99da77 MS |
3894 | if (duplicate_decls (x, t)) |
3895 | return t; | |
8d08fdba | 3896 | } |
eb68cb58 | 3897 | else if ((DECL_EXTERN_C_FUNCTION_P (x) |
5566b478 MS |
3898 | || DECL_FUNCTION_TEMPLATE_P (x)) |
3899 | && is_overloaded_fn (t)) | |
a1c65f9f | 3900 | /* Don't do anything just yet. */; |
e1cd6e56 MS |
3901 | else if (t == wchar_decl_node) |
3902 | { | |
3903 | if (pedantic && ! DECL_IN_SYSTEM_HEADER (x)) | |
33bd39a2 | 3904 | pedwarn ("redeclaration of `wchar_t' as `%T'", |
2b0cbc5d | 3905 | TREE_TYPE (x)); |
e1cd6e56 MS |
3906 | |
3907 | /* Throw away the redeclaration. */ | |
3908 | return t; | |
3909 | } | |
8926095f | 3910 | else if (TREE_CODE (t) != TREE_CODE (x)) |
8d08fdba | 3911 | { |
9ed182dc | 3912 | if (duplicate_decls (x, t)) |
51c184be | 3913 | return t; |
8d08fdba MS |
3914 | } |
3915 | else if (duplicate_decls (x, t)) | |
51c184be | 3916 | { |
7177d104 MS |
3917 | if (TREE_CODE (t) == TYPE_DECL) |
3918 | SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t)); | |
f30432d7 MS |
3919 | else if (TREE_CODE (t) == FUNCTION_DECL) |
3920 | check_default_args (t); | |
7177d104 | 3921 | |
51c184be MS |
3922 | return t; |
3923 | } | |
35680744 MM |
3924 | else if (DECL_MAIN_P (x)) |
3925 | { | |
3926 | /* A redeclaration of main, but not a duplicate of the | |
68642fb6 | 3927 | previous one. |
35680744 MM |
3928 | |
3929 | [basic.start.main] | |
3930 | ||
3931 | This function shall not be overloaded. */ | |
8251199e | 3932 | cp_error_at ("invalid redeclaration of `%D'", t); |
33bd39a2 | 3933 | error ("as `%D'", x); |
35680744 MM |
3934 | /* We don't try to push this declaration since that |
3935 | causes a crash. */ | |
3936 | return x; | |
3937 | } | |
8d08fdba | 3938 | } |
8926095f | 3939 | |
f3400fe2 JM |
3940 | check_template_shadow (x); |
3941 | ||
fcfcdfc8 JM |
3942 | /* If this is a function conjured up by the backend, massage it |
3943 | so it looks friendly. */ | |
eb68cb58 | 3944 | if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x)) |
fcfcdfc8 JM |
3945 | { |
3946 | retrofit_lang_decl (x); | |
5d2ed28c | 3947 | SET_DECL_LANGUAGE (x, lang_c); |
fcfcdfc8 JM |
3948 | } |
3949 | ||
eb68cb58 | 3950 | if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x)) |
8926095f | 3951 | { |
7bdbfa05 | 3952 | t = push_overloaded_decl (x, PUSH_LOCAL); |
5fdaba89 | 3953 | if (t != x) |
8926095f | 3954 | return t; |
f181d4ae MM |
3955 | if (!namespace_bindings_p ()) |
3956 | /* We do not need to create a binding for this name; | |
3957 | push_overloaded_decl will have already done so if | |
3958 | necessary. */ | |
3959 | need_new_binding = 0; | |
8926095f | 3960 | } |
6eb3bb27 | 3961 | else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x)) |
cfe507be MM |
3962 | { |
3963 | t = push_overloaded_decl (x, PUSH_GLOBAL); | |
3964 | if (t == x) | |
3965 | add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t))); | |
3966 | return t; | |
3967 | } | |
8d08fdba | 3968 | |
a1774733 BK |
3969 | /* If declaring a type as a typedef, copy the type (unless we're |
3970 | at line 0), and install this TYPE_DECL as the new type's typedef | |
a1c65f9f | 3971 | name. See the extensive comment in ../c-decl.c (pushdecl). */ |
8d08fdba MS |
3972 | if (TREE_CODE (x) == TYPE_DECL) |
3973 | { | |
3974 | tree type = TREE_TYPE (x); | |
655dc6ee | 3975 | if (DECL_SOURCE_LINE (x) == 0) |
a1774733 BK |
3976 | { |
3977 | if (TYPE_NAME (type) == 0) | |
3978 | TYPE_NAME (type) = x; | |
3979 | } | |
1c80fb65 MM |
3980 | else if (type != error_mark_node && TYPE_NAME (type) != x |
3981 | /* We don't want to copy the type when all we're | |
3982 | doing is making a TYPE_DECL for the purposes of | |
3983 | inlining. */ | |
68642fb6 | 3984 | && (!TYPE_NAME (type) |
1c80fb65 | 3985 | || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x))) |
a1774733 BK |
3986 | { |
3987 | DECL_ORIGINAL_TYPE (x) = type; | |
3988 | type = build_type_copy (type); | |
3989 | TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x)); | |
3990 | TYPE_NAME (type) = x; | |
3991 | TREE_TYPE (x) = type; | |
3992 | } | |
8d08fdba | 3993 | |
8d08fdba MS |
3994 | if (type != error_mark_node |
3995 | && TYPE_NAME (type) | |
3996 | && TYPE_IDENTIFIER (type)) | |
68642fb6 | 3997 | set_identifier_type_value_with_scope (DECL_NAME (x), type, |
f181d4ae MM |
3998 | current_binding_level); |
3999 | ||
8d08fdba MS |
4000 | } |
4001 | ||
4002 | /* Multiple external decls of the same identifier ought to match. | |
4003 | ||
4004 | We get warnings about inline functions where they are defined. | |
39211cd5 | 4005 | We get warnings about other functions from push_overloaded_decl. |
68642fb6 | 4006 | |
8d08fdba | 4007 | Avoid duplicate warnings where they are used. */ |
39211cd5 | 4008 | if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL) |
8d08fdba MS |
4009 | { |
4010 | tree decl; | |
4011 | ||
f49fad00 JM |
4012 | decl = IDENTIFIER_NAMESPACE_VALUE (name); |
4013 | if (decl && TREE_CODE (decl) == OVERLOAD) | |
4014 | decl = OVL_FUNCTION (decl); | |
8d08fdba | 4015 | |
f49fad00 JM |
4016 | if (decl && decl != error_mark_node |
4017 | && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl)) | |
8d08fdba MS |
4018 | /* If different sort of thing, we already gave an error. */ |
4019 | && TREE_CODE (decl) == TREE_CODE (x) | |
3bfdc719 | 4020 | && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl))) |
8d08fdba | 4021 | { |
33bd39a2 | 4022 | pedwarn ("type mismatch with previous external decl", x); |
8251199e | 4023 | cp_pedwarn_at ("previous external decl of `%#D'", decl); |
8d08fdba MS |
4024 | } |
4025 | } | |
4026 | ||
8d08fdba MS |
4027 | /* This name is new in its binding level. |
4028 | Install the new declaration and return it. */ | |
2c73f9f5 | 4029 | if (namespace_bindings_p ()) |
8d08fdba MS |
4030 | { |
4031 | /* Install a global value. */ | |
4032 | ||
8d08fdba MS |
4033 | /* If the first global decl has external linkage, |
4034 | warn if we later see static one. */ | |
31928556 | 4035 | if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x)) |
8d08fdba | 4036 | TREE_PUBLIC (name) = 1; |
5362b086 | 4037 | |
92643fea MM |
4038 | /* Bind the name for the entity. */ |
4039 | if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x) | |
4040 | && t != NULL_TREE) | |
4041 | && (TREE_CODE (x) == TYPE_DECL | |
4042 | || TREE_CODE (x) == VAR_DECL | |
4043 | || TREE_CODE (x) == NAMESPACE_DECL | |
4044 | || TREE_CODE (x) == CONST_DECL | |
4045 | || TREE_CODE (x) == TEMPLATE_DECL)) | |
4046 | SET_IDENTIFIER_NAMESPACE_VALUE (name, x); | |
8d08fdba MS |
4047 | |
4048 | /* Don't forget if the function was used via an implicit decl. */ | |
4049 | if (IDENTIFIER_IMPLICIT_DECL (name) | |
4050 | && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name))) | |
4051 | TREE_USED (x) = 1; | |
4052 | ||
4053 | /* Don't forget if its address was taken in that way. */ | |
4054 | if (IDENTIFIER_IMPLICIT_DECL (name) | |
4055 | && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name))) | |
4056 | TREE_ADDRESSABLE (x) = 1; | |
4057 | ||
4058 | /* Warn about mismatches against previous implicit decl. */ | |
4059 | if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE | |
4060 | /* If this real decl matches the implicit, don't complain. */ | |
4061 | && ! (TREE_CODE (x) == FUNCTION_DECL | |
4062 | && TREE_TYPE (TREE_TYPE (x)) == integer_type_node)) | |
33bd39a2 | 4063 | warning |
8251199e | 4064 | ("`%D' was previously implicitly declared to return `int'", x); |
8d08fdba MS |
4065 | |
4066 | /* If new decl is `static' and an `extern' was seen previously, | |
4067 | warn about it. */ | |
a0a33927 MS |
4068 | if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t)) |
4069 | warn_extern_redeclared_static (x, t); | |
8d08fdba MS |
4070 | } |
4071 | else | |
4072 | { | |
4073 | /* Here to install a non-global value. */ | |
f181d4ae | 4074 | tree oldlocal = IDENTIFIER_VALUE (name); |
31928556 | 4075 | tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name); |
8d08fdba | 4076 | |
f181d4ae MM |
4077 | if (need_new_binding) |
4078 | { | |
0034cf72 | 4079 | push_local_binding (name, x, 0); |
f181d4ae MM |
4080 | /* Because push_local_binding will hook X on to the |
4081 | current_binding_level's name list, we don't want to | |
4082 | do that again below. */ | |
4083 | need_new_binding = 0; | |
4084 | } | |
8d08fdba MS |
4085 | |
4086 | /* If this is a TYPE_DECL, push it into the type value slot. */ | |
4087 | if (TREE_CODE (x) == TYPE_DECL) | |
68642fb6 | 4088 | set_identifier_type_value_with_scope (name, TREE_TYPE (x), |
f181d4ae | 4089 | current_binding_level); |
8d08fdba | 4090 | |
a9aedbc2 MS |
4091 | /* Clear out any TYPE_DECL shadowed by a namespace so that |
4092 | we won't think this is a type. The C struct hack doesn't | |
4093 | go through namespaces. */ | |
4094 | if (TREE_CODE (x) == NAMESPACE_DECL) | |
68642fb6 | 4095 | set_identifier_type_value_with_scope (name, NULL_TREE, |
f181d4ae | 4096 | current_binding_level); |
a9aedbc2 | 4097 | |
e905ac8a MS |
4098 | if (oldlocal) |
4099 | { | |
4100 | tree d = oldlocal; | |
17aec3eb | 4101 | |
74dc0d8c MS |
4102 | while (oldlocal |
4103 | && TREE_CODE (oldlocal) == VAR_DECL | |
4104 | && DECL_DEAD_FOR_LOCAL (oldlocal)) | |
17aec3eb RK |
4105 | oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal); |
4106 | ||
e905ac8a MS |
4107 | if (oldlocal == NULL_TREE) |
4108 | oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d)); | |
4109 | } | |
4110 | ||
8d08fdba MS |
4111 | /* If this is an extern function declaration, see if we |
4112 | have a global definition or declaration for the function. */ | |
4113 | if (oldlocal == NULL_TREE | |
faae18ab | 4114 | && DECL_EXTERNAL (x) |
31928556 | 4115 | && oldglobal != NULL_TREE |
8d08fdba | 4116 | && TREE_CODE (x) == FUNCTION_DECL |
31928556 | 4117 | && TREE_CODE (oldglobal) == FUNCTION_DECL) |
8d08fdba MS |
4118 | { |
4119 | /* We have one. Their types must agree. */ | |
31928556 | 4120 | if (decls_match (x, oldglobal)) |
6060a796 MS |
4121 | /* OK */; |
4122 | else | |
8d08fdba | 4123 | { |
33bd39a2 | 4124 | warning ("extern declaration of `%#D' doesn't match", x); |
8251199e | 4125 | cp_warning_at ("global declaration `%#D'", oldglobal); |
8d08fdba | 4126 | } |
8d08fdba MS |
4127 | } |
4128 | /* If we have a local external declaration, | |
4129 | and no file-scope declaration has yet been seen, | |
4130 | then if we later have a file-scope decl it must not be static. */ | |
4131 | if (oldlocal == NULL_TREE | |
31928556 | 4132 | && oldglobal == NULL_TREE |
8d08fdba MS |
4133 | && DECL_EXTERNAL (x) |
4134 | && TREE_PUBLIC (x)) | |
f181d4ae | 4135 | TREE_PUBLIC (name) = 1; |
8d08fdba | 4136 | |
8d08fdba | 4137 | /* Warn if shadowing an argument at the top level of the body. */ |
e905ac8a MS |
4138 | if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x) |
4139 | /* Inline decls shadow nothing. */ | |
4140 | && !DECL_FROM_INLINE (x) | |
8ac9ea61 | 4141 | && TREE_CODE (oldlocal) == PARM_DECL) |
8d08fdba | 4142 | { |
8ac9ea61 | 4143 | bool err = false; |
8d08fdba | 4144 | |
8ac9ea61 NB |
4145 | /* Don't complain if it's from an enclosing function. */ |
4146 | if (DECL_CONTEXT (oldlocal) == current_function_decl | |
4147 | && TREE_CODE (x) != PARM_DECL) | |
4148 | { | |
4149 | /* Go to where the parms should be and see if we find | |
4150 | them there. */ | |
e2500fed | 4151 | struct cp_binding_level *b = current_binding_level->level_chain; |
8ac9ea61 | 4152 | |
8ac9ea61 NB |
4153 | /* ARM $8.3 */ |
4154 | if (b->parm_flag == 1) | |
4155 | { | |
33bd39a2 | 4156 | error ("declaration of `%#D' shadows a parameter", |
8ac9ea61 NB |
4157 | name); |
4158 | err = true; | |
4159 | } | |
4160 | } | |
8d08fdba | 4161 | |
8ac9ea61 NB |
4162 | if (warn_shadow && !err) |
4163 | shadow_warning ("a parameter", name, oldlocal); | |
8d08fdba | 4164 | } |
e905ac8a | 4165 | |
8d08fdba | 4166 | /* Maybe warn if shadowing something else. */ |
8ac9ea61 | 4167 | else if (warn_shadow && !DECL_EXTERNAL (x) |
e905ac8a MS |
4168 | /* No shadow warnings for internally generated vars. */ |
4169 | && ! DECL_ARTIFICIAL (x) | |
4170 | /* No shadow warnings for vars made for inlining. */ | |
4171 | && ! DECL_FROM_INLINE (x)) | |
8d08fdba | 4172 | { |
8ac9ea61 | 4173 | if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE |
4ac14744 | 4174 | && current_class_ptr |
8d08fdba | 4175 | && !TREE_STATIC (name)) |
33bd39a2 | 4176 | warning ("declaration of `%s' shadows a member of `this'", |
8ac9ea61 NB |
4177 | IDENTIFIER_POINTER (name)); |
4178 | else if (oldlocal != NULL_TREE | |
4179 | && TREE_CODE (oldlocal) == VAR_DECL) | |
4180 | shadow_warning ("a previous local", name, oldlocal); | |
4181 | else if (oldglobal != NULL_TREE | |
4182 | && TREE_CODE (oldglobal) == VAR_DECL) | |
30394414 | 4183 | /* XXX shadow warnings in outer-more namespaces */ |
8ac9ea61 | 4184 | shadow_warning ("a global declaration", name, oldglobal); |
8d08fdba | 4185 | } |
e1cd6e56 | 4186 | } |
8d08fdba | 4187 | |
e1cd6e56 | 4188 | if (TREE_CODE (x) == FUNCTION_DECL) |
f30432d7 | 4189 | check_default_args (x); |
8145f082 | 4190 | |
0154eaa8 MM |
4191 | if (TREE_CODE (x) == VAR_DECL) |
4192 | maybe_register_incomplete_var (x); | |
8d08fdba MS |
4193 | } |
4194 | ||
f181d4ae | 4195 | if (need_new_binding) |
68642fb6 | 4196 | add_decl_to_level (x, |
efee38a9 MM |
4197 | DECL_NAMESPACE_SCOPE_P (x) |
4198 | ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x)) | |
4199 | : current_binding_level); | |
8d08fdba MS |
4200 | |
4201 | return x; | |
4202 | } | |
4203 | ||
5566b478 MS |
4204 | /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the |
4205 | caller to set DECL_CONTEXT properly. */ | |
8d08fdba MS |
4206 | |
4207 | static tree | |
4208 | pushdecl_with_scope (x, level) | |
4209 | tree x; | |
e2500fed | 4210 | struct cp_binding_level *level; |
8d08fdba | 4211 | { |
e2500fed | 4212 | register struct cp_binding_level *b; |
5566b478 | 4213 | tree function_decl = current_function_decl; |
8d08fdba | 4214 | |
5566b478 | 4215 | current_function_decl = NULL_TREE; |
8d019cef JM |
4216 | if (level->parm_flag == 2) |
4217 | { | |
4218 | b = class_binding_level; | |
4219 | class_binding_level = level; | |
4220 | pushdecl_class_level (x); | |
4221 | class_binding_level = b; | |
4222 | } | |
4223 | else | |
4224 | { | |
4225 | b = current_binding_level; | |
4226 | current_binding_level = level; | |
4227 | x = pushdecl (x); | |
4228 | current_binding_level = b; | |
4229 | } | |
5566b478 | 4230 | current_function_decl = function_decl; |
8d08fdba MS |
4231 | return x; |
4232 | } | |
4233 | ||
2c73f9f5 | 4234 | /* Like pushdecl, only it places X in the current namespace, |
8d08fdba | 4235 | if appropriate. */ |
e92cc029 | 4236 | |
8d08fdba | 4237 | tree |
2c73f9f5 | 4238 | pushdecl_namespace_level (x) |
8d08fdba MS |
4239 | tree x; |
4240 | { | |
e2500fed | 4241 | register struct cp_binding_level *b = current_binding_level; |
2c73f9f5 ML |
4242 | register tree t; |
4243 | ||
4244 | t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace)); | |
8d08fdba MS |
4245 | |
4246 | /* Now, the type_shadowed stack may screw us. Munge it so it does | |
4247 | what we want. */ | |
4248 | if (TREE_CODE (x) == TYPE_DECL) | |
4249 | { | |
4250 | tree name = DECL_NAME (x); | |
4251 | tree newval; | |
4252 | tree *ptr = (tree *)0; | |
4253 | for (; b != global_binding_level; b = b->level_chain) | |
4254 | { | |
4255 | tree shadowed = b->type_shadowed; | |
4256 | for (; shadowed; shadowed = TREE_CHAIN (shadowed)) | |
4257 | if (TREE_PURPOSE (shadowed) == name) | |
4258 | { | |
4259 | ptr = &TREE_VALUE (shadowed); | |
4260 | /* Can't break out of the loop here because sometimes | |
4261 | a binding level will have duplicate bindings for | |
4262 | PT names. It's gross, but I haven't time to fix it. */ | |
4263 | } | |
4264 | } | |
4265 | newval = TREE_TYPE (x); | |
4266 | if (ptr == (tree *)0) | |
4267 | { | |
4268 | /* @@ This shouldn't be needed. My test case "zstring.cc" trips | |
4269 | up here if this is changed to an assertion. --KR */ | |
4270 | SET_IDENTIFIER_TYPE_VALUE (name, newval); | |
4271 | } | |
4272 | else | |
4273 | { | |
8d08fdba MS |
4274 | *ptr = newval; |
4275 | } | |
4276 | } | |
4277 | return t; | |
4278 | } | |
4279 | ||
2c73f9f5 ML |
4280 | /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL, |
4281 | if appropriate. */ | |
4282 | ||
4283 | tree | |
4284 | pushdecl_top_level (x) | |
4285 | tree x; | |
4286 | { | |
b35d4555 | 4287 | push_to_top_level (); |
2c73f9f5 | 4288 | x = pushdecl_namespace_level (x); |
b35d4555 | 4289 | pop_from_top_level (); |
2c73f9f5 ML |
4290 | return x; |
4291 | } | |
4292 | ||
8d08fdba | 4293 | /* Make the declaration of X appear in CLASS scope. */ |
e92cc029 | 4294 | |
61a127b3 | 4295 | void |
8d08fdba MS |
4296 | pushdecl_class_level (x) |
4297 | tree x; | |
4298 | { | |
22eadedb | 4299 | tree name; |
8f032717 | 4300 | |
22eadedb | 4301 | /* Get the name of X. */ |
8f032717 | 4302 | if (TREE_CODE (x) == OVERLOAD) |
22eadedb MM |
4303 | name = DECL_NAME (get_first_fn (x)); |
4304 | else | |
4305 | name = DECL_NAME (x); | |
8d08fdba MS |
4306 | |
4307 | if (name) | |
4308 | { | |
4309 | push_class_level_binding (name, x); | |
4310 | if (TREE_CODE (x) == TYPE_DECL) | |
f181d4ae | 4311 | set_identifier_type_value (name, TREE_TYPE (x)); |
8d08fdba | 4312 | } |
6bdb8141 | 4313 | else if (ANON_AGGR_TYPE_P (TREE_TYPE (x))) |
8f032717 | 4314 | { |
22eadedb MM |
4315 | /* If X is an anonymous aggregate, all of its members are |
4316 | treated as if they were members of the class containing the | |
4317 | aggregate, for naming purposes. */ | |
8f032717 MM |
4318 | tree f; |
4319 | ||
22eadedb | 4320 | for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f)) |
8f032717 MM |
4321 | pushdecl_class_level (f); |
4322 | } | |
8d08fdba MS |
4323 | } |
4324 | ||
9188c363 MM |
4325 | /* Enter DECL into the symbol table, if that's appropriate. Returns |
4326 | DECL, or a modified version thereof. */ | |
4327 | ||
4328 | tree | |
4329 | maybe_push_decl (decl) | |
4330 | tree decl; | |
4331 | { | |
4332 | tree type = TREE_TYPE (decl); | |
4333 | ||
4334 | /* Add this decl to the current binding level, but not if it comes | |
4335 | from another scope, e.g. a static member variable. TEM may equal | |
4336 | DECL or it may be a previous decl of the same name. */ | |
07c88314 MM |
4337 | if (decl == error_mark_node |
4338 | || (TREE_CODE (decl) != PARM_DECL | |
4339 | && DECL_CONTEXT (decl) != NULL_TREE | |
4340 | /* Definitions of namespace members outside their namespace are | |
a1c65f9f | 4341 | possible. */ |
07c88314 | 4342 | && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL) |
9188c363 MM |
4343 | || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ()) |
4344 | || TREE_CODE (type) == UNKNOWN_TYPE | |
558475f0 | 4345 | /* The declaration of a template specialization does not affect |
9188c363 MM |
4346 | the functions available for overload resolution, so we do not |
4347 | call pushdecl. */ | |
4348 | || (TREE_CODE (decl) == FUNCTION_DECL | |
4349 | && DECL_TEMPLATE_SPECIALIZATION (decl))) | |
4350 | return decl; | |
4351 | else | |
4352 | return pushdecl (decl); | |
4353 | } | |
4354 | ||
8d08fdba MS |
4355 | /* Make the declaration(s) of X appear in CLASS scope |
4356 | under the name NAME. */ | |
e92cc029 | 4357 | |
8d08fdba MS |
4358 | void |
4359 | push_class_level_binding (name, x) | |
4360 | tree name; | |
4361 | tree x; | |
4362 | { | |
8f032717 | 4363 | tree binding; |
68642fb6 | 4364 | /* The class_binding_level will be NULL if x is a template |
98c1c668 JM |
4365 | parameter name in a member template. */ |
4366 | if (!class_binding_level) | |
4367 | return; | |
4368 | ||
908c4e83 MM |
4369 | /* Make sure that this new member does not have the same name |
4370 | as a template parameter. */ | |
4371 | if (TYPE_BEING_DEFINED (current_class_type)) | |
4372 | check_template_shadow (x); | |
4373 | ||
f181d4ae MM |
4374 | /* If this declaration shadows a declaration from an enclosing |
4375 | class, then we will need to restore IDENTIFIER_CLASS_VALUE when | |
4376 | we leave this class. Record the shadowed declaration here. */ | |
8f032717 | 4377 | binding = IDENTIFIER_BINDING (name); |
68642fb6 | 4378 | if (binding |
8f032717 MM |
4379 | && ((TREE_CODE (x) == OVERLOAD |
4380 | && BINDING_VALUE (binding) | |
4381 | && is_overloaded_fn (BINDING_VALUE (binding))) | |
4382 | || INHERITED_VALUE_BINDING_P (binding))) | |
4383 | { | |
4384 | tree shadow; | |
4385 | tree old_decl; | |
4386 | ||
4387 | /* If the old binding was from a base class, and was for a tag | |
4388 | name, slide it over to make room for the new binding. The | |
4389 | old binding is still visible if explicitly qualified with a | |
4390 | class-key. */ | |
4391 | if (INHERITED_VALUE_BINDING_P (binding) | |
4392 | && BINDING_VALUE (binding) | |
4393 | && TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL | |
4394 | && DECL_ARTIFICIAL (BINDING_VALUE (binding)) | |
4395 | && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x))) | |
4396 | { | |
4397 | old_decl = BINDING_TYPE (binding); | |
4398 | BINDING_TYPE (binding) = BINDING_VALUE (binding); | |
4399 | BINDING_VALUE (binding) = NULL_TREE; | |
4400 | INHERITED_VALUE_BINDING_P (binding) = 0; | |
4401 | } | |
4402 | else | |
4403 | old_decl = BINDING_VALUE (binding); | |
4404 | ||
76191fdd NS |
4405 | /* Find the previous binding of name on the class-shadowed |
4406 | list, and update it. */ | |
8f032717 MM |
4407 | for (shadow = class_binding_level->class_shadowed; |
4408 | shadow; | |
4409 | shadow = TREE_CHAIN (shadow)) | |
4410 | if (TREE_PURPOSE (shadow) == name | |
4411 | && TREE_TYPE (shadow) == old_decl) | |
4412 | { | |
4413 | BINDING_VALUE (binding) = x; | |
4414 | INHERITED_VALUE_BINDING_P (binding) = 0; | |
4415 | TREE_TYPE (shadow) = x; | |
76191fdd | 4416 | IDENTIFIER_CLASS_VALUE (name) = x; |
8f032717 MM |
4417 | return; |
4418 | } | |
4419 | } | |
f181d4ae | 4420 | |
8f032717 | 4421 | /* If we didn't replace an existing binding, put the binding on the |
76191fdd | 4422 | stack of bindings for the identifier, and update the shadowed list. */ |
8f032717 MM |
4423 | if (push_class_binding (name, x)) |
4424 | { | |
8f032717 | 4425 | class_binding_level->class_shadowed |
76191fdd | 4426 | = tree_cons (name, NULL, |
8f032717 | 4427 | class_binding_level->class_shadowed); |
8f032717 MM |
4428 | /* Record the value we are binding NAME to so that we can know |
4429 | what to pop later. */ | |
4430 | TREE_TYPE (class_binding_level->class_shadowed) = x; | |
4431 | } | |
8d08fdba MS |
4432 | } |
4433 | ||
dd4fae80 ML |
4434 | /* Insert another USING_DECL into the current binding level, returning |
4435 | this declaration. If this is a redeclaration, do nothing, and | |
4436 | return NULL_TREE if this not in namespace scope (in namespace | |
4437 | scope, a using decl might extend any previous bindings). */ | |
e92cc029 | 4438 | |
2c73f9f5 ML |
4439 | tree |
4440 | push_using_decl (scope, name) | |
4441 | tree scope; | |
4442 | tree name; | |
8d08fdba | 4443 | { |
2c73f9f5 | 4444 | tree decl; |
68642fb6 | 4445 | |
2c73f9f5 ML |
4446 | my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383); |
4447 | my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384); | |
4448 | for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl)) | |
4449 | if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name) | |
4450 | break; | |
4451 | if (decl) | |
dd4fae80 | 4452 | return namespace_bindings_p () ? decl : NULL_TREE; |
4ce3d537 | 4453 | decl = build_lang_decl (USING_DECL, name, void_type_node); |
2c73f9f5 ML |
4454 | DECL_INITIAL (decl) = scope; |
4455 | TREE_CHAIN (decl) = current_binding_level->usings; | |
4456 | current_binding_level->usings = decl; | |
4457 | return decl; | |
8d08fdba MS |
4458 | } |
4459 | ||
ea9635c7 ML |
4460 | /* Add namespace to using_directives. Return NULL_TREE if nothing was |
4461 | changed (i.e. there was already a directive), or the fresh | |
4462 | TREE_LIST otherwise. */ | |
4463 | ||
4464 | tree | |
9ed182dc | 4465 | push_using_directive (used) |
ea9635c7 | 4466 | tree used; |
ea9635c7 ML |
4467 | { |
4468 | tree ud = current_binding_level->using_directives; | |
9ed182dc | 4469 | tree iter, ancestor; |
68642fb6 | 4470 | |
a1c65f9f | 4471 | /* Check if we already have this. */ |
ea9635c7 ML |
4472 | if (purpose_member (used, ud) != NULL_TREE) |
4473 | return NULL_TREE; | |
00dc6358 | 4474 | |
a1c65f9f | 4475 | /* Recursively add all namespaces used. */ |
00dc6358 | 4476 | for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter)) |
9ed182dc | 4477 | push_using_directive (TREE_PURPOSE (iter)); |
00dc6358 | 4478 | |
9ed182dc | 4479 | ancestor = namespace_ancestor (current_decl_namespace (), used); |
00dc6358 | 4480 | ud = current_binding_level->using_directives; |
e1b3e07d | 4481 | ud = tree_cons (used, ancestor, ud); |
ea9635c7 ML |
4482 | current_binding_level->using_directives = ud; |
4483 | return ud; | |
4484 | } | |
4485 | ||
f181d4ae MM |
4486 | /* DECL is a FUNCTION_DECL for a non-member function, which may have |
4487 | other definitions already in place. We get around this by making | |
4488 | the value of the identifier point to a list of all the things that | |
4489 | want to be referenced by that name. It is then up to the users of | |
4490 | that name to decide what to do with that list. | |
8d08fdba | 4491 | |
17aec3eb RK |
4492 | DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its |
4493 | DECL_TEMPLATE_RESULT. It is dealt with the same way. | |
8d08fdba | 4494 | |
7bdbfa05 MM |
4495 | FLAGS is a bitwise-or of the following values: |
4496 | PUSH_LOCAL: Bind DECL in the current scope, rather than at | |
4497 | namespace scope. | |
4498 | PUSH_USING: DECL is being pushed as the result of a using | |
68642fb6 | 4499 | declaration. |
7bdbfa05 | 4500 | |
8d08fdba MS |
4501 | The value returned may be a previous declaration if we guessed wrong |
4502 | about what language DECL should belong to (C or C++). Otherwise, | |
4503 | it's always DECL (and never something that's not a _DECL). */ | |
e92cc029 | 4504 | |
7bdbfa05 MM |
4505 | tree |
4506 | push_overloaded_decl (decl, flags) | |
8d08fdba | 4507 | tree decl; |
7bdbfa05 | 4508 | int flags; |
8d08fdba | 4509 | { |
f181d4ae | 4510 | tree name = DECL_NAME (decl); |
700f8a87 | 4511 | tree old; |
f181d4ae | 4512 | tree new_binding; |
7bdbfa05 | 4513 | int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL)); |
700f8a87 MS |
4514 | |
4515 | if (doing_global) | |
9f33663b | 4516 | old = namespace_binding (name, DECL_CONTEXT (decl)); |
700f8a87 | 4517 | else |
f181d4ae | 4518 | old = lookup_name_current_level (name); |
8d08fdba | 4519 | |
700f8a87 | 4520 | if (old) |
8d08fdba | 4521 | { |
e1cd6e56 | 4522 | if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old)) |
8926095f | 4523 | { |
700f8a87 | 4524 | tree t = TREE_TYPE (old); |
cdf5b885 MS |
4525 | if (IS_AGGR_TYPE (t) && warn_shadow |
4526 | && (! DECL_IN_SYSTEM_HEADER (decl) | |
4527 | || ! DECL_IN_SYSTEM_HEADER (old))) | |
33bd39a2 | 4528 | warning ("`%#D' hides constructor for `%#T'", decl, t); |
5b605f68 | 4529 | old = NULL_TREE; |
8926095f | 4530 | } |
700f8a87 | 4531 | else if (is_overloaded_fn (old)) |
8d08fdba | 4532 | { |
8d08fdba | 4533 | tree tmp; |
68642fb6 | 4534 | |
2c73f9f5 | 4535 | for (tmp = old; tmp; tmp = OVL_NEXT (tmp)) |
7bdbfa05 MM |
4536 | { |
4537 | tree fn = OVL_CURRENT (tmp); | |
4538 | ||
4539 | if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp) | |
4540 | && !(flags & PUSH_USING) | |
4541 | && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)), | |
4542 | TYPE_ARG_TYPES (TREE_TYPE (decl)))) | |
33bd39a2 | 4543 | error ("`%#D' conflicts with previous using declaration `%#D'", |
7bdbfa05 | 4544 | decl, fn); |
68642fb6 | 4545 | |
7bdbfa05 MM |
4546 | if (duplicate_decls (decl, fn)) |
4547 | return fn; | |
4548 | } | |
8d08fdba | 4549 | } |
655dc6ee JM |
4550 | else if (old == error_mark_node) |
4551 | /* Ignore the undefined symbol marker. */ | |
4552 | old = NULL_TREE; | |
e1cd6e56 MS |
4553 | else |
4554 | { | |
8251199e | 4555 | cp_error_at ("previous non-function declaration `%#D'", old); |
33bd39a2 | 4556 | error ("conflicts with function declaration `%#D'", decl); |
a9aedbc2 | 4557 | return decl; |
e1cd6e56 | 4558 | } |
8d08fdba | 4559 | } |
7177d104 | 4560 | |
700f8a87 | 4561 | if (old || TREE_CODE (decl) == TEMPLATE_DECL) |
8d08fdba | 4562 | { |
2c73f9f5 | 4563 | if (old && TREE_CODE (old) != OVERLOAD) |
f181d4ae MM |
4564 | new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE)); |
4565 | else | |
4566 | new_binding = ovl_cons (decl, old); | |
347d73d7 ML |
4567 | if (flags & PUSH_USING) |
4568 | OVL_USED (new_binding) = 1; | |
8d08fdba MS |
4569 | } |
4570 | else | |
f181d4ae MM |
4571 | /* NAME is not ambiguous. */ |
4572 | new_binding = decl; | |
700f8a87 MS |
4573 | |
4574 | if (doing_global) | |
f181d4ae | 4575 | set_namespace_binding (name, current_namespace, new_binding); |
700f8a87 | 4576 | else |
f181d4ae MM |
4577 | { |
4578 | /* We only create an OVERLOAD if there was a previous binding at | |
0580c9aa ML |
4579 | this level, or if decl is a template. In the former case, we |
4580 | need to remove the old binding and replace it with the new | |
4581 | binding. We must also run through the NAMES on the binding | |
4582 | level where the name was bound to update the chain. */ | |
4583 | ||
4584 | if (TREE_CODE (new_binding) == OVERLOAD && old) | |
f181d4ae MM |
4585 | { |
4586 | tree *d; | |
68642fb6 | 4587 | |
f181d4ae MM |
4588 | for (d = &BINDING_LEVEL (IDENTIFIER_BINDING (name))->names; |
4589 | *d; | |
4590 | d = &TREE_CHAIN (*d)) | |
4591 | if (*d == old | |
4592 | || (TREE_CODE (*d) == TREE_LIST | |
4593 | && TREE_VALUE (*d) == old)) | |
4594 | { | |
d8f8dca1 MM |
4595 | if (TREE_CODE (*d) == TREE_LIST) |
4596 | /* Just replace the old binding with the new. */ | |
4597 | TREE_VALUE (*d) = new_binding; | |
4598 | else | |
4599 | /* Build a TREE_LIST to wrap the OVERLOAD. */ | |
68642fb6 | 4600 | *d = tree_cons (NULL_TREE, new_binding, |
ed5511d9 | 4601 | TREE_CHAIN (*d)); |
d8f8dca1 MM |
4602 | |
4603 | /* And update the CPLUS_BINDING node. */ | |
4604 | BINDING_VALUE (IDENTIFIER_BINDING (name)) | |
4605 | = new_binding; | |
4606 | return decl; | |
f181d4ae MM |
4607 | } |
4608 | ||
d8f8dca1 | 4609 | /* We should always find a previous binding in this case. */ |
a98facb0 | 4610 | abort (); |
f181d4ae MM |
4611 | } |
4612 | ||
4613 | /* Install the new binding. */ | |
0034cf72 | 4614 | push_local_binding (name, new_binding, flags); |
f181d4ae | 4615 | } |
700f8a87 | 4616 | |
8d08fdba MS |
4617 | return decl; |
4618 | } | |
4619 | \f | |
4620 | /* Generate an implicit declaration for identifier FUNCTIONID | |
4621 | as a function of type int (). Print a warning if appropriate. */ | |
4622 | ||
4623 | tree | |
4624 | implicitly_declare (functionid) | |
4625 | tree functionid; | |
4626 | { | |
4627 | register tree decl; | |
8d08fdba MS |
4628 | |
4629 | /* We used to reuse an old implicit decl here, | |
4630 | but this loses with inline functions because it can clobber | |
4631 | the saved decl chains. */ | |
4632 | decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type); | |
4633 | ||
4634 | DECL_EXTERNAL (decl) = 1; | |
4635 | TREE_PUBLIC (decl) = 1; | |
4636 | ||
cab1f180 | 4637 | /* ISO standard says implicit declarations are in the innermost block. |
d22c8596 | 4638 | So we record the decl in the standard fashion. */ |
8d08fdba | 4639 | pushdecl (decl); |
3e411c3f | 4640 | rest_of_decl_compilation (decl, NULL, 0, 0); |
8d08fdba MS |
4641 | |
4642 | if (warn_implicit | |
4643 | /* Only one warning per identifier. */ | |
4644 | && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE) | |
4645 | { | |
33bd39a2 | 4646 | pedwarn ("implicit declaration of function `%#D'", decl); |
8d08fdba MS |
4647 | } |
4648 | ||
4649 | SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl); | |
4650 | ||
8d08fdba MS |
4651 | return decl; |
4652 | } | |
4653 | ||
8251199e | 4654 | /* Return zero if the declaration NEWDECL is valid |
8d08fdba MS |
4655 | when the declaration OLDDECL (assumed to be for the same name) |
4656 | has already been seen. | |
4657 | Otherwise return an error message format string with a %s | |
4658 | where the identifier should go. */ | |
4659 | ||
d8e178a0 | 4660 | static const char * |
8d08fdba MS |
4661 | redeclaration_error_message (newdecl, olddecl) |
4662 | tree newdecl, olddecl; | |
4663 | { | |
4664 | if (TREE_CODE (newdecl) == TYPE_DECL) | |
4665 | { | |
4666 | /* Because C++ can put things into name space for free, | |
4667 | constructs like "typedef struct foo { ... } foo" | |
4668 | would look like an erroneous redeclaration. */ | |
3bfdc719 | 4669 | if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl))) |
8251199e | 4670 | return 0; |
8d08fdba | 4671 | else |
8251199e | 4672 | return "redefinition of `%#D'"; |
8d08fdba MS |
4673 | } |
4674 | else if (TREE_CODE (newdecl) == FUNCTION_DECL) | |
4675 | { | |
4676 | /* If this is a pure function, its olddecl will actually be | |
4677 | the original initialization to `0' (which we force to call | |
4678 | abort()). Don't complain about redefinition in this case. */ | |
fee7654e | 4679 | if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)) |
8251199e | 4680 | return 0; |
8d08fdba | 4681 | |
2c73f9f5 | 4682 | /* If both functions come from different namespaces, this is not |
a1c65f9f | 4683 | a redeclaration - this is a conflict with a used function. */ |
6eb3bb27 | 4684 | if (DECL_NAMESPACE_SCOPE_P (olddecl) |
2c73f9f5 | 4685 | && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)) |
8251199e | 4686 | return "`%D' conflicts with used function"; |
2c73f9f5 | 4687 | |
db5ae43f MS |
4688 | /* We'll complain about linkage mismatches in |
4689 | warn_extern_redeclared_static. */ | |
4690 | ||
2c73f9f5 | 4691 | /* Defining the same name twice is no good. */ |
8d08fdba | 4692 | if (DECL_INITIAL (olddecl) != NULL_TREE |
faae18ab | 4693 | && DECL_INITIAL (newdecl) != NULL_TREE) |
8d08fdba MS |
4694 | { |
4695 | if (DECL_NAME (olddecl) == NULL_TREE) | |
8251199e | 4696 | return "`%#D' not declared in class"; |
8d08fdba | 4697 | else |
8251199e | 4698 | return "redefinition of `%#D'"; |
8d08fdba | 4699 | } |
8251199e | 4700 | return 0; |
8d08fdba | 4701 | } |
51c184be MS |
4702 | else if (TREE_CODE (newdecl) == TEMPLATE_DECL) |
4703 | { | |
ec255269 | 4704 | if ((TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL |
5362b086 | 4705 | && (DECL_TEMPLATE_RESULT (newdecl) |
f4381d62 | 4706 | != DECL_TEMPLATE_RESULT (olddecl)) |
ec255269 MS |
4707 | && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl)) |
4708 | && DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl))) | |
4709 | || (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL | |
d0f062fb NS |
4710 | && COMPLETE_TYPE_P (TREE_TYPE (newdecl)) |
4711 | && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))) | |
8251199e JM |
4712 | return "redefinition of `%#D'"; |
4713 | return 0; | |
51c184be | 4714 | } |
1f51a992 | 4715 | else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl)) |
8d08fdba MS |
4716 | { |
4717 | /* Objects declared at top level: */ | |
4718 | /* If at least one is a reference, it's ok. */ | |
4719 | if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl)) | |
8251199e | 4720 | return 0; |
8926095f | 4721 | /* Reject two definitions. */ |
8251199e | 4722 | return "redefinition of `%#D'"; |
8d08fdba MS |
4723 | } |
4724 | else | |
4725 | { | |
4726 | /* Objects declared with block scope: */ | |
4727 | /* Reject two definitions, and reject a definition | |
4728 | together with an external reference. */ | |
4729 | if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl))) | |
8251199e JM |
4730 | return "redeclaration of `%#D'"; |
4731 | return 0; | |
8d08fdba MS |
4732 | } |
4733 | } | |
4734 | \f | |
acef433b | 4735 | /* Create a new label, named ID. */ |
8d08fdba | 4736 | |
acef433b MM |
4737 | static tree |
4738 | make_label_decl (id, local_p) | |
8d08fdba | 4739 | tree id; |
acef433b | 4740 | int local_p; |
8d08fdba | 4741 | { |
acef433b | 4742 | tree decl; |
8d08fdba | 4743 | |
acef433b | 4744 | decl = build_decl (LABEL_DECL, id, void_type_node); |
b35d4555 | 4745 | if (expanding_p) |
acef433b MM |
4746 | /* Make sure every label has an rtx. */ |
4747 | label_rtx (decl); | |
4748 | ||
4749 | DECL_CONTEXT (decl) = current_function_decl; | |
4750 | DECL_MODE (decl) = VOIDmode; | |
4751 | C_DECLARED_LABEL_FLAG (decl) = local_p; | |
8d08fdba | 4752 | |
acef433b MM |
4753 | /* Say where one reference is to the label, for the sake of the |
4754 | error if it is not defined. */ | |
4755 | DECL_SOURCE_LINE (decl) = lineno; | |
4756 | DECL_SOURCE_FILE (decl) = input_filename; | |
4757 | ||
4758 | /* Record the fact that this identifier is bound to this label. */ | |
4759 | SET_IDENTIFIER_LABEL_VALUE (id, decl); | |
4760 | ||
6625cdb5 JM |
4761 | return decl; |
4762 | } | |
4763 | ||
4764 | /* Record this label on the list of used labels so that we can check | |
4765 | at the end of the function to see whether or not the label was | |
4766 | actually defined, and so we can check when the label is defined whether | |
4767 | this use is valid. */ | |
4768 | ||
4769 | static void | |
4770 | use_label (decl) | |
4771 | tree decl; | |
4772 | { | |
4773 | if (named_label_uses == NULL | |
4774 | || named_label_uses->names_in_scope != current_binding_level->names | |
4775 | || named_label_uses->label_decl != decl) | |
4776 | { | |
4777 | struct named_label_use_list *new_ent; | |
4778 | new_ent = ((struct named_label_use_list *) | |
4779 | ggc_alloc (sizeof (struct named_label_use_list))); | |
e349ee73 MS |
4780 | new_ent->label_decl = decl; |
4781 | new_ent->names_in_scope = current_binding_level->names; | |
4782 | new_ent->binding_level = current_binding_level; | |
4783 | new_ent->lineno_o_goto = lineno; | |
4784 | new_ent->filename_o_goto = input_filename; | |
4785 | new_ent->next = named_label_uses; | |
4786 | named_label_uses = new_ent; | |
8d08fdba | 4787 | } |
acef433b | 4788 | } |
8d08fdba | 4789 | |
acef433b MM |
4790 | /* Look for a label named ID in the current function. If one cannot |
4791 | be found, create one. (We keep track of used, but undefined, | |
4792 | labels, and complain about them at the end of a function.) */ | |
8d08fdba | 4793 | |
68642fb6 | 4794 | tree |
acef433b MM |
4795 | lookup_label (id) |
4796 | tree id; | |
4797 | { | |
4798 | tree decl; | |
6625cdb5 | 4799 | struct named_label_list *ent; |
8d08fdba | 4800 | |
acef433b MM |
4801 | /* You can't use labels at global scope. */ |
4802 | if (current_function_decl == NULL_TREE) | |
4803 | { | |
4804 | error ("label `%s' referenced outside of any function", | |
4805 | IDENTIFIER_POINTER (id)); | |
4806 | return NULL_TREE; | |
4807 | } | |
68642fb6 | 4808 | |
acef433b MM |
4809 | /* See if we've already got this label. */ |
4810 | decl = IDENTIFIER_LABEL_VALUE (id); | |
4811 | if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl) | |
4812 | return decl; | |
8d08fdba | 4813 | |
acef433b MM |
4814 | /* Record this label on the list of labels used in this function. |
4815 | We do this before calling make_label_decl so that we get the | |
4816 | IDENTIFIER_LABEL_VALUE before the new label is declared. */ | |
6625cdb5 | 4817 | ent = ((struct named_label_list *) |
f8a83ee3 | 4818 | ggc_alloc_cleared (sizeof (struct named_label_list))); |
6625cdb5 JM |
4819 | ent->old_value = IDENTIFIER_LABEL_VALUE (id); |
4820 | ent->next = named_labels; | |
4821 | named_labels = ent; | |
4822 | ||
acef433b MM |
4823 | /* We need a new label. */ |
4824 | decl = make_label_decl (id, /*local_p=*/0); | |
6625cdb5 | 4825 | |
acef433b | 4826 | /* Now fill in the information we didn't have before. */ |
6625cdb5 | 4827 | ent->label_decl = decl; |
8d08fdba MS |
4828 | |
4829 | return decl; | |
4830 | } | |
4831 | ||
acef433b | 4832 | /* Declare a local label named ID. */ |
8d08fdba MS |
4833 | |
4834 | tree | |
acef433b MM |
4835 | declare_local_label (id) |
4836 | tree id; | |
8d08fdba | 4837 | { |
acef433b | 4838 | tree decl; |
8d08fdba | 4839 | |
acef433b MM |
4840 | /* Add a new entry to the SHADOWED_LABELS list so that when we leave |
4841 | this scope we can restore the old value of | |
4842 | IDENTIFIER_TYPE_VALUE. */ | |
68642fb6 | 4843 | current_binding_level->shadowed_labels |
acef433b MM |
4844 | = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE, |
4845 | current_binding_level->shadowed_labels); | |
4846 | /* Look for the label. */ | |
4847 | decl = make_label_decl (id, /*local_p=*/1); | |
4848 | /* Now fill in the information we didn't have before. */ | |
4849 | TREE_VALUE (current_binding_level->shadowed_labels) = decl; | |
68642fb6 | 4850 | |
acef433b | 4851 | return decl; |
8d08fdba MS |
4852 | } |
4853 | ||
6625cdb5 JM |
4854 | /* Returns nonzero if it is ill-formed to jump past the declaration of |
4855 | DECL. Returns 2 if it's also a real problem. */ | |
4856 | ||
4857 | static int | |
4858 | decl_jump_unsafe (decl) | |
4859 | tree decl; | |
4860 | { | |
4861 | if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl)) | |
4862 | return 0; | |
4863 | ||
4864 | if (DECL_INITIAL (decl) == NULL_TREE | |
4865 | && pod_type_p (TREE_TYPE (decl))) | |
4866 | return 0; | |
4867 | ||
4868 | /* This is really only important if we're crossing an initialization. | |
4869 | The POD stuff is just pedantry; why should it matter if the class | |
4870 | contains a field of pointer to member type? */ | |
4871 | if (DECL_INITIAL (decl) | |
4872 | || (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))) | |
4873 | return 2; | |
4874 | return 1; | |
4875 | } | |
4876 | ||
4877 | /* Check that a single previously seen jump to a newly defined label | |
4878 | is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for | |
4879 | the jump context; NAMES are the names in scope in LEVEL at the jump | |
4880 | context; FILE and LINE are the source position of the jump or 0. */ | |
4881 | ||
4882 | static void | |
4883 | check_previous_goto_1 (decl, level, names, file, line) | |
4884 | tree decl; | |
e2500fed | 4885 | struct cp_binding_level *level; |
6625cdb5 JM |
4886 | tree names; |
4887 | const char *file; | |
4888 | int line; | |
4889 | { | |
4890 | int identified = 0; | |
4891 | int saw_eh = 0; | |
e2500fed | 4892 | struct cp_binding_level *b = current_binding_level; |
6625cdb5 JM |
4893 | for (; b; b = b->level_chain) |
4894 | { | |
4895 | tree new_decls = b->names; | |
4896 | tree old_decls = (b == level ? names : NULL_TREE); | |
4897 | for (; new_decls != old_decls; | |
4898 | new_decls = TREE_CHAIN (new_decls)) | |
4899 | { | |
4900 | int problem = decl_jump_unsafe (new_decls); | |
4901 | if (! problem) | |
4902 | continue; | |
4903 | ||
4904 | if (! identified) | |
4905 | { | |
4906 | if (decl) | |
33bd39a2 | 4907 | pedwarn ("jump to label `%D'", decl); |
6625cdb5 JM |
4908 | else |
4909 | pedwarn ("jump to case label"); | |
4910 | ||
4911 | if (file) | |
4912 | pedwarn_with_file_and_line (file, line, " from here"); | |
4913 | identified = 1; | |
4914 | } | |
4915 | ||
826840d9 | 4916 | if (problem > 1) |
6625cdb5 JM |
4917 | cp_error_at (" crosses initialization of `%#D'", |
4918 | new_decls); | |
4919 | else | |
4920 | cp_pedwarn_at (" enters scope of non-POD `%#D'", | |
4921 | new_decls); | |
4922 | } | |
4923 | ||
4924 | if (b == level) | |
4925 | break; | |
826840d9 | 4926 | if ((b->is_try_scope || b->is_catch_scope) && ! saw_eh) |
6625cdb5 JM |
4927 | { |
4928 | if (! identified) | |
4929 | { | |
4930 | if (decl) | |
33bd39a2 | 4931 | pedwarn ("jump to label `%D'", decl); |
6625cdb5 JM |
4932 | else |
4933 | pedwarn ("jump to case label"); | |
4934 | ||
4935 | if (file) | |
4936 | pedwarn_with_file_and_line (file, line, " from here"); | |
4937 | identified = 1; | |
4938 | } | |
826840d9 RH |
4939 | if (b->is_try_scope) |
4940 | error (" enters try block"); | |
4941 | else | |
4942 | error (" enters catch block"); | |
6625cdb5 JM |
4943 | saw_eh = 1; |
4944 | } | |
4945 | } | |
4946 | } | |
4947 | ||
4948 | static void | |
4949 | check_previous_goto (use) | |
4950 | struct named_label_use_list *use; | |
4951 | { | |
4952 | check_previous_goto_1 (use->label_decl, use->binding_level, | |
4953 | use->names_in_scope, use->filename_o_goto, | |
4954 | use->lineno_o_goto); | |
4955 | } | |
4956 | ||
4957 | static void | |
4958 | check_switch_goto (level) | |
e2500fed | 4959 | struct cp_binding_level *level; |
6625cdb5 JM |
4960 | { |
4961 | check_previous_goto_1 (NULL_TREE, level, level->names, NULL, 0); | |
4962 | } | |
4963 | ||
4964 | /* Check that any previously seen jumps to a newly defined label DECL | |
4965 | are OK. Called by define_label. */ | |
4966 | ||
4967 | static void | |
4968 | check_previous_gotos (decl) | |
4969 | tree decl; | |
4970 | { | |
4971 | struct named_label_use_list **usep; | |
4972 | ||
4973 | if (! TREE_USED (decl)) | |
4974 | return; | |
4975 | ||
4976 | for (usep = &named_label_uses; *usep; ) | |
4977 | { | |
4978 | struct named_label_use_list *use = *usep; | |
4979 | if (use->label_decl == decl) | |
4980 | { | |
4981 | check_previous_goto (use); | |
4982 | *usep = use->next; | |
4983 | } | |
4984 | else | |
4985 | usep = &(use->next); | |
4986 | } | |
4987 | } | |
4988 | ||
4989 | /* Check that a new jump to a label DECL is OK. Called by | |
4990 | finish_goto_stmt. */ | |
4991 | ||
4992 | void | |
4993 | check_goto (decl) | |
4994 | tree decl; | |
4995 | { | |
4996 | int identified = 0; | |
4997 | tree bad; | |
4998 | struct named_label_list *lab; | |
4999 | ||
e3cd9945 APB |
5000 | /* We can't know where a computed goto is jumping. So we assume |
5001 | that it's OK. */ | |
5002 | if (! DECL_P (decl)) | |
5003 | return; | |
5004 | ||
6625cdb5 JM |
5005 | /* If the label hasn't been defined yet, defer checking. */ |
5006 | if (! DECL_INITIAL (decl)) | |
5007 | { | |
5008 | use_label (decl); | |
5009 | return; | |
5010 | } | |
5011 | ||
5012 | for (lab = named_labels; lab; lab = lab->next) | |
5013 | if (decl == lab->label_decl) | |
5014 | break; | |
5015 | ||
5016 | /* If the label is not on named_labels it's a gcc local label, so | |
5017 | it must be in an outer scope, so jumping to it is always OK. */ | |
5018 | if (lab == 0) | |
5019 | return; | |
5020 | ||
826840d9 RH |
5021 | if ((lab->in_try_scope || lab->in_catch_scope || lab->bad_decls) |
5022 | && !identified) | |
6625cdb5 JM |
5023 | { |
5024 | cp_pedwarn_at ("jump to label `%D'", decl); | |
5025 | pedwarn (" from here"); | |
5026 | identified = 1; | |
5027 | } | |
5028 | ||
5029 | for (bad = lab->bad_decls; bad; bad = TREE_CHAIN (bad)) | |
5030 | { | |
5031 | tree b = TREE_VALUE (bad); | |
5032 | int u = decl_jump_unsafe (b); | |
5033 | ||
5034 | if (u > 1 && DECL_ARTIFICIAL (b)) | |
5035 | /* Can't skip init of __exception_info. */ | |
5036 | cp_error_at (" enters catch block", b); | |
5037 | else if (u > 1) | |
5038 | cp_error_at (" skips initialization of `%#D'", b); | |
5039 | else | |
5040 | cp_pedwarn_at (" enters scope of non-POD `%#D'", b); | |
5041 | } | |
5042 | ||
826840d9 | 5043 | if (lab->in_try_scope) |
6625cdb5 | 5044 | error (" enters try block"); |
826840d9 RH |
5045 | else if (lab->in_catch_scope) |
5046 | error (" enters catch block"); | |
6625cdb5 JM |
5047 | } |
5048 | ||
8d08fdba MS |
5049 | /* Define a label, specifying the location in the source file. |
5050 | Return the LABEL_DECL node for the label, if the definition is valid. | |
5051 | Otherwise return 0. */ | |
5052 | ||
5053 | tree | |
5054 | define_label (filename, line, name) | |
3b304f5b | 5055 | const char *filename; |
8d08fdba MS |
5056 | int line; |
5057 | tree name; | |
5058 | { | |
f01b0acb | 5059 | tree decl = lookup_label (name); |
6625cdb5 | 5060 | struct named_label_list *ent; |
e2500fed | 5061 | register struct cp_binding_level *p; |
6625cdb5 JM |
5062 | |
5063 | for (ent = named_labels; ent; ent = ent->next) | |
5064 | if (ent->label_decl == decl) | |
5065 | break; | |
8d08fdba | 5066 | |
3dc9aec6 | 5067 | /* After labels, make any new cleanups in the function go into their |
8d08fdba | 5068 | own new (temporary) binding contour. */ |
3dc9aec6 WC |
5069 | for (p = current_binding_level; !(p->parm_flag); p = p->level_chain) |
5070 | p->more_cleanups_ok = 0; | |
8d08fdba | 5071 | |
e1cd6e56 | 5072 | if (name == get_identifier ("wchar_t")) |
33bd39a2 | 5073 | pedwarn ("label named wchar_t"); |
e1cd6e56 | 5074 | |
8d08fdba MS |
5075 | if (DECL_INITIAL (decl) != NULL_TREE) |
5076 | { | |
33bd39a2 | 5077 | error ("duplicate label `%D'", decl); |
8d08fdba MS |
5078 | return 0; |
5079 | } | |
5080 | else | |
5081 | { | |
8d08fdba MS |
5082 | /* Mark label as having been defined. */ |
5083 | DECL_INITIAL (decl) = error_mark_node; | |
5084 | /* Say where in the source. */ | |
5085 | DECL_SOURCE_FILE (decl) = filename; | |
5086 | DECL_SOURCE_LINE (decl) = line; | |
6625cdb5 JM |
5087 | if (ent) |
5088 | { | |
5089 | ent->names_in_scope = current_binding_level->names; | |
5090 | ent->binding_level = current_binding_level; | |
5091 | } | |
5092 | check_previous_gotos (decl); | |
8d08fdba MS |
5093 | return decl; |
5094 | } | |
5095 | } | |
5096 | ||
a5894242 MS |
5097 | struct cp_switch |
5098 | { | |
e2500fed | 5099 | struct cp_binding_level *level; |
a5894242 | 5100 | struct cp_switch *next; |
56cb9733 MM |
5101 | /* The SWITCH_STMT being built. */ |
5102 | tree switch_stmt; | |
5103 | /* A splay-tree mapping the low element of a case range to the high | |
5104 | element, or NULL_TREE if there is no high element. Used to | |
5105 | determine whether or not a new case label duplicates an old case | |
5106 | label. We need a tree, rather than simply a hash table, because | |
5107 | of the GNU case range extension. */ | |
5108 | splay_tree cases; | |
a5894242 MS |
5109 | }; |
5110 | ||
56cb9733 MM |
5111 | /* A stack of the currently active switch statements. The innermost |
5112 | switch statement is on the top of the stack. There is no need to | |
5113 | mark the stack for garbage collection because it is only active | |
5114 | during the processing of the body of a function, and we never | |
5115 | collect at that point. */ | |
5362b086 | 5116 | |
a5894242 MS |
5117 | static struct cp_switch *switch_stack; |
5118 | ||
56cb9733 MM |
5119 | /* Called right after a switch-statement condition is parsed. |
5120 | SWITCH_STMT is the switch statement being parsed. */ | |
5121 | ||
a5894242 | 5122 | void |
56cb9733 MM |
5123 | push_switch (switch_stmt) |
5124 | tree switch_stmt; | |
a5894242 MS |
5125 | { |
5126 | struct cp_switch *p | |
bedda2da | 5127 | = (struct cp_switch *) xmalloc (sizeof (struct cp_switch)); |
a5894242 MS |
5128 | p->level = current_binding_level; |
5129 | p->next = switch_stack; | |
56cb9733 MM |
5130 | p->switch_stmt = switch_stmt; |
5131 | p->cases = splay_tree_new (case_compare, NULL, NULL); | |
a5894242 MS |
5132 | switch_stack = p; |
5133 | } | |
5134 | ||
5135 | void | |
5136 | pop_switch () | |
5137 | { | |
bedda2da | 5138 | struct cp_switch *cs; |
5362b086 | 5139 | |
bedda2da | 5140 | cs = switch_stack; |
56cb9733 | 5141 | splay_tree_delete (cs->cases); |
a5894242 | 5142 | switch_stack = switch_stack->next; |
bedda2da | 5143 | free (cs); |
a5894242 MS |
5144 | } |
5145 | ||
b0a1da19 JM |
5146 | /* Note that we've seen a definition of a case label, and complain if this |
5147 | is a bad place for one. */ | |
e92cc029 | 5148 | |
3e4d04a1 | 5149 | tree |
56cb9733 MM |
5150 | finish_case_label (low_value, high_value) |
5151 | tree low_value; | |
5152 | tree high_value; | |
8d08fdba | 5153 | { |
3e4d04a1 | 5154 | tree cond, r; |
e2500fed | 5155 | register struct cp_binding_level *p; |
a5894242 | 5156 | |
b0a1da19 | 5157 | if (! switch_stack) |
56cb9733 MM |
5158 | { |
5159 | if (high_value) | |
5160 | error ("case label not within a switch statement"); | |
5161 | else if (low_value) | |
33bd39a2 | 5162 | error ("case label `%E' not within a switch statement", |
56cb9733 MM |
5163 | low_value); |
5164 | else | |
5165 | error ("`default' label not within a switch statement"); | |
3e4d04a1 | 5166 | return NULL_TREE; |
56cb9733 MM |
5167 | } |
5168 | ||
56cb9733 MM |
5169 | if (processing_template_decl) |
5170 | { | |
8f17b5c5 MM |
5171 | tree label; |
5172 | ||
56cb9733 MM |
5173 | /* For templates, just add the case label; we'll do semantic |
5174 | analysis at instantiation-time. */ | |
8f17b5c5 | 5175 | label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE); |
3e4d04a1 | 5176 | return add_stmt (build_case_label (low_value, high_value, label)); |
56cb9733 MM |
5177 | } |
5178 | ||
5179 | /* Find the condition on which this switch statement depends. */ | |
5180 | cond = SWITCH_COND (switch_stack->switch_stmt); | |
5181 | if (cond && TREE_CODE (cond) == TREE_LIST) | |
5182 | cond = TREE_VALUE (cond); | |
56cb9733 | 5183 | |
3e4d04a1 RH |
5184 | r = c_add_case_label (switch_stack->cases, cond, low_value, high_value); |
5185 | if (r == error_mark_node) | |
5186 | r = NULL_TREE; | |
8d08fdba | 5187 | |
6625cdb5 | 5188 | check_switch_goto (switch_stack->level); |
a5894242 | 5189 | |
3dc9aec6 | 5190 | /* After labels, make any new cleanups in the function go into their |
8d08fdba | 5191 | own new (temporary) binding contour. */ |
3dc9aec6 WC |
5192 | for (p = current_binding_level; !(p->parm_flag); p = p->level_chain) |
5193 | p->more_cleanups_ok = 0; | |
3e4d04a1 RH |
5194 | |
5195 | return r; | |
8d08fdba MS |
5196 | } |
5197 | \f | |
5198 | /* Return the list of declarations of the current level. | |
5199 | Note that this list is in reverse order unless/until | |
5200 | you nreverse it; and when you do nreverse it, you must | |
5201 | store the result back using `storedecls' or you will lose. */ | |
5202 | ||
5203 | tree | |
5204 | getdecls () | |
5205 | { | |
5206 | return current_binding_level->names; | |
5207 | } | |
5208 | ||
5209 | /* Return the list of type-tags (for structs, etc) of the current level. */ | |
5210 | ||
5211 | tree | |
5212 | gettags () | |
5213 | { | |
5214 | return current_binding_level->tags; | |
5215 | } | |
5216 | ||
5217 | /* Store the list of declarations of the current level. | |
5218 | This is done for the parameter declarations of a function being defined, | |
5219 | after they are modified in the light of any missing parameters. */ | |
5220 | ||
5221 | static void | |
5222 | storedecls (decls) | |
5223 | tree decls; | |
5224 | { | |
5225 | current_binding_level->names = decls; | |
5226 | } | |
5227 | ||
5228 | /* Similarly, store the list of tags of the current level. */ | |
5229 | ||
280f9385 | 5230 | void |
8d08fdba MS |
5231 | storetags (tags) |
5232 | tree tags; | |
5233 | { | |
5234 | current_binding_level->tags = tags; | |
5235 | } | |
5236 | \f | |
4782bd5b RS |
5237 | /* Return the type that should be used when TYPE's name is preceded |
5238 | by a tag such as 'struct' or 'union', or null if the name cannot | |
5239 | be used in this way. | |
5240 | ||
5241 | For example, when processing the third line of: | |
5242 | ||
5243 | struct A; | |
5244 | typedef struct A A; | |
5245 | struct A; | |
5246 | ||
5247 | lookup of A will find the typedef. Given A's typedef, this function | |
5248 | will return the type associated with "struct A". For the tag to be | |
5249 | anything other than TYPE, TYPE must be a typedef whose original type | |
5250 | has the same name and context as TYPE itself. | |
5251 | ||
5252 | It is not valid for a typedef of an anonymous type to be used with | |
5253 | an explicit tag: | |
5254 | ||
5255 | typedef struct { ... } B; | |
5256 | struct B; | |
5257 | ||
5258 | Return null for this case. */ | |
5259 | ||
5260 | static tree | |
5261 | follow_tag_typedef (type) | |
5262 | tree type; | |
5263 | { | |
5264 | tree original; | |
5265 | ||
5266 | original = original_type (type); | |
3a73bffb JJ |
5267 | if (! TYPE_NAME (original)) |
5268 | return NULL_TREE; | |
4782bd5b RS |
5269 | if (TYPE_IDENTIFIER (original) == TYPE_IDENTIFIER (type) |
5270 | && (CP_DECL_CONTEXT (TYPE_NAME (original)) | |
5271 | == CP_DECL_CONTEXT (TYPE_NAME (type))) | |
5272 | && !(CLASS_TYPE_P (original) && TYPE_WAS_ANONYMOUS (original))) | |
5273 | return original; | |
5274 | else | |
5275 | return NULL_TREE; | |
5276 | } | |
5277 | ||
8d08fdba MS |
5278 | /* Given NAME, an IDENTIFIER_NODE, |
5279 | return the structure (or union or enum) definition for that name. | |
5280 | Searches binding levels from BINDING_LEVEL up to the global level. | |
5281 | If THISLEVEL_ONLY is nonzero, searches only the specified context | |
5282 | (but skips any tag-transparent contexts to find one that is | |
5283 | meaningful for tags). | |
5284 | FORM says which kind of type the caller wants; | |
5285 | it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE. | |
5286 | If the wrong kind of type is found, and it's not a template, an error is | |
5287 | reported. */ | |
5288 | ||
5289 | static tree | |
5290 | lookup_tag (form, name, binding_level, thislevel_only) | |
5291 | enum tree_code form; | |
8d08fdba | 5292 | tree name; |
e2500fed | 5293 | struct cp_binding_level *binding_level; |
8d08fdba MS |
5294 | int thislevel_only; |
5295 | { | |
e2500fed | 5296 | register struct cp_binding_level *level; |
838dfd8a | 5297 | /* Nonzero if, we should look past a template parameter level, even |
74b846e0 MM |
5298 | if THISLEVEL_ONLY. */ |
5299 | int allow_template_parms_p = 1; | |
8d08fdba MS |
5300 | |
5301 | for (level = binding_level; level; level = level->level_chain) | |
5302 | { | |
5303 | register tree tail; | |
5304 | if (ANON_AGGRNAME_P (name)) | |
5305 | for (tail = level->tags; tail; tail = TREE_CHAIN (tail)) | |
5306 | { | |
5307 | /* There's no need for error checking here, because | |
5308 | anon names are unique throughout the compilation. */ | |
5309 | if (TYPE_IDENTIFIER (TREE_VALUE (tail)) == name) | |
5310 | return TREE_VALUE (tail); | |
5311 | } | |
2c73f9f5 | 5312 | else if (level->namespace_p) |
a1c65f9f | 5313 | /* Do namespace lookup. */ |
6c011b01 | 5314 | for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail)) |
2c73f9f5 | 5315 | { |
36a117a5 MM |
5316 | tree old = binding_for_name (name, tail); |
5317 | ||
74b846e0 MM |
5318 | /* If we just skipped past a template parameter level, |
5319 | even though THISLEVEL_ONLY, and we find a template | |
5320 | class declaration, then we use the _TYPE node for the | |
36a117a5 | 5321 | template. See the example below. */ |
74b846e0 | 5322 | if (thislevel_only && !allow_template_parms_p |
68642fb6 | 5323 | && old && BINDING_VALUE (old) |
36a117a5 MM |
5324 | && DECL_CLASS_TEMPLATE_P (BINDING_VALUE (old))) |
5325 | old = TREE_TYPE (BINDING_VALUE (old)); | |
68642fb6 | 5326 | else |
36a117a5 MM |
5327 | old = BINDING_TYPE (old); |
5328 | ||
4782bd5b | 5329 | if (old) |
2c73f9f5 | 5330 | { |
4782bd5b RS |
5331 | /* We've found something at this binding level. If it is |
5332 | a typedef, extract the tag it refers to. Lookup fails | |
5333 | if the typedef doesn't refer to a taggable type. */ | |
5334 | old = follow_tag_typedef (old); | |
5335 | if (!old) | |
5336 | return NULL_TREE; | |
5337 | if (TREE_CODE (old) != form | |
5338 | && (form == ENUMERAL_TYPE | |
5339 | || TREE_CODE (old) == ENUMERAL_TYPE)) | |
5340 | { | |
5341 | error ("`%#D' redeclared as %C", old, form); | |
5342 | return NULL_TREE; | |
5343 | } | |
5344 | return old; | |
2c73f9f5 | 5345 | } |
2c73f9f5 ML |
5346 | if (thislevel_only || tail == global_namespace) |
5347 | return NULL_TREE; | |
5348 | } | |
8d08fdba MS |
5349 | else |
5350 | for (tail = level->tags; tail; tail = TREE_CHAIN (tail)) | |
5351 | { | |
a80e4195 | 5352 | if (TREE_PURPOSE (tail) == name) |
8d08fdba MS |
5353 | { |
5354 | enum tree_code code = TREE_CODE (TREE_VALUE (tail)); | |
fd87b7a7 | 5355 | |
8d08fdba | 5356 | if (code != form |
fd87b7a7 | 5357 | && (form == ENUMERAL_TYPE || code == ENUMERAL_TYPE)) |
8d08fdba MS |
5358 | { |
5359 | /* Definition isn't the kind we were looking for. */ | |
fd87b7a7 | 5360 | error ("`%#D' redeclared as %C", TREE_VALUE (tail), form); |
72b7eeff | 5361 | return NULL_TREE; |
8d08fdba MS |
5362 | } |
5363 | return TREE_VALUE (tail); | |
5364 | } | |
5365 | } | |
5366 | if (thislevel_only && ! level->tag_transparent) | |
5566b478 | 5367 | { |
74b846e0 | 5368 | if (level->template_parms_p && allow_template_parms_p) |
5566b478 | 5369 | { |
36a117a5 | 5370 | /* We must deal with cases like this: |
68642fb6 | 5371 | |
36a117a5 MM |
5372 | template <class T> struct S; |
5373 | template <class T> struct S {}; | |
68642fb6 | 5374 | |
36a117a5 MM |
5375 | When looking up `S', for the second declaration, we |
5376 | would like to find the first declaration. But, we | |
5377 | are in the pseudo-global level created for the | |
5378 | template parameters, rather than the (surrounding) | |
5379 | namespace level. Thus, we keep going one more level, | |
838dfd8a | 5380 | even though THISLEVEL_ONLY is nonzero. */ |
74b846e0 | 5381 | allow_template_parms_p = 0; |
36a117a5 | 5382 | continue; |
5566b478 | 5383 | } |
36a117a5 MM |
5384 | else |
5385 | return NULL_TREE; | |
5566b478 | 5386 | } |
8d08fdba MS |
5387 | } |
5388 | return NULL_TREE; | |
5389 | } | |
5390 | ||
bd6dd845 | 5391 | #if 0 |
8d08fdba MS |
5392 | void |
5393 | set_current_level_tags_transparency (tags_transparent) | |
5394 | int tags_transparent; | |
5395 | { | |
5396 | current_binding_level->tag_transparent = tags_transparent; | |
5397 | } | |
bd6dd845 | 5398 | #endif |
8d08fdba MS |
5399 | |
5400 | /* Given a type, find the tag that was defined for it and return the tag name. | |
5401 | Otherwise return 0. However, the value can never be 0 | |
5402 | in the cases in which this is used. | |
5403 | ||
838dfd8a | 5404 | C++: If NAME is nonzero, this is the new name to install. This is |
8d08fdba MS |
5405 | done when replacing anonymous tags with real tag names. */ |
5406 | ||
5407 | static tree | |
5408 | lookup_tag_reverse (type, name) | |
5409 | tree type; | |
5410 | tree name; | |
5411 | { | |
e2500fed | 5412 | register struct cp_binding_level *level; |
8d08fdba MS |
5413 | |
5414 | for (level = current_binding_level; level; level = level->level_chain) | |
5415 | { | |
5416 | register tree tail; | |
5417 | for (tail = level->tags; tail; tail = TREE_CHAIN (tail)) | |
5418 | { | |
5419 | if (TREE_VALUE (tail) == type) | |
5420 | { | |
5421 | if (name) | |
5422 | TREE_PURPOSE (tail) = name; | |
5423 | return TREE_PURPOSE (tail); | |
5424 | } | |
5425 | } | |
5426 | } | |
5427 | return NULL_TREE; | |
5428 | } | |
8d08fdba | 5429 | \f |
a9aedbc2 | 5430 | /* Look up NAME in the NAMESPACE. */ |
e92cc029 | 5431 | |
a9aedbc2 MS |
5432 | tree |
5433 | lookup_namespace_name (namespace, name) | |
5434 | tree namespace, name; | |
5435 | { | |
30394414 | 5436 | tree val; |
f30c84c9 | 5437 | tree template_id = NULL_TREE; |
2c73f9f5 | 5438 | |
30394414 | 5439 | my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370); |
1231fb96 | 5440 | |
1231fb96 | 5441 | if (TREE_CODE (name) == NAMESPACE_DECL) |
a1c65f9f | 5442 | /* This happens for A::B<int> when B is a namespace. */ |
1231fb96 | 5443 | return name; |
d8f8dca1 MM |
5444 | else if (TREE_CODE (name) == TEMPLATE_DECL) |
5445 | { | |
5446 | /* This happens for A::B where B is a template, and there are no | |
5447 | template arguments. */ | |
33bd39a2 | 5448 | error ("invalid use of `%D'", name); |
d8f8dca1 MM |
5449 | return error_mark_node; |
5450 | } | |
1231fb96 | 5451 | |
b262d64c JM |
5452 | namespace = ORIGINAL_NAMESPACE (namespace); |
5453 | ||
f30c84c9 MM |
5454 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) |
5455 | { | |
5456 | template_id = name; | |
5457 | name = TREE_OPERAND (name, 0); | |
5458 | if (TREE_CODE (name) == OVERLOAD) | |
5459 | name = DECL_NAME (OVL_CURRENT (name)); | |
2f939d94 | 5460 | else if (DECL_P (name)) |
f30c84c9 MM |
5461 | name = DECL_NAME (name); |
5462 | } | |
5463 | ||
30394414 | 5464 | my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373); |
68642fb6 | 5465 | |
87e3dbc9 | 5466 | val = make_node (CPLUS_BINDING); |
52c11ef6 | 5467 | if (!qualified_lookup_using_namespace (name, namespace, val, 0)) |
2c73f9f5 ML |
5468 | return error_mark_node; |
5469 | ||
5470 | if (BINDING_VALUE (val)) | |
1c35f5b6 JM |
5471 | { |
5472 | val = BINDING_VALUE (val); | |
5473 | ||
f30c84c9 MM |
5474 | if (template_id) |
5475 | { | |
5476 | if (DECL_CLASS_TEMPLATE_P (val)) | |
68642fb6 | 5477 | val = lookup_template_class (val, |
f30c84c9 MM |
5478 | TREE_OPERAND (template_id, 1), |
5479 | /*in_decl=*/NULL_TREE, | |
5480 | /*context=*/NULL_TREE, | |
f9c244b8 | 5481 | /*entering_scope=*/0, |
c2ea3a40 | 5482 | tf_error | tf_warning); |
f30c84c9 MM |
5483 | else if (DECL_FUNCTION_TEMPLATE_P (val) |
5484 | || TREE_CODE (val) == OVERLOAD) | |
68642fb6 | 5485 | val = lookup_template_function (val, |
f30c84c9 MM |
5486 | TREE_OPERAND (template_id, 1)); |
5487 | else | |
5488 | { | |
33bd39a2 | 5489 | error ("`%D::%D' is not a template", |
f30c84c9 MM |
5490 | namespace, name); |
5491 | return error_mark_node; | |
5492 | } | |
5493 | } | |
5494 | ||
1c35f5b6 JM |
5495 | /* If we have a single function from a using decl, pull it out. */ |
5496 | if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val)) | |
5497 | val = OVL_FUNCTION (val); | |
d52e4867 RS |
5498 | |
5499 | /* Ignore built-in functions that haven't been prototyped yet. */ | |
5500 | if (!val || !DECL_P(val) | |
5501 | || !DECL_LANG_SPECIFIC(val) | |
5502 | || !DECL_ANTICIPATED (val)) | |
5503 | return val; | |
1c35f5b6 JM |
5504 | } |
5505 | ||
33bd39a2 | 5506 | error ("`%D' undeclared in namespace `%D'", name, namespace); |
30394414 | 5507 | return error_mark_node; |
a9aedbc2 MS |
5508 | } |
5509 | ||
7ddedda4 MM |
5510 | /* Hash a TYPENAME_TYPE. K is really of type `tree'. */ |
5511 | ||
e2500fed | 5512 | static hashval_t |
7ddedda4 | 5513 | typename_hash (k) |
e2500fed | 5514 | const void * k; |
7ddedda4 | 5515 | { |
e2500fed GK |
5516 | hashval_t hash; |
5517 | tree t = (tree) k; | |
7ddedda4 | 5518 | |
e2500fed GK |
5519 | hash = (((hashval_t) TYPE_CONTEXT (t)) |
5520 | ^ ((hashval_t) DECL_NAME (TYPE_NAME (t)))); | |
7ddedda4 MM |
5521 | |
5522 | return hash; | |
5523 | } | |
5524 | ||
5525 | /* Compare two TYPENAME_TYPEs. K1 and K2 are really of type `tree'. */ | |
5526 | ||
e2500fed | 5527 | static int |
7ddedda4 | 5528 | typename_compare (k1, k2) |
e2500fed GK |
5529 | const void * k1; |
5530 | const void * k2; | |
7ddedda4 MM |
5531 | { |
5532 | tree t1; | |
5533 | tree t2; | |
5534 | tree d1; | |
5535 | tree d2; | |
5536 | ||
5537 | t1 = (tree) k1; | |
5538 | t2 = (tree) k2; | |
5539 | d1 = TYPE_NAME (t1); | |
5540 | d2 = TYPE_NAME (t2); | |
68642fb6 | 5541 | |
7ddedda4 | 5542 | return (DECL_NAME (d1) == DECL_NAME (d2) |
e2500fed | 5543 | && TYPE_CONTEXT (t1) == TYPE_CONTEXT (t2) |
68642fb6 | 5544 | && ((TREE_TYPE (t1) != NULL_TREE) |
7ddedda4 MM |
5545 | == (TREE_TYPE (t2) != NULL_TREE)) |
5546 | && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)) | |
5547 | && TYPENAME_TYPE_FULLNAME (t1) == TYPENAME_TYPE_FULLNAME (t2)); | |
5548 | } | |
5549 | ||
45869a6c MM |
5550 | /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is |
5551 | the type of `T', NAME is the IDENTIFIER_NODE for `t'. If BASE_TYPE | |
5552 | is non-NULL, this type is being created by the implicit typename | |
5553 | extension, and BASE_TYPE is a type named `t' in some base class of | |
68642fb6 | 5554 | `T' which depends on template parameters. |
45869a6c MM |
5555 | |
5556 | Returns the new TYPENAME_TYPE. */ | |
5557 | ||
e2500fed GK |
5558 | static GTY ((param_is (union tree_node))) htab_t typename_htab; |
5559 | ||
45869a6c MM |
5560 | tree |
5561 | build_typename_type (context, name, fullname, base_type) | |
5562 | tree context; | |
5563 | tree name; | |
5564 | tree fullname; | |
5565 | tree base_type; | |
5566 | { | |
5567 | tree t; | |
5568 | tree d; | |
e2500fed | 5569 | PTR *e; |
45869a6c | 5570 | |
e2500fed | 5571 | if (typename_htab == NULL) |
9cd64686 | 5572 | { |
e2500fed GK |
5573 | typename_htab = htab_create_ggc (61, &typename_hash, |
5574 | &typename_compare, NULL); | |
9cd64686 | 5575 | } |
45869a6c MM |
5576 | |
5577 | /* Build the TYPENAME_TYPE. */ | |
33848bb0 | 5578 | t = make_aggr_type (TYPENAME_TYPE); |
45869a6c MM |
5579 | TYPE_CONTEXT (t) = FROB_CONTEXT (context); |
5580 | TYPENAME_TYPE_FULLNAME (t) = fullname; | |
5581 | TREE_TYPE (t) = base_type; | |
45869a6c MM |
5582 | |
5583 | /* Build the corresponding TYPE_DECL. */ | |
5584 | d = build_decl (TYPE_DECL, name, t); | |
5585 | TYPE_NAME (TREE_TYPE (d)) = d; | |
5586 | TYPE_STUB_DECL (TREE_TYPE (d)) = d; | |
5587 | DECL_CONTEXT (d) = FROB_CONTEXT (context); | |
2642b9bf | 5588 | DECL_ARTIFICIAL (d) = 1; |
45869a6c | 5589 | |
7ddedda4 | 5590 | /* See if we already have this type. */ |
e2500fed GK |
5591 | e = htab_find_slot (typename_htab, t, INSERT); |
5592 | if (*e) | |
5593 | t = (tree) *e; | |
7ddedda4 | 5594 | else |
e2500fed | 5595 | *e = t; |
7ddedda4 | 5596 | |
45869a6c MM |
5597 | return t; |
5598 | } | |
5599 | ||
3baa501d MM |
5600 | /* Resolve `typename CONTEXT::NAME'. Returns an appropriate type, |
5601 | unless an error occurs, in which case error_mark_node is returned. | |
4f2b0fb2 NS |
5602 | If we locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is |
5603 | set, we return that, rather than the _TYPE it corresponds to, in | |
5604 | other cases we look through the type decl. If TF_ERROR is set, | |
5605 | complain about errors, otherwise be quiet. */ | |
3baa501d | 5606 | |
5566b478 | 5607 | tree |
3baa501d | 5608 | make_typename_type (context, name, complain) |
5566b478 | 5609 | tree context, name; |
4f2b0fb2 | 5610 | tsubst_flags_t complain; |
5566b478 | 5611 | { |
b2b7d40a | 5612 | tree fullname; |
a80e4195 | 5613 | |
2f939d94 | 5614 | if (TYPE_P (name)) |
78638e24 | 5615 | { |
68642fb6 UD |
5616 | if (!(TYPE_LANG_SPECIFIC (name) |
5617 | && (CLASSTYPE_IS_TEMPLATE (name) | |
78638e24 MM |
5618 | || CLASSTYPE_USE_TEMPLATE (name)))) |
5619 | name = TYPE_IDENTIFIER (name); | |
5620 | else | |
5621 | /* Create a TEMPLATE_ID_EXPR for the type. */ | |
5622 | name = build_nt (TEMPLATE_ID_EXPR, | |
5623 | CLASSTYPE_TI_TEMPLATE (name), | |
5624 | CLASSTYPE_TI_ARGS (name)); | |
5625 | } | |
653cc74a | 5626 | else if (TREE_CODE (name) == TYPE_DECL) |
a80e4195 | 5627 | name = DECL_NAME (name); |
b2b7d40a JM |
5628 | |
5629 | fullname = name; | |
5630 | ||
5631 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) | |
11686454 JM |
5632 | { |
5633 | name = TREE_OPERAND (name, 0); | |
5634 | if (TREE_CODE (name) == TEMPLATE_DECL) | |
5635 | name = TREE_OPERAND (fullname, 0) = DECL_NAME (name); | |
5636 | } | |
8a2b77e7 JM |
5637 | if (TREE_CODE (name) == TEMPLATE_DECL) |
5638 | { | |
33bd39a2 | 5639 | error ("`%D' used without template parameters", name); |
8a2b77e7 JM |
5640 | return error_mark_node; |
5641 | } | |
b2b7d40a | 5642 | if (TREE_CODE (name) != IDENTIFIER_NODE) |
a98facb0 | 5643 | abort (); |
5566b478 | 5644 | |
04ddee1b BK |
5645 | if (TREE_CODE (context) == NAMESPACE_DECL) |
5646 | { | |
5647 | /* We can get here from typename_sub0 in the explicit_template_type | |
5648 | expansion. Just fail. */ | |
4f2b0fb2 | 5649 | if (complain & tf_error) |
33bd39a2 | 5650 | error ("no class template named `%#T' in `%#T'", |
3baa501d | 5651 | name, context); |
04ddee1b BK |
5652 | return error_mark_node; |
5653 | } | |
5654 | ||
85b71cf2 | 5655 | if (! uses_template_parms (context) |
b77ead33 | 5656 | || currently_open_class (context)) |
5566b478 | 5657 | { |
b2b7d40a JM |
5658 | if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR) |
5659 | { | |
ad810b22 | 5660 | tree tmpl = NULL_TREE; |
b2b7d40a | 5661 | if (IS_AGGR_TYPE (context)) |
ad810b22 MM |
5662 | tmpl = lookup_field (context, name, 0, 0); |
5663 | if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl)) | |
b2b7d40a | 5664 | { |
4f2b0fb2 | 5665 | if (complain & tf_error) |
33bd39a2 | 5666 | error ("no class template named `%#T' in `%#T'", |
3baa501d | 5667 | name, context); |
b2b7d40a JM |
5668 | return error_mark_node; |
5669 | } | |
ffb690bd | 5670 | |
0e902d98 KL |
5671 | if (complain & tf_parsing) |
5672 | type_access_control (context, tmpl); | |
5673 | else | |
5674 | enforce_access (context, tmpl); | |
ba59df78 | 5675 | |
68642fb6 | 5676 | return lookup_template_class (tmpl, |
ad810b22 | 5677 | TREE_OPERAND (fullname, 1), |
68642fb6 | 5678 | NULL_TREE, context, |
f9c244b8 | 5679 | /*entering_scope=*/0, |
c2ea3a40 | 5680 | tf_error | tf_warning); |
b2b7d40a JM |
5681 | } |
5682 | else | |
5566b478 | 5683 | { |
b4f70b3d | 5684 | tree t; |
68642fb6 | 5685 | |
b4f70b3d | 5686 | if (!IS_AGGR_TYPE (context)) |
b2b7d40a | 5687 | { |
4f2b0fb2 | 5688 | if (complain & tf_error) |
33bd39a2 | 5689 | error ("no type named `%#T' in `%#T'", name, context); |
b2b7d40a JM |
5690 | return error_mark_node; |
5691 | } | |
1107c4b3 | 5692 | |
b4f70b3d | 5693 | t = lookup_field (context, name, 0, 1); |
7d4bdeed | 5694 | if (t) |
4f2b0fb2 | 5695 | { |
0e902d98 KL |
5696 | if (complain & tf_parsing) |
5697 | type_access_control (context, t); | |
5698 | else | |
5699 | enforce_access (context, t); | |
ba59df78 | 5700 | |
4f2b0fb2 NS |
5701 | if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl)) |
5702 | t = TREE_TYPE (t); | |
ba3307c0 NS |
5703 | if (IMPLICIT_TYPENAME_P (t)) |
5704 | { | |
5705 | /* Lookup found an implicit typename that we had | |
5706 | injected into the current scope. Doing things | |
5707 | properly would have located the exact same type, | |
5708 | so there is no error here. We must remove the | |
5709 | implicitness so that we do not warn about it. */ | |
5710 | t = copy_node (t); | |
5711 | TREE_TYPE (t) = NULL_TREE; | |
5712 | } | |
5713 | ||
4f2b0fb2 NS |
5714 | return t; |
5715 | } | |
5566b478 | 5716 | } |
5566b478 | 5717 | } |
11249cf0 MM |
5718 | |
5719 | /* If the CONTEXT is not a template type, then either the field is | |
5720 | there now or its never going to be. */ | |
b4f70b3d | 5721 | if (!uses_template_parms (context)) |
11249cf0 | 5722 | { |
4f2b0fb2 | 5723 | if (complain & tf_error) |
33bd39a2 | 5724 | error ("no type named `%#T' in `%#T'", name, context); |
11249cf0 MM |
5725 | return error_mark_node; |
5726 | } | |
68642fb6 | 5727 | |
45869a6c | 5728 | return build_typename_type (context, name, fullname, NULL_TREE); |
5566b478 MS |
5729 | } |
5730 | ||
b8c6534b KL |
5731 | /* Resolve `CONTEXT::template NAME'. Returns an appropriate type, |
5732 | unless an error occurs, in which case error_mark_node is returned. | |
4f2b0fb2 NS |
5733 | If we locate a TYPE_DECL, we return that, rather than the _TYPE it |
5734 | corresponds to. If COMPLAIN zero, don't complain about any errors | |
5735 | that occur. */ | |
b8c6534b KL |
5736 | |
5737 | tree | |
5738 | make_unbound_class_template (context, name, complain) | |
5739 | tree context, name; | |
5740 | int complain; | |
5741 | { | |
5742 | tree t; | |
5743 | tree d; | |
5744 | ||
5745 | if (TYPE_P (name)) | |
5746 | name = TYPE_IDENTIFIER (name); | |
5747 | else if (DECL_P (name)) | |
5748 | name = DECL_NAME (name); | |
5749 | if (TREE_CODE (name) != IDENTIFIER_NODE) | |
a98facb0 | 5750 | abort (); |
b8c6534b KL |
5751 | |
5752 | if (!uses_template_parms (context) | |
5753 | || currently_open_class (context)) | |
5754 | { | |
5755 | tree tmpl = NULL_TREE; | |
5756 | ||
5757 | if (IS_AGGR_TYPE (context)) | |
5758 | tmpl = lookup_field (context, name, 0, 0); | |
5759 | ||
5760 | if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl)) | |
5761 | { | |
5762 | if (complain) | |
33bd39a2 | 5763 | error ("no class template named `%#T' in `%#T'", name, context); |
b8c6534b KL |
5764 | return error_mark_node; |
5765 | } | |
5766 | ||
0e902d98 KL |
5767 | if (complain & tf_parsing) |
5768 | type_access_control (context, tmpl); | |
5769 | else | |
5770 | enforce_access (context, tmpl); | |
b8c6534b KL |
5771 | |
5772 | return tmpl; | |
5773 | } | |
5774 | ||
5775 | /* Build the UNBOUND_CLASS_TEMPLATE. */ | |
5776 | t = make_aggr_type (UNBOUND_CLASS_TEMPLATE); | |
5777 | TYPE_CONTEXT (t) = FROB_CONTEXT (context); | |
5778 | TREE_TYPE (t) = NULL_TREE; | |
5779 | ||
5780 | /* Build the corresponding TEMPLATE_DECL. */ | |
5781 | d = build_decl (TEMPLATE_DECL, name, t); | |
5782 | TYPE_NAME (TREE_TYPE (d)) = d; | |
5783 | TYPE_STUB_DECL (TREE_TYPE (d)) = d; | |
5784 | DECL_CONTEXT (d) = FROB_CONTEXT (context); | |
5785 | DECL_ARTIFICIAL (d) = 1; | |
5786 | ||
5787 | return t; | |
5788 | } | |
5789 | ||
a1c65f9f | 5790 | /* Select the right _DECL from multiple choices. */ |
2c73f9f5 ML |
5791 | |
5792 | static tree | |
52c11ef6 | 5793 | select_decl (binding, flags) |
2c73f9f5 | 5794 | tree binding; |
52c11ef6 | 5795 | int flags; |
2c73f9f5 ML |
5796 | { |
5797 | tree val; | |
5798 | val = BINDING_VALUE (binding); | |
97ba1e3c | 5799 | |
52c11ef6 | 5800 | if (LOOKUP_NAMESPACES_ONLY (flags)) |
3e3f722c | 5801 | { |
a1c65f9f | 5802 | /* We are not interested in types. */ |
3e3f722c ML |
5803 | if (val && TREE_CODE (val) == NAMESPACE_DECL) |
5804 | return val; | |
5805 | return NULL_TREE; | |
5806 | } | |
68642fb6 | 5807 | |
2c73f9f5 ML |
5808 | /* If we could have a type and |
5809 | we have nothing or we need a type and have none. */ | |
5810 | if (BINDING_TYPE (binding) | |
52c11ef6 JM |
5811 | && (!val || ((flags & LOOKUP_PREFER_TYPES) |
5812 | && TREE_CODE (val) != TYPE_DECL))) | |
2c73f9f5 | 5813 | val = TYPE_STUB_DECL (BINDING_TYPE (binding)); |
a1c65f9f | 5814 | /* Don't return non-types if we really prefer types. */ |
52c11ef6 | 5815 | else if (val && LOOKUP_TYPES_ONLY (flags) && TREE_CODE (val) != TYPE_DECL |
c592d5d2 ML |
5816 | && (TREE_CODE (val) != TEMPLATE_DECL |
5817 | || !DECL_CLASS_TEMPLATE_P (val))) | |
2c73f9f5 | 5818 | val = NULL_TREE; |
1c35f5b6 | 5819 | |
2c73f9f5 ML |
5820 | return val; |
5821 | } | |
5822 | ||
2c169bab JM |
5823 | /* Unscoped lookup of a global: iterate over current namespaces, |
5824 | considering using-directives. If SPACESP is non-NULL, store a list | |
5825 | of the namespaces we've considered in it. */ | |
ea9635c7 | 5826 | |
2c169bab JM |
5827 | tree |
5828 | unqualified_namespace_lookup (name, flags, spacesp) | |
ea9635c7 | 5829 | tree name; |
52c11ef6 | 5830 | int flags; |
2c169bab | 5831 | tree *spacesp; |
ea9635c7 | 5832 | { |
87e3dbc9 | 5833 | tree b = make_node (CPLUS_BINDING); |
ee8fc32b | 5834 | tree initial = current_decl_namespace (); |
ea9635c7 ML |
5835 | tree scope = initial; |
5836 | tree siter; | |
e2500fed | 5837 | struct cp_binding_level *level; |
ea9635c7 ML |
5838 | tree val = NULL_TREE; |
5839 | ||
2c169bab JM |
5840 | if (spacesp) |
5841 | *spacesp = NULL_TREE; | |
5842 | ||
5843 | for (; !val; scope = CP_DECL_CONTEXT (scope)) | |
ea9635c7 | 5844 | { |
2c169bab | 5845 | if (spacesp) |
e1b3e07d | 5846 | *spacesp = tree_cons (scope, NULL_TREE, *spacesp); |
ea9635c7 ML |
5847 | val = binding_for_name (name, scope); |
5848 | ||
d52e4867 RS |
5849 | /* Ignore anticipated built-in functions. */ |
5850 | if (val && BINDING_VALUE (val) | |
5851 | && DECL_P (BINDING_VALUE (val)) | |
5852 | && DECL_LANG_SPECIFIC (BINDING_VALUE (val)) | |
5853 | && DECL_ANTICIPATED (BINDING_VALUE (val))) | |
5854 | { | |
5855 | BINDING_VALUE (b) = NULL_TREE; | |
5856 | BINDING_TYPE (b) = NULL_TREE; | |
5857 | } | |
5858 | else | |
5859 | { | |
a1c65f9f | 5860 | /* Initialize binding for this context. */ |
d52e4867 RS |
5861 | BINDING_VALUE (b) = BINDING_VALUE (val); |
5862 | BINDING_TYPE (b) = BINDING_TYPE (val); | |
5863 | } | |
ea9635c7 | 5864 | |
a1c65f9f | 5865 | /* Add all _DECLs seen through local using-directives. */ |
68642fb6 | 5866 | for (level = current_binding_level; |
ea9635c7 ML |
5867 | !level->namespace_p; |
5868 | level = level->level_chain) | |
52c11ef6 | 5869 | if (!lookup_using_namespace (name, b, level->using_directives, |
2c169bab | 5870 | scope, flags, spacesp)) |
a1c65f9f | 5871 | /* Give up because of error. */ |
5b163de4 | 5872 | return error_mark_node; |
ea9635c7 | 5873 | |
a1c65f9f KH |
5874 | /* Add all _DECLs seen through global using-directives. */ |
5875 | /* XXX local and global using lists should work equally. */ | |
ea9635c7 ML |
5876 | siter = initial; |
5877 | while (1) | |
5878 | { | |
68642fb6 | 5879 | if (!lookup_using_namespace (name, b, DECL_NAMESPACE_USING (siter), |
2c169bab | 5880 | scope, flags, spacesp)) |
a1c65f9f | 5881 | /* Give up because of error. */ |
5b163de4 | 5882 | return error_mark_node; |
ea9635c7 ML |
5883 | if (siter == scope) break; |
5884 | siter = CP_DECL_CONTEXT (siter); | |
5885 | } | |
5886 | ||
52c11ef6 | 5887 | val = select_decl (b, flags); |
ea9635c7 ML |
5888 | if (scope == global_namespace) |
5889 | break; | |
ea9635c7 ML |
5890 | } |
5891 | return val; | |
5892 | } | |
5893 | ||
52c11ef6 JM |
5894 | /* Combine prefer_type and namespaces_only into flags. */ |
5895 | ||
5896 | static int | |
5897 | lookup_flags (prefer_type, namespaces_only) | |
5898 | int prefer_type, namespaces_only; | |
5899 | { | |
5900 | if (namespaces_only) | |
5901 | return LOOKUP_PREFER_NAMESPACES; | |
5902 | if (prefer_type > 1) | |
5903 | return LOOKUP_PREFER_TYPES; | |
5904 | if (prefer_type > 0) | |
5905 | return LOOKUP_PREFER_BOTH; | |
5906 | return 0; | |
5907 | } | |
5908 | ||
5909 | /* Given a lookup that returned VAL, use FLAGS to decide if we want to | |
5910 | ignore it or not. Subroutine of lookup_name_real. */ | |
5911 | ||
5912 | static tree | |
5913 | qualify_lookup (val, flags) | |
5914 | tree val; | |
5915 | int flags; | |
5916 | { | |
5917 | if (val == NULL_TREE) | |
5918 | return val; | |
6b945830 JM |
5919 | if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL) |
5920 | return val; | |
5921 | if ((flags & LOOKUP_PREFER_TYPES) | |
5922 | && (TREE_CODE (val) == TYPE_DECL | |
5923 | || ((flags & LOOKUP_TEMPLATES_EXPECTED) | |
5924 | && DECL_CLASS_TEMPLATE_P (val)))) | |
5925 | return val; | |
5926 | if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES)) | |
52c11ef6 JM |
5927 | return NULL_TREE; |
5928 | return val; | |
5929 | } | |
5930 | ||
235f734d MM |
5931 | /* Any other BINDING overrides an implicit TYPENAME. Warn about |
5932 | that. */ | |
5933 | ||
5934 | static void | |
5935 | warn_about_implicit_typename_lookup (typename, binding) | |
5936 | tree typename; | |
5937 | tree binding; | |
5938 | { | |
5939 | tree subtype = TREE_TYPE (TREE_TYPE (typename)); | |
5940 | tree name = DECL_NAME (typename); | |
5941 | ||
5942 | if (! (TREE_CODE (binding) == TEMPLATE_DECL | |
5943 | && CLASSTYPE_TEMPLATE_INFO (subtype) | |
5944 | && CLASSTYPE_TI_TEMPLATE (subtype) == binding) | |
5945 | && ! (TREE_CODE (binding) == TYPE_DECL | |
5946 | && same_type_p (TREE_TYPE (binding), subtype))) | |
5947 | { | |
33bd39a2 | 5948 | warning ("lookup of `%D' finds `%#D'", |
235f734d | 5949 | name, binding); |
33bd39a2 | 5950 | warning (" instead of `%D' from dependent base class", |
235f734d | 5951 | typename); |
33bd39a2 | 5952 | warning (" (use `typename %T::%D' if that's what you meant)", |
235f734d MM |
5953 | constructor_name (current_class_type), name); |
5954 | } | |
5955 | } | |
5956 | ||
5f261ba9 MM |
5957 | /* Check to see whether or not DECL is a variable that would have been |
5958 | in scope under the ARM, but is not in scope under the ANSI/ISO | |
5959 | standard. If so, issue an error message. If name lookup would | |
5960 | work in both cases, but return a different result, this function | |
5961 | returns the result of ANSI/ISO lookup. Otherwise, it returns | |
5962 | DECL. */ | |
5963 | ||
5964 | tree | |
5965 | check_for_out_of_scope_variable (tree decl) | |
5966 | { | |
5967 | tree shadowed; | |
5968 | ||
5969 | /* We only care about out of scope variables. */ | |
5970 | if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl))) | |
5971 | return decl; | |
5972 | ||
5973 | shadowed = DECL_SHADOWED_FOR_VAR (decl); | |
5974 | while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL | |
5975 | && DECL_DEAD_FOR_LOCAL (shadowed)) | |
5976 | shadowed = DECL_SHADOWED_FOR_VAR (shadowed); | |
5977 | if (!shadowed) | |
5978 | shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl)); | |
5979 | if (shadowed) | |
5980 | { | |
5981 | if (!DECL_ERROR_REPORTED (decl)) | |
5982 | { | |
5983 | warning ("name lookup of `%D' changed", | |
5984 | DECL_NAME (decl)); | |
5985 | cp_warning_at (" matches this `%D' under ISO standard rules", | |
5986 | shadowed); | |
5987 | cp_warning_at (" matches this `%D' under old rules", decl); | |
5988 | DECL_ERROR_REPORTED (decl) = 1; | |
5989 | } | |
5990 | return shadowed; | |
5991 | } | |
5992 | ||
5993 | /* If we have already complained about this declaration, there's no | |
5994 | need to do it again. */ | |
5995 | if (DECL_ERROR_REPORTED (decl)) | |
5996 | return decl; | |
5997 | ||
5998 | DECL_ERROR_REPORTED (decl) = 1; | |
5999 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))) | |
6000 | { | |
6001 | error ("name lookup of `%D' changed for new ISO `for' scoping", | |
6002 | DECL_NAME (decl)); | |
6003 | cp_error_at (" cannot use obsolete binding at `%D' because it has a destructor", decl); | |
6004 | return error_mark_node; | |
6005 | } | |
6006 | else | |
6007 | { | |
6008 | pedwarn ("name lookup of `%D' changed for new ISO `for' scoping", | |
6009 | DECL_NAME (decl)); | |
6010 | cp_pedwarn_at (" using obsolete binding at `%D'", decl); | |
6011 | } | |
6012 | ||
6013 | return decl; | |
6014 | } | |
6015 | ||
8d08fdba MS |
6016 | /* Look up NAME in the current binding level and its superiors in the |
6017 | namespace of variables, functions and typedefs. Return a ..._DECL | |
6018 | node of some kind representing its definition if there is only one | |
6019 | such declaration, or return a TREE_LIST with all the overloaded | |
6020 | definitions if there are many, or return 0 if it is undefined. | |
6021 | ||
2c73f9f5 ML |
6022 | If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces. |
6023 | If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces). | |
a28e3c7f | 6024 | If PREFER_TYPE is -2, we're being called from yylex(). (UGLY) |
68642fb6 | 6025 | Otherwise we prefer non-TYPE_DECLs. |
f84b4be9 | 6026 | |
838dfd8a | 6027 | If NONCLASS is nonzero, we don't look for the NAME in class scope, |
f84b4be9 | 6028 | using IDENTIFIER_CLASS_VALUE. */ |
8d08fdba | 6029 | |
824b9a4c | 6030 | static tree |
3e3f722c | 6031 | lookup_name_real (name, prefer_type, nonclass, namespaces_only) |
8d08fdba | 6032 | tree name; |
3e3f722c | 6033 | int prefer_type, nonclass, namespaces_only; |
8d08fdba | 6034 | { |
235f734d MM |
6035 | tree t; |
6036 | tree val = NULL_TREE; | |
a28e3c7f | 6037 | int yylex = 0; |
e1cd6e56 | 6038 | tree from_obj = NULL_TREE; |
52c11ef6 | 6039 | int flags; |
235f734d | 6040 | int val_is_implicit_typename = 0; |
8d08fdba | 6041 | |
a1c65f9f | 6042 | /* Hack: copy flag set by parser, if set. */ |
3e3f722c ML |
6043 | if (only_namespace_names) |
6044 | namespaces_only = 1; | |
52c11ef6 | 6045 | |
a28e3c7f MS |
6046 | if (prefer_type == -2) |
6047 | { | |
6048 | extern int looking_for_typename; | |
fc378698 | 6049 | tree type = NULL_TREE; |
a28e3c7f MS |
6050 | |
6051 | yylex = 1; | |
6052 | prefer_type = looking_for_typename; | |
e1cd6e56 | 6053 | |
52c11ef6 | 6054 | flags = lookup_flags (prefer_type, namespaces_only); |
a1c65f9f | 6055 | /* If the next thing is '<', class templates are types. */ |
52c11ef6 JM |
6056 | if (looking_for_template) |
6057 | flags |= LOOKUP_TEMPLATES_EXPECTED; | |
6058 | ||
e1cd6e56 MS |
6059 | if (got_scope) |
6060 | type = got_scope; | |
dff6b454 | 6061 | else if (got_object != error_mark_node) |
e1cd6e56 | 6062 | type = got_object; |
68642fb6 | 6063 | |
e1cd6e56 | 6064 | if (type) |
a28e3c7f | 6065 | { |
e1cd6e56 | 6066 | if (type == error_mark_node) |
f376e137 | 6067 | return error_mark_node; |
a80e4195 MS |
6068 | if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type)) |
6069 | type = TREE_TYPE (type); | |
5566b478 | 6070 | |
2b9dc906 JM |
6071 | if (TYPE_P (type)) |
6072 | type = complete_type (type); | |
5566b478 | 6073 | |
a1774733 | 6074 | if (TREE_CODE (type) == VOID_TYPE) |
2c73f9f5 ML |
6075 | type = global_namespace; |
6076 | if (TREE_CODE (type) == NAMESPACE_DECL) | |
a9aedbc2 | 6077 | { |
87e3dbc9 | 6078 | val = make_node (CPLUS_BINDING); |
6ad07332 | 6079 | flags |= LOOKUP_COMPLAIN; |
52c11ef6 | 6080 | if (!qualified_lookup_using_namespace (name, type, val, flags)) |
2c73f9f5 | 6081 | return NULL_TREE; |
52c11ef6 | 6082 | val = select_decl (val, flags); |
a9aedbc2 | 6083 | } |
5566b478 | 6084 | else if (! IS_AGGR_TYPE (type) |
5156628f | 6085 | || TREE_CODE (type) == TEMPLATE_TYPE_PARM |
a1281f45 | 6086 | || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM |
5156628f | 6087 | || TREE_CODE (type) == TYPENAME_TYPE) |
e92cc029 | 6088 | /* Someone else will give an error about this if needed. */ |
a28e3c7f | 6089 | val = NULL_TREE; |
e1cd6e56 | 6090 | else if (type == current_class_type) |
a28e3c7f | 6091 | val = IDENTIFIER_CLASS_VALUE (name); |
a28e3c7f | 6092 | else |
70adf8a9 JM |
6093 | { |
6094 | val = lookup_member (type, name, 0, prefer_type); | |
0e902d98 KL |
6095 | if (!uses_template_parms (type)) |
6096 | type_access_control (type, val); | |
e69c8072 JM |
6097 | |
6098 | /* Restore the containing TYPENAME_TYPE if we looked | |
6099 | through it before. */ | |
6100 | if (got_scope && got_scope != type | |
6101 | && val && TREE_CODE (val) == TYPE_DECL | |
6102 | && TREE_CODE (TREE_TYPE (val)) == TYPENAME_TYPE) | |
abf92e26 JM |
6103 | { |
6104 | val = TREE_TYPE (val); | |
6105 | val = build_typename_type (got_scope, name, | |
6106 | TYPENAME_TYPE_FULLNAME (val), | |
6107 | TREE_TYPE (val)); | |
6108 | val = TYPE_STUB_DECL (val); | |
6109 | } | |
70adf8a9 | 6110 | } |
a28e3c7f | 6111 | } |
e1cd6e56 MS |
6112 | else |
6113 | val = NULL_TREE; | |
6114 | ||
594740f3 | 6115 | if (got_scope) |
e1cd6e56 | 6116 | goto done; |
594740f3 | 6117 | else if (got_object && val) |
aca77bd1 JM |
6118 | { |
6119 | from_obj = val; | |
6120 | val = NULL_TREE; | |
6121 | } | |
a28e3c7f | 6122 | } |
52c11ef6 | 6123 | else |
5b163de4 JM |
6124 | { |
6125 | flags = lookup_flags (prefer_type, namespaces_only); | |
a1c65f9f | 6126 | /* If we're not parsing, we need to complain. */ |
5b163de4 JM |
6127 | flags |= LOOKUP_COMPLAIN; |
6128 | } | |
e76a2646 | 6129 | |
d8f8dca1 | 6130 | /* First, look in non-namespace scopes. */ |
6f1b4c42 JM |
6131 | |
6132 | if (current_class_type == NULL_TREE) | |
6133 | nonclass = 1; | |
6134 | ||
235f734d | 6135 | for (t = IDENTIFIER_BINDING (name); t; t = TREE_CHAIN (t)) |
d8f8dca1 | 6136 | { |
235f734d MM |
6137 | tree binding; |
6138 | ||
6139 | if (!LOCAL_BINDING_P (t) && nonclass) | |
d8f8dca1 MM |
6140 | /* We're not looking for class-scoped bindings, so keep going. */ |
6141 | continue; | |
68642fb6 | 6142 | |
d8f8dca1 | 6143 | /* If this is the kind of thing we're looking for, we're done. */ |
235f734d MM |
6144 | if (qualify_lookup (BINDING_VALUE (t), flags)) |
6145 | binding = BINDING_VALUE (t); | |
68642fb6 | 6146 | else if ((flags & LOOKUP_PREFER_TYPES) |
235f734d MM |
6147 | && qualify_lookup (BINDING_TYPE (t), flags)) |
6148 | binding = BINDING_TYPE (t); | |
6149 | else | |
6150 | binding = NULL_TREE; | |
6151 | ||
70adf8a9 JM |
6152 | /* Handle access control on types from enclosing or base classes. */ |
6153 | if (binding && ! yylex | |
6154 | && BINDING_LEVEL (t) && BINDING_LEVEL (t)->parm_flag == 2) | |
6155 | type_access_control (BINDING_LEVEL (t)->this_class, binding); | |
6156 | ||
235f734d | 6157 | if (binding |
83233dca | 6158 | && (!val || !IMPLICIT_TYPENAME_TYPE_DECL_P (binding))) |
d8f8dca1 | 6159 | { |
235f734d MM |
6160 | if (val_is_implicit_typename && !yylex) |
6161 | warn_about_implicit_typename_lookup (val, binding); | |
6162 | val = binding; | |
68642fb6 | 6163 | val_is_implicit_typename |
83233dca | 6164 | = IMPLICIT_TYPENAME_TYPE_DECL_P (val); |
235f734d MM |
6165 | if (!val_is_implicit_typename) |
6166 | break; | |
d8f8dca1 MM |
6167 | } |
6168 | } | |
f181d4ae | 6169 | |
235f734d MM |
6170 | /* Now lookup in namespace scopes. */ |
6171 | if (!val || val_is_implicit_typename) | |
e76a2646 | 6172 | { |
2c169bab | 6173 | t = unqualified_namespace_lookup (name, flags, 0); |
235f734d | 6174 | if (t) |
c1def683 | 6175 | { |
235f734d MM |
6176 | if (val_is_implicit_typename && !yylex) |
6177 | warn_about_implicit_typename_lookup (val, t); | |
6178 | val = t; | |
c1def683 JM |
6179 | } |
6180 | } | |
6181 | ||
a28e3c7f | 6182 | done: |
8d08fdba MS |
6183 | if (val) |
6184 | { | |
c91a56d2 | 6185 | /* This should only warn about types used in qualified-ids. */ |
e1cd6e56 | 6186 | if (from_obj && from_obj != val) |
5566b478 | 6187 | { |
c91a56d2 MS |
6188 | if (looking_for_typename && TREE_CODE (from_obj) == TYPE_DECL |
6189 | && TREE_CODE (val) == TYPE_DECL | |
aca77bd1 | 6190 | && ! same_type_p (TREE_TYPE (from_obj), TREE_TYPE (val))) |
33bd39a2 | 6191 | pedwarn ("\ |
bd0d5d4a JM |
6192 | lookup of `%D' in the scope of `%#T' (`%#D') \ |
6193 | does not match lookup in the current scope (`%#D')", | |
6194 | name, got_object, from_obj, val); | |
594740f3 | 6195 | |
b8b1a3c1 JM |
6196 | /* We don't change val to from_obj if got_object depends on |
6197 | template parms because that breaks implicit typename for | |
6198 | destructor calls. */ | |
6199 | if (! uses_template_parms (got_object)) | |
53fdbb3b | 6200 | val = from_obj; |
5566b478 | 6201 | } |
e1cd6e56 | 6202 | |
0c64a9ca JM |
6203 | /* If we have a single function from a using decl, pull it out. */ |
6204 | if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val)) | |
6205 | val = OVL_FUNCTION (val); | |
8d08fdba | 6206 | } |
e1cd6e56 MS |
6207 | else if (from_obj) |
6208 | val = from_obj; | |
8d08fdba MS |
6209 | |
6210 | return val; | |
6211 | } | |
6212 | ||
700f8a87 MS |
6213 | tree |
6214 | lookup_name_nonclass (name) | |
6215 | tree name; | |
6216 | { | |
3e3f722c | 6217 | return lookup_name_real (name, 0, 1, 0); |
700f8a87 MS |
6218 | } |
6219 | ||
2c73f9f5 ML |
6220 | tree |
6221 | lookup_function_nonclass (name, args) | |
6222 | tree name; | |
6223 | tree args; | |
6224 | { | |
6225 | return lookup_arg_dependent (name, lookup_name_nonclass (name), args); | |
6226 | } | |
6227 | ||
3e3f722c ML |
6228 | tree |
6229 | lookup_name_namespace_only (name) | |
6230 | tree name; | |
6231 | { | |
6232 | /* type-or-namespace, nonclass, namespace_only */ | |
6233 | return lookup_name_real (name, 1, 1, 1); | |
6234 | } | |
6235 | ||
700f8a87 MS |
6236 | tree |
6237 | lookup_name (name, prefer_type) | |
6238 | tree name; | |
6239 | int prefer_type; | |
6240 | { | |
3e3f722c | 6241 | return lookup_name_real (name, prefer_type, 0, 0); |
700f8a87 MS |
6242 | } |
6243 | ||
a7d2d407 MM |
6244 | /* Similar to `lookup_name' but look only in the innermost non-class |
6245 | binding level. */ | |
8d08fdba MS |
6246 | |
6247 | tree | |
6248 | lookup_name_current_level (name) | |
6249 | tree name; | |
6250 | { | |
e2500fed | 6251 | struct cp_binding_level *b; |
a7d2d407 | 6252 | tree t = NULL_TREE; |
8d08fdba | 6253 | |
a7d2d407 MM |
6254 | b = current_binding_level; |
6255 | while (b->parm_flag == 2) | |
6256 | b = b->level_chain; | |
6257 | ||
6258 | if (b->namespace_p) | |
8d08fdba | 6259 | { |
5fdaba89 | 6260 | t = IDENTIFIER_NAMESPACE_VALUE (name); |
8d08fdba MS |
6261 | |
6262 | /* extern "C" function() */ | |
6263 | if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST) | |
6264 | t = TREE_VALUE (t); | |
6265 | } | |
68642fb6 | 6266 | else if (IDENTIFIER_BINDING (name) |
f181d4ae | 6267 | && LOCAL_BINDING_P (IDENTIFIER_BINDING (name))) |
8d08fdba | 6268 | { |
a4443a08 MS |
6269 | while (1) |
6270 | { | |
f181d4ae MM |
6271 | if (BINDING_LEVEL (IDENTIFIER_BINDING (name)) == b) |
6272 | return IDENTIFIER_VALUE (name); | |
68642fb6 | 6273 | |
9ed182dc JM |
6274 | if (b->keep == 2) |
6275 | b = b->level_chain; | |
6276 | else | |
6277 | break; | |
6278 | } | |
6279 | } | |
6280 | ||
6281 | return t; | |
6282 | } | |
6283 | ||
6284 | /* Like lookup_name_current_level, but for types. */ | |
6285 | ||
6286 | tree | |
6287 | lookup_type_current_level (name) | |
6288 | tree name; | |
6289 | { | |
6290 | register tree t = NULL_TREE; | |
6291 | ||
6292 | my_friendly_assert (! current_binding_level->namespace_p, 980716); | |
6293 | ||
6294 | if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE | |
6295 | && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node) | |
6296 | { | |
e2500fed | 6297 | struct cp_binding_level *b = current_binding_level; |
9ed182dc JM |
6298 | while (1) |
6299 | { | |
6300 | if (purpose_member (name, b->type_shadowed)) | |
6301 | return REAL_IDENTIFIER_TYPE_VALUE (name); | |
a4443a08 MS |
6302 | if (b->keep == 2) |
6303 | b = b->level_chain; | |
6304 | else | |
6305 | break; | |
6306 | } | |
8d08fdba MS |
6307 | } |
6308 | ||
6309 | return t; | |
6310 | } | |
3e3f722c ML |
6311 | |
6312 | void | |
6313 | begin_only_namespace_names () | |
6314 | { | |
6315 | only_namespace_names = 1; | |
6316 | } | |
6317 | ||
6318 | void | |
6319 | end_only_namespace_names () | |
6320 | { | |
6321 | only_namespace_names = 0; | |
6322 | } | |
8d08fdba | 6323 | \f |
8d08fdba | 6324 | /* Push the declarations of builtin types into the namespace. |
0e5921e8 | 6325 | RID_INDEX is the index of the builtin type |
8d08fdba MS |
6326 | in the array RID_POINTERS. NAME is the name used when looking |
6327 | up the builtin type. TYPE is the _TYPE node for the builtin type. */ | |
6328 | ||
eaa7c03f | 6329 | void |
8d08fdba MS |
6330 | record_builtin_type (rid_index, name, type) |
6331 | enum rid rid_index; | |
d8e178a0 | 6332 | const char *name; |
8d08fdba MS |
6333 | tree type; |
6334 | { | |
6335 | tree rname = NULL_TREE, tname = NULL_TREE; | |
a703fb38 | 6336 | tree tdecl = NULL_TREE; |
8d08fdba | 6337 | |
0e5921e8 | 6338 | if ((int) rid_index < (int) RID_MAX) |
8d08fdba MS |
6339 | rname = ridpointers[(int) rid_index]; |
6340 | if (name) | |
6341 | tname = get_identifier (name); | |
6342 | ||
6343 | TYPE_BUILT_IN (type) = 1; | |
68642fb6 | 6344 | |
8d08fdba MS |
6345 | if (tname) |
6346 | { | |
8d08fdba | 6347 | tdecl = pushdecl (build_decl (TYPE_DECL, tname, type)); |
8d08fdba | 6348 | set_identifier_type_value (tname, NULL_TREE); |
0e5921e8 | 6349 | if ((int) rid_index < (int) RID_MAX) |
a1c65f9f | 6350 | /* Built-in types live in the global namespace. */ |
2c73f9f5 | 6351 | SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl); |
8d08fdba MS |
6352 | } |
6353 | if (rname != NULL_TREE) | |
6354 | { | |
6355 | if (tname != NULL_TREE) | |
6356 | { | |
6357 | set_identifier_type_value (rname, NULL_TREE); | |
2c73f9f5 | 6358 | SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl); |
8d08fdba MS |
6359 | } |
6360 | else | |
6361 | { | |
8d08fdba | 6362 | tdecl = pushdecl (build_decl (TYPE_DECL, rname, type)); |
8d08fdba MS |
6363 | set_identifier_type_value (rname, NULL_TREE); |
6364 | } | |
6365 | } | |
8d08fdba MS |
6366 | } |
6367 | ||
eff71ab0 | 6368 | /* Record one of the standard Java types. |
4d8a1dd6 MM |
6369 | * Declare it as having the given NAME. |
6370 | * If SIZE > 0, it is the size of one of the integral types; | |
6371 | * otherwise it is the negative of the size of one of the other types. */ | |
eff71ab0 PB |
6372 | |
6373 | static tree | |
6374 | record_builtin_java_type (name, size) | |
d8e178a0 | 6375 | const char *name; |
eff71ab0 PB |
6376 | int size; |
6377 | { | |
6378 | tree type, decl; | |
6379 | if (size > 0) | |
6380 | type = make_signed_type (size); | |
6381 | else if (size > -32) | |
a1c65f9f | 6382 | { /* "__java_char" or ""__java_boolean". */ |
eff71ab0 PB |
6383 | type = make_unsigned_type (-size); |
6384 | /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/ | |
6385 | } | |
6386 | else | |
a1c65f9f | 6387 | { /* "__java_float" or ""__java_double". */ |
eff71ab0 PB |
6388 | type = make_node (REAL_TYPE); |
6389 | TYPE_PRECISION (type) = - size; | |
6390 | layout_type (type); | |
6391 | } | |
0e5921e8 | 6392 | record_builtin_type (RID_MAX, name, type); |
eff71ab0 | 6393 | decl = TYPE_NAME (type); |
e229f2cd PB |
6394 | |
6395 | /* Suppress generate debug symbol entries for these types, | |
6396 | since for normal C++ they are just clutter. | |
a1c65f9f | 6397 | However, push_lang_context undoes this if extern "Java" is seen. */ |
eff71ab0 | 6398 | DECL_IGNORED_P (decl) = 1; |
e229f2cd | 6399 | |
eff71ab0 PB |
6400 | TYPE_FOR_JAVA (type) = 1; |
6401 | return type; | |
6402 | } | |
6403 | ||
a1c65f9f | 6404 | /* Push a type into the namespace so that the back-ends ignore it. */ |
036407f7 ML |
6405 | |
6406 | static void | |
6407 | record_unknown_type (type, name) | |
6408 | tree type; | |
d8e178a0 | 6409 | const char *name; |
036407f7 ML |
6410 | { |
6411 | tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type)); | |
6412 | /* Make sure the "unknown type" typedecl gets ignored for debug info. */ | |
6413 | DECL_IGNORED_P (decl) = 1; | |
6414 | TYPE_DECL_SUPPRESS_DEBUG (decl) = 1; | |
6415 | TYPE_SIZE (type) = TYPE_SIZE (void_type_node); | |
6416 | TYPE_ALIGN (type) = 1; | |
11cf4d18 | 6417 | TYPE_USER_ALIGN (type) = 0; |
036407f7 | 6418 | TYPE_MODE (type) = TYPE_MODE (void_type_node); |
68642fb6 | 6419 | } |
036407f7 | 6420 | |
d43829f9 MM |
6421 | /* An string for which we should create an IDENTIFIER_NODE at |
6422 | startup. */ | |
6423 | ||
6424 | typedef struct predefined_identifier | |
6425 | { | |
6426 | /* The name of the identifier. */ | |
8b60264b | 6427 | const char *const name; |
d43829f9 | 6428 | /* The place where the IDENTIFIER_NODE should be stored. */ |
8b60264b | 6429 | tree *const node; |
838dfd8a | 6430 | /* Nonzero if this is the name of a constructor or destructor. */ |
8b60264b | 6431 | const int ctor_or_dtor_p; |
d43829f9 MM |
6432 | } predefined_identifier; |
6433 | ||
6434 | /* Create all the predefined identifiers. */ | |
6435 | ||
6436 | static void | |
5362b086 | 6437 | initialize_predefined_identifiers () |
d43829f9 | 6438 | { |
8b60264b | 6439 | const predefined_identifier *pid; |
d43829f9 MM |
6440 | |
6441 | /* A table of identifiers to create at startup. */ | |
8b60264b | 6442 | static const predefined_identifier predefined_identifiers[] = { |
298d6f60 MM |
6443 | { "C++", &lang_name_cplusplus, 0 }, |
6444 | { "C", &lang_name_c, 0 }, | |
6445 | { "Java", &lang_name_java, 0 }, | |
6446 | { CTOR_NAME, &ctor_identifier, 1 }, | |
6447 | { "__base_ctor", &base_ctor_identifier, 1 }, | |
6448 | { "__comp_ctor", &complete_ctor_identifier, 1 }, | |
6449 | { DTOR_NAME, &dtor_identifier, 1 }, | |
6450 | { "__comp_dtor", &complete_dtor_identifier, 1 }, | |
6451 | { "__base_dtor", &base_dtor_identifier, 1 }, | |
6452 | { "__deleting_dtor", &deleting_dtor_identifier, 1 }, | |
298d6f60 | 6453 | { IN_CHARGE_NAME, &in_charge_identifier, 0 }, |
298d6f60 MM |
6454 | { "nelts", &nelts_identifier, 0 }, |
6455 | { THIS_NAME, &this_identifier, 0 }, | |
c4372ef4 | 6456 | { VTABLE_DELTA_NAME, &delta_identifier, 0 }, |
298d6f60 | 6457 | { VTABLE_PFN_NAME, &pfn_identifier, 0 }, |
298d6f60 | 6458 | { "_vptr", &vptr_identifier, 0 }, |
3ec6bad3 | 6459 | { "__vtt_parm", &vtt_parm_identifier, 0 }, |
1f6e1acc | 6460 | { "std", &std_identifier, 0 }, |
298d6f60 | 6461 | { NULL, NULL, 0 } |
d43829f9 MM |
6462 | }; |
6463 | ||
6464 | for (pid = predefined_identifiers; pid->name; ++pid) | |
298d6f60 MM |
6465 | { |
6466 | *pid->node = get_identifier (pid->name); | |
6467 | if (pid->ctor_or_dtor_p) | |
6468 | IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1; | |
6469 | } | |
d43829f9 MM |
6470 | } |
6471 | ||
8d08fdba MS |
6472 | /* Create the predefined scalar types of C, |
6473 | and some nodes representing standard constants (0, 1, (void *)0). | |
6474 | Initialize the global binding level. | |
6475 | Make definitions for built-in primitive functions. */ | |
6476 | ||
6477 | void | |
f5e99456 | 6478 | cxx_init_decl_processing () |
8d08fdba | 6479 | { |
10841285 MM |
6480 | tree void_ftype; |
6481 | tree void_ftype_ptr; | |
8d08fdba | 6482 | |
d43829f9 MM |
6483 | /* Create all the identifiers we need. */ |
6484 | initialize_predefined_identifiers (); | |
8d08fdba | 6485 | |
8f17b5c5 | 6486 | /* Fill in back-end hooks. */ |
d363e7bf | 6487 | lang_missing_noreturn_ok_p = &cp_missing_noreturn_ok_p; |
99dccabc | 6488 | |
9cd64686 MM |
6489 | /* Create the global variables. */ |
6490 | push_to_top_level (); | |
8012c983 | 6491 | |
a1c65f9f | 6492 | /* Enter the global namespace. */ |
30394414 | 6493 | my_friendly_assert (global_namespace == NULL_TREE, 375); |
30394414 JM |
6494 | push_namespace (get_identifier ("::")); |
6495 | global_namespace = current_namespace; | |
6496 | current_lang_name = NULL_TREE; | |
6497 | ||
bccd95ae | 6498 | /* Adjust various flags based on command-line settings. */ |
2642b9bf JM |
6499 | if (! flag_permissive && ! pedantic) |
6500 | flag_pedantic_errors = 1; | |
bccd95ae | 6501 | if (!flag_no_inline) |
b850de4f MM |
6502 | { |
6503 | flag_inline_trees = 1; | |
6504 | flag_no_inline = 1; | |
6505 | } | |
acc72c37 MM |
6506 | if (flag_inline_functions) |
6507 | { | |
6508 | flag_inline_trees = 2; | |
6509 | flag_inline_functions = 0; | |
6510 | } | |
830fcda8 | 6511 | |
f963b5d9 RS |
6512 | /* Force minimum function alignment if using the least significant |
6513 | bit of function pointers to store the virtual bit. */ | |
6514 | if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn | |
6515 | && force_align_functions_log < 1) | |
6516 | force_align_functions_log = 1; | |
6517 | ||
8d08fdba MS |
6518 | /* Initially, C. */ |
6519 | current_lang_name = lang_name_c; | |
6520 | ||
6521 | current_function_decl = NULL_TREE; | |
8d08fdba MS |
6522 | current_binding_level = NULL_BINDING_LEVEL; |
6523 | free_binding_level = NULL_BINDING_LEVEL; | |
6524 | ||
81b3411c BS |
6525 | build_common_tree_nodes (flag_signed_char); |
6526 | ||
8d08fdba MS |
6527 | error_mark_list = build_tree_list (error_mark_node, error_mark_node); |
6528 | TREE_TYPE (error_mark_list) = error_mark_node; | |
6529 | ||
a28e3c7f MS |
6530 | /* Make the binding_level structure for global names. */ |
6531 | pushlevel (0); | |
8d08fdba | 6532 | global_binding_level = current_binding_level; |
2c73f9f5 ML |
6533 | /* The global level is the namespace level of ::. */ |
6534 | NAMESPACE_LEVEL (global_namespace) = global_binding_level; | |
6535 | declare_namespace_level (); | |
8d08fdba | 6536 | |
6bcedb4e | 6537 | /* Create the `std' namespace. */ |
1dbb6023 NS |
6538 | push_namespace (std_identifier); |
6539 | std_node = current_namespace; | |
6540 | pop_namespace (); | |
5362b086 | 6541 | |
eaa7c03f | 6542 | c_common_nodes_and_builtins (); |
37c46b43 | 6543 | |
4d8a1dd6 MM |
6544 | java_byte_type_node = record_builtin_java_type ("__java_byte", 8); |
6545 | java_short_type_node = record_builtin_java_type ("__java_short", 16); | |
6546 | java_int_type_node = record_builtin_java_type ("__java_int", 32); | |
6547 | java_long_type_node = record_builtin_java_type ("__java_long", 64); | |
6548 | java_float_type_node = record_builtin_java_type ("__java_float", -32); | |
6549 | java_double_type_node = record_builtin_java_type ("__java_double", -64); | |
6550 | java_char_type_node = record_builtin_java_type ("__java_char", -16); | |
6551 | java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1); | |
eff71ab0 | 6552 | |
8d08fdba MS |
6553 | integer_two_node = build_int_2 (2, 0); |
6554 | TREE_TYPE (integer_two_node) = integer_type_node; | |
6555 | integer_three_node = build_int_2 (3, 0); | |
6556 | TREE_TYPE (integer_three_node) = integer_type_node; | |
8d08fdba | 6557 | |
255512c1 JM |
6558 | boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE); |
6559 | TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE); | |
c1ea6a0b BK |
6560 | TYPE_MAX_VALUE (boolean_type_node) = build_int_2 (1, 0); |
6561 | TREE_TYPE (TYPE_MAX_VALUE (boolean_type_node)) = boolean_type_node; | |
6562 | TYPE_PRECISION (boolean_type_node) = 1; | |
255512c1 JM |
6563 | record_builtin_type (RID_BOOL, "bool", boolean_type_node); |
6564 | boolean_false_node = build_int_2 (0, 0); | |
6565 | TREE_TYPE (boolean_false_node) = boolean_type_node; | |
6566 | boolean_true_node = build_int_2 (1, 0); | |
6567 | TREE_TYPE (boolean_true_node) = boolean_type_node; | |
2986ae00 | 6568 | |
4cc1d462 | 6569 | empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE); |
5fd8e536 JM |
6570 | |
6571 | #if 0 | |
3e411c3f | 6572 | record_builtin_type (RID_MAX, NULL, string_type_node); |
5fd8e536 JM |
6573 | #endif |
6574 | ||
1f84ec23 MM |
6575 | delta_type_node = ptrdiff_type_node; |
6576 | vtable_index_type = ptrdiff_type_node; | |
c7e266a6 | 6577 | |
3ec6bad3 | 6578 | vtt_parm_type = build_pointer_type (const_ptr_type_node); |
10841285 MM |
6579 | void_ftype = build_function_type (void_type_node, void_list_node); |
6580 | void_ftype_ptr = build_function_type (void_type_node, | |
6581 | tree_cons (NULL_TREE, | |
6582 | ptr_type_node, | |
6583 | void_list_node)); | |
824b9a4c | 6584 | void_ftype_ptr |
4cc1d462 | 6585 | = build_exception_variant (void_ftype_ptr, empty_except_spec); |
8d08fdba | 6586 | |
8d08fdba MS |
6587 | /* C++ extensions */ |
6588 | ||
6589 | unknown_type_node = make_node (UNKNOWN_TYPE); | |
036407f7 ML |
6590 | record_unknown_type (unknown_type_node, "unknown type"); |
6591 | ||
8d08fdba MS |
6592 | /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */ |
6593 | TREE_TYPE (unknown_type_node) = unknown_type_node; | |
a6967cc0 | 6594 | |
a6967cc0 JM |
6595 | /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same |
6596 | result. */ | |
8d08fdba MS |
6597 | TYPE_POINTER_TO (unknown_type_node) = unknown_type_node; |
6598 | TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node; | |
6599 | ||
c4372ef4 NS |
6600 | { |
6601 | /* Make sure we get a unique function type, so we can give | |
6602 | its pointer type a name. (This wins for gdb.) */ | |
6603 | tree vfunc_type = make_node (FUNCTION_TYPE); | |
6604 | TREE_TYPE (vfunc_type) = integer_type_node; | |
6605 | TYPE_ARG_TYPES (vfunc_type) = NULL_TREE; | |
6606 | layout_type (vfunc_type); | |
6607 | ||
6608 | vtable_entry_type = build_pointer_type (vfunc_type); | |
6609 | } | |
0e5921e8 | 6610 | record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type); |
8d08fdba | 6611 | |
8d08fdba | 6612 | vtbl_type_node |
52bf7d5d | 6613 | = build_cplus_array_type (vtable_entry_type, NULL_TREE); |
8d08fdba | 6614 | layout_type (vtbl_type_node); |
91063b51 | 6615 | vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST); |
3e411c3f | 6616 | record_builtin_type (RID_MAX, NULL, vtbl_type_node); |
849da744 MM |
6617 | vtbl_ptr_type_node = build_pointer_type (vtable_entry_type); |
6618 | layout_type (vtbl_ptr_type_node); | |
3e411c3f | 6619 | record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node); |
8d08fdba | 6620 | |
1f84ec23 MM |
6621 | push_namespace (get_identifier ("__cxxabiv1")); |
6622 | abi_node = current_namespace; | |
6623 | pop_namespace (); | |
6633d636 | 6624 | |
2c73f9f5 | 6625 | global_type_node = make_node (LANG_TYPE); |
036407f7 | 6626 | record_unknown_type (global_type_node, "global type"); |
2c73f9f5 | 6627 | |
db5ae43f MS |
6628 | /* Now, C++. */ |
6629 | current_lang_name = lang_name_cplusplus; | |
8d08fdba | 6630 | |
ced78d8b | 6631 | { |
2c73f9f5 | 6632 | tree bad_alloc_type_node, newtype, deltype; |
10841285 MM |
6633 | tree ptr_ftype_sizetype; |
6634 | ||
1dbb6023 | 6635 | push_namespace (std_identifier); |
88e5899c MM |
6636 | bad_alloc_type_node |
6637 | = xref_tag (class_type, get_identifier ("bad_alloc"), | |
6638 | /*attributes=*/NULL_TREE, 1); | |
1dbb6023 | 6639 | pop_namespace (); |
10841285 MM |
6640 | ptr_ftype_sizetype |
6641 | = build_function_type (ptr_type_node, | |
6642 | tree_cons (NULL_TREE, | |
6643 | c_size_type_node, | |
6644 | void_list_node)); | |
2c73f9f5 | 6645 | newtype = build_exception_variant |
0ba8a114 NS |
6646 | (ptr_ftype_sizetype, add_exception_specifier |
6647 | (NULL_TREE, bad_alloc_type_node, -1)); | |
4cc1d462 | 6648 | deltype = build_exception_variant (void_ftype_ptr, empty_except_spec); |
596ea4e5 AS |
6649 | push_cp_library_fn (NEW_EXPR, newtype); |
6650 | push_cp_library_fn (VEC_NEW_EXPR, newtype); | |
6651 | global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype); | |
6652 | push_cp_library_fn (VEC_DELETE_EXPR, deltype); | |
ced78d8b | 6653 | } |
8d08fdba MS |
6654 | |
6655 | abort_fndecl | |
1f84ec23 | 6656 | = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype); |
8d08fdba | 6657 | |
8d08fdba MS |
6658 | /* Perform other language dependent initializations. */ |
6659 | init_class_processing (); | |
8d08fdba | 6660 | init_search_processing (); |
db48b831 | 6661 | init_rtti_processing (); |
8d08fdba | 6662 | |
6467930b | 6663 | if (flag_exceptions) |
8d2733ca | 6664 | init_exception_processing (); |
9e9ff709 | 6665 | |
7fcdf4c2 | 6666 | if (! supports_one_only ()) |
72b7eeff | 6667 | flag_weak = 0; |
8d08fdba | 6668 | |
2ce07e2d | 6669 | make_fname_decl = cp_make_fname_decl; |
0ba8a114 | 6670 | start_fname_decls (); |
8d08fdba | 6671 | |
e9a25f70 JL |
6672 | /* Show we use EH for cleanups. */ |
6673 | using_eh_for_cleanups (); | |
62c154ed | 6674 | |
d9cf7c82 JM |
6675 | /* Maintain consistency. Perhaps we should just complain if they |
6676 | say -fwritable-strings? */ | |
6677 | if (flag_writable_strings) | |
6678 | flag_const_strings = 0; | |
62c154ed JM |
6679 | } |
6680 | ||
0ba8a114 NS |
6681 | /* Generate an initializer for a function naming variable from |
6682 | NAME. NAME may be NULL, in which case we generate a special | |
a1c65f9f | 6683 | ERROR_MARK node which should be replaced later. */ |
0ba8a114 NS |
6684 | |
6685 | tree | |
6686 | cp_fname_init (name) | |
6687 | const char *name; | |
6688 | { | |
6689 | tree domain = NULL_TREE; | |
6690 | tree type; | |
6691 | tree init = NULL_TREE; | |
6692 | size_t length = 0; | |
6693 | ||
6694 | if (name) | |
6695 | { | |
6696 | length = strlen (name); | |
6697 | domain = build_index_type (size_int (length)); | |
6698 | init = build_string (length + 1, name); | |
6699 | } | |
6700 | ||
6701 | type = build_qualified_type (char_type_node, TYPE_QUAL_CONST); | |
6702 | type = build_cplus_array_type (type, domain); | |
6703 | ||
6704 | if (init) | |
6705 | TREE_TYPE (init) = type; | |
6706 | else | |
6707 | /* We don't know the value until instantiation time. Make | |
a1c65f9f | 6708 | something which will be digested now, but replaced later. */ |
0ba8a114 NS |
6709 | init = build (ERROR_MARK, type); |
6710 | ||
6711 | return init; | |
6712 | } | |
6713 | ||
2ce07e2d NS |
6714 | /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the |
6715 | decl, NAME is the initialization string and TYPE_DEP indicates whether | |
6716 | NAME depended on the type of the function. We make use of that to detect | |
0ba8a114 NS |
6717 | __PRETTY_FUNCTION__ inside a template fn. This is being done |
6718 | lazily at the point of first use, so we musn't push the decl now. */ | |
2ce07e2d NS |
6719 | |
6720 | static tree | |
0ba8a114 | 6721 | cp_make_fname_decl (id, type_dep) |
2ce07e2d | 6722 | tree id; |
2ce07e2d NS |
6723 | int type_dep; |
6724 | { | |
83182544 | 6725 | const char *const name = (type_dep && processing_template_decl |
0ba8a114 NS |
6726 | ? NULL : fname_as_string (type_dep)); |
6727 | tree init = cp_fname_init (name); | |
6728 | tree decl = build_decl (VAR_DECL, id, TREE_TYPE (init)); | |
2ce07e2d | 6729 | |
a1c65f9f | 6730 | /* As we don't push the decl here, we must set the context. */ |
0ba8a114 NS |
6731 | DECL_CONTEXT (decl) = current_function_decl; |
6732 | DECL_PRETTY_FUNCTION_P (decl) = type_dep; | |
6733 | ||
2ce07e2d NS |
6734 | TREE_STATIC (decl) = 1; |
6735 | TREE_READONLY (decl) = 1; | |
2ce07e2d | 6736 | DECL_ARTIFICIAL (decl) = 1; |
2ce07e2d | 6737 | DECL_INITIAL (decl) = init; |
0ba8a114 NS |
6738 | |
6739 | TREE_USED (decl) = 1; | |
5362b086 | 6740 | |
0ba8a114 NS |
6741 | cp_finish_decl (decl, init, NULL_TREE, LOOKUP_ONLYCONVERTING); |
6742 | ||
2ce07e2d NS |
6743 | return decl; |
6744 | } | |
6745 | ||
d52e4867 RS |
6746 | /* Make a definition for a builtin function named NAME in the current |
6747 | namespace, whose data type is TYPE and whose context is CONTEXT. | |
6748 | TYPE should be a function type with argument types. | |
8d08fdba | 6749 | |
0c11ada6 JM |
6750 | CLASS and CODE tell later passes how to compile calls to this function. |
6751 | See tree.h for possible values. | |
6752 | ||
6753 | If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME, | |
6a2dd09a RS |
6754 | the name to be called if we can't opencode the function. |
6755 | If ATTRS is nonzero, use that for the function's attribute | |
6756 | list. */ | |
8d08fdba | 6757 | |
d52e4867 | 6758 | static tree |
6a2dd09a | 6759 | builtin_function_1 (name, type, context, code, class, libname, attrs) |
d8e178a0 | 6760 | const char *name; |
8d08fdba | 6761 | tree type; |
d52e4867 | 6762 | tree context; |
0c11ada6 JM |
6763 | int code; |
6764 | enum built_in_class class; | |
6765 | const char *libname; | |
6a2dd09a | 6766 | tree attrs; |
8d08fdba | 6767 | { |
596ea4e5 | 6768 | tree decl = build_library_fn_1 (get_identifier (name), ERROR_MARK, type); |
0c11ada6 JM |
6769 | DECL_BUILT_IN_CLASS (decl) = class; |
6770 | DECL_FUNCTION_CODE (decl) = code; | |
d52e4867 | 6771 | DECL_CONTEXT (decl) = context; |
12a22e76 | 6772 | |
6bcedb4e | 6773 | pushdecl (decl); |
6bcedb4e | 6774 | |
8d08fdba MS |
6775 | /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME, |
6776 | we cannot change DECL_ASSEMBLER_NAME until we have installed this | |
6777 | function in the namespace. */ | |
0c11ada6 | 6778 | if (libname) |
92643fea MM |
6779 | SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname)); |
6780 | make_decl_rtl (decl, NULL); | |
935d1834 ZW |
6781 | |
6782 | /* Warn if a function in the namespace for users | |
6783 | is used without an occasion to consider it declared. */ | |
6784 | if (name[0] != '_' || name[1] != '_') | |
6785 | DECL_ANTICIPATED (decl) = 1; | |
6786 | ||
6431177a | 6787 | /* Possibly apply some default attributes to this built-in function. */ |
6a2dd09a RS |
6788 | if (attrs) |
6789 | decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN); | |
6790 | else | |
6791 | decl_attributes (&decl, NULL_TREE, 0); | |
6431177a | 6792 | |
8d08fdba MS |
6793 | return decl; |
6794 | } | |
7f4edbcb | 6795 | |
d52e4867 RS |
6796 | /* Entry point for the benefit of c_common_nodes_and_builtins. |
6797 | ||
6798 | Make a defintion for a builtin function named NAME and whose data type | |
6799 | is TYPE. TYPE should be a function type with argument types. This | |
6800 | function places the anticipated declaration in the global namespace | |
6801 | and additionally in the std namespace if appropriate. | |
6802 | ||
6803 | CLASS and CODE tell later passes how to compile calls to this function. | |
6804 | See tree.h for possible values. | |
6805 | ||
6806 | If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME, | |
6a2dd09a RS |
6807 | the name to be called if we can't opencode the function. |
6808 | ||
6809 | If ATTRS is nonzero, use that for the function's attribute | |
6810 | list. */ | |
d52e4867 RS |
6811 | |
6812 | tree | |
6a2dd09a | 6813 | builtin_function (name, type, code, class, libname, attrs) |
d52e4867 RS |
6814 | const char *name; |
6815 | tree type; | |
6816 | int code; | |
6817 | enum built_in_class class; | |
6818 | const char *libname; | |
6a2dd09a | 6819 | tree attrs; |
d52e4867 RS |
6820 | { |
6821 | /* All builtins that don't begin with an '_' should additionally | |
6822 | go in the 'std' namespace. */ | |
6823 | if (name[0] != '_') | |
6824 | { | |
6825 | push_namespace (std_identifier); | |
6a2dd09a | 6826 | builtin_function_1 (name, type, std_node, code, class, libname, attrs); |
d52e4867 RS |
6827 | pop_namespace (); |
6828 | } | |
6829 | ||
6a2dd09a RS |
6830 | return builtin_function_1 (name, type, NULL_TREE, code, |
6831 | class, libname, attrs); | |
d52e4867 RS |
6832 | } |
6833 | ||
0c11ada6 JM |
6834 | /* Generate a FUNCTION_DECL with the typical flags for a runtime library |
6835 | function. Not called directly. */ | |
6836 | ||
6837 | static tree | |
596ea4e5 | 6838 | build_library_fn_1 (name, operator_code, type) |
0c11ada6 | 6839 | tree name; |
596ea4e5 | 6840 | enum tree_code operator_code; |
0c11ada6 JM |
6841 | tree type; |
6842 | { | |
6843 | tree fn = build_lang_decl (FUNCTION_DECL, name, type); | |
6844 | DECL_EXTERNAL (fn) = 1; | |
6845 | TREE_PUBLIC (fn) = 1; | |
6846 | DECL_ARTIFICIAL (fn) = 1; | |
6847 | TREE_NOTHROW (fn) = 1; | |
596ea4e5 | 6848 | SET_OVERLOADED_OPERATOR_CODE (fn, operator_code); |
5d2ed28c | 6849 | SET_DECL_LANGUAGE (fn, lang_c); |
0c11ada6 JM |
6850 | return fn; |
6851 | } | |
c2a37c55 | 6852 | |
0c11ada6 JM |
6853 | /* Returns the _DECL for a library function with C linkage. |
6854 | We assume that such functions never throw; if this is incorrect, | |
6855 | callers should unset TREE_NOTHROW. */ | |
c2a37c55 | 6856 | |
7f4edbcb | 6857 | tree |
0c11ada6 JM |
6858 | build_library_fn (name, type) |
6859 | tree name; | |
6860 | tree type; | |
6861 | { | |
19e7881c | 6862 | return build_library_fn_1 (name, ERROR_MARK, type); |
0c11ada6 JM |
6863 | } |
6864 | ||
6865 | /* Returns the _DECL for a library function with C++ linkage. */ | |
6866 | ||
596ea4e5 AS |
6867 | static tree |
6868 | build_cp_library_fn (name, operator_code, type) | |
0c11ada6 | 6869 | tree name; |
596ea4e5 | 6870 | enum tree_code operator_code; |
0c11ada6 JM |
6871 | tree type; |
6872 | { | |
596ea4e5 | 6873 | tree fn = build_library_fn_1 (name, operator_code, type); |
0c11ada6 | 6874 | TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type); |
6bbf1598 | 6875 | DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace); |
5d2ed28c | 6876 | SET_DECL_LANGUAGE (fn, lang_cplusplus); |
0c11ada6 | 6877 | set_mangled_name_for_decl (fn); |
0c11ada6 JM |
6878 | return fn; |
6879 | } | |
6880 | ||
6881 | /* Like build_library_fn, but takes a C string instead of an | |
6882 | IDENTIFIER_NODE. */ | |
6883 | ||
6884 | tree | |
6885 | build_library_fn_ptr (name, type) | |
7f4edbcb BS |
6886 | const char *name; |
6887 | tree type; | |
7f4edbcb | 6888 | { |
0c11ada6 JM |
6889 | return build_library_fn (get_identifier (name), type); |
6890 | } | |
6891 | ||
6892 | /* Like build_cp_library_fn, but takes a C string instead of an | |
6893 | IDENTIFIER_NODE. */ | |
6894 | ||
6895 | tree | |
6896 | build_cp_library_fn_ptr (name, type) | |
6897 | const char *name; | |
6898 | tree type; | |
6899 | { | |
596ea4e5 | 6900 | return build_cp_library_fn (get_identifier (name), ERROR_MARK, type); |
0c11ada6 JM |
6901 | } |
6902 | ||
6903 | /* Like build_library_fn, but also pushes the function so that we will | |
6904 | be able to find it via IDENTIFIER_GLOBAL_VALUE. */ | |
6905 | ||
6906 | tree | |
6907 | push_library_fn (name, type) | |
6908 | tree name, type; | |
6909 | { | |
6910 | tree fn = build_library_fn (name, type); | |
6911 | pushdecl_top_level (fn); | |
6912 | return fn; | |
6913 | } | |
6914 | ||
6915 | /* Like build_cp_library_fn, but also pushes the function so that it | |
6916 | will be found by normal lookup. */ | |
6917 | ||
596ea4e5 AS |
6918 | static tree |
6919 | push_cp_library_fn (operator_code, type) | |
6920 | enum tree_code operator_code; | |
0c11ada6 JM |
6921 | tree type; |
6922 | { | |
5362b086 | 6923 | tree fn = build_cp_library_fn (ansi_opname (operator_code), |
596ea4e5 AS |
6924 | operator_code, |
6925 | type); | |
0c11ada6 JM |
6926 | pushdecl (fn); |
6927 | return fn; | |
6928 | } | |
6929 | ||
6930 | /* Like push_library_fn, but takes a TREE_LIST of parm types rather than | |
6931 | a FUNCTION_TYPE. */ | |
6932 | ||
6933 | tree | |
6934 | push_void_library_fn (name, parmtypes) | |
6935 | tree name, parmtypes; | |
6936 | { | |
6937 | tree type = build_function_type (void_type_node, parmtypes); | |
6938 | return push_library_fn (name, type); | |
6939 | } | |
6940 | ||
cf74fb86 | 6941 | /* Like push_library_fn, but also note that this function throws |
0c11ada6 JM |
6942 | and does not return. Used for __throw_foo and the like. */ |
6943 | ||
6944 | tree | |
cf74fb86 JM |
6945 | push_throw_library_fn (name, type) |
6946 | tree name, type; | |
0c11ada6 | 6947 | { |
cf74fb86 | 6948 | tree fn = push_library_fn (name, type); |
0c11ada6 JM |
6949 | TREE_THIS_VOLATILE (fn) = 1; |
6950 | TREE_NOTHROW (fn) = 0; | |
6951 | return fn; | |
7f4edbcb | 6952 | } |
6431177a JM |
6953 | |
6954 | /* Apply default attributes to a function, if a system function with default | |
6955 | attributes. */ | |
6956 | ||
6957 | void | |
7ffb4fd2 | 6958 | cxx_insert_default_attributes (decl) |
6431177a JM |
6959 | tree decl; |
6960 | { | |
6961 | if (!DECL_EXTERN_C_FUNCTION_P (decl)) | |
6962 | return; | |
6963 | if (!TREE_PUBLIC (decl)) | |
6964 | return; | |
6965 | c_common_insert_default_attributes (decl); | |
6966 | } | |
8d08fdba | 6967 | \f |
61a127b3 MM |
6968 | /* When we call finish_struct for an anonymous union, we create |
6969 | default copy constructors and such. But, an anonymous union | |
6970 | shouldn't have such things; this function undoes the damage to the | |
6971 | anonymous union type T. | |
6972 | ||
6973 | (The reason that we create the synthesized methods is that we don't | |
6974 | distinguish `union { int i; }' from `typedef union { int i; } U'. | |
6975 | The first is an anonymous union; the second is just an ordinary | |
6976 | union type.) */ | |
6977 | ||
6978 | void | |
6bdb8141 | 6979 | fixup_anonymous_aggr (t) |
61a127b3 MM |
6980 | tree t; |
6981 | { | |
6982 | tree *q; | |
6983 | ||
6984 | /* Wipe out memory of synthesized methods */ | |
6985 | TYPE_HAS_CONSTRUCTOR (t) = 0; | |
6986 | TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0; | |
6987 | TYPE_HAS_INIT_REF (t) = 0; | |
6988 | TYPE_HAS_CONST_INIT_REF (t) = 0; | |
6989 | TYPE_HAS_ASSIGN_REF (t) = 0; | |
61a127b3 MM |
6990 | TYPE_HAS_CONST_ASSIGN_REF (t) = 0; |
6991 | ||
6992 | /* Splice the implicitly generated functions out of the TYPE_METHODS | |
6993 | list. */ | |
6994 | q = &TYPE_METHODS (t); | |
6995 | while (*q) | |
6996 | { | |
6997 | if (DECL_ARTIFICIAL (*q)) | |
6998 | *q = TREE_CHAIN (*q); | |
6999 | else | |
7000 | q = &TREE_CHAIN (*q); | |
7001 | } | |
7002 | ||
cab1f180 | 7003 | /* ISO C++ 9.5.3. Anonymous unions may not have function members. */ |
61a127b3 | 7004 | if (TYPE_METHODS (t)) |
1f0d71c5 | 7005 | cp_error_at ("an anonymous union cannot have function members", t); |
a1c2b86d JJ |
7006 | |
7007 | /* Anonymous aggregates cannot have fields with ctors, dtors or complex | |
7008 | assignment operators (because they cannot have these methods themselves). | |
7009 | For anonymous unions this is already checked because they are not allowed | |
7010 | in any union, otherwise we have to check it. */ | |
7011 | if (TREE_CODE (t) != UNION_TYPE) | |
7012 | { | |
7013 | tree field, type; | |
7014 | ||
7015 | for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field)) | |
7016 | if (TREE_CODE (field) == FIELD_DECL) | |
7017 | { | |
7018 | type = TREE_TYPE (field); | |
7019 | if (CLASS_TYPE_P (type)) | |
7020 | { | |
7021 | if (TYPE_NEEDS_CONSTRUCTING (type)) | |
7022 | cp_error_at ("member %#D' with constructor not allowed in anonymous aggregate", | |
7023 | field); | |
7024 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) | |
7025 | cp_error_at ("member %#D' with destructor not allowed in anonymous aggregate", | |
7026 | field); | |
7027 | if (TYPE_HAS_COMPLEX_ASSIGN_REF (type)) | |
7028 | cp_error_at ("member %#D' with copy assignment operator not allowed in anonymous aggregate", | |
7029 | field); | |
7030 | } | |
7031 | } | |
7032 | } | |
61a127b3 MM |
7033 | } |
7034 | ||
72a93143 JM |
7035 | /* Make sure that a declaration with no declarator is well-formed, i.e. |
7036 | just defines a tagged type or anonymous union. | |
8d08fdba | 7037 | |
72a93143 | 7038 | Returns the type defined, if any. */ |
8d08fdba | 7039 | |
72a93143 JM |
7040 | tree |
7041 | check_tag_decl (declspecs) | |
8d08fdba MS |
7042 | tree declspecs; |
7043 | { | |
72a93143 | 7044 | int found_type = 0; |
2bdb0643 | 7045 | int saw_friend = 0; |
e8186ecf | 7046 | int saw_typedef = 0; |
2986ae00 | 7047 | tree ob_modifier = NULL_TREE; |
8d08fdba | 7048 | register tree link; |
8d08fdba MS |
7049 | register tree t = NULL_TREE; |
7050 | ||
7051 | for (link = declspecs; link; link = TREE_CHAIN (link)) | |
7052 | { | |
7053 | register tree value = TREE_VALUE (link); | |
7054 | ||
2bdb0643 | 7055 | if (TYPE_P (value) |
bd0d5d4a | 7056 | || TREE_CODE (value) == TYPE_DECL |
2bdb0643 JM |
7057 | || (TREE_CODE (value) == IDENTIFIER_NODE |
7058 | && IDENTIFIER_GLOBAL_VALUE (value) | |
bd0d5d4a | 7059 | && TREE_CODE (IDENTIFIER_GLOBAL_VALUE (value)) == TYPE_DECL)) |
8d08fdba | 7060 | { |
72a93143 | 7061 | ++found_type; |
5566b478 | 7062 | |
bd0d5d4a JM |
7063 | if (found_type == 2 && TREE_CODE (value) == IDENTIFIER_NODE) |
7064 | { | |
7065 | if (! in_system_header) | |
33bd39a2 | 7066 | pedwarn ("redeclaration of C++ built-in type `%T'", value); |
bd0d5d4a JM |
7067 | return NULL_TREE; |
7068 | } | |
7069 | ||
7070 | if (TYPE_P (value) | |
7071 | && ((TREE_CODE (value) != TYPENAME_TYPE && IS_AGGR_TYPE (value)) | |
7072 | || TREE_CODE (value) == ENUMERAL_TYPE)) | |
72a93143 JM |
7073 | { |
7074 | my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261); | |
7075 | t = value; | |
7076 | } | |
8d08fdba | 7077 | } |
e8186ecf GDR |
7078 | else if (value == ridpointers[(int) RID_TYPEDEF]) |
7079 | saw_typedef = 1; | |
83f660b7 JM |
7080 | else if (value == ridpointers[(int) RID_FRIEND]) |
7081 | { | |
83f660b7 JM |
7082 | if (current_class_type == NULL_TREE |
7083 | || current_scope () != current_class_type) | |
7084 | ob_modifier = value; | |
2bdb0643 JM |
7085 | else |
7086 | saw_friend = 1; | |
83f660b7 | 7087 | } |
8d08fdba | 7088 | else if (value == ridpointers[(int) RID_STATIC] |
2986ae00 MS |
7089 | || value == ridpointers[(int) RID_EXTERN] |
7090 | || value == ridpointers[(int) RID_AUTO] | |
28cbf42c MS |
7091 | || value == ridpointers[(int) RID_REGISTER] |
7092 | || value == ridpointers[(int) RID_INLINE] | |
7093 | || value == ridpointers[(int) RID_VIRTUAL] | |
72a93143 JM |
7094 | || value == ridpointers[(int) RID_CONST] |
7095 | || value == ridpointers[(int) RID_VOLATILE] | |
7a1f3f5f RH |
7096 | || value == ridpointers[(int) RID_EXPLICIT] |
7097 | || value == ridpointers[(int) RID_THREAD]) | |
2986ae00 | 7098 | ob_modifier = value; |
8d08fdba MS |
7099 | } |
7100 | ||
72a93143 JM |
7101 | if (found_type > 1) |
7102 | error ("multiple types in one declaration"); | |
7e2067ca | 7103 | |
2bdb0643 | 7104 | if (t == NULL_TREE && ! saw_friend) |
7e2067ca | 7105 | pedwarn ("declaration does not declare anything"); |
0dd3962d | 7106 | |
1951a1b6 | 7107 | /* Check for an anonymous union. */ |
6bdb8141 | 7108 | else if (t && IS_AGGR_TYPE_CODE (TREE_CODE (t)) |
1951a1b6 | 7109 | && TYPE_ANONYMOUS_P (t)) |
0dd3962d | 7110 | { |
e8186ecf GDR |
7111 | /* 7/3 In a simple-declaration, the optional init-declarator-list |
7112 | can be omitted only when declaring a class (clause 9) or | |
7113 | enumeration (7.2), that is, when the decl-specifier-seq contains | |
7114 | either a class-specifier, an elaborated-type-specifier with | |
7115 | a class-key (9.1), or an enum-specifier. In these cases and | |
7116 | whenever a class-specifier or enum-specifier is present in the | |
7117 | decl-specifier-seq, the identifiers in these specifiers are among | |
7118 | the names being declared by the declaration (as class-name, | |
7119 | enum-names, or enumerators, depending on the syntax). In such | |
7120 | cases, and except for the declaration of an unnamed bit-field (9.6), | |
7121 | the decl-specifier-seq shall introduce one or more names into the | |
7122 | program, or shall redeclare a name introduced by a previous | |
7123 | declaration. [Example: | |
7124 | enum { }; // ill-formed | |
7125 | typedef class { }; // ill-formed | |
7126 | --end example] */ | |
7127 | if (saw_typedef) | |
7128 | { | |
357351e5 | 7129 | error ("missing type-name in typedef-declaration"); |
e8186ecf GDR |
7130 | return NULL_TREE; |
7131 | } | |
0dd3962d | 7132 | /* Anonymous unions are objects, so they can have specifiers. */; |
6bdb8141 JM |
7133 | SET_ANON_AGGR_TYPE_P (t); |
7134 | ||
7135 | if (TREE_CODE (t) != UNION_TYPE && pedantic && ! in_system_header) | |
7136 | pedwarn ("ISO C++ prohibits anonymous structs"); | |
0dd3962d JM |
7137 | } |
7138 | ||
83f660b7 | 7139 | else if (ob_modifier) |
8d08fdba | 7140 | { |
83f660b7 JM |
7141 | if (ob_modifier == ridpointers[(int) RID_INLINE] |
7142 | || ob_modifier == ridpointers[(int) RID_VIRTUAL]) | |
33bd39a2 | 7143 | error ("`%D' can only be specified for functions", ob_modifier); |
83f660b7 | 7144 | else if (ob_modifier == ridpointers[(int) RID_FRIEND]) |
33bd39a2 | 7145 | error ("`%D' can only be specified inside a class", ob_modifier); |
83f660b7 | 7146 | else if (ob_modifier == ridpointers[(int) RID_EXPLICIT]) |
33bd39a2 | 7147 | error ("`%D' can only be specified for constructors", |
83f660b7 JM |
7148 | ob_modifier); |
7149 | else | |
33bd39a2 | 7150 | error ("`%D' can only be specified for objects and functions", |
83f660b7 | 7151 | ob_modifier); |
72a93143 | 7152 | } |
8d08fdba | 7153 | |
72a93143 JM |
7154 | return t; |
7155 | } | |
7156 | ||
7157 | /* Called when a declaration is seen that contains no names to declare. | |
7158 | If its type is a reference to a structure, union or enum inherited | |
7159 | from a containing scope, shadow that tag name for the current scope | |
7160 | with a forward reference. | |
7161 | If its type defines a new named structure or union | |
7162 | or defines an enum, it is valid but we need not do anything here. | |
7163 | Otherwise, it is an error. | |
7164 | ||
7165 | C++: may have to grok the declspecs to learn about static, | |
7166 | complain for anonymous unions. */ | |
7167 | ||
7168 | void | |
7169 | shadow_tag (declspecs) | |
7170 | tree declspecs; | |
7171 | { | |
7172 | tree t = check_tag_decl (declspecs); | |
7173 | ||
7174 | if (t) | |
7175 | maybe_process_partial_specialization (t); | |
7176 | ||
7177 | /* This is where the variables in an anonymous union are | |
7178 | declared. An anonymous union declaration looks like: | |
7179 | union { ... } ; | |
7180 | because there is no declarator after the union, the parser | |
7181 | sends that declaration here. */ | |
6bdb8141 | 7182 | if (t && ANON_AGGR_TYPE_P (t)) |
72a93143 | 7183 | { |
6bdb8141 | 7184 | fixup_anonymous_aggr (t); |
72a93143 JM |
7185 | |
7186 | if (TYPE_FIELDS (t)) | |
7187 | { | |
7188 | tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0, | |
91d231cb | 7189 | NULL); |
72a93143 JM |
7190 | finish_anon_union (decl); |
7191 | } | |
8d08fdba MS |
7192 | } |
7193 | } | |
7194 | \f | |
7195 | /* Decode a "typename", such as "int **", returning a ..._TYPE node. */ | |
7196 | ||
7197 | tree | |
7198 | groktypename (typename) | |
7199 | tree typename; | |
7200 | { | |
98884b26 JM |
7201 | tree specs, attrs; |
7202 | tree type; | |
8d08fdba MS |
7203 | if (TREE_CODE (typename) != TREE_LIST) |
7204 | return typename; | |
98884b26 JM |
7205 | split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs); |
7206 | type = grokdeclarator (TREE_VALUE (typename), specs, | |
7207 | TYPENAME, 0, &attrs); | |
7208 | if (attrs) | |
7209 | cplus_decl_attributes (&type, attrs, 0); | |
7210 | return type; | |
8d08fdba MS |
7211 | } |
7212 | ||
7213 | /* Decode a declarator in an ordinary declaration or data definition. | |
7214 | This is called as soon as the type information and variable name | |
7215 | have been parsed, before parsing the initializer if any. | |
7216 | Here we create the ..._DECL node, fill in its type, | |
7217 | and put it on the list of decls for the current context. | |
7218 | The ..._DECL node is returned as the value. | |
7219 | ||
7220 | Exception: for arrays where the length is not specified, | |
82580166 | 7221 | the type is left null, to be filled in by `cp_finish_decl'. |
8d08fdba MS |
7222 | |
7223 | Function definitions do not come here; they go to start_function | |
7224 | instead. However, external and forward declarations of functions | |
7225 | do go through here. Structure field declarations are done by | |
7226 | grokfield and not through here. */ | |
7227 | ||
8d08fdba | 7228 | tree |
a1774733 | 7229 | start_decl (declarator, declspecs, initialized, attributes, prefix_attributes) |
8d08fdba MS |
7230 | tree declarator, declspecs; |
7231 | int initialized; | |
a1774733 | 7232 | tree attributes, prefix_attributes; |
8d08fdba | 7233 | { |
59387d2e | 7234 | tree decl; |
8d08fdba MS |
7235 | register tree type, tem; |
7236 | tree context; | |
8d08fdba | 7237 | |
5566b478 MS |
7238 | #if 0 |
7239 | /* See code below that used this. */ | |
8d08fdba | 7240 | int init_written = initialized; |
5566b478 | 7241 | #endif |
8d08fdba | 7242 | |
e92cc029 | 7243 | /* This should only be done once on the top most decl. */ |
594bb0e7 | 7244 | if (have_extern_spec) |
8d08fdba | 7245 | { |
1f8f4a0b MM |
7246 | declspecs = tree_cons (NULL_TREE, get_identifier ("extern"), |
7247 | declspecs); | |
594bb0e7 | 7248 | have_extern_spec = false; |
8d08fdba MS |
7249 | } |
7250 | ||
e23bd218 IR |
7251 | /* An object declared as __attribute__((deprecated)) suppresses |
7252 | warnings of uses of other deprecated items. */ | |
7253 | if (lookup_attribute ("deprecated", attributes)) | |
7254 | deprecated_state = DEPRECATED_SUPPRESS; | |
7255 | ||
91d231cb | 7256 | attributes = chainon (attributes, prefix_attributes); |
b17e2870 | 7257 | |
c11b6f21 | 7258 | decl = grokdeclarator (declarator, declspecs, NORMAL, initialized, |
91d231cb | 7259 | &attributes); |
68642fb6 | 7260 | |
e23bd218 IR |
7261 | deprecated_state = DEPRECATED_NORMAL; |
7262 | ||
a1774733 | 7263 | if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE) |
8d08fdba MS |
7264 | return NULL_TREE; |
7265 | ||
7266 | type = TREE_TYPE (decl); | |
7267 | ||
44689c12 ML |
7268 | if (type == error_mark_node) |
7269 | return NULL_TREE; | |
7270 | ||
4f1c5b7d | 7271 | context = DECL_CONTEXT (decl); |
8d08fdba | 7272 | |
9a68c51f JM |
7273 | if (initialized && context && TREE_CODE (context) == NAMESPACE_DECL |
7274 | && context != current_namespace && TREE_CODE (decl) == VAR_DECL) | |
7275 | { | |
7276 | /* When parsing the initializer, lookup should use the object's | |
a1c65f9f | 7277 | namespace. */ |
9a68c51f JM |
7278 | push_decl_namespace (context); |
7279 | } | |
7280 | ||
a1c65f9f | 7281 | /* We are only interested in class contexts, later. */ |
2c73f9f5 ML |
7282 | if (context && TREE_CODE (context) == NAMESPACE_DECL) |
7283 | context = NULL_TREE; | |
7284 | ||
8d08fdba MS |
7285 | if (initialized) |
7286 | /* Is it valid for this decl to have an initializer at all? | |
7287 | If not, set INITIALIZED to zero, which will indirectly | |
82580166 | 7288 | tell `cp_finish_decl' to ignore the initializer once it is parsed. */ |
8d08fdba MS |
7289 | switch (TREE_CODE (decl)) |
7290 | { | |
7291 | case TYPE_DECL: | |
95f79357 ZW |
7292 | error ("typedef `%D' is initialized", decl); |
7293 | initialized = 0; | |
8d08fdba MS |
7294 | break; |
7295 | ||
7296 | case FUNCTION_DECL: | |
33bd39a2 | 7297 | error ("function `%#D' is initialized like a variable", decl); |
8d08fdba MS |
7298 | initialized = 0; |
7299 | break; | |
7300 | ||
7301 | default: | |
3e41d13b | 7302 | break; |
8d08fdba MS |
7303 | } |
7304 | ||
8d08fdba MS |
7305 | if (initialized) |
7306 | { | |
a9aedbc2 | 7307 | if (! toplevel_bindings_p () |
8d08fdba | 7308 | && DECL_EXTERNAL (decl)) |
33bd39a2 | 7309 | warning ("declaration of `%#D' has `extern' and is initialized", |
8d08fdba MS |
7310 | decl); |
7311 | DECL_EXTERNAL (decl) = 0; | |
5566b478 | 7312 | if (toplevel_bindings_p ()) |
8d08fdba MS |
7313 | TREE_STATIC (decl) = 1; |
7314 | ||
7315 | /* Tell `pushdecl' this is an initialized decl | |
7316 | even though we don't yet have the initializer expression. | |
82580166 | 7317 | Also tell `cp_finish_decl' it may store the real initializer. */ |
8d08fdba MS |
7318 | DECL_INITIAL (decl) = error_mark_node; |
7319 | } | |
7320 | ||
fa20888b | 7321 | /* Set attributes here so if duplicate decl, will have proper attributes. */ |
91d231cb | 7322 | cplus_decl_attributes (&decl, attributes, 0); |
fa20888b | 7323 | |
ecb0eece RH |
7324 | /* If #pragma weak was used, mark the decl weak now. */ |
7325 | if (current_binding_level == global_binding_level) | |
7326 | maybe_apply_pragma_weak (decl); | |
7327 | ||
97055d5c AO |
7328 | if (TREE_CODE (decl) == FUNCTION_DECL |
7329 | && DECL_DECLARED_INLINE_P (decl) | |
7330 | && DECL_UNINLINABLE (decl) | |
7331 | && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl))) | |
7332 | warning_with_decl (decl, | |
7333 | "inline function `%s' given attribute noinline"); | |
7334 | ||
d0f062fb | 7335 | if (context && COMPLETE_TYPE_P (complete_type (context))) |
5b605f68 | 7336 | { |
6b400b21 | 7337 | push_nested_class (context, 2); |
e97e5263 | 7338 | |
5b605f68 MS |
7339 | if (TREE_CODE (decl) == VAR_DECL) |
7340 | { | |
7341 | tree field = lookup_field (context, DECL_NAME (decl), 0, 0); | |
7342 | if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL) | |
33bd39a2 | 7343 | error ("`%#D' is not a static member of `%#T'", decl, context); |
e349ee73 MS |
7344 | else |
7345 | { | |
7346 | if (DECL_CONTEXT (field) != context) | |
f2d773a2 | 7347 | { |
33bd39a2 | 7348 | pedwarn ("ISO C++ does not permit `%T::%D' to be defined as `%T::%D'", |
f2d773a2 JM |
7349 | DECL_CONTEXT (field), DECL_NAME (decl), |
7350 | context, DECL_NAME (decl)); | |
7351 | DECL_CONTEXT (decl) = DECL_CONTEXT (field); | |
7352 | } | |
75650646 MM |
7353 | /* Static data member are tricky; an in-class initialization |
7354 | still doesn't provide a definition, so the in-class | |
7355 | declaration will have DECL_EXTERNAL set, but will have an | |
7356 | initialization. Thus, duplicate_decls won't warn | |
7357 | about this situation, and so we check here. */ | |
7358 | if (DECL_INITIAL (decl) && DECL_INITIAL (field)) | |
33bd39a2 | 7359 | error ("duplicate initialization of %D", decl); |
e349ee73 MS |
7360 | if (duplicate_decls (decl, field)) |
7361 | decl = field; | |
7362 | } | |
5b605f68 | 7363 | } |
f30432d7 MS |
7364 | else |
7365 | { | |
5566b478 | 7366 | tree field = check_classfn (context, decl); |
f30432d7 MS |
7367 | if (field && duplicate_decls (decl, field)) |
7368 | decl = field; | |
7369 | } | |
7370 | ||
7371 | /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */ | |
dff3e828 | 7372 | DECL_IN_AGGR_P (decl) = 0; |
68642fb6 | 7373 | if ((DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl)) |
1f6e1acc | 7374 | || CLASSTYPE_TEMPLATE_INSTANTIATION (context)) |
84e6233f JM |
7375 | { |
7376 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
7377 | /* [temp.expl.spec] An explicit specialization of a static data | |
7378 | member of a template is a definition if the declaration | |
7379 | includes an initializer; otherwise, it is a declaration. | |
7380 | ||
7381 | We check for processing_specialization so this only applies | |
7382 | to the new specialization syntax. */ | |
7383 | if (DECL_INITIAL (decl) == NULL_TREE && processing_specialization) | |
7384 | DECL_EXTERNAL (decl) = 1; | |
7385 | } | |
f30432d7 | 7386 | |
b7698cf0 | 7387 | if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)) |
33bd39a2 | 7388 | pedwarn ("declaration of `%#D' outside of class is not definition", |
f30432d7 | 7389 | decl); |
5b605f68 MS |
7390 | } |
7391 | ||
9188c363 MM |
7392 | /* Enter this declaration into the symbol table. */ |
7393 | tem = maybe_push_decl (decl); | |
2ee887f2 | 7394 | |
5156628f | 7395 | if (processing_template_decl) |
cd9f6678 | 7396 | tem = push_template_decl (tem); |
5566b478 | 7397 | |
2ee887f2 | 7398 | #if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS) |
a3203465 | 7399 | /* Tell the back-end to use or not use .common as appropriate. If we say |
a50f0918 MS |
7400 | -fconserve-space, we want this to save .data space, at the expense of |
7401 | wrong semantics. If we say -fno-conserve-space, we want this to | |
7402 | produce errors about redefs; to do this we force variables into the | |
7403 | data segment. */ | |
a3203465 | 7404 | DECL_COMMON (tem) = flag_conserve_space || ! TREE_PUBLIC (tem); |
2ee887f2 | 7405 | #endif |
68642fb6 | 7406 | |
5156628f | 7407 | if (! processing_template_decl) |
5566b478 | 7408 | start_decl_1 (tem); |
8d08fdba | 7409 | |
8d08fdba MS |
7410 | return tem; |
7411 | } | |
7412 | ||
5566b478 MS |
7413 | void |
7414 | start_decl_1 (decl) | |
7415 | tree decl; | |
8d08fdba | 7416 | { |
5566b478 MS |
7417 | tree type = TREE_TYPE (decl); |
7418 | int initialized = (DECL_INITIAL (decl) != NULL_TREE); | |
8d08fdba | 7419 | |
44689c12 ML |
7420 | if (type == error_mark_node) |
7421 | return; | |
7422 | ||
bd0d5d4a | 7423 | maybe_push_cleanup_level (type); |
5566b478 MS |
7424 | |
7425 | if (initialized) | |
7426 | /* Is it valid for this decl to have an initializer at all? | |
7427 | If not, set INITIALIZED to zero, which will indirectly | |
7428 | tell `cp_finish_decl' to ignore the initializer once it is parsed. */ | |
8d08fdba | 7429 | { |
5566b478 MS |
7430 | /* Don't allow initializations for incomplete types except for |
7431 | arrays which might be completed by the initialization. */ | |
d0f062fb | 7432 | if (COMPLETE_TYPE_P (complete_type (type))) |
5566b478 MS |
7433 | ; /* A complete type is ok. */ |
7434 | else if (TREE_CODE (type) != ARRAY_TYPE) | |
8d08fdba | 7435 | { |
33bd39a2 | 7436 | error ("variable `%#D' has initializer but incomplete type", |
5566b478 MS |
7437 | decl); |
7438 | initialized = 0; | |
25eb19ff | 7439 | type = TREE_TYPE (decl) = error_mark_node; |
5566b478 | 7440 | } |
d0f062fb | 7441 | else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type)))) |
5566b478 MS |
7442 | { |
7443 | if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)) | |
33bd39a2 | 7444 | error ("elements of array `%#D' have incomplete type", decl); |
5566b478 MS |
7445 | /* else we already gave an error in start_decl. */ |
7446 | initialized = 0; | |
8d08fdba | 7447 | } |
8d08fdba MS |
7448 | } |
7449 | ||
5566b478 MS |
7450 | if (!initialized |
7451 | && TREE_CODE (decl) != TYPE_DECL | |
7452 | && TREE_CODE (decl) != TEMPLATE_DECL | |
07c88314 | 7453 | && type != error_mark_node |
5362b086 | 7454 | && IS_AGGR_TYPE (type) |
07c88314 | 7455 | && ! DECL_EXTERNAL (decl)) |
8d08fdba | 7456 | { |
5156628f | 7457 | if ((! processing_template_decl || ! uses_template_parms (type)) |
d0f062fb | 7458 | && !COMPLETE_TYPE_P (complete_type (type))) |
5566b478 | 7459 | { |
0154eaa8 | 7460 | error ("aggregate `%#D' has incomplete type and cannot be defined", |
5566b478 MS |
7461 | decl); |
7462 | /* Change the type so that assemble_variable will give | |
7463 | DECL an rtl we can live with: (mem (const_int 0)). */ | |
25eb19ff | 7464 | type = TREE_TYPE (decl) = error_mark_node; |
5566b478 MS |
7465 | } |
7466 | else | |
7467 | { | |
7468 | /* If any base type in the hierarchy of TYPE needs a constructor, | |
7469 | then we set initialized to 1. This way any nodes which are | |
7470 | created for the purposes of initializing this aggregate | |
7471 | will live as long as it does. This is necessary for global | |
7472 | aggregates which do not have their initializers processed until | |
7473 | the end of the file. */ | |
7474 | initialized = TYPE_NEEDS_CONSTRUCTING (type); | |
7475 | } | |
7476 | } | |
7477 | ||
5566b478 MS |
7478 | if (! initialized) |
7479 | DECL_INITIAL (decl) = NULL_TREE; | |
7480 | } | |
7481 | ||
7482 | /* Handle initialization of references. | |
38e01259 | 7483 | These three arguments are from `cp_finish_decl', and have the |
e92cc029 MS |
7484 | same meaning here that they do there. |
7485 | ||
7486 | Quotes on semantics can be found in ARM 8.4.3. */ | |
7487 | ||
8e4ce833 | 7488 | static tree |
a703fb38 | 7489 | grok_reference_init (decl, type, init) |
5566b478 | 7490 | tree decl, type, init; |
5566b478 MS |
7491 | { |
7492 | tree tmp; | |
7493 | ||
7494 | if (init == NULL_TREE) | |
7495 | { | |
7496 | if ((DECL_LANG_SPECIFIC (decl) == 0 | |
7497 | || DECL_IN_AGGR_P (decl) == 0) | |
7498 | && ! DECL_THIS_EXTERN (decl)) | |
33bd39a2 | 7499 | error ("`%D' declared as reference but not initialized", decl); |
8e4ce833 | 7500 | return NULL_TREE; |
5566b478 MS |
7501 | } |
7502 | ||
7503 | if (init == error_mark_node) | |
8e4ce833 | 7504 | return NULL_TREE; |
5566b478 | 7505 | |
ed5511d9 | 7506 | if (TREE_CODE (init) == CONSTRUCTOR) |
5566b478 | 7507 | { |
33bd39a2 | 7508 | error ("ISO C++ forbids use of initializer list to initialize reference `%D'", decl); |
8e4ce833 | 7509 | return NULL_TREE; |
8d08fdba MS |
7510 | } |
7511 | ||
7512 | if (TREE_CODE (init) == TREE_LIST) | |
7513 | init = build_compound_expr (init); | |
8d08fdba | 7514 | |
8ccc31eb MS |
7515 | if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE) |
7516 | init = convert_from_reference (init); | |
7517 | ||
8d08fdba MS |
7518 | if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE |
7519 | && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE) | |
7520 | { | |
a3203465 | 7521 | /* Note: default conversion is only called in very special cases. */ |
8d08fdba MS |
7522 | init = default_conversion (init); |
7523 | } | |
68642fb6 | 7524 | |
24bef158 MM |
7525 | /* Convert INIT to the reference type TYPE. This may involve the |
7526 | creation of a temporary, whose lifetime must be the same as that | |
7527 | of the reference. If so, a DECL_STMT for the temporary will be | |
7528 | added just after the DECL_STMT for DECL. That's why we don't set | |
7529 | DECL_INITIAL for local references (instead assigning to them | |
7530 | explicitly); we need to allow the temporary to be initialized | |
7531 | first. */ | |
a3203465 | 7532 | tmp = convert_to_reference |
9a3b49ac | 7533 | (type, init, CONV_IMPLICIT, |
ce7715bd JM |
7534 | LOOKUP_ONLYCONVERTING|LOOKUP_SPECULATIVELY|LOOKUP_NORMAL|DIRECT_BIND, |
7535 | decl); | |
8d08fdba | 7536 | |
a3203465 | 7537 | if (tmp == error_mark_node) |
8e4ce833 JJ |
7538 | return NULL_TREE; |
7539 | else if (tmp == NULL_TREE) | |
8d08fdba | 7540 | { |
33bd39a2 | 7541 | error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init)); |
8e4ce833 | 7542 | return NULL_TREE; |
8d08fdba | 7543 | } |
8d08fdba | 7544 | |
8e4ce833 JJ |
7545 | if (TREE_STATIC (decl) && !TREE_CONSTANT (tmp)) |
7546 | return tmp; | |
7547 | ||
08ac397c | 7548 | DECL_INITIAL (decl) = tmp; |
8e4ce833 JJ |
7549 | |
7550 | return NULL_TREE; | |
8d08fdba MS |
7551 | } |
7552 | ||
27778b73 MM |
7553 | /* When parsing `int a[] = {1, 2};' we don't know the size of the |
7554 | array until we finish parsing the initializer. If that's the | |
7555 | situation we're in, update DECL accordingly. */ | |
7556 | ||
7557 | static void | |
7558 | maybe_deduce_size_from_array_init (decl, init) | |
7559 | tree decl; | |
7560 | tree init; | |
7561 | { | |
7562 | tree type = TREE_TYPE (decl); | |
7563 | ||
7564 | if (TREE_CODE (type) == ARRAY_TYPE | |
7565 | && TYPE_DOMAIN (type) == NULL_TREE | |
7566 | && TREE_CODE (decl) != TYPE_DECL) | |
7567 | { | |
f2ae0c45 JM |
7568 | /* do_default is really a C-ism to deal with tentative definitions. |
7569 | But let's leave it here to ease the eventual merge. */ | |
7570 | int do_default = !DECL_EXTERNAL (decl); | |
27778b73 MM |
7571 | tree initializer = init ? init : DECL_INITIAL (decl); |
7572 | int failure = complete_array_type (type, initializer, do_default); | |
7573 | ||
7574 | if (failure == 1) | |
33bd39a2 | 7575 | error ("initializer fails to determine size of `%D'", decl); |
27778b73 MM |
7576 | |
7577 | if (failure == 2) | |
7578 | { | |
7579 | if (do_default) | |
33bd39a2 | 7580 | error ("array size missing in `%D'", decl); |
27778b73 MM |
7581 | /* If a `static' var's size isn't known, make it extern as |
7582 | well as static, so it does not get allocated. If it's not | |
7583 | `static', then don't mark it extern; finish_incomplete_decl | |
7584 | will give it a default size and it will get allocated. */ | |
7585 | else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl)) | |
7586 | DECL_EXTERNAL (decl) = 1; | |
7587 | } | |
7588 | ||
7589 | if (pedantic && TYPE_DOMAIN (type) != NULL_TREE | |
7590 | && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)), | |
7591 | integer_zero_node)) | |
33bd39a2 | 7592 | error ("zero-size array `%D'", decl); |
27778b73 MM |
7593 | |
7594 | layout_decl (decl, 0); | |
7595 | } | |
7596 | } | |
7597 | ||
7598 | /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue | |
57b52417 | 7599 | any appropriate error messages regarding the layout. */ |
27778b73 | 7600 | |
57b52417 MM |
7601 | static void |
7602 | layout_var_decl (decl) | |
27778b73 | 7603 | tree decl; |
27778b73 | 7604 | { |
57b52417 | 7605 | tree type = TREE_TYPE (decl); |
c95cd22e | 7606 | #if 0 |
57b52417 | 7607 | tree ttype = target_type (type); |
c95cd22e | 7608 | #endif |
57b52417 MM |
7609 | |
7610 | /* If we haven't already layed out this declaration, do so now. | |
7611 | Note that we must not call complete type for an external object | |
7612 | because it's type might involve templates that we are not | |
0e339752 | 7613 | supposed to isntantiate yet. (And it's perfectly valid to say |
57b52417 MM |
7614 | `extern X x' for some incomplete type `X'.) */ |
7615 | if (!DECL_EXTERNAL (decl)) | |
7616 | complete_type (type); | |
d0f062fb | 7617 | if (!DECL_SIZE (decl) && COMPLETE_TYPE_P (type)) |
27778b73 MM |
7618 | layout_decl (decl, 0); |
7619 | ||
c82dbd95 | 7620 | if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE) |
27778b73 MM |
7621 | { |
7622 | /* An automatic variable with an incomplete type: that is an error. | |
7623 | Don't talk about array types here, since we took care of that | |
7624 | message in grokdeclarator. */ | |
33bd39a2 | 7625 | error ("storage size of `%D' isn't known", decl); |
27778b73 MM |
7626 | TREE_TYPE (decl) = error_mark_node; |
7627 | } | |
ae673f14 JM |
7628 | #if 0 |
7629 | /* Keep this code around in case we later want to control debug info | |
7630 | based on whether a type is "used". (jason 1999-11-11) */ | |
7631 | ||
27778b73 MM |
7632 | else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype)) |
7633 | /* Let debugger know it should output info for this type. */ | |
7634 | note_debug_info_needed (ttype); | |
7635 | ||
7636 | if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl)) | |
7637 | note_debug_info_needed (DECL_CONTEXT (decl)); | |
ae673f14 | 7638 | #endif |
27778b73 MM |
7639 | |
7640 | if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl)) | |
7641 | && DECL_SIZE (decl) != NULL_TREE | |
7642 | && ! TREE_CONSTANT (DECL_SIZE (decl))) | |
7643 | { | |
7644 | if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST) | |
7645 | constant_expression_warning (DECL_SIZE (decl)); | |
7646 | else | |
33bd39a2 | 7647 | error ("storage size of `%D' isn't constant", decl); |
27778b73 | 7648 | } |
8e4ce833 JJ |
7649 | |
7650 | if (TREE_STATIC (decl) | |
7651 | && !DECL_ARTIFICIAL (decl) | |
7652 | && current_function_decl | |
7653 | && DECL_CONTEXT (decl) == current_function_decl) | |
7654 | push_local_name (decl); | |
27778b73 MM |
7655 | } |
7656 | ||
27778b73 MM |
7657 | /* If a local static variable is declared in an inline function, or if |
7658 | we have a weak definition, we must endeavor to create only one | |
7659 | instance of the variable at link-time. */ | |
7660 | ||
7661 | static void | |
7662 | maybe_commonize_var (decl) | |
7663 | tree decl; | |
7664 | { | |
7665 | /* Static data in a function with comdat linkage also has comdat | |
7666 | linkage. */ | |
7667 | if (TREE_STATIC (decl) | |
7668 | /* Don't mess with __FUNCTION__. */ | |
cf74fb86 | 7669 | && ! DECL_ARTIFICIAL (decl) |
27778b73 MM |
7670 | && current_function_decl |
7671 | && DECL_CONTEXT (decl) == current_function_decl | |
79065db2 | 7672 | && (DECL_DECLARED_INLINE_P (current_function_decl) |
27778b73 MM |
7673 | || DECL_TEMPLATE_INSTANTIATION (current_function_decl)) |
7674 | && TREE_PUBLIC (current_function_decl)) | |
7675 | { | |
27778b73 MM |
7676 | /* If flag_weak, we don't need to mess with this, as we can just |
7677 | make the function weak, and let it refer to its unique local | |
7678 | copy. This works because we don't allow the function to be | |
7679 | inlined. */ | |
7680 | if (! flag_weak) | |
7681 | { | |
7682 | if (DECL_INTERFACE_KNOWN (current_function_decl)) | |
7683 | { | |
7684 | TREE_PUBLIC (decl) = 1; | |
7685 | DECL_EXTERNAL (decl) = DECL_EXTERNAL (current_function_decl); | |
7686 | } | |
7687 | else if (DECL_INITIAL (decl) == NULL_TREE | |
7688 | || DECL_INITIAL (decl) == error_mark_node) | |
7689 | { | |
7690 | TREE_PUBLIC (decl) = 1; | |
7691 | DECL_COMMON (decl) = 1; | |
7692 | } | |
7693 | /* else we lose. We can only do this if we can use common, | |
7694 | which we can't if it has been initialized. */ | |
7695 | ||
92643fea | 7696 | if (!TREE_PUBLIC (decl)) |
27778b73 MM |
7697 | { |
7698 | cp_warning_at ("sorry: semantics of inline function static data `%#D' are wrong (you'll wind up with multiple copies)", decl); | |
7699 | cp_warning_at (" you can work around this by removing the initializer", decl); | |
7700 | } | |
7701 | } | |
8e4ce833 JJ |
7702 | else |
7703 | comdat_linkage (decl); | |
27778b73 MM |
7704 | } |
7705 | else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl)) | |
7706 | /* Set it up again; we might have set DECL_INITIAL since the last | |
7707 | time. */ | |
7708 | comdat_linkage (decl); | |
7709 | } | |
7710 | ||
91063b51 MM |
7711 | /* Issue an error message if DECL is an uninitialized const variable. */ |
7712 | ||
7713 | static void | |
7714 | check_for_uninitialized_const_var (decl) | |
7715 | tree decl; | |
7716 | { | |
7717 | tree type = TREE_TYPE (decl); | |
7718 | ||
7719 | /* ``Unless explicitly declared extern, a const object does not have | |
7720 | external linkage and must be initialized. ($8.4; $12.1)'' ARM | |
7721 | 7.1.6 */ | |
7722 | if (TREE_CODE (decl) == VAR_DECL | |
7723 | && TREE_CODE (type) != REFERENCE_TYPE | |
7724 | && CP_TYPE_CONST_P (type) | |
7725 | && !TYPE_NEEDS_CONSTRUCTING (type) | |
7726 | && !DECL_INITIAL (decl)) | |
33bd39a2 | 7727 | error ("uninitialized const `%D'", decl); |
91063b51 MM |
7728 | } |
7729 | ||
c82dbd95 MM |
7730 | /* Verify INIT (the initializer for DECL), and record the |
7731 | initialization in DECL_INITIAL, if appropriate. Returns a new | |
7732 | value for INIT. */ | |
27778b73 | 7733 | |
c82dbd95 MM |
7734 | static tree |
7735 | check_initializer (decl, init) | |
27778b73 | 7736 | tree decl; |
c82dbd95 | 7737 | tree init; |
27778b73 | 7738 | { |
17bbb839 | 7739 | tree type = TREE_TYPE (decl); |
fc0e7bf5 | 7740 | |
27778b73 MM |
7741 | /* If `start_decl' didn't like having an initialization, ignore it now. */ |
7742 | if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE) | |
7743 | init = NULL_TREE; | |
27778b73 | 7744 | |
17bbb839 MM |
7745 | /* If an initializer is present, DECL_INITIAL has been |
7746 | error_mark_node, to indicate that an as-of-yet unevaluated | |
7747 | initialization will occur. From now on, DECL_INITIAL reflects | |
7748 | the static initialization -- if any -- of DECL. */ | |
7749 | DECL_INITIAL (decl) = NULL_TREE; | |
7750 | ||
c82dbd95 | 7751 | /* Check the initializer. */ |
27778b73 MM |
7752 | if (init) |
7753 | { | |
c82dbd95 MM |
7754 | /* Things that are going to be initialized need to have complete |
7755 | type. */ | |
7756 | TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl)); | |
7757 | ||
7758 | if (type == error_mark_node) | |
7759 | /* We will have already complained. */ | |
7760 | init = NULL_TREE; | |
d0f062fb | 7761 | else if (COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type))) |
27778b73 | 7762 | { |
33bd39a2 | 7763 | error ("variable-sized object `%D' may not be initialized", decl); |
27778b73 MM |
7764 | init = NULL_TREE; |
7765 | } | |
c82dbd95 | 7766 | else if (TREE_CODE (type) == ARRAY_TYPE |
fe5b6c1c | 7767 | && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (type)))) |
27778b73 | 7768 | { |
33bd39a2 | 7769 | error ("elements of array `%#D' have incomplete type", decl); |
27778b73 MM |
7770 | init = NULL_TREE; |
7771 | } | |
fe5b6c1c | 7772 | else if (TREE_CODE (type) != ARRAY_TYPE && !COMPLETE_TYPE_P (type)) |
c82dbd95 | 7773 | { |
33bd39a2 | 7774 | error ("`%D' has incomplete type", decl); |
c82dbd95 MM |
7775 | TREE_TYPE (decl) = error_mark_node; |
7776 | init = NULL_TREE; | |
7777 | } | |
27778b73 MM |
7778 | } |
7779 | ||
7780 | if (TREE_CODE (decl) == CONST_DECL) | |
7781 | { | |
7782 | my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148); | |
7783 | ||
7784 | DECL_INITIAL (decl) = init; | |
7785 | ||
27778b73 MM |
7786 | my_friendly_assert (init != NULL_TREE, 149); |
7787 | init = NULL_TREE; | |
7788 | } | |
c82dbd95 | 7789 | else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE) |
17bbb839 | 7790 | init = grok_reference_init (decl, type, init); |
27778b73 MM |
7791 | else if (init) |
7792 | { | |
7793 | if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type)) | |
7794 | { | |
7795 | if (TREE_CODE (type) == ARRAY_TYPE) | |
7796 | init = digest_init (type, init, (tree *) 0); | |
7797 | else if (TREE_CODE (init) == CONSTRUCTOR | |
7798 | && TREE_HAS_CONSTRUCTOR (init)) | |
7799 | { | |
7800 | if (TYPE_NON_AGGREGATE_CLASS (type)) | |
7801 | { | |
33bd39a2 | 7802 | error ("`%D' must be initialized by constructor, not by `{...}'", |
27778b73 MM |
7803 | decl); |
7804 | init = error_mark_node; | |
7805 | } | |
7806 | else | |
7807 | goto dont_use_constructor; | |
7808 | } | |
7809 | } | |
7810 | else | |
7811 | { | |
7812 | dont_use_constructor: | |
7813 | if (TREE_CODE (init) != TREE_VEC) | |
7814 | init = store_init_value (decl, init); | |
7815 | } | |
27778b73 MM |
7816 | } |
7817 | else if (DECL_EXTERNAL (decl)) | |
7818 | ; | |
2f939d94 | 7819 | else if (TYPE_P (type) |
27778b73 MM |
7820 | && (IS_AGGR_TYPE (type) || TYPE_NEEDS_CONSTRUCTING (type))) |
7821 | { | |
7822 | tree core_type = strip_array_types (type); | |
7823 | ||
7824 | if (! TYPE_NEEDS_CONSTRUCTING (core_type)) | |
7825 | { | |
7826 | if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)) | |
33bd39a2 | 7827 | error ("structure `%D' with uninitialized const members", decl); |
27778b73 | 7828 | if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)) |
33bd39a2 | 7829 | error ("structure `%D' with uninitialized reference members", |
27778b73 MM |
7830 | decl); |
7831 | } | |
7832 | ||
7833 | check_for_uninitialized_const_var (decl); | |
27778b73 MM |
7834 | } |
7835 | else | |
7836 | check_for_uninitialized_const_var (decl); | |
68642fb6 | 7837 | |
c82dbd95 | 7838 | return init; |
27778b73 MM |
7839 | } |
7840 | ||
7841 | /* If DECL is not a local variable, give it RTL. */ | |
7842 | ||
7843 | static void | |
7844 | make_rtl_for_nonlocal_decl (decl, init, asmspec) | |
7845 | tree decl; | |
7846 | tree init; | |
7847 | const char *asmspec; | |
7848 | { | |
95ee998c MM |
7849 | int toplev = toplevel_bindings_p (); |
7850 | int defer_p; | |
27778b73 | 7851 | |
f39ee884 MM |
7852 | /* Handle non-variables up front. */ |
7853 | if (TREE_CODE (decl) != VAR_DECL) | |
7854 | { | |
7855 | rest_of_decl_compilation (decl, asmspec, toplev, at_eof); | |
7856 | return; | |
7857 | } | |
7858 | ||
95ee998c MM |
7859 | /* If we see a class member here, it should be a static data |
7860 | member. */ | |
7861 | if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl)) | |
7862 | { | |
7863 | my_friendly_assert (TREE_STATIC (decl), 19990828); | |
7864 | /* An in-class declaration of a static data member should be | |
7865 | external; it is only a declaration, and not a definition. */ | |
7866 | if (init == NULL_TREE) | |
7867 | my_friendly_assert (DECL_EXTERNAL (decl), 20000723); | |
7868 | } | |
7869 | ||
f39ee884 MM |
7870 | /* Set the DECL_ASSEMBLER_NAME for the variable. */ |
7871 | if (asmspec) | |
7ed47c04 MM |
7872 | { |
7873 | SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec)); | |
7874 | /* The `register' keyword, when used together with an | |
7875 | asm-specification, indicates that the variable should be | |
7876 | placed in a particular register. */ | |
7877 | if (DECL_REGISTER (decl)) | |
7878 | DECL_C_HARD_REGISTER (decl) = 1; | |
7879 | } | |
f39ee884 | 7880 | |
95ee998c MM |
7881 | /* We don't create any RTL for local variables. */ |
7882 | if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl)) | |
7883 | return; | |
27778b73 | 7884 | |
95ee998c MM |
7885 | /* We defer emission of local statics until the corresponding |
7886 | DECL_STMT is expanded. */ | |
7887 | defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl); | |
7888 | ||
b602511f JM |
7889 | /* We try to defer namespace-scope static constants so that they are |
7890 | not emitted into the object file unnecessarily. */ | |
7891 | if (!DECL_VIRTUAL_P (decl) | |
7892 | && TREE_READONLY (decl) | |
7893 | && DECL_INITIAL (decl) != NULL_TREE | |
7894 | && DECL_INITIAL (decl) != error_mark_node | |
7895 | && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)) | |
7896 | && toplev | |
7897 | && !TREE_PUBLIC (decl)) | |
5cc90635 JM |
7898 | { |
7899 | /* Fool with the linkage of static consts according to #pragma | |
7900 | interface. */ | |
7901 | if (!interface_unknown && !TREE_PUBLIC (decl)) | |
27778b73 | 7902 | { |
95ee998c MM |
7903 | TREE_PUBLIC (decl) = 1; |
7904 | DECL_EXTERNAL (decl) = interface_only; | |
27778b73 | 7905 | } |
27778b73 | 7906 | |
95ee998c | 7907 | defer_p = 1; |
27778b73 | 7908 | } |
b602511f JM |
7909 | /* Likewise for template instantiations. */ |
7910 | else if (DECL_COMDAT (decl)) | |
7911 | defer_p = 1; | |
95ee998c | 7912 | |
92643fea MM |
7913 | /* If we're deferring the variable, we only need to make RTL if |
7914 | there's an ASMSPEC. Otherwise, we'll lazily create it later when | |
7915 | we need it. (There's no way to lazily create RTL for things that | |
7916 | have assembly specs because the information about the specifier | |
7917 | isn't stored in the tree, yet) */ | |
7918 | if (defer_p && asmspec) | |
6c418184 | 7919 | make_decl_rtl (decl, asmspec); |
95ee998c | 7920 | /* If we're not deferring, go ahead and assemble the variable. */ |
92643fea | 7921 | else if (!defer_p) |
27778b73 | 7922 | rest_of_decl_compilation (decl, asmspec, toplev, at_eof); |
27778b73 MM |
7923 | } |
7924 | ||
7925 | /* The old ARM scoping rules injected variables declared in the | |
7926 | initialization statement of a for-statement into the surrounding | |
7927 | scope. We support this usage, in order to be backward-compatible. | |
7928 | DECL is a just-declared VAR_DECL; if necessary inject its | |
7929 | declaration into the surrounding scope. */ | |
7930 | ||
b7b8bcd2 | 7931 | void |
27778b73 MM |
7932 | maybe_inject_for_scope_var (decl) |
7933 | tree decl; | |
7934 | { | |
c3783399 NS |
7935 | if (!DECL_NAME (decl)) |
7936 | return; | |
5362b086 | 7937 | |
27778b73 MM |
7938 | if (current_binding_level->is_for_scope) |
7939 | { | |
e2500fed | 7940 | struct cp_binding_level *outer |
27778b73 MM |
7941 | = current_binding_level->level_chain; |
7942 | ||
7943 | /* Check to see if the same name is already bound at the outer | |
7944 | level, either because it was directly declared, or because a | |
7945 | dead for-decl got preserved. In either case, the code would | |
7946 | not have been valid under the ARM scope rules, so clear | |
7947 | is_for_scope for the current_binding_level. | |
7948 | ||
7949 | Otherwise, we need to preserve the temp slot for decl to last | |
7950 | into the outer binding level. */ | |
7951 | ||
68642fb6 | 7952 | tree outer_binding |
27778b73 | 7953 | = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (decl))); |
68642fb6 | 7954 | |
27778b73 | 7955 | if (outer_binding && BINDING_LEVEL (outer_binding) == outer |
68642fb6 | 7956 | && (TREE_CODE (BINDING_VALUE (outer_binding)) |
27778b73 MM |
7957 | == VAR_DECL) |
7958 | && DECL_DEAD_FOR_LOCAL (BINDING_VALUE (outer_binding))) | |
7959 | { | |
7960 | BINDING_VALUE (outer_binding) | |
7961 | = DECL_SHADOWED_FOR_VAR (BINDING_VALUE (outer_binding)); | |
7962 | current_binding_level->is_for_scope = 0; | |
7963 | } | |
7964 | else if (DECL_IN_MEMORY_P (decl)) | |
7965 | preserve_temp_slots (DECL_RTL (decl)); | |
7966 | } | |
7967 | } | |
7968 | ||
ed5511d9 | 7969 | /* Generate code to initialize DECL (a local variable). */ |
27778b73 | 7970 | |
b7b8bcd2 MM |
7971 | void |
7972 | initialize_local_var (decl, init, flags) | |
27778b73 MM |
7973 | tree decl; |
7974 | tree init; | |
27778b73 MM |
7975 | int flags; |
7976 | { | |
9ed9e79a | 7977 | tree type = TREE_TYPE (decl); |
27778b73 | 7978 | |
9ed9e79a MM |
7979 | /* If the type is bogus, don't bother initializing the variable. */ |
7980 | if (type == error_mark_node) | |
7981 | return; | |
b7b8bcd2 | 7982 | |
b7b8bcd2 MM |
7983 | if (DECL_SIZE (decl) == NULL_TREE && !TREE_STATIC (decl)) |
7984 | { | |
7985 | /* If we used it already as memory, it must stay in memory. */ | |
7986 | DECL_INITIAL (decl) = NULL_TREE; | |
7987 | TREE_ADDRESSABLE (decl) = TREE_USED (decl); | |
7988 | } | |
7989 | ||
9ed9e79a MM |
7990 | /* Local statics are handled differently from ordinary automatic |
7991 | variables. */ | |
7992 | if (TREE_STATIC (decl)) | |
7993 | { | |
7994 | if (TYPE_NEEDS_CONSTRUCTING (type) || init != NULL_TREE | |
834c6dff | 7995 | || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) |
9ed9e79a MM |
7996 | expand_static_init (decl, init); |
7997 | return; | |
7998 | } | |
7999 | ||
27778b73 MM |
8000 | if (DECL_SIZE (decl) && type != error_mark_node) |
8001 | { | |
8002 | int already_used; | |
68642fb6 | 8003 | |
27778b73 | 8004 | /* Compute and store the initial value. */ |
27778b73 MM |
8005 | already_used = TREE_USED (decl) || TREE_USED (type); |
8006 | ||
8007 | if (init || TYPE_NEEDS_CONSTRUCTING (type)) | |
8008 | { | |
24bef158 MM |
8009 | int saved_stmts_are_full_exprs_p; |
8010 | ||
3a0d3e1e | 8011 | my_friendly_assert (building_stmt_tree (), 20000906); |
f2c5f623 | 8012 | saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p (); |
ae499cce | 8013 | current_stmt_tree ()->stmts_are_full_exprs_p = 1; |
3a0d3e1e | 8014 | finish_expr_stmt (build_aggr_init (decl, init, flags)); |
5362b086 | 8015 | current_stmt_tree ()->stmts_are_full_exprs_p = |
ae499cce | 8016 | saved_stmts_are_full_exprs_p; |
27778b73 MM |
8017 | } |
8018 | ||
8019 | /* Set this to 0 so we can tell whether an aggregate which was | |
8020 | initialized was ever used. Don't do this if it has a | |
8021 | destructor, so we don't complain about the 'resource | |
b7b8bcd2 MM |
8022 | allocation is initialization' idiom. Now set |
8023 | attribute((unused)) on types so decls of that type will be | |
8024 | marked used. (see TREE_USED, above.) */ | |
27778b73 MM |
8025 | if (TYPE_NEEDS_CONSTRUCTING (type) |
8026 | && ! already_used | |
834c6dff | 8027 | && TYPE_HAS_TRIVIAL_DESTRUCTOR (type) |
27778b73 MM |
8028 | && DECL_NAME (decl)) |
8029 | TREE_USED (decl) = 0; | |
b7b8bcd2 | 8030 | else if (already_used) |
27778b73 MM |
8031 | TREE_USED (decl) = 1; |
8032 | } | |
24bef158 | 8033 | } |
27778b73 | 8034 | |
24bef158 MM |
8035 | /* Generate code to destroy DECL (a local variable). */ |
8036 | ||
68642fb6 | 8037 | static void |
24bef158 MM |
8038 | destroy_local_var (decl) |
8039 | tree decl; | |
8040 | { | |
9d85d30c MM |
8041 | tree type = TREE_TYPE (decl); |
8042 | tree cleanup; | |
8043 | ||
8044 | /* Only variables get cleaned up. */ | |
8045 | if (TREE_CODE (decl) != VAR_DECL) | |
8046 | return; | |
68642fb6 | 8047 | |
9d85d30c | 8048 | /* And only things with destructors need cleaning up. */ |
655dc6ee JM |
8049 | if (type == error_mark_node |
8050 | || TYPE_HAS_TRIVIAL_DESTRUCTOR (type)) | |
9d85d30c MM |
8051 | return; |
8052 | ||
8053 | if (TREE_CODE (decl) == VAR_DECL && | |
8054 | (DECL_EXTERNAL (decl) || TREE_STATIC (decl))) | |
8055 | /* We don't clean up things that aren't defined in this | |
8056 | translation unit, or that need a static cleanup. The latter | |
8057 | are handled by finish_file. */ | |
8058 | return; | |
68642fb6 | 8059 | |
9d85d30c | 8060 | /* Compute the cleanup. */ |
c88770e9 | 8061 | cleanup = cxx_maybe_build_cleanup (decl); |
27778b73 | 8062 | |
b7b8bcd2 | 8063 | /* Record the cleanup required for this declaration. */ |
6e4ae815 | 8064 | if (DECL_SIZE (decl) && cleanup) |
24bef158 MM |
8065 | finish_decl_cleanup (decl, cleanup); |
8066 | } | |
8067 | ||
8d08fdba MS |
8068 | /* Finish processing of a declaration; |
8069 | install its line number and initial value. | |
8070 | If the length of an array type is not known before, | |
8071 | it must be determined now, from the initial value, or it is an error. | |
8072 | ||
8b27e9ef | 8073 | INIT holds the value of an initializer that should be allowed to escape |
8d08fdba MS |
8074 | the normal rules. |
8075 | ||
920f9474 | 8076 | FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0 |
f78c7bc6 | 8077 | if the (init) syntax was used. */ |
8d08fdba MS |
8078 | |
8079 | void | |
cd9f6678 | 8080 | cp_finish_decl (decl, init, asmspec_tree, flags) |
8d08fdba MS |
8081 | tree decl, init; |
8082 | tree asmspec_tree; | |
6060a796 | 8083 | int flags; |
8d08fdba MS |
8084 | { |
8085 | register tree type; | |
27778b73 | 8086 | tree ttype = NULL_TREE; |
9c0758dd | 8087 | const char *asmspec = NULL; |
8d08fdba MS |
8088 | int was_readonly = 0; |
8089 | ||
8d08fdba MS |
8090 | if (! decl) |
8091 | { | |
8092 | if (init) | |
8251199e | 8093 | error ("assignment (not initialization) in declaration"); |
8d08fdba MS |
8094 | return; |
8095 | } | |
8096 | ||
a4443a08 | 8097 | /* If a name was specified, get the string. */ |
41c64394 RH |
8098 | if (current_binding_level == global_binding_level) |
8099 | asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree); | |
8d08fdba | 8100 | if (asmspec_tree) |
5cc90635 | 8101 | asmspec = TREE_STRING_POINTER (asmspec_tree); |
8d08fdba | 8102 | |
2c73f9f5 ML |
8103 | if (init && TREE_CODE (init) == NAMESPACE_DECL) |
8104 | { | |
33bd39a2 | 8105 | error ("cannot initialize `%D' to namespace `%D'", |
2c73f9f5 ML |
8106 | decl, init); |
8107 | init = NULL_TREE; | |
8108 | } | |
8109 | ||
6ba89f8e | 8110 | if (current_class_type |
4f1c5b7d | 8111 | && CP_DECL_CONTEXT (decl) == current_class_type |
6ba89f8e MM |
8112 | && TYPE_BEING_DEFINED (current_class_type) |
8113 | && (DECL_INITIAL (decl) || init)) | |
3febd123 | 8114 | DECL_INITIALIZED_IN_CLASS_P (decl) = 1; |
6ba89f8e | 8115 | |
68642fb6 | 8116 | if (TREE_CODE (decl) == VAR_DECL |
9a68c51f JM |
8117 | && DECL_CONTEXT (decl) |
8118 | && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL | |
8119 | && DECL_CONTEXT (decl) != current_namespace | |
8120 | && init) | |
8121 | { | |
a1c65f9f | 8122 | /* Leave the namespace of the object. */ |
9a68c51f JM |
8123 | pop_decl_namespace (); |
8124 | } | |
8125 | ||
c82dbd95 | 8126 | type = TREE_TYPE (decl); |
8d08fdba | 8127 | |
f376e137 | 8128 | if (type == error_mark_node) |
cd9f6678 | 8129 | return; |
5362b086 | 8130 | |
a7a7710d NS |
8131 | if (TYPE_HAS_MUTABLE_P (type)) |
8132 | TREE_READONLY (decl) = 0; | |
24bef158 | 8133 | |
5156628f | 8134 | if (processing_template_decl) |
5566b478 | 8135 | { |
08ac397c JM |
8136 | /* Add this declaration to the statement-tree. */ |
8137 | if (at_function_scope_p () | |
8138 | && TREE_CODE (decl) != RESULT_DECL) | |
8139 | add_decl_stmt (decl); | |
8140 | ||
5566b478 | 8141 | if (init && DECL_INITIAL (decl)) |
2a1e9fdd | 8142 | DECL_INITIAL (decl) = init; |
5566b478 MS |
8143 | goto finish_end0; |
8144 | } | |
3e41d13b | 8145 | |
27778b73 MM |
8146 | /* Parameters are handled by store_parm_decls, not cp_finish_decl. */ |
8147 | my_friendly_assert (TREE_CODE (decl) != PARM_DECL, 19990828); | |
8148 | ||
8d08fdba MS |
8149 | /* Take care of TYPE_DECLs up front. */ |
8150 | if (TREE_CODE (decl) == TYPE_DECL) | |
8151 | { | |
a0a33927 MS |
8152 | if (type != error_mark_node |
8153 | && IS_AGGR_TYPE (type) && DECL_NAME (decl)) | |
8d08fdba MS |
8154 | { |
8155 | if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type) | |
33bd39a2 | 8156 | warning ("shadowing previous type declaration of `%#D'", decl); |
8d08fdba MS |
8157 | set_identifier_type_value (DECL_NAME (decl), type); |
8158 | CLASSTYPE_GOT_SEMICOLON (type) = 1; | |
8159 | } | |
cffa8729 MS |
8160 | |
8161 | /* If we have installed this as the canonical typedef for this | |
8162 | type, and that type has not been defined yet, delay emitting | |
956d6950 | 8163 | the debug information for it, as we will emit it later. */ |
d2e5ee5c | 8164 | if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl |
d0f062fb | 8165 | && !COMPLETE_TYPE_P (TREE_TYPE (decl))) |
cffa8729 MS |
8166 | TYPE_DECL_SUPPRESS_DEBUG (decl) = 1; |
8167 | ||
3e411c3f | 8168 | rest_of_decl_compilation (decl, NULL, |
5566b478 | 8169 | DECL_CONTEXT (decl) == NULL_TREE, at_eof); |
8d08fdba MS |
8170 | goto finish_end; |
8171 | } | |
3e41d13b | 8172 | |
8d08fdba | 8173 | if (TREE_CODE (decl) != FUNCTION_DECL) |
3e41d13b | 8174 | ttype = target_type (type); |
8d08fdba MS |
8175 | |
8176 | if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl) | |
8177 | && TYPE_NEEDS_CONSTRUCTING (type)) | |
8178 | { | |
8d08fdba MS |
8179 | /* Currently, GNU C++ puts constants in text space, making them |
8180 | impossible to initialize. In the future, one would hope for | |
8181 | an operating system which understood the difference between | |
8182 | initialization and the running of a program. */ | |
8183 | was_readonly = 1; | |
8184 | TREE_READONLY (decl) = 0; | |
8185 | } | |
8186 | ||
27778b73 | 8187 | if (TREE_CODE (decl) == FIELD_DECL && asmspec) |
8d08fdba | 8188 | { |
27778b73 MM |
8189 | /* This must override the asm specifier which was placed by |
8190 | grokclassfn. Lay this out fresh. */ | |
19e7881c | 8191 | SET_DECL_RTL (TREE_TYPE (decl), NULL_RTX); |
92643fea | 8192 | SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec)); |
6c418184 | 8193 | make_decl_rtl (decl, asmspec); |
8d08fdba | 8194 | } |
17bbb839 MM |
8195 | else if (TREE_CODE (decl) == RESULT_DECL) |
8196 | init = check_initializer (decl, init); | |
8197 | else if (TREE_CODE (decl) == VAR_DECL) | |
8198 | { | |
8199 | /* Only PODs can have thread-local storage. Other types may require | |
8200 | various kinds of non-trivial initialization. */ | |
8201 | if (DECL_THREAD_LOCAL (decl) && !pod_type_p (TREE_TYPE (decl))) | |
8202 | error ("`%D' cannot be thread-local because it has non-POD type `%T'", | |
8203 | decl, TREE_TYPE (decl)); | |
8204 | /* Convert the initializer to the type of DECL, if we have not | |
8205 | already initialized DECL. */ | |
8206 | if (!DECL_INITIALIZED_P (decl) | |
8207 | /* If !DECL_EXTERNAL then DECL is being defined. In the | |
8208 | case of a static data memberm initialized inside the | |
8209 | class-specifier, there can be an initializer even if DECL | |
8210 | is *not* defined. */ | |
8211 | && (!DECL_EXTERNAL (decl) || init)) | |
8212 | { | |
8213 | init = check_initializer (decl, init); | |
b4c20e52 MM |
8214 | /* Thread-local storage cannot be dynamically initialized. */ |
8215 | if (DECL_THREAD_LOCAL (decl) && init) | |
8216 | { | |
8217 | error ("`%D' is thread-local and so cannot be dynamically " | |
8218 | "initialized", decl); | |
8219 | init = NULL_TREE; | |
8220 | } | |
17bbb839 MM |
8221 | /* If DECL has an array type without a specific bound, deduce the |
8222 | array size from the initializer. Note that this must be done | |
8223 | after check_initializer is called because of cases like this: | |
8224 | ||
8225 | struct S { int a; int b; }; | |
8226 | struct S a[] = { 1, 2 }; | |
8227 | ||
8228 | which creates a one-element array, not a two-element array. */ | |
8229 | maybe_deduce_size_from_array_init (decl, init); | |
8230 | /* Handle: | |
8231 | ||
8232 | [dcl.init] | |
8233 | ||
8234 | The memory occupied by any object of static storage | |
8235 | duration is zero-initialized at program startup before | |
8236 | any other initialization takes place. | |
8237 | ||
8238 | We cannot create an appropriate initializer until after | |
8239 | the type of DECL is finalized. If DECL_INITIAL is set, | |
8240 | then the DECL is statically initialized, and any | |
8241 | necessary zero-initialization has already been performed. */ | |
8242 | if (TREE_STATIC (decl) && !DECL_INITIAL (decl)) | |
8243 | DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl), | |
8244 | /*static_storage_p=*/true); | |
8245 | /* Remember that the initialization for this variable has | |
8246 | taken place. */ | |
8247 | DECL_INITIALIZED_P (decl) = 1; | |
8248 | } | |
d881ff25 MM |
8249 | /* If the variable has an array type, lay out the type, even if |
8250 | there is no initializer. It is valid to index through the | |
8251 | array, and we must get TYPE_ALIGN set correctly on the array | |
8252 | type. */ | |
8253 | else if (TREE_CODE (type) == ARRAY_TYPE) | |
8254 | layout_type (type); | |
17bbb839 | 8255 | } |
3e41d13b | 8256 | |
57cf57fb JM |
8257 | /* Add this declaration to the statement-tree. This needs to happen |
8258 | after the call to check_initializer so that the DECL_STMT for a | |
8259 | reference temp is added before the DECL_STMT for the reference itself. */ | |
08ac397c JM |
8260 | if (building_stmt_tree () |
8261 | && at_function_scope_p () | |
8262 | && TREE_CODE (decl) != RESULT_DECL) | |
8263 | add_decl_stmt (decl); | |
8264 | ||
8d08fdba | 8265 | if (TREE_CODE (decl) == VAR_DECL) |
57b52417 | 8266 | layout_var_decl (decl); |
8d08fdba MS |
8267 | |
8268 | /* Output the assembler code and/or RTL code for variables and functions, | |
8269 | unless the type is an undefined structure or union. | |
8270 | If not, it will get done when the type is completed. */ | |
8d08fdba MS |
8271 | if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL |
8272 | || TREE_CODE (decl) == RESULT_DECL) | |
8273 | { | |
27778b73 MM |
8274 | if (TREE_CODE (decl) == VAR_DECL) |
8275 | maybe_commonize_var (decl); | |
8d08fdba | 8276 | |
27778b73 | 8277 | make_rtl_for_nonlocal_decl (decl, init, asmspec); |
8d08fdba | 8278 | |
68642fb6 | 8279 | if (TREE_CODE (type) == FUNCTION_TYPE |
27778b73 | 8280 | || TREE_CODE (type) == METHOD_TYPE) |
68642fb6 | 8281 | abstract_virtuals_error (decl, |
27778b73 | 8282 | strip_array_types (TREE_TYPE (type))); |
68642fb6 | 8283 | else |
27778b73 | 8284 | abstract_virtuals_error (decl, strip_array_types (type)); |
8d08fdba | 8285 | |
8d08fdba | 8286 | if (TREE_CODE (decl) == FUNCTION_DECL) |
faae18ab | 8287 | ; |
67d743fe MS |
8288 | else if (DECL_EXTERNAL (decl) |
8289 | && ! (DECL_LANG_SPECIFIC (decl) | |
8290 | && DECL_NOT_REALLY_EXTERN (decl))) | |
5566b478 MS |
8291 | { |
8292 | if (init) | |
8293 | DECL_INITIAL (decl) = init; | |
8294 | } | |
b35d4555 | 8295 | else if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL) |
8d08fdba | 8296 | { |
b7b8bcd2 | 8297 | /* This is a local declaration. */ |
b35d4555 MM |
8298 | if (doing_semantic_analysis_p ()) |
8299 | maybe_inject_for_scope_var (decl); | |
b7b8bcd2 MM |
8300 | /* Initialize the local variable. But, if we're building a |
8301 | statement-tree, we'll do the initialization when we | |
8302 | expand the tree. */ | |
24bef158 MM |
8303 | if (processing_template_decl) |
8304 | { | |
8305 | if (init || DECL_INITIAL (decl) == error_mark_node) | |
8306 | DECL_INITIAL (decl) = init; | |
8307 | } | |
8308 | else | |
8309 | { | |
b35d4555 MM |
8310 | /* If we're not building RTL, then we need to do so |
8311 | now. */ | |
44835fdd | 8312 | my_friendly_assert (building_stmt_tree (), 20000906); |
b35d4555 | 8313 | /* Initialize the variable. */ |
24bef158 MM |
8314 | initialize_local_var (decl, init, flags); |
8315 | /* Clean up the variable. */ | |
8316 | destroy_local_var (decl); | |
8317 | } | |
8d08fdba | 8318 | } |
9ed9e79a MM |
8319 | else if (TREE_STATIC (decl) && type != error_mark_node) |
8320 | { | |
8321 | /* Cleanups for static variables are handled by `finish_file'. */ | |
8322 | if (TYPE_NEEDS_CONSTRUCTING (type) || init != NULL_TREE | |
834c6dff | 8323 | || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) |
9ed9e79a MM |
8324 | expand_static_init (decl, init); |
8325 | } | |
8d08fdba MS |
8326 | finish_end0: |
8327 | ||
8328 | /* Undo call to `pushclass' that was done in `start_decl' | |
8329 | due to initialization of qualified member variable. | |
8330 | I.e., Foo::x = 10; */ | |
8331 | { | |
4f1c5b7d | 8332 | tree context = CP_DECL_CONTEXT (decl); |
8d08fdba | 8333 | if (context |
2f939d94 | 8334 | && TYPE_P (context) |
8d08fdba MS |
8335 | && (TREE_CODE (decl) == VAR_DECL |
8336 | /* We also have a pushclass done that we need to undo here | |
8337 | if we're at top level and declare a method. */ | |
5566b478 MS |
8338 | || TREE_CODE (decl) == FUNCTION_DECL) |
8339 | /* If size hasn't been set, we're still defining it, | |
8340 | and therefore inside the class body; don't pop | |
8341 | the binding level.. */ | |
d0f062fb | 8342 | && COMPLETE_TYPE_P (context) |
5566b478 | 8343 | && context == current_class_type) |
6b400b21 | 8344 | pop_nested_class (); |
8d08fdba MS |
8345 | } |
8346 | } | |
8347 | ||
8348 | finish_end: | |
8349 | ||
8d08fdba MS |
8350 | if (was_readonly) |
8351 | TREE_READONLY (decl) = 1; | |
8d08fdba MS |
8352 | } |
8353 | ||
82580166 | 8354 | /* This is here for a midend callback from c-common.c */ |
e92cc029 | 8355 | |
82580166 MS |
8356 | void |
8357 | finish_decl (decl, init, asmspec_tree) | |
8358 | tree decl, init; | |
8359 | tree asmspec_tree; | |
8360 | { | |
cd9f6678 | 8361 | cp_finish_decl (decl, init, asmspec_tree, 0); |
82580166 MS |
8362 | } |
8363 | ||
db4283a0 MM |
8364 | /* Returns a declaration for a VAR_DECL as if: |
8365 | ||
8366 | extern "C" TYPE NAME; | |
8367 | ||
8368 | had been seen. Used to create compiler-generated global | |
8369 | variables. */ | |
8370 | ||
8371 | tree | |
8372 | declare_global_var (name, type) | |
8373 | tree name; | |
8374 | tree type; | |
8375 | { | |
8376 | tree decl; | |
8377 | ||
8378 | push_to_top_level (); | |
8379 | decl = build_decl (VAR_DECL, name, type); | |
8380 | TREE_PUBLIC (decl) = 1; | |
8381 | DECL_EXTERNAL (decl) = 1; | |
8382 | DECL_ARTIFICIAL (decl) = 1; | |
8383 | pushdecl (decl); | |
8384 | cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0); | |
8385 | pop_from_top_level (); | |
8386 | ||
8387 | return decl; | |
8388 | } | |
8389 | ||
8390 | /* Returns a pointer to the `atexit' function. Note that if | |
838dfd8a | 8391 | FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new |
db4283a0 MM |
8392 | `__cxa_atexit' function specified in the IA64 C++ ABI. */ |
8393 | ||
8394 | static tree | |
8395 | get_atexit_node () | |
8396 | { | |
8397 | tree atexit_fndecl; | |
8398 | tree arg_types; | |
8399 | tree fn_type; | |
8400 | tree fn_ptr_type; | |
8401 | const char *name; | |
8402 | ||
8403 | if (atexit_node) | |
8404 | return atexit_node; | |
8405 | ||
8406 | if (flag_use_cxa_atexit) | |
8407 | { | |
8408 | /* The declaration for `__cxa_atexit' is: | |
8409 | ||
8410 | int __cxa_atexit (void (*)(void *), void *, void *) | |
8411 | ||
8412 | We build up the argument types and then then function type | |
8413 | itself. */ | |
68642fb6 | 8414 | |
db4283a0 MM |
8415 | /* First, build the pointer-to-function type for the first |
8416 | argument. */ | |
8417 | arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node); | |
8418 | fn_type = build_function_type (void_type_node, arg_types); | |
8419 | fn_ptr_type = build_pointer_type (fn_type); | |
8420 | /* Then, build the rest of the argument types. */ | |
8421 | arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node); | |
8422 | arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types); | |
8423 | arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types); | |
8424 | /* And the final __cxa_atexit type. */ | |
8425 | fn_type = build_function_type (integer_type_node, arg_types); | |
8426 | fn_ptr_type = build_pointer_type (fn_type); | |
8427 | name = "__cxa_atexit"; | |
8428 | } | |
8429 | else | |
8430 | { | |
8431 | /* The declaration for `atexit' is: | |
68642fb6 | 8432 | |
db4283a0 MM |
8433 | int atexit (void (*)()); |
8434 | ||
8435 | We build up the argument types and then then function type | |
8436 | itself. */ | |
8437 | fn_type = build_function_type (void_type_node, void_list_node); | |
8438 | fn_ptr_type = build_pointer_type (fn_type); | |
8439 | arg_types = tree_cons (NULL_TREE, fn_ptr_type, void_list_node); | |
8440 | /* Build the final atexit type. */ | |
8441 | fn_type = build_function_type (integer_type_node, arg_types); | |
8442 | name = "atexit"; | |
8443 | } | |
8444 | ||
8445 | /* Now, build the function declaration. */ | |
8446 | push_lang_context (lang_name_c); | |
0c11ada6 | 8447 | atexit_fndecl = build_library_fn_ptr (name, fn_type); |
db4283a0 MM |
8448 | mark_used (atexit_fndecl); |
8449 | pop_lang_context (); | |
8450 | atexit_node = default_conversion (atexit_fndecl); | |
8451 | ||
8452 | return atexit_node; | |
8453 | } | |
8454 | ||
8455 | /* Returns the __dso_handle VAR_DECL. */ | |
8456 | ||
8457 | static tree | |
8458 | get_dso_handle_node () | |
8459 | { | |
8460 | if (dso_handle_node) | |
8461 | return dso_handle_node; | |
8462 | ||
8463 | /* Declare the variable. */ | |
8464 | dso_handle_node = declare_global_var (get_identifier ("__dso_handle"), | |
8465 | ptr_type_node); | |
8466 | ||
8467 | return dso_handle_node; | |
8468 | } | |
8469 | ||
8470 | /* Begin a new function with internal linkage whose job will be simply | |
8471 | to destroy some particular variable. */ | |
8472 | ||
8473 | static tree | |
8474 | start_cleanup_fn () | |
8475 | { | |
8476 | static int counter = 0; | |
23ccd1f3 | 8477 | int old_interface_only = interface_only; |
db4283a0 MM |
8478 | int old_interface_unknown = interface_unknown; |
8479 | char name[32]; | |
8480 | tree parmtypes; | |
8481 | tree fntype; | |
8482 | tree fndecl; | |
8483 | ||
8484 | push_to_top_level (); | |
8485 | ||
8486 | /* No need to mangle this. */ | |
8487 | push_lang_context (lang_name_c); | |
8488 | ||
23ccd1f3 | 8489 | interface_only = 0; |
db4283a0 MM |
8490 | interface_unknown = 1; |
8491 | ||
8492 | /* Build the parameter-types. */ | |
8493 | parmtypes = void_list_node; | |
8494 | /* Functions passed to __cxa_atexit take an additional parameter. | |
8495 | We'll just ignore it. After we implement the new calling | |
8496 | convention for destructors, we can eliminate the use of | |
8497 | additional cleanup functions entirely in the -fnew-abi case. */ | |
8498 | if (flag_use_cxa_atexit) | |
8499 | parmtypes = tree_cons (NULL_TREE, ptr_type_node, parmtypes); | |
8500 | /* Build the function type itself. */ | |
8501 | fntype = build_function_type (void_type_node, parmtypes); | |
8502 | /* Build the name of the function. */ | |
8503 | sprintf (name, "__tcf_%d", counter++); | |
8504 | /* Build the function declaration. */ | |
8505 | fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype); | |
8506 | /* It's a function with internal linkage, generated by the | |
8507 | compiler. */ | |
8508 | TREE_PUBLIC (fndecl) = 0; | |
8509 | DECL_ARTIFICIAL (fndecl) = 1; | |
5a728aca MM |
8510 | /* Make the function `inline' so that it is only emitted if it is |
8511 | actually needed. It is unlikely that it will be inlined, since | |
aba649ba | 8512 | it is only called via a function pointer, but we avoid unnecessary |
5a728aca MM |
8513 | emissions this way. */ |
8514 | DECL_INLINE (fndecl) = 1; | |
db4283a0 MM |
8515 | /* Build the parameter. */ |
8516 | if (flag_use_cxa_atexit) | |
8517 | { | |
8518 | tree parmdecl; | |
8519 | ||
8e51619a | 8520 | parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node); |
db4283a0 | 8521 | DECL_CONTEXT (parmdecl) = fndecl; |
db4283a0 MM |
8522 | TREE_USED (parmdecl) = 1; |
8523 | DECL_ARGUMENTS (fndecl) = parmdecl; | |
8524 | } | |
8525 | ||
09ed39ad | 8526 | pushdecl (fndecl); |
db4283a0 | 8527 | start_function (/*specs=*/NULL_TREE, fndecl, NULL_TREE, SF_PRE_PARSED); |
db4283a0 MM |
8528 | |
8529 | interface_unknown = old_interface_unknown; | |
23ccd1f3 | 8530 | interface_only = old_interface_only; |
db4283a0 MM |
8531 | |
8532 | pop_lang_context (); | |
8533 | ||
8534 | return current_function_decl; | |
8535 | } | |
8536 | ||
8537 | /* Finish the cleanup function begun by start_cleanup_fn. */ | |
8538 | ||
8539 | static void | |
8540 | end_cleanup_fn () | |
8541 | { | |
0acf7199 | 8542 | expand_body (finish_function (0)); |
db4283a0 MM |
8543 | |
8544 | pop_from_top_level (); | |
8545 | } | |
8546 | ||
bf419747 MM |
8547 | /* Generate code to handle the destruction of DECL, an object with |
8548 | static storage duration. */ | |
f0105ed3 | 8549 | |
bf419747 MM |
8550 | void |
8551 | register_dtor_fn (decl) | |
f0105ed3 MM |
8552 | tree decl; |
8553 | { | |
db4283a0 | 8554 | tree cleanup; |
f0105ed3 | 8555 | tree compound_stmt; |
db4283a0 MM |
8556 | tree args; |
8557 | tree fcall; | |
f0105ed3 | 8558 | |
db4283a0 | 8559 | int saved_flag_access_control; |
f0105ed3 | 8560 | |
834c6dff | 8561 | if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl))) |
bf419747 MM |
8562 | return; |
8563 | ||
f0105ed3 MM |
8564 | /* Call build_cleanup before we enter the anonymous function so that |
8565 | any access checks will be done relative to the current scope, | |
8566 | rather than the scope of the anonymous function. */ | |
8567 | build_cleanup (decl); | |
8568 | ||
8569 | /* Now start the function. */ | |
db4283a0 | 8570 | cleanup = start_cleanup_fn (); |
f0105ed3 MM |
8571 | |
8572 | /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer | |
8573 | to the original function, rather than the anonymous one. That | |
8574 | will make the back-end think that nested functions are in use, | |
8575 | which causes confusion. */ | |
8576 | saved_flag_access_control = flag_access_control; | |
8577 | flag_access_control = 0; | |
8578 | fcall = build_cleanup (decl); | |
8579 | flag_access_control = saved_flag_access_control; | |
8580 | ||
8581 | /* Create the body of the anonymous function. */ | |
8582 | compound_stmt = begin_compound_stmt (/*has_no_scope=*/0); | |
8583 | finish_expr_stmt (fcall); | |
8584 | finish_compound_stmt (/*has_no_scope=*/0, compound_stmt); | |
db4283a0 | 8585 | end_cleanup_fn (); |
f0105ed3 MM |
8586 | |
8587 | /* Call atexit with the cleanup function. */ | |
dffd7eb6 | 8588 | cxx_mark_addressable (cleanup); |
f0105ed3 | 8589 | cleanup = build_unary_op (ADDR_EXPR, cleanup, 0); |
db4283a0 MM |
8590 | if (flag_use_cxa_atexit) |
8591 | { | |
0f268005 MM |
8592 | args = tree_cons (NULL_TREE, |
8593 | build_unary_op (ADDR_EXPR, get_dso_handle_node (), 0), | |
8594 | NULL_TREE); | |
db4283a0 MM |
8595 | args = tree_cons (NULL_TREE, null_pointer_node, args); |
8596 | args = tree_cons (NULL_TREE, cleanup, args); | |
8597 | } | |
8598 | else | |
8599 | args = tree_cons (NULL_TREE, cleanup, NULL_TREE); | |
8600 | finish_expr_stmt (build_function_call (get_atexit_node (), args)); | |
f0105ed3 MM |
8601 | } |
8602 | ||
8d08fdba MS |
8603 | void |
8604 | expand_static_init (decl, init) | |
8605 | tree decl; | |
8606 | tree init; | |
8607 | { | |
8608 | tree oldstatic = value_member (decl, static_aggregates); | |
a4443a08 | 8609 | |
8d08fdba MS |
8610 | if (oldstatic) |
8611 | { | |
8612 | if (TREE_PURPOSE (oldstatic) && init != NULL_TREE) | |
33bd39a2 | 8613 | error ("multiple initializations given for `%D'", decl); |
8d08fdba | 8614 | } |
0aafb128 | 8615 | else if (! toplevel_bindings_p ()) |
8d08fdba MS |
8616 | { |
8617 | /* Emit code to perform this initialization but once. */ | |
b7b8bcd2 | 8618 | tree if_stmt; |
f0105ed3 | 8619 | tree then_clause; |
f1dedc31 | 8620 | tree assignment; |
c395453c MM |
8621 | tree guard; |
8622 | tree guard_init; | |
8d08fdba | 8623 | |
2036a15c MM |
8624 | /* Emit code to perform this initialization but once. This code |
8625 | looks like: | |
8626 | ||
c395453c MM |
8627 | static int guard = 0; |
8628 | if (!guard) { | |
2036a15c | 8629 | // Do initialization. |
c395453c | 8630 | guard = 1; |
2036a15c MM |
8631 | // Register variable for destruction at end of program. |
8632 | } | |
8633 | ||
8634 | Note that the `temp' variable is only set to 1 *after* the | |
8635 | initialization is complete. This ensures that an exception, | |
8636 | thrown during the construction, will cause the variable to | |
8637 | reinitialized when we pass through this code again, as per: | |
68642fb6 | 8638 | |
2036a15c MM |
8639 | [stmt.dcl] |
8640 | ||
8641 | If the initialization exits by throwing an exception, the | |
8642 | initialization is not complete, so it will be tried again | |
8643 | the next time control enters the declaration. | |
8644 | ||
8645 | In theory, this process should be thread-safe, too; multiple | |
8646 | threads should not be able to initialize the variable more | |
8647 | than once. We don't yet attempt to ensure thread-safety. */ | |
c395453c MM |
8648 | |
8649 | /* Create the guard variable. */ | |
8650 | guard = get_guard (decl); | |
2036a15c MM |
8651 | |
8652 | /* Begin the conditional initialization. */ | |
b7b8bcd2 | 8653 | if_stmt = begin_if_stmt (); |
c395453c | 8654 | finish_if_stmt_cond (get_guard_cond (guard), if_stmt); |
f0105ed3 | 8655 | then_clause = begin_compound_stmt (/*has_no_scope=*/0); |
72b7eeff | 8656 | |
2036a15c | 8657 | /* Do the initialization itself. */ |
28cbf42c | 8658 | if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)) |
f30432d7 | 8659 | || (init && TREE_CODE (init) == TREE_LIST)) |
f1dedc31 | 8660 | assignment = build_aggr_init (decl, init, 0); |
f30432d7 | 8661 | else if (init) |
c557501d MM |
8662 | /* The initialization we're doing here is just a bitwise |
8663 | copy. */ | |
8664 | assignment = build (INIT_EXPR, TREE_TYPE (decl), decl, init); | |
f1dedc31 MM |
8665 | else |
8666 | assignment = NULL_TREE; | |
8667 | ||
8668 | /* Once the assignment is complete, set TEMP to 1. Since the | |
8669 | construction of the static object is complete at this point, | |
8670 | we want to make sure TEMP is set to 1 even if a temporary | |
8671 | constructed during the initialization throws an exception | |
8672 | when it is destroyed. So, we combine the initialization and | |
8673 | the assignment to TEMP into a single expression, ensuring | |
8674 | that when we call finish_expr_stmt the cleanups will not be | |
8675 | run until after TEMP is set to 1. */ | |
c395453c | 8676 | guard_init = set_guard (guard); |
f1dedc31 MM |
8677 | if (assignment) |
8678 | { | |
8679 | assignment = tree_cons (NULL_TREE, assignment, | |
68642fb6 | 8680 | build_tree_list (NULL_TREE, |
c395453c | 8681 | guard_init)); |
f1dedc31 MM |
8682 | assignment = build_compound_expr (assignment); |
8683 | } | |
8684 | else | |
c395453c | 8685 | assignment = guard_init; |
f1dedc31 | 8686 | finish_expr_stmt (assignment); |
72b7eeff | 8687 | |
2036a15c MM |
8688 | /* Use atexit to register a function for destroying this static |
8689 | variable. */ | |
bf419747 | 8690 | register_dtor_fn (decl); |
72b7eeff | 8691 | |
f0105ed3 | 8692 | finish_compound_stmt (/*has_no_scope=*/0, then_clause); |
b7b8bcd2 MM |
8693 | finish_then_clause (if_stmt); |
8694 | finish_if_stmt (); | |
8d08fdba MS |
8695 | } |
8696 | else | |
bbd15aac | 8697 | static_aggregates = tree_cons (init, decl, static_aggregates); |
8d08fdba | 8698 | } |
3c5c0849 MM |
8699 | |
8700 | /* Finish the declaration of a catch-parameter. */ | |
8701 | ||
b35d4555 | 8702 | tree |
3c5c0849 MM |
8703 | start_handler_parms (declspecs, declarator) |
8704 | tree declspecs; | |
8705 | tree declarator; | |
8706 | { | |
8707 | tree decl; | |
8708 | if (declspecs) | |
8709 | { | |
8710 | decl = grokdeclarator (declarator, declspecs, CATCHPARM, | |
91d231cb | 8711 | 1, NULL); |
3c5c0849 MM |
8712 | if (decl == NULL_TREE) |
8713 | error ("invalid catch parameter"); | |
8714 | } | |
8715 | else | |
8716 | decl = NULL_TREE; | |
b35d4555 MM |
8717 | |
8718 | return decl; | |
3c5c0849 MM |
8719 | } |
8720 | ||
8d08fdba MS |
8721 | \f |
8722 | /* Make TYPE a complete type based on INITIAL_VALUE. | |
8723 | Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered, | |
6ab5c740 | 8724 | 2 if there was no information (in which case assume 0 if DO_DEFAULT). */ |
8d08fdba MS |
8725 | |
8726 | int | |
8727 | complete_array_type (type, initial_value, do_default) | |
8728 | tree type, initial_value; | |
8729 | int do_default; | |
8730 | { | |
8731 | register tree maxindex = NULL_TREE; | |
8732 | int value = 0; | |
68642fb6 | 8733 | |
8d08fdba MS |
8734 | if (initial_value) |
8735 | { | |
7b019c19 MM |
8736 | /* An array of character type can be initialized from a |
8737 | brace-enclosed string constant. */ | |
8738 | if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))) | |
8739 | && TREE_CODE (initial_value) == CONSTRUCTOR | |
8740 | && CONSTRUCTOR_ELTS (initial_value) | |
8741 | && (TREE_CODE (TREE_VALUE (CONSTRUCTOR_ELTS (initial_value))) | |
8742 | == STRING_CST) | |
8743 | && TREE_CHAIN (CONSTRUCTOR_ELTS (initial_value)) == NULL_TREE) | |
8744 | initial_value = TREE_VALUE (CONSTRUCTOR_ELTS (initial_value)); | |
8745 | ||
8746 | /* Note MAXINDEX is really the maximum index, one less than the | |
8747 | size. */ | |
8d08fdba | 8748 | if (TREE_CODE (initial_value) == STRING_CST) |
e1cd6e56 MS |
8749 | { |
8750 | int eltsize | |
8751 | = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value))); | |
8752 | maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value) | |
8753 | / eltsize) - 1, 0); | |
8754 | } | |
8d08fdba MS |
8755 | else if (TREE_CODE (initial_value) == CONSTRUCTOR) |
8756 | { | |
e1cd6e56 | 8757 | tree elts = CONSTRUCTOR_ELTS (initial_value); |
fed3cef0 RK |
8758 | |
8759 | maxindex = ssize_int (-1); | |
e1cd6e56 MS |
8760 | for (; elts; elts = TREE_CHAIN (elts)) |
8761 | { | |
8762 | if (TREE_PURPOSE (elts)) | |
8763 | maxindex = TREE_PURPOSE (elts); | |
8764 | else | |
fed3cef0 | 8765 | maxindex = size_binop (PLUS_EXPR, maxindex, ssize_int (1)); |
e1cd6e56 MS |
8766 | } |
8767 | maxindex = copy_node (maxindex); | |
8d08fdba MS |
8768 | } |
8769 | else | |
8770 | { | |
8771 | /* Make an error message unless that happened already. */ | |
8772 | if (initial_value != error_mark_node) | |
8773 | value = 1; | |
0db982be ML |
8774 | else |
8775 | initial_value = NULL_TREE; | |
8d08fdba MS |
8776 | |
8777 | /* Prevent further error messages. */ | |
8778 | maxindex = build_int_2 (0, 0); | |
8779 | } | |
8780 | } | |
8781 | ||
8782 | if (!maxindex) | |
8783 | { | |
8784 | if (do_default) | |
8785 | maxindex = build_int_2 (0, 0); | |
8786 | value = 2; | |
8787 | } | |
8788 | ||
8789 | if (maxindex) | |
8790 | { | |
51c184be | 8791 | tree itype; |
6ab5c740 NS |
8792 | tree domain; |
8793 | ||
8794 | domain = build_index_type (maxindex); | |
8795 | TYPE_DOMAIN (type) = domain; | |
51c184be | 8796 | |
dff6b454 | 8797 | if (! TREE_TYPE (maxindex)) |
6ab5c740 | 8798 | TREE_TYPE (maxindex) = domain; |
51c184be MS |
8799 | if (initial_value) |
8800 | itype = TREE_TYPE (initial_value); | |
8801 | else | |
8802 | itype = NULL; | |
8803 | if (itype && !TYPE_DOMAIN (itype)) | |
6ab5c740 | 8804 | TYPE_DOMAIN (itype) = domain; |
dff6b454 RK |
8805 | /* The type of the main variant should never be used for arrays |
8806 | of different sizes. It should only ever be completed with the | |
8807 | size of the array. */ | |
8808 | if (! TYPE_DOMAIN (TYPE_MAIN_VARIANT (type))) | |
6ab5c740 | 8809 | TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)) = domain; |
8d08fdba MS |
8810 | } |
8811 | ||
8812 | /* Lay out the type now that we can get the real answer. */ | |
8813 | ||
8814 | layout_type (type); | |
8815 | ||
8816 | return value; | |
8817 | } | |
8818 | \f | |
8819 | /* Return zero if something is declared to be a member of type | |
8820 | CTYPE when in the context of CUR_TYPE. STRING is the error | |
8821 | message to print in that case. Otherwise, quietly return 1. */ | |
e92cc029 | 8822 | |
8d08fdba | 8823 | static int |
4dacf5bd | 8824 | member_function_or_else (ctype, cur_type, flags) |
8d08fdba | 8825 | tree ctype, cur_type; |
4dacf5bd | 8826 | enum overload_flags flags; |
8d08fdba MS |
8827 | { |
8828 | if (ctype && ctype != cur_type) | |
8829 | { | |
4dacf5bd | 8830 | if (flags == DTOR_FLAG) |
33bd39a2 | 8831 | error ("destructor for alien class `%T' cannot be a member", |
2ae7bada | 8832 | ctype); |
4dacf5bd | 8833 | else |
33bd39a2 | 8834 | error ("constructor for alien class `%T' cannot be a member", |
2ae7bada | 8835 | ctype); |
8d08fdba MS |
8836 | return 0; |
8837 | } | |
8838 | return 1; | |
8839 | } | |
8840 | \f | |
8841 | /* Subroutine of `grokdeclarator'. */ | |
8842 | ||
8843 | /* Generate errors possibly applicable for a given set of specifiers. | |
8844 | This is for ARM $7.1.2. */ | |
e92cc029 | 8845 | |
8d08fdba MS |
8846 | static void |
8847 | bad_specifiers (object, type, virtualp, quals, inlinep, friendp, raises) | |
8848 | tree object; | |
d8e178a0 | 8849 | const char *type; |
8d08fdba MS |
8850 | int virtualp, quals, friendp, raises, inlinep; |
8851 | { | |
8852 | if (virtualp) | |
33bd39a2 | 8853 | error ("`%D' declared as a `virtual' %s", object, type); |
8d08fdba | 8854 | if (inlinep) |
33bd39a2 | 8855 | error ("`%D' declared as an `inline' %s", object, type); |
8d08fdba | 8856 | if (quals) |
33bd39a2 | 8857 | error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration", |
a28e3c7f | 8858 | object, type); |
8d08fdba | 8859 | if (friendp) |
f8e55f34 | 8860 | cp_error_at ("`%D' declared as a friend", object); |
e1be26f4 RS |
8861 | if (raises |
8862 | && (TREE_CODE (object) == TYPE_DECL | |
8863 | || (!TYPE_PTRFN_P (TREE_TYPE (object)) | |
742a37d5 | 8864 | && !TYPE_REFFN_P (TREE_TYPE (object)) |
e1be26f4 | 8865 | && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object))))) |
f8e55f34 | 8866 | cp_error_at ("`%D' declared with an exception specification", object); |
8d08fdba MS |
8867 | } |
8868 | ||
8869 | /* CTYPE is class type, or null if non-class. | |
8870 | TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE | |
8871 | or METHOD_TYPE. | |
8872 | DECLARATOR is the function's name. | |
8873 | VIRTUALP is truthvalue of whether the function is virtual or not. | |
8874 | FLAGS are to be passed through to `grokclassfn'. | |
8875 | QUALS are qualifiers indicating whether the function is `const' | |
8876 | or `volatile'. | |
8877 | RAISES is a list of exceptions that this function can raise. | |
8878 | CHECK is 1 if we must find this method in CTYPE, 0 if we should | |
68642fb6 | 8879 | not look, and -1 if we should not call `grokclassfn' at all. |
3ddfb0e6 | 8880 | |
20496fa2 | 8881 | Returns `NULL_TREE' if something goes wrong, after issuing |
3ddfb0e6 | 8882 | applicable error messages. */ |
e92cc029 | 8883 | |
8d08fdba | 8884 | static tree |
386b8a85 | 8885 | grokfndecl (ctype, type, declarator, orig_declarator, virtualp, flags, quals, |
7a8f9fa9 | 8886 | raises, check, friendp, publicp, inlinep, funcdef_flag, |
2c73f9f5 | 8887 | template_count, in_namespace) |
8d08fdba MS |
8888 | tree ctype, type; |
8889 | tree declarator; | |
386b8a85 | 8890 | tree orig_declarator; |
8d08fdba MS |
8891 | int virtualp; |
8892 | enum overload_flags flags; | |
7a8f9fa9 | 8893 | tree quals, raises; |
386b8a85 | 8894 | int check, friendp, publicp, inlinep, funcdef_flag, template_count; |
2c73f9f5 | 8895 | tree in_namespace; |
8d08fdba | 8896 | { |
1951a1b6 | 8897 | tree decl; |
8d08fdba | 8898 | int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE; |
856216bb | 8899 | int has_default_arg = 0; |
42976354 | 8900 | tree t; |
8d08fdba | 8901 | |
8d08fdba | 8902 | if (raises) |
271e6f02 | 8903 | type = build_exception_variant (type, raises); |
c11b6f21 | 8904 | |
8d08fdba | 8905 | decl = build_lang_decl (FUNCTION_DECL, declarator, type); |
a1c65f9f | 8906 | /* Propagate volatile out from type to decl. */ |
8d08fdba | 8907 | if (TYPE_VOLATILE (type)) |
893de33c | 8908 | TREE_THIS_VOLATILE (decl) = 1; |
8d08fdba | 8909 | |
79c4d4b7 | 8910 | /* If this decl has namespace scope, set that up. */ |
2c73f9f5 | 8911 | if (in_namespace) |
b262d64c | 8912 | set_decl_namespace (decl, in_namespace, friendp); |
adae082f | 8913 | else if (!ctype) |
79c4d4b7 | 8914 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); |
2c73f9f5 | 8915 | |
0f8766b8 JM |
8916 | /* `main' and builtins have implicit 'C' linkage. */ |
8917 | if ((MAIN_NAME_P (declarator) | |
8918 | || (IDENTIFIER_LENGTH (declarator) > 10 | |
8919 | && IDENTIFIER_POINTER (declarator)[0] == '_' | |
8920 | && IDENTIFIER_POINTER (declarator)[1] == '_' | |
8921 | && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0)) | |
8922 | && current_lang_name == lang_name_cplusplus | |
94706a5c | 8923 | && ctype == NULL_TREE |
79c4d4b7 JM |
8924 | /* NULL_TREE means global namespace. */ |
8925 | && DECL_CONTEXT (decl) == NULL_TREE) | |
5d2ed28c | 8926 | SET_DECL_LANGUAGE (decl, lang_c); |
0f8766b8 | 8927 | |
8d08fdba MS |
8928 | /* Should probably propagate const out from type to decl I bet (mrs). */ |
8929 | if (staticp) | |
8930 | { | |
8931 | DECL_STATIC_FUNCTION_P (decl) = 1; | |
8932 | DECL_CONTEXT (decl) = ctype; | |
8d08fdba MS |
8933 | } |
8934 | ||
e76a2646 | 8935 | if (ctype) |
4f1c5b7d | 8936 | DECL_CONTEXT (decl) = ctype; |
e76a2646 | 8937 | |
0f8766b8 | 8938 | if (ctype == NULL_TREE && DECL_MAIN_P (decl)) |
faae18ab | 8939 | { |
848b92e1 | 8940 | if (processing_template_decl) |
cb9a3ff8 | 8941 | error ("cannot declare `::main' to be a template"); |
faae18ab | 8942 | if (inlinep) |
cb9a3ff8 | 8943 | error ("cannot declare `::main' to be inline"); |
f22967f3 | 8944 | if (!publicp) |
cb9a3ff8 | 8945 | error ("cannot declare `::main' to be static"); |
f22967f3 MM |
8946 | if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)), |
8947 | integer_type_node)) | |
8948 | error ("`main' must return `int'"); | |
faae18ab MS |
8949 | inlinep = 0; |
8950 | publicp = 1; | |
8951 | } | |
50a6dbd7 | 8952 | |
59e76fc6 JM |
8953 | /* Members of anonymous types and local classes have no linkage; make |
8954 | them internal. */ | |
1951a1b6 JM |
8955 | /* FIXME what if it gets a name from typedef? */ |
8956 | if (ctype && (TYPE_ANONYMOUS_P (ctype) | |
4f1c5b7d | 8957 | || decl_function_context (TYPE_MAIN_DECL (ctype)))) |
50a6dbd7 JM |
8958 | publicp = 0; |
8959 | ||
8960 | if (publicp) | |
8961 | { | |
8962 | /* [basic.link]: A name with no linkage (notably, the name of a class | |
8963 | or enumeration declared in a local scope) shall not be used to | |
8964 | declare an entity with linkage. | |
8965 | ||
8966 | Only check this for public decls for now. */ | |
8967 | t = no_linkage_check (TREE_TYPE (decl)); | |
8968 | if (t) | |
8969 | { | |
1951a1b6 | 8970 | if (TYPE_ANONYMOUS_P (t)) |
7f7c930e | 8971 | { |
eb68cb58 | 8972 | if (DECL_EXTERN_C_P (decl)) |
7f7c930e JM |
8973 | /* Allow this; it's pretty common in C. */; |
8974 | else | |
1951a1b6 | 8975 | { |
33bd39a2 | 8976 | pedwarn ("non-local function `%#D' uses anonymous type", |
1951a1b6 JM |
8977 | decl); |
8978 | if (DECL_ORIGINAL_TYPE (TYPE_NAME (t))) | |
8979 | cp_pedwarn_at ("\ | |
8980 | `%#D' does not refer to the unqualified type, so it is not used for linkage", | |
8981 | TYPE_NAME (t)); | |
8982 | } | |
7f7c930e | 8983 | } |
50a6dbd7 | 8984 | else |
33bd39a2 | 8985 | pedwarn ("non-local function `%#D' uses local type `%T'", |
cce2be43 | 8986 | decl, t); |
50a6dbd7 JM |
8987 | } |
8988 | } | |
8989 | ||
893de33c | 8990 | TREE_PUBLIC (decl) = publicp; |
faae18ab | 8991 | if (! publicp) |
893de33c JM |
8992 | { |
8993 | DECL_INTERFACE_KNOWN (decl) = 1; | |
8994 | DECL_NOT_REALLY_EXTERN (decl) = 1; | |
8995 | } | |
faae18ab | 8996 | |
acc72c37 | 8997 | /* If the declaration was declared inline, mark it as such. */ |
faae18ab | 8998 | if (inlinep) |
acc72c37 MM |
8999 | DECL_DECLARED_INLINE_P (decl) = 1; |
9000 | /* We inline functions that are explicitly declared inline, or, when | |
9001 | the user explicitly asks us to, all functions. */ | |
9002 | if (DECL_DECLARED_INLINE_P (decl) || flag_inline_trees == 2) | |
9003 | DECL_INLINE (decl) = 1; | |
8d08fdba MS |
9004 | |
9005 | DECL_EXTERNAL (decl) = 1; | |
9006 | if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE) | |
9007 | { | |
33bd39a2 | 9008 | error ("%smember function `%D' cannot have `%T' method qualifier", |
8d08fdba MS |
9009 | (ctype ? "static " : "non-"), decl, TREE_VALUE (quals)); |
9010 | quals = NULL_TREE; | |
9011 | } | |
9012 | ||
596ea4e5 | 9013 | if (IDENTIFIER_OPNAME_P (DECL_NAME (decl))) |
271e6f02 | 9014 | grok_op_properties (decl, friendp); |
8d08fdba | 9015 | |
4f1c5b7d | 9016 | if (ctype && decl_function_context (decl)) |
893de33c | 9017 | DECL_NO_STATIC_CHAIN (decl) = 1; |
e76a2646 | 9018 | |
42976354 BK |
9019 | for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t)) |
9020 | if (TREE_PURPOSE (t) | |
9021 | && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG) | |
9022 | { | |
856216bb | 9023 | has_default_arg = 1; |
42976354 BK |
9024 | break; |
9025 | } | |
9026 | ||
f9d94ea4 JM |
9027 | if (friendp |
9028 | && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR) | |
9029 | { | |
9030 | if (funcdef_flag) | |
33bd39a2 | 9031 | error |
8251199e | 9032 | ("defining explicit specialization `%D' in friend declaration", |
f9d94ea4 JM |
9033 | orig_declarator); |
9034 | else | |
9035 | { | |
76e57b45 NS |
9036 | tree fns = TREE_OPERAND (orig_declarator, 0); |
9037 | tree args = TREE_OPERAND (orig_declarator, 1); | |
d363e7bf | 9038 | |
7e2421f7 MM |
9039 | if (PROCESSING_REAL_TEMPLATE_DECL_P ()) |
9040 | { | |
9041 | /* Something like `template <class T> friend void f<T>()'. */ | |
33bd39a2 | 9042 | error ("invalid use of template-id `%D' in declaration of primary template", |
7e2421f7 | 9043 | orig_declarator); |
20496fa2 | 9044 | return NULL_TREE; |
7e2421f7 MM |
9045 | } |
9046 | ||
856216bb | 9047 | |
f9d94ea4 JM |
9048 | /* A friend declaration of the form friend void f<>(). Record |
9049 | the information in the TEMPLATE_ID_EXPR. */ | |
9050 | SET_DECL_IMPLICIT_INSTANTIATION (decl); | |
76e57b45 NS |
9051 | |
9052 | if (TREE_CODE (fns) == COMPONENT_REF) | |
9053 | { | |
9054 | /* Due to bison parser ickiness, we will have already looked | |
9055 | up an operator_name or PFUNCNAME within the current class | |
9056 | (see template_id in parse.y). If the current class contains | |
a1c65f9f | 9057 | such a name, we'll get a COMPONENT_REF here. Undo that. */ |
d363e7bf | 9058 | |
76e57b45 NS |
9059 | my_friendly_assert (TREE_TYPE (TREE_OPERAND (fns, 0)) |
9060 | == current_class_type, 20001120); | |
9061 | fns = TREE_OPERAND (fns, 1); | |
9062 | } | |
9063 | my_friendly_assert (TREE_CODE (fns) == IDENTIFIER_NODE | |
9064 | || TREE_CODE (fns) == LOOKUP_EXPR | |
9065 | || TREE_CODE (fns) == OVERLOAD, 20001120); | |
9066 | DECL_TEMPLATE_INFO (decl) = tree_cons (fns, args, NULL_TREE); | |
856216bb MM |
9067 | |
9068 | if (has_default_arg) | |
9069 | { | |
33bd39a2 | 9070 | error ("default arguments are not allowed in declaration of friend template specialization `%D'", |
856216bb MM |
9071 | decl); |
9072 | return NULL_TREE; | |
9073 | } | |
9074 | ||
9075 | if (inlinep) | |
9076 | { | |
33bd39a2 | 9077 | error ("`inline' is not allowed in declaration of friend template specialization `%D'", |
856216bb MM |
9078 | decl); |
9079 | return NULL_TREE; | |
9080 | } | |
f9d94ea4 | 9081 | } |
f84b4be9 | 9082 | } |
386b8a85 | 9083 | |
856216bb MM |
9084 | if (has_default_arg) |
9085 | add_defarg_fn (decl); | |
9086 | ||
1eb0072d JM |
9087 | if (funcdef_flag) |
9088 | /* Make the init_value nonzero so pushdecl knows this is not | |
9089 | tentative. error_mark_node is replaced later with the BLOCK. */ | |
9090 | DECL_INITIAL (decl) = error_mark_node; | |
9091 | ||
93ca4ba7 | 9092 | if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl)) |
1660cb3a JM |
9093 | TREE_NOTHROW (decl) = 1; |
9094 | ||
75650646 | 9095 | /* Caller will do the rest of this. */ |
8d08fdba MS |
9096 | if (check < 0) |
9097 | return decl; | |
9098 | ||
1951a1b6 | 9099 | if (flags == NO_SPECIAL && ctype && constructor_name (ctype) == declarator) |
74b846e0 MM |
9100 | DECL_CONSTRUCTOR_P (decl) = 1; |
9101 | ||
9102 | /* Function gets the ugly name, field gets the nice one. This call | |
9103 | may change the type of the function (because of default | |
9104 | parameters)! */ | |
9105 | if (ctype != NULL_TREE) | |
9106 | grokclassfn (ctype, decl, flags, quals); | |
9107 | ||
9108 | decl = check_explicit_specialization (orig_declarator, decl, | |
9109 | template_count, | |
9110 | 2 * (funcdef_flag != 0) + | |
9111 | 4 * (friendp != 0)); | |
9112 | if (decl == error_mark_node) | |
9113 | return NULL_TREE; | |
98c1c668 | 9114 | |
74b846e0 MM |
9115 | if (ctype != NULL_TREE |
9116 | && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl)) | |
9117 | && check) | |
8d08fdba | 9118 | { |
74b846e0 | 9119 | tree old_decl; |
8d08fdba | 9120 | |
74b846e0 | 9121 | old_decl = check_classfn (ctype, decl); |
8d08fdba | 9122 | |
74b846e0 MM |
9123 | if (old_decl && TREE_CODE (old_decl) == TEMPLATE_DECL) |
9124 | /* Because grokfndecl is always supposed to return a | |
9125 | FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT | |
9126 | here. We depend on our callers to figure out that its | |
9127 | really a template that's being returned. */ | |
9128 | old_decl = DECL_TEMPLATE_RESULT (old_decl); | |
6c30752f | 9129 | |
74b846e0 MM |
9130 | if (old_decl && DECL_STATIC_FUNCTION_P (old_decl) |
9131 | && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE) | |
5566b478 | 9132 | { |
74b846e0 MM |
9133 | /* Remove the `this' parm added by grokclassfn. |
9134 | XXX Isn't this done in start_function, too? */ | |
3afb32a4 | 9135 | revert_static_member_fn (decl); |
74b846e0 | 9136 | last_function_parms = TREE_CHAIN (last_function_parms); |
5566b478 | 9137 | } |
74b846e0 | 9138 | if (old_decl && DECL_ARTIFICIAL (old_decl)) |
33bd39a2 | 9139 | error ("definition of implicitly-declared `%D'", old_decl); |
8d08fdba | 9140 | |
74b846e0 | 9141 | if (old_decl) |
8d08fdba | 9142 | { |
74b846e0 MM |
9143 | /* Since we've smashed OLD_DECL to its |
9144 | DECL_TEMPLATE_RESULT, we must do the same to DECL. */ | |
9145 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
9146 | decl = DECL_TEMPLATE_RESULT (decl); | |
9147 | ||
9148 | /* Attempt to merge the declarations. This can fail, in | |
0e339752 | 9149 | the case of some invalid specialization declarations. */ |
74b846e0 | 9150 | if (!duplicate_decls (decl, old_decl)) |
33bd39a2 | 9151 | error ("no `%#D' member function declared in class `%T'", |
74b846e0 MM |
9152 | decl, ctype); |
9153 | return old_decl; | |
8d08fdba MS |
9154 | } |
9155 | } | |
74b846e0 MM |
9156 | |
9157 | if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl)) | |
9158 | return NULL_TREE; | |
9159 | ||
9160 | if (ctype == NULL_TREE || check) | |
9161 | return decl; | |
9162 | ||
9163 | if (virtualp) | |
cbb40945 | 9164 | DECL_VIRTUAL_P (decl) = 1; |
74b846e0 | 9165 | |
8d08fdba MS |
9166 | return decl; |
9167 | } | |
9168 | ||
9e259dd1 MM |
9169 | /* Create a VAR_DECL named NAME with the indicated TYPE. |
9170 | ||
9171 | If SCOPE is non-NULL, it is the class type or namespace containing | |
9172 | the variable. If SCOPE is NULL, the variable should is created in | |
9173 | the innermost enclosings scope. */ | |
9174 | ||
8d08fdba | 9175 | static tree |
9e259dd1 | 9176 | grokvardecl (type, name, specbits_in, initialized, constp, scope) |
8d08fdba | 9177 | tree type; |
9e259dd1 | 9178 | tree name; |
d2e5ee5c | 9179 | RID_BIT_TYPE *specbits_in; |
8d08fdba | 9180 | int initialized; |
a9aedbc2 | 9181 | int constp; |
9e259dd1 | 9182 | tree scope; |
8d08fdba MS |
9183 | { |
9184 | tree decl; | |
f7da6097 MS |
9185 | RID_BIT_TYPE specbits; |
9186 | ||
9e259dd1 MM |
9187 | my_friendly_assert (!name || TREE_CODE (name) == IDENTIFIER_NODE, |
9188 | 20020808); | |
9189 | ||
f7da6097 | 9190 | specbits = *specbits_in; |
8d08fdba | 9191 | |
9e259dd1 MM |
9192 | /* Compute the scope in which to place the variable. */ |
9193 | if (!scope) | |
8d08fdba | 9194 | { |
9e259dd1 MM |
9195 | /* An explicit "extern" specifier indicates a namespace-scope |
9196 | variable. */ | |
9197 | if (RIDBIT_SETP (RID_EXTERN, specbits)) | |
9198 | scope = current_namespace; | |
9199 | else if (!at_function_scope_p ()) | |
9200 | { | |
9201 | scope = current_scope (); | |
9202 | if (!scope) | |
9203 | scope = current_namespace; | |
9204 | } | |
9205 | } | |
9206 | ||
9207 | if (scope | |
9208 | && (/* If the variable is a namespace-scope variable declared in a | |
9209 | template, we need DECL_LANG_SPECIFIC. */ | |
9210 | (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl) | |
9211 | /* Similarly for namespace-scope variables with language linkage | |
9212 | other than C++. */ | |
9213 | || (TREE_CODE (scope) == NAMESPACE_DECL | |
9214 | && current_lang_name != lang_name_cplusplus) | |
9215 | /* Similarly for static data members. */ | |
9216 | || TYPE_P (scope))) | |
9217 | decl = build_lang_decl (VAR_DECL, name, type); | |
8d08fdba | 9218 | else |
9e259dd1 | 9219 | decl = build_decl (VAR_DECL, name, type); |
79c4d4b7 | 9220 | |
9e259dd1 MM |
9221 | if (scope && TREE_CODE (scope) == NAMESPACE_DECL) |
9222 | set_decl_namespace (decl, scope, 0); | |
9223 | else | |
9224 | DECL_CONTEXT (decl) = scope; | |
6060a796 | 9225 | |
9e259dd1 MM |
9226 | if (name && scope && current_lang_name != lang_name_c) |
9227 | /* We can't mangle lazily here because we don't have any | |
9228 | way to recover whether or not a variable was `extern | |
9229 | "C"' later. */ | |
9230 | mangle_decl (decl); | |
2c73f9f5 | 9231 | |
8d08fdba MS |
9232 | if (RIDBIT_SETP (RID_EXTERN, specbits)) |
9233 | { | |
9234 | DECL_THIS_EXTERN (decl) = 1; | |
9235 | DECL_EXTERNAL (decl) = !initialized; | |
9236 | } | |
9237 | ||
9238 | /* In class context, static means one per class, | |
9239 | public access, and static storage. */ | |
2b9dc906 | 9240 | if (DECL_CLASS_SCOPE_P (decl)) |
8d08fdba MS |
9241 | { |
9242 | TREE_PUBLIC (decl) = 1; | |
9243 | TREE_STATIC (decl) = 1; | |
5b605f68 | 9244 | DECL_EXTERNAL (decl) = 0; |
8d08fdba MS |
9245 | } |
9246 | /* At top level, either `static' or no s.c. makes a definition | |
9247 | (perhaps tentative), and absence of `static' makes it public. */ | |
a9aedbc2 | 9248 | else if (toplevel_bindings_p ()) |
8d08fdba | 9249 | { |
a9aedbc2 | 9250 | TREE_PUBLIC (decl) = (RIDBIT_NOTSETP (RID_STATIC, specbits) |
9fffd093 | 9251 | && (DECL_THIS_EXTERN (decl) || ! constp)); |
8d08fdba MS |
9252 | TREE_STATIC (decl) = ! DECL_EXTERNAL (decl); |
9253 | } | |
9254 | /* Not at top level, only `static' makes a static definition. */ | |
9255 | else | |
9256 | { | |
9257 | TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits); | |
9258 | TREE_PUBLIC (decl) = DECL_EXTERNAL (decl); | |
9259 | } | |
50a6dbd7 | 9260 | |
7a1f3f5f RH |
9261 | if (RIDBIT_SETP (RID_THREAD, specbits)) |
9262 | { | |
9263 | if (targetm.have_tls) | |
9264 | DECL_THREAD_LOCAL (decl) = 1; | |
9265 | else | |
9266 | /* A mere warning is sure to result in improper semantics | |
9267 | at runtime. Don't bother to allow this to compile. */ | |
9268 | error ("thread-local storage not supported for this target"); | |
9269 | } | |
9270 | ||
50a6dbd7 JM |
9271 | if (TREE_PUBLIC (decl)) |
9272 | { | |
9273 | /* [basic.link]: A name with no linkage (notably, the name of a class | |
9274 | or enumeration declared in a local scope) shall not be used to | |
9275 | declare an entity with linkage. | |
9276 | ||
9277 | Only check this for public decls for now. */ | |
9278 | tree t = no_linkage_check (TREE_TYPE (decl)); | |
9279 | if (t) | |
9280 | { | |
1951a1b6 | 9281 | if (TYPE_ANONYMOUS_P (t)) |
50a6dbd7 JM |
9282 | /* Ignore for now; `enum { foo } e' is pretty common. */; |
9283 | else | |
33bd39a2 | 9284 | pedwarn ("non-local variable `%#D' uses local type `%T'", |
50a6dbd7 JM |
9285 | decl, t); |
9286 | } | |
9287 | } | |
9288 | ||
8d08fdba MS |
9289 | return decl; |
9290 | } | |
9291 | ||
d8f8dca1 MM |
9292 | /* Create and return a canonical pointer to member function type, for |
9293 | TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */ | |
8d08fdba MS |
9294 | |
9295 | tree | |
9296 | build_ptrmemfunc_type (type) | |
9297 | tree type; | |
9298 | { | |
9299 | tree fields[4]; | |
9300 | tree t; | |
46cbda4a | 9301 | tree unqualified_variant = NULL_TREE; |
8d08fdba | 9302 | |
d48ebde1 NS |
9303 | if (type == error_mark_node) |
9304 | return type; | |
d363e7bf | 9305 | |
8d08fdba MS |
9306 | /* If a canonical type already exists for this type, use it. We use |
9307 | this method instead of type_hash_canon, because it only does a | |
9308 | simple equality check on the list of field members. */ | |
9309 | ||
9310 | if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type))) | |
9311 | return t; | |
9312 | ||
46cbda4a MM |
9313 | /* Make sure that we always have the unqualified pointer-to-member |
9314 | type first. */ | |
89d684bb | 9315 | if (cp_type_quals (type) != TYPE_UNQUALIFIED) |
68642fb6 | 9316 | unqualified_variant |
46cbda4a MM |
9317 | = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type)); |
9318 | ||
33848bb0 | 9319 | t = make_aggr_type (RECORD_TYPE); |
2c73f9f5 | 9320 | /* Let the front-end know this is a pointer to member function... */ |
db5ae43f | 9321 | TYPE_PTRMEMFUNC_FLAG (t) = 1; |
2c73f9f5 | 9322 | /* ... and not really an aggregate. */ |
7ddedda4 | 9323 | SET_IS_AGGR_TYPE (t, 0); |
8d08fdba | 9324 | |
1f84ec23 MM |
9325 | fields[0] = build_decl (FIELD_DECL, pfn_identifier, type); |
9326 | fields[1] = build_decl (FIELD_DECL, delta_identifier, | |
9327 | delta_type_node); | |
18ae7f63 | 9328 | finish_builtin_type (t, "__ptrmemfunc_type", fields, 1, ptr_type_node); |
8d08fdba | 9329 | |
8d08fdba MS |
9330 | /* Zap out the name so that the back-end will give us the debugging |
9331 | information for this anonymous RECORD_TYPE. */ | |
9332 | TYPE_NAME (t) = NULL_TREE; | |
9333 | ||
46cbda4a MM |
9334 | /* If this is not the unqualified form of this pointer-to-member |
9335 | type, set the TYPE_MAIN_VARIANT for this type to be the | |
9336 | unqualified type. Since they are actually RECORD_TYPEs that are | |
9337 | not variants of each other, we must do this manually. */ | |
89d684bb | 9338 | if (cp_type_quals (type) != TYPE_UNQUALIFIED) |
46cbda4a | 9339 | { |
89d684bb | 9340 | t = build_qualified_type (t, cp_type_quals (type)); |
46cbda4a MM |
9341 | TYPE_MAIN_VARIANT (t) = unqualified_variant; |
9342 | TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant); | |
9343 | TYPE_NEXT_VARIANT (unqualified_variant) = t; | |
9344 | } | |
9345 | ||
9346 | /* Cache this pointer-to-member type so that we can find it again | |
9347 | later. */ | |
8d08fdba MS |
9348 | TYPE_SET_PTRMEMFUNC_TYPE (type, t); |
9349 | ||
e92cc029 | 9350 | /* Seems to be wanted. */ |
8d08fdba | 9351 | CLASSTYPE_GOT_SEMICOLON (t) = 1; |
46cbda4a | 9352 | |
8d08fdba MS |
9353 | return t; |
9354 | } | |
9355 | ||
9e259dd1 MM |
9356 | /* Create and return a pointer to data member type. */ |
9357 | ||
9358 | tree | |
9359 | build_ptrmem_type (tree class_type, tree member_type) | |
9360 | { | |
9361 | return build_pointer_type (build_offset_type (class_type, member_type)); | |
9362 | } | |
9363 | ||
b17e2870 JM |
9364 | /* DECL is a VAR_DECL defined in-class, whose TYPE is also given. |
9365 | Check to see that the definition is valid. Issue appropriate error | |
9366 | messages. Return 1 if the definition is particularly bad, or 0 | |
9367 | otherwise. */ | |
9368 | ||
9369 | int | |
9370 | check_static_variable_definition (decl, type) | |
9371 | tree decl; | |
9372 | tree type; | |
9373 | { | |
9374 | /* Motion 10 at San Diego: If a static const integral data member is | |
9375 | initialized with an integral constant expression, the initializer | |
9376 | may appear either in the declaration (within the class), or in | |
9377 | the definition, but not both. If it appears in the class, the | |
9378 | member is a member constant. The file-scope definition is always | |
9379 | required. */ | |
9380 | if (CLASS_TYPE_P (type) || TREE_CODE (type) == REFERENCE_TYPE) | |
9381 | { | |
33bd39a2 | 9382 | error ("invalid in-class initialization of static data member of non-integral type `%T'", |
b17e2870 JM |
9383 | type); |
9384 | /* If we just return the declaration, crashes will sometimes | |
9385 | occur. We therefore return void_type_node, as if this was a | |
9386 | friend declaration, to cause callers to completely ignore | |
9387 | this declaration. */ | |
9388 | return 1; | |
9389 | } | |
9390 | else if (!CP_TYPE_CONST_P (type)) | |
33bd39a2 | 9391 | error ("ISO C++ forbids in-class initialization of non-const static member `%D'", |
b17e2870 JM |
9392 | decl); |
9393 | else if (pedantic && !INTEGRAL_TYPE_P (type)) | |
33bd39a2 | 9394 | pedwarn ("ISO C++ forbids initialization of member constant `%D' of non-integral type `%T'", decl, type); |
b17e2870 JM |
9395 | |
9396 | return 0; | |
9397 | } | |
9398 | ||
2848ad0f MM |
9399 | /* Given the SIZE (i.e., number of elements) in an array, compute an |
9400 | appropriate index type for the array. If non-NULL, NAME is the | |
9401 | name of the thing being declared. */ | |
9402 | ||
c95cd22e | 9403 | tree |
2848ad0f MM |
9404 | compute_array_index_type (name, size) |
9405 | tree name; | |
9406 | tree size; | |
9407 | { | |
9408 | tree itype; | |
9409 | ||
2848ad0f MM |
9410 | /* If this involves a template parameter, it will be a constant at |
9411 | instantiation time, but we don't know what the value is yet. | |
9412 | Even if no template parameters are involved, we may an expression | |
9413 | that is not a constant; we don't even simplify `1 + 2' when | |
9414 | processing a template. */ | |
9415 | if (processing_template_decl) | |
9416 | { | |
9417 | /* Resolve a qualified reference to an enumerator or static | |
9418 | const data member of ours. */ | |
9419 | if (TREE_CODE (size) == SCOPE_REF | |
9420 | && TREE_OPERAND (size, 0) == current_class_type) | |
9421 | { | |
9422 | tree t = lookup_field (current_class_type, | |
9423 | TREE_OPERAND (size, 1), 0, 0); | |
9424 | if (t) | |
9425 | size = t; | |
9426 | } | |
9427 | ||
9428 | return build_index_type (build_min (MINUS_EXPR, sizetype, | |
9429 | size, integer_one_node)); | |
9430 | } | |
9431 | ||
a1c65f9f | 9432 | /* The size might be the result of a cast. */ |
80f5bb34 MM |
9433 | STRIP_TYPE_NOPS (size); |
9434 | ||
9435 | /* It might be a const variable or enumeration constant. */ | |
9436 | size = decl_constant_value (size); | |
9437 | ||
2848ad0f MM |
9438 | /* The array bound must be an integer type. */ |
9439 | if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE | |
9440 | && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE | |
9441 | && TREE_CODE (TREE_TYPE (size)) != BOOLEAN_TYPE) | |
9442 | { | |
d67cdbc3 | 9443 | if (name) |
33bd39a2 | 9444 | error ("size of array `%D' has non-integer type", name); |
d67cdbc3 | 9445 | else |
33bd39a2 | 9446 | error ("size of array has non-integer type"); |
2848ad0f MM |
9447 | size = integer_one_node; |
9448 | } | |
9449 | ||
9450 | /* Normally, the array-bound will be a constant. */ | |
2bb5d995 | 9451 | if (TREE_CODE (size) == INTEGER_CST) |
2848ad0f MM |
9452 | { |
9453 | /* Check to see if the array bound overflowed. Make that an | |
9454 | error, no matter how generous we're being. */ | |
9455 | int old_flag_pedantic_errors = flag_pedantic_errors; | |
9456 | int old_pedantic = pedantic; | |
9457 | pedantic = flag_pedantic_errors = 1; | |
9458 | constant_expression_warning (size); | |
9459 | pedantic = old_pedantic; | |
9460 | flag_pedantic_errors = old_flag_pedantic_errors; | |
9461 | ||
9462 | /* An array must have a positive number of elements. */ | |
9463 | if (INT_CST_LT (size, integer_zero_node)) | |
9464 | { | |
d67cdbc3 | 9465 | if (name) |
33bd39a2 | 9466 | error ("size of array `%D' is negative", name); |
d67cdbc3 | 9467 | else |
33bd39a2 | 9468 | error ("size of array is negative"); |
2848ad0f MM |
9469 | size = integer_one_node; |
9470 | } | |
9471 | /* Except that an extension we allow zero-sized arrays. We | |
68642fb6 | 9472 | always allow them in system headers because glibc uses |
2848ad0f MM |
9473 | them. */ |
9474 | else if (integer_zerop (size) && pedantic && !in_system_header) | |
d67cdbc3 JM |
9475 | { |
9476 | if (name) | |
33bd39a2 | 9477 | pedwarn ("ISO C++ forbids zero-size array `%D'", name); |
d67cdbc3 | 9478 | else |
33bd39a2 | 9479 | pedwarn ("ISO C++ forbids zero-size array"); |
d67cdbc3 | 9480 | } |
2848ad0f | 9481 | } |
2bb5d995 JM |
9482 | else if (TREE_CONSTANT (size)) |
9483 | { | |
9484 | /* `(int) &fn' is not a valid array bound. */ | |
9485 | if (name) | |
33bd39a2 | 9486 | error ("size of array `%D' is not an integral constant-expression", |
2bb5d995 JM |
9487 | name); |
9488 | else | |
33bd39a2 | 9489 | error ("size of array is not an integral constant-expression"); |
2bb5d995 | 9490 | } |
2848ad0f MM |
9491 | |
9492 | /* Compute the index of the largest element in the array. It is | |
9493 | one less than the number of elements in the array. */ | |
9494 | itype | |
ab76ca54 MM |
9495 | = fold (cp_build_binary_op (MINUS_EXPR, |
9496 | cp_convert (ssizetype, size), | |
9497 | cp_convert (ssizetype, | |
9498 | integer_one_node))); | |
68642fb6 | 9499 | |
2848ad0f MM |
9500 | /* Check for variable-sized arrays. We allow such things as an |
9501 | extension, even though they are not allowed in ANSI/ISO C++. */ | |
9502 | if (!TREE_CONSTANT (itype)) | |
9503 | { | |
9504 | if (pedantic) | |
9505 | { | |
9506 | if (name) | |
33bd39a2 | 9507 | pedwarn ("ISO C++ forbids variable-size array `%D'", |
2848ad0f MM |
9508 | name); |
9509 | else | |
33bd39a2 | 9510 | pedwarn ("ISO C++ forbids variable-size array"); |
2848ad0f MM |
9511 | } |
9512 | ||
9513 | /* Create a variable-sized array index type. */ | |
9514 | itype = variable_size (itype); | |
9515 | } | |
9516 | /* Make sure that there was no overflow when creating to a signed | |
9517 | index type. (For example, on a 32-bit machine, an array with | |
9518 | size 2^32 - 1 is too big.) */ | |
9519 | else if (TREE_OVERFLOW (itype)) | |
9520 | { | |
9521 | error ("overflow in array dimension"); | |
9522 | TREE_OVERFLOW (itype) = 0; | |
9523 | } | |
68642fb6 | 9524 | |
2848ad0f MM |
9525 | /* Create and return the appropriate index type. */ |
9526 | return build_index_type (itype); | |
9527 | } | |
9528 | ||
9529 | /* Returns an ARRAY_TYPE for an array with SIZE elements of the | |
9530 | indicated TYPE. If non-NULL, NAME is the NAME of the declaration | |
9531 | with this type. */ | |
9532 | ||
9533 | static tree | |
9534 | create_array_type_for_decl (name, type, size) | |
9535 | tree name; | |
9536 | tree type; | |
9537 | tree size; | |
9538 | { | |
9539 | tree itype = NULL_TREE; | |
9540 | const char* error_msg; | |
9541 | ||
9542 | /* If things have already gone awry, bail now. */ | |
9543 | if (type == error_mark_node || size == error_mark_node) | |
9544 | return error_mark_node; | |
9545 | ||
9546 | /* Assume that everything will go OK. */ | |
9547 | error_msg = NULL; | |
9548 | ||
9549 | /* There are some types which cannot be array elements. */ | |
9550 | switch (TREE_CODE (type)) | |
9551 | { | |
9552 | case VOID_TYPE: | |
9553 | error_msg = "array of void"; | |
9554 | break; | |
9555 | ||
9556 | case FUNCTION_TYPE: | |
9557 | error_msg = "array of functions"; | |
9558 | break; | |
9559 | ||
9560 | case REFERENCE_TYPE: | |
9561 | error_msg = "array of references"; | |
9562 | break; | |
9563 | ||
9564 | case OFFSET_TYPE: | |
9565 | error_msg = "array of data members"; | |
9566 | break; | |
9567 | ||
9568 | case METHOD_TYPE: | |
9569 | error_msg = "array of function members"; | |
9570 | break; | |
9571 | ||
9572 | default: | |
9573 | break; | |
9574 | } | |
9575 | ||
9576 | /* If something went wrong, issue an error-message and return. */ | |
9577 | if (error_msg) | |
9578 | { | |
9579 | if (name) | |
33bd39a2 | 9580 | error ("declaration of `%D' as %s", name, error_msg); |
2848ad0f | 9581 | else |
33bd39a2 | 9582 | error ("creating %s", error_msg); |
2848ad0f MM |
9583 | |
9584 | return error_mark_node; | |
9585 | } | |
9586 | ||
9587 | /* [dcl.array] | |
68642fb6 | 9588 | |
2848ad0f MM |
9589 | The constant expressions that specify the bounds of the arrays |
9590 | can be omitted only for the first member of the sequence. */ | |
9591 | if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)) | |
9592 | { | |
b3faacfd | 9593 | if (name) |
33bd39a2 | 9594 | error ("declaration of `%D' as multidimensional array must have bounds for all dimensions except the first", |
b3faacfd KL |
9595 | name); |
9596 | else | |
33bd39a2 | 9597 | error ("multidimensional array must have bounds for all dimensions except the first"); |
2848ad0f MM |
9598 | |
9599 | return error_mark_node; | |
9600 | } | |
9601 | ||
9602 | /* Figure out the index type for the array. */ | |
9603 | if (size) | |
9604 | itype = compute_array_index_type (name, size); | |
9605 | ||
9606 | return build_cplus_array_type (type, itype); | |
9607 | } | |
9608 | ||
3dbc07b6 MM |
9609 | /* Check that it's OK to declare a function with the indicated TYPE. |
9610 | SFK indicates the kind of special function (if any) that this | |
1f84ec23 | 9611 | function is. OPTYPE is the type given in a conversion operator |
3dbc07b6 MM |
9612 | declaration. Returns the actual return type of the function; that |
9613 | may be different than TYPE if an error occurs, or for certain | |
9614 | special functions. */ | |
9615 | ||
9616 | static tree | |
1f84ec23 | 9617 | check_special_function_return_type (sfk, type, optype) |
3dbc07b6 MM |
9618 | special_function_kind sfk; |
9619 | tree type; | |
3dbc07b6 MM |
9620 | tree optype; |
9621 | { | |
9622 | switch (sfk) | |
9623 | { | |
9624 | case sfk_constructor: | |
9625 | if (type) | |
33bd39a2 | 9626 | error ("return type specification for constructor invalid"); |
5362b086 | 9627 | |
1f84ec23 | 9628 | type = void_type_node; |
3dbc07b6 MM |
9629 | break; |
9630 | ||
9631 | case sfk_destructor: | |
9632 | if (type) | |
33bd39a2 | 9633 | error ("return type specification for destructor invalid"); |
3dbc07b6 MM |
9634 | type = void_type_node; |
9635 | break; | |
9636 | ||
9637 | case sfk_conversion: | |
9638 | if (type && !same_type_p (type, optype)) | |
33bd39a2 | 9639 | error ("operator `%T' declared to return `%T'", optype, type); |
3dbc07b6 | 9640 | else if (type) |
33bd39a2 | 9641 | pedwarn ("return type specified for `operator %T'", optype); |
3dbc07b6 MM |
9642 | type = optype; |
9643 | break; | |
9644 | ||
9645 | default: | |
a98facb0 | 9646 | abort (); |
3dbc07b6 MM |
9647 | break; |
9648 | } | |
9649 | ||
9650 | return type; | |
9651 | } | |
9652 | ||
8d08fdba MS |
9653 | /* Given declspecs and a declarator, |
9654 | determine the name and type of the object declared | |
9655 | and construct a ..._DECL node for it. | |
9656 | (In one case we can return a ..._TYPE node instead. | |
9657 | For invalid input we sometimes return 0.) | |
9658 | ||
9659 | DECLSPECS is a chain of tree_list nodes whose value fields | |
9660 | are the storage classes and type specifiers. | |
9661 | ||
9662 | DECL_CONTEXT says which syntactic context this declaration is in: | |
9663 | NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL. | |
9664 | FUNCDEF for a function definition. Like NORMAL but a few different | |
9665 | error messages in each case. Return value may be zero meaning | |
9666 | this definition is too screwy to try to parse. | |
9667 | MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to | |
9668 | handle member functions (which have FIELD context). | |
9669 | Return value may be zero meaning this definition is too screwy to | |
9670 | try to parse. | |
9671 | PARM for a parameter declaration (either within a function prototype | |
9672 | or before a function body). Make a PARM_DECL, or return void_type_node. | |
db5ae43f | 9673 | CATCHPARM for a parameter declaration before a catch clause. |
8d08fdba MS |
9674 | TYPENAME if for a typename (in a cast or sizeof). |
9675 | Don't make a DECL node; just return the ..._TYPE node. | |
9676 | FIELD for a struct or union field; make a FIELD_DECL. | |
9677 | BITFIELD for a field with specified width. | |
9678 | INITIALIZED is 1 if the decl has an initializer. | |
9679 | ||
91d231cb JM |
9680 | ATTRLIST is a pointer to the list of attributes, which may be NULL |
9681 | if there are none; *ATTRLIST may be modified if attributes from inside | |
9682 | the declarator should be applied to the declaration. | |
b17e2870 | 9683 | |
70adf8a9 | 9684 | In the TYPENAME case, DECLARATOR is really an abstract declarator. |
8d08fdba MS |
9685 | It may also be so in the PARM case, for a prototype where the |
9686 | argument type is specified but not the name. | |
9687 | ||
9688 | This function is where the complicated C meanings of `static' | |
9689 | and `extern' are interpreted. | |
9690 | ||
9691 | For C++, if there is any monkey business to do, the function which | |
9692 | calls this one must do it, i.e., prepending instance variables, | |
9693 | renaming overloaded function names, etc. | |
9694 | ||
9695 | Note that for this C++, it is an error to define a method within a class | |
9696 | which does not belong to that class. | |
9697 | ||
9698 | Except in the case where SCOPE_REFs are implicitly known (such as | |
9699 | methods within a class being redundantly qualified), | |
9700 | declarations which involve SCOPE_REFs are returned as SCOPE_REFs | |
9701 | (class_name::decl_name). The caller must also deal with this. | |
9702 | ||
9703 | If a constructor or destructor is seen, and the context is FIELD, | |
9704 | then the type gains the attribute TREE_HAS_x. If such a declaration | |
9705 | is erroneous, NULL_TREE is returned. | |
9706 | ||
9707 | QUALS is used only for FUNCDEF and MEMFUNCDEF cases. For a member | |
535233a8 NS |
9708 | function, these are the qualifiers to give to the `this' pointer. We |
9709 | apply TYPE_QUAL_RESTRICT to the this ptr, not the object. | |
8d08fdba MS |
9710 | |
9711 | May return void_type_node if the declarator turned out to be a friend. | |
9712 | See grokfield for details. */ | |
9713 | ||
8d08fdba | 9714 | tree |
c11b6f21 | 9715 | grokdeclarator (declarator, declspecs, decl_context, initialized, attrlist) |
8d08fdba MS |
9716 | tree declspecs; |
9717 | tree declarator; | |
9718 | enum decl_context decl_context; | |
9719 | int initialized; | |
91d231cb | 9720 | tree *attrlist; |
8d08fdba MS |
9721 | { |
9722 | RID_BIT_TYPE specbits; | |
9723 | int nclasses = 0; | |
9724 | tree spec; | |
9725 | tree type = NULL_TREE; | |
9726 | int longlong = 0; | |
91063b51 | 9727 | int type_quals; |
db5ae43f | 9728 | int virtualp, explicitp, friendp, inlinep, staticp; |
8d08fdba MS |
9729 | int explicit_int = 0; |
9730 | int explicit_char = 0; | |
37c46b43 | 9731 | int defaulted_int = 0; |
270d8c65 | 9732 | int extern_langp = 0; |
d5614afb | 9733 | tree dependant_name = NULL_TREE; |
270d8c65 | 9734 | |
8d08fdba | 9735 | tree typedef_decl = NULL_TREE; |
9c0758dd | 9736 | const char *name; |
8d08fdba MS |
9737 | tree typedef_type = NULL_TREE; |
9738 | int funcdef_flag = 0; | |
9739 | enum tree_code innermost_code = ERROR_MARK; | |
9740 | int bitfield = 0; | |
6125f3be DE |
9741 | #if 0 |
9742 | /* See the code below that used this. */ | |
91d231cb | 9743 | tree decl_attr = NULL_TREE; |
6125f3be | 9744 | #endif |
8d08fdba MS |
9745 | /* Set this to error_mark_node for FIELD_DECLs we could not handle properly. |
9746 | All FIELD_DECLs we build here have `init' put into their DECL_INITIAL. */ | |
9747 | tree init = NULL_TREE; | |
9748 | ||
9749 | /* Keep track of what sort of function is being processed | |
9750 | so that we can warn about default return values, or explicit | |
9751 | return values which do not match prescribed defaults. */ | |
3dbc07b6 | 9752 | special_function_kind sfk = sfk_none; |
8d08fdba MS |
9753 | |
9754 | tree dname = NULL_TREE; | |
9755 | tree ctype = current_class_type; | |
9756 | tree ctor_return_type = NULL_TREE; | |
9757 | enum overload_flags flags = NO_SPECIAL; | |
8d08fdba | 9758 | tree quals = NULL_TREE; |
c11b6f21 | 9759 | tree raises = NULL_TREE; |
386b8a85 | 9760 | int template_count = 0; |
2c73f9f5 | 9761 | tree in_namespace = NULL_TREE; |
91d231cb | 9762 | tree returned_attrs = NULL_TREE; |
8d08fdba MS |
9763 | |
9764 | RIDBIT_RESET_ALL (specbits); | |
9765 | if (decl_context == FUNCDEF) | |
9766 | funcdef_flag = 1, decl_context = NORMAL; | |
9767 | else if (decl_context == MEMFUNCDEF) | |
9768 | funcdef_flag = -1, decl_context = FIELD; | |
9769 | else if (decl_context == BITFIELD) | |
9770 | bitfield = 1, decl_context = FIELD; | |
9771 | ||
8d08fdba MS |
9772 | /* Look inside a declarator for the name being declared |
9773 | and get it as a string, for an error message. */ | |
9774 | { | |
be99da77 MS |
9775 | tree *next = &declarator; |
9776 | register tree decl; | |
8d08fdba MS |
9777 | name = NULL; |
9778 | ||
be99da77 MS |
9779 | while (next && *next) |
9780 | { | |
9781 | decl = *next; | |
9782 | switch (TREE_CODE (decl)) | |
8d08fdba | 9783 | { |
52fbc847 JM |
9784 | case TREE_LIST: |
9785 | /* For attributes. */ | |
9786 | next = &TREE_VALUE (decl); | |
9787 | break; | |
9788 | ||
be99da77 MS |
9789 | case COND_EXPR: |
9790 | ctype = NULL_TREE; | |
9791 | next = &TREE_OPERAND (decl, 0); | |
9792 | break; | |
8d08fdba | 9793 | |
2c73f9f5 | 9794 | case BIT_NOT_EXPR: /* For C++ destructors! */ |
8d08fdba | 9795 | { |
be99da77 MS |
9796 | tree name = TREE_OPERAND (decl, 0); |
9797 | tree rename = NULL_TREE; | |
9798 | ||
9799 | my_friendly_assert (flags == NO_SPECIAL, 152); | |
9800 | flags = DTOR_FLAG; | |
3dbc07b6 | 9801 | sfk = sfk_destructor; |
5566b478 MS |
9802 | if (TREE_CODE (name) == TYPE_DECL) |
9803 | TREE_OPERAND (decl, 0) = name = constructor_name (name); | |
be99da77 MS |
9804 | my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153); |
9805 | if (ctype == NULL_TREE) | |
9806 | { | |
9807 | if (current_class_type == NULL_TREE) | |
9808 | { | |
8251199e | 9809 | error ("destructors must be member functions"); |
be99da77 MS |
9810 | flags = NO_SPECIAL; |
9811 | } | |
9812 | else | |
9813 | { | |
9814 | tree t = constructor_name (current_class_name); | |
9815 | if (t != name) | |
9816 | rename = t; | |
9817 | } | |
9818 | } | |
8d08fdba | 9819 | else |
be99da77 MS |
9820 | { |
9821 | tree t = constructor_name (ctype); | |
9822 | if (t != name) | |
9823 | rename = t; | |
9824 | } | |
51c184be | 9825 | |
be99da77 | 9826 | if (rename) |
39211cd5 | 9827 | { |
33bd39a2 | 9828 | error ("destructor `%T' must match class name `%T'", |
5566b478 | 9829 | name, rename); |
be99da77 | 9830 | TREE_OPERAND (decl, 0) = rename; |
39211cd5 | 9831 | } |
be99da77 | 9832 | next = &name; |
51c184be | 9833 | } |
be99da77 | 9834 | break; |
8d08fdba | 9835 | |
be99da77 | 9836 | case ADDR_EXPR: /* C++ reference declaration */ |
a1c65f9f | 9837 | /* Fall through. */ |
be99da77 MS |
9838 | case ARRAY_REF: |
9839 | case INDIRECT_REF: | |
9840 | ctype = NULL_TREE; | |
9841 | innermost_code = TREE_CODE (decl); | |
9842 | next = &TREE_OPERAND (decl, 0); | |
9843 | break; | |
8d08fdba | 9844 | |
be99da77 | 9845 | case CALL_EXPR: |
43f887f9 | 9846 | if (parmlist_is_exprlist (CALL_DECLARATOR_PARMS (decl))) |
8d08fdba | 9847 | { |
b17e2870 JM |
9848 | /* This is actually a variable declaration using |
9849 | constructor syntax. We need to call start_decl and | |
9850 | cp_finish_decl so we can get the variable | |
9851 | initialized... */ | |
9852 | ||
91d231cb | 9853 | tree attributes; |
be99da77 | 9854 | |
9e259dd1 MM |
9855 | if (decl_context != NORMAL) |
9856 | { | |
9857 | error ("variable declaration is not allowed here"); | |
9858 | return error_mark_node; | |
9859 | } | |
9860 | ||
be99da77 | 9861 | *next = TREE_OPERAND (decl, 0); |
43f887f9 | 9862 | init = CALL_DECLARATOR_PARMS (decl); |
be99da77 | 9863 | |
b17e2870 JM |
9864 | if (attrlist) |
9865 | { | |
91d231cb | 9866 | attributes = *attrlist; |
b17e2870 JM |
9867 | } |
9868 | else | |
9869 | { | |
9870 | attributes = NULL_TREE; | |
b17e2870 JM |
9871 | } |
9872 | ||
9873 | decl = start_decl (declarator, declspecs, 1, | |
91d231cb | 9874 | attributes, NULL_TREE); |
9fc336c7 | 9875 | decl_type_access_control (decl); |
1eb0072d JM |
9876 | if (decl) |
9877 | { | |
9878 | /* Look for __unused__ attribute */ | |
9879 | if (TREE_USED (TREE_TYPE (decl))) | |
9880 | TREE_USED (decl) = 1; | |
9881 | finish_decl (decl, init, NULL_TREE); | |
9882 | } | |
9883 | else | |
33bd39a2 | 9884 | error ("invalid declarator"); |
9e259dd1 | 9885 | return NULL_TREE; |
8d08fdba | 9886 | } |
be99da77 MS |
9887 | innermost_code = TREE_CODE (decl); |
9888 | if (decl_context == FIELD && ctype == NULL_TREE) | |
9889 | ctype = current_class_type; | |
45537677 | 9890 | if (ctype |
c11b6f21 | 9891 | && TREE_OPERAND (decl, 0) |
45537677 | 9892 | && (TREE_CODE (TREE_OPERAND (decl, 0)) == TYPE_DECL |
8ba658ee MM |
9893 | && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 0)), |
9894 | ctype))) | |
be99da77 MS |
9895 | TREE_OPERAND (decl, 0) = constructor_name (ctype); |
9896 | next = &TREE_OPERAND (decl, 0); | |
9897 | decl = *next; | |
9898 | if (ctype != NULL_TREE | |
9899 | && decl != NULL_TREE && flags != DTOR_FLAG | |
9900 | && decl == constructor_name (ctype)) | |
8d08fdba | 9901 | { |
3dbc07b6 | 9902 | sfk = sfk_constructor; |
be99da77 | 9903 | ctor_return_type = ctype; |
8d08fdba | 9904 | } |
be99da77 MS |
9905 | ctype = NULL_TREE; |
9906 | break; | |
68642fb6 | 9907 | |
386b8a85 JM |
9908 | case TEMPLATE_ID_EXPR: |
9909 | { | |
9910 | tree fns = TREE_OPERAND (decl, 0); | |
9911 | ||
9912 | if (TREE_CODE (fns) == LOOKUP_EXPR) | |
9913 | fns = TREE_OPERAND (fns, 0); | |
9914 | ||
8f032717 MM |
9915 | dname = fns; |
9916 | if (TREE_CODE (dname) == COMPONENT_REF) | |
9917 | dname = TREE_OPERAND (dname, 1); | |
9918 | if (TREE_CODE (dname) != IDENTIFIER_NODE) | |
9919 | { | |
9920 | my_friendly_assert (is_overloaded_fn (dname), | |
9921 | 19990331); | |
9922 | dname = DECL_NAME (get_first_fn (dname)); | |
9923 | } | |
386b8a85 | 9924 | } |
a1c65f9f | 9925 | /* Fall through. */ |
be99da77 MS |
9926 | |
9927 | case IDENTIFIER_NODE: | |
386b8a85 JM |
9928 | if (TREE_CODE (decl) == IDENTIFIER_NODE) |
9929 | dname = decl; | |
9930 | ||
be99da77 MS |
9931 | next = 0; |
9932 | ||
0e5921e8 | 9933 | if (C_IS_RESERVED_WORD (dname)) |
8d08fdba | 9934 | { |
33bd39a2 | 9935 | error ("declarator-id missing; using reserved word `%D'", |
be99da77 MS |
9936 | dname); |
9937 | name = IDENTIFIER_POINTER (dname); | |
8d08fdba | 9938 | } |
596ea4e5 | 9939 | else if (!IDENTIFIER_TYPENAME_P (dname)) |
be99da77 | 9940 | name = IDENTIFIER_POINTER (dname); |
8d08fdba MS |
9941 | else |
9942 | { | |
596ea4e5 AS |
9943 | my_friendly_assert (flags == NO_SPECIAL, 154); |
9944 | flags = TYPENAME_FLAG; | |
9945 | ctor_return_type = TREE_TYPE (dname); | |
9946 | sfk = sfk_conversion; | |
9947 | if (IDENTIFIER_GLOBAL_VALUE (dname) | |
5362b086 | 9948 | && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (dname)) |
596ea4e5 AS |
9949 | == TYPE_DECL)) |
9950 | name = IDENTIFIER_POINTER (dname); | |
9951 | else | |
9952 | name = "<invalid operator>"; | |
8d08fdba | 9953 | } |
be99da77 | 9954 | break; |
8d08fdba | 9955 | |
be99da77 MS |
9956 | /* C++ extension */ |
9957 | case SCOPE_REF: | |
9958 | { | |
9959 | /* Perform error checking, and decide on a ctype. */ | |
9960 | tree cname = TREE_OPERAND (decl, 0); | |
9961 | if (cname == NULL_TREE) | |
9962 | ctype = NULL_TREE; | |
2c73f9f5 ML |
9963 | else if (TREE_CODE (cname) == NAMESPACE_DECL) |
9964 | { | |
9965 | ctype = NULL_TREE; | |
9966 | in_namespace = TREE_OPERAND (decl, 0); | |
9967 | TREE_OPERAND (decl, 0) = NULL_TREE; | |
9968 | } | |
be99da77 MS |
9969 | else if (! is_aggr_type (cname, 1)) |
9970 | TREE_OPERAND (decl, 0) = NULL_TREE; | |
9971 | /* Must test TREE_OPERAND (decl, 1), in case user gives | |
9972 | us `typedef (class::memfunc)(int); memfunc *memfuncptr;' */ | |
9973 | else if (TREE_OPERAND (decl, 1) | |
9974 | && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF) | |
9975 | ctype = cname; | |
73b0fce8 | 9976 | else if (TREE_CODE (cname) == TEMPLATE_TYPE_PARM |
a1281f45 | 9977 | || TREE_CODE (cname) == BOUND_TEMPLATE_TEMPLATE_PARM) |
5566b478 | 9978 | { |
d5614afb NS |
9979 | /* This might be declaring a member of a template |
9980 | parm to be a friend. */ | |
9981 | ctype = cname; | |
9982 | dependant_name = TREE_OPERAND (decl, 1); | |
5566b478 | 9983 | } |
be99da77 MS |
9984 | else if (ctype == NULL_TREE) |
9985 | ctype = cname; | |
9986 | else if (TREE_COMPLEXITY (decl) == current_class_depth) | |
9987 | TREE_OPERAND (decl, 0) = ctype; | |
9988 | else | |
9989 | { | |
9990 | if (! UNIQUELY_DERIVED_FROM_P (cname, ctype)) | |
9991 | { | |
33bd39a2 | 9992 | error ("type `%T' is not derived from type `%T'", |
be99da77 MS |
9993 | cname, ctype); |
9994 | TREE_OPERAND (decl, 0) = NULL_TREE; | |
9995 | } | |
9996 | else | |
9997 | ctype = cname; | |
9998 | } | |
9999 | ||
c91a56d2 | 10000 | if (ctype && TREE_CODE (TREE_OPERAND (decl, 1)) == TYPE_DECL |
8ba658ee MM |
10001 | && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 1)), |
10002 | ctype)) | |
be99da77 MS |
10003 | TREE_OPERAND (decl, 1) = constructor_name (ctype); |
10004 | next = &TREE_OPERAND (decl, 1); | |
10005 | decl = *next; | |
10006 | if (ctype) | |
10007 | { | |
10008 | if (TREE_CODE (decl) == IDENTIFIER_NODE | |
10009 | && constructor_name (ctype) == decl) | |
10010 | { | |
3dbc07b6 | 10011 | sfk = sfk_constructor; |
be99da77 MS |
10012 | ctor_return_type = ctype; |
10013 | } | |
10014 | else if (TREE_CODE (decl) == BIT_NOT_EXPR | |
10015 | && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE | |
8ba658ee MM |
10016 | && constructor_name_p (TREE_OPERAND (decl, 0), |
10017 | ctype)) | |
be99da77 | 10018 | { |
3dbc07b6 | 10019 | sfk = sfk_destructor; |
be99da77 MS |
10020 | ctor_return_type = ctype; |
10021 | flags = DTOR_FLAG; | |
10022 | TREE_OPERAND (decl, 0) = constructor_name (ctype); | |
10023 | next = &TREE_OPERAND (decl, 0); | |
10024 | } | |
10025 | } | |
10026 | } | |
10027 | break; | |
10028 | ||
10029 | case ERROR_MARK: | |
10030 | next = 0; | |
10031 | break; | |
10032 | ||
45537677 MS |
10033 | case TYPE_DECL: |
10034 | /* Parse error puts this typespec where | |
10035 | a declarator should go. */ | |
33bd39a2 | 10036 | error ("`%T' specified as declarator-id", DECL_NAME (decl)); |
45537677 | 10037 | if (TREE_TYPE (decl) == current_class_type) |
33bd39a2 | 10038 | error (" perhaps you want `%T' for a constructor", |
45537677 MS |
10039 | current_class_name); |
10040 | dname = DECL_NAME (decl); | |
10041 | name = IDENTIFIER_POINTER (dname); | |
10042 | ||
e92cc029 | 10043 | /* Avoid giving two errors for this. */ |
45537677 MS |
10044 | IDENTIFIER_CLASS_VALUE (dname) = NULL_TREE; |
10045 | ||
58010b57 | 10046 | declspecs = tree_cons (NULL_TREE, integer_type_node, declspecs); |
45537677 MS |
10047 | *next = dname; |
10048 | next = 0; | |
10049 | break; | |
10050 | ||
50ad9642 MM |
10051 | case BASELINK: |
10052 | next = &BASELINK_FUNCTIONS (decl); | |
10053 | break; | |
10054 | ||
be99da77 | 10055 | default: |
dd29188b | 10056 | my_friendly_assert (0, 20020917); |
8d08fdba | 10057 | } |
be99da77 | 10058 | } |
8d08fdba MS |
10059 | } |
10060 | ||
10061 | /* A function definition's declarator must have the form of | |
10062 | a function declarator. */ | |
10063 | ||
10064 | if (funcdef_flag && innermost_code != CALL_EXPR) | |
10065 | return 0; | |
10066 | ||
e1cd6e56 MS |
10067 | if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG) |
10068 | && innermost_code != CALL_EXPR | |
10069 | && ! (ctype && declspecs == NULL_TREE)) | |
10070 | { | |
33bd39a2 | 10071 | error ("declaration of `%D' as non-function", dname); |
e1cd6e56 MS |
10072 | return void_type_node; |
10073 | } | |
10074 | ||
8d08fdba MS |
10075 | /* Anything declared one level down from the top level |
10076 | must be one of the parameters of a function | |
10077 | (because the body is at least two levels down). */ | |
10078 | ||
10079 | /* This heuristic cannot be applied to C++ nodes! Fixed, however, | |
10080 | by not allowing C++ class definitions to specify their parameters | |
10081 | with xdecls (must be spec.d in the parmlist). | |
10082 | ||
10083 | Since we now wait to push a class scope until we are sure that | |
10084 | we are in a legitimate method context, we must set oldcname | |
a9aedbc2 MS |
10085 | explicitly (since current_class_name is not yet alive). |
10086 | ||
10087 | We also want to avoid calling this a PARM if it is in a namespace. */ | |
8d08fdba | 10088 | |
9188c363 | 10089 | if (decl_context == NORMAL && !toplevel_bindings_p ()) |
a9aedbc2 | 10090 | { |
e2500fed | 10091 | struct cp_binding_level *b = current_binding_level; |
a9aedbc2 MS |
10092 | current_binding_level = b->level_chain; |
10093 | if (current_binding_level != 0 && toplevel_bindings_p ()) | |
10094 | decl_context = PARM; | |
10095 | current_binding_level = b; | |
10096 | } | |
8d08fdba | 10097 | |
34fc957d NS |
10098 | if (name == NULL) |
10099 | name = decl_context == PARM ? "parameter" : "type name"; | |
68642fb6 | 10100 | |
8d08fdba MS |
10101 | /* Look through the decl specs and record which ones appear. |
10102 | Some typespecs are defined as built-in typenames. | |
10103 | Others, the ones that are modifiers of other types, | |
10104 | are represented by bits in SPECBITS: set the bits for | |
10105 | the modifiers that appear. Storage class keywords are also in SPECBITS. | |
10106 | ||
10107 | If there is a typedef name or a type, store the type in TYPE. | |
10108 | This includes builtin typedefs such as `int'. | |
10109 | ||
10110 | Set EXPLICIT_INT if the type is `int' or `char' and did not | |
10111 | come from a user typedef. | |
10112 | ||
10113 | Set LONGLONG if `long' is mentioned twice. | |
10114 | ||
10115 | For C++, constructors and destructors have their own fast treatment. */ | |
10116 | ||
10117 | for (spec = declspecs; spec; spec = TREE_CHAIN (spec)) | |
10118 | { | |
10119 | register int i; | |
10120 | register tree id; | |
10121 | ||
10122 | /* Certain parse errors slip through. For example, | |
10123 | `int class;' is not caught by the parser. Try | |
10124 | weakly to recover here. */ | |
10125 | if (TREE_CODE (spec) != TREE_LIST) | |
10126 | return 0; | |
10127 | ||
10128 | id = TREE_VALUE (spec); | |
10129 | ||
e23bd218 IR |
10130 | /* If the entire declaration is itself tagged as deprecated then |
10131 | suppress reports of deprecated items. */ | |
10132 | if (!adding_implicit_members && id && TREE_DEPRECATED (id)) | |
10133 | { | |
10134 | if (deprecated_state != DEPRECATED_SUPPRESS) | |
10135 | warn_deprecated_use (id); | |
10136 | } | |
10137 | ||
8d08fdba MS |
10138 | if (TREE_CODE (id) == IDENTIFIER_NODE) |
10139 | { | |
a3203465 MS |
10140 | if (id == ridpointers[(int) RID_INT] |
10141 | || id == ridpointers[(int) RID_CHAR] | |
10142 | || id == ridpointers[(int) RID_BOOL] | |
10143 | || id == ridpointers[(int) RID_WCHAR]) | |
8d08fdba MS |
10144 | { |
10145 | if (type) | |
8ccc31eb MS |
10146 | { |
10147 | if (id == ridpointers[(int) RID_BOOL]) | |
8251199e | 10148 | error ("`bool' is now a keyword"); |
8ccc31eb | 10149 | else |
33bd39a2 | 10150 | error ("extraneous `%T' ignored", id); |
8ccc31eb | 10151 | } |
8d08fdba MS |
10152 | else |
10153 | { | |
a3203465 MS |
10154 | if (id == ridpointers[(int) RID_INT]) |
10155 | explicit_int = 1; | |
10156 | else if (id == ridpointers[(int) RID_CHAR]) | |
10157 | explicit_char = 1; | |
8d08fdba MS |
10158 | type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id)); |
10159 | } | |
10160 | goto found; | |
10161 | } | |
e92cc029 | 10162 | /* C++ aggregate types. */ |
8d08fdba MS |
10163 | if (IDENTIFIER_HAS_TYPE_VALUE (id)) |
10164 | { | |
10165 | if (type) | |
33bd39a2 | 10166 | error ("multiple declarations `%T' and `%T'", type, id); |
8d08fdba MS |
10167 | else |
10168 | type = IDENTIFIER_TYPE_VALUE (id); | |
10169 | goto found; | |
10170 | } | |
10171 | ||
f376e137 | 10172 | for (i = (int) RID_FIRST_MODIFIER; i <= (int) RID_LAST_MODIFIER; i++) |
8d08fdba MS |
10173 | { |
10174 | if (ridpointers[i] == id) | |
10175 | { | |
10176 | if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits)) | |
10177 | { | |
795add94 | 10178 | if (pedantic && ! in_system_header && warn_long_long) |
cb9a3ff8 | 10179 | pedwarn ("ISO C++ does not support `long long'"); |
9a3b49ac | 10180 | if (longlong) |
8251199e | 10181 | error ("`long long long' is too long for GCC"); |
8d08fdba MS |
10182 | else |
10183 | longlong = 1; | |
10184 | } | |
10185 | else if (RIDBIT_SETP (i, specbits)) | |
8251199e | 10186 | pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id)); |
7a1f3f5f RH |
10187 | |
10188 | /* Diagnose "__thread extern". Recall that this list | |
10189 | is in the reverse order seen in the text. */ | |
10190 | if (i == (int)RID_THREAD) | |
10191 | { | |
10192 | if (RIDBIT_SETP (RID_EXTERN, specbits)) | |
10193 | error ("`__thread' before `extern'"); | |
10194 | if (RIDBIT_SETP (RID_STATIC, specbits)) | |
10195 | error ("`__thread' before `static'"); | |
10196 | } | |
10197 | ||
270d8c65 NS |
10198 | if (i == (int)RID_EXTERN |
10199 | && TREE_PURPOSE (spec) == error_mark_node) | |
10200 | /* This extern was part of a language linkage. */ | |
10201 | extern_langp = 1; | |
7a1f3f5f | 10202 | |
8d08fdba MS |
10203 | RIDBIT_SET (i, specbits); |
10204 | goto found; | |
10205 | } | |
10206 | } | |
10207 | } | |
1899c3a4 | 10208 | else if (TREE_CODE (id) == TYPE_DECL) |
45537677 MS |
10209 | { |
10210 | if (type) | |
33bd39a2 | 10211 | error ("multiple declarations `%T' and `%T'", type, |
45537677 MS |
10212 | TREE_TYPE (id)); |
10213 | else | |
5566b478 MS |
10214 | { |
10215 | type = TREE_TYPE (id); | |
10216 | TREE_VALUE (spec) = type; | |
8d6e459d | 10217 | typedef_decl = id; |
5566b478 | 10218 | } |
45537677 MS |
10219 | goto found; |
10220 | } | |
8d08fdba | 10221 | if (type) |
8251199e | 10222 | error ("two or more data types in declaration of `%s'", name); |
8d08fdba MS |
10223 | else if (TREE_CODE (id) == IDENTIFIER_NODE) |
10224 | { | |
10225 | register tree t = lookup_name (id, 1); | |
10226 | if (!t || TREE_CODE (t) != TYPE_DECL) | |
8251199e | 10227 | error ("`%s' fails to be a typedef or built in type", |
8d08fdba MS |
10228 | IDENTIFIER_POINTER (id)); |
10229 | else | |
10230 | { | |
10231 | type = TREE_TYPE (t); | |
10232 | typedef_decl = t; | |
10233 | } | |
10234 | } | |
bd6dd845 | 10235 | else if (id != error_mark_node) |
8d08fdba MS |
10236 | /* Can't change CLASS nodes into RECORD nodes here! */ |
10237 | type = id; | |
10238 | ||
10239 | found: ; | |
10240 | } | |
10241 | ||
8d6e459d NS |
10242 | #if 0 |
10243 | /* See the code below that used this. */ | |
10244 | if (typedef_decl) | |
10245 | decl_attr = DECL_ATTRIBUTES (typedef_decl); | |
10246 | #endif | |
8d08fdba MS |
10247 | typedef_type = type; |
10248 | ||
37c46b43 | 10249 | /* No type at all: default to `int', and set DEFAULTED_INT |
6eabb241 | 10250 | because it was not a user-defined typedef. */ |
8d08fdba | 10251 | |
a3203465 MS |
10252 | if (type == NULL_TREE |
10253 | && (RIDBIT_SETP (RID_SIGNED, specbits) | |
10254 | || RIDBIT_SETP (RID_UNSIGNED, specbits) | |
10255 | || RIDBIT_SETP (RID_LONG, specbits) | |
10256 | || RIDBIT_SETP (RID_SHORT, specbits))) | |
10257 | { | |
10258 | /* These imply 'int'. */ | |
10259 | type = integer_type_node; | |
37c46b43 | 10260 | defaulted_int = 1; |
a3203465 MS |
10261 | } |
10262 | ||
3dbc07b6 MM |
10263 | if (sfk != sfk_none) |
10264 | type = check_special_function_return_type (sfk, type, | |
3dbc07b6 MM |
10265 | ctor_return_type); |
10266 | else if (type == NULL_TREE) | |
8d08fdba | 10267 | { |
3dbc07b6 MM |
10268 | int is_main; |
10269 | ||
8d08fdba | 10270 | explicit_int = -1; |
3dbc07b6 | 10271 | |
0fd0b7ee JM |
10272 | /* We handle `main' specially here, because 'main () { }' is so |
10273 | common. With no options, it is allowed. With -Wreturn-type, | |
10274 | it is a warning. It is only an error with -pedantic-errors. */ | |
3dbc07b6 | 10275 | is_main = (funcdef_flag |
7437519c | 10276 | && dname && MAIN_NAME_P (dname) |
3dbc07b6 MM |
10277 | && ctype == NULL_TREE |
10278 | && in_namespace == NULL_TREE | |
10279 | && current_namespace == global_namespace); | |
10280 | ||
10281 | if (in_system_header || flag_ms_extensions) | |
10282 | /* Allow it, sigh. */; | |
10283 | else if (pedantic || ! is_main) | |
33bd39a2 | 10284 | pedwarn ("ISO C++ forbids declaration of `%s' with no type", |
3dbc07b6 MM |
10285 | name); |
10286 | else if (warn_return_type) | |
33bd39a2 | 10287 | warning ("ISO C++ forbids declaration of `%s' with no type", |
3dbc07b6 | 10288 | name); |
41eff652 | 10289 | |
3dbc07b6 | 10290 | type = integer_type_node; |
51c184be | 10291 | } |
ad1a6d45 | 10292 | |
8d6e459d | 10293 | if (type && IMPLICIT_TYPENAME_P (type)) |
ad1a6d45 NS |
10294 | { |
10295 | /* The implicit typename extension is deprecated and will be | |
10296 | removed. Warn about its use now. */ | |
33bd39a2 | 10297 | warning ("`%T' is implicitly a typename", type); |
ad1a6d45 NS |
10298 | cp_deprecated ("implicit typename"); |
10299 | ||
10300 | /* Now remove its implicitness, so that we don't warn again. | |
10301 | For instance this might be a typedef, and we do not want to | |
10302 | warn on uses of the typedef itself. Simply clearing the | |
10303 | TREE_TYPE is insufficient. */ | |
10304 | type = copy_node (type); | |
10305 | TREE_TYPE (type) = NULL_TREE; | |
10306 | } | |
8d08fdba MS |
10307 | |
10308 | ctype = NULL_TREE; | |
10309 | ||
10310 | /* Now process the modifiers that were specified | |
10311 | and check for invalid combinations. */ | |
10312 | ||
10313 | /* Long double is a special combination. */ | |
10314 | ||
10315 | if (RIDBIT_SETP (RID_LONG, specbits) | |
10316 | && TYPE_MAIN_VARIANT (type) == double_type_node) | |
10317 | { | |
10318 | RIDBIT_RESET (RID_LONG, specbits); | |
68642fb6 | 10319 | type = build_qualified_type (long_double_type_node, |
89d684bb | 10320 | cp_type_quals (type)); |
8d08fdba MS |
10321 | } |
10322 | ||
10323 | /* Check all other uses of type modifiers. */ | |
10324 | ||
10325 | if (RIDBIT_SETP (RID_UNSIGNED, specbits) | |
10326 | || RIDBIT_SETP (RID_SIGNED, specbits) | |
10327 | || RIDBIT_SETP (RID_LONG, specbits) | |
10328 | || RIDBIT_SETP (RID_SHORT, specbits)) | |
10329 | { | |
10330 | int ok = 0; | |
10331 | ||
10332 | if (TREE_CODE (type) == REAL_TYPE) | |
8251199e | 10333 | error ("short, signed or unsigned invalid for `%s'", name); |
b7484fbe | 10334 | else if (TREE_CODE (type) != INTEGER_TYPE) |
8251199e | 10335 | error ("long, short, signed or unsigned invalid for `%s'", name); |
8d08fdba MS |
10336 | else if (RIDBIT_SETP (RID_LONG, specbits) |
10337 | && RIDBIT_SETP (RID_SHORT, specbits)) | |
8251199e | 10338 | error ("long and short specified together for `%s'", name); |
8d08fdba MS |
10339 | else if ((RIDBIT_SETP (RID_LONG, specbits) |
10340 | || RIDBIT_SETP (RID_SHORT, specbits)) | |
10341 | && explicit_char) | |
8251199e | 10342 | error ("long or short specified with char for `%s'", name); |
8d08fdba MS |
10343 | else if ((RIDBIT_SETP (RID_LONG, specbits) |
10344 | || RIDBIT_SETP (RID_SHORT, specbits)) | |
10345 | && TREE_CODE (type) == REAL_TYPE) | |
8251199e | 10346 | error ("long or short specified with floating type for `%s'", name); |
8d08fdba MS |
10347 | else if (RIDBIT_SETP (RID_SIGNED, specbits) |
10348 | && RIDBIT_SETP (RID_UNSIGNED, specbits)) | |
8251199e | 10349 | error ("signed and unsigned given together for `%s'", name); |
8d08fdba MS |
10350 | else |
10351 | { | |
10352 | ok = 1; | |
37c46b43 | 10353 | if (!explicit_int && !defaulted_int && !explicit_char && pedantic) |
8d08fdba | 10354 | { |
8251199e | 10355 | pedwarn ("long, short, signed or unsigned used invalidly for `%s'", |
8d08fdba MS |
10356 | name); |
10357 | if (flag_pedantic_errors) | |
10358 | ok = 0; | |
10359 | } | |
10360 | } | |
10361 | ||
10362 | /* Discard the type modifiers if they are invalid. */ | |
10363 | if (! ok) | |
10364 | { | |
10365 | RIDBIT_RESET (RID_UNSIGNED, specbits); | |
10366 | RIDBIT_RESET (RID_SIGNED, specbits); | |
10367 | RIDBIT_RESET (RID_LONG, specbits); | |
10368 | RIDBIT_RESET (RID_SHORT, specbits); | |
10369 | longlong = 0; | |
10370 | } | |
10371 | } | |
10372 | ||
37c46b43 MS |
10373 | if (RIDBIT_SETP (RID_COMPLEX, specbits) |
10374 | && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE) | |
10375 | { | |
8251199e | 10376 | error ("complex invalid for `%s'", name); |
37c46b43 MS |
10377 | RIDBIT_RESET (RID_COMPLEX, specbits); |
10378 | } | |
10379 | ||
8d08fdba MS |
10380 | /* Decide whether an integer type is signed or not. |
10381 | Optionally treat bitfields as signed by default. */ | |
10382 | if (RIDBIT_SETP (RID_UNSIGNED, specbits) | |
b89c5a7b MM |
10383 | /* [class.bit] |
10384 | ||
10385 | It is implementation-defined whether a plain (neither | |
10386 | explicitly signed or unsigned) char, short, int, or long | |
10387 | bit-field is signed or unsigned. | |
68642fb6 | 10388 | |
b89c5a7b MM |
10389 | Naturally, we extend this to long long as well. Note that |
10390 | this does not include wchar_t. */ | |
10391 | || (bitfield && !flag_signed_bitfields | |
10392 | && RIDBIT_NOTSETP (RID_SIGNED, specbits) | |
10393 | /* A typedef for plain `int' without `signed' can be | |
10394 | controlled just like plain `int', but a typedef for | |
10395 | `signed int' cannot be so controlled. */ | |
68642fb6 | 10396 | && !(typedef_decl |
29bbeb1c | 10397 | && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)) |
b89c5a7b MM |
10398 | && (TREE_CODE (type) == INTEGER_TYPE |
10399 | || TREE_CODE (type) == CHAR_TYPE) | |
29bbeb1c | 10400 | && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node))) |
8d08fdba MS |
10401 | { |
10402 | if (longlong) | |
10403 | type = long_long_unsigned_type_node; | |
10404 | else if (RIDBIT_SETP (RID_LONG, specbits)) | |
10405 | type = long_unsigned_type_node; | |
10406 | else if (RIDBIT_SETP (RID_SHORT, specbits)) | |
10407 | type = short_unsigned_type_node; | |
10408 | else if (type == char_type_node) | |
10409 | type = unsigned_char_type_node; | |
10410 | else if (typedef_decl) | |
ceef8ce4 | 10411 | type = c_common_unsigned_type (type); |
8d08fdba MS |
10412 | else |
10413 | type = unsigned_type_node; | |
10414 | } | |
10415 | else if (RIDBIT_SETP (RID_SIGNED, specbits) | |
10416 | && type == char_type_node) | |
10417 | type = signed_char_type_node; | |
10418 | else if (longlong) | |
10419 | type = long_long_integer_type_node; | |
10420 | else if (RIDBIT_SETP (RID_LONG, specbits)) | |
10421 | type = long_integer_type_node; | |
10422 | else if (RIDBIT_SETP (RID_SHORT, specbits)) | |
10423 | type = short_integer_type_node; | |
10424 | ||
37c46b43 MS |
10425 | if (RIDBIT_SETP (RID_COMPLEX, specbits)) |
10426 | { | |
10427 | /* If we just have "complex", it is equivalent to | |
10428 | "complex double", but if any modifiers at all are specified it is | |
10429 | the complex form of TYPE. E.g, "complex short" is | |
10430 | "complex short int". */ | |
10431 | ||
10432 | if (defaulted_int && ! longlong | |
10433 | && ! (RIDBIT_SETP (RID_LONG, specbits) | |
10434 | || RIDBIT_SETP (RID_SHORT, specbits) | |
10435 | || RIDBIT_SETP (RID_SIGNED, specbits) | |
10436 | || RIDBIT_SETP (RID_UNSIGNED, specbits))) | |
10437 | type = complex_double_type_node; | |
10438 | else if (type == integer_type_node) | |
10439 | type = complex_integer_type_node; | |
10440 | else if (type == float_type_node) | |
10441 | type = complex_float_type_node; | |
10442 | else if (type == double_type_node) | |
10443 | type = complex_double_type_node; | |
10444 | else if (type == long_double_type_node) | |
10445 | type = complex_long_double_type_node; | |
10446 | else | |
10447 | type = build_complex_type (type); | |
10448 | } | |
10449 | ||
4f2b0fb2 NS |
10450 | type_quals = TYPE_UNQUALIFIED; |
10451 | if (RIDBIT_SETP (RID_CONST, specbits)) | |
10452 | type_quals |= TYPE_QUAL_CONST; | |
10453 | if (RIDBIT_SETP (RID_VOLATILE, specbits)) | |
10454 | type_quals |= TYPE_QUAL_VOLATILE; | |
10455 | if (RIDBIT_SETP (RID_RESTRICT, specbits)) | |
10456 | type_quals |= TYPE_QUAL_RESTRICT; | |
10457 | if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED) | |
33bd39a2 | 10458 | error ("qualifiers are not allowed on declaration of `operator %T'", |
3ac3d9ea MM |
10459 | ctor_return_type); |
10460 | ||
4f2b0fb2 NS |
10461 | type_quals |= cp_type_quals (type); |
10462 | type = cp_build_qualified_type_real | |
10463 | (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl) | |
10464 | ? tf_ignore_bad_quals : 0) | tf_error | tf_warning)); | |
10465 | /* We might have ignored or rejected some of the qualifiers. */ | |
10466 | type_quals = cp_type_quals (type); | |
10467 | ||
8d08fdba MS |
10468 | staticp = 0; |
10469 | inlinep = !! RIDBIT_SETP (RID_INLINE, specbits); | |
8d08fdba | 10470 | virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits); |
db5ae43f MS |
10471 | RIDBIT_RESET (RID_VIRTUAL, specbits); |
10472 | explicitp = RIDBIT_SETP (RID_EXPLICIT, specbits) != 0; | |
10473 | RIDBIT_RESET (RID_EXPLICIT, specbits); | |
8d08fdba | 10474 | |
8d08fdba MS |
10475 | if (RIDBIT_SETP (RID_STATIC, specbits)) |
10476 | staticp = 1 + (decl_context == FIELD); | |
10477 | ||
10478 | if (virtualp && staticp == 2) | |
10479 | { | |
33bd39a2 | 10480 | error ("member `%D' cannot be declared both virtual and static", |
8d08fdba MS |
10481 | dname); |
10482 | staticp = 0; | |
10483 | } | |
10484 | friendp = RIDBIT_SETP (RID_FRIEND, specbits); | |
8d08fdba MS |
10485 | RIDBIT_RESET (RID_FRIEND, specbits); |
10486 | ||
d5614afb NS |
10487 | if (dependant_name && !friendp) |
10488 | { | |
10489 | error ("`%T::%D' is not a valid declarator", ctype, dependant_name); | |
10490 | return void_type_node; | |
10491 | } | |
10492 | ||
8d08fdba MS |
10493 | /* Warn if two storage classes are given. Default to `auto'. */ |
10494 | ||
10495 | if (RIDBIT_ANY_SET (specbits)) | |
10496 | { | |
10497 | if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++; | |
270d8c65 | 10498 | if (RIDBIT_SETP (RID_EXTERN, specbits) && !extern_langp) nclasses++; |
7a1f3f5f | 10499 | if (RIDBIT_SETP (RID_THREAD, specbits)) nclasses++; |
8d08fdba | 10500 | if (decl_context == PARM && nclasses > 0) |
8251199e | 10501 | error ("storage class specifiers invalid in parameter declarations"); |
8d08fdba MS |
10502 | if (RIDBIT_SETP (RID_TYPEDEF, specbits)) |
10503 | { | |
10504 | if (decl_context == PARM) | |
8251199e | 10505 | error ("typedef declaration invalid in parameter declaration"); |
8d08fdba MS |
10506 | nclasses++; |
10507 | } | |
10508 | if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++; | |
10509 | if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++; | |
270d8c65 NS |
10510 | if (!nclasses && !friendp && extern_langp) |
10511 | nclasses++; | |
8d08fdba MS |
10512 | } |
10513 | ||
10514 | /* Give error if `virtual' is used outside of class declaration. */ | |
b7484fbe MS |
10515 | if (virtualp |
10516 | && (current_class_name == NULL_TREE || decl_context != FIELD)) | |
8d08fdba | 10517 | { |
8251199e | 10518 | error ("virtual outside class declaration"); |
8d08fdba MS |
10519 | virtualp = 0; |
10520 | } | |
8d08fdba MS |
10521 | |
10522 | /* Static anonymous unions are dealt with here. */ | |
10523 | if (staticp && decl_context == TYPENAME | |
10524 | && TREE_CODE (declspecs) == TREE_LIST | |
6bdb8141 | 10525 | && ANON_AGGR_TYPE_P (TREE_VALUE (declspecs))) |
8d08fdba MS |
10526 | decl_context = FIELD; |
10527 | ||
8d08fdba MS |
10528 | /* Warn about storage classes that are invalid for certain |
10529 | kinds of declarations (parameters, typenames, etc.). */ | |
10530 | ||
7a1f3f5f RH |
10531 | /* "static __thread" and "extern __thread" are allowed. */ |
10532 | if (nclasses == 2 | |
10533 | && RIDBIT_SETP (RID_THREAD, specbits) | |
10534 | && (RIDBIT_SETP (RID_EXTERN, specbits) | |
10535 | || RIDBIT_SETP (RID_STATIC, specbits))) | |
10536 | nclasses = 1; | |
10537 | ||
8d08fdba | 10538 | if (nclasses > 1) |
8251199e | 10539 | error ("multiple storage classes in declaration of `%s'", name); |
8d08fdba MS |
10540 | else if (decl_context != NORMAL && nclasses > 0) |
10541 | { | |
db5ae43f | 10542 | if ((decl_context == PARM || decl_context == CATCHPARM) |
8d08fdba MS |
10543 | && (RIDBIT_SETP (RID_REGISTER, specbits) |
10544 | || RIDBIT_SETP (RID_AUTO, specbits))) | |
10545 | ; | |
fc378698 MS |
10546 | else if (RIDBIT_SETP (RID_TYPEDEF, specbits)) |
10547 | ; | |
8d08fdba | 10548 | else if (decl_context == FIELD |
d363e7bf AJ |
10549 | /* C++ allows static class elements */ |
10550 | && RIDBIT_SETP (RID_STATIC, specbits)) | |
10551 | /* C++ also allows inlines and signed and unsigned elements, | |
10552 | but in those cases we don't come in here. */ | |
8d08fdba MS |
10553 | ; |
10554 | else | |
10555 | { | |
10556 | if (decl_context == FIELD) | |
10557 | { | |
b7484fbe MS |
10558 | tree tmp = NULL_TREE; |
10559 | register int op = 0; | |
10560 | ||
10561 | if (declarator) | |
10562 | { | |
68642fb6 | 10563 | /* Avoid trying to get an operand off an identifier node. */ |
9e9ff709 MS |
10564 | if (TREE_CODE (declarator) == IDENTIFIER_NODE) |
10565 | tmp = declarator; | |
10566 | else | |
10567 | tmp = TREE_OPERAND (declarator, 0); | |
b7484fbe | 10568 | op = IDENTIFIER_OPNAME_P (tmp); |
596ea4e5 AS |
10569 | if (IDENTIFIER_TYPENAME_P (tmp)) |
10570 | { | |
10571 | if (IDENTIFIER_GLOBAL_VALUE (tmp) | |
5362b086 | 10572 | && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (tmp)) |
596ea4e5 AS |
10573 | == TYPE_DECL)) |
10574 | name = IDENTIFIER_POINTER (tmp); | |
10575 | else | |
10576 | name = "<invalid operator>"; | |
10577 | } | |
b7484fbe | 10578 | } |
8251199e | 10579 | error ("storage class specified for %s `%s'", |
6eabb241 | 10580 | op ? "member operator" : "field", |
596ea4e5 | 10581 | name); |
8d08fdba MS |
10582 | } |
10583 | else | |
b9d12519 KG |
10584 | { |
10585 | if (decl_context == PARM || decl_context == CATCHPARM) | |
10586 | error ("storage class specified for parameter `%s'", name); | |
10587 | else | |
10588 | error ("storage class specified for typename"); | |
10589 | } | |
8d08fdba MS |
10590 | RIDBIT_RESET (RID_REGISTER, specbits); |
10591 | RIDBIT_RESET (RID_AUTO, specbits); | |
10592 | RIDBIT_RESET (RID_EXTERN, specbits); | |
7a1f3f5f | 10593 | RIDBIT_RESET (RID_THREAD, specbits); |
8d08fdba MS |
10594 | } |
10595 | } | |
10596 | else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag) | |
10597 | { | |
a9aedbc2 | 10598 | if (toplevel_bindings_p ()) |
8d08fdba | 10599 | { |
59be0cdd | 10600 | /* It's common practice (and completely valid) to have a const |
8d08fdba | 10601 | be initialized and declared extern. */ |
950ad3c3 | 10602 | if (!(type_quals & TYPE_QUAL_CONST)) |
8251199e | 10603 | warning ("`%s' initialized and declared `extern'", name); |
8d08fdba MS |
10604 | } |
10605 | else | |
8251199e | 10606 | error ("`%s' has both `extern' and initializer", name); |
8d08fdba MS |
10607 | } |
10608 | else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag | |
a9aedbc2 | 10609 | && ! toplevel_bindings_p ()) |
8251199e | 10610 | error ("nested function `%s' declared `extern'", name); |
a9aedbc2 | 10611 | else if (toplevel_bindings_p ()) |
8d08fdba MS |
10612 | { |
10613 | if (RIDBIT_SETP (RID_AUTO, specbits)) | |
8251199e | 10614 | error ("top-level declaration of `%s' specifies `auto'", name); |
8d08fdba | 10615 | } |
7a1f3f5f RH |
10616 | else if (RIDBIT_SETP (RID_THREAD, specbits) |
10617 | && !RIDBIT_SETP (RID_EXTERN, specbits) | |
10618 | && !RIDBIT_SETP (RID_STATIC, specbits)) | |
10619 | { | |
10620 | error ("function-scope `%s' implicitly auto and declared `__thread'", | |
10621 | name); | |
10622 | RIDBIT_RESET (RID_THREAD, specbits); | |
10623 | } | |
8d08fdba | 10624 | |
909e536a | 10625 | if (nclasses > 0 && friendp) |
8251199e | 10626 | error ("storage class specifiers invalid in friend function declarations"); |
909e536a | 10627 | |
8d08fdba MS |
10628 | /* Now figure out the structure of the declarator proper. |
10629 | Descend through it, creating more complex types, until we reach | |
9e259dd1 | 10630 | the declared identifier (or NULL_TREE, in an abstract declarator). */ |
8d08fdba | 10631 | |
386b8a85 JM |
10632 | while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE |
10633 | && TREE_CODE (declarator) != TEMPLATE_ID_EXPR) | |
8d08fdba MS |
10634 | { |
10635 | /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]), | |
10636 | an INDIRECT_REF (for *...), | |
10637 | a CALL_EXPR (for ...(...)), | |
10638 | an identifier (for the name being declared) | |
10639 | or a null pointer (for the place in an absolute declarator | |
10640 | where the name was omitted). | |
10641 | For the last two cases, we have just exited the loop. | |
10642 | ||
10643 | For C++ it could also be | |
10644 | a SCOPE_REF (for class :: ...). In this case, we have converted | |
10645 | sensible names to types, and those are the values we use to | |
10646 | qualify the member name. | |
10647 | an ADDR_EXPR (for &...), | |
10648 | a BIT_NOT_EXPR (for destructors) | |
8d08fdba MS |
10649 | |
10650 | At this point, TYPE is the type of elements of an array, | |
10651 | or for a function to return, or for a pointer to point to. | |
10652 | After this sequence of ifs, TYPE is the type of the | |
10653 | array or function or pointer, and DECLARATOR has had its | |
10654 | outermost layer removed. */ | |
10655 | ||
bd6dd845 | 10656 | if (type == error_mark_node) |
8d08fdba MS |
10657 | { |
10658 | if (TREE_CODE (declarator) == SCOPE_REF) | |
10659 | declarator = TREE_OPERAND (declarator, 1); | |
10660 | else | |
10661 | declarator = TREE_OPERAND (declarator, 0); | |
10662 | continue; | |
10663 | } | |
10664 | if (quals != NULL_TREE | |
10665 | && (declarator == NULL_TREE | |
10666 | || TREE_CODE (declarator) != SCOPE_REF)) | |
10667 | { | |
10668 | if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE) | |
10669 | ctype = TYPE_METHOD_BASETYPE (type); | |
10670 | if (ctype != NULL_TREE) | |
10671 | { | |
8d08fdba | 10672 | tree dummy = build_decl (TYPE_DECL, NULL_TREE, type); |
535233a8 | 10673 | grok_method_quals (ctype, dummy, quals); |
8d08fdba | 10674 | type = TREE_TYPE (dummy); |
535233a8 | 10675 | ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))); |
8d08fdba MS |
10676 | quals = NULL_TREE; |
10677 | } | |
10678 | } | |
1eab9b56 | 10679 | |
8d08fdba MS |
10680 | switch (TREE_CODE (declarator)) |
10681 | { | |
52fbc847 JM |
10682 | case TREE_LIST: |
10683 | { | |
10684 | /* We encode a declarator with embedded attributes using | |
91d231cb JM |
10685 | a TREE_LIST. */ |
10686 | tree attrs = TREE_PURPOSE (declarator); | |
10687 | tree inner_decl; | |
c9574c9f R |
10688 | int attr_flags; |
10689 | ||
52fbc847 | 10690 | declarator = TREE_VALUE (declarator); |
91d231cb JM |
10691 | inner_decl = declarator; |
10692 | while (inner_decl != NULL_TREE | |
10693 | && TREE_CODE (inner_decl) == TREE_LIST) | |
10694 | inner_decl = TREE_VALUE (inner_decl); | |
c9574c9f | 10695 | attr_flags = 0; |
91d231cb JM |
10696 | if (inner_decl == NULL_TREE |
10697 | || TREE_CODE (inner_decl) == IDENTIFIER_NODE) | |
10698 | attr_flags |= (int) ATTR_FLAG_DECL_NEXT; | |
10699 | if (TREE_CODE (inner_decl) == CALL_EXPR) | |
10700 | attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT; | |
10701 | if (TREE_CODE (inner_decl) == ARRAY_REF) | |
10702 | attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT; | |
10703 | returned_attrs = decl_attributes (&type, | |
10704 | chainon (returned_attrs, attrs), | |
10705 | attr_flags); | |
52fbc847 JM |
10706 | } |
10707 | break; | |
10708 | ||
8d08fdba MS |
10709 | case ARRAY_REF: |
10710 | { | |
2848ad0f | 10711 | register tree size; |
8d08fdba | 10712 | |
2848ad0f | 10713 | size = TREE_OPERAND (declarator, 1); |
8d08fdba | 10714 | |
f2e2cbd4 | 10715 | /* VC++ spells a zero-sized array with []. */ |
e8ea7036 MM |
10716 | if (size == NULL_TREE && decl_context == FIELD && ! staticp |
10717 | && ! RIDBIT_SETP (RID_TYPEDEF, specbits)) | |
f2e2cbd4 JM |
10718 | size = integer_zero_node; |
10719 | ||
2848ad0f | 10720 | declarator = TREE_OPERAND (declarator, 0); |
8d08fdba | 10721 | |
2848ad0f | 10722 | type = create_array_type_for_decl (dname, type, size); |
c1b177ec | 10723 | |
a1c65f9f | 10724 | /* VLAs never work as fields. */ |
5362b086 | 10725 | if (decl_context == FIELD && !processing_template_decl |
c1b177ec ML |
10726 | && TREE_CODE (type) == ARRAY_TYPE |
10727 | && TYPE_DOMAIN (type) != NULL_TREE | |
10728 | && !TREE_CONSTANT (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))) | |
10729 | { | |
33bd39a2 | 10730 | error ("size of member `%D' is not constant", dname); |
c1b177ec | 10731 | /* Proceed with arbitrary constant size, so that offset |
a1c65f9f | 10732 | computations don't get confused. */ |
c1b177ec ML |
10733 | type = create_array_type_for_decl (dname, TREE_TYPE (type), |
10734 | integer_one_node); | |
10735 | } | |
10736 | ||
8d08fdba MS |
10737 | ctype = NULL_TREE; |
10738 | } | |
10739 | break; | |
10740 | ||
10741 | case CALL_EXPR: | |
10742 | { | |
10743 | tree arg_types; | |
f376e137 | 10744 | int funcdecl_p; |
43f887f9 | 10745 | tree inner_parms = CALL_DECLARATOR_PARMS (declarator); |
f376e137 | 10746 | tree inner_decl = TREE_OPERAND (declarator, 0); |
8d08fdba MS |
10747 | |
10748 | /* Declaring a function type. | |
10749 | Make sure we have a valid type for the function to return. */ | |
8d08fdba | 10750 | |
91063b51 | 10751 | /* We now know that the TYPE_QUALS don't apply to the |
14ae7e7d | 10752 | decl, but to its return type. */ |
91063b51 | 10753 | type_quals = TYPE_UNQUALIFIED; |
8d08fdba MS |
10754 | |
10755 | /* Warn about some types functions can't return. */ | |
10756 | ||
10757 | if (TREE_CODE (type) == FUNCTION_TYPE) | |
10758 | { | |
8251199e | 10759 | error ("`%s' declared as function returning a function", name); |
8d08fdba MS |
10760 | type = integer_type_node; |
10761 | } | |
10762 | if (TREE_CODE (type) == ARRAY_TYPE) | |
10763 | { | |
8251199e | 10764 | error ("`%s' declared as function returning an array", name); |
8d08fdba MS |
10765 | type = integer_type_node; |
10766 | } | |
10767 | ||
f376e137 MS |
10768 | if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF) |
10769 | inner_decl = TREE_OPERAND (inner_decl, 1); | |
10770 | ||
68642fb6 | 10771 | if (inner_decl && TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR) |
386b8a85 JM |
10772 | inner_decl = dname; |
10773 | ||
b7484fbe | 10774 | /* Pick up type qualifiers which should be applied to `this'. */ |
43f887f9 | 10775 | quals = CALL_DECLARATOR_QUALS (declarator); |
b7484fbe | 10776 | |
c11b6f21 | 10777 | /* Pick up the exception specifications. */ |
43f887f9 | 10778 | raises = CALL_DECLARATOR_EXCEPTION_SPEC (declarator); |
c11b6f21 | 10779 | |
f376e137 MS |
10780 | /* Say it's a definition only for the CALL_EXPR |
10781 | closest to the identifier. */ | |
beb53fb8 | 10782 | funcdecl_p |
68642fb6 | 10783 | = inner_decl |
386b8a85 | 10784 | && (TREE_CODE (inner_decl) == IDENTIFIER_NODE |
68642fb6 | 10785 | || TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR |
386b8a85 | 10786 | || TREE_CODE (inner_decl) == BIT_NOT_EXPR); |
68642fb6 | 10787 | |
8d08fdba MS |
10788 | if (ctype == NULL_TREE |
10789 | && decl_context == FIELD | |
f376e137 | 10790 | && funcdecl_p |
8d08fdba MS |
10791 | && (friendp == 0 || dname == current_class_name)) |
10792 | ctype = current_class_type; | |
10793 | ||
3dbc07b6 | 10794 | if (ctype && sfk == sfk_conversion) |
8d08fdba MS |
10795 | TYPE_HAS_CONVERSION (ctype) = 1; |
10796 | if (ctype && constructor_name (ctype) == dname) | |
10797 | { | |
10798 | /* We are within a class's scope. If our declarator name | |
10799 | is the same as the class name, and we are defining | |
10800 | a function, then it is a constructor/destructor, and | |
10801 | therefore returns a void type. */ | |
10802 | ||
10803 | if (flags == DTOR_FLAG) | |
10804 | { | |
cab1f180 ML |
10805 | /* ISO C++ 12.4/2. A destructor may not be |
10806 | declared const or volatile. A destructor may | |
10807 | not be static. */ | |
8d08fdba | 10808 | if (staticp == 2) |
8251199e | 10809 | error ("destructor cannot be static member function"); |
b7484fbe | 10810 | if (quals) |
8d08fdba | 10811 | { |
33bd39a2 | 10812 | error ("destructors may not be `%s'", |
4f70a846 MM |
10813 | IDENTIFIER_POINTER (TREE_VALUE (quals))); |
10814 | quals = NULL_TREE; | |
8d08fdba MS |
10815 | } |
10816 | if (decl_context == FIELD) | |
10817 | { | |
4dacf5bd KG |
10818 | if (! member_function_or_else (ctype, |
10819 | current_class_type, | |
10820 | flags)) | |
8d08fdba MS |
10821 | return void_type_node; |
10822 | } | |
10823 | } | |
2c73f9f5 | 10824 | else /* It's a constructor. */ |
8d08fdba | 10825 | { |
db5ae43f MS |
10826 | if (explicitp == 1) |
10827 | explicitp = 2; | |
cab1f180 ML |
10828 | /* ISO C++ 12.1. A constructor may not be |
10829 | declared const or volatile. A constructor may | |
10830 | not be virtual. A constructor may not be | |
10831 | static. */ | |
8d08fdba | 10832 | if (staticp == 2) |
8251199e | 10833 | error ("constructor cannot be static member function"); |
8d08fdba MS |
10834 | if (virtualp) |
10835 | { | |
8251199e | 10836 | pedwarn ("constructors cannot be declared virtual"); |
8d08fdba MS |
10837 | virtualp = 0; |
10838 | } | |
b7484fbe | 10839 | if (quals) |
8d08fdba | 10840 | { |
33bd39a2 | 10841 | error ("constructors may not be `%s'", |
4f70a846 MM |
10842 | IDENTIFIER_POINTER (TREE_VALUE (quals))); |
10843 | quals = NULL_TREE; | |
d363e7bf | 10844 | } |
8d08fdba | 10845 | { |
51c184be | 10846 | RID_BIT_TYPE tmp_bits; |
4e135bdd | 10847 | memcpy (&tmp_bits, &specbits, sizeof (RID_BIT_TYPE)); |
51c184be MS |
10848 | RIDBIT_RESET (RID_INLINE, tmp_bits); |
10849 | RIDBIT_RESET (RID_STATIC, tmp_bits); | |
10850 | if (RIDBIT_ANY_SET (tmp_bits)) | |
8251199e | 10851 | error ("return value type specifier for constructor ignored"); |
8d08fdba | 10852 | } |
6eabb241 | 10853 | if (decl_context == FIELD) |
8d08fdba | 10854 | { |
4dacf5bd KG |
10855 | if (! member_function_or_else (ctype, |
10856 | current_class_type, | |
10857 | flags)) | |
8d08fdba MS |
10858 | return void_type_node; |
10859 | TYPE_HAS_CONSTRUCTOR (ctype) = 1; | |
3dbc07b6 | 10860 | if (sfk != sfk_constructor) |
8d08fdba MS |
10861 | return NULL_TREE; |
10862 | } | |
10863 | } | |
10864 | if (decl_context == FIELD) | |
10865 | staticp = 0; | |
10866 | } | |
b7484fbe | 10867 | else if (friendp) |
8d08fdba | 10868 | { |
b7484fbe | 10869 | if (initialized) |
8251199e | 10870 | error ("can't initialize friend function `%s'", name); |
b7484fbe MS |
10871 | if (virtualp) |
10872 | { | |
10873 | /* Cannot be both friend and virtual. */ | |
8251199e | 10874 | error ("virtual functions cannot be friends"); |
b7484fbe MS |
10875 | RIDBIT_RESET (RID_FRIEND, specbits); |
10876 | friendp = 0; | |
10877 | } | |
28cbf42c | 10878 | if (decl_context == NORMAL) |
8251199e | 10879 | error ("friend declaration not in class definition"); |
28cbf42c | 10880 | if (current_function_decl && funcdef_flag) |
33bd39a2 | 10881 | error ("can't define friend function `%s' in a local class definition", |
28cbf42c | 10882 | name); |
8d08fdba MS |
10883 | } |
10884 | ||
8d08fdba MS |
10885 | /* Construct the function type and go to the next |
10886 | inner layer of declarator. */ | |
10887 | ||
f376e137 | 10888 | declarator = TREE_OPERAND (declarator, 0); |
8d08fdba | 10889 | |
f376e137 MS |
10890 | /* FIXME: This is where default args should be fully |
10891 | processed. */ | |
8d08fdba | 10892 | |
5cce22b6 | 10893 | arg_types = grokparms (inner_parms); |
8d08fdba | 10894 | |
4f70a846 | 10895 | if (declarator && flags == DTOR_FLAG) |
8d08fdba | 10896 | { |
4f70a846 MM |
10897 | /* A destructor declared in the body of a class will |
10898 | be represented as a BIT_NOT_EXPR. But, we just | |
10899 | want the underlying IDENTIFIER. */ | |
8d08fdba | 10900 | if (TREE_CODE (declarator) == BIT_NOT_EXPR) |
4f70a846 | 10901 | declarator = TREE_OPERAND (declarator, 0); |
68642fb6 | 10902 | |
8b27e9ef | 10903 | if (arg_types != void_list_node) |
8d08fdba | 10904 | { |
33bd39a2 | 10905 | error ("destructors may not have parameters"); |
4f70a846 MM |
10906 | arg_types = void_list_node; |
10907 | last_function_parms = NULL_TREE; | |
8d08fdba MS |
10908 | } |
10909 | } | |
10910 | ||
d22c8596 | 10911 | /* ANSI says that `const int foo ();' |
8d08fdba | 10912 | does not make the function foo const. */ |
d22c8596 | 10913 | type = build_function_type (type, arg_types); |
42976354 BK |
10914 | |
10915 | { | |
10916 | tree t; | |
10917 | for (t = arg_types; t; t = TREE_CHAIN (t)) | |
10918 | if (TREE_PURPOSE (t) | |
10919 | && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG) | |
10920 | { | |
10921 | add_defarg_fn (type); | |
10922 | break; | |
10923 | } | |
10924 | } | |
8d08fdba MS |
10925 | } |
10926 | break; | |
10927 | ||
10928 | case ADDR_EXPR: | |
10929 | case INDIRECT_REF: | |
10930 | /* Filter out pointers-to-references and references-to-references. | |
10931 | We can get these if a TYPE_DECL is used. */ | |
10932 | ||
10933 | if (TREE_CODE (type) == REFERENCE_TYPE) | |
10934 | { | |
dd29188b NS |
10935 | error (TREE_CODE (declarator) == ADDR_EXPR |
10936 | ? "cannot declare reference to `%#T'" | |
10937 | : "cannot declare pointer to `%#T'", type); | |
a5894242 MS |
10938 | type = TREE_TYPE (type); |
10939 | } | |
dd29188b NS |
10940 | else if (VOID_TYPE_P (type) |
10941 | && (ctype || TREE_CODE (declarator) == ADDR_EXPR)) | |
10942 | error (ctype ? "cannot declare pointer to `%#T' member" | |
10943 | : "cannot declare reference to `%#T'", type); | |
a5894242 | 10944 | |
61a127b3 MM |
10945 | /* Merge any constancy or volatility into the target type |
10946 | for the pointer. */ | |
10947 | ||
91063b51 MM |
10948 | /* We now know that the TYPE_QUALS don't apply to the decl, |
10949 | but to the target of the pointer. */ | |
10950 | type_quals = TYPE_UNQUALIFIED; | |
8d08fdba | 10951 | |
6eabb241 | 10952 | if (TREE_CODE (declarator) == ADDR_EXPR) |
8d08fdba | 10953 | { |
dd29188b | 10954 | if (!VOID_TYPE_P (type)) |
69851283 | 10955 | type = build_reference_type (type); |
8d08fdba MS |
10956 | } |
10957 | else if (TREE_CODE (type) == METHOD_TYPE) | |
d8f8dca1 | 10958 | type = build_ptrmemfunc_type (build_pointer_type (type)); |
9e259dd1 MM |
10959 | else if (ctype) |
10960 | type = build_ptrmem_type (ctype, type); | |
8d08fdba MS |
10961 | else |
10962 | type = build_pointer_type (type); | |
10963 | ||
10964 | /* Process a list of type modifier keywords (such as | |
10965 | const or volatile) that were given inside the `*' or `&'. */ | |
10966 | ||
10967 | if (TREE_TYPE (declarator)) | |
10968 | { | |
10969 | register tree typemodlist; | |
10970 | int erred = 0; | |
4f2b0fb2 NS |
10971 | int constp = 0; |
10972 | int volatilep = 0; | |
10973 | int restrictp = 0; | |
10974 | ||
8d08fdba MS |
10975 | for (typemodlist = TREE_TYPE (declarator); typemodlist; |
10976 | typemodlist = TREE_CHAIN (typemodlist)) | |
10977 | { | |
91063b51 MM |
10978 | tree qualifier = TREE_VALUE (typemodlist); |
10979 | ||
10980 | if (qualifier == ridpointers[(int) RID_CONST]) | |
4f2b0fb2 NS |
10981 | { |
10982 | constp++; | |
10983 | type_quals |= TYPE_QUAL_CONST; | |
10984 | } | |
91063b51 | 10985 | else if (qualifier == ridpointers[(int) RID_VOLATILE]) |
4f2b0fb2 NS |
10986 | { |
10987 | volatilep++; | |
10988 | type_quals |= TYPE_QUAL_VOLATILE; | |
10989 | } | |
91063b51 | 10990 | else if (qualifier == ridpointers[(int) RID_RESTRICT]) |
4f2b0fb2 NS |
10991 | { |
10992 | restrictp++; | |
10993 | type_quals |= TYPE_QUAL_RESTRICT; | |
10994 | } | |
8d08fdba MS |
10995 | else if (!erred) |
10996 | { | |
10997 | erred = 1; | |
91063b51 | 10998 | error ("invalid type modifier within pointer declarator"); |
8d08fdba MS |
10999 | } |
11000 | } | |
11001 | if (constp > 1) | |
8251199e | 11002 | pedwarn ("duplicate `const'"); |
8d08fdba | 11003 | if (volatilep > 1) |
8251199e | 11004 | pedwarn ("duplicate `volatile'"); |
91063b51 MM |
11005 | if (restrictp > 1) |
11006 | pedwarn ("duplicate `restrict'"); | |
91063b51 | 11007 | type = cp_build_qualified_type (type, type_quals); |
4f2b0fb2 | 11008 | type_quals = cp_type_quals (type); |
8d08fdba MS |
11009 | } |
11010 | declarator = TREE_OPERAND (declarator, 0); | |
11011 | ctype = NULL_TREE; | |
11012 | break; | |
11013 | ||
11014 | case SCOPE_REF: | |
11015 | { | |
11016 | /* We have converted type names to NULL_TREE if the | |
11017 | name was bogus, or to a _TYPE node, if not. | |
11018 | ||
11019 | The variable CTYPE holds the type we will ultimately | |
11020 | resolve to. The code here just needs to build | |
11021 | up appropriate member types. */ | |
11022 | tree sname = TREE_OPERAND (declarator, 1); | |
386b8a85 JM |
11023 | tree t; |
11024 | ||
8d08fdba MS |
11025 | /* Destructors can have their visibilities changed as well. */ |
11026 | if (TREE_CODE (sname) == BIT_NOT_EXPR) | |
11027 | sname = TREE_OPERAND (sname, 0); | |
11028 | ||
11029 | if (TREE_COMPLEXITY (declarator) == 0) | |
11030 | /* This needs to be here, in case we are called | |
11031 | multiple times. */ ; | |
9a68c51f | 11032 | else if (TREE_COMPLEXITY (declarator) == -1) |
a1c65f9f | 11033 | /* Namespace member. */ |
9a68c51f | 11034 | pop_decl_namespace (); |
8d08fdba | 11035 | else if (friendp && (TREE_COMPLEXITY (declarator) < 2)) |
2c73f9f5 | 11036 | /* Don't fall out into global scope. Hides real bug? --eichin */ ; |
5566b478 MS |
11037 | else if (! IS_AGGR_TYPE_CODE |
11038 | (TREE_CODE (TREE_OPERAND (declarator, 0)))) | |
11039 | ; | |
8d08fdba MS |
11040 | else if (TREE_COMPLEXITY (declarator) == current_class_depth) |
11041 | { | |
9a3b49ac MS |
11042 | /* Resolve any TYPENAME_TYPEs from the decl-specifier-seq |
11043 | that refer to ctype. They couldn't be resolved earlier | |
11044 | because we hadn't pushed into the class yet. | |
11045 | Example: resolve 'B<T>::type' in | |
11046 | 'B<typename B<T>::type> B<T>::f () { }'. */ | |
11047 | if (current_template_parms | |
11048 | && uses_template_parms (type) | |
11049 | && uses_template_parms (current_class_type)) | |
11050 | { | |
11051 | tree args = current_template_args (); | |
c2ea3a40 NS |
11052 | type = tsubst (type, args, tf_error | tf_warning, |
11053 | NULL_TREE); | |
9a3b49ac MS |
11054 | } |
11055 | ||
a28e3c7f MS |
11056 | /* This pop_nested_class corresponds to the |
11057 | push_nested_class used to push into class scope for | |
11058 | parsing the argument list of a function decl, in | |
11059 | qualified_id. */ | |
b74a0560 | 11060 | pop_nested_class (); |
8d08fdba MS |
11061 | TREE_COMPLEXITY (declarator) = current_class_depth; |
11062 | } | |
11063 | else | |
a98facb0 | 11064 | abort (); |
8d08fdba MS |
11065 | |
11066 | if (TREE_OPERAND (declarator, 0) == NULL_TREE) | |
11067 | { | |
11068 | /* We had a reference to a global decl, or | |
11069 | perhaps we were given a non-aggregate typedef, | |
11070 | in which case we cleared this out, and should just | |
11071 | keep going as though it wasn't there. */ | |
11072 | declarator = sname; | |
11073 | continue; | |
11074 | } | |
11075 | ctype = TREE_OPERAND (declarator, 0); | |
11076 | ||
386b8a85 | 11077 | t = ctype; |
68642fb6 | 11078 | while (t != NULL_TREE && CLASS_TYPE_P (t)) |
386b8a85 | 11079 | { |
5362b086 | 11080 | /* You're supposed to have one `template <...>' |
da8a7456 MM |
11081 | for every template class, but you don't need one |
11082 | for a full specialization. For example: | |
11083 | ||
11084 | template <class T> struct S{}; | |
11085 | template <> struct S<int> { void f(); }; | |
11086 | void S<int>::f () {} | |
11087 | ||
11088 | is correct; there shouldn't be a `template <>' for | |
11089 | the definition of `S<int>::f'. */ | |
11090 | if (CLASSTYPE_TEMPLATE_INFO (t) | |
11091 | && (CLASSTYPE_TEMPLATE_INSTANTIATION (t) | |
92ecdfb7 NS |
11092 | || uses_template_parms (CLASSTYPE_TI_ARGS (t))) |
11093 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t))) | |
386b8a85 | 11094 | template_count += 1; |
da8a7456 | 11095 | |
386b8a85 | 11096 | t = TYPE_MAIN_DECL (t); |
92ecdfb7 | 11097 | t = DECL_CONTEXT (t); |
386b8a85 JM |
11098 | } |
11099 | ||
8d08fdba MS |
11100 | if (sname == NULL_TREE) |
11101 | goto done_scoping; | |
11102 | ||
11103 | if (TREE_CODE (sname) == IDENTIFIER_NODE) | |
11104 | { | |
11105 | /* This is the `standard' use of the scoping operator: | |
11106 | basetype :: member . */ | |
11107 | ||
db5ae43f | 11108 | if (ctype == current_class_type) |
28cbf42c MS |
11109 | { |
11110 | /* class A { | |
11111 | void A::f (); | |
11112 | }; | |
11113 | ||
11114 | Is this ill-formed? */ | |
11115 | ||
11116 | if (pedantic) | |
33bd39a2 | 11117 | pedwarn ("extra qualification `%T::' on member `%s' ignored", |
28cbf42c MS |
11118 | ctype, name); |
11119 | } | |
db5ae43f | 11120 | else if (TREE_CODE (type) == FUNCTION_TYPE) |
8d08fdba | 11121 | { |
eeb75383 | 11122 | if (current_class_type == NULL_TREE || friendp) |
14ae7e7d JM |
11123 | type = build_cplus_method_type (ctype, TREE_TYPE (type), |
11124 | TYPE_ARG_TYPES (type)); | |
8d08fdba MS |
11125 | else |
11126 | { | |
33bd39a2 | 11127 | error ("cannot declare member function `%T::%s' within `%T'", |
a3203465 | 11128 | ctype, name, current_class_type); |
8d08fdba MS |
11129 | return void_type_node; |
11130 | } | |
11131 | } | |
5566b478 | 11132 | else if (RIDBIT_SETP (RID_TYPEDEF, specbits) |
d0f062fb | 11133 | || COMPLETE_TYPE_P (complete_type (ctype))) |
8d08fdba | 11134 | { |
2c73f9f5 | 11135 | /* Have to move this code elsewhere in this function. |
db5ae43f MS |
11136 | this code is used for i.e., typedef int A::M; M *pm; |
11137 | ||
11138 | It is? How? jason 10/2/94 */ | |
8d08fdba | 11139 | |
8d08fdba MS |
11140 | if (current_class_type) |
11141 | { | |
33bd39a2 | 11142 | error ("cannot declare member `%T::%s' within `%T'", |
db5ae43f MS |
11143 | ctype, name, current_class_type); |
11144 | return void_type_node; | |
8d08fdba | 11145 | } |
8d08fdba | 11146 | } |
8d08fdba | 11147 | else |
eeb75383 | 11148 | { |
7a228918 | 11149 | cxx_incomplete_type_error (NULL_TREE, ctype); |
eeb75383 | 11150 | return error_mark_node; |
d363e7bf | 11151 | } |
8d08fdba MS |
11152 | |
11153 | declarator = sname; | |
11154 | } | |
8d08fdba | 11155 | else if (TREE_CODE (sname) == SCOPE_REF) |
a98facb0 | 11156 | abort (); |
8d08fdba MS |
11157 | else |
11158 | { | |
11159 | done_scoping: | |
11160 | declarator = TREE_OPERAND (declarator, 1); | |
11161 | if (declarator && TREE_CODE (declarator) == CALL_EXPR) | |
11162 | /* In this case, we will deal with it later. */ | |
11163 | ; | |
9e259dd1 MM |
11164 | else if (TREE_CODE (type) == FUNCTION_TYPE) |
11165 | type = build_cplus_method_type (ctype, TREE_TYPE (type), | |
11166 | TYPE_ARG_TYPES (type)); | |
8d08fdba MS |
11167 | } |
11168 | } | |
11169 | break; | |
11170 | ||
11171 | case BIT_NOT_EXPR: | |
11172 | declarator = TREE_OPERAND (declarator, 0); | |
11173 | break; | |
11174 | ||
50ad9642 MM |
11175 | case BASELINK: |
11176 | declarator = BASELINK_FUNCTIONS (declarator); | |
11177 | break; | |
11178 | ||
8d08fdba MS |
11179 | case RECORD_TYPE: |
11180 | case UNION_TYPE: | |
11181 | case ENUMERAL_TYPE: | |
11182 | declarator = NULL_TREE; | |
11183 | break; | |
11184 | ||
11185 | case ERROR_MARK: | |
11186 | declarator = NULL_TREE; | |
11187 | break; | |
11188 | ||
11189 | default: | |
a98facb0 | 11190 | abort (); |
8d08fdba MS |
11191 | } |
11192 | } | |
11193 | ||
91d231cb | 11194 | if (returned_attrs) |
1eab9b56 | 11195 | { |
91d231cb JM |
11196 | if (attrlist) |
11197 | *attrlist = chainon (returned_attrs, *attrlist); | |
1eab9b56 | 11198 | else |
91d231cb | 11199 | attrlist = &returned_attrs; |
1eab9b56 JM |
11200 | } |
11201 | ||
419c6212 JM |
11202 | /* Now TYPE has the actual type. */ |
11203 | ||
55b3d665 JM |
11204 | /* Did array size calculations overflow? */ |
11205 | ||
11206 | if (TREE_CODE (type) == ARRAY_TYPE | |
11207 | && COMPLETE_TYPE_P (type) | |
11208 | && TREE_OVERFLOW (TYPE_SIZE (type))) | |
11209 | { | |
11210 | error ("size of array `%s' is too large", name); | |
aba649ba | 11211 | /* If we proceed with the array type as it is, we'll eventually |
55b3d665 JM |
11212 | crash in tree_low_cst(). */ |
11213 | type = error_mark_node; | |
11214 | } | |
11215 | ||
34fc957d | 11216 | if (explicitp == 1 || (explicitp && friendp)) |
db5ae43f | 11217 | { |
34fc957d NS |
11218 | /* [dcl.fct.spec] The explicit specifier shall only be used in |
11219 | declarations of constructors within a class definition. */ | |
11220 | error ("only declarations of constructors can be `explicit'"); | |
db5ae43f MS |
11221 | explicitp = 0; |
11222 | } | |
11223 | ||
f30432d7 MS |
11224 | if (RIDBIT_SETP (RID_MUTABLE, specbits)) |
11225 | { | |
34fc957d NS |
11226 | if (current_class_name == NULL_TREE || decl_context == PARM || friendp) |
11227 | { | |
11228 | error ("non-member `%s' cannot be declared `mutable'", name); | |
11229 | RIDBIT_RESET (RID_MUTABLE, specbits); | |
11230 | } | |
11231 | else if (decl_context == TYPENAME || RIDBIT_SETP (RID_TYPEDEF, specbits)) | |
f30432d7 | 11232 | { |
34fc957d | 11233 | error ("non-object member `%s' cannot be declared `mutable'", name); |
f30432d7 MS |
11234 | RIDBIT_RESET (RID_MUTABLE, specbits); |
11235 | } | |
34fc957d NS |
11236 | else if (TREE_CODE (type) == FUNCTION_TYPE |
11237 | || TREE_CODE (type) == METHOD_TYPE) | |
11238 | { | |
11239 | error ("function `%s' cannot be declared `mutable'", name); | |
11240 | RIDBIT_RESET (RID_MUTABLE, specbits); | |
11241 | } | |
f30432d7 MS |
11242 | else if (staticp) |
11243 | { | |
8251199e | 11244 | error ("static `%s' cannot be declared `mutable'", name); |
f30432d7 MS |
11245 | RIDBIT_RESET (RID_MUTABLE, specbits); |
11246 | } | |
34fc957d NS |
11247 | else if (type_quals & TYPE_QUAL_CONST) |
11248 | { | |
11249 | error ("const `%s' cannot be declared `mutable'", name); | |
4f2b0fb2 | 11250 | RIDBIT_RESET (RID_MUTABLE, specbits); |
34fc957d | 11251 | } |
f30432d7 MS |
11252 | } |
11253 | ||
efe38fab JM |
11254 | if (declarator == NULL_TREE |
11255 | || TREE_CODE (declarator) == IDENTIFIER_NODE | |
11256 | || (TREE_CODE (declarator) == TEMPLATE_ID_EXPR | |
11257 | && (TREE_CODE (type) == FUNCTION_TYPE | |
11258 | || TREE_CODE (type) == METHOD_TYPE))) | |
11259 | /* OK */; | |
11260 | else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) | |
11261 | { | |
33bd39a2 | 11262 | error ("template-id `%D' used as a declarator", declarator); |
efe38fab JM |
11263 | declarator = dname; |
11264 | } | |
11265 | else | |
419c6212 | 11266 | /* Unexpected declarator format. */ |
a98facb0 | 11267 | abort (); |
efe38fab | 11268 | |
419c6212 JM |
11269 | /* If this is declaring a typedef name, return a TYPE_DECL. */ |
11270 | ||
fc378698 | 11271 | if (RIDBIT_SETP (RID_TYPEDEF, specbits) && decl_context != TYPENAME) |
8d08fdba MS |
11272 | { |
11273 | tree decl; | |
11274 | ||
11275 | /* Note that the grammar rejects storage classes | |
11276 | in typenames, fields or parameters. */ | |
eff71ab0 PB |
11277 | if (current_lang_name == lang_name_java) |
11278 | TYPE_FOR_JAVA (type) = 1; | |
8d08fdba | 11279 | |
d2e5ee5c MS |
11280 | if (decl_context == FIELD) |
11281 | { | |
094fe153 | 11282 | if (declarator == constructor_name (current_class_type)) |
33bd39a2 | 11283 | pedwarn ("ISO C++ forbids nested type `%D' with same name as enclosing class", |
d2e5ee5c MS |
11284 | declarator); |
11285 | decl = build_lang_decl (TYPE_DECL, declarator, type); | |
d2e5ee5c MS |
11286 | } |
11287 | else | |
6bda7a5e NS |
11288 | { |
11289 | decl = build_decl (TYPE_DECL, declarator, type); | |
11290 | if (!current_function_decl) | |
11291 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); | |
11292 | } | |
11293 | ||
9188c363 MM |
11294 | /* If the user declares "typedef struct {...} foo" then the |
11295 | struct will have an anonymous name. Fill that name in now. | |
11296 | Nothing can refer to it, so nothing needs know about the name | |
11297 | change. */ | |
8d08fdba | 11298 | if (type != error_mark_node |
2bdb0643 | 11299 | && declarator |
8d08fdba MS |
11300 | && TYPE_NAME (type) |
11301 | && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL | |
1951a1b6 | 11302 | && TYPE_ANONYMOUS_P (type) |
ac905924 JM |
11303 | /* Don't do this if there are attributes. */ |
11304 | && (!attrlist || !*attrlist) | |
89d684bb | 11305 | && cp_type_quals (type) == TYPE_UNQUALIFIED) |
8d08fdba | 11306 | { |
dcd08efc JM |
11307 | tree oldname = TYPE_NAME (type); |
11308 | tree t; | |
11309 | ||
2c73f9f5 | 11310 | /* Replace the anonymous name with the real name everywhere. */ |
8d08fdba | 11311 | lookup_tag_reverse (type, declarator); |
dcd08efc JM |
11312 | for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) |
11313 | if (TYPE_NAME (t) == oldname) | |
11314 | TYPE_NAME (t) = decl; | |
8d08fdba MS |
11315 | |
11316 | if (TYPE_LANG_SPECIFIC (type)) | |
11317 | TYPE_WAS_ANONYMOUS (type) = 1; | |
11318 | ||
33964bf4 MM |
11319 | /* If this is a typedef within a template class, the nested |
11320 | type is a (non-primary) template. The name for the | |
11321 | template needs updating as well. */ | |
11322 | if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type)) | |
68642fb6 | 11323 | DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)) |
33964bf4 MM |
11324 | = TYPE_IDENTIFIER (type); |
11325 | ||
50a6dbd7 JM |
11326 | /* FIXME remangle member functions; member functions of a |
11327 | type with external linkage have external linkage. */ | |
fc378698 | 11328 | } |
fc378698 | 11329 | |
8d08fdba MS |
11330 | if (TREE_CODE (type) == OFFSET_TYPE || TREE_CODE (type) == METHOD_TYPE) |
11331 | { | |
8251199e | 11332 | cp_error_at ("typedef name may not be class-qualified", decl); |
a3203465 | 11333 | return NULL_TREE; |
8d08fdba MS |
11334 | } |
11335 | else if (quals) | |
11336 | { | |
11337 | if (ctype == NULL_TREE) | |
11338 | { | |
11339 | if (TREE_CODE (type) != METHOD_TYPE) | |
cb9a3ff8 | 11340 | cp_error_at ("invalid type qualifier for non-member function type", decl); |
8d08fdba MS |
11341 | else |
11342 | ctype = TYPE_METHOD_BASETYPE (type); | |
11343 | } | |
11344 | if (ctype != NULL_TREE) | |
11345 | grok_method_quals (ctype, decl, quals); | |
11346 | } | |
11347 | ||
11348 | if (RIDBIT_SETP (RID_SIGNED, specbits) | |
11349 | || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))) | |
11350 | C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1; | |
11351 | ||
c91a56d2 MS |
11352 | bad_specifiers (decl, "type", virtualp, quals != NULL_TREE, |
11353 | inlinep, friendp, raises != NULL_TREE); | |
11354 | ||
8d08fdba MS |
11355 | return decl; |
11356 | } | |
11357 | ||
11358 | /* Detect the case of an array type of unspecified size | |
11359 | which came, as such, direct from a typedef name. | |
8d6e459d NS |
11360 | We must copy the type, so that the array's domain can be |
11361 | individually set by the object's initializer. */ | |
8d08fdba | 11362 | |
8d6e459d NS |
11363 | if (type && typedef_type |
11364 | && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type) | |
55b3d665 | 11365 | && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type)) |
8d6e459d | 11366 | type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE); |
8d08fdba | 11367 | |
969fd501 NS |
11368 | /* Detect where we're using a typedef of function type to declare a |
11369 | function. last_function_parms will not be set, so we must create | |
11370 | it now. */ | |
11371 | ||
11372 | if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE) | |
11373 | { | |
11374 | tree decls = NULL_TREE; | |
11375 | tree args; | |
11376 | ||
11377 | for (args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args)) | |
11378 | { | |
8e51619a | 11379 | tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args)); |
969fd501 NS |
11380 | |
11381 | TREE_CHAIN (decl) = decls; | |
11382 | decls = decl; | |
11383 | } | |
11384 | ||
11385 | last_function_parms = nreverse (decls); | |
11386 | } | |
11387 | ||
8d08fdba MS |
11388 | /* If this is a type name (such as, in a cast or sizeof), |
11389 | compute the type and return it now. */ | |
11390 | ||
11391 | if (decl_context == TYPENAME) | |
11392 | { | |
11393 | /* Note that the grammar rejects storage classes | |
11394 | in typenames, fields or parameters. */ | |
91063b51 | 11395 | if (type_quals != TYPE_UNQUALIFIED) |
6eabb241 | 11396 | type_quals = TYPE_UNQUALIFIED; |
8d08fdba MS |
11397 | |
11398 | /* Special case: "friend class foo" looks like a TYPENAME context. */ | |
11399 | if (friendp) | |
11400 | { | |
91063b51 | 11401 | if (type_quals != TYPE_UNQUALIFIED) |
b7484fbe | 11402 | { |
33bd39a2 | 11403 | error ("type qualifiers specified for friend class declaration"); |
91063b51 | 11404 | type_quals = TYPE_UNQUALIFIED; |
b7484fbe MS |
11405 | } |
11406 | if (inlinep) | |
11407 | { | |
33bd39a2 | 11408 | error ("`inline' specified for friend class declaration"); |
b7484fbe MS |
11409 | inlinep = 0; |
11410 | } | |
f2ae0c45 JM |
11411 | |
11412 | /* Until core issue 180 is resolved, allow 'friend typename A::B'. | |
c00996a3 | 11413 | But don't allow implicit typenames except with a class-key. */ |
f2ae0c45 JM |
11414 | if (!current_aggr && (TREE_CODE (type) != TYPENAME_TYPE |
11415 | || IMPLICIT_TYPENAME_P (type))) | |
4b054b80 NS |
11416 | { |
11417 | if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) | |
33bd39a2 | 11418 | pedwarn ("template parameters cannot be friends"); |
f2ae0c45 | 11419 | else if (TREE_CODE (type) == TYPENAME_TYPE) |
33bd39a2 | 11420 | pedwarn ("\ |
f2ae0c45 JM |
11421 | friend declaration requires class-key, i.e. `friend class %T::%T'", |
11422 | constructor_name (current_class_type), | |
11423 | TYPE_IDENTIFIER (type)); | |
4b054b80 | 11424 | else |
33bd39a2 | 11425 | pedwarn ("\ |
f2ae0c45 JM |
11426 | friend declaration requires class-key, i.e. `friend %#T'", |
11427 | type); | |
4b054b80 | 11428 | } |
b7484fbe MS |
11429 | |
11430 | /* Only try to do this stuff if we didn't already give up. */ | |
11431 | if (type != integer_type_node) | |
11432 | { | |
ab04e34b | 11433 | decl_type_access_control (TYPE_NAME (type)); |
d363e7bf | 11434 | |
b7484fbe MS |
11435 | /* A friendly class? */ |
11436 | if (current_class_type) | |
11437 | make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type)); | |
11438 | else | |
33bd39a2 | 11439 | error ("trying to make class `%T' a friend of global scope", |
2ae7bada | 11440 | type); |
d363e7bf | 11441 | |
b7484fbe MS |
11442 | type = void_type_node; |
11443 | } | |
8d08fdba MS |
11444 | } |
11445 | else if (quals) | |
11446 | { | |
8d08fdba MS |
11447 | if (ctype == NULL_TREE) |
11448 | { | |
41cbc04c | 11449 | if (TREE_CODE (type) != METHOD_TYPE) |
33bd39a2 | 11450 | error ("invalid qualifiers on non-member function type"); |
41cbc04c NS |
11451 | else |
11452 | ctype = TYPE_METHOD_BASETYPE (type); | |
11453 | } | |
11454 | if (ctype) | |
11455 | { | |
11456 | tree dummy = build_decl (TYPE_DECL, declarator, type); | |
11457 | grok_method_quals (ctype, dummy, quals); | |
11458 | type = TREE_TYPE (dummy); | |
8d08fdba | 11459 | } |
8d08fdba MS |
11460 | } |
11461 | ||
11462 | return type; | |
11463 | } | |
11464 | else if (declarator == NULL_TREE && decl_context != PARM | |
db5ae43f | 11465 | && decl_context != CATCHPARM |
8d08fdba MS |
11466 | && TREE_CODE (type) != UNION_TYPE |
11467 | && ! bitfield) | |
11468 | { | |
33bd39a2 | 11469 | error ("abstract declarator `%T' used as declaration", type); |
8d08fdba MS |
11470 | declarator = make_anon_name (); |
11471 | } | |
11472 | ||
11473 | /* `void' at top level (not within pointer) | |
11474 | is allowed only in typedefs or type names. | |
11475 | We don't complain about parms either, but that is because | |
11476 | a better error message can be made later. */ | |
11477 | ||
a1774733 | 11478 | if (TREE_CODE (type) == VOID_TYPE && decl_context != PARM) |
8d08fdba | 11479 | { |
b7484fbe | 11480 | if (! declarator) |
8251199e | 11481 | error ("unnamed variable or field declared void"); |
b7484fbe | 11482 | else if (TREE_CODE (declarator) == IDENTIFIER_NODE) |
8d08fdba MS |
11483 | { |
11484 | if (IDENTIFIER_OPNAME_P (declarator)) | |
a98facb0 | 11485 | abort (); |
8d08fdba | 11486 | else |
8251199e | 11487 | error ("variable or field `%s' declared void", name); |
8d08fdba MS |
11488 | } |
11489 | else | |
8251199e | 11490 | error ("variable or field declared void"); |
8d08fdba MS |
11491 | type = integer_type_node; |
11492 | } | |
11493 | ||
11494 | /* Now create the decl, which may be a VAR_DECL, a PARM_DECL | |
11495 | or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */ | |
11496 | ||
14ae7e7d JM |
11497 | if (decl_context == PARM || decl_context == CATCHPARM) |
11498 | { | |
11499 | if (ctype || in_namespace) | |
11500 | error ("cannot use `::' in parameter declaration"); | |
11501 | ||
11502 | /* A parameter declared as an array of T is really a pointer to T. | |
11503 | One declared as a function is really a pointer to a function. | |
11504 | One declared as a member is really a pointer to member. */ | |
11505 | ||
11506 | if (TREE_CODE (type) == ARRAY_TYPE) | |
11507 | { | |
11508 | /* Transfer const-ness of array into that of type pointed to. */ | |
11509 | type = build_pointer_type (TREE_TYPE (type)); | |
91063b51 | 11510 | type_quals = TYPE_UNQUALIFIED; |
14ae7e7d JM |
11511 | } |
11512 | else if (TREE_CODE (type) == FUNCTION_TYPE) | |
11513 | type = build_pointer_type (type); | |
11514 | else if (TREE_CODE (type) == OFFSET_TYPE) | |
11515 | type = build_pointer_type (type); | |
14ae7e7d | 11516 | } |
68642fb6 | 11517 | |
8d08fdba MS |
11518 | { |
11519 | register tree decl; | |
11520 | ||
11521 | if (decl_context == PARM) | |
11522 | { | |
8e51619a | 11523 | decl = cp_build_parm_decl (declarator, type); |
8d08fdba MS |
11524 | |
11525 | bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE, | |
11526 | inlinep, friendp, raises != NULL_TREE); | |
8d08fdba MS |
11527 | } |
11528 | else if (decl_context == FIELD) | |
11529 | { | |
11530 | if (type == error_mark_node) | |
11531 | { | |
11532 | /* Happens when declaring arrays of sizes which | |
11533 | are error_mark_node, for example. */ | |
11534 | decl = NULL_TREE; | |
11535 | } | |
997a088c | 11536 | else if (in_namespace && !friendp) |
05008fb9 MM |
11537 | { |
11538 | /* Something like struct S { int N::j; }; */ | |
33bd39a2 | 11539 | error ("invalid use of `::'"); |
05008fb9 MM |
11540 | decl = NULL_TREE; |
11541 | } | |
8d08fdba MS |
11542 | else if (TREE_CODE (type) == FUNCTION_TYPE) |
11543 | { | |
11544 | int publicp = 0; | |
e1467ff2 | 11545 | tree function_context; |
8d08fdba | 11546 | |
72b7eeff MS |
11547 | /* We catch the others as conflicts with the builtin |
11548 | typedefs. */ | |
11549 | if (friendp && declarator == ridpointers[(int) RID_SIGNED]) | |
11550 | { | |
33bd39a2 | 11551 | error ("function `%D' cannot be declared friend", |
72b7eeff MS |
11552 | declarator); |
11553 | friendp = 0; | |
11554 | } | |
11555 | ||
8d08fdba MS |
11556 | if (friendp == 0) |
11557 | { | |
11558 | if (ctype == NULL_TREE) | |
11559 | ctype = current_class_type; | |
11560 | ||
11561 | if (ctype == NULL_TREE) | |
11562 | { | |
33bd39a2 | 11563 | error ("can't make `%D' into a method -- not in a class", |
8d08fdba MS |
11564 | declarator); |
11565 | return void_type_node; | |
11566 | } | |
11567 | ||
11568 | /* ``A union may [ ... ] not [ have ] virtual functions.'' | |
11569 | ARM 9.5 */ | |
11570 | if (virtualp && TREE_CODE (ctype) == UNION_TYPE) | |
11571 | { | |
33bd39a2 | 11572 | error ("function `%D' declared virtual inside a union", |
8d08fdba MS |
11573 | declarator); |
11574 | return void_type_node; | |
11575 | } | |
11576 | ||
596ea4e5 AS |
11577 | if (declarator == ansi_opname (NEW_EXPR) |
11578 | || declarator == ansi_opname (VEC_NEW_EXPR) | |
11579 | || declarator == ansi_opname (DELETE_EXPR) | |
11580 | || declarator == ansi_opname (VEC_DELETE_EXPR)) | |
8d08fdba MS |
11581 | { |
11582 | if (virtualp) | |
11583 | { | |
33bd39a2 | 11584 | error ("`%D' cannot be declared virtual, since it is always static", |
8d08fdba MS |
11585 | declarator); |
11586 | virtualp = 0; | |
11587 | } | |
11588 | } | |
11589 | else if (staticp < 2) | |
14ae7e7d JM |
11590 | type = build_cplus_method_type (ctype, TREE_TYPE (type), |
11591 | TYPE_ARG_TYPES (type)); | |
8d08fdba MS |
11592 | } |
11593 | ||
11594 | /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */ | |
68642fb6 | 11595 | function_context = (ctype != NULL_TREE) ? |
4f1c5b7d | 11596 | decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE; |
e1467ff2 MM |
11597 | publicp = (! friendp || ! staticp) |
11598 | && function_context == NULL_TREE; | |
68642fb6 | 11599 | decl = grokfndecl (ctype, type, |
386b8a85 JM |
11600 | TREE_CODE (declarator) != TEMPLATE_ID_EXPR |
11601 | ? declarator : dname, | |
11602 | declarator, | |
7a8f9fa9 | 11603 | virtualp, flags, quals, raises, |
386b8a85 | 11604 | friendp ? -1 : 0, friendp, publicp, inlinep, |
2c73f9f5 | 11605 | funcdef_flag, template_count, in_namespace); |
20496fa2 | 11606 | if (decl == NULL_TREE) |
3ddfb0e6 | 11607 | return decl; |
6125f3be DE |
11608 | #if 0 |
11609 | /* This clobbers the attrs stored in `decl' from `attrlist'. */ | |
91d231cb JM |
11610 | /* The decl and setting of decl_attr is also turned off. */ |
11611 | decl = build_decl_attribute_variant (decl, decl_attr); | |
6125f3be | 11612 | #endif |
f0e01782 | 11613 | |
cc804e51 MM |
11614 | /* [class.conv.ctor] |
11615 | ||
11616 | A constructor declared without the function-specifier | |
11617 | explicit that can be called with a single parameter | |
11618 | specifies a conversion from the type of its first | |
11619 | parameter to the type of its class. Such a constructor | |
11620 | is called a converting constructor. */ | |
db5ae43f MS |
11621 | if (explicitp == 2) |
11622 | DECL_NONCONVERTING_P (decl) = 1; | |
cc804e51 MM |
11623 | else if (DECL_CONSTRUCTOR_P (decl)) |
11624 | { | |
11625 | /* The constructor can be called with exactly one | |
11626 | parameter if there is at least one parameter, and | |
11627 | any subsequent parameters have default arguments. | |
e0fff4b3 JM |
11628 | Ignore any compiler-added parms. */ |
11629 | tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (decl); | |
cc804e51 MM |
11630 | |
11631 | if (arg_types == void_list_node | |
68642fb6 UD |
11632 | || (arg_types |
11633 | && TREE_CHAIN (arg_types) | |
cc804e51 MM |
11634 | && TREE_CHAIN (arg_types) != void_list_node |
11635 | && !TREE_PURPOSE (TREE_CHAIN (arg_types)))) | |
11636 | DECL_NONCONVERTING_P (decl) = 1; | |
11637 | } | |
8d08fdba MS |
11638 | } |
11639 | else if (TREE_CODE (type) == METHOD_TYPE) | |
11640 | { | |
faae18ab MS |
11641 | /* We only get here for friend declarations of |
11642 | members of other classes. */ | |
8d08fdba MS |
11643 | /* All method decls are public, so tell grokfndecl to set |
11644 | TREE_PUBLIC, also. */ | |
386b8a85 | 11645 | decl = grokfndecl (ctype, type, declarator, declarator, |
7a8f9fa9 | 11646 | virtualp, flags, quals, raises, |
386b8a85 | 11647 | friendp ? -1 : 0, friendp, 1, 0, funcdef_flag, |
2c73f9f5 | 11648 | template_count, in_namespace); |
f0e01782 MS |
11649 | if (decl == NULL_TREE) |
11650 | return NULL_TREE; | |
8d08fdba | 11651 | } |
5566b478 | 11652 | else if (!staticp && ! processing_template_decl |
d0f062fb | 11653 | && !COMPLETE_TYPE_P (complete_type (type)) |
8d08fdba MS |
11654 | && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0)) |
11655 | { | |
b7484fbe | 11656 | if (declarator) |
33bd39a2 | 11657 | error ("field `%D' has incomplete type", declarator); |
b7484fbe | 11658 | else |
33bd39a2 | 11659 | error ("name `%T' has incomplete type", type); |
8d08fdba MS |
11660 | |
11661 | /* If we're instantiating a template, tell them which | |
11662 | instantiation made the field's type be incomplete. */ | |
11663 | if (current_class_type | |
11664 | && TYPE_NAME (current_class_type) | |
d2e5ee5c | 11665 | && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type)) |
8d08fdba MS |
11666 | && declspecs && TREE_VALUE (declspecs) |
11667 | && TREE_TYPE (TREE_VALUE (declspecs)) == type) | |
33bd39a2 | 11668 | error (" in instantiation of template `%T'", |
db5ae43f MS |
11669 | current_class_type); |
11670 | ||
8d08fdba MS |
11671 | type = error_mark_node; |
11672 | decl = NULL_TREE; | |
11673 | } | |
11674 | else | |
11675 | { | |
11676 | if (friendp) | |
11677 | { | |
cb9a3ff8 | 11678 | error ("`%s' is neither function nor member function; cannot be declared friend", |
8d08fdba MS |
11679 | IDENTIFIER_POINTER (declarator)); |
11680 | friendp = 0; | |
11681 | } | |
11682 | decl = NULL_TREE; | |
11683 | } | |
11684 | ||
11685 | if (friendp) | |
11686 | { | |
11687 | /* Friends are treated specially. */ | |
11688 | if (ctype == current_class_type) | |
8251199e | 11689 | warning ("member functions are implicitly friends of their class"); |
ab04e34b NS |
11690 | else |
11691 | { | |
11692 | tree t = NULL_TREE; | |
11693 | if (decl && DECL_NAME (decl)) | |
11694 | { | |
11695 | if (template_class_depth (current_class_type) == 0) | |
11696 | { | |
11697 | decl | |
11698 | = check_explicit_specialization | |
11699 | (declarator, decl, | |
11700 | template_count, 2 * (funcdef_flag != 0) + 4); | |
11701 | if (decl == error_mark_node) | |
11702 | return error_mark_node; | |
11703 | } | |
11704 | ||
11705 | t = do_friend (ctype, declarator, decl, | |
11706 | last_function_parms, *attrlist, | |
11707 | flags, quals, funcdef_flag); | |
11708 | } | |
11709 | if (t && funcdef_flag) | |
11710 | return t; | |
11711 | ||
11712 | return void_type_node; | |
11713 | } | |
8d08fdba MS |
11714 | } |
11715 | ||
11716 | /* Structure field. It may not be a function, except for C++ */ | |
11717 | ||
11718 | if (decl == NULL_TREE) | |
11719 | { | |
8d08fdba MS |
11720 | if (initialized) |
11721 | { | |
3ac3d9ea MM |
11722 | if (!staticp) |
11723 | { | |
11724 | /* An attempt is being made to initialize a non-static | |
11725 | member. But, from [class.mem]: | |
68642fb6 | 11726 | |
3ac3d9ea MM |
11727 | 4 A member-declarator can contain a |
11728 | constant-initializer only if it declares a static | |
11729 | member (_class.static_) of integral or enumeration | |
68642fb6 | 11730 | type, see _class.static.data_. |
3ac3d9ea MM |
11731 | |
11732 | This used to be relatively common practice, but | |
11733 | the rest of the compiler does not correctly | |
11734 | handle the initialization unless the member is | |
11735 | static so we make it static below. */ | |
33bd39a2 | 11736 | pedwarn ("ISO C++ forbids initialization of member `%D'", |
3ac3d9ea | 11737 | declarator); |
33bd39a2 | 11738 | pedwarn ("making `%D' static", declarator); |
3ac3d9ea MM |
11739 | staticp = 1; |
11740 | } | |
11741 | ||
6ba89f8e MM |
11742 | if (uses_template_parms (type)) |
11743 | /* We'll check at instantiation time. */ | |
11744 | ; | |
11745 | else if (check_static_variable_definition (declarator, | |
11746 | type)) | |
11747 | /* If we just return the declaration, crashes | |
11748 | will sometimes occur. We therefore return | |
72a93143 JM |
11749 | void_type_node, as if this was a friend |
11750 | declaration, to cause callers to completely | |
11751 | ignore this declaration. */ | |
6ba89f8e | 11752 | return void_type_node; |
8d08fdba MS |
11753 | } |
11754 | ||
8ebeee52 | 11755 | /* 9.2p13 [class.mem] */ |
4d7614e9 | 11756 | if (declarator == constructor_name (current_class_type) |
e54930f9 JM |
11757 | /* The standard does not allow non-static data members |
11758 | here either, but we agreed at the 10/99 meeting | |
11759 | to change that in TC 1 so that they are allowed in | |
11760 | classes with no user-defined constructors. */ | |
11761 | && staticp) | |
33bd39a2 | 11762 | pedwarn ("ISO C++ forbids static data member `%D' with same name as enclosing class", |
8ebeee52 JM |
11763 | declarator); |
11764 | ||
3ac3d9ea | 11765 | if (staticp) |
8d08fdba | 11766 | { |
f18a14bc MM |
11767 | /* C++ allows static class members. All other work |
11768 | for this is done by grokfield. */ | |
4ce3d537 | 11769 | decl = build_lang_decl (VAR_DECL, declarator, type); |
5b605f68 MS |
11770 | TREE_STATIC (decl) = 1; |
11771 | /* In class context, 'static' means public access. */ | |
3ac3d9ea | 11772 | TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = 1; |
8d08fdba MS |
11773 | } |
11774 | else | |
11775 | { | |
721c3b42 | 11776 | decl = build_decl (FIELD_DECL, declarator, type); |
2bf105ab | 11777 | DECL_NONADDRESSABLE_P (decl) = bitfield; |
8d08fdba MS |
11778 | if (RIDBIT_SETP (RID_MUTABLE, specbits)) |
11779 | { | |
11780 | DECL_MUTABLE_P (decl) = 1; | |
11781 | RIDBIT_RESET (RID_MUTABLE, specbits); | |
11782 | } | |
11783 | } | |
11784 | ||
11785 | bad_specifiers (decl, "field", virtualp, quals != NULL_TREE, | |
11786 | inlinep, friendp, raises != NULL_TREE); | |
11787 | } | |
11788 | } | |
11789 | else if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE) | |
11790 | { | |
386b8a85 | 11791 | tree original_name; |
8d08fdba MS |
11792 | int publicp = 0; |
11793 | ||
11794 | if (! declarator) | |
11795 | return NULL_TREE; | |
11796 | ||
386b8a85 JM |
11797 | if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) |
11798 | original_name = dname; | |
11799 | else | |
11800 | original_name = declarator; | |
11801 | ||
8926095f | 11802 | if (RIDBIT_SETP (RID_AUTO, specbits)) |
8251199e | 11803 | error ("storage class `auto' invalid for function `%s'", name); |
8926095f | 11804 | else if (RIDBIT_SETP (RID_REGISTER, specbits)) |
8251199e | 11805 | error ("storage class `register' invalid for function `%s'", name); |
7a1f3f5f RH |
11806 | else if (RIDBIT_SETP (RID_THREAD, specbits)) |
11807 | error ("storage class `__thread' invalid for function `%s'", name); | |
8d08fdba MS |
11808 | |
11809 | /* Function declaration not at top level. | |
11810 | Storage classes other than `extern' are not allowed | |
11811 | and `extern' makes no difference. */ | |
a9aedbc2 | 11812 | if (! toplevel_bindings_p () |
8926095f MS |
11813 | && (RIDBIT_SETP (RID_STATIC, specbits) |
11814 | || RIDBIT_SETP (RID_INLINE, specbits)) | |
8d08fdba | 11815 | && pedantic) |
8926095f MS |
11816 | { |
11817 | if (RIDBIT_SETP (RID_STATIC, specbits)) | |
8251199e | 11818 | pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name); |
8926095f | 11819 | else |
8251199e | 11820 | pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name); |
8926095f | 11821 | } |
68642fb6 | 11822 | |
8d08fdba MS |
11823 | if (ctype == NULL_TREE) |
11824 | { | |
11825 | if (virtualp) | |
11826 | { | |
8251199e | 11827 | error ("virtual non-class function `%s'", name); |
8d08fdba MS |
11828 | virtualp = 0; |
11829 | } | |
8d08fdba MS |
11830 | } |
11831 | else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2) | |
14ae7e7d JM |
11832 | type = build_cplus_method_type (ctype, TREE_TYPE (type), |
11833 | TYPE_ARG_TYPES (type)); | |
8d08fdba | 11834 | |
eb66be0e | 11835 | /* Record presence of `static'. */ |
faae18ab | 11836 | publicp = (ctype != NULL_TREE |
a9aedbc2 | 11837 | || RIDBIT_SETP (RID_EXTERN, specbits) |
eb66be0e | 11838 | || !RIDBIT_SETP (RID_STATIC, specbits)); |
8d08fdba | 11839 | |
386b8a85 | 11840 | decl = grokfndecl (ctype, type, original_name, declarator, |
7a8f9fa9 | 11841 | virtualp, flags, quals, raises, |
75650646 | 11842 | 1, friendp, |
68642fb6 | 11843 | publicp, inlinep, funcdef_flag, |
2c73f9f5 | 11844 | template_count, in_namespace); |
f0e01782 MS |
11845 | if (decl == NULL_TREE) |
11846 | return NULL_TREE; | |
8d08fdba | 11847 | |
8d08fdba MS |
11848 | if (staticp == 1) |
11849 | { | |
0e339752 | 11850 | int invalid_static = 0; |
8d08fdba MS |
11851 | |
11852 | /* Don't allow a static member function in a class, and forbid | |
11853 | declaring main to be static. */ | |
11854 | if (TREE_CODE (type) == METHOD_TYPE) | |
11855 | { | |
33bd39a2 | 11856 | pedwarn ("cannot declare member function `%D' to have static linkage", decl); |
0e339752 | 11857 | invalid_static = 1; |
8d08fdba | 11858 | } |
8d08fdba MS |
11859 | else if (current_function_decl) |
11860 | { | |
11861 | /* FIXME need arm citation */ | |
8251199e | 11862 | error ("cannot declare static function inside another function"); |
0e339752 | 11863 | invalid_static = 1; |
8d08fdba MS |
11864 | } |
11865 | ||
0e339752 | 11866 | if (invalid_static) |
8d08fdba MS |
11867 | { |
11868 | staticp = 0; | |
11869 | RIDBIT_RESET (RID_STATIC, specbits); | |
11870 | } | |
11871 | } | |
8d08fdba MS |
11872 | } |
11873 | else | |
11874 | { | |
11875 | /* It's a variable. */ | |
11876 | ||
11877 | /* An uninitialized decl with `extern' is a reference. */ | |
68642fb6 UD |
11878 | decl = grokvardecl (type, declarator, &specbits, |
11879 | initialized, | |
11880 | (type_quals & TYPE_QUAL_CONST) != 0, | |
9e259dd1 | 11881 | ctype ? ctype : in_namespace); |
8d08fdba MS |
11882 | bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE, |
11883 | inlinep, friendp, raises != NULL_TREE); | |
11884 | ||
11885 | if (ctype) | |
11886 | { | |
f0e01782 | 11887 | DECL_CONTEXT (decl) = ctype; |
8d08fdba MS |
11888 | if (staticp == 1) |
11889 | { | |
33bd39a2 | 11890 | pedwarn ("`static' may not be used when defining (as opposed to declaring) a static data member"); |
8d08fdba MS |
11891 | staticp = 0; |
11892 | RIDBIT_RESET (RID_STATIC, specbits); | |
11893 | } | |
b7484fbe MS |
11894 | if (RIDBIT_SETP (RID_REGISTER, specbits) && TREE_STATIC (decl)) |
11895 | { | |
33bd39a2 | 11896 | error ("static member `%D' declared `register'", decl); |
b7484fbe MS |
11897 | RIDBIT_RESET (RID_REGISTER, specbits); |
11898 | } | |
f30432d7 | 11899 | if (RIDBIT_SETP (RID_EXTERN, specbits) && pedantic) |
8d08fdba | 11900 | { |
33bd39a2 | 11901 | pedwarn ("cannot explicitly declare member `%#D' to have extern linkage", |
f30432d7 | 11902 | decl); |
8d08fdba MS |
11903 | RIDBIT_RESET (RID_EXTERN, specbits); |
11904 | } | |
11905 | } | |
11906 | } | |
11907 | ||
34fc957d | 11908 | my_friendly_assert (!RIDBIT_SETP (RID_MUTABLE, specbits), 19990927); |
8d08fdba MS |
11909 | |
11910 | /* Record `register' declaration for warnings on & | |
11911 | and in case doing stupid register allocation. */ | |
11912 | ||
11913 | if (RIDBIT_SETP (RID_REGISTER, specbits)) | |
11914 | DECL_REGISTER (decl) = 1; | |
11915 | ||
8926095f MS |
11916 | if (RIDBIT_SETP (RID_EXTERN, specbits)) |
11917 | DECL_THIS_EXTERN (decl) = 1; | |
11918 | ||
faae18ab MS |
11919 | if (RIDBIT_SETP (RID_STATIC, specbits)) |
11920 | DECL_THIS_STATIC (decl) = 1; | |
11921 | ||
adecb3f4 MM |
11922 | /* Record constancy and volatility. There's no need to do this |
11923 | when processing a template; we'll do this for the instantiated | |
11924 | declaration based on the type of DECL. */ | |
11925 | if (!processing_template_decl) | |
11926 | c_apply_type_quals_to_decl (type_quals, decl); | |
8d08fdba MS |
11927 | |
11928 | return decl; | |
11929 | } | |
11930 | } | |
11931 | \f | |
11932 | /* Tell if a parmlist/exprlist looks like an exprlist or a parmlist. | |
11933 | An empty exprlist is a parmlist. An exprlist which | |
11934 | contains only identifiers at the global level | |
11935 | is a parmlist. Otherwise, it is an exprlist. */ | |
e92cc029 | 11936 | |
8d08fdba MS |
11937 | int |
11938 | parmlist_is_exprlist (exprs) | |
11939 | tree exprs; | |
11940 | { | |
11941 | if (exprs == NULL_TREE || TREE_PARMLIST (exprs)) | |
11942 | return 0; | |
11943 | ||
a9aedbc2 | 11944 | if (toplevel_bindings_p ()) |
8d08fdba MS |
11945 | { |
11946 | /* At the global level, if these are all identifiers, | |
11947 | then it is a parmlist. */ | |
11948 | while (exprs) | |
11949 | { | |
11950 | if (TREE_CODE (TREE_VALUE (exprs)) != IDENTIFIER_NODE) | |
11951 | return 1; | |
11952 | exprs = TREE_CHAIN (exprs); | |
11953 | } | |
11954 | return 0; | |
11955 | } | |
11956 | return 1; | |
11957 | } | |
11958 | ||
f181d4ae MM |
11959 | /* Subroutine of start_function. Ensure that each of the parameter |
11960 | types (as listed in PARMS) is complete, as is required for a | |
11961 | function definition. */ | |
e92cc029 | 11962 | |
8d08fdba MS |
11963 | static void |
11964 | require_complete_types_for_parms (parms) | |
11965 | tree parms; | |
11966 | { | |
07c88314 | 11967 | for (; parms; parms = TREE_CHAIN (parms)) |
8d08fdba | 11968 | { |
5cce22b6 NS |
11969 | if (VOID_TYPE_P (TREE_TYPE (parms))) |
11970 | /* grokparms will have already issued an error */ | |
11971 | TREE_TYPE (parms) = error_mark_node; | |
11972 | else if (complete_type_or_else (TREE_TYPE (parms), parms)) | |
42f989ff | 11973 | layout_decl (parms, 0); |
5cce22b6 NS |
11974 | else |
11975 | TREE_TYPE (parms) = error_mark_node; | |
8d08fdba MS |
11976 | } |
11977 | } | |
11978 | ||
838dfd8a | 11979 | /* Returns nonzero if T is a local variable. */ |
297e73d8 | 11980 | |
46e8c075 MM |
11981 | int |
11982 | local_variable_p (t) | |
11983 | tree t; | |
297e73d8 | 11984 | { |
68642fb6 | 11985 | if ((TREE_CODE (t) == VAR_DECL |
297e73d8 MM |
11986 | /* A VAR_DECL with a context that is a _TYPE is a static data |
11987 | member. */ | |
11988 | && !TYPE_P (CP_DECL_CONTEXT (t)) | |
11989 | /* Any other non-local variable must be at namespace scope. */ | |
46e8c075 | 11990 | && !DECL_NAMESPACE_SCOPE_P (t)) |
297e73d8 | 11991 | || (TREE_CODE (t) == PARM_DECL)) |
46e8c075 | 11992 | return 1; |
297e73d8 | 11993 | |
46e8c075 MM |
11994 | return 0; |
11995 | } | |
11996 | ||
838dfd8a | 11997 | /* Returns nonzero if T is an automatic local variable or a label. |
46e8c075 MM |
11998 | (These are the declarations that need to be remapped when the code |
11999 | containing them is duplicated.) */ | |
12000 | ||
12001 | int | |
12002 | nonstatic_local_decl_p (t) | |
12003 | tree t; | |
12004 | { | |
12005 | return ((local_variable_p (t) && !TREE_STATIC (t)) | |
12006 | || TREE_CODE (t) == LABEL_DECL | |
12007 | || TREE_CODE (t) == RESULT_DECL); | |
12008 | } | |
12009 | ||
12010 | /* Like local_variable_p, but suitable for use as a tree-walking | |
12011 | function. */ | |
12012 | ||
12013 | static tree | |
12014 | local_variable_p_walkfn (tp, walk_subtrees, data) | |
12015 | tree *tp; | |
12016 | int *walk_subtrees ATTRIBUTE_UNUSED; | |
12017 | void *data ATTRIBUTE_UNUSED; | |
12018 | { | |
68642fb6 | 12019 | return ((local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp)) |
46e8c075 | 12020 | ? *tp : NULL_TREE); |
297e73d8 MM |
12021 | } |
12022 | ||
12023 | /* Check that ARG, which is a default-argument expression for a | |
0e339752 | 12024 | parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if |
297e73d8 MM |
12025 | something goes wrong. DECL may also be a _TYPE node, rather than a |
12026 | DECL, if there is no DECL available. */ | |
12027 | ||
12028 | tree | |
12029 | check_default_argument (decl, arg) | |
12030 | tree decl; | |
12031 | tree arg; | |
12032 | { | |
12033 | tree var; | |
12034 | tree decl_type; | |
12035 | ||
12036 | if (TREE_CODE (arg) == DEFAULT_ARG) | |
12037 | /* We get a DEFAULT_ARG when looking at an in-class declaration | |
12038 | with a default argument. Ignore the argument for now; we'll | |
12039 | deal with it after the class is complete. */ | |
12040 | return arg; | |
12041 | ||
12042 | if (processing_template_decl || uses_template_parms (arg)) | |
12043 | /* We don't do anything checking until instantiation-time. Note | |
12044 | that there may be uninstantiated arguments even for an | |
12045 | instantiated function, since default arguments are not | |
12046 | instantiated until they are needed. */ | |
12047 | return arg; | |
12048 | ||
12049 | if (TYPE_P (decl)) | |
12050 | { | |
12051 | decl_type = decl; | |
12052 | decl = NULL_TREE; | |
12053 | } | |
12054 | else | |
12055 | decl_type = TREE_TYPE (decl); | |
12056 | ||
68642fb6 | 12057 | if (arg == error_mark_node |
297e73d8 MM |
12058 | || decl == error_mark_node |
12059 | || TREE_TYPE (arg) == error_mark_node | |
12060 | || decl_type == error_mark_node) | |
12061 | /* Something already went wrong. There's no need to check | |
12062 | further. */ | |
12063 | return error_mark_node; | |
12064 | ||
12065 | /* [dcl.fct.default] | |
68642fb6 | 12066 | |
297e73d8 MM |
12067 | A default argument expression is implicitly converted to the |
12068 | parameter type. */ | |
12069 | if (!TREE_TYPE (arg) | |
12070 | || !can_convert_arg (decl_type, TREE_TYPE (arg), arg)) | |
12071 | { | |
12072 | if (decl) | |
33bd39a2 | 12073 | error ("default argument for `%#D' has type `%T'", |
297e73d8 MM |
12074 | decl, TREE_TYPE (arg)); |
12075 | else | |
33bd39a2 | 12076 | error ("default argument for parameter of type `%T' has type `%T'", |
297e73d8 MM |
12077 | decl_type, TREE_TYPE (arg)); |
12078 | ||
12079 | return error_mark_node; | |
12080 | } | |
12081 | ||
12082 | /* [dcl.fct.default] | |
12083 | ||
12084 | Local variables shall not be used in default argument | |
68642fb6 | 12085 | expressions. |
297e73d8 MM |
12086 | |
12087 | The keyword `this' shall not be used in a default argument of a | |
12088 | member function. */ | |
5362b086 | 12089 | var = walk_tree_without_duplicates (&arg, local_variable_p_walkfn, |
ee94fce6 | 12090 | NULL); |
297e73d8 MM |
12091 | if (var) |
12092 | { | |
33bd39a2 | 12093 | error ("default argument `%E' uses local variable `%D'", |
297e73d8 MM |
12094 | arg, var); |
12095 | return error_mark_node; | |
12096 | } | |
12097 | ||
12098 | /* All is well. */ | |
12099 | return arg; | |
12100 | } | |
12101 | ||
8d08fdba MS |
12102 | /* Decode the list of parameter types for a function type. |
12103 | Given the list of things declared inside the parens, | |
12104 | return a list of types. | |
12105 | ||
5cce22b6 NS |
12106 | We determine whether ellipsis parms are used by PARMLIST_ELLIPSIS_P |
12107 | flag. If unset, we append void_list_node. A parmlist declared | |
12108 | as `(void)' is accepted as the empty parmlist. | |
12109 | ||
12110 | Also set last_function_parms to the chain of PARM_DECLs. */ | |
8d08fdba MS |
12111 | |
12112 | static tree | |
5cce22b6 | 12113 | grokparms (first_parm) |
8d08fdba | 12114 | tree first_parm; |
8d08fdba MS |
12115 | { |
12116 | tree result = NULL_TREE; | |
12117 | tree decls = NULL_TREE; | |
5cce22b6 NS |
12118 | int ellipsis = !first_parm || PARMLIST_ELLIPSIS_P (first_parm); |
12119 | tree parm, chain; | |
12120 | int any_error = 0; | |
8d08fdba | 12121 | |
5cce22b6 NS |
12122 | my_friendly_assert (!first_parm || TREE_PARMLIST (first_parm), 20001115); |
12123 | ||
12124 | for (parm = first_parm; parm != NULL_TREE; parm = chain) | |
8d08fdba | 12125 | { |
0657c69c | 12126 | tree type = NULL_TREE; |
98884b26 | 12127 | tree decl = TREE_VALUE (parm); |
5cce22b6 | 12128 | tree init = TREE_PURPOSE (parm); |
98884b26 | 12129 | tree specs, attrs; |
8d08fdba | 12130 | |
5cce22b6 NS |
12131 | chain = TREE_CHAIN (parm); |
12132 | /* @@ weak defense against parse errors. */ | |
12133 | if (TREE_CODE (decl) != VOID_TYPE | |
12134 | && TREE_CODE (decl) != TREE_LIST) | |
8d08fdba | 12135 | { |
5cce22b6 NS |
12136 | /* Give various messages as the need arises. */ |
12137 | if (TREE_CODE (decl) == STRING_CST) | |
33bd39a2 | 12138 | error ("invalid string constant `%E'", decl); |
5cce22b6 NS |
12139 | else if (TREE_CODE (decl) == INTEGER_CST) |
12140 | error ("invalid integer constant in parameter list, did you forget to give parameter name?"); | |
12141 | continue; | |
12142 | } | |
8d08fdba | 12143 | |
5cce22b6 NS |
12144 | if (parm == void_list_node) |
12145 | break; | |
8d08fdba | 12146 | |
98884b26 JM |
12147 | split_specs_attrs (TREE_PURPOSE (decl), &specs, &attrs); |
12148 | decl = grokdeclarator (TREE_VALUE (decl), specs, | |
12149 | PARM, init != NULL_TREE, &attrs); | |
5cce22b6 NS |
12150 | if (! decl || TREE_TYPE (decl) == error_mark_node) |
12151 | continue; | |
d363e7bf | 12152 | |
98884b26 JM |
12153 | if (attrs) |
12154 | cplus_decl_attributes (&decl, attrs, 0); | |
12155 | ||
5cce22b6 NS |
12156 | type = TREE_TYPE (decl); |
12157 | if (VOID_TYPE_P (type)) | |
12158 | { | |
12159 | if (same_type_p (type, void_type_node) | |
12160 | && !DECL_NAME (decl) && !result && !chain && !ellipsis) | |
12161 | /* this is a parmlist of `(void)', which is ok. */ | |
12162 | break; | |
7a228918 | 12163 | cxx_incomplete_type_error (decl, type); |
0657c69c MM |
12164 | /* It's not a good idea to actually create parameters of |
12165 | type `void'; other parts of the compiler assume that a | |
12166 | void type terminates the parameter list. */ | |
04f3dc2b | 12167 | type = error_mark_node; |
0657c69c | 12168 | TREE_TYPE (decl) = error_mark_node; |
5cce22b6 | 12169 | } |
8d08fdba | 12170 | |
d363e7bf | 12171 | if (type != error_mark_node) |
04f3dc2b MM |
12172 | { |
12173 | /* Top-level qualifiers on the parameters are | |
12174 | ignored for function types. */ | |
12175 | type = TYPE_MAIN_VARIANT (type); | |
12176 | if (TREE_CODE (type) == METHOD_TYPE) | |
12177 | { | |
33bd39a2 | 12178 | error ("parameter `%D' invalidly declared method type", decl); |
04f3dc2b MM |
12179 | type = build_pointer_type (type); |
12180 | TREE_TYPE (decl) = type; | |
12181 | } | |
12182 | else if (TREE_CODE (type) == OFFSET_TYPE) | |
12183 | { | |
33bd39a2 | 12184 | error ("parameter `%D' invalidly declared offset type", decl); |
04f3dc2b MM |
12185 | type = build_pointer_type (type); |
12186 | TREE_TYPE (decl) = type; | |
12187 | } | |
12188 | else if (abstract_virtuals_error (decl, type)) | |
a1c65f9f | 12189 | any_error = 1; /* Seems like a good idea. */ |
04f3dc2b MM |
12190 | else if (POINTER_TYPE_P (type)) |
12191 | { | |
12192 | /* [dcl.fct]/6, parameter types cannot contain pointers | |
12193 | (references) to arrays of unknown bound. */ | |
98979fe0 NS |
12194 | tree t = TREE_TYPE (type); |
12195 | int ptr = TYPE_PTR_P (type); | |
12196 | ||
12197 | while (1) | |
12198 | { | |
12199 | if (TYPE_PTR_P (t)) | |
12200 | ptr = 1; | |
12201 | else if (TREE_CODE (t) != ARRAY_TYPE) | |
12202 | break; | |
12203 | else if (!TYPE_DOMAIN (t)) | |
12204 | break; | |
12205 | t = TREE_TYPE (t); | |
12206 | } | |
04f3dc2b | 12207 | if (TREE_CODE (t) == ARRAY_TYPE) |
33bd39a2 | 12208 | error ("parameter `%D' includes %s to array of unknown bound `%T'", |
98979fe0 | 12209 | decl, ptr ? "pointer" : "reference", t); |
04f3dc2b MM |
12210 | } |
12211 | ||
04f3dc2b MM |
12212 | if (!any_error && init) |
12213 | init = check_default_argument (decl, init); | |
12214 | else | |
12215 | init = NULL_TREE; | |
12216 | } | |
8d08fdba | 12217 | |
5cce22b6 NS |
12218 | TREE_CHAIN (decl) = decls; |
12219 | decls = decl; | |
0657c69c | 12220 | result = tree_cons (init, type, result); |
8d08fdba | 12221 | } |
5cce22b6 NS |
12222 | decls = nreverse (decls); |
12223 | result = nreverse (result); | |
12224 | if (!ellipsis) | |
12225 | result = chainon (result, void_list_node); | |
8d08fdba MS |
12226 | last_function_parms = decls; |
12227 | ||
8d08fdba MS |
12228 | return result; |
12229 | } | |
42976354 | 12230 | |
8d08fdba | 12231 | \f |
271e6f02 NS |
12232 | /* D is a constructor or overloaded `operator='. |
12233 | ||
12234 | Let T be the class in which D is declared. Then, this function | |
12235 | returns: | |
12236 | ||
12237 | -1 if D's is an ill-formed constructor or copy assignment operator | |
12238 | whose first parameter is of type `T'. | |
12239 | 0 if D is not a copy constructor or copy assignment | |
12240 | operator. | |
12241 | 1 if D is a copy constructor or copy assignment operator whose | |
12242 | first parameter is a reference to const qualified T. | |
12243 | 2 if D is a copy constructor or copy assignment operator whose | |
12244 | first parameter is a reference to non-const qualified T. | |
12245 | ||
12246 | This function can be used as a predicate. Positive values indicate | |
838dfd8a | 12247 | a copy constructor and nonzero values indicate a copy assignment |
4f1c5b7d MM |
12248 | operator. */ |
12249 | ||
c11b6f21 | 12250 | int |
271e6f02 | 12251 | copy_fn_p (d) |
c11b6f21 MS |
12252 | tree d; |
12253 | { | |
271e6f02 NS |
12254 | tree args; |
12255 | tree arg_type; | |
12256 | int result = 1; | |
12257 | ||
12258 | my_friendly_assert (DECL_FUNCTION_MEMBER_P (d), 20011208); | |
4f1c5b7d | 12259 | |
271e6f02 NS |
12260 | if (DECL_TEMPLATE_INFO (d) && is_member_template (DECL_TI_TEMPLATE (d))) |
12261 | /* Instantiations of template member functions are never copy | |
12262 | functions. Note that member functions of templated classes are | |
12263 | represented as template functions internally, and we must | |
12264 | accept those as copy functions. */ | |
12265 | return 0; | |
12266 | ||
12267 | args = FUNCTION_FIRST_USER_PARMTYPE (d); | |
12268 | if (!args) | |
4f1c5b7d MM |
12269 | return 0; |
12270 | ||
271e6f02 NS |
12271 | arg_type = TREE_VALUE (args); |
12272 | ||
12273 | if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d)) | |
12274 | { | |
12275 | /* Pass by value copy assignment operator. */ | |
12276 | result = -1; | |
12277 | } | |
12278 | else if (TREE_CODE (arg_type) == REFERENCE_TYPE | |
12279 | && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d)) | |
12280 | { | |
12281 | if (CP_TYPE_CONST_P (TREE_TYPE (arg_type))) | |
12282 | result = 2; | |
12283 | } | |
12284 | else | |
12285 | return 0; | |
12286 | ||
12287 | args = TREE_CHAIN (args); | |
12288 | ||
12289 | if (args && args != void_list_node && !TREE_PURPOSE (args)) | |
12290 | /* There are more non-optional args. */ | |
12291 | return 0; | |
12292 | ||
12293 | return result; | |
12294 | } | |
12295 | ||
12296 | /* Remember any special properties of member function DECL. */ | |
12297 | ||
12298 | void grok_special_member_properties (decl) | |
12299 | tree decl; | |
12300 | { | |
12301 | if (!DECL_NONSTATIC_MEMBER_FUNCTION_P(decl)) | |
12302 | ; /* Not special. */ | |
12303 | else if (DECL_CONSTRUCTOR_P (decl)) | |
12304 | { | |
12305 | int ctor = copy_fn_p (decl); | |
12306 | ||
12307 | if (ctor > 0) | |
12308 | { | |
12309 | /* [class.copy] | |
12310 | ||
12311 | A non-template constructor for class X is a copy | |
12312 | constructor if its first parameter is of type X&, const | |
12313 | X&, volatile X& or const volatile X&, and either there | |
12314 | are no other parameters or else all other parameters have | |
12315 | default arguments. */ | |
12316 | TYPE_HAS_INIT_REF (DECL_CONTEXT (decl)) = 1; | |
12317 | if (ctor > 1) | |
12318 | TYPE_HAS_CONST_INIT_REF (DECL_CONTEXT (decl)) = 1; | |
12319 | } | |
12320 | else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl))) | |
12321 | TYPE_HAS_DEFAULT_CONSTRUCTOR (DECL_CONTEXT (decl)) = 1; | |
12322 | } | |
12323 | else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR) | |
12324 | { | |
12325 | /* [class.copy] | |
12326 | ||
12327 | A non-template assignment operator for class X is a copy | |
12328 | assignment operator if its parameter is of type X, X&, const | |
12329 | X&, volatile X& or const volatile X&. */ | |
12330 | ||
12331 | int assop = copy_fn_p (decl); | |
12332 | ||
12333 | if (assop) | |
12334 | { | |
12335 | TYPE_HAS_ASSIGN_REF (DECL_CONTEXT (decl)) = 1; | |
12336 | if (assop != 1) | |
12337 | TYPE_HAS_CONST_ASSIGN_REF (DECL_CONTEXT (decl)) = 1; | |
12338 | if (DECL_PURE_VIRTUAL_P (decl)) | |
12339 | TYPE_HAS_ABSTRACT_ASSIGN_REF (DECL_CONTEXT (decl)) = 1; | |
12340 | } | |
12341 | } | |
c11b6f21 MS |
12342 | } |
12343 | ||
271e6f02 NS |
12344 | /* Check a constructor DECL has the correct form. Complains |
12345 | if the class has a constructor of the form X(X). */ | |
e92cc029 | 12346 | |
a0a33927 | 12347 | int |
8d08fdba MS |
12348 | grok_ctor_properties (ctype, decl) |
12349 | tree ctype, decl; | |
12350 | { | |
271e6f02 NS |
12351 | int ctor_parm = copy_fn_p (decl); |
12352 | ||
12353 | if (ctor_parm < 0) | |
12354 | { | |
12355 | /* [class.copy] | |
12356 | ||
12357 | A declaration of a constructor for a class X is ill-formed if | |
12358 | its first parameter is of type (optionally cv-qualified) X | |
12359 | and either there are no other parameters or else all other | |
12360 | parameters have default arguments. | |
12361 | ||
12362 | We *don't* complain about member template instantiations that | |
12363 | have this form, though; they can occur as we try to decide | |
12364 | what constructor to use during overload resolution. Since | |
12365 | overload resolution will never prefer such a constructor to | |
12366 | the non-template copy constructor (which is either explicitly | |
12367 | or implicitly defined), there's no need to worry about their | |
12368 | existence. Theoretically, they should never even be | |
12369 | instantiated, but that's hard to forestall. */ | |
33bd39a2 | 12370 | error ("invalid constructor; you probably meant `%T (const %T&)'", |
0b41abe6 JM |
12371 | ctype, ctype); |
12372 | SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype); | |
12373 | return 0; | |
8d08fdba | 12374 | } |
271e6f02 | 12375 | |
a0a33927 | 12376 | return 1; |
8d08fdba MS |
12377 | } |
12378 | ||
596ea4e5 | 12379 | /* An operator with this code is unary, but can also be binary. */ |
e92cc029 | 12380 | |
a28e3c7f | 12381 | static int |
596ea4e5 AS |
12382 | ambi_op_p (code) |
12383 | enum tree_code code; | |
8d08fdba | 12384 | { |
596ea4e5 AS |
12385 | return (code == INDIRECT_REF |
12386 | || code == ADDR_EXPR | |
12387 | || code == CONVERT_EXPR | |
12388 | || code == NEGATE_EXPR | |
12389 | || code == PREINCREMENT_EXPR | |
12390 | || code == PREDECREMENT_EXPR); | |
8d08fdba MS |
12391 | } |
12392 | ||
12393 | /* An operator with this name can only be unary. */ | |
e92cc029 | 12394 | |
a28e3c7f | 12395 | static int |
596ea4e5 AS |
12396 | unary_op_p (code) |
12397 | enum tree_code code; | |
8d08fdba | 12398 | { |
596ea4e5 AS |
12399 | return (code == TRUTH_NOT_EXPR |
12400 | || code == BIT_NOT_EXPR | |
12401 | || code == COMPONENT_REF | |
12402 | || code == TYPE_EXPR); | |
8d08fdba MS |
12403 | } |
12404 | ||
12405 | /* Do a little sanity-checking on how they declared their operator. */ | |
e92cc029 | 12406 | |
5566b478 | 12407 | void |
271e6f02 | 12408 | grok_op_properties (decl, friendp) |
8d08fdba | 12409 | tree decl; |
271e6f02 | 12410 | int friendp; |
8d08fdba MS |
12411 | { |
12412 | tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
596ea4e5 | 12413 | tree argtype; |
8d08fdba MS |
12414 | int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE); |
12415 | tree name = DECL_NAME (decl); | |
596ea4e5 AS |
12416 | enum tree_code operator_code; |
12417 | int arity; | |
12418 | ||
12419 | /* Count the number of arguments. */ | |
12420 | for (argtype = argtypes, arity = 0; | |
12421 | argtype && argtype != void_list_node; | |
12422 | argtype = TREE_CHAIN (argtype)) | |
12423 | ++arity; | |
8d08fdba | 12424 | |
a28e3c7f MS |
12425 | if (current_class_type == NULL_TREE) |
12426 | friendp = 1; | |
8d08fdba | 12427 | |
596ea4e5 AS |
12428 | if (DECL_CONV_FN_P (decl)) |
12429 | operator_code = TYPE_EXPR; | |
12430 | else | |
12431 | do | |
12432 | { | |
0c918ce5 MM |
12433 | #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \ |
12434 | if (ansi_opname (CODE) == name) \ | |
12435 | { \ | |
75ac8dec | 12436 | operator_code = (CODE); \ |
0c918ce5 MM |
12437 | break; \ |
12438 | } \ | |
12439 | else if (ansi_assopname (CODE) == name) \ | |
12440 | { \ | |
75ac8dec | 12441 | operator_code = (CODE); \ |
0c918ce5 MM |
12442 | DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \ |
12443 | break; \ | |
596ea4e5 AS |
12444 | } |
12445 | ||
12446 | #include "operators.def" | |
12447 | #undef DEF_OPERATOR | |
12448 | ||
a98facb0 | 12449 | abort (); |
596ea4e5 AS |
12450 | } |
12451 | while (0); | |
12452 | my_friendly_assert (operator_code != LAST_CPLUS_TREE_CODE, 20000526); | |
12453 | SET_OVERLOADED_OPERATOR_CODE (decl, operator_code); | |
12454 | ||
a28e3c7f MS |
12455 | if (! friendp) |
12456 | { | |
596ea4e5 AS |
12457 | switch (operator_code) |
12458 | { | |
12459 | case CALL_EXPR: | |
12460 | TYPE_OVERLOADS_CALL_EXPR (current_class_type) = 1; | |
12461 | break; | |
5362b086 | 12462 | |
596ea4e5 AS |
12463 | case ARRAY_REF: |
12464 | TYPE_OVERLOADS_ARRAY_REF (current_class_type) = 1; | |
12465 | break; | |
f181d4ae | 12466 | |
596ea4e5 AS |
12467 | case COMPONENT_REF: |
12468 | case MEMBER_REF: | |
12469 | TYPE_OVERLOADS_ARROW (current_class_type) = 1; | |
12470 | break; | |
5362b086 | 12471 | |
596ea4e5 AS |
12472 | case NEW_EXPR: |
12473 | TYPE_HAS_NEW_OPERATOR (current_class_type) = 1; | |
12474 | break; | |
5362b086 | 12475 | |
596ea4e5 AS |
12476 | case DELETE_EXPR: |
12477 | TYPE_GETS_DELETE (current_class_type) |= 1; | |
12478 | break; | |
5362b086 | 12479 | |
596ea4e5 AS |
12480 | case VEC_NEW_EXPR: |
12481 | TYPE_HAS_ARRAY_NEW_OPERATOR (current_class_type) = 1; | |
12482 | break; | |
5362b086 | 12483 | |
596ea4e5 AS |
12484 | case VEC_DELETE_EXPR: |
12485 | TYPE_GETS_DELETE (current_class_type) |= 2; | |
12486 | break; | |
12487 | ||
12488 | default: | |
12489 | break; | |
12490 | } | |
12491 | } | |
12492 | ||
12493 | if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR) | |
8d08fdba | 12494 | { |
8d08fdba MS |
12495 | /* When the compiler encounters the definition of A::operator new, it |
12496 | doesn't look at the class declaration to find out if it's static. */ | |
a28e3c7f | 12497 | if (methodp) |
3afb32a4 | 12498 | revert_static_member_fn (decl); |
68642fb6 | 12499 | |
36791f1e | 12500 | TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl)); |
8d08fdba | 12501 | } |
596ea4e5 | 12502 | else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR) |
8d08fdba | 12503 | { |
a28e3c7f | 12504 | if (methodp) |
3afb32a4 | 12505 | revert_static_member_fn (decl); |
68642fb6 | 12506 | |
36791f1e | 12507 | TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl)); |
8d08fdba | 12508 | } |
8d08fdba MS |
12509 | else |
12510 | { | |
12511 | /* An operator function must either be a non-static member function | |
12512 | or have at least one parameter of a class, a reference to a class, | |
12513 | an enumeration, or a reference to an enumeration. 13.4.0.6 */ | |
700f8a87 | 12514 | if (! methodp || DECL_STATIC_FUNCTION_P (decl)) |
8d08fdba | 12515 | { |
596ea4e5 AS |
12516 | if (operator_code == TYPE_EXPR |
12517 | || operator_code == CALL_EXPR | |
12518 | || operator_code == COMPONENT_REF | |
12519 | || operator_code == ARRAY_REF | |
12520 | || operator_code == NOP_EXPR) | |
33bd39a2 | 12521 | error ("`%D' must be a nonstatic member function", decl); |
8d08fdba MS |
12522 | else |
12523 | { | |
12524 | tree p = argtypes; | |
12525 | ||
700f8a87 | 12526 | if (DECL_STATIC_FUNCTION_P (decl)) |
33bd39a2 | 12527 | error ("`%D' must be either a non-static member function or a non-member function", decl); |
700f8a87 | 12528 | |
8d08fdba | 12529 | if (p) |
a1774733 | 12530 | for (; TREE_CODE (TREE_VALUE (p)) != VOID_TYPE ; p = TREE_CHAIN (p)) |
8d08fdba MS |
12531 | { |
12532 | tree arg = TREE_VALUE (p); | |
12533 | if (TREE_CODE (arg) == REFERENCE_TYPE) | |
12534 | arg = TREE_TYPE (arg); | |
12535 | ||
12536 | /* This lets bad template code slip through. */ | |
12537 | if (IS_AGGR_TYPE (arg) | |
12538 | || TREE_CODE (arg) == ENUMERAL_TYPE | |
73b0fce8 | 12539 | || TREE_CODE (arg) == TEMPLATE_TYPE_PARM |
a1281f45 | 12540 | || TREE_CODE (arg) == BOUND_TEMPLATE_TEMPLATE_PARM) |
8d08fdba MS |
12541 | goto foundaggr; |
12542 | } | |
33bd39a2 | 12543 | error |
8251199e | 12544 | ("`%D' must have an argument of class or enumerated type", |
8d08fdba MS |
12545 | decl); |
12546 | foundaggr: | |
12547 | ; | |
12548 | } | |
12549 | } | |
68642fb6 | 12550 | |
596ea4e5 | 12551 | if (operator_code == CALL_EXPR) |
a1c65f9f | 12552 | return; /* No restrictions on args. */ |
8d08fdba | 12553 | |
9a3b49ac | 12554 | if (IDENTIFIER_TYPENAME_P (name) && ! DECL_TEMPLATE_INFO (decl)) |
a0a33927 MS |
12555 | { |
12556 | tree t = TREE_TYPE (name); | |
17708e90 | 12557 | if (! friendp) |
a0a33927 MS |
12558 | { |
12559 | int ref = (TREE_CODE (t) == REFERENCE_TYPE); | |
d8e178a0 | 12560 | const char *what = 0; |
5362b086 | 12561 | |
a0a33927 MS |
12562 | if (ref) |
12563 | t = TYPE_MAIN_VARIANT (TREE_TYPE (t)); | |
12564 | ||
17708e90 NS |
12565 | if (TREE_CODE (t) == VOID_TYPE) |
12566 | what = "void"; | |
12567 | else if (t == current_class_type) | |
a0a33927 | 12568 | what = "the same type"; |
9a3b49ac | 12569 | /* Don't force t to be complete here. */ |
a0a33927 | 12570 | else if (IS_AGGR_TYPE (t) |
d0f062fb | 12571 | && COMPLETE_TYPE_P (t) |
a0a33927 MS |
12572 | && DERIVED_FROM_P (t, current_class_type)) |
12573 | what = "a base class"; | |
12574 | ||
12575 | if (what) | |
8251199e | 12576 | warning ("conversion to %s%s will never use a type conversion operator", |
a0a33927 MS |
12577 | ref ? "a reference to " : "", what); |
12578 | } | |
12579 | } | |
271e6f02 | 12580 | if (operator_code == COND_EXPR) |
8d08fdba MS |
12581 | { |
12582 | /* 13.4.0.3 */ | |
33bd39a2 | 12583 | error ("ISO C++ prohibits overloading operator ?:"); |
68642fb6 | 12584 | } |
596ea4e5 | 12585 | else if (ambi_op_p (operator_code)) |
8d08fdba | 12586 | { |
596ea4e5 AS |
12587 | if (arity == 1) |
12588 | /* We pick the one-argument operator codes by default, so | |
12589 | we don't have to change anything. */ | |
12590 | ; | |
12591 | else if (arity == 2) | |
8d08fdba | 12592 | { |
596ea4e5 AS |
12593 | /* If we thought this was a unary operator, we now know |
12594 | it to be a binary operator. */ | |
12595 | switch (operator_code) | |
12596 | { | |
12597 | case INDIRECT_REF: | |
12598 | operator_code = MULT_EXPR; | |
12599 | break; | |
12600 | ||
12601 | case ADDR_EXPR: | |
12602 | operator_code = BIT_AND_EXPR; | |
12603 | break; | |
12604 | ||
12605 | case CONVERT_EXPR: | |
12606 | operator_code = PLUS_EXPR; | |
12607 | break; | |
12608 | ||
12609 | case NEGATE_EXPR: | |
12610 | operator_code = MINUS_EXPR; | |
12611 | break; | |
12612 | ||
12613 | case PREINCREMENT_EXPR: | |
12614 | operator_code = POSTINCREMENT_EXPR; | |
12615 | break; | |
12616 | ||
12617 | case PREDECREMENT_EXPR: | |
655dc6ee | 12618 | operator_code = POSTDECREMENT_EXPR; |
596ea4e5 AS |
12619 | break; |
12620 | ||
12621 | default: | |
a98facb0 | 12622 | abort (); |
596ea4e5 AS |
12623 | } |
12624 | ||
12625 | SET_OVERLOADED_OPERATOR_CODE (decl, operator_code); | |
12626 | ||
12627 | if ((operator_code == POSTINCREMENT_EXPR | |
12628 | || operator_code == POSTDECREMENT_EXPR) | |
5156628f | 12629 | && ! processing_template_decl |
007e5fea | 12630 | && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node)) |
8d08fdba MS |
12631 | { |
12632 | if (methodp) | |
33bd39a2 | 12633 | error ("postfix `%D' must take `int' as its argument", |
8d08fdba MS |
12634 | decl); |
12635 | else | |
33bd39a2 | 12636 | error |
8251199e JM |
12637 | ("postfix `%D' must take `int' as its second argument", |
12638 | decl); | |
8d08fdba MS |
12639 | } |
12640 | } | |
12641 | else | |
12642 | { | |
12643 | if (methodp) | |
33bd39a2 | 12644 | error ("`%D' must take either zero or one argument", decl); |
8d08fdba | 12645 | else |
33bd39a2 | 12646 | error ("`%D' must take either one or two arguments", decl); |
8d08fdba | 12647 | } |
824b9a4c MS |
12648 | |
12649 | /* More Effective C++ rule 6. */ | |
eb448459 | 12650 | if (warn_ecpp |
596ea4e5 AS |
12651 | && (operator_code == POSTINCREMENT_EXPR |
12652 | || operator_code == POSTDECREMENT_EXPR | |
12653 | || operator_code == PREINCREMENT_EXPR | |
12654 | || operator_code == PREDECREMENT_EXPR)) | |
824b9a4c MS |
12655 | { |
12656 | tree arg = TREE_VALUE (argtypes); | |
12657 | tree ret = TREE_TYPE (TREE_TYPE (decl)); | |
12658 | if (methodp || TREE_CODE (arg) == REFERENCE_TYPE) | |
12659 | arg = TREE_TYPE (arg); | |
12660 | arg = TYPE_MAIN_VARIANT (arg); | |
596ea4e5 AS |
12661 | if (operator_code == PREINCREMENT_EXPR |
12662 | || operator_code == PREDECREMENT_EXPR) | |
824b9a4c MS |
12663 | { |
12664 | if (TREE_CODE (ret) != REFERENCE_TYPE | |
3bfdc719 MM |
12665 | || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), |
12666 | arg)) | |
33bd39a2 | 12667 | warning ("prefix `%D' should return `%T'", decl, |
824b9a4c MS |
12668 | build_reference_type (arg)); |
12669 | } | |
12670 | else | |
12671 | { | |
3bfdc719 | 12672 | if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg)) |
33bd39a2 | 12673 | warning ("postfix `%D' should return `%T'", decl, arg); |
824b9a4c MS |
12674 | } |
12675 | } | |
8d08fdba | 12676 | } |
596ea4e5 | 12677 | else if (unary_op_p (operator_code)) |
8d08fdba | 12678 | { |
596ea4e5 | 12679 | if (arity != 1) |
8d08fdba MS |
12680 | { |
12681 | if (methodp) | |
33bd39a2 | 12682 | error ("`%D' must take `void'", decl); |
8d08fdba | 12683 | else |
33bd39a2 | 12684 | error ("`%D' must take exactly one argument", decl); |
8d08fdba MS |
12685 | } |
12686 | } | |
596ea4e5 | 12687 | else /* if (binary_op_p (operator_code)) */ |
8d08fdba | 12688 | { |
596ea4e5 | 12689 | if (arity != 2) |
8d08fdba MS |
12690 | { |
12691 | if (methodp) | |
33bd39a2 | 12692 | error ("`%D' must take exactly one argument", decl); |
8d08fdba | 12693 | else |
33bd39a2 | 12694 | error ("`%D' must take exactly two arguments", decl); |
8d08fdba | 12695 | } |
824b9a4c MS |
12696 | |
12697 | /* More Effective C++ rule 7. */ | |
eb448459 | 12698 | if (warn_ecpp |
596ea4e5 AS |
12699 | && (operator_code == TRUTH_ANDIF_EXPR |
12700 | || operator_code == TRUTH_ORIF_EXPR | |
12701 | || operator_code == COMPOUND_EXPR)) | |
33bd39a2 | 12702 | warning ("user-defined `%D' always evaluates both arguments", |
824b9a4c MS |
12703 | decl); |
12704 | } | |
12705 | ||
12706 | /* Effective C++ rule 23. */ | |
eb448459 | 12707 | if (warn_ecpp |
596ea4e5 | 12708 | && arity == 2 |
4bd7c270 | 12709 | && !DECL_ASSIGNMENT_OPERATOR_P (decl) |
596ea4e5 AS |
12710 | && (operator_code == PLUS_EXPR |
12711 | || operator_code == MINUS_EXPR | |
12712 | || operator_code == TRUNC_DIV_EXPR | |
4bd7c270 NS |
12713 | || operator_code == MULT_EXPR |
12714 | || operator_code == TRUNC_MOD_EXPR) | |
824b9a4c | 12715 | && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE) |
33bd39a2 | 12716 | warning ("`%D' should return by value", decl); |
8d08fdba | 12717 | |
271e6f02 | 12718 | /* [over.oper]/8 */ |
34332678 CT |
12719 | for (; argtypes && argtypes != void_list_node; |
12720 | argtypes = TREE_CHAIN (argtypes)) | |
12721 | if (TREE_PURPOSE (argtypes)) | |
12722 | { | |
12723 | TREE_PURPOSE (argtypes) = NULL_TREE; | |
596ea4e5 AS |
12724 | if (operator_code == POSTINCREMENT_EXPR |
12725 | || operator_code == POSTDECREMENT_EXPR) | |
34332678 CT |
12726 | { |
12727 | if (pedantic) | |
33bd39a2 | 12728 | pedwarn ("`%D' cannot have default arguments", decl); |
34332678 CT |
12729 | } |
12730 | else | |
33bd39a2 | 12731 | error ("`%D' cannot have default arguments", decl); |
34332678 CT |
12732 | } |
12733 | ||
8d08fdba MS |
12734 | } |
12735 | } | |
12736 | \f | |
d8e178a0 | 12737 | static const char * |
094fe153 JM |
12738 | tag_name (code) |
12739 | enum tag_types code; | |
12740 | { | |
12741 | switch (code) | |
12742 | { | |
12743 | case record_type: | |
12744 | return "struct"; | |
12745 | case class_type: | |
12746 | return "class"; | |
12747 | case union_type: | |
12748 | return "union "; | |
12749 | case enum_type: | |
12750 | return "enum"; | |
094fe153 | 12751 | default: |
a98facb0 | 12752 | abort (); |
094fe153 JM |
12753 | } |
12754 | } | |
12755 | ||
8d08fdba MS |
12756 | /* Get the struct, enum or union (CODE says which) with tag NAME. |
12757 | Define the tag as a forward-reference if it is not defined. | |
12758 | ||
12759 | C++: If a class derivation is given, process it here, and report | |
12760 | an error if multiple derivation declarations are not identical. | |
12761 | ||
12762 | If this is a definition, come in through xref_tag and only look in | |
12763 | the current frame for the name (since C++ allows new names in any | |
12764 | scope.) */ | |
12765 | ||
8d08fdba | 12766 | tree |
88e5899c MM |
12767 | xref_tag (enum tag_types tag_code, tree name, tree attributes, |
12768 | bool globalize) | |
8d08fdba | 12769 | { |
8d08fdba | 12770 | enum tree_code code; |
8d08fdba | 12771 | register tree ref, t; |
e2500fed | 12772 | struct cp_binding_level *b = current_binding_level; |
25aab5d0 | 12773 | tree context = NULL_TREE; |
dc8263bc | 12774 | |
8d08fdba MS |
12775 | switch (tag_code) |
12776 | { | |
12777 | case record_type: | |
12778 | case class_type: | |
8d08fdba | 12779 | code = RECORD_TYPE; |
8d08fdba MS |
12780 | break; |
12781 | case union_type: | |
12782 | code = UNION_TYPE; | |
8d08fdba MS |
12783 | break; |
12784 | case enum_type: | |
12785 | code = ENUMERAL_TYPE; | |
12786 | break; | |
12787 | default: | |
a98facb0 | 12788 | abort (); |
8d08fdba MS |
12789 | } |
12790 | ||
12791 | /* If a cross reference is requested, look up the type | |
12792 | already defined for this tag and return it. */ | |
2f939d94 | 12793 | if (TYPE_P (name)) |
be99da77 MS |
12794 | { |
12795 | t = name; | |
a80e4195 | 12796 | name = TYPE_IDENTIFIER (t); |
be99da77 MS |
12797 | } |
12798 | else | |
12799 | t = IDENTIFIER_TYPE_VALUE (name); | |
61a127b3 | 12800 | |
f2ae0c45 JM |
12801 | /* Warn about 'friend struct Inherited;' doing the wrong thing. */ |
12802 | if (t && globalize && TREE_CODE (t) == TYPENAME_TYPE) | |
12803 | { | |
12804 | static int explained; | |
2b0cbc5d NS |
12805 | tree shadowed; |
12806 | ||
33bd39a2 | 12807 | warning ("`%s %T' declares a new type at namespace scope", |
2b0cbc5d NS |
12808 | tag_name (tag_code), name); |
12809 | if (!explained++) | |
33bd39a2 | 12810 | 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 |
12811 | tag_name (tag_code), |
12812 | constructor_name (current_class_type), | |
12813 | TYPE_IDENTIFIER (t)); | |
12814 | ||
12815 | /* We need to remove the class scope binding for the | |
a1c65f9f | 12816 | TYPENAME_TYPE as otherwise poplevel_class gets confused. */ |
2b0cbc5d NS |
12817 | for (shadowed = b->class_shadowed; |
12818 | shadowed; | |
12819 | shadowed = TREE_CHAIN (shadowed)) | |
12820 | if (TREE_TYPE (shadowed) == TYPE_NAME (t)) | |
12821 | { | |
12822 | TREE_PURPOSE (shadowed) = NULL_TREE; | |
12823 | break; | |
12824 | } | |
f2ae0c45 JM |
12825 | } |
12826 | ||
73b0fce8 | 12827 | if (t && TREE_CODE (t) != code && TREE_CODE (t) != TEMPLATE_TYPE_PARM |
a1281f45 | 12828 | && TREE_CODE (t) != BOUND_TEMPLATE_TEMPLATE_PARM) |
a0a33927 MS |
12829 | t = NULL_TREE; |
12830 | ||
8ccc31eb | 12831 | if (! globalize) |
8d08fdba | 12832 | { |
f3400fe2 JM |
12833 | /* If we know we are defining this tag, only look it up in |
12834 | this scope and don't try to find it as a type. */ | |
12835 | ref = lookup_tag (code, name, b, 1); | |
8d08fdba MS |
12836 | } |
12837 | else | |
12838 | { | |
25aab5d0 | 12839 | if (t) |
36a117a5 | 12840 | { |
4782bd5b RS |
12841 | ref = follow_tag_typedef (t); |
12842 | ||
25aab5d0 MM |
12843 | /* [dcl.type.elab] If the identifier resolves to a |
12844 | typedef-name or a template type-parameter, the | |
12845 | elaborated-type-specifier is ill-formed. */ | |
4782bd5b RS |
12846 | if (!ref) |
12847 | { | |
12848 | pedwarn ("using typedef-name `%D' after `%s'", | |
12849 | TYPE_NAME (t), tag_name (tag_code)); | |
12850 | ref = t; | |
12851 | } | |
25aab5d0 | 12852 | else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM) |
33bd39a2 | 12853 | error ("using template type parameter `%T' after `%s'", |
4782bd5b | 12854 | t, tag_name (tag_code)); |
25aab5d0 MM |
12855 | } |
12856 | else | |
12857 | ref = lookup_tag (code, name, b, 0); | |
68642fb6 | 12858 | |
25aab5d0 MM |
12859 | if (! ref) |
12860 | { | |
12861 | /* Try finding it as a type declaration. If that wins, | |
68642fb6 | 12862 | use it. */ |
25aab5d0 MM |
12863 | ref = lookup_name (name, 1); |
12864 | ||
12865 | if (ref != NULL_TREE | |
12866 | && processing_template_decl | |
12867 | && DECL_CLASS_TEMPLATE_P (ref) | |
12868 | && template_class_depth (current_class_type) == 0) | |
12869 | /* Since GLOBALIZE is true, we're declaring a global | |
6757edfe | 12870 | template, so we want this type. */ |
17aec3eb | 12871 | ref = DECL_TEMPLATE_RESULT (ref); |
6757edfe | 12872 | |
25aab5d0 MM |
12873 | if (ref && TREE_CODE (ref) == TYPE_DECL |
12874 | && TREE_CODE (TREE_TYPE (ref)) == code) | |
12875 | ref = TREE_TYPE (ref); | |
12876 | else | |
12877 | ref = NULL_TREE; | |
12878 | } | |
12879 | ||
68642fb6 UD |
12880 | if (ref && current_class_type |
12881 | && template_class_depth (current_class_type) | |
12882 | && PROCESSING_REAL_TEMPLATE_DECL_P ()) | |
25aab5d0 | 12883 | { |
838dfd8a | 12884 | /* Since GLOBALIZE is nonzero, we are not looking at a |
25aab5d0 MM |
12885 | definition of this tag. Since, in addition, we are currently |
12886 | processing a (member) template declaration of a template | |
12887 | class, we must be very careful; consider: | |
12888 | ||
12889 | template <class X> | |
12890 | struct S1 | |
12891 | ||
12892 | template <class U> | |
12893 | struct S2 | |
12894 | { template <class V> | |
12895 | friend struct S1; }; | |
12896 | ||
12897 | Here, the S2::S1 declaration should not be confused with the | |
12898 | outer declaration. In particular, the inner version should | |
12899 | have a template parameter of level 2, not level 1. This | |
12900 | would be particularly important if the member declaration | |
12901 | were instead: | |
12902 | ||
12903 | template <class V = U> friend struct S1; | |
12904 | ||
12905 | say, when we should tsubst into `U' when instantiating | |
12906 | S2. On the other hand, when presented with: | |
12907 | ||
12908 | template <class T> | |
12909 | struct S1 { | |
12910 | template <class U> | |
12911 | struct S2 {}; | |
12912 | template <class U> | |
12913 | friend struct S2; | |
12914 | }; | |
12915 | ||
12916 | we must find the inner binding eventually. We | |
12917 | accomplish this by making sure that the new type we | |
12918 | create to represent this declaration has the right | |
12919 | TYPE_CONTEXT. */ | |
12920 | context = TYPE_CONTEXT (ref); | |
12921 | ref = NULL_TREE; | |
8d08fdba MS |
12922 | } |
12923 | } | |
12924 | ||
8d08fdba MS |
12925 | if (! ref) |
12926 | { | |
12927 | /* If no such tag is yet defined, create a forward-reference node | |
12928 | and record it as the "definition". | |
12929 | When a real declaration of this type is found, | |
12930 | the forward-reference will be altered into a real type. */ | |
8d08fdba MS |
12931 | if (code == ENUMERAL_TYPE) |
12932 | { | |
33bd39a2 | 12933 | error ("use of enum `%#D' without previous declaration", name); |
fc378698 | 12934 | |
8d08fdba MS |
12935 | ref = make_node (ENUMERAL_TYPE); |
12936 | ||
12937 | /* Give the type a default layout like unsigned int | |
12938 | to avoid crashing if it does not get defined. */ | |
12939 | TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node); | |
12940 | TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node); | |
11cf4d18 | 12941 | TYPE_USER_ALIGN (ref) = 0; |
8d08fdba MS |
12942 | TREE_UNSIGNED (ref) = 1; |
12943 | TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node); | |
12944 | TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node); | |
12945 | TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node); | |
12946 | ||
12947 | /* Enable us to recognize when a type is created in class context. | |
12948 | To do nested classes correctly, this should probably be cleared | |
12949 | out when we leave this classes scope. Currently this in only | |
12950 | done in `start_enum'. */ | |
12951 | ||
12952 | pushtag (name, ref, globalize); | |
8d08fdba | 12953 | } |
8d08fdba MS |
12954 | else |
12955 | { | |
e2500fed | 12956 | struct cp_binding_level *old_b = class_binding_level; |
8d08fdba | 12957 | |
33848bb0 | 12958 | ref = make_aggr_type (code); |
25aab5d0 | 12959 | TYPE_CONTEXT (ref) = context; |
8d08fdba | 12960 | |
8d08fdba MS |
12961 | #ifdef NONNESTED_CLASSES |
12962 | /* Class types don't nest the way enums do. */ | |
e2500fed | 12963 | class_binding_level = (struct cp_binding_level *)0; |
8d08fdba MS |
12964 | #endif |
12965 | pushtag (name, ref, globalize); | |
12966 | class_binding_level = old_b; | |
8d08fdba MS |
12967 | } |
12968 | } | |
12969 | else | |
12970 | { | |
7fe6899f | 12971 | if (!globalize && processing_template_decl && IS_AGGR_TYPE (ref)) |
6757edfe | 12972 | redeclare_class_template (ref, current_template_parms); |
8d08fdba MS |
12973 | } |
12974 | ||
a588fe25 | 12975 | TYPE_ATTRIBUTES (ref) = attributes; |
dc8263bc | 12976 | |
8d08fdba MS |
12977 | return ref; |
12978 | } | |
8ccc31eb | 12979 | |
fc378698 MS |
12980 | tree |
12981 | xref_tag_from_type (old, id, globalize) | |
12982 | tree old, id; | |
12983 | int globalize; | |
12984 | { | |
88e5899c | 12985 | enum tag_types tag_kind; |
fc378698 MS |
12986 | |
12987 | if (TREE_CODE (old) == RECORD_TYPE) | |
88e5899c | 12988 | tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type); |
fc378698 | 12989 | else |
88e5899c | 12990 | tag_kind = union_type; |
fc378698 MS |
12991 | |
12992 | if (id == NULL_TREE) | |
12993 | id = TYPE_IDENTIFIER (old); | |
12994 | ||
88e5899c | 12995 | return xref_tag (tag_kind, id, /*attributes=*/NULL_TREE, globalize); |
fc378698 MS |
12996 | } |
12997 | ||
3fd71a52 MM |
12998 | /* REF is a type (named NAME), for which we have just seen some |
12999 | baseclasses. BINFO is a list of those baseclasses; the | |
13000 | TREE_PURPOSE is an access_* node, and the TREE_VALUE is the type of | |
13001 | the base-class. CODE_TYPE_NODE indicates whether REF is a class, | |
13002 | struct, or union. */ | |
13003 | ||
8ccc31eb | 13004 | void |
da15dae6 | 13005 | xref_basetypes (ref, binfo) |
9780c24f | 13006 | tree ref; |
8ccc31eb MS |
13007 | tree binfo; |
13008 | { | |
13009 | /* In the declaration `A : X, Y, ... Z' we mark all the types | |
13010 | (A, X, Y, ..., Z) so we can check for duplicates. */ | |
13011 | tree binfos; | |
d6479fe7 MM |
13012 | tree base; |
13013 | ||
8ccc31eb | 13014 | int i, len; |
591382c4 | 13015 | enum tag_types tag_code; |
a5d7c4a3 | 13016 | |
da15dae6 | 13017 | if (TREE_CODE (ref) == UNION_TYPE) |
8ccc31eb | 13018 | { |
33bd39a2 | 13019 | error ("derived union `%T' invalid", ref); |
8ccc31eb MS |
13020 | return; |
13021 | } | |
13022 | ||
da15dae6 MM |
13023 | tag_code = (CLASSTYPE_DECLARED_CLASS (ref) ? class_type : record_type); |
13024 | ||
8ccc31eb | 13025 | len = list_length (binfo); |
8ccc31eb | 13026 | |
d6479fe7 MM |
13027 | /* First, make sure that any templates in base-classes are |
13028 | instantiated. This ensures that if we call ourselves recursively | |
13029 | we do not get confused about which classes are marked and which | |
13030 | are not. */ | |
13031 | for (base = binfo; base; base = TREE_CHAIN (base)) | |
13032 | complete_type (TREE_VALUE (base)); | |
13033 | ||
8ccc31eb MS |
13034 | SET_CLASSTYPE_MARKED (ref); |
13035 | BINFO_BASETYPES (TYPE_BINFO (ref)) = binfos = make_tree_vec (len); | |
13036 | ||
13037 | for (i = 0; binfo; binfo = TREE_CHAIN (binfo)) | |
13038 | { | |
13039 | /* The base of a derived struct is public by default. */ | |
13040 | int via_public | |
be99da77 MS |
13041 | = (TREE_PURPOSE (binfo) == access_public_node |
13042 | || TREE_PURPOSE (binfo) == access_public_virtual_node | |
8ccc31eb | 13043 | || (tag_code != class_type |
be99da77 MS |
13044 | && (TREE_PURPOSE (binfo) == access_default_node |
13045 | || TREE_PURPOSE (binfo) == access_default_virtual_node))); | |
d8b55a76 JM |
13046 | int via_protected |
13047 | = (TREE_PURPOSE (binfo) == access_protected_node | |
13048 | || TREE_PURPOSE (binfo) == access_protected_virtual_node); | |
8ccc31eb | 13049 | int via_virtual |
be99da77 | 13050 | = (TREE_PURPOSE (binfo) == access_private_virtual_node |
d8b55a76 | 13051 | || TREE_PURPOSE (binfo) == access_protected_virtual_node |
be99da77 MS |
13052 | || TREE_PURPOSE (binfo) == access_public_virtual_node |
13053 | || TREE_PURPOSE (binfo) == access_default_virtual_node); | |
13054 | tree basetype = TREE_VALUE (binfo); | |
8ccc31eb MS |
13055 | tree base_binfo; |
13056 | ||
8ccc31eb MS |
13057 | if (basetype && TREE_CODE (basetype) == TYPE_DECL) |
13058 | basetype = TREE_TYPE (basetype); | |
5566b478 MS |
13059 | if (!basetype |
13060 | || (TREE_CODE (basetype) != RECORD_TYPE | |
a80e4195 | 13061 | && TREE_CODE (basetype) != TYPENAME_TYPE |
73b0fce8 | 13062 | && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM |
a1281f45 | 13063 | && TREE_CODE (basetype) != BOUND_TEMPLATE_TEMPLATE_PARM)) |
8ccc31eb | 13064 | { |
33bd39a2 | 13065 | error ("base type `%T' fails to be a struct or class type", |
8ccc31eb MS |
13066 | TREE_VALUE (binfo)); |
13067 | continue; | |
13068 | } | |
2b9dc906 | 13069 | |
8adf5b5e JM |
13070 | /* This code replaces similar code in layout_basetypes. |
13071 | We put the complete_type first for implicit `typename'. */ | |
d0f062fb | 13072 | if (!COMPLETE_TYPE_P (basetype) |
2b9dc906 | 13073 | && ! (current_template_parms && uses_template_parms (basetype))) |
8ccc31eb | 13074 | { |
33bd39a2 | 13075 | error ("base class `%T' has incomplete type", basetype); |
8ccc31eb MS |
13076 | continue; |
13077 | } | |
8ccc31eb MS |
13078 | else |
13079 | { | |
13080 | if (CLASSTYPE_MARKED (basetype)) | |
13081 | { | |
13082 | if (basetype == ref) | |
33bd39a2 | 13083 | error ("recursive type `%T' undefined", basetype); |
8ccc31eb | 13084 | else |
33bd39a2 | 13085 | error ("duplicate base type `%T' invalid", basetype); |
8ccc31eb MS |
13086 | continue; |
13087 | } | |
13088 | ||
eff71ab0 | 13089 | if (TYPE_FOR_JAVA (basetype) |
46ccf50a | 13090 | && (current_lang_depth () == 0)) |
eff71ab0 PB |
13091 | TYPE_FOR_JAVA (ref) = 1; |
13092 | ||
8ccc31eb MS |
13093 | /* Note that the BINFO records which describe individual |
13094 | inheritances are *not* shared in the lattice! They | |
13095 | cannot be shared because a given baseclass may be | |
13096 | inherited with different `accessibility' by different | |
13097 | derived classes. (Each BINFO record describing an | |
13098 | individual inheritance contains flags which say what | |
13099 | the `accessibility' of that particular inheritance is.) */ | |
68642fb6 UD |
13100 | |
13101 | base_binfo | |
fed3cef0 | 13102 | = make_binfo (size_zero_node, basetype, |
7ddedda4 MM |
13103 | CLASS_TYPE_P (basetype) |
13104 | ? TYPE_BINFO_VTABLE (basetype) : NULL_TREE, | |
13105 | CLASS_TYPE_P (basetype) | |
13106 | ? TYPE_BINFO_VIRTUALS (basetype) : NULL_TREE); | |
68642fb6 | 13107 | |
8ccc31eb MS |
13108 | TREE_VEC_ELT (binfos, i) = base_binfo; |
13109 | TREE_VIA_PUBLIC (base_binfo) = via_public; | |
13110 | TREE_VIA_PROTECTED (base_binfo) = via_protected; | |
13111 | TREE_VIA_VIRTUAL (base_binfo) = via_virtual; | |
13112 | BINFO_INHERITANCE_CHAIN (base_binfo) = TYPE_BINFO (ref); | |
13113 | ||
dfbcd65a JM |
13114 | /* We need to unshare the binfos now so that lookups during class |
13115 | definition work. */ | |
13116 | unshare_base_binfos (base_binfo); | |
13117 | ||
8ccc31eb | 13118 | SET_CLASSTYPE_MARKED (basetype); |
9e9ff709 | 13119 | |
8ccc31eb MS |
13120 | /* We are free to modify these bits because they are meaningless |
13121 | at top level, and BASETYPE is a top-level type. */ | |
13122 | if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype)) | |
13123 | { | |
13124 | TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1; | |
4c6b7393 MM |
13125 | /* Converting to a virtual base class requires looking |
13126 | up the offset of the virtual base. */ | |
13127 | TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1; | |
8ccc31eb MS |
13128 | } |
13129 | ||
7ddedda4 MM |
13130 | if (CLASS_TYPE_P (basetype)) |
13131 | { | |
5362b086 | 13132 | TYPE_HAS_NEW_OPERATOR (ref) |
834c6dff | 13133 | |= TYPE_HAS_NEW_OPERATOR (basetype); |
5362b086 | 13134 | TYPE_HAS_ARRAY_NEW_OPERATOR (ref) |
834c6dff | 13135 | |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype); |
7ddedda4 | 13136 | TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype); |
4c6b7393 | 13137 | /* If the base-class uses multiple inheritance, so do we. */ |
68642fb6 | 13138 | TYPE_USES_MULTIPLE_INHERITANCE (ref) |
4c6b7393 MM |
13139 | |= TYPE_USES_MULTIPLE_INHERITANCE (basetype); |
13140 | /* Likewise, if converting to a base of the base may require | |
13141 | code, then we may need to generate code to convert to a | |
13142 | base as well. */ | |
68642fb6 | 13143 | TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) |
4c6b7393 | 13144 | |= TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype); |
7ddedda4 MM |
13145 | } |
13146 | ||
8ccc31eb MS |
13147 | i += 1; |
13148 | } | |
13149 | } | |
13150 | if (i) | |
13151 | TREE_VEC_LENGTH (binfos) = i; | |
13152 | else | |
13153 | BINFO_BASETYPES (TYPE_BINFO (ref)) = NULL_TREE; | |
13154 | ||
13155 | if (i > 1) | |
7ddedda4 | 13156 | { |
4c6b7393 MM |
13157 | TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1; |
13158 | /* If there is more than one non-empty they cannot be at the same | |
13159 | address. */ | |
13160 | TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1; | |
7ddedda4 MM |
13161 | } |
13162 | ||
8ccc31eb MS |
13163 | /* Unmark all the types. */ |
13164 | while (--i >= 0) | |
13165 | CLEAR_CLASSTYPE_MARKED (BINFO_TYPE (TREE_VEC_ELT (binfos, i))); | |
13166 | CLEAR_CLASSTYPE_MARKED (ref); | |
13167 | ||
70c532b5 MM |
13168 | /* Now that we know all the base-classes, set up the list of virtual |
13169 | bases. */ | |
23381155 | 13170 | get_vbase_types (ref); |
8ccc31eb | 13171 | } |
68642fb6 | 13172 | |
8d08fdba | 13173 | \f |
8d08fdba MS |
13174 | /* Begin compiling the definition of an enumeration type. |
13175 | NAME is its name (or null if anonymous). | |
13176 | Returns the type object, as yet incomplete. | |
13177 | Also records info about it so that build_enumerator | |
13178 | may be used to declare the individual values as they are read. */ | |
13179 | ||
13180 | tree | |
13181 | start_enum (name) | |
13182 | tree name; | |
13183 | { | |
13184 | register tree enumtype = NULL_TREE; | |
e2500fed | 13185 | struct cp_binding_level *b = current_binding_level; |
8d08fdba MS |
13186 | |
13187 | /* If this is the real definition for a previous forward reference, | |
13188 | fill in the contents in the same object that used to be the | |
13189 | forward reference. */ | |
13190 | ||
13191 | if (name != NULL_TREE) | |
13192 | enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1); | |
13193 | ||
13194 | if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE) | |
30ff8252 | 13195 | { |
33bd39a2 | 13196 | error ("multiple definition of `%#T'", enumtype); |
30ff8252 | 13197 | cp_error_at ("previous definition here", enumtype); |
58595203 MM |
13198 | /* Clear out TYPE_VALUES, and start again. */ |
13199 | TYPE_VALUES (enumtype) = NULL_TREE; | |
30ff8252 | 13200 | } |
8d08fdba MS |
13201 | else |
13202 | { | |
13203 | enumtype = make_node (ENUMERAL_TYPE); | |
13204 | pushtag (name, enumtype, 0); | |
13205 | } | |
13206 | ||
8d08fdba MS |
13207 | return enumtype; |
13208 | } | |
13209 | ||
13210 | /* After processing and defining all the values of an enumeration type, | |
13211 | install their decls in the enumeration type and finish it off. | |
968b956a | 13212 | ENUMTYPE is the type object and VALUES a list of name-value pairs. */ |
8d08fdba | 13213 | |
968b956a | 13214 | void |
219670f1 | 13215 | finish_enum (enumtype) |
dbfe2124 | 13216 | tree enumtype; |
8d08fdba | 13217 | { |
968b956a MM |
13218 | tree pair; |
13219 | tree minnode; | |
13220 | tree maxnode; | |
13221 | tree t; | |
13222 | bool unsignedp; | |
13223 | int lowprec; | |
13224 | int highprec; | |
13225 | int precision; | |
13226 | ||
13227 | /* We built up the VALUES in reverse order. */ | |
13228 | TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype)); | |
13229 | ||
13230 | /* [dcl.enum] | |
13231 | ||
13232 | Following the closing brace of an enum-specifier, each | |
13233 | enumerator has the type of its enumeration. Prior to the | |
13234 | closing brace, the type of each enumerator is the type of | |
13235 | its initializing value. */ | |
13236 | for (pair = TYPE_VALUES (enumtype); pair; pair = TREE_CHAIN (pair)) | |
13237 | TREE_TYPE (TREE_VALUE (pair)) = enumtype; | |
13238 | ||
27631dae | 13239 | /* For an enum defined in a template, all further processing is |
968b956a MM |
13240 | postponed until the template is instantiated. */ |
13241 | if (processing_template_decl) | |
13242 | { | |
5f261ba9 | 13243 | if (at_function_scope_p ()) |
968b956a | 13244 | add_stmt (build_min (TAG_DEFN, enumtype)); |
968b956a MM |
13245 | return; |
13246 | } | |
13247 | ||
13248 | /* Figure out what the minimum and maximum values of the enumerators | |
13249 | are. */ | |
13250 | if (TYPE_VALUES (enumtype)) | |
8d08fdba | 13251 | { |
968b956a | 13252 | minnode = maxnode = NULL_TREE; |
5566b478 | 13253 | |
968b956a MM |
13254 | for (pair = TYPE_VALUES (enumtype); |
13255 | pair; | |
13256 | pair = TREE_CHAIN (pair)) | |
8d08fdba | 13257 | { |
ed44da02 MM |
13258 | tree value; |
13259 | ||
968b956a | 13260 | value = DECL_INITIAL (TREE_VALUE (pair)); |
ed44da02 | 13261 | |
968b956a MM |
13262 | if (!minnode) |
13263 | minnode = maxnode = value; | |
13264 | else if (tree_int_cst_lt (maxnode, value)) | |
13265 | maxnode = value; | |
13266 | else if (tree_int_cst_lt (value, minnode)) | |
13267 | minnode = value; | |
8d08fdba MS |
13268 | } |
13269 | } | |
f376e137 | 13270 | else |
968b956a MM |
13271 | minnode = maxnode = integer_zero_node; |
13272 | ||
13273 | /* Compute the number of bits require to represent all values of the | |
13274 | enumeration. We must do this before the type of MINNODE and | |
13275 | MAXNODE are transformed, since min_precision relies on the | |
13276 | TREE_TYPE of the value it is passed. */ | |
13277 | unsignedp = tree_int_cst_sgn (minnode) >= 0; | |
13278 | lowprec = min_precision (minnode, unsignedp); | |
13279 | highprec = min_precision (maxnode, unsignedp); | |
13280 | precision = MAX (lowprec, highprec); | |
13281 | ||
13282 | /* Set the TREE_TYPE for the values as well. That's so that when we | |
13283 | call decl_constant_value we get an entity of the right type (but | |
13284 | with the constant value). In addition, transform the TYPE_VALUES | |
13285 | list to contain the values, rather than the CONST_DECLs for them. */ | |
13286 | for (pair = TYPE_VALUES (enumtype); pair; pair = TREE_CHAIN (pair)) | |
13287 | { | |
13288 | tree value = DECL_INITIAL (TREE_VALUE (pair)); | |
13289 | ||
13290 | TREE_TYPE (value) = enumtype; | |
13291 | TREE_VALUE (pair) = value; | |
13292 | } | |
13293 | ||
13294 | /* Set TYPE_MIN_VALUE and TYPE_MAX_VALUE according to `precision'. */ | |
13295 | TYPE_SIZE (enumtype) = NULL_TREE; | |
13296 | TYPE_PRECISION (enumtype) = precision; | |
13297 | if (unsignedp) | |
13298 | fixup_unsigned_type (enumtype); | |
cbf882af | 13299 | else |
968b956a | 13300 | fixup_signed_type (enumtype); |
8d08fdba | 13301 | |
219670f1 BE |
13302 | if (flag_short_enums || (precision > TYPE_PRECISION (integer_type_node))) |
13303 | /* Use the width of the narrowest normal C type which is wide | |
13304 | enough. */ | |
b0c48229 | 13305 | TYPE_PRECISION (enumtype) = TYPE_PRECISION (c_common_type_for_size |
219670f1 | 13306 | (precision, 1)); |
968b956a MM |
13307 | else |
13308 | TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node); | |
68642fb6 | 13309 | |
968b956a MM |
13310 | TYPE_SIZE (enumtype) = NULL_TREE; |
13311 | layout_type (enumtype); | |
8d08fdba | 13312 | |
968b956a MM |
13313 | /* Fix up all variant types of this enum type. */ |
13314 | for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t)) | |
13315 | { | |
13316 | TYPE_VALUES (t) = TYPE_VALUES (enumtype); | |
13317 | TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (enumtype); | |
13318 | TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (enumtype); | |
13319 | TYPE_SIZE (t) = TYPE_SIZE (enumtype); | |
13320 | TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (enumtype); | |
13321 | TYPE_MODE (t) = TYPE_MODE (enumtype); | |
13322 | TYPE_PRECISION (t) = TYPE_PRECISION (enumtype); | |
13323 | TYPE_ALIGN (t) = TYPE_ALIGN (enumtype); | |
13324 | TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (enumtype); | |
13325 | TREE_UNSIGNED (t) = TREE_UNSIGNED (enumtype); | |
cbf882af MM |
13326 | } |
13327 | ||
968b956a MM |
13328 | /* Finish debugging output for this type. */ |
13329 | rest_of_type_compilation (enumtype, namespace_bindings_p ()); | |
8d08fdba MS |
13330 | } |
13331 | ||
079e1098 | 13332 | /* Build and install a CONST_DECL for an enumeration constant of the |
58595203 | 13333 | enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided. |
8d08fdba MS |
13334 | Assignment of sequential values by default is handled here. */ |
13335 | ||
58595203 MM |
13336 | void |
13337 | build_enumerator (name, value, enumtype) | |
079e1098 MM |
13338 | tree name; |
13339 | tree value; | |
58595203 | 13340 | tree enumtype; |
8d08fdba | 13341 | { |
58595203 | 13342 | tree decl; |
e8bd800e | 13343 | tree context; |
58595203 MM |
13344 | tree type; |
13345 | tree values; | |
8d08fdba MS |
13346 | |
13347 | /* Remove no-op casts from the value. */ | |
13348 | if (value) | |
13349 | STRIP_TYPE_NOPS (value); | |
13350 | ||
58595203 MM |
13351 | if (! processing_template_decl) |
13352 | { | |
13353 | /* Validate and default VALUE. */ | |
13354 | if (value != NULL_TREE) | |
13355 | { | |
fc611ce0 | 13356 | value = decl_constant_value (value); |
58595203 MM |
13357 | |
13358 | if (TREE_CODE (value) == INTEGER_CST) | |
13359 | { | |
13360 | value = default_conversion (value); | |
13361 | constant_expression_warning (value); | |
13362 | } | |
13363 | else | |
13364 | { | |
33bd39a2 | 13365 | error ("enumerator value for `%D' not integer constant", name); |
58595203 MM |
13366 | value = NULL_TREE; |
13367 | } | |
13368 | } | |
13369 | ||
13370 | /* Default based on previous value. */ | |
13371 | if (value == NULL_TREE && ! processing_template_decl) | |
13372 | { | |
13373 | tree prev_value; | |
13374 | ||
13375 | if (TYPE_VALUES (enumtype)) | |
13376 | { | |
a1c65f9f | 13377 | /* The next value is the previous value ... */ |
58595203 MM |
13378 | prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype))); |
13379 | /* ... plus one. */ | |
ab76ca54 MM |
13380 | value = cp_build_binary_op (PLUS_EXPR, |
13381 | prev_value, | |
13382 | integer_one_node); | |
68642fb6 | 13383 | |
58595203 | 13384 | if (tree_int_cst_lt (value, prev_value)) |
33bd39a2 | 13385 | error ("overflow in enumeration values at `%D'", name); |
58595203 MM |
13386 | } |
13387 | else | |
13388 | value = integer_zero_node; | |
13389 | } | |
13390 | ||
13391 | /* Remove no-op casts from the value. */ | |
13392 | if (value) | |
13393 | STRIP_TYPE_NOPS (value); | |
013bc8af | 13394 | #if 0 |
58595203 MM |
13395 | /* To fix MAX_VAL enum consts. (bkoz) */ |
13396 | TREE_TYPE (value) = integer_type_node; | |
013bc8af | 13397 | #endif |
58595203 | 13398 | } |
8d08fdba | 13399 | |
58595203 MM |
13400 | /* We always have to copy here; not all INTEGER_CSTs are unshared. |
13401 | Even in other cases, we will later (in finish_enum) be setting | |
13402 | the type of VALUE. But, we don't need to make a copy if this | |
13403 | VALUE is one of the enumeration constants for this same | |
13404 | enumeration type. */ | |
13405 | for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values)) | |
13406 | if (TREE_VALUE (values) == value) | |
13407 | break; | |
13408 | /* If we didn't break out of the loop, then we do need a copy. */ | |
13409 | if (!values && value) | |
13410 | value = copy_node (value); | |
ed44da02 | 13411 | |
8d08fdba | 13412 | /* C++ associates enums with global, function, or class declarations. */ |
58595203 MM |
13413 | context = current_scope (); |
13414 | ||
13415 | /* Build the actual enumeration constant. Note that the enumeration | |
13416 | constants have the type of their initializers until the | |
13417 | enumeration is complete: | |
13418 | ||
13419 | [ dcl.enum ] | |
13420 | ||
13421 | Following the closing brace of an enum-specifier, each enumer- | |
13422 | ator has the type of its enumeration. Prior to the closing | |
13423 | brace, the type of each enumerator is the type of its | |
13424 | initializing value. | |
13425 | ||
13426 | In finish_enum we will reset the type. Of course, if we're | |
a1c65f9f | 13427 | processing a template, there may be no value. */ |
58595203 MM |
13428 | type = value ? TREE_TYPE (value) : NULL_TREE; |
13429 | ||
13430 | if (context && context == current_class_type) | |
13431 | /* This enum declaration is local to the class. We need the full | |
8f17b5c5 | 13432 | lang_decl so that we can record DECL_CLASS_CONTEXT, for example. */ |
58595203 MM |
13433 | decl = build_lang_decl (CONST_DECL, name, type); |
13434 | else | |
13435 | /* It's a global enum, or it's local to a function. (Note local to | |
e8bd800e | 13436 | a function could mean local to a class method. */ |
58595203 | 13437 | decl = build_decl (CONST_DECL, name, type); |
e8bd800e | 13438 | |
58595203 MM |
13439 | DECL_CONTEXT (decl) = FROB_CONTEXT (context); |
13440 | DECL_INITIAL (decl) = value; | |
13441 | TREE_READONLY (decl) = 1; | |
e8bd800e | 13442 | |
58595203 MM |
13443 | if (context && context == current_class_type) |
13444 | /* In something like `struct S { enum E { i = 7 }; };' we put `i' | |
8f032717 MM |
13445 | on the TYPE_FIELDS list for `S'. (That's so that you can say |
13446 | things like `S::i' later.) */ | |
58595203 MM |
13447 | finish_member_declaration (decl); |
13448 | else | |
9780c24f | 13449 | pushdecl (decl); |
58595203 MM |
13450 | |
13451 | /* Add this enumeration constant to the list for this type. */ | |
13452 | TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype)); | |
8d08fdba MS |
13453 | } |
13454 | ||
8d08fdba | 13455 | \f |
a8f73d4b MM |
13456 | /* We're defining DECL. Make sure that it's type is OK. */ |
13457 | ||
13458 | static void | |
f444e36b | 13459 | check_function_type (decl, current_function_parms) |
a8f73d4b | 13460 | tree decl; |
f444e36b | 13461 | tree current_function_parms; |
a8f73d4b MM |
13462 | { |
13463 | tree fntype = TREE_TYPE (decl); | |
d0f062fb | 13464 | tree return_type = complete_type (TREE_TYPE (fntype)); |
a8f73d4b MM |
13465 | |
13466 | /* In a function definition, arg types must be complete. */ | |
13467 | require_complete_types_for_parms (current_function_parms); | |
13468 | ||
d0f062fb | 13469 | if (!COMPLETE_OR_VOID_TYPE_P (return_type)) |
a8f73d4b | 13470 | { |
33bd39a2 | 13471 | error ("return type `%#T' is incomplete", TREE_TYPE (fntype)); |
a8f73d4b MM |
13472 | |
13473 | /* Make it return void instead, but don't change the | |
13474 | type of the DECL_RESULT, in case we have a named return value. */ | |
13475 | if (TREE_CODE (fntype) == METHOD_TYPE) | |
13476 | { | |
13477 | tree ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype))); | |
13478 | TREE_TYPE (decl) | |
13479 | = build_cplus_method_type (ctype, | |
13480 | void_type_node, | |
13481 | FUNCTION_ARG_CHAIN (decl)); | |
13482 | } | |
13483 | else | |
13484 | TREE_TYPE (decl) | |
13485 | = build_function_type (void_type_node, | |
13486 | TYPE_ARG_TYPES (TREE_TYPE (decl))); | |
68642fb6 | 13487 | TREE_TYPE (decl) |
a8f73d4b MM |
13488 | = build_exception_variant (fntype, |
13489 | TYPE_RAISES_EXCEPTIONS (fntype)); | |
13490 | } | |
13491 | else | |
13492 | abstract_virtuals_error (decl, TREE_TYPE (fntype)); | |
13493 | } | |
13494 | ||
8d08fdba MS |
13495 | /* Create the FUNCTION_DECL for a function definition. |
13496 | DECLSPECS and DECLARATOR are the parts of the declaration; | |
13497 | they describe the function's name and the type it returns, | |
13498 | but twisted together in a fashion that parallels the syntax of C. | |
13499 | ||
a8f73d4b MM |
13500 | FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the |
13501 | DECLARATOR is really the DECL for the function we are about to | |
13502 | process and that DECLSPECS should be ignored), SF_INCLASS_INLINE | |
f444e36b | 13503 | indicating that the function is an inline defined in-class. |
68642fb6 | 13504 | |
8d08fdba MS |
13505 | This function creates a binding context for the function body |
13506 | as well as setting up the FUNCTION_DECL in current_function_decl. | |
13507 | ||
13508 | Returns 1 on success. If the DECLARATOR is not suitable for a function | |
13509 | (it defines a datum instead), we return 0, which tells | |
13510 | yyparse to report a parse error. | |
13511 | ||
13512 | For C++, we must first check whether that datum makes any sense. | |
13513 | For example, "class A local_a(1,2);" means that variable local_a | |
13514 | is an aggregate of type A, which should have a constructor | |
87e3dbc9 | 13515 | applied to it with the argument list [1, 2]. */ |
8d08fdba MS |
13516 | |
13517 | int | |
a8f73d4b | 13518 | start_function (declspecs, declarator, attrs, flags) |
c11b6f21 | 13519 | tree declspecs, declarator, attrs; |
a8f73d4b | 13520 | int flags; |
8d08fdba | 13521 | { |
5566b478 | 13522 | tree decl1; |
8d08fdba MS |
13523 | tree ctype = NULL_TREE; |
13524 | tree fntype; | |
13525 | tree restype; | |
8d08fdba | 13526 | int doing_friend = 0; |
e2500fed | 13527 | struct cp_binding_level *bl; |
f444e36b | 13528 | tree current_function_parms; |
8d08fdba | 13529 | |
8d08fdba | 13530 | /* Sanity check. */ |
a1774733 | 13531 | my_friendly_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE, 160); |
8d08fdba MS |
13532 | my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161); |
13533 | ||
e92cc029 | 13534 | /* This should only be done once on the top most decl. */ |
594bb0e7 | 13535 | if (have_extern_spec) |
8d08fdba | 13536 | { |
1f8f4a0b | 13537 | declspecs = tree_cons (NULL_TREE, get_identifier ("extern"), declspecs); |
594bb0e7 | 13538 | have_extern_spec = false; |
8d08fdba MS |
13539 | } |
13540 | ||
a8f73d4b | 13541 | if (flags & SF_PRE_PARSED) |
8d08fdba MS |
13542 | { |
13543 | decl1 = declarator; | |
13544 | ||
8d08fdba MS |
13545 | fntype = TREE_TYPE (decl1); |
13546 | if (TREE_CODE (fntype) == METHOD_TYPE) | |
13547 | ctype = TYPE_METHOD_BASETYPE (fntype); | |
13548 | ||
cab1f180 ML |
13549 | /* ISO C++ 11.4/5. A friend function defined in a class is in |
13550 | the (lexical) scope of the class in which it is defined. */ | |
8d08fdba MS |
13551 | if (!ctype && DECL_FRIEND_P (decl1)) |
13552 | { | |
4f1c5b7d | 13553 | ctype = DECL_FRIEND_CONTEXT (decl1); |
8d08fdba MS |
13554 | |
13555 | /* CTYPE could be null here if we're dealing with a template; | |
13556 | for example, `inline friend float foo()' inside a template | |
13557 | will have no CTYPE set. */ | |
13558 | if (ctype && TREE_CODE (ctype) != RECORD_TYPE) | |
13559 | ctype = NULL_TREE; | |
13560 | else | |
13561 | doing_friend = 1; | |
13562 | } | |
13563 | ||
5566b478 | 13564 | last_function_parms = DECL_ARGUMENTS (decl1); |
8d08fdba MS |
13565 | } |
13566 | else | |
13567 | { | |
91d231cb | 13568 | decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL); |
8d08fdba MS |
13569 | /* If the declarator is not suitable for a function definition, |
13570 | cause a syntax error. */ | |
e89a6075 JM |
13571 | if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL) |
13572 | return 0; | |
13573 | ||
13574 | cplus_decl_attributes (&decl1, attrs, 0); | |
8d08fdba | 13575 | |
ecb0eece RH |
13576 | /* If #pragma weak was used, mark the decl weak now. */ |
13577 | if (current_binding_level == global_binding_level) | |
13578 | maybe_apply_pragma_weak (decl1); | |
13579 | ||
8d08fdba MS |
13580 | fntype = TREE_TYPE (decl1); |
13581 | ||
13582 | restype = TREE_TYPE (fntype); | |
7ddedda4 | 13583 | if (CLASS_TYPE_P (restype) && !CLASSTYPE_GOT_SEMICOLON (restype)) |
8d08fdba | 13584 | { |
33bd39a2 | 13585 | error ("semicolon missing after declaration of `%#T'", restype); |
051e6fd7 | 13586 | shadow_tag (build_tree_list (NULL_TREE, restype)); |
8d08fdba MS |
13587 | CLASSTYPE_GOT_SEMICOLON (restype) = 1; |
13588 | if (TREE_CODE (fntype) == FUNCTION_TYPE) | |
13589 | fntype = build_function_type (integer_type_node, | |
13590 | TYPE_ARG_TYPES (fntype)); | |
13591 | else | |
13592 | fntype = build_cplus_method_type (build_type_variant (TYPE_METHOD_BASETYPE (fntype), TREE_READONLY (decl1), TREE_SIDE_EFFECTS (decl1)), | |
13593 | integer_type_node, | |
13594 | TYPE_ARG_TYPES (fntype)); | |
13595 | TREE_TYPE (decl1) = fntype; | |
13596 | } | |
13597 | ||
13598 | if (TREE_CODE (fntype) == METHOD_TYPE) | |
13599 | ctype = TYPE_METHOD_BASETYPE (fntype); | |
35680744 | 13600 | else if (DECL_MAIN_P (decl1)) |
8d08fdba MS |
13601 | { |
13602 | /* If this doesn't return integer_type, complain. */ | |
13603 | if (TREE_TYPE (TREE_TYPE (decl1)) != integer_type_node) | |
13604 | { | |
a28e3c7f | 13605 | if (pedantic || warn_return_type) |
8251199e | 13606 | pedwarn ("return type for `main' changed to `int'"); |
8d08fdba MS |
13607 | TREE_TYPE (decl1) = fntype = default_function_type; |
13608 | } | |
8d08fdba MS |
13609 | } |
13610 | } | |
68642fb6 | 13611 | |
97055d5c AO |
13612 | if (DECL_DECLARED_INLINE_P (decl1) |
13613 | && lookup_attribute ("noinline", attrs)) | |
13614 | warning_with_decl (decl1, | |
13615 | "inline function `%s' given attribute noinline"); | |
13616 | ||
5f6eeeb3 NS |
13617 | if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1)) |
13618 | /* This is a constructor, we must ensure that any default args | |
13619 | introduced by this definition are propagated to the clones | |
13620 | now. The clones are used directly in overload resolution. */ | |
13621 | adjust_clone_args (decl1); | |
13622 | ||
b35d4555 MM |
13623 | /* Sometimes we don't notice that a function is a static member, and |
13624 | build a METHOD_TYPE for it. Fix that up now. */ | |
13625 | if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1) | |
13626 | && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE) | |
13627 | { | |
3afb32a4 | 13628 | revert_static_member_fn (decl1); |
b35d4555 MM |
13629 | last_function_parms = TREE_CHAIN (last_function_parms); |
13630 | ctype = NULL_TREE; | |
13631 | } | |
8d08fdba MS |
13632 | |
13633 | /* Warn if function was previously implicitly declared | |
13634 | (but not if we warned then). */ | |
13635 | if (! warn_implicit | |
13636 | && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE) | |
8251199e | 13637 | cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1))); |
8d08fdba | 13638 | |
f181d4ae MM |
13639 | /* Set up current_class_type, and enter the scope of the class, if |
13640 | appropriate. */ | |
13641 | if (ctype) | |
13642 | push_nested_class (ctype, 1); | |
13643 | else if (DECL_STATIC_FUNCTION_P (decl1)) | |
13644 | push_nested_class (DECL_CONTEXT (decl1), 2); | |
13645 | ||
13646 | /* Now that we have entered the scope of the class, we must restore | |
13647 | the bindings for any template parameters surrounding DECL1, if it | |
13648 | is an inline member template. (Order is important; consider the | |
13649 | case where a template parameter has the same name as a field of | |
13650 | the class.) It is not until after this point that | |
13651 | PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */ | |
a8f73d4b | 13652 | if (flags & SF_INCLASS_INLINE) |
f181d4ae MM |
13653 | maybe_begin_member_template_processing (decl1); |
13654 | ||
56cb9733 | 13655 | /* Effective C++ rule 15. */ |
9188c363 | 13656 | if (warn_ecpp |
596ea4e5 | 13657 | && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR |
9188c363 | 13658 | && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE) |
33bd39a2 | 13659 | warning ("`operator=' should return a reference to `*this'"); |
9188c363 MM |
13660 | |
13661 | /* Make the init_value nonzero so pushdecl knows this is not tentative. | |
13662 | error_mark_node is replaced below (in poplevel) with the BLOCK. */ | |
b35d4555 MM |
13663 | if (!DECL_INITIAL (decl1)) |
13664 | DECL_INITIAL (decl1) = error_mark_node; | |
9188c363 | 13665 | |
9188c363 MM |
13666 | /* This function exists in static storage. |
13667 | (This does not mean `static' in the C sense!) */ | |
13668 | TREE_STATIC (decl1) = 1; | |
13669 | ||
13670 | /* We must call push_template_decl after current_class_type is set | |
13671 | up. (If we are processing inline definitions after exiting a | |
13672 | class scope, current_class_type will be NULL_TREE until set above | |
13673 | by push_nested_class.) */ | |
13674 | if (processing_template_decl) | |
13675 | decl1 = push_template_decl (decl1); | |
13676 | ||
f181d4ae | 13677 | /* We are now in the scope of the function being defined. */ |
8d08fdba | 13678 | current_function_decl = decl1; |
f181d4ae | 13679 | |
5566b478 MS |
13680 | /* Save the parm names or decls from this function's declarator |
13681 | where store_parm_decls will find them. */ | |
13682 | current_function_parms = last_function_parms; | |
8d08fdba | 13683 | |
a8f73d4b MM |
13684 | /* Make sure the parameter and return types are reasonable. When |
13685 | you declare a function, these types can be incomplete, but they | |
13686 | must be complete when you define the function. */ | |
5156628f | 13687 | if (! processing_template_decl) |
f444e36b | 13688 | check_function_type (decl1, current_function_parms); |
f181d4ae | 13689 | |
a8f73d4b MM |
13690 | /* Build the return declaration for the function. */ |
13691 | restype = TREE_TYPE (fntype); | |
e89a6075 JM |
13692 | /* Promote the value to int before returning it. */ |
13693 | if (c_promoting_integer_type_p (restype)) | |
13694 | restype = type_promotes_to (restype); | |
13695 | if (DECL_RESULT (decl1) == NULL_TREE) | |
a8f73d4b | 13696 | { |
e89a6075 JM |
13697 | DECL_RESULT (decl1) |
13698 | = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype)); | |
13699 | c_apply_type_quals_to_decl (cp_type_quals (restype), | |
13700 | DECL_RESULT (decl1)); | |
5566b478 | 13701 | } |
a8f73d4b MM |
13702 | |
13703 | /* Initialize RTL machinery. We cannot do this until | |
13704 | CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this | |
13705 | even when processing a template; this is how we get | |
c00996a3 JM |
13706 | CFUN set up, and our per-function variables initialized. |
13707 | FIXME factor out the non-RTL stuff. */ | |
a8f73d4b MM |
13708 | bl = current_binding_level; |
13709 | init_function_start (decl1, input_filename, lineno); | |
13710 | current_binding_level = bl; | |
a8f73d4b MM |
13711 | |
13712 | /* Even though we're inside a function body, we still don't want to | |
13713 | call expand_expr to calculate the size of a variable-sized array. | |
13714 | We haven't necessarily assigned RTL to all variables yet, so it's | |
13715 | not safe to try to expand expressions involving them. */ | |
13716 | immediate_size_expand = 0; | |
01d939e8 | 13717 | cfun->x_dont_save_pending_sizes_p = 1; |
a8f73d4b | 13718 | |
f444e36b MM |
13719 | /* Start the statement-tree, start the tree now. */ |
13720 | begin_stmt_tree (&DECL_SAVED_TREE (decl1)); | |
6f80451c | 13721 | |
a8f73d4b | 13722 | /* Let the user know we're compiling this function. */ |
ea11ca7e | 13723 | announce_function (decl1); |
b7484fbe | 13724 | |
878cd289 MS |
13725 | /* Record the decl so that the function name is defined. |
13726 | If we already have a decl for this name, and it is a FUNCTION_DECL, | |
13727 | use the old decl. */ | |
a8f73d4b | 13728 | if (!processing_template_decl && !(flags & SF_PRE_PARSED)) |
878cd289 | 13729 | { |
75650646 | 13730 | /* A specialization is not used to guide overload resolution. */ |
2228d450 | 13731 | if (!DECL_TEMPLATE_SPECIALIZATION (decl1) |
959d8796 | 13732 | && ! DECL_FUNCTION_MEMBER_P (decl1)) |
75650646 | 13733 | decl1 = pushdecl (decl1); |
2c73f9f5 | 13734 | else |
b7698cf0 | 13735 | { |
a1c65f9f | 13736 | /* We need to set the DECL_CONTEXT. */ |
b7698cf0 JM |
13737 | if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1)) |
13738 | DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1)); | |
13739 | /* And make sure we have enough default args. */ | |
13740 | check_default_args (decl1); | |
13741 | } | |
878cd289 MS |
13742 | fntype = TREE_TYPE (decl1); |
13743 | } | |
5566b478 | 13744 | |
a8f73d4b | 13745 | /* Reset these in case the call to pushdecl changed them. */ |
5566b478 | 13746 | current_function_decl = decl1; |
01d939e8 | 13747 | cfun->decl = decl1; |
878cd289 | 13748 | |
78c120b5 MM |
13749 | /* If we are (erroneously) defining a function that we have already |
13750 | defined before, wipe out what we knew before. */ | |
e2500fed GK |
13751 | if (!DECL_PENDING_INLINE_P (decl1)) |
13752 | DECL_SAVED_FUNCTION_DATA (decl1) = NULL; | |
b35d4555 | 13753 | |
f444e36b | 13754 | if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1)) |
b35d4555 MM |
13755 | { |
13756 | /* We know that this was set up by `grokclassfn'. We do not | |
13757 | wait until `store_parm_decls', since evil parse errors may | |
13758 | never get us to that point. Here we keep the consistency | |
13759 | between `current_class_type' and `current_class_ptr'. */ | |
13760 | tree t = DECL_ARGUMENTS (decl1); | |
68642fb6 UD |
13761 | |
13762 | my_friendly_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL, | |
b35d4555 MM |
13763 | 162); |
13764 | my_friendly_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE, | |
13765 | 19990811); | |
68642fb6 UD |
13766 | |
13767 | cp_function_chain->x_current_class_ref | |
3e411c3f | 13768 | = build_indirect_ref (t, NULL); |
b35d4555 MM |
13769 | cp_function_chain->x_current_class_ptr = t; |
13770 | ||
018fc244 MM |
13771 | /* Constructors and destructors need to know whether they're "in |
13772 | charge" of initializing virtual base classes. */ | |
e0fff4b3 | 13773 | t = TREE_CHAIN (t); |
454fa7a7 | 13774 | if (DECL_HAS_IN_CHARGE_PARM_P (decl1)) |
e0fff4b3 JM |
13775 | { |
13776 | current_in_charge_parm = t; | |
13777 | t = TREE_CHAIN (t); | |
13778 | } | |
13779 | if (DECL_HAS_VTT_PARM_P (decl1)) | |
13780 | { | |
13781 | if (DECL_NAME (t) != vtt_parm_identifier) | |
13782 | abort (); | |
13783 | current_vtt_parm = t; | |
13784 | } | |
b35d4555 MM |
13785 | } |
13786 | ||
db5ae43f | 13787 | if (DECL_INTERFACE_KNOWN (decl1)) |
faae18ab | 13788 | { |
4f1c5b7d | 13789 | tree ctx = decl_function_context (decl1); |
86052cc3 | 13790 | |
faae18ab MS |
13791 | if (DECL_NOT_REALLY_EXTERN (decl1)) |
13792 | DECL_EXTERNAL (decl1) = 0; | |
86052cc3 | 13793 | |
79065db2 | 13794 | if (ctx != NULL_TREE && DECL_DECLARED_INLINE_P (ctx) |
86052cc3 JM |
13795 | && TREE_PUBLIC (ctx)) |
13796 | /* This is a function in a local class in an extern inline | |
13797 | function. */ | |
13798 | comdat_linkage (decl1); | |
faae18ab | 13799 | } |
8d08fdba MS |
13800 | /* If this function belongs to an interface, it is public. |
13801 | If it belongs to someone else's interface, it is also external. | |
1f901793 | 13802 | This only affects inlines and template instantiations. */ |
5566b478 MS |
13803 | else if (interface_unknown == 0 |
13804 | && (! DECL_TEMPLATE_INSTANTIATION (decl1) | |
d11ad92e | 13805 | || flag_alt_external_templates)) |
8d08fdba | 13806 | { |
79065db2 MM |
13807 | if (DECL_DECLARED_INLINE_P (decl1) |
13808 | || DECL_TEMPLATE_INSTANTIATION (decl1) | |
5156628f | 13809 | || processing_template_decl) |
1f901793 JM |
13810 | { |
13811 | DECL_EXTERNAL (decl1) | |
13812 | = (interface_only | |
79065db2 MM |
13813 | || (DECL_DECLARED_INLINE_P (decl1) |
13814 | && ! flag_implement_inlines | |
9c73ec84 | 13815 | && !DECL_VINDEX (decl1))); |
1f901793 JM |
13816 | |
13817 | /* For WIN32 we also want to put these in linkonce sections. */ | |
13818 | maybe_make_one_only (decl1); | |
13819 | } | |
db5ae43f | 13820 | else |
893de33c | 13821 | DECL_EXTERNAL (decl1) = 0; |
e8abc66f | 13822 | DECL_NOT_REALLY_EXTERN (decl1) = 0; |
db5ae43f | 13823 | DECL_INTERFACE_KNOWN (decl1) = 1; |
8d08fdba | 13824 | } |
c16c47fb JM |
13825 | else if (interface_unknown && interface_only |
13826 | && (! DECL_TEMPLATE_INSTANTIATION (decl1) | |
13827 | || flag_alt_external_templates)) | |
13828 | { | |
13829 | /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma | |
13830 | interface, we will have interface_only set but not | |
13831 | interface_known. In that case, we don't want to use the normal | |
13832 | heuristics because someone will supply a #pragma implementation | |
13833 | elsewhere, and deducing it here would produce a conflict. */ | |
13834 | comdat_linkage (decl1); | |
13835 | DECL_EXTERNAL (decl1) = 0; | |
13836 | DECL_INTERFACE_KNOWN (decl1) = 1; | |
13837 | DECL_DEFER_OUTPUT (decl1) = 1; | |
13838 | } | |
8d08fdba | 13839 | else |
a0a33927 MS |
13840 | { |
13841 | /* This is a definition, not a reference. | |
b7484fbe MS |
13842 | So clear DECL_EXTERNAL. */ |
13843 | DECL_EXTERNAL (decl1) = 0; | |
faae18ab | 13844 | |
79065db2 MM |
13845 | if ((DECL_DECLARED_INLINE_P (decl1) |
13846 | || DECL_TEMPLATE_INSTANTIATION (decl1)) | |
5566b478 MS |
13847 | && ! DECL_INTERFACE_KNOWN (decl1) |
13848 | /* Don't try to defer nested functions for now. */ | |
4f1c5b7d | 13849 | && ! decl_function_context (decl1)) |
878cd289 MS |
13850 | DECL_DEFER_OUTPUT (decl1) = 1; |
13851 | else | |
893de33c | 13852 | DECL_INTERFACE_KNOWN (decl1) = 1; |
db5ae43f | 13853 | } |
a9aedbc2 | 13854 | |
f444e36b MM |
13855 | pushlevel (0); |
13856 | current_binding_level->parm_flag = 1; | |
8d08fdba | 13857 | |
5566b478 MS |
13858 | ++function_depth; |
13859 | ||
0d9eb3ba | 13860 | if (DECL_DESTRUCTOR_P (decl1)) |
46e8c075 MM |
13861 | { |
13862 | dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE); | |
13863 | DECL_CONTEXT (dtor_label) = current_function_decl; | |
13864 | } | |
8d08fdba | 13865 | |
0ba8a114 NS |
13866 | start_fname_decls (); |
13867 | ||
f444e36b MM |
13868 | store_parm_decls (current_function_parms); |
13869 | ||
8d08fdba MS |
13870 | return 1; |
13871 | } | |
13872 | \f | |
13873 | /* Store the parameter declarations into the current function declaration. | |
13874 | This is called after parsing the parameter declarations, before | |
13875 | digesting the body of the function. | |
13876 | ||
13877 | Also install to binding contour return value identifier, if any. */ | |
13878 | ||
f444e36b MM |
13879 | static void |
13880 | store_parm_decls (current_function_parms) | |
13881 | tree current_function_parms; | |
8d08fdba MS |
13882 | { |
13883 | register tree fndecl = current_function_decl; | |
13884 | register tree parm; | |
8d08fdba | 13885 | |
8d08fdba MS |
13886 | /* This is a chain of any other decls that came in among the parm |
13887 | declarations. If a parm is declared with enum {foo, bar} x; | |
13888 | then CONST_DECLs for foo and bar are put here. */ | |
13889 | tree nonparms = NULL_TREE; | |
13890 | ||
b35d4555 | 13891 | if (current_function_parms) |
8d08fdba MS |
13892 | { |
13893 | /* This case is when the function was defined with an ANSI prototype. | |
13894 | The parms already have decls, so we need not do anything here | |
13895 | except record them as in effect | |
13896 | and complain if any redundant old-style parm decls were written. */ | |
13897 | ||
b35d4555 MM |
13898 | tree specparms = current_function_parms; |
13899 | tree next; | |
13900 | ||
f444e36b | 13901 | /* Must clear this because it might contain TYPE_DECLs declared |
b35d4555 | 13902 | at class level. */ |
f444e36b | 13903 | storedecls (NULL_TREE); |
8d08fdba | 13904 | |
f444e36b | 13905 | /* If we're doing semantic analysis, then we'll call pushdecl |
b35d4555 MM |
13906 | for each of these. We must do them in reverse order so that |
13907 | they end in the correct forward order. */ | |
f444e36b | 13908 | specparms = nreverse (specparms); |
5566b478 | 13909 | |
b35d4555 | 13910 | for (parm = specparms; parm; parm = next) |
8d08fdba MS |
13911 | { |
13912 | next = TREE_CHAIN (parm); | |
13913 | if (TREE_CODE (parm) == PARM_DECL) | |
13914 | { | |
f444e36b MM |
13915 | if (DECL_NAME (parm) == NULL_TREE |
13916 | || TREE_CODE (parm) != VOID_TYPE) | |
13917 | pushdecl (parm); | |
13918 | else | |
33bd39a2 | 13919 | error ("parameter `%D' declared void", parm); |
8d08fdba MS |
13920 | } |
13921 | else | |
13922 | { | |
13923 | /* If we find an enum constant or a type tag, | |
13924 | put it aside for the moment. */ | |
13925 | TREE_CHAIN (parm) = NULL_TREE; | |
13926 | nonparms = chainon (nonparms, parm); | |
13927 | } | |
13928 | } | |
13929 | ||
f444e36b MM |
13930 | /* Get the decls in their original chain order and record in the |
13931 | function. This is all and only the PARM_DECLs that were | |
13932 | pushed into scope by the loop above. */ | |
13933 | DECL_ARGUMENTS (fndecl) = getdecls (); | |
969fd501 | 13934 | storetags (gettags ()); |
8d08fdba MS |
13935 | } |
13936 | else | |
13937 | DECL_ARGUMENTS (fndecl) = NULL_TREE; | |
13938 | ||
13939 | /* Now store the final chain of decls for the arguments | |
13940 | as the decl-chain of the current lexical scope. | |
13941 | Put the enumerators in as well, at the front so that | |
13942 | DECL_ARGUMENTS is not modified. */ | |
f444e36b | 13943 | storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl))); |
eb448459 | 13944 | |
b35d4555 | 13945 | /* Do the starting of the exception specifications, if we have any. */ |
68642fb6 | 13946 | if (flag_exceptions && !processing_template_decl |
1660cb3a | 13947 | && flag_enforce_eh_specs |
b35d4555 | 13948 | && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl))) |
52a11cbf | 13949 | current_eh_spec_block = begin_eh_spec_block (); |
8d08fdba MS |
13950 | } |
13951 | ||
8d08fdba | 13952 | \f |
59026e79 MM |
13953 | /* We have finished doing semantic analysis on DECL, but have not yet |
13954 | generated RTL for its body. Save away our current state, so that | |
13955 | when we want to generate RTL later we know what to do. */ | |
13956 | ||
13957 | static void | |
13958 | save_function_data (decl) | |
13959 | tree decl; | |
13960 | { | |
e2500fed | 13961 | struct language_function *f; |
59026e79 MM |
13962 | |
13963 | /* Save the language-specific per-function data so that we can | |
13964 | get it back when we really expand this function. */ | |
13965 | my_friendly_assert (!DECL_PENDING_INLINE_P (decl), | |
13966 | 19990908); | |
68642fb6 | 13967 | |
59026e79 | 13968 | /* Make a copy. */ |
e2500fed GK |
13969 | f = ((struct language_function *) |
13970 | ggc_alloc (sizeof (struct language_function))); | |
13971 | memcpy (f, cp_function_chain, sizeof (struct language_function)); | |
59026e79 MM |
13972 | DECL_SAVED_FUNCTION_DATA (decl) = f; |
13973 | ||
13974 | /* Clear out the bits we don't need. */ | |
ae499cce MM |
13975 | f->base.x_stmt_tree.x_last_stmt = NULL_TREE; |
13976 | f->base.x_stmt_tree.x_last_expr_type = NULL_TREE; | |
59026e79 MM |
13977 | f->x_named_label_uses = NULL; |
13978 | f->bindings = NULL; | |
8e4ce833 | 13979 | f->x_local_names = NULL; |
59026e79 MM |
13980 | |
13981 | /* When we get back here again, we will be expanding. */ | |
13982 | f->x_expanding_p = 1; | |
914653a2 MM |
13983 | |
13984 | /* If we've already decided that we cannot inline this function, we | |
13985 | must remember that fact when we actually go to expand the | |
13986 | function. */ | |
acc72c37 MM |
13987 | if (current_function_cannot_inline) |
13988 | { | |
13989 | f->cannot_inline = current_function_cannot_inline; | |
13990 | DECL_INLINE (decl) = 0; | |
13991 | } | |
59026e79 MM |
13992 | } |
13993 | ||
cdd2559c JM |
13994 | /* Add a note to mark the beginning of the main body of the constructor. |
13995 | This is used to set up the data structures for the cleanup regions for | |
13996 | fully-constructed bases and members. */ | |
13997 | ||
13998 | static void | |
13999 | begin_constructor_body () | |
14000 | { | |
cdd2559c JM |
14001 | } |
14002 | ||
ade3dc07 JM |
14003 | /* Add a note to mark the end of the main body of the constructor. This is |
14004 | used to end the cleanup regions for fully-constructed bases and | |
14005 | members. */ | |
efee38a9 MM |
14006 | |
14007 | static void | |
14008 | finish_constructor_body () | |
14009 | { | |
efee38a9 MM |
14010 | } |
14011 | ||
cdd2559c JM |
14012 | /* Do all the processing for the beginning of a destructor; set up the |
14013 | vtable pointers and cleanups for bases and members. */ | |
14014 | ||
14015 | static void | |
14016 | begin_destructor_body () | |
14017 | { | |
14018 | tree if_stmt; | |
14019 | tree compound_stmt; | |
14020 | ||
14021 | /* If the dtor is empty, and we know there is not any possible | |
14022 | way we could use any vtable entries, before they are possibly | |
14023 | set by a base class dtor, we don't have to setup the vtables, | |
14024 | as we know that any base class dtor will set up any vtables | |
14025 | it needs. We avoid MI, because one base class dtor can do a | |
14026 | virtual dispatch to an overridden function that would need to | |
14027 | have a non-related vtable set up, we cannot avoid setting up | |
14028 | vtables in that case. We could change this to see if there | |
14029 | is just one vtable. | |
14030 | ||
14031 | ??? In the destructor for a class, the vtables are set | |
14032 | appropriately for that class. There will be no non-related | |
14033 | vtables. jason 2001-12-11. */ | |
14034 | if_stmt = begin_if_stmt (); | |
14035 | ||
14036 | /* If it is not safe to avoid setting up the vtables, then | |
14037 | someone will change the condition to be boolean_true_node. | |
14038 | (Actually, for now, we do not have code to set the condition | |
14039 | appropriately, so we just assume that we always need to | |
14040 | initialize the vtables.) */ | |
14041 | finish_if_stmt_cond (boolean_true_node, if_stmt); | |
cdd2559c JM |
14042 | |
14043 | compound_stmt = begin_compound_stmt (/*has_no_scope=*/0); | |
14044 | ||
14045 | /* Make all virtual function table pointers in non-virtual base | |
14046 | classes point to CURRENT_CLASS_TYPE's virtual function | |
14047 | tables. */ | |
14048 | initialize_vtbl_ptrs (current_class_ptr); | |
14049 | ||
14050 | finish_compound_stmt (/*has_no_scope=*/0, compound_stmt); | |
14051 | finish_then_clause (if_stmt); | |
14052 | finish_if_stmt (); | |
14053 | ||
14054 | /* And insert cleanups for our bases and members so that they | |
14055 | will be properly destroyed if we throw. */ | |
14056 | push_base_cleanups (); | |
14057 | } | |
14058 | ||
ade3dc07 JM |
14059 | /* At the end of every destructor we generate code to delete the object if |
14060 | necessary. Do that now. */ | |
9bfadf57 MM |
14061 | |
14062 | static void | |
14063 | finish_destructor_body () | |
14064 | { | |
9bfadf57 MM |
14065 | tree exprstmt; |
14066 | ||
52682a1b MM |
14067 | /* In a virtual destructor, we must call delete. */ |
14068 | if (DECL_VIRTUAL_P (current_function_decl)) | |
14069 | { | |
14070 | tree if_stmt; | |
fa72b064 | 14071 | tree virtual_size = cxx_sizeof (current_class_type); |
68642fb6 | 14072 | |
52682a1b | 14073 | /* [class.dtor] |
68642fb6 | 14074 | |
ade3dc07 JM |
14075 | At the point of definition of a virtual destructor (including |
14076 | an implicit definition), non-placement operator delete shall | |
14077 | be looked up in the scope of the destructor's class and if | |
14078 | found shall be accessible and unambiguous. */ | |
52682a1b MM |
14079 | exprstmt = build_op_delete_call |
14080 | (DELETE_EXPR, current_class_ptr, virtual_size, | |
14081 | LOOKUP_NORMAL | LOOKUP_SPECULATIVELY, NULL_TREE); | |
298d6f60 | 14082 | |
52682a1b MM |
14083 | if_stmt = begin_if_stmt (); |
14084 | finish_if_stmt_cond (build (BIT_AND_EXPR, integer_type_node, | |
14085 | current_in_charge_parm, | |
14086 | integer_one_node), | |
14087 | if_stmt); | |
14088 | finish_expr_stmt (exprstmt); | |
14089 | finish_then_clause (if_stmt); | |
14090 | finish_if_stmt (); | |
14091 | } | |
ade3dc07 | 14092 | } |
9bfadf57 | 14093 | |
ade3dc07 JM |
14094 | /* Do the necessary processing for the beginning of a function body, which |
14095 | in this case includes member-initializers, but not the catch clauses of | |
14096 | a function-try-block. Currently, this means opening a binding level | |
14097 | for the member-initializers (in a ctor) and member cleanups (in a dtor). | |
14098 | In other functions, this isn't necessary, but it doesn't hurt. */ | |
14099 | ||
14100 | tree | |
14101 | begin_function_body () | |
14102 | { | |
cdd2559c JM |
14103 | tree stmt; |
14104 | ||
b5856475 JM |
14105 | if (processing_template_decl) |
14106 | /* Do nothing now. */; | |
14107 | else | |
14108 | /* Always keep the BLOCK node associated with the outermost pair of | |
14109 | curly braces of a function. These are needed for correct | |
14110 | operation of dwarfout.c. */ | |
14111 | keep_next_level (1); | |
14112 | ||
cdd2559c | 14113 | stmt = begin_compound_stmt (0); |
ade3dc07 | 14114 | COMPOUND_STMT_BODY_BLOCK (stmt) = 1; |
cdd2559c JM |
14115 | |
14116 | if (processing_template_decl) | |
14117 | /* Do nothing now. */; | |
14118 | else if (DECL_CONSTRUCTOR_P (current_function_decl)) | |
14119 | begin_constructor_body (); | |
14120 | else if (DECL_DESTRUCTOR_P (current_function_decl)) | |
14121 | begin_destructor_body (); | |
14122 | ||
ade3dc07 | 14123 | return stmt; |
9bfadf57 MM |
14124 | } |
14125 | ||
ade3dc07 JM |
14126 | /* Do the processing for the end of a function body. Currently, this means |
14127 | closing out the cleanups for fully-constructed bases and members, and in | |
14128 | the case of the destructor, deleting the object if desired. Again, this | |
14129 | is only meaningful for [cd]tors, since they are the only functions where | |
14130 | there is a significant distinction between the main body and any | |
14131 | function catch clauses. Handling, say, main() return semantics here | |
14132 | would be wrong, as flowing off the end of a function catch clause for | |
14133 | main() would also need to return 0. */ | |
14134 | ||
14135 | void | |
14136 | finish_function_body (compstmt) | |
14137 | tree compstmt; | |
14138 | { | |
14139 | if (processing_template_decl) | |
14140 | /* Do nothing now. */; | |
14141 | else if (DECL_DESTRUCTOR_P (current_function_decl)) | |
14142 | /* Any return from a destructor will end up here. Put it before the | |
14143 | cleanups so that an explicit return doesn't duplicate them. */ | |
14144 | add_stmt (build_stmt (LABEL_STMT, dtor_label)); | |
14145 | ||
14146 | /* Close the block; in a destructor, run the member cleanups. */ | |
14147 | finish_compound_stmt (0, compstmt); | |
14148 | ||
14149 | if (processing_template_decl) | |
14150 | /* Do nothing now. */; | |
14151 | else if (DECL_CONSTRUCTOR_P (current_function_decl)) | |
14152 | finish_constructor_body (); | |
14153 | else if (DECL_DESTRUCTOR_P (current_function_decl)) | |
14154 | finish_destructor_body (); | |
14155 | } | |
14156 | ||
8d08fdba MS |
14157 | /* Finish up a function declaration and compile that function |
14158 | all the way to assembler language output. The free the storage | |
14159 | for the function definition. | |
14160 | ||
68642fb6 | 14161 | FLAGS is a bitwise or of the following values: |
f181d4ae MM |
14162 | 2 - INCLASS_INLINE |
14163 | We just finished processing the body of an in-class inline | |
14164 | function definition. (This processing will have taken place | |
87e3dbc9 | 14165 | after the class definition is complete.) */ |
8d08fdba | 14166 | |
4d6abc1c | 14167 | tree |
0acf7199 | 14168 | finish_function (flags) |
f181d4ae | 14169 | int flags; |
8d08fdba MS |
14170 | { |
14171 | register tree fndecl = current_function_decl; | |
14172 | tree fntype, ctype = NULL_TREE; | |
f181d4ae | 14173 | int inclass_inline = (flags & 2) != 0; |
87e3dbc9 | 14174 | int nested; |
8d08fdba MS |
14175 | |
14176 | /* When we get some parse errors, we can end up without a | |
14177 | current_function_decl, so cope. */ | |
14178 | if (fndecl == NULL_TREE) | |
4d6abc1c | 14179 | return error_mark_node; |
8d08fdba | 14180 | |
87e3dbc9 | 14181 | nested = function_depth > 1; |
8d08fdba MS |
14182 | fntype = TREE_TYPE (fndecl); |
14183 | ||
9bfadf57 MM |
14184 | /* TREE_READONLY (fndecl) = 1; |
14185 | This caused &foo to be of type ptr-to-const-function | |
14186 | which then got a warning when stored in a ptr-to-function variable. */ | |
8d08fdba | 14187 | |
f444e36b | 14188 | my_friendly_assert (building_stmt_tree (), 20000911); |
8d08fdba | 14189 | |
0ba8a114 NS |
14190 | finish_fname_decls (); |
14191 | ||
db9b2174 MM |
14192 | /* For a cloned function, we've already got all the code we need; |
14193 | there's no need to add any extra bits. */ | |
f444e36b | 14194 | if (!DECL_CLONED_FUNCTION_P (fndecl)) |
8d08fdba | 14195 | { |
ade3dc07 | 14196 | if (DECL_MAIN_P (current_function_decl)) |
efee38a9 MM |
14197 | { |
14198 | /* Make it so that `main' always returns 0 by default. */ | |
08c7ae5a | 14199 | #if VMS_TARGET |
efee38a9 MM |
14200 | finish_return_stmt (integer_one_node); |
14201 | #else | |
14202 | finish_return_stmt (integer_zero_node); | |
14203 | #endif | |
14204 | } | |
87e3dbc9 | 14205 | |
b35d4555 MM |
14206 | /* Finish dealing with exception specifiers. */ |
14207 | if (flag_exceptions && !processing_template_decl | |
1660cb3a | 14208 | && flag_enforce_eh_specs |
b35d4555 | 14209 | && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl))) |
52a11cbf RH |
14210 | finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS |
14211 | (TREE_TYPE (current_function_decl)), | |
14212 | current_eh_spec_block); | |
5566b478 | 14213 | } |
68642fb6 | 14214 | |
558475f0 | 14215 | /* If we're saving up tree structure, tie off the function now. */ |
f444e36b | 14216 | finish_stmt_tree (&DECL_SAVED_TREE (fndecl)); |
8d2733ca | 14217 | |
8d08fdba MS |
14218 | /* This must come after expand_function_end because cleanups might |
14219 | have declarations (from inline functions) that need to go into | |
14220 | this function's blocks. */ | |
7437519c ZW |
14221 | |
14222 | /* If the current binding level isn't the outermost binding level | |
14223 | for this function, either there is a bug, or we have experienced | |
14224 | syntax errors and the statement tree is malformed. */ | |
f444e36b | 14225 | if (current_binding_level->parm_flag != 1) |
7437519c ZW |
14226 | { |
14227 | /* Make sure we have already experienced errors. */ | |
14228 | if (errorcount == 0) | |
14229 | abort (); | |
14230 | ||
14231 | /* Throw away the broken statement tree and extra binding | |
14232 | levels. */ | |
14233 | DECL_SAVED_TREE (fndecl) = build_stmt (COMPOUND_STMT, NULL_TREE); | |
14234 | ||
14235 | while (current_binding_level->parm_flag != 1) | |
14236 | { | |
14237 | if (current_binding_level->parm_flag == 2) | |
14238 | pop_nested_class (); | |
14239 | else | |
14240 | poplevel (0, 0, 0); | |
14241 | } | |
14242 | } | |
f444e36b | 14243 | poplevel (1, 0, 1); |
8d08fdba | 14244 | |
07b2f2fd JM |
14245 | /* Set up the named return value optimization, if we can. Here, we |
14246 | eliminate the copy from the nrv into the RESULT_DECL and any cleanup | |
14247 | for the nrv. genrtl_start_function and declare_return_variable | |
14248 | handle making the nrv and RESULT_DECL share space. */ | |
14249 | if (current_function_return_value) | |
14250 | { | |
14251 | tree r = current_function_return_value; | |
14252 | /* This is only worth doing for fns that return in memory--and | |
14253 | simpler, since we don't have to worry about promoted modes. */ | |
14254 | if (r != error_mark_node | |
14255 | && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)))) | |
14256 | { | |
14257 | DECL_ALIGN (r) = DECL_ALIGN (DECL_RESULT (fndecl)); | |
14258 | walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl), | |
14259 | nullify_returns_r, r); | |
14260 | } | |
14261 | else | |
14262 | /* Clear it so genrtl_start_function and declare_return_variable | |
14263 | know we're not optimizing. */ | |
14264 | current_function_return_value = NULL_TREE; | |
14265 | } | |
14266 | ||
a8f73d4b | 14267 | /* Remember that we were in class scope. */ |
db5ae43f | 14268 | if (current_class_name) |
a8f73d4b | 14269 | ctype = current_class_type; |
db5ae43f | 14270 | |
1caa11d3 MM |
14271 | /* Must mark the RESULT_DECL as being in this function. */ |
14272 | DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl; | |
14273 | ||
14274 | /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point | |
14275 | to the FUNCTION_DECL node itself. */ | |
14276 | BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl; | |
14277 | ||
59026e79 | 14278 | /* Save away current state, if appropriate. */ |
f444e36b | 14279 | if (!processing_template_decl) |
59026e79 MM |
14280 | save_function_data (fndecl); |
14281 | ||
95fabfd3 MM |
14282 | /* If this function calls `setjmp' it cannot be inlined. When |
14283 | `longjmp' is called it is not guaranteed to restore the value of | |
14284 | local variables that have been modified since the call to | |
14285 | `setjmp'. So, if were to inline this function into some caller | |
14286 | `c', then when we `longjmp', we might not restore all variables | |
14287 | in `c'. (It might seem, at first blush, that there's no way for | |
14288 | this function to modify local variables in `c', but their | |
14289 | addresses may have been stored somewhere accessible to this | |
14290 | function.) */ | |
f444e36b | 14291 | if (!processing_template_decl && calls_setjmp_p (fndecl)) |
95fabfd3 MM |
14292 | DECL_UNINLINABLE (fndecl) = 1; |
14293 | ||
efe49da0 | 14294 | /* Complain if there's just no return statement. */ |
46cfb101 JM |
14295 | if (warn_return_type |
14296 | && !processing_template_decl | |
efe49da0 | 14297 | && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE |
57d5032b | 14298 | && !current_function_returns_value && !current_function_returns_null |
efe49da0 JM |
14299 | /* Don't complain if we abort or throw. */ |
14300 | && !current_function_returns_abnormally | |
46cfb101 JM |
14301 | && !DECL_NAME (DECL_RESULT (fndecl)) |
14302 | /* Normally, with -Wreturn-type, flow will complain. Unless we're an | |
efe49da0 | 14303 | inline function, as we might never be compiled separately. */ |
46cfb101 | 14304 | && DECL_INLINE (fndecl)) |
efe49da0 JM |
14305 | warning ("no return statement in function returning non-void"); |
14306 | ||
f444e36b MM |
14307 | /* Clear out memory we no longer need. */ |
14308 | free_after_parsing (cfun); | |
14309 | /* Since we never call rest_of_compilation, we never clear | |
14310 | CFUN. Do so explicitly. */ | |
14311 | free_after_compilation (cfun); | |
14312 | cfun = NULL; | |
a8f73d4b | 14313 | |
27631dae | 14314 | /* If this is an in-class inline definition, we may have to pop the |
a8f73d4b MM |
14315 | bindings for the template parameters that we added in |
14316 | maybe_begin_member_template_processing when start_function was | |
14317 | called. */ | |
14318 | if (inclass_inline) | |
14319 | maybe_end_member_template_processing (); | |
14320 | ||
14321 | /* Leave the scope of the class. */ | |
14322 | if (ctype) | |
14323 | pop_nested_class (); | |
5566b478 MS |
14324 | |
14325 | --function_depth; | |
8d08fdba | 14326 | |
4d6abc1c | 14327 | /* Clean up. */ |
28cbf42c | 14328 | if (! nested) |
1f8f4a0b MM |
14329 | /* Let the error reporting routines know that we're outside a |
14330 | function. For a nested function, this value is used in | |
b03e38e1 | 14331 | cxx_pop_function_context and then reset via pop_function_context. */ |
1f8f4a0b | 14332 | current_function_decl = NULL_TREE; |
4d6abc1c MM |
14333 | |
14334 | return fndecl; | |
8d08fdba MS |
14335 | } |
14336 | \f | |
14337 | /* Create the FUNCTION_DECL for a function definition. | |
8d08fdba MS |
14338 | DECLSPECS and DECLARATOR are the parts of the declaration; |
14339 | they describe the return type and the name of the function, | |
14340 | but twisted together in a fashion that parallels the syntax of C. | |
14341 | ||
14342 | This function creates a binding context for the function body | |
14343 | as well as setting up the FUNCTION_DECL in current_function_decl. | |
14344 | ||
14345 | Returns a FUNCTION_DECL on success. | |
14346 | ||
14347 | If the DECLARATOR is not suitable for a function (it defines a datum | |
14348 | instead), we return 0, which tells yyparse to report a parse error. | |
14349 | ||
14350 | May return void_type_node indicating that this method is actually | |
14351 | a friend. See grokfield for more details. | |
14352 | ||
14353 | Came here with a `.pushlevel' . | |
14354 | ||
14355 | DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING | |
14356 | CHANGES TO CODE IN `grokfield'. */ | |
e92cc029 | 14357 | |
8d08fdba | 14358 | tree |
acf82af2 JM |
14359 | start_method (declspecs, declarator, attrlist) |
14360 | tree declarator, declspecs, attrlist; | |
8d08fdba | 14361 | { |
c11b6f21 | 14362 | tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0, |
91d231cb | 14363 | &attrlist); |
8d08fdba MS |
14364 | |
14365 | /* Something too ugly to handle. */ | |
14366 | if (fndecl == NULL_TREE) | |
14367 | return NULL_TREE; | |
14368 | ||
195a5def IR |
14369 | if (attrlist) |
14370 | cplus_decl_attributes (&fndecl, attrlist, 0); | |
14371 | ||
8d08fdba | 14372 | /* Pass friends other than inline friend functions back. */ |
a1774733 | 14373 | if (fndecl == void_type_node) |
8d08fdba MS |
14374 | return fndecl; |
14375 | ||
14376 | if (TREE_CODE (fndecl) != FUNCTION_DECL) | |
14377 | /* Not a function, tell parser to report parse error. */ | |
14378 | return NULL_TREE; | |
14379 | ||
8d08fdba MS |
14380 | if (DECL_IN_AGGR_P (fndecl)) |
14381 | { | |
14382 | if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type) | |
14383 | { | |
68642fb6 | 14384 | if (DECL_CONTEXT (fndecl) |
2c73f9f5 | 14385 | && TREE_CODE( DECL_CONTEXT (fndecl)) != NAMESPACE_DECL) |
33bd39a2 | 14386 | error ("`%D' is already defined in class `%T'", fndecl, |
2ae7bada | 14387 | DECL_CONTEXT (fndecl)); |
8d08fdba MS |
14388 | } |
14389 | return void_type_node; | |
14390 | } | |
14391 | ||
f3400fe2 JM |
14392 | check_template_shadow (fndecl); |
14393 | ||
79065db2 | 14394 | DECL_DECLARED_INLINE_P (fndecl) = 1; |
faae18ab | 14395 | |
8926095f | 14396 | if (flag_default_inline) |
8d08fdba MS |
14397 | DECL_INLINE (fndecl) = 1; |
14398 | ||
36a117a5 MM |
14399 | /* We process method specializations in finish_struct_1. */ |
14400 | if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl)) | |
3ac3d9ea | 14401 | fndecl = push_template_decl (fndecl); |
a0a33927 | 14402 | |
8d08fdba MS |
14403 | if (! DECL_FRIEND_P (fndecl)) |
14404 | { | |
8d08fdba MS |
14405 | if (TREE_CHAIN (fndecl)) |
14406 | { | |
14407 | fndecl = copy_node (fndecl); | |
14408 | TREE_CHAIN (fndecl) = NULL_TREE; | |
14409 | } | |
271e6f02 | 14410 | grok_special_member_properties (fndecl); |
8d08fdba MS |
14411 | } |
14412 | ||
cd9f6678 | 14413 | cp_finish_decl (fndecl, NULL_TREE, NULL_TREE, 0); |
8d08fdba MS |
14414 | |
14415 | /* Make a place for the parms */ | |
14416 | pushlevel (0); | |
14417 | current_binding_level->parm_flag = 1; | |
68642fb6 | 14418 | |
8d08fdba MS |
14419 | DECL_IN_AGGR_P (fndecl) = 1; |
14420 | return fndecl; | |
14421 | } | |
14422 | ||
14423 | /* Go through the motions of finishing a function definition. | |
14424 | We don't compile this method until after the whole class has | |
14425 | been processed. | |
14426 | ||
14427 | FINISH_METHOD must return something that looks as though it | |
14428 | came from GROKFIELD (since we are defining a method, after all). | |
14429 | ||
14430 | This is called after parsing the body of the function definition. | |
14431 | STMTS is the chain of statements that makes up the function body. | |
14432 | ||
14433 | DECL is the ..._DECL that `start_method' provided. */ | |
14434 | ||
14435 | tree | |
14436 | finish_method (decl) | |
14437 | tree decl; | |
14438 | { | |
14439 | register tree fndecl = decl; | |
14440 | tree old_initial; | |
8d08fdba MS |
14441 | |
14442 | register tree link; | |
14443 | ||
a1774733 | 14444 | if (decl == void_type_node) |
8d08fdba MS |
14445 | return decl; |
14446 | ||
14447 | old_initial = DECL_INITIAL (fndecl); | |
14448 | ||
14449 | /* Undo the level for the parms (from start_method). | |
14450 | This is like poplevel, but it causes nothing to be | |
14451 | saved. Saving information here confuses symbol-table | |
14452 | output routines. Besides, this information will | |
14453 | be correctly output when this method is actually | |
14454 | compiled. */ | |
14455 | ||
14456 | /* Clear out the meanings of the local variables of this level; | |
14457 | also record in each decl which block it belongs to. */ | |
14458 | ||
14459 | for (link = current_binding_level->names; link; link = TREE_CHAIN (link)) | |
14460 | { | |
14461 | if (DECL_NAME (link) != NULL_TREE) | |
d8f8dca1 | 14462 | pop_binding (DECL_NAME (link), link); |
8d08fdba MS |
14463 | my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163); |
14464 | DECL_CONTEXT (link) = NULL_TREE; | |
14465 | } | |
14466 | ||
8d08fdba MS |
14467 | poplevel (0, 0, 0); |
14468 | ||
14469 | DECL_INITIAL (fndecl) = old_initial; | |
14470 | ||
14471 | /* We used to check if the context of FNDECL was different from | |
14472 | current_class_type as another way to get inside here. This didn't work | |
14473 | for String.cc in libg++. */ | |
14474 | if (DECL_FRIEND_P (fndecl)) | |
14475 | { | |
14476 | CLASSTYPE_INLINE_FRIENDS (current_class_type) | |
14477 | = tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type)); | |
14478 | decl = void_type_node; | |
14479 | } | |
14480 | ||
14481 | return decl; | |
14482 | } | |
14483 | \f | |
0154eaa8 MM |
14484 | |
14485 | /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that | |
14486 | we can lay it out later, when and if its type becomes complete. */ | |
8d08fdba MS |
14487 | |
14488 | void | |
0154eaa8 MM |
14489 | maybe_register_incomplete_var (var) |
14490 | tree var; | |
8d08fdba | 14491 | { |
0154eaa8 | 14492 | my_friendly_assert (TREE_CODE (var) == VAR_DECL, 20020406); |
8d08fdba | 14493 | |
0154eaa8 MM |
14494 | /* Keep track of variables with incomplete types. */ |
14495 | if (!processing_template_decl && TREE_TYPE (var) != error_mark_node | |
14496 | && DECL_EXTERNAL (var)) | |
70adf8a9 | 14497 | { |
0154eaa8 MM |
14498 | tree inner_type = TREE_TYPE (var); |
14499 | ||
14500 | while (TREE_CODE (inner_type) == ARRAY_TYPE) | |
14501 | inner_type = TREE_TYPE (inner_type); | |
14502 | inner_type = TYPE_MAIN_VARIANT (inner_type); | |
14503 | ||
14504 | if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type)) | |
14505 | /* RTTI TD entries are created while defining the type_info. */ | |
14506 | || (TYPE_LANG_SPECIFIC (inner_type) | |
14507 | && TYPE_BEING_DEFINED (inner_type))) | |
14508 | incomplete_vars = tree_cons (inner_type, var, incomplete_vars); | |
70adf8a9 | 14509 | } |
0154eaa8 | 14510 | } |
70adf8a9 | 14511 | |
0154eaa8 MM |
14512 | /* Called when a class type (given by TYPE) is defined. If there are |
14513 | any existing VAR_DECLs whose type hsa been completed by this | |
14514 | declaration, update them now. */ | |
70adf8a9 | 14515 | |
0154eaa8 MM |
14516 | void |
14517 | complete_vars (type) | |
14518 | tree type; | |
14519 | { | |
14520 | tree *list = &incomplete_vars; | |
14521 | ||
14522 | my_friendly_assert (CLASS_TYPE_P (type), 20020406); | |
14523 | while (*list) | |
14524 | { | |
14525 | if (same_type_p (type, TREE_PURPOSE (*list))) | |
70adf8a9 | 14526 | { |
0154eaa8 | 14527 | tree var = TREE_VALUE (*list); |
c740732f MM |
14528 | /* Complete the type of the variable. The VAR_DECL itself |
14529 | will be laid out in expand_expr. */ | |
14530 | complete_type (TREE_TYPE (var)); | |
0154eaa8 MM |
14531 | /* Remove this entry from the list. */ |
14532 | *list = TREE_CHAIN (*list); | |
f30432d7 MS |
14533 | } |
14534 | else | |
0154eaa8 | 14535 | list = &TREE_CHAIN (*list); |
f30432d7 | 14536 | } |
8d08fdba MS |
14537 | } |
14538 | ||
86f45d2c MM |
14539 | /* If DECL is of a type which needs a cleanup, build that cleanup |
14540 | here. */ | |
e92cc029 | 14541 | |
86f45d2c | 14542 | tree |
c88770e9 | 14543 | cxx_maybe_build_cleanup (decl) |
86f45d2c | 14544 | tree decl; |
8d08fdba MS |
14545 | { |
14546 | tree type = TREE_TYPE (decl); | |
86f45d2c | 14547 | |
834c6dff | 14548 | if (type != error_mark_node && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) |
8d08fdba | 14549 | { |
80048418 | 14550 | int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR; |
8d08fdba | 14551 | tree rval; |
8d08fdba | 14552 | |
8d08fdba MS |
14553 | if (TREE_CODE (type) == ARRAY_TYPE) |
14554 | rval = decl; | |
14555 | else | |
14556 | { | |
dffd7eb6 | 14557 | cxx_mark_addressable (decl); |
8d08fdba MS |
14558 | rval = build_unary_op (ADDR_EXPR, decl, 0); |
14559 | } | |
14560 | ||
14561 | /* Optimize for space over speed here. */ | |
14562 | if (! TYPE_USES_VIRTUAL_BASECLASSES (type) | |
14563 | || flag_expensive_optimizations) | |
14564 | flags |= LOOKUP_NONVIRTUAL; | |
14565 | ||
86f45d2c MM |
14566 | rval = build_delete (TREE_TYPE (rval), rval, |
14567 | sfk_complete_destructor, flags, 0); | |
8d08fdba MS |
14568 | |
14569 | if (TYPE_USES_VIRTUAL_BASECLASSES (type) | |
14570 | && ! TYPE_HAS_DESTRUCTOR (type)) | |
e1b3e07d | 14571 | rval = build_compound_expr (tree_cons (NULL_TREE, rval, |
051e6fd7 | 14572 | build_tree_list (NULL_TREE, build_vbase_delete (type, decl)))); |
8d08fdba | 14573 | |
8d08fdba MS |
14574 | return rval; |
14575 | } | |
6e4ae815 | 14576 | return NULL_TREE; |
8d08fdba MS |
14577 | } |
14578 | \f | |
558475f0 | 14579 | /* When a stmt has been parsed, this function is called. */ |
8d08fdba MS |
14580 | |
14581 | void | |
14582 | finish_stmt () | |
14583 | { | |
558475f0 MM |
14584 | /* Always assume this statement was not an expression statement. If |
14585 | it actually was an expression statement, its our callers | |
14586 | responsibility to fix this up. */ | |
14587 | last_expr_type = NULL_TREE; | |
8d08fdba MS |
14588 | } |
14589 | ||
3afb32a4 MM |
14590 | /* DECL was originally constructed as a non-static member function, |
14591 | but turned out to be static. Update it accordingly. */ | |
700f8a87 | 14592 | |
8857f91e | 14593 | void |
3afb32a4 MM |
14594 | revert_static_member_fn (decl) |
14595 | tree decl; | |
8d08fdba | 14596 | { |
700f8a87 | 14597 | tree tmp; |
3afb32a4 MM |
14598 | tree function = TREE_TYPE (decl); |
14599 | tree args = TYPE_ARG_TYPES (function); | |
8d08fdba | 14600 | |
89d684bb | 14601 | if (cp_type_quals (TREE_TYPE (TREE_VALUE (args))) |
91063b51 | 14602 | != TYPE_UNQUALIFIED) |
33bd39a2 | 14603 | error ("static member function `%#D' declared with type qualifiers", |
11306230 | 14604 | decl); |
f30432d7 | 14605 | |
700f8a87 MS |
14606 | args = TREE_CHAIN (args); |
14607 | tmp = build_function_type (TREE_TYPE (function), args); | |
89d684bb | 14608 | tmp = build_qualified_type (tmp, cp_type_quals (function)); |
f30432d7 | 14609 | tmp = build_exception_variant (tmp, |
8d08fdba | 14610 | TYPE_RAISES_EXCEPTIONS (function)); |
3afb32a4 MM |
14611 | TREE_TYPE (decl) = tmp; |
14612 | if (DECL_ARGUMENTS (decl)) | |
14613 | DECL_ARGUMENTS (decl) = TREE_CHAIN (DECL_ARGUMENTS (decl)); | |
14614 | DECL_STATIC_FUNCTION_P (decl) = 1; | |
8d08fdba | 14615 | } |
a4443a08 | 14616 | |
68642fb6 UD |
14617 | /* Initialize the variables used during compilation of a C++ |
14618 | function. */ | |
db5ae43f | 14619 | |
b03e38e1 NB |
14620 | void |
14621 | cxx_push_function_context (f) | |
99dccabc MM |
14622 | struct function *f; |
14623 | { | |
e2500fed GK |
14624 | struct language_function *p |
14625 | = ((struct language_function *) | |
14626 | ggc_alloc_cleared (sizeof (struct language_function))); | |
14627 | f->language = p; | |
db5ae43f | 14628 | |
b35d4555 MM |
14629 | /* It takes an explicit call to expand_body to generate RTL for a |
14630 | function. */ | |
14631 | expanding_p = 0; | |
f1dedc31 MM |
14632 | |
14633 | /* Whenever we start a new function, we destroy temporaries in the | |
14634 | usual way. */ | |
ae499cce | 14635 | current_stmt_tree ()->stmts_are_full_exprs_p = 1; |
db5ae43f MS |
14636 | } |
14637 | ||
a8f73d4b MM |
14638 | /* Free the language-specific parts of F, now that we've finished |
14639 | compiling the function. */ | |
db5ae43f | 14640 | |
b03e38e1 NB |
14641 | void |
14642 | cxx_pop_function_context (f) | |
99dccabc | 14643 | struct function *f; |
db5ae43f | 14644 | { |
99dccabc | 14645 | f->language = 0; |
db5ae43f | 14646 | } |
ebfc180f | 14647 | |
e2500fed GK |
14648 | /* Return which tree structure is used by T, or TS_CP_GENERIC if T is |
14649 | one of the language-independent trees. */ | |
4519c0a8 | 14650 | |
e2500fed GK |
14651 | enum cp_tree_node_structure_enum |
14652 | cp_tree_node_structure (t) | |
14653 | union lang_tree_node *t; | |
4519c0a8 | 14654 | { |
e2500fed | 14655 | switch (TREE_CODE (&t->generic)) |
4519c0a8 | 14656 | { |
e2500fed GK |
14657 | case DEFAULT_ARG: return TS_CP_IDENTIFIER; |
14658 | case IDENTIFIER_NODE: return TS_CP_IDENTIFIER; | |
14659 | case CPLUS_BINDING: return TS_CP_BINDING; | |
14660 | case OVERLOAD: return TS_CP_OVERLOAD; | |
14661 | case TEMPLATE_PARM_INDEX: return TS_CP_TPI; | |
14662 | case PTRMEM_CST: return TS_CP_PTRMEM; | |
14663 | case WRAPPER: return TS_CP_WRAPPER; | |
14664 | case SRCLOC: return TS_CP_SRCLOC; | |
14665 | default: return TS_CP_GENERIC; | |
4519c0a8 MM |
14666 | } |
14667 | } | |
5fd8e536 JM |
14668 | |
14669 | /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since | |
14670 | the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */ | |
14671 | ||
14672 | tree | |
14673 | identifier_global_value (t) | |
14674 | tree t; | |
14675 | { | |
14676 | return IDENTIFIER_GLOBAL_VALUE (t); | |
14677 | } | |
d363e7bf | 14678 | |
eaa7c03f JM |
14679 | /* Build the void_list_node (void_type_node having been created). */ |
14680 | tree | |
14681 | build_void_list_node () | |
14682 | { | |
14683 | tree t = build_tree_list (NULL_TREE, void_type_node); | |
14684 | TREE_PARMLIST (t) = 1; | |
14685 | return t; | |
14686 | } | |
14687 | ||
d363e7bf AJ |
14688 | static int |
14689 | cp_missing_noreturn_ok_p (decl) | |
14690 | tree decl; | |
14691 | { | |
14692 | /* A missing noreturn is ok for the `main' function. */ | |
92643fea | 14693 | return DECL_MAIN_P (decl); |
d363e7bf | 14694 | } |
e2500fed GK |
14695 | |
14696 | #include "gt-cp-decl.h" | |
14697 | #include "gtype-cp.h" |