]>
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" |
7ddedda4 | 44 | #include "../hash.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" |
8d08fdba | 51 | |
255512c1 | 52 | #ifndef BOOL_TYPE_SIZE |
3461fba7 | 53 | /* `bool' has size and alignment `1', on all platforms. */ |
d2e5ee5c | 54 | #define BOOL_TYPE_SIZE CHAR_TYPE_SIZE |
e1cd6e56 | 55 | #endif |
255512c1 | 56 | |
5cce22b6 | 57 | static tree grokparms PARAMS ((tree)); |
158991b7 | 58 | static const char *redeclaration_error_message PARAMS ((tree, tree)); |
8d08fdba | 59 | |
158991b7 | 60 | static void push_binding_level PARAMS ((struct binding_level *, int, |
49c249e1 | 61 | int)); |
158991b7 KG |
62 | static void pop_binding_level PARAMS ((void)); |
63 | static void suspend_binding_level PARAMS ((void)); | |
64 | static void resume_binding_level PARAMS ((struct binding_level *)); | |
65 | static struct binding_level *make_binding_level PARAMS ((void)); | |
66 | static void declare_namespace_level PARAMS ((void)); | |
6625cdb5 | 67 | static int decl_jump_unsafe PARAMS ((tree)); |
158991b7 KG |
68 | static void storedecls PARAMS ((tree)); |
69 | static void require_complete_types_for_parms PARAMS ((tree)); | |
596ea4e5 AS |
70 | static int ambi_op_p PARAMS ((enum tree_code)); |
71 | static int unary_op_p PARAMS ((enum tree_code)); | |
158991b7 KG |
72 | static tree store_bindings PARAMS ((tree, tree)); |
73 | static tree lookup_tag_reverse PARAMS ((tree, tree)); | |
74 | static tree obscure_complex_init PARAMS ((tree, tree)); | |
158991b7 | 75 | static tree lookup_name_real PARAMS ((tree, int, int, int)); |
8e4ce833 | 76 | static void push_local_name PARAMS ((tree)); |
158991b7 | 77 | static void warn_extern_redeclared_static PARAMS ((tree, tree)); |
8e4ce833 | 78 | static tree grok_reference_init PARAMS ((tree, tree, tree)); |
158991b7 | 79 | static tree grokfndecl PARAMS ((tree, tree, tree, tree, int, |
2c73f9f5 | 80 | enum overload_flags, tree, |
7a8f9fa9 | 81 | tree, int, int, int, int, int, int, tree)); |
158991b7 | 82 | static tree grokvardecl PARAMS ((tree, tree, RID_BIT_TYPE *, int, int, tree)); |
4782bd5b | 83 | static tree follow_tag_typedef PARAMS ((tree)); |
158991b7 | 84 | static tree lookup_tag PARAMS ((enum tree_code, tree, |
49c249e1 JM |
85 | struct binding_level *, int)); |
86 | static void set_identifier_type_value_with_scope | |
158991b7 | 87 | PARAMS ((tree, tree, struct binding_level *)); |
158991b7 | 88 | static void record_unknown_type PARAMS ((tree, const char *)); |
d52e4867 RS |
89 | static tree builtin_function_1 PARAMS ((const char *, tree, tree, int, |
90 | enum built_in_class, const char *)); | |
596ea4e5 | 91 | static tree build_library_fn_1 PARAMS ((tree, enum tree_code, tree)); |
158991b7 KG |
92 | static int member_function_or_else PARAMS ((tree, tree, enum overload_flags)); |
93 | static void bad_specifiers PARAMS ((tree, const char *, int, int, int, int, | |
49c249e1 | 94 | int)); |
158991b7 KG |
95 | static tree maybe_process_template_type_declaration PARAMS ((tree, int, struct binding_level*)); |
96 | static void check_for_uninitialized_const_var PARAMS ((tree)); | |
97 | static unsigned long typename_hash PARAMS ((hash_table_key)); | |
d6edb99e | 98 | static bool typename_compare PARAMS ((hash_table_key, hash_table_key)); |
158991b7 KG |
99 | static void push_binding PARAMS ((tree, tree, struct binding_level*)); |
100 | static int add_binding PARAMS ((tree, tree)); | |
101 | static void pop_binding PARAMS ((tree, tree)); | |
102 | static tree local_variable_p_walkfn PARAMS ((tree *, int *, void *)); | |
103 | static tree find_binding PARAMS ((tree, tree)); | |
104 | static tree select_decl PARAMS ((tree, int)); | |
105 | static int lookup_flags PARAMS ((int, int)); | |
106 | static tree qualify_lookup PARAMS ((tree, int)); | |
107 | static tree record_builtin_java_type PARAMS ((const char *, int)); | |
108 | static const char *tag_name PARAMS ((enum tag_types code)); | |
109 | static void find_class_binding_level PARAMS ((void)); | |
110 | static struct binding_level *innermost_nonclass_level PARAMS ((void)); | |
111 | static void warn_about_implicit_typename_lookup PARAMS ((tree, tree)); | |
112 | static int walk_namespaces_r PARAMS ((tree, walk_namespaces_fn, void *)); | |
113 | static int walk_globals_r PARAMS ((tree, void *)); | |
114 | static void add_decl_to_level PARAMS ((tree, struct binding_level *)); | |
115 | static tree make_label_decl PARAMS ((tree, int)); | |
6625cdb5 JM |
116 | static void use_label PARAMS ((tree)); |
117 | static void check_previous_goto_1 PARAMS ((tree, struct binding_level *, tree, | |
118 | const char *, int)); | |
119 | static void check_previous_goto PARAMS ((struct named_label_use_list *)); | |
120 | static void check_switch_goto PARAMS ((struct binding_level *)); | |
121 | static void check_previous_gotos PARAMS ((tree)); | |
122 | static void pop_label PARAMS ((tree, tree)); | |
158991b7 KG |
123 | static void pop_labels PARAMS ((tree)); |
124 | static void maybe_deduce_size_from_array_init PARAMS ((tree, tree)); | |
125 | static void layout_var_decl PARAMS ((tree)); | |
126 | static void maybe_commonize_var PARAMS ((tree)); | |
127 | static tree check_initializer PARAMS ((tree, tree)); | |
128 | static void make_rtl_for_nonlocal_decl PARAMS ((tree, tree, const char *)); | |
158991b7 | 129 | static void mark_binding_level PARAMS ((void *)); |
2f8edb39 | 130 | static void mark_named_label_lists PARAMS ((void *, void *)); |
158991b7 | 131 | static void mark_saved_scope PARAMS ((void *)); |
ae499cce | 132 | static void mark_lang_function PARAMS ((struct cp_language_function *)); |
158991b7 | 133 | static void save_function_data PARAMS ((tree)); |
f444e36b | 134 | static void check_function_type PARAMS ((tree, tree)); |
158991b7 | 135 | static void destroy_local_var PARAMS ((tree)); |
cdd2559c | 136 | static void begin_constructor_body PARAMS ((void)); |
158991b7 | 137 | static void finish_constructor_body PARAMS ((void)); |
cdd2559c | 138 | static void begin_destructor_body PARAMS ((void)); |
158991b7 KG |
139 | static void finish_destructor_body PARAMS ((void)); |
140 | static tree create_array_type_for_decl PARAMS ((tree, tree, tree)); | |
141 | static tree get_atexit_node PARAMS ((void)); | |
142 | static tree get_dso_handle_node PARAMS ((void)); | |
143 | static tree start_cleanup_fn PARAMS ((void)); | |
144 | static void end_cleanup_fn PARAMS ((void)); | |
0ba8a114 | 145 | static tree cp_make_fname_decl PARAMS ((tree, int)); |
d43829f9 | 146 | static void initialize_predefined_identifiers PARAMS ((void)); |
5362b086 | 147 | static tree check_special_function_return_type |
1f84ec23 | 148 | PARAMS ((special_function_kind, tree, tree)); |
596ea4e5 AS |
149 | static tree push_cp_library_fn PARAMS ((enum tree_code, tree)); |
150 | static tree build_cp_library_fn PARAMS ((tree, enum tree_code, tree)); | |
f444e36b | 151 | static void store_parm_decls PARAMS ((tree)); |
d363e7bf | 152 | static int cp_missing_noreturn_ok_p PARAMS ((tree)); |
8d08fdba | 153 | |
69ac77ce | 154 | #if defined (DEBUG_CP_BINDING_LEVELS) |
158991b7 | 155 | static void indent PARAMS ((void)); |
69ac77ce JL |
156 | #endif |
157 | ||
8d08fdba MS |
158 | /* Erroneous argument lists can use this *IFF* they do not modify it. */ |
159 | tree error_mark_list; | |
160 | ||
7f4edbcb | 161 | /* The following symbols are subsumed in the cp_global_trees array, and |
68642fb6 | 162 | listed here individually for documentation purposes. |
8d08fdba | 163 | |
7f4edbcb BS |
164 | C++ extensions |
165 | tree wchar_decl_node; | |
37c46b43 | 166 | |
7f4edbcb BS |
167 | tree vtable_entry_type; |
168 | tree delta_type_node; | |
7f4edbcb | 169 | tree __t_desc_type_node; |
db1147b2 | 170 | tree ti_desc_type_node; |
1f4cb92b | 171 | tree bltn_desc_type_node, ptr_desc_type_node; |
db1147b2 NS |
172 | tree ary_desc_type_node, func_desc_type_node, enum_desc_type_node; |
173 | tree class_desc_type_node, si_class_desc_type_node, vmi_class_desc_type_node; | |
387769ed | 174 | tree ptm_desc_type_node; |
db1147b2 | 175 | tree base_desc_type_node; |
8d08fdba | 176 | |
7f4edbcb BS |
177 | tree class_type_node, record_type_node, union_type_node, enum_type_node; |
178 | tree unknown_type_node; | |
8d08fdba | 179 | |
7f4edbcb | 180 | Array type `vtable_entry_type[]' |
8d08fdba | 181 | |
7f4edbcb BS |
182 | tree vtbl_type_node; |
183 | tree vtbl_ptr_type_node; | |
8d08fdba | 184 | |
2854d3c6 | 185 | Namespaces, |
8d08fdba | 186 | |
7f4edbcb | 187 | tree std_node; |
2854d3c6 | 188 | tree abi_node; |
8d08fdba | 189 | |
7f4edbcb BS |
190 | A FUNCTION_DECL which can call `abort'. Not necessarily the |
191 | one that the user will declare, but sufficient to be called | |
192 | by routines that want to abort the program. | |
8d08fdba | 193 | |
7f4edbcb | 194 | tree abort_fndecl; |
8d08fdba | 195 | |
7f4edbcb | 196 | The FUNCTION_DECL for the default `::operator delete'. |
2986ae00 | 197 | |
7f4edbcb | 198 | tree global_delete_fndecl; |
8d08fdba | 199 | |
7f4edbcb | 200 | Used by RTTI |
669ec2b4 JM |
201 | tree type_info_type_node, tinfo_decl_id, tinfo_decl_type; |
202 | tree tinfo_var_id; | |
203 | ||
7f4edbcb | 204 | */ |
8d08fdba | 205 | |
7f4edbcb | 206 | tree cp_global_trees[CPTI_MAX]; |
8d08fdba | 207 | |
2c73f9f5 | 208 | /* Indicates that there is a type value in some namespace, although |
7f4edbcb | 209 | that is not necessarily in scope at the moment. */ |
2c73f9f5 ML |
210 | |
211 | static tree global_type_node; | |
212 | ||
3e3f722c ML |
213 | /* Expect only namespace names now. */ |
214 | static int only_namespace_names; | |
215 | ||
6625cdb5 JM |
216 | /* Used only for jumps to as-yet undefined labels, since jumps to |
217 | defined labels can have their validity checked immediately. */ | |
218 | ||
219 | struct named_label_use_list | |
e349ee73 MS |
220 | { |
221 | struct binding_level *binding_level; | |
222 | tree names_in_scope; | |
223 | tree label_decl; | |
9c0758dd | 224 | const char *filename_o_goto; |
e349ee73 | 225 | int lineno_o_goto; |
6625cdb5 | 226 | struct named_label_use_list *next; |
e349ee73 MS |
227 | }; |
228 | ||
ed5511d9 | 229 | #define named_label_uses cp_function_chain->x_named_label_uses |
8d08fdba | 230 | |
8e4ce833 JJ |
231 | #define local_names cp_function_chain->x_local_names |
232 | ||
8d08fdba MS |
233 | /* A list of objects which have constructors or destructors |
234 | which reside in the global scope. The decl is stored in | |
235 | the TREE_VALUE slot and the initializer is stored | |
236 | in the TREE_PURPOSE slot. */ | |
237 | tree static_aggregates; | |
238 | ||
8d08fdba MS |
239 | /* -- end of C++ */ |
240 | ||
81b3411c | 241 | /* A node for the integer constants 2, and 3. */ |
d11ad92e | 242 | |
81b3411c | 243 | tree integer_two_node, integer_three_node; |
8d08fdba | 244 | |
8d08fdba | 245 | /* Similar, for last_function_parm_tags. */ |
9cd64686 | 246 | tree last_function_parms; |
8d08fdba | 247 | |
6625cdb5 JM |
248 | /* A list of all LABEL_DECLs in the function that have names. Here so |
249 | we can clear out their names' definitions at the end of the | |
250 | function, and so we can check the validity of jumps to these labels. */ | |
251 | ||
252 | struct named_label_list | |
253 | { | |
254 | struct binding_level *binding_level; | |
255 | tree names_in_scope; | |
256 | tree old_value; | |
257 | tree label_decl; | |
258 | tree bad_decls; | |
6625cdb5 | 259 | struct named_label_list *next; |
826840d9 RH |
260 | unsigned int in_try_scope : 1; |
261 | unsigned int in_catch_scope : 1; | |
6625cdb5 | 262 | }; |
8d08fdba | 263 | |
4519c0a8 | 264 | #define named_labels cp_function_chain->x_named_labels |
8d08fdba | 265 | |
b8458e3e JM |
266 | /* Nonzero means use the ISO C94 dialect of C. */ |
267 | ||
268 | int flag_isoc94; | |
269 | ||
83309c26 | 270 | /* Nonzero means use the ISO C99 dialect of C. */ |
68642fb6 | 271 | |
83309c26 | 272 | int flag_isoc99; |
68642fb6 | 273 | |
93e2382f JM |
274 | /* Nonzero means we are a hosted implementation for code shared with C. */ |
275 | ||
276 | int flag_hosted = 1; | |
277 | ||
278 | /* Nonzero means add default format_arg attributes for functions not | |
279 | in ISO C. */ | |
280 | ||
281 | int flag_noniso_default_format_attributes = 1; | |
282 | ||
8d08fdba MS |
283 | /* Nonzero if we want to conserve space in the .o files. We do this |
284 | by putting uninitialized data and runtime initialized data into | |
ddd5a7c1 | 285 | .common instead of .data at the expense of not flagging multiple |
8d08fdba MS |
286 | definitions. */ |
287 | extern int flag_conserve_space; | |
8d08fdba | 288 | \f |
51c184be | 289 | /* C and C++ flags are in decl2.c. */ |
8d08fdba | 290 | |
e92cc029 MS |
291 | /* A expression of value 0 with the same precision as a sizetype |
292 | node, but signed. */ | |
293 | tree signed_size_zero_node; | |
294 | ||
0c8feefe MM |
295 | /* The name of the anonymous namespace, throughout this translation |
296 | unit. */ | |
297 | tree anonymous_namespace_name; | |
298 | ||
b2244c65 MM |
299 | /* The number of function bodies which we are currently processing. |
300 | (Zero if we are at namespace scope, one inside the body of a | |
301 | function, two inside the body of a function in a local class, etc.) */ | |
302 | int function_depth; | |
e23bd218 IR |
303 | |
304 | /* States indicating how grokdeclarator() should handle declspecs marked | |
305 | with __attribute__((deprecated)). An object declared as | |
306 | __attribute__((deprecated)) suppresses warnings of uses of other | |
307 | deprecated items. */ | |
308 | ||
309 | enum deprecated_states { | |
310 | DEPRECATED_NORMAL, | |
311 | DEPRECATED_SUPPRESS | |
312 | }; | |
313 | ||
314 | static enum deprecated_states deprecated_state = DEPRECATED_NORMAL; | |
315 | ||
316 | /* Set by add_implicitly_declared_members() to keep those members from | |
317 | being flagged as deprecated or reported as using deprecated | |
318 | types. */ | |
319 | int adding_implicit_members = 0; | |
8d08fdba MS |
320 | \f |
321 | /* For each binding contour we allocate a binding_level structure | |
e92cc029 MS |
322 | which records the names defined in that contour. |
323 | Contours include: | |
324 | 0) the global one | |
325 | 1) one for each function definition, | |
326 | where internal declarations of the parameters appear. | |
327 | 2) one for each compound statement, | |
328 | to record its declarations. | |
329 | ||
330 | The current meaning of a name can be found by searching the levels | |
331 | from the current one out to the global one. | |
332 | ||
333 | Off to the side, may be the class_binding_level. This exists only | |
334 | to catch class-local declarations. It is otherwise nonexistent. | |
335 | ||
336 | Also there may be binding levels that catch cleanups that must be | |
d8f8dca1 MM |
337 | run when exceptions occur. Thus, to see whether a name is bound in |
338 | the current scope, it is not enough to look in the | |
339 | CURRENT_BINDING_LEVEL. You should use lookup_name_current_level | |
340 | instead. */ | |
8d08fdba MS |
341 | |
342 | /* Note that the information in the `names' component of the global contour | |
343 | is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */ | |
344 | ||
345 | struct binding_level | |
346 | { | |
347 | /* A chain of _DECL nodes for all variables, constants, functions, | |
e92cc029 | 348 | and typedef types. These are in the reverse of the order |
f181d4ae MM |
349 | supplied. There may be OVERLOADs on this list, too, but they |
350 | are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD. */ | |
8d08fdba MS |
351 | tree names; |
352 | ||
e92cc029 MS |
353 | /* A list of structure, union and enum definitions, for looking up |
354 | tag names. | |
355 | It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name, | |
356 | or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE, | |
357 | or ENUMERAL_TYPE node. | |
358 | ||
359 | C++: the TREE_VALUE nodes can be simple types for | |
360 | component_bindings. */ | |
8d08fdba MS |
361 | tree tags; |
362 | ||
2c73f9f5 ML |
363 | /* A list of USING_DECL nodes. */ |
364 | tree usings; | |
365 | ||
ea9635c7 ML |
366 | /* A list of used namespaces. PURPOSE is the namespace, |
367 | VALUE the common ancestor with this binding_level's namespace. */ | |
368 | tree using_directives; | |
369 | ||
f181d4ae MM |
370 | /* If this binding level is the binding level for a class, then |
371 | class_shadowed is a TREE_LIST. The TREE_PURPOSE of each node | |
76191fdd | 372 | is the name of an entity bound in the class. The TREE_TYPE is |
d8f8dca1 | 373 | the DECL bound by this name in the class. */ |
8d08fdba MS |
374 | tree class_shadowed; |
375 | ||
f181d4ae | 376 | /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and |
76191fdd NS |
377 | is used for all binding levels. In addition the TREE_VALUE is the |
378 | IDENTIFIER_TYPE_VALUE before we entered the class. */ | |
8d08fdba MS |
379 | tree type_shadowed; |
380 | ||
acef433b MM |
381 | /* A TREE_LIST. Each TREE_VALUE is the LABEL_DECL for a local |
382 | label in this scope. The TREE_PURPOSE is the previous value of | |
383 | the IDENTIFIER_LABEL VALUE. */ | |
384 | tree shadowed_labels; | |
385 | ||
8d08fdba MS |
386 | /* For each level (except not the global one), |
387 | a chain of BLOCK nodes for all the levels | |
388 | that were entered and exited one level down. */ | |
389 | tree blocks; | |
390 | ||
70adf8a9 JM |
391 | /* The _TYPE node for this level, if parm_flag == 2. */ |
392 | tree this_class; | |
393 | ||
8d08fdba MS |
394 | /* The binding level which this one is contained in (inherits from). */ |
395 | struct binding_level *level_chain; | |
396 | ||
8d6e462b | 397 | /* List of VAR_DECLS saved from a previous for statement. |
cab1f180 | 398 | These would be dead in ISO-conforming code, but might |
f181d4ae MM |
399 | be referenced in ARM-era code. These are stored in a |
400 | TREE_LIST; the TREE_VALUE is the actual declaration. */ | |
8d6e462b PB |
401 | tree dead_vars_from_for; |
402 | ||
8d08fdba | 403 | /* 1 for the level that holds the parameters of a function. |
74b846e0 MM |
404 | 2 for the level that holds a class declaration. */ |
405 | unsigned parm_flag : 2; | |
8d08fdba MS |
406 | |
407 | /* 1 means make a BLOCK for this level regardless of all else. | |
408 | 2 for temporary binding contours created by the compiler. */ | |
74b846e0 | 409 | unsigned keep : 2; |
8d08fdba MS |
410 | |
411 | /* Nonzero if this level "doesn't exist" for tags. */ | |
412 | unsigned tag_transparent : 1; | |
413 | ||
414 | /* Nonzero if this level can safely have additional | |
415 | cleanup-needing variables added to it. */ | |
416 | unsigned more_cleanups_ok : 1; | |
417 | unsigned have_cleanups : 1; | |
418 | ||
74b846e0 | 419 | /* Nonzero if this scope is for storing the decls for template |
5566b478 MS |
420 | parameters and generic decls; these decls will be discarded and |
421 | replaced with a TEMPLATE_DECL. */ | |
74b846e0 MM |
422 | unsigned template_parms_p : 1; |
423 | ||
5362b086 | 424 | /* Nonzero if this scope corresponds to the `<>' in a |
74b846e0 MM |
425 | `template <>' clause. Whenever this flag is set, |
426 | TEMPLATE_PARMS_P will be set as well. */ | |
427 | unsigned template_spec_p : 1; | |
8d08fdba | 428 | |
a9aedbc2 MS |
429 | /* This is set for a namespace binding level. */ |
430 | unsigned namespace_p : 1; | |
431 | ||
2ee887f2 | 432 | /* True if this level is that of a for-statement where we need to |
cab1f180 | 433 | worry about ambiguous (ARM or ISO) scope rules. */ |
8d6e462b PB |
434 | unsigned is_for_scope : 1; |
435 | ||
826840d9 RH |
436 | /* True if this level corresponds to a TRY block. Currently this |
437 | information is only available while building the tree structure. */ | |
438 | unsigned is_try_scope : 1; | |
227cf171 | 439 | |
826840d9 RH |
440 | /* True if this level corresponds to a CATCH block. Currently this |
441 | information is only available while building the tree structure. */ | |
442 | unsigned is_catch_scope : 1; | |
443 | ||
444 | /* Three bits left for this word. */ | |
8d08fdba MS |
445 | |
446 | #if defined(DEBUG_CP_BINDING_LEVELS) | |
447 | /* Binding depth at which this level began. */ | |
448 | unsigned binding_depth; | |
449 | #endif /* defined(DEBUG_CP_BINDING_LEVELS) */ | |
450 | }; | |
451 | ||
452 | #define NULL_BINDING_LEVEL ((struct binding_level *) NULL) | |
68642fb6 | 453 | |
8f032717 | 454 | /* The binding level currently in effect. */ |
8d08fdba | 455 | |
a8f73d4b | 456 | #define current_binding_level \ |
52a11cbf | 457 | (cfun && cp_function_chain->bindings \ |
d363e7bf | 458 | ? cp_function_chain->bindings \ |
a8f73d4b | 459 | : scope_chain->bindings) |
8d08fdba MS |
460 | |
461 | /* The binding level of the current class, if any. */ | |
462 | ||
9cd64686 | 463 | #define class_binding_level scope_chain->class_bindings |
8d08fdba | 464 | |
8d08fdba MS |
465 | /* A chain of binding_level structures awaiting reuse. */ |
466 | ||
467 | static struct binding_level *free_binding_level; | |
468 | ||
469 | /* The outermost binding level, for names of file scope. | |
470 | This is created when the compiler is started and exists | |
471 | through the entire run. */ | |
472 | ||
473 | static struct binding_level *global_binding_level; | |
474 | ||
8d08fdba MS |
475 | /* Nonzero means unconditionally make a BLOCK for the next level pushed. */ |
476 | ||
477 | static int keep_next_level_flag; | |
478 | ||
0154eaa8 MM |
479 | /* A TREE_LIST of VAR_DECLs. The TREE_PURPOSE is a RECORD_TYPE or |
480 | UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type. At the | |
481 | time the VAR_DECL was declared, the type was incomplete. */ | |
482 | ||
483 | static tree incomplete_vars; | |
484 | ||
8d08fdba MS |
485 | #if defined(DEBUG_CP_BINDING_LEVELS) |
486 | static int binding_depth = 0; | |
487 | static int is_class_level = 0; | |
488 | ||
489 | static void | |
490 | indent () | |
491 | { | |
492 | register unsigned i; | |
493 | ||
494 | for (i = 0; i < binding_depth*2; i++) | |
495 | putc (' ', stderr); | |
496 | } | |
497 | #endif /* defined(DEBUG_CP_BINDING_LEVELS) */ | |
498 | ||
158991b7 | 499 | static tree pushdecl_with_scope PARAMS ((tree, struct binding_level *)); |
8d08fdba MS |
500 | |
501 | static void | |
502 | push_binding_level (newlevel, tag_transparent, keep) | |
503 | struct binding_level *newlevel; | |
504 | int tag_transparent, keep; | |
505 | { | |
506 | /* Add this level to the front of the chain (stack) of levels that | |
507 | are active. */ | |
961192e1 | 508 | memset ((char*) newlevel, 0, sizeof (struct binding_level)); |
8f032717 | 509 | newlevel->level_chain = current_binding_level; |
8d08fdba MS |
510 | current_binding_level = newlevel; |
511 | newlevel->tag_transparent = tag_transparent; | |
512 | newlevel->more_cleanups_ok = 1; | |
227cf171 | 513 | |
8d08fdba MS |
514 | newlevel->keep = keep; |
515 | #if defined(DEBUG_CP_BINDING_LEVELS) | |
516 | newlevel->binding_depth = binding_depth; | |
517 | indent (); | |
518 | fprintf (stderr, "push %s level 0x%08x line %d\n", | |
519 | (is_class_level) ? "class" : "block", newlevel, lineno); | |
520 | is_class_level = 0; | |
521 | binding_depth++; | |
522 | #endif /* defined(DEBUG_CP_BINDING_LEVELS) */ | |
523 | } | |
524 | ||
8f032717 MM |
525 | /* Find the innermost enclosing class scope, and reset |
526 | CLASS_BINDING_LEVEL appropriately. */ | |
527 | ||
528 | static void | |
529 | find_class_binding_level () | |
530 | { | |
531 | struct binding_level *level = current_binding_level; | |
532 | ||
533 | while (level && level->parm_flag != 2) | |
534 | level = level->level_chain; | |
535 | if (level && level->parm_flag == 2) | |
536 | class_binding_level = level; | |
537 | else | |
538 | class_binding_level = 0; | |
539 | } | |
540 | ||
8d08fdba MS |
541 | static void |
542 | pop_binding_level () | |
543 | { | |
8d08fdba MS |
544 | if (global_binding_level) |
545 | { | |
2c73f9f5 | 546 | /* Cannot pop a level, if there are none left to pop. */ |
8d08fdba | 547 | if (current_binding_level == global_binding_level) |
a98facb0 | 548 | abort (); |
8d08fdba MS |
549 | } |
550 | /* Pop the current level, and free the structure for reuse. */ | |
551 | #if defined(DEBUG_CP_BINDING_LEVELS) | |
552 | binding_depth--; | |
553 | indent (); | |
554 | fprintf (stderr, "pop %s level 0x%08x line %d\n", | |
555 | (is_class_level) ? "class" : "block", | |
556 | current_binding_level, lineno); | |
557 | if (is_class_level != (current_binding_level == class_binding_level)) | |
9e9ff709 MS |
558 | { |
559 | indent (); | |
560 | fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n"); | |
561 | } | |
8d08fdba MS |
562 | is_class_level = 0; |
563 | #endif /* defined(DEBUG_CP_BINDING_LEVELS) */ | |
564 | { | |
565 | register struct binding_level *level = current_binding_level; | |
566 | current_binding_level = current_binding_level->level_chain; | |
567 | level->level_chain = free_binding_level; | |
568 | #if 0 /* defined(DEBUG_CP_BINDING_LEVELS) */ | |
569 | if (level->binding_depth != binding_depth) | |
570 | abort (); | |
571 | #endif /* defined(DEBUG_CP_BINDING_LEVELS) */ | |
8f032717 MM |
572 | free_binding_level = level; |
573 | find_class_binding_level (); | |
8d08fdba MS |
574 | } |
575 | } | |
a9aedbc2 MS |
576 | |
577 | static void | |
578 | suspend_binding_level () | |
579 | { | |
580 | if (class_binding_level) | |
581 | current_binding_level = class_binding_level; | |
582 | ||
583 | if (global_binding_level) | |
584 | { | |
2c73f9f5 | 585 | /* Cannot suspend a level, if there are none left to suspend. */ |
a9aedbc2 | 586 | if (current_binding_level == global_binding_level) |
a98facb0 | 587 | abort (); |
a9aedbc2 MS |
588 | } |
589 | /* Suspend the current level. */ | |
590 | #if defined(DEBUG_CP_BINDING_LEVELS) | |
591 | binding_depth--; | |
592 | indent (); | |
593 | fprintf (stderr, "suspend %s level 0x%08x line %d\n", | |
594 | (is_class_level) ? "class" : "block", | |
595 | current_binding_level, lineno); | |
596 | if (is_class_level != (current_binding_level == class_binding_level)) | |
9e9ff709 MS |
597 | { |
598 | indent (); | |
599 | fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n"); | |
600 | } | |
a9aedbc2 MS |
601 | is_class_level = 0; |
602 | #endif /* defined(DEBUG_CP_BINDING_LEVELS) */ | |
8f032717 MM |
603 | current_binding_level = current_binding_level->level_chain; |
604 | find_class_binding_level (); | |
a9aedbc2 MS |
605 | } |
606 | ||
824b9a4c | 607 | static void |
a9aedbc2 MS |
608 | resume_binding_level (b) |
609 | struct binding_level *b; | |
610 | { | |
2c73f9f5 ML |
611 | /* Resuming binding levels is meant only for namespaces, |
612 | and those cannot nest into classes. */ | |
613 | my_friendly_assert(!class_binding_level, 386); | |
614 | /* Also, resuming a non-directly nested namespace is a no-no. */ | |
615 | my_friendly_assert(b->level_chain == current_binding_level, 386); | |
a9aedbc2 MS |
616 | current_binding_level = b; |
617 | #if defined(DEBUG_CP_BINDING_LEVELS) | |
618 | b->binding_depth = binding_depth; | |
619 | indent (); | |
620 | fprintf (stderr, "resume %s level 0x%08x line %d\n", | |
621 | (is_class_level) ? "class" : "block", b, lineno); | |
622 | is_class_level = 0; | |
623 | binding_depth++; | |
624 | #endif /* defined(DEBUG_CP_BINDING_LEVELS) */ | |
625 | } | |
8d08fdba | 626 | \f |
cffa8729 MS |
627 | /* Create a new `struct binding_level'. */ |
628 | ||
629 | static | |
630 | struct binding_level * | |
631 | make_binding_level () | |
632 | { | |
633 | /* NOSTRICT */ | |
634 | return (struct binding_level *) xmalloc (sizeof (struct binding_level)); | |
635 | } | |
636 | ||
8d08fdba MS |
637 | /* Nonzero if we are currently in the global binding level. */ |
638 | ||
639 | int | |
640 | global_bindings_p () | |
641 | { | |
642 | return current_binding_level == global_binding_level; | |
643 | } | |
644 | ||
8f032717 MM |
645 | /* Return the innermost binding level that is not for a class scope. */ |
646 | ||
647 | static struct binding_level * | |
648 | innermost_nonclass_level () | |
649 | { | |
650 | struct binding_level *b; | |
651 | ||
652 | b = current_binding_level; | |
653 | while (b->parm_flag == 2) | |
654 | b = b->level_chain; | |
655 | ||
656 | return b; | |
657 | } | |
658 | ||
a9aedbc2 MS |
659 | /* Nonzero if we are currently in a toplevel binding level. This |
660 | means either the global binding level or a namespace in a toplevel | |
8f032717 | 661 | binding level. Since there are no non-toplevel namespace levels, |
74b846e0 MM |
662 | this really means any namespace or template parameter level. We |
663 | also include a class whose context is toplevel. */ | |
a9aedbc2 MS |
664 | |
665 | int | |
666 | toplevel_bindings_p () | |
667 | { | |
8f032717 MM |
668 | struct binding_level *b = innermost_nonclass_level (); |
669 | ||
74b846e0 | 670 | return b->namespace_p || b->template_parms_p; |
a9aedbc2 MS |
671 | } |
672 | ||
8f032717 MM |
673 | /* Nonzero if this is a namespace scope, or if we are defining a class |
674 | which is itself at namespace scope, or whose enclosing class is | |
675 | such a class, etc. */ | |
a9aedbc2 | 676 | |
7bdbfa05 | 677 | int |
a9aedbc2 MS |
678 | namespace_bindings_p () |
679 | { | |
8f032717 MM |
680 | struct binding_level *b = innermost_nonclass_level (); |
681 | ||
682 | return b->namespace_p; | |
a9aedbc2 MS |
683 | } |
684 | ||
f1dedc31 MM |
685 | /* If KEEP is non-zero, make a BLOCK node for the next binding level, |
686 | unconditionally. Otherwise, use the normal logic to decide whether | |
687 | or not to create a BLOCK. */ | |
688 | ||
8d08fdba | 689 | void |
f1dedc31 MM |
690 | keep_next_level (keep) |
691 | int keep; | |
8d08fdba | 692 | { |
f1dedc31 | 693 | keep_next_level_flag = keep; |
8d08fdba MS |
694 | } |
695 | ||
696 | /* Nonzero if the current level needs to have a BLOCK made. */ | |
697 | ||
698 | int | |
699 | kept_level_p () | |
700 | { | |
701 | return (current_binding_level->blocks != NULL_TREE | |
702 | || current_binding_level->keep | |
703 | || current_binding_level->names != NULL_TREE | |
704 | || (current_binding_level->tags != NULL_TREE | |
705 | && !current_binding_level->tag_transparent)); | |
706 | } | |
707 | ||
824b9a4c | 708 | static void |
a9aedbc2 MS |
709 | declare_namespace_level () |
710 | { | |
711 | current_binding_level->namespace_p = 1; | |
712 | } | |
713 | ||
74b846e0 MM |
714 | /* Returns non-zero if this scope was created to store template |
715 | parameters. */ | |
716 | ||
8d08fdba | 717 | int |
74b846e0 | 718 | template_parm_scope_p () |
8d08fdba | 719 | { |
74b846e0 MM |
720 | return current_binding_level->template_parms_p; |
721 | } | |
722 | ||
723 | /* Returns the kind of template specialization we are currently | |
724 | processing, given that it's declaration contained N_CLASS_SCOPES | |
725 | explicit scope qualifications. */ | |
726 | ||
727 | tmpl_spec_kind | |
728 | current_tmpl_spec_kind (n_class_scopes) | |
729 | int n_class_scopes; | |
730 | { | |
731 | int n_template_parm_scopes = 0; | |
732 | int seen_specialization_p = 0; | |
733 | int innermost_specialization_p = 0; | |
734 | struct binding_level *b; | |
735 | ||
736 | /* Scan through the template parameter scopes. */ | |
737 | for (b = current_binding_level; b->template_parms_p; b = b->level_chain) | |
738 | { | |
739 | /* If we see a specialization scope inside a parameter scope, | |
740 | then something is wrong. That corresponds to a declaration | |
741 | like: | |
742 | ||
743 | template <class T> template <> ... | |
744 | ||
745 | which is always illegal since [temp.expl.spec] forbids the | |
746 | specialization of a class member template if the enclosing | |
747 | class templates are not explicitly specialized as well. */ | |
748 | if (b->template_spec_p) | |
749 | { | |
750 | if (n_template_parm_scopes == 0) | |
751 | innermost_specialization_p = 1; | |
752 | else | |
753 | seen_specialization_p = 1; | |
754 | } | |
755 | else if (seen_specialization_p == 1) | |
756 | return tsk_invalid_member_spec; | |
757 | ||
758 | ++n_template_parm_scopes; | |
759 | } | |
760 | ||
761 | /* Handle explicit instantiations. */ | |
762 | if (processing_explicit_instantiation) | |
763 | { | |
764 | if (n_template_parm_scopes != 0) | |
765 | /* We've seen a template parameter list during an explicit | |
766 | instantiation. For example: | |
767 | ||
768 | template <class T> template void f(int); | |
769 | ||
770 | This is erroneous. */ | |
771 | return tsk_invalid_expl_inst; | |
772 | else | |
773 | return tsk_expl_inst; | |
774 | } | |
775 | ||
776 | if (n_template_parm_scopes < n_class_scopes) | |
777 | /* We've not seen enough template headers to match all the | |
778 | specialized classes present. For example: | |
779 | ||
780 | template <class T> void R<T>::S<T>::f(int); | |
781 | ||
782 | This is illegal; there needs to be one set of template | |
783 | parameters for each class. */ | |
784 | return tsk_insufficient_parms; | |
785 | else if (n_template_parm_scopes == n_class_scopes) | |
786 | /* We're processing a non-template declaration (even though it may | |
787 | be a member of a template class.) For example: | |
788 | ||
789 | template <class T> void S<T>::f(int); | |
790 | ||
791 | The `class T' maches the `S<T>', leaving no template headers | |
792 | corresponding to the `f'. */ | |
793 | return tsk_none; | |
794 | else if (n_template_parm_scopes > n_class_scopes + 1) | |
795 | /* We've got too many template headers. For example: | |
796 | ||
797 | template <> template <class T> void f (T); | |
798 | ||
799 | There need to be more enclosing classes. */ | |
800 | return tsk_excessive_parms; | |
801 | else | |
802 | /* This must be a template. It's of the form: | |
803 | ||
804 | template <class T> template <class U> void S<T>::f(U); | |
805 | ||
806 | This is a specialization if the innermost level was a | |
807 | specialization; otherwise it's just a definition of the | |
808 | template. */ | |
809 | return innermost_specialization_p ? tsk_expl_spec : tsk_template; | |
8d08fdba MS |
810 | } |
811 | ||
812 | void | |
813 | set_class_shadows (shadows) | |
814 | tree shadows; | |
815 | { | |
816 | class_binding_level->class_shadowed = shadows; | |
817 | } | |
818 | ||
819 | /* Enter a new binding level. | |
820 | If TAG_TRANSPARENT is nonzero, do so only for the name space of variables, | |
821 | not for that of tags. */ | |
822 | ||
823 | void | |
824 | pushlevel (tag_transparent) | |
825 | int tag_transparent; | |
826 | { | |
b35d4555 | 827 | struct binding_level *newlevel; |
8d08fdba | 828 | |
01d939e8 | 829 | if (cfun && !doing_semantic_analysis_p ()) |
b35d4555 | 830 | return; |
8d08fdba | 831 | |
b35d4555 | 832 | /* Reuse or create a struct for this binding level. */ |
8d08fdba MS |
833 | #if defined(DEBUG_CP_BINDING_LEVELS) |
834 | if (0) | |
835 | #else /* !defined(DEBUG_CP_BINDING_LEVELS) */ | |
836 | if (free_binding_level) | |
837 | #endif /* !defined(DEBUG_CP_BINDING_LEVELS) */ | |
838 | { | |
839 | newlevel = free_binding_level; | |
840 | free_binding_level = free_binding_level->level_chain; | |
841 | } | |
842 | else | |
acef433b | 843 | newlevel = make_binding_level (); |
cffa8729 | 844 | |
8d08fdba | 845 | push_binding_level (newlevel, tag_transparent, keep_next_level_flag); |
8d08fdba MS |
846 | keep_next_level_flag = 0; |
847 | } | |
848 | ||
bd0d5d4a JM |
849 | /* We're defining an object of type TYPE. If it needs a cleanup, but |
850 | we're not allowed to add any more objects with cleanups to the current | |
851 | scope, create a new binding level. */ | |
852 | ||
853 | void | |
854 | maybe_push_cleanup_level (type) | |
855 | tree type; | |
856 | { | |
857 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) | |
858 | && current_binding_level->more_cleanups_ok == 0) | |
859 | { | |
860 | keep_next_level (2); | |
861 | pushlevel (1); | |
862 | clear_last_expr (); | |
863 | add_scope_stmt (/*begin_p=*/1, /*partial_p=*/1); | |
864 | } | |
865 | } | |
866 | ||
74b846e0 MM |
867 | /* Enter a new scope. The KIND indicates what kind of scope is being |
868 | created. */ | |
869 | ||
870 | void | |
871 | begin_scope (sk) | |
872 | scope_kind sk; | |
873 | { | |
874 | pushlevel (0); | |
875 | ||
876 | switch (sk) | |
877 | { | |
878 | case sk_template_spec: | |
879 | current_binding_level->template_spec_p = 1; | |
880 | /* Fall through. */ | |
881 | ||
882 | case sk_template_parms: | |
883 | current_binding_level->template_parms_p = 1; | |
884 | break; | |
885 | ||
886 | default: | |
a98facb0 | 887 | abort (); |
74b846e0 MM |
888 | } |
889 | } | |
890 | ||
891 | /* Exit the current scope. */ | |
892 | ||
893 | void | |
894 | finish_scope () | |
895 | { | |
896 | poplevel (0, 0, 0); | |
897 | } | |
898 | ||
5566b478 | 899 | void |
8d6e462b PB |
900 | note_level_for_for () |
901 | { | |
902 | current_binding_level->is_for_scope = 1; | |
903 | } | |
904 | ||
6625cdb5 JM |
905 | /* Record that the current binding level represents a try block. */ |
906 | ||
907 | void | |
826840d9 | 908 | note_level_for_try () |
6625cdb5 | 909 | { |
826840d9 RH |
910 | current_binding_level->is_try_scope = 1; |
911 | } | |
912 | ||
913 | /* Record that the current binding level represents a catch block. */ | |
914 | ||
915 | void | |
916 | note_level_for_catch () | |
917 | { | |
918 | current_binding_level->is_catch_scope = 1; | |
6625cdb5 JM |
919 | } |
920 | ||
f181d4ae MM |
921 | /* For a binding between a name and an entity at a block scope, |
922 | this is the `struct binding_level' for the block. */ | |
923 | #define BINDING_LEVEL(NODE) \ | |
75ac8dec | 924 | (((struct tree_binding*)(NODE))->scope.level) |
f181d4ae | 925 | |
4890c2f4 MM |
926 | /* A free list of CPLUS_BINDING nodes, connected by their |
927 | TREE_CHAINs. */ | |
928 | ||
929 | static tree free_bindings; | |
930 | ||
f181d4ae MM |
931 | /* Make DECL the innermost binding for ID. The LEVEL is the binding |
932 | level at which this declaration is being bound. */ | |
933 | ||
934 | static void | |
935 | push_binding (id, decl, level) | |
936 | tree id; | |
937 | tree decl; | |
938 | struct binding_level* level; | |
939 | { | |
940 | tree binding; | |
941 | ||
4890c2f4 MM |
942 | if (free_bindings) |
943 | { | |
944 | binding = free_bindings; | |
945 | free_bindings = TREE_CHAIN (binding); | |
946 | } | |
947 | else | |
948 | binding = make_node (CPLUS_BINDING); | |
f181d4ae MM |
949 | |
950 | /* Now, fill in the binding information. */ | |
951 | BINDING_VALUE (binding) = decl; | |
d8f8dca1 | 952 | BINDING_TYPE (binding) = NULL_TREE; |
f181d4ae | 953 | BINDING_LEVEL (binding) = level; |
8f032717 | 954 | INHERITED_VALUE_BINDING_P (binding) = 0; |
f181d4ae | 955 | LOCAL_BINDING_P (binding) = (level != class_binding_level); |
87e3dbc9 | 956 | BINDING_HAS_LEVEL_P (binding) = 1; |
f181d4ae | 957 | |
c45df9c1 | 958 | /* And put it on the front of the list of bindings for ID. */ |
f181d4ae MM |
959 | TREE_CHAIN (binding) = IDENTIFIER_BINDING (id); |
960 | IDENTIFIER_BINDING (id) = binding; | |
961 | } | |
962 | ||
d8f8dca1 MM |
963 | /* ID is already bound in the current scope. But, DECL is an |
964 | additional binding for ID in the same scope. This is the `struct | |
965 | stat' hack whereby a non-typedef class-name or enum-name can be | |
966 | bound at the same level as some other kind of entity. It's the | |
967 | responsibility of the caller to check that inserting this name is | |
8f032717 MM |
968 | legal here. Returns nonzero if the new binding was successful. */ |
969 | static int | |
d8f8dca1 MM |
970 | add_binding (id, decl) |
971 | tree id; | |
972 | tree decl; | |
973 | { | |
974 | tree binding = IDENTIFIER_BINDING (id); | |
8f032717 | 975 | int ok = 1; |
d8f8dca1 MM |
976 | |
977 | if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl)) | |
978 | /* The new name is the type name. */ | |
979 | BINDING_TYPE (binding) = decl; | |
8f032717 MM |
980 | else if (!BINDING_VALUE (binding)) |
981 | /* This situation arises when push_class_level_binding moves an | |
982 | inherited type-binding out of the way to make room for a new | |
983 | value binding. */ | |
984 | BINDING_VALUE (binding) = decl; | |
985 | else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL | |
986 | && DECL_ARTIFICIAL (BINDING_VALUE (binding))) | |
987 | { | |
988 | /* The old binding was a type name. It was placed in | |
989 | BINDING_VALUE because it was thought, at the point it was | |
990 | declared, to be the only entity with such a name. Move the | |
991 | type name into the type slot; it is now hidden by the new | |
992 | binding. */ | |
d8f8dca1 MM |
993 | BINDING_TYPE (binding) = BINDING_VALUE (binding); |
994 | BINDING_VALUE (binding) = decl; | |
8f032717 | 995 | INHERITED_VALUE_BINDING_P (binding) = 0; |
d8f8dca1 | 996 | } |
263505af MM |
997 | else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL |
998 | && TREE_CODE (decl) == TYPE_DECL | |
999 | && DECL_NAME (decl) == DECL_NAME (BINDING_VALUE (binding)) | |
9aaceb4b NS |
1000 | && (same_type_p (TREE_TYPE (decl), |
1001 | TREE_TYPE (BINDING_VALUE (binding))) | |
1002 | /* If either type involves template parameters, we must | |
1003 | wait until instantiation. */ | |
1004 | || uses_template_parms (TREE_TYPE (decl)) | |
1005 | || uses_template_parms (TREE_TYPE (BINDING_VALUE (binding))))) | |
263505af MM |
1006 | /* We have two typedef-names, both naming the same type to have |
1007 | the same name. This is OK because of: | |
1008 | ||
1009 | [dcl.typedef] | |
1010 | ||
1011 | In a given scope, a typedef specifier can be used to redefine | |
1012 | the name of any type declared in that scope to refer to the | |
1013 | type to which it already refers. */ | |
1014 | ok = 0; | |
7b176381 MM |
1015 | /* There can be two block-scope declarations of the same variable, |
1016 | so long as they are `extern' declarations. */ | |
1017 | else if (TREE_CODE (decl) == VAR_DECL | |
1018 | && TREE_CODE (BINDING_VALUE (binding)) == VAR_DECL | |
1019 | && DECL_EXTERNAL (decl) | |
1020 | && DECL_EXTERNAL (BINDING_VALUE (binding))) | |
1021 | { | |
1022 | duplicate_decls (decl, BINDING_VALUE (binding)); | |
1023 | ok = 0; | |
1024 | } | |
8f032717 MM |
1025 | else |
1026 | { | |
33bd39a2 | 1027 | error ("declaration of `%#D'", decl); |
8f032717 MM |
1028 | cp_error_at ("conflicts with previous declaration `%#D'", |
1029 | BINDING_VALUE (binding)); | |
1030 | ok = 0; | |
1031 | } | |
1032 | ||
1033 | return ok; | |
d8f8dca1 MM |
1034 | } |
1035 | ||
cfe507be MM |
1036 | /* Add DECL to the list of things declared in B. */ |
1037 | ||
1038 | static void | |
1039 | add_decl_to_level (decl, b) | |
1040 | tree decl; | |
1041 | struct binding_level *b; | |
1042 | { | |
cfe507be MM |
1043 | /* We build up the list in reverse order, and reverse it later if |
1044 | necessary. */ | |
1045 | TREE_CHAIN (decl) = b->names; | |
1046 | b->names = decl; | |
1047 | } | |
1048 | ||
1049 | /* Bind DECL to ID in the current_binding_level, assumed to be a local | |
1050 | binding level. If PUSH_USING is set in FLAGS, we know that DECL | |
1051 | doesn't really belong to this binding level, that it got here | |
1052 | through a using-declaration. */ | |
f181d4ae MM |
1053 | |
1054 | void | |
0034cf72 | 1055 | push_local_binding (id, decl, flags) |
f181d4ae MM |
1056 | tree id; |
1057 | tree decl; | |
0034cf72 | 1058 | int flags; |
f181d4ae | 1059 | { |
8f032717 MM |
1060 | struct binding_level *b; |
1061 | ||
1062 | /* Skip over any local classes. This makes sense if we call | |
1063 | push_local_binding with a friend decl of a local class. */ | |
1064 | b = current_binding_level; | |
1065 | while (b->parm_flag == 2) | |
1066 | b = b->level_chain; | |
f181d4ae | 1067 | |
d8f8dca1 | 1068 | if (lookup_name_current_level (id)) |
8f032717 MM |
1069 | { |
1070 | /* Supplement the existing binding. */ | |
1071 | if (!add_binding (id, decl)) | |
1072 | /* It didn't work. Something else must be bound at this | |
1073 | level. Do not add DECL to the list of things to pop | |
1074 | later. */ | |
1075 | return; | |
1076 | } | |
d8f8dca1 MM |
1077 | else |
1078 | /* Create a new binding. */ | |
8f032717 | 1079 | push_binding (id, decl, b); |
f181d4ae | 1080 | |
0034cf72 | 1081 | if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING)) |
a06d48ef JM |
1082 | /* We must put the OVERLOAD into a TREE_LIST since the |
1083 | TREE_CHAIN of an OVERLOAD is already used. Similarly for | |
1084 | decls that got here through a using-declaration. */ | |
1085 | decl = build_tree_list (NULL_TREE, decl); | |
1086 | ||
f181d4ae MM |
1087 | /* And put DECL on the list of things declared by the current |
1088 | binding level. */ | |
cfe507be | 1089 | add_decl_to_level (decl, b); |
f181d4ae MM |
1090 | } |
1091 | ||
8f032717 MM |
1092 | /* Bind DECL to ID in the class_binding_level. Returns nonzero if the |
1093 | binding was successful. */ | |
f181d4ae | 1094 | |
8f032717 | 1095 | int |
f181d4ae MM |
1096 | push_class_binding (id, decl) |
1097 | tree id; | |
1098 | tree decl; | |
1099 | { | |
8f032717 MM |
1100 | int result = 1; |
1101 | tree binding = IDENTIFIER_BINDING (id); | |
1102 | tree context; | |
1103 | ||
1104 | /* Note that we declared this value so that we can issue an error if | |
1105 | this an illegal redeclaration of a name already used for some | |
1106 | other purpose. */ | |
1107 | note_name_declared_in_class (id, decl); | |
1108 | ||
1109 | if (binding && BINDING_LEVEL (binding) == class_binding_level) | |
d8f8dca1 | 1110 | /* Supplement the existing binding. */ |
8f032717 | 1111 | result = add_binding (id, decl); |
d8f8dca1 MM |
1112 | else |
1113 | /* Create a new binding. */ | |
1114 | push_binding (id, decl, class_binding_level); | |
1115 | ||
1116 | /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the | |
1117 | class-level declaration. Note that we do not use DECL here | |
1118 | because of the possibility of the `struct stat' hack; if DECL is | |
1119 | a class-name or enum-name we might prefer a field-name, or some | |
1120 | such. */ | |
1121 | IDENTIFIER_CLASS_VALUE (id) = BINDING_VALUE (IDENTIFIER_BINDING (id)); | |
8f032717 MM |
1122 | |
1123 | /* If this is a binding from a base class, mark it as such. */ | |
1124 | binding = IDENTIFIER_BINDING (id); | |
1125 | if (BINDING_VALUE (binding) == decl && TREE_CODE (decl) != TREE_LIST) | |
1126 | { | |
280f9385 MM |
1127 | /* Any implicit typename must be from a base-class. The |
1128 | context for an implicit typename declaration is always | |
1129 | the derived class in which the lookup was done, so the checks | |
1130 | based on the context of DECL below will not trigger. */ | |
83233dca | 1131 | if (IMPLICIT_TYPENAME_TYPE_DECL_P (decl)) |
280f9385 | 1132 | INHERITED_VALUE_BINDING_P (binding) = 1; |
8f032717 MM |
1133 | else |
1134 | { | |
280f9385 | 1135 | if (TREE_CODE (decl) == OVERLOAD) |
4f1c5b7d | 1136 | context = CP_DECL_CONTEXT (OVL_CURRENT (decl)); |
280f9385 MM |
1137 | else |
1138 | { | |
2f939d94 | 1139 | my_friendly_assert (DECL_P (decl), 0); |
55de1b66 | 1140 | context = context_for_name_lookup (decl); |
280f9385 | 1141 | } |
8f032717 | 1142 | |
280f9385 MM |
1143 | if (is_properly_derived_from (current_class_type, context)) |
1144 | INHERITED_VALUE_BINDING_P (binding) = 1; | |
1145 | else | |
1146 | INHERITED_VALUE_BINDING_P (binding) = 0; | |
1147 | } | |
8f032717 MM |
1148 | } |
1149 | else if (BINDING_VALUE (binding) == decl) | |
1150 | /* We only encounter a TREE_LIST when push_class_decls detects an | |
1151 | ambiguity. Such an ambiguity can be overridden by a definition | |
1152 | in this class. */ | |
1153 | INHERITED_VALUE_BINDING_P (binding) = 1; | |
1154 | ||
1155 | return result; | |
f181d4ae MM |
1156 | } |
1157 | ||
d8f8dca1 MM |
1158 | /* Remove the binding for DECL which should be the innermost binding |
1159 | for ID. */ | |
f181d4ae | 1160 | |
68642fb6 UD |
1161 | static void |
1162 | pop_binding (id, decl) | |
f181d4ae | 1163 | tree id; |
d8f8dca1 | 1164 | tree decl; |
f181d4ae MM |
1165 | { |
1166 | tree binding; | |
68642fb6 | 1167 | |
f181d4ae MM |
1168 | if (id == NULL_TREE) |
1169 | /* It's easiest to write the loops that call this function without | |
1170 | checking whether or not the entities involved have names. We | |
1171 | get here for such an entity. */ | |
1172 | return; | |
1173 | ||
d8f8dca1 | 1174 | /* Get the innermost binding for ID. */ |
f181d4ae | 1175 | binding = IDENTIFIER_BINDING (id); |
f181d4ae | 1176 | |
d8f8dca1 MM |
1177 | /* The name should be bound. */ |
1178 | my_friendly_assert (binding != NULL_TREE, 0); | |
1179 | ||
1180 | /* The DECL will be either the ordinary binding or the type | |
1181 | binding for this identifier. Remove that binding. */ | |
1182 | if (BINDING_VALUE (binding) == decl) | |
1183 | BINDING_VALUE (binding) = NULL_TREE; | |
1184 | else if (BINDING_TYPE (binding) == decl) | |
1185 | BINDING_TYPE (binding) = NULL_TREE; | |
1186 | else | |
a98facb0 | 1187 | abort (); |
d8f8dca1 MM |
1188 | |
1189 | if (!BINDING_VALUE (binding) && !BINDING_TYPE (binding)) | |
4890c2f4 MM |
1190 | { |
1191 | /* We're completely done with the innermost binding for this | |
1192 | identifier. Unhook it from the list of bindings. */ | |
1193 | IDENTIFIER_BINDING (id) = TREE_CHAIN (binding); | |
1194 | ||
1195 | /* Add it to the free list. */ | |
1196 | TREE_CHAIN (binding) = free_bindings; | |
1197 | free_bindings = binding; | |
1198 | ||
1199 | /* Clear the BINDING_LEVEL so the garbage collector doesn't walk | |
1200 | it. */ | |
1201 | BINDING_LEVEL (binding) = NULL; | |
1202 | } | |
f181d4ae MM |
1203 | } |
1204 | ||
acef433b MM |
1205 | /* When a label goes out of scope, check to see if that label was used |
1206 | in a valid manner, and issue any appropriate warnings or errors. */ | |
1207 | ||
1208 | static void | |
6625cdb5 JM |
1209 | pop_label (label, old_value) |
1210 | tree label; | |
1211 | tree old_value; | |
acef433b | 1212 | { |
88848bde | 1213 | if (!processing_template_decl && doing_semantic_analysis_p ()) |
acef433b | 1214 | { |
88848bde MM |
1215 | if (DECL_INITIAL (label) == NULL_TREE) |
1216 | { | |
1217 | cp_error_at ("label `%D' used but not defined", label); | |
1218 | /* Avoid crashing later. */ | |
1219 | define_label (input_filename, 1, DECL_NAME (label)); | |
1220 | } | |
078721e1 | 1221 | else if (warn_unused_label && !TREE_USED (label)) |
88848bde | 1222 | cp_warning_at ("label `%D' defined but not used", label); |
acef433b | 1223 | } |
acef433b | 1224 | |
6625cdb5 | 1225 | SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value); |
acef433b MM |
1226 | } |
1227 | ||
0811ea8f | 1228 | /* At the end of a function, all labels declared within the function |
68642fb6 | 1229 | go out of scope. BLOCK is the top-level block for the |
acef433b MM |
1230 | function. */ |
1231 | ||
1232 | static void | |
1233 | pop_labels (block) | |
1234 | tree block; | |
1235 | { | |
6625cdb5 | 1236 | struct named_label_list *link; |
acef433b MM |
1237 | |
1238 | /* Clear out the definitions of all label names, since their scopes | |
1239 | end here. */ | |
6625cdb5 | 1240 | for (link = named_labels; link; link = link->next) |
acef433b | 1241 | { |
6625cdb5 | 1242 | pop_label (link->label_decl, link->old_value); |
acef433b MM |
1243 | /* Put the labels into the "variables" of the top-level block, |
1244 | so debugger can see them. */ | |
6625cdb5 JM |
1245 | TREE_CHAIN (link->label_decl) = BLOCK_VARS (block); |
1246 | BLOCK_VARS (block) = link->label_decl; | |
acef433b MM |
1247 | } |
1248 | ||
6625cdb5 | 1249 | named_labels = NULL; |
acef433b MM |
1250 | } |
1251 | ||
8d08fdba MS |
1252 | /* Exit a binding level. |
1253 | Pop the level off, and restore the state of the identifier-decl mappings | |
1254 | that were in effect when this level was entered. | |
1255 | ||
1256 | If KEEP == 1, this level had explicit declarations, so | |
1257 | and create a "block" (a BLOCK node) for the level | |
1258 | to record its declarations and subblocks for symbol table output. | |
1259 | ||
8d08fdba MS |
1260 | If FUNCTIONBODY is nonzero, this level is the body of a function, |
1261 | so create a block as if KEEP were set and also clear out all | |
1262 | label names. | |
1263 | ||
1264 | If REVERSE is nonzero, reverse the order of decls before putting | |
1265 | them into the BLOCK. */ | |
1266 | ||
1267 | tree | |
1268 | poplevel (keep, reverse, functionbody) | |
1269 | int keep; | |
1270 | int reverse; | |
1271 | int functionbody; | |
1272 | { | |
1273 | register tree link; | |
1274 | /* The chain of decls was accumulated in reverse order. | |
1275 | Put it into forward order, just for cleanliness. */ | |
1276 | tree decls; | |
1277 | int tmp = functionbody; | |
b35d4555 MM |
1278 | int real_functionbody; |
1279 | tree tags; | |
1280 | tree subblocks; | |
8d08fdba MS |
1281 | tree block = NULL_TREE; |
1282 | tree decl; | |
f181d4ae MM |
1283 | int leaving_for_scope; |
1284 | ||
01d939e8 | 1285 | if (cfun && !doing_semantic_analysis_p ()) |
b35d4555 MM |
1286 | return NULL_TREE; |
1287 | ||
273a708f MM |
1288 | my_friendly_assert (current_binding_level->parm_flag != 2, |
1289 | 19990916); | |
b74a0560 | 1290 | |
b35d4555 MM |
1291 | real_functionbody = (current_binding_level->keep == 2 |
1292 | ? ((functionbody = 0), tmp) : functionbody); | |
1293 | tags = functionbody >= 0 ? current_binding_level->tags : 0; | |
1294 | subblocks = functionbody >= 0 ? current_binding_level->blocks : 0; | |
1295 | ||
b74a0560 MM |
1296 | my_friendly_assert (!current_binding_level->class_shadowed, |
1297 | 19990414); | |
8d08fdba | 1298 | |
536333d4 MM |
1299 | /* We used to use KEEP == 2 to indicate that the new block should go |
1300 | at the beginning of the list of blocks at this binding level, | |
1301 | rather than the end. This hack is no longer used. */ | |
1302 | my_friendly_assert (keep == 0 || keep == 1, 0); | |
1303 | ||
8d08fdba MS |
1304 | if (current_binding_level->keep == 1) |
1305 | keep = 1; | |
1306 | ||
6625cdb5 JM |
1307 | /* Any uses of undefined labels, and any defined labels, now operate |
1308 | under constraints of next binding contour. */ | |
1309 | if (cfun && !functionbody) | |
1310 | { | |
1311 | struct binding_level *level_chain; | |
1312 | level_chain = current_binding_level->level_chain; | |
1313 | if (level_chain) | |
1314 | { | |
1315 | struct named_label_use_list *uses; | |
1316 | struct named_label_list *labels; | |
1317 | for (labels = named_labels; labels; labels = labels->next) | |
1318 | if (labels->binding_level == current_binding_level) | |
1319 | { | |
1320 | tree decl; | |
826840d9 RH |
1321 | if (current_binding_level->is_try_scope) |
1322 | labels->in_try_scope = 1; | |
1323 | if (current_binding_level->is_catch_scope) | |
1324 | labels->in_catch_scope = 1; | |
6625cdb5 JM |
1325 | for (decl = labels->names_in_scope; decl; |
1326 | decl = TREE_CHAIN (decl)) | |
1327 | if (decl_jump_unsafe (decl)) | |
1328 | labels->bad_decls = tree_cons (NULL_TREE, decl, | |
1329 | labels->bad_decls); | |
1330 | labels->binding_level = level_chain; | |
1331 | labels->names_in_scope = level_chain->names; | |
1332 | } | |
1333 | ||
1334 | for (uses = named_label_uses; uses; uses = uses->next) | |
1335 | if (uses->binding_level == current_binding_level) | |
1336 | { | |
1337 | uses->binding_level = level_chain; | |
1338 | uses->names_in_scope = level_chain->names; | |
1339 | } | |
1340 | } | |
1341 | } | |
1342 | ||
8d08fdba MS |
1343 | /* Get the decls in the order they were written. |
1344 | Usually current_binding_level->names is in reverse order. | |
1345 | But parameter decls were previously put in forward order. */ | |
1346 | ||
1347 | if (reverse) | |
1348 | current_binding_level->names | |
1349 | = decls = nreverse (current_binding_level->names); | |
1350 | else | |
1351 | decls = current_binding_level->names; | |
1352 | ||
1353 | /* Output any nested inline functions within this block | |
1354 | if they weren't already output. */ | |
8d08fdba MS |
1355 | for (decl = decls; decl; decl = TREE_CHAIN (decl)) |
1356 | if (TREE_CODE (decl) == FUNCTION_DECL | |
1357 | && ! TREE_ASM_WRITTEN (decl) | |
1358 | && DECL_INITIAL (decl) != NULL_TREE | |
6060a796 MS |
1359 | && TREE_ADDRESSABLE (decl) |
1360 | && decl_function_context (decl) == current_function_decl) | |
8d08fdba MS |
1361 | { |
1362 | /* If this decl was copied from a file-scope decl | |
1363 | on account of a block-scope extern decl, | |
1364 | propagate TREE_ADDRESSABLE to the file-scope decl. */ | |
1365 | if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE) | |
1366 | TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1; | |
1367 | else | |
1368 | { | |
1369 | push_function_context (); | |
1370 | output_inline_function (decl); | |
1371 | pop_function_context (); | |
1372 | } | |
1373 | } | |
1374 | ||
d9b2d9da MM |
1375 | /* When not in function-at-a-time mode, expand_end_bindings will |
1376 | warn about unused variables. But, in function-at-a-time mode | |
1377 | expand_end_bindings is not passed the list of variables in the | |
1378 | current scope, and therefore no warning is emitted. So, we | |
1379 | explicitly warn here. */ | |
1380 | if (!processing_template_decl) | |
1381 | warn_about_unused_variables (getdecls ()); | |
1382 | ||
8d08fdba MS |
1383 | /* If there were any declarations or structure tags in that level, |
1384 | or if this level is a function body, | |
1385 | create a BLOCK to record them for the life of this function. */ | |
8d08fdba | 1386 | block = NULL_TREE; |
f444e36b | 1387 | if (keep == 1 || functionbody) |
8d08fdba MS |
1388 | block = make_node (BLOCK); |
1389 | if (block != NULL_TREE) | |
1390 | { | |
f444e36b MM |
1391 | BLOCK_VARS (block) = decls; |
1392 | BLOCK_SUBBLOCKS (block) = subblocks; | |
8d08fdba MS |
1393 | } |
1394 | ||
1395 | /* In each subblock, record that this is its superior. */ | |
8d08fdba MS |
1396 | if (keep >= 0) |
1397 | for (link = subblocks; link; link = TREE_CHAIN (link)) | |
1398 | BLOCK_SUPERCONTEXT (link) = block; | |
1399 | ||
f181d4ae MM |
1400 | /* We still support the old for-scope rules, whereby the variables |
1401 | in a for-init statement were in scope after the for-statement | |
1402 | ended. We only use the new rules in flag_new_for_scope is | |
1403 | nonzero. */ | |
68642fb6 | 1404 | leaving_for_scope |
f181d4ae MM |
1405 | = current_binding_level->is_for_scope && flag_new_for_scope == 1; |
1406 | ||
1407 | /* Remove declarations for all the DECLs in this level. */ | |
1408 | for (link = decls; link; link = TREE_CHAIN (link)) | |
1409 | { | |
c3783399 NS |
1410 | if (leaving_for_scope && TREE_CODE (link) == VAR_DECL |
1411 | && DECL_NAME (link)) | |
f181d4ae | 1412 | { |
68642fb6 | 1413 | tree outer_binding |
f181d4ae MM |
1414 | = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (link))); |
1415 | tree ns_binding; | |
1416 | ||
1417 | if (!outer_binding) | |
1418 | ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link)); | |
c7dda1e3 MM |
1419 | else |
1420 | ns_binding = NULL_TREE; | |
1421 | ||
68642fb6 UD |
1422 | if (outer_binding |
1423 | && (BINDING_LEVEL (outer_binding) | |
f181d4ae MM |
1424 | == current_binding_level->level_chain)) |
1425 | /* We have something like: | |
68642fb6 | 1426 | |
f181d4ae MM |
1427 | int i; |
1428 | for (int i; ;); | |
68642fb6 | 1429 | |
f181d4ae MM |
1430 | and we are leaving the `for' scope. There's no reason to |
1431 | keep the binding of the inner `i' in this case. */ | |
d8f8dca1 | 1432 | pop_binding (DECL_NAME (link), link); |
68642fb6 UD |
1433 | else if ((outer_binding |
1434 | && (TREE_CODE (BINDING_VALUE (outer_binding)) | |
f181d4ae | 1435 | == TYPE_DECL)) |
68642fb6 | 1436 | || (ns_binding |
f181d4ae MM |
1437 | && TREE_CODE (ns_binding) == TYPE_DECL)) |
1438 | /* Here, we have something like: | |
1439 | ||
1440 | typedef int I; | |
1441 | ||
1442 | void f () { | |
1443 | for (int I; ;); | |
1444 | } | |
1445 | ||
1446 | We must pop the for-scope binding so we know what's a | |
1447 | type and what isn't. */ | |
d8f8dca1 | 1448 | pop_binding (DECL_NAME (link), link); |
e76a2646 | 1449 | else |
e76a2646 | 1450 | { |
f181d4ae MM |
1451 | /* Mark this VAR_DECL as dead so that we can tell we left it |
1452 | there only for backward compatibility. */ | |
1453 | DECL_DEAD_FOR_LOCAL (link) = 1; | |
68642fb6 | 1454 | |
f181d4ae MM |
1455 | /* Keep track of what should of have happenned when we |
1456 | popped the binding. */ | |
1457 | if (outer_binding && BINDING_VALUE (outer_binding)) | |
68642fb6 | 1458 | DECL_SHADOWED_FOR_VAR (link) |
f181d4ae MM |
1459 | = BINDING_VALUE (outer_binding); |
1460 | ||
1461 | /* Add it to the list of dead variables in the next | |
1462 | outermost binding to that we can remove these when we | |
1463 | leave that binding. */ | |
1464 | current_binding_level->level_chain->dead_vars_from_for | |
1465 | = tree_cons (NULL_TREE, link, | |
1466 | current_binding_level->level_chain-> | |
1467 | dead_vars_from_for); | |
1468 | ||
1469 | /* Although we don't pop the CPLUS_BINDING, we do clear | |
1470 | its BINDING_LEVEL since the level is going away now. */ | |
1471 | BINDING_LEVEL (IDENTIFIER_BINDING (DECL_NAME (link))) | |
1472 | = 0; | |
e76a2646 | 1473 | } |
2ee887f2 | 1474 | } |
68642fb6 | 1475 | else |
8d6e462b | 1476 | { |
f181d4ae | 1477 | /* Remove the binding. */ |
0034cf72 JM |
1478 | decl = link; |
1479 | if (TREE_CODE (decl) == TREE_LIST) | |
1480 | decl = TREE_VALUE (decl); | |
2f939d94 | 1481 | if (DECL_P (decl)) |
0034cf72 JM |
1482 | pop_binding (DECL_NAME (decl), decl); |
1483 | else if (TREE_CODE (decl) == OVERLOAD) | |
1484 | pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl); | |
68642fb6 | 1485 | else |
a98facb0 | 1486 | abort (); |
8d08fdba | 1487 | } |
f181d4ae | 1488 | } |
8d08fdba | 1489 | |
f181d4ae MM |
1490 | /* Remove declarations for any `for' variables from inner scopes |
1491 | that we kept around. */ | |
1492 | for (link = current_binding_level->dead_vars_from_for; | |
1493 | link; link = TREE_CHAIN (link)) | |
d8f8dca1 | 1494 | pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link)); |
2ee887f2 | 1495 | |
f181d4ae MM |
1496 | /* Restore the IDENTIFIER_TYPE_VALUEs. */ |
1497 | for (link = current_binding_level->type_shadowed; | |
1498 | link; link = TREE_CHAIN (link)) | |
1499 | SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link)); | |
acef433b MM |
1500 | |
1501 | /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */ | |
1502 | for (link = current_binding_level->shadowed_labels; | |
68642fb6 | 1503 | link; |
acef433b | 1504 | link = TREE_CHAIN (link)) |
6625cdb5 | 1505 | pop_label (TREE_VALUE (link), TREE_PURPOSE (link)); |
acef433b | 1506 | |
f181d4ae MM |
1507 | /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs |
1508 | list if a `using' declaration put them there. The debugging | |
1509 | back-ends won't understand OVERLOAD, so we remove them here. | |
1510 | Because the BLOCK_VARS are (temporarily) shared with | |
1511 | CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have | |
1512 | popped all the bindings. */ | |
1513 | if (block) | |
1514 | { | |
1515 | tree* d; | |
8d6e462b | 1516 | |
a06d48ef JM |
1517 | for (d = &BLOCK_VARS (block); *d; ) |
1518 | { | |
1519 | if (TREE_CODE (*d) == TREE_LIST) | |
1520 | *d = TREE_CHAIN (*d); | |
1521 | else | |
1522 | d = &TREE_CHAIN (*d); | |
1523 | } | |
8d6e462b | 1524 | } |
8d08fdba MS |
1525 | |
1526 | /* If the level being exited is the top level of a function, | |
1527 | check over all the labels. */ | |
8d08fdba MS |
1528 | if (functionbody) |
1529 | { | |
acef433b MM |
1530 | /* Since this is the top level block of a function, the vars are |
1531 | the function's parameters. Don't leave them in the BLOCK | |
1532 | because they are found in the FUNCTION_DECL instead. */ | |
8d08fdba | 1533 | BLOCK_VARS (block) = 0; |
acef433b | 1534 | pop_labels (block); |
8d08fdba MS |
1535 | } |
1536 | ||
8d08fdba MS |
1537 | tmp = current_binding_level->keep; |
1538 | ||
1539 | pop_binding_level (); | |
1540 | if (functionbody) | |
1541 | DECL_INITIAL (current_function_decl) = block; | |
1542 | else if (block) | |
f444e36b MM |
1543 | current_binding_level->blocks |
1544 | = chainon (current_binding_level->blocks, block); | |
1545 | ||
8d08fdba MS |
1546 | /* If we did not make a block for the level just exited, |
1547 | any blocks made for inner levels | |
1548 | (since they cannot be recorded as subblocks in that level) | |
1549 | must be carried forward so they will later become subblocks | |
1550 | of something else. */ | |
1551 | else if (subblocks) | |
536333d4 MM |
1552 | current_binding_level->blocks |
1553 | = chainon (current_binding_level->blocks, subblocks); | |
8d08fdba | 1554 | |
d9b2d9da MM |
1555 | /* Each and every BLOCK node created here in `poplevel' is important |
1556 | (e.g. for proper debugging information) so if we created one | |
1557 | earlier, mark it as "used". */ | |
1558 | if (block) | |
1559 | TREE_USED (block) = 1; | |
1560 | ||
8d08fdba | 1561 | /* Take care of compiler's internal binding structures. */ |
a4443a08 | 1562 | if (tmp == 2) |
8d08fdba | 1563 | { |
d9b2d9da MM |
1564 | tree scope_stmts; |
1565 | ||
68642fb6 | 1566 | scope_stmts |
d9b2d9da | 1567 | = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/1); |
8d08fdba | 1568 | if (block) |
d9b2d9da MM |
1569 | { |
1570 | SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block; | |
1571 | SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block; | |
1572 | } | |
1573 | ||
8f471b0d | 1574 | block = poplevel (keep, reverse, functionbody); |
8d08fdba MS |
1575 | } |
1576 | ||
8d08fdba MS |
1577 | return block; |
1578 | } | |
1579 | ||
1580 | /* Delete the node BLOCK from the current binding level. | |
1581 | This is used for the block inside a stmt expr ({...}) | |
1582 | so that the block can be reinserted where appropriate. */ | |
1583 | ||
1584 | void | |
1585 | delete_block (block) | |
1586 | tree block; | |
1587 | { | |
1588 | tree t; | |
1589 | if (current_binding_level->blocks == block) | |
1590 | current_binding_level->blocks = TREE_CHAIN (block); | |
1591 | for (t = current_binding_level->blocks; t;) | |
1592 | { | |
1593 | if (TREE_CHAIN (t) == block) | |
1594 | TREE_CHAIN (t) = TREE_CHAIN (block); | |
1595 | else | |
1596 | t = TREE_CHAIN (t); | |
1597 | } | |
1598 | TREE_CHAIN (block) = NULL_TREE; | |
1599 | /* Clear TREE_USED which is always set by poplevel. | |
1600 | The flag is set again if insert_block is called. */ | |
1601 | TREE_USED (block) = 0; | |
1602 | } | |
1603 | ||
1604 | /* Insert BLOCK at the end of the list of subblocks of the | |
1605 | current binding level. This is used when a BIND_EXPR is expanded, | |
1606 | to handle the BLOCK node inside the BIND_EXPR. */ | |
1607 | ||
1608 | void | |
1609 | insert_block (block) | |
1610 | tree block; | |
1611 | { | |
1612 | TREE_USED (block) = 1; | |
1613 | current_binding_level->blocks | |
1614 | = chainon (current_binding_level->blocks, block); | |
1615 | } | |
1616 | ||
8d08fdba MS |
1617 | /* Set the BLOCK node for the innermost scope |
1618 | (the one we are currently in). */ | |
1619 | ||
1620 | void | |
1621 | set_block (block) | |
f444e36b | 1622 | tree block ATTRIBUTE_UNUSED; |
8d08fdba | 1623 | { |
f444e36b MM |
1624 | /* The RTL expansion machinery requires us to provide this callback, |
1625 | but it is not applicable in function-at-a-time mode. */ | |
1626 | my_friendly_assert (cfun && !doing_semantic_analysis_p (), 20000911); | |
8d08fdba MS |
1627 | } |
1628 | ||
1629 | /* Do a pushlevel for class declarations. */ | |
e92cc029 | 1630 | |
8d08fdba MS |
1631 | void |
1632 | pushlevel_class () | |
1633 | { | |
1634 | register struct binding_level *newlevel; | |
1635 | ||
1636 | /* Reuse or create a struct for this binding level. */ | |
1637 | #if defined(DEBUG_CP_BINDING_LEVELS) | |
1638 | if (0) | |
1639 | #else /* !defined(DEBUG_CP_BINDING_LEVELS) */ | |
1640 | if (free_binding_level) | |
1641 | #endif /* !defined(DEBUG_CP_BINDING_LEVELS) */ | |
1642 | { | |
1643 | newlevel = free_binding_level; | |
1644 | free_binding_level = free_binding_level->level_chain; | |
1645 | } | |
1646 | else | |
f181d4ae | 1647 | newlevel = make_binding_level (); |
8d08fdba MS |
1648 | |
1649 | #if defined(DEBUG_CP_BINDING_LEVELS) | |
1650 | is_class_level = 1; | |
1651 | #endif /* defined(DEBUG_CP_BINDING_LEVELS) */ | |
1652 | ||
1653 | push_binding_level (newlevel, 0, 0); | |
1654 | ||
8d08fdba MS |
1655 | class_binding_level = current_binding_level; |
1656 | class_binding_level->parm_flag = 2; | |
70adf8a9 | 1657 | class_binding_level->this_class = current_class_type; |
8d08fdba MS |
1658 | } |
1659 | ||
b74a0560 | 1660 | /* ...and a poplevel for class declarations. */ |
e92cc029 | 1661 | |
273a708f | 1662 | void |
b74a0560 | 1663 | poplevel_class () |
8d08fdba MS |
1664 | { |
1665 | register struct binding_level *level = class_binding_level; | |
8d08fdba MS |
1666 | tree shadowed; |
1667 | ||
1668 | my_friendly_assert (level != 0, 354); | |
68642fb6 | 1669 | |
8d08fdba | 1670 | /* If we're leaving a toplevel class, don't bother to do the setting |
ddd5a7c1 | 1671 | of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot |
8d08fdba | 1672 | shouldn't even be used when current_class_type isn't set, and second, |
ddd5a7c1 | 1673 | if we don't touch it here, we're able to use the cache effect if the |
8d08fdba | 1674 | next time we're entering a class scope, it is the same class. */ |
b74a0560 | 1675 | if (current_class_depth != 1) |
8f032717 MM |
1676 | { |
1677 | struct binding_level* b; | |
1678 | ||
1679 | /* Clear out our IDENTIFIER_CLASS_VALUEs. */ | |
1680 | for (shadowed = level->class_shadowed; | |
1681 | shadowed; | |
1682 | shadowed = TREE_CHAIN (shadowed)) | |
1683 | IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE; | |
68642fb6 | 1684 | |
8f032717 MM |
1685 | /* Find the next enclosing class, and recreate |
1686 | IDENTIFIER_CLASS_VALUEs appropriate for that class. */ | |
1687 | b = level->level_chain; | |
1688 | while (b && b->parm_flag != 2) | |
1689 | b = b->level_chain; | |
1690 | ||
1691 | if (b) | |
68642fb6 UD |
1692 | for (shadowed = b->class_shadowed; |
1693 | shadowed; | |
8f032717 MM |
1694 | shadowed = TREE_CHAIN (shadowed)) |
1695 | { | |
1696 | tree t; | |
1697 | ||
1698 | t = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed)); | |
1699 | while (t && BINDING_LEVEL (t) != b) | |
1700 | t = TREE_CHAIN (t); | |
68642fb6 | 1701 | |
8f032717 | 1702 | if (t) |
68642fb6 | 1703 | IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) |
8f032717 MM |
1704 | = BINDING_VALUE (t); |
1705 | } | |
1706 | } | |
8d08fdba MS |
1707 | else |
1708 | /* Remember to save what IDENTIFIER's were bound in this scope so we | |
1709 | can recover from cache misses. */ | |
e76a2646 MS |
1710 | { |
1711 | previous_class_type = current_class_type; | |
1712 | previous_class_values = class_binding_level->class_shadowed; | |
1713 | } | |
8d08fdba MS |
1714 | for (shadowed = level->type_shadowed; |
1715 | shadowed; | |
1716 | shadowed = TREE_CHAIN (shadowed)) | |
2c73f9f5 | 1717 | SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed)); |
8d08fdba | 1718 | |
f181d4ae | 1719 | /* Remove the bindings for all of the class-level declarations. */ |
68642fb6 UD |
1720 | for (shadowed = level->class_shadowed; |
1721 | shadowed; | |
f181d4ae | 1722 | shadowed = TREE_CHAIN (shadowed)) |
d8f8dca1 | 1723 | pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed)); |
f181d4ae | 1724 | |
38e01259 | 1725 | /* Now, pop out of the binding level which we created up in the |
8d08fdba MS |
1726 | `pushlevel_class' routine. */ |
1727 | #if defined(DEBUG_CP_BINDING_LEVELS) | |
1728 | is_class_level = 1; | |
1729 | #endif /* defined(DEBUG_CP_BINDING_LEVELS) */ | |
1730 | ||
1731 | pop_binding_level (); | |
8d08fdba | 1732 | } |
8f032717 MM |
1733 | |
1734 | /* We are entering the scope of a class. Clear IDENTIFIER_CLASS_VALUE | |
1735 | for any names in enclosing classes. */ | |
1736 | ||
1737 | void | |
1738 | clear_identifier_class_values () | |
1739 | { | |
1740 | tree t; | |
1741 | ||
1742 | if (!class_binding_level) | |
1743 | return; | |
1744 | ||
1745 | for (t = class_binding_level->class_shadowed; | |
1746 | t; | |
1747 | t = TREE_CHAIN (t)) | |
1748 | IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE; | |
1749 | } | |
1750 | ||
0aafb128 MM |
1751 | /* Returns non-zero if T is a virtual function table. */ |
1752 | ||
1753 | int | |
1754 | vtable_decl_p (t, data) | |
1755 | tree t; | |
1756 | void *data ATTRIBUTE_UNUSED; | |
1757 | { | |
1758 | return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t)); | |
1759 | } | |
1760 | ||
1761 | /* Returns non-zero if T is a TYPE_DECL for a type with virtual | |
1762 | functions. */ | |
1763 | ||
1764 | int | |
1765 | vtype_decl_p (t, data) | |
1766 | tree t; | |
1767 | void *data ATTRIBUTE_UNUSED; | |
1768 | { | |
1769 | return (TREE_CODE (t) == TYPE_DECL | |
34249844 NS |
1770 | && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE |
1771 | && TYPE_POLYMORPHIC_P (TREE_TYPE (t))); | |
0aafb128 MM |
1772 | } |
1773 | ||
f71f87f9 MM |
1774 | /* Return the declarations that are members of the namespace NS. */ |
1775 | ||
1776 | tree | |
1777 | cp_namespace_decls (ns) | |
1778 | tree ns; | |
1779 | { | |
1780 | return NAMESPACE_LEVEL (ns)->names; | |
1781 | } | |
1782 | ||
0aafb128 MM |
1783 | /* Walk all the namespaces contained NAMESPACE, including NAMESPACE |
1784 | itself, calling F for each. The DATA is passed to F as well. */ | |
1785 | ||
1786 | static int | |
1787 | walk_namespaces_r (namespace, f, data) | |
1788 | tree namespace; | |
1789 | walk_namespaces_fn f; | |
1790 | void *data; | |
1791 | { | |
1792 | tree current; | |
1793 | int result = 0; | |
1794 | ||
1795 | result |= (*f) (namespace, data); | |
1796 | ||
f71f87f9 | 1797 | for (current = cp_namespace_decls (namespace); |
0aafb128 MM |
1798 | current; |
1799 | current = TREE_CHAIN (current)) | |
1800 | { | |
1801 | if (TREE_CODE (current) != NAMESPACE_DECL | |
1802 | || DECL_NAMESPACE_ALIAS (current)) | |
1803 | continue; | |
0aafb128 MM |
1804 | |
1805 | /* We found a namespace. */ | |
1806 | result |= walk_namespaces_r (current, f, data); | |
1807 | } | |
1808 | ||
1809 | return result; | |
1810 | } | |
1811 | ||
1812 | /* Walk all the namespaces, calling F for each. The DATA is passed to | |
1813 | F as well. */ | |
1814 | ||
1815 | int | |
1816 | walk_namespaces (f, data) | |
1817 | walk_namespaces_fn f; | |
1818 | void *data; | |
1819 | { | |
1820 | return walk_namespaces_r (global_namespace, f, data); | |
1821 | } | |
1822 | ||
1823 | struct walk_globals_data { | |
1824 | walk_globals_pred p; | |
1825 | walk_globals_fn f; | |
1826 | void *data; | |
1827 | }; | |
1828 | ||
1829 | /* Walk the global declarations in NAMESPACE. Whenever one is found | |
1830 | for which P returns non-zero, call F with its address. If any call | |
1831 | to F returns a non-zero value, return a non-zero value. */ | |
1832 | ||
68642fb6 | 1833 | static int |
0aafb128 MM |
1834 | walk_globals_r (namespace, data) |
1835 | tree namespace; | |
1836 | void *data; | |
1837 | { | |
1838 | struct walk_globals_data* wgd = (struct walk_globals_data *) data; | |
1839 | walk_globals_pred p = wgd->p; | |
1840 | walk_globals_fn f = wgd->f; | |
1841 | void *d = wgd->data; | |
1842 | tree *t; | |
1843 | int result = 0; | |
1844 | ||
1845 | t = &NAMESPACE_LEVEL (namespace)->names; | |
1846 | ||
1847 | while (*t) | |
1848 | { | |
1849 | tree glbl = *t; | |
1850 | ||
1851 | if ((*p) (glbl, d)) | |
1852 | result |= (*f) (t, d); | |
1853 | ||
1854 | /* If F changed *T, then *T still points at the next item to | |
1855 | examine. */ | |
1856 | if (*t == glbl) | |
1857 | t = &TREE_CHAIN (*t); | |
1858 | } | |
1859 | ||
1860 | return result; | |
1861 | } | |
1862 | ||
1863 | /* Walk the global declarations. Whenever one is found for which P | |
1864 | returns non-zero, call F with its address. If any call to F | |
1865 | returns a non-zero value, return a non-zero value. */ | |
1866 | ||
1867 | int | |
1868 | walk_globals (p, f, data) | |
1869 | walk_globals_pred p; | |
1870 | walk_globals_fn f; | |
1871 | void *data; | |
1872 | { | |
1873 | struct walk_globals_data wgd; | |
1874 | wgd.p = p; | |
1875 | wgd.f = f; | |
1876 | wgd.data = data; | |
1877 | ||
1878 | return walk_namespaces (walk_globals_r, &wgd); | |
1879 | } | |
1880 | ||
1881 | /* Call wrapup_globals_declarations for the globals in NAMESPACE. If | |
1882 | DATA is non-NULL, this is the last time we will call | |
1883 | wrapup_global_declarations for this NAMESPACE. */ | |
1884 | ||
1885 | int | |
1886 | wrapup_globals_for_namespace (namespace, data) | |
1887 | tree namespace; | |
1888 | void *data; | |
1889 | { | |
f71f87f9 | 1890 | tree globals = cp_namespace_decls (namespace); |
0aafb128 MM |
1891 | int len = list_length (globals); |
1892 | tree *vec = (tree *) alloca (sizeof (tree) * len); | |
1893 | int i; | |
0352cfc8 | 1894 | int result; |
0aafb128 MM |
1895 | tree decl; |
1896 | int last_time = (data != 0); | |
1897 | ||
1898 | if (last_time && namespace == global_namespace) | |
1899 | /* Let compile_file handle the global namespace. */ | |
1900 | return 0; | |
1901 | ||
1902 | /* Process the decls in reverse order--earliest first. | |
1903 | Put them into VEC from back to front, then take out from front. */ | |
0aafb128 | 1904 | for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl)) |
62136084 | 1905 | vec[len - i - 1] = decl; |
68642fb6 | 1906 | |
0352cfc8 MM |
1907 | if (last_time) |
1908 | { | |
1909 | check_global_declarations (vec, len); | |
1910 | return 0; | |
1911 | } | |
1912 | ||
1913 | /* Temporarily mark vtables as external. That prevents | |
1914 | wrapup_global_declarations from writing them out; we must process | |
1915 | them ourselves in finish_vtable_vardecl. */ | |
1916 | for (i = 0; i < len; ++i) | |
91f2aa22 | 1917 | if (vtable_decl_p (vec[i], /*data=*/0) && !DECL_EXTERNAL (vec[i])) |
0352cfc8 MM |
1918 | { |
1919 | DECL_NOT_REALLY_EXTERN (vec[i]) = 1; | |
1920 | DECL_EXTERNAL (vec[i]) = 1; | |
1921 | } | |
0aafb128 | 1922 | |
0352cfc8 MM |
1923 | /* Write out any globals that need to be output. */ |
1924 | result = wrapup_global_declarations (vec, len); | |
1925 | ||
1926 | /* Undo the hack to DECL_EXTERNAL above. */ | |
1927 | for (i = 0; i < len; ++i) | |
1928 | if (vtable_decl_p (vec[i], /*data=*/0) | |
1929 | && DECL_NOT_REALLY_EXTERN (vec[i])) | |
1930 | { | |
1931 | DECL_NOT_REALLY_EXTERN (vec[i]) = 0; | |
1932 | DECL_EXTERNAL (vec[i]) = 0; | |
1933 | } | |
1934 | ||
1935 | return result; | |
0aafb128 MM |
1936 | } |
1937 | ||
4519c0a8 | 1938 | \f |
fc6af6e3 RH |
1939 | /* Mark ARG (which is really a struct binding_level **) for GC. */ |
1940 | ||
4519c0a8 MM |
1941 | static void |
1942 | mark_binding_level (arg) | |
1943 | void *arg; | |
1944 | { | |
1945 | struct binding_level *lvl = *(struct binding_level **)arg; | |
1946 | ||
6625cdb5 | 1947 | for (; lvl; lvl = lvl->level_chain) |
4519c0a8 MM |
1948 | { |
1949 | ggc_mark_tree (lvl->names); | |
1950 | ggc_mark_tree (lvl->tags); | |
1951 | ggc_mark_tree (lvl->usings); | |
1952 | ggc_mark_tree (lvl->using_directives); | |
1953 | ggc_mark_tree (lvl->class_shadowed); | |
1954 | ggc_mark_tree (lvl->type_shadowed); | |
1955 | ggc_mark_tree (lvl->shadowed_labels); | |
1956 | ggc_mark_tree (lvl->blocks); | |
70adf8a9 | 1957 | ggc_mark_tree (lvl->this_class); |
4519c0a8 | 1958 | ggc_mark_tree (lvl->dead_vars_from_for); |
6625cdb5 JM |
1959 | } |
1960 | } | |
1961 | ||
1962 | static void | |
1963 | mark_named_label_lists (labs, uses) | |
1964 | void *labs; | |
1965 | void *uses; | |
1966 | { | |
1967 | struct named_label_list *l = *(struct named_label_list **)labs; | |
1968 | struct named_label_use_list *u = *(struct named_label_use_list **)uses; | |
4519c0a8 | 1969 | |
6625cdb5 JM |
1970 | for (; l; l = l->next) |
1971 | { | |
1972 | ggc_mark (l); | |
1973 | mark_binding_level (l->binding_level); | |
1974 | ggc_mark_tree (l->old_value); | |
1975 | ggc_mark_tree (l->label_decl); | |
1976 | ggc_mark_tree (l->bad_decls); | |
4519c0a8 | 1977 | } |
6625cdb5 JM |
1978 | |
1979 | for (; u; u = u->next) | |
1980 | ggc_mark (u); | |
4519c0a8 | 1981 | } |
8d08fdba MS |
1982 | \f |
1983 | /* For debugging. */ | |
5566b478 MS |
1984 | static int no_print_functions = 0; |
1985 | static int no_print_builtins = 0; | |
8d08fdba MS |
1986 | |
1987 | void | |
1988 | print_binding_level (lvl) | |
1989 | struct binding_level *lvl; | |
1990 | { | |
1991 | tree t; | |
1992 | int i = 0, len; | |
1993 | fprintf (stderr, " blocks="); | |
1994 | fprintf (stderr, HOST_PTR_PRINTF, lvl->blocks); | |
8d08fdba MS |
1995 | if (lvl->tag_transparent) |
1996 | fprintf (stderr, " tag-transparent"); | |
1997 | if (lvl->more_cleanups_ok) | |
1998 | fprintf (stderr, " more-cleanups-ok"); | |
1999 | if (lvl->have_cleanups) | |
2000 | fprintf (stderr, " have-cleanups"); | |
8d08fdba MS |
2001 | fprintf (stderr, "\n"); |
2002 | if (lvl->names) | |
2003 | { | |
2004 | fprintf (stderr, " names:\t"); | |
2005 | /* We can probably fit 3 names to a line? */ | |
2006 | for (t = lvl->names; t; t = TREE_CHAIN (t)) | |
2007 | { | |
68642fb6 | 2008 | if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL)) |
8d08fdba MS |
2009 | continue; |
2010 | if (no_print_builtins | |
fc378698 MS |
2011 | && (TREE_CODE (t) == TYPE_DECL) |
2012 | && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>"))) | |
8d08fdba MS |
2013 | continue; |
2014 | ||
2015 | /* Function decls tend to have longer names. */ | |
2016 | if (TREE_CODE (t) == FUNCTION_DECL) | |
2017 | len = 3; | |
2018 | else | |
2019 | len = 2; | |
2020 | i += len; | |
2021 | if (i > 6) | |
2022 | { | |
2023 | fprintf (stderr, "\n\t"); | |
2024 | i = len; | |
2025 | } | |
2026 | print_node_brief (stderr, "", t, 0); | |
bd6dd845 | 2027 | if (t == error_mark_node) |
8d08fdba MS |
2028 | break; |
2029 | } | |
2030 | if (i) | |
2031 | fprintf (stderr, "\n"); | |
2032 | } | |
2033 | if (lvl->tags) | |
2034 | { | |
2035 | fprintf (stderr, " tags:\t"); | |
2036 | i = 0; | |
2037 | for (t = lvl->tags; t; t = TREE_CHAIN (t)) | |
2038 | { | |
2039 | if (TREE_PURPOSE (t) == NULL_TREE) | |
2040 | len = 3; | |
2041 | else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t))) | |
2042 | len = 2; | |
2043 | else | |
2044 | len = 4; | |
2045 | i += len; | |
2046 | if (i > 5) | |
2047 | { | |
2048 | fprintf (stderr, "\n\t"); | |
2049 | i = len; | |
2050 | } | |
2051 | if (TREE_PURPOSE (t) == NULL_TREE) | |
2052 | { | |
2053 | print_node_brief (stderr, "<unnamed-typedef", TREE_VALUE (t), 0); | |
2054 | fprintf (stderr, ">"); | |
2055 | } | |
2056 | else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t))) | |
2057 | print_node_brief (stderr, "", TREE_VALUE (t), 0); | |
2058 | else | |
2059 | { | |
2060 | print_node_brief (stderr, "<typedef", TREE_PURPOSE (t), 0); | |
2061 | print_node_brief (stderr, "", TREE_VALUE (t), 0); | |
2062 | fprintf (stderr, ">"); | |
2063 | } | |
2064 | } | |
2065 | if (i) | |
2066 | fprintf (stderr, "\n"); | |
2067 | } | |
8d08fdba MS |
2068 | if (lvl->class_shadowed) |
2069 | { | |
2070 | fprintf (stderr, " class-shadowed:"); | |
2071 | for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t)) | |
2072 | { | |
2073 | fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t))); | |
2074 | } | |
2075 | fprintf (stderr, "\n"); | |
2076 | } | |
2077 | if (lvl->type_shadowed) | |
2078 | { | |
2079 | fprintf (stderr, " type-shadowed:"); | |
2080 | for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t)) | |
2081 | { | |
8d08fdba | 2082 | fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t))); |
8d08fdba MS |
2083 | } |
2084 | fprintf (stderr, "\n"); | |
2085 | } | |
2086 | } | |
2087 | ||
2088 | void | |
2089 | print_other_binding_stack (stack) | |
2090 | struct binding_level *stack; | |
2091 | { | |
2092 | struct binding_level *level; | |
2093 | for (level = stack; level != global_binding_level; level = level->level_chain) | |
2094 | { | |
2095 | fprintf (stderr, "binding level "); | |
2096 | fprintf (stderr, HOST_PTR_PRINTF, level); | |
2097 | fprintf (stderr, "\n"); | |
2098 | print_binding_level (level); | |
2099 | } | |
2100 | } | |
2101 | ||
2102 | void | |
2103 | print_binding_stack () | |
2104 | { | |
2105 | struct binding_level *b; | |
2106 | fprintf (stderr, "current_binding_level="); | |
2107 | fprintf (stderr, HOST_PTR_PRINTF, current_binding_level); | |
2108 | fprintf (stderr, "\nclass_binding_level="); | |
2109 | fprintf (stderr, HOST_PTR_PRINTF, class_binding_level); | |
2110 | fprintf (stderr, "\nglobal_binding_level="); | |
2111 | fprintf (stderr, HOST_PTR_PRINTF, global_binding_level); | |
2112 | fprintf (stderr, "\n"); | |
2113 | if (class_binding_level) | |
2114 | { | |
2115 | for (b = class_binding_level; b; b = b->level_chain) | |
2116 | if (b == current_binding_level) | |
2117 | break; | |
2118 | if (b) | |
2119 | b = class_binding_level; | |
2120 | else | |
2121 | b = current_binding_level; | |
2122 | } | |
2123 | else | |
2124 | b = current_binding_level; | |
2125 | print_other_binding_stack (b); | |
2126 | fprintf (stderr, "global:\n"); | |
2127 | print_binding_level (global_binding_level); | |
2128 | } | |
a9aedbc2 | 2129 | |
2c73f9f5 ML |
2130 | /* Namespace binding access routines: The namespace_bindings field of |
2131 | the identifier is polymorphic, with three possible values: | |
2132 | NULL_TREE, a list of CPLUS_BINDINGS, or any other tree_node | |
2133 | indicating the BINDING_VALUE of global_namespace. */ | |
30394414 | 2134 | |
2c73f9f5 ML |
2135 | /* Check whether the a binding for the name to scope is known. |
2136 | Assumes that the bindings of the name are already a list | |
2137 | of bindings. Returns the binding found, or NULL_TREE. */ | |
2138 | ||
2139 | static tree | |
2140 | find_binding (name, scope) | |
30394414 JM |
2141 | tree name; |
2142 | tree scope; | |
2143 | { | |
2c73f9f5 | 2144 | tree iter, prev = NULL_TREE; |
3e3f722c ML |
2145 | |
2146 | scope = ORIGINAL_NAMESPACE (scope); | |
68642fb6 | 2147 | |
30394414 JM |
2148 | for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name); iter; |
2149 | iter = TREE_CHAIN (iter)) | |
2150 | { | |
2151 | my_friendly_assert (TREE_CODE (iter) == CPLUS_BINDING, 374); | |
2152 | if (BINDING_SCOPE (iter) == scope) | |
2c73f9f5 | 2153 | { |
cfe507be | 2154 | /* Move binding found to the front of the list, so |
2c73f9f5 ML |
2155 | subsequent lookups will find it faster. */ |
2156 | if (prev) | |
2157 | { | |
2158 | TREE_CHAIN (prev) = TREE_CHAIN (iter); | |
2159 | TREE_CHAIN (iter) = IDENTIFIER_NAMESPACE_BINDINGS (name); | |
2160 | IDENTIFIER_NAMESPACE_BINDINGS (name) = iter; | |
2161 | } | |
2162 | return iter; | |
2163 | } | |
2164 | prev = iter; | |
30394414 | 2165 | } |
2c73f9f5 ML |
2166 | return NULL_TREE; |
2167 | } | |
2168 | ||
2169 | /* Always returns a binding for name in scope. If the | |
2170 | namespace_bindings is not a list, convert it to one first. | |
2171 | If no binding is found, make a new one. */ | |
2172 | ||
2173 | tree | |
2174 | binding_for_name (name, scope) | |
2175 | tree name; | |
2176 | tree scope; | |
2177 | { | |
2178 | tree b = IDENTIFIER_NAMESPACE_BINDINGS (name); | |
2179 | tree result; | |
3e3f722c ML |
2180 | |
2181 | scope = ORIGINAL_NAMESPACE (scope); | |
68642fb6 | 2182 | |
2c73f9f5 ML |
2183 | if (b && TREE_CODE (b) != CPLUS_BINDING) |
2184 | { | |
2185 | /* Get rid of optimization for global scope. */ | |
2186 | IDENTIFIER_NAMESPACE_BINDINGS (name) = NULL_TREE; | |
2187 | BINDING_VALUE (binding_for_name (name, global_namespace)) = b; | |
2188 | b = IDENTIFIER_NAMESPACE_BINDINGS (name); | |
2189 | } | |
2190 | if (b && (result = find_binding (name, scope))) | |
2191 | return result; | |
cd9f6678 | 2192 | /* Not found, make a new one. */ |
2c73f9f5 ML |
2193 | result = make_node (CPLUS_BINDING); |
2194 | TREE_CHAIN (result) = b; | |
2195 | IDENTIFIER_NAMESPACE_BINDINGS (name) = result; | |
2196 | BINDING_SCOPE (result) = scope; | |
2197 | BINDING_TYPE (result) = NULL_TREE; | |
2198 | BINDING_VALUE (result) = NULL_TREE; | |
2c73f9f5 ML |
2199 | return result; |
2200 | } | |
2201 | ||
2202 | /* Return the binding value for name in scope, considering that | |
2203 | namespace_binding may or may not be a list of CPLUS_BINDINGS. */ | |
2204 | ||
2205 | tree | |
2206 | namespace_binding (name, scope) | |
2207 | tree name; | |
2208 | tree scope; | |
2209 | { | |
2210 | tree b = IDENTIFIER_NAMESPACE_BINDINGS (name); | |
2211 | if (b == NULL_TREE) | |
2212 | return NULL_TREE; | |
cb0dbb9a JM |
2213 | if (scope == NULL_TREE) |
2214 | scope = global_namespace; | |
2c73f9f5 ML |
2215 | if (TREE_CODE (b) != CPLUS_BINDING) |
2216 | return (scope == global_namespace) ? b : NULL_TREE; | |
2217 | name = find_binding (name,scope); | |
2218 | if (name == NULL_TREE) | |
2219 | return name; | |
2220 | return BINDING_VALUE (name); | |
2221 | } | |
2222 | ||
2223 | /* Set the binding value for name in scope. If modifying the binding | |
2224 | of global_namespace is attempted, try to optimize it. */ | |
2225 | ||
2226 | void | |
2227 | set_namespace_binding (name, scope, val) | |
2228 | tree name; | |
2229 | tree scope; | |
2230 | tree val; | |
2231 | { | |
2232 | tree b; | |
cb0dbb9a JM |
2233 | |
2234 | if (scope == NULL_TREE) | |
2235 | scope = global_namespace; | |
68642fb6 | 2236 | |
2c73f9f5 ML |
2237 | if (scope == global_namespace) |
2238 | { | |
2239 | b = IDENTIFIER_NAMESPACE_BINDINGS (name); | |
2240 | if (b == NULL_TREE || TREE_CODE (b) != CPLUS_BINDING) | |
2241 | { | |
2242 | IDENTIFIER_NAMESPACE_BINDINGS (name) = val; | |
2243 | return; | |
2244 | } | |
2245 | } | |
2246 | b = binding_for_name (name, scope); | |
2247 | BINDING_VALUE (b) = val; | |
30394414 JM |
2248 | } |
2249 | ||
dff6b454 RK |
2250 | /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we |
2251 | select a name that is unique to this compilation unit. */ | |
e92cc029 | 2252 | |
a9aedbc2 MS |
2253 | void |
2254 | push_namespace (name) | |
2255 | tree name; | |
2256 | { | |
b370501f | 2257 | tree d = NULL_TREE; |
30394414 JM |
2258 | int need_new = 1; |
2259 | int implicit_use = 0; | |
2c73f9f5 | 2260 | int global = 0; |
30394414 JM |
2261 | if (!global_namespace) |
2262 | { | |
2c73f9f5 | 2263 | /* This must be ::. */ |
30394414 | 2264 | my_friendly_assert (name == get_identifier ("::"), 377); |
2c73f9f5 | 2265 | global = 1; |
30394414 JM |
2266 | } |
2267 | else if (!name) | |
2268 | { | |
3ab52652 ML |
2269 | /* The name of anonymous namespace is unique for the translation |
2270 | unit. */ | |
0c8feefe MM |
2271 | if (!anonymous_namespace_name) |
2272 | anonymous_namespace_name = get_file_function_name ('N'); | |
2273 | name = anonymous_namespace_name; | |
3ab52652 ML |
2274 | d = IDENTIFIER_NAMESPACE_VALUE (name); |
2275 | if (d) | |
2276 | /* Reopening anonymous namespace. */ | |
2277 | need_new = 0; | |
30394414 | 2278 | implicit_use = 1; |
2c73f9f5 | 2279 | } |
30394414 JM |
2280 | else |
2281 | { | |
2c73f9f5 | 2282 | /* Check whether this is an extended namespace definition. */ |
30394414 JM |
2283 | d = IDENTIFIER_NAMESPACE_VALUE (name); |
2284 | if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL) | |
3e3f722c ML |
2285 | { |
2286 | need_new = 0; | |
2287 | if (DECL_NAMESPACE_ALIAS (d)) | |
2288 | { | |
33bd39a2 | 2289 | error ("namespace alias `%D' not allowed here, assuming `%D'", |
3e3f722c ML |
2290 | d, DECL_NAMESPACE_ALIAS (d)); |
2291 | d = DECL_NAMESPACE_ALIAS (d); | |
2292 | } | |
2293 | } | |
30394414 | 2294 | } |
68642fb6 | 2295 | |
30394414 JM |
2296 | if (need_new) |
2297 | { | |
2c73f9f5 | 2298 | /* Make a new namespace, binding the name to it. */ |
6b57ac29 | 2299 | d = build_lang_decl (NAMESPACE_DECL, name, void_type_node); |
2c73f9f5 ML |
2300 | /* The global namespace is not pushed, and the global binding |
2301 | level is set elsewhere. */ | |
2302 | if (!global) | |
2303 | { | |
40f1e342 | 2304 | DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace); |
2c73f9f5 ML |
2305 | d = pushdecl (d); |
2306 | pushlevel (0); | |
2307 | declare_namespace_level (); | |
2308 | NAMESPACE_LEVEL (d) = current_binding_level; | |
2309 | } | |
30394414 | 2310 | } |
2c73f9f5 ML |
2311 | else |
2312 | resume_binding_level (NAMESPACE_LEVEL (d)); | |
2313 | ||
30394414 JM |
2314 | if (implicit_use) |
2315 | do_using_directive (d); | |
2c73f9f5 | 2316 | /* Enter the name space. */ |
30394414 | 2317 | current_namespace = d; |
a9aedbc2 MS |
2318 | } |
2319 | ||
2320 | /* Pop from the scope of the current namespace. */ | |
e92cc029 | 2321 | |
a9aedbc2 MS |
2322 | void |
2323 | pop_namespace () | |
2324 | { | |
1dbb6023 | 2325 | my_friendly_assert (current_namespace != global_namespace, 20010801); |
cb0dbb9a | 2326 | current_namespace = CP_DECL_CONTEXT (current_namespace); |
2c73f9f5 ML |
2327 | /* The binding level is not popped, as it might be re-opened later. */ |
2328 | suspend_binding_level (); | |
2329 | } | |
a9aedbc2 | 2330 | |
61fc8c9e MM |
2331 | /* Push into the scope of the namespace NS, even if it is deeply |
2332 | nested within another namespace. */ | |
2333 | ||
2334 | void | |
2335 | push_nested_namespace (ns) | |
2336 | tree ns; | |
2337 | { | |
2338 | if (ns == global_namespace) | |
2339 | push_to_top_level (); | |
2340 | else | |
2341 | { | |
2342 | push_nested_namespace (CP_DECL_CONTEXT (ns)); | |
2343 | push_namespace (DECL_NAME (ns)); | |
2344 | } | |
2345 | } | |
2346 | ||
2347 | /* Pop back from the scope of the namespace NS, which was previously | |
2348 | entered with push_nested_namespace. */ | |
68642fb6 | 2349 | |
61fc8c9e MM |
2350 | void |
2351 | pop_nested_namespace (ns) | |
2352 | tree ns; | |
2353 | { | |
2354 | while (ns != global_namespace) | |
2355 | { | |
2356 | pop_namespace (); | |
2357 | ns = CP_DECL_CONTEXT (ns); | |
2358 | } | |
2359 | ||
2360 | pop_from_top_level (); | |
2361 | } | |
2362 | ||
8d08fdba MS |
2363 | \f |
2364 | /* Subroutines for reverting temporarily to top-level for instantiation | |
2365 | of templates and such. We actually need to clear out the class- and | |
2366 | local-value slots of all identifiers, so that only the global values | |
2367 | are at all visible. Simply setting current_binding_level to the global | |
2368 | scope isn't enough, because more binding levels may be pushed. */ | |
9cd64686 | 2369 | struct saved_scope *scope_chain; |
8d08fdba | 2370 | |
9cd64686 | 2371 | /* Mark ARG (which is really a struct saved_scope **) for GC. */ |
fc6af6e3 RH |
2372 | |
2373 | static void | |
2374 | mark_saved_scope (arg) | |
2375 | void *arg; | |
2376 | { | |
2377 | struct saved_scope *t = *(struct saved_scope **)arg; | |
2378 | while (t) | |
2379 | { | |
fc6af6e3 RH |
2380 | mark_binding_level (&t->class_bindings); |
2381 | ggc_mark_tree (t->old_bindings); | |
2382 | ggc_mark_tree (t->old_namespace); | |
ee8fc32b | 2383 | ggc_mark_tree (t->decl_ns_list); |
fc6af6e3 RH |
2384 | ggc_mark_tree (t->class_name); |
2385 | ggc_mark_tree (t->class_type); | |
2386 | ggc_mark_tree (t->access_specifier); | |
a8f73d4b | 2387 | ggc_mark_tree (t->function_decl); |
fc6af6e3 | 2388 | if (t->lang_base) |
9cd64686 | 2389 | ggc_mark_tree_varray (t->lang_base); |
fc6af6e3 | 2390 | ggc_mark_tree (t->lang_name); |
fc6af6e3 | 2391 | ggc_mark_tree (t->template_parms); |
9cd64686 MM |
2392 | ggc_mark_tree (t->x_previous_class_type); |
2393 | ggc_mark_tree (t->x_previous_class_values); | |
6f80451c | 2394 | ggc_mark_tree (t->x_saved_tree); |
70adf8a9 | 2395 | ggc_mark_tree (t->lookups); |
6f80451c MM |
2396 | |
2397 | mark_stmt_tree (&t->x_stmt_tree); | |
a8f73d4b | 2398 | mark_binding_level (&t->bindings); |
fc6af6e3 RH |
2399 | t = t->prev; |
2400 | } | |
2401 | } | |
2402 | ||
e92cc029 | 2403 | static tree |
45537677 MS |
2404 | store_bindings (names, old_bindings) |
2405 | tree names, old_bindings; | |
2406 | { | |
2407 | tree t; | |
c00996a3 JM |
2408 | tree search_bindings = old_bindings; |
2409 | ||
45537677 MS |
2410 | for (t = names; t; t = TREE_CHAIN (t)) |
2411 | { | |
2412 | tree binding, t1, id; | |
2413 | ||
2414 | if (TREE_CODE (t) == TREE_LIST) | |
2415 | id = TREE_PURPOSE (t); | |
2416 | else | |
2417 | id = DECL_NAME (t); | |
2418 | ||
68642fb6 | 2419 | if (!id |
f181d4ae MM |
2420 | /* Note that we may have an IDENTIFIER_CLASS_VALUE even when |
2421 | we have no IDENTIFIER_BINDING if we have left the class | |
2422 | scope, but cached the class-level declarations. */ | |
2423 | || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id))) | |
45537677 MS |
2424 | continue; |
2425 | ||
c00996a3 | 2426 | for (t1 = search_bindings; t1; t1 = TREE_CHAIN (t1)) |
45537677 MS |
2427 | if (TREE_VEC_ELT (t1, 0) == id) |
2428 | goto skip_it; | |
78957a2a | 2429 | |
31a714f6 | 2430 | my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135); |
87e3dbc9 | 2431 | binding = make_tree_vec (4); |
31a714f6 MM |
2432 | TREE_VEC_ELT (binding, 0) = id; |
2433 | TREE_VEC_ELT (binding, 1) = REAL_IDENTIFIER_TYPE_VALUE (id); | |
2434 | TREE_VEC_ELT (binding, 2) = IDENTIFIER_BINDING (id); | |
2435 | TREE_VEC_ELT (binding, 3) = IDENTIFIER_CLASS_VALUE (id); | |
2436 | IDENTIFIER_BINDING (id) = NULL_TREE; | |
2437 | IDENTIFIER_CLASS_VALUE (id) = NULL_TREE; | |
45537677 MS |
2438 | TREE_CHAIN (binding) = old_bindings; |
2439 | old_bindings = binding; | |
2440 | skip_it: | |
2441 | ; | |
2442 | } | |
2443 | return old_bindings; | |
2444 | } | |
2445 | ||
8d08fdba | 2446 | void |
5566b478 MS |
2447 | maybe_push_to_top_level (pseudo) |
2448 | int pseudo; | |
8d08fdba | 2449 | { |
a8f73d4b | 2450 | struct saved_scope *s; |
9cd64686 | 2451 | struct binding_level *b; |
a8f73d4b MM |
2452 | tree old_bindings; |
2453 | int need_pop; | |
2454 | ||
2455 | s = (struct saved_scope *) xcalloc (1, sizeof (struct saved_scope)); | |
8d08fdba | 2456 | |
9cd64686 MM |
2457 | b = scope_chain ? current_binding_level : 0; |
2458 | ||
a8f73d4b | 2459 | /* If we're in the middle of some function, save our state. */ |
01d939e8 | 2460 | if (cfun) |
a8f73d4b MM |
2461 | { |
2462 | need_pop = 1; | |
2463 | push_function_context_to (NULL_TREE); | |
2464 | } | |
2465 | else | |
2466 | need_pop = 0; | |
e349ee73 | 2467 | |
a8f73d4b | 2468 | old_bindings = NULL_TREE; |
9cd64686 | 2469 | if (scope_chain && previous_class_type) |
a50f0918 MS |
2470 | old_bindings = store_bindings (previous_class_values, old_bindings); |
2471 | ||
8d08fdba MS |
2472 | /* Have to include global_binding_level, because class-level decls |
2473 | aren't listed anywhere useful. */ | |
2474 | for (; b; b = b->level_chain) | |
2475 | { | |
2476 | tree t; | |
2477 | ||
2c73f9f5 ML |
2478 | /* Template IDs are inserted into the global level. If they were |
2479 | inserted into namespace level, finish_file wouldn't find them | |
2480 | when doing pending instantiations. Therefore, don't stop at | |
2481 | namespace level, but continue until :: . */ | |
74b846e0 | 2482 | if (b == global_binding_level || (pseudo && b->template_parms_p)) |
5566b478 | 2483 | break; |
8d08fdba | 2484 | |
45537677 | 2485 | old_bindings = store_bindings (b->names, old_bindings); |
cffa8729 | 2486 | /* We also need to check class_shadowed to save class-level type |
45537677 MS |
2487 | bindings, since pushclass doesn't fill in b->names. */ |
2488 | if (b->parm_flag == 2) | |
cffa8729 | 2489 | old_bindings = store_bindings (b->class_shadowed, old_bindings); |
8d08fdba | 2490 | |
8d08fdba MS |
2491 | /* Unwind type-value slots back to top level. */ |
2492 | for (t = b->type_shadowed; t; t = TREE_CHAIN (t)) | |
2493 | SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t)); | |
2494 | } | |
fc0e7bf5 MM |
2495 | s->prev = scope_chain; |
2496 | s->old_bindings = old_bindings; | |
a8f73d4b MM |
2497 | s->bindings = b; |
2498 | s->need_pop_function_context = need_pop; | |
2499 | s->function_decl = current_function_decl; | |
0f4237c2 | 2500 | s->last_parms = last_function_parms; |
fc0e7bf5 MM |
2501 | |
2502 | scope_chain = s; | |
8d08fdba | 2503 | current_function_decl = NULL_TREE; |
9cd64686 | 2504 | VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base"); |
51c184be | 2505 | current_lang_name = lang_name_cplusplus; |
2c73f9f5 | 2506 | current_namespace = global_namespace; |
5566b478 MS |
2507 | } |
2508 | ||
2509 | void | |
2510 | push_to_top_level () | |
2511 | { | |
2512 | maybe_push_to_top_level (0); | |
8d08fdba MS |
2513 | } |
2514 | ||
2515 | void | |
2516 | pop_from_top_level () | |
2517 | { | |
9cd64686 | 2518 | struct saved_scope *s = scope_chain; |
8d08fdba MS |
2519 | tree t; |
2520 | ||
e76a2646 | 2521 | /* Clear out class-level bindings cache. */ |
8d08fdba | 2522 | if (previous_class_type) |
8f032717 | 2523 | invalidate_class_lookup_cache (); |
8d08fdba | 2524 | |
9cd64686 MM |
2525 | VARRAY_FREE (current_lang_base); |
2526 | ||
2527 | scope_chain = s->prev; | |
87e3dbc9 | 2528 | for (t = s->old_bindings; t; t = TREE_CHAIN (t)) |
8d08fdba MS |
2529 | { |
2530 | tree id = TREE_VEC_ELT (t, 0); | |
31a714f6 MM |
2531 | |
2532 | SET_IDENTIFIER_TYPE_VALUE (id, TREE_VEC_ELT (t, 1)); | |
2533 | IDENTIFIER_BINDING (id) = TREE_VEC_ELT (t, 2); | |
2534 | IDENTIFIER_CLASS_VALUE (id) = TREE_VEC_ELT (t, 3); | |
8d08fdba | 2535 | } |
9cd64686 | 2536 | |
a8f73d4b MM |
2537 | /* If we were in the middle of compiling a function, restore our |
2538 | state. */ | |
2539 | if (s->need_pop_function_context) | |
2540 | pop_function_context_from (NULL_TREE); | |
2541 | current_function_decl = s->function_decl; | |
0f4237c2 | 2542 | last_function_parms = s->last_parms; |
e349ee73 | 2543 | |
a8f73d4b | 2544 | free (s); |
8d08fdba MS |
2545 | } |
2546 | \f | |
2547 | /* Push a definition of struct, union or enum tag "name". | |
68642fb6 | 2548 | into binding_level "b". "type" should be the type node, |
8d08fdba MS |
2549 | We assume that the tag "name" is not already defined. |
2550 | ||
2551 | Note that the definition may really be just a forward reference. | |
2552 | In that case, the TYPE_SIZE will be a NULL_TREE. | |
2553 | ||
e92cc029 | 2554 | C++ gratuitously puts all these tags in the name space. */ |
8d08fdba MS |
2555 | |
2556 | /* When setting the IDENTIFIER_TYPE_VALUE field of an identifier ID, | |
2557 | record the shadowed value for this binding contour. TYPE is | |
2558 | the type that ID maps to. */ | |
2559 | ||
2560 | static void | |
2561 | set_identifier_type_value_with_scope (id, type, b) | |
2562 | tree id; | |
2563 | tree type; | |
2564 | struct binding_level *b; | |
2565 | { | |
2c73f9f5 | 2566 | if (!b->namespace_p) |
8d08fdba | 2567 | { |
2c73f9f5 ML |
2568 | /* Shadow the marker, not the real thing, so that the marker |
2569 | gets restored later. */ | |
2570 | tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id); | |
8d08fdba MS |
2571 | b->type_shadowed |
2572 | = tree_cons (id, old_type_value, b->type_shadowed); | |
2573 | } | |
2c73f9f5 ML |
2574 | else |
2575 | { | |
2576 | tree binding = binding_for_name (id, current_namespace); | |
2577 | BINDING_TYPE (binding) = type; | |
2578 | /* Store marker instead of real type. */ | |
2579 | type = global_type_node; | |
2580 | } | |
8d08fdba MS |
2581 | SET_IDENTIFIER_TYPE_VALUE (id, type); |
2582 | } | |
2583 | ||
8f032717 | 2584 | /* As set_identifier_type_value_with_scope, but using current_binding_level. */ |
8d08fdba MS |
2585 | |
2586 | void | |
2587 | set_identifier_type_value (id, type) | |
2588 | tree id; | |
2589 | tree type; | |
2590 | { | |
8f032717 | 2591 | set_identifier_type_value_with_scope (id, type, current_binding_level); |
8d08fdba MS |
2592 | } |
2593 | ||
2c73f9f5 ML |
2594 | /* Return the type associated with id. */ |
2595 | ||
2596 | tree | |
2597 | identifier_type_value (id) | |
2598 | tree id; | |
2599 | { | |
2600 | /* There is no type with that name, anywhere. */ | |
2601 | if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE) | |
2602 | return NULL_TREE; | |
2603 | /* This is not the type marker, but the real thing. */ | |
2604 | if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node) | |
2605 | return REAL_IDENTIFIER_TYPE_VALUE (id); | |
2606 | /* Have to search for it. It must be on the global level, now. | |
2607 | Ask lookup_name not to return non-types. */ | |
3e3f722c | 2608 | id = lookup_name_real (id, 2, 1, 0); |
2c73f9f5 ML |
2609 | if (id) |
2610 | return TREE_TYPE (id); | |
2611 | return NULL_TREE; | |
2612 | } | |
2613 | ||
a9aedbc2 MS |
2614 | /* Pop off extraneous binding levels left over due to syntax errors. |
2615 | ||
2616 | We don't pop past namespaces, as they might be valid. */ | |
e92cc029 | 2617 | |
8926095f MS |
2618 | void |
2619 | pop_everything () | |
2620 | { | |
2621 | #ifdef DEBUG_CP_BINDING_LEVELS | |
2622 | fprintf (stderr, "XXX entering pop_everything ()\n"); | |
2623 | #endif | |
8f032717 | 2624 | while (!toplevel_bindings_p ()) |
8926095f | 2625 | { |
8f032717 | 2626 | if (current_binding_level->parm_flag == 2) |
b74a0560 | 2627 | pop_nested_class (); |
8926095f MS |
2628 | else |
2629 | poplevel (0, 0, 0); | |
2630 | } | |
2631 | #ifdef DEBUG_CP_BINDING_LEVELS | |
2632 | fprintf (stderr, "XXX leaving pop_everything ()\n"); | |
2633 | #endif | |
2634 | } | |
2635 | ||
39c01e4c MM |
2636 | /* The type TYPE is being declared. If it is a class template, or a |
2637 | specialization of a class template, do any processing required and | |
2638 | perform error-checking. If IS_FRIEND is non-zero, this TYPE is | |
2639 | being declared a friend. B is the binding level at which this TYPE | |
2640 | should be bound. | |
2641 | ||
2642 | Returns the TYPE_DECL for TYPE, which may have been altered by this | |
2643 | processing. */ | |
2644 | ||
68642fb6 | 2645 | static tree |
39c01e4c MM |
2646 | maybe_process_template_type_declaration (type, globalize, b) |
2647 | tree type; | |
2648 | int globalize; | |
2649 | struct binding_level* b; | |
2650 | { | |
2651 | tree decl = TYPE_NAME (type); | |
68642fb6 | 2652 | |
39c01e4c MM |
2653 | if (processing_template_parmlist) |
2654 | /* You can't declare a new template type in a template parameter | |
2655 | list. But, you can declare a non-template type: | |
68642fb6 | 2656 | |
39c01e4c | 2657 | template <class A*> struct S; |
68642fb6 | 2658 | |
39c01e4c MM |
2659 | is a forward-declaration of `A'. */ |
2660 | ; | |
68642fb6 | 2661 | else |
39c01e4c MM |
2662 | { |
2663 | maybe_check_template_type (type); | |
2664 | ||
68642fb6 | 2665 | my_friendly_assert (IS_AGGR_TYPE (type) |
ed44da02 | 2666 | || TREE_CODE (type) == ENUMERAL_TYPE, 0); |
68642fb6 UD |
2667 | |
2668 | ||
3ebc5c52 | 2669 | if (processing_template_decl) |
39c01e4c MM |
2670 | { |
2671 | /* This may change after the call to | |
2672 | push_template_decl_real, but we want the original value. */ | |
2673 | tree name = DECL_NAME (decl); | |
2674 | ||
2675 | decl = push_template_decl_real (decl, globalize); | |
2676 | /* If the current binding level is the binding level for the | |
2677 | template parameters (see the comment in | |
2678 | begin_template_parm_list) and the enclosing level is a class | |
2679 | scope, and we're not looking at a friend, push the | |
2680 | declaration of the member class into the class scope. In the | |
2681 | friend case, push_template_decl will already have put the | |
2682 | friend into global scope, if appropriate. */ | |
ed44da02 | 2683 | if (TREE_CODE (type) != ENUMERAL_TYPE |
74b846e0 | 2684 | && !globalize && b->template_parms_p |
39c01e4c MM |
2685 | && b->level_chain->parm_flag == 2) |
2686 | { | |
61a127b3 | 2687 | finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type)); |
39c01e4c MM |
2688 | /* Put this tag on the list of tags for the class, since |
2689 | that won't happen below because B is not the class | |
2690 | binding level, but is instead the pseudo-global level. */ | |
68642fb6 | 2691 | b->level_chain->tags = |
e1b3e07d | 2692 | tree_cons (name, type, b->level_chain->tags); |
d0f062fb | 2693 | if (!COMPLETE_TYPE_P (current_class_type)) |
39c01e4c MM |
2694 | CLASSTYPE_TAGS (current_class_type) = b->level_chain->tags; |
2695 | } | |
2696 | } | |
2697 | } | |
2698 | ||
2699 | return decl; | |
2700 | } | |
2701 | ||
9188c363 MM |
2702 | /* In C++, you don't have to write `struct S' to refer to `S'; you |
2703 | can just use `S'. We accomplish this by creating a TYPE_DECL as | |
2704 | if the user had written `typedef struct S S'. Create and return | |
2705 | the TYPE_DECL for TYPE. */ | |
2706 | ||
2707 | tree | |
2708 | create_implicit_typedef (name, type) | |
2709 | tree name; | |
2710 | tree type; | |
2711 | { | |
2712 | tree decl; | |
2713 | ||
2714 | decl = build_decl (TYPE_DECL, name, type); | |
c727aa5e | 2715 | DECL_ARTIFICIAL (decl) = 1; |
9188c363 MM |
2716 | /* There are other implicit type declarations, like the one *within* |
2717 | a class that allows you to write `S::S'. We must distinguish | |
2718 | amongst these. */ | |
2719 | SET_DECL_IMPLICIT_TYPEDEF_P (decl); | |
2720 | TYPE_NAME (type) = decl; | |
2721 | ||
2722 | return decl; | |
2723 | } | |
2724 | ||
8e4ce833 JJ |
2725 | /* Remember a local name for name-mangling purposes. */ |
2726 | ||
2727 | static void | |
2728 | push_local_name (decl) | |
2729 | tree decl; | |
2730 | { | |
2731 | size_t i, nelts; | |
2732 | tree t, name; | |
2733 | ||
2734 | if (!local_names) | |
2735 | VARRAY_TREE_INIT (local_names, 8, "local_names"); | |
2736 | ||
2737 | name = DECL_NAME (decl); | |
2738 | ||
2739 | nelts = VARRAY_ACTIVE_SIZE (local_names); | |
2740 | for (i = 0; i < nelts; i++) | |
2741 | { | |
2742 | t = VARRAY_TREE (local_names, i); | |
2743 | if (DECL_NAME (t) == name) | |
2744 | { | |
2745 | if (!DECL_LANG_SPECIFIC (decl)) | |
2746 | retrofit_lang_decl (decl); | |
2747 | if (DECL_LANG_SPECIFIC (t)) | |
2748 | DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1; | |
2749 | else | |
2750 | DECL_DISCRIMINATOR (decl) = 1; | |
2751 | ||
2752 | VARRAY_TREE (local_names, i) = decl; | |
2753 | return; | |
2754 | } | |
2755 | } | |
2756 | ||
2757 | VARRAY_PUSH_TREE (local_names, decl); | |
2758 | } | |
2759 | ||
8d08fdba | 2760 | /* Push a tag name NAME for struct/class/union/enum type TYPE. |
6757edfe | 2761 | Normally put it into the inner-most non-tag-transparent scope, |
8d08fdba | 2762 | but if GLOBALIZE is true, put it in the inner-most non-class scope. |
e92cc029 | 2763 | The latter is needed for implicit declarations. */ |
8d08fdba MS |
2764 | |
2765 | void | |
2766 | pushtag (name, type, globalize) | |
2767 | tree name, type; | |
2768 | int globalize; | |
2769 | { | |
2770 | register struct binding_level *b; | |
8d08fdba | 2771 | |
8f032717 | 2772 | b = current_binding_level; |
8d08fdba | 2773 | while (b->tag_transparent |
fcebb2dd NS |
2774 | || (b->parm_flag == 2 |
2775 | && (globalize | |
2776 | /* We may be defining a new type in the initializer | |
2777 | of a static member variable. We allow this when | |
2778 | not pedantic, and it is particularly useful for | |
2779 | type punning via an anonymous union. */ | |
2780 | || COMPLETE_TYPE_P (b->this_class)))) | |
8d08fdba MS |
2781 | b = b->level_chain; |
2782 | ||
e1b3e07d | 2783 | b->tags = tree_cons (name, type, b->tags); |
8d08fdba MS |
2784 | |
2785 | if (name) | |
2786 | { | |
8d08fdba | 2787 | /* Do C++ gratuitous typedefing. */ |
db5ae43f | 2788 | if (IDENTIFIER_TYPE_VALUE (name) != type) |
8d08fdba | 2789 | { |
93cdc044 | 2790 | register tree d = NULL_TREE; |
848cf1e0 | 2791 | int in_class = 0; |
9188c363 | 2792 | tree context = TYPE_CONTEXT (type); |
280f9385 | 2793 | |
280f9385 MM |
2794 | if (! context) |
2795 | { | |
2796 | tree cs = current_scope (); | |
2797 | ||
2798 | if (! globalize) | |
2799 | context = cs; | |
2f939d94 | 2800 | else if (cs != NULL_TREE && TYPE_P (cs)) |
280f9385 MM |
2801 | /* When declaring a friend class of a local class, we want |
2802 | to inject the newly named class into the scope | |
2803 | containing the local class, not the namespace scope. */ | |
4f1c5b7d | 2804 | context = decl_function_context (get_type_decl (cs)); |
280f9385 | 2805 | } |
280f9385 MM |
2806 | if (!context) |
2807 | context = current_namespace; | |
8d08fdba | 2808 | |
74b846e0 | 2809 | if ((b->template_parms_p && b->level_chain->parm_flag == 2) |
93cdc044 JM |
2810 | || b->parm_flag == 2) |
2811 | in_class = 1; | |
93cdc044 | 2812 | |
848cf1e0 MM |
2813 | if (current_lang_name == lang_name_java) |
2814 | TYPE_FOR_JAVA (type) = 1; | |
93cdc044 | 2815 | |
9188c363 | 2816 | d = create_implicit_typedef (name, type); |
cb0dbb9a | 2817 | DECL_CONTEXT (d) = FROB_CONTEXT (context); |
9188c363 MM |
2818 | if (! in_class) |
2819 | set_identifier_type_value_with_scope (name, type, b); | |
e1cd6e56 | 2820 | |
39c01e4c MM |
2821 | d = maybe_process_template_type_declaration (type, |
2822 | globalize, b); | |
93cdc044 JM |
2823 | |
2824 | if (b->parm_flag == 2) | |
61a127b3 | 2825 | { |
848cf1e0 | 2826 | if (!PROCESSING_REAL_TEMPLATE_DECL_P ()) |
61a127b3 MM |
2827 | /* Put this TYPE_DECL on the TYPE_FIELDS list for the |
2828 | class. But if it's a member template class, we | |
2829 | want the TEMPLATE_DECL, not the TYPE_DECL, so this | |
2830 | is done later. */ | |
2831 | finish_member_declaration (d); | |
8f032717 MM |
2832 | else |
2833 | pushdecl_class_level (d); | |
61a127b3 | 2834 | } |
93cdc044 JM |
2835 | else |
2836 | d = pushdecl_with_scope (d, b); | |
2837 | ||
1951a1b6 | 2838 | /* FIXME what if it gets a name from typedef? */ |
848cf1e0 MM |
2839 | if (ANON_AGGRNAME_P (name)) |
2840 | DECL_IGNORED_P (d) = 1; | |
2841 | ||
2842 | TYPE_CONTEXT (type) = DECL_CONTEXT (d); | |
1f6e1acc AS |
2843 | |
2844 | /* If this is a local class, keep track of it. We need this | |
2845 | information for name-mangling, and so that it is possible to find | |
2846 | all function definitions in a translation unit in a convenient | |
2847 | way. (It's otherwise tricky to find a member function definition | |
2848 | it's only pointed to from within a local class.) */ | |
5362b086 | 2849 | if (TYPE_CONTEXT (type) |
089acd57 MM |
2850 | && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL |
2851 | && !processing_template_decl) | |
1f6e1acc | 2852 | VARRAY_PUSH_TREE (local_classes, type); |
8d08fdba MS |
2853 | } |
2854 | if (b->parm_flag == 2) | |
2855 | { | |
d0f062fb | 2856 | if (!COMPLETE_TYPE_P (current_class_type)) |
8d08fdba MS |
2857 | CLASSTYPE_TAGS (current_class_type) = b->tags; |
2858 | } | |
2859 | } | |
2860 | ||
2861 | if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL) | |
2862 | /* Use the canonical TYPE_DECL for this node. */ | |
2863 | TYPE_STUB_DECL (type) = TYPE_NAME (type); | |
2864 | else | |
2865 | { | |
2866 | /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE | |
2867 | will be the tagged type we just added to the current | |
2868 | binding level. This fake NULL-named TYPE_DECL node helps | |
2869 | dwarfout.c to know when it needs to output a | |
2870 | representation of a tagged type, and it also gives us a | |
2871 | convenient place to record the "scope start" address for | |
2872 | the tagged type. */ | |
2873 | ||
8d08fdba | 2874 | tree d = build_decl (TYPE_DECL, NULL_TREE, type); |
8d08fdba MS |
2875 | TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b); |
2876 | } | |
2877 | } | |
2878 | ||
2879 | /* Counter used to create anonymous type names. */ | |
e92cc029 | 2880 | |
8d08fdba MS |
2881 | static int anon_cnt = 0; |
2882 | ||
2883 | /* Return an IDENTIFIER which can be used as a name for | |
2884 | anonymous structs and unions. */ | |
e92cc029 | 2885 | |
8d08fdba MS |
2886 | tree |
2887 | make_anon_name () | |
2888 | { | |
2889 | char buf[32]; | |
2890 | ||
2891 | sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++); | |
2892 | return get_identifier (buf); | |
2893 | } | |
2894 | ||
2895 | /* Clear the TREE_PURPOSE slot of tags which have anonymous typenames. | |
2896 | This keeps dbxout from getting confused. */ | |
e92cc029 | 2897 | |
8d08fdba MS |
2898 | void |
2899 | clear_anon_tags () | |
2900 | { | |
2901 | register struct binding_level *b; | |
2902 | register tree tags; | |
2903 | static int last_cnt = 0; | |
2904 | ||
2905 | /* Fast out if no new anon names were declared. */ | |
2906 | if (last_cnt == anon_cnt) | |
2907 | return; | |
2908 | ||
2909 | b = current_binding_level; | |
2910 | while (b->tag_transparent) | |
2911 | b = b->level_chain; | |
2912 | tags = b->tags; | |
2913 | while (tags) | |
2914 | { | |
2915 | /* A NULL purpose means we have already processed all tags | |
2916 | from here to the end of the list. */ | |
2917 | if (TREE_PURPOSE (tags) == NULL_TREE) | |
2918 | break; | |
2919 | if (ANON_AGGRNAME_P (TREE_PURPOSE (tags))) | |
2920 | TREE_PURPOSE (tags) = NULL_TREE; | |
2921 | tags = TREE_CHAIN (tags); | |
2922 | } | |
2923 | last_cnt = anon_cnt; | |
2924 | } | |
2925 | \f | |
2926 | /* Subroutine of duplicate_decls: return truthvalue of whether | |
2927 | or not types of these decls match. | |
2928 | ||
2929 | For C++, we must compare the parameter list so that `int' can match | |
2930 | `int&' in a parameter position, but `int&' is not confused with | |
2931 | `const int&'. */ | |
e92cc029 | 2932 | |
6060a796 | 2933 | int |
8d08fdba MS |
2934 | decls_match (newdecl, olddecl) |
2935 | tree newdecl, olddecl; | |
2936 | { | |
2937 | int types_match; | |
2938 | ||
347d73d7 ML |
2939 | if (newdecl == olddecl) |
2940 | return 1; | |
2941 | ||
6b4b3deb MM |
2942 | if (TREE_CODE (newdecl) != TREE_CODE (olddecl)) |
2943 | /* If the two DECLs are not even the same kind of thing, we're not | |
2944 | interested in their types. */ | |
2945 | return 0; | |
2946 | ||
2947 | if (TREE_CODE (newdecl) == FUNCTION_DECL) | |
8d08fdba MS |
2948 | { |
2949 | tree f1 = TREE_TYPE (newdecl); | |
2950 | tree f2 = TREE_TYPE (olddecl); | |
2951 | tree p1 = TYPE_ARG_TYPES (f1); | |
2952 | tree p2 = TYPE_ARG_TYPES (f2); | |
2953 | ||
4f1c5b7d | 2954 | if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl) |
eb68cb58 MM |
2955 | && ! (DECL_EXTERN_C_P (newdecl) |
2956 | && DECL_EXTERN_C_P (olddecl))) | |
c5a6fc45 JM |
2957 | return 0; |
2958 | ||
8d08fdba | 2959 | if (TREE_CODE (f1) != TREE_CODE (f2)) |
e867a179 | 2960 | return 0; |
8d08fdba | 2961 | |
3bfdc719 | 2962 | if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2))) |
8926095f | 2963 | { |
8b27e9ef NS |
2964 | if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl) |
2965 | && (DECL_BUILT_IN (olddecl) | |
2966 | #ifndef NO_IMPLICIT_EXTERN_C | |
2967 | || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl)) | |
2968 | || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl)) | |
2969 | #endif | |
2970 | )) | |
a28e3c7f MS |
2971 | { |
2972 | types_match = self_promoting_args_p (p1); | |
2973 | if (p1 == void_list_node) | |
2974 | TREE_TYPE (newdecl) = TREE_TYPE (olddecl); | |
2975 | } | |
8b27e9ef NS |
2976 | #ifndef NO_IMPLICIT_EXTERN_C |
2977 | else if (p1 == NULL_TREE | |
2978 | && (DECL_EXTERN_C_P (olddecl) | |
2979 | && DECL_IN_SYSTEM_HEADER (olddecl) | |
2980 | && !DECL_CLASS_SCOPE_P (olddecl)) | |
2981 | && (DECL_EXTERN_C_P (newdecl) | |
2982 | && DECL_IN_SYSTEM_HEADER (newdecl) | |
2983 | && !DECL_CLASS_SCOPE_P (newdecl))) | |
a28e3c7f MS |
2984 | { |
2985 | types_match = self_promoting_args_p (p2); | |
2986 | TREE_TYPE (newdecl) = TREE_TYPE (olddecl); | |
2987 | } | |
8b27e9ef | 2988 | #endif |
8926095f | 2989 | else |
91063b51 | 2990 | types_match = compparms (p1, p2); |
8926095f | 2991 | } |
8d08fdba MS |
2992 | else |
2993 | types_match = 0; | |
2994 | } | |
6b4b3deb | 2995 | else if (TREE_CODE (newdecl) == TEMPLATE_DECL) |
51c184be | 2996 | { |
f84b4be9 JM |
2997 | if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl), |
2998 | DECL_TEMPLATE_PARMS (olddecl))) | |
2999 | return 0; | |
68642fb6 | 3000 | |
2bb5d995 JM |
3001 | if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) |
3002 | != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))) | |
3003 | return 0; | |
3004 | ||
f84b4be9 JM |
3005 | if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL) |
3006 | types_match = 1; | |
3007 | else | |
3008 | types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl), | |
3009 | DECL_TEMPLATE_RESULT (newdecl)); | |
51c184be | 3010 | } |
8d08fdba MS |
3011 | else |
3012 | { | |
3013 | if (TREE_TYPE (newdecl) == error_mark_node) | |
3014 | types_match = TREE_TYPE (olddecl) == error_mark_node; | |
3015 | else if (TREE_TYPE (olddecl) == NULL_TREE) | |
3016 | types_match = TREE_TYPE (newdecl) == NULL_TREE; | |
a0a33927 MS |
3017 | else if (TREE_TYPE (newdecl) == NULL_TREE) |
3018 | types_match = 0; | |
8d08fdba | 3019 | else |
01240200 | 3020 | types_match = comptypes (TREE_TYPE (newdecl), |
3bfdc719 MM |
3021 | TREE_TYPE (olddecl), |
3022 | COMPARE_REDECLARATION); | |
8d08fdba MS |
3023 | } |
3024 | ||
3025 | return types_match; | |
3026 | } | |
3027 | ||
3028 | /* If NEWDECL is `static' and an `extern' was seen previously, | |
24f30ed4 | 3029 | warn about it. OLDDECL is the previous declaration. |
8d08fdba MS |
3030 | |
3031 | Note that this does not apply to the C++ case of declaring | |
3032 | a variable `extern const' and then later `const'. | |
3033 | ||
8d08fdba MS |
3034 | Don't complain about built-in functions, since they are beyond |
3035 | the user's control. */ | |
3036 | ||
3037 | static void | |
3038 | warn_extern_redeclared_static (newdecl, olddecl) | |
3039 | tree newdecl, olddecl; | |
3040 | { | |
83182544 | 3041 | static const char *const explicit_extern_static_warning |
8251199e | 3042 | = "`%D' was declared `extern' and later `static'"; |
83182544 | 3043 | static const char *const implicit_extern_static_warning |
8251199e JM |
3044 | = "`%D' was declared implicitly `extern' and later `static'"; |
3045 | ||
24f30ed4 MM |
3046 | tree name; |
3047 | ||
68642fb6 | 3048 | if (TREE_CODE (newdecl) == TYPE_DECL |
655dc6ee JM |
3049 | || TREE_CODE (newdecl) == TEMPLATE_DECL |
3050 | || TREE_CODE (newdecl) == CONST_DECL) | |
8d08fdba | 3051 | return; |
68642fb6 | 3052 | |
963d5758 MM |
3053 | /* Don't get confused by static member functions; that's a different |
3054 | use of `static'. */ | |
3055 | if (TREE_CODE (newdecl) == FUNCTION_DECL | |
3056 | && DECL_STATIC_FUNCTION_P (newdecl)) | |
3057 | return; | |
8d08fdba | 3058 | |
24f30ed4 MM |
3059 | /* If the old declaration was `static', or the new one isn't, then |
3060 | then everything is OK. */ | |
3061 | if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl)) | |
3062 | return; | |
3063 | ||
3064 | /* It's OK to declare a builtin function as `static'. */ | |
3065 | if (TREE_CODE (olddecl) == FUNCTION_DECL | |
3066 | && DECL_ARTIFICIAL (olddecl)) | |
3067 | return; | |
3068 | ||
8d08fdba | 3069 | name = DECL_ASSEMBLER_NAME (newdecl); |
33bd39a2 | 3070 | pedwarn (IDENTIFIER_IMPLICIT_DECL (name) |
24f30ed4 MM |
3071 | ? implicit_extern_static_warning |
3072 | : explicit_extern_static_warning, newdecl); | |
3073 | cp_pedwarn_at ("previous declaration of `%D'", olddecl); | |
8d08fdba MS |
3074 | } |
3075 | ||
3076 | /* Handle when a new declaration NEWDECL has the same name as an old | |
3077 | one OLDDECL in the same binding contour. Prints an error message | |
3078 | if appropriate. | |
3079 | ||
3080 | If safely possible, alter OLDDECL to look like NEWDECL, and return 1. | |
3081 | Otherwise, return 0. */ | |
3082 | ||
51c184be | 3083 | int |
8d08fdba | 3084 | duplicate_decls (newdecl, olddecl) |
824b9a4c | 3085 | tree newdecl, olddecl; |
8d08fdba | 3086 | { |
8d08fdba MS |
3087 | unsigned olddecl_uid = DECL_UID (olddecl); |
3088 | int olddecl_friend = 0, types_match = 0; | |
0b60dfe3 | 3089 | int new_defines_function = 0; |
5566b478 MS |
3090 | |
3091 | if (newdecl == olddecl) | |
3092 | return 1; | |
8d08fdba | 3093 | |
8926095f | 3094 | types_match = decls_match (newdecl, olddecl); |
8d08fdba | 3095 | |
8d08fdba MS |
3096 | /* If either the type of the new decl or the type of the old decl is an |
3097 | error_mark_node, then that implies that we have already issued an | |
3098 | error (earlier) for some bogus type specification, and in that case, | |
3099 | it is rather pointless to harass the user with yet more error message | |
0b60dfe3 | 3100 | about the same declaration, so just pretend the types match here. */ |
bd6dd845 MS |
3101 | if (TREE_TYPE (newdecl) == error_mark_node |
3102 | || TREE_TYPE (olddecl) == error_mark_node) | |
8d08fdba | 3103 | types_match = 1; |
68642fb6 | 3104 | |
97055d5c AO |
3105 | if (DECL_P (olddecl) |
3106 | && TREE_CODE (newdecl) == FUNCTION_DECL | |
3107 | && TREE_CODE (olddecl) == FUNCTION_DECL | |
3108 | && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl))) | |
3109 | { | |
3110 | if (DECL_DECLARED_INLINE_P (newdecl) | |
3111 | && DECL_UNINLINABLE (newdecl) | |
3112 | && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))) | |
3113 | /* Already warned elsewhere. */; | |
3114 | else if (DECL_DECLARED_INLINE_P (olddecl) | |
3115 | && DECL_UNINLINABLE (olddecl) | |
3116 | && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl))) | |
3117 | /* Already warned. */; | |
3118 | else if (DECL_DECLARED_INLINE_P (newdecl) | |
3119 | && DECL_UNINLINABLE (olddecl) | |
3120 | && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl))) | |
3121 | { | |
3122 | warning_with_decl (newdecl, | |
3123 | "function `%s' redeclared as inline"); | |
3124 | warning_with_decl (olddecl, | |
3125 | "previous declaration of function `%s' with attribute noinline"); | |
3126 | } | |
3127 | else if (DECL_DECLARED_INLINE_P (olddecl) | |
3128 | && DECL_UNINLINABLE (newdecl) | |
3129 | && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))) | |
3130 | { | |
3131 | warning_with_decl (newdecl, | |
3132 | "function `%s' redeclared with attribute noinline"); | |
3133 | warning_with_decl (olddecl, | |
3134 | "previous declaration of function `%s' was inline"); | |
3135 | } | |
3136 | } | |
3137 | ||
0b60dfe3 | 3138 | /* Check for redeclaration and other discrepancies. */ |
d22c8596 | 3139 | if (TREE_CODE (olddecl) == FUNCTION_DECL |
9f33663b JM |
3140 | && DECL_ARTIFICIAL (olddecl)) |
3141 | { | |
3142 | if (TREE_CODE (newdecl) != FUNCTION_DECL) | |
3143 | { | |
d52e4867 RS |
3144 | /* Avoid warnings redeclaring anticipated built-ins. */ |
3145 | if (DECL_ANTICIPATED (olddecl)) | |
3146 | return 0; | |
3147 | ||
9f33663b JM |
3148 | /* If you declare a built-in or predefined function name as static, |
3149 | the old definition is overridden, but optionally warn this was a | |
3150 | bad choice of name. */ | |
3151 | if (! TREE_PUBLIC (newdecl)) | |
3152 | { | |
3153 | if (warn_shadow) | |
33bd39a2 | 3154 | warning ("shadowing %s function `%#D'", |
9f33663b JM |
3155 | DECL_BUILT_IN (olddecl) ? "built-in" : "library", |
3156 | olddecl); | |
3157 | /* Discard the old built-in function. */ | |
3158 | return 0; | |
3159 | } | |
3160 | /* If the built-in is not ansi, then programs can override | |
3161 | it even globally without an error. */ | |
3162 | else if (! DECL_BUILT_IN (olddecl)) | |
33bd39a2 | 3163 | warning ("library function `%#D' redeclared as non-function `%#D'", |
9f33663b JM |
3164 | olddecl, newdecl); |
3165 | else | |
3166 | { | |
33bd39a2 ZW |
3167 | error ("declaration of `%#D'", newdecl); |
3168 | error ("conflicts with built-in declaration `%#D'", | |
a4443a08 | 3169 | olddecl); |
9f33663b | 3170 | } |
a4443a08 MS |
3171 | return 0; |
3172 | } | |
9f33663b | 3173 | else if (!types_match) |
8d08fdba | 3174 | { |
d52e4867 RS |
3175 | /* Avoid warnings redeclaring anticipated built-ins. */ |
3176 | if (DECL_ANTICIPATED (olddecl)) | |
3177 | ; /* Do nothing yet. */ | |
3178 | else if ((DECL_EXTERN_C_P (newdecl) | |
eb68cb58 | 3179 | && DECL_EXTERN_C_P (olddecl)) |
9f33663b JM |
3180 | || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)), |
3181 | TYPE_ARG_TYPES (TREE_TYPE (olddecl)))) | |
a4443a08 | 3182 | { |
9f33663b JM |
3183 | /* A near match; override the builtin. */ |
3184 | ||
3185 | if (TREE_PUBLIC (newdecl)) | |
a4443a08 | 3186 | { |
33bd39a2 ZW |
3187 | warning ("new declaration `%#D'", newdecl); |
3188 | warning ("ambiguates built-in declaration `%#D'", | |
9f33663b | 3189 | olddecl); |
a4443a08 | 3190 | } |
9f33663b | 3191 | else if (warn_shadow) |
33bd39a2 | 3192 | warning ("shadowing %s function `%#D'", |
9f33663b JM |
3193 | DECL_BUILT_IN (olddecl) ? "built-in" : "library", |
3194 | olddecl); | |
a4443a08 | 3195 | } |
9f33663b JM |
3196 | else |
3197 | /* Discard the old built-in function. */ | |
3198 | return 0; | |
04a9d080 RS |
3199 | |
3200 | /* Replace the old RTL to avoid problems with inlining. */ | |
3201 | SET_DECL_RTL (olddecl, DECL_RTL (newdecl)); | |
8d08fdba | 3202 | } |
68642fb6 | 3203 | |
24f30ed4 MM |
3204 | if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl)) |
3205 | { | |
3206 | /* If a builtin function is redeclared as `static', merge | |
3207 | the declarations, but make the original one static. */ | |
3208 | DECL_THIS_STATIC (olddecl) = 1; | |
3209 | TREE_PUBLIC (olddecl) = 0; | |
9bfadf57 | 3210 | |
421844e7 MM |
3211 | /* Make the old declaration consistent with the new one so |
3212 | that all remnants of the builtin-ness of this function | |
3213 | will be banished. */ | |
5d2ed28c | 3214 | SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl)); |
19e7881c | 3215 | SET_DECL_RTL (olddecl, DECL_RTL (newdecl)); |
24f30ed4 | 3216 | } |
39211cd5 MS |
3217 | } |
3218 | else if (TREE_CODE (olddecl) != TREE_CODE (newdecl)) | |
3219 | { | |
9ed182dc JM |
3220 | if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl) |
3221 | && TREE_CODE (newdecl) != TYPE_DECL | |
3222 | && ! (TREE_CODE (newdecl) == TEMPLATE_DECL | |
3223 | && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)) | |
3224 | || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl) | |
3225 | && TREE_CODE (olddecl) != TYPE_DECL | |
3226 | && ! (TREE_CODE (olddecl) == TEMPLATE_DECL | |
3227 | && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) | |
3228 | == TYPE_DECL)))) | |
3229 | { | |
3230 | /* We do nothing special here, because C++ does such nasty | |
3231 | things with TYPE_DECLs. Instead, just let the TYPE_DECL | |
3232 | get shadowed, and know that if we need to find a TYPE_DECL | |
3233 | for a given name, we can look in the IDENTIFIER_TYPE_VALUE | |
3234 | slot of the identifier. */ | |
3235 | return 0; | |
3236 | } | |
3237 | ||
39211cd5 | 3238 | if ((TREE_CODE (newdecl) == FUNCTION_DECL |
5566b478 | 3239 | && DECL_FUNCTION_TEMPLATE_P (olddecl)) |
39211cd5 | 3240 | || (TREE_CODE (olddecl) == FUNCTION_DECL |
5566b478 | 3241 | && DECL_FUNCTION_TEMPLATE_P (newdecl))) |
39211cd5 | 3242 | return 0; |
9ed182dc | 3243 | |
33bd39a2 | 3244 | error ("`%#D' redeclared as different kind of symbol", newdecl); |
39211cd5 MS |
3245 | if (TREE_CODE (olddecl) == TREE_LIST) |
3246 | olddecl = TREE_VALUE (olddecl); | |
8251199e | 3247 | cp_error_at ("previous declaration of `%#D'", olddecl); |
39211cd5 MS |
3248 | |
3249 | /* New decl is completely inconsistent with the old one => | |
3250 | tell caller to replace the old one. */ | |
3251 | ||
3252 | return 0; | |
8d08fdba | 3253 | } |
8d08fdba MS |
3254 | else if (!types_match) |
3255 | { | |
4f1c5b7d | 3256 | if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)) |
7bdbfa05 MM |
3257 | /* These are certainly not duplicate declarations; they're |
3258 | from different scopes. */ | |
3259 | return 0; | |
3260 | ||
8926095f | 3261 | if (TREE_CODE (newdecl) == TEMPLATE_DECL) |
f0e01782 MS |
3262 | { |
3263 | /* The name of a class template may not be declared to refer to | |
3264 | any other template, class, function, object, namespace, value, | |
e92cc029 | 3265 | or type in the same scope. */ |
5566b478 MS |
3266 | if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL |
3267 | || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL) | |
f0e01782 | 3268 | { |
33bd39a2 | 3269 | error ("declaration of template `%#D'", newdecl); |
8251199e | 3270 | cp_error_at ("conflicts with previous declaration `%#D'", |
f0e01782 MS |
3271 | olddecl); |
3272 | } | |
ec255269 MS |
3273 | else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL |
3274 | && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL | |
3275 | && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))), | |
91063b51 | 3276 | TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl)))) |
75650646 MM |
3277 | && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl), |
3278 | DECL_TEMPLATE_PARMS (olddecl))) | |
ec255269 | 3279 | { |
33bd39a2 | 3280 | error ("new declaration `%#D'", newdecl); |
8251199e | 3281 | cp_error_at ("ambiguates old declaration `%#D'", olddecl); |
ec255269 | 3282 | } |
f0e01782 MS |
3283 | return 0; |
3284 | } | |
8926095f MS |
3285 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
3286 | { | |
eb68cb58 | 3287 | if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl)) |
8926095f | 3288 | { |
33bd39a2 | 3289 | error ("declaration of C function `%#D' conflicts with", |
8926095f | 3290 | newdecl); |
8251199e | 3291 | cp_error_at ("previous declaration `%#D' here", olddecl); |
8926095f | 3292 | } |
00595019 | 3293 | else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)), |
91063b51 | 3294 | TYPE_ARG_TYPES (TREE_TYPE (olddecl)))) |
700f8a87 | 3295 | { |
33bd39a2 | 3296 | error ("new declaration `%#D'", newdecl); |
8251199e | 3297 | cp_error_at ("ambiguates old declaration `%#D'", olddecl); |
700f8a87 MS |
3298 | } |
3299 | else | |
3300 | return 0; | |
8926095f | 3301 | } |
8d08fdba MS |
3302 | |
3303 | /* Already complained about this, so don't do so again. */ | |
a4443a08 | 3304 | else if (current_class_type == NULL_TREE |
8d08fdba MS |
3305 | || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type) |
3306 | { | |
33bd39a2 | 3307 | error ("conflicting types for `%#D'", newdecl); |
8251199e | 3308 | cp_error_at ("previous declaration as `%#D'", olddecl); |
8d08fdba MS |
3309 | } |
3310 | } | |
68642fb6 | 3311 | else if (TREE_CODE (newdecl) == FUNCTION_DECL |
75650646 MM |
3312 | && ((DECL_TEMPLATE_SPECIALIZATION (olddecl) |
3313 | && (!DECL_TEMPLATE_INFO (newdecl) | |
68642fb6 | 3314 | || (DECL_TI_TEMPLATE (newdecl) |
75650646 MM |
3315 | != DECL_TI_TEMPLATE (olddecl)))) |
3316 | || (DECL_TEMPLATE_SPECIALIZATION (newdecl) | |
3317 | && (!DECL_TEMPLATE_INFO (olddecl) | |
68642fb6 | 3318 | || (DECL_TI_TEMPLATE (olddecl) |
75650646 | 3319 | != DECL_TI_TEMPLATE (newdecl)))))) |
386b8a85 JM |
3320 | /* It's OK to have a template specialization and a non-template |
3321 | with the same type, or to have specializations of two | |
75650646 MM |
3322 | different templates with the same type. Note that if one is a |
3323 | specialization, and the other is an instantiation of the same | |
3324 | template, that we do not exit at this point. That situation | |
3325 | can occur if we instantiate a template class, and then | |
3326 | specialize one of its methods. This situation is legal, but | |
3327 | the declarations must be merged in the usual way. */ | |
3328 | return 0; | |
68642fb6 UD |
3329 | else if (TREE_CODE (newdecl) == FUNCTION_DECL |
3330 | && ((DECL_TEMPLATE_INSTANTIATION (olddecl) | |
75650646 MM |
3331 | && !DECL_USE_TEMPLATE (newdecl)) |
3332 | || (DECL_TEMPLATE_INSTANTIATION (newdecl) | |
3333 | && !DECL_USE_TEMPLATE (olddecl)))) | |
3334 | /* One of the declarations is a template instantiation, and the | |
3335 | other is not a template at all. That's OK. */ | |
386b8a85 | 3336 | return 0; |
85c6cbaf ML |
3337 | else if (TREE_CODE (newdecl) == NAMESPACE_DECL |
3338 | && DECL_NAMESPACE_ALIAS (newdecl) | |
3339 | && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl)) | |
3340 | /* Redeclaration of namespace alias, ignore it. */ | |
3341 | return 1; | |
8d08fdba MS |
3342 | else |
3343 | { | |
d8e178a0 | 3344 | const char *errmsg = redeclaration_error_message (newdecl, olddecl); |
8251199e | 3345 | if (errmsg) |
8d08fdba | 3346 | { |
33bd39a2 | 3347 | error (errmsg, newdecl); |
8d08fdba MS |
3348 | if (DECL_NAME (olddecl) != NULL_TREE) |
3349 | cp_error_at ((DECL_INITIAL (olddecl) | |
2c73f9f5 | 3350 | && namespace_bindings_p ()) |
8251199e JM |
3351 | ? "`%#D' previously defined here" |
3352 | : "`%#D' previously declared here", olddecl); | |
00fae85d | 3353 | return 0; |
8d08fdba MS |
3354 | } |
3355 | else if (TREE_CODE (olddecl) == FUNCTION_DECL | |
3356 | && DECL_INITIAL (olddecl) != NULL_TREE | |
3357 | && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE | |
3358 | && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE) | |
3359 | { | |
3360 | /* Prototype decl follows defn w/o prototype. */ | |
8251199e JM |
3361 | cp_warning_at ("prototype for `%#D'", newdecl); |
3362 | cp_warning_at ("follows non-prototype definition here", olddecl); | |
8d08fdba MS |
3363 | } |
3364 | else if (TREE_CODE (olddecl) == FUNCTION_DECL | |
3365 | && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl)) | |
8926095f MS |
3366 | { |
3367 | /* extern "C" int foo (); | |
3368 | int foo () { bar (); } | |
3369 | is OK. */ | |
46ccf50a | 3370 | if (current_lang_depth () == 0) |
5d2ed28c | 3371 | SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl)); |
8926095f MS |
3372 | else |
3373 | { | |
8251199e | 3374 | cp_error_at ("previous declaration of `%#D' with %L linkage", |
8926095f | 3375 | olddecl, DECL_LANGUAGE (olddecl)); |
33bd39a2 | 3376 | error ("conflicts with new declaration with %L linkage", |
8926095f MS |
3377 | DECL_LANGUAGE (newdecl)); |
3378 | } | |
3379 | } | |
e1cd6e56 | 3380 | |
a6f02587 | 3381 | if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl)) |
c32381b1 JM |
3382 | ; |
3383 | else if (TREE_CODE (olddecl) == FUNCTION_DECL) | |
e1cd6e56 MS |
3384 | { |
3385 | tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl)); | |
3386 | tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)); | |
3387 | int i = 1; | |
3388 | ||
3389 | if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE) | |
3390 | t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2); | |
68642fb6 | 3391 | |
e1cd6e56 MS |
3392 | for (; t1 && t1 != void_list_node; |
3393 | t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++) | |
3394 | if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2)) | |
3395 | { | |
2507f3b5 RK |
3396 | if (1 == simple_cst_equal (TREE_PURPOSE (t1), |
3397 | TREE_PURPOSE (t2))) | |
e1cd6e56 | 3398 | { |
624a5800 CR |
3399 | pedwarn ("default argument given for parameter %d of `%#D'", |
3400 | i, newdecl); | |
3401 | cp_pedwarn_at ("after previous specification in `%#D'", | |
3402 | olddecl); | |
e1cd6e56 MS |
3403 | } |
3404 | else | |
3405 | { | |
33bd39a2 | 3406 | error ("default argument given for parameter %d of `%#D'", |
e1cd6e56 | 3407 | i, newdecl); |
8251199e | 3408 | cp_error_at ("after previous specification in `%#D'", |
e1cd6e56 MS |
3409 | olddecl); |
3410 | } | |
3411 | } | |
a5894242 | 3412 | |
79065db2 MM |
3413 | if (DECL_DECLARED_INLINE_P (newdecl) |
3414 | && ! DECL_DECLARED_INLINE_P (olddecl) | |
7fcdf4c2 | 3415 | && TREE_ADDRESSABLE (olddecl) && warn_inline) |
dff6b454 | 3416 | { |
33bd39a2 | 3417 | warning ("`%#D' was used before it was declared inline", |
7fcdf4c2 | 3418 | newdecl); |
8251199e | 3419 | cp_warning_at ("previous non-inline declaration here", |
7fcdf4c2 | 3420 | olddecl); |
dff6b454 | 3421 | } |
e1cd6e56 | 3422 | } |
8d08fdba MS |
3423 | } |
3424 | ||
4782bd5b RS |
3425 | /* Do not merge an implicit typedef with an explicit one. In: |
3426 | ||
3427 | class A; | |
3428 | ... | |
3429 | typedef class A A __attribute__ ((foo)); | |
3430 | ||
3431 | the attribute should apply only to the typedef. */ | |
3432 | if (TREE_CODE (olddecl) == TYPE_DECL | |
3433 | && (DECL_IMPLICIT_TYPEDEF_P (olddecl) | |
3434 | || DECL_IMPLICIT_TYPEDEF_P (newdecl))) | |
3435 | return 0; | |
3436 | ||
8d08fdba MS |
3437 | /* If new decl is `static' and an `extern' was seen previously, |
3438 | warn about it. */ | |
3439 | warn_extern_redeclared_static (newdecl, olddecl); | |
3440 | ||
e92cc029 | 3441 | /* We have committed to returning 1 at this point. */ |
8d08fdba MS |
3442 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
3443 | { | |
3444 | /* Now that functions must hold information normally held | |
3445 | by field decls, there is extra work to do so that | |
3446 | declaration information does not get destroyed during | |
3447 | definition. */ | |
3448 | if (DECL_VINDEX (olddecl)) | |
3449 | DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl); | |
4f1c5b7d MM |
3450 | if (DECL_VIRTUAL_CONTEXT (olddecl)) |
3451 | DECL_VIRTUAL_CONTEXT (newdecl) = DECL_VIRTUAL_CONTEXT (olddecl); | |
8d08fdba MS |
3452 | if (DECL_CONTEXT (olddecl)) |
3453 | DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl); | |
0e5921e8 | 3454 | if (DECL_PENDING_INLINE_INFO (newdecl) == 0) |
8d08fdba | 3455 | DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl); |
e1cd6e56 MS |
3456 | DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl); |
3457 | DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl); | |
fee7654e | 3458 | DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl); |
864b83b9 | 3459 | DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl); |
4a67c9e9 | 3460 | DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl); |
963d5758 | 3461 | DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl); |
e0fff4b3 JM |
3462 | if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK) |
3463 | SET_OVERLOADED_OPERATOR_CODE | |
3464 | (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl)); | |
0b60dfe3 | 3465 | new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE; |
68642fb6 | 3466 | |
0b60dfe3 BK |
3467 | /* Optionally warn about more than one declaration for the same |
3468 | name, but don't warn about a function declaration followed by a | |
3469 | definition. */ | |
3470 | if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl) | |
3471 | && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE) | |
3472 | /* Don't warn about extern decl followed by definition. */ | |
3473 | && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl)) | |
3474 | /* Don't warn about friends, let add_friend take care of it. */ | |
3581fae0 | 3475 | && ! (DECL_FRIEND_P (newdecl) || DECL_FRIEND_P (olddecl))) |
0b60dfe3 | 3476 | { |
33bd39a2 | 3477 | warning ("redundant redeclaration of `%D' in same scope", newdecl); |
8251199e | 3478 | cp_warning_at ("previous declaration of `%D'", olddecl); |
0b60dfe3 | 3479 | } |
8d08fdba MS |
3480 | } |
3481 | ||
3482 | /* Deal with C++: must preserve virtual function table size. */ | |
3483 | if (TREE_CODE (olddecl) == TYPE_DECL) | |
3484 | { | |
3485 | register tree newtype = TREE_TYPE (newdecl); | |
3486 | register tree oldtype = TREE_TYPE (olddecl); | |
3487 | ||
3488 | if (newtype != error_mark_node && oldtype != error_mark_node | |
3489 | && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype)) | |
3490 | { | |
3491 | CLASSTYPE_VSIZE (newtype) = CLASSTYPE_VSIZE (oldtype); | |
3492 | CLASSTYPE_FRIEND_CLASSES (newtype) | |
3493 | = CLASSTYPE_FRIEND_CLASSES (oldtype); | |
3494 | } | |
970d6386 MM |
3495 | |
3496 | DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl); | |
8d08fdba MS |
3497 | } |
3498 | ||
8d08fdba MS |
3499 | /* Copy all the DECL_... slots specified in the new decl |
3500 | except for any that we copy here from the old type. */ | |
91d231cb | 3501 | DECL_ATTRIBUTES (newdecl) |
f6897b10 | 3502 | = (*targetm.merge_decl_attributes) (olddecl, newdecl); |
6aa77e6c | 3503 | decl_attributes (&newdecl, DECL_ATTRIBUTES (newdecl), 0); |
8d08fdba | 3504 | |
5566b478 MS |
3505 | if (TREE_CODE (newdecl) == TEMPLATE_DECL) |
3506 | { | |
4d85e00e | 3507 | TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl)); |
68642fb6 | 3508 | DECL_TEMPLATE_SPECIALIZATIONS (olddecl) |
4d85e00e MM |
3509 | = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl), |
3510 | DECL_TEMPLATE_SPECIALIZATIONS (newdecl)); | |
68642fb6 | 3511 | |
daa8df65 MM |
3512 | /* If the new declaration is a definition, update the file and |
3513 | line information on the declaration. */ | |
06d5e633 NS |
3514 | if (DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)) == NULL_TREE |
3515 | && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl)) != NULL_TREE) | |
daa8df65 MM |
3516 | { |
3517 | DECL_SOURCE_LINE (olddecl) | |
06d5e633 | 3518 | = DECL_SOURCE_LINE (DECL_TEMPLATE_RESULT (olddecl)) |
daa8df65 MM |
3519 | = DECL_SOURCE_LINE (newdecl); |
3520 | DECL_SOURCE_FILE (olddecl) | |
06d5e633 | 3521 | = DECL_SOURCE_FILE (DECL_TEMPLATE_RESULT (olddecl)) |
daa8df65 MM |
3522 | = DECL_SOURCE_FILE (newdecl); |
3523 | } | |
3524 | ||
5566b478 MS |
3525 | return 1; |
3526 | } | |
68642fb6 | 3527 | |
8d08fdba MS |
3528 | if (types_match) |
3529 | { | |
3530 | /* Automatically handles default parameters. */ | |
3531 | tree oldtype = TREE_TYPE (olddecl); | |
e1cd6e56 | 3532 | tree newtype; |
8d08fdba | 3533 | |
e1cd6e56 | 3534 | /* Merge the data types specified in the two decls. */ |
6da794e8 | 3535 | newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl)); |
e1cd6e56 | 3536 | |
6da794e8 | 3537 | /* If merge_types produces a non-typedef type, just use the old type. */ |
9076e292 JM |
3538 | if (TREE_CODE (newdecl) == TYPE_DECL |
3539 | && newtype == DECL_ORIGINAL_TYPE (newdecl)) | |
3540 | newtype = oldtype; | |
3541 | ||
8d08fdba MS |
3542 | if (TREE_CODE (newdecl) == VAR_DECL) |
3543 | DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl); | |
6da794e8 | 3544 | /* Do this after calling `merge_types' so that default |
8d08fdba MS |
3545 | parameters don't confuse us. */ |
3546 | else if (TREE_CODE (newdecl) == FUNCTION_DECL | |
3547 | && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)) | |
3548 | != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)))) | |
3549 | { | |
f30432d7 | 3550 | TREE_TYPE (newdecl) = build_exception_variant (newtype, |
8d08fdba | 3551 | TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))); |
f30432d7 | 3552 | TREE_TYPE (olddecl) = build_exception_variant (newtype, |
8d08fdba MS |
3553 | TYPE_RAISES_EXCEPTIONS (oldtype)); |
3554 | ||
9a224b4a JM |
3555 | if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl)) |
3556 | && DECL_SOURCE_LINE (olddecl) != 0 | |
da20811c | 3557 | && flag_exceptions |
4cc1d462 NS |
3558 | && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)), |
3559 | TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1)) | |
8d08fdba | 3560 | { |
33bd39a2 | 3561 | error ("declaration of `%F' throws different exceptions", |
a28e3c7f | 3562 | newdecl); |
a09ba2e0 | 3563 | cp_error_at ("than previous declaration `%F'", olddecl); |
8d08fdba MS |
3564 | } |
3565 | } | |
3566 | TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype; | |
3567 | ||
3568 | /* Lay the type out, unless already done. */ | |
407f03b8 | 3569 | if (! same_type_p (newtype, oldtype) |
5566b478 | 3570 | && TREE_TYPE (newdecl) != error_mark_node |
5156628f | 3571 | && !(processing_template_decl && uses_template_parms (newdecl))) |
b7484fbe MS |
3572 | layout_type (TREE_TYPE (newdecl)); |
3573 | ||
5566b478 MS |
3574 | if ((TREE_CODE (newdecl) == VAR_DECL |
3575 | || TREE_CODE (newdecl) == PARM_DECL | |
3576 | || TREE_CODE (newdecl) == RESULT_DECL | |
3577 | || TREE_CODE (newdecl) == FIELD_DECL | |
3578 | || TREE_CODE (newdecl) == TYPE_DECL) | |
5156628f | 3579 | && !(processing_template_decl && uses_template_parms (newdecl))) |
b7484fbe | 3580 | layout_decl (newdecl, 0); |
8d08fdba MS |
3581 | |
3582 | /* Merge the type qualifiers. */ | |
3583 | if (TREE_READONLY (newdecl)) | |
3584 | TREE_READONLY (olddecl) = 1; | |
3585 | if (TREE_THIS_VOLATILE (newdecl)) | |
3586 | TREE_THIS_VOLATILE (olddecl) = 1; | |
3587 | ||
3588 | /* Merge the initialization information. */ | |
8926095f MS |
3589 | if (DECL_INITIAL (newdecl) == NULL_TREE |
3590 | && DECL_INITIAL (olddecl) != NULL_TREE) | |
3591 | { | |
3592 | DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl); | |
3593 | DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl); | |
3594 | DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl); | |
b0d06515 MM |
3595 | if (CAN_HAVE_FULL_LANG_DECL_P (newdecl) |
3596 | && DECL_LANG_SPECIFIC (newdecl) | |
4d85e00e MM |
3597 | && DECL_LANG_SPECIFIC (olddecl)) |
3598 | DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl); | |
8926095f | 3599 | } |
39211cd5 MS |
3600 | |
3601 | /* Merge the section attribute. | |
3602 | We want to issue an error if the sections conflict but that must be | |
3603 | done later in decl_attributes since we are called before attributes | |
3604 | are assigned. */ | |
3605 | if (DECL_SECTION_NAME (newdecl) == NULL_TREE) | |
3606 | DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl); | |
3607 | ||
a157febd GK |
3608 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
3609 | { | |
3610 | DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl) | |
3611 | |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl); | |
a157febd GK |
3612 | DECL_NO_LIMIT_STACK (newdecl) |
3613 | |= DECL_NO_LIMIT_STACK (olddecl); | |
c740732f MM |
3614 | /* Keep the old RTL. */ |
3615 | COPY_DECL_RTL (olddecl, newdecl); | |
3616 | } | |
3617 | else if (TREE_CODE (newdecl) == VAR_DECL | |
3618 | && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl))) | |
3619 | { | |
3620 | /* Keep the old RTL. We cannot keep the old RTL if the old | |
3621 | declaration was for an incomplete object and the new | |
3622 | declaration is not since many attributes of the RTL will | |
3623 | change. */ | |
3624 | COPY_DECL_RTL (olddecl, newdecl); | |
a157febd | 3625 | } |
8d08fdba MS |
3626 | } |
3627 | /* If cannot merge, then use the new type and qualifiers, | |
3628 | and don't preserve the old rtl. */ | |
3629 | else | |
3630 | { | |
3631 | /* Clean out any memory we had of the old declaration. */ | |
3632 | tree oldstatic = value_member (olddecl, static_aggregates); | |
3633 | if (oldstatic) | |
3634 | TREE_VALUE (oldstatic) = error_mark_node; | |
3635 | ||
3636 | TREE_TYPE (olddecl) = TREE_TYPE (newdecl); | |
3637 | TREE_READONLY (olddecl) = TREE_READONLY (newdecl); | |
3638 | TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl); | |
3639 | TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl); | |
3640 | } | |
3641 | ||
3642 | /* Merge the storage class information. */ | |
a9aedbc2 | 3643 | DECL_WEAK (newdecl) |= DECL_WEAK (olddecl); |
e92cc029 | 3644 | DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl); |
794d4a61 | 3645 | DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl); |
a9aedbc2 MS |
3646 | TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl); |
3647 | TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl); | |
3648 | if (! DECL_EXTERNAL (olddecl)) | |
3649 | DECL_EXTERNAL (newdecl) = 0; | |
68642fb6 | 3650 | |
0b60dfe3 | 3651 | if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl)) |
8d08fdba | 3652 | { |
a9aedbc2 MS |
3653 | DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl); |
3654 | DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl); | |
77be6f82 | 3655 | DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl); |
68642fb6 | 3656 | DECL_TEMPLATE_INSTANTIATED (newdecl) |
2b0a63a3 | 3657 | |= DECL_TEMPLATE_INSTANTIATED (olddecl); |
0b60dfe3 BK |
3658 | /* Don't really know how much of the language-specific |
3659 | values we should copy from old to new. */ | |
3660 | DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl); | |
3661 | DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl); | |
3662 | DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl); | |
fbf1c34b | 3663 | DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl); |
3febd123 NS |
3664 | DECL_INITIALIZED_IN_CLASS_P (newdecl) |
3665 | |= DECL_INITIALIZED_IN_CLASS_P (olddecl); | |
0b60dfe3 | 3666 | olddecl_friend = DECL_FRIEND_P (olddecl); |
6a629cac MM |
3667 | |
3668 | /* Only functions have DECL_BEFRIENDING_CLASSES. */ | |
3669 | if (TREE_CODE (newdecl) == FUNCTION_DECL | |
3670 | || DECL_FUNCTION_TEMPLATE_P (newdecl)) | |
3671 | DECL_BEFRIENDING_CLASSES (newdecl) | |
3672 | = chainon (DECL_BEFRIENDING_CLASSES (newdecl), | |
3673 | DECL_BEFRIENDING_CLASSES (olddecl)); | |
8d08fdba MS |
3674 | } |
3675 | ||
8d08fdba MS |
3676 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
3677 | { | |
68642fb6 UD |
3678 | if (DECL_TEMPLATE_INSTANTIATION (olddecl) |
3679 | && !DECL_TEMPLATE_INSTANTIATION (newdecl)) | |
75650646 MM |
3680 | { |
3681 | /* If newdecl is not a specialization, then it is not a | |
3682 | template-related function at all. And that means that we | |
3683 | shoud have exited above, returning 0. */ | |
3684 | my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl), | |
3685 | 0); | |
3686 | ||
68642fb6 | 3687 | if (TREE_USED (olddecl)) |
75650646 | 3688 | /* From [temp.expl.spec]: |
68642fb6 | 3689 | |
75650646 MM |
3690 | If a template, a member template or the member of a class |
3691 | template is explicitly specialized then that | |
3692 | specialization shall be declared before the first use of | |
3693 | that specialization that would cause an implicit | |
3694 | instantiation to take place, in every translation unit in | |
3695 | which such a use occurs. */ | |
33bd39a2 | 3696 | error ("explicit specialization of %D after first use", |
75650646 MM |
3697 | olddecl); |
3698 | ||
3699 | SET_DECL_TEMPLATE_SPECIALIZATION (olddecl); | |
3700 | } | |
79065db2 | 3701 | DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl); |
faae18ab MS |
3702 | |
3703 | /* If either decl says `inline', this fn is inline, unless its | |
3704 | definition was passed already. */ | |
3705 | if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE) | |
3706 | DECL_INLINE (olddecl) = 1; | |
3707 | DECL_INLINE (newdecl) = DECL_INLINE (olddecl); | |
3708 | ||
97055d5c AO |
3709 | DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl) |
3710 | = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)); | |
3711 | ||
5daf7c0a JM |
3712 | /* Preserve abstractness on cloned [cd]tors. */ |
3713 | DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl); | |
3714 | ||
700f8a87 MS |
3715 | if (! types_match) |
3716 | { | |
5d2ed28c | 3717 | SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl)); |
92643fea | 3718 | COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl); |
19e7881c | 3719 | SET_DECL_RTL (olddecl, DECL_RTL (newdecl)); |
5566b478 MS |
3720 | } |
3721 | if (! types_match || new_defines_function) | |
3722 | { | |
6f1b4c42 JM |
3723 | /* These need to be copied so that the names are available. |
3724 | Note that if the types do match, we'll preserve inline | |
3725 | info and other bits, but if not, we won't. */ | |
700f8a87 MS |
3726 | DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl); |
3727 | DECL_RESULT (olddecl) = DECL_RESULT (newdecl); | |
700f8a87 | 3728 | } |
8d08fdba MS |
3729 | if (new_defines_function) |
3730 | /* If defining a function declared with other language | |
3731 | linkage, use the previously declared language linkage. */ | |
5d2ed28c | 3732 | SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl)); |
6f1b4c42 | 3733 | else if (types_match) |
8d08fdba MS |
3734 | { |
3735 | /* If redeclaring a builtin function, and not a definition, | |
3736 | it stays built in. */ | |
3737 | if (DECL_BUILT_IN (olddecl)) | |
3738 | { | |
26db82d8 | 3739 | DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl); |
39211cd5 | 3740 | DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl); |
8d08fdba MS |
3741 | /* If we're keeping the built-in definition, keep the rtl, |
3742 | regardless of declaration matches. */ | |
19e7881c | 3743 | SET_DECL_RTL (newdecl, DECL_RTL (olddecl)); |
8d08fdba MS |
3744 | } |
3745 | else | |
b850de4f | 3746 | DECL_NUM_STMTS (newdecl) = DECL_NUM_STMTS (olddecl); |
8d08fdba MS |
3747 | |
3748 | DECL_RESULT (newdecl) = DECL_RESULT (olddecl); | |
8d08fdba MS |
3749 | /* Don't clear out the arguments if we're redefining a function. */ |
3750 | if (DECL_ARGUMENTS (olddecl)) | |
3751 | DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl); | |
3752 | } | |
3753 | } | |
0b1161fc MM |
3754 | else if (TREE_CODE (newdecl) == NAMESPACE_DECL) |
3755 | NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl); | |
a9aedbc2 | 3756 | |
8d08fdba MS |
3757 | /* Now preserve various other info from the definition. */ |
3758 | TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl); | |
3759 | TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl); | |
3760 | DECL_COMMON (newdecl) = DECL_COMMON (olddecl); | |
92643fea | 3761 | COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl); |
8d08fdba | 3762 | |
8d08fdba MS |
3763 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
3764 | { | |
3765 | int function_size; | |
8d08fdba MS |
3766 | |
3767 | function_size = sizeof (struct tree_decl); | |
3768 | ||
730e1556 KG |
3769 | memcpy ((char *) olddecl + sizeof (struct tree_common), |
3770 | (char *) newdecl + sizeof (struct tree_common), | |
3771 | function_size - sizeof (struct tree_common)); | |
8d08fdba | 3772 | |
75650646 MM |
3773 | if (DECL_TEMPLATE_INSTANTIATION (newdecl)) |
3774 | { | |
3775 | /* If newdecl is a template instantiation, it is possible that | |
3776 | the following sequence of events has occurred: | |
3777 | ||
3778 | o A friend function was declared in a class template. The | |
68642fb6 | 3779 | class template was instantiated. |
75650646 | 3780 | |
68642fb6 UD |
3781 | o The instantiation of the friend declaration was |
3782 | recorded on the instantiation list, and is newdecl. | |
75650646 MM |
3783 | |
3784 | o Later, however, instantiate_class_template called pushdecl | |
3785 | on the newdecl to perform name injection. But, pushdecl in | |
3786 | turn called duplicate_decls when it discovered that another | |
3787 | declaration of a global function with the same name already | |
68642fb6 | 3788 | existed. |
75650646 MM |
3789 | |
3790 | o Here, in duplicate_decls, we decided to clobber newdecl. | |
3791 | ||
3792 | If we're going to do that, we'd better make sure that | |
3793 | olddecl, and not newdecl, is on the list of | |
3794 | instantiations so that if we try to do the instantiation | |
3795 | again we won't get the clobbered declaration. */ | |
3796 | ||
68642fb6 UD |
3797 | tree tmpl = DECL_TI_TEMPLATE (newdecl); |
3798 | tree decls = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); | |
75650646 MM |
3799 | |
3800 | for (; decls; decls = TREE_CHAIN (decls)) | |
3801 | if (TREE_VALUE (decls) == newdecl) | |
3802 | TREE_VALUE (decls) = olddecl; | |
3803 | } | |
8d08fdba MS |
3804 | } |
3805 | else | |
3806 | { | |
730e1556 KG |
3807 | memcpy ((char *) olddecl + sizeof (struct tree_common), |
3808 | (char *) newdecl + sizeof (struct tree_common), | |
3809 | sizeof (struct tree_decl) - sizeof (struct tree_common) | |
2f9834e8 | 3810 | + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *)); |
8d08fdba MS |
3811 | } |
3812 | ||
3813 | DECL_UID (olddecl) = olddecl_uid; | |
3814 | if (olddecl_friend) | |
3815 | DECL_FRIEND_P (olddecl) = 1; | |
3816 | ||
d9525bec BK |
3817 | /* NEWDECL contains the merged attribute lists. |
3818 | Update OLDDECL to be the same. */ | |
91d231cb | 3819 | DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl); |
d9525bec | 3820 | |
8d08fdba MS |
3821 | return 1; |
3822 | } | |
3823 | ||
3824 | /* Record a decl-node X as belonging to the current lexical scope. | |
3825 | Check for errors (such as an incompatible declaration for the same | |
3826 | name already seen in the same scope). | |
3827 | ||
3828 | Returns either X or an old decl for the same name. | |
3829 | If an old decl is returned, it may have been smashed | |
3830 | to agree with what X says. */ | |
3831 | ||
3832 | tree | |
3833 | pushdecl (x) | |
3834 | tree x; | |
3835 | { | |
3836 | register tree t; | |
b35d4555 MM |
3837 | register tree name; |
3838 | int need_new_binding; | |
3839 | ||
3840 | /* We shouldn't be calling pushdecl when we're generating RTL for a | |
3841 | function that we already did semantic analysis on previously. */ | |
01d939e8 | 3842 | my_friendly_assert (!cfun || doing_semantic_analysis_p (), |
b35d4555 MM |
3843 | 19990913); |
3844 | ||
b35d4555 | 3845 | need_new_binding = 1; |
8d08fdba | 3846 | |
50714e79 MM |
3847 | if (DECL_TEMPLATE_PARM_P (x)) |
3848 | /* Template parameters have no context; they are not X::T even | |
3849 | when declared within a class or namespace. */ | |
3850 | ; | |
3851 | else | |
3852 | { | |
3853 | if (current_function_decl && x != current_function_decl | |
3854 | /* A local declaration for a function doesn't constitute | |
3855 | nesting. */ | |
f39ee884 MM |
3856 | && !(TREE_CODE (x) == FUNCTION_DECL && !DECL_INITIAL (x)) |
3857 | /* A local declaration for an `extern' variable is in the | |
94dfccd1 | 3858 | scope of the current namespace, not the current |
f39ee884 MM |
3859 | function. */ |
3860 | && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x)) | |
50714e79 MM |
3861 | && !DECL_CONTEXT (x)) |
3862 | DECL_CONTEXT (x) = current_function_decl; | |
cd9f6678 MM |
3863 | |
3864 | /* If this is the declaration for a namespace-scope function, | |
3865 | but the declaration itself is in a local scope, mark the | |
3866 | declaration. */ | |
68642fb6 | 3867 | if (TREE_CODE (x) == FUNCTION_DECL |
cd9f6678 MM |
3868 | && DECL_NAMESPACE_SCOPE_P (x) |
3869 | && current_function_decl | |
3870 | && x != current_function_decl) | |
3871 | DECL_LOCAL_FUNCTION_P (x) = 1; | |
50714e79 | 3872 | } |
8d08fdba | 3873 | |
fe8fadc1 | 3874 | name = DECL_NAME (x); |
8d08fdba MS |
3875 | if (name) |
3876 | { | |
94dfccd1 JM |
3877 | int different_binding_level = 0; |
3878 | ||
386b8a85 JM |
3879 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) |
3880 | name = TREE_OPERAND (name, 0); | |
68642fb6 | 3881 | |
94dfccd1 JM |
3882 | /* In case this decl was explicitly namespace-qualified, look it |
3883 | up in its namespace context. */ | |
3884 | if (TREE_CODE (x) == VAR_DECL && DECL_NAMESPACE_SCOPE_P (x) | |
3885 | && namespace_bindings_p ()) | |
2c73f9f5 ML |
3886 | t = namespace_binding (name, DECL_CONTEXT (x)); |
3887 | else | |
3888 | t = lookup_name_current_level (name); | |
fe8fadc1 | 3889 | |
94dfccd1 JM |
3890 | /* [basic.link] If there is a visible declaration of an entity |
3891 | with linkage having the same name and type, ignoring entities | |
3892 | declared outside the innermost enclosing namespace scope, the | |
3893 | block scope declaration declares that same entity and | |
3894 | receives the linkage of the previous declaration. */ | |
3895 | if (! t && current_function_decl && x != current_function_decl | |
3896 | && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL) | |
3897 | && DECL_EXTERNAL (x)) | |
3898 | { | |
3899 | /* Look in block scope. */ | |
3900 | t = IDENTIFIER_VALUE (name); | |
3901 | /* Or in the innermost namespace. */ | |
3902 | if (! t) | |
3903 | t = namespace_binding (name, DECL_CONTEXT (x)); | |
fab09a24 JM |
3904 | /* Does it have linkage? Note that if this isn't a DECL, it's an |
3905 | OVERLOAD, which is OK. */ | |
3906 | if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t))) | |
94dfccd1 JM |
3907 | t = NULL_TREE; |
3908 | if (t) | |
3909 | different_binding_level = 1; | |
3910 | } | |
3911 | ||
fe8fadc1 MM |
3912 | /* If we are declaring a function, and the result of name-lookup |
3913 | was an OVERLOAD, look for an overloaded instance that is | |
3914 | actually the same as the function we are declaring. (If | |
3915 | there is one, we have to merge our declaration with the | |
3916 | previous declaration.) */ | |
655dc6ee | 3917 | if (t && TREE_CODE (t) == OVERLOAD) |
fe8fadc1 MM |
3918 | { |
3919 | tree match; | |
3920 | ||
655dc6ee JM |
3921 | if (TREE_CODE (x) == FUNCTION_DECL) |
3922 | for (match = t; match; match = OVL_NEXT (match)) | |
3923 | { | |
92643fea | 3924 | if (decls_match (OVL_CURRENT (match), x)) |
655dc6ee JM |
3925 | break; |
3926 | } | |
3927 | else | |
3928 | /* Just choose one. */ | |
3929 | match = t; | |
fe8fadc1 MM |
3930 | |
3931 | if (match) | |
3932 | t = OVL_CURRENT (match); | |
3933 | else | |
3934 | t = NULL_TREE; | |
3935 | } | |
3936 | ||
8d08fdba MS |
3937 | if (t == error_mark_node) |
3938 | { | |
3939 | /* error_mark_node is 0 for a while during initialization! */ | |
3940 | t = NULL_TREE; | |
8251199e | 3941 | cp_error_at ("`%#D' used prior to declaration", x); |
8d08fdba | 3942 | } |
51c184be | 3943 | else if (t != NULL_TREE) |
8d08fdba | 3944 | { |
94dfccd1 JM |
3945 | if (different_binding_level) |
3946 | { | |
3947 | if (decls_match (x, t)) | |
3948 | /* The standard only says that the local extern | |
3949 | inherits linkage from the previous decl; in | |
3950 | particular, default args are not shared. It would | |
3951 | be nice to propagate inlining info, though. FIXME. */ | |
3952 | TREE_PUBLIC (x) = TREE_PUBLIC (t); | |
3953 | } | |
3954 | else if (TREE_CODE (t) == PARM_DECL) | |
8d08fdba MS |
3955 | { |
3956 | if (DECL_CONTEXT (t) == NULL_TREE) | |
400500c4 RK |
3957 | /* This is probaby caused by too many errors, but calling |
3958 | abort will say that if errors have occurred. */ | |
3959 | abort (); | |
be99da77 | 3960 | |
e92cc029 | 3961 | /* Check for duplicate params. */ |
be99da77 MS |
3962 | if (duplicate_decls (x, t)) |
3963 | return t; | |
8d08fdba | 3964 | } |
eb68cb58 | 3965 | else if ((DECL_EXTERN_C_FUNCTION_P (x) |
5566b478 MS |
3966 | || DECL_FUNCTION_TEMPLATE_P (x)) |
3967 | && is_overloaded_fn (t)) | |
2c73f9f5 | 3968 | /* Don't do anything just yet. */; |
e1cd6e56 MS |
3969 | else if (t == wchar_decl_node) |
3970 | { | |
3971 | if (pedantic && ! DECL_IN_SYSTEM_HEADER (x)) | |
33bd39a2 | 3972 | pedwarn ("redeclaration of `wchar_t' as `%T'", |
2b0cbc5d | 3973 | TREE_TYPE (x)); |
e1cd6e56 MS |
3974 | |
3975 | /* Throw away the redeclaration. */ | |
3976 | return t; | |
3977 | } | |
8926095f | 3978 | else if (TREE_CODE (t) != TREE_CODE (x)) |
8d08fdba | 3979 | { |
9ed182dc | 3980 | if (duplicate_decls (x, t)) |
51c184be | 3981 | return t; |
8d08fdba MS |
3982 | } |
3983 | else if (duplicate_decls (x, t)) | |
51c184be | 3984 | { |
7177d104 MS |
3985 | if (TREE_CODE (t) == TYPE_DECL) |
3986 | SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t)); | |
f30432d7 MS |
3987 | else if (TREE_CODE (t) == FUNCTION_DECL) |
3988 | check_default_args (t); | |
7177d104 | 3989 | |
51c184be MS |
3990 | return t; |
3991 | } | |
35680744 MM |
3992 | else if (DECL_MAIN_P (x)) |
3993 | { | |
3994 | /* A redeclaration of main, but not a duplicate of the | |
68642fb6 | 3995 | previous one. |
35680744 MM |
3996 | |
3997 | [basic.start.main] | |
3998 | ||
3999 | This function shall not be overloaded. */ | |
8251199e | 4000 | cp_error_at ("invalid redeclaration of `%D'", t); |
33bd39a2 | 4001 | error ("as `%D'", x); |
35680744 MM |
4002 | /* We don't try to push this declaration since that |
4003 | causes a crash. */ | |
4004 | return x; | |
4005 | } | |
8d08fdba | 4006 | } |
8926095f | 4007 | |
f3400fe2 JM |
4008 | check_template_shadow (x); |
4009 | ||
fcfcdfc8 JM |
4010 | /* If this is a function conjured up by the backend, massage it |
4011 | so it looks friendly. */ | |
eb68cb58 | 4012 | if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x)) |
fcfcdfc8 JM |
4013 | { |
4014 | retrofit_lang_decl (x); | |
5d2ed28c | 4015 | SET_DECL_LANGUAGE (x, lang_c); |
fcfcdfc8 JM |
4016 | } |
4017 | ||
eb68cb58 | 4018 | if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x)) |
8926095f | 4019 | { |
7bdbfa05 | 4020 | t = push_overloaded_decl (x, PUSH_LOCAL); |
5fdaba89 | 4021 | if (t != x) |
8926095f | 4022 | return t; |
f181d4ae MM |
4023 | if (!namespace_bindings_p ()) |
4024 | /* We do not need to create a binding for this name; | |
4025 | push_overloaded_decl will have already done so if | |
4026 | necessary. */ | |
4027 | need_new_binding = 0; | |
8926095f | 4028 | } |
6eb3bb27 | 4029 | else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x)) |
cfe507be MM |
4030 | { |
4031 | t = push_overloaded_decl (x, PUSH_GLOBAL); | |
4032 | if (t == x) | |
4033 | add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t))); | |
4034 | return t; | |
4035 | } | |
8d08fdba | 4036 | |
a1774733 BK |
4037 | /* If declaring a type as a typedef, copy the type (unless we're |
4038 | at line 0), and install this TYPE_DECL as the new type's typedef | |
4039 | name. See the extensive comment in ../c-decl.c (pushdecl). */ | |
8d08fdba MS |
4040 | if (TREE_CODE (x) == TYPE_DECL) |
4041 | { | |
4042 | tree type = TREE_TYPE (x); | |
655dc6ee | 4043 | if (DECL_SOURCE_LINE (x) == 0) |
a1774733 BK |
4044 | { |
4045 | if (TYPE_NAME (type) == 0) | |
4046 | TYPE_NAME (type) = x; | |
4047 | } | |
1c80fb65 MM |
4048 | else if (type != error_mark_node && TYPE_NAME (type) != x |
4049 | /* We don't want to copy the type when all we're | |
4050 | doing is making a TYPE_DECL for the purposes of | |
4051 | inlining. */ | |
68642fb6 | 4052 | && (!TYPE_NAME (type) |
1c80fb65 | 4053 | || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x))) |
a1774733 BK |
4054 | { |
4055 | DECL_ORIGINAL_TYPE (x) = type; | |
4056 | type = build_type_copy (type); | |
4057 | TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x)); | |
4058 | TYPE_NAME (type) = x; | |
4059 | TREE_TYPE (x) = type; | |
4060 | } | |
8d08fdba | 4061 | |
8d08fdba MS |
4062 | if (type != error_mark_node |
4063 | && TYPE_NAME (type) | |
4064 | && TYPE_IDENTIFIER (type)) | |
68642fb6 | 4065 | set_identifier_type_value_with_scope (DECL_NAME (x), type, |
f181d4ae MM |
4066 | current_binding_level); |
4067 | ||
8d08fdba MS |
4068 | } |
4069 | ||
4070 | /* Multiple external decls of the same identifier ought to match. | |
4071 | ||
4072 | We get warnings about inline functions where they are defined. | |
39211cd5 | 4073 | We get warnings about other functions from push_overloaded_decl. |
68642fb6 | 4074 | |
8d08fdba | 4075 | Avoid duplicate warnings where they are used. */ |
39211cd5 | 4076 | if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL) |
8d08fdba MS |
4077 | { |
4078 | tree decl; | |
4079 | ||
f49fad00 JM |
4080 | decl = IDENTIFIER_NAMESPACE_VALUE (name); |
4081 | if (decl && TREE_CODE (decl) == OVERLOAD) | |
4082 | decl = OVL_FUNCTION (decl); | |
8d08fdba | 4083 | |
f49fad00 JM |
4084 | if (decl && decl != error_mark_node |
4085 | && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl)) | |
8d08fdba MS |
4086 | /* If different sort of thing, we already gave an error. */ |
4087 | && TREE_CODE (decl) == TREE_CODE (x) | |
3bfdc719 | 4088 | && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl))) |
8d08fdba | 4089 | { |
33bd39a2 | 4090 | pedwarn ("type mismatch with previous external decl", x); |
8251199e | 4091 | cp_pedwarn_at ("previous external decl of `%#D'", decl); |
8d08fdba MS |
4092 | } |
4093 | } | |
4094 | ||
8d08fdba MS |
4095 | /* This name is new in its binding level. |
4096 | Install the new declaration and return it. */ | |
2c73f9f5 | 4097 | if (namespace_bindings_p ()) |
8d08fdba MS |
4098 | { |
4099 | /* Install a global value. */ | |
4100 | ||
8d08fdba MS |
4101 | /* If the first global decl has external linkage, |
4102 | warn if we later see static one. */ | |
31928556 | 4103 | if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x)) |
8d08fdba | 4104 | TREE_PUBLIC (name) = 1; |
5362b086 | 4105 | |
92643fea MM |
4106 | /* Bind the name for the entity. */ |
4107 | if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x) | |
4108 | && t != NULL_TREE) | |
4109 | && (TREE_CODE (x) == TYPE_DECL | |
4110 | || TREE_CODE (x) == VAR_DECL | |
4111 | || TREE_CODE (x) == NAMESPACE_DECL | |
4112 | || TREE_CODE (x) == CONST_DECL | |
4113 | || TREE_CODE (x) == TEMPLATE_DECL)) | |
4114 | SET_IDENTIFIER_NAMESPACE_VALUE (name, x); | |
8d08fdba MS |
4115 | |
4116 | /* Don't forget if the function was used via an implicit decl. */ | |
4117 | if (IDENTIFIER_IMPLICIT_DECL (name) | |
4118 | && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name))) | |
4119 | TREE_USED (x) = 1; | |
4120 | ||
4121 | /* Don't forget if its address was taken in that way. */ | |
4122 | if (IDENTIFIER_IMPLICIT_DECL (name) | |
4123 | && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name))) | |
4124 | TREE_ADDRESSABLE (x) = 1; | |
4125 | ||
4126 | /* Warn about mismatches against previous implicit decl. */ | |
4127 | if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE | |
4128 | /* If this real decl matches the implicit, don't complain. */ | |
4129 | && ! (TREE_CODE (x) == FUNCTION_DECL | |
4130 | && TREE_TYPE (TREE_TYPE (x)) == integer_type_node)) | |
33bd39a2 | 4131 | warning |
8251199e | 4132 | ("`%D' was previously implicitly declared to return `int'", x); |
8d08fdba MS |
4133 | |
4134 | /* If new decl is `static' and an `extern' was seen previously, | |
4135 | warn about it. */ | |
a0a33927 MS |
4136 | if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t)) |
4137 | warn_extern_redeclared_static (x, t); | |
8d08fdba MS |
4138 | } |
4139 | else | |
4140 | { | |
4141 | /* Here to install a non-global value. */ | |
f181d4ae | 4142 | tree oldlocal = IDENTIFIER_VALUE (name); |
31928556 | 4143 | tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name); |
8d08fdba | 4144 | |
f181d4ae MM |
4145 | if (need_new_binding) |
4146 | { | |
0034cf72 | 4147 | push_local_binding (name, x, 0); |
f181d4ae MM |
4148 | /* Because push_local_binding will hook X on to the |
4149 | current_binding_level's name list, we don't want to | |
4150 | do that again below. */ | |
4151 | need_new_binding = 0; | |
4152 | } | |
8d08fdba MS |
4153 | |
4154 | /* If this is a TYPE_DECL, push it into the type value slot. */ | |
4155 | if (TREE_CODE (x) == TYPE_DECL) | |
68642fb6 | 4156 | set_identifier_type_value_with_scope (name, TREE_TYPE (x), |
f181d4ae | 4157 | current_binding_level); |
8d08fdba | 4158 | |
a9aedbc2 MS |
4159 | /* Clear out any TYPE_DECL shadowed by a namespace so that |
4160 | we won't think this is a type. The C struct hack doesn't | |
4161 | go through namespaces. */ | |
4162 | if (TREE_CODE (x) == NAMESPACE_DECL) | |
68642fb6 | 4163 | set_identifier_type_value_with_scope (name, NULL_TREE, |
f181d4ae | 4164 | current_binding_level); |
a9aedbc2 | 4165 | |
e905ac8a MS |
4166 | if (oldlocal) |
4167 | { | |
4168 | tree d = oldlocal; | |
17aec3eb | 4169 | |
74dc0d8c MS |
4170 | while (oldlocal |
4171 | && TREE_CODE (oldlocal) == VAR_DECL | |
4172 | && DECL_DEAD_FOR_LOCAL (oldlocal)) | |
17aec3eb RK |
4173 | oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal); |
4174 | ||
e905ac8a MS |
4175 | if (oldlocal == NULL_TREE) |
4176 | oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d)); | |
4177 | } | |
4178 | ||
8d08fdba MS |
4179 | /* If this is an extern function declaration, see if we |
4180 | have a global definition or declaration for the function. */ | |
4181 | if (oldlocal == NULL_TREE | |
faae18ab | 4182 | && DECL_EXTERNAL (x) |
31928556 | 4183 | && oldglobal != NULL_TREE |
8d08fdba | 4184 | && TREE_CODE (x) == FUNCTION_DECL |
31928556 | 4185 | && TREE_CODE (oldglobal) == FUNCTION_DECL) |
8d08fdba MS |
4186 | { |
4187 | /* We have one. Their types must agree. */ | |
31928556 | 4188 | if (decls_match (x, oldglobal)) |
6060a796 MS |
4189 | /* OK */; |
4190 | else | |
8d08fdba | 4191 | { |
33bd39a2 | 4192 | warning ("extern declaration of `%#D' doesn't match", x); |
8251199e | 4193 | cp_warning_at ("global declaration `%#D'", oldglobal); |
8d08fdba | 4194 | } |
8d08fdba MS |
4195 | } |
4196 | /* If we have a local external declaration, | |
4197 | and no file-scope declaration has yet been seen, | |
4198 | then if we later have a file-scope decl it must not be static. */ | |
4199 | if (oldlocal == NULL_TREE | |
31928556 | 4200 | && oldglobal == NULL_TREE |
8d08fdba MS |
4201 | && DECL_EXTERNAL (x) |
4202 | && TREE_PUBLIC (x)) | |
f181d4ae | 4203 | TREE_PUBLIC (name) = 1; |
8d08fdba | 4204 | |
8d08fdba | 4205 | /* Warn if shadowing an argument at the top level of the body. */ |
e905ac8a MS |
4206 | if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x) |
4207 | /* Inline decls shadow nothing. */ | |
4208 | && !DECL_FROM_INLINE (x) | |
8ac9ea61 | 4209 | && TREE_CODE (oldlocal) == PARM_DECL) |
8d08fdba | 4210 | { |
8ac9ea61 | 4211 | bool err = false; |
8d08fdba | 4212 | |
8ac9ea61 NB |
4213 | /* Don't complain if it's from an enclosing function. */ |
4214 | if (DECL_CONTEXT (oldlocal) == current_function_decl | |
4215 | && TREE_CODE (x) != PARM_DECL) | |
4216 | { | |
4217 | /* Go to where the parms should be and see if we find | |
4218 | them there. */ | |
4219 | struct binding_level *b = current_binding_level->level_chain; | |
4220 | ||
8ac9ea61 NB |
4221 | /* ARM $8.3 */ |
4222 | if (b->parm_flag == 1) | |
4223 | { | |
33bd39a2 | 4224 | error ("declaration of `%#D' shadows a parameter", |
8ac9ea61 NB |
4225 | name); |
4226 | err = true; | |
4227 | } | |
4228 | } | |
8d08fdba | 4229 | |
8ac9ea61 NB |
4230 | if (warn_shadow && !err) |
4231 | shadow_warning ("a parameter", name, oldlocal); | |
8d08fdba | 4232 | } |
e905ac8a | 4233 | |
8d08fdba | 4234 | /* Maybe warn if shadowing something else. */ |
8ac9ea61 | 4235 | else if (warn_shadow && !DECL_EXTERNAL (x) |
e905ac8a MS |
4236 | /* No shadow warnings for internally generated vars. */ |
4237 | && ! DECL_ARTIFICIAL (x) | |
4238 | /* No shadow warnings for vars made for inlining. */ | |
4239 | && ! DECL_FROM_INLINE (x)) | |
8d08fdba | 4240 | { |
8ac9ea61 | 4241 | if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE |
4ac14744 | 4242 | && current_class_ptr |
8d08fdba | 4243 | && !TREE_STATIC (name)) |
33bd39a2 | 4244 | warning ("declaration of `%s' shadows a member of `this'", |
8ac9ea61 NB |
4245 | IDENTIFIER_POINTER (name)); |
4246 | else if (oldlocal != NULL_TREE | |
4247 | && TREE_CODE (oldlocal) == VAR_DECL) | |
4248 | shadow_warning ("a previous local", name, oldlocal); | |
4249 | else if (oldglobal != NULL_TREE | |
4250 | && TREE_CODE (oldglobal) == VAR_DECL) | |
30394414 | 4251 | /* XXX shadow warnings in outer-more namespaces */ |
8ac9ea61 | 4252 | shadow_warning ("a global declaration", name, oldglobal); |
8d08fdba | 4253 | } |
e1cd6e56 | 4254 | } |
8d08fdba | 4255 | |
e1cd6e56 | 4256 | if (TREE_CODE (x) == FUNCTION_DECL) |
f30432d7 | 4257 | check_default_args (x); |
8145f082 | 4258 | |
0154eaa8 MM |
4259 | if (TREE_CODE (x) == VAR_DECL) |
4260 | maybe_register_incomplete_var (x); | |
8d08fdba MS |
4261 | } |
4262 | ||
f181d4ae | 4263 | if (need_new_binding) |
68642fb6 | 4264 | add_decl_to_level (x, |
efee38a9 MM |
4265 | DECL_NAMESPACE_SCOPE_P (x) |
4266 | ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x)) | |
4267 | : current_binding_level); | |
8d08fdba MS |
4268 | |
4269 | return x; | |
4270 | } | |
4271 | ||
5566b478 MS |
4272 | /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the |
4273 | caller to set DECL_CONTEXT properly. */ | |
8d08fdba MS |
4274 | |
4275 | static tree | |
4276 | pushdecl_with_scope (x, level) | |
4277 | tree x; | |
4278 | struct binding_level *level; | |
4279 | { | |
8d019cef | 4280 | register struct binding_level *b; |
5566b478 | 4281 | tree function_decl = current_function_decl; |
8d08fdba | 4282 | |
5566b478 | 4283 | current_function_decl = NULL_TREE; |
8d019cef JM |
4284 | if (level->parm_flag == 2) |
4285 | { | |
4286 | b = class_binding_level; | |
4287 | class_binding_level = level; | |
4288 | pushdecl_class_level (x); | |
4289 | class_binding_level = b; | |
4290 | } | |
4291 | else | |
4292 | { | |
4293 | b = current_binding_level; | |
4294 | current_binding_level = level; | |
4295 | x = pushdecl (x); | |
4296 | current_binding_level = b; | |
4297 | } | |
5566b478 | 4298 | current_function_decl = function_decl; |
8d08fdba MS |
4299 | return x; |
4300 | } | |
4301 | ||
2c73f9f5 | 4302 | /* Like pushdecl, only it places X in the current namespace, |
8d08fdba | 4303 | if appropriate. */ |
e92cc029 | 4304 | |
8d08fdba | 4305 | tree |
2c73f9f5 | 4306 | pushdecl_namespace_level (x) |
8d08fdba MS |
4307 | tree x; |
4308 | { | |
8f032717 | 4309 | register struct binding_level *b = current_binding_level; |
2c73f9f5 ML |
4310 | register tree t; |
4311 | ||
4312 | t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace)); | |
8d08fdba MS |
4313 | |
4314 | /* Now, the type_shadowed stack may screw us. Munge it so it does | |
4315 | what we want. */ | |
4316 | if (TREE_CODE (x) == TYPE_DECL) | |
4317 | { | |
4318 | tree name = DECL_NAME (x); | |
4319 | tree newval; | |
4320 | tree *ptr = (tree *)0; | |
4321 | for (; b != global_binding_level; b = b->level_chain) | |
4322 | { | |
4323 | tree shadowed = b->type_shadowed; | |
4324 | for (; shadowed; shadowed = TREE_CHAIN (shadowed)) | |
4325 | if (TREE_PURPOSE (shadowed) == name) | |
4326 | { | |
4327 | ptr = &TREE_VALUE (shadowed); | |
4328 | /* Can't break out of the loop here because sometimes | |
4329 | a binding level will have duplicate bindings for | |
4330 | PT names. It's gross, but I haven't time to fix it. */ | |
4331 | } | |
4332 | } | |
4333 | newval = TREE_TYPE (x); | |
4334 | if (ptr == (tree *)0) | |
4335 | { | |
4336 | /* @@ This shouldn't be needed. My test case "zstring.cc" trips | |
4337 | up here if this is changed to an assertion. --KR */ | |
4338 | SET_IDENTIFIER_TYPE_VALUE (name, newval); | |
4339 | } | |
4340 | else | |
4341 | { | |
8d08fdba MS |
4342 | *ptr = newval; |
4343 | } | |
4344 | } | |
4345 | return t; | |
4346 | } | |
4347 | ||
2c73f9f5 ML |
4348 | /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL, |
4349 | if appropriate. */ | |
4350 | ||
4351 | tree | |
4352 | pushdecl_top_level (x) | |
4353 | tree x; | |
4354 | { | |
b35d4555 | 4355 | push_to_top_level (); |
2c73f9f5 | 4356 | x = pushdecl_namespace_level (x); |
b35d4555 | 4357 | pop_from_top_level (); |
2c73f9f5 ML |
4358 | return x; |
4359 | } | |
4360 | ||
8d08fdba | 4361 | /* Make the declaration of X appear in CLASS scope. */ |
e92cc029 | 4362 | |
61a127b3 | 4363 | void |
8d08fdba MS |
4364 | pushdecl_class_level (x) |
4365 | tree x; | |
4366 | { | |
4367 | /* Don't use DECL_ASSEMBLER_NAME here! Everything that looks in class | |
4368 | scope looks for the pre-mangled name. */ | |
8f032717 MM |
4369 | register tree name; |
4370 | ||
4371 | if (TREE_CODE (x) == OVERLOAD) | |
4372 | x = OVL_CURRENT (x); | |
4373 | name = DECL_NAME (x); | |
8d08fdba MS |
4374 | |
4375 | if (name) | |
4376 | { | |
4377 | push_class_level_binding (name, x); | |
4378 | if (TREE_CODE (x) == TYPE_DECL) | |
f181d4ae | 4379 | set_identifier_type_value (name, TREE_TYPE (x)); |
8d08fdba | 4380 | } |
6bdb8141 | 4381 | else if (ANON_AGGR_TYPE_P (TREE_TYPE (x))) |
8f032717 MM |
4382 | { |
4383 | tree f; | |
4384 | ||
4385 | for (f = TYPE_FIELDS (TREE_TYPE (x)); | |
4386 | f; | |
4387 | f = TREE_CHAIN (f)) | |
4388 | pushdecl_class_level (f); | |
4389 | } | |
8d08fdba MS |
4390 | } |
4391 | ||
9188c363 MM |
4392 | /* Enter DECL into the symbol table, if that's appropriate. Returns |
4393 | DECL, or a modified version thereof. */ | |
4394 | ||
4395 | tree | |
4396 | maybe_push_decl (decl) | |
4397 | tree decl; | |
4398 | { | |
4399 | tree type = TREE_TYPE (decl); | |
4400 | ||
4401 | /* Add this decl to the current binding level, but not if it comes | |
4402 | from another scope, e.g. a static member variable. TEM may equal | |
4403 | DECL or it may be a previous decl of the same name. */ | |
07c88314 MM |
4404 | if (decl == error_mark_node |
4405 | || (TREE_CODE (decl) != PARM_DECL | |
4406 | && DECL_CONTEXT (decl) != NULL_TREE | |
4407 | /* Definitions of namespace members outside their namespace are | |
4408 | possible. */ | |
4409 | && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL) | |
9188c363 MM |
4410 | || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ()) |
4411 | || TREE_CODE (type) == UNKNOWN_TYPE | |
558475f0 | 4412 | /* The declaration of a template specialization does not affect |
9188c363 MM |
4413 | the functions available for overload resolution, so we do not |
4414 | call pushdecl. */ | |
4415 | || (TREE_CODE (decl) == FUNCTION_DECL | |
4416 | && DECL_TEMPLATE_SPECIALIZATION (decl))) | |
4417 | return decl; | |
4418 | else | |
4419 | return pushdecl (decl); | |
4420 | } | |
4421 | ||
8d08fdba MS |
4422 | /* Make the declaration(s) of X appear in CLASS scope |
4423 | under the name NAME. */ | |
e92cc029 | 4424 | |
8d08fdba MS |
4425 | void |
4426 | push_class_level_binding (name, x) | |
4427 | tree name; | |
4428 | tree x; | |
4429 | { | |
8f032717 | 4430 | tree binding; |
68642fb6 | 4431 | /* The class_binding_level will be NULL if x is a template |
98c1c668 JM |
4432 | parameter name in a member template. */ |
4433 | if (!class_binding_level) | |
4434 | return; | |
4435 | ||
908c4e83 MM |
4436 | /* Make sure that this new member does not have the same name |
4437 | as a template parameter. */ | |
4438 | if (TYPE_BEING_DEFINED (current_class_type)) | |
4439 | check_template_shadow (x); | |
4440 | ||
f181d4ae MM |
4441 | /* If this declaration shadows a declaration from an enclosing |
4442 | class, then we will need to restore IDENTIFIER_CLASS_VALUE when | |
4443 | we leave this class. Record the shadowed declaration here. */ | |
8f032717 | 4444 | binding = IDENTIFIER_BINDING (name); |
68642fb6 | 4445 | if (binding |
8f032717 MM |
4446 | && ((TREE_CODE (x) == OVERLOAD |
4447 | && BINDING_VALUE (binding) | |
4448 | && is_overloaded_fn (BINDING_VALUE (binding))) | |
4449 | || INHERITED_VALUE_BINDING_P (binding))) | |
4450 | { | |
4451 | tree shadow; | |
4452 | tree old_decl; | |
4453 | ||
4454 | /* If the old binding was from a base class, and was for a tag | |
4455 | name, slide it over to make room for the new binding. The | |
4456 | old binding is still visible if explicitly qualified with a | |
4457 | class-key. */ | |
4458 | if (INHERITED_VALUE_BINDING_P (binding) | |
4459 | && BINDING_VALUE (binding) | |
4460 | && TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL | |
4461 | && DECL_ARTIFICIAL (BINDING_VALUE (binding)) | |
4462 | && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x))) | |
4463 | { | |
4464 | old_decl = BINDING_TYPE (binding); | |
4465 | BINDING_TYPE (binding) = BINDING_VALUE (binding); | |
4466 | BINDING_VALUE (binding) = NULL_TREE; | |
4467 | INHERITED_VALUE_BINDING_P (binding) = 0; | |
4468 | } | |
4469 | else | |
4470 | old_decl = BINDING_VALUE (binding); | |
4471 | ||
76191fdd NS |
4472 | /* Find the previous binding of name on the class-shadowed |
4473 | list, and update it. */ | |
8f032717 MM |
4474 | for (shadow = class_binding_level->class_shadowed; |
4475 | shadow; | |
4476 | shadow = TREE_CHAIN (shadow)) | |
4477 | if (TREE_PURPOSE (shadow) == name | |
4478 | && TREE_TYPE (shadow) == old_decl) | |
4479 | { | |
4480 | BINDING_VALUE (binding) = x; | |
4481 | INHERITED_VALUE_BINDING_P (binding) = 0; | |
4482 | TREE_TYPE (shadow) = x; | |
76191fdd | 4483 | IDENTIFIER_CLASS_VALUE (name) = x; |
8f032717 MM |
4484 | return; |
4485 | } | |
4486 | } | |
f181d4ae | 4487 | |
8f032717 | 4488 | /* If we didn't replace an existing binding, put the binding on the |
76191fdd | 4489 | stack of bindings for the identifier, and update the shadowed list. */ |
8f032717 MM |
4490 | if (push_class_binding (name, x)) |
4491 | { | |
8f032717 | 4492 | class_binding_level->class_shadowed |
76191fdd | 4493 | = tree_cons (name, NULL, |
8f032717 | 4494 | class_binding_level->class_shadowed); |
8f032717 MM |
4495 | /* Record the value we are binding NAME to so that we can know |
4496 | what to pop later. */ | |
4497 | TREE_TYPE (class_binding_level->class_shadowed) = x; | |
4498 | } | |
8d08fdba MS |
4499 | } |
4500 | ||
dd4fae80 ML |
4501 | /* Insert another USING_DECL into the current binding level, returning |
4502 | this declaration. If this is a redeclaration, do nothing, and | |
4503 | return NULL_TREE if this not in namespace scope (in namespace | |
4504 | scope, a using decl might extend any previous bindings). */ | |
e92cc029 | 4505 | |
2c73f9f5 ML |
4506 | tree |
4507 | push_using_decl (scope, name) | |
4508 | tree scope; | |
4509 | tree name; | |
8d08fdba | 4510 | { |
2c73f9f5 | 4511 | tree decl; |
68642fb6 | 4512 | |
2c73f9f5 ML |
4513 | my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383); |
4514 | my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384); | |
4515 | for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl)) | |
4516 | if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name) | |
4517 | break; | |
4518 | if (decl) | |
dd4fae80 | 4519 | return namespace_bindings_p () ? decl : NULL_TREE; |
4ce3d537 | 4520 | decl = build_lang_decl (USING_DECL, name, void_type_node); |
2c73f9f5 ML |
4521 | DECL_INITIAL (decl) = scope; |
4522 | TREE_CHAIN (decl) = current_binding_level->usings; | |
4523 | current_binding_level->usings = decl; | |
4524 | return decl; | |
8d08fdba MS |
4525 | } |
4526 | ||
ea9635c7 ML |
4527 | /* Add namespace to using_directives. Return NULL_TREE if nothing was |
4528 | changed (i.e. there was already a directive), or the fresh | |
4529 | TREE_LIST otherwise. */ | |
4530 | ||
4531 | tree | |
9ed182dc | 4532 | push_using_directive (used) |
ea9635c7 | 4533 | tree used; |
ea9635c7 ML |
4534 | { |
4535 | tree ud = current_binding_level->using_directives; | |
9ed182dc | 4536 | tree iter, ancestor; |
68642fb6 | 4537 | |
ea9635c7 ML |
4538 | /* Check if we already have this. */ |
4539 | if (purpose_member (used, ud) != NULL_TREE) | |
4540 | return NULL_TREE; | |
00dc6358 JM |
4541 | |
4542 | /* Recursively add all namespaces used. */ | |
4543 | for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter)) | |
9ed182dc | 4544 | push_using_directive (TREE_PURPOSE (iter)); |
00dc6358 | 4545 | |
9ed182dc | 4546 | ancestor = namespace_ancestor (current_decl_namespace (), used); |
00dc6358 | 4547 | ud = current_binding_level->using_directives; |
e1b3e07d | 4548 | ud = tree_cons (used, ancestor, ud); |
ea9635c7 ML |
4549 | current_binding_level->using_directives = ud; |
4550 | return ud; | |
4551 | } | |
4552 | ||
f181d4ae MM |
4553 | /* DECL is a FUNCTION_DECL for a non-member function, which may have |
4554 | other definitions already in place. We get around this by making | |
4555 | the value of the identifier point to a list of all the things that | |
4556 | want to be referenced by that name. It is then up to the users of | |
4557 | that name to decide what to do with that list. | |
8d08fdba | 4558 | |
17aec3eb RK |
4559 | DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its |
4560 | DECL_TEMPLATE_RESULT. It is dealt with the same way. | |
8d08fdba | 4561 | |
7bdbfa05 MM |
4562 | FLAGS is a bitwise-or of the following values: |
4563 | PUSH_LOCAL: Bind DECL in the current scope, rather than at | |
4564 | namespace scope. | |
4565 | PUSH_USING: DECL is being pushed as the result of a using | |
68642fb6 | 4566 | declaration. |
7bdbfa05 | 4567 | |
8d08fdba MS |
4568 | The value returned may be a previous declaration if we guessed wrong |
4569 | about what language DECL should belong to (C or C++). Otherwise, | |
4570 | it's always DECL (and never something that's not a _DECL). */ | |
e92cc029 | 4571 | |
7bdbfa05 MM |
4572 | tree |
4573 | push_overloaded_decl (decl, flags) | |
8d08fdba | 4574 | tree decl; |
7bdbfa05 | 4575 | int flags; |
8d08fdba | 4576 | { |
f181d4ae | 4577 | tree name = DECL_NAME (decl); |
700f8a87 | 4578 | tree old; |
f181d4ae | 4579 | tree new_binding; |
7bdbfa05 | 4580 | int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL)); |
700f8a87 MS |
4581 | |
4582 | if (doing_global) | |
9f33663b | 4583 | old = namespace_binding (name, DECL_CONTEXT (decl)); |
700f8a87 | 4584 | else |
f181d4ae | 4585 | old = lookup_name_current_level (name); |
8d08fdba | 4586 | |
700f8a87 | 4587 | if (old) |
8d08fdba | 4588 | { |
e1cd6e56 | 4589 | if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old)) |
8926095f | 4590 | { |
700f8a87 | 4591 | tree t = TREE_TYPE (old); |
cdf5b885 MS |
4592 | if (IS_AGGR_TYPE (t) && warn_shadow |
4593 | && (! DECL_IN_SYSTEM_HEADER (decl) | |
4594 | || ! DECL_IN_SYSTEM_HEADER (old))) | |
33bd39a2 | 4595 | warning ("`%#D' hides constructor for `%#T'", decl, t); |
5b605f68 | 4596 | old = NULL_TREE; |
8926095f | 4597 | } |
700f8a87 | 4598 | else if (is_overloaded_fn (old)) |
8d08fdba | 4599 | { |
8d08fdba | 4600 | tree tmp; |
68642fb6 | 4601 | |
2c73f9f5 | 4602 | for (tmp = old; tmp; tmp = OVL_NEXT (tmp)) |
7bdbfa05 MM |
4603 | { |
4604 | tree fn = OVL_CURRENT (tmp); | |
4605 | ||
4606 | if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp) | |
4607 | && !(flags & PUSH_USING) | |
4608 | && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)), | |
4609 | TYPE_ARG_TYPES (TREE_TYPE (decl)))) | |
33bd39a2 | 4610 | error ("`%#D' conflicts with previous using declaration `%#D'", |
7bdbfa05 | 4611 | decl, fn); |
68642fb6 | 4612 | |
7bdbfa05 MM |
4613 | if (duplicate_decls (decl, fn)) |
4614 | return fn; | |
4615 | } | |
8d08fdba | 4616 | } |
655dc6ee JM |
4617 | else if (old == error_mark_node) |
4618 | /* Ignore the undefined symbol marker. */ | |
4619 | old = NULL_TREE; | |
e1cd6e56 MS |
4620 | else |
4621 | { | |
8251199e | 4622 | cp_error_at ("previous non-function declaration `%#D'", old); |
33bd39a2 | 4623 | error ("conflicts with function declaration `%#D'", decl); |
a9aedbc2 | 4624 | return decl; |
e1cd6e56 | 4625 | } |
8d08fdba | 4626 | } |
7177d104 | 4627 | |
700f8a87 | 4628 | if (old || TREE_CODE (decl) == TEMPLATE_DECL) |
8d08fdba | 4629 | { |
2c73f9f5 | 4630 | if (old && TREE_CODE (old) != OVERLOAD) |
f181d4ae MM |
4631 | new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE)); |
4632 | else | |
4633 | new_binding = ovl_cons (decl, old); | |
347d73d7 ML |
4634 | if (flags & PUSH_USING) |
4635 | OVL_USED (new_binding) = 1; | |
8d08fdba MS |
4636 | } |
4637 | else | |
f181d4ae MM |
4638 | /* NAME is not ambiguous. */ |
4639 | new_binding = decl; | |
700f8a87 MS |
4640 | |
4641 | if (doing_global) | |
f181d4ae | 4642 | set_namespace_binding (name, current_namespace, new_binding); |
700f8a87 | 4643 | else |
f181d4ae MM |
4644 | { |
4645 | /* We only create an OVERLOAD if there was a previous binding at | |
0580c9aa ML |
4646 | this level, or if decl is a template. In the former case, we |
4647 | need to remove the old binding and replace it with the new | |
4648 | binding. We must also run through the NAMES on the binding | |
4649 | level where the name was bound to update the chain. */ | |
4650 | ||
4651 | if (TREE_CODE (new_binding) == OVERLOAD && old) | |
f181d4ae MM |
4652 | { |
4653 | tree *d; | |
68642fb6 | 4654 | |
f181d4ae MM |
4655 | for (d = &BINDING_LEVEL (IDENTIFIER_BINDING (name))->names; |
4656 | *d; | |
4657 | d = &TREE_CHAIN (*d)) | |
4658 | if (*d == old | |
4659 | || (TREE_CODE (*d) == TREE_LIST | |
4660 | && TREE_VALUE (*d) == old)) | |
4661 | { | |
d8f8dca1 MM |
4662 | if (TREE_CODE (*d) == TREE_LIST) |
4663 | /* Just replace the old binding with the new. */ | |
4664 | TREE_VALUE (*d) = new_binding; | |
4665 | else | |
4666 | /* Build a TREE_LIST to wrap the OVERLOAD. */ | |
68642fb6 | 4667 | *d = tree_cons (NULL_TREE, new_binding, |
ed5511d9 | 4668 | TREE_CHAIN (*d)); |
d8f8dca1 MM |
4669 | |
4670 | /* And update the CPLUS_BINDING node. */ | |
4671 | BINDING_VALUE (IDENTIFIER_BINDING (name)) | |
4672 | = new_binding; | |
4673 | return decl; | |
f181d4ae MM |
4674 | } |
4675 | ||
d8f8dca1 | 4676 | /* We should always find a previous binding in this case. */ |
a98facb0 | 4677 | abort (); |
f181d4ae MM |
4678 | } |
4679 | ||
4680 | /* Install the new binding. */ | |
0034cf72 | 4681 | push_local_binding (name, new_binding, flags); |
f181d4ae | 4682 | } |
700f8a87 | 4683 | |
8d08fdba MS |
4684 | return decl; |
4685 | } | |
4686 | \f | |
4687 | /* Generate an implicit declaration for identifier FUNCTIONID | |
4688 | as a function of type int (). Print a warning if appropriate. */ | |
4689 | ||
4690 | tree | |
4691 | implicitly_declare (functionid) | |
4692 | tree functionid; | |
4693 | { | |
4694 | register tree decl; | |
8d08fdba MS |
4695 | |
4696 | /* We used to reuse an old implicit decl here, | |
4697 | but this loses with inline functions because it can clobber | |
4698 | the saved decl chains. */ | |
4699 | decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type); | |
4700 | ||
4701 | DECL_EXTERNAL (decl) = 1; | |
4702 | TREE_PUBLIC (decl) = 1; | |
4703 | ||
cab1f180 | 4704 | /* ISO standard says implicit declarations are in the innermost block. |
d22c8596 | 4705 | So we record the decl in the standard fashion. */ |
8d08fdba | 4706 | pushdecl (decl); |
3e411c3f | 4707 | rest_of_decl_compilation (decl, NULL, 0, 0); |
8d08fdba MS |
4708 | |
4709 | if (warn_implicit | |
4710 | /* Only one warning per identifier. */ | |
4711 | && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE) | |
4712 | { | |
33bd39a2 | 4713 | pedwarn ("implicit declaration of function `%#D'", decl); |
8d08fdba MS |
4714 | } |
4715 | ||
4716 | SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl); | |
4717 | ||
8d08fdba MS |
4718 | return decl; |
4719 | } | |
4720 | ||
8251199e | 4721 | /* Return zero if the declaration NEWDECL is valid |
8d08fdba MS |
4722 | when the declaration OLDDECL (assumed to be for the same name) |
4723 | has already been seen. | |
4724 | Otherwise return an error message format string with a %s | |
4725 | where the identifier should go. */ | |
4726 | ||
d8e178a0 | 4727 | static const char * |
8d08fdba MS |
4728 | redeclaration_error_message (newdecl, olddecl) |
4729 | tree newdecl, olddecl; | |
4730 | { | |
4731 | if (TREE_CODE (newdecl) == TYPE_DECL) | |
4732 | { | |
4733 | /* Because C++ can put things into name space for free, | |
4734 | constructs like "typedef struct foo { ... } foo" | |
4735 | would look like an erroneous redeclaration. */ | |
3bfdc719 | 4736 | if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl))) |
8251199e | 4737 | return 0; |
8d08fdba | 4738 | else |
8251199e | 4739 | return "redefinition of `%#D'"; |
8d08fdba MS |
4740 | } |
4741 | else if (TREE_CODE (newdecl) == FUNCTION_DECL) | |
4742 | { | |
4743 | /* If this is a pure function, its olddecl will actually be | |
4744 | the original initialization to `0' (which we force to call | |
4745 | abort()). Don't complain about redefinition in this case. */ | |
fee7654e | 4746 | if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)) |
8251199e | 4747 | return 0; |
8d08fdba | 4748 | |
2c73f9f5 ML |
4749 | /* If both functions come from different namespaces, this is not |
4750 | a redeclaration - this is a conflict with a used function. */ | |
6eb3bb27 | 4751 | if (DECL_NAMESPACE_SCOPE_P (olddecl) |
2c73f9f5 | 4752 | && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)) |
8251199e | 4753 | return "`%D' conflicts with used function"; |
2c73f9f5 | 4754 | |
db5ae43f MS |
4755 | /* We'll complain about linkage mismatches in |
4756 | warn_extern_redeclared_static. */ | |
4757 | ||
2c73f9f5 | 4758 | /* Defining the same name twice is no good. */ |
8d08fdba | 4759 | if (DECL_INITIAL (olddecl) != NULL_TREE |
faae18ab | 4760 | && DECL_INITIAL (newdecl) != NULL_TREE) |
8d08fdba MS |
4761 | { |
4762 | if (DECL_NAME (olddecl) == NULL_TREE) | |
8251199e | 4763 | return "`%#D' not declared in class"; |
8d08fdba | 4764 | else |
8251199e | 4765 | return "redefinition of `%#D'"; |
8d08fdba | 4766 | } |
8251199e | 4767 | return 0; |
8d08fdba | 4768 | } |
51c184be MS |
4769 | else if (TREE_CODE (newdecl) == TEMPLATE_DECL) |
4770 | { | |
ec255269 | 4771 | if ((TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL |
5362b086 | 4772 | && (DECL_TEMPLATE_RESULT (newdecl) |
f4381d62 | 4773 | != DECL_TEMPLATE_RESULT (olddecl)) |
ec255269 MS |
4774 | && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl)) |
4775 | && DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl))) | |
4776 | || (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL | |
d0f062fb NS |
4777 | && COMPLETE_TYPE_P (TREE_TYPE (newdecl)) |
4778 | && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))) | |
8251199e JM |
4779 | return "redefinition of `%#D'"; |
4780 | return 0; | |
51c184be | 4781 | } |
1f51a992 | 4782 | else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl)) |
8d08fdba MS |
4783 | { |
4784 | /* Objects declared at top level: */ | |
4785 | /* If at least one is a reference, it's ok. */ | |
4786 | if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl)) | |
8251199e | 4787 | return 0; |
8926095f | 4788 | /* Reject two definitions. */ |
8251199e | 4789 | return "redefinition of `%#D'"; |
8d08fdba MS |
4790 | } |
4791 | else | |
4792 | { | |
4793 | /* Objects declared with block scope: */ | |
4794 | /* Reject two definitions, and reject a definition | |
4795 | together with an external reference. */ | |
4796 | if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl))) | |
8251199e JM |
4797 | return "redeclaration of `%#D'"; |
4798 | return 0; | |
8d08fdba MS |
4799 | } |
4800 | } | |
4801 | \f | |
acef433b | 4802 | /* Create a new label, named ID. */ |
8d08fdba | 4803 | |
acef433b MM |
4804 | static tree |
4805 | make_label_decl (id, local_p) | |
8d08fdba | 4806 | tree id; |
acef433b | 4807 | int local_p; |
8d08fdba | 4808 | { |
acef433b | 4809 | tree decl; |
8d08fdba | 4810 | |
acef433b | 4811 | decl = build_decl (LABEL_DECL, id, void_type_node); |
b35d4555 | 4812 | if (expanding_p) |
acef433b MM |
4813 | /* Make sure every label has an rtx. */ |
4814 | label_rtx (decl); | |
4815 | ||
4816 | DECL_CONTEXT (decl) = current_function_decl; | |
4817 | DECL_MODE (decl) = VOIDmode; | |
4818 | C_DECLARED_LABEL_FLAG (decl) = local_p; | |
8d08fdba | 4819 | |
acef433b MM |
4820 | /* Say where one reference is to the label, for the sake of the |
4821 | error if it is not defined. */ | |
4822 | DECL_SOURCE_LINE (decl) = lineno; | |
4823 | DECL_SOURCE_FILE (decl) = input_filename; | |
4824 | ||
4825 | /* Record the fact that this identifier is bound to this label. */ | |
4826 | SET_IDENTIFIER_LABEL_VALUE (id, decl); | |
4827 | ||
6625cdb5 JM |
4828 | return decl; |
4829 | } | |
4830 | ||
4831 | /* Record this label on the list of used labels so that we can check | |
4832 | at the end of the function to see whether or not the label was | |
4833 | actually defined, and so we can check when the label is defined whether | |
4834 | this use is valid. */ | |
4835 | ||
4836 | static void | |
4837 | use_label (decl) | |
4838 | tree decl; | |
4839 | { | |
4840 | if (named_label_uses == NULL | |
4841 | || named_label_uses->names_in_scope != current_binding_level->names | |
4842 | || named_label_uses->label_decl != decl) | |
4843 | { | |
4844 | struct named_label_use_list *new_ent; | |
4845 | new_ent = ((struct named_label_use_list *) | |
4846 | ggc_alloc (sizeof (struct named_label_use_list))); | |
e349ee73 MS |
4847 | new_ent->label_decl = decl; |
4848 | new_ent->names_in_scope = current_binding_level->names; | |
4849 | new_ent->binding_level = current_binding_level; | |
4850 | new_ent->lineno_o_goto = lineno; | |
4851 | new_ent->filename_o_goto = input_filename; | |
4852 | new_ent->next = named_label_uses; | |
4853 | named_label_uses = new_ent; | |
8d08fdba | 4854 | } |
acef433b | 4855 | } |
8d08fdba | 4856 | |
acef433b MM |
4857 | /* Look for a label named ID in the current function. If one cannot |
4858 | be found, create one. (We keep track of used, but undefined, | |
4859 | labels, and complain about them at the end of a function.) */ | |
8d08fdba | 4860 | |
68642fb6 | 4861 | tree |
acef433b MM |
4862 | lookup_label (id) |
4863 | tree id; | |
4864 | { | |
4865 | tree decl; | |
6625cdb5 | 4866 | struct named_label_list *ent; |
8d08fdba | 4867 | |
acef433b MM |
4868 | /* You can't use labels at global scope. */ |
4869 | if (current_function_decl == NULL_TREE) | |
4870 | { | |
4871 | error ("label `%s' referenced outside of any function", | |
4872 | IDENTIFIER_POINTER (id)); | |
4873 | return NULL_TREE; | |
4874 | } | |
68642fb6 | 4875 | |
acef433b MM |
4876 | /* See if we've already got this label. */ |
4877 | decl = IDENTIFIER_LABEL_VALUE (id); | |
4878 | if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl) | |
4879 | return decl; | |
8d08fdba | 4880 | |
acef433b MM |
4881 | /* Record this label on the list of labels used in this function. |
4882 | We do this before calling make_label_decl so that we get the | |
4883 | IDENTIFIER_LABEL_VALUE before the new label is declared. */ | |
6625cdb5 | 4884 | ent = ((struct named_label_list *) |
f8a83ee3 | 4885 | ggc_alloc_cleared (sizeof (struct named_label_list))); |
6625cdb5 JM |
4886 | ent->old_value = IDENTIFIER_LABEL_VALUE (id); |
4887 | ent->next = named_labels; | |
4888 | named_labels = ent; | |
4889 | ||
acef433b MM |
4890 | /* We need a new label. */ |
4891 | decl = make_label_decl (id, /*local_p=*/0); | |
6625cdb5 | 4892 | |
acef433b | 4893 | /* Now fill in the information we didn't have before. */ |
6625cdb5 | 4894 | ent->label_decl = decl; |
8d08fdba MS |
4895 | |
4896 | return decl; | |
4897 | } | |
4898 | ||
acef433b | 4899 | /* Declare a local label named ID. */ |
8d08fdba MS |
4900 | |
4901 | tree | |
acef433b MM |
4902 | declare_local_label (id) |
4903 | tree id; | |
8d08fdba | 4904 | { |
acef433b | 4905 | tree decl; |
8d08fdba | 4906 | |
acef433b MM |
4907 | /* Add a new entry to the SHADOWED_LABELS list so that when we leave |
4908 | this scope we can restore the old value of | |
4909 | IDENTIFIER_TYPE_VALUE. */ | |
68642fb6 | 4910 | current_binding_level->shadowed_labels |
acef433b MM |
4911 | = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE, |
4912 | current_binding_level->shadowed_labels); | |
4913 | /* Look for the label. */ | |
4914 | decl = make_label_decl (id, /*local_p=*/1); | |
4915 | /* Now fill in the information we didn't have before. */ | |
4916 | TREE_VALUE (current_binding_level->shadowed_labels) = decl; | |
68642fb6 | 4917 | |
acef433b | 4918 | return decl; |
8d08fdba MS |
4919 | } |
4920 | ||
6625cdb5 JM |
4921 | /* Returns nonzero if it is ill-formed to jump past the declaration of |
4922 | DECL. Returns 2 if it's also a real problem. */ | |
4923 | ||
4924 | static int | |
4925 | decl_jump_unsafe (decl) | |
4926 | tree decl; | |
4927 | { | |
4928 | if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl)) | |
4929 | return 0; | |
4930 | ||
4931 | if (DECL_INITIAL (decl) == NULL_TREE | |
4932 | && pod_type_p (TREE_TYPE (decl))) | |
4933 | return 0; | |
4934 | ||
4935 | /* This is really only important if we're crossing an initialization. | |
4936 | The POD stuff is just pedantry; why should it matter if the class | |
4937 | contains a field of pointer to member type? */ | |
4938 | if (DECL_INITIAL (decl) | |
4939 | || (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))) | |
4940 | return 2; | |
4941 | return 1; | |
4942 | } | |
4943 | ||
4944 | /* Check that a single previously seen jump to a newly defined label | |
4945 | is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for | |
4946 | the jump context; NAMES are the names in scope in LEVEL at the jump | |
4947 | context; FILE and LINE are the source position of the jump or 0. */ | |
4948 | ||
4949 | static void | |
4950 | check_previous_goto_1 (decl, level, names, file, line) | |
4951 | tree decl; | |
4952 | struct binding_level *level; | |
4953 | tree names; | |
4954 | const char *file; | |
4955 | int line; | |
4956 | { | |
4957 | int identified = 0; | |
4958 | int saw_eh = 0; | |
4959 | struct binding_level *b = current_binding_level; | |
4960 | for (; b; b = b->level_chain) | |
4961 | { | |
4962 | tree new_decls = b->names; | |
4963 | tree old_decls = (b == level ? names : NULL_TREE); | |
4964 | for (; new_decls != old_decls; | |
4965 | new_decls = TREE_CHAIN (new_decls)) | |
4966 | { | |
4967 | int problem = decl_jump_unsafe (new_decls); | |
4968 | if (! problem) | |
4969 | continue; | |
4970 | ||
4971 | if (! identified) | |
4972 | { | |
4973 | if (decl) | |
33bd39a2 | 4974 | pedwarn ("jump to label `%D'", decl); |
6625cdb5 JM |
4975 | else |
4976 | pedwarn ("jump to case label"); | |
4977 | ||
4978 | if (file) | |
4979 | pedwarn_with_file_and_line (file, line, " from here"); | |
4980 | identified = 1; | |
4981 | } | |
4982 | ||
826840d9 | 4983 | if (problem > 1) |
6625cdb5 JM |
4984 | cp_error_at (" crosses initialization of `%#D'", |
4985 | new_decls); | |
4986 | else | |
4987 | cp_pedwarn_at (" enters scope of non-POD `%#D'", | |
4988 | new_decls); | |
4989 | } | |
4990 | ||
4991 | if (b == level) | |
4992 | break; | |
826840d9 | 4993 | if ((b->is_try_scope || b->is_catch_scope) && ! saw_eh) |
6625cdb5 JM |
4994 | { |
4995 | if (! identified) | |
4996 | { | |
4997 | if (decl) | |
33bd39a2 | 4998 | pedwarn ("jump to label `%D'", decl); |
6625cdb5 JM |
4999 | else |
5000 | pedwarn ("jump to case label"); | |
5001 | ||
5002 | if (file) | |
5003 | pedwarn_with_file_and_line (file, line, " from here"); | |
5004 | identified = 1; | |
5005 | } | |
826840d9 RH |
5006 | if (b->is_try_scope) |
5007 | error (" enters try block"); | |
5008 | else | |
5009 | error (" enters catch block"); | |
6625cdb5 JM |
5010 | saw_eh = 1; |
5011 | } | |
5012 | } | |
5013 | } | |
5014 | ||
5015 | static void | |
5016 | check_previous_goto (use) | |
5017 | struct named_label_use_list *use; | |
5018 | { | |
5019 | check_previous_goto_1 (use->label_decl, use->binding_level, | |
5020 | use->names_in_scope, use->filename_o_goto, | |
5021 | use->lineno_o_goto); | |
5022 | } | |
5023 | ||
5024 | static void | |
5025 | check_switch_goto (level) | |
5026 | struct binding_level *level; | |
5027 | { | |
5028 | check_previous_goto_1 (NULL_TREE, level, level->names, NULL, 0); | |
5029 | } | |
5030 | ||
5031 | /* Check that any previously seen jumps to a newly defined label DECL | |
5032 | are OK. Called by define_label. */ | |
5033 | ||
5034 | static void | |
5035 | check_previous_gotos (decl) | |
5036 | tree decl; | |
5037 | { | |
5038 | struct named_label_use_list **usep; | |
5039 | ||
5040 | if (! TREE_USED (decl)) | |
5041 | return; | |
5042 | ||
5043 | for (usep = &named_label_uses; *usep; ) | |
5044 | { | |
5045 | struct named_label_use_list *use = *usep; | |
5046 | if (use->label_decl == decl) | |
5047 | { | |
5048 | check_previous_goto (use); | |
5049 | *usep = use->next; | |
5050 | } | |
5051 | else | |
5052 | usep = &(use->next); | |
5053 | } | |
5054 | } | |
5055 | ||
5056 | /* Check that a new jump to a label DECL is OK. Called by | |
5057 | finish_goto_stmt. */ | |
5058 | ||
5059 | void | |
5060 | check_goto (decl) | |
5061 | tree decl; | |
5062 | { | |
5063 | int identified = 0; | |
5064 | tree bad; | |
5065 | struct named_label_list *lab; | |
5066 | ||
e3cd9945 APB |
5067 | /* We can't know where a computed goto is jumping. So we assume |
5068 | that it's OK. */ | |
5069 | if (! DECL_P (decl)) | |
5070 | return; | |
5071 | ||
6625cdb5 JM |
5072 | /* If the label hasn't been defined yet, defer checking. */ |
5073 | if (! DECL_INITIAL (decl)) | |
5074 | { | |
5075 | use_label (decl); | |
5076 | return; | |
5077 | } | |
5078 | ||
5079 | for (lab = named_labels; lab; lab = lab->next) | |
5080 | if (decl == lab->label_decl) | |
5081 | break; | |
5082 | ||
5083 | /* If the label is not on named_labels it's a gcc local label, so | |
5084 | it must be in an outer scope, so jumping to it is always OK. */ | |
5085 | if (lab == 0) | |
5086 | return; | |
5087 | ||
826840d9 RH |
5088 | if ((lab->in_try_scope || lab->in_catch_scope || lab->bad_decls) |
5089 | && !identified) | |
6625cdb5 JM |
5090 | { |
5091 | cp_pedwarn_at ("jump to label `%D'", decl); | |
5092 | pedwarn (" from here"); | |
5093 | identified = 1; | |
5094 | } | |
5095 | ||
5096 | for (bad = lab->bad_decls; bad; bad = TREE_CHAIN (bad)) | |
5097 | { | |
5098 | tree b = TREE_VALUE (bad); | |
5099 | int u = decl_jump_unsafe (b); | |
5100 | ||
5101 | if (u > 1 && DECL_ARTIFICIAL (b)) | |
5102 | /* Can't skip init of __exception_info. */ | |
5103 | cp_error_at (" enters catch block", b); | |
5104 | else if (u > 1) | |
5105 | cp_error_at (" skips initialization of `%#D'", b); | |
5106 | else | |
5107 | cp_pedwarn_at (" enters scope of non-POD `%#D'", b); | |
5108 | } | |
5109 | ||
826840d9 | 5110 | if (lab->in_try_scope) |
6625cdb5 | 5111 | error (" enters try block"); |
826840d9 RH |
5112 | else if (lab->in_catch_scope) |
5113 | error (" enters catch block"); | |
6625cdb5 JM |
5114 | } |
5115 | ||
8d08fdba MS |
5116 | /* Define a label, specifying the location in the source file. |
5117 | Return the LABEL_DECL node for the label, if the definition is valid. | |
5118 | Otherwise return 0. */ | |
5119 | ||
5120 | tree | |
5121 | define_label (filename, line, name) | |
3b304f5b | 5122 | const char *filename; |
8d08fdba MS |
5123 | int line; |
5124 | tree name; | |
5125 | { | |
f01b0acb | 5126 | tree decl = lookup_label (name); |
6625cdb5 | 5127 | struct named_label_list *ent; |
3dc9aec6 | 5128 | register struct binding_level *p; |
6625cdb5 JM |
5129 | |
5130 | for (ent = named_labels; ent; ent = ent->next) | |
5131 | if (ent->label_decl == decl) | |
5132 | break; | |
8d08fdba | 5133 | |
3dc9aec6 | 5134 | /* After labels, make any new cleanups in the function go into their |
8d08fdba | 5135 | own new (temporary) binding contour. */ |
3dc9aec6 WC |
5136 | for (p = current_binding_level; !(p->parm_flag); p = p->level_chain) |
5137 | p->more_cleanups_ok = 0; | |
8d08fdba | 5138 | |
e1cd6e56 | 5139 | if (name == get_identifier ("wchar_t")) |
33bd39a2 | 5140 | pedwarn ("label named wchar_t"); |
e1cd6e56 | 5141 | |
8d08fdba MS |
5142 | if (DECL_INITIAL (decl) != NULL_TREE) |
5143 | { | |
33bd39a2 | 5144 | error ("duplicate label `%D'", decl); |
8d08fdba MS |
5145 | return 0; |
5146 | } | |
5147 | else | |
5148 | { | |
8d08fdba MS |
5149 | /* Mark label as having been defined. */ |
5150 | DECL_INITIAL (decl) = error_mark_node; | |
5151 | /* Say where in the source. */ | |
5152 | DECL_SOURCE_FILE (decl) = filename; | |
5153 | DECL_SOURCE_LINE (decl) = line; | |
6625cdb5 JM |
5154 | if (ent) |
5155 | { | |
5156 | ent->names_in_scope = current_binding_level->names; | |
5157 | ent->binding_level = current_binding_level; | |
5158 | } | |
5159 | check_previous_gotos (decl); | |
8d08fdba MS |
5160 | return decl; |
5161 | } | |
5162 | } | |
5163 | ||
a5894242 MS |
5164 | struct cp_switch |
5165 | { | |
5166 | struct binding_level *level; | |
5167 | struct cp_switch *next; | |
56cb9733 MM |
5168 | /* The SWITCH_STMT being built. */ |
5169 | tree switch_stmt; | |
5170 | /* A splay-tree mapping the low element of a case range to the high | |
5171 | element, or NULL_TREE if there is no high element. Used to | |
5172 | determine whether or not a new case label duplicates an old case | |
5173 | label. We need a tree, rather than simply a hash table, because | |
5174 | of the GNU case range extension. */ | |
5175 | splay_tree cases; | |
a5894242 MS |
5176 | }; |
5177 | ||
56cb9733 MM |
5178 | /* A stack of the currently active switch statements. The innermost |
5179 | switch statement is on the top of the stack. There is no need to | |
5180 | mark the stack for garbage collection because it is only active | |
5181 | during the processing of the body of a function, and we never | |
5182 | collect at that point. */ | |
5362b086 | 5183 | |
a5894242 MS |
5184 | static struct cp_switch *switch_stack; |
5185 | ||
56cb9733 MM |
5186 | /* Called right after a switch-statement condition is parsed. |
5187 | SWITCH_STMT is the switch statement being parsed. */ | |
5188 | ||
a5894242 | 5189 | void |
56cb9733 MM |
5190 | push_switch (switch_stmt) |
5191 | tree switch_stmt; | |
a5894242 MS |
5192 | { |
5193 | struct cp_switch *p | |
bedda2da | 5194 | = (struct cp_switch *) xmalloc (sizeof (struct cp_switch)); |
a5894242 MS |
5195 | p->level = current_binding_level; |
5196 | p->next = switch_stack; | |
56cb9733 MM |
5197 | p->switch_stmt = switch_stmt; |
5198 | p->cases = splay_tree_new (case_compare, NULL, NULL); | |
a5894242 MS |
5199 | switch_stack = p; |
5200 | } | |
5201 | ||
5202 | void | |
5203 | pop_switch () | |
5204 | { | |
bedda2da | 5205 | struct cp_switch *cs; |
5362b086 | 5206 | |
bedda2da | 5207 | cs = switch_stack; |
56cb9733 | 5208 | splay_tree_delete (cs->cases); |
a5894242 | 5209 | switch_stack = switch_stack->next; |
bedda2da | 5210 | free (cs); |
a5894242 MS |
5211 | } |
5212 | ||
b0a1da19 JM |
5213 | /* Note that we've seen a definition of a case label, and complain if this |
5214 | is a bad place for one. */ | |
e92cc029 | 5215 | |
3e4d04a1 | 5216 | tree |
56cb9733 MM |
5217 | finish_case_label (low_value, high_value) |
5218 | tree low_value; | |
5219 | tree high_value; | |
8d08fdba | 5220 | { |
3e4d04a1 | 5221 | tree cond, r; |
3dc9aec6 | 5222 | register struct binding_level *p; |
a5894242 | 5223 | |
b0a1da19 | 5224 | if (! switch_stack) |
56cb9733 MM |
5225 | { |
5226 | if (high_value) | |
5227 | error ("case label not within a switch statement"); | |
5228 | else if (low_value) | |
33bd39a2 | 5229 | error ("case label `%E' not within a switch statement", |
56cb9733 MM |
5230 | low_value); |
5231 | else | |
5232 | error ("`default' label not within a switch statement"); | |
3e4d04a1 | 5233 | return NULL_TREE; |
56cb9733 MM |
5234 | } |
5235 | ||
56cb9733 MM |
5236 | if (processing_template_decl) |
5237 | { | |
8f17b5c5 MM |
5238 | tree label; |
5239 | ||
56cb9733 MM |
5240 | /* For templates, just add the case label; we'll do semantic |
5241 | analysis at instantiation-time. */ | |
8f17b5c5 | 5242 | label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE); |
3e4d04a1 | 5243 | return add_stmt (build_case_label (low_value, high_value, label)); |
56cb9733 MM |
5244 | } |
5245 | ||
5246 | /* Find the condition on which this switch statement depends. */ | |
5247 | cond = SWITCH_COND (switch_stack->switch_stmt); | |
5248 | if (cond && TREE_CODE (cond) == TREE_LIST) | |
5249 | cond = TREE_VALUE (cond); | |
56cb9733 | 5250 | |
3e4d04a1 RH |
5251 | r = c_add_case_label (switch_stack->cases, cond, low_value, high_value); |
5252 | if (r == error_mark_node) | |
5253 | r = NULL_TREE; | |
8d08fdba | 5254 | |
6625cdb5 | 5255 | check_switch_goto (switch_stack->level); |
a5894242 | 5256 | |
3dc9aec6 | 5257 | /* After labels, make any new cleanups in the function go into their |
8d08fdba | 5258 | own new (temporary) binding contour. */ |
3dc9aec6 WC |
5259 | for (p = current_binding_level; !(p->parm_flag); p = p->level_chain) |
5260 | p->more_cleanups_ok = 0; | |
3e4d04a1 RH |
5261 | |
5262 | return r; | |
8d08fdba MS |
5263 | } |
5264 | \f | |
5265 | /* Return the list of declarations of the current level. | |
5266 | Note that this list is in reverse order unless/until | |
5267 | you nreverse it; and when you do nreverse it, you must | |
5268 | store the result back using `storedecls' or you will lose. */ | |
5269 | ||
5270 | tree | |
5271 | getdecls () | |
5272 | { | |
5273 | return current_binding_level->names; | |
5274 | } | |
5275 | ||
5276 | /* Return the list of type-tags (for structs, etc) of the current level. */ | |
5277 | ||
5278 | tree | |
5279 | gettags () | |
5280 | { | |
5281 | return current_binding_level->tags; | |
5282 | } | |
5283 | ||
5284 | /* Store the list of declarations of the current level. | |
5285 | This is done for the parameter declarations of a function being defined, | |
5286 | after they are modified in the light of any missing parameters. */ | |
5287 | ||
5288 | static void | |
5289 | storedecls (decls) | |
5290 | tree decls; | |
5291 | { | |
5292 | current_binding_level->names = decls; | |
5293 | } | |
5294 | ||
5295 | /* Similarly, store the list of tags of the current level. */ | |
5296 | ||
280f9385 | 5297 | void |
8d08fdba MS |
5298 | storetags (tags) |
5299 | tree tags; | |
5300 | { | |
5301 | current_binding_level->tags = tags; | |
5302 | } | |
5303 | \f | |
4782bd5b RS |
5304 | /* Return the type that should be used when TYPE's name is preceded |
5305 | by a tag such as 'struct' or 'union', or null if the name cannot | |
5306 | be used in this way. | |
5307 | ||
5308 | For example, when processing the third line of: | |
5309 | ||
5310 | struct A; | |
5311 | typedef struct A A; | |
5312 | struct A; | |
5313 | ||
5314 | lookup of A will find the typedef. Given A's typedef, this function | |
5315 | will return the type associated with "struct A". For the tag to be | |
5316 | anything other than TYPE, TYPE must be a typedef whose original type | |
5317 | has the same name and context as TYPE itself. | |
5318 | ||
5319 | It is not valid for a typedef of an anonymous type to be used with | |
5320 | an explicit tag: | |
5321 | ||
5322 | typedef struct { ... } B; | |
5323 | struct B; | |
5324 | ||
5325 | Return null for this case. */ | |
5326 | ||
5327 | static tree | |
5328 | follow_tag_typedef (type) | |
5329 | tree type; | |
5330 | { | |
5331 | tree original; | |
5332 | ||
5333 | original = original_type (type); | |
5334 | if (TYPE_IDENTIFIER (original) == TYPE_IDENTIFIER (type) | |
5335 | && (CP_DECL_CONTEXT (TYPE_NAME (original)) | |
5336 | == CP_DECL_CONTEXT (TYPE_NAME (type))) | |
5337 | && !(CLASS_TYPE_P (original) && TYPE_WAS_ANONYMOUS (original))) | |
5338 | return original; | |
5339 | else | |
5340 | return NULL_TREE; | |
5341 | } | |
5342 | ||
8d08fdba MS |
5343 | /* Given NAME, an IDENTIFIER_NODE, |
5344 | return the structure (or union or enum) definition for that name. | |
5345 | Searches binding levels from BINDING_LEVEL up to the global level. | |
5346 | If THISLEVEL_ONLY is nonzero, searches only the specified context | |
5347 | (but skips any tag-transparent contexts to find one that is | |
5348 | meaningful for tags). | |
5349 | FORM says which kind of type the caller wants; | |
5350 | it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE. | |
5351 | If the wrong kind of type is found, and it's not a template, an error is | |
5352 | reported. */ | |
5353 | ||
5354 | static tree | |
5355 | lookup_tag (form, name, binding_level, thislevel_only) | |
5356 | enum tree_code form; | |
8d08fdba | 5357 | tree name; |
cffa8729 | 5358 | struct binding_level *binding_level; |
8d08fdba MS |
5359 | int thislevel_only; |
5360 | { | |
5361 | register struct binding_level *level; | |
74b846e0 MM |
5362 | /* Non-zero if, we should look past a template parameter level, even |
5363 | if THISLEVEL_ONLY. */ | |
5364 | int allow_template_parms_p = 1; | |
8d08fdba MS |
5365 | |
5366 | for (level = binding_level; level; level = level->level_chain) | |
5367 | { | |
5368 | register tree tail; | |
5369 | if (ANON_AGGRNAME_P (name)) | |
5370 | for (tail = level->tags; tail; tail = TREE_CHAIN (tail)) | |
5371 | { | |
5372 | /* There's no need for error checking here, because | |
5373 | anon names are unique throughout the compilation. */ | |
5374 | if (TYPE_IDENTIFIER (TREE_VALUE (tail)) == name) | |
5375 | return TREE_VALUE (tail); | |
5376 | } | |
2c73f9f5 ML |
5377 | else if (level->namespace_p) |
5378 | /* Do namespace lookup. */ | |
6c011b01 | 5379 | for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail)) |
2c73f9f5 | 5380 | { |
36a117a5 MM |
5381 | tree old = binding_for_name (name, tail); |
5382 | ||
74b846e0 MM |
5383 | /* If we just skipped past a template parameter level, |
5384 | even though THISLEVEL_ONLY, and we find a template | |
5385 | class declaration, then we use the _TYPE node for the | |
36a117a5 | 5386 | template. See the example below. */ |
74b846e0 | 5387 | if (thislevel_only && !allow_template_parms_p |
68642fb6 | 5388 | && old && BINDING_VALUE (old) |
36a117a5 MM |
5389 | && DECL_CLASS_TEMPLATE_P (BINDING_VALUE (old))) |
5390 | old = TREE_TYPE (BINDING_VALUE (old)); | |
68642fb6 | 5391 | else |
36a117a5 MM |
5392 | old = BINDING_TYPE (old); |
5393 | ||
4782bd5b | 5394 | if (old) |
2c73f9f5 | 5395 | { |
4782bd5b RS |
5396 | /* We've found something at this binding level. If it is |
5397 | a typedef, extract the tag it refers to. Lookup fails | |
5398 | if the typedef doesn't refer to a taggable type. */ | |
5399 | old = follow_tag_typedef (old); | |
5400 | if (!old) | |
5401 | return NULL_TREE; | |
5402 | if (TREE_CODE (old) != form | |
5403 | && (form == ENUMERAL_TYPE | |
5404 | || TREE_CODE (old) == ENUMERAL_TYPE)) | |
5405 | { | |
5406 | error ("`%#D' redeclared as %C", old, form); | |
5407 | return NULL_TREE; | |
5408 | } | |
5409 | return old; | |
2c73f9f5 | 5410 | } |
2c73f9f5 ML |
5411 | if (thislevel_only || tail == global_namespace) |
5412 | return NULL_TREE; | |
5413 | } | |
8d08fdba MS |
5414 | else |
5415 | for (tail = level->tags; tail; tail = TREE_CHAIN (tail)) | |
5416 | { | |
a80e4195 | 5417 | if (TREE_PURPOSE (tail) == name) |
8d08fdba MS |
5418 | { |
5419 | enum tree_code code = TREE_CODE (TREE_VALUE (tail)); | |
fd87b7a7 | 5420 | |
8d08fdba | 5421 | if (code != form |
fd87b7a7 | 5422 | && (form == ENUMERAL_TYPE || code == ENUMERAL_TYPE)) |
8d08fdba MS |
5423 | { |
5424 | /* Definition isn't the kind we were looking for. */ | |
fd87b7a7 | 5425 | error ("`%#D' redeclared as %C", TREE_VALUE (tail), form); |
72b7eeff | 5426 | return NULL_TREE; |
8d08fdba MS |
5427 | } |
5428 | return TREE_VALUE (tail); | |
5429 | } | |
5430 | } | |
5431 | if (thislevel_only && ! level->tag_transparent) | |
5566b478 | 5432 | { |
74b846e0 | 5433 | if (level->template_parms_p && allow_template_parms_p) |
5566b478 | 5434 | { |
36a117a5 | 5435 | /* We must deal with cases like this: |
68642fb6 | 5436 | |
36a117a5 MM |
5437 | template <class T> struct S; |
5438 | template <class T> struct S {}; | |
68642fb6 | 5439 | |
36a117a5 MM |
5440 | When looking up `S', for the second declaration, we |
5441 | would like to find the first declaration. But, we | |
5442 | are in the pseudo-global level created for the | |
5443 | template parameters, rather than the (surrounding) | |
5444 | namespace level. Thus, we keep going one more level, | |
5445 | even though THISLEVEL_ONLY is non-zero. */ | |
74b846e0 | 5446 | allow_template_parms_p = 0; |
36a117a5 | 5447 | continue; |
5566b478 | 5448 | } |
36a117a5 MM |
5449 | else |
5450 | return NULL_TREE; | |
5566b478 | 5451 | } |
8d08fdba MS |
5452 | } |
5453 | return NULL_TREE; | |
5454 | } | |
5455 | ||
bd6dd845 | 5456 | #if 0 |
8d08fdba MS |
5457 | void |
5458 | set_current_level_tags_transparency (tags_transparent) | |
5459 | int tags_transparent; | |
5460 | { | |
5461 | current_binding_level->tag_transparent = tags_transparent; | |
5462 | } | |
bd6dd845 | 5463 | #endif |
8d08fdba MS |
5464 | |
5465 | /* Given a type, find the tag that was defined for it and return the tag name. | |
5466 | Otherwise return 0. However, the value can never be 0 | |
5467 | in the cases in which this is used. | |
5468 | ||
5469 | C++: If NAME is non-zero, this is the new name to install. This is | |
5470 | done when replacing anonymous tags with real tag names. */ | |
5471 | ||
5472 | static tree | |
5473 | lookup_tag_reverse (type, name) | |
5474 | tree type; | |
5475 | tree name; | |
5476 | { | |
5477 | register struct binding_level *level; | |
5478 | ||
5479 | for (level = current_binding_level; level; level = level->level_chain) | |
5480 | { | |
5481 | register tree tail; | |
5482 | for (tail = level->tags; tail; tail = TREE_CHAIN (tail)) | |
5483 | { | |
5484 | if (TREE_VALUE (tail) == type) | |
5485 | { | |
5486 | if (name) | |
5487 | TREE_PURPOSE (tail) = name; | |
5488 | return TREE_PURPOSE (tail); | |
5489 | } | |
5490 | } | |
5491 | } | |
5492 | return NULL_TREE; | |
5493 | } | |
8d08fdba | 5494 | \f |
a9aedbc2 | 5495 | /* Look up NAME in the NAMESPACE. */ |
e92cc029 | 5496 | |
a9aedbc2 MS |
5497 | tree |
5498 | lookup_namespace_name (namespace, name) | |
5499 | tree namespace, name; | |
5500 | { | |
30394414 | 5501 | tree val; |
f30c84c9 | 5502 | tree template_id = NULL_TREE; |
2c73f9f5 | 5503 | |
30394414 | 5504 | my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370); |
1231fb96 | 5505 | |
1231fb96 | 5506 | if (TREE_CODE (name) == NAMESPACE_DECL) |
d8f8dca1 | 5507 | /* This happens for A::B<int> when B is a namespace. */ |
1231fb96 | 5508 | return name; |
d8f8dca1 MM |
5509 | else if (TREE_CODE (name) == TEMPLATE_DECL) |
5510 | { | |
5511 | /* This happens for A::B where B is a template, and there are no | |
5512 | template arguments. */ | |
33bd39a2 | 5513 | error ("invalid use of `%D'", name); |
d8f8dca1 MM |
5514 | return error_mark_node; |
5515 | } | |
1231fb96 | 5516 | |
b262d64c JM |
5517 | namespace = ORIGINAL_NAMESPACE (namespace); |
5518 | ||
f30c84c9 MM |
5519 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) |
5520 | { | |
5521 | template_id = name; | |
5522 | name = TREE_OPERAND (name, 0); | |
5523 | if (TREE_CODE (name) == OVERLOAD) | |
5524 | name = DECL_NAME (OVL_CURRENT (name)); | |
2f939d94 | 5525 | else if (DECL_P (name)) |
f30c84c9 MM |
5526 | name = DECL_NAME (name); |
5527 | } | |
5528 | ||
30394414 | 5529 | my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373); |
68642fb6 | 5530 | |
87e3dbc9 | 5531 | val = make_node (CPLUS_BINDING); |
52c11ef6 | 5532 | if (!qualified_lookup_using_namespace (name, namespace, val, 0)) |
2c73f9f5 ML |
5533 | return error_mark_node; |
5534 | ||
5535 | if (BINDING_VALUE (val)) | |
1c35f5b6 JM |
5536 | { |
5537 | val = BINDING_VALUE (val); | |
5538 | ||
f30c84c9 MM |
5539 | if (template_id) |
5540 | { | |
5541 | if (DECL_CLASS_TEMPLATE_P (val)) | |
68642fb6 | 5542 | val = lookup_template_class (val, |
f30c84c9 MM |
5543 | TREE_OPERAND (template_id, 1), |
5544 | /*in_decl=*/NULL_TREE, | |
5545 | /*context=*/NULL_TREE, | |
f9c244b8 | 5546 | /*entering_scope=*/0, |
c2ea3a40 | 5547 | tf_error | tf_warning); |
f30c84c9 MM |
5548 | else if (DECL_FUNCTION_TEMPLATE_P (val) |
5549 | || TREE_CODE (val) == OVERLOAD) | |
68642fb6 | 5550 | val = lookup_template_function (val, |
f30c84c9 MM |
5551 | TREE_OPERAND (template_id, 1)); |
5552 | else | |
5553 | { | |
33bd39a2 | 5554 | error ("`%D::%D' is not a template", |
f30c84c9 MM |
5555 | namespace, name); |
5556 | return error_mark_node; | |
5557 | } | |
5558 | } | |
5559 | ||
1c35f5b6 JM |
5560 | /* If we have a single function from a using decl, pull it out. */ |
5561 | if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val)) | |
5562 | val = OVL_FUNCTION (val); | |
d52e4867 RS |
5563 | |
5564 | /* Ignore built-in functions that haven't been prototyped yet. */ | |
5565 | if (!val || !DECL_P(val) | |
5566 | || !DECL_LANG_SPECIFIC(val) | |
5567 | || !DECL_ANTICIPATED (val)) | |
5568 | return val; | |
1c35f5b6 JM |
5569 | } |
5570 | ||
33bd39a2 | 5571 | error ("`%D' undeclared in namespace `%D'", name, namespace); |
30394414 | 5572 | return error_mark_node; |
a9aedbc2 MS |
5573 | } |
5574 | ||
7ddedda4 MM |
5575 | /* Hash a TYPENAME_TYPE. K is really of type `tree'. */ |
5576 | ||
5577 | static unsigned long | |
5578 | typename_hash (k) | |
5579 | hash_table_key k; | |
5580 | { | |
5581 | unsigned long hash; | |
5582 | tree t; | |
5583 | ||
5584 | t = (tree) k; | |
5585 | hash = (((unsigned long) TYPE_CONTEXT (t)) | |
5586 | ^ ((unsigned long) DECL_NAME (TYPE_NAME (t)))); | |
5587 | ||
5588 | return hash; | |
5589 | } | |
5590 | ||
5591 | /* Compare two TYPENAME_TYPEs. K1 and K2 are really of type `tree'. */ | |
5592 | ||
d6edb99e | 5593 | static bool |
7ddedda4 MM |
5594 | typename_compare (k1, k2) |
5595 | hash_table_key k1; | |
5596 | hash_table_key k2; | |
5597 | { | |
5598 | tree t1; | |
5599 | tree t2; | |
5600 | tree d1; | |
5601 | tree d2; | |
5602 | ||
5603 | t1 = (tree) k1; | |
5604 | t2 = (tree) k2; | |
5605 | d1 = TYPE_NAME (t1); | |
5606 | d2 = TYPE_NAME (t2); | |
68642fb6 | 5607 | |
7ddedda4 MM |
5608 | return (DECL_NAME (d1) == DECL_NAME (d2) |
5609 | && same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)) | |
68642fb6 | 5610 | && ((TREE_TYPE (t1) != NULL_TREE) |
7ddedda4 MM |
5611 | == (TREE_TYPE (t2) != NULL_TREE)) |
5612 | && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)) | |
5613 | && TYPENAME_TYPE_FULLNAME (t1) == TYPENAME_TYPE_FULLNAME (t2)); | |
5614 | } | |
5615 | ||
45869a6c MM |
5616 | /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is |
5617 | the type of `T', NAME is the IDENTIFIER_NODE for `t'. If BASE_TYPE | |
5618 | is non-NULL, this type is being created by the implicit typename | |
5619 | extension, and BASE_TYPE is a type named `t' in some base class of | |
68642fb6 | 5620 | `T' which depends on template parameters. |
45869a6c MM |
5621 | |
5622 | Returns the new TYPENAME_TYPE. */ | |
5623 | ||
5624 | tree | |
5625 | build_typename_type (context, name, fullname, base_type) | |
5626 | tree context; | |
5627 | tree name; | |
5628 | tree fullname; | |
5629 | tree base_type; | |
5630 | { | |
5631 | tree t; | |
5632 | tree d; | |
400500c4 | 5633 | struct hash_entry *e; |
45869a6c | 5634 | |
7ddedda4 MM |
5635 | static struct hash_table ht; |
5636 | ||
9cd64686 MM |
5637 | if (!ht.table) |
5638 | { | |
5639 | static struct hash_table *h = &ht; | |
400500c4 RK |
5640 | |
5641 | hash_table_init (&ht, &hash_newfunc, &typename_hash, &typename_compare); | |
9cd64686 MM |
5642 | ggc_add_tree_hash_table_root (&h, 1); |
5643 | } | |
45869a6c MM |
5644 | |
5645 | /* Build the TYPENAME_TYPE. */ | |
33848bb0 | 5646 | t = make_aggr_type (TYPENAME_TYPE); |
45869a6c MM |
5647 | TYPE_CONTEXT (t) = FROB_CONTEXT (context); |
5648 | TYPENAME_TYPE_FULLNAME (t) = fullname; | |
5649 | TREE_TYPE (t) = base_type; | |
45869a6c MM |
5650 | |
5651 | /* Build the corresponding TYPE_DECL. */ | |
5652 | d = build_decl (TYPE_DECL, name, t); | |
5653 | TYPE_NAME (TREE_TYPE (d)) = d; | |
5654 | TYPE_STUB_DECL (TREE_TYPE (d)) = d; | |
5655 | DECL_CONTEXT (d) = FROB_CONTEXT (context); | |
2642b9bf | 5656 | DECL_ARTIFICIAL (d) = 1; |
45869a6c | 5657 | |
7ddedda4 MM |
5658 | /* See if we already have this type. */ |
5659 | e = hash_lookup (&ht, t, /*create=*/false, /*copy=*/0); | |
5660 | if (e) | |
87e3dbc9 | 5661 | t = (tree) e->key; |
7ddedda4 MM |
5662 | else |
5663 | /* Insert the type into the table. */ | |
5664 | hash_lookup (&ht, t, /*create=*/true, /*copy=*/0); | |
5665 | ||
45869a6c MM |
5666 | return t; |
5667 | } | |
5668 | ||
3baa501d MM |
5669 | /* Resolve `typename CONTEXT::NAME'. Returns an appropriate type, |
5670 | unless an error occurs, in which case error_mark_node is returned. | |
4f2b0fb2 NS |
5671 | If we locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is |
5672 | set, we return that, rather than the _TYPE it corresponds to, in | |
5673 | other cases we look through the type decl. If TF_ERROR is set, | |
5674 | complain about errors, otherwise be quiet. */ | |
3baa501d | 5675 | |
5566b478 | 5676 | tree |
3baa501d | 5677 | make_typename_type (context, name, complain) |
5566b478 | 5678 | tree context, name; |
4f2b0fb2 | 5679 | tsubst_flags_t complain; |
5566b478 | 5680 | { |
b2b7d40a | 5681 | tree fullname; |
a80e4195 | 5682 | |
2f939d94 | 5683 | if (TYPE_P (name)) |
78638e24 | 5684 | { |
68642fb6 UD |
5685 | if (!(TYPE_LANG_SPECIFIC (name) |
5686 | && (CLASSTYPE_IS_TEMPLATE (name) | |
78638e24 MM |
5687 | || CLASSTYPE_USE_TEMPLATE (name)))) |
5688 | name = TYPE_IDENTIFIER (name); | |
5689 | else | |
5690 | /* Create a TEMPLATE_ID_EXPR for the type. */ | |
5691 | name = build_nt (TEMPLATE_ID_EXPR, | |
5692 | CLASSTYPE_TI_TEMPLATE (name), | |
5693 | CLASSTYPE_TI_ARGS (name)); | |
5694 | } | |
653cc74a | 5695 | else if (TREE_CODE (name) == TYPE_DECL) |
a80e4195 | 5696 | name = DECL_NAME (name); |
b2b7d40a JM |
5697 | |
5698 | fullname = name; | |
5699 | ||
5700 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) | |
11686454 JM |
5701 | { |
5702 | name = TREE_OPERAND (name, 0); | |
5703 | if (TREE_CODE (name) == TEMPLATE_DECL) | |
5704 | name = TREE_OPERAND (fullname, 0) = DECL_NAME (name); | |
5705 | } | |
8a2b77e7 JM |
5706 | if (TREE_CODE (name) == TEMPLATE_DECL) |
5707 | { | |
33bd39a2 | 5708 | error ("`%D' used without template parameters", name); |
8a2b77e7 JM |
5709 | return error_mark_node; |
5710 | } | |
b2b7d40a | 5711 | if (TREE_CODE (name) != IDENTIFIER_NODE) |
a98facb0 | 5712 | abort (); |
5566b478 | 5713 | |
04ddee1b BK |
5714 | if (TREE_CODE (context) == NAMESPACE_DECL) |
5715 | { | |
5716 | /* We can get here from typename_sub0 in the explicit_template_type | |
5717 | expansion. Just fail. */ | |
4f2b0fb2 | 5718 | if (complain & tf_error) |
33bd39a2 | 5719 | error ("no class template named `%#T' in `%#T'", |
3baa501d | 5720 | name, context); |
04ddee1b BK |
5721 | return error_mark_node; |
5722 | } | |
5723 | ||
85b71cf2 | 5724 | if (! uses_template_parms (context) |
b77ead33 | 5725 | || currently_open_class (context)) |
5566b478 | 5726 | { |
b2b7d40a JM |
5727 | if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR) |
5728 | { | |
ad810b22 | 5729 | tree tmpl = NULL_TREE; |
b2b7d40a | 5730 | if (IS_AGGR_TYPE (context)) |
ad810b22 MM |
5731 | tmpl = lookup_field (context, name, 0, 0); |
5732 | if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl)) | |
b2b7d40a | 5733 | { |
4f2b0fb2 | 5734 | if (complain & tf_error) |
33bd39a2 | 5735 | error ("no class template named `%#T' in `%#T'", |
3baa501d | 5736 | name, context); |
b2b7d40a JM |
5737 | return error_mark_node; |
5738 | } | |
ffb690bd | 5739 | |
68642fb6 | 5740 | return lookup_template_class (tmpl, |
ad810b22 | 5741 | TREE_OPERAND (fullname, 1), |
68642fb6 | 5742 | NULL_TREE, context, |
f9c244b8 | 5743 | /*entering_scope=*/0, |
c2ea3a40 | 5744 | tf_error | tf_warning); |
b2b7d40a JM |
5745 | } |
5746 | else | |
5566b478 | 5747 | { |
b4f70b3d | 5748 | tree t; |
68642fb6 | 5749 | |
b4f70b3d | 5750 | if (!IS_AGGR_TYPE (context)) |
b2b7d40a | 5751 | { |
4f2b0fb2 | 5752 | if (complain & tf_error) |
33bd39a2 | 5753 | error ("no type named `%#T' in `%#T'", name, context); |
b2b7d40a JM |
5754 | return error_mark_node; |
5755 | } | |
1107c4b3 | 5756 | |
b4f70b3d | 5757 | t = lookup_field (context, name, 0, 1); |
7d4bdeed | 5758 | if (t) |
4f2b0fb2 NS |
5759 | { |
5760 | if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl)) | |
5761 | t = TREE_TYPE (t); | |
ba3307c0 NS |
5762 | if (IMPLICIT_TYPENAME_P (t)) |
5763 | { | |
5764 | /* Lookup found an implicit typename that we had | |
5765 | injected into the current scope. Doing things | |
5766 | properly would have located the exact same type, | |
5767 | so there is no error here. We must remove the | |
5768 | implicitness so that we do not warn about it. */ | |
5769 | t = copy_node (t); | |
5770 | TREE_TYPE (t) = NULL_TREE; | |
5771 | } | |
5772 | ||
4f2b0fb2 NS |
5773 | return t; |
5774 | } | |
5566b478 | 5775 | } |
5566b478 | 5776 | } |
11249cf0 MM |
5777 | |
5778 | /* If the CONTEXT is not a template type, then either the field is | |
5779 | there now or its never going to be. */ | |
b4f70b3d | 5780 | if (!uses_template_parms (context)) |
11249cf0 | 5781 | { |
4f2b0fb2 | 5782 | if (complain & tf_error) |
33bd39a2 | 5783 | error ("no type named `%#T' in `%#T'", name, context); |
11249cf0 MM |
5784 | return error_mark_node; |
5785 | } | |
68642fb6 UD |
5786 | |
5787 | ||
45869a6c | 5788 | return build_typename_type (context, name, fullname, NULL_TREE); |
5566b478 MS |
5789 | } |
5790 | ||
b8c6534b KL |
5791 | /* Resolve `CONTEXT::template NAME'. Returns an appropriate type, |
5792 | unless an error occurs, in which case error_mark_node is returned. | |
4f2b0fb2 NS |
5793 | If we locate a TYPE_DECL, we return that, rather than the _TYPE it |
5794 | corresponds to. If COMPLAIN zero, don't complain about any errors | |
5795 | that occur. */ | |
b8c6534b KL |
5796 | |
5797 | tree | |
5798 | make_unbound_class_template (context, name, complain) | |
5799 | tree context, name; | |
5800 | int complain; | |
5801 | { | |
5802 | tree t; | |
5803 | tree d; | |
5804 | ||
5805 | if (TYPE_P (name)) | |
5806 | name = TYPE_IDENTIFIER (name); | |
5807 | else if (DECL_P (name)) | |
5808 | name = DECL_NAME (name); | |
5809 | if (TREE_CODE (name) != IDENTIFIER_NODE) | |
a98facb0 | 5810 | abort (); |
b8c6534b KL |
5811 | |
5812 | if (!uses_template_parms (context) | |
5813 | || currently_open_class (context)) | |
5814 | { | |
5815 | tree tmpl = NULL_TREE; | |
5816 | ||
5817 | if (IS_AGGR_TYPE (context)) | |
5818 | tmpl = lookup_field (context, name, 0, 0); | |
5819 | ||
5820 | if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl)) | |
5821 | { | |
5822 | if (complain) | |
33bd39a2 | 5823 | error ("no class template named `%#T' in `%#T'", name, context); |
b8c6534b KL |
5824 | return error_mark_node; |
5825 | } | |
5826 | ||
5827 | if (!enforce_access (context, tmpl)) | |
5828 | return error_mark_node; | |
5829 | ||
5830 | return tmpl; | |
5831 | } | |
5832 | ||
5833 | /* Build the UNBOUND_CLASS_TEMPLATE. */ | |
5834 | t = make_aggr_type (UNBOUND_CLASS_TEMPLATE); | |
5835 | TYPE_CONTEXT (t) = FROB_CONTEXT (context); | |
5836 | TREE_TYPE (t) = NULL_TREE; | |
5837 | ||
5838 | /* Build the corresponding TEMPLATE_DECL. */ | |
5839 | d = build_decl (TEMPLATE_DECL, name, t); | |
5840 | TYPE_NAME (TREE_TYPE (d)) = d; | |
5841 | TYPE_STUB_DECL (TREE_TYPE (d)) = d; | |
5842 | DECL_CONTEXT (d) = FROB_CONTEXT (context); | |
5843 | DECL_ARTIFICIAL (d) = 1; | |
5844 | ||
5845 | return t; | |
5846 | } | |
5847 | ||
2c73f9f5 ML |
5848 | /* Select the right _DECL from multiple choices. */ |
5849 | ||
5850 | static tree | |
52c11ef6 | 5851 | select_decl (binding, flags) |
2c73f9f5 | 5852 | tree binding; |
52c11ef6 | 5853 | int flags; |
2c73f9f5 ML |
5854 | { |
5855 | tree val; | |
5856 | val = BINDING_VALUE (binding); | |
97ba1e3c | 5857 | |
52c11ef6 | 5858 | if (LOOKUP_NAMESPACES_ONLY (flags)) |
3e3f722c ML |
5859 | { |
5860 | /* We are not interested in types. */ | |
5861 | if (val && TREE_CODE (val) == NAMESPACE_DECL) | |
5862 | return val; | |
5863 | return NULL_TREE; | |
5864 | } | |
68642fb6 | 5865 | |
2c73f9f5 ML |
5866 | /* If we could have a type and |
5867 | we have nothing or we need a type and have none. */ | |
5868 | if (BINDING_TYPE (binding) | |
52c11ef6 JM |
5869 | && (!val || ((flags & LOOKUP_PREFER_TYPES) |
5870 | && TREE_CODE (val) != TYPE_DECL))) | |
2c73f9f5 ML |
5871 | val = TYPE_STUB_DECL (BINDING_TYPE (binding)); |
5872 | /* Don't return non-types if we really prefer types. */ | |
52c11ef6 | 5873 | else if (val && LOOKUP_TYPES_ONLY (flags) && TREE_CODE (val) != TYPE_DECL |
c592d5d2 ML |
5874 | && (TREE_CODE (val) != TEMPLATE_DECL |
5875 | || !DECL_CLASS_TEMPLATE_P (val))) | |
2c73f9f5 | 5876 | val = NULL_TREE; |
1c35f5b6 | 5877 | |
2c73f9f5 ML |
5878 | return val; |
5879 | } | |
5880 | ||
2c169bab JM |
5881 | /* Unscoped lookup of a global: iterate over current namespaces, |
5882 | considering using-directives. If SPACESP is non-NULL, store a list | |
5883 | of the namespaces we've considered in it. */ | |
ea9635c7 | 5884 | |
2c169bab JM |
5885 | tree |
5886 | unqualified_namespace_lookup (name, flags, spacesp) | |
ea9635c7 | 5887 | tree name; |
52c11ef6 | 5888 | int flags; |
2c169bab | 5889 | tree *spacesp; |
ea9635c7 | 5890 | { |
87e3dbc9 | 5891 | tree b = make_node (CPLUS_BINDING); |
ee8fc32b | 5892 | tree initial = current_decl_namespace (); |
ea9635c7 ML |
5893 | tree scope = initial; |
5894 | tree siter; | |
5895 | struct binding_level *level; | |
5896 | tree val = NULL_TREE; | |
5897 | ||
2c169bab JM |
5898 | if (spacesp) |
5899 | *spacesp = NULL_TREE; | |
5900 | ||
5901 | for (; !val; scope = CP_DECL_CONTEXT (scope)) | |
ea9635c7 | 5902 | { |
2c169bab | 5903 | if (spacesp) |
e1b3e07d | 5904 | *spacesp = tree_cons (scope, NULL_TREE, *spacesp); |
ea9635c7 ML |
5905 | val = binding_for_name (name, scope); |
5906 | ||
d52e4867 RS |
5907 | /* Ignore anticipated built-in functions. */ |
5908 | if (val && BINDING_VALUE (val) | |
5909 | && DECL_P (BINDING_VALUE (val)) | |
5910 | && DECL_LANG_SPECIFIC (BINDING_VALUE (val)) | |
5911 | && DECL_ANTICIPATED (BINDING_VALUE (val))) | |
5912 | { | |
5913 | BINDING_VALUE (b) = NULL_TREE; | |
5914 | BINDING_TYPE (b) = NULL_TREE; | |
5915 | } | |
5916 | else | |
5917 | { | |
5918 | /* Initialize binding for this context. */ | |
5919 | BINDING_VALUE (b) = BINDING_VALUE (val); | |
5920 | BINDING_TYPE (b) = BINDING_TYPE (val); | |
5921 | } | |
ea9635c7 ML |
5922 | |
5923 | /* Add all _DECLs seen through local using-directives. */ | |
68642fb6 | 5924 | for (level = current_binding_level; |
ea9635c7 ML |
5925 | !level->namespace_p; |
5926 | level = level->level_chain) | |
52c11ef6 | 5927 | if (!lookup_using_namespace (name, b, level->using_directives, |
2c169bab | 5928 | scope, flags, spacesp)) |
ea9635c7 | 5929 | /* Give up because of error. */ |
5b163de4 | 5930 | return error_mark_node; |
ea9635c7 ML |
5931 | |
5932 | /* Add all _DECLs seen through global using-directives. */ | |
5933 | /* XXX local and global using lists should work equally. */ | |
5934 | siter = initial; | |
5935 | while (1) | |
5936 | { | |
68642fb6 | 5937 | if (!lookup_using_namespace (name, b, DECL_NAMESPACE_USING (siter), |
2c169bab | 5938 | scope, flags, spacesp)) |
ea9635c7 | 5939 | /* Give up because of error. */ |
5b163de4 | 5940 | return error_mark_node; |
ea9635c7 ML |
5941 | if (siter == scope) break; |
5942 | siter = CP_DECL_CONTEXT (siter); | |
5943 | } | |
5944 | ||
52c11ef6 | 5945 | val = select_decl (b, flags); |
ea9635c7 ML |
5946 | if (scope == global_namespace) |
5947 | break; | |
ea9635c7 ML |
5948 | } |
5949 | return val; | |
5950 | } | |
5951 | ||
52c11ef6 JM |
5952 | /* Combine prefer_type and namespaces_only into flags. */ |
5953 | ||
5954 | static int | |
5955 | lookup_flags (prefer_type, namespaces_only) | |
5956 | int prefer_type, namespaces_only; | |
5957 | { | |
5958 | if (namespaces_only) | |
5959 | return LOOKUP_PREFER_NAMESPACES; | |
5960 | if (prefer_type > 1) | |
5961 | return LOOKUP_PREFER_TYPES; | |
5962 | if (prefer_type > 0) | |
5963 | return LOOKUP_PREFER_BOTH; | |
5964 | return 0; | |
5965 | } | |
5966 | ||
5967 | /* Given a lookup that returned VAL, use FLAGS to decide if we want to | |
5968 | ignore it or not. Subroutine of lookup_name_real. */ | |
5969 | ||
5970 | static tree | |
5971 | qualify_lookup (val, flags) | |
5972 | tree val; | |
5973 | int flags; | |
5974 | { | |
5975 | if (val == NULL_TREE) | |
5976 | return val; | |
6b945830 JM |
5977 | if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL) |
5978 | return val; | |
5979 | if ((flags & LOOKUP_PREFER_TYPES) | |
5980 | && (TREE_CODE (val) == TYPE_DECL | |
5981 | || ((flags & LOOKUP_TEMPLATES_EXPECTED) | |
5982 | && DECL_CLASS_TEMPLATE_P (val)))) | |
5983 | return val; | |
5984 | if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES)) | |
52c11ef6 JM |
5985 | return NULL_TREE; |
5986 | return val; | |
5987 | } | |
5988 | ||
235f734d MM |
5989 | /* Any other BINDING overrides an implicit TYPENAME. Warn about |
5990 | that. */ | |
5991 | ||
5992 | static void | |
5993 | warn_about_implicit_typename_lookup (typename, binding) | |
5994 | tree typename; | |
5995 | tree binding; | |
5996 | { | |
5997 | tree subtype = TREE_TYPE (TREE_TYPE (typename)); | |
5998 | tree name = DECL_NAME (typename); | |
5999 | ||
6000 | if (! (TREE_CODE (binding) == TEMPLATE_DECL | |
6001 | && CLASSTYPE_TEMPLATE_INFO (subtype) | |
6002 | && CLASSTYPE_TI_TEMPLATE (subtype) == binding) | |
6003 | && ! (TREE_CODE (binding) == TYPE_DECL | |
6004 | && same_type_p (TREE_TYPE (binding), subtype))) | |
6005 | { | |
33bd39a2 | 6006 | warning ("lookup of `%D' finds `%#D'", |
235f734d | 6007 | name, binding); |
33bd39a2 | 6008 | warning (" instead of `%D' from dependent base class", |
235f734d | 6009 | typename); |
33bd39a2 | 6010 | warning (" (use `typename %T::%D' if that's what you meant)", |
235f734d MM |
6011 | constructor_name (current_class_type), name); |
6012 | } | |
6013 | } | |
6014 | ||
8d08fdba MS |
6015 | /* Look up NAME in the current binding level and its superiors in the |
6016 | namespace of variables, functions and typedefs. Return a ..._DECL | |
6017 | node of some kind representing its definition if there is only one | |
6018 | such declaration, or return a TREE_LIST with all the overloaded | |
6019 | definitions if there are many, or return 0 if it is undefined. | |
6020 | ||
2c73f9f5 ML |
6021 | If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces. |
6022 | If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces). | |
a28e3c7f | 6023 | If PREFER_TYPE is -2, we're being called from yylex(). (UGLY) |
68642fb6 | 6024 | Otherwise we prefer non-TYPE_DECLs. |
f84b4be9 JM |
6025 | |
6026 | If NONCLASS is non-zero, we don't look for the NAME in class scope, | |
6027 | using IDENTIFIER_CLASS_VALUE. */ | |
8d08fdba | 6028 | |
824b9a4c | 6029 | static tree |
3e3f722c | 6030 | lookup_name_real (name, prefer_type, nonclass, namespaces_only) |
8d08fdba | 6031 | tree name; |
3e3f722c | 6032 | int prefer_type, nonclass, namespaces_only; |
8d08fdba | 6033 | { |
235f734d MM |
6034 | tree t; |
6035 | tree val = NULL_TREE; | |
a28e3c7f | 6036 | int yylex = 0; |
e1cd6e56 | 6037 | tree from_obj = NULL_TREE; |
52c11ef6 | 6038 | int flags; |
235f734d | 6039 | int val_is_implicit_typename = 0; |
8d08fdba | 6040 | |
3e3f722c ML |
6041 | /* Hack: copy flag set by parser, if set. */ |
6042 | if (only_namespace_names) | |
6043 | namespaces_only = 1; | |
52c11ef6 | 6044 | |
a28e3c7f MS |
6045 | if (prefer_type == -2) |
6046 | { | |
6047 | extern int looking_for_typename; | |
fc378698 | 6048 | tree type = NULL_TREE; |
a28e3c7f MS |
6049 | |
6050 | yylex = 1; | |
6051 | prefer_type = looking_for_typename; | |
e1cd6e56 | 6052 | |
52c11ef6 | 6053 | flags = lookup_flags (prefer_type, namespaces_only); |
52c11ef6 JM |
6054 | /* If the next thing is '<', class templates are types. */ |
6055 | if (looking_for_template) | |
6056 | flags |= LOOKUP_TEMPLATES_EXPECTED; | |
6057 | ||
e1cd6e56 MS |
6058 | if (got_scope) |
6059 | type = got_scope; | |
dff6b454 | 6060 | else if (got_object != error_mark_node) |
e1cd6e56 | 6061 | type = got_object; |
68642fb6 | 6062 | |
e1cd6e56 | 6063 | if (type) |
a28e3c7f | 6064 | { |
e1cd6e56 | 6065 | if (type == error_mark_node) |
f376e137 | 6066 | return error_mark_node; |
a80e4195 MS |
6067 | if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type)) |
6068 | type = TREE_TYPE (type); | |
5566b478 | 6069 | |
2b9dc906 JM |
6070 | if (TYPE_P (type)) |
6071 | type = complete_type (type); | |
5566b478 | 6072 | |
a1774733 | 6073 | if (TREE_CODE (type) == VOID_TYPE) |
2c73f9f5 ML |
6074 | type = global_namespace; |
6075 | if (TREE_CODE (type) == NAMESPACE_DECL) | |
a9aedbc2 | 6076 | { |
87e3dbc9 | 6077 | val = make_node (CPLUS_BINDING); |
6ad07332 | 6078 | flags |= LOOKUP_COMPLAIN; |
52c11ef6 | 6079 | if (!qualified_lookup_using_namespace (name, type, val, flags)) |
2c73f9f5 | 6080 | return NULL_TREE; |
52c11ef6 | 6081 | val = select_decl (val, flags); |
a9aedbc2 | 6082 | } |
5566b478 | 6083 | else if (! IS_AGGR_TYPE (type) |
5156628f | 6084 | || TREE_CODE (type) == TEMPLATE_TYPE_PARM |
a1281f45 | 6085 | || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM |
5156628f | 6086 | || TREE_CODE (type) == TYPENAME_TYPE) |
e92cc029 | 6087 | /* Someone else will give an error about this if needed. */ |
a28e3c7f | 6088 | val = NULL_TREE; |
e1cd6e56 | 6089 | else if (type == current_class_type) |
a28e3c7f | 6090 | val = IDENTIFIER_CLASS_VALUE (name); |
a28e3c7f | 6091 | else |
70adf8a9 JM |
6092 | { |
6093 | val = lookup_member (type, name, 0, prefer_type); | |
6094 | type_access_control (type, val); | |
e69c8072 JM |
6095 | |
6096 | /* Restore the containing TYPENAME_TYPE if we looked | |
6097 | through it before. */ | |
6098 | if (got_scope && got_scope != type | |
6099 | && val && TREE_CODE (val) == TYPE_DECL | |
6100 | && TREE_CODE (TREE_TYPE (val)) == TYPENAME_TYPE) | |
6101 | TYPE_CONTEXT (TREE_TYPE (val)) = got_scope; | |
70adf8a9 | 6102 | } |
a28e3c7f | 6103 | } |
e1cd6e56 MS |
6104 | else |
6105 | val = NULL_TREE; | |
6106 | ||
594740f3 | 6107 | if (got_scope) |
e1cd6e56 | 6108 | goto done; |
594740f3 | 6109 | else if (got_object && val) |
aca77bd1 JM |
6110 | { |
6111 | from_obj = val; | |
6112 | val = NULL_TREE; | |
6113 | } | |
a28e3c7f | 6114 | } |
52c11ef6 | 6115 | else |
5b163de4 JM |
6116 | { |
6117 | flags = lookup_flags (prefer_type, namespaces_only); | |
6118 | /* If we're not parsing, we need to complain. */ | |
6119 | flags |= LOOKUP_COMPLAIN; | |
6120 | } | |
e76a2646 | 6121 | |
d8f8dca1 | 6122 | /* First, look in non-namespace scopes. */ |
6f1b4c42 JM |
6123 | |
6124 | if (current_class_type == NULL_TREE) | |
6125 | nonclass = 1; | |
6126 | ||
235f734d | 6127 | for (t = IDENTIFIER_BINDING (name); t; t = TREE_CHAIN (t)) |
d8f8dca1 | 6128 | { |
235f734d MM |
6129 | tree binding; |
6130 | ||
6131 | if (!LOCAL_BINDING_P (t) && nonclass) | |
d8f8dca1 MM |
6132 | /* We're not looking for class-scoped bindings, so keep going. */ |
6133 | continue; | |
68642fb6 | 6134 | |
d8f8dca1 | 6135 | /* If this is the kind of thing we're looking for, we're done. */ |
235f734d MM |
6136 | if (qualify_lookup (BINDING_VALUE (t), flags)) |
6137 | binding = BINDING_VALUE (t); | |
68642fb6 | 6138 | else if ((flags & LOOKUP_PREFER_TYPES) |
235f734d MM |
6139 | && qualify_lookup (BINDING_TYPE (t), flags)) |
6140 | binding = BINDING_TYPE (t); | |
6141 | else | |
6142 | binding = NULL_TREE; | |
6143 | ||
70adf8a9 JM |
6144 | /* Handle access control on types from enclosing or base classes. */ |
6145 | if (binding && ! yylex | |
6146 | && BINDING_LEVEL (t) && BINDING_LEVEL (t)->parm_flag == 2) | |
6147 | type_access_control (BINDING_LEVEL (t)->this_class, binding); | |
6148 | ||
235f734d | 6149 | if (binding |
83233dca | 6150 | && (!val || !IMPLICIT_TYPENAME_TYPE_DECL_P (binding))) |
d8f8dca1 | 6151 | { |
235f734d MM |
6152 | if (val_is_implicit_typename && !yylex) |
6153 | warn_about_implicit_typename_lookup (val, binding); | |
6154 | val = binding; | |
68642fb6 | 6155 | val_is_implicit_typename |
83233dca | 6156 | = IMPLICIT_TYPENAME_TYPE_DECL_P (val); |
235f734d MM |
6157 | if (!val_is_implicit_typename) |
6158 | break; | |
d8f8dca1 MM |
6159 | } |
6160 | } | |
f181d4ae | 6161 | |
235f734d MM |
6162 | /* Now lookup in namespace scopes. */ |
6163 | if (!val || val_is_implicit_typename) | |
e76a2646 | 6164 | { |
2c169bab | 6165 | t = unqualified_namespace_lookup (name, flags, 0); |
235f734d | 6166 | if (t) |
c1def683 | 6167 | { |
235f734d MM |
6168 | if (val_is_implicit_typename && !yylex) |
6169 | warn_about_implicit_typename_lookup (val, t); | |
6170 | val = t; | |
c1def683 JM |
6171 | } |
6172 | } | |
6173 | ||
a28e3c7f | 6174 | done: |
8d08fdba MS |
6175 | if (val) |
6176 | { | |
c91a56d2 | 6177 | /* This should only warn about types used in qualified-ids. */ |
e1cd6e56 | 6178 | if (from_obj && from_obj != val) |
5566b478 | 6179 | { |
c91a56d2 MS |
6180 | if (looking_for_typename && TREE_CODE (from_obj) == TYPE_DECL |
6181 | && TREE_CODE (val) == TYPE_DECL | |
aca77bd1 | 6182 | && ! same_type_p (TREE_TYPE (from_obj), TREE_TYPE (val))) |
33bd39a2 | 6183 | pedwarn ("\ |
bd0d5d4a JM |
6184 | lookup of `%D' in the scope of `%#T' (`%#D') \ |
6185 | does not match lookup in the current scope (`%#D')", | |
6186 | name, got_object, from_obj, val); | |
594740f3 | 6187 | |
b8b1a3c1 JM |
6188 | /* We don't change val to from_obj if got_object depends on |
6189 | template parms because that breaks implicit typename for | |
6190 | destructor calls. */ | |
6191 | if (! uses_template_parms (got_object)) | |
53fdbb3b | 6192 | val = from_obj; |
5566b478 | 6193 | } |
e1cd6e56 | 6194 | |
0c64a9ca JM |
6195 | /* If we have a single function from a using decl, pull it out. */ |
6196 | if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val)) | |
6197 | val = OVL_FUNCTION (val); | |
8d08fdba | 6198 | } |
e1cd6e56 MS |
6199 | else if (from_obj) |
6200 | val = from_obj; | |
8d08fdba MS |
6201 | |
6202 | return val; | |
6203 | } | |
6204 | ||
700f8a87 MS |
6205 | tree |
6206 | lookup_name_nonclass (name) | |
6207 | tree name; | |
6208 | { | |
3e3f722c | 6209 | return lookup_name_real (name, 0, 1, 0); |
700f8a87 MS |
6210 | } |
6211 | ||
2c73f9f5 ML |
6212 | tree |
6213 | lookup_function_nonclass (name, args) | |
6214 | tree name; | |
6215 | tree args; | |
6216 | { | |
6217 | return lookup_arg_dependent (name, lookup_name_nonclass (name), args); | |
6218 | } | |
6219 | ||
3e3f722c ML |
6220 | tree |
6221 | lookup_name_namespace_only (name) | |
6222 | tree name; | |
6223 | { | |
6224 | /* type-or-namespace, nonclass, namespace_only */ | |
6225 | return lookup_name_real (name, 1, 1, 1); | |
6226 | } | |
6227 | ||
700f8a87 MS |
6228 | tree |
6229 | lookup_name (name, prefer_type) | |
6230 | tree name; | |
6231 | int prefer_type; | |
6232 | { | |
3e3f722c | 6233 | return lookup_name_real (name, prefer_type, 0, 0); |
700f8a87 MS |
6234 | } |
6235 | ||
a7d2d407 MM |
6236 | /* Similar to `lookup_name' but look only in the innermost non-class |
6237 | binding level. */ | |
8d08fdba MS |
6238 | |
6239 | tree | |
6240 | lookup_name_current_level (name) | |
6241 | tree name; | |
6242 | { | |
a7d2d407 MM |
6243 | struct binding_level *b; |
6244 | tree t = NULL_TREE; | |
8d08fdba | 6245 | |
a7d2d407 MM |
6246 | b = current_binding_level; |
6247 | while (b->parm_flag == 2) | |
6248 | b = b->level_chain; | |
6249 | ||
6250 | if (b->namespace_p) | |
8d08fdba | 6251 | { |
5fdaba89 | 6252 | t = IDENTIFIER_NAMESPACE_VALUE (name); |
8d08fdba MS |
6253 | |
6254 | /* extern "C" function() */ | |
6255 | if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST) | |
6256 | t = TREE_VALUE (t); | |
6257 | } | |
68642fb6 | 6258 | else if (IDENTIFIER_BINDING (name) |
f181d4ae | 6259 | && LOCAL_BINDING_P (IDENTIFIER_BINDING (name))) |
8d08fdba | 6260 | { |
a4443a08 MS |
6261 | while (1) |
6262 | { | |
f181d4ae MM |
6263 | if (BINDING_LEVEL (IDENTIFIER_BINDING (name)) == b) |
6264 | return IDENTIFIER_VALUE (name); | |
68642fb6 | 6265 | |
9ed182dc JM |
6266 | if (b->keep == 2) |
6267 | b = b->level_chain; | |
6268 | else | |
6269 | break; | |
6270 | } | |
6271 | } | |
6272 | ||
6273 | return t; | |
6274 | } | |
6275 | ||
6276 | /* Like lookup_name_current_level, but for types. */ | |
6277 | ||
6278 | tree | |
6279 | lookup_type_current_level (name) | |
6280 | tree name; | |
6281 | { | |
6282 | register tree t = NULL_TREE; | |
6283 | ||
6284 | my_friendly_assert (! current_binding_level->namespace_p, 980716); | |
6285 | ||
6286 | if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE | |
6287 | && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node) | |
6288 | { | |
6289 | struct binding_level *b = current_binding_level; | |
6290 | while (1) | |
6291 | { | |
6292 | if (purpose_member (name, b->type_shadowed)) | |
6293 | return REAL_IDENTIFIER_TYPE_VALUE (name); | |
a4443a08 MS |
6294 | if (b->keep == 2) |
6295 | b = b->level_chain; | |
6296 | else | |
6297 | break; | |
6298 | } | |
8d08fdba MS |
6299 | } |
6300 | ||
6301 | return t; | |
6302 | } | |
3e3f722c ML |
6303 | |
6304 | void | |
6305 | begin_only_namespace_names () | |
6306 | { | |
6307 | only_namespace_names = 1; | |
6308 | } | |
6309 | ||
6310 | void | |
6311 | end_only_namespace_names () | |
6312 | { | |
6313 | only_namespace_names = 0; | |
6314 | } | |
8d08fdba | 6315 | \f |
8d08fdba | 6316 | /* Push the declarations of builtin types into the namespace. |
0e5921e8 | 6317 | RID_INDEX is the index of the builtin type |
8d08fdba MS |
6318 | in the array RID_POINTERS. NAME is the name used when looking |
6319 | up the builtin type. TYPE is the _TYPE node for the builtin type. */ | |
6320 | ||
eaa7c03f | 6321 | void |
8d08fdba MS |
6322 | record_builtin_type (rid_index, name, type) |
6323 | enum rid rid_index; | |
d8e178a0 | 6324 | const char *name; |
8d08fdba MS |
6325 | tree type; |
6326 | { | |
6327 | tree rname = NULL_TREE, tname = NULL_TREE; | |
a703fb38 | 6328 | tree tdecl = NULL_TREE; |
8d08fdba | 6329 | |
0e5921e8 | 6330 | if ((int) rid_index < (int) RID_MAX) |
8d08fdba MS |
6331 | rname = ridpointers[(int) rid_index]; |
6332 | if (name) | |
6333 | tname = get_identifier (name); | |
6334 | ||
6335 | TYPE_BUILT_IN (type) = 1; | |
68642fb6 | 6336 | |
8d08fdba MS |
6337 | if (tname) |
6338 | { | |
8d08fdba | 6339 | tdecl = pushdecl (build_decl (TYPE_DECL, tname, type)); |
8d08fdba | 6340 | set_identifier_type_value (tname, NULL_TREE); |
0e5921e8 | 6341 | if ((int) rid_index < (int) RID_MAX) |
2c73f9f5 ML |
6342 | /* Built-in types live in the global namespace. */ |
6343 | SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl); | |
8d08fdba MS |
6344 | } |
6345 | if (rname != NULL_TREE) | |
6346 | { | |
6347 | if (tname != NULL_TREE) | |
6348 | { | |
6349 | set_identifier_type_value (rname, NULL_TREE); | |
2c73f9f5 | 6350 | SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl); |
8d08fdba MS |
6351 | } |
6352 | else | |
6353 | { | |
8d08fdba | 6354 | tdecl = pushdecl (build_decl (TYPE_DECL, rname, type)); |
8d08fdba MS |
6355 | set_identifier_type_value (rname, NULL_TREE); |
6356 | } | |
6357 | } | |
8d08fdba MS |
6358 | } |
6359 | ||
eff71ab0 | 6360 | /* Record one of the standard Java types. |
4d8a1dd6 MM |
6361 | * Declare it as having the given NAME. |
6362 | * If SIZE > 0, it is the size of one of the integral types; | |
6363 | * otherwise it is the negative of the size of one of the other types. */ | |
eff71ab0 PB |
6364 | |
6365 | static tree | |
6366 | record_builtin_java_type (name, size) | |
d8e178a0 | 6367 | const char *name; |
eff71ab0 PB |
6368 | int size; |
6369 | { | |
6370 | tree type, decl; | |
6371 | if (size > 0) | |
6372 | type = make_signed_type (size); | |
6373 | else if (size > -32) | |
6374 | { /* "__java_char" or ""__java_boolean". */ | |
6375 | type = make_unsigned_type (-size); | |
6376 | /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/ | |
6377 | } | |
6378 | else | |
6379 | { /* "__java_float" or ""__java_double". */ | |
6380 | type = make_node (REAL_TYPE); | |
6381 | TYPE_PRECISION (type) = - size; | |
6382 | layout_type (type); | |
6383 | } | |
0e5921e8 | 6384 | record_builtin_type (RID_MAX, name, type); |
eff71ab0 | 6385 | decl = TYPE_NAME (type); |
e229f2cd PB |
6386 | |
6387 | /* Suppress generate debug symbol entries for these types, | |
6388 | since for normal C++ they are just clutter. | |
6389 | However, push_lang_context undoes this if extern "Java" is seen. */ | |
eff71ab0 | 6390 | DECL_IGNORED_P (decl) = 1; |
e229f2cd | 6391 | |
eff71ab0 PB |
6392 | TYPE_FOR_JAVA (type) = 1; |
6393 | return type; | |
6394 | } | |
6395 | ||
036407f7 ML |
6396 | /* Push a type into the namespace so that the back-ends ignore it. */ |
6397 | ||
6398 | static void | |
6399 | record_unknown_type (type, name) | |
6400 | tree type; | |
d8e178a0 | 6401 | const char *name; |
036407f7 ML |
6402 | { |
6403 | tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type)); | |
6404 | /* Make sure the "unknown type" typedecl gets ignored for debug info. */ | |
6405 | DECL_IGNORED_P (decl) = 1; | |
6406 | TYPE_DECL_SUPPRESS_DEBUG (decl) = 1; | |
6407 | TYPE_SIZE (type) = TYPE_SIZE (void_type_node); | |
6408 | TYPE_ALIGN (type) = 1; | |
11cf4d18 | 6409 | TYPE_USER_ALIGN (type) = 0; |
036407f7 | 6410 | TYPE_MODE (type) = TYPE_MODE (void_type_node); |
68642fb6 | 6411 | } |
036407f7 | 6412 | |
d43829f9 MM |
6413 | /* An string for which we should create an IDENTIFIER_NODE at |
6414 | startup. */ | |
6415 | ||
6416 | typedef struct predefined_identifier | |
6417 | { | |
6418 | /* The name of the identifier. */ | |
8b60264b | 6419 | const char *const name; |
d43829f9 | 6420 | /* The place where the IDENTIFIER_NODE should be stored. */ |
8b60264b | 6421 | tree *const node; |
298d6f60 | 6422 | /* Non-zero if this is the name of a constructor or destructor. */ |
8b60264b | 6423 | const int ctor_or_dtor_p; |
d43829f9 MM |
6424 | } predefined_identifier; |
6425 | ||
6426 | /* Create all the predefined identifiers. */ | |
6427 | ||
6428 | static void | |
5362b086 | 6429 | initialize_predefined_identifiers () |
d43829f9 | 6430 | { |
8b60264b | 6431 | const predefined_identifier *pid; |
d43829f9 MM |
6432 | |
6433 | /* A table of identifiers to create at startup. */ | |
8b60264b | 6434 | static const predefined_identifier predefined_identifiers[] = { |
298d6f60 MM |
6435 | { "C++", &lang_name_cplusplus, 0 }, |
6436 | { "C", &lang_name_c, 0 }, | |
6437 | { "Java", &lang_name_java, 0 }, | |
6438 | { CTOR_NAME, &ctor_identifier, 1 }, | |
6439 | { "__base_ctor", &base_ctor_identifier, 1 }, | |
6440 | { "__comp_ctor", &complete_ctor_identifier, 1 }, | |
6441 | { DTOR_NAME, &dtor_identifier, 1 }, | |
6442 | { "__comp_dtor", &complete_dtor_identifier, 1 }, | |
6443 | { "__base_dtor", &base_dtor_identifier, 1 }, | |
6444 | { "__deleting_dtor", &deleting_dtor_identifier, 1 }, | |
298d6f60 | 6445 | { IN_CHARGE_NAME, &in_charge_identifier, 0 }, |
298d6f60 MM |
6446 | { "nelts", &nelts_identifier, 0 }, |
6447 | { THIS_NAME, &this_identifier, 0 }, | |
c4372ef4 | 6448 | { VTABLE_DELTA_NAME, &delta_identifier, 0 }, |
298d6f60 | 6449 | { VTABLE_PFN_NAME, &pfn_identifier, 0 }, |
298d6f60 | 6450 | { "_vptr", &vptr_identifier, 0 }, |
3ec6bad3 | 6451 | { "__vtt_parm", &vtt_parm_identifier, 0 }, |
1f6e1acc | 6452 | { "std", &std_identifier, 0 }, |
298d6f60 | 6453 | { NULL, NULL, 0 } |
d43829f9 MM |
6454 | }; |
6455 | ||
6456 | for (pid = predefined_identifiers; pid->name; ++pid) | |
298d6f60 MM |
6457 | { |
6458 | *pid->node = get_identifier (pid->name); | |
6459 | if (pid->ctor_or_dtor_p) | |
6460 | IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1; | |
6461 | } | |
d43829f9 MM |
6462 | } |
6463 | ||
8d08fdba MS |
6464 | /* Create the predefined scalar types of C, |
6465 | and some nodes representing standard constants (0, 1, (void *)0). | |
6466 | Initialize the global binding level. | |
6467 | Make definitions for built-in primitive functions. */ | |
6468 | ||
6469 | void | |
f5e99456 | 6470 | cxx_init_decl_processing () |
8d08fdba | 6471 | { |
10841285 MM |
6472 | tree void_ftype; |
6473 | tree void_ftype_ptr; | |
8d08fdba | 6474 | |
d43829f9 MM |
6475 | /* Create all the identifiers we need. */ |
6476 | initialize_predefined_identifiers (); | |
8d08fdba | 6477 | |
8f17b5c5 | 6478 | /* Fill in back-end hooks. */ |
d363e7bf | 6479 | lang_missing_noreturn_ok_p = &cp_missing_noreturn_ok_p; |
99dccabc | 6480 | |
fc6af6e3 RH |
6481 | cp_parse_init (); |
6482 | init_decl2 (); | |
9cd64686 | 6483 | init_pt (); |
fc6af6e3 | 6484 | |
9cd64686 MM |
6485 | /* Create the global variables. */ |
6486 | push_to_top_level (); | |
8012c983 | 6487 | |
2c73f9f5 | 6488 | /* Enter the global namespace. */ |
30394414 | 6489 | my_friendly_assert (global_namespace == NULL_TREE, 375); |
30394414 JM |
6490 | push_namespace (get_identifier ("::")); |
6491 | global_namespace = current_namespace; | |
6492 | current_lang_name = NULL_TREE; | |
6493 | ||
bccd95ae | 6494 | /* Adjust various flags based on command-line settings. */ |
2642b9bf JM |
6495 | if (! flag_permissive && ! pedantic) |
6496 | flag_pedantic_errors = 1; | |
bccd95ae | 6497 | if (!flag_no_inline) |
b850de4f MM |
6498 | { |
6499 | flag_inline_trees = 1; | |
6500 | flag_no_inline = 1; | |
6501 | } | |
acc72c37 MM |
6502 | if (flag_inline_functions) |
6503 | { | |
6504 | flag_inline_trees = 2; | |
6505 | flag_inline_functions = 0; | |
6506 | } | |
830fcda8 | 6507 | |
8d08fdba MS |
6508 | /* Initially, C. */ |
6509 | current_lang_name = lang_name_c; | |
6510 | ||
6511 | current_function_decl = NULL_TREE; | |
8d08fdba MS |
6512 | current_binding_level = NULL_BINDING_LEVEL; |
6513 | free_binding_level = NULL_BINDING_LEVEL; | |
6514 | ||
81b3411c BS |
6515 | build_common_tree_nodes (flag_signed_char); |
6516 | ||
8d08fdba MS |
6517 | error_mark_list = build_tree_list (error_mark_node, error_mark_node); |
6518 | TREE_TYPE (error_mark_list) = error_mark_node; | |
6519 | ||
a28e3c7f MS |
6520 | /* Make the binding_level structure for global names. */ |
6521 | pushlevel (0); | |
8d08fdba | 6522 | global_binding_level = current_binding_level; |
2c73f9f5 ML |
6523 | /* The global level is the namespace level of ::. */ |
6524 | NAMESPACE_LEVEL (global_namespace) = global_binding_level; | |
6525 | declare_namespace_level (); | |
8d08fdba | 6526 | |
6bcedb4e | 6527 | /* Create the `std' namespace. */ |
1dbb6023 NS |
6528 | push_namespace (std_identifier); |
6529 | std_node = current_namespace; | |
6530 | pop_namespace (); | |
5362b086 | 6531 | |
eaa7c03f | 6532 | c_common_nodes_and_builtins (); |
37c46b43 | 6533 | |
4d8a1dd6 MM |
6534 | java_byte_type_node = record_builtin_java_type ("__java_byte", 8); |
6535 | java_short_type_node = record_builtin_java_type ("__java_short", 16); | |
6536 | java_int_type_node = record_builtin_java_type ("__java_int", 32); | |
6537 | java_long_type_node = record_builtin_java_type ("__java_long", 64); | |
6538 | java_float_type_node = record_builtin_java_type ("__java_float", -32); | |
6539 | java_double_type_node = record_builtin_java_type ("__java_double", -64); | |
6540 | java_char_type_node = record_builtin_java_type ("__java_char", -16); | |
6541 | java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1); | |
eff71ab0 | 6542 | |
8d08fdba MS |
6543 | integer_two_node = build_int_2 (2, 0); |
6544 | TREE_TYPE (integer_two_node) = integer_type_node; | |
6545 | integer_three_node = build_int_2 (3, 0); | |
6546 | TREE_TYPE (integer_three_node) = integer_type_node; | |
8d08fdba | 6547 | |
255512c1 JM |
6548 | boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE); |
6549 | TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE); | |
c1ea6a0b BK |
6550 | TYPE_MAX_VALUE (boolean_type_node) = build_int_2 (1, 0); |
6551 | TREE_TYPE (TYPE_MAX_VALUE (boolean_type_node)) = boolean_type_node; | |
6552 | TYPE_PRECISION (boolean_type_node) = 1; | |
255512c1 JM |
6553 | record_builtin_type (RID_BOOL, "bool", boolean_type_node); |
6554 | boolean_false_node = build_int_2 (0, 0); | |
6555 | TREE_TYPE (boolean_false_node) = boolean_type_node; | |
6556 | boolean_true_node = build_int_2 (1, 0); | |
6557 | TREE_TYPE (boolean_true_node) = boolean_type_node; | |
2986ae00 | 6558 | |
ff0bee63 | 6559 | signed_size_zero_node = build_int_2 (0, 0); |
e92cc029 MS |
6560 | TREE_TYPE (signed_size_zero_node) = make_signed_type (TYPE_PRECISION (sizetype)); |
6561 | ||
4cc1d462 | 6562 | empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE); |
5fd8e536 JM |
6563 | |
6564 | #if 0 | |
3e411c3f | 6565 | record_builtin_type (RID_MAX, NULL, string_type_node); |
5fd8e536 JM |
6566 | #endif |
6567 | ||
1f84ec23 MM |
6568 | delta_type_node = ptrdiff_type_node; |
6569 | vtable_index_type = ptrdiff_type_node; | |
c7e266a6 | 6570 | |
3ec6bad3 | 6571 | vtt_parm_type = build_pointer_type (const_ptr_type_node); |
10841285 MM |
6572 | void_ftype = build_function_type (void_type_node, void_list_node); |
6573 | void_ftype_ptr = build_function_type (void_type_node, | |
6574 | tree_cons (NULL_TREE, | |
6575 | ptr_type_node, | |
6576 | void_list_node)); | |
824b9a4c | 6577 | void_ftype_ptr |
4cc1d462 | 6578 | = build_exception_variant (void_ftype_ptr, empty_except_spec); |
8d08fdba | 6579 | |
8d08fdba MS |
6580 | /* C++ extensions */ |
6581 | ||
6582 | unknown_type_node = make_node (UNKNOWN_TYPE); | |
036407f7 ML |
6583 | record_unknown_type (unknown_type_node, "unknown type"); |
6584 | ||
8d08fdba MS |
6585 | /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */ |
6586 | TREE_TYPE (unknown_type_node) = unknown_type_node; | |
a6967cc0 | 6587 | |
a6967cc0 JM |
6588 | /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same |
6589 | result. */ | |
8d08fdba MS |
6590 | TYPE_POINTER_TO (unknown_type_node) = unknown_type_node; |
6591 | TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node; | |
6592 | ||
c4372ef4 NS |
6593 | { |
6594 | /* Make sure we get a unique function type, so we can give | |
6595 | its pointer type a name. (This wins for gdb.) */ | |
6596 | tree vfunc_type = make_node (FUNCTION_TYPE); | |
6597 | TREE_TYPE (vfunc_type) = integer_type_node; | |
6598 | TYPE_ARG_TYPES (vfunc_type) = NULL_TREE; | |
6599 | layout_type (vfunc_type); | |
6600 | ||
6601 | vtable_entry_type = build_pointer_type (vfunc_type); | |
6602 | } | |
0e5921e8 | 6603 | record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type); |
8d08fdba | 6604 | |
8d08fdba | 6605 | vtbl_type_node |
52bf7d5d | 6606 | = build_cplus_array_type (vtable_entry_type, NULL_TREE); |
8d08fdba | 6607 | layout_type (vtbl_type_node); |
91063b51 | 6608 | vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST); |
3e411c3f | 6609 | record_builtin_type (RID_MAX, NULL, vtbl_type_node); |
849da744 MM |
6610 | vtbl_ptr_type_node = build_pointer_type (vtable_entry_type); |
6611 | layout_type (vtbl_ptr_type_node); | |
3e411c3f | 6612 | record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node); |
8d08fdba | 6613 | |
1f84ec23 MM |
6614 | push_namespace (get_identifier ("__cxxabiv1")); |
6615 | abi_node = current_namespace; | |
6616 | pop_namespace (); | |
6633d636 | 6617 | |
2c73f9f5 | 6618 | global_type_node = make_node (LANG_TYPE); |
036407f7 | 6619 | record_unknown_type (global_type_node, "global type"); |
2c73f9f5 | 6620 | |
db5ae43f MS |
6621 | /* Now, C++. */ |
6622 | current_lang_name = lang_name_cplusplus; | |
8d08fdba | 6623 | |
ced78d8b | 6624 | { |
2c73f9f5 | 6625 | tree bad_alloc_type_node, newtype, deltype; |
10841285 MM |
6626 | tree ptr_ftype_sizetype; |
6627 | ||
1dbb6023 | 6628 | push_namespace (std_identifier); |
2c73f9f5 | 6629 | bad_alloc_type_node = xref_tag |
ca107ded | 6630 | (class_type_node, get_identifier ("bad_alloc"), 1); |
1dbb6023 | 6631 | pop_namespace (); |
10841285 MM |
6632 | ptr_ftype_sizetype |
6633 | = build_function_type (ptr_type_node, | |
6634 | tree_cons (NULL_TREE, | |
6635 | c_size_type_node, | |
6636 | void_list_node)); | |
2c73f9f5 | 6637 | newtype = build_exception_variant |
0ba8a114 NS |
6638 | (ptr_ftype_sizetype, add_exception_specifier |
6639 | (NULL_TREE, bad_alloc_type_node, -1)); | |
4cc1d462 | 6640 | deltype = build_exception_variant (void_ftype_ptr, empty_except_spec); |
596ea4e5 AS |
6641 | push_cp_library_fn (NEW_EXPR, newtype); |
6642 | push_cp_library_fn (VEC_NEW_EXPR, newtype); | |
6643 | global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype); | |
6644 | push_cp_library_fn (VEC_DELETE_EXPR, deltype); | |
ced78d8b | 6645 | } |
8d08fdba MS |
6646 | |
6647 | abort_fndecl | |
1f84ec23 | 6648 | = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype); |
8d08fdba | 6649 | |
8d08fdba MS |
6650 | /* Perform other language dependent initializations. */ |
6651 | init_class_processing (); | |
8d08fdba | 6652 | init_search_processing (); |
db48b831 | 6653 | init_rtti_processing (); |
8d08fdba | 6654 | |
6467930b | 6655 | if (flag_exceptions) |
8d2733ca | 6656 | init_exception_processing (); |
9e9ff709 | 6657 | |
7fcdf4c2 | 6658 | if (! supports_one_only ()) |
72b7eeff | 6659 | flag_weak = 0; |
8d08fdba | 6660 | |
2ce07e2d | 6661 | make_fname_decl = cp_make_fname_decl; |
0ba8a114 | 6662 | start_fname_decls (); |
8d08fdba | 6663 | |
e9a25f70 JL |
6664 | /* Show we use EH for cleanups. */ |
6665 | using_eh_for_cleanups (); | |
62c154ed | 6666 | |
d9cf7c82 JM |
6667 | /* Maintain consistency. Perhaps we should just complain if they |
6668 | say -fwritable-strings? */ | |
6669 | if (flag_writable_strings) | |
6670 | flag_const_strings = 0; | |
fc6af6e3 RH |
6671 | |
6672 | /* Add GC roots for all of our global variables. */ | |
ca7558fc KG |
6673 | ggc_add_tree_root (c_global_trees, ARRAY_SIZE (c_global_trees)); |
6674 | ggc_add_tree_root (cp_global_trees, ARRAY_SIZE (cp_global_trees)); | |
fc6af6e3 RH |
6675 | ggc_add_tree_root (&integer_three_node, 1); |
6676 | ggc_add_tree_root (&integer_two_node, 1); | |
fc6af6e3 | 6677 | ggc_add_tree_root (&signed_size_zero_node, 1); |
fc6af6e3 RH |
6678 | ggc_add_tree_root (&size_one_node, 1); |
6679 | ggc_add_tree_root (&size_zero_node, 1); | |
fc6af6e3 RH |
6680 | ggc_add_root (&global_binding_level, 1, sizeof global_binding_level, |
6681 | mark_binding_level); | |
9cd64686 | 6682 | ggc_add_root (&scope_chain, 1, sizeof scope_chain, &mark_saved_scope); |
fc6af6e3 RH |
6683 | ggc_add_tree_root (&static_ctors, 1); |
6684 | ggc_add_tree_root (&static_dtors, 1); | |
9cd64686 | 6685 | ggc_add_tree_root (&lastiddecl, 1); |
fc6af6e3 | 6686 | |
9cd64686 | 6687 | ggc_add_tree_root (&last_function_parms, 1); |
fc6af6e3 | 6688 | ggc_add_tree_root (&error_mark_list, 1); |
9cd64686 | 6689 | |
fc6af6e3 | 6690 | ggc_add_tree_root (&global_namespace, 1); |
fc6af6e3 RH |
6691 | ggc_add_tree_root (&global_type_node, 1); |
6692 | ggc_add_tree_root (&anonymous_namespace_name, 1); | |
9cd64686 MM |
6693 | |
6694 | ggc_add_tree_root (&got_object, 1); | |
6695 | ggc_add_tree_root (&got_scope, 1); | |
6696 | ||
6697 | ggc_add_tree_root (¤t_lang_name, 1); | |
6698 | ggc_add_tree_root (&static_aggregates, 1); | |
4890c2f4 | 6699 | ggc_add_tree_root (&free_bindings, 1); |
0154eaa8 | 6700 | ggc_add_tree_root (&incomplete_vars, 1); |
62c154ed JM |
6701 | } |
6702 | ||
0ba8a114 NS |
6703 | /* Generate an initializer for a function naming variable from |
6704 | NAME. NAME may be NULL, in which case we generate a special | |
6705 | ERROR_MARK node which should be replaced later. */ | |
6706 | ||
6707 | tree | |
6708 | cp_fname_init (name) | |
6709 | const char *name; | |
6710 | { | |
6711 | tree domain = NULL_TREE; | |
6712 | tree type; | |
6713 | tree init = NULL_TREE; | |
6714 | size_t length = 0; | |
6715 | ||
6716 | if (name) | |
6717 | { | |
6718 | length = strlen (name); | |
6719 | domain = build_index_type (size_int (length)); | |
6720 | init = build_string (length + 1, name); | |
6721 | } | |
6722 | ||
6723 | type = build_qualified_type (char_type_node, TYPE_QUAL_CONST); | |
6724 | type = build_cplus_array_type (type, domain); | |
6725 | ||
6726 | if (init) | |
6727 | TREE_TYPE (init) = type; | |
6728 | else | |
6729 | /* We don't know the value until instantiation time. Make | |
6730 | something which will be digested now, but replaced later. */ | |
6731 | init = build (ERROR_MARK, type); | |
6732 | ||
6733 | return init; | |
6734 | } | |
6735 | ||
2ce07e2d NS |
6736 | /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the |
6737 | decl, NAME is the initialization string and TYPE_DEP indicates whether | |
6738 | NAME depended on the type of the function. We make use of that to detect | |
0ba8a114 NS |
6739 | __PRETTY_FUNCTION__ inside a template fn. This is being done |
6740 | lazily at the point of first use, so we musn't push the decl now. */ | |
2ce07e2d NS |
6741 | |
6742 | static tree | |
0ba8a114 | 6743 | cp_make_fname_decl (id, type_dep) |
2ce07e2d | 6744 | tree id; |
2ce07e2d NS |
6745 | int type_dep; |
6746 | { | |
83182544 | 6747 | const char *const name = (type_dep && processing_template_decl |
0ba8a114 NS |
6748 | ? NULL : fname_as_string (type_dep)); |
6749 | tree init = cp_fname_init (name); | |
6750 | tree decl = build_decl (VAR_DECL, id, TREE_TYPE (init)); | |
2ce07e2d | 6751 | |
0ba8a114 NS |
6752 | /* As we don't push the decl here, we must set the context. */ |
6753 | DECL_CONTEXT (decl) = current_function_decl; | |
6754 | DECL_PRETTY_FUNCTION_P (decl) = type_dep; | |
6755 | ||
2ce07e2d NS |
6756 | TREE_STATIC (decl) = 1; |
6757 | TREE_READONLY (decl) = 1; | |
2ce07e2d | 6758 | DECL_ARTIFICIAL (decl) = 1; |
2ce07e2d | 6759 | DECL_INITIAL (decl) = init; |
0ba8a114 NS |
6760 | |
6761 | TREE_USED (decl) = 1; | |
5362b086 | 6762 | |
0ba8a114 NS |
6763 | cp_finish_decl (decl, init, NULL_TREE, LOOKUP_ONLYCONVERTING); |
6764 | ||
2ce07e2d NS |
6765 | return decl; |
6766 | } | |
6767 | ||
d52e4867 RS |
6768 | /* Make a definition for a builtin function named NAME in the current |
6769 | namespace, whose data type is TYPE and whose context is CONTEXT. | |
6770 | TYPE should be a function type with argument types. | |
8d08fdba | 6771 | |
0c11ada6 JM |
6772 | CLASS and CODE tell later passes how to compile calls to this function. |
6773 | See tree.h for possible values. | |
6774 | ||
6775 | If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME, | |
8d08fdba MS |
6776 | the name to be called if we can't opencode the function. */ |
6777 | ||
d52e4867 RS |
6778 | static tree |
6779 | builtin_function_1 (name, type, context, code, class, libname) | |
d8e178a0 | 6780 | const char *name; |
8d08fdba | 6781 | tree type; |
d52e4867 | 6782 | tree context; |
0c11ada6 JM |
6783 | int code; |
6784 | enum built_in_class class; | |
6785 | const char *libname; | |
8d08fdba | 6786 | { |
596ea4e5 | 6787 | tree decl = build_library_fn_1 (get_identifier (name), ERROR_MARK, type); |
0c11ada6 JM |
6788 | DECL_BUILT_IN_CLASS (decl) = class; |
6789 | DECL_FUNCTION_CODE (decl) = code; | |
d52e4867 | 6790 | DECL_CONTEXT (decl) = context; |
12a22e76 | 6791 | |
57d5032b JM |
6792 | /* The return builtins leave the current function. */ |
6793 | if (code == BUILT_IN_RETURN || code == BUILT_IN_EH_RETURN) | |
6794 | TREE_THIS_VOLATILE (decl) = 1; | |
6795 | ||
6bcedb4e | 6796 | pushdecl (decl); |
6bcedb4e | 6797 | |
8d08fdba MS |
6798 | /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME, |
6799 | we cannot change DECL_ASSEMBLER_NAME until we have installed this | |
6800 | function in the namespace. */ | |
0c11ada6 | 6801 | if (libname) |
92643fea MM |
6802 | SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname)); |
6803 | make_decl_rtl (decl, NULL); | |
935d1834 ZW |
6804 | |
6805 | /* Warn if a function in the namespace for users | |
6806 | is used without an occasion to consider it declared. */ | |
6807 | if (name[0] != '_' || name[1] != '_') | |
6808 | DECL_ANTICIPATED (decl) = 1; | |
6809 | ||
6431177a JM |
6810 | /* Possibly apply some default attributes to this built-in function. */ |
6811 | decl_attributes (&decl, NULL_TREE, 0); | |
6812 | ||
8d08fdba MS |
6813 | return decl; |
6814 | } | |
7f4edbcb | 6815 | |
d52e4867 RS |
6816 | /* Entry point for the benefit of c_common_nodes_and_builtins. |
6817 | ||
6818 | Make a defintion for a builtin function named NAME and whose data type | |
6819 | is TYPE. TYPE should be a function type with argument types. This | |
6820 | function places the anticipated declaration in the global namespace | |
6821 | and additionally in the std namespace if appropriate. | |
6822 | ||
6823 | CLASS and CODE tell later passes how to compile calls to this function. | |
6824 | See tree.h for possible values. | |
6825 | ||
6826 | If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME, | |
6827 | the name to be called if we can't opencode the function. */ | |
6828 | ||
6829 | tree | |
6830 | builtin_function (name, type, code, class, libname) | |
6831 | const char *name; | |
6832 | tree type; | |
6833 | int code; | |
6834 | enum built_in_class class; | |
6835 | const char *libname; | |
6836 | { | |
6837 | /* All builtins that don't begin with an '_' should additionally | |
6838 | go in the 'std' namespace. */ | |
6839 | if (name[0] != '_') | |
6840 | { | |
6841 | push_namespace (std_identifier); | |
6842 | builtin_function_1 (name, type, std_node, code, class, libname); | |
6843 | pop_namespace (); | |
6844 | } | |
6845 | ||
6846 | return builtin_function_1 (name, type, NULL_TREE, code, class, libname); | |
6847 | } | |
6848 | ||
0c11ada6 JM |
6849 | /* Generate a FUNCTION_DECL with the typical flags for a runtime library |
6850 | function. Not called directly. */ | |
6851 | ||
6852 | static tree | |
596ea4e5 | 6853 | build_library_fn_1 (name, operator_code, type) |
0c11ada6 | 6854 | tree name; |
596ea4e5 | 6855 | enum tree_code operator_code; |
0c11ada6 JM |
6856 | tree type; |
6857 | { | |
6858 | tree fn = build_lang_decl (FUNCTION_DECL, name, type); | |
6859 | DECL_EXTERNAL (fn) = 1; | |
6860 | TREE_PUBLIC (fn) = 1; | |
6861 | DECL_ARTIFICIAL (fn) = 1; | |
6862 | TREE_NOTHROW (fn) = 1; | |
596ea4e5 | 6863 | SET_OVERLOADED_OPERATOR_CODE (fn, operator_code); |
5d2ed28c | 6864 | SET_DECL_LANGUAGE (fn, lang_c); |
0c11ada6 JM |
6865 | return fn; |
6866 | } | |
c2a37c55 | 6867 | |
0c11ada6 JM |
6868 | /* Returns the _DECL for a library function with C linkage. |
6869 | We assume that such functions never throw; if this is incorrect, | |
6870 | callers should unset TREE_NOTHROW. */ | |
c2a37c55 | 6871 | |
7f4edbcb | 6872 | tree |
0c11ada6 JM |
6873 | build_library_fn (name, type) |
6874 | tree name; | |
6875 | tree type; | |
6876 | { | |
19e7881c | 6877 | return build_library_fn_1 (name, ERROR_MARK, type); |
0c11ada6 JM |
6878 | } |
6879 | ||
6880 | /* Returns the _DECL for a library function with C++ linkage. */ | |
6881 | ||
596ea4e5 AS |
6882 | static tree |
6883 | build_cp_library_fn (name, operator_code, type) | |
0c11ada6 | 6884 | tree name; |
596ea4e5 | 6885 | enum tree_code operator_code; |
0c11ada6 JM |
6886 | tree type; |
6887 | { | |
596ea4e5 | 6888 | tree fn = build_library_fn_1 (name, operator_code, type); |
0c11ada6 | 6889 | TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type); |
6bbf1598 | 6890 | DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace); |
5d2ed28c | 6891 | SET_DECL_LANGUAGE (fn, lang_cplusplus); |
0c11ada6 | 6892 | set_mangled_name_for_decl (fn); |
0c11ada6 JM |
6893 | return fn; |
6894 | } | |
6895 | ||
6896 | /* Like build_library_fn, but takes a C string instead of an | |
6897 | IDENTIFIER_NODE. */ | |
6898 | ||
6899 | tree | |
6900 | build_library_fn_ptr (name, type) | |
7f4edbcb BS |
6901 | const char *name; |
6902 | tree type; | |
7f4edbcb | 6903 | { |
0c11ada6 JM |
6904 | return build_library_fn (get_identifier (name), type); |
6905 | } | |
6906 | ||
6907 | /* Like build_cp_library_fn, but takes a C string instead of an | |
6908 | IDENTIFIER_NODE. */ | |
6909 | ||
6910 | tree | |
6911 | build_cp_library_fn_ptr (name, type) | |
6912 | const char *name; | |
6913 | tree type; | |
6914 | { | |
596ea4e5 | 6915 | return build_cp_library_fn (get_identifier (name), ERROR_MARK, type); |
0c11ada6 JM |
6916 | } |
6917 | ||
6918 | /* Like build_library_fn, but also pushes the function so that we will | |
6919 | be able to find it via IDENTIFIER_GLOBAL_VALUE. */ | |
6920 | ||
6921 | tree | |
6922 | push_library_fn (name, type) | |
6923 | tree name, type; | |
6924 | { | |
6925 | tree fn = build_library_fn (name, type); | |
6926 | pushdecl_top_level (fn); | |
6927 | return fn; | |
6928 | } | |
6929 | ||
6930 | /* Like build_cp_library_fn, but also pushes the function so that it | |
6931 | will be found by normal lookup. */ | |
6932 | ||
596ea4e5 AS |
6933 | static tree |
6934 | push_cp_library_fn (operator_code, type) | |
6935 | enum tree_code operator_code; | |
0c11ada6 JM |
6936 | tree type; |
6937 | { | |
5362b086 | 6938 | tree fn = build_cp_library_fn (ansi_opname (operator_code), |
596ea4e5 AS |
6939 | operator_code, |
6940 | type); | |
0c11ada6 JM |
6941 | pushdecl (fn); |
6942 | return fn; | |
6943 | } | |
6944 | ||
6945 | /* Like push_library_fn, but takes a TREE_LIST of parm types rather than | |
6946 | a FUNCTION_TYPE. */ | |
6947 | ||
6948 | tree | |
6949 | push_void_library_fn (name, parmtypes) | |
6950 | tree name, parmtypes; | |
6951 | { | |
6952 | tree type = build_function_type (void_type_node, parmtypes); | |
6953 | return push_library_fn (name, type); | |
6954 | } | |
6955 | ||
cf74fb86 | 6956 | /* Like push_library_fn, but also note that this function throws |
0c11ada6 JM |
6957 | and does not return. Used for __throw_foo and the like. */ |
6958 | ||
6959 | tree | |
cf74fb86 JM |
6960 | push_throw_library_fn (name, type) |
6961 | tree name, type; | |
0c11ada6 | 6962 | { |
cf74fb86 | 6963 | tree fn = push_library_fn (name, type); |
0c11ada6 JM |
6964 | TREE_THIS_VOLATILE (fn) = 1; |
6965 | TREE_NOTHROW (fn) = 0; | |
6966 | return fn; | |
7f4edbcb | 6967 | } |
6431177a JM |
6968 | |
6969 | /* Apply default attributes to a function, if a system function with default | |
6970 | attributes. */ | |
6971 | ||
6972 | void | |
7ffb4fd2 | 6973 | cxx_insert_default_attributes (decl) |
6431177a JM |
6974 | tree decl; |
6975 | { | |
6976 | if (!DECL_EXTERN_C_FUNCTION_P (decl)) | |
6977 | return; | |
6978 | if (!TREE_PUBLIC (decl)) | |
6979 | return; | |
6980 | c_common_insert_default_attributes (decl); | |
6981 | } | |
8d08fdba | 6982 | \f |
61a127b3 MM |
6983 | /* When we call finish_struct for an anonymous union, we create |
6984 | default copy constructors and such. But, an anonymous union | |
6985 | shouldn't have such things; this function undoes the damage to the | |
6986 | anonymous union type T. | |
6987 | ||
6988 | (The reason that we create the synthesized methods is that we don't | |
6989 | distinguish `union { int i; }' from `typedef union { int i; } U'. | |
6990 | The first is an anonymous union; the second is just an ordinary | |
6991 | union type.) */ | |
6992 | ||
6993 | void | |
6bdb8141 | 6994 | fixup_anonymous_aggr (t) |
61a127b3 MM |
6995 | tree t; |
6996 | { | |
6997 | tree *q; | |
6998 | ||
6999 | /* Wipe out memory of synthesized methods */ | |
7000 | TYPE_HAS_CONSTRUCTOR (t) = 0; | |
7001 | TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0; | |
7002 | TYPE_HAS_INIT_REF (t) = 0; | |
7003 | TYPE_HAS_CONST_INIT_REF (t) = 0; | |
7004 | TYPE_HAS_ASSIGN_REF (t) = 0; | |
61a127b3 MM |
7005 | TYPE_HAS_CONST_ASSIGN_REF (t) = 0; |
7006 | ||
7007 | /* Splice the implicitly generated functions out of the TYPE_METHODS | |
7008 | list. */ | |
7009 | q = &TYPE_METHODS (t); | |
7010 | while (*q) | |
7011 | { | |
7012 | if (DECL_ARTIFICIAL (*q)) | |
7013 | *q = TREE_CHAIN (*q); | |
7014 | else | |
7015 | q = &TREE_CHAIN (*q); | |
7016 | } | |
7017 | ||
cab1f180 | 7018 | /* ISO C++ 9.5.3. Anonymous unions may not have function members. */ |
61a127b3 | 7019 | if (TYPE_METHODS (t)) |
1f0d71c5 | 7020 | cp_error_at ("an anonymous union cannot have function members", t); |
a1c2b86d JJ |
7021 | |
7022 | /* Anonymous aggregates cannot have fields with ctors, dtors or complex | |
7023 | assignment operators (because they cannot have these methods themselves). | |
7024 | For anonymous unions this is already checked because they are not allowed | |
7025 | in any union, otherwise we have to check it. */ | |
7026 | if (TREE_CODE (t) != UNION_TYPE) | |
7027 | { | |
7028 | tree field, type; | |
7029 | ||
7030 | for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field)) | |
7031 | if (TREE_CODE (field) == FIELD_DECL) | |
7032 | { | |
7033 | type = TREE_TYPE (field); | |
7034 | if (CLASS_TYPE_P (type)) | |
7035 | { | |
7036 | if (TYPE_NEEDS_CONSTRUCTING (type)) | |
7037 | cp_error_at ("member %#D' with constructor not allowed in anonymous aggregate", | |
7038 | field); | |
7039 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) | |
7040 | cp_error_at ("member %#D' with destructor not allowed in anonymous aggregate", | |
7041 | field); | |
7042 | if (TYPE_HAS_COMPLEX_ASSIGN_REF (type)) | |
7043 | cp_error_at ("member %#D' with copy assignment operator not allowed in anonymous aggregate", | |
7044 | field); | |
7045 | } | |
7046 | } | |
7047 | } | |
61a127b3 MM |
7048 | } |
7049 | ||
72a93143 JM |
7050 | /* Make sure that a declaration with no declarator is well-formed, i.e. |
7051 | just defines a tagged type or anonymous union. | |
8d08fdba | 7052 | |
72a93143 | 7053 | Returns the type defined, if any. */ |
8d08fdba | 7054 | |
72a93143 JM |
7055 | tree |
7056 | check_tag_decl (declspecs) | |
8d08fdba MS |
7057 | tree declspecs; |
7058 | { | |
72a93143 | 7059 | int found_type = 0; |
2bdb0643 | 7060 | int saw_friend = 0; |
e8186ecf | 7061 | int saw_typedef = 0; |
2986ae00 | 7062 | tree ob_modifier = NULL_TREE; |
8d08fdba | 7063 | register tree link; |
8d08fdba MS |
7064 | register tree t = NULL_TREE; |
7065 | ||
7066 | for (link = declspecs; link; link = TREE_CHAIN (link)) | |
7067 | { | |
7068 | register tree value = TREE_VALUE (link); | |
7069 | ||
2bdb0643 | 7070 | if (TYPE_P (value) |
bd0d5d4a | 7071 | || TREE_CODE (value) == TYPE_DECL |
2bdb0643 JM |
7072 | || (TREE_CODE (value) == IDENTIFIER_NODE |
7073 | && IDENTIFIER_GLOBAL_VALUE (value) | |
bd0d5d4a | 7074 | && TREE_CODE (IDENTIFIER_GLOBAL_VALUE (value)) == TYPE_DECL)) |
8d08fdba | 7075 | { |
72a93143 | 7076 | ++found_type; |
5566b478 | 7077 | |
bd0d5d4a JM |
7078 | if (found_type == 2 && TREE_CODE (value) == IDENTIFIER_NODE) |
7079 | { | |
7080 | if (! in_system_header) | |
33bd39a2 | 7081 | pedwarn ("redeclaration of C++ built-in type `%T'", value); |
bd0d5d4a JM |
7082 | return NULL_TREE; |
7083 | } | |
7084 | ||
7085 | if (TYPE_P (value) | |
7086 | && ((TREE_CODE (value) != TYPENAME_TYPE && IS_AGGR_TYPE (value)) | |
7087 | || TREE_CODE (value) == ENUMERAL_TYPE)) | |
72a93143 JM |
7088 | { |
7089 | my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261); | |
7090 | t = value; | |
7091 | } | |
8d08fdba | 7092 | } |
e8186ecf GDR |
7093 | else if (value == ridpointers[(int) RID_TYPEDEF]) |
7094 | saw_typedef = 1; | |
83f660b7 JM |
7095 | else if (value == ridpointers[(int) RID_FRIEND]) |
7096 | { | |
83f660b7 JM |
7097 | if (current_class_type == NULL_TREE |
7098 | || current_scope () != current_class_type) | |
7099 | ob_modifier = value; | |
2bdb0643 JM |
7100 | else |
7101 | saw_friend = 1; | |
83f660b7 | 7102 | } |
8d08fdba | 7103 | else if (value == ridpointers[(int) RID_STATIC] |
2986ae00 MS |
7104 | || value == ridpointers[(int) RID_EXTERN] |
7105 | || value == ridpointers[(int) RID_AUTO] | |
28cbf42c MS |
7106 | || value == ridpointers[(int) RID_REGISTER] |
7107 | || value == ridpointers[(int) RID_INLINE] | |
7108 | || value == ridpointers[(int) RID_VIRTUAL] | |
72a93143 JM |
7109 | || value == ridpointers[(int) RID_CONST] |
7110 | || value == ridpointers[(int) RID_VOLATILE] | |
28cbf42c | 7111 | || value == ridpointers[(int) RID_EXPLICIT]) |
2986ae00 | 7112 | ob_modifier = value; |
8d08fdba MS |
7113 | } |
7114 | ||
72a93143 JM |
7115 | if (found_type > 1) |
7116 | error ("multiple types in one declaration"); | |
7e2067ca | 7117 | |
2bdb0643 | 7118 | if (t == NULL_TREE && ! saw_friend) |
7e2067ca | 7119 | pedwarn ("declaration does not declare anything"); |
0dd3962d | 7120 | |
1951a1b6 | 7121 | /* Check for an anonymous union. */ |
6bdb8141 | 7122 | else if (t && IS_AGGR_TYPE_CODE (TREE_CODE (t)) |
1951a1b6 | 7123 | && TYPE_ANONYMOUS_P (t)) |
0dd3962d | 7124 | { |
e8186ecf GDR |
7125 | /* 7/3 In a simple-declaration, the optional init-declarator-list |
7126 | can be omitted only when declaring a class (clause 9) or | |
7127 | enumeration (7.2), that is, when the decl-specifier-seq contains | |
7128 | either a class-specifier, an elaborated-type-specifier with | |
7129 | a class-key (9.1), or an enum-specifier. In these cases and | |
7130 | whenever a class-specifier or enum-specifier is present in the | |
7131 | decl-specifier-seq, the identifiers in these specifiers are among | |
7132 | the names being declared by the declaration (as class-name, | |
7133 | enum-names, or enumerators, depending on the syntax). In such | |
7134 | cases, and except for the declaration of an unnamed bit-field (9.6), | |
7135 | the decl-specifier-seq shall introduce one or more names into the | |
7136 | program, or shall redeclare a name introduced by a previous | |
7137 | declaration. [Example: | |
7138 | enum { }; // ill-formed | |
7139 | typedef class { }; // ill-formed | |
7140 | --end example] */ | |
7141 | if (saw_typedef) | |
7142 | { | |
357351e5 | 7143 | error ("missing type-name in typedef-declaration"); |
e8186ecf GDR |
7144 | return NULL_TREE; |
7145 | } | |
0dd3962d | 7146 | /* Anonymous unions are objects, so they can have specifiers. */; |
6bdb8141 JM |
7147 | SET_ANON_AGGR_TYPE_P (t); |
7148 | ||
7149 | if (TREE_CODE (t) != UNION_TYPE && pedantic && ! in_system_header) | |
7150 | pedwarn ("ISO C++ prohibits anonymous structs"); | |
0dd3962d JM |
7151 | } |
7152 | ||
83f660b7 | 7153 | else if (ob_modifier) |
8d08fdba | 7154 | { |
83f660b7 JM |
7155 | if (ob_modifier == ridpointers[(int) RID_INLINE] |
7156 | || ob_modifier == ridpointers[(int) RID_VIRTUAL]) | |
33bd39a2 | 7157 | error ("`%D' can only be specified for functions", ob_modifier); |
83f660b7 | 7158 | else if (ob_modifier == ridpointers[(int) RID_FRIEND]) |
33bd39a2 | 7159 | error ("`%D' can only be specified inside a class", ob_modifier); |
83f660b7 | 7160 | else if (ob_modifier == ridpointers[(int) RID_EXPLICIT]) |
33bd39a2 | 7161 | error ("`%D' can only be specified for constructors", |
83f660b7 JM |
7162 | ob_modifier); |
7163 | else | |
33bd39a2 | 7164 | error ("`%D' can only be specified for objects and functions", |
83f660b7 | 7165 | ob_modifier); |
72a93143 | 7166 | } |
8d08fdba | 7167 | |
72a93143 JM |
7168 | return t; |
7169 | } | |
7170 | ||
7171 | /* Called when a declaration is seen that contains no names to declare. | |
7172 | If its type is a reference to a structure, union or enum inherited | |
7173 | from a containing scope, shadow that tag name for the current scope | |
7174 | with a forward reference. | |
7175 | If its type defines a new named structure or union | |
7176 | or defines an enum, it is valid but we need not do anything here. | |
7177 | Otherwise, it is an error. | |
7178 | ||
7179 | C++: may have to grok the declspecs to learn about static, | |
7180 | complain for anonymous unions. */ | |
7181 | ||
7182 | void | |
7183 | shadow_tag (declspecs) | |
7184 | tree declspecs; | |
7185 | { | |
7186 | tree t = check_tag_decl (declspecs); | |
7187 | ||
7188 | if (t) | |
7189 | maybe_process_partial_specialization (t); | |
7190 | ||
7191 | /* This is where the variables in an anonymous union are | |
7192 | declared. An anonymous union declaration looks like: | |
7193 | union { ... } ; | |
7194 | because there is no declarator after the union, the parser | |
7195 | sends that declaration here. */ | |
6bdb8141 | 7196 | if (t && ANON_AGGR_TYPE_P (t)) |
72a93143 | 7197 | { |
6bdb8141 | 7198 | fixup_anonymous_aggr (t); |
72a93143 JM |
7199 | |
7200 | if (TYPE_FIELDS (t)) | |
7201 | { | |
7202 | tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0, | |
91d231cb | 7203 | NULL); |
72a93143 JM |
7204 | finish_anon_union (decl); |
7205 | } | |
8d08fdba MS |
7206 | } |
7207 | } | |
7208 | \f | |
7209 | /* Decode a "typename", such as "int **", returning a ..._TYPE node. */ | |
7210 | ||
7211 | tree | |
7212 | groktypename (typename) | |
7213 | tree typename; | |
7214 | { | |
98884b26 JM |
7215 | tree specs, attrs; |
7216 | tree type; | |
8d08fdba MS |
7217 | if (TREE_CODE (typename) != TREE_LIST) |
7218 | return typename; | |
98884b26 JM |
7219 | split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs); |
7220 | type = grokdeclarator (TREE_VALUE (typename), specs, | |
7221 | TYPENAME, 0, &attrs); | |
7222 | if (attrs) | |
7223 | cplus_decl_attributes (&type, attrs, 0); | |
7224 | return type; | |
8d08fdba MS |
7225 | } |
7226 | ||
7227 | /* Decode a declarator in an ordinary declaration or data definition. | |
7228 | This is called as soon as the type information and variable name | |
7229 | have been parsed, before parsing the initializer if any. | |
7230 | Here we create the ..._DECL node, fill in its type, | |
7231 | and put it on the list of decls for the current context. | |
7232 | The ..._DECL node is returned as the value. | |
7233 | ||
7234 | Exception: for arrays where the length is not specified, | |
82580166 | 7235 | the type is left null, to be filled in by `cp_finish_decl'. |
8d08fdba MS |
7236 | |
7237 | Function definitions do not come here; they go to start_function | |
7238 | instead. However, external and forward declarations of functions | |
7239 | do go through here. Structure field declarations are done by | |
7240 | grokfield and not through here. */ | |
7241 | ||
8d08fdba | 7242 | tree |
a1774733 | 7243 | start_decl (declarator, declspecs, initialized, attributes, prefix_attributes) |
8d08fdba MS |
7244 | tree declarator, declspecs; |
7245 | int initialized; | |
a1774733 | 7246 | tree attributes, prefix_attributes; |
8d08fdba | 7247 | { |
59387d2e | 7248 | tree decl; |
8d08fdba MS |
7249 | register tree type, tem; |
7250 | tree context; | |
7251 | extern int have_extern_spec; | |
7252 | extern int used_extern_spec; | |
7253 | ||
5566b478 MS |
7254 | #if 0 |
7255 | /* See code below that used this. */ | |
8d08fdba | 7256 | int init_written = initialized; |
5566b478 | 7257 | #endif |
8d08fdba | 7258 | |
e92cc029 | 7259 | /* This should only be done once on the top most decl. */ |
8d08fdba MS |
7260 | if (have_extern_spec && !used_extern_spec) |
7261 | { | |
1f8f4a0b MM |
7262 | declspecs = tree_cons (NULL_TREE, get_identifier ("extern"), |
7263 | declspecs); | |
8d08fdba MS |
7264 | used_extern_spec = 1; |
7265 | } | |
7266 | ||
e23bd218 IR |
7267 | /* An object declared as __attribute__((deprecated)) suppresses |
7268 | warnings of uses of other deprecated items. */ | |
7269 | if (lookup_attribute ("deprecated", attributes)) | |
7270 | deprecated_state = DEPRECATED_SUPPRESS; | |
7271 | ||
91d231cb | 7272 | attributes = chainon (attributes, prefix_attributes); |
b17e2870 | 7273 | |
c11b6f21 | 7274 | decl = grokdeclarator (declarator, declspecs, NORMAL, initialized, |
91d231cb | 7275 | &attributes); |
68642fb6 | 7276 | |
e23bd218 IR |
7277 | deprecated_state = DEPRECATED_NORMAL; |
7278 | ||
a1774733 | 7279 | if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE) |
8d08fdba MS |
7280 | return NULL_TREE; |
7281 | ||
7282 | type = TREE_TYPE (decl); | |
7283 | ||
44689c12 ML |
7284 | if (type == error_mark_node) |
7285 | return NULL_TREE; | |
7286 | ||
4f1c5b7d | 7287 | context = DECL_CONTEXT (decl); |
8d08fdba | 7288 | |
9a68c51f JM |
7289 | if (initialized && context && TREE_CODE (context) == NAMESPACE_DECL |
7290 | && context != current_namespace && TREE_CODE (decl) == VAR_DECL) | |
7291 | { | |
7292 | /* When parsing the initializer, lookup should use the object's | |
7293 | namespace. */ | |
7294 | push_decl_namespace (context); | |
7295 | } | |
7296 | ||
2c73f9f5 ML |
7297 | /* We are only interested in class contexts, later. */ |
7298 | if (context && TREE_CODE (context) == NAMESPACE_DECL) | |
7299 | context = NULL_TREE; | |
7300 | ||
8d08fdba MS |
7301 | if (initialized) |
7302 | /* Is it valid for this decl to have an initializer at all? | |
7303 | If not, set INITIALIZED to zero, which will indirectly | |
82580166 | 7304 | tell `cp_finish_decl' to ignore the initializer once it is parsed. */ |
8d08fdba MS |
7305 | switch (TREE_CODE (decl)) |
7306 | { | |
7307 | case TYPE_DECL: | |
7308 | /* typedef foo = bar means give foo the same type as bar. | |
82580166 | 7309 | We haven't parsed bar yet, so `cp_finish_decl' will fix that up. |
8d08fdba MS |
7310 | Any other case of an initialization in a TYPE_DECL is an error. */ |
7311 | if (pedantic || list_length (declspecs) > 1) | |
7312 | { | |
33bd39a2 | 7313 | error ("typedef `%D' is initialized", decl); |
8d08fdba MS |
7314 | initialized = 0; |
7315 | } | |
7316 | break; | |
7317 | ||
7318 | case FUNCTION_DECL: | |
33bd39a2 | 7319 | error ("function `%#D' is initialized like a variable", decl); |
8d08fdba MS |
7320 | initialized = 0; |
7321 | break; | |
7322 | ||
7323 | default: | |
3e41d13b | 7324 | break; |
8d08fdba MS |
7325 | } |
7326 | ||
8d08fdba MS |
7327 | if (initialized) |
7328 | { | |
a9aedbc2 | 7329 | if (! toplevel_bindings_p () |
8d08fdba | 7330 | && DECL_EXTERNAL (decl)) |
33bd39a2 | 7331 | warning ("declaration of `%#D' has `extern' and is initialized", |
8d08fdba MS |
7332 | decl); |
7333 | DECL_EXTERNAL (decl) = 0; | |
5566b478 | 7334 | if (toplevel_bindings_p ()) |
8d08fdba MS |
7335 | TREE_STATIC (decl) = 1; |
7336 | ||
7337 | /* Tell `pushdecl' this is an initialized decl | |
7338 | even though we don't yet have the initializer expression. | |
82580166 | 7339 | Also tell `cp_finish_decl' it may store the real initializer. */ |
8d08fdba MS |
7340 | DECL_INITIAL (decl) = error_mark_node; |
7341 | } | |
7342 | ||
fa20888b | 7343 | /* Set attributes here so if duplicate decl, will have proper attributes. */ |
91d231cb | 7344 | cplus_decl_attributes (&decl, attributes, 0); |
fa20888b | 7345 | |
ecb0eece RH |
7346 | /* If #pragma weak was used, mark the decl weak now. */ |
7347 | if (current_binding_level == global_binding_level) | |
7348 | maybe_apply_pragma_weak (decl); | |
7349 | ||
97055d5c AO |
7350 | if (TREE_CODE (decl) == FUNCTION_DECL |
7351 | && DECL_DECLARED_INLINE_P (decl) | |
7352 | && DECL_UNINLINABLE (decl) | |
7353 | && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl))) | |
7354 | warning_with_decl (decl, | |
7355 | "inline function `%s' given attribute noinline"); | |
7356 | ||
d0f062fb | 7357 | if (context && COMPLETE_TYPE_P (complete_type (context))) |
5b605f68 | 7358 | { |
6b400b21 | 7359 | push_nested_class (context, 2); |
e97e5263 | 7360 | |
5b605f68 MS |
7361 | if (TREE_CODE (decl) == VAR_DECL) |
7362 | { | |
7363 | tree field = lookup_field (context, DECL_NAME (decl), 0, 0); | |
7364 | if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL) | |
33bd39a2 | 7365 | error ("`%#D' is not a static member of `%#T'", decl, context); |
e349ee73 MS |
7366 | else |
7367 | { | |
7368 | if (DECL_CONTEXT (field) != context) | |
f2d773a2 | 7369 | { |
33bd39a2 | 7370 | pedwarn ("ISO C++ does not permit `%T::%D' to be defined as `%T::%D'", |
f2d773a2 JM |
7371 | DECL_CONTEXT (field), DECL_NAME (decl), |
7372 | context, DECL_NAME (decl)); | |
7373 | DECL_CONTEXT (decl) = DECL_CONTEXT (field); | |
7374 | } | |
75650646 MM |
7375 | /* Static data member are tricky; an in-class initialization |
7376 | still doesn't provide a definition, so the in-class | |
7377 | declaration will have DECL_EXTERNAL set, but will have an | |
7378 | initialization. Thus, duplicate_decls won't warn | |
7379 | about this situation, and so we check here. */ | |
7380 | if (DECL_INITIAL (decl) && DECL_INITIAL (field)) | |
33bd39a2 | 7381 | error ("duplicate initialization of %D", decl); |
e349ee73 MS |
7382 | if (duplicate_decls (decl, field)) |
7383 | decl = field; | |
7384 | } | |
5b605f68 | 7385 | } |
f30432d7 MS |
7386 | else |
7387 | { | |
5566b478 | 7388 | tree field = check_classfn (context, decl); |
f30432d7 MS |
7389 | if (field && duplicate_decls (decl, field)) |
7390 | decl = field; | |
7391 | } | |
7392 | ||
7393 | /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */ | |
dff3e828 | 7394 | DECL_IN_AGGR_P (decl) = 0; |
68642fb6 | 7395 | if ((DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl)) |
1f6e1acc | 7396 | || CLASSTYPE_TEMPLATE_INSTANTIATION (context)) |
84e6233f JM |
7397 | { |
7398 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
7399 | /* [temp.expl.spec] An explicit specialization of a static data | |
7400 | member of a template is a definition if the declaration | |
7401 | includes an initializer; otherwise, it is a declaration. | |
7402 | ||
7403 | We check for processing_specialization so this only applies | |
7404 | to the new specialization syntax. */ | |
7405 | if (DECL_INITIAL (decl) == NULL_TREE && processing_specialization) | |
7406 | DECL_EXTERNAL (decl) = 1; | |
7407 | } | |
f30432d7 | 7408 | |
b7698cf0 | 7409 | if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)) |
33bd39a2 | 7410 | pedwarn ("declaration of `%#D' outside of class is not definition", |
f30432d7 | 7411 | decl); |
5b605f68 MS |
7412 | } |
7413 | ||
9188c363 MM |
7414 | /* Enter this declaration into the symbol table. */ |
7415 | tem = maybe_push_decl (decl); | |
2ee887f2 | 7416 | |
5156628f | 7417 | if (processing_template_decl) |
cd9f6678 | 7418 | tem = push_template_decl (tem); |
5566b478 | 7419 | |
2ee887f2 | 7420 | #if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS) |
a3203465 | 7421 | /* Tell the back-end to use or not use .common as appropriate. If we say |
a50f0918 MS |
7422 | -fconserve-space, we want this to save .data space, at the expense of |
7423 | wrong semantics. If we say -fno-conserve-space, we want this to | |
7424 | produce errors about redefs; to do this we force variables into the | |
7425 | data segment. */ | |
a3203465 | 7426 | DECL_COMMON (tem) = flag_conserve_space || ! TREE_PUBLIC (tem); |
2ee887f2 | 7427 | #endif |
68642fb6 | 7428 | |
5156628f | 7429 | if (! processing_template_decl) |
5566b478 | 7430 | start_decl_1 (tem); |
8d08fdba | 7431 | |
8d08fdba MS |
7432 | return tem; |
7433 | } | |
7434 | ||
5566b478 MS |
7435 | void |
7436 | start_decl_1 (decl) | |
7437 | tree decl; | |
8d08fdba | 7438 | { |
5566b478 MS |
7439 | tree type = TREE_TYPE (decl); |
7440 | int initialized = (DECL_INITIAL (decl) != NULL_TREE); | |
8d08fdba | 7441 | |
44689c12 ML |
7442 | if (type == error_mark_node) |
7443 | return; | |
7444 | ||
bd0d5d4a | 7445 | maybe_push_cleanup_level (type); |
5566b478 MS |
7446 | |
7447 | if (initialized) | |
7448 | /* Is it valid for this decl to have an initializer at all? | |
7449 | If not, set INITIALIZED to zero, which will indirectly | |
7450 | tell `cp_finish_decl' to ignore the initializer once it is parsed. */ | |
8d08fdba | 7451 | { |
5566b478 MS |
7452 | /* Don't allow initializations for incomplete types except for |
7453 | arrays which might be completed by the initialization. */ | |
d0f062fb | 7454 | if (COMPLETE_TYPE_P (complete_type (type))) |
5566b478 MS |
7455 | ; /* A complete type is ok. */ |
7456 | else if (TREE_CODE (type) != ARRAY_TYPE) | |
8d08fdba | 7457 | { |
33bd39a2 | 7458 | error ("variable `%#D' has initializer but incomplete type", |
5566b478 MS |
7459 | decl); |
7460 | initialized = 0; | |
25eb19ff | 7461 | type = TREE_TYPE (decl) = error_mark_node; |
5566b478 | 7462 | } |
d0f062fb | 7463 | else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type)))) |
5566b478 MS |
7464 | { |
7465 | if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)) | |
33bd39a2 | 7466 | error ("elements of array `%#D' have incomplete type", decl); |
5566b478 MS |
7467 | /* else we already gave an error in start_decl. */ |
7468 | initialized = 0; | |
8d08fdba | 7469 | } |
8d08fdba MS |
7470 | } |
7471 | ||
5566b478 MS |
7472 | if (!initialized |
7473 | && TREE_CODE (decl) != TYPE_DECL | |
7474 | && TREE_CODE (decl) != TEMPLATE_DECL | |
07c88314 | 7475 | && type != error_mark_node |
5362b086 | 7476 | && IS_AGGR_TYPE (type) |
07c88314 | 7477 | && ! DECL_EXTERNAL (decl)) |
8d08fdba | 7478 | { |
5156628f | 7479 | if ((! processing_template_decl || ! uses_template_parms (type)) |
d0f062fb | 7480 | && !COMPLETE_TYPE_P (complete_type (type))) |
5566b478 | 7481 | { |
0154eaa8 | 7482 | error ("aggregate `%#D' has incomplete type and cannot be defined", |
5566b478 MS |
7483 | decl); |
7484 | /* Change the type so that assemble_variable will give | |
7485 | DECL an rtl we can live with: (mem (const_int 0)). */ | |
25eb19ff | 7486 | type = TREE_TYPE (decl) = error_mark_node; |
5566b478 MS |
7487 | } |
7488 | else | |
7489 | { | |
7490 | /* If any base type in the hierarchy of TYPE needs a constructor, | |
7491 | then we set initialized to 1. This way any nodes which are | |
7492 | created for the purposes of initializing this aggregate | |
7493 | will live as long as it does. This is necessary for global | |
7494 | aggregates which do not have their initializers processed until | |
7495 | the end of the file. */ | |
7496 | initialized = TYPE_NEEDS_CONSTRUCTING (type); | |
7497 | } | |
7498 | } | |
7499 | ||
5566b478 MS |
7500 | if (! initialized) |
7501 | DECL_INITIAL (decl) = NULL_TREE; | |
7502 | } | |
7503 | ||
7504 | /* Handle initialization of references. | |
38e01259 | 7505 | These three arguments are from `cp_finish_decl', and have the |
e92cc029 MS |
7506 | same meaning here that they do there. |
7507 | ||
7508 | Quotes on semantics can be found in ARM 8.4.3. */ | |
7509 | ||
8e4ce833 | 7510 | static tree |
a703fb38 | 7511 | grok_reference_init (decl, type, init) |
5566b478 | 7512 | tree decl, type, init; |
5566b478 MS |
7513 | { |
7514 | tree tmp; | |
7515 | ||
7516 | if (init == NULL_TREE) | |
7517 | { | |
7518 | if ((DECL_LANG_SPECIFIC (decl) == 0 | |
7519 | || DECL_IN_AGGR_P (decl) == 0) | |
7520 | && ! DECL_THIS_EXTERN (decl)) | |
33bd39a2 | 7521 | error ("`%D' declared as reference but not initialized", decl); |
8e4ce833 | 7522 | return NULL_TREE; |
5566b478 MS |
7523 | } |
7524 | ||
7525 | if (init == error_mark_node) | |
8e4ce833 | 7526 | return NULL_TREE; |
5566b478 | 7527 | |
ed5511d9 | 7528 | if (TREE_CODE (init) == CONSTRUCTOR) |
5566b478 | 7529 | { |
33bd39a2 | 7530 | error ("ISO C++ forbids use of initializer list to initialize reference `%D'", decl); |
8e4ce833 | 7531 | return NULL_TREE; |
8d08fdba MS |
7532 | } |
7533 | ||
7534 | if (TREE_CODE (init) == TREE_LIST) | |
7535 | init = build_compound_expr (init); | |
8d08fdba | 7536 | |
8ccc31eb MS |
7537 | if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE) |
7538 | init = convert_from_reference (init); | |
7539 | ||
8d08fdba MS |
7540 | if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE |
7541 | && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE) | |
7542 | { | |
a3203465 | 7543 | /* Note: default conversion is only called in very special cases. */ |
8d08fdba MS |
7544 | init = default_conversion (init); |
7545 | } | |
68642fb6 | 7546 | |
24bef158 MM |
7547 | /* Convert INIT to the reference type TYPE. This may involve the |
7548 | creation of a temporary, whose lifetime must be the same as that | |
7549 | of the reference. If so, a DECL_STMT for the temporary will be | |
7550 | added just after the DECL_STMT for DECL. That's why we don't set | |
7551 | DECL_INITIAL for local references (instead assigning to them | |
7552 | explicitly); we need to allow the temporary to be initialized | |
7553 | first. */ | |
a3203465 | 7554 | tmp = convert_to_reference |
9a3b49ac | 7555 | (type, init, CONV_IMPLICIT, |
ce7715bd JM |
7556 | LOOKUP_ONLYCONVERTING|LOOKUP_SPECULATIVELY|LOOKUP_NORMAL|DIRECT_BIND, |
7557 | decl); | |
8d08fdba | 7558 | |
a3203465 | 7559 | if (tmp == error_mark_node) |
8e4ce833 JJ |
7560 | return NULL_TREE; |
7561 | else if (tmp == NULL_TREE) | |
8d08fdba | 7562 | { |
33bd39a2 | 7563 | error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init)); |
8e4ce833 | 7564 | return NULL_TREE; |
8d08fdba | 7565 | } |
8d08fdba | 7566 | |
8e4ce833 JJ |
7567 | if (TREE_STATIC (decl) && !TREE_CONSTANT (tmp)) |
7568 | return tmp; | |
7569 | ||
08ac397c | 7570 | DECL_INITIAL (decl) = tmp; |
8e4ce833 JJ |
7571 | |
7572 | return NULL_TREE; | |
8d08fdba MS |
7573 | } |
7574 | ||
6060a796 MS |
7575 | /* Fill in DECL_INITIAL with some magical value to prevent expand_decl from |
7576 | mucking with forces it does not comprehend (i.e. initialization with a | |
7577 | constructor). If we are at global scope and won't go into COMMON, fill | |
7578 | it in with a dummy CONSTRUCTOR to force the variable into .data; | |
7579 | otherwise we can use error_mark_node. */ | |
7580 | ||
28cbf42c MS |
7581 | static tree |
7582 | obscure_complex_init (decl, init) | |
7583 | tree decl, init; | |
6060a796 | 7584 | { |
28cbf42c MS |
7585 | if (! flag_no_inline && TREE_STATIC (decl)) |
7586 | { | |
7587 | if (extract_init (decl, init)) | |
7588 | return NULL_TREE; | |
7589 | } | |
7590 | ||
2ee887f2 | 7591 | #if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS) |
a9aedbc2 | 7592 | if (toplevel_bindings_p () && ! DECL_COMMON (decl)) |
6060a796 MS |
7593 | DECL_INITIAL (decl) = build (CONSTRUCTOR, TREE_TYPE (decl), NULL_TREE, |
7594 | NULL_TREE); | |
7595 | else | |
2ee887f2 | 7596 | #endif |
6060a796 | 7597 | DECL_INITIAL (decl) = error_mark_node; |
28cbf42c MS |
7598 | |
7599 | return init; | |
6060a796 MS |
7600 | } |
7601 | ||
27778b73 MM |
7602 | /* When parsing `int a[] = {1, 2};' we don't know the size of the |
7603 | array until we finish parsing the initializer. If that's the | |
7604 | situation we're in, update DECL accordingly. */ | |
7605 | ||
7606 | static void | |
7607 | maybe_deduce_size_from_array_init (decl, init) | |
7608 | tree decl; | |
7609 | tree init; | |
7610 | { | |
7611 | tree type = TREE_TYPE (decl); | |
7612 | ||
7613 | if (TREE_CODE (type) == ARRAY_TYPE | |
7614 | && TYPE_DOMAIN (type) == NULL_TREE | |
7615 | && TREE_CODE (decl) != TYPE_DECL) | |
7616 | { | |
f2ae0c45 JM |
7617 | /* do_default is really a C-ism to deal with tentative definitions. |
7618 | But let's leave it here to ease the eventual merge. */ | |
7619 | int do_default = !DECL_EXTERNAL (decl); | |
27778b73 MM |
7620 | tree initializer = init ? init : DECL_INITIAL (decl); |
7621 | int failure = complete_array_type (type, initializer, do_default); | |
7622 | ||
7623 | if (failure == 1) | |
33bd39a2 | 7624 | error ("initializer fails to determine size of `%D'", decl); |
27778b73 MM |
7625 | |
7626 | if (failure == 2) | |
7627 | { | |
7628 | if (do_default) | |
33bd39a2 | 7629 | error ("array size missing in `%D'", decl); |
27778b73 MM |
7630 | /* If a `static' var's size isn't known, make it extern as |
7631 | well as static, so it does not get allocated. If it's not | |
7632 | `static', then don't mark it extern; finish_incomplete_decl | |
7633 | will give it a default size and it will get allocated. */ | |
7634 | else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl)) | |
7635 | DECL_EXTERNAL (decl) = 1; | |
7636 | } | |
7637 | ||
7638 | if (pedantic && TYPE_DOMAIN (type) != NULL_TREE | |
7639 | && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)), | |
7640 | integer_zero_node)) | |
33bd39a2 | 7641 | error ("zero-size array `%D'", decl); |
27778b73 MM |
7642 | |
7643 | layout_decl (decl, 0); | |
7644 | } | |
7645 | } | |
7646 | ||
7647 | /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue | |
57b52417 | 7648 | any appropriate error messages regarding the layout. */ |
27778b73 | 7649 | |
57b52417 MM |
7650 | static void |
7651 | layout_var_decl (decl) | |
27778b73 | 7652 | tree decl; |
27778b73 | 7653 | { |
57b52417 | 7654 | tree type = TREE_TYPE (decl); |
c95cd22e | 7655 | #if 0 |
57b52417 | 7656 | tree ttype = target_type (type); |
c95cd22e | 7657 | #endif |
57b52417 MM |
7658 | |
7659 | /* If we haven't already layed out this declaration, do so now. | |
7660 | Note that we must not call complete type for an external object | |
7661 | because it's type might involve templates that we are not | |
68642fb6 | 7662 | supposed to isntantiate yet. (And it's perfectly legal to say |
57b52417 MM |
7663 | `extern X x' for some incomplete type `X'.) */ |
7664 | if (!DECL_EXTERNAL (decl)) | |
7665 | complete_type (type); | |
d0f062fb | 7666 | if (!DECL_SIZE (decl) && COMPLETE_TYPE_P (type)) |
27778b73 MM |
7667 | layout_decl (decl, 0); |
7668 | ||
c82dbd95 | 7669 | if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE) |
27778b73 MM |
7670 | { |
7671 | /* An automatic variable with an incomplete type: that is an error. | |
7672 | Don't talk about array types here, since we took care of that | |
7673 | message in grokdeclarator. */ | |
33bd39a2 | 7674 | error ("storage size of `%D' isn't known", decl); |
27778b73 MM |
7675 | TREE_TYPE (decl) = error_mark_node; |
7676 | } | |
ae673f14 JM |
7677 | #if 0 |
7678 | /* Keep this code around in case we later want to control debug info | |
7679 | based on whether a type is "used". (jason 1999-11-11) */ | |
7680 | ||
27778b73 MM |
7681 | else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype)) |
7682 | /* Let debugger know it should output info for this type. */ | |
7683 | note_debug_info_needed (ttype); | |
7684 | ||
7685 | if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl)) | |
7686 | note_debug_info_needed (DECL_CONTEXT (decl)); | |
ae673f14 | 7687 | #endif |
27778b73 MM |
7688 | |
7689 | if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl)) | |
7690 | && DECL_SIZE (decl) != NULL_TREE | |
7691 | && ! TREE_CONSTANT (DECL_SIZE (decl))) | |
7692 | { | |
7693 | if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST) | |
7694 | constant_expression_warning (DECL_SIZE (decl)); | |
7695 | else | |
33bd39a2 | 7696 | error ("storage size of `%D' isn't constant", decl); |
27778b73 | 7697 | } |
8e4ce833 JJ |
7698 | |
7699 | if (TREE_STATIC (decl) | |
7700 | && !DECL_ARTIFICIAL (decl) | |
7701 | && current_function_decl | |
7702 | && DECL_CONTEXT (decl) == current_function_decl) | |
7703 | push_local_name (decl); | |
27778b73 MM |
7704 | } |
7705 | ||
27778b73 MM |
7706 | /* If a local static variable is declared in an inline function, or if |
7707 | we have a weak definition, we must endeavor to create only one | |
7708 | instance of the variable at link-time. */ | |
7709 | ||
7710 | static void | |
7711 | maybe_commonize_var (decl) | |
7712 | tree decl; | |
7713 | { | |
7714 | /* Static data in a function with comdat linkage also has comdat | |
7715 | linkage. */ | |
7716 | if (TREE_STATIC (decl) | |
7717 | /* Don't mess with __FUNCTION__. */ | |
cf74fb86 | 7718 | && ! DECL_ARTIFICIAL (decl) |
27778b73 MM |
7719 | && current_function_decl |
7720 | && DECL_CONTEXT (decl) == current_function_decl | |
79065db2 | 7721 | && (DECL_DECLARED_INLINE_P (current_function_decl) |
27778b73 MM |
7722 | || DECL_TEMPLATE_INSTANTIATION (current_function_decl)) |
7723 | && TREE_PUBLIC (current_function_decl)) | |
7724 | { | |
27778b73 MM |
7725 | /* If flag_weak, we don't need to mess with this, as we can just |
7726 | make the function weak, and let it refer to its unique local | |
7727 | copy. This works because we don't allow the function to be | |
7728 | inlined. */ | |
7729 | if (! flag_weak) | |
7730 | { | |
7731 | if (DECL_INTERFACE_KNOWN (current_function_decl)) | |
7732 | { | |
7733 | TREE_PUBLIC (decl) = 1; | |
7734 | DECL_EXTERNAL (decl) = DECL_EXTERNAL (current_function_decl); | |
7735 | } | |
7736 | else if (DECL_INITIAL (decl) == NULL_TREE | |
7737 | || DECL_INITIAL (decl) == error_mark_node) | |
7738 | { | |
7739 | TREE_PUBLIC (decl) = 1; | |
7740 | DECL_COMMON (decl) = 1; | |
7741 | } | |
7742 | /* else we lose. We can only do this if we can use common, | |
7743 | which we can't if it has been initialized. */ | |
7744 | ||
92643fea | 7745 | if (!TREE_PUBLIC (decl)) |
27778b73 MM |
7746 | { |
7747 | cp_warning_at ("sorry: semantics of inline function static data `%#D' are wrong (you'll wind up with multiple copies)", decl); | |
7748 | cp_warning_at (" you can work around this by removing the initializer", decl); | |
7749 | } | |
7750 | } | |
8e4ce833 JJ |
7751 | else |
7752 | comdat_linkage (decl); | |
27778b73 MM |
7753 | } |
7754 | else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl)) | |
7755 | /* Set it up again; we might have set DECL_INITIAL since the last | |
7756 | time. */ | |
7757 | comdat_linkage (decl); | |
7758 | } | |
7759 | ||
91063b51 MM |
7760 | /* Issue an error message if DECL is an uninitialized const variable. */ |
7761 | ||
7762 | static void | |
7763 | check_for_uninitialized_const_var (decl) | |
7764 | tree decl; | |
7765 | { | |
7766 | tree type = TREE_TYPE (decl); | |
7767 | ||
7768 | /* ``Unless explicitly declared extern, a const object does not have | |
7769 | external linkage and must be initialized. ($8.4; $12.1)'' ARM | |
7770 | 7.1.6 */ | |
7771 | if (TREE_CODE (decl) == VAR_DECL | |
7772 | && TREE_CODE (type) != REFERENCE_TYPE | |
7773 | && CP_TYPE_CONST_P (type) | |
7774 | && !TYPE_NEEDS_CONSTRUCTING (type) | |
7775 | && !DECL_INITIAL (decl)) | |
33bd39a2 | 7776 | error ("uninitialized const `%D'", decl); |
91063b51 MM |
7777 | } |
7778 | ||
c82dbd95 MM |
7779 | /* Verify INIT (the initializer for DECL), and record the |
7780 | initialization in DECL_INITIAL, if appropriate. Returns a new | |
7781 | value for INIT. */ | |
27778b73 | 7782 | |
c82dbd95 MM |
7783 | static tree |
7784 | check_initializer (decl, init) | |
27778b73 | 7785 | tree decl; |
c82dbd95 | 7786 | tree init; |
27778b73 | 7787 | { |
27778b73 MM |
7788 | tree type; |
7789 | ||
7790 | if (TREE_CODE (decl) == FIELD_DECL) | |
c82dbd95 | 7791 | return init; |
27778b73 | 7792 | |
fc0e7bf5 MM |
7793 | type = TREE_TYPE (decl); |
7794 | ||
27778b73 MM |
7795 | /* If `start_decl' didn't like having an initialization, ignore it now. */ |
7796 | if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE) | |
7797 | init = NULL_TREE; | |
27778b73 | 7798 | |
c82dbd95 | 7799 | /* Check the initializer. */ |
27778b73 MM |
7800 | if (init) |
7801 | { | |
c82dbd95 MM |
7802 | /* Things that are going to be initialized need to have complete |
7803 | type. */ | |
7804 | TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl)); | |
7805 | ||
7806 | if (type == error_mark_node) | |
7807 | /* We will have already complained. */ | |
7808 | init = NULL_TREE; | |
d0f062fb | 7809 | else if (COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type))) |
27778b73 | 7810 | { |
33bd39a2 | 7811 | error ("variable-sized object `%D' may not be initialized", decl); |
27778b73 MM |
7812 | init = NULL_TREE; |
7813 | } | |
c82dbd95 | 7814 | else if (TREE_CODE (type) == ARRAY_TYPE |
fe5b6c1c | 7815 | && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (type)))) |
27778b73 | 7816 | { |
33bd39a2 | 7817 | error ("elements of array `%#D' have incomplete type", decl); |
27778b73 MM |
7818 | init = NULL_TREE; |
7819 | } | |
fe5b6c1c | 7820 | else if (TREE_CODE (type) != ARRAY_TYPE && !COMPLETE_TYPE_P (type)) |
c82dbd95 | 7821 | { |
33bd39a2 | 7822 | error ("`%D' has incomplete type", decl); |
c82dbd95 MM |
7823 | TREE_TYPE (decl) = error_mark_node; |
7824 | init = NULL_TREE; | |
7825 | } | |
27778b73 MM |
7826 | } |
7827 | ||
7828 | if (TREE_CODE (decl) == CONST_DECL) | |
7829 | { | |
7830 | my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148); | |
7831 | ||
7832 | DECL_INITIAL (decl) = init; | |
7833 | ||
27778b73 MM |
7834 | my_friendly_assert (init != NULL_TREE, 149); |
7835 | init = NULL_TREE; | |
7836 | } | |
c82dbd95 MM |
7837 | else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE) |
7838 | { | |
8e4ce833 JJ |
7839 | init = grok_reference_init (decl, type, init); |
7840 | if (init) | |
7841 | init = obscure_complex_init (decl, init); | |
c82dbd95 | 7842 | } |
27778b73 MM |
7843 | else if (init) |
7844 | { | |
7845 | if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type)) | |
7846 | { | |
7847 | if (TREE_CODE (type) == ARRAY_TYPE) | |
7848 | init = digest_init (type, init, (tree *) 0); | |
7849 | else if (TREE_CODE (init) == CONSTRUCTOR | |
7850 | && TREE_HAS_CONSTRUCTOR (init)) | |
7851 | { | |
7852 | if (TYPE_NON_AGGREGATE_CLASS (type)) | |
7853 | { | |
33bd39a2 | 7854 | error ("`%D' must be initialized by constructor, not by `{...}'", |
27778b73 MM |
7855 | decl); |
7856 | init = error_mark_node; | |
7857 | } | |
7858 | else | |
7859 | goto dont_use_constructor; | |
7860 | } | |
7861 | } | |
7862 | else | |
7863 | { | |
7864 | dont_use_constructor: | |
7865 | if (TREE_CODE (init) != TREE_VEC) | |
7866 | init = store_init_value (decl, init); | |
7867 | } | |
7868 | ||
7869 | if (init) | |
7870 | /* We must hide the initializer so that expand_decl | |
7871 | won't try to do something it does not understand. */ | |
7872 | init = obscure_complex_init (decl, init); | |
7873 | } | |
7874 | else if (DECL_EXTERNAL (decl)) | |
7875 | ; | |
2f939d94 | 7876 | else if (TYPE_P (type) |
27778b73 MM |
7877 | && (IS_AGGR_TYPE (type) || TYPE_NEEDS_CONSTRUCTING (type))) |
7878 | { | |
7879 | tree core_type = strip_array_types (type); | |
7880 | ||
7881 | if (! TYPE_NEEDS_CONSTRUCTING (core_type)) | |
7882 | { | |
7883 | if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)) | |
33bd39a2 | 7884 | error ("structure `%D' with uninitialized const members", decl); |
27778b73 | 7885 | if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)) |
33bd39a2 | 7886 | error ("structure `%D' with uninitialized reference members", |
27778b73 MM |
7887 | decl); |
7888 | } | |
7889 | ||
7890 | check_for_uninitialized_const_var (decl); | |
7891 | ||
d0f062fb | 7892 | if (COMPLETE_TYPE_P (type) && TYPE_NEEDS_CONSTRUCTING (type)) |
27778b73 MM |
7893 | init = obscure_complex_init (decl, NULL_TREE); |
7894 | ||
7895 | } | |
7896 | else | |
7897 | check_for_uninitialized_const_var (decl); | |
68642fb6 | 7898 | |
c82dbd95 | 7899 | return init; |
27778b73 MM |
7900 | } |
7901 | ||
7902 | /* If DECL is not a local variable, give it RTL. */ | |
7903 | ||
7904 | static void | |
7905 | make_rtl_for_nonlocal_decl (decl, init, asmspec) | |
7906 | tree decl; | |
7907 | tree init; | |
7908 | const char *asmspec; | |
7909 | { | |
95ee998c MM |
7910 | int toplev = toplevel_bindings_p (); |
7911 | int defer_p; | |
27778b73 | 7912 | |
f39ee884 MM |
7913 | /* Handle non-variables up front. */ |
7914 | if (TREE_CODE (decl) != VAR_DECL) | |
7915 | { | |
7916 | rest_of_decl_compilation (decl, asmspec, toplev, at_eof); | |
7917 | return; | |
7918 | } | |
7919 | ||
95ee998c MM |
7920 | /* If we see a class member here, it should be a static data |
7921 | member. */ | |
7922 | if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl)) | |
7923 | { | |
7924 | my_friendly_assert (TREE_STATIC (decl), 19990828); | |
7925 | /* An in-class declaration of a static data member should be | |
7926 | external; it is only a declaration, and not a definition. */ | |
7927 | if (init == NULL_TREE) | |
7928 | my_friendly_assert (DECL_EXTERNAL (decl), 20000723); | |
7929 | } | |
7930 | ||
f39ee884 MM |
7931 | /* Set the DECL_ASSEMBLER_NAME for the variable. */ |
7932 | if (asmspec) | |
7ed47c04 MM |
7933 | { |
7934 | SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec)); | |
7935 | /* The `register' keyword, when used together with an | |
7936 | asm-specification, indicates that the variable should be | |
7937 | placed in a particular register. */ | |
7938 | if (DECL_REGISTER (decl)) | |
7939 | DECL_C_HARD_REGISTER (decl) = 1; | |
7940 | } | |
f39ee884 | 7941 | |
95ee998c MM |
7942 | /* We don't create any RTL for local variables. */ |
7943 | if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl)) | |
7944 | return; | |
27778b73 | 7945 | |
95ee998c MM |
7946 | /* We defer emission of local statics until the corresponding |
7947 | DECL_STMT is expanded. */ | |
7948 | defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl); | |
7949 | ||
b602511f JM |
7950 | /* We try to defer namespace-scope static constants so that they are |
7951 | not emitted into the object file unnecessarily. */ | |
7952 | if (!DECL_VIRTUAL_P (decl) | |
7953 | && TREE_READONLY (decl) | |
7954 | && DECL_INITIAL (decl) != NULL_TREE | |
7955 | && DECL_INITIAL (decl) != error_mark_node | |
7956 | && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)) | |
7957 | && toplev | |
7958 | && !TREE_PUBLIC (decl)) | |
5cc90635 JM |
7959 | { |
7960 | /* Fool with the linkage of static consts according to #pragma | |
7961 | interface. */ | |
7962 | if (!interface_unknown && !TREE_PUBLIC (decl)) | |
27778b73 | 7963 | { |
95ee998c MM |
7964 | TREE_PUBLIC (decl) = 1; |
7965 | DECL_EXTERNAL (decl) = interface_only; | |
27778b73 | 7966 | } |
27778b73 | 7967 | |
95ee998c | 7968 | defer_p = 1; |
27778b73 | 7969 | } |
b602511f JM |
7970 | /* Likewise for template instantiations. */ |
7971 | else if (DECL_COMDAT (decl)) | |
7972 | defer_p = 1; | |
95ee998c | 7973 | |
92643fea MM |
7974 | /* If we're deferring the variable, we only need to make RTL if |
7975 | there's an ASMSPEC. Otherwise, we'll lazily create it later when | |
7976 | we need it. (There's no way to lazily create RTL for things that | |
7977 | have assembly specs because the information about the specifier | |
7978 | isn't stored in the tree, yet) */ | |
7979 | if (defer_p && asmspec) | |
6c418184 | 7980 | make_decl_rtl (decl, asmspec); |
95ee998c | 7981 | /* If we're not deferring, go ahead and assemble the variable. */ |
92643fea | 7982 | else if (!defer_p) |
27778b73 | 7983 | rest_of_decl_compilation (decl, asmspec, toplev, at_eof); |
27778b73 MM |
7984 | } |
7985 | ||
7986 | /* The old ARM scoping rules injected variables declared in the | |
7987 | initialization statement of a for-statement into the surrounding | |
7988 | scope. We support this usage, in order to be backward-compatible. | |
7989 | DECL is a just-declared VAR_DECL; if necessary inject its | |
7990 | declaration into the surrounding scope. */ | |
7991 | ||
b7b8bcd2 | 7992 | void |
27778b73 MM |
7993 | maybe_inject_for_scope_var (decl) |
7994 | tree decl; | |
7995 | { | |
c3783399 NS |
7996 | if (!DECL_NAME (decl)) |
7997 | return; | |
5362b086 | 7998 | |
27778b73 MM |
7999 | if (current_binding_level->is_for_scope) |
8000 | { | |
68642fb6 | 8001 | struct binding_level *outer |
27778b73 MM |
8002 | = current_binding_level->level_chain; |
8003 | ||
8004 | /* Check to see if the same name is already bound at the outer | |
8005 | level, either because it was directly declared, or because a | |
8006 | dead for-decl got preserved. In either case, the code would | |
8007 | not have been valid under the ARM scope rules, so clear | |
8008 | is_for_scope for the current_binding_level. | |
8009 | ||
8010 | Otherwise, we need to preserve the temp slot for decl to last | |
8011 | into the outer binding level. */ | |
8012 | ||
68642fb6 | 8013 | tree outer_binding |
27778b73 | 8014 | = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (decl))); |
68642fb6 | 8015 | |
27778b73 | 8016 | if (outer_binding && BINDING_LEVEL (outer_binding) == outer |
68642fb6 | 8017 | && (TREE_CODE (BINDING_VALUE (outer_binding)) |
27778b73 MM |
8018 | == VAR_DECL) |
8019 | && DECL_DEAD_FOR_LOCAL (BINDING_VALUE (outer_binding))) | |
8020 | { | |
8021 | BINDING_VALUE (outer_binding) | |
8022 | = DECL_SHADOWED_FOR_VAR (BINDING_VALUE (outer_binding)); | |
8023 | current_binding_level->is_for_scope = 0; | |
8024 | } | |
8025 | else if (DECL_IN_MEMORY_P (decl)) | |
8026 | preserve_temp_slots (DECL_RTL (decl)); | |
8027 | } | |
8028 | } | |
8029 | ||
ed5511d9 | 8030 | /* Generate code to initialize DECL (a local variable). */ |
27778b73 | 8031 | |
b7b8bcd2 MM |
8032 | void |
8033 | initialize_local_var (decl, init, flags) | |
27778b73 MM |
8034 | tree decl; |
8035 | tree init; | |
27778b73 MM |
8036 | int flags; |
8037 | { | |
9ed9e79a | 8038 | tree type = TREE_TYPE (decl); |
27778b73 | 8039 | |
9ed9e79a MM |
8040 | /* If the type is bogus, don't bother initializing the variable. */ |
8041 | if (type == error_mark_node) | |
8042 | return; | |
b7b8bcd2 | 8043 | |
b7b8bcd2 MM |
8044 | if (DECL_SIZE (decl) == NULL_TREE && !TREE_STATIC (decl)) |
8045 | { | |
8046 | /* If we used it already as memory, it must stay in memory. */ | |
8047 | DECL_INITIAL (decl) = NULL_TREE; | |
8048 | TREE_ADDRESSABLE (decl) = TREE_USED (decl); | |
8049 | } | |
8050 | ||
9ed9e79a MM |
8051 | /* Local statics are handled differently from ordinary automatic |
8052 | variables. */ | |
8053 | if (TREE_STATIC (decl)) | |
8054 | { | |
8055 | if (TYPE_NEEDS_CONSTRUCTING (type) || init != NULL_TREE | |
834c6dff | 8056 | || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) |
9ed9e79a MM |
8057 | expand_static_init (decl, init); |
8058 | return; | |
8059 | } | |
8060 | ||
27778b73 MM |
8061 | if (DECL_SIZE (decl) && type != error_mark_node) |
8062 | { | |
8063 | int already_used; | |
68642fb6 | 8064 | |
27778b73 | 8065 | /* Compute and store the initial value. */ |
27778b73 MM |
8066 | already_used = TREE_USED (decl) || TREE_USED (type); |
8067 | ||
8068 | if (init || TYPE_NEEDS_CONSTRUCTING (type)) | |
8069 | { | |
24bef158 MM |
8070 | int saved_stmts_are_full_exprs_p; |
8071 | ||
3a0d3e1e | 8072 | my_friendly_assert (building_stmt_tree (), 20000906); |
f2c5f623 | 8073 | saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p (); |
ae499cce | 8074 | current_stmt_tree ()->stmts_are_full_exprs_p = 1; |
3a0d3e1e | 8075 | finish_expr_stmt (build_aggr_init (decl, init, flags)); |
5362b086 | 8076 | current_stmt_tree ()->stmts_are_full_exprs_p = |
ae499cce | 8077 | saved_stmts_are_full_exprs_p; |
27778b73 MM |
8078 | } |
8079 | ||
8080 | /* Set this to 0 so we can tell whether an aggregate which was | |
8081 | initialized was ever used. Don't do this if it has a | |
8082 | destructor, so we don't complain about the 'resource | |
b7b8bcd2 MM |
8083 | allocation is initialization' idiom. Now set |
8084 | attribute((unused)) on types so decls of that type will be | |
8085 | marked used. (see TREE_USED, above.) */ | |
27778b73 MM |
8086 | if (TYPE_NEEDS_CONSTRUCTING (type) |
8087 | && ! already_used | |
834c6dff | 8088 | && TYPE_HAS_TRIVIAL_DESTRUCTOR (type) |
27778b73 MM |
8089 | && DECL_NAME (decl)) |
8090 | TREE_USED (decl) = 0; | |
b7b8bcd2 | 8091 | else if (already_used) |
27778b73 MM |
8092 | TREE_USED (decl) = 1; |
8093 | } | |
24bef158 | 8094 | } |
27778b73 | 8095 | |
24bef158 MM |
8096 | /* Generate code to destroy DECL (a local variable). */ |
8097 | ||
68642fb6 | 8098 | static void |
24bef158 MM |
8099 | destroy_local_var (decl) |
8100 | tree decl; | |
8101 | { | |
9d85d30c MM |
8102 | tree type = TREE_TYPE (decl); |
8103 | tree cleanup; | |
8104 | ||
8105 | /* Only variables get cleaned up. */ | |
8106 | if (TREE_CODE (decl) != VAR_DECL) | |
8107 | return; | |
68642fb6 | 8108 | |
9d85d30c | 8109 | /* And only things with destructors need cleaning up. */ |
655dc6ee JM |
8110 | if (type == error_mark_node |
8111 | || TYPE_HAS_TRIVIAL_DESTRUCTOR (type)) | |
9d85d30c MM |
8112 | return; |
8113 | ||
8114 | if (TREE_CODE (decl) == VAR_DECL && | |
8115 | (DECL_EXTERNAL (decl) || TREE_STATIC (decl))) | |
8116 | /* We don't clean up things that aren't defined in this | |
8117 | translation unit, or that need a static cleanup. The latter | |
8118 | are handled by finish_file. */ | |
8119 | return; | |
68642fb6 | 8120 | |
9d85d30c | 8121 | /* Compute the cleanup. */ |
c88770e9 | 8122 | cleanup = cxx_maybe_build_cleanup (decl); |
27778b73 | 8123 | |
b7b8bcd2 | 8124 | /* Record the cleanup required for this declaration. */ |
6e4ae815 | 8125 | if (DECL_SIZE (decl) && cleanup) |
24bef158 MM |
8126 | finish_decl_cleanup (decl, cleanup); |
8127 | } | |
8128 | ||
8d08fdba MS |
8129 | /* Finish processing of a declaration; |
8130 | install its line number and initial value. | |
8131 | If the length of an array type is not known before, | |
8132 | it must be determined now, from the initial value, or it is an error. | |
8133 | ||
8b27e9ef | 8134 | INIT holds the value of an initializer that should be allowed to escape |
8d08fdba MS |
8135 | the normal rules. |
8136 | ||
920f9474 | 8137 | FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0 |
f78c7bc6 | 8138 | if the (init) syntax was used. */ |
8d08fdba MS |
8139 | |
8140 | void | |
cd9f6678 | 8141 | cp_finish_decl (decl, init, asmspec_tree, flags) |
8d08fdba MS |
8142 | tree decl, init; |
8143 | tree asmspec_tree; | |
6060a796 | 8144 | int flags; |
8d08fdba MS |
8145 | { |
8146 | register tree type; | |
27778b73 | 8147 | tree ttype = NULL_TREE; |
9c0758dd | 8148 | const char *asmspec = NULL; |
8d08fdba MS |
8149 | int was_readonly = 0; |
8150 | ||
8d08fdba MS |
8151 | if (! decl) |
8152 | { | |
8153 | if (init) | |
8251199e | 8154 | error ("assignment (not initialization) in declaration"); |
8d08fdba MS |
8155 | return; |
8156 | } | |
8157 | ||
a4443a08 | 8158 | /* If a name was specified, get the string. */ |
41c64394 RH |
8159 | if (current_binding_level == global_binding_level) |
8160 | asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree); | |
8d08fdba | 8161 | if (asmspec_tree) |
5cc90635 | 8162 | asmspec = TREE_STRING_POINTER (asmspec_tree); |
8d08fdba | 8163 | |
2c73f9f5 ML |
8164 | if (init && TREE_CODE (init) == NAMESPACE_DECL) |
8165 | { | |
33bd39a2 | 8166 | error ("cannot initialize `%D' to namespace `%D'", |
2c73f9f5 ML |
8167 | decl, init); |
8168 | init = NULL_TREE; | |
8169 | } | |
8170 | ||
6ba89f8e | 8171 | if (current_class_type |
4f1c5b7d | 8172 | && CP_DECL_CONTEXT (decl) == current_class_type |
6ba89f8e MM |
8173 | && TYPE_BEING_DEFINED (current_class_type) |
8174 | && (DECL_INITIAL (decl) || init)) | |
3febd123 | 8175 | DECL_INITIALIZED_IN_CLASS_P (decl) = 1; |
6ba89f8e | 8176 | |
68642fb6 | 8177 | if (TREE_CODE (decl) == VAR_DECL |
9a68c51f JM |
8178 | && DECL_CONTEXT (decl) |
8179 | && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL | |
8180 | && DECL_CONTEXT (decl) != current_namespace | |
8181 | && init) | |
8182 | { | |
8183 | /* Leave the namespace of the object. */ | |
8184 | pop_decl_namespace (); | |
8185 | } | |
8186 | ||
c82dbd95 | 8187 | type = TREE_TYPE (decl); |
8d08fdba | 8188 | |
f376e137 | 8189 | if (type == error_mark_node) |
cd9f6678 | 8190 | return; |
5362b086 | 8191 | |
a7a7710d NS |
8192 | if (TYPE_HAS_MUTABLE_P (type)) |
8193 | TREE_READONLY (decl) = 0; | |
24bef158 | 8194 | |
5156628f | 8195 | if (processing_template_decl) |
5566b478 | 8196 | { |
08ac397c JM |
8197 | /* Add this declaration to the statement-tree. */ |
8198 | if (at_function_scope_p () | |
8199 | && TREE_CODE (decl) != RESULT_DECL) | |
8200 | add_decl_stmt (decl); | |
8201 | ||
5566b478 | 8202 | if (init && DECL_INITIAL (decl)) |
2a1e9fdd | 8203 | DECL_INITIAL (decl) = init; |
5566b478 MS |
8204 | goto finish_end0; |
8205 | } | |
3e41d13b | 8206 | |
27778b73 MM |
8207 | /* Parameters are handled by store_parm_decls, not cp_finish_decl. */ |
8208 | my_friendly_assert (TREE_CODE (decl) != PARM_DECL, 19990828); | |
8209 | ||
8d08fdba MS |
8210 | /* Take care of TYPE_DECLs up front. */ |
8211 | if (TREE_CODE (decl) == TYPE_DECL) | |
8212 | { | |
8213 | if (init && DECL_INITIAL (decl)) | |
8214 | { | |
8215 | /* typedef foo = bar; store the type of bar as the type of foo. */ | |
8216 | TREE_TYPE (decl) = type = TREE_TYPE (init); | |
8217 | DECL_INITIAL (decl) = init = NULL_TREE; | |
8218 | } | |
a0a33927 MS |
8219 | if (type != error_mark_node |
8220 | && IS_AGGR_TYPE (type) && DECL_NAME (decl)) | |
8d08fdba MS |
8221 | { |
8222 | if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type) | |
33bd39a2 | 8223 | warning ("shadowing previous type declaration of `%#D'", decl); |
8d08fdba MS |
8224 | set_identifier_type_value (DECL_NAME (decl), type); |
8225 | CLASSTYPE_GOT_SEMICOLON (type) = 1; | |
8226 | } | |
cffa8729 MS |
8227 | |
8228 | /* If we have installed this as the canonical typedef for this | |
8229 | type, and that type has not been defined yet, delay emitting | |
956d6950 | 8230 | the debug information for it, as we will emit it later. */ |
d2e5ee5c | 8231 | if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl |
d0f062fb | 8232 | && !COMPLETE_TYPE_P (TREE_TYPE (decl))) |
cffa8729 MS |
8233 | TYPE_DECL_SUPPRESS_DEBUG (decl) = 1; |
8234 | ||
3e411c3f | 8235 | rest_of_decl_compilation (decl, NULL, |
5566b478 | 8236 | DECL_CONTEXT (decl) == NULL_TREE, at_eof); |
8d08fdba MS |
8237 | goto finish_end; |
8238 | } | |
3e41d13b | 8239 | |
8d08fdba | 8240 | if (TREE_CODE (decl) != FUNCTION_DECL) |
3e41d13b | 8241 | ttype = target_type (type); |
8d08fdba MS |
8242 | |
8243 | if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl) | |
8244 | && TYPE_NEEDS_CONSTRUCTING (type)) | |
8245 | { | |
8d08fdba MS |
8246 | /* Currently, GNU C++ puts constants in text space, making them |
8247 | impossible to initialize. In the future, one would hope for | |
8248 | an operating system which understood the difference between | |
8249 | initialization and the running of a program. */ | |
8250 | was_readonly = 1; | |
8251 | TREE_READONLY (decl) = 0; | |
8252 | } | |
8253 | ||
27778b73 | 8254 | if (TREE_CODE (decl) == FIELD_DECL && asmspec) |
8d08fdba | 8255 | { |
27778b73 MM |
8256 | /* This must override the asm specifier which was placed by |
8257 | grokclassfn. Lay this out fresh. */ | |
19e7881c | 8258 | SET_DECL_RTL (TREE_TYPE (decl), NULL_RTX); |
92643fea | 8259 | SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec)); |
6c418184 | 8260 | make_decl_rtl (decl, asmspec); |
8d08fdba MS |
8261 | } |
8262 | ||
c82dbd95 | 8263 | /* Deduce size of array from initialization, if not already known. */ |
c82dbd95 | 8264 | init = check_initializer (decl, init); |
fe5b6c1c | 8265 | maybe_deduce_size_from_array_init (decl, init); |
3e41d13b | 8266 | |
57cf57fb JM |
8267 | /* Add this declaration to the statement-tree. This needs to happen |
8268 | after the call to check_initializer so that the DECL_STMT for a | |
8269 | reference temp is added before the DECL_STMT for the reference itself. */ | |
08ac397c JM |
8270 | if (building_stmt_tree () |
8271 | && at_function_scope_p () | |
8272 | && TREE_CODE (decl) != RESULT_DECL) | |
8273 | add_decl_stmt (decl); | |
8274 | ||
8d08fdba | 8275 | if (TREE_CODE (decl) == VAR_DECL) |
57b52417 | 8276 | layout_var_decl (decl); |
8d08fdba MS |
8277 | |
8278 | /* Output the assembler code and/or RTL code for variables and functions, | |
8279 | unless the type is an undefined structure or union. | |
8280 | If not, it will get done when the type is completed. */ | |
8d08fdba MS |
8281 | if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL |
8282 | || TREE_CODE (decl) == RESULT_DECL) | |
8283 | { | |
27778b73 MM |
8284 | if (TREE_CODE (decl) == VAR_DECL) |
8285 | maybe_commonize_var (decl); | |
8d08fdba | 8286 | |
27778b73 | 8287 | make_rtl_for_nonlocal_decl (decl, init, asmspec); |
8d08fdba | 8288 | |
68642fb6 | 8289 | if (TREE_CODE (type) == FUNCTION_TYPE |
27778b73 | 8290 | || TREE_CODE (type) == METHOD_TYPE) |
68642fb6 | 8291 | abstract_virtuals_error (decl, |
27778b73 | 8292 | strip_array_types (TREE_TYPE (type))); |
68642fb6 | 8293 | else |
27778b73 | 8294 | abstract_virtuals_error (decl, strip_array_types (type)); |
8d08fdba | 8295 | |
8d08fdba | 8296 | if (TREE_CODE (decl) == FUNCTION_DECL) |
faae18ab | 8297 | ; |
67d743fe MS |
8298 | else if (DECL_EXTERNAL (decl) |
8299 | && ! (DECL_LANG_SPECIFIC (decl) | |
8300 | && DECL_NOT_REALLY_EXTERN (decl))) | |
5566b478 MS |
8301 | { |
8302 | if (init) | |
8303 | DECL_INITIAL (decl) = init; | |
8304 | } | |
b35d4555 | 8305 | else if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL) |
8d08fdba | 8306 | { |
b7b8bcd2 | 8307 | /* This is a local declaration. */ |
b35d4555 MM |
8308 | if (doing_semantic_analysis_p ()) |
8309 | maybe_inject_for_scope_var (decl); | |
b7b8bcd2 MM |
8310 | /* Initialize the local variable. But, if we're building a |
8311 | statement-tree, we'll do the initialization when we | |
8312 | expand the tree. */ | |
24bef158 MM |
8313 | if (processing_template_decl) |
8314 | { | |
8315 | if (init || DECL_INITIAL (decl) == error_mark_node) | |
8316 | DECL_INITIAL (decl) = init; | |
8317 | } | |
8318 | else | |
8319 | { | |
b35d4555 MM |
8320 | /* If we're not building RTL, then we need to do so |
8321 | now. */ | |
44835fdd | 8322 | my_friendly_assert (building_stmt_tree (), 20000906); |
b35d4555 | 8323 | /* Initialize the variable. */ |
24bef158 MM |
8324 | initialize_local_var (decl, init, flags); |
8325 | /* Clean up the variable. */ | |
8326 | destroy_local_var (decl); | |
8327 | } | |
8d08fdba | 8328 | } |
9ed9e79a MM |
8329 | else if (TREE_STATIC (decl) && type != error_mark_node) |
8330 | { | |
8331 | /* Cleanups for static variables are handled by `finish_file'. */ | |
8332 | if (TYPE_NEEDS_CONSTRUCTING (type) || init != NULL_TREE | |
834c6dff | 8333 | || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) |
9ed9e79a MM |
8334 | expand_static_init (decl, init); |
8335 | } | |
8d08fdba MS |
8336 | finish_end0: |
8337 | ||
8338 | /* Undo call to `pushclass' that was done in `start_decl' | |
8339 | due to initialization of qualified member variable. | |
8340 | I.e., Foo::x = 10; */ | |
8341 | { | |
4f1c5b7d | 8342 | tree context = CP_DECL_CONTEXT (decl); |
8d08fdba | 8343 | if (context |
2f939d94 | 8344 | && TYPE_P (context) |
8d08fdba MS |
8345 | && (TREE_CODE (decl) == VAR_DECL |
8346 | /* We also have a pushclass done that we need to undo here | |
8347 | if we're at top level and declare a method. */ | |
5566b478 MS |
8348 | || TREE_CODE (decl) == FUNCTION_DECL) |
8349 | /* If size hasn't been set, we're still defining it, | |
8350 | and therefore inside the class body; don't pop | |
8351 | the binding level.. */ | |
d0f062fb | 8352 | && COMPLETE_TYPE_P (context) |
5566b478 | 8353 | && context == current_class_type) |
6b400b21 | 8354 | pop_nested_class (); |
8d08fdba MS |
8355 | } |
8356 | } | |
8357 | ||
8358 | finish_end: | |
8359 | ||
8d08fdba MS |
8360 | if (was_readonly) |
8361 | TREE_READONLY (decl) = 1; | |
8d08fdba MS |
8362 | } |
8363 | ||
82580166 | 8364 | /* This is here for a midend callback from c-common.c */ |
e92cc029 | 8365 | |
82580166 MS |
8366 | void |
8367 | finish_decl (decl, init, asmspec_tree) | |
8368 | tree decl, init; | |
8369 | tree asmspec_tree; | |
8370 | { | |
cd9f6678 | 8371 | cp_finish_decl (decl, init, asmspec_tree, 0); |
82580166 MS |
8372 | } |
8373 | ||
db4283a0 MM |
8374 | /* Returns a declaration for a VAR_DECL as if: |
8375 | ||
8376 | extern "C" TYPE NAME; | |
8377 | ||
8378 | had been seen. Used to create compiler-generated global | |
8379 | variables. */ | |
8380 | ||
8381 | tree | |
8382 | declare_global_var (name, type) | |
8383 | tree name; | |
8384 | tree type; | |
8385 | { | |
8386 | tree decl; | |
8387 | ||
8388 | push_to_top_level (); | |
8389 | decl = build_decl (VAR_DECL, name, type); | |
8390 | TREE_PUBLIC (decl) = 1; | |
8391 | DECL_EXTERNAL (decl) = 1; | |
8392 | DECL_ARTIFICIAL (decl) = 1; | |
8393 | pushdecl (decl); | |
8394 | cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0); | |
8395 | pop_from_top_level (); | |
8396 | ||
8397 | return decl; | |
8398 | } | |
8399 | ||
8400 | /* Returns a pointer to the `atexit' function. Note that if | |
8401 | FLAG_USE_CXA_ATEXIT is non-zero, then this will actually be the new | |
8402 | `__cxa_atexit' function specified in the IA64 C++ ABI. */ | |
8403 | ||
8404 | static tree | |
8405 | get_atexit_node () | |
8406 | { | |
8407 | tree atexit_fndecl; | |
8408 | tree arg_types; | |
8409 | tree fn_type; | |
8410 | tree fn_ptr_type; | |
8411 | const char *name; | |
8412 | ||
8413 | if (atexit_node) | |
8414 | return atexit_node; | |
8415 | ||
8416 | if (flag_use_cxa_atexit) | |
8417 | { | |
8418 | /* The declaration for `__cxa_atexit' is: | |
8419 | ||
8420 | int __cxa_atexit (void (*)(void *), void *, void *) | |
8421 | ||
8422 | We build up the argument types and then then function type | |
8423 | itself. */ | |
68642fb6 | 8424 | |
db4283a0 MM |
8425 | /* First, build the pointer-to-function type for the first |
8426 | argument. */ | |
8427 | arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node); | |
8428 | fn_type = build_function_type (void_type_node, arg_types); | |
8429 | fn_ptr_type = build_pointer_type (fn_type); | |
8430 | /* Then, build the rest of the argument types. */ | |
8431 | arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node); | |
8432 | arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types); | |
8433 | arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types); | |
8434 | /* And the final __cxa_atexit type. */ | |
8435 | fn_type = build_function_type (integer_type_node, arg_types); | |
8436 | fn_ptr_type = build_pointer_type (fn_type); | |
8437 | name = "__cxa_atexit"; | |
8438 | } | |
8439 | else | |
8440 | { | |
8441 | /* The declaration for `atexit' is: | |
68642fb6 | 8442 | |
db4283a0 MM |
8443 | int atexit (void (*)()); |
8444 | ||
8445 | We build up the argument types and then then function type | |
8446 | itself. */ | |
8447 | fn_type = build_function_type (void_type_node, void_list_node); | |
8448 | fn_ptr_type = build_pointer_type (fn_type); | |
8449 | arg_types = tree_cons (NULL_TREE, fn_ptr_type, void_list_node); | |
8450 | /* Build the final atexit type. */ | |
8451 | fn_type = build_function_type (integer_type_node, arg_types); | |
8452 | name = "atexit"; | |
8453 | } | |
8454 | ||
8455 | /* Now, build the function declaration. */ | |
8456 | push_lang_context (lang_name_c); | |
0c11ada6 | 8457 | atexit_fndecl = build_library_fn_ptr (name, fn_type); |
db4283a0 MM |
8458 | mark_used (atexit_fndecl); |
8459 | pop_lang_context (); | |
8460 | atexit_node = default_conversion (atexit_fndecl); | |
8461 | ||
8462 | return atexit_node; | |
8463 | } | |
8464 | ||
8465 | /* Returns the __dso_handle VAR_DECL. */ | |
8466 | ||
8467 | static tree | |
8468 | get_dso_handle_node () | |
8469 | { | |
8470 | if (dso_handle_node) | |
8471 | return dso_handle_node; | |
8472 | ||
8473 | /* Declare the variable. */ | |
8474 | dso_handle_node = declare_global_var (get_identifier ("__dso_handle"), | |
8475 | ptr_type_node); | |
8476 | ||
8477 | return dso_handle_node; | |
8478 | } | |
8479 | ||
8480 | /* Begin a new function with internal linkage whose job will be simply | |
8481 | to destroy some particular variable. */ | |
8482 | ||
8483 | static tree | |
8484 | start_cleanup_fn () | |
8485 | { | |
8486 | static int counter = 0; | |
8487 | int old_interface_unknown = interface_unknown; | |
8488 | char name[32]; | |
8489 | tree parmtypes; | |
8490 | tree fntype; | |
8491 | tree fndecl; | |
8492 | ||
8493 | push_to_top_level (); | |
8494 | ||
8495 | /* No need to mangle this. */ | |
8496 | push_lang_context (lang_name_c); | |
8497 | ||
8498 | interface_unknown = 1; | |
8499 | ||
8500 | /* Build the parameter-types. */ | |
8501 | parmtypes = void_list_node; | |
8502 | /* Functions passed to __cxa_atexit take an additional parameter. | |
8503 | We'll just ignore it. After we implement the new calling | |
8504 | convention for destructors, we can eliminate the use of | |
8505 | additional cleanup functions entirely in the -fnew-abi case. */ | |
8506 | if (flag_use_cxa_atexit) | |
8507 | parmtypes = tree_cons (NULL_TREE, ptr_type_node, parmtypes); | |
8508 | /* Build the function type itself. */ | |
8509 | fntype = build_function_type (void_type_node, parmtypes); | |
8510 | /* Build the name of the function. */ | |
8511 | sprintf (name, "__tcf_%d", counter++); | |
8512 | /* Build the function declaration. */ | |
8513 | fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype); | |
8514 | /* It's a function with internal linkage, generated by the | |
8515 | compiler. */ | |
8516 | TREE_PUBLIC (fndecl) = 0; | |
8517 | DECL_ARTIFICIAL (fndecl) = 1; | |
5a728aca MM |
8518 | /* Make the function `inline' so that it is only emitted if it is |
8519 | actually needed. It is unlikely that it will be inlined, since | |
aba649ba | 8520 | it is only called via a function pointer, but we avoid unnecessary |
5a728aca MM |
8521 | emissions this way. */ |
8522 | DECL_INLINE (fndecl) = 1; | |
db4283a0 MM |
8523 | /* Build the parameter. */ |
8524 | if (flag_use_cxa_atexit) | |
8525 | { | |
8526 | tree parmdecl; | |
8527 | ||
8528 | parmdecl = build_decl (PARM_DECL, NULL_TREE, ptr_type_node); | |
8529 | DECL_CONTEXT (parmdecl) = fndecl; | |
8530 | DECL_ARG_TYPE (parmdecl) = ptr_type_node; | |
8531 | TREE_USED (parmdecl) = 1; | |
8532 | DECL_ARGUMENTS (fndecl) = parmdecl; | |
8533 | } | |
8534 | ||
09ed39ad | 8535 | pushdecl (fndecl); |
db4283a0 | 8536 | start_function (/*specs=*/NULL_TREE, fndecl, NULL_TREE, SF_PRE_PARSED); |
db4283a0 MM |
8537 | |
8538 | interface_unknown = old_interface_unknown; | |
8539 | ||
8540 | pop_lang_context (); | |
8541 | ||
8542 | return current_function_decl; | |
8543 | } | |
8544 | ||
8545 | /* Finish the cleanup function begun by start_cleanup_fn. */ | |
8546 | ||
8547 | static void | |
8548 | end_cleanup_fn () | |
8549 | { | |
0acf7199 | 8550 | expand_body (finish_function (0)); |
db4283a0 MM |
8551 | |
8552 | pop_from_top_level (); | |
8553 | } | |
8554 | ||
bf419747 MM |
8555 | /* Generate code to handle the destruction of DECL, an object with |
8556 | static storage duration. */ | |
f0105ed3 | 8557 | |
bf419747 MM |
8558 | void |
8559 | register_dtor_fn (decl) | |
f0105ed3 MM |
8560 | tree decl; |
8561 | { | |
db4283a0 | 8562 | tree cleanup; |
f0105ed3 | 8563 | tree compound_stmt; |
db4283a0 MM |
8564 | tree args; |
8565 | tree fcall; | |
f0105ed3 | 8566 | |
db4283a0 | 8567 | int saved_flag_access_control; |
f0105ed3 | 8568 | |
834c6dff | 8569 | if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl))) |
bf419747 MM |
8570 | return; |
8571 | ||
f0105ed3 MM |
8572 | /* Call build_cleanup before we enter the anonymous function so that |
8573 | any access checks will be done relative to the current scope, | |
8574 | rather than the scope of the anonymous function. */ | |
8575 | build_cleanup (decl); | |
8576 | ||
8577 | /* Now start the function. */ | |
db4283a0 | 8578 | cleanup = start_cleanup_fn (); |
f0105ed3 MM |
8579 | |
8580 | /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer | |
8581 | to the original function, rather than the anonymous one. That | |
8582 | will make the back-end think that nested functions are in use, | |
8583 | which causes confusion. */ | |
8584 | saved_flag_access_control = flag_access_control; | |
8585 | flag_access_control = 0; | |
8586 | fcall = build_cleanup (decl); | |
8587 | flag_access_control = saved_flag_access_control; | |
8588 | ||
8589 | /* Create the body of the anonymous function. */ | |
8590 | compound_stmt = begin_compound_stmt (/*has_no_scope=*/0); | |
8591 | finish_expr_stmt (fcall); | |
8592 | finish_compound_stmt (/*has_no_scope=*/0, compound_stmt); | |
db4283a0 | 8593 | end_cleanup_fn (); |
f0105ed3 MM |
8594 | |
8595 | /* Call atexit with the cleanup function. */ | |
dffd7eb6 | 8596 | cxx_mark_addressable (cleanup); |
f0105ed3 | 8597 | cleanup = build_unary_op (ADDR_EXPR, cleanup, 0); |
db4283a0 MM |
8598 | if (flag_use_cxa_atexit) |
8599 | { | |
0f268005 MM |
8600 | args = tree_cons (NULL_TREE, |
8601 | build_unary_op (ADDR_EXPR, get_dso_handle_node (), 0), | |
8602 | NULL_TREE); | |
db4283a0 MM |
8603 | args = tree_cons (NULL_TREE, null_pointer_node, args); |
8604 | args = tree_cons (NULL_TREE, cleanup, args); | |
8605 | } | |
8606 | else | |
8607 | args = tree_cons (NULL_TREE, cleanup, NULL_TREE); | |
8608 | finish_expr_stmt (build_function_call (get_atexit_node (), args)); | |
f0105ed3 MM |
8609 | } |
8610 | ||
8d08fdba MS |
8611 | void |
8612 | expand_static_init (decl, init) | |
8613 | tree decl; | |
8614 | tree init; | |
8615 | { | |
8616 | tree oldstatic = value_member (decl, static_aggregates); | |
a4443a08 | 8617 | |
8d08fdba MS |
8618 | if (oldstatic) |
8619 | { | |
8620 | if (TREE_PURPOSE (oldstatic) && init != NULL_TREE) | |
33bd39a2 | 8621 | error ("multiple initializations given for `%D'", decl); |
8d08fdba | 8622 | } |
0aafb128 | 8623 | else if (! toplevel_bindings_p ()) |
8d08fdba MS |
8624 | { |
8625 | /* Emit code to perform this initialization but once. */ | |
b7b8bcd2 | 8626 | tree if_stmt; |
f0105ed3 | 8627 | tree then_clause; |
f1dedc31 | 8628 | tree assignment; |
c395453c MM |
8629 | tree guard; |
8630 | tree guard_init; | |
8d08fdba | 8631 | |
2036a15c MM |
8632 | /* Emit code to perform this initialization but once. This code |
8633 | looks like: | |
8634 | ||
c395453c MM |
8635 | static int guard = 0; |
8636 | if (!guard) { | |
2036a15c | 8637 | // Do initialization. |
c395453c | 8638 | guard = 1; |
2036a15c MM |
8639 | // Register variable for destruction at end of program. |
8640 | } | |
8641 | ||
8642 | Note that the `temp' variable is only set to 1 *after* the | |
8643 | initialization is complete. This ensures that an exception, | |
8644 | thrown during the construction, will cause the variable to | |
8645 | reinitialized when we pass through this code again, as per: | |
68642fb6 | 8646 | |
2036a15c MM |
8647 | [stmt.dcl] |
8648 | ||
8649 | If the initialization exits by throwing an exception, the | |
8650 | initialization is not complete, so it will be tried again | |
8651 | the next time control enters the declaration. | |
8652 | ||
8653 | In theory, this process should be thread-safe, too; multiple | |
8654 | threads should not be able to initialize the variable more | |
8655 | than once. We don't yet attempt to ensure thread-safety. */ | |
c395453c MM |
8656 | |
8657 | /* Create the guard variable. */ | |
8658 | guard = get_guard (decl); | |
2036a15c MM |
8659 | |
8660 | /* Begin the conditional initialization. */ | |
b7b8bcd2 | 8661 | if_stmt = begin_if_stmt (); |
c395453c | 8662 | finish_if_stmt_cond (get_guard_cond (guard), if_stmt); |
f0105ed3 | 8663 | then_clause = begin_compound_stmt (/*has_no_scope=*/0); |
72b7eeff | 8664 | |
2036a15c | 8665 | /* Do the initialization itself. */ |
28cbf42c | 8666 | if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)) |
f30432d7 | 8667 | || (init && TREE_CODE (init) == TREE_LIST)) |
f1dedc31 | 8668 | assignment = build_aggr_init (decl, init, 0); |
f30432d7 | 8669 | else if (init) |
c557501d MM |
8670 | /* The initialization we're doing here is just a bitwise |
8671 | copy. */ | |
8672 | assignment = build (INIT_EXPR, TREE_TYPE (decl), decl, init); | |
f1dedc31 MM |
8673 | else |
8674 | assignment = NULL_TREE; | |
8675 | ||
8676 | /* Once the assignment is complete, set TEMP to 1. Since the | |
8677 | construction of the static object is complete at this point, | |
8678 | we want to make sure TEMP is set to 1 even if a temporary | |
8679 | constructed during the initialization throws an exception | |
8680 | when it is destroyed. So, we combine the initialization and | |
8681 | the assignment to TEMP into a single expression, ensuring | |
8682 | that when we call finish_expr_stmt the cleanups will not be | |
8683 | run until after TEMP is set to 1. */ | |
c395453c | 8684 | guard_init = set_guard (guard); |
f1dedc31 MM |
8685 | if (assignment) |
8686 | { | |
8687 | assignment = tree_cons (NULL_TREE, assignment, | |
68642fb6 | 8688 | build_tree_list (NULL_TREE, |
c395453c | 8689 | guard_init)); |
f1dedc31 MM |
8690 | assignment = build_compound_expr (assignment); |
8691 | } | |
8692 | else | |
c395453c | 8693 | assignment = guard_init; |
f1dedc31 | 8694 | finish_expr_stmt (assignment); |
72b7eeff | 8695 | |
2036a15c MM |
8696 | /* Use atexit to register a function for destroying this static |
8697 | variable. */ | |
bf419747 | 8698 | register_dtor_fn (decl); |
72b7eeff | 8699 | |
f0105ed3 | 8700 | finish_compound_stmt (/*has_no_scope=*/0, then_clause); |
b7b8bcd2 MM |
8701 | finish_then_clause (if_stmt); |
8702 | finish_if_stmt (); | |
8d08fdba MS |
8703 | } |
8704 | else | |
bbd15aac | 8705 | static_aggregates = tree_cons (init, decl, static_aggregates); |
8d08fdba | 8706 | } |
3c5c0849 MM |
8707 | |
8708 | /* Finish the declaration of a catch-parameter. */ | |
8709 | ||
b35d4555 | 8710 | tree |
3c5c0849 MM |
8711 | start_handler_parms (declspecs, declarator) |
8712 | tree declspecs; | |
8713 | tree declarator; | |
8714 | { | |
8715 | tree decl; | |
8716 | if (declspecs) | |
8717 | { | |
8718 | decl = grokdeclarator (declarator, declspecs, CATCHPARM, | |
91d231cb | 8719 | 1, NULL); |
3c5c0849 MM |
8720 | if (decl == NULL_TREE) |
8721 | error ("invalid catch parameter"); | |
8722 | } | |
8723 | else | |
8724 | decl = NULL_TREE; | |
b35d4555 MM |
8725 | |
8726 | return decl; | |
3c5c0849 MM |
8727 | } |
8728 | ||
8d08fdba MS |
8729 | \f |
8730 | /* Make TYPE a complete type based on INITIAL_VALUE. | |
8731 | Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered, | |
6ab5c740 | 8732 | 2 if there was no information (in which case assume 0 if DO_DEFAULT). */ |
8d08fdba MS |
8733 | |
8734 | int | |
8735 | complete_array_type (type, initial_value, do_default) | |
8736 | tree type, initial_value; | |
8737 | int do_default; | |
8738 | { | |
8739 | register tree maxindex = NULL_TREE; | |
8740 | int value = 0; | |
68642fb6 | 8741 | |
8d08fdba MS |
8742 | if (initial_value) |
8743 | { | |
7b019c19 MM |
8744 | /* An array of character type can be initialized from a |
8745 | brace-enclosed string constant. */ | |
8746 | if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))) | |
8747 | && TREE_CODE (initial_value) == CONSTRUCTOR | |
8748 | && CONSTRUCTOR_ELTS (initial_value) | |
8749 | && (TREE_CODE (TREE_VALUE (CONSTRUCTOR_ELTS (initial_value))) | |
8750 | == STRING_CST) | |
8751 | && TREE_CHAIN (CONSTRUCTOR_ELTS (initial_value)) == NULL_TREE) | |
8752 | initial_value = TREE_VALUE (CONSTRUCTOR_ELTS (initial_value)); | |
8753 | ||
8754 | /* Note MAXINDEX is really the maximum index, one less than the | |
8755 | size. */ | |
8d08fdba | 8756 | if (TREE_CODE (initial_value) == STRING_CST) |
e1cd6e56 MS |
8757 | { |
8758 | int eltsize | |
8759 | = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value))); | |
8760 | maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value) | |
8761 | / eltsize) - 1, 0); | |
8762 | } | |
8d08fdba MS |
8763 | else if (TREE_CODE (initial_value) == CONSTRUCTOR) |
8764 | { | |
e1cd6e56 | 8765 | tree elts = CONSTRUCTOR_ELTS (initial_value); |
fed3cef0 RK |
8766 | |
8767 | maxindex = ssize_int (-1); | |
e1cd6e56 MS |
8768 | for (; elts; elts = TREE_CHAIN (elts)) |
8769 | { | |
8770 | if (TREE_PURPOSE (elts)) | |
8771 | maxindex = TREE_PURPOSE (elts); | |
8772 | else | |
fed3cef0 | 8773 | maxindex = size_binop (PLUS_EXPR, maxindex, ssize_int (1)); |
e1cd6e56 MS |
8774 | } |
8775 | maxindex = copy_node (maxindex); | |
8d08fdba MS |
8776 | } |
8777 | else | |
8778 | { | |
8779 | /* Make an error message unless that happened already. */ | |
8780 | if (initial_value != error_mark_node) | |
8781 | value = 1; | |
0db982be ML |
8782 | else |
8783 | initial_value = NULL_TREE; | |
8d08fdba MS |
8784 | |
8785 | /* Prevent further error messages. */ | |
8786 | maxindex = build_int_2 (0, 0); | |
8787 | } | |
8788 | } | |
8789 | ||
8790 | if (!maxindex) | |
8791 | { | |
8792 | if (do_default) | |
8793 | maxindex = build_int_2 (0, 0); | |
8794 | value = 2; | |
8795 | } | |
8796 | ||
8797 | if (maxindex) | |
8798 | { | |
51c184be | 8799 | tree itype; |
6ab5c740 NS |
8800 | tree domain; |
8801 | ||
8802 | domain = build_index_type (maxindex); | |
8803 | TYPE_DOMAIN (type) = domain; | |
51c184be | 8804 | |
dff6b454 | 8805 | if (! TREE_TYPE (maxindex)) |
6ab5c740 | 8806 | TREE_TYPE (maxindex) = domain; |
51c184be MS |
8807 | if (initial_value) |
8808 | itype = TREE_TYPE (initial_value); | |
8809 | else | |
8810 | itype = NULL; | |
8811 | if (itype && !TYPE_DOMAIN (itype)) | |
6ab5c740 | 8812 | TYPE_DOMAIN (itype) = domain; |
dff6b454 RK |
8813 | /* The type of the main variant should never be used for arrays |
8814 | of different sizes. It should only ever be completed with the | |
8815 | size of the array. */ | |
8816 | if (! TYPE_DOMAIN (TYPE_MAIN_VARIANT (type))) | |
6ab5c740 | 8817 | TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)) = domain; |
8d08fdba MS |
8818 | } |
8819 | ||
8820 | /* Lay out the type now that we can get the real answer. */ | |
8821 | ||
8822 | layout_type (type); | |
8823 | ||
8824 | return value; | |
8825 | } | |
8826 | \f | |
8827 | /* Return zero if something is declared to be a member of type | |
8828 | CTYPE when in the context of CUR_TYPE. STRING is the error | |
8829 | message to print in that case. Otherwise, quietly return 1. */ | |
e92cc029 | 8830 | |
8d08fdba | 8831 | static int |
4dacf5bd | 8832 | member_function_or_else (ctype, cur_type, flags) |
8d08fdba | 8833 | tree ctype, cur_type; |
4dacf5bd | 8834 | enum overload_flags flags; |
8d08fdba MS |
8835 | { |
8836 | if (ctype && ctype != cur_type) | |
8837 | { | |
4dacf5bd | 8838 | if (flags == DTOR_FLAG) |
33bd39a2 | 8839 | error ("destructor for alien class `%T' cannot be a member", |
2ae7bada | 8840 | ctype); |
4dacf5bd | 8841 | else |
33bd39a2 | 8842 | error ("constructor for alien class `%T' cannot be a member", |
2ae7bada | 8843 | ctype); |
8d08fdba MS |
8844 | return 0; |
8845 | } | |
8846 | return 1; | |
8847 | } | |
8848 | \f | |
8849 | /* Subroutine of `grokdeclarator'. */ | |
8850 | ||
8851 | /* Generate errors possibly applicable for a given set of specifiers. | |
8852 | This is for ARM $7.1.2. */ | |
e92cc029 | 8853 | |
8d08fdba MS |
8854 | static void |
8855 | bad_specifiers (object, type, virtualp, quals, inlinep, friendp, raises) | |
8856 | tree object; | |
d8e178a0 | 8857 | const char *type; |
8d08fdba MS |
8858 | int virtualp, quals, friendp, raises, inlinep; |
8859 | { | |
8860 | if (virtualp) | |
33bd39a2 | 8861 | error ("`%D' declared as a `virtual' %s", object, type); |
8d08fdba | 8862 | if (inlinep) |
33bd39a2 | 8863 | error ("`%D' declared as an `inline' %s", object, type); |
8d08fdba | 8864 | if (quals) |
33bd39a2 | 8865 | error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration", |
a28e3c7f | 8866 | object, type); |
8d08fdba | 8867 | if (friendp) |
f8e55f34 | 8868 | cp_error_at ("`%D' declared as a friend", object); |
e1be26f4 RS |
8869 | if (raises |
8870 | && (TREE_CODE (object) == TYPE_DECL | |
8871 | || (!TYPE_PTRFN_P (TREE_TYPE (object)) | |
742a37d5 | 8872 | && !TYPE_REFFN_P (TREE_TYPE (object)) |
e1be26f4 | 8873 | && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object))))) |
f8e55f34 | 8874 | cp_error_at ("`%D' declared with an exception specification", object); |
8d08fdba MS |
8875 | } |
8876 | ||
8877 | /* CTYPE is class type, or null if non-class. | |
8878 | TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE | |
8879 | or METHOD_TYPE. | |
8880 | DECLARATOR is the function's name. | |
8881 | VIRTUALP is truthvalue of whether the function is virtual or not. | |
8882 | FLAGS are to be passed through to `grokclassfn'. | |
8883 | QUALS are qualifiers indicating whether the function is `const' | |
8884 | or `volatile'. | |
8885 | RAISES is a list of exceptions that this function can raise. | |
8886 | CHECK is 1 if we must find this method in CTYPE, 0 if we should | |
68642fb6 | 8887 | not look, and -1 if we should not call `grokclassfn' at all. |
3ddfb0e6 | 8888 | |
20496fa2 | 8889 | Returns `NULL_TREE' if something goes wrong, after issuing |
3ddfb0e6 | 8890 | applicable error messages. */ |
e92cc029 | 8891 | |
8d08fdba | 8892 | static tree |
386b8a85 | 8893 | grokfndecl (ctype, type, declarator, orig_declarator, virtualp, flags, quals, |
7a8f9fa9 | 8894 | raises, check, friendp, publicp, inlinep, funcdef_flag, |
2c73f9f5 | 8895 | template_count, in_namespace) |
8d08fdba MS |
8896 | tree ctype, type; |
8897 | tree declarator; | |
386b8a85 | 8898 | tree orig_declarator; |
8d08fdba MS |
8899 | int virtualp; |
8900 | enum overload_flags flags; | |
7a8f9fa9 | 8901 | tree quals, raises; |
386b8a85 | 8902 | int check, friendp, publicp, inlinep, funcdef_flag, template_count; |
2c73f9f5 | 8903 | tree in_namespace; |
8d08fdba | 8904 | { |
1951a1b6 | 8905 | tree decl; |
8d08fdba | 8906 | int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE; |
856216bb | 8907 | int has_default_arg = 0; |
42976354 | 8908 | tree t; |
8d08fdba | 8909 | |
8d08fdba | 8910 | if (raises) |
271e6f02 | 8911 | type = build_exception_variant (type, raises); |
c11b6f21 | 8912 | |
8d08fdba | 8913 | decl = build_lang_decl (FUNCTION_DECL, declarator, type); |
2c73f9f5 | 8914 | /* Propagate volatile out from type to decl. */ |
8d08fdba | 8915 | if (TYPE_VOLATILE (type)) |
893de33c | 8916 | TREE_THIS_VOLATILE (decl) = 1; |
8d08fdba | 8917 | |
79c4d4b7 | 8918 | /* If this decl has namespace scope, set that up. */ |
2c73f9f5 | 8919 | if (in_namespace) |
b262d64c | 8920 | set_decl_namespace (decl, in_namespace, friendp); |
adae082f | 8921 | else if (!ctype) |
79c4d4b7 | 8922 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); |
2c73f9f5 | 8923 | |
0f8766b8 JM |
8924 | /* `main' and builtins have implicit 'C' linkage. */ |
8925 | if ((MAIN_NAME_P (declarator) | |
8926 | || (IDENTIFIER_LENGTH (declarator) > 10 | |
8927 | && IDENTIFIER_POINTER (declarator)[0] == '_' | |
8928 | && IDENTIFIER_POINTER (declarator)[1] == '_' | |
8929 | && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0)) | |
8930 | && current_lang_name == lang_name_cplusplus | |
94706a5c | 8931 | && ctype == NULL_TREE |
79c4d4b7 JM |
8932 | /* NULL_TREE means global namespace. */ |
8933 | && DECL_CONTEXT (decl) == NULL_TREE) | |
5d2ed28c | 8934 | SET_DECL_LANGUAGE (decl, lang_c); |
0f8766b8 | 8935 | |
8d08fdba MS |
8936 | /* Should probably propagate const out from type to decl I bet (mrs). */ |
8937 | if (staticp) | |
8938 | { | |
8939 | DECL_STATIC_FUNCTION_P (decl) = 1; | |
8940 | DECL_CONTEXT (decl) = ctype; | |
8d08fdba MS |
8941 | } |
8942 | ||
e76a2646 | 8943 | if (ctype) |
4f1c5b7d | 8944 | DECL_CONTEXT (decl) = ctype; |
e76a2646 | 8945 | |
0f8766b8 | 8946 | if (ctype == NULL_TREE && DECL_MAIN_P (decl)) |
faae18ab | 8947 | { |
848b92e1 | 8948 | if (processing_template_decl) |
cb9a3ff8 | 8949 | error ("cannot declare `::main' to be a template"); |
faae18ab | 8950 | if (inlinep) |
cb9a3ff8 | 8951 | error ("cannot declare `::main' to be inline"); |
f22967f3 | 8952 | if (!publicp) |
cb9a3ff8 | 8953 | error ("cannot declare `::main' to be static"); |
f22967f3 MM |
8954 | if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)), |
8955 | integer_type_node)) | |
8956 | error ("`main' must return `int'"); | |
faae18ab MS |
8957 | inlinep = 0; |
8958 | publicp = 1; | |
8959 | } | |
50a6dbd7 | 8960 | |
59e76fc6 JM |
8961 | /* Members of anonymous types and local classes have no linkage; make |
8962 | them internal. */ | |
1951a1b6 JM |
8963 | /* FIXME what if it gets a name from typedef? */ |
8964 | if (ctype && (TYPE_ANONYMOUS_P (ctype) | |
4f1c5b7d | 8965 | || decl_function_context (TYPE_MAIN_DECL (ctype)))) |
50a6dbd7 JM |
8966 | publicp = 0; |
8967 | ||
8968 | if (publicp) | |
8969 | { | |
8970 | /* [basic.link]: A name with no linkage (notably, the name of a class | |
8971 | or enumeration declared in a local scope) shall not be used to | |
8972 | declare an entity with linkage. | |
8973 | ||
8974 | Only check this for public decls for now. */ | |
8975 | t = no_linkage_check (TREE_TYPE (decl)); | |
8976 | if (t) | |
8977 | { | |
1951a1b6 | 8978 | if (TYPE_ANONYMOUS_P (t)) |
7f7c930e | 8979 | { |
eb68cb58 | 8980 | if (DECL_EXTERN_C_P (decl)) |
7f7c930e JM |
8981 | /* Allow this; it's pretty common in C. */; |
8982 | else | |
1951a1b6 | 8983 | { |
33bd39a2 | 8984 | pedwarn ("non-local function `%#D' uses anonymous type", |
1951a1b6 JM |
8985 | decl); |
8986 | if (DECL_ORIGINAL_TYPE (TYPE_NAME (t))) | |
8987 | cp_pedwarn_at ("\ | |
8988 | `%#D' does not refer to the unqualified type, so it is not used for linkage", | |
8989 | TYPE_NAME (t)); | |
8990 | } | |
7f7c930e | 8991 | } |
50a6dbd7 | 8992 | else |
33bd39a2 | 8993 | pedwarn ("non-local function `%#D' uses local type `%T'", |
cce2be43 | 8994 | decl, t); |
50a6dbd7 JM |
8995 | } |
8996 | } | |
8997 | ||
893de33c | 8998 | TREE_PUBLIC (decl) = publicp; |
faae18ab | 8999 | if (! publicp) |
893de33c JM |
9000 | { |
9001 | DECL_INTERFACE_KNOWN (decl) = 1; | |
9002 | DECL_NOT_REALLY_EXTERN (decl) = 1; | |
9003 | } | |
faae18ab | 9004 | |
acc72c37 | 9005 | /* If the declaration was declared inline, mark it as such. */ |
faae18ab | 9006 | if (inlinep) |
acc72c37 MM |
9007 | DECL_DECLARED_INLINE_P (decl) = 1; |
9008 | /* We inline functions that are explicitly declared inline, or, when | |
9009 | the user explicitly asks us to, all functions. */ | |
9010 | if (DECL_DECLARED_INLINE_P (decl) || flag_inline_trees == 2) | |
9011 | DECL_INLINE (decl) = 1; | |
8d08fdba MS |
9012 | |
9013 | DECL_EXTERNAL (decl) = 1; | |
9014 | if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE) | |
9015 | { | |
33bd39a2 | 9016 | error ("%smember function `%D' cannot have `%T' method qualifier", |
8d08fdba MS |
9017 | (ctype ? "static " : "non-"), decl, TREE_VALUE (quals)); |
9018 | quals = NULL_TREE; | |
9019 | } | |
9020 | ||
596ea4e5 | 9021 | if (IDENTIFIER_OPNAME_P (DECL_NAME (decl))) |
271e6f02 | 9022 | grok_op_properties (decl, friendp); |
8d08fdba | 9023 | |
4f1c5b7d | 9024 | if (ctype && decl_function_context (decl)) |
893de33c | 9025 | DECL_NO_STATIC_CHAIN (decl) = 1; |
e76a2646 | 9026 | |
42976354 BK |
9027 | for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t)) |
9028 | if (TREE_PURPOSE (t) | |
9029 | && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG) | |
9030 | { | |
856216bb | 9031 | has_default_arg = 1; |
42976354 BK |
9032 | break; |
9033 | } | |
9034 | ||
f9d94ea4 JM |
9035 | if (friendp |
9036 | && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR) | |
9037 | { | |
9038 | if (funcdef_flag) | |
33bd39a2 | 9039 | error |
8251199e | 9040 | ("defining explicit specialization `%D' in friend declaration", |
f9d94ea4 JM |
9041 | orig_declarator); |
9042 | else | |
9043 | { | |
76e57b45 NS |
9044 | tree fns = TREE_OPERAND (orig_declarator, 0); |
9045 | tree args = TREE_OPERAND (orig_declarator, 1); | |
d363e7bf | 9046 | |
7e2421f7 MM |
9047 | if (PROCESSING_REAL_TEMPLATE_DECL_P ()) |
9048 | { | |
9049 | /* Something like `template <class T> friend void f<T>()'. */ | |
33bd39a2 | 9050 | error ("invalid use of template-id `%D' in declaration of primary template", |
7e2421f7 | 9051 | orig_declarator); |
20496fa2 | 9052 | return NULL_TREE; |
7e2421f7 MM |
9053 | } |
9054 | ||
856216bb | 9055 | |
f9d94ea4 JM |
9056 | /* A friend declaration of the form friend void f<>(). Record |
9057 | the information in the TEMPLATE_ID_EXPR. */ | |
9058 | SET_DECL_IMPLICIT_INSTANTIATION (decl); | |
76e57b45 NS |
9059 | |
9060 | if (TREE_CODE (fns) == COMPONENT_REF) | |
9061 | { | |
9062 | /* Due to bison parser ickiness, we will have already looked | |
9063 | up an operator_name or PFUNCNAME within the current class | |
9064 | (see template_id in parse.y). If the current class contains | |
9065 | such a name, we'll get a COMPONENT_REF here. Undo that. */ | |
d363e7bf | 9066 | |
76e57b45 NS |
9067 | my_friendly_assert (TREE_TYPE (TREE_OPERAND (fns, 0)) |
9068 | == current_class_type, 20001120); | |
9069 | fns = TREE_OPERAND (fns, 1); | |
9070 | } | |
9071 | my_friendly_assert (TREE_CODE (fns) == IDENTIFIER_NODE | |
9072 | || TREE_CODE (fns) == LOOKUP_EXPR | |
9073 | || TREE_CODE (fns) == OVERLOAD, 20001120); | |
9074 | DECL_TEMPLATE_INFO (decl) = tree_cons (fns, args, NULL_TREE); | |
856216bb MM |
9075 | |
9076 | if (has_default_arg) | |
9077 | { | |
33bd39a2 | 9078 | error ("default arguments are not allowed in declaration of friend template specialization `%D'", |
856216bb MM |
9079 | decl); |
9080 | return NULL_TREE; | |
9081 | } | |
9082 | ||
9083 | if (inlinep) | |
9084 | { | |
33bd39a2 | 9085 | error ("`inline' is not allowed in declaration of friend template specialization `%D'", |
856216bb MM |
9086 | decl); |
9087 | return NULL_TREE; | |
9088 | } | |
f9d94ea4 | 9089 | } |
f84b4be9 | 9090 | } |
386b8a85 | 9091 | |
856216bb MM |
9092 | if (has_default_arg) |
9093 | add_defarg_fn (decl); | |
9094 | ||
1eb0072d JM |
9095 | if (funcdef_flag) |
9096 | /* Make the init_value nonzero so pushdecl knows this is not | |
9097 | tentative. error_mark_node is replaced later with the BLOCK. */ | |
9098 | DECL_INITIAL (decl) = error_mark_node; | |
9099 | ||
93ca4ba7 | 9100 | if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl)) |
1660cb3a JM |
9101 | TREE_NOTHROW (decl) = 1; |
9102 | ||
75650646 | 9103 | /* Caller will do the rest of this. */ |
8d08fdba MS |
9104 | if (check < 0) |
9105 | return decl; | |
9106 | ||
1951a1b6 | 9107 | if (flags == NO_SPECIAL && ctype && constructor_name (ctype) == declarator) |
74b846e0 MM |
9108 | DECL_CONSTRUCTOR_P (decl) = 1; |
9109 | ||
9110 | /* Function gets the ugly name, field gets the nice one. This call | |
9111 | may change the type of the function (because of default | |
9112 | parameters)! */ | |
9113 | if (ctype != NULL_TREE) | |
9114 | grokclassfn (ctype, decl, flags, quals); | |
9115 | ||
9116 | decl = check_explicit_specialization (orig_declarator, decl, | |
9117 | template_count, | |
9118 | 2 * (funcdef_flag != 0) + | |
9119 | 4 * (friendp != 0)); | |
9120 | if (decl == error_mark_node) | |
9121 | return NULL_TREE; | |
98c1c668 | 9122 | |
74b846e0 MM |
9123 | if (ctype != NULL_TREE |
9124 | && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl)) | |
9125 | && check) | |
8d08fdba | 9126 | { |
74b846e0 | 9127 | tree old_decl; |
8d08fdba | 9128 | |
74b846e0 | 9129 | old_decl = check_classfn (ctype, decl); |
8d08fdba | 9130 | |
74b846e0 MM |
9131 | if (old_decl && TREE_CODE (old_decl) == TEMPLATE_DECL) |
9132 | /* Because grokfndecl is always supposed to return a | |
9133 | FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT | |
9134 | here. We depend on our callers to figure out that its | |
9135 | really a template that's being returned. */ | |
9136 | old_decl = DECL_TEMPLATE_RESULT (old_decl); | |
6c30752f | 9137 | |
74b846e0 MM |
9138 | if (old_decl && DECL_STATIC_FUNCTION_P (old_decl) |
9139 | && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE) | |
5566b478 | 9140 | { |
74b846e0 MM |
9141 | /* Remove the `this' parm added by grokclassfn. |
9142 | XXX Isn't this done in start_function, too? */ | |
3afb32a4 | 9143 | revert_static_member_fn (decl); |
74b846e0 | 9144 | last_function_parms = TREE_CHAIN (last_function_parms); |
5566b478 | 9145 | } |
74b846e0 | 9146 | if (old_decl && DECL_ARTIFICIAL (old_decl)) |
33bd39a2 | 9147 | error ("definition of implicitly-declared `%D'", old_decl); |
8d08fdba | 9148 | |
74b846e0 | 9149 | if (old_decl) |
8d08fdba | 9150 | { |
74b846e0 MM |
9151 | /* Since we've smashed OLD_DECL to its |
9152 | DECL_TEMPLATE_RESULT, we must do the same to DECL. */ | |
9153 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
9154 | decl = DECL_TEMPLATE_RESULT (decl); | |
9155 | ||
9156 | /* Attempt to merge the declarations. This can fail, in | |
9157 | the case of some illegal specialization declarations. */ | |
9158 | if (!duplicate_decls (decl, old_decl)) | |
33bd39a2 | 9159 | error ("no `%#D' member function declared in class `%T'", |
74b846e0 MM |
9160 | decl, ctype); |
9161 | return old_decl; | |
8d08fdba MS |
9162 | } |
9163 | } | |
74b846e0 MM |
9164 | |
9165 | if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl)) | |
9166 | return NULL_TREE; | |
9167 | ||
9168 | if (ctype == NULL_TREE || check) | |
9169 | return decl; | |
9170 | ||
9171 | if (virtualp) | |
cbb40945 | 9172 | DECL_VIRTUAL_P (decl) = 1; |
74b846e0 | 9173 | |
8d08fdba MS |
9174 | return decl; |
9175 | } | |
9176 | ||
9177 | static tree | |
2c73f9f5 | 9178 | grokvardecl (type, declarator, specbits_in, initialized, constp, in_namespace) |
8d08fdba MS |
9179 | tree type; |
9180 | tree declarator; | |
d2e5ee5c | 9181 | RID_BIT_TYPE *specbits_in; |
8d08fdba | 9182 | int initialized; |
a9aedbc2 | 9183 | int constp; |
2c73f9f5 | 9184 | tree in_namespace; |
8d08fdba MS |
9185 | { |
9186 | tree decl; | |
f7da6097 MS |
9187 | RID_BIT_TYPE specbits; |
9188 | ||
9189 | specbits = *specbits_in; | |
8d08fdba MS |
9190 | |
9191 | if (TREE_CODE (type) == OFFSET_TYPE) | |
9192 | { | |
9193 | /* If you declare a static member so that it | |
9194 | can be initialized, the code will reach here. */ | |
5b605f68 MS |
9195 | tree basetype = TYPE_OFFSET_BASETYPE (type); |
9196 | type = TREE_TYPE (type); | |
4ce3d537 | 9197 | decl = build_lang_decl (VAR_DECL, declarator, type); |
5b605f68 | 9198 | DECL_CONTEXT (decl) = basetype; |
8d08fdba MS |
9199 | } |
9200 | else | |
30394414 | 9201 | { |
79c4d4b7 JM |
9202 | tree context; |
9203 | ||
9204 | if (in_namespace) | |
9205 | context = in_namespace; | |
9206 | else if (namespace_bindings_p () || RIDBIT_SETP (RID_EXTERN, specbits)) | |
9207 | context = current_namespace; | |
820fcad8 JM |
9208 | else |
9209 | context = NULL_TREE; | |
79c4d4b7 | 9210 | |
5d2ed28c MM |
9211 | /* For namespace-scope variables, declared in a template, we |
9212 | need the full lang_decl. The same is true for | |
9213 | namespace-scope variables that do not have C++ language | |
9214 | linkage. */ | |
9215 | if (context | |
9216 | && (processing_template_decl | |
9217 | || current_lang_name != lang_name_cplusplus)) | |
cd9f6678 | 9218 | decl = build_lang_decl (VAR_DECL, declarator, type); |
9188c363 | 9219 | else |
c82dbd95 | 9220 | decl = build_decl (VAR_DECL, declarator, type); |
79c4d4b7 JM |
9221 | |
9222 | if (context) | |
b262d64c | 9223 | set_decl_namespace (decl, context, 0); |
79c4d4b7 JM |
9224 | |
9225 | context = DECL_CONTEXT (decl); | |
5362b086 | 9226 | if (declarator && context && current_lang_name != lang_name_c) |
92643fea MM |
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); | |
30394414 | 9231 | } |
6060a796 | 9232 | |
2c73f9f5 | 9233 | if (in_namespace) |
b262d64c | 9234 | set_decl_namespace (decl, in_namespace, 0); |
2c73f9f5 | 9235 | |
8d08fdba MS |
9236 | if (RIDBIT_SETP (RID_EXTERN, specbits)) |
9237 | { | |
9238 | DECL_THIS_EXTERN (decl) = 1; | |
9239 | DECL_EXTERNAL (decl) = !initialized; | |
9240 | } | |
9241 | ||
9242 | /* In class context, static means one per class, | |
9243 | public access, and static storage. */ | |
2b9dc906 | 9244 | if (DECL_CLASS_SCOPE_P (decl)) |
8d08fdba MS |
9245 | { |
9246 | TREE_PUBLIC (decl) = 1; | |
9247 | TREE_STATIC (decl) = 1; | |
5b605f68 | 9248 | DECL_EXTERNAL (decl) = 0; |
8d08fdba MS |
9249 | } |
9250 | /* At top level, either `static' or no s.c. makes a definition | |
9251 | (perhaps tentative), and absence of `static' makes it public. */ | |
a9aedbc2 | 9252 | else if (toplevel_bindings_p ()) |
8d08fdba | 9253 | { |
a9aedbc2 | 9254 | TREE_PUBLIC (decl) = (RIDBIT_NOTSETP (RID_STATIC, specbits) |
9fffd093 | 9255 | && (DECL_THIS_EXTERN (decl) || ! constp)); |
8d08fdba MS |
9256 | TREE_STATIC (decl) = ! DECL_EXTERNAL (decl); |
9257 | } | |
9258 | /* Not at top level, only `static' makes a static definition. */ | |
9259 | else | |
9260 | { | |
9261 | TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits); | |
9262 | TREE_PUBLIC (decl) = DECL_EXTERNAL (decl); | |
9263 | } | |
50a6dbd7 JM |
9264 | |
9265 | if (TREE_PUBLIC (decl)) | |
9266 | { | |
9267 | /* [basic.link]: A name with no linkage (notably, the name of a class | |
9268 | or enumeration declared in a local scope) shall not be used to | |
9269 | declare an entity with linkage. | |
9270 | ||
9271 | Only check this for public decls for now. */ | |
9272 | tree t = no_linkage_check (TREE_TYPE (decl)); | |
9273 | if (t) | |
9274 | { | |
1951a1b6 | 9275 | if (TYPE_ANONYMOUS_P (t)) |
50a6dbd7 JM |
9276 | /* Ignore for now; `enum { foo } e' is pretty common. */; |
9277 | else | |
33bd39a2 | 9278 | pedwarn ("non-local variable `%#D' uses local type `%T'", |
50a6dbd7 JM |
9279 | decl, t); |
9280 | } | |
9281 | } | |
9282 | ||
8d08fdba MS |
9283 | return decl; |
9284 | } | |
9285 | ||
d8f8dca1 MM |
9286 | /* Create and return a canonical pointer to member function type, for |
9287 | TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */ | |
8d08fdba MS |
9288 | |
9289 | tree | |
9290 | build_ptrmemfunc_type (type) | |
9291 | tree type; | |
9292 | { | |
9293 | tree fields[4]; | |
9294 | tree t; | |
46cbda4a | 9295 | tree unqualified_variant = NULL_TREE; |
8d08fdba | 9296 | |
d48ebde1 NS |
9297 | if (type == error_mark_node) |
9298 | return type; | |
d363e7bf | 9299 | |
8d08fdba MS |
9300 | /* If a canonical type already exists for this type, use it. We use |
9301 | this method instead of type_hash_canon, because it only does a | |
9302 | simple equality check on the list of field members. */ | |
9303 | ||
9304 | if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type))) | |
9305 | return t; | |
9306 | ||
46cbda4a MM |
9307 | /* Make sure that we always have the unqualified pointer-to-member |
9308 | type first. */ | |
89d684bb | 9309 | if (cp_type_quals (type) != TYPE_UNQUALIFIED) |
68642fb6 | 9310 | unqualified_variant |
46cbda4a MM |
9311 | = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type)); |
9312 | ||
33848bb0 | 9313 | t = make_aggr_type (RECORD_TYPE); |
2c73f9f5 | 9314 | /* Let the front-end know this is a pointer to member function... */ |
db5ae43f | 9315 | TYPE_PTRMEMFUNC_FLAG (t) = 1; |
2c73f9f5 | 9316 | /* ... and not really an aggregate. */ |
7ddedda4 | 9317 | SET_IS_AGGR_TYPE (t, 0); |
8d08fdba | 9318 | |
1f84ec23 MM |
9319 | fields[0] = build_decl (FIELD_DECL, pfn_identifier, type); |
9320 | fields[1] = build_decl (FIELD_DECL, delta_identifier, | |
9321 | delta_type_node); | |
18ae7f63 | 9322 | finish_builtin_type (t, "__ptrmemfunc_type", fields, 1, ptr_type_node); |
8d08fdba | 9323 | |
8d08fdba MS |
9324 | /* Zap out the name so that the back-end will give us the debugging |
9325 | information for this anonymous RECORD_TYPE. */ | |
9326 | TYPE_NAME (t) = NULL_TREE; | |
9327 | ||
46cbda4a MM |
9328 | /* If this is not the unqualified form of this pointer-to-member |
9329 | type, set the TYPE_MAIN_VARIANT for this type to be the | |
9330 | unqualified type. Since they are actually RECORD_TYPEs that are | |
9331 | not variants of each other, we must do this manually. */ | |
89d684bb | 9332 | if (cp_type_quals (type) != TYPE_UNQUALIFIED) |
46cbda4a | 9333 | { |
89d684bb | 9334 | t = build_qualified_type (t, cp_type_quals (type)); |
46cbda4a MM |
9335 | TYPE_MAIN_VARIANT (t) = unqualified_variant; |
9336 | TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant); | |
9337 | TYPE_NEXT_VARIANT (unqualified_variant) = t; | |
9338 | } | |
9339 | ||
9340 | /* Cache this pointer-to-member type so that we can find it again | |
9341 | later. */ | |
8d08fdba MS |
9342 | TYPE_SET_PTRMEMFUNC_TYPE (type, t); |
9343 | ||
e92cc029 | 9344 | /* Seems to be wanted. */ |
8d08fdba | 9345 | CLASSTYPE_GOT_SEMICOLON (t) = 1; |
46cbda4a | 9346 | |
8d08fdba MS |
9347 | return t; |
9348 | } | |
9349 | ||
b17e2870 JM |
9350 | /* DECL is a VAR_DECL defined in-class, whose TYPE is also given. |
9351 | Check to see that the definition is valid. Issue appropriate error | |
9352 | messages. Return 1 if the definition is particularly bad, or 0 | |
9353 | otherwise. */ | |
9354 | ||
9355 | int | |
9356 | check_static_variable_definition (decl, type) | |
9357 | tree decl; | |
9358 | tree type; | |
9359 | { | |
9360 | /* Motion 10 at San Diego: If a static const integral data member is | |
9361 | initialized with an integral constant expression, the initializer | |
9362 | may appear either in the declaration (within the class), or in | |
9363 | the definition, but not both. If it appears in the class, the | |
9364 | member is a member constant. The file-scope definition is always | |
9365 | required. */ | |
9366 | if (CLASS_TYPE_P (type) || TREE_CODE (type) == REFERENCE_TYPE) | |
9367 | { | |
33bd39a2 | 9368 | error ("invalid in-class initialization of static data member of non-integral type `%T'", |
b17e2870 JM |
9369 | type); |
9370 | /* If we just return the declaration, crashes will sometimes | |
9371 | occur. We therefore return void_type_node, as if this was a | |
9372 | friend declaration, to cause callers to completely ignore | |
9373 | this declaration. */ | |
9374 | return 1; | |
9375 | } | |
9376 | else if (!CP_TYPE_CONST_P (type)) | |
33bd39a2 | 9377 | error ("ISO C++ forbids in-class initialization of non-const static member `%D'", |
b17e2870 JM |
9378 | decl); |
9379 | else if (pedantic && !INTEGRAL_TYPE_P (type)) | |
33bd39a2 | 9380 | pedwarn ("ISO C++ forbids initialization of member constant `%D' of non-integral type `%T'", decl, type); |
b17e2870 JM |
9381 | |
9382 | return 0; | |
9383 | } | |
9384 | ||
2848ad0f MM |
9385 | /* Given the SIZE (i.e., number of elements) in an array, compute an |
9386 | appropriate index type for the array. If non-NULL, NAME is the | |
9387 | name of the thing being declared. */ | |
9388 | ||
c95cd22e | 9389 | tree |
2848ad0f MM |
9390 | compute_array_index_type (name, size) |
9391 | tree name; | |
9392 | tree size; | |
9393 | { | |
9394 | tree itype; | |
9395 | ||
2848ad0f MM |
9396 | /* If this involves a template parameter, it will be a constant at |
9397 | instantiation time, but we don't know what the value is yet. | |
9398 | Even if no template parameters are involved, we may an expression | |
9399 | that is not a constant; we don't even simplify `1 + 2' when | |
9400 | processing a template. */ | |
9401 | if (processing_template_decl) | |
9402 | { | |
9403 | /* Resolve a qualified reference to an enumerator or static | |
9404 | const data member of ours. */ | |
9405 | if (TREE_CODE (size) == SCOPE_REF | |
9406 | && TREE_OPERAND (size, 0) == current_class_type) | |
9407 | { | |
9408 | tree t = lookup_field (current_class_type, | |
9409 | TREE_OPERAND (size, 1), 0, 0); | |
9410 | if (t) | |
9411 | size = t; | |
9412 | } | |
9413 | ||
9414 | return build_index_type (build_min (MINUS_EXPR, sizetype, | |
9415 | size, integer_one_node)); | |
9416 | } | |
9417 | ||
80f5bb34 MM |
9418 | /* The size might be the result of a cast. */ |
9419 | STRIP_TYPE_NOPS (size); | |
9420 | ||
9421 | /* It might be a const variable or enumeration constant. */ | |
9422 | size = decl_constant_value (size); | |
9423 | ||
2848ad0f MM |
9424 | /* The array bound must be an integer type. */ |
9425 | if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE | |
9426 | && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE | |
9427 | && TREE_CODE (TREE_TYPE (size)) != BOOLEAN_TYPE) | |
9428 | { | |
d67cdbc3 | 9429 | if (name) |
33bd39a2 | 9430 | error ("size of array `%D' has non-integer type", name); |
d67cdbc3 | 9431 | else |
33bd39a2 | 9432 | error ("size of array has non-integer type"); |
2848ad0f MM |
9433 | size = integer_one_node; |
9434 | } | |
9435 | ||
9436 | /* Normally, the array-bound will be a constant. */ | |
2bb5d995 | 9437 | if (TREE_CODE (size) == INTEGER_CST) |
2848ad0f MM |
9438 | { |
9439 | /* Check to see if the array bound overflowed. Make that an | |
9440 | error, no matter how generous we're being. */ | |
9441 | int old_flag_pedantic_errors = flag_pedantic_errors; | |
9442 | int old_pedantic = pedantic; | |
9443 | pedantic = flag_pedantic_errors = 1; | |
9444 | constant_expression_warning (size); | |
9445 | pedantic = old_pedantic; | |
9446 | flag_pedantic_errors = old_flag_pedantic_errors; | |
9447 | ||
9448 | /* An array must have a positive number of elements. */ | |
9449 | if (INT_CST_LT (size, integer_zero_node)) | |
9450 | { | |
d67cdbc3 | 9451 | if (name) |
33bd39a2 | 9452 | error ("size of array `%D' is negative", name); |
d67cdbc3 | 9453 | else |
33bd39a2 | 9454 | error ("size of array is negative"); |
2848ad0f MM |
9455 | size = integer_one_node; |
9456 | } | |
9457 | /* Except that an extension we allow zero-sized arrays. We | |
68642fb6 | 9458 | always allow them in system headers because glibc uses |
2848ad0f MM |
9459 | them. */ |
9460 | else if (integer_zerop (size) && pedantic && !in_system_header) | |
d67cdbc3 JM |
9461 | { |
9462 | if (name) | |
33bd39a2 | 9463 | pedwarn ("ISO C++ forbids zero-size array `%D'", name); |
d67cdbc3 | 9464 | else |
33bd39a2 | 9465 | pedwarn ("ISO C++ forbids zero-size array"); |
d67cdbc3 | 9466 | } |
2848ad0f | 9467 | } |
2bb5d995 JM |
9468 | else if (TREE_CONSTANT (size)) |
9469 | { | |
9470 | /* `(int) &fn' is not a valid array bound. */ | |
9471 | if (name) | |
33bd39a2 | 9472 | error ("size of array `%D' is not an integral constant-expression", |
2bb5d995 JM |
9473 | name); |
9474 | else | |
33bd39a2 | 9475 | error ("size of array is not an integral constant-expression"); |
2bb5d995 | 9476 | } |
2848ad0f MM |
9477 | |
9478 | /* Compute the index of the largest element in the array. It is | |
9479 | one less than the number of elements in the array. */ | |
9480 | itype | |
ab76ca54 MM |
9481 | = fold (cp_build_binary_op (MINUS_EXPR, |
9482 | cp_convert (ssizetype, size), | |
9483 | cp_convert (ssizetype, | |
9484 | integer_one_node))); | |
68642fb6 | 9485 | |
2848ad0f MM |
9486 | /* Check for variable-sized arrays. We allow such things as an |
9487 | extension, even though they are not allowed in ANSI/ISO C++. */ | |
9488 | if (!TREE_CONSTANT (itype)) | |
9489 | { | |
9490 | if (pedantic) | |
9491 | { | |
9492 | if (name) | |
33bd39a2 | 9493 | pedwarn ("ISO C++ forbids variable-size array `%D'", |
2848ad0f MM |
9494 | name); |
9495 | else | |
33bd39a2 | 9496 | pedwarn ("ISO C++ forbids variable-size array"); |
2848ad0f MM |
9497 | } |
9498 | ||
9499 | /* Create a variable-sized array index type. */ | |
9500 | itype = variable_size (itype); | |
9501 | } | |
9502 | /* Make sure that there was no overflow when creating to a signed | |
9503 | index type. (For example, on a 32-bit machine, an array with | |
9504 | size 2^32 - 1 is too big.) */ | |
9505 | else if (TREE_OVERFLOW (itype)) | |
9506 | { | |
9507 | error ("overflow in array dimension"); | |
9508 | TREE_OVERFLOW (itype) = 0; | |
9509 | } | |
68642fb6 | 9510 | |
2848ad0f MM |
9511 | /* Create and return the appropriate index type. */ |
9512 | return build_index_type (itype); | |
9513 | } | |
9514 | ||
9515 | /* Returns an ARRAY_TYPE for an array with SIZE elements of the | |
9516 | indicated TYPE. If non-NULL, NAME is the NAME of the declaration | |
9517 | with this type. */ | |
9518 | ||
9519 | static tree | |
9520 | create_array_type_for_decl (name, type, size) | |
9521 | tree name; | |
9522 | tree type; | |
9523 | tree size; | |
9524 | { | |
9525 | tree itype = NULL_TREE; | |
9526 | const char* error_msg; | |
9527 | ||
9528 | /* If things have already gone awry, bail now. */ | |
9529 | if (type == error_mark_node || size == error_mark_node) | |
9530 | return error_mark_node; | |
9531 | ||
9532 | /* Assume that everything will go OK. */ | |
9533 | error_msg = NULL; | |
9534 | ||
9535 | /* There are some types which cannot be array elements. */ | |
9536 | switch (TREE_CODE (type)) | |
9537 | { | |
9538 | case VOID_TYPE: | |
9539 | error_msg = "array of void"; | |
9540 | break; | |
9541 | ||
9542 | case FUNCTION_TYPE: | |
9543 | error_msg = "array of functions"; | |
9544 | break; | |
9545 | ||
9546 | case REFERENCE_TYPE: | |
9547 | error_msg = "array of references"; | |
9548 | break; | |
9549 | ||
9550 | case OFFSET_TYPE: | |
9551 | error_msg = "array of data members"; | |
9552 | break; | |
9553 | ||
9554 | case METHOD_TYPE: | |
9555 | error_msg = "array of function members"; | |
9556 | break; | |
9557 | ||
9558 | default: | |
9559 | break; | |
9560 | } | |
9561 | ||
9562 | /* If something went wrong, issue an error-message and return. */ | |
9563 | if (error_msg) | |
9564 | { | |
9565 | if (name) | |
33bd39a2 | 9566 | error ("declaration of `%D' as %s", name, error_msg); |
2848ad0f | 9567 | else |
33bd39a2 | 9568 | error ("creating %s", error_msg); |
2848ad0f MM |
9569 | |
9570 | return error_mark_node; | |
9571 | } | |
9572 | ||
9573 | /* [dcl.array] | |
68642fb6 | 9574 | |
2848ad0f MM |
9575 | The constant expressions that specify the bounds of the arrays |
9576 | can be omitted only for the first member of the sequence. */ | |
9577 | if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)) | |
9578 | { | |
b3faacfd | 9579 | if (name) |
33bd39a2 | 9580 | error ("declaration of `%D' as multidimensional array must have bounds for all dimensions except the first", |
b3faacfd KL |
9581 | name); |
9582 | else | |
33bd39a2 | 9583 | error ("multidimensional array must have bounds for all dimensions except the first"); |
2848ad0f MM |
9584 | |
9585 | return error_mark_node; | |
9586 | } | |
9587 | ||
9588 | /* Figure out the index type for the array. */ | |
9589 | if (size) | |
9590 | itype = compute_array_index_type (name, size); | |
9591 | ||
9592 | return build_cplus_array_type (type, itype); | |
9593 | } | |
9594 | ||
3dbc07b6 MM |
9595 | /* Check that it's OK to declare a function with the indicated TYPE. |
9596 | SFK indicates the kind of special function (if any) that this | |
1f84ec23 | 9597 | function is. OPTYPE is the type given in a conversion operator |
3dbc07b6 MM |
9598 | declaration. Returns the actual return type of the function; that |
9599 | may be different than TYPE if an error occurs, or for certain | |
9600 | special functions. */ | |
9601 | ||
9602 | static tree | |
1f84ec23 | 9603 | check_special_function_return_type (sfk, type, optype) |
3dbc07b6 MM |
9604 | special_function_kind sfk; |
9605 | tree type; | |
3dbc07b6 MM |
9606 | tree optype; |
9607 | { | |
9608 | switch (sfk) | |
9609 | { | |
9610 | case sfk_constructor: | |
9611 | if (type) | |
33bd39a2 | 9612 | error ("return type specification for constructor invalid"); |
5362b086 | 9613 | |
1f84ec23 | 9614 | type = void_type_node; |
3dbc07b6 MM |
9615 | break; |
9616 | ||
9617 | case sfk_destructor: | |
9618 | if (type) | |
33bd39a2 | 9619 | error ("return type specification for destructor invalid"); |
3dbc07b6 MM |
9620 | type = void_type_node; |
9621 | break; | |
9622 | ||
9623 | case sfk_conversion: | |
9624 | if (type && !same_type_p (type, optype)) | |
33bd39a2 | 9625 | error ("operator `%T' declared to return `%T'", optype, type); |
3dbc07b6 | 9626 | else if (type) |
33bd39a2 | 9627 | pedwarn ("return type specified for `operator %T'", optype); |
3dbc07b6 MM |
9628 | type = optype; |
9629 | break; | |
9630 | ||
9631 | default: | |
a98facb0 | 9632 | abort (); |
3dbc07b6 MM |
9633 | break; |
9634 | } | |
9635 | ||
9636 | return type; | |
9637 | } | |
9638 | ||
8d08fdba MS |
9639 | /* Given declspecs and a declarator, |
9640 | determine the name and type of the object declared | |
9641 | and construct a ..._DECL node for it. | |
9642 | (In one case we can return a ..._TYPE node instead. | |
9643 | For invalid input we sometimes return 0.) | |
9644 | ||
9645 | DECLSPECS is a chain of tree_list nodes whose value fields | |
9646 | are the storage classes and type specifiers. | |
9647 | ||
9648 | DECL_CONTEXT says which syntactic context this declaration is in: | |
9649 | NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL. | |
9650 | FUNCDEF for a function definition. Like NORMAL but a few different | |
9651 | error messages in each case. Return value may be zero meaning | |
9652 | this definition is too screwy to try to parse. | |
9653 | MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to | |
9654 | handle member functions (which have FIELD context). | |
9655 | Return value may be zero meaning this definition is too screwy to | |
9656 | try to parse. | |
9657 | PARM for a parameter declaration (either within a function prototype | |
9658 | or before a function body). Make a PARM_DECL, or return void_type_node. | |
db5ae43f | 9659 | CATCHPARM for a parameter declaration before a catch clause. |
8d08fdba MS |
9660 | TYPENAME if for a typename (in a cast or sizeof). |
9661 | Don't make a DECL node; just return the ..._TYPE node. | |
9662 | FIELD for a struct or union field; make a FIELD_DECL. | |
9663 | BITFIELD for a field with specified width. | |
9664 | INITIALIZED is 1 if the decl has an initializer. | |
9665 | ||
91d231cb JM |
9666 | ATTRLIST is a pointer to the list of attributes, which may be NULL |
9667 | if there are none; *ATTRLIST may be modified if attributes from inside | |
9668 | the declarator should be applied to the declaration. | |
b17e2870 | 9669 | |
70adf8a9 | 9670 | In the TYPENAME case, DECLARATOR is really an abstract declarator. |
8d08fdba MS |
9671 | It may also be so in the PARM case, for a prototype where the |
9672 | argument type is specified but not the name. | |
9673 | ||
9674 | This function is where the complicated C meanings of `static' | |
9675 | and `extern' are interpreted. | |
9676 | ||
9677 | For C++, if there is any monkey business to do, the function which | |
9678 | calls this one must do it, i.e., prepending instance variables, | |
9679 | renaming overloaded function names, etc. | |
9680 | ||
9681 | Note that for this C++, it is an error to define a method within a class | |
9682 | which does not belong to that class. | |
9683 | ||
9684 | Except in the case where SCOPE_REFs are implicitly known (such as | |
9685 | methods within a class being redundantly qualified), | |
9686 | declarations which involve SCOPE_REFs are returned as SCOPE_REFs | |
9687 | (class_name::decl_name). The caller must also deal with this. | |
9688 | ||
9689 | If a constructor or destructor is seen, and the context is FIELD, | |
9690 | then the type gains the attribute TREE_HAS_x. If such a declaration | |
9691 | is erroneous, NULL_TREE is returned. | |
9692 | ||
9693 | QUALS is used only for FUNCDEF and MEMFUNCDEF cases. For a member | |
535233a8 NS |
9694 | function, these are the qualifiers to give to the `this' pointer. We |
9695 | apply TYPE_QUAL_RESTRICT to the this ptr, not the object. | |
8d08fdba MS |
9696 | |
9697 | May return void_type_node if the declarator turned out to be a friend. | |
9698 | See grokfield for details. */ | |
9699 | ||
8d08fdba | 9700 | tree |
c11b6f21 | 9701 | grokdeclarator (declarator, declspecs, decl_context, initialized, attrlist) |
8d08fdba MS |
9702 | tree declspecs; |
9703 | tree declarator; | |
9704 | enum decl_context decl_context; | |
9705 | int initialized; | |
91d231cb | 9706 | tree *attrlist; |
8d08fdba MS |
9707 | { |
9708 | RID_BIT_TYPE specbits; | |
9709 | int nclasses = 0; | |
9710 | tree spec; | |
9711 | tree type = NULL_TREE; | |
9712 | int longlong = 0; | |
91063b51 | 9713 | int type_quals; |
db5ae43f | 9714 | int virtualp, explicitp, friendp, inlinep, staticp; |
8d08fdba MS |
9715 | int explicit_int = 0; |
9716 | int explicit_char = 0; | |
37c46b43 | 9717 | int defaulted_int = 0; |
270d8c65 | 9718 | int extern_langp = 0; |
d5614afb | 9719 | tree dependant_name = NULL_TREE; |
270d8c65 | 9720 | |
8d08fdba | 9721 | tree typedef_decl = NULL_TREE; |
9c0758dd | 9722 | const char *name; |
8d08fdba MS |
9723 | tree typedef_type = NULL_TREE; |
9724 | int funcdef_flag = 0; | |
9725 | enum tree_code innermost_code = ERROR_MARK; | |
9726 | int bitfield = 0; | |
6125f3be DE |
9727 | #if 0 |
9728 | /* See the code below that used this. */ | |
91d231cb | 9729 | tree decl_attr = NULL_TREE; |
6125f3be | 9730 | #endif |
8d08fdba MS |
9731 | /* Set this to error_mark_node for FIELD_DECLs we could not handle properly. |
9732 | All FIELD_DECLs we build here have `init' put into their DECL_INITIAL. */ | |
9733 | tree init = NULL_TREE; | |
9734 | ||
9735 | /* Keep track of what sort of function is being processed | |
9736 | so that we can warn about default return values, or explicit | |
9737 | return values which do not match prescribed defaults. */ | |
3dbc07b6 | 9738 | special_function_kind sfk = sfk_none; |
8d08fdba MS |
9739 | |
9740 | tree dname = NULL_TREE; | |
9741 | tree ctype = current_class_type; | |
9742 | tree ctor_return_type = NULL_TREE; | |
9743 | enum overload_flags flags = NO_SPECIAL; | |
8d08fdba | 9744 | tree quals = NULL_TREE; |
c11b6f21 | 9745 | tree raises = NULL_TREE; |
386b8a85 | 9746 | int template_count = 0; |
2c73f9f5 | 9747 | tree in_namespace = NULL_TREE; |
91d231cb | 9748 | tree returned_attrs = NULL_TREE; |
8d08fdba MS |
9749 | |
9750 | RIDBIT_RESET_ALL (specbits); | |
9751 | if (decl_context == FUNCDEF) | |
9752 | funcdef_flag = 1, decl_context = NORMAL; | |
9753 | else if (decl_context == MEMFUNCDEF) | |
9754 | funcdef_flag = -1, decl_context = FIELD; | |
9755 | else if (decl_context == BITFIELD) | |
9756 | bitfield = 1, decl_context = FIELD; | |
9757 | ||
8d08fdba MS |
9758 | /* Look inside a declarator for the name being declared |
9759 | and get it as a string, for an error message. */ | |
9760 | { | |
be99da77 MS |
9761 | tree *next = &declarator; |
9762 | register tree decl; | |
8d08fdba MS |
9763 | name = NULL; |
9764 | ||
be99da77 MS |
9765 | while (next && *next) |
9766 | { | |
9767 | decl = *next; | |
9768 | switch (TREE_CODE (decl)) | |
8d08fdba | 9769 | { |
52fbc847 JM |
9770 | case TREE_LIST: |
9771 | /* For attributes. */ | |
9772 | next = &TREE_VALUE (decl); | |
9773 | break; | |
9774 | ||
be99da77 MS |
9775 | case COND_EXPR: |
9776 | ctype = NULL_TREE; | |
9777 | next = &TREE_OPERAND (decl, 0); | |
9778 | break; | |
8d08fdba | 9779 | |
2c73f9f5 | 9780 | case BIT_NOT_EXPR: /* For C++ destructors! */ |
8d08fdba | 9781 | { |
be99da77 MS |
9782 | tree name = TREE_OPERAND (decl, 0); |
9783 | tree rename = NULL_TREE; | |
9784 | ||
9785 | my_friendly_assert (flags == NO_SPECIAL, 152); | |
9786 | flags = DTOR_FLAG; | |
3dbc07b6 | 9787 | sfk = sfk_destructor; |
5566b478 MS |
9788 | if (TREE_CODE (name) == TYPE_DECL) |
9789 | TREE_OPERAND (decl, 0) = name = constructor_name (name); | |
be99da77 MS |
9790 | my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153); |
9791 | if (ctype == NULL_TREE) | |
9792 | { | |
9793 | if (current_class_type == NULL_TREE) | |
9794 | { | |
8251199e | 9795 | error ("destructors must be member functions"); |
be99da77 MS |
9796 | flags = NO_SPECIAL; |
9797 | } | |
9798 | else | |
9799 | { | |
9800 | tree t = constructor_name (current_class_name); | |
9801 | if (t != name) | |
9802 | rename = t; | |
9803 | } | |
9804 | } | |
8d08fdba | 9805 | else |
be99da77 MS |
9806 | { |
9807 | tree t = constructor_name (ctype); | |
9808 | if (t != name) | |
9809 | rename = t; | |
9810 | } | |
51c184be | 9811 | |
be99da77 | 9812 | if (rename) |
39211cd5 | 9813 | { |
33bd39a2 | 9814 | error ("destructor `%T' must match class name `%T'", |
5566b478 | 9815 | name, rename); |
be99da77 | 9816 | TREE_OPERAND (decl, 0) = rename; |
39211cd5 | 9817 | } |
be99da77 | 9818 | next = &name; |
51c184be | 9819 | } |
be99da77 | 9820 | break; |
8d08fdba | 9821 | |
be99da77 | 9822 | case ADDR_EXPR: /* C++ reference declaration */ |
2c73f9f5 | 9823 | /* Fall through. */ |
be99da77 MS |
9824 | case ARRAY_REF: |
9825 | case INDIRECT_REF: | |
9826 | ctype = NULL_TREE; | |
9827 | innermost_code = TREE_CODE (decl); | |
9828 | next = &TREE_OPERAND (decl, 0); | |
9829 | break; | |
8d08fdba | 9830 | |
be99da77 | 9831 | case CALL_EXPR: |
43f887f9 | 9832 | if (parmlist_is_exprlist (CALL_DECLARATOR_PARMS (decl))) |
8d08fdba | 9833 | { |
b17e2870 JM |
9834 | /* This is actually a variable declaration using |
9835 | constructor syntax. We need to call start_decl and | |
9836 | cp_finish_decl so we can get the variable | |
9837 | initialized... */ | |
9838 | ||
91d231cb | 9839 | tree attributes; |
be99da77 MS |
9840 | |
9841 | *next = TREE_OPERAND (decl, 0); | |
43f887f9 | 9842 | init = CALL_DECLARATOR_PARMS (decl); |
be99da77 | 9843 | |
b17e2870 JM |
9844 | if (attrlist) |
9845 | { | |
91d231cb | 9846 | attributes = *attrlist; |
b17e2870 JM |
9847 | } |
9848 | else | |
9849 | { | |
9850 | attributes = NULL_TREE; | |
b17e2870 JM |
9851 | } |
9852 | ||
9853 | decl = start_decl (declarator, declspecs, 1, | |
91d231cb | 9854 | attributes, NULL_TREE); |
9fc336c7 | 9855 | decl_type_access_control (decl); |
1eb0072d JM |
9856 | if (decl) |
9857 | { | |
9858 | /* Look for __unused__ attribute */ | |
9859 | if (TREE_USED (TREE_TYPE (decl))) | |
9860 | TREE_USED (decl) = 1; | |
9861 | finish_decl (decl, init, NULL_TREE); | |
9862 | } | |
9863 | else | |
33bd39a2 | 9864 | error ("invalid declarator"); |
be99da77 | 9865 | return 0; |
8d08fdba | 9866 | } |
be99da77 MS |
9867 | innermost_code = TREE_CODE (decl); |
9868 | if (decl_context == FIELD && ctype == NULL_TREE) | |
9869 | ctype = current_class_type; | |
45537677 | 9870 | if (ctype |
c11b6f21 | 9871 | && TREE_OPERAND (decl, 0) |
45537677 MS |
9872 | && (TREE_CODE (TREE_OPERAND (decl, 0)) == TYPE_DECL |
9873 | && ((DECL_NAME (TREE_OPERAND (decl, 0)) | |
9874 | == constructor_name_full (ctype)) | |
9875 | || (DECL_NAME (TREE_OPERAND (decl, 0)) | |
9876 | == constructor_name (ctype))))) | |
be99da77 MS |
9877 | TREE_OPERAND (decl, 0) = constructor_name (ctype); |
9878 | next = &TREE_OPERAND (decl, 0); | |
9879 | decl = *next; | |
9880 | if (ctype != NULL_TREE | |
9881 | && decl != NULL_TREE && flags != DTOR_FLAG | |
9882 | && decl == constructor_name (ctype)) | |
8d08fdba | 9883 | { |
3dbc07b6 | 9884 | sfk = sfk_constructor; |
be99da77 | 9885 | ctor_return_type = ctype; |
8d08fdba | 9886 | } |
be99da77 MS |
9887 | ctype = NULL_TREE; |
9888 | break; | |
68642fb6 | 9889 | |
386b8a85 JM |
9890 | case TEMPLATE_ID_EXPR: |
9891 | { | |
9892 | tree fns = TREE_OPERAND (decl, 0); | |
9893 | ||
9894 | if (TREE_CODE (fns) == LOOKUP_EXPR) | |
9895 | fns = TREE_OPERAND (fns, 0); | |
9896 | ||
8f032717 MM |
9897 | dname = fns; |
9898 | if (TREE_CODE (dname) == COMPONENT_REF) | |
9899 | dname = TREE_OPERAND (dname, 1); | |
9900 | if (TREE_CODE (dname) != IDENTIFIER_NODE) | |
9901 | { | |
9902 | my_friendly_assert (is_overloaded_fn (dname), | |
9903 | 19990331); | |
9904 | dname = DECL_NAME (get_first_fn (dname)); | |
9905 | } | |
386b8a85 | 9906 | } |
2c73f9f5 | 9907 | /* Fall through. */ |
be99da77 MS |
9908 | |
9909 | case IDENTIFIER_NODE: | |
386b8a85 JM |
9910 | if (TREE_CODE (decl) == IDENTIFIER_NODE) |
9911 | dname = decl; | |
9912 | ||
be99da77 MS |
9913 | next = 0; |
9914 | ||
0e5921e8 | 9915 | if (C_IS_RESERVED_WORD (dname)) |
8d08fdba | 9916 | { |
33bd39a2 | 9917 | error ("declarator-id missing; using reserved word `%D'", |
be99da77 MS |
9918 | dname); |
9919 | name = IDENTIFIER_POINTER (dname); | |
8d08fdba | 9920 | } |
596ea4e5 | 9921 | else if (!IDENTIFIER_TYPENAME_P (dname)) |
be99da77 | 9922 | name = IDENTIFIER_POINTER (dname); |
8d08fdba MS |
9923 | else |
9924 | { | |
596ea4e5 AS |
9925 | my_friendly_assert (flags == NO_SPECIAL, 154); |
9926 | flags = TYPENAME_FLAG; | |
9927 | ctor_return_type = TREE_TYPE (dname); | |
9928 | sfk = sfk_conversion; | |
9929 | if (IDENTIFIER_GLOBAL_VALUE (dname) | |
5362b086 | 9930 | && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (dname)) |
596ea4e5 AS |
9931 | == TYPE_DECL)) |
9932 | name = IDENTIFIER_POINTER (dname); | |
9933 | else | |
9934 | name = "<invalid operator>"; | |
8d08fdba | 9935 | } |
be99da77 | 9936 | break; |
8d08fdba | 9937 | |
be99da77 MS |
9938 | /* C++ extension */ |
9939 | case SCOPE_REF: | |
9940 | { | |
9941 | /* Perform error checking, and decide on a ctype. */ | |
9942 | tree cname = TREE_OPERAND (decl, 0); | |
9943 | if (cname == NULL_TREE) | |
9944 | ctype = NULL_TREE; | |
2c73f9f5 ML |
9945 | else if (TREE_CODE (cname) == NAMESPACE_DECL) |
9946 | { | |
9947 | ctype = NULL_TREE; | |
9948 | in_namespace = TREE_OPERAND (decl, 0); | |
9949 | TREE_OPERAND (decl, 0) = NULL_TREE; | |
9950 | } | |
be99da77 MS |
9951 | else if (! is_aggr_type (cname, 1)) |
9952 | TREE_OPERAND (decl, 0) = NULL_TREE; | |
9953 | /* Must test TREE_OPERAND (decl, 1), in case user gives | |
9954 | us `typedef (class::memfunc)(int); memfunc *memfuncptr;' */ | |
9955 | else if (TREE_OPERAND (decl, 1) | |
9956 | && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF) | |
9957 | ctype = cname; | |
73b0fce8 | 9958 | else if (TREE_CODE (cname) == TEMPLATE_TYPE_PARM |
a1281f45 | 9959 | || TREE_CODE (cname) == BOUND_TEMPLATE_TEMPLATE_PARM) |
5566b478 | 9960 | { |
d5614afb NS |
9961 | /* This might be declaring a member of a template |
9962 | parm to be a friend. */ | |
9963 | ctype = cname; | |
9964 | dependant_name = TREE_OPERAND (decl, 1); | |
5566b478 | 9965 | } |
be99da77 MS |
9966 | else if (ctype == NULL_TREE) |
9967 | ctype = cname; | |
9968 | else if (TREE_COMPLEXITY (decl) == current_class_depth) | |
9969 | TREE_OPERAND (decl, 0) = ctype; | |
9970 | else | |
9971 | { | |
9972 | if (! UNIQUELY_DERIVED_FROM_P (cname, ctype)) | |
9973 | { | |
33bd39a2 | 9974 | error ("type `%T' is not derived from type `%T'", |
be99da77 MS |
9975 | cname, ctype); |
9976 | TREE_OPERAND (decl, 0) = NULL_TREE; | |
9977 | } | |
9978 | else | |
9979 | ctype = cname; | |
9980 | } | |
9981 | ||
c91a56d2 MS |
9982 | if (ctype && TREE_CODE (TREE_OPERAND (decl, 1)) == TYPE_DECL |
9983 | && ((DECL_NAME (TREE_OPERAND (decl, 1)) | |
9984 | == constructor_name_full (ctype)) | |
9985 | || (DECL_NAME (TREE_OPERAND (decl, 1)) | |
9986 | == constructor_name (ctype)))) | |
be99da77 MS |
9987 | TREE_OPERAND (decl, 1) = constructor_name (ctype); |
9988 | next = &TREE_OPERAND (decl, 1); | |
9989 | decl = *next; | |
9990 | if (ctype) | |
9991 | { | |
9992 | if (TREE_CODE (decl) == IDENTIFIER_NODE | |
9993 | && constructor_name (ctype) == decl) | |
9994 | { | |
3dbc07b6 | 9995 | sfk = sfk_constructor; |
be99da77 MS |
9996 | ctor_return_type = ctype; |
9997 | } | |
9998 | else if (TREE_CODE (decl) == BIT_NOT_EXPR | |
9999 | && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE | |
10000 | && (constructor_name (ctype) == TREE_OPERAND (decl, 0) | |
10001 | || constructor_name_full (ctype) == TREE_OPERAND (decl, 0))) | |
10002 | { | |
3dbc07b6 | 10003 | sfk = sfk_destructor; |
be99da77 MS |
10004 | ctor_return_type = ctype; |
10005 | flags = DTOR_FLAG; | |
10006 | TREE_OPERAND (decl, 0) = constructor_name (ctype); | |
10007 | next = &TREE_OPERAND (decl, 0); | |
10008 | } | |
10009 | } | |
10010 | } | |
10011 | break; | |
10012 | ||
10013 | case ERROR_MARK: | |
10014 | next = 0; | |
10015 | break; | |
10016 | ||
45537677 MS |
10017 | case TYPE_DECL: |
10018 | /* Parse error puts this typespec where | |
10019 | a declarator should go. */ | |
33bd39a2 | 10020 | error ("`%T' specified as declarator-id", DECL_NAME (decl)); |
45537677 | 10021 | if (TREE_TYPE (decl) == current_class_type) |
33bd39a2 | 10022 | error (" perhaps you want `%T' for a constructor", |
45537677 MS |
10023 | current_class_name); |
10024 | dname = DECL_NAME (decl); | |
10025 | name = IDENTIFIER_POINTER (dname); | |
10026 | ||
e92cc029 | 10027 | /* Avoid giving two errors for this. */ |
45537677 MS |
10028 | IDENTIFIER_CLASS_VALUE (dname) = NULL_TREE; |
10029 | ||
58010b57 | 10030 | declspecs = tree_cons (NULL_TREE, integer_type_node, declspecs); |
45537677 MS |
10031 | *next = dname; |
10032 | next = 0; | |
10033 | break; | |
10034 | ||
be99da77 | 10035 | default: |
33bd39a2 | 10036 | internal_error ("`%D' as declarator", decl); |
8d08fdba | 10037 | } |
be99da77 | 10038 | } |
8d08fdba MS |
10039 | } |
10040 | ||
10041 | /* A function definition's declarator must have the form of | |
10042 | a function declarator. */ | |
10043 | ||
10044 | if (funcdef_flag && innermost_code != CALL_EXPR) | |
10045 | return 0; | |
10046 | ||
e1cd6e56 MS |
10047 | if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG) |
10048 | && innermost_code != CALL_EXPR | |
10049 | && ! (ctype && declspecs == NULL_TREE)) | |
10050 | { | |
33bd39a2 | 10051 | error ("declaration of `%D' as non-function", dname); |
e1cd6e56 MS |
10052 | return void_type_node; |
10053 | } | |
10054 | ||
8d08fdba MS |
10055 | /* Anything declared one level down from the top level |
10056 | must be one of the parameters of a function | |
10057 | (because the body is at least two levels down). */ | |
10058 | ||
10059 | /* This heuristic cannot be applied to C++ nodes! Fixed, however, | |
10060 | by not allowing C++ class definitions to specify their parameters | |
10061 | with xdecls (must be spec.d in the parmlist). | |
10062 | ||
10063 | Since we now wait to push a class scope until we are sure that | |
10064 | we are in a legitimate method context, we must set oldcname | |
a9aedbc2 MS |
10065 | explicitly (since current_class_name is not yet alive). |
10066 | ||
10067 | We also want to avoid calling this a PARM if it is in a namespace. */ | |
8d08fdba | 10068 | |
9188c363 | 10069 | if (decl_context == NORMAL && !toplevel_bindings_p ()) |
a9aedbc2 MS |
10070 | { |
10071 | struct binding_level *b = current_binding_level; | |
10072 | current_binding_level = b->level_chain; | |
10073 | if (current_binding_level != 0 && toplevel_bindings_p ()) | |
10074 | decl_context = PARM; | |
10075 | current_binding_level = b; | |
10076 | } | |
8d08fdba | 10077 | |
34fc957d NS |
10078 | if (name == NULL) |
10079 | name = decl_context == PARM ? "parameter" : "type name"; | |
68642fb6 | 10080 | |
8d08fdba MS |
10081 | /* Look through the decl specs and record which ones appear. |
10082 | Some typespecs are defined as built-in typenames. | |
10083 | Others, the ones that are modifiers of other types, | |
10084 | are represented by bits in SPECBITS: set the bits for | |
10085 | the modifiers that appear. Storage class keywords are also in SPECBITS. | |
10086 | ||
10087 | If there is a typedef name or a type, store the type in TYPE. | |
10088 | This includes builtin typedefs such as `int'. | |
10089 | ||
10090 | Set EXPLICIT_INT if the type is `int' or `char' and did not | |
10091 | come from a user typedef. | |
10092 | ||
10093 | Set LONGLONG if `long' is mentioned twice. | |
10094 | ||
10095 | For C++, constructors and destructors have their own fast treatment. */ | |
10096 | ||
10097 | for (spec = declspecs; spec; spec = TREE_CHAIN (spec)) | |
10098 | { | |
10099 | register int i; | |
10100 | register tree id; | |
10101 | ||
10102 | /* Certain parse errors slip through. For example, | |
10103 | `int class;' is not caught by the parser. Try | |
10104 | weakly to recover here. */ | |
10105 | if (TREE_CODE (spec) != TREE_LIST) | |
10106 | return 0; | |
10107 | ||
10108 | id = TREE_VALUE (spec); | |
10109 | ||
e23bd218 IR |
10110 | /* If the entire declaration is itself tagged as deprecated then |
10111 | suppress reports of deprecated items. */ | |
10112 | if (!adding_implicit_members && id && TREE_DEPRECATED (id)) | |
10113 | { | |
10114 | if (deprecated_state != DEPRECATED_SUPPRESS) | |
10115 | warn_deprecated_use (id); | |
10116 | } | |
10117 | ||
8d08fdba MS |
10118 | if (TREE_CODE (id) == IDENTIFIER_NODE) |
10119 | { | |
a3203465 MS |
10120 | if (id == ridpointers[(int) RID_INT] |
10121 | || id == ridpointers[(int) RID_CHAR] | |
10122 | || id == ridpointers[(int) RID_BOOL] | |
10123 | || id == ridpointers[(int) RID_WCHAR]) | |
8d08fdba MS |
10124 | { |
10125 | if (type) | |
8ccc31eb MS |
10126 | { |
10127 | if (id == ridpointers[(int) RID_BOOL]) | |
8251199e | 10128 | error ("`bool' is now a keyword"); |
8ccc31eb | 10129 | else |
33bd39a2 | 10130 | error ("extraneous `%T' ignored", id); |
8ccc31eb | 10131 | } |
8d08fdba MS |
10132 | else |
10133 | { | |
a3203465 MS |
10134 | if (id == ridpointers[(int) RID_INT]) |
10135 | explicit_int = 1; | |
10136 | else if (id == ridpointers[(int) RID_CHAR]) | |
10137 | explicit_char = 1; | |
8d08fdba MS |
10138 | type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id)); |
10139 | } | |
10140 | goto found; | |
10141 | } | |
e92cc029 | 10142 | /* C++ aggregate types. */ |
8d08fdba MS |
10143 | if (IDENTIFIER_HAS_TYPE_VALUE (id)) |
10144 | { | |
10145 | if (type) | |
33bd39a2 | 10146 | error ("multiple declarations `%T' and `%T'", type, id); |
8d08fdba MS |
10147 | else |
10148 | type = IDENTIFIER_TYPE_VALUE (id); | |
10149 | goto found; | |
10150 | } | |
10151 | ||
f376e137 | 10152 | for (i = (int) RID_FIRST_MODIFIER; i <= (int) RID_LAST_MODIFIER; i++) |
8d08fdba MS |
10153 | { |
10154 | if (ridpointers[i] == id) | |
10155 | { | |
10156 | if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits)) | |
10157 | { | |
795add94 | 10158 | if (pedantic && ! in_system_header && warn_long_long) |
cb9a3ff8 | 10159 | pedwarn ("ISO C++ does not support `long long'"); |
9a3b49ac | 10160 | if (longlong) |
8251199e | 10161 | error ("`long long long' is too long for GCC"); |
8d08fdba MS |
10162 | else |
10163 | longlong = 1; | |
10164 | } | |
10165 | else if (RIDBIT_SETP (i, specbits)) | |
8251199e | 10166 | pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id)); |
270d8c65 NS |
10167 | if (i == (int)RID_EXTERN |
10168 | && TREE_PURPOSE (spec) == error_mark_node) | |
10169 | /* This extern was part of a language linkage. */ | |
10170 | extern_langp = 1; | |
8d08fdba MS |
10171 | RIDBIT_SET (i, specbits); |
10172 | goto found; | |
10173 | } | |
10174 | } | |
10175 | } | |
1899c3a4 | 10176 | else if (TREE_CODE (id) == TYPE_DECL) |
45537677 MS |
10177 | { |
10178 | if (type) | |
33bd39a2 | 10179 | error ("multiple declarations `%T' and `%T'", type, |
45537677 MS |
10180 | TREE_TYPE (id)); |
10181 | else | |
5566b478 MS |
10182 | { |
10183 | type = TREE_TYPE (id); | |
10184 | TREE_VALUE (spec) = type; | |
8d6e459d | 10185 | typedef_decl = id; |
5566b478 | 10186 | } |
45537677 MS |
10187 | goto found; |
10188 | } | |
8d08fdba | 10189 | if (type) |
8251199e | 10190 | error ("two or more data types in declaration of `%s'", name); |
8d08fdba MS |
10191 | else if (TREE_CODE (id) == IDENTIFIER_NODE) |
10192 | { | |
10193 | register tree t = lookup_name (id, 1); | |
10194 | if (!t || TREE_CODE (t) != TYPE_DECL) | |
8251199e | 10195 | error ("`%s' fails to be a typedef or built in type", |
8d08fdba MS |
10196 | IDENTIFIER_POINTER (id)); |
10197 | else | |
10198 | { | |
10199 | type = TREE_TYPE (t); | |
10200 | typedef_decl = t; | |
10201 | } | |
10202 | } | |
bd6dd845 | 10203 | else if (id != error_mark_node) |
8d08fdba MS |
10204 | /* Can't change CLASS nodes into RECORD nodes here! */ |
10205 | type = id; | |
10206 | ||
10207 | found: ; | |
10208 | } | |
10209 | ||
8d6e459d NS |
10210 | #if 0 |
10211 | /* See the code below that used this. */ | |
10212 | if (typedef_decl) | |
10213 | decl_attr = DECL_ATTRIBUTES (typedef_decl); | |
10214 | #endif | |
8d08fdba MS |
10215 | typedef_type = type; |
10216 | ||
37c46b43 | 10217 | /* No type at all: default to `int', and set DEFAULTED_INT |
6eabb241 | 10218 | because it was not a user-defined typedef. */ |
8d08fdba | 10219 | |
a3203465 MS |
10220 | if (type == NULL_TREE |
10221 | && (RIDBIT_SETP (RID_SIGNED, specbits) | |
10222 | || RIDBIT_SETP (RID_UNSIGNED, specbits) | |
10223 | || RIDBIT_SETP (RID_LONG, specbits) | |
10224 | || RIDBIT_SETP (RID_SHORT, specbits))) | |
10225 | { | |
10226 | /* These imply 'int'. */ | |
10227 | type = integer_type_node; | |
37c46b43 | 10228 | defaulted_int = 1; |
a3203465 MS |
10229 | } |
10230 | ||
3dbc07b6 MM |
10231 | if (sfk != sfk_none) |
10232 | type = check_special_function_return_type (sfk, type, | |
3dbc07b6 MM |
10233 | ctor_return_type); |
10234 | else if (type == NULL_TREE) | |
8d08fdba | 10235 | { |
3dbc07b6 MM |
10236 | int is_main; |
10237 | ||
8d08fdba | 10238 | explicit_int = -1; |
3dbc07b6 | 10239 | |
0fd0b7ee JM |
10240 | /* We handle `main' specially here, because 'main () { }' is so |
10241 | common. With no options, it is allowed. With -Wreturn-type, | |
10242 | it is a warning. It is only an error with -pedantic-errors. */ | |
3dbc07b6 | 10243 | is_main = (funcdef_flag |
7437519c | 10244 | && dname && MAIN_NAME_P (dname) |
3dbc07b6 MM |
10245 | && ctype == NULL_TREE |
10246 | && in_namespace == NULL_TREE | |
10247 | && current_namespace == global_namespace); | |
10248 | ||
10249 | if (in_system_header || flag_ms_extensions) | |
10250 | /* Allow it, sigh. */; | |
10251 | else if (pedantic || ! is_main) | |
33bd39a2 | 10252 | pedwarn ("ISO C++ forbids declaration of `%s' with no type", |
3dbc07b6 MM |
10253 | name); |
10254 | else if (warn_return_type) | |
33bd39a2 | 10255 | warning ("ISO C++ forbids declaration of `%s' with no type", |
3dbc07b6 | 10256 | name); |
41eff652 | 10257 | |
3dbc07b6 | 10258 | type = integer_type_node; |
51c184be | 10259 | } |
ad1a6d45 | 10260 | |
8d6e459d | 10261 | if (type && IMPLICIT_TYPENAME_P (type)) |
ad1a6d45 NS |
10262 | { |
10263 | /* The implicit typename extension is deprecated and will be | |
10264 | removed. Warn about its use now. */ | |
33bd39a2 | 10265 | warning ("`%T' is implicitly a typename", type); |
ad1a6d45 NS |
10266 | cp_deprecated ("implicit typename"); |
10267 | ||
10268 | /* Now remove its implicitness, so that we don't warn again. | |
10269 | For instance this might be a typedef, and we do not want to | |
10270 | warn on uses of the typedef itself. Simply clearing the | |
10271 | TREE_TYPE is insufficient. */ | |
10272 | type = copy_node (type); | |
10273 | TREE_TYPE (type) = NULL_TREE; | |
10274 | } | |
8d08fdba MS |
10275 | |
10276 | ctype = NULL_TREE; | |
10277 | ||
10278 | /* Now process the modifiers that were specified | |
10279 | and check for invalid combinations. */ | |
10280 | ||
10281 | /* Long double is a special combination. */ | |
10282 | ||
10283 | if (RIDBIT_SETP (RID_LONG, specbits) | |
10284 | && TYPE_MAIN_VARIANT (type) == double_type_node) | |
10285 | { | |
10286 | RIDBIT_RESET (RID_LONG, specbits); | |
68642fb6 | 10287 | type = build_qualified_type (long_double_type_node, |
89d684bb | 10288 | cp_type_quals (type)); |
8d08fdba MS |
10289 | } |
10290 | ||
10291 | /* Check all other uses of type modifiers. */ | |
10292 | ||
10293 | if (RIDBIT_SETP (RID_UNSIGNED, specbits) | |
10294 | || RIDBIT_SETP (RID_SIGNED, specbits) | |
10295 | || RIDBIT_SETP (RID_LONG, specbits) | |
10296 | || RIDBIT_SETP (RID_SHORT, specbits)) | |
10297 | { | |
10298 | int ok = 0; | |
10299 | ||
10300 | if (TREE_CODE (type) == REAL_TYPE) | |
8251199e | 10301 | error ("short, signed or unsigned invalid for `%s'", name); |
b7484fbe | 10302 | else if (TREE_CODE (type) != INTEGER_TYPE) |
8251199e | 10303 | error ("long, short, signed or unsigned invalid for `%s'", name); |
8d08fdba MS |
10304 | else if (RIDBIT_SETP (RID_LONG, specbits) |
10305 | && RIDBIT_SETP (RID_SHORT, specbits)) | |
8251199e | 10306 | error ("long and short specified together for `%s'", name); |
8d08fdba MS |
10307 | else if ((RIDBIT_SETP (RID_LONG, specbits) |
10308 | || RIDBIT_SETP (RID_SHORT, specbits)) | |
10309 | && explicit_char) | |
8251199e | 10310 | error ("long or short specified with char for `%s'", name); |
8d08fdba MS |
10311 | else if ((RIDBIT_SETP (RID_LONG, specbits) |
10312 | || RIDBIT_SETP (RID_SHORT, specbits)) | |
10313 | && TREE_CODE (type) == REAL_TYPE) | |
8251199e | 10314 | error ("long or short specified with floating type for `%s'", name); |
8d08fdba MS |
10315 | else if (RIDBIT_SETP (RID_SIGNED, specbits) |
10316 | && RIDBIT_SETP (RID_UNSIGNED, specbits)) | |
8251199e | 10317 | error ("signed and unsigned given together for `%s'", name); |
8d08fdba MS |
10318 | else |
10319 | { | |
10320 | ok = 1; | |
37c46b43 | 10321 | if (!explicit_int && !defaulted_int && !explicit_char && pedantic) |
8d08fdba | 10322 | { |
8251199e | 10323 | pedwarn ("long, short, signed or unsigned used invalidly for `%s'", |
8d08fdba MS |
10324 | name); |
10325 | if (flag_pedantic_errors) | |
10326 | ok = 0; | |
10327 | } | |
10328 | } | |
10329 | ||
10330 | /* Discard the type modifiers if they are invalid. */ | |
10331 | if (! ok) | |
10332 | { | |
10333 | RIDBIT_RESET (RID_UNSIGNED, specbits); | |
10334 | RIDBIT_RESET (RID_SIGNED, specbits); | |
10335 | RIDBIT_RESET (RID_LONG, specbits); | |
10336 | RIDBIT_RESET (RID_SHORT, specbits); | |
10337 | longlong = 0; | |
10338 | } | |
10339 | } | |
10340 | ||
37c46b43 MS |
10341 | if (RIDBIT_SETP (RID_COMPLEX, specbits) |
10342 | && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE) | |
10343 | { | |
8251199e | 10344 | error ("complex invalid for `%s'", name); |
37c46b43 MS |
10345 | RIDBIT_RESET (RID_COMPLEX, specbits); |
10346 | } | |
10347 | ||
8d08fdba MS |
10348 | /* Decide whether an integer type is signed or not. |
10349 | Optionally treat bitfields as signed by default. */ | |
10350 | if (RIDBIT_SETP (RID_UNSIGNED, specbits) | |
b89c5a7b MM |
10351 | /* [class.bit] |
10352 | ||
10353 | It is implementation-defined whether a plain (neither | |
10354 | explicitly signed or unsigned) char, short, int, or long | |
10355 | bit-field is signed or unsigned. | |
68642fb6 | 10356 | |
b89c5a7b MM |
10357 | Naturally, we extend this to long long as well. Note that |
10358 | this does not include wchar_t. */ | |
10359 | || (bitfield && !flag_signed_bitfields | |
10360 | && RIDBIT_NOTSETP (RID_SIGNED, specbits) | |
10361 | /* A typedef for plain `int' without `signed' can be | |
10362 | controlled just like plain `int', but a typedef for | |
10363 | `signed int' cannot be so controlled. */ | |
68642fb6 | 10364 | && !(typedef_decl |
29bbeb1c | 10365 | && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)) |
b89c5a7b MM |
10366 | && (TREE_CODE (type) == INTEGER_TYPE |
10367 | || TREE_CODE (type) == CHAR_TYPE) | |
29bbeb1c | 10368 | && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node))) |
8d08fdba MS |
10369 | { |
10370 | if (longlong) | |
10371 | type = long_long_unsigned_type_node; | |
10372 | else if (RIDBIT_SETP (RID_LONG, specbits)) | |
10373 | type = long_unsigned_type_node; | |
10374 | else if (RIDBIT_SETP (RID_SHORT, specbits)) | |
10375 | type = short_unsigned_type_node; | |
10376 | else if (type == char_type_node) | |
10377 | type = unsigned_char_type_node; | |
10378 | else if (typedef_decl) | |
ceef8ce4 | 10379 | type = c_common_unsigned_type (type); |
8d08fdba MS |
10380 | else |
10381 | type = unsigned_type_node; | |
10382 | } | |
10383 | else if (RIDBIT_SETP (RID_SIGNED, specbits) | |
10384 | && type == char_type_node) | |
10385 | type = signed_char_type_node; | |
10386 | else if (longlong) | |
10387 | type = long_long_integer_type_node; | |
10388 | else if (RIDBIT_SETP (RID_LONG, specbits)) | |
10389 | type = long_integer_type_node; | |
10390 | else if (RIDBIT_SETP (RID_SHORT, specbits)) | |
10391 | type = short_integer_type_node; | |
10392 | ||
37c46b43 MS |
10393 | if (RIDBIT_SETP (RID_COMPLEX, specbits)) |
10394 | { | |
10395 | /* If we just have "complex", it is equivalent to | |
10396 | "complex double", but if any modifiers at all are specified it is | |
10397 | the complex form of TYPE. E.g, "complex short" is | |
10398 | "complex short int". */ | |
10399 | ||
10400 | if (defaulted_int && ! longlong | |
10401 | && ! (RIDBIT_SETP (RID_LONG, specbits) | |
10402 | || RIDBIT_SETP (RID_SHORT, specbits) | |
10403 | || RIDBIT_SETP (RID_SIGNED, specbits) | |
10404 | || RIDBIT_SETP (RID_UNSIGNED, specbits))) | |
10405 | type = complex_double_type_node; | |
10406 | else if (type == integer_type_node) | |
10407 | type = complex_integer_type_node; | |
10408 | else if (type == float_type_node) | |
10409 | type = complex_float_type_node; | |
10410 | else if (type == double_type_node) | |
10411 | type = complex_double_type_node; | |
10412 | else if (type == long_double_type_node) | |
10413 | type = complex_long_double_type_node; | |
10414 | else | |
10415 | type = build_complex_type (type); | |
10416 | } | |
10417 | ||
4f2b0fb2 NS |
10418 | type_quals = TYPE_UNQUALIFIED; |
10419 | if (RIDBIT_SETP (RID_CONST, specbits)) | |
10420 | type_quals |= TYPE_QUAL_CONST; | |
10421 | if (RIDBIT_SETP (RID_VOLATILE, specbits)) | |
10422 | type_quals |= TYPE_QUAL_VOLATILE; | |
10423 | if (RIDBIT_SETP (RID_RESTRICT, specbits)) | |
10424 | type_quals |= TYPE_QUAL_RESTRICT; | |
10425 | if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED) | |
33bd39a2 | 10426 | error ("qualifiers are not allowed on declaration of `operator %T'", |
3ac3d9ea MM |
10427 | ctor_return_type); |
10428 | ||
4f2b0fb2 NS |
10429 | type_quals |= cp_type_quals (type); |
10430 | type = cp_build_qualified_type_real | |
10431 | (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl) | |
10432 | ? tf_ignore_bad_quals : 0) | tf_error | tf_warning)); | |
10433 | /* We might have ignored or rejected some of the qualifiers. */ | |
10434 | type_quals = cp_type_quals (type); | |
10435 | ||
8d08fdba MS |
10436 | staticp = 0; |
10437 | inlinep = !! RIDBIT_SETP (RID_INLINE, specbits); | |
8d08fdba | 10438 | virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits); |
db5ae43f MS |
10439 | RIDBIT_RESET (RID_VIRTUAL, specbits); |
10440 | explicitp = RIDBIT_SETP (RID_EXPLICIT, specbits) != 0; | |
10441 | RIDBIT_RESET (RID_EXPLICIT, specbits); | |
8d08fdba | 10442 | |
8d08fdba MS |
10443 | if (RIDBIT_SETP (RID_STATIC, specbits)) |
10444 | staticp = 1 + (decl_context == FIELD); | |
10445 | ||
10446 | if (virtualp && staticp == 2) | |
10447 | { | |
33bd39a2 | 10448 | error ("member `%D' cannot be declared both virtual and static", |
8d08fdba MS |
10449 | dname); |
10450 | staticp = 0; | |
10451 | } | |
10452 | friendp = RIDBIT_SETP (RID_FRIEND, specbits); | |
8d08fdba MS |
10453 | RIDBIT_RESET (RID_FRIEND, specbits); |
10454 | ||
d5614afb NS |
10455 | if (dependant_name && !friendp) |
10456 | { | |
10457 | error ("`%T::%D' is not a valid declarator", ctype, dependant_name); | |
10458 | return void_type_node; | |
10459 | } | |
10460 | ||
8d08fdba MS |
10461 | /* Warn if two storage classes are given. Default to `auto'. */ |
10462 | ||
10463 | if (RIDBIT_ANY_SET (specbits)) | |
10464 | { | |
10465 | if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++; | |
270d8c65 | 10466 | if (RIDBIT_SETP (RID_EXTERN, specbits) && !extern_langp) nclasses++; |
8d08fdba | 10467 | if (decl_context == PARM && nclasses > 0) |
8251199e | 10468 | error ("storage class specifiers invalid in parameter declarations"); |
8d08fdba MS |
10469 | if (RIDBIT_SETP (RID_TYPEDEF, specbits)) |
10470 | { | |
10471 | if (decl_context == PARM) | |
8251199e | 10472 | error ("typedef declaration invalid in parameter declaration"); |
8d08fdba MS |
10473 | nclasses++; |
10474 | } | |
10475 | if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++; | |
10476 | if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++; | |
270d8c65 NS |
10477 | if (!nclasses && !friendp && extern_langp) |
10478 | nclasses++; | |
8d08fdba MS |
10479 | } |
10480 | ||
10481 | /* Give error if `virtual' is used outside of class declaration. */ | |
b7484fbe MS |
10482 | if (virtualp |
10483 | && (current_class_name == NULL_TREE || decl_context != FIELD)) | |
8d08fdba | 10484 | { |
8251199e | 10485 | error ("virtual outside class declaration"); |
8d08fdba MS |
10486 | virtualp = 0; |
10487 | } | |
8d08fdba MS |
10488 | |
10489 | /* Static anonymous unions are dealt with here. */ | |
10490 | if (staticp && decl_context == TYPENAME | |
10491 | && TREE_CODE (declspecs) == TREE_LIST | |
6bdb8141 | 10492 | && ANON_AGGR_TYPE_P (TREE_VALUE (declspecs))) |
8d08fdba MS |
10493 | decl_context = FIELD; |
10494 | ||
8d08fdba MS |
10495 | /* Warn about storage classes that are invalid for certain |
10496 | kinds of declarations (parameters, typenames, etc.). */ | |
10497 | ||
10498 | if (nclasses > 1) | |
8251199e | 10499 | error ("multiple storage classes in declaration of `%s'", name); |
8d08fdba MS |
10500 | else if (decl_context != NORMAL && nclasses > 0) |
10501 | { | |
db5ae43f | 10502 | if ((decl_context == PARM || decl_context == CATCHPARM) |
8d08fdba MS |
10503 | && (RIDBIT_SETP (RID_REGISTER, specbits) |
10504 | || RIDBIT_SETP (RID_AUTO, specbits))) | |
10505 | ; | |
fc378698 MS |
10506 | else if (RIDBIT_SETP (RID_TYPEDEF, specbits)) |
10507 | ; | |
8d08fdba | 10508 | else if (decl_context == FIELD |
d363e7bf AJ |
10509 | /* C++ allows static class elements */ |
10510 | && RIDBIT_SETP (RID_STATIC, specbits)) | |
10511 | /* C++ also allows inlines and signed and unsigned elements, | |
10512 | but in those cases we don't come in here. */ | |
8d08fdba MS |
10513 | ; |
10514 | else | |
10515 | { | |
10516 | if (decl_context == FIELD) | |
10517 | { | |
b7484fbe MS |
10518 | tree tmp = NULL_TREE; |
10519 | register int op = 0; | |
10520 | ||
10521 | if (declarator) | |
10522 | { | |
68642fb6 | 10523 | /* Avoid trying to get an operand off an identifier node. */ |
9e9ff709 MS |
10524 | if (TREE_CODE (declarator) == IDENTIFIER_NODE) |
10525 | tmp = declarator; | |
10526 | else | |
10527 | tmp = TREE_OPERAND (declarator, 0); | |
b7484fbe | 10528 | op = IDENTIFIER_OPNAME_P (tmp); |
596ea4e5 AS |
10529 | if (IDENTIFIER_TYPENAME_P (tmp)) |
10530 | { | |
10531 | if (IDENTIFIER_GLOBAL_VALUE (tmp) | |
5362b086 | 10532 | && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (tmp)) |
596ea4e5 AS |
10533 | == TYPE_DECL)) |
10534 | name = IDENTIFIER_POINTER (tmp); | |
10535 | else | |
10536 | name = "<invalid operator>"; | |
10537 | } | |
b7484fbe | 10538 | } |
8251199e | 10539 | error ("storage class specified for %s `%s'", |
6eabb241 | 10540 | op ? "member operator" : "field", |
596ea4e5 | 10541 | name); |
8d08fdba MS |
10542 | } |
10543 | else | |
b9d12519 KG |
10544 | { |
10545 | if (decl_context == PARM || decl_context == CATCHPARM) | |
10546 | error ("storage class specified for parameter `%s'", name); | |
10547 | else | |
10548 | error ("storage class specified for typename"); | |
10549 | } | |
8d08fdba MS |
10550 | RIDBIT_RESET (RID_REGISTER, specbits); |
10551 | RIDBIT_RESET (RID_AUTO, specbits); | |
10552 | RIDBIT_RESET (RID_EXTERN, specbits); | |
8d08fdba MS |
10553 | } |
10554 | } | |
10555 | else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag) | |
10556 | { | |
a9aedbc2 | 10557 | if (toplevel_bindings_p ()) |
8d08fdba | 10558 | { |
59be0cdd | 10559 | /* It's common practice (and completely valid) to have a const |
8d08fdba | 10560 | be initialized and declared extern. */ |
950ad3c3 | 10561 | if (!(type_quals & TYPE_QUAL_CONST)) |
8251199e | 10562 | warning ("`%s' initialized and declared `extern'", name); |
8d08fdba MS |
10563 | } |
10564 | else | |
8251199e | 10565 | error ("`%s' has both `extern' and initializer", name); |
8d08fdba MS |
10566 | } |
10567 | else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag | |
a9aedbc2 | 10568 | && ! toplevel_bindings_p ()) |
8251199e | 10569 | error ("nested function `%s' declared `extern'", name); |
a9aedbc2 | 10570 | else if (toplevel_bindings_p ()) |
8d08fdba MS |
10571 | { |
10572 | if (RIDBIT_SETP (RID_AUTO, specbits)) | |
8251199e | 10573 | error ("top-level declaration of `%s' specifies `auto'", name); |
8d08fdba MS |
10574 | } |
10575 | ||
909e536a | 10576 | if (nclasses > 0 && friendp) |
8251199e | 10577 | error ("storage class specifiers invalid in friend function declarations"); |
909e536a | 10578 | |
8d08fdba MS |
10579 | /* Now figure out the structure of the declarator proper. |
10580 | Descend through it, creating more complex types, until we reach | |
10581 | the declared identifier (or NULL_TREE, in an absolute declarator). */ | |
10582 | ||
386b8a85 JM |
10583 | while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE |
10584 | && TREE_CODE (declarator) != TEMPLATE_ID_EXPR) | |
8d08fdba MS |
10585 | { |
10586 | /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]), | |
10587 | an INDIRECT_REF (for *...), | |
10588 | a CALL_EXPR (for ...(...)), | |
10589 | an identifier (for the name being declared) | |
10590 | or a null pointer (for the place in an absolute declarator | |
10591 | where the name was omitted). | |
10592 | For the last two cases, we have just exited the loop. | |
10593 | ||
10594 | For C++ it could also be | |
10595 | a SCOPE_REF (for class :: ...). In this case, we have converted | |
10596 | sensible names to types, and those are the values we use to | |
10597 | qualify the member name. | |
10598 | an ADDR_EXPR (for &...), | |
10599 | a BIT_NOT_EXPR (for destructors) | |
8d08fdba MS |
10600 | |
10601 | At this point, TYPE is the type of elements of an array, | |
10602 | or for a function to return, or for a pointer to point to. | |
10603 | After this sequence of ifs, TYPE is the type of the | |
10604 | array or function or pointer, and DECLARATOR has had its | |
10605 | outermost layer removed. */ | |
10606 | ||
bd6dd845 | 10607 | if (type == error_mark_node) |
8d08fdba MS |
10608 | { |
10609 | if (TREE_CODE (declarator) == SCOPE_REF) | |
10610 | declarator = TREE_OPERAND (declarator, 1); | |
10611 | else | |
10612 | declarator = TREE_OPERAND (declarator, 0); | |
10613 | continue; | |
10614 | } | |
10615 | if (quals != NULL_TREE | |
10616 | && (declarator == NULL_TREE | |
10617 | || TREE_CODE (declarator) != SCOPE_REF)) | |
10618 | { | |
10619 | if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE) | |
10620 | ctype = TYPE_METHOD_BASETYPE (type); | |
10621 | if (ctype != NULL_TREE) | |
10622 | { | |
8d08fdba | 10623 | tree dummy = build_decl (TYPE_DECL, NULL_TREE, type); |
535233a8 | 10624 | grok_method_quals (ctype, dummy, quals); |
8d08fdba | 10625 | type = TREE_TYPE (dummy); |
535233a8 | 10626 | ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))); |
8d08fdba MS |
10627 | quals = NULL_TREE; |
10628 | } | |
10629 | } | |
1eab9b56 | 10630 | |
8d08fdba MS |
10631 | switch (TREE_CODE (declarator)) |
10632 | { | |
52fbc847 JM |
10633 | case TREE_LIST: |
10634 | { | |
10635 | /* We encode a declarator with embedded attributes using | |
91d231cb JM |
10636 | a TREE_LIST. */ |
10637 | tree attrs = TREE_PURPOSE (declarator); | |
10638 | tree inner_decl; | |
c9574c9f R |
10639 | int attr_flags; |
10640 | ||
52fbc847 | 10641 | declarator = TREE_VALUE (declarator); |
91d231cb JM |
10642 | inner_decl = declarator; |
10643 | while (inner_decl != NULL_TREE | |
10644 | && TREE_CODE (inner_decl) == TREE_LIST) | |
10645 | inner_decl = TREE_VALUE (inner_decl); | |
c9574c9f | 10646 | attr_flags = 0; |
91d231cb JM |
10647 | if (inner_decl == NULL_TREE |
10648 | || TREE_CODE (inner_decl) == IDENTIFIER_NODE) | |
10649 | attr_flags |= (int) ATTR_FLAG_DECL_NEXT; | |
10650 | if (TREE_CODE (inner_decl) == CALL_EXPR) | |
10651 | attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT; | |
10652 | if (TREE_CODE (inner_decl) == ARRAY_REF) | |
10653 | attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT; | |
10654 | returned_attrs = decl_attributes (&type, | |
10655 | chainon (returned_attrs, attrs), | |
10656 | attr_flags); | |
52fbc847 JM |
10657 | } |
10658 | break; | |
10659 | ||
8d08fdba MS |
10660 | case ARRAY_REF: |
10661 | { | |
2848ad0f | 10662 | register tree size; |
8d08fdba | 10663 | |
2848ad0f | 10664 | size = TREE_OPERAND (declarator, 1); |
8d08fdba | 10665 | |
f2e2cbd4 | 10666 | /* VC++ spells a zero-sized array with []. */ |
e8ea7036 MM |
10667 | if (size == NULL_TREE && decl_context == FIELD && ! staticp |
10668 | && ! RIDBIT_SETP (RID_TYPEDEF, specbits)) | |
f2e2cbd4 JM |
10669 | size = integer_zero_node; |
10670 | ||
2848ad0f | 10671 | declarator = TREE_OPERAND (declarator, 0); |
8d08fdba | 10672 | |
2848ad0f | 10673 | type = create_array_type_for_decl (dname, type, size); |
c1b177ec ML |
10674 | |
10675 | /* VLAs never work as fields. */ | |
5362b086 | 10676 | if (decl_context == FIELD && !processing_template_decl |
c1b177ec ML |
10677 | && TREE_CODE (type) == ARRAY_TYPE |
10678 | && TYPE_DOMAIN (type) != NULL_TREE | |
10679 | && !TREE_CONSTANT (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))) | |
10680 | { | |
33bd39a2 | 10681 | error ("size of member `%D' is not constant", dname); |
c1b177ec ML |
10682 | /* Proceed with arbitrary constant size, so that offset |
10683 | computations don't get confused. */ | |
10684 | type = create_array_type_for_decl (dname, TREE_TYPE (type), | |
10685 | integer_one_node); | |
10686 | } | |
10687 | ||
8d08fdba MS |
10688 | ctype = NULL_TREE; |
10689 | } | |
10690 | break; | |
10691 | ||
10692 | case CALL_EXPR: | |
10693 | { | |
10694 | tree arg_types; | |
f376e137 | 10695 | int funcdecl_p; |
43f887f9 | 10696 | tree inner_parms = CALL_DECLARATOR_PARMS (declarator); |
f376e137 | 10697 | tree inner_decl = TREE_OPERAND (declarator, 0); |
8d08fdba MS |
10698 | |
10699 | /* Declaring a function type. | |
10700 | Make sure we have a valid type for the function to return. */ | |
8d08fdba | 10701 | |
91063b51 | 10702 | /* We now know that the TYPE_QUALS don't apply to the |
14ae7e7d | 10703 | decl, but to its return type. */ |
91063b51 | 10704 | type_quals = TYPE_UNQUALIFIED; |
8d08fdba MS |
10705 | |
10706 | /* Warn about some types functions can't return. */ | |
10707 | ||
10708 | if (TREE_CODE (type) == FUNCTION_TYPE) | |
10709 | { | |
8251199e | 10710 | error ("`%s' declared as function returning a function", name); |
8d08fdba MS |
10711 | type = integer_type_node; |
10712 | } | |
10713 | if (TREE_CODE (type) == ARRAY_TYPE) | |
10714 | { | |
8251199e | 10715 | error ("`%s' declared as function returning an array", name); |
8d08fdba MS |
10716 | type = integer_type_node; |
10717 | } | |
10718 | ||
f376e137 MS |
10719 | if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF) |
10720 | inner_decl = TREE_OPERAND (inner_decl, 1); | |
10721 | ||
68642fb6 | 10722 | if (inner_decl && TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR) |
386b8a85 JM |
10723 | inner_decl = dname; |
10724 | ||
b7484fbe | 10725 | /* Pick up type qualifiers which should be applied to `this'. */ |
43f887f9 | 10726 | quals = CALL_DECLARATOR_QUALS (declarator); |
b7484fbe | 10727 | |
c11b6f21 | 10728 | /* Pick up the exception specifications. */ |
43f887f9 | 10729 | raises = CALL_DECLARATOR_EXCEPTION_SPEC (declarator); |
c11b6f21 | 10730 | |
f376e137 MS |
10731 | /* Say it's a definition only for the CALL_EXPR |
10732 | closest to the identifier. */ | |
beb53fb8 | 10733 | funcdecl_p |
68642fb6 | 10734 | = inner_decl |
386b8a85 | 10735 | && (TREE_CODE (inner_decl) == IDENTIFIER_NODE |
68642fb6 | 10736 | || TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR |
386b8a85 | 10737 | || TREE_CODE (inner_decl) == BIT_NOT_EXPR); |
68642fb6 | 10738 | |
8d08fdba MS |
10739 | if (ctype == NULL_TREE |
10740 | && decl_context == FIELD | |
f376e137 | 10741 | && funcdecl_p |
8d08fdba MS |
10742 | && (friendp == 0 || dname == current_class_name)) |
10743 | ctype = current_class_type; | |
10744 | ||
3dbc07b6 | 10745 | if (ctype && sfk == sfk_conversion) |
8d08fdba MS |
10746 | TYPE_HAS_CONVERSION (ctype) = 1; |
10747 | if (ctype && constructor_name (ctype) == dname) | |
10748 | { | |
10749 | /* We are within a class's scope. If our declarator name | |
10750 | is the same as the class name, and we are defining | |
10751 | a function, then it is a constructor/destructor, and | |
10752 | therefore returns a void type. */ | |
10753 | ||
10754 | if (flags == DTOR_FLAG) | |
10755 | { | |
cab1f180 ML |
10756 | /* ISO C++ 12.4/2. A destructor may not be |
10757 | declared const or volatile. A destructor may | |
10758 | not be static. */ | |
8d08fdba | 10759 | if (staticp == 2) |
8251199e | 10760 | error ("destructor cannot be static member function"); |
b7484fbe | 10761 | if (quals) |
8d08fdba | 10762 | { |
33bd39a2 | 10763 | error ("destructors may not be `%s'", |
4f70a846 MM |
10764 | IDENTIFIER_POINTER (TREE_VALUE (quals))); |
10765 | quals = NULL_TREE; | |
8d08fdba MS |
10766 | } |
10767 | if (decl_context == FIELD) | |
10768 | { | |
4dacf5bd KG |
10769 | if (! member_function_or_else (ctype, |
10770 | current_class_type, | |
10771 | flags)) | |
8d08fdba MS |
10772 | return void_type_node; |
10773 | } | |
10774 | } | |
2c73f9f5 | 10775 | else /* It's a constructor. */ |
8d08fdba | 10776 | { |
db5ae43f MS |
10777 | if (explicitp == 1) |
10778 | explicitp = 2; | |
cab1f180 ML |
10779 | /* ISO C++ 12.1. A constructor may not be |
10780 | declared const or volatile. A constructor may | |
10781 | not be virtual. A constructor may not be | |
10782 | static. */ | |
8d08fdba | 10783 | if (staticp == 2) |
8251199e | 10784 | error ("constructor cannot be static member function"); |
8d08fdba MS |
10785 | if (virtualp) |
10786 | { | |
8251199e | 10787 | pedwarn ("constructors cannot be declared virtual"); |
8d08fdba MS |
10788 | virtualp = 0; |
10789 | } | |
b7484fbe | 10790 | if (quals) |
8d08fdba | 10791 | { |
33bd39a2 | 10792 | error ("constructors may not be `%s'", |
4f70a846 MM |
10793 | IDENTIFIER_POINTER (TREE_VALUE (quals))); |
10794 | quals = NULL_TREE; | |
d363e7bf | 10795 | } |
8d08fdba | 10796 | { |
51c184be | 10797 | RID_BIT_TYPE tmp_bits; |
4e135bdd | 10798 | memcpy (&tmp_bits, &specbits, sizeof (RID_BIT_TYPE)); |
51c184be MS |
10799 | RIDBIT_RESET (RID_INLINE, tmp_bits); |
10800 | RIDBIT_RESET (RID_STATIC, tmp_bits); | |
10801 | if (RIDBIT_ANY_SET (tmp_bits)) | |
8251199e | 10802 | error ("return value type specifier for constructor ignored"); |
8d08fdba | 10803 | } |
6eabb241 | 10804 | if (decl_context == FIELD) |
8d08fdba | 10805 | { |
4dacf5bd KG |
10806 | if (! member_function_or_else (ctype, |
10807 | current_class_type, | |
10808 | flags)) | |
8d08fdba MS |
10809 | return void_type_node; |
10810 | TYPE_HAS_CONSTRUCTOR (ctype) = 1; | |
3dbc07b6 | 10811 | if (sfk != sfk_constructor) |
8d08fdba MS |
10812 | return NULL_TREE; |
10813 | } | |
10814 | } | |
10815 | if (decl_context == FIELD) | |
10816 | staticp = 0; | |
10817 | } | |
b7484fbe | 10818 | else if (friendp) |
8d08fdba | 10819 | { |
b7484fbe | 10820 | if (initialized) |
8251199e | 10821 | error ("can't initialize friend function `%s'", name); |
b7484fbe MS |
10822 | if (virtualp) |
10823 | { | |
10824 | /* Cannot be both friend and virtual. */ | |
8251199e | 10825 | error ("virtual functions cannot be friends"); |
b7484fbe MS |
10826 | RIDBIT_RESET (RID_FRIEND, specbits); |
10827 | friendp = 0; | |
10828 | } | |
28cbf42c | 10829 | if (decl_context == NORMAL) |
8251199e | 10830 | error ("friend declaration not in class definition"); |
28cbf42c | 10831 | if (current_function_decl && funcdef_flag) |
33bd39a2 | 10832 | error ("can't define friend function `%s' in a local class definition", |
28cbf42c | 10833 | name); |
8d08fdba MS |
10834 | } |
10835 | ||
8d08fdba MS |
10836 | /* Construct the function type and go to the next |
10837 | inner layer of declarator. */ | |
10838 | ||
f376e137 | 10839 | declarator = TREE_OPERAND (declarator, 0); |
8d08fdba | 10840 | |
f376e137 MS |
10841 | /* FIXME: This is where default args should be fully |
10842 | processed. */ | |
8d08fdba | 10843 | |
5cce22b6 | 10844 | arg_types = grokparms (inner_parms); |
8d08fdba | 10845 | |
4f70a846 | 10846 | if (declarator && flags == DTOR_FLAG) |
8d08fdba | 10847 | { |
4f70a846 MM |
10848 | /* A destructor declared in the body of a class will |
10849 | be represented as a BIT_NOT_EXPR. But, we just | |
10850 | want the underlying IDENTIFIER. */ | |
8d08fdba | 10851 | if (TREE_CODE (declarator) == BIT_NOT_EXPR) |
4f70a846 | 10852 | declarator = TREE_OPERAND (declarator, 0); |
68642fb6 | 10853 | |
8b27e9ef | 10854 | if (arg_types != void_list_node) |
8d08fdba | 10855 | { |
33bd39a2 | 10856 | error ("destructors may not have parameters"); |
4f70a846 MM |
10857 | arg_types = void_list_node; |
10858 | last_function_parms = NULL_TREE; | |
8d08fdba MS |
10859 | } |
10860 | } | |
10861 | ||
d22c8596 | 10862 | /* ANSI says that `const int foo ();' |
8d08fdba | 10863 | does not make the function foo const. */ |
d22c8596 | 10864 | type = build_function_type (type, arg_types); |
42976354 BK |
10865 | |
10866 | { | |
10867 | tree t; | |
10868 | for (t = arg_types; t; t = TREE_CHAIN (t)) | |
10869 | if (TREE_PURPOSE (t) | |
10870 | && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG) | |
10871 | { | |
10872 | add_defarg_fn (type); | |
10873 | break; | |
10874 | } | |
10875 | } | |
8d08fdba MS |
10876 | } |
10877 | break; | |
10878 | ||
10879 | case ADDR_EXPR: | |
10880 | case INDIRECT_REF: | |
10881 | /* Filter out pointers-to-references and references-to-references. | |
10882 | We can get these if a TYPE_DECL is used. */ | |
10883 | ||
10884 | if (TREE_CODE (type) == REFERENCE_TYPE) | |
10885 | { | |
8251199e | 10886 | error ("cannot declare %s to references", |
8d08fdba MS |
10887 | TREE_CODE (declarator) == ADDR_EXPR |
10888 | ? "references" : "pointers"); | |
10889 | declarator = TREE_OPERAND (declarator, 0); | |
10890 | continue; | |
10891 | } | |
10892 | ||
a5894242 MS |
10893 | if (TREE_CODE (type) == OFFSET_TYPE |
10894 | && (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE | |
10895 | || TREE_CODE (TREE_TYPE (type)) == REFERENCE_TYPE)) | |
10896 | { | |
33bd39a2 | 10897 | error ("cannot declare pointer to `%#T' member", |
a5894242 MS |
10898 | TREE_TYPE (type)); |
10899 | type = TREE_TYPE (type); | |
10900 | } | |
10901 | ||
61a127b3 MM |
10902 | /* Merge any constancy or volatility into the target type |
10903 | for the pointer. */ | |
10904 | ||
91063b51 MM |
10905 | /* We now know that the TYPE_QUALS don't apply to the decl, |
10906 | but to the target of the pointer. */ | |
10907 | type_quals = TYPE_UNQUALIFIED; | |
8d08fdba | 10908 | |
6eabb241 | 10909 | if (TREE_CODE (declarator) == ADDR_EXPR) |
8d08fdba | 10910 | { |
69851283 MM |
10911 | if (TREE_CODE (type) == VOID_TYPE) |
10912 | error ("invalid type: `void &'"); | |
8d08fdba | 10913 | else |
69851283 | 10914 | type = build_reference_type (type); |
8d08fdba MS |
10915 | } |
10916 | else if (TREE_CODE (type) == METHOD_TYPE) | |
d8f8dca1 | 10917 | type = build_ptrmemfunc_type (build_pointer_type (type)); |
8d08fdba MS |
10918 | else |
10919 | type = build_pointer_type (type); | |
10920 | ||
10921 | /* Process a list of type modifier keywords (such as | |
10922 | const or volatile) that were given inside the `*' or `&'. */ | |
10923 | ||
10924 | if (TREE_TYPE (declarator)) | |
10925 | { | |
10926 | register tree typemodlist; | |
10927 | int erred = 0; | |
4f2b0fb2 NS |
10928 | int constp = 0; |
10929 | int volatilep = 0; | |
10930 | int restrictp = 0; | |
10931 | ||
8d08fdba MS |
10932 | for (typemodlist = TREE_TYPE (declarator); typemodlist; |
10933 | typemodlist = TREE_CHAIN (typemodlist)) | |
10934 | { | |
91063b51 MM |
10935 | tree qualifier = TREE_VALUE (typemodlist); |
10936 | ||
10937 | if (qualifier == ridpointers[(int) RID_CONST]) | |
4f2b0fb2 NS |
10938 | { |
10939 | constp++; | |
10940 | type_quals |= TYPE_QUAL_CONST; | |
10941 | } | |
91063b51 | 10942 | else if (qualifier == ridpointers[(int) RID_VOLATILE]) |
4f2b0fb2 NS |
10943 | { |
10944 | volatilep++; | |
10945 | type_quals |= TYPE_QUAL_VOLATILE; | |
10946 | } | |
91063b51 | 10947 | else if (qualifier == ridpointers[(int) RID_RESTRICT]) |
4f2b0fb2 NS |
10948 | { |
10949 | restrictp++; | |
10950 | type_quals |= TYPE_QUAL_RESTRICT; | |
10951 | } | |
8d08fdba MS |
10952 | else if (!erred) |
10953 | { | |
10954 | erred = 1; | |
91063b51 | 10955 | error ("invalid type modifier within pointer declarator"); |
8d08fdba MS |
10956 | } |
10957 | } | |
10958 | if (constp > 1) | |
8251199e | 10959 | pedwarn ("duplicate `const'"); |
8d08fdba | 10960 | if (volatilep > 1) |
8251199e | 10961 | pedwarn ("duplicate `volatile'"); |
91063b51 MM |
10962 | if (restrictp > 1) |
10963 | pedwarn ("duplicate `restrict'"); | |
91063b51 | 10964 | type = cp_build_qualified_type (type, type_quals); |
4f2b0fb2 | 10965 | type_quals = cp_type_quals (type); |
8d08fdba MS |
10966 | } |
10967 | declarator = TREE_OPERAND (declarator, 0); | |
10968 | ctype = NULL_TREE; | |
10969 | break; | |
10970 | ||
10971 | case SCOPE_REF: | |
10972 | { | |
10973 | /* We have converted type names to NULL_TREE if the | |
10974 | name was bogus, or to a _TYPE node, if not. | |
10975 | ||
10976 | The variable CTYPE holds the type we will ultimately | |
10977 | resolve to. The code here just needs to build | |
10978 | up appropriate member types. */ | |
10979 | tree sname = TREE_OPERAND (declarator, 1); | |
386b8a85 JM |
10980 | tree t; |
10981 | ||
8d08fdba MS |
10982 | /* Destructors can have their visibilities changed as well. */ |
10983 | if (TREE_CODE (sname) == BIT_NOT_EXPR) | |
10984 | sname = TREE_OPERAND (sname, 0); | |
10985 | ||
10986 | if (TREE_COMPLEXITY (declarator) == 0) | |
10987 | /* This needs to be here, in case we are called | |
10988 | multiple times. */ ; | |
9a68c51f JM |
10989 | else if (TREE_COMPLEXITY (declarator) == -1) |
10990 | /* Namespace member. */ | |
10991 | pop_decl_namespace (); | |
8d08fdba | 10992 | else if (friendp && (TREE_COMPLEXITY (declarator) < 2)) |
2c73f9f5 | 10993 | /* Don't fall out into global scope. Hides real bug? --eichin */ ; |
5566b478 MS |
10994 | else if (! IS_AGGR_TYPE_CODE |
10995 | (TREE_CODE (TREE_OPERAND (declarator, 0)))) | |
10996 | ; | |
8d08fdba MS |
10997 | else if (TREE_COMPLEXITY (declarator) == current_class_depth) |
10998 | { | |
9a3b49ac MS |
10999 | /* Resolve any TYPENAME_TYPEs from the decl-specifier-seq |
11000 | that refer to ctype. They couldn't be resolved earlier | |
11001 | because we hadn't pushed into the class yet. | |
11002 | Example: resolve 'B<T>::type' in | |
11003 | 'B<typename B<T>::type> B<T>::f () { }'. */ | |
11004 | if (current_template_parms | |
11005 | && uses_template_parms (type) | |
11006 | && uses_template_parms (current_class_type)) | |
11007 | { | |
11008 | tree args = current_template_args (); | |
c2ea3a40 NS |
11009 | type = tsubst (type, args, tf_error | tf_warning, |
11010 | NULL_TREE); | |
9a3b49ac MS |
11011 | } |
11012 | ||
a28e3c7f MS |
11013 | /* This pop_nested_class corresponds to the |
11014 | push_nested_class used to push into class scope for | |
11015 | parsing the argument list of a function decl, in | |
11016 | qualified_id. */ | |
b74a0560 | 11017 | pop_nested_class (); |
8d08fdba MS |
11018 | TREE_COMPLEXITY (declarator) = current_class_depth; |
11019 | } | |
11020 | else | |
a98facb0 | 11021 | abort (); |
8d08fdba MS |
11022 | |
11023 | if (TREE_OPERAND (declarator, 0) == NULL_TREE) | |
11024 | { | |
11025 | /* We had a reference to a global decl, or | |
11026 | perhaps we were given a non-aggregate typedef, | |
11027 | in which case we cleared this out, and should just | |
11028 | keep going as though it wasn't there. */ | |
11029 | declarator = sname; | |
11030 | continue; | |
11031 | } | |
11032 | ctype = TREE_OPERAND (declarator, 0); | |
11033 | ||
386b8a85 | 11034 | t = ctype; |
68642fb6 | 11035 | while (t != NULL_TREE && CLASS_TYPE_P (t)) |
386b8a85 | 11036 | { |
5362b086 | 11037 | /* You're supposed to have one `template <...>' |
da8a7456 MM |
11038 | for every template class, but you don't need one |
11039 | for a full specialization. For example: | |
11040 | ||
11041 | template <class T> struct S{}; | |
11042 | template <> struct S<int> { void f(); }; | |
11043 | void S<int>::f () {} | |
11044 | ||
11045 | is correct; there shouldn't be a `template <>' for | |
11046 | the definition of `S<int>::f'. */ | |
11047 | if (CLASSTYPE_TEMPLATE_INFO (t) | |
11048 | && (CLASSTYPE_TEMPLATE_INSTANTIATION (t) | |
92ecdfb7 NS |
11049 | || uses_template_parms (CLASSTYPE_TI_ARGS (t))) |
11050 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t))) | |
386b8a85 | 11051 | template_count += 1; |
da8a7456 | 11052 | |
386b8a85 | 11053 | t = TYPE_MAIN_DECL (t); |
92ecdfb7 | 11054 | t = DECL_CONTEXT (t); |
386b8a85 JM |
11055 | } |
11056 | ||
8d08fdba MS |
11057 | if (sname == NULL_TREE) |
11058 | goto done_scoping; | |
11059 | ||
11060 | if (TREE_CODE (sname) == IDENTIFIER_NODE) | |
11061 | { | |
11062 | /* This is the `standard' use of the scoping operator: | |
11063 | basetype :: member . */ | |
11064 | ||
db5ae43f | 11065 | if (ctype == current_class_type) |
28cbf42c MS |
11066 | { |
11067 | /* class A { | |
11068 | void A::f (); | |
11069 | }; | |
11070 | ||
11071 | Is this ill-formed? */ | |
11072 | ||
11073 | if (pedantic) | |
33bd39a2 | 11074 | pedwarn ("extra qualification `%T::' on member `%s' ignored", |
28cbf42c MS |
11075 | ctype, name); |
11076 | } | |
db5ae43f | 11077 | else if (TREE_CODE (type) == FUNCTION_TYPE) |
8d08fdba | 11078 | { |
eeb75383 | 11079 | if (current_class_type == NULL_TREE || friendp) |
14ae7e7d JM |
11080 | type = build_cplus_method_type (ctype, TREE_TYPE (type), |
11081 | TYPE_ARG_TYPES (type)); | |
8d08fdba MS |
11082 | else |
11083 | { | |
33bd39a2 | 11084 | error ("cannot declare member function `%T::%s' within `%T'", |
a3203465 | 11085 | ctype, name, current_class_type); |
8d08fdba MS |
11086 | return void_type_node; |
11087 | } | |
11088 | } | |
5566b478 | 11089 | else if (RIDBIT_SETP (RID_TYPEDEF, specbits) |
d0f062fb | 11090 | || COMPLETE_TYPE_P (complete_type (ctype))) |
8d08fdba | 11091 | { |
2c73f9f5 | 11092 | /* Have to move this code elsewhere in this function. |
db5ae43f MS |
11093 | this code is used for i.e., typedef int A::M; M *pm; |
11094 | ||
11095 | It is? How? jason 10/2/94 */ | |
8d08fdba | 11096 | |
8d08fdba MS |
11097 | if (current_class_type) |
11098 | { | |
33bd39a2 | 11099 | error ("cannot declare member `%T::%s' within `%T'", |
db5ae43f MS |
11100 | ctype, name, current_class_type); |
11101 | return void_type_node; | |
8d08fdba MS |
11102 | } |
11103 | type = build_offset_type (ctype, type); | |
11104 | } | |
8d08fdba | 11105 | else |
eeb75383 | 11106 | { |
7a228918 | 11107 | cxx_incomplete_type_error (NULL_TREE, ctype); |
eeb75383 | 11108 | return error_mark_node; |
d363e7bf | 11109 | } |
8d08fdba MS |
11110 | |
11111 | declarator = sname; | |
11112 | } | |
8d08fdba | 11113 | else if (TREE_CODE (sname) == SCOPE_REF) |
a98facb0 | 11114 | abort (); |
8d08fdba MS |
11115 | else |
11116 | { | |
11117 | done_scoping: | |
11118 | declarator = TREE_OPERAND (declarator, 1); | |
11119 | if (declarator && TREE_CODE (declarator) == CALL_EXPR) | |
11120 | /* In this case, we will deal with it later. */ | |
11121 | ; | |
11122 | else | |
11123 | { | |
11124 | if (TREE_CODE (type) == FUNCTION_TYPE) | |
14ae7e7d JM |
11125 | type = build_cplus_method_type (ctype, TREE_TYPE (type), |
11126 | TYPE_ARG_TYPES (type)); | |
8d08fdba MS |
11127 | else |
11128 | type = build_offset_type (ctype, type); | |
11129 | } | |
11130 | } | |
11131 | } | |
11132 | break; | |
11133 | ||
11134 | case BIT_NOT_EXPR: | |
11135 | declarator = TREE_OPERAND (declarator, 0); | |
11136 | break; | |
11137 | ||
8d08fdba MS |
11138 | case RECORD_TYPE: |
11139 | case UNION_TYPE: | |
11140 | case ENUMERAL_TYPE: | |
11141 | declarator = NULL_TREE; | |
11142 | break; | |
11143 | ||
11144 | case ERROR_MARK: | |
11145 | declarator = NULL_TREE; | |
11146 | break; | |
11147 | ||
11148 | default: | |
a98facb0 | 11149 | abort (); |
8d08fdba MS |
11150 | } |
11151 | } | |
11152 | ||
91d231cb | 11153 | if (returned_attrs) |
1eab9b56 | 11154 | { |
91d231cb JM |
11155 | if (attrlist) |
11156 | *attrlist = chainon (returned_attrs, *attrlist); | |
1eab9b56 | 11157 | else |
91d231cb | 11158 | attrlist = &returned_attrs; |
1eab9b56 JM |
11159 | } |
11160 | ||
419c6212 JM |
11161 | /* Now TYPE has the actual type. */ |
11162 | ||
55b3d665 JM |
11163 | /* Did array size calculations overflow? */ |
11164 | ||
11165 | if (TREE_CODE (type) == ARRAY_TYPE | |
11166 | && COMPLETE_TYPE_P (type) | |
11167 | && TREE_OVERFLOW (TYPE_SIZE (type))) | |
11168 | { | |
11169 | error ("size of array `%s' is too large", name); | |
aba649ba | 11170 | /* If we proceed with the array type as it is, we'll eventually |
55b3d665 JM |
11171 | crash in tree_low_cst(). */ |
11172 | type = error_mark_node; | |
11173 | } | |
11174 | ||
34fc957d | 11175 | if (explicitp == 1 || (explicitp && friendp)) |
db5ae43f | 11176 | { |
34fc957d NS |
11177 | /* [dcl.fct.spec] The explicit specifier shall only be used in |
11178 | declarations of constructors within a class definition. */ | |
11179 | error ("only declarations of constructors can be `explicit'"); | |
db5ae43f MS |
11180 | explicitp = 0; |
11181 | } | |
11182 | ||
f30432d7 MS |
11183 | if (RIDBIT_SETP (RID_MUTABLE, specbits)) |
11184 | { | |
34fc957d NS |
11185 | if (current_class_name == NULL_TREE || decl_context == PARM || friendp) |
11186 | { | |
11187 | error ("non-member `%s' cannot be declared `mutable'", name); | |
11188 | RIDBIT_RESET (RID_MUTABLE, specbits); | |
11189 | } | |
11190 | else if (decl_context == TYPENAME || RIDBIT_SETP (RID_TYPEDEF, specbits)) | |
f30432d7 | 11191 | { |
34fc957d | 11192 | error ("non-object member `%s' cannot be declared `mutable'", name); |
f30432d7 MS |
11193 | RIDBIT_RESET (RID_MUTABLE, specbits); |
11194 | } | |
34fc957d NS |
11195 | else if (TREE_CODE (type) == FUNCTION_TYPE |
11196 | || TREE_CODE (type) == METHOD_TYPE) | |
11197 | { | |
11198 | error ("function `%s' cannot be declared `mutable'", name); | |
11199 | RIDBIT_RESET (RID_MUTABLE, specbits); | |
11200 | } | |
f30432d7 MS |
11201 | else if (staticp) |
11202 | { | |
8251199e | 11203 | error ("static `%s' cannot be declared `mutable'", name); |
f30432d7 MS |
11204 | RIDBIT_RESET (RID_MUTABLE, specbits); |
11205 | } | |
34fc957d NS |
11206 | else if (type_quals & TYPE_QUAL_CONST) |
11207 | { | |
11208 | error ("const `%s' cannot be declared `mutable'", name); | |
4f2b0fb2 | 11209 | RIDBIT_RESET (RID_MUTABLE, specbits); |
34fc957d | 11210 | } |
f30432d7 MS |
11211 | } |
11212 | ||
efe38fab JM |
11213 | if (declarator == NULL_TREE |
11214 | || TREE_CODE (declarator) == IDENTIFIER_NODE | |
11215 | || (TREE_CODE (declarator) == TEMPLATE_ID_EXPR | |
11216 | && (TREE_CODE (type) == FUNCTION_TYPE | |
11217 | || TREE_CODE (type) == METHOD_TYPE))) | |
11218 | /* OK */; | |
11219 | else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) | |
11220 | { | |
33bd39a2 | 11221 | error ("template-id `%D' used as a declarator", declarator); |
efe38fab JM |
11222 | declarator = dname; |
11223 | } | |
11224 | else | |
419c6212 | 11225 | /* Unexpected declarator format. */ |
a98facb0 | 11226 | abort (); |
efe38fab | 11227 | |
419c6212 JM |
11228 | /* If this is declaring a typedef name, return a TYPE_DECL. */ |
11229 | ||
fc378698 | 11230 | if (RIDBIT_SETP (RID_TYPEDEF, specbits) && decl_context != TYPENAME) |
8d08fdba MS |
11231 | { |
11232 | tree decl; | |
11233 | ||
11234 | /* Note that the grammar rejects storage classes | |
11235 | in typenames, fields or parameters. */ | |
eff71ab0 PB |
11236 | if (current_lang_name == lang_name_java) |
11237 | TYPE_FOR_JAVA (type) = 1; | |
8d08fdba | 11238 | |
d2e5ee5c MS |
11239 | if (decl_context == FIELD) |
11240 | { | |
094fe153 | 11241 | if (declarator == constructor_name (current_class_type)) |
33bd39a2 | 11242 | pedwarn ("ISO C++ forbids nested type `%D' with same name as enclosing class", |
d2e5ee5c MS |
11243 | declarator); |
11244 | decl = build_lang_decl (TYPE_DECL, declarator, type); | |
d2e5ee5c MS |
11245 | } |
11246 | else | |
6bda7a5e NS |
11247 | { |
11248 | decl = build_decl (TYPE_DECL, declarator, type); | |
11249 | if (!current_function_decl) | |
11250 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); | |
11251 | } | |
11252 | ||
9188c363 MM |
11253 | /* If the user declares "typedef struct {...} foo" then the |
11254 | struct will have an anonymous name. Fill that name in now. | |
11255 | Nothing can refer to it, so nothing needs know about the name | |
11256 | change. */ | |
8d08fdba | 11257 | if (type != error_mark_node |
2bdb0643 | 11258 | && declarator |
8d08fdba MS |
11259 | && TYPE_NAME (type) |
11260 | && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL | |
1951a1b6 | 11261 | && TYPE_ANONYMOUS_P (type) |
89d684bb | 11262 | && cp_type_quals (type) == TYPE_UNQUALIFIED) |
8d08fdba | 11263 | { |
dcd08efc JM |
11264 | tree oldname = TYPE_NAME (type); |
11265 | tree t; | |
11266 | ||
2c73f9f5 | 11267 | /* Replace the anonymous name with the real name everywhere. */ |
8d08fdba | 11268 | lookup_tag_reverse (type, declarator); |
dcd08efc JM |
11269 | for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) |
11270 | if (TYPE_NAME (t) == oldname) | |
11271 | TYPE_NAME (t) = decl; | |
8d08fdba MS |
11272 | |
11273 | if (TYPE_LANG_SPECIFIC (type)) | |
11274 | TYPE_WAS_ANONYMOUS (type) = 1; | |
11275 | ||
33964bf4 MM |
11276 | /* If this is a typedef within a template class, the nested |
11277 | type is a (non-primary) template. The name for the | |
11278 | template needs updating as well. */ | |
11279 | if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type)) | |
68642fb6 | 11280 | DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)) |
33964bf4 MM |
11281 | = TYPE_IDENTIFIER (type); |
11282 | ||
50a6dbd7 JM |
11283 | /* FIXME remangle member functions; member functions of a |
11284 | type with external linkage have external linkage. */ | |
fc378698 | 11285 | } |
fc378698 | 11286 | |
8d08fdba MS |
11287 | if (TREE_CODE (type) == OFFSET_TYPE || TREE_CODE (type) == METHOD_TYPE) |
11288 | { | |
8251199e | 11289 | cp_error_at ("typedef name may not be class-qualified", decl); |
a3203465 | 11290 | return NULL_TREE; |
8d08fdba MS |
11291 | } |
11292 | else if (quals) | |
11293 | { | |
11294 | if (ctype == NULL_TREE) | |
11295 | { | |
11296 | if (TREE_CODE (type) != METHOD_TYPE) | |
cb9a3ff8 | 11297 | cp_error_at ("invalid type qualifier for non-member function type", decl); |
8d08fdba MS |
11298 | else |
11299 | ctype = TYPE_METHOD_BASETYPE (type); | |
11300 | } | |
11301 | if (ctype != NULL_TREE) | |
11302 | grok_method_quals (ctype, decl, quals); | |
11303 | } | |
11304 | ||
11305 | if (RIDBIT_SETP (RID_SIGNED, specbits) | |
11306 | || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))) | |
11307 | C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1; | |
11308 | ||
c91a56d2 MS |
11309 | bad_specifiers (decl, "type", virtualp, quals != NULL_TREE, |
11310 | inlinep, friendp, raises != NULL_TREE); | |
11311 | ||
11312 | if (initialized) | |
8251199e | 11313 | error ("typedef declaration includes an initializer"); |
c91a56d2 | 11314 | |
8d08fdba MS |
11315 | return decl; |
11316 | } | |
11317 | ||
11318 | /* Detect the case of an array type of unspecified size | |
11319 | which came, as such, direct from a typedef name. | |
8d6e459d NS |
11320 | We must copy the type, so that the array's domain can be |
11321 | individually set by the object's initializer. */ | |
8d08fdba | 11322 | |
8d6e459d NS |
11323 | if (type && typedef_type |
11324 | && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type) | |
55b3d665 | 11325 | && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type)) |
8d6e459d | 11326 | type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE); |
8d08fdba | 11327 | |
969fd501 NS |
11328 | /* Detect where we're using a typedef of function type to declare a |
11329 | function. last_function_parms will not be set, so we must create | |
11330 | it now. */ | |
11331 | ||
11332 | if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE) | |
11333 | { | |
11334 | tree decls = NULL_TREE; | |
11335 | tree args; | |
11336 | ||
11337 | for (args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args)) | |
11338 | { | |
11339 | tree decl = build_decl (PARM_DECL, NULL_TREE, TREE_VALUE (args)); | |
11340 | ||
11341 | TREE_CHAIN (decl) = decls; | |
11342 | decls = decl; | |
11343 | } | |
11344 | ||
11345 | last_function_parms = nreverse (decls); | |
11346 | } | |
11347 | ||
8d08fdba MS |
11348 | /* If this is a type name (such as, in a cast or sizeof), |
11349 | compute the type and return it now. */ | |
11350 | ||
11351 | if (decl_context == TYPENAME) | |
11352 | { | |
11353 | /* Note that the grammar rejects storage classes | |
11354 | in typenames, fields or parameters. */ | |
91063b51 | 11355 | if (type_quals != TYPE_UNQUALIFIED) |
6eabb241 | 11356 | type_quals = TYPE_UNQUALIFIED; |
8d08fdba MS |
11357 | |
11358 | /* Special case: "friend class foo" looks like a TYPENAME context. */ | |
11359 | if (friendp) | |
11360 | { | |
91063b51 | 11361 | if (type_quals != TYPE_UNQUALIFIED) |
b7484fbe | 11362 | { |
33bd39a2 | 11363 | error ("type qualifiers specified for friend class declaration"); |
91063b51 | 11364 | type_quals = TYPE_UNQUALIFIED; |
b7484fbe MS |
11365 | } |
11366 | if (inlinep) | |
11367 | { | |
33bd39a2 | 11368 | error ("`inline' specified for friend class declaration"); |
b7484fbe MS |
11369 | inlinep = 0; |
11370 | } | |
f2ae0c45 JM |
11371 | |
11372 | /* Until core issue 180 is resolved, allow 'friend typename A::B'. | |
c00996a3 | 11373 | But don't allow implicit typenames except with a class-key. */ |
f2ae0c45 JM |
11374 | if (!current_aggr && (TREE_CODE (type) != TYPENAME_TYPE |
11375 | || IMPLICIT_TYPENAME_P (type))) | |
4b054b80 NS |
11376 | { |
11377 | if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) | |
33bd39a2 | 11378 | pedwarn ("template parameters cannot be friends"); |
f2ae0c45 | 11379 | else if (TREE_CODE (type) == TYPENAME_TYPE) |
33bd39a2 | 11380 | pedwarn ("\ |
f2ae0c45 JM |
11381 | friend declaration requires class-key, i.e. `friend class %T::%T'", |
11382 | constructor_name (current_class_type), | |
11383 | TYPE_IDENTIFIER (type)); | |
4b054b80 | 11384 | else |
33bd39a2 | 11385 | pedwarn ("\ |
f2ae0c45 JM |
11386 | friend declaration requires class-key, i.e. `friend %#T'", |
11387 | type); | |
4b054b80 | 11388 | } |
b7484fbe MS |
11389 | |
11390 | /* Only try to do this stuff if we didn't already give up. */ | |
11391 | if (type != integer_type_node) | |
11392 | { | |
ab04e34b | 11393 | decl_type_access_control (TYPE_NAME (type)); |
d363e7bf | 11394 | |
b7484fbe MS |
11395 | /* A friendly class? */ |
11396 | if (current_class_type) | |
11397 | make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type)); | |
11398 | else | |
33bd39a2 | 11399 | error ("trying to make class `%T' a friend of global scope", |
2ae7bada | 11400 | type); |
d363e7bf | 11401 | |
b7484fbe MS |
11402 | type = void_type_node; |
11403 | } | |
8d08fdba MS |
11404 | } |
11405 | else if (quals) | |
11406 | { | |
8d08fdba MS |
11407 | if (ctype == NULL_TREE) |
11408 | { | |
41cbc04c | 11409 | if (TREE_CODE (type) != METHOD_TYPE) |
33bd39a2 | 11410 | error ("invalid qualifiers on non-member function type"); |
41cbc04c NS |
11411 | else |
11412 | ctype = TYPE_METHOD_BASETYPE (type); | |
11413 | } | |
11414 | if (ctype) | |
11415 | { | |
11416 | tree dummy = build_decl (TYPE_DECL, declarator, type); | |
11417 | grok_method_quals (ctype, dummy, quals); | |
11418 | type = TREE_TYPE (dummy); | |
8d08fdba | 11419 | } |
8d08fdba MS |
11420 | } |
11421 | ||
11422 | return type; | |
11423 | } | |
11424 | else if (declarator == NULL_TREE && decl_context != PARM | |
db5ae43f | 11425 | && decl_context != CATCHPARM |
8d08fdba MS |
11426 | && TREE_CODE (type) != UNION_TYPE |
11427 | && ! bitfield) | |
11428 | { | |
33bd39a2 | 11429 | error ("abstract declarator `%T' used as declaration", type); |
8d08fdba MS |
11430 | declarator = make_anon_name (); |
11431 | } | |
11432 | ||
11433 | /* `void' at top level (not within pointer) | |
11434 | is allowed only in typedefs or type names. | |
11435 | We don't complain about parms either, but that is because | |
11436 | a better error message can be made later. */ | |
11437 | ||
a1774733 | 11438 | if (TREE_CODE (type) == VOID_TYPE && decl_context != PARM) |
8d08fdba | 11439 | { |
b7484fbe | 11440 | if (! declarator) |
8251199e | 11441 | error ("unnamed variable or field declared void"); |
b7484fbe | 11442 | else if (TREE_CODE (declarator) == IDENTIFIER_NODE) |
8d08fdba MS |
11443 | { |
11444 | if (IDENTIFIER_OPNAME_P (declarator)) | |
a98facb0 | 11445 | abort (); |
8d08fdba | 11446 | else |
8251199e | 11447 | error ("variable or field `%s' declared void", name); |
8d08fdba MS |
11448 | } |
11449 | else | |
8251199e | 11450 | error ("variable or field declared void"); |
8d08fdba MS |
11451 | type = integer_type_node; |
11452 | } | |
11453 | ||
11454 | /* Now create the decl, which may be a VAR_DECL, a PARM_DECL | |
11455 | or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */ | |
11456 | ||
14ae7e7d JM |
11457 | if (decl_context == PARM || decl_context == CATCHPARM) |
11458 | { | |
11459 | if (ctype || in_namespace) | |
11460 | error ("cannot use `::' in parameter declaration"); | |
11461 | ||
11462 | /* A parameter declared as an array of T is really a pointer to T. | |
11463 | One declared as a function is really a pointer to a function. | |
11464 | One declared as a member is really a pointer to member. */ | |
11465 | ||
11466 | if (TREE_CODE (type) == ARRAY_TYPE) | |
11467 | { | |
11468 | /* Transfer const-ness of array into that of type pointed to. */ | |
11469 | type = build_pointer_type (TREE_TYPE (type)); | |
91063b51 | 11470 | type_quals = TYPE_UNQUALIFIED; |
14ae7e7d JM |
11471 | } |
11472 | else if (TREE_CODE (type) == FUNCTION_TYPE) | |
11473 | type = build_pointer_type (type); | |
11474 | else if (TREE_CODE (type) == OFFSET_TYPE) | |
11475 | type = build_pointer_type (type); | |
14ae7e7d | 11476 | } |
68642fb6 | 11477 | |
8d08fdba MS |
11478 | { |
11479 | register tree decl; | |
11480 | ||
11481 | if (decl_context == PARM) | |
11482 | { | |
ff350acd | 11483 | decl = build_decl (PARM_DECL, declarator, type); |
8d08fdba MS |
11484 | |
11485 | bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE, | |
11486 | inlinep, friendp, raises != NULL_TREE); | |
8d08fdba MS |
11487 | |
11488 | /* Compute the type actually passed in the parmlist, | |
11489 | for the case where there is no prototype. | |
11490 | (For example, shorts and chars are passed as ints.) | |
11491 | When there is a prototype, this is overridden later. */ | |
11492 | ||
39211cd5 | 11493 | DECL_ARG_TYPE (decl) = type_promotes_to (type); |
8d08fdba MS |
11494 | } |
11495 | else if (decl_context == FIELD) | |
11496 | { | |
11497 | if (type == error_mark_node) | |
11498 | { | |
11499 | /* Happens when declaring arrays of sizes which | |
11500 | are error_mark_node, for example. */ | |
11501 | decl = NULL_TREE; | |
11502 | } | |
997a088c | 11503 | else if (in_namespace && !friendp) |
05008fb9 MM |
11504 | { |
11505 | /* Something like struct S { int N::j; }; */ | |
33bd39a2 | 11506 | error ("invalid use of `::'"); |
05008fb9 MM |
11507 | decl = NULL_TREE; |
11508 | } | |
8d08fdba MS |
11509 | else if (TREE_CODE (type) == FUNCTION_TYPE) |
11510 | { | |
11511 | int publicp = 0; | |
e1467ff2 | 11512 | tree function_context; |
8d08fdba | 11513 | |
72b7eeff MS |
11514 | /* We catch the others as conflicts with the builtin |
11515 | typedefs. */ | |
11516 | if (friendp && declarator == ridpointers[(int) RID_SIGNED]) | |
11517 | { | |
33bd39a2 | 11518 | error ("function `%D' cannot be declared friend", |
72b7eeff MS |
11519 | declarator); |
11520 | friendp = 0; | |
11521 | } | |
11522 | ||
8d08fdba MS |
11523 | if (friendp == 0) |
11524 | { | |
11525 | if (ctype == NULL_TREE) | |
11526 | ctype = current_class_type; | |
11527 | ||
11528 | if (ctype == NULL_TREE) | |
11529 | { | |
33bd39a2 | 11530 | error ("can't make `%D' into a method -- not in a class", |
8d08fdba MS |
11531 | declarator); |
11532 | return void_type_node; | |
11533 | } | |
11534 | ||
11535 | /* ``A union may [ ... ] not [ have ] virtual functions.'' | |
11536 | ARM 9.5 */ | |
11537 | if (virtualp && TREE_CODE (ctype) == UNION_TYPE) | |
11538 | { | |
33bd39a2 | 11539 | error ("function `%D' declared virtual inside a union", |
8d08fdba MS |
11540 | declarator); |
11541 | return void_type_node; | |
11542 | } | |
11543 | ||
596ea4e5 AS |
11544 | if (declarator == ansi_opname (NEW_EXPR) |
11545 | || declarator == ansi_opname (VEC_NEW_EXPR) | |
11546 | || declarator == ansi_opname (DELETE_EXPR) | |
11547 | || declarator == ansi_opname (VEC_DELETE_EXPR)) | |
8d08fdba MS |
11548 | { |
11549 | if (virtualp) | |
11550 | { | |
33bd39a2 | 11551 | error ("`%D' cannot be declared virtual, since it is always static", |
8d08fdba MS |
11552 | declarator); |
11553 | virtualp = 0; | |
11554 | } | |
11555 | } | |
11556 | else if (staticp < 2) | |
14ae7e7d JM |
11557 | type = build_cplus_method_type (ctype, TREE_TYPE (type), |
11558 | TYPE_ARG_TYPES (type)); | |
8d08fdba MS |
11559 | } |
11560 | ||
11561 | /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */ | |
68642fb6 | 11562 | function_context = (ctype != NULL_TREE) ? |
4f1c5b7d | 11563 | decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE; |
e1467ff2 MM |
11564 | publicp = (! friendp || ! staticp) |
11565 | && function_context == NULL_TREE; | |
68642fb6 | 11566 | decl = grokfndecl (ctype, type, |
386b8a85 JM |
11567 | TREE_CODE (declarator) != TEMPLATE_ID_EXPR |
11568 | ? declarator : dname, | |
11569 | declarator, | |
7a8f9fa9 | 11570 | virtualp, flags, quals, raises, |
386b8a85 | 11571 | friendp ? -1 : 0, friendp, publicp, inlinep, |
2c73f9f5 | 11572 | funcdef_flag, template_count, in_namespace); |
20496fa2 | 11573 | if (decl == NULL_TREE) |
3ddfb0e6 | 11574 | return decl; |
6125f3be DE |
11575 | #if 0 |
11576 | /* This clobbers the attrs stored in `decl' from `attrlist'. */ | |
91d231cb JM |
11577 | /* The decl and setting of decl_attr is also turned off. */ |
11578 | decl = build_decl_attribute_variant (decl, decl_attr); | |
6125f3be | 11579 | #endif |
f0e01782 | 11580 | |
cc804e51 MM |
11581 | /* [class.conv.ctor] |
11582 | ||
11583 | A constructor declared without the function-specifier | |
11584 | explicit that can be called with a single parameter | |
11585 | specifies a conversion from the type of its first | |
11586 | parameter to the type of its class. Such a constructor | |
11587 | is called a converting constructor. */ | |
db5ae43f MS |
11588 | if (explicitp == 2) |
11589 | DECL_NONCONVERTING_P (decl) = 1; | |
cc804e51 MM |
11590 | else if (DECL_CONSTRUCTOR_P (decl)) |
11591 | { | |
11592 | /* The constructor can be called with exactly one | |
11593 | parameter if there is at least one parameter, and | |
11594 | any subsequent parameters have default arguments. | |
e0fff4b3 JM |
11595 | Ignore any compiler-added parms. */ |
11596 | tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (decl); | |
cc804e51 MM |
11597 | |
11598 | if (arg_types == void_list_node | |
68642fb6 UD |
11599 | || (arg_types |
11600 | && TREE_CHAIN (arg_types) | |
cc804e51 MM |
11601 | && TREE_CHAIN (arg_types) != void_list_node |
11602 | && !TREE_PURPOSE (TREE_CHAIN (arg_types)))) | |
11603 | DECL_NONCONVERTING_P (decl) = 1; | |
11604 | } | |
8d08fdba MS |
11605 | } |
11606 | else if (TREE_CODE (type) == METHOD_TYPE) | |
11607 | { | |
faae18ab MS |
11608 | /* We only get here for friend declarations of |
11609 | members of other classes. */ | |
8d08fdba MS |
11610 | /* All method decls are public, so tell grokfndecl to set |
11611 | TREE_PUBLIC, also. */ | |
386b8a85 | 11612 | decl = grokfndecl (ctype, type, declarator, declarator, |
7a8f9fa9 | 11613 | virtualp, flags, quals, raises, |
386b8a85 | 11614 | friendp ? -1 : 0, friendp, 1, 0, funcdef_flag, |
2c73f9f5 | 11615 | template_count, in_namespace); |
f0e01782 MS |
11616 | if (decl == NULL_TREE) |
11617 | return NULL_TREE; | |
8d08fdba | 11618 | } |
5566b478 | 11619 | else if (!staticp && ! processing_template_decl |
d0f062fb | 11620 | && !COMPLETE_TYPE_P (complete_type (type)) |
8d08fdba MS |
11621 | && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0)) |
11622 | { | |
b7484fbe | 11623 | if (declarator) |
33bd39a2 | 11624 | error ("field `%D' has incomplete type", declarator); |
b7484fbe | 11625 | else |
33bd39a2 | 11626 | error ("name `%T' has incomplete type", type); |
8d08fdba MS |
11627 | |
11628 | /* If we're instantiating a template, tell them which | |
11629 | instantiation made the field's type be incomplete. */ | |
11630 | if (current_class_type | |
11631 | && TYPE_NAME (current_class_type) | |
d2e5ee5c | 11632 | && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type)) |
8d08fdba MS |
11633 | && declspecs && TREE_VALUE (declspecs) |
11634 | && TREE_TYPE (TREE_VALUE (declspecs)) == type) | |
33bd39a2 | 11635 | error (" in instantiation of template `%T'", |
db5ae43f MS |
11636 | current_class_type); |
11637 | ||
8d08fdba MS |
11638 | type = error_mark_node; |
11639 | decl = NULL_TREE; | |
11640 | } | |
11641 | else | |
11642 | { | |
11643 | if (friendp) | |
11644 | { | |
cb9a3ff8 | 11645 | error ("`%s' is neither function nor member function; cannot be declared friend", |
8d08fdba MS |
11646 | IDENTIFIER_POINTER (declarator)); |
11647 | friendp = 0; | |
11648 | } | |
11649 | decl = NULL_TREE; | |
11650 | } | |
11651 | ||
11652 | if (friendp) | |
11653 | { | |
11654 | /* Friends are treated specially. */ | |
11655 | if (ctype == current_class_type) | |
8251199e | 11656 | warning ("member functions are implicitly friends of their class"); |
ab04e34b NS |
11657 | else |
11658 | { | |
11659 | tree t = NULL_TREE; | |
11660 | if (decl && DECL_NAME (decl)) | |
11661 | { | |
11662 | if (template_class_depth (current_class_type) == 0) | |
11663 | { | |
11664 | decl | |
11665 | = check_explicit_specialization | |
11666 | (declarator, decl, | |
11667 | template_count, 2 * (funcdef_flag != 0) + 4); | |
11668 | if (decl == error_mark_node) | |
11669 | return error_mark_node; | |
11670 | } | |
11671 | ||
11672 | t = do_friend (ctype, declarator, decl, | |
11673 | last_function_parms, *attrlist, | |
11674 | flags, quals, funcdef_flag); | |
11675 | } | |
11676 | if (t && funcdef_flag) | |
11677 | return t; | |
11678 | ||
11679 | return void_type_node; | |
11680 | } | |
8d08fdba MS |
11681 | } |
11682 | ||
11683 | /* Structure field. It may not be a function, except for C++ */ | |
11684 | ||
11685 | if (decl == NULL_TREE) | |
11686 | { | |
8d08fdba MS |
11687 | if (initialized) |
11688 | { | |
3ac3d9ea MM |
11689 | if (!staticp) |
11690 | { | |
11691 | /* An attempt is being made to initialize a non-static | |
11692 | member. But, from [class.mem]: | |
68642fb6 | 11693 | |
3ac3d9ea MM |
11694 | 4 A member-declarator can contain a |
11695 | constant-initializer only if it declares a static | |
11696 | member (_class.static_) of integral or enumeration | |
68642fb6 | 11697 | type, see _class.static.data_. |
3ac3d9ea MM |
11698 | |
11699 | This used to be relatively common practice, but | |
11700 | the rest of the compiler does not correctly | |
11701 | handle the initialization unless the member is | |
11702 | static so we make it static below. */ | |
33bd39a2 | 11703 | pedwarn ("ISO C++ forbids initialization of member `%D'", |
3ac3d9ea | 11704 | declarator); |
33bd39a2 | 11705 | pedwarn ("making `%D' static", declarator); |
3ac3d9ea MM |
11706 | staticp = 1; |
11707 | } | |
11708 | ||
6ba89f8e MM |
11709 | if (uses_template_parms (type)) |
11710 | /* We'll check at instantiation time. */ | |
11711 | ; | |
11712 | else if (check_static_variable_definition (declarator, | |
11713 | type)) | |
11714 | /* If we just return the declaration, crashes | |
11715 | will sometimes occur. We therefore return | |
72a93143 JM |
11716 | void_type_node, as if this was a friend |
11717 | declaration, to cause callers to completely | |
11718 | ignore this declaration. */ | |
6ba89f8e | 11719 | return void_type_node; |
8d08fdba MS |
11720 | } |
11721 | ||
8ebeee52 | 11722 | /* 9.2p13 [class.mem] */ |
4d7614e9 | 11723 | if (declarator == constructor_name (current_class_type) |
e54930f9 JM |
11724 | /* The standard does not allow non-static data members |
11725 | here either, but we agreed at the 10/99 meeting | |
11726 | to change that in TC 1 so that they are allowed in | |
11727 | classes with no user-defined constructors. */ | |
11728 | && staticp) | |
33bd39a2 | 11729 | pedwarn ("ISO C++ forbids static data member `%D' with same name as enclosing class", |
8ebeee52 JM |
11730 | declarator); |
11731 | ||
3ac3d9ea | 11732 | if (staticp) |
8d08fdba | 11733 | { |
f18a14bc MM |
11734 | /* C++ allows static class members. All other work |
11735 | for this is done by grokfield. */ | |
4ce3d537 | 11736 | decl = build_lang_decl (VAR_DECL, declarator, type); |
5b605f68 MS |
11737 | TREE_STATIC (decl) = 1; |
11738 | /* In class context, 'static' means public access. */ | |
3ac3d9ea | 11739 | TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = 1; |
8d08fdba MS |
11740 | } |
11741 | else | |
11742 | { | |
721c3b42 | 11743 | decl = build_decl (FIELD_DECL, declarator, type); |
2bf105ab | 11744 | DECL_NONADDRESSABLE_P (decl) = bitfield; |
8d08fdba MS |
11745 | if (RIDBIT_SETP (RID_MUTABLE, specbits)) |
11746 | { | |
11747 | DECL_MUTABLE_P (decl) = 1; | |
11748 | RIDBIT_RESET (RID_MUTABLE, specbits); | |
11749 | } | |
11750 | } | |
11751 | ||
11752 | bad_specifiers (decl, "field", virtualp, quals != NULL_TREE, | |
11753 | inlinep, friendp, raises != NULL_TREE); | |
11754 | } | |
11755 | } | |
11756 | else if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE) | |
11757 | { | |
386b8a85 | 11758 | tree original_name; |
8d08fdba MS |
11759 | int publicp = 0; |
11760 | ||
11761 | if (! declarator) | |
11762 | return NULL_TREE; | |
11763 | ||
386b8a85 JM |
11764 | if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) |
11765 | original_name = dname; | |
11766 | else | |
11767 | original_name = declarator; | |
11768 | ||
8926095f | 11769 | if (RIDBIT_SETP (RID_AUTO, specbits)) |
8251199e | 11770 | error ("storage class `auto' invalid for function `%s'", name); |
8926095f | 11771 | else if (RIDBIT_SETP (RID_REGISTER, specbits)) |
8251199e | 11772 | error ("storage class `register' invalid for function `%s'", name); |
8d08fdba MS |
11773 | |
11774 | /* Function declaration not at top level. | |
11775 | Storage classes other than `extern' are not allowed | |
11776 | and `extern' makes no difference. */ | |
a9aedbc2 | 11777 | if (! toplevel_bindings_p () |
8926095f MS |
11778 | && (RIDBIT_SETP (RID_STATIC, specbits) |
11779 | || RIDBIT_SETP (RID_INLINE, specbits)) | |
8d08fdba | 11780 | && pedantic) |
8926095f MS |
11781 | { |
11782 | if (RIDBIT_SETP (RID_STATIC, specbits)) | |
8251199e | 11783 | pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name); |
8926095f | 11784 | else |
8251199e | 11785 | pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name); |
8926095f | 11786 | } |
68642fb6 | 11787 | |
8d08fdba MS |
11788 | if (ctype == NULL_TREE) |
11789 | { | |
11790 | if (virtualp) | |
11791 | { | |
8251199e | 11792 | error ("virtual non-class function `%s'", name); |
8d08fdba MS |
11793 | virtualp = 0; |
11794 | } | |
8d08fdba MS |
11795 | } |
11796 | else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2) | |
14ae7e7d JM |
11797 | type = build_cplus_method_type (ctype, TREE_TYPE (type), |
11798 | TYPE_ARG_TYPES (type)); | |
8d08fdba | 11799 | |
eb66be0e | 11800 | /* Record presence of `static'. */ |
faae18ab | 11801 | publicp = (ctype != NULL_TREE |
a9aedbc2 | 11802 | || RIDBIT_SETP (RID_EXTERN, specbits) |
eb66be0e | 11803 | || !RIDBIT_SETP (RID_STATIC, specbits)); |
8d08fdba | 11804 | |
386b8a85 | 11805 | decl = grokfndecl (ctype, type, original_name, declarator, |
7a8f9fa9 | 11806 | virtualp, flags, quals, raises, |
75650646 | 11807 | 1, friendp, |
68642fb6 | 11808 | publicp, inlinep, funcdef_flag, |
2c73f9f5 | 11809 | template_count, in_namespace); |
f0e01782 MS |
11810 | if (decl == NULL_TREE) |
11811 | return NULL_TREE; | |
8d08fdba | 11812 | |
8d08fdba MS |
11813 | if (staticp == 1) |
11814 | { | |
11815 | int illegal_static = 0; | |
11816 | ||
11817 | /* Don't allow a static member function in a class, and forbid | |
11818 | declaring main to be static. */ | |
11819 | if (TREE_CODE (type) == METHOD_TYPE) | |
11820 | { | |
33bd39a2 | 11821 | pedwarn ("cannot declare member function `%D' to have static linkage", decl); |
8d08fdba MS |
11822 | illegal_static = 1; |
11823 | } | |
8d08fdba MS |
11824 | else if (current_function_decl) |
11825 | { | |
11826 | /* FIXME need arm citation */ | |
8251199e | 11827 | error ("cannot declare static function inside another function"); |
8d08fdba MS |
11828 | illegal_static = 1; |
11829 | } | |
11830 | ||
11831 | if (illegal_static) | |
11832 | { | |
11833 | staticp = 0; | |
11834 | RIDBIT_RESET (RID_STATIC, specbits); | |
11835 | } | |
11836 | } | |
8d08fdba MS |
11837 | } |
11838 | else | |
11839 | { | |
11840 | /* It's a variable. */ | |
11841 | ||
11842 | /* An uninitialized decl with `extern' is a reference. */ | |
68642fb6 UD |
11843 | decl = grokvardecl (type, declarator, &specbits, |
11844 | initialized, | |
11845 | (type_quals & TYPE_QUAL_CONST) != 0, | |
950ad3c3 | 11846 | in_namespace); |
8d08fdba MS |
11847 | bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE, |
11848 | inlinep, friendp, raises != NULL_TREE); | |
11849 | ||
11850 | if (ctype) | |
11851 | { | |
f0e01782 | 11852 | DECL_CONTEXT (decl) = ctype; |
8d08fdba MS |
11853 | if (staticp == 1) |
11854 | { | |
33bd39a2 | 11855 | pedwarn ("`static' may not be used when defining (as opposed to declaring) a static data member"); |
8d08fdba MS |
11856 | staticp = 0; |
11857 | RIDBIT_RESET (RID_STATIC, specbits); | |
11858 | } | |
b7484fbe MS |
11859 | if (RIDBIT_SETP (RID_REGISTER, specbits) && TREE_STATIC (decl)) |
11860 | { | |
33bd39a2 | 11861 | error ("static member `%D' declared `register'", decl); |
b7484fbe MS |
11862 | RIDBIT_RESET (RID_REGISTER, specbits); |
11863 | } | |
f30432d7 | 11864 | if (RIDBIT_SETP (RID_EXTERN, specbits) && pedantic) |
8d08fdba | 11865 | { |
33bd39a2 | 11866 | pedwarn ("cannot explicitly declare member `%#D' to have extern linkage", |
f30432d7 | 11867 | decl); |
8d08fdba MS |
11868 | RIDBIT_RESET (RID_EXTERN, specbits); |
11869 | } | |
11870 | } | |
11871 | } | |
11872 | ||
34fc957d | 11873 | my_friendly_assert (!RIDBIT_SETP (RID_MUTABLE, specbits), 19990927); |
8d08fdba MS |
11874 | |
11875 | /* Record `register' declaration for warnings on & | |
11876 | and in case doing stupid register allocation. */ | |
11877 | ||
11878 | if (RIDBIT_SETP (RID_REGISTER, specbits)) | |
11879 | DECL_REGISTER (decl) = 1; | |
11880 | ||
8926095f MS |
11881 | if (RIDBIT_SETP (RID_EXTERN, specbits)) |
11882 | DECL_THIS_EXTERN (decl) = 1; | |
11883 | ||
faae18ab MS |
11884 | if (RIDBIT_SETP (RID_STATIC, specbits)) |
11885 | DECL_THIS_STATIC (decl) = 1; | |
11886 | ||
adecb3f4 MM |
11887 | /* Record constancy and volatility. There's no need to do this |
11888 | when processing a template; we'll do this for the instantiated | |
11889 | declaration based on the type of DECL. */ | |
11890 | if (!processing_template_decl) | |
11891 | c_apply_type_quals_to_decl (type_quals, decl); | |
8d08fdba MS |
11892 | |
11893 | return decl; | |
11894 | } | |
11895 | } | |
11896 | \f | |
11897 | /* Tell if a parmlist/exprlist looks like an exprlist or a parmlist. | |
11898 | An empty exprlist is a parmlist. An exprlist which | |
11899 | contains only identifiers at the global level | |
11900 | is a parmlist. Otherwise, it is an exprlist. */ | |
e92cc029 | 11901 | |
8d08fdba MS |
11902 | int |
11903 | parmlist_is_exprlist (exprs) | |
11904 | tree exprs; | |
11905 | { | |
11906 | if (exprs == NULL_TREE || TREE_PARMLIST (exprs)) | |
11907 | return 0; | |
11908 | ||
a9aedbc2 | 11909 | if (toplevel_bindings_p ()) |
8d08fdba MS |
11910 | { |
11911 | /* At the global level, if these are all identifiers, | |
11912 | then it is a parmlist. */ | |
11913 | while (exprs) | |
11914 | { | |
11915 | if (TREE_CODE (TREE_VALUE (exprs)) != IDENTIFIER_NODE) | |
11916 | return 1; | |
11917 | exprs = TREE_CHAIN (exprs); | |
11918 | } | |
11919 | return 0; | |
11920 | } | |
11921 | return 1; | |
11922 | } | |
11923 | ||
f181d4ae MM |
11924 | /* Subroutine of start_function. Ensure that each of the parameter |
11925 | types (as listed in PARMS) is complete, as is required for a | |
11926 | function definition. */ | |
e92cc029 | 11927 | |
8d08fdba MS |
11928 | static void |
11929 | require_complete_types_for_parms (parms) | |
11930 | tree parms; | |
11931 | { | |
07c88314 | 11932 | for (; parms; parms = TREE_CHAIN (parms)) |
8d08fdba | 11933 | { |
5cce22b6 NS |
11934 | if (VOID_TYPE_P (TREE_TYPE (parms))) |
11935 | /* grokparms will have already issued an error */ | |
11936 | TREE_TYPE (parms) = error_mark_node; | |
11937 | else if (complete_type_or_else (TREE_TYPE (parms), parms)) | |
42f989ff | 11938 | layout_decl (parms, 0); |
5cce22b6 NS |
11939 | else |
11940 | TREE_TYPE (parms) = error_mark_node; | |
8d08fdba MS |
11941 | } |
11942 | } | |
11943 | ||
46e8c075 | 11944 | /* Returns non-zero if T is a local variable. */ |
297e73d8 | 11945 | |
46e8c075 MM |
11946 | int |
11947 | local_variable_p (t) | |
11948 | tree t; | |
297e73d8 | 11949 | { |
68642fb6 | 11950 | if ((TREE_CODE (t) == VAR_DECL |
297e73d8 MM |
11951 | /* A VAR_DECL with a context that is a _TYPE is a static data |
11952 | member. */ | |
11953 | && !TYPE_P (CP_DECL_CONTEXT (t)) | |
11954 | /* Any other non-local variable must be at namespace scope. */ | |
46e8c075 | 11955 | && !DECL_NAMESPACE_SCOPE_P (t)) |
297e73d8 | 11956 | || (TREE_CODE (t) == PARM_DECL)) |
46e8c075 | 11957 | return 1; |
297e73d8 | 11958 | |
46e8c075 MM |
11959 | return 0; |
11960 | } | |
11961 | ||
11962 | /* Returns non-zero if T is an automatic local variable or a label. | |
11963 | (These are the declarations that need to be remapped when the code | |
11964 | containing them is duplicated.) */ | |
11965 | ||
11966 | int | |
11967 | nonstatic_local_decl_p (t) | |
11968 | tree t; | |
11969 | { | |
11970 | return ((local_variable_p (t) && !TREE_STATIC (t)) | |
11971 | || TREE_CODE (t) == LABEL_DECL | |
11972 | || TREE_CODE (t) == RESULT_DECL); | |
11973 | } | |
11974 | ||
11975 | /* Like local_variable_p, but suitable for use as a tree-walking | |
11976 | function. */ | |
11977 | ||
11978 | static tree | |
11979 | local_variable_p_walkfn (tp, walk_subtrees, data) | |
11980 | tree *tp; | |
11981 | int *walk_subtrees ATTRIBUTE_UNUSED; | |
11982 | void *data ATTRIBUTE_UNUSED; | |
11983 | { | |
68642fb6 | 11984 | return ((local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp)) |
46e8c075 | 11985 | ? *tp : NULL_TREE); |
297e73d8 MM |
11986 | } |
11987 | ||
11988 | /* Check that ARG, which is a default-argument expression for a | |
11989 | parameter DECL, is legal. Returns ARG, or ERROR_MARK_NODE, if | |
11990 | something goes wrong. DECL may also be a _TYPE node, rather than a | |
11991 | DECL, if there is no DECL available. */ | |
11992 | ||
11993 | tree | |
11994 | check_default_argument (decl, arg) | |
11995 | tree decl; | |
11996 | tree arg; | |
11997 | { | |
11998 | tree var; | |
11999 | tree decl_type; | |
12000 | ||
12001 | if (TREE_CODE (arg) == DEFAULT_ARG) | |
12002 | /* We get a DEFAULT_ARG when looking at an in-class declaration | |
12003 | with a default argument. Ignore the argument for now; we'll | |
12004 | deal with it after the class is complete. */ | |
12005 | return arg; | |
12006 | ||
12007 | if (processing_template_decl || uses_template_parms (arg)) | |
12008 | /* We don't do anything checking until instantiation-time. Note | |
12009 | that there may be uninstantiated arguments even for an | |
12010 | instantiated function, since default arguments are not | |
12011 | instantiated until they are needed. */ | |
12012 | return arg; | |
12013 | ||
12014 | if (TYPE_P (decl)) | |
12015 | { | |
12016 | decl_type = decl; | |
12017 | decl = NULL_TREE; | |
12018 | } | |
12019 | else | |
12020 | decl_type = TREE_TYPE (decl); | |
12021 | ||
68642fb6 | 12022 | if (arg == error_mark_node |
297e73d8 MM |
12023 | || decl == error_mark_node |
12024 | || TREE_TYPE (arg) == error_mark_node | |
12025 | || decl_type == error_mark_node) | |
12026 | /* Something already went wrong. There's no need to check | |
12027 | further. */ | |
12028 | return error_mark_node; | |
12029 | ||
12030 | /* [dcl.fct.default] | |
68642fb6 | 12031 | |
297e73d8 MM |
12032 | A default argument expression is implicitly converted to the |
12033 | parameter type. */ | |
12034 | if (!TREE_TYPE (arg) | |
12035 | || !can_convert_arg (decl_type, TREE_TYPE (arg), arg)) | |
12036 | { | |
12037 | if (decl) | |
33bd39a2 | 12038 | error ("default argument for `%#D' has type `%T'", |
297e73d8 MM |
12039 | decl, TREE_TYPE (arg)); |
12040 | else | |
33bd39a2 | 12041 | error ("default argument for parameter of type `%T' has type `%T'", |
297e73d8 MM |
12042 | decl_type, TREE_TYPE (arg)); |
12043 | ||
12044 | return error_mark_node; | |
12045 | } | |
12046 | ||
12047 | /* [dcl.fct.default] | |
12048 | ||
12049 | Local variables shall not be used in default argument | |
68642fb6 | 12050 | expressions. |
297e73d8 MM |
12051 | |
12052 | The keyword `this' shall not be used in a default argument of a | |
12053 | member function. */ | |
5362b086 | 12054 | var = walk_tree_without_duplicates (&arg, local_variable_p_walkfn, |
ee94fce6 | 12055 | NULL); |
297e73d8 MM |
12056 | if (var) |
12057 | { | |
33bd39a2 | 12058 | error ("default argument `%E' uses local variable `%D'", |
297e73d8 MM |
12059 | arg, var); |
12060 | return error_mark_node; | |
12061 | } | |
12062 | ||
12063 | /* All is well. */ | |
12064 | return arg; | |
12065 | } | |
12066 | ||
8d08fdba MS |
12067 | /* Decode the list of parameter types for a function type. |
12068 | Given the list of things declared inside the parens, | |
12069 | return a list of types. | |
12070 | ||
5cce22b6 NS |
12071 | We determine whether ellipsis parms are used by PARMLIST_ELLIPSIS_P |
12072 | flag. If unset, we append void_list_node. A parmlist declared | |
12073 | as `(void)' is accepted as the empty parmlist. | |
12074 | ||
12075 | Also set last_function_parms to the chain of PARM_DECLs. */ | |
8d08fdba MS |
12076 | |
12077 | static tree | |
5cce22b6 | 12078 | grokparms (first_parm) |
8d08fdba | 12079 | tree first_parm; |
8d08fdba MS |
12080 | { |
12081 | tree result = NULL_TREE; | |
12082 | tree decls = NULL_TREE; | |
5cce22b6 NS |
12083 | int ellipsis = !first_parm || PARMLIST_ELLIPSIS_P (first_parm); |
12084 | tree parm, chain; | |
12085 | int any_error = 0; | |
8d08fdba | 12086 | |
5cce22b6 NS |
12087 | my_friendly_assert (!first_parm || TREE_PARMLIST (first_parm), 20001115); |
12088 | ||
12089 | for (parm = first_parm; parm != NULL_TREE; parm = chain) | |
8d08fdba | 12090 | { |
0657c69c | 12091 | tree type = NULL_TREE; |
98884b26 | 12092 | tree decl = TREE_VALUE (parm); |
5cce22b6 | 12093 | tree init = TREE_PURPOSE (parm); |
98884b26 | 12094 | tree specs, attrs; |
8d08fdba | 12095 | |
5cce22b6 NS |
12096 | chain = TREE_CHAIN (parm); |
12097 | /* @@ weak defense against parse errors. */ | |
12098 | if (TREE_CODE (decl) != VOID_TYPE | |
12099 | && TREE_CODE (decl) != TREE_LIST) | |
8d08fdba | 12100 | { |
5cce22b6 NS |
12101 | /* Give various messages as the need arises. */ |
12102 | if (TREE_CODE (decl) == STRING_CST) | |
33bd39a2 | 12103 | error ("invalid string constant `%E'", decl); |
5cce22b6 NS |
12104 | else if (TREE_CODE (decl) == INTEGER_CST) |
12105 | error ("invalid integer constant in parameter list, did you forget to give parameter name?"); | |
12106 | continue; | |
12107 | } | |
8d08fdba | 12108 | |
5cce22b6 NS |
12109 | if (parm == void_list_node) |
12110 | break; | |
8d08fdba | 12111 | |
98884b26 JM |
12112 | split_specs_attrs (TREE_PURPOSE (decl), &specs, &attrs); |
12113 | decl = grokdeclarator (TREE_VALUE (decl), specs, | |
12114 | PARM, init != NULL_TREE, &attrs); | |
5cce22b6 NS |
12115 | if (! decl || TREE_TYPE (decl) == error_mark_node) |
12116 | continue; | |
d363e7bf | 12117 | |
98884b26 JM |
12118 | if (attrs) |
12119 | cplus_decl_attributes (&decl, attrs, 0); | |
12120 | ||
5cce22b6 NS |
12121 | type = TREE_TYPE (decl); |
12122 | if (VOID_TYPE_P (type)) | |
12123 | { | |
12124 | if (same_type_p (type, void_type_node) | |
12125 | && !DECL_NAME (decl) && !result && !chain && !ellipsis) | |
12126 | /* this is a parmlist of `(void)', which is ok. */ | |
12127 | break; | |
7a228918 | 12128 | cxx_incomplete_type_error (decl, type); |
0657c69c MM |
12129 | /* It's not a good idea to actually create parameters of |
12130 | type `void'; other parts of the compiler assume that a | |
12131 | void type terminates the parameter list. */ | |
04f3dc2b | 12132 | type = error_mark_node; |
0657c69c | 12133 | TREE_TYPE (decl) = error_mark_node; |
5cce22b6 | 12134 | } |
8d08fdba | 12135 | |
d363e7bf | 12136 | if (type != error_mark_node) |
04f3dc2b MM |
12137 | { |
12138 | /* Top-level qualifiers on the parameters are | |
12139 | ignored for function types. */ | |
12140 | type = TYPE_MAIN_VARIANT (type); | |
12141 | if (TREE_CODE (type) == METHOD_TYPE) | |
12142 | { | |
33bd39a2 | 12143 | error ("parameter `%D' invalidly declared method type", decl); |
04f3dc2b MM |
12144 | type = build_pointer_type (type); |
12145 | TREE_TYPE (decl) = type; | |
12146 | } | |
12147 | else if (TREE_CODE (type) == OFFSET_TYPE) | |
12148 | { | |
33bd39a2 | 12149 | error ("parameter `%D' invalidly declared offset type", decl); |
04f3dc2b MM |
12150 | type = build_pointer_type (type); |
12151 | TREE_TYPE (decl) = type; | |
12152 | } | |
12153 | else if (abstract_virtuals_error (decl, type)) | |
12154 | any_error = 1; /* Seems like a good idea. */ | |
12155 | else if (POINTER_TYPE_P (type)) | |
12156 | { | |
12157 | /* [dcl.fct]/6, parameter types cannot contain pointers | |
12158 | (references) to arrays of unknown bound. */ | |
98979fe0 NS |
12159 | tree t = TREE_TYPE (type); |
12160 | int ptr = TYPE_PTR_P (type); | |
12161 | ||
12162 | while (1) | |
12163 | { | |
12164 | if (TYPE_PTR_P (t)) | |
12165 | ptr = 1; | |
12166 | else if (TREE_CODE (t) != ARRAY_TYPE) | |
12167 | break; | |
12168 | else if (!TYPE_DOMAIN (t)) | |
12169 | break; | |
12170 | t = TREE_TYPE (t); | |
12171 | } | |
04f3dc2b | 12172 | if (TREE_CODE (t) == ARRAY_TYPE) |
33bd39a2 | 12173 | error ("parameter `%D' includes %s to array of unknown bound `%T'", |
98979fe0 | 12174 | decl, ptr ? "pointer" : "reference", t); |
04f3dc2b MM |
12175 | } |
12176 | ||
12177 | DECL_ARG_TYPE (decl) = TREE_TYPE (decl); | |
12178 | if (PROMOTE_PROTOTYPES | |
acf97e0b | 12179 | && INTEGRAL_TYPE_P (type) |
04f3dc2b MM |
12180 | && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) |
12181 | DECL_ARG_TYPE (decl) = integer_type_node; | |
12182 | if (!any_error && init) | |
12183 | init = check_default_argument (decl, init); | |
12184 | else | |
12185 | init = NULL_TREE; | |
12186 | } | |
8d08fdba | 12187 | |
5cce22b6 NS |
12188 | TREE_CHAIN (decl) = decls; |
12189 | decls = decl; | |
0657c69c | 12190 | result = tree_cons (init, type, result); |
8d08fdba | 12191 | } |
5cce22b6 NS |
12192 | decls = nreverse (decls); |
12193 | result = nreverse (result); | |
12194 | if (!ellipsis) | |
12195 | result = chainon (result, void_list_node); | |
8d08fdba MS |
12196 | last_function_parms = decls; |
12197 | ||
8d08fdba MS |
12198 | return result; |
12199 | } | |
42976354 | 12200 | |
8d08fdba | 12201 | \f |
271e6f02 NS |
12202 | /* D is a constructor or overloaded `operator='. |
12203 | ||
12204 | Let T be the class in which D is declared. Then, this function | |
12205 | returns: | |
12206 | ||
12207 | -1 if D's is an ill-formed constructor or copy assignment operator | |
12208 | whose first parameter is of type `T'. | |
12209 | 0 if D is not a copy constructor or copy assignment | |
12210 | operator. | |
12211 | 1 if D is a copy constructor or copy assignment operator whose | |
12212 | first parameter is a reference to const qualified T. | |
12213 | 2 if D is a copy constructor or copy assignment operator whose | |
12214 | first parameter is a reference to non-const qualified T. | |
12215 | ||
12216 | This function can be used as a predicate. Positive values indicate | |
12217 | a copy constructor and non-zero values indicate a copy assignment | |
4f1c5b7d MM |
12218 | operator. */ |
12219 | ||
c11b6f21 | 12220 | int |
271e6f02 | 12221 | copy_fn_p (d) |
c11b6f21 MS |
12222 | tree d; |
12223 | { | |
271e6f02 NS |
12224 | tree args; |
12225 | tree arg_type; | |
12226 | int result = 1; | |
12227 | ||
12228 | my_friendly_assert (DECL_FUNCTION_MEMBER_P (d), 20011208); | |
4f1c5b7d | 12229 | |
271e6f02 NS |
12230 | if (DECL_TEMPLATE_INFO (d) && is_member_template (DECL_TI_TEMPLATE (d))) |
12231 | /* Instantiations of template member functions are never copy | |
12232 | functions. Note that member functions of templated classes are | |
12233 | represented as template functions internally, and we must | |
12234 | accept those as copy functions. */ | |
12235 | return 0; | |
12236 | ||
12237 | args = FUNCTION_FIRST_USER_PARMTYPE (d); | |
12238 | if (!args) | |
4f1c5b7d MM |
12239 | return 0; |
12240 | ||
271e6f02 NS |
12241 | arg_type = TREE_VALUE (args); |
12242 | ||
12243 | if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d)) | |
12244 | { | |
12245 | /* Pass by value copy assignment operator. */ | |
12246 | result = -1; | |
12247 | } | |
12248 | else if (TREE_CODE (arg_type) == REFERENCE_TYPE | |
12249 | && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d)) | |
12250 | { | |
12251 | if (CP_TYPE_CONST_P (TREE_TYPE (arg_type))) | |
12252 | result = 2; | |
12253 | } | |
12254 | else | |
12255 | return 0; | |
12256 | ||
12257 | args = TREE_CHAIN (args); | |
12258 | ||
12259 | if (args && args != void_list_node && !TREE_PURPOSE (args)) | |
12260 | /* There are more non-optional args. */ | |
12261 | return 0; | |
12262 | ||
12263 | return result; | |
12264 | } | |
12265 | ||
12266 | /* Remember any special properties of member function DECL. */ | |
12267 | ||
12268 | void grok_special_member_properties (decl) | |
12269 | tree decl; | |
12270 | { | |
12271 | if (!DECL_NONSTATIC_MEMBER_FUNCTION_P(decl)) | |
12272 | ; /* Not special. */ | |
12273 | else if (DECL_CONSTRUCTOR_P (decl)) | |
12274 | { | |
12275 | int ctor = copy_fn_p (decl); | |
12276 | ||
12277 | if (ctor > 0) | |
12278 | { | |
12279 | /* [class.copy] | |
12280 | ||
12281 | A non-template constructor for class X is a copy | |
12282 | constructor if its first parameter is of type X&, const | |
12283 | X&, volatile X& or const volatile X&, and either there | |
12284 | are no other parameters or else all other parameters have | |
12285 | default arguments. */ | |
12286 | TYPE_HAS_INIT_REF (DECL_CONTEXT (decl)) = 1; | |
12287 | if (ctor > 1) | |
12288 | TYPE_HAS_CONST_INIT_REF (DECL_CONTEXT (decl)) = 1; | |
12289 | } | |
12290 | else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl))) | |
12291 | TYPE_HAS_DEFAULT_CONSTRUCTOR (DECL_CONTEXT (decl)) = 1; | |
12292 | } | |
12293 | else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR) | |
12294 | { | |
12295 | /* [class.copy] | |
12296 | ||
12297 | A non-template assignment operator for class X is a copy | |
12298 | assignment operator if its parameter is of type X, X&, const | |
12299 | X&, volatile X& or const volatile X&. */ | |
12300 | ||
12301 | int assop = copy_fn_p (decl); | |
12302 | ||
12303 | if (assop) | |
12304 | { | |
12305 | TYPE_HAS_ASSIGN_REF (DECL_CONTEXT (decl)) = 1; | |
12306 | if (assop != 1) | |
12307 | TYPE_HAS_CONST_ASSIGN_REF (DECL_CONTEXT (decl)) = 1; | |
12308 | if (DECL_PURE_VIRTUAL_P (decl)) | |
12309 | TYPE_HAS_ABSTRACT_ASSIGN_REF (DECL_CONTEXT (decl)) = 1; | |
12310 | } | |
12311 | } | |
c11b6f21 MS |
12312 | } |
12313 | ||
271e6f02 NS |
12314 | /* Check a constructor DECL has the correct form. Complains |
12315 | if the class has a constructor of the form X(X). */ | |
e92cc029 | 12316 | |
a0a33927 | 12317 | int |
8d08fdba MS |
12318 | grok_ctor_properties (ctype, decl) |
12319 | tree ctype, decl; | |
12320 | { | |
271e6f02 NS |
12321 | int ctor_parm = copy_fn_p (decl); |
12322 | ||
12323 | if (ctor_parm < 0) | |
12324 | { | |
12325 | /* [class.copy] | |
12326 | ||
12327 | A declaration of a constructor for a class X is ill-formed if | |
12328 | its first parameter is of type (optionally cv-qualified) X | |
12329 | and either there are no other parameters or else all other | |
12330 | parameters have default arguments. | |
12331 | ||
12332 | We *don't* complain about member template instantiations that | |
12333 | have this form, though; they can occur as we try to decide | |
12334 | what constructor to use during overload resolution. Since | |
12335 | overload resolution will never prefer such a constructor to | |
12336 | the non-template copy constructor (which is either explicitly | |
12337 | or implicitly defined), there's no need to worry about their | |
12338 | existence. Theoretically, they should never even be | |
12339 | instantiated, but that's hard to forestall. */ | |
33bd39a2 | 12340 | error ("invalid constructor; you probably meant `%T (const %T&)'", |
0b41abe6 JM |
12341 | ctype, ctype); |
12342 | SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype); | |
12343 | return 0; | |
8d08fdba | 12344 | } |
271e6f02 | 12345 | |
a0a33927 | 12346 | return 1; |
8d08fdba MS |
12347 | } |
12348 | ||
596ea4e5 | 12349 | /* An operator with this code is unary, but can also be binary. */ |
e92cc029 | 12350 | |
a28e3c7f | 12351 | static int |
596ea4e5 AS |
12352 | ambi_op_p (code) |
12353 | enum tree_code code; | |
8d08fdba | 12354 | { |
596ea4e5 AS |
12355 | return (code == INDIRECT_REF |
12356 | || code == ADDR_EXPR | |
12357 | || code == CONVERT_EXPR | |
12358 | || code == NEGATE_EXPR | |
12359 | || code == PREINCREMENT_EXPR | |
12360 | || code == PREDECREMENT_EXPR); | |
8d08fdba MS |
12361 | } |
12362 | ||
12363 | /* An operator with this name can only be unary. */ | |
e92cc029 | 12364 | |
a28e3c7f | 12365 | static int |
596ea4e5 AS |
12366 | unary_op_p (code) |
12367 | enum tree_code code; | |
8d08fdba | 12368 | { |
596ea4e5 AS |
12369 | return (code == TRUTH_NOT_EXPR |
12370 | || code == BIT_NOT_EXPR | |
12371 | || code == COMPONENT_REF | |
12372 | || code == TYPE_EXPR); | |
8d08fdba MS |
12373 | } |
12374 | ||
12375 | /* Do a little sanity-checking on how they declared their operator. */ | |
e92cc029 | 12376 | |
5566b478 | 12377 | void |
271e6f02 | 12378 | grok_op_properties (decl, friendp) |
8d08fdba | 12379 | tree decl; |
271e6f02 | 12380 | int friendp; |
8d08fdba MS |
12381 | { |
12382 | tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
596ea4e5 | 12383 | tree argtype; |
8d08fdba MS |
12384 | int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE); |
12385 | tree name = DECL_NAME (decl); | |
596ea4e5 AS |
12386 | enum tree_code operator_code; |
12387 | int arity; | |
12388 | ||
12389 | /* Count the number of arguments. */ | |
12390 | for (argtype = argtypes, arity = 0; | |
12391 | argtype && argtype != void_list_node; | |
12392 | argtype = TREE_CHAIN (argtype)) | |
12393 | ++arity; | |
8d08fdba | 12394 | |
a28e3c7f MS |
12395 | if (current_class_type == NULL_TREE) |
12396 | friendp = 1; | |
8d08fdba | 12397 | |
596ea4e5 AS |
12398 | if (DECL_CONV_FN_P (decl)) |
12399 | operator_code = TYPE_EXPR; | |
12400 | else | |
12401 | do | |
12402 | { | |
0c918ce5 MM |
12403 | #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \ |
12404 | if (ansi_opname (CODE) == name) \ | |
12405 | { \ | |
75ac8dec | 12406 | operator_code = (CODE); \ |
0c918ce5 MM |
12407 | break; \ |
12408 | } \ | |
12409 | else if (ansi_assopname (CODE) == name) \ | |
12410 | { \ | |
75ac8dec | 12411 | operator_code = (CODE); \ |
0c918ce5 MM |
12412 | DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \ |
12413 | break; \ | |
596ea4e5 AS |
12414 | } |
12415 | ||
12416 | #include "operators.def" | |
12417 | #undef DEF_OPERATOR | |
12418 | ||
a98facb0 | 12419 | abort (); |
596ea4e5 AS |
12420 | } |
12421 | while (0); | |
12422 | my_friendly_assert (operator_code != LAST_CPLUS_TREE_CODE, 20000526); | |
12423 | SET_OVERLOADED_OPERATOR_CODE (decl, operator_code); | |
12424 | ||
a28e3c7f MS |
12425 | if (! friendp) |
12426 | { | |
596ea4e5 AS |
12427 | switch (operator_code) |
12428 | { | |
12429 | case CALL_EXPR: | |
12430 | TYPE_OVERLOADS_CALL_EXPR (current_class_type) = 1; | |
12431 | break; | |
5362b086 | 12432 | |
596ea4e5 AS |
12433 | case ARRAY_REF: |
12434 | TYPE_OVERLOADS_ARRAY_REF (current_class_type) = 1; | |
12435 | break; | |
f181d4ae | 12436 | |
596ea4e5 AS |
12437 | case COMPONENT_REF: |
12438 | case MEMBER_REF: | |
12439 | TYPE_OVERLOADS_ARROW (current_class_type) = 1; | |
12440 | break; | |
5362b086 | 12441 | |
596ea4e5 AS |
12442 | case NEW_EXPR: |
12443 | TYPE_HAS_NEW_OPERATOR (current_class_type) = 1; | |
12444 | break; | |
5362b086 | 12445 | |
596ea4e5 AS |
12446 | case DELETE_EXPR: |
12447 | TYPE_GETS_DELETE (current_class_type) |= 1; | |
12448 | break; | |
5362b086 | 12449 | |
596ea4e5 AS |
12450 | case VEC_NEW_EXPR: |
12451 | TYPE_HAS_ARRAY_NEW_OPERATOR (current_class_type) = 1; | |
12452 | break; | |
5362b086 | 12453 | |
596ea4e5 AS |
12454 | case VEC_DELETE_EXPR: |
12455 | TYPE_GETS_DELETE (current_class_type) |= 2; | |
12456 | break; | |
12457 | ||
12458 | default: | |
12459 | break; | |
12460 | } | |
12461 | } | |
12462 | ||
12463 | if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR) | |
8d08fdba | 12464 | { |
8d08fdba MS |
12465 | /* When the compiler encounters the definition of A::operator new, it |
12466 | doesn't look at the class declaration to find out if it's static. */ | |
a28e3c7f | 12467 | if (methodp) |
3afb32a4 | 12468 | revert_static_member_fn (decl); |
68642fb6 | 12469 | |
36791f1e | 12470 | TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl)); |
8d08fdba | 12471 | } |
596ea4e5 | 12472 | else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR) |
8d08fdba | 12473 | { |
a28e3c7f | 12474 | if (methodp) |
3afb32a4 | 12475 | revert_static_member_fn (decl); |
68642fb6 | 12476 | |
36791f1e | 12477 | TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl)); |
8d08fdba | 12478 | } |
8d08fdba MS |
12479 | else |
12480 | { | |
12481 | /* An operator function must either be a non-static member function | |
12482 | or have at least one parameter of a class, a reference to a class, | |
12483 | an enumeration, or a reference to an enumeration. 13.4.0.6 */ | |
700f8a87 | 12484 | if (! methodp || DECL_STATIC_FUNCTION_P (decl)) |
8d08fdba | 12485 | { |
596ea4e5 AS |
12486 | if (operator_code == TYPE_EXPR |
12487 | || operator_code == CALL_EXPR | |
12488 | || operator_code == COMPONENT_REF | |
12489 | || operator_code == ARRAY_REF | |
12490 | || operator_code == NOP_EXPR) | |
33bd39a2 | 12491 | error ("`%D' must be a nonstatic member function", decl); |
8d08fdba MS |
12492 | else |
12493 | { | |
12494 | tree p = argtypes; | |
12495 | ||
700f8a87 | 12496 | if (DECL_STATIC_FUNCTION_P (decl)) |
33bd39a2 | 12497 | error ("`%D' must be either a non-static member function or a non-member function", decl); |
700f8a87 | 12498 | |
8d08fdba | 12499 | if (p) |
a1774733 | 12500 | for (; TREE_CODE (TREE_VALUE (p)) != VOID_TYPE ; p = TREE_CHAIN (p)) |
8d08fdba MS |
12501 | { |
12502 | tree arg = TREE_VALUE (p); | |
12503 | if (TREE_CODE (arg) == REFERENCE_TYPE) | |
12504 | arg = TREE_TYPE (arg); | |
12505 | ||
12506 | /* This lets bad template code slip through. */ | |
12507 | if (IS_AGGR_TYPE (arg) | |
12508 | || TREE_CODE (arg) == ENUMERAL_TYPE | |
73b0fce8 | 12509 | || TREE_CODE (arg) == TEMPLATE_TYPE_PARM |
a1281f45 | 12510 | || TREE_CODE (arg) == BOUND_TEMPLATE_TEMPLATE_PARM) |
8d08fdba MS |
12511 | goto foundaggr; |
12512 | } | |
33bd39a2 | 12513 | error |
8251199e | 12514 | ("`%D' must have an argument of class or enumerated type", |
8d08fdba MS |
12515 | decl); |
12516 | foundaggr: | |
12517 | ; | |
12518 | } | |
12519 | } | |
68642fb6 | 12520 | |
596ea4e5 | 12521 | if (operator_code == CALL_EXPR) |
2c73f9f5 | 12522 | return; /* No restrictions on args. */ |
8d08fdba | 12523 | |
9a3b49ac | 12524 | if (IDENTIFIER_TYPENAME_P (name) && ! DECL_TEMPLATE_INFO (decl)) |
a0a33927 MS |
12525 | { |
12526 | tree t = TREE_TYPE (name); | |
17708e90 | 12527 | if (! friendp) |
a0a33927 MS |
12528 | { |
12529 | int ref = (TREE_CODE (t) == REFERENCE_TYPE); | |
d8e178a0 | 12530 | const char *what = 0; |
5362b086 | 12531 | |
a0a33927 MS |
12532 | if (ref) |
12533 | t = TYPE_MAIN_VARIANT (TREE_TYPE (t)); | |
12534 | ||
17708e90 NS |
12535 | if (TREE_CODE (t) == VOID_TYPE) |
12536 | what = "void"; | |
12537 | else if (t == current_class_type) | |
a0a33927 | 12538 | what = "the same type"; |
9a3b49ac | 12539 | /* Don't force t to be complete here. */ |
a0a33927 | 12540 | else if (IS_AGGR_TYPE (t) |
d0f062fb | 12541 | && COMPLETE_TYPE_P (t) |
a0a33927 MS |
12542 | && DERIVED_FROM_P (t, current_class_type)) |
12543 | what = "a base class"; | |
12544 | ||
12545 | if (what) | |
8251199e | 12546 | warning ("conversion to %s%s will never use a type conversion operator", |
a0a33927 MS |
12547 | ref ? "a reference to " : "", what); |
12548 | } | |
12549 | } | |
271e6f02 | 12550 | if (operator_code == COND_EXPR) |
8d08fdba MS |
12551 | { |
12552 | /* 13.4.0.3 */ | |
33bd39a2 | 12553 | error ("ISO C++ prohibits overloading operator ?:"); |
68642fb6 | 12554 | } |
596ea4e5 | 12555 | else if (ambi_op_p (operator_code)) |
8d08fdba | 12556 | { |
596ea4e5 AS |
12557 | if (arity == 1) |
12558 | /* We pick the one-argument operator codes by default, so | |
12559 | we don't have to change anything. */ | |
12560 | ; | |
12561 | else if (arity == 2) | |
8d08fdba | 12562 | { |
596ea4e5 AS |
12563 | /* If we thought this was a unary operator, we now know |
12564 | it to be a binary operator. */ | |
12565 | switch (operator_code) | |
12566 | { | |
12567 | case INDIRECT_REF: | |
12568 | operator_code = MULT_EXPR; | |
12569 | break; | |
12570 | ||
12571 | case ADDR_EXPR: | |
12572 | operator_code = BIT_AND_EXPR; | |
12573 | break; | |
12574 | ||
12575 | case CONVERT_EXPR: | |
12576 | operator_code = PLUS_EXPR; | |
12577 | break; | |
12578 | ||
12579 | case NEGATE_EXPR: | |
12580 | operator_code = MINUS_EXPR; | |
12581 | break; | |
12582 | ||
12583 | case PREINCREMENT_EXPR: | |
12584 | operator_code = POSTINCREMENT_EXPR; | |
12585 | break; | |
12586 | ||
12587 | case PREDECREMENT_EXPR: | |
655dc6ee | 12588 | operator_code = POSTDECREMENT_EXPR; |
596ea4e5 AS |
12589 | break; |
12590 | ||
12591 | default: | |
a98facb0 | 12592 | abort (); |
596ea4e5 AS |
12593 | } |
12594 | ||
12595 | SET_OVERLOADED_OPERATOR_CODE (decl, operator_code); | |
12596 | ||
12597 | if ((operator_code == POSTINCREMENT_EXPR | |
12598 | || operator_code == POSTDECREMENT_EXPR) | |
5156628f | 12599 | && ! processing_template_decl |
007e5fea | 12600 | && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node)) |
8d08fdba MS |
12601 | { |
12602 | if (methodp) | |
33bd39a2 | 12603 | error ("postfix `%D' must take `int' as its argument", |
8d08fdba MS |
12604 | decl); |
12605 | else | |
33bd39a2 | 12606 | error |
8251199e JM |
12607 | ("postfix `%D' must take `int' as its second argument", |
12608 | decl); | |
8d08fdba MS |
12609 | } |
12610 | } | |
12611 | else | |
12612 | { | |
12613 | if (methodp) | |
33bd39a2 | 12614 | error ("`%D' must take either zero or one argument", decl); |
8d08fdba | 12615 | else |
33bd39a2 | 12616 | error ("`%D' must take either one or two arguments", decl); |
8d08fdba | 12617 | } |
824b9a4c MS |
12618 | |
12619 | /* More Effective C++ rule 6. */ | |
eb448459 | 12620 | if (warn_ecpp |
596ea4e5 AS |
12621 | && (operator_code == POSTINCREMENT_EXPR |
12622 | || operator_code == POSTDECREMENT_EXPR | |
12623 | || operator_code == PREINCREMENT_EXPR | |
12624 | || operator_code == PREDECREMENT_EXPR)) | |
824b9a4c MS |
12625 | { |
12626 | tree arg = TREE_VALUE (argtypes); | |
12627 | tree ret = TREE_TYPE (TREE_TYPE (decl)); | |
12628 | if (methodp || TREE_CODE (arg) == REFERENCE_TYPE) | |
12629 | arg = TREE_TYPE (arg); | |
12630 | arg = TYPE_MAIN_VARIANT (arg); | |
596ea4e5 AS |
12631 | if (operator_code == PREINCREMENT_EXPR |
12632 | || operator_code == PREDECREMENT_EXPR) | |
824b9a4c MS |
12633 | { |
12634 | if (TREE_CODE (ret) != REFERENCE_TYPE | |
3bfdc719 MM |
12635 | || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), |
12636 | arg)) | |
33bd39a2 | 12637 | warning ("prefix `%D' should return `%T'", decl, |
824b9a4c MS |
12638 | build_reference_type (arg)); |
12639 | } | |
12640 | else | |
12641 | { | |
3bfdc719 | 12642 | if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg)) |
33bd39a2 | 12643 | warning ("postfix `%D' should return `%T'", decl, arg); |
824b9a4c MS |
12644 | } |
12645 | } | |
8d08fdba | 12646 | } |
596ea4e5 | 12647 | else if (unary_op_p (operator_code)) |
8d08fdba | 12648 | { |
596ea4e5 | 12649 | if (arity != 1) |
8d08fdba MS |
12650 | { |
12651 | if (methodp) | |
33bd39a2 | 12652 | error ("`%D' must take `void'", decl); |
8d08fdba | 12653 | else |
33bd39a2 | 12654 | error ("`%D' must take exactly one argument", decl); |
8d08fdba MS |
12655 | } |
12656 | } | |
596ea4e5 | 12657 | else /* if (binary_op_p (operator_code)) */ |
8d08fdba | 12658 | { |
596ea4e5 | 12659 | if (arity != 2) |
8d08fdba MS |
12660 | { |
12661 | if (methodp) | |
33bd39a2 | 12662 | error ("`%D' must take exactly one argument", decl); |
8d08fdba | 12663 | else |
33bd39a2 | 12664 | error ("`%D' must take exactly two arguments", decl); |
8d08fdba | 12665 | } |
824b9a4c MS |
12666 | |
12667 | /* More Effective C++ rule 7. */ | |
eb448459 | 12668 | if (warn_ecpp |
596ea4e5 AS |
12669 | && (operator_code == TRUTH_ANDIF_EXPR |
12670 | || operator_code == TRUTH_ORIF_EXPR | |
12671 | || operator_code == COMPOUND_EXPR)) | |
33bd39a2 | 12672 | warning ("user-defined `%D' always evaluates both arguments", |
824b9a4c MS |
12673 | decl); |
12674 | } | |
12675 | ||
12676 | /* Effective C++ rule 23. */ | |
eb448459 | 12677 | if (warn_ecpp |
596ea4e5 AS |
12678 | && arity == 2 |
12679 | && (operator_code == PLUS_EXPR | |
12680 | || operator_code == MINUS_EXPR | |
12681 | || operator_code == TRUNC_DIV_EXPR | |
12682 | || operator_code == MULT_EXPR) | |
824b9a4c | 12683 | && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE) |
33bd39a2 | 12684 | warning ("`%D' should return by value", decl); |
8d08fdba | 12685 | |
271e6f02 | 12686 | /* [over.oper]/8 */ |
34332678 CT |
12687 | for (; argtypes && argtypes != void_list_node; |
12688 | argtypes = TREE_CHAIN (argtypes)) | |
12689 | if (TREE_PURPOSE (argtypes)) | |
12690 | { | |
12691 | TREE_PURPOSE (argtypes) = NULL_TREE; | |
596ea4e5 AS |
12692 | if (operator_code == POSTINCREMENT_EXPR |
12693 | || operator_code == POSTDECREMENT_EXPR) | |
34332678 CT |
12694 | { |
12695 | if (pedantic) | |
33bd39a2 | 12696 | pedwarn ("`%D' cannot have default arguments", decl); |
34332678 CT |
12697 | } |
12698 | else | |
33bd39a2 | 12699 | error ("`%D' cannot have default arguments", decl); |
34332678 CT |
12700 | } |
12701 | ||
8d08fdba MS |
12702 | } |
12703 | } | |
12704 | \f | |
d8e178a0 | 12705 | static const char * |
094fe153 JM |
12706 | tag_name (code) |
12707 | enum tag_types code; | |
12708 | { | |
12709 | switch (code) | |
12710 | { | |
12711 | case record_type: | |
12712 | return "struct"; | |
12713 | case class_type: | |
12714 | return "class"; | |
12715 | case union_type: | |
12716 | return "union "; | |
12717 | case enum_type: | |
12718 | return "enum"; | |
094fe153 | 12719 | default: |
a98facb0 | 12720 | abort (); |
094fe153 JM |
12721 | } |
12722 | } | |
12723 | ||
8d08fdba MS |
12724 | /* Get the struct, enum or union (CODE says which) with tag NAME. |
12725 | Define the tag as a forward-reference if it is not defined. | |
12726 | ||
12727 | C++: If a class derivation is given, process it here, and report | |
12728 | an error if multiple derivation declarations are not identical. | |
12729 | ||
12730 | If this is a definition, come in through xref_tag and only look in | |
12731 | the current frame for the name (since C++ allows new names in any | |
12732 | scope.) */ | |
12733 | ||
8d08fdba | 12734 | tree |
ca107ded | 12735 | xref_tag (code_type_node, name, globalize) |
8d08fdba | 12736 | tree code_type_node; |
ca107ded | 12737 | tree name; |
8d08fdba MS |
12738 | int globalize; |
12739 | { | |
12740 | enum tag_types tag_code; | |
12741 | enum tree_code code; | |
8d08fdba | 12742 | register tree ref, t; |
8f032717 | 12743 | struct binding_level *b = current_binding_level; |
dc8263bc | 12744 | tree attributes = NULL_TREE; |
25aab5d0 | 12745 | tree context = NULL_TREE; |
dc8263bc JM |
12746 | |
12747 | /* If we are called from the parser, code_type_node will sometimes be a | |
12748 | TREE_LIST. This indicates that the user wrote | |
12749 | "class __attribute__ ((foo)) bar". Extract the attributes so we can | |
12750 | use them later. */ | |
12751 | if (TREE_CODE (code_type_node) == TREE_LIST) | |
12752 | { | |
12753 | attributes = TREE_PURPOSE (code_type_node); | |
12754 | code_type_node = TREE_VALUE (code_type_node); | |
12755 | } | |
8d08fdba | 12756 | |
665f2503 | 12757 | tag_code = (enum tag_types) tree_low_cst (code_type_node, 1); |
8d08fdba MS |
12758 | switch (tag_code) |
12759 | { | |
12760 | case record_type: | |
12761 | case class_type: | |
8d08fdba | 12762 | code = RECORD_TYPE; |
8d08fdba MS |
12763 | break; |
12764 | case union_type: | |
12765 | code = UNION_TYPE; | |
8d08fdba MS |
12766 | break; |
12767 | case enum_type: | |
12768 | code = ENUMERAL_TYPE; | |
12769 | break; | |
12770 | default: | |
a98facb0 | 12771 | abort (); |
8d08fdba MS |
12772 | } |
12773 | ||
12774 | /* If a cross reference is requested, look up the type | |
12775 | already defined for this tag and return it. */ | |
2f939d94 | 12776 | if (TYPE_P (name)) |
be99da77 MS |
12777 | { |
12778 | t = name; | |
a80e4195 | 12779 | name = TYPE_IDENTIFIER (t); |
be99da77 MS |
12780 | } |
12781 | else | |
12782 | t = IDENTIFIER_TYPE_VALUE (name); | |
61a127b3 | 12783 | |
f2ae0c45 JM |
12784 | /* Warn about 'friend struct Inherited;' doing the wrong thing. */ |
12785 | if (t && globalize && TREE_CODE (t) == TYPENAME_TYPE) | |
12786 | { | |
12787 | static int explained; | |
2b0cbc5d NS |
12788 | tree shadowed; |
12789 | ||
33bd39a2 | 12790 | warning ("`%s %T' declares a new type at namespace scope", |
2b0cbc5d NS |
12791 | tag_name (tag_code), name); |
12792 | if (!explained++) | |
33bd39a2 | 12793 | 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 |
12794 | tag_name (tag_code), |
12795 | constructor_name (current_class_type), | |
12796 | TYPE_IDENTIFIER (t)); | |
12797 | ||
12798 | /* We need to remove the class scope binding for the | |
12799 | TYPENAME_TYPE as otherwise poplevel_class gets confused. */ | |
12800 | for (shadowed = b->class_shadowed; | |
12801 | shadowed; | |
12802 | shadowed = TREE_CHAIN (shadowed)) | |
12803 | if (TREE_TYPE (shadowed) == TYPE_NAME (t)) | |
12804 | { | |
12805 | TREE_PURPOSE (shadowed) = NULL_TREE; | |
12806 | break; | |
12807 | } | |
f2ae0c45 JM |
12808 | } |
12809 | ||
73b0fce8 | 12810 | if (t && TREE_CODE (t) != code && TREE_CODE (t) != TEMPLATE_TYPE_PARM |
a1281f45 | 12811 | && TREE_CODE (t) != BOUND_TEMPLATE_TEMPLATE_PARM) |
a0a33927 MS |
12812 | t = NULL_TREE; |
12813 | ||
8ccc31eb | 12814 | if (! globalize) |
8d08fdba | 12815 | { |
f3400fe2 JM |
12816 | /* If we know we are defining this tag, only look it up in |
12817 | this scope and don't try to find it as a type. */ | |
12818 | ref = lookup_tag (code, name, b, 1); | |
8d08fdba MS |
12819 | } |
12820 | else | |
12821 | { | |
25aab5d0 | 12822 | if (t) |
36a117a5 | 12823 | { |
4782bd5b RS |
12824 | ref = follow_tag_typedef (t); |
12825 | ||
25aab5d0 MM |
12826 | /* [dcl.type.elab] If the identifier resolves to a |
12827 | typedef-name or a template type-parameter, the | |
12828 | elaborated-type-specifier is ill-formed. */ | |
4782bd5b RS |
12829 | if (!ref) |
12830 | { | |
12831 | pedwarn ("using typedef-name `%D' after `%s'", | |
12832 | TYPE_NAME (t), tag_name (tag_code)); | |
12833 | ref = t; | |
12834 | } | |
25aab5d0 | 12835 | else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM) |
33bd39a2 | 12836 | error ("using template type parameter `%T' after `%s'", |
4782bd5b | 12837 | t, tag_name (tag_code)); |
25aab5d0 MM |
12838 | } |
12839 | else | |
12840 | ref = lookup_tag (code, name, b, 0); | |
68642fb6 | 12841 | |
25aab5d0 MM |
12842 | if (! ref) |
12843 | { | |
12844 | /* Try finding it as a type declaration. If that wins, | |
68642fb6 | 12845 | use it. */ |
25aab5d0 MM |
12846 | ref = lookup_name (name, 1); |
12847 | ||
12848 | if (ref != NULL_TREE | |
12849 | && processing_template_decl | |
12850 | && DECL_CLASS_TEMPLATE_P (ref) | |
12851 | && template_class_depth (current_class_type) == 0) | |
12852 | /* Since GLOBALIZE is true, we're declaring a global | |
6757edfe | 12853 | template, so we want this type. */ |
17aec3eb | 12854 | ref = DECL_TEMPLATE_RESULT (ref); |
6757edfe | 12855 | |
25aab5d0 MM |
12856 | if (ref && TREE_CODE (ref) == TYPE_DECL |
12857 | && TREE_CODE (TREE_TYPE (ref)) == code) | |
12858 | ref = TREE_TYPE (ref); | |
12859 | else | |
12860 | ref = NULL_TREE; | |
12861 | } | |
12862 | ||
68642fb6 UD |
12863 | if (ref && current_class_type |
12864 | && template_class_depth (current_class_type) | |
12865 | && PROCESSING_REAL_TEMPLATE_DECL_P ()) | |
25aab5d0 MM |
12866 | { |
12867 | /* Since GLOBALIZE is non-zero, we are not looking at a | |
12868 | definition of this tag. Since, in addition, we are currently | |
12869 | processing a (member) template declaration of a template | |
12870 | class, we must be very careful; consider: | |
12871 | ||
12872 | template <class X> | |
12873 | struct S1 | |
12874 | ||
12875 | template <class U> | |
12876 | struct S2 | |
12877 | { template <class V> | |
12878 | friend struct S1; }; | |
12879 | ||
12880 | Here, the S2::S1 declaration should not be confused with the | |
12881 | outer declaration. In particular, the inner version should | |
12882 | have a template parameter of level 2, not level 1. This | |
12883 | would be particularly important if the member declaration | |
12884 | were instead: | |
12885 | ||
12886 | template <class V = U> friend struct S1; | |
12887 | ||
12888 | say, when we should tsubst into `U' when instantiating | |
12889 | S2. On the other hand, when presented with: | |
12890 | ||
12891 | template <class T> | |
12892 | struct S1 { | |
12893 | template <class U> | |
12894 | struct S2 {}; | |
12895 | template <class U> | |
12896 | friend struct S2; | |
12897 | }; | |
12898 | ||
12899 | we must find the inner binding eventually. We | |
12900 | accomplish this by making sure that the new type we | |
12901 | create to represent this declaration has the right | |
12902 | TYPE_CONTEXT. */ | |
12903 | context = TYPE_CONTEXT (ref); | |
12904 | ref = NULL_TREE; | |
8d08fdba MS |
12905 | } |
12906 | } | |
12907 | ||
8d08fdba MS |
12908 | if (! ref) |
12909 | { | |
12910 | /* If no such tag is yet defined, create a forward-reference node | |
12911 | and record it as the "definition". | |
12912 | When a real declaration of this type is found, | |
12913 | the forward-reference will be altered into a real type. */ | |
8d08fdba MS |
12914 | if (code == ENUMERAL_TYPE) |
12915 | { | |
33bd39a2 | 12916 | error ("use of enum `%#D' without previous declaration", name); |
fc378698 | 12917 | |
8d08fdba MS |
12918 | ref = make_node (ENUMERAL_TYPE); |
12919 | ||
12920 | /* Give the type a default layout like unsigned int | |
12921 | to avoid crashing if it does not get defined. */ | |
12922 | TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node); | |
12923 | TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node); | |
11cf4d18 | 12924 | TYPE_USER_ALIGN (ref) = 0; |
8d08fdba MS |
12925 | TREE_UNSIGNED (ref) = 1; |
12926 | TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node); | |
12927 | TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node); | |
12928 | TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node); | |
12929 | ||
12930 | /* Enable us to recognize when a type is created in class context. | |
12931 | To do nested classes correctly, this should probably be cleared | |
12932 | out when we leave this classes scope. Currently this in only | |
12933 | done in `start_enum'. */ | |
12934 | ||
12935 | pushtag (name, ref, globalize); | |
8d08fdba | 12936 | } |
8d08fdba MS |
12937 | else |
12938 | { | |
8d08fdba | 12939 | struct binding_level *old_b = class_binding_level; |
8d08fdba | 12940 | |
33848bb0 | 12941 | ref = make_aggr_type (code); |
25aab5d0 | 12942 | TYPE_CONTEXT (ref) = context; |
8d08fdba | 12943 | |
8d08fdba MS |
12944 | #ifdef NONNESTED_CLASSES |
12945 | /* Class types don't nest the way enums do. */ | |
12946 | class_binding_level = (struct binding_level *)0; | |
12947 | #endif | |
12948 | pushtag (name, ref, globalize); | |
12949 | class_binding_level = old_b; | |
8d08fdba MS |
12950 | } |
12951 | } | |
12952 | else | |
12953 | { | |
7fe6899f | 12954 | if (!globalize && processing_template_decl && IS_AGGR_TYPE (ref)) |
6757edfe | 12955 | redeclare_class_template (ref, current_template_parms); |
8d08fdba MS |
12956 | } |
12957 | ||
a588fe25 | 12958 | TYPE_ATTRIBUTES (ref) = attributes; |
dc8263bc | 12959 | |
8d08fdba MS |
12960 | return ref; |
12961 | } | |
8ccc31eb | 12962 | |
fc378698 MS |
12963 | tree |
12964 | xref_tag_from_type (old, id, globalize) | |
12965 | tree old, id; | |
12966 | int globalize; | |
12967 | { | |
12968 | tree code_type_node; | |
12969 | ||
12970 | if (TREE_CODE (old) == RECORD_TYPE) | |
12971 | code_type_node = (CLASSTYPE_DECLARED_CLASS (old) | |
12972 | ? class_type_node : record_type_node); | |
12973 | else | |
12974 | code_type_node = union_type_node; | |
12975 | ||
12976 | if (id == NULL_TREE) | |
12977 | id = TYPE_IDENTIFIER (old); | |
12978 | ||
ca107ded | 12979 | return xref_tag (code_type_node, id, globalize); |
fc378698 MS |
12980 | } |
12981 | ||
3fd71a52 MM |
12982 | /* REF is a type (named NAME), for which we have just seen some |
12983 | baseclasses. BINFO is a list of those baseclasses; the | |
12984 | TREE_PURPOSE is an access_* node, and the TREE_VALUE is the type of | |
12985 | the base-class. CODE_TYPE_NODE indicates whether REF is a class, | |
12986 | struct, or union. */ | |
12987 | ||
8ccc31eb MS |
12988 | void |
12989 | xref_basetypes (code_type_node, name, ref, binfo) | |
12990 | tree code_type_node; | |
9780c24f JM |
12991 | tree name ATTRIBUTE_UNUSED; |
12992 | tree ref; | |
8ccc31eb MS |
12993 | tree binfo; |
12994 | { | |
12995 | /* In the declaration `A : X, Y, ... Z' we mark all the types | |
12996 | (A, X, Y, ..., Z) so we can check for duplicates. */ | |
12997 | tree binfos; | |
d6479fe7 MM |
12998 | tree base; |
12999 | ||
8ccc31eb | 13000 | int i, len; |
591382c4 | 13001 | enum tag_types tag_code; |
a5d7c4a3 MK |
13002 | |
13003 | /* If we are called from the parser, code_type_node will sometimes be a | |
13004 | TREE_LIST. This indicates that the user wrote | |
13005 | "class __attribute__ ((foo)) bar". Extract the attributes so that | |
13006 | tree_low_cst doesn't crash. */ | |
13007 | if (TREE_CODE (code_type_node) == TREE_LIST) | |
13008 | code_type_node = TREE_VALUE (code_type_node); | |
13009 | ||
591382c4 | 13010 | tag_code = (enum tag_types) tree_low_cst (code_type_node, 1); |
8ccc31eb MS |
13011 | |
13012 | if (tag_code == union_type) | |
13013 | { | |
33bd39a2 | 13014 | error ("derived union `%T' invalid", ref); |
8ccc31eb MS |
13015 | return; |
13016 | } | |
13017 | ||
13018 | len = list_length (binfo); | |
8ccc31eb | 13019 | |
d6479fe7 MM |
13020 | /* First, make sure that any templates in base-classes are |
13021 | instantiated. This ensures that if we call ourselves recursively | |
13022 | we do not get confused about which classes are marked and which | |
13023 | are not. */ | |
13024 | for (base = binfo; base; base = TREE_CHAIN (base)) | |
13025 | complete_type (TREE_VALUE (base)); | |
13026 | ||
8ccc31eb MS |
13027 | SET_CLASSTYPE_MARKED (ref); |
13028 | BINFO_BASETYPES (TYPE_BINFO (ref)) = binfos = make_tree_vec (len); | |
13029 | ||
13030 | for (i = 0; binfo; binfo = TREE_CHAIN (binfo)) | |
13031 | { | |
13032 | /* The base of a derived struct is public by default. */ | |
13033 | int via_public | |
be99da77 MS |
13034 | = (TREE_PURPOSE (binfo) == access_public_node |
13035 | || TREE_PURPOSE (binfo) == access_public_virtual_node | |
8ccc31eb | 13036 | || (tag_code != class_type |
be99da77 MS |
13037 | && (TREE_PURPOSE (binfo) == access_default_node |
13038 | || TREE_PURPOSE (binfo) == access_default_virtual_node))); | |
d8b55a76 JM |
13039 | int via_protected |
13040 | = (TREE_PURPOSE (binfo) == access_protected_node | |
13041 | || TREE_PURPOSE (binfo) == access_protected_virtual_node); | |
8ccc31eb | 13042 | int via_virtual |
be99da77 | 13043 | = (TREE_PURPOSE (binfo) == access_private_virtual_node |
d8b55a76 | 13044 | || TREE_PURPOSE (binfo) == access_protected_virtual_node |
be99da77 MS |
13045 | || TREE_PURPOSE (binfo) == access_public_virtual_node |
13046 | || TREE_PURPOSE (binfo) == access_default_virtual_node); | |
13047 | tree basetype = TREE_VALUE (binfo); | |
8ccc31eb MS |
13048 | tree base_binfo; |
13049 | ||
8ccc31eb MS |
13050 | if (basetype && TREE_CODE (basetype) == TYPE_DECL) |
13051 | basetype = TREE_TYPE (basetype); | |
5566b478 MS |
13052 | if (!basetype |
13053 | || (TREE_CODE (basetype) != RECORD_TYPE | |
a80e4195 | 13054 | && TREE_CODE (basetype) != TYPENAME_TYPE |
73b0fce8 | 13055 | && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM |
a1281f45 | 13056 | && TREE_CODE (basetype) != BOUND_TEMPLATE_TEMPLATE_PARM)) |
8ccc31eb | 13057 | { |
33bd39a2 | 13058 | error ("base type `%T' fails to be a struct or class type", |
8ccc31eb MS |
13059 | TREE_VALUE (binfo)); |
13060 | continue; | |
13061 | } | |
2b9dc906 | 13062 | |
8adf5b5e JM |
13063 | /* This code replaces similar code in layout_basetypes. |
13064 | We put the complete_type first for implicit `typename'. */ | |
d0f062fb | 13065 | if (!COMPLETE_TYPE_P (basetype) |
2b9dc906 | 13066 | && ! (current_template_parms && uses_template_parms (basetype))) |
8ccc31eb | 13067 | { |
33bd39a2 | 13068 | error ("base class `%T' has incomplete type", basetype); |
8ccc31eb MS |
13069 | continue; |
13070 | } | |
8ccc31eb MS |
13071 | else |
13072 | { | |
13073 | if (CLASSTYPE_MARKED (basetype)) | |
13074 | { | |
13075 | if (basetype == ref) | |
33bd39a2 | 13076 | error ("recursive type `%T' undefined", basetype); |
8ccc31eb | 13077 | else |
33bd39a2 | 13078 | error ("duplicate base type `%T' invalid", basetype); |
8ccc31eb MS |
13079 | continue; |
13080 | } | |
13081 | ||
eff71ab0 | 13082 | if (TYPE_FOR_JAVA (basetype) |
46ccf50a | 13083 | && (current_lang_depth () == 0)) |
eff71ab0 PB |
13084 | TYPE_FOR_JAVA (ref) = 1; |
13085 | ||
8ccc31eb MS |
13086 | /* Note that the BINFO records which describe individual |
13087 | inheritances are *not* shared in the lattice! They | |
13088 | cannot be shared because a given baseclass may be | |
13089 | inherited with different `accessibility' by different | |
13090 | derived classes. (Each BINFO record describing an | |
13091 | individual inheritance contains flags which say what | |
13092 | the `accessibility' of that particular inheritance is.) */ | |
68642fb6 UD |
13093 | |
13094 | base_binfo | |
fed3cef0 | 13095 | = make_binfo (size_zero_node, basetype, |
7ddedda4 MM |
13096 | CLASS_TYPE_P (basetype) |
13097 | ? TYPE_BINFO_VTABLE (basetype) : NULL_TREE, | |
13098 | CLASS_TYPE_P (basetype) | |
13099 | ? TYPE_BINFO_VIRTUALS (basetype) : NULL_TREE); | |
68642fb6 | 13100 | |
8ccc31eb MS |
13101 | TREE_VEC_ELT (binfos, i) = base_binfo; |
13102 | TREE_VIA_PUBLIC (base_binfo) = via_public; | |
13103 | TREE_VIA_PROTECTED (base_binfo) = via_protected; | |
13104 | TREE_VIA_VIRTUAL (base_binfo) = via_virtual; | |
13105 | BINFO_INHERITANCE_CHAIN (base_binfo) = TYPE_BINFO (ref); | |
13106 | ||
dfbcd65a JM |
13107 | /* We need to unshare the binfos now so that lookups during class |
13108 | definition work. */ | |
13109 | unshare_base_binfos (base_binfo); | |
13110 | ||
8ccc31eb | 13111 | SET_CLASSTYPE_MARKED (basetype); |
9e9ff709 | 13112 | |
8ccc31eb MS |
13113 | /* We are free to modify these bits because they are meaningless |
13114 | at top level, and BASETYPE is a top-level type. */ | |
13115 | if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype)) | |
13116 | { | |
13117 | TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1; | |
4c6b7393 MM |
13118 | /* Converting to a virtual base class requires looking |
13119 | up the offset of the virtual base. */ | |
13120 | TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1; | |
8ccc31eb MS |
13121 | } |
13122 | ||
7ddedda4 MM |
13123 | if (CLASS_TYPE_P (basetype)) |
13124 | { | |
5362b086 | 13125 | TYPE_HAS_NEW_OPERATOR (ref) |
834c6dff | 13126 | |= TYPE_HAS_NEW_OPERATOR (basetype); |
5362b086 | 13127 | TYPE_HAS_ARRAY_NEW_OPERATOR (ref) |
834c6dff | 13128 | |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype); |
7ddedda4 | 13129 | TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype); |
4c6b7393 | 13130 | /* If the base-class uses multiple inheritance, so do we. */ |
68642fb6 | 13131 | TYPE_USES_MULTIPLE_INHERITANCE (ref) |
4c6b7393 MM |
13132 | |= TYPE_USES_MULTIPLE_INHERITANCE (basetype); |
13133 | /* Likewise, if converting to a base of the base may require | |
13134 | code, then we may need to generate code to convert to a | |
13135 | base as well. */ | |
68642fb6 | 13136 | TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) |
4c6b7393 | 13137 | |= TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype); |
7ddedda4 MM |
13138 | } |
13139 | ||
8ccc31eb MS |
13140 | i += 1; |
13141 | } | |
13142 | } | |
13143 | if (i) | |
13144 | TREE_VEC_LENGTH (binfos) = i; | |
13145 | else | |
13146 | BINFO_BASETYPES (TYPE_BINFO (ref)) = NULL_TREE; | |
13147 | ||
13148 | if (i > 1) | |
7ddedda4 | 13149 | { |
4c6b7393 MM |
13150 | TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1; |
13151 | /* If there is more than one non-empty they cannot be at the same | |
13152 | address. */ | |
13153 | TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1; | |
7ddedda4 MM |
13154 | } |
13155 | ||
8ccc31eb MS |
13156 | /* Unmark all the types. */ |
13157 | while (--i >= 0) | |
13158 | CLEAR_CLASSTYPE_MARKED (BINFO_TYPE (TREE_VEC_ELT (binfos, i))); | |
13159 | CLEAR_CLASSTYPE_MARKED (ref); | |
13160 | ||
70c532b5 MM |
13161 | /* Now that we know all the base-classes, set up the list of virtual |
13162 | bases. */ | |
23381155 | 13163 | get_vbase_types (ref); |
8ccc31eb | 13164 | } |
68642fb6 | 13165 | |
8d08fdba | 13166 | \f |
8d08fdba MS |
13167 | /* Begin compiling the definition of an enumeration type. |
13168 | NAME is its name (or null if anonymous). | |
13169 | Returns the type object, as yet incomplete. | |
13170 | Also records info about it so that build_enumerator | |
13171 | may be used to declare the individual values as they are read. */ | |
13172 | ||
13173 | tree | |
13174 | start_enum (name) | |
13175 | tree name; | |
13176 | { | |
13177 | register tree enumtype = NULL_TREE; | |
8f032717 | 13178 | struct binding_level *b = current_binding_level; |
8d08fdba MS |
13179 | |
13180 | /* If this is the real definition for a previous forward reference, | |
13181 | fill in the contents in the same object that used to be the | |
13182 | forward reference. */ | |
13183 | ||
13184 | if (name != NULL_TREE) | |
13185 | enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1); | |
13186 | ||
13187 | if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE) | |
30ff8252 | 13188 | { |
33bd39a2 | 13189 | error ("multiple definition of `%#T'", enumtype); |
30ff8252 | 13190 | cp_error_at ("previous definition here", enumtype); |
58595203 MM |
13191 | /* Clear out TYPE_VALUES, and start again. */ |
13192 | TYPE_VALUES (enumtype) = NULL_TREE; | |
30ff8252 | 13193 | } |
8d08fdba MS |
13194 | else |
13195 | { | |
13196 | enumtype = make_node (ENUMERAL_TYPE); | |
13197 | pushtag (name, enumtype, 0); | |
13198 | } | |
13199 | ||
8d08fdba MS |
13200 | return enumtype; |
13201 | } | |
13202 | ||
13203 | /* After processing and defining all the values of an enumeration type, | |
13204 | install their decls in the enumeration type and finish it off. | |
968b956a | 13205 | ENUMTYPE is the type object and VALUES a list of name-value pairs. */ |
8d08fdba | 13206 | |
968b956a | 13207 | void |
219670f1 | 13208 | finish_enum (enumtype) |
dbfe2124 | 13209 | tree enumtype; |
8d08fdba | 13210 | { |
968b956a MM |
13211 | tree pair; |
13212 | tree minnode; | |
13213 | tree maxnode; | |
13214 | tree t; | |
13215 | bool unsignedp; | |
13216 | int lowprec; | |
13217 | int highprec; | |
13218 | int precision; | |
13219 | ||
13220 | /* We built up the VALUES in reverse order. */ | |
13221 | TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype)); | |
13222 | ||
13223 | /* [dcl.enum] | |
13224 | ||
13225 | Following the closing brace of an enum-specifier, each | |
13226 | enumerator has the type of its enumeration. Prior to the | |
13227 | closing brace, the type of each enumerator is the type of | |
13228 | its initializing value. */ | |
13229 | for (pair = TYPE_VALUES (enumtype); pair; pair = TREE_CHAIN (pair)) | |
13230 | TREE_TYPE (TREE_VALUE (pair)) = enumtype; | |
13231 | ||
13232 | /* For a enum defined in a template, all further processing is | |
13233 | postponed until the template is instantiated. */ | |
13234 | if (processing_template_decl) | |
13235 | { | |
13236 | tree scope = current_scope (); | |
13237 | if (scope && TREE_CODE (scope) == FUNCTION_DECL) | |
13238 | add_stmt (build_min (TAG_DEFN, enumtype)); | |
8d08fdba | 13239 | |
0ba8a114 | 13240 | |
968b956a MM |
13241 | return; |
13242 | } | |
13243 | ||
13244 | /* Figure out what the minimum and maximum values of the enumerators | |
13245 | are. */ | |
13246 | if (TYPE_VALUES (enumtype)) | |
8d08fdba | 13247 | { |
968b956a | 13248 | minnode = maxnode = NULL_TREE; |
5566b478 | 13249 | |
968b956a MM |
13250 | for (pair = TYPE_VALUES (enumtype); |
13251 | pair; | |
13252 | pair = TREE_CHAIN (pair)) | |
8d08fdba | 13253 | { |
ed44da02 MM |
13254 | tree value; |
13255 | ||
968b956a | 13256 | value = DECL_INITIAL (TREE_VALUE (pair)); |
ed44da02 | 13257 | |
968b956a MM |
13258 | if (!minnode) |
13259 | minnode = maxnode = value; | |
13260 | else if (tree_int_cst_lt (maxnode, value)) | |
13261 | maxnode = value; | |
13262 | else if (tree_int_cst_lt (value, minnode)) | |
13263 | minnode = value; | |
8d08fdba MS |
13264 | } |
13265 | } | |
f376e137 | 13266 | else |
968b956a MM |
13267 | minnode = maxnode = integer_zero_node; |
13268 | ||
13269 | /* Compute the number of bits require to represent all values of the | |
13270 | enumeration. We must do this before the type of MINNODE and | |
13271 | MAXNODE are transformed, since min_precision relies on the | |
13272 | TREE_TYPE of the value it is passed. */ | |
13273 | unsignedp = tree_int_cst_sgn (minnode) >= 0; | |
13274 | lowprec = min_precision (minnode, unsignedp); | |
13275 | highprec = min_precision (maxnode, unsignedp); | |
13276 | precision = MAX (lowprec, highprec); | |
13277 | ||
13278 | /* Set the TREE_TYPE for the values as well. That's so that when we | |
13279 | call decl_constant_value we get an entity of the right type (but | |
13280 | with the constant value). In addition, transform the TYPE_VALUES | |
13281 | list to contain the values, rather than the CONST_DECLs for them. */ | |
13282 | for (pair = TYPE_VALUES (enumtype); pair; pair = TREE_CHAIN (pair)) | |
13283 | { | |
13284 | tree value = DECL_INITIAL (TREE_VALUE (pair)); | |
13285 | ||
13286 | TREE_TYPE (value) = enumtype; | |
13287 | TREE_VALUE (pair) = value; | |
13288 | } | |
13289 | ||
13290 | /* Set TYPE_MIN_VALUE and TYPE_MAX_VALUE according to `precision'. */ | |
13291 | TYPE_SIZE (enumtype) = NULL_TREE; | |
13292 | TYPE_PRECISION (enumtype) = precision; | |
13293 | if (unsignedp) | |
13294 | fixup_unsigned_type (enumtype); | |
cbf882af | 13295 | else |
968b956a | 13296 | fixup_signed_type (enumtype); |
8d08fdba | 13297 | |
219670f1 BE |
13298 | if (flag_short_enums || (precision > TYPE_PRECISION (integer_type_node))) |
13299 | /* Use the width of the narrowest normal C type which is wide | |
13300 | enough. */ | |
b0c48229 | 13301 | TYPE_PRECISION (enumtype) = TYPE_PRECISION (c_common_type_for_size |
219670f1 | 13302 | (precision, 1)); |
968b956a MM |
13303 | else |
13304 | TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node); | |
68642fb6 | 13305 | |
968b956a MM |
13306 | TYPE_SIZE (enumtype) = NULL_TREE; |
13307 | layout_type (enumtype); | |
8d08fdba | 13308 | |
968b956a MM |
13309 | /* Fix up all variant types of this enum type. */ |
13310 | for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t)) | |
13311 | { | |
13312 | TYPE_VALUES (t) = TYPE_VALUES (enumtype); | |
13313 | TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (enumtype); | |
13314 | TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (enumtype); | |
13315 | TYPE_SIZE (t) = TYPE_SIZE (enumtype); | |
13316 | TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (enumtype); | |
13317 | TYPE_MODE (t) = TYPE_MODE (enumtype); | |
13318 | TYPE_PRECISION (t) = TYPE_PRECISION (enumtype); | |
13319 | TYPE_ALIGN (t) = TYPE_ALIGN (enumtype); | |
13320 | TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (enumtype); | |
13321 | TREE_UNSIGNED (t) = TREE_UNSIGNED (enumtype); | |
cbf882af MM |
13322 | } |
13323 | ||
968b956a MM |
13324 | /* Finish debugging output for this type. */ |
13325 | rest_of_type_compilation (enumtype, namespace_bindings_p ()); | |
8d08fdba MS |
13326 | } |
13327 | ||
079e1098 | 13328 | /* Build and install a CONST_DECL for an enumeration constant of the |
58595203 | 13329 | enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided. |
8d08fdba MS |
13330 | Assignment of sequential values by default is handled here. */ |
13331 | ||
58595203 MM |
13332 | void |
13333 | build_enumerator (name, value, enumtype) | |
079e1098 MM |
13334 | tree name; |
13335 | tree value; | |
58595203 | 13336 | tree enumtype; |
8d08fdba | 13337 | { |
58595203 | 13338 | tree decl; |
e8bd800e | 13339 | tree context; |
58595203 MM |
13340 | tree type; |
13341 | tree values; | |
8d08fdba MS |
13342 | |
13343 | /* Remove no-op casts from the value. */ | |
13344 | if (value) | |
13345 | STRIP_TYPE_NOPS (value); | |
13346 | ||
58595203 MM |
13347 | if (! processing_template_decl) |
13348 | { | |
13349 | /* Validate and default VALUE. */ | |
13350 | if (value != NULL_TREE) | |
13351 | { | |
fc611ce0 | 13352 | value = decl_constant_value (value); |
58595203 MM |
13353 | |
13354 | if (TREE_CODE (value) == INTEGER_CST) | |
13355 | { | |
13356 | value = default_conversion (value); | |
13357 | constant_expression_warning (value); | |
13358 | } | |
13359 | else | |
13360 | { | |
33bd39a2 | 13361 | error ("enumerator value for `%D' not integer constant", name); |
58595203 MM |
13362 | value = NULL_TREE; |
13363 | } | |
13364 | } | |
13365 | ||
13366 | /* Default based on previous value. */ | |
13367 | if (value == NULL_TREE && ! processing_template_decl) | |
13368 | { | |
13369 | tree prev_value; | |
13370 | ||
13371 | if (TYPE_VALUES (enumtype)) | |
13372 | { | |
13373 | /* The next value is the previous value ... */ | |
13374 | prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype))); | |
13375 | /* ... plus one. */ | |
ab76ca54 MM |
13376 | value = cp_build_binary_op (PLUS_EXPR, |
13377 | prev_value, | |
13378 | integer_one_node); | |
68642fb6 | 13379 | |
58595203 | 13380 | if (tree_int_cst_lt (value, prev_value)) |
33bd39a2 | 13381 | error ("overflow in enumeration values at `%D'", name); |
58595203 MM |
13382 | } |
13383 | else | |
13384 | value = integer_zero_node; | |
13385 | } | |
13386 | ||
13387 | /* Remove no-op casts from the value. */ | |
13388 | if (value) | |
13389 | STRIP_TYPE_NOPS (value); | |
013bc8af | 13390 | #if 0 |
58595203 MM |
13391 | /* To fix MAX_VAL enum consts. (bkoz) */ |
13392 | TREE_TYPE (value) = integer_type_node; | |
013bc8af | 13393 | #endif |
58595203 | 13394 | } |
8d08fdba | 13395 | |
58595203 MM |
13396 | /* We always have to copy here; not all INTEGER_CSTs are unshared. |
13397 | Even in other cases, we will later (in finish_enum) be setting | |
13398 | the type of VALUE. But, we don't need to make a copy if this | |
13399 | VALUE is one of the enumeration constants for this same | |
13400 | enumeration type. */ | |
13401 | for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values)) | |
13402 | if (TREE_VALUE (values) == value) | |
13403 | break; | |
13404 | /* If we didn't break out of the loop, then we do need a copy. */ | |
13405 | if (!values && value) | |
13406 | value = copy_node (value); | |
ed44da02 | 13407 | |
8d08fdba | 13408 | /* C++ associates enums with global, function, or class declarations. */ |
58595203 MM |
13409 | context = current_scope (); |
13410 | ||
13411 | /* Build the actual enumeration constant. Note that the enumeration | |
13412 | constants have the type of their initializers until the | |
13413 | enumeration is complete: | |
13414 | ||
13415 | [ dcl.enum ] | |
13416 | ||
13417 | Following the closing brace of an enum-specifier, each enumer- | |
13418 | ator has the type of its enumeration. Prior to the closing | |
13419 | brace, the type of each enumerator is the type of its | |
13420 | initializing value. | |
13421 | ||
13422 | In finish_enum we will reset the type. Of course, if we're | |
13423 | processing a template, there may be no value. */ | |
13424 | type = value ? TREE_TYPE (value) : NULL_TREE; | |
13425 | ||
13426 | if (context && context == current_class_type) | |
13427 | /* This enum declaration is local to the class. We need the full | |
8f17b5c5 | 13428 | lang_decl so that we can record DECL_CLASS_CONTEXT, for example. */ |
58595203 MM |
13429 | decl = build_lang_decl (CONST_DECL, name, type); |
13430 | else | |
13431 | /* It's a global enum, or it's local to a function. (Note local to | |
e8bd800e | 13432 | a function could mean local to a class method. */ |
58595203 | 13433 | decl = build_decl (CONST_DECL, name, type); |
e8bd800e | 13434 | |
58595203 MM |
13435 | DECL_CONTEXT (decl) = FROB_CONTEXT (context); |
13436 | DECL_INITIAL (decl) = value; | |
13437 | TREE_READONLY (decl) = 1; | |
e8bd800e | 13438 | |
58595203 MM |
13439 | if (context && context == current_class_type) |
13440 | /* In something like `struct S { enum E { i = 7 }; };' we put `i' | |
8f032717 MM |
13441 | on the TYPE_FIELDS list for `S'. (That's so that you can say |
13442 | things like `S::i' later.) */ | |
58595203 MM |
13443 | finish_member_declaration (decl); |
13444 | else | |
9780c24f | 13445 | pushdecl (decl); |
58595203 MM |
13446 | |
13447 | /* Add this enumeration constant to the list for this type. */ | |
13448 | TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype)); | |
8d08fdba MS |
13449 | } |
13450 | ||
8d08fdba | 13451 | \f |
a8f73d4b MM |
13452 | /* We're defining DECL. Make sure that it's type is OK. */ |
13453 | ||
13454 | static void | |
f444e36b | 13455 | check_function_type (decl, current_function_parms) |
a8f73d4b | 13456 | tree decl; |
f444e36b | 13457 | tree current_function_parms; |
a8f73d4b MM |
13458 | { |
13459 | tree fntype = TREE_TYPE (decl); | |
d0f062fb | 13460 | tree return_type = complete_type (TREE_TYPE (fntype)); |
a8f73d4b MM |
13461 | |
13462 | /* In a function definition, arg types must be complete. */ | |
13463 | require_complete_types_for_parms (current_function_parms); | |
13464 | ||
d0f062fb | 13465 | if (!COMPLETE_OR_VOID_TYPE_P (return_type)) |
a8f73d4b | 13466 | { |
33bd39a2 | 13467 | error ("return type `%#T' is incomplete", TREE_TYPE (fntype)); |
a8f73d4b MM |
13468 | |
13469 | /* Make it return void instead, but don't change the | |
13470 | type of the DECL_RESULT, in case we have a named return value. */ | |
13471 | if (TREE_CODE (fntype) == METHOD_TYPE) | |
13472 | { | |
13473 | tree ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype))); | |
13474 | TREE_TYPE (decl) | |
13475 | = build_cplus_method_type (ctype, | |
13476 | void_type_node, | |
13477 | FUNCTION_ARG_CHAIN (decl)); | |
13478 | } | |
13479 | else | |
13480 | TREE_TYPE (decl) | |
13481 | = build_function_type (void_type_node, | |
13482 | TYPE_ARG_TYPES (TREE_TYPE (decl))); | |
68642fb6 | 13483 | TREE_TYPE (decl) |
a8f73d4b MM |
13484 | = build_exception_variant (fntype, |
13485 | TYPE_RAISES_EXCEPTIONS (fntype)); | |
13486 | } | |
13487 | else | |
13488 | abstract_virtuals_error (decl, TREE_TYPE (fntype)); | |
13489 | } | |
13490 | ||
8d08fdba MS |
13491 | /* Create the FUNCTION_DECL for a function definition. |
13492 | DECLSPECS and DECLARATOR are the parts of the declaration; | |
13493 | they describe the function's name and the type it returns, | |
13494 | but twisted together in a fashion that parallels the syntax of C. | |
13495 | ||
a8f73d4b MM |
13496 | FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the |
13497 | DECLARATOR is really the DECL for the function we are about to | |
13498 | process and that DECLSPECS should be ignored), SF_INCLASS_INLINE | |
f444e36b | 13499 | indicating that the function is an inline defined in-class. |
68642fb6 | 13500 | |
8d08fdba MS |
13501 | This function creates a binding context for the function body |
13502 | as well as setting up the FUNCTION_DECL in current_function_decl. | |
13503 | ||
13504 | Returns 1 on success. If the DECLARATOR is not suitable for a function | |
13505 | (it defines a datum instead), we return 0, which tells | |
13506 | yyparse to report a parse error. | |
13507 | ||
13508 | For C++, we must first check whether that datum makes any sense. | |
13509 | For example, "class A local_a(1,2);" means that variable local_a | |
13510 | is an aggregate of type A, which should have a constructor | |
87e3dbc9 | 13511 | applied to it with the argument list [1, 2]. */ |
8d08fdba MS |
13512 | |
13513 | int | |
a8f73d4b | 13514 | start_function (declspecs, declarator, attrs, flags) |
c11b6f21 | 13515 | tree declspecs, declarator, attrs; |
a8f73d4b | 13516 | int flags; |
8d08fdba | 13517 | { |
5566b478 | 13518 | tree decl1; |
8d08fdba MS |
13519 | tree ctype = NULL_TREE; |
13520 | tree fntype; | |
13521 | tree restype; | |
13522 | extern int have_extern_spec; | |
13523 | extern int used_extern_spec; | |
13524 | int doing_friend = 0; | |
a8f73d4b | 13525 | struct binding_level *bl; |
f444e36b | 13526 | tree current_function_parms; |
8d08fdba | 13527 | |
8d08fdba | 13528 | /* Sanity check. */ |
a1774733 | 13529 | my_friendly_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE, 160); |
8d08fdba MS |
13530 | my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161); |
13531 | ||
e92cc029 | 13532 | /* This should only be done once on the top most decl. */ |
8d08fdba MS |
13533 | if (have_extern_spec && !used_extern_spec) |
13534 | { | |
1f8f4a0b | 13535 | declspecs = tree_cons (NULL_TREE, get_identifier ("extern"), declspecs); |
8d08fdba MS |
13536 | used_extern_spec = 1; |
13537 | } | |
13538 | ||
a8f73d4b | 13539 | if (flags & SF_PRE_PARSED) |
8d08fdba MS |
13540 | { |
13541 | decl1 = declarator; | |
13542 | ||
8d08fdba MS |
13543 | fntype = TREE_TYPE (decl1); |
13544 | if (TREE_CODE (fntype) == METHOD_TYPE) | |
13545 | ctype = TYPE_METHOD_BASETYPE (fntype); | |
13546 | ||
cab1f180 ML |
13547 | /* ISO C++ 11.4/5. A friend function defined in a class is in |
13548 | the (lexical) scope of the class in which it is defined. */ | |
8d08fdba MS |
13549 | if (!ctype && DECL_FRIEND_P (decl1)) |
13550 | { | |
4f1c5b7d | 13551 | ctype = DECL_FRIEND_CONTEXT (decl1); |
8d08fdba MS |
13552 | |
13553 | /* CTYPE could be null here if we're dealing with a template; | |
13554 | for example, `inline friend float foo()' inside a template | |
13555 | will have no CTYPE set. */ | |
13556 | if (ctype && TREE_CODE (ctype) != RECORD_TYPE) | |
13557 | ctype = NULL_TREE; | |
13558 | else | |
13559 | doing_friend = 1; | |
13560 | } | |
13561 | ||
5566b478 | 13562 | last_function_parms = DECL_ARGUMENTS (decl1); |
8d08fdba MS |
13563 | } |
13564 | else | |
13565 | { | |
91d231cb | 13566 | decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL); |
8d08fdba MS |
13567 | /* If the declarator is not suitable for a function definition, |
13568 | cause a syntax error. */ | |
e89a6075 JM |
13569 | if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL) |
13570 | return 0; | |
13571 | ||
13572 | cplus_decl_attributes (&decl1, attrs, 0); | |
8d08fdba | 13573 | |
ecb0eece RH |
13574 | /* If #pragma weak was used, mark the decl weak now. */ |
13575 | if (current_binding_level == global_binding_level) | |
13576 | maybe_apply_pragma_weak (decl1); | |
13577 | ||
8d08fdba MS |
13578 | fntype = TREE_TYPE (decl1); |
13579 | ||
13580 | restype = TREE_TYPE (fntype); | |
7ddedda4 | 13581 | if (CLASS_TYPE_P (restype) && !CLASSTYPE_GOT_SEMICOLON (restype)) |
8d08fdba | 13582 | { |
33bd39a2 | 13583 | error ("semicolon missing after declaration of `%#T'", restype); |
051e6fd7 | 13584 | shadow_tag (build_tree_list (NULL_TREE, restype)); |
8d08fdba MS |
13585 | CLASSTYPE_GOT_SEMICOLON (restype) = 1; |
13586 | if (TREE_CODE (fntype) == FUNCTION_TYPE) | |
13587 | fntype = build_function_type (integer_type_node, | |
13588 | TYPE_ARG_TYPES (fntype)); | |
13589 | else | |
13590 | fntype = build_cplus_method_type (build_type_variant (TYPE_METHOD_BASETYPE (fntype), TREE_READONLY (decl1), TREE_SIDE_EFFECTS (decl1)), | |
13591 | integer_type_node, | |
13592 | TYPE_ARG_TYPES (fntype)); | |
13593 | TREE_TYPE (decl1) = fntype; | |
13594 | } | |
13595 | ||
13596 | if (TREE_CODE (fntype) == METHOD_TYPE) | |
13597 | ctype = TYPE_METHOD_BASETYPE (fntype); | |
35680744 | 13598 | else if (DECL_MAIN_P (decl1)) |
8d08fdba MS |
13599 | { |
13600 | /* If this doesn't return integer_type, complain. */ | |
13601 | if (TREE_TYPE (TREE_TYPE (decl1)) != integer_type_node) | |
13602 | { | |
a28e3c7f | 13603 | if (pedantic || warn_return_type) |
8251199e | 13604 | pedwarn ("return type for `main' changed to `int'"); |
8d08fdba MS |
13605 | TREE_TYPE (decl1) = fntype = default_function_type; |
13606 | } | |
8d08fdba MS |
13607 | } |
13608 | } | |
68642fb6 | 13609 | |
97055d5c AO |
13610 | if (DECL_DECLARED_INLINE_P (decl1) |
13611 | && lookup_attribute ("noinline", attrs)) | |
13612 | warning_with_decl (decl1, | |
13613 | "inline function `%s' given attribute noinline"); | |
13614 | ||
5f6eeeb3 NS |
13615 | if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1)) |
13616 | /* This is a constructor, we must ensure that any default args | |
13617 | introduced by this definition are propagated to the clones | |
13618 | now. The clones are used directly in overload resolution. */ | |
13619 | adjust_clone_args (decl1); | |
13620 | ||
b35d4555 MM |
13621 | /* Sometimes we don't notice that a function is a static member, and |
13622 | build a METHOD_TYPE for it. Fix that up now. */ | |
13623 | if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1) | |
13624 | && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE) | |
13625 | { | |
3afb32a4 | 13626 | revert_static_member_fn (decl1); |
b35d4555 MM |
13627 | last_function_parms = TREE_CHAIN (last_function_parms); |
13628 | ctype = NULL_TREE; | |
13629 | } | |
8d08fdba MS |
13630 | |
13631 | /* Warn if function was previously implicitly declared | |
13632 | (but not if we warned then). */ | |
13633 | if (! warn_implicit | |
13634 | && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE) | |
8251199e | 13635 | cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1))); |
8d08fdba | 13636 | |
f181d4ae MM |
13637 | /* Set up current_class_type, and enter the scope of the class, if |
13638 | appropriate. */ | |
13639 | if (ctype) | |
13640 | push_nested_class (ctype, 1); | |
13641 | else if (DECL_STATIC_FUNCTION_P (decl1)) | |
13642 | push_nested_class (DECL_CONTEXT (decl1), 2); | |
13643 | ||
13644 | /* Now that we have entered the scope of the class, we must restore | |
13645 | the bindings for any template parameters surrounding DECL1, if it | |
13646 | is an inline member template. (Order is important; consider the | |
13647 | case where a template parameter has the same name as a field of | |
13648 | the class.) It is not until after this point that | |
13649 | PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */ | |
a8f73d4b | 13650 | if (flags & SF_INCLASS_INLINE) |
f181d4ae MM |
13651 | maybe_begin_member_template_processing (decl1); |
13652 | ||
56cb9733 | 13653 | /* Effective C++ rule 15. */ |
9188c363 | 13654 | if (warn_ecpp |
596ea4e5 | 13655 | && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR |
9188c363 | 13656 | && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE) |
33bd39a2 | 13657 | warning ("`operator=' should return a reference to `*this'"); |
9188c363 MM |
13658 | |
13659 | /* Make the init_value nonzero so pushdecl knows this is not tentative. | |
13660 | error_mark_node is replaced below (in poplevel) with the BLOCK. */ | |
b35d4555 MM |
13661 | if (!DECL_INITIAL (decl1)) |
13662 | DECL_INITIAL (decl1) = error_mark_node; | |
9188c363 | 13663 | |
9188c363 MM |
13664 | /* This function exists in static storage. |
13665 | (This does not mean `static' in the C sense!) */ | |
13666 | TREE_STATIC (decl1) = 1; | |
13667 | ||
13668 | /* We must call push_template_decl after current_class_type is set | |
13669 | up. (If we are processing inline definitions after exiting a | |
13670 | class scope, current_class_type will be NULL_TREE until set above | |
13671 | by push_nested_class.) */ | |
13672 | if (processing_template_decl) | |
13673 | decl1 = push_template_decl (decl1); | |
13674 | ||
f181d4ae | 13675 | /* We are now in the scope of the function being defined. */ |
8d08fdba | 13676 | current_function_decl = decl1; |
f181d4ae | 13677 | |
5566b478 MS |
13678 | /* Save the parm names or decls from this function's declarator |
13679 | where store_parm_decls will find them. */ | |
13680 | current_function_parms = last_function_parms; | |
8d08fdba | 13681 | |
a8f73d4b MM |
13682 | /* Make sure the parameter and return types are reasonable. When |
13683 | you declare a function, these types can be incomplete, but they | |
13684 | must be complete when you define the function. */ | |
5156628f | 13685 | if (! processing_template_decl) |
f444e36b | 13686 | check_function_type (decl1, current_function_parms); |
f181d4ae | 13687 | |
a8f73d4b MM |
13688 | /* Build the return declaration for the function. */ |
13689 | restype = TREE_TYPE (fntype); | |
e89a6075 JM |
13690 | /* Promote the value to int before returning it. */ |
13691 | if (c_promoting_integer_type_p (restype)) | |
13692 | restype = type_promotes_to (restype); | |
13693 | if (DECL_RESULT (decl1) == NULL_TREE) | |
a8f73d4b | 13694 | { |
e89a6075 JM |
13695 | DECL_RESULT (decl1) |
13696 | = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype)); | |
13697 | c_apply_type_quals_to_decl (cp_type_quals (restype), | |
13698 | DECL_RESULT (decl1)); | |
5566b478 | 13699 | } |
a8f73d4b MM |
13700 | |
13701 | /* Initialize RTL machinery. We cannot do this until | |
13702 | CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this | |
13703 | even when processing a template; this is how we get | |
c00996a3 JM |
13704 | CFUN set up, and our per-function variables initialized. |
13705 | FIXME factor out the non-RTL stuff. */ | |
a8f73d4b MM |
13706 | bl = current_binding_level; |
13707 | init_function_start (decl1, input_filename, lineno); | |
13708 | current_binding_level = bl; | |
a8f73d4b MM |
13709 | |
13710 | /* Even though we're inside a function body, we still don't want to | |
13711 | call expand_expr to calculate the size of a variable-sized array. | |
13712 | We haven't necessarily assigned RTL to all variables yet, so it's | |
13713 | not safe to try to expand expressions involving them. */ | |
13714 | immediate_size_expand = 0; | |
01d939e8 | 13715 | cfun->x_dont_save_pending_sizes_p = 1; |
a8f73d4b | 13716 | |
f444e36b MM |
13717 | /* Start the statement-tree, start the tree now. */ |
13718 | begin_stmt_tree (&DECL_SAVED_TREE (decl1)); | |
6f80451c | 13719 | |
a8f73d4b | 13720 | /* Let the user know we're compiling this function. */ |
ea11ca7e | 13721 | announce_function (decl1); |
b7484fbe | 13722 | |
878cd289 MS |
13723 | /* Record the decl so that the function name is defined. |
13724 | If we already have a decl for this name, and it is a FUNCTION_DECL, | |
13725 | use the old decl. */ | |
a8f73d4b | 13726 | if (!processing_template_decl && !(flags & SF_PRE_PARSED)) |
878cd289 | 13727 | { |
75650646 | 13728 | /* A specialization is not used to guide overload resolution. */ |
2228d450 | 13729 | if (!DECL_TEMPLATE_SPECIALIZATION (decl1) |
959d8796 | 13730 | && ! DECL_FUNCTION_MEMBER_P (decl1)) |
75650646 | 13731 | decl1 = pushdecl (decl1); |
2c73f9f5 | 13732 | else |
b7698cf0 JM |
13733 | { |
13734 | /* We need to set the DECL_CONTEXT. */ | |
13735 | if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1)) | |
13736 | DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1)); | |
13737 | /* And make sure we have enough default args. */ | |
13738 | check_default_args (decl1); | |
13739 | } | |
878cd289 MS |
13740 | fntype = TREE_TYPE (decl1); |
13741 | } | |
5566b478 | 13742 | |
a8f73d4b | 13743 | /* Reset these in case the call to pushdecl changed them. */ |
5566b478 | 13744 | current_function_decl = decl1; |
01d939e8 | 13745 | cfun->decl = decl1; |
878cd289 | 13746 | |
78c120b5 MM |
13747 | /* If we are (erroneously) defining a function that we have already |
13748 | defined before, wipe out what we knew before. */ | |
5362b086 | 13749 | if (!DECL_PENDING_INLINE_P (decl1) |
78c120b5 MM |
13750 | && DECL_SAVED_FUNCTION_DATA (decl1)) |
13751 | { | |
13752 | free (DECL_SAVED_FUNCTION_DATA (decl1)); | |
13753 | DECL_SAVED_FUNCTION_DATA (decl1) = NULL; | |
13754 | } | |
b35d4555 | 13755 | |
f444e36b | 13756 | if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1)) |
b35d4555 MM |
13757 | { |
13758 | /* We know that this was set up by `grokclassfn'. We do not | |
13759 | wait until `store_parm_decls', since evil parse errors may | |
13760 | never get us to that point. Here we keep the consistency | |
13761 | between `current_class_type' and `current_class_ptr'. */ | |
13762 | tree t = DECL_ARGUMENTS (decl1); | |
68642fb6 UD |
13763 | |
13764 | my_friendly_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL, | |
b35d4555 MM |
13765 | 162); |
13766 | my_friendly_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE, | |
13767 | 19990811); | |
68642fb6 UD |
13768 | |
13769 | cp_function_chain->x_current_class_ref | |
3e411c3f | 13770 | = build_indirect_ref (t, NULL); |
b35d4555 MM |
13771 | cp_function_chain->x_current_class_ptr = t; |
13772 | ||
018fc244 MM |
13773 | /* Constructors and destructors need to know whether they're "in |
13774 | charge" of initializing virtual base classes. */ | |
e0fff4b3 | 13775 | t = TREE_CHAIN (t); |
454fa7a7 | 13776 | if (DECL_HAS_IN_CHARGE_PARM_P (decl1)) |
e0fff4b3 JM |
13777 | { |
13778 | current_in_charge_parm = t; | |
13779 | t = TREE_CHAIN (t); | |
13780 | } | |
13781 | if (DECL_HAS_VTT_PARM_P (decl1)) | |
13782 | { | |
13783 | if (DECL_NAME (t) != vtt_parm_identifier) | |
13784 | abort (); | |
13785 | current_vtt_parm = t; | |
13786 | } | |
b35d4555 MM |
13787 | } |
13788 | ||
db5ae43f | 13789 | if (DECL_INTERFACE_KNOWN (decl1)) |
faae18ab | 13790 | { |
4f1c5b7d | 13791 | tree ctx = decl_function_context (decl1); |
86052cc3 | 13792 | |
faae18ab MS |
13793 | if (DECL_NOT_REALLY_EXTERN (decl1)) |
13794 | DECL_EXTERNAL (decl1) = 0; | |
86052cc3 | 13795 | |
79065db2 | 13796 | if (ctx != NULL_TREE && DECL_DECLARED_INLINE_P (ctx) |
86052cc3 JM |
13797 | && TREE_PUBLIC (ctx)) |
13798 | /* This is a function in a local class in an extern inline | |
13799 | function. */ | |
13800 | comdat_linkage (decl1); | |
faae18ab | 13801 | } |
8d08fdba MS |
13802 | /* If this function belongs to an interface, it is public. |
13803 | If it belongs to someone else's interface, it is also external. | |
1f901793 | 13804 | This only affects inlines and template instantiations. */ |
5566b478 MS |
13805 | else if (interface_unknown == 0 |
13806 | && (! DECL_TEMPLATE_INSTANTIATION (decl1) | |
d11ad92e | 13807 | || flag_alt_external_templates)) |
8d08fdba | 13808 | { |
79065db2 MM |
13809 | if (DECL_DECLARED_INLINE_P (decl1) |
13810 | || DECL_TEMPLATE_INSTANTIATION (decl1) | |
5156628f | 13811 | || processing_template_decl) |
1f901793 JM |
13812 | { |
13813 | DECL_EXTERNAL (decl1) | |
13814 | = (interface_only | |
79065db2 MM |
13815 | || (DECL_DECLARED_INLINE_P (decl1) |
13816 | && ! flag_implement_inlines | |
9c73ec84 | 13817 | && !DECL_VINDEX (decl1))); |
1f901793 JM |
13818 | |
13819 | /* For WIN32 we also want to put these in linkonce sections. */ | |
13820 | maybe_make_one_only (decl1); | |
13821 | } | |
db5ae43f | 13822 | else |
893de33c | 13823 | DECL_EXTERNAL (decl1) = 0; |
e8abc66f | 13824 | DECL_NOT_REALLY_EXTERN (decl1) = 0; |
db5ae43f | 13825 | DECL_INTERFACE_KNOWN (decl1) = 1; |
8d08fdba | 13826 | } |
c16c47fb JM |
13827 | else if (interface_unknown && interface_only |
13828 | && (! DECL_TEMPLATE_INSTANTIATION (decl1) | |
13829 | || flag_alt_external_templates)) | |
13830 | { | |
13831 | /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma | |
13832 | interface, we will have interface_only set but not | |
13833 | interface_known. In that case, we don't want to use the normal | |
13834 | heuristics because someone will supply a #pragma implementation | |
13835 | elsewhere, and deducing it here would produce a conflict. */ | |
13836 | comdat_linkage (decl1); | |
13837 | DECL_EXTERNAL (decl1) = 0; | |
13838 | DECL_INTERFACE_KNOWN (decl1) = 1; | |
13839 | DECL_DEFER_OUTPUT (decl1) = 1; | |
13840 | } | |
8d08fdba | 13841 | else |
a0a33927 MS |
13842 | { |
13843 | /* This is a definition, not a reference. | |
b7484fbe MS |
13844 | So clear DECL_EXTERNAL. */ |
13845 | DECL_EXTERNAL (decl1) = 0; | |
faae18ab | 13846 | |
79065db2 MM |
13847 | if ((DECL_DECLARED_INLINE_P (decl1) |
13848 | || DECL_TEMPLATE_INSTANTIATION (decl1)) | |
5566b478 MS |
13849 | && ! DECL_INTERFACE_KNOWN (decl1) |
13850 | /* Don't try to defer nested functions for now. */ | |
4f1c5b7d | 13851 | && ! decl_function_context (decl1)) |
878cd289 MS |
13852 | DECL_DEFER_OUTPUT (decl1) = 1; |
13853 | else | |
893de33c | 13854 | DECL_INTERFACE_KNOWN (decl1) = 1; |
db5ae43f | 13855 | } |
a9aedbc2 | 13856 | |
f444e36b MM |
13857 | pushlevel (0); |
13858 | current_binding_level->parm_flag = 1; | |
8d08fdba | 13859 | |
5566b478 MS |
13860 | ++function_depth; |
13861 | ||
0d9eb3ba | 13862 | if (DECL_DESTRUCTOR_P (decl1)) |
46e8c075 MM |
13863 | { |
13864 | dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE); | |
13865 | DECL_CONTEXT (dtor_label) = current_function_decl; | |
13866 | } | |
8d08fdba | 13867 | |
0ba8a114 NS |
13868 | start_fname_decls (); |
13869 | ||
f444e36b MM |
13870 | store_parm_decls (current_function_parms); |
13871 | ||
8d08fdba MS |
13872 | return 1; |
13873 | } | |
13874 | \f | |
13875 | /* Store the parameter declarations into the current function declaration. | |
13876 | This is called after parsing the parameter declarations, before | |
13877 | digesting the body of the function. | |
13878 | ||
13879 | Also install to binding contour return value identifier, if any. */ | |
13880 | ||
f444e36b MM |
13881 | static void |
13882 | store_parm_decls (current_function_parms) | |
13883 | tree current_function_parms; | |
8d08fdba MS |
13884 | { |
13885 | register tree fndecl = current_function_decl; | |
13886 | register tree parm; | |
8d08fdba | 13887 | |
8d08fdba MS |
13888 | /* This is a chain of any other decls that came in among the parm |
13889 | declarations. If a parm is declared with enum {foo, bar} x; | |
13890 | then CONST_DECLs for foo and bar are put here. */ | |
13891 | tree nonparms = NULL_TREE; | |
13892 | ||
b35d4555 | 13893 | if (current_function_parms) |
8d08fdba MS |
13894 | { |
13895 | /* This case is when the function was defined with an ANSI prototype. | |
13896 | The parms already have decls, so we need not do anything here | |
13897 | except record them as in effect | |
13898 | and complain if any redundant old-style parm decls were written. */ | |
13899 | ||
b35d4555 MM |
13900 | tree specparms = current_function_parms; |
13901 | tree next; | |
13902 | ||
f444e36b | 13903 | /* Must clear this because it might contain TYPE_DECLs declared |
b35d4555 | 13904 | at class level. */ |
f444e36b | 13905 | storedecls (NULL_TREE); |
8d08fdba | 13906 | |
f444e36b | 13907 | /* If we're doing semantic analysis, then we'll call pushdecl |
b35d4555 MM |
13908 | for each of these. We must do them in reverse order so that |
13909 | they end in the correct forward order. */ | |
f444e36b | 13910 | specparms = nreverse (specparms); |
5566b478 | 13911 | |
b35d4555 | 13912 | for (parm = specparms; parm; parm = next) |
8d08fdba MS |
13913 | { |
13914 | next = TREE_CHAIN (parm); | |
13915 | if (TREE_CODE (parm) == PARM_DECL) | |
13916 | { | |
f444e36b MM |
13917 | if (DECL_NAME (parm) == NULL_TREE |
13918 | || TREE_CODE (parm) != VOID_TYPE) | |
13919 | pushdecl (parm); | |
13920 | else | |
33bd39a2 | 13921 | error ("parameter `%D' declared void", parm); |
8d08fdba MS |
13922 | } |
13923 | else | |
13924 | { | |
13925 | /* If we find an enum constant or a type tag, | |
13926 | put it aside for the moment. */ | |
13927 | TREE_CHAIN (parm) = NULL_TREE; | |
13928 | nonparms = chainon (nonparms, parm); | |
13929 | } | |
13930 | } | |
13931 | ||
f444e36b MM |
13932 | /* Get the decls in their original chain order and record in the |
13933 | function. This is all and only the PARM_DECLs that were | |
13934 | pushed into scope by the loop above. */ | |
13935 | DECL_ARGUMENTS (fndecl) = getdecls (); | |
969fd501 | 13936 | storetags (gettags ()); |
8d08fdba MS |
13937 | } |
13938 | else | |
13939 | DECL_ARGUMENTS (fndecl) = NULL_TREE; | |
13940 | ||
13941 | /* Now store the final chain of decls for the arguments | |
13942 | as the decl-chain of the current lexical scope. | |
13943 | Put the enumerators in as well, at the front so that | |
13944 | DECL_ARGUMENTS is not modified. */ | |
f444e36b | 13945 | storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl))); |
eb448459 | 13946 | |
b35d4555 | 13947 | /* Do the starting of the exception specifications, if we have any. */ |
68642fb6 | 13948 | if (flag_exceptions && !processing_template_decl |
1660cb3a | 13949 | && flag_enforce_eh_specs |
b35d4555 | 13950 | && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl))) |
52a11cbf | 13951 | current_eh_spec_block = begin_eh_spec_block (); |
8d08fdba MS |
13952 | } |
13953 | ||
8d08fdba | 13954 | \f |
59026e79 MM |
13955 | /* We have finished doing semantic analysis on DECL, but have not yet |
13956 | generated RTL for its body. Save away our current state, so that | |
13957 | when we want to generate RTL later we know what to do. */ | |
13958 | ||
13959 | static void | |
13960 | save_function_data (decl) | |
13961 | tree decl; | |
13962 | { | |
ae499cce | 13963 | struct cp_language_function *f; |
59026e79 MM |
13964 | |
13965 | /* Save the language-specific per-function data so that we can | |
13966 | get it back when we really expand this function. */ | |
13967 | my_friendly_assert (!DECL_PENDING_INLINE_P (decl), | |
13968 | 19990908); | |
68642fb6 | 13969 | |
59026e79 | 13970 | /* Make a copy. */ |
ae499cce MM |
13971 | f = ((struct cp_language_function *) |
13972 | xmalloc (sizeof (struct cp_language_function))); | |
4e135bdd | 13973 | memcpy (f, cp_function_chain, sizeof (struct cp_language_function)); |
59026e79 MM |
13974 | DECL_SAVED_FUNCTION_DATA (decl) = f; |
13975 | ||
13976 | /* Clear out the bits we don't need. */ | |
ae499cce MM |
13977 | f->base.x_stmt_tree.x_last_stmt = NULL_TREE; |
13978 | f->base.x_stmt_tree.x_last_expr_type = NULL_TREE; | |
59026e79 MM |
13979 | f->x_named_label_uses = NULL; |
13980 | f->bindings = NULL; | |
8e4ce833 | 13981 | f->x_local_names = NULL; |
59026e79 MM |
13982 | |
13983 | /* When we get back here again, we will be expanding. */ | |
13984 | f->x_expanding_p = 1; | |
914653a2 MM |
13985 | |
13986 | /* If we've already decided that we cannot inline this function, we | |
13987 | must remember that fact when we actually go to expand the | |
13988 | function. */ | |
acc72c37 MM |
13989 | if (current_function_cannot_inline) |
13990 | { | |
13991 | f->cannot_inline = current_function_cannot_inline; | |
13992 | DECL_INLINE (decl) = 0; | |
13993 | } | |
59026e79 MM |
13994 | } |
13995 | ||
cdd2559c JM |
13996 | /* Add a note to mark the beginning of the main body of the constructor. |
13997 | This is used to set up the data structures for the cleanup regions for | |
13998 | fully-constructed bases and members. */ | |
13999 | ||
14000 | static void | |
14001 | begin_constructor_body () | |
14002 | { | |
cdd2559c JM |
14003 | } |
14004 | ||
ade3dc07 JM |
14005 | /* Add a note to mark the end of the main body of the constructor. This is |
14006 | used to end the cleanup regions for fully-constructed bases and | |
14007 | members. */ | |
efee38a9 MM |
14008 | |
14009 | static void | |
14010 | finish_constructor_body () | |
14011 | { | |
efee38a9 MM |
14012 | } |
14013 | ||
cdd2559c JM |
14014 | /* Do all the processing for the beginning of a destructor; set up the |
14015 | vtable pointers and cleanups for bases and members. */ | |
14016 | ||
14017 | static void | |
14018 | begin_destructor_body () | |
14019 | { | |
14020 | tree if_stmt; | |
14021 | tree compound_stmt; | |
14022 | ||
14023 | /* If the dtor is empty, and we know there is not any possible | |
14024 | way we could use any vtable entries, before they are possibly | |
14025 | set by a base class dtor, we don't have to setup the vtables, | |
14026 | as we know that any base class dtor will set up any vtables | |
14027 | it needs. We avoid MI, because one base class dtor can do a | |
14028 | virtual dispatch to an overridden function that would need to | |
14029 | have a non-related vtable set up, we cannot avoid setting up | |
14030 | vtables in that case. We could change this to see if there | |
14031 | is just one vtable. | |
14032 | ||
14033 | ??? In the destructor for a class, the vtables are set | |
14034 | appropriately for that class. There will be no non-related | |
14035 | vtables. jason 2001-12-11. */ | |
14036 | if_stmt = begin_if_stmt (); | |
14037 | ||
14038 | /* If it is not safe to avoid setting up the vtables, then | |
14039 | someone will change the condition to be boolean_true_node. | |
14040 | (Actually, for now, we do not have code to set the condition | |
14041 | appropriately, so we just assume that we always need to | |
14042 | initialize the vtables.) */ | |
14043 | finish_if_stmt_cond (boolean_true_node, if_stmt); | |
14044 | current_vcalls_possible_p = &IF_COND (if_stmt); | |
14045 | ||
14046 | compound_stmt = begin_compound_stmt (/*has_no_scope=*/0); | |
14047 | ||
14048 | /* Make all virtual function table pointers in non-virtual base | |
14049 | classes point to CURRENT_CLASS_TYPE's virtual function | |
14050 | tables. */ | |
14051 | initialize_vtbl_ptrs (current_class_ptr); | |
14052 | ||
14053 | finish_compound_stmt (/*has_no_scope=*/0, compound_stmt); | |
14054 | finish_then_clause (if_stmt); | |
14055 | finish_if_stmt (); | |
14056 | ||
14057 | /* And insert cleanups for our bases and members so that they | |
14058 | will be properly destroyed if we throw. */ | |
14059 | push_base_cleanups (); | |
14060 | } | |
14061 | ||
ade3dc07 JM |
14062 | /* At the end of every destructor we generate code to delete the object if |
14063 | necessary. Do that now. */ | |
9bfadf57 MM |
14064 | |
14065 | static void | |
14066 | finish_destructor_body () | |
14067 | { | |
9bfadf57 MM |
14068 | tree exprstmt; |
14069 | ||
52682a1b MM |
14070 | /* In a virtual destructor, we must call delete. */ |
14071 | if (DECL_VIRTUAL_P (current_function_decl)) | |
14072 | { | |
14073 | tree if_stmt; | |
14074 | tree virtual_size = c_sizeof (current_class_type); | |
68642fb6 | 14075 | |
52682a1b | 14076 | /* [class.dtor] |
68642fb6 | 14077 | |
ade3dc07 JM |
14078 | At the point of definition of a virtual destructor (including |
14079 | an implicit definition), non-placement operator delete shall | |
14080 | be looked up in the scope of the destructor's class and if | |
14081 | found shall be accessible and unambiguous. */ | |
52682a1b MM |
14082 | exprstmt = build_op_delete_call |
14083 | (DELETE_EXPR, current_class_ptr, virtual_size, | |
14084 | LOOKUP_NORMAL | LOOKUP_SPECULATIVELY, NULL_TREE); | |
298d6f60 | 14085 | |
52682a1b MM |
14086 | if_stmt = begin_if_stmt (); |
14087 | finish_if_stmt_cond (build (BIT_AND_EXPR, integer_type_node, | |
14088 | current_in_charge_parm, | |
14089 | integer_one_node), | |
14090 | if_stmt); | |
14091 | finish_expr_stmt (exprstmt); | |
14092 | finish_then_clause (if_stmt); | |
14093 | finish_if_stmt (); | |
14094 | } | |
ade3dc07 | 14095 | } |
9bfadf57 | 14096 | |
ade3dc07 JM |
14097 | /* Do the necessary processing for the beginning of a function body, which |
14098 | in this case includes member-initializers, but not the catch clauses of | |
14099 | a function-try-block. Currently, this means opening a binding level | |
14100 | for the member-initializers (in a ctor) and member cleanups (in a dtor). | |
14101 | In other functions, this isn't necessary, but it doesn't hurt. */ | |
14102 | ||
14103 | tree | |
14104 | begin_function_body () | |
14105 | { | |
cdd2559c JM |
14106 | tree stmt; |
14107 | ||
b5856475 JM |
14108 | if (processing_template_decl) |
14109 | /* Do nothing now. */; | |
14110 | else | |
14111 | /* Always keep the BLOCK node associated with the outermost pair of | |
14112 | curly braces of a function. These are needed for correct | |
14113 | operation of dwarfout.c. */ | |
14114 | keep_next_level (1); | |
14115 | ||
cdd2559c | 14116 | stmt = begin_compound_stmt (0); |
ade3dc07 | 14117 | COMPOUND_STMT_BODY_BLOCK (stmt) = 1; |
cdd2559c JM |
14118 | |
14119 | if (processing_template_decl) | |
14120 | /* Do nothing now. */; | |
14121 | else if (DECL_CONSTRUCTOR_P (current_function_decl)) | |
14122 | begin_constructor_body (); | |
14123 | else if (DECL_DESTRUCTOR_P (current_function_decl)) | |
14124 | begin_destructor_body (); | |
14125 | ||
ade3dc07 | 14126 | return stmt; |
9bfadf57 MM |
14127 | } |
14128 | ||
ade3dc07 JM |
14129 | /* Do the processing for the end of a function body. Currently, this means |
14130 | closing out the cleanups for fully-constructed bases and members, and in | |
14131 | the case of the destructor, deleting the object if desired. Again, this | |
14132 | is only meaningful for [cd]tors, since they are the only functions where | |
14133 | there is a significant distinction between the main body and any | |
14134 | function catch clauses. Handling, say, main() return semantics here | |
14135 | would be wrong, as flowing off the end of a function catch clause for | |
14136 | main() would also need to return 0. */ | |
14137 | ||
14138 | void | |
14139 | finish_function_body (compstmt) | |
14140 | tree compstmt; | |
14141 | { | |
14142 | if (processing_template_decl) | |
14143 | /* Do nothing now. */; | |
14144 | else if (DECL_DESTRUCTOR_P (current_function_decl)) | |
14145 | /* Any return from a destructor will end up here. Put it before the | |
14146 | cleanups so that an explicit return doesn't duplicate them. */ | |
14147 | add_stmt (build_stmt (LABEL_STMT, dtor_label)); | |
14148 | ||
14149 | /* Close the block; in a destructor, run the member cleanups. */ | |
14150 | finish_compound_stmt (0, compstmt); | |
14151 | ||
14152 | if (processing_template_decl) | |
14153 | /* Do nothing now. */; | |
14154 | else if (DECL_CONSTRUCTOR_P (current_function_decl)) | |
14155 | finish_constructor_body (); | |
14156 | else if (DECL_DESTRUCTOR_P (current_function_decl)) | |
14157 | finish_destructor_body (); | |
14158 | } | |
14159 | ||
8d08fdba MS |
14160 | /* Finish up a function declaration and compile that function |
14161 | all the way to assembler language output. The free the storage | |
14162 | for the function definition. | |
14163 | ||
68642fb6 | 14164 | FLAGS is a bitwise or of the following values: |
f181d4ae MM |
14165 | 2 - INCLASS_INLINE |
14166 | We just finished processing the body of an in-class inline | |
14167 | function definition. (This processing will have taken place | |
87e3dbc9 | 14168 | after the class definition is complete.) */ |
8d08fdba | 14169 | |
4d6abc1c | 14170 | tree |
0acf7199 | 14171 | finish_function (flags) |
f181d4ae | 14172 | int flags; |
8d08fdba MS |
14173 | { |
14174 | register tree fndecl = current_function_decl; | |
14175 | tree fntype, ctype = NULL_TREE; | |
f181d4ae | 14176 | int inclass_inline = (flags & 2) != 0; |
87e3dbc9 | 14177 | int nested; |
8d08fdba MS |
14178 | |
14179 | /* When we get some parse errors, we can end up without a | |
14180 | current_function_decl, so cope. */ | |
14181 | if (fndecl == NULL_TREE) | |
4d6abc1c | 14182 | return error_mark_node; |
8d08fdba | 14183 | |
87e3dbc9 | 14184 | nested = function_depth > 1; |
8d08fdba MS |
14185 | fntype = TREE_TYPE (fndecl); |
14186 | ||
9bfadf57 MM |
14187 | /* TREE_READONLY (fndecl) = 1; |
14188 | This caused &foo to be of type ptr-to-const-function | |
14189 | which then got a warning when stored in a ptr-to-function variable. */ | |
8d08fdba | 14190 | |
f444e36b | 14191 | my_friendly_assert (building_stmt_tree (), 20000911); |
8d08fdba | 14192 | |
0ba8a114 NS |
14193 | finish_fname_decls (); |
14194 | ||
db9b2174 MM |
14195 | /* For a cloned function, we've already got all the code we need; |
14196 | there's no need to add any extra bits. */ | |
f444e36b | 14197 | if (!DECL_CLONED_FUNCTION_P (fndecl)) |
8d08fdba | 14198 | { |
ade3dc07 | 14199 | if (DECL_MAIN_P (current_function_decl)) |
efee38a9 MM |
14200 | { |
14201 | /* Make it so that `main' always returns 0 by default. */ | |
a0de9d20 | 14202 | #ifdef VMS_TARGET |
efee38a9 MM |
14203 | finish_return_stmt (integer_one_node); |
14204 | #else | |
14205 | finish_return_stmt (integer_zero_node); | |
14206 | #endif | |
14207 | } | |
87e3dbc9 | 14208 | |
b35d4555 MM |
14209 | /* Finish dealing with exception specifiers. */ |
14210 | if (flag_exceptions && !processing_template_decl | |
1660cb3a | 14211 | && flag_enforce_eh_specs |
b35d4555 | 14212 | && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl))) |
52a11cbf RH |
14213 | finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS |
14214 | (TREE_TYPE (current_function_decl)), | |
14215 | current_eh_spec_block); | |
5566b478 | 14216 | } |
68642fb6 | 14217 | |
558475f0 | 14218 | /* If we're saving up tree structure, tie off the function now. */ |
f444e36b | 14219 | finish_stmt_tree (&DECL_SAVED_TREE (fndecl)); |
8d2733ca | 14220 | |
8d08fdba MS |
14221 | /* This must come after expand_function_end because cleanups might |
14222 | have declarations (from inline functions) that need to go into | |
14223 | this function's blocks. */ | |
7437519c ZW |
14224 | |
14225 | /* If the current binding level isn't the outermost binding level | |
14226 | for this function, either there is a bug, or we have experienced | |
14227 | syntax errors and the statement tree is malformed. */ | |
f444e36b | 14228 | if (current_binding_level->parm_flag != 1) |
7437519c ZW |
14229 | { |
14230 | /* Make sure we have already experienced errors. */ | |
14231 | if (errorcount == 0) | |
14232 | abort (); | |
14233 | ||
14234 | /* Throw away the broken statement tree and extra binding | |
14235 | levels. */ | |
14236 | DECL_SAVED_TREE (fndecl) = build_stmt (COMPOUND_STMT, NULL_TREE); | |
14237 | ||
14238 | while (current_binding_level->parm_flag != 1) | |
14239 | { | |
14240 | if (current_binding_level->parm_flag == 2) | |
14241 | pop_nested_class (); | |
14242 | else | |
14243 | poplevel (0, 0, 0); | |
14244 | } | |
14245 | } | |
f444e36b | 14246 | poplevel (1, 0, 1); |
8d08fdba | 14247 | |
07b2f2fd JM |
14248 | /* Set up the named return value optimization, if we can. Here, we |
14249 | eliminate the copy from the nrv into the RESULT_DECL and any cleanup | |
14250 | for the nrv. genrtl_start_function and declare_return_variable | |
14251 | handle making the nrv and RESULT_DECL share space. */ | |
14252 | if (current_function_return_value) | |
14253 | { | |
14254 | tree r = current_function_return_value; | |
14255 | /* This is only worth doing for fns that return in memory--and | |
14256 | simpler, since we don't have to worry about promoted modes. */ | |
14257 | if (r != error_mark_node | |
14258 | && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)))) | |
14259 | { | |
14260 | DECL_ALIGN (r) = DECL_ALIGN (DECL_RESULT (fndecl)); | |
14261 | walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl), | |
14262 | nullify_returns_r, r); | |
14263 | } | |
14264 | else | |
14265 | /* Clear it so genrtl_start_function and declare_return_variable | |
14266 | know we're not optimizing. */ | |
14267 | current_function_return_value = NULL_TREE; | |
14268 | } | |
14269 | ||
a8f73d4b | 14270 | /* Remember that we were in class scope. */ |
db5ae43f | 14271 | if (current_class_name) |
a8f73d4b | 14272 | ctype = current_class_type; |
db5ae43f | 14273 | |
1caa11d3 MM |
14274 | /* Must mark the RESULT_DECL as being in this function. */ |
14275 | DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl; | |
14276 | ||
14277 | /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point | |
14278 | to the FUNCTION_DECL node itself. */ | |
14279 | BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl; | |
14280 | ||
59026e79 | 14281 | /* Save away current state, if appropriate. */ |
f444e36b | 14282 | if (!processing_template_decl) |
59026e79 MM |
14283 | save_function_data (fndecl); |
14284 | ||
95fabfd3 MM |
14285 | /* If this function calls `setjmp' it cannot be inlined. When |
14286 | `longjmp' is called it is not guaranteed to restore the value of | |
14287 | local variables that have been modified since the call to | |
14288 | `setjmp'. So, if were to inline this function into some caller | |
14289 | `c', then when we `longjmp', we might not restore all variables | |
14290 | in `c'. (It might seem, at first blush, that there's no way for | |
14291 | this function to modify local variables in `c', but their | |
14292 | addresses may have been stored somewhere accessible to this | |
14293 | function.) */ | |
f444e36b | 14294 | if (!processing_template_decl && calls_setjmp_p (fndecl)) |
95fabfd3 MM |
14295 | DECL_UNINLINABLE (fndecl) = 1; |
14296 | ||
efe49da0 | 14297 | /* Complain if there's just no return statement. */ |
46cfb101 JM |
14298 | if (warn_return_type |
14299 | && !processing_template_decl | |
efe49da0 | 14300 | && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE |
57d5032b | 14301 | && !current_function_returns_value && !current_function_returns_null |
efe49da0 JM |
14302 | /* Don't complain if we abort or throw. */ |
14303 | && !current_function_returns_abnormally | |
46cfb101 JM |
14304 | && !DECL_NAME (DECL_RESULT (fndecl)) |
14305 | /* Normally, with -Wreturn-type, flow will complain. Unless we're an | |
efe49da0 | 14306 | inline function, as we might never be compiled separately. */ |
46cfb101 | 14307 | && DECL_INLINE (fndecl)) |
efe49da0 JM |
14308 | warning ("no return statement in function returning non-void"); |
14309 | ||
f444e36b MM |
14310 | /* Clear out memory we no longer need. */ |
14311 | free_after_parsing (cfun); | |
14312 | /* Since we never call rest_of_compilation, we never clear | |
14313 | CFUN. Do so explicitly. */ | |
14314 | free_after_compilation (cfun); | |
14315 | cfun = NULL; | |
a8f73d4b MM |
14316 | |
14317 | /* If this is a in-class inline definition, we may have to pop the | |
14318 | bindings for the template parameters that we added in | |
14319 | maybe_begin_member_template_processing when start_function was | |
14320 | called. */ | |
14321 | if (inclass_inline) | |
14322 | maybe_end_member_template_processing (); | |
14323 | ||
14324 | /* Leave the scope of the class. */ | |
14325 | if (ctype) | |
14326 | pop_nested_class (); | |
5566b478 MS |
14327 | |
14328 | --function_depth; | |
8d08fdba | 14329 | |
4d6abc1c | 14330 | /* Clean up. */ |
28cbf42c | 14331 | if (! nested) |
1f8f4a0b MM |
14332 | /* Let the error reporting routines know that we're outside a |
14333 | function. For a nested function, this value is used in | |
b03e38e1 | 14334 | cxx_pop_function_context and then reset via pop_function_context. */ |
1f8f4a0b | 14335 | current_function_decl = NULL_TREE; |
4d6abc1c MM |
14336 | |
14337 | return fndecl; | |
8d08fdba MS |
14338 | } |
14339 | \f | |
14340 | /* Create the FUNCTION_DECL for a function definition. | |
8d08fdba MS |
14341 | DECLSPECS and DECLARATOR are the parts of the declaration; |
14342 | they describe the return type and the name of the function, | |
14343 | but twisted together in a fashion that parallels the syntax of C. | |
14344 | ||
14345 | This function creates a binding context for the function body | |
14346 | as well as setting up the FUNCTION_DECL in current_function_decl. | |
14347 | ||
14348 | Returns a FUNCTION_DECL on success. | |
14349 | ||
14350 | If the DECLARATOR is not suitable for a function (it defines a datum | |
14351 | instead), we return 0, which tells yyparse to report a parse error. | |
14352 | ||
14353 | May return void_type_node indicating that this method is actually | |
14354 | a friend. See grokfield for more details. | |
14355 | ||
14356 | Came here with a `.pushlevel' . | |
14357 | ||
14358 | DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING | |
14359 | CHANGES TO CODE IN `grokfield'. */ | |
e92cc029 | 14360 | |
8d08fdba | 14361 | tree |
acf82af2 JM |
14362 | start_method (declspecs, declarator, attrlist) |
14363 | tree declarator, declspecs, attrlist; | |
8d08fdba | 14364 | { |
c11b6f21 | 14365 | tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0, |
91d231cb | 14366 | &attrlist); |
8d08fdba MS |
14367 | |
14368 | /* Something too ugly to handle. */ | |
14369 | if (fndecl == NULL_TREE) | |
14370 | return NULL_TREE; | |
14371 | ||
195a5def IR |
14372 | if (attrlist) |
14373 | cplus_decl_attributes (&fndecl, attrlist, 0); | |
14374 | ||
8d08fdba | 14375 | /* Pass friends other than inline friend functions back. */ |
a1774733 | 14376 | if (fndecl == void_type_node) |
8d08fdba MS |
14377 | return fndecl; |
14378 | ||
14379 | if (TREE_CODE (fndecl) != FUNCTION_DECL) | |
14380 | /* Not a function, tell parser to report parse error. */ | |
14381 | return NULL_TREE; | |
14382 | ||
8d08fdba MS |
14383 | if (DECL_IN_AGGR_P (fndecl)) |
14384 | { | |
14385 | if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type) | |
14386 | { | |
68642fb6 | 14387 | if (DECL_CONTEXT (fndecl) |
2c73f9f5 | 14388 | && TREE_CODE( DECL_CONTEXT (fndecl)) != NAMESPACE_DECL) |
33bd39a2 | 14389 | error ("`%D' is already defined in class `%T'", fndecl, |
2ae7bada | 14390 | DECL_CONTEXT (fndecl)); |
8d08fdba MS |
14391 | } |
14392 | return void_type_node; | |
14393 | } | |
14394 | ||
f3400fe2 JM |
14395 | check_template_shadow (fndecl); |
14396 | ||
79065db2 | 14397 | DECL_DECLARED_INLINE_P (fndecl) = 1; |
faae18ab | 14398 | |
8926095f | 14399 | if (flag_default_inline) |
8d08fdba MS |
14400 | DECL_INLINE (fndecl) = 1; |
14401 | ||
36a117a5 MM |
14402 | /* We process method specializations in finish_struct_1. */ |
14403 | if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl)) | |
3ac3d9ea | 14404 | fndecl = push_template_decl (fndecl); |
a0a33927 | 14405 | |
8d08fdba MS |
14406 | if (! DECL_FRIEND_P (fndecl)) |
14407 | { | |
8d08fdba MS |
14408 | if (TREE_CHAIN (fndecl)) |
14409 | { | |
14410 | fndecl = copy_node (fndecl); | |
14411 | TREE_CHAIN (fndecl) = NULL_TREE; | |
14412 | } | |
271e6f02 | 14413 | grok_special_member_properties (fndecl); |
8d08fdba MS |
14414 | } |
14415 | ||
cd9f6678 | 14416 | cp_finish_decl (fndecl, NULL_TREE, NULL_TREE, 0); |
8d08fdba MS |
14417 | |
14418 | /* Make a place for the parms */ | |
14419 | pushlevel (0); | |
14420 | current_binding_level->parm_flag = 1; | |
68642fb6 | 14421 | |
8d08fdba MS |
14422 | DECL_IN_AGGR_P (fndecl) = 1; |
14423 | return fndecl; | |
14424 | } | |
14425 | ||
14426 | /* Go through the motions of finishing a function definition. | |
14427 | We don't compile this method until after the whole class has | |
14428 | been processed. | |
14429 | ||
14430 | FINISH_METHOD must return something that looks as though it | |
14431 | came from GROKFIELD (since we are defining a method, after all). | |
14432 | ||
14433 | This is called after parsing the body of the function definition. | |
14434 | STMTS is the chain of statements that makes up the function body. | |
14435 | ||
14436 | DECL is the ..._DECL that `start_method' provided. */ | |
14437 | ||
14438 | tree | |
14439 | finish_method (decl) | |
14440 | tree decl; | |
14441 | { | |
14442 | register tree fndecl = decl; | |
14443 | tree old_initial; | |
8d08fdba MS |
14444 | |
14445 | register tree link; | |
14446 | ||
a1774733 | 14447 | if (decl == void_type_node) |
8d08fdba MS |
14448 | return decl; |
14449 | ||
14450 | old_initial = DECL_INITIAL (fndecl); | |
14451 | ||
14452 | /* Undo the level for the parms (from start_method). | |
14453 | This is like poplevel, but it causes nothing to be | |
14454 | saved. Saving information here confuses symbol-table | |
14455 | output routines. Besides, this information will | |
14456 | be correctly output when this method is actually | |
14457 | compiled. */ | |
14458 | ||
14459 | /* Clear out the meanings of the local variables of this level; | |
14460 | also record in each decl which block it belongs to. */ | |
14461 | ||
14462 | for (link = current_binding_level->names; link; link = TREE_CHAIN (link)) | |
14463 | { | |
14464 | if (DECL_NAME (link) != NULL_TREE) | |
d8f8dca1 | 14465 | pop_binding (DECL_NAME (link), link); |
8d08fdba MS |
14466 | my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163); |
14467 | DECL_CONTEXT (link) = NULL_TREE; | |
14468 | } | |
14469 | ||
8d08fdba MS |
14470 | poplevel (0, 0, 0); |
14471 | ||
14472 | DECL_INITIAL (fndecl) = old_initial; | |
14473 | ||
14474 | /* We used to check if the context of FNDECL was different from | |
14475 | current_class_type as another way to get inside here. This didn't work | |
14476 | for String.cc in libg++. */ | |
14477 | if (DECL_FRIEND_P (fndecl)) | |
14478 | { | |
14479 | CLASSTYPE_INLINE_FRIENDS (current_class_type) | |
14480 | = tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type)); | |
14481 | decl = void_type_node; | |
14482 | } | |
14483 | ||
14484 | return decl; | |
14485 | } | |
14486 | \f | |
0154eaa8 MM |
14487 | |
14488 | /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that | |
14489 | we can lay it out later, when and if its type becomes complete. */ | |
8d08fdba MS |
14490 | |
14491 | void | |
0154eaa8 MM |
14492 | maybe_register_incomplete_var (var) |
14493 | tree var; | |
8d08fdba | 14494 | { |
0154eaa8 | 14495 | my_friendly_assert (TREE_CODE (var) == VAR_DECL, 20020406); |
8d08fdba | 14496 | |
0154eaa8 MM |
14497 | /* Keep track of variables with incomplete types. */ |
14498 | if (!processing_template_decl && TREE_TYPE (var) != error_mark_node | |
14499 | && DECL_EXTERNAL (var)) | |
70adf8a9 | 14500 | { |
0154eaa8 MM |
14501 | tree inner_type = TREE_TYPE (var); |
14502 | ||
14503 | while (TREE_CODE (inner_type) == ARRAY_TYPE) | |
14504 | inner_type = TREE_TYPE (inner_type); | |
14505 | inner_type = TYPE_MAIN_VARIANT (inner_type); | |
14506 | ||
14507 | if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type)) | |
14508 | /* RTTI TD entries are created while defining the type_info. */ | |
14509 | || (TYPE_LANG_SPECIFIC (inner_type) | |
14510 | && TYPE_BEING_DEFINED (inner_type))) | |
14511 | incomplete_vars = tree_cons (inner_type, var, incomplete_vars); | |
70adf8a9 | 14512 | } |
0154eaa8 | 14513 | } |
70adf8a9 | 14514 | |
0154eaa8 MM |
14515 | /* Called when a class type (given by TYPE) is defined. If there are |
14516 | any existing VAR_DECLs whose type hsa been completed by this | |
14517 | declaration, update them now. */ | |
70adf8a9 | 14518 | |
0154eaa8 MM |
14519 | void |
14520 | complete_vars (type) | |
14521 | tree type; | |
14522 | { | |
14523 | tree *list = &incomplete_vars; | |
14524 | ||
14525 | my_friendly_assert (CLASS_TYPE_P (type), 20020406); | |
14526 | while (*list) | |
14527 | { | |
14528 | if (same_type_p (type, TREE_PURPOSE (*list))) | |
70adf8a9 | 14529 | { |
0154eaa8 | 14530 | tree var = TREE_VALUE (*list); |
c740732f MM |
14531 | /* Complete the type of the variable. The VAR_DECL itself |
14532 | will be laid out in expand_expr. */ | |
14533 | complete_type (TREE_TYPE (var)); | |
0154eaa8 MM |
14534 | /* Remove this entry from the list. */ |
14535 | *list = TREE_CHAIN (*list); | |
f30432d7 MS |
14536 | } |
14537 | else | |
0154eaa8 | 14538 | list = &TREE_CHAIN (*list); |
f30432d7 | 14539 | } |
8d08fdba MS |
14540 | } |
14541 | ||
86f45d2c MM |
14542 | /* If DECL is of a type which needs a cleanup, build that cleanup |
14543 | here. */ | |
e92cc029 | 14544 | |
86f45d2c | 14545 | tree |
c88770e9 | 14546 | cxx_maybe_build_cleanup (decl) |
86f45d2c | 14547 | tree decl; |
8d08fdba MS |
14548 | { |
14549 | tree type = TREE_TYPE (decl); | |
86f45d2c | 14550 | |
834c6dff | 14551 | if (type != error_mark_node && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) |
8d08fdba | 14552 | { |
80048418 | 14553 | int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR; |
8d08fdba | 14554 | tree rval; |
8d08fdba | 14555 | |
8d08fdba MS |
14556 | if (TREE_CODE (type) == ARRAY_TYPE) |
14557 | rval = decl; | |
14558 | else | |
14559 | { | |
dffd7eb6 | 14560 | cxx_mark_addressable (decl); |
8d08fdba MS |
14561 | rval = build_unary_op (ADDR_EXPR, decl, 0); |
14562 | } | |
14563 | ||
14564 | /* Optimize for space over speed here. */ | |
14565 | if (! TYPE_USES_VIRTUAL_BASECLASSES (type) | |
14566 | || flag_expensive_optimizations) | |
14567 | flags |= LOOKUP_NONVIRTUAL; | |
14568 | ||
86f45d2c MM |
14569 | rval = build_delete (TREE_TYPE (rval), rval, |
14570 | sfk_complete_destructor, flags, 0); | |
8d08fdba MS |
14571 | |
14572 | if (TYPE_USES_VIRTUAL_BASECLASSES (type) | |
14573 | && ! TYPE_HAS_DESTRUCTOR (type)) | |
e1b3e07d | 14574 | rval = build_compound_expr (tree_cons (NULL_TREE, rval, |
051e6fd7 | 14575 | build_tree_list (NULL_TREE, build_vbase_delete (type, decl)))); |
8d08fdba | 14576 | |
8d08fdba MS |
14577 | return rval; |
14578 | } | |
6e4ae815 | 14579 | return NULL_TREE; |
8d08fdba MS |
14580 | } |
14581 | \f | |
558475f0 | 14582 | /* When a stmt has been parsed, this function is called. */ |
8d08fdba MS |
14583 | |
14584 | void | |
14585 | finish_stmt () | |
14586 | { | |
558475f0 MM |
14587 | /* Always assume this statement was not an expression statement. If |
14588 | it actually was an expression statement, its our callers | |
14589 | responsibility to fix this up. */ | |
14590 | last_expr_type = NULL_TREE; | |
8d08fdba MS |
14591 | } |
14592 | ||
3afb32a4 MM |
14593 | /* DECL was originally constructed as a non-static member function, |
14594 | but turned out to be static. Update it accordingly. */ | |
700f8a87 | 14595 | |
8857f91e | 14596 | void |
3afb32a4 MM |
14597 | revert_static_member_fn (decl) |
14598 | tree decl; | |
8d08fdba | 14599 | { |
700f8a87 | 14600 | tree tmp; |
3afb32a4 MM |
14601 | tree function = TREE_TYPE (decl); |
14602 | tree args = TYPE_ARG_TYPES (function); | |
8d08fdba | 14603 | |
89d684bb | 14604 | if (cp_type_quals (TREE_TYPE (TREE_VALUE (args))) |
91063b51 | 14605 | != TYPE_UNQUALIFIED) |
33bd39a2 | 14606 | error ("static member function `%#D' declared with type qualifiers", |
11306230 | 14607 | decl); |
f30432d7 | 14608 | |
700f8a87 MS |
14609 | args = TREE_CHAIN (args); |
14610 | tmp = build_function_type (TREE_TYPE (function), args); | |
89d684bb | 14611 | tmp = build_qualified_type (tmp, cp_type_quals (function)); |
f30432d7 | 14612 | tmp = build_exception_variant (tmp, |
8d08fdba | 14613 | TYPE_RAISES_EXCEPTIONS (function)); |
3afb32a4 MM |
14614 | TREE_TYPE (decl) = tmp; |
14615 | if (DECL_ARGUMENTS (decl)) | |
14616 | DECL_ARGUMENTS (decl) = TREE_CHAIN (DECL_ARGUMENTS (decl)); | |
14617 | DECL_STATIC_FUNCTION_P (decl) = 1; | |
8d08fdba | 14618 | } |
a4443a08 | 14619 | |
68642fb6 UD |
14620 | /* Initialize the variables used during compilation of a C++ |
14621 | function. */ | |
db5ae43f | 14622 | |
b03e38e1 NB |
14623 | void |
14624 | cxx_push_function_context (f) | |
99dccabc MM |
14625 | struct function *f; |
14626 | { | |
ae499cce MM |
14627 | struct cp_language_function *p |
14628 | = ((struct cp_language_function *) | |
14629 | xcalloc (1, sizeof (struct cp_language_function))); | |
14630 | f->language = (struct language_function *) p; | |
db5ae43f | 14631 | |
b35d4555 MM |
14632 | /* It takes an explicit call to expand_body to generate RTL for a |
14633 | function. */ | |
14634 | expanding_p = 0; | |
f1dedc31 MM |
14635 | |
14636 | /* Whenever we start a new function, we destroy temporaries in the | |
14637 | usual way. */ | |
ae499cce | 14638 | current_stmt_tree ()->stmts_are_full_exprs_p = 1; |
db5ae43f MS |
14639 | } |
14640 | ||
a8f73d4b MM |
14641 | /* Free the language-specific parts of F, now that we've finished |
14642 | compiling the function. */ | |
db5ae43f | 14643 | |
b03e38e1 NB |
14644 | void |
14645 | cxx_pop_function_context (f) | |
99dccabc | 14646 | struct function *f; |
db5ae43f | 14647 | { |
d658cd4c | 14648 | if (f->language) |
8e4ce833 JJ |
14649 | { |
14650 | struct cp_language_function *cp = | |
14651 | (struct cp_language_function *) f->language; | |
14652 | if (cp->x_local_names) | |
14653 | VARRAY_FREE (cp->x_local_names); | |
14654 | free (f->language); | |
14655 | } | |
99dccabc | 14656 | f->language = 0; |
db5ae43f | 14657 | } |
ebfc180f | 14658 | |
59026e79 | 14659 | /* Mark P for GC. */ |
4519c0a8 | 14660 | |
59026e79 MM |
14661 | static void |
14662 | mark_lang_function (p) | |
ae499cce | 14663 | struct cp_language_function *p; |
59026e79 | 14664 | { |
87e3dbc9 MM |
14665 | if (!p) |
14666 | return; | |
14667 | ||
8f17b5c5 MM |
14668 | mark_c_language_function (&p->base); |
14669 | ||
4519c0a8 | 14670 | ggc_mark_tree (p->x_dtor_label); |
4519c0a8 MM |
14671 | ggc_mark_tree (p->x_current_class_ptr); |
14672 | ggc_mark_tree (p->x_current_class_ref); | |
52a11cbf | 14673 | ggc_mark_tree (p->x_eh_spec_block); |
8e4ce833 | 14674 | ggc_mark_tree_varray (p->x_local_names); |
4519c0a8 | 14675 | |
6625cdb5 | 14676 | mark_named_label_lists (&p->x_named_labels, &p->x_named_label_uses); |
a8f73d4b | 14677 | mark_binding_level (&p->bindings); |
0a01c261 | 14678 | mark_pending_inlines (&p->unparsed_inlines); |
4519c0a8 MM |
14679 | } |
14680 | ||
59026e79 MM |
14681 | /* Mark the language-specific data in F for GC. */ |
14682 | ||
b03e38e1 NB |
14683 | void |
14684 | cxx_mark_function_context (f) | |
59026e79 MM |
14685 | struct function *f; |
14686 | { | |
ae499cce | 14687 | mark_lang_function ((struct cp_language_function *) f->language); |
59026e79 | 14688 | } |
4519c0a8 | 14689 | |
4519c0a8 | 14690 | void |
4f0ade92 | 14691 | cxx_mark_tree (t) |
4519c0a8 MM |
14692 | tree t; |
14693 | { | |
14694 | enum tree_code code = TREE_CODE (t); | |
14695 | if (code == IDENTIFIER_NODE) | |
14696 | { | |
14697 | struct lang_identifier *li = (struct lang_identifier *) t; | |
14698 | struct lang_id2 *li2 = li->x; | |
14699 | ggc_mark_tree (li->namespace_bindings); | |
a8f73d4b | 14700 | ggc_mark_tree (li->bindings); |
4519c0a8 MM |
14701 | ggc_mark_tree (li->class_value); |
14702 | ggc_mark_tree (li->class_template_info); | |
14703 | ||
14704 | if (li2) | |
14705 | { | |
14706 | ggc_mark_tree (li2->label_value); | |
14707 | ggc_mark_tree (li2->implicit_decl); | |
14708 | ggc_mark_tree (li2->error_locus); | |
14709 | } | |
14710 | } | |
87e3dbc9 MM |
14711 | else if (code == CPLUS_BINDING) |
14712 | { | |
14713 | if (BINDING_HAS_LEVEL_P (t)) | |
14714 | mark_binding_level (&BINDING_LEVEL (t)); | |
14715 | else | |
14716 | ggc_mark_tree (BINDING_SCOPE (t)); | |
14717 | ggc_mark_tree (BINDING_VALUE (t)); | |
14718 | } | |
14719 | else if (code == OVERLOAD) | |
14720 | ggc_mark_tree (OVL_FUNCTION (t)); | |
14721 | else if (code == TEMPLATE_PARM_INDEX) | |
14722 | ggc_mark_tree (TEMPLATE_PARM_DECL (t)); | |
4519c0a8 MM |
14723 | else if (TREE_CODE_CLASS (code) == 'd') |
14724 | { | |
14725 | struct lang_decl *ld = DECL_LANG_SPECIFIC (t); | |
14726 | ||
14727 | if (ld) | |
14728 | { | |
d60f72ae | 14729 | ggc_mark (ld); |
8f17b5c5 | 14730 | c_mark_lang_decl (&ld->decl_flags.base); |
5362b086 | 14731 | if (!DECL_GLOBAL_CTOR_P (t) |
6462c441 | 14732 | && !DECL_GLOBAL_DTOR_P (t) |
8e4ce833 JJ |
14733 | && !DECL_THUNK_P (t) |
14734 | && !DECL_DISCRIMINATOR_P (t)) | |
af3b4e59 | 14735 | ggc_mark_tree (ld->decl_flags.u2.access); |
31f8e4f3 MM |
14736 | else if (DECL_THUNK_P (t)) |
14737 | ggc_mark_tree (ld->decl_flags.u2.vcall_offset); | |
4519c0a8 MM |
14738 | if (TREE_CODE (t) != NAMESPACE_DECL) |
14739 | ggc_mark_tree (ld->decl_flags.u.template_info); | |
16bb3387 JM |
14740 | else |
14741 | mark_binding_level (&NAMESPACE_LEVEL (t)); | |
4519c0a8 MM |
14742 | if (CAN_HAVE_FULL_LANG_DECL_P (t)) |
14743 | { | |
4519c0a8 | 14744 | ggc_mark_tree (ld->befriending_classes); |
8f17b5c5 | 14745 | ggc_mark_tree (ld->context); |
db9b2174 | 14746 | ggc_mark_tree (ld->cloned_function); |
4519c0a8 MM |
14747 | if (TREE_CODE (t) == TYPE_DECL) |
14748 | ggc_mark_tree (ld->u.sorted_fields); | |
59026e79 MM |
14749 | else if (TREE_CODE (t) == FUNCTION_DECL |
14750 | && !DECL_PENDING_INLINE_P (t)) | |
14751 | mark_lang_function (DECL_SAVED_FUNCTION_DATA (t)); | |
4519c0a8 MM |
14752 | } |
14753 | } | |
14754 | } | |
14755 | else if (TREE_CODE_CLASS (code) == 't') | |
14756 | { | |
14757 | struct lang_type *lt = TYPE_LANG_SPECIFIC (t); | |
14758 | ||
68642fb6 | 14759 | if (lt && !(TREE_CODE (t) == POINTER_TYPE |
87e3dbc9 | 14760 | && TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)) |
4519c0a8 | 14761 | { |
c27db0df | 14762 | ggc_mark (lt); |
911a71a7 | 14763 | ggc_mark_tree (lt->primary_base); |
4519c0a8 MM |
14764 | ggc_mark_tree (lt->vfields); |
14765 | ggc_mark_tree (lt->vbases); | |
14766 | ggc_mark_tree (lt->tags); | |
4519c0a8 | 14767 | ggc_mark_tree (lt->size); |
fee7654e | 14768 | ggc_mark_tree (lt->pure_virtuals); |
4519c0a8 MM |
14769 | ggc_mark_tree (lt->friend_classes); |
14770 | ggc_mark_tree (lt->rtti); | |
14771 | ggc_mark_tree (lt->methods); | |
14772 | ggc_mark_tree (lt->template_info); | |
87e3dbc9 | 14773 | ggc_mark_tree (lt->befriending_classes); |
4519c0a8 | 14774 | } |
87e3dbc9 MM |
14775 | else if (lt) |
14776 | /* In the case of pointer-to-member function types, the | |
14777 | TYPE_LANG_SPECIFIC is really just a tree. */ | |
14778 | ggc_mark_tree ((tree) lt); | |
4519c0a8 MM |
14779 | } |
14780 | } | |
5fd8e536 JM |
14781 | |
14782 | /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since | |
14783 | the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */ | |
14784 | ||
14785 | tree | |
14786 | identifier_global_value (t) | |
14787 | tree t; | |
14788 | { | |
14789 | return IDENTIFIER_GLOBAL_VALUE (t); | |
14790 | } | |
d363e7bf | 14791 | |
eaa7c03f JM |
14792 | /* Build the void_list_node (void_type_node having been created). */ |
14793 | tree | |
14794 | build_void_list_node () | |
14795 | { | |
14796 | tree t = build_tree_list (NULL_TREE, void_type_node); | |
14797 | TREE_PARMLIST (t) = 1; | |
14798 | return t; | |
14799 | } | |
14800 | ||
d363e7bf AJ |
14801 | static int |
14802 | cp_missing_noreturn_ok_p (decl) | |
14803 | tree decl; | |
14804 | { | |
14805 | /* A missing noreturn is ok for the `main' function. */ | |
92643fea | 14806 | return DECL_MAIN_P (decl); |
d363e7bf | 14807 | } |