]>
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 | |
91d231cb | 52 | extern const struct attribute_spec *lang_attribute_table; |
62c154ed | 53 | |
255512c1 | 54 | #ifndef BOOL_TYPE_SIZE |
3461fba7 | 55 | /* `bool' has size and alignment `1', on all platforms. */ |
d2e5ee5c | 56 | #define BOOL_TYPE_SIZE CHAR_TYPE_SIZE |
e1cd6e56 | 57 | #endif |
255512c1 | 58 | |
5cce22b6 | 59 | static tree grokparms PARAMS ((tree)); |
158991b7 | 60 | static const char *redeclaration_error_message PARAMS ((tree, tree)); |
8d08fdba | 61 | |
158991b7 | 62 | static void push_binding_level PARAMS ((struct binding_level *, int, |
49c249e1 | 63 | int)); |
158991b7 KG |
64 | static void pop_binding_level PARAMS ((void)); |
65 | static void suspend_binding_level PARAMS ((void)); | |
66 | static void resume_binding_level PARAMS ((struct binding_level *)); | |
67 | static struct binding_level *make_binding_level PARAMS ((void)); | |
68 | static void declare_namespace_level PARAMS ((void)); | |
6625cdb5 | 69 | static int decl_jump_unsafe PARAMS ((tree)); |
158991b7 KG |
70 | static void storedecls PARAMS ((tree)); |
71 | static void require_complete_types_for_parms PARAMS ((tree)); | |
596ea4e5 AS |
72 | static int ambi_op_p PARAMS ((enum tree_code)); |
73 | static int unary_op_p PARAMS ((enum tree_code)); | |
158991b7 KG |
74 | static tree store_bindings PARAMS ((tree, tree)); |
75 | static tree lookup_tag_reverse PARAMS ((tree, tree)); | |
76 | static tree obscure_complex_init PARAMS ((tree, tree)); | |
158991b7 | 77 | static tree lookup_name_real PARAMS ((tree, int, int, int)); |
8e4ce833 | 78 | static void push_local_name PARAMS ((tree)); |
158991b7 | 79 | static void warn_extern_redeclared_static PARAMS ((tree, tree)); |
8e4ce833 | 80 | static tree grok_reference_init PARAMS ((tree, tree, tree)); |
158991b7 | 81 | static tree grokfndecl PARAMS ((tree, tree, tree, tree, int, |
2c73f9f5 | 82 | enum overload_flags, tree, |
7a8f9fa9 | 83 | tree, int, int, int, int, int, int, tree)); |
158991b7 | 84 | static tree grokvardecl PARAMS ((tree, tree, RID_BIT_TYPE *, int, int, tree)); |
4782bd5b | 85 | static tree follow_tag_typedef PARAMS ((tree)); |
158991b7 | 86 | static tree lookup_tag PARAMS ((enum tree_code, tree, |
49c249e1 JM |
87 | struct binding_level *, int)); |
88 | static void set_identifier_type_value_with_scope | |
158991b7 | 89 | PARAMS ((tree, tree, struct binding_level *)); |
158991b7 | 90 | static void record_unknown_type PARAMS ((tree, const char *)); |
d52e4867 RS |
91 | static tree builtin_function_1 PARAMS ((const char *, tree, tree, int, |
92 | enum built_in_class, const char *)); | |
596ea4e5 | 93 | static tree build_library_fn_1 PARAMS ((tree, enum tree_code, tree)); |
158991b7 KG |
94 | static int member_function_or_else PARAMS ((tree, tree, enum overload_flags)); |
95 | static void bad_specifiers PARAMS ((tree, const char *, int, int, int, int, | |
49c249e1 | 96 | int)); |
158991b7 KG |
97 | static tree maybe_process_template_type_declaration PARAMS ((tree, int, struct binding_level*)); |
98 | static void check_for_uninitialized_const_var PARAMS ((tree)); | |
99 | static unsigned long typename_hash PARAMS ((hash_table_key)); | |
d6edb99e | 100 | static bool typename_compare PARAMS ((hash_table_key, hash_table_key)); |
158991b7 KG |
101 | static void push_binding PARAMS ((tree, tree, struct binding_level*)); |
102 | static int add_binding PARAMS ((tree, tree)); | |
103 | static void pop_binding PARAMS ((tree, tree)); | |
104 | static tree local_variable_p_walkfn PARAMS ((tree *, int *, void *)); | |
105 | static tree find_binding PARAMS ((tree, tree)); | |
106 | static tree select_decl PARAMS ((tree, int)); | |
107 | static int lookup_flags PARAMS ((int, int)); | |
108 | static tree qualify_lookup PARAMS ((tree, int)); | |
109 | static tree record_builtin_java_type PARAMS ((const char *, int)); | |
110 | static const char *tag_name PARAMS ((enum tag_types code)); | |
111 | static void find_class_binding_level PARAMS ((void)); | |
112 | static struct binding_level *innermost_nonclass_level PARAMS ((void)); | |
113 | static void warn_about_implicit_typename_lookup PARAMS ((tree, tree)); | |
114 | static int walk_namespaces_r PARAMS ((tree, walk_namespaces_fn, void *)); | |
115 | static int walk_globals_r PARAMS ((tree, void *)); | |
116 | static void add_decl_to_level PARAMS ((tree, struct binding_level *)); | |
117 | static tree make_label_decl PARAMS ((tree, int)); | |
6625cdb5 JM |
118 | static void use_label PARAMS ((tree)); |
119 | static void check_previous_goto_1 PARAMS ((tree, struct binding_level *, tree, | |
120 | const char *, int)); | |
121 | static void check_previous_goto PARAMS ((struct named_label_use_list *)); | |
122 | static void check_switch_goto PARAMS ((struct binding_level *)); | |
123 | static void check_previous_gotos PARAMS ((tree)); | |
124 | static void pop_label PARAMS ((tree, tree)); | |
158991b7 KG |
125 | static void pop_labels PARAMS ((tree)); |
126 | static void maybe_deduce_size_from_array_init PARAMS ((tree, tree)); | |
127 | static void layout_var_decl PARAMS ((tree)); | |
128 | static void maybe_commonize_var PARAMS ((tree)); | |
129 | static tree check_initializer PARAMS ((tree, tree)); | |
130 | static void make_rtl_for_nonlocal_decl PARAMS ((tree, tree, const char *)); | |
131 | static void push_cp_function_context PARAMS ((struct function *)); | |
132 | static void pop_cp_function_context PARAMS ((struct function *)); | |
133 | static void mark_binding_level PARAMS ((void *)); | |
2f8edb39 | 134 | static void mark_named_label_lists PARAMS ((void *, void *)); |
158991b7 KG |
135 | static void mark_cp_function_context PARAMS ((struct function *)); |
136 | static void mark_saved_scope PARAMS ((void *)); | |
ae499cce | 137 | static void mark_lang_function PARAMS ((struct cp_language_function *)); |
158991b7 | 138 | static void save_function_data PARAMS ((tree)); |
f444e36b | 139 | static void check_function_type PARAMS ((tree, tree)); |
158991b7 | 140 | static void destroy_local_var PARAMS ((tree)); |
cdd2559c | 141 | static void begin_constructor_body PARAMS ((void)); |
158991b7 | 142 | static void finish_constructor_body PARAMS ((void)); |
cdd2559c | 143 | static void begin_destructor_body PARAMS ((void)); |
158991b7 KG |
144 | static void finish_destructor_body PARAMS ((void)); |
145 | static tree create_array_type_for_decl PARAMS ((tree, tree, tree)); | |
146 | static tree get_atexit_node PARAMS ((void)); | |
147 | static tree get_dso_handle_node PARAMS ((void)); | |
148 | static tree start_cleanup_fn PARAMS ((void)); | |
149 | static void end_cleanup_fn PARAMS ((void)); | |
0ba8a114 | 150 | static tree cp_make_fname_decl PARAMS ((tree, int)); |
d43829f9 | 151 | static void initialize_predefined_identifiers PARAMS ((void)); |
5362b086 | 152 | static tree check_special_function_return_type |
1f84ec23 | 153 | PARAMS ((special_function_kind, tree, tree)); |
596ea4e5 AS |
154 | static tree push_cp_library_fn PARAMS ((enum tree_code, tree)); |
155 | static tree build_cp_library_fn PARAMS ((tree, enum tree_code, tree)); | |
f444e36b | 156 | static void store_parm_decls PARAMS ((tree)); |
d363e7bf | 157 | static int cp_missing_noreturn_ok_p PARAMS ((tree)); |
8d08fdba | 158 | |
69ac77ce | 159 | #if defined (DEBUG_CP_BINDING_LEVELS) |
158991b7 | 160 | static void indent PARAMS ((void)); |
69ac77ce JL |
161 | #endif |
162 | ||
8d08fdba MS |
163 | /* Erroneous argument lists can use this *IFF* they do not modify it. */ |
164 | tree error_mark_list; | |
165 | ||
7f4edbcb | 166 | /* The following symbols are subsumed in the cp_global_trees array, and |
68642fb6 | 167 | listed here individually for documentation purposes. |
8d08fdba | 168 | |
7f4edbcb BS |
169 | C++ extensions |
170 | tree wchar_decl_node; | |
37c46b43 | 171 | |
7f4edbcb BS |
172 | tree vtable_entry_type; |
173 | tree delta_type_node; | |
7f4edbcb | 174 | tree __t_desc_type_node; |
db1147b2 | 175 | tree ti_desc_type_node; |
1f4cb92b | 176 | tree bltn_desc_type_node, ptr_desc_type_node; |
db1147b2 NS |
177 | tree ary_desc_type_node, func_desc_type_node, enum_desc_type_node; |
178 | tree class_desc_type_node, si_class_desc_type_node, vmi_class_desc_type_node; | |
387769ed | 179 | tree ptm_desc_type_node; |
db1147b2 | 180 | tree base_desc_type_node; |
8d08fdba | 181 | |
7f4edbcb BS |
182 | tree class_type_node, record_type_node, union_type_node, enum_type_node; |
183 | tree unknown_type_node; | |
8d08fdba | 184 | |
7f4edbcb | 185 | Array type `vtable_entry_type[]' |
8d08fdba | 186 | |
7f4edbcb BS |
187 | tree vtbl_type_node; |
188 | tree vtbl_ptr_type_node; | |
8d08fdba | 189 | |
2854d3c6 | 190 | Namespaces, |
8d08fdba | 191 | |
7f4edbcb | 192 | tree std_node; |
2854d3c6 | 193 | tree abi_node; |
8d08fdba | 194 | |
7f4edbcb BS |
195 | A FUNCTION_DECL which can call `abort'. Not necessarily the |
196 | one that the user will declare, but sufficient to be called | |
197 | by routines that want to abort the program. | |
8d08fdba | 198 | |
7f4edbcb | 199 | tree abort_fndecl; |
8d08fdba | 200 | |
7f4edbcb | 201 | The FUNCTION_DECL for the default `::operator delete'. |
2986ae00 | 202 | |
7f4edbcb | 203 | tree global_delete_fndecl; |
8d08fdba | 204 | |
7f4edbcb | 205 | Used by RTTI |
669ec2b4 JM |
206 | tree type_info_type_node, tinfo_decl_id, tinfo_decl_type; |
207 | tree tinfo_var_id; | |
208 | ||
7f4edbcb | 209 | */ |
8d08fdba | 210 | |
7f4edbcb | 211 | tree cp_global_trees[CPTI_MAX]; |
8d08fdba | 212 | |
2c73f9f5 | 213 | /* Indicates that there is a type value in some namespace, although |
7f4edbcb | 214 | that is not necessarily in scope at the moment. */ |
2c73f9f5 ML |
215 | |
216 | static tree global_type_node; | |
217 | ||
3e3f722c ML |
218 | /* Expect only namespace names now. */ |
219 | static int only_namespace_names; | |
220 | ||
6625cdb5 JM |
221 | /* Used only for jumps to as-yet undefined labels, since jumps to |
222 | defined labels can have their validity checked immediately. */ | |
223 | ||
224 | struct named_label_use_list | |
e349ee73 MS |
225 | { |
226 | struct binding_level *binding_level; | |
227 | tree names_in_scope; | |
228 | tree label_decl; | |
9c0758dd | 229 | const char *filename_o_goto; |
e349ee73 | 230 | int lineno_o_goto; |
6625cdb5 | 231 | struct named_label_use_list *next; |
e349ee73 MS |
232 | }; |
233 | ||
ed5511d9 | 234 | #define named_label_uses cp_function_chain->x_named_label_uses |
8d08fdba | 235 | |
8e4ce833 JJ |
236 | #define local_names cp_function_chain->x_local_names |
237 | ||
8d08fdba MS |
238 | /* A list of objects which have constructors or destructors |
239 | which reside in the global scope. The decl is stored in | |
240 | the TREE_VALUE slot and the initializer is stored | |
241 | in the TREE_PURPOSE slot. */ | |
242 | tree static_aggregates; | |
243 | ||
8d08fdba MS |
244 | /* -- end of C++ */ |
245 | ||
81b3411c | 246 | /* A node for the integer constants 2, and 3. */ |
d11ad92e | 247 | |
81b3411c | 248 | tree integer_two_node, integer_three_node; |
8d08fdba | 249 | |
8d08fdba | 250 | /* Similar, for last_function_parm_tags. */ |
9cd64686 | 251 | tree last_function_parms; |
8d08fdba | 252 | |
6625cdb5 JM |
253 | /* A list of all LABEL_DECLs in the function that have names. Here so |
254 | we can clear out their names' definitions at the end of the | |
255 | function, and so we can check the validity of jumps to these labels. */ | |
256 | ||
257 | struct named_label_list | |
258 | { | |
259 | struct binding_level *binding_level; | |
260 | tree names_in_scope; | |
261 | tree old_value; | |
262 | tree label_decl; | |
263 | tree bad_decls; | |
6625cdb5 | 264 | struct named_label_list *next; |
826840d9 RH |
265 | unsigned int in_try_scope : 1; |
266 | unsigned int in_catch_scope : 1; | |
6625cdb5 | 267 | }; |
8d08fdba | 268 | |
4519c0a8 | 269 | #define named_labels cp_function_chain->x_named_labels |
8d08fdba | 270 | |
b8458e3e JM |
271 | /* Nonzero means use the ISO C94 dialect of C. */ |
272 | ||
273 | int flag_isoc94; | |
274 | ||
83309c26 | 275 | /* Nonzero means use the ISO C99 dialect of C. */ |
68642fb6 | 276 | |
83309c26 | 277 | int flag_isoc99; |
68642fb6 | 278 | |
93e2382f JM |
279 | /* Nonzero means we are a hosted implementation for code shared with C. */ |
280 | ||
281 | int flag_hosted = 1; | |
282 | ||
283 | /* Nonzero means add default format_arg attributes for functions not | |
284 | in ISO C. */ | |
285 | ||
286 | int flag_noniso_default_format_attributes = 1; | |
287 | ||
8d08fdba MS |
288 | /* Nonzero if we want to conserve space in the .o files. We do this |
289 | by putting uninitialized data and runtime initialized data into | |
ddd5a7c1 | 290 | .common instead of .data at the expense of not flagging multiple |
8d08fdba MS |
291 | definitions. */ |
292 | extern int flag_conserve_space; | |
8d08fdba | 293 | \f |
51c184be | 294 | /* C and C++ flags are in decl2.c. */ |
8d08fdba | 295 | |
e92cc029 MS |
296 | /* A expression of value 0 with the same precision as a sizetype |
297 | node, but signed. */ | |
298 | tree signed_size_zero_node; | |
299 | ||
0c8feefe MM |
300 | /* The name of the anonymous namespace, throughout this translation |
301 | unit. */ | |
302 | tree anonymous_namespace_name; | |
303 | ||
b2244c65 MM |
304 | /* The number of function bodies which we are currently processing. |
305 | (Zero if we are at namespace scope, one inside the body of a | |
306 | function, two inside the body of a function in a local class, etc.) */ | |
307 | int function_depth; | |
e23bd218 IR |
308 | |
309 | /* States indicating how grokdeclarator() should handle declspecs marked | |
310 | with __attribute__((deprecated)). An object declared as | |
311 | __attribute__((deprecated)) suppresses warnings of uses of other | |
312 | deprecated items. */ | |
313 | ||
314 | enum deprecated_states { | |
315 | DEPRECATED_NORMAL, | |
316 | DEPRECATED_SUPPRESS | |
317 | }; | |
318 | ||
319 | static enum deprecated_states deprecated_state = DEPRECATED_NORMAL; | |
320 | ||
321 | /* Set by add_implicitly_declared_members() to keep those members from | |
322 | being flagged as deprecated or reported as using deprecated | |
323 | types. */ | |
324 | int adding_implicit_members = 0; | |
8d08fdba MS |
325 | \f |
326 | /* For each binding contour we allocate a binding_level structure | |
e92cc029 MS |
327 | which records the names defined in that contour. |
328 | Contours include: | |
329 | 0) the global one | |
330 | 1) one for each function definition, | |
331 | where internal declarations of the parameters appear. | |
332 | 2) one for each compound statement, | |
333 | to record its declarations. | |
334 | ||
335 | The current meaning of a name can be found by searching the levels | |
336 | from the current one out to the global one. | |
337 | ||
338 | Off to the side, may be the class_binding_level. This exists only | |
339 | to catch class-local declarations. It is otherwise nonexistent. | |
340 | ||
341 | Also there may be binding levels that catch cleanups that must be | |
d8f8dca1 MM |
342 | run when exceptions occur. Thus, to see whether a name is bound in |
343 | the current scope, it is not enough to look in the | |
344 | CURRENT_BINDING_LEVEL. You should use lookup_name_current_level | |
345 | instead. */ | |
8d08fdba MS |
346 | |
347 | /* Note that the information in the `names' component of the global contour | |
348 | is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */ | |
349 | ||
350 | struct binding_level | |
351 | { | |
352 | /* A chain of _DECL nodes for all variables, constants, functions, | |
e92cc029 | 353 | and typedef types. These are in the reverse of the order |
f181d4ae MM |
354 | supplied. There may be OVERLOADs on this list, too, but they |
355 | are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD. */ | |
8d08fdba MS |
356 | tree names; |
357 | ||
e92cc029 MS |
358 | /* A list of structure, union and enum definitions, for looking up |
359 | tag names. | |
360 | It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name, | |
361 | or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE, | |
362 | or ENUMERAL_TYPE node. | |
363 | ||
364 | C++: the TREE_VALUE nodes can be simple types for | |
365 | component_bindings. */ | |
8d08fdba MS |
366 | tree tags; |
367 | ||
2c73f9f5 ML |
368 | /* A list of USING_DECL nodes. */ |
369 | tree usings; | |
370 | ||
ea9635c7 ML |
371 | /* A list of used namespaces. PURPOSE is the namespace, |
372 | VALUE the common ancestor with this binding_level's namespace. */ | |
373 | tree using_directives; | |
374 | ||
f181d4ae MM |
375 | /* If this binding level is the binding level for a class, then |
376 | class_shadowed is a TREE_LIST. The TREE_PURPOSE of each node | |
76191fdd | 377 | is the name of an entity bound in the class. The TREE_TYPE is |
d8f8dca1 | 378 | the DECL bound by this name in the class. */ |
8d08fdba MS |
379 | tree class_shadowed; |
380 | ||
f181d4ae | 381 | /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and |
76191fdd NS |
382 | is used for all binding levels. In addition the TREE_VALUE is the |
383 | IDENTIFIER_TYPE_VALUE before we entered the class. */ | |
8d08fdba MS |
384 | tree type_shadowed; |
385 | ||
acef433b MM |
386 | /* A TREE_LIST. Each TREE_VALUE is the LABEL_DECL for a local |
387 | label in this scope. The TREE_PURPOSE is the previous value of | |
388 | the IDENTIFIER_LABEL VALUE. */ | |
389 | tree shadowed_labels; | |
390 | ||
8d08fdba MS |
391 | /* For each level (except not the global one), |
392 | a chain of BLOCK nodes for all the levels | |
393 | that were entered and exited one level down. */ | |
394 | tree blocks; | |
395 | ||
70adf8a9 JM |
396 | /* The _TYPE node for this level, if parm_flag == 2. */ |
397 | tree this_class; | |
398 | ||
8d08fdba MS |
399 | /* The binding level which this one is contained in (inherits from). */ |
400 | struct binding_level *level_chain; | |
401 | ||
8d6e462b | 402 | /* List of VAR_DECLS saved from a previous for statement. |
cab1f180 | 403 | These would be dead in ISO-conforming code, but might |
f181d4ae MM |
404 | be referenced in ARM-era code. These are stored in a |
405 | TREE_LIST; the TREE_VALUE is the actual declaration. */ | |
8d6e462b PB |
406 | tree dead_vars_from_for; |
407 | ||
8d08fdba | 408 | /* 1 for the level that holds the parameters of a function. |
74b846e0 MM |
409 | 2 for the level that holds a class declaration. */ |
410 | unsigned parm_flag : 2; | |
8d08fdba MS |
411 | |
412 | /* 1 means make a BLOCK for this level regardless of all else. | |
413 | 2 for temporary binding contours created by the compiler. */ | |
74b846e0 | 414 | unsigned keep : 2; |
8d08fdba MS |
415 | |
416 | /* Nonzero if this level "doesn't exist" for tags. */ | |
417 | unsigned tag_transparent : 1; | |
418 | ||
419 | /* Nonzero if this level can safely have additional | |
420 | cleanup-needing variables added to it. */ | |
421 | unsigned more_cleanups_ok : 1; | |
422 | unsigned have_cleanups : 1; | |
423 | ||
74b846e0 | 424 | /* Nonzero if this scope is for storing the decls for template |
5566b478 MS |
425 | parameters and generic decls; these decls will be discarded and |
426 | replaced with a TEMPLATE_DECL. */ | |
74b846e0 MM |
427 | unsigned template_parms_p : 1; |
428 | ||
5362b086 | 429 | /* Nonzero if this scope corresponds to the `<>' in a |
74b846e0 MM |
430 | `template <>' clause. Whenever this flag is set, |
431 | TEMPLATE_PARMS_P will be set as well. */ | |
432 | unsigned template_spec_p : 1; | |
8d08fdba | 433 | |
a9aedbc2 MS |
434 | /* This is set for a namespace binding level. */ |
435 | unsigned namespace_p : 1; | |
436 | ||
2ee887f2 | 437 | /* True if this level is that of a for-statement where we need to |
cab1f180 | 438 | worry about ambiguous (ARM or ISO) scope rules. */ |
8d6e462b PB |
439 | unsigned is_for_scope : 1; |
440 | ||
826840d9 RH |
441 | /* True if this level corresponds to a TRY block. Currently this |
442 | information is only available while building the tree structure. */ | |
443 | unsigned is_try_scope : 1; | |
227cf171 | 444 | |
826840d9 RH |
445 | /* True if this level corresponds to a CATCH block. Currently this |
446 | information is only available while building the tree structure. */ | |
447 | unsigned is_catch_scope : 1; | |
448 | ||
449 | /* Three bits left for this word. */ | |
8d08fdba MS |
450 | |
451 | #if defined(DEBUG_CP_BINDING_LEVELS) | |
452 | /* Binding depth at which this level began. */ | |
453 | unsigned binding_depth; | |
454 | #endif /* defined(DEBUG_CP_BINDING_LEVELS) */ | |
455 | }; | |
456 | ||
457 | #define NULL_BINDING_LEVEL ((struct binding_level *) NULL) | |
68642fb6 | 458 | |
8f032717 | 459 | /* The binding level currently in effect. */ |
8d08fdba | 460 | |
a8f73d4b | 461 | #define current_binding_level \ |
52a11cbf | 462 | (cfun && cp_function_chain->bindings \ |
d363e7bf | 463 | ? cp_function_chain->bindings \ |
a8f73d4b | 464 | : scope_chain->bindings) |
8d08fdba MS |
465 | |
466 | /* The binding level of the current class, if any. */ | |
467 | ||
9cd64686 | 468 | #define class_binding_level scope_chain->class_bindings |
8d08fdba | 469 | |
8d08fdba MS |
470 | /* A chain of binding_level structures awaiting reuse. */ |
471 | ||
472 | static struct binding_level *free_binding_level; | |
473 | ||
474 | /* The outermost binding level, for names of file scope. | |
475 | This is created when the compiler is started and exists | |
476 | through the entire run. */ | |
477 | ||
478 | static struct binding_level *global_binding_level; | |
479 | ||
8d08fdba MS |
480 | /* Nonzero means unconditionally make a BLOCK for the next level pushed. */ |
481 | ||
482 | static int keep_next_level_flag; | |
483 | ||
0154eaa8 MM |
484 | /* A TREE_LIST of VAR_DECLs. The TREE_PURPOSE is a RECORD_TYPE or |
485 | UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type. At the | |
486 | time the VAR_DECL was declared, the type was incomplete. */ | |
487 | ||
488 | static tree incomplete_vars; | |
489 | ||
8d08fdba MS |
490 | #if defined(DEBUG_CP_BINDING_LEVELS) |
491 | static int binding_depth = 0; | |
492 | static int is_class_level = 0; | |
493 | ||
494 | static void | |
495 | indent () | |
496 | { | |
497 | register unsigned i; | |
498 | ||
499 | for (i = 0; i < binding_depth*2; i++) | |
500 | putc (' ', stderr); | |
501 | } | |
502 | #endif /* defined(DEBUG_CP_BINDING_LEVELS) */ | |
503 | ||
158991b7 | 504 | static tree pushdecl_with_scope PARAMS ((tree, struct binding_level *)); |
8d08fdba MS |
505 | |
506 | static void | |
507 | push_binding_level (newlevel, tag_transparent, keep) | |
508 | struct binding_level *newlevel; | |
509 | int tag_transparent, keep; | |
510 | { | |
511 | /* Add this level to the front of the chain (stack) of levels that | |
512 | are active. */ | |
961192e1 | 513 | memset ((char*) newlevel, 0, sizeof (struct binding_level)); |
8f032717 | 514 | newlevel->level_chain = current_binding_level; |
8d08fdba MS |
515 | current_binding_level = newlevel; |
516 | newlevel->tag_transparent = tag_transparent; | |
517 | newlevel->more_cleanups_ok = 1; | |
227cf171 | 518 | |
8d08fdba MS |
519 | newlevel->keep = keep; |
520 | #if defined(DEBUG_CP_BINDING_LEVELS) | |
521 | newlevel->binding_depth = binding_depth; | |
522 | indent (); | |
523 | fprintf (stderr, "push %s level 0x%08x line %d\n", | |
524 | (is_class_level) ? "class" : "block", newlevel, lineno); | |
525 | is_class_level = 0; | |
526 | binding_depth++; | |
527 | #endif /* defined(DEBUG_CP_BINDING_LEVELS) */ | |
528 | } | |
529 | ||
8f032717 MM |
530 | /* Find the innermost enclosing class scope, and reset |
531 | CLASS_BINDING_LEVEL appropriately. */ | |
532 | ||
533 | static void | |
534 | find_class_binding_level () | |
535 | { | |
536 | struct binding_level *level = current_binding_level; | |
537 | ||
538 | while (level && level->parm_flag != 2) | |
539 | level = level->level_chain; | |
540 | if (level && level->parm_flag == 2) | |
541 | class_binding_level = level; | |
542 | else | |
543 | class_binding_level = 0; | |
544 | } | |
545 | ||
8d08fdba MS |
546 | static void |
547 | pop_binding_level () | |
548 | { | |
8d08fdba MS |
549 | if (global_binding_level) |
550 | { | |
2c73f9f5 | 551 | /* Cannot pop a level, if there are none left to pop. */ |
8d08fdba | 552 | if (current_binding_level == global_binding_level) |
a98facb0 | 553 | abort (); |
8d08fdba MS |
554 | } |
555 | /* Pop the current level, and free the structure for reuse. */ | |
556 | #if defined(DEBUG_CP_BINDING_LEVELS) | |
557 | binding_depth--; | |
558 | indent (); | |
559 | fprintf (stderr, "pop %s level 0x%08x line %d\n", | |
560 | (is_class_level) ? "class" : "block", | |
561 | current_binding_level, lineno); | |
562 | if (is_class_level != (current_binding_level == class_binding_level)) | |
9e9ff709 MS |
563 | { |
564 | indent (); | |
565 | fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n"); | |
566 | } | |
8d08fdba MS |
567 | is_class_level = 0; |
568 | #endif /* defined(DEBUG_CP_BINDING_LEVELS) */ | |
569 | { | |
570 | register struct binding_level *level = current_binding_level; | |
571 | current_binding_level = current_binding_level->level_chain; | |
572 | level->level_chain = free_binding_level; | |
573 | #if 0 /* defined(DEBUG_CP_BINDING_LEVELS) */ | |
574 | if (level->binding_depth != binding_depth) | |
575 | abort (); | |
576 | #endif /* defined(DEBUG_CP_BINDING_LEVELS) */ | |
8f032717 MM |
577 | free_binding_level = level; |
578 | find_class_binding_level (); | |
8d08fdba MS |
579 | } |
580 | } | |
a9aedbc2 MS |
581 | |
582 | static void | |
583 | suspend_binding_level () | |
584 | { | |
585 | if (class_binding_level) | |
586 | current_binding_level = class_binding_level; | |
587 | ||
588 | if (global_binding_level) | |
589 | { | |
2c73f9f5 | 590 | /* Cannot suspend a level, if there are none left to suspend. */ |
a9aedbc2 | 591 | if (current_binding_level == global_binding_level) |
a98facb0 | 592 | abort (); |
a9aedbc2 MS |
593 | } |
594 | /* Suspend the current level. */ | |
595 | #if defined(DEBUG_CP_BINDING_LEVELS) | |
596 | binding_depth--; | |
597 | indent (); | |
598 | fprintf (stderr, "suspend %s level 0x%08x line %d\n", | |
599 | (is_class_level) ? "class" : "block", | |
600 | current_binding_level, lineno); | |
601 | if (is_class_level != (current_binding_level == class_binding_level)) | |
9e9ff709 MS |
602 | { |
603 | indent (); | |
604 | fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n"); | |
605 | } | |
a9aedbc2 MS |
606 | is_class_level = 0; |
607 | #endif /* defined(DEBUG_CP_BINDING_LEVELS) */ | |
8f032717 MM |
608 | current_binding_level = current_binding_level->level_chain; |
609 | find_class_binding_level (); | |
a9aedbc2 MS |
610 | } |
611 | ||
824b9a4c | 612 | static void |
a9aedbc2 MS |
613 | resume_binding_level (b) |
614 | struct binding_level *b; | |
615 | { | |
2c73f9f5 ML |
616 | /* Resuming binding levels is meant only for namespaces, |
617 | and those cannot nest into classes. */ | |
618 | my_friendly_assert(!class_binding_level, 386); | |
619 | /* Also, resuming a non-directly nested namespace is a no-no. */ | |
620 | my_friendly_assert(b->level_chain == current_binding_level, 386); | |
a9aedbc2 MS |
621 | current_binding_level = b; |
622 | #if defined(DEBUG_CP_BINDING_LEVELS) | |
623 | b->binding_depth = binding_depth; | |
624 | indent (); | |
625 | fprintf (stderr, "resume %s level 0x%08x line %d\n", | |
626 | (is_class_level) ? "class" : "block", b, lineno); | |
627 | is_class_level = 0; | |
628 | binding_depth++; | |
629 | #endif /* defined(DEBUG_CP_BINDING_LEVELS) */ | |
630 | } | |
8d08fdba | 631 | \f |
cffa8729 MS |
632 | /* Create a new `struct binding_level'. */ |
633 | ||
634 | static | |
635 | struct binding_level * | |
636 | make_binding_level () | |
637 | { | |
638 | /* NOSTRICT */ | |
639 | return (struct binding_level *) xmalloc (sizeof (struct binding_level)); | |
640 | } | |
641 | ||
8d08fdba MS |
642 | /* Nonzero if we are currently in the global binding level. */ |
643 | ||
644 | int | |
645 | global_bindings_p () | |
646 | { | |
647 | return current_binding_level == global_binding_level; | |
648 | } | |
649 | ||
8f032717 MM |
650 | /* Return the innermost binding level that is not for a class scope. */ |
651 | ||
652 | static struct binding_level * | |
653 | innermost_nonclass_level () | |
654 | { | |
655 | struct binding_level *b; | |
656 | ||
657 | b = current_binding_level; | |
658 | while (b->parm_flag == 2) | |
659 | b = b->level_chain; | |
660 | ||
661 | return b; | |
662 | } | |
663 | ||
a9aedbc2 MS |
664 | /* Nonzero if we are currently in a toplevel binding level. This |
665 | means either the global binding level or a namespace in a toplevel | |
8f032717 | 666 | binding level. Since there are no non-toplevel namespace levels, |
74b846e0 MM |
667 | this really means any namespace or template parameter level. We |
668 | also include a class whose context is toplevel. */ | |
a9aedbc2 MS |
669 | |
670 | int | |
671 | toplevel_bindings_p () | |
672 | { | |
8f032717 MM |
673 | struct binding_level *b = innermost_nonclass_level (); |
674 | ||
74b846e0 | 675 | return b->namespace_p || b->template_parms_p; |
a9aedbc2 MS |
676 | } |
677 | ||
8f032717 MM |
678 | /* Nonzero if this is a namespace scope, or if we are defining a class |
679 | which is itself at namespace scope, or whose enclosing class is | |
680 | such a class, etc. */ | |
a9aedbc2 | 681 | |
7bdbfa05 | 682 | int |
a9aedbc2 MS |
683 | namespace_bindings_p () |
684 | { | |
8f032717 MM |
685 | struct binding_level *b = innermost_nonclass_level (); |
686 | ||
687 | return b->namespace_p; | |
a9aedbc2 MS |
688 | } |
689 | ||
f1dedc31 MM |
690 | /* If KEEP is non-zero, make a BLOCK node for the next binding level, |
691 | unconditionally. Otherwise, use the normal logic to decide whether | |
692 | or not to create a BLOCK. */ | |
693 | ||
8d08fdba | 694 | void |
f1dedc31 MM |
695 | keep_next_level (keep) |
696 | int keep; | |
8d08fdba | 697 | { |
f1dedc31 | 698 | keep_next_level_flag = keep; |
8d08fdba MS |
699 | } |
700 | ||
701 | /* Nonzero if the current level needs to have a BLOCK made. */ | |
702 | ||
703 | int | |
704 | kept_level_p () | |
705 | { | |
706 | return (current_binding_level->blocks != NULL_TREE | |
707 | || current_binding_level->keep | |
708 | || current_binding_level->names != NULL_TREE | |
709 | || (current_binding_level->tags != NULL_TREE | |
710 | && !current_binding_level->tag_transparent)); | |
711 | } | |
712 | ||
824b9a4c | 713 | static void |
a9aedbc2 MS |
714 | declare_namespace_level () |
715 | { | |
716 | current_binding_level->namespace_p = 1; | |
717 | } | |
718 | ||
74b846e0 MM |
719 | /* Returns non-zero if this scope was created to store template |
720 | parameters. */ | |
721 | ||
8d08fdba | 722 | int |
74b846e0 | 723 | template_parm_scope_p () |
8d08fdba | 724 | { |
74b846e0 MM |
725 | return current_binding_level->template_parms_p; |
726 | } | |
727 | ||
728 | /* Returns the kind of template specialization we are currently | |
729 | processing, given that it's declaration contained N_CLASS_SCOPES | |
730 | explicit scope qualifications. */ | |
731 | ||
732 | tmpl_spec_kind | |
733 | current_tmpl_spec_kind (n_class_scopes) | |
734 | int n_class_scopes; | |
735 | { | |
736 | int n_template_parm_scopes = 0; | |
737 | int seen_specialization_p = 0; | |
738 | int innermost_specialization_p = 0; | |
739 | struct binding_level *b; | |
740 | ||
741 | /* Scan through the template parameter scopes. */ | |
742 | for (b = current_binding_level; b->template_parms_p; b = b->level_chain) | |
743 | { | |
744 | /* If we see a specialization scope inside a parameter scope, | |
745 | then something is wrong. That corresponds to a declaration | |
746 | like: | |
747 | ||
748 | template <class T> template <> ... | |
749 | ||
750 | which is always illegal since [temp.expl.spec] forbids the | |
751 | specialization of a class member template if the enclosing | |
752 | class templates are not explicitly specialized as well. */ | |
753 | if (b->template_spec_p) | |
754 | { | |
755 | if (n_template_parm_scopes == 0) | |
756 | innermost_specialization_p = 1; | |
757 | else | |
758 | seen_specialization_p = 1; | |
759 | } | |
760 | else if (seen_specialization_p == 1) | |
761 | return tsk_invalid_member_spec; | |
762 | ||
763 | ++n_template_parm_scopes; | |
764 | } | |
765 | ||
766 | /* Handle explicit instantiations. */ | |
767 | if (processing_explicit_instantiation) | |
768 | { | |
769 | if (n_template_parm_scopes != 0) | |
770 | /* We've seen a template parameter list during an explicit | |
771 | instantiation. For example: | |
772 | ||
773 | template <class T> template void f(int); | |
774 | ||
775 | This is erroneous. */ | |
776 | return tsk_invalid_expl_inst; | |
777 | else | |
778 | return tsk_expl_inst; | |
779 | } | |
780 | ||
781 | if (n_template_parm_scopes < n_class_scopes) | |
782 | /* We've not seen enough template headers to match all the | |
783 | specialized classes present. For example: | |
784 | ||
785 | template <class T> void R<T>::S<T>::f(int); | |
786 | ||
787 | This is illegal; there needs to be one set of template | |
788 | parameters for each class. */ | |
789 | return tsk_insufficient_parms; | |
790 | else if (n_template_parm_scopes == n_class_scopes) | |
791 | /* We're processing a non-template declaration (even though it may | |
792 | be a member of a template class.) For example: | |
793 | ||
794 | template <class T> void S<T>::f(int); | |
795 | ||
796 | The `class T' maches the `S<T>', leaving no template headers | |
797 | corresponding to the `f'. */ | |
798 | return tsk_none; | |
799 | else if (n_template_parm_scopes > n_class_scopes + 1) | |
800 | /* We've got too many template headers. For example: | |
801 | ||
802 | template <> template <class T> void f (T); | |
803 | ||
804 | There need to be more enclosing classes. */ | |
805 | return tsk_excessive_parms; | |
806 | else | |
807 | /* This must be a template. It's of the form: | |
808 | ||
809 | template <class T> template <class U> void S<T>::f(U); | |
810 | ||
811 | This is a specialization if the innermost level was a | |
812 | specialization; otherwise it's just a definition of the | |
813 | template. */ | |
814 | return innermost_specialization_p ? tsk_expl_spec : tsk_template; | |
8d08fdba MS |
815 | } |
816 | ||
817 | void | |
818 | set_class_shadows (shadows) | |
819 | tree shadows; | |
820 | { | |
821 | class_binding_level->class_shadowed = shadows; | |
822 | } | |
823 | ||
824 | /* Enter a new binding level. | |
825 | If TAG_TRANSPARENT is nonzero, do so only for the name space of variables, | |
826 | not for that of tags. */ | |
827 | ||
828 | void | |
829 | pushlevel (tag_transparent) | |
830 | int tag_transparent; | |
831 | { | |
b35d4555 | 832 | struct binding_level *newlevel; |
8d08fdba | 833 | |
01d939e8 | 834 | if (cfun && !doing_semantic_analysis_p ()) |
b35d4555 | 835 | return; |
8d08fdba | 836 | |
b35d4555 | 837 | /* Reuse or create a struct for this binding level. */ |
8d08fdba MS |
838 | #if defined(DEBUG_CP_BINDING_LEVELS) |
839 | if (0) | |
840 | #else /* !defined(DEBUG_CP_BINDING_LEVELS) */ | |
841 | if (free_binding_level) | |
842 | #endif /* !defined(DEBUG_CP_BINDING_LEVELS) */ | |
843 | { | |
844 | newlevel = free_binding_level; | |
845 | free_binding_level = free_binding_level->level_chain; | |
846 | } | |
847 | else | |
acef433b | 848 | newlevel = make_binding_level (); |
cffa8729 | 849 | |
8d08fdba | 850 | push_binding_level (newlevel, tag_transparent, keep_next_level_flag); |
8d08fdba MS |
851 | keep_next_level_flag = 0; |
852 | } | |
853 | ||
bd0d5d4a JM |
854 | /* We're defining an object of type TYPE. If it needs a cleanup, but |
855 | we're not allowed to add any more objects with cleanups to the current | |
856 | scope, create a new binding level. */ | |
857 | ||
858 | void | |
859 | maybe_push_cleanup_level (type) | |
860 | tree type; | |
861 | { | |
862 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) | |
863 | && current_binding_level->more_cleanups_ok == 0) | |
864 | { | |
865 | keep_next_level (2); | |
866 | pushlevel (1); | |
867 | clear_last_expr (); | |
868 | add_scope_stmt (/*begin_p=*/1, /*partial_p=*/1); | |
869 | } | |
870 | } | |
871 | ||
74b846e0 MM |
872 | /* Enter a new scope. The KIND indicates what kind of scope is being |
873 | created. */ | |
874 | ||
875 | void | |
876 | begin_scope (sk) | |
877 | scope_kind sk; | |
878 | { | |
879 | pushlevel (0); | |
880 | ||
881 | switch (sk) | |
882 | { | |
883 | case sk_template_spec: | |
884 | current_binding_level->template_spec_p = 1; | |
885 | /* Fall through. */ | |
886 | ||
887 | case sk_template_parms: | |
888 | current_binding_level->template_parms_p = 1; | |
889 | break; | |
890 | ||
891 | default: | |
a98facb0 | 892 | abort (); |
74b846e0 MM |
893 | } |
894 | } | |
895 | ||
896 | /* Exit the current scope. */ | |
897 | ||
898 | void | |
899 | finish_scope () | |
900 | { | |
901 | poplevel (0, 0, 0); | |
902 | } | |
903 | ||
5566b478 | 904 | void |
8d6e462b PB |
905 | note_level_for_for () |
906 | { | |
907 | current_binding_level->is_for_scope = 1; | |
908 | } | |
909 | ||
6625cdb5 JM |
910 | /* Record that the current binding level represents a try block. */ |
911 | ||
912 | void | |
826840d9 | 913 | note_level_for_try () |
6625cdb5 | 914 | { |
826840d9 RH |
915 | current_binding_level->is_try_scope = 1; |
916 | } | |
917 | ||
918 | /* Record that the current binding level represents a catch block. */ | |
919 | ||
920 | void | |
921 | note_level_for_catch () | |
922 | { | |
923 | current_binding_level->is_catch_scope = 1; | |
6625cdb5 JM |
924 | } |
925 | ||
f181d4ae MM |
926 | /* For a binding between a name and an entity at a block scope, |
927 | this is the `struct binding_level' for the block. */ | |
928 | #define BINDING_LEVEL(NODE) \ | |
75ac8dec | 929 | (((struct tree_binding*)(NODE))->scope.level) |
f181d4ae | 930 | |
4890c2f4 MM |
931 | /* A free list of CPLUS_BINDING nodes, connected by their |
932 | TREE_CHAINs. */ | |
933 | ||
934 | static tree free_bindings; | |
935 | ||
f181d4ae MM |
936 | /* Make DECL the innermost binding for ID. The LEVEL is the binding |
937 | level at which this declaration is being bound. */ | |
938 | ||
939 | static void | |
940 | push_binding (id, decl, level) | |
941 | tree id; | |
942 | tree decl; | |
943 | struct binding_level* level; | |
944 | { | |
945 | tree binding; | |
946 | ||
4890c2f4 MM |
947 | if (free_bindings) |
948 | { | |
949 | binding = free_bindings; | |
950 | free_bindings = TREE_CHAIN (binding); | |
951 | } | |
952 | else | |
953 | binding = make_node (CPLUS_BINDING); | |
f181d4ae MM |
954 | |
955 | /* Now, fill in the binding information. */ | |
956 | BINDING_VALUE (binding) = decl; | |
d8f8dca1 | 957 | BINDING_TYPE (binding) = NULL_TREE; |
f181d4ae | 958 | BINDING_LEVEL (binding) = level; |
8f032717 | 959 | INHERITED_VALUE_BINDING_P (binding) = 0; |
f181d4ae | 960 | LOCAL_BINDING_P (binding) = (level != class_binding_level); |
87e3dbc9 | 961 | BINDING_HAS_LEVEL_P (binding) = 1; |
f181d4ae | 962 | |
c45df9c1 | 963 | /* And put it on the front of the list of bindings for ID. */ |
f181d4ae MM |
964 | TREE_CHAIN (binding) = IDENTIFIER_BINDING (id); |
965 | IDENTIFIER_BINDING (id) = binding; | |
966 | } | |
967 | ||
d8f8dca1 MM |
968 | /* ID is already bound in the current scope. But, DECL is an |
969 | additional binding for ID in the same scope. This is the `struct | |
970 | stat' hack whereby a non-typedef class-name or enum-name can be | |
971 | bound at the same level as some other kind of entity. It's the | |
972 | responsibility of the caller to check that inserting this name is | |
8f032717 MM |
973 | legal here. Returns nonzero if the new binding was successful. */ |
974 | static int | |
d8f8dca1 MM |
975 | add_binding (id, decl) |
976 | tree id; | |
977 | tree decl; | |
978 | { | |
979 | tree binding = IDENTIFIER_BINDING (id); | |
8f032717 | 980 | int ok = 1; |
d8f8dca1 MM |
981 | |
982 | if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl)) | |
983 | /* The new name is the type name. */ | |
984 | BINDING_TYPE (binding) = decl; | |
8f032717 MM |
985 | else if (!BINDING_VALUE (binding)) |
986 | /* This situation arises when push_class_level_binding moves an | |
987 | inherited type-binding out of the way to make room for a new | |
988 | value binding. */ | |
989 | BINDING_VALUE (binding) = decl; | |
990 | else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL | |
991 | && DECL_ARTIFICIAL (BINDING_VALUE (binding))) | |
992 | { | |
993 | /* The old binding was a type name. It was placed in | |
994 | BINDING_VALUE because it was thought, at the point it was | |
995 | declared, to be the only entity with such a name. Move the | |
996 | type name into the type slot; it is now hidden by the new | |
997 | binding. */ | |
d8f8dca1 MM |
998 | BINDING_TYPE (binding) = BINDING_VALUE (binding); |
999 | BINDING_VALUE (binding) = decl; | |
8f032717 | 1000 | INHERITED_VALUE_BINDING_P (binding) = 0; |
d8f8dca1 | 1001 | } |
263505af MM |
1002 | else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL |
1003 | && TREE_CODE (decl) == TYPE_DECL | |
1004 | && DECL_NAME (decl) == DECL_NAME (BINDING_VALUE (binding)) | |
9aaceb4b NS |
1005 | && (same_type_p (TREE_TYPE (decl), |
1006 | TREE_TYPE (BINDING_VALUE (binding))) | |
1007 | /* If either type involves template parameters, we must | |
1008 | wait until instantiation. */ | |
1009 | || uses_template_parms (TREE_TYPE (decl)) | |
1010 | || uses_template_parms (TREE_TYPE (BINDING_VALUE (binding))))) | |
263505af MM |
1011 | /* We have two typedef-names, both naming the same type to have |
1012 | the same name. This is OK because of: | |
1013 | ||
1014 | [dcl.typedef] | |
1015 | ||
1016 | In a given scope, a typedef specifier can be used to redefine | |
1017 | the name of any type declared in that scope to refer to the | |
1018 | type to which it already refers. */ | |
1019 | ok = 0; | |
7b176381 MM |
1020 | /* There can be two block-scope declarations of the same variable, |
1021 | so long as they are `extern' declarations. */ | |
1022 | else if (TREE_CODE (decl) == VAR_DECL | |
1023 | && TREE_CODE (BINDING_VALUE (binding)) == VAR_DECL | |
1024 | && DECL_EXTERNAL (decl) | |
1025 | && DECL_EXTERNAL (BINDING_VALUE (binding))) | |
1026 | { | |
1027 | duplicate_decls (decl, BINDING_VALUE (binding)); | |
1028 | ok = 0; | |
1029 | } | |
8f032717 MM |
1030 | else |
1031 | { | |
33bd39a2 | 1032 | error ("declaration of `%#D'", decl); |
8f032717 MM |
1033 | cp_error_at ("conflicts with previous declaration `%#D'", |
1034 | BINDING_VALUE (binding)); | |
1035 | ok = 0; | |
1036 | } | |
1037 | ||
1038 | return ok; | |
d8f8dca1 MM |
1039 | } |
1040 | ||
cfe507be MM |
1041 | /* Add DECL to the list of things declared in B. */ |
1042 | ||
1043 | static void | |
1044 | add_decl_to_level (decl, b) | |
1045 | tree decl; | |
1046 | struct binding_level *b; | |
1047 | { | |
cfe507be MM |
1048 | /* We build up the list in reverse order, and reverse it later if |
1049 | necessary. */ | |
1050 | TREE_CHAIN (decl) = b->names; | |
1051 | b->names = decl; | |
1052 | } | |
1053 | ||
1054 | /* Bind DECL to ID in the current_binding_level, assumed to be a local | |
1055 | binding level. If PUSH_USING is set in FLAGS, we know that DECL | |
1056 | doesn't really belong to this binding level, that it got here | |
1057 | through a using-declaration. */ | |
f181d4ae MM |
1058 | |
1059 | void | |
0034cf72 | 1060 | push_local_binding (id, decl, flags) |
f181d4ae MM |
1061 | tree id; |
1062 | tree decl; | |
0034cf72 | 1063 | int flags; |
f181d4ae | 1064 | { |
8f032717 MM |
1065 | struct binding_level *b; |
1066 | ||
1067 | /* Skip over any local classes. This makes sense if we call | |
1068 | push_local_binding with a friend decl of a local class. */ | |
1069 | b = current_binding_level; | |
1070 | while (b->parm_flag == 2) | |
1071 | b = b->level_chain; | |
f181d4ae | 1072 | |
d8f8dca1 | 1073 | if (lookup_name_current_level (id)) |
8f032717 MM |
1074 | { |
1075 | /* Supplement the existing binding. */ | |
1076 | if (!add_binding (id, decl)) | |
1077 | /* It didn't work. Something else must be bound at this | |
1078 | level. Do not add DECL to the list of things to pop | |
1079 | later. */ | |
1080 | return; | |
1081 | } | |
d8f8dca1 MM |
1082 | else |
1083 | /* Create a new binding. */ | |
8f032717 | 1084 | push_binding (id, decl, b); |
f181d4ae | 1085 | |
0034cf72 | 1086 | if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING)) |
a06d48ef JM |
1087 | /* We must put the OVERLOAD into a TREE_LIST since the |
1088 | TREE_CHAIN of an OVERLOAD is already used. Similarly for | |
1089 | decls that got here through a using-declaration. */ | |
1090 | decl = build_tree_list (NULL_TREE, decl); | |
1091 | ||
f181d4ae MM |
1092 | /* And put DECL on the list of things declared by the current |
1093 | binding level. */ | |
cfe507be | 1094 | add_decl_to_level (decl, b); |
f181d4ae MM |
1095 | } |
1096 | ||
8f032717 MM |
1097 | /* Bind DECL to ID in the class_binding_level. Returns nonzero if the |
1098 | binding was successful. */ | |
f181d4ae | 1099 | |
8f032717 | 1100 | int |
f181d4ae MM |
1101 | push_class_binding (id, decl) |
1102 | tree id; | |
1103 | tree decl; | |
1104 | { | |
8f032717 MM |
1105 | int result = 1; |
1106 | tree binding = IDENTIFIER_BINDING (id); | |
1107 | tree context; | |
1108 | ||
1109 | /* Note that we declared this value so that we can issue an error if | |
1110 | this an illegal redeclaration of a name already used for some | |
1111 | other purpose. */ | |
1112 | note_name_declared_in_class (id, decl); | |
1113 | ||
1114 | if (binding && BINDING_LEVEL (binding) == class_binding_level) | |
d8f8dca1 | 1115 | /* Supplement the existing binding. */ |
8f032717 | 1116 | result = add_binding (id, decl); |
d8f8dca1 MM |
1117 | else |
1118 | /* Create a new binding. */ | |
1119 | push_binding (id, decl, class_binding_level); | |
1120 | ||
1121 | /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the | |
1122 | class-level declaration. Note that we do not use DECL here | |
1123 | because of the possibility of the `struct stat' hack; if DECL is | |
1124 | a class-name or enum-name we might prefer a field-name, or some | |
1125 | such. */ | |
1126 | IDENTIFIER_CLASS_VALUE (id) = BINDING_VALUE (IDENTIFIER_BINDING (id)); | |
8f032717 MM |
1127 | |
1128 | /* If this is a binding from a base class, mark it as such. */ | |
1129 | binding = IDENTIFIER_BINDING (id); | |
1130 | if (BINDING_VALUE (binding) == decl && TREE_CODE (decl) != TREE_LIST) | |
1131 | { | |
280f9385 MM |
1132 | /* Any implicit typename must be from a base-class. The |
1133 | context for an implicit typename declaration is always | |
1134 | the derived class in which the lookup was done, so the checks | |
1135 | based on the context of DECL below will not trigger. */ | |
83233dca | 1136 | if (IMPLICIT_TYPENAME_TYPE_DECL_P (decl)) |
280f9385 | 1137 | INHERITED_VALUE_BINDING_P (binding) = 1; |
8f032717 MM |
1138 | else |
1139 | { | |
280f9385 | 1140 | if (TREE_CODE (decl) == OVERLOAD) |
4f1c5b7d | 1141 | context = CP_DECL_CONTEXT (OVL_CURRENT (decl)); |
280f9385 MM |
1142 | else |
1143 | { | |
2f939d94 | 1144 | my_friendly_assert (DECL_P (decl), 0); |
55de1b66 | 1145 | context = context_for_name_lookup (decl); |
280f9385 | 1146 | } |
8f032717 | 1147 | |
280f9385 MM |
1148 | if (is_properly_derived_from (current_class_type, context)) |
1149 | INHERITED_VALUE_BINDING_P (binding) = 1; | |
1150 | else | |
1151 | INHERITED_VALUE_BINDING_P (binding) = 0; | |
1152 | } | |
8f032717 MM |
1153 | } |
1154 | else if (BINDING_VALUE (binding) == decl) | |
1155 | /* We only encounter a TREE_LIST when push_class_decls detects an | |
1156 | ambiguity. Such an ambiguity can be overridden by a definition | |
1157 | in this class. */ | |
1158 | INHERITED_VALUE_BINDING_P (binding) = 1; | |
1159 | ||
1160 | return result; | |
f181d4ae MM |
1161 | } |
1162 | ||
d8f8dca1 MM |
1163 | /* Remove the binding for DECL which should be the innermost binding |
1164 | for ID. */ | |
f181d4ae | 1165 | |
68642fb6 UD |
1166 | static void |
1167 | pop_binding (id, decl) | |
f181d4ae | 1168 | tree id; |
d8f8dca1 | 1169 | tree decl; |
f181d4ae MM |
1170 | { |
1171 | tree binding; | |
68642fb6 | 1172 | |
f181d4ae MM |
1173 | if (id == NULL_TREE) |
1174 | /* It's easiest to write the loops that call this function without | |
1175 | checking whether or not the entities involved have names. We | |
1176 | get here for such an entity. */ | |
1177 | return; | |
1178 | ||
d8f8dca1 | 1179 | /* Get the innermost binding for ID. */ |
f181d4ae | 1180 | binding = IDENTIFIER_BINDING (id); |
f181d4ae | 1181 | |
d8f8dca1 MM |
1182 | /* The name should be bound. */ |
1183 | my_friendly_assert (binding != NULL_TREE, 0); | |
1184 | ||
1185 | /* The DECL will be either the ordinary binding or the type | |
1186 | binding for this identifier. Remove that binding. */ | |
1187 | if (BINDING_VALUE (binding) == decl) | |
1188 | BINDING_VALUE (binding) = NULL_TREE; | |
1189 | else if (BINDING_TYPE (binding) == decl) | |
1190 | BINDING_TYPE (binding) = NULL_TREE; | |
1191 | else | |
a98facb0 | 1192 | abort (); |
d8f8dca1 MM |
1193 | |
1194 | if (!BINDING_VALUE (binding) && !BINDING_TYPE (binding)) | |
4890c2f4 MM |
1195 | { |
1196 | /* We're completely done with the innermost binding for this | |
1197 | identifier. Unhook it from the list of bindings. */ | |
1198 | IDENTIFIER_BINDING (id) = TREE_CHAIN (binding); | |
1199 | ||
1200 | /* Add it to the free list. */ | |
1201 | TREE_CHAIN (binding) = free_bindings; | |
1202 | free_bindings = binding; | |
1203 | ||
1204 | /* Clear the BINDING_LEVEL so the garbage collector doesn't walk | |
1205 | it. */ | |
1206 | BINDING_LEVEL (binding) = NULL; | |
1207 | } | |
f181d4ae MM |
1208 | } |
1209 | ||
acef433b MM |
1210 | /* When a label goes out of scope, check to see if that label was used |
1211 | in a valid manner, and issue any appropriate warnings or errors. */ | |
1212 | ||
1213 | static void | |
6625cdb5 JM |
1214 | pop_label (label, old_value) |
1215 | tree label; | |
1216 | tree old_value; | |
acef433b | 1217 | { |
88848bde | 1218 | if (!processing_template_decl && doing_semantic_analysis_p ()) |
acef433b | 1219 | { |
88848bde MM |
1220 | if (DECL_INITIAL (label) == NULL_TREE) |
1221 | { | |
1222 | cp_error_at ("label `%D' used but not defined", label); | |
1223 | /* Avoid crashing later. */ | |
1224 | define_label (input_filename, 1, DECL_NAME (label)); | |
1225 | } | |
078721e1 | 1226 | else if (warn_unused_label && !TREE_USED (label)) |
88848bde | 1227 | cp_warning_at ("label `%D' defined but not used", label); |
acef433b | 1228 | } |
acef433b | 1229 | |
6625cdb5 | 1230 | SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value); |
acef433b MM |
1231 | } |
1232 | ||
0811ea8f | 1233 | /* At the end of a function, all labels declared within the function |
68642fb6 | 1234 | go out of scope. BLOCK is the top-level block for the |
acef433b MM |
1235 | function. */ |
1236 | ||
1237 | static void | |
1238 | pop_labels (block) | |
1239 | tree block; | |
1240 | { | |
6625cdb5 | 1241 | struct named_label_list *link; |
acef433b MM |
1242 | |
1243 | /* Clear out the definitions of all label names, since their scopes | |
1244 | end here. */ | |
6625cdb5 | 1245 | for (link = named_labels; link; link = link->next) |
acef433b | 1246 | { |
6625cdb5 | 1247 | pop_label (link->label_decl, link->old_value); |
acef433b MM |
1248 | /* Put the labels into the "variables" of the top-level block, |
1249 | so debugger can see them. */ | |
6625cdb5 JM |
1250 | TREE_CHAIN (link->label_decl) = BLOCK_VARS (block); |
1251 | BLOCK_VARS (block) = link->label_decl; | |
acef433b MM |
1252 | } |
1253 | ||
6625cdb5 | 1254 | named_labels = NULL; |
acef433b MM |
1255 | } |
1256 | ||
8d08fdba MS |
1257 | /* Exit a binding level. |
1258 | Pop the level off, and restore the state of the identifier-decl mappings | |
1259 | that were in effect when this level was entered. | |
1260 | ||
1261 | If KEEP == 1, this level had explicit declarations, so | |
1262 | and create a "block" (a BLOCK node) for the level | |
1263 | to record its declarations and subblocks for symbol table output. | |
1264 | ||
8d08fdba MS |
1265 | If FUNCTIONBODY is nonzero, this level is the body of a function, |
1266 | so create a block as if KEEP were set and also clear out all | |
1267 | label names. | |
1268 | ||
1269 | If REVERSE is nonzero, reverse the order of decls before putting | |
1270 | them into the BLOCK. */ | |
1271 | ||
1272 | tree | |
1273 | poplevel (keep, reverse, functionbody) | |
1274 | int keep; | |
1275 | int reverse; | |
1276 | int functionbody; | |
1277 | { | |
1278 | register tree link; | |
1279 | /* The chain of decls was accumulated in reverse order. | |
1280 | Put it into forward order, just for cleanliness. */ | |
1281 | tree decls; | |
1282 | int tmp = functionbody; | |
b35d4555 MM |
1283 | int real_functionbody; |
1284 | tree tags; | |
1285 | tree subblocks; | |
8d08fdba MS |
1286 | tree block = NULL_TREE; |
1287 | tree decl; | |
f181d4ae MM |
1288 | int leaving_for_scope; |
1289 | ||
01d939e8 | 1290 | if (cfun && !doing_semantic_analysis_p ()) |
b35d4555 MM |
1291 | return NULL_TREE; |
1292 | ||
273a708f MM |
1293 | my_friendly_assert (current_binding_level->parm_flag != 2, |
1294 | 19990916); | |
b74a0560 | 1295 | |
b35d4555 MM |
1296 | real_functionbody = (current_binding_level->keep == 2 |
1297 | ? ((functionbody = 0), tmp) : functionbody); | |
1298 | tags = functionbody >= 0 ? current_binding_level->tags : 0; | |
1299 | subblocks = functionbody >= 0 ? current_binding_level->blocks : 0; | |
1300 | ||
b74a0560 MM |
1301 | my_friendly_assert (!current_binding_level->class_shadowed, |
1302 | 19990414); | |
8d08fdba | 1303 | |
536333d4 MM |
1304 | /* We used to use KEEP == 2 to indicate that the new block should go |
1305 | at the beginning of the list of blocks at this binding level, | |
1306 | rather than the end. This hack is no longer used. */ | |
1307 | my_friendly_assert (keep == 0 || keep == 1, 0); | |
1308 | ||
8d08fdba MS |
1309 | if (current_binding_level->keep == 1) |
1310 | keep = 1; | |
1311 | ||
6625cdb5 JM |
1312 | /* Any uses of undefined labels, and any defined labels, now operate |
1313 | under constraints of next binding contour. */ | |
1314 | if (cfun && !functionbody) | |
1315 | { | |
1316 | struct binding_level *level_chain; | |
1317 | level_chain = current_binding_level->level_chain; | |
1318 | if (level_chain) | |
1319 | { | |
1320 | struct named_label_use_list *uses; | |
1321 | struct named_label_list *labels; | |
1322 | for (labels = named_labels; labels; labels = labels->next) | |
1323 | if (labels->binding_level == current_binding_level) | |
1324 | { | |
1325 | tree decl; | |
826840d9 RH |
1326 | if (current_binding_level->is_try_scope) |
1327 | labels->in_try_scope = 1; | |
1328 | if (current_binding_level->is_catch_scope) | |
1329 | labels->in_catch_scope = 1; | |
6625cdb5 JM |
1330 | for (decl = labels->names_in_scope; decl; |
1331 | decl = TREE_CHAIN (decl)) | |
1332 | if (decl_jump_unsafe (decl)) | |
1333 | labels->bad_decls = tree_cons (NULL_TREE, decl, | |
1334 | labels->bad_decls); | |
1335 | labels->binding_level = level_chain; | |
1336 | labels->names_in_scope = level_chain->names; | |
1337 | } | |
1338 | ||
1339 | for (uses = named_label_uses; uses; uses = uses->next) | |
1340 | if (uses->binding_level == current_binding_level) | |
1341 | { | |
1342 | uses->binding_level = level_chain; | |
1343 | uses->names_in_scope = level_chain->names; | |
1344 | } | |
1345 | } | |
1346 | } | |
1347 | ||
8d08fdba MS |
1348 | /* Get the decls in the order they were written. |
1349 | Usually current_binding_level->names is in reverse order. | |
1350 | But parameter decls were previously put in forward order. */ | |
1351 | ||
1352 | if (reverse) | |
1353 | current_binding_level->names | |
1354 | = decls = nreverse (current_binding_level->names); | |
1355 | else | |
1356 | decls = current_binding_level->names; | |
1357 | ||
1358 | /* Output any nested inline functions within this block | |
1359 | if they weren't already output. */ | |
8d08fdba MS |
1360 | for (decl = decls; decl; decl = TREE_CHAIN (decl)) |
1361 | if (TREE_CODE (decl) == FUNCTION_DECL | |
1362 | && ! TREE_ASM_WRITTEN (decl) | |
1363 | && DECL_INITIAL (decl) != NULL_TREE | |
6060a796 MS |
1364 | && TREE_ADDRESSABLE (decl) |
1365 | && decl_function_context (decl) == current_function_decl) | |
8d08fdba MS |
1366 | { |
1367 | /* If this decl was copied from a file-scope decl | |
1368 | on account of a block-scope extern decl, | |
1369 | propagate TREE_ADDRESSABLE to the file-scope decl. */ | |
1370 | if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE) | |
1371 | TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1; | |
1372 | else | |
1373 | { | |
1374 | push_function_context (); | |
1375 | output_inline_function (decl); | |
1376 | pop_function_context (); | |
1377 | } | |
1378 | } | |
1379 | ||
d9b2d9da MM |
1380 | /* When not in function-at-a-time mode, expand_end_bindings will |
1381 | warn about unused variables. But, in function-at-a-time mode | |
1382 | expand_end_bindings is not passed the list of variables in the | |
1383 | current scope, and therefore no warning is emitted. So, we | |
1384 | explicitly warn here. */ | |
1385 | if (!processing_template_decl) | |
1386 | warn_about_unused_variables (getdecls ()); | |
1387 | ||
8d08fdba MS |
1388 | /* If there were any declarations or structure tags in that level, |
1389 | or if this level is a function body, | |
1390 | create a BLOCK to record them for the life of this function. */ | |
8d08fdba | 1391 | block = NULL_TREE; |
f444e36b | 1392 | if (keep == 1 || functionbody) |
8d08fdba MS |
1393 | block = make_node (BLOCK); |
1394 | if (block != NULL_TREE) | |
1395 | { | |
f444e36b MM |
1396 | BLOCK_VARS (block) = decls; |
1397 | BLOCK_SUBBLOCKS (block) = subblocks; | |
8d08fdba MS |
1398 | } |
1399 | ||
1400 | /* In each subblock, record that this is its superior. */ | |
8d08fdba MS |
1401 | if (keep >= 0) |
1402 | for (link = subblocks; link; link = TREE_CHAIN (link)) | |
1403 | BLOCK_SUPERCONTEXT (link) = block; | |
1404 | ||
f181d4ae MM |
1405 | /* We still support the old for-scope rules, whereby the variables |
1406 | in a for-init statement were in scope after the for-statement | |
1407 | ended. We only use the new rules in flag_new_for_scope is | |
1408 | nonzero. */ | |
68642fb6 | 1409 | leaving_for_scope |
f181d4ae MM |
1410 | = current_binding_level->is_for_scope && flag_new_for_scope == 1; |
1411 | ||
1412 | /* Remove declarations for all the DECLs in this level. */ | |
1413 | for (link = decls; link; link = TREE_CHAIN (link)) | |
1414 | { | |
c3783399 NS |
1415 | if (leaving_for_scope && TREE_CODE (link) == VAR_DECL |
1416 | && DECL_NAME (link)) | |
f181d4ae | 1417 | { |
68642fb6 | 1418 | tree outer_binding |
f181d4ae MM |
1419 | = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (link))); |
1420 | tree ns_binding; | |
1421 | ||
1422 | if (!outer_binding) | |
1423 | ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link)); | |
c7dda1e3 MM |
1424 | else |
1425 | ns_binding = NULL_TREE; | |
1426 | ||
68642fb6 UD |
1427 | if (outer_binding |
1428 | && (BINDING_LEVEL (outer_binding) | |
f181d4ae MM |
1429 | == current_binding_level->level_chain)) |
1430 | /* We have something like: | |
68642fb6 | 1431 | |
f181d4ae MM |
1432 | int i; |
1433 | for (int i; ;); | |
68642fb6 | 1434 | |
f181d4ae MM |
1435 | and we are leaving the `for' scope. There's no reason to |
1436 | keep the binding of the inner `i' in this case. */ | |
d8f8dca1 | 1437 | pop_binding (DECL_NAME (link), link); |
68642fb6 UD |
1438 | else if ((outer_binding |
1439 | && (TREE_CODE (BINDING_VALUE (outer_binding)) | |
f181d4ae | 1440 | == TYPE_DECL)) |
68642fb6 | 1441 | || (ns_binding |
f181d4ae MM |
1442 | && TREE_CODE (ns_binding) == TYPE_DECL)) |
1443 | /* Here, we have something like: | |
1444 | ||
1445 | typedef int I; | |
1446 | ||
1447 | void f () { | |
1448 | for (int I; ;); | |
1449 | } | |
1450 | ||
1451 | We must pop the for-scope binding so we know what's a | |
1452 | type and what isn't. */ | |
d8f8dca1 | 1453 | pop_binding (DECL_NAME (link), link); |
e76a2646 | 1454 | else |
e76a2646 | 1455 | { |
f181d4ae MM |
1456 | /* Mark this VAR_DECL as dead so that we can tell we left it |
1457 | there only for backward compatibility. */ | |
1458 | DECL_DEAD_FOR_LOCAL (link) = 1; | |
68642fb6 | 1459 | |
f181d4ae MM |
1460 | /* Keep track of what should of have happenned when we |
1461 | popped the binding. */ | |
1462 | if (outer_binding && BINDING_VALUE (outer_binding)) | |
68642fb6 | 1463 | DECL_SHADOWED_FOR_VAR (link) |
f181d4ae MM |
1464 | = BINDING_VALUE (outer_binding); |
1465 | ||
1466 | /* Add it to the list of dead variables in the next | |
1467 | outermost binding to that we can remove these when we | |
1468 | leave that binding. */ | |
1469 | current_binding_level->level_chain->dead_vars_from_for | |
1470 | = tree_cons (NULL_TREE, link, | |
1471 | current_binding_level->level_chain-> | |
1472 | dead_vars_from_for); | |
1473 | ||
1474 | /* Although we don't pop the CPLUS_BINDING, we do clear | |
1475 | its BINDING_LEVEL since the level is going away now. */ | |
1476 | BINDING_LEVEL (IDENTIFIER_BINDING (DECL_NAME (link))) | |
1477 | = 0; | |
e76a2646 | 1478 | } |
2ee887f2 | 1479 | } |
68642fb6 | 1480 | else |
8d6e462b | 1481 | { |
f181d4ae | 1482 | /* Remove the binding. */ |
0034cf72 JM |
1483 | decl = link; |
1484 | if (TREE_CODE (decl) == TREE_LIST) | |
1485 | decl = TREE_VALUE (decl); | |
2f939d94 | 1486 | if (DECL_P (decl)) |
0034cf72 JM |
1487 | pop_binding (DECL_NAME (decl), decl); |
1488 | else if (TREE_CODE (decl) == OVERLOAD) | |
1489 | pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl); | |
68642fb6 | 1490 | else |
a98facb0 | 1491 | abort (); |
8d08fdba | 1492 | } |
f181d4ae | 1493 | } |
8d08fdba | 1494 | |
f181d4ae MM |
1495 | /* Remove declarations for any `for' variables from inner scopes |
1496 | that we kept around. */ | |
1497 | for (link = current_binding_level->dead_vars_from_for; | |
1498 | link; link = TREE_CHAIN (link)) | |
d8f8dca1 | 1499 | pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link)); |
2ee887f2 | 1500 | |
f181d4ae MM |
1501 | /* Restore the IDENTIFIER_TYPE_VALUEs. */ |
1502 | for (link = current_binding_level->type_shadowed; | |
1503 | link; link = TREE_CHAIN (link)) | |
1504 | SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link)); | |
acef433b MM |
1505 | |
1506 | /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */ | |
1507 | for (link = current_binding_level->shadowed_labels; | |
68642fb6 | 1508 | link; |
acef433b | 1509 | link = TREE_CHAIN (link)) |
6625cdb5 | 1510 | pop_label (TREE_VALUE (link), TREE_PURPOSE (link)); |
acef433b | 1511 | |
f181d4ae MM |
1512 | /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs |
1513 | list if a `using' declaration put them there. The debugging | |
1514 | back-ends won't understand OVERLOAD, so we remove them here. | |
1515 | Because the BLOCK_VARS are (temporarily) shared with | |
1516 | CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have | |
1517 | popped all the bindings. */ | |
1518 | if (block) | |
1519 | { | |
1520 | tree* d; | |
8d6e462b | 1521 | |
a06d48ef JM |
1522 | for (d = &BLOCK_VARS (block); *d; ) |
1523 | { | |
1524 | if (TREE_CODE (*d) == TREE_LIST) | |
1525 | *d = TREE_CHAIN (*d); | |
1526 | else | |
1527 | d = &TREE_CHAIN (*d); | |
1528 | } | |
8d6e462b | 1529 | } |
8d08fdba MS |
1530 | |
1531 | /* If the level being exited is the top level of a function, | |
1532 | check over all the labels. */ | |
8d08fdba MS |
1533 | if (functionbody) |
1534 | { | |
acef433b MM |
1535 | /* Since this is the top level block of a function, the vars are |
1536 | the function's parameters. Don't leave them in the BLOCK | |
1537 | because they are found in the FUNCTION_DECL instead. */ | |
8d08fdba | 1538 | BLOCK_VARS (block) = 0; |
acef433b | 1539 | pop_labels (block); |
8d08fdba MS |
1540 | } |
1541 | ||
8d08fdba MS |
1542 | tmp = current_binding_level->keep; |
1543 | ||
1544 | pop_binding_level (); | |
1545 | if (functionbody) | |
1546 | DECL_INITIAL (current_function_decl) = block; | |
1547 | else if (block) | |
f444e36b MM |
1548 | current_binding_level->blocks |
1549 | = chainon (current_binding_level->blocks, block); | |
1550 | ||
8d08fdba MS |
1551 | /* If we did not make a block for the level just exited, |
1552 | any blocks made for inner levels | |
1553 | (since they cannot be recorded as subblocks in that level) | |
1554 | must be carried forward so they will later become subblocks | |
1555 | of something else. */ | |
1556 | else if (subblocks) | |
536333d4 MM |
1557 | current_binding_level->blocks |
1558 | = chainon (current_binding_level->blocks, subblocks); | |
8d08fdba | 1559 | |
d9b2d9da MM |
1560 | /* Each and every BLOCK node created here in `poplevel' is important |
1561 | (e.g. for proper debugging information) so if we created one | |
1562 | earlier, mark it as "used". */ | |
1563 | if (block) | |
1564 | TREE_USED (block) = 1; | |
1565 | ||
8d08fdba | 1566 | /* Take care of compiler's internal binding structures. */ |
a4443a08 | 1567 | if (tmp == 2) |
8d08fdba | 1568 | { |
d9b2d9da MM |
1569 | tree scope_stmts; |
1570 | ||
68642fb6 | 1571 | scope_stmts |
d9b2d9da | 1572 | = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/1); |
8d08fdba | 1573 | if (block) |
d9b2d9da MM |
1574 | { |
1575 | SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block; | |
1576 | SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block; | |
1577 | } | |
1578 | ||
8f471b0d | 1579 | block = poplevel (keep, reverse, functionbody); |
8d08fdba MS |
1580 | } |
1581 | ||
8d08fdba MS |
1582 | return block; |
1583 | } | |
1584 | ||
1585 | /* Delete the node BLOCK from the current binding level. | |
1586 | This is used for the block inside a stmt expr ({...}) | |
1587 | so that the block can be reinserted where appropriate. */ | |
1588 | ||
1589 | void | |
1590 | delete_block (block) | |
1591 | tree block; | |
1592 | { | |
1593 | tree t; | |
1594 | if (current_binding_level->blocks == block) | |
1595 | current_binding_level->blocks = TREE_CHAIN (block); | |
1596 | for (t = current_binding_level->blocks; t;) | |
1597 | { | |
1598 | if (TREE_CHAIN (t) == block) | |
1599 | TREE_CHAIN (t) = TREE_CHAIN (block); | |
1600 | else | |
1601 | t = TREE_CHAIN (t); | |
1602 | } | |
1603 | TREE_CHAIN (block) = NULL_TREE; | |
1604 | /* Clear TREE_USED which is always set by poplevel. | |
1605 | The flag is set again if insert_block is called. */ | |
1606 | TREE_USED (block) = 0; | |
1607 | } | |
1608 | ||
1609 | /* Insert BLOCK at the end of the list of subblocks of the | |
1610 | current binding level. This is used when a BIND_EXPR is expanded, | |
1611 | to handle the BLOCK node inside the BIND_EXPR. */ | |
1612 | ||
1613 | void | |
1614 | insert_block (block) | |
1615 | tree block; | |
1616 | { | |
1617 | TREE_USED (block) = 1; | |
1618 | current_binding_level->blocks | |
1619 | = chainon (current_binding_level->blocks, block); | |
1620 | } | |
1621 | ||
8d08fdba MS |
1622 | /* Set the BLOCK node for the innermost scope |
1623 | (the one we are currently in). */ | |
1624 | ||
1625 | void | |
1626 | set_block (block) | |
f444e36b | 1627 | tree block ATTRIBUTE_UNUSED; |
8d08fdba | 1628 | { |
f444e36b MM |
1629 | /* The RTL expansion machinery requires us to provide this callback, |
1630 | but it is not applicable in function-at-a-time mode. */ | |
1631 | my_friendly_assert (cfun && !doing_semantic_analysis_p (), 20000911); | |
8d08fdba MS |
1632 | } |
1633 | ||
1634 | /* Do a pushlevel for class declarations. */ | |
e92cc029 | 1635 | |
8d08fdba MS |
1636 | void |
1637 | pushlevel_class () | |
1638 | { | |
1639 | register struct binding_level *newlevel; | |
1640 | ||
1641 | /* Reuse or create a struct for this binding level. */ | |
1642 | #if defined(DEBUG_CP_BINDING_LEVELS) | |
1643 | if (0) | |
1644 | #else /* !defined(DEBUG_CP_BINDING_LEVELS) */ | |
1645 | if (free_binding_level) | |
1646 | #endif /* !defined(DEBUG_CP_BINDING_LEVELS) */ | |
1647 | { | |
1648 | newlevel = free_binding_level; | |
1649 | free_binding_level = free_binding_level->level_chain; | |
1650 | } | |
1651 | else | |
f181d4ae | 1652 | newlevel = make_binding_level (); |
8d08fdba MS |
1653 | |
1654 | #if defined(DEBUG_CP_BINDING_LEVELS) | |
1655 | is_class_level = 1; | |
1656 | #endif /* defined(DEBUG_CP_BINDING_LEVELS) */ | |
1657 | ||
1658 | push_binding_level (newlevel, 0, 0); | |
1659 | ||
8d08fdba MS |
1660 | class_binding_level = current_binding_level; |
1661 | class_binding_level->parm_flag = 2; | |
70adf8a9 | 1662 | class_binding_level->this_class = current_class_type; |
8d08fdba MS |
1663 | } |
1664 | ||
b74a0560 | 1665 | /* ...and a poplevel for class declarations. */ |
e92cc029 | 1666 | |
273a708f | 1667 | void |
b74a0560 | 1668 | poplevel_class () |
8d08fdba MS |
1669 | { |
1670 | register struct binding_level *level = class_binding_level; | |
8d08fdba MS |
1671 | tree shadowed; |
1672 | ||
1673 | my_friendly_assert (level != 0, 354); | |
68642fb6 | 1674 | |
8d08fdba | 1675 | /* If we're leaving a toplevel class, don't bother to do the setting |
ddd5a7c1 | 1676 | of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot |
8d08fdba | 1677 | shouldn't even be used when current_class_type isn't set, and second, |
ddd5a7c1 | 1678 | if we don't touch it here, we're able to use the cache effect if the |
8d08fdba | 1679 | next time we're entering a class scope, it is the same class. */ |
b74a0560 | 1680 | if (current_class_depth != 1) |
8f032717 MM |
1681 | { |
1682 | struct binding_level* b; | |
1683 | ||
1684 | /* Clear out our IDENTIFIER_CLASS_VALUEs. */ | |
1685 | for (shadowed = level->class_shadowed; | |
1686 | shadowed; | |
1687 | shadowed = TREE_CHAIN (shadowed)) | |
1688 | IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE; | |
68642fb6 | 1689 | |
8f032717 MM |
1690 | /* Find the next enclosing class, and recreate |
1691 | IDENTIFIER_CLASS_VALUEs appropriate for that class. */ | |
1692 | b = level->level_chain; | |
1693 | while (b && b->parm_flag != 2) | |
1694 | b = b->level_chain; | |
1695 | ||
1696 | if (b) | |
68642fb6 UD |
1697 | for (shadowed = b->class_shadowed; |
1698 | shadowed; | |
8f032717 MM |
1699 | shadowed = TREE_CHAIN (shadowed)) |
1700 | { | |
1701 | tree t; | |
1702 | ||
1703 | t = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed)); | |
1704 | while (t && BINDING_LEVEL (t) != b) | |
1705 | t = TREE_CHAIN (t); | |
68642fb6 | 1706 | |
8f032717 | 1707 | if (t) |
68642fb6 | 1708 | IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) |
8f032717 MM |
1709 | = BINDING_VALUE (t); |
1710 | } | |
1711 | } | |
8d08fdba MS |
1712 | else |
1713 | /* Remember to save what IDENTIFIER's were bound in this scope so we | |
1714 | can recover from cache misses. */ | |
e76a2646 MS |
1715 | { |
1716 | previous_class_type = current_class_type; | |
1717 | previous_class_values = class_binding_level->class_shadowed; | |
1718 | } | |
8d08fdba MS |
1719 | for (shadowed = level->type_shadowed; |
1720 | shadowed; | |
1721 | shadowed = TREE_CHAIN (shadowed)) | |
2c73f9f5 | 1722 | SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed)); |
8d08fdba | 1723 | |
f181d4ae | 1724 | /* Remove the bindings for all of the class-level declarations. */ |
68642fb6 UD |
1725 | for (shadowed = level->class_shadowed; |
1726 | shadowed; | |
f181d4ae | 1727 | shadowed = TREE_CHAIN (shadowed)) |
d8f8dca1 | 1728 | pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed)); |
f181d4ae | 1729 | |
38e01259 | 1730 | /* Now, pop out of the binding level which we created up in the |
8d08fdba MS |
1731 | `pushlevel_class' routine. */ |
1732 | #if defined(DEBUG_CP_BINDING_LEVELS) | |
1733 | is_class_level = 1; | |
1734 | #endif /* defined(DEBUG_CP_BINDING_LEVELS) */ | |
1735 | ||
1736 | pop_binding_level (); | |
8d08fdba | 1737 | } |
8f032717 MM |
1738 | |
1739 | /* We are entering the scope of a class. Clear IDENTIFIER_CLASS_VALUE | |
1740 | for any names in enclosing classes. */ | |
1741 | ||
1742 | void | |
1743 | clear_identifier_class_values () | |
1744 | { | |
1745 | tree t; | |
1746 | ||
1747 | if (!class_binding_level) | |
1748 | return; | |
1749 | ||
1750 | for (t = class_binding_level->class_shadowed; | |
1751 | t; | |
1752 | t = TREE_CHAIN (t)) | |
1753 | IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE; | |
1754 | } | |
1755 | ||
0aafb128 MM |
1756 | /* Returns non-zero if T is a virtual function table. */ |
1757 | ||
1758 | int | |
1759 | vtable_decl_p (t, data) | |
1760 | tree t; | |
1761 | void *data ATTRIBUTE_UNUSED; | |
1762 | { | |
1763 | return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t)); | |
1764 | } | |
1765 | ||
1766 | /* Returns non-zero if T is a TYPE_DECL for a type with virtual | |
1767 | functions. */ | |
1768 | ||
1769 | int | |
1770 | vtype_decl_p (t, data) | |
1771 | tree t; | |
1772 | void *data ATTRIBUTE_UNUSED; | |
1773 | { | |
1774 | return (TREE_CODE (t) == TYPE_DECL | |
34249844 NS |
1775 | && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE |
1776 | && TYPE_POLYMORPHIC_P (TREE_TYPE (t))); | |
0aafb128 MM |
1777 | } |
1778 | ||
f71f87f9 MM |
1779 | /* Return the declarations that are members of the namespace NS. */ |
1780 | ||
1781 | tree | |
1782 | cp_namespace_decls (ns) | |
1783 | tree ns; | |
1784 | { | |
1785 | return NAMESPACE_LEVEL (ns)->names; | |
1786 | } | |
1787 | ||
0aafb128 MM |
1788 | /* Walk all the namespaces contained NAMESPACE, including NAMESPACE |
1789 | itself, calling F for each. The DATA is passed to F as well. */ | |
1790 | ||
1791 | static int | |
1792 | walk_namespaces_r (namespace, f, data) | |
1793 | tree namespace; | |
1794 | walk_namespaces_fn f; | |
1795 | void *data; | |
1796 | { | |
1797 | tree current; | |
1798 | int result = 0; | |
1799 | ||
1800 | result |= (*f) (namespace, data); | |
1801 | ||
f71f87f9 | 1802 | for (current = cp_namespace_decls (namespace); |
0aafb128 MM |
1803 | current; |
1804 | current = TREE_CHAIN (current)) | |
1805 | { | |
1806 | if (TREE_CODE (current) != NAMESPACE_DECL | |
1807 | || DECL_NAMESPACE_ALIAS (current)) | |
1808 | continue; | |
0aafb128 MM |
1809 | |
1810 | /* We found a namespace. */ | |
1811 | result |= walk_namespaces_r (current, f, data); | |
1812 | } | |
1813 | ||
1814 | return result; | |
1815 | } | |
1816 | ||
1817 | /* Walk all the namespaces, calling F for each. The DATA is passed to | |
1818 | F as well. */ | |
1819 | ||
1820 | int | |
1821 | walk_namespaces (f, data) | |
1822 | walk_namespaces_fn f; | |
1823 | void *data; | |
1824 | { | |
1825 | return walk_namespaces_r (global_namespace, f, data); | |
1826 | } | |
1827 | ||
1828 | struct walk_globals_data { | |
1829 | walk_globals_pred p; | |
1830 | walk_globals_fn f; | |
1831 | void *data; | |
1832 | }; | |
1833 | ||
1834 | /* Walk the global declarations in NAMESPACE. Whenever one is found | |
1835 | for which P returns non-zero, call F with its address. If any call | |
1836 | to F returns a non-zero value, return a non-zero value. */ | |
1837 | ||
68642fb6 | 1838 | static int |
0aafb128 MM |
1839 | walk_globals_r (namespace, data) |
1840 | tree namespace; | |
1841 | void *data; | |
1842 | { | |
1843 | struct walk_globals_data* wgd = (struct walk_globals_data *) data; | |
1844 | walk_globals_pred p = wgd->p; | |
1845 | walk_globals_fn f = wgd->f; | |
1846 | void *d = wgd->data; | |
1847 | tree *t; | |
1848 | int result = 0; | |
1849 | ||
1850 | t = &NAMESPACE_LEVEL (namespace)->names; | |
1851 | ||
1852 | while (*t) | |
1853 | { | |
1854 | tree glbl = *t; | |
1855 | ||
1856 | if ((*p) (glbl, d)) | |
1857 | result |= (*f) (t, d); | |
1858 | ||
1859 | /* If F changed *T, then *T still points at the next item to | |
1860 | examine. */ | |
1861 | if (*t == glbl) | |
1862 | t = &TREE_CHAIN (*t); | |
1863 | } | |
1864 | ||
1865 | return result; | |
1866 | } | |
1867 | ||
1868 | /* Walk the global declarations. Whenever one is found for which P | |
1869 | returns non-zero, call F with its address. If any call to F | |
1870 | returns a non-zero value, return a non-zero value. */ | |
1871 | ||
1872 | int | |
1873 | walk_globals (p, f, data) | |
1874 | walk_globals_pred p; | |
1875 | walk_globals_fn f; | |
1876 | void *data; | |
1877 | { | |
1878 | struct walk_globals_data wgd; | |
1879 | wgd.p = p; | |
1880 | wgd.f = f; | |
1881 | wgd.data = data; | |
1882 | ||
1883 | return walk_namespaces (walk_globals_r, &wgd); | |
1884 | } | |
1885 | ||
1886 | /* Call wrapup_globals_declarations for the globals in NAMESPACE. If | |
1887 | DATA is non-NULL, this is the last time we will call | |
1888 | wrapup_global_declarations for this NAMESPACE. */ | |
1889 | ||
1890 | int | |
1891 | wrapup_globals_for_namespace (namespace, data) | |
1892 | tree namespace; | |
1893 | void *data; | |
1894 | { | |
f71f87f9 | 1895 | tree globals = cp_namespace_decls (namespace); |
0aafb128 MM |
1896 | int len = list_length (globals); |
1897 | tree *vec = (tree *) alloca (sizeof (tree) * len); | |
1898 | int i; | |
0352cfc8 | 1899 | int result; |
0aafb128 MM |
1900 | tree decl; |
1901 | int last_time = (data != 0); | |
1902 | ||
1903 | if (last_time && namespace == global_namespace) | |
1904 | /* Let compile_file handle the global namespace. */ | |
1905 | return 0; | |
1906 | ||
1907 | /* Process the decls in reverse order--earliest first. | |
1908 | Put them into VEC from back to front, then take out from front. */ | |
0aafb128 | 1909 | for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl)) |
62136084 | 1910 | vec[len - i - 1] = decl; |
68642fb6 | 1911 | |
0352cfc8 MM |
1912 | if (last_time) |
1913 | { | |
1914 | check_global_declarations (vec, len); | |
1915 | return 0; | |
1916 | } | |
1917 | ||
1918 | /* Temporarily mark vtables as external. That prevents | |
1919 | wrapup_global_declarations from writing them out; we must process | |
1920 | them ourselves in finish_vtable_vardecl. */ | |
1921 | for (i = 0; i < len; ++i) | |
91f2aa22 | 1922 | if (vtable_decl_p (vec[i], /*data=*/0) && !DECL_EXTERNAL (vec[i])) |
0352cfc8 MM |
1923 | { |
1924 | DECL_NOT_REALLY_EXTERN (vec[i]) = 1; | |
1925 | DECL_EXTERNAL (vec[i]) = 1; | |
1926 | } | |
0aafb128 | 1927 | |
0352cfc8 MM |
1928 | /* Write out any globals that need to be output. */ |
1929 | result = wrapup_global_declarations (vec, len); | |
1930 | ||
1931 | /* Undo the hack to DECL_EXTERNAL above. */ | |
1932 | for (i = 0; i < len; ++i) | |
1933 | if (vtable_decl_p (vec[i], /*data=*/0) | |
1934 | && DECL_NOT_REALLY_EXTERN (vec[i])) | |
1935 | { | |
1936 | DECL_NOT_REALLY_EXTERN (vec[i]) = 0; | |
1937 | DECL_EXTERNAL (vec[i]) = 0; | |
1938 | } | |
1939 | ||
1940 | return result; | |
0aafb128 MM |
1941 | } |
1942 | ||
4519c0a8 | 1943 | \f |
fc6af6e3 RH |
1944 | /* Mark ARG (which is really a struct binding_level **) for GC. */ |
1945 | ||
4519c0a8 MM |
1946 | static void |
1947 | mark_binding_level (arg) | |
1948 | void *arg; | |
1949 | { | |
1950 | struct binding_level *lvl = *(struct binding_level **)arg; | |
1951 | ||
6625cdb5 | 1952 | for (; lvl; lvl = lvl->level_chain) |
4519c0a8 MM |
1953 | { |
1954 | ggc_mark_tree (lvl->names); | |
1955 | ggc_mark_tree (lvl->tags); | |
1956 | ggc_mark_tree (lvl->usings); | |
1957 | ggc_mark_tree (lvl->using_directives); | |
1958 | ggc_mark_tree (lvl->class_shadowed); | |
1959 | ggc_mark_tree (lvl->type_shadowed); | |
1960 | ggc_mark_tree (lvl->shadowed_labels); | |
1961 | ggc_mark_tree (lvl->blocks); | |
70adf8a9 | 1962 | ggc_mark_tree (lvl->this_class); |
4519c0a8 | 1963 | ggc_mark_tree (lvl->dead_vars_from_for); |
6625cdb5 JM |
1964 | } |
1965 | } | |
1966 | ||
1967 | static void | |
1968 | mark_named_label_lists (labs, uses) | |
1969 | void *labs; | |
1970 | void *uses; | |
1971 | { | |
1972 | struct named_label_list *l = *(struct named_label_list **)labs; | |
1973 | struct named_label_use_list *u = *(struct named_label_use_list **)uses; | |
4519c0a8 | 1974 | |
6625cdb5 JM |
1975 | for (; l; l = l->next) |
1976 | { | |
1977 | ggc_mark (l); | |
1978 | mark_binding_level (l->binding_level); | |
1979 | ggc_mark_tree (l->old_value); | |
1980 | ggc_mark_tree (l->label_decl); | |
1981 | ggc_mark_tree (l->bad_decls); | |
4519c0a8 | 1982 | } |
6625cdb5 JM |
1983 | |
1984 | for (; u; u = u->next) | |
1985 | ggc_mark (u); | |
4519c0a8 | 1986 | } |
8d08fdba MS |
1987 | \f |
1988 | /* For debugging. */ | |
5566b478 MS |
1989 | static int no_print_functions = 0; |
1990 | static int no_print_builtins = 0; | |
8d08fdba MS |
1991 | |
1992 | void | |
1993 | print_binding_level (lvl) | |
1994 | struct binding_level *lvl; | |
1995 | { | |
1996 | tree t; | |
1997 | int i = 0, len; | |
1998 | fprintf (stderr, " blocks="); | |
1999 | fprintf (stderr, HOST_PTR_PRINTF, lvl->blocks); | |
8d08fdba MS |
2000 | if (lvl->tag_transparent) |
2001 | fprintf (stderr, " tag-transparent"); | |
2002 | if (lvl->more_cleanups_ok) | |
2003 | fprintf (stderr, " more-cleanups-ok"); | |
2004 | if (lvl->have_cleanups) | |
2005 | fprintf (stderr, " have-cleanups"); | |
8d08fdba MS |
2006 | fprintf (stderr, "\n"); |
2007 | if (lvl->names) | |
2008 | { | |
2009 | fprintf (stderr, " names:\t"); | |
2010 | /* We can probably fit 3 names to a line? */ | |
2011 | for (t = lvl->names; t; t = TREE_CHAIN (t)) | |
2012 | { | |
68642fb6 | 2013 | if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL)) |
8d08fdba MS |
2014 | continue; |
2015 | if (no_print_builtins | |
fc378698 MS |
2016 | && (TREE_CODE (t) == TYPE_DECL) |
2017 | && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>"))) | |
8d08fdba MS |
2018 | continue; |
2019 | ||
2020 | /* Function decls tend to have longer names. */ | |
2021 | if (TREE_CODE (t) == FUNCTION_DECL) | |
2022 | len = 3; | |
2023 | else | |
2024 | len = 2; | |
2025 | i += len; | |
2026 | if (i > 6) | |
2027 | { | |
2028 | fprintf (stderr, "\n\t"); | |
2029 | i = len; | |
2030 | } | |
2031 | print_node_brief (stderr, "", t, 0); | |
bd6dd845 | 2032 | if (t == error_mark_node) |
8d08fdba MS |
2033 | break; |
2034 | } | |
2035 | if (i) | |
2036 | fprintf (stderr, "\n"); | |
2037 | } | |
2038 | if (lvl->tags) | |
2039 | { | |
2040 | fprintf (stderr, " tags:\t"); | |
2041 | i = 0; | |
2042 | for (t = lvl->tags; t; t = TREE_CHAIN (t)) | |
2043 | { | |
2044 | if (TREE_PURPOSE (t) == NULL_TREE) | |
2045 | len = 3; | |
2046 | else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t))) | |
2047 | len = 2; | |
2048 | else | |
2049 | len = 4; | |
2050 | i += len; | |
2051 | if (i > 5) | |
2052 | { | |
2053 | fprintf (stderr, "\n\t"); | |
2054 | i = len; | |
2055 | } | |
2056 | if (TREE_PURPOSE (t) == NULL_TREE) | |
2057 | { | |
2058 | print_node_brief (stderr, "<unnamed-typedef", TREE_VALUE (t), 0); | |
2059 | fprintf (stderr, ">"); | |
2060 | } | |
2061 | else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t))) | |
2062 | print_node_brief (stderr, "", TREE_VALUE (t), 0); | |
2063 | else | |
2064 | { | |
2065 | print_node_brief (stderr, "<typedef", TREE_PURPOSE (t), 0); | |
2066 | print_node_brief (stderr, "", TREE_VALUE (t), 0); | |
2067 | fprintf (stderr, ">"); | |
2068 | } | |
2069 | } | |
2070 | if (i) | |
2071 | fprintf (stderr, "\n"); | |
2072 | } | |
8d08fdba MS |
2073 | if (lvl->class_shadowed) |
2074 | { | |
2075 | fprintf (stderr, " class-shadowed:"); | |
2076 | for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t)) | |
2077 | { | |
2078 | fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t))); | |
2079 | } | |
2080 | fprintf (stderr, "\n"); | |
2081 | } | |
2082 | if (lvl->type_shadowed) | |
2083 | { | |
2084 | fprintf (stderr, " type-shadowed:"); | |
2085 | for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t)) | |
2086 | { | |
8d08fdba | 2087 | fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t))); |
8d08fdba MS |
2088 | } |
2089 | fprintf (stderr, "\n"); | |
2090 | } | |
2091 | } | |
2092 | ||
2093 | void | |
2094 | print_other_binding_stack (stack) | |
2095 | struct binding_level *stack; | |
2096 | { | |
2097 | struct binding_level *level; | |
2098 | for (level = stack; level != global_binding_level; level = level->level_chain) | |
2099 | { | |
2100 | fprintf (stderr, "binding level "); | |
2101 | fprintf (stderr, HOST_PTR_PRINTF, level); | |
2102 | fprintf (stderr, "\n"); | |
2103 | print_binding_level (level); | |
2104 | } | |
2105 | } | |
2106 | ||
2107 | void | |
2108 | print_binding_stack () | |
2109 | { | |
2110 | struct binding_level *b; | |
2111 | fprintf (stderr, "current_binding_level="); | |
2112 | fprintf (stderr, HOST_PTR_PRINTF, current_binding_level); | |
2113 | fprintf (stderr, "\nclass_binding_level="); | |
2114 | fprintf (stderr, HOST_PTR_PRINTF, class_binding_level); | |
2115 | fprintf (stderr, "\nglobal_binding_level="); | |
2116 | fprintf (stderr, HOST_PTR_PRINTF, global_binding_level); | |
2117 | fprintf (stderr, "\n"); | |
2118 | if (class_binding_level) | |
2119 | { | |
2120 | for (b = class_binding_level; b; b = b->level_chain) | |
2121 | if (b == current_binding_level) | |
2122 | break; | |
2123 | if (b) | |
2124 | b = class_binding_level; | |
2125 | else | |
2126 | b = current_binding_level; | |
2127 | } | |
2128 | else | |
2129 | b = current_binding_level; | |
2130 | print_other_binding_stack (b); | |
2131 | fprintf (stderr, "global:\n"); | |
2132 | print_binding_level (global_binding_level); | |
2133 | } | |
a9aedbc2 | 2134 | |
2c73f9f5 ML |
2135 | /* Namespace binding access routines: The namespace_bindings field of |
2136 | the identifier is polymorphic, with three possible values: | |
2137 | NULL_TREE, a list of CPLUS_BINDINGS, or any other tree_node | |
2138 | indicating the BINDING_VALUE of global_namespace. */ | |
30394414 | 2139 | |
2c73f9f5 ML |
2140 | /* Check whether the a binding for the name to scope is known. |
2141 | Assumes that the bindings of the name are already a list | |
2142 | of bindings. Returns the binding found, or NULL_TREE. */ | |
2143 | ||
2144 | static tree | |
2145 | find_binding (name, scope) | |
30394414 JM |
2146 | tree name; |
2147 | tree scope; | |
2148 | { | |
2c73f9f5 | 2149 | tree iter, prev = NULL_TREE; |
3e3f722c ML |
2150 | |
2151 | scope = ORIGINAL_NAMESPACE (scope); | |
68642fb6 | 2152 | |
30394414 JM |
2153 | for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name); iter; |
2154 | iter = TREE_CHAIN (iter)) | |
2155 | { | |
2156 | my_friendly_assert (TREE_CODE (iter) == CPLUS_BINDING, 374); | |
2157 | if (BINDING_SCOPE (iter) == scope) | |
2c73f9f5 | 2158 | { |
cfe507be | 2159 | /* Move binding found to the front of the list, so |
2c73f9f5 ML |
2160 | subsequent lookups will find it faster. */ |
2161 | if (prev) | |
2162 | { | |
2163 | TREE_CHAIN (prev) = TREE_CHAIN (iter); | |
2164 | TREE_CHAIN (iter) = IDENTIFIER_NAMESPACE_BINDINGS (name); | |
2165 | IDENTIFIER_NAMESPACE_BINDINGS (name) = iter; | |
2166 | } | |
2167 | return iter; | |
2168 | } | |
2169 | prev = iter; | |
30394414 | 2170 | } |
2c73f9f5 ML |
2171 | return NULL_TREE; |
2172 | } | |
2173 | ||
2174 | /* Always returns a binding for name in scope. If the | |
2175 | namespace_bindings is not a list, convert it to one first. | |
2176 | If no binding is found, make a new one. */ | |
2177 | ||
2178 | tree | |
2179 | binding_for_name (name, scope) | |
2180 | tree name; | |
2181 | tree scope; | |
2182 | { | |
2183 | tree b = IDENTIFIER_NAMESPACE_BINDINGS (name); | |
2184 | tree result; | |
3e3f722c ML |
2185 | |
2186 | scope = ORIGINAL_NAMESPACE (scope); | |
68642fb6 | 2187 | |
2c73f9f5 ML |
2188 | if (b && TREE_CODE (b) != CPLUS_BINDING) |
2189 | { | |
2190 | /* Get rid of optimization for global scope. */ | |
2191 | IDENTIFIER_NAMESPACE_BINDINGS (name) = NULL_TREE; | |
2192 | BINDING_VALUE (binding_for_name (name, global_namespace)) = b; | |
2193 | b = IDENTIFIER_NAMESPACE_BINDINGS (name); | |
2194 | } | |
2195 | if (b && (result = find_binding (name, scope))) | |
2196 | return result; | |
cd9f6678 | 2197 | /* Not found, make a new one. */ |
2c73f9f5 ML |
2198 | result = make_node (CPLUS_BINDING); |
2199 | TREE_CHAIN (result) = b; | |
2200 | IDENTIFIER_NAMESPACE_BINDINGS (name) = result; | |
2201 | BINDING_SCOPE (result) = scope; | |
2202 | BINDING_TYPE (result) = NULL_TREE; | |
2203 | BINDING_VALUE (result) = NULL_TREE; | |
2c73f9f5 ML |
2204 | return result; |
2205 | } | |
2206 | ||
2207 | /* Return the binding value for name in scope, considering that | |
2208 | namespace_binding may or may not be a list of CPLUS_BINDINGS. */ | |
2209 | ||
2210 | tree | |
2211 | namespace_binding (name, scope) | |
2212 | tree name; | |
2213 | tree scope; | |
2214 | { | |
2215 | tree b = IDENTIFIER_NAMESPACE_BINDINGS (name); | |
2216 | if (b == NULL_TREE) | |
2217 | return NULL_TREE; | |
cb0dbb9a JM |
2218 | if (scope == NULL_TREE) |
2219 | scope = global_namespace; | |
2c73f9f5 ML |
2220 | if (TREE_CODE (b) != CPLUS_BINDING) |
2221 | return (scope == global_namespace) ? b : NULL_TREE; | |
2222 | name = find_binding (name,scope); | |
2223 | if (name == NULL_TREE) | |
2224 | return name; | |
2225 | return BINDING_VALUE (name); | |
2226 | } | |
2227 | ||
2228 | /* Set the binding value for name in scope. If modifying the binding | |
2229 | of global_namespace is attempted, try to optimize it. */ | |
2230 | ||
2231 | void | |
2232 | set_namespace_binding (name, scope, val) | |
2233 | tree name; | |
2234 | tree scope; | |
2235 | tree val; | |
2236 | { | |
2237 | tree b; | |
cb0dbb9a JM |
2238 | |
2239 | if (scope == NULL_TREE) | |
2240 | scope = global_namespace; | |
68642fb6 | 2241 | |
2c73f9f5 ML |
2242 | if (scope == global_namespace) |
2243 | { | |
2244 | b = IDENTIFIER_NAMESPACE_BINDINGS (name); | |
2245 | if (b == NULL_TREE || TREE_CODE (b) != CPLUS_BINDING) | |
2246 | { | |
2247 | IDENTIFIER_NAMESPACE_BINDINGS (name) = val; | |
2248 | return; | |
2249 | } | |
2250 | } | |
2251 | b = binding_for_name (name, scope); | |
2252 | BINDING_VALUE (b) = val; | |
30394414 JM |
2253 | } |
2254 | ||
dff6b454 RK |
2255 | /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we |
2256 | select a name that is unique to this compilation unit. */ | |
e92cc029 | 2257 | |
a9aedbc2 MS |
2258 | void |
2259 | push_namespace (name) | |
2260 | tree name; | |
2261 | { | |
b370501f | 2262 | tree d = NULL_TREE; |
30394414 JM |
2263 | int need_new = 1; |
2264 | int implicit_use = 0; | |
2c73f9f5 | 2265 | int global = 0; |
30394414 JM |
2266 | if (!global_namespace) |
2267 | { | |
2c73f9f5 | 2268 | /* This must be ::. */ |
30394414 | 2269 | my_friendly_assert (name == get_identifier ("::"), 377); |
2c73f9f5 | 2270 | global = 1; |
30394414 JM |
2271 | } |
2272 | else if (!name) | |
2273 | { | |
3ab52652 ML |
2274 | /* The name of anonymous namespace is unique for the translation |
2275 | unit. */ | |
0c8feefe MM |
2276 | if (!anonymous_namespace_name) |
2277 | anonymous_namespace_name = get_file_function_name ('N'); | |
2278 | name = anonymous_namespace_name; | |
3ab52652 ML |
2279 | d = IDENTIFIER_NAMESPACE_VALUE (name); |
2280 | if (d) | |
2281 | /* Reopening anonymous namespace. */ | |
2282 | need_new = 0; | |
30394414 | 2283 | implicit_use = 1; |
2c73f9f5 | 2284 | } |
30394414 JM |
2285 | else |
2286 | { | |
2c73f9f5 | 2287 | /* Check whether this is an extended namespace definition. */ |
30394414 JM |
2288 | d = IDENTIFIER_NAMESPACE_VALUE (name); |
2289 | if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL) | |
3e3f722c ML |
2290 | { |
2291 | need_new = 0; | |
2292 | if (DECL_NAMESPACE_ALIAS (d)) | |
2293 | { | |
33bd39a2 | 2294 | error ("namespace alias `%D' not allowed here, assuming `%D'", |
3e3f722c ML |
2295 | d, DECL_NAMESPACE_ALIAS (d)); |
2296 | d = DECL_NAMESPACE_ALIAS (d); | |
2297 | } | |
2298 | } | |
30394414 | 2299 | } |
68642fb6 | 2300 | |
30394414 JM |
2301 | if (need_new) |
2302 | { | |
2c73f9f5 | 2303 | /* Make a new namespace, binding the name to it. */ |
6b57ac29 | 2304 | d = build_lang_decl (NAMESPACE_DECL, name, void_type_node); |
2c73f9f5 ML |
2305 | /* The global namespace is not pushed, and the global binding |
2306 | level is set elsewhere. */ | |
2307 | if (!global) | |
2308 | { | |
40f1e342 | 2309 | DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace); |
2c73f9f5 ML |
2310 | d = pushdecl (d); |
2311 | pushlevel (0); | |
2312 | declare_namespace_level (); | |
2313 | NAMESPACE_LEVEL (d) = current_binding_level; | |
2314 | } | |
30394414 | 2315 | } |
2c73f9f5 ML |
2316 | else |
2317 | resume_binding_level (NAMESPACE_LEVEL (d)); | |
2318 | ||
30394414 JM |
2319 | if (implicit_use) |
2320 | do_using_directive (d); | |
2c73f9f5 | 2321 | /* Enter the name space. */ |
30394414 | 2322 | current_namespace = d; |
a9aedbc2 MS |
2323 | } |
2324 | ||
2325 | /* Pop from the scope of the current namespace. */ | |
e92cc029 | 2326 | |
a9aedbc2 MS |
2327 | void |
2328 | pop_namespace () | |
2329 | { | |
1dbb6023 | 2330 | my_friendly_assert (current_namespace != global_namespace, 20010801); |
cb0dbb9a | 2331 | current_namespace = CP_DECL_CONTEXT (current_namespace); |
2c73f9f5 ML |
2332 | /* The binding level is not popped, as it might be re-opened later. */ |
2333 | suspend_binding_level (); | |
2334 | } | |
a9aedbc2 | 2335 | |
61fc8c9e MM |
2336 | /* Push into the scope of the namespace NS, even if it is deeply |
2337 | nested within another namespace. */ | |
2338 | ||
2339 | void | |
2340 | push_nested_namespace (ns) | |
2341 | tree ns; | |
2342 | { | |
2343 | if (ns == global_namespace) | |
2344 | push_to_top_level (); | |
2345 | else | |
2346 | { | |
2347 | push_nested_namespace (CP_DECL_CONTEXT (ns)); | |
2348 | push_namespace (DECL_NAME (ns)); | |
2349 | } | |
2350 | } | |
2351 | ||
2352 | /* Pop back from the scope of the namespace NS, which was previously | |
2353 | entered with push_nested_namespace. */ | |
68642fb6 | 2354 | |
61fc8c9e MM |
2355 | void |
2356 | pop_nested_namespace (ns) | |
2357 | tree ns; | |
2358 | { | |
2359 | while (ns != global_namespace) | |
2360 | { | |
2361 | pop_namespace (); | |
2362 | ns = CP_DECL_CONTEXT (ns); | |
2363 | } | |
2364 | ||
2365 | pop_from_top_level (); | |
2366 | } | |
2367 | ||
8d08fdba MS |
2368 | \f |
2369 | /* Subroutines for reverting temporarily to top-level for instantiation | |
2370 | of templates and such. We actually need to clear out the class- and | |
2371 | local-value slots of all identifiers, so that only the global values | |
2372 | are at all visible. Simply setting current_binding_level to the global | |
2373 | scope isn't enough, because more binding levels may be pushed. */ | |
9cd64686 | 2374 | struct saved_scope *scope_chain; |
8d08fdba | 2375 | |
9cd64686 | 2376 | /* Mark ARG (which is really a struct saved_scope **) for GC. */ |
fc6af6e3 RH |
2377 | |
2378 | static void | |
2379 | mark_saved_scope (arg) | |
2380 | void *arg; | |
2381 | { | |
2382 | struct saved_scope *t = *(struct saved_scope **)arg; | |
2383 | while (t) | |
2384 | { | |
fc6af6e3 RH |
2385 | mark_binding_level (&t->class_bindings); |
2386 | ggc_mark_tree (t->old_bindings); | |
2387 | ggc_mark_tree (t->old_namespace); | |
ee8fc32b | 2388 | ggc_mark_tree (t->decl_ns_list); |
fc6af6e3 RH |
2389 | ggc_mark_tree (t->class_name); |
2390 | ggc_mark_tree (t->class_type); | |
2391 | ggc_mark_tree (t->access_specifier); | |
a8f73d4b | 2392 | ggc_mark_tree (t->function_decl); |
fc6af6e3 | 2393 | if (t->lang_base) |
9cd64686 | 2394 | ggc_mark_tree_varray (t->lang_base); |
fc6af6e3 | 2395 | ggc_mark_tree (t->lang_name); |
fc6af6e3 | 2396 | ggc_mark_tree (t->template_parms); |
9cd64686 MM |
2397 | ggc_mark_tree (t->x_previous_class_type); |
2398 | ggc_mark_tree (t->x_previous_class_values); | |
6f80451c | 2399 | ggc_mark_tree (t->x_saved_tree); |
70adf8a9 | 2400 | ggc_mark_tree (t->lookups); |
6f80451c MM |
2401 | |
2402 | mark_stmt_tree (&t->x_stmt_tree); | |
a8f73d4b | 2403 | mark_binding_level (&t->bindings); |
fc6af6e3 RH |
2404 | t = t->prev; |
2405 | } | |
2406 | } | |
2407 | ||
e92cc029 | 2408 | static tree |
45537677 MS |
2409 | store_bindings (names, old_bindings) |
2410 | tree names, old_bindings; | |
2411 | { | |
2412 | tree t; | |
c00996a3 JM |
2413 | tree search_bindings = old_bindings; |
2414 | ||
45537677 MS |
2415 | for (t = names; t; t = TREE_CHAIN (t)) |
2416 | { | |
2417 | tree binding, t1, id; | |
2418 | ||
2419 | if (TREE_CODE (t) == TREE_LIST) | |
2420 | id = TREE_PURPOSE (t); | |
2421 | else | |
2422 | id = DECL_NAME (t); | |
2423 | ||
68642fb6 | 2424 | if (!id |
f181d4ae MM |
2425 | /* Note that we may have an IDENTIFIER_CLASS_VALUE even when |
2426 | we have no IDENTIFIER_BINDING if we have left the class | |
2427 | scope, but cached the class-level declarations. */ | |
2428 | || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id))) | |
45537677 MS |
2429 | continue; |
2430 | ||
c00996a3 | 2431 | for (t1 = search_bindings; t1; t1 = TREE_CHAIN (t1)) |
45537677 MS |
2432 | if (TREE_VEC_ELT (t1, 0) == id) |
2433 | goto skip_it; | |
78957a2a | 2434 | |
31a714f6 | 2435 | my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135); |
87e3dbc9 | 2436 | binding = make_tree_vec (4); |
31a714f6 MM |
2437 | TREE_VEC_ELT (binding, 0) = id; |
2438 | TREE_VEC_ELT (binding, 1) = REAL_IDENTIFIER_TYPE_VALUE (id); | |
2439 | TREE_VEC_ELT (binding, 2) = IDENTIFIER_BINDING (id); | |
2440 | TREE_VEC_ELT (binding, 3) = IDENTIFIER_CLASS_VALUE (id); | |
2441 | IDENTIFIER_BINDING (id) = NULL_TREE; | |
2442 | IDENTIFIER_CLASS_VALUE (id) = NULL_TREE; | |
45537677 MS |
2443 | TREE_CHAIN (binding) = old_bindings; |
2444 | old_bindings = binding; | |
2445 | skip_it: | |
2446 | ; | |
2447 | } | |
2448 | return old_bindings; | |
2449 | } | |
2450 | ||
8d08fdba | 2451 | void |
5566b478 MS |
2452 | maybe_push_to_top_level (pseudo) |
2453 | int pseudo; | |
8d08fdba | 2454 | { |
a8f73d4b | 2455 | struct saved_scope *s; |
9cd64686 | 2456 | struct binding_level *b; |
a8f73d4b MM |
2457 | tree old_bindings; |
2458 | int need_pop; | |
2459 | ||
2460 | s = (struct saved_scope *) xcalloc (1, sizeof (struct saved_scope)); | |
8d08fdba | 2461 | |
9cd64686 MM |
2462 | b = scope_chain ? current_binding_level : 0; |
2463 | ||
a8f73d4b | 2464 | /* If we're in the middle of some function, save our state. */ |
01d939e8 | 2465 | if (cfun) |
a8f73d4b MM |
2466 | { |
2467 | need_pop = 1; | |
2468 | push_function_context_to (NULL_TREE); | |
2469 | } | |
2470 | else | |
2471 | need_pop = 0; | |
e349ee73 | 2472 | |
a8f73d4b | 2473 | old_bindings = NULL_TREE; |
9cd64686 | 2474 | if (scope_chain && previous_class_type) |
a50f0918 MS |
2475 | old_bindings = store_bindings (previous_class_values, old_bindings); |
2476 | ||
8d08fdba MS |
2477 | /* Have to include global_binding_level, because class-level decls |
2478 | aren't listed anywhere useful. */ | |
2479 | for (; b; b = b->level_chain) | |
2480 | { | |
2481 | tree t; | |
2482 | ||
2c73f9f5 ML |
2483 | /* Template IDs are inserted into the global level. If they were |
2484 | inserted into namespace level, finish_file wouldn't find them | |
2485 | when doing pending instantiations. Therefore, don't stop at | |
2486 | namespace level, but continue until :: . */ | |
74b846e0 | 2487 | if (b == global_binding_level || (pseudo && b->template_parms_p)) |
5566b478 | 2488 | break; |
8d08fdba | 2489 | |
45537677 | 2490 | old_bindings = store_bindings (b->names, old_bindings); |
cffa8729 | 2491 | /* We also need to check class_shadowed to save class-level type |
45537677 MS |
2492 | bindings, since pushclass doesn't fill in b->names. */ |
2493 | if (b->parm_flag == 2) | |
cffa8729 | 2494 | old_bindings = store_bindings (b->class_shadowed, old_bindings); |
8d08fdba | 2495 | |
8d08fdba MS |
2496 | /* Unwind type-value slots back to top level. */ |
2497 | for (t = b->type_shadowed; t; t = TREE_CHAIN (t)) | |
2498 | SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t)); | |
2499 | } | |
fc0e7bf5 MM |
2500 | s->prev = scope_chain; |
2501 | s->old_bindings = old_bindings; | |
a8f73d4b MM |
2502 | s->bindings = b; |
2503 | s->need_pop_function_context = need_pop; | |
2504 | s->function_decl = current_function_decl; | |
0f4237c2 | 2505 | s->last_parms = last_function_parms; |
fc0e7bf5 MM |
2506 | |
2507 | scope_chain = s; | |
8d08fdba | 2508 | current_function_decl = NULL_TREE; |
9cd64686 | 2509 | VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base"); |
51c184be | 2510 | current_lang_name = lang_name_cplusplus; |
2c73f9f5 | 2511 | current_namespace = global_namespace; |
5566b478 MS |
2512 | } |
2513 | ||
2514 | void | |
2515 | push_to_top_level () | |
2516 | { | |
2517 | maybe_push_to_top_level (0); | |
8d08fdba MS |
2518 | } |
2519 | ||
2520 | void | |
2521 | pop_from_top_level () | |
2522 | { | |
9cd64686 | 2523 | struct saved_scope *s = scope_chain; |
8d08fdba MS |
2524 | tree t; |
2525 | ||
e76a2646 | 2526 | /* Clear out class-level bindings cache. */ |
8d08fdba | 2527 | if (previous_class_type) |
8f032717 | 2528 | invalidate_class_lookup_cache (); |
8d08fdba | 2529 | |
9cd64686 MM |
2530 | VARRAY_FREE (current_lang_base); |
2531 | ||
2532 | scope_chain = s->prev; | |
87e3dbc9 | 2533 | for (t = s->old_bindings; t; t = TREE_CHAIN (t)) |
8d08fdba MS |
2534 | { |
2535 | tree id = TREE_VEC_ELT (t, 0); | |
31a714f6 MM |
2536 | |
2537 | SET_IDENTIFIER_TYPE_VALUE (id, TREE_VEC_ELT (t, 1)); | |
2538 | IDENTIFIER_BINDING (id) = TREE_VEC_ELT (t, 2); | |
2539 | IDENTIFIER_CLASS_VALUE (id) = TREE_VEC_ELT (t, 3); | |
8d08fdba | 2540 | } |
9cd64686 | 2541 | |
a8f73d4b MM |
2542 | /* If we were in the middle of compiling a function, restore our |
2543 | state. */ | |
2544 | if (s->need_pop_function_context) | |
2545 | pop_function_context_from (NULL_TREE); | |
2546 | current_function_decl = s->function_decl; | |
0f4237c2 | 2547 | last_function_parms = s->last_parms; |
e349ee73 | 2548 | |
a8f73d4b | 2549 | free (s); |
8d08fdba MS |
2550 | } |
2551 | \f | |
2552 | /* Push a definition of struct, union or enum tag "name". | |
68642fb6 | 2553 | into binding_level "b". "type" should be the type node, |
8d08fdba MS |
2554 | We assume that the tag "name" is not already defined. |
2555 | ||
2556 | Note that the definition may really be just a forward reference. | |
2557 | In that case, the TYPE_SIZE will be a NULL_TREE. | |
2558 | ||
e92cc029 | 2559 | C++ gratuitously puts all these tags in the name space. */ |
8d08fdba MS |
2560 | |
2561 | /* When setting the IDENTIFIER_TYPE_VALUE field of an identifier ID, | |
2562 | record the shadowed value for this binding contour. TYPE is | |
2563 | the type that ID maps to. */ | |
2564 | ||
2565 | static void | |
2566 | set_identifier_type_value_with_scope (id, type, b) | |
2567 | tree id; | |
2568 | tree type; | |
2569 | struct binding_level *b; | |
2570 | { | |
2c73f9f5 | 2571 | if (!b->namespace_p) |
8d08fdba | 2572 | { |
2c73f9f5 ML |
2573 | /* Shadow the marker, not the real thing, so that the marker |
2574 | gets restored later. */ | |
2575 | tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id); | |
8d08fdba MS |
2576 | b->type_shadowed |
2577 | = tree_cons (id, old_type_value, b->type_shadowed); | |
2578 | } | |
2c73f9f5 ML |
2579 | else |
2580 | { | |
2581 | tree binding = binding_for_name (id, current_namespace); | |
2582 | BINDING_TYPE (binding) = type; | |
2583 | /* Store marker instead of real type. */ | |
2584 | type = global_type_node; | |
2585 | } | |
8d08fdba MS |
2586 | SET_IDENTIFIER_TYPE_VALUE (id, type); |
2587 | } | |
2588 | ||
8f032717 | 2589 | /* As set_identifier_type_value_with_scope, but using current_binding_level. */ |
8d08fdba MS |
2590 | |
2591 | void | |
2592 | set_identifier_type_value (id, type) | |
2593 | tree id; | |
2594 | tree type; | |
2595 | { | |
8f032717 | 2596 | set_identifier_type_value_with_scope (id, type, current_binding_level); |
8d08fdba MS |
2597 | } |
2598 | ||
2c73f9f5 ML |
2599 | /* Return the type associated with id. */ |
2600 | ||
2601 | tree | |
2602 | identifier_type_value (id) | |
2603 | tree id; | |
2604 | { | |
2605 | /* There is no type with that name, anywhere. */ | |
2606 | if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE) | |
2607 | return NULL_TREE; | |
2608 | /* This is not the type marker, but the real thing. */ | |
2609 | if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node) | |
2610 | return REAL_IDENTIFIER_TYPE_VALUE (id); | |
2611 | /* Have to search for it. It must be on the global level, now. | |
2612 | Ask lookup_name not to return non-types. */ | |
3e3f722c | 2613 | id = lookup_name_real (id, 2, 1, 0); |
2c73f9f5 ML |
2614 | if (id) |
2615 | return TREE_TYPE (id); | |
2616 | return NULL_TREE; | |
2617 | } | |
2618 | ||
a9aedbc2 MS |
2619 | /* Pop off extraneous binding levels left over due to syntax errors. |
2620 | ||
2621 | We don't pop past namespaces, as they might be valid. */ | |
e92cc029 | 2622 | |
8926095f MS |
2623 | void |
2624 | pop_everything () | |
2625 | { | |
2626 | #ifdef DEBUG_CP_BINDING_LEVELS | |
2627 | fprintf (stderr, "XXX entering pop_everything ()\n"); | |
2628 | #endif | |
8f032717 | 2629 | while (!toplevel_bindings_p ()) |
8926095f | 2630 | { |
8f032717 | 2631 | if (current_binding_level->parm_flag == 2) |
b74a0560 | 2632 | pop_nested_class (); |
8926095f MS |
2633 | else |
2634 | poplevel (0, 0, 0); | |
2635 | } | |
2636 | #ifdef DEBUG_CP_BINDING_LEVELS | |
2637 | fprintf (stderr, "XXX leaving pop_everything ()\n"); | |
2638 | #endif | |
2639 | } | |
2640 | ||
39c01e4c MM |
2641 | /* The type TYPE is being declared. If it is a class template, or a |
2642 | specialization of a class template, do any processing required and | |
2643 | perform error-checking. If IS_FRIEND is non-zero, this TYPE is | |
2644 | being declared a friend. B is the binding level at which this TYPE | |
2645 | should be bound. | |
2646 | ||
2647 | Returns the TYPE_DECL for TYPE, which may have been altered by this | |
2648 | processing. */ | |
2649 | ||
68642fb6 | 2650 | static tree |
39c01e4c MM |
2651 | maybe_process_template_type_declaration (type, globalize, b) |
2652 | tree type; | |
2653 | int globalize; | |
2654 | struct binding_level* b; | |
2655 | { | |
2656 | tree decl = TYPE_NAME (type); | |
68642fb6 | 2657 | |
39c01e4c MM |
2658 | if (processing_template_parmlist) |
2659 | /* You can't declare a new template type in a template parameter | |
2660 | list. But, you can declare a non-template type: | |
68642fb6 | 2661 | |
39c01e4c | 2662 | template <class A*> struct S; |
68642fb6 | 2663 | |
39c01e4c MM |
2664 | is a forward-declaration of `A'. */ |
2665 | ; | |
68642fb6 | 2666 | else |
39c01e4c MM |
2667 | { |
2668 | maybe_check_template_type (type); | |
2669 | ||
68642fb6 | 2670 | my_friendly_assert (IS_AGGR_TYPE (type) |
ed44da02 | 2671 | || TREE_CODE (type) == ENUMERAL_TYPE, 0); |
68642fb6 UD |
2672 | |
2673 | ||
3ebc5c52 | 2674 | if (processing_template_decl) |
39c01e4c MM |
2675 | { |
2676 | /* This may change after the call to | |
2677 | push_template_decl_real, but we want the original value. */ | |
2678 | tree name = DECL_NAME (decl); | |
2679 | ||
2680 | decl = push_template_decl_real (decl, globalize); | |
2681 | /* If the current binding level is the binding level for the | |
2682 | template parameters (see the comment in | |
2683 | begin_template_parm_list) and the enclosing level is a class | |
2684 | scope, and we're not looking at a friend, push the | |
2685 | declaration of the member class into the class scope. In the | |
2686 | friend case, push_template_decl will already have put the | |
2687 | friend into global scope, if appropriate. */ | |
ed44da02 | 2688 | if (TREE_CODE (type) != ENUMERAL_TYPE |
74b846e0 | 2689 | && !globalize && b->template_parms_p |
39c01e4c MM |
2690 | && b->level_chain->parm_flag == 2) |
2691 | { | |
61a127b3 | 2692 | finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type)); |
39c01e4c MM |
2693 | /* Put this tag on the list of tags for the class, since |
2694 | that won't happen below because B is not the class | |
2695 | binding level, but is instead the pseudo-global level. */ | |
68642fb6 | 2696 | b->level_chain->tags = |
e1b3e07d | 2697 | tree_cons (name, type, b->level_chain->tags); |
d0f062fb | 2698 | if (!COMPLETE_TYPE_P (current_class_type)) |
39c01e4c MM |
2699 | CLASSTYPE_TAGS (current_class_type) = b->level_chain->tags; |
2700 | } | |
2701 | } | |
2702 | } | |
2703 | ||
2704 | return decl; | |
2705 | } | |
2706 | ||
9188c363 MM |
2707 | /* In C++, you don't have to write `struct S' to refer to `S'; you |
2708 | can just use `S'. We accomplish this by creating a TYPE_DECL as | |
2709 | if the user had written `typedef struct S S'. Create and return | |
2710 | the TYPE_DECL for TYPE. */ | |
2711 | ||
2712 | tree | |
2713 | create_implicit_typedef (name, type) | |
2714 | tree name; | |
2715 | tree type; | |
2716 | { | |
2717 | tree decl; | |
2718 | ||
2719 | decl = build_decl (TYPE_DECL, name, type); | |
c727aa5e | 2720 | DECL_ARTIFICIAL (decl) = 1; |
9188c363 MM |
2721 | /* There are other implicit type declarations, like the one *within* |
2722 | a class that allows you to write `S::S'. We must distinguish | |
2723 | amongst these. */ | |
2724 | SET_DECL_IMPLICIT_TYPEDEF_P (decl); | |
2725 | TYPE_NAME (type) = decl; | |
2726 | ||
2727 | return decl; | |
2728 | } | |
2729 | ||
8e4ce833 JJ |
2730 | /* Remember a local name for name-mangling purposes. */ |
2731 | ||
2732 | static void | |
2733 | push_local_name (decl) | |
2734 | tree decl; | |
2735 | { | |
2736 | size_t i, nelts; | |
2737 | tree t, name; | |
2738 | ||
2739 | if (!local_names) | |
2740 | VARRAY_TREE_INIT (local_names, 8, "local_names"); | |
2741 | ||
2742 | name = DECL_NAME (decl); | |
2743 | ||
2744 | nelts = VARRAY_ACTIVE_SIZE (local_names); | |
2745 | for (i = 0; i < nelts; i++) | |
2746 | { | |
2747 | t = VARRAY_TREE (local_names, i); | |
2748 | if (DECL_NAME (t) == name) | |
2749 | { | |
2750 | if (!DECL_LANG_SPECIFIC (decl)) | |
2751 | retrofit_lang_decl (decl); | |
2752 | if (DECL_LANG_SPECIFIC (t)) | |
2753 | DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1; | |
2754 | else | |
2755 | DECL_DISCRIMINATOR (decl) = 1; | |
2756 | ||
2757 | VARRAY_TREE (local_names, i) = decl; | |
2758 | return; | |
2759 | } | |
2760 | } | |
2761 | ||
2762 | VARRAY_PUSH_TREE (local_names, decl); | |
2763 | } | |
2764 | ||
8d08fdba | 2765 | /* Push a tag name NAME for struct/class/union/enum type TYPE. |
6757edfe | 2766 | Normally put it into the inner-most non-tag-transparent scope, |
8d08fdba | 2767 | but if GLOBALIZE is true, put it in the inner-most non-class scope. |
e92cc029 | 2768 | The latter is needed for implicit declarations. */ |
8d08fdba MS |
2769 | |
2770 | void | |
2771 | pushtag (name, type, globalize) | |
2772 | tree name, type; | |
2773 | int globalize; | |
2774 | { | |
2775 | register struct binding_level *b; | |
8d08fdba | 2776 | |
8f032717 | 2777 | b = current_binding_level; |
8d08fdba | 2778 | while (b->tag_transparent |
fcebb2dd NS |
2779 | || (b->parm_flag == 2 |
2780 | && (globalize | |
2781 | /* We may be defining a new type in the initializer | |
2782 | of a static member variable. We allow this when | |
2783 | not pedantic, and it is particularly useful for | |
2784 | type punning via an anonymous union. */ | |
2785 | || COMPLETE_TYPE_P (b->this_class)))) | |
8d08fdba MS |
2786 | b = b->level_chain; |
2787 | ||
e1b3e07d | 2788 | b->tags = tree_cons (name, type, b->tags); |
8d08fdba MS |
2789 | |
2790 | if (name) | |
2791 | { | |
8d08fdba | 2792 | /* Do C++ gratuitous typedefing. */ |
db5ae43f | 2793 | if (IDENTIFIER_TYPE_VALUE (name) != type) |
8d08fdba | 2794 | { |
93cdc044 | 2795 | register tree d = NULL_TREE; |
848cf1e0 | 2796 | int in_class = 0; |
9188c363 | 2797 | tree context = TYPE_CONTEXT (type); |
280f9385 | 2798 | |
280f9385 MM |
2799 | if (! context) |
2800 | { | |
2801 | tree cs = current_scope (); | |
2802 | ||
2803 | if (! globalize) | |
2804 | context = cs; | |
2f939d94 | 2805 | else if (cs != NULL_TREE && TYPE_P (cs)) |
280f9385 MM |
2806 | /* When declaring a friend class of a local class, we want |
2807 | to inject the newly named class into the scope | |
2808 | containing the local class, not the namespace scope. */ | |
4f1c5b7d | 2809 | context = decl_function_context (get_type_decl (cs)); |
280f9385 | 2810 | } |
280f9385 MM |
2811 | if (!context) |
2812 | context = current_namespace; | |
8d08fdba | 2813 | |
74b846e0 | 2814 | if ((b->template_parms_p && b->level_chain->parm_flag == 2) |
93cdc044 JM |
2815 | || b->parm_flag == 2) |
2816 | in_class = 1; | |
93cdc044 | 2817 | |
848cf1e0 MM |
2818 | if (current_lang_name == lang_name_java) |
2819 | TYPE_FOR_JAVA (type) = 1; | |
93cdc044 | 2820 | |
9188c363 | 2821 | d = create_implicit_typedef (name, type); |
cb0dbb9a | 2822 | DECL_CONTEXT (d) = FROB_CONTEXT (context); |
9188c363 MM |
2823 | if (! in_class) |
2824 | set_identifier_type_value_with_scope (name, type, b); | |
e1cd6e56 | 2825 | |
39c01e4c MM |
2826 | d = maybe_process_template_type_declaration (type, |
2827 | globalize, b); | |
93cdc044 JM |
2828 | |
2829 | if (b->parm_flag == 2) | |
61a127b3 | 2830 | { |
848cf1e0 | 2831 | if (!PROCESSING_REAL_TEMPLATE_DECL_P ()) |
61a127b3 MM |
2832 | /* Put this TYPE_DECL on the TYPE_FIELDS list for the |
2833 | class. But if it's a member template class, we | |
2834 | want the TEMPLATE_DECL, not the TYPE_DECL, so this | |
2835 | is done later. */ | |
2836 | finish_member_declaration (d); | |
8f032717 MM |
2837 | else |
2838 | pushdecl_class_level (d); | |
61a127b3 | 2839 | } |
93cdc044 JM |
2840 | else |
2841 | d = pushdecl_with_scope (d, b); | |
2842 | ||
1951a1b6 | 2843 | /* FIXME what if it gets a name from typedef? */ |
848cf1e0 MM |
2844 | if (ANON_AGGRNAME_P (name)) |
2845 | DECL_IGNORED_P (d) = 1; | |
2846 | ||
2847 | TYPE_CONTEXT (type) = DECL_CONTEXT (d); | |
1f6e1acc AS |
2848 | |
2849 | /* If this is a local class, keep track of it. We need this | |
2850 | information for name-mangling, and so that it is possible to find | |
2851 | all function definitions in a translation unit in a convenient | |
2852 | way. (It's otherwise tricky to find a member function definition | |
2853 | it's only pointed to from within a local class.) */ | |
5362b086 | 2854 | if (TYPE_CONTEXT (type) |
089acd57 MM |
2855 | && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL |
2856 | && !processing_template_decl) | |
1f6e1acc | 2857 | VARRAY_PUSH_TREE (local_classes, type); |
8d08fdba MS |
2858 | } |
2859 | if (b->parm_flag == 2) | |
2860 | { | |
d0f062fb | 2861 | if (!COMPLETE_TYPE_P (current_class_type)) |
8d08fdba MS |
2862 | CLASSTYPE_TAGS (current_class_type) = b->tags; |
2863 | } | |
2864 | } | |
2865 | ||
2866 | if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL) | |
2867 | /* Use the canonical TYPE_DECL for this node. */ | |
2868 | TYPE_STUB_DECL (type) = TYPE_NAME (type); | |
2869 | else | |
2870 | { | |
2871 | /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE | |
2872 | will be the tagged type we just added to the current | |
2873 | binding level. This fake NULL-named TYPE_DECL node helps | |
2874 | dwarfout.c to know when it needs to output a | |
2875 | representation of a tagged type, and it also gives us a | |
2876 | convenient place to record the "scope start" address for | |
2877 | the tagged type. */ | |
2878 | ||
8d08fdba | 2879 | tree d = build_decl (TYPE_DECL, NULL_TREE, type); |
8d08fdba MS |
2880 | TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b); |
2881 | } | |
2882 | } | |
2883 | ||
2884 | /* Counter used to create anonymous type names. */ | |
e92cc029 | 2885 | |
8d08fdba MS |
2886 | static int anon_cnt = 0; |
2887 | ||
2888 | /* Return an IDENTIFIER which can be used as a name for | |
2889 | anonymous structs and unions. */ | |
e92cc029 | 2890 | |
8d08fdba MS |
2891 | tree |
2892 | make_anon_name () | |
2893 | { | |
2894 | char buf[32]; | |
2895 | ||
2896 | sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++); | |
2897 | return get_identifier (buf); | |
2898 | } | |
2899 | ||
2900 | /* Clear the TREE_PURPOSE slot of tags which have anonymous typenames. | |
2901 | This keeps dbxout from getting confused. */ | |
e92cc029 | 2902 | |
8d08fdba MS |
2903 | void |
2904 | clear_anon_tags () | |
2905 | { | |
2906 | register struct binding_level *b; | |
2907 | register tree tags; | |
2908 | static int last_cnt = 0; | |
2909 | ||
2910 | /* Fast out if no new anon names were declared. */ | |
2911 | if (last_cnt == anon_cnt) | |
2912 | return; | |
2913 | ||
2914 | b = current_binding_level; | |
2915 | while (b->tag_transparent) | |
2916 | b = b->level_chain; | |
2917 | tags = b->tags; | |
2918 | while (tags) | |
2919 | { | |
2920 | /* A NULL purpose means we have already processed all tags | |
2921 | from here to the end of the list. */ | |
2922 | if (TREE_PURPOSE (tags) == NULL_TREE) | |
2923 | break; | |
2924 | if (ANON_AGGRNAME_P (TREE_PURPOSE (tags))) | |
2925 | TREE_PURPOSE (tags) = NULL_TREE; | |
2926 | tags = TREE_CHAIN (tags); | |
2927 | } | |
2928 | last_cnt = anon_cnt; | |
2929 | } | |
2930 | \f | |
2931 | /* Subroutine of duplicate_decls: return truthvalue of whether | |
2932 | or not types of these decls match. | |
2933 | ||
2934 | For C++, we must compare the parameter list so that `int' can match | |
2935 | `int&' in a parameter position, but `int&' is not confused with | |
2936 | `const int&'. */ | |
e92cc029 | 2937 | |
6060a796 | 2938 | int |
8d08fdba MS |
2939 | decls_match (newdecl, olddecl) |
2940 | tree newdecl, olddecl; | |
2941 | { | |
2942 | int types_match; | |
2943 | ||
347d73d7 ML |
2944 | if (newdecl == olddecl) |
2945 | return 1; | |
2946 | ||
6b4b3deb MM |
2947 | if (TREE_CODE (newdecl) != TREE_CODE (olddecl)) |
2948 | /* If the two DECLs are not even the same kind of thing, we're not | |
2949 | interested in their types. */ | |
2950 | return 0; | |
2951 | ||
2952 | if (TREE_CODE (newdecl) == FUNCTION_DECL) | |
8d08fdba MS |
2953 | { |
2954 | tree f1 = TREE_TYPE (newdecl); | |
2955 | tree f2 = TREE_TYPE (olddecl); | |
2956 | tree p1 = TYPE_ARG_TYPES (f1); | |
2957 | tree p2 = TYPE_ARG_TYPES (f2); | |
2958 | ||
4f1c5b7d | 2959 | if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl) |
eb68cb58 MM |
2960 | && ! (DECL_EXTERN_C_P (newdecl) |
2961 | && DECL_EXTERN_C_P (olddecl))) | |
c5a6fc45 JM |
2962 | return 0; |
2963 | ||
8d08fdba | 2964 | if (TREE_CODE (f1) != TREE_CODE (f2)) |
e867a179 | 2965 | return 0; |
8d08fdba | 2966 | |
3bfdc719 | 2967 | if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2))) |
8926095f | 2968 | { |
8b27e9ef NS |
2969 | if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl) |
2970 | && (DECL_BUILT_IN (olddecl) | |
2971 | #ifndef NO_IMPLICIT_EXTERN_C | |
2972 | || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl)) | |
2973 | || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl)) | |
2974 | #endif | |
2975 | )) | |
a28e3c7f MS |
2976 | { |
2977 | types_match = self_promoting_args_p (p1); | |
2978 | if (p1 == void_list_node) | |
2979 | TREE_TYPE (newdecl) = TREE_TYPE (olddecl); | |
2980 | } | |
8b27e9ef NS |
2981 | #ifndef NO_IMPLICIT_EXTERN_C |
2982 | else if (p1 == NULL_TREE | |
2983 | && (DECL_EXTERN_C_P (olddecl) | |
2984 | && DECL_IN_SYSTEM_HEADER (olddecl) | |
2985 | && !DECL_CLASS_SCOPE_P (olddecl)) | |
2986 | && (DECL_EXTERN_C_P (newdecl) | |
2987 | && DECL_IN_SYSTEM_HEADER (newdecl) | |
2988 | && !DECL_CLASS_SCOPE_P (newdecl))) | |
a28e3c7f MS |
2989 | { |
2990 | types_match = self_promoting_args_p (p2); | |
2991 | TREE_TYPE (newdecl) = TREE_TYPE (olddecl); | |
2992 | } | |
8b27e9ef | 2993 | #endif |
8926095f | 2994 | else |
91063b51 | 2995 | types_match = compparms (p1, p2); |
8926095f | 2996 | } |
8d08fdba MS |
2997 | else |
2998 | types_match = 0; | |
2999 | } | |
6b4b3deb | 3000 | else if (TREE_CODE (newdecl) == TEMPLATE_DECL) |
51c184be | 3001 | { |
f84b4be9 JM |
3002 | if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl), |
3003 | DECL_TEMPLATE_PARMS (olddecl))) | |
3004 | return 0; | |
68642fb6 | 3005 | |
2bb5d995 JM |
3006 | if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) |
3007 | != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))) | |
3008 | return 0; | |
3009 | ||
f84b4be9 JM |
3010 | if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL) |
3011 | types_match = 1; | |
3012 | else | |
3013 | types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl), | |
3014 | DECL_TEMPLATE_RESULT (newdecl)); | |
51c184be | 3015 | } |
8d08fdba MS |
3016 | else |
3017 | { | |
3018 | if (TREE_TYPE (newdecl) == error_mark_node) | |
3019 | types_match = TREE_TYPE (olddecl) == error_mark_node; | |
3020 | else if (TREE_TYPE (olddecl) == NULL_TREE) | |
3021 | types_match = TREE_TYPE (newdecl) == NULL_TREE; | |
a0a33927 MS |
3022 | else if (TREE_TYPE (newdecl) == NULL_TREE) |
3023 | types_match = 0; | |
8d08fdba | 3024 | else |
01240200 | 3025 | types_match = comptypes (TREE_TYPE (newdecl), |
3bfdc719 MM |
3026 | TREE_TYPE (olddecl), |
3027 | COMPARE_REDECLARATION); | |
8d08fdba MS |
3028 | } |
3029 | ||
3030 | return types_match; | |
3031 | } | |
3032 | ||
3033 | /* If NEWDECL is `static' and an `extern' was seen previously, | |
24f30ed4 | 3034 | warn about it. OLDDECL is the previous declaration. |
8d08fdba MS |
3035 | |
3036 | Note that this does not apply to the C++ case of declaring | |
3037 | a variable `extern const' and then later `const'. | |
3038 | ||
8d08fdba MS |
3039 | Don't complain about built-in functions, since they are beyond |
3040 | the user's control. */ | |
3041 | ||
3042 | static void | |
3043 | warn_extern_redeclared_static (newdecl, olddecl) | |
3044 | tree newdecl, olddecl; | |
3045 | { | |
83182544 | 3046 | static const char *const explicit_extern_static_warning |
8251199e | 3047 | = "`%D' was declared `extern' and later `static'"; |
83182544 | 3048 | static const char *const implicit_extern_static_warning |
8251199e JM |
3049 | = "`%D' was declared implicitly `extern' and later `static'"; |
3050 | ||
24f30ed4 MM |
3051 | tree name; |
3052 | ||
68642fb6 | 3053 | if (TREE_CODE (newdecl) == TYPE_DECL |
655dc6ee JM |
3054 | || TREE_CODE (newdecl) == TEMPLATE_DECL |
3055 | || TREE_CODE (newdecl) == CONST_DECL) | |
8d08fdba | 3056 | return; |
68642fb6 | 3057 | |
963d5758 MM |
3058 | /* Don't get confused by static member functions; that's a different |
3059 | use of `static'. */ | |
3060 | if (TREE_CODE (newdecl) == FUNCTION_DECL | |
3061 | && DECL_STATIC_FUNCTION_P (newdecl)) | |
3062 | return; | |
8d08fdba | 3063 | |
24f30ed4 MM |
3064 | /* If the old declaration was `static', or the new one isn't, then |
3065 | then everything is OK. */ | |
3066 | if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl)) | |
3067 | return; | |
3068 | ||
3069 | /* It's OK to declare a builtin function as `static'. */ | |
3070 | if (TREE_CODE (olddecl) == FUNCTION_DECL | |
3071 | && DECL_ARTIFICIAL (olddecl)) | |
3072 | return; | |
3073 | ||
8d08fdba | 3074 | name = DECL_ASSEMBLER_NAME (newdecl); |
33bd39a2 | 3075 | pedwarn (IDENTIFIER_IMPLICIT_DECL (name) |
24f30ed4 MM |
3076 | ? implicit_extern_static_warning |
3077 | : explicit_extern_static_warning, newdecl); | |
3078 | cp_pedwarn_at ("previous declaration of `%D'", olddecl); | |
8d08fdba MS |
3079 | } |
3080 | ||
3081 | /* Handle when a new declaration NEWDECL has the same name as an old | |
3082 | one OLDDECL in the same binding contour. Prints an error message | |
3083 | if appropriate. | |
3084 | ||
3085 | If safely possible, alter OLDDECL to look like NEWDECL, and return 1. | |
3086 | Otherwise, return 0. */ | |
3087 | ||
51c184be | 3088 | int |
8d08fdba | 3089 | duplicate_decls (newdecl, olddecl) |
824b9a4c | 3090 | tree newdecl, olddecl; |
8d08fdba | 3091 | { |
8d08fdba MS |
3092 | unsigned olddecl_uid = DECL_UID (olddecl); |
3093 | int olddecl_friend = 0, types_match = 0; | |
0b60dfe3 | 3094 | int new_defines_function = 0; |
5566b478 MS |
3095 | |
3096 | if (newdecl == olddecl) | |
3097 | return 1; | |
8d08fdba | 3098 | |
8926095f | 3099 | types_match = decls_match (newdecl, olddecl); |
8d08fdba | 3100 | |
8d08fdba MS |
3101 | /* If either the type of the new decl or the type of the old decl is an |
3102 | error_mark_node, then that implies that we have already issued an | |
3103 | error (earlier) for some bogus type specification, and in that case, | |
3104 | it is rather pointless to harass the user with yet more error message | |
0b60dfe3 | 3105 | about the same declaration, so just pretend the types match here. */ |
bd6dd845 MS |
3106 | if (TREE_TYPE (newdecl) == error_mark_node |
3107 | || TREE_TYPE (olddecl) == error_mark_node) | |
8d08fdba | 3108 | types_match = 1; |
68642fb6 | 3109 | |
97055d5c AO |
3110 | if (DECL_P (olddecl) |
3111 | && TREE_CODE (newdecl) == FUNCTION_DECL | |
3112 | && TREE_CODE (olddecl) == FUNCTION_DECL | |
3113 | && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl))) | |
3114 | { | |
3115 | if (DECL_DECLARED_INLINE_P (newdecl) | |
3116 | && DECL_UNINLINABLE (newdecl) | |
3117 | && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))) | |
3118 | /* Already warned elsewhere. */; | |
3119 | else if (DECL_DECLARED_INLINE_P (olddecl) | |
3120 | && DECL_UNINLINABLE (olddecl) | |
3121 | && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl))) | |
3122 | /* Already warned. */; | |
3123 | else if (DECL_DECLARED_INLINE_P (newdecl) | |
3124 | && DECL_UNINLINABLE (olddecl) | |
3125 | && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl))) | |
3126 | { | |
3127 | warning_with_decl (newdecl, | |
3128 | "function `%s' redeclared as inline"); | |
3129 | warning_with_decl (olddecl, | |
3130 | "previous declaration of function `%s' with attribute noinline"); | |
3131 | } | |
3132 | else if (DECL_DECLARED_INLINE_P (olddecl) | |
3133 | && DECL_UNINLINABLE (newdecl) | |
3134 | && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))) | |
3135 | { | |
3136 | warning_with_decl (newdecl, | |
3137 | "function `%s' redeclared with attribute noinline"); | |
3138 | warning_with_decl (olddecl, | |
3139 | "previous declaration of function `%s' was inline"); | |
3140 | } | |
3141 | } | |
3142 | ||
0b60dfe3 | 3143 | /* Check for redeclaration and other discrepancies. */ |
d22c8596 | 3144 | if (TREE_CODE (olddecl) == FUNCTION_DECL |
9f33663b JM |
3145 | && DECL_ARTIFICIAL (olddecl)) |
3146 | { | |
3147 | if (TREE_CODE (newdecl) != FUNCTION_DECL) | |
3148 | { | |
d52e4867 RS |
3149 | /* Avoid warnings redeclaring anticipated built-ins. */ |
3150 | if (DECL_ANTICIPATED (olddecl)) | |
3151 | return 0; | |
3152 | ||
9f33663b JM |
3153 | /* If you declare a built-in or predefined function name as static, |
3154 | the old definition is overridden, but optionally warn this was a | |
3155 | bad choice of name. */ | |
3156 | if (! TREE_PUBLIC (newdecl)) | |
3157 | { | |
3158 | if (warn_shadow) | |
33bd39a2 | 3159 | warning ("shadowing %s function `%#D'", |
9f33663b JM |
3160 | DECL_BUILT_IN (olddecl) ? "built-in" : "library", |
3161 | olddecl); | |
3162 | /* Discard the old built-in function. */ | |
3163 | return 0; | |
3164 | } | |
3165 | /* If the built-in is not ansi, then programs can override | |
3166 | it even globally without an error. */ | |
3167 | else if (! DECL_BUILT_IN (olddecl)) | |
33bd39a2 | 3168 | warning ("library function `%#D' redeclared as non-function `%#D'", |
9f33663b JM |
3169 | olddecl, newdecl); |
3170 | else | |
3171 | { | |
33bd39a2 ZW |
3172 | error ("declaration of `%#D'", newdecl); |
3173 | error ("conflicts with built-in declaration `%#D'", | |
a4443a08 | 3174 | olddecl); |
9f33663b | 3175 | } |
a4443a08 MS |
3176 | return 0; |
3177 | } | |
9f33663b | 3178 | else if (!types_match) |
8d08fdba | 3179 | { |
d52e4867 RS |
3180 | /* Avoid warnings redeclaring anticipated built-ins. */ |
3181 | if (DECL_ANTICIPATED (olddecl)) | |
3182 | ; /* Do nothing yet. */ | |
3183 | else if ((DECL_EXTERN_C_P (newdecl) | |
eb68cb58 | 3184 | && DECL_EXTERN_C_P (olddecl)) |
9f33663b JM |
3185 | || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)), |
3186 | TYPE_ARG_TYPES (TREE_TYPE (olddecl)))) | |
a4443a08 | 3187 | { |
9f33663b JM |
3188 | /* A near match; override the builtin. */ |
3189 | ||
3190 | if (TREE_PUBLIC (newdecl)) | |
a4443a08 | 3191 | { |
33bd39a2 ZW |
3192 | warning ("new declaration `%#D'", newdecl); |
3193 | warning ("ambiguates built-in declaration `%#D'", | |
9f33663b | 3194 | olddecl); |
a4443a08 | 3195 | } |
9f33663b | 3196 | else if (warn_shadow) |
33bd39a2 | 3197 | warning ("shadowing %s function `%#D'", |
9f33663b JM |
3198 | DECL_BUILT_IN (olddecl) ? "built-in" : "library", |
3199 | olddecl); | |
a4443a08 | 3200 | } |
9f33663b JM |
3201 | else |
3202 | /* Discard the old built-in function. */ | |
3203 | return 0; | |
04a9d080 RS |
3204 | |
3205 | /* Replace the old RTL to avoid problems with inlining. */ | |
3206 | SET_DECL_RTL (olddecl, DECL_RTL (newdecl)); | |
8d08fdba | 3207 | } |
68642fb6 | 3208 | |
24f30ed4 MM |
3209 | if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl)) |
3210 | { | |
3211 | /* If a builtin function is redeclared as `static', merge | |
3212 | the declarations, but make the original one static. */ | |
3213 | DECL_THIS_STATIC (olddecl) = 1; | |
3214 | TREE_PUBLIC (olddecl) = 0; | |
9bfadf57 | 3215 | |
421844e7 MM |
3216 | /* Make the old declaration consistent with the new one so |
3217 | that all remnants of the builtin-ness of this function | |
3218 | will be banished. */ | |
5d2ed28c | 3219 | SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl)); |
19e7881c | 3220 | SET_DECL_RTL (olddecl, DECL_RTL (newdecl)); |
24f30ed4 | 3221 | } |
39211cd5 MS |
3222 | } |
3223 | else if (TREE_CODE (olddecl) != TREE_CODE (newdecl)) | |
3224 | { | |
9ed182dc JM |
3225 | if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl) |
3226 | && TREE_CODE (newdecl) != TYPE_DECL | |
3227 | && ! (TREE_CODE (newdecl) == TEMPLATE_DECL | |
3228 | && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)) | |
3229 | || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl) | |
3230 | && TREE_CODE (olddecl) != TYPE_DECL | |
3231 | && ! (TREE_CODE (olddecl) == TEMPLATE_DECL | |
3232 | && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) | |
3233 | == TYPE_DECL)))) | |
3234 | { | |
3235 | /* We do nothing special here, because C++ does such nasty | |
3236 | things with TYPE_DECLs. Instead, just let the TYPE_DECL | |
3237 | get shadowed, and know that if we need to find a TYPE_DECL | |
3238 | for a given name, we can look in the IDENTIFIER_TYPE_VALUE | |
3239 | slot of the identifier. */ | |
3240 | return 0; | |
3241 | } | |
3242 | ||
39211cd5 | 3243 | if ((TREE_CODE (newdecl) == FUNCTION_DECL |
5566b478 | 3244 | && DECL_FUNCTION_TEMPLATE_P (olddecl)) |
39211cd5 | 3245 | || (TREE_CODE (olddecl) == FUNCTION_DECL |
5566b478 | 3246 | && DECL_FUNCTION_TEMPLATE_P (newdecl))) |
39211cd5 | 3247 | return 0; |
9ed182dc | 3248 | |
33bd39a2 | 3249 | error ("`%#D' redeclared as different kind of symbol", newdecl); |
39211cd5 MS |
3250 | if (TREE_CODE (olddecl) == TREE_LIST) |
3251 | olddecl = TREE_VALUE (olddecl); | |
8251199e | 3252 | cp_error_at ("previous declaration of `%#D'", olddecl); |
39211cd5 MS |
3253 | |
3254 | /* New decl is completely inconsistent with the old one => | |
3255 | tell caller to replace the old one. */ | |
3256 | ||
3257 | return 0; | |
8d08fdba | 3258 | } |
8d08fdba MS |
3259 | else if (!types_match) |
3260 | { | |
4f1c5b7d | 3261 | if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)) |
7bdbfa05 MM |
3262 | /* These are certainly not duplicate declarations; they're |
3263 | from different scopes. */ | |
3264 | return 0; | |
3265 | ||
8926095f | 3266 | if (TREE_CODE (newdecl) == TEMPLATE_DECL) |
f0e01782 MS |
3267 | { |
3268 | /* The name of a class template may not be declared to refer to | |
3269 | any other template, class, function, object, namespace, value, | |
e92cc029 | 3270 | or type in the same scope. */ |
5566b478 MS |
3271 | if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL |
3272 | || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL) | |
f0e01782 | 3273 | { |
33bd39a2 | 3274 | error ("declaration of template `%#D'", newdecl); |
8251199e | 3275 | cp_error_at ("conflicts with previous declaration `%#D'", |
f0e01782 MS |
3276 | olddecl); |
3277 | } | |
ec255269 MS |
3278 | else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL |
3279 | && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL | |
3280 | && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))), | |
91063b51 | 3281 | TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl)))) |
75650646 MM |
3282 | && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl), |
3283 | DECL_TEMPLATE_PARMS (olddecl))) | |
ec255269 | 3284 | { |
33bd39a2 | 3285 | error ("new declaration `%#D'", newdecl); |
8251199e | 3286 | cp_error_at ("ambiguates old declaration `%#D'", olddecl); |
ec255269 | 3287 | } |
f0e01782 MS |
3288 | return 0; |
3289 | } | |
8926095f MS |
3290 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
3291 | { | |
eb68cb58 | 3292 | if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl)) |
8926095f | 3293 | { |
33bd39a2 | 3294 | error ("declaration of C function `%#D' conflicts with", |
8926095f | 3295 | newdecl); |
8251199e | 3296 | cp_error_at ("previous declaration `%#D' here", olddecl); |
8926095f | 3297 | } |
00595019 | 3298 | else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)), |
91063b51 | 3299 | TYPE_ARG_TYPES (TREE_TYPE (olddecl)))) |
700f8a87 | 3300 | { |
33bd39a2 | 3301 | error ("new declaration `%#D'", newdecl); |
8251199e | 3302 | cp_error_at ("ambiguates old declaration `%#D'", olddecl); |
700f8a87 MS |
3303 | } |
3304 | else | |
3305 | return 0; | |
8926095f | 3306 | } |
8d08fdba MS |
3307 | |
3308 | /* Already complained about this, so don't do so again. */ | |
a4443a08 | 3309 | else if (current_class_type == NULL_TREE |
8d08fdba MS |
3310 | || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type) |
3311 | { | |
33bd39a2 | 3312 | error ("conflicting types for `%#D'", newdecl); |
8251199e | 3313 | cp_error_at ("previous declaration as `%#D'", olddecl); |
8d08fdba MS |
3314 | } |
3315 | } | |
68642fb6 | 3316 | else if (TREE_CODE (newdecl) == FUNCTION_DECL |
75650646 MM |
3317 | && ((DECL_TEMPLATE_SPECIALIZATION (olddecl) |
3318 | && (!DECL_TEMPLATE_INFO (newdecl) | |
68642fb6 | 3319 | || (DECL_TI_TEMPLATE (newdecl) |
75650646 MM |
3320 | != DECL_TI_TEMPLATE (olddecl)))) |
3321 | || (DECL_TEMPLATE_SPECIALIZATION (newdecl) | |
3322 | && (!DECL_TEMPLATE_INFO (olddecl) | |
68642fb6 | 3323 | || (DECL_TI_TEMPLATE (olddecl) |
75650646 | 3324 | != DECL_TI_TEMPLATE (newdecl)))))) |
386b8a85 JM |
3325 | /* It's OK to have a template specialization and a non-template |
3326 | with the same type, or to have specializations of two | |
75650646 MM |
3327 | different templates with the same type. Note that if one is a |
3328 | specialization, and the other is an instantiation of the same | |
3329 | template, that we do not exit at this point. That situation | |
3330 | can occur if we instantiate a template class, and then | |
3331 | specialize one of its methods. This situation is legal, but | |
3332 | the declarations must be merged in the usual way. */ | |
3333 | return 0; | |
68642fb6 UD |
3334 | else if (TREE_CODE (newdecl) == FUNCTION_DECL |
3335 | && ((DECL_TEMPLATE_INSTANTIATION (olddecl) | |
75650646 MM |
3336 | && !DECL_USE_TEMPLATE (newdecl)) |
3337 | || (DECL_TEMPLATE_INSTANTIATION (newdecl) | |
3338 | && !DECL_USE_TEMPLATE (olddecl)))) | |
3339 | /* One of the declarations is a template instantiation, and the | |
3340 | other is not a template at all. That's OK. */ | |
386b8a85 | 3341 | return 0; |
85c6cbaf ML |
3342 | else if (TREE_CODE (newdecl) == NAMESPACE_DECL |
3343 | && DECL_NAMESPACE_ALIAS (newdecl) | |
3344 | && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl)) | |
3345 | /* Redeclaration of namespace alias, ignore it. */ | |
3346 | return 1; | |
8d08fdba MS |
3347 | else |
3348 | { | |
d8e178a0 | 3349 | const char *errmsg = redeclaration_error_message (newdecl, olddecl); |
8251199e | 3350 | if (errmsg) |
8d08fdba | 3351 | { |
33bd39a2 | 3352 | error (errmsg, newdecl); |
8d08fdba MS |
3353 | if (DECL_NAME (olddecl) != NULL_TREE) |
3354 | cp_error_at ((DECL_INITIAL (olddecl) | |
2c73f9f5 | 3355 | && namespace_bindings_p ()) |
8251199e JM |
3356 | ? "`%#D' previously defined here" |
3357 | : "`%#D' previously declared here", olddecl); | |
00fae85d | 3358 | return 0; |
8d08fdba MS |
3359 | } |
3360 | else if (TREE_CODE (olddecl) == FUNCTION_DECL | |
3361 | && DECL_INITIAL (olddecl) != NULL_TREE | |
3362 | && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE | |
3363 | && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE) | |
3364 | { | |
3365 | /* Prototype decl follows defn w/o prototype. */ | |
8251199e JM |
3366 | cp_warning_at ("prototype for `%#D'", newdecl); |
3367 | cp_warning_at ("follows non-prototype definition here", olddecl); | |
8d08fdba MS |
3368 | } |
3369 | else if (TREE_CODE (olddecl) == FUNCTION_DECL | |
3370 | && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl)) | |
8926095f MS |
3371 | { |
3372 | /* extern "C" int foo (); | |
3373 | int foo () { bar (); } | |
3374 | is OK. */ | |
46ccf50a | 3375 | if (current_lang_depth () == 0) |
5d2ed28c | 3376 | SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl)); |
8926095f MS |
3377 | else |
3378 | { | |
8251199e | 3379 | cp_error_at ("previous declaration of `%#D' with %L linkage", |
8926095f | 3380 | olddecl, DECL_LANGUAGE (olddecl)); |
33bd39a2 | 3381 | error ("conflicts with new declaration with %L linkage", |
8926095f MS |
3382 | DECL_LANGUAGE (newdecl)); |
3383 | } | |
3384 | } | |
e1cd6e56 | 3385 | |
a6f02587 | 3386 | if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl)) |
c32381b1 JM |
3387 | ; |
3388 | else if (TREE_CODE (olddecl) == FUNCTION_DECL) | |
e1cd6e56 MS |
3389 | { |
3390 | tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl)); | |
3391 | tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)); | |
3392 | int i = 1; | |
3393 | ||
3394 | if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE) | |
3395 | t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2); | |
68642fb6 | 3396 | |
e1cd6e56 MS |
3397 | for (; t1 && t1 != void_list_node; |
3398 | t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++) | |
3399 | if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2)) | |
3400 | { | |
2507f3b5 RK |
3401 | if (1 == simple_cst_equal (TREE_PURPOSE (t1), |
3402 | TREE_PURPOSE (t2))) | |
e1cd6e56 | 3403 | { |
624a5800 CR |
3404 | pedwarn ("default argument given for parameter %d of `%#D'", |
3405 | i, newdecl); | |
3406 | cp_pedwarn_at ("after previous specification in `%#D'", | |
3407 | olddecl); | |
e1cd6e56 MS |
3408 | } |
3409 | else | |
3410 | { | |
33bd39a2 | 3411 | error ("default argument given for parameter %d of `%#D'", |
e1cd6e56 | 3412 | i, newdecl); |
8251199e | 3413 | cp_error_at ("after previous specification in `%#D'", |
e1cd6e56 MS |
3414 | olddecl); |
3415 | } | |
3416 | } | |
a5894242 | 3417 | |
79065db2 MM |
3418 | if (DECL_DECLARED_INLINE_P (newdecl) |
3419 | && ! DECL_DECLARED_INLINE_P (olddecl) | |
7fcdf4c2 | 3420 | && TREE_ADDRESSABLE (olddecl) && warn_inline) |
dff6b454 | 3421 | { |
33bd39a2 | 3422 | warning ("`%#D' was used before it was declared inline", |
7fcdf4c2 | 3423 | newdecl); |
8251199e | 3424 | cp_warning_at ("previous non-inline declaration here", |
7fcdf4c2 | 3425 | olddecl); |
dff6b454 | 3426 | } |
e1cd6e56 | 3427 | } |
8d08fdba MS |
3428 | } |
3429 | ||
4782bd5b RS |
3430 | /* Do not merge an implicit typedef with an explicit one. In: |
3431 | ||
3432 | class A; | |
3433 | ... | |
3434 | typedef class A A __attribute__ ((foo)); | |
3435 | ||
3436 | the attribute should apply only to the typedef. */ | |
3437 | if (TREE_CODE (olddecl) == TYPE_DECL | |
3438 | && (DECL_IMPLICIT_TYPEDEF_P (olddecl) | |
3439 | || DECL_IMPLICIT_TYPEDEF_P (newdecl))) | |
3440 | return 0; | |
3441 | ||
8d08fdba MS |
3442 | /* If new decl is `static' and an `extern' was seen previously, |
3443 | warn about it. */ | |
3444 | warn_extern_redeclared_static (newdecl, olddecl); | |
3445 | ||
e92cc029 | 3446 | /* We have committed to returning 1 at this point. */ |
8d08fdba MS |
3447 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
3448 | { | |
3449 | /* Now that functions must hold information normally held | |
3450 | by field decls, there is extra work to do so that | |
3451 | declaration information does not get destroyed during | |
3452 | definition. */ | |
3453 | if (DECL_VINDEX (olddecl)) | |
3454 | DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl); | |
4f1c5b7d MM |
3455 | if (DECL_VIRTUAL_CONTEXT (olddecl)) |
3456 | DECL_VIRTUAL_CONTEXT (newdecl) = DECL_VIRTUAL_CONTEXT (olddecl); | |
8d08fdba MS |
3457 | if (DECL_CONTEXT (olddecl)) |
3458 | DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl); | |
0e5921e8 | 3459 | if (DECL_PENDING_INLINE_INFO (newdecl) == 0) |
8d08fdba | 3460 | DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl); |
e1cd6e56 MS |
3461 | DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl); |
3462 | DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl); | |
fee7654e | 3463 | DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl); |
864b83b9 | 3464 | DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl); |
4a67c9e9 | 3465 | DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl); |
963d5758 | 3466 | DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl); |
e0fff4b3 JM |
3467 | if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK) |
3468 | SET_OVERLOADED_OPERATOR_CODE | |
3469 | (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl)); | |
0b60dfe3 | 3470 | new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE; |
68642fb6 | 3471 | |
0b60dfe3 BK |
3472 | /* Optionally warn about more than one declaration for the same |
3473 | name, but don't warn about a function declaration followed by a | |
3474 | definition. */ | |
3475 | if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl) | |
3476 | && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE) | |
3477 | /* Don't warn about extern decl followed by definition. */ | |
3478 | && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl)) | |
3479 | /* Don't warn about friends, let add_friend take care of it. */ | |
3581fae0 | 3480 | && ! (DECL_FRIEND_P (newdecl) || DECL_FRIEND_P (olddecl))) |
0b60dfe3 | 3481 | { |
33bd39a2 | 3482 | warning ("redundant redeclaration of `%D' in same scope", newdecl); |
8251199e | 3483 | cp_warning_at ("previous declaration of `%D'", olddecl); |
0b60dfe3 | 3484 | } |
8d08fdba MS |
3485 | } |
3486 | ||
3487 | /* Deal with C++: must preserve virtual function table size. */ | |
3488 | if (TREE_CODE (olddecl) == TYPE_DECL) | |
3489 | { | |
3490 | register tree newtype = TREE_TYPE (newdecl); | |
3491 | register tree oldtype = TREE_TYPE (olddecl); | |
3492 | ||
3493 | if (newtype != error_mark_node && oldtype != error_mark_node | |
3494 | && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype)) | |
3495 | { | |
3496 | CLASSTYPE_VSIZE (newtype) = CLASSTYPE_VSIZE (oldtype); | |
3497 | CLASSTYPE_FRIEND_CLASSES (newtype) | |
3498 | = CLASSTYPE_FRIEND_CLASSES (oldtype); | |
3499 | } | |
970d6386 MM |
3500 | |
3501 | DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl); | |
8d08fdba MS |
3502 | } |
3503 | ||
8d08fdba MS |
3504 | /* Copy all the DECL_... slots specified in the new decl |
3505 | except for any that we copy here from the old type. */ | |
91d231cb | 3506 | DECL_ATTRIBUTES (newdecl) |
f6897b10 | 3507 | = (*targetm.merge_decl_attributes) (olddecl, newdecl); |
6aa77e6c | 3508 | decl_attributes (&newdecl, DECL_ATTRIBUTES (newdecl), 0); |
8d08fdba | 3509 | |
5566b478 MS |
3510 | if (TREE_CODE (newdecl) == TEMPLATE_DECL) |
3511 | { | |
4d85e00e | 3512 | TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl)); |
68642fb6 | 3513 | DECL_TEMPLATE_SPECIALIZATIONS (olddecl) |
4d85e00e MM |
3514 | = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl), |
3515 | DECL_TEMPLATE_SPECIALIZATIONS (newdecl)); | |
68642fb6 | 3516 | |
daa8df65 MM |
3517 | /* If the new declaration is a definition, update the file and |
3518 | line information on the declaration. */ | |
06d5e633 NS |
3519 | if (DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)) == NULL_TREE |
3520 | && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl)) != NULL_TREE) | |
daa8df65 MM |
3521 | { |
3522 | DECL_SOURCE_LINE (olddecl) | |
06d5e633 | 3523 | = DECL_SOURCE_LINE (DECL_TEMPLATE_RESULT (olddecl)) |
daa8df65 MM |
3524 | = DECL_SOURCE_LINE (newdecl); |
3525 | DECL_SOURCE_FILE (olddecl) | |
06d5e633 | 3526 | = DECL_SOURCE_FILE (DECL_TEMPLATE_RESULT (olddecl)) |
daa8df65 MM |
3527 | = DECL_SOURCE_FILE (newdecl); |
3528 | } | |
3529 | ||
5566b478 MS |
3530 | return 1; |
3531 | } | |
68642fb6 | 3532 | |
8d08fdba MS |
3533 | if (types_match) |
3534 | { | |
3535 | /* Automatically handles default parameters. */ | |
3536 | tree oldtype = TREE_TYPE (olddecl); | |
e1cd6e56 | 3537 | tree newtype; |
8d08fdba | 3538 | |
e1cd6e56 | 3539 | /* Merge the data types specified in the two decls. */ |
6da794e8 | 3540 | newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl)); |
e1cd6e56 | 3541 | |
6da794e8 | 3542 | /* If merge_types produces a non-typedef type, just use the old type. */ |
9076e292 JM |
3543 | if (TREE_CODE (newdecl) == TYPE_DECL |
3544 | && newtype == DECL_ORIGINAL_TYPE (newdecl)) | |
3545 | newtype = oldtype; | |
3546 | ||
8d08fdba MS |
3547 | if (TREE_CODE (newdecl) == VAR_DECL) |
3548 | DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl); | |
6da794e8 | 3549 | /* Do this after calling `merge_types' so that default |
8d08fdba MS |
3550 | parameters don't confuse us. */ |
3551 | else if (TREE_CODE (newdecl) == FUNCTION_DECL | |
3552 | && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)) | |
3553 | != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)))) | |
3554 | { | |
f30432d7 | 3555 | TREE_TYPE (newdecl) = build_exception_variant (newtype, |
8d08fdba | 3556 | TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))); |
f30432d7 | 3557 | TREE_TYPE (olddecl) = build_exception_variant (newtype, |
8d08fdba MS |
3558 | TYPE_RAISES_EXCEPTIONS (oldtype)); |
3559 | ||
9a224b4a JM |
3560 | if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl)) |
3561 | && DECL_SOURCE_LINE (olddecl) != 0 | |
da20811c | 3562 | && flag_exceptions |
4cc1d462 NS |
3563 | && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)), |
3564 | TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1)) | |
8d08fdba | 3565 | { |
33bd39a2 | 3566 | error ("declaration of `%F' throws different exceptions", |
a28e3c7f | 3567 | newdecl); |
a09ba2e0 | 3568 | cp_error_at ("than previous declaration `%F'", olddecl); |
8d08fdba MS |
3569 | } |
3570 | } | |
3571 | TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype; | |
3572 | ||
3573 | /* Lay the type out, unless already done. */ | |
407f03b8 | 3574 | if (! same_type_p (newtype, oldtype) |
5566b478 | 3575 | && TREE_TYPE (newdecl) != error_mark_node |
5156628f | 3576 | && !(processing_template_decl && uses_template_parms (newdecl))) |
b7484fbe MS |
3577 | layout_type (TREE_TYPE (newdecl)); |
3578 | ||
5566b478 MS |
3579 | if ((TREE_CODE (newdecl) == VAR_DECL |
3580 | || TREE_CODE (newdecl) == PARM_DECL | |
3581 | || TREE_CODE (newdecl) == RESULT_DECL | |
3582 | || TREE_CODE (newdecl) == FIELD_DECL | |
3583 | || TREE_CODE (newdecl) == TYPE_DECL) | |
5156628f | 3584 | && !(processing_template_decl && uses_template_parms (newdecl))) |
b7484fbe | 3585 | layout_decl (newdecl, 0); |
8d08fdba MS |
3586 | |
3587 | /* Merge the type qualifiers. */ | |
3588 | if (TREE_READONLY (newdecl)) | |
3589 | TREE_READONLY (olddecl) = 1; | |
3590 | if (TREE_THIS_VOLATILE (newdecl)) | |
3591 | TREE_THIS_VOLATILE (olddecl) = 1; | |
3592 | ||
3593 | /* Merge the initialization information. */ | |
8926095f MS |
3594 | if (DECL_INITIAL (newdecl) == NULL_TREE |
3595 | && DECL_INITIAL (olddecl) != NULL_TREE) | |
3596 | { | |
3597 | DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl); | |
3598 | DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl); | |
3599 | DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl); | |
b0d06515 MM |
3600 | if (CAN_HAVE_FULL_LANG_DECL_P (newdecl) |
3601 | && DECL_LANG_SPECIFIC (newdecl) | |
4d85e00e MM |
3602 | && DECL_LANG_SPECIFIC (olddecl)) |
3603 | DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl); | |
8926095f | 3604 | } |
39211cd5 MS |
3605 | |
3606 | /* Merge the section attribute. | |
3607 | We want to issue an error if the sections conflict but that must be | |
3608 | done later in decl_attributes since we are called before attributes | |
3609 | are assigned. */ | |
3610 | if (DECL_SECTION_NAME (newdecl) == NULL_TREE) | |
3611 | DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl); | |
3612 | ||
5d73aa63 | 3613 | /* Keep the old rtl since we can safely use it. */ |
19e7881c | 3614 | COPY_DECL_RTL (olddecl, newdecl); |
a157febd GK |
3615 | |
3616 | if (TREE_CODE (newdecl) == FUNCTION_DECL) | |
3617 | { | |
3618 | DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl) | |
3619 | |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl); | |
a157febd GK |
3620 | DECL_NO_LIMIT_STACK (newdecl) |
3621 | |= DECL_NO_LIMIT_STACK (olddecl); | |
3622 | } | |
8d08fdba MS |
3623 | } |
3624 | /* If cannot merge, then use the new type and qualifiers, | |
3625 | and don't preserve the old rtl. */ | |
3626 | else | |
3627 | { | |
3628 | /* Clean out any memory we had of the old declaration. */ | |
3629 | tree oldstatic = value_member (olddecl, static_aggregates); | |
3630 | if (oldstatic) | |
3631 | TREE_VALUE (oldstatic) = error_mark_node; | |
3632 | ||
3633 | TREE_TYPE (olddecl) = TREE_TYPE (newdecl); | |
3634 | TREE_READONLY (olddecl) = TREE_READONLY (newdecl); | |
3635 | TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl); | |
3636 | TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl); | |
3637 | } | |
3638 | ||
3639 | /* Merge the storage class information. */ | |
a9aedbc2 | 3640 | DECL_WEAK (newdecl) |= DECL_WEAK (olddecl); |
e92cc029 | 3641 | DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl); |
794d4a61 | 3642 | DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl); |
a9aedbc2 MS |
3643 | TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl); |
3644 | TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl); | |
3645 | if (! DECL_EXTERNAL (olddecl)) | |
3646 | DECL_EXTERNAL (newdecl) = 0; | |
68642fb6 | 3647 | |
0b60dfe3 | 3648 | if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl)) |
8d08fdba | 3649 | { |
a9aedbc2 MS |
3650 | DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl); |
3651 | DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl); | |
77be6f82 | 3652 | DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl); |
68642fb6 | 3653 | DECL_TEMPLATE_INSTANTIATED (newdecl) |
2b0a63a3 | 3654 | |= DECL_TEMPLATE_INSTANTIATED (olddecl); |
0b60dfe3 BK |
3655 | /* Don't really know how much of the language-specific |
3656 | values we should copy from old to new. */ | |
3657 | DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl); | |
3658 | DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl); | |
3659 | DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl); | |
fbf1c34b | 3660 | DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl); |
3febd123 NS |
3661 | DECL_INITIALIZED_IN_CLASS_P (newdecl) |
3662 | |= DECL_INITIALIZED_IN_CLASS_P (olddecl); | |
0b60dfe3 | 3663 | olddecl_friend = DECL_FRIEND_P (olddecl); |
6a629cac MM |
3664 | |
3665 | /* Only functions have DECL_BEFRIENDING_CLASSES. */ | |
3666 | if (TREE_CODE (newdecl) == FUNCTION_DECL | |
3667 | || DECL_FUNCTION_TEMPLATE_P (newdecl)) | |
3668 | DECL_BEFRIENDING_CLASSES (newdecl) | |
3669 | = chainon (DECL_BEFRIENDING_CLASSES (newdecl), | |
3670 | DECL_BEFRIENDING_CLASSES (olddecl)); | |
8d08fdba MS |
3671 | } |
3672 | ||
8d08fdba MS |
3673 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
3674 | { | |
68642fb6 UD |
3675 | if (DECL_TEMPLATE_INSTANTIATION (olddecl) |
3676 | && !DECL_TEMPLATE_INSTANTIATION (newdecl)) | |
75650646 MM |
3677 | { |
3678 | /* If newdecl is not a specialization, then it is not a | |
3679 | template-related function at all. And that means that we | |
3680 | shoud have exited above, returning 0. */ | |
3681 | my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl), | |
3682 | 0); | |
3683 | ||
68642fb6 | 3684 | if (TREE_USED (olddecl)) |
75650646 | 3685 | /* From [temp.expl.spec]: |
68642fb6 | 3686 | |
75650646 MM |
3687 | If a template, a member template or the member of a class |
3688 | template is explicitly specialized then that | |
3689 | specialization shall be declared before the first use of | |
3690 | that specialization that would cause an implicit | |
3691 | instantiation to take place, in every translation unit in | |
3692 | which such a use occurs. */ | |
33bd39a2 | 3693 | error ("explicit specialization of %D after first use", |
75650646 MM |
3694 | olddecl); |
3695 | ||
3696 | SET_DECL_TEMPLATE_SPECIALIZATION (olddecl); | |
3697 | } | |
79065db2 | 3698 | DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl); |
faae18ab MS |
3699 | |
3700 | /* If either decl says `inline', this fn is inline, unless its | |
3701 | definition was passed already. */ | |
3702 | if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE) | |
3703 | DECL_INLINE (olddecl) = 1; | |
3704 | DECL_INLINE (newdecl) = DECL_INLINE (olddecl); | |
3705 | ||
97055d5c AO |
3706 | DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl) |
3707 | = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)); | |
3708 | ||
5daf7c0a JM |
3709 | /* Preserve abstractness on cloned [cd]tors. */ |
3710 | DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl); | |
3711 | ||
700f8a87 MS |
3712 | if (! types_match) |
3713 | { | |
5d2ed28c | 3714 | SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl)); |
92643fea | 3715 | COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl); |
19e7881c | 3716 | SET_DECL_RTL (olddecl, DECL_RTL (newdecl)); |
5566b478 MS |
3717 | } |
3718 | if (! types_match || new_defines_function) | |
3719 | { | |
6f1b4c42 JM |
3720 | /* These need to be copied so that the names are available. |
3721 | Note that if the types do match, we'll preserve inline | |
3722 | info and other bits, but if not, we won't. */ | |
700f8a87 MS |
3723 | DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl); |
3724 | DECL_RESULT (olddecl) = DECL_RESULT (newdecl); | |
700f8a87 | 3725 | } |
8d08fdba MS |
3726 | if (new_defines_function) |
3727 | /* If defining a function declared with other language | |
3728 | linkage, use the previously declared language linkage. */ | |
5d2ed28c | 3729 | SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl)); |
6f1b4c42 | 3730 | else if (types_match) |
8d08fdba MS |
3731 | { |
3732 | /* If redeclaring a builtin function, and not a definition, | |
3733 | it stays built in. */ | |
3734 | if (DECL_BUILT_IN (olddecl)) | |
3735 | { | |
26db82d8 | 3736 | DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl); |
39211cd5 | 3737 | DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl); |
8d08fdba MS |
3738 | /* If we're keeping the built-in definition, keep the rtl, |
3739 | regardless of declaration matches. */ | |
19e7881c | 3740 | SET_DECL_RTL (newdecl, DECL_RTL (olddecl)); |
8d08fdba MS |
3741 | } |
3742 | else | |
b850de4f | 3743 | DECL_NUM_STMTS (newdecl) = DECL_NUM_STMTS (olddecl); |
8d08fdba MS |
3744 | |
3745 | DECL_RESULT (newdecl) = DECL_RESULT (olddecl); | |
8d08fdba MS |
3746 | /* Don't clear out the arguments if we're redefining a function. */ |
3747 | if (DECL_ARGUMENTS (olddecl)) | |
3748 | DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl); | |
3749 | } | |
3750 | } | |
0b1161fc MM |
3751 | else if (TREE_CODE (newdecl) == NAMESPACE_DECL) |
3752 | NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl); | |
a9aedbc2 | 3753 | |
8d08fdba MS |
3754 | /* Now preserve various other info from the definition. */ |
3755 | TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl); | |
3756 | TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl); | |
3757 | DECL_COMMON (newdecl) = DECL_COMMON (olddecl); | |
92643fea | 3758 | COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl); |
8d08fdba | 3759 | |
8d08fdba MS |
3760 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
3761 | { | |
3762 | int function_size; | |
8d08fdba MS |
3763 | |
3764 | function_size = sizeof (struct tree_decl); | |
3765 | ||
730e1556 KG |
3766 | memcpy ((char *) olddecl + sizeof (struct tree_common), |
3767 | (char *) newdecl + sizeof (struct tree_common), | |
3768 | function_size - sizeof (struct tree_common)); | |
8d08fdba | 3769 | |
75650646 MM |
3770 | if (DECL_TEMPLATE_INSTANTIATION (newdecl)) |
3771 | { | |
3772 | /* If newdecl is a template instantiation, it is possible that | |
3773 | the following sequence of events has occurred: | |
3774 | ||
3775 | o A friend function was declared in a class template. The | |
68642fb6 | 3776 | class template was instantiated. |
75650646 | 3777 | |
68642fb6 UD |
3778 | o The instantiation of the friend declaration was |
3779 | recorded on the instantiation list, and is newdecl. | |
75650646 MM |
3780 | |
3781 | o Later, however, instantiate_class_template called pushdecl | |
3782 | on the newdecl to perform name injection. But, pushdecl in | |
3783 | turn called duplicate_decls when it discovered that another | |
3784 | declaration of a global function with the same name already | |
68642fb6 | 3785 | existed. |
75650646 MM |
3786 | |
3787 | o Here, in duplicate_decls, we decided to clobber newdecl. | |
3788 | ||
3789 | If we're going to do that, we'd better make sure that | |
3790 | olddecl, and not newdecl, is on the list of | |
3791 | instantiations so that if we try to do the instantiation | |
3792 | again we won't get the clobbered declaration. */ | |
3793 | ||
68642fb6 UD |
3794 | tree tmpl = DECL_TI_TEMPLATE (newdecl); |
3795 | tree decls = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); | |
75650646 MM |
3796 | |
3797 | for (; decls; decls = TREE_CHAIN (decls)) | |
3798 | if (TREE_VALUE (decls) == newdecl) | |
3799 | TREE_VALUE (decls) = olddecl; | |
3800 | } | |
8d08fdba MS |
3801 | } |
3802 | else | |
3803 | { | |
730e1556 KG |
3804 | memcpy ((char *) olddecl + sizeof (struct tree_common), |
3805 | (char *) newdecl + sizeof (struct tree_common), | |
3806 | sizeof (struct tree_decl) - sizeof (struct tree_common) | |
2f9834e8 | 3807 | + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *)); |
8d08fdba MS |
3808 | } |
3809 | ||
3810 | DECL_UID (olddecl) = olddecl_uid; | |
3811 | if (olddecl_friend) | |
3812 | DECL_FRIEND_P (olddecl) = 1; | |
3813 | ||
d9525bec BK |
3814 | /* NEWDECL contains the merged attribute lists. |
3815 | Update OLDDECL to be the same. */ | |
91d231cb | 3816 | DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl); |
d9525bec | 3817 | |
8d08fdba MS |
3818 | return 1; |
3819 | } | |
3820 | ||
3821 | /* Record a decl-node X as belonging to the current lexical scope. | |
3822 | Check for errors (such as an incompatible declaration for the same | |
3823 | name already seen in the same scope). | |
3824 | ||
3825 | Returns either X or an old decl for the same name. | |
3826 | If an old decl is returned, it may have been smashed | |
3827 | to agree with what X says. */ | |
3828 | ||
3829 | tree | |
3830 | pushdecl (x) | |
3831 | tree x; | |
3832 | { | |
3833 | register tree t; | |
b35d4555 MM |
3834 | register tree name; |
3835 | int need_new_binding; | |
3836 | ||
3837 | /* We shouldn't be calling pushdecl when we're generating RTL for a | |
3838 | function that we already did semantic analysis on previously. */ | |
01d939e8 | 3839 | my_friendly_assert (!cfun || doing_semantic_analysis_p (), |
b35d4555 MM |
3840 | 19990913); |
3841 | ||
b35d4555 | 3842 | need_new_binding = 1; |
8d08fdba | 3843 | |
50714e79 MM |
3844 | if (DECL_TEMPLATE_PARM_P (x)) |
3845 | /* Template parameters have no context; they are not X::T even | |
3846 | when declared within a class or namespace. */ | |
3847 | ; | |
3848 | else | |
3849 | { | |
3850 | if (current_function_decl && x != current_function_decl | |
3851 | /* A local declaration for a function doesn't constitute | |
3852 | nesting. */ | |
f39ee884 MM |
3853 | && !(TREE_CODE (x) == FUNCTION_DECL && !DECL_INITIAL (x)) |
3854 | /* A local declaration for an `extern' variable is in the | |
94dfccd1 | 3855 | scope of the current namespace, not the current |
f39ee884 MM |
3856 | function. */ |
3857 | && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x)) | |
50714e79 MM |
3858 | && !DECL_CONTEXT (x)) |
3859 | DECL_CONTEXT (x) = current_function_decl; | |
cd9f6678 MM |
3860 | |
3861 | /* If this is the declaration for a namespace-scope function, | |
3862 | but the declaration itself is in a local scope, mark the | |
3863 | declaration. */ | |
68642fb6 | 3864 | if (TREE_CODE (x) == FUNCTION_DECL |
cd9f6678 MM |
3865 | && DECL_NAMESPACE_SCOPE_P (x) |
3866 | && current_function_decl | |
3867 | && x != current_function_decl) | |
3868 | DECL_LOCAL_FUNCTION_P (x) = 1; | |
50714e79 | 3869 | } |
8d08fdba | 3870 | |
fe8fadc1 | 3871 | name = DECL_NAME (x); |
8d08fdba MS |
3872 | if (name) |
3873 | { | |
94dfccd1 JM |
3874 | int different_binding_level = 0; |
3875 | ||
386b8a85 JM |
3876 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) |
3877 | name = TREE_OPERAND (name, 0); | |
68642fb6 | 3878 | |
94dfccd1 JM |
3879 | /* In case this decl was explicitly namespace-qualified, look it |
3880 | up in its namespace context. */ | |
3881 | if (TREE_CODE (x) == VAR_DECL && DECL_NAMESPACE_SCOPE_P (x) | |
3882 | && namespace_bindings_p ()) | |
2c73f9f5 ML |
3883 | t = namespace_binding (name, DECL_CONTEXT (x)); |
3884 | else | |
3885 | t = lookup_name_current_level (name); | |
fe8fadc1 | 3886 | |
94dfccd1 JM |
3887 | /* [basic.link] If there is a visible declaration of an entity |
3888 | with linkage having the same name and type, ignoring entities | |
3889 | declared outside the innermost enclosing namespace scope, the | |
3890 | block scope declaration declares that same entity and | |
3891 | receives the linkage of the previous declaration. */ | |
3892 | if (! t && current_function_decl && x != current_function_decl | |
3893 | && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL) | |
3894 | && DECL_EXTERNAL (x)) | |
3895 | { | |
3896 | /* Look in block scope. */ | |
3897 | t = IDENTIFIER_VALUE (name); | |
3898 | /* Or in the innermost namespace. */ | |
3899 | if (! t) | |
3900 | t = namespace_binding (name, DECL_CONTEXT (x)); | |
fab09a24 JM |
3901 | /* Does it have linkage? Note that if this isn't a DECL, it's an |
3902 | OVERLOAD, which is OK. */ | |
3903 | if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t))) | |
94dfccd1 JM |
3904 | t = NULL_TREE; |
3905 | if (t) | |
3906 | different_binding_level = 1; | |
3907 | } | |
3908 | ||
fe8fadc1 MM |
3909 | /* If we are declaring a function, and the result of name-lookup |
3910 | was an OVERLOAD, look for an overloaded instance that is | |
3911 | actually the same as the function we are declaring. (If | |
3912 | there is one, we have to merge our declaration with the | |
3913 | previous declaration.) */ | |
655dc6ee | 3914 | if (t && TREE_CODE (t) == OVERLOAD) |
fe8fadc1 MM |
3915 | { |
3916 | tree match; | |
3917 | ||
655dc6ee JM |
3918 | if (TREE_CODE (x) == FUNCTION_DECL) |
3919 | for (match = t; match; match = OVL_NEXT (match)) | |
3920 | { | |
92643fea | 3921 | if (decls_match (OVL_CURRENT (match), x)) |
655dc6ee JM |
3922 | break; |
3923 | } | |
3924 | else | |
3925 | /* Just choose one. */ | |
3926 | match = t; | |
fe8fadc1 MM |
3927 | |
3928 | if (match) | |
3929 | t = OVL_CURRENT (match); | |
3930 | else | |
3931 | t = NULL_TREE; | |
3932 | } | |
3933 | ||
8d08fdba MS |
3934 | if (t == error_mark_node) |
3935 | { | |
3936 | /* error_mark_node is 0 for a while during initialization! */ | |
3937 | t = NULL_TREE; | |
8251199e | 3938 | cp_error_at ("`%#D' used prior to declaration", x); |
8d08fdba | 3939 | } |
51c184be | 3940 | else if (t != NULL_TREE) |
8d08fdba | 3941 | { |
94dfccd1 JM |
3942 | if (different_binding_level) |
3943 | { | |
3944 | if (decls_match (x, t)) | |
3945 | /* The standard only says that the local extern | |
3946 | inherits linkage from the previous decl; in | |
3947 | particular, default args are not shared. It would | |
3948 | be nice to propagate inlining info, though. FIXME. */ | |
3949 | TREE_PUBLIC (x) = TREE_PUBLIC (t); | |
3950 | } | |
3951 | else if (TREE_CODE (t) == PARM_DECL) | |
8d08fdba MS |
3952 | { |
3953 | if (DECL_CONTEXT (t) == NULL_TREE) | |
400500c4 RK |
3954 | /* This is probaby caused by too many errors, but calling |
3955 | abort will say that if errors have occurred. */ | |
3956 | abort (); | |
be99da77 | 3957 | |
e92cc029 | 3958 | /* Check for duplicate params. */ |
be99da77 MS |
3959 | if (duplicate_decls (x, t)) |
3960 | return t; | |
8d08fdba | 3961 | } |
eb68cb58 | 3962 | else if ((DECL_EXTERN_C_FUNCTION_P (x) |
5566b478 MS |
3963 | || DECL_FUNCTION_TEMPLATE_P (x)) |
3964 | && is_overloaded_fn (t)) | |
2c73f9f5 | 3965 | /* Don't do anything just yet. */; |
e1cd6e56 MS |
3966 | else if (t == wchar_decl_node) |
3967 | { | |
3968 | if (pedantic && ! DECL_IN_SYSTEM_HEADER (x)) | |
33bd39a2 | 3969 | pedwarn ("redeclaration of `wchar_t' as `%T'", |
2b0cbc5d | 3970 | TREE_TYPE (x)); |
e1cd6e56 MS |
3971 | |
3972 | /* Throw away the redeclaration. */ | |
3973 | return t; | |
3974 | } | |
8926095f | 3975 | else if (TREE_CODE (t) != TREE_CODE (x)) |
8d08fdba | 3976 | { |
9ed182dc | 3977 | if (duplicate_decls (x, t)) |
51c184be | 3978 | return t; |
8d08fdba MS |
3979 | } |
3980 | else if (duplicate_decls (x, t)) | |
51c184be | 3981 | { |
7177d104 MS |
3982 | if (TREE_CODE (t) == TYPE_DECL) |
3983 | SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t)); | |
f30432d7 MS |
3984 | else if (TREE_CODE (t) == FUNCTION_DECL) |
3985 | check_default_args (t); | |
7177d104 | 3986 | |
51c184be MS |
3987 | return t; |
3988 | } | |
35680744 MM |
3989 | else if (DECL_MAIN_P (x)) |
3990 | { | |
3991 | /* A redeclaration of main, but not a duplicate of the | |
68642fb6 | 3992 | previous one. |
35680744 MM |
3993 | |
3994 | [basic.start.main] | |
3995 | ||
3996 | This function shall not be overloaded. */ | |
8251199e | 3997 | cp_error_at ("invalid redeclaration of `%D'", t); |
33bd39a2 | 3998 | error ("as `%D'", x); |
35680744 MM |
3999 | /* We don't try to push this declaration since that |
4000 | causes a crash. */ | |
4001 | return x; | |
4002 | } | |
8d08fdba | 4003 | } |
8926095f | 4004 | |
f3400fe2 JM |
4005 | check_template_shadow (x); |
4006 | ||
fcfcdfc8 JM |
4007 | /* If this is a function conjured up by the backend, massage it |
4008 | so it looks friendly. */ | |
eb68cb58 | 4009 | if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x)) |
fcfcdfc8 JM |
4010 | { |
4011 | retrofit_lang_decl (x); | |
5d2ed28c | 4012 | SET_DECL_LANGUAGE (x, lang_c); |
fcfcdfc8 JM |
4013 | } |
4014 | ||
eb68cb58 | 4015 | if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x)) |
8926095f | 4016 | { |
7bdbfa05 | 4017 | t = push_overloaded_decl (x, PUSH_LOCAL); |
5fdaba89 | 4018 | if (t != x) |
8926095f | 4019 | return t; |
f181d4ae MM |
4020 | if (!namespace_bindings_p ()) |
4021 | /* We do not need to create a binding for this name; | |
4022 | push_overloaded_decl will have already done so if | |
4023 | necessary. */ | |
4024 | need_new_binding = 0; | |
8926095f | 4025 | } |
6eb3bb27 | 4026 | else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x)) |
cfe507be MM |
4027 | { |
4028 | t = push_overloaded_decl (x, PUSH_GLOBAL); | |
4029 | if (t == x) | |
4030 | add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t))); | |
4031 | return t; | |
4032 | } | |
8d08fdba | 4033 | |
a1774733 BK |
4034 | /* If declaring a type as a typedef, copy the type (unless we're |
4035 | at line 0), and install this TYPE_DECL as the new type's typedef | |
4036 | name. See the extensive comment in ../c-decl.c (pushdecl). */ | |
8d08fdba MS |
4037 | if (TREE_CODE (x) == TYPE_DECL) |
4038 | { | |
4039 | tree type = TREE_TYPE (x); | |
655dc6ee | 4040 | if (DECL_SOURCE_LINE (x) == 0) |
a1774733 BK |
4041 | { |
4042 | if (TYPE_NAME (type) == 0) | |
4043 | TYPE_NAME (type) = x; | |
4044 | } | |
1c80fb65 MM |
4045 | else if (type != error_mark_node && TYPE_NAME (type) != x |
4046 | /* We don't want to copy the type when all we're | |
4047 | doing is making a TYPE_DECL for the purposes of | |
4048 | inlining. */ | |
68642fb6 | 4049 | && (!TYPE_NAME (type) |
1c80fb65 | 4050 | || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x))) |
a1774733 BK |
4051 | { |
4052 | DECL_ORIGINAL_TYPE (x) = type; | |
4053 | type = build_type_copy (type); | |
4054 | TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x)); | |
4055 | TYPE_NAME (type) = x; | |
4056 | TREE_TYPE (x) = type; | |
4057 | } | |
8d08fdba | 4058 | |
8d08fdba MS |
4059 | if (type != error_mark_node |
4060 | && TYPE_NAME (type) | |
4061 | && TYPE_IDENTIFIER (type)) | |
68642fb6 | 4062 | set_identifier_type_value_with_scope (DECL_NAME (x), type, |
f181d4ae MM |
4063 | current_binding_level); |
4064 | ||
8d08fdba MS |
4065 | } |
4066 | ||
4067 | /* Multiple external decls of the same identifier ought to match. | |
4068 | ||
4069 | We get warnings about inline functions where they are defined. | |
39211cd5 | 4070 | We get warnings about other functions from push_overloaded_decl. |
68642fb6 | 4071 | |
8d08fdba | 4072 | Avoid duplicate warnings where they are used. */ |
39211cd5 | 4073 | if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL) |
8d08fdba MS |
4074 | { |
4075 | tree decl; | |
4076 | ||
f49fad00 JM |
4077 | decl = IDENTIFIER_NAMESPACE_VALUE (name); |
4078 | if (decl && TREE_CODE (decl) == OVERLOAD) | |
4079 | decl = OVL_FUNCTION (decl); | |
8d08fdba | 4080 | |
f49fad00 JM |
4081 | if (decl && decl != error_mark_node |
4082 | && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl)) | |
8d08fdba MS |
4083 | /* If different sort of thing, we already gave an error. */ |
4084 | && TREE_CODE (decl) == TREE_CODE (x) | |
3bfdc719 | 4085 | && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl))) |
8d08fdba | 4086 | { |
33bd39a2 | 4087 | pedwarn ("type mismatch with previous external decl", x); |
8251199e | 4088 | cp_pedwarn_at ("previous external decl of `%#D'", decl); |
8d08fdba MS |
4089 | } |
4090 | } | |
4091 | ||
8d08fdba MS |
4092 | /* This name is new in its binding level. |
4093 | Install the new declaration and return it. */ | |
2c73f9f5 | 4094 | if (namespace_bindings_p ()) |
8d08fdba MS |
4095 | { |
4096 | /* Install a global value. */ | |
4097 | ||
8d08fdba MS |
4098 | /* If the first global decl has external linkage, |
4099 | warn if we later see static one. */ | |
31928556 | 4100 | if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x)) |
8d08fdba | 4101 | TREE_PUBLIC (name) = 1; |
5362b086 | 4102 | |
92643fea MM |
4103 | /* Bind the name for the entity. */ |
4104 | if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x) | |
4105 | && t != NULL_TREE) | |
4106 | && (TREE_CODE (x) == TYPE_DECL | |
4107 | || TREE_CODE (x) == VAR_DECL | |
4108 | || TREE_CODE (x) == NAMESPACE_DECL | |
4109 | || TREE_CODE (x) == CONST_DECL | |
4110 | || TREE_CODE (x) == TEMPLATE_DECL)) | |
4111 | SET_IDENTIFIER_NAMESPACE_VALUE (name, x); | |
8d08fdba MS |
4112 | |
4113 | /* Don't forget if the function was used via an implicit decl. */ | |
4114 | if (IDENTIFIER_IMPLICIT_DECL (name) | |
4115 | && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name))) | |
4116 | TREE_USED (x) = 1; | |
4117 | ||
4118 | /* Don't forget if its address was taken in that way. */ | |
4119 | if (IDENTIFIER_IMPLICIT_DECL (name) | |
4120 | && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name))) | |
4121 | TREE_ADDRESSABLE (x) = 1; | |
4122 | ||
4123 | /* Warn about mismatches against previous implicit decl. */ | |
4124 | if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE | |
4125 | /* If this real decl matches the implicit, don't complain. */ | |
4126 | && ! (TREE_CODE (x) == FUNCTION_DECL | |
4127 | && TREE_TYPE (TREE_TYPE (x)) == integer_type_node)) | |
33bd39a2 | 4128 | warning |
8251199e | 4129 | ("`%D' was previously implicitly declared to return `int'", x); |
8d08fdba MS |
4130 | |
4131 | /* If new decl is `static' and an `extern' was seen previously, | |
4132 | warn about it. */ | |
a0a33927 MS |
4133 | if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t)) |
4134 | warn_extern_redeclared_static (x, t); | |
8d08fdba MS |
4135 | } |
4136 | else | |
4137 | { | |
4138 | /* Here to install a non-global value. */ | |
f181d4ae | 4139 | tree oldlocal = IDENTIFIER_VALUE (name); |
31928556 | 4140 | tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name); |
8d08fdba | 4141 | |
f181d4ae MM |
4142 | if (need_new_binding) |
4143 | { | |
0034cf72 | 4144 | push_local_binding (name, x, 0); |
f181d4ae MM |
4145 | /* Because push_local_binding will hook X on to the |
4146 | current_binding_level's name list, we don't want to | |
4147 | do that again below. */ | |
4148 | need_new_binding = 0; | |
4149 | } | |
8d08fdba MS |
4150 | |
4151 | /* If this is a TYPE_DECL, push it into the type value slot. */ | |
4152 | if (TREE_CODE (x) == TYPE_DECL) | |
68642fb6 | 4153 | set_identifier_type_value_with_scope (name, TREE_TYPE (x), |
f181d4ae | 4154 | current_binding_level); |
8d08fdba | 4155 | |
a9aedbc2 MS |
4156 | /* Clear out any TYPE_DECL shadowed by a namespace so that |
4157 | we won't think this is a type. The C struct hack doesn't | |
4158 | go through namespaces. */ | |
4159 | if (TREE_CODE (x) == NAMESPACE_DECL) | |
68642fb6 | 4160 | set_identifier_type_value_with_scope (name, NULL_TREE, |
f181d4ae | 4161 | current_binding_level); |
a9aedbc2 | 4162 | |
e905ac8a MS |
4163 | if (oldlocal) |
4164 | { | |
4165 | tree d = oldlocal; | |
17aec3eb | 4166 | |
74dc0d8c MS |
4167 | while (oldlocal |
4168 | && TREE_CODE (oldlocal) == VAR_DECL | |
4169 | && DECL_DEAD_FOR_LOCAL (oldlocal)) | |
17aec3eb RK |
4170 | oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal); |
4171 | ||
e905ac8a MS |
4172 | if (oldlocal == NULL_TREE) |
4173 | oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d)); | |
4174 | } | |
4175 | ||
8d08fdba MS |
4176 | /* If this is an extern function declaration, see if we |
4177 | have a global definition or declaration for the function. */ | |
4178 | if (oldlocal == NULL_TREE | |
faae18ab | 4179 | && DECL_EXTERNAL (x) |
31928556 | 4180 | && oldglobal != NULL_TREE |
8d08fdba | 4181 | && TREE_CODE (x) == FUNCTION_DECL |
31928556 | 4182 | && TREE_CODE (oldglobal) == FUNCTION_DECL) |
8d08fdba MS |
4183 | { |
4184 | /* We have one. Their types must agree. */ | |
31928556 | 4185 | if (decls_match (x, oldglobal)) |
6060a796 MS |
4186 | /* OK */; |
4187 | else | |
8d08fdba | 4188 | { |
33bd39a2 | 4189 | warning ("extern declaration of `%#D' doesn't match", x); |
8251199e | 4190 | cp_warning_at ("global declaration `%#D'", oldglobal); |
8d08fdba | 4191 | } |
8d08fdba MS |
4192 | } |
4193 | /* If we have a local external declaration, | |
4194 | and no file-scope declaration has yet been seen, | |
4195 | then if we later have a file-scope decl it must not be static. */ | |
4196 | if (oldlocal == NULL_TREE | |
31928556 | 4197 | && oldglobal == NULL_TREE |
8d08fdba MS |
4198 | && DECL_EXTERNAL (x) |
4199 | && TREE_PUBLIC (x)) | |
f181d4ae | 4200 | TREE_PUBLIC (name) = 1; |
8d08fdba | 4201 | |
8d08fdba | 4202 | /* Warn if shadowing an argument at the top level of the body. */ |
e905ac8a MS |
4203 | if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x) |
4204 | /* Inline decls shadow nothing. */ | |
4205 | && !DECL_FROM_INLINE (x) | |
8ac9ea61 | 4206 | && TREE_CODE (oldlocal) == PARM_DECL) |
8d08fdba | 4207 | { |
8ac9ea61 | 4208 | bool err = false; |
8d08fdba | 4209 | |
8ac9ea61 NB |
4210 | /* Don't complain if it's from an enclosing function. */ |
4211 | if (DECL_CONTEXT (oldlocal) == current_function_decl | |
4212 | && TREE_CODE (x) != PARM_DECL) | |
4213 | { | |
4214 | /* Go to where the parms should be and see if we find | |
4215 | them there. */ | |
4216 | struct binding_level *b = current_binding_level->level_chain; | |
4217 | ||
8ac9ea61 NB |
4218 | /* ARM $8.3 */ |
4219 | if (b->parm_flag == 1) | |
4220 | { | |
33bd39a2 | 4221 | error ("declaration of `%#D' shadows a parameter", |
8ac9ea61 NB |
4222 | name); |
4223 | err = true; | |
4224 | } | |
4225 | } | |
8d08fdba | 4226 | |
8ac9ea61 NB |
4227 | if (warn_shadow && !err) |
4228 | shadow_warning ("a parameter", name, oldlocal); | |
8d08fdba | 4229 | } |
e905ac8a | 4230 | |
8d08fdba | 4231 | /* Maybe warn if shadowing something else. */ |
8ac9ea61 | 4232 | else if (warn_shadow && !DECL_EXTERNAL (x) |
e905ac8a MS |
4233 | /* No shadow warnings for internally generated vars. */ |
4234 | && ! DECL_ARTIFICIAL (x) | |
4235 | /* No shadow warnings for vars made for inlining. */ | |
4236 | && ! DECL_FROM_INLINE (x)) | |
8d08fdba | 4237 | { |
8ac9ea61 | 4238 | if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE |
4ac14744 | 4239 | && current_class_ptr |
8d08fdba | 4240 | && !TREE_STATIC (name)) |
33bd39a2 | 4241 | warning ("declaration of `%s' shadows a member of `this'", |
8ac9ea61 NB |
4242 | IDENTIFIER_POINTER (name)); |
4243 | else if (oldlocal != NULL_TREE | |
4244 | && TREE_CODE (oldlocal) == VAR_DECL) | |
4245 | shadow_warning ("a previous local", name, oldlocal); | |
4246 | else if (oldglobal != NULL_TREE | |
4247 | && TREE_CODE (oldglobal) == VAR_DECL) | |
30394414 | 4248 | /* XXX shadow warnings in outer-more namespaces */ |
8ac9ea61 | 4249 | shadow_warning ("a global declaration", name, oldglobal); |
8d08fdba | 4250 | } |
e1cd6e56 | 4251 | } |
8d08fdba | 4252 | |
e1cd6e56 | 4253 | if (TREE_CODE (x) == FUNCTION_DECL) |
f30432d7 | 4254 | check_default_args (x); |
8145f082 | 4255 | |
0154eaa8 MM |
4256 | if (TREE_CODE (x) == VAR_DECL) |
4257 | maybe_register_incomplete_var (x); | |
8d08fdba MS |
4258 | } |
4259 | ||
f181d4ae | 4260 | if (need_new_binding) |
68642fb6 | 4261 | add_decl_to_level (x, |
efee38a9 MM |
4262 | DECL_NAMESPACE_SCOPE_P (x) |
4263 | ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x)) | |
4264 | : current_binding_level); | |
8d08fdba MS |
4265 | |
4266 | return x; | |
4267 | } | |
4268 | ||
5566b478 MS |
4269 | /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the |
4270 | caller to set DECL_CONTEXT properly. */ | |
8d08fdba MS |
4271 | |
4272 | static tree | |
4273 | pushdecl_with_scope (x, level) | |
4274 | tree x; | |
4275 | struct binding_level *level; | |
4276 | { | |
8d019cef | 4277 | register struct binding_level *b; |
5566b478 | 4278 | tree function_decl = current_function_decl; |
8d08fdba | 4279 | |
5566b478 | 4280 | current_function_decl = NULL_TREE; |
8d019cef JM |
4281 | if (level->parm_flag == 2) |
4282 | { | |
4283 | b = class_binding_level; | |
4284 | class_binding_level = level; | |
4285 | pushdecl_class_level (x); | |
4286 | class_binding_level = b; | |
4287 | } | |
4288 | else | |
4289 | { | |
4290 | b = current_binding_level; | |
4291 | current_binding_level = level; | |
4292 | x = pushdecl (x); | |
4293 | current_binding_level = b; | |
4294 | } | |
5566b478 | 4295 | current_function_decl = function_decl; |
8d08fdba MS |
4296 | return x; |
4297 | } | |
4298 | ||
2c73f9f5 | 4299 | /* Like pushdecl, only it places X in the current namespace, |
8d08fdba | 4300 | if appropriate. */ |
e92cc029 | 4301 | |
8d08fdba | 4302 | tree |
2c73f9f5 | 4303 | pushdecl_namespace_level (x) |
8d08fdba MS |
4304 | tree x; |
4305 | { | |
8f032717 | 4306 | register struct binding_level *b = current_binding_level; |
2c73f9f5 ML |
4307 | register tree t; |
4308 | ||
4309 | t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace)); | |
8d08fdba MS |
4310 | |
4311 | /* Now, the type_shadowed stack may screw us. Munge it so it does | |
4312 | what we want. */ | |
4313 | if (TREE_CODE (x) == TYPE_DECL) | |
4314 | { | |
4315 | tree name = DECL_NAME (x); | |
4316 | tree newval; | |
4317 | tree *ptr = (tree *)0; | |
4318 | for (; b != global_binding_level; b = b->level_chain) | |
4319 | { | |
4320 | tree shadowed = b->type_shadowed; | |
4321 | for (; shadowed; shadowed = TREE_CHAIN (shadowed)) | |
4322 | if (TREE_PURPOSE (shadowed) == name) | |
4323 | { | |
4324 | ptr = &TREE_VALUE (shadowed); | |
4325 | /* Can't break out of the loop here because sometimes | |
4326 | a binding level will have duplicate bindings for | |
4327 | PT names. It's gross, but I haven't time to fix it. */ | |
4328 | } | |
4329 | } | |
4330 | newval = TREE_TYPE (x); | |
4331 | if (ptr == (tree *)0) | |
4332 | { | |
4333 | /* @@ This shouldn't be needed. My test case "zstring.cc" trips | |
4334 | up here if this is changed to an assertion. --KR */ | |
4335 | SET_IDENTIFIER_TYPE_VALUE (name, newval); | |
4336 | } | |
4337 | else | |
4338 | { | |
8d08fdba MS |
4339 | *ptr = newval; |
4340 | } | |
4341 | } | |
4342 | return t; | |
4343 | } | |
4344 | ||
2c73f9f5 ML |
4345 | /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL, |
4346 | if appropriate. */ | |
4347 | ||
4348 | tree | |
4349 | pushdecl_top_level (x) | |
4350 | tree x; | |
4351 | { | |
b35d4555 | 4352 | push_to_top_level (); |
2c73f9f5 | 4353 | x = pushdecl_namespace_level (x); |
b35d4555 | 4354 | pop_from_top_level (); |
2c73f9f5 ML |
4355 | return x; |
4356 | } | |
4357 | ||
8d08fdba | 4358 | /* Make the declaration of X appear in CLASS scope. */ |
e92cc029 | 4359 | |
61a127b3 | 4360 | void |
8d08fdba MS |
4361 | pushdecl_class_level (x) |
4362 | tree x; | |
4363 | { | |
4364 | /* Don't use DECL_ASSEMBLER_NAME here! Everything that looks in class | |
4365 | scope looks for the pre-mangled name. */ | |
8f032717 MM |
4366 | register tree name; |
4367 | ||
4368 | if (TREE_CODE (x) == OVERLOAD) | |
4369 | x = OVL_CURRENT (x); | |
4370 | name = DECL_NAME (x); | |
8d08fdba MS |
4371 | |
4372 | if (name) | |
4373 | { | |
4374 | push_class_level_binding (name, x); | |
4375 | if (TREE_CODE (x) == TYPE_DECL) | |
f181d4ae | 4376 | set_identifier_type_value (name, TREE_TYPE (x)); |
8d08fdba | 4377 | } |
6bdb8141 | 4378 | else if (ANON_AGGR_TYPE_P (TREE_TYPE (x))) |
8f032717 MM |
4379 | { |
4380 | tree f; | |
4381 | ||
4382 | for (f = TYPE_FIELDS (TREE_TYPE (x)); | |
4383 | f; | |
4384 | f = TREE_CHAIN (f)) | |
4385 | pushdecl_class_level (f); | |
4386 | } | |
8d08fdba MS |
4387 | } |
4388 | ||
9188c363 MM |
4389 | /* Enter DECL into the symbol table, if that's appropriate. Returns |
4390 | DECL, or a modified version thereof. */ | |
4391 | ||
4392 | tree | |
4393 | maybe_push_decl (decl) | |
4394 | tree decl; | |
4395 | { | |
4396 | tree type = TREE_TYPE (decl); | |
4397 | ||
4398 | /* Add this decl to the current binding level, but not if it comes | |
4399 | from another scope, e.g. a static member variable. TEM may equal | |
4400 | DECL or it may be a previous decl of the same name. */ | |
07c88314 MM |
4401 | if (decl == error_mark_node |
4402 | || (TREE_CODE (decl) != PARM_DECL | |
4403 | && DECL_CONTEXT (decl) != NULL_TREE | |
4404 | /* Definitions of namespace members outside their namespace are | |
4405 | possible. */ | |
4406 | && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL) | |
9188c363 MM |
4407 | || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ()) |
4408 | || TREE_CODE (type) == UNKNOWN_TYPE | |
558475f0 | 4409 | /* The declaration of a template specialization does not affect |
9188c363 MM |
4410 | the functions available for overload resolution, so we do not |
4411 | call pushdecl. */ | |
4412 | || (TREE_CODE (decl) == FUNCTION_DECL | |
4413 | && DECL_TEMPLATE_SPECIALIZATION (decl))) | |
4414 | return decl; | |
4415 | else | |
4416 | return pushdecl (decl); | |
4417 | } | |
4418 | ||
8d08fdba MS |
4419 | /* Make the declaration(s) of X appear in CLASS scope |
4420 | under the name NAME. */ | |
e92cc029 | 4421 | |
8d08fdba MS |
4422 | void |
4423 | push_class_level_binding (name, x) | |
4424 | tree name; | |
4425 | tree x; | |
4426 | { | |
8f032717 | 4427 | tree binding; |
68642fb6 | 4428 | /* The class_binding_level will be NULL if x is a template |
98c1c668 JM |
4429 | parameter name in a member template. */ |
4430 | if (!class_binding_level) | |
4431 | return; | |
4432 | ||
908c4e83 MM |
4433 | /* Make sure that this new member does not have the same name |
4434 | as a template parameter. */ | |
4435 | if (TYPE_BEING_DEFINED (current_class_type)) | |
4436 | check_template_shadow (x); | |
4437 | ||
f181d4ae MM |
4438 | /* If this declaration shadows a declaration from an enclosing |
4439 | class, then we will need to restore IDENTIFIER_CLASS_VALUE when | |
4440 | we leave this class. Record the shadowed declaration here. */ | |
8f032717 | 4441 | binding = IDENTIFIER_BINDING (name); |
68642fb6 | 4442 | if (binding |
8f032717 MM |
4443 | && ((TREE_CODE (x) == OVERLOAD |
4444 | && BINDING_VALUE (binding) | |
4445 | && is_overloaded_fn (BINDING_VALUE (binding))) | |
4446 | || INHERITED_VALUE_BINDING_P (binding))) | |
4447 | { | |
4448 | tree shadow; | |
4449 | tree old_decl; | |
4450 | ||
4451 | /* If the old binding was from a base class, and was for a tag | |
4452 | name, slide it over to make room for the new binding. The | |
4453 | old binding is still visible if explicitly qualified with a | |
4454 | class-key. */ | |
4455 | if (INHERITED_VALUE_BINDING_P (binding) | |
4456 | && BINDING_VALUE (binding) | |
4457 | && TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL | |
4458 | && DECL_ARTIFICIAL (BINDING_VALUE (binding)) | |
4459 | && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x))) | |
4460 | { | |
4461 | old_decl = BINDING_TYPE (binding); | |
4462 | BINDING_TYPE (binding) = BINDING_VALUE (binding); | |
4463 | BINDING_VALUE (binding) = NULL_TREE; | |
4464 | INHERITED_VALUE_BINDING_P (binding) = 0; | |
4465 | } | |
4466 | else | |
4467 | old_decl = BINDING_VALUE (binding); | |
4468 | ||
76191fdd NS |
4469 | /* Find the previous binding of name on the class-shadowed |
4470 | list, and update it. */ | |
8f032717 MM |
4471 | for (shadow = class_binding_level->class_shadowed; |
4472 | shadow; | |
4473 | shadow = TREE_CHAIN (shadow)) | |
4474 | if (TREE_PURPOSE (shadow) == name | |
4475 | && TREE_TYPE (shadow) == old_decl) | |
4476 | { | |
4477 | BINDING_VALUE (binding) = x; | |
4478 | INHERITED_VALUE_BINDING_P (binding) = 0; | |
4479 | TREE_TYPE (shadow) = x; | |
76191fdd | 4480 | IDENTIFIER_CLASS_VALUE (name) = x; |
8f032717 MM |
4481 | return; |
4482 | } | |
4483 | } | |
f181d4ae | 4484 | |
8f032717 | 4485 | /* If we didn't replace an existing binding, put the binding on the |
76191fdd | 4486 | stack of bindings for the identifier, and update the shadowed list. */ |
8f032717 MM |
4487 | if (push_class_binding (name, x)) |
4488 | { | |
8f032717 | 4489 | class_binding_level->class_shadowed |
76191fdd | 4490 | = tree_cons (name, NULL, |
8f032717 | 4491 | class_binding_level->class_shadowed); |
8f032717 MM |
4492 | /* Record the value we are binding NAME to so that we can know |
4493 | what to pop later. */ | |
4494 | TREE_TYPE (class_binding_level->class_shadowed) = x; | |
4495 | } | |
8d08fdba MS |
4496 | } |
4497 | ||
dd4fae80 ML |
4498 | /* Insert another USING_DECL into the current binding level, returning |
4499 | this declaration. If this is a redeclaration, do nothing, and | |
4500 | return NULL_TREE if this not in namespace scope (in namespace | |
4501 | scope, a using decl might extend any previous bindings). */ | |
e92cc029 | 4502 | |
2c73f9f5 ML |
4503 | tree |
4504 | push_using_decl (scope, name) | |
4505 | tree scope; | |
4506 | tree name; | |
8d08fdba | 4507 | { |
2c73f9f5 | 4508 | tree decl; |
68642fb6 | 4509 | |
2c73f9f5 ML |
4510 | my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383); |
4511 | my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384); | |
4512 | for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl)) | |
4513 | if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name) | |
4514 | break; | |
4515 | if (decl) | |
dd4fae80 | 4516 | return namespace_bindings_p () ? decl : NULL_TREE; |
4ce3d537 | 4517 | decl = build_lang_decl (USING_DECL, name, void_type_node); |
2c73f9f5 ML |
4518 | DECL_INITIAL (decl) = scope; |
4519 | TREE_CHAIN (decl) = current_binding_level->usings; | |
4520 | current_binding_level->usings = decl; | |
4521 | return decl; | |
8d08fdba MS |
4522 | } |
4523 | ||
ea9635c7 ML |
4524 | /* Add namespace to using_directives. Return NULL_TREE if nothing was |
4525 | changed (i.e. there was already a directive), or the fresh | |
4526 | TREE_LIST otherwise. */ | |
4527 | ||
4528 | tree | |
9ed182dc | 4529 | push_using_directive (used) |
ea9635c7 | 4530 | tree used; |
ea9635c7 ML |
4531 | { |
4532 | tree ud = current_binding_level->using_directives; | |
9ed182dc | 4533 | tree iter, ancestor; |
68642fb6 | 4534 | |
ea9635c7 ML |
4535 | /* Check if we already have this. */ |
4536 | if (purpose_member (used, ud) != NULL_TREE) | |
4537 | return NULL_TREE; | |
00dc6358 JM |
4538 | |
4539 | /* Recursively add all namespaces used. */ | |
4540 | for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter)) | |
9ed182dc | 4541 | push_using_directive (TREE_PURPOSE (iter)); |
00dc6358 | 4542 | |
9ed182dc | 4543 | ancestor = namespace_ancestor (current_decl_namespace (), used); |
00dc6358 | 4544 | ud = current_binding_level->using_directives; |
e1b3e07d | 4545 | ud = tree_cons (used, ancestor, ud); |
ea9635c7 ML |
4546 | current_binding_level->using_directives = ud; |
4547 | return ud; | |
4548 | } | |
4549 | ||
f181d4ae MM |
4550 | /* DECL is a FUNCTION_DECL for a non-member function, which may have |
4551 | other definitions already in place. We get around this by making | |
4552 | the value of the identifier point to a list of all the things that | |
4553 | want to be referenced by that name. It is then up to the users of | |
4554 | that name to decide what to do with that list. | |
8d08fdba | 4555 | |
17aec3eb RK |
4556 | DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its |
4557 | DECL_TEMPLATE_RESULT. It is dealt with the same way. | |
8d08fdba | 4558 | |
7bdbfa05 MM |
4559 | FLAGS is a bitwise-or of the following values: |
4560 | PUSH_LOCAL: Bind DECL in the current scope, rather than at | |
4561 | namespace scope. | |
4562 | PUSH_USING: DECL is being pushed as the result of a using | |
68642fb6 | 4563 | declaration. |
7bdbfa05 | 4564 | |
8d08fdba MS |
4565 | The value returned may be a previous declaration if we guessed wrong |
4566 | about what language DECL should belong to (C or C++). Otherwise, | |
4567 | it's always DECL (and never something that's not a _DECL). */ | |
e92cc029 | 4568 | |
7bdbfa05 MM |
4569 | tree |
4570 | push_overloaded_decl (decl, flags) | |
8d08fdba | 4571 | tree decl; |
7bdbfa05 | 4572 | int flags; |
8d08fdba | 4573 | { |
f181d4ae | 4574 | tree name = DECL_NAME (decl); |
700f8a87 | 4575 | tree old; |
f181d4ae | 4576 | tree new_binding; |
7bdbfa05 | 4577 | int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL)); |
700f8a87 MS |
4578 | |
4579 | if (doing_global) | |
9f33663b | 4580 | old = namespace_binding (name, DECL_CONTEXT (decl)); |
700f8a87 | 4581 | else |
f181d4ae | 4582 | old = lookup_name_current_level (name); |
8d08fdba | 4583 | |
700f8a87 | 4584 | if (old) |
8d08fdba | 4585 | { |
e1cd6e56 | 4586 | if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old)) |
8926095f | 4587 | { |
700f8a87 | 4588 | tree t = TREE_TYPE (old); |
cdf5b885 MS |
4589 | if (IS_AGGR_TYPE (t) && warn_shadow |
4590 | && (! DECL_IN_SYSTEM_HEADER (decl) | |
4591 | || ! DECL_IN_SYSTEM_HEADER (old))) | |
33bd39a2 | 4592 | warning ("`%#D' hides constructor for `%#T'", decl, t); |
5b605f68 | 4593 | old = NULL_TREE; |
8926095f | 4594 | } |
700f8a87 | 4595 | else if (is_overloaded_fn (old)) |
8d08fdba | 4596 | { |
8d08fdba | 4597 | tree tmp; |
68642fb6 | 4598 | |
2c73f9f5 | 4599 | for (tmp = old; tmp; tmp = OVL_NEXT (tmp)) |
7bdbfa05 MM |
4600 | { |
4601 | tree fn = OVL_CURRENT (tmp); | |
4602 | ||
4603 | if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp) | |
4604 | && !(flags & PUSH_USING) | |
4605 | && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)), | |
4606 | TYPE_ARG_TYPES (TREE_TYPE (decl)))) | |
33bd39a2 | 4607 | error ("`%#D' conflicts with previous using declaration `%#D'", |
7bdbfa05 | 4608 | decl, fn); |
68642fb6 | 4609 | |
7bdbfa05 MM |
4610 | if (duplicate_decls (decl, fn)) |
4611 | return fn; | |
4612 | } | |
8d08fdba | 4613 | } |
655dc6ee JM |
4614 | else if (old == error_mark_node) |
4615 | /* Ignore the undefined symbol marker. */ | |
4616 | old = NULL_TREE; | |
e1cd6e56 MS |
4617 | else |
4618 | { | |
8251199e | 4619 | cp_error_at ("previous non-function declaration `%#D'", old); |
33bd39a2 | 4620 | error ("conflicts with function declaration `%#D'", decl); |
a9aedbc2 | 4621 | return decl; |
e1cd6e56 | 4622 | } |
8d08fdba | 4623 | } |
7177d104 | 4624 | |
700f8a87 | 4625 | if (old || TREE_CODE (decl) == TEMPLATE_DECL) |
8d08fdba | 4626 | { |
2c73f9f5 | 4627 | if (old && TREE_CODE (old) != OVERLOAD) |
f181d4ae MM |
4628 | new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE)); |
4629 | else | |
4630 | new_binding = ovl_cons (decl, old); | |
347d73d7 ML |
4631 | if (flags & PUSH_USING) |
4632 | OVL_USED (new_binding) = 1; | |
8d08fdba MS |
4633 | } |
4634 | else | |
f181d4ae MM |
4635 | /* NAME is not ambiguous. */ |
4636 | new_binding = decl; | |
700f8a87 MS |
4637 | |
4638 | if (doing_global) | |
f181d4ae | 4639 | set_namespace_binding (name, current_namespace, new_binding); |
700f8a87 | 4640 | else |
f181d4ae MM |
4641 | { |
4642 | /* We only create an OVERLOAD if there was a previous binding at | |
0580c9aa ML |
4643 | this level, or if decl is a template. In the former case, we |
4644 | need to remove the old binding and replace it with the new | |
4645 | binding. We must also run through the NAMES on the binding | |
4646 | level where the name was bound to update the chain. */ | |
4647 | ||
4648 | if (TREE_CODE (new_binding) == OVERLOAD && old) | |
f181d4ae MM |
4649 | { |
4650 | tree *d; | |
68642fb6 | 4651 | |
f181d4ae MM |
4652 | for (d = &BINDING_LEVEL (IDENTIFIER_BINDING (name))->names; |
4653 | *d; | |
4654 | d = &TREE_CHAIN (*d)) | |
4655 | if (*d == old | |
4656 | || (TREE_CODE (*d) == TREE_LIST | |
4657 | && TREE_VALUE (*d) == old)) | |
4658 | { | |
d8f8dca1 MM |
4659 | if (TREE_CODE (*d) == TREE_LIST) |
4660 | /* Just replace the old binding with the new. */ | |
4661 | TREE_VALUE (*d) = new_binding; | |
4662 | else | |
4663 | /* Build a TREE_LIST to wrap the OVERLOAD. */ | |
68642fb6 | 4664 | *d = tree_cons (NULL_TREE, new_binding, |
ed5511d9 | 4665 | TREE_CHAIN (*d)); |
d8f8dca1 MM |
4666 | |
4667 | /* And update the CPLUS_BINDING node. */ | |
4668 | BINDING_VALUE (IDENTIFIER_BINDING (name)) | |
4669 | = new_binding; | |
4670 | return decl; | |
f181d4ae MM |
4671 | } |
4672 | ||
d8f8dca1 | 4673 | /* We should always find a previous binding in this case. */ |
a98facb0 | 4674 | abort (); |
f181d4ae MM |
4675 | } |
4676 | ||
4677 | /* Install the new binding. */ | |
0034cf72 | 4678 | push_local_binding (name, new_binding, flags); |
f181d4ae | 4679 | } |
700f8a87 | 4680 | |
8d08fdba MS |
4681 | return decl; |
4682 | } | |
4683 | \f | |
4684 | /* Generate an implicit declaration for identifier FUNCTIONID | |
4685 | as a function of type int (). Print a warning if appropriate. */ | |
4686 | ||
4687 | tree | |
4688 | implicitly_declare (functionid) | |
4689 | tree functionid; | |
4690 | { | |
4691 | register tree decl; | |
8d08fdba MS |
4692 | |
4693 | /* We used to reuse an old implicit decl here, | |
4694 | but this loses with inline functions because it can clobber | |
4695 | the saved decl chains. */ | |
4696 | decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type); | |
4697 | ||
4698 | DECL_EXTERNAL (decl) = 1; | |
4699 | TREE_PUBLIC (decl) = 1; | |
4700 | ||
cab1f180 | 4701 | /* ISO standard says implicit declarations are in the innermost block. |
d22c8596 | 4702 | So we record the decl in the standard fashion. */ |
8d08fdba | 4703 | pushdecl (decl); |
3e411c3f | 4704 | rest_of_decl_compilation (decl, NULL, 0, 0); |
8d08fdba MS |
4705 | |
4706 | if (warn_implicit | |
4707 | /* Only one warning per identifier. */ | |
4708 | && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE) | |
4709 | { | |
33bd39a2 | 4710 | pedwarn ("implicit declaration of function `%#D'", decl); |
8d08fdba MS |
4711 | } |
4712 | ||
4713 | SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl); | |
4714 | ||
8d08fdba MS |
4715 | return decl; |
4716 | } | |
4717 | ||
8251199e | 4718 | /* Return zero if the declaration NEWDECL is valid |
8d08fdba MS |
4719 | when the declaration OLDDECL (assumed to be for the same name) |
4720 | has already been seen. | |
4721 | Otherwise return an error message format string with a %s | |
4722 | where the identifier should go. */ | |
4723 | ||
d8e178a0 | 4724 | static const char * |
8d08fdba MS |
4725 | redeclaration_error_message (newdecl, olddecl) |
4726 | tree newdecl, olddecl; | |
4727 | { | |
4728 | if (TREE_CODE (newdecl) == TYPE_DECL) | |
4729 | { | |
4730 | /* Because C++ can put things into name space for free, | |
4731 | constructs like "typedef struct foo { ... } foo" | |
4732 | would look like an erroneous redeclaration. */ | |
3bfdc719 | 4733 | if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl))) |
8251199e | 4734 | return 0; |
8d08fdba | 4735 | else |
8251199e | 4736 | return "redefinition of `%#D'"; |
8d08fdba MS |
4737 | } |
4738 | else if (TREE_CODE (newdecl) == FUNCTION_DECL) | |
4739 | { | |
4740 | /* If this is a pure function, its olddecl will actually be | |
4741 | the original initialization to `0' (which we force to call | |
4742 | abort()). Don't complain about redefinition in this case. */ | |
fee7654e | 4743 | if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)) |
8251199e | 4744 | return 0; |
8d08fdba | 4745 | |
2c73f9f5 ML |
4746 | /* If both functions come from different namespaces, this is not |
4747 | a redeclaration - this is a conflict with a used function. */ | |
6eb3bb27 | 4748 | if (DECL_NAMESPACE_SCOPE_P (olddecl) |
2c73f9f5 | 4749 | && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)) |
8251199e | 4750 | return "`%D' conflicts with used function"; |
2c73f9f5 | 4751 | |
db5ae43f MS |
4752 | /* We'll complain about linkage mismatches in |
4753 | warn_extern_redeclared_static. */ | |
4754 | ||
2c73f9f5 | 4755 | /* Defining the same name twice is no good. */ |
8d08fdba | 4756 | if (DECL_INITIAL (olddecl) != NULL_TREE |
faae18ab | 4757 | && DECL_INITIAL (newdecl) != NULL_TREE) |
8d08fdba MS |
4758 | { |
4759 | if (DECL_NAME (olddecl) == NULL_TREE) | |
8251199e | 4760 | return "`%#D' not declared in class"; |
8d08fdba | 4761 | else |
8251199e | 4762 | return "redefinition of `%#D'"; |
8d08fdba | 4763 | } |
8251199e | 4764 | return 0; |
8d08fdba | 4765 | } |
51c184be MS |
4766 | else if (TREE_CODE (newdecl) == TEMPLATE_DECL) |
4767 | { | |
ec255269 | 4768 | if ((TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL |
5362b086 | 4769 | && (DECL_TEMPLATE_RESULT (newdecl) |
f4381d62 | 4770 | != DECL_TEMPLATE_RESULT (olddecl)) |
ec255269 MS |
4771 | && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl)) |
4772 | && DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl))) | |
4773 | || (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL | |
d0f062fb NS |
4774 | && COMPLETE_TYPE_P (TREE_TYPE (newdecl)) |
4775 | && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))) | |
8251199e JM |
4776 | return "redefinition of `%#D'"; |
4777 | return 0; | |
51c184be | 4778 | } |
1f51a992 | 4779 | else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl)) |
8d08fdba MS |
4780 | { |
4781 | /* Objects declared at top level: */ | |
4782 | /* If at least one is a reference, it's ok. */ | |
4783 | if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl)) | |
8251199e | 4784 | return 0; |
8926095f | 4785 | /* Reject two definitions. */ |
8251199e | 4786 | return "redefinition of `%#D'"; |
8d08fdba MS |
4787 | } |
4788 | else | |
4789 | { | |
4790 | /* Objects declared with block scope: */ | |
4791 | /* Reject two definitions, and reject a definition | |
4792 | together with an external reference. */ | |
4793 | if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl))) | |
8251199e JM |
4794 | return "redeclaration of `%#D'"; |
4795 | return 0; | |
8d08fdba MS |
4796 | } |
4797 | } | |
4798 | \f | |
acef433b | 4799 | /* Create a new label, named ID. */ |
8d08fdba | 4800 | |
acef433b MM |
4801 | static tree |
4802 | make_label_decl (id, local_p) | |
8d08fdba | 4803 | tree id; |
acef433b | 4804 | int local_p; |
8d08fdba | 4805 | { |
acef433b | 4806 | tree decl; |
8d08fdba | 4807 | |
acef433b | 4808 | decl = build_decl (LABEL_DECL, id, void_type_node); |
b35d4555 | 4809 | if (expanding_p) |
acef433b MM |
4810 | /* Make sure every label has an rtx. */ |
4811 | label_rtx (decl); | |
4812 | ||
4813 | DECL_CONTEXT (decl) = current_function_decl; | |
4814 | DECL_MODE (decl) = VOIDmode; | |
4815 | C_DECLARED_LABEL_FLAG (decl) = local_p; | |
8d08fdba | 4816 | |
acef433b MM |
4817 | /* Say where one reference is to the label, for the sake of the |
4818 | error if it is not defined. */ | |
4819 | DECL_SOURCE_LINE (decl) = lineno; | |
4820 | DECL_SOURCE_FILE (decl) = input_filename; | |
4821 | ||
4822 | /* Record the fact that this identifier is bound to this label. */ | |
4823 | SET_IDENTIFIER_LABEL_VALUE (id, decl); | |
4824 | ||
6625cdb5 JM |
4825 | return decl; |
4826 | } | |
4827 | ||
4828 | /* Record this label on the list of used labels so that we can check | |
4829 | at the end of the function to see whether or not the label was | |
4830 | actually defined, and so we can check when the label is defined whether | |
4831 | this use is valid. */ | |
4832 | ||
4833 | static void | |
4834 | use_label (decl) | |
4835 | tree decl; | |
4836 | { | |
4837 | if (named_label_uses == NULL | |
4838 | || named_label_uses->names_in_scope != current_binding_level->names | |
4839 | || named_label_uses->label_decl != decl) | |
4840 | { | |
4841 | struct named_label_use_list *new_ent; | |
4842 | new_ent = ((struct named_label_use_list *) | |
4843 | ggc_alloc (sizeof (struct named_label_use_list))); | |
e349ee73 MS |
4844 | new_ent->label_decl = decl; |
4845 | new_ent->names_in_scope = current_binding_level->names; | |
4846 | new_ent->binding_level = current_binding_level; | |
4847 | new_ent->lineno_o_goto = lineno; | |
4848 | new_ent->filename_o_goto = input_filename; | |
4849 | new_ent->next = named_label_uses; | |
4850 | named_label_uses = new_ent; | |
8d08fdba | 4851 | } |
acef433b | 4852 | } |
8d08fdba | 4853 | |
acef433b MM |
4854 | /* Look for a label named ID in the current function. If one cannot |
4855 | be found, create one. (We keep track of used, but undefined, | |
4856 | labels, and complain about them at the end of a function.) */ | |
8d08fdba | 4857 | |
68642fb6 | 4858 | tree |
acef433b MM |
4859 | lookup_label (id) |
4860 | tree id; | |
4861 | { | |
4862 | tree decl; | |
6625cdb5 | 4863 | struct named_label_list *ent; |
8d08fdba | 4864 | |
acef433b MM |
4865 | /* You can't use labels at global scope. */ |
4866 | if (current_function_decl == NULL_TREE) | |
4867 | { | |
4868 | error ("label `%s' referenced outside of any function", | |
4869 | IDENTIFIER_POINTER (id)); | |
4870 | return NULL_TREE; | |
4871 | } | |
68642fb6 | 4872 | |
acef433b MM |
4873 | /* See if we've already got this label. */ |
4874 | decl = IDENTIFIER_LABEL_VALUE (id); | |
4875 | if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl) | |
4876 | return decl; | |
8d08fdba | 4877 | |
acef433b MM |
4878 | /* Record this label on the list of labels used in this function. |
4879 | We do this before calling make_label_decl so that we get the | |
4880 | IDENTIFIER_LABEL_VALUE before the new label is declared. */ | |
6625cdb5 | 4881 | ent = ((struct named_label_list *) |
f8a83ee3 | 4882 | ggc_alloc_cleared (sizeof (struct named_label_list))); |
6625cdb5 JM |
4883 | ent->old_value = IDENTIFIER_LABEL_VALUE (id); |
4884 | ent->next = named_labels; | |
4885 | named_labels = ent; | |
4886 | ||
acef433b MM |
4887 | /* We need a new label. */ |
4888 | decl = make_label_decl (id, /*local_p=*/0); | |
6625cdb5 | 4889 | |
acef433b | 4890 | /* Now fill in the information we didn't have before. */ |
6625cdb5 | 4891 | ent->label_decl = decl; |
8d08fdba MS |
4892 | |
4893 | return decl; | |
4894 | } | |
4895 | ||
acef433b | 4896 | /* Declare a local label named ID. */ |
8d08fdba MS |
4897 | |
4898 | tree | |
acef433b MM |
4899 | declare_local_label (id) |
4900 | tree id; | |
8d08fdba | 4901 | { |
acef433b | 4902 | tree decl; |
8d08fdba | 4903 | |
acef433b MM |
4904 | /* Add a new entry to the SHADOWED_LABELS list so that when we leave |
4905 | this scope we can restore the old value of | |
4906 | IDENTIFIER_TYPE_VALUE. */ | |
68642fb6 | 4907 | current_binding_level->shadowed_labels |
acef433b MM |
4908 | = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE, |
4909 | current_binding_level->shadowed_labels); | |
4910 | /* Look for the label. */ | |
4911 | decl = make_label_decl (id, /*local_p=*/1); | |
4912 | /* Now fill in the information we didn't have before. */ | |
4913 | TREE_VALUE (current_binding_level->shadowed_labels) = decl; | |
68642fb6 | 4914 | |
acef433b | 4915 | return decl; |
8d08fdba MS |
4916 | } |
4917 | ||
6625cdb5 JM |
4918 | /* Returns nonzero if it is ill-formed to jump past the declaration of |
4919 | DECL. Returns 2 if it's also a real problem. */ | |
4920 | ||
4921 | static int | |
4922 | decl_jump_unsafe (decl) | |
4923 | tree decl; | |
4924 | { | |
4925 | if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl)) | |
4926 | return 0; | |
4927 | ||
4928 | if (DECL_INITIAL (decl) == NULL_TREE | |
4929 | && pod_type_p (TREE_TYPE (decl))) | |
4930 | return 0; | |
4931 | ||
4932 | /* This is really only important if we're crossing an initialization. | |
4933 | The POD stuff is just pedantry; why should it matter if the class | |
4934 | contains a field of pointer to member type? */ | |
4935 | if (DECL_INITIAL (decl) | |
4936 | || (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))) | |
4937 | return 2; | |
4938 | return 1; | |
4939 | } | |
4940 | ||
4941 | /* Check that a single previously seen jump to a newly defined label | |
4942 | is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for | |
4943 | the jump context; NAMES are the names in scope in LEVEL at the jump | |
4944 | context; FILE and LINE are the source position of the jump or 0. */ | |
4945 | ||
4946 | static void | |
4947 | check_previous_goto_1 (decl, level, names, file, line) | |
4948 | tree decl; | |
4949 | struct binding_level *level; | |
4950 | tree names; | |
4951 | const char *file; | |
4952 | int line; | |
4953 | { | |
4954 | int identified = 0; | |
4955 | int saw_eh = 0; | |
4956 | struct binding_level *b = current_binding_level; | |
4957 | for (; b; b = b->level_chain) | |
4958 | { | |
4959 | tree new_decls = b->names; | |
4960 | tree old_decls = (b == level ? names : NULL_TREE); | |
4961 | for (; new_decls != old_decls; | |
4962 | new_decls = TREE_CHAIN (new_decls)) | |
4963 | { | |
4964 | int problem = decl_jump_unsafe (new_decls); | |
4965 | if (! problem) | |
4966 | continue; | |
4967 | ||
4968 | if (! identified) | |
4969 | { | |
4970 | if (decl) | |
33bd39a2 | 4971 | pedwarn ("jump to label `%D'", decl); |
6625cdb5 JM |
4972 | else |
4973 | pedwarn ("jump to case label"); | |
4974 | ||
4975 | if (file) | |
4976 | pedwarn_with_file_and_line (file, line, " from here"); | |
4977 | identified = 1; | |
4978 | } | |
4979 | ||
826840d9 | 4980 | if (problem > 1) |
6625cdb5 JM |
4981 | cp_error_at (" crosses initialization of `%#D'", |
4982 | new_decls); | |
4983 | else | |
4984 | cp_pedwarn_at (" enters scope of non-POD `%#D'", | |
4985 | new_decls); | |
4986 | } | |
4987 | ||
4988 | if (b == level) | |
4989 | break; | |
826840d9 | 4990 | if ((b->is_try_scope || b->is_catch_scope) && ! saw_eh) |
6625cdb5 JM |
4991 | { |
4992 | if (! identified) | |
4993 | { | |
4994 | if (decl) | |
33bd39a2 | 4995 | pedwarn ("jump to label `%D'", decl); |
6625cdb5 JM |
4996 | else |
4997 | pedwarn ("jump to case label"); | |
4998 | ||
4999 | if (file) | |
5000 | pedwarn_with_file_and_line (file, line, " from here"); | |
5001 | identified = 1; | |
5002 | } | |
826840d9 RH |
5003 | if (b->is_try_scope) |
5004 | error (" enters try block"); | |
5005 | else | |
5006 | error (" enters catch block"); | |
6625cdb5 JM |
5007 | saw_eh = 1; |
5008 | } | |
5009 | } | |
5010 | } | |
5011 | ||
5012 | static void | |
5013 | check_previous_goto (use) | |
5014 | struct named_label_use_list *use; | |
5015 | { | |
5016 | check_previous_goto_1 (use->label_decl, use->binding_level, | |
5017 | use->names_in_scope, use->filename_o_goto, | |
5018 | use->lineno_o_goto); | |
5019 | } | |
5020 | ||
5021 | static void | |
5022 | check_switch_goto (level) | |
5023 | struct binding_level *level; | |
5024 | { | |
5025 | check_previous_goto_1 (NULL_TREE, level, level->names, NULL, 0); | |
5026 | } | |
5027 | ||
5028 | /* Check that any previously seen jumps to a newly defined label DECL | |
5029 | are OK. Called by define_label. */ | |
5030 | ||
5031 | static void | |
5032 | check_previous_gotos (decl) | |
5033 | tree decl; | |
5034 | { | |
5035 | struct named_label_use_list **usep; | |
5036 | ||
5037 | if (! TREE_USED (decl)) | |
5038 | return; | |
5039 | ||
5040 | for (usep = &named_label_uses; *usep; ) | |
5041 | { | |
5042 | struct named_label_use_list *use = *usep; | |
5043 | if (use->label_decl == decl) | |
5044 | { | |
5045 | check_previous_goto (use); | |
5046 | *usep = use->next; | |
5047 | } | |
5048 | else | |
5049 | usep = &(use->next); | |
5050 | } | |
5051 | } | |
5052 | ||
5053 | /* Check that a new jump to a label DECL is OK. Called by | |
5054 | finish_goto_stmt. */ | |
5055 | ||
5056 | void | |
5057 | check_goto (decl) | |
5058 | tree decl; | |
5059 | { | |
5060 | int identified = 0; | |
5061 | tree bad; | |
5062 | struct named_label_list *lab; | |
5063 | ||
e3cd9945 APB |
5064 | /* We can't know where a computed goto is jumping. So we assume |
5065 | that it's OK. */ | |
5066 | if (! DECL_P (decl)) | |
5067 | return; | |
5068 | ||
6625cdb5 JM |
5069 | /* If the label hasn't been defined yet, defer checking. */ |
5070 | if (! DECL_INITIAL (decl)) | |
5071 | { | |
5072 | use_label (decl); | |
5073 | return; | |
5074 | } | |
5075 | ||
5076 | for (lab = named_labels; lab; lab = lab->next) | |
5077 | if (decl == lab->label_decl) | |
5078 | break; | |
5079 | ||
5080 | /* If the label is not on named_labels it's a gcc local label, so | |
5081 | it must be in an outer scope, so jumping to it is always OK. */ | |
5082 | if (lab == 0) | |
5083 | return; | |
5084 | ||
826840d9 RH |
5085 | if ((lab->in_try_scope || lab->in_catch_scope || lab->bad_decls) |
5086 | && !identified) | |
6625cdb5 JM |
5087 | { |
5088 | cp_pedwarn_at ("jump to label `%D'", decl); | |
5089 | pedwarn (" from here"); | |
5090 | identified = 1; | |
5091 | } | |
5092 | ||
5093 | for (bad = lab->bad_decls; bad; bad = TREE_CHAIN (bad)) | |
5094 | { | |
5095 | tree b = TREE_VALUE (bad); | |
5096 | int u = decl_jump_unsafe (b); | |
5097 | ||
5098 | if (u > 1 && DECL_ARTIFICIAL (b)) | |
5099 | /* Can't skip init of __exception_info. */ | |
5100 | cp_error_at (" enters catch block", b); | |
5101 | else if (u > 1) | |
5102 | cp_error_at (" skips initialization of `%#D'", b); | |
5103 | else | |
5104 | cp_pedwarn_at (" enters scope of non-POD `%#D'", b); | |
5105 | } | |
5106 | ||
826840d9 | 5107 | if (lab->in_try_scope) |
6625cdb5 | 5108 | error (" enters try block"); |
826840d9 RH |
5109 | else if (lab->in_catch_scope) |
5110 | error (" enters catch block"); | |
6625cdb5 JM |
5111 | } |
5112 | ||
8d08fdba MS |
5113 | /* Define a label, specifying the location in the source file. |
5114 | Return the LABEL_DECL node for the label, if the definition is valid. | |
5115 | Otherwise return 0. */ | |
5116 | ||
5117 | tree | |
5118 | define_label (filename, line, name) | |
3b304f5b | 5119 | const char *filename; |
8d08fdba MS |
5120 | int line; |
5121 | tree name; | |
5122 | { | |
f01b0acb | 5123 | tree decl = lookup_label (name); |
6625cdb5 | 5124 | struct named_label_list *ent; |
3dc9aec6 | 5125 | register struct binding_level *p; |
6625cdb5 JM |
5126 | |
5127 | for (ent = named_labels; ent; ent = ent->next) | |
5128 | if (ent->label_decl == decl) | |
5129 | break; | |
8d08fdba | 5130 | |
3dc9aec6 | 5131 | /* After labels, make any new cleanups in the function go into their |
8d08fdba | 5132 | own new (temporary) binding contour. */ |
3dc9aec6 WC |
5133 | for (p = current_binding_level; !(p->parm_flag); p = p->level_chain) |
5134 | p->more_cleanups_ok = 0; | |
8d08fdba | 5135 | |
e1cd6e56 | 5136 | if (name == get_identifier ("wchar_t")) |
33bd39a2 | 5137 | pedwarn ("label named wchar_t"); |
e1cd6e56 | 5138 | |
8d08fdba MS |
5139 | if (DECL_INITIAL (decl) != NULL_TREE) |
5140 | { | |
33bd39a2 | 5141 | error ("duplicate label `%D'", decl); |
8d08fdba MS |
5142 | return 0; |
5143 | } | |
5144 | else | |
5145 | { | |
8d08fdba MS |
5146 | /* Mark label as having been defined. */ |
5147 | DECL_INITIAL (decl) = error_mark_node; | |
5148 | /* Say where in the source. */ | |
5149 | DECL_SOURCE_FILE (decl) = filename; | |
5150 | DECL_SOURCE_LINE (decl) = line; | |
6625cdb5 JM |
5151 | if (ent) |
5152 | { | |
5153 | ent->names_in_scope = current_binding_level->names; | |
5154 | ent->binding_level = current_binding_level; | |
5155 | } | |
5156 | check_previous_gotos (decl); | |
8d08fdba MS |
5157 | return decl; |
5158 | } | |
5159 | } | |
5160 | ||
a5894242 MS |
5161 | struct cp_switch |
5162 | { | |
5163 | struct binding_level *level; | |
5164 | struct cp_switch *next; | |
56cb9733 MM |
5165 | /* The SWITCH_STMT being built. */ |
5166 | tree switch_stmt; | |
5167 | /* A splay-tree mapping the low element of a case range to the high | |
5168 | element, or NULL_TREE if there is no high element. Used to | |
5169 | determine whether or not a new case label duplicates an old case | |
5170 | label. We need a tree, rather than simply a hash table, because | |
5171 | of the GNU case range extension. */ | |
5172 | splay_tree cases; | |
a5894242 MS |
5173 | }; |
5174 | ||
56cb9733 MM |
5175 | /* A stack of the currently active switch statements. The innermost |
5176 | switch statement is on the top of the stack. There is no need to | |
5177 | mark the stack for garbage collection because it is only active | |
5178 | during the processing of the body of a function, and we never | |
5179 | collect at that point. */ | |
5362b086 | 5180 | |
a5894242 MS |
5181 | static struct cp_switch *switch_stack; |
5182 | ||
56cb9733 MM |
5183 | /* Called right after a switch-statement condition is parsed. |
5184 | SWITCH_STMT is the switch statement being parsed. */ | |
5185 | ||
a5894242 | 5186 | void |
56cb9733 MM |
5187 | push_switch (switch_stmt) |
5188 | tree switch_stmt; | |
a5894242 MS |
5189 | { |
5190 | struct cp_switch *p | |
bedda2da | 5191 | = (struct cp_switch *) xmalloc (sizeof (struct cp_switch)); |
a5894242 MS |
5192 | p->level = current_binding_level; |
5193 | p->next = switch_stack; | |
56cb9733 MM |
5194 | p->switch_stmt = switch_stmt; |
5195 | p->cases = splay_tree_new (case_compare, NULL, NULL); | |
a5894242 MS |
5196 | switch_stack = p; |
5197 | } | |
5198 | ||
5199 | void | |
5200 | pop_switch () | |
5201 | { | |
bedda2da | 5202 | struct cp_switch *cs; |
5362b086 | 5203 | |
bedda2da | 5204 | cs = switch_stack; |
56cb9733 | 5205 | splay_tree_delete (cs->cases); |
a5894242 | 5206 | switch_stack = switch_stack->next; |
bedda2da | 5207 | free (cs); |
a5894242 MS |
5208 | } |
5209 | ||
b0a1da19 JM |
5210 | /* Note that we've seen a definition of a case label, and complain if this |
5211 | is a bad place for one. */ | |
e92cc029 | 5212 | |
3e4d04a1 | 5213 | tree |
56cb9733 MM |
5214 | finish_case_label (low_value, high_value) |
5215 | tree low_value; | |
5216 | tree high_value; | |
8d08fdba | 5217 | { |
3e4d04a1 | 5218 | tree cond, r; |
3dc9aec6 | 5219 | register struct binding_level *p; |
a5894242 | 5220 | |
b0a1da19 | 5221 | if (! switch_stack) |
56cb9733 MM |
5222 | { |
5223 | if (high_value) | |
5224 | error ("case label not within a switch statement"); | |
5225 | else if (low_value) | |
33bd39a2 | 5226 | error ("case label `%E' not within a switch statement", |
56cb9733 MM |
5227 | low_value); |
5228 | else | |
5229 | error ("`default' label not within a switch statement"); | |
3e4d04a1 | 5230 | return NULL_TREE; |
56cb9733 MM |
5231 | } |
5232 | ||
56cb9733 MM |
5233 | if (processing_template_decl) |
5234 | { | |
8f17b5c5 MM |
5235 | tree label; |
5236 | ||
56cb9733 MM |
5237 | /* For templates, just add the case label; we'll do semantic |
5238 | analysis at instantiation-time. */ | |
8f17b5c5 | 5239 | label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE); |
3e4d04a1 | 5240 | return add_stmt (build_case_label (low_value, high_value, label)); |
56cb9733 MM |
5241 | } |
5242 | ||
5243 | /* Find the condition on which this switch statement depends. */ | |
5244 | cond = SWITCH_COND (switch_stack->switch_stmt); | |
5245 | if (cond && TREE_CODE (cond) == TREE_LIST) | |
5246 | cond = TREE_VALUE (cond); | |
56cb9733 | 5247 | |
3e4d04a1 RH |
5248 | r = c_add_case_label (switch_stack->cases, cond, low_value, high_value); |
5249 | if (r == error_mark_node) | |
5250 | r = NULL_TREE; | |
8d08fdba | 5251 | |
6625cdb5 | 5252 | check_switch_goto (switch_stack->level); |
a5894242 | 5253 | |
3dc9aec6 | 5254 | /* After labels, make any new cleanups in the function go into their |
8d08fdba | 5255 | own new (temporary) binding contour. */ |
3dc9aec6 WC |
5256 | for (p = current_binding_level; !(p->parm_flag); p = p->level_chain) |
5257 | p->more_cleanups_ok = 0; | |
3e4d04a1 RH |
5258 | |
5259 | return r; | |
8d08fdba MS |
5260 | } |
5261 | \f | |
5262 | /* Return the list of declarations of the current level. | |
5263 | Note that this list is in reverse order unless/until | |
5264 | you nreverse it; and when you do nreverse it, you must | |
5265 | store the result back using `storedecls' or you will lose. */ | |
5266 | ||
5267 | tree | |
5268 | getdecls () | |
5269 | { | |
5270 | return current_binding_level->names; | |
5271 | } | |
5272 | ||
5273 | /* Return the list of type-tags (for structs, etc) of the current level. */ | |
5274 | ||
5275 | tree | |
5276 | gettags () | |
5277 | { | |
5278 | return current_binding_level->tags; | |
5279 | } | |
5280 | ||
5281 | /* Store the list of declarations of the current level. | |
5282 | This is done for the parameter declarations of a function being defined, | |
5283 | after they are modified in the light of any missing parameters. */ | |
5284 | ||
5285 | static void | |
5286 | storedecls (decls) | |
5287 | tree decls; | |
5288 | { | |
5289 | current_binding_level->names = decls; | |
5290 | } | |
5291 | ||
5292 | /* Similarly, store the list of tags of the current level. */ | |
5293 | ||
280f9385 | 5294 | void |
8d08fdba MS |
5295 | storetags (tags) |
5296 | tree tags; | |
5297 | { | |
5298 | current_binding_level->tags = tags; | |
5299 | } | |
5300 | \f | |
4782bd5b RS |
5301 | /* Return the type that should be used when TYPE's name is preceded |
5302 | by a tag such as 'struct' or 'union', or null if the name cannot | |
5303 | be used in this way. | |
5304 | ||
5305 | For example, when processing the third line of: | |
5306 | ||
5307 | struct A; | |
5308 | typedef struct A A; | |
5309 | struct A; | |
5310 | ||
5311 | lookup of A will find the typedef. Given A's typedef, this function | |
5312 | will return the type associated with "struct A". For the tag to be | |
5313 | anything other than TYPE, TYPE must be a typedef whose original type | |
5314 | has the same name and context as TYPE itself. | |
5315 | ||
5316 | It is not valid for a typedef of an anonymous type to be used with | |
5317 | an explicit tag: | |
5318 | ||
5319 | typedef struct { ... } B; | |
5320 | struct B; | |
5321 | ||
5322 | Return null for this case. */ | |
5323 | ||
5324 | static tree | |
5325 | follow_tag_typedef (type) | |
5326 | tree type; | |
5327 | { | |
5328 | tree original; | |
5329 | ||
5330 | original = original_type (type); | |
5331 | if (TYPE_IDENTIFIER (original) == TYPE_IDENTIFIER (type) | |
5332 | && (CP_DECL_CONTEXT (TYPE_NAME (original)) | |
5333 | == CP_DECL_CONTEXT (TYPE_NAME (type))) | |
5334 | && !(CLASS_TYPE_P (original) && TYPE_WAS_ANONYMOUS (original))) | |
5335 | return original; | |
5336 | else | |
5337 | return NULL_TREE; | |
5338 | } | |
5339 | ||
8d08fdba MS |
5340 | /* Given NAME, an IDENTIFIER_NODE, |
5341 | return the structure (or union or enum) definition for that name. | |
5342 | Searches binding levels from BINDING_LEVEL up to the global level. | |
5343 | If THISLEVEL_ONLY is nonzero, searches only the specified context | |
5344 | (but skips any tag-transparent contexts to find one that is | |
5345 | meaningful for tags). | |
5346 | FORM says which kind of type the caller wants; | |
5347 | it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE. | |
5348 | If the wrong kind of type is found, and it's not a template, an error is | |
5349 | reported. */ | |
5350 | ||
5351 | static tree | |
5352 | lookup_tag (form, name, binding_level, thislevel_only) | |
5353 | enum tree_code form; | |
8d08fdba | 5354 | tree name; |
cffa8729 | 5355 | struct binding_level *binding_level; |
8d08fdba MS |
5356 | int thislevel_only; |
5357 | { | |
5358 | register struct binding_level *level; | |
74b846e0 MM |
5359 | /* Non-zero if, we should look past a template parameter level, even |
5360 | if THISLEVEL_ONLY. */ | |
5361 | int allow_template_parms_p = 1; | |
8d08fdba MS |
5362 | |
5363 | for (level = binding_level; level; level = level->level_chain) | |
5364 | { | |
5365 | register tree tail; | |
5366 | if (ANON_AGGRNAME_P (name)) | |
5367 | for (tail = level->tags; tail; tail = TREE_CHAIN (tail)) | |
5368 | { | |
5369 | /* There's no need for error checking here, because | |
5370 | anon names are unique throughout the compilation. */ | |
5371 | if (TYPE_IDENTIFIER (TREE_VALUE (tail)) == name) | |
5372 | return TREE_VALUE (tail); | |
5373 | } | |
2c73f9f5 ML |
5374 | else if (level->namespace_p) |
5375 | /* Do namespace lookup. */ | |
6c011b01 | 5376 | for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail)) |
2c73f9f5 | 5377 | { |
36a117a5 MM |
5378 | tree old = binding_for_name (name, tail); |
5379 | ||
74b846e0 MM |
5380 | /* If we just skipped past a template parameter level, |
5381 | even though THISLEVEL_ONLY, and we find a template | |
5382 | class declaration, then we use the _TYPE node for the | |
36a117a5 | 5383 | template. See the example below. */ |
74b846e0 | 5384 | if (thislevel_only && !allow_template_parms_p |
68642fb6 | 5385 | && old && BINDING_VALUE (old) |
36a117a5 MM |
5386 | && DECL_CLASS_TEMPLATE_P (BINDING_VALUE (old))) |
5387 | old = TREE_TYPE (BINDING_VALUE (old)); | |
68642fb6 | 5388 | else |
36a117a5 MM |
5389 | old = BINDING_TYPE (old); |
5390 | ||
4782bd5b | 5391 | if (old) |
2c73f9f5 | 5392 | { |
4782bd5b RS |
5393 | /* We've found something at this binding level. If it is |
5394 | a typedef, extract the tag it refers to. Lookup fails | |
5395 | if the typedef doesn't refer to a taggable type. */ | |
5396 | old = follow_tag_typedef (old); | |
5397 | if (!old) | |
5398 | return NULL_TREE; | |
5399 | if (TREE_CODE (old) != form | |
5400 | && (form == ENUMERAL_TYPE | |
5401 | || TREE_CODE (old) == ENUMERAL_TYPE)) | |
5402 | { | |
5403 | error ("`%#D' redeclared as %C", old, form); | |
5404 | return NULL_TREE; | |
5405 | } | |
5406 | return old; | |
2c73f9f5 | 5407 | } |
2c73f9f5 ML |
5408 | if (thislevel_only || tail == global_namespace) |
5409 | return NULL_TREE; | |
5410 | } | |
8d08fdba MS |
5411 | else |
5412 | for (tail = level->tags; tail; tail = TREE_CHAIN (tail)) | |
5413 | { | |
a80e4195 | 5414 | if (TREE_PURPOSE (tail) == name) |
8d08fdba MS |
5415 | { |
5416 | enum tree_code code = TREE_CODE (TREE_VALUE (tail)); | |
fd87b7a7 | 5417 | |
8d08fdba | 5418 | if (code != form |
fd87b7a7 | 5419 | && (form == ENUMERAL_TYPE || code == ENUMERAL_TYPE)) |
8d08fdba MS |
5420 | { |
5421 | /* Definition isn't the kind we were looking for. */ | |
fd87b7a7 | 5422 | error ("`%#D' redeclared as %C", TREE_VALUE (tail), form); |
72b7eeff | 5423 | return NULL_TREE; |
8d08fdba MS |
5424 | } |
5425 | return TREE_VALUE (tail); | |
5426 | } | |
5427 | } | |
5428 | if (thislevel_only && ! level->tag_transparent) | |
5566b478 | 5429 | { |
74b846e0 | 5430 | if (level->template_parms_p && allow_template_parms_p) |
5566b478 | 5431 | { |
36a117a5 | 5432 | /* We must deal with cases like this: |
68642fb6 | 5433 | |
36a117a5 MM |
5434 | template <class T> struct S; |
5435 | template <class T> struct S {}; | |
68642fb6 | 5436 | |
36a117a5 MM |
5437 | When looking up `S', for the second declaration, we |
5438 | would like to find the first declaration. But, we | |
5439 | are in the pseudo-global level created for the | |
5440 | template parameters, rather than the (surrounding) | |
5441 | namespace level. Thus, we keep going one more level, | |
5442 | even though THISLEVEL_ONLY is non-zero. */ | |
74b846e0 | 5443 | allow_template_parms_p = 0; |
36a117a5 | 5444 | continue; |
5566b478 | 5445 | } |
36a117a5 MM |
5446 | else |
5447 | return NULL_TREE; | |
5566b478 | 5448 | } |
8d08fdba MS |
5449 | } |
5450 | return NULL_TREE; | |
5451 | } | |
5452 | ||
bd6dd845 | 5453 | #if 0 |
8d08fdba MS |
5454 | void |
5455 | set_current_level_tags_transparency (tags_transparent) | |
5456 | int tags_transparent; | |
5457 | { | |
5458 | current_binding_level->tag_transparent = tags_transparent; | |
5459 | } | |
bd6dd845 | 5460 | #endif |
8d08fdba MS |
5461 | |
5462 | /* Given a type, find the tag that was defined for it and return the tag name. | |
5463 | Otherwise return 0. However, the value can never be 0 | |
5464 | in the cases in which this is used. | |
5465 | ||
5466 | C++: If NAME is non-zero, this is the new name to install. This is | |
5467 | done when replacing anonymous tags with real tag names. */ | |
5468 | ||
5469 | static tree | |
5470 | lookup_tag_reverse (type, name) | |
5471 | tree type; | |
5472 | tree name; | |
5473 | { | |
5474 | register struct binding_level *level; | |
5475 | ||
5476 | for (level = current_binding_level; level; level = level->level_chain) | |
5477 | { | |
5478 | register tree tail; | |
5479 | for (tail = level->tags; tail; tail = TREE_CHAIN (tail)) | |
5480 | { | |
5481 | if (TREE_VALUE (tail) == type) | |
5482 | { | |
5483 | if (name) | |
5484 | TREE_PURPOSE (tail) = name; | |
5485 | return TREE_PURPOSE (tail); | |
5486 | } | |
5487 | } | |
5488 | } | |
5489 | return NULL_TREE; | |
5490 | } | |
8d08fdba | 5491 | \f |
a9aedbc2 | 5492 | /* Look up NAME in the NAMESPACE. */ |
e92cc029 | 5493 | |
a9aedbc2 MS |
5494 | tree |
5495 | lookup_namespace_name (namespace, name) | |
5496 | tree namespace, name; | |
5497 | { | |
30394414 | 5498 | tree val; |
f30c84c9 | 5499 | tree template_id = NULL_TREE; |
2c73f9f5 | 5500 | |
30394414 | 5501 | my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370); |
1231fb96 | 5502 | |
1231fb96 | 5503 | if (TREE_CODE (name) == NAMESPACE_DECL) |
d8f8dca1 | 5504 | /* This happens for A::B<int> when B is a namespace. */ |
1231fb96 | 5505 | return name; |
d8f8dca1 MM |
5506 | else if (TREE_CODE (name) == TEMPLATE_DECL) |
5507 | { | |
5508 | /* This happens for A::B where B is a template, and there are no | |
5509 | template arguments. */ | |
33bd39a2 | 5510 | error ("invalid use of `%D'", name); |
d8f8dca1 MM |
5511 | return error_mark_node; |
5512 | } | |
1231fb96 | 5513 | |
b262d64c JM |
5514 | namespace = ORIGINAL_NAMESPACE (namespace); |
5515 | ||
f30c84c9 MM |
5516 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) |
5517 | { | |
5518 | template_id = name; | |
5519 | name = TREE_OPERAND (name, 0); | |
5520 | if (TREE_CODE (name) == OVERLOAD) | |
5521 | name = DECL_NAME (OVL_CURRENT (name)); | |
2f939d94 | 5522 | else if (DECL_P (name)) |
f30c84c9 MM |
5523 | name = DECL_NAME (name); |
5524 | } | |
5525 | ||
30394414 | 5526 | my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373); |
68642fb6 | 5527 | |
87e3dbc9 | 5528 | val = make_node (CPLUS_BINDING); |
52c11ef6 | 5529 | if (!qualified_lookup_using_namespace (name, namespace, val, 0)) |
2c73f9f5 ML |
5530 | return error_mark_node; |
5531 | ||
5532 | if (BINDING_VALUE (val)) | |
1c35f5b6 JM |
5533 | { |
5534 | val = BINDING_VALUE (val); | |
5535 | ||
f30c84c9 MM |
5536 | if (template_id) |
5537 | { | |
5538 | if (DECL_CLASS_TEMPLATE_P (val)) | |
68642fb6 | 5539 | val = lookup_template_class (val, |
f30c84c9 MM |
5540 | TREE_OPERAND (template_id, 1), |
5541 | /*in_decl=*/NULL_TREE, | |
5542 | /*context=*/NULL_TREE, | |
f9c244b8 | 5543 | /*entering_scope=*/0, |
c2ea3a40 | 5544 | tf_error | tf_warning); |
f30c84c9 MM |
5545 | else if (DECL_FUNCTION_TEMPLATE_P (val) |
5546 | || TREE_CODE (val) == OVERLOAD) | |
68642fb6 | 5547 | val = lookup_template_function (val, |
f30c84c9 MM |
5548 | TREE_OPERAND (template_id, 1)); |
5549 | else | |
5550 | { | |
33bd39a2 | 5551 | error ("`%D::%D' is not a template", |
f30c84c9 MM |
5552 | namespace, name); |
5553 | return error_mark_node; | |
5554 | } | |
5555 | } | |
5556 | ||
1c35f5b6 JM |
5557 | /* If we have a single function from a using decl, pull it out. */ |
5558 | if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val)) | |
5559 | val = OVL_FUNCTION (val); | |
d52e4867 RS |
5560 | |
5561 | /* Ignore built-in functions that haven't been prototyped yet. */ | |
5562 | if (!val || !DECL_P(val) | |
5563 | || !DECL_LANG_SPECIFIC(val) | |
5564 | || !DECL_ANTICIPATED (val)) | |
5565 | return val; | |
1c35f5b6 JM |
5566 | } |
5567 | ||
33bd39a2 | 5568 | error ("`%D' undeclared in namespace `%D'", name, namespace); |
30394414 | 5569 | return error_mark_node; |
a9aedbc2 MS |
5570 | } |
5571 | ||
7ddedda4 MM |
5572 | /* Hash a TYPENAME_TYPE. K is really of type `tree'. */ |
5573 | ||
5574 | static unsigned long | |
5575 | typename_hash (k) | |
5576 | hash_table_key k; | |
5577 | { | |
5578 | unsigned long hash; | |
5579 | tree t; | |
5580 | ||
5581 | t = (tree) k; | |
5582 | hash = (((unsigned long) TYPE_CONTEXT (t)) | |
5583 | ^ ((unsigned long) DECL_NAME (TYPE_NAME (t)))); | |
5584 | ||
5585 | return hash; | |
5586 | } | |
5587 | ||
5588 | /* Compare two TYPENAME_TYPEs. K1 and K2 are really of type `tree'. */ | |
5589 | ||
d6edb99e | 5590 | static bool |
7ddedda4 MM |
5591 | typename_compare (k1, k2) |
5592 | hash_table_key k1; | |
5593 | hash_table_key k2; | |
5594 | { | |
5595 | tree t1; | |
5596 | tree t2; | |
5597 | tree d1; | |
5598 | tree d2; | |
5599 | ||
5600 | t1 = (tree) k1; | |
5601 | t2 = (tree) k2; | |
5602 | d1 = TYPE_NAME (t1); | |
5603 | d2 = TYPE_NAME (t2); | |
68642fb6 | 5604 | |
7ddedda4 MM |
5605 | return (DECL_NAME (d1) == DECL_NAME (d2) |
5606 | && same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)) | |
68642fb6 | 5607 | && ((TREE_TYPE (t1) != NULL_TREE) |
7ddedda4 MM |
5608 | == (TREE_TYPE (t2) != NULL_TREE)) |
5609 | && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)) | |
5610 | && TYPENAME_TYPE_FULLNAME (t1) == TYPENAME_TYPE_FULLNAME (t2)); | |
5611 | } | |
5612 | ||
45869a6c MM |
5613 | /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is |
5614 | the type of `T', NAME is the IDENTIFIER_NODE for `t'. If BASE_TYPE | |
5615 | is non-NULL, this type is being created by the implicit typename | |
5616 | extension, and BASE_TYPE is a type named `t' in some base class of | |
68642fb6 | 5617 | `T' which depends on template parameters. |
45869a6c MM |
5618 | |
5619 | Returns the new TYPENAME_TYPE. */ | |
5620 | ||
5621 | tree | |
5622 | build_typename_type (context, name, fullname, base_type) | |
5623 | tree context; | |
5624 | tree name; | |
5625 | tree fullname; | |
5626 | tree base_type; | |
5627 | { | |
5628 | tree t; | |
5629 | tree d; | |
400500c4 | 5630 | struct hash_entry *e; |
45869a6c | 5631 | |
7ddedda4 MM |
5632 | static struct hash_table ht; |
5633 | ||
9cd64686 MM |
5634 | if (!ht.table) |
5635 | { | |
5636 | static struct hash_table *h = &ht; | |
400500c4 RK |
5637 | |
5638 | hash_table_init (&ht, &hash_newfunc, &typename_hash, &typename_compare); | |
9cd64686 MM |
5639 | ggc_add_tree_hash_table_root (&h, 1); |
5640 | } | |
45869a6c MM |
5641 | |
5642 | /* Build the TYPENAME_TYPE. */ | |
33848bb0 | 5643 | t = make_aggr_type (TYPENAME_TYPE); |
45869a6c MM |
5644 | TYPE_CONTEXT (t) = FROB_CONTEXT (context); |
5645 | TYPENAME_TYPE_FULLNAME (t) = fullname; | |
5646 | TREE_TYPE (t) = base_type; | |
45869a6c MM |
5647 | |
5648 | /* Build the corresponding TYPE_DECL. */ | |
5649 | d = build_decl (TYPE_DECL, name, t); | |
5650 | TYPE_NAME (TREE_TYPE (d)) = d; | |
5651 | TYPE_STUB_DECL (TREE_TYPE (d)) = d; | |
5652 | DECL_CONTEXT (d) = FROB_CONTEXT (context); | |
2642b9bf | 5653 | DECL_ARTIFICIAL (d) = 1; |
45869a6c | 5654 | |
7ddedda4 MM |
5655 | /* See if we already have this type. */ |
5656 | e = hash_lookup (&ht, t, /*create=*/false, /*copy=*/0); | |
5657 | if (e) | |
87e3dbc9 | 5658 | t = (tree) e->key; |
7ddedda4 MM |
5659 | else |
5660 | /* Insert the type into the table. */ | |
5661 | hash_lookup (&ht, t, /*create=*/true, /*copy=*/0); | |
5662 | ||
45869a6c MM |
5663 | return t; |
5664 | } | |
5665 | ||
3baa501d MM |
5666 | /* Resolve `typename CONTEXT::NAME'. Returns an appropriate type, |
5667 | unless an error occurs, in which case error_mark_node is returned. | |
4f2b0fb2 NS |
5668 | If we locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is |
5669 | set, we return that, rather than the _TYPE it corresponds to, in | |
5670 | other cases we look through the type decl. If TF_ERROR is set, | |
5671 | complain about errors, otherwise be quiet. */ | |
3baa501d | 5672 | |
5566b478 | 5673 | tree |
3baa501d | 5674 | make_typename_type (context, name, complain) |
5566b478 | 5675 | tree context, name; |
4f2b0fb2 | 5676 | tsubst_flags_t complain; |
5566b478 | 5677 | { |
b2b7d40a | 5678 | tree fullname; |
a80e4195 | 5679 | |
2f939d94 | 5680 | if (TYPE_P (name)) |
78638e24 | 5681 | { |
68642fb6 UD |
5682 | if (!(TYPE_LANG_SPECIFIC (name) |
5683 | && (CLASSTYPE_IS_TEMPLATE (name) | |
78638e24 MM |
5684 | || CLASSTYPE_USE_TEMPLATE (name)))) |
5685 | name = TYPE_IDENTIFIER (name); | |
5686 | else | |
5687 | /* Create a TEMPLATE_ID_EXPR for the type. */ | |
5688 | name = build_nt (TEMPLATE_ID_EXPR, | |
5689 | CLASSTYPE_TI_TEMPLATE (name), | |
5690 | CLASSTYPE_TI_ARGS (name)); | |
5691 | } | |
653cc74a | 5692 | else if (TREE_CODE (name) == TYPE_DECL) |
a80e4195 | 5693 | name = DECL_NAME (name); |
b2b7d40a JM |
5694 | |
5695 | fullname = name; | |
5696 | ||
5697 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) | |
11686454 JM |
5698 | { |
5699 | name = TREE_OPERAND (name, 0); | |
5700 | if (TREE_CODE (name) == TEMPLATE_DECL) | |
5701 | name = TREE_OPERAND (fullname, 0) = DECL_NAME (name); | |
5702 | } | |
8a2b77e7 JM |
5703 | if (TREE_CODE (name) == TEMPLATE_DECL) |
5704 | { | |
33bd39a2 | 5705 | error ("`%D' used without template parameters", name); |
8a2b77e7 JM |
5706 | return error_mark_node; |
5707 | } | |
b2b7d40a | 5708 | if (TREE_CODE (name) != IDENTIFIER_NODE) |
a98facb0 | 5709 | abort (); |
5566b478 | 5710 | |
04ddee1b BK |
5711 | if (TREE_CODE (context) == NAMESPACE_DECL) |
5712 | { | |
5713 | /* We can get here from typename_sub0 in the explicit_template_type | |
5714 | expansion. Just fail. */ | |
4f2b0fb2 | 5715 | if (complain & tf_error) |
33bd39a2 | 5716 | error ("no class template named `%#T' in `%#T'", |
3baa501d | 5717 | name, context); |
04ddee1b BK |
5718 | return error_mark_node; |
5719 | } | |
5720 | ||
85b71cf2 | 5721 | if (! uses_template_parms (context) |
b77ead33 | 5722 | || currently_open_class (context)) |
5566b478 | 5723 | { |
b2b7d40a JM |
5724 | if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR) |
5725 | { | |
ad810b22 | 5726 | tree tmpl = NULL_TREE; |
b2b7d40a | 5727 | if (IS_AGGR_TYPE (context)) |
ad810b22 MM |
5728 | tmpl = lookup_field (context, name, 0, 0); |
5729 | if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl)) | |
b2b7d40a | 5730 | { |
4f2b0fb2 | 5731 | if (complain & tf_error) |
33bd39a2 | 5732 | error ("no class template named `%#T' in `%#T'", |
3baa501d | 5733 | name, context); |
b2b7d40a JM |
5734 | return error_mark_node; |
5735 | } | |
ffb690bd | 5736 | |
68642fb6 | 5737 | return lookup_template_class (tmpl, |
ad810b22 | 5738 | TREE_OPERAND (fullname, 1), |
68642fb6 | 5739 | NULL_TREE, context, |
f9c244b8 | 5740 | /*entering_scope=*/0, |
c2ea3a40 | 5741 | tf_error | tf_warning); |
b2b7d40a JM |
5742 | } |
5743 | else | |
5566b478 | 5744 | { |
b4f70b3d | 5745 | tree t; |
68642fb6 | 5746 | |
b4f70b3d | 5747 | if (!IS_AGGR_TYPE (context)) |
b2b7d40a | 5748 | { |
4f2b0fb2 | 5749 | if (complain & tf_error) |
33bd39a2 | 5750 | error ("no type named `%#T' in `%#T'", name, context); |
b2b7d40a JM |
5751 | return error_mark_node; |
5752 | } | |
1107c4b3 | 5753 | |
b4f70b3d | 5754 | t = lookup_field (context, name, 0, 1); |
7d4bdeed | 5755 | if (t) |
4f2b0fb2 NS |
5756 | { |
5757 | if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl)) | |
5758 | t = TREE_TYPE (t); | |
ba3307c0 NS |
5759 | if (IMPLICIT_TYPENAME_P (t)) |
5760 | { | |
5761 | /* Lookup found an implicit typename that we had | |
5762 | injected into the current scope. Doing things | |
5763 | properly would have located the exact same type, | |
5764 | so there is no error here. We must remove the | |
5765 | implicitness so that we do not warn about it. */ | |
5766 | t = copy_node (t); | |
5767 | TREE_TYPE (t) = NULL_TREE; | |
5768 | } | |
5769 | ||
4f2b0fb2 NS |
5770 | return t; |
5771 | } | |
5566b478 | 5772 | } |
5566b478 | 5773 | } |
11249cf0 MM |
5774 | |
5775 | /* If the CONTEXT is not a template type, then either the field is | |
5776 | there now or its never going to be. */ | |
b4f70b3d | 5777 | if (!uses_template_parms (context)) |
11249cf0 | 5778 | { |
4f2b0fb2 | 5779 | if (complain & tf_error) |
33bd39a2 | 5780 | error ("no type named `%#T' in `%#T'", name, context); |
11249cf0 MM |
5781 | return error_mark_node; |
5782 | } | |
68642fb6 UD |
5783 | |
5784 | ||
45869a6c | 5785 | return build_typename_type (context, name, fullname, NULL_TREE); |
5566b478 MS |
5786 | } |
5787 | ||
b8c6534b KL |
5788 | /* Resolve `CONTEXT::template NAME'. Returns an appropriate type, |
5789 | unless an error occurs, in which case error_mark_node is returned. | |
4f2b0fb2 NS |
5790 | If we locate a TYPE_DECL, we return that, rather than the _TYPE it |
5791 | corresponds to. If COMPLAIN zero, don't complain about any errors | |
5792 | that occur. */ | |
b8c6534b KL |
5793 | |
5794 | tree | |
5795 | make_unbound_class_template (context, name, complain) | |
5796 | tree context, name; | |
5797 | int complain; | |
5798 | { | |
5799 | tree t; | |
5800 | tree d; | |
5801 | ||
5802 | if (TYPE_P (name)) | |
5803 | name = TYPE_IDENTIFIER (name); | |
5804 | else if (DECL_P (name)) | |
5805 | name = DECL_NAME (name); | |
5806 | if (TREE_CODE (name) != IDENTIFIER_NODE) | |
a98facb0 | 5807 | abort (); |
b8c6534b KL |
5808 | |
5809 | if (!uses_template_parms (context) | |
5810 | || currently_open_class (context)) | |
5811 | { | |
5812 | tree tmpl = NULL_TREE; | |
5813 | ||
5814 | if (IS_AGGR_TYPE (context)) | |
5815 | tmpl = lookup_field (context, name, 0, 0); | |
5816 | ||
5817 | if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl)) | |
5818 | { | |
5819 | if (complain) | |
33bd39a2 | 5820 | error ("no class template named `%#T' in `%#T'", name, context); |
b8c6534b KL |
5821 | return error_mark_node; |
5822 | } | |
5823 | ||
5824 | if (!enforce_access (context, tmpl)) | |
5825 | return error_mark_node; | |
5826 | ||
5827 | return tmpl; | |
5828 | } | |
5829 | ||
5830 | /* Build the UNBOUND_CLASS_TEMPLATE. */ | |
5831 | t = make_aggr_type (UNBOUND_CLASS_TEMPLATE); | |
5832 | TYPE_CONTEXT (t) = FROB_CONTEXT (context); | |
5833 | TREE_TYPE (t) = NULL_TREE; | |
5834 | ||
5835 | /* Build the corresponding TEMPLATE_DECL. */ | |
5836 | d = build_decl (TEMPLATE_DECL, name, t); | |
5837 | TYPE_NAME (TREE_TYPE (d)) = d; | |
5838 | TYPE_STUB_DECL (TREE_TYPE (d)) = d; | |
5839 | DECL_CONTEXT (d) = FROB_CONTEXT (context); | |
5840 | DECL_ARTIFICIAL (d) = 1; | |
5841 | ||
5842 | return t; | |
5843 | } | |
5844 | ||
2c73f9f5 ML |
5845 | /* Select the right _DECL from multiple choices. */ |
5846 | ||
5847 | static tree | |
52c11ef6 | 5848 | select_decl (binding, flags) |
2c73f9f5 | 5849 | tree binding; |
52c11ef6 | 5850 | int flags; |
2c73f9f5 ML |
5851 | { |
5852 | tree val; | |
5853 | val = BINDING_VALUE (binding); | |
97ba1e3c | 5854 | |
52c11ef6 | 5855 | if (LOOKUP_NAMESPACES_ONLY (flags)) |
3e3f722c ML |
5856 | { |
5857 | /* We are not interested in types. */ | |
5858 | if (val && TREE_CODE (val) == NAMESPACE_DECL) | |
5859 | return val; | |
5860 | return NULL_TREE; | |
5861 | } | |
68642fb6 | 5862 | |
2c73f9f5 ML |
5863 | /* If we could have a type and |
5864 | we have nothing or we need a type and have none. */ | |
5865 | if (BINDING_TYPE (binding) | |
52c11ef6 JM |
5866 | && (!val || ((flags & LOOKUP_PREFER_TYPES) |
5867 | && TREE_CODE (val) != TYPE_DECL))) | |
2c73f9f5 ML |
5868 | val = TYPE_STUB_DECL (BINDING_TYPE (binding)); |
5869 | /* Don't return non-types if we really prefer types. */ | |
52c11ef6 | 5870 | else if (val && LOOKUP_TYPES_ONLY (flags) && TREE_CODE (val) != TYPE_DECL |
c592d5d2 ML |
5871 | && (TREE_CODE (val) != TEMPLATE_DECL |
5872 | || !DECL_CLASS_TEMPLATE_P (val))) | |
2c73f9f5 | 5873 | val = NULL_TREE; |
1c35f5b6 | 5874 | |
2c73f9f5 ML |
5875 | return val; |
5876 | } | |
5877 | ||
2c169bab JM |
5878 | /* Unscoped lookup of a global: iterate over current namespaces, |
5879 | considering using-directives. If SPACESP is non-NULL, store a list | |
5880 | of the namespaces we've considered in it. */ | |
ea9635c7 | 5881 | |
2c169bab JM |
5882 | tree |
5883 | unqualified_namespace_lookup (name, flags, spacesp) | |
ea9635c7 | 5884 | tree name; |
52c11ef6 | 5885 | int flags; |
2c169bab | 5886 | tree *spacesp; |
ea9635c7 | 5887 | { |
87e3dbc9 | 5888 | tree b = make_node (CPLUS_BINDING); |
ee8fc32b | 5889 | tree initial = current_decl_namespace (); |
ea9635c7 ML |
5890 | tree scope = initial; |
5891 | tree siter; | |
5892 | struct binding_level *level; | |
5893 | tree val = NULL_TREE; | |
5894 | ||
2c169bab JM |
5895 | if (spacesp) |
5896 | *spacesp = NULL_TREE; | |
5897 | ||
5898 | for (; !val; scope = CP_DECL_CONTEXT (scope)) | |
ea9635c7 | 5899 | { |
2c169bab | 5900 | if (spacesp) |
e1b3e07d | 5901 | *spacesp = tree_cons (scope, NULL_TREE, *spacesp); |
ea9635c7 ML |
5902 | val = binding_for_name (name, scope); |
5903 | ||
d52e4867 RS |
5904 | /* Ignore anticipated built-in functions. */ |
5905 | if (val && BINDING_VALUE (val) | |
5906 | && DECL_P (BINDING_VALUE (val)) | |
5907 | && DECL_LANG_SPECIFIC (BINDING_VALUE (val)) | |
5908 | && DECL_ANTICIPATED (BINDING_VALUE (val))) | |
5909 | { | |
5910 | BINDING_VALUE (b) = NULL_TREE; | |
5911 | BINDING_TYPE (b) = NULL_TREE; | |
5912 | } | |
5913 | else | |
5914 | { | |
5915 | /* Initialize binding for this context. */ | |
5916 | BINDING_VALUE (b) = BINDING_VALUE (val); | |
5917 | BINDING_TYPE (b) = BINDING_TYPE (val); | |
5918 | } | |
ea9635c7 ML |
5919 | |
5920 | /* Add all _DECLs seen through local using-directives. */ | |
68642fb6 | 5921 | for (level = current_binding_level; |
ea9635c7 ML |
5922 | !level->namespace_p; |
5923 | level = level->level_chain) | |
52c11ef6 | 5924 | if (!lookup_using_namespace (name, b, level->using_directives, |
2c169bab | 5925 | scope, flags, spacesp)) |
ea9635c7 | 5926 | /* Give up because of error. */ |
5b163de4 | 5927 | return error_mark_node; |
ea9635c7 ML |
5928 | |
5929 | /* Add all _DECLs seen through global using-directives. */ | |
5930 | /* XXX local and global using lists should work equally. */ | |
5931 | siter = initial; | |
5932 | while (1) | |
5933 | { | |
68642fb6 | 5934 | if (!lookup_using_namespace (name, b, DECL_NAMESPACE_USING (siter), |
2c169bab | 5935 | scope, flags, spacesp)) |
ea9635c7 | 5936 | /* Give up because of error. */ |
5b163de4 | 5937 | return error_mark_node; |
ea9635c7 ML |
5938 | if (siter == scope) break; |
5939 | siter = CP_DECL_CONTEXT (siter); | |
5940 | } | |
5941 | ||
52c11ef6 | 5942 | val = select_decl (b, flags); |
ea9635c7 ML |
5943 | if (scope == global_namespace) |
5944 | break; | |
ea9635c7 ML |
5945 | } |
5946 | return val; | |
5947 | } | |
5948 | ||
52c11ef6 JM |
5949 | /* Combine prefer_type and namespaces_only into flags. */ |
5950 | ||
5951 | static int | |
5952 | lookup_flags (prefer_type, namespaces_only) | |
5953 | int prefer_type, namespaces_only; | |
5954 | { | |
5955 | if (namespaces_only) | |
5956 | return LOOKUP_PREFER_NAMESPACES; | |
5957 | if (prefer_type > 1) | |
5958 | return LOOKUP_PREFER_TYPES; | |
5959 | if (prefer_type > 0) | |
5960 | return LOOKUP_PREFER_BOTH; | |
5961 | return 0; | |
5962 | } | |
5963 | ||
5964 | /* Given a lookup that returned VAL, use FLAGS to decide if we want to | |
5965 | ignore it or not. Subroutine of lookup_name_real. */ | |
5966 | ||
5967 | static tree | |
5968 | qualify_lookup (val, flags) | |
5969 | tree val; | |
5970 | int flags; | |
5971 | { | |
5972 | if (val == NULL_TREE) | |
5973 | return val; | |
6b945830 JM |
5974 | if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL) |
5975 | return val; | |
5976 | if ((flags & LOOKUP_PREFER_TYPES) | |
5977 | && (TREE_CODE (val) == TYPE_DECL | |
5978 | || ((flags & LOOKUP_TEMPLATES_EXPECTED) | |
5979 | && DECL_CLASS_TEMPLATE_P (val)))) | |
5980 | return val; | |
5981 | if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES)) | |
52c11ef6 JM |
5982 | return NULL_TREE; |
5983 | return val; | |
5984 | } | |
5985 | ||
235f734d MM |
5986 | /* Any other BINDING overrides an implicit TYPENAME. Warn about |
5987 | that. */ | |
5988 | ||
5989 | static void | |
5990 | warn_about_implicit_typename_lookup (typename, binding) | |
5991 | tree typename; | |
5992 | tree binding; | |
5993 | { | |
5994 | tree subtype = TREE_TYPE (TREE_TYPE (typename)); | |
5995 | tree name = DECL_NAME (typename); | |
5996 | ||
5997 | if (! (TREE_CODE (binding) == TEMPLATE_DECL | |
5998 | && CLASSTYPE_TEMPLATE_INFO (subtype) | |
5999 | && CLASSTYPE_TI_TEMPLATE (subtype) == binding) | |
6000 | && ! (TREE_CODE (binding) == TYPE_DECL | |
6001 | && same_type_p (TREE_TYPE (binding), subtype))) | |
6002 | { | |
33bd39a2 | 6003 | warning ("lookup of `%D' finds `%#D'", |
235f734d | 6004 | name, binding); |
33bd39a2 | 6005 | warning (" instead of `%D' from dependent base class", |
235f734d | 6006 | typename); |
33bd39a2 | 6007 | warning (" (use `typename %T::%D' if that's what you meant)", |
235f734d MM |
6008 | constructor_name (current_class_type), name); |
6009 | } | |
6010 | } | |
6011 | ||
8d08fdba MS |
6012 | /* Look up NAME in the current binding level and its superiors in the |
6013 | namespace of variables, functions and typedefs. Return a ..._DECL | |
6014 | node of some kind representing its definition if there is only one | |
6015 | such declaration, or return a TREE_LIST with all the overloaded | |
6016 | definitions if there are many, or return 0 if it is undefined. | |
6017 | ||
2c73f9f5 ML |
6018 | If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces. |
6019 | If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces). | |
a28e3c7f | 6020 | If PREFER_TYPE is -2, we're being called from yylex(). (UGLY) |
68642fb6 | 6021 | Otherwise we prefer non-TYPE_DECLs. |
f84b4be9 JM |
6022 | |
6023 | If NONCLASS is non-zero, we don't look for the NAME in class scope, | |
6024 | using IDENTIFIER_CLASS_VALUE. */ | |
8d08fdba | 6025 | |
824b9a4c | 6026 | static tree |
3e3f722c | 6027 | lookup_name_real (name, prefer_type, nonclass, namespaces_only) |
8d08fdba | 6028 | tree name; |
3e3f722c | 6029 | int prefer_type, nonclass, namespaces_only; |
8d08fdba | 6030 | { |
235f734d MM |
6031 | tree t; |
6032 | tree val = NULL_TREE; | |
a28e3c7f | 6033 | int yylex = 0; |
e1cd6e56 | 6034 | tree from_obj = NULL_TREE; |
52c11ef6 | 6035 | int flags; |
235f734d | 6036 | int val_is_implicit_typename = 0; |
8d08fdba | 6037 | |
3e3f722c ML |
6038 | /* Hack: copy flag set by parser, if set. */ |
6039 | if (only_namespace_names) | |
6040 | namespaces_only = 1; | |
52c11ef6 | 6041 | |
a28e3c7f MS |
6042 | if (prefer_type == -2) |
6043 | { | |
6044 | extern int looking_for_typename; | |
fc378698 | 6045 | tree type = NULL_TREE; |
a28e3c7f MS |
6046 | |
6047 | yylex = 1; | |
6048 | prefer_type = looking_for_typename; | |
e1cd6e56 | 6049 | |
52c11ef6 | 6050 | flags = lookup_flags (prefer_type, namespaces_only); |
52c11ef6 JM |
6051 | /* If the next thing is '<', class templates are types. */ |
6052 | if (looking_for_template) | |
6053 | flags |= LOOKUP_TEMPLATES_EXPECTED; | |
6054 | ||
e1cd6e56 MS |
6055 | if (got_scope) |
6056 | type = got_scope; | |
dff6b454 | 6057 | else if (got_object != error_mark_node) |
e1cd6e56 | 6058 | type = got_object; |
68642fb6 | 6059 | |
e1cd6e56 | 6060 | if (type) |
a28e3c7f | 6061 | { |
e1cd6e56 | 6062 | if (type == error_mark_node) |
f376e137 | 6063 | return error_mark_node; |
a80e4195 MS |
6064 | if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type)) |
6065 | type = TREE_TYPE (type); | |
5566b478 | 6066 | |
2b9dc906 JM |
6067 | if (TYPE_P (type)) |
6068 | type = complete_type (type); | |
5566b478 | 6069 | |
a1774733 | 6070 | if (TREE_CODE (type) == VOID_TYPE) |
2c73f9f5 ML |
6071 | type = global_namespace; |
6072 | if (TREE_CODE (type) == NAMESPACE_DECL) | |
a9aedbc2 | 6073 | { |
87e3dbc9 | 6074 | val = make_node (CPLUS_BINDING); |
6ad07332 | 6075 | flags |= LOOKUP_COMPLAIN; |
52c11ef6 | 6076 | if (!qualified_lookup_using_namespace (name, type, val, flags)) |
2c73f9f5 | 6077 | return NULL_TREE; |
52c11ef6 | 6078 | val = select_decl (val, flags); |
a9aedbc2 | 6079 | } |
5566b478 | 6080 | else if (! IS_AGGR_TYPE (type) |
5156628f | 6081 | || TREE_CODE (type) == TEMPLATE_TYPE_PARM |
a1281f45 | 6082 | || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM |
5156628f | 6083 | || TREE_CODE (type) == TYPENAME_TYPE) |
e92cc029 | 6084 | /* Someone else will give an error about this if needed. */ |
a28e3c7f | 6085 | val = NULL_TREE; |
e1cd6e56 | 6086 | else if (type == current_class_type) |
a28e3c7f | 6087 | val = IDENTIFIER_CLASS_VALUE (name); |
a28e3c7f | 6088 | else |
70adf8a9 JM |
6089 | { |
6090 | val = lookup_member (type, name, 0, prefer_type); | |
6091 | type_access_control (type, val); | |
e69c8072 JM |
6092 | |
6093 | /* Restore the containing TYPENAME_TYPE if we looked | |
6094 | through it before. */ | |
6095 | if (got_scope && got_scope != type | |
6096 | && val && TREE_CODE (val) == TYPE_DECL | |
6097 | && TREE_CODE (TREE_TYPE (val)) == TYPENAME_TYPE) | |
6098 | TYPE_CONTEXT (TREE_TYPE (val)) = got_scope; | |
70adf8a9 | 6099 | } |
a28e3c7f | 6100 | } |
e1cd6e56 MS |
6101 | else |
6102 | val = NULL_TREE; | |
6103 | ||
594740f3 | 6104 | if (got_scope) |
e1cd6e56 | 6105 | goto done; |
594740f3 | 6106 | else if (got_object && val) |
aca77bd1 JM |
6107 | { |
6108 | from_obj = val; | |
6109 | val = NULL_TREE; | |
6110 | } | |
a28e3c7f | 6111 | } |
52c11ef6 | 6112 | else |
5b163de4 JM |
6113 | { |
6114 | flags = lookup_flags (prefer_type, namespaces_only); | |
6115 | /* If we're not parsing, we need to complain. */ | |
6116 | flags |= LOOKUP_COMPLAIN; | |
6117 | } | |
e76a2646 | 6118 | |
d8f8dca1 | 6119 | /* First, look in non-namespace scopes. */ |
6f1b4c42 JM |
6120 | |
6121 | if (current_class_type == NULL_TREE) | |
6122 | nonclass = 1; | |
6123 | ||
235f734d | 6124 | for (t = IDENTIFIER_BINDING (name); t; t = TREE_CHAIN (t)) |
d8f8dca1 | 6125 | { |
235f734d MM |
6126 | tree binding; |
6127 | ||
6128 | if (!LOCAL_BINDING_P (t) && nonclass) | |
d8f8dca1 MM |
6129 | /* We're not looking for class-scoped bindings, so keep going. */ |
6130 | continue; | |
68642fb6 | 6131 | |
d8f8dca1 | 6132 | /* If this is the kind of thing we're looking for, we're done. */ |
235f734d MM |
6133 | if (qualify_lookup (BINDING_VALUE (t), flags)) |
6134 | binding = BINDING_VALUE (t); | |
68642fb6 | 6135 | else if ((flags & LOOKUP_PREFER_TYPES) |
235f734d MM |
6136 | && qualify_lookup (BINDING_TYPE (t), flags)) |
6137 | binding = BINDING_TYPE (t); | |
6138 | else | |
6139 | binding = NULL_TREE; | |
6140 | ||
70adf8a9 JM |
6141 | /* Handle access control on types from enclosing or base classes. */ |
6142 | if (binding && ! yylex | |
6143 | && BINDING_LEVEL (t) && BINDING_LEVEL (t)->parm_flag == 2) | |
6144 | type_access_control (BINDING_LEVEL (t)->this_class, binding); | |
6145 | ||
235f734d | 6146 | if (binding |
83233dca | 6147 | && (!val || !IMPLICIT_TYPENAME_TYPE_DECL_P (binding))) |
d8f8dca1 | 6148 | { |
235f734d MM |
6149 | if (val_is_implicit_typename && !yylex) |
6150 | warn_about_implicit_typename_lookup (val, binding); | |
6151 | val = binding; | |
68642fb6 | 6152 | val_is_implicit_typename |
83233dca | 6153 | = IMPLICIT_TYPENAME_TYPE_DECL_P (val); |
235f734d MM |
6154 | if (!val_is_implicit_typename) |
6155 | break; | |
d8f8dca1 MM |
6156 | } |
6157 | } | |
f181d4ae | 6158 | |
235f734d MM |
6159 | /* Now lookup in namespace scopes. */ |
6160 | if (!val || val_is_implicit_typename) | |
e76a2646 | 6161 | { |
2c169bab | 6162 | t = unqualified_namespace_lookup (name, flags, 0); |
235f734d | 6163 | if (t) |
c1def683 | 6164 | { |
235f734d MM |
6165 | if (val_is_implicit_typename && !yylex) |
6166 | warn_about_implicit_typename_lookup (val, t); | |
6167 | val = t; | |
c1def683 JM |
6168 | } |
6169 | } | |
6170 | ||
a28e3c7f | 6171 | done: |
8d08fdba MS |
6172 | if (val) |
6173 | { | |
c91a56d2 | 6174 | /* This should only warn about types used in qualified-ids. */ |
e1cd6e56 | 6175 | if (from_obj && from_obj != val) |
5566b478 | 6176 | { |
c91a56d2 MS |
6177 | if (looking_for_typename && TREE_CODE (from_obj) == TYPE_DECL |
6178 | && TREE_CODE (val) == TYPE_DECL | |
aca77bd1 | 6179 | && ! same_type_p (TREE_TYPE (from_obj), TREE_TYPE (val))) |
33bd39a2 | 6180 | pedwarn ("\ |
bd0d5d4a JM |
6181 | lookup of `%D' in the scope of `%#T' (`%#D') \ |
6182 | does not match lookup in the current scope (`%#D')", | |
6183 | name, got_object, from_obj, val); | |
594740f3 | 6184 | |
b8b1a3c1 JM |
6185 | /* We don't change val to from_obj if got_object depends on |
6186 | template parms because that breaks implicit typename for | |
6187 | destructor calls. */ | |
6188 | if (! uses_template_parms (got_object)) | |
53fdbb3b | 6189 | val = from_obj; |
5566b478 | 6190 | } |
e1cd6e56 | 6191 | |
0c64a9ca JM |
6192 | /* If we have a single function from a using decl, pull it out. */ |
6193 | if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val)) | |
6194 | val = OVL_FUNCTION (val); | |
8d08fdba | 6195 | } |
e1cd6e56 MS |
6196 | else if (from_obj) |
6197 | val = from_obj; | |
8d08fdba MS |
6198 | |
6199 | return val; | |
6200 | } | |
6201 | ||
700f8a87 MS |
6202 | tree |
6203 | lookup_name_nonclass (name) | |
6204 | tree name; | |
6205 | { | |
3e3f722c | 6206 | return lookup_name_real (name, 0, 1, 0); |
700f8a87 MS |
6207 | } |
6208 | ||
2c73f9f5 ML |
6209 | tree |
6210 | lookup_function_nonclass (name, args) | |
6211 | tree name; | |
6212 | tree args; | |
6213 | { | |
6214 | return lookup_arg_dependent (name, lookup_name_nonclass (name), args); | |
6215 | } | |
6216 | ||
3e3f722c ML |
6217 | tree |
6218 | lookup_name_namespace_only (name) | |
6219 | tree name; | |
6220 | { | |
6221 | /* type-or-namespace, nonclass, namespace_only */ | |
6222 | return lookup_name_real (name, 1, 1, 1); | |
6223 | } | |
6224 | ||
700f8a87 MS |
6225 | tree |
6226 | lookup_name (name, prefer_type) | |
6227 | tree name; | |
6228 | int prefer_type; | |
6229 | { | |
3e3f722c | 6230 | return lookup_name_real (name, prefer_type, 0, 0); |
700f8a87 MS |
6231 | } |
6232 | ||
a7d2d407 MM |
6233 | /* Similar to `lookup_name' but look only in the innermost non-class |
6234 | binding level. */ | |
8d08fdba MS |
6235 | |
6236 | tree | |
6237 | lookup_name_current_level (name) | |
6238 | tree name; | |
6239 | { | |
a7d2d407 MM |
6240 | struct binding_level *b; |
6241 | tree t = NULL_TREE; | |
8d08fdba | 6242 | |
a7d2d407 MM |
6243 | b = current_binding_level; |
6244 | while (b->parm_flag == 2) | |
6245 | b = b->level_chain; | |
6246 | ||
6247 | if (b->namespace_p) | |
8d08fdba | 6248 | { |
5fdaba89 | 6249 | t = IDENTIFIER_NAMESPACE_VALUE (name); |
8d08fdba MS |
6250 | |
6251 | /* extern "C" function() */ | |
6252 | if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST) | |
6253 | t = TREE_VALUE (t); | |
6254 | } | |
68642fb6 | 6255 | else if (IDENTIFIER_BINDING (name) |
f181d4ae | 6256 | && LOCAL_BINDING_P (IDENTIFIER_BINDING (name))) |
8d08fdba | 6257 | { |
a4443a08 MS |
6258 | while (1) |
6259 | { | |
f181d4ae MM |
6260 | if (BINDING_LEVEL (IDENTIFIER_BINDING (name)) == b) |
6261 | return IDENTIFIER_VALUE (name); | |
68642fb6 | 6262 | |
9ed182dc JM |
6263 | if (b->keep == 2) |
6264 | b = b->level_chain; | |
6265 | else | |
6266 | break; | |
6267 | } | |
6268 | } | |
6269 | ||
6270 | return t; | |
6271 | } | |
6272 | ||
6273 | /* Like lookup_name_current_level, but for types. */ | |
6274 | ||
6275 | tree | |
6276 | lookup_type_current_level (name) | |
6277 | tree name; | |
6278 | { | |
6279 | register tree t = NULL_TREE; | |
6280 | ||
6281 | my_friendly_assert (! current_binding_level->namespace_p, 980716); | |
6282 | ||
6283 | if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE | |
6284 | && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node) | |
6285 | { | |
6286 | struct binding_level *b = current_binding_level; | |
6287 | while (1) | |
6288 | { | |
6289 | if (purpose_member (name, b->type_shadowed)) | |
6290 | return REAL_IDENTIFIER_TYPE_VALUE (name); | |
a4443a08 MS |
6291 | if (b->keep == 2) |
6292 | b = b->level_chain; | |
6293 | else | |
6294 | break; | |
6295 | } | |
8d08fdba MS |
6296 | } |
6297 | ||
6298 | return t; | |
6299 | } | |
3e3f722c ML |
6300 | |
6301 | void | |
6302 | begin_only_namespace_names () | |
6303 | { | |
6304 | only_namespace_names = 1; | |
6305 | } | |
6306 | ||
6307 | void | |
6308 | end_only_namespace_names () | |
6309 | { | |
6310 | only_namespace_names = 0; | |
6311 | } | |
8d08fdba | 6312 | \f |
8d08fdba | 6313 | /* Push the declarations of builtin types into the namespace. |
0e5921e8 | 6314 | RID_INDEX is the index of the builtin type |
8d08fdba MS |
6315 | in the array RID_POINTERS. NAME is the name used when looking |
6316 | up the builtin type. TYPE is the _TYPE node for the builtin type. */ | |
6317 | ||
eaa7c03f | 6318 | void |
8d08fdba MS |
6319 | record_builtin_type (rid_index, name, type) |
6320 | enum rid rid_index; | |
d8e178a0 | 6321 | const char *name; |
8d08fdba MS |
6322 | tree type; |
6323 | { | |
6324 | tree rname = NULL_TREE, tname = NULL_TREE; | |
a703fb38 | 6325 | tree tdecl = NULL_TREE; |
8d08fdba | 6326 | |
0e5921e8 | 6327 | if ((int) rid_index < (int) RID_MAX) |
8d08fdba MS |
6328 | rname = ridpointers[(int) rid_index]; |
6329 | if (name) | |
6330 | tname = get_identifier (name); | |
6331 | ||
6332 | TYPE_BUILT_IN (type) = 1; | |
68642fb6 | 6333 | |
8d08fdba MS |
6334 | if (tname) |
6335 | { | |
8d08fdba | 6336 | tdecl = pushdecl (build_decl (TYPE_DECL, tname, type)); |
8d08fdba | 6337 | set_identifier_type_value (tname, NULL_TREE); |
0e5921e8 | 6338 | if ((int) rid_index < (int) RID_MAX) |
2c73f9f5 ML |
6339 | /* Built-in types live in the global namespace. */ |
6340 | SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl); | |
8d08fdba MS |
6341 | } |
6342 | if (rname != NULL_TREE) | |
6343 | { | |
6344 | if (tname != NULL_TREE) | |
6345 | { | |
6346 | set_identifier_type_value (rname, NULL_TREE); | |
2c73f9f5 | 6347 | SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl); |
8d08fdba MS |
6348 | } |
6349 | else | |
6350 | { | |
8d08fdba | 6351 | tdecl = pushdecl (build_decl (TYPE_DECL, rname, type)); |
8d08fdba MS |
6352 | set_identifier_type_value (rname, NULL_TREE); |
6353 | } | |
6354 | } | |
8d08fdba MS |
6355 | } |
6356 | ||
eff71ab0 | 6357 | /* Record one of the standard Java types. |
4d8a1dd6 MM |
6358 | * Declare it as having the given NAME. |
6359 | * If SIZE > 0, it is the size of one of the integral types; | |
6360 | * otherwise it is the negative of the size of one of the other types. */ | |
eff71ab0 PB |
6361 | |
6362 | static tree | |
6363 | record_builtin_java_type (name, size) | |
d8e178a0 | 6364 | const char *name; |
eff71ab0 PB |
6365 | int size; |
6366 | { | |
6367 | tree type, decl; | |
6368 | if (size > 0) | |
6369 | type = make_signed_type (size); | |
6370 | else if (size > -32) | |
6371 | { /* "__java_char" or ""__java_boolean". */ | |
6372 | type = make_unsigned_type (-size); | |
6373 | /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/ | |
6374 | } | |
6375 | else | |
6376 | { /* "__java_float" or ""__java_double". */ | |
6377 | type = make_node (REAL_TYPE); | |
6378 | TYPE_PRECISION (type) = - size; | |
6379 | layout_type (type); | |
6380 | } | |
0e5921e8 | 6381 | record_builtin_type (RID_MAX, name, type); |
eff71ab0 | 6382 | decl = TYPE_NAME (type); |
e229f2cd PB |
6383 | |
6384 | /* Suppress generate debug symbol entries for these types, | |
6385 | since for normal C++ they are just clutter. | |
6386 | However, push_lang_context undoes this if extern "Java" is seen. */ | |
eff71ab0 | 6387 | DECL_IGNORED_P (decl) = 1; |
e229f2cd | 6388 | |
eff71ab0 PB |
6389 | TYPE_FOR_JAVA (type) = 1; |
6390 | return type; | |
6391 | } | |
6392 | ||
036407f7 ML |
6393 | /* Push a type into the namespace so that the back-ends ignore it. */ |
6394 | ||
6395 | static void | |
6396 | record_unknown_type (type, name) | |
6397 | tree type; | |
d8e178a0 | 6398 | const char *name; |
036407f7 ML |
6399 | { |
6400 | tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type)); | |
6401 | /* Make sure the "unknown type" typedecl gets ignored for debug info. */ | |
6402 | DECL_IGNORED_P (decl) = 1; | |
6403 | TYPE_DECL_SUPPRESS_DEBUG (decl) = 1; | |
6404 | TYPE_SIZE (type) = TYPE_SIZE (void_type_node); | |
6405 | TYPE_ALIGN (type) = 1; | |
11cf4d18 | 6406 | TYPE_USER_ALIGN (type) = 0; |
036407f7 | 6407 | TYPE_MODE (type) = TYPE_MODE (void_type_node); |
68642fb6 | 6408 | } |
036407f7 | 6409 | |
d43829f9 MM |
6410 | /* An string for which we should create an IDENTIFIER_NODE at |
6411 | startup. */ | |
6412 | ||
6413 | typedef struct predefined_identifier | |
6414 | { | |
6415 | /* The name of the identifier. */ | |
8b60264b | 6416 | const char *const name; |
d43829f9 | 6417 | /* The place where the IDENTIFIER_NODE should be stored. */ |
8b60264b | 6418 | tree *const node; |
298d6f60 | 6419 | /* Non-zero if this is the name of a constructor or destructor. */ |
8b60264b | 6420 | const int ctor_or_dtor_p; |
d43829f9 MM |
6421 | } predefined_identifier; |
6422 | ||
6423 | /* Create all the predefined identifiers. */ | |
6424 | ||
6425 | static void | |
5362b086 | 6426 | initialize_predefined_identifiers () |
d43829f9 | 6427 | { |
8b60264b | 6428 | const predefined_identifier *pid; |
d43829f9 MM |
6429 | |
6430 | /* A table of identifiers to create at startup. */ | |
8b60264b | 6431 | static const predefined_identifier predefined_identifiers[] = { |
298d6f60 MM |
6432 | { "C++", &lang_name_cplusplus, 0 }, |
6433 | { "C", &lang_name_c, 0 }, | |
6434 | { "Java", &lang_name_java, 0 }, | |
6435 | { CTOR_NAME, &ctor_identifier, 1 }, | |
6436 | { "__base_ctor", &base_ctor_identifier, 1 }, | |
6437 | { "__comp_ctor", &complete_ctor_identifier, 1 }, | |
6438 | { DTOR_NAME, &dtor_identifier, 1 }, | |
6439 | { "__comp_dtor", &complete_dtor_identifier, 1 }, | |
6440 | { "__base_dtor", &base_dtor_identifier, 1 }, | |
6441 | { "__deleting_dtor", &deleting_dtor_identifier, 1 }, | |
298d6f60 | 6442 | { IN_CHARGE_NAME, &in_charge_identifier, 0 }, |
298d6f60 MM |
6443 | { "nelts", &nelts_identifier, 0 }, |
6444 | { THIS_NAME, &this_identifier, 0 }, | |
c4372ef4 | 6445 | { VTABLE_DELTA_NAME, &delta_identifier, 0 }, |
298d6f60 | 6446 | { VTABLE_PFN_NAME, &pfn_identifier, 0 }, |
298d6f60 | 6447 | { "_vptr", &vptr_identifier, 0 }, |
3ec6bad3 | 6448 | { "__vtt_parm", &vtt_parm_identifier, 0 }, |
1f6e1acc | 6449 | { "std", &std_identifier, 0 }, |
298d6f60 | 6450 | { NULL, NULL, 0 } |
d43829f9 MM |
6451 | }; |
6452 | ||
6453 | for (pid = predefined_identifiers; pid->name; ++pid) | |
298d6f60 MM |
6454 | { |
6455 | *pid->node = get_identifier (pid->name); | |
6456 | if (pid->ctor_or_dtor_p) | |
6457 | IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1; | |
6458 | } | |
d43829f9 MM |
6459 | } |
6460 | ||
8d08fdba MS |
6461 | /* Create the predefined scalar types of C, |
6462 | and some nodes representing standard constants (0, 1, (void *)0). | |
6463 | Initialize the global binding level. | |
6464 | Make definitions for built-in primitive functions. */ | |
6465 | ||
6466 | void | |
f5e99456 | 6467 | cxx_init_decl_processing () |
8d08fdba | 6468 | { |
10841285 MM |
6469 | tree void_ftype; |
6470 | tree void_ftype_ptr; | |
8d08fdba | 6471 | |
d43829f9 MM |
6472 | /* Create all the identifiers we need. */ |
6473 | initialize_predefined_identifiers (); | |
8d08fdba | 6474 | |
8f17b5c5 | 6475 | /* Fill in back-end hooks. */ |
a8f73d4b MM |
6476 | init_lang_status = &push_cp_function_context; |
6477 | free_lang_status = &pop_cp_function_context; | |
b4b8bee7 | 6478 | mark_lang_status = &mark_cp_function_context; |
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 | |
6431177a JM |
6532 | lang_attribute_table = cp_attribute_table; |
6533 | ||
eaa7c03f | 6534 | c_common_nodes_and_builtins (); |
37c46b43 | 6535 | |
4d8a1dd6 MM |
6536 | java_byte_type_node = record_builtin_java_type ("__java_byte", 8); |
6537 | java_short_type_node = record_builtin_java_type ("__java_short", 16); | |
6538 | java_int_type_node = record_builtin_java_type ("__java_int", 32); | |
6539 | java_long_type_node = record_builtin_java_type ("__java_long", 64); | |
6540 | java_float_type_node = record_builtin_java_type ("__java_float", -32); | |
6541 | java_double_type_node = record_builtin_java_type ("__java_double", -64); | |
6542 | java_char_type_node = record_builtin_java_type ("__java_char", -16); | |
6543 | java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1); | |
eff71ab0 | 6544 | |
8d08fdba MS |
6545 | integer_two_node = build_int_2 (2, 0); |
6546 | TREE_TYPE (integer_two_node) = integer_type_node; | |
6547 | integer_three_node = build_int_2 (3, 0); | |
6548 | TREE_TYPE (integer_three_node) = integer_type_node; | |
8d08fdba | 6549 | |
255512c1 JM |
6550 | boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE); |
6551 | TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE); | |
c1ea6a0b BK |
6552 | TYPE_MAX_VALUE (boolean_type_node) = build_int_2 (1, 0); |
6553 | TREE_TYPE (TYPE_MAX_VALUE (boolean_type_node)) = boolean_type_node; | |
6554 | TYPE_PRECISION (boolean_type_node) = 1; | |
255512c1 JM |
6555 | record_builtin_type (RID_BOOL, "bool", boolean_type_node); |
6556 | boolean_false_node = build_int_2 (0, 0); | |
6557 | TREE_TYPE (boolean_false_node) = boolean_type_node; | |
6558 | boolean_true_node = build_int_2 (1, 0); | |
6559 | TREE_TYPE (boolean_true_node) = boolean_type_node; | |
2986ae00 | 6560 | |
ff0bee63 | 6561 | signed_size_zero_node = build_int_2 (0, 0); |
e92cc029 MS |
6562 | TREE_TYPE (signed_size_zero_node) = make_signed_type (TYPE_PRECISION (sizetype)); |
6563 | ||
4cc1d462 | 6564 | empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE); |
5fd8e536 JM |
6565 | |
6566 | #if 0 | |
3e411c3f | 6567 | record_builtin_type (RID_MAX, NULL, string_type_node); |
5fd8e536 JM |
6568 | #endif |
6569 | ||
1f84ec23 MM |
6570 | delta_type_node = ptrdiff_type_node; |
6571 | vtable_index_type = ptrdiff_type_node; | |
c7e266a6 | 6572 | |
3ec6bad3 | 6573 | vtt_parm_type = build_pointer_type (const_ptr_type_node); |
356955cf | 6574 | lang_type_promotes_to = convert_type_from_ellipsis; |
7f4edbcb | 6575 | |
10841285 MM |
6576 | void_ftype = build_function_type (void_type_node, void_list_node); |
6577 | void_ftype_ptr = build_function_type (void_type_node, | |
6578 | tree_cons (NULL_TREE, | |
6579 | ptr_type_node, | |
6580 | void_list_node)); | |
824b9a4c | 6581 | void_ftype_ptr |
4cc1d462 | 6582 | = build_exception_variant (void_ftype_ptr, empty_except_spec); |
8d08fdba | 6583 | |
8d08fdba MS |
6584 | /* C++ extensions */ |
6585 | ||
6586 | unknown_type_node = make_node (UNKNOWN_TYPE); | |
036407f7 ML |
6587 | record_unknown_type (unknown_type_node, "unknown type"); |
6588 | ||
8d08fdba MS |
6589 | /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */ |
6590 | TREE_TYPE (unknown_type_node) = unknown_type_node; | |
a6967cc0 | 6591 | |
a6967cc0 JM |
6592 | /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same |
6593 | result. */ | |
8d08fdba MS |
6594 | TYPE_POINTER_TO (unknown_type_node) = unknown_type_node; |
6595 | TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node; | |
6596 | ||
c4372ef4 NS |
6597 | { |
6598 | /* Make sure we get a unique function type, so we can give | |
6599 | its pointer type a name. (This wins for gdb.) */ | |
6600 | tree vfunc_type = make_node (FUNCTION_TYPE); | |
6601 | TREE_TYPE (vfunc_type) = integer_type_node; | |
6602 | TYPE_ARG_TYPES (vfunc_type) = NULL_TREE; | |
6603 | layout_type (vfunc_type); | |
6604 | ||
6605 | vtable_entry_type = build_pointer_type (vfunc_type); | |
6606 | } | |
0e5921e8 | 6607 | record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type); |
8d08fdba | 6608 | |
8d08fdba | 6609 | vtbl_type_node |
52bf7d5d | 6610 | = build_cplus_array_type (vtable_entry_type, NULL_TREE); |
8d08fdba | 6611 | layout_type (vtbl_type_node); |
91063b51 | 6612 | vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST); |
3e411c3f | 6613 | record_builtin_type (RID_MAX, NULL, vtbl_type_node); |
849da744 MM |
6614 | vtbl_ptr_type_node = build_pointer_type (vtable_entry_type); |
6615 | layout_type (vtbl_ptr_type_node); | |
3e411c3f | 6616 | record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node); |
8d08fdba | 6617 | |
1f84ec23 MM |
6618 | push_namespace (get_identifier ("__cxxabiv1")); |
6619 | abi_node = current_namespace; | |
6620 | pop_namespace (); | |
6633d636 | 6621 | |
2c73f9f5 | 6622 | global_type_node = make_node (LANG_TYPE); |
036407f7 | 6623 | record_unknown_type (global_type_node, "global type"); |
2c73f9f5 | 6624 | |
db5ae43f MS |
6625 | /* Now, C++. */ |
6626 | current_lang_name = lang_name_cplusplus; | |
8d08fdba | 6627 | |
ced78d8b | 6628 | { |
2c73f9f5 | 6629 | tree bad_alloc_type_node, newtype, deltype; |
10841285 MM |
6630 | tree ptr_ftype_sizetype; |
6631 | ||
1dbb6023 | 6632 | push_namespace (std_identifier); |
2c73f9f5 | 6633 | bad_alloc_type_node = xref_tag |
ca107ded | 6634 | (class_type_node, get_identifier ("bad_alloc"), 1); |
1dbb6023 | 6635 | pop_namespace (); |
10841285 MM |
6636 | ptr_ftype_sizetype |
6637 | = build_function_type (ptr_type_node, | |
6638 | tree_cons (NULL_TREE, | |
6639 | c_size_type_node, | |
6640 | void_list_node)); | |
2c73f9f5 | 6641 | newtype = build_exception_variant |
0ba8a114 NS |
6642 | (ptr_ftype_sizetype, add_exception_specifier |
6643 | (NULL_TREE, bad_alloc_type_node, -1)); | |
4cc1d462 | 6644 | deltype = build_exception_variant (void_ftype_ptr, empty_except_spec); |
596ea4e5 AS |
6645 | push_cp_library_fn (NEW_EXPR, newtype); |
6646 | push_cp_library_fn (VEC_NEW_EXPR, newtype); | |
6647 | global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype); | |
6648 | push_cp_library_fn (VEC_DELETE_EXPR, deltype); | |
ced78d8b | 6649 | } |
8d08fdba MS |
6650 | |
6651 | abort_fndecl | |
1f84ec23 | 6652 | = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype); |
8d08fdba | 6653 | |
8d08fdba MS |
6654 | /* Perform other language dependent initializations. */ |
6655 | init_class_processing (); | |
8d08fdba | 6656 | init_search_processing (); |
db48b831 | 6657 | init_rtti_processing (); |
8d08fdba | 6658 | |
6467930b | 6659 | if (flag_exceptions) |
8d2733ca | 6660 | init_exception_processing (); |
9e9ff709 | 6661 | |
7fcdf4c2 | 6662 | if (! supports_one_only ()) |
72b7eeff | 6663 | flag_weak = 0; |
8d08fdba | 6664 | |
2ce07e2d | 6665 | make_fname_decl = cp_make_fname_decl; |
0ba8a114 | 6666 | start_fname_decls (); |
8d08fdba | 6667 | |
e9a25f70 JL |
6668 | /* Show we use EH for cleanups. */ |
6669 | using_eh_for_cleanups (); | |
62c154ed | 6670 | |
d9cf7c82 JM |
6671 | /* Maintain consistency. Perhaps we should just complain if they |
6672 | say -fwritable-strings? */ | |
6673 | if (flag_writable_strings) | |
6674 | flag_const_strings = 0; | |
fc6af6e3 RH |
6675 | |
6676 | /* Add GC roots for all of our global variables. */ | |
ca7558fc KG |
6677 | ggc_add_tree_root (c_global_trees, ARRAY_SIZE (c_global_trees)); |
6678 | ggc_add_tree_root (cp_global_trees, ARRAY_SIZE (cp_global_trees)); | |
fc6af6e3 RH |
6679 | ggc_add_tree_root (&integer_three_node, 1); |
6680 | ggc_add_tree_root (&integer_two_node, 1); | |
fc6af6e3 | 6681 | ggc_add_tree_root (&signed_size_zero_node, 1); |
fc6af6e3 RH |
6682 | ggc_add_tree_root (&size_one_node, 1); |
6683 | ggc_add_tree_root (&size_zero_node, 1); | |
fc6af6e3 RH |
6684 | ggc_add_root (&global_binding_level, 1, sizeof global_binding_level, |
6685 | mark_binding_level); | |
9cd64686 | 6686 | ggc_add_root (&scope_chain, 1, sizeof scope_chain, &mark_saved_scope); |
fc6af6e3 RH |
6687 | ggc_add_tree_root (&static_ctors, 1); |
6688 | ggc_add_tree_root (&static_dtors, 1); | |
9cd64686 | 6689 | ggc_add_tree_root (&lastiddecl, 1); |
fc6af6e3 | 6690 | |
9cd64686 | 6691 | ggc_add_tree_root (&last_function_parms, 1); |
fc6af6e3 | 6692 | ggc_add_tree_root (&error_mark_list, 1); |
9cd64686 | 6693 | |
fc6af6e3 | 6694 | ggc_add_tree_root (&global_namespace, 1); |
fc6af6e3 RH |
6695 | ggc_add_tree_root (&global_type_node, 1); |
6696 | ggc_add_tree_root (&anonymous_namespace_name, 1); | |
9cd64686 MM |
6697 | |
6698 | ggc_add_tree_root (&got_object, 1); | |
6699 | ggc_add_tree_root (&got_scope, 1); | |
6700 | ||
6701 | ggc_add_tree_root (¤t_lang_name, 1); | |
6702 | ggc_add_tree_root (&static_aggregates, 1); | |
4890c2f4 | 6703 | ggc_add_tree_root (&free_bindings, 1); |
0154eaa8 | 6704 | ggc_add_tree_root (&incomplete_vars, 1); |
62c154ed JM |
6705 | } |
6706 | ||
0ba8a114 NS |
6707 | /* Generate an initializer for a function naming variable from |
6708 | NAME. NAME may be NULL, in which case we generate a special | |
6709 | ERROR_MARK node which should be replaced later. */ | |
6710 | ||
6711 | tree | |
6712 | cp_fname_init (name) | |
6713 | const char *name; | |
6714 | { | |
6715 | tree domain = NULL_TREE; | |
6716 | tree type; | |
6717 | tree init = NULL_TREE; | |
6718 | size_t length = 0; | |
6719 | ||
6720 | if (name) | |
6721 | { | |
6722 | length = strlen (name); | |
6723 | domain = build_index_type (size_int (length)); | |
6724 | init = build_string (length + 1, name); | |
6725 | } | |
6726 | ||
6727 | type = build_qualified_type (char_type_node, TYPE_QUAL_CONST); | |
6728 | type = build_cplus_array_type (type, domain); | |
6729 | ||
6730 | if (init) | |
6731 | TREE_TYPE (init) = type; | |
6732 | else | |
6733 | /* We don't know the value until instantiation time. Make | |
6734 | something which will be digested now, but replaced later. */ | |
6735 | init = build (ERROR_MARK, type); | |
6736 | ||
6737 | return init; | |
6738 | } | |
6739 | ||
2ce07e2d NS |
6740 | /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the |
6741 | decl, NAME is the initialization string and TYPE_DEP indicates whether | |
6742 | NAME depended on the type of the function. We make use of that to detect | |
0ba8a114 NS |
6743 | __PRETTY_FUNCTION__ inside a template fn. This is being done |
6744 | lazily at the point of first use, so we musn't push the decl now. */ | |
2ce07e2d NS |
6745 | |
6746 | static tree | |
0ba8a114 | 6747 | cp_make_fname_decl (id, type_dep) |
2ce07e2d | 6748 | tree id; |
2ce07e2d NS |
6749 | int type_dep; |
6750 | { | |
83182544 | 6751 | const char *const name = (type_dep && processing_template_decl |
0ba8a114 NS |
6752 | ? NULL : fname_as_string (type_dep)); |
6753 | tree init = cp_fname_init (name); | |
6754 | tree decl = build_decl (VAR_DECL, id, TREE_TYPE (init)); | |
2ce07e2d | 6755 | |
0ba8a114 NS |
6756 | /* As we don't push the decl here, we must set the context. */ |
6757 | DECL_CONTEXT (decl) = current_function_decl; | |
6758 | DECL_PRETTY_FUNCTION_P (decl) = type_dep; | |
6759 | ||
2ce07e2d NS |
6760 | TREE_STATIC (decl) = 1; |
6761 | TREE_READONLY (decl) = 1; | |
2ce07e2d | 6762 | DECL_ARTIFICIAL (decl) = 1; |
2ce07e2d | 6763 | DECL_INITIAL (decl) = init; |
0ba8a114 NS |
6764 | |
6765 | TREE_USED (decl) = 1; | |
5362b086 | 6766 | |
0ba8a114 NS |
6767 | cp_finish_decl (decl, init, NULL_TREE, LOOKUP_ONLYCONVERTING); |
6768 | ||
2ce07e2d NS |
6769 | return decl; |
6770 | } | |
6771 | ||
d52e4867 RS |
6772 | /* Make a definition for a builtin function named NAME in the current |
6773 | namespace, whose data type is TYPE and whose context is CONTEXT. | |
6774 | TYPE should be a function type with argument types. | |
8d08fdba | 6775 | |
0c11ada6 JM |
6776 | CLASS and CODE tell later passes how to compile calls to this function. |
6777 | See tree.h for possible values. | |
6778 | ||
6779 | If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME, | |
8d08fdba MS |
6780 | the name to be called if we can't opencode the function. */ |
6781 | ||
d52e4867 RS |
6782 | static tree |
6783 | builtin_function_1 (name, type, context, code, class, libname) | |
d8e178a0 | 6784 | const char *name; |
8d08fdba | 6785 | tree type; |
d52e4867 | 6786 | tree context; |
0c11ada6 JM |
6787 | int code; |
6788 | enum built_in_class class; | |
6789 | const char *libname; | |
8d08fdba | 6790 | { |
596ea4e5 | 6791 | tree decl = build_library_fn_1 (get_identifier (name), ERROR_MARK, type); |
0c11ada6 JM |
6792 | DECL_BUILT_IN_CLASS (decl) = class; |
6793 | DECL_FUNCTION_CODE (decl) = code; | |
d52e4867 | 6794 | DECL_CONTEXT (decl) = context; |
12a22e76 | 6795 | |
57d5032b JM |
6796 | /* The return builtins leave the current function. */ |
6797 | if (code == BUILT_IN_RETURN || code == BUILT_IN_EH_RETURN) | |
6798 | TREE_THIS_VOLATILE (decl) = 1; | |
6799 | ||
6bcedb4e | 6800 | pushdecl (decl); |
6bcedb4e | 6801 | |
8d08fdba MS |
6802 | /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME, |
6803 | we cannot change DECL_ASSEMBLER_NAME until we have installed this | |
6804 | function in the namespace. */ | |
0c11ada6 | 6805 | if (libname) |
92643fea MM |
6806 | SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname)); |
6807 | make_decl_rtl (decl, NULL); | |
935d1834 ZW |
6808 | |
6809 | /* Warn if a function in the namespace for users | |
6810 | is used without an occasion to consider it declared. */ | |
6811 | if (name[0] != '_' || name[1] != '_') | |
6812 | DECL_ANTICIPATED (decl) = 1; | |
6813 | ||
6431177a JM |
6814 | /* Possibly apply some default attributes to this built-in function. */ |
6815 | decl_attributes (&decl, NULL_TREE, 0); | |
6816 | ||
8d08fdba MS |
6817 | return decl; |
6818 | } | |
7f4edbcb | 6819 | |
d52e4867 RS |
6820 | /* Entry point for the benefit of c_common_nodes_and_builtins. |
6821 | ||
6822 | Make a defintion for a builtin function named NAME and whose data type | |
6823 | is TYPE. TYPE should be a function type with argument types. This | |
6824 | function places the anticipated declaration in the global namespace | |
6825 | and additionally in the std namespace if appropriate. | |
6826 | ||
6827 | CLASS and CODE tell later passes how to compile calls to this function. | |
6828 | See tree.h for possible values. | |
6829 | ||
6830 | If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME, | |
6831 | the name to be called if we can't opencode the function. */ | |
6832 | ||
6833 | tree | |
6834 | builtin_function (name, type, code, class, libname) | |
6835 | const char *name; | |
6836 | tree type; | |
6837 | int code; | |
6838 | enum built_in_class class; | |
6839 | const char *libname; | |
6840 | { | |
6841 | /* All builtins that don't begin with an '_' should additionally | |
6842 | go in the 'std' namespace. */ | |
6843 | if (name[0] != '_') | |
6844 | { | |
6845 | push_namespace (std_identifier); | |
6846 | builtin_function_1 (name, type, std_node, code, class, libname); | |
6847 | pop_namespace (); | |
6848 | } | |
6849 | ||
6850 | return builtin_function_1 (name, type, NULL_TREE, code, class, libname); | |
6851 | } | |
6852 | ||
0c11ada6 JM |
6853 | /* Generate a FUNCTION_DECL with the typical flags for a runtime library |
6854 | function. Not called directly. */ | |
6855 | ||
6856 | static tree | |
596ea4e5 | 6857 | build_library_fn_1 (name, operator_code, type) |
0c11ada6 | 6858 | tree name; |
596ea4e5 | 6859 | enum tree_code operator_code; |
0c11ada6 JM |
6860 | tree type; |
6861 | { | |
6862 | tree fn = build_lang_decl (FUNCTION_DECL, name, type); | |
6863 | DECL_EXTERNAL (fn) = 1; | |
6864 | TREE_PUBLIC (fn) = 1; | |
6865 | DECL_ARTIFICIAL (fn) = 1; | |
6866 | TREE_NOTHROW (fn) = 1; | |
596ea4e5 | 6867 | SET_OVERLOADED_OPERATOR_CODE (fn, operator_code); |
5d2ed28c | 6868 | SET_DECL_LANGUAGE (fn, lang_c); |
0c11ada6 JM |
6869 | return fn; |
6870 | } | |
c2a37c55 | 6871 | |
0c11ada6 JM |
6872 | /* Returns the _DECL for a library function with C linkage. |
6873 | We assume that such functions never throw; if this is incorrect, | |
6874 | callers should unset TREE_NOTHROW. */ | |
c2a37c55 | 6875 | |
7f4edbcb | 6876 | tree |
0c11ada6 JM |
6877 | build_library_fn (name, type) |
6878 | tree name; | |
6879 | tree type; | |
6880 | { | |
19e7881c | 6881 | return build_library_fn_1 (name, ERROR_MARK, type); |
0c11ada6 JM |
6882 | } |
6883 | ||
6884 | /* Returns the _DECL for a library function with C++ linkage. */ | |
6885 | ||
596ea4e5 AS |
6886 | static tree |
6887 | build_cp_library_fn (name, operator_code, type) | |
0c11ada6 | 6888 | tree name; |
596ea4e5 | 6889 | enum tree_code operator_code; |
0c11ada6 JM |
6890 | tree type; |
6891 | { | |
596ea4e5 | 6892 | tree fn = build_library_fn_1 (name, operator_code, type); |
0c11ada6 | 6893 | TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type); |
6bbf1598 | 6894 | DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace); |
5d2ed28c | 6895 | SET_DECL_LANGUAGE (fn, lang_cplusplus); |
0c11ada6 | 6896 | set_mangled_name_for_decl (fn); |
0c11ada6 JM |
6897 | return fn; |
6898 | } | |
6899 | ||
6900 | /* Like build_library_fn, but takes a C string instead of an | |
6901 | IDENTIFIER_NODE. */ | |
6902 | ||
6903 | tree | |
6904 | build_library_fn_ptr (name, type) | |
7f4edbcb BS |
6905 | const char *name; |
6906 | tree type; | |
7f4edbcb | 6907 | { |
0c11ada6 JM |
6908 | return build_library_fn (get_identifier (name), type); |
6909 | } | |
6910 | ||
6911 | /* Like build_cp_library_fn, but takes a C string instead of an | |
6912 | IDENTIFIER_NODE. */ | |
6913 | ||
6914 | tree | |
6915 | build_cp_library_fn_ptr (name, type) | |
6916 | const char *name; | |
6917 | tree type; | |
6918 | { | |
596ea4e5 | 6919 | return build_cp_library_fn (get_identifier (name), ERROR_MARK, type); |
0c11ada6 JM |
6920 | } |
6921 | ||
6922 | /* Like build_library_fn, but also pushes the function so that we will | |
6923 | be able to find it via IDENTIFIER_GLOBAL_VALUE. */ | |
6924 | ||
6925 | tree | |
6926 | push_library_fn (name, type) | |
6927 | tree name, type; | |
6928 | { | |
6929 | tree fn = build_library_fn (name, type); | |
6930 | pushdecl_top_level (fn); | |
6931 | return fn; | |
6932 | } | |
6933 | ||
6934 | /* Like build_cp_library_fn, but also pushes the function so that it | |
6935 | will be found by normal lookup. */ | |
6936 | ||
596ea4e5 AS |
6937 | static tree |
6938 | push_cp_library_fn (operator_code, type) | |
6939 | enum tree_code operator_code; | |
0c11ada6 JM |
6940 | tree type; |
6941 | { | |
5362b086 | 6942 | tree fn = build_cp_library_fn (ansi_opname (operator_code), |
596ea4e5 AS |
6943 | operator_code, |
6944 | type); | |
0c11ada6 JM |
6945 | pushdecl (fn); |
6946 | return fn; | |
6947 | } | |
6948 | ||
6949 | /* Like push_library_fn, but takes a TREE_LIST of parm types rather than | |
6950 | a FUNCTION_TYPE. */ | |
6951 | ||
6952 | tree | |
6953 | push_void_library_fn (name, parmtypes) | |
6954 | tree name, parmtypes; | |
6955 | { | |
6956 | tree type = build_function_type (void_type_node, parmtypes); | |
6957 | return push_library_fn (name, type); | |
6958 | } | |
6959 | ||
cf74fb86 | 6960 | /* Like push_library_fn, but also note that this function throws |
0c11ada6 JM |
6961 | and does not return. Used for __throw_foo and the like. */ |
6962 | ||
6963 | tree | |
cf74fb86 JM |
6964 | push_throw_library_fn (name, type) |
6965 | tree name, type; | |
0c11ada6 | 6966 | { |
cf74fb86 | 6967 | tree fn = push_library_fn (name, type); |
0c11ada6 JM |
6968 | TREE_THIS_VOLATILE (fn) = 1; |
6969 | TREE_NOTHROW (fn) = 0; | |
6970 | return fn; | |
7f4edbcb | 6971 | } |
6431177a JM |
6972 | |
6973 | /* Apply default attributes to a function, if a system function with default | |
6974 | attributes. */ | |
6975 | ||
6976 | void | |
7ffb4fd2 | 6977 | cxx_insert_default_attributes (decl) |
6431177a JM |
6978 | tree decl; |
6979 | { | |
6980 | if (!DECL_EXTERN_C_FUNCTION_P (decl)) | |
6981 | return; | |
6982 | if (!TREE_PUBLIC (decl)) | |
6983 | return; | |
6984 | c_common_insert_default_attributes (decl); | |
6985 | } | |
8d08fdba | 6986 | \f |
61a127b3 MM |
6987 | /* When we call finish_struct for an anonymous union, we create |
6988 | default copy constructors and such. But, an anonymous union | |
6989 | shouldn't have such things; this function undoes the damage to the | |
6990 | anonymous union type T. | |
6991 | ||
6992 | (The reason that we create the synthesized methods is that we don't | |
6993 | distinguish `union { int i; }' from `typedef union { int i; } U'. | |
6994 | The first is an anonymous union; the second is just an ordinary | |
6995 | union type.) */ | |
6996 | ||
6997 | void | |
6bdb8141 | 6998 | fixup_anonymous_aggr (t) |
61a127b3 MM |
6999 | tree t; |
7000 | { | |
7001 | tree *q; | |
7002 | ||
7003 | /* Wipe out memory of synthesized methods */ | |
7004 | TYPE_HAS_CONSTRUCTOR (t) = 0; | |
7005 | TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0; | |
7006 | TYPE_HAS_INIT_REF (t) = 0; | |
7007 | TYPE_HAS_CONST_INIT_REF (t) = 0; | |
7008 | TYPE_HAS_ASSIGN_REF (t) = 0; | |
61a127b3 MM |
7009 | TYPE_HAS_CONST_ASSIGN_REF (t) = 0; |
7010 | ||
7011 | /* Splice the implicitly generated functions out of the TYPE_METHODS | |
7012 | list. */ | |
7013 | q = &TYPE_METHODS (t); | |
7014 | while (*q) | |
7015 | { | |
7016 | if (DECL_ARTIFICIAL (*q)) | |
7017 | *q = TREE_CHAIN (*q); | |
7018 | else | |
7019 | q = &TREE_CHAIN (*q); | |
7020 | } | |
7021 | ||
cab1f180 | 7022 | /* ISO C++ 9.5.3. Anonymous unions may not have function members. */ |
61a127b3 | 7023 | if (TYPE_METHODS (t)) |
1f0d71c5 | 7024 | cp_error_at ("an anonymous union cannot have function members", t); |
a1c2b86d JJ |
7025 | |
7026 | /* Anonymous aggregates cannot have fields with ctors, dtors or complex | |
7027 | assignment operators (because they cannot have these methods themselves). | |
7028 | For anonymous unions this is already checked because they are not allowed | |
7029 | in any union, otherwise we have to check it. */ | |
7030 | if (TREE_CODE (t) != UNION_TYPE) | |
7031 | { | |
7032 | tree field, type; | |
7033 | ||
7034 | for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field)) | |
7035 | if (TREE_CODE (field) == FIELD_DECL) | |
7036 | { | |
7037 | type = TREE_TYPE (field); | |
7038 | if (CLASS_TYPE_P (type)) | |
7039 | { | |
7040 | if (TYPE_NEEDS_CONSTRUCTING (type)) | |
7041 | cp_error_at ("member %#D' with constructor not allowed in anonymous aggregate", | |
7042 | field); | |
7043 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) | |
7044 | cp_error_at ("member %#D' with destructor not allowed in anonymous aggregate", | |
7045 | field); | |
7046 | if (TYPE_HAS_COMPLEX_ASSIGN_REF (type)) | |
7047 | cp_error_at ("member %#D' with copy assignment operator not allowed in anonymous aggregate", | |
7048 | field); | |
7049 | } | |
7050 | } | |
7051 | } | |
61a127b3 MM |
7052 | } |
7053 | ||
72a93143 JM |
7054 | /* Make sure that a declaration with no declarator is well-formed, i.e. |
7055 | just defines a tagged type or anonymous union. | |
8d08fdba | 7056 | |
72a93143 | 7057 | Returns the type defined, if any. */ |
8d08fdba | 7058 | |
72a93143 JM |
7059 | tree |
7060 | check_tag_decl (declspecs) | |
8d08fdba MS |
7061 | tree declspecs; |
7062 | { | |
72a93143 | 7063 | int found_type = 0; |
2bdb0643 | 7064 | int saw_friend = 0; |
e8186ecf | 7065 | int saw_typedef = 0; |
2986ae00 | 7066 | tree ob_modifier = NULL_TREE; |
8d08fdba | 7067 | register tree link; |
8d08fdba MS |
7068 | register tree t = NULL_TREE; |
7069 | ||
7070 | for (link = declspecs; link; link = TREE_CHAIN (link)) | |
7071 | { | |
7072 | register tree value = TREE_VALUE (link); | |
7073 | ||
2bdb0643 | 7074 | if (TYPE_P (value) |
bd0d5d4a | 7075 | || TREE_CODE (value) == TYPE_DECL |
2bdb0643 JM |
7076 | || (TREE_CODE (value) == IDENTIFIER_NODE |
7077 | && IDENTIFIER_GLOBAL_VALUE (value) | |
bd0d5d4a | 7078 | && TREE_CODE (IDENTIFIER_GLOBAL_VALUE (value)) == TYPE_DECL)) |
8d08fdba | 7079 | { |
72a93143 | 7080 | ++found_type; |
5566b478 | 7081 | |
bd0d5d4a JM |
7082 | if (found_type == 2 && TREE_CODE (value) == IDENTIFIER_NODE) |
7083 | { | |
7084 | if (! in_system_header) | |
33bd39a2 | 7085 | pedwarn ("redeclaration of C++ built-in type `%T'", value); |
bd0d5d4a JM |
7086 | return NULL_TREE; |
7087 | } | |
7088 | ||
7089 | if (TYPE_P (value) | |
7090 | && ((TREE_CODE (value) != TYPENAME_TYPE && IS_AGGR_TYPE (value)) | |
7091 | || TREE_CODE (value) == ENUMERAL_TYPE)) | |
72a93143 JM |
7092 | { |
7093 | my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261); | |
7094 | t = value; | |
7095 | } | |
8d08fdba | 7096 | } |
e8186ecf GDR |
7097 | else if (value == ridpointers[(int) RID_TYPEDEF]) |
7098 | saw_typedef = 1; | |
83f660b7 JM |
7099 | else if (value == ridpointers[(int) RID_FRIEND]) |
7100 | { | |
83f660b7 JM |
7101 | if (current_class_type == NULL_TREE |
7102 | || current_scope () != current_class_type) | |
7103 | ob_modifier = value; | |
2bdb0643 JM |
7104 | else |
7105 | saw_friend = 1; | |
83f660b7 | 7106 | } |
8d08fdba | 7107 | else if (value == ridpointers[(int) RID_STATIC] |
2986ae00 MS |
7108 | || value == ridpointers[(int) RID_EXTERN] |
7109 | || value == ridpointers[(int) RID_AUTO] | |
28cbf42c MS |
7110 | || value == ridpointers[(int) RID_REGISTER] |
7111 | || value == ridpointers[(int) RID_INLINE] | |
7112 | || value == ridpointers[(int) RID_VIRTUAL] | |
72a93143 JM |
7113 | || value == ridpointers[(int) RID_CONST] |
7114 | || value == ridpointers[(int) RID_VOLATILE] | |
28cbf42c | 7115 | || value == ridpointers[(int) RID_EXPLICIT]) |
2986ae00 | 7116 | ob_modifier = value; |
8d08fdba MS |
7117 | } |
7118 | ||
72a93143 JM |
7119 | if (found_type > 1) |
7120 | error ("multiple types in one declaration"); | |
7e2067ca | 7121 | |
2bdb0643 | 7122 | if (t == NULL_TREE && ! saw_friend) |
7e2067ca | 7123 | pedwarn ("declaration does not declare anything"); |
0dd3962d | 7124 | |
1951a1b6 | 7125 | /* Check for an anonymous union. */ |
6bdb8141 | 7126 | else if (t && IS_AGGR_TYPE_CODE (TREE_CODE (t)) |
1951a1b6 | 7127 | && TYPE_ANONYMOUS_P (t)) |
0dd3962d | 7128 | { |
e8186ecf GDR |
7129 | /* 7/3 In a simple-declaration, the optional init-declarator-list |
7130 | can be omitted only when declaring a class (clause 9) or | |
7131 | enumeration (7.2), that is, when the decl-specifier-seq contains | |
7132 | either a class-specifier, an elaborated-type-specifier with | |
7133 | a class-key (9.1), or an enum-specifier. In these cases and | |
7134 | whenever a class-specifier or enum-specifier is present in the | |
7135 | decl-specifier-seq, the identifiers in these specifiers are among | |
7136 | the names being declared by the declaration (as class-name, | |
7137 | enum-names, or enumerators, depending on the syntax). In such | |
7138 | cases, and except for the declaration of an unnamed bit-field (9.6), | |
7139 | the decl-specifier-seq shall introduce one or more names into the | |
7140 | program, or shall redeclare a name introduced by a previous | |
7141 | declaration. [Example: | |
7142 | enum { }; // ill-formed | |
7143 | typedef class { }; // ill-formed | |
7144 | --end example] */ | |
7145 | if (saw_typedef) | |
7146 | { | |
357351e5 | 7147 | error ("missing type-name in typedef-declaration"); |
e8186ecf GDR |
7148 | return NULL_TREE; |
7149 | } | |
0dd3962d | 7150 | /* Anonymous unions are objects, so they can have specifiers. */; |
6bdb8141 JM |
7151 | SET_ANON_AGGR_TYPE_P (t); |
7152 | ||
7153 | if (TREE_CODE (t) != UNION_TYPE && pedantic && ! in_system_header) | |
7154 | pedwarn ("ISO C++ prohibits anonymous structs"); | |
0dd3962d JM |
7155 | } |
7156 | ||
83f660b7 | 7157 | else if (ob_modifier) |
8d08fdba | 7158 | { |
83f660b7 JM |
7159 | if (ob_modifier == ridpointers[(int) RID_INLINE] |
7160 | || ob_modifier == ridpointers[(int) RID_VIRTUAL]) | |
33bd39a2 | 7161 | error ("`%D' can only be specified for functions", ob_modifier); |
83f660b7 | 7162 | else if (ob_modifier == ridpointers[(int) RID_FRIEND]) |
33bd39a2 | 7163 | error ("`%D' can only be specified inside a class", ob_modifier); |
83f660b7 | 7164 | else if (ob_modifier == ridpointers[(int) RID_EXPLICIT]) |
33bd39a2 | 7165 | error ("`%D' can only be specified for constructors", |
83f660b7 JM |
7166 | ob_modifier); |
7167 | else | |
33bd39a2 | 7168 | error ("`%D' can only be specified for objects and functions", |
83f660b7 | 7169 | ob_modifier); |
72a93143 | 7170 | } |
8d08fdba | 7171 | |
72a93143 JM |
7172 | return t; |
7173 | } | |
7174 | ||
7175 | /* Called when a declaration is seen that contains no names to declare. | |
7176 | If its type is a reference to a structure, union or enum inherited | |
7177 | from a containing scope, shadow that tag name for the current scope | |
7178 | with a forward reference. | |
7179 | If its type defines a new named structure or union | |
7180 | or defines an enum, it is valid but we need not do anything here. | |
7181 | Otherwise, it is an error. | |
7182 | ||
7183 | C++: may have to grok the declspecs to learn about static, | |
7184 | complain for anonymous unions. */ | |
7185 | ||
7186 | void | |
7187 | shadow_tag (declspecs) | |
7188 | tree declspecs; | |
7189 | { | |
7190 | tree t = check_tag_decl (declspecs); | |
7191 | ||
7192 | if (t) | |
7193 | maybe_process_partial_specialization (t); | |
7194 | ||
7195 | /* This is where the variables in an anonymous union are | |
7196 | declared. An anonymous union declaration looks like: | |
7197 | union { ... } ; | |
7198 | because there is no declarator after the union, the parser | |
7199 | sends that declaration here. */ | |
6bdb8141 | 7200 | if (t && ANON_AGGR_TYPE_P (t)) |
72a93143 | 7201 | { |
6bdb8141 | 7202 | fixup_anonymous_aggr (t); |
72a93143 JM |
7203 | |
7204 | if (TYPE_FIELDS (t)) | |
7205 | { | |
7206 | tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0, | |
91d231cb | 7207 | NULL); |
72a93143 JM |
7208 | finish_anon_union (decl); |
7209 | } | |
8d08fdba MS |
7210 | } |
7211 | } | |
7212 | \f | |
7213 | /* Decode a "typename", such as "int **", returning a ..._TYPE node. */ | |
7214 | ||
7215 | tree | |
7216 | groktypename (typename) | |
7217 | tree typename; | |
7218 | { | |
98884b26 JM |
7219 | tree specs, attrs; |
7220 | tree type; | |
8d08fdba MS |
7221 | if (TREE_CODE (typename) != TREE_LIST) |
7222 | return typename; | |
98884b26 JM |
7223 | split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs); |
7224 | type = grokdeclarator (TREE_VALUE (typename), specs, | |
7225 | TYPENAME, 0, &attrs); | |
7226 | if (attrs) | |
7227 | cplus_decl_attributes (&type, attrs, 0); | |
7228 | return type; | |
8d08fdba MS |
7229 | } |
7230 | ||
7231 | /* Decode a declarator in an ordinary declaration or data definition. | |
7232 | This is called as soon as the type information and variable name | |
7233 | have been parsed, before parsing the initializer if any. | |
7234 | Here we create the ..._DECL node, fill in its type, | |
7235 | and put it on the list of decls for the current context. | |
7236 | The ..._DECL node is returned as the value. | |
7237 | ||
7238 | Exception: for arrays where the length is not specified, | |
82580166 | 7239 | the type is left null, to be filled in by `cp_finish_decl'. |
8d08fdba MS |
7240 | |
7241 | Function definitions do not come here; they go to start_function | |
7242 | instead. However, external and forward declarations of functions | |
7243 | do go through here. Structure field declarations are done by | |
7244 | grokfield and not through here. */ | |
7245 | ||
8d08fdba | 7246 | tree |
a1774733 | 7247 | start_decl (declarator, declspecs, initialized, attributes, prefix_attributes) |
8d08fdba MS |
7248 | tree declarator, declspecs; |
7249 | int initialized; | |
a1774733 | 7250 | tree attributes, prefix_attributes; |
8d08fdba | 7251 | { |
59387d2e | 7252 | tree decl; |
8d08fdba MS |
7253 | register tree type, tem; |
7254 | tree context; | |
7255 | extern int have_extern_spec; | |
7256 | extern int used_extern_spec; | |
7257 | ||
5566b478 MS |
7258 | #if 0 |
7259 | /* See code below that used this. */ | |
8d08fdba | 7260 | int init_written = initialized; |
5566b478 | 7261 | #endif |
8d08fdba | 7262 | |
e92cc029 | 7263 | /* This should only be done once on the top most decl. */ |
8d08fdba MS |
7264 | if (have_extern_spec && !used_extern_spec) |
7265 | { | |
1f8f4a0b MM |
7266 | declspecs = tree_cons (NULL_TREE, get_identifier ("extern"), |
7267 | declspecs); | |
8d08fdba MS |
7268 | used_extern_spec = 1; |
7269 | } | |
7270 | ||
e23bd218 IR |
7271 | /* An object declared as __attribute__((deprecated)) suppresses |
7272 | warnings of uses of other deprecated items. */ | |
7273 | if (lookup_attribute ("deprecated", attributes)) | |
7274 | deprecated_state = DEPRECATED_SUPPRESS; | |
7275 | ||
91d231cb | 7276 | attributes = chainon (attributes, prefix_attributes); |
b17e2870 | 7277 | |
c11b6f21 | 7278 | decl = grokdeclarator (declarator, declspecs, NORMAL, initialized, |
91d231cb | 7279 | &attributes); |
68642fb6 | 7280 | |
e23bd218 IR |
7281 | deprecated_state = DEPRECATED_NORMAL; |
7282 | ||
a1774733 | 7283 | if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE) |
8d08fdba MS |
7284 | return NULL_TREE; |
7285 | ||
7286 | type = TREE_TYPE (decl); | |
7287 | ||
44689c12 ML |
7288 | if (type == error_mark_node) |
7289 | return NULL_TREE; | |
7290 | ||
4f1c5b7d | 7291 | context = DECL_CONTEXT (decl); |
8d08fdba | 7292 | |
9a68c51f JM |
7293 | if (initialized && context && TREE_CODE (context) == NAMESPACE_DECL |
7294 | && context != current_namespace && TREE_CODE (decl) == VAR_DECL) | |
7295 | { | |
7296 | /* When parsing the initializer, lookup should use the object's | |
7297 | namespace. */ | |
7298 | push_decl_namespace (context); | |
7299 | } | |
7300 | ||
2c73f9f5 ML |
7301 | /* We are only interested in class contexts, later. */ |
7302 | if (context && TREE_CODE (context) == NAMESPACE_DECL) | |
7303 | context = NULL_TREE; | |
7304 | ||
8d08fdba MS |
7305 | if (initialized) |
7306 | /* Is it valid for this decl to have an initializer at all? | |
7307 | If not, set INITIALIZED to zero, which will indirectly | |
82580166 | 7308 | tell `cp_finish_decl' to ignore the initializer once it is parsed. */ |
8d08fdba MS |
7309 | switch (TREE_CODE (decl)) |
7310 | { | |
7311 | case TYPE_DECL: | |
7312 | /* typedef foo = bar means give foo the same type as bar. | |
82580166 | 7313 | We haven't parsed bar yet, so `cp_finish_decl' will fix that up. |
8d08fdba MS |
7314 | Any other case of an initialization in a TYPE_DECL is an error. */ |
7315 | if (pedantic || list_length (declspecs) > 1) | |
7316 | { | |
33bd39a2 | 7317 | error ("typedef `%D' is initialized", decl); |
8d08fdba MS |
7318 | initialized = 0; |
7319 | } | |
7320 | break; | |
7321 | ||
7322 | case FUNCTION_DECL: | |
33bd39a2 | 7323 | error ("function `%#D' is initialized like a variable", decl); |
8d08fdba MS |
7324 | initialized = 0; |
7325 | break; | |
7326 | ||
7327 | default: | |
3e41d13b | 7328 | break; |
8d08fdba MS |
7329 | } |
7330 | ||
8d08fdba MS |
7331 | if (initialized) |
7332 | { | |
a9aedbc2 | 7333 | if (! toplevel_bindings_p () |
8d08fdba | 7334 | && DECL_EXTERNAL (decl)) |
33bd39a2 | 7335 | warning ("declaration of `%#D' has `extern' and is initialized", |
8d08fdba MS |
7336 | decl); |
7337 | DECL_EXTERNAL (decl) = 0; | |
5566b478 | 7338 | if (toplevel_bindings_p ()) |
8d08fdba MS |
7339 | TREE_STATIC (decl) = 1; |
7340 | ||
7341 | /* Tell `pushdecl' this is an initialized decl | |
7342 | even though we don't yet have the initializer expression. | |
82580166 | 7343 | Also tell `cp_finish_decl' it may store the real initializer. */ |
8d08fdba MS |
7344 | DECL_INITIAL (decl) = error_mark_node; |
7345 | } | |
7346 | ||
fa20888b | 7347 | /* Set attributes here so if duplicate decl, will have proper attributes. */ |
91d231cb | 7348 | cplus_decl_attributes (&decl, attributes, 0); |
fa20888b | 7349 | |
ecb0eece RH |
7350 | /* If #pragma weak was used, mark the decl weak now. */ |
7351 | if (current_binding_level == global_binding_level) | |
7352 | maybe_apply_pragma_weak (decl); | |
7353 | ||
97055d5c AO |
7354 | if (TREE_CODE (decl) == FUNCTION_DECL |
7355 | && DECL_DECLARED_INLINE_P (decl) | |
7356 | && DECL_UNINLINABLE (decl) | |
7357 | && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl))) | |
7358 | warning_with_decl (decl, | |
7359 | "inline function `%s' given attribute noinline"); | |
7360 | ||
d0f062fb | 7361 | if (context && COMPLETE_TYPE_P (complete_type (context))) |
5b605f68 | 7362 | { |
6b400b21 | 7363 | push_nested_class (context, 2); |
e97e5263 | 7364 | |
5b605f68 MS |
7365 | if (TREE_CODE (decl) == VAR_DECL) |
7366 | { | |
7367 | tree field = lookup_field (context, DECL_NAME (decl), 0, 0); | |
7368 | if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL) | |
33bd39a2 | 7369 | error ("`%#D' is not a static member of `%#T'", decl, context); |
e349ee73 MS |
7370 | else |
7371 | { | |
7372 | if (DECL_CONTEXT (field) != context) | |
f2d773a2 | 7373 | { |
33bd39a2 | 7374 | pedwarn ("ISO C++ does not permit `%T::%D' to be defined as `%T::%D'", |
f2d773a2 JM |
7375 | DECL_CONTEXT (field), DECL_NAME (decl), |
7376 | context, DECL_NAME (decl)); | |
7377 | DECL_CONTEXT (decl) = DECL_CONTEXT (field); | |
7378 | } | |
75650646 MM |
7379 | /* Static data member are tricky; an in-class initialization |
7380 | still doesn't provide a definition, so the in-class | |
7381 | declaration will have DECL_EXTERNAL set, but will have an | |
7382 | initialization. Thus, duplicate_decls won't warn | |
7383 | about this situation, and so we check here. */ | |
7384 | if (DECL_INITIAL (decl) && DECL_INITIAL (field)) | |
33bd39a2 | 7385 | error ("duplicate initialization of %D", decl); |
e349ee73 MS |
7386 | if (duplicate_decls (decl, field)) |
7387 | decl = field; | |
7388 | } | |
5b605f68 | 7389 | } |
f30432d7 MS |
7390 | else |
7391 | { | |
5566b478 | 7392 | tree field = check_classfn (context, decl); |
f30432d7 MS |
7393 | if (field && duplicate_decls (decl, field)) |
7394 | decl = field; | |
7395 | } | |
7396 | ||
7397 | /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */ | |
dff3e828 | 7398 | DECL_IN_AGGR_P (decl) = 0; |
68642fb6 | 7399 | if ((DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl)) |
1f6e1acc | 7400 | || CLASSTYPE_TEMPLATE_INSTANTIATION (context)) |
84e6233f JM |
7401 | { |
7402 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
7403 | /* [temp.expl.spec] An explicit specialization of a static data | |
7404 | member of a template is a definition if the declaration | |
7405 | includes an initializer; otherwise, it is a declaration. | |
7406 | ||
7407 | We check for processing_specialization so this only applies | |
7408 | to the new specialization syntax. */ | |
7409 | if (DECL_INITIAL (decl) == NULL_TREE && processing_specialization) | |
7410 | DECL_EXTERNAL (decl) = 1; | |
7411 | } | |
f30432d7 | 7412 | |
b7698cf0 | 7413 | if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)) |
33bd39a2 | 7414 | pedwarn ("declaration of `%#D' outside of class is not definition", |
f30432d7 | 7415 | decl); |
5b605f68 MS |
7416 | } |
7417 | ||
9188c363 MM |
7418 | /* Enter this declaration into the symbol table. */ |
7419 | tem = maybe_push_decl (decl); | |
2ee887f2 | 7420 | |
5156628f | 7421 | if (processing_template_decl) |
cd9f6678 | 7422 | tem = push_template_decl (tem); |
5566b478 | 7423 | |
2ee887f2 | 7424 | #if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS) |
a3203465 | 7425 | /* Tell the back-end to use or not use .common as appropriate. If we say |
a50f0918 MS |
7426 | -fconserve-space, we want this to save .data space, at the expense of |
7427 | wrong semantics. If we say -fno-conserve-space, we want this to | |
7428 | produce errors about redefs; to do this we force variables into the | |
7429 | data segment. */ | |
a3203465 | 7430 | DECL_COMMON (tem) = flag_conserve_space || ! TREE_PUBLIC (tem); |
2ee887f2 | 7431 | #endif |
68642fb6 | 7432 | |
5156628f | 7433 | if (! processing_template_decl) |
5566b478 | 7434 | start_decl_1 (tem); |
8d08fdba | 7435 | |
8d08fdba MS |
7436 | return tem; |
7437 | } | |
7438 | ||
5566b478 MS |
7439 | void |
7440 | start_decl_1 (decl) | |
7441 | tree decl; | |
8d08fdba | 7442 | { |
5566b478 MS |
7443 | tree type = TREE_TYPE (decl); |
7444 | int initialized = (DECL_INITIAL (decl) != NULL_TREE); | |
8d08fdba | 7445 | |
44689c12 ML |
7446 | if (type == error_mark_node) |
7447 | return; | |
7448 | ||
bd0d5d4a | 7449 | maybe_push_cleanup_level (type); |
5566b478 MS |
7450 | |
7451 | if (initialized) | |
7452 | /* Is it valid for this decl to have an initializer at all? | |
7453 | If not, set INITIALIZED to zero, which will indirectly | |
7454 | tell `cp_finish_decl' to ignore the initializer once it is parsed. */ | |
8d08fdba | 7455 | { |
5566b478 MS |
7456 | /* Don't allow initializations for incomplete types except for |
7457 | arrays which might be completed by the initialization. */ | |
d0f062fb | 7458 | if (COMPLETE_TYPE_P (complete_type (type))) |
5566b478 MS |
7459 | ; /* A complete type is ok. */ |
7460 | else if (TREE_CODE (type) != ARRAY_TYPE) | |
8d08fdba | 7461 | { |
33bd39a2 | 7462 | error ("variable `%#D' has initializer but incomplete type", |
5566b478 MS |
7463 | decl); |
7464 | initialized = 0; | |
25eb19ff | 7465 | type = TREE_TYPE (decl) = error_mark_node; |
5566b478 | 7466 | } |
d0f062fb | 7467 | else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type)))) |
5566b478 MS |
7468 | { |
7469 | if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)) | |
33bd39a2 | 7470 | error ("elements of array `%#D' have incomplete type", decl); |
5566b478 MS |
7471 | /* else we already gave an error in start_decl. */ |
7472 | initialized = 0; | |
8d08fdba | 7473 | } |
8d08fdba MS |
7474 | } |
7475 | ||
5566b478 MS |
7476 | if (!initialized |
7477 | && TREE_CODE (decl) != TYPE_DECL | |
7478 | && TREE_CODE (decl) != TEMPLATE_DECL | |
07c88314 | 7479 | && type != error_mark_node |
5362b086 | 7480 | && IS_AGGR_TYPE (type) |
07c88314 | 7481 | && ! DECL_EXTERNAL (decl)) |
8d08fdba | 7482 | { |
5156628f | 7483 | if ((! processing_template_decl || ! uses_template_parms (type)) |
d0f062fb | 7484 | && !COMPLETE_TYPE_P (complete_type (type))) |
5566b478 | 7485 | { |
0154eaa8 | 7486 | error ("aggregate `%#D' has incomplete type and cannot be defined", |
5566b478 MS |
7487 | decl); |
7488 | /* Change the type so that assemble_variable will give | |
7489 | DECL an rtl we can live with: (mem (const_int 0)). */ | |
25eb19ff | 7490 | type = TREE_TYPE (decl) = error_mark_node; |
5566b478 MS |
7491 | } |
7492 | else | |
7493 | { | |
7494 | /* If any base type in the hierarchy of TYPE needs a constructor, | |
7495 | then we set initialized to 1. This way any nodes which are | |
7496 | created for the purposes of initializing this aggregate | |
7497 | will live as long as it does. This is necessary for global | |
7498 | aggregates which do not have their initializers processed until | |
7499 | the end of the file. */ | |
7500 | initialized = TYPE_NEEDS_CONSTRUCTING (type); | |
7501 | } | |
7502 | } | |
7503 | ||
5566b478 MS |
7504 | if (! initialized) |
7505 | DECL_INITIAL (decl) = NULL_TREE; | |
7506 | } | |
7507 | ||
7508 | /* Handle initialization of references. | |
38e01259 | 7509 | These three arguments are from `cp_finish_decl', and have the |
e92cc029 MS |
7510 | same meaning here that they do there. |
7511 | ||
7512 | Quotes on semantics can be found in ARM 8.4.3. */ | |
7513 | ||
8e4ce833 | 7514 | static tree |
a703fb38 | 7515 | grok_reference_init (decl, type, init) |
5566b478 | 7516 | tree decl, type, init; |
5566b478 MS |
7517 | { |
7518 | tree tmp; | |
7519 | ||
7520 | if (init == NULL_TREE) | |
7521 | { | |
7522 | if ((DECL_LANG_SPECIFIC (decl) == 0 | |
7523 | || DECL_IN_AGGR_P (decl) == 0) | |
7524 | && ! DECL_THIS_EXTERN (decl)) | |
33bd39a2 | 7525 | error ("`%D' declared as reference but not initialized", decl); |
8e4ce833 | 7526 | return NULL_TREE; |
5566b478 MS |
7527 | } |
7528 | ||
7529 | if (init == error_mark_node) | |
8e4ce833 | 7530 | return NULL_TREE; |
5566b478 | 7531 | |
ed5511d9 | 7532 | if (TREE_CODE (init) == CONSTRUCTOR) |
5566b478 | 7533 | { |
33bd39a2 | 7534 | error ("ISO C++ forbids use of initializer list to initialize reference `%D'", decl); |
8e4ce833 | 7535 | return NULL_TREE; |
8d08fdba MS |
7536 | } |
7537 | ||
7538 | if (TREE_CODE (init) == TREE_LIST) | |
7539 | init = build_compound_expr (init); | |
8d08fdba | 7540 | |
8ccc31eb MS |
7541 | if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE) |
7542 | init = convert_from_reference (init); | |
7543 | ||
8d08fdba MS |
7544 | if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE |
7545 | && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE) | |
7546 | { | |
a3203465 | 7547 | /* Note: default conversion is only called in very special cases. */ |
8d08fdba MS |
7548 | init = default_conversion (init); |
7549 | } | |
68642fb6 | 7550 | |
24bef158 MM |
7551 | /* Convert INIT to the reference type TYPE. This may involve the |
7552 | creation of a temporary, whose lifetime must be the same as that | |
7553 | of the reference. If so, a DECL_STMT for the temporary will be | |
7554 | added just after the DECL_STMT for DECL. That's why we don't set | |
7555 | DECL_INITIAL for local references (instead assigning to them | |
7556 | explicitly); we need to allow the temporary to be initialized | |
7557 | first. */ | |
a3203465 | 7558 | tmp = convert_to_reference |
9a3b49ac | 7559 | (type, init, CONV_IMPLICIT, |
ce7715bd JM |
7560 | LOOKUP_ONLYCONVERTING|LOOKUP_SPECULATIVELY|LOOKUP_NORMAL|DIRECT_BIND, |
7561 | decl); | |
8d08fdba | 7562 | |
a3203465 | 7563 | if (tmp == error_mark_node) |
8e4ce833 JJ |
7564 | return NULL_TREE; |
7565 | else if (tmp == NULL_TREE) | |
8d08fdba | 7566 | { |
33bd39a2 | 7567 | error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init)); |
8e4ce833 | 7568 | return NULL_TREE; |
8d08fdba | 7569 | } |
8d08fdba | 7570 | |
8e4ce833 JJ |
7571 | if (TREE_STATIC (decl) && !TREE_CONSTANT (tmp)) |
7572 | return tmp; | |
7573 | ||
08ac397c | 7574 | DECL_INITIAL (decl) = tmp; |
8e4ce833 JJ |
7575 | |
7576 | return NULL_TREE; | |
8d08fdba MS |
7577 | } |
7578 | ||
6060a796 MS |
7579 | /* Fill in DECL_INITIAL with some magical value to prevent expand_decl from |
7580 | mucking with forces it does not comprehend (i.e. initialization with a | |
7581 | constructor). If we are at global scope and won't go into COMMON, fill | |
7582 | it in with a dummy CONSTRUCTOR to force the variable into .data; | |
7583 | otherwise we can use error_mark_node. */ | |
7584 | ||
28cbf42c MS |
7585 | static tree |
7586 | obscure_complex_init (decl, init) | |
7587 | tree decl, init; | |
6060a796 | 7588 | { |
28cbf42c MS |
7589 | if (! flag_no_inline && TREE_STATIC (decl)) |
7590 | { | |
7591 | if (extract_init (decl, init)) | |
7592 | return NULL_TREE; | |
7593 | } | |
7594 | ||
2ee887f2 | 7595 | #if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS) |
a9aedbc2 | 7596 | if (toplevel_bindings_p () && ! DECL_COMMON (decl)) |
6060a796 MS |
7597 | DECL_INITIAL (decl) = build (CONSTRUCTOR, TREE_TYPE (decl), NULL_TREE, |
7598 | NULL_TREE); | |
7599 | else | |
2ee887f2 | 7600 | #endif |
6060a796 | 7601 | DECL_INITIAL (decl) = error_mark_node; |
28cbf42c MS |
7602 | |
7603 | return init; | |
6060a796 MS |
7604 | } |
7605 | ||
27778b73 MM |
7606 | /* When parsing `int a[] = {1, 2};' we don't know the size of the |
7607 | array until we finish parsing the initializer. If that's the | |
7608 | situation we're in, update DECL accordingly. */ | |
7609 | ||
7610 | static void | |
7611 | maybe_deduce_size_from_array_init (decl, init) | |
7612 | tree decl; | |
7613 | tree init; | |
7614 | { | |
7615 | tree type = TREE_TYPE (decl); | |
7616 | ||
7617 | if (TREE_CODE (type) == ARRAY_TYPE | |
7618 | && TYPE_DOMAIN (type) == NULL_TREE | |
7619 | && TREE_CODE (decl) != TYPE_DECL) | |
7620 | { | |
f2ae0c45 JM |
7621 | /* do_default is really a C-ism to deal with tentative definitions. |
7622 | But let's leave it here to ease the eventual merge. */ | |
7623 | int do_default = !DECL_EXTERNAL (decl); | |
27778b73 MM |
7624 | tree initializer = init ? init : DECL_INITIAL (decl); |
7625 | int failure = complete_array_type (type, initializer, do_default); | |
7626 | ||
7627 | if (failure == 1) | |
33bd39a2 | 7628 | error ("initializer fails to determine size of `%D'", decl); |
27778b73 MM |
7629 | |
7630 | if (failure == 2) | |
7631 | { | |
7632 | if (do_default) | |
33bd39a2 | 7633 | error ("array size missing in `%D'", decl); |
27778b73 MM |
7634 | /* If a `static' var's size isn't known, make it extern as |
7635 | well as static, so it does not get allocated. If it's not | |
7636 | `static', then don't mark it extern; finish_incomplete_decl | |
7637 | will give it a default size and it will get allocated. */ | |
7638 | else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl)) | |
7639 | DECL_EXTERNAL (decl) = 1; | |
7640 | } | |
7641 | ||
7642 | if (pedantic && TYPE_DOMAIN (type) != NULL_TREE | |
7643 | && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)), | |
7644 | integer_zero_node)) | |
33bd39a2 | 7645 | error ("zero-size array `%D'", decl); |
27778b73 MM |
7646 | |
7647 | layout_decl (decl, 0); | |
7648 | } | |
7649 | } | |
7650 | ||
7651 | /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue | |
57b52417 | 7652 | any appropriate error messages regarding the layout. */ |
27778b73 | 7653 | |
57b52417 MM |
7654 | static void |
7655 | layout_var_decl (decl) | |
27778b73 | 7656 | tree decl; |
27778b73 | 7657 | { |
57b52417 | 7658 | tree type = TREE_TYPE (decl); |
c95cd22e | 7659 | #if 0 |
57b52417 | 7660 | tree ttype = target_type (type); |
c95cd22e | 7661 | #endif |
57b52417 MM |
7662 | |
7663 | /* If we haven't already layed out this declaration, do so now. | |
7664 | Note that we must not call complete type for an external object | |
7665 | because it's type might involve templates that we are not | |
68642fb6 | 7666 | supposed to isntantiate yet. (And it's perfectly legal to say |
57b52417 MM |
7667 | `extern X x' for some incomplete type `X'.) */ |
7668 | if (!DECL_EXTERNAL (decl)) | |
7669 | complete_type (type); | |
d0f062fb | 7670 | if (!DECL_SIZE (decl) && COMPLETE_TYPE_P (type)) |
27778b73 MM |
7671 | layout_decl (decl, 0); |
7672 | ||
c82dbd95 | 7673 | if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE) |
27778b73 MM |
7674 | { |
7675 | /* An automatic variable with an incomplete type: that is an error. | |
7676 | Don't talk about array types here, since we took care of that | |
7677 | message in grokdeclarator. */ | |
33bd39a2 | 7678 | error ("storage size of `%D' isn't known", decl); |
27778b73 MM |
7679 | TREE_TYPE (decl) = error_mark_node; |
7680 | } | |
ae673f14 JM |
7681 | #if 0 |
7682 | /* Keep this code around in case we later want to control debug info | |
7683 | based on whether a type is "used". (jason 1999-11-11) */ | |
7684 | ||
27778b73 MM |
7685 | else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype)) |
7686 | /* Let debugger know it should output info for this type. */ | |
7687 | note_debug_info_needed (ttype); | |
7688 | ||
7689 | if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl)) | |
7690 | note_debug_info_needed (DECL_CONTEXT (decl)); | |
ae673f14 | 7691 | #endif |
27778b73 MM |
7692 | |
7693 | if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl)) | |
7694 | && DECL_SIZE (decl) != NULL_TREE | |
7695 | && ! TREE_CONSTANT (DECL_SIZE (decl))) | |
7696 | { | |
7697 | if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST) | |
7698 | constant_expression_warning (DECL_SIZE (decl)); | |
7699 | else | |
33bd39a2 | 7700 | error ("storage size of `%D' isn't constant", decl); |
27778b73 | 7701 | } |
8e4ce833 JJ |
7702 | |
7703 | if (TREE_STATIC (decl) | |
7704 | && !DECL_ARTIFICIAL (decl) | |
7705 | && current_function_decl | |
7706 | && DECL_CONTEXT (decl) == current_function_decl) | |
7707 | push_local_name (decl); | |
27778b73 MM |
7708 | } |
7709 | ||
27778b73 MM |
7710 | /* If a local static variable is declared in an inline function, or if |
7711 | we have a weak definition, we must endeavor to create only one | |
7712 | instance of the variable at link-time. */ | |
7713 | ||
7714 | static void | |
7715 | maybe_commonize_var (decl) | |
7716 | tree decl; | |
7717 | { | |
7718 | /* Static data in a function with comdat linkage also has comdat | |
7719 | linkage. */ | |
7720 | if (TREE_STATIC (decl) | |
7721 | /* Don't mess with __FUNCTION__. */ | |
cf74fb86 | 7722 | && ! DECL_ARTIFICIAL (decl) |
27778b73 MM |
7723 | && current_function_decl |
7724 | && DECL_CONTEXT (decl) == current_function_decl | |
79065db2 | 7725 | && (DECL_DECLARED_INLINE_P (current_function_decl) |
27778b73 MM |
7726 | || DECL_TEMPLATE_INSTANTIATION (current_function_decl)) |
7727 | && TREE_PUBLIC (current_function_decl)) | |
7728 | { | |
27778b73 MM |
7729 | /* If flag_weak, we don't need to mess with this, as we can just |
7730 | make the function weak, and let it refer to its unique local | |
7731 | copy. This works because we don't allow the function to be | |
7732 | inlined. */ | |
7733 | if (! flag_weak) | |
7734 | { | |
7735 | if (DECL_INTERFACE_KNOWN (current_function_decl)) | |
7736 | { | |
7737 | TREE_PUBLIC (decl) = 1; | |
7738 | DECL_EXTERNAL (decl) = DECL_EXTERNAL (current_function_decl); | |
7739 | } | |
7740 | else if (DECL_INITIAL (decl) == NULL_TREE | |
7741 | || DECL_INITIAL (decl) == error_mark_node) | |
7742 | { | |
7743 | TREE_PUBLIC (decl) = 1; | |
7744 | DECL_COMMON (decl) = 1; | |
7745 | } | |
7746 | /* else we lose. We can only do this if we can use common, | |
7747 | which we can't if it has been initialized. */ | |
7748 | ||
92643fea | 7749 | if (!TREE_PUBLIC (decl)) |
27778b73 MM |
7750 | { |
7751 | cp_warning_at ("sorry: semantics of inline function static data `%#D' are wrong (you'll wind up with multiple copies)", decl); | |
7752 | cp_warning_at (" you can work around this by removing the initializer", decl); | |
7753 | } | |
7754 | } | |
8e4ce833 JJ |
7755 | else |
7756 | comdat_linkage (decl); | |
27778b73 MM |
7757 | } |
7758 | else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl)) | |
7759 | /* Set it up again; we might have set DECL_INITIAL since the last | |
7760 | time. */ | |
7761 | comdat_linkage (decl); | |
7762 | } | |
7763 | ||
91063b51 MM |
7764 | /* Issue an error message if DECL is an uninitialized const variable. */ |
7765 | ||
7766 | static void | |
7767 | check_for_uninitialized_const_var (decl) | |
7768 | tree decl; | |
7769 | { | |
7770 | tree type = TREE_TYPE (decl); | |
7771 | ||
7772 | /* ``Unless explicitly declared extern, a const object does not have | |
7773 | external linkage and must be initialized. ($8.4; $12.1)'' ARM | |
7774 | 7.1.6 */ | |
7775 | if (TREE_CODE (decl) == VAR_DECL | |
7776 | && TREE_CODE (type) != REFERENCE_TYPE | |
7777 | && CP_TYPE_CONST_P (type) | |
7778 | && !TYPE_NEEDS_CONSTRUCTING (type) | |
7779 | && !DECL_INITIAL (decl)) | |
33bd39a2 | 7780 | error ("uninitialized const `%D'", decl); |
91063b51 MM |
7781 | } |
7782 | ||
c82dbd95 MM |
7783 | /* Verify INIT (the initializer for DECL), and record the |
7784 | initialization in DECL_INITIAL, if appropriate. Returns a new | |
7785 | value for INIT. */ | |
27778b73 | 7786 | |
c82dbd95 MM |
7787 | static tree |
7788 | check_initializer (decl, init) | |
27778b73 | 7789 | tree decl; |
c82dbd95 | 7790 | tree init; |
27778b73 | 7791 | { |
27778b73 MM |
7792 | tree type; |
7793 | ||
7794 | if (TREE_CODE (decl) == FIELD_DECL) | |
c82dbd95 | 7795 | return init; |
27778b73 | 7796 | |
fc0e7bf5 MM |
7797 | type = TREE_TYPE (decl); |
7798 | ||
27778b73 MM |
7799 | /* If `start_decl' didn't like having an initialization, ignore it now. */ |
7800 | if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE) | |
7801 | init = NULL_TREE; | |
27778b73 | 7802 | |
c82dbd95 | 7803 | /* Check the initializer. */ |
27778b73 MM |
7804 | if (init) |
7805 | { | |
c82dbd95 MM |
7806 | /* Things that are going to be initialized need to have complete |
7807 | type. */ | |
7808 | TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl)); | |
7809 | ||
7810 | if (type == error_mark_node) | |
7811 | /* We will have already complained. */ | |
7812 | init = NULL_TREE; | |
d0f062fb | 7813 | else if (COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type))) |
27778b73 | 7814 | { |
33bd39a2 | 7815 | error ("variable-sized object `%D' may not be initialized", decl); |
27778b73 MM |
7816 | init = NULL_TREE; |
7817 | } | |
c82dbd95 | 7818 | else if (TREE_CODE (type) == ARRAY_TYPE |
fe5b6c1c | 7819 | && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (type)))) |
27778b73 | 7820 | { |
33bd39a2 | 7821 | error ("elements of array `%#D' have incomplete type", decl); |
27778b73 MM |
7822 | init = NULL_TREE; |
7823 | } | |
fe5b6c1c | 7824 | else if (TREE_CODE (type) != ARRAY_TYPE && !COMPLETE_TYPE_P (type)) |
c82dbd95 | 7825 | { |
33bd39a2 | 7826 | error ("`%D' has incomplete type", decl); |
c82dbd95 MM |
7827 | TREE_TYPE (decl) = error_mark_node; |
7828 | init = NULL_TREE; | |
7829 | } | |
27778b73 MM |
7830 | } |
7831 | ||
7832 | if (TREE_CODE (decl) == CONST_DECL) | |
7833 | { | |
7834 | my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148); | |
7835 | ||
7836 | DECL_INITIAL (decl) = init; | |
7837 | ||
27778b73 MM |
7838 | my_friendly_assert (init != NULL_TREE, 149); |
7839 | init = NULL_TREE; | |
7840 | } | |
c82dbd95 MM |
7841 | else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE) |
7842 | { | |
8e4ce833 JJ |
7843 | init = grok_reference_init (decl, type, init); |
7844 | if (init) | |
7845 | init = obscure_complex_init (decl, init); | |
c82dbd95 | 7846 | } |
27778b73 MM |
7847 | else if (init) |
7848 | { | |
7849 | if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type)) | |
7850 | { | |
7851 | if (TREE_CODE (type) == ARRAY_TYPE) | |
7852 | init = digest_init (type, init, (tree *) 0); | |
7853 | else if (TREE_CODE (init) == CONSTRUCTOR | |
7854 | && TREE_HAS_CONSTRUCTOR (init)) | |
7855 | { | |
7856 | if (TYPE_NON_AGGREGATE_CLASS (type)) | |
7857 | { | |
33bd39a2 | 7858 | error ("`%D' must be initialized by constructor, not by `{...}'", |
27778b73 MM |
7859 | decl); |
7860 | init = error_mark_node; | |
7861 | } | |
7862 | else | |
7863 | goto dont_use_constructor; | |
7864 | } | |
7865 | } | |
7866 | else | |
7867 | { | |
7868 | dont_use_constructor: | |
7869 | if (TREE_CODE (init) != TREE_VEC) | |
7870 | init = store_init_value (decl, init); | |
7871 | } | |
7872 | ||
7873 | if (init) | |
7874 | /* We must hide the initializer so that expand_decl | |
7875 | won't try to do something it does not understand. */ | |
7876 | init = obscure_complex_init (decl, init); | |
7877 | } | |
7878 | else if (DECL_EXTERNAL (decl)) | |
7879 | ; | |
2f939d94 | 7880 | else if (TYPE_P (type) |
27778b73 MM |
7881 | && (IS_AGGR_TYPE (type) || TYPE_NEEDS_CONSTRUCTING (type))) |
7882 | { | |
7883 | tree core_type = strip_array_types (type); | |
7884 | ||
7885 | if (! TYPE_NEEDS_CONSTRUCTING (core_type)) | |
7886 | { | |
7887 | if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)) | |
33bd39a2 | 7888 | error ("structure `%D' with uninitialized const members", decl); |
27778b73 | 7889 | if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)) |
33bd39a2 | 7890 | error ("structure `%D' with uninitialized reference members", |
27778b73 MM |
7891 | decl); |
7892 | } | |
7893 | ||
7894 | check_for_uninitialized_const_var (decl); | |
7895 | ||
d0f062fb | 7896 | if (COMPLETE_TYPE_P (type) && TYPE_NEEDS_CONSTRUCTING (type)) |
27778b73 MM |
7897 | init = obscure_complex_init (decl, NULL_TREE); |
7898 | ||
7899 | } | |
7900 | else | |
7901 | check_for_uninitialized_const_var (decl); | |
68642fb6 | 7902 | |
c82dbd95 | 7903 | return init; |
27778b73 MM |
7904 | } |
7905 | ||
7906 | /* If DECL is not a local variable, give it RTL. */ | |
7907 | ||
7908 | static void | |
7909 | make_rtl_for_nonlocal_decl (decl, init, asmspec) | |
7910 | tree decl; | |
7911 | tree init; | |
7912 | const char *asmspec; | |
7913 | { | |
95ee998c MM |
7914 | int toplev = toplevel_bindings_p (); |
7915 | int defer_p; | |
27778b73 | 7916 | |
f39ee884 MM |
7917 | /* Handle non-variables up front. */ |
7918 | if (TREE_CODE (decl) != VAR_DECL) | |
7919 | { | |
7920 | rest_of_decl_compilation (decl, asmspec, toplev, at_eof); | |
7921 | return; | |
7922 | } | |
7923 | ||
95ee998c MM |
7924 | /* If we see a class member here, it should be a static data |
7925 | member. */ | |
7926 | if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl)) | |
7927 | { | |
7928 | my_friendly_assert (TREE_STATIC (decl), 19990828); | |
7929 | /* An in-class declaration of a static data member should be | |
7930 | external; it is only a declaration, and not a definition. */ | |
7931 | if (init == NULL_TREE) | |
7932 | my_friendly_assert (DECL_EXTERNAL (decl), 20000723); | |
7933 | } | |
7934 | ||
f39ee884 MM |
7935 | /* Set the DECL_ASSEMBLER_NAME for the variable. */ |
7936 | if (asmspec) | |
7ed47c04 MM |
7937 | { |
7938 | SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec)); | |
7939 | /* The `register' keyword, when used together with an | |
7940 | asm-specification, indicates that the variable should be | |
7941 | placed in a particular register. */ | |
7942 | if (DECL_REGISTER (decl)) | |
7943 | DECL_C_HARD_REGISTER (decl) = 1; | |
7944 | } | |
f39ee884 | 7945 | |
95ee998c MM |
7946 | /* We don't create any RTL for local variables. */ |
7947 | if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl)) | |
7948 | return; | |
27778b73 | 7949 | |
95ee998c MM |
7950 | /* We defer emission of local statics until the corresponding |
7951 | DECL_STMT is expanded. */ | |
7952 | defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl); | |
7953 | ||
5cc90635 JM |
7954 | /* We try to defer namespace-scope static constants and template |
7955 | instantiations so that they are not emitted into the object file | |
7956 | unnecessarily. */ | |
7957 | if ((!DECL_VIRTUAL_P (decl) | |
7958 | && TREE_READONLY (decl) | |
7959 | && DECL_INITIAL (decl) != NULL_TREE | |
7960 | && DECL_INITIAL (decl) != error_mark_node | |
7961 | && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)) | |
7962 | && toplev | |
7963 | && !TREE_PUBLIC (decl)) | |
7964 | || DECL_COMDAT (decl)) | |
7965 | { | |
7966 | /* Fool with the linkage of static consts according to #pragma | |
7967 | interface. */ | |
7968 | if (!interface_unknown && !TREE_PUBLIC (decl)) | |
27778b73 | 7969 | { |
95ee998c MM |
7970 | TREE_PUBLIC (decl) = 1; |
7971 | DECL_EXTERNAL (decl) = interface_only; | |
27778b73 | 7972 | } |
27778b73 | 7973 | |
95ee998c | 7974 | defer_p = 1; |
27778b73 | 7975 | } |
95ee998c | 7976 | |
92643fea MM |
7977 | /* If we're deferring the variable, we only need to make RTL if |
7978 | there's an ASMSPEC. Otherwise, we'll lazily create it later when | |
7979 | we need it. (There's no way to lazily create RTL for things that | |
7980 | have assembly specs because the information about the specifier | |
7981 | isn't stored in the tree, yet) */ | |
7982 | if (defer_p && asmspec) | |
6c418184 | 7983 | make_decl_rtl (decl, asmspec); |
95ee998c | 7984 | /* If we're not deferring, go ahead and assemble the variable. */ |
92643fea | 7985 | else if (!defer_p) |
27778b73 | 7986 | rest_of_decl_compilation (decl, asmspec, toplev, at_eof); |
27778b73 MM |
7987 | } |
7988 | ||
7989 | /* The old ARM scoping rules injected variables declared in the | |
7990 | initialization statement of a for-statement into the surrounding | |
7991 | scope. We support this usage, in order to be backward-compatible. | |
7992 | DECL is a just-declared VAR_DECL; if necessary inject its | |
7993 | declaration into the surrounding scope. */ | |
7994 | ||
b7b8bcd2 | 7995 | void |
27778b73 MM |
7996 | maybe_inject_for_scope_var (decl) |
7997 | tree decl; | |
7998 | { | |
c3783399 NS |
7999 | if (!DECL_NAME (decl)) |
8000 | return; | |
5362b086 | 8001 | |
27778b73 MM |
8002 | if (current_binding_level->is_for_scope) |
8003 | { | |
68642fb6 | 8004 | struct binding_level *outer |
27778b73 MM |
8005 | = current_binding_level->level_chain; |
8006 | ||
8007 | /* Check to see if the same name is already bound at the outer | |
8008 | level, either because it was directly declared, or because a | |
8009 | dead for-decl got preserved. In either case, the code would | |
8010 | not have been valid under the ARM scope rules, so clear | |
8011 | is_for_scope for the current_binding_level. | |
8012 | ||
8013 | Otherwise, we need to preserve the temp slot for decl to last | |
8014 | into the outer binding level. */ | |
8015 | ||
68642fb6 | 8016 | tree outer_binding |
27778b73 | 8017 | = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (decl))); |
68642fb6 | 8018 | |
27778b73 | 8019 | if (outer_binding && BINDING_LEVEL (outer_binding) == outer |
68642fb6 | 8020 | && (TREE_CODE (BINDING_VALUE (outer_binding)) |
27778b73 MM |
8021 | == VAR_DECL) |
8022 | && DECL_DEAD_FOR_LOCAL (BINDING_VALUE (outer_binding))) | |
8023 | { | |
8024 | BINDING_VALUE (outer_binding) | |
8025 | = DECL_SHADOWED_FOR_VAR (BINDING_VALUE (outer_binding)); | |
8026 | current_binding_level->is_for_scope = 0; | |
8027 | } | |
8028 | else if (DECL_IN_MEMORY_P (decl)) | |
8029 | preserve_temp_slots (DECL_RTL (decl)); | |
8030 | } | |
8031 | } | |
8032 | ||
ed5511d9 | 8033 | /* Generate code to initialize DECL (a local variable). */ |
27778b73 | 8034 | |
b7b8bcd2 MM |
8035 | void |
8036 | initialize_local_var (decl, init, flags) | |
27778b73 MM |
8037 | tree decl; |
8038 | tree init; | |
27778b73 MM |
8039 | int flags; |
8040 | { | |
9ed9e79a | 8041 | tree type = TREE_TYPE (decl); |
27778b73 | 8042 | |
9ed9e79a MM |
8043 | /* If the type is bogus, don't bother initializing the variable. */ |
8044 | if (type == error_mark_node) | |
8045 | return; | |
b7b8bcd2 | 8046 | |
b7b8bcd2 MM |
8047 | if (DECL_SIZE (decl) == NULL_TREE && !TREE_STATIC (decl)) |
8048 | { | |
8049 | /* If we used it already as memory, it must stay in memory. */ | |
8050 | DECL_INITIAL (decl) = NULL_TREE; | |
8051 | TREE_ADDRESSABLE (decl) = TREE_USED (decl); | |
8052 | } | |
8053 | ||
9ed9e79a MM |
8054 | /* Local statics are handled differently from ordinary automatic |
8055 | variables. */ | |
8056 | if (TREE_STATIC (decl)) | |
8057 | { | |
8058 | if (TYPE_NEEDS_CONSTRUCTING (type) || init != NULL_TREE | |
834c6dff | 8059 | || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) |
9ed9e79a MM |
8060 | expand_static_init (decl, init); |
8061 | return; | |
8062 | } | |
8063 | ||
27778b73 MM |
8064 | if (DECL_SIZE (decl) && type != error_mark_node) |
8065 | { | |
8066 | int already_used; | |
68642fb6 | 8067 | |
27778b73 | 8068 | /* Compute and store the initial value. */ |
27778b73 MM |
8069 | already_used = TREE_USED (decl) || TREE_USED (type); |
8070 | ||
8071 | if (init || TYPE_NEEDS_CONSTRUCTING (type)) | |
8072 | { | |
24bef158 MM |
8073 | int saved_stmts_are_full_exprs_p; |
8074 | ||
3a0d3e1e | 8075 | my_friendly_assert (building_stmt_tree (), 20000906); |
f2c5f623 | 8076 | saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p (); |
ae499cce | 8077 | current_stmt_tree ()->stmts_are_full_exprs_p = 1; |
3a0d3e1e | 8078 | finish_expr_stmt (build_aggr_init (decl, init, flags)); |
5362b086 | 8079 | current_stmt_tree ()->stmts_are_full_exprs_p = |
ae499cce | 8080 | saved_stmts_are_full_exprs_p; |
27778b73 MM |
8081 | } |
8082 | ||
8083 | /* Set this to 0 so we can tell whether an aggregate which was | |
8084 | initialized was ever used. Don't do this if it has a | |
8085 | destructor, so we don't complain about the 'resource | |
b7b8bcd2 MM |
8086 | allocation is initialization' idiom. Now set |
8087 | attribute((unused)) on types so decls of that type will be | |
8088 | marked used. (see TREE_USED, above.) */ | |
27778b73 MM |
8089 | if (TYPE_NEEDS_CONSTRUCTING (type) |
8090 | && ! already_used | |
834c6dff | 8091 | && TYPE_HAS_TRIVIAL_DESTRUCTOR (type) |
27778b73 MM |
8092 | && DECL_NAME (decl)) |
8093 | TREE_USED (decl) = 0; | |
b7b8bcd2 | 8094 | else if (already_used) |
27778b73 MM |
8095 | TREE_USED (decl) = 1; |
8096 | } | |
24bef158 | 8097 | } |
27778b73 | 8098 | |
24bef158 MM |
8099 | /* Generate code to destroy DECL (a local variable). */ |
8100 | ||
68642fb6 | 8101 | static void |
24bef158 MM |
8102 | destroy_local_var (decl) |
8103 | tree decl; | |
8104 | { | |
9d85d30c MM |
8105 | tree type = TREE_TYPE (decl); |
8106 | tree cleanup; | |
8107 | ||
8108 | /* Only variables get cleaned up. */ | |
8109 | if (TREE_CODE (decl) != VAR_DECL) | |
8110 | return; | |
68642fb6 | 8111 | |
9d85d30c | 8112 | /* And only things with destructors need cleaning up. */ |
655dc6ee JM |
8113 | if (type == error_mark_node |
8114 | || TYPE_HAS_TRIVIAL_DESTRUCTOR (type)) | |
9d85d30c MM |
8115 | return; |
8116 | ||
8117 | if (TREE_CODE (decl) == VAR_DECL && | |
8118 | (DECL_EXTERNAL (decl) || TREE_STATIC (decl))) | |
8119 | /* We don't clean up things that aren't defined in this | |
8120 | translation unit, or that need a static cleanup. The latter | |
8121 | are handled by finish_file. */ | |
8122 | return; | |
68642fb6 | 8123 | |
9d85d30c | 8124 | /* Compute the cleanup. */ |
c88770e9 | 8125 | cleanup = cxx_maybe_build_cleanup (decl); |
27778b73 | 8126 | |
b7b8bcd2 | 8127 | /* Record the cleanup required for this declaration. */ |
6e4ae815 | 8128 | if (DECL_SIZE (decl) && cleanup) |
24bef158 MM |
8129 | finish_decl_cleanup (decl, cleanup); |
8130 | } | |
8131 | ||
8d08fdba MS |
8132 | /* Finish processing of a declaration; |
8133 | install its line number and initial value. | |
8134 | If the length of an array type is not known before, | |
8135 | it must be determined now, from the initial value, or it is an error. | |
8136 | ||
8b27e9ef | 8137 | INIT holds the value of an initializer that should be allowed to escape |
8d08fdba MS |
8138 | the normal rules. |
8139 | ||
920f9474 | 8140 | FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0 |
f78c7bc6 | 8141 | if the (init) syntax was used. */ |
8d08fdba MS |
8142 | |
8143 | void | |
cd9f6678 | 8144 | cp_finish_decl (decl, init, asmspec_tree, flags) |
8d08fdba MS |
8145 | tree decl, init; |
8146 | tree asmspec_tree; | |
6060a796 | 8147 | int flags; |
8d08fdba MS |
8148 | { |
8149 | register tree type; | |
27778b73 | 8150 | tree ttype = NULL_TREE; |
9c0758dd | 8151 | const char *asmspec = NULL; |
8d08fdba MS |
8152 | int was_readonly = 0; |
8153 | ||
8d08fdba MS |
8154 | if (! decl) |
8155 | { | |
8156 | if (init) | |
8251199e | 8157 | error ("assignment (not initialization) in declaration"); |
8d08fdba MS |
8158 | return; |
8159 | } | |
8160 | ||
a4443a08 | 8161 | /* If a name was specified, get the string. */ |
41c64394 RH |
8162 | if (current_binding_level == global_binding_level) |
8163 | asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree); | |
8d08fdba | 8164 | if (asmspec_tree) |
5cc90635 | 8165 | asmspec = TREE_STRING_POINTER (asmspec_tree); |
8d08fdba | 8166 | |
2c73f9f5 ML |
8167 | if (init && TREE_CODE (init) == NAMESPACE_DECL) |
8168 | { | |
33bd39a2 | 8169 | error ("cannot initialize `%D' to namespace `%D'", |
2c73f9f5 ML |
8170 | decl, init); |
8171 | init = NULL_TREE; | |
8172 | } | |
8173 | ||
6ba89f8e | 8174 | if (current_class_type |
4f1c5b7d | 8175 | && CP_DECL_CONTEXT (decl) == current_class_type |
6ba89f8e MM |
8176 | && TYPE_BEING_DEFINED (current_class_type) |
8177 | && (DECL_INITIAL (decl) || init)) | |
3febd123 | 8178 | DECL_INITIALIZED_IN_CLASS_P (decl) = 1; |
6ba89f8e | 8179 | |
68642fb6 | 8180 | if (TREE_CODE (decl) == VAR_DECL |
9a68c51f JM |
8181 | && DECL_CONTEXT (decl) |
8182 | && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL | |
8183 | && DECL_CONTEXT (decl) != current_namespace | |
8184 | && init) | |
8185 | { | |
8186 | /* Leave the namespace of the object. */ | |
8187 | pop_decl_namespace (); | |
8188 | } | |
8189 | ||
c82dbd95 | 8190 | type = TREE_TYPE (decl); |
8d08fdba | 8191 | |
f376e137 | 8192 | if (type == error_mark_node) |
cd9f6678 | 8193 | return; |
5362b086 | 8194 | |
a7a7710d NS |
8195 | if (TYPE_HAS_MUTABLE_P (type)) |
8196 | TREE_READONLY (decl) = 0; | |
24bef158 | 8197 | |
5156628f | 8198 | if (processing_template_decl) |
5566b478 | 8199 | { |
08ac397c JM |
8200 | /* Add this declaration to the statement-tree. */ |
8201 | if (at_function_scope_p () | |
8202 | && TREE_CODE (decl) != RESULT_DECL) | |
8203 | add_decl_stmt (decl); | |
8204 | ||
5566b478 | 8205 | if (init && DECL_INITIAL (decl)) |
2a1e9fdd | 8206 | DECL_INITIAL (decl) = init; |
5566b478 MS |
8207 | goto finish_end0; |
8208 | } | |
3e41d13b | 8209 | |
27778b73 MM |
8210 | /* Parameters are handled by store_parm_decls, not cp_finish_decl. */ |
8211 | my_friendly_assert (TREE_CODE (decl) != PARM_DECL, 19990828); | |
8212 | ||
8d08fdba MS |
8213 | /* Take care of TYPE_DECLs up front. */ |
8214 | if (TREE_CODE (decl) == TYPE_DECL) | |
8215 | { | |
8216 | if (init && DECL_INITIAL (decl)) | |
8217 | { | |
8218 | /* typedef foo = bar; store the type of bar as the type of foo. */ | |
8219 | TREE_TYPE (decl) = type = TREE_TYPE (init); | |
8220 | DECL_INITIAL (decl) = init = NULL_TREE; | |
8221 | } | |
a0a33927 MS |
8222 | if (type != error_mark_node |
8223 | && IS_AGGR_TYPE (type) && DECL_NAME (decl)) | |
8d08fdba MS |
8224 | { |
8225 | if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type) | |
33bd39a2 | 8226 | warning ("shadowing previous type declaration of `%#D'", decl); |
8d08fdba MS |
8227 | set_identifier_type_value (DECL_NAME (decl), type); |
8228 | CLASSTYPE_GOT_SEMICOLON (type) = 1; | |
8229 | } | |
cffa8729 MS |
8230 | |
8231 | /* If we have installed this as the canonical typedef for this | |
8232 | type, and that type has not been defined yet, delay emitting | |
956d6950 | 8233 | the debug information for it, as we will emit it later. */ |
d2e5ee5c | 8234 | if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl |
d0f062fb | 8235 | && !COMPLETE_TYPE_P (TREE_TYPE (decl))) |
cffa8729 MS |
8236 | TYPE_DECL_SUPPRESS_DEBUG (decl) = 1; |
8237 | ||
3e411c3f | 8238 | rest_of_decl_compilation (decl, NULL, |
5566b478 | 8239 | DECL_CONTEXT (decl) == NULL_TREE, at_eof); |
8d08fdba MS |
8240 | goto finish_end; |
8241 | } | |
3e41d13b | 8242 | |
8d08fdba | 8243 | if (TREE_CODE (decl) != FUNCTION_DECL) |
3e41d13b | 8244 | ttype = target_type (type); |
8d08fdba MS |
8245 | |
8246 | if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl) | |
8247 | && TYPE_NEEDS_CONSTRUCTING (type)) | |
8248 | { | |
8d08fdba MS |
8249 | /* Currently, GNU C++ puts constants in text space, making them |
8250 | impossible to initialize. In the future, one would hope for | |
8251 | an operating system which understood the difference between | |
8252 | initialization and the running of a program. */ | |
8253 | was_readonly = 1; | |
8254 | TREE_READONLY (decl) = 0; | |
8255 | } | |
8256 | ||
27778b73 | 8257 | if (TREE_CODE (decl) == FIELD_DECL && asmspec) |
8d08fdba | 8258 | { |
27778b73 MM |
8259 | /* This must override the asm specifier which was placed by |
8260 | grokclassfn. Lay this out fresh. */ | |
19e7881c | 8261 | SET_DECL_RTL (TREE_TYPE (decl), NULL_RTX); |
92643fea | 8262 | SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec)); |
6c418184 | 8263 | make_decl_rtl (decl, asmspec); |
8d08fdba MS |
8264 | } |
8265 | ||
c82dbd95 | 8266 | /* Deduce size of array from initialization, if not already known. */ |
c82dbd95 | 8267 | init = check_initializer (decl, init); |
fe5b6c1c | 8268 | maybe_deduce_size_from_array_init (decl, init); |
3e41d13b | 8269 | |
57cf57fb JM |
8270 | /* Add this declaration to the statement-tree. This needs to happen |
8271 | after the call to check_initializer so that the DECL_STMT for a | |
8272 | reference temp is added before the DECL_STMT for the reference itself. */ | |
08ac397c JM |
8273 | if (building_stmt_tree () |
8274 | && at_function_scope_p () | |
8275 | && TREE_CODE (decl) != RESULT_DECL) | |
8276 | add_decl_stmt (decl); | |
8277 | ||
8d08fdba | 8278 | if (TREE_CODE (decl) == VAR_DECL) |
57b52417 | 8279 | layout_var_decl (decl); |
8d08fdba MS |
8280 | |
8281 | /* Output the assembler code and/or RTL code for variables and functions, | |
8282 | unless the type is an undefined structure or union. | |
8283 | If not, it will get done when the type is completed. */ | |
8d08fdba MS |
8284 | if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL |
8285 | || TREE_CODE (decl) == RESULT_DECL) | |
8286 | { | |
27778b73 MM |
8287 | if (TREE_CODE (decl) == VAR_DECL) |
8288 | maybe_commonize_var (decl); | |
8d08fdba | 8289 | |
27778b73 | 8290 | make_rtl_for_nonlocal_decl (decl, init, asmspec); |
8d08fdba | 8291 | |
68642fb6 | 8292 | if (TREE_CODE (type) == FUNCTION_TYPE |
27778b73 | 8293 | || TREE_CODE (type) == METHOD_TYPE) |
68642fb6 | 8294 | abstract_virtuals_error (decl, |
27778b73 | 8295 | strip_array_types (TREE_TYPE (type))); |
68642fb6 | 8296 | else |
27778b73 | 8297 | abstract_virtuals_error (decl, strip_array_types (type)); |
8d08fdba | 8298 | |
8d08fdba | 8299 | if (TREE_CODE (decl) == FUNCTION_DECL) |
faae18ab | 8300 | ; |
67d743fe MS |
8301 | else if (DECL_EXTERNAL (decl) |
8302 | && ! (DECL_LANG_SPECIFIC (decl) | |
8303 | && DECL_NOT_REALLY_EXTERN (decl))) | |
5566b478 MS |
8304 | { |
8305 | if (init) | |
8306 | DECL_INITIAL (decl) = init; | |
8307 | } | |
b35d4555 | 8308 | else if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL) |
8d08fdba | 8309 | { |
b7b8bcd2 | 8310 | /* This is a local declaration. */ |
b35d4555 MM |
8311 | if (doing_semantic_analysis_p ()) |
8312 | maybe_inject_for_scope_var (decl); | |
b7b8bcd2 MM |
8313 | /* Initialize the local variable. But, if we're building a |
8314 | statement-tree, we'll do the initialization when we | |
8315 | expand the tree. */ | |
24bef158 MM |
8316 | if (processing_template_decl) |
8317 | { | |
8318 | if (init || DECL_INITIAL (decl) == error_mark_node) | |
8319 | DECL_INITIAL (decl) = init; | |
8320 | } | |
8321 | else | |
8322 | { | |
b35d4555 MM |
8323 | /* If we're not building RTL, then we need to do so |
8324 | now. */ | |
44835fdd | 8325 | my_friendly_assert (building_stmt_tree (), 20000906); |
b35d4555 | 8326 | /* Initialize the variable. */ |
24bef158 MM |
8327 | initialize_local_var (decl, init, flags); |
8328 | /* Clean up the variable. */ | |
8329 | destroy_local_var (decl); | |
8330 | } | |
8d08fdba | 8331 | } |
9ed9e79a MM |
8332 | else if (TREE_STATIC (decl) && type != error_mark_node) |
8333 | { | |
8334 | /* Cleanups for static variables are handled by `finish_file'. */ | |
8335 | if (TYPE_NEEDS_CONSTRUCTING (type) || init != NULL_TREE | |
834c6dff | 8336 | || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) |
9ed9e79a MM |
8337 | expand_static_init (decl, init); |
8338 | } | |
8d08fdba MS |
8339 | finish_end0: |
8340 | ||
8341 | /* Undo call to `pushclass' that was done in `start_decl' | |
8342 | due to initialization of qualified member variable. | |
8343 | I.e., Foo::x = 10; */ | |
8344 | { | |
4f1c5b7d | 8345 | tree context = CP_DECL_CONTEXT (decl); |
8d08fdba | 8346 | if (context |
2f939d94 | 8347 | && TYPE_P (context) |
8d08fdba MS |
8348 | && (TREE_CODE (decl) == VAR_DECL |
8349 | /* We also have a pushclass done that we need to undo here | |
8350 | if we're at top level and declare a method. */ | |
5566b478 MS |
8351 | || TREE_CODE (decl) == FUNCTION_DECL) |
8352 | /* If size hasn't been set, we're still defining it, | |
8353 | and therefore inside the class body; don't pop | |
8354 | the binding level.. */ | |
d0f062fb | 8355 | && COMPLETE_TYPE_P (context) |
5566b478 | 8356 | && context == current_class_type) |
6b400b21 | 8357 | pop_nested_class (); |
8d08fdba MS |
8358 | } |
8359 | } | |
8360 | ||
8361 | finish_end: | |
8362 | ||
8d08fdba MS |
8363 | if (was_readonly) |
8364 | TREE_READONLY (decl) = 1; | |
8d08fdba MS |
8365 | } |
8366 | ||
82580166 | 8367 | /* This is here for a midend callback from c-common.c */ |
e92cc029 | 8368 | |
82580166 MS |
8369 | void |
8370 | finish_decl (decl, init, asmspec_tree) | |
8371 | tree decl, init; | |
8372 | tree asmspec_tree; | |
8373 | { | |
cd9f6678 | 8374 | cp_finish_decl (decl, init, asmspec_tree, 0); |
82580166 MS |
8375 | } |
8376 | ||
db4283a0 MM |
8377 | /* Returns a declaration for a VAR_DECL as if: |
8378 | ||
8379 | extern "C" TYPE NAME; | |
8380 | ||
8381 | had been seen. Used to create compiler-generated global | |
8382 | variables. */ | |
8383 | ||
8384 | tree | |
8385 | declare_global_var (name, type) | |
8386 | tree name; | |
8387 | tree type; | |
8388 | { | |
8389 | tree decl; | |
8390 | ||
8391 | push_to_top_level (); | |
8392 | decl = build_decl (VAR_DECL, name, type); | |
8393 | TREE_PUBLIC (decl) = 1; | |
8394 | DECL_EXTERNAL (decl) = 1; | |
8395 | DECL_ARTIFICIAL (decl) = 1; | |
8396 | pushdecl (decl); | |
8397 | cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0); | |
8398 | pop_from_top_level (); | |
8399 | ||
8400 | return decl; | |
8401 | } | |
8402 | ||
8403 | /* Returns a pointer to the `atexit' function. Note that if | |
8404 | FLAG_USE_CXA_ATEXIT is non-zero, then this will actually be the new | |
8405 | `__cxa_atexit' function specified in the IA64 C++ ABI. */ | |
8406 | ||
8407 | static tree | |
8408 | get_atexit_node () | |
8409 | { | |
8410 | tree atexit_fndecl; | |
8411 | tree arg_types; | |
8412 | tree fn_type; | |
8413 | tree fn_ptr_type; | |
8414 | const char *name; | |
8415 | ||
8416 | if (atexit_node) | |
8417 | return atexit_node; | |
8418 | ||
8419 | if (flag_use_cxa_atexit) | |
8420 | { | |
8421 | /* The declaration for `__cxa_atexit' is: | |
8422 | ||
8423 | int __cxa_atexit (void (*)(void *), void *, void *) | |
8424 | ||
8425 | We build up the argument types and then then function type | |
8426 | itself. */ | |
68642fb6 | 8427 | |
db4283a0 MM |
8428 | /* First, build the pointer-to-function type for the first |
8429 | argument. */ | |
8430 | arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node); | |
8431 | fn_type = build_function_type (void_type_node, arg_types); | |
8432 | fn_ptr_type = build_pointer_type (fn_type); | |
8433 | /* Then, build the rest of the argument types. */ | |
8434 | arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node); | |
8435 | arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types); | |
8436 | arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types); | |
8437 | /* And the final __cxa_atexit type. */ | |
8438 | fn_type = build_function_type (integer_type_node, arg_types); | |
8439 | fn_ptr_type = build_pointer_type (fn_type); | |
8440 | name = "__cxa_atexit"; | |
8441 | } | |
8442 | else | |
8443 | { | |
8444 | /* The declaration for `atexit' is: | |
68642fb6 | 8445 | |
db4283a0 MM |
8446 | int atexit (void (*)()); |
8447 | ||
8448 | We build up the argument types and then then function type | |
8449 | itself. */ | |
8450 | fn_type = build_function_type (void_type_node, void_list_node); | |
8451 | fn_ptr_type = build_pointer_type (fn_type); | |
8452 | arg_types = tree_cons (NULL_TREE, fn_ptr_type, void_list_node); | |
8453 | /* Build the final atexit type. */ | |
8454 | fn_type = build_function_type (integer_type_node, arg_types); | |
8455 | name = "atexit"; | |
8456 | } | |
8457 | ||
8458 | /* Now, build the function declaration. */ | |
8459 | push_lang_context (lang_name_c); | |
0c11ada6 | 8460 | atexit_fndecl = build_library_fn_ptr (name, fn_type); |
db4283a0 MM |
8461 | mark_used (atexit_fndecl); |
8462 | pop_lang_context (); | |
8463 | atexit_node = default_conversion (atexit_fndecl); | |
8464 | ||
8465 | return atexit_node; | |
8466 | } | |
8467 | ||
8468 | /* Returns the __dso_handle VAR_DECL. */ | |
8469 | ||
8470 | static tree | |
8471 | get_dso_handle_node () | |
8472 | { | |
8473 | if (dso_handle_node) | |
8474 | return dso_handle_node; | |
8475 | ||
8476 | /* Declare the variable. */ | |
8477 | dso_handle_node = declare_global_var (get_identifier ("__dso_handle"), | |
8478 | ptr_type_node); | |
8479 | ||
8480 | return dso_handle_node; | |
8481 | } | |
8482 | ||
8483 | /* Begin a new function with internal linkage whose job will be simply | |
8484 | to destroy some particular variable. */ | |
8485 | ||
8486 | static tree | |
8487 | start_cleanup_fn () | |
8488 | { | |
8489 | static int counter = 0; | |
8490 | int old_interface_unknown = interface_unknown; | |
8491 | char name[32]; | |
8492 | tree parmtypes; | |
8493 | tree fntype; | |
8494 | tree fndecl; | |
8495 | ||
8496 | push_to_top_level (); | |
8497 | ||
8498 | /* No need to mangle this. */ | |
8499 | push_lang_context (lang_name_c); | |
8500 | ||
8501 | interface_unknown = 1; | |
8502 | ||
8503 | /* Build the parameter-types. */ | |
8504 | parmtypes = void_list_node; | |
8505 | /* Functions passed to __cxa_atexit take an additional parameter. | |
8506 | We'll just ignore it. After we implement the new calling | |
8507 | convention for destructors, we can eliminate the use of | |
8508 | additional cleanup functions entirely in the -fnew-abi case. */ | |
8509 | if (flag_use_cxa_atexit) | |
8510 | parmtypes = tree_cons (NULL_TREE, ptr_type_node, parmtypes); | |
8511 | /* Build the function type itself. */ | |
8512 | fntype = build_function_type (void_type_node, parmtypes); | |
8513 | /* Build the name of the function. */ | |
8514 | sprintf (name, "__tcf_%d", counter++); | |
8515 | /* Build the function declaration. */ | |
8516 | fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype); | |
8517 | /* It's a function with internal linkage, generated by the | |
8518 | compiler. */ | |
8519 | TREE_PUBLIC (fndecl) = 0; | |
8520 | DECL_ARTIFICIAL (fndecl) = 1; | |
5a728aca MM |
8521 | /* Make the function `inline' so that it is only emitted if it is |
8522 | actually needed. It is unlikely that it will be inlined, since | |
aba649ba | 8523 | it is only called via a function pointer, but we avoid unnecessary |
5a728aca MM |
8524 | emissions this way. */ |
8525 | DECL_INLINE (fndecl) = 1; | |
db4283a0 MM |
8526 | /* Build the parameter. */ |
8527 | if (flag_use_cxa_atexit) | |
8528 | { | |
8529 | tree parmdecl; | |
8530 | ||
8531 | parmdecl = build_decl (PARM_DECL, NULL_TREE, ptr_type_node); | |
8532 | DECL_CONTEXT (parmdecl) = fndecl; | |
8533 | DECL_ARG_TYPE (parmdecl) = ptr_type_node; | |
8534 | TREE_USED (parmdecl) = 1; | |
8535 | DECL_ARGUMENTS (fndecl) = parmdecl; | |
8536 | } | |
8537 | ||
09ed39ad | 8538 | pushdecl (fndecl); |
db4283a0 | 8539 | start_function (/*specs=*/NULL_TREE, fndecl, NULL_TREE, SF_PRE_PARSED); |
db4283a0 MM |
8540 | |
8541 | interface_unknown = old_interface_unknown; | |
8542 | ||
8543 | pop_lang_context (); | |
8544 | ||
8545 | return current_function_decl; | |
8546 | } | |
8547 | ||
8548 | /* Finish the cleanup function begun by start_cleanup_fn. */ | |
8549 | ||
8550 | static void | |
8551 | end_cleanup_fn () | |
8552 | { | |
0acf7199 | 8553 | expand_body (finish_function (0)); |
db4283a0 MM |
8554 | |
8555 | pop_from_top_level (); | |
8556 | } | |
8557 | ||
bf419747 MM |
8558 | /* Generate code to handle the destruction of DECL, an object with |
8559 | static storage duration. */ | |
f0105ed3 | 8560 | |
bf419747 MM |
8561 | void |
8562 | register_dtor_fn (decl) | |
f0105ed3 MM |
8563 | tree decl; |
8564 | { | |
db4283a0 | 8565 | tree cleanup; |
f0105ed3 | 8566 | tree compound_stmt; |
db4283a0 MM |
8567 | tree args; |
8568 | tree fcall; | |
f0105ed3 | 8569 | |
db4283a0 | 8570 | int saved_flag_access_control; |
f0105ed3 | 8571 | |
834c6dff | 8572 | if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl))) |
bf419747 MM |
8573 | return; |
8574 | ||
f0105ed3 MM |
8575 | /* Call build_cleanup before we enter the anonymous function so that |
8576 | any access checks will be done relative to the current scope, | |
8577 | rather than the scope of the anonymous function. */ | |
8578 | build_cleanup (decl); | |
8579 | ||
8580 | /* Now start the function. */ | |
db4283a0 | 8581 | cleanup = start_cleanup_fn (); |
f0105ed3 MM |
8582 | |
8583 | /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer | |
8584 | to the original function, rather than the anonymous one. That | |
8585 | will make the back-end think that nested functions are in use, | |
8586 | which causes confusion. */ | |
8587 | saved_flag_access_control = flag_access_control; | |
8588 | flag_access_control = 0; | |
8589 | fcall = build_cleanup (decl); | |
8590 | flag_access_control = saved_flag_access_control; | |
8591 | ||
8592 | /* Create the body of the anonymous function. */ | |
8593 | compound_stmt = begin_compound_stmt (/*has_no_scope=*/0); | |
8594 | finish_expr_stmt (fcall); | |
8595 | finish_compound_stmt (/*has_no_scope=*/0, compound_stmt); | |
db4283a0 | 8596 | end_cleanup_fn (); |
f0105ed3 MM |
8597 | |
8598 | /* Call atexit with the cleanup function. */ | |
dffd7eb6 | 8599 | cxx_mark_addressable (cleanup); |
f0105ed3 | 8600 | cleanup = build_unary_op (ADDR_EXPR, cleanup, 0); |
db4283a0 MM |
8601 | if (flag_use_cxa_atexit) |
8602 | { | |
8603 | args = tree_cons (NULL_TREE, get_dso_handle_node (), NULL_TREE); | |
8604 | args = tree_cons (NULL_TREE, null_pointer_node, args); | |
8605 | args = tree_cons (NULL_TREE, cleanup, args); | |
8606 | } | |
8607 | else | |
8608 | args = tree_cons (NULL_TREE, cleanup, NULL_TREE); | |
8609 | finish_expr_stmt (build_function_call (get_atexit_node (), args)); | |
f0105ed3 MM |
8610 | } |
8611 | ||
8d08fdba MS |
8612 | void |
8613 | expand_static_init (decl, init) | |
8614 | tree decl; | |
8615 | tree init; | |
8616 | { | |
8617 | tree oldstatic = value_member (decl, static_aggregates); | |
a4443a08 | 8618 | |
8d08fdba MS |
8619 | if (oldstatic) |
8620 | { | |
8621 | if (TREE_PURPOSE (oldstatic) && init != NULL_TREE) | |
33bd39a2 | 8622 | error ("multiple initializations given for `%D'", decl); |
8d08fdba | 8623 | } |
0aafb128 | 8624 | else if (! toplevel_bindings_p ()) |
8d08fdba MS |
8625 | { |
8626 | /* Emit code to perform this initialization but once. */ | |
b7b8bcd2 | 8627 | tree if_stmt; |
f0105ed3 | 8628 | tree then_clause; |
f1dedc31 | 8629 | tree assignment; |
c395453c MM |
8630 | tree guard; |
8631 | tree guard_init; | |
8d08fdba | 8632 | |
2036a15c MM |
8633 | /* Emit code to perform this initialization but once. This code |
8634 | looks like: | |
8635 | ||
c395453c MM |
8636 | static int guard = 0; |
8637 | if (!guard) { | |
2036a15c | 8638 | // Do initialization. |
c395453c | 8639 | guard = 1; |
2036a15c MM |
8640 | // Register variable for destruction at end of program. |
8641 | } | |
8642 | ||
8643 | Note that the `temp' variable is only set to 1 *after* the | |
8644 | initialization is complete. This ensures that an exception, | |
8645 | thrown during the construction, will cause the variable to | |
8646 | reinitialized when we pass through this code again, as per: | |
68642fb6 | 8647 | |
2036a15c MM |
8648 | [stmt.dcl] |
8649 | ||
8650 | If the initialization exits by throwing an exception, the | |
8651 | initialization is not complete, so it will be tried again | |
8652 | the next time control enters the declaration. | |
8653 | ||
8654 | In theory, this process should be thread-safe, too; multiple | |
8655 | threads should not be able to initialize the variable more | |
8656 | than once. We don't yet attempt to ensure thread-safety. */ | |
c395453c MM |
8657 | |
8658 | /* Create the guard variable. */ | |
8659 | guard = get_guard (decl); | |
2036a15c MM |
8660 | |
8661 | /* Begin the conditional initialization. */ | |
b7b8bcd2 | 8662 | if_stmt = begin_if_stmt (); |
c395453c | 8663 | finish_if_stmt_cond (get_guard_cond (guard), if_stmt); |
f0105ed3 | 8664 | then_clause = begin_compound_stmt (/*has_no_scope=*/0); |
72b7eeff | 8665 | |
2036a15c | 8666 | /* Do the initialization itself. */ |
28cbf42c | 8667 | if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)) |
f30432d7 | 8668 | || (init && TREE_CODE (init) == TREE_LIST)) |
f1dedc31 | 8669 | assignment = build_aggr_init (decl, init, 0); |
f30432d7 | 8670 | else if (init) |
c557501d MM |
8671 | /* The initialization we're doing here is just a bitwise |
8672 | copy. */ | |
8673 | assignment = build (INIT_EXPR, TREE_TYPE (decl), decl, init); | |
f1dedc31 MM |
8674 | else |
8675 | assignment = NULL_TREE; | |
8676 | ||
8677 | /* Once the assignment is complete, set TEMP to 1. Since the | |
8678 | construction of the static object is complete at this point, | |
8679 | we want to make sure TEMP is set to 1 even if a temporary | |
8680 | constructed during the initialization throws an exception | |
8681 | when it is destroyed. So, we combine the initialization and | |
8682 | the assignment to TEMP into a single expression, ensuring | |
8683 | that when we call finish_expr_stmt the cleanups will not be | |
8684 | run until after TEMP is set to 1. */ | |
c395453c | 8685 | guard_init = set_guard (guard); |
f1dedc31 MM |
8686 | if (assignment) |
8687 | { | |
8688 | assignment = tree_cons (NULL_TREE, assignment, | |
68642fb6 | 8689 | build_tree_list (NULL_TREE, |
c395453c | 8690 | guard_init)); |
f1dedc31 MM |
8691 | assignment = build_compound_expr (assignment); |
8692 | } | |
8693 | else | |
c395453c | 8694 | assignment = guard_init; |
f1dedc31 | 8695 | finish_expr_stmt (assignment); |
72b7eeff | 8696 | |
2036a15c MM |
8697 | /* Use atexit to register a function for destroying this static |
8698 | variable. */ | |
bf419747 | 8699 | register_dtor_fn (decl); |
72b7eeff | 8700 | |
f0105ed3 | 8701 | finish_compound_stmt (/*has_no_scope=*/0, then_clause); |
b7b8bcd2 MM |
8702 | finish_then_clause (if_stmt); |
8703 | finish_if_stmt (); | |
8d08fdba MS |
8704 | } |
8705 | else | |
bbd15aac | 8706 | static_aggregates = tree_cons (init, decl, static_aggregates); |
8d08fdba | 8707 | } |
3c5c0849 MM |
8708 | |
8709 | /* Finish the declaration of a catch-parameter. */ | |
8710 | ||
b35d4555 | 8711 | tree |
3c5c0849 MM |
8712 | start_handler_parms (declspecs, declarator) |
8713 | tree declspecs; | |
8714 | tree declarator; | |
8715 | { | |
8716 | tree decl; | |
8717 | if (declspecs) | |
8718 | { | |
8719 | decl = grokdeclarator (declarator, declspecs, CATCHPARM, | |
91d231cb | 8720 | 1, NULL); |
3c5c0849 MM |
8721 | if (decl == NULL_TREE) |
8722 | error ("invalid catch parameter"); | |
8723 | } | |
8724 | else | |
8725 | decl = NULL_TREE; | |
b35d4555 MM |
8726 | |
8727 | return decl; | |
3c5c0849 MM |
8728 | } |
8729 | ||
8d08fdba MS |
8730 | \f |
8731 | /* Make TYPE a complete type based on INITIAL_VALUE. | |
8732 | Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered, | |
6ab5c740 | 8733 | 2 if there was no information (in which case assume 0 if DO_DEFAULT). */ |
8d08fdba MS |
8734 | |
8735 | int | |
8736 | complete_array_type (type, initial_value, do_default) | |
8737 | tree type, initial_value; | |
8738 | int do_default; | |
8739 | { | |
8740 | register tree maxindex = NULL_TREE; | |
8741 | int value = 0; | |
68642fb6 | 8742 | |
8d08fdba MS |
8743 | if (initial_value) |
8744 | { | |
7b019c19 MM |
8745 | /* An array of character type can be initialized from a |
8746 | brace-enclosed string constant. */ | |
8747 | if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))) | |
8748 | && TREE_CODE (initial_value) == CONSTRUCTOR | |
8749 | && CONSTRUCTOR_ELTS (initial_value) | |
8750 | && (TREE_CODE (TREE_VALUE (CONSTRUCTOR_ELTS (initial_value))) | |
8751 | == STRING_CST) | |
8752 | && TREE_CHAIN (CONSTRUCTOR_ELTS (initial_value)) == NULL_TREE) | |
8753 | initial_value = TREE_VALUE (CONSTRUCTOR_ELTS (initial_value)); | |
8754 | ||
8755 | /* Note MAXINDEX is really the maximum index, one less than the | |
8756 | size. */ | |
8d08fdba | 8757 | if (TREE_CODE (initial_value) == STRING_CST) |
e1cd6e56 MS |
8758 | { |
8759 | int eltsize | |
8760 | = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value))); | |
8761 | maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value) | |
8762 | / eltsize) - 1, 0); | |
8763 | } | |
8d08fdba MS |
8764 | else if (TREE_CODE (initial_value) == CONSTRUCTOR) |
8765 | { | |
e1cd6e56 | 8766 | tree elts = CONSTRUCTOR_ELTS (initial_value); |
fed3cef0 RK |
8767 | |
8768 | maxindex = ssize_int (-1); | |
e1cd6e56 MS |
8769 | for (; elts; elts = TREE_CHAIN (elts)) |
8770 | { | |
8771 | if (TREE_PURPOSE (elts)) | |
8772 | maxindex = TREE_PURPOSE (elts); | |
8773 | else | |
fed3cef0 | 8774 | maxindex = size_binop (PLUS_EXPR, maxindex, ssize_int (1)); |
e1cd6e56 MS |
8775 | } |
8776 | maxindex = copy_node (maxindex); | |
8d08fdba MS |
8777 | } |
8778 | else | |
8779 | { | |
8780 | /* Make an error message unless that happened already. */ | |
8781 | if (initial_value != error_mark_node) | |
8782 | value = 1; | |
0db982be ML |
8783 | else |
8784 | initial_value = NULL_TREE; | |
8d08fdba MS |
8785 | |
8786 | /* Prevent further error messages. */ | |
8787 | maxindex = build_int_2 (0, 0); | |
8788 | } | |
8789 | } | |
8790 | ||
8791 | if (!maxindex) | |
8792 | { | |
8793 | if (do_default) | |
8794 | maxindex = build_int_2 (0, 0); | |
8795 | value = 2; | |
8796 | } | |
8797 | ||
8798 | if (maxindex) | |
8799 | { | |
51c184be | 8800 | tree itype; |
6ab5c740 NS |
8801 | tree domain; |
8802 | ||
8803 | domain = build_index_type (maxindex); | |
8804 | TYPE_DOMAIN (type) = domain; | |
51c184be | 8805 | |
dff6b454 | 8806 | if (! TREE_TYPE (maxindex)) |
6ab5c740 | 8807 | TREE_TYPE (maxindex) = domain; |
51c184be MS |
8808 | if (initial_value) |
8809 | itype = TREE_TYPE (initial_value); | |
8810 | else | |
8811 | itype = NULL; | |
8812 | if (itype && !TYPE_DOMAIN (itype)) | |
6ab5c740 | 8813 | TYPE_DOMAIN (itype) = domain; |
dff6b454 RK |
8814 | /* The type of the main variant should never be used for arrays |
8815 | of different sizes. It should only ever be completed with the | |
8816 | size of the array. */ | |
8817 | if (! TYPE_DOMAIN (TYPE_MAIN_VARIANT (type))) | |
6ab5c740 | 8818 | TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)) = domain; |
8d08fdba MS |
8819 | } |
8820 | ||
8821 | /* Lay out the type now that we can get the real answer. */ | |
8822 | ||
8823 | layout_type (type); | |
8824 | ||
8825 | return value; | |
8826 | } | |
8827 | \f | |
8828 | /* Return zero if something is declared to be a member of type | |
8829 | CTYPE when in the context of CUR_TYPE. STRING is the error | |
8830 | message to print in that case. Otherwise, quietly return 1. */ | |
e92cc029 | 8831 | |
8d08fdba | 8832 | static int |
4dacf5bd | 8833 | member_function_or_else (ctype, cur_type, flags) |
8d08fdba | 8834 | tree ctype, cur_type; |
4dacf5bd | 8835 | enum overload_flags flags; |
8d08fdba MS |
8836 | { |
8837 | if (ctype && ctype != cur_type) | |
8838 | { | |
4dacf5bd | 8839 | if (flags == DTOR_FLAG) |
33bd39a2 | 8840 | error ("destructor for alien class `%T' cannot be a member", |
2ae7bada | 8841 | ctype); |
4dacf5bd | 8842 | else |
33bd39a2 | 8843 | error ("constructor for alien class `%T' cannot be a member", |
2ae7bada | 8844 | ctype); |
8d08fdba MS |
8845 | return 0; |
8846 | } | |
8847 | return 1; | |
8848 | } | |
8849 | \f | |
8850 | /* Subroutine of `grokdeclarator'. */ | |
8851 | ||
8852 | /* Generate errors possibly applicable for a given set of specifiers. | |
8853 | This is for ARM $7.1.2. */ | |
e92cc029 | 8854 | |
8d08fdba MS |
8855 | static void |
8856 | bad_specifiers (object, type, virtualp, quals, inlinep, friendp, raises) | |
8857 | tree object; | |
d8e178a0 | 8858 | const char *type; |
8d08fdba MS |
8859 | int virtualp, quals, friendp, raises, inlinep; |
8860 | { | |
8861 | if (virtualp) | |
33bd39a2 | 8862 | error ("`%D' declared as a `virtual' %s", object, type); |
8d08fdba | 8863 | if (inlinep) |
33bd39a2 | 8864 | error ("`%D' declared as an `inline' %s", object, type); |
8d08fdba | 8865 | if (quals) |
33bd39a2 | 8866 | error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration", |
a28e3c7f | 8867 | object, type); |
8d08fdba | 8868 | if (friendp) |
f8e55f34 | 8869 | cp_error_at ("`%D' declared as a friend", object); |
e1be26f4 RS |
8870 | if (raises |
8871 | && (TREE_CODE (object) == TYPE_DECL | |
8872 | || (!TYPE_PTRFN_P (TREE_TYPE (object)) | |
742a37d5 | 8873 | && !TYPE_REFFN_P (TREE_TYPE (object)) |
e1be26f4 | 8874 | && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object))))) |
f8e55f34 | 8875 | cp_error_at ("`%D' declared with an exception specification", object); |
8d08fdba MS |
8876 | } |
8877 | ||
8878 | /* CTYPE is class type, or null if non-class. | |
8879 | TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE | |
8880 | or METHOD_TYPE. | |
8881 | DECLARATOR is the function's name. | |
8882 | VIRTUALP is truthvalue of whether the function is virtual or not. | |
8883 | FLAGS are to be passed through to `grokclassfn'. | |
8884 | QUALS are qualifiers indicating whether the function is `const' | |
8885 | or `volatile'. | |
8886 | RAISES is a list of exceptions that this function can raise. | |
8887 | CHECK is 1 if we must find this method in CTYPE, 0 if we should | |
68642fb6 | 8888 | not look, and -1 if we should not call `grokclassfn' at all. |
3ddfb0e6 | 8889 | |
20496fa2 | 8890 | Returns `NULL_TREE' if something goes wrong, after issuing |
3ddfb0e6 | 8891 | applicable error messages. */ |
e92cc029 | 8892 | |
8d08fdba | 8893 | static tree |
386b8a85 | 8894 | grokfndecl (ctype, type, declarator, orig_declarator, virtualp, flags, quals, |
7a8f9fa9 | 8895 | raises, check, friendp, publicp, inlinep, funcdef_flag, |
2c73f9f5 | 8896 | template_count, in_namespace) |
8d08fdba MS |
8897 | tree ctype, type; |
8898 | tree declarator; | |
386b8a85 | 8899 | tree orig_declarator; |
8d08fdba MS |
8900 | int virtualp; |
8901 | enum overload_flags flags; | |
7a8f9fa9 | 8902 | tree quals, raises; |
386b8a85 | 8903 | int check, friendp, publicp, inlinep, funcdef_flag, template_count; |
2c73f9f5 | 8904 | tree in_namespace; |
8d08fdba | 8905 | { |
1951a1b6 | 8906 | tree decl; |
8d08fdba | 8907 | int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE; |
856216bb | 8908 | int has_default_arg = 0; |
42976354 | 8909 | tree t; |
8d08fdba | 8910 | |
8d08fdba | 8911 | if (raises) |
271e6f02 | 8912 | type = build_exception_variant (type, raises); |
c11b6f21 | 8913 | |
8d08fdba | 8914 | decl = build_lang_decl (FUNCTION_DECL, declarator, type); |
2c73f9f5 | 8915 | /* Propagate volatile out from type to decl. */ |
8d08fdba | 8916 | if (TYPE_VOLATILE (type)) |
893de33c | 8917 | TREE_THIS_VOLATILE (decl) = 1; |
8d08fdba | 8918 | |
79c4d4b7 | 8919 | /* If this decl has namespace scope, set that up. */ |
2c73f9f5 | 8920 | if (in_namespace) |
b262d64c | 8921 | set_decl_namespace (decl, in_namespace, friendp); |
adae082f | 8922 | else if (!ctype) |
79c4d4b7 | 8923 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); |
2c73f9f5 | 8924 | |
0f8766b8 JM |
8925 | /* `main' and builtins have implicit 'C' linkage. */ |
8926 | if ((MAIN_NAME_P (declarator) | |
8927 | || (IDENTIFIER_LENGTH (declarator) > 10 | |
8928 | && IDENTIFIER_POINTER (declarator)[0] == '_' | |
8929 | && IDENTIFIER_POINTER (declarator)[1] == '_' | |
8930 | && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0)) | |
8931 | && current_lang_name == lang_name_cplusplus | |
94706a5c | 8932 | && ctype == NULL_TREE |
79c4d4b7 JM |
8933 | /* NULL_TREE means global namespace. */ |
8934 | && DECL_CONTEXT (decl) == NULL_TREE) | |
5d2ed28c | 8935 | SET_DECL_LANGUAGE (decl, lang_c); |
0f8766b8 | 8936 | |
8d08fdba MS |
8937 | /* Should probably propagate const out from type to decl I bet (mrs). */ |
8938 | if (staticp) | |
8939 | { | |
8940 | DECL_STATIC_FUNCTION_P (decl) = 1; | |
8941 | DECL_CONTEXT (decl) = ctype; | |
8d08fdba MS |
8942 | } |
8943 | ||
e76a2646 | 8944 | if (ctype) |
4f1c5b7d | 8945 | DECL_CONTEXT (decl) = ctype; |
e76a2646 | 8946 | |
0f8766b8 | 8947 | if (ctype == NULL_TREE && DECL_MAIN_P (decl)) |
faae18ab | 8948 | { |
848b92e1 | 8949 | if (processing_template_decl) |
cb9a3ff8 | 8950 | error ("cannot declare `::main' to be a template"); |
faae18ab | 8951 | if (inlinep) |
cb9a3ff8 | 8952 | error ("cannot declare `::main' to be inline"); |
f22967f3 | 8953 | if (!publicp) |
cb9a3ff8 | 8954 | error ("cannot declare `::main' to be static"); |
f22967f3 MM |
8955 | if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)), |
8956 | integer_type_node)) | |
8957 | error ("`main' must return `int'"); | |
faae18ab MS |
8958 | inlinep = 0; |
8959 | publicp = 1; | |
8960 | } | |
50a6dbd7 | 8961 | |
59e76fc6 JM |
8962 | /* Members of anonymous types and local classes have no linkage; make |
8963 | them internal. */ | |
1951a1b6 JM |
8964 | /* FIXME what if it gets a name from typedef? */ |
8965 | if (ctype && (TYPE_ANONYMOUS_P (ctype) | |
4f1c5b7d | 8966 | || decl_function_context (TYPE_MAIN_DECL (ctype)))) |
50a6dbd7 JM |
8967 | publicp = 0; |
8968 | ||
8969 | if (publicp) | |
8970 | { | |
8971 | /* [basic.link]: A name with no linkage (notably, the name of a class | |
8972 | or enumeration declared in a local scope) shall not be used to | |
8973 | declare an entity with linkage. | |
8974 | ||
8975 | Only check this for public decls for now. */ | |
8976 | t = no_linkage_check (TREE_TYPE (decl)); | |
8977 | if (t) | |
8978 | { | |
1951a1b6 | 8979 | if (TYPE_ANONYMOUS_P (t)) |
7f7c930e | 8980 | { |
eb68cb58 | 8981 | if (DECL_EXTERN_C_P (decl)) |
7f7c930e JM |
8982 | /* Allow this; it's pretty common in C. */; |
8983 | else | |
1951a1b6 | 8984 | { |
33bd39a2 | 8985 | pedwarn ("non-local function `%#D' uses anonymous type", |
1951a1b6 JM |
8986 | decl); |
8987 | if (DECL_ORIGINAL_TYPE (TYPE_NAME (t))) | |
8988 | cp_pedwarn_at ("\ | |
8989 | `%#D' does not refer to the unqualified type, so it is not used for linkage", | |
8990 | TYPE_NAME (t)); | |
8991 | } | |
7f7c930e | 8992 | } |
50a6dbd7 | 8993 | else |
33bd39a2 | 8994 | pedwarn ("non-local function `%#D' uses local type `%T'", |
cce2be43 | 8995 | decl, t); |
50a6dbd7 JM |
8996 | } |
8997 | } | |
8998 | ||
893de33c | 8999 | TREE_PUBLIC (decl) = publicp; |
faae18ab | 9000 | if (! publicp) |
893de33c JM |
9001 | { |
9002 | DECL_INTERFACE_KNOWN (decl) = 1; | |
9003 | DECL_NOT_REALLY_EXTERN (decl) = 1; | |
9004 | } | |
faae18ab | 9005 | |
acc72c37 | 9006 | /* If the declaration was declared inline, mark it as such. */ |
faae18ab | 9007 | if (inlinep) |
acc72c37 MM |
9008 | DECL_DECLARED_INLINE_P (decl) = 1; |
9009 | /* We inline functions that are explicitly declared inline, or, when | |
9010 | the user explicitly asks us to, all functions. */ | |
9011 | if (DECL_DECLARED_INLINE_P (decl) || flag_inline_trees == 2) | |
9012 | DECL_INLINE (decl) = 1; | |
8d08fdba MS |
9013 | |
9014 | DECL_EXTERNAL (decl) = 1; | |
9015 | if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE) | |
9016 | { | |
33bd39a2 | 9017 | error ("%smember function `%D' cannot have `%T' method qualifier", |
8d08fdba MS |
9018 | (ctype ? "static " : "non-"), decl, TREE_VALUE (quals)); |
9019 | quals = NULL_TREE; | |
9020 | } | |
9021 | ||
596ea4e5 | 9022 | if (IDENTIFIER_OPNAME_P (DECL_NAME (decl))) |
271e6f02 | 9023 | grok_op_properties (decl, friendp); |
8d08fdba | 9024 | |
4f1c5b7d | 9025 | if (ctype && decl_function_context (decl)) |
893de33c | 9026 | DECL_NO_STATIC_CHAIN (decl) = 1; |
e76a2646 | 9027 | |
42976354 BK |
9028 | for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t)) |
9029 | if (TREE_PURPOSE (t) | |
9030 | && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG) | |
9031 | { | |
856216bb | 9032 | has_default_arg = 1; |
42976354 BK |
9033 | break; |
9034 | } | |
9035 | ||
f9d94ea4 JM |
9036 | if (friendp |
9037 | && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR) | |
9038 | { | |
9039 | if (funcdef_flag) | |
33bd39a2 | 9040 | error |
8251199e | 9041 | ("defining explicit specialization `%D' in friend declaration", |
f9d94ea4 JM |
9042 | orig_declarator); |
9043 | else | |
9044 | { | |
76e57b45 NS |
9045 | tree fns = TREE_OPERAND (orig_declarator, 0); |
9046 | tree args = TREE_OPERAND (orig_declarator, 1); | |
d363e7bf | 9047 | |
7e2421f7 MM |
9048 | if (PROCESSING_REAL_TEMPLATE_DECL_P ()) |
9049 | { | |
9050 | /* Something like `template <class T> friend void f<T>()'. */ | |
33bd39a2 | 9051 | error ("invalid use of template-id `%D' in declaration of primary template", |
7e2421f7 | 9052 | orig_declarator); |
20496fa2 | 9053 | return NULL_TREE; |
7e2421f7 MM |
9054 | } |
9055 | ||
856216bb | 9056 | |
f9d94ea4 JM |
9057 | /* A friend declaration of the form friend void f<>(). Record |
9058 | the information in the TEMPLATE_ID_EXPR. */ | |
9059 | SET_DECL_IMPLICIT_INSTANTIATION (decl); | |
76e57b45 NS |
9060 | |
9061 | if (TREE_CODE (fns) == COMPONENT_REF) | |
9062 | { | |
9063 | /* Due to bison parser ickiness, we will have already looked | |
9064 | up an operator_name or PFUNCNAME within the current class | |
9065 | (see template_id in parse.y). If the current class contains | |
9066 | such a name, we'll get a COMPONENT_REF here. Undo that. */ | |
d363e7bf | 9067 | |
76e57b45 NS |
9068 | my_friendly_assert (TREE_TYPE (TREE_OPERAND (fns, 0)) |
9069 | == current_class_type, 20001120); | |
9070 | fns = TREE_OPERAND (fns, 1); | |
9071 | } | |
9072 | my_friendly_assert (TREE_CODE (fns) == IDENTIFIER_NODE | |
9073 | || TREE_CODE (fns) == LOOKUP_EXPR | |
9074 | || TREE_CODE (fns) == OVERLOAD, 20001120); | |
9075 | DECL_TEMPLATE_INFO (decl) = tree_cons (fns, args, NULL_TREE); | |
856216bb MM |
9076 | |
9077 | if (has_default_arg) | |
9078 | { | |
33bd39a2 | 9079 | error ("default arguments are not allowed in declaration of friend template specialization `%D'", |
856216bb MM |
9080 | decl); |
9081 | return NULL_TREE; | |
9082 | } | |
9083 | ||
9084 | if (inlinep) | |
9085 | { | |
33bd39a2 | 9086 | error ("`inline' is not allowed in declaration of friend template specialization `%D'", |
856216bb MM |
9087 | decl); |
9088 | return NULL_TREE; | |
9089 | } | |
f9d94ea4 | 9090 | } |
f84b4be9 | 9091 | } |
386b8a85 | 9092 | |
856216bb MM |
9093 | if (has_default_arg) |
9094 | add_defarg_fn (decl); | |
9095 | ||
1eb0072d JM |
9096 | if (funcdef_flag) |
9097 | /* Make the init_value nonzero so pushdecl knows this is not | |
9098 | tentative. error_mark_node is replaced later with the BLOCK. */ | |
9099 | DECL_INITIAL (decl) = error_mark_node; | |
9100 | ||
93ca4ba7 | 9101 | if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl)) |
1660cb3a JM |
9102 | TREE_NOTHROW (decl) = 1; |
9103 | ||
75650646 | 9104 | /* Caller will do the rest of this. */ |
8d08fdba MS |
9105 | if (check < 0) |
9106 | return decl; | |
9107 | ||
1951a1b6 | 9108 | if (flags == NO_SPECIAL && ctype && constructor_name (ctype) == declarator) |
74b846e0 MM |
9109 | DECL_CONSTRUCTOR_P (decl) = 1; |
9110 | ||
9111 | /* Function gets the ugly name, field gets the nice one. This call | |
9112 | may change the type of the function (because of default | |
9113 | parameters)! */ | |
9114 | if (ctype != NULL_TREE) | |
9115 | grokclassfn (ctype, decl, flags, quals); | |
9116 | ||
9117 | decl = check_explicit_specialization (orig_declarator, decl, | |
9118 | template_count, | |
9119 | 2 * (funcdef_flag != 0) + | |
9120 | 4 * (friendp != 0)); | |
9121 | if (decl == error_mark_node) | |
9122 | return NULL_TREE; | |
98c1c668 | 9123 | |
74b846e0 MM |
9124 | if (ctype != NULL_TREE |
9125 | && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl)) | |
9126 | && check) | |
8d08fdba | 9127 | { |
74b846e0 | 9128 | tree old_decl; |
8d08fdba | 9129 | |
74b846e0 | 9130 | old_decl = check_classfn (ctype, decl); |
8d08fdba | 9131 | |
74b846e0 MM |
9132 | if (old_decl && TREE_CODE (old_decl) == TEMPLATE_DECL) |
9133 | /* Because grokfndecl is always supposed to return a | |
9134 | FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT | |
9135 | here. We depend on our callers to figure out that its | |
9136 | really a template that's being returned. */ | |
9137 | old_decl = DECL_TEMPLATE_RESULT (old_decl); | |
6c30752f | 9138 | |
74b846e0 MM |
9139 | if (old_decl && DECL_STATIC_FUNCTION_P (old_decl) |
9140 | && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE) | |
5566b478 | 9141 | { |
74b846e0 MM |
9142 | /* Remove the `this' parm added by grokclassfn. |
9143 | XXX Isn't this done in start_function, too? */ | |
3afb32a4 | 9144 | revert_static_member_fn (decl); |
74b846e0 | 9145 | last_function_parms = TREE_CHAIN (last_function_parms); |
5566b478 | 9146 | } |
74b846e0 | 9147 | if (old_decl && DECL_ARTIFICIAL (old_decl)) |
33bd39a2 | 9148 | error ("definition of implicitly-declared `%D'", old_decl); |
8d08fdba | 9149 | |
74b846e0 | 9150 | if (old_decl) |
8d08fdba | 9151 | { |
74b846e0 MM |
9152 | /* Since we've smashed OLD_DECL to its |
9153 | DECL_TEMPLATE_RESULT, we must do the same to DECL. */ | |
9154 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
9155 | decl = DECL_TEMPLATE_RESULT (decl); | |
9156 | ||
9157 | /* Attempt to merge the declarations. This can fail, in | |
9158 | the case of some illegal specialization declarations. */ | |
9159 | if (!duplicate_decls (decl, old_decl)) | |
33bd39a2 | 9160 | error ("no `%#D' member function declared in class `%T'", |
74b846e0 MM |
9161 | decl, ctype); |
9162 | return old_decl; | |
8d08fdba MS |
9163 | } |
9164 | } | |
74b846e0 MM |
9165 | |
9166 | if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl)) | |
9167 | return NULL_TREE; | |
9168 | ||
9169 | if (ctype == NULL_TREE || check) | |
9170 | return decl; | |
9171 | ||
9172 | if (virtualp) | |
cbb40945 | 9173 | DECL_VIRTUAL_P (decl) = 1; |
74b846e0 | 9174 | |
8d08fdba MS |
9175 | return decl; |
9176 | } | |
9177 | ||
9178 | static tree | |
2c73f9f5 | 9179 | grokvardecl (type, declarator, specbits_in, initialized, constp, in_namespace) |
8d08fdba MS |
9180 | tree type; |
9181 | tree declarator; | |
d2e5ee5c | 9182 | RID_BIT_TYPE *specbits_in; |
8d08fdba | 9183 | int initialized; |
a9aedbc2 | 9184 | int constp; |
2c73f9f5 | 9185 | tree in_namespace; |
8d08fdba MS |
9186 | { |
9187 | tree decl; | |
f7da6097 MS |
9188 | RID_BIT_TYPE specbits; |
9189 | ||
9190 | specbits = *specbits_in; | |
8d08fdba MS |
9191 | |
9192 | if (TREE_CODE (type) == OFFSET_TYPE) | |
9193 | { | |
9194 | /* If you declare a static member so that it | |
9195 | can be initialized, the code will reach here. */ | |
5b605f68 MS |
9196 | tree basetype = TYPE_OFFSET_BASETYPE (type); |
9197 | type = TREE_TYPE (type); | |
4ce3d537 | 9198 | decl = build_lang_decl (VAR_DECL, declarator, type); |
5b605f68 | 9199 | DECL_CONTEXT (decl) = basetype; |
8d08fdba MS |
9200 | } |
9201 | else | |
30394414 | 9202 | { |
79c4d4b7 JM |
9203 | tree context; |
9204 | ||
9205 | if (in_namespace) | |
9206 | context = in_namespace; | |
9207 | else if (namespace_bindings_p () || RIDBIT_SETP (RID_EXTERN, specbits)) | |
9208 | context = current_namespace; | |
820fcad8 JM |
9209 | else |
9210 | context = NULL_TREE; | |
79c4d4b7 | 9211 | |
5d2ed28c MM |
9212 | /* For namespace-scope variables, declared in a template, we |
9213 | need the full lang_decl. The same is true for | |
9214 | namespace-scope variables that do not have C++ language | |
9215 | linkage. */ | |
9216 | if (context | |
9217 | && (processing_template_decl | |
9218 | || current_lang_name != lang_name_cplusplus)) | |
cd9f6678 | 9219 | decl = build_lang_decl (VAR_DECL, declarator, type); |
9188c363 | 9220 | else |
c82dbd95 | 9221 | decl = build_decl (VAR_DECL, declarator, type); |
79c4d4b7 JM |
9222 | |
9223 | if (context) | |
b262d64c | 9224 | set_decl_namespace (decl, context, 0); |
79c4d4b7 JM |
9225 | |
9226 | context = DECL_CONTEXT (decl); | |
5362b086 | 9227 | if (declarator && context && current_lang_name != lang_name_c) |
92643fea MM |
9228 | /* We can't mangle lazily here because we don't have any |
9229 | way to recover whether or not a variable was `extern | |
9230 | "C"' later. */ | |
9231 | mangle_decl (decl); | |
30394414 | 9232 | } |
6060a796 | 9233 | |
2c73f9f5 | 9234 | if (in_namespace) |
b262d64c | 9235 | set_decl_namespace (decl, in_namespace, 0); |
2c73f9f5 | 9236 | |
8d08fdba MS |
9237 | if (RIDBIT_SETP (RID_EXTERN, specbits)) |
9238 | { | |
9239 | DECL_THIS_EXTERN (decl) = 1; | |
9240 | DECL_EXTERNAL (decl) = !initialized; | |
9241 | } | |
9242 | ||
9243 | /* In class context, static means one per class, | |
9244 | public access, and static storage. */ | |
2b9dc906 | 9245 | if (DECL_CLASS_SCOPE_P (decl)) |
8d08fdba MS |
9246 | { |
9247 | TREE_PUBLIC (decl) = 1; | |
9248 | TREE_STATIC (decl) = 1; | |
5b605f68 | 9249 | DECL_EXTERNAL (decl) = 0; |
8d08fdba MS |
9250 | } |
9251 | /* At top level, either `static' or no s.c. makes a definition | |
9252 | (perhaps tentative), and absence of `static' makes it public. */ | |
a9aedbc2 | 9253 | else if (toplevel_bindings_p ()) |
8d08fdba | 9254 | { |
a9aedbc2 | 9255 | TREE_PUBLIC (decl) = (RIDBIT_NOTSETP (RID_STATIC, specbits) |
9fffd093 | 9256 | && (DECL_THIS_EXTERN (decl) || ! constp)); |
8d08fdba MS |
9257 | TREE_STATIC (decl) = ! DECL_EXTERNAL (decl); |
9258 | } | |
9259 | /* Not at top level, only `static' makes a static definition. */ | |
9260 | else | |
9261 | { | |
9262 | TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits); | |
9263 | TREE_PUBLIC (decl) = DECL_EXTERNAL (decl); | |
9264 | } | |
50a6dbd7 JM |
9265 | |
9266 | if (TREE_PUBLIC (decl)) | |
9267 | { | |
9268 | /* [basic.link]: A name with no linkage (notably, the name of a class | |
9269 | or enumeration declared in a local scope) shall not be used to | |
9270 | declare an entity with linkage. | |
9271 | ||
9272 | Only check this for public decls for now. */ | |
9273 | tree t = no_linkage_check (TREE_TYPE (decl)); | |
9274 | if (t) | |
9275 | { | |
1951a1b6 | 9276 | if (TYPE_ANONYMOUS_P (t)) |
50a6dbd7 JM |
9277 | /* Ignore for now; `enum { foo } e' is pretty common. */; |
9278 | else | |
33bd39a2 | 9279 | pedwarn ("non-local variable `%#D' uses local type `%T'", |
50a6dbd7 JM |
9280 | decl, t); |
9281 | } | |
9282 | } | |
9283 | ||
8d08fdba MS |
9284 | return decl; |
9285 | } | |
9286 | ||
d8f8dca1 MM |
9287 | /* Create and return a canonical pointer to member function type, for |
9288 | TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */ | |
8d08fdba MS |
9289 | |
9290 | tree | |
9291 | build_ptrmemfunc_type (type) | |
9292 | tree type; | |
9293 | { | |
9294 | tree fields[4]; | |
9295 | tree t; | |
46cbda4a | 9296 | tree unqualified_variant = NULL_TREE; |
8d08fdba | 9297 | |
d48ebde1 NS |
9298 | if (type == error_mark_node) |
9299 | return type; | |
d363e7bf | 9300 | |
8d08fdba MS |
9301 | /* If a canonical type already exists for this type, use it. We use |
9302 | this method instead of type_hash_canon, because it only does a | |
9303 | simple equality check on the list of field members. */ | |
9304 | ||
9305 | if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type))) | |
9306 | return t; | |
9307 | ||
46cbda4a MM |
9308 | /* Make sure that we always have the unqualified pointer-to-member |
9309 | type first. */ | |
89d684bb | 9310 | if (cp_type_quals (type) != TYPE_UNQUALIFIED) |
68642fb6 | 9311 | unqualified_variant |
46cbda4a MM |
9312 | = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type)); |
9313 | ||
33848bb0 | 9314 | t = make_aggr_type (RECORD_TYPE); |
2c73f9f5 | 9315 | /* Let the front-end know this is a pointer to member function... */ |
db5ae43f | 9316 | TYPE_PTRMEMFUNC_FLAG (t) = 1; |
2c73f9f5 | 9317 | /* ... and not really an aggregate. */ |
7ddedda4 | 9318 | SET_IS_AGGR_TYPE (t, 0); |
8d08fdba | 9319 | |
1f84ec23 MM |
9320 | fields[0] = build_decl (FIELD_DECL, pfn_identifier, type); |
9321 | fields[1] = build_decl (FIELD_DECL, delta_identifier, | |
9322 | delta_type_node); | |
18ae7f63 | 9323 | finish_builtin_type (t, "__ptrmemfunc_type", fields, 1, ptr_type_node); |
8d08fdba | 9324 | |
8d08fdba MS |
9325 | /* Zap out the name so that the back-end will give us the debugging |
9326 | information for this anonymous RECORD_TYPE. */ | |
9327 | TYPE_NAME (t) = NULL_TREE; | |
9328 | ||
46cbda4a MM |
9329 | /* If this is not the unqualified form of this pointer-to-member |
9330 | type, set the TYPE_MAIN_VARIANT for this type to be the | |
9331 | unqualified type. Since they are actually RECORD_TYPEs that are | |
9332 | not variants of each other, we must do this manually. */ | |
89d684bb | 9333 | if (cp_type_quals (type) != TYPE_UNQUALIFIED) |
46cbda4a | 9334 | { |
89d684bb | 9335 | t = build_qualified_type (t, cp_type_quals (type)); |
46cbda4a MM |
9336 | TYPE_MAIN_VARIANT (t) = unqualified_variant; |
9337 | TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant); | |
9338 | TYPE_NEXT_VARIANT (unqualified_variant) = t; | |
9339 | } | |
9340 | ||
9341 | /* Cache this pointer-to-member type so that we can find it again | |
9342 | later. */ | |
8d08fdba MS |
9343 | TYPE_SET_PTRMEMFUNC_TYPE (type, t); |
9344 | ||
e92cc029 | 9345 | /* Seems to be wanted. */ |
8d08fdba | 9346 | CLASSTYPE_GOT_SEMICOLON (t) = 1; |
46cbda4a | 9347 | |
8d08fdba MS |
9348 | return t; |
9349 | } | |
9350 | ||
b17e2870 JM |
9351 | /* DECL is a VAR_DECL defined in-class, whose TYPE is also given. |
9352 | Check to see that the definition is valid. Issue appropriate error | |
9353 | messages. Return 1 if the definition is particularly bad, or 0 | |
9354 | otherwise. */ | |
9355 | ||
9356 | int | |
9357 | check_static_variable_definition (decl, type) | |
9358 | tree decl; | |
9359 | tree type; | |
9360 | { | |
9361 | /* Motion 10 at San Diego: If a static const integral data member is | |
9362 | initialized with an integral constant expression, the initializer | |
9363 | may appear either in the declaration (within the class), or in | |
9364 | the definition, but not both. If it appears in the class, the | |
9365 | member is a member constant. The file-scope definition is always | |
9366 | required. */ | |
9367 | if (CLASS_TYPE_P (type) || TREE_CODE (type) == REFERENCE_TYPE) | |
9368 | { | |
33bd39a2 | 9369 | error ("invalid in-class initialization of static data member of non-integral type `%T'", |
b17e2870 JM |
9370 | type); |
9371 | /* If we just return the declaration, crashes will sometimes | |
9372 | occur. We therefore return void_type_node, as if this was a | |
9373 | friend declaration, to cause callers to completely ignore | |
9374 | this declaration. */ | |
9375 | return 1; | |
9376 | } | |
9377 | else if (!CP_TYPE_CONST_P (type)) | |
33bd39a2 | 9378 | error ("ISO C++ forbids in-class initialization of non-const static member `%D'", |
b17e2870 JM |
9379 | decl); |
9380 | else if (pedantic && !INTEGRAL_TYPE_P (type)) | |
33bd39a2 | 9381 | pedwarn ("ISO C++ forbids initialization of member constant `%D' of non-integral type `%T'", decl, type); |
b17e2870 JM |
9382 | |
9383 | return 0; | |
9384 | } | |
9385 | ||
2848ad0f MM |
9386 | /* Given the SIZE (i.e., number of elements) in an array, compute an |
9387 | appropriate index type for the array. If non-NULL, NAME is the | |
9388 | name of the thing being declared. */ | |
9389 | ||
c95cd22e | 9390 | tree |
2848ad0f MM |
9391 | compute_array_index_type (name, size) |
9392 | tree name; | |
9393 | tree size; | |
9394 | { | |
9395 | tree itype; | |
9396 | ||
2848ad0f MM |
9397 | /* If this involves a template parameter, it will be a constant at |
9398 | instantiation time, but we don't know what the value is yet. | |
9399 | Even if no template parameters are involved, we may an expression | |
9400 | that is not a constant; we don't even simplify `1 + 2' when | |
9401 | processing a template. */ | |
9402 | if (processing_template_decl) | |
9403 | { | |
9404 | /* Resolve a qualified reference to an enumerator or static | |
9405 | const data member of ours. */ | |
9406 | if (TREE_CODE (size) == SCOPE_REF | |
9407 | && TREE_OPERAND (size, 0) == current_class_type) | |
9408 | { | |
9409 | tree t = lookup_field (current_class_type, | |
9410 | TREE_OPERAND (size, 1), 0, 0); | |
9411 | if (t) | |
9412 | size = t; | |
9413 | } | |
9414 | ||
9415 | return build_index_type (build_min (MINUS_EXPR, sizetype, | |
9416 | size, integer_one_node)); | |
9417 | } | |
9418 | ||
80f5bb34 MM |
9419 | /* The size might be the result of a cast. */ |
9420 | STRIP_TYPE_NOPS (size); | |
9421 | ||
9422 | /* It might be a const variable or enumeration constant. */ | |
9423 | size = decl_constant_value (size); | |
9424 | ||
2848ad0f MM |
9425 | /* The array bound must be an integer type. */ |
9426 | if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE | |
9427 | && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE | |
9428 | && TREE_CODE (TREE_TYPE (size)) != BOOLEAN_TYPE) | |
9429 | { | |
d67cdbc3 | 9430 | if (name) |
33bd39a2 | 9431 | error ("size of array `%D' has non-integer type", name); |
d67cdbc3 | 9432 | else |
33bd39a2 | 9433 | error ("size of array has non-integer type"); |
2848ad0f MM |
9434 | size = integer_one_node; |
9435 | } | |
9436 | ||
9437 | /* Normally, the array-bound will be a constant. */ | |
2bb5d995 | 9438 | if (TREE_CODE (size) == INTEGER_CST) |
2848ad0f MM |
9439 | { |
9440 | /* Check to see if the array bound overflowed. Make that an | |
9441 | error, no matter how generous we're being. */ | |
9442 | int old_flag_pedantic_errors = flag_pedantic_errors; | |
9443 | int old_pedantic = pedantic; | |
9444 | pedantic = flag_pedantic_errors = 1; | |
9445 | constant_expression_warning (size); | |
9446 | pedantic = old_pedantic; | |
9447 | flag_pedantic_errors = old_flag_pedantic_errors; | |
9448 | ||
9449 | /* An array must have a positive number of elements. */ | |
9450 | if (INT_CST_LT (size, integer_zero_node)) | |
9451 | { | |
d67cdbc3 | 9452 | if (name) |
33bd39a2 | 9453 | error ("size of array `%D' is negative", name); |
d67cdbc3 | 9454 | else |
33bd39a2 | 9455 | error ("size of array is negative"); |
2848ad0f MM |
9456 | size = integer_one_node; |
9457 | } | |
9458 | /* Except that an extension we allow zero-sized arrays. We | |
68642fb6 | 9459 | always allow them in system headers because glibc uses |
2848ad0f MM |
9460 | them. */ |
9461 | else if (integer_zerop (size) && pedantic && !in_system_header) | |
d67cdbc3 JM |
9462 | { |
9463 | if (name) | |
33bd39a2 | 9464 | pedwarn ("ISO C++ forbids zero-size array `%D'", name); |
d67cdbc3 | 9465 | else |
33bd39a2 | 9466 | pedwarn ("ISO C++ forbids zero-size array"); |
d67cdbc3 | 9467 | } |
2848ad0f | 9468 | } |
2bb5d995 JM |
9469 | else if (TREE_CONSTANT (size)) |
9470 | { | |
9471 | /* `(int) &fn' is not a valid array bound. */ | |
9472 | if (name) | |
33bd39a2 | 9473 | error ("size of array `%D' is not an integral constant-expression", |
2bb5d995 JM |
9474 | name); |
9475 | else | |
33bd39a2 | 9476 | error ("size of array is not an integral constant-expression"); |
2bb5d995 | 9477 | } |
2848ad0f MM |
9478 | |
9479 | /* Compute the index of the largest element in the array. It is | |
9480 | one less than the number of elements in the array. */ | |
9481 | itype | |
ab76ca54 MM |
9482 | = fold (cp_build_binary_op (MINUS_EXPR, |
9483 | cp_convert (ssizetype, size), | |
9484 | cp_convert (ssizetype, | |
9485 | integer_one_node))); | |
68642fb6 | 9486 | |
2848ad0f MM |
9487 | /* Check for variable-sized arrays. We allow such things as an |
9488 | extension, even though they are not allowed in ANSI/ISO C++. */ | |
9489 | if (!TREE_CONSTANT (itype)) | |
9490 | { | |
9491 | if (pedantic) | |
9492 | { | |
9493 | if (name) | |
33bd39a2 | 9494 | pedwarn ("ISO C++ forbids variable-size array `%D'", |
2848ad0f MM |
9495 | name); |
9496 | else | |
33bd39a2 | 9497 | pedwarn ("ISO C++ forbids variable-size array"); |
2848ad0f MM |
9498 | } |
9499 | ||
9500 | /* Create a variable-sized array index type. */ | |
9501 | itype = variable_size (itype); | |
9502 | } | |
9503 | /* Make sure that there was no overflow when creating to a signed | |
9504 | index type. (For example, on a 32-bit machine, an array with | |
9505 | size 2^32 - 1 is too big.) */ | |
9506 | else if (TREE_OVERFLOW (itype)) | |
9507 | { | |
9508 | error ("overflow in array dimension"); | |
9509 | TREE_OVERFLOW (itype) = 0; | |
9510 | } | |
68642fb6 | 9511 | |
2848ad0f MM |
9512 | /* Create and return the appropriate index type. */ |
9513 | return build_index_type (itype); | |
9514 | } | |
9515 | ||
9516 | /* Returns an ARRAY_TYPE for an array with SIZE elements of the | |
9517 | indicated TYPE. If non-NULL, NAME is the NAME of the declaration | |
9518 | with this type. */ | |
9519 | ||
9520 | static tree | |
9521 | create_array_type_for_decl (name, type, size) | |
9522 | tree name; | |
9523 | tree type; | |
9524 | tree size; | |
9525 | { | |
9526 | tree itype = NULL_TREE; | |
9527 | const char* error_msg; | |
9528 | ||
9529 | /* If things have already gone awry, bail now. */ | |
9530 | if (type == error_mark_node || size == error_mark_node) | |
9531 | return error_mark_node; | |
9532 | ||
9533 | /* Assume that everything will go OK. */ | |
9534 | error_msg = NULL; | |
9535 | ||
9536 | /* There are some types which cannot be array elements. */ | |
9537 | switch (TREE_CODE (type)) | |
9538 | { | |
9539 | case VOID_TYPE: | |
9540 | error_msg = "array of void"; | |
9541 | break; | |
9542 | ||
9543 | case FUNCTION_TYPE: | |
9544 | error_msg = "array of functions"; | |
9545 | break; | |
9546 | ||
9547 | case REFERENCE_TYPE: | |
9548 | error_msg = "array of references"; | |
9549 | break; | |
9550 | ||
9551 | case OFFSET_TYPE: | |
9552 | error_msg = "array of data members"; | |
9553 | break; | |
9554 | ||
9555 | case METHOD_TYPE: | |
9556 | error_msg = "array of function members"; | |
9557 | break; | |
9558 | ||
9559 | default: | |
9560 | break; | |
9561 | } | |
9562 | ||
9563 | /* If something went wrong, issue an error-message and return. */ | |
9564 | if (error_msg) | |
9565 | { | |
9566 | if (name) | |
33bd39a2 | 9567 | error ("declaration of `%D' as %s", name, error_msg); |
2848ad0f | 9568 | else |
33bd39a2 | 9569 | error ("creating %s", error_msg); |
2848ad0f MM |
9570 | |
9571 | return error_mark_node; | |
9572 | } | |
9573 | ||
9574 | /* [dcl.array] | |
68642fb6 | 9575 | |
2848ad0f MM |
9576 | The constant expressions that specify the bounds of the arrays |
9577 | can be omitted only for the first member of the sequence. */ | |
9578 | if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)) | |
9579 | { | |
b3faacfd | 9580 | if (name) |
33bd39a2 | 9581 | error ("declaration of `%D' as multidimensional array must have bounds for all dimensions except the first", |
b3faacfd KL |
9582 | name); |
9583 | else | |
33bd39a2 | 9584 | error ("multidimensional array must have bounds for all dimensions except the first"); |
2848ad0f MM |
9585 | |
9586 | return error_mark_node; | |
9587 | } | |
9588 | ||
9589 | /* Figure out the index type for the array. */ | |
9590 | if (size) | |
9591 | itype = compute_array_index_type (name, size); | |
9592 | ||
9593 | return build_cplus_array_type (type, itype); | |
9594 | } | |
9595 | ||
3dbc07b6 MM |
9596 | /* Check that it's OK to declare a function with the indicated TYPE. |
9597 | SFK indicates the kind of special function (if any) that this | |
1f84ec23 | 9598 | function is. OPTYPE is the type given in a conversion operator |
3dbc07b6 MM |
9599 | declaration. Returns the actual return type of the function; that |
9600 | may be different than TYPE if an error occurs, or for certain | |
9601 | special functions. */ | |
9602 | ||
9603 | static tree | |
1f84ec23 | 9604 | check_special_function_return_type (sfk, type, optype) |
3dbc07b6 MM |
9605 | special_function_kind sfk; |
9606 | tree type; | |
3dbc07b6 MM |
9607 | tree optype; |
9608 | { | |
9609 | switch (sfk) | |
9610 | { | |
9611 | case sfk_constructor: | |
9612 | if (type) | |
33bd39a2 | 9613 | error ("return type specification for constructor invalid"); |
5362b086 | 9614 | |
1f84ec23 | 9615 | type = void_type_node; |
3dbc07b6 MM |
9616 | break; |
9617 | ||
9618 | case sfk_destructor: | |
9619 | if (type) | |
33bd39a2 | 9620 | error ("return type specification for destructor invalid"); |
3dbc07b6 MM |
9621 | type = void_type_node; |
9622 | break; | |
9623 | ||
9624 | case sfk_conversion: | |
9625 | if (type && !same_type_p (type, optype)) | |
33bd39a2 | 9626 | error ("operator `%T' declared to return `%T'", optype, type); |
3dbc07b6 | 9627 | else if (type) |
33bd39a2 | 9628 | pedwarn ("return type specified for `operator %T'", optype); |
3dbc07b6 MM |
9629 | type = optype; |
9630 | break; | |
9631 | ||
9632 | default: | |
a98facb0 | 9633 | abort (); |
3dbc07b6 MM |
9634 | break; |
9635 | } | |
9636 | ||
9637 | return type; | |
9638 | } | |
9639 | ||
8d08fdba MS |
9640 | /* Given declspecs and a declarator, |
9641 | determine the name and type of the object declared | |
9642 | and construct a ..._DECL node for it. | |
9643 | (In one case we can return a ..._TYPE node instead. | |
9644 | For invalid input we sometimes return 0.) | |
9645 | ||
9646 | DECLSPECS is a chain of tree_list nodes whose value fields | |
9647 | are the storage classes and type specifiers. | |
9648 | ||
9649 | DECL_CONTEXT says which syntactic context this declaration is in: | |
9650 | NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL. | |
9651 | FUNCDEF for a function definition. Like NORMAL but a few different | |
9652 | error messages in each case. Return value may be zero meaning | |
9653 | this definition is too screwy to try to parse. | |
9654 | MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to | |
9655 | handle member functions (which have FIELD context). | |
9656 | Return value may be zero meaning this definition is too screwy to | |
9657 | try to parse. | |
9658 | PARM for a parameter declaration (either within a function prototype | |
9659 | or before a function body). Make a PARM_DECL, or return void_type_node. | |
db5ae43f | 9660 | CATCHPARM for a parameter declaration before a catch clause. |
8d08fdba MS |
9661 | TYPENAME if for a typename (in a cast or sizeof). |
9662 | Don't make a DECL node; just return the ..._TYPE node. | |
9663 | FIELD for a struct or union field; make a FIELD_DECL. | |
9664 | BITFIELD for a field with specified width. | |
9665 | INITIALIZED is 1 if the decl has an initializer. | |
9666 | ||
91d231cb JM |
9667 | ATTRLIST is a pointer to the list of attributes, which may be NULL |
9668 | if there are none; *ATTRLIST may be modified if attributes from inside | |
9669 | the declarator should be applied to the declaration. | |
b17e2870 | 9670 | |
70adf8a9 | 9671 | In the TYPENAME case, DECLARATOR is really an abstract declarator. |
8d08fdba MS |
9672 | It may also be so in the PARM case, for a prototype where the |
9673 | argument type is specified but not the name. | |
9674 | ||
9675 | This function is where the complicated C meanings of `static' | |
9676 | and `extern' are interpreted. | |
9677 | ||
9678 | For C++, if there is any monkey business to do, the function which | |
9679 | calls this one must do it, i.e., prepending instance variables, | |
9680 | renaming overloaded function names, etc. | |
9681 | ||
9682 | Note that for this C++, it is an error to define a method within a class | |
9683 | which does not belong to that class. | |
9684 | ||
9685 | Except in the case where SCOPE_REFs are implicitly known (such as | |
9686 | methods within a class being redundantly qualified), | |
9687 | declarations which involve SCOPE_REFs are returned as SCOPE_REFs | |
9688 | (class_name::decl_name). The caller must also deal with this. | |
9689 | ||
9690 | If a constructor or destructor is seen, and the context is FIELD, | |
9691 | then the type gains the attribute TREE_HAS_x. If such a declaration | |
9692 | is erroneous, NULL_TREE is returned. | |
9693 | ||
9694 | QUALS is used only for FUNCDEF and MEMFUNCDEF cases. For a member | |
535233a8 NS |
9695 | function, these are the qualifiers to give to the `this' pointer. We |
9696 | apply TYPE_QUAL_RESTRICT to the this ptr, not the object. | |
8d08fdba MS |
9697 | |
9698 | May return void_type_node if the declarator turned out to be a friend. | |
9699 | See grokfield for details. */ | |
9700 | ||
8d08fdba | 9701 | tree |
c11b6f21 | 9702 | grokdeclarator (declarator, declspecs, decl_context, initialized, attrlist) |
8d08fdba MS |
9703 | tree declspecs; |
9704 | tree declarator; | |
9705 | enum decl_context decl_context; | |
9706 | int initialized; | |
91d231cb | 9707 | tree *attrlist; |
8d08fdba MS |
9708 | { |
9709 | RID_BIT_TYPE specbits; | |
9710 | int nclasses = 0; | |
9711 | tree spec; | |
9712 | tree type = NULL_TREE; | |
9713 | int longlong = 0; | |
91063b51 | 9714 | int type_quals; |
db5ae43f | 9715 | int virtualp, explicitp, friendp, inlinep, staticp; |
8d08fdba MS |
9716 | int explicit_int = 0; |
9717 | int explicit_char = 0; | |
37c46b43 | 9718 | int defaulted_int = 0; |
270d8c65 | 9719 | int extern_langp = 0; |
d5614afb | 9720 | tree dependant_name = NULL_TREE; |
270d8c65 | 9721 | |
8d08fdba | 9722 | tree typedef_decl = NULL_TREE; |
9c0758dd | 9723 | const char *name; |
8d08fdba MS |
9724 | tree typedef_type = NULL_TREE; |
9725 | int funcdef_flag = 0; | |
9726 | enum tree_code innermost_code = ERROR_MARK; | |
9727 | int bitfield = 0; | |
6125f3be DE |
9728 | #if 0 |
9729 | /* See the code below that used this. */ | |
91d231cb | 9730 | tree decl_attr = NULL_TREE; |
6125f3be | 9731 | #endif |
8d08fdba MS |
9732 | /* Set this to error_mark_node for FIELD_DECLs we could not handle properly. |
9733 | All FIELD_DECLs we build here have `init' put into their DECL_INITIAL. */ | |
9734 | tree init = NULL_TREE; | |
9735 | ||
9736 | /* Keep track of what sort of function is being processed | |
9737 | so that we can warn about default return values, or explicit | |
9738 | return values which do not match prescribed defaults. */ | |
3dbc07b6 | 9739 | special_function_kind sfk = sfk_none; |
8d08fdba MS |
9740 | |
9741 | tree dname = NULL_TREE; | |
9742 | tree ctype = current_class_type; | |
9743 | tree ctor_return_type = NULL_TREE; | |
9744 | enum overload_flags flags = NO_SPECIAL; | |
8d08fdba | 9745 | tree quals = NULL_TREE; |
c11b6f21 | 9746 | tree raises = NULL_TREE; |
386b8a85 | 9747 | int template_count = 0; |
2c73f9f5 | 9748 | tree in_namespace = NULL_TREE; |
91d231cb | 9749 | tree returned_attrs = NULL_TREE; |
8d08fdba MS |
9750 | |
9751 | RIDBIT_RESET_ALL (specbits); | |
9752 | if (decl_context == FUNCDEF) | |
9753 | funcdef_flag = 1, decl_context = NORMAL; | |
9754 | else if (decl_context == MEMFUNCDEF) | |
9755 | funcdef_flag = -1, decl_context = FIELD; | |
9756 | else if (decl_context == BITFIELD) | |
9757 | bitfield = 1, decl_context = FIELD; | |
9758 | ||
8d08fdba MS |
9759 | /* Look inside a declarator for the name being declared |
9760 | and get it as a string, for an error message. */ | |
9761 | { | |
be99da77 MS |
9762 | tree *next = &declarator; |
9763 | register tree decl; | |
8d08fdba MS |
9764 | name = NULL; |
9765 | ||
be99da77 MS |
9766 | while (next && *next) |
9767 | { | |
9768 | decl = *next; | |
9769 | switch (TREE_CODE (decl)) | |
8d08fdba | 9770 | { |
52fbc847 JM |
9771 | case TREE_LIST: |
9772 | /* For attributes. */ | |
9773 | next = &TREE_VALUE (decl); | |
9774 | break; | |
9775 | ||
be99da77 MS |
9776 | case COND_EXPR: |
9777 | ctype = NULL_TREE; | |
9778 | next = &TREE_OPERAND (decl, 0); | |
9779 | break; | |
8d08fdba | 9780 | |
2c73f9f5 | 9781 | case BIT_NOT_EXPR: /* For C++ destructors! */ |
8d08fdba | 9782 | { |
be99da77 MS |
9783 | tree name = TREE_OPERAND (decl, 0); |
9784 | tree rename = NULL_TREE; | |
9785 | ||
9786 | my_friendly_assert (flags == NO_SPECIAL, 152); | |
9787 | flags = DTOR_FLAG; | |
3dbc07b6 | 9788 | sfk = sfk_destructor; |
5566b478 MS |
9789 | if (TREE_CODE (name) == TYPE_DECL) |
9790 | TREE_OPERAND (decl, 0) = name = constructor_name (name); | |
be99da77 MS |
9791 | my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153); |
9792 | if (ctype == NULL_TREE) | |
9793 | { | |
9794 | if (current_class_type == NULL_TREE) | |
9795 | { | |
8251199e | 9796 | error ("destructors must be member functions"); |
be99da77 MS |
9797 | flags = NO_SPECIAL; |
9798 | } | |
9799 | else | |
9800 | { | |
9801 | tree t = constructor_name (current_class_name); | |
9802 | if (t != name) | |
9803 | rename = t; | |
9804 | } | |
9805 | } | |
8d08fdba | 9806 | else |
be99da77 MS |
9807 | { |
9808 | tree t = constructor_name (ctype); | |
9809 | if (t != name) | |
9810 | rename = t; | |
9811 | } | |
51c184be | 9812 | |
be99da77 | 9813 | if (rename) |
39211cd5 | 9814 | { |
33bd39a2 | 9815 | error ("destructor `%T' must match class name `%T'", |
5566b478 | 9816 | name, rename); |
be99da77 | 9817 | TREE_OPERAND (decl, 0) = rename; |
39211cd5 | 9818 | } |
be99da77 | 9819 | next = &name; |
51c184be | 9820 | } |
be99da77 | 9821 | break; |
8d08fdba | 9822 | |
be99da77 | 9823 | case ADDR_EXPR: /* C++ reference declaration */ |
2c73f9f5 | 9824 | /* Fall through. */ |
be99da77 MS |
9825 | case ARRAY_REF: |
9826 | case INDIRECT_REF: | |
9827 | ctype = NULL_TREE; | |
9828 | innermost_code = TREE_CODE (decl); | |
9829 | next = &TREE_OPERAND (decl, 0); | |
9830 | break; | |
8d08fdba | 9831 | |
be99da77 | 9832 | case CALL_EXPR: |
43f887f9 | 9833 | if (parmlist_is_exprlist (CALL_DECLARATOR_PARMS (decl))) |
8d08fdba | 9834 | { |
b17e2870 JM |
9835 | /* This is actually a variable declaration using |
9836 | constructor syntax. We need to call start_decl and | |
9837 | cp_finish_decl so we can get the variable | |
9838 | initialized... */ | |
9839 | ||
91d231cb | 9840 | tree attributes; |
be99da77 MS |
9841 | |
9842 | *next = TREE_OPERAND (decl, 0); | |
43f887f9 | 9843 | init = CALL_DECLARATOR_PARMS (decl); |
be99da77 | 9844 | |
b17e2870 JM |
9845 | if (attrlist) |
9846 | { | |
91d231cb | 9847 | attributes = *attrlist; |
b17e2870 JM |
9848 | } |
9849 | else | |
9850 | { | |
9851 | attributes = NULL_TREE; | |
b17e2870 JM |
9852 | } |
9853 | ||
9854 | decl = start_decl (declarator, declspecs, 1, | |
91d231cb | 9855 | attributes, NULL_TREE); |
9fc336c7 | 9856 | decl_type_access_control (decl); |
1eb0072d JM |
9857 | if (decl) |
9858 | { | |
9859 | /* Look for __unused__ attribute */ | |
9860 | if (TREE_USED (TREE_TYPE (decl))) | |
9861 | TREE_USED (decl) = 1; | |
9862 | finish_decl (decl, init, NULL_TREE); | |
9863 | } | |
9864 | else | |
33bd39a2 | 9865 | error ("invalid declarator"); |
be99da77 | 9866 | return 0; |
8d08fdba | 9867 | } |
be99da77 MS |
9868 | innermost_code = TREE_CODE (decl); |
9869 | if (decl_context == FIELD && ctype == NULL_TREE) | |
9870 | ctype = current_class_type; | |
45537677 | 9871 | if (ctype |
c11b6f21 | 9872 | && TREE_OPERAND (decl, 0) |
45537677 MS |
9873 | && (TREE_CODE (TREE_OPERAND (decl, 0)) == TYPE_DECL |
9874 | && ((DECL_NAME (TREE_OPERAND (decl, 0)) | |
9875 | == constructor_name_full (ctype)) | |
9876 | || (DECL_NAME (TREE_OPERAND (decl, 0)) | |
9877 | == constructor_name (ctype))))) | |
be99da77 MS |
9878 | TREE_OPERAND (decl, 0) = constructor_name (ctype); |
9879 | next = &TREE_OPERAND (decl, 0); | |
9880 | decl = *next; | |
9881 | if (ctype != NULL_TREE | |
9882 | && decl != NULL_TREE && flags != DTOR_FLAG | |
9883 | && decl == constructor_name (ctype)) | |
8d08fdba | 9884 | { |
3dbc07b6 | 9885 | sfk = sfk_constructor; |
be99da77 | 9886 | ctor_return_type = ctype; |
8d08fdba | 9887 | } |
be99da77 MS |
9888 | ctype = NULL_TREE; |
9889 | break; | |
68642fb6 | 9890 | |
386b8a85 JM |
9891 | case TEMPLATE_ID_EXPR: |
9892 | { | |
9893 | tree fns = TREE_OPERAND (decl, 0); | |
9894 | ||
9895 | if (TREE_CODE (fns) == LOOKUP_EXPR) | |
9896 | fns = TREE_OPERAND (fns, 0); | |
9897 | ||
8f032717 MM |
9898 | dname = fns; |
9899 | if (TREE_CODE (dname) == COMPONENT_REF) | |
9900 | dname = TREE_OPERAND (dname, 1); | |
9901 | if (TREE_CODE (dname) != IDENTIFIER_NODE) | |
9902 | { | |
9903 | my_friendly_assert (is_overloaded_fn (dname), | |
9904 | 19990331); | |
9905 | dname = DECL_NAME (get_first_fn (dname)); | |
9906 | } | |
386b8a85 | 9907 | } |
2c73f9f5 | 9908 | /* Fall through. */ |
be99da77 MS |
9909 | |
9910 | case IDENTIFIER_NODE: | |
386b8a85 JM |
9911 | if (TREE_CODE (decl) == IDENTIFIER_NODE) |
9912 | dname = decl; | |
9913 | ||
be99da77 MS |
9914 | next = 0; |
9915 | ||
0e5921e8 | 9916 | if (C_IS_RESERVED_WORD (dname)) |
8d08fdba | 9917 | { |
33bd39a2 | 9918 | error ("declarator-id missing; using reserved word `%D'", |
be99da77 MS |
9919 | dname); |
9920 | name = IDENTIFIER_POINTER (dname); | |
8d08fdba | 9921 | } |
596ea4e5 | 9922 | else if (!IDENTIFIER_TYPENAME_P (dname)) |
be99da77 | 9923 | name = IDENTIFIER_POINTER (dname); |
8d08fdba MS |
9924 | else |
9925 | { | |
596ea4e5 AS |
9926 | my_friendly_assert (flags == NO_SPECIAL, 154); |
9927 | flags = TYPENAME_FLAG; | |
9928 | ctor_return_type = TREE_TYPE (dname); | |
9929 | sfk = sfk_conversion; | |
9930 | if (IDENTIFIER_GLOBAL_VALUE (dname) | |
5362b086 | 9931 | && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (dname)) |
596ea4e5 AS |
9932 | == TYPE_DECL)) |
9933 | name = IDENTIFIER_POINTER (dname); | |
9934 | else | |
9935 | name = "<invalid operator>"; | |
8d08fdba | 9936 | } |
be99da77 | 9937 | break; |
8d08fdba | 9938 | |
be99da77 MS |
9939 | /* C++ extension */ |
9940 | case SCOPE_REF: | |
9941 | { | |
9942 | /* Perform error checking, and decide on a ctype. */ | |
9943 | tree cname = TREE_OPERAND (decl, 0); | |
9944 | if (cname == NULL_TREE) | |
9945 | ctype = NULL_TREE; | |
2c73f9f5 ML |
9946 | else if (TREE_CODE (cname) == NAMESPACE_DECL) |
9947 | { | |
9948 | ctype = NULL_TREE; | |
9949 | in_namespace = TREE_OPERAND (decl, 0); | |
9950 | TREE_OPERAND (decl, 0) = NULL_TREE; | |
9951 | } | |
be99da77 MS |
9952 | else if (! is_aggr_type (cname, 1)) |
9953 | TREE_OPERAND (decl, 0) = NULL_TREE; | |
9954 | /* Must test TREE_OPERAND (decl, 1), in case user gives | |
9955 | us `typedef (class::memfunc)(int); memfunc *memfuncptr;' */ | |
9956 | else if (TREE_OPERAND (decl, 1) | |
9957 | && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF) | |
9958 | ctype = cname; | |
73b0fce8 | 9959 | else if (TREE_CODE (cname) == TEMPLATE_TYPE_PARM |
a1281f45 | 9960 | || TREE_CODE (cname) == BOUND_TEMPLATE_TEMPLATE_PARM) |
5566b478 | 9961 | { |
d5614afb NS |
9962 | /* This might be declaring a member of a template |
9963 | parm to be a friend. */ | |
9964 | ctype = cname; | |
9965 | dependant_name = TREE_OPERAND (decl, 1); | |
5566b478 | 9966 | } |
be99da77 MS |
9967 | else if (ctype == NULL_TREE) |
9968 | ctype = cname; | |
9969 | else if (TREE_COMPLEXITY (decl) == current_class_depth) | |
9970 | TREE_OPERAND (decl, 0) = ctype; | |
9971 | else | |
9972 | { | |
9973 | if (! UNIQUELY_DERIVED_FROM_P (cname, ctype)) | |
9974 | { | |
33bd39a2 | 9975 | error ("type `%T' is not derived from type `%T'", |
be99da77 MS |
9976 | cname, ctype); |
9977 | TREE_OPERAND (decl, 0) = NULL_TREE; | |
9978 | } | |
9979 | else | |
9980 | ctype = cname; | |
9981 | } | |
9982 | ||
c91a56d2 MS |
9983 | if (ctype && TREE_CODE (TREE_OPERAND (decl, 1)) == TYPE_DECL |
9984 | && ((DECL_NAME (TREE_OPERAND (decl, 1)) | |
9985 | == constructor_name_full (ctype)) | |
9986 | || (DECL_NAME (TREE_OPERAND (decl, 1)) | |
9987 | == constructor_name (ctype)))) | |
be99da77 MS |
9988 | TREE_OPERAND (decl, 1) = constructor_name (ctype); |
9989 | next = &TREE_OPERAND (decl, 1); | |
9990 | decl = *next; | |
9991 | if (ctype) | |
9992 | { | |
9993 | if (TREE_CODE (decl) == IDENTIFIER_NODE | |
9994 | && constructor_name (ctype) == decl) | |
9995 | { | |
3dbc07b6 | 9996 | sfk = sfk_constructor; |
be99da77 MS |
9997 | ctor_return_type = ctype; |
9998 | } | |
9999 | else if (TREE_CODE (decl) == BIT_NOT_EXPR | |
10000 | && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE | |
10001 | && (constructor_name (ctype) == TREE_OPERAND (decl, 0) | |
10002 | || constructor_name_full (ctype) == TREE_OPERAND (decl, 0))) | |
10003 | { | |
3dbc07b6 | 10004 | sfk = sfk_destructor; |
be99da77 MS |
10005 | ctor_return_type = ctype; |
10006 | flags = DTOR_FLAG; | |
10007 | TREE_OPERAND (decl, 0) = constructor_name (ctype); | |
10008 | next = &TREE_OPERAND (decl, 0); | |
10009 | } | |
10010 | } | |
10011 | } | |
10012 | break; | |
10013 | ||
10014 | case ERROR_MARK: | |
10015 | next = 0; | |
10016 | break; | |
10017 | ||
45537677 MS |
10018 | case TYPE_DECL: |
10019 | /* Parse error puts this typespec where | |
10020 | a declarator should go. */ | |
33bd39a2 | 10021 | error ("`%T' specified as declarator-id", DECL_NAME (decl)); |
45537677 | 10022 | if (TREE_TYPE (decl) == current_class_type) |
33bd39a2 | 10023 | error (" perhaps you want `%T' for a constructor", |
45537677 MS |
10024 | current_class_name); |
10025 | dname = DECL_NAME (decl); | |
10026 | name = IDENTIFIER_POINTER (dname); | |
10027 | ||
e92cc029 | 10028 | /* Avoid giving two errors for this. */ |
45537677 MS |
10029 | IDENTIFIER_CLASS_VALUE (dname) = NULL_TREE; |
10030 | ||
58010b57 | 10031 | declspecs = tree_cons (NULL_TREE, integer_type_node, declspecs); |
45537677 MS |
10032 | *next = dname; |
10033 | next = 0; | |
10034 | break; | |
10035 | ||
be99da77 | 10036 | default: |
33bd39a2 | 10037 | internal_error ("`%D' as declarator", decl); |
8d08fdba | 10038 | } |
be99da77 | 10039 | } |
8d08fdba MS |
10040 | } |
10041 | ||
10042 | /* A function definition's declarator must have the form of | |
10043 | a function declarator. */ | |
10044 | ||
10045 | if (funcdef_flag && innermost_code != CALL_EXPR) | |
10046 | return 0; | |
10047 | ||
e1cd6e56 MS |
10048 | if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG) |
10049 | && innermost_code != CALL_EXPR | |
10050 | && ! (ctype && declspecs == NULL_TREE)) | |
10051 | { | |
33bd39a2 | 10052 | error ("declaration of `%D' as non-function", dname); |
e1cd6e56 MS |
10053 | return void_type_node; |
10054 | } | |
10055 | ||
8d08fdba MS |
10056 | /* Anything declared one level down from the top level |
10057 | must be one of the parameters of a function | |
10058 | (because the body is at least two levels down). */ | |
10059 | ||
10060 | /* This heuristic cannot be applied to C++ nodes! Fixed, however, | |
10061 | by not allowing C++ class definitions to specify their parameters | |
10062 | with xdecls (must be spec.d in the parmlist). | |
10063 | ||
10064 | Since we now wait to push a class scope until we are sure that | |
10065 | we are in a legitimate method context, we must set oldcname | |
a9aedbc2 MS |
10066 | explicitly (since current_class_name is not yet alive). |
10067 | ||
10068 | We also want to avoid calling this a PARM if it is in a namespace. */ | |
8d08fdba | 10069 | |
9188c363 | 10070 | if (decl_context == NORMAL && !toplevel_bindings_p ()) |
a9aedbc2 MS |
10071 | { |
10072 | struct binding_level *b = current_binding_level; | |
10073 | current_binding_level = b->level_chain; | |
10074 | if (current_binding_level != 0 && toplevel_bindings_p ()) | |
10075 | decl_context = PARM; | |
10076 | current_binding_level = b; | |
10077 | } | |
8d08fdba | 10078 | |
34fc957d NS |
10079 | if (name == NULL) |
10080 | name = decl_context == PARM ? "parameter" : "type name"; | |
68642fb6 | 10081 | |
8d08fdba MS |
10082 | /* Look through the decl specs and record which ones appear. |
10083 | Some typespecs are defined as built-in typenames. | |
10084 | Others, the ones that are modifiers of other types, | |
10085 | are represented by bits in SPECBITS: set the bits for | |
10086 | the modifiers that appear. Storage class keywords are also in SPECBITS. | |
10087 | ||
10088 | If there is a typedef name or a type, store the type in TYPE. | |
10089 | This includes builtin typedefs such as `int'. | |
10090 | ||
10091 | Set EXPLICIT_INT if the type is `int' or `char' and did not | |
10092 | come from a user typedef. | |
10093 | ||
10094 | Set LONGLONG if `long' is mentioned twice. | |
10095 | ||
10096 | For C++, constructors and destructors have their own fast treatment. */ | |
10097 | ||
10098 | for (spec = declspecs; spec; spec = TREE_CHAIN (spec)) | |
10099 | { | |
10100 | register int i; | |
10101 | register tree id; | |
10102 | ||
10103 | /* Certain parse errors slip through. For example, | |
10104 | `int class;' is not caught by the parser. Try | |
10105 | weakly to recover here. */ | |
10106 | if (TREE_CODE (spec) != TREE_LIST) | |
10107 | return 0; | |
10108 | ||
10109 | id = TREE_VALUE (spec); | |
10110 | ||
e23bd218 IR |
10111 | /* If the entire declaration is itself tagged as deprecated then |
10112 | suppress reports of deprecated items. */ | |
10113 | if (!adding_implicit_members && id && TREE_DEPRECATED (id)) | |
10114 | { | |
10115 | if (deprecated_state != DEPRECATED_SUPPRESS) | |
10116 | warn_deprecated_use (id); | |
10117 | } | |
10118 | ||
8d08fdba MS |
10119 | if (TREE_CODE (id) == IDENTIFIER_NODE) |
10120 | { | |
a3203465 MS |
10121 | if (id == ridpointers[(int) RID_INT] |
10122 | || id == ridpointers[(int) RID_CHAR] | |
10123 | || id == ridpointers[(int) RID_BOOL] | |
10124 | || id == ridpointers[(int) RID_WCHAR]) | |
8d08fdba MS |
10125 | { |
10126 | if (type) | |
8ccc31eb MS |
10127 | { |
10128 | if (id == ridpointers[(int) RID_BOOL]) | |
8251199e | 10129 | error ("`bool' is now a keyword"); |
8ccc31eb | 10130 | else |
33bd39a2 | 10131 | error ("extraneous `%T' ignored", id); |
8ccc31eb | 10132 | } |
8d08fdba MS |
10133 | else |
10134 | { | |
a3203465 MS |
10135 | if (id == ridpointers[(int) RID_INT]) |
10136 | explicit_int = 1; | |
10137 | else if (id == ridpointers[(int) RID_CHAR]) | |
10138 | explicit_char = 1; | |
8d08fdba MS |
10139 | type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id)); |
10140 | } | |
10141 | goto found; | |
10142 | } | |
e92cc029 | 10143 | /* C++ aggregate types. */ |
8d08fdba MS |
10144 | if (IDENTIFIER_HAS_TYPE_VALUE (id)) |
10145 | { | |
10146 | if (type) | |
33bd39a2 | 10147 | error ("multiple declarations `%T' and `%T'", type, id); |
8d08fdba MS |
10148 | else |
10149 | type = IDENTIFIER_TYPE_VALUE (id); | |
10150 | goto found; | |
10151 | } | |
10152 | ||
f376e137 | 10153 | for (i = (int) RID_FIRST_MODIFIER; i <= (int) RID_LAST_MODIFIER; i++) |
8d08fdba MS |
10154 | { |
10155 | if (ridpointers[i] == id) | |
10156 | { | |
10157 | if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits)) | |
10158 | { | |
795add94 | 10159 | if (pedantic && ! in_system_header && warn_long_long) |
cb9a3ff8 | 10160 | pedwarn ("ISO C++ does not support `long long'"); |
9a3b49ac | 10161 | if (longlong) |
8251199e | 10162 | error ("`long long long' is too long for GCC"); |
8d08fdba MS |
10163 | else |
10164 | longlong = 1; | |
10165 | } | |
10166 | else if (RIDBIT_SETP (i, specbits)) | |
8251199e | 10167 | pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id)); |
270d8c65 NS |
10168 | if (i == (int)RID_EXTERN |
10169 | && TREE_PURPOSE (spec) == error_mark_node) | |
10170 | /* This extern was part of a language linkage. */ | |
10171 | extern_langp = 1; | |
8d08fdba MS |
10172 | RIDBIT_SET (i, specbits); |
10173 | goto found; | |
10174 | } | |
10175 | } | |
10176 | } | |
1899c3a4 | 10177 | else if (TREE_CODE (id) == TYPE_DECL) |
45537677 MS |
10178 | { |
10179 | if (type) | |
33bd39a2 | 10180 | error ("multiple declarations `%T' and `%T'", type, |
45537677 MS |
10181 | TREE_TYPE (id)); |
10182 | else | |
5566b478 MS |
10183 | { |
10184 | type = TREE_TYPE (id); | |
10185 | TREE_VALUE (spec) = type; | |
8d6e459d | 10186 | typedef_decl = id; |
5566b478 | 10187 | } |
45537677 MS |
10188 | goto found; |
10189 | } | |
8d08fdba | 10190 | if (type) |
8251199e | 10191 | error ("two or more data types in declaration of `%s'", name); |
8d08fdba MS |
10192 | else if (TREE_CODE (id) == IDENTIFIER_NODE) |
10193 | { | |
10194 | register tree t = lookup_name (id, 1); | |
10195 | if (!t || TREE_CODE (t) != TYPE_DECL) | |
8251199e | 10196 | error ("`%s' fails to be a typedef or built in type", |
8d08fdba MS |
10197 | IDENTIFIER_POINTER (id)); |
10198 | else | |
10199 | { | |
10200 | type = TREE_TYPE (t); | |
10201 | typedef_decl = t; | |
10202 | } | |
10203 | } | |
bd6dd845 | 10204 | else if (id != error_mark_node) |
8d08fdba MS |
10205 | /* Can't change CLASS nodes into RECORD nodes here! */ |
10206 | type = id; | |
10207 | ||
10208 | found: ; | |
10209 | } | |
10210 | ||
8d6e459d NS |
10211 | #if 0 |
10212 | /* See the code below that used this. */ | |
10213 | if (typedef_decl) | |
10214 | decl_attr = DECL_ATTRIBUTES (typedef_decl); | |
10215 | #endif | |
8d08fdba MS |
10216 | typedef_type = type; |
10217 | ||
37c46b43 | 10218 | /* No type at all: default to `int', and set DEFAULTED_INT |
6eabb241 | 10219 | because it was not a user-defined typedef. */ |
8d08fdba | 10220 | |
a3203465 MS |
10221 | if (type == NULL_TREE |
10222 | && (RIDBIT_SETP (RID_SIGNED, specbits) | |
10223 | || RIDBIT_SETP (RID_UNSIGNED, specbits) | |
10224 | || RIDBIT_SETP (RID_LONG, specbits) | |
10225 | || RIDBIT_SETP (RID_SHORT, specbits))) | |
10226 | { | |
10227 | /* These imply 'int'. */ | |
10228 | type = integer_type_node; | |
37c46b43 | 10229 | defaulted_int = 1; |
a3203465 MS |
10230 | } |
10231 | ||
3dbc07b6 MM |
10232 | if (sfk != sfk_none) |
10233 | type = check_special_function_return_type (sfk, type, | |
3dbc07b6 MM |
10234 | ctor_return_type); |
10235 | else if (type == NULL_TREE) | |
8d08fdba | 10236 | { |
3dbc07b6 MM |
10237 | int is_main; |
10238 | ||
8d08fdba | 10239 | explicit_int = -1; |
3dbc07b6 | 10240 | |
0fd0b7ee JM |
10241 | /* We handle `main' specially here, because 'main () { }' is so |
10242 | common. With no options, it is allowed. With -Wreturn-type, | |
10243 | it is a warning. It is only an error with -pedantic-errors. */ | |
3dbc07b6 | 10244 | is_main = (funcdef_flag |
7437519c | 10245 | && dname && MAIN_NAME_P (dname) |
3dbc07b6 MM |
10246 | && ctype == NULL_TREE |
10247 | && in_namespace == NULL_TREE | |
10248 | && current_namespace == global_namespace); | |
10249 | ||
10250 | if (in_system_header || flag_ms_extensions) | |
10251 | /* Allow it, sigh. */; | |
10252 | else if (pedantic || ! is_main) | |
33bd39a2 | 10253 | pedwarn ("ISO C++ forbids declaration of `%s' with no type", |
3dbc07b6 MM |
10254 | name); |
10255 | else if (warn_return_type) | |
33bd39a2 | 10256 | warning ("ISO C++ forbids declaration of `%s' with no type", |
3dbc07b6 | 10257 | name); |
41eff652 | 10258 | |
3dbc07b6 | 10259 | type = integer_type_node; |
51c184be | 10260 | } |
ad1a6d45 | 10261 | |
8d6e459d | 10262 | if (type && IMPLICIT_TYPENAME_P (type)) |
ad1a6d45 NS |
10263 | { |
10264 | /* The implicit typename extension is deprecated and will be | |
10265 | removed. Warn about its use now. */ | |
33bd39a2 | 10266 | warning ("`%T' is implicitly a typename", type); |
ad1a6d45 NS |
10267 | cp_deprecated ("implicit typename"); |
10268 | ||
10269 | /* Now remove its implicitness, so that we don't warn again. | |
10270 | For instance this might be a typedef, and we do not want to | |
10271 | warn on uses of the typedef itself. Simply clearing the | |
10272 | TREE_TYPE is insufficient. */ | |
10273 | type = copy_node (type); | |
10274 | TREE_TYPE (type) = NULL_TREE; | |
10275 | } | |
8d08fdba MS |
10276 | |
10277 | ctype = NULL_TREE; | |
10278 | ||
10279 | /* Now process the modifiers that were specified | |
10280 | and check for invalid combinations. */ | |
10281 | ||
10282 | /* Long double is a special combination. */ | |
10283 | ||
10284 | if (RIDBIT_SETP (RID_LONG, specbits) | |
10285 | && TYPE_MAIN_VARIANT (type) == double_type_node) | |
10286 | { | |
10287 | RIDBIT_RESET (RID_LONG, specbits); | |
68642fb6 | 10288 | type = build_qualified_type (long_double_type_node, |
89d684bb | 10289 | cp_type_quals (type)); |
8d08fdba MS |
10290 | } |
10291 | ||
10292 | /* Check all other uses of type modifiers. */ | |
10293 | ||
10294 | if (RIDBIT_SETP (RID_UNSIGNED, specbits) | |
10295 | || RIDBIT_SETP (RID_SIGNED, specbits) | |
10296 | || RIDBIT_SETP (RID_LONG, specbits) | |
10297 | || RIDBIT_SETP (RID_SHORT, specbits)) | |
10298 | { | |
10299 | int ok = 0; | |
10300 | ||
10301 | if (TREE_CODE (type) == REAL_TYPE) | |
8251199e | 10302 | error ("short, signed or unsigned invalid for `%s'", name); |
b7484fbe | 10303 | else if (TREE_CODE (type) != INTEGER_TYPE) |
8251199e | 10304 | error ("long, short, signed or unsigned invalid for `%s'", name); |
8d08fdba MS |
10305 | else if (RIDBIT_SETP (RID_LONG, specbits) |
10306 | && RIDBIT_SETP (RID_SHORT, specbits)) | |
8251199e | 10307 | error ("long and short specified together for `%s'", name); |
8d08fdba MS |
10308 | else if ((RIDBIT_SETP (RID_LONG, specbits) |
10309 | || RIDBIT_SETP (RID_SHORT, specbits)) | |
10310 | && explicit_char) | |
8251199e | 10311 | error ("long or short specified with char for `%s'", name); |
8d08fdba MS |
10312 | else if ((RIDBIT_SETP (RID_LONG, specbits) |
10313 | || RIDBIT_SETP (RID_SHORT, specbits)) | |
10314 | && TREE_CODE (type) == REAL_TYPE) | |
8251199e | 10315 | error ("long or short specified with floating type for `%s'", name); |
8d08fdba MS |
10316 | else if (RIDBIT_SETP (RID_SIGNED, specbits) |
10317 | && RIDBIT_SETP (RID_UNSIGNED, specbits)) | |
8251199e | 10318 | error ("signed and unsigned given together for `%s'", name); |
8d08fdba MS |
10319 | else |
10320 | { | |
10321 | ok = 1; | |
37c46b43 | 10322 | if (!explicit_int && !defaulted_int && !explicit_char && pedantic) |
8d08fdba | 10323 | { |
8251199e | 10324 | pedwarn ("long, short, signed or unsigned used invalidly for `%s'", |
8d08fdba MS |
10325 | name); |
10326 | if (flag_pedantic_errors) | |
10327 | ok = 0; | |
10328 | } | |
10329 | } | |
10330 | ||
10331 | /* Discard the type modifiers if they are invalid. */ | |
10332 | if (! ok) | |
10333 | { | |
10334 | RIDBIT_RESET (RID_UNSIGNED, specbits); | |
10335 | RIDBIT_RESET (RID_SIGNED, specbits); | |
10336 | RIDBIT_RESET (RID_LONG, specbits); | |
10337 | RIDBIT_RESET (RID_SHORT, specbits); | |
10338 | longlong = 0; | |
10339 | } | |
10340 | } | |
10341 | ||
37c46b43 MS |
10342 | if (RIDBIT_SETP (RID_COMPLEX, specbits) |
10343 | && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE) | |
10344 | { | |
8251199e | 10345 | error ("complex invalid for `%s'", name); |
37c46b43 MS |
10346 | RIDBIT_RESET (RID_COMPLEX, specbits); |
10347 | } | |
10348 | ||
8d08fdba MS |
10349 | /* Decide whether an integer type is signed or not. |
10350 | Optionally treat bitfields as signed by default. */ | |
10351 | if (RIDBIT_SETP (RID_UNSIGNED, specbits) | |
b89c5a7b MM |
10352 | /* [class.bit] |
10353 | ||
10354 | It is implementation-defined whether a plain (neither | |
10355 | explicitly signed or unsigned) char, short, int, or long | |
10356 | bit-field is signed or unsigned. | |
68642fb6 | 10357 | |
b89c5a7b MM |
10358 | Naturally, we extend this to long long as well. Note that |
10359 | this does not include wchar_t. */ | |
10360 | || (bitfield && !flag_signed_bitfields | |
10361 | && RIDBIT_NOTSETP (RID_SIGNED, specbits) | |
10362 | /* A typedef for plain `int' without `signed' can be | |
10363 | controlled just like plain `int', but a typedef for | |
10364 | `signed int' cannot be so controlled. */ | |
68642fb6 | 10365 | && !(typedef_decl |
29bbeb1c | 10366 | && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)) |
b89c5a7b MM |
10367 | && (TREE_CODE (type) == INTEGER_TYPE |
10368 | || TREE_CODE (type) == CHAR_TYPE) | |
29bbeb1c | 10369 | && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node))) |
8d08fdba MS |
10370 | { |
10371 | if (longlong) | |
10372 | type = long_long_unsigned_type_node; | |
10373 | else if (RIDBIT_SETP (RID_LONG, specbits)) | |
10374 | type = long_unsigned_type_node; | |
10375 | else if (RIDBIT_SETP (RID_SHORT, specbits)) | |
10376 | type = short_unsigned_type_node; | |
10377 | else if (type == char_type_node) | |
10378 | type = unsigned_char_type_node; | |
10379 | else if (typedef_decl) | |
ceef8ce4 | 10380 | type = c_common_unsigned_type (type); |
8d08fdba MS |
10381 | else |
10382 | type = unsigned_type_node; | |
10383 | } | |
10384 | else if (RIDBIT_SETP (RID_SIGNED, specbits) | |
10385 | && type == char_type_node) | |
10386 | type = signed_char_type_node; | |
10387 | else if (longlong) | |
10388 | type = long_long_integer_type_node; | |
10389 | else if (RIDBIT_SETP (RID_LONG, specbits)) | |
10390 | type = long_integer_type_node; | |
10391 | else if (RIDBIT_SETP (RID_SHORT, specbits)) | |
10392 | type = short_integer_type_node; | |
10393 | ||
37c46b43 MS |
10394 | if (RIDBIT_SETP (RID_COMPLEX, specbits)) |
10395 | { | |
10396 | /* If we just have "complex", it is equivalent to | |
10397 | "complex double", but if any modifiers at all are specified it is | |
10398 | the complex form of TYPE. E.g, "complex short" is | |
10399 | "complex short int". */ | |
10400 | ||
10401 | if (defaulted_int && ! longlong | |
10402 | && ! (RIDBIT_SETP (RID_LONG, specbits) | |
10403 | || RIDBIT_SETP (RID_SHORT, specbits) | |
10404 | || RIDBIT_SETP (RID_SIGNED, specbits) | |
10405 | || RIDBIT_SETP (RID_UNSIGNED, specbits))) | |
10406 | type = complex_double_type_node; | |
10407 | else if (type == integer_type_node) | |
10408 | type = complex_integer_type_node; | |
10409 | else if (type == float_type_node) | |
10410 | type = complex_float_type_node; | |
10411 | else if (type == double_type_node) | |
10412 | type = complex_double_type_node; | |
10413 | else if (type == long_double_type_node) | |
10414 | type = complex_long_double_type_node; | |
10415 | else | |
10416 | type = build_complex_type (type); | |
10417 | } | |
10418 | ||
4f2b0fb2 NS |
10419 | type_quals = TYPE_UNQUALIFIED; |
10420 | if (RIDBIT_SETP (RID_CONST, specbits)) | |
10421 | type_quals |= TYPE_QUAL_CONST; | |
10422 | if (RIDBIT_SETP (RID_VOLATILE, specbits)) | |
10423 | type_quals |= TYPE_QUAL_VOLATILE; | |
10424 | if (RIDBIT_SETP (RID_RESTRICT, specbits)) | |
10425 | type_quals |= TYPE_QUAL_RESTRICT; | |
10426 | if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED) | |
33bd39a2 | 10427 | error ("qualifiers are not allowed on declaration of `operator %T'", |
3ac3d9ea MM |
10428 | ctor_return_type); |
10429 | ||
4f2b0fb2 NS |
10430 | type_quals |= cp_type_quals (type); |
10431 | type = cp_build_qualified_type_real | |
10432 | (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl) | |
10433 | ? tf_ignore_bad_quals : 0) | tf_error | tf_warning)); | |
10434 | /* We might have ignored or rejected some of the qualifiers. */ | |
10435 | type_quals = cp_type_quals (type); | |
10436 | ||
8d08fdba MS |
10437 | staticp = 0; |
10438 | inlinep = !! RIDBIT_SETP (RID_INLINE, specbits); | |
8d08fdba | 10439 | virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits); |
db5ae43f MS |
10440 | RIDBIT_RESET (RID_VIRTUAL, specbits); |
10441 | explicitp = RIDBIT_SETP (RID_EXPLICIT, specbits) != 0; | |
10442 | RIDBIT_RESET (RID_EXPLICIT, specbits); | |
8d08fdba | 10443 | |
8d08fdba MS |
10444 | if (RIDBIT_SETP (RID_STATIC, specbits)) |
10445 | staticp = 1 + (decl_context == FIELD); | |
10446 | ||
10447 | if (virtualp && staticp == 2) | |
10448 | { | |
33bd39a2 | 10449 | error ("member `%D' cannot be declared both virtual and static", |
8d08fdba MS |
10450 | dname); |
10451 | staticp = 0; | |
10452 | } | |
10453 | friendp = RIDBIT_SETP (RID_FRIEND, specbits); | |
8d08fdba MS |
10454 | RIDBIT_RESET (RID_FRIEND, specbits); |
10455 | ||
d5614afb NS |
10456 | if (dependant_name && !friendp) |
10457 | { | |
10458 | error ("`%T::%D' is not a valid declarator", ctype, dependant_name); | |
10459 | return void_type_node; | |
10460 | } | |
10461 | ||
8d08fdba MS |
10462 | /* Warn if two storage classes are given. Default to `auto'. */ |
10463 | ||
10464 | if (RIDBIT_ANY_SET (specbits)) | |
10465 | { | |
10466 | if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++; | |
270d8c65 | 10467 | if (RIDBIT_SETP (RID_EXTERN, specbits) && !extern_langp) nclasses++; |
8d08fdba | 10468 | if (decl_context == PARM && nclasses > 0) |
8251199e | 10469 | error ("storage class specifiers invalid in parameter declarations"); |
8d08fdba MS |
10470 | if (RIDBIT_SETP (RID_TYPEDEF, specbits)) |
10471 | { | |
10472 | if (decl_context == PARM) | |
8251199e | 10473 | error ("typedef declaration invalid in parameter declaration"); |
8d08fdba MS |
10474 | nclasses++; |
10475 | } | |
10476 | if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++; | |
10477 | if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++; | |
270d8c65 NS |
10478 | if (!nclasses && !friendp && extern_langp) |
10479 | nclasses++; | |
8d08fdba MS |
10480 | } |
10481 | ||
10482 | /* Give error if `virtual' is used outside of class declaration. */ | |
b7484fbe MS |
10483 | if (virtualp |
10484 | && (current_class_name == NULL_TREE || decl_context != FIELD)) | |
8d08fdba | 10485 | { |
8251199e | 10486 | error ("virtual outside class declaration"); |
8d08fdba MS |
10487 | virtualp = 0; |
10488 | } | |
8d08fdba MS |
10489 | |
10490 | /* Static anonymous unions are dealt with here. */ | |
10491 | if (staticp && decl_context == TYPENAME | |
10492 | && TREE_CODE (declspecs) == TREE_LIST | |
6bdb8141 | 10493 | && ANON_AGGR_TYPE_P (TREE_VALUE (declspecs))) |
8d08fdba MS |
10494 | decl_context = FIELD; |
10495 | ||
8d08fdba MS |
10496 | /* Warn about storage classes that are invalid for certain |
10497 | kinds of declarations (parameters, typenames, etc.). */ | |
10498 | ||
10499 | if (nclasses > 1) | |
8251199e | 10500 | error ("multiple storage classes in declaration of `%s'", name); |
8d08fdba MS |
10501 | else if (decl_context != NORMAL && nclasses > 0) |
10502 | { | |
db5ae43f | 10503 | if ((decl_context == PARM || decl_context == CATCHPARM) |
8d08fdba MS |
10504 | && (RIDBIT_SETP (RID_REGISTER, specbits) |
10505 | || RIDBIT_SETP (RID_AUTO, specbits))) | |
10506 | ; | |
fc378698 MS |
10507 | else if (RIDBIT_SETP (RID_TYPEDEF, specbits)) |
10508 | ; | |
8d08fdba | 10509 | else if (decl_context == FIELD |
d363e7bf AJ |
10510 | /* C++ allows static class elements */ |
10511 | && RIDBIT_SETP (RID_STATIC, specbits)) | |
10512 | /* C++ also allows inlines and signed and unsigned elements, | |
10513 | but in those cases we don't come in here. */ | |
8d08fdba MS |
10514 | ; |
10515 | else | |
10516 | { | |
10517 | if (decl_context == FIELD) | |
10518 | { | |
b7484fbe MS |
10519 | tree tmp = NULL_TREE; |
10520 | register int op = 0; | |
10521 | ||
10522 | if (declarator) | |
10523 | { | |
68642fb6 | 10524 | /* Avoid trying to get an operand off an identifier node. */ |
9e9ff709 MS |
10525 | if (TREE_CODE (declarator) == IDENTIFIER_NODE) |
10526 | tmp = declarator; | |
10527 | else | |
10528 | tmp = TREE_OPERAND (declarator, 0); | |
b7484fbe | 10529 | op = IDENTIFIER_OPNAME_P (tmp); |
596ea4e5 AS |
10530 | if (IDENTIFIER_TYPENAME_P (tmp)) |
10531 | { | |
10532 | if (IDENTIFIER_GLOBAL_VALUE (tmp) | |
5362b086 | 10533 | && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (tmp)) |
596ea4e5 AS |
10534 | == TYPE_DECL)) |
10535 | name = IDENTIFIER_POINTER (tmp); | |
10536 | else | |
10537 | name = "<invalid operator>"; | |
10538 | } | |
b7484fbe | 10539 | } |
8251199e | 10540 | error ("storage class specified for %s `%s'", |
6eabb241 | 10541 | op ? "member operator" : "field", |
596ea4e5 | 10542 | name); |
8d08fdba MS |
10543 | } |
10544 | else | |
b9d12519 KG |
10545 | { |
10546 | if (decl_context == PARM || decl_context == CATCHPARM) | |
10547 | error ("storage class specified for parameter `%s'", name); | |
10548 | else | |
10549 | error ("storage class specified for typename"); | |
10550 | } | |
8d08fdba MS |
10551 | RIDBIT_RESET (RID_REGISTER, specbits); |
10552 | RIDBIT_RESET (RID_AUTO, specbits); | |
10553 | RIDBIT_RESET (RID_EXTERN, specbits); | |
8d08fdba MS |
10554 | } |
10555 | } | |
10556 | else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag) | |
10557 | { | |
a9aedbc2 | 10558 | if (toplevel_bindings_p ()) |
8d08fdba | 10559 | { |
59be0cdd | 10560 | /* It's common practice (and completely valid) to have a const |
8d08fdba | 10561 | be initialized and declared extern. */ |
950ad3c3 | 10562 | if (!(type_quals & TYPE_QUAL_CONST)) |
8251199e | 10563 | warning ("`%s' initialized and declared `extern'", name); |
8d08fdba MS |
10564 | } |
10565 | else | |
8251199e | 10566 | error ("`%s' has both `extern' and initializer", name); |
8d08fdba MS |
10567 | } |
10568 | else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag | |
a9aedbc2 | 10569 | && ! toplevel_bindings_p ()) |
8251199e | 10570 | error ("nested function `%s' declared `extern'", name); |
a9aedbc2 | 10571 | else if (toplevel_bindings_p ()) |
8d08fdba MS |
10572 | { |
10573 | if (RIDBIT_SETP (RID_AUTO, specbits)) | |
8251199e | 10574 | error ("top-level declaration of `%s' specifies `auto'", name); |
8d08fdba MS |
10575 | } |
10576 | ||
909e536a | 10577 | if (nclasses > 0 && friendp) |
8251199e | 10578 | error ("storage class specifiers invalid in friend function declarations"); |
909e536a | 10579 | |
8d08fdba MS |
10580 | /* Now figure out the structure of the declarator proper. |
10581 | Descend through it, creating more complex types, until we reach | |
10582 | the declared identifier (or NULL_TREE, in an absolute declarator). */ | |
10583 | ||
386b8a85 JM |
10584 | while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE |
10585 | && TREE_CODE (declarator) != TEMPLATE_ID_EXPR) | |
8d08fdba MS |
10586 | { |
10587 | /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]), | |
10588 | an INDIRECT_REF (for *...), | |
10589 | a CALL_EXPR (for ...(...)), | |
10590 | an identifier (for the name being declared) | |
10591 | or a null pointer (for the place in an absolute declarator | |
10592 | where the name was omitted). | |
10593 | For the last two cases, we have just exited the loop. | |
10594 | ||
10595 | For C++ it could also be | |
10596 | a SCOPE_REF (for class :: ...). In this case, we have converted | |
10597 | sensible names to types, and those are the values we use to | |
10598 | qualify the member name. | |
10599 | an ADDR_EXPR (for &...), | |
10600 | a BIT_NOT_EXPR (for destructors) | |
8d08fdba MS |
10601 | |
10602 | At this point, TYPE is the type of elements of an array, | |
10603 | or for a function to return, or for a pointer to point to. | |
10604 | After this sequence of ifs, TYPE is the type of the | |
10605 | array or function or pointer, and DECLARATOR has had its | |
10606 | outermost layer removed. */ | |
10607 | ||
bd6dd845 | 10608 | if (type == error_mark_node) |
8d08fdba MS |
10609 | { |
10610 | if (TREE_CODE (declarator) == SCOPE_REF) | |
10611 | declarator = TREE_OPERAND (declarator, 1); | |
10612 | else | |
10613 | declarator = TREE_OPERAND (declarator, 0); | |
10614 | continue; | |
10615 | } | |
10616 | if (quals != NULL_TREE | |
10617 | && (declarator == NULL_TREE | |
10618 | || TREE_CODE (declarator) != SCOPE_REF)) | |
10619 | { | |
10620 | if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE) | |
10621 | ctype = TYPE_METHOD_BASETYPE (type); | |
10622 | if (ctype != NULL_TREE) | |
10623 | { | |
8d08fdba | 10624 | tree dummy = build_decl (TYPE_DECL, NULL_TREE, type); |
535233a8 | 10625 | grok_method_quals (ctype, dummy, quals); |
8d08fdba | 10626 | type = TREE_TYPE (dummy); |
535233a8 | 10627 | ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))); |
8d08fdba MS |
10628 | quals = NULL_TREE; |
10629 | } | |
10630 | } | |
1eab9b56 | 10631 | |
8d08fdba MS |
10632 | switch (TREE_CODE (declarator)) |
10633 | { | |
52fbc847 JM |
10634 | case TREE_LIST: |
10635 | { | |
10636 | /* We encode a declarator with embedded attributes using | |
91d231cb JM |
10637 | a TREE_LIST. */ |
10638 | tree attrs = TREE_PURPOSE (declarator); | |
10639 | tree inner_decl; | |
c9574c9f R |
10640 | int attr_flags; |
10641 | ||
52fbc847 | 10642 | declarator = TREE_VALUE (declarator); |
91d231cb JM |
10643 | inner_decl = declarator; |
10644 | while (inner_decl != NULL_TREE | |
10645 | && TREE_CODE (inner_decl) == TREE_LIST) | |
10646 | inner_decl = TREE_VALUE (inner_decl); | |
c9574c9f | 10647 | attr_flags = 0; |
91d231cb JM |
10648 | if (inner_decl == NULL_TREE |
10649 | || TREE_CODE (inner_decl) == IDENTIFIER_NODE) | |
10650 | attr_flags |= (int) ATTR_FLAG_DECL_NEXT; | |
10651 | if (TREE_CODE (inner_decl) == CALL_EXPR) | |
10652 | attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT; | |
10653 | if (TREE_CODE (inner_decl) == ARRAY_REF) | |
10654 | attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT; | |
10655 | returned_attrs = decl_attributes (&type, | |
10656 | chainon (returned_attrs, attrs), | |
10657 | attr_flags); | |
52fbc847 JM |
10658 | } |
10659 | break; | |
10660 | ||
8d08fdba MS |
10661 | case ARRAY_REF: |
10662 | { | |
2848ad0f | 10663 | register tree size; |
8d08fdba | 10664 | |
2848ad0f | 10665 | size = TREE_OPERAND (declarator, 1); |
8d08fdba | 10666 | |
f2e2cbd4 | 10667 | /* VC++ spells a zero-sized array with []. */ |
e8ea7036 MM |
10668 | if (size == NULL_TREE && decl_context == FIELD && ! staticp |
10669 | && ! RIDBIT_SETP (RID_TYPEDEF, specbits)) | |
f2e2cbd4 JM |
10670 | size = integer_zero_node; |
10671 | ||
2848ad0f | 10672 | declarator = TREE_OPERAND (declarator, 0); |
8d08fdba | 10673 | |
2848ad0f | 10674 | type = create_array_type_for_decl (dname, type, size); |
c1b177ec ML |
10675 | |
10676 | /* VLAs never work as fields. */ | |
5362b086 | 10677 | if (decl_context == FIELD && !processing_template_decl |
c1b177ec ML |
10678 | && TREE_CODE (type) == ARRAY_TYPE |
10679 | && TYPE_DOMAIN (type) != NULL_TREE | |
10680 | && !TREE_CONSTANT (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))) | |
10681 | { | |
33bd39a2 | 10682 | error ("size of member `%D' is not constant", dname); |
c1b177ec ML |
10683 | /* Proceed with arbitrary constant size, so that offset |
10684 | computations don't get confused. */ | |
10685 | type = create_array_type_for_decl (dname, TREE_TYPE (type), | |
10686 | integer_one_node); | |
10687 | } | |
10688 | ||
8d08fdba MS |
10689 | ctype = NULL_TREE; |
10690 | } | |
10691 | break; | |
10692 | ||
10693 | case CALL_EXPR: | |
10694 | { | |
10695 | tree arg_types; | |
f376e137 | 10696 | int funcdecl_p; |
43f887f9 | 10697 | tree inner_parms = CALL_DECLARATOR_PARMS (declarator); |
f376e137 | 10698 | tree inner_decl = TREE_OPERAND (declarator, 0); |
8d08fdba MS |
10699 | |
10700 | /* Declaring a function type. | |
10701 | Make sure we have a valid type for the function to return. */ | |
8d08fdba | 10702 | |
91063b51 | 10703 | /* We now know that the TYPE_QUALS don't apply to the |
14ae7e7d | 10704 | decl, but to its return type. */ |
91063b51 | 10705 | type_quals = TYPE_UNQUALIFIED; |
8d08fdba MS |
10706 | |
10707 | /* Warn about some types functions can't return. */ | |
10708 | ||
10709 | if (TREE_CODE (type) == FUNCTION_TYPE) | |
10710 | { | |
8251199e | 10711 | error ("`%s' declared as function returning a function", name); |
8d08fdba MS |
10712 | type = integer_type_node; |
10713 | } | |
10714 | if (TREE_CODE (type) == ARRAY_TYPE) | |
10715 | { | |
8251199e | 10716 | error ("`%s' declared as function returning an array", name); |
8d08fdba MS |
10717 | type = integer_type_node; |
10718 | } | |
10719 | ||
f376e137 MS |
10720 | if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF) |
10721 | inner_decl = TREE_OPERAND (inner_decl, 1); | |
10722 | ||
68642fb6 | 10723 | if (inner_decl && TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR) |
386b8a85 JM |
10724 | inner_decl = dname; |
10725 | ||
b7484fbe | 10726 | /* Pick up type qualifiers which should be applied to `this'. */ |
43f887f9 | 10727 | quals = CALL_DECLARATOR_QUALS (declarator); |
b7484fbe | 10728 | |
c11b6f21 | 10729 | /* Pick up the exception specifications. */ |
43f887f9 | 10730 | raises = CALL_DECLARATOR_EXCEPTION_SPEC (declarator); |
c11b6f21 | 10731 | |
f376e137 MS |
10732 | /* Say it's a definition only for the CALL_EXPR |
10733 | closest to the identifier. */ | |
beb53fb8 | 10734 | funcdecl_p |
68642fb6 | 10735 | = inner_decl |
386b8a85 | 10736 | && (TREE_CODE (inner_decl) == IDENTIFIER_NODE |
68642fb6 | 10737 | || TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR |
386b8a85 | 10738 | || TREE_CODE (inner_decl) == BIT_NOT_EXPR); |
68642fb6 | 10739 | |
8d08fdba MS |
10740 | if (ctype == NULL_TREE |
10741 | && decl_context == FIELD | |
f376e137 | 10742 | && funcdecl_p |
8d08fdba MS |
10743 | && (friendp == 0 || dname == current_class_name)) |
10744 | ctype = current_class_type; | |
10745 | ||
3dbc07b6 | 10746 | if (ctype && sfk == sfk_conversion) |
8d08fdba MS |
10747 | TYPE_HAS_CONVERSION (ctype) = 1; |
10748 | if (ctype && constructor_name (ctype) == dname) | |
10749 | { | |
10750 | /* We are within a class's scope. If our declarator name | |
10751 | is the same as the class name, and we are defining | |
10752 | a function, then it is a constructor/destructor, and | |
10753 | therefore returns a void type. */ | |
10754 | ||
10755 | if (flags == DTOR_FLAG) | |
10756 | { | |
cab1f180 ML |
10757 | /* ISO C++ 12.4/2. A destructor may not be |
10758 | declared const or volatile. A destructor may | |
10759 | not be static. */ | |
8d08fdba | 10760 | if (staticp == 2) |
8251199e | 10761 | error ("destructor cannot be static member function"); |
b7484fbe | 10762 | if (quals) |
8d08fdba | 10763 | { |
33bd39a2 | 10764 | error ("destructors may not be `%s'", |
4f70a846 MM |
10765 | IDENTIFIER_POINTER (TREE_VALUE (quals))); |
10766 | quals = NULL_TREE; | |
8d08fdba MS |
10767 | } |
10768 | if (decl_context == FIELD) | |
10769 | { | |
4dacf5bd KG |
10770 | if (! member_function_or_else (ctype, |
10771 | current_class_type, | |
10772 | flags)) | |
8d08fdba MS |
10773 | return void_type_node; |
10774 | } | |
10775 | } | |
2c73f9f5 | 10776 | else /* It's a constructor. */ |
8d08fdba | 10777 | { |
db5ae43f MS |
10778 | if (explicitp == 1) |
10779 | explicitp = 2; | |
cab1f180 ML |
10780 | /* ISO C++ 12.1. A constructor may not be |
10781 | declared const or volatile. A constructor may | |
10782 | not be virtual. A constructor may not be | |
10783 | static. */ | |
8d08fdba | 10784 | if (staticp == 2) |
8251199e | 10785 | error ("constructor cannot be static member function"); |
8d08fdba MS |
10786 | if (virtualp) |
10787 | { | |
8251199e | 10788 | pedwarn ("constructors cannot be declared virtual"); |
8d08fdba MS |
10789 | virtualp = 0; |
10790 | } | |
b7484fbe | 10791 | if (quals) |
8d08fdba | 10792 | { |
33bd39a2 | 10793 | error ("constructors may not be `%s'", |
4f70a846 MM |
10794 | IDENTIFIER_POINTER (TREE_VALUE (quals))); |
10795 | quals = NULL_TREE; | |
d363e7bf | 10796 | } |
8d08fdba | 10797 | { |
51c184be | 10798 | RID_BIT_TYPE tmp_bits; |
4e135bdd | 10799 | memcpy (&tmp_bits, &specbits, sizeof (RID_BIT_TYPE)); |
51c184be MS |
10800 | RIDBIT_RESET (RID_INLINE, tmp_bits); |
10801 | RIDBIT_RESET (RID_STATIC, tmp_bits); | |
10802 | if (RIDBIT_ANY_SET (tmp_bits)) | |
8251199e | 10803 | error ("return value type specifier for constructor ignored"); |
8d08fdba | 10804 | } |
6eabb241 | 10805 | if (decl_context == FIELD) |
8d08fdba | 10806 | { |
4dacf5bd KG |
10807 | if (! member_function_or_else (ctype, |
10808 | current_class_type, | |
10809 | flags)) | |
8d08fdba MS |
10810 | return void_type_node; |
10811 | TYPE_HAS_CONSTRUCTOR (ctype) = 1; | |
3dbc07b6 | 10812 | if (sfk != sfk_constructor) |
8d08fdba MS |
10813 | return NULL_TREE; |
10814 | } | |
10815 | } | |
10816 | if (decl_context == FIELD) | |
10817 | staticp = 0; | |
10818 | } | |
b7484fbe | 10819 | else if (friendp) |
8d08fdba | 10820 | { |
b7484fbe | 10821 | if (initialized) |
8251199e | 10822 | error ("can't initialize friend function `%s'", name); |
b7484fbe MS |
10823 | if (virtualp) |
10824 | { | |
10825 | /* Cannot be both friend and virtual. */ | |
8251199e | 10826 | error ("virtual functions cannot be friends"); |
b7484fbe MS |
10827 | RIDBIT_RESET (RID_FRIEND, specbits); |
10828 | friendp = 0; | |
10829 | } | |
28cbf42c | 10830 | if (decl_context == NORMAL) |
8251199e | 10831 | error ("friend declaration not in class definition"); |
28cbf42c | 10832 | if (current_function_decl && funcdef_flag) |
33bd39a2 | 10833 | error ("can't define friend function `%s' in a local class definition", |
28cbf42c | 10834 | name); |
8d08fdba MS |
10835 | } |
10836 | ||
8d08fdba MS |
10837 | /* Construct the function type and go to the next |
10838 | inner layer of declarator. */ | |
10839 | ||
f376e137 | 10840 | declarator = TREE_OPERAND (declarator, 0); |
8d08fdba | 10841 | |
f376e137 MS |
10842 | /* FIXME: This is where default args should be fully |
10843 | processed. */ | |
8d08fdba | 10844 | |
5cce22b6 | 10845 | arg_types = grokparms (inner_parms); |
8d08fdba | 10846 | |
4f70a846 | 10847 | if (declarator && flags == DTOR_FLAG) |
8d08fdba | 10848 | { |
4f70a846 MM |
10849 | /* A destructor declared in the body of a class will |
10850 | be represented as a BIT_NOT_EXPR. But, we just | |
10851 | want the underlying IDENTIFIER. */ | |
8d08fdba | 10852 | if (TREE_CODE (declarator) == BIT_NOT_EXPR) |
4f70a846 | 10853 | declarator = TREE_OPERAND (declarator, 0); |
68642fb6 | 10854 | |
8b27e9ef | 10855 | if (arg_types != void_list_node) |
8d08fdba | 10856 | { |
33bd39a2 | 10857 | error ("destructors may not have parameters"); |
4f70a846 MM |
10858 | arg_types = void_list_node; |
10859 | last_function_parms = NULL_TREE; | |
8d08fdba MS |
10860 | } |
10861 | } | |
10862 | ||
d22c8596 | 10863 | /* ANSI says that `const int foo ();' |
8d08fdba | 10864 | does not make the function foo const. */ |
d22c8596 | 10865 | type = build_function_type (type, arg_types); |
42976354 BK |
10866 | |
10867 | { | |
10868 | tree t; | |
10869 | for (t = arg_types; t; t = TREE_CHAIN (t)) | |
10870 | if (TREE_PURPOSE (t) | |
10871 | && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG) | |
10872 | { | |
10873 | add_defarg_fn (type); | |
10874 | break; | |
10875 | } | |
10876 | } | |
8d08fdba MS |
10877 | } |
10878 | break; | |
10879 | ||
10880 | case ADDR_EXPR: | |
10881 | case INDIRECT_REF: | |
10882 | /* Filter out pointers-to-references and references-to-references. | |
10883 | We can get these if a TYPE_DECL is used. */ | |
10884 | ||
10885 | if (TREE_CODE (type) == REFERENCE_TYPE) | |
10886 | { | |
8251199e | 10887 | error ("cannot declare %s to references", |
8d08fdba MS |
10888 | TREE_CODE (declarator) == ADDR_EXPR |
10889 | ? "references" : "pointers"); | |
10890 | declarator = TREE_OPERAND (declarator, 0); | |
10891 | continue; | |
10892 | } | |
10893 | ||
a5894242 MS |
10894 | if (TREE_CODE (type) == OFFSET_TYPE |
10895 | && (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE | |
10896 | || TREE_CODE (TREE_TYPE (type)) == REFERENCE_TYPE)) | |
10897 | { | |
33bd39a2 | 10898 | error ("cannot declare pointer to `%#T' member", |
a5894242 MS |
10899 | TREE_TYPE (type)); |
10900 | type = TREE_TYPE (type); | |
10901 | } | |
10902 | ||
61a127b3 MM |
10903 | /* Merge any constancy or volatility into the target type |
10904 | for the pointer. */ | |
10905 | ||
91063b51 MM |
10906 | /* We now know that the TYPE_QUALS don't apply to the decl, |
10907 | but to the target of the pointer. */ | |
10908 | type_quals = TYPE_UNQUALIFIED; | |
8d08fdba | 10909 | |
6eabb241 | 10910 | if (TREE_CODE (declarator) == ADDR_EXPR) |
8d08fdba | 10911 | { |
69851283 MM |
10912 | if (TREE_CODE (type) == VOID_TYPE) |
10913 | error ("invalid type: `void &'"); | |
8d08fdba | 10914 | else |
69851283 | 10915 | type = build_reference_type (type); |
8d08fdba MS |
10916 | } |
10917 | else if (TREE_CODE (type) == METHOD_TYPE) | |
d8f8dca1 | 10918 | type = build_ptrmemfunc_type (build_pointer_type (type)); |
8d08fdba MS |
10919 | else |
10920 | type = build_pointer_type (type); | |
10921 | ||
10922 | /* Process a list of type modifier keywords (such as | |
10923 | const or volatile) that were given inside the `*' or `&'. */ | |
10924 | ||
10925 | if (TREE_TYPE (declarator)) | |
10926 | { | |
10927 | register tree typemodlist; | |
10928 | int erred = 0; | |
4f2b0fb2 NS |
10929 | int constp = 0; |
10930 | int volatilep = 0; | |
10931 | int restrictp = 0; | |
10932 | ||
8d08fdba MS |
10933 | for (typemodlist = TREE_TYPE (declarator); typemodlist; |
10934 | typemodlist = TREE_CHAIN (typemodlist)) | |
10935 | { | |
91063b51 MM |
10936 | tree qualifier = TREE_VALUE (typemodlist); |
10937 | ||
10938 | if (qualifier == ridpointers[(int) RID_CONST]) | |
4f2b0fb2 NS |
10939 | { |
10940 | constp++; | |
10941 | type_quals |= TYPE_QUAL_CONST; | |
10942 | } | |
91063b51 | 10943 | else if (qualifier == ridpointers[(int) RID_VOLATILE]) |
4f2b0fb2 NS |
10944 | { |
10945 | volatilep++; | |
10946 | type_quals |= TYPE_QUAL_VOLATILE; | |
10947 | } | |
91063b51 | 10948 | else if (qualifier == ridpointers[(int) RID_RESTRICT]) |
4f2b0fb2 NS |
10949 | { |
10950 | restrictp++; | |
10951 | type_quals |= TYPE_QUAL_RESTRICT; | |
10952 | } | |
8d08fdba MS |
10953 | else if (!erred) |
10954 | { | |
10955 | erred = 1; | |
91063b51 | 10956 | error ("invalid type modifier within pointer declarator"); |
8d08fdba MS |
10957 | } |
10958 | } | |
10959 | if (constp > 1) | |
8251199e | 10960 | pedwarn ("duplicate `const'"); |
8d08fdba | 10961 | if (volatilep > 1) |
8251199e | 10962 | pedwarn ("duplicate `volatile'"); |
91063b51 MM |
10963 | if (restrictp > 1) |
10964 | pedwarn ("duplicate `restrict'"); | |
91063b51 | 10965 | type = cp_build_qualified_type (type, type_quals); |
4f2b0fb2 | 10966 | type_quals = cp_type_quals (type); |
8d08fdba MS |
10967 | } |
10968 | declarator = TREE_OPERAND (declarator, 0); | |
10969 | ctype = NULL_TREE; | |
10970 | break; | |
10971 | ||
10972 | case SCOPE_REF: | |
10973 | { | |
10974 | /* We have converted type names to NULL_TREE if the | |
10975 | name was bogus, or to a _TYPE node, if not. | |
10976 | ||
10977 | The variable CTYPE holds the type we will ultimately | |
10978 | resolve to. The code here just needs to build | |
10979 | up appropriate member types. */ | |
10980 | tree sname = TREE_OPERAND (declarator, 1); | |
386b8a85 JM |
10981 | tree t; |
10982 | ||
8d08fdba MS |
10983 | /* Destructors can have their visibilities changed as well. */ |
10984 | if (TREE_CODE (sname) == BIT_NOT_EXPR) | |
10985 | sname = TREE_OPERAND (sname, 0); | |
10986 | ||
10987 | if (TREE_COMPLEXITY (declarator) == 0) | |
10988 | /* This needs to be here, in case we are called | |
10989 | multiple times. */ ; | |
9a68c51f JM |
10990 | else if (TREE_COMPLEXITY (declarator) == -1) |
10991 | /* Namespace member. */ | |
10992 | pop_decl_namespace (); | |
8d08fdba | 10993 | else if (friendp && (TREE_COMPLEXITY (declarator) < 2)) |
2c73f9f5 | 10994 | /* Don't fall out into global scope. Hides real bug? --eichin */ ; |
5566b478 MS |
10995 | else if (! IS_AGGR_TYPE_CODE |
10996 | (TREE_CODE (TREE_OPERAND (declarator, 0)))) | |
10997 | ; | |
8d08fdba MS |
10998 | else if (TREE_COMPLEXITY (declarator) == current_class_depth) |
10999 | { | |
9a3b49ac MS |
11000 | /* Resolve any TYPENAME_TYPEs from the decl-specifier-seq |
11001 | that refer to ctype. They couldn't be resolved earlier | |
11002 | because we hadn't pushed into the class yet. | |
11003 | Example: resolve 'B<T>::type' in | |
11004 | 'B<typename B<T>::type> B<T>::f () { }'. */ | |
11005 | if (current_template_parms | |
11006 | && uses_template_parms (type) | |
11007 | && uses_template_parms (current_class_type)) | |
11008 | { | |
11009 | tree args = current_template_args (); | |
c2ea3a40 NS |
11010 | type = tsubst (type, args, tf_error | tf_warning, |
11011 | NULL_TREE); | |
9a3b49ac MS |
11012 | } |
11013 | ||
a28e3c7f MS |
11014 | /* This pop_nested_class corresponds to the |
11015 | push_nested_class used to push into class scope for | |
11016 | parsing the argument list of a function decl, in | |
11017 | qualified_id. */ | |
b74a0560 | 11018 | pop_nested_class (); |
8d08fdba MS |
11019 | TREE_COMPLEXITY (declarator) = current_class_depth; |
11020 | } | |
11021 | else | |
a98facb0 | 11022 | abort (); |
8d08fdba MS |
11023 | |
11024 | if (TREE_OPERAND (declarator, 0) == NULL_TREE) | |
11025 | { | |
11026 | /* We had a reference to a global decl, or | |
11027 | perhaps we were given a non-aggregate typedef, | |
11028 | in which case we cleared this out, and should just | |
11029 | keep going as though it wasn't there. */ | |
11030 | declarator = sname; | |
11031 | continue; | |
11032 | } | |
11033 | ctype = TREE_OPERAND (declarator, 0); | |
11034 | ||
386b8a85 | 11035 | t = ctype; |
68642fb6 | 11036 | while (t != NULL_TREE && CLASS_TYPE_P (t)) |
386b8a85 | 11037 | { |
5362b086 | 11038 | /* You're supposed to have one `template <...>' |
da8a7456 MM |
11039 | for every template class, but you don't need one |
11040 | for a full specialization. For example: | |
11041 | ||
11042 | template <class T> struct S{}; | |
11043 | template <> struct S<int> { void f(); }; | |
11044 | void S<int>::f () {} | |
11045 | ||
11046 | is correct; there shouldn't be a `template <>' for | |
11047 | the definition of `S<int>::f'. */ | |
11048 | if (CLASSTYPE_TEMPLATE_INFO (t) | |
11049 | && (CLASSTYPE_TEMPLATE_INSTANTIATION (t) | |
92ecdfb7 NS |
11050 | || uses_template_parms (CLASSTYPE_TI_ARGS (t))) |
11051 | && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t))) | |
386b8a85 | 11052 | template_count += 1; |
da8a7456 | 11053 | |
386b8a85 | 11054 | t = TYPE_MAIN_DECL (t); |
92ecdfb7 | 11055 | t = DECL_CONTEXT (t); |
386b8a85 JM |
11056 | } |
11057 | ||
8d08fdba MS |
11058 | if (sname == NULL_TREE) |
11059 | goto done_scoping; | |
11060 | ||
11061 | if (TREE_CODE (sname) == IDENTIFIER_NODE) | |
11062 | { | |
11063 | /* This is the `standard' use of the scoping operator: | |
11064 | basetype :: member . */ | |
11065 | ||
db5ae43f | 11066 | if (ctype == current_class_type) |
28cbf42c MS |
11067 | { |
11068 | /* class A { | |
11069 | void A::f (); | |
11070 | }; | |
11071 | ||
11072 | Is this ill-formed? */ | |
11073 | ||
11074 | if (pedantic) | |
33bd39a2 | 11075 | pedwarn ("extra qualification `%T::' on member `%s' ignored", |
28cbf42c MS |
11076 | ctype, name); |
11077 | } | |
db5ae43f | 11078 | else if (TREE_CODE (type) == FUNCTION_TYPE) |
8d08fdba | 11079 | { |
eeb75383 | 11080 | if (current_class_type == NULL_TREE || friendp) |
14ae7e7d JM |
11081 | type = build_cplus_method_type (ctype, TREE_TYPE (type), |
11082 | TYPE_ARG_TYPES (type)); | |
8d08fdba MS |
11083 | else |
11084 | { | |
33bd39a2 | 11085 | error ("cannot declare member function `%T::%s' within `%T'", |
a3203465 | 11086 | ctype, name, current_class_type); |
8d08fdba MS |
11087 | return void_type_node; |
11088 | } | |
11089 | } | |
5566b478 | 11090 | else if (RIDBIT_SETP (RID_TYPEDEF, specbits) |
d0f062fb | 11091 | || COMPLETE_TYPE_P (complete_type (ctype))) |
8d08fdba | 11092 | { |
2c73f9f5 | 11093 | /* Have to move this code elsewhere in this function. |
db5ae43f MS |
11094 | this code is used for i.e., typedef int A::M; M *pm; |
11095 | ||
11096 | It is? How? jason 10/2/94 */ | |
8d08fdba | 11097 | |
8d08fdba MS |
11098 | if (current_class_type) |
11099 | { | |
33bd39a2 | 11100 | error ("cannot declare member `%T::%s' within `%T'", |
db5ae43f MS |
11101 | ctype, name, current_class_type); |
11102 | return void_type_node; | |
8d08fdba MS |
11103 | } |
11104 | type = build_offset_type (ctype, type); | |
11105 | } | |
8d08fdba | 11106 | else |
eeb75383 NS |
11107 | { |
11108 | incomplete_type_error (NULL_TREE, ctype); | |
11109 | return error_mark_node; | |
d363e7bf | 11110 | } |
8d08fdba MS |
11111 | |
11112 | declarator = sname; | |
11113 | } | |
8d08fdba | 11114 | else if (TREE_CODE (sname) == SCOPE_REF) |
a98facb0 | 11115 | abort (); |
8d08fdba MS |
11116 | else |
11117 | { | |
11118 | done_scoping: | |
11119 | declarator = TREE_OPERAND (declarator, 1); | |
11120 | if (declarator && TREE_CODE (declarator) == CALL_EXPR) | |
11121 | /* In this case, we will deal with it later. */ | |
11122 | ; | |
11123 | else | |
11124 | { | |
11125 | if (TREE_CODE (type) == FUNCTION_TYPE) | |
14ae7e7d JM |
11126 | type = build_cplus_method_type (ctype, TREE_TYPE (type), |
11127 | TYPE_ARG_TYPES (type)); | |
8d08fdba MS |
11128 | else |
11129 | type = build_offset_type (ctype, type); | |
11130 | } | |
11131 | } | |
11132 | } | |
11133 | break; | |
11134 | ||
11135 | case BIT_NOT_EXPR: | |
11136 | declarator = TREE_OPERAND (declarator, 0); | |
11137 | break; | |
11138 | ||
8d08fdba MS |
11139 | case RECORD_TYPE: |
11140 | case UNION_TYPE: | |
11141 | case ENUMERAL_TYPE: | |
11142 | declarator = NULL_TREE; | |
11143 | break; | |
11144 | ||
11145 | case ERROR_MARK: | |
11146 | declarator = NULL_TREE; | |
11147 | break; | |
11148 | ||
11149 | default: | |
a98facb0 | 11150 | abort (); |
8d08fdba MS |
11151 | } |
11152 | } | |
11153 | ||
91d231cb | 11154 | if (returned_attrs) |
1eab9b56 | 11155 | { |
91d231cb JM |
11156 | if (attrlist) |
11157 | *attrlist = chainon (returned_attrs, *attrlist); | |
1eab9b56 | 11158 | else |
91d231cb | 11159 | attrlist = &returned_attrs; |
1eab9b56 JM |
11160 | } |
11161 | ||
419c6212 JM |
11162 | /* Now TYPE has the actual type. */ |
11163 | ||
55b3d665 JM |
11164 | /* Did array size calculations overflow? */ |
11165 | ||
11166 | if (TREE_CODE (type) == ARRAY_TYPE | |
11167 | && COMPLETE_TYPE_P (type) | |
11168 | && TREE_OVERFLOW (TYPE_SIZE (type))) | |
11169 | { | |
11170 | error ("size of array `%s' is too large", name); | |
aba649ba | 11171 | /* If we proceed with the array type as it is, we'll eventually |
55b3d665 JM |
11172 | crash in tree_low_cst(). */ |
11173 | type = error_mark_node; | |
11174 | } | |
11175 | ||
34fc957d | 11176 | if (explicitp == 1 || (explicitp && friendp)) |
db5ae43f | 11177 | { |
34fc957d NS |
11178 | /* [dcl.fct.spec] The explicit specifier shall only be used in |
11179 | declarations of constructors within a class definition. */ | |
11180 | error ("only declarations of constructors can be `explicit'"); | |
db5ae43f MS |
11181 | explicitp = 0; |
11182 | } | |
11183 | ||
f30432d7 MS |
11184 | if (RIDBIT_SETP (RID_MUTABLE, specbits)) |
11185 | { | |
34fc957d NS |
11186 | if (current_class_name == NULL_TREE || decl_context == PARM || friendp) |
11187 | { | |
11188 | error ("non-member `%s' cannot be declared `mutable'", name); | |
11189 | RIDBIT_RESET (RID_MUTABLE, specbits); | |
11190 | } | |
11191 | else if (decl_context == TYPENAME || RIDBIT_SETP (RID_TYPEDEF, specbits)) | |
f30432d7 | 11192 | { |
34fc957d | 11193 | error ("non-object member `%s' cannot be declared `mutable'", name); |
f30432d7 MS |
11194 | RIDBIT_RESET (RID_MUTABLE, specbits); |
11195 | } | |
34fc957d NS |
11196 | else if (TREE_CODE (type) == FUNCTION_TYPE |
11197 | || TREE_CODE (type) == METHOD_TYPE) | |
11198 | { | |
11199 | error ("function `%s' cannot be declared `mutable'", name); | |
11200 | RIDBIT_RESET (RID_MUTABLE, specbits); | |
11201 | } | |
f30432d7 MS |
11202 | else if (staticp) |
11203 | { | |
8251199e | 11204 | error ("static `%s' cannot be declared `mutable'", name); |
f30432d7 MS |
11205 | RIDBIT_RESET (RID_MUTABLE, specbits); |
11206 | } | |
34fc957d NS |
11207 | else if (type_quals & TYPE_QUAL_CONST) |
11208 | { | |
11209 | error ("const `%s' cannot be declared `mutable'", name); | |
4f2b0fb2 | 11210 | RIDBIT_RESET (RID_MUTABLE, specbits); |
34fc957d | 11211 | } |
f30432d7 MS |
11212 | } |
11213 | ||
efe38fab JM |
11214 | if (declarator == NULL_TREE |
11215 | || TREE_CODE (declarator) == IDENTIFIER_NODE | |
11216 | || (TREE_CODE (declarator) == TEMPLATE_ID_EXPR | |
11217 | && (TREE_CODE (type) == FUNCTION_TYPE | |
11218 | || TREE_CODE (type) == METHOD_TYPE))) | |
11219 | /* OK */; | |
11220 | else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) | |
11221 | { | |
33bd39a2 | 11222 | error ("template-id `%D' used as a declarator", declarator); |
efe38fab JM |
11223 | declarator = dname; |
11224 | } | |
11225 | else | |
419c6212 | 11226 | /* Unexpected declarator format. */ |
a98facb0 | 11227 | abort (); |
efe38fab | 11228 | |
419c6212 JM |
11229 | /* If this is declaring a typedef name, return a TYPE_DECL. */ |
11230 | ||
fc378698 | 11231 | if (RIDBIT_SETP (RID_TYPEDEF, specbits) && decl_context != TYPENAME) |
8d08fdba MS |
11232 | { |
11233 | tree decl; | |
11234 | ||
11235 | /* Note that the grammar rejects storage classes | |
11236 | in typenames, fields or parameters. */ | |
eff71ab0 PB |
11237 | if (current_lang_name == lang_name_java) |
11238 | TYPE_FOR_JAVA (type) = 1; | |
8d08fdba | 11239 | |
d2e5ee5c MS |
11240 | if (decl_context == FIELD) |
11241 | { | |
094fe153 | 11242 | if (declarator == constructor_name (current_class_type)) |
33bd39a2 | 11243 | pedwarn ("ISO C++ forbids nested type `%D' with same name as enclosing class", |
d2e5ee5c MS |
11244 | declarator); |
11245 | decl = build_lang_decl (TYPE_DECL, declarator, type); | |
d2e5ee5c MS |
11246 | } |
11247 | else | |
6bda7a5e NS |
11248 | { |
11249 | decl = build_decl (TYPE_DECL, declarator, type); | |
11250 | if (!current_function_decl) | |
11251 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); | |
11252 | } | |
11253 | ||
9188c363 MM |
11254 | /* If the user declares "typedef struct {...} foo" then the |
11255 | struct will have an anonymous name. Fill that name in now. | |
11256 | Nothing can refer to it, so nothing needs know about the name | |
11257 | change. */ | |
8d08fdba | 11258 | if (type != error_mark_node |
2bdb0643 | 11259 | && declarator |
8d08fdba MS |
11260 | && TYPE_NAME (type) |
11261 | && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL | |
1951a1b6 | 11262 | && TYPE_ANONYMOUS_P (type) |
89d684bb | 11263 | && cp_type_quals (type) == TYPE_UNQUALIFIED) |
8d08fdba | 11264 | { |
dcd08efc JM |
11265 | tree oldname = TYPE_NAME (type); |
11266 | tree t; | |
11267 | ||
2c73f9f5 | 11268 | /* Replace the anonymous name with the real name everywhere. */ |
8d08fdba | 11269 | lookup_tag_reverse (type, declarator); |
dcd08efc JM |
11270 | for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) |
11271 | if (TYPE_NAME (t) == oldname) | |
11272 | TYPE_NAME (t) = decl; | |
8d08fdba MS |
11273 | |
11274 | if (TYPE_LANG_SPECIFIC (type)) | |
11275 | TYPE_WAS_ANONYMOUS (type) = 1; | |
11276 | ||
33964bf4 MM |
11277 | /* If this is a typedef within a template class, the nested |
11278 | type is a (non-primary) template. The name for the | |
11279 | template needs updating as well. */ | |
11280 | if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type)) | |
68642fb6 | 11281 | DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)) |
33964bf4 MM |
11282 | = TYPE_IDENTIFIER (type); |
11283 | ||
50a6dbd7 JM |
11284 | /* FIXME remangle member functions; member functions of a |
11285 | type with external linkage have external linkage. */ | |
fc378698 | 11286 | } |
fc378698 | 11287 | |
8d08fdba MS |
11288 | if (TREE_CODE (type) == OFFSET_TYPE || TREE_CODE (type) == METHOD_TYPE) |
11289 | { | |
8251199e | 11290 | cp_error_at ("typedef name may not be class-qualified", decl); |
a3203465 | 11291 | return NULL_TREE; |
8d08fdba MS |
11292 | } |
11293 | else if (quals) | |
11294 | { | |
11295 | if (ctype == NULL_TREE) | |
11296 | { | |
11297 | if (TREE_CODE (type) != METHOD_TYPE) | |
cb9a3ff8 | 11298 | cp_error_at ("invalid type qualifier for non-member function type", decl); |
8d08fdba MS |
11299 | else |
11300 | ctype = TYPE_METHOD_BASETYPE (type); | |
11301 | } | |
11302 | if (ctype != NULL_TREE) | |
11303 | grok_method_quals (ctype, decl, quals); | |
11304 | } | |
11305 | ||
11306 | if (RIDBIT_SETP (RID_SIGNED, specbits) | |
11307 | || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))) | |
11308 | C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1; | |
11309 | ||
c91a56d2 MS |
11310 | bad_specifiers (decl, "type", virtualp, quals != NULL_TREE, |
11311 | inlinep, friendp, raises != NULL_TREE); | |
11312 | ||
11313 | if (initialized) | |
8251199e | 11314 | error ("typedef declaration includes an initializer"); |
c91a56d2 | 11315 | |
8d08fdba MS |
11316 | return decl; |
11317 | } | |
11318 | ||
11319 | /* Detect the case of an array type of unspecified size | |
11320 | which came, as such, direct from a typedef name. | |
8d6e459d NS |
11321 | We must copy the type, so that the array's domain can be |
11322 | individually set by the object's initializer. */ | |
8d08fdba | 11323 | |
8d6e459d NS |
11324 | if (type && typedef_type |
11325 | && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type) | |
55b3d665 | 11326 | && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type)) |
8d6e459d | 11327 | type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE); |
8d08fdba | 11328 | |
969fd501 NS |
11329 | /* Detect where we're using a typedef of function type to declare a |
11330 | function. last_function_parms will not be set, so we must create | |
11331 | it now. */ | |
11332 | ||
11333 | if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE) | |
11334 | { | |
11335 | tree decls = NULL_TREE; | |
11336 | tree args; | |
11337 | ||
11338 | for (args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args)) | |
11339 | { | |
11340 | tree decl = build_decl (PARM_DECL, NULL_TREE, TREE_VALUE (args)); | |
11341 | ||
11342 | TREE_CHAIN (decl) = decls; | |
11343 | decls = decl; | |
11344 | } | |
11345 | ||
11346 | last_function_parms = nreverse (decls); | |
11347 | } | |
11348 | ||
8d08fdba MS |
11349 | /* If this is a type name (such as, in a cast or sizeof), |
11350 | compute the type and return it now. */ | |
11351 | ||
11352 | if (decl_context == TYPENAME) | |
11353 | { | |
11354 | /* Note that the grammar rejects storage classes | |
11355 | in typenames, fields or parameters. */ | |
91063b51 | 11356 | if (type_quals != TYPE_UNQUALIFIED) |
6eabb241 | 11357 | type_quals = TYPE_UNQUALIFIED; |
8d08fdba MS |
11358 | |
11359 | /* Special case: "friend class foo" looks like a TYPENAME context. */ | |
11360 | if (friendp) | |
11361 | { | |
91063b51 | 11362 | if (type_quals != TYPE_UNQUALIFIED) |
b7484fbe | 11363 | { |
33bd39a2 | 11364 | error ("type qualifiers specified for friend class declaration"); |
91063b51 | 11365 | type_quals = TYPE_UNQUALIFIED; |
b7484fbe MS |
11366 | } |
11367 | if (inlinep) | |
11368 | { | |
33bd39a2 | 11369 | error ("`inline' specified for friend class declaration"); |
b7484fbe MS |
11370 | inlinep = 0; |
11371 | } | |
f2ae0c45 JM |
11372 | |
11373 | /* Until core issue 180 is resolved, allow 'friend typename A::B'. | |
c00996a3 | 11374 | But don't allow implicit typenames except with a class-key. */ |
f2ae0c45 JM |
11375 | if (!current_aggr && (TREE_CODE (type) != TYPENAME_TYPE |
11376 | || IMPLICIT_TYPENAME_P (type))) | |
4b054b80 NS |
11377 | { |
11378 | if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) | |
33bd39a2 | 11379 | pedwarn ("template parameters cannot be friends"); |
f2ae0c45 | 11380 | else if (TREE_CODE (type) == TYPENAME_TYPE) |
33bd39a2 | 11381 | pedwarn ("\ |
f2ae0c45 JM |
11382 | friend declaration requires class-key, i.e. `friend class %T::%T'", |
11383 | constructor_name (current_class_type), | |
11384 | TYPE_IDENTIFIER (type)); | |
4b054b80 | 11385 | else |
33bd39a2 | 11386 | pedwarn ("\ |
f2ae0c45 JM |
11387 | friend declaration requires class-key, i.e. `friend %#T'", |
11388 | type); | |
4b054b80 | 11389 | } |
b7484fbe MS |
11390 | |
11391 | /* Only try to do this stuff if we didn't already give up. */ | |
11392 | if (type != integer_type_node) | |
11393 | { | |
ab04e34b | 11394 | decl_type_access_control (TYPE_NAME (type)); |
d363e7bf | 11395 | |
b7484fbe MS |
11396 | /* A friendly class? */ |
11397 | if (current_class_type) | |
11398 | make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type)); | |
11399 | else | |
33bd39a2 | 11400 | error ("trying to make class `%T' a friend of global scope", |
2ae7bada | 11401 | type); |
d363e7bf | 11402 | |
b7484fbe MS |
11403 | type = void_type_node; |
11404 | } | |
8d08fdba MS |
11405 | } |
11406 | else if (quals) | |
11407 | { | |
8d08fdba MS |
11408 | if (ctype == NULL_TREE) |
11409 | { | |
41cbc04c | 11410 | if (TREE_CODE (type) != METHOD_TYPE) |
33bd39a2 | 11411 | error ("invalid qualifiers on non-member function type"); |
41cbc04c NS |
11412 | else |
11413 | ctype = TYPE_METHOD_BASETYPE (type); | |
11414 | } | |
11415 | if (ctype) | |
11416 | { | |
11417 | tree dummy = build_decl (TYPE_DECL, declarator, type); | |
11418 | grok_method_quals (ctype, dummy, quals); | |
11419 | type = TREE_TYPE (dummy); | |
8d08fdba | 11420 | } |
8d08fdba MS |
11421 | } |
11422 | ||
11423 | return type; | |
11424 | } | |
11425 | else if (declarator == NULL_TREE && decl_context != PARM | |
db5ae43f | 11426 | && decl_context != CATCHPARM |
8d08fdba MS |
11427 | && TREE_CODE (type) != UNION_TYPE |
11428 | && ! bitfield) | |
11429 | { | |
33bd39a2 | 11430 | error ("abstract declarator `%T' used as declaration", type); |
8d08fdba MS |
11431 | declarator = make_anon_name (); |
11432 | } | |
11433 | ||
11434 | /* `void' at top level (not within pointer) | |
11435 | is allowed only in typedefs or type names. | |
11436 | We don't complain about parms either, but that is because | |
11437 | a better error message can be made later. */ | |
11438 | ||
a1774733 | 11439 | if (TREE_CODE (type) == VOID_TYPE && decl_context != PARM) |
8d08fdba | 11440 | { |
b7484fbe | 11441 | if (! declarator) |
8251199e | 11442 | error ("unnamed variable or field declared void"); |
b7484fbe | 11443 | else if (TREE_CODE (declarator) == IDENTIFIER_NODE) |
8d08fdba MS |
11444 | { |
11445 | if (IDENTIFIER_OPNAME_P (declarator)) | |
a98facb0 | 11446 | abort (); |
8d08fdba | 11447 | else |
8251199e | 11448 | error ("variable or field `%s' declared void", name); |
8d08fdba MS |
11449 | } |
11450 | else | |
8251199e | 11451 | error ("variable or field declared void"); |
8d08fdba MS |
11452 | type = integer_type_node; |
11453 | } | |
11454 | ||
11455 | /* Now create the decl, which may be a VAR_DECL, a PARM_DECL | |
11456 | or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */ | |
11457 | ||
14ae7e7d JM |
11458 | if (decl_context == PARM || decl_context == CATCHPARM) |
11459 | { | |
11460 | if (ctype || in_namespace) | |
11461 | error ("cannot use `::' in parameter declaration"); | |
11462 | ||
11463 | /* A parameter declared as an array of T is really a pointer to T. | |
11464 | One declared as a function is really a pointer to a function. | |
11465 | One declared as a member is really a pointer to member. */ | |
11466 | ||
11467 | if (TREE_CODE (type) == ARRAY_TYPE) | |
11468 | { | |
11469 | /* Transfer const-ness of array into that of type pointed to. */ | |
11470 | type = build_pointer_type (TREE_TYPE (type)); | |
91063b51 | 11471 | type_quals = TYPE_UNQUALIFIED; |
14ae7e7d JM |
11472 | } |
11473 | else if (TREE_CODE (type) == FUNCTION_TYPE) | |
11474 | type = build_pointer_type (type); | |
11475 | else if (TREE_CODE (type) == OFFSET_TYPE) | |
11476 | type = build_pointer_type (type); | |
14ae7e7d | 11477 | } |
68642fb6 | 11478 | |
8d08fdba MS |
11479 | { |
11480 | register tree decl; | |
11481 | ||
11482 | if (decl_context == PARM) | |
11483 | { | |
ff350acd | 11484 | decl = build_decl (PARM_DECL, declarator, type); |
8d08fdba MS |
11485 | |
11486 | bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE, | |
11487 | inlinep, friendp, raises != NULL_TREE); | |
8d08fdba MS |
11488 | |
11489 | /* Compute the type actually passed in the parmlist, | |
11490 | for the case where there is no prototype. | |
11491 | (For example, shorts and chars are passed as ints.) | |
11492 | When there is a prototype, this is overridden later. */ | |
11493 | ||
39211cd5 | 11494 | DECL_ARG_TYPE (decl) = type_promotes_to (type); |
8d08fdba MS |
11495 | } |
11496 | else if (decl_context == FIELD) | |
11497 | { | |
11498 | if (type == error_mark_node) | |
11499 | { | |
11500 | /* Happens when declaring arrays of sizes which | |
11501 | are error_mark_node, for example. */ | |
11502 | decl = NULL_TREE; | |
11503 | } | |
997a088c | 11504 | else if (in_namespace && !friendp) |
05008fb9 MM |
11505 | { |
11506 | /* Something like struct S { int N::j; }; */ | |
33bd39a2 | 11507 | error ("invalid use of `::'"); |
05008fb9 MM |
11508 | decl = NULL_TREE; |
11509 | } | |
8d08fdba MS |
11510 | else if (TREE_CODE (type) == FUNCTION_TYPE) |
11511 | { | |
11512 | int publicp = 0; | |
e1467ff2 | 11513 | tree function_context; |
8d08fdba | 11514 | |
72b7eeff MS |
11515 | /* We catch the others as conflicts with the builtin |
11516 | typedefs. */ | |
11517 | if (friendp && declarator == ridpointers[(int) RID_SIGNED]) | |
11518 | { | |
33bd39a2 | 11519 | error ("function `%D' cannot be declared friend", |
72b7eeff MS |
11520 | declarator); |
11521 | friendp = 0; | |
11522 | } | |
11523 | ||
8d08fdba MS |
11524 | if (friendp == 0) |
11525 | { | |
11526 | if (ctype == NULL_TREE) | |
11527 | ctype = current_class_type; | |
11528 | ||
11529 | if (ctype == NULL_TREE) | |
11530 | { | |
33bd39a2 | 11531 | error ("can't make `%D' into a method -- not in a class", |
8d08fdba MS |
11532 | declarator); |
11533 | return void_type_node; | |
11534 | } | |
11535 | ||
11536 | /* ``A union may [ ... ] not [ have ] virtual functions.'' | |
11537 | ARM 9.5 */ | |
11538 | if (virtualp && TREE_CODE (ctype) == UNION_TYPE) | |
11539 | { | |
33bd39a2 | 11540 | error ("function `%D' declared virtual inside a union", |
8d08fdba MS |
11541 | declarator); |
11542 | return void_type_node; | |
11543 | } | |
11544 | ||
596ea4e5 AS |
11545 | if (declarator == ansi_opname (NEW_EXPR) |
11546 | || declarator == ansi_opname (VEC_NEW_EXPR) | |
11547 | || declarator == ansi_opname (DELETE_EXPR) | |
11548 | || declarator == ansi_opname (VEC_DELETE_EXPR)) | |
8d08fdba MS |
11549 | { |
11550 | if (virtualp) | |
11551 | { | |
33bd39a2 | 11552 | error ("`%D' cannot be declared virtual, since it is always static", |
8d08fdba MS |
11553 | declarator); |
11554 | virtualp = 0; | |
11555 | } | |
11556 | } | |
11557 | else if (staticp < 2) | |
14ae7e7d JM |
11558 | type = build_cplus_method_type (ctype, TREE_TYPE (type), |
11559 | TYPE_ARG_TYPES (type)); | |
8d08fdba MS |
11560 | } |
11561 | ||
11562 | /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */ | |
68642fb6 | 11563 | function_context = (ctype != NULL_TREE) ? |
4f1c5b7d | 11564 | decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE; |
e1467ff2 MM |
11565 | publicp = (! friendp || ! staticp) |
11566 | && function_context == NULL_TREE; | |
68642fb6 | 11567 | decl = grokfndecl (ctype, type, |
386b8a85 JM |
11568 | TREE_CODE (declarator) != TEMPLATE_ID_EXPR |
11569 | ? declarator : dname, | |
11570 | declarator, | |
7a8f9fa9 | 11571 | virtualp, flags, quals, raises, |
386b8a85 | 11572 | friendp ? -1 : 0, friendp, publicp, inlinep, |
2c73f9f5 | 11573 | funcdef_flag, template_count, in_namespace); |
20496fa2 | 11574 | if (decl == NULL_TREE) |
3ddfb0e6 | 11575 | return decl; |
6125f3be DE |
11576 | #if 0 |
11577 | /* This clobbers the attrs stored in `decl' from `attrlist'. */ | |
91d231cb JM |
11578 | /* The decl and setting of decl_attr is also turned off. */ |
11579 | decl = build_decl_attribute_variant (decl, decl_attr); | |
6125f3be | 11580 | #endif |
f0e01782 | 11581 | |
cc804e51 MM |
11582 | /* [class.conv.ctor] |
11583 | ||
11584 | A constructor declared without the function-specifier | |
11585 | explicit that can be called with a single parameter | |
11586 | specifies a conversion from the type of its first | |
11587 | parameter to the type of its class. Such a constructor | |
11588 | is called a converting constructor. */ | |
db5ae43f MS |
11589 | if (explicitp == 2) |
11590 | DECL_NONCONVERTING_P (decl) = 1; | |
cc804e51 MM |
11591 | else if (DECL_CONSTRUCTOR_P (decl)) |
11592 | { | |
11593 | /* The constructor can be called with exactly one | |
11594 | parameter if there is at least one parameter, and | |
11595 | any subsequent parameters have default arguments. | |
e0fff4b3 JM |
11596 | Ignore any compiler-added parms. */ |
11597 | tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (decl); | |
cc804e51 MM |
11598 | |
11599 | if (arg_types == void_list_node | |
68642fb6 UD |
11600 | || (arg_types |
11601 | && TREE_CHAIN (arg_types) | |
cc804e51 MM |
11602 | && TREE_CHAIN (arg_types) != void_list_node |
11603 | && !TREE_PURPOSE (TREE_CHAIN (arg_types)))) | |
11604 | DECL_NONCONVERTING_P (decl) = 1; | |
11605 | } | |
8d08fdba MS |
11606 | } |
11607 | else if (TREE_CODE (type) == METHOD_TYPE) | |
11608 | { | |
faae18ab MS |
11609 | /* We only get here for friend declarations of |
11610 | members of other classes. */ | |
8d08fdba MS |
11611 | /* All method decls are public, so tell grokfndecl to set |
11612 | TREE_PUBLIC, also. */ | |
386b8a85 | 11613 | decl = grokfndecl (ctype, type, declarator, declarator, |
7a8f9fa9 | 11614 | virtualp, flags, quals, raises, |
386b8a85 | 11615 | friendp ? -1 : 0, friendp, 1, 0, funcdef_flag, |
2c73f9f5 | 11616 | template_count, in_namespace); |
f0e01782 MS |
11617 | if (decl == NULL_TREE) |
11618 | return NULL_TREE; | |
8d08fdba | 11619 | } |
5566b478 | 11620 | else if (!staticp && ! processing_template_decl |
d0f062fb | 11621 | && !COMPLETE_TYPE_P (complete_type (type)) |
8d08fdba MS |
11622 | && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0)) |
11623 | { | |
b7484fbe | 11624 | if (declarator) |
33bd39a2 | 11625 | error ("field `%D' has incomplete type", declarator); |
b7484fbe | 11626 | else |
33bd39a2 | 11627 | error ("name `%T' has incomplete type", type); |
8d08fdba MS |
11628 | |
11629 | /* If we're instantiating a template, tell them which | |
11630 | instantiation made the field's type be incomplete. */ | |
11631 | if (current_class_type | |
11632 | && TYPE_NAME (current_class_type) | |
d2e5ee5c | 11633 | && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type)) |
8d08fdba MS |
11634 | && declspecs && TREE_VALUE (declspecs) |
11635 | && TREE_TYPE (TREE_VALUE (declspecs)) == type) | |
33bd39a2 | 11636 | error (" in instantiation of template `%T'", |
db5ae43f MS |
11637 | current_class_type); |
11638 | ||
8d08fdba MS |
11639 | type = error_mark_node; |
11640 | decl = NULL_TREE; | |
11641 | } | |
11642 | else | |
11643 | { | |
11644 | if (friendp) | |
11645 | { | |
cb9a3ff8 | 11646 | error ("`%s' is neither function nor member function; cannot be declared friend", |
8d08fdba MS |
11647 | IDENTIFIER_POINTER (declarator)); |
11648 | friendp = 0; | |
11649 | } | |
11650 | decl = NULL_TREE; | |
11651 | } | |
11652 | ||
11653 | if (friendp) | |
11654 | { | |
11655 | /* Friends are treated specially. */ | |
11656 | if (ctype == current_class_type) | |
8251199e | 11657 | warning ("member functions are implicitly friends of their class"); |
ab04e34b NS |
11658 | else |
11659 | { | |
11660 | tree t = NULL_TREE; | |
11661 | if (decl && DECL_NAME (decl)) | |
11662 | { | |
11663 | if (template_class_depth (current_class_type) == 0) | |
11664 | { | |
11665 | decl | |
11666 | = check_explicit_specialization | |
11667 | (declarator, decl, | |
11668 | template_count, 2 * (funcdef_flag != 0) + 4); | |
11669 | if (decl == error_mark_node) | |
11670 | return error_mark_node; | |
11671 | } | |
11672 | ||
11673 | t = do_friend (ctype, declarator, decl, | |
11674 | last_function_parms, *attrlist, | |
11675 | flags, quals, funcdef_flag); | |
11676 | } | |
11677 | if (t && funcdef_flag) | |
11678 | return t; | |
11679 | ||
11680 | return void_type_node; | |
11681 | } | |
8d08fdba MS |
11682 | } |
11683 | ||
11684 | /* Structure field. It may not be a function, except for C++ */ | |
11685 | ||
11686 | if (decl == NULL_TREE) | |
11687 | { | |
8d08fdba MS |
11688 | if (initialized) |
11689 | { | |
3ac3d9ea MM |
11690 | if (!staticp) |
11691 | { | |
11692 | /* An attempt is being made to initialize a non-static | |
11693 | member. But, from [class.mem]: | |
68642fb6 | 11694 | |
3ac3d9ea MM |
11695 | 4 A member-declarator can contain a |
11696 | constant-initializer only if it declares a static | |
11697 | member (_class.static_) of integral or enumeration | |
68642fb6 | 11698 | type, see _class.static.data_. |
3ac3d9ea MM |
11699 | |
11700 | This used to be relatively common practice, but | |
11701 | the rest of the compiler does not correctly | |
11702 | handle the initialization unless the member is | |
11703 | static so we make it static below. */ | |
33bd39a2 | 11704 | pedwarn ("ISO C++ forbids initialization of member `%D'", |
3ac3d9ea | 11705 | declarator); |
33bd39a2 | 11706 | pedwarn ("making `%D' static", declarator); |
3ac3d9ea MM |
11707 | staticp = 1; |
11708 | } | |
11709 | ||
6ba89f8e MM |
11710 | if (uses_template_parms (type)) |
11711 | /* We'll check at instantiation time. */ | |
11712 | ; | |
11713 | else if (check_static_variable_definition (declarator, | |
11714 | type)) | |
11715 | /* If we just return the declaration, crashes | |
11716 | will sometimes occur. We therefore return | |
72a93143 JM |
11717 | void_type_node, as if this was a friend |
11718 | declaration, to cause callers to completely | |
11719 | ignore this declaration. */ | |
6ba89f8e | 11720 | return void_type_node; |
8d08fdba MS |
11721 | } |
11722 | ||
8ebeee52 | 11723 | /* 9.2p13 [class.mem] */ |
4d7614e9 | 11724 | if (declarator == constructor_name (current_class_type) |
e54930f9 JM |
11725 | /* The standard does not allow non-static data members |
11726 | here either, but we agreed at the 10/99 meeting | |
11727 | to change that in TC 1 so that they are allowed in | |
11728 | classes with no user-defined constructors. */ | |
11729 | && staticp) | |
33bd39a2 | 11730 | pedwarn ("ISO C++ forbids static data member `%D' with same name as enclosing class", |
8ebeee52 JM |
11731 | declarator); |
11732 | ||
3ac3d9ea | 11733 | if (staticp) |
8d08fdba | 11734 | { |
f18a14bc MM |
11735 | /* C++ allows static class members. All other work |
11736 | for this is done by grokfield. */ | |
4ce3d537 | 11737 | decl = build_lang_decl (VAR_DECL, declarator, type); |
5b605f68 MS |
11738 | TREE_STATIC (decl) = 1; |
11739 | /* In class context, 'static' means public access. */ | |
3ac3d9ea | 11740 | TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = 1; |
8d08fdba MS |
11741 | } |
11742 | else | |
11743 | { | |
721c3b42 | 11744 | decl = build_decl (FIELD_DECL, declarator, type); |
2bf105ab | 11745 | DECL_NONADDRESSABLE_P (decl) = bitfield; |
8d08fdba MS |
11746 | if (RIDBIT_SETP (RID_MUTABLE, specbits)) |
11747 | { | |
11748 | DECL_MUTABLE_P (decl) = 1; | |
11749 | RIDBIT_RESET (RID_MUTABLE, specbits); | |
11750 | } | |
11751 | } | |
11752 | ||
11753 | bad_specifiers (decl, "field", virtualp, quals != NULL_TREE, | |
11754 | inlinep, friendp, raises != NULL_TREE); | |
11755 | } | |
11756 | } | |
11757 | else if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE) | |
11758 | { | |
386b8a85 | 11759 | tree original_name; |
8d08fdba MS |
11760 | int publicp = 0; |
11761 | ||
11762 | if (! declarator) | |
11763 | return NULL_TREE; | |
11764 | ||
386b8a85 JM |
11765 | if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) |
11766 | original_name = dname; | |
11767 | else | |
11768 | original_name = declarator; | |
11769 | ||
8926095f | 11770 | if (RIDBIT_SETP (RID_AUTO, specbits)) |
8251199e | 11771 | error ("storage class `auto' invalid for function `%s'", name); |
8926095f | 11772 | else if (RIDBIT_SETP (RID_REGISTER, specbits)) |
8251199e | 11773 | error ("storage class `register' invalid for function `%s'", name); |
8d08fdba MS |
11774 | |
11775 | /* Function declaration not at top level. | |
11776 | Storage classes other than `extern' are not allowed | |
11777 | and `extern' makes no difference. */ | |
a9aedbc2 | 11778 | if (! toplevel_bindings_p () |
8926095f MS |
11779 | && (RIDBIT_SETP (RID_STATIC, specbits) |
11780 | || RIDBIT_SETP (RID_INLINE, specbits)) | |
8d08fdba | 11781 | && pedantic) |
8926095f MS |
11782 | { |
11783 | if (RIDBIT_SETP (RID_STATIC, specbits)) | |
8251199e | 11784 | pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name); |
8926095f | 11785 | else |
8251199e | 11786 | pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name); |
8926095f | 11787 | } |
68642fb6 | 11788 | |
8d08fdba MS |
11789 | if (ctype == NULL_TREE) |
11790 | { | |
11791 | if (virtualp) | |
11792 | { | |
8251199e | 11793 | error ("virtual non-class function `%s'", name); |
8d08fdba MS |
11794 | virtualp = 0; |
11795 | } | |
8d08fdba MS |
11796 | } |
11797 | else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2) | |
14ae7e7d JM |
11798 | type = build_cplus_method_type (ctype, TREE_TYPE (type), |
11799 | TYPE_ARG_TYPES (type)); | |
8d08fdba | 11800 | |
eb66be0e | 11801 | /* Record presence of `static'. */ |
faae18ab | 11802 | publicp = (ctype != NULL_TREE |
a9aedbc2 | 11803 | || RIDBIT_SETP (RID_EXTERN, specbits) |
eb66be0e | 11804 | || !RIDBIT_SETP (RID_STATIC, specbits)); |
8d08fdba | 11805 | |
386b8a85 | 11806 | decl = grokfndecl (ctype, type, original_name, declarator, |
7a8f9fa9 | 11807 | virtualp, flags, quals, raises, |
75650646 | 11808 | 1, friendp, |
68642fb6 | 11809 | publicp, inlinep, funcdef_flag, |
2c73f9f5 | 11810 | template_count, in_namespace); |
f0e01782 MS |
11811 | if (decl == NULL_TREE) |
11812 | return NULL_TREE; | |
8d08fdba | 11813 | |
8d08fdba MS |
11814 | if (staticp == 1) |
11815 | { | |
11816 | int illegal_static = 0; | |
11817 | ||
11818 | /* Don't allow a static member function in a class, and forbid | |
11819 | declaring main to be static. */ | |
11820 | if (TREE_CODE (type) == METHOD_TYPE) | |
11821 | { | |
33bd39a2 | 11822 | pedwarn ("cannot declare member function `%D' to have static linkage", decl); |
8d08fdba MS |
11823 | illegal_static = 1; |
11824 | } | |
8d08fdba MS |
11825 | else if (current_function_decl) |
11826 | { | |
11827 | /* FIXME need arm citation */ | |
8251199e | 11828 | error ("cannot declare static function inside another function"); |
8d08fdba MS |
11829 | illegal_static = 1; |
11830 | } | |
11831 | ||
11832 | if (illegal_static) | |
11833 | { | |
11834 | staticp = 0; | |
11835 | RIDBIT_RESET (RID_STATIC, specbits); | |
11836 | } | |
11837 | } | |
8d08fdba MS |
11838 | } |
11839 | else | |
11840 | { | |
11841 | /* It's a variable. */ | |
11842 | ||
11843 | /* An uninitialized decl with `extern' is a reference. */ | |
68642fb6 UD |
11844 | decl = grokvardecl (type, declarator, &specbits, |
11845 | initialized, | |
11846 | (type_quals & TYPE_QUAL_CONST) != 0, | |
950ad3c3 | 11847 | in_namespace); |
8d08fdba MS |
11848 | bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE, |
11849 | inlinep, friendp, raises != NULL_TREE); | |
11850 | ||
11851 | if (ctype) | |
11852 | { | |
f0e01782 | 11853 | DECL_CONTEXT (decl) = ctype; |
8d08fdba MS |
11854 | if (staticp == 1) |
11855 | { | |
33bd39a2 | 11856 | pedwarn ("`static' may not be used when defining (as opposed to declaring) a static data member"); |
8d08fdba MS |
11857 | staticp = 0; |
11858 | RIDBIT_RESET (RID_STATIC, specbits); | |
11859 | } | |
b7484fbe MS |
11860 | if (RIDBIT_SETP (RID_REGISTER, specbits) && TREE_STATIC (decl)) |
11861 | { | |
33bd39a2 | 11862 | error ("static member `%D' declared `register'", decl); |
b7484fbe MS |
11863 | RIDBIT_RESET (RID_REGISTER, specbits); |
11864 | } | |
f30432d7 | 11865 | if (RIDBIT_SETP (RID_EXTERN, specbits) && pedantic) |
8d08fdba | 11866 | { |
33bd39a2 | 11867 | pedwarn ("cannot explicitly declare member `%#D' to have extern linkage", |
f30432d7 | 11868 | decl); |
8d08fdba MS |
11869 | RIDBIT_RESET (RID_EXTERN, specbits); |
11870 | } | |
11871 | } | |
11872 | } | |
11873 | ||
34fc957d | 11874 | my_friendly_assert (!RIDBIT_SETP (RID_MUTABLE, specbits), 19990927); |
8d08fdba MS |
11875 | |
11876 | /* Record `register' declaration for warnings on & | |
11877 | and in case doing stupid register allocation. */ | |
11878 | ||
11879 | if (RIDBIT_SETP (RID_REGISTER, specbits)) | |
11880 | DECL_REGISTER (decl) = 1; | |
11881 | ||
8926095f MS |
11882 | if (RIDBIT_SETP (RID_EXTERN, specbits)) |
11883 | DECL_THIS_EXTERN (decl) = 1; | |
11884 | ||
faae18ab MS |
11885 | if (RIDBIT_SETP (RID_STATIC, specbits)) |
11886 | DECL_THIS_STATIC (decl) = 1; | |
11887 | ||
adecb3f4 MM |
11888 | /* Record constancy and volatility. There's no need to do this |
11889 | when processing a template; we'll do this for the instantiated | |
11890 | declaration based on the type of DECL. */ | |
11891 | if (!processing_template_decl) | |
11892 | c_apply_type_quals_to_decl (type_quals, decl); | |
8d08fdba MS |
11893 | |
11894 | return decl; | |
11895 | } | |
11896 | } | |
11897 | \f | |
11898 | /* Tell if a parmlist/exprlist looks like an exprlist or a parmlist. | |
11899 | An empty exprlist is a parmlist. An exprlist which | |
11900 | contains only identifiers at the global level | |
11901 | is a parmlist. Otherwise, it is an exprlist. */ | |
e92cc029 | 11902 | |
8d08fdba MS |
11903 | int |
11904 | parmlist_is_exprlist (exprs) | |
11905 | tree exprs; | |
11906 | { | |
11907 | if (exprs == NULL_TREE || TREE_PARMLIST (exprs)) | |
11908 | return 0; | |
11909 | ||
a9aedbc2 | 11910 | if (toplevel_bindings_p ()) |
8d08fdba MS |
11911 | { |
11912 | /* At the global level, if these are all identifiers, | |
11913 | then it is a parmlist. */ | |
11914 | while (exprs) | |
11915 | { | |
11916 | if (TREE_CODE (TREE_VALUE (exprs)) != IDENTIFIER_NODE) | |
11917 | return 1; | |
11918 | exprs = TREE_CHAIN (exprs); | |
11919 | } | |
11920 | return 0; | |
11921 | } | |
11922 | return 1; | |
11923 | } | |
11924 | ||
f181d4ae MM |
11925 | /* Subroutine of start_function. Ensure that each of the parameter |
11926 | types (as listed in PARMS) is complete, as is required for a | |
11927 | function definition. */ | |
e92cc029 | 11928 | |
8d08fdba MS |
11929 | static void |
11930 | require_complete_types_for_parms (parms) | |
11931 | tree parms; | |
11932 | { | |
07c88314 | 11933 | for (; parms; parms = TREE_CHAIN (parms)) |
8d08fdba | 11934 | { |
5cce22b6 NS |
11935 | if (VOID_TYPE_P (TREE_TYPE (parms))) |
11936 | /* grokparms will have already issued an error */ | |
11937 | TREE_TYPE (parms) = error_mark_node; | |
11938 | else if (complete_type_or_else (TREE_TYPE (parms), parms)) | |
42f989ff | 11939 | layout_decl (parms, 0); |
5cce22b6 NS |
11940 | else |
11941 | TREE_TYPE (parms) = error_mark_node; | |
8d08fdba MS |
11942 | } |
11943 | } | |
11944 | ||
46e8c075 | 11945 | /* Returns non-zero if T is a local variable. */ |
297e73d8 | 11946 | |
46e8c075 MM |
11947 | int |
11948 | local_variable_p (t) | |
11949 | tree t; | |
297e73d8 | 11950 | { |
68642fb6 | 11951 | if ((TREE_CODE (t) == VAR_DECL |
297e73d8 MM |
11952 | /* A VAR_DECL with a context that is a _TYPE is a static data |
11953 | member. */ | |
11954 | && !TYPE_P (CP_DECL_CONTEXT (t)) | |
11955 | /* Any other non-local variable must be at namespace scope. */ | |
46e8c075 | 11956 | && !DECL_NAMESPACE_SCOPE_P (t)) |
297e73d8 | 11957 | || (TREE_CODE (t) == PARM_DECL)) |
46e8c075 | 11958 | return 1; |
297e73d8 | 11959 | |
46e8c075 MM |
11960 | return 0; |
11961 | } | |
11962 | ||
11963 | /* Returns non-zero if T is an automatic local variable or a label. | |
11964 | (These are the declarations that need to be remapped when the code | |
11965 | containing them is duplicated.) */ | |
11966 | ||
11967 | int | |
11968 | nonstatic_local_decl_p (t) | |
11969 | tree t; | |
11970 | { | |
11971 | return ((local_variable_p (t) && !TREE_STATIC (t)) | |
11972 | || TREE_CODE (t) == LABEL_DECL | |
11973 | || TREE_CODE (t) == RESULT_DECL); | |
11974 | } | |
11975 | ||
11976 | /* Like local_variable_p, but suitable for use as a tree-walking | |
11977 | function. */ | |
11978 | ||
11979 | static tree | |
11980 | local_variable_p_walkfn (tp, walk_subtrees, data) | |
11981 | tree *tp; | |
11982 | int *walk_subtrees ATTRIBUTE_UNUSED; | |
11983 | void *data ATTRIBUTE_UNUSED; | |
11984 | { | |
68642fb6 | 11985 | return ((local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp)) |
46e8c075 | 11986 | ? *tp : NULL_TREE); |
297e73d8 MM |
11987 | } |
11988 | ||
11989 | /* Check that ARG, which is a default-argument expression for a | |
11990 | parameter DECL, is legal. Returns ARG, or ERROR_MARK_NODE, if | |
11991 | something goes wrong. DECL may also be a _TYPE node, rather than a | |
11992 | DECL, if there is no DECL available. */ | |
11993 | ||
11994 | tree | |
11995 | check_default_argument (decl, arg) | |
11996 | tree decl; | |
11997 | tree arg; | |
11998 | { | |
11999 | tree var; | |
12000 | tree decl_type; | |
12001 | ||
12002 | if (TREE_CODE (arg) == DEFAULT_ARG) | |
12003 | /* We get a DEFAULT_ARG when looking at an in-class declaration | |
12004 | with a default argument. Ignore the argument for now; we'll | |
12005 | deal with it after the class is complete. */ | |
12006 | return arg; | |
12007 | ||
12008 | if (processing_template_decl || uses_template_parms (arg)) | |
12009 | /* We don't do anything checking until instantiation-time. Note | |
12010 | that there may be uninstantiated arguments even for an | |
12011 | instantiated function, since default arguments are not | |
12012 | instantiated until they are needed. */ | |
12013 | return arg; | |
12014 | ||
12015 | if (TYPE_P (decl)) | |
12016 | { | |
12017 | decl_type = decl; | |
12018 | decl = NULL_TREE; | |
12019 | } | |
12020 | else | |
12021 | decl_type = TREE_TYPE (decl); | |
12022 | ||
68642fb6 | 12023 | if (arg == error_mark_node |
297e73d8 MM |
12024 | || decl == error_mark_node |
12025 | || TREE_TYPE (arg) == error_mark_node | |
12026 | || decl_type == error_mark_node) | |
12027 | /* Something already went wrong. There's no need to check | |
12028 | further. */ | |
12029 | return error_mark_node; | |
12030 | ||
12031 | /* [dcl.fct.default] | |
68642fb6 | 12032 | |
297e73d8 MM |
12033 | A default argument expression is implicitly converted to the |
12034 | parameter type. */ | |
12035 | if (!TREE_TYPE (arg) | |
12036 | || !can_convert_arg (decl_type, TREE_TYPE (arg), arg)) | |
12037 | { | |
12038 | if (decl) | |
33bd39a2 | 12039 | error ("default argument for `%#D' has type `%T'", |
297e73d8 MM |
12040 | decl, TREE_TYPE (arg)); |
12041 | else | |
33bd39a2 | 12042 | error ("default argument for parameter of type `%T' has type `%T'", |
297e73d8 MM |
12043 | decl_type, TREE_TYPE (arg)); |
12044 | ||
12045 | return error_mark_node; | |
12046 | } | |
12047 | ||
12048 | /* [dcl.fct.default] | |
12049 | ||
12050 | Local variables shall not be used in default argument | |
68642fb6 | 12051 | expressions. |
297e73d8 MM |
12052 | |
12053 | The keyword `this' shall not be used in a default argument of a | |
12054 | member function. */ | |
5362b086 | 12055 | var = walk_tree_without_duplicates (&arg, local_variable_p_walkfn, |
ee94fce6 | 12056 | NULL); |
297e73d8 MM |
12057 | if (var) |
12058 | { | |
33bd39a2 | 12059 | error ("default argument `%E' uses local variable `%D'", |
297e73d8 MM |
12060 | arg, var); |
12061 | return error_mark_node; | |
12062 | } | |
12063 | ||
12064 | /* All is well. */ | |
12065 | return arg; | |
12066 | } | |
12067 | ||
8d08fdba MS |
12068 | /* Decode the list of parameter types for a function type. |
12069 | Given the list of things declared inside the parens, | |
12070 | return a list of types. | |
12071 | ||
5cce22b6 NS |
12072 | We determine whether ellipsis parms are used by PARMLIST_ELLIPSIS_P |
12073 | flag. If unset, we append void_list_node. A parmlist declared | |
12074 | as `(void)' is accepted as the empty parmlist. | |
12075 | ||
12076 | Also set last_function_parms to the chain of PARM_DECLs. */ | |
8d08fdba MS |
12077 | |
12078 | static tree | |
5cce22b6 | 12079 | grokparms (first_parm) |
8d08fdba | 12080 | tree first_parm; |
8d08fdba MS |
12081 | { |
12082 | tree result = NULL_TREE; | |
12083 | tree decls = NULL_TREE; | |
5cce22b6 NS |
12084 | int ellipsis = !first_parm || PARMLIST_ELLIPSIS_P (first_parm); |
12085 | tree parm, chain; | |
12086 | int any_error = 0; | |
8d08fdba | 12087 | |
5cce22b6 NS |
12088 | my_friendly_assert (!first_parm || TREE_PARMLIST (first_parm), 20001115); |
12089 | ||
12090 | for (parm = first_parm; parm != NULL_TREE; parm = chain) | |
8d08fdba | 12091 | { |
0657c69c | 12092 | tree type = NULL_TREE; |
98884b26 | 12093 | tree decl = TREE_VALUE (parm); |
5cce22b6 | 12094 | tree init = TREE_PURPOSE (parm); |
98884b26 | 12095 | tree specs, attrs; |
8d08fdba | 12096 | |
5cce22b6 NS |
12097 | chain = TREE_CHAIN (parm); |
12098 | /* @@ weak defense against parse errors. */ | |
12099 | if (TREE_CODE (decl) != VOID_TYPE | |
12100 | && TREE_CODE (decl) != TREE_LIST) | |
8d08fdba | 12101 | { |
5cce22b6 NS |
12102 | /* Give various messages as the need arises. */ |
12103 | if (TREE_CODE (decl) == STRING_CST) | |
33bd39a2 | 12104 | error ("invalid string constant `%E'", decl); |
5cce22b6 NS |
12105 | else if (TREE_CODE (decl) == INTEGER_CST) |
12106 | error ("invalid integer constant in parameter list, did you forget to give parameter name?"); | |
12107 | continue; | |
12108 | } | |
8d08fdba | 12109 | |
5cce22b6 NS |
12110 | if (parm == void_list_node) |
12111 | break; | |
8d08fdba | 12112 | |
98884b26 JM |
12113 | split_specs_attrs (TREE_PURPOSE (decl), &specs, &attrs); |
12114 | decl = grokdeclarator (TREE_VALUE (decl), specs, | |
12115 | PARM, init != NULL_TREE, &attrs); | |
5cce22b6 NS |
12116 | if (! decl || TREE_TYPE (decl) == error_mark_node) |
12117 | continue; | |
d363e7bf | 12118 | |
98884b26 JM |
12119 | if (attrs) |
12120 | cplus_decl_attributes (&decl, attrs, 0); | |
12121 | ||
5cce22b6 NS |
12122 | type = TREE_TYPE (decl); |
12123 | if (VOID_TYPE_P (type)) | |
12124 | { | |
12125 | if (same_type_p (type, void_type_node) | |
12126 | && !DECL_NAME (decl) && !result && !chain && !ellipsis) | |
12127 | /* this is a parmlist of `(void)', which is ok. */ | |
12128 | break; | |
12129 | incomplete_type_error (decl, type); | |
0657c69c MM |
12130 | /* It's not a good idea to actually create parameters of |
12131 | type `void'; other parts of the compiler assume that a | |
12132 | void type terminates the parameter list. */ | |
04f3dc2b | 12133 | type = error_mark_node; |
0657c69c | 12134 | TREE_TYPE (decl) = error_mark_node; |
5cce22b6 | 12135 | } |
8d08fdba | 12136 | |
d363e7bf | 12137 | if (type != error_mark_node) |
04f3dc2b MM |
12138 | { |
12139 | /* Top-level qualifiers on the parameters are | |
12140 | ignored for function types. */ | |
12141 | type = TYPE_MAIN_VARIANT (type); | |
12142 | if (TREE_CODE (type) == METHOD_TYPE) | |
12143 | { | |
33bd39a2 | 12144 | error ("parameter `%D' invalidly declared method type", decl); |
04f3dc2b MM |
12145 | type = build_pointer_type (type); |
12146 | TREE_TYPE (decl) = type; | |
12147 | } | |
12148 | else if (TREE_CODE (type) == OFFSET_TYPE) | |
12149 | { | |
33bd39a2 | 12150 | error ("parameter `%D' invalidly declared offset type", decl); |
04f3dc2b MM |
12151 | type = build_pointer_type (type); |
12152 | TREE_TYPE (decl) = type; | |
12153 | } | |
12154 | else if (abstract_virtuals_error (decl, type)) | |
12155 | any_error = 1; /* Seems like a good idea. */ | |
12156 | else if (POINTER_TYPE_P (type)) | |
12157 | { | |
12158 | /* [dcl.fct]/6, parameter types cannot contain pointers | |
12159 | (references) to arrays of unknown bound. */ | |
98979fe0 NS |
12160 | tree t = TREE_TYPE (type); |
12161 | int ptr = TYPE_PTR_P (type); | |
12162 | ||
12163 | while (1) | |
12164 | { | |
12165 | if (TYPE_PTR_P (t)) | |
12166 | ptr = 1; | |
12167 | else if (TREE_CODE (t) != ARRAY_TYPE) | |
12168 | break; | |
12169 | else if (!TYPE_DOMAIN (t)) | |
12170 | break; | |
12171 | t = TREE_TYPE (t); | |
12172 | } | |
04f3dc2b | 12173 | if (TREE_CODE (t) == ARRAY_TYPE) |
33bd39a2 | 12174 | error ("parameter `%D' includes %s to array of unknown bound `%T'", |
98979fe0 | 12175 | decl, ptr ? "pointer" : "reference", t); |
04f3dc2b MM |
12176 | } |
12177 | ||
12178 | DECL_ARG_TYPE (decl) = TREE_TYPE (decl); | |
12179 | if (PROMOTE_PROTOTYPES | |
acf97e0b | 12180 | && INTEGRAL_TYPE_P (type) |
04f3dc2b MM |
12181 | && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) |
12182 | DECL_ARG_TYPE (decl) = integer_type_node; | |
12183 | if (!any_error && init) | |
12184 | init = check_default_argument (decl, init); | |
12185 | else | |
12186 | init = NULL_TREE; | |
12187 | } | |
8d08fdba | 12188 | |
5cce22b6 NS |
12189 | TREE_CHAIN (decl) = decls; |
12190 | decls = decl; | |
0657c69c | 12191 | result = tree_cons (init, type, result); |
8d08fdba | 12192 | } |
5cce22b6 NS |
12193 | decls = nreverse (decls); |
12194 | result = nreverse (result); | |
12195 | if (!ellipsis) | |
12196 | result = chainon (result, void_list_node); | |
8d08fdba MS |
12197 | last_function_parms = decls; |
12198 | ||
8d08fdba MS |
12199 | return result; |
12200 | } | |
42976354 | 12201 | |
8d08fdba | 12202 | \f |
271e6f02 NS |
12203 | /* D is a constructor or overloaded `operator='. |
12204 | ||
12205 | Let T be the class in which D is declared. Then, this function | |
12206 | returns: | |
12207 | ||
12208 | -1 if D's is an ill-formed constructor or copy assignment operator | |
12209 | whose first parameter is of type `T'. | |
12210 | 0 if D is not a copy constructor or copy assignment | |
12211 | operator. | |
12212 | 1 if D is a copy constructor or copy assignment operator whose | |
12213 | first parameter is a reference to const qualified T. | |
12214 | 2 if D is a copy constructor or copy assignment operator whose | |
12215 | first parameter is a reference to non-const qualified T. | |
12216 | ||
12217 | This function can be used as a predicate. Positive values indicate | |
12218 | a copy constructor and non-zero values indicate a copy assignment | |
4f1c5b7d MM |
12219 | operator. */ |
12220 | ||
c11b6f21 | 12221 | int |
271e6f02 | 12222 | copy_fn_p (d) |
c11b6f21 MS |
12223 | tree d; |
12224 | { | |
271e6f02 NS |
12225 | tree args; |
12226 | tree arg_type; | |
12227 | int result = 1; | |
12228 | ||
12229 | my_friendly_assert (DECL_FUNCTION_MEMBER_P (d), 20011208); | |
4f1c5b7d | 12230 | |
271e6f02 NS |
12231 | if (DECL_TEMPLATE_INFO (d) && is_member_template (DECL_TI_TEMPLATE (d))) |
12232 | /* Instantiations of template member functions are never copy | |
12233 | functions. Note that member functions of templated classes are | |
12234 | represented as template functions internally, and we must | |
12235 | accept those as copy functions. */ | |
12236 | return 0; | |
12237 | ||
12238 | args = FUNCTION_FIRST_USER_PARMTYPE (d); | |
12239 | if (!args) | |
4f1c5b7d MM |
12240 | return 0; |
12241 | ||
271e6f02 NS |
12242 | arg_type = TREE_VALUE (args); |
12243 | ||
12244 | if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d)) | |
12245 | { | |
12246 | /* Pass by value copy assignment operator. */ | |
12247 | result = -1; | |
12248 | } | |
12249 | else if (TREE_CODE (arg_type) == REFERENCE_TYPE | |
12250 | && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d)) | |
12251 | { | |
12252 | if (CP_TYPE_CONST_P (TREE_TYPE (arg_type))) | |
12253 | result = 2; | |
12254 | } | |
12255 | else | |
12256 | return 0; | |
12257 | ||
12258 | args = TREE_CHAIN (args); | |
12259 | ||
12260 | if (args && args != void_list_node && !TREE_PURPOSE (args)) | |
12261 | /* There are more non-optional args. */ | |
12262 | return 0; | |
12263 | ||
12264 | return result; | |
12265 | } | |
12266 | ||
12267 | /* Remember any special properties of member function DECL. */ | |
12268 | ||
12269 | void grok_special_member_properties (decl) | |
12270 | tree decl; | |
12271 | { | |
12272 | if (!DECL_NONSTATIC_MEMBER_FUNCTION_P(decl)) | |
12273 | ; /* Not special. */ | |
12274 | else if (DECL_CONSTRUCTOR_P (decl)) | |
12275 | { | |
12276 | int ctor = copy_fn_p (decl); | |
12277 | ||
12278 | if (ctor > 0) | |
12279 | { | |
12280 | /* [class.copy] | |
12281 | ||
12282 | A non-template constructor for class X is a copy | |
12283 | constructor if its first parameter is of type X&, const | |
12284 | X&, volatile X& or const volatile X&, and either there | |
12285 | are no other parameters or else all other parameters have | |
12286 | default arguments. */ | |
12287 | TYPE_HAS_INIT_REF (DECL_CONTEXT (decl)) = 1; | |
12288 | if (ctor > 1) | |
12289 | TYPE_HAS_CONST_INIT_REF (DECL_CONTEXT (decl)) = 1; | |
12290 | } | |
12291 | else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl))) | |
12292 | TYPE_HAS_DEFAULT_CONSTRUCTOR (DECL_CONTEXT (decl)) = 1; | |
12293 | } | |
12294 | else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR) | |
12295 | { | |
12296 | /* [class.copy] | |
12297 | ||
12298 | A non-template assignment operator for class X is a copy | |
12299 | assignment operator if its parameter is of type X, X&, const | |
12300 | X&, volatile X& or const volatile X&. */ | |
12301 | ||
12302 | int assop = copy_fn_p (decl); | |
12303 | ||
12304 | if (assop) | |
12305 | { | |
12306 | TYPE_HAS_ASSIGN_REF (DECL_CONTEXT (decl)) = 1; | |
12307 | if (assop != 1) | |
12308 | TYPE_HAS_CONST_ASSIGN_REF (DECL_CONTEXT (decl)) = 1; | |
12309 | if (DECL_PURE_VIRTUAL_P (decl)) | |
12310 | TYPE_HAS_ABSTRACT_ASSIGN_REF (DECL_CONTEXT (decl)) = 1; | |
12311 | } | |
12312 | } | |
c11b6f21 MS |
12313 | } |
12314 | ||
271e6f02 NS |
12315 | /* Check a constructor DECL has the correct form. Complains |
12316 | if the class has a constructor of the form X(X). */ | |
e92cc029 | 12317 | |
a0a33927 | 12318 | int |
8d08fdba MS |
12319 | grok_ctor_properties (ctype, decl) |
12320 | tree ctype, decl; | |
12321 | { | |
271e6f02 NS |
12322 | int ctor_parm = copy_fn_p (decl); |
12323 | ||
12324 | if (ctor_parm < 0) | |
12325 | { | |
12326 | /* [class.copy] | |
12327 | ||
12328 | A declaration of a constructor for a class X is ill-formed if | |
12329 | its first parameter is of type (optionally cv-qualified) X | |
12330 | and either there are no other parameters or else all other | |
12331 | parameters have default arguments. | |
12332 | ||
12333 | We *don't* complain about member template instantiations that | |
12334 | have this form, though; they can occur as we try to decide | |
12335 | what constructor to use during overload resolution. Since | |
12336 | overload resolution will never prefer such a constructor to | |
12337 | the non-template copy constructor (which is either explicitly | |
12338 | or implicitly defined), there's no need to worry about their | |
12339 | existence. Theoretically, they should never even be | |
12340 | instantiated, but that's hard to forestall. */ | |
33bd39a2 | 12341 | error ("invalid constructor; you probably meant `%T (const %T&)'", |
0b41abe6 JM |
12342 | ctype, ctype); |
12343 | SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype); | |
12344 | return 0; | |
8d08fdba | 12345 | } |
271e6f02 | 12346 | |
a0a33927 | 12347 | return 1; |
8d08fdba MS |
12348 | } |
12349 | ||
596ea4e5 | 12350 | /* An operator with this code is unary, but can also be binary. */ |
e92cc029 | 12351 | |
a28e3c7f | 12352 | static int |
596ea4e5 AS |
12353 | ambi_op_p (code) |
12354 | enum tree_code code; | |
8d08fdba | 12355 | { |
596ea4e5 AS |
12356 | return (code == INDIRECT_REF |
12357 | || code == ADDR_EXPR | |
12358 | || code == CONVERT_EXPR | |
12359 | || code == NEGATE_EXPR | |
12360 | || code == PREINCREMENT_EXPR | |
12361 | || code == PREDECREMENT_EXPR); | |
8d08fdba MS |
12362 | } |
12363 | ||
12364 | /* An operator with this name can only be unary. */ | |
e92cc029 | 12365 | |
a28e3c7f | 12366 | static int |
596ea4e5 AS |
12367 | unary_op_p (code) |
12368 | enum tree_code code; | |
8d08fdba | 12369 | { |
596ea4e5 AS |
12370 | return (code == TRUTH_NOT_EXPR |
12371 | || code == BIT_NOT_EXPR | |
12372 | || code == COMPONENT_REF | |
12373 | || code == TYPE_EXPR); | |
8d08fdba MS |
12374 | } |
12375 | ||
12376 | /* Do a little sanity-checking on how they declared their operator. */ | |
e92cc029 | 12377 | |
5566b478 | 12378 | void |
271e6f02 | 12379 | grok_op_properties (decl, friendp) |
8d08fdba | 12380 | tree decl; |
271e6f02 | 12381 | int friendp; |
8d08fdba MS |
12382 | { |
12383 | tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
596ea4e5 | 12384 | tree argtype; |
8d08fdba MS |
12385 | int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE); |
12386 | tree name = DECL_NAME (decl); | |
596ea4e5 AS |
12387 | enum tree_code operator_code; |
12388 | int arity; | |
12389 | ||
12390 | /* Count the number of arguments. */ | |
12391 | for (argtype = argtypes, arity = 0; | |
12392 | argtype && argtype != void_list_node; | |
12393 | argtype = TREE_CHAIN (argtype)) | |
12394 | ++arity; | |
8d08fdba | 12395 | |
a28e3c7f MS |
12396 | if (current_class_type == NULL_TREE) |
12397 | friendp = 1; | |
8d08fdba | 12398 | |
596ea4e5 AS |
12399 | if (DECL_CONV_FN_P (decl)) |
12400 | operator_code = TYPE_EXPR; | |
12401 | else | |
12402 | do | |
12403 | { | |
0c918ce5 MM |
12404 | #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \ |
12405 | if (ansi_opname (CODE) == name) \ | |
12406 | { \ | |
75ac8dec | 12407 | operator_code = (CODE); \ |
0c918ce5 MM |
12408 | break; \ |
12409 | } \ | |
12410 | else if (ansi_assopname (CODE) == name) \ | |
12411 | { \ | |
75ac8dec | 12412 | operator_code = (CODE); \ |
0c918ce5 MM |
12413 | DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \ |
12414 | break; \ | |
596ea4e5 AS |
12415 | } |
12416 | ||
12417 | #include "operators.def" | |
12418 | #undef DEF_OPERATOR | |
12419 | ||
a98facb0 | 12420 | abort (); |
596ea4e5 AS |
12421 | } |
12422 | while (0); | |
12423 | my_friendly_assert (operator_code != LAST_CPLUS_TREE_CODE, 20000526); | |
12424 | SET_OVERLOADED_OPERATOR_CODE (decl, operator_code); | |
12425 | ||
a28e3c7f MS |
12426 | if (! friendp) |
12427 | { | |
596ea4e5 AS |
12428 | switch (operator_code) |
12429 | { | |
12430 | case CALL_EXPR: | |
12431 | TYPE_OVERLOADS_CALL_EXPR (current_class_type) = 1; | |
12432 | break; | |
5362b086 | 12433 | |
596ea4e5 AS |
12434 | case ARRAY_REF: |
12435 | TYPE_OVERLOADS_ARRAY_REF (current_class_type) = 1; | |
12436 | break; | |
f181d4ae | 12437 | |
596ea4e5 AS |
12438 | case COMPONENT_REF: |
12439 | case MEMBER_REF: | |
12440 | TYPE_OVERLOADS_ARROW (current_class_type) = 1; | |
12441 | break; | |
5362b086 | 12442 | |
596ea4e5 AS |
12443 | case NEW_EXPR: |
12444 | TYPE_HAS_NEW_OPERATOR (current_class_type) = 1; | |
12445 | break; | |
5362b086 | 12446 | |
596ea4e5 AS |
12447 | case DELETE_EXPR: |
12448 | TYPE_GETS_DELETE (current_class_type) |= 1; | |
12449 | break; | |
5362b086 | 12450 | |
596ea4e5 AS |
12451 | case VEC_NEW_EXPR: |
12452 | TYPE_HAS_ARRAY_NEW_OPERATOR (current_class_type) = 1; | |
12453 | break; | |
5362b086 | 12454 | |
596ea4e5 AS |
12455 | case VEC_DELETE_EXPR: |
12456 | TYPE_GETS_DELETE (current_class_type) |= 2; | |
12457 | break; | |
12458 | ||
12459 | default: | |
12460 | break; | |
12461 | } | |
12462 | } | |
12463 | ||
12464 | if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR) | |
8d08fdba | 12465 | { |
8d08fdba MS |
12466 | /* When the compiler encounters the definition of A::operator new, it |
12467 | doesn't look at the class declaration to find out if it's static. */ | |
a28e3c7f | 12468 | if (methodp) |
3afb32a4 | 12469 | revert_static_member_fn (decl); |
68642fb6 | 12470 | |
36791f1e | 12471 | TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl)); |
8d08fdba | 12472 | } |
596ea4e5 | 12473 | else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR) |
8d08fdba | 12474 | { |
a28e3c7f | 12475 | if (methodp) |
3afb32a4 | 12476 | revert_static_member_fn (decl); |
68642fb6 | 12477 | |
36791f1e | 12478 | TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl)); |
8d08fdba | 12479 | } |
8d08fdba MS |
12480 | else |
12481 | { | |
12482 | /* An operator function must either be a non-static member function | |
12483 | or have at least one parameter of a class, a reference to a class, | |
12484 | an enumeration, or a reference to an enumeration. 13.4.0.6 */ | |
700f8a87 | 12485 | if (! methodp || DECL_STATIC_FUNCTION_P (decl)) |
8d08fdba | 12486 | { |
596ea4e5 AS |
12487 | if (operator_code == TYPE_EXPR |
12488 | || operator_code == CALL_EXPR | |
12489 | || operator_code == COMPONENT_REF | |
12490 | || operator_code == ARRAY_REF | |
12491 | || operator_code == NOP_EXPR) | |
33bd39a2 | 12492 | error ("`%D' must be a nonstatic member function", decl); |
8d08fdba MS |
12493 | else |
12494 | { | |
12495 | tree p = argtypes; | |
12496 | ||
700f8a87 | 12497 | if (DECL_STATIC_FUNCTION_P (decl)) |
33bd39a2 | 12498 | error ("`%D' must be either a non-static member function or a non-member function", decl); |
700f8a87 | 12499 | |
8d08fdba | 12500 | if (p) |
a1774733 | 12501 | for (; TREE_CODE (TREE_VALUE (p)) != VOID_TYPE ; p = TREE_CHAIN (p)) |
8d08fdba MS |
12502 | { |
12503 | tree arg = TREE_VALUE (p); | |
12504 | if (TREE_CODE (arg) == REFERENCE_TYPE) | |
12505 | arg = TREE_TYPE (arg); | |
12506 | ||
12507 | /* This lets bad template code slip through. */ | |
12508 | if (IS_AGGR_TYPE (arg) | |
12509 | || TREE_CODE (arg) == ENUMERAL_TYPE | |
73b0fce8 | 12510 | || TREE_CODE (arg) == TEMPLATE_TYPE_PARM |
a1281f45 | 12511 | || TREE_CODE (arg) == BOUND_TEMPLATE_TEMPLATE_PARM) |
8d08fdba MS |
12512 | goto foundaggr; |
12513 | } | |
33bd39a2 | 12514 | error |
8251199e | 12515 | ("`%D' must have an argument of class or enumerated type", |
8d08fdba MS |
12516 | decl); |
12517 | foundaggr: | |
12518 | ; | |
12519 | } | |
12520 | } | |
68642fb6 | 12521 | |
596ea4e5 | 12522 | if (operator_code == CALL_EXPR) |
2c73f9f5 | 12523 | return; /* No restrictions on args. */ |
8d08fdba | 12524 | |
9a3b49ac | 12525 | if (IDENTIFIER_TYPENAME_P (name) && ! DECL_TEMPLATE_INFO (decl)) |
a0a33927 MS |
12526 | { |
12527 | tree t = TREE_TYPE (name); | |
17708e90 | 12528 | if (! friendp) |
a0a33927 MS |
12529 | { |
12530 | int ref = (TREE_CODE (t) == REFERENCE_TYPE); | |
d8e178a0 | 12531 | const char *what = 0; |
5362b086 | 12532 | |
a0a33927 MS |
12533 | if (ref) |
12534 | t = TYPE_MAIN_VARIANT (TREE_TYPE (t)); | |
12535 | ||
17708e90 NS |
12536 | if (TREE_CODE (t) == VOID_TYPE) |
12537 | what = "void"; | |
12538 | else if (t == current_class_type) | |
a0a33927 | 12539 | what = "the same type"; |
9a3b49ac | 12540 | /* Don't force t to be complete here. */ |
a0a33927 | 12541 | else if (IS_AGGR_TYPE (t) |
d0f062fb | 12542 | && COMPLETE_TYPE_P (t) |
a0a33927 MS |
12543 | && DERIVED_FROM_P (t, current_class_type)) |
12544 | what = "a base class"; | |
12545 | ||
12546 | if (what) | |
8251199e | 12547 | warning ("conversion to %s%s will never use a type conversion operator", |
a0a33927 MS |
12548 | ref ? "a reference to " : "", what); |
12549 | } | |
12550 | } | |
271e6f02 | 12551 | if (operator_code == COND_EXPR) |
8d08fdba MS |
12552 | { |
12553 | /* 13.4.0.3 */ | |
33bd39a2 | 12554 | error ("ISO C++ prohibits overloading operator ?:"); |
68642fb6 | 12555 | } |
596ea4e5 | 12556 | else if (ambi_op_p (operator_code)) |
8d08fdba | 12557 | { |
596ea4e5 AS |
12558 | if (arity == 1) |
12559 | /* We pick the one-argument operator codes by default, so | |
12560 | we don't have to change anything. */ | |
12561 | ; | |
12562 | else if (arity == 2) | |
8d08fdba | 12563 | { |
596ea4e5 AS |
12564 | /* If we thought this was a unary operator, we now know |
12565 | it to be a binary operator. */ | |
12566 | switch (operator_code) | |
12567 | { | |
12568 | case INDIRECT_REF: | |
12569 | operator_code = MULT_EXPR; | |
12570 | break; | |
12571 | ||
12572 | case ADDR_EXPR: | |
12573 | operator_code = BIT_AND_EXPR; | |
12574 | break; | |
12575 | ||
12576 | case CONVERT_EXPR: | |
12577 | operator_code = PLUS_EXPR; | |
12578 | break; | |
12579 | ||
12580 | case NEGATE_EXPR: | |
12581 | operator_code = MINUS_EXPR; | |
12582 | break; | |
12583 | ||
12584 | case PREINCREMENT_EXPR: | |
12585 | operator_code = POSTINCREMENT_EXPR; | |
12586 | break; | |
12587 | ||
12588 | case PREDECREMENT_EXPR: | |
655dc6ee | 12589 | operator_code = POSTDECREMENT_EXPR; |
596ea4e5 AS |
12590 | break; |
12591 | ||
12592 | default: | |
a98facb0 | 12593 | abort (); |
596ea4e5 AS |
12594 | } |
12595 | ||
12596 | SET_OVERLOADED_OPERATOR_CODE (decl, operator_code); | |
12597 | ||
12598 | if ((operator_code == POSTINCREMENT_EXPR | |
12599 | || operator_code == POSTDECREMENT_EXPR) | |
5156628f | 12600 | && ! processing_template_decl |
007e5fea | 12601 | && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node)) |
8d08fdba MS |
12602 | { |
12603 | if (methodp) | |
33bd39a2 | 12604 | error ("postfix `%D' must take `int' as its argument", |
8d08fdba MS |
12605 | decl); |
12606 | else | |
33bd39a2 | 12607 | error |
8251199e JM |
12608 | ("postfix `%D' must take `int' as its second argument", |
12609 | decl); | |
8d08fdba MS |
12610 | } |
12611 | } | |
12612 | else | |
12613 | { | |
12614 | if (methodp) | |
33bd39a2 | 12615 | error ("`%D' must take either zero or one argument", decl); |
8d08fdba | 12616 | else |
33bd39a2 | 12617 | error ("`%D' must take either one or two arguments", decl); |
8d08fdba | 12618 | } |
824b9a4c MS |
12619 | |
12620 | /* More Effective C++ rule 6. */ | |
eb448459 | 12621 | if (warn_ecpp |
596ea4e5 AS |
12622 | && (operator_code == POSTINCREMENT_EXPR |
12623 | || operator_code == POSTDECREMENT_EXPR | |
12624 | || operator_code == PREINCREMENT_EXPR | |
12625 | || operator_code == PREDECREMENT_EXPR)) | |
824b9a4c MS |
12626 | { |
12627 | tree arg = TREE_VALUE (argtypes); | |
12628 | tree ret = TREE_TYPE (TREE_TYPE (decl)); | |
12629 | if (methodp || TREE_CODE (arg) == REFERENCE_TYPE) | |
12630 | arg = TREE_TYPE (arg); | |
12631 | arg = TYPE_MAIN_VARIANT (arg); | |
596ea4e5 AS |
12632 | if (operator_code == PREINCREMENT_EXPR |
12633 | || operator_code == PREDECREMENT_EXPR) | |
824b9a4c MS |
12634 | { |
12635 | if (TREE_CODE (ret) != REFERENCE_TYPE | |
3bfdc719 MM |
12636 | || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), |
12637 | arg)) | |
33bd39a2 | 12638 | warning ("prefix `%D' should return `%T'", decl, |
824b9a4c MS |
12639 | build_reference_type (arg)); |
12640 | } | |
12641 | else | |
12642 | { | |
3bfdc719 | 12643 | if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg)) |
33bd39a2 | 12644 | warning ("postfix `%D' should return `%T'", decl, arg); |
824b9a4c MS |
12645 | } |
12646 | } | |
8d08fdba | 12647 | } |
596ea4e5 | 12648 | else if (unary_op_p (operator_code)) |
8d08fdba | 12649 | { |
596ea4e5 | 12650 | if (arity != 1) |
8d08fdba MS |
12651 | { |
12652 | if (methodp) | |
33bd39a2 | 12653 | error ("`%D' must take `void'", decl); |
8d08fdba | 12654 | else |
33bd39a2 | 12655 | error ("`%D' must take exactly one argument", decl); |
8d08fdba MS |
12656 | } |
12657 | } | |
596ea4e5 | 12658 | else /* if (binary_op_p (operator_code)) */ |
8d08fdba | 12659 | { |
596ea4e5 | 12660 | if (arity != 2) |
8d08fdba MS |
12661 | { |
12662 | if (methodp) | |
33bd39a2 | 12663 | error ("`%D' must take exactly one argument", decl); |
8d08fdba | 12664 | else |
33bd39a2 | 12665 | error ("`%D' must take exactly two arguments", decl); |
8d08fdba | 12666 | } |
824b9a4c MS |
12667 | |
12668 | /* More Effective C++ rule 7. */ | |
eb448459 | 12669 | if (warn_ecpp |
596ea4e5 AS |
12670 | && (operator_code == TRUTH_ANDIF_EXPR |
12671 | || operator_code == TRUTH_ORIF_EXPR | |
12672 | || operator_code == COMPOUND_EXPR)) | |
33bd39a2 | 12673 | warning ("user-defined `%D' always evaluates both arguments", |
824b9a4c MS |
12674 | decl); |
12675 | } | |
12676 | ||
12677 | /* Effective C++ rule 23. */ | |
eb448459 | 12678 | if (warn_ecpp |
596ea4e5 AS |
12679 | && arity == 2 |
12680 | && (operator_code == PLUS_EXPR | |
12681 | || operator_code == MINUS_EXPR | |
12682 | || operator_code == TRUNC_DIV_EXPR | |
12683 | || operator_code == MULT_EXPR) | |
824b9a4c | 12684 | && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE) |
33bd39a2 | 12685 | warning ("`%D' should return by value", decl); |
8d08fdba | 12686 | |
271e6f02 | 12687 | /* [over.oper]/8 */ |
34332678 CT |
12688 | for (; argtypes && argtypes != void_list_node; |
12689 | argtypes = TREE_CHAIN (argtypes)) | |
12690 | if (TREE_PURPOSE (argtypes)) | |
12691 | { | |
12692 | TREE_PURPOSE (argtypes) = NULL_TREE; | |
596ea4e5 AS |
12693 | if (operator_code == POSTINCREMENT_EXPR |
12694 | || operator_code == POSTDECREMENT_EXPR) | |
34332678 CT |
12695 | { |
12696 | if (pedantic) | |
33bd39a2 | 12697 | pedwarn ("`%D' cannot have default arguments", decl); |
34332678 CT |
12698 | } |
12699 | else | |
33bd39a2 | 12700 | error ("`%D' cannot have default arguments", decl); |
34332678 CT |
12701 | } |
12702 | ||
8d08fdba MS |
12703 | } |
12704 | } | |
12705 | \f | |
d8e178a0 | 12706 | static const char * |
094fe153 JM |
12707 | tag_name (code) |
12708 | enum tag_types code; | |
12709 | { | |
12710 | switch (code) | |
12711 | { | |
12712 | case record_type: | |
12713 | return "struct"; | |
12714 | case class_type: | |
12715 | return "class"; | |
12716 | case union_type: | |
12717 | return "union "; | |
12718 | case enum_type: | |
12719 | return "enum"; | |
094fe153 | 12720 | default: |
a98facb0 | 12721 | abort (); |
094fe153 JM |
12722 | } |
12723 | } | |
12724 | ||
8d08fdba MS |
12725 | /* Get the struct, enum or union (CODE says which) with tag NAME. |
12726 | Define the tag as a forward-reference if it is not defined. | |
12727 | ||
12728 | C++: If a class derivation is given, process it here, and report | |
12729 | an error if multiple derivation declarations are not identical. | |
12730 | ||
12731 | If this is a definition, come in through xref_tag and only look in | |
12732 | the current frame for the name (since C++ allows new names in any | |
12733 | scope.) */ | |
12734 | ||
8d08fdba | 12735 | tree |
ca107ded | 12736 | xref_tag (code_type_node, name, globalize) |
8d08fdba | 12737 | tree code_type_node; |
ca107ded | 12738 | tree name; |
8d08fdba MS |
12739 | int globalize; |
12740 | { | |
12741 | enum tag_types tag_code; | |
12742 | enum tree_code code; | |
8d08fdba | 12743 | register tree ref, t; |
8f032717 | 12744 | struct binding_level *b = current_binding_level; |
dc8263bc | 12745 | tree attributes = NULL_TREE; |
25aab5d0 | 12746 | tree context = NULL_TREE; |
dc8263bc JM |
12747 | |
12748 | /* If we are called from the parser, code_type_node will sometimes be a | |
12749 | TREE_LIST. This indicates that the user wrote | |
12750 | "class __attribute__ ((foo)) bar". Extract the attributes so we can | |
12751 | use them later. */ | |
12752 | if (TREE_CODE (code_type_node) == TREE_LIST) | |
12753 | { | |
12754 | attributes = TREE_PURPOSE (code_type_node); | |
12755 | code_type_node = TREE_VALUE (code_type_node); | |
12756 | } | |
8d08fdba | 12757 | |
665f2503 | 12758 | tag_code = (enum tag_types) tree_low_cst (code_type_node, 1); |
8d08fdba MS |
12759 | switch (tag_code) |
12760 | { | |
12761 | case record_type: | |
12762 | case class_type: | |
8d08fdba | 12763 | code = RECORD_TYPE; |
8d08fdba MS |
12764 | break; |
12765 | case union_type: | |
12766 | code = UNION_TYPE; | |
8d08fdba MS |
12767 | break; |
12768 | case enum_type: | |
12769 | code = ENUMERAL_TYPE; | |
12770 | break; | |
12771 | default: | |
a98facb0 | 12772 | abort (); |
8d08fdba MS |
12773 | } |
12774 | ||
12775 | /* If a cross reference is requested, look up the type | |
12776 | already defined for this tag and return it. */ | |
2f939d94 | 12777 | if (TYPE_P (name)) |
be99da77 MS |
12778 | { |
12779 | t = name; | |
a80e4195 | 12780 | name = TYPE_IDENTIFIER (t); |
be99da77 MS |
12781 | } |
12782 | else | |
12783 | t = IDENTIFIER_TYPE_VALUE (name); | |
61a127b3 | 12784 | |
f2ae0c45 JM |
12785 | /* Warn about 'friend struct Inherited;' doing the wrong thing. */ |
12786 | if (t && globalize && TREE_CODE (t) == TYPENAME_TYPE) | |
12787 | { | |
12788 | static int explained; | |
2b0cbc5d NS |
12789 | tree shadowed; |
12790 | ||
33bd39a2 | 12791 | warning ("`%s %T' declares a new type at namespace scope", |
2b0cbc5d NS |
12792 | tag_name (tag_code), name); |
12793 | if (!explained++) | |
33bd39a2 | 12794 | 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 |
12795 | tag_name (tag_code), |
12796 | constructor_name (current_class_type), | |
12797 | TYPE_IDENTIFIER (t)); | |
12798 | ||
12799 | /* We need to remove the class scope binding for the | |
12800 | TYPENAME_TYPE as otherwise poplevel_class gets confused. */ | |
12801 | for (shadowed = b->class_shadowed; | |
12802 | shadowed; | |
12803 | shadowed = TREE_CHAIN (shadowed)) | |
12804 | if (TREE_TYPE (shadowed) == TYPE_NAME (t)) | |
12805 | { | |
12806 | TREE_PURPOSE (shadowed) = NULL_TREE; | |
12807 | break; | |
12808 | } | |
f2ae0c45 JM |
12809 | } |
12810 | ||
73b0fce8 | 12811 | if (t && TREE_CODE (t) != code && TREE_CODE (t) != TEMPLATE_TYPE_PARM |
a1281f45 | 12812 | && TREE_CODE (t) != BOUND_TEMPLATE_TEMPLATE_PARM) |
a0a33927 MS |
12813 | t = NULL_TREE; |
12814 | ||
8ccc31eb | 12815 | if (! globalize) |
8d08fdba | 12816 | { |
f3400fe2 JM |
12817 | /* If we know we are defining this tag, only look it up in |
12818 | this scope and don't try to find it as a type. */ | |
12819 | ref = lookup_tag (code, name, b, 1); | |
8d08fdba MS |
12820 | } |
12821 | else | |
12822 | { | |
25aab5d0 | 12823 | if (t) |
36a117a5 | 12824 | { |
4782bd5b RS |
12825 | ref = follow_tag_typedef (t); |
12826 | ||
25aab5d0 MM |
12827 | /* [dcl.type.elab] If the identifier resolves to a |
12828 | typedef-name or a template type-parameter, the | |
12829 | elaborated-type-specifier is ill-formed. */ | |
4782bd5b RS |
12830 | if (!ref) |
12831 | { | |
12832 | pedwarn ("using typedef-name `%D' after `%s'", | |
12833 | TYPE_NAME (t), tag_name (tag_code)); | |
12834 | ref = t; | |
12835 | } | |
25aab5d0 | 12836 | else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM) |
33bd39a2 | 12837 | error ("using template type parameter `%T' after `%s'", |
4782bd5b | 12838 | t, tag_name (tag_code)); |
25aab5d0 MM |
12839 | } |
12840 | else | |
12841 | ref = lookup_tag (code, name, b, 0); | |
68642fb6 | 12842 | |
25aab5d0 MM |
12843 | if (! ref) |
12844 | { | |
12845 | /* Try finding it as a type declaration. If that wins, | |
68642fb6 | 12846 | use it. */ |
25aab5d0 MM |
12847 | ref = lookup_name (name, 1); |
12848 | ||
12849 | if (ref != NULL_TREE | |
12850 | && processing_template_decl | |
12851 | && DECL_CLASS_TEMPLATE_P (ref) | |
12852 | && template_class_depth (current_class_type) == 0) | |
12853 | /* Since GLOBALIZE is true, we're declaring a global | |
6757edfe | 12854 | template, so we want this type. */ |
17aec3eb | 12855 | ref = DECL_TEMPLATE_RESULT (ref); |
6757edfe | 12856 | |
25aab5d0 MM |
12857 | if (ref && TREE_CODE (ref) == TYPE_DECL |
12858 | && TREE_CODE (TREE_TYPE (ref)) == code) | |
12859 | ref = TREE_TYPE (ref); | |
12860 | else | |
12861 | ref = NULL_TREE; | |
12862 | } | |
12863 | ||
68642fb6 UD |
12864 | if (ref && current_class_type |
12865 | && template_class_depth (current_class_type) | |
12866 | && PROCESSING_REAL_TEMPLATE_DECL_P ()) | |
25aab5d0 MM |
12867 | { |
12868 | /* Since GLOBALIZE is non-zero, we are not looking at a | |
12869 | definition of this tag. Since, in addition, we are currently | |
12870 | processing a (member) template declaration of a template | |
12871 | class, we must be very careful; consider: | |
12872 | ||
12873 | template <class X> | |
12874 | struct S1 | |
12875 | ||
12876 | template <class U> | |
12877 | struct S2 | |
12878 | { template <class V> | |
12879 | friend struct S1; }; | |
12880 | ||
12881 | Here, the S2::S1 declaration should not be confused with the | |
12882 | outer declaration. In particular, the inner version should | |
12883 | have a template parameter of level 2, not level 1. This | |
12884 | would be particularly important if the member declaration | |
12885 | were instead: | |
12886 | ||
12887 | template <class V = U> friend struct S1; | |
12888 | ||
12889 | say, when we should tsubst into `U' when instantiating | |
12890 | S2. On the other hand, when presented with: | |
12891 | ||
12892 | template <class T> | |
12893 | struct S1 { | |
12894 | template <class U> | |
12895 | struct S2 {}; | |
12896 | template <class U> | |
12897 | friend struct S2; | |
12898 | }; | |
12899 | ||
12900 | we must find the inner binding eventually. We | |
12901 | accomplish this by making sure that the new type we | |
12902 | create to represent this declaration has the right | |
12903 | TYPE_CONTEXT. */ | |
12904 | context = TYPE_CONTEXT (ref); | |
12905 | ref = NULL_TREE; | |
8d08fdba MS |
12906 | } |
12907 | } | |
12908 | ||
8d08fdba MS |
12909 | if (! ref) |
12910 | { | |
12911 | /* If no such tag is yet defined, create a forward-reference node | |
12912 | and record it as the "definition". | |
12913 | When a real declaration of this type is found, | |
12914 | the forward-reference will be altered into a real type. */ | |
8d08fdba MS |
12915 | if (code == ENUMERAL_TYPE) |
12916 | { | |
33bd39a2 | 12917 | error ("use of enum `%#D' without previous declaration", name); |
fc378698 | 12918 | |
8d08fdba MS |
12919 | ref = make_node (ENUMERAL_TYPE); |
12920 | ||
12921 | /* Give the type a default layout like unsigned int | |
12922 | to avoid crashing if it does not get defined. */ | |
12923 | TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node); | |
12924 | TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node); | |
11cf4d18 | 12925 | TYPE_USER_ALIGN (ref) = 0; |
8d08fdba MS |
12926 | TREE_UNSIGNED (ref) = 1; |
12927 | TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node); | |
12928 | TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node); | |
12929 | TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node); | |
12930 | ||
12931 | /* Enable us to recognize when a type is created in class context. | |
12932 | To do nested classes correctly, this should probably be cleared | |
12933 | out when we leave this classes scope. Currently this in only | |
12934 | done in `start_enum'. */ | |
12935 | ||
12936 | pushtag (name, ref, globalize); | |
8d08fdba | 12937 | } |
8d08fdba MS |
12938 | else |
12939 | { | |
8d08fdba | 12940 | struct binding_level *old_b = class_binding_level; |
8d08fdba | 12941 | |
33848bb0 | 12942 | ref = make_aggr_type (code); |
25aab5d0 | 12943 | TYPE_CONTEXT (ref) = context; |
8d08fdba | 12944 | |
8d08fdba MS |
12945 | #ifdef NONNESTED_CLASSES |
12946 | /* Class types don't nest the way enums do. */ | |
12947 | class_binding_level = (struct binding_level *)0; | |
12948 | #endif | |
12949 | pushtag (name, ref, globalize); | |
12950 | class_binding_level = old_b; | |
8d08fdba MS |
12951 | } |
12952 | } | |
12953 | else | |
12954 | { | |
7fe6899f | 12955 | if (!globalize && processing_template_decl && IS_AGGR_TYPE (ref)) |
6757edfe | 12956 | redeclare_class_template (ref, current_template_parms); |
8d08fdba MS |
12957 | } |
12958 | ||
a588fe25 | 12959 | TYPE_ATTRIBUTES (ref) = attributes; |
dc8263bc | 12960 | |
8d08fdba MS |
12961 | return ref; |
12962 | } | |
8ccc31eb | 12963 | |
fc378698 MS |
12964 | tree |
12965 | xref_tag_from_type (old, id, globalize) | |
12966 | tree old, id; | |
12967 | int globalize; | |
12968 | { | |
12969 | tree code_type_node; | |
12970 | ||
12971 | if (TREE_CODE (old) == RECORD_TYPE) | |
12972 | code_type_node = (CLASSTYPE_DECLARED_CLASS (old) | |
12973 | ? class_type_node : record_type_node); | |
12974 | else | |
12975 | code_type_node = union_type_node; | |
12976 | ||
12977 | if (id == NULL_TREE) | |
12978 | id = TYPE_IDENTIFIER (old); | |
12979 | ||
ca107ded | 12980 | return xref_tag (code_type_node, id, globalize); |
fc378698 MS |
12981 | } |
12982 | ||
3fd71a52 MM |
12983 | /* REF is a type (named NAME), for which we have just seen some |
12984 | baseclasses. BINFO is a list of those baseclasses; the | |
12985 | TREE_PURPOSE is an access_* node, and the TREE_VALUE is the type of | |
12986 | the base-class. CODE_TYPE_NODE indicates whether REF is a class, | |
12987 | struct, or union. */ | |
12988 | ||
8ccc31eb MS |
12989 | void |
12990 | xref_basetypes (code_type_node, name, ref, binfo) | |
12991 | tree code_type_node; | |
9780c24f JM |
12992 | tree name ATTRIBUTE_UNUSED; |
12993 | tree ref; | |
8ccc31eb MS |
12994 | tree binfo; |
12995 | { | |
12996 | /* In the declaration `A : X, Y, ... Z' we mark all the types | |
12997 | (A, X, Y, ..., Z) so we can check for duplicates. */ | |
12998 | tree binfos; | |
d6479fe7 MM |
12999 | tree base; |
13000 | ||
8ccc31eb | 13001 | int i, len; |
591382c4 | 13002 | enum tag_types tag_code; |
a5d7c4a3 MK |
13003 | |
13004 | /* If we are called from the parser, code_type_node will sometimes be a | |
13005 | TREE_LIST. This indicates that the user wrote | |
13006 | "class __attribute__ ((foo)) bar". Extract the attributes so that | |
13007 | tree_low_cst doesn't crash. */ | |
13008 | if (TREE_CODE (code_type_node) == TREE_LIST) | |
13009 | code_type_node = TREE_VALUE (code_type_node); | |
13010 | ||
591382c4 | 13011 | tag_code = (enum tag_types) tree_low_cst (code_type_node, 1); |
8ccc31eb MS |
13012 | |
13013 | if (tag_code == union_type) | |
13014 | { | |
33bd39a2 | 13015 | error ("derived union `%T' invalid", ref); |
8ccc31eb MS |
13016 | return; |
13017 | } | |
13018 | ||
13019 | len = list_length (binfo); | |
8ccc31eb | 13020 | |
d6479fe7 MM |
13021 | /* First, make sure that any templates in base-classes are |
13022 | instantiated. This ensures that if we call ourselves recursively | |
13023 | we do not get confused about which classes are marked and which | |
13024 | are not. */ | |
13025 | for (base = binfo; base; base = TREE_CHAIN (base)) | |
13026 | complete_type (TREE_VALUE (base)); | |
13027 | ||
8ccc31eb MS |
13028 | SET_CLASSTYPE_MARKED (ref); |
13029 | BINFO_BASETYPES (TYPE_BINFO (ref)) = binfos = make_tree_vec (len); | |
13030 | ||
13031 | for (i = 0; binfo; binfo = TREE_CHAIN (binfo)) | |
13032 | { | |
13033 | /* The base of a derived struct is public by default. */ | |
13034 | int via_public | |
be99da77 MS |
13035 | = (TREE_PURPOSE (binfo) == access_public_node |
13036 | || TREE_PURPOSE (binfo) == access_public_virtual_node | |
8ccc31eb | 13037 | || (tag_code != class_type |
be99da77 MS |
13038 | && (TREE_PURPOSE (binfo) == access_default_node |
13039 | || TREE_PURPOSE (binfo) == access_default_virtual_node))); | |
d8b55a76 JM |
13040 | int via_protected |
13041 | = (TREE_PURPOSE (binfo) == access_protected_node | |
13042 | || TREE_PURPOSE (binfo) == access_protected_virtual_node); | |
8ccc31eb | 13043 | int via_virtual |
be99da77 | 13044 | = (TREE_PURPOSE (binfo) == access_private_virtual_node |
d8b55a76 | 13045 | || TREE_PURPOSE (binfo) == access_protected_virtual_node |
be99da77 MS |
13046 | || TREE_PURPOSE (binfo) == access_public_virtual_node |
13047 | || TREE_PURPOSE (binfo) == access_default_virtual_node); | |
13048 | tree basetype = TREE_VALUE (binfo); | |
8ccc31eb MS |
13049 | tree base_binfo; |
13050 | ||
8ccc31eb MS |
13051 | if (basetype && TREE_CODE (basetype) == TYPE_DECL) |
13052 | basetype = TREE_TYPE (basetype); | |
5566b478 MS |
13053 | if (!basetype |
13054 | || (TREE_CODE (basetype) != RECORD_TYPE | |
a80e4195 | 13055 | && TREE_CODE (basetype) != TYPENAME_TYPE |
73b0fce8 | 13056 | && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM |
a1281f45 | 13057 | && TREE_CODE (basetype) != BOUND_TEMPLATE_TEMPLATE_PARM)) |
8ccc31eb | 13058 | { |
33bd39a2 | 13059 | error ("base type `%T' fails to be a struct or class type", |
8ccc31eb MS |
13060 | TREE_VALUE (binfo)); |
13061 | continue; | |
13062 | } | |
2b9dc906 | 13063 | |
8adf5b5e JM |
13064 | /* This code replaces similar code in layout_basetypes. |
13065 | We put the complete_type first for implicit `typename'. */ | |
d0f062fb | 13066 | if (!COMPLETE_TYPE_P (basetype) |
2b9dc906 | 13067 | && ! (current_template_parms && uses_template_parms (basetype))) |
8ccc31eb | 13068 | { |
33bd39a2 | 13069 | error ("base class `%T' has incomplete type", basetype); |
8ccc31eb MS |
13070 | continue; |
13071 | } | |
8ccc31eb MS |
13072 | else |
13073 | { | |
13074 | if (CLASSTYPE_MARKED (basetype)) | |
13075 | { | |
13076 | if (basetype == ref) | |
33bd39a2 | 13077 | error ("recursive type `%T' undefined", basetype); |
8ccc31eb | 13078 | else |
33bd39a2 | 13079 | error ("duplicate base type `%T' invalid", basetype); |
8ccc31eb MS |
13080 | continue; |
13081 | } | |
13082 | ||
eff71ab0 | 13083 | if (TYPE_FOR_JAVA (basetype) |
46ccf50a | 13084 | && (current_lang_depth () == 0)) |
eff71ab0 PB |
13085 | TYPE_FOR_JAVA (ref) = 1; |
13086 | ||
8ccc31eb MS |
13087 | /* Note that the BINFO records which describe individual |
13088 | inheritances are *not* shared in the lattice! They | |
13089 | cannot be shared because a given baseclass may be | |
13090 | inherited with different `accessibility' by different | |
13091 | derived classes. (Each BINFO record describing an | |
13092 | individual inheritance contains flags which say what | |
13093 | the `accessibility' of that particular inheritance is.) */ | |
68642fb6 UD |
13094 | |
13095 | base_binfo | |
fed3cef0 | 13096 | = make_binfo (size_zero_node, basetype, |
7ddedda4 MM |
13097 | CLASS_TYPE_P (basetype) |
13098 | ? TYPE_BINFO_VTABLE (basetype) : NULL_TREE, | |
13099 | CLASS_TYPE_P (basetype) | |
13100 | ? TYPE_BINFO_VIRTUALS (basetype) : NULL_TREE); | |
68642fb6 | 13101 | |
8ccc31eb MS |
13102 | TREE_VEC_ELT (binfos, i) = base_binfo; |
13103 | TREE_VIA_PUBLIC (base_binfo) = via_public; | |
13104 | TREE_VIA_PROTECTED (base_binfo) = via_protected; | |
13105 | TREE_VIA_VIRTUAL (base_binfo) = via_virtual; | |
13106 | BINFO_INHERITANCE_CHAIN (base_binfo) = TYPE_BINFO (ref); | |
13107 | ||
dfbcd65a JM |
13108 | /* We need to unshare the binfos now so that lookups during class |
13109 | definition work. */ | |
13110 | unshare_base_binfos (base_binfo); | |
13111 | ||
8ccc31eb | 13112 | SET_CLASSTYPE_MARKED (basetype); |
9e9ff709 | 13113 | |
8ccc31eb MS |
13114 | /* We are free to modify these bits because they are meaningless |
13115 | at top level, and BASETYPE is a top-level type. */ | |
13116 | if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype)) | |
13117 | { | |
13118 | TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1; | |
4c6b7393 MM |
13119 | /* Converting to a virtual base class requires looking |
13120 | up the offset of the virtual base. */ | |
13121 | TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1; | |
8ccc31eb MS |
13122 | } |
13123 | ||
7ddedda4 MM |
13124 | if (CLASS_TYPE_P (basetype)) |
13125 | { | |
5362b086 | 13126 | TYPE_HAS_NEW_OPERATOR (ref) |
834c6dff | 13127 | |= TYPE_HAS_NEW_OPERATOR (basetype); |
5362b086 | 13128 | TYPE_HAS_ARRAY_NEW_OPERATOR (ref) |
834c6dff | 13129 | |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype); |
7ddedda4 | 13130 | TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype); |
4c6b7393 | 13131 | /* If the base-class uses multiple inheritance, so do we. */ |
68642fb6 | 13132 | TYPE_USES_MULTIPLE_INHERITANCE (ref) |
4c6b7393 MM |
13133 | |= TYPE_USES_MULTIPLE_INHERITANCE (basetype); |
13134 | /* Likewise, if converting to a base of the base may require | |
13135 | code, then we may need to generate code to convert to a | |
13136 | base as well. */ | |
68642fb6 | 13137 | TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) |
4c6b7393 | 13138 | |= TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype); |
7ddedda4 MM |
13139 | } |
13140 | ||
8ccc31eb MS |
13141 | i += 1; |
13142 | } | |
13143 | } | |
13144 | if (i) | |
13145 | TREE_VEC_LENGTH (binfos) = i; | |
13146 | else | |
13147 | BINFO_BASETYPES (TYPE_BINFO (ref)) = NULL_TREE; | |
13148 | ||
13149 | if (i > 1) | |
7ddedda4 | 13150 | { |
4c6b7393 MM |
13151 | TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1; |
13152 | /* If there is more than one non-empty they cannot be at the same | |
13153 | address. */ | |
13154 | TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1; | |
7ddedda4 MM |
13155 | } |
13156 | ||
8ccc31eb MS |
13157 | /* Unmark all the types. */ |
13158 | while (--i >= 0) | |
13159 | CLEAR_CLASSTYPE_MARKED (BINFO_TYPE (TREE_VEC_ELT (binfos, i))); | |
13160 | CLEAR_CLASSTYPE_MARKED (ref); | |
13161 | ||
70c532b5 MM |
13162 | /* Now that we know all the base-classes, set up the list of virtual |
13163 | bases. */ | |
23381155 | 13164 | get_vbase_types (ref); |
8ccc31eb | 13165 | } |
68642fb6 | 13166 | |
8d08fdba | 13167 | \f |
8d08fdba MS |
13168 | /* Begin compiling the definition of an enumeration type. |
13169 | NAME is its name (or null if anonymous). | |
13170 | Returns the type object, as yet incomplete. | |
13171 | Also records info about it so that build_enumerator | |
13172 | may be used to declare the individual values as they are read. */ | |
13173 | ||
13174 | tree | |
13175 | start_enum (name) | |
13176 | tree name; | |
13177 | { | |
13178 | register tree enumtype = NULL_TREE; | |
8f032717 | 13179 | struct binding_level *b = current_binding_level; |
8d08fdba MS |
13180 | |
13181 | /* If this is the real definition for a previous forward reference, | |
13182 | fill in the contents in the same object that used to be the | |
13183 | forward reference. */ | |
13184 | ||
13185 | if (name != NULL_TREE) | |
13186 | enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1); | |
13187 | ||
13188 | if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE) | |
30ff8252 | 13189 | { |
33bd39a2 | 13190 | error ("multiple definition of `%#T'", enumtype); |
30ff8252 | 13191 | cp_error_at ("previous definition here", enumtype); |
58595203 MM |
13192 | /* Clear out TYPE_VALUES, and start again. */ |
13193 | TYPE_VALUES (enumtype) = NULL_TREE; | |
30ff8252 | 13194 | } |
8d08fdba MS |
13195 | else |
13196 | { | |
13197 | enumtype = make_node (ENUMERAL_TYPE); | |
13198 | pushtag (name, enumtype, 0); | |
13199 | } | |
13200 | ||
8d08fdba MS |
13201 | return enumtype; |
13202 | } | |
13203 | ||
13204 | /* After processing and defining all the values of an enumeration type, | |
13205 | install their decls in the enumeration type and finish it off. | |
968b956a | 13206 | ENUMTYPE is the type object and VALUES a list of name-value pairs. */ |
8d08fdba | 13207 | |
968b956a | 13208 | void |
219670f1 | 13209 | finish_enum (enumtype) |
dbfe2124 | 13210 | tree enumtype; |
8d08fdba | 13211 | { |
968b956a MM |
13212 | tree pair; |
13213 | tree minnode; | |
13214 | tree maxnode; | |
13215 | tree t; | |
13216 | bool unsignedp; | |
13217 | int lowprec; | |
13218 | int highprec; | |
13219 | int precision; | |
13220 | ||
13221 | /* We built up the VALUES in reverse order. */ | |
13222 | TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype)); | |
13223 | ||
13224 | /* [dcl.enum] | |
13225 | ||
13226 | Following the closing brace of an enum-specifier, each | |
13227 | enumerator has the type of its enumeration. Prior to the | |
13228 | closing brace, the type of each enumerator is the type of | |
13229 | its initializing value. */ | |
13230 | for (pair = TYPE_VALUES (enumtype); pair; pair = TREE_CHAIN (pair)) | |
13231 | TREE_TYPE (TREE_VALUE (pair)) = enumtype; | |
13232 | ||
13233 | /* For a enum defined in a template, all further processing is | |
13234 | postponed until the template is instantiated. */ | |
13235 | if (processing_template_decl) | |
13236 | { | |
13237 | tree scope = current_scope (); | |
13238 | if (scope && TREE_CODE (scope) == FUNCTION_DECL) | |
13239 | add_stmt (build_min (TAG_DEFN, enumtype)); | |
8d08fdba | 13240 | |
0ba8a114 | 13241 | |
968b956a MM |
13242 | return; |
13243 | } | |
13244 | ||
13245 | /* Figure out what the minimum and maximum values of the enumerators | |
13246 | are. */ | |
13247 | if (TYPE_VALUES (enumtype)) | |
8d08fdba | 13248 | { |
968b956a | 13249 | minnode = maxnode = NULL_TREE; |
5566b478 | 13250 | |
968b956a MM |
13251 | for (pair = TYPE_VALUES (enumtype); |
13252 | pair; | |
13253 | pair = TREE_CHAIN (pair)) | |
8d08fdba | 13254 | { |
ed44da02 MM |
13255 | tree value; |
13256 | ||
968b956a | 13257 | value = DECL_INITIAL (TREE_VALUE (pair)); |
ed44da02 | 13258 | |
968b956a MM |
13259 | if (!minnode) |
13260 | minnode = maxnode = value; | |
13261 | else if (tree_int_cst_lt (maxnode, value)) | |
13262 | maxnode = value; | |
13263 | else if (tree_int_cst_lt (value, minnode)) | |
13264 | minnode = value; | |
8d08fdba MS |
13265 | } |
13266 | } | |
f376e137 | 13267 | else |
968b956a MM |
13268 | minnode = maxnode = integer_zero_node; |
13269 | ||
13270 | /* Compute the number of bits require to represent all values of the | |
13271 | enumeration. We must do this before the type of MINNODE and | |
13272 | MAXNODE are transformed, since min_precision relies on the | |
13273 | TREE_TYPE of the value it is passed. */ | |
13274 | unsignedp = tree_int_cst_sgn (minnode) >= 0; | |
13275 | lowprec = min_precision (minnode, unsignedp); | |
13276 | highprec = min_precision (maxnode, unsignedp); | |
13277 | precision = MAX (lowprec, highprec); | |
13278 | ||
13279 | /* Set the TREE_TYPE for the values as well. That's so that when we | |
13280 | call decl_constant_value we get an entity of the right type (but | |
13281 | with the constant value). In addition, transform the TYPE_VALUES | |
13282 | list to contain the values, rather than the CONST_DECLs for them. */ | |
13283 | for (pair = TYPE_VALUES (enumtype); pair; pair = TREE_CHAIN (pair)) | |
13284 | { | |
13285 | tree value = DECL_INITIAL (TREE_VALUE (pair)); | |
13286 | ||
13287 | TREE_TYPE (value) = enumtype; | |
13288 | TREE_VALUE (pair) = value; | |
13289 | } | |
13290 | ||
13291 | /* Set TYPE_MIN_VALUE and TYPE_MAX_VALUE according to `precision'. */ | |
13292 | TYPE_SIZE (enumtype) = NULL_TREE; | |
13293 | TYPE_PRECISION (enumtype) = precision; | |
13294 | if (unsignedp) | |
13295 | fixup_unsigned_type (enumtype); | |
cbf882af | 13296 | else |
968b956a | 13297 | fixup_signed_type (enumtype); |
8d08fdba | 13298 | |
219670f1 BE |
13299 | if (flag_short_enums || (precision > TYPE_PRECISION (integer_type_node))) |
13300 | /* Use the width of the narrowest normal C type which is wide | |
13301 | enough. */ | |
b0c48229 | 13302 | TYPE_PRECISION (enumtype) = TYPE_PRECISION (c_common_type_for_size |
219670f1 | 13303 | (precision, 1)); |
968b956a MM |
13304 | else |
13305 | TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node); | |
68642fb6 | 13306 | |
968b956a MM |
13307 | TYPE_SIZE (enumtype) = NULL_TREE; |
13308 | layout_type (enumtype); | |
8d08fdba | 13309 | |
968b956a MM |
13310 | /* Fix up all variant types of this enum type. */ |
13311 | for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t)) | |
13312 | { | |
13313 | TYPE_VALUES (t) = TYPE_VALUES (enumtype); | |
13314 | TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (enumtype); | |
13315 | TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (enumtype); | |
13316 | TYPE_SIZE (t) = TYPE_SIZE (enumtype); | |
13317 | TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (enumtype); | |
13318 | TYPE_MODE (t) = TYPE_MODE (enumtype); | |
13319 | TYPE_PRECISION (t) = TYPE_PRECISION (enumtype); | |
13320 | TYPE_ALIGN (t) = TYPE_ALIGN (enumtype); | |
13321 | TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (enumtype); | |
13322 | TREE_UNSIGNED (t) = TREE_UNSIGNED (enumtype); | |
cbf882af MM |
13323 | } |
13324 | ||
968b956a MM |
13325 | /* Finish debugging output for this type. */ |
13326 | rest_of_type_compilation (enumtype, namespace_bindings_p ()); | |
8d08fdba MS |
13327 | } |
13328 | ||
079e1098 | 13329 | /* Build and install a CONST_DECL for an enumeration constant of the |
58595203 | 13330 | enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided. |
8d08fdba MS |
13331 | Assignment of sequential values by default is handled here. */ |
13332 | ||
58595203 MM |
13333 | void |
13334 | build_enumerator (name, value, enumtype) | |
079e1098 MM |
13335 | tree name; |
13336 | tree value; | |
58595203 | 13337 | tree enumtype; |
8d08fdba | 13338 | { |
58595203 | 13339 | tree decl; |
e8bd800e | 13340 | tree context; |
58595203 MM |
13341 | tree type; |
13342 | tree values; | |
8d08fdba MS |
13343 | |
13344 | /* Remove no-op casts from the value. */ | |
13345 | if (value) | |
13346 | STRIP_TYPE_NOPS (value); | |
13347 | ||
58595203 MM |
13348 | if (! processing_template_decl) |
13349 | { | |
13350 | /* Validate and default VALUE. */ | |
13351 | if (value != NULL_TREE) | |
13352 | { | |
fc611ce0 | 13353 | value = decl_constant_value (value); |
58595203 MM |
13354 | |
13355 | if (TREE_CODE (value) == INTEGER_CST) | |
13356 | { | |
13357 | value = default_conversion (value); | |
13358 | constant_expression_warning (value); | |
13359 | } | |
13360 | else | |
13361 | { | |
33bd39a2 | 13362 | error ("enumerator value for `%D' not integer constant", name); |
58595203 MM |
13363 | value = NULL_TREE; |
13364 | } | |
13365 | } | |
13366 | ||
13367 | /* Default based on previous value. */ | |
13368 | if (value == NULL_TREE && ! processing_template_decl) | |
13369 | { | |
13370 | tree prev_value; | |
13371 | ||
13372 | if (TYPE_VALUES (enumtype)) | |
13373 | { | |
13374 | /* The next value is the previous value ... */ | |
13375 | prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype))); | |
13376 | /* ... plus one. */ | |
ab76ca54 MM |
13377 | value = cp_build_binary_op (PLUS_EXPR, |
13378 | prev_value, | |
13379 | integer_one_node); | |
68642fb6 | 13380 | |
58595203 | 13381 | if (tree_int_cst_lt (value, prev_value)) |
33bd39a2 | 13382 | error ("overflow in enumeration values at `%D'", name); |
58595203 MM |
13383 | } |
13384 | else | |
13385 | value = integer_zero_node; | |
13386 | } | |
13387 | ||
13388 | /* Remove no-op casts from the value. */ | |
13389 | if (value) | |
13390 | STRIP_TYPE_NOPS (value); | |
013bc8af | 13391 | #if 0 |
58595203 MM |
13392 | /* To fix MAX_VAL enum consts. (bkoz) */ |
13393 | TREE_TYPE (value) = integer_type_node; | |
013bc8af | 13394 | #endif |
58595203 | 13395 | } |
8d08fdba | 13396 | |
58595203 MM |
13397 | /* We always have to copy here; not all INTEGER_CSTs are unshared. |
13398 | Even in other cases, we will later (in finish_enum) be setting | |
13399 | the type of VALUE. But, we don't need to make a copy if this | |
13400 | VALUE is one of the enumeration constants for this same | |
13401 | enumeration type. */ | |
13402 | for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values)) | |
13403 | if (TREE_VALUE (values) == value) | |
13404 | break; | |
13405 | /* If we didn't break out of the loop, then we do need a copy. */ | |
13406 | if (!values && value) | |
13407 | value = copy_node (value); | |
ed44da02 | 13408 | |
8d08fdba | 13409 | /* C++ associates enums with global, function, or class declarations. */ |
58595203 MM |
13410 | context = current_scope (); |
13411 | ||
13412 | /* Build the actual enumeration constant. Note that the enumeration | |
13413 | constants have the type of their initializers until the | |
13414 | enumeration is complete: | |
13415 | ||
13416 | [ dcl.enum ] | |
13417 | ||
13418 | Following the closing brace of an enum-specifier, each enumer- | |
13419 | ator has the type of its enumeration. Prior to the closing | |
13420 | brace, the type of each enumerator is the type of its | |
13421 | initializing value. | |
13422 | ||
13423 | In finish_enum we will reset the type. Of course, if we're | |
13424 | processing a template, there may be no value. */ | |
13425 | type = value ? TREE_TYPE (value) : NULL_TREE; | |
13426 | ||
13427 | if (context && context == current_class_type) | |
13428 | /* This enum declaration is local to the class. We need the full | |
8f17b5c5 | 13429 | lang_decl so that we can record DECL_CLASS_CONTEXT, for example. */ |
58595203 MM |
13430 | decl = build_lang_decl (CONST_DECL, name, type); |
13431 | else | |
13432 | /* It's a global enum, or it's local to a function. (Note local to | |
e8bd800e | 13433 | a function could mean local to a class method. */ |
58595203 | 13434 | decl = build_decl (CONST_DECL, name, type); |
e8bd800e | 13435 | |
58595203 MM |
13436 | DECL_CONTEXT (decl) = FROB_CONTEXT (context); |
13437 | DECL_INITIAL (decl) = value; | |
13438 | TREE_READONLY (decl) = 1; | |
e8bd800e | 13439 | |
58595203 MM |
13440 | if (context && context == current_class_type) |
13441 | /* In something like `struct S { enum E { i = 7 }; };' we put `i' | |
8f032717 MM |
13442 | on the TYPE_FIELDS list for `S'. (That's so that you can say |
13443 | things like `S::i' later.) */ | |
58595203 MM |
13444 | finish_member_declaration (decl); |
13445 | else | |
9780c24f | 13446 | pushdecl (decl); |
58595203 MM |
13447 | |
13448 | /* Add this enumeration constant to the list for this type. */ | |
13449 | TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype)); | |
8d08fdba MS |
13450 | } |
13451 | ||
8d08fdba | 13452 | \f |
a8f73d4b MM |
13453 | /* We're defining DECL. Make sure that it's type is OK. */ |
13454 | ||
13455 | static void | |
f444e36b | 13456 | check_function_type (decl, current_function_parms) |
a8f73d4b | 13457 | tree decl; |
f444e36b | 13458 | tree current_function_parms; |
a8f73d4b MM |
13459 | { |
13460 | tree fntype = TREE_TYPE (decl); | |
d0f062fb | 13461 | tree return_type = complete_type (TREE_TYPE (fntype)); |
a8f73d4b MM |
13462 | |
13463 | /* In a function definition, arg types must be complete. */ | |
13464 | require_complete_types_for_parms (current_function_parms); | |
13465 | ||
d0f062fb | 13466 | if (!COMPLETE_OR_VOID_TYPE_P (return_type)) |
a8f73d4b | 13467 | { |
33bd39a2 | 13468 | error ("return type `%#T' is incomplete", TREE_TYPE (fntype)); |
a8f73d4b MM |
13469 | |
13470 | /* Make it return void instead, but don't change the | |
13471 | type of the DECL_RESULT, in case we have a named return value. */ | |
13472 | if (TREE_CODE (fntype) == METHOD_TYPE) | |
13473 | { | |
13474 | tree ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype))); | |
13475 | TREE_TYPE (decl) | |
13476 | = build_cplus_method_type (ctype, | |
13477 | void_type_node, | |
13478 | FUNCTION_ARG_CHAIN (decl)); | |
13479 | } | |
13480 | else | |
13481 | TREE_TYPE (decl) | |
13482 | = build_function_type (void_type_node, | |
13483 | TYPE_ARG_TYPES (TREE_TYPE (decl))); | |
68642fb6 | 13484 | TREE_TYPE (decl) |
a8f73d4b MM |
13485 | = build_exception_variant (fntype, |
13486 | TYPE_RAISES_EXCEPTIONS (fntype)); | |
13487 | } | |
13488 | else | |
13489 | abstract_virtuals_error (decl, TREE_TYPE (fntype)); | |
13490 | } | |
13491 | ||
8d08fdba MS |
13492 | /* Create the FUNCTION_DECL for a function definition. |
13493 | DECLSPECS and DECLARATOR are the parts of the declaration; | |
13494 | they describe the function's name and the type it returns, | |
13495 | but twisted together in a fashion that parallels the syntax of C. | |
13496 | ||
a8f73d4b MM |
13497 | FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the |
13498 | DECLARATOR is really the DECL for the function we are about to | |
13499 | process and that DECLSPECS should be ignored), SF_INCLASS_INLINE | |
f444e36b | 13500 | indicating that the function is an inline defined in-class. |
68642fb6 | 13501 | |
8d08fdba MS |
13502 | This function creates a binding context for the function body |
13503 | as well as setting up the FUNCTION_DECL in current_function_decl. | |
13504 | ||
13505 | Returns 1 on success. If the DECLARATOR is not suitable for a function | |
13506 | (it defines a datum instead), we return 0, which tells | |
13507 | yyparse to report a parse error. | |
13508 | ||
13509 | For C++, we must first check whether that datum makes any sense. | |
13510 | For example, "class A local_a(1,2);" means that variable local_a | |
13511 | is an aggregate of type A, which should have a constructor | |
87e3dbc9 | 13512 | applied to it with the argument list [1, 2]. */ |
8d08fdba MS |
13513 | |
13514 | int | |
a8f73d4b | 13515 | start_function (declspecs, declarator, attrs, flags) |
c11b6f21 | 13516 | tree declspecs, declarator, attrs; |
a8f73d4b | 13517 | int flags; |
8d08fdba | 13518 | { |
5566b478 | 13519 | tree decl1; |
8d08fdba MS |
13520 | tree ctype = NULL_TREE; |
13521 | tree fntype; | |
13522 | tree restype; | |
13523 | extern int have_extern_spec; | |
13524 | extern int used_extern_spec; | |
13525 | int doing_friend = 0; | |
a8f73d4b | 13526 | struct binding_level *bl; |
f444e36b | 13527 | tree current_function_parms; |
8d08fdba | 13528 | |
8d08fdba | 13529 | /* Sanity check. */ |
a1774733 | 13530 | my_friendly_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE, 160); |
8d08fdba MS |
13531 | my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161); |
13532 | ||
e92cc029 | 13533 | /* This should only be done once on the top most decl. */ |
8d08fdba MS |
13534 | if (have_extern_spec && !used_extern_spec) |
13535 | { | |
1f8f4a0b | 13536 | declspecs = tree_cons (NULL_TREE, get_identifier ("extern"), declspecs); |
8d08fdba MS |
13537 | used_extern_spec = 1; |
13538 | } | |
13539 | ||
a8f73d4b | 13540 | if (flags & SF_PRE_PARSED) |
8d08fdba MS |
13541 | { |
13542 | decl1 = declarator; | |
13543 | ||
8d08fdba MS |
13544 | fntype = TREE_TYPE (decl1); |
13545 | if (TREE_CODE (fntype) == METHOD_TYPE) | |
13546 | ctype = TYPE_METHOD_BASETYPE (fntype); | |
13547 | ||
cab1f180 ML |
13548 | /* ISO C++ 11.4/5. A friend function defined in a class is in |
13549 | the (lexical) scope of the class in which it is defined. */ | |
8d08fdba MS |
13550 | if (!ctype && DECL_FRIEND_P (decl1)) |
13551 | { | |
4f1c5b7d | 13552 | ctype = DECL_FRIEND_CONTEXT (decl1); |
8d08fdba MS |
13553 | |
13554 | /* CTYPE could be null here if we're dealing with a template; | |
13555 | for example, `inline friend float foo()' inside a template | |
13556 | will have no CTYPE set. */ | |
13557 | if (ctype && TREE_CODE (ctype) != RECORD_TYPE) | |
13558 | ctype = NULL_TREE; | |
13559 | else | |
13560 | doing_friend = 1; | |
13561 | } | |
13562 | ||
5566b478 | 13563 | last_function_parms = DECL_ARGUMENTS (decl1); |
8d08fdba MS |
13564 | } |
13565 | else | |
13566 | { | |
91d231cb | 13567 | decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL); |
8d08fdba MS |
13568 | /* If the declarator is not suitable for a function definition, |
13569 | cause a syntax error. */ | |
e89a6075 JM |
13570 | if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL) |
13571 | return 0; | |
13572 | ||
13573 | cplus_decl_attributes (&decl1, attrs, 0); | |
8d08fdba | 13574 | |
ecb0eece RH |
13575 | /* If #pragma weak was used, mark the decl weak now. */ |
13576 | if (current_binding_level == global_binding_level) | |
13577 | maybe_apply_pragma_weak (decl1); | |
13578 | ||
8d08fdba MS |
13579 | fntype = TREE_TYPE (decl1); |
13580 | ||
13581 | restype = TREE_TYPE (fntype); | |
7ddedda4 | 13582 | if (CLASS_TYPE_P (restype) && !CLASSTYPE_GOT_SEMICOLON (restype)) |
8d08fdba | 13583 | { |
33bd39a2 | 13584 | error ("semicolon missing after declaration of `%#T'", restype); |
051e6fd7 | 13585 | shadow_tag (build_tree_list (NULL_TREE, restype)); |
8d08fdba MS |
13586 | CLASSTYPE_GOT_SEMICOLON (restype) = 1; |
13587 | if (TREE_CODE (fntype) == FUNCTION_TYPE) | |
13588 | fntype = build_function_type (integer_type_node, | |
13589 | TYPE_ARG_TYPES (fntype)); | |
13590 | else | |
13591 | fntype = build_cplus_method_type (build_type_variant (TYPE_METHOD_BASETYPE (fntype), TREE_READONLY (decl1), TREE_SIDE_EFFECTS (decl1)), | |
13592 | integer_type_node, | |
13593 | TYPE_ARG_TYPES (fntype)); | |
13594 | TREE_TYPE (decl1) = fntype; | |
13595 | } | |
13596 | ||
13597 | if (TREE_CODE (fntype) == METHOD_TYPE) | |
13598 | ctype = TYPE_METHOD_BASETYPE (fntype); | |
35680744 | 13599 | else if (DECL_MAIN_P (decl1)) |
8d08fdba MS |
13600 | { |
13601 | /* If this doesn't return integer_type, complain. */ | |
13602 | if (TREE_TYPE (TREE_TYPE (decl1)) != integer_type_node) | |
13603 | { | |
a28e3c7f | 13604 | if (pedantic || warn_return_type) |
8251199e | 13605 | pedwarn ("return type for `main' changed to `int'"); |
8d08fdba MS |
13606 | TREE_TYPE (decl1) = fntype = default_function_type; |
13607 | } | |
8d08fdba MS |
13608 | } |
13609 | } | |
68642fb6 | 13610 | |
97055d5c AO |
13611 | if (DECL_DECLARED_INLINE_P (decl1) |
13612 | && lookup_attribute ("noinline", attrs)) | |
13613 | warning_with_decl (decl1, | |
13614 | "inline function `%s' given attribute noinline"); | |
13615 | ||
5f6eeeb3 NS |
13616 | if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1)) |
13617 | /* This is a constructor, we must ensure that any default args | |
13618 | introduced by this definition are propagated to the clones | |
13619 | now. The clones are used directly in overload resolution. */ | |
13620 | adjust_clone_args (decl1); | |
13621 | ||
b35d4555 MM |
13622 | /* Sometimes we don't notice that a function is a static member, and |
13623 | build a METHOD_TYPE for it. Fix that up now. */ | |
13624 | if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1) | |
13625 | && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE) | |
13626 | { | |
3afb32a4 | 13627 | revert_static_member_fn (decl1); |
b35d4555 MM |
13628 | last_function_parms = TREE_CHAIN (last_function_parms); |
13629 | ctype = NULL_TREE; | |
13630 | } | |
8d08fdba MS |
13631 | |
13632 | /* Warn if function was previously implicitly declared | |
13633 | (but not if we warned then). */ | |
13634 | if (! warn_implicit | |
13635 | && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE) | |
8251199e | 13636 | cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1))); |
8d08fdba | 13637 | |
f181d4ae MM |
13638 | /* Set up current_class_type, and enter the scope of the class, if |
13639 | appropriate. */ | |
13640 | if (ctype) | |
13641 | push_nested_class (ctype, 1); | |
13642 | else if (DECL_STATIC_FUNCTION_P (decl1)) | |
13643 | push_nested_class (DECL_CONTEXT (decl1), 2); | |
13644 | ||
13645 | /* Now that we have entered the scope of the class, we must restore | |
13646 | the bindings for any template parameters surrounding DECL1, if it | |
13647 | is an inline member template. (Order is important; consider the | |
13648 | case where a template parameter has the same name as a field of | |
13649 | the class.) It is not until after this point that | |
13650 | PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */ | |
a8f73d4b | 13651 | if (flags & SF_INCLASS_INLINE) |
f181d4ae MM |
13652 | maybe_begin_member_template_processing (decl1); |
13653 | ||
56cb9733 | 13654 | /* Effective C++ rule 15. */ |
9188c363 | 13655 | if (warn_ecpp |
596ea4e5 | 13656 | && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR |
9188c363 | 13657 | && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE) |
33bd39a2 | 13658 | warning ("`operator=' should return a reference to `*this'"); |
9188c363 MM |
13659 | |
13660 | /* Make the init_value nonzero so pushdecl knows this is not tentative. | |
13661 | error_mark_node is replaced below (in poplevel) with the BLOCK. */ | |
b35d4555 MM |
13662 | if (!DECL_INITIAL (decl1)) |
13663 | DECL_INITIAL (decl1) = error_mark_node; | |
9188c363 | 13664 | |
9188c363 MM |
13665 | /* This function exists in static storage. |
13666 | (This does not mean `static' in the C sense!) */ | |
13667 | TREE_STATIC (decl1) = 1; | |
13668 | ||
13669 | /* We must call push_template_decl after current_class_type is set | |
13670 | up. (If we are processing inline definitions after exiting a | |
13671 | class scope, current_class_type will be NULL_TREE until set above | |
13672 | by push_nested_class.) */ | |
13673 | if (processing_template_decl) | |
13674 | decl1 = push_template_decl (decl1); | |
13675 | ||
f181d4ae | 13676 | /* We are now in the scope of the function being defined. */ |
8d08fdba | 13677 | current_function_decl = decl1; |
f181d4ae | 13678 | |
5566b478 MS |
13679 | /* Save the parm names or decls from this function's declarator |
13680 | where store_parm_decls will find them. */ | |
13681 | current_function_parms = last_function_parms; | |
8d08fdba | 13682 | |
a8f73d4b MM |
13683 | /* Make sure the parameter and return types are reasonable. When |
13684 | you declare a function, these types can be incomplete, but they | |
13685 | must be complete when you define the function. */ | |
5156628f | 13686 | if (! processing_template_decl) |
f444e36b | 13687 | check_function_type (decl1, current_function_parms); |
f181d4ae | 13688 | |
a8f73d4b MM |
13689 | /* Build the return declaration for the function. */ |
13690 | restype = TREE_TYPE (fntype); | |
e89a6075 JM |
13691 | /* Promote the value to int before returning it. */ |
13692 | if (c_promoting_integer_type_p (restype)) | |
13693 | restype = type_promotes_to (restype); | |
13694 | if (DECL_RESULT (decl1) == NULL_TREE) | |
a8f73d4b | 13695 | { |
e89a6075 JM |
13696 | DECL_RESULT (decl1) |
13697 | = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype)); | |
13698 | c_apply_type_quals_to_decl (cp_type_quals (restype), | |
13699 | DECL_RESULT (decl1)); | |
5566b478 | 13700 | } |
a8f73d4b MM |
13701 | |
13702 | /* Initialize RTL machinery. We cannot do this until | |
13703 | CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this | |
13704 | even when processing a template; this is how we get | |
c00996a3 JM |
13705 | CFUN set up, and our per-function variables initialized. |
13706 | FIXME factor out the non-RTL stuff. */ | |
a8f73d4b MM |
13707 | bl = current_binding_level; |
13708 | init_function_start (decl1, input_filename, lineno); | |
13709 | current_binding_level = bl; | |
a8f73d4b MM |
13710 | |
13711 | /* Even though we're inside a function body, we still don't want to | |
13712 | call expand_expr to calculate the size of a variable-sized array. | |
13713 | We haven't necessarily assigned RTL to all variables yet, so it's | |
13714 | not safe to try to expand expressions involving them. */ | |
13715 | immediate_size_expand = 0; | |
01d939e8 | 13716 | cfun->x_dont_save_pending_sizes_p = 1; |
a8f73d4b | 13717 | |
f444e36b MM |
13718 | /* Start the statement-tree, start the tree now. */ |
13719 | begin_stmt_tree (&DECL_SAVED_TREE (decl1)); | |
6f80451c | 13720 | |
a8f73d4b | 13721 | /* Let the user know we're compiling this function. */ |
ea11ca7e | 13722 | announce_function (decl1); |
b7484fbe | 13723 | |
878cd289 MS |
13724 | /* Record the decl so that the function name is defined. |
13725 | If we already have a decl for this name, and it is a FUNCTION_DECL, | |
13726 | use the old decl. */ | |
a8f73d4b | 13727 | if (!processing_template_decl && !(flags & SF_PRE_PARSED)) |
878cd289 | 13728 | { |
75650646 | 13729 | /* A specialization is not used to guide overload resolution. */ |
2228d450 | 13730 | if (!DECL_TEMPLATE_SPECIALIZATION (decl1) |
959d8796 | 13731 | && ! DECL_FUNCTION_MEMBER_P (decl1)) |
75650646 | 13732 | decl1 = pushdecl (decl1); |
2c73f9f5 | 13733 | else |
b7698cf0 JM |
13734 | { |
13735 | /* We need to set the DECL_CONTEXT. */ | |
13736 | if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1)) | |
13737 | DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1)); | |
13738 | /* And make sure we have enough default args. */ | |
13739 | check_default_args (decl1); | |
13740 | } | |
878cd289 MS |
13741 | fntype = TREE_TYPE (decl1); |
13742 | } | |
5566b478 | 13743 | |
a8f73d4b | 13744 | /* Reset these in case the call to pushdecl changed them. */ |
5566b478 | 13745 | current_function_decl = decl1; |
01d939e8 | 13746 | cfun->decl = decl1; |
878cd289 | 13747 | |
78c120b5 MM |
13748 | /* If we are (erroneously) defining a function that we have already |
13749 | defined before, wipe out what we knew before. */ | |
5362b086 | 13750 | if (!DECL_PENDING_INLINE_P (decl1) |
78c120b5 MM |
13751 | && DECL_SAVED_FUNCTION_DATA (decl1)) |
13752 | { | |
13753 | free (DECL_SAVED_FUNCTION_DATA (decl1)); | |
13754 | DECL_SAVED_FUNCTION_DATA (decl1) = NULL; | |
13755 | } | |
b35d4555 | 13756 | |
f444e36b | 13757 | if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1)) |
b35d4555 MM |
13758 | { |
13759 | /* We know that this was set up by `grokclassfn'. We do not | |
13760 | wait until `store_parm_decls', since evil parse errors may | |
13761 | never get us to that point. Here we keep the consistency | |
13762 | between `current_class_type' and `current_class_ptr'. */ | |
13763 | tree t = DECL_ARGUMENTS (decl1); | |
68642fb6 UD |
13764 | |
13765 | my_friendly_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL, | |
b35d4555 MM |
13766 | 162); |
13767 | my_friendly_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE, | |
13768 | 19990811); | |
68642fb6 UD |
13769 | |
13770 | cp_function_chain->x_current_class_ref | |
3e411c3f | 13771 | = build_indirect_ref (t, NULL); |
b35d4555 MM |
13772 | cp_function_chain->x_current_class_ptr = t; |
13773 | ||
018fc244 MM |
13774 | /* Constructors and destructors need to know whether they're "in |
13775 | charge" of initializing virtual base classes. */ | |
e0fff4b3 | 13776 | t = TREE_CHAIN (t); |
454fa7a7 | 13777 | if (DECL_HAS_IN_CHARGE_PARM_P (decl1)) |
e0fff4b3 JM |
13778 | { |
13779 | current_in_charge_parm = t; | |
13780 | t = TREE_CHAIN (t); | |
13781 | } | |
13782 | if (DECL_HAS_VTT_PARM_P (decl1)) | |
13783 | { | |
13784 | if (DECL_NAME (t) != vtt_parm_identifier) | |
13785 | abort (); | |
13786 | current_vtt_parm = t; | |
13787 | } | |
b35d4555 MM |
13788 | } |
13789 | ||
db5ae43f | 13790 | if (DECL_INTERFACE_KNOWN (decl1)) |
faae18ab | 13791 | { |
4f1c5b7d | 13792 | tree ctx = decl_function_context (decl1); |
86052cc3 | 13793 | |
faae18ab MS |
13794 | if (DECL_NOT_REALLY_EXTERN (decl1)) |
13795 | DECL_EXTERNAL (decl1) = 0; | |
86052cc3 | 13796 | |
79065db2 | 13797 | if (ctx != NULL_TREE && DECL_DECLARED_INLINE_P (ctx) |
86052cc3 JM |
13798 | && TREE_PUBLIC (ctx)) |
13799 | /* This is a function in a local class in an extern inline | |
13800 | function. */ | |
13801 | comdat_linkage (decl1); | |
faae18ab | 13802 | } |
8d08fdba MS |
13803 | /* If this function belongs to an interface, it is public. |
13804 | If it belongs to someone else's interface, it is also external. | |
1f901793 | 13805 | This only affects inlines and template instantiations. */ |
5566b478 MS |
13806 | else if (interface_unknown == 0 |
13807 | && (! DECL_TEMPLATE_INSTANTIATION (decl1) | |
d11ad92e | 13808 | || flag_alt_external_templates)) |
8d08fdba | 13809 | { |
79065db2 MM |
13810 | if (DECL_DECLARED_INLINE_P (decl1) |
13811 | || DECL_TEMPLATE_INSTANTIATION (decl1) | |
5156628f | 13812 | || processing_template_decl) |
1f901793 JM |
13813 | { |
13814 | DECL_EXTERNAL (decl1) | |
13815 | = (interface_only | |
79065db2 MM |
13816 | || (DECL_DECLARED_INLINE_P (decl1) |
13817 | && ! flag_implement_inlines | |
9c73ec84 | 13818 | && !DECL_VINDEX (decl1))); |
1f901793 JM |
13819 | |
13820 | /* For WIN32 we also want to put these in linkonce sections. */ | |
13821 | maybe_make_one_only (decl1); | |
13822 | } | |
db5ae43f | 13823 | else |
893de33c | 13824 | DECL_EXTERNAL (decl1) = 0; |
e8abc66f | 13825 | DECL_NOT_REALLY_EXTERN (decl1) = 0; |
db5ae43f | 13826 | DECL_INTERFACE_KNOWN (decl1) = 1; |
8d08fdba | 13827 | } |
c16c47fb JM |
13828 | else if (interface_unknown && interface_only |
13829 | && (! DECL_TEMPLATE_INSTANTIATION (decl1) | |
13830 | || flag_alt_external_templates)) | |
13831 | { | |
13832 | /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma | |
13833 | interface, we will have interface_only set but not | |
13834 | interface_known. In that case, we don't want to use the normal | |
13835 | heuristics because someone will supply a #pragma implementation | |
13836 | elsewhere, and deducing it here would produce a conflict. */ | |
13837 | comdat_linkage (decl1); | |
13838 | DECL_EXTERNAL (decl1) = 0; | |
13839 | DECL_INTERFACE_KNOWN (decl1) = 1; | |
13840 | DECL_DEFER_OUTPUT (decl1) = 1; | |
13841 | } | |
8d08fdba | 13842 | else |
a0a33927 MS |
13843 | { |
13844 | /* This is a definition, not a reference. | |
b7484fbe MS |
13845 | So clear DECL_EXTERNAL. */ |
13846 | DECL_EXTERNAL (decl1) = 0; | |
faae18ab | 13847 | |
79065db2 MM |
13848 | if ((DECL_DECLARED_INLINE_P (decl1) |
13849 | || DECL_TEMPLATE_INSTANTIATION (decl1)) | |
5566b478 MS |
13850 | && ! DECL_INTERFACE_KNOWN (decl1) |
13851 | /* Don't try to defer nested functions for now. */ | |
4f1c5b7d | 13852 | && ! decl_function_context (decl1)) |
878cd289 MS |
13853 | DECL_DEFER_OUTPUT (decl1) = 1; |
13854 | else | |
893de33c | 13855 | DECL_INTERFACE_KNOWN (decl1) = 1; |
db5ae43f | 13856 | } |
a9aedbc2 | 13857 | |
f444e36b MM |
13858 | pushlevel (0); |
13859 | current_binding_level->parm_flag = 1; | |
8d08fdba | 13860 | |
5566b478 MS |
13861 | ++function_depth; |
13862 | ||
0d9eb3ba | 13863 | if (DECL_DESTRUCTOR_P (decl1)) |
46e8c075 MM |
13864 | { |
13865 | dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE); | |
13866 | DECL_CONTEXT (dtor_label) = current_function_decl; | |
13867 | } | |
8d08fdba | 13868 | |
0ba8a114 NS |
13869 | start_fname_decls (); |
13870 | ||
f444e36b MM |
13871 | store_parm_decls (current_function_parms); |
13872 | ||
8d08fdba MS |
13873 | return 1; |
13874 | } | |
13875 | \f | |
13876 | /* Store the parameter declarations into the current function declaration. | |
13877 | This is called after parsing the parameter declarations, before | |
13878 | digesting the body of the function. | |
13879 | ||
13880 | Also install to binding contour return value identifier, if any. */ | |
13881 | ||
f444e36b MM |
13882 | static void |
13883 | store_parm_decls (current_function_parms) | |
13884 | tree current_function_parms; | |
8d08fdba MS |
13885 | { |
13886 | register tree fndecl = current_function_decl; | |
13887 | register tree parm; | |
8d08fdba | 13888 | |
8d08fdba MS |
13889 | /* This is a chain of any other decls that came in among the parm |
13890 | declarations. If a parm is declared with enum {foo, bar} x; | |
13891 | then CONST_DECLs for foo and bar are put here. */ | |
13892 | tree nonparms = NULL_TREE; | |
13893 | ||
b35d4555 | 13894 | if (current_function_parms) |
8d08fdba MS |
13895 | { |
13896 | /* This case is when the function was defined with an ANSI prototype. | |
13897 | The parms already have decls, so we need not do anything here | |
13898 | except record them as in effect | |
13899 | and complain if any redundant old-style parm decls were written. */ | |
13900 | ||
b35d4555 MM |
13901 | tree specparms = current_function_parms; |
13902 | tree next; | |
13903 | ||
f444e36b | 13904 | /* Must clear this because it might contain TYPE_DECLs declared |
b35d4555 | 13905 | at class level. */ |
f444e36b | 13906 | storedecls (NULL_TREE); |
8d08fdba | 13907 | |
f444e36b | 13908 | /* If we're doing semantic analysis, then we'll call pushdecl |
b35d4555 MM |
13909 | for each of these. We must do them in reverse order so that |
13910 | they end in the correct forward order. */ | |
f444e36b | 13911 | specparms = nreverse (specparms); |
5566b478 | 13912 | |
b35d4555 | 13913 | for (parm = specparms; parm; parm = next) |
8d08fdba MS |
13914 | { |
13915 | next = TREE_CHAIN (parm); | |
13916 | if (TREE_CODE (parm) == PARM_DECL) | |
13917 | { | |
f444e36b MM |
13918 | if (DECL_NAME (parm) == NULL_TREE |
13919 | || TREE_CODE (parm) != VOID_TYPE) | |
13920 | pushdecl (parm); | |
13921 | else | |
33bd39a2 | 13922 | error ("parameter `%D' declared void", parm); |
8d08fdba MS |
13923 | } |
13924 | else | |
13925 | { | |
13926 | /* If we find an enum constant or a type tag, | |
13927 | put it aside for the moment. */ | |
13928 | TREE_CHAIN (parm) = NULL_TREE; | |
13929 | nonparms = chainon (nonparms, parm); | |
13930 | } | |
13931 | } | |
13932 | ||
f444e36b MM |
13933 | /* Get the decls in their original chain order and record in the |
13934 | function. This is all and only the PARM_DECLs that were | |
13935 | pushed into scope by the loop above. */ | |
13936 | DECL_ARGUMENTS (fndecl) = getdecls (); | |
969fd501 | 13937 | storetags (gettags ()); |
8d08fdba MS |
13938 | } |
13939 | else | |
13940 | DECL_ARGUMENTS (fndecl) = NULL_TREE; | |
13941 | ||
13942 | /* Now store the final chain of decls for the arguments | |
13943 | as the decl-chain of the current lexical scope. | |
13944 | Put the enumerators in as well, at the front so that | |
13945 | DECL_ARGUMENTS is not modified. */ | |
f444e36b | 13946 | storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl))); |
eb448459 | 13947 | |
b35d4555 | 13948 | /* Do the starting of the exception specifications, if we have any. */ |
68642fb6 | 13949 | if (flag_exceptions && !processing_template_decl |
1660cb3a | 13950 | && flag_enforce_eh_specs |
b35d4555 | 13951 | && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl))) |
52a11cbf | 13952 | current_eh_spec_block = begin_eh_spec_block (); |
8d08fdba MS |
13953 | } |
13954 | ||
8d08fdba | 13955 | \f |
59026e79 MM |
13956 | /* We have finished doing semantic analysis on DECL, but have not yet |
13957 | generated RTL for its body. Save away our current state, so that | |
13958 | when we want to generate RTL later we know what to do. */ | |
13959 | ||
13960 | static void | |
13961 | save_function_data (decl) | |
13962 | tree decl; | |
13963 | { | |
ae499cce | 13964 | struct cp_language_function *f; |
59026e79 MM |
13965 | |
13966 | /* Save the language-specific per-function data so that we can | |
13967 | get it back when we really expand this function. */ | |
13968 | my_friendly_assert (!DECL_PENDING_INLINE_P (decl), | |
13969 | 19990908); | |
68642fb6 | 13970 | |
59026e79 | 13971 | /* Make a copy. */ |
ae499cce MM |
13972 | f = ((struct cp_language_function *) |
13973 | xmalloc (sizeof (struct cp_language_function))); | |
4e135bdd | 13974 | memcpy (f, cp_function_chain, sizeof (struct cp_language_function)); |
59026e79 MM |
13975 | DECL_SAVED_FUNCTION_DATA (decl) = f; |
13976 | ||
13977 | /* Clear out the bits we don't need. */ | |
ae499cce MM |
13978 | f->base.x_stmt_tree.x_last_stmt = NULL_TREE; |
13979 | f->base.x_stmt_tree.x_last_expr_type = NULL_TREE; | |
59026e79 MM |
13980 | f->x_named_label_uses = NULL; |
13981 | f->bindings = NULL; | |
8e4ce833 | 13982 | f->x_local_names = NULL; |
59026e79 MM |
13983 | |
13984 | /* When we get back here again, we will be expanding. */ | |
13985 | f->x_expanding_p = 1; | |
914653a2 MM |
13986 | |
13987 | /* If we've already decided that we cannot inline this function, we | |
13988 | must remember that fact when we actually go to expand the | |
13989 | function. */ | |
acc72c37 MM |
13990 | if (current_function_cannot_inline) |
13991 | { | |
13992 | f->cannot_inline = current_function_cannot_inline; | |
13993 | DECL_INLINE (decl) = 0; | |
13994 | } | |
59026e79 MM |
13995 | } |
13996 | ||
cdd2559c JM |
13997 | /* Add a note to mark the beginning of the main body of the constructor. |
13998 | This is used to set up the data structures for the cleanup regions for | |
13999 | fully-constructed bases and members. */ | |
14000 | ||
14001 | static void | |
14002 | begin_constructor_body () | |
14003 | { | |
cdd2559c JM |
14004 | } |
14005 | ||
ade3dc07 JM |
14006 | /* Add a note to mark the end of the main body of the constructor. This is |
14007 | used to end the cleanup regions for fully-constructed bases and | |
14008 | members. */ | |
efee38a9 MM |
14009 | |
14010 | static void | |
14011 | finish_constructor_body () | |
14012 | { | |
efee38a9 MM |
14013 | } |
14014 | ||
cdd2559c JM |
14015 | /* Do all the processing for the beginning of a destructor; set up the |
14016 | vtable pointers and cleanups for bases and members. */ | |
14017 | ||
14018 | static void | |
14019 | begin_destructor_body () | |
14020 | { | |
14021 | tree if_stmt; | |
14022 | tree compound_stmt; | |
14023 | ||
14024 | /* If the dtor is empty, and we know there is not any possible | |
14025 | way we could use any vtable entries, before they are possibly | |
14026 | set by a base class dtor, we don't have to setup the vtables, | |
14027 | as we know that any base class dtor will set up any vtables | |
14028 | it needs. We avoid MI, because one base class dtor can do a | |
14029 | virtual dispatch to an overridden function that would need to | |
14030 | have a non-related vtable set up, we cannot avoid setting up | |
14031 | vtables in that case. We could change this to see if there | |
14032 | is just one vtable. | |
14033 | ||
14034 | ??? In the destructor for a class, the vtables are set | |
14035 | appropriately for that class. There will be no non-related | |
14036 | vtables. jason 2001-12-11. */ | |
14037 | if_stmt = begin_if_stmt (); | |
14038 | ||
14039 | /* If it is not safe to avoid setting up the vtables, then | |
14040 | someone will change the condition to be boolean_true_node. | |
14041 | (Actually, for now, we do not have code to set the condition | |
14042 | appropriately, so we just assume that we always need to | |
14043 | initialize the vtables.) */ | |
14044 | finish_if_stmt_cond (boolean_true_node, if_stmt); | |
14045 | current_vcalls_possible_p = &IF_COND (if_stmt); | |
14046 | ||
14047 | compound_stmt = begin_compound_stmt (/*has_no_scope=*/0); | |
14048 | ||
14049 | /* Make all virtual function table pointers in non-virtual base | |
14050 | classes point to CURRENT_CLASS_TYPE's virtual function | |
14051 | tables. */ | |
14052 | initialize_vtbl_ptrs (current_class_ptr); | |
14053 | ||
14054 | finish_compound_stmt (/*has_no_scope=*/0, compound_stmt); | |
14055 | finish_then_clause (if_stmt); | |
14056 | finish_if_stmt (); | |
14057 | ||
14058 | /* And insert cleanups for our bases and members so that they | |
14059 | will be properly destroyed if we throw. */ | |
14060 | push_base_cleanups (); | |
14061 | } | |
14062 | ||
ade3dc07 JM |
14063 | /* At the end of every destructor we generate code to delete the object if |
14064 | necessary. Do that now. */ | |
9bfadf57 MM |
14065 | |
14066 | static void | |
14067 | finish_destructor_body () | |
14068 | { | |
9bfadf57 MM |
14069 | tree exprstmt; |
14070 | ||
52682a1b MM |
14071 | /* In a virtual destructor, we must call delete. */ |
14072 | if (DECL_VIRTUAL_P (current_function_decl)) | |
14073 | { | |
14074 | tree if_stmt; | |
14075 | tree virtual_size = c_sizeof (current_class_type); | |
68642fb6 | 14076 | |
52682a1b | 14077 | /* [class.dtor] |
68642fb6 | 14078 | |
ade3dc07 JM |
14079 | At the point of definition of a virtual destructor (including |
14080 | an implicit definition), non-placement operator delete shall | |
14081 | be looked up in the scope of the destructor's class and if | |
14082 | found shall be accessible and unambiguous. */ | |
52682a1b MM |
14083 | exprstmt = build_op_delete_call |
14084 | (DELETE_EXPR, current_class_ptr, virtual_size, | |
14085 | LOOKUP_NORMAL | LOOKUP_SPECULATIVELY, NULL_TREE); | |
298d6f60 | 14086 | |
52682a1b MM |
14087 | if_stmt = begin_if_stmt (); |
14088 | finish_if_stmt_cond (build (BIT_AND_EXPR, integer_type_node, | |
14089 | current_in_charge_parm, | |
14090 | integer_one_node), | |
14091 | if_stmt); | |
14092 | finish_expr_stmt (exprstmt); | |
14093 | finish_then_clause (if_stmt); | |
14094 | finish_if_stmt (); | |
14095 | } | |
ade3dc07 | 14096 | } |
9bfadf57 | 14097 | |
ade3dc07 JM |
14098 | /* Do the necessary processing for the beginning of a function body, which |
14099 | in this case includes member-initializers, but not the catch clauses of | |
14100 | a function-try-block. Currently, this means opening a binding level | |
14101 | for the member-initializers (in a ctor) and member cleanups (in a dtor). | |
14102 | In other functions, this isn't necessary, but it doesn't hurt. */ | |
14103 | ||
14104 | tree | |
14105 | begin_function_body () | |
14106 | { | |
cdd2559c JM |
14107 | tree stmt; |
14108 | ||
b5856475 JM |
14109 | if (processing_template_decl) |
14110 | /* Do nothing now. */; | |
14111 | else | |
14112 | /* Always keep the BLOCK node associated with the outermost pair of | |
14113 | curly braces of a function. These are needed for correct | |
14114 | operation of dwarfout.c. */ | |
14115 | keep_next_level (1); | |
14116 | ||
cdd2559c | 14117 | stmt = begin_compound_stmt (0); |
ade3dc07 | 14118 | COMPOUND_STMT_BODY_BLOCK (stmt) = 1; |
cdd2559c JM |
14119 | |
14120 | if (processing_template_decl) | |
14121 | /* Do nothing now. */; | |
14122 | else if (DECL_CONSTRUCTOR_P (current_function_decl)) | |
14123 | begin_constructor_body (); | |
14124 | else if (DECL_DESTRUCTOR_P (current_function_decl)) | |
14125 | begin_destructor_body (); | |
14126 | ||
ade3dc07 | 14127 | return stmt; |
9bfadf57 MM |
14128 | } |
14129 | ||
ade3dc07 JM |
14130 | /* Do the processing for the end of a function body. Currently, this means |
14131 | closing out the cleanups for fully-constructed bases and members, and in | |
14132 | the case of the destructor, deleting the object if desired. Again, this | |
14133 | is only meaningful for [cd]tors, since they are the only functions where | |
14134 | there is a significant distinction between the main body and any | |
14135 | function catch clauses. Handling, say, main() return semantics here | |
14136 | would be wrong, as flowing off the end of a function catch clause for | |
14137 | main() would also need to return 0. */ | |
14138 | ||
14139 | void | |
14140 | finish_function_body (compstmt) | |
14141 | tree compstmt; | |
14142 | { | |
14143 | if (processing_template_decl) | |
14144 | /* Do nothing now. */; | |
14145 | else if (DECL_DESTRUCTOR_P (current_function_decl)) | |
14146 | /* Any return from a destructor will end up here. Put it before the | |
14147 | cleanups so that an explicit return doesn't duplicate them. */ | |
14148 | add_stmt (build_stmt (LABEL_STMT, dtor_label)); | |
14149 | ||
14150 | /* Close the block; in a destructor, run the member cleanups. */ | |
14151 | finish_compound_stmt (0, compstmt); | |
14152 | ||
14153 | if (processing_template_decl) | |
14154 | /* Do nothing now. */; | |
14155 | else if (DECL_CONSTRUCTOR_P (current_function_decl)) | |
14156 | finish_constructor_body (); | |
14157 | else if (DECL_DESTRUCTOR_P (current_function_decl)) | |
14158 | finish_destructor_body (); | |
14159 | } | |
14160 | ||
8d08fdba MS |
14161 | /* Finish up a function declaration and compile that function |
14162 | all the way to assembler language output. The free the storage | |
14163 | for the function definition. | |
14164 | ||
68642fb6 | 14165 | FLAGS is a bitwise or of the following values: |
f181d4ae MM |
14166 | 2 - INCLASS_INLINE |
14167 | We just finished processing the body of an in-class inline | |
14168 | function definition. (This processing will have taken place | |
87e3dbc9 | 14169 | after the class definition is complete.) */ |
8d08fdba | 14170 | |
4d6abc1c | 14171 | tree |
0acf7199 | 14172 | finish_function (flags) |
f181d4ae | 14173 | int flags; |
8d08fdba MS |
14174 | { |
14175 | register tree fndecl = current_function_decl; | |
14176 | tree fntype, ctype = NULL_TREE; | |
f181d4ae | 14177 | int inclass_inline = (flags & 2) != 0; |
87e3dbc9 | 14178 | int nested; |
8d08fdba MS |
14179 | |
14180 | /* When we get some parse errors, we can end up without a | |
14181 | current_function_decl, so cope. */ | |
14182 | if (fndecl == NULL_TREE) | |
4d6abc1c | 14183 | return error_mark_node; |
8d08fdba | 14184 | |
87e3dbc9 | 14185 | nested = function_depth > 1; |
8d08fdba MS |
14186 | fntype = TREE_TYPE (fndecl); |
14187 | ||
9bfadf57 MM |
14188 | /* TREE_READONLY (fndecl) = 1; |
14189 | This caused &foo to be of type ptr-to-const-function | |
14190 | which then got a warning when stored in a ptr-to-function variable. */ | |
8d08fdba | 14191 | |
f444e36b | 14192 | my_friendly_assert (building_stmt_tree (), 20000911); |
8d08fdba | 14193 | |
0ba8a114 NS |
14194 | finish_fname_decls (); |
14195 | ||
db9b2174 MM |
14196 | /* For a cloned function, we've already got all the code we need; |
14197 | there's no need to add any extra bits. */ | |
f444e36b | 14198 | if (!DECL_CLONED_FUNCTION_P (fndecl)) |
8d08fdba | 14199 | { |
ade3dc07 | 14200 | if (DECL_MAIN_P (current_function_decl)) |
efee38a9 MM |
14201 | { |
14202 | /* Make it so that `main' always returns 0 by default. */ | |
a0de9d20 | 14203 | #ifdef VMS_TARGET |
efee38a9 MM |
14204 | finish_return_stmt (integer_one_node); |
14205 | #else | |
14206 | finish_return_stmt (integer_zero_node); | |
14207 | #endif | |
14208 | } | |
87e3dbc9 | 14209 | |
b35d4555 MM |
14210 | /* Finish dealing with exception specifiers. */ |
14211 | if (flag_exceptions && !processing_template_decl | |
1660cb3a | 14212 | && flag_enforce_eh_specs |
b35d4555 | 14213 | && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl))) |
52a11cbf RH |
14214 | finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS |
14215 | (TREE_TYPE (current_function_decl)), | |
14216 | current_eh_spec_block); | |
5566b478 | 14217 | } |
68642fb6 | 14218 | |
558475f0 | 14219 | /* If we're saving up tree structure, tie off the function now. */ |
f444e36b | 14220 | finish_stmt_tree (&DECL_SAVED_TREE (fndecl)); |
8d2733ca | 14221 | |
8d08fdba MS |
14222 | /* This must come after expand_function_end because cleanups might |
14223 | have declarations (from inline functions) that need to go into | |
14224 | this function's blocks. */ | |
7437519c ZW |
14225 | |
14226 | /* If the current binding level isn't the outermost binding level | |
14227 | for this function, either there is a bug, or we have experienced | |
14228 | syntax errors and the statement tree is malformed. */ | |
f444e36b | 14229 | if (current_binding_level->parm_flag != 1) |
7437519c ZW |
14230 | { |
14231 | /* Make sure we have already experienced errors. */ | |
14232 | if (errorcount == 0) | |
14233 | abort (); | |
14234 | ||
14235 | /* Throw away the broken statement tree and extra binding | |
14236 | levels. */ | |
14237 | DECL_SAVED_TREE (fndecl) = build_stmt (COMPOUND_STMT, NULL_TREE); | |
14238 | ||
14239 | while (current_binding_level->parm_flag != 1) | |
14240 | { | |
14241 | if (current_binding_level->parm_flag == 2) | |
14242 | pop_nested_class (); | |
14243 | else | |
14244 | poplevel (0, 0, 0); | |
14245 | } | |
14246 | } | |
f444e36b | 14247 | poplevel (1, 0, 1); |
8d08fdba | 14248 | |
07b2f2fd JM |
14249 | /* Set up the named return value optimization, if we can. Here, we |
14250 | eliminate the copy from the nrv into the RESULT_DECL and any cleanup | |
14251 | for the nrv. genrtl_start_function and declare_return_variable | |
14252 | handle making the nrv and RESULT_DECL share space. */ | |
14253 | if (current_function_return_value) | |
14254 | { | |
14255 | tree r = current_function_return_value; | |
14256 | /* This is only worth doing for fns that return in memory--and | |
14257 | simpler, since we don't have to worry about promoted modes. */ | |
14258 | if (r != error_mark_node | |
14259 | && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)))) | |
14260 | { | |
14261 | DECL_ALIGN (r) = DECL_ALIGN (DECL_RESULT (fndecl)); | |
14262 | walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl), | |
14263 | nullify_returns_r, r); | |
14264 | } | |
14265 | else | |
14266 | /* Clear it so genrtl_start_function and declare_return_variable | |
14267 | know we're not optimizing. */ | |
14268 | current_function_return_value = NULL_TREE; | |
14269 | } | |
14270 | ||
a8f73d4b | 14271 | /* Remember that we were in class scope. */ |
db5ae43f | 14272 | if (current_class_name) |
a8f73d4b | 14273 | ctype = current_class_type; |
db5ae43f | 14274 | |
1caa11d3 MM |
14275 | /* Must mark the RESULT_DECL as being in this function. */ |
14276 | DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl; | |
14277 | ||
14278 | /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point | |
14279 | to the FUNCTION_DECL node itself. */ | |
14280 | BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl; | |
14281 | ||
59026e79 | 14282 | /* Save away current state, if appropriate. */ |
f444e36b | 14283 | if (!processing_template_decl) |
59026e79 MM |
14284 | save_function_data (fndecl); |
14285 | ||
95fabfd3 MM |
14286 | /* If this function calls `setjmp' it cannot be inlined. When |
14287 | `longjmp' is called it is not guaranteed to restore the value of | |
14288 | local variables that have been modified since the call to | |
14289 | `setjmp'. So, if were to inline this function into some caller | |
14290 | `c', then when we `longjmp', we might not restore all variables | |
14291 | in `c'. (It might seem, at first blush, that there's no way for | |
14292 | this function to modify local variables in `c', but their | |
14293 | addresses may have been stored somewhere accessible to this | |
14294 | function.) */ | |
f444e36b | 14295 | if (!processing_template_decl && calls_setjmp_p (fndecl)) |
95fabfd3 MM |
14296 | DECL_UNINLINABLE (fndecl) = 1; |
14297 | ||
efe49da0 | 14298 | /* Complain if there's just no return statement. */ |
46cfb101 JM |
14299 | if (warn_return_type |
14300 | && !processing_template_decl | |
efe49da0 | 14301 | && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE |
57d5032b | 14302 | && !current_function_returns_value && !current_function_returns_null |
efe49da0 JM |
14303 | /* Don't complain if we abort or throw. */ |
14304 | && !current_function_returns_abnormally | |
46cfb101 JM |
14305 | && !DECL_NAME (DECL_RESULT (fndecl)) |
14306 | /* Normally, with -Wreturn-type, flow will complain. Unless we're an | |
efe49da0 | 14307 | inline function, as we might never be compiled separately. */ |
46cfb101 | 14308 | && DECL_INLINE (fndecl)) |
efe49da0 JM |
14309 | warning ("no return statement in function returning non-void"); |
14310 | ||
f444e36b MM |
14311 | /* Clear out memory we no longer need. */ |
14312 | free_after_parsing (cfun); | |
14313 | /* Since we never call rest_of_compilation, we never clear | |
14314 | CFUN. Do so explicitly. */ | |
14315 | free_after_compilation (cfun); | |
14316 | cfun = NULL; | |
a8f73d4b MM |
14317 | |
14318 | /* If this is a in-class inline definition, we may have to pop the | |
14319 | bindings for the template parameters that we added in | |
14320 | maybe_begin_member_template_processing when start_function was | |
14321 | called. */ | |
14322 | if (inclass_inline) | |
14323 | maybe_end_member_template_processing (); | |
14324 | ||
14325 | /* Leave the scope of the class. */ | |
14326 | if (ctype) | |
14327 | pop_nested_class (); | |
5566b478 MS |
14328 | |
14329 | --function_depth; | |
8d08fdba | 14330 | |
4d6abc1c | 14331 | /* Clean up. */ |
28cbf42c | 14332 | if (! nested) |
1f8f4a0b MM |
14333 | /* Let the error reporting routines know that we're outside a |
14334 | function. For a nested function, this value is used in | |
14335 | pop_cp_function_context and then reset via pop_function_context. */ | |
14336 | current_function_decl = NULL_TREE; | |
4d6abc1c MM |
14337 | |
14338 | return fndecl; | |
8d08fdba MS |
14339 | } |
14340 | \f | |
14341 | /* Create the FUNCTION_DECL for a function definition. | |
8d08fdba MS |
14342 | DECLSPECS and DECLARATOR are the parts of the declaration; |
14343 | they describe the return type and the name of the function, | |
14344 | but twisted together in a fashion that parallels the syntax of C. | |
14345 | ||
14346 | This function creates a binding context for the function body | |
14347 | as well as setting up the FUNCTION_DECL in current_function_decl. | |
14348 | ||
14349 | Returns a FUNCTION_DECL on success. | |
14350 | ||
14351 | If the DECLARATOR is not suitable for a function (it defines a datum | |
14352 | instead), we return 0, which tells yyparse to report a parse error. | |
14353 | ||
14354 | May return void_type_node indicating that this method is actually | |
14355 | a friend. See grokfield for more details. | |
14356 | ||
14357 | Came here with a `.pushlevel' . | |
14358 | ||
14359 | DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING | |
14360 | CHANGES TO CODE IN `grokfield'. */ | |
e92cc029 | 14361 | |
8d08fdba | 14362 | tree |
acf82af2 JM |
14363 | start_method (declspecs, declarator, attrlist) |
14364 | tree declarator, declspecs, attrlist; | |
8d08fdba | 14365 | { |
c11b6f21 | 14366 | tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0, |
91d231cb | 14367 | &attrlist); |
8d08fdba MS |
14368 | |
14369 | /* Something too ugly to handle. */ | |
14370 | if (fndecl == NULL_TREE) | |
14371 | return NULL_TREE; | |
14372 | ||
195a5def IR |
14373 | if (attrlist) |
14374 | cplus_decl_attributes (&fndecl, attrlist, 0); | |
14375 | ||
8d08fdba | 14376 | /* Pass friends other than inline friend functions back. */ |
a1774733 | 14377 | if (fndecl == void_type_node) |
8d08fdba MS |
14378 | return fndecl; |
14379 | ||
14380 | if (TREE_CODE (fndecl) != FUNCTION_DECL) | |
14381 | /* Not a function, tell parser to report parse error. */ | |
14382 | return NULL_TREE; | |
14383 | ||
8d08fdba MS |
14384 | if (DECL_IN_AGGR_P (fndecl)) |
14385 | { | |
14386 | if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type) | |
14387 | { | |
68642fb6 | 14388 | if (DECL_CONTEXT (fndecl) |
2c73f9f5 | 14389 | && TREE_CODE( DECL_CONTEXT (fndecl)) != NAMESPACE_DECL) |
33bd39a2 | 14390 | error ("`%D' is already defined in class `%T'", fndecl, |
2ae7bada | 14391 | DECL_CONTEXT (fndecl)); |
8d08fdba MS |
14392 | } |
14393 | return void_type_node; | |
14394 | } | |
14395 | ||
f3400fe2 JM |
14396 | check_template_shadow (fndecl); |
14397 | ||
79065db2 | 14398 | DECL_DECLARED_INLINE_P (fndecl) = 1; |
faae18ab | 14399 | |
8926095f | 14400 | if (flag_default_inline) |
8d08fdba MS |
14401 | DECL_INLINE (fndecl) = 1; |
14402 | ||
36a117a5 MM |
14403 | /* We process method specializations in finish_struct_1. */ |
14404 | if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl)) | |
3ac3d9ea | 14405 | fndecl = push_template_decl (fndecl); |
a0a33927 | 14406 | |
8d08fdba MS |
14407 | if (! DECL_FRIEND_P (fndecl)) |
14408 | { | |
8d08fdba MS |
14409 | if (TREE_CHAIN (fndecl)) |
14410 | { | |
14411 | fndecl = copy_node (fndecl); | |
14412 | TREE_CHAIN (fndecl) = NULL_TREE; | |
14413 | } | |
271e6f02 | 14414 | grok_special_member_properties (fndecl); |
8d08fdba MS |
14415 | } |
14416 | ||
cd9f6678 | 14417 | cp_finish_decl (fndecl, NULL_TREE, NULL_TREE, 0); |
8d08fdba MS |
14418 | |
14419 | /* Make a place for the parms */ | |
14420 | pushlevel (0); | |
14421 | current_binding_level->parm_flag = 1; | |
68642fb6 | 14422 | |
8d08fdba MS |
14423 | DECL_IN_AGGR_P (fndecl) = 1; |
14424 | return fndecl; | |
14425 | } | |
14426 | ||
14427 | /* Go through the motions of finishing a function definition. | |
14428 | We don't compile this method until after the whole class has | |
14429 | been processed. | |
14430 | ||
14431 | FINISH_METHOD must return something that looks as though it | |
14432 | came from GROKFIELD (since we are defining a method, after all). | |
14433 | ||
14434 | This is called after parsing the body of the function definition. | |
14435 | STMTS is the chain of statements that makes up the function body. | |
14436 | ||
14437 | DECL is the ..._DECL that `start_method' provided. */ | |
14438 | ||
14439 | tree | |
14440 | finish_method (decl) | |
14441 | tree decl; | |
14442 | { | |
14443 | register tree fndecl = decl; | |
14444 | tree old_initial; | |
8d08fdba MS |
14445 | |
14446 | register tree link; | |
14447 | ||
a1774733 | 14448 | if (decl == void_type_node) |
8d08fdba MS |
14449 | return decl; |
14450 | ||
14451 | old_initial = DECL_INITIAL (fndecl); | |
14452 | ||
14453 | /* Undo the level for the parms (from start_method). | |
14454 | This is like poplevel, but it causes nothing to be | |
14455 | saved. Saving information here confuses symbol-table | |
14456 | output routines. Besides, this information will | |
14457 | be correctly output when this method is actually | |
14458 | compiled. */ | |
14459 | ||
14460 | /* Clear out the meanings of the local variables of this level; | |
14461 | also record in each decl which block it belongs to. */ | |
14462 | ||
14463 | for (link = current_binding_level->names; link; link = TREE_CHAIN (link)) | |
14464 | { | |
14465 | if (DECL_NAME (link) != NULL_TREE) | |
d8f8dca1 | 14466 | pop_binding (DECL_NAME (link), link); |
8d08fdba MS |
14467 | my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163); |
14468 | DECL_CONTEXT (link) = NULL_TREE; | |
14469 | } | |
14470 | ||
8d08fdba MS |
14471 | poplevel (0, 0, 0); |
14472 | ||
14473 | DECL_INITIAL (fndecl) = old_initial; | |
14474 | ||
14475 | /* We used to check if the context of FNDECL was different from | |
14476 | current_class_type as another way to get inside here. This didn't work | |
14477 | for String.cc in libg++. */ | |
14478 | if (DECL_FRIEND_P (fndecl)) | |
14479 | { | |
14480 | CLASSTYPE_INLINE_FRIENDS (current_class_type) | |
14481 | = tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type)); | |
14482 | decl = void_type_node; | |
14483 | } | |
14484 | ||
14485 | return decl; | |
14486 | } | |
14487 | \f | |
0154eaa8 MM |
14488 | |
14489 | /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that | |
14490 | we can lay it out later, when and if its type becomes complete. */ | |
8d08fdba MS |
14491 | |
14492 | void | |
0154eaa8 MM |
14493 | maybe_register_incomplete_var (var) |
14494 | tree var; | |
8d08fdba | 14495 | { |
0154eaa8 | 14496 | my_friendly_assert (TREE_CODE (var) == VAR_DECL, 20020406); |
8d08fdba | 14497 | |
0154eaa8 MM |
14498 | /* Keep track of variables with incomplete types. */ |
14499 | if (!processing_template_decl && TREE_TYPE (var) != error_mark_node | |
14500 | && DECL_EXTERNAL (var)) | |
70adf8a9 | 14501 | { |
0154eaa8 MM |
14502 | tree inner_type = TREE_TYPE (var); |
14503 | ||
14504 | while (TREE_CODE (inner_type) == ARRAY_TYPE) | |
14505 | inner_type = TREE_TYPE (inner_type); | |
14506 | inner_type = TYPE_MAIN_VARIANT (inner_type); | |
14507 | ||
14508 | if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type)) | |
14509 | /* RTTI TD entries are created while defining the type_info. */ | |
14510 | || (TYPE_LANG_SPECIFIC (inner_type) | |
14511 | && TYPE_BEING_DEFINED (inner_type))) | |
14512 | incomplete_vars = tree_cons (inner_type, var, incomplete_vars); | |
70adf8a9 | 14513 | } |
0154eaa8 | 14514 | } |
70adf8a9 | 14515 | |
0154eaa8 MM |
14516 | /* Called when a class type (given by TYPE) is defined. If there are |
14517 | any existing VAR_DECLs whose type hsa been completed by this | |
14518 | declaration, update them now. */ | |
70adf8a9 | 14519 | |
0154eaa8 MM |
14520 | void |
14521 | complete_vars (type) | |
14522 | tree type; | |
14523 | { | |
14524 | tree *list = &incomplete_vars; | |
14525 | ||
14526 | my_friendly_assert (CLASS_TYPE_P (type), 20020406); | |
14527 | while (*list) | |
14528 | { | |
14529 | if (same_type_p (type, TREE_PURPOSE (*list))) | |
70adf8a9 | 14530 | { |
0154eaa8 MM |
14531 | tree var = TREE_VALUE (*list); |
14532 | /* Make sure that the type of the VAR has been laid out. It | |
14533 | might not have been if the type of VAR is an array. */ | |
14534 | layout_type (TREE_TYPE (var)); | |
14535 | /* Lay out the variable itself. */ | |
14536 | layout_decl (var, 0); | |
14537 | /* Remove this entry from the list. */ | |
14538 | *list = TREE_CHAIN (*list); | |
f30432d7 MS |
14539 | } |
14540 | else | |
0154eaa8 | 14541 | list = &TREE_CHAIN (*list); |
f30432d7 | 14542 | } |
8d08fdba MS |
14543 | } |
14544 | ||
86f45d2c MM |
14545 | /* If DECL is of a type which needs a cleanup, build that cleanup |
14546 | here. */ | |
e92cc029 | 14547 | |
86f45d2c | 14548 | tree |
c88770e9 | 14549 | cxx_maybe_build_cleanup (decl) |
86f45d2c | 14550 | tree decl; |
8d08fdba MS |
14551 | { |
14552 | tree type = TREE_TYPE (decl); | |
86f45d2c | 14553 | |
834c6dff | 14554 | if (type != error_mark_node && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) |
8d08fdba | 14555 | { |
80048418 | 14556 | int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR; |
8d08fdba | 14557 | tree rval; |
8d08fdba | 14558 | |
8d08fdba MS |
14559 | if (TREE_CODE (type) == ARRAY_TYPE) |
14560 | rval = decl; | |
14561 | else | |
14562 | { | |
dffd7eb6 | 14563 | cxx_mark_addressable (decl); |
8d08fdba MS |
14564 | rval = build_unary_op (ADDR_EXPR, decl, 0); |
14565 | } | |
14566 | ||
14567 | /* Optimize for space over speed here. */ | |
14568 | if (! TYPE_USES_VIRTUAL_BASECLASSES (type) | |
14569 | || flag_expensive_optimizations) | |
14570 | flags |= LOOKUP_NONVIRTUAL; | |
14571 | ||
86f45d2c MM |
14572 | rval = build_delete (TREE_TYPE (rval), rval, |
14573 | sfk_complete_destructor, flags, 0); | |
8d08fdba MS |
14574 | |
14575 | if (TYPE_USES_VIRTUAL_BASECLASSES (type) | |
14576 | && ! TYPE_HAS_DESTRUCTOR (type)) | |
e1b3e07d | 14577 | rval = build_compound_expr (tree_cons (NULL_TREE, rval, |
051e6fd7 | 14578 | build_tree_list (NULL_TREE, build_vbase_delete (type, decl)))); |
8d08fdba | 14579 | |
8d08fdba MS |
14580 | return rval; |
14581 | } | |
6e4ae815 | 14582 | return NULL_TREE; |
8d08fdba MS |
14583 | } |
14584 | \f | |
558475f0 | 14585 | /* When a stmt has been parsed, this function is called. */ |
8d08fdba MS |
14586 | |
14587 | void | |
14588 | finish_stmt () | |
14589 | { | |
558475f0 MM |
14590 | /* Always assume this statement was not an expression statement. If |
14591 | it actually was an expression statement, its our callers | |
14592 | responsibility to fix this up. */ | |
14593 | last_expr_type = NULL_TREE; | |
8d08fdba MS |
14594 | } |
14595 | ||
3afb32a4 MM |
14596 | /* DECL was originally constructed as a non-static member function, |
14597 | but turned out to be static. Update it accordingly. */ | |
700f8a87 | 14598 | |
8857f91e | 14599 | void |
3afb32a4 MM |
14600 | revert_static_member_fn (decl) |
14601 | tree decl; | |
8d08fdba | 14602 | { |
700f8a87 | 14603 | tree tmp; |
3afb32a4 MM |
14604 | tree function = TREE_TYPE (decl); |
14605 | tree args = TYPE_ARG_TYPES (function); | |
8d08fdba | 14606 | |
89d684bb | 14607 | if (cp_type_quals (TREE_TYPE (TREE_VALUE (args))) |
91063b51 | 14608 | != TYPE_UNQUALIFIED) |
33bd39a2 | 14609 | error ("static member function `%#D' declared with type qualifiers", |
11306230 | 14610 | decl); |
f30432d7 | 14611 | |
700f8a87 MS |
14612 | args = TREE_CHAIN (args); |
14613 | tmp = build_function_type (TREE_TYPE (function), args); | |
89d684bb | 14614 | tmp = build_qualified_type (tmp, cp_type_quals (function)); |
f30432d7 | 14615 | tmp = build_exception_variant (tmp, |
8d08fdba | 14616 | TYPE_RAISES_EXCEPTIONS (function)); |
3afb32a4 MM |
14617 | TREE_TYPE (decl) = tmp; |
14618 | if (DECL_ARGUMENTS (decl)) | |
14619 | DECL_ARGUMENTS (decl) = TREE_CHAIN (DECL_ARGUMENTS (decl)); | |
14620 | DECL_STATIC_FUNCTION_P (decl) = 1; | |
8d08fdba | 14621 | } |
a4443a08 | 14622 | |
68642fb6 UD |
14623 | /* Initialize the variables used during compilation of a C++ |
14624 | function. */ | |
db5ae43f | 14625 | |
99dccabc MM |
14626 | static void |
14627 | push_cp_function_context (f) | |
14628 | struct function *f; | |
14629 | { | |
ae499cce MM |
14630 | struct cp_language_function *p |
14631 | = ((struct cp_language_function *) | |
14632 | xcalloc (1, sizeof (struct cp_language_function))); | |
14633 | f->language = (struct language_function *) p; | |
db5ae43f | 14634 | |
b35d4555 MM |
14635 | /* It takes an explicit call to expand_body to generate RTL for a |
14636 | function. */ | |
14637 | expanding_p = 0; | |
f1dedc31 MM |
14638 | |
14639 | /* Whenever we start a new function, we destroy temporaries in the | |
14640 | usual way. */ | |
ae499cce | 14641 | current_stmt_tree ()->stmts_are_full_exprs_p = 1; |
db5ae43f MS |
14642 | } |
14643 | ||
a8f73d4b MM |
14644 | /* Free the language-specific parts of F, now that we've finished |
14645 | compiling the function. */ | |
db5ae43f | 14646 | |
99dccabc MM |
14647 | static void |
14648 | pop_cp_function_context (f) | |
14649 | struct function *f; | |
db5ae43f | 14650 | { |
d658cd4c | 14651 | if (f->language) |
8e4ce833 JJ |
14652 | { |
14653 | struct cp_language_function *cp = | |
14654 | (struct cp_language_function *) f->language; | |
14655 | if (cp->x_local_names) | |
14656 | VARRAY_FREE (cp->x_local_names); | |
14657 | free (f->language); | |
14658 | } | |
99dccabc | 14659 | f->language = 0; |
db5ae43f | 14660 | } |
ebfc180f | 14661 | |
59026e79 | 14662 | /* Mark P for GC. */ |
4519c0a8 | 14663 | |
59026e79 MM |
14664 | static void |
14665 | mark_lang_function (p) | |
ae499cce | 14666 | struct cp_language_function *p; |
59026e79 | 14667 | { |
87e3dbc9 MM |
14668 | if (!p) |
14669 | return; | |
14670 | ||
8f17b5c5 MM |
14671 | mark_c_language_function (&p->base); |
14672 | ||
4519c0a8 | 14673 | ggc_mark_tree (p->x_dtor_label); |
4519c0a8 MM |
14674 | ggc_mark_tree (p->x_current_class_ptr); |
14675 | ggc_mark_tree (p->x_current_class_ref); | |
52a11cbf | 14676 | ggc_mark_tree (p->x_eh_spec_block); |
8e4ce833 | 14677 | ggc_mark_tree_varray (p->x_local_names); |
4519c0a8 | 14678 | |
6625cdb5 | 14679 | mark_named_label_lists (&p->x_named_labels, &p->x_named_label_uses); |
a8f73d4b | 14680 | mark_binding_level (&p->bindings); |
0a01c261 | 14681 | mark_pending_inlines (&p->unparsed_inlines); |
4519c0a8 MM |
14682 | } |
14683 | ||
59026e79 MM |
14684 | /* Mark the language-specific data in F for GC. */ |
14685 | ||
a09ba2e0 | 14686 | static void |
59026e79 MM |
14687 | mark_cp_function_context (f) |
14688 | struct function *f; | |
14689 | { | |
ae499cce | 14690 | mark_lang_function ((struct cp_language_function *) f->language); |
59026e79 | 14691 | } |
4519c0a8 | 14692 | |
4519c0a8 | 14693 | void |
4f0ade92 | 14694 | cxx_mark_tree (t) |
4519c0a8 MM |
14695 | tree t; |
14696 | { | |
14697 | enum tree_code code = TREE_CODE (t); | |
14698 | if (code == IDENTIFIER_NODE) | |
14699 | { | |
14700 | struct lang_identifier *li = (struct lang_identifier *) t; | |
14701 | struct lang_id2 *li2 = li->x; | |
14702 | ggc_mark_tree (li->namespace_bindings); | |
a8f73d4b | 14703 | ggc_mark_tree (li->bindings); |
4519c0a8 MM |
14704 | ggc_mark_tree (li->class_value); |
14705 | ggc_mark_tree (li->class_template_info); | |
14706 | ||
14707 | if (li2) | |
14708 | { | |
14709 | ggc_mark_tree (li2->label_value); | |
14710 | ggc_mark_tree (li2->implicit_decl); | |
14711 | ggc_mark_tree (li2->error_locus); | |
14712 | } | |
14713 | } | |
87e3dbc9 MM |
14714 | else if (code == CPLUS_BINDING) |
14715 | { | |
14716 | if (BINDING_HAS_LEVEL_P (t)) | |
14717 | mark_binding_level (&BINDING_LEVEL (t)); | |
14718 | else | |
14719 | ggc_mark_tree (BINDING_SCOPE (t)); | |
14720 | ggc_mark_tree (BINDING_VALUE (t)); | |
14721 | } | |
14722 | else if (code == OVERLOAD) | |
14723 | ggc_mark_tree (OVL_FUNCTION (t)); | |
14724 | else if (code == TEMPLATE_PARM_INDEX) | |
14725 | ggc_mark_tree (TEMPLATE_PARM_DECL (t)); | |
4519c0a8 MM |
14726 | else if (TREE_CODE_CLASS (code) == 'd') |
14727 | { | |
14728 | struct lang_decl *ld = DECL_LANG_SPECIFIC (t); | |
14729 | ||
14730 | if (ld) | |
14731 | { | |
d60f72ae | 14732 | ggc_mark (ld); |
8f17b5c5 | 14733 | c_mark_lang_decl (&ld->decl_flags.base); |
5362b086 | 14734 | if (!DECL_GLOBAL_CTOR_P (t) |
6462c441 | 14735 | && !DECL_GLOBAL_DTOR_P (t) |
8e4ce833 JJ |
14736 | && !DECL_THUNK_P (t) |
14737 | && !DECL_DISCRIMINATOR_P (t)) | |
af3b4e59 | 14738 | ggc_mark_tree (ld->decl_flags.u2.access); |
31f8e4f3 MM |
14739 | else if (DECL_THUNK_P (t)) |
14740 | ggc_mark_tree (ld->decl_flags.u2.vcall_offset); | |
4519c0a8 MM |
14741 | if (TREE_CODE (t) != NAMESPACE_DECL) |
14742 | ggc_mark_tree (ld->decl_flags.u.template_info); | |
16bb3387 JM |
14743 | else |
14744 | mark_binding_level (&NAMESPACE_LEVEL (t)); | |
4519c0a8 MM |
14745 | if (CAN_HAVE_FULL_LANG_DECL_P (t)) |
14746 | { | |
4519c0a8 | 14747 | ggc_mark_tree (ld->befriending_classes); |
8f17b5c5 | 14748 | ggc_mark_tree (ld->context); |
db9b2174 | 14749 | ggc_mark_tree (ld->cloned_function); |
4519c0a8 MM |
14750 | if (TREE_CODE (t) == TYPE_DECL) |
14751 | ggc_mark_tree (ld->u.sorted_fields); | |
59026e79 MM |
14752 | else if (TREE_CODE (t) == FUNCTION_DECL |
14753 | && !DECL_PENDING_INLINE_P (t)) | |
14754 | mark_lang_function (DECL_SAVED_FUNCTION_DATA (t)); | |
4519c0a8 MM |
14755 | } |
14756 | } | |
14757 | } | |
14758 | else if (TREE_CODE_CLASS (code) == 't') | |
14759 | { | |
14760 | struct lang_type *lt = TYPE_LANG_SPECIFIC (t); | |
14761 | ||
68642fb6 | 14762 | if (lt && !(TREE_CODE (t) == POINTER_TYPE |
87e3dbc9 | 14763 | && TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)) |
4519c0a8 | 14764 | { |
c27db0df | 14765 | ggc_mark (lt); |
911a71a7 | 14766 | ggc_mark_tree (lt->primary_base); |
4519c0a8 MM |
14767 | ggc_mark_tree (lt->vfields); |
14768 | ggc_mark_tree (lt->vbases); | |
14769 | ggc_mark_tree (lt->tags); | |
4519c0a8 | 14770 | ggc_mark_tree (lt->size); |
fee7654e | 14771 | ggc_mark_tree (lt->pure_virtuals); |
4519c0a8 MM |
14772 | ggc_mark_tree (lt->friend_classes); |
14773 | ggc_mark_tree (lt->rtti); | |
14774 | ggc_mark_tree (lt->methods); | |
14775 | ggc_mark_tree (lt->template_info); | |
87e3dbc9 | 14776 | ggc_mark_tree (lt->befriending_classes); |
4519c0a8 | 14777 | } |
87e3dbc9 MM |
14778 | else if (lt) |
14779 | /* In the case of pointer-to-member function types, the | |
14780 | TYPE_LANG_SPECIFIC is really just a tree. */ | |
14781 | ggc_mark_tree ((tree) lt); | |
4519c0a8 MM |
14782 | } |
14783 | } | |
5fd8e536 JM |
14784 | |
14785 | /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since | |
14786 | the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */ | |
14787 | ||
14788 | tree | |
14789 | identifier_global_value (t) | |
14790 | tree t; | |
14791 | { | |
14792 | return IDENTIFIER_GLOBAL_VALUE (t); | |
14793 | } | |
d363e7bf | 14794 | |
eaa7c03f JM |
14795 | /* Build the void_list_node (void_type_node having been created). */ |
14796 | tree | |
14797 | build_void_list_node () | |
14798 | { | |
14799 | tree t = build_tree_list (NULL_TREE, void_type_node); | |
14800 | TREE_PARMLIST (t) = 1; | |
14801 | return t; | |
14802 | } | |
14803 | ||
d363e7bf AJ |
14804 | static int |
14805 | cp_missing_noreturn_ok_p (decl) | |
14806 | tree decl; | |
14807 | { | |
14808 | /* A missing noreturn is ok for the `main' function. */ | |
92643fea | 14809 | return DECL_MAIN_P (decl); |
d363e7bf | 14810 | } |