]>
Commit | Line | Data |
---|---|---|
e5e809f4 | 1 | /* Process declarations and variables for C compiler. |
06ceef4e RK |
2 | Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000 |
3 | 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" | |
35 | #include "flags.h" | |
36 | #include "cp-tree.h" | |
37 | #include "decl.h" | |
38 | #include "lex.h" | |
8d08fdba MS |
39 | #include <signal.h> |
40 | #include "obstack.h" | |
72b7eeff | 41 | #include "defaults.h" |
49c249e1 JM |
42 | #include "output.h" |
43 | #include "except.h" | |
54f92bfb | 44 | #include "toplev.h" |
7ddedda4 | 45 | #include "../hash.h" |
091f9839 | 46 | #include "defaults.h" |
4519c0a8 | 47 | #include "ggc.h" |
8d08fdba MS |
48 | |
49 | #define obstack_chunk_alloc xmalloc | |
50 | #define obstack_chunk_free free | |
51 | ||
52 | extern struct obstack permanent_obstack; | |
53 | ||
54 | extern int current_class_depth; | |
55 | ||
e1cd6e56 MS |
56 | extern tree static_ctors, static_dtors; |
57 | ||
30394414 JM |
58 | extern tree global_namespace; |
59 | ||
158991b7 | 60 | extern int (*valid_lang_attribute) PARAMS ((tree, tree, tree, tree)); |
62c154ed | 61 | |
47e19316 | 62 | /* Use garbage collection. */ |
fc6af6e3 | 63 | |
47e19316 | 64 | int ggc_p = 1; |
fc6af6e3 | 65 | |
8d08fdba MS |
66 | #ifndef WCHAR_UNSIGNED |
67 | #define WCHAR_UNSIGNED 0 | |
68 | #endif | |
69 | ||
81b3411c BS |
70 | #ifndef CHAR_TYPE_SIZE |
71 | #define CHAR_TYPE_SIZE BITS_PER_UNIT | |
8d08fdba MS |
72 | #endif |
73 | ||
255512c1 | 74 | #ifndef BOOL_TYPE_SIZE |
e1cd6e56 | 75 | #ifdef SLOW_BYTE_ACCESS |
2ef16140 MM |
76 | /* In the new ABI, `bool' has size and alignment `1', on all |
77 | platforms. */ | |
78 | #define BOOL_TYPE_SIZE \ | |
79 | ((SLOW_BYTE_ACCESS && !flag_new_abi) ? (POINTER_SIZE) : (CHAR_TYPE_SIZE)) | |
e1cd6e56 | 80 | #else |
d2e5ee5c | 81 | #define BOOL_TYPE_SIZE CHAR_TYPE_SIZE |
e1cd6e56 | 82 | #endif |
255512c1 JM |
83 | #endif |
84 | ||
8d08fdba MS |
85 | /* We let tm.h override the types used here, to handle trivial differences |
86 | such as the choice of unsigned int or long unsigned int for size_t. | |
87 | When machines start needing nontrivial differences in the size type, | |
88 | it would be best to do something here to figure out automatically | |
89 | from other information what type to use. */ | |
90 | ||
91 | #ifndef SIZE_TYPE | |
92 | #define SIZE_TYPE "long unsigned int" | |
93 | #endif | |
94 | ||
95 | #ifndef PTRDIFF_TYPE | |
96 | #define PTRDIFF_TYPE "long int" | |
97 | #endif | |
98 | ||
99 | #ifndef WCHAR_TYPE | |
100 | #define WCHAR_TYPE "int" | |
101 | #endif | |
102 | ||
158991b7 KG |
103 | static tree grokparms PARAMS ((tree, int)); |
104 | static const char *redeclaration_error_message PARAMS ((tree, tree)); | |
8d08fdba | 105 | |
158991b7 | 106 | static void push_binding_level PARAMS ((struct binding_level *, int, |
49c249e1 | 107 | int)); |
158991b7 KG |
108 | static void pop_binding_level PARAMS ((void)); |
109 | static void suspend_binding_level PARAMS ((void)); | |
110 | static void resume_binding_level PARAMS ((struct binding_level *)); | |
111 | static struct binding_level *make_binding_level PARAMS ((void)); | |
112 | static void declare_namespace_level PARAMS ((void)); | |
113 | static void signal_catch PARAMS ((int)) ATTRIBUTE_NORETURN; | |
114 | static void storedecls PARAMS ((tree)); | |
115 | static void require_complete_types_for_parms PARAMS ((tree)); | |
158991b7 KG |
116 | static int ambi_op_p PARAMS ((tree)); |
117 | static int unary_op_p PARAMS ((tree)); | |
118 | static tree store_bindings PARAMS ((tree, tree)); | |
119 | static tree lookup_tag_reverse PARAMS ((tree, tree)); | |
120 | static tree obscure_complex_init PARAMS ((tree, tree)); | |
121 | static tree maybe_build_cleanup_1 PARAMS ((tree, tree)); | |
122 | static tree lookup_name_real PARAMS ((tree, int, int, int)); | |
123 | static void warn_extern_redeclared_static PARAMS ((tree, tree)); | |
124 | static void grok_reference_init PARAMS ((tree, tree, tree)); | |
125 | static tree grokfndecl PARAMS ((tree, tree, tree, tree, int, | |
2c73f9f5 | 126 | enum overload_flags, tree, |
7a8f9fa9 | 127 | tree, int, int, int, int, int, int, tree)); |
158991b7 KG |
128 | static tree grokvardecl PARAMS ((tree, tree, RID_BIT_TYPE *, int, int, tree)); |
129 | static tree lookup_tag PARAMS ((enum tree_code, tree, | |
49c249e1 JM |
130 | struct binding_level *, int)); |
131 | static void set_identifier_type_value_with_scope | |
158991b7 KG |
132 | PARAMS ((tree, tree, struct binding_level *)); |
133 | static void record_builtin_type PARAMS ((enum rid, const char *, tree)); | |
134 | static void record_unknown_type PARAMS ((tree, const char *)); | |
0c11ada6 | 135 | static tree build_library_fn_1 PARAMS ((tree, tree)); |
158991b7 KG |
136 | static int member_function_or_else PARAMS ((tree, tree, enum overload_flags)); |
137 | static void bad_specifiers PARAMS ((tree, const char *, int, int, int, int, | |
49c249e1 | 138 | int)); |
158991b7 KG |
139 | static void lang_print_error_function PARAMS ((const char *)); |
140 | static tree maybe_process_template_type_declaration PARAMS ((tree, int, struct binding_level*)); | |
141 | static void check_for_uninitialized_const_var PARAMS ((tree)); | |
142 | static unsigned long typename_hash PARAMS ((hash_table_key)); | |
143 | static boolean typename_compare PARAMS ((hash_table_key, hash_table_key)); | |
144 | static void push_binding PARAMS ((tree, tree, struct binding_level*)); | |
145 | static int add_binding PARAMS ((tree, tree)); | |
146 | static void pop_binding PARAMS ((tree, tree)); | |
147 | static tree local_variable_p_walkfn PARAMS ((tree *, int *, void *)); | |
148 | static tree find_binding PARAMS ((tree, tree)); | |
149 | static tree select_decl PARAMS ((tree, int)); | |
150 | static int lookup_flags PARAMS ((int, int)); | |
151 | static tree qualify_lookup PARAMS ((tree, int)); | |
152 | static tree record_builtin_java_type PARAMS ((const char *, int)); | |
153 | static const char *tag_name PARAMS ((enum tag_types code)); | |
154 | static void find_class_binding_level PARAMS ((void)); | |
155 | static struct binding_level *innermost_nonclass_level PARAMS ((void)); | |
156 | static void warn_about_implicit_typename_lookup PARAMS ((tree, tree)); | |
157 | static int walk_namespaces_r PARAMS ((tree, walk_namespaces_fn, void *)); | |
158 | static int walk_globals_r PARAMS ((tree, void *)); | |
159 | static void add_decl_to_level PARAMS ((tree, struct binding_level *)); | |
160 | static tree make_label_decl PARAMS ((tree, int)); | |
161 | static void pop_label PARAMS ((tree)); | |
162 | static void pop_labels PARAMS ((tree)); | |
163 | static void maybe_deduce_size_from_array_init PARAMS ((tree, tree)); | |
164 | static void layout_var_decl PARAMS ((tree)); | |
165 | static void maybe_commonize_var PARAMS ((tree)); | |
166 | static tree check_initializer PARAMS ((tree, tree)); | |
167 | static void make_rtl_for_nonlocal_decl PARAMS ((tree, tree, const char *)); | |
168 | static void push_cp_function_context PARAMS ((struct function *)); | |
169 | static void pop_cp_function_context PARAMS ((struct function *)); | |
170 | static void mark_binding_level PARAMS ((void *)); | |
171 | static void mark_cp_function_context PARAMS ((struct function *)); | |
172 | static void mark_saved_scope PARAMS ((void *)); | |
173 | static void mark_lang_function PARAMS ((struct language_function *)); | |
174 | static void mark_stmt_tree PARAMS ((struct stmt_tree *)); | |
175 | static void save_function_data PARAMS ((tree)); | |
176 | static void check_function_type PARAMS ((tree)); | |
177 | static void destroy_local_var PARAMS ((tree)); | |
178 | static void finish_constructor_body PARAMS ((void)); | |
179 | static void finish_destructor_body PARAMS ((void)); | |
180 | static tree create_array_type_for_decl PARAMS ((tree, tree, tree)); | |
181 | static tree get_atexit_node PARAMS ((void)); | |
182 | static tree get_dso_handle_node PARAMS ((void)); | |
183 | static tree start_cleanup_fn PARAMS ((void)); | |
184 | static void end_cleanup_fn PARAMS ((void)); | |
2ce07e2d | 185 | static tree cp_make_fname_decl PARAMS ((tree, const char *, int)); |
8d08fdba | 186 | |
69ac77ce | 187 | #if defined (DEBUG_CP_BINDING_LEVELS) |
158991b7 | 188 | static void indent PARAMS ((void)); |
69ac77ce JL |
189 | #endif |
190 | ||
8d08fdba MS |
191 | /* Erroneous argument lists can use this *IFF* they do not modify it. */ |
192 | tree error_mark_list; | |
193 | ||
7f4edbcb | 194 | /* The following symbols are subsumed in the cp_global_trees array, and |
68642fb6 | 195 | listed here individually for documentation purposes. |
8d08fdba | 196 | |
7f4edbcb BS |
197 | C++ extensions |
198 | tree wchar_decl_node; | |
199 | tree void_zero_node; | |
37c46b43 | 200 | |
7f4edbcb BS |
201 | tree vtable_entry_type; |
202 | tree delta_type_node; | |
203 | #if 0 | |
204 | Old rtti stuff. | |
205 | tree __baselist_desc_type_node; | |
206 | tree __i_desc_type_node, __m_desc_type_node; | |
207 | tree __t_desc_array_type, __i_desc_array_type, __m_desc_array_type; | |
946dc1c8 | 208 | #endif |
7f4edbcb BS |
209 | tree __t_desc_type_node; |
210 | #if 0 | |
211 | tree __tp_desc_type_node; | |
212 | #endif | |
db1147b2 | 213 | tree ti_desc_type_node; |
1f4cb92b | 214 | tree bltn_desc_type_node, ptr_desc_type_node; |
db1147b2 NS |
215 | tree ary_desc_type_node, func_desc_type_node, enum_desc_type_node; |
216 | tree class_desc_type_node, si_class_desc_type_node, vmi_class_desc_type_node; | |
217 | tree ptmd_desc_type_node; | |
218 | tree base_desc_type_node; | |
7f4edbcb BS |
219 | #if 0 |
220 | Not needed yet? May be needed one day? | |
221 | tree __bltn_desc_array_type, __user_desc_array_type, __class_desc_array_type; | |
222 | tree __ptr_desc_array_type, __attr_dec_array_type, __func_desc_array_type; | |
223 | tree __ptmf_desc_array_type, __ptmd_desc_array_type; | |
946dc1c8 | 224 | #endif |
8d08fdba | 225 | |
7f4edbcb BS |
226 | tree class_type_node, record_type_node, union_type_node, enum_type_node; |
227 | tree unknown_type_node; | |
8d08fdba | 228 | |
7f4edbcb | 229 | Array type `vtable_entry_type[]' |
8d08fdba | 230 | |
7f4edbcb BS |
231 | tree vtbl_type_node; |
232 | tree vtbl_ptr_type_node; | |
8d08fdba | 233 | |
7f4edbcb | 234 | Nnamespace std |
8d08fdba | 235 | |
7f4edbcb | 236 | tree std_node; |
8d08fdba | 237 | |
7f4edbcb BS |
238 | A FUNCTION_DECL which can call `abort'. Not necessarily the |
239 | one that the user will declare, but sufficient to be called | |
240 | by routines that want to abort the program. | |
8d08fdba | 241 | |
7f4edbcb | 242 | tree abort_fndecl; |
8d08fdba | 243 | |
7f4edbcb | 244 | The FUNCTION_DECL for the default `::operator delete'. |
2986ae00 | 245 | |
7f4edbcb | 246 | tree global_delete_fndecl; |
8d08fdba | 247 | |
7f4edbcb | 248 | Used by RTTI |
db1147b2 NS |
249 | tree type_info_type_node, tinfo_decl_id, tinfo_decl_type; |
250 | tree tinfo_var_id; | |
8d08fdba | 251 | |
7f4edbcb | 252 | */ |
8d08fdba | 253 | |
7f4edbcb | 254 | tree cp_global_trees[CPTI_MAX]; |
8d08fdba | 255 | |
2c73f9f5 | 256 | /* Indicates that there is a type value in some namespace, although |
7f4edbcb | 257 | that is not necessarily in scope at the moment. */ |
2c73f9f5 ML |
258 | |
259 | static tree global_type_node; | |
260 | ||
7f4edbcb | 261 | /* Namespace std. */ |
0dde4175 | 262 | int in_std; |
6633d636 | 263 | |
3e3f722c ML |
264 | /* Expect only namespace names now. */ |
265 | static int only_namespace_names; | |
266 | ||
8d08fdba MS |
267 | /* If original DECL_RESULT of current function was a register, |
268 | but due to being an addressable named return value, would up | |
269 | on the stack, this variable holds the named return value's | |
270 | original location. */ | |
8d08fdba | 271 | |
4519c0a8 | 272 | #define original_result_rtx cp_function_chain->x_result_rtx |
8d08fdba | 273 | |
e349ee73 MS |
274 | struct named_label_list |
275 | { | |
276 | struct binding_level *binding_level; | |
277 | tree names_in_scope; | |
278 | tree label_decl; | |
9c0758dd | 279 | const char *filename_o_goto; |
e349ee73 MS |
280 | int lineno_o_goto; |
281 | struct named_label_list *next; | |
282 | }; | |
283 | ||
ed5511d9 MM |
284 | /* Used only for jumps to as-yet undefined labels, since jumps to |
285 | defined labels can have their validity checked by stmt.c. */ | |
8d08fdba | 286 | |
ed5511d9 | 287 | #define named_label_uses cp_function_chain->x_named_label_uses |
8d08fdba MS |
288 | |
289 | /* A list of objects which have constructors or destructors | |
290 | which reside in the global scope. The decl is stored in | |
291 | the TREE_VALUE slot and the initializer is stored | |
292 | in the TREE_PURPOSE slot. */ | |
293 | tree static_aggregates; | |
294 | ||
8d08fdba MS |
295 | /* -- end of C++ */ |
296 | ||
81b3411c | 297 | /* A node for the integer constants 2, and 3. */ |
d11ad92e | 298 | |
81b3411c | 299 | tree integer_two_node, integer_three_node; |
8d08fdba | 300 | |
8d08fdba MS |
301 | /* Parsing a function declarator leaves here a chain of structure |
302 | and enum types declared in the parmlist. */ | |
303 | ||
304 | static tree last_function_parm_tags; | |
305 | ||
8d08fdba | 306 | /* Similar, for last_function_parm_tags. */ |
9cd64686 | 307 | tree last_function_parms; |
8d08fdba MS |
308 | static tree current_function_parm_tags; |
309 | ||
310 | /* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function | |
311 | that have names. Here so we can clear out their names' definitions | |
acef433b MM |
312 | at the end of the function. The TREE_VALUE is a LABEL_DECL; the |
313 | TREE_PURPOSE is the previous binding of the label. */ | |
8d08fdba | 314 | |
4519c0a8 | 315 | #define named_labels cp_function_chain->x_named_labels |
8d08fdba | 316 | |
8d08fdba MS |
317 | /* Set to 0 at beginning of a function definition, and whenever |
318 | a label (case or named) is defined. Set to value of expression | |
319 | returned from function when that value can be transformed into | |
320 | a named return value. */ | |
321 | ||
322 | tree current_function_return_value; | |
323 | ||
83309c26 | 324 | /* Nonzero means use the ISO C99 dialect of C. */ |
68642fb6 | 325 | |
83309c26 | 326 | int flag_isoc99; |
68642fb6 | 327 | |
8d08fdba MS |
328 | /* Nonzero means give `double' the same size as `float'. */ |
329 | ||
330 | extern int flag_short_double; | |
331 | ||
332 | /* Nonzero means don't recognize any builtin functions. */ | |
333 | ||
334 | extern int flag_no_builtin; | |
335 | ||
00595019 MS |
336 | /* Nonzero means don't recognize the non-ANSI builtin functions. |
337 | -ansi sets this. */ | |
338 | ||
339 | extern int flag_no_nonansi_builtin; | |
340 | ||
8d08fdba | 341 | /* Nonzero if we want to support huge (> 2^(sizeof(short)*8-1) bytes) |
e92cc029 | 342 | objects. */ |
8d08fdba MS |
343 | extern int flag_huge_objects; |
344 | ||
345 | /* Nonzero if we want to conserve space in the .o files. We do this | |
346 | by putting uninitialized data and runtime initialized data into | |
ddd5a7c1 | 347 | .common instead of .data at the expense of not flagging multiple |
8d08fdba MS |
348 | definitions. */ |
349 | extern int flag_conserve_space; | |
8d08fdba | 350 | \f |
51c184be | 351 | /* C and C++ flags are in decl2.c. */ |
8d08fdba | 352 | |
51c184be | 353 | /* Flag used when debugging spew.c */ |
8d08fdba MS |
354 | |
355 | extern int spew_debug; | |
356 | ||
e92cc029 MS |
357 | /* A expression of value 0 with the same precision as a sizetype |
358 | node, but signed. */ | |
359 | tree signed_size_zero_node; | |
360 | ||
0c8feefe MM |
361 | /* The name of the anonymous namespace, throughout this translation |
362 | unit. */ | |
363 | tree anonymous_namespace_name; | |
364 | ||
8d08fdba MS |
365 | \f |
366 | /* For each binding contour we allocate a binding_level structure | |
e92cc029 MS |
367 | which records the names defined in that contour. |
368 | Contours include: | |
369 | 0) the global one | |
370 | 1) one for each function definition, | |
371 | where internal declarations of the parameters appear. | |
372 | 2) one for each compound statement, | |
373 | to record its declarations. | |
374 | ||
375 | The current meaning of a name can be found by searching the levels | |
376 | from the current one out to the global one. | |
377 | ||
378 | Off to the side, may be the class_binding_level. This exists only | |
379 | to catch class-local declarations. It is otherwise nonexistent. | |
380 | ||
381 | Also there may be binding levels that catch cleanups that must be | |
d8f8dca1 MM |
382 | run when exceptions occur. Thus, to see whether a name is bound in |
383 | the current scope, it is not enough to look in the | |
384 | CURRENT_BINDING_LEVEL. You should use lookup_name_current_level | |
385 | instead. */ | |
8d08fdba MS |
386 | |
387 | /* Note that the information in the `names' component of the global contour | |
388 | is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */ | |
389 | ||
390 | struct binding_level | |
391 | { | |
392 | /* A chain of _DECL nodes for all variables, constants, functions, | |
e92cc029 | 393 | and typedef types. These are in the reverse of the order |
f181d4ae MM |
394 | supplied. There may be OVERLOADs on this list, too, but they |
395 | are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD. */ | |
8d08fdba MS |
396 | tree names; |
397 | ||
e92cc029 MS |
398 | /* A list of structure, union and enum definitions, for looking up |
399 | tag names. | |
400 | It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name, | |
401 | or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE, | |
402 | or ENUMERAL_TYPE node. | |
403 | ||
404 | C++: the TREE_VALUE nodes can be simple types for | |
405 | component_bindings. */ | |
8d08fdba MS |
406 | tree tags; |
407 | ||
2c73f9f5 ML |
408 | /* A list of USING_DECL nodes. */ |
409 | tree usings; | |
410 | ||
ea9635c7 ML |
411 | /* A list of used namespaces. PURPOSE is the namespace, |
412 | VALUE the common ancestor with this binding_level's namespace. */ | |
413 | tree using_directives; | |
414 | ||
f181d4ae MM |
415 | /* If this binding level is the binding level for a class, then |
416 | class_shadowed is a TREE_LIST. The TREE_PURPOSE of each node | |
417 | is the name of an entity bound in the class; the TREE_VALUE is | |
418 | the IDENTIFIER_CLASS_VALUE before we entered the class. Thus, | |
419 | when leaving class scope, we can restore the | |
d8f8dca1 MM |
420 | IDENTIFIER_CLASS_VALUE by walking this list. The TREE_TYPE is |
421 | the DECL bound by this name in the class. */ | |
8d08fdba MS |
422 | tree class_shadowed; |
423 | ||
f181d4ae MM |
424 | /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and |
425 | is used for all binding levels. */ | |
8d08fdba MS |
426 | tree type_shadowed; |
427 | ||
acef433b MM |
428 | /* A TREE_LIST. Each TREE_VALUE is the LABEL_DECL for a local |
429 | label in this scope. The TREE_PURPOSE is the previous value of | |
430 | the IDENTIFIER_LABEL VALUE. */ | |
431 | tree shadowed_labels; | |
432 | ||
8d08fdba MS |
433 | /* For each level (except not the global one), |
434 | a chain of BLOCK nodes for all the levels | |
435 | that were entered and exited one level down. */ | |
436 | tree blocks; | |
437 | ||
438 | /* The BLOCK node for this level, if one has been preallocated. | |
439 | If 0, the BLOCK is allocated (if needed) when the level is popped. */ | |
440 | tree this_block; | |
441 | ||
70adf8a9 JM |
442 | /* The _TYPE node for this level, if parm_flag == 2. */ |
443 | tree this_class; | |
444 | ||
8d08fdba MS |
445 | /* The binding level which this one is contained in (inherits from). */ |
446 | struct binding_level *level_chain; | |
447 | ||
f30432d7 | 448 | /* List of decls in `names' that have incomplete |
8d08fdba | 449 | structure or union types. */ |
f30432d7 | 450 | tree incomplete; |
8d08fdba | 451 | |
8d6e462b | 452 | /* List of VAR_DECLS saved from a previous for statement. |
cab1f180 | 453 | These would be dead in ISO-conforming code, but might |
f181d4ae MM |
454 | be referenced in ARM-era code. These are stored in a |
455 | TREE_LIST; the TREE_VALUE is the actual declaration. */ | |
8d6e462b PB |
456 | tree dead_vars_from_for; |
457 | ||
8d08fdba | 458 | /* 1 for the level that holds the parameters of a function. |
74b846e0 MM |
459 | 2 for the level that holds a class declaration. */ |
460 | unsigned parm_flag : 2; | |
8d08fdba MS |
461 | |
462 | /* 1 means make a BLOCK for this level regardless of all else. | |
463 | 2 for temporary binding contours created by the compiler. */ | |
74b846e0 | 464 | unsigned keep : 2; |
8d08fdba MS |
465 | |
466 | /* Nonzero if this level "doesn't exist" for tags. */ | |
467 | unsigned tag_transparent : 1; | |
468 | ||
469 | /* Nonzero if this level can safely have additional | |
470 | cleanup-needing variables added to it. */ | |
471 | unsigned more_cleanups_ok : 1; | |
472 | unsigned have_cleanups : 1; | |
473 | ||
74b846e0 | 474 | /* Nonzero if this scope is for storing the decls for template |
5566b478 MS |
475 | parameters and generic decls; these decls will be discarded and |
476 | replaced with a TEMPLATE_DECL. */ | |
74b846e0 MM |
477 | unsigned template_parms_p : 1; |
478 | ||
479 | /* Nonzero if this scope corresponds to the `<>' in a | |
480 | `template <>' clause. Whenever this flag is set, | |
481 | TEMPLATE_PARMS_P will be set as well. */ | |
482 | unsigned template_spec_p : 1; | |
8d08fdba | 483 | |
a9aedbc2 MS |
484 | /* This is set for a namespace binding level. */ |
485 | unsigned namespace_p : 1; | |
486 | ||
2ee887f2 | 487 | /* True if this level is that of a for-statement where we need to |
cab1f180 | 488 | worry about ambiguous (ARM or ISO) scope rules. */ |
8d6e462b PB |
489 | unsigned is_for_scope : 1; |
490 | ||
227cf171 JM |
491 | /* True if this level corresponds to an EH region, as for a try block. */ |
492 | unsigned eh_region : 1; | |
493 | ||
74b846e0 | 494 | /* Four bits left for this word. */ |
8d08fdba MS |
495 | |
496 | #if defined(DEBUG_CP_BINDING_LEVELS) | |
497 | /* Binding depth at which this level began. */ | |
498 | unsigned binding_depth; | |
499 | #endif /* defined(DEBUG_CP_BINDING_LEVELS) */ | |
500 | }; | |
501 | ||
502 | #define NULL_BINDING_LEVEL ((struct binding_level *) NULL) | |
68642fb6 | 503 | |
8f032717 | 504 | /* The binding level currently in effect. */ |
8d08fdba | 505 | |
a8f73d4b | 506 | #define current_binding_level \ |
01d939e8 | 507 | (cfun \ |
a8f73d4b MM |
508 | ? cp_function_chain->bindings \ |
509 | : scope_chain->bindings) | |
8d08fdba MS |
510 | |
511 | /* The binding level of the current class, if any. */ | |
512 | ||
9cd64686 | 513 | #define class_binding_level scope_chain->class_bindings |
8d08fdba | 514 | |
8d08fdba MS |
515 | /* A chain of binding_level structures awaiting reuse. */ |
516 | ||
517 | static struct binding_level *free_binding_level; | |
518 | ||
519 | /* The outermost binding level, for names of file scope. | |
520 | This is created when the compiler is started and exists | |
521 | through the entire run. */ | |
522 | ||
523 | static struct binding_level *global_binding_level; | |
524 | ||
8d08fdba MS |
525 | /* Nonzero means unconditionally make a BLOCK for the next level pushed. */ |
526 | ||
527 | static int keep_next_level_flag; | |
528 | ||
529 | #if defined(DEBUG_CP_BINDING_LEVELS) | |
530 | static int binding_depth = 0; | |
531 | static int is_class_level = 0; | |
532 | ||
533 | static void | |
534 | indent () | |
535 | { | |
536 | register unsigned i; | |
537 | ||
538 | for (i = 0; i < binding_depth*2; i++) | |
539 | putc (' ', stderr); | |
540 | } | |
541 | #endif /* defined(DEBUG_CP_BINDING_LEVELS) */ | |
542 | ||
158991b7 | 543 | static tree pushdecl_with_scope PARAMS ((tree, struct binding_level *)); |
8d08fdba MS |
544 | |
545 | static void | |
546 | push_binding_level (newlevel, tag_transparent, keep) | |
547 | struct binding_level *newlevel; | |
548 | int tag_transparent, keep; | |
549 | { | |
550 | /* Add this level to the front of the chain (stack) of levels that | |
551 | are active. */ | |
acef433b | 552 | bzero ((char*) newlevel, sizeof (struct binding_level)); |
8f032717 | 553 | newlevel->level_chain = current_binding_level; |
8d08fdba MS |
554 | current_binding_level = newlevel; |
555 | newlevel->tag_transparent = tag_transparent; | |
556 | newlevel->more_cleanups_ok = 1; | |
227cf171 JM |
557 | |
558 | /* We are called before expand_start_bindings, but after | |
559 | expand_eh_region_start for a try block; so we check this now, | |
560 | before the EH block is covered up. */ | |
561 | newlevel->eh_region = is_eh_region (); | |
562 | ||
8d08fdba MS |
563 | newlevel->keep = keep; |
564 | #if defined(DEBUG_CP_BINDING_LEVELS) | |
565 | newlevel->binding_depth = binding_depth; | |
566 | indent (); | |
567 | fprintf (stderr, "push %s level 0x%08x line %d\n", | |
568 | (is_class_level) ? "class" : "block", newlevel, lineno); | |
569 | is_class_level = 0; | |
570 | binding_depth++; | |
571 | #endif /* defined(DEBUG_CP_BINDING_LEVELS) */ | |
572 | } | |
573 | ||
8f032717 MM |
574 | /* Find the innermost enclosing class scope, and reset |
575 | CLASS_BINDING_LEVEL appropriately. */ | |
576 | ||
577 | static void | |
578 | find_class_binding_level () | |
579 | { | |
580 | struct binding_level *level = current_binding_level; | |
581 | ||
582 | while (level && level->parm_flag != 2) | |
583 | level = level->level_chain; | |
584 | if (level && level->parm_flag == 2) | |
585 | class_binding_level = level; | |
586 | else | |
587 | class_binding_level = 0; | |
588 | } | |
589 | ||
8d08fdba MS |
590 | static void |
591 | pop_binding_level () | |
592 | { | |
8d08fdba MS |
593 | if (global_binding_level) |
594 | { | |
2c73f9f5 | 595 | /* Cannot pop a level, if there are none left to pop. */ |
8d08fdba MS |
596 | if (current_binding_level == global_binding_level) |
597 | my_friendly_abort (123); | |
598 | } | |
599 | /* Pop the current level, and free the structure for reuse. */ | |
600 | #if defined(DEBUG_CP_BINDING_LEVELS) | |
601 | binding_depth--; | |
602 | indent (); | |
603 | fprintf (stderr, "pop %s level 0x%08x line %d\n", | |
604 | (is_class_level) ? "class" : "block", | |
605 | current_binding_level, lineno); | |
606 | if (is_class_level != (current_binding_level == class_binding_level)) | |
9e9ff709 MS |
607 | { |
608 | indent (); | |
609 | fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n"); | |
610 | } | |
8d08fdba MS |
611 | is_class_level = 0; |
612 | #endif /* defined(DEBUG_CP_BINDING_LEVELS) */ | |
613 | { | |
614 | register struct binding_level *level = current_binding_level; | |
615 | current_binding_level = current_binding_level->level_chain; | |
616 | level->level_chain = free_binding_level; | |
617 | #if 0 /* defined(DEBUG_CP_BINDING_LEVELS) */ | |
618 | if (level->binding_depth != binding_depth) | |
619 | abort (); | |
620 | #endif /* defined(DEBUG_CP_BINDING_LEVELS) */ | |
8f032717 MM |
621 | free_binding_level = level; |
622 | find_class_binding_level (); | |
8d08fdba MS |
623 | } |
624 | } | |
a9aedbc2 MS |
625 | |
626 | static void | |
627 | suspend_binding_level () | |
628 | { | |
629 | if (class_binding_level) | |
630 | current_binding_level = class_binding_level; | |
631 | ||
632 | if (global_binding_level) | |
633 | { | |
2c73f9f5 | 634 | /* Cannot suspend a level, if there are none left to suspend. */ |
a9aedbc2 MS |
635 | if (current_binding_level == global_binding_level) |
636 | my_friendly_abort (123); | |
637 | } | |
638 | /* Suspend the current level. */ | |
639 | #if defined(DEBUG_CP_BINDING_LEVELS) | |
640 | binding_depth--; | |
641 | indent (); | |
642 | fprintf (stderr, "suspend %s level 0x%08x line %d\n", | |
643 | (is_class_level) ? "class" : "block", | |
644 | current_binding_level, lineno); | |
645 | if (is_class_level != (current_binding_level == class_binding_level)) | |
9e9ff709 MS |
646 | { |
647 | indent (); | |
648 | fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n"); | |
649 | } | |
a9aedbc2 MS |
650 | is_class_level = 0; |
651 | #endif /* defined(DEBUG_CP_BINDING_LEVELS) */ | |
8f032717 MM |
652 | current_binding_level = current_binding_level->level_chain; |
653 | find_class_binding_level (); | |
a9aedbc2 MS |
654 | } |
655 | ||
824b9a4c | 656 | static void |
a9aedbc2 MS |
657 | resume_binding_level (b) |
658 | struct binding_level *b; | |
659 | { | |
2c73f9f5 ML |
660 | /* Resuming binding levels is meant only for namespaces, |
661 | and those cannot nest into classes. */ | |
662 | my_friendly_assert(!class_binding_level, 386); | |
663 | /* Also, resuming a non-directly nested namespace is a no-no. */ | |
664 | my_friendly_assert(b->level_chain == current_binding_level, 386); | |
a9aedbc2 MS |
665 | current_binding_level = b; |
666 | #if defined(DEBUG_CP_BINDING_LEVELS) | |
667 | b->binding_depth = binding_depth; | |
668 | indent (); | |
669 | fprintf (stderr, "resume %s level 0x%08x line %d\n", | |
670 | (is_class_level) ? "class" : "block", b, lineno); | |
671 | is_class_level = 0; | |
672 | binding_depth++; | |
673 | #endif /* defined(DEBUG_CP_BINDING_LEVELS) */ | |
674 | } | |
8d08fdba | 675 | \f |
cffa8729 MS |
676 | /* Create a new `struct binding_level'. */ |
677 | ||
678 | static | |
679 | struct binding_level * | |
680 | make_binding_level () | |
681 | { | |
682 | /* NOSTRICT */ | |
683 | return (struct binding_level *) xmalloc (sizeof (struct binding_level)); | |
684 | } | |
685 | ||
8d08fdba MS |
686 | /* Nonzero if we are currently in the global binding level. */ |
687 | ||
688 | int | |
689 | global_bindings_p () | |
690 | { | |
691 | return current_binding_level == global_binding_level; | |
692 | } | |
693 | ||
8f032717 MM |
694 | /* Return the innermost binding level that is not for a class scope. */ |
695 | ||
696 | static struct binding_level * | |
697 | innermost_nonclass_level () | |
698 | { | |
699 | struct binding_level *b; | |
700 | ||
701 | b = current_binding_level; | |
702 | while (b->parm_flag == 2) | |
703 | b = b->level_chain; | |
704 | ||
705 | return b; | |
706 | } | |
707 | ||
a9aedbc2 MS |
708 | /* Nonzero if we are currently in a toplevel binding level. This |
709 | means either the global binding level or a namespace in a toplevel | |
8f032717 | 710 | binding level. Since there are no non-toplevel namespace levels, |
74b846e0 MM |
711 | this really means any namespace or template parameter level. We |
712 | also include a class whose context is toplevel. */ | |
a9aedbc2 MS |
713 | |
714 | int | |
715 | toplevel_bindings_p () | |
716 | { | |
8f032717 MM |
717 | struct binding_level *b = innermost_nonclass_level (); |
718 | ||
74b846e0 | 719 | return b->namespace_p || b->template_parms_p; |
a9aedbc2 MS |
720 | } |
721 | ||
8f032717 MM |
722 | /* Nonzero if this is a namespace scope, or if we are defining a class |
723 | which is itself at namespace scope, or whose enclosing class is | |
724 | such a class, etc. */ | |
a9aedbc2 | 725 | |
7bdbfa05 | 726 | int |
a9aedbc2 MS |
727 | namespace_bindings_p () |
728 | { | |
8f032717 MM |
729 | struct binding_level *b = innermost_nonclass_level (); |
730 | ||
731 | return b->namespace_p; | |
a9aedbc2 MS |
732 | } |
733 | ||
f1dedc31 MM |
734 | /* If KEEP is non-zero, make a BLOCK node for the next binding level, |
735 | unconditionally. Otherwise, use the normal logic to decide whether | |
736 | or not to create a BLOCK. */ | |
737 | ||
8d08fdba | 738 | void |
f1dedc31 MM |
739 | keep_next_level (keep) |
740 | int keep; | |
8d08fdba | 741 | { |
f1dedc31 | 742 | keep_next_level_flag = keep; |
8d08fdba MS |
743 | } |
744 | ||
745 | /* Nonzero if the current level needs to have a BLOCK made. */ | |
746 | ||
747 | int | |
748 | kept_level_p () | |
749 | { | |
750 | return (current_binding_level->blocks != NULL_TREE | |
751 | || current_binding_level->keep | |
752 | || current_binding_level->names != NULL_TREE | |
753 | || (current_binding_level->tags != NULL_TREE | |
754 | && !current_binding_level->tag_transparent)); | |
755 | } | |
756 | ||
824b9a4c | 757 | static void |
a9aedbc2 MS |
758 | declare_namespace_level () |
759 | { | |
760 | current_binding_level->namespace_p = 1; | |
761 | } | |
762 | ||
74b846e0 MM |
763 | /* Returns non-zero if this scope was created to store template |
764 | parameters. */ | |
765 | ||
8d08fdba | 766 | int |
74b846e0 | 767 | template_parm_scope_p () |
8d08fdba | 768 | { |
74b846e0 MM |
769 | return current_binding_level->template_parms_p; |
770 | } | |
771 | ||
772 | /* Returns the kind of template specialization we are currently | |
773 | processing, given that it's declaration contained N_CLASS_SCOPES | |
774 | explicit scope qualifications. */ | |
775 | ||
776 | tmpl_spec_kind | |
777 | current_tmpl_spec_kind (n_class_scopes) | |
778 | int n_class_scopes; | |
779 | { | |
780 | int n_template_parm_scopes = 0; | |
781 | int seen_specialization_p = 0; | |
782 | int innermost_specialization_p = 0; | |
783 | struct binding_level *b; | |
784 | ||
785 | /* Scan through the template parameter scopes. */ | |
786 | for (b = current_binding_level; b->template_parms_p; b = b->level_chain) | |
787 | { | |
788 | /* If we see a specialization scope inside a parameter scope, | |
789 | then something is wrong. That corresponds to a declaration | |
790 | like: | |
791 | ||
792 | template <class T> template <> ... | |
793 | ||
794 | which is always illegal since [temp.expl.spec] forbids the | |
795 | specialization of a class member template if the enclosing | |
796 | class templates are not explicitly specialized as well. */ | |
797 | if (b->template_spec_p) | |
798 | { | |
799 | if (n_template_parm_scopes == 0) | |
800 | innermost_specialization_p = 1; | |
801 | else | |
802 | seen_specialization_p = 1; | |
803 | } | |
804 | else if (seen_specialization_p == 1) | |
805 | return tsk_invalid_member_spec; | |
806 | ||
807 | ++n_template_parm_scopes; | |
808 | } | |
809 | ||
810 | /* Handle explicit instantiations. */ | |
811 | if (processing_explicit_instantiation) | |
812 | { | |
813 | if (n_template_parm_scopes != 0) | |
814 | /* We've seen a template parameter list during an explicit | |
815 | instantiation. For example: | |
816 | ||
817 | template <class T> template void f(int); | |
818 | ||
819 | This is erroneous. */ | |
820 | return tsk_invalid_expl_inst; | |
821 | else | |
822 | return tsk_expl_inst; | |
823 | } | |
824 | ||
825 | if (n_template_parm_scopes < n_class_scopes) | |
826 | /* We've not seen enough template headers to match all the | |
827 | specialized classes present. For example: | |
828 | ||
829 | template <class T> void R<T>::S<T>::f(int); | |
830 | ||
831 | This is illegal; there needs to be one set of template | |
832 | parameters for each class. */ | |
833 | return tsk_insufficient_parms; | |
834 | else if (n_template_parm_scopes == n_class_scopes) | |
835 | /* We're processing a non-template declaration (even though it may | |
836 | be a member of a template class.) For example: | |
837 | ||
838 | template <class T> void S<T>::f(int); | |
839 | ||
840 | The `class T' maches the `S<T>', leaving no template headers | |
841 | corresponding to the `f'. */ | |
842 | return tsk_none; | |
843 | else if (n_template_parm_scopes > n_class_scopes + 1) | |
844 | /* We've got too many template headers. For example: | |
845 | ||
846 | template <> template <class T> void f (T); | |
847 | ||
848 | There need to be more enclosing classes. */ | |
849 | return tsk_excessive_parms; | |
850 | else | |
851 | /* This must be a template. It's of the form: | |
852 | ||
853 | template <class T> template <class U> void S<T>::f(U); | |
854 | ||
855 | This is a specialization if the innermost level was a | |
856 | specialization; otherwise it's just a definition of the | |
857 | template. */ | |
858 | return innermost_specialization_p ? tsk_expl_spec : tsk_template; | |
8d08fdba MS |
859 | } |
860 | ||
861 | void | |
862 | set_class_shadows (shadows) | |
863 | tree shadows; | |
864 | { | |
865 | class_binding_level->class_shadowed = shadows; | |
866 | } | |
867 | ||
868 | /* Enter a new binding level. | |
869 | If TAG_TRANSPARENT is nonzero, do so only for the name space of variables, | |
870 | not for that of tags. */ | |
871 | ||
872 | void | |
873 | pushlevel (tag_transparent) | |
874 | int tag_transparent; | |
875 | { | |
b35d4555 | 876 | struct binding_level *newlevel; |
8d08fdba | 877 | |
01d939e8 | 878 | if (cfun && !doing_semantic_analysis_p ()) |
b35d4555 | 879 | return; |
8d08fdba | 880 | |
b35d4555 | 881 | /* Reuse or create a struct for this binding level. */ |
8d08fdba MS |
882 | #if defined(DEBUG_CP_BINDING_LEVELS) |
883 | if (0) | |
884 | #else /* !defined(DEBUG_CP_BINDING_LEVELS) */ | |
885 | if (free_binding_level) | |
886 | #endif /* !defined(DEBUG_CP_BINDING_LEVELS) */ | |
887 | { | |
888 | newlevel = free_binding_level; | |
889 | free_binding_level = free_binding_level->level_chain; | |
890 | } | |
891 | else | |
acef433b | 892 | newlevel = make_binding_level (); |
cffa8729 | 893 | |
8d08fdba MS |
894 | push_binding_level (newlevel, tag_transparent, keep_next_level_flag); |
895 | GNU_xref_start_scope ((HOST_WIDE_INT) newlevel); | |
896 | keep_next_level_flag = 0; | |
897 | } | |
898 | ||
74b846e0 MM |
899 | /* Enter a new scope. The KIND indicates what kind of scope is being |
900 | created. */ | |
901 | ||
902 | void | |
903 | begin_scope (sk) | |
904 | scope_kind sk; | |
905 | { | |
906 | pushlevel (0); | |
907 | ||
908 | switch (sk) | |
909 | { | |
910 | case sk_template_spec: | |
911 | current_binding_level->template_spec_p = 1; | |
912 | /* Fall through. */ | |
913 | ||
914 | case sk_template_parms: | |
915 | current_binding_level->template_parms_p = 1; | |
916 | break; | |
917 | ||
918 | default: | |
919 | my_friendly_abort (20000309); | |
920 | } | |
921 | } | |
922 | ||
923 | /* Exit the current scope. */ | |
924 | ||
925 | void | |
926 | finish_scope () | |
927 | { | |
928 | poplevel (0, 0, 0); | |
929 | } | |
930 | ||
5566b478 | 931 | void |
8d6e462b PB |
932 | note_level_for_for () |
933 | { | |
934 | current_binding_level->is_for_scope = 1; | |
935 | } | |
936 | ||
f181d4ae MM |
937 | /* For a binding between a name and an entity at a block scope, |
938 | this is the `struct binding_level' for the block. */ | |
939 | #define BINDING_LEVEL(NODE) \ | |
c7a932b1 | 940 | (((struct tree_binding*)NODE)->scope.level) |
f181d4ae | 941 | |
f181d4ae MM |
942 | /* Make DECL the innermost binding for ID. The LEVEL is the binding |
943 | level at which this declaration is being bound. */ | |
944 | ||
945 | static void | |
946 | push_binding (id, decl, level) | |
947 | tree id; | |
948 | tree decl; | |
949 | struct binding_level* level; | |
950 | { | |
951 | tree binding; | |
952 | ||
87e3dbc9 | 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)) | |
1005 | && same_type_p (TREE_TYPE (decl), | |
1006 | TREE_TYPE (BINDING_VALUE (binding)))) | |
1007 | /* We have two typedef-names, both naming the same type to have | |
1008 | the same name. This is OK because of: | |
1009 | ||
1010 | [dcl.typedef] | |
1011 | ||
1012 | In a given scope, a typedef specifier can be used to redefine | |
1013 | the name of any type declared in that scope to refer to the | |
1014 | type to which it already refers. */ | |
1015 | ok = 0; | |
8f032717 MM |
1016 | else |
1017 | { | |
1018 | cp_error ("declaration of `%#D'", decl); | |
1019 | cp_error_at ("conflicts with previous declaration `%#D'", | |
1020 | BINDING_VALUE (binding)); | |
1021 | ok = 0; | |
1022 | } | |
1023 | ||
1024 | return ok; | |
d8f8dca1 MM |
1025 | } |
1026 | ||
cfe507be MM |
1027 | /* Add DECL to the list of things declared in B. */ |
1028 | ||
1029 | static void | |
1030 | add_decl_to_level (decl, b) | |
1031 | tree decl; | |
1032 | struct binding_level *b; | |
1033 | { | |
cfe507be MM |
1034 | /* We build up the list in reverse order, and reverse it later if |
1035 | necessary. */ | |
1036 | TREE_CHAIN (decl) = b->names; | |
1037 | b->names = decl; | |
1038 | } | |
1039 | ||
1040 | /* Bind DECL to ID in the current_binding_level, assumed to be a local | |
1041 | binding level. If PUSH_USING is set in FLAGS, we know that DECL | |
1042 | doesn't really belong to this binding level, that it got here | |
1043 | through a using-declaration. */ | |
f181d4ae MM |
1044 | |
1045 | void | |
0034cf72 | 1046 | push_local_binding (id, decl, flags) |
f181d4ae MM |
1047 | tree id; |
1048 | tree decl; | |
0034cf72 | 1049 | int flags; |
f181d4ae | 1050 | { |
8f032717 MM |
1051 | struct binding_level *b; |
1052 | ||
1053 | /* Skip over any local classes. This makes sense if we call | |
1054 | push_local_binding with a friend decl of a local class. */ | |
1055 | b = current_binding_level; | |
1056 | while (b->parm_flag == 2) | |
1057 | b = b->level_chain; | |
f181d4ae | 1058 | |
d8f8dca1 | 1059 | if (lookup_name_current_level (id)) |
8f032717 MM |
1060 | { |
1061 | /* Supplement the existing binding. */ | |
1062 | if (!add_binding (id, decl)) | |
1063 | /* It didn't work. Something else must be bound at this | |
1064 | level. Do not add DECL to the list of things to pop | |
1065 | later. */ | |
1066 | return; | |
1067 | } | |
d8f8dca1 MM |
1068 | else |
1069 | /* Create a new binding. */ | |
8f032717 | 1070 | push_binding (id, decl, b); |
f181d4ae | 1071 | |
0034cf72 | 1072 | if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING)) |
a06d48ef JM |
1073 | /* We must put the OVERLOAD into a TREE_LIST since the |
1074 | TREE_CHAIN of an OVERLOAD is already used. Similarly for | |
1075 | decls that got here through a using-declaration. */ | |
1076 | decl = build_tree_list (NULL_TREE, decl); | |
1077 | ||
f181d4ae MM |
1078 | /* And put DECL on the list of things declared by the current |
1079 | binding level. */ | |
cfe507be | 1080 | add_decl_to_level (decl, b); |
f181d4ae MM |
1081 | } |
1082 | ||
8f032717 MM |
1083 | /* Bind DECL to ID in the class_binding_level. Returns nonzero if the |
1084 | binding was successful. */ | |
f181d4ae | 1085 | |
8f032717 | 1086 | int |
f181d4ae MM |
1087 | push_class_binding (id, decl) |
1088 | tree id; | |
1089 | tree decl; | |
1090 | { | |
8f032717 MM |
1091 | int result = 1; |
1092 | tree binding = IDENTIFIER_BINDING (id); | |
1093 | tree context; | |
1094 | ||
1095 | /* Note that we declared this value so that we can issue an error if | |
1096 | this an illegal redeclaration of a name already used for some | |
1097 | other purpose. */ | |
1098 | note_name_declared_in_class (id, decl); | |
1099 | ||
1100 | if (binding && BINDING_LEVEL (binding) == class_binding_level) | |
d8f8dca1 | 1101 | /* Supplement the existing binding. */ |
8f032717 | 1102 | result = add_binding (id, decl); |
d8f8dca1 MM |
1103 | else |
1104 | /* Create a new binding. */ | |
1105 | push_binding (id, decl, class_binding_level); | |
1106 | ||
1107 | /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the | |
1108 | class-level declaration. Note that we do not use DECL here | |
1109 | because of the possibility of the `struct stat' hack; if DECL is | |
1110 | a class-name or enum-name we might prefer a field-name, or some | |
1111 | such. */ | |
1112 | IDENTIFIER_CLASS_VALUE (id) = BINDING_VALUE (IDENTIFIER_BINDING (id)); | |
8f032717 MM |
1113 | |
1114 | /* If this is a binding from a base class, mark it as such. */ | |
1115 | binding = IDENTIFIER_BINDING (id); | |
1116 | if (BINDING_VALUE (binding) == decl && TREE_CODE (decl) != TREE_LIST) | |
1117 | { | |
280f9385 MM |
1118 | /* Any implicit typename must be from a base-class. The |
1119 | context for an implicit typename declaration is always | |
1120 | the derived class in which the lookup was done, so the checks | |
1121 | based on the context of DECL below will not trigger. */ | |
83233dca | 1122 | if (IMPLICIT_TYPENAME_TYPE_DECL_P (decl)) |
280f9385 | 1123 | INHERITED_VALUE_BINDING_P (binding) = 1; |
8f032717 MM |
1124 | else |
1125 | { | |
280f9385 | 1126 | if (TREE_CODE (decl) == OVERLOAD) |
4f1c5b7d | 1127 | context = CP_DECL_CONTEXT (OVL_CURRENT (decl)); |
280f9385 MM |
1128 | else |
1129 | { | |
1130 | my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd', | |
1131 | 0); | |
4f1c5b7d | 1132 | context = CP_DECL_CONTEXT (decl); |
280f9385 | 1133 | } |
8f032717 | 1134 | |
280f9385 MM |
1135 | if (is_properly_derived_from (current_class_type, context)) |
1136 | INHERITED_VALUE_BINDING_P (binding) = 1; | |
1137 | else | |
1138 | INHERITED_VALUE_BINDING_P (binding) = 0; | |
1139 | } | |
8f032717 MM |
1140 | } |
1141 | else if (BINDING_VALUE (binding) == decl) | |
1142 | /* We only encounter a TREE_LIST when push_class_decls detects an | |
1143 | ambiguity. Such an ambiguity can be overridden by a definition | |
1144 | in this class. */ | |
1145 | INHERITED_VALUE_BINDING_P (binding) = 1; | |
1146 | ||
1147 | return result; | |
f181d4ae MM |
1148 | } |
1149 | ||
d8f8dca1 MM |
1150 | /* Remove the binding for DECL which should be the innermost binding |
1151 | for ID. */ | |
f181d4ae | 1152 | |
68642fb6 UD |
1153 | static void |
1154 | pop_binding (id, decl) | |
f181d4ae | 1155 | tree id; |
d8f8dca1 | 1156 | tree decl; |
f181d4ae MM |
1157 | { |
1158 | tree binding; | |
68642fb6 | 1159 | |
f181d4ae MM |
1160 | if (id == NULL_TREE) |
1161 | /* It's easiest to write the loops that call this function without | |
1162 | checking whether or not the entities involved have names. We | |
1163 | get here for such an entity. */ | |
1164 | return; | |
1165 | ||
d8f8dca1 | 1166 | /* Get the innermost binding for ID. */ |
f181d4ae | 1167 | binding = IDENTIFIER_BINDING (id); |
f181d4ae | 1168 | |
d8f8dca1 MM |
1169 | /* The name should be bound. */ |
1170 | my_friendly_assert (binding != NULL_TREE, 0); | |
1171 | ||
1172 | /* The DECL will be either the ordinary binding or the type | |
1173 | binding for this identifier. Remove that binding. */ | |
1174 | if (BINDING_VALUE (binding) == decl) | |
1175 | BINDING_VALUE (binding) = NULL_TREE; | |
1176 | else if (BINDING_TYPE (binding) == decl) | |
1177 | BINDING_TYPE (binding) = NULL_TREE; | |
1178 | else | |
1179 | my_friendly_abort (0); | |
1180 | ||
1181 | if (!BINDING_VALUE (binding) && !BINDING_TYPE (binding)) | |
87e3dbc9 MM |
1182 | /* We're completely done with the innermost binding for this |
1183 | identifier. Unhook it from the list of bindings. */ | |
1184 | IDENTIFIER_BINDING (id) = TREE_CHAIN (binding); | |
f181d4ae MM |
1185 | } |
1186 | ||
acef433b MM |
1187 | /* When a label goes out of scope, check to see if that label was used |
1188 | in a valid manner, and issue any appropriate warnings or errors. */ | |
1189 | ||
1190 | static void | |
1191 | pop_label (link) | |
1192 | tree link; | |
1193 | { | |
1194 | tree label = TREE_VALUE (link); | |
1195 | ||
88848bde | 1196 | if (!processing_template_decl && doing_semantic_analysis_p ()) |
acef433b | 1197 | { |
88848bde MM |
1198 | if (DECL_INITIAL (label) == NULL_TREE) |
1199 | { | |
1200 | cp_error_at ("label `%D' used but not defined", label); | |
1201 | /* Avoid crashing later. */ | |
1202 | define_label (input_filename, 1, DECL_NAME (label)); | |
1203 | } | |
1204 | else if (warn_unused && !TREE_USED (label)) | |
1205 | cp_warning_at ("label `%D' defined but not used", label); | |
acef433b | 1206 | } |
acef433b MM |
1207 | |
1208 | SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), TREE_PURPOSE (link)); | |
1209 | } | |
1210 | ||
1211 | /* At the end of a function, all labels declared within the fucntion | |
68642fb6 | 1212 | go out of scope. BLOCK is the top-level block for the |
acef433b MM |
1213 | function. */ |
1214 | ||
1215 | static void | |
1216 | pop_labels (block) | |
1217 | tree block; | |
1218 | { | |
1219 | tree link; | |
1220 | ||
1221 | /* Clear out the definitions of all label names, since their scopes | |
1222 | end here. */ | |
1223 | for (link = named_labels; link; link = TREE_CHAIN (link)) | |
1224 | { | |
1225 | pop_label (link); | |
1226 | /* Put the labels into the "variables" of the top-level block, | |
1227 | so debugger can see them. */ | |
1228 | TREE_CHAIN (TREE_VALUE (link)) = BLOCK_VARS (block); | |
1229 | BLOCK_VARS (block) = TREE_VALUE (link); | |
1230 | } | |
1231 | ||
1232 | named_labels = NULL_TREE; | |
1233 | } | |
1234 | ||
8d08fdba MS |
1235 | /* Exit a binding level. |
1236 | Pop the level off, and restore the state of the identifier-decl mappings | |
1237 | that were in effect when this level was entered. | |
1238 | ||
1239 | If KEEP == 1, this level had explicit declarations, so | |
1240 | and create a "block" (a BLOCK node) for the level | |
1241 | to record its declarations and subblocks for symbol table output. | |
1242 | ||
8d08fdba MS |
1243 | If FUNCTIONBODY is nonzero, this level is the body of a function, |
1244 | so create a block as if KEEP were set and also clear out all | |
1245 | label names. | |
1246 | ||
1247 | If REVERSE is nonzero, reverse the order of decls before putting | |
1248 | them into the BLOCK. */ | |
1249 | ||
1250 | tree | |
1251 | poplevel (keep, reverse, functionbody) | |
1252 | int keep; | |
1253 | int reverse; | |
1254 | int functionbody; | |
1255 | { | |
1256 | register tree link; | |
1257 | /* The chain of decls was accumulated in reverse order. | |
1258 | Put it into forward order, just for cleanliness. */ | |
1259 | tree decls; | |
1260 | int tmp = functionbody; | |
b35d4555 MM |
1261 | int real_functionbody; |
1262 | tree tags; | |
1263 | tree subblocks; | |
8d08fdba MS |
1264 | tree block = NULL_TREE; |
1265 | tree decl; | |
1266 | int block_previously_created; | |
f181d4ae MM |
1267 | int leaving_for_scope; |
1268 | ||
01d939e8 | 1269 | if (cfun && !doing_semantic_analysis_p ()) |
b35d4555 MM |
1270 | return NULL_TREE; |
1271 | ||
273a708f MM |
1272 | my_friendly_assert (current_binding_level->parm_flag != 2, |
1273 | 19990916); | |
b74a0560 | 1274 | |
b35d4555 MM |
1275 | real_functionbody = (current_binding_level->keep == 2 |
1276 | ? ((functionbody = 0), tmp) : functionbody); | |
1277 | tags = functionbody >= 0 ? current_binding_level->tags : 0; | |
1278 | subblocks = functionbody >= 0 ? current_binding_level->blocks : 0; | |
1279 | ||
b74a0560 MM |
1280 | my_friendly_assert (!current_binding_level->class_shadowed, |
1281 | 19990414); | |
8d08fdba | 1282 | |
536333d4 MM |
1283 | /* We used to use KEEP == 2 to indicate that the new block should go |
1284 | at the beginning of the list of blocks at this binding level, | |
1285 | rather than the end. This hack is no longer used. */ | |
1286 | my_friendly_assert (keep == 0 || keep == 1, 0); | |
1287 | ||
8d08fdba MS |
1288 | GNU_xref_end_scope ((HOST_WIDE_INT) current_binding_level, |
1289 | (HOST_WIDE_INT) current_binding_level->level_chain, | |
1290 | current_binding_level->parm_flag, | |
5566b478 | 1291 | current_binding_level->keep); |
8d08fdba MS |
1292 | |
1293 | if (current_binding_level->keep == 1) | |
1294 | keep = 1; | |
1295 | ||
8d08fdba MS |
1296 | /* Get the decls in the order they were written. |
1297 | Usually current_binding_level->names is in reverse order. | |
1298 | But parameter decls were previously put in forward order. */ | |
1299 | ||
1300 | if (reverse) | |
1301 | current_binding_level->names | |
1302 | = decls = nreverse (current_binding_level->names); | |
1303 | else | |
1304 | decls = current_binding_level->names; | |
1305 | ||
1306 | /* Output any nested inline functions within this block | |
1307 | if they weren't already output. */ | |
8d08fdba MS |
1308 | for (decl = decls; decl; decl = TREE_CHAIN (decl)) |
1309 | if (TREE_CODE (decl) == FUNCTION_DECL | |
1310 | && ! TREE_ASM_WRITTEN (decl) | |
1311 | && DECL_INITIAL (decl) != NULL_TREE | |
6060a796 MS |
1312 | && TREE_ADDRESSABLE (decl) |
1313 | && decl_function_context (decl) == current_function_decl) | |
8d08fdba MS |
1314 | { |
1315 | /* If this decl was copied from a file-scope decl | |
1316 | on account of a block-scope extern decl, | |
1317 | propagate TREE_ADDRESSABLE to the file-scope decl. */ | |
1318 | if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE) | |
1319 | TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1; | |
1320 | else | |
1321 | { | |
1322 | push_function_context (); | |
1323 | output_inline_function (decl); | |
1324 | pop_function_context (); | |
1325 | } | |
1326 | } | |
1327 | ||
d9b2d9da MM |
1328 | /* When not in function-at-a-time mode, expand_end_bindings will |
1329 | warn about unused variables. But, in function-at-a-time mode | |
1330 | expand_end_bindings is not passed the list of variables in the | |
1331 | current scope, and therefore no warning is emitted. So, we | |
1332 | explicitly warn here. */ | |
1333 | if (!processing_template_decl) | |
1334 | warn_about_unused_variables (getdecls ()); | |
1335 | ||
8d08fdba MS |
1336 | /* If there were any declarations or structure tags in that level, |
1337 | or if this level is a function body, | |
1338 | create a BLOCK to record them for the life of this function. */ | |
8d08fdba MS |
1339 | block = NULL_TREE; |
1340 | block_previously_created = (current_binding_level->this_block != NULL_TREE); | |
1341 | if (block_previously_created) | |
1342 | block = current_binding_level->this_block; | |
1343 | else if (keep == 1 || functionbody) | |
1344 | block = make_node (BLOCK); | |
1345 | if (block != NULL_TREE) | |
1346 | { | |
72b7eeff MS |
1347 | if (block_previously_created) |
1348 | { | |
1349 | if (decls || tags || subblocks) | |
1350 | { | |
2af0ac28 | 1351 | if (BLOCK_VARS (block)) |
f181d4ae MM |
1352 | warning ("internal compiler error: debugging info corrupted"); |
1353 | ||
72b7eeff | 1354 | BLOCK_VARS (block) = decls; |
be99da77 MS |
1355 | |
1356 | /* We can have previous subblocks and new subblocks when | |
1357 | doing fixup_gotos with complex cleanups. We chain the new | |
1358 | subblocks onto the end of any pre-existing subblocks. */ | |
1359 | BLOCK_SUBBLOCKS (block) = chainon (BLOCK_SUBBLOCKS (block), | |
1360 | subblocks); | |
72b7eeff | 1361 | } |
72b7eeff MS |
1362 | } |
1363 | else | |
1364 | { | |
1365 | BLOCK_VARS (block) = decls; | |
72b7eeff | 1366 | BLOCK_SUBBLOCKS (block) = subblocks; |
72b7eeff | 1367 | } |
8d08fdba MS |
1368 | } |
1369 | ||
1370 | /* In each subblock, record that this is its superior. */ | |
8d08fdba MS |
1371 | if (keep >= 0) |
1372 | for (link = subblocks; link; link = TREE_CHAIN (link)) | |
1373 | BLOCK_SUPERCONTEXT (link) = block; | |
1374 | ||
f181d4ae MM |
1375 | /* We still support the old for-scope rules, whereby the variables |
1376 | in a for-init statement were in scope after the for-statement | |
1377 | ended. We only use the new rules in flag_new_for_scope is | |
1378 | nonzero. */ | |
68642fb6 | 1379 | leaving_for_scope |
f181d4ae MM |
1380 | = current_binding_level->is_for_scope && flag_new_for_scope == 1; |
1381 | ||
1382 | /* Remove declarations for all the DECLs in this level. */ | |
1383 | for (link = decls; link; link = TREE_CHAIN (link)) | |
1384 | { | |
1385 | if (leaving_for_scope && TREE_CODE (link) == VAR_DECL) | |
1386 | { | |
68642fb6 | 1387 | tree outer_binding |
f181d4ae MM |
1388 | = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (link))); |
1389 | tree ns_binding; | |
1390 | ||
1391 | if (!outer_binding) | |
1392 | ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link)); | |
c7dda1e3 MM |
1393 | else |
1394 | ns_binding = NULL_TREE; | |
1395 | ||
68642fb6 UD |
1396 | if (outer_binding |
1397 | && (BINDING_LEVEL (outer_binding) | |
f181d4ae MM |
1398 | == current_binding_level->level_chain)) |
1399 | /* We have something like: | |
68642fb6 | 1400 | |
f181d4ae MM |
1401 | int i; |
1402 | for (int i; ;); | |
68642fb6 | 1403 | |
f181d4ae MM |
1404 | and we are leaving the `for' scope. There's no reason to |
1405 | keep the binding of the inner `i' in this case. */ | |
d8f8dca1 | 1406 | pop_binding (DECL_NAME (link), link); |
68642fb6 UD |
1407 | else if ((outer_binding |
1408 | && (TREE_CODE (BINDING_VALUE (outer_binding)) | |
f181d4ae | 1409 | == TYPE_DECL)) |
68642fb6 | 1410 | || (ns_binding |
f181d4ae MM |
1411 | && TREE_CODE (ns_binding) == TYPE_DECL)) |
1412 | /* Here, we have something like: | |
1413 | ||
1414 | typedef int I; | |
1415 | ||
1416 | void f () { | |
1417 | for (int I; ;); | |
1418 | } | |
1419 | ||
1420 | We must pop the for-scope binding so we know what's a | |
1421 | type and what isn't. */ | |
d8f8dca1 | 1422 | pop_binding (DECL_NAME (link), link); |
e76a2646 | 1423 | else |
e76a2646 | 1424 | { |
f181d4ae MM |
1425 | /* Mark this VAR_DECL as dead so that we can tell we left it |
1426 | there only for backward compatibility. */ | |
1427 | DECL_DEAD_FOR_LOCAL (link) = 1; | |
68642fb6 | 1428 | |
f181d4ae MM |
1429 | /* Keep track of what should of have happenned when we |
1430 | popped the binding. */ | |
1431 | if (outer_binding && BINDING_VALUE (outer_binding)) | |
68642fb6 | 1432 | DECL_SHADOWED_FOR_VAR (link) |
f181d4ae MM |
1433 | = BINDING_VALUE (outer_binding); |
1434 | ||
1435 | /* Add it to the list of dead variables in the next | |
1436 | outermost binding to that we can remove these when we | |
1437 | leave that binding. */ | |
1438 | current_binding_level->level_chain->dead_vars_from_for | |
1439 | = tree_cons (NULL_TREE, link, | |
1440 | current_binding_level->level_chain-> | |
1441 | dead_vars_from_for); | |
1442 | ||
1443 | /* Although we don't pop the CPLUS_BINDING, we do clear | |
1444 | its BINDING_LEVEL since the level is going away now. */ | |
1445 | BINDING_LEVEL (IDENTIFIER_BINDING (DECL_NAME (link))) | |
1446 | = 0; | |
e76a2646 | 1447 | } |
2ee887f2 | 1448 | } |
68642fb6 | 1449 | else |
8d6e462b | 1450 | { |
f181d4ae | 1451 | /* Remove the binding. */ |
0034cf72 JM |
1452 | decl = link; |
1453 | if (TREE_CODE (decl) == TREE_LIST) | |
1454 | decl = TREE_VALUE (decl); | |
1455 | if (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd') | |
1456 | pop_binding (DECL_NAME (decl), decl); | |
1457 | else if (TREE_CODE (decl) == OVERLOAD) | |
1458 | pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl); | |
68642fb6 | 1459 | else |
f181d4ae | 1460 | my_friendly_abort (0); |
8d08fdba | 1461 | } |
f181d4ae | 1462 | } |
8d08fdba | 1463 | |
f181d4ae MM |
1464 | /* Remove declarations for any `for' variables from inner scopes |
1465 | that we kept around. */ | |
1466 | for (link = current_binding_level->dead_vars_from_for; | |
1467 | link; link = TREE_CHAIN (link)) | |
d8f8dca1 | 1468 | pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link)); |
2ee887f2 | 1469 | |
f181d4ae MM |
1470 | /* Restore the IDENTIFIER_TYPE_VALUEs. */ |
1471 | for (link = current_binding_level->type_shadowed; | |
1472 | link; link = TREE_CHAIN (link)) | |
1473 | SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link)); | |
acef433b MM |
1474 | |
1475 | /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */ | |
1476 | for (link = current_binding_level->shadowed_labels; | |
68642fb6 | 1477 | link; |
acef433b MM |
1478 | link = TREE_CHAIN (link)) |
1479 | pop_label (link); | |
1480 | ||
f181d4ae MM |
1481 | /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs |
1482 | list if a `using' declaration put them there. The debugging | |
1483 | back-ends won't understand OVERLOAD, so we remove them here. | |
1484 | Because the BLOCK_VARS are (temporarily) shared with | |
1485 | CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have | |
1486 | popped all the bindings. */ | |
1487 | if (block) | |
1488 | { | |
1489 | tree* d; | |
8d6e462b | 1490 | |
a06d48ef JM |
1491 | for (d = &BLOCK_VARS (block); *d; ) |
1492 | { | |
1493 | if (TREE_CODE (*d) == TREE_LIST) | |
1494 | *d = TREE_CHAIN (*d); | |
1495 | else | |
1496 | d = &TREE_CHAIN (*d); | |
1497 | } | |
8d6e462b | 1498 | } |
8d08fdba MS |
1499 | |
1500 | /* If the level being exited is the top level of a function, | |
1501 | check over all the labels. */ | |
8d08fdba MS |
1502 | if (functionbody) |
1503 | { | |
acef433b MM |
1504 | /* Since this is the top level block of a function, the vars are |
1505 | the function's parameters. Don't leave them in the BLOCK | |
1506 | because they are found in the FUNCTION_DECL instead. */ | |
8d08fdba | 1507 | BLOCK_VARS (block) = 0; |
acef433b | 1508 | pop_labels (block); |
8d08fdba MS |
1509 | } |
1510 | ||
1511 | /* Any uses of undefined labels now operate under constraints | |
1512 | of next binding contour. */ | |
01d939e8 | 1513 | if (cfun) |
a8f73d4b MM |
1514 | { |
1515 | struct binding_level *level_chain; | |
1516 | level_chain = current_binding_level->level_chain; | |
1517 | if (level_chain) | |
1518 | { | |
1519 | struct named_label_list *labels; | |
1520 | for (labels = named_label_uses; labels; labels = labels->next) | |
1521 | if (labels->binding_level == current_binding_level) | |
1522 | { | |
1523 | labels->binding_level = level_chain; | |
1524 | labels->names_in_scope = level_chain->names; | |
1525 | } | |
1526 | } | |
1527 | } | |
8d08fdba MS |
1528 | |
1529 | tmp = current_binding_level->keep; | |
1530 | ||
1531 | pop_binding_level (); | |
1532 | if (functionbody) | |
1533 | DECL_INITIAL (current_function_decl) = block; | |
1534 | else if (block) | |
1535 | { | |
1536 | if (!block_previously_created) | |
1537 | current_binding_level->blocks | |
1538 | = chainon (current_binding_level->blocks, block); | |
1539 | } | |
1540 | /* If we did not make a block for the level just exited, | |
1541 | any blocks made for inner levels | |
1542 | (since they cannot be recorded as subblocks in that level) | |
1543 | must be carried forward so they will later become subblocks | |
1544 | of something else. */ | |
1545 | else if (subblocks) | |
536333d4 MM |
1546 | current_binding_level->blocks |
1547 | = chainon (current_binding_level->blocks, subblocks); | |
8d08fdba | 1548 | |
d9b2d9da MM |
1549 | /* Each and every BLOCK node created here in `poplevel' is important |
1550 | (e.g. for proper debugging information) so if we created one | |
1551 | earlier, mark it as "used". */ | |
1552 | if (block) | |
1553 | TREE_USED (block) = 1; | |
1554 | ||
8d08fdba | 1555 | /* Take care of compiler's internal binding structures. */ |
a4443a08 | 1556 | if (tmp == 2) |
8d08fdba | 1557 | { |
d9b2d9da MM |
1558 | tree scope_stmts; |
1559 | ||
68642fb6 | 1560 | scope_stmts |
d9b2d9da | 1561 | = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/1); |
8d08fdba | 1562 | if (block) |
d9b2d9da MM |
1563 | { |
1564 | SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block; | |
1565 | SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block; | |
1566 | } | |
1567 | ||
8f471b0d | 1568 | block = poplevel (keep, reverse, functionbody); |
8d08fdba MS |
1569 | } |
1570 | ||
8d08fdba MS |
1571 | return block; |
1572 | } | |
1573 | ||
1574 | /* Delete the node BLOCK from the current binding level. | |
1575 | This is used for the block inside a stmt expr ({...}) | |
1576 | so that the block can be reinserted where appropriate. */ | |
1577 | ||
1578 | void | |
1579 | delete_block (block) | |
1580 | tree block; | |
1581 | { | |
1582 | tree t; | |
1583 | if (current_binding_level->blocks == block) | |
1584 | current_binding_level->blocks = TREE_CHAIN (block); | |
1585 | for (t = current_binding_level->blocks; t;) | |
1586 | { | |
1587 | if (TREE_CHAIN (t) == block) | |
1588 | TREE_CHAIN (t) = TREE_CHAIN (block); | |
1589 | else | |
1590 | t = TREE_CHAIN (t); | |
1591 | } | |
1592 | TREE_CHAIN (block) = NULL_TREE; | |
1593 | /* Clear TREE_USED which is always set by poplevel. | |
1594 | The flag is set again if insert_block is called. */ | |
1595 | TREE_USED (block) = 0; | |
1596 | } | |
1597 | ||
1598 | /* Insert BLOCK at the end of the list of subblocks of the | |
1599 | current binding level. This is used when a BIND_EXPR is expanded, | |
1600 | to handle the BLOCK node inside the BIND_EXPR. */ | |
1601 | ||
1602 | void | |
1603 | insert_block (block) | |
1604 | tree block; | |
1605 | { | |
1606 | TREE_USED (block) = 1; | |
1607 | current_binding_level->blocks | |
1608 | = chainon (current_binding_level->blocks, block); | |
1609 | } | |
1610 | ||
8d08fdba MS |
1611 | /* Set the BLOCK node for the innermost scope |
1612 | (the one we are currently in). */ | |
1613 | ||
1614 | void | |
1615 | set_block (block) | |
1616 | register tree block; | |
1617 | { | |
1618 | current_binding_level->this_block = block; | |
1619 | } | |
1620 | ||
1621 | /* Do a pushlevel for class declarations. */ | |
e92cc029 | 1622 | |
8d08fdba MS |
1623 | void |
1624 | pushlevel_class () | |
1625 | { | |
1626 | register struct binding_level *newlevel; | |
1627 | ||
1628 | /* Reuse or create a struct for this binding level. */ | |
1629 | #if defined(DEBUG_CP_BINDING_LEVELS) | |
1630 | if (0) | |
1631 | #else /* !defined(DEBUG_CP_BINDING_LEVELS) */ | |
1632 | if (free_binding_level) | |
1633 | #endif /* !defined(DEBUG_CP_BINDING_LEVELS) */ | |
1634 | { | |
1635 | newlevel = free_binding_level; | |
1636 | free_binding_level = free_binding_level->level_chain; | |
1637 | } | |
1638 | else | |
f181d4ae | 1639 | newlevel = make_binding_level (); |
8d08fdba MS |
1640 | |
1641 | #if defined(DEBUG_CP_BINDING_LEVELS) | |
1642 | is_class_level = 1; | |
1643 | #endif /* defined(DEBUG_CP_BINDING_LEVELS) */ | |
1644 | ||
1645 | push_binding_level (newlevel, 0, 0); | |
1646 | ||
8d08fdba MS |
1647 | class_binding_level = current_binding_level; |
1648 | class_binding_level->parm_flag = 2; | |
70adf8a9 | 1649 | class_binding_level->this_class = current_class_type; |
8d08fdba MS |
1650 | } |
1651 | ||
b74a0560 | 1652 | /* ...and a poplevel for class declarations. */ |
e92cc029 | 1653 | |
273a708f | 1654 | void |
b74a0560 | 1655 | poplevel_class () |
8d08fdba MS |
1656 | { |
1657 | register struct binding_level *level = class_binding_level; | |
8d08fdba MS |
1658 | tree shadowed; |
1659 | ||
1660 | my_friendly_assert (level != 0, 354); | |
68642fb6 | 1661 | |
8d08fdba | 1662 | /* If we're leaving a toplevel class, don't bother to do the setting |
ddd5a7c1 | 1663 | of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot |
8d08fdba | 1664 | shouldn't even be used when current_class_type isn't set, and second, |
ddd5a7c1 | 1665 | if we don't touch it here, we're able to use the cache effect if the |
8d08fdba | 1666 | next time we're entering a class scope, it is the same class. */ |
b74a0560 | 1667 | if (current_class_depth != 1) |
8f032717 MM |
1668 | { |
1669 | struct binding_level* b; | |
1670 | ||
1671 | /* Clear out our IDENTIFIER_CLASS_VALUEs. */ | |
1672 | for (shadowed = level->class_shadowed; | |
1673 | shadowed; | |
1674 | shadowed = TREE_CHAIN (shadowed)) | |
1675 | IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE; | |
68642fb6 | 1676 | |
8f032717 MM |
1677 | /* Find the next enclosing class, and recreate |
1678 | IDENTIFIER_CLASS_VALUEs appropriate for that class. */ | |
1679 | b = level->level_chain; | |
1680 | while (b && b->parm_flag != 2) | |
1681 | b = b->level_chain; | |
1682 | ||
1683 | if (b) | |
68642fb6 UD |
1684 | for (shadowed = b->class_shadowed; |
1685 | shadowed; | |
8f032717 MM |
1686 | shadowed = TREE_CHAIN (shadowed)) |
1687 | { | |
1688 | tree t; | |
1689 | ||
1690 | t = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed)); | |
1691 | while (t && BINDING_LEVEL (t) != b) | |
1692 | t = TREE_CHAIN (t); | |
68642fb6 | 1693 | |
8f032717 | 1694 | if (t) |
68642fb6 | 1695 | IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) |
8f032717 MM |
1696 | = BINDING_VALUE (t); |
1697 | } | |
1698 | } | |
8d08fdba MS |
1699 | else |
1700 | /* Remember to save what IDENTIFIER's were bound in this scope so we | |
1701 | can recover from cache misses. */ | |
e76a2646 MS |
1702 | { |
1703 | previous_class_type = current_class_type; | |
1704 | previous_class_values = class_binding_level->class_shadowed; | |
1705 | } | |
8d08fdba MS |
1706 | for (shadowed = level->type_shadowed; |
1707 | shadowed; | |
1708 | shadowed = TREE_CHAIN (shadowed)) | |
2c73f9f5 | 1709 | SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed)); |
8d08fdba | 1710 | |
f181d4ae | 1711 | /* Remove the bindings for all of the class-level declarations. */ |
68642fb6 UD |
1712 | for (shadowed = level->class_shadowed; |
1713 | shadowed; | |
f181d4ae | 1714 | shadowed = TREE_CHAIN (shadowed)) |
d8f8dca1 | 1715 | pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed)); |
f181d4ae | 1716 | |
8d08fdba MS |
1717 | GNU_xref_end_scope ((HOST_WIDE_INT) class_binding_level, |
1718 | (HOST_WIDE_INT) class_binding_level->level_chain, | |
1719 | class_binding_level->parm_flag, | |
5566b478 | 1720 | class_binding_level->keep); |
8d08fdba | 1721 | |
38e01259 | 1722 | /* Now, pop out of the binding level which we created up in the |
8d08fdba MS |
1723 | `pushlevel_class' routine. */ |
1724 | #if defined(DEBUG_CP_BINDING_LEVELS) | |
1725 | is_class_level = 1; | |
1726 | #endif /* defined(DEBUG_CP_BINDING_LEVELS) */ | |
1727 | ||
1728 | pop_binding_level (); | |
8d08fdba | 1729 | } |
8f032717 MM |
1730 | |
1731 | /* We are entering the scope of a class. Clear IDENTIFIER_CLASS_VALUE | |
1732 | for any names in enclosing classes. */ | |
1733 | ||
1734 | void | |
1735 | clear_identifier_class_values () | |
1736 | { | |
1737 | tree t; | |
1738 | ||
1739 | if (!class_binding_level) | |
1740 | return; | |
1741 | ||
1742 | for (t = class_binding_level->class_shadowed; | |
1743 | t; | |
1744 | t = TREE_CHAIN (t)) | |
1745 | IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE; | |
1746 | } | |
1747 | ||
0aafb128 MM |
1748 | /* Returns non-zero if T is a virtual function table. */ |
1749 | ||
1750 | int | |
1751 | vtable_decl_p (t, data) | |
1752 | tree t; | |
1753 | void *data ATTRIBUTE_UNUSED; | |
1754 | { | |
1755 | return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t)); | |
1756 | } | |
1757 | ||
1758 | /* Returns non-zero if T is a TYPE_DECL for a type with virtual | |
1759 | functions. */ | |
1760 | ||
1761 | int | |
1762 | vtype_decl_p (t, data) | |
1763 | tree t; | |
1764 | void *data ATTRIBUTE_UNUSED; | |
1765 | { | |
1766 | return (TREE_CODE (t) == TYPE_DECL | |
1767 | && TREE_TYPE (t) != error_mark_node | |
1768 | && TYPE_LANG_SPECIFIC (TREE_TYPE (t)) | |
1769 | && CLASSTYPE_VSIZE (TREE_TYPE (t))); | |
1770 | } | |
1771 | ||
f71f87f9 MM |
1772 | /* Return the declarations that are members of the namespace NS. */ |
1773 | ||
1774 | tree | |
1775 | cp_namespace_decls (ns) | |
1776 | tree ns; | |
1777 | { | |
1778 | return NAMESPACE_LEVEL (ns)->names; | |
1779 | } | |
1780 | ||
0aafb128 MM |
1781 | /* Walk all the namespaces contained NAMESPACE, including NAMESPACE |
1782 | itself, calling F for each. The DATA is passed to F as well. */ | |
1783 | ||
1784 | static int | |
1785 | walk_namespaces_r (namespace, f, data) | |
1786 | tree namespace; | |
1787 | walk_namespaces_fn f; | |
1788 | void *data; | |
1789 | { | |
1790 | tree current; | |
1791 | int result = 0; | |
1792 | ||
1793 | result |= (*f) (namespace, data); | |
1794 | ||
f71f87f9 | 1795 | for (current = cp_namespace_decls (namespace); |
0aafb128 MM |
1796 | current; |
1797 | current = TREE_CHAIN (current)) | |
1798 | { | |
1799 | if (TREE_CODE (current) != NAMESPACE_DECL | |
1800 | || DECL_NAMESPACE_ALIAS (current)) | |
1801 | continue; | |
1802 | if (!DECL_LANG_SPECIFIC (current)) | |
1803 | { | |
1804 | /* Hmm. std. */ | |
1805 | my_friendly_assert (current == std_node, 393); | |
1806 | continue; | |
1807 | } | |
1808 | ||
1809 | /* We found a namespace. */ | |
1810 | result |= walk_namespaces_r (current, f, data); | |
1811 | } | |
1812 | ||
1813 | return result; | |
1814 | } | |
1815 | ||
1816 | /* Walk all the namespaces, calling F for each. The DATA is passed to | |
1817 | F as well. */ | |
1818 | ||
1819 | int | |
1820 | walk_namespaces (f, data) | |
1821 | walk_namespaces_fn f; | |
1822 | void *data; | |
1823 | { | |
1824 | return walk_namespaces_r (global_namespace, f, data); | |
1825 | } | |
1826 | ||
1827 | struct walk_globals_data { | |
1828 | walk_globals_pred p; | |
1829 | walk_globals_fn f; | |
1830 | void *data; | |
1831 | }; | |
1832 | ||
1833 | /* Walk the global declarations in NAMESPACE. Whenever one is found | |
1834 | for which P returns non-zero, call F with its address. If any call | |
1835 | to F returns a non-zero value, return a non-zero value. */ | |
1836 | ||
68642fb6 | 1837 | static int |
0aafb128 MM |
1838 | walk_globals_r (namespace, data) |
1839 | tree namespace; | |
1840 | void *data; | |
1841 | { | |
1842 | struct walk_globals_data* wgd = (struct walk_globals_data *) data; | |
1843 | walk_globals_pred p = wgd->p; | |
1844 | walk_globals_fn f = wgd->f; | |
1845 | void *d = wgd->data; | |
1846 | tree *t; | |
1847 | int result = 0; | |
1848 | ||
1849 | t = &NAMESPACE_LEVEL (namespace)->names; | |
1850 | ||
1851 | while (*t) | |
1852 | { | |
1853 | tree glbl = *t; | |
1854 | ||
1855 | if ((*p) (glbl, d)) | |
1856 | result |= (*f) (t, d); | |
1857 | ||
1858 | /* If F changed *T, then *T still points at the next item to | |
1859 | examine. */ | |
1860 | if (*t == glbl) | |
1861 | t = &TREE_CHAIN (*t); | |
1862 | } | |
1863 | ||
1864 | return result; | |
1865 | } | |
1866 | ||
1867 | /* Walk the global declarations. Whenever one is found for which P | |
1868 | returns non-zero, call F with its address. If any call to F | |
1869 | returns a non-zero value, return a non-zero value. */ | |
1870 | ||
1871 | int | |
1872 | walk_globals (p, f, data) | |
1873 | walk_globals_pred p; | |
1874 | walk_globals_fn f; | |
1875 | void *data; | |
1876 | { | |
1877 | struct walk_globals_data wgd; | |
1878 | wgd.p = p; | |
1879 | wgd.f = f; | |
1880 | wgd.data = data; | |
1881 | ||
1882 | return walk_namespaces (walk_globals_r, &wgd); | |
1883 | } | |
1884 | ||
1885 | /* Call wrapup_globals_declarations for the globals in NAMESPACE. If | |
1886 | DATA is non-NULL, this is the last time we will call | |
1887 | wrapup_global_declarations for this NAMESPACE. */ | |
1888 | ||
1889 | int | |
1890 | wrapup_globals_for_namespace (namespace, data) | |
1891 | tree namespace; | |
1892 | void *data; | |
1893 | { | |
f71f87f9 | 1894 | tree globals = cp_namespace_decls (namespace); |
0aafb128 MM |
1895 | int len = list_length (globals); |
1896 | tree *vec = (tree *) alloca (sizeof (tree) * len); | |
1897 | int i; | |
0352cfc8 | 1898 | int result; |
0aafb128 MM |
1899 | tree decl; |
1900 | int last_time = (data != 0); | |
1901 | ||
1902 | if (last_time && namespace == global_namespace) | |
1903 | /* Let compile_file handle the global namespace. */ | |
1904 | return 0; | |
1905 | ||
1906 | /* Process the decls in reverse order--earliest first. | |
1907 | Put them into VEC from back to front, then take out from front. */ | |
68642fb6 | 1908 | |
0aafb128 | 1909 | for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl)) |
2ce07e2d NS |
1910 | { |
1911 | /* Pretend we've output an unused static variable. This ensures | |
1912 | that the toplevel __FUNCTION__ etc won't be emitted, unless | |
1913 | needed. */ | |
cf74fb86 JM |
1914 | if (TREE_CODE (decl) == VAR_DECL && DECL_ARTIFICIAL (decl) |
1915 | && !TREE_PUBLIC (decl) && !TREE_USED (decl)) | |
1916 | { | |
1917 | TREE_ASM_WRITTEN (decl) = 1; | |
1918 | DECL_IGNORED_P (decl) = 1; | |
1919 | } | |
2ce07e2d NS |
1920 | vec[len - i - 1] = decl; |
1921 | } | |
68642fb6 | 1922 | |
0352cfc8 MM |
1923 | if (last_time) |
1924 | { | |
1925 | check_global_declarations (vec, len); | |
1926 | return 0; | |
1927 | } | |
1928 | ||
1929 | /* Temporarily mark vtables as external. That prevents | |
1930 | wrapup_global_declarations from writing them out; we must process | |
1931 | them ourselves in finish_vtable_vardecl. */ | |
1932 | for (i = 0; i < len; ++i) | |
91f2aa22 | 1933 | if (vtable_decl_p (vec[i], /*data=*/0) && !DECL_EXTERNAL (vec[i])) |
0352cfc8 MM |
1934 | { |
1935 | DECL_NOT_REALLY_EXTERN (vec[i]) = 1; | |
1936 | DECL_EXTERNAL (vec[i]) = 1; | |
1937 | } | |
0aafb128 | 1938 | |
0352cfc8 MM |
1939 | /* Write out any globals that need to be output. */ |
1940 | result = wrapup_global_declarations (vec, len); | |
1941 | ||
1942 | /* Undo the hack to DECL_EXTERNAL above. */ | |
1943 | for (i = 0; i < len; ++i) | |
1944 | if (vtable_decl_p (vec[i], /*data=*/0) | |
1945 | && DECL_NOT_REALLY_EXTERN (vec[i])) | |
1946 | { | |
1947 | DECL_NOT_REALLY_EXTERN (vec[i]) = 0; | |
1948 | DECL_EXTERNAL (vec[i]) = 0; | |
1949 | } | |
1950 | ||
1951 | return result; | |
0aafb128 MM |
1952 | } |
1953 | ||
4519c0a8 | 1954 | \f |
fc6af6e3 RH |
1955 | /* Mark ARG (which is really a struct binding_level **) for GC. */ |
1956 | ||
4519c0a8 MM |
1957 | static void |
1958 | mark_binding_level (arg) | |
1959 | void *arg; | |
1960 | { | |
1961 | struct binding_level *lvl = *(struct binding_level **)arg; | |
1962 | ||
1963 | while (lvl) | |
1964 | { | |
1965 | ggc_mark_tree (lvl->names); | |
1966 | ggc_mark_tree (lvl->tags); | |
1967 | ggc_mark_tree (lvl->usings); | |
1968 | ggc_mark_tree (lvl->using_directives); | |
1969 | ggc_mark_tree (lvl->class_shadowed); | |
1970 | ggc_mark_tree (lvl->type_shadowed); | |
1971 | ggc_mark_tree (lvl->shadowed_labels); | |
1972 | ggc_mark_tree (lvl->blocks); | |
1973 | ggc_mark_tree (lvl->this_block); | |
70adf8a9 | 1974 | ggc_mark_tree (lvl->this_class); |
4519c0a8 MM |
1975 | ggc_mark_tree (lvl->incomplete); |
1976 | ggc_mark_tree (lvl->dead_vars_from_for); | |
1977 | ||
1978 | lvl = lvl->level_chain; | |
1979 | } | |
1980 | } | |
8d08fdba MS |
1981 | \f |
1982 | /* For debugging. */ | |
5566b478 MS |
1983 | static int no_print_functions = 0; |
1984 | static int no_print_builtins = 0; | |
8d08fdba MS |
1985 | |
1986 | void | |
1987 | print_binding_level (lvl) | |
1988 | struct binding_level *lvl; | |
1989 | { | |
1990 | tree t; | |
1991 | int i = 0, len; | |
1992 | fprintf (stderr, " blocks="); | |
1993 | fprintf (stderr, HOST_PTR_PRINTF, lvl->blocks); | |
1994 | fprintf (stderr, " n_incomplete=%d parm_flag=%d keep=%d", | |
f30432d7 | 1995 | list_length (lvl->incomplete), lvl->parm_flag, lvl->keep); |
8d08fdba MS |
1996 | if (lvl->tag_transparent) |
1997 | fprintf (stderr, " tag-transparent"); | |
1998 | if (lvl->more_cleanups_ok) | |
1999 | fprintf (stderr, " more-cleanups-ok"); | |
2000 | if (lvl->have_cleanups) | |
2001 | fprintf (stderr, " have-cleanups"); | |
8d08fdba MS |
2002 | fprintf (stderr, "\n"); |
2003 | if (lvl->names) | |
2004 | { | |
2005 | fprintf (stderr, " names:\t"); | |
2006 | /* We can probably fit 3 names to a line? */ | |
2007 | for (t = lvl->names; t; t = TREE_CHAIN (t)) | |
2008 | { | |
68642fb6 | 2009 | if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL)) |
8d08fdba MS |
2010 | continue; |
2011 | if (no_print_builtins | |
fc378698 MS |
2012 | && (TREE_CODE (t) == TYPE_DECL) |
2013 | && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>"))) | |
8d08fdba MS |
2014 | continue; |
2015 | ||
2016 | /* Function decls tend to have longer names. */ | |
2017 | if (TREE_CODE (t) == FUNCTION_DECL) | |
2018 | len = 3; | |
2019 | else | |
2020 | len = 2; | |
2021 | i += len; | |
2022 | if (i > 6) | |
2023 | { | |
2024 | fprintf (stderr, "\n\t"); | |
2025 | i = len; | |
2026 | } | |
2027 | print_node_brief (stderr, "", t, 0); | |
bd6dd845 | 2028 | if (t == error_mark_node) |
8d08fdba MS |
2029 | break; |
2030 | } | |
2031 | if (i) | |
2032 | fprintf (stderr, "\n"); | |
2033 | } | |
2034 | if (lvl->tags) | |
2035 | { | |
2036 | fprintf (stderr, " tags:\t"); | |
2037 | i = 0; | |
2038 | for (t = lvl->tags; t; t = TREE_CHAIN (t)) | |
2039 | { | |
2040 | if (TREE_PURPOSE (t) == NULL_TREE) | |
2041 | len = 3; | |
2042 | else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t))) | |
2043 | len = 2; | |
2044 | else | |
2045 | len = 4; | |
2046 | i += len; | |
2047 | if (i > 5) | |
2048 | { | |
2049 | fprintf (stderr, "\n\t"); | |
2050 | i = len; | |
2051 | } | |
2052 | if (TREE_PURPOSE (t) == NULL_TREE) | |
2053 | { | |
2054 | print_node_brief (stderr, "<unnamed-typedef", TREE_VALUE (t), 0); | |
2055 | fprintf (stderr, ">"); | |
2056 | } | |
2057 | else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t))) | |
2058 | print_node_brief (stderr, "", TREE_VALUE (t), 0); | |
2059 | else | |
2060 | { | |
2061 | print_node_brief (stderr, "<typedef", TREE_PURPOSE (t), 0); | |
2062 | print_node_brief (stderr, "", TREE_VALUE (t), 0); | |
2063 | fprintf (stderr, ">"); | |
2064 | } | |
2065 | } | |
2066 | if (i) | |
2067 | fprintf (stderr, "\n"); | |
2068 | } | |
8d08fdba MS |
2069 | if (lvl->class_shadowed) |
2070 | { | |
2071 | fprintf (stderr, " class-shadowed:"); | |
2072 | for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t)) | |
2073 | { | |
2074 | fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t))); | |
2075 | } | |
2076 | fprintf (stderr, "\n"); | |
2077 | } | |
2078 | if (lvl->type_shadowed) | |
2079 | { | |
2080 | fprintf (stderr, " type-shadowed:"); | |
2081 | for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t)) | |
2082 | { | |
8d08fdba | 2083 | fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t))); |
8d08fdba MS |
2084 | } |
2085 | fprintf (stderr, "\n"); | |
2086 | } | |
2087 | } | |
2088 | ||
2089 | void | |
2090 | print_other_binding_stack (stack) | |
2091 | struct binding_level *stack; | |
2092 | { | |
2093 | struct binding_level *level; | |
2094 | for (level = stack; level != global_binding_level; level = level->level_chain) | |
2095 | { | |
2096 | fprintf (stderr, "binding level "); | |
2097 | fprintf (stderr, HOST_PTR_PRINTF, level); | |
2098 | fprintf (stderr, "\n"); | |
2099 | print_binding_level (level); | |
2100 | } | |
2101 | } | |
2102 | ||
2103 | void | |
2104 | print_binding_stack () | |
2105 | { | |
2106 | struct binding_level *b; | |
2107 | fprintf (stderr, "current_binding_level="); | |
2108 | fprintf (stderr, HOST_PTR_PRINTF, current_binding_level); | |
2109 | fprintf (stderr, "\nclass_binding_level="); | |
2110 | fprintf (stderr, HOST_PTR_PRINTF, class_binding_level); | |
2111 | fprintf (stderr, "\nglobal_binding_level="); | |
2112 | fprintf (stderr, HOST_PTR_PRINTF, global_binding_level); | |
2113 | fprintf (stderr, "\n"); | |
2114 | if (class_binding_level) | |
2115 | { | |
2116 | for (b = class_binding_level; b; b = b->level_chain) | |
2117 | if (b == current_binding_level) | |
2118 | break; | |
2119 | if (b) | |
2120 | b = class_binding_level; | |
2121 | else | |
2122 | b = current_binding_level; | |
2123 | } | |
2124 | else | |
2125 | b = current_binding_level; | |
2126 | print_other_binding_stack (b); | |
2127 | fprintf (stderr, "global:\n"); | |
2128 | print_binding_level (global_binding_level); | |
2129 | } | |
a9aedbc2 | 2130 | |
2c73f9f5 ML |
2131 | /* Namespace binding access routines: The namespace_bindings field of |
2132 | the identifier is polymorphic, with three possible values: | |
2133 | NULL_TREE, a list of CPLUS_BINDINGS, or any other tree_node | |
2134 | indicating the BINDING_VALUE of global_namespace. */ | |
30394414 | 2135 | |
2c73f9f5 ML |
2136 | /* Check whether the a binding for the name to scope is known. |
2137 | Assumes that the bindings of the name are already a list | |
2138 | of bindings. Returns the binding found, or NULL_TREE. */ | |
2139 | ||
2140 | static tree | |
2141 | find_binding (name, scope) | |
30394414 JM |
2142 | tree name; |
2143 | tree scope; | |
2144 | { | |
2c73f9f5 | 2145 | tree iter, prev = NULL_TREE; |
3e3f722c ML |
2146 | |
2147 | scope = ORIGINAL_NAMESPACE (scope); | |
68642fb6 | 2148 | |
30394414 JM |
2149 | for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name); iter; |
2150 | iter = TREE_CHAIN (iter)) | |
2151 | { | |
2152 | my_friendly_assert (TREE_CODE (iter) == CPLUS_BINDING, 374); | |
2153 | if (BINDING_SCOPE (iter) == scope) | |
2c73f9f5 | 2154 | { |
cfe507be | 2155 | /* Move binding found to the front of the list, so |
2c73f9f5 ML |
2156 | subsequent lookups will find it faster. */ |
2157 | if (prev) | |
2158 | { | |
2159 | TREE_CHAIN (prev) = TREE_CHAIN (iter); | |
2160 | TREE_CHAIN (iter) = IDENTIFIER_NAMESPACE_BINDINGS (name); | |
2161 | IDENTIFIER_NAMESPACE_BINDINGS (name) = iter; | |
2162 | } | |
2163 | return iter; | |
2164 | } | |
2165 | prev = iter; | |
30394414 | 2166 | } |
2c73f9f5 ML |
2167 | return NULL_TREE; |
2168 | } | |
2169 | ||
2170 | /* Always returns a binding for name in scope. If the | |
2171 | namespace_bindings is not a list, convert it to one first. | |
2172 | If no binding is found, make a new one. */ | |
2173 | ||
2174 | tree | |
2175 | binding_for_name (name, scope) | |
2176 | tree name; | |
2177 | tree scope; | |
2178 | { | |
2179 | tree b = IDENTIFIER_NAMESPACE_BINDINGS (name); | |
2180 | tree result; | |
3e3f722c ML |
2181 | |
2182 | scope = ORIGINAL_NAMESPACE (scope); | |
68642fb6 | 2183 | |
2c73f9f5 ML |
2184 | if (b && TREE_CODE (b) != CPLUS_BINDING) |
2185 | { | |
2186 | /* Get rid of optimization for global scope. */ | |
2187 | IDENTIFIER_NAMESPACE_BINDINGS (name) = NULL_TREE; | |
2188 | BINDING_VALUE (binding_for_name (name, global_namespace)) = b; | |
2189 | b = IDENTIFIER_NAMESPACE_BINDINGS (name); | |
2190 | } | |
2191 | if (b && (result = find_binding (name, scope))) | |
2192 | return result; | |
cd9f6678 | 2193 | /* Not found, make a new one. */ |
2c73f9f5 ML |
2194 | result = make_node (CPLUS_BINDING); |
2195 | TREE_CHAIN (result) = b; | |
2196 | IDENTIFIER_NAMESPACE_BINDINGS (name) = result; | |
2197 | BINDING_SCOPE (result) = scope; | |
2198 | BINDING_TYPE (result) = NULL_TREE; | |
2199 | BINDING_VALUE (result) = NULL_TREE; | |
2c73f9f5 ML |
2200 | return result; |
2201 | } | |
2202 | ||
2203 | /* Return the binding value for name in scope, considering that | |
2204 | namespace_binding may or may not be a list of CPLUS_BINDINGS. */ | |
2205 | ||
2206 | tree | |
2207 | namespace_binding (name, scope) | |
2208 | tree name; | |
2209 | tree scope; | |
2210 | { | |
2211 | tree b = IDENTIFIER_NAMESPACE_BINDINGS (name); | |
2212 | if (b == NULL_TREE) | |
2213 | return NULL_TREE; | |
cb0dbb9a JM |
2214 | if (scope == NULL_TREE) |
2215 | scope = global_namespace; | |
2c73f9f5 ML |
2216 | if (TREE_CODE (b) != CPLUS_BINDING) |
2217 | return (scope == global_namespace) ? b : NULL_TREE; | |
2218 | name = find_binding (name,scope); | |
2219 | if (name == NULL_TREE) | |
2220 | return name; | |
2221 | return BINDING_VALUE (name); | |
2222 | } | |
2223 | ||
2224 | /* Set the binding value for name in scope. If modifying the binding | |
2225 | of global_namespace is attempted, try to optimize it. */ | |
2226 | ||
2227 | void | |
2228 | set_namespace_binding (name, scope, val) | |
2229 | tree name; | |
2230 | tree scope; | |
2231 | tree val; | |
2232 | { | |
2233 | tree b; | |
cb0dbb9a JM |
2234 | |
2235 | if (scope == NULL_TREE) | |
2236 | scope = global_namespace; | |
68642fb6 | 2237 | |
2c73f9f5 ML |
2238 | if (scope == global_namespace) |
2239 | { | |
2240 | b = IDENTIFIER_NAMESPACE_BINDINGS (name); | |
2241 | if (b == NULL_TREE || TREE_CODE (b) != CPLUS_BINDING) | |
2242 | { | |
2243 | IDENTIFIER_NAMESPACE_BINDINGS (name) = val; | |
2244 | return; | |
2245 | } | |
2246 | } | |
2247 | b = binding_for_name (name, scope); | |
2248 | BINDING_VALUE (b) = val; | |
30394414 JM |
2249 | } |
2250 | ||
dff6b454 RK |
2251 | /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we |
2252 | select a name that is unique to this compilation unit. */ | |
e92cc029 | 2253 | |
a9aedbc2 MS |
2254 | void |
2255 | push_namespace (name) | |
2256 | tree name; | |
2257 | { | |
b370501f | 2258 | tree d = NULL_TREE; |
30394414 JM |
2259 | int need_new = 1; |
2260 | int implicit_use = 0; | |
2c73f9f5 | 2261 | int global = 0; |
30394414 JM |
2262 | if (!global_namespace) |
2263 | { | |
2c73f9f5 | 2264 | /* This must be ::. */ |
30394414 | 2265 | my_friendly_assert (name == get_identifier ("::"), 377); |
2c73f9f5 | 2266 | global = 1; |
30394414 JM |
2267 | } |
2268 | else if (!name) | |
2269 | { | |
3ab52652 ML |
2270 | /* The name of anonymous namespace is unique for the translation |
2271 | unit. */ | |
0c8feefe MM |
2272 | if (!anonymous_namespace_name) |
2273 | anonymous_namespace_name = get_file_function_name ('N'); | |
2274 | name = anonymous_namespace_name; | |
3ab52652 ML |
2275 | d = IDENTIFIER_NAMESPACE_VALUE (name); |
2276 | if (d) | |
2277 | /* Reopening anonymous namespace. */ | |
2278 | need_new = 0; | |
30394414 | 2279 | implicit_use = 1; |
2c73f9f5 ML |
2280 | } |
2281 | else if (current_namespace == global_namespace | |
2282 | && name == DECL_NAME (std_node)) | |
2283 | { | |
2284 | in_std++; | |
2285 | return; | |
2286 | } | |
30394414 JM |
2287 | else |
2288 | { | |
2c73f9f5 | 2289 | /* Check whether this is an extended namespace definition. */ |
30394414 JM |
2290 | d = IDENTIFIER_NAMESPACE_VALUE (name); |
2291 | if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL) | |
3e3f722c ML |
2292 | { |
2293 | need_new = 0; | |
2294 | if (DECL_NAMESPACE_ALIAS (d)) | |
2295 | { | |
8251199e | 2296 | cp_error ("namespace alias `%D' not allowed here, assuming `%D'", |
3e3f722c ML |
2297 | d, DECL_NAMESPACE_ALIAS (d)); |
2298 | d = DECL_NAMESPACE_ALIAS (d); | |
2299 | } | |
2300 | } | |
30394414 | 2301 | } |
68642fb6 | 2302 | |
30394414 JM |
2303 | if (need_new) |
2304 | { | |
2c73f9f5 | 2305 | /* Make a new namespace, binding the name to it. */ |
6b57ac29 | 2306 | d = build_lang_decl (NAMESPACE_DECL, name, void_type_node); |
2c73f9f5 ML |
2307 | /* The global namespace is not pushed, and the global binding |
2308 | level is set elsewhere. */ | |
2309 | if (!global) | |
2310 | { | |
2311 | d = pushdecl (d); | |
2312 | pushlevel (0); | |
2313 | declare_namespace_level (); | |
2314 | NAMESPACE_LEVEL (d) = current_binding_level; | |
2315 | } | |
30394414 | 2316 | } |
2c73f9f5 ML |
2317 | else |
2318 | resume_binding_level (NAMESPACE_LEVEL (d)); | |
2319 | ||
30394414 JM |
2320 | if (implicit_use) |
2321 | do_using_directive (d); | |
2c73f9f5 | 2322 | /* Enter the name space. */ |
30394414 | 2323 | current_namespace = d; |
a9aedbc2 MS |
2324 | } |
2325 | ||
2326 | /* Pop from the scope of the current namespace. */ | |
e92cc029 | 2327 | |
a9aedbc2 MS |
2328 | void |
2329 | pop_namespace () | |
2330 | { | |
2c73f9f5 ML |
2331 | if (current_namespace == global_namespace) |
2332 | { | |
2333 | my_friendly_assert (in_std>0, 980421); | |
2334 | in_std--; | |
2335 | return; | |
2336 | } | |
cb0dbb9a | 2337 | current_namespace = CP_DECL_CONTEXT (current_namespace); |
2c73f9f5 ML |
2338 | /* The binding level is not popped, as it might be re-opened later. */ |
2339 | suspend_binding_level (); | |
2340 | } | |
a9aedbc2 | 2341 | |
61fc8c9e MM |
2342 | /* Push into the scope of the namespace NS, even if it is deeply |
2343 | nested within another namespace. */ | |
2344 | ||
2345 | void | |
2346 | push_nested_namespace (ns) | |
2347 | tree ns; | |
2348 | { | |
2349 | if (ns == global_namespace) | |
2350 | push_to_top_level (); | |
2351 | else | |
2352 | { | |
2353 | push_nested_namespace (CP_DECL_CONTEXT (ns)); | |
2354 | push_namespace (DECL_NAME (ns)); | |
2355 | } | |
2356 | } | |
2357 | ||
2358 | /* Pop back from the scope of the namespace NS, which was previously | |
2359 | entered with push_nested_namespace. */ | |
68642fb6 | 2360 | |
61fc8c9e MM |
2361 | void |
2362 | pop_nested_namespace (ns) | |
2363 | tree ns; | |
2364 | { | |
2365 | while (ns != global_namespace) | |
2366 | { | |
2367 | pop_namespace (); | |
2368 | ns = CP_DECL_CONTEXT (ns); | |
2369 | } | |
2370 | ||
2371 | pop_from_top_level (); | |
2372 | } | |
2373 | ||
8d08fdba MS |
2374 | \f |
2375 | /* Subroutines for reverting temporarily to top-level for instantiation | |
2376 | of templates and such. We actually need to clear out the class- and | |
2377 | local-value slots of all identifiers, so that only the global values | |
2378 | are at all visible. Simply setting current_binding_level to the global | |
2379 | scope isn't enough, because more binding levels may be pushed. */ | |
9cd64686 | 2380 | struct saved_scope *scope_chain; |
8d08fdba | 2381 | |
6f80451c MM |
2382 | /* Mark ST for GC. */ |
2383 | ||
2384 | static void | |
2385 | mark_stmt_tree (st) | |
2386 | struct stmt_tree *st; | |
2387 | { | |
2388 | ggc_mark_tree (st->x_last_stmt); | |
2389 | ggc_mark_tree (st->x_last_expr_type); | |
2390 | } | |
2391 | ||
9cd64686 | 2392 | /* Mark ARG (which is really a struct saved_scope **) for GC. */ |
fc6af6e3 RH |
2393 | |
2394 | static void | |
2395 | mark_saved_scope (arg) | |
2396 | void *arg; | |
2397 | { | |
2398 | struct saved_scope *t = *(struct saved_scope **)arg; | |
2399 | while (t) | |
2400 | { | |
fc6af6e3 RH |
2401 | mark_binding_level (&t->class_bindings); |
2402 | ggc_mark_tree (t->old_bindings); | |
2403 | ggc_mark_tree (t->old_namespace); | |
2404 | ggc_mark_tree (t->class_name); | |
2405 | ggc_mark_tree (t->class_type); | |
2406 | ggc_mark_tree (t->access_specifier); | |
a8f73d4b | 2407 | ggc_mark_tree (t->function_decl); |
fc6af6e3 | 2408 | if (t->lang_base) |
9cd64686 | 2409 | ggc_mark_tree_varray (t->lang_base); |
fc6af6e3 | 2410 | ggc_mark_tree (t->lang_name); |
9cd64686 | 2411 | ggc_mark_tree (t->x_function_parms); |
fc6af6e3 | 2412 | ggc_mark_tree (t->template_parms); |
9cd64686 MM |
2413 | ggc_mark_tree (t->x_previous_class_type); |
2414 | ggc_mark_tree (t->x_previous_class_values); | |
6f80451c | 2415 | ggc_mark_tree (t->x_saved_tree); |
70adf8a9 JM |
2416 | ggc_mark_tree (t->incomplete); |
2417 | ggc_mark_tree (t->lookups); | |
6f80451c MM |
2418 | |
2419 | mark_stmt_tree (&t->x_stmt_tree); | |
a8f73d4b | 2420 | mark_binding_level (&t->bindings); |
fc6af6e3 RH |
2421 | t = t->prev; |
2422 | } | |
2423 | } | |
2424 | ||
e92cc029 | 2425 | static tree |
45537677 MS |
2426 | store_bindings (names, old_bindings) |
2427 | tree names, old_bindings; | |
2428 | { | |
2429 | tree t; | |
2430 | for (t = names; t; t = TREE_CHAIN (t)) | |
2431 | { | |
2432 | tree binding, t1, id; | |
2433 | ||
2434 | if (TREE_CODE (t) == TREE_LIST) | |
2435 | id = TREE_PURPOSE (t); | |
2436 | else | |
2437 | id = DECL_NAME (t); | |
2438 | ||
68642fb6 | 2439 | if (!id |
f181d4ae MM |
2440 | /* Note that we may have an IDENTIFIER_CLASS_VALUE even when |
2441 | we have no IDENTIFIER_BINDING if we have left the class | |
2442 | scope, but cached the class-level declarations. */ | |
2443 | || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id))) | |
45537677 MS |
2444 | continue; |
2445 | ||
2446 | for (t1 = old_bindings; t1; t1 = TREE_CHAIN (t1)) | |
2447 | if (TREE_VEC_ELT (t1, 0) == id) | |
2448 | goto skip_it; | |
78957a2a | 2449 | |
87e3dbc9 | 2450 | binding = make_tree_vec (4); |
78957a2a | 2451 | |
45537677 MS |
2452 | if (id) |
2453 | { | |
2454 | my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135); | |
2455 | TREE_VEC_ELT (binding, 0) = id; | |
2c73f9f5 | 2456 | TREE_VEC_ELT (binding, 1) = REAL_IDENTIFIER_TYPE_VALUE (id); |
f181d4ae | 2457 | TREE_VEC_ELT (binding, 2) = IDENTIFIER_BINDING (id); |
45537677 | 2458 | TREE_VEC_ELT (binding, 3) = IDENTIFIER_CLASS_VALUE (id); |
f181d4ae | 2459 | IDENTIFIER_BINDING (id) = NULL_TREE; |
45537677 MS |
2460 | IDENTIFIER_CLASS_VALUE (id) = NULL_TREE; |
2461 | } | |
2462 | TREE_CHAIN (binding) = old_bindings; | |
2463 | old_bindings = binding; | |
2464 | skip_it: | |
2465 | ; | |
2466 | } | |
2467 | return old_bindings; | |
2468 | } | |
2469 | ||
8d08fdba | 2470 | void |
5566b478 MS |
2471 | maybe_push_to_top_level (pseudo) |
2472 | int pseudo; | |
8d08fdba | 2473 | { |
a8f73d4b | 2474 | struct saved_scope *s; |
9cd64686 | 2475 | struct binding_level *b; |
a8f73d4b MM |
2476 | tree old_bindings; |
2477 | int need_pop; | |
2478 | ||
2479 | s = (struct saved_scope *) xcalloc (1, sizeof (struct saved_scope)); | |
8d08fdba | 2480 | |
9cd64686 MM |
2481 | b = scope_chain ? current_binding_level : 0; |
2482 | ||
a8f73d4b | 2483 | /* If we're in the middle of some function, save our state. */ |
01d939e8 | 2484 | if (cfun) |
a8f73d4b MM |
2485 | { |
2486 | need_pop = 1; | |
2487 | push_function_context_to (NULL_TREE); | |
2488 | } | |
2489 | else | |
2490 | need_pop = 0; | |
e349ee73 | 2491 | |
a8f73d4b | 2492 | old_bindings = NULL_TREE; |
9cd64686 | 2493 | if (scope_chain && previous_class_type) |
a50f0918 MS |
2494 | old_bindings = store_bindings (previous_class_values, old_bindings); |
2495 | ||
8d08fdba MS |
2496 | /* Have to include global_binding_level, because class-level decls |
2497 | aren't listed anywhere useful. */ | |
2498 | for (; b; b = b->level_chain) | |
2499 | { | |
2500 | tree t; | |
2501 | ||
2c73f9f5 ML |
2502 | /* Template IDs are inserted into the global level. If they were |
2503 | inserted into namespace level, finish_file wouldn't find them | |
2504 | when doing pending instantiations. Therefore, don't stop at | |
2505 | namespace level, but continue until :: . */ | |
74b846e0 | 2506 | if (b == global_binding_level || (pseudo && b->template_parms_p)) |
5566b478 | 2507 | break; |
8d08fdba | 2508 | |
45537677 | 2509 | old_bindings = store_bindings (b->names, old_bindings); |
cffa8729 | 2510 | /* We also need to check class_shadowed to save class-level type |
45537677 MS |
2511 | bindings, since pushclass doesn't fill in b->names. */ |
2512 | if (b->parm_flag == 2) | |
cffa8729 | 2513 | old_bindings = store_bindings (b->class_shadowed, old_bindings); |
8d08fdba | 2514 | |
8d08fdba MS |
2515 | /* Unwind type-value slots back to top level. */ |
2516 | for (t = b->type_shadowed; t; t = TREE_CHAIN (t)) | |
2517 | SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t)); | |
2518 | } | |
fc0e7bf5 MM |
2519 | s->prev = scope_chain; |
2520 | s->old_bindings = old_bindings; | |
a8f73d4b MM |
2521 | s->bindings = b; |
2522 | s->need_pop_function_context = need_pop; | |
2523 | s->function_decl = current_function_decl; | |
fc0e7bf5 MM |
2524 | |
2525 | scope_chain = s; | |
8d08fdba | 2526 | current_function_decl = NULL_TREE; |
9cd64686 MM |
2527 | VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base"); |
2528 | current_lang_stack = &VARRAY_TREE (current_lang_base, 0); | |
51c184be MS |
2529 | current_lang_name = lang_name_cplusplus; |
2530 | strict_prototype = strict_prototypes_lang_cplusplus; | |
2c73f9f5 | 2531 | current_namespace = global_namespace; |
5566b478 MS |
2532 | } |
2533 | ||
2534 | void | |
2535 | push_to_top_level () | |
2536 | { | |
2537 | maybe_push_to_top_level (0); | |
8d08fdba MS |
2538 | } |
2539 | ||
2540 | void | |
2541 | pop_from_top_level () | |
2542 | { | |
9cd64686 | 2543 | struct saved_scope *s = scope_chain; |
8d08fdba MS |
2544 | tree t; |
2545 | ||
e76a2646 | 2546 | /* Clear out class-level bindings cache. */ |
8d08fdba | 2547 | if (previous_class_type) |
8f032717 | 2548 | invalidate_class_lookup_cache (); |
8d08fdba | 2549 | |
9cd64686 MM |
2550 | VARRAY_FREE (current_lang_base); |
2551 | ||
2552 | scope_chain = s->prev; | |
87e3dbc9 | 2553 | for (t = s->old_bindings; t; t = TREE_CHAIN (t)) |
8d08fdba MS |
2554 | { |
2555 | tree id = TREE_VEC_ELT (t, 0); | |
2556 | if (id) | |
2557 | { | |
2c73f9f5 | 2558 | SET_IDENTIFIER_TYPE_VALUE (id, TREE_VEC_ELT (t, 1)); |
f181d4ae | 2559 | IDENTIFIER_BINDING (id) = TREE_VEC_ELT (t, 2); |
8d08fdba | 2560 | IDENTIFIER_CLASS_VALUE (id) = TREE_VEC_ELT (t, 3); |
9cd64686 | 2561 | } |
8d08fdba | 2562 | } |
9cd64686 | 2563 | |
51c184be MS |
2564 | if (current_lang_name == lang_name_cplusplus) |
2565 | strict_prototype = strict_prototypes_lang_cplusplus; | |
2566 | else if (current_lang_name == lang_name_c) | |
2567 | strict_prototype = strict_prototypes_lang_c; | |
51c184be | 2568 | |
a8f73d4b MM |
2569 | /* If we were in the middle of compiling a function, restore our |
2570 | state. */ | |
2571 | if (s->need_pop_function_context) | |
2572 | pop_function_context_from (NULL_TREE); | |
2573 | current_function_decl = s->function_decl; | |
e349ee73 | 2574 | |
a8f73d4b | 2575 | free (s); |
8d08fdba MS |
2576 | } |
2577 | \f | |
2578 | /* Push a definition of struct, union or enum tag "name". | |
68642fb6 | 2579 | into binding_level "b". "type" should be the type node, |
8d08fdba MS |
2580 | We assume that the tag "name" is not already defined. |
2581 | ||
2582 | Note that the definition may really be just a forward reference. | |
2583 | In that case, the TYPE_SIZE will be a NULL_TREE. | |
2584 | ||
e92cc029 | 2585 | C++ gratuitously puts all these tags in the name space. */ |
8d08fdba MS |
2586 | |
2587 | /* When setting the IDENTIFIER_TYPE_VALUE field of an identifier ID, | |
2588 | record the shadowed value for this binding contour. TYPE is | |
2589 | the type that ID maps to. */ | |
2590 | ||
2591 | static void | |
2592 | set_identifier_type_value_with_scope (id, type, b) | |
2593 | tree id; | |
2594 | tree type; | |
2595 | struct binding_level *b; | |
2596 | { | |
2c73f9f5 | 2597 | if (!b->namespace_p) |
8d08fdba | 2598 | { |
2c73f9f5 ML |
2599 | /* Shadow the marker, not the real thing, so that the marker |
2600 | gets restored later. */ | |
2601 | tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id); | |
8d08fdba MS |
2602 | b->type_shadowed |
2603 | = tree_cons (id, old_type_value, b->type_shadowed); | |
2604 | } | |
2c73f9f5 ML |
2605 | else |
2606 | { | |
2607 | tree binding = binding_for_name (id, current_namespace); | |
2608 | BINDING_TYPE (binding) = type; | |
2609 | /* Store marker instead of real type. */ | |
2610 | type = global_type_node; | |
2611 | } | |
8d08fdba MS |
2612 | SET_IDENTIFIER_TYPE_VALUE (id, type); |
2613 | } | |
2614 | ||
8f032717 | 2615 | /* As set_identifier_type_value_with_scope, but using current_binding_level. */ |
8d08fdba MS |
2616 | |
2617 | void | |
2618 | set_identifier_type_value (id, type) | |
2619 | tree id; | |
2620 | tree type; | |
2621 | { | |
8f032717 | 2622 | set_identifier_type_value_with_scope (id, type, current_binding_level); |
8d08fdba MS |
2623 | } |
2624 | ||
2c73f9f5 ML |
2625 | /* Return the type associated with id. */ |
2626 | ||
2627 | tree | |
2628 | identifier_type_value (id) | |
2629 | tree id; | |
2630 | { | |
2631 | /* There is no type with that name, anywhere. */ | |
2632 | if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE) | |
2633 | return NULL_TREE; | |
2634 | /* This is not the type marker, but the real thing. */ | |
2635 | if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node) | |
2636 | return REAL_IDENTIFIER_TYPE_VALUE (id); | |
2637 | /* Have to search for it. It must be on the global level, now. | |
2638 | Ask lookup_name not to return non-types. */ | |
3e3f722c | 2639 | id = lookup_name_real (id, 2, 1, 0); |
2c73f9f5 ML |
2640 | if (id) |
2641 | return TREE_TYPE (id); | |
2642 | return NULL_TREE; | |
2643 | } | |
2644 | ||
a9aedbc2 MS |
2645 | /* Pop off extraneous binding levels left over due to syntax errors. |
2646 | ||
2647 | We don't pop past namespaces, as they might be valid. */ | |
e92cc029 | 2648 | |
8926095f MS |
2649 | void |
2650 | pop_everything () | |
2651 | { | |
2652 | #ifdef DEBUG_CP_BINDING_LEVELS | |
2653 | fprintf (stderr, "XXX entering pop_everything ()\n"); | |
2654 | #endif | |
8f032717 | 2655 | while (!toplevel_bindings_p ()) |
8926095f | 2656 | { |
8f032717 | 2657 | if (current_binding_level->parm_flag == 2) |
b74a0560 | 2658 | pop_nested_class (); |
8926095f MS |
2659 | else |
2660 | poplevel (0, 0, 0); | |
2661 | } | |
2662 | #ifdef DEBUG_CP_BINDING_LEVELS | |
2663 | fprintf (stderr, "XXX leaving pop_everything ()\n"); | |
2664 | #endif | |
2665 | } | |
2666 | ||
39c01e4c MM |
2667 | /* The type TYPE is being declared. If it is a class template, or a |
2668 | specialization of a class template, do any processing required and | |
2669 | perform error-checking. If IS_FRIEND is non-zero, this TYPE is | |
2670 | being declared a friend. B is the binding level at which this TYPE | |
2671 | should be bound. | |
2672 | ||
2673 | Returns the TYPE_DECL for TYPE, which may have been altered by this | |
2674 | processing. */ | |
2675 | ||
68642fb6 | 2676 | static tree |
39c01e4c MM |
2677 | maybe_process_template_type_declaration (type, globalize, b) |
2678 | tree type; | |
2679 | int globalize; | |
2680 | struct binding_level* b; | |
2681 | { | |
2682 | tree decl = TYPE_NAME (type); | |
68642fb6 | 2683 | |
39c01e4c MM |
2684 | if (processing_template_parmlist) |
2685 | /* You can't declare a new template type in a template parameter | |
2686 | list. But, you can declare a non-template type: | |
68642fb6 | 2687 | |
39c01e4c | 2688 | template <class A*> struct S; |
68642fb6 | 2689 | |
39c01e4c MM |
2690 | is a forward-declaration of `A'. */ |
2691 | ; | |
68642fb6 | 2692 | else |
39c01e4c MM |
2693 | { |
2694 | maybe_check_template_type (type); | |
2695 | ||
68642fb6 | 2696 | my_friendly_assert (IS_AGGR_TYPE (type) |
ed44da02 | 2697 | || TREE_CODE (type) == ENUMERAL_TYPE, 0); |
68642fb6 UD |
2698 | |
2699 | ||
3ebc5c52 | 2700 | if (processing_template_decl) |
39c01e4c MM |
2701 | { |
2702 | /* This may change after the call to | |
2703 | push_template_decl_real, but we want the original value. */ | |
2704 | tree name = DECL_NAME (decl); | |
2705 | ||
2706 | decl = push_template_decl_real (decl, globalize); | |
2707 | /* If the current binding level is the binding level for the | |
2708 | template parameters (see the comment in | |
2709 | begin_template_parm_list) and the enclosing level is a class | |
2710 | scope, and we're not looking at a friend, push the | |
2711 | declaration of the member class into the class scope. In the | |
2712 | friend case, push_template_decl will already have put the | |
2713 | friend into global scope, if appropriate. */ | |
ed44da02 | 2714 | if (TREE_CODE (type) != ENUMERAL_TYPE |
74b846e0 | 2715 | && !globalize && b->template_parms_p |
39c01e4c MM |
2716 | && b->level_chain->parm_flag == 2) |
2717 | { | |
61a127b3 | 2718 | finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type)); |
39c01e4c MM |
2719 | /* Put this tag on the list of tags for the class, since |
2720 | that won't happen below because B is not the class | |
2721 | binding level, but is instead the pseudo-global level. */ | |
68642fb6 | 2722 | b->level_chain->tags = |
e1b3e07d | 2723 | tree_cons (name, type, b->level_chain->tags); |
39c01e4c MM |
2724 | if (TYPE_SIZE (current_class_type) == NULL_TREE) |
2725 | CLASSTYPE_TAGS (current_class_type) = b->level_chain->tags; | |
2726 | } | |
2727 | } | |
2728 | } | |
2729 | ||
2730 | return decl; | |
2731 | } | |
2732 | ||
9188c363 MM |
2733 | /* In C++, you don't have to write `struct S' to refer to `S'; you |
2734 | can just use `S'. We accomplish this by creating a TYPE_DECL as | |
2735 | if the user had written `typedef struct S S'. Create and return | |
2736 | the TYPE_DECL for TYPE. */ | |
2737 | ||
2738 | tree | |
2739 | create_implicit_typedef (name, type) | |
2740 | tree name; | |
2741 | tree type; | |
2742 | { | |
2743 | tree decl; | |
2744 | ||
2745 | decl = build_decl (TYPE_DECL, name, type); | |
2746 | SET_DECL_ARTIFICIAL (decl); | |
2747 | /* There are other implicit type declarations, like the one *within* | |
2748 | a class that allows you to write `S::S'. We must distinguish | |
2749 | amongst these. */ | |
2750 | SET_DECL_IMPLICIT_TYPEDEF_P (decl); | |
2751 | TYPE_NAME (type) = decl; | |
2752 | ||
2753 | return decl; | |
2754 | } | |
2755 | ||
8d08fdba | 2756 | /* Push a tag name NAME for struct/class/union/enum type TYPE. |
6757edfe | 2757 | Normally put it into the inner-most non-tag-transparent scope, |
8d08fdba | 2758 | but if GLOBALIZE is true, put it in the inner-most non-class scope. |
e92cc029 | 2759 | The latter is needed for implicit declarations. */ |
8d08fdba MS |
2760 | |
2761 | void | |
2762 | pushtag (name, type, globalize) | |
2763 | tree name, type; | |
2764 | int globalize; | |
2765 | { | |
2766 | register struct binding_level *b; | |
8d08fdba | 2767 | |
8f032717 | 2768 | b = current_binding_level; |
8d08fdba MS |
2769 | while (b->tag_transparent |
2770 | || (globalize && b->parm_flag == 2)) | |
2771 | b = b->level_chain; | |
2772 | ||
e1b3e07d | 2773 | b->tags = tree_cons (name, type, b->tags); |
8d08fdba MS |
2774 | |
2775 | if (name) | |
2776 | { | |
8d08fdba | 2777 | /* Do C++ gratuitous typedefing. */ |
db5ae43f | 2778 | if (IDENTIFIER_TYPE_VALUE (name) != type) |
8d08fdba | 2779 | { |
93cdc044 | 2780 | register tree d = NULL_TREE; |
848cf1e0 | 2781 | int in_class = 0; |
9188c363 | 2782 | tree context = TYPE_CONTEXT (type); |
280f9385 | 2783 | |
280f9385 MM |
2784 | if (! context) |
2785 | { | |
2786 | tree cs = current_scope (); | |
2787 | ||
2788 | if (! globalize) | |
2789 | context = cs; | |
68642fb6 | 2790 | else if (cs != NULL_TREE |
280f9385 MM |
2791 | && TREE_CODE_CLASS (TREE_CODE (cs)) == 't') |
2792 | /* When declaring a friend class of a local class, we want | |
2793 | to inject the newly named class into the scope | |
2794 | containing the local class, not the namespace scope. */ | |
4f1c5b7d | 2795 | context = decl_function_context (get_type_decl (cs)); |
280f9385 | 2796 | } |
280f9385 MM |
2797 | if (!context) |
2798 | context = current_namespace; | |
8d08fdba | 2799 | |
74b846e0 | 2800 | if ((b->template_parms_p && b->level_chain->parm_flag == 2) |
93cdc044 JM |
2801 | || b->parm_flag == 2) |
2802 | in_class = 1; | |
93cdc044 | 2803 | |
848cf1e0 MM |
2804 | if (current_lang_name == lang_name_java) |
2805 | TYPE_FOR_JAVA (type) = 1; | |
93cdc044 | 2806 | |
9188c363 | 2807 | d = create_implicit_typedef (name, type); |
cb0dbb9a | 2808 | DECL_CONTEXT (d) = FROB_CONTEXT (context); |
9188c363 MM |
2809 | if (! in_class) |
2810 | set_identifier_type_value_with_scope (name, type, b); | |
e1cd6e56 | 2811 | |
39c01e4c MM |
2812 | d = maybe_process_template_type_declaration (type, |
2813 | globalize, b); | |
93cdc044 JM |
2814 | |
2815 | if (b->parm_flag == 2) | |
61a127b3 | 2816 | { |
848cf1e0 | 2817 | if (!PROCESSING_REAL_TEMPLATE_DECL_P ()) |
61a127b3 MM |
2818 | /* Put this TYPE_DECL on the TYPE_FIELDS list for the |
2819 | class. But if it's a member template class, we | |
2820 | want the TEMPLATE_DECL, not the TYPE_DECL, so this | |
2821 | is done later. */ | |
2822 | finish_member_declaration (d); | |
8f032717 MM |
2823 | else |
2824 | pushdecl_class_level (d); | |
61a127b3 | 2825 | } |
93cdc044 JM |
2826 | else |
2827 | d = pushdecl_with_scope (d, b); | |
2828 | ||
848cf1e0 MM |
2829 | if (ANON_AGGRNAME_P (name)) |
2830 | DECL_IGNORED_P (d) = 1; | |
2831 | ||
2832 | TYPE_CONTEXT (type) = DECL_CONTEXT (d); | |
2833 | DECL_ASSEMBLER_NAME (d) = DECL_NAME (d); | |
2834 | if (!uses_template_parms (type)) | |
2835 | DECL_ASSEMBLER_NAME (d) | |
2836 | = get_identifier (build_overload_name (type, 1, 1)); | |
8d08fdba MS |
2837 | } |
2838 | if (b->parm_flag == 2) | |
2839 | { | |
8d08fdba MS |
2840 | if (TYPE_SIZE (current_class_type) == NULL_TREE) |
2841 | CLASSTYPE_TAGS (current_class_type) = b->tags; | |
2842 | } | |
2843 | } | |
2844 | ||
2845 | if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL) | |
2846 | /* Use the canonical TYPE_DECL for this node. */ | |
2847 | TYPE_STUB_DECL (type) = TYPE_NAME (type); | |
2848 | else | |
2849 | { | |
2850 | /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE | |
2851 | will be the tagged type we just added to the current | |
2852 | binding level. This fake NULL-named TYPE_DECL node helps | |
2853 | dwarfout.c to know when it needs to output a | |
2854 | representation of a tagged type, and it also gives us a | |
2855 | convenient place to record the "scope start" address for | |
2856 | the tagged type. */ | |
2857 | ||
8d08fdba | 2858 | tree d = build_decl (TYPE_DECL, NULL_TREE, type); |
8d08fdba MS |
2859 | TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b); |
2860 | } | |
2861 | } | |
2862 | ||
2863 | /* Counter used to create anonymous type names. */ | |
e92cc029 | 2864 | |
8d08fdba MS |
2865 | static int anon_cnt = 0; |
2866 | ||
2867 | /* Return an IDENTIFIER which can be used as a name for | |
2868 | anonymous structs and unions. */ | |
e92cc029 | 2869 | |
8d08fdba MS |
2870 | tree |
2871 | make_anon_name () | |
2872 | { | |
2873 | char buf[32]; | |
2874 | ||
2875 | sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++); | |
2876 | return get_identifier (buf); | |
2877 | } | |
2878 | ||
2879 | /* Clear the TREE_PURPOSE slot of tags which have anonymous typenames. | |
2880 | This keeps dbxout from getting confused. */ | |
e92cc029 | 2881 | |
8d08fdba MS |
2882 | void |
2883 | clear_anon_tags () | |
2884 | { | |
2885 | register struct binding_level *b; | |
2886 | register tree tags; | |
2887 | static int last_cnt = 0; | |
2888 | ||
2889 | /* Fast out if no new anon names were declared. */ | |
2890 | if (last_cnt == anon_cnt) | |
2891 | return; | |
2892 | ||
2893 | b = current_binding_level; | |
2894 | while (b->tag_transparent) | |
2895 | b = b->level_chain; | |
2896 | tags = b->tags; | |
2897 | while (tags) | |
2898 | { | |
2899 | /* A NULL purpose means we have already processed all tags | |
2900 | from here to the end of the list. */ | |
2901 | if (TREE_PURPOSE (tags) == NULL_TREE) | |
2902 | break; | |
2903 | if (ANON_AGGRNAME_P (TREE_PURPOSE (tags))) | |
2904 | TREE_PURPOSE (tags) = NULL_TREE; | |
2905 | tags = TREE_CHAIN (tags); | |
2906 | } | |
2907 | last_cnt = anon_cnt; | |
2908 | } | |
2909 | \f | |
2910 | /* Subroutine of duplicate_decls: return truthvalue of whether | |
2911 | or not types of these decls match. | |
2912 | ||
2913 | For C++, we must compare the parameter list so that `int' can match | |
2914 | `int&' in a parameter position, but `int&' is not confused with | |
2915 | `const int&'. */ | |
e92cc029 | 2916 | |
6060a796 | 2917 | int |
8d08fdba MS |
2918 | decls_match (newdecl, olddecl) |
2919 | tree newdecl, olddecl; | |
2920 | { | |
2921 | int types_match; | |
2922 | ||
347d73d7 ML |
2923 | if (newdecl == olddecl) |
2924 | return 1; | |
2925 | ||
6b4b3deb MM |
2926 | if (TREE_CODE (newdecl) != TREE_CODE (olddecl)) |
2927 | /* If the two DECLs are not even the same kind of thing, we're not | |
2928 | interested in their types. */ | |
2929 | return 0; | |
2930 | ||
2931 | if (TREE_CODE (newdecl) == FUNCTION_DECL) | |
8d08fdba MS |
2932 | { |
2933 | tree f1 = TREE_TYPE (newdecl); | |
2934 | tree f2 = TREE_TYPE (olddecl); | |
2935 | tree p1 = TYPE_ARG_TYPES (f1); | |
2936 | tree p2 = TYPE_ARG_TYPES (f2); | |
2937 | ||
4f1c5b7d | 2938 | if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl) |
c5a6fc45 JM |
2939 | && ! (DECL_LANGUAGE (newdecl) == lang_c |
2940 | && DECL_LANGUAGE (olddecl) == lang_c)) | |
2941 | return 0; | |
2942 | ||
8d08fdba | 2943 | if (TREE_CODE (f1) != TREE_CODE (f2)) |
e867a179 | 2944 | return 0; |
8d08fdba | 2945 | |
3bfdc719 | 2946 | if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2))) |
8926095f | 2947 | { |
a28e3c7f | 2948 | if (! strict_prototypes_lang_c && DECL_LANGUAGE (olddecl) == lang_c |
8926095f | 2949 | && p2 == NULL_TREE) |
a28e3c7f MS |
2950 | { |
2951 | types_match = self_promoting_args_p (p1); | |
2952 | if (p1 == void_list_node) | |
2953 | TREE_TYPE (newdecl) = TREE_TYPE (olddecl); | |
2954 | } | |
2955 | else if (!strict_prototypes_lang_c && DECL_LANGUAGE (olddecl)==lang_c | |
2956 | && DECL_LANGUAGE (newdecl) == lang_c && p1 == NULL_TREE) | |
2957 | { | |
2958 | types_match = self_promoting_args_p (p2); | |
2959 | TREE_TYPE (newdecl) = TREE_TYPE (olddecl); | |
2960 | } | |
8926095f | 2961 | else |
91063b51 | 2962 | types_match = compparms (p1, p2); |
8926095f | 2963 | } |
8d08fdba MS |
2964 | else |
2965 | types_match = 0; | |
2966 | } | |
6b4b3deb | 2967 | else if (TREE_CODE (newdecl) == TEMPLATE_DECL) |
51c184be | 2968 | { |
f84b4be9 JM |
2969 | if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl), |
2970 | DECL_TEMPLATE_PARMS (olddecl))) | |
2971 | return 0; | |
68642fb6 | 2972 | |
f84b4be9 JM |
2973 | if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL) |
2974 | types_match = 1; | |
2975 | else | |
2976 | types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl), | |
2977 | DECL_TEMPLATE_RESULT (newdecl)); | |
51c184be | 2978 | } |
8d08fdba MS |
2979 | else |
2980 | { | |
2981 | if (TREE_TYPE (newdecl) == error_mark_node) | |
2982 | types_match = TREE_TYPE (olddecl) == error_mark_node; | |
2983 | else if (TREE_TYPE (olddecl) == NULL_TREE) | |
2984 | types_match = TREE_TYPE (newdecl) == NULL_TREE; | |
a0a33927 MS |
2985 | else if (TREE_TYPE (newdecl) == NULL_TREE) |
2986 | types_match = 0; | |
8d08fdba | 2987 | else |
01240200 | 2988 | types_match = comptypes (TREE_TYPE (newdecl), |
3bfdc719 MM |
2989 | TREE_TYPE (olddecl), |
2990 | COMPARE_REDECLARATION); | |
8d08fdba MS |
2991 | } |
2992 | ||
2993 | return types_match; | |
2994 | } | |
2995 | ||
2996 | /* If NEWDECL is `static' and an `extern' was seen previously, | |
24f30ed4 | 2997 | warn about it. OLDDECL is the previous declaration. |
8d08fdba MS |
2998 | |
2999 | Note that this does not apply to the C++ case of declaring | |
3000 | a variable `extern const' and then later `const'. | |
3001 | ||
8d08fdba MS |
3002 | Don't complain about built-in functions, since they are beyond |
3003 | the user's control. */ | |
3004 | ||
3005 | static void | |
3006 | warn_extern_redeclared_static (newdecl, olddecl) | |
3007 | tree newdecl, olddecl; | |
3008 | { | |
d8e178a0 | 3009 | static const char *explicit_extern_static_warning |
8251199e | 3010 | = "`%D' was declared `extern' and later `static'"; |
d8e178a0 | 3011 | static const char *implicit_extern_static_warning |
8251199e JM |
3012 | = "`%D' was declared implicitly `extern' and later `static'"; |
3013 | ||
24f30ed4 MM |
3014 | tree name; |
3015 | ||
68642fb6 | 3016 | if (TREE_CODE (newdecl) == TYPE_DECL |
7c355bca | 3017 | || TREE_CODE (newdecl) == TEMPLATE_DECL) |
8d08fdba | 3018 | return; |
68642fb6 | 3019 | |
963d5758 MM |
3020 | /* Don't get confused by static member functions; that's a different |
3021 | use of `static'. */ | |
3022 | if (TREE_CODE (newdecl) == FUNCTION_DECL | |
3023 | && DECL_STATIC_FUNCTION_P (newdecl)) | |
3024 | return; | |
8d08fdba | 3025 | |
24f30ed4 MM |
3026 | /* If the old declaration was `static', or the new one isn't, then |
3027 | then everything is OK. */ | |
3028 | if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl)) | |
3029 | return; | |
3030 | ||
3031 | /* It's OK to declare a builtin function as `static'. */ | |
3032 | if (TREE_CODE (olddecl) == FUNCTION_DECL | |
3033 | && DECL_ARTIFICIAL (olddecl)) | |
3034 | return; | |
3035 | ||
8d08fdba | 3036 | name = DECL_ASSEMBLER_NAME (newdecl); |
24f30ed4 MM |
3037 | cp_pedwarn (IDENTIFIER_IMPLICIT_DECL (name) |
3038 | ? implicit_extern_static_warning | |
3039 | : explicit_extern_static_warning, newdecl); | |
3040 | cp_pedwarn_at ("previous declaration of `%D'", olddecl); | |
8d08fdba MS |
3041 | } |
3042 | ||
3043 | /* Handle when a new declaration NEWDECL has the same name as an old | |
3044 | one OLDDECL in the same binding contour. Prints an error message | |
3045 | if appropriate. | |
3046 | ||
3047 | If safely possible, alter OLDDECL to look like NEWDECL, and return 1. | |
3048 | Otherwise, return 0. */ | |
3049 | ||
51c184be | 3050 | int |
8d08fdba | 3051 | duplicate_decls (newdecl, olddecl) |
824b9a4c | 3052 | tree newdecl, olddecl; |
8d08fdba | 3053 | { |
8d08fdba MS |
3054 | unsigned olddecl_uid = DECL_UID (olddecl); |
3055 | int olddecl_friend = 0, types_match = 0; | |
0b60dfe3 | 3056 | int new_defines_function = 0; |
5566b478 MS |
3057 | |
3058 | if (newdecl == olddecl) | |
3059 | return 1; | |
8d08fdba | 3060 | |
8926095f | 3061 | types_match = decls_match (newdecl, olddecl); |
8d08fdba | 3062 | |
8d08fdba MS |
3063 | /* If either the type of the new decl or the type of the old decl is an |
3064 | error_mark_node, then that implies that we have already issued an | |
3065 | error (earlier) for some bogus type specification, and in that case, | |
3066 | it is rather pointless to harass the user with yet more error message | |
0b60dfe3 | 3067 | about the same declaration, so just pretend the types match here. */ |
bd6dd845 MS |
3068 | if (TREE_TYPE (newdecl) == error_mark_node |
3069 | || TREE_TYPE (olddecl) == error_mark_node) | |
8d08fdba | 3070 | types_match = 1; |
68642fb6 | 3071 | |
0b60dfe3 | 3072 | /* Check for redeclaration and other discrepancies. */ |
d22c8596 | 3073 | if (TREE_CODE (olddecl) == FUNCTION_DECL |
9f33663b JM |
3074 | && DECL_ARTIFICIAL (olddecl)) |
3075 | { | |
3076 | if (TREE_CODE (newdecl) != FUNCTION_DECL) | |
3077 | { | |
3078 | /* If you declare a built-in or predefined function name as static, | |
3079 | the old definition is overridden, but optionally warn this was a | |
3080 | bad choice of name. */ | |
3081 | if (! TREE_PUBLIC (newdecl)) | |
3082 | { | |
3083 | if (warn_shadow) | |
3084 | cp_warning ("shadowing %s function `%#D'", | |
3085 | DECL_BUILT_IN (olddecl) ? "built-in" : "library", | |
3086 | olddecl); | |
3087 | /* Discard the old built-in function. */ | |
3088 | return 0; | |
3089 | } | |
3090 | /* If the built-in is not ansi, then programs can override | |
3091 | it even globally without an error. */ | |
3092 | else if (! DECL_BUILT_IN (olddecl)) | |
3093 | cp_warning ("library function `%#D' redeclared as non-function `%#D'", | |
3094 | olddecl, newdecl); | |
3095 | else | |
3096 | { | |
3097 | cp_error ("declaration of `%#D'", newdecl); | |
3098 | cp_error ("conflicts with built-in declaration `%#D'", | |
a4443a08 | 3099 | olddecl); |
9f33663b | 3100 | } |
a4443a08 MS |
3101 | return 0; |
3102 | } | |
9f33663b | 3103 | else if (!types_match) |
8d08fdba | 3104 | { |
9f33663b JM |
3105 | if ((DECL_LANGUAGE (newdecl) == lang_c |
3106 | && DECL_LANGUAGE (olddecl) == lang_c) | |
3107 | || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)), | |
3108 | TYPE_ARG_TYPES (TREE_TYPE (olddecl)))) | |
a4443a08 | 3109 | { |
9f33663b JM |
3110 | /* A near match; override the builtin. */ |
3111 | ||
3112 | if (TREE_PUBLIC (newdecl)) | |
a4443a08 | 3113 | { |
9f33663b JM |
3114 | cp_warning ("new declaration `%#D'", newdecl); |
3115 | cp_warning ("ambiguates built-in declaration `%#D'", | |
3116 | olddecl); | |
a4443a08 | 3117 | } |
9f33663b JM |
3118 | else if (warn_shadow) |
3119 | cp_warning ("shadowing %s function `%#D'", | |
3120 | DECL_BUILT_IN (olddecl) ? "built-in" : "library", | |
3121 | olddecl); | |
a4443a08 | 3122 | } |
9f33663b JM |
3123 | else |
3124 | /* Discard the old built-in function. */ | |
3125 | return 0; | |
8d08fdba | 3126 | } |
68642fb6 | 3127 | |
24f30ed4 MM |
3128 | if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl)) |
3129 | { | |
3130 | /* If a builtin function is redeclared as `static', merge | |
3131 | the declarations, but make the original one static. */ | |
3132 | DECL_THIS_STATIC (olddecl) = 1; | |
3133 | TREE_PUBLIC (olddecl) = 0; | |
9bfadf57 MM |
3134 | |
3135 | /* Make the olddeclaration consistent with the new one so that | |
3136 | all remnants of the builtin-ness of this function will be | |
3137 | banished. */ | |
3138 | DECL_LANGUAGE (olddecl) = DECL_LANGUAGE (newdecl); | |
3139 | DECL_RTL (olddecl) = DECL_RTL (newdecl); | |
3140 | DECL_ASSEMBLER_NAME (olddecl) = DECL_ASSEMBLER_NAME (newdecl); | |
3141 | SET_IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (newdecl), | |
3142 | newdecl); | |
24f30ed4 | 3143 | } |
39211cd5 MS |
3144 | } |
3145 | else if (TREE_CODE (olddecl) != TREE_CODE (newdecl)) | |
3146 | { | |
9ed182dc JM |
3147 | if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl) |
3148 | && TREE_CODE (newdecl) != TYPE_DECL | |
3149 | && ! (TREE_CODE (newdecl) == TEMPLATE_DECL | |
3150 | && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)) | |
3151 | || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl) | |
3152 | && TREE_CODE (olddecl) != TYPE_DECL | |
3153 | && ! (TREE_CODE (olddecl) == TEMPLATE_DECL | |
3154 | && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) | |
3155 | == TYPE_DECL)))) | |
3156 | { | |
3157 | /* We do nothing special here, because C++ does such nasty | |
3158 | things with TYPE_DECLs. Instead, just let the TYPE_DECL | |
3159 | get shadowed, and know that if we need to find a TYPE_DECL | |
3160 | for a given name, we can look in the IDENTIFIER_TYPE_VALUE | |
3161 | slot of the identifier. */ | |
3162 | return 0; | |
3163 | } | |
3164 | ||
39211cd5 | 3165 | if ((TREE_CODE (newdecl) == FUNCTION_DECL |
5566b478 | 3166 | && DECL_FUNCTION_TEMPLATE_P (olddecl)) |
39211cd5 | 3167 | || (TREE_CODE (olddecl) == FUNCTION_DECL |
5566b478 | 3168 | && DECL_FUNCTION_TEMPLATE_P (newdecl))) |
39211cd5 | 3169 | return 0; |
9ed182dc | 3170 | |
8251199e | 3171 | cp_error ("`%#D' redeclared as different kind of symbol", newdecl); |
39211cd5 MS |
3172 | if (TREE_CODE (olddecl) == TREE_LIST) |
3173 | olddecl = TREE_VALUE (olddecl); | |
8251199e | 3174 | cp_error_at ("previous declaration of `%#D'", olddecl); |
39211cd5 MS |
3175 | |
3176 | /* New decl is completely inconsistent with the old one => | |
3177 | tell caller to replace the old one. */ | |
3178 | ||
3179 | return 0; | |
8d08fdba | 3180 | } |
8d08fdba MS |
3181 | else if (!types_match) |
3182 | { | |
4f1c5b7d | 3183 | if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)) |
7bdbfa05 MM |
3184 | /* These are certainly not duplicate declarations; they're |
3185 | from different scopes. */ | |
3186 | return 0; | |
3187 | ||
8926095f | 3188 | if (TREE_CODE (newdecl) == TEMPLATE_DECL) |
f0e01782 MS |
3189 | { |
3190 | /* The name of a class template may not be declared to refer to | |
3191 | any other template, class, function, object, namespace, value, | |
e92cc029 | 3192 | or type in the same scope. */ |
5566b478 MS |
3193 | if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL |
3194 | || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL) | |
f0e01782 | 3195 | { |
8251199e JM |
3196 | cp_error ("declaration of template `%#D'", newdecl); |
3197 | cp_error_at ("conflicts with previous declaration `%#D'", | |
f0e01782 MS |
3198 | olddecl); |
3199 | } | |
ec255269 MS |
3200 | else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL |
3201 | && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL | |
3202 | && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))), | |
91063b51 | 3203 | TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl)))) |
75650646 MM |
3204 | && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl), |
3205 | DECL_TEMPLATE_PARMS (olddecl))) | |
ec255269 | 3206 | { |
8251199e JM |
3207 | cp_error ("new declaration `%#D'", newdecl); |
3208 | cp_error_at ("ambiguates old declaration `%#D'", olddecl); | |
ec255269 | 3209 | } |
f0e01782 MS |
3210 | return 0; |
3211 | } | |
8926095f MS |
3212 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
3213 | { | |
3214 | if (DECL_LANGUAGE (newdecl) == lang_c | |
3215 | && DECL_LANGUAGE (olddecl) == lang_c) | |
3216 | { | |
8251199e | 3217 | cp_error ("declaration of C function `%#D' conflicts with", |
8926095f | 3218 | newdecl); |
8251199e | 3219 | cp_error_at ("previous declaration `%#D' here", olddecl); |
8926095f | 3220 | } |
00595019 | 3221 | else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)), |
91063b51 | 3222 | TYPE_ARG_TYPES (TREE_TYPE (olddecl)))) |
700f8a87 | 3223 | { |
8251199e JM |
3224 | cp_error ("new declaration `%#D'", newdecl); |
3225 | cp_error_at ("ambiguates old declaration `%#D'", olddecl); | |
700f8a87 MS |
3226 | } |
3227 | else | |
3228 | return 0; | |
8926095f | 3229 | } |
8d08fdba MS |
3230 | |
3231 | /* Already complained about this, so don't do so again. */ | |
a4443a08 | 3232 | else if (current_class_type == NULL_TREE |
8d08fdba MS |
3233 | || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type) |
3234 | { | |
8251199e JM |
3235 | cp_error ("conflicting types for `%#D'", newdecl); |
3236 | cp_error_at ("previous declaration as `%#D'", olddecl); | |
8d08fdba MS |
3237 | } |
3238 | } | |
68642fb6 | 3239 | else if (TREE_CODE (newdecl) == FUNCTION_DECL |
75650646 MM |
3240 | && ((DECL_TEMPLATE_SPECIALIZATION (olddecl) |
3241 | && (!DECL_TEMPLATE_INFO (newdecl) | |
68642fb6 | 3242 | || (DECL_TI_TEMPLATE (newdecl) |
75650646 MM |
3243 | != DECL_TI_TEMPLATE (olddecl)))) |
3244 | || (DECL_TEMPLATE_SPECIALIZATION (newdecl) | |
3245 | && (!DECL_TEMPLATE_INFO (olddecl) | |
68642fb6 | 3246 | || (DECL_TI_TEMPLATE (olddecl) |
75650646 | 3247 | != DECL_TI_TEMPLATE (newdecl)))))) |
386b8a85 JM |
3248 | /* It's OK to have a template specialization and a non-template |
3249 | with the same type, or to have specializations of two | |
75650646 MM |
3250 | different templates with the same type. Note that if one is a |
3251 | specialization, and the other is an instantiation of the same | |
3252 | template, that we do not exit at this point. That situation | |
3253 | can occur if we instantiate a template class, and then | |
3254 | specialize one of its methods. This situation is legal, but | |
3255 | the declarations must be merged in the usual way. */ | |
3256 | return 0; | |
68642fb6 UD |
3257 | else if (TREE_CODE (newdecl) == FUNCTION_DECL |
3258 | && ((DECL_TEMPLATE_INSTANTIATION (olddecl) | |
75650646 MM |
3259 | && !DECL_USE_TEMPLATE (newdecl)) |
3260 | || (DECL_TEMPLATE_INSTANTIATION (newdecl) | |
3261 | && !DECL_USE_TEMPLATE (olddecl)))) | |
3262 | /* One of the declarations is a template instantiation, and the | |
3263 | other is not a template at all. That's OK. */ | |
386b8a85 | 3264 | return 0; |
85c6cbaf ML |
3265 | else if (TREE_CODE (newdecl) == NAMESPACE_DECL |
3266 | && DECL_NAMESPACE_ALIAS (newdecl) | |
3267 | && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl)) | |
3268 | /* Redeclaration of namespace alias, ignore it. */ | |
3269 | return 1; | |
8d08fdba MS |
3270 | else |
3271 | { | |
d8e178a0 | 3272 | const char *errmsg = redeclaration_error_message (newdecl, olddecl); |
8251199e | 3273 | if (errmsg) |
8d08fdba | 3274 | { |
8251199e | 3275 | cp_error (errmsg, newdecl); |
8d08fdba MS |
3276 | if (DECL_NAME (olddecl) != NULL_TREE) |
3277 | cp_error_at ((DECL_INITIAL (olddecl) | |
2c73f9f5 | 3278 | && namespace_bindings_p ()) |
8251199e JM |
3279 | ? "`%#D' previously defined here" |
3280 | : "`%#D' previously declared here", olddecl); | |
8d08fdba MS |
3281 | } |
3282 | else if (TREE_CODE (olddecl) == FUNCTION_DECL | |
3283 | && DECL_INITIAL (olddecl) != NULL_TREE | |
3284 | && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE | |
3285 | && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE) | |
3286 | { | |
3287 | /* Prototype decl follows defn w/o prototype. */ | |
8251199e JM |
3288 | cp_warning_at ("prototype for `%#D'", newdecl); |
3289 | cp_warning_at ("follows non-prototype definition here", olddecl); | |
8d08fdba MS |
3290 | } |
3291 | else if (TREE_CODE (olddecl) == FUNCTION_DECL | |
3292 | && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl)) | |
8926095f MS |
3293 | { |
3294 | /* extern "C" int foo (); | |
3295 | int foo () { bar (); } | |
3296 | is OK. */ | |
9cd64686 MM |
3297 | if (current_lang_stack |
3298 | == &VARRAY_TREE (current_lang_base, 0)) | |
a28e3c7f | 3299 | DECL_LANGUAGE (newdecl) = DECL_LANGUAGE (olddecl); |
8926095f MS |
3300 | else |
3301 | { | |
8251199e | 3302 | cp_error_at ("previous declaration of `%#D' with %L linkage", |
8926095f | 3303 | olddecl, DECL_LANGUAGE (olddecl)); |
8251199e | 3304 | cp_error ("conflicts with new declaration with %L linkage", |
8926095f MS |
3305 | DECL_LANGUAGE (newdecl)); |
3306 | } | |
3307 | } | |
e1cd6e56 | 3308 | |
a6f02587 | 3309 | if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl)) |
c32381b1 JM |
3310 | ; |
3311 | else if (TREE_CODE (olddecl) == FUNCTION_DECL) | |
e1cd6e56 MS |
3312 | { |
3313 | tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl)); | |
3314 | tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)); | |
3315 | int i = 1; | |
3316 | ||
3317 | if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE) | |
3318 | t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2); | |
68642fb6 | 3319 | |
e1cd6e56 MS |
3320 | for (; t1 && t1 != void_list_node; |
3321 | t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++) | |
3322 | if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2)) | |
3323 | { | |
2507f3b5 RK |
3324 | if (1 == simple_cst_equal (TREE_PURPOSE (t1), |
3325 | TREE_PURPOSE (t2))) | |
e1cd6e56 MS |
3326 | { |
3327 | if (pedantic) | |
3328 | { | |
8251199e | 3329 | cp_pedwarn ("default argument given for parameter %d of `%#D'", |
e1cd6e56 | 3330 | i, newdecl); |
8251199e | 3331 | cp_pedwarn_at ("after previous specification in `%#D'", |
e1cd6e56 MS |
3332 | olddecl); |
3333 | } | |
3334 | } | |
3335 | else | |
3336 | { | |
8251199e | 3337 | cp_error ("default argument given for parameter %d of `%#D'", |
e1cd6e56 | 3338 | i, newdecl); |
8251199e | 3339 | cp_error_at ("after previous specification in `%#D'", |
e1cd6e56 MS |
3340 | olddecl); |
3341 | } | |
3342 | } | |
a5894242 | 3343 | |
7fcdf4c2 MS |
3344 | if (DECL_THIS_INLINE (newdecl) && ! DECL_THIS_INLINE (olddecl) |
3345 | && TREE_ADDRESSABLE (olddecl) && warn_inline) | |
dff6b454 | 3346 | { |
8251199e | 3347 | cp_warning ("`%#D' was used before it was declared inline", |
7fcdf4c2 | 3348 | newdecl); |
8251199e | 3349 | cp_warning_at ("previous non-inline declaration here", |
7fcdf4c2 | 3350 | olddecl); |
dff6b454 | 3351 | } |
e1cd6e56 | 3352 | } |
8d08fdba MS |
3353 | } |
3354 | ||
3355 | /* If new decl is `static' and an `extern' was seen previously, | |
3356 | warn about it. */ | |
3357 | warn_extern_redeclared_static (newdecl, olddecl); | |
3358 | ||
e92cc029 | 3359 | /* We have committed to returning 1 at this point. */ |
8d08fdba MS |
3360 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
3361 | { | |
3362 | /* Now that functions must hold information normally held | |
3363 | by field decls, there is extra work to do so that | |
3364 | declaration information does not get destroyed during | |
3365 | definition. */ | |
3366 | if (DECL_VINDEX (olddecl)) | |
3367 | DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl); | |
4f1c5b7d MM |
3368 | if (DECL_VIRTUAL_CONTEXT (olddecl)) |
3369 | DECL_VIRTUAL_CONTEXT (newdecl) = DECL_VIRTUAL_CONTEXT (olddecl); | |
8d08fdba MS |
3370 | if (DECL_CONTEXT (olddecl)) |
3371 | DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl); | |
8d08fdba MS |
3372 | if (DECL_PENDING_INLINE_INFO (newdecl) == (struct pending_inline *)0) |
3373 | DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl); | |
e1cd6e56 MS |
3374 | DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl); |
3375 | DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl); | |
fee7654e | 3376 | DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl); |
864b83b9 | 3377 | DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl); |
4a67c9e9 | 3378 | DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl); |
963d5758 | 3379 | DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl); |
0b60dfe3 | 3380 | new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE; |
68642fb6 | 3381 | |
0b60dfe3 BK |
3382 | /* Optionally warn about more than one declaration for the same |
3383 | name, but don't warn about a function declaration followed by a | |
3384 | definition. */ | |
3385 | if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl) | |
3386 | && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE) | |
3387 | /* Don't warn about extern decl followed by definition. */ | |
3388 | && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl)) | |
3389 | /* Don't warn about friends, let add_friend take care of it. */ | |
3390 | && ! DECL_FRIEND_P (newdecl)) | |
3391 | { | |
8251199e JM |
3392 | cp_warning ("redundant redeclaration of `%D' in same scope", newdecl); |
3393 | cp_warning_at ("previous declaration of `%D'", olddecl); | |
0b60dfe3 | 3394 | } |
8d08fdba MS |
3395 | } |
3396 | ||
3397 | /* Deal with C++: must preserve virtual function table size. */ | |
3398 | if (TREE_CODE (olddecl) == TYPE_DECL) | |
3399 | { | |
3400 | register tree newtype = TREE_TYPE (newdecl); | |
3401 | register tree oldtype = TREE_TYPE (olddecl); | |
3402 | ||
3403 | if (newtype != error_mark_node && oldtype != error_mark_node | |
3404 | && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype)) | |
3405 | { | |
3406 | CLASSTYPE_VSIZE (newtype) = CLASSTYPE_VSIZE (oldtype); | |
3407 | CLASSTYPE_FRIEND_CLASSES (newtype) | |
3408 | = CLASSTYPE_FRIEND_CLASSES (oldtype); | |
3409 | } | |
8d08fdba MS |
3410 | } |
3411 | ||
8d08fdba MS |
3412 | /* Copy all the DECL_... slots specified in the new decl |
3413 | except for any that we copy here from the old type. */ | |
68642fb6 | 3414 | DECL_MACHINE_ATTRIBUTES (newdecl) |
0b60dfe3 | 3415 | = merge_machine_decl_attributes (olddecl, newdecl); |
8d08fdba | 3416 | |
5566b478 MS |
3417 | if (TREE_CODE (newdecl) == TEMPLATE_DECL) |
3418 | { | |
4d85e00e MM |
3419 | if (! duplicate_decls (DECL_TEMPLATE_RESULT (newdecl), |
3420 | DECL_TEMPLATE_RESULT (olddecl))) | |
8251199e | 3421 | cp_error ("invalid redeclaration of %D", newdecl); |
4d85e00e | 3422 | TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl)); |
68642fb6 | 3423 | DECL_TEMPLATE_SPECIALIZATIONS (olddecl) |
4d85e00e MM |
3424 | = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl), |
3425 | DECL_TEMPLATE_SPECIALIZATIONS (newdecl)); | |
68642fb6 | 3426 | |
5566b478 MS |
3427 | return 1; |
3428 | } | |
68642fb6 | 3429 | |
8d08fdba MS |
3430 | if (types_match) |
3431 | { | |
3432 | /* Automatically handles default parameters. */ | |
3433 | tree oldtype = TREE_TYPE (olddecl); | |
e1cd6e56 | 3434 | tree newtype; |
8d08fdba | 3435 | |
e1cd6e56 MS |
3436 | /* Merge the data types specified in the two decls. */ |
3437 | newtype = common_type (TREE_TYPE (newdecl), TREE_TYPE (olddecl)); | |
3438 | ||
8d08fdba MS |
3439 | if (TREE_CODE (newdecl) == VAR_DECL) |
3440 | DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl); | |
3441 | /* Do this after calling `common_type' so that default | |
3442 | parameters don't confuse us. */ | |
3443 | else if (TREE_CODE (newdecl) == FUNCTION_DECL | |
3444 | && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)) | |
3445 | != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)))) | |
3446 | { | |
f30432d7 | 3447 | TREE_TYPE (newdecl) = build_exception_variant (newtype, |
8d08fdba | 3448 | TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))); |
f30432d7 | 3449 | TREE_TYPE (olddecl) = build_exception_variant (newtype, |
8d08fdba MS |
3450 | TYPE_RAISES_EXCEPTIONS (oldtype)); |
3451 | ||
9a224b4a JM |
3452 | if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl)) |
3453 | && DECL_SOURCE_LINE (olddecl) != 0 | |
da20811c | 3454 | && flag_exceptions |
4cc1d462 NS |
3455 | && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)), |
3456 | TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1)) | |
8d08fdba | 3457 | { |
4cc1d462 | 3458 | cp_error ("declaration of `%F' throws different exceptions", |
a28e3c7f | 3459 | newdecl); |
a09ba2e0 | 3460 | cp_error_at ("than previous declaration `%F'", olddecl); |
8d08fdba MS |
3461 | } |
3462 | } | |
3463 | TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype; | |
3464 | ||
3465 | /* Lay the type out, unless already done. */ | |
407f03b8 | 3466 | if (! same_type_p (newtype, oldtype) |
5566b478 | 3467 | && TREE_TYPE (newdecl) != error_mark_node |
5156628f | 3468 | && !(processing_template_decl && uses_template_parms (newdecl))) |
b7484fbe MS |
3469 | layout_type (TREE_TYPE (newdecl)); |
3470 | ||
5566b478 MS |
3471 | if ((TREE_CODE (newdecl) == VAR_DECL |
3472 | || TREE_CODE (newdecl) == PARM_DECL | |
3473 | || TREE_CODE (newdecl) == RESULT_DECL | |
3474 | || TREE_CODE (newdecl) == FIELD_DECL | |
3475 | || TREE_CODE (newdecl) == TYPE_DECL) | |
5156628f | 3476 | && !(processing_template_decl && uses_template_parms (newdecl))) |
b7484fbe | 3477 | layout_decl (newdecl, 0); |
8d08fdba MS |
3478 | |
3479 | /* Merge the type qualifiers. */ | |
3480 | if (TREE_READONLY (newdecl)) | |
3481 | TREE_READONLY (olddecl) = 1; | |
3482 | if (TREE_THIS_VOLATILE (newdecl)) | |
3483 | TREE_THIS_VOLATILE (olddecl) = 1; | |
3484 | ||
3485 | /* Merge the initialization information. */ | |
8926095f MS |
3486 | if (DECL_INITIAL (newdecl) == NULL_TREE |
3487 | && DECL_INITIAL (olddecl) != NULL_TREE) | |
3488 | { | |
3489 | DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl); | |
3490 | DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl); | |
3491 | DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl); | |
b0d06515 MM |
3492 | if (CAN_HAVE_FULL_LANG_DECL_P (newdecl) |
3493 | && DECL_LANG_SPECIFIC (newdecl) | |
4d85e00e MM |
3494 | && DECL_LANG_SPECIFIC (olddecl)) |
3495 | DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl); | |
8926095f | 3496 | } |
39211cd5 MS |
3497 | |
3498 | /* Merge the section attribute. | |
3499 | We want to issue an error if the sections conflict but that must be | |
3500 | done later in decl_attributes since we are called before attributes | |
3501 | are assigned. */ | |
3502 | if (DECL_SECTION_NAME (newdecl) == NULL_TREE) | |
3503 | DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl); | |
3504 | ||
5d73aa63 MM |
3505 | /* Keep the old rtl since we can safely use it. */ |
3506 | DECL_RTL (newdecl) = DECL_RTL (olddecl); | |
a157febd GK |
3507 | |
3508 | if (TREE_CODE (newdecl) == FUNCTION_DECL) | |
3509 | { | |
3510 | DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl) | |
3511 | |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl); | |
3512 | DECL_NO_CHECK_MEMORY_USAGE (newdecl) | |
3513 | |= DECL_NO_CHECK_MEMORY_USAGE (olddecl); | |
3514 | DECL_NO_LIMIT_STACK (newdecl) | |
3515 | |= DECL_NO_LIMIT_STACK (olddecl); | |
3516 | } | |
8d08fdba MS |
3517 | } |
3518 | /* If cannot merge, then use the new type and qualifiers, | |
3519 | and don't preserve the old rtl. */ | |
3520 | else | |
3521 | { | |
3522 | /* Clean out any memory we had of the old declaration. */ | |
3523 | tree oldstatic = value_member (olddecl, static_aggregates); | |
3524 | if (oldstatic) | |
3525 | TREE_VALUE (oldstatic) = error_mark_node; | |
3526 | ||
3527 | TREE_TYPE (olddecl) = TREE_TYPE (newdecl); | |
3528 | TREE_READONLY (olddecl) = TREE_READONLY (newdecl); | |
3529 | TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl); | |
3530 | TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl); | |
3531 | } | |
3532 | ||
3533 | /* Merge the storage class information. */ | |
a9aedbc2 | 3534 | DECL_WEAK (newdecl) |= DECL_WEAK (olddecl); |
e92cc029 | 3535 | DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl); |
794d4a61 | 3536 | DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl); |
a9aedbc2 MS |
3537 | TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl); |
3538 | TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl); | |
3539 | if (! DECL_EXTERNAL (olddecl)) | |
3540 | DECL_EXTERNAL (newdecl) = 0; | |
68642fb6 | 3541 | |
0b60dfe3 | 3542 | if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl)) |
8d08fdba | 3543 | { |
a9aedbc2 MS |
3544 | DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl); |
3545 | DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl); | |
77be6f82 | 3546 | DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl); |
68642fb6 | 3547 | DECL_TEMPLATE_INSTANTIATED (newdecl) |
2b0a63a3 | 3548 | |= DECL_TEMPLATE_INSTANTIATED (olddecl); |
0b60dfe3 BK |
3549 | /* Don't really know how much of the language-specific |
3550 | values we should copy from old to new. */ | |
3551 | DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl); | |
3552 | DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl); | |
3553 | DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl); | |
fbf1c34b | 3554 | DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl); |
0b60dfe3 | 3555 | olddecl_friend = DECL_FRIEND_P (olddecl); |
6a629cac MM |
3556 | |
3557 | /* Only functions have DECL_BEFRIENDING_CLASSES. */ | |
3558 | if (TREE_CODE (newdecl) == FUNCTION_DECL | |
3559 | || DECL_FUNCTION_TEMPLATE_P (newdecl)) | |
3560 | DECL_BEFRIENDING_CLASSES (newdecl) | |
3561 | = chainon (DECL_BEFRIENDING_CLASSES (newdecl), | |
3562 | DECL_BEFRIENDING_CLASSES (olddecl)); | |
8d08fdba MS |
3563 | } |
3564 | ||
8d08fdba MS |
3565 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
3566 | { | |
68642fb6 UD |
3567 | if (DECL_TEMPLATE_INSTANTIATION (olddecl) |
3568 | && !DECL_TEMPLATE_INSTANTIATION (newdecl)) | |
75650646 MM |
3569 | { |
3570 | /* If newdecl is not a specialization, then it is not a | |
3571 | template-related function at all. And that means that we | |
3572 | shoud have exited above, returning 0. */ | |
3573 | my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl), | |
3574 | 0); | |
3575 | ||
68642fb6 | 3576 | if (TREE_USED (olddecl)) |
75650646 | 3577 | /* From [temp.expl.spec]: |
68642fb6 | 3578 | |
75650646 MM |
3579 | If a template, a member template or the member of a class |
3580 | template is explicitly specialized then that | |
3581 | specialization shall be declared before the first use of | |
3582 | that specialization that would cause an implicit | |
3583 | instantiation to take place, in every translation unit in | |
3584 | which such a use occurs. */ | |
68642fb6 | 3585 | cp_error ("explicit specialization of %D after first use", |
75650646 MM |
3586 | olddecl); |
3587 | ||
3588 | SET_DECL_TEMPLATE_SPECIALIZATION (olddecl); | |
3589 | } | |
faae18ab MS |
3590 | DECL_THIS_INLINE (newdecl) |= DECL_THIS_INLINE (olddecl); |
3591 | ||
3592 | /* If either decl says `inline', this fn is inline, unless its | |
3593 | definition was passed already. */ | |
3594 | if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE) | |
3595 | DECL_INLINE (olddecl) = 1; | |
3596 | DECL_INLINE (newdecl) = DECL_INLINE (olddecl); | |
3597 | ||
700f8a87 MS |
3598 | if (! types_match) |
3599 | { | |
3600 | DECL_LANGUAGE (olddecl) = DECL_LANGUAGE (newdecl); | |
3601 | DECL_ASSEMBLER_NAME (olddecl) = DECL_ASSEMBLER_NAME (newdecl); | |
5566b478 MS |
3602 | DECL_RTL (olddecl) = DECL_RTL (newdecl); |
3603 | } | |
3604 | if (! types_match || new_defines_function) | |
3605 | { | |
6f1b4c42 JM |
3606 | /* These need to be copied so that the names are available. |
3607 | Note that if the types do match, we'll preserve inline | |
3608 | info and other bits, but if not, we won't. */ | |
700f8a87 MS |
3609 | DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl); |
3610 | DECL_RESULT (olddecl) = DECL_RESULT (newdecl); | |
700f8a87 | 3611 | } |
8d08fdba MS |
3612 | if (new_defines_function) |
3613 | /* If defining a function declared with other language | |
3614 | linkage, use the previously declared language linkage. */ | |
3615 | DECL_LANGUAGE (newdecl) = DECL_LANGUAGE (olddecl); | |
6f1b4c42 | 3616 | else if (types_match) |
8d08fdba MS |
3617 | { |
3618 | /* If redeclaring a builtin function, and not a definition, | |
3619 | it stays built in. */ | |
3620 | if (DECL_BUILT_IN (olddecl)) | |
3621 | { | |
26db82d8 | 3622 | DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl); |
39211cd5 | 3623 | DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl); |
8d08fdba MS |
3624 | /* If we're keeping the built-in definition, keep the rtl, |
3625 | regardless of declaration matches. */ | |
3626 | DECL_RTL (newdecl) = DECL_RTL (olddecl); | |
3627 | } | |
3628 | else | |
3629 | DECL_FRAME_SIZE (newdecl) = DECL_FRAME_SIZE (olddecl); | |
3630 | ||
3631 | DECL_RESULT (newdecl) = DECL_RESULT (olddecl); | |
8926095f | 3632 | if ((DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl))) |
8d08fdba MS |
3633 | /* Previously saved insns go together with |
3634 | the function's previous definition. */ | |
3635 | DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl); | |
3636 | /* Don't clear out the arguments if we're redefining a function. */ | |
3637 | if (DECL_ARGUMENTS (olddecl)) | |
3638 | DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl); | |
3639 | } | |
6060a796 MS |
3640 | if (DECL_LANG_SPECIFIC (olddecl)) |
3641 | DECL_MAIN_VARIANT (newdecl) = DECL_MAIN_VARIANT (olddecl); | |
8d08fdba MS |
3642 | } |
3643 | ||
a9aedbc2 MS |
3644 | if (TREE_CODE (newdecl) == NAMESPACE_DECL) |
3645 | { | |
3646 | NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl); | |
3647 | } | |
3648 | ||
8d08fdba MS |
3649 | /* Now preserve various other info from the definition. */ |
3650 | TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl); | |
3651 | TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl); | |
3652 | DECL_COMMON (newdecl) = DECL_COMMON (olddecl); | |
f376e137 | 3653 | DECL_ASSEMBLER_NAME (newdecl) = DECL_ASSEMBLER_NAME (olddecl); |
8d08fdba | 3654 | |
8d08fdba MS |
3655 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
3656 | { | |
3657 | int function_size; | |
8d08fdba MS |
3658 | |
3659 | function_size = sizeof (struct tree_decl); | |
3660 | ||
3661 | bcopy ((char *) newdecl + sizeof (struct tree_common), | |
3662 | (char *) olddecl + sizeof (struct tree_common), | |
3663 | function_size - sizeof (struct tree_common)); | |
3664 | ||
75650646 MM |
3665 | if (DECL_TEMPLATE_INSTANTIATION (newdecl)) |
3666 | { | |
3667 | /* If newdecl is a template instantiation, it is possible that | |
3668 | the following sequence of events has occurred: | |
3669 | ||
3670 | o A friend function was declared in a class template. The | |
68642fb6 | 3671 | class template was instantiated. |
75650646 | 3672 | |
68642fb6 UD |
3673 | o The instantiation of the friend declaration was |
3674 | recorded on the instantiation list, and is newdecl. | |
75650646 MM |
3675 | |
3676 | o Later, however, instantiate_class_template called pushdecl | |
3677 | on the newdecl to perform name injection. But, pushdecl in | |
3678 | turn called duplicate_decls when it discovered that another | |
3679 | declaration of a global function with the same name already | |
68642fb6 | 3680 | existed. |
75650646 MM |
3681 | |
3682 | o Here, in duplicate_decls, we decided to clobber newdecl. | |
3683 | ||
3684 | If we're going to do that, we'd better make sure that | |
3685 | olddecl, and not newdecl, is on the list of | |
3686 | instantiations so that if we try to do the instantiation | |
3687 | again we won't get the clobbered declaration. */ | |
3688 | ||
68642fb6 UD |
3689 | tree tmpl = DECL_TI_TEMPLATE (newdecl); |
3690 | tree decls = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); | |
75650646 MM |
3691 | |
3692 | for (; decls; decls = TREE_CHAIN (decls)) | |
3693 | if (TREE_VALUE (decls) == newdecl) | |
3694 | TREE_VALUE (decls) = olddecl; | |
3695 | } | |
8d08fdba MS |
3696 | } |
3697 | else | |
3698 | { | |
3699 | bcopy ((char *) newdecl + sizeof (struct tree_common), | |
3700 | (char *) olddecl + sizeof (struct tree_common), | |
3701 | sizeof (struct tree_decl) - sizeof (struct tree_common) | |
3702 | + tree_code_length [(int)TREE_CODE (newdecl)] * sizeof (char *)); | |
3703 | } | |
3704 | ||
3705 | DECL_UID (olddecl) = olddecl_uid; | |
3706 | if (olddecl_friend) | |
3707 | DECL_FRIEND_P (olddecl) = 1; | |
3708 | ||
d9525bec BK |
3709 | /* NEWDECL contains the merged attribute lists. |
3710 | Update OLDDECL to be the same. */ | |
3711 | DECL_MACHINE_ATTRIBUTES (olddecl) = DECL_MACHINE_ATTRIBUTES (newdecl); | |
3712 | ||
8d08fdba MS |
3713 | return 1; |
3714 | } | |
3715 | ||
3716 | /* Record a decl-node X as belonging to the current lexical scope. | |
3717 | Check for errors (such as an incompatible declaration for the same | |
3718 | name already seen in the same scope). | |
3719 | ||
3720 | Returns either X or an old decl for the same name. | |
3721 | If an old decl is returned, it may have been smashed | |
3722 | to agree with what X says. */ | |
3723 | ||
3724 | tree | |
3725 | pushdecl (x) | |
3726 | tree x; | |
3727 | { | |
3728 | register tree t; | |
b35d4555 MM |
3729 | register tree name; |
3730 | int need_new_binding; | |
3731 | ||
3732 | /* We shouldn't be calling pushdecl when we're generating RTL for a | |
3733 | function that we already did semantic analysis on previously. */ | |
01d939e8 | 3734 | my_friendly_assert (!cfun || doing_semantic_analysis_p (), |
b35d4555 MM |
3735 | 19990913); |
3736 | ||
3737 | name = DECL_ASSEMBLER_NAME (x); | |
3738 | need_new_binding = 1; | |
8d08fdba | 3739 | |
50714e79 MM |
3740 | if (DECL_TEMPLATE_PARM_P (x)) |
3741 | /* Template parameters have no context; they are not X::T even | |
3742 | when declared within a class or namespace. */ | |
3743 | ; | |
3744 | else | |
3745 | { | |
3746 | if (current_function_decl && x != current_function_decl | |
3747 | /* A local declaration for a function doesn't constitute | |
3748 | nesting. */ | |
f39ee884 MM |
3749 | && !(TREE_CODE (x) == FUNCTION_DECL && !DECL_INITIAL (x)) |
3750 | /* A local declaration for an `extern' variable is in the | |
3751 | scoped of the current namespace, not the current | |
3752 | function. */ | |
3753 | && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x)) | |
50714e79 MM |
3754 | && !DECL_CONTEXT (x)) |
3755 | DECL_CONTEXT (x) = current_function_decl; | |
3756 | if (!DECL_CONTEXT (x)) | |
3757 | DECL_CONTEXT (x) = FROB_CONTEXT (current_namespace); | |
cd9f6678 MM |
3758 | |
3759 | /* If this is the declaration for a namespace-scope function, | |
3760 | but the declaration itself is in a local scope, mark the | |
3761 | declaration. */ | |
68642fb6 | 3762 | if (TREE_CODE (x) == FUNCTION_DECL |
cd9f6678 MM |
3763 | && DECL_NAMESPACE_SCOPE_P (x) |
3764 | && current_function_decl | |
3765 | && x != current_function_decl) | |
3766 | DECL_LOCAL_FUNCTION_P (x) = 1; | |
50714e79 | 3767 | } |
8d08fdba | 3768 | |
8d08fdba | 3769 | /* Type are looked up using the DECL_NAME, as that is what the rest of the |
e92cc029 | 3770 | compiler wants to use. */ |
a9aedbc2 | 3771 | if (TREE_CODE (x) == TYPE_DECL || TREE_CODE (x) == VAR_DECL |
0c20d3d6 | 3772 | || TREE_CODE (x) == NAMESPACE_DECL) |
8d08fdba | 3773 | name = DECL_NAME (x); |
8d08fdba MS |
3774 | |
3775 | if (name) | |
3776 | { | |
5566b478 MS |
3777 | #if 0 |
3778 | /* Not needed...see below. */ | |
8d08fdba MS |
3779 | char *file; |
3780 | int line; | |
5566b478 | 3781 | #endif |
386b8a85 JM |
3782 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) |
3783 | name = TREE_OPERAND (name, 0); | |
68642fb6 | 3784 | |
2c73f9f5 | 3785 | /* Namespace-scoped variables are not found in the current level. */ |
6eb3bb27 | 3786 | if (TREE_CODE (x) == VAR_DECL && DECL_NAMESPACE_SCOPE_P (x)) |
2c73f9f5 ML |
3787 | t = namespace_binding (name, DECL_CONTEXT (x)); |
3788 | else | |
3789 | t = lookup_name_current_level (name); | |
8d08fdba MS |
3790 | if (t == error_mark_node) |
3791 | { | |
3792 | /* error_mark_node is 0 for a while during initialization! */ | |
3793 | t = NULL_TREE; | |
8251199e | 3794 | cp_error_at ("`%#D' used prior to declaration", x); |
8d08fdba MS |
3795 | } |
3796 | ||
51c184be | 3797 | else if (t != NULL_TREE) |
8d08fdba | 3798 | { |
5566b478 MS |
3799 | #if 0 |
3800 | /* This is turned off until I have time to do it right (bpk). */ | |
e92cc029 | 3801 | /* With the code below that uses it... */ |
8d6e462b PB |
3802 | file = DECL_SOURCE_FILE (t); |
3803 | line = DECL_SOURCE_LINE (t); | |
5566b478 | 3804 | #endif |
2ee887f2 | 3805 | if (TREE_CODE (t) == PARM_DECL) |
8d08fdba MS |
3806 | { |
3807 | if (DECL_CONTEXT (t) == NULL_TREE) | |
3808 | fatal ("parse errors have confused me too much"); | |
be99da77 | 3809 | |
e92cc029 | 3810 | /* Check for duplicate params. */ |
be99da77 MS |
3811 | if (duplicate_decls (x, t)) |
3812 | return t; | |
8d08fdba | 3813 | } |
8d6e462b | 3814 | else if (((TREE_CODE (x) == FUNCTION_DECL && DECL_LANGUAGE (x) == lang_c) |
5566b478 MS |
3815 | || DECL_FUNCTION_TEMPLATE_P (x)) |
3816 | && is_overloaded_fn (t)) | |
2c73f9f5 | 3817 | /* Don't do anything just yet. */; |
e1cd6e56 MS |
3818 | else if (t == wchar_decl_node) |
3819 | { | |
3820 | if (pedantic && ! DECL_IN_SYSTEM_HEADER (x)) | |
8251199e | 3821 | cp_pedwarn ("redeclaration of wchar_t as `%T'", TREE_TYPE (x)); |
e1cd6e56 MS |
3822 | |
3823 | /* Throw away the redeclaration. */ | |
3824 | return t; | |
3825 | } | |
8926095f | 3826 | else if (TREE_CODE (t) != TREE_CODE (x)) |
8d08fdba | 3827 | { |
9ed182dc | 3828 | if (duplicate_decls (x, t)) |
51c184be | 3829 | return t; |
8d08fdba MS |
3830 | } |
3831 | else if (duplicate_decls (x, t)) | |
51c184be | 3832 | { |
8d08fdba | 3833 | #if 0 |
8926095f | 3834 | /* This is turned off until I have time to do it right (bpk). */ |
8d08fdba | 3835 | |
8926095f MS |
3836 | /* Also warn if they did a prototype with `static' on it, but |
3837 | then later left the `static' off. */ | |
3838 | if (! TREE_PUBLIC (name) && TREE_PUBLIC (x)) | |
8d08fdba | 3839 | { |
8926095f MS |
3840 | if (DECL_LANG_SPECIFIC (t) && DECL_FRIEND_P (t)) |
3841 | return t; | |
8d08fdba | 3842 | |
8926095f MS |
3843 | if (extra_warnings) |
3844 | { | |
8251199e | 3845 | cp_warning ("`static' missing from declaration of `%D'", |
a28e3c7f | 3846 | t); |
8926095f MS |
3847 | warning_with_file_and_line (file, line, |
3848 | "previous declaration of `%s'", | |
3849 | decl_as_string (t, 0)); | |
3850 | } | |
8d08fdba | 3851 | |
8926095f MS |
3852 | /* Now fix things so it'll do what they expect. */ |
3853 | if (current_function_decl) | |
3854 | TREE_PUBLIC (current_function_decl) = 0; | |
3855 | } | |
51c184be MS |
3856 | /* Due to interference in memory reclamation (X may be |
3857 | obstack-deallocated at this point), we must guard against | |
8926095f MS |
3858 | one really special case. [jason: This should be handled |
3859 | by start_function] */ | |
51c184be MS |
3860 | if (current_function_decl == x) |
3861 | current_function_decl = t; | |
8926095f | 3862 | #endif |
7177d104 MS |
3863 | if (TREE_CODE (t) == TYPE_DECL) |
3864 | SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t)); | |
f30432d7 MS |
3865 | else if (TREE_CODE (t) == FUNCTION_DECL) |
3866 | check_default_args (t); | |
7177d104 | 3867 | |
51c184be MS |
3868 | return t; |
3869 | } | |
35680744 MM |
3870 | else if (DECL_MAIN_P (x)) |
3871 | { | |
3872 | /* A redeclaration of main, but not a duplicate of the | |
68642fb6 | 3873 | previous one. |
35680744 MM |
3874 | |
3875 | [basic.start.main] | |
3876 | ||
3877 | This function shall not be overloaded. */ | |
8251199e JM |
3878 | cp_error_at ("invalid redeclaration of `%D'", t); |
3879 | cp_error ("as `%D'", x); | |
35680744 MM |
3880 | /* We don't try to push this declaration since that |
3881 | causes a crash. */ | |
3882 | return x; | |
3883 | } | |
8d08fdba | 3884 | } |
8926095f | 3885 | |
f3400fe2 JM |
3886 | check_template_shadow (x); |
3887 | ||
fcfcdfc8 JM |
3888 | /* If this is a function conjured up by the backend, massage it |
3889 | so it looks friendly. */ | |
3890 | if (TREE_CODE (x) == FUNCTION_DECL | |
3891 | && ! DECL_LANG_SPECIFIC (x)) | |
3892 | { | |
3893 | retrofit_lang_decl (x); | |
3894 | DECL_LANGUAGE (x) = lang_c; | |
3895 | } | |
3896 | ||
8926095f MS |
3897 | if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_FUNCTION_MEMBER_P (x)) |
3898 | { | |
7bdbfa05 | 3899 | t = push_overloaded_decl (x, PUSH_LOCAL); |
5fdaba89 | 3900 | if (t != x) |
8926095f | 3901 | return t; |
f181d4ae MM |
3902 | if (!namespace_bindings_p ()) |
3903 | /* We do not need to create a binding for this name; | |
3904 | push_overloaded_decl will have already done so if | |
3905 | necessary. */ | |
3906 | need_new_binding = 0; | |
8926095f | 3907 | } |
6eb3bb27 | 3908 | else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x)) |
cfe507be MM |
3909 | { |
3910 | t = push_overloaded_decl (x, PUSH_GLOBAL); | |
3911 | if (t == x) | |
3912 | add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t))); | |
3913 | return t; | |
3914 | } | |
8d08fdba | 3915 | |
a1774733 BK |
3916 | /* If declaring a type as a typedef, copy the type (unless we're |
3917 | at line 0), and install this TYPE_DECL as the new type's typedef | |
3918 | name. See the extensive comment in ../c-decl.c (pushdecl). */ | |
8d08fdba MS |
3919 | if (TREE_CODE (x) == TYPE_DECL) |
3920 | { | |
3921 | tree type = TREE_TYPE (x); | |
a1774733 BK |
3922 | if (DECL_SOURCE_LINE (x) == 0) |
3923 | { | |
3924 | if (TYPE_NAME (type) == 0) | |
3925 | TYPE_NAME (type) = x; | |
3926 | } | |
1c80fb65 MM |
3927 | else if (type != error_mark_node && TYPE_NAME (type) != x |
3928 | /* We don't want to copy the type when all we're | |
3929 | doing is making a TYPE_DECL for the purposes of | |
3930 | inlining. */ | |
68642fb6 | 3931 | && (!TYPE_NAME (type) |
1c80fb65 | 3932 | || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x))) |
a1774733 BK |
3933 | { |
3934 | DECL_ORIGINAL_TYPE (x) = type; | |
3935 | type = build_type_copy (type); | |
3936 | TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x)); | |
3937 | TYPE_NAME (type) = x; | |
3938 | TREE_TYPE (x) = type; | |
3939 | } | |
8d08fdba | 3940 | |
8d08fdba MS |
3941 | if (type != error_mark_node |
3942 | && TYPE_NAME (type) | |
3943 | && TYPE_IDENTIFIER (type)) | |
68642fb6 | 3944 | set_identifier_type_value_with_scope (DECL_NAME (x), type, |
f181d4ae MM |
3945 | current_binding_level); |
3946 | ||
8d08fdba MS |
3947 | } |
3948 | ||
3949 | /* Multiple external decls of the same identifier ought to match. | |
3950 | ||
3951 | We get warnings about inline functions where they are defined. | |
39211cd5 | 3952 | We get warnings about other functions from push_overloaded_decl. |
68642fb6 | 3953 | |
8d08fdba | 3954 | Avoid duplicate warnings where they are used. */ |
39211cd5 | 3955 | if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL) |
8d08fdba MS |
3956 | { |
3957 | tree decl; | |
3958 | ||
31928556 JM |
3959 | if (IDENTIFIER_NAMESPACE_VALUE (name) != NULL_TREE |
3960 | && (DECL_EXTERNAL (IDENTIFIER_NAMESPACE_VALUE (name)) | |
3961 | || TREE_PUBLIC (IDENTIFIER_NAMESPACE_VALUE (name)))) | |
3962 | decl = IDENTIFIER_NAMESPACE_VALUE (name); | |
8d08fdba MS |
3963 | else |
3964 | decl = NULL_TREE; | |
3965 | ||
39211cd5 | 3966 | if (decl |
8d08fdba MS |
3967 | /* If different sort of thing, we already gave an error. */ |
3968 | && TREE_CODE (decl) == TREE_CODE (x) | |
3bfdc719 | 3969 | && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl))) |
8d08fdba | 3970 | { |
8251199e JM |
3971 | cp_pedwarn ("type mismatch with previous external decl", x); |
3972 | cp_pedwarn_at ("previous external decl of `%#D'", decl); | |
8d08fdba MS |
3973 | } |
3974 | } | |
3975 | ||
8d08fdba MS |
3976 | /* This name is new in its binding level. |
3977 | Install the new declaration and return it. */ | |
2c73f9f5 | 3978 | if (namespace_bindings_p ()) |
8d08fdba MS |
3979 | { |
3980 | /* Install a global value. */ | |
3981 | ||
8d08fdba MS |
3982 | /* If the first global decl has external linkage, |
3983 | warn if we later see static one. */ | |
31928556 | 3984 | if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x)) |
8d08fdba MS |
3985 | TREE_PUBLIC (name) = 1; |
3986 | ||
d8f8dca1 | 3987 | if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x) |
5fdaba89 MM |
3988 | && t != NULL_TREE) |
3989 | /* For an ordinary function, we create a binding from | |
3990 | the mangled name (i.e., NAME) to the DECL. But, for | |
3991 | an `extern "C"' function, the mangled name and the | |
3992 | ordinary name are the same so we need not do this. */ | |
68642fb6 | 3993 | && !(TREE_CODE (x) == FUNCTION_DECL && |
5fdaba89 | 3994 | DECL_LANGUAGE (x) == lang_c)) |
30394414 JM |
3995 | { |
3996 | if (TREE_CODE (x) == FUNCTION_DECL) | |
68642fb6 | 3997 | my_friendly_assert |
31928556 JM |
3998 | ((IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE) |
3999 | || (IDENTIFIER_GLOBAL_VALUE (name) == x), 378); | |
4000 | SET_IDENTIFIER_NAMESPACE_VALUE (name, x); | |
30394414 | 4001 | } |
8d08fdba MS |
4002 | |
4003 | /* Don't forget if the function was used via an implicit decl. */ | |
4004 | if (IDENTIFIER_IMPLICIT_DECL (name) | |
4005 | && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name))) | |
4006 | TREE_USED (x) = 1; | |
4007 | ||
4008 | /* Don't forget if its address was taken in that way. */ | |
4009 | if (IDENTIFIER_IMPLICIT_DECL (name) | |
4010 | && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name))) | |
4011 | TREE_ADDRESSABLE (x) = 1; | |
4012 | ||
4013 | /* Warn about mismatches against previous implicit decl. */ | |
4014 | if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE | |
4015 | /* If this real decl matches the implicit, don't complain. */ | |
4016 | && ! (TREE_CODE (x) == FUNCTION_DECL | |
4017 | && TREE_TYPE (TREE_TYPE (x)) == integer_type_node)) | |
8251199e JM |
4018 | cp_warning |
4019 | ("`%D' was previously implicitly declared to return `int'", x); | |
8d08fdba MS |
4020 | |
4021 | /* If new decl is `static' and an `extern' was seen previously, | |
4022 | warn about it. */ | |
a0a33927 MS |
4023 | if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t)) |
4024 | warn_extern_redeclared_static (x, t); | |
8d08fdba MS |
4025 | } |
4026 | else | |
4027 | { | |
4028 | /* Here to install a non-global value. */ | |
f181d4ae | 4029 | tree oldlocal = IDENTIFIER_VALUE (name); |
31928556 | 4030 | tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name); |
8d08fdba | 4031 | |
f181d4ae MM |
4032 | if (need_new_binding) |
4033 | { | |
0034cf72 | 4034 | push_local_binding (name, x, 0); |
f181d4ae MM |
4035 | /* Because push_local_binding will hook X on to the |
4036 | current_binding_level's name list, we don't want to | |
4037 | do that again below. */ | |
4038 | need_new_binding = 0; | |
4039 | } | |
8d08fdba MS |
4040 | |
4041 | /* If this is a TYPE_DECL, push it into the type value slot. */ | |
4042 | if (TREE_CODE (x) == TYPE_DECL) | |
68642fb6 | 4043 | set_identifier_type_value_with_scope (name, TREE_TYPE (x), |
f181d4ae | 4044 | current_binding_level); |
8d08fdba | 4045 | |
a9aedbc2 MS |
4046 | /* Clear out any TYPE_DECL shadowed by a namespace so that |
4047 | we won't think this is a type. The C struct hack doesn't | |
4048 | go through namespaces. */ | |
4049 | if (TREE_CODE (x) == NAMESPACE_DECL) | |
68642fb6 | 4050 | set_identifier_type_value_with_scope (name, NULL_TREE, |
f181d4ae | 4051 | current_binding_level); |
a9aedbc2 | 4052 | |
e905ac8a MS |
4053 | if (oldlocal) |
4054 | { | |
4055 | tree d = oldlocal; | |
74dc0d8c MS |
4056 | while (oldlocal |
4057 | && TREE_CODE (oldlocal) == VAR_DECL | |
4058 | && DECL_DEAD_FOR_LOCAL (oldlocal)) | |
e905ac8a MS |
4059 | { |
4060 | oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal); | |
4061 | } | |
4062 | if (oldlocal == NULL_TREE) | |
4063 | oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d)); | |
4064 | } | |
4065 | ||
8d08fdba MS |
4066 | /* If this is an extern function declaration, see if we |
4067 | have a global definition or declaration for the function. */ | |
4068 | if (oldlocal == NULL_TREE | |
faae18ab | 4069 | && DECL_EXTERNAL (x) |
31928556 | 4070 | && oldglobal != NULL_TREE |
8d08fdba | 4071 | && TREE_CODE (x) == FUNCTION_DECL |
31928556 | 4072 | && TREE_CODE (oldglobal) == FUNCTION_DECL) |
8d08fdba MS |
4073 | { |
4074 | /* We have one. Their types must agree. */ | |
31928556 | 4075 | if (decls_match (x, oldglobal)) |
6060a796 MS |
4076 | /* OK */; |
4077 | else | |
8d08fdba | 4078 | { |
8251199e JM |
4079 | cp_warning ("extern declaration of `%#D' doesn't match", x); |
4080 | cp_warning_at ("global declaration `%#D'", oldglobal); | |
8d08fdba | 4081 | } |
8d08fdba MS |
4082 | } |
4083 | /* If we have a local external declaration, | |
4084 | and no file-scope declaration has yet been seen, | |
4085 | then if we later have a file-scope decl it must not be static. */ | |
4086 | if (oldlocal == NULL_TREE | |
31928556 | 4087 | && oldglobal == NULL_TREE |
8d08fdba MS |
4088 | && DECL_EXTERNAL (x) |
4089 | && TREE_PUBLIC (x)) | |
f181d4ae | 4090 | TREE_PUBLIC (name) = 1; |
8d08fdba | 4091 | |
8d08fdba | 4092 | /* Warn if shadowing an argument at the top level of the body. */ |
e905ac8a MS |
4093 | if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x) |
4094 | /* Inline decls shadow nothing. */ | |
4095 | && !DECL_FROM_INLINE (x) | |
4096 | && TREE_CODE (oldlocal) == PARM_DECL | |
4097 | /* Don't complain if it's from an enclosing function. */ | |
4098 | && DECL_CONTEXT (oldlocal) == current_function_decl | |
4099 | && TREE_CODE (x) != PARM_DECL) | |
8d08fdba MS |
4100 | { |
4101 | /* Go to where the parms should be and see if we | |
4102 | find them there. */ | |
4103 | struct binding_level *b = current_binding_level->level_chain; | |
4104 | ||
4105 | if (cleanup_label) | |
4106 | b = b->level_chain; | |
4107 | ||
4108 | /* ARM $8.3 */ | |
4109 | if (b->parm_flag == 1) | |
8251199e | 4110 | cp_error ("declaration of `%#D' shadows a parameter", name); |
8d08fdba | 4111 | } |
e905ac8a | 4112 | |
8d08fdba | 4113 | /* Maybe warn if shadowing something else. */ |
e905ac8a MS |
4114 | if (warn_shadow && !DECL_EXTERNAL (x) |
4115 | /* Inline decls shadow nothing. */ | |
4116 | && !DECL_FROM_INLINE (x) | |
4117 | /* No shadow warnings for internally generated vars. */ | |
4118 | && ! DECL_ARTIFICIAL (x) | |
4119 | /* No shadow warnings for vars made for inlining. */ | |
4120 | && ! DECL_FROM_INLINE (x)) | |
8d08fdba | 4121 | { |
8d08fdba | 4122 | if (oldlocal != NULL_TREE && TREE_CODE (oldlocal) == PARM_DECL) |
b9d12519 KG |
4123 | warning ("declaration of `%s' shadows a parameter", |
4124 | IDENTIFIER_POINTER (name)); | |
8d08fdba | 4125 | else if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE |
4ac14744 | 4126 | && current_class_ptr |
8d08fdba | 4127 | && !TREE_STATIC (name)) |
b9d12519 KG |
4128 | warning ("declaration of `%s' shadows a member of `this'", |
4129 | IDENTIFIER_POINTER (name)); | |
8d08fdba | 4130 | else if (oldlocal != NULL_TREE) |
b9d12519 KG |
4131 | warning ("declaration of `%s' shadows previous local", |
4132 | IDENTIFIER_POINTER (name)); | |
31928556 | 4133 | else if (oldglobal != NULL_TREE) |
30394414 | 4134 | /* XXX shadow warnings in outer-more namespaces */ |
b9d12519 KG |
4135 | warning ("declaration of `%s' shadows global declaration", |
4136 | IDENTIFIER_POINTER (name)); | |
8d08fdba | 4137 | } |
e1cd6e56 | 4138 | } |
8d08fdba | 4139 | |
e1cd6e56 | 4140 | if (TREE_CODE (x) == FUNCTION_DECL) |
f30432d7 | 4141 | check_default_args (x); |
8145f082 MS |
4142 | |
4143 | /* Keep count of variables in this level with incomplete type. */ | |
8145f082 | 4144 | if (TREE_CODE (x) == VAR_DECL |
28cbf42c | 4145 | && TREE_TYPE (x) != error_mark_node |
f30432d7 MS |
4146 | && ((TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE |
4147 | && PROMOTES_TO_AGGR_TYPE (TREE_TYPE (x), ARRAY_TYPE)) | |
4148 | /* RTTI TD entries are created while defining the type_info. */ | |
4149 | || (TYPE_LANG_SPECIFIC (TREE_TYPE (x)) | |
4150 | && TYPE_BEING_DEFINED (TREE_TYPE (x))))) | |
70adf8a9 JM |
4151 | { |
4152 | if (namespace_bindings_p ()) | |
4153 | namespace_scope_incomplete | |
4154 | = tree_cons (NULL_TREE, x, namespace_scope_incomplete); | |
4155 | else | |
4156 | current_binding_level->incomplete | |
4157 | = tree_cons (NULL_TREE, x, current_binding_level->incomplete); | |
4158 | } | |
8d08fdba MS |
4159 | } |
4160 | ||
f181d4ae | 4161 | if (need_new_binding) |
68642fb6 | 4162 | add_decl_to_level (x, |
efee38a9 MM |
4163 | DECL_NAMESPACE_SCOPE_P (x) |
4164 | ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x)) | |
4165 | : current_binding_level); | |
8d08fdba MS |
4166 | |
4167 | return x; | |
4168 | } | |
4169 | ||
5566b478 MS |
4170 | /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the |
4171 | caller to set DECL_CONTEXT properly. */ | |
8d08fdba MS |
4172 | |
4173 | static tree | |
4174 | pushdecl_with_scope (x, level) | |
4175 | tree x; | |
4176 | struct binding_level *level; | |
4177 | { | |
8d019cef | 4178 | register struct binding_level *b; |
5566b478 | 4179 | tree function_decl = current_function_decl; |
8d08fdba | 4180 | |
5566b478 | 4181 | current_function_decl = NULL_TREE; |
8d019cef JM |
4182 | if (level->parm_flag == 2) |
4183 | { | |
4184 | b = class_binding_level; | |
4185 | class_binding_level = level; | |
4186 | pushdecl_class_level (x); | |
4187 | class_binding_level = b; | |
4188 | } | |
4189 | else | |
4190 | { | |
4191 | b = current_binding_level; | |
4192 | current_binding_level = level; | |
4193 | x = pushdecl (x); | |
4194 | current_binding_level = b; | |
4195 | } | |
5566b478 | 4196 | current_function_decl = function_decl; |
8d08fdba MS |
4197 | return x; |
4198 | } | |
4199 | ||
2c73f9f5 | 4200 | /* Like pushdecl, only it places X in the current namespace, |
8d08fdba | 4201 | if appropriate. */ |
e92cc029 | 4202 | |
8d08fdba | 4203 | tree |
2c73f9f5 | 4204 | pushdecl_namespace_level (x) |
8d08fdba MS |
4205 | tree x; |
4206 | { | |
8f032717 | 4207 | register struct binding_level *b = current_binding_level; |
2c73f9f5 ML |
4208 | register tree t; |
4209 | ||
4210 | t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace)); | |
8d08fdba MS |
4211 | |
4212 | /* Now, the type_shadowed stack may screw us. Munge it so it does | |
4213 | what we want. */ | |
4214 | if (TREE_CODE (x) == TYPE_DECL) | |
4215 | { | |
4216 | tree name = DECL_NAME (x); | |
4217 | tree newval; | |
4218 | tree *ptr = (tree *)0; | |
4219 | for (; b != global_binding_level; b = b->level_chain) | |
4220 | { | |
4221 | tree shadowed = b->type_shadowed; | |
4222 | for (; shadowed; shadowed = TREE_CHAIN (shadowed)) | |
4223 | if (TREE_PURPOSE (shadowed) == name) | |
4224 | { | |
4225 | ptr = &TREE_VALUE (shadowed); | |
4226 | /* Can't break out of the loop here because sometimes | |
4227 | a binding level will have duplicate bindings for | |
4228 | PT names. It's gross, but I haven't time to fix it. */ | |
4229 | } | |
4230 | } | |
4231 | newval = TREE_TYPE (x); | |
4232 | if (ptr == (tree *)0) | |
4233 | { | |
4234 | /* @@ This shouldn't be needed. My test case "zstring.cc" trips | |
4235 | up here if this is changed to an assertion. --KR */ | |
4236 | SET_IDENTIFIER_TYPE_VALUE (name, newval); | |
4237 | } | |
4238 | else | |
4239 | { | |
8d08fdba MS |
4240 | *ptr = newval; |
4241 | } | |
4242 | } | |
4243 | return t; | |
4244 | } | |
4245 | ||
2c73f9f5 ML |
4246 | /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL, |
4247 | if appropriate. */ | |
4248 | ||
4249 | tree | |
4250 | pushdecl_top_level (x) | |
4251 | tree x; | |
4252 | { | |
b35d4555 | 4253 | push_to_top_level (); |
2c73f9f5 | 4254 | x = pushdecl_namespace_level (x); |
b35d4555 | 4255 | pop_from_top_level (); |
2c73f9f5 ML |
4256 | return x; |
4257 | } | |
4258 | ||
8d08fdba | 4259 | /* Make the declaration of X appear in CLASS scope. */ |
e92cc029 | 4260 | |
61a127b3 | 4261 | void |
8d08fdba MS |
4262 | pushdecl_class_level (x) |
4263 | tree x; | |
4264 | { | |
4265 | /* Don't use DECL_ASSEMBLER_NAME here! Everything that looks in class | |
4266 | scope looks for the pre-mangled name. */ | |
8f032717 MM |
4267 | register tree name; |
4268 | ||
4269 | if (TREE_CODE (x) == OVERLOAD) | |
4270 | x = OVL_CURRENT (x); | |
4271 | name = DECL_NAME (x); | |
8d08fdba MS |
4272 | |
4273 | if (name) | |
4274 | { | |
4275 | push_class_level_binding (name, x); | |
4276 | if (TREE_CODE (x) == TYPE_DECL) | |
f181d4ae | 4277 | set_identifier_type_value (name, TREE_TYPE (x)); |
8d08fdba | 4278 | } |
6bdb8141 | 4279 | else if (ANON_AGGR_TYPE_P (TREE_TYPE (x))) |
8f032717 MM |
4280 | { |
4281 | tree f; | |
4282 | ||
4283 | for (f = TYPE_FIELDS (TREE_TYPE (x)); | |
4284 | f; | |
4285 | f = TREE_CHAIN (f)) | |
4286 | pushdecl_class_level (f); | |
4287 | } | |
8d08fdba MS |
4288 | } |
4289 | ||
9188c363 MM |
4290 | /* Enter DECL into the symbol table, if that's appropriate. Returns |
4291 | DECL, or a modified version thereof. */ | |
4292 | ||
4293 | tree | |
4294 | maybe_push_decl (decl) | |
4295 | tree decl; | |
4296 | { | |
4297 | tree type = TREE_TYPE (decl); | |
4298 | ||
4299 | /* Add this decl to the current binding level, but not if it comes | |
4300 | from another scope, e.g. a static member variable. TEM may equal | |
4301 | DECL or it may be a previous decl of the same name. */ | |
07c88314 MM |
4302 | if (decl == error_mark_node |
4303 | || (TREE_CODE (decl) != PARM_DECL | |
4304 | && DECL_CONTEXT (decl) != NULL_TREE | |
4305 | /* Definitions of namespace members outside their namespace are | |
4306 | possible. */ | |
4307 | && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL) | |
9188c363 MM |
4308 | || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ()) |
4309 | || TREE_CODE (type) == UNKNOWN_TYPE | |
558475f0 | 4310 | /* The declaration of a template specialization does not affect |
9188c363 MM |
4311 | the functions available for overload resolution, so we do not |
4312 | call pushdecl. */ | |
4313 | || (TREE_CODE (decl) == FUNCTION_DECL | |
4314 | && DECL_TEMPLATE_SPECIALIZATION (decl))) | |
4315 | return decl; | |
4316 | else | |
4317 | return pushdecl (decl); | |
4318 | } | |
4319 | ||
8d08fdba MS |
4320 | /* Make the declaration(s) of X appear in CLASS scope |
4321 | under the name NAME. */ | |
e92cc029 | 4322 | |
8d08fdba MS |
4323 | void |
4324 | push_class_level_binding (name, x) | |
4325 | tree name; | |
4326 | tree x; | |
4327 | { | |
8f032717 | 4328 | tree binding; |
68642fb6 | 4329 | /* The class_binding_level will be NULL if x is a template |
98c1c668 JM |
4330 | parameter name in a member template. */ |
4331 | if (!class_binding_level) | |
4332 | return; | |
4333 | ||
908c4e83 MM |
4334 | /* Make sure that this new member does not have the same name |
4335 | as a template parameter. */ | |
4336 | if (TYPE_BEING_DEFINED (current_class_type)) | |
4337 | check_template_shadow (x); | |
4338 | ||
f181d4ae MM |
4339 | /* If this declaration shadows a declaration from an enclosing |
4340 | class, then we will need to restore IDENTIFIER_CLASS_VALUE when | |
4341 | we leave this class. Record the shadowed declaration here. */ | |
8f032717 | 4342 | binding = IDENTIFIER_BINDING (name); |
68642fb6 | 4343 | if (binding |
8f032717 MM |
4344 | && ((TREE_CODE (x) == OVERLOAD |
4345 | && BINDING_VALUE (binding) | |
4346 | && is_overloaded_fn (BINDING_VALUE (binding))) | |
4347 | || INHERITED_VALUE_BINDING_P (binding))) | |
4348 | { | |
4349 | tree shadow; | |
4350 | tree old_decl; | |
4351 | ||
4352 | /* If the old binding was from a base class, and was for a tag | |
4353 | name, slide it over to make room for the new binding. The | |
4354 | old binding is still visible if explicitly qualified with a | |
4355 | class-key. */ | |
4356 | if (INHERITED_VALUE_BINDING_P (binding) | |
4357 | && BINDING_VALUE (binding) | |
4358 | && TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL | |
4359 | && DECL_ARTIFICIAL (BINDING_VALUE (binding)) | |
4360 | && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x))) | |
4361 | { | |
4362 | old_decl = BINDING_TYPE (binding); | |
4363 | BINDING_TYPE (binding) = BINDING_VALUE (binding); | |
4364 | BINDING_VALUE (binding) = NULL_TREE; | |
4365 | INHERITED_VALUE_BINDING_P (binding) = 0; | |
4366 | } | |
4367 | else | |
4368 | old_decl = BINDING_VALUE (binding); | |
4369 | ||
4370 | /* There was already a binding for X containing fewer | |
4371 | functions than are named in X. Find the previous | |
4372 | declaration of X on the class-shadowed list, and update it. */ | |
4373 | for (shadow = class_binding_level->class_shadowed; | |
4374 | shadow; | |
4375 | shadow = TREE_CHAIN (shadow)) | |
4376 | if (TREE_PURPOSE (shadow) == name | |
4377 | && TREE_TYPE (shadow) == old_decl) | |
4378 | { | |
4379 | BINDING_VALUE (binding) = x; | |
4380 | INHERITED_VALUE_BINDING_P (binding) = 0; | |
4381 | TREE_TYPE (shadow) = x; | |
4382 | return; | |
4383 | } | |
4384 | } | |
f181d4ae | 4385 | |
8f032717 MM |
4386 | /* If we didn't replace an existing binding, put the binding on the |
4387 | stack of bindings for the identifier, and update | |
4388 | IDENTIFIER_CLASS_VALUE. */ | |
4389 | if (push_class_binding (name, x)) | |
4390 | { | |
8f032717 MM |
4391 | class_binding_level->class_shadowed |
4392 | = tree_cons (name, IDENTIFIER_CLASS_VALUE (name), | |
4393 | class_binding_level->class_shadowed); | |
8f032717 MM |
4394 | /* Record the value we are binding NAME to so that we can know |
4395 | what to pop later. */ | |
4396 | TREE_TYPE (class_binding_level->class_shadowed) = x; | |
4397 | } | |
8d08fdba MS |
4398 | } |
4399 | ||
2c73f9f5 ML |
4400 | /* Insert another USING_DECL into the current binding level, |
4401 | returning this declaration. If this is a redeclaration, | |
4402 | do nothing and return NULL_TREE. */ | |
e92cc029 | 4403 | |
2c73f9f5 ML |
4404 | tree |
4405 | push_using_decl (scope, name) | |
4406 | tree scope; | |
4407 | tree name; | |
8d08fdba | 4408 | { |
2c73f9f5 | 4409 | tree decl; |
68642fb6 | 4410 | |
2c73f9f5 ML |
4411 | my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383); |
4412 | my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384); | |
4413 | for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl)) | |
4414 | if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name) | |
4415 | break; | |
4416 | if (decl) | |
4417 | return NULL_TREE; | |
4ce3d537 | 4418 | decl = build_lang_decl (USING_DECL, name, void_type_node); |
2c73f9f5 ML |
4419 | DECL_INITIAL (decl) = scope; |
4420 | TREE_CHAIN (decl) = current_binding_level->usings; | |
4421 | current_binding_level->usings = decl; | |
4422 | return decl; | |
8d08fdba MS |
4423 | } |
4424 | ||
ea9635c7 ML |
4425 | /* Add namespace to using_directives. Return NULL_TREE if nothing was |
4426 | changed (i.e. there was already a directive), or the fresh | |
4427 | TREE_LIST otherwise. */ | |
4428 | ||
4429 | tree | |
9ed182dc | 4430 | push_using_directive (used) |
ea9635c7 | 4431 | tree used; |
ea9635c7 ML |
4432 | { |
4433 | tree ud = current_binding_level->using_directives; | |
9ed182dc | 4434 | tree iter, ancestor; |
68642fb6 | 4435 | |
ea9635c7 ML |
4436 | /* Check if we already have this. */ |
4437 | if (purpose_member (used, ud) != NULL_TREE) | |
4438 | return NULL_TREE; | |
00dc6358 JM |
4439 | |
4440 | /* Recursively add all namespaces used. */ | |
4441 | for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter)) | |
9ed182dc | 4442 | push_using_directive (TREE_PURPOSE (iter)); |
00dc6358 | 4443 | |
9ed182dc | 4444 | ancestor = namespace_ancestor (current_decl_namespace (), used); |
00dc6358 | 4445 | ud = current_binding_level->using_directives; |
e1b3e07d | 4446 | ud = tree_cons (used, ancestor, ud); |
ea9635c7 ML |
4447 | current_binding_level->using_directives = ud; |
4448 | return ud; | |
4449 | } | |
4450 | ||
f181d4ae MM |
4451 | /* DECL is a FUNCTION_DECL for a non-member function, which may have |
4452 | other definitions already in place. We get around this by making | |
4453 | the value of the identifier point to a list of all the things that | |
4454 | want to be referenced by that name. It is then up to the users of | |
4455 | that name to decide what to do with that list. | |
8d08fdba MS |
4456 | |
4457 | DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its DECL_RESULT | |
4458 | slot. It is dealt with the same way. | |
4459 | ||
7bdbfa05 MM |
4460 | FLAGS is a bitwise-or of the following values: |
4461 | PUSH_LOCAL: Bind DECL in the current scope, rather than at | |
4462 | namespace scope. | |
4463 | PUSH_USING: DECL is being pushed as the result of a using | |
68642fb6 | 4464 | declaration. |
7bdbfa05 | 4465 | |
8d08fdba MS |
4466 | The value returned may be a previous declaration if we guessed wrong |
4467 | about what language DECL should belong to (C or C++). Otherwise, | |
4468 | it's always DECL (and never something that's not a _DECL). */ | |
e92cc029 | 4469 | |
7bdbfa05 MM |
4470 | tree |
4471 | push_overloaded_decl (decl, flags) | |
8d08fdba | 4472 | tree decl; |
7bdbfa05 | 4473 | int flags; |
8d08fdba | 4474 | { |
f181d4ae | 4475 | tree name = DECL_NAME (decl); |
700f8a87 | 4476 | tree old; |
f181d4ae | 4477 | tree new_binding; |
7bdbfa05 | 4478 | int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL)); |
700f8a87 MS |
4479 | |
4480 | if (doing_global) | |
9f33663b | 4481 | old = namespace_binding (name, DECL_CONTEXT (decl)); |
700f8a87 | 4482 | else |
f181d4ae | 4483 | old = lookup_name_current_level (name); |
8d08fdba | 4484 | |
700f8a87 | 4485 | if (old) |
8d08fdba | 4486 | { |
e1cd6e56 | 4487 | if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old)) |
8926095f | 4488 | { |
700f8a87 | 4489 | tree t = TREE_TYPE (old); |
cdf5b885 MS |
4490 | if (IS_AGGR_TYPE (t) && warn_shadow |
4491 | && (! DECL_IN_SYSTEM_HEADER (decl) | |
4492 | || ! DECL_IN_SYSTEM_HEADER (old))) | |
8251199e | 4493 | cp_warning ("`%#D' hides constructor for `%#T'", decl, t); |
5b605f68 | 4494 | old = NULL_TREE; |
8926095f | 4495 | } |
700f8a87 | 4496 | else if (is_overloaded_fn (old)) |
8d08fdba | 4497 | { |
8d08fdba | 4498 | tree tmp; |
68642fb6 | 4499 | |
2c73f9f5 | 4500 | for (tmp = old; tmp; tmp = OVL_NEXT (tmp)) |
7bdbfa05 MM |
4501 | { |
4502 | tree fn = OVL_CURRENT (tmp); | |
4503 | ||
4504 | if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp) | |
4505 | && !(flags & PUSH_USING) | |
4506 | && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)), | |
4507 | TYPE_ARG_TYPES (TREE_TYPE (decl)))) | |
4508 | cp_error ("`%#D' conflicts with previous using declaration `%#D'", | |
4509 | decl, fn); | |
68642fb6 | 4510 | |
7bdbfa05 MM |
4511 | if (duplicate_decls (decl, fn)) |
4512 | return fn; | |
4513 | } | |
8d08fdba | 4514 | } |
e1cd6e56 MS |
4515 | else |
4516 | { | |
8251199e JM |
4517 | cp_error_at ("previous non-function declaration `%#D'", old); |
4518 | cp_error ("conflicts with function declaration `%#D'", decl); | |
a9aedbc2 | 4519 | return decl; |
e1cd6e56 | 4520 | } |
8d08fdba | 4521 | } |
7177d104 | 4522 | |
700f8a87 | 4523 | if (old || TREE_CODE (decl) == TEMPLATE_DECL) |
8d08fdba | 4524 | { |
2c73f9f5 | 4525 | if (old && TREE_CODE (old) != OVERLOAD) |
f181d4ae MM |
4526 | new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE)); |
4527 | else | |
4528 | new_binding = ovl_cons (decl, old); | |
347d73d7 ML |
4529 | if (flags & PUSH_USING) |
4530 | OVL_USED (new_binding) = 1; | |
8d08fdba MS |
4531 | } |
4532 | else | |
f181d4ae MM |
4533 | /* NAME is not ambiguous. */ |
4534 | new_binding = decl; | |
700f8a87 MS |
4535 | |
4536 | if (doing_global) | |
f181d4ae | 4537 | set_namespace_binding (name, current_namespace, new_binding); |
700f8a87 | 4538 | else |
f181d4ae MM |
4539 | { |
4540 | /* We only create an OVERLOAD if there was a previous binding at | |
0580c9aa ML |
4541 | this level, or if decl is a template. In the former case, we |
4542 | need to remove the old binding and replace it with the new | |
4543 | binding. We must also run through the NAMES on the binding | |
4544 | level where the name was bound to update the chain. */ | |
4545 | ||
4546 | if (TREE_CODE (new_binding) == OVERLOAD && old) | |
f181d4ae MM |
4547 | { |
4548 | tree *d; | |
68642fb6 | 4549 | |
f181d4ae MM |
4550 | for (d = &BINDING_LEVEL (IDENTIFIER_BINDING (name))->names; |
4551 | *d; | |
4552 | d = &TREE_CHAIN (*d)) | |
4553 | if (*d == old | |
4554 | || (TREE_CODE (*d) == TREE_LIST | |
4555 | && TREE_VALUE (*d) == old)) | |
4556 | { | |
d8f8dca1 MM |
4557 | if (TREE_CODE (*d) == TREE_LIST) |
4558 | /* Just replace the old binding with the new. */ | |
4559 | TREE_VALUE (*d) = new_binding; | |
4560 | else | |
4561 | /* Build a TREE_LIST to wrap the OVERLOAD. */ | |
68642fb6 | 4562 | *d = tree_cons (NULL_TREE, new_binding, |
ed5511d9 | 4563 | TREE_CHAIN (*d)); |
d8f8dca1 MM |
4564 | |
4565 | /* And update the CPLUS_BINDING node. */ | |
4566 | BINDING_VALUE (IDENTIFIER_BINDING (name)) | |
4567 | = new_binding; | |
4568 | return decl; | |
f181d4ae MM |
4569 | } |
4570 | ||
d8f8dca1 MM |
4571 | /* We should always find a previous binding in this case. */ |
4572 | my_friendly_abort (0); | |
f181d4ae MM |
4573 | } |
4574 | ||
4575 | /* Install the new binding. */ | |
0034cf72 | 4576 | push_local_binding (name, new_binding, flags); |
f181d4ae | 4577 | } |
700f8a87 | 4578 | |
8d08fdba MS |
4579 | return decl; |
4580 | } | |
4581 | \f | |
4582 | /* Generate an implicit declaration for identifier FUNCTIONID | |
4583 | as a function of type int (). Print a warning if appropriate. */ | |
4584 | ||
4585 | tree | |
4586 | implicitly_declare (functionid) | |
4587 | tree functionid; | |
4588 | { | |
4589 | register tree decl; | |
8d08fdba MS |
4590 | |
4591 | /* We used to reuse an old implicit decl here, | |
4592 | but this loses with inline functions because it can clobber | |
4593 | the saved decl chains. */ | |
4594 | decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type); | |
4595 | ||
4596 | DECL_EXTERNAL (decl) = 1; | |
4597 | TREE_PUBLIC (decl) = 1; | |
4598 | ||
cab1f180 | 4599 | /* ISO standard says implicit declarations are in the innermost block. |
d22c8596 | 4600 | So we record the decl in the standard fashion. */ |
8d08fdba MS |
4601 | pushdecl (decl); |
4602 | rest_of_decl_compilation (decl, NULL_PTR, 0, 0); | |
4603 | ||
4604 | if (warn_implicit | |
4605 | /* Only one warning per identifier. */ | |
4606 | && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE) | |
4607 | { | |
8251199e | 4608 | cp_pedwarn ("implicit declaration of function `%#D'", decl); |
8d08fdba MS |
4609 | } |
4610 | ||
4611 | SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl); | |
4612 | ||
8d08fdba MS |
4613 | return decl; |
4614 | } | |
4615 | ||
8251199e | 4616 | /* Return zero if the declaration NEWDECL is valid |
8d08fdba MS |
4617 | when the declaration OLDDECL (assumed to be for the same name) |
4618 | has already been seen. | |
4619 | Otherwise return an error message format string with a %s | |
4620 | where the identifier should go. */ | |
4621 | ||
d8e178a0 | 4622 | static const char * |
8d08fdba MS |
4623 | redeclaration_error_message (newdecl, olddecl) |
4624 | tree newdecl, olddecl; | |
4625 | { | |
4626 | if (TREE_CODE (newdecl) == TYPE_DECL) | |
4627 | { | |
4628 | /* Because C++ can put things into name space for free, | |
4629 | constructs like "typedef struct foo { ... } foo" | |
4630 | would look like an erroneous redeclaration. */ | |
3bfdc719 | 4631 | if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl))) |
8251199e | 4632 | return 0; |
8d08fdba | 4633 | else |
8251199e | 4634 | return "redefinition of `%#D'"; |
8d08fdba MS |
4635 | } |
4636 | else if (TREE_CODE (newdecl) == FUNCTION_DECL) | |
4637 | { | |
4638 | /* If this is a pure function, its olddecl will actually be | |
4639 | the original initialization to `0' (which we force to call | |
4640 | abort()). Don't complain about redefinition in this case. */ | |
fee7654e | 4641 | if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)) |
8251199e | 4642 | return 0; |
8d08fdba | 4643 | |
2c73f9f5 ML |
4644 | /* If both functions come from different namespaces, this is not |
4645 | a redeclaration - this is a conflict with a used function. */ | |
6eb3bb27 | 4646 | if (DECL_NAMESPACE_SCOPE_P (olddecl) |
2c73f9f5 | 4647 | && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)) |
8251199e | 4648 | return "`%D' conflicts with used function"; |
2c73f9f5 | 4649 | |
db5ae43f MS |
4650 | /* We'll complain about linkage mismatches in |
4651 | warn_extern_redeclared_static. */ | |
4652 | ||
2c73f9f5 | 4653 | /* Defining the same name twice is no good. */ |
8d08fdba | 4654 | if (DECL_INITIAL (olddecl) != NULL_TREE |
faae18ab | 4655 | && DECL_INITIAL (newdecl) != NULL_TREE) |
8d08fdba MS |
4656 | { |
4657 | if (DECL_NAME (olddecl) == NULL_TREE) | |
8251199e | 4658 | return "`%#D' not declared in class"; |
8d08fdba | 4659 | else |
8251199e | 4660 | return "redefinition of `%#D'"; |
8d08fdba | 4661 | } |
8251199e | 4662 | return 0; |
8d08fdba | 4663 | } |
51c184be MS |
4664 | else if (TREE_CODE (newdecl) == TEMPLATE_DECL) |
4665 | { | |
ec255269 MS |
4666 | if ((TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL |
4667 | && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl)) | |
4668 | && DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl))) | |
4669 | || (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL | |
4670 | && TYPE_SIZE (TREE_TYPE (newdecl)) | |
4671 | && TYPE_SIZE (TREE_TYPE (olddecl)))) | |
8251199e JM |
4672 | return "redefinition of `%#D'"; |
4673 | return 0; | |
51c184be | 4674 | } |
1f51a992 | 4675 | else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl)) |
8d08fdba MS |
4676 | { |
4677 | /* Objects declared at top level: */ | |
4678 | /* If at least one is a reference, it's ok. */ | |
4679 | if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl)) | |
8251199e | 4680 | return 0; |
8926095f | 4681 | /* Reject two definitions. */ |
8251199e | 4682 | return "redefinition of `%#D'"; |
8d08fdba MS |
4683 | } |
4684 | else | |
4685 | { | |
4686 | /* Objects declared with block scope: */ | |
4687 | /* Reject two definitions, and reject a definition | |
4688 | together with an external reference. */ | |
4689 | if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl))) | |
8251199e JM |
4690 | return "redeclaration of `%#D'"; |
4691 | return 0; | |
8d08fdba MS |
4692 | } |
4693 | } | |
4694 | \f | |
acef433b | 4695 | /* Create a new label, named ID. */ |
8d08fdba | 4696 | |
acef433b MM |
4697 | static tree |
4698 | make_label_decl (id, local_p) | |
8d08fdba | 4699 | tree id; |
acef433b | 4700 | int local_p; |
8d08fdba | 4701 | { |
acef433b | 4702 | tree decl; |
8d08fdba | 4703 | |
acef433b | 4704 | decl = build_decl (LABEL_DECL, id, void_type_node); |
b35d4555 | 4705 | if (expanding_p) |
acef433b MM |
4706 | /* Make sure every label has an rtx. */ |
4707 | label_rtx (decl); | |
4708 | ||
4709 | DECL_CONTEXT (decl) = current_function_decl; | |
4710 | DECL_MODE (decl) = VOIDmode; | |
4711 | C_DECLARED_LABEL_FLAG (decl) = local_p; | |
8d08fdba | 4712 | |
acef433b MM |
4713 | /* Say where one reference is to the label, for the sake of the |
4714 | error if it is not defined. */ | |
4715 | DECL_SOURCE_LINE (decl) = lineno; | |
4716 | DECL_SOURCE_FILE (decl) = input_filename; | |
4717 | ||
4718 | /* Record the fact that this identifier is bound to this label. */ | |
4719 | SET_IDENTIFIER_LABEL_VALUE (id, decl); | |
4720 | ||
4721 | /* Record this label on the list of used labels so that we can check | |
4722 | at the end of the function to see whether or not the label was | |
4723 | actually defined. */ | |
4724 | if ((named_label_uses == NULL || named_label_uses->label_decl != decl) | |
e349ee73 MS |
4725 | && (named_label_uses == NULL |
4726 | || named_label_uses->names_in_scope != current_binding_level->names | |
4727 | || named_label_uses->label_decl != decl)) | |
4728 | { | |
4729 | struct named_label_list *new_ent; | |
4730 | new_ent | |
4731 | = (struct named_label_list*)oballoc (sizeof (struct named_label_list)); | |
4732 | new_ent->label_decl = decl; | |
4733 | new_ent->names_in_scope = current_binding_level->names; | |
4734 | new_ent->binding_level = current_binding_level; | |
4735 | new_ent->lineno_o_goto = lineno; | |
4736 | new_ent->filename_o_goto = input_filename; | |
4737 | new_ent->next = named_label_uses; | |
4738 | named_label_uses = new_ent; | |
8d08fdba MS |
4739 | } |
4740 | ||
acef433b MM |
4741 | return decl; |
4742 | } | |
8d08fdba | 4743 | |
acef433b MM |
4744 | /* Look for a label named ID in the current function. If one cannot |
4745 | be found, create one. (We keep track of used, but undefined, | |
4746 | labels, and complain about them at the end of a function.) */ | |
8d08fdba | 4747 | |
68642fb6 | 4748 | tree |
acef433b MM |
4749 | lookup_label (id) |
4750 | tree id; | |
4751 | { | |
4752 | tree decl; | |
8d08fdba | 4753 | |
acef433b MM |
4754 | /* You can't use labels at global scope. */ |
4755 | if (current_function_decl == NULL_TREE) | |
4756 | { | |
4757 | error ("label `%s' referenced outside of any function", | |
4758 | IDENTIFIER_POINTER (id)); | |
4759 | return NULL_TREE; | |
4760 | } | |
68642fb6 | 4761 | |
acef433b MM |
4762 | /* See if we've already got this label. */ |
4763 | decl = IDENTIFIER_LABEL_VALUE (id); | |
4764 | if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl) | |
4765 | return decl; | |
8d08fdba | 4766 | |
acef433b MM |
4767 | /* Record this label on the list of labels used in this function. |
4768 | We do this before calling make_label_decl so that we get the | |
4769 | IDENTIFIER_LABEL_VALUE before the new label is declared. */ | |
4770 | named_labels = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE, | |
4771 | named_labels); | |
4772 | /* We need a new label. */ | |
4773 | decl = make_label_decl (id, /*local_p=*/0); | |
4774 | /* Now fill in the information we didn't have before. */ | |
4775 | TREE_VALUE (named_labels) = decl; | |
8d08fdba MS |
4776 | |
4777 | return decl; | |
4778 | } | |
4779 | ||
acef433b | 4780 | /* Declare a local label named ID. */ |
8d08fdba MS |
4781 | |
4782 | tree | |
acef433b MM |
4783 | declare_local_label (id) |
4784 | tree id; | |
8d08fdba | 4785 | { |
acef433b | 4786 | tree decl; |
8d08fdba | 4787 | |
acef433b MM |
4788 | /* Add a new entry to the SHADOWED_LABELS list so that when we leave |
4789 | this scope we can restore the old value of | |
4790 | IDENTIFIER_TYPE_VALUE. */ | |
68642fb6 | 4791 | current_binding_level->shadowed_labels |
acef433b MM |
4792 | = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE, |
4793 | current_binding_level->shadowed_labels); | |
4794 | /* Look for the label. */ | |
4795 | decl = make_label_decl (id, /*local_p=*/1); | |
4796 | /* Now fill in the information we didn't have before. */ | |
4797 | TREE_VALUE (current_binding_level->shadowed_labels) = decl; | |
68642fb6 | 4798 | |
acef433b | 4799 | return decl; |
8d08fdba MS |
4800 | } |
4801 | ||
4802 | /* Define a label, specifying the location in the source file. | |
4803 | Return the LABEL_DECL node for the label, if the definition is valid. | |
4804 | Otherwise return 0. */ | |
4805 | ||
4806 | tree | |
4807 | define_label (filename, line, name) | |
4808 | char *filename; | |
4809 | int line; | |
4810 | tree name; | |
4811 | { | |
f01b0acb | 4812 | tree decl = lookup_label (name); |
8d08fdba MS |
4813 | |
4814 | /* After labels, make any new cleanups go into their | |
4815 | own new (temporary) binding contour. */ | |
4816 | current_binding_level->more_cleanups_ok = 0; | |
4817 | ||
e1cd6e56 | 4818 | if (name == get_identifier ("wchar_t")) |
8251199e | 4819 | cp_pedwarn ("label named wchar_t"); |
e1cd6e56 | 4820 | |
8d08fdba MS |
4821 | if (DECL_INITIAL (decl) != NULL_TREE) |
4822 | { | |
8251199e | 4823 | cp_error ("duplicate label `%D'", decl); |
8d08fdba MS |
4824 | return 0; |
4825 | } | |
4826 | else | |
4827 | { | |
e349ee73 | 4828 | struct named_label_list *uses, *prev; |
28cbf42c | 4829 | int identified = 0; |
227cf171 | 4830 | int saw_eh = 0; |
8d08fdba MS |
4831 | |
4832 | /* Mark label as having been defined. */ | |
4833 | DECL_INITIAL (decl) = error_mark_node; | |
4834 | /* Say where in the source. */ | |
4835 | DECL_SOURCE_FILE (decl) = filename; | |
4836 | DECL_SOURCE_LINE (decl) = line; | |
4837 | ||
e349ee73 MS |
4838 | prev = NULL; |
4839 | uses = named_label_uses; | |
4840 | while (uses != NULL) | |
4841 | if (uses->label_decl == decl) | |
8d08fdba MS |
4842 | { |
4843 | struct binding_level *b = current_binding_level; | |
4844 | while (b) | |
4845 | { | |
4846 | tree new_decls = b->names; | |
e349ee73 MS |
4847 | tree old_decls = (b == uses->binding_level) |
4848 | ? uses->names_in_scope : NULL_TREE; | |
8d08fdba MS |
4849 | while (new_decls != old_decls) |
4850 | { | |
4851 | if (TREE_CODE (new_decls) == VAR_DECL | |
4852 | /* Don't complain about crossing initialization | |
4853 | of internal entities. They can't be accessed, | |
4854 | and they should be cleaned up | |
4855 | by the time we get to the label. */ | |
700f8a87 | 4856 | && ! DECL_ARTIFICIAL (new_decls) |
5524676d JM |
4857 | && !(DECL_INITIAL (new_decls) == NULL_TREE |
4858 | && pod_type_p (TREE_TYPE (new_decls)))) | |
8d08fdba | 4859 | { |
b607c87f JM |
4860 | /* This is really only important if we're crossing |
4861 | an initialization. The POD stuff is just | |
4862 | pedantry; why should it matter if the class | |
4863 | contains a field of pointer to member type? */ | |
4864 | int problem = (DECL_INITIAL (new_decls) | |
4865 | || (TYPE_NEEDS_CONSTRUCTING | |
4866 | (TREE_TYPE (new_decls)))); | |
4867 | ||
4868 | if (! identified) | |
e349ee73 | 4869 | { |
b607c87f JM |
4870 | if (problem) |
4871 | { | |
4872 | cp_error ("jump to label `%D'", decl); | |
4873 | error_with_file_and_line | |
4874 | (uses->filename_o_goto, | |
4875 | uses->lineno_o_goto, " from here"); | |
4876 | } | |
4877 | else | |
4878 | { | |
4879 | cp_pedwarn ("jump to label `%D'", decl); | |
4880 | pedwarn_with_file_and_line | |
4881 | (uses->filename_o_goto, | |
4882 | uses->lineno_o_goto, " from here"); | |
4883 | } | |
e349ee73 | 4884 | identified = 1; |
227cf171 | 4885 | } |
b607c87f JM |
4886 | |
4887 | if (problem) | |
5524676d JM |
4888 | cp_error_at (" crosses initialization of `%#D'", |
4889 | new_decls); | |
4890 | else | |
b607c87f | 4891 | cp_pedwarn_at (" enters scope of non-POD `%#D'", |
5524676d | 4892 | new_decls); |
8d08fdba MS |
4893 | } |
4894 | new_decls = TREE_CHAIN (new_decls); | |
4895 | } | |
e349ee73 | 4896 | if (b == uses->binding_level) |
8d08fdba | 4897 | break; |
227cf171 JM |
4898 | if (b->eh_region && ! saw_eh) |
4899 | { | |
4900 | if (! identified) | |
4901 | { | |
4902 | cp_error ("jump to label `%D'", decl); | |
4903 | error_with_file_and_line | |
4904 | (uses->filename_o_goto, | |
4905 | uses->lineno_o_goto, " from here"); | |
4906 | identified = 1; | |
4907 | } | |
4908 | error (" enters exception handling block"); | |
4909 | saw_eh = 1; | |
4910 | } | |
8d08fdba MS |
4911 | b = b->level_chain; |
4912 | } | |
4913 | ||
e349ee73 MS |
4914 | if (prev != NULL) |
4915 | prev->next = uses->next; | |
8d08fdba | 4916 | else |
e349ee73 MS |
4917 | named_label_uses = uses->next; |
4918 | ||
4919 | uses = uses->next; | |
4920 | } | |
4921 | else | |
4922 | { | |
4923 | prev = uses; | |
4924 | uses = uses->next; | |
8d08fdba MS |
4925 | } |
4926 | current_function_return_value = NULL_TREE; | |
4927 | return decl; | |
4928 | } | |
4929 | } | |
4930 | ||
a5894242 MS |
4931 | struct cp_switch |
4932 | { | |
4933 | struct binding_level *level; | |
4934 | struct cp_switch *next; | |
4935 | }; | |
4936 | ||
4937 | static struct cp_switch *switch_stack; | |
4938 | ||
4939 | void | |
4940 | push_switch () | |
4941 | { | |
4942 | struct cp_switch *p | |
4943 | = (struct cp_switch *) oballoc (sizeof (struct cp_switch)); | |
4944 | p->level = current_binding_level; | |
4945 | p->next = switch_stack; | |
4946 | switch_stack = p; | |
4947 | } | |
4948 | ||
4949 | void | |
4950 | pop_switch () | |
4951 | { | |
4952 | switch_stack = switch_stack->next; | |
4953 | } | |
4954 | ||
b0a1da19 JM |
4955 | /* Note that we've seen a definition of a case label, and complain if this |
4956 | is a bad place for one. */ | |
e92cc029 | 4957 | |
8d08fdba | 4958 | void |
b370501f | 4959 | define_case_label () |
8d08fdba MS |
4960 | { |
4961 | tree cleanup = last_cleanup_this_contour (); | |
a5894242 MS |
4962 | struct binding_level *b = current_binding_level; |
4963 | int identified = 0; | |
4964 | ||
b0a1da19 JM |
4965 | if (! switch_stack) |
4966 | /* Don't crash; we'll complain in do_case. */ | |
4967 | return; | |
68642fb6 | 4968 | |
8d08fdba MS |
4969 | if (cleanup) |
4970 | { | |
4971 | static int explained = 0; | |
8251199e JM |
4972 | cp_warning_at ("destructor needed for `%#D'", TREE_PURPOSE (cleanup)); |
4973 | warning ("where case label appears here"); | |
8d08fdba MS |
4974 | if (!explained) |
4975 | { | |
8251199e JM |
4976 | warning ("(enclose actions of previous case statements requiring"); |
4977 | warning ("destructors in their own binding contours.)"); | |
8d08fdba MS |
4978 | explained = 1; |
4979 | } | |
4980 | } | |
4981 | ||
a5894242 MS |
4982 | for (; b && b != switch_stack->level; b = b->level_chain) |
4983 | { | |
4984 | tree new_decls = b->names; | |
4985 | for (; new_decls; new_decls = TREE_CHAIN (new_decls)) | |
4986 | { | |
4987 | if (TREE_CODE (new_decls) == VAR_DECL | |
4988 | /* Don't complain about crossing initialization | |
4989 | of internal entities. They can't be accessed, | |
4990 | and they should be cleaned up | |
4991 | by the time we get to the label. */ | |
4992 | && ! DECL_ARTIFICIAL (new_decls) | |
4993 | && ((DECL_INITIAL (new_decls) != NULL_TREE | |
4994 | && DECL_INITIAL (new_decls) != error_mark_node) | |
4995 | || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (new_decls)))) | |
4996 | { | |
4997 | if (! identified) | |
8251199e | 4998 | error ("jump to case label"); |
a5894242 | 4999 | identified = 1; |
8251199e | 5000 | cp_error_at (" crosses initialization of `%#D'", |
a5894242 MS |
5001 | new_decls); |
5002 | } | |
5003 | } | |
5004 | } | |
5005 | ||
8d08fdba MS |
5006 | /* After labels, make any new cleanups go into their |
5007 | own new (temporary) binding contour. */ | |
5008 | ||
5009 | current_binding_level->more_cleanups_ok = 0; | |
5010 | current_function_return_value = NULL_TREE; | |
5011 | } | |
5012 | \f | |
5013 | /* Return the list of declarations of the current level. | |
5014 | Note that this list is in reverse order unless/until | |
5015 | you nreverse it; and when you do nreverse it, you must | |
5016 | store the result back using `storedecls' or you will lose. */ | |
5017 | ||
5018 | tree | |
5019 | getdecls () | |
5020 | { | |
5021 | return current_binding_level->names; | |
5022 | } | |
5023 | ||
5024 | /* Return the list of type-tags (for structs, etc) of the current level. */ | |
5025 | ||
5026 | tree | |
5027 | gettags () | |
5028 | { | |
5029 | return current_binding_level->tags; | |
5030 | } | |
5031 | ||
5032 | /* Store the list of declarations of the current level. | |
5033 | This is done for the parameter declarations of a function being defined, | |
5034 | after they are modified in the light of any missing parameters. */ | |
5035 | ||
5036 | static void | |
5037 | storedecls (decls) | |
5038 | tree decls; | |
5039 | { | |
5040 | current_binding_level->names = decls; | |
5041 | } | |
5042 | ||
5043 | /* Similarly, store the list of tags of the current level. */ | |
5044 | ||
280f9385 | 5045 | void |
8d08fdba MS |
5046 | storetags (tags) |
5047 | tree tags; | |
5048 | { | |
5049 | current_binding_level->tags = tags; | |
5050 | } | |
5051 | \f | |
5052 | /* Given NAME, an IDENTIFIER_NODE, | |
5053 | return the structure (or union or enum) definition for that name. | |
5054 | Searches binding levels from BINDING_LEVEL up to the global level. | |
5055 | If THISLEVEL_ONLY is nonzero, searches only the specified context | |
5056 | (but skips any tag-transparent contexts to find one that is | |
5057 | meaningful for tags). | |
5058 | FORM says which kind of type the caller wants; | |
5059 | it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE. | |
5060 | If the wrong kind of type is found, and it's not a template, an error is | |
5061 | reported. */ | |
5062 | ||
5063 | static tree | |
5064 | lookup_tag (form, name, binding_level, thislevel_only) | |
5065 | enum tree_code form; | |
8d08fdba | 5066 | tree name; |
cffa8729 | 5067 | struct binding_level *binding_level; |
8d08fdba MS |
5068 | int thislevel_only; |
5069 | { | |
5070 | register struct binding_level *level; | |
74b846e0 MM |
5071 | /* Non-zero if, we should look past a template parameter level, even |
5072 | if THISLEVEL_ONLY. */ | |
5073 | int allow_template_parms_p = 1; | |
8d08fdba MS |
5074 | |
5075 | for (level = binding_level; level; level = level->level_chain) | |
5076 | { | |
5077 | register tree tail; | |
5078 | if (ANON_AGGRNAME_P (name)) | |
5079 | for (tail = level->tags; tail; tail = TREE_CHAIN (tail)) | |
5080 | { | |
5081 | /* There's no need for error checking here, because | |
5082 | anon names are unique throughout the compilation. */ | |
5083 | if (TYPE_IDENTIFIER (TREE_VALUE (tail)) == name) | |
5084 | return TREE_VALUE (tail); | |
5085 | } | |
2c73f9f5 ML |
5086 | else if (level->namespace_p) |
5087 | /* Do namespace lookup. */ | |
6c011b01 | 5088 | for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail)) |
2c73f9f5 | 5089 | { |
36a117a5 MM |
5090 | tree old = binding_for_name (name, tail); |
5091 | ||
74b846e0 MM |
5092 | /* If we just skipped past a template parameter level, |
5093 | even though THISLEVEL_ONLY, and we find a template | |
5094 | class declaration, then we use the _TYPE node for the | |
36a117a5 | 5095 | template. See the example below. */ |
74b846e0 | 5096 | if (thislevel_only && !allow_template_parms_p |
68642fb6 | 5097 | && old && BINDING_VALUE (old) |
36a117a5 MM |
5098 | && DECL_CLASS_TEMPLATE_P (BINDING_VALUE (old))) |
5099 | old = TREE_TYPE (BINDING_VALUE (old)); | |
68642fb6 | 5100 | else |
36a117a5 MM |
5101 | old = BINDING_TYPE (old); |
5102 | ||
2c73f9f5 ML |
5103 | /* If it has an original type, it is a typedef, and we |
5104 | should not return it. */ | |
5105 | if (old && DECL_ORIGINAL_TYPE (TYPE_NAME (old))) | |
5106 | old = NULL_TREE; | |
5107 | if (old && TREE_CODE (old) != form | |
5108 | && !(form != ENUMERAL_TYPE && TREE_CODE (old) == TEMPLATE_DECL)) | |
5109 | { | |
8251199e | 5110 | cp_error ("`%#D' redeclared as %C", old, form); |
2c73f9f5 ML |
5111 | return NULL_TREE; |
5112 | } | |
5113 | if (old) | |
5114 | return old; | |
5115 | if (thislevel_only || tail == global_namespace) | |
5116 | return NULL_TREE; | |
5117 | } | |
8d08fdba MS |
5118 | else |
5119 | for (tail = level->tags; tail; tail = TREE_CHAIN (tail)) | |
5120 | { | |
a80e4195 | 5121 | if (TREE_PURPOSE (tail) == name) |
8d08fdba MS |
5122 | { |
5123 | enum tree_code code = TREE_CODE (TREE_VALUE (tail)); | |
5124 | /* Should tighten this up; it'll probably permit | |
5125 | UNION_TYPE and a struct template, for example. */ | |
5126 | if (code != form | |
5566b478 | 5127 | && !(form != ENUMERAL_TYPE && code == TEMPLATE_DECL)) |
8d08fdba MS |
5128 | { |
5129 | /* Definition isn't the kind we were looking for. */ | |
8251199e | 5130 | cp_error ("`%#D' redeclared as %C", TREE_VALUE (tail), |
8d08fdba | 5131 | form); |
72b7eeff | 5132 | return NULL_TREE; |
8d08fdba MS |
5133 | } |
5134 | return TREE_VALUE (tail); | |
5135 | } | |
5136 | } | |
5137 | if (thislevel_only && ! level->tag_transparent) | |
5566b478 | 5138 | { |
74b846e0 | 5139 | if (level->template_parms_p && allow_template_parms_p) |
5566b478 | 5140 | { |
36a117a5 | 5141 | /* We must deal with cases like this: |
68642fb6 | 5142 | |
36a117a5 MM |
5143 | template <class T> struct S; |
5144 | template <class T> struct S {}; | |
68642fb6 | 5145 | |
36a117a5 MM |
5146 | When looking up `S', for the second declaration, we |
5147 | would like to find the first declaration. But, we | |
5148 | are in the pseudo-global level created for the | |
5149 | template parameters, rather than the (surrounding) | |
5150 | namespace level. Thus, we keep going one more level, | |
5151 | even though THISLEVEL_ONLY is non-zero. */ | |
74b846e0 | 5152 | allow_template_parms_p = 0; |
36a117a5 | 5153 | continue; |
5566b478 | 5154 | } |
36a117a5 MM |
5155 | else |
5156 | return NULL_TREE; | |
5566b478 | 5157 | } |
8d08fdba MS |
5158 | } |
5159 | return NULL_TREE; | |
5160 | } | |
5161 | ||
bd6dd845 | 5162 | #if 0 |
8d08fdba MS |
5163 | void |
5164 | set_current_level_tags_transparency (tags_transparent) | |
5165 | int tags_transparent; | |
5166 | { | |
5167 | current_binding_level->tag_transparent = tags_transparent; | |
5168 | } | |
bd6dd845 | 5169 | #endif |
8d08fdba MS |
5170 | |
5171 | /* Given a type, find the tag that was defined for it and return the tag name. | |
5172 | Otherwise return 0. However, the value can never be 0 | |
5173 | in the cases in which this is used. | |
5174 | ||
5175 | C++: If NAME is non-zero, this is the new name to install. This is | |
5176 | done when replacing anonymous tags with real tag names. */ | |
5177 | ||
5178 | static tree | |
5179 | lookup_tag_reverse (type, name) | |
5180 | tree type; | |
5181 | tree name; | |
5182 | { | |
5183 | register struct binding_level *level; | |
5184 | ||
5185 | for (level = current_binding_level; level; level = level->level_chain) | |
5186 | { | |
5187 | register tree tail; | |
5188 | for (tail = level->tags; tail; tail = TREE_CHAIN (tail)) | |
5189 | { | |
5190 | if (TREE_VALUE (tail) == type) | |
5191 | { | |
5192 | if (name) | |
5193 | TREE_PURPOSE (tail) = name; | |
5194 | return TREE_PURPOSE (tail); | |
5195 | } | |
5196 | } | |
5197 | } | |
5198 | return NULL_TREE; | |
5199 | } | |
8d08fdba | 5200 | \f |
a9aedbc2 | 5201 | /* Look up NAME in the NAMESPACE. */ |
e92cc029 | 5202 | |
a9aedbc2 MS |
5203 | tree |
5204 | lookup_namespace_name (namespace, name) | |
5205 | tree namespace, name; | |
5206 | { | |
30394414 | 5207 | tree val; |
f30c84c9 | 5208 | tree template_id = NULL_TREE; |
2c73f9f5 | 5209 | |
30394414 | 5210 | my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370); |
1231fb96 | 5211 | |
1231fb96 | 5212 | if (TREE_CODE (name) == NAMESPACE_DECL) |
d8f8dca1 | 5213 | /* This happens for A::B<int> when B is a namespace. */ |
1231fb96 | 5214 | return name; |
d8f8dca1 MM |
5215 | else if (TREE_CODE (name) == TEMPLATE_DECL) |
5216 | { | |
5217 | /* This happens for A::B where B is a template, and there are no | |
5218 | template arguments. */ | |
5219 | cp_error ("invalid use of `%D'", name); | |
5220 | return error_mark_node; | |
5221 | } | |
1231fb96 | 5222 | |
b262d64c JM |
5223 | namespace = ORIGINAL_NAMESPACE (namespace); |
5224 | ||
f30c84c9 MM |
5225 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) |
5226 | { | |
5227 | template_id = name; | |
5228 | name = TREE_OPERAND (name, 0); | |
5229 | if (TREE_CODE (name) == OVERLOAD) | |
5230 | name = DECL_NAME (OVL_CURRENT (name)); | |
5231 | else if (TREE_CODE_CLASS (TREE_CODE (name)) == 'd') | |
5232 | name = DECL_NAME (name); | |
5233 | } | |
5234 | ||
30394414 | 5235 | my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373); |
68642fb6 | 5236 | |
87e3dbc9 | 5237 | val = make_node (CPLUS_BINDING); |
52c11ef6 | 5238 | if (!qualified_lookup_using_namespace (name, namespace, val, 0)) |
2c73f9f5 ML |
5239 | return error_mark_node; |
5240 | ||
5241 | if (BINDING_VALUE (val)) | |
1c35f5b6 JM |
5242 | { |
5243 | val = BINDING_VALUE (val); | |
5244 | ||
f30c84c9 MM |
5245 | if (template_id) |
5246 | { | |
5247 | if (DECL_CLASS_TEMPLATE_P (val)) | |
68642fb6 | 5248 | val = lookup_template_class (val, |
f30c84c9 MM |
5249 | TREE_OPERAND (template_id, 1), |
5250 | /*in_decl=*/NULL_TREE, | |
5251 | /*context=*/NULL_TREE, | |
5252 | /*entering_scope=*/0); | |
5253 | else if (DECL_FUNCTION_TEMPLATE_P (val) | |
5254 | || TREE_CODE (val) == OVERLOAD) | |
68642fb6 | 5255 | val = lookup_template_function (val, |
f30c84c9 MM |
5256 | TREE_OPERAND (template_id, 1)); |
5257 | else | |
5258 | { | |
5259 | cp_error ("`%D::%D' is not a template", | |
5260 | namespace, name); | |
5261 | return error_mark_node; | |
5262 | } | |
5263 | } | |
5264 | ||
1c35f5b6 JM |
5265 | /* If we have a single function from a using decl, pull it out. */ |
5266 | if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val)) | |
5267 | val = OVL_FUNCTION (val); | |
5268 | return val; | |
5269 | } | |
5270 | ||
8251199e | 5271 | cp_error ("`%D' undeclared in namespace `%D'", name, namespace); |
30394414 | 5272 | return error_mark_node; |
a9aedbc2 MS |
5273 | } |
5274 | ||
7ddedda4 MM |
5275 | /* Hash a TYPENAME_TYPE. K is really of type `tree'. */ |
5276 | ||
5277 | static unsigned long | |
5278 | typename_hash (k) | |
5279 | hash_table_key k; | |
5280 | { | |
5281 | unsigned long hash; | |
5282 | tree t; | |
5283 | ||
5284 | t = (tree) k; | |
5285 | hash = (((unsigned long) TYPE_CONTEXT (t)) | |
5286 | ^ ((unsigned long) DECL_NAME (TYPE_NAME (t)))); | |
5287 | ||
5288 | return hash; | |
5289 | } | |
5290 | ||
5291 | /* Compare two TYPENAME_TYPEs. K1 and K2 are really of type `tree'. */ | |
5292 | ||
5293 | static boolean | |
5294 | typename_compare (k1, k2) | |
5295 | hash_table_key k1; | |
5296 | hash_table_key k2; | |
5297 | { | |
5298 | tree t1; | |
5299 | tree t2; | |
5300 | tree d1; | |
5301 | tree d2; | |
5302 | ||
5303 | t1 = (tree) k1; | |
5304 | t2 = (tree) k2; | |
5305 | d1 = TYPE_NAME (t1); | |
5306 | d2 = TYPE_NAME (t2); | |
68642fb6 | 5307 | |
7ddedda4 MM |
5308 | return (DECL_NAME (d1) == DECL_NAME (d2) |
5309 | && same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)) | |
68642fb6 | 5310 | && ((TREE_TYPE (t1) != NULL_TREE) |
7ddedda4 MM |
5311 | == (TREE_TYPE (t2) != NULL_TREE)) |
5312 | && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)) | |
5313 | && TYPENAME_TYPE_FULLNAME (t1) == TYPENAME_TYPE_FULLNAME (t2)); | |
5314 | } | |
5315 | ||
45869a6c MM |
5316 | /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is |
5317 | the type of `T', NAME is the IDENTIFIER_NODE for `t'. If BASE_TYPE | |
5318 | is non-NULL, this type is being created by the implicit typename | |
5319 | extension, and BASE_TYPE is a type named `t' in some base class of | |
68642fb6 | 5320 | `T' which depends on template parameters. |
45869a6c MM |
5321 | |
5322 | Returns the new TYPENAME_TYPE. */ | |
5323 | ||
5324 | tree | |
5325 | build_typename_type (context, name, fullname, base_type) | |
5326 | tree context; | |
5327 | tree name; | |
5328 | tree fullname; | |
5329 | tree base_type; | |
5330 | { | |
5331 | tree t; | |
5332 | tree d; | |
7ddedda4 | 5333 | struct hash_entry* e; |
45869a6c | 5334 | |
7ddedda4 MM |
5335 | static struct hash_table ht; |
5336 | ||
9cd64686 MM |
5337 | if (!ht.table) |
5338 | { | |
5339 | static struct hash_table *h = &ht; | |
68642fb6 | 5340 | if (!hash_table_init (&ht, &hash_newfunc, &typename_hash, |
9cd64686 MM |
5341 | &typename_compare)) |
5342 | fatal ("virtual memory exhausted"); | |
5343 | ggc_add_tree_hash_table_root (&h, 1); | |
5344 | } | |
45869a6c MM |
5345 | |
5346 | /* Build the TYPENAME_TYPE. */ | |
33848bb0 | 5347 | t = make_aggr_type (TYPENAME_TYPE); |
45869a6c MM |
5348 | TYPE_CONTEXT (t) = FROB_CONTEXT (context); |
5349 | TYPENAME_TYPE_FULLNAME (t) = fullname; | |
5350 | TREE_TYPE (t) = base_type; | |
45869a6c MM |
5351 | |
5352 | /* Build the corresponding TYPE_DECL. */ | |
5353 | d = build_decl (TYPE_DECL, name, t); | |
5354 | TYPE_NAME (TREE_TYPE (d)) = d; | |
5355 | TYPE_STUB_DECL (TREE_TYPE (d)) = d; | |
5356 | DECL_CONTEXT (d) = FROB_CONTEXT (context); | |
2642b9bf | 5357 | DECL_ARTIFICIAL (d) = 1; |
45869a6c | 5358 | |
7ddedda4 MM |
5359 | /* See if we already have this type. */ |
5360 | e = hash_lookup (&ht, t, /*create=*/false, /*copy=*/0); | |
5361 | if (e) | |
87e3dbc9 | 5362 | t = (tree) e->key; |
7ddedda4 MM |
5363 | else |
5364 | /* Insert the type into the table. */ | |
5365 | hash_lookup (&ht, t, /*create=*/true, /*copy=*/0); | |
5366 | ||
45869a6c MM |
5367 | return t; |
5368 | } | |
5369 | ||
3baa501d MM |
5370 | /* Resolve `typename CONTEXT::NAME'. Returns an appropriate type, |
5371 | unless an error occurs, in which case error_mark_node is returned. | |
5372 | If COMPLAIN zero, don't complain about any errors that occur. */ | |
5373 | ||
5566b478 | 5374 | tree |
3baa501d | 5375 | make_typename_type (context, name, complain) |
5566b478 | 5376 | tree context, name; |
3baa501d | 5377 | int complain; |
5566b478 | 5378 | { |
b2b7d40a | 5379 | tree fullname; |
a80e4195 | 5380 | |
653cc74a | 5381 | if (TREE_CODE_CLASS (TREE_CODE (name)) == 't') |
78638e24 | 5382 | { |
68642fb6 UD |
5383 | if (!(TYPE_LANG_SPECIFIC (name) |
5384 | && (CLASSTYPE_IS_TEMPLATE (name) | |
78638e24 MM |
5385 | || CLASSTYPE_USE_TEMPLATE (name)))) |
5386 | name = TYPE_IDENTIFIER (name); | |
5387 | else | |
5388 | /* Create a TEMPLATE_ID_EXPR for the type. */ | |
5389 | name = build_nt (TEMPLATE_ID_EXPR, | |
5390 | CLASSTYPE_TI_TEMPLATE (name), | |
5391 | CLASSTYPE_TI_ARGS (name)); | |
5392 | } | |
653cc74a | 5393 | else if (TREE_CODE (name) == TYPE_DECL) |
a80e4195 | 5394 | name = DECL_NAME (name); |
b2b7d40a JM |
5395 | |
5396 | fullname = name; | |
5397 | ||
5398 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) | |
11686454 JM |
5399 | { |
5400 | name = TREE_OPERAND (name, 0); | |
5401 | if (TREE_CODE (name) == TEMPLATE_DECL) | |
5402 | name = TREE_OPERAND (fullname, 0) = DECL_NAME (name); | |
5403 | } | |
b2b7d40a | 5404 | if (TREE_CODE (name) != IDENTIFIER_NODE) |
a80e4195 | 5405 | my_friendly_abort (2000); |
5566b478 | 5406 | |
04ddee1b BK |
5407 | if (TREE_CODE (context) == NAMESPACE_DECL) |
5408 | { | |
5409 | /* We can get here from typename_sub0 in the explicit_template_type | |
5410 | expansion. Just fail. */ | |
3baa501d MM |
5411 | if (complain) |
5412 | cp_error ("no class template named `%#T' in `%#T'", | |
5413 | name, context); | |
04ddee1b BK |
5414 | return error_mark_node; |
5415 | } | |
5416 | ||
85b71cf2 | 5417 | if (! uses_template_parms (context) |
b77ead33 | 5418 | || currently_open_class (context)) |
5566b478 | 5419 | { |
b2b7d40a JM |
5420 | if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR) |
5421 | { | |
ad810b22 | 5422 | tree tmpl = NULL_TREE; |
b2b7d40a | 5423 | if (IS_AGGR_TYPE (context)) |
ad810b22 MM |
5424 | tmpl = lookup_field (context, name, 0, 0); |
5425 | if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl)) | |
b2b7d40a | 5426 | { |
3baa501d MM |
5427 | if (complain) |
5428 | cp_error ("no class template named `%#T' in `%#T'", | |
5429 | name, context); | |
b2b7d40a JM |
5430 | return error_mark_node; |
5431 | } | |
ffb690bd | 5432 | |
68642fb6 | 5433 | return lookup_template_class (tmpl, |
ad810b22 | 5434 | TREE_OPERAND (fullname, 1), |
68642fb6 | 5435 | NULL_TREE, context, |
ad810b22 | 5436 | /*entering_scope=*/0); |
b2b7d40a JM |
5437 | } |
5438 | else | |
5566b478 | 5439 | { |
b4f70b3d | 5440 | tree t; |
68642fb6 | 5441 | |
b4f70b3d | 5442 | if (!IS_AGGR_TYPE (context)) |
b2b7d40a | 5443 | { |
3baa501d MM |
5444 | if (complain) |
5445 | cp_error ("no type named `%#T' in `%#T'", name, context); | |
b2b7d40a JM |
5446 | return error_mark_node; |
5447 | } | |
1107c4b3 | 5448 | |
b4f70b3d | 5449 | t = lookup_field (context, name, 0, 1); |
7d4bdeed MM |
5450 | if (t) |
5451 | return TREE_TYPE (t); | |
5566b478 | 5452 | } |
5566b478 | 5453 | } |
11249cf0 MM |
5454 | |
5455 | /* If the CONTEXT is not a template type, then either the field is | |
5456 | there now or its never going to be. */ | |
b4f70b3d | 5457 | if (!uses_template_parms (context)) |
11249cf0 | 5458 | { |
3baa501d MM |
5459 | if (complain) |
5460 | cp_error ("no type named `%#T' in `%#T'", name, context); | |
11249cf0 MM |
5461 | return error_mark_node; |
5462 | } | |
68642fb6 UD |
5463 | |
5464 | ||
45869a6c | 5465 | return build_typename_type (context, name, fullname, NULL_TREE); |
5566b478 MS |
5466 | } |
5467 | ||
2c73f9f5 ML |
5468 | /* Select the right _DECL from multiple choices. */ |
5469 | ||
5470 | static tree | |
52c11ef6 | 5471 | select_decl (binding, flags) |
2c73f9f5 | 5472 | tree binding; |
52c11ef6 | 5473 | int flags; |
2c73f9f5 ML |
5474 | { |
5475 | tree val; | |
5476 | val = BINDING_VALUE (binding); | |
52c11ef6 | 5477 | if (LOOKUP_NAMESPACES_ONLY (flags)) |
3e3f722c ML |
5478 | { |
5479 | /* We are not interested in types. */ | |
5480 | if (val && TREE_CODE (val) == NAMESPACE_DECL) | |
5481 | return val; | |
5482 | return NULL_TREE; | |
5483 | } | |
68642fb6 | 5484 | |
2c73f9f5 ML |
5485 | /* If we could have a type and |
5486 | we have nothing or we need a type and have none. */ | |
5487 | if (BINDING_TYPE (binding) | |
52c11ef6 JM |
5488 | && (!val || ((flags & LOOKUP_PREFER_TYPES) |
5489 | && TREE_CODE (val) != TYPE_DECL))) | |
2c73f9f5 ML |
5490 | val = TYPE_STUB_DECL (BINDING_TYPE (binding)); |
5491 | /* Don't return non-types if we really prefer types. */ | |
52c11ef6 | 5492 | else if (val && LOOKUP_TYPES_ONLY (flags) && TREE_CODE (val) != TYPE_DECL |
c592d5d2 ML |
5493 | && (TREE_CODE (val) != TEMPLATE_DECL |
5494 | || !DECL_CLASS_TEMPLATE_P (val))) | |
2c73f9f5 | 5495 | val = NULL_TREE; |
1c35f5b6 | 5496 | |
2c73f9f5 ML |
5497 | return val; |
5498 | } | |
5499 | ||
2c169bab JM |
5500 | /* Unscoped lookup of a global: iterate over current namespaces, |
5501 | considering using-directives. If SPACESP is non-NULL, store a list | |
5502 | of the namespaces we've considered in it. */ | |
ea9635c7 | 5503 | |
2c169bab JM |
5504 | tree |
5505 | unqualified_namespace_lookup (name, flags, spacesp) | |
ea9635c7 | 5506 | tree name; |
52c11ef6 | 5507 | int flags; |
2c169bab | 5508 | tree *spacesp; |
ea9635c7 | 5509 | { |
87e3dbc9 | 5510 | tree b = make_node (CPLUS_BINDING); |
ea9635c7 ML |
5511 | tree initial = current_decl_namespace(); |
5512 | tree scope = initial; | |
5513 | tree siter; | |
5514 | struct binding_level *level; | |
5515 | tree val = NULL_TREE; | |
5516 | ||
2c169bab JM |
5517 | if (spacesp) |
5518 | *spacesp = NULL_TREE; | |
5519 | ||
5520 | for (; !val; scope = CP_DECL_CONTEXT (scope)) | |
ea9635c7 | 5521 | { |
2c169bab | 5522 | if (spacesp) |
e1b3e07d | 5523 | *spacesp = tree_cons (scope, NULL_TREE, *spacesp); |
ea9635c7 ML |
5524 | val = binding_for_name (name, scope); |
5525 | ||
5526 | /* Initialize binding for this context. */ | |
5527 | BINDING_VALUE (b) = BINDING_VALUE (val); | |
5528 | BINDING_TYPE (b) = BINDING_TYPE (val); | |
5529 | ||
5530 | /* Add all _DECLs seen through local using-directives. */ | |
68642fb6 | 5531 | for (level = current_binding_level; |
ea9635c7 ML |
5532 | !level->namespace_p; |
5533 | level = level->level_chain) | |
52c11ef6 | 5534 | if (!lookup_using_namespace (name, b, level->using_directives, |
2c169bab | 5535 | scope, flags, spacesp)) |
ea9635c7 | 5536 | /* Give up because of error. */ |
5b163de4 | 5537 | return error_mark_node; |
ea9635c7 ML |
5538 | |
5539 | /* Add all _DECLs seen through global using-directives. */ | |
5540 | /* XXX local and global using lists should work equally. */ | |
5541 | siter = initial; | |
5542 | while (1) | |
5543 | { | |
68642fb6 | 5544 | if (!lookup_using_namespace (name, b, DECL_NAMESPACE_USING (siter), |
2c169bab | 5545 | scope, flags, spacesp)) |
ea9635c7 | 5546 | /* Give up because of error. */ |
5b163de4 | 5547 | return error_mark_node; |
ea9635c7 ML |
5548 | if (siter == scope) break; |
5549 | siter = CP_DECL_CONTEXT (siter); | |
5550 | } | |
5551 | ||
52c11ef6 | 5552 | val = select_decl (b, flags); |
ea9635c7 ML |
5553 | if (scope == global_namespace) |
5554 | break; | |
ea9635c7 ML |
5555 | } |
5556 | return val; | |
5557 | } | |
5558 | ||
52c11ef6 JM |
5559 | /* Combine prefer_type and namespaces_only into flags. */ |
5560 | ||
5561 | static int | |
5562 | lookup_flags (prefer_type, namespaces_only) | |
5563 | int prefer_type, namespaces_only; | |
5564 | { | |
5565 | if (namespaces_only) | |
5566 | return LOOKUP_PREFER_NAMESPACES; | |
5567 | if (prefer_type > 1) | |
5568 | return LOOKUP_PREFER_TYPES; | |
5569 | if (prefer_type > 0) | |
5570 | return LOOKUP_PREFER_BOTH; | |
5571 | return 0; | |
5572 | } | |
5573 | ||
5574 | /* Given a lookup that returned VAL, use FLAGS to decide if we want to | |
5575 | ignore it or not. Subroutine of lookup_name_real. */ | |
5576 | ||
5577 | static tree | |
5578 | qualify_lookup (val, flags) | |
5579 | tree val; | |
5580 | int flags; | |
5581 | { | |
5582 | if (val == NULL_TREE) | |
5583 | return val; | |
6b945830 JM |
5584 | if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL) |
5585 | return val; | |
5586 | if ((flags & LOOKUP_PREFER_TYPES) | |
5587 | && (TREE_CODE (val) == TYPE_DECL | |
5588 | || ((flags & LOOKUP_TEMPLATES_EXPECTED) | |
5589 | && DECL_CLASS_TEMPLATE_P (val)))) | |
5590 | return val; | |
5591 | if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES)) | |
52c11ef6 JM |
5592 | return NULL_TREE; |
5593 | return val; | |
5594 | } | |
5595 | ||
235f734d MM |
5596 | /* Any other BINDING overrides an implicit TYPENAME. Warn about |
5597 | that. */ | |
5598 | ||
5599 | static void | |
5600 | warn_about_implicit_typename_lookup (typename, binding) | |
5601 | tree typename; | |
5602 | tree binding; | |
5603 | { | |
5604 | tree subtype = TREE_TYPE (TREE_TYPE (typename)); | |
5605 | tree name = DECL_NAME (typename); | |
5606 | ||
5607 | if (! (TREE_CODE (binding) == TEMPLATE_DECL | |
5608 | && CLASSTYPE_TEMPLATE_INFO (subtype) | |
5609 | && CLASSTYPE_TI_TEMPLATE (subtype) == binding) | |
5610 | && ! (TREE_CODE (binding) == TYPE_DECL | |
5611 | && same_type_p (TREE_TYPE (binding), subtype))) | |
5612 | { | |
68642fb6 | 5613 | cp_warning ("lookup of `%D' finds `%#D'", |
235f734d MM |
5614 | name, binding); |
5615 | cp_warning (" instead of `%D' from dependent base class", | |
5616 | typename); | |
5617 | cp_warning (" (use `typename %T::%D' if that's what you meant)", | |
5618 | constructor_name (current_class_type), name); | |
5619 | } | |
5620 | } | |
5621 | ||
8d08fdba MS |
5622 | /* Look up NAME in the current binding level and its superiors in the |
5623 | namespace of variables, functions and typedefs. Return a ..._DECL | |
5624 | node of some kind representing its definition if there is only one | |
5625 | such declaration, or return a TREE_LIST with all the overloaded | |
5626 | definitions if there are many, or return 0 if it is undefined. | |
5627 | ||
2c73f9f5 ML |
5628 | If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces. |
5629 | If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces). | |
a28e3c7f | 5630 | If PREFER_TYPE is -2, we're being called from yylex(). (UGLY) |
68642fb6 | 5631 | Otherwise we prefer non-TYPE_DECLs. |
f84b4be9 JM |
5632 | |
5633 | If NONCLASS is non-zero, we don't look for the NAME in class scope, | |
5634 | using IDENTIFIER_CLASS_VALUE. */ | |
8d08fdba | 5635 | |
824b9a4c | 5636 | static tree |
3e3f722c | 5637 | lookup_name_real (name, prefer_type, nonclass, namespaces_only) |
8d08fdba | 5638 | tree name; |
3e3f722c | 5639 | int prefer_type, nonclass, namespaces_only; |
8d08fdba | 5640 | { |
235f734d MM |
5641 | tree t; |
5642 | tree val = NULL_TREE; | |
a28e3c7f | 5643 | int yylex = 0; |
e1cd6e56 | 5644 | tree from_obj = NULL_TREE; |
52c11ef6 | 5645 | int flags; |
235f734d | 5646 | int val_is_implicit_typename = 0; |
8d08fdba | 5647 | |
3e3f722c ML |
5648 | /* Hack: copy flag set by parser, if set. */ |
5649 | if (only_namespace_names) | |
5650 | namespaces_only = 1; | |
52c11ef6 | 5651 | |
a28e3c7f MS |
5652 | if (prefer_type == -2) |
5653 | { | |
5654 | extern int looking_for_typename; | |
fc378698 | 5655 | tree type = NULL_TREE; |
a28e3c7f MS |
5656 | |
5657 | yylex = 1; | |
5658 | prefer_type = looking_for_typename; | |
e1cd6e56 | 5659 | |
52c11ef6 | 5660 | flags = lookup_flags (prefer_type, namespaces_only); |
52c11ef6 JM |
5661 | /* If the next thing is '<', class templates are types. */ |
5662 | if (looking_for_template) | |
5663 | flags |= LOOKUP_TEMPLATES_EXPECTED; | |
5664 | ||
653cc74a JM |
5665 | /* std:: becomes :: for now. */ |
5666 | if (got_scope == std_node) | |
5667 | got_scope = void_type_node; | |
5668 | ||
e1cd6e56 MS |
5669 | if (got_scope) |
5670 | type = got_scope; | |
dff6b454 | 5671 | else if (got_object != error_mark_node) |
e1cd6e56 | 5672 | type = got_object; |
68642fb6 | 5673 | |
e1cd6e56 | 5674 | if (type) |
a28e3c7f | 5675 | { |
e1cd6e56 | 5676 | if (type == error_mark_node) |
f376e137 | 5677 | return error_mark_node; |
a80e4195 MS |
5678 | if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type)) |
5679 | type = TREE_TYPE (type); | |
5566b478 | 5680 | |
2b9dc906 JM |
5681 | if (TYPE_P (type)) |
5682 | type = complete_type (type); | |
5566b478 | 5683 | |
a1774733 | 5684 | if (TREE_CODE (type) == VOID_TYPE) |
2c73f9f5 ML |
5685 | type = global_namespace; |
5686 | if (TREE_CODE (type) == NAMESPACE_DECL) | |
a9aedbc2 | 5687 | { |
87e3dbc9 | 5688 | val = make_node (CPLUS_BINDING); |
6ad07332 | 5689 | flags |= LOOKUP_COMPLAIN; |
52c11ef6 | 5690 | if (!qualified_lookup_using_namespace (name, type, val, flags)) |
2c73f9f5 | 5691 | return NULL_TREE; |
52c11ef6 | 5692 | val = select_decl (val, flags); |
a9aedbc2 | 5693 | } |
5566b478 | 5694 | else if (! IS_AGGR_TYPE (type) |
5156628f | 5695 | || TREE_CODE (type) == TEMPLATE_TYPE_PARM |
73b0fce8 | 5696 | || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM |
5156628f | 5697 | || TREE_CODE (type) == TYPENAME_TYPE) |
e92cc029 | 5698 | /* Someone else will give an error about this if needed. */ |
a28e3c7f | 5699 | val = NULL_TREE; |
e1cd6e56 | 5700 | else if (type == current_class_type) |
a28e3c7f | 5701 | val = IDENTIFIER_CLASS_VALUE (name); |
a28e3c7f | 5702 | else |
70adf8a9 JM |
5703 | { |
5704 | val = lookup_member (type, name, 0, prefer_type); | |
5705 | type_access_control (type, val); | |
5706 | } | |
a28e3c7f | 5707 | } |
e1cd6e56 MS |
5708 | else |
5709 | val = NULL_TREE; | |
5710 | ||
594740f3 | 5711 | if (got_scope) |
e1cd6e56 | 5712 | goto done; |
594740f3 | 5713 | else if (got_object && val) |
e1cd6e56 | 5714 | from_obj = val; |
a28e3c7f | 5715 | } |
52c11ef6 | 5716 | else |
5b163de4 JM |
5717 | { |
5718 | flags = lookup_flags (prefer_type, namespaces_only); | |
5719 | /* If we're not parsing, we need to complain. */ | |
5720 | flags |= LOOKUP_COMPLAIN; | |
5721 | } | |
e76a2646 | 5722 | |
d8f8dca1 | 5723 | /* First, look in non-namespace scopes. */ |
6f1b4c42 JM |
5724 | |
5725 | if (current_class_type == NULL_TREE) | |
5726 | nonclass = 1; | |
5727 | ||
235f734d | 5728 | for (t = IDENTIFIER_BINDING (name); t; t = TREE_CHAIN (t)) |
d8f8dca1 | 5729 | { |
235f734d MM |
5730 | tree binding; |
5731 | ||
5732 | if (!LOCAL_BINDING_P (t) && nonclass) | |
d8f8dca1 MM |
5733 | /* We're not looking for class-scoped bindings, so keep going. */ |
5734 | continue; | |
68642fb6 | 5735 | |
d8f8dca1 | 5736 | /* If this is the kind of thing we're looking for, we're done. */ |
235f734d MM |
5737 | if (qualify_lookup (BINDING_VALUE (t), flags)) |
5738 | binding = BINDING_VALUE (t); | |
68642fb6 | 5739 | else if ((flags & LOOKUP_PREFER_TYPES) |
235f734d MM |
5740 | && qualify_lookup (BINDING_TYPE (t), flags)) |
5741 | binding = BINDING_TYPE (t); | |
5742 | else | |
5743 | binding = NULL_TREE; | |
5744 | ||
70adf8a9 JM |
5745 | /* Handle access control on types from enclosing or base classes. */ |
5746 | if (binding && ! yylex | |
5747 | && BINDING_LEVEL (t) && BINDING_LEVEL (t)->parm_flag == 2) | |
5748 | type_access_control (BINDING_LEVEL (t)->this_class, binding); | |
5749 | ||
235f734d | 5750 | if (binding |
83233dca | 5751 | && (!val || !IMPLICIT_TYPENAME_TYPE_DECL_P (binding))) |
d8f8dca1 | 5752 | { |
235f734d MM |
5753 | if (val_is_implicit_typename && !yylex) |
5754 | warn_about_implicit_typename_lookup (val, binding); | |
5755 | val = binding; | |
68642fb6 | 5756 | val_is_implicit_typename |
83233dca | 5757 | = IMPLICIT_TYPENAME_TYPE_DECL_P (val); |
235f734d MM |
5758 | if (!val_is_implicit_typename) |
5759 | break; | |
d8f8dca1 MM |
5760 | } |
5761 | } | |
f181d4ae | 5762 | |
235f734d MM |
5763 | /* Now lookup in namespace scopes. */ |
5764 | if (!val || val_is_implicit_typename) | |
e76a2646 | 5765 | { |
2c169bab | 5766 | t = unqualified_namespace_lookup (name, flags, 0); |
235f734d | 5767 | if (t) |
c1def683 | 5768 | { |
235f734d MM |
5769 | if (val_is_implicit_typename && !yylex) |
5770 | warn_about_implicit_typename_lookup (val, t); | |
5771 | val = t; | |
c1def683 JM |
5772 | } |
5773 | } | |
5774 | ||
a28e3c7f | 5775 | done: |
8d08fdba MS |
5776 | if (val) |
5777 | { | |
c91a56d2 | 5778 | /* This should only warn about types used in qualified-ids. */ |
e1cd6e56 | 5779 | if (from_obj && from_obj != val) |
5566b478 | 5780 | { |
c91a56d2 MS |
5781 | if (looking_for_typename && TREE_CODE (from_obj) == TYPE_DECL |
5782 | && TREE_CODE (val) == TYPE_DECL | |
5783 | && TREE_TYPE (from_obj) != TREE_TYPE (val)) | |
5784 | { | |
8251199e | 5785 | cp_pedwarn ("lookup of `%D' in the scope of `%#T' (`%#T')", |
c91a56d2 | 5786 | name, got_object, TREE_TYPE (from_obj)); |
8251199e | 5787 | cp_pedwarn (" does not match lookup in the current scope (`%#T')", |
c91a56d2 MS |
5788 | TREE_TYPE (val)); |
5789 | } | |
594740f3 | 5790 | |
b8b1a3c1 JM |
5791 | /* We don't change val to from_obj if got_object depends on |
5792 | template parms because that breaks implicit typename for | |
5793 | destructor calls. */ | |
5794 | if (! uses_template_parms (got_object)) | |
53fdbb3b | 5795 | val = from_obj; |
5566b478 | 5796 | } |
e1cd6e56 | 5797 | |
0c64a9ca JM |
5798 | /* If we have a single function from a using decl, pull it out. */ |
5799 | if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val)) | |
5800 | val = OVL_FUNCTION (val); | |
8d08fdba | 5801 | } |
e1cd6e56 MS |
5802 | else if (from_obj) |
5803 | val = from_obj; | |
8d08fdba MS |
5804 | |
5805 | return val; | |
5806 | } | |
5807 | ||
700f8a87 MS |
5808 | tree |
5809 | lookup_name_nonclass (name) | |
5810 | tree name; | |
5811 | { | |
3e3f722c | 5812 | return lookup_name_real (name, 0, 1, 0); |
700f8a87 MS |
5813 | } |
5814 | ||
2c73f9f5 ML |
5815 | tree |
5816 | lookup_function_nonclass (name, args) | |
5817 | tree name; | |
5818 | tree args; | |
5819 | { | |
5820 | return lookup_arg_dependent (name, lookup_name_nonclass (name), args); | |
5821 | } | |
5822 | ||
3e3f722c ML |
5823 | tree |
5824 | lookup_name_namespace_only (name) | |
5825 | tree name; | |
5826 | { | |
5827 | /* type-or-namespace, nonclass, namespace_only */ | |
5828 | return lookup_name_real (name, 1, 1, 1); | |
5829 | } | |
5830 | ||
700f8a87 MS |
5831 | tree |
5832 | lookup_name (name, prefer_type) | |
5833 | tree name; | |
5834 | int prefer_type; | |
5835 | { | |
3e3f722c | 5836 | return lookup_name_real (name, prefer_type, 0, 0); |
700f8a87 MS |
5837 | } |
5838 | ||
a7d2d407 MM |
5839 | /* Similar to `lookup_name' but look only in the innermost non-class |
5840 | binding level. */ | |
8d08fdba MS |
5841 | |
5842 | tree | |
5843 | lookup_name_current_level (name) | |
5844 | tree name; | |
5845 | { | |
a7d2d407 MM |
5846 | struct binding_level *b; |
5847 | tree t = NULL_TREE; | |
8d08fdba | 5848 | |
a7d2d407 MM |
5849 | b = current_binding_level; |
5850 | while (b->parm_flag == 2) | |
5851 | b = b->level_chain; | |
5852 | ||
5853 | if (b->namespace_p) | |
8d08fdba | 5854 | { |
5fdaba89 | 5855 | t = IDENTIFIER_NAMESPACE_VALUE (name); |
8d08fdba MS |
5856 | |
5857 | /* extern "C" function() */ | |
5858 | if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST) | |
5859 | t = TREE_VALUE (t); | |
5860 | } | |
68642fb6 | 5861 | else if (IDENTIFIER_BINDING (name) |
f181d4ae | 5862 | && LOCAL_BINDING_P (IDENTIFIER_BINDING (name))) |
8d08fdba | 5863 | { |
a4443a08 MS |
5864 | while (1) |
5865 | { | |
f181d4ae MM |
5866 | if (BINDING_LEVEL (IDENTIFIER_BINDING (name)) == b) |
5867 | return IDENTIFIER_VALUE (name); | |
68642fb6 | 5868 | |
9ed182dc JM |
5869 | if (b->keep == 2) |
5870 | b = b->level_chain; | |
5871 | else | |
5872 | break; | |
5873 | } | |
5874 | } | |
5875 | ||
5876 | return t; | |
5877 | } | |
5878 | ||
5879 | /* Like lookup_name_current_level, but for types. */ | |
5880 | ||
5881 | tree | |
5882 | lookup_type_current_level (name) | |
5883 | tree name; | |
5884 | { | |
5885 | register tree t = NULL_TREE; | |
5886 | ||
5887 | my_friendly_assert (! current_binding_level->namespace_p, 980716); | |
5888 | ||
5889 | if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE | |
5890 | && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node) | |
5891 | { | |
5892 | struct binding_level *b = current_binding_level; | |
5893 | while (1) | |
5894 | { | |
5895 | if (purpose_member (name, b->type_shadowed)) | |
5896 | return REAL_IDENTIFIER_TYPE_VALUE (name); | |
a4443a08 MS |
5897 | if (b->keep == 2) |
5898 | b = b->level_chain; | |
5899 | else | |
5900 | break; | |
5901 | } | |
8d08fdba MS |
5902 | } |
5903 | ||
5904 | return t; | |
5905 | } | |
3e3f722c ML |
5906 | |
5907 | void | |
5908 | begin_only_namespace_names () | |
5909 | { | |
5910 | only_namespace_names = 1; | |
5911 | } | |
5912 | ||
5913 | void | |
5914 | end_only_namespace_names () | |
5915 | { | |
5916 | only_namespace_names = 0; | |
5917 | } | |
8d08fdba MS |
5918 | \f |
5919 | /* Arrange for the user to get a source line number, even when the | |
5920 | compiler is going down in flames, so that she at least has a | |
5921 | chance of working around problems in the compiler. We used to | |
5922 | call error(), but that let the segmentation fault continue | |
5923 | through; now, it's much more passive by asking them to send the | |
5924 | maintainers mail about the problem. */ | |
5925 | ||
5926 | static void | |
5927 | signal_catch (sig) | |
7dee3f36 | 5928 | int sig ATTRIBUTE_UNUSED; |
8d08fdba MS |
5929 | { |
5930 | signal (SIGSEGV, SIG_DFL); | |
5931 | #ifdef SIGIOT | |
5932 | signal (SIGIOT, SIG_DFL); | |
5933 | #endif | |
5934 | #ifdef SIGILL | |
5935 | signal (SIGILL, SIG_DFL); | |
5936 | #endif | |
5937 | #ifdef SIGABRT | |
5938 | signal (SIGABRT, SIG_DFL); | |
5939 | #endif | |
5940 | #ifdef SIGBUS | |
5941 | signal (SIGBUS, SIG_DFL); | |
5942 | #endif | |
5943 | my_friendly_abort (0); | |
5944 | } | |
5945 | ||
8d08fdba MS |
5946 | /* Push the declarations of builtin types into the namespace. |
5947 | RID_INDEX, if < RID_MAX is the index of the builtin type | |
5948 | in the array RID_POINTERS. NAME is the name used when looking | |
5949 | up the builtin type. TYPE is the _TYPE node for the builtin type. */ | |
5950 | ||
5951 | static void | |
5952 | record_builtin_type (rid_index, name, type) | |
5953 | enum rid rid_index; | |
d8e178a0 | 5954 | const char *name; |
8d08fdba MS |
5955 | tree type; |
5956 | { | |
5957 | tree rname = NULL_TREE, tname = NULL_TREE; | |
a703fb38 | 5958 | tree tdecl = NULL_TREE; |
8d08fdba MS |
5959 | |
5960 | if ((int) rid_index < (int) RID_MAX) | |
5961 | rname = ridpointers[(int) rid_index]; | |
5962 | if (name) | |
5963 | tname = get_identifier (name); | |
5964 | ||
5965 | TYPE_BUILT_IN (type) = 1; | |
68642fb6 | 5966 | |
8d08fdba MS |
5967 | if (tname) |
5968 | { | |
8d08fdba | 5969 | tdecl = pushdecl (build_decl (TYPE_DECL, tname, type)); |
8d08fdba MS |
5970 | set_identifier_type_value (tname, NULL_TREE); |
5971 | if ((int) rid_index < (int) RID_MAX) | |
2c73f9f5 ML |
5972 | /* Built-in types live in the global namespace. */ |
5973 | SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl); | |
8d08fdba MS |
5974 | } |
5975 | if (rname != NULL_TREE) | |
5976 | { | |
5977 | if (tname != NULL_TREE) | |
5978 | { | |
5979 | set_identifier_type_value (rname, NULL_TREE); | |
2c73f9f5 | 5980 | SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl); |
8d08fdba MS |
5981 | } |
5982 | else | |
5983 | { | |
8d08fdba | 5984 | tdecl = pushdecl (build_decl (TYPE_DECL, rname, type)); |
8d08fdba MS |
5985 | set_identifier_type_value (rname, NULL_TREE); |
5986 | } | |
5987 | } | |
8d08fdba MS |
5988 | } |
5989 | ||
eff71ab0 PB |
5990 | /* Record one of the standard Java types. |
5991 | * Declare it as having the given NAME. | |
5992 | * If SIZE > 0, it is the size of one of the integral types; | |
5993 | * otherwise it is the negative of the size of one of the other types. */ | |
5994 | ||
5995 | static tree | |
5996 | record_builtin_java_type (name, size) | |
d8e178a0 | 5997 | const char *name; |
eff71ab0 PB |
5998 | int size; |
5999 | { | |
6000 | tree type, decl; | |
6001 | if (size > 0) | |
6002 | type = make_signed_type (size); | |
6003 | else if (size > -32) | |
6004 | { /* "__java_char" or ""__java_boolean". */ | |
6005 | type = make_unsigned_type (-size); | |
6006 | /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/ | |
6007 | } | |
6008 | else | |
6009 | { /* "__java_float" or ""__java_double". */ | |
6010 | type = make_node (REAL_TYPE); | |
6011 | TYPE_PRECISION (type) = - size; | |
6012 | layout_type (type); | |
6013 | } | |
6014 | record_builtin_type (RID_MAX, name, type); | |
6015 | decl = TYPE_NAME (type); | |
e229f2cd PB |
6016 | |
6017 | /* Suppress generate debug symbol entries for these types, | |
6018 | since for normal C++ they are just clutter. | |
6019 | However, push_lang_context undoes this if extern "Java" is seen. */ | |
eff71ab0 | 6020 | DECL_IGNORED_P (decl) = 1; |
e229f2cd | 6021 | |
eff71ab0 PB |
6022 | TYPE_FOR_JAVA (type) = 1; |
6023 | return type; | |
6024 | } | |
6025 | ||
036407f7 ML |
6026 | /* Push a type into the namespace so that the back-ends ignore it. */ |
6027 | ||
6028 | static void | |
6029 | record_unknown_type (type, name) | |
6030 | tree type; | |
d8e178a0 | 6031 | const char *name; |
036407f7 ML |
6032 | { |
6033 | tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type)); | |
6034 | /* Make sure the "unknown type" typedecl gets ignored for debug info. */ | |
6035 | DECL_IGNORED_P (decl) = 1; | |
6036 | TYPE_DECL_SUPPRESS_DEBUG (decl) = 1; | |
6037 | TYPE_SIZE (type) = TYPE_SIZE (void_type_node); | |
6038 | TYPE_ALIGN (type) = 1; | |
6039 | TYPE_MODE (type) = TYPE_MODE (void_type_node); | |
68642fb6 | 6040 | } |
036407f7 | 6041 | |
8d08fdba MS |
6042 | /* Create the predefined scalar types of C, |
6043 | and some nodes representing standard constants (0, 1, (void *)0). | |
6044 | Initialize the global binding level. | |
6045 | Make definitions for built-in primitive functions. */ | |
6046 | ||
6047 | void | |
6048 | init_decl_processing () | |
6049 | { | |
8d08fdba | 6050 | tree fields[20]; |
8d08fdba | 6051 | int wchar_type_size; |
8d08fdba MS |
6052 | tree array_domain_type; |
6053 | ||
5bb2a292 MM |
6054 | /* Check to see that the user did not specify an invalid combination |
6055 | of command-line options. */ | |
6056 | if (flag_new_abi && !flag_vtable_thunks) | |
6057 | fatal ("the new ABI requires vtable thunks"); | |
6058 | ||
8d08fdba MS |
6059 | /* Have to make these distinct before we try using them. */ |
6060 | lang_name_cplusplus = get_identifier ("C++"); | |
6061 | lang_name_c = get_identifier ("C"); | |
a1774733 | 6062 | lang_name_java = get_identifier ("Java"); |
8d08fdba | 6063 | |
99dccabc MM |
6064 | /* Let the back-end now how to save and restore language-specific |
6065 | per-function globals. */ | |
a8f73d4b MM |
6066 | init_lang_status = &push_cp_function_context; |
6067 | free_lang_status = &pop_cp_function_context; | |
b4b8bee7 | 6068 | mark_lang_status = &mark_cp_function_context; |
99dccabc | 6069 | |
fc6af6e3 RH |
6070 | cp_parse_init (); |
6071 | init_decl2 (); | |
9cd64686 | 6072 | init_pt (); |
fc6af6e3 | 6073 | |
9cd64686 MM |
6074 | /* Create the global variables. */ |
6075 | push_to_top_level (); | |
8012c983 | 6076 | |
2c73f9f5 | 6077 | /* Enter the global namespace. */ |
30394414 | 6078 | my_friendly_assert (global_namespace == NULL_TREE, 375); |
30394414 JM |
6079 | push_namespace (get_identifier ("::")); |
6080 | global_namespace = current_namespace; | |
6081 | current_lang_name = NULL_TREE; | |
6082 | ||
bccd95ae | 6083 | /* Adjust various flags based on command-line settings. */ |
e1cd6e56 | 6084 | if (flag_strict_prototype == 2) |
830fcda8 | 6085 | flag_strict_prototype = pedantic; |
2642b9bf JM |
6086 | if (! flag_permissive && ! pedantic) |
6087 | flag_pedantic_errors = 1; | |
bccd95ae MM |
6088 | if (!flag_no_inline) |
6089 | flag_inline_trees = 1; | |
830fcda8 JM |
6090 | |
6091 | strict_prototypes_lang_c = flag_strict_prototype; | |
8926095f | 6092 | |
8d08fdba MS |
6093 | /* Initially, C. */ |
6094 | current_lang_name = lang_name_c; | |
6095 | ||
6096 | current_function_decl = NULL_TREE; | |
8d08fdba MS |
6097 | current_binding_level = NULL_BINDING_LEVEL; |
6098 | free_binding_level = NULL_BINDING_LEVEL; | |
6099 | ||
6100 | /* Because most segmentation signals can be traced back into user | |
6101 | code, catch them and at least give the user a chance of working | |
e92cc029 | 6102 | around compiler bugs. */ |
8d08fdba MS |
6103 | signal (SIGSEGV, signal_catch); |
6104 | ||
6105 | /* We will also catch aborts in the back-end through signal_catch and | |
6106 | give the user a chance to see where the error might be, and to defeat | |
6107 | aborts in the back-end when there have been errors previously in their | |
e92cc029 | 6108 | code. */ |
8d08fdba MS |
6109 | #ifdef SIGIOT |
6110 | signal (SIGIOT, signal_catch); | |
6111 | #endif | |
6112 | #ifdef SIGILL | |
6113 | signal (SIGILL, signal_catch); | |
6114 | #endif | |
6115 | #ifdef SIGABRT | |
6116 | signal (SIGABRT, signal_catch); | |
6117 | #endif | |
6118 | #ifdef SIGBUS | |
6119 | signal (SIGBUS, signal_catch); | |
6120 | #endif | |
6121 | ||
81b3411c BS |
6122 | build_common_tree_nodes (flag_signed_char); |
6123 | ||
8d08fdba MS |
6124 | error_mark_list = build_tree_list (error_mark_node, error_mark_node); |
6125 | TREE_TYPE (error_mark_list) = error_mark_node; | |
6126 | ||
a28e3c7f MS |
6127 | /* Make the binding_level structure for global names. */ |
6128 | pushlevel (0); | |
8d08fdba | 6129 | global_binding_level = current_binding_level; |
2c73f9f5 ML |
6130 | /* The global level is the namespace level of ::. */ |
6131 | NAMESPACE_LEVEL (global_namespace) = global_binding_level; | |
6132 | declare_namespace_level (); | |
8d08fdba MS |
6133 | |
6134 | this_identifier = get_identifier (THIS_NAME); | |
6135 | in_charge_identifier = get_identifier (IN_CHARGE_NAME); | |
fc378698 MS |
6136 | ctor_identifier = get_identifier (CTOR_NAME); |
6137 | dtor_identifier = get_identifier (DTOR_NAME); | |
8d08fdba MS |
6138 | pfn_identifier = get_identifier (VTABLE_PFN_NAME); |
6139 | index_identifier = get_identifier (VTABLE_INDEX_NAME); | |
6140 | delta_identifier = get_identifier (VTABLE_DELTA_NAME); | |
6141 | delta2_identifier = get_identifier (VTABLE_DELTA2_NAME); | |
6142 | pfn_or_delta2_identifier = get_identifier ("__pfn_or_delta2"); | |
6143 | ||
6144 | /* Define `int' and `char' first so that dbx will output them first. */ | |
8d08fdba | 6145 | record_builtin_type (RID_INT, NULL_PTR, integer_type_node); |
8d08fdba MS |
6146 | record_builtin_type (RID_CHAR, "char", char_type_node); |
6147 | ||
45075bf3 NS |
6148 | /* `signed' is the same as `int' */ |
6149 | record_builtin_type (RID_SIGNED, NULL_PTR, integer_type_node); | |
8d08fdba | 6150 | record_builtin_type (RID_LONG, "long int", long_integer_type_node); |
8d08fdba | 6151 | record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node); |
8d08fdba MS |
6152 | record_builtin_type (RID_MAX, "long unsigned int", long_unsigned_type_node); |
6153 | record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node); | |
8d08fdba | 6154 | record_builtin_type (RID_MAX, "long long int", long_long_integer_type_node); |
8d08fdba MS |
6155 | record_builtin_type (RID_MAX, "long long unsigned int", |
6156 | long_long_unsigned_type_node); | |
6157 | record_builtin_type (RID_MAX, "long long unsigned", | |
6158 | long_long_unsigned_type_node); | |
5156628f | 6159 | record_builtin_type (RID_SHORT, "short int", short_integer_type_node); |
5156628f MS |
6160 | record_builtin_type (RID_MAX, "short unsigned int", short_unsigned_type_node); |
6161 | record_builtin_type (RID_MAX, "unsigned short", short_unsigned_type_node); | |
6162 | ||
8d08fdba MS |
6163 | ptrdiff_type_node |
6164 | = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (PTRDIFF_TYPE))); | |
6165 | ||
8d08fdba | 6166 | /* Define both `signed char' and `unsigned char'. */ |
8d08fdba | 6167 | record_builtin_type (RID_MAX, "signed char", signed_char_type_node); |
8d08fdba MS |
6168 | record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node); |
6169 | ||
81b3411c BS |
6170 | /* `unsigned long' is the standard type for sizeof. |
6171 | Note that stddef.h uses `unsigned long', | |
6172 | and this must agree, even if long and int are the same size. */ | |
6173 | set_sizetype | |
6174 | (TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (SIZE_TYPE)))); | |
6175 | ||
835f9b4d GRK |
6176 | /* Create the widest literal types. */ |
6177 | widest_integer_literal_type_node = make_signed_type (HOST_BITS_PER_WIDE_INT * 2); | |
68642fb6 | 6178 | pushdecl (build_decl (TYPE_DECL, NULL_TREE, |
835f9b4d GRK |
6179 | widest_integer_literal_type_node)); |
6180 | ||
6181 | widest_unsigned_literal_type_node = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2); | |
68642fb6 | 6182 | pushdecl (build_decl (TYPE_DECL, NULL_TREE, |
835f9b4d GRK |
6183 | widest_unsigned_literal_type_node)); |
6184 | ||
8d08fdba | 6185 | /* These are types that type_for_size and type_for_mode use. */ |
8d08fdba | 6186 | pushdecl (build_decl (TYPE_DECL, NULL_TREE, intQI_type_node)); |
8d08fdba | 6187 | pushdecl (build_decl (TYPE_DECL, NULL_TREE, intHI_type_node)); |
8d08fdba | 6188 | pushdecl (build_decl (TYPE_DECL, NULL_TREE, intSI_type_node)); |
8d08fdba | 6189 | pushdecl (build_decl (TYPE_DECL, NULL_TREE, intDI_type_node)); |
946dc1c8 | 6190 | #if HOST_BITS_PER_WIDE_INT >= 64 |
5ebcdddb | 6191 | pushdecl (build_decl (TYPE_DECL, get_identifier ("__int128_t"), intTI_type_node)); |
946dc1c8 | 6192 | #endif |
8d08fdba | 6193 | pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intQI_type_node)); |
8d08fdba | 6194 | pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intHI_type_node)); |
8d08fdba | 6195 | pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intSI_type_node)); |
8d08fdba | 6196 | pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intDI_type_node)); |
946dc1c8 | 6197 | #if HOST_BITS_PER_WIDE_INT >= 64 |
5ebcdddb | 6198 | pushdecl (build_decl (TYPE_DECL, get_identifier ("__uint128_t"), unsigned_intTI_type_node)); |
946dc1c8 | 6199 | #endif |
8d08fdba | 6200 | |
81b3411c | 6201 | build_common_tree_nodes_2 (flag_short_double); |
37c46b43 | 6202 | |
eff71ab0 PB |
6203 | java_byte_type_node = record_builtin_java_type ("__java_byte", 8); |
6204 | java_short_type_node = record_builtin_java_type ("__java_short", 16); | |
6205 | java_int_type_node = record_builtin_java_type ("__java_int", 32); | |
6206 | java_long_type_node = record_builtin_java_type ("__java_long", 64); | |
6207 | java_float_type_node = record_builtin_java_type ("__java_float", -32); | |
6208 | java_double_type_node = record_builtin_java_type ("__java_double", -64); | |
6209 | java_char_type_node = record_builtin_java_type ("__java_char", -16); | |
6210 | java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1); | |
6211 | ||
8d08fdba MS |
6212 | integer_two_node = build_int_2 (2, 0); |
6213 | TREE_TYPE (integer_two_node) = integer_type_node; | |
6214 | integer_three_node = build_int_2 (3, 0); | |
6215 | TREE_TYPE (integer_three_node) = integer_type_node; | |
8d08fdba | 6216 | |
255512c1 JM |
6217 | boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE); |
6218 | TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE); | |
c1ea6a0b BK |
6219 | TYPE_MAX_VALUE (boolean_type_node) = build_int_2 (1, 0); |
6220 | TREE_TYPE (TYPE_MAX_VALUE (boolean_type_node)) = boolean_type_node; | |
6221 | TYPE_PRECISION (boolean_type_node) = 1; | |
255512c1 JM |
6222 | record_builtin_type (RID_BOOL, "bool", boolean_type_node); |
6223 | boolean_false_node = build_int_2 (0, 0); | |
6224 | TREE_TYPE (boolean_false_node) = boolean_type_node; | |
6225 | boolean_true_node = build_int_2 (1, 0); | |
6226 | TREE_TYPE (boolean_true_node) = boolean_type_node; | |
2986ae00 | 6227 | |
ff0bee63 JM |
6228 | signed_size_zero_node = build_int_2 (0, 0); |
6229 | record_builtin_type (RID_FLOAT, NULL_PTR, float_type_node); | |
81b3411c BS |
6230 | record_builtin_type (RID_DOUBLE, NULL_PTR, double_type_node); |
6231 | record_builtin_type (RID_MAX, "long double", long_double_type_node); | |
6232 | ||
6233 | pushdecl (build_decl (TYPE_DECL, get_identifier ("complex int"), | |
6234 | complex_integer_type_node)); | |
6235 | pushdecl (build_decl (TYPE_DECL, get_identifier ("complex float"), | |
6236 | complex_float_type_node)); | |
6237 | pushdecl (build_decl (TYPE_DECL, get_identifier ("complex double"), | |
6238 | complex_double_type_node)); | |
6239 | pushdecl (build_decl (TYPE_DECL, get_identifier ("complex long double"), | |
6240 | complex_long_double_type_node)); | |
8d08fdba | 6241 | |
e92cc029 MS |
6242 | TREE_TYPE (signed_size_zero_node) = make_signed_type (TYPE_PRECISION (sizetype)); |
6243 | ||
8d08fdba | 6244 | record_builtin_type (RID_VOID, NULL_PTR, void_type_node); |
8d08fdba MS |
6245 | void_list_node = build_tree_list (NULL_TREE, void_type_node); |
6246 | TREE_PARMLIST (void_list_node) = 1; | |
6247 | ||
8d08fdba MS |
6248 | /* Used for expressions that do nothing, but are not errors. */ |
6249 | void_zero_node = build_int_2 (0, 0); | |
6250 | TREE_TYPE (void_zero_node) = void_type_node; | |
6251 | ||
6252 | string_type_node = build_pointer_type (char_type_node); | |
beb53fb8 | 6253 | const_string_type_node |
68642fb6 | 6254 | = build_pointer_type (build_qualified_type (char_type_node, |
91063b51 | 6255 | TYPE_QUAL_CONST)); |
4cc1d462 | 6256 | empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE); |
6b5fbb55 | 6257 | #if 0 |
8d08fdba | 6258 | record_builtin_type (RID_MAX, NULL_PTR, string_type_node); |
6b5fbb55 | 6259 | #endif |
8d08fdba MS |
6260 | |
6261 | /* Make a type to be the domain of a few array types | |
6262 | whose domains don't really matter. | |
2ce07e2d | 6263 | 200 is small enough that it always fits in size_t. */ |
8d08fdba MS |
6264 | array_domain_type = build_index_type (build_int_2 (200, 0)); |
6265 | ||
2c73f9f5 | 6266 | /* Make a type for arrays of characters. |
8d08fdba MS |
6267 | With luck nothing will ever really depend on the length of this |
6268 | array type. */ | |
6269 | char_array_type_node | |
6270 | = build_array_type (char_type_node, array_domain_type); | |
6271 | /* Likewise for arrays of ints. */ | |
6272 | int_array_type_node | |
6273 | = build_array_type (integer_type_node, array_domain_type); | |
6274 | ||
c7e266a6 MM |
6275 | if (flag_new_abi) |
6276 | delta_type_node = ptrdiff_type_node; | |
6277 | else if (flag_huge_objects) | |
7f4edbcb BS |
6278 | delta_type_node = long_integer_type_node; |
6279 | else | |
6280 | delta_type_node = short_integer_type_node; | |
6281 | ||
c7e266a6 MM |
6282 | if (flag_new_abi) |
6283 | vtable_index_type = ptrdiff_type_node; | |
6284 | else | |
6285 | vtable_index_type = delta_type_node; | |
6286 | ||
8d08fdba MS |
6287 | default_function_type |
6288 | = build_function_type (integer_type_node, NULL_TREE); | |
8d08fdba MS |
6289 | |
6290 | ptr_type_node = build_pointer_type (void_type_node); | |
beb53fb8 | 6291 | const_ptr_type_node |
91063b51 | 6292 | = build_pointer_type (build_qualified_type (void_type_node, |
7f4edbcb BS |
6293 | TYPE_QUAL_CONST)); |
6294 | c_common_nodes_and_builtins (1, flag_no_builtin, flag_no_nonansi_builtin); | |
356955cf | 6295 | lang_type_promotes_to = convert_type_from_ellipsis; |
7f4edbcb | 6296 | |
824b9a4c | 6297 | void_ftype_ptr |
4cc1d462 | 6298 | = build_exception_variant (void_ftype_ptr, empty_except_spec); |
8d08fdba | 6299 | |
8d08fdba MS |
6300 | /* C++ extensions */ |
6301 | ||
6302 | unknown_type_node = make_node (UNKNOWN_TYPE); | |
036407f7 ML |
6303 | record_unknown_type (unknown_type_node, "unknown type"); |
6304 | ||
8d08fdba MS |
6305 | /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */ |
6306 | TREE_TYPE (unknown_type_node) = unknown_type_node; | |
a6967cc0 | 6307 | |
03d0f4af | 6308 | TREE_TYPE (null_node) = type_for_size (POINTER_SIZE, 0); |
a6967cc0 JM |
6309 | |
6310 | /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same | |
6311 | result. */ | |
8d08fdba MS |
6312 | TYPE_POINTER_TO (unknown_type_node) = unknown_type_node; |
6313 | TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node; | |
6314 | ||
e92cc029 | 6315 | /* This is special for C++ so functions can be overloaded. */ |
5096c664 JM |
6316 | wchar_type_node = get_identifier (flag_short_wchar |
6317 | ? "short unsigned int" | |
6318 | : WCHAR_TYPE); | |
6319 | wchar_type_node = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (wchar_type_node)); | |
8d08fdba MS |
6320 | wchar_type_size = TYPE_PRECISION (wchar_type_node); |
6321 | signed_wchar_type_node = make_signed_type (wchar_type_size); | |
6322 | unsigned_wchar_type_node = make_unsigned_type (wchar_type_size); | |
6323 | wchar_type_node | |
6324 | = TREE_UNSIGNED (wchar_type_node) | |
6325 | ? unsigned_wchar_type_node | |
6326 | : signed_wchar_type_node; | |
6327 | record_builtin_type (RID_WCHAR, "__wchar_t", wchar_type_node); | |
6328 | ||
f376e137 MS |
6329 | /* Artificial declaration of wchar_t -- can be bashed */ |
6330 | wchar_decl_node = build_decl (TYPE_DECL, get_identifier ("wchar_t"), | |
6331 | wchar_type_node); | |
6332 | pushdecl (wchar_decl_node); | |
6333 | ||
8d08fdba MS |
6334 | /* This is for wide string constants. */ |
6335 | wchar_array_type_node | |
6336 | = build_array_type (wchar_type_node, array_domain_type); | |
6337 | ||
8926095f | 6338 | if (flag_vtable_thunks) |
700f8a87 MS |
6339 | { |
6340 | /* Make sure we get a unique function type, so we can give | |
6341 | its pointer type a name. (This wins for gdb.) */ | |
6342 | tree vfunc_type = make_node (FUNCTION_TYPE); | |
6343 | TREE_TYPE (vfunc_type) = integer_type_node; | |
6344 | TYPE_ARG_TYPES (vfunc_type) = NULL_TREE; | |
6345 | layout_type (vfunc_type); | |
6346 | ||
6347 | vtable_entry_type = build_pointer_type (vfunc_type); | |
6348 | } | |
8926095f | 6349 | else |
700f8a87 | 6350 | { |
33848bb0 | 6351 | vtable_entry_type = make_aggr_type (RECORD_TYPE); |
4ce3d537 MM |
6352 | fields[0] = build_lang_decl (FIELD_DECL, delta_identifier, |
6353 | delta_type_node); | |
6354 | fields[1] = build_lang_decl (FIELD_DECL, index_identifier, | |
6355 | delta_type_node); | |
6356 | fields[2] = build_lang_decl (FIELD_DECL, pfn_identifier, | |
6357 | ptr_type_node); | |
700f8a87 MS |
6358 | finish_builtin_type (vtable_entry_type, VTBL_PTR_TYPE, fields, 2, |
6359 | double_type_node); | |
6360 | ||
6361 | /* Make this part of an invisible union. */ | |
6362 | fields[3] = copy_node (fields[2]); | |
6363 | TREE_TYPE (fields[3]) = delta_type_node; | |
6364 | DECL_NAME (fields[3]) = delta2_identifier; | |
6365 | DECL_MODE (fields[3]) = TYPE_MODE (delta_type_node); | |
6366 | DECL_SIZE (fields[3]) = TYPE_SIZE (delta_type_node); | |
06ceef4e | 6367 | DECL_SIZE_UNIT (fields[3]) = TYPE_SIZE_UNIT (delta_type_node); |
700f8a87 MS |
6368 | TREE_UNSIGNED (fields[3]) = 0; |
6369 | TREE_CHAIN (fields[2]) = fields[3]; | |
91063b51 MM |
6370 | vtable_entry_type = build_qualified_type (vtable_entry_type, |
6371 | TYPE_QUAL_CONST); | |
700f8a87 MS |
6372 | } |
6373 | record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type); | |
8d08fdba | 6374 | |
8d08fdba | 6375 | vtbl_type_node |
52bf7d5d | 6376 | = build_cplus_array_type (vtable_entry_type, NULL_TREE); |
8d08fdba | 6377 | layout_type (vtbl_type_node); |
91063b51 | 6378 | vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST); |
8d08fdba | 6379 | record_builtin_type (RID_MAX, NULL_PTR, vtbl_type_node); |
849da744 MM |
6380 | vtbl_ptr_type_node = build_pointer_type (vtable_entry_type); |
6381 | layout_type (vtbl_ptr_type_node); | |
6382 | record_builtin_type (RID_MAX, NULL_PTR, vtbl_ptr_type_node); | |
8d08fdba | 6383 | |
68642fb6 | 6384 | std_node = build_decl (NAMESPACE_DECL, |
2c73f9f5 | 6385 | get_identifier (flag_honor_std ? "fake std":"std"), |
6633d636 MS |
6386 | void_type_node); |
6387 | pushdecl (std_node); | |
6388 | ||
2c73f9f5 | 6389 | global_type_node = make_node (LANG_TYPE); |
036407f7 | 6390 | record_unknown_type (global_type_node, "global type"); |
2c73f9f5 | 6391 | |
db5ae43f MS |
6392 | /* Now, C++. */ |
6393 | current_lang_name = lang_name_cplusplus; | |
8d08fdba | 6394 | |
ced78d8b | 6395 | { |
2c73f9f5 ML |
6396 | tree bad_alloc_type_node, newtype, deltype; |
6397 | if (flag_honor_std) | |
6398 | push_namespace (get_identifier ("std")); | |
6399 | bad_alloc_type_node = xref_tag | |
ca107ded | 6400 | (class_type_node, get_identifier ("bad_alloc"), 1); |
2c73f9f5 ML |
6401 | if (flag_honor_std) |
6402 | pop_namespace (); | |
6403 | newtype = build_exception_variant | |
4cc1d462 NS |
6404 | (ptr_ftype_sizetype, add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1)); |
6405 | deltype = build_exception_variant (void_ftype_ptr, empty_except_spec); | |
0c11ada6 JM |
6406 | push_cp_library_fn (ansi_opname[(int) NEW_EXPR], newtype); |
6407 | push_cp_library_fn (ansi_opname[(int) VEC_NEW_EXPR], newtype); | |
6408 | global_delete_fndecl = push_cp_library_fn (ansi_opname[(int) DELETE_EXPR], | |
6409 | deltype); | |
6410 | push_cp_library_fn (ansi_opname[(int) VEC_DELETE_EXPR], deltype); | |
ced78d8b | 6411 | } |
8d08fdba MS |
6412 | |
6413 | abort_fndecl | |
0c11ada6 | 6414 | = build_library_fn_ptr ("__pure_virtual", void_ftype); |
8d08fdba | 6415 | |
8d08fdba MS |
6416 | /* Perform other language dependent initializations. */ |
6417 | init_class_processing (); | |
6418 | init_init_processing (); | |
6419 | init_search_processing (); | |
db48b831 | 6420 | init_rtti_processing (); |
8d08fdba | 6421 | |
6467930b | 6422 | if (flag_exceptions) |
8d2733ca | 6423 | init_exception_processing (); |
8d08fdba MS |
6424 | if (flag_no_inline) |
6425 | { | |
6426 | flag_inline_functions = 0; | |
8d08fdba | 6427 | } |
9e9ff709 | 6428 | |
7fcdf4c2 | 6429 | if (! supports_one_only ()) |
72b7eeff | 6430 | flag_weak = 0; |
8d08fdba MS |
6431 | |
6432 | /* Create the global bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */ | |
2ce07e2d | 6433 | make_fname_decl = cp_make_fname_decl; |
8d08fdba MS |
6434 | declare_function_name (); |
6435 | ||
6436 | /* Prepare to check format strings against argument lists. */ | |
6437 | init_function_format_info (); | |
e9a25f70 JL |
6438 | |
6439 | /* Show we use EH for cleanups. */ | |
6440 | using_eh_for_cleanups (); | |
62c154ed JM |
6441 | |
6442 | print_error_function = lang_print_error_function; | |
501ba25a | 6443 | lang_get_alias_set = &c_get_alias_set; |
e5dc5fb2 | 6444 | valid_lang_attribute = cp_valid_lang_attribute; |
d9cf7c82 JM |
6445 | |
6446 | /* Maintain consistency. Perhaps we should just complain if they | |
6447 | say -fwritable-strings? */ | |
6448 | if (flag_writable_strings) | |
6449 | flag_const_strings = 0; | |
fc6af6e3 RH |
6450 | |
6451 | /* Add GC roots for all of our global variables. */ | |
6452 | ggc_add_tree_root (c_global_trees, sizeof c_global_trees / sizeof(tree)); | |
6453 | ggc_add_tree_root (cp_global_trees, sizeof cp_global_trees / sizeof(tree)); | |
fc6af6e3 RH |
6454 | ggc_add_tree_root (&integer_three_node, 1); |
6455 | ggc_add_tree_root (&integer_two_node, 1); | |
fc6af6e3 | 6456 | ggc_add_tree_root (&signed_size_zero_node, 1); |
fc6af6e3 RH |
6457 | ggc_add_tree_root (&size_one_node, 1); |
6458 | ggc_add_tree_root (&size_zero_node, 1); | |
fc6af6e3 RH |
6459 | ggc_add_root (&global_binding_level, 1, sizeof global_binding_level, |
6460 | mark_binding_level); | |
9cd64686 | 6461 | ggc_add_root (&scope_chain, 1, sizeof scope_chain, &mark_saved_scope); |
fc6af6e3 RH |
6462 | ggc_add_tree_root (&static_ctors, 1); |
6463 | ggc_add_tree_root (&static_dtors, 1); | |
9cd64686 | 6464 | ggc_add_tree_root (&lastiddecl, 1); |
fc6af6e3 | 6465 | |
fc6af6e3 RH |
6466 | ggc_add_tree_root (&last_function_parm_tags, 1); |
6467 | ggc_add_tree_root (¤t_function_return_value, 1); | |
6468 | ggc_add_tree_root (¤t_function_parms, 1); | |
6469 | ggc_add_tree_root (¤t_function_parm_tags, 1); | |
9cd64686 | 6470 | ggc_add_tree_root (&last_function_parms, 1); |
fc6af6e3 | 6471 | ggc_add_tree_root (&error_mark_list, 1); |
9cd64686 | 6472 | |
fc6af6e3 | 6473 | ggc_add_tree_root (&global_namespace, 1); |
fc6af6e3 RH |
6474 | ggc_add_tree_root (&global_type_node, 1); |
6475 | ggc_add_tree_root (&anonymous_namespace_name, 1); | |
9cd64686 MM |
6476 | |
6477 | ggc_add_tree_root (&got_object, 1); | |
6478 | ggc_add_tree_root (&got_scope, 1); | |
6479 | ||
6480 | ggc_add_tree_root (¤t_lang_name, 1); | |
6481 | ggc_add_tree_root (&static_aggregates, 1); | |
62c154ed JM |
6482 | } |
6483 | ||
2ce07e2d NS |
6484 | /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the |
6485 | decl, NAME is the initialization string and TYPE_DEP indicates whether | |
6486 | NAME depended on the type of the function. We make use of that to detect | |
6487 | __PRETTY_FUNCTION__ inside a template fn. Because we build a tree for | |
6488 | the function before emitting any of it, we don't need to treat the | |
6489 | VAR_DECL specially. We can decide whether to emit it later, if it was | |
6490 | used. */ | |
6491 | ||
6492 | static tree | |
6493 | cp_make_fname_decl (id, name, type_dep) | |
6494 | tree id; | |
6495 | const char *name; | |
6496 | int type_dep; | |
6497 | { | |
6498 | tree decl, type, init; | |
6499 | size_t length = strlen (name); | |
6500 | tree domain = NULL_TREE; | |
6501 | ||
6502 | if (!processing_template_decl) | |
6503 | type_dep = 0; | |
6504 | if (!type_dep) | |
6505 | domain = build_index_type (build_int_2 (length, 0)); | |
6506 | ||
6507 | type = build_cplus_array_type | |
6508 | (build_qualified_type (char_type_node, TYPE_QUAL_CONST), | |
6509 | domain); | |
6510 | ||
6511 | decl = build_lang_decl (VAR_DECL, id, type); | |
6512 | TREE_STATIC (decl) = 1; | |
6513 | TREE_READONLY (decl) = 1; | |
6514 | DECL_SOURCE_LINE (decl) = 0; | |
6515 | DECL_ARTIFICIAL (decl) = 1; | |
6516 | DECL_IN_SYSTEM_HEADER (decl) = 1; | |
6517 | pushdecl (decl); | |
6518 | if (processing_template_decl) | |
6519 | decl = push_template_decl (decl); | |
6520 | if (type_dep) | |
6521 | { | |
6522 | init = build (FUNCTION_NAME, type); | |
6523 | DECL_PRETTY_FUNCTION_P (decl) = 1; | |
6524 | } | |
6525 | else | |
6526 | { | |
6527 | init = build_string (length + 1, name); | |
6528 | TREE_TYPE (init) = type; | |
6529 | } | |
6530 | DECL_INITIAL (decl) = init; | |
6531 | cp_finish_decl (decl, init, NULL_TREE, LOOKUP_ONLYCONVERTING); | |
6532 | ||
6533 | /* We will have to make sure we only emit this, if it is actually used. */ | |
6534 | return decl; | |
6535 | } | |
6536 | ||
62c154ed JM |
6537 | /* Function to print any language-specific context for an error message. */ |
6538 | ||
6539 | static void | |
6540 | lang_print_error_function (file) | |
8df4696d | 6541 | const char *file; |
62c154ed JM |
6542 | { |
6543 | default_print_error_function (file); | |
6544 | maybe_print_template_context (); | |
8d08fdba MS |
6545 | } |
6546 | ||
0c11ada6 JM |
6547 | /* Entry point for the benefit of c_common_nodes_and_builtins. |
6548 | ||
6549 | Make a definition for a builtin function named NAME and whose data type | |
8d08fdba | 6550 | is TYPE. TYPE should be a function type with argument types. |
8d08fdba | 6551 | |
0c11ada6 JM |
6552 | CLASS and CODE tell later passes how to compile calls to this function. |
6553 | See tree.h for possible values. | |
6554 | ||
6555 | If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME, | |
8d08fdba MS |
6556 | the name to be called if we can't opencode the function. */ |
6557 | ||
6558 | tree | |
0c11ada6 | 6559 | builtin_function (name, type, code, class, libname) |
d8e178a0 | 6560 | const char *name; |
8d08fdba | 6561 | tree type; |
0c11ada6 JM |
6562 | int code; |
6563 | enum built_in_class class; | |
6564 | const char *libname; | |
8d08fdba | 6565 | { |
0c11ada6 JM |
6566 | tree decl = build_library_fn_1 (get_identifier (name), type); |
6567 | DECL_BUILT_IN_CLASS (decl) = class; | |
6568 | DECL_FUNCTION_CODE (decl) = code; | |
12a22e76 | 6569 | |
2c73f9f5 | 6570 | my_friendly_assert (DECL_CONTEXT (decl) == NULL_TREE, 392); |
2c73f9f5 | 6571 | |
8d08fdba MS |
6572 | /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME, |
6573 | we cannot change DECL_ASSEMBLER_NAME until we have installed this | |
6574 | function in the namespace. */ | |
0c11ada6 JM |
6575 | pushdecl (decl); |
6576 | if (libname) | |
6577 | DECL_ASSEMBLER_NAME (decl) = get_identifier (libname); | |
8d08fdba | 6578 | make_function_rtl (decl); |
8d08fdba MS |
6579 | return decl; |
6580 | } | |
7f4edbcb | 6581 | |
0c11ada6 JM |
6582 | /* Generate a FUNCTION_DECL with the typical flags for a runtime library |
6583 | function. Not called directly. */ | |
6584 | ||
6585 | static tree | |
6586 | build_library_fn_1 (name, type) | |
6587 | tree name; | |
6588 | tree type; | |
6589 | { | |
6590 | tree fn = build_lang_decl (FUNCTION_DECL, name, type); | |
6591 | DECL_EXTERNAL (fn) = 1; | |
6592 | TREE_PUBLIC (fn) = 1; | |
6593 | DECL_ARTIFICIAL (fn) = 1; | |
6594 | TREE_NOTHROW (fn) = 1; | |
6595 | return fn; | |
6596 | } | |
c2a37c55 | 6597 | |
0c11ada6 JM |
6598 | /* Returns the _DECL for a library function with C linkage. |
6599 | We assume that such functions never throw; if this is incorrect, | |
6600 | callers should unset TREE_NOTHROW. */ | |
c2a37c55 | 6601 | |
7f4edbcb | 6602 | tree |
0c11ada6 JM |
6603 | build_library_fn (name, type) |
6604 | tree name; | |
6605 | tree type; | |
6606 | { | |
6607 | tree fn = build_library_fn_1 (name, type); | |
6608 | make_function_rtl (fn); | |
6609 | return fn; | |
6610 | } | |
6611 | ||
6612 | /* Returns the _DECL for a library function with C++ linkage. */ | |
6613 | ||
6614 | tree | |
6615 | build_cp_library_fn (name, type) | |
6616 | tree name; | |
6617 | tree type; | |
6618 | { | |
6619 | tree fn = build_library_fn_1 (name, type); | |
6620 | TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type); | |
6621 | set_mangled_name_for_decl (fn); | |
6622 | make_function_rtl (fn); | |
6623 | return fn; | |
6624 | } | |
6625 | ||
6626 | /* Like build_library_fn, but takes a C string instead of an | |
6627 | IDENTIFIER_NODE. */ | |
6628 | ||
6629 | tree | |
6630 | build_library_fn_ptr (name, type) | |
7f4edbcb BS |
6631 | const char *name; |
6632 | tree type; | |
7f4edbcb | 6633 | { |
0c11ada6 JM |
6634 | return build_library_fn (get_identifier (name), type); |
6635 | } | |
6636 | ||
6637 | /* Like build_cp_library_fn, but takes a C string instead of an | |
6638 | IDENTIFIER_NODE. */ | |
6639 | ||
6640 | tree | |
6641 | build_cp_library_fn_ptr (name, type) | |
6642 | const char *name; | |
6643 | tree type; | |
6644 | { | |
6645 | return build_cp_library_fn (get_identifier (name), type); | |
6646 | } | |
6647 | ||
6648 | /* Like build_library_fn, but also pushes the function so that we will | |
6649 | be able to find it via IDENTIFIER_GLOBAL_VALUE. */ | |
6650 | ||
6651 | tree | |
6652 | push_library_fn (name, type) | |
6653 | tree name, type; | |
6654 | { | |
6655 | tree fn = build_library_fn (name, type); | |
6656 | pushdecl_top_level (fn); | |
6657 | return fn; | |
6658 | } | |
6659 | ||
6660 | /* Like build_cp_library_fn, but also pushes the function so that it | |
6661 | will be found by normal lookup. */ | |
6662 | ||
6663 | tree | |
6664 | push_cp_library_fn (name, type) | |
6665 | tree name; | |
6666 | tree type; | |
6667 | { | |
6668 | tree fn = build_cp_library_fn (name, type); | |
6669 | pushdecl (fn); | |
6670 | return fn; | |
6671 | } | |
6672 | ||
6673 | /* Like push_library_fn, but takes a TREE_LIST of parm types rather than | |
6674 | a FUNCTION_TYPE. */ | |
6675 | ||
6676 | tree | |
6677 | push_void_library_fn (name, parmtypes) | |
6678 | tree name, parmtypes; | |
6679 | { | |
6680 | tree type = build_function_type (void_type_node, parmtypes); | |
6681 | return push_library_fn (name, type); | |
6682 | } | |
6683 | ||
cf74fb86 | 6684 | /* Like push_library_fn, but also note that this function throws |
0c11ada6 JM |
6685 | and does not return. Used for __throw_foo and the like. */ |
6686 | ||
6687 | tree | |
cf74fb86 JM |
6688 | push_throw_library_fn (name, type) |
6689 | tree name, type; | |
0c11ada6 | 6690 | { |
cf74fb86 | 6691 | tree fn = push_library_fn (name, type); |
0c11ada6 JM |
6692 | TREE_THIS_VOLATILE (fn) = 1; |
6693 | TREE_NOTHROW (fn) = 0; | |
6694 | return fn; | |
7f4edbcb | 6695 | } |
8d08fdba | 6696 | \f |
61a127b3 MM |
6697 | /* When we call finish_struct for an anonymous union, we create |
6698 | default copy constructors and such. But, an anonymous union | |
6699 | shouldn't have such things; this function undoes the damage to the | |
6700 | anonymous union type T. | |
6701 | ||
6702 | (The reason that we create the synthesized methods is that we don't | |
6703 | distinguish `union { int i; }' from `typedef union { int i; } U'. | |
6704 | The first is an anonymous union; the second is just an ordinary | |
6705 | union type.) */ | |
6706 | ||
6707 | void | |
6bdb8141 | 6708 | fixup_anonymous_aggr (t) |
61a127b3 MM |
6709 | tree t; |
6710 | { | |
6711 | tree *q; | |
6712 | ||
6713 | /* Wipe out memory of synthesized methods */ | |
6714 | TYPE_HAS_CONSTRUCTOR (t) = 0; | |
6715 | TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0; | |
6716 | TYPE_HAS_INIT_REF (t) = 0; | |
6717 | TYPE_HAS_CONST_INIT_REF (t) = 0; | |
6718 | TYPE_HAS_ASSIGN_REF (t) = 0; | |
61a127b3 MM |
6719 | TYPE_HAS_CONST_ASSIGN_REF (t) = 0; |
6720 | ||
6721 | /* Splice the implicitly generated functions out of the TYPE_METHODS | |
6722 | list. */ | |
6723 | q = &TYPE_METHODS (t); | |
6724 | while (*q) | |
6725 | { | |
6726 | if (DECL_ARTIFICIAL (*q)) | |
6727 | *q = TREE_CHAIN (*q); | |
6728 | else | |
6729 | q = &TREE_CHAIN (*q); | |
6730 | } | |
6731 | ||
cab1f180 | 6732 | /* ISO C++ 9.5.3. Anonymous unions may not have function members. */ |
61a127b3 MM |
6733 | if (TYPE_METHODS (t)) |
6734 | error ("an anonymous union cannot have function members"); | |
6735 | } | |
6736 | ||
72a93143 JM |
6737 | /* Make sure that a declaration with no declarator is well-formed, i.e. |
6738 | just defines a tagged type or anonymous union. | |
8d08fdba | 6739 | |
72a93143 | 6740 | Returns the type defined, if any. */ |
8d08fdba | 6741 | |
72a93143 JM |
6742 | tree |
6743 | check_tag_decl (declspecs) | |
8d08fdba MS |
6744 | tree declspecs; |
6745 | { | |
72a93143 | 6746 | int found_type = 0; |
2986ae00 | 6747 | tree ob_modifier = NULL_TREE; |
8d08fdba | 6748 | register tree link; |
8d08fdba MS |
6749 | register tree t = NULL_TREE; |
6750 | ||
6751 | for (link = declspecs; link; link = TREE_CHAIN (link)) | |
6752 | { | |
6753 | register tree value = TREE_VALUE (link); | |
6754 | ||
72a93143 | 6755 | if (TYPE_P (value)) |
8d08fdba | 6756 | { |
72a93143 | 6757 | ++found_type; |
5566b478 | 6758 | |
72a93143 JM |
6759 | if (IS_AGGR_TYPE (value) || TREE_CODE (value) == ENUMERAL_TYPE) |
6760 | { | |
6761 | my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261); | |
6762 | t = value; | |
6763 | } | |
8d08fdba | 6764 | } |
83f660b7 JM |
6765 | else if (value == ridpointers[(int) RID_FRIEND]) |
6766 | { | |
83f660b7 JM |
6767 | if (current_class_type == NULL_TREE |
6768 | || current_scope () != current_class_type) | |
6769 | ob_modifier = value; | |
6770 | } | |
8d08fdba | 6771 | else if (value == ridpointers[(int) RID_STATIC] |
2986ae00 MS |
6772 | || value == ridpointers[(int) RID_EXTERN] |
6773 | || value == ridpointers[(int) RID_AUTO] | |
28cbf42c MS |
6774 | || value == ridpointers[(int) RID_REGISTER] |
6775 | || value == ridpointers[(int) RID_INLINE] | |
6776 | || value == ridpointers[(int) RID_VIRTUAL] | |
72a93143 JM |
6777 | || value == ridpointers[(int) RID_CONST] |
6778 | || value == ridpointers[(int) RID_VOLATILE] | |
28cbf42c | 6779 | || value == ridpointers[(int) RID_EXPLICIT]) |
2986ae00 | 6780 | ob_modifier = value; |
8d08fdba MS |
6781 | } |
6782 | ||
72a93143 JM |
6783 | if (found_type > 1) |
6784 | error ("multiple types in one declaration"); | |
7e2067ca JM |
6785 | |
6786 | /* Inside a class, we might be in a friend or access declaration. | |
6787 | Until we have a good way of detecting the latter, don't warn. */ | |
6788 | if (t == NULL_TREE && ! current_class_type) | |
6789 | pedwarn ("declaration does not declare anything"); | |
0dd3962d JM |
6790 | |
6791 | /* Check for an anonymous union. We're careful | |
6792 | accessing TYPE_IDENTIFIER because some built-in types, like | |
6793 | pointer-to-member types, do not have TYPE_NAME. */ | |
6bdb8141 | 6794 | else if (t && IS_AGGR_TYPE_CODE (TREE_CODE (t)) |
0dd3962d JM |
6795 | && TYPE_NAME (t) |
6796 | && ANON_AGGRNAME_P (TYPE_IDENTIFIER (t))) | |
6797 | { | |
6798 | /* Anonymous unions are objects, so they can have specifiers. */; | |
6bdb8141 JM |
6799 | SET_ANON_AGGR_TYPE_P (t); |
6800 | ||
6801 | if (TREE_CODE (t) != UNION_TYPE && pedantic && ! in_system_header) | |
6802 | pedwarn ("ISO C++ prohibits anonymous structs"); | |
0dd3962d JM |
6803 | } |
6804 | ||
83f660b7 | 6805 | else if (ob_modifier) |
8d08fdba | 6806 | { |
83f660b7 JM |
6807 | if (ob_modifier == ridpointers[(int) RID_INLINE] |
6808 | || ob_modifier == ridpointers[(int) RID_VIRTUAL]) | |
6809 | cp_error ("`%D' can only be specified for functions", ob_modifier); | |
6810 | else if (ob_modifier == ridpointers[(int) RID_FRIEND]) | |
6811 | cp_error ("`%D' can only be specified inside a class", ob_modifier); | |
6812 | else if (ob_modifier == ridpointers[(int) RID_EXPLICIT]) | |
6813 | cp_error ("`%D' can only be specified for constructors", | |
6814 | ob_modifier); | |
6815 | else | |
6816 | cp_error ("`%D' can only be specified for objects and functions", | |
6817 | ob_modifier); | |
72a93143 | 6818 | } |
8d08fdba | 6819 | |
72a93143 JM |
6820 | return t; |
6821 | } | |
6822 | ||
6823 | /* Called when a declaration is seen that contains no names to declare. | |
6824 | If its type is a reference to a structure, union or enum inherited | |
6825 | from a containing scope, shadow that tag name for the current scope | |
6826 | with a forward reference. | |
6827 | If its type defines a new named structure or union | |
6828 | or defines an enum, it is valid but we need not do anything here. | |
6829 | Otherwise, it is an error. | |
6830 | ||
6831 | C++: may have to grok the declspecs to learn about static, | |
6832 | complain for anonymous unions. */ | |
6833 | ||
6834 | void | |
6835 | shadow_tag (declspecs) | |
6836 | tree declspecs; | |
6837 | { | |
6838 | tree t = check_tag_decl (declspecs); | |
6839 | ||
6840 | if (t) | |
6841 | maybe_process_partial_specialization (t); | |
6842 | ||
6843 | /* This is where the variables in an anonymous union are | |
6844 | declared. An anonymous union declaration looks like: | |
6845 | union { ... } ; | |
6846 | because there is no declarator after the union, the parser | |
6847 | sends that declaration here. */ | |
6bdb8141 | 6848 | if (t && ANON_AGGR_TYPE_P (t)) |
72a93143 | 6849 | { |
6bdb8141 | 6850 | fixup_anonymous_aggr (t); |
72a93143 JM |
6851 | |
6852 | if (TYPE_FIELDS (t)) | |
6853 | { | |
6854 | tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0, | |
6855 | NULL_TREE); | |
6856 | finish_anon_union (decl); | |
6857 | } | |
8d08fdba MS |
6858 | } |
6859 | } | |
6860 | \f | |
6861 | /* Decode a "typename", such as "int **", returning a ..._TYPE node. */ | |
6862 | ||
6863 | tree | |
6864 | groktypename (typename) | |
6865 | tree typename; | |
6866 | { | |
6867 | if (TREE_CODE (typename) != TREE_LIST) | |
6868 | return typename; | |
6869 | return grokdeclarator (TREE_VALUE (typename), | |
6870 | TREE_PURPOSE (typename), | |
c11b6f21 | 6871 | TYPENAME, 0, NULL_TREE); |
8d08fdba MS |
6872 | } |
6873 | ||
6874 | /* Decode a declarator in an ordinary declaration or data definition. | |
6875 | This is called as soon as the type information and variable name | |
6876 | have been parsed, before parsing the initializer if any. | |
6877 | Here we create the ..._DECL node, fill in its type, | |
6878 | and put it on the list of decls for the current context. | |
6879 | The ..._DECL node is returned as the value. | |
6880 | ||
6881 | Exception: for arrays where the length is not specified, | |
82580166 | 6882 | the type is left null, to be filled in by `cp_finish_decl'. |
8d08fdba MS |
6883 | |
6884 | Function definitions do not come here; they go to start_function | |
6885 | instead. However, external and forward declarations of functions | |
6886 | do go through here. Structure field declarations are done by | |
6887 | grokfield and not through here. */ | |
6888 | ||
6889 | /* Set this to zero to debug not using the temporary obstack | |
6890 | to parse initializers. */ | |
6891 | int debug_temp_inits = 1; | |
6892 | ||
6893 | tree | |
a1774733 | 6894 | start_decl (declarator, declspecs, initialized, attributes, prefix_attributes) |
8d08fdba MS |
6895 | tree declarator, declspecs; |
6896 | int initialized; | |
a1774733 | 6897 | tree attributes, prefix_attributes; |
8d08fdba MS |
6898 | { |
6899 | register tree decl; | |
6900 | register tree type, tem; | |
6901 | tree context; | |
6902 | extern int have_extern_spec; | |
6903 | extern int used_extern_spec; | |
b17e2870 | 6904 | tree attrlist; |
8d08fdba | 6905 | |
5566b478 MS |
6906 | #if 0 |
6907 | /* See code below that used this. */ | |
8d08fdba | 6908 | int init_written = initialized; |
5566b478 | 6909 | #endif |
8d08fdba | 6910 | |
e92cc029 | 6911 | /* This should only be done once on the top most decl. */ |
8d08fdba MS |
6912 | if (have_extern_spec && !used_extern_spec) |
6913 | { | |
a28e3c7f MS |
6914 | declspecs = decl_tree_cons (NULL_TREE, get_identifier ("extern"), |
6915 | declspecs); | |
8d08fdba MS |
6916 | used_extern_spec = 1; |
6917 | } | |
6918 | ||
b17e2870 | 6919 | if (attributes || prefix_attributes) |
051e6fd7 | 6920 | attrlist = build_tree_list (attributes, prefix_attributes); |
b17e2870 JM |
6921 | else |
6922 | attrlist = NULL_TREE; | |
6923 | ||
c11b6f21 | 6924 | decl = grokdeclarator (declarator, declspecs, NORMAL, initialized, |
b17e2870 | 6925 | attrlist); |
68642fb6 | 6926 | |
a1774733 | 6927 | if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE) |
8d08fdba MS |
6928 | return NULL_TREE; |
6929 | ||
6930 | type = TREE_TYPE (decl); | |
6931 | ||
44689c12 ML |
6932 | if (type == error_mark_node) |
6933 | return NULL_TREE; | |
6934 | ||
4f1c5b7d | 6935 | context = DECL_CONTEXT (decl); |
8d08fdba | 6936 | |
9a68c51f JM |
6937 | if (initialized && context && TREE_CODE (context) == NAMESPACE_DECL |
6938 | && context != current_namespace && TREE_CODE (decl) == VAR_DECL) | |
6939 | { | |
6940 | /* When parsing the initializer, lookup should use the object's | |
6941 | namespace. */ | |
6942 | push_decl_namespace (context); | |
6943 | } | |
6944 | ||
2c73f9f5 ML |
6945 | /* We are only interested in class contexts, later. */ |
6946 | if (context && TREE_CODE (context) == NAMESPACE_DECL) | |
6947 | context = NULL_TREE; | |
6948 | ||
8d08fdba MS |
6949 | if (initialized) |
6950 | /* Is it valid for this decl to have an initializer at all? | |
6951 | If not, set INITIALIZED to zero, which will indirectly | |
82580166 | 6952 | tell `cp_finish_decl' to ignore the initializer once it is parsed. */ |
8d08fdba MS |
6953 | switch (TREE_CODE (decl)) |
6954 | { | |
6955 | case TYPE_DECL: | |
6956 | /* typedef foo = bar means give foo the same type as bar. | |
82580166 | 6957 | We haven't parsed bar yet, so `cp_finish_decl' will fix that up. |
8d08fdba MS |
6958 | Any other case of an initialization in a TYPE_DECL is an error. */ |
6959 | if (pedantic || list_length (declspecs) > 1) | |
6960 | { | |
8251199e | 6961 | cp_error ("typedef `%D' is initialized", decl); |
8d08fdba MS |
6962 | initialized = 0; |
6963 | } | |
6964 | break; | |
6965 | ||
6966 | case FUNCTION_DECL: | |
8251199e | 6967 | cp_error ("function `%#D' is initialized like a variable", decl); |
8d08fdba MS |
6968 | initialized = 0; |
6969 | break; | |
6970 | ||
6971 | default: | |
3e41d13b | 6972 | break; |
8d08fdba MS |
6973 | } |
6974 | ||
8d08fdba MS |
6975 | if (initialized) |
6976 | { | |
a9aedbc2 | 6977 | if (! toplevel_bindings_p () |
8d08fdba | 6978 | && DECL_EXTERNAL (decl)) |
8251199e | 6979 | cp_warning ("declaration of `%#D' has `extern' and is initialized", |
8d08fdba MS |
6980 | decl); |
6981 | DECL_EXTERNAL (decl) = 0; | |
5566b478 | 6982 | if (toplevel_bindings_p ()) |
8d08fdba MS |
6983 | TREE_STATIC (decl) = 1; |
6984 | ||
6985 | /* Tell `pushdecl' this is an initialized decl | |
6986 | even though we don't yet have the initializer expression. | |
82580166 | 6987 | Also tell `cp_finish_decl' it may store the real initializer. */ |
8d08fdba MS |
6988 | DECL_INITIAL (decl) = error_mark_node; |
6989 | } | |
6990 | ||
fa20888b MK |
6991 | #ifdef SET_DEFAULT_DECL_ATTRIBUTES |
6992 | SET_DEFAULT_DECL_ATTRIBUTES (decl, attributes); | |
6993 | #endif | |
68642fb6 | 6994 | |
fa20888b MK |
6995 | /* Set attributes here so if duplicate decl, will have proper attributes. */ |
6996 | cplus_decl_attributes (decl, attributes, prefix_attributes); | |
6997 | ||
5566b478 | 6998 | if (context && TYPE_SIZE (complete_type (context)) != NULL_TREE) |
5b605f68 | 6999 | { |
6b400b21 | 7000 | push_nested_class (context, 2); |
e97e5263 | 7001 | |
5b605f68 MS |
7002 | if (TREE_CODE (decl) == VAR_DECL) |
7003 | { | |
7004 | tree field = lookup_field (context, DECL_NAME (decl), 0, 0); | |
7005 | if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL) | |
8251199e | 7006 | cp_error ("`%#D' is not a static member of `%#T'", decl, context); |
e349ee73 MS |
7007 | else |
7008 | { | |
7009 | if (DECL_CONTEXT (field) != context) | |
f2d773a2 | 7010 | { |
cb9a3ff8 | 7011 | cp_pedwarn ("ISO C++ does not permit `%T::%D' to be defined as `%T::%D'", |
f2d773a2 JM |
7012 | DECL_CONTEXT (field), DECL_NAME (decl), |
7013 | context, DECL_NAME (decl)); | |
7014 | DECL_CONTEXT (decl) = DECL_CONTEXT (field); | |
7015 | } | |
75650646 MM |
7016 | /* Static data member are tricky; an in-class initialization |
7017 | still doesn't provide a definition, so the in-class | |
7018 | declaration will have DECL_EXTERNAL set, but will have an | |
7019 | initialization. Thus, duplicate_decls won't warn | |
7020 | about this situation, and so we check here. */ | |
7021 | if (DECL_INITIAL (decl) && DECL_INITIAL (field)) | |
8251199e | 7022 | cp_error ("duplicate initialization of %D", decl); |
e349ee73 MS |
7023 | if (duplicate_decls (decl, field)) |
7024 | decl = field; | |
7025 | } | |
5b605f68 | 7026 | } |
f30432d7 MS |
7027 | else |
7028 | { | |
5566b478 | 7029 | tree field = check_classfn (context, decl); |
f30432d7 MS |
7030 | if (field && duplicate_decls (decl, field)) |
7031 | decl = field; | |
7032 | } | |
7033 | ||
7034 | /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */ | |
dff3e828 | 7035 | DECL_IN_AGGR_P (decl) = 0; |
68642fb6 | 7036 | if ((DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl)) |
dff3e828 | 7037 | || CLASSTYPE_USE_TEMPLATE (context)) |
84e6233f JM |
7038 | { |
7039 | SET_DECL_TEMPLATE_SPECIALIZATION (decl); | |
7040 | /* [temp.expl.spec] An explicit specialization of a static data | |
7041 | member of a template is a definition if the declaration | |
7042 | includes an initializer; otherwise, it is a declaration. | |
7043 | ||
7044 | We check for processing_specialization so this only applies | |
7045 | to the new specialization syntax. */ | |
7046 | if (DECL_INITIAL (decl) == NULL_TREE && processing_specialization) | |
7047 | DECL_EXTERNAL (decl) = 1; | |
7048 | } | |
f30432d7 | 7049 | |
b7698cf0 | 7050 | if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)) |
8251199e | 7051 | cp_pedwarn ("declaration of `%#D' outside of class is not definition", |
f30432d7 | 7052 | decl); |
5b605f68 MS |
7053 | } |
7054 | ||
9188c363 MM |
7055 | /* Enter this declaration into the symbol table. */ |
7056 | tem = maybe_push_decl (decl); | |
2ee887f2 | 7057 | |
5156628f | 7058 | if (processing_template_decl) |
cd9f6678 | 7059 | tem = push_template_decl (tem); |
5566b478 | 7060 | |
2ee887f2 | 7061 | #if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS) |
a3203465 | 7062 | /* Tell the back-end to use or not use .common as appropriate. If we say |
a50f0918 MS |
7063 | -fconserve-space, we want this to save .data space, at the expense of |
7064 | wrong semantics. If we say -fno-conserve-space, we want this to | |
7065 | produce errors about redefs; to do this we force variables into the | |
7066 | data segment. */ | |
a3203465 | 7067 | DECL_COMMON (tem) = flag_conserve_space || ! TREE_PUBLIC (tem); |
2ee887f2 | 7068 | #endif |
68642fb6 | 7069 | |
5156628f | 7070 | if (! processing_template_decl) |
5566b478 | 7071 | start_decl_1 (tem); |
8d08fdba | 7072 | |
8d08fdba MS |
7073 | return tem; |
7074 | } | |
7075 | ||
5566b478 MS |
7076 | void |
7077 | start_decl_1 (decl) | |
7078 | tree decl; | |
8d08fdba | 7079 | { |
5566b478 MS |
7080 | tree type = TREE_TYPE (decl); |
7081 | int initialized = (DECL_INITIAL (decl) != NULL_TREE); | |
8d08fdba | 7082 | |
44689c12 ML |
7083 | if (type == error_mark_node) |
7084 | return; | |
7085 | ||
8f471b0d MM |
7086 | /* If this type of object needs a cleanup, but we're not allowed to |
7087 | add any more objects with cleanups to the current scope, create a | |
7088 | new binding level. */ | |
834c6dff | 7089 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) |
5566b478 | 7090 | && current_binding_level->more_cleanups_ok == 0) |
8f471b0d MM |
7091 | { |
7092 | keep_next_level (2); | |
7093 | pushlevel (1); | |
7094 | clear_last_expr (); | |
7095 | add_scope_stmt (/*begin_p=*/1, /*partial_p=*/1); | |
7096 | } | |
5566b478 MS |
7097 | |
7098 | if (initialized) | |
7099 | /* Is it valid for this decl to have an initializer at all? | |
7100 | If not, set INITIALIZED to zero, which will indirectly | |
7101 | tell `cp_finish_decl' to ignore the initializer once it is parsed. */ | |
8d08fdba | 7102 | { |
5566b478 MS |
7103 | /* Don't allow initializations for incomplete types except for |
7104 | arrays which might be completed by the initialization. */ | |
44689c12 | 7105 | if (TYPE_SIZE (complete_type (type)) != NULL_TREE) |
5566b478 MS |
7106 | ; /* A complete type is ok. */ |
7107 | else if (TREE_CODE (type) != ARRAY_TYPE) | |
8d08fdba | 7108 | { |
8251199e | 7109 | cp_error ("variable `%#D' has initializer but incomplete type", |
5566b478 MS |
7110 | decl); |
7111 | initialized = 0; | |
25eb19ff | 7112 | type = TREE_TYPE (decl) = error_mark_node; |
5566b478 MS |
7113 | } |
7114 | else if (TYPE_SIZE (complete_type (TREE_TYPE (type))) == NULL_TREE) | |
7115 | { | |
7116 | if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)) | |
8251199e | 7117 | cp_error ("elements of array `%#D' have incomplete type", decl); |
5566b478 MS |
7118 | /* else we already gave an error in start_decl. */ |
7119 | initialized = 0; | |
8d08fdba | 7120 | } |
8d08fdba MS |
7121 | } |
7122 | ||
5566b478 MS |
7123 | if (!initialized |
7124 | && TREE_CODE (decl) != TYPE_DECL | |
7125 | && TREE_CODE (decl) != TEMPLATE_DECL | |
07c88314 MM |
7126 | && type != error_mark_node |
7127 | && IS_AGGR_TYPE (type) | |
7128 | && ! DECL_EXTERNAL (decl)) | |
8d08fdba | 7129 | { |
5156628f | 7130 | if ((! processing_template_decl || ! uses_template_parms (type)) |
7fcdf4c2 | 7131 | && TYPE_SIZE (complete_type (type)) == NULL_TREE) |
5566b478 | 7132 | { |
8251199e | 7133 | cp_error ("aggregate `%#D' has incomplete type and cannot be initialized", |
5566b478 MS |
7134 | decl); |
7135 | /* Change the type so that assemble_variable will give | |
7136 | DECL an rtl we can live with: (mem (const_int 0)). */ | |
25eb19ff | 7137 | type = TREE_TYPE (decl) = error_mark_node; |
5566b478 MS |
7138 | } |
7139 | else | |
7140 | { | |
7141 | /* If any base type in the hierarchy of TYPE needs a constructor, | |
7142 | then we set initialized to 1. This way any nodes which are | |
7143 | created for the purposes of initializing this aggregate | |
7144 | will live as long as it does. This is necessary for global | |
7145 | aggregates which do not have their initializers processed until | |
7146 | the end of the file. */ | |
7147 | initialized = TYPE_NEEDS_CONSTRUCTING (type); | |
7148 | } | |
7149 | } | |
7150 | ||
5566b478 MS |
7151 | if (! initialized) |
7152 | DECL_INITIAL (decl) = NULL_TREE; | |
7153 | } | |
7154 | ||
7155 | /* Handle initialization of references. | |
38e01259 | 7156 | These three arguments are from `cp_finish_decl', and have the |
e92cc029 MS |
7157 | same meaning here that they do there. |
7158 | ||
7159 | Quotes on semantics can be found in ARM 8.4.3. */ | |
7160 | ||
5566b478 | 7161 | static void |
a703fb38 | 7162 | grok_reference_init (decl, type, init) |
5566b478 | 7163 | tree decl, type, init; |
5566b478 MS |
7164 | { |
7165 | tree tmp; | |
7166 | ||
7167 | if (init == NULL_TREE) | |
7168 | { | |
7169 | if ((DECL_LANG_SPECIFIC (decl) == 0 | |
7170 | || DECL_IN_AGGR_P (decl) == 0) | |
7171 | && ! DECL_THIS_EXTERN (decl)) | |
ed5511d9 | 7172 | cp_error ("`%D' declared as reference but not initialized", decl); |
5566b478 MS |
7173 | return; |
7174 | } | |
7175 | ||
7176 | if (init == error_mark_node) | |
7177 | return; | |
7178 | ||
ed5511d9 | 7179 | if (TREE_CODE (init) == CONSTRUCTOR) |
5566b478 | 7180 | { |
cb9a3ff8 | 7181 | cp_error ("ISO C++ forbids use of initializer list to initialize reference `%D'", decl); |
8d08fdba MS |
7182 | return; |
7183 | } | |
7184 | ||
7185 | if (TREE_CODE (init) == TREE_LIST) | |
7186 | init = build_compound_expr (init); | |
8d08fdba | 7187 | |
8ccc31eb MS |
7188 | if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE) |
7189 | init = convert_from_reference (init); | |
7190 | ||
8d08fdba MS |
7191 | if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE |
7192 | && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE) | |
7193 | { | |
a3203465 | 7194 | /* Note: default conversion is only called in very special cases. */ |
8d08fdba MS |
7195 | init = default_conversion (init); |
7196 | } | |
68642fb6 | 7197 | |
24bef158 MM |
7198 | /* Convert INIT to the reference type TYPE. This may involve the |
7199 | creation of a temporary, whose lifetime must be the same as that | |
7200 | of the reference. If so, a DECL_STMT for the temporary will be | |
7201 | added just after the DECL_STMT for DECL. That's why we don't set | |
7202 | DECL_INITIAL for local references (instead assigning to them | |
7203 | explicitly); we need to allow the temporary to be initialized | |
7204 | first. */ | |
a3203465 | 7205 | tmp = convert_to_reference |
9a3b49ac MS |
7206 | (type, init, CONV_IMPLICIT, |
7207 | LOOKUP_SPECULATIVELY|LOOKUP_NORMAL|DIRECT_BIND, decl); | |
8d08fdba | 7208 | |
a3203465 | 7209 | if (tmp == error_mark_node) |
ed5511d9 | 7210 | return; |
a3203465 | 7211 | else if (tmp != NULL_TREE) |
8d08fdba | 7212 | { |
a3203465 | 7213 | init = tmp; |
24bef158 MM |
7214 | tmp = save_expr (tmp); |
7215 | if (building_stmt_tree ()) | |
7216 | { | |
7217 | /* Initialize the declaration. */ | |
7218 | tmp = build (INIT_EXPR, TREE_TYPE (decl), decl, tmp); | |
24bef158 MM |
7219 | finish_expr_stmt (tmp); |
7220 | } | |
7221 | else | |
7222 | DECL_INITIAL (decl) = tmp; | |
8d08fdba | 7223 | } |
a3203465 | 7224 | else |
8d08fdba | 7225 | { |
8251199e | 7226 | cp_error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init)); |
ed5511d9 | 7227 | return; |
8d08fdba | 7228 | } |
8d08fdba | 7229 | |
8d08fdba MS |
7230 | if (TREE_STATIC (decl) && ! TREE_CONSTANT (DECL_INITIAL (decl))) |
7231 | { | |
7232 | expand_static_init (decl, DECL_INITIAL (decl)); | |
7233 | DECL_INITIAL (decl) = NULL_TREE; | |
7234 | } | |
7235 | return; | |
8d08fdba MS |
7236 | } |
7237 | ||
6060a796 MS |
7238 | /* Fill in DECL_INITIAL with some magical value to prevent expand_decl from |
7239 | mucking with forces it does not comprehend (i.e. initialization with a | |
7240 | constructor). If we are at global scope and won't go into COMMON, fill | |
7241 | it in with a dummy CONSTRUCTOR to force the variable into .data; | |
7242 | otherwise we can use error_mark_node. */ | |
7243 | ||
28cbf42c MS |
7244 | static tree |
7245 | obscure_complex_init (decl, init) | |
7246 | tree decl, init; | |
6060a796 | 7247 | { |
28cbf42c MS |
7248 | if (! flag_no_inline && TREE_STATIC (decl)) |
7249 | { | |
7250 | if (extract_init (decl, init)) | |
7251 | return NULL_TREE; | |
7252 | } | |
7253 | ||
2ee887f2 | 7254 | #if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS) |
a9aedbc2 | 7255 | if (toplevel_bindings_p () && ! DECL_COMMON (decl)) |
6060a796 MS |
7256 | DECL_INITIAL (decl) = build (CONSTRUCTOR, TREE_TYPE (decl), NULL_TREE, |
7257 | NULL_TREE); | |
7258 | else | |
2ee887f2 | 7259 | #endif |
6060a796 | 7260 | DECL_INITIAL (decl) = error_mark_node; |
28cbf42c MS |
7261 | |
7262 | return init; | |
6060a796 MS |
7263 | } |
7264 | ||
27778b73 MM |
7265 | /* When parsing `int a[] = {1, 2};' we don't know the size of the |
7266 | array until we finish parsing the initializer. If that's the | |
7267 | situation we're in, update DECL accordingly. */ | |
7268 | ||
7269 | static void | |
7270 | maybe_deduce_size_from_array_init (decl, init) | |
7271 | tree decl; | |
7272 | tree init; | |
7273 | { | |
7274 | tree type = TREE_TYPE (decl); | |
7275 | ||
7276 | if (TREE_CODE (type) == ARRAY_TYPE | |
7277 | && TYPE_DOMAIN (type) == NULL_TREE | |
7278 | && TREE_CODE (decl) != TYPE_DECL) | |
7279 | { | |
7280 | int do_default | |
7281 | = (TREE_STATIC (decl) | |
7282 | /* Even if pedantic, an external linkage array | |
7283 | may have incomplete type at first. */ | |
7284 | ? pedantic && ! DECL_EXTERNAL (decl) | |
7285 | : !DECL_EXTERNAL (decl)); | |
7286 | tree initializer = init ? init : DECL_INITIAL (decl); | |
7287 | int failure = complete_array_type (type, initializer, do_default); | |
7288 | ||
7289 | if (failure == 1) | |
7290 | cp_error ("initializer fails to determine size of `%D'", decl); | |
7291 | ||
7292 | if (failure == 2) | |
7293 | { | |
7294 | if (do_default) | |
7295 | cp_error ("array size missing in `%D'", decl); | |
7296 | /* If a `static' var's size isn't known, make it extern as | |
7297 | well as static, so it does not get allocated. If it's not | |
7298 | `static', then don't mark it extern; finish_incomplete_decl | |
7299 | will give it a default size and it will get allocated. */ | |
7300 | else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl)) | |
7301 | DECL_EXTERNAL (decl) = 1; | |
7302 | } | |
7303 | ||
7304 | if (pedantic && TYPE_DOMAIN (type) != NULL_TREE | |
7305 | && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)), | |
7306 | integer_zero_node)) | |
7307 | cp_error ("zero-size array `%D'", decl); | |
7308 | ||
7309 | layout_decl (decl, 0); | |
7310 | } | |
7311 | } | |
7312 | ||
7313 | /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue | |
57b52417 | 7314 | any appropriate error messages regarding the layout. */ |
27778b73 | 7315 | |
57b52417 MM |
7316 | static void |
7317 | layout_var_decl (decl) | |
27778b73 | 7318 | tree decl; |
27778b73 | 7319 | { |
57b52417 | 7320 | tree type = TREE_TYPE (decl); |
c95cd22e | 7321 | #if 0 |
57b52417 | 7322 | tree ttype = target_type (type); |
c95cd22e | 7323 | #endif |
57b52417 MM |
7324 | |
7325 | /* If we haven't already layed out this declaration, do so now. | |
7326 | Note that we must not call complete type for an external object | |
7327 | because it's type might involve templates that we are not | |
68642fb6 | 7328 | supposed to isntantiate yet. (And it's perfectly legal to say |
57b52417 MM |
7329 | `extern X x' for some incomplete type `X'.) */ |
7330 | if (!DECL_EXTERNAL (decl)) | |
7331 | complete_type (type); | |
06ceef4e | 7332 | if (!DECL_SIZE (decl) && TYPE_SIZE (type)) |
27778b73 MM |
7333 | layout_decl (decl, 0); |
7334 | ||
c82dbd95 | 7335 | if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE) |
27778b73 MM |
7336 | { |
7337 | /* An automatic variable with an incomplete type: that is an error. | |
7338 | Don't talk about array types here, since we took care of that | |
7339 | message in grokdeclarator. */ | |
7340 | cp_error ("storage size of `%D' isn't known", decl); | |
7341 | TREE_TYPE (decl) = error_mark_node; | |
7342 | } | |
ae673f14 JM |
7343 | #if 0 |
7344 | /* Keep this code around in case we later want to control debug info | |
7345 | based on whether a type is "used". (jason 1999-11-11) */ | |
7346 | ||
27778b73 MM |
7347 | else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype)) |
7348 | /* Let debugger know it should output info for this type. */ | |
7349 | note_debug_info_needed (ttype); | |
7350 | ||
7351 | if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl)) | |
7352 | note_debug_info_needed (DECL_CONTEXT (decl)); | |
ae673f14 | 7353 | #endif |
27778b73 MM |
7354 | |
7355 | if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl)) | |
7356 | && DECL_SIZE (decl) != NULL_TREE | |
7357 | && ! TREE_CONSTANT (DECL_SIZE (decl))) | |
7358 | { | |
7359 | if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST) | |
7360 | constant_expression_warning (DECL_SIZE (decl)); | |
7361 | else | |
7362 | cp_error ("storage size of `%D' isn't constant", decl); | |
7363 | } | |
7364 | } | |
7365 | ||
27778b73 MM |
7366 | /* If a local static variable is declared in an inline function, or if |
7367 | we have a weak definition, we must endeavor to create only one | |
7368 | instance of the variable at link-time. */ | |
7369 | ||
7370 | static void | |
7371 | maybe_commonize_var (decl) | |
7372 | tree decl; | |
7373 | { | |
7374 | /* Static data in a function with comdat linkage also has comdat | |
7375 | linkage. */ | |
7376 | if (TREE_STATIC (decl) | |
7377 | /* Don't mess with __FUNCTION__. */ | |
cf74fb86 | 7378 | && ! DECL_ARTIFICIAL (decl) |
27778b73 MM |
7379 | && current_function_decl |
7380 | && DECL_CONTEXT (decl) == current_function_decl | |
7381 | && (DECL_THIS_INLINE (current_function_decl) | |
7382 | || DECL_TEMPLATE_INSTANTIATION (current_function_decl)) | |
7383 | && TREE_PUBLIC (current_function_decl)) | |
7384 | { | |
7385 | /* Rather than try to get this right with inlining, we suppress | |
7386 | inlining of such functions. */ | |
7387 | current_function_cannot_inline | |
7388 | = "function with static variable cannot be inline"; | |
7389 | ||
7390 | /* If flag_weak, we don't need to mess with this, as we can just | |
7391 | make the function weak, and let it refer to its unique local | |
7392 | copy. This works because we don't allow the function to be | |
7393 | inlined. */ | |
7394 | if (! flag_weak) | |
7395 | { | |
7396 | if (DECL_INTERFACE_KNOWN (current_function_decl)) | |
7397 | { | |
7398 | TREE_PUBLIC (decl) = 1; | |
7399 | DECL_EXTERNAL (decl) = DECL_EXTERNAL (current_function_decl); | |
7400 | } | |
7401 | else if (DECL_INITIAL (decl) == NULL_TREE | |
7402 | || DECL_INITIAL (decl) == error_mark_node) | |
7403 | { | |
7404 | TREE_PUBLIC (decl) = 1; | |
7405 | DECL_COMMON (decl) = 1; | |
7406 | } | |
7407 | /* else we lose. We can only do this if we can use common, | |
7408 | which we can't if it has been initialized. */ | |
7409 | ||
7410 | if (TREE_PUBLIC (decl)) | |
7411 | DECL_ASSEMBLER_NAME (decl) | |
7412 | = build_static_name (current_function_decl, DECL_NAME (decl)); | |
cf74fb86 | 7413 | else |
27778b73 MM |
7414 | { |
7415 | cp_warning_at ("sorry: semantics of inline function static data `%#D' are wrong (you'll wind up with multiple copies)", decl); | |
7416 | cp_warning_at (" you can work around this by removing the initializer", decl); | |
7417 | } | |
7418 | } | |
7419 | } | |
7420 | else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl)) | |
7421 | /* Set it up again; we might have set DECL_INITIAL since the last | |
7422 | time. */ | |
7423 | comdat_linkage (decl); | |
7424 | } | |
7425 | ||
91063b51 MM |
7426 | /* Issue an error message if DECL is an uninitialized const variable. */ |
7427 | ||
7428 | static void | |
7429 | check_for_uninitialized_const_var (decl) | |
7430 | tree decl; | |
7431 | { | |
7432 | tree type = TREE_TYPE (decl); | |
7433 | ||
7434 | /* ``Unless explicitly declared extern, a const object does not have | |
7435 | external linkage and must be initialized. ($8.4; $12.1)'' ARM | |
7436 | 7.1.6 */ | |
7437 | if (TREE_CODE (decl) == VAR_DECL | |
7438 | && TREE_CODE (type) != REFERENCE_TYPE | |
7439 | && CP_TYPE_CONST_P (type) | |
7440 | && !TYPE_NEEDS_CONSTRUCTING (type) | |
7441 | && !DECL_INITIAL (decl)) | |
7442 | cp_error ("uninitialized const `%D'", decl); | |
7443 | } | |
7444 | ||
c82dbd95 MM |
7445 | /* Verify INIT (the initializer for DECL), and record the |
7446 | initialization in DECL_INITIAL, if appropriate. Returns a new | |
7447 | value for INIT. */ | |
27778b73 | 7448 | |
c82dbd95 MM |
7449 | static tree |
7450 | check_initializer (decl, init) | |
27778b73 | 7451 | tree decl; |
c82dbd95 | 7452 | tree init; |
27778b73 | 7453 | { |
27778b73 MM |
7454 | tree type; |
7455 | ||
7456 | if (TREE_CODE (decl) == FIELD_DECL) | |
c82dbd95 | 7457 | return init; |
27778b73 | 7458 | |
fc0e7bf5 MM |
7459 | type = TREE_TYPE (decl); |
7460 | ||
27778b73 MM |
7461 | /* If `start_decl' didn't like having an initialization, ignore it now. */ |
7462 | if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE) | |
7463 | init = NULL_TREE; | |
27778b73 | 7464 | |
c82dbd95 | 7465 | /* Check the initializer. */ |
27778b73 MM |
7466 | if (init) |
7467 | { | |
c82dbd95 MM |
7468 | /* Things that are going to be initialized need to have complete |
7469 | type. */ | |
7470 | TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl)); | |
7471 | ||
7472 | if (type == error_mark_node) | |
7473 | /* We will have already complained. */ | |
7474 | init = NULL_TREE; | |
7475 | else if (TYPE_SIZE (type) && !TREE_CONSTANT (TYPE_SIZE (type))) | |
27778b73 MM |
7476 | { |
7477 | cp_error ("variable-sized object `%D' may not be initialized", decl); | |
7478 | init = NULL_TREE; | |
7479 | } | |
c82dbd95 MM |
7480 | else if (TREE_CODE (type) == ARRAY_TYPE |
7481 | && !TYPE_SIZE (TREE_TYPE (type))) | |
27778b73 MM |
7482 | { |
7483 | cp_error ("elements of array `%#D' have incomplete type", decl); | |
7484 | init = NULL_TREE; | |
7485 | } | |
c82dbd95 MM |
7486 | else if (!TYPE_SIZE (type)) |
7487 | { | |
7488 | cp_error ("`%D' has incomplete type", decl); | |
7489 | TREE_TYPE (decl) = error_mark_node; | |
7490 | init = NULL_TREE; | |
7491 | } | |
27778b73 MM |
7492 | } |
7493 | ||
7494 | if (TREE_CODE (decl) == CONST_DECL) | |
7495 | { | |
7496 | my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148); | |
7497 | ||
7498 | DECL_INITIAL (decl) = init; | |
7499 | ||
7500 | /* This will keep us from needing to worry about our obstacks. */ | |
7501 | my_friendly_assert (init != NULL_TREE, 149); | |
7502 | init = NULL_TREE; | |
7503 | } | |
c82dbd95 MM |
7504 | else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE) |
7505 | { | |
7506 | if (TREE_STATIC (decl)) | |
7507 | make_decl_rtl (decl, NULL_PTR, toplevel_bindings_p ()); | |
7508 | grok_reference_init (decl, type, init); | |
7509 | init = NULL_TREE; | |
7510 | } | |
27778b73 MM |
7511 | else if (init) |
7512 | { | |
7513 | if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type)) | |
7514 | { | |
7515 | if (TREE_CODE (type) == ARRAY_TYPE) | |
7516 | init = digest_init (type, init, (tree *) 0); | |
7517 | else if (TREE_CODE (init) == CONSTRUCTOR | |
7518 | && TREE_HAS_CONSTRUCTOR (init)) | |
7519 | { | |
7520 | if (TYPE_NON_AGGREGATE_CLASS (type)) | |
7521 | { | |
7522 | cp_error ("`%D' must be initialized by constructor, not by `{...}'", | |
7523 | decl); | |
7524 | init = error_mark_node; | |
7525 | } | |
7526 | else | |
7527 | goto dont_use_constructor; | |
7528 | } | |
7529 | } | |
7530 | else | |
7531 | { | |
7532 | dont_use_constructor: | |
7533 | if (TREE_CODE (init) != TREE_VEC) | |
7534 | init = store_init_value (decl, init); | |
7535 | } | |
7536 | ||
7537 | if (init) | |
7538 | /* We must hide the initializer so that expand_decl | |
7539 | won't try to do something it does not understand. */ | |
7540 | init = obscure_complex_init (decl, init); | |
7541 | } | |
7542 | else if (DECL_EXTERNAL (decl)) | |
7543 | ; | |
7544 | else if (TREE_CODE_CLASS (TREE_CODE (type)) == 't' | |
7545 | && (IS_AGGR_TYPE (type) || TYPE_NEEDS_CONSTRUCTING (type))) | |
7546 | { | |
7547 | tree core_type = strip_array_types (type); | |
7548 | ||
7549 | if (! TYPE_NEEDS_CONSTRUCTING (core_type)) | |
7550 | { | |
7551 | if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)) | |
7552 | cp_error ("structure `%D' with uninitialized const members", decl); | |
7553 | if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)) | |
7554 | cp_error ("structure `%D' with uninitialized reference members", | |
7555 | decl); | |
7556 | } | |
7557 | ||
7558 | check_for_uninitialized_const_var (decl); | |
7559 | ||
7560 | if (TYPE_SIZE (type) != NULL_TREE | |
7561 | && TYPE_NEEDS_CONSTRUCTING (type)) | |
7562 | init = obscure_complex_init (decl, NULL_TREE); | |
7563 | ||
7564 | } | |
7565 | else | |
7566 | check_for_uninitialized_const_var (decl); | |
68642fb6 | 7567 | |
c82dbd95 | 7568 | return init; |
27778b73 MM |
7569 | } |
7570 | ||
7571 | /* If DECL is not a local variable, give it RTL. */ | |
7572 | ||
7573 | static void | |
7574 | make_rtl_for_nonlocal_decl (decl, init, asmspec) | |
7575 | tree decl; | |
7576 | tree init; | |
7577 | const char *asmspec; | |
7578 | { | |
27778b73 MM |
7579 | int toplev; |
7580 | tree type; | |
7581 | ||
7582 | type = TREE_TYPE (decl); | |
7583 | toplev = toplevel_bindings_p (); | |
27778b73 | 7584 | |
f39ee884 MM |
7585 | /* Handle non-variables up front. */ |
7586 | if (TREE_CODE (decl) != VAR_DECL) | |
7587 | { | |
7588 | rest_of_decl_compilation (decl, asmspec, toplev, at_eof); | |
7589 | return; | |
7590 | } | |
7591 | ||
7592 | /* Set the DECL_ASSEMBLER_NAME for the variable. */ | |
7593 | if (asmspec) | |
7594 | DECL_ASSEMBLER_NAME (decl) = get_identifier (asmspec); | |
7595 | ||
7596 | if (DECL_VIRTUAL_P (decl)) | |
27778b73 | 7597 | make_decl_rtl (decl, NULL_PTR, toplev); |
f39ee884 | 7598 | else if (TREE_READONLY (decl) |
27778b73 MM |
7599 | && DECL_INITIAL (decl) != NULL_TREE |
7600 | && DECL_INITIAL (decl) != error_mark_node | |
7601 | && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))) | |
7602 | { | |
7603 | DECL_INITIAL (decl) = save_expr (DECL_INITIAL (decl)); | |
7604 | ||
f3010146 | 7605 | if (toplev && ! TREE_PUBLIC (decl)) |
27778b73 MM |
7606 | { |
7607 | /* If this is a static const, change its apparent linkage | |
7608 | if it belongs to a #pragma interface. */ | |
7609 | if (!interface_unknown) | |
7610 | { | |
7611 | TREE_PUBLIC (decl) = 1; | |
7612 | DECL_EXTERNAL (decl) = interface_only; | |
7613 | } | |
7614 | make_decl_rtl (decl, asmspec, toplev); | |
7615 | } | |
f39ee884 | 7616 | else if (toplev) |
27778b73 MM |
7617 | rest_of_decl_compilation (decl, asmspec, toplev, at_eof); |
7618 | } | |
f39ee884 | 7619 | else if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl)) |
27778b73 MM |
7620 | { |
7621 | my_friendly_assert (TREE_STATIC (decl), 19990828); | |
7622 | ||
7623 | if (init == NULL_TREE | |
7624 | #ifdef DEFAULT_STATIC_DEFS | |
7625 | /* If this code is dead, then users must | |
7626 | explicitly declare static member variables | |
7627 | outside the class def'n as well. */ | |
7628 | && TYPE_NEEDS_CONSTRUCTING (type) | |
7629 | #endif | |
7630 | ) | |
7631 | { | |
7632 | DECL_EXTERNAL (decl) = 1; | |
7633 | make_decl_rtl (decl, asmspec, 1); | |
7634 | } | |
7635 | else | |
7636 | rest_of_decl_compilation (decl, asmspec, toplev, at_eof); | |
7637 | } | |
f39ee884 MM |
7638 | else if (TREE_CODE (CP_DECL_CONTEXT (decl)) == NAMESPACE_DECL |
7639 | || (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))) | |
27778b73 | 7640 | rest_of_decl_compilation (decl, asmspec, toplev, at_eof); |
27778b73 MM |
7641 | } |
7642 | ||
f3010146 MM |
7643 | /* Create RTL for the local static variable DECL. */ |
7644 | ||
7645 | void | |
7646 | make_rtl_for_local_static (decl) | |
7647 | tree decl; | |
7648 | { | |
f3010146 MM |
7649 | const char *asmspec = NULL; |
7650 | ||
46e8c075 MM |
7651 | /* If we inlined this variable, we could see it's declaration |
7652 | again. */ | |
7653 | if (DECL_RTL (decl)) | |
7654 | return; | |
f3010146 MM |
7655 | |
7656 | if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)) | |
7657 | { | |
7658 | /* The only way this situaton can occur is if the | |
7659 | user specified a name for this DECL using the | |
7660 | `attribute' syntax. */ | |
7661 | asmspec = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)); | |
7662 | DECL_ASSEMBLER_NAME (decl) = DECL_NAME (decl); | |
7663 | } | |
7664 | ||
7665 | rest_of_decl_compilation (decl, asmspec, /*top_level=*/0, /*at_end=*/0); | |
7666 | } | |
7667 | ||
27778b73 MM |
7668 | /* The old ARM scoping rules injected variables declared in the |
7669 | initialization statement of a for-statement into the surrounding | |
7670 | scope. We support this usage, in order to be backward-compatible. | |
7671 | DECL is a just-declared VAR_DECL; if necessary inject its | |
7672 | declaration into the surrounding scope. */ | |
7673 | ||
b7b8bcd2 | 7674 | void |
27778b73 MM |
7675 | maybe_inject_for_scope_var (decl) |
7676 | tree decl; | |
7677 | { | |
7678 | if (current_binding_level->is_for_scope) | |
7679 | { | |
68642fb6 | 7680 | struct binding_level *outer |
27778b73 MM |
7681 | = current_binding_level->level_chain; |
7682 | ||
7683 | /* Check to see if the same name is already bound at the outer | |
7684 | level, either because it was directly declared, or because a | |
7685 | dead for-decl got preserved. In either case, the code would | |
7686 | not have been valid under the ARM scope rules, so clear | |
7687 | is_for_scope for the current_binding_level. | |
7688 | ||
7689 | Otherwise, we need to preserve the temp slot for decl to last | |
7690 | into the outer binding level. */ | |
7691 | ||
68642fb6 | 7692 | tree outer_binding |
27778b73 | 7693 | = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (decl))); |
68642fb6 | 7694 | |
27778b73 | 7695 | if (outer_binding && BINDING_LEVEL (outer_binding) == outer |
68642fb6 | 7696 | && (TREE_CODE (BINDING_VALUE (outer_binding)) |
27778b73 MM |
7697 | == VAR_DECL) |
7698 | && DECL_DEAD_FOR_LOCAL (BINDING_VALUE (outer_binding))) | |
7699 | { | |
7700 | BINDING_VALUE (outer_binding) | |
7701 | = DECL_SHADOWED_FOR_VAR (BINDING_VALUE (outer_binding)); | |
7702 | current_binding_level->is_for_scope = 0; | |
7703 | } | |
7704 | else if (DECL_IN_MEMORY_P (decl)) | |
7705 | preserve_temp_slots (DECL_RTL (decl)); | |
7706 | } | |
7707 | } | |
7708 | ||
ed5511d9 | 7709 | /* Generate code to initialize DECL (a local variable). */ |
27778b73 | 7710 | |
b7b8bcd2 MM |
7711 | void |
7712 | initialize_local_var (decl, init, flags) | |
27778b73 MM |
7713 | tree decl; |
7714 | tree init; | |
27778b73 MM |
7715 | int flags; |
7716 | { | |
9ed9e79a | 7717 | tree type = TREE_TYPE (decl); |
27778b73 | 7718 | |
9ed9e79a MM |
7719 | /* If the type is bogus, don't bother initializing the variable. */ |
7720 | if (type == error_mark_node) | |
7721 | return; | |
b7b8bcd2 | 7722 | |
b7b8bcd2 MM |
7723 | if (DECL_SIZE (decl) == NULL_TREE && !TREE_STATIC (decl)) |
7724 | { | |
7725 | /* If we used it already as memory, it must stay in memory. */ | |
7726 | DECL_INITIAL (decl) = NULL_TREE; | |
7727 | TREE_ADDRESSABLE (decl) = TREE_USED (decl); | |
7728 | } | |
7729 | ||
9ed9e79a MM |
7730 | /* Local statics are handled differently from ordinary automatic |
7731 | variables. */ | |
7732 | if (TREE_STATIC (decl)) | |
7733 | { | |
7734 | if (TYPE_NEEDS_CONSTRUCTING (type) || init != NULL_TREE | |
834c6dff | 7735 | || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) |
9ed9e79a MM |
7736 | expand_static_init (decl, init); |
7737 | return; | |
7738 | } | |
7739 | ||
27778b73 MM |
7740 | if (DECL_SIZE (decl) && type != error_mark_node) |
7741 | { | |
7742 | int already_used; | |
68642fb6 | 7743 | |
27778b73 | 7744 | /* Compute and store the initial value. */ |
27778b73 MM |
7745 | already_used = TREE_USED (decl) || TREE_USED (type); |
7746 | ||
7747 | if (init || TYPE_NEEDS_CONSTRUCTING (type)) | |
7748 | { | |
24bef158 MM |
7749 | int saved_stmts_are_full_exprs_p; |
7750 | ||
27778b73 MM |
7751 | emit_line_note (DECL_SOURCE_FILE (decl), |
7752 | DECL_SOURCE_LINE (decl)); | |
24bef158 MM |
7753 | saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p; |
7754 | stmts_are_full_exprs_p = 1; | |
27778b73 | 7755 | finish_expr_stmt (build_aggr_init (decl, init, flags)); |
24bef158 | 7756 | stmts_are_full_exprs_p = saved_stmts_are_full_exprs_p; |
27778b73 MM |
7757 | } |
7758 | ||
7759 | /* Set this to 0 so we can tell whether an aggregate which was | |
7760 | initialized was ever used. Don't do this if it has a | |
7761 | destructor, so we don't complain about the 'resource | |
b7b8bcd2 MM |
7762 | allocation is initialization' idiom. Now set |
7763 | attribute((unused)) on types so decls of that type will be | |
7764 | marked used. (see TREE_USED, above.) */ | |
27778b73 MM |
7765 | if (TYPE_NEEDS_CONSTRUCTING (type) |
7766 | && ! already_used | |
834c6dff | 7767 | && TYPE_HAS_TRIVIAL_DESTRUCTOR (type) |
27778b73 MM |
7768 | && DECL_NAME (decl)) |
7769 | TREE_USED (decl) = 0; | |
b7b8bcd2 | 7770 | else if (already_used) |
27778b73 MM |
7771 | TREE_USED (decl) = 1; |
7772 | } | |
24bef158 | 7773 | } |
27778b73 | 7774 | |
24bef158 MM |
7775 | /* Generate code to destroy DECL (a local variable). */ |
7776 | ||
68642fb6 | 7777 | static void |
24bef158 MM |
7778 | destroy_local_var (decl) |
7779 | tree decl; | |
7780 | { | |
9d85d30c MM |
7781 | tree type = TREE_TYPE (decl); |
7782 | tree cleanup; | |
7783 | ||
7784 | /* Only variables get cleaned up. */ | |
7785 | if (TREE_CODE (decl) != VAR_DECL) | |
7786 | return; | |
68642fb6 | 7787 | |
9d85d30c | 7788 | /* And only things with destructors need cleaning up. */ |
834c6dff | 7789 | if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)) |
9d85d30c MM |
7790 | return; |
7791 | ||
7792 | if (TREE_CODE (decl) == VAR_DECL && | |
7793 | (DECL_EXTERNAL (decl) || TREE_STATIC (decl))) | |
7794 | /* We don't clean up things that aren't defined in this | |
7795 | translation unit, or that need a static cleanup. The latter | |
7796 | are handled by finish_file. */ | |
7797 | return; | |
68642fb6 | 7798 | |
9d85d30c MM |
7799 | /* Compute the cleanup. */ |
7800 | cleanup = maybe_build_cleanup (decl); | |
27778b73 | 7801 | |
b7b8bcd2 | 7802 | /* Record the cleanup required for this declaration. */ |
24bef158 MM |
7803 | if (DECL_SIZE (decl) && TREE_TYPE (decl) != error_mark_node |
7804 | && cleanup) | |
7805 | finish_decl_cleanup (decl, cleanup); | |
7806 | } | |
7807 | ||
7808 | /* Let the back-end know about DECL. */ | |
7809 | ||
7810 | void | |
7811 | emit_local_var (decl) | |
7812 | tree decl; | |
7813 | { | |
7814 | /* Create RTL for this variable. */ | |
7815 | if (DECL_RTL (decl)) | |
46e8c075 MM |
7816 | /* Only a RESULT_DECL should have non-NULL RTL when arriving here. |
7817 | All other local variables are assigned RTL in this function. */ | |
68642fb6 | 7818 | my_friendly_assert (TREE_CODE (decl) == RESULT_DECL, |
24bef158 MM |
7819 | 19990828); |
7820 | else | |
f39ee884 MM |
7821 | { |
7822 | if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)) | |
7823 | /* The user must have specified an assembler name for this | |
7824 | variable. Set that up now. */ | |
68642fb6 | 7825 | rest_of_decl_compilation |
f39ee884 MM |
7826 | (decl, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), |
7827 | /*top_level=*/0, /*at_end=*/0); | |
7828 | else | |
7829 | expand_decl (decl); | |
7830 | } | |
24bef158 MM |
7831 | |
7832 | /* Actually do the initialization. */ | |
7833 | expand_start_target_temps (); | |
7834 | expand_decl_init (decl); | |
7835 | expand_end_target_temps (); | |
27778b73 MM |
7836 | } |
7837 | ||
8d08fdba MS |
7838 | /* Finish processing of a declaration; |
7839 | install its line number and initial value. | |
7840 | If the length of an array type is not known before, | |
7841 | it must be determined now, from the initial value, or it is an error. | |
7842 | ||
8d08fdba MS |
7843 | INIT0 holds the value of an initializer that should be allowed to escape |
7844 | the normal rules. | |
7845 | ||
920f9474 | 7846 | FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0 |
f78c7bc6 | 7847 | if the (init) syntax was used. */ |
8d08fdba MS |
7848 | |
7849 | void | |
cd9f6678 | 7850 | cp_finish_decl (decl, init, asmspec_tree, flags) |
8d08fdba MS |
7851 | tree decl, init; |
7852 | tree asmspec_tree; | |
6060a796 | 7853 | int flags; |
8d08fdba MS |
7854 | { |
7855 | register tree type; | |
27778b73 | 7856 | tree ttype = NULL_TREE; |
9c0758dd | 7857 | const char *asmspec = NULL; |
8d08fdba MS |
7858 | int was_readonly = 0; |
7859 | ||
8d08fdba MS |
7860 | if (! decl) |
7861 | { | |
7862 | if (init) | |
8251199e | 7863 | error ("assignment (not initialization) in declaration"); |
8d08fdba MS |
7864 | return; |
7865 | } | |
7866 | ||
a4443a08 | 7867 | /* If a name was specified, get the string. */ |
8d08fdba | 7868 | if (asmspec_tree) |
8d08fdba | 7869 | asmspec = TREE_STRING_POINTER (asmspec_tree); |
8d08fdba | 7870 | |
2c73f9f5 ML |
7871 | if (init && TREE_CODE (init) == NAMESPACE_DECL) |
7872 | { | |
920f9474 | 7873 | cp_error ("cannot initialize `%D' to namespace `%D'", |
2c73f9f5 ML |
7874 | decl, init); |
7875 | init = NULL_TREE; | |
7876 | } | |
7877 | ||
6ba89f8e | 7878 | if (current_class_type |
4f1c5b7d | 7879 | && CP_DECL_CONTEXT (decl) == current_class_type |
6ba89f8e MM |
7880 | && TYPE_BEING_DEFINED (current_class_type) |
7881 | && (DECL_INITIAL (decl) || init)) | |
7882 | DECL_DEFINED_IN_CLASS_P (decl) = 1; | |
7883 | ||
68642fb6 | 7884 | if (TREE_CODE (decl) == VAR_DECL |
9a68c51f JM |
7885 | && DECL_CONTEXT (decl) |
7886 | && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL | |
7887 | && DECL_CONTEXT (decl) != current_namespace | |
7888 | && init) | |
7889 | { | |
7890 | /* Leave the namespace of the object. */ | |
7891 | pop_decl_namespace (); | |
7892 | } | |
7893 | ||
c82dbd95 | 7894 | type = TREE_TYPE (decl); |
8d08fdba | 7895 | |
f376e137 | 7896 | if (type == error_mark_node) |
cd9f6678 | 7897 | return; |
f376e137 | 7898 | |
24bef158 | 7899 | /* Add this declaration to the statement-tree. */ |
f9817201 | 7900 | if (building_stmt_tree () && at_function_scope_p ()) |
24bef158 MM |
7901 | add_decl_stmt (decl); |
7902 | ||
a7a7710d NS |
7903 | if (TYPE_HAS_MUTABLE_P (type)) |
7904 | TREE_READONLY (decl) = 0; | |
24bef158 | 7905 | |
5156628f | 7906 | if (processing_template_decl) |
5566b478 MS |
7907 | { |
7908 | if (init && DECL_INITIAL (decl)) | |
2a1e9fdd | 7909 | DECL_INITIAL (decl) = init; |
5566b478 MS |
7910 | goto finish_end0; |
7911 | } | |
3e41d13b | 7912 | |
27778b73 MM |
7913 | /* Parameters are handled by store_parm_decls, not cp_finish_decl. */ |
7914 | my_friendly_assert (TREE_CODE (decl) != PARM_DECL, 19990828); | |
7915 | ||
8d08fdba MS |
7916 | /* Take care of TYPE_DECLs up front. */ |
7917 | if (TREE_CODE (decl) == TYPE_DECL) | |
7918 | { | |
7919 | if (init && DECL_INITIAL (decl)) | |
7920 | { | |
7921 | /* typedef foo = bar; store the type of bar as the type of foo. */ | |
7922 | TREE_TYPE (decl) = type = TREE_TYPE (init); | |
7923 | DECL_INITIAL (decl) = init = NULL_TREE; | |
7924 | } | |
a0a33927 MS |
7925 | if (type != error_mark_node |
7926 | && IS_AGGR_TYPE (type) && DECL_NAME (decl)) | |
8d08fdba MS |
7927 | { |
7928 | if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type) | |
8251199e | 7929 | cp_warning ("shadowing previous type declaration of `%#D'", decl); |
8d08fdba MS |
7930 | set_identifier_type_value (DECL_NAME (decl), type); |
7931 | CLASSTYPE_GOT_SEMICOLON (type) = 1; | |
7932 | } | |
7933 | GNU_xref_decl (current_function_decl, decl); | |
cffa8729 MS |
7934 | |
7935 | /* If we have installed this as the canonical typedef for this | |
7936 | type, and that type has not been defined yet, delay emitting | |
956d6950 | 7937 | the debug information for it, as we will emit it later. */ |
d2e5ee5c | 7938 | if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl |
cffa8729 MS |
7939 | && TYPE_SIZE (TREE_TYPE (decl)) == NULL_TREE) |
7940 | TYPE_DECL_SUPPRESS_DEBUG (decl) = 1; | |
7941 | ||
8d08fdba | 7942 | rest_of_decl_compilation (decl, NULL_PTR, |
5566b478 | 7943 | DECL_CONTEXT (decl) == NULL_TREE, at_eof); |
8d08fdba MS |
7944 | goto finish_end; |
7945 | } | |
3e41d13b | 7946 | |
8d08fdba | 7947 | if (TREE_CODE (decl) != FUNCTION_DECL) |
3e41d13b | 7948 | ttype = target_type (type); |
8d08fdba MS |
7949 | |
7950 | if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl) | |
7951 | && TYPE_NEEDS_CONSTRUCTING (type)) | |
7952 | { | |
8d08fdba MS |
7953 | /* Currently, GNU C++ puts constants in text space, making them |
7954 | impossible to initialize. In the future, one would hope for | |
7955 | an operating system which understood the difference between | |
7956 | initialization and the running of a program. */ | |
7957 | was_readonly = 1; | |
7958 | TREE_READONLY (decl) = 0; | |
7959 | } | |
7960 | ||
27778b73 | 7961 | if (TREE_CODE (decl) == FIELD_DECL && asmspec) |
8d08fdba | 7962 | { |
27778b73 MM |
7963 | /* This must override the asm specifier which was placed by |
7964 | grokclassfn. Lay this out fresh. */ | |
7965 | DECL_RTL (TREE_TYPE (decl)) = NULL_RTX; | |
7966 | DECL_ASSEMBLER_NAME (decl) = get_identifier (asmspec); | |
7967 | make_decl_rtl (decl, asmspec, 0); | |
8d08fdba MS |
7968 | } |
7969 | ||
c82dbd95 MM |
7970 | /* Deduce size of array from initialization, if not already known. */ |
7971 | maybe_deduce_size_from_array_init (decl, init); | |
7972 | init = check_initializer (decl, init); | |
3e41d13b | 7973 | |
8d08fdba MS |
7974 | GNU_xref_decl (current_function_decl, decl); |
7975 | ||
8d08fdba | 7976 | if (TREE_CODE (decl) == VAR_DECL) |
57b52417 | 7977 | layout_var_decl (decl); |
8d08fdba MS |
7978 | |
7979 | /* Output the assembler code and/or RTL code for variables and functions, | |
7980 | unless the type is an undefined structure or union. | |
7981 | If not, it will get done when the type is completed. */ | |
8d08fdba MS |
7982 | if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL |
7983 | || TREE_CODE (decl) == RESULT_DECL) | |
7984 | { | |
27778b73 MM |
7985 | if (TREE_CODE (decl) == VAR_DECL) |
7986 | maybe_commonize_var (decl); | |
8d08fdba | 7987 | |
27778b73 | 7988 | make_rtl_for_nonlocal_decl (decl, init, asmspec); |
8d08fdba | 7989 | |
68642fb6 | 7990 | if (TREE_CODE (type) == FUNCTION_TYPE |
27778b73 | 7991 | || TREE_CODE (type) == METHOD_TYPE) |
68642fb6 | 7992 | abstract_virtuals_error (decl, |
27778b73 | 7993 | strip_array_types (TREE_TYPE (type))); |
68642fb6 | 7994 | else |
27778b73 | 7995 | abstract_virtuals_error (decl, strip_array_types (type)); |
8d08fdba | 7996 | |
8d08fdba | 7997 | if (TREE_CODE (decl) == FUNCTION_DECL) |
faae18ab | 7998 | ; |
67d743fe MS |
7999 | else if (DECL_EXTERNAL (decl) |
8000 | && ! (DECL_LANG_SPECIFIC (decl) | |
8001 | && DECL_NOT_REALLY_EXTERN (decl))) | |
5566b478 MS |
8002 | { |
8003 | if (init) | |
8004 | DECL_INITIAL (decl) = init; | |
8005 | } | |
b35d4555 | 8006 | else if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL) |
8d08fdba | 8007 | { |
b7b8bcd2 | 8008 | /* This is a local declaration. */ |
b35d4555 MM |
8009 | if (doing_semantic_analysis_p ()) |
8010 | maybe_inject_for_scope_var (decl); | |
b7b8bcd2 MM |
8011 | /* Initialize the local variable. But, if we're building a |
8012 | statement-tree, we'll do the initialization when we | |
8013 | expand the tree. */ | |
24bef158 MM |
8014 | if (processing_template_decl) |
8015 | { | |
8016 | if (init || DECL_INITIAL (decl) == error_mark_node) | |
8017 | DECL_INITIAL (decl) = init; | |
8018 | } | |
8019 | else | |
8020 | { | |
b35d4555 MM |
8021 | /* If we're not building RTL, then we need to do so |
8022 | now. */ | |
24bef158 MM |
8023 | if (!building_stmt_tree ()) |
8024 | emit_local_var (decl); | |
b35d4555 | 8025 | /* Initialize the variable. */ |
24bef158 MM |
8026 | initialize_local_var (decl, init, flags); |
8027 | /* Clean up the variable. */ | |
8028 | destroy_local_var (decl); | |
8029 | } | |
8d08fdba | 8030 | } |
9ed9e79a MM |
8031 | else if (TREE_STATIC (decl) && type != error_mark_node) |
8032 | { | |
8033 | /* Cleanups for static variables are handled by `finish_file'. */ | |
8034 | if (TYPE_NEEDS_CONSTRUCTING (type) || init != NULL_TREE | |
834c6dff | 8035 | || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) |
9ed9e79a MM |
8036 | expand_static_init (decl, init); |
8037 | } | |
8d08fdba MS |
8038 | finish_end0: |
8039 | ||
8040 | /* Undo call to `pushclass' that was done in `start_decl' | |
8041 | due to initialization of qualified member variable. | |
8042 | I.e., Foo::x = 10; */ | |
8043 | { | |
4f1c5b7d | 8044 | tree context = CP_DECL_CONTEXT (decl); |
8d08fdba MS |
8045 | if (context |
8046 | && TREE_CODE_CLASS (TREE_CODE (context)) == 't' | |
8047 | && (TREE_CODE (decl) == VAR_DECL | |
8048 | /* We also have a pushclass done that we need to undo here | |
8049 | if we're at top level and declare a method. */ | |
5566b478 MS |
8050 | || TREE_CODE (decl) == FUNCTION_DECL) |
8051 | /* If size hasn't been set, we're still defining it, | |
8052 | and therefore inside the class body; don't pop | |
8053 | the binding level.. */ | |
8054 | && TYPE_SIZE (context) != NULL_TREE | |
8055 | && context == current_class_type) | |
6b400b21 | 8056 | pop_nested_class (); |
8d08fdba MS |
8057 | } |
8058 | } | |
8059 | ||
8060 | finish_end: | |
8061 | ||
8d08fdba MS |
8062 | if (was_readonly) |
8063 | TREE_READONLY (decl) = 1; | |
8d08fdba MS |
8064 | } |
8065 | ||
82580166 | 8066 | /* This is here for a midend callback from c-common.c */ |
e92cc029 | 8067 | |
82580166 MS |
8068 | void |
8069 | finish_decl (decl, init, asmspec_tree) | |
8070 | tree decl, init; | |
8071 | tree asmspec_tree; | |
8072 | { | |
cd9f6678 | 8073 | cp_finish_decl (decl, init, asmspec_tree, 0); |
82580166 MS |
8074 | } |
8075 | ||
db4283a0 MM |
8076 | /* Returns a declaration for a VAR_DECL as if: |
8077 | ||
8078 | extern "C" TYPE NAME; | |
8079 | ||
8080 | had been seen. Used to create compiler-generated global | |
8081 | variables. */ | |
8082 | ||
8083 | tree | |
8084 | declare_global_var (name, type) | |
8085 | tree name; | |
8086 | tree type; | |
8087 | { | |
8088 | tree decl; | |
8089 | ||
8090 | push_to_top_level (); | |
8091 | decl = build_decl (VAR_DECL, name, type); | |
8092 | TREE_PUBLIC (decl) = 1; | |
8093 | DECL_EXTERNAL (decl) = 1; | |
8094 | DECL_ARTIFICIAL (decl) = 1; | |
8095 | pushdecl (decl); | |
8096 | cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0); | |
8097 | pop_from_top_level (); | |
8098 | ||
8099 | return decl; | |
8100 | } | |
8101 | ||
8102 | /* Returns a pointer to the `atexit' function. Note that if | |
8103 | FLAG_USE_CXA_ATEXIT is non-zero, then this will actually be the new | |
8104 | `__cxa_atexit' function specified in the IA64 C++ ABI. */ | |
8105 | ||
8106 | static tree | |
8107 | get_atexit_node () | |
8108 | { | |
8109 | tree atexit_fndecl; | |
8110 | tree arg_types; | |
8111 | tree fn_type; | |
8112 | tree fn_ptr_type; | |
8113 | const char *name; | |
8114 | ||
8115 | if (atexit_node) | |
8116 | return atexit_node; | |
8117 | ||
8118 | if (flag_use_cxa_atexit) | |
8119 | { | |
8120 | /* The declaration for `__cxa_atexit' is: | |
8121 | ||
8122 | int __cxa_atexit (void (*)(void *), void *, void *) | |
8123 | ||
8124 | We build up the argument types and then then function type | |
8125 | itself. */ | |
68642fb6 | 8126 | |
db4283a0 MM |
8127 | /* First, build the pointer-to-function type for the first |
8128 | argument. */ | |
8129 | arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node); | |
8130 | fn_type = build_function_type (void_type_node, arg_types); | |
8131 | fn_ptr_type = build_pointer_type (fn_type); | |
8132 | /* Then, build the rest of the argument types. */ | |
8133 | arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node); | |
8134 | arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types); | |
8135 | arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types); | |
8136 | /* And the final __cxa_atexit type. */ | |
8137 | fn_type = build_function_type (integer_type_node, arg_types); | |
8138 | fn_ptr_type = build_pointer_type (fn_type); | |
8139 | name = "__cxa_atexit"; | |
8140 | } | |
8141 | else | |
8142 | { | |
8143 | /* The declaration for `atexit' is: | |
68642fb6 | 8144 | |
db4283a0 MM |
8145 | int atexit (void (*)()); |
8146 | ||
8147 | We build up the argument types and then then function type | |
8148 | itself. */ | |
8149 | fn_type = build_function_type (void_type_node, void_list_node); | |
8150 | fn_ptr_type = build_pointer_type (fn_type); | |
8151 | arg_types = tree_cons (NULL_TREE, fn_ptr_type, void_list_node); | |
8152 | /* Build the final atexit type. */ | |
8153 | fn_type = build_function_type (integer_type_node, arg_types); | |
8154 | name = "atexit"; | |
8155 | } | |
8156 | ||
8157 | /* Now, build the function declaration. */ | |
8158 | push_lang_context (lang_name_c); | |
0c11ada6 | 8159 | atexit_fndecl = build_library_fn_ptr (name, fn_type); |
db4283a0 MM |
8160 | mark_used (atexit_fndecl); |
8161 | pop_lang_context (); | |
8162 | atexit_node = default_conversion (atexit_fndecl); | |
8163 | ||
8164 | return atexit_node; | |
8165 | } | |
8166 | ||
8167 | /* Returns the __dso_handle VAR_DECL. */ | |
8168 | ||
8169 | static tree | |
8170 | get_dso_handle_node () | |
8171 | { | |
8172 | if (dso_handle_node) | |
8173 | return dso_handle_node; | |
8174 | ||
8175 | /* Declare the variable. */ | |
8176 | dso_handle_node = declare_global_var (get_identifier ("__dso_handle"), | |
8177 | ptr_type_node); | |
8178 | ||
8179 | return dso_handle_node; | |
8180 | } | |
8181 | ||
8182 | /* Begin a new function with internal linkage whose job will be simply | |
8183 | to destroy some particular variable. */ | |
8184 | ||
8185 | static tree | |
8186 | start_cleanup_fn () | |
8187 | { | |
8188 | static int counter = 0; | |
8189 | int old_interface_unknown = interface_unknown; | |
8190 | char name[32]; | |
8191 | tree parmtypes; | |
8192 | tree fntype; | |
8193 | tree fndecl; | |
8194 | ||
8195 | push_to_top_level (); | |
8196 | ||
8197 | /* No need to mangle this. */ | |
8198 | push_lang_context (lang_name_c); | |
8199 | ||
8200 | interface_unknown = 1; | |
8201 | ||
8202 | /* Build the parameter-types. */ | |
8203 | parmtypes = void_list_node; | |
8204 | /* Functions passed to __cxa_atexit take an additional parameter. | |
8205 | We'll just ignore it. After we implement the new calling | |
8206 | convention for destructors, we can eliminate the use of | |
8207 | additional cleanup functions entirely in the -fnew-abi case. */ | |
8208 | if (flag_use_cxa_atexit) | |
8209 | parmtypes = tree_cons (NULL_TREE, ptr_type_node, parmtypes); | |
8210 | /* Build the function type itself. */ | |
8211 | fntype = build_function_type (void_type_node, parmtypes); | |
8212 | /* Build the name of the function. */ | |
8213 | sprintf (name, "__tcf_%d", counter++); | |
8214 | /* Build the function declaration. */ | |
8215 | fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype); | |
8216 | /* It's a function with internal linkage, generated by the | |
8217 | compiler. */ | |
8218 | TREE_PUBLIC (fndecl) = 0; | |
8219 | DECL_ARTIFICIAL (fndecl) = 1; | |
8220 | /* Build the parameter. */ | |
8221 | if (flag_use_cxa_atexit) | |
8222 | { | |
8223 | tree parmdecl; | |
8224 | ||
8225 | parmdecl = build_decl (PARM_DECL, NULL_TREE, ptr_type_node); | |
8226 | DECL_CONTEXT (parmdecl) = fndecl; | |
8227 | DECL_ARG_TYPE (parmdecl) = ptr_type_node; | |
8228 | TREE_USED (parmdecl) = 1; | |
8229 | DECL_ARGUMENTS (fndecl) = parmdecl; | |
8230 | } | |
8231 | ||
09ed39ad | 8232 | pushdecl (fndecl); |
db4283a0 MM |
8233 | start_function (/*specs=*/NULL_TREE, fndecl, NULL_TREE, SF_PRE_PARSED); |
8234 | do_pushlevel (); | |
8235 | ||
8236 | interface_unknown = old_interface_unknown; | |
8237 | ||
8238 | pop_lang_context (); | |
8239 | ||
8240 | return current_function_decl; | |
8241 | } | |
8242 | ||
8243 | /* Finish the cleanup function begun by start_cleanup_fn. */ | |
8244 | ||
8245 | static void | |
8246 | end_cleanup_fn () | |
8247 | { | |
8248 | do_poplevel (); | |
8249 | ||
8250 | expand_body (finish_function (lineno, 0)); | |
8251 | ||
8252 | pop_from_top_level (); | |
8253 | } | |
8254 | ||
bf419747 MM |
8255 | /* Generate code to handle the destruction of DECL, an object with |
8256 | static storage duration. */ | |
f0105ed3 | 8257 | |
bf419747 MM |
8258 | void |
8259 | register_dtor_fn (decl) | |
f0105ed3 MM |
8260 | tree decl; |
8261 | { | |
db4283a0 | 8262 | tree cleanup; |
f0105ed3 | 8263 | tree compound_stmt; |
db4283a0 MM |
8264 | tree args; |
8265 | tree fcall; | |
f0105ed3 | 8266 | |
db4283a0 | 8267 | int saved_flag_access_control; |
f0105ed3 | 8268 | |
834c6dff | 8269 | if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl))) |
bf419747 MM |
8270 | return; |
8271 | ||
f0105ed3 MM |
8272 | /* Call build_cleanup before we enter the anonymous function so that |
8273 | any access checks will be done relative to the current scope, | |
8274 | rather than the scope of the anonymous function. */ | |
8275 | build_cleanup (decl); | |
8276 | ||
8277 | /* Now start the function. */ | |
db4283a0 | 8278 | cleanup = start_cleanup_fn (); |
f0105ed3 MM |
8279 | |
8280 | /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer | |
8281 | to the original function, rather than the anonymous one. That | |
8282 | will make the back-end think that nested functions are in use, | |
8283 | which causes confusion. */ | |
8284 | saved_flag_access_control = flag_access_control; | |
8285 | flag_access_control = 0; | |
8286 | fcall = build_cleanup (decl); | |
8287 | flag_access_control = saved_flag_access_control; | |
8288 | ||
8289 | /* Create the body of the anonymous function. */ | |
8290 | compound_stmt = begin_compound_stmt (/*has_no_scope=*/0); | |
8291 | finish_expr_stmt (fcall); | |
8292 | finish_compound_stmt (/*has_no_scope=*/0, compound_stmt); | |
db4283a0 | 8293 | end_cleanup_fn (); |
f0105ed3 MM |
8294 | |
8295 | /* Call atexit with the cleanup function. */ | |
8296 | mark_addressable (cleanup); | |
8297 | cleanup = build_unary_op (ADDR_EXPR, cleanup, 0); | |
db4283a0 MM |
8298 | if (flag_use_cxa_atexit) |
8299 | { | |
8300 | args = tree_cons (NULL_TREE, get_dso_handle_node (), NULL_TREE); | |
8301 | args = tree_cons (NULL_TREE, null_pointer_node, args); | |
8302 | args = tree_cons (NULL_TREE, cleanup, args); | |
8303 | } | |
8304 | else | |
8305 | args = tree_cons (NULL_TREE, cleanup, NULL_TREE); | |
8306 | finish_expr_stmt (build_function_call (get_atexit_node (), args)); | |
f0105ed3 MM |
8307 | } |
8308 | ||
8d08fdba MS |
8309 | void |
8310 | expand_static_init (decl, init) | |
8311 | tree decl; | |
8312 | tree init; | |
8313 | { | |
8314 | tree oldstatic = value_member (decl, static_aggregates); | |
a4443a08 | 8315 | |
8d08fdba MS |
8316 | if (oldstatic) |
8317 | { | |
8318 | if (TREE_PURPOSE (oldstatic) && init != NULL_TREE) | |
8251199e | 8319 | cp_error ("multiple initializations given for `%D'", decl); |
8d08fdba | 8320 | } |
0aafb128 | 8321 | else if (! toplevel_bindings_p ()) |
8d08fdba MS |
8322 | { |
8323 | /* Emit code to perform this initialization but once. */ | |
8324 | tree temp; | |
b7b8bcd2 | 8325 | tree if_stmt; |
f0105ed3 | 8326 | tree then_clause; |
f1dedc31 MM |
8327 | tree assignment; |
8328 | tree temp_init; | |
8d08fdba | 8329 | |
2036a15c MM |
8330 | /* Emit code to perform this initialization but once. This code |
8331 | looks like: | |
8332 | ||
8333 | static int temp = 0; | |
8334 | if (!temp) { | |
8335 | // Do initialization. | |
8336 | temp = 1; | |
8337 | // Register variable for destruction at end of program. | |
8338 | } | |
8339 | ||
8340 | Note that the `temp' variable is only set to 1 *after* the | |
8341 | initialization is complete. This ensures that an exception, | |
8342 | thrown during the construction, will cause the variable to | |
8343 | reinitialized when we pass through this code again, as per: | |
68642fb6 | 8344 | |
2036a15c MM |
8345 | [stmt.dcl] |
8346 | ||
8347 | If the initialization exits by throwing an exception, the | |
8348 | initialization is not complete, so it will be tried again | |
8349 | the next time control enters the declaration. | |
8350 | ||
8351 | In theory, this process should be thread-safe, too; multiple | |
8352 | threads should not be able to initialize the variable more | |
8353 | than once. We don't yet attempt to ensure thread-safety. */ | |
8d08fdba MS |
8354 | temp = get_temp_name (integer_type_node, 1); |
8355 | rest_of_decl_compilation (temp, NULL_PTR, 0, 0); | |
2036a15c MM |
8356 | |
8357 | /* Begin the conditional initialization. */ | |
b7b8bcd2 MM |
8358 | if_stmt = begin_if_stmt (); |
8359 | finish_if_stmt_cond (build_binary_op (EQ_EXPR, temp, | |
68642fb6 | 8360 | integer_zero_node), |
b7b8bcd2 | 8361 | if_stmt); |
f0105ed3 | 8362 | then_clause = begin_compound_stmt (/*has_no_scope=*/0); |
72b7eeff | 8363 | |
2036a15c | 8364 | /* Do the initialization itself. */ |
28cbf42c | 8365 | if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)) |
f30432d7 | 8366 | || (init && TREE_CODE (init) == TREE_LIST)) |
f1dedc31 | 8367 | assignment = build_aggr_init (decl, init, 0); |
f30432d7 | 8368 | else if (init) |
c557501d MM |
8369 | /* The initialization we're doing here is just a bitwise |
8370 | copy. */ | |
8371 | assignment = build (INIT_EXPR, TREE_TYPE (decl), decl, init); | |
f1dedc31 MM |
8372 | else |
8373 | assignment = NULL_TREE; | |
8374 | ||
8375 | /* Once the assignment is complete, set TEMP to 1. Since the | |
8376 | construction of the static object is complete at this point, | |
8377 | we want to make sure TEMP is set to 1 even if a temporary | |
8378 | constructed during the initialization throws an exception | |
8379 | when it is destroyed. So, we combine the initialization and | |
8380 | the assignment to TEMP into a single expression, ensuring | |
8381 | that when we call finish_expr_stmt the cleanups will not be | |
8382 | run until after TEMP is set to 1. */ | |
8383 | temp_init = build_modify_expr (temp, NOP_EXPR, integer_one_node); | |
8384 | if (assignment) | |
8385 | { | |
8386 | assignment = tree_cons (NULL_TREE, assignment, | |
68642fb6 | 8387 | build_tree_list (NULL_TREE, |
f1dedc31 MM |
8388 | temp_init)); |
8389 | assignment = build_compound_expr (assignment); | |
8390 | } | |
8391 | else | |
8392 | assignment = temp_init; | |
8393 | finish_expr_stmt (assignment); | |
72b7eeff | 8394 | |
2036a15c MM |
8395 | /* Use atexit to register a function for destroying this static |
8396 | variable. */ | |
bf419747 | 8397 | register_dtor_fn (decl); |
72b7eeff | 8398 | |
f0105ed3 | 8399 | finish_compound_stmt (/*has_no_scope=*/0, then_clause); |
b7b8bcd2 MM |
8400 | finish_then_clause (if_stmt); |
8401 | finish_if_stmt (); | |
8d08fdba MS |
8402 | } |
8403 | else | |
bbd15aac | 8404 | static_aggregates = tree_cons (init, decl, static_aggregates); |
8d08fdba | 8405 | } |
3c5c0849 MM |
8406 | |
8407 | /* Finish the declaration of a catch-parameter. */ | |
8408 | ||
b35d4555 | 8409 | tree |
3c5c0849 MM |
8410 | start_handler_parms (declspecs, declarator) |
8411 | tree declspecs; | |
8412 | tree declarator; | |
8413 | { | |
8414 | tree decl; | |
8415 | if (declspecs) | |
8416 | { | |
8417 | decl = grokdeclarator (declarator, declspecs, CATCHPARM, | |
8418 | 1, NULL_TREE); | |
8419 | if (decl == NULL_TREE) | |
8420 | error ("invalid catch parameter"); | |
8421 | } | |
8422 | else | |
8423 | decl = NULL_TREE; | |
b35d4555 MM |
8424 | |
8425 | return decl; | |
3c5c0849 MM |
8426 | } |
8427 | ||
8d08fdba MS |
8428 | \f |
8429 | /* Make TYPE a complete type based on INITIAL_VALUE. | |
8430 | Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered, | |
6ab5c740 | 8431 | 2 if there was no information (in which case assume 0 if DO_DEFAULT). */ |
8d08fdba MS |
8432 | |
8433 | int | |
8434 | complete_array_type (type, initial_value, do_default) | |
8435 | tree type, initial_value; | |
8436 | int do_default; | |
8437 | { | |
8438 | register tree maxindex = NULL_TREE; | |
8439 | int value = 0; | |
68642fb6 | 8440 | |
8d08fdba MS |
8441 | if (initial_value) |
8442 | { | |
8443 | /* Note MAXINDEX is really the maximum index, | |
8444 | one less than the size. */ | |
8445 | if (TREE_CODE (initial_value) == STRING_CST) | |
e1cd6e56 MS |
8446 | { |
8447 | int eltsize | |
8448 | = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value))); | |
8449 | maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value) | |
8450 | / eltsize) - 1, 0); | |
8451 | } | |
8d08fdba MS |
8452 | else if (TREE_CODE (initial_value) == CONSTRUCTOR) |
8453 | { | |
e1cd6e56 | 8454 | tree elts = CONSTRUCTOR_ELTS (initial_value); |
fed3cef0 RK |
8455 | |
8456 | maxindex = ssize_int (-1); | |
e1cd6e56 MS |
8457 | for (; elts; elts = TREE_CHAIN (elts)) |
8458 | { | |
8459 | if (TREE_PURPOSE (elts)) | |
8460 | maxindex = TREE_PURPOSE (elts); | |
8461 | else | |
fed3cef0 | 8462 | maxindex = size_binop (PLUS_EXPR, maxindex, ssize_int (1)); |
e1cd6e56 MS |
8463 | } |
8464 | maxindex = copy_node (maxindex); | |
8d08fdba MS |
8465 | } |
8466 | else | |
8467 | { | |
8468 | /* Make an error message unless that happened already. */ | |
8469 | if (initial_value != error_mark_node) | |
8470 | value = 1; | |
0db982be ML |
8471 | else |
8472 | initial_value = NULL_TREE; | |
8d08fdba MS |
8473 | |
8474 | /* Prevent further error messages. */ | |
8475 | maxindex = build_int_2 (0, 0); | |
8476 | } | |
8477 | } | |
8478 | ||
8479 | if (!maxindex) | |
8480 | { | |
8481 | if (do_default) | |
8482 | maxindex = build_int_2 (0, 0); | |
8483 | value = 2; | |
8484 | } | |
8485 | ||
8486 | if (maxindex) | |
8487 | { | |
51c184be | 8488 | tree itype; |
6ab5c740 NS |
8489 | tree domain; |
8490 | ||
8491 | domain = build_index_type (maxindex); | |
8492 | TYPE_DOMAIN (type) = domain; | |
51c184be | 8493 | |
dff6b454 | 8494 | if (! TREE_TYPE (maxindex)) |
6ab5c740 | 8495 | TREE_TYPE (maxindex) = domain; |
51c184be MS |
8496 | if (initial_value) |
8497 | itype = TREE_TYPE (initial_value); | |
8498 | else | |
8499 | itype = NULL; | |
8500 | if (itype && !TYPE_DOMAIN (itype)) | |
6ab5c740 | 8501 | TYPE_DOMAIN (itype) = domain; |
dff6b454 RK |
8502 | /* The type of the main variant should never be used for arrays |
8503 | of different sizes. It should only ever be completed with the | |
8504 | size of the array. */ | |
8505 | if (! TYPE_DOMAIN (TYPE_MAIN_VARIANT (type))) | |
6ab5c740 | 8506 | TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)) = domain; |
8d08fdba MS |
8507 | } |
8508 | ||
8509 | /* Lay out the type now that we can get the real answer. */ | |
8510 | ||
8511 | layout_type (type); | |
8512 | ||
8513 | return value; | |
8514 | } | |
8515 | \f | |
8516 | /* Return zero if something is declared to be a member of type | |
8517 | CTYPE when in the context of CUR_TYPE. STRING is the error | |
8518 | message to print in that case. Otherwise, quietly return 1. */ | |
e92cc029 | 8519 | |
8d08fdba | 8520 | static int |
4dacf5bd | 8521 | member_function_or_else (ctype, cur_type, flags) |
8d08fdba | 8522 | tree ctype, cur_type; |
4dacf5bd | 8523 | enum overload_flags flags; |
8d08fdba MS |
8524 | { |
8525 | if (ctype && ctype != cur_type) | |
8526 | { | |
4dacf5bd KG |
8527 | if (flags == DTOR_FLAG) |
8528 | error ("destructor for alien class `%s' cannot be a member", | |
8529 | TYPE_NAME_STRING (ctype)); | |
8530 | else | |
8531 | error ("constructor for alien class `%s' cannot be a member", | |
8532 | TYPE_NAME_STRING (ctype)); | |
8d08fdba MS |
8533 | return 0; |
8534 | } | |
8535 | return 1; | |
8536 | } | |
8537 | \f | |
8538 | /* Subroutine of `grokdeclarator'. */ | |
8539 | ||
8540 | /* Generate errors possibly applicable for a given set of specifiers. | |
8541 | This is for ARM $7.1.2. */ | |
e92cc029 | 8542 | |
8d08fdba MS |
8543 | static void |
8544 | bad_specifiers (object, type, virtualp, quals, inlinep, friendp, raises) | |
8545 | tree object; | |
d8e178a0 | 8546 | const char *type; |
8d08fdba MS |
8547 | int virtualp, quals, friendp, raises, inlinep; |
8548 | { | |
8549 | if (virtualp) | |
8251199e | 8550 | cp_error ("`%D' declared as a `virtual' %s", object, type); |
8d08fdba | 8551 | if (inlinep) |
8251199e | 8552 | cp_error ("`%D' declared as an `inline' %s", object, type); |
8d08fdba | 8553 | if (quals) |
8251199e | 8554 | cp_error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration", |
a28e3c7f | 8555 | object, type); |
8d08fdba | 8556 | if (friendp) |
f8e55f34 | 8557 | cp_error_at ("`%D' declared as a friend", object); |
f3e4d63c | 8558 | if (raises) |
f8e55f34 | 8559 | cp_error_at ("`%D' declared with an exception specification", object); |
8d08fdba MS |
8560 | } |
8561 | ||
8562 | /* CTYPE is class type, or null if non-class. | |
8563 | TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE | |
8564 | or METHOD_TYPE. | |
8565 | DECLARATOR is the function's name. | |
8566 | VIRTUALP is truthvalue of whether the function is virtual or not. | |
8567 | FLAGS are to be passed through to `grokclassfn'. | |
8568 | QUALS are qualifiers indicating whether the function is `const' | |
8569 | or `volatile'. | |
8570 | RAISES is a list of exceptions that this function can raise. | |
8571 | CHECK is 1 if we must find this method in CTYPE, 0 if we should | |
68642fb6 | 8572 | not look, and -1 if we should not call `grokclassfn' at all. |
3ddfb0e6 | 8573 | |
20496fa2 | 8574 | Returns `NULL_TREE' if something goes wrong, after issuing |
3ddfb0e6 | 8575 | applicable error messages. */ |
e92cc029 | 8576 | |
8d08fdba | 8577 | static tree |
386b8a85 | 8578 | grokfndecl (ctype, type, declarator, orig_declarator, virtualp, flags, quals, |
7a8f9fa9 | 8579 | raises, check, friendp, publicp, inlinep, funcdef_flag, |
2c73f9f5 | 8580 | template_count, in_namespace) |
8d08fdba MS |
8581 | tree ctype, type; |
8582 | tree declarator; | |
386b8a85 | 8583 | tree orig_declarator; |
8d08fdba MS |
8584 | int virtualp; |
8585 | enum overload_flags flags; | |
7a8f9fa9 | 8586 | tree quals, raises; |
386b8a85 | 8587 | int check, friendp, publicp, inlinep, funcdef_flag, template_count; |
2c73f9f5 | 8588 | tree in_namespace; |
8d08fdba MS |
8589 | { |
8590 | tree cname, decl; | |
8591 | int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE; | |
856216bb | 8592 | int has_default_arg = 0; |
42976354 | 8593 | tree t; |
8d08fdba MS |
8594 | |
8595 | if (ctype) | |
8596 | cname = TREE_CODE (TYPE_NAME (ctype)) == TYPE_DECL | |
8597 | ? TYPE_IDENTIFIER (ctype) : TYPE_NAME (ctype); | |
8598 | else | |
8599 | cname = NULL_TREE; | |
8600 | ||
8601 | if (raises) | |
8602 | { | |
f30432d7 | 8603 | type = build_exception_variant (type, raises); |
8d08fdba | 8604 | } |
c11b6f21 | 8605 | |
8d08fdba | 8606 | decl = build_lang_decl (FUNCTION_DECL, declarator, type); |
2c73f9f5 | 8607 | /* Propagate volatile out from type to decl. */ |
8d08fdba | 8608 | if (TYPE_VOLATILE (type)) |
893de33c | 8609 | TREE_THIS_VOLATILE (decl) = 1; |
8d08fdba | 8610 | |
79c4d4b7 | 8611 | /* If this decl has namespace scope, set that up. */ |
2c73f9f5 | 8612 | if (in_namespace) |
b262d64c | 8613 | set_decl_namespace (decl, in_namespace, friendp); |
79c4d4b7 JM |
8614 | else if (publicp && ! ctype) |
8615 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); | |
2c73f9f5 | 8616 | |
0f8766b8 JM |
8617 | /* `main' and builtins have implicit 'C' linkage. */ |
8618 | if ((MAIN_NAME_P (declarator) | |
8619 | || (IDENTIFIER_LENGTH (declarator) > 10 | |
8620 | && IDENTIFIER_POINTER (declarator)[0] == '_' | |
8621 | && IDENTIFIER_POINTER (declarator)[1] == '_' | |
8622 | && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0)) | |
8623 | && current_lang_name == lang_name_cplusplus | |
94706a5c | 8624 | && ctype == NULL_TREE |
79c4d4b7 JM |
8625 | /* NULL_TREE means global namespace. */ |
8626 | && DECL_CONTEXT (decl) == NULL_TREE) | |
0f8766b8 JM |
8627 | DECL_LANGUAGE (decl) = lang_c; |
8628 | ||
8d08fdba MS |
8629 | /* Should probably propagate const out from type to decl I bet (mrs). */ |
8630 | if (staticp) | |
8631 | { | |
8632 | DECL_STATIC_FUNCTION_P (decl) = 1; | |
8633 | DECL_CONTEXT (decl) = ctype; | |
8d08fdba MS |
8634 | } |
8635 | ||
e76a2646 | 8636 | if (ctype) |
4f1c5b7d | 8637 | DECL_CONTEXT (decl) = ctype; |
e76a2646 | 8638 | |
0f8766b8 | 8639 | if (ctype == NULL_TREE && DECL_MAIN_P (decl)) |
faae18ab | 8640 | { |
848b92e1 | 8641 | if (processing_template_decl) |
cb9a3ff8 | 8642 | error ("cannot declare `::main' to be a template"); |
faae18ab | 8643 | if (inlinep) |
cb9a3ff8 | 8644 | error ("cannot declare `::main' to be inline"); |
faae18ab | 8645 | else if (! publicp) |
cb9a3ff8 | 8646 | error ("cannot declare `::main' to be static"); |
faae18ab MS |
8647 | inlinep = 0; |
8648 | publicp = 1; | |
8649 | } | |
50a6dbd7 | 8650 | |
59e76fc6 JM |
8651 | /* Members of anonymous types and local classes have no linkage; make |
8652 | them internal. */ | |
8653 | if (ctype && (ANON_AGGRNAME_P (TYPE_IDENTIFIER (ctype)) | |
4f1c5b7d | 8654 | || decl_function_context (TYPE_MAIN_DECL (ctype)))) |
50a6dbd7 JM |
8655 | publicp = 0; |
8656 | ||
8657 | if (publicp) | |
8658 | { | |
8659 | /* [basic.link]: A name with no linkage (notably, the name of a class | |
8660 | or enumeration declared in a local scope) shall not be used to | |
8661 | declare an entity with linkage. | |
8662 | ||
8663 | Only check this for public decls for now. */ | |
8664 | t = no_linkage_check (TREE_TYPE (decl)); | |
8665 | if (t) | |
8666 | { | |
7f7c930e JM |
8667 | if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t))) |
8668 | { | |
8669 | if (DECL_LANGUAGE (decl) == lang_c) | |
8670 | /* Allow this; it's pretty common in C. */; | |
8671 | else | |
8672 | cp_pedwarn ("non-local function `%#D' uses anonymous type", | |
8673 | decl); | |
8674 | } | |
50a6dbd7 | 8675 | else |
8251199e | 8676 | cp_pedwarn ("non-local function `%#D' uses local type `%T'", |
cce2be43 | 8677 | decl, t); |
50a6dbd7 JM |
8678 | } |
8679 | } | |
8680 | ||
893de33c | 8681 | TREE_PUBLIC (decl) = publicp; |
faae18ab | 8682 | if (! publicp) |
893de33c JM |
8683 | { |
8684 | DECL_INTERFACE_KNOWN (decl) = 1; | |
8685 | DECL_NOT_REALLY_EXTERN (decl) = 1; | |
8686 | } | |
faae18ab MS |
8687 | |
8688 | if (inlinep) | |
8689 | DECL_THIS_INLINE (decl) = DECL_INLINE (decl) = 1; | |
8d08fdba MS |
8690 | |
8691 | DECL_EXTERNAL (decl) = 1; | |
8692 | if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE) | |
8693 | { | |
8251199e | 8694 | cp_error ("%smember function `%D' cannot have `%T' method qualifier", |
8d08fdba MS |
8695 | (ctype ? "static " : "non-"), decl, TREE_VALUE (quals)); |
8696 | quals = NULL_TREE; | |
8697 | } | |
8698 | ||
8699 | if (IDENTIFIER_OPNAME_P (DECL_NAME (decl))) | |
8700 | grok_op_properties (decl, virtualp, check < 0); | |
8701 | ||
4f1c5b7d | 8702 | if (ctype && decl_function_context (decl)) |
893de33c | 8703 | DECL_NO_STATIC_CHAIN (decl) = 1; |
e76a2646 | 8704 | |
42976354 BK |
8705 | for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t)) |
8706 | if (TREE_PURPOSE (t) | |
8707 | && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG) | |
8708 | { | |
856216bb | 8709 | has_default_arg = 1; |
42976354 BK |
8710 | break; |
8711 | } | |
8712 | ||
f9d94ea4 JM |
8713 | if (friendp |
8714 | && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR) | |
8715 | { | |
8716 | if (funcdef_flag) | |
8251199e JM |
8717 | cp_error |
8718 | ("defining explicit specialization `%D' in friend declaration", | |
f9d94ea4 JM |
8719 | orig_declarator); |
8720 | else | |
8721 | { | |
7e2421f7 MM |
8722 | if (PROCESSING_REAL_TEMPLATE_DECL_P ()) |
8723 | { | |
8724 | /* Something like `template <class T> friend void f<T>()'. */ | |
cb9a3ff8 | 8725 | cp_error ("invalid use of template-id `%D' in declaration of primary template", |
7e2421f7 | 8726 | orig_declarator); |
20496fa2 | 8727 | return NULL_TREE; |
7e2421f7 MM |
8728 | } |
8729 | ||
856216bb | 8730 | |
f9d94ea4 JM |
8731 | /* A friend declaration of the form friend void f<>(). Record |
8732 | the information in the TEMPLATE_ID_EXPR. */ | |
8733 | SET_DECL_IMPLICIT_INSTANTIATION (decl); | |
8734 | DECL_TEMPLATE_INFO (decl) | |
e1b3e07d MM |
8735 | = tree_cons (TREE_OPERAND (orig_declarator, 0), |
8736 | TREE_OPERAND (orig_declarator, 1), | |
8737 | NULL_TREE); | |
856216bb MM |
8738 | |
8739 | if (has_default_arg) | |
8740 | { | |
8741 | cp_error ("default arguments are not allowed in declaration of friend template specialization `%D'", | |
8742 | decl); | |
8743 | return NULL_TREE; | |
8744 | } | |
8745 | ||
8746 | if (inlinep) | |
8747 | { | |
68642fb6 | 8748 | cp_error ("`inline' is not allowed in declaration of friend template specialization `%D'", |
856216bb MM |
8749 | decl); |
8750 | return NULL_TREE; | |
8751 | } | |
f9d94ea4 | 8752 | } |
f84b4be9 | 8753 | } |
386b8a85 | 8754 | |
856216bb MM |
8755 | if (has_default_arg) |
8756 | add_defarg_fn (decl); | |
8757 | ||
0f8766b8 JM |
8758 | /* Plain overloading: will not be grok'd by grokclassfn. */ |
8759 | if (! ctype && ! processing_template_decl | |
8760 | && DECL_LANGUAGE (decl) != lang_c | |
8761 | && (! DECL_USE_TEMPLATE (decl) || name_mangling_version < 1)) | |
8762 | set_mangled_name_for_decl (decl); | |
8763 | ||
1eb0072d JM |
8764 | if (funcdef_flag) |
8765 | /* Make the init_value nonzero so pushdecl knows this is not | |
8766 | tentative. error_mark_node is replaced later with the BLOCK. */ | |
8767 | DECL_INITIAL (decl) = error_mark_node; | |
8768 | ||
93ca4ba7 | 8769 | if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl)) |
1660cb3a JM |
8770 | TREE_NOTHROW (decl) = 1; |
8771 | ||
75650646 | 8772 | /* Caller will do the rest of this. */ |
8d08fdba MS |
8773 | if (check < 0) |
8774 | return decl; | |
8775 | ||
8776 | if (flags == NO_SPECIAL && ctype && constructor_name (cname) == declarator) | |
74b846e0 MM |
8777 | DECL_CONSTRUCTOR_P (decl) = 1; |
8778 | ||
8779 | /* Function gets the ugly name, field gets the nice one. This call | |
8780 | may change the type of the function (because of default | |
8781 | parameters)! */ | |
8782 | if (ctype != NULL_TREE) | |
8783 | grokclassfn (ctype, decl, flags, quals); | |
8784 | ||
8785 | decl = check_explicit_specialization (orig_declarator, decl, | |
8786 | template_count, | |
8787 | 2 * (funcdef_flag != 0) + | |
8788 | 4 * (friendp != 0)); | |
8789 | if (decl == error_mark_node) | |
8790 | return NULL_TREE; | |
98c1c668 | 8791 | |
74b846e0 MM |
8792 | if (ctype != NULL_TREE |
8793 | && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl)) | |
8794 | && check) | |
8d08fdba | 8795 | { |
74b846e0 | 8796 | tree old_decl; |
8d08fdba | 8797 | |
74b846e0 | 8798 | old_decl = check_classfn (ctype, decl); |
8d08fdba | 8799 | |
74b846e0 MM |
8800 | if (old_decl && TREE_CODE (old_decl) == TEMPLATE_DECL) |
8801 | /* Because grokfndecl is always supposed to return a | |
8802 | FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT | |
8803 | here. We depend on our callers to figure out that its | |
8804 | really a template that's being returned. */ | |
8805 | old_decl = DECL_TEMPLATE_RESULT (old_decl); | |
6c30752f | 8806 | |
74b846e0 MM |
8807 | if (old_decl && DECL_STATIC_FUNCTION_P (old_decl) |
8808 | && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE) | |
5566b478 | 8809 | { |
74b846e0 MM |
8810 | /* Remove the `this' parm added by grokclassfn. |
8811 | XXX Isn't this done in start_function, too? */ | |
3afb32a4 | 8812 | revert_static_member_fn (decl); |
74b846e0 | 8813 | last_function_parms = TREE_CHAIN (last_function_parms); |
5566b478 | 8814 | } |
74b846e0 MM |
8815 | if (old_decl && DECL_ARTIFICIAL (old_decl)) |
8816 | cp_error ("definition of implicitly-declared `%D'", old_decl); | |
8d08fdba | 8817 | |
74b846e0 | 8818 | if (old_decl) |
8d08fdba | 8819 | { |
74b846e0 MM |
8820 | /* Since we've smashed OLD_DECL to its |
8821 | DECL_TEMPLATE_RESULT, we must do the same to DECL. */ | |
8822 | if (TREE_CODE (decl) == TEMPLATE_DECL) | |
8823 | decl = DECL_TEMPLATE_RESULT (decl); | |
8824 | ||
8825 | /* Attempt to merge the declarations. This can fail, in | |
8826 | the case of some illegal specialization declarations. */ | |
8827 | if (!duplicate_decls (decl, old_decl)) | |
8828 | cp_error ("no `%#D' member function declared in class `%T'", | |
8829 | decl, ctype); | |
8830 | return old_decl; | |
8d08fdba MS |
8831 | } |
8832 | } | |
74b846e0 MM |
8833 | |
8834 | if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl)) | |
8835 | return NULL_TREE; | |
8836 | ||
8837 | if (ctype == NULL_TREE || check) | |
8838 | return decl; | |
8839 | ||
8840 | if (virtualp) | |
8841 | { | |
8842 | DECL_VIRTUAL_P (decl) = 1; | |
8843 | if (DECL_VINDEX (decl) == NULL_TREE) | |
8844 | DECL_VINDEX (decl) = error_mark_node; | |
8845 | IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1; | |
8846 | } | |
8847 | ||
8d08fdba MS |
8848 | return decl; |
8849 | } | |
8850 | ||
8851 | static tree | |
2c73f9f5 | 8852 | grokvardecl (type, declarator, specbits_in, initialized, constp, in_namespace) |
8d08fdba MS |
8853 | tree type; |
8854 | tree declarator; | |
d2e5ee5c | 8855 | RID_BIT_TYPE *specbits_in; |
8d08fdba | 8856 | int initialized; |
a9aedbc2 | 8857 | int constp; |
2c73f9f5 | 8858 | tree in_namespace; |
8d08fdba MS |
8859 | { |
8860 | tree decl; | |
f7da6097 MS |
8861 | RID_BIT_TYPE specbits; |
8862 | ||
8863 | specbits = *specbits_in; | |
8d08fdba MS |
8864 | |
8865 | if (TREE_CODE (type) == OFFSET_TYPE) | |
8866 | { | |
8867 | /* If you declare a static member so that it | |
8868 | can be initialized, the code will reach here. */ | |
5b605f68 MS |
8869 | tree basetype = TYPE_OFFSET_BASETYPE (type); |
8870 | type = TREE_TYPE (type); | |
4ce3d537 | 8871 | decl = build_lang_decl (VAR_DECL, declarator, type); |
5b605f68 | 8872 | DECL_CONTEXT (decl) = basetype; |
f376e137 | 8873 | DECL_ASSEMBLER_NAME (decl) = build_static_name (basetype, declarator); |
8d08fdba MS |
8874 | } |
8875 | else | |
30394414 | 8876 | { |
79c4d4b7 JM |
8877 | tree context; |
8878 | ||
8879 | if (in_namespace) | |
8880 | context = in_namespace; | |
8881 | else if (namespace_bindings_p () || RIDBIT_SETP (RID_EXTERN, specbits)) | |
8882 | context = current_namespace; | |
820fcad8 JM |
8883 | else |
8884 | context = NULL_TREE; | |
79c4d4b7 | 8885 | |
68642fb6 | 8886 | if (processing_template_decl) |
cd9f6678 MM |
8887 | /* If we're in a template, we need DECL_LANG_SPECIFIC so that |
8888 | we can call push_template_decl. */ | |
8889 | decl = build_lang_decl (VAR_DECL, declarator, type); | |
9188c363 | 8890 | else |
c82dbd95 | 8891 | decl = build_decl (VAR_DECL, declarator, type); |
79c4d4b7 JM |
8892 | |
8893 | if (context) | |
b262d64c | 8894 | set_decl_namespace (decl, context, 0); |
79c4d4b7 JM |
8895 | |
8896 | context = DECL_CONTEXT (decl); | |
8897 | if (declarator && context && current_lang_name != lang_name_c) | |
8898 | DECL_ASSEMBLER_NAME (decl) = build_static_name (context, declarator); | |
30394414 | 8899 | } |
6060a796 | 8900 | |
2c73f9f5 | 8901 | if (in_namespace) |
b262d64c | 8902 | set_decl_namespace (decl, in_namespace, 0); |
2c73f9f5 | 8903 | |
8d08fdba MS |
8904 | if (RIDBIT_SETP (RID_EXTERN, specbits)) |
8905 | { | |
8906 | DECL_THIS_EXTERN (decl) = 1; | |
8907 | DECL_EXTERNAL (decl) = !initialized; | |
8908 | } | |
8909 | ||
8910 | /* In class context, static means one per class, | |
8911 | public access, and static storage. */ | |
2b9dc906 | 8912 | if (DECL_CLASS_SCOPE_P (decl)) |
8d08fdba MS |
8913 | { |
8914 | TREE_PUBLIC (decl) = 1; | |
8915 | TREE_STATIC (decl) = 1; | |
5b605f68 | 8916 | DECL_EXTERNAL (decl) = 0; |
8d08fdba MS |
8917 | } |
8918 | /* At top level, either `static' or no s.c. makes a definition | |
8919 | (perhaps tentative), and absence of `static' makes it public. */ | |
a9aedbc2 | 8920 | else if (toplevel_bindings_p ()) |
8d08fdba | 8921 | { |
a9aedbc2 | 8922 | TREE_PUBLIC (decl) = (RIDBIT_NOTSETP (RID_STATIC, specbits) |
9fffd093 | 8923 | && (DECL_THIS_EXTERN (decl) || ! constp)); |
8d08fdba MS |
8924 | TREE_STATIC (decl) = ! DECL_EXTERNAL (decl); |
8925 | } | |
8926 | /* Not at top level, only `static' makes a static definition. */ | |
8927 | else | |
8928 | { | |
8929 | TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits); | |
8930 | TREE_PUBLIC (decl) = DECL_EXTERNAL (decl); | |
8931 | } | |
50a6dbd7 JM |
8932 | |
8933 | if (TREE_PUBLIC (decl)) | |
8934 | { | |
8935 | /* [basic.link]: A name with no linkage (notably, the name of a class | |
8936 | or enumeration declared in a local scope) shall not be used to | |
8937 | declare an entity with linkage. | |
8938 | ||
8939 | Only check this for public decls for now. */ | |
8940 | tree t = no_linkage_check (TREE_TYPE (decl)); | |
8941 | if (t) | |
8942 | { | |
8943 | if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t))) | |
8944 | /* Ignore for now; `enum { foo } e' is pretty common. */; | |
8945 | else | |
8251199e | 8946 | cp_pedwarn ("non-local variable `%#D' uses local type `%T'", |
50a6dbd7 JM |
8947 | decl, t); |
8948 | } | |
8949 | } | |
8950 | ||
8d08fdba MS |
8951 | return decl; |
8952 | } | |
8953 | ||
d8f8dca1 MM |
8954 | /* Create and return a canonical pointer to member function type, for |
8955 | TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */ | |
8d08fdba MS |
8956 | |
8957 | tree | |
8958 | build_ptrmemfunc_type (type) | |
8959 | tree type; | |
8960 | { | |
8961 | tree fields[4]; | |
8962 | tree t; | |
8963 | tree u; | |
46cbda4a | 8964 | tree unqualified_variant = NULL_TREE; |
8d08fdba MS |
8965 | |
8966 | /* If a canonical type already exists for this type, use it. We use | |
8967 | this method instead of type_hash_canon, because it only does a | |
8968 | simple equality check on the list of field members. */ | |
8969 | ||
8970 | if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type))) | |
8971 | return t; | |
8972 | ||
46cbda4a MM |
8973 | /* Make sure that we always have the unqualified pointer-to-member |
8974 | type first. */ | |
8975 | if (CP_TYPE_QUALS (type) != TYPE_UNQUALIFIED) | |
68642fb6 | 8976 | unqualified_variant |
46cbda4a MM |
8977 | = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type)); |
8978 | ||
33848bb0 | 8979 | t = make_aggr_type (RECORD_TYPE); |
2c73f9f5 | 8980 | /* Let the front-end know this is a pointer to member function... */ |
db5ae43f | 8981 | TYPE_PTRMEMFUNC_FLAG (t) = 1; |
2c73f9f5 | 8982 | /* ... and not really an aggregate. */ |
7ddedda4 | 8983 | SET_IS_AGGR_TYPE (t, 0); |
8d08fdba | 8984 | |
c7e266a6 MM |
8985 | if (!flag_new_abi) |
8986 | { | |
8987 | u = make_aggr_type (UNION_TYPE); | |
8988 | SET_IS_AGGR_TYPE (u, 0); | |
8989 | fields[0] = build_lang_decl (FIELD_DECL, pfn_identifier, type); | |
8990 | fields[1] = build_lang_decl (FIELD_DECL, delta2_identifier, | |
8991 | delta_type_node); | |
8992 | finish_builtin_type (u, "__ptrmemfunc_type", fields, 1, ptr_type_node); | |
8993 | TYPE_NAME (u) = NULL_TREE; | |
8994 | ||
8995 | fields[0] = build_lang_decl (FIELD_DECL, delta_identifier, | |
8996 | delta_type_node); | |
8997 | fields[1] = build_lang_decl (FIELD_DECL, index_identifier, | |
8998 | delta_type_node); | |
8999 | fields[2] = build_lang_decl (FIELD_DECL, pfn_or_delta2_identifier, u); | |
9000 | finish_builtin_type (t, "__ptrmemfunc_type", fields, 2, ptr_type_node); | |
9001 | } | |
9002 | else | |
9003 | { | |
9004 | fields[0] = build_lang_decl (FIELD_DECL, pfn_identifier, type); | |
9005 | fields[1] = build_lang_decl (FIELD_DECL, delta_identifier, | |
9006 | delta_type_node); | |
9007 | finish_builtin_type (t, "__ptrmemfunc_type", fields, 1, ptr_type_node); | |
9008 | } | |
8d08fdba | 9009 | |
8d08fdba MS |
9010 | /* Zap out the name so that the back-end will give us the debugging |
9011 | information for this anonymous RECORD_TYPE. */ | |
9012 | TYPE_NAME (t) = NULL_TREE; | |
9013 | ||
46cbda4a MM |
9014 | /* If this is not the unqualified form of this pointer-to-member |
9015 | type, set the TYPE_MAIN_VARIANT for this type to be the | |
9016 | unqualified type. Since they are actually RECORD_TYPEs that are | |
9017 | not variants of each other, we must do this manually. */ | |
9018 | if (CP_TYPE_QUALS (type) != TYPE_UNQUALIFIED) | |
9019 | { | |
9020 | t = build_qualified_type (t, CP_TYPE_QUALS (type)); | |
9021 | TYPE_MAIN_VARIANT (t) = unqualified_variant; | |
9022 | TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant); | |
9023 | TYPE_NEXT_VARIANT (unqualified_variant) = t; | |
9024 | } | |
9025 | ||
9026 | /* Cache this pointer-to-member type so that we can find it again | |
9027 | later. */ | |
8d08fdba MS |
9028 | TYPE_SET_PTRMEMFUNC_TYPE (type, t); |
9029 | ||
e92cc029 | 9030 | /* Seems to be wanted. */ |
8d08fdba | 9031 | CLASSTYPE_GOT_SEMICOLON (t) = 1; |
46cbda4a | 9032 | |
8d08fdba MS |
9033 | return t; |
9034 | } | |
9035 | ||
b17e2870 JM |
9036 | /* DECL is a VAR_DECL defined in-class, whose TYPE is also given. |
9037 | Check to see that the definition is valid. Issue appropriate error | |
9038 | messages. Return 1 if the definition is particularly bad, or 0 | |
9039 | otherwise. */ | |
9040 | ||
9041 | int | |
9042 | check_static_variable_definition (decl, type) | |
9043 | tree decl; | |
9044 | tree type; | |
9045 | { | |
9046 | /* Motion 10 at San Diego: If a static const integral data member is | |
9047 | initialized with an integral constant expression, the initializer | |
9048 | may appear either in the declaration (within the class), or in | |
9049 | the definition, but not both. If it appears in the class, the | |
9050 | member is a member constant. The file-scope definition is always | |
9051 | required. */ | |
9052 | if (CLASS_TYPE_P (type) || TREE_CODE (type) == REFERENCE_TYPE) | |
9053 | { | |
cb9a3ff8 | 9054 | cp_error ("invalid in-class initialization of static data member of non-integral type `%T'", |
b17e2870 JM |
9055 | type); |
9056 | /* If we just return the declaration, crashes will sometimes | |
9057 | occur. We therefore return void_type_node, as if this was a | |
9058 | friend declaration, to cause callers to completely ignore | |
9059 | this declaration. */ | |
9060 | return 1; | |
9061 | } | |
9062 | else if (!CP_TYPE_CONST_P (type)) | |
cb9a3ff8 | 9063 | cp_error ("ISO C++ forbids in-class initialization of non-const static member `%D'", |
b17e2870 JM |
9064 | decl); |
9065 | else if (pedantic && !INTEGRAL_TYPE_P (type)) | |
cb9a3ff8 | 9066 | cp_pedwarn ("ISO C++ forbids initialization of member constant `%D' of non-integral type `%T'", decl, type); |
b17e2870 JM |
9067 | |
9068 | return 0; | |
9069 | } | |
9070 | ||
2848ad0f MM |
9071 | /* Given the SIZE (i.e., number of elements) in an array, compute an |
9072 | appropriate index type for the array. If non-NULL, NAME is the | |
9073 | name of the thing being declared. */ | |
9074 | ||
c95cd22e | 9075 | tree |
2848ad0f MM |
9076 | compute_array_index_type (name, size) |
9077 | tree name; | |
9078 | tree size; | |
9079 | { | |
9080 | tree itype; | |
9081 | ||
9082 | /* The size might be the result of a cast. */ | |
9083 | STRIP_TYPE_NOPS (size); | |
9084 | ||
9085 | /* It might be a const variable or enumeration constant. */ | |
9086 | if (TREE_READONLY_DECL_P (size)) | |
9087 | size = decl_constant_value (size); | |
9088 | ||
9089 | /* If this involves a template parameter, it will be a constant at | |
9090 | instantiation time, but we don't know what the value is yet. | |
9091 | Even if no template parameters are involved, we may an expression | |
9092 | that is not a constant; we don't even simplify `1 + 2' when | |
9093 | processing a template. */ | |
9094 | if (processing_template_decl) | |
9095 | { | |
9096 | /* Resolve a qualified reference to an enumerator or static | |
9097 | const data member of ours. */ | |
9098 | if (TREE_CODE (size) == SCOPE_REF | |
9099 | && TREE_OPERAND (size, 0) == current_class_type) | |
9100 | { | |
9101 | tree t = lookup_field (current_class_type, | |
9102 | TREE_OPERAND (size, 1), 0, 0); | |
9103 | if (t) | |
9104 | size = t; | |
9105 | } | |
9106 | ||
9107 | return build_index_type (build_min (MINUS_EXPR, sizetype, | |
9108 | size, integer_one_node)); | |
9109 | } | |
9110 | ||
9111 | /* The array bound must be an integer type. */ | |
9112 | if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE | |
9113 | && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE | |
9114 | && TREE_CODE (TREE_TYPE (size)) != BOOLEAN_TYPE) | |
9115 | { | |
d67cdbc3 JM |
9116 | if (name) |
9117 | cp_error ("size of array `%D' has non-integer type", name); | |
9118 | else | |
9119 | cp_error ("size of array has non-integer type"); | |
2848ad0f MM |
9120 | size = integer_one_node; |
9121 | } | |
9122 | ||
9123 | /* Normally, the array-bound will be a constant. */ | |
9124 | if (TREE_CONSTANT (size)) | |
9125 | { | |
9126 | /* Check to see if the array bound overflowed. Make that an | |
9127 | error, no matter how generous we're being. */ | |
9128 | int old_flag_pedantic_errors = flag_pedantic_errors; | |
9129 | int old_pedantic = pedantic; | |
9130 | pedantic = flag_pedantic_errors = 1; | |
9131 | constant_expression_warning (size); | |
9132 | pedantic = old_pedantic; | |
9133 | flag_pedantic_errors = old_flag_pedantic_errors; | |
9134 | ||
9135 | /* An array must have a positive number of elements. */ | |
9136 | if (INT_CST_LT (size, integer_zero_node)) | |
9137 | { | |
d67cdbc3 JM |
9138 | if (name) |
9139 | cp_error ("size of array `%D' is negative", name); | |
9140 | else | |
9141 | cp_error ("size of array is negative"); | |
2848ad0f MM |
9142 | size = integer_one_node; |
9143 | } | |
9144 | /* Except that an extension we allow zero-sized arrays. We | |
68642fb6 | 9145 | always allow them in system headers because glibc uses |
2848ad0f MM |
9146 | them. */ |
9147 | else if (integer_zerop (size) && pedantic && !in_system_header) | |
d67cdbc3 JM |
9148 | { |
9149 | if (name) | |
cb9a3ff8 | 9150 | cp_pedwarn ("ISO C++ forbids zero-size array `%D'", name); |
d67cdbc3 | 9151 | else |
cb9a3ff8 | 9152 | cp_pedwarn ("ISO C++ forbids zero-size array"); |
d67cdbc3 | 9153 | } |
2848ad0f MM |
9154 | } |
9155 | ||
9156 | /* Compute the index of the largest element in the array. It is | |
9157 | one less than the number of elements in the array. */ | |
9158 | itype | |
9159 | = fold (build_binary_op (MINUS_EXPR, | |
9160 | cp_convert (ssizetype, size), | |
9161 | cp_convert (ssizetype, | |
9162 | integer_one_node))); | |
68642fb6 | 9163 | |
2848ad0f MM |
9164 | /* Check for variable-sized arrays. We allow such things as an |
9165 | extension, even though they are not allowed in ANSI/ISO C++. */ | |
9166 | if (!TREE_CONSTANT (itype)) | |
9167 | { | |
9168 | if (pedantic) | |
9169 | { | |
9170 | if (name) | |
cb9a3ff8 | 9171 | cp_pedwarn ("ISO C++ forbids variable-size array `%D'", |
2848ad0f MM |
9172 | name); |
9173 | else | |
cb9a3ff8 | 9174 | cp_pedwarn ("ISO C++ forbids variable-size array"); |
2848ad0f MM |
9175 | } |
9176 | ||
9177 | /* Create a variable-sized array index type. */ | |
9178 | itype = variable_size (itype); | |
9179 | } | |
9180 | /* Make sure that there was no overflow when creating to a signed | |
9181 | index type. (For example, on a 32-bit machine, an array with | |
9182 | size 2^32 - 1 is too big.) */ | |
9183 | else if (TREE_OVERFLOW (itype)) | |
9184 | { | |
9185 | error ("overflow in array dimension"); | |
9186 | TREE_OVERFLOW (itype) = 0; | |
9187 | } | |
68642fb6 | 9188 | |
2848ad0f MM |
9189 | /* Create and return the appropriate index type. */ |
9190 | return build_index_type (itype); | |
9191 | } | |
9192 | ||
9193 | /* Returns an ARRAY_TYPE for an array with SIZE elements of the | |
9194 | indicated TYPE. If non-NULL, NAME is the NAME of the declaration | |
9195 | with this type. */ | |
9196 | ||
9197 | static tree | |
9198 | create_array_type_for_decl (name, type, size) | |
9199 | tree name; | |
9200 | tree type; | |
9201 | tree size; | |
9202 | { | |
9203 | tree itype = NULL_TREE; | |
9204 | const char* error_msg; | |
9205 | ||
9206 | /* If things have already gone awry, bail now. */ | |
9207 | if (type == error_mark_node || size == error_mark_node) | |
9208 | return error_mark_node; | |
9209 | ||
9210 | /* Assume that everything will go OK. */ | |
9211 | error_msg = NULL; | |
9212 | ||
9213 | /* There are some types which cannot be array elements. */ | |
9214 | switch (TREE_CODE (type)) | |
9215 | { | |
9216 | case VOID_TYPE: | |
9217 | error_msg = "array of void"; | |
9218 | break; | |
9219 | ||
9220 | case FUNCTION_TYPE: | |
9221 | error_msg = "array of functions"; | |
9222 | break; | |
9223 | ||
9224 | case REFERENCE_TYPE: | |
9225 | error_msg = "array of references"; | |
9226 | break; | |
9227 | ||
9228 | case OFFSET_TYPE: | |
9229 | error_msg = "array of data members"; | |
9230 | break; | |
9231 | ||
9232 | case METHOD_TYPE: | |
9233 | error_msg = "array of function members"; | |
9234 | break; | |
9235 | ||
9236 | default: | |
9237 | break; | |
9238 | } | |
9239 | ||
9240 | /* If something went wrong, issue an error-message and return. */ | |
9241 | if (error_msg) | |
9242 | { | |
9243 | if (name) | |
9244 | cp_error ("declaration of `%D' as %s", name, error_msg); | |
9245 | else | |
9246 | cp_error ("creating %s", error_msg); | |
9247 | ||
9248 | return error_mark_node; | |
9249 | } | |
9250 | ||
9251 | /* [dcl.array] | |
68642fb6 | 9252 | |
2848ad0f MM |
9253 | The constant expressions that specify the bounds of the arrays |
9254 | can be omitted only for the first member of the sequence. */ | |
9255 | if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)) | |
9256 | { | |
cb9a3ff8 | 9257 | cp_error ("declaration of `%D' as multidimensional array must have bounds for all dimensions except the first", |
2848ad0f | 9258 | name); |
2848ad0f MM |
9259 | |
9260 | return error_mark_node; | |
9261 | } | |
9262 | ||
9263 | /* Figure out the index type for the array. */ | |
9264 | if (size) | |
9265 | itype = compute_array_index_type (name, size); | |
9266 | ||
9267 | return build_cplus_array_type (type, itype); | |
9268 | } | |
9269 | ||
8d08fdba MS |
9270 | /* Given declspecs and a declarator, |
9271 | determine the name and type of the object declared | |
9272 | and construct a ..._DECL node for it. | |
9273 | (In one case we can return a ..._TYPE node instead. | |
9274 | For invalid input we sometimes return 0.) | |
9275 | ||
9276 | DECLSPECS is a chain of tree_list nodes whose value fields | |
9277 | are the storage classes and type specifiers. | |
9278 | ||
9279 | DECL_CONTEXT says which syntactic context this declaration is in: | |
9280 | NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL. | |
9281 | FUNCDEF for a function definition. Like NORMAL but a few different | |
9282 | error messages in each case. Return value may be zero meaning | |
9283 | this definition is too screwy to try to parse. | |
9284 | MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to | |
9285 | handle member functions (which have FIELD context). | |
9286 | Return value may be zero meaning this definition is too screwy to | |
9287 | try to parse. | |
9288 | PARM for a parameter declaration (either within a function prototype | |
9289 | or before a function body). Make a PARM_DECL, or return void_type_node. | |
db5ae43f | 9290 | CATCHPARM for a parameter declaration before a catch clause. |
8d08fdba MS |
9291 | TYPENAME if for a typename (in a cast or sizeof). |
9292 | Don't make a DECL node; just return the ..._TYPE node. | |
9293 | FIELD for a struct or union field; make a FIELD_DECL. | |
9294 | BITFIELD for a field with specified width. | |
9295 | INITIALIZED is 1 if the decl has an initializer. | |
9296 | ||
b17e2870 JM |
9297 | ATTRLIST is a TREE_LIST node with prefix attributes in TREE_VALUE and |
9298 | normal attributes in TREE_PURPOSE, or NULL_TREE. | |
9299 | ||
70adf8a9 | 9300 | In the TYPENAME case, DECLARATOR is really an abstract declarator. |
8d08fdba MS |
9301 | It may also be so in the PARM case, for a prototype where the |
9302 | argument type is specified but not the name. | |
9303 | ||
9304 | This function is where the complicated C meanings of `static' | |
9305 | and `extern' are interpreted. | |
9306 | ||
9307 | For C++, if there is any monkey business to do, the function which | |
9308 | calls this one must do it, i.e., prepending instance variables, | |
9309 | renaming overloaded function names, etc. | |
9310 | ||
9311 | Note that for this C++, it is an error to define a method within a class | |
9312 | which does not belong to that class. | |
9313 | ||
9314 | Except in the case where SCOPE_REFs are implicitly known (such as | |
9315 | methods within a class being redundantly qualified), | |
9316 | declarations which involve SCOPE_REFs are returned as SCOPE_REFs | |
9317 | (class_name::decl_name). The caller must also deal with this. | |
9318 | ||
9319 | If a constructor or destructor is seen, and the context is FIELD, | |
9320 | then the type gains the attribute TREE_HAS_x. If such a declaration | |
9321 | is erroneous, NULL_TREE is returned. | |
9322 | ||
9323 | QUALS is used only for FUNCDEF and MEMFUNCDEF cases. For a member | |
535233a8 NS |
9324 | function, these are the qualifiers to give to the `this' pointer. We |
9325 | apply TYPE_QUAL_RESTRICT to the this ptr, not the object. | |
8d08fdba MS |
9326 | |
9327 | May return void_type_node if the declarator turned out to be a friend. | |
9328 | See grokfield for details. */ | |
9329 | ||
9330 | enum return_types { return_normal, return_ctor, return_dtor, return_conversion }; | |
9331 | ||
9332 | tree | |
c11b6f21 | 9333 | grokdeclarator (declarator, declspecs, decl_context, initialized, attrlist) |
8d08fdba MS |
9334 | tree declspecs; |
9335 | tree declarator; | |
9336 | enum decl_context decl_context; | |
9337 | int initialized; | |
c11b6f21 | 9338 | tree attrlist; |
8d08fdba MS |
9339 | { |
9340 | RID_BIT_TYPE specbits; | |
9341 | int nclasses = 0; | |
9342 | tree spec; | |
9343 | tree type = NULL_TREE; | |
9344 | int longlong = 0; | |
9345 | int constp; | |
91063b51 | 9346 | int restrictp; |
8d08fdba | 9347 | int volatilep; |
91063b51 | 9348 | int type_quals; |
db5ae43f | 9349 | int virtualp, explicitp, friendp, inlinep, staticp; |
8d08fdba MS |
9350 | int explicit_int = 0; |
9351 | int explicit_char = 0; | |
37c46b43 | 9352 | int defaulted_int = 0; |
8d08fdba | 9353 | tree typedef_decl = NULL_TREE; |
9c0758dd | 9354 | const char *name; |
8d08fdba MS |
9355 | tree typedef_type = NULL_TREE; |
9356 | int funcdef_flag = 0; | |
9357 | enum tree_code innermost_code = ERROR_MARK; | |
9358 | int bitfield = 0; | |
6125f3be DE |
9359 | #if 0 |
9360 | /* See the code below that used this. */ | |
f6abb50a | 9361 | tree decl_machine_attr = NULL_TREE; |
6125f3be | 9362 | #endif |
8d08fdba MS |
9363 | /* Set this to error_mark_node for FIELD_DECLs we could not handle properly. |
9364 | All FIELD_DECLs we build here have `init' put into their DECL_INITIAL. */ | |
9365 | tree init = NULL_TREE; | |
9366 | ||
9367 | /* Keep track of what sort of function is being processed | |
9368 | so that we can warn about default return values, or explicit | |
9369 | return values which do not match prescribed defaults. */ | |
9370 | enum return_types return_type = return_normal; | |
9371 | ||
9372 | tree dname = NULL_TREE; | |
9373 | tree ctype = current_class_type; | |
9374 | tree ctor_return_type = NULL_TREE; | |
9375 | enum overload_flags flags = NO_SPECIAL; | |
8d08fdba | 9376 | tree quals = NULL_TREE; |
c11b6f21 | 9377 | tree raises = NULL_TREE; |
386b8a85 | 9378 | int template_count = 0; |
2c73f9f5 | 9379 | tree in_namespace = NULL_TREE; |
1eab9b56 JM |
9380 | tree inner_attrs; |
9381 | int ignore_attrs; | |
8d08fdba MS |
9382 | |
9383 | RIDBIT_RESET_ALL (specbits); | |
9384 | if (decl_context == FUNCDEF) | |
9385 | funcdef_flag = 1, decl_context = NORMAL; | |
9386 | else if (decl_context == MEMFUNCDEF) | |
9387 | funcdef_flag = -1, decl_context = FIELD; | |
9388 | else if (decl_context == BITFIELD) | |
9389 | bitfield = 1, decl_context = FIELD; | |
9390 | ||
8d08fdba MS |
9391 | /* Look inside a declarator for the name being declared |
9392 | and get it as a string, for an error message. */ | |
9393 | { | |
be99da77 MS |
9394 | tree *next = &declarator; |
9395 | register tree decl; | |
8d08fdba MS |
9396 | name = NULL; |
9397 | ||
be99da77 MS |
9398 | while (next && *next) |
9399 | { | |
9400 | decl = *next; | |
9401 | switch (TREE_CODE (decl)) | |
8d08fdba | 9402 | { |
52fbc847 JM |
9403 | case TREE_LIST: |
9404 | /* For attributes. */ | |
9405 | next = &TREE_VALUE (decl); | |
9406 | break; | |
9407 | ||
be99da77 MS |
9408 | case COND_EXPR: |
9409 | ctype = NULL_TREE; | |
9410 | next = &TREE_OPERAND (decl, 0); | |
9411 | break; | |
8d08fdba | 9412 | |
2c73f9f5 | 9413 | case BIT_NOT_EXPR: /* For C++ destructors! */ |
8d08fdba | 9414 | { |
be99da77 MS |
9415 | tree name = TREE_OPERAND (decl, 0); |
9416 | tree rename = NULL_TREE; | |
9417 | ||
9418 | my_friendly_assert (flags == NO_SPECIAL, 152); | |
9419 | flags = DTOR_FLAG; | |
9420 | return_type = return_dtor; | |
5566b478 MS |
9421 | if (TREE_CODE (name) == TYPE_DECL) |
9422 | TREE_OPERAND (decl, 0) = name = constructor_name (name); | |
be99da77 MS |
9423 | my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153); |
9424 | if (ctype == NULL_TREE) | |
9425 | { | |
9426 | if (current_class_type == NULL_TREE) | |
9427 | { | |
8251199e | 9428 | error ("destructors must be member functions"); |
be99da77 MS |
9429 | flags = NO_SPECIAL; |
9430 | } | |
9431 | else | |
9432 | { | |
9433 | tree t = constructor_name (current_class_name); | |
9434 | if (t != name) | |
9435 | rename = t; | |
9436 | } | |
9437 | } | |
8d08fdba | 9438 | else |
be99da77 MS |
9439 | { |
9440 | tree t = constructor_name (ctype); | |
9441 | if (t != name) | |
9442 | rename = t; | |
9443 | } | |
51c184be | 9444 | |
be99da77 | 9445 | if (rename) |
39211cd5 | 9446 | { |
8251199e | 9447 | cp_error ("destructor `%T' must match class name `%T'", |
5566b478 | 9448 | name, rename); |
be99da77 | 9449 | TREE_OPERAND (decl, 0) = rename; |
39211cd5 | 9450 | } |
be99da77 | 9451 | next = &name; |
51c184be | 9452 | } |
be99da77 | 9453 | break; |
8d08fdba | 9454 | |
be99da77 | 9455 | case ADDR_EXPR: /* C++ reference declaration */ |
2c73f9f5 | 9456 | /* Fall through. */ |
be99da77 MS |
9457 | case ARRAY_REF: |
9458 | case INDIRECT_REF: | |
9459 | ctype = NULL_TREE; | |
9460 | innermost_code = TREE_CODE (decl); | |
9461 | next = &TREE_OPERAND (decl, 0); | |
9462 | break; | |
8d08fdba | 9463 | |
be99da77 | 9464 | case CALL_EXPR: |
43f887f9 | 9465 | if (parmlist_is_exprlist (CALL_DECLARATOR_PARMS (decl))) |
8d08fdba | 9466 | { |
b17e2870 JM |
9467 | /* This is actually a variable declaration using |
9468 | constructor syntax. We need to call start_decl and | |
9469 | cp_finish_decl so we can get the variable | |
9470 | initialized... */ | |
9471 | ||
9472 | tree attributes, prefix_attributes; | |
be99da77 MS |
9473 | |
9474 | *next = TREE_OPERAND (decl, 0); | |
43f887f9 | 9475 | init = CALL_DECLARATOR_PARMS (decl); |
be99da77 | 9476 | |
b17e2870 JM |
9477 | if (attrlist) |
9478 | { | |
9479 | attributes = TREE_PURPOSE (attrlist); | |
9480 | prefix_attributes = TREE_VALUE (attrlist); | |
9481 | } | |
9482 | else | |
9483 | { | |
9484 | attributes = NULL_TREE; | |
9485 | prefix_attributes = NULL_TREE; | |
9486 | } | |
9487 | ||
9488 | decl = start_decl (declarator, declspecs, 1, | |
9489 | attributes, prefix_attributes); | |
9fc336c7 | 9490 | decl_type_access_control (decl); |
1eb0072d JM |
9491 | if (decl) |
9492 | { | |
9493 | /* Look for __unused__ attribute */ | |
9494 | if (TREE_USED (TREE_TYPE (decl))) | |
9495 | TREE_USED (decl) = 1; | |
9496 | finish_decl (decl, init, NULL_TREE); | |
9497 | } | |
9498 | else | |
9499 | cp_error ("invalid declarator"); | |
be99da77 | 9500 | return 0; |
8d08fdba | 9501 | } |
be99da77 MS |
9502 | innermost_code = TREE_CODE (decl); |
9503 | if (decl_context == FIELD && ctype == NULL_TREE) | |
9504 | ctype = current_class_type; | |
45537677 | 9505 | if (ctype |
c11b6f21 | 9506 | && TREE_OPERAND (decl, 0) |
45537677 MS |
9507 | && (TREE_CODE (TREE_OPERAND (decl, 0)) == TYPE_DECL |
9508 | && ((DECL_NAME (TREE_OPERAND (decl, 0)) | |
9509 | == constructor_name_full (ctype)) | |
9510 | || (DECL_NAME (TREE_OPERAND (decl, 0)) | |
9511 | == constructor_name (ctype))))) | |
be99da77 MS |
9512 | TREE_OPERAND (decl, 0) = constructor_name (ctype); |
9513 | next = &TREE_OPERAND (decl, 0); | |
9514 | decl = *next; | |
9515 | if (ctype != NULL_TREE | |
9516 | && decl != NULL_TREE && flags != DTOR_FLAG | |
9517 | && decl == constructor_name (ctype)) | |
8d08fdba | 9518 | { |
be99da77 MS |
9519 | return_type = return_ctor; |
9520 | ctor_return_type = ctype; | |
8d08fdba | 9521 | } |
be99da77 MS |
9522 | ctype = NULL_TREE; |
9523 | break; | |
68642fb6 | 9524 | |
386b8a85 JM |
9525 | case TEMPLATE_ID_EXPR: |
9526 | { | |
9527 | tree fns = TREE_OPERAND (decl, 0); | |
9528 | ||
9529 | if (TREE_CODE (fns) == LOOKUP_EXPR) | |
9530 | fns = TREE_OPERAND (fns, 0); | |
9531 | ||
8f032717 MM |
9532 | dname = fns; |
9533 | if (TREE_CODE (dname) == COMPONENT_REF) | |
9534 | dname = TREE_OPERAND (dname, 1); | |
9535 | if (TREE_CODE (dname) != IDENTIFIER_NODE) | |
9536 | { | |
9537 | my_friendly_assert (is_overloaded_fn (dname), | |
9538 | 19990331); | |
9539 | dname = DECL_NAME (get_first_fn (dname)); | |
9540 | } | |
386b8a85 | 9541 | } |
2c73f9f5 | 9542 | /* Fall through. */ |
be99da77 MS |
9543 | |
9544 | case IDENTIFIER_NODE: | |
386b8a85 JM |
9545 | if (TREE_CODE (decl) == IDENTIFIER_NODE) |
9546 | dname = decl; | |
9547 | ||
be99da77 MS |
9548 | next = 0; |
9549 | ||
9550 | if (is_rid (dname)) | |
8d08fdba | 9551 | { |
8251199e | 9552 | cp_error ("declarator-id missing; using reserved word `%D'", |
be99da77 MS |
9553 | dname); |
9554 | name = IDENTIFIER_POINTER (dname); | |
8d08fdba | 9555 | } |
be99da77 | 9556 | if (! IDENTIFIER_OPNAME_P (dname) |
956d6950 | 9557 | /* GNU/Linux headers use '__op'. Arrgh. */ |
a703fb38 | 9558 | || (IDENTIFIER_TYPENAME_P (dname) && ! TREE_TYPE (dname))) |
be99da77 | 9559 | name = IDENTIFIER_POINTER (dname); |
8d08fdba MS |
9560 | else |
9561 | { | |
be99da77 | 9562 | if (IDENTIFIER_TYPENAME_P (dname)) |
8d08fdba | 9563 | { |
be99da77 MS |
9564 | my_friendly_assert (flags == NO_SPECIAL, 154); |
9565 | flags = TYPENAME_FLAG; | |
9566 | ctor_return_type = TREE_TYPE (dname); | |
9567 | return_type = return_conversion; | |
8d08fdba | 9568 | } |
be99da77 | 9569 | name = operator_name_string (dname); |
8d08fdba | 9570 | } |
be99da77 | 9571 | break; |
8d08fdba | 9572 | |
be99da77 MS |
9573 | /* C++ extension */ |
9574 | case SCOPE_REF: | |
9575 | { | |
9576 | /* Perform error checking, and decide on a ctype. */ | |
9577 | tree cname = TREE_OPERAND (decl, 0); | |
9578 | if (cname == NULL_TREE) | |
9579 | ctype = NULL_TREE; | |
2c73f9f5 ML |
9580 | else if (TREE_CODE (cname) == NAMESPACE_DECL) |
9581 | { | |
9582 | ctype = NULL_TREE; | |
9583 | in_namespace = TREE_OPERAND (decl, 0); | |
9584 | TREE_OPERAND (decl, 0) = NULL_TREE; | |
9585 | } | |
be99da77 MS |
9586 | else if (! is_aggr_type (cname, 1)) |
9587 | TREE_OPERAND (decl, 0) = NULL_TREE; | |
9588 | /* Must test TREE_OPERAND (decl, 1), in case user gives | |
9589 | us `typedef (class::memfunc)(int); memfunc *memfuncptr;' */ | |
9590 | else if (TREE_OPERAND (decl, 1) | |
9591 | && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF) | |
9592 | ctype = cname; | |
73b0fce8 KL |
9593 | else if (TREE_CODE (cname) == TEMPLATE_TYPE_PARM |
9594 | || TREE_CODE (cname) == TEMPLATE_TEMPLATE_PARM) | |
5566b478 | 9595 | { |
8251199e | 9596 | cp_error ("`%T::%D' is not a valid declarator", cname, |
5566b478 | 9597 | TREE_OPERAND (decl, 1)); |
8251199e | 9598 | cp_error (" perhaps you want `typename %T::%D' to make it a type", |
5566b478 MS |
9599 | cname, TREE_OPERAND (decl, 1)); |
9600 | return void_type_node; | |
9601 | } | |
be99da77 MS |
9602 | else if (ctype == NULL_TREE) |
9603 | ctype = cname; | |
9604 | else if (TREE_COMPLEXITY (decl) == current_class_depth) | |
9605 | TREE_OPERAND (decl, 0) = ctype; | |
9606 | else | |
9607 | { | |
9608 | if (! UNIQUELY_DERIVED_FROM_P (cname, ctype)) | |
9609 | { | |
8251199e | 9610 | cp_error ("type `%T' is not derived from type `%T'", |
be99da77 MS |
9611 | cname, ctype); |
9612 | TREE_OPERAND (decl, 0) = NULL_TREE; | |
9613 | } | |
9614 | else | |
9615 | ctype = cname; | |
9616 | } | |
9617 | ||
c91a56d2 MS |
9618 | if (ctype && TREE_CODE (TREE_OPERAND (decl, 1)) == TYPE_DECL |
9619 | && ((DECL_NAME (TREE_OPERAND (decl, 1)) | |
9620 | == constructor_name_full (ctype)) | |
9621 | || (DECL_NAME (TREE_OPERAND (decl, 1)) | |
9622 | == constructor_name (ctype)))) | |
be99da77 MS |
9623 | TREE_OPERAND (decl, 1) = constructor_name (ctype); |
9624 | next = &TREE_OPERAND (decl, 1); | |
9625 | decl = *next; | |
9626 | if (ctype) | |
9627 | { | |
9628 | if (TREE_CODE (decl) == IDENTIFIER_NODE | |
9629 | && constructor_name (ctype) == decl) | |
9630 | { | |
9631 | return_type = return_ctor; | |
9632 | ctor_return_type = ctype; | |
9633 | } | |
9634 | else if (TREE_CODE (decl) == BIT_NOT_EXPR | |
9635 | && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE | |
9636 | && (constructor_name (ctype) == TREE_OPERAND (decl, 0) | |
9637 | || constructor_name_full (ctype) == TREE_OPERAND (decl, 0))) | |
9638 | { | |
9639 | return_type = return_dtor; | |
9640 | ctor_return_type = ctype; | |
9641 | flags = DTOR_FLAG; | |
9642 | TREE_OPERAND (decl, 0) = constructor_name (ctype); | |
9643 | next = &TREE_OPERAND (decl, 0); | |
9644 | } | |
9645 | } | |
9646 | } | |
9647 | break; | |
9648 | ||
9649 | case ERROR_MARK: | |
9650 | next = 0; | |
9651 | break; | |
9652 | ||
45537677 MS |
9653 | case TYPE_DECL: |
9654 | /* Parse error puts this typespec where | |
9655 | a declarator should go. */ | |
8251199e | 9656 | cp_error ("`%T' specified as declarator-id", DECL_NAME (decl)); |
45537677 | 9657 | if (TREE_TYPE (decl) == current_class_type) |
8251199e | 9658 | cp_error (" perhaps you want `%T' for a constructor", |
45537677 MS |
9659 | current_class_name); |
9660 | dname = DECL_NAME (decl); | |
9661 | name = IDENTIFIER_POINTER (dname); | |
9662 | ||
e92cc029 | 9663 | /* Avoid giving two errors for this. */ |
45537677 MS |
9664 | IDENTIFIER_CLASS_VALUE (dname) = NULL_TREE; |
9665 | ||
58010b57 | 9666 | declspecs = tree_cons (NULL_TREE, integer_type_node, declspecs); |
45537677 MS |
9667 | *next = dname; |
9668 | next = 0; | |
9669 | break; | |
9670 | ||
be99da77 | 9671 | default: |
8251199e | 9672 | cp_compiler_error ("`%D' as declarator", decl); |
be99da77 | 9673 | return 0; /* We used to do a 155 abort here. */ |
8d08fdba | 9674 | } |
be99da77 | 9675 | } |
8d08fdba MS |
9676 | } |
9677 | ||
9678 | /* A function definition's declarator must have the form of | |
9679 | a function declarator. */ | |
9680 | ||
9681 | if (funcdef_flag && innermost_code != CALL_EXPR) | |
9682 | return 0; | |
9683 | ||
e1cd6e56 MS |
9684 | if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG) |
9685 | && innermost_code != CALL_EXPR | |
9686 | && ! (ctype && declspecs == NULL_TREE)) | |
9687 | { | |
8251199e | 9688 | cp_error ("declaration of `%D' as non-function", dname); |
e1cd6e56 MS |
9689 | return void_type_node; |
9690 | } | |
9691 | ||
8d08fdba MS |
9692 | /* Anything declared one level down from the top level |
9693 | must be one of the parameters of a function | |
9694 | (because the body is at least two levels down). */ | |
9695 | ||
9696 | /* This heuristic cannot be applied to C++ nodes! Fixed, however, | |
9697 | by not allowing C++ class definitions to specify their parameters | |
9698 | with xdecls (must be spec.d in the parmlist). | |
9699 | ||
9700 | Since we now wait to push a class scope until we are sure that | |
9701 | we are in a legitimate method context, we must set oldcname | |
a9aedbc2 MS |
9702 | explicitly (since current_class_name is not yet alive). |
9703 | ||
9704 | We also want to avoid calling this a PARM if it is in a namespace. */ | |
8d08fdba | 9705 | |
9188c363 | 9706 | if (decl_context == NORMAL && !toplevel_bindings_p ()) |
a9aedbc2 MS |
9707 | { |
9708 | struct binding_level *b = current_binding_level; | |
9709 | current_binding_level = b->level_chain; | |
9710 | if (current_binding_level != 0 && toplevel_bindings_p ()) | |
9711 | decl_context = PARM; | |
9712 | current_binding_level = b; | |
9713 | } | |
8d08fdba | 9714 | |
34fc957d NS |
9715 | if (name == NULL) |
9716 | name = decl_context == PARM ? "parameter" : "type name"; | |
68642fb6 | 9717 | |
8d08fdba MS |
9718 | /* Look through the decl specs and record which ones appear. |
9719 | Some typespecs are defined as built-in typenames. | |
9720 | Others, the ones that are modifiers of other types, | |
9721 | are represented by bits in SPECBITS: set the bits for | |
9722 | the modifiers that appear. Storage class keywords are also in SPECBITS. | |
9723 | ||
9724 | If there is a typedef name or a type, store the type in TYPE. | |
9725 | This includes builtin typedefs such as `int'. | |
9726 | ||
9727 | Set EXPLICIT_INT if the type is `int' or `char' and did not | |
9728 | come from a user typedef. | |
9729 | ||
9730 | Set LONGLONG if `long' is mentioned twice. | |
9731 | ||
9732 | For C++, constructors and destructors have their own fast treatment. */ | |
9733 | ||
9734 | for (spec = declspecs; spec; spec = TREE_CHAIN (spec)) | |
9735 | { | |
9736 | register int i; | |
9737 | register tree id; | |
9738 | ||
9739 | /* Certain parse errors slip through. For example, | |
9740 | `int class;' is not caught by the parser. Try | |
9741 | weakly to recover here. */ | |
9742 | if (TREE_CODE (spec) != TREE_LIST) | |
9743 | return 0; | |
9744 | ||
9745 | id = TREE_VALUE (spec); | |
9746 | ||
9747 | if (TREE_CODE (id) == IDENTIFIER_NODE) | |
9748 | { | |
a3203465 MS |
9749 | if (id == ridpointers[(int) RID_INT] |
9750 | || id == ridpointers[(int) RID_CHAR] | |
9751 | || id == ridpointers[(int) RID_BOOL] | |
9752 | || id == ridpointers[(int) RID_WCHAR]) | |
8d08fdba MS |
9753 | { |
9754 | if (type) | |
8ccc31eb MS |
9755 | { |
9756 | if (id == ridpointers[(int) RID_BOOL]) | |
8251199e | 9757 | error ("`bool' is now a keyword"); |
8ccc31eb | 9758 | else |
8251199e | 9759 | cp_error ("extraneous `%T' ignored", id); |
8ccc31eb | 9760 | } |
8d08fdba MS |
9761 | else |
9762 | { | |
a3203465 MS |
9763 | if (id == ridpointers[(int) RID_INT]) |
9764 | explicit_int = 1; | |
9765 | else if (id == ridpointers[(int) RID_CHAR]) | |
9766 | explicit_char = 1; | |
8d08fdba MS |
9767 | type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id)); |
9768 | } | |
9769 | goto found; | |
9770 | } | |
e92cc029 | 9771 | /* C++ aggregate types. */ |
8d08fdba MS |
9772 | if (IDENTIFIER_HAS_TYPE_VALUE (id)) |
9773 | { | |
9774 | if (type) | |
8251199e | 9775 | cp_error ("multiple declarations `%T' and `%T'", type, id); |
8d08fdba MS |
9776 | else |
9777 | type = IDENTIFIER_TYPE_VALUE (id); | |
9778 | goto found; | |
9779 | } | |
9780 | ||
f376e137 | 9781 | for (i = (int) RID_FIRST_MODIFIER; i <= (int) RID_LAST_MODIFIER; i++) |
8d08fdba MS |
9782 | { |
9783 | if (ridpointers[i] == id) | |
9784 | { | |
9785 | if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits)) | |
9786 | { | |
795add94 | 9787 | if (pedantic && ! in_system_header && warn_long_long) |
cb9a3ff8 | 9788 | pedwarn ("ISO C++ does not support `long long'"); |
9a3b49ac | 9789 | if (longlong) |
8251199e | 9790 | error ("`long long long' is too long for GCC"); |
8d08fdba MS |
9791 | else |
9792 | longlong = 1; | |
9793 | } | |
9794 | else if (RIDBIT_SETP (i, specbits)) | |
8251199e | 9795 | pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id)); |
8d08fdba MS |
9796 | RIDBIT_SET (i, specbits); |
9797 | goto found; | |
9798 | } | |
9799 | } | |
9800 | } | |
e92cc029 | 9801 | /* C++ aggregate types. */ |
73b0fce8 | 9802 | else if (TREE_CODE (id) == TYPE_DECL || TREE_CODE (id) == TEMPLATE_DECL) |
45537677 MS |
9803 | { |
9804 | if (type) | |
8251199e | 9805 | cp_error ("multiple declarations `%T' and `%T'", type, |
45537677 MS |
9806 | TREE_TYPE (id)); |
9807 | else | |
5566b478 MS |
9808 | { |
9809 | type = TREE_TYPE (id); | |
9810 | TREE_VALUE (spec) = type; | |
9811 | } | |
45537677 MS |
9812 | goto found; |
9813 | } | |
8d08fdba | 9814 | if (type) |
8251199e | 9815 | error ("two or more data types in declaration of `%s'", name); |
8d08fdba MS |
9816 | else if (TREE_CODE (id) == IDENTIFIER_NODE) |
9817 | { | |
9818 | register tree t = lookup_name (id, 1); | |
9819 | if (!t || TREE_CODE (t) != TYPE_DECL) | |
8251199e | 9820 | error ("`%s' fails to be a typedef or built in type", |
8d08fdba MS |
9821 | IDENTIFIER_POINTER (id)); |
9822 | else | |
9823 | { | |
9824 | type = TREE_TYPE (t); | |
6125f3be DE |
9825 | #if 0 |
9826 | /* See the code below that used this. */ | |
f6abb50a | 9827 | decl_machine_attr = DECL_MACHINE_ATTRIBUTES (id); |
6125f3be | 9828 | #endif |
8d08fdba MS |
9829 | typedef_decl = t; |
9830 | } | |
9831 | } | |
bd6dd845 | 9832 | else if (id != error_mark_node) |
8d08fdba MS |
9833 | /* Can't change CLASS nodes into RECORD nodes here! */ |
9834 | type = id; | |
9835 | ||
9836 | found: ; | |
9837 | } | |
9838 | ||
9839 | typedef_type = type; | |
9840 | ||
37c46b43 | 9841 | /* No type at all: default to `int', and set DEFAULTED_INT |
6eabb241 | 9842 | because it was not a user-defined typedef. */ |
8d08fdba | 9843 | |
a3203465 MS |
9844 | if (type == NULL_TREE |
9845 | && (RIDBIT_SETP (RID_SIGNED, specbits) | |
9846 | || RIDBIT_SETP (RID_UNSIGNED, specbits) | |
9847 | || RIDBIT_SETP (RID_LONG, specbits) | |
9848 | || RIDBIT_SETP (RID_SHORT, specbits))) | |
9849 | { | |
9850 | /* These imply 'int'. */ | |
9851 | type = integer_type_node; | |
37c46b43 | 9852 | defaulted_int = 1; |
a3203465 MS |
9853 | } |
9854 | ||
8d08fdba MS |
9855 | if (type == NULL_TREE) |
9856 | { | |
9857 | explicit_int = -1; | |
9858 | if (return_type == return_dtor) | |
9859 | type = void_type_node; | |
9860 | else if (return_type == return_ctor) | |
f30432d7 | 9861 | type = build_pointer_type (ctor_return_type); |
51c184be MS |
9862 | else if (return_type == return_conversion) |
9863 | type = ctor_return_type; | |
8d08fdba MS |
9864 | else |
9865 | { | |
41eff652 JM |
9866 | /* We handle `main' specially here, because 'main () { }' is so |
9867 | common. With no options, it is allowed. With -Wreturn-type, | |
9868 | it is a warning. It is only an error with -pedantic-errors. */ | |
9869 | int is_main = (funcdef_flag | |
9870 | && MAIN_NAME_P (dname) | |
9871 | && ctype == NULL_TREE | |
9872 | && in_namespace == NULL_TREE | |
9873 | && current_namespace == global_namespace); | |
9874 | ||
d2c192ad | 9875 | if (in_system_header || flag_ms_extensions) |
1f901793 JM |
9876 | /* Allow it, sigh. */; |
9877 | else if (pedantic || ! is_main) | |
cb9a3ff8 | 9878 | cp_pedwarn ("ISO C++ forbids declaration of `%s' with no type", |
34fc957d | 9879 | name); |
41eff652 | 9880 | else if (warn_return_type) |
cb9a3ff8 | 9881 | cp_warning ("ISO C++ forbids declaration of `%s' with no type", |
34fc957d | 9882 | name); |
41eff652 | 9883 | |
8d08fdba MS |
9884 | type = integer_type_node; |
9885 | } | |
9886 | } | |
9887 | else if (return_type == return_dtor) | |
9888 | { | |
8251199e | 9889 | error ("return type specification for destructor invalid"); |
8d08fdba MS |
9890 | type = void_type_node; |
9891 | } | |
9892 | else if (return_type == return_ctor) | |
9893 | { | |
8251199e | 9894 | error ("return type specification for constructor invalid"); |
f30432d7 | 9895 | type = build_pointer_type (ctor_return_type); |
8d08fdba | 9896 | } |
51c184be MS |
9897 | else if (return_type == return_conversion) |
9898 | { | |
3bfdc719 | 9899 | if (!same_type_p (type, ctor_return_type)) |
8251199e | 9900 | cp_error ("operator `%T' declared to return `%T'", |
51c184be MS |
9901 | ctor_return_type, type); |
9902 | else | |
8251199e | 9903 | cp_pedwarn ("return type specified for `operator %T'", |
51c184be MS |
9904 | ctor_return_type); |
9905 | ||
9906 | type = ctor_return_type; | |
9907 | } | |
8d08fdba MS |
9908 | |
9909 | ctype = NULL_TREE; | |
9910 | ||
9911 | /* Now process the modifiers that were specified | |
9912 | and check for invalid combinations. */ | |
9913 | ||
9914 | /* Long double is a special combination. */ | |
9915 | ||
9916 | if (RIDBIT_SETP (RID_LONG, specbits) | |
9917 | && TYPE_MAIN_VARIANT (type) == double_type_node) | |
9918 | { | |
9919 | RIDBIT_RESET (RID_LONG, specbits); | |
68642fb6 | 9920 | type = build_qualified_type (long_double_type_node, |
91063b51 | 9921 | CP_TYPE_QUALS (type)); |
8d08fdba MS |
9922 | } |
9923 | ||
9924 | /* Check all other uses of type modifiers. */ | |
9925 | ||
9926 | if (RIDBIT_SETP (RID_UNSIGNED, specbits) | |
9927 | || RIDBIT_SETP (RID_SIGNED, specbits) | |
9928 | || RIDBIT_SETP (RID_LONG, specbits) | |
9929 | || RIDBIT_SETP (RID_SHORT, specbits)) | |
9930 | { | |
9931 | int ok = 0; | |
9932 | ||
9933 | if (TREE_CODE (type) == REAL_TYPE) | |
8251199e | 9934 | error ("short, signed or unsigned invalid for `%s'", name); |
b7484fbe | 9935 | else if (TREE_CODE (type) != INTEGER_TYPE) |
8251199e | 9936 | error ("long, short, signed or unsigned invalid for `%s'", name); |
8d08fdba MS |
9937 | else if (RIDBIT_SETP (RID_LONG, specbits) |
9938 | && RIDBIT_SETP (RID_SHORT, specbits)) | |
8251199e | 9939 | error ("long and short specified together for `%s'", name); |
8d08fdba MS |
9940 | else if ((RIDBIT_SETP (RID_LONG, specbits) |
9941 | || RIDBIT_SETP (RID_SHORT, specbits)) | |
9942 | && explicit_char) | |
8251199e | 9943 | error ("long or short specified with char for `%s'", name); |
8d08fdba MS |
9944 | else if ((RIDBIT_SETP (RID_LONG, specbits) |
9945 | || RIDBIT_SETP (RID_SHORT, specbits)) | |
9946 | && TREE_CODE (type) == REAL_TYPE) | |
8251199e | 9947 | error ("long or short specified with floating type for `%s'", name); |
8d08fdba MS |
9948 | else if (RIDBIT_SETP (RID_SIGNED, specbits) |
9949 | && RIDBIT_SETP (RID_UNSIGNED, specbits)) | |
8251199e | 9950 | error ("signed and unsigned given together for `%s'", name); |
8d08fdba MS |
9951 | else |
9952 | { | |
9953 | ok = 1; | |
37c46b43 | 9954 | if (!explicit_int && !defaulted_int && !explicit_char && pedantic) |
8d08fdba | 9955 | { |
8251199e | 9956 | pedwarn ("long, short, signed or unsigned used invalidly for `%s'", |
8d08fdba MS |
9957 | name); |
9958 | if (flag_pedantic_errors) | |
9959 | ok = 0; | |
9960 | } | |
9961 | } | |
9962 | ||
9963 | /* Discard the type modifiers if they are invalid. */ | |
9964 | if (! ok) | |
9965 | { | |
9966 | RIDBIT_RESET (RID_UNSIGNED, specbits); | |
9967 | RIDBIT_RESET (RID_SIGNED, specbits); | |
9968 | RIDBIT_RESET (RID_LONG, specbits); | |
9969 | RIDBIT_RESET (RID_SHORT, specbits); | |
9970 | longlong = 0; | |
9971 | } | |
9972 | } | |
9973 | ||
37c46b43 MS |
9974 | if (RIDBIT_SETP (RID_COMPLEX, specbits) |
9975 | && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE) | |
9976 | { | |
8251199e | 9977 | error ("complex invalid for `%s'", name); |
37c46b43 MS |
9978 | RIDBIT_RESET (RID_COMPLEX, specbits); |
9979 | } | |
9980 | ||
8d08fdba MS |
9981 | /* Decide whether an integer type is signed or not. |
9982 | Optionally treat bitfields as signed by default. */ | |
9983 | if (RIDBIT_SETP (RID_UNSIGNED, specbits) | |
b89c5a7b MM |
9984 | /* [class.bit] |
9985 | ||
9986 | It is implementation-defined whether a plain (neither | |
9987 | explicitly signed or unsigned) char, short, int, or long | |
9988 | bit-field is signed or unsigned. | |
68642fb6 | 9989 | |
b89c5a7b MM |
9990 | Naturally, we extend this to long long as well. Note that |
9991 | this does not include wchar_t. */ | |
9992 | || (bitfield && !flag_signed_bitfields | |
9993 | && RIDBIT_NOTSETP (RID_SIGNED, specbits) | |
9994 | /* A typedef for plain `int' without `signed' can be | |
9995 | controlled just like plain `int', but a typedef for | |
9996 | `signed int' cannot be so controlled. */ | |
68642fb6 | 9997 | && !(typedef_decl |
29bbeb1c | 9998 | && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)) |
b89c5a7b MM |
9999 | && (TREE_CODE (type) == INTEGER_TYPE |
10000 | || TREE_CODE (type) == CHAR_TYPE) | |
29bbeb1c | 10001 | && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node))) |
8d08fdba MS |
10002 | { |
10003 | if (longlong) | |
10004 | type = long_long_unsigned_type_node; | |
10005 | else if (RIDBIT_SETP (RID_LONG, specbits)) | |
10006 | type = long_unsigned_type_node; | |
10007 | else if (RIDBIT_SETP (RID_SHORT, specbits)) | |
10008 | type = short_unsigned_type_node; | |
10009 | else if (type == char_type_node) | |
10010 | type = unsigned_char_type_node; | |
10011 | else if (typedef_decl) | |
10012 | type = unsigned_type (type); | |
10013 | else | |
10014 | type = unsigned_type_node; | |
10015 | } | |
10016 | else if (RIDBIT_SETP (RID_SIGNED, specbits) | |
10017 | && type == char_type_node) | |
10018 | type = signed_char_type_node; | |
10019 | else if (longlong) | |
10020 | type = long_long_integer_type_node; | |
10021 | else if (RIDBIT_SETP (RID_LONG, specbits)) | |
10022 | type = long_integer_type_node; | |
10023 | else if (RIDBIT_SETP (RID_SHORT, specbits)) | |
10024 | type = short_integer_type_node; | |
10025 | ||
37c46b43 MS |
10026 | if (RIDBIT_SETP (RID_COMPLEX, specbits)) |
10027 | { | |
10028 | /* If we just have "complex", it is equivalent to | |
10029 | "complex double", but if any modifiers at all are specified it is | |
10030 | the complex form of TYPE. E.g, "complex short" is | |
10031 | "complex short int". */ | |
10032 | ||
10033 | if (defaulted_int && ! longlong | |
10034 | && ! (RIDBIT_SETP (RID_LONG, specbits) | |
10035 | || RIDBIT_SETP (RID_SHORT, specbits) | |
10036 | || RIDBIT_SETP (RID_SIGNED, specbits) | |
10037 | || RIDBIT_SETP (RID_UNSIGNED, specbits))) | |
10038 | type = complex_double_type_node; | |
10039 | else if (type == integer_type_node) | |
10040 | type = complex_integer_type_node; | |
10041 | else if (type == float_type_node) | |
10042 | type = complex_float_type_node; | |
10043 | else if (type == double_type_node) | |
10044 | type = complex_double_type_node; | |
10045 | else if (type == long_double_type_node) | |
10046 | type = complex_long_double_type_node; | |
10047 | else | |
10048 | type = build_complex_type (type); | |
10049 | } | |
10050 | ||
68642fb6 | 10051 | if (return_type == return_conversion |
3ac3d9ea | 10052 | && (RIDBIT_SETP (RID_CONST, specbits) |
91063b51 MM |
10053 | || RIDBIT_SETP (RID_VOLATILE, specbits) |
10054 | || RIDBIT_SETP (RID_RESTRICT, specbits))) | |
10055 | cp_error ("qualifiers are not allowed on declaration of `operator %T'", | |
3ac3d9ea MM |
10056 | ctor_return_type); |
10057 | ||
8d08fdba MS |
10058 | /* Set CONSTP if this declaration is `const', whether by |
10059 | explicit specification or via a typedef. | |
10060 | Likewise for VOLATILEP. */ | |
10061 | ||
91063b51 | 10062 | constp = !! RIDBIT_SETP (RID_CONST, specbits) + CP_TYPE_CONST_P (type); |
68642fb6 | 10063 | restrictp = |
91063b51 | 10064 | !! RIDBIT_SETP (RID_RESTRICT, specbits) + CP_TYPE_RESTRICT_P (type); |
68642fb6 | 10065 | volatilep = |
91063b51 MM |
10066 | !! RIDBIT_SETP (RID_VOLATILE, specbits) + CP_TYPE_VOLATILE_P (type); |
10067 | type_quals = ((constp ? TYPE_QUAL_CONST : 0) | |
10068 | | (restrictp ? TYPE_QUAL_RESTRICT : 0) | |
10069 | | (volatilep ? TYPE_QUAL_VOLATILE : 0)); | |
10070 | type = cp_build_qualified_type (type, type_quals); | |
8d08fdba MS |
10071 | staticp = 0; |
10072 | inlinep = !! RIDBIT_SETP (RID_INLINE, specbits); | |
8d08fdba | 10073 | virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits); |
db5ae43f MS |
10074 | RIDBIT_RESET (RID_VIRTUAL, specbits); |
10075 | explicitp = RIDBIT_SETP (RID_EXPLICIT, specbits) != 0; | |
10076 | RIDBIT_RESET (RID_EXPLICIT, specbits); | |
8d08fdba | 10077 | |
8d08fdba MS |
10078 | if (RIDBIT_SETP (RID_STATIC, specbits)) |
10079 | staticp = 1 + (decl_context == FIELD); | |
10080 | ||
10081 | if (virtualp && staticp == 2) | |
10082 | { | |
8251199e | 10083 | cp_error ("member `%D' cannot be declared both virtual and static", |
8d08fdba MS |
10084 | dname); |
10085 | staticp = 0; | |
10086 | } | |
10087 | friendp = RIDBIT_SETP (RID_FRIEND, specbits); | |
8d08fdba MS |
10088 | RIDBIT_RESET (RID_FRIEND, specbits); |
10089 | ||
8d08fdba MS |
10090 | /* Warn if two storage classes are given. Default to `auto'. */ |
10091 | ||
10092 | if (RIDBIT_ANY_SET (specbits)) | |
10093 | { | |
10094 | if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++; | |
10095 | if (RIDBIT_SETP (RID_EXTERN, specbits)) nclasses++; | |
10096 | if (decl_context == PARM && nclasses > 0) | |
8251199e | 10097 | error ("storage class specifiers invalid in parameter declarations"); |
8d08fdba MS |
10098 | if (RIDBIT_SETP (RID_TYPEDEF, specbits)) |
10099 | { | |
10100 | if (decl_context == PARM) | |
8251199e | 10101 | error ("typedef declaration invalid in parameter declaration"); |
8d08fdba MS |
10102 | nclasses++; |
10103 | } | |
10104 | if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++; | |
10105 | if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++; | |
10106 | } | |
10107 | ||
10108 | /* Give error if `virtual' is used outside of class declaration. */ | |
b7484fbe MS |
10109 | if (virtualp |
10110 | && (current_class_name == NULL_TREE || decl_context != FIELD)) | |
8d08fdba | 10111 | { |
8251199e | 10112 | error ("virtual outside class declaration"); |
8d08fdba MS |
10113 | virtualp = 0; |
10114 | } | |
8d08fdba MS |
10115 | |
10116 | /* Static anonymous unions are dealt with here. */ | |
10117 | if (staticp && decl_context == TYPENAME | |
10118 | && TREE_CODE (declspecs) == TREE_LIST | |
6bdb8141 | 10119 | && ANON_AGGR_TYPE_P (TREE_VALUE (declspecs))) |
8d08fdba MS |
10120 | decl_context = FIELD; |
10121 | ||
8d08fdba MS |
10122 | /* Warn about storage classes that are invalid for certain |
10123 | kinds of declarations (parameters, typenames, etc.). */ | |
10124 | ||
10125 | if (nclasses > 1) | |
8251199e | 10126 | error ("multiple storage classes in declaration of `%s'", name); |
8d08fdba MS |
10127 | else if (decl_context != NORMAL && nclasses > 0) |
10128 | { | |
db5ae43f | 10129 | if ((decl_context == PARM || decl_context == CATCHPARM) |
8d08fdba MS |
10130 | && (RIDBIT_SETP (RID_REGISTER, specbits) |
10131 | || RIDBIT_SETP (RID_AUTO, specbits))) | |
10132 | ; | |
fc378698 MS |
10133 | else if (RIDBIT_SETP (RID_TYPEDEF, specbits)) |
10134 | ; | |
8d08fdba | 10135 | else if (decl_context == FIELD |
8d08fdba MS |
10136 | /* C++ allows static class elements */ |
10137 | && RIDBIT_SETP (RID_STATIC, specbits)) | |
10138 | /* C++ also allows inlines and signed and unsigned elements, | |
10139 | but in those cases we don't come in here. */ | |
10140 | ; | |
10141 | else | |
10142 | { | |
10143 | if (decl_context == FIELD) | |
10144 | { | |
b7484fbe MS |
10145 | tree tmp = NULL_TREE; |
10146 | register int op = 0; | |
10147 | ||
10148 | if (declarator) | |
10149 | { | |
68642fb6 | 10150 | /* Avoid trying to get an operand off an identifier node. */ |
9e9ff709 MS |
10151 | if (TREE_CODE (declarator) == IDENTIFIER_NODE) |
10152 | tmp = declarator; | |
10153 | else | |
10154 | tmp = TREE_OPERAND (declarator, 0); | |
b7484fbe MS |
10155 | op = IDENTIFIER_OPNAME_P (tmp); |
10156 | } | |
8251199e | 10157 | error ("storage class specified for %s `%s'", |
6eabb241 | 10158 | op ? "member operator" : "field", |
8d08fdba MS |
10159 | op ? operator_name_string (tmp) : name); |
10160 | } | |
10161 | else | |
b9d12519 KG |
10162 | { |
10163 | if (decl_context == PARM || decl_context == CATCHPARM) | |
10164 | error ("storage class specified for parameter `%s'", name); | |
10165 | else | |
10166 | error ("storage class specified for typename"); | |
10167 | } | |
8d08fdba MS |
10168 | RIDBIT_RESET (RID_REGISTER, specbits); |
10169 | RIDBIT_RESET (RID_AUTO, specbits); | |
10170 | RIDBIT_RESET (RID_EXTERN, specbits); | |
8d08fdba MS |
10171 | } |
10172 | } | |
10173 | else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag) | |
10174 | { | |
a9aedbc2 | 10175 | if (toplevel_bindings_p ()) |
8d08fdba | 10176 | { |
59be0cdd | 10177 | /* It's common practice (and completely valid) to have a const |
8d08fdba | 10178 | be initialized and declared extern. */ |
950ad3c3 | 10179 | if (!(type_quals & TYPE_QUAL_CONST)) |
8251199e | 10180 | warning ("`%s' initialized and declared `extern'", name); |
8d08fdba MS |
10181 | } |
10182 | else | |
8251199e | 10183 | error ("`%s' has both `extern' and initializer", name); |
8d08fdba MS |
10184 | } |
10185 | else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag | |
a9aedbc2 | 10186 | && ! toplevel_bindings_p ()) |
8251199e | 10187 | error ("nested function `%s' declared `extern'", name); |
a9aedbc2 | 10188 | else if (toplevel_bindings_p ()) |
8d08fdba MS |
10189 | { |
10190 | if (RIDBIT_SETP (RID_AUTO, specbits)) | |
8251199e | 10191 | error ("top-level declaration of `%s' specifies `auto'", name); |
8d08fdba MS |
10192 | } |
10193 | ||
909e536a | 10194 | if (nclasses > 0 && friendp) |
8251199e | 10195 | error ("storage class specifiers invalid in friend function declarations"); |
909e536a | 10196 | |
8d08fdba MS |
10197 | /* Now figure out the structure of the declarator proper. |
10198 | Descend through it, creating more complex types, until we reach | |
10199 | the declared identifier (or NULL_TREE, in an absolute declarator). */ | |
10200 | ||
1eab9b56 | 10201 | inner_attrs = NULL_TREE; |
68642fb6 | 10202 | ignore_attrs = 0; |
1eab9b56 | 10203 | |
386b8a85 JM |
10204 | while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE |
10205 | && TREE_CODE (declarator) != TEMPLATE_ID_EXPR) | |
8d08fdba MS |
10206 | { |
10207 | /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]), | |
10208 | an INDIRECT_REF (for *...), | |
10209 | a CALL_EXPR (for ...(...)), | |
10210 | an identifier (for the name being declared) | |
10211 | or a null pointer (for the place in an absolute declarator | |
10212 | where the name was omitted). | |
10213 | For the last two cases, we have just exited the loop. | |
10214 | ||
10215 | For C++ it could also be | |
10216 | a SCOPE_REF (for class :: ...). In this case, we have converted | |
10217 | sensible names to types, and those are the values we use to | |
10218 | qualify the member name. | |
10219 | an ADDR_EXPR (for &...), | |
10220 | a BIT_NOT_EXPR (for destructors) | |
8d08fdba MS |
10221 | |
10222 | At this point, TYPE is the type of elements of an array, | |
10223 | or for a function to return, or for a pointer to point to. | |
10224 | After this sequence of ifs, TYPE is the type of the | |
10225 | array or function or pointer, and DECLARATOR has had its | |
10226 | outermost layer removed. */ | |
10227 | ||
bd6dd845 | 10228 | if (type == error_mark_node) |
8d08fdba MS |
10229 | { |
10230 | if (TREE_CODE (declarator) == SCOPE_REF) | |
10231 | declarator = TREE_OPERAND (declarator, 1); | |
10232 | else | |
10233 | declarator = TREE_OPERAND (declarator, 0); | |
10234 | continue; | |
10235 | } | |
10236 | if (quals != NULL_TREE | |
10237 | && (declarator == NULL_TREE | |
10238 | || TREE_CODE (declarator) != SCOPE_REF)) | |
10239 | { | |
10240 | if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE) | |
10241 | ctype = TYPE_METHOD_BASETYPE (type); | |
10242 | if (ctype != NULL_TREE) | |
10243 | { | |
8d08fdba | 10244 | tree dummy = build_decl (TYPE_DECL, NULL_TREE, type); |
535233a8 | 10245 | grok_method_quals (ctype, dummy, quals); |
8d08fdba | 10246 | type = TREE_TYPE (dummy); |
535233a8 | 10247 | ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type))); |
8d08fdba MS |
10248 | quals = NULL_TREE; |
10249 | } | |
10250 | } | |
1eab9b56 JM |
10251 | |
10252 | /* See the comment for the TREE_LIST case, below. */ | |
10253 | if (ignore_attrs) | |
10254 | ignore_attrs = 0; | |
10255 | else if (inner_attrs) | |
10256 | { | |
10257 | decl_attributes (type, inner_attrs, NULL_TREE); | |
10258 | inner_attrs = NULL_TREE; | |
10259 | } | |
10260 | ||
8d08fdba MS |
10261 | switch (TREE_CODE (declarator)) |
10262 | { | |
52fbc847 JM |
10263 | case TREE_LIST: |
10264 | { | |
10265 | /* We encode a declarator with embedded attributes using | |
1eab9b56 JM |
10266 | a TREE_LIST. The attributes apply to the declarator |
10267 | directly inside them, so we have to skip an iteration | |
10268 | before applying them to the type. If the declarator just | |
10269 | inside is the declarator-id, we apply the attrs to the | |
10270 | decl itself. */ | |
10271 | inner_attrs = TREE_PURPOSE (declarator); | |
10272 | ignore_attrs = 1; | |
52fbc847 | 10273 | declarator = TREE_VALUE (declarator); |
52fbc847 JM |
10274 | } |
10275 | break; | |
10276 | ||
8d08fdba MS |
10277 | case ARRAY_REF: |
10278 | { | |
2848ad0f | 10279 | register tree size; |
8d08fdba | 10280 | |
2848ad0f | 10281 | size = TREE_OPERAND (declarator, 1); |
8d08fdba | 10282 | |
f2e2cbd4 | 10283 | /* VC++ spells a zero-sized array with []. */ |
e8ea7036 MM |
10284 | if (size == NULL_TREE && decl_context == FIELD && ! staticp |
10285 | && ! RIDBIT_SETP (RID_TYPEDEF, specbits)) | |
f2e2cbd4 JM |
10286 | size = integer_zero_node; |
10287 | ||
2848ad0f | 10288 | declarator = TREE_OPERAND (declarator, 0); |
8d08fdba | 10289 | |
2848ad0f | 10290 | type = create_array_type_for_decl (dname, type, size); |
8d08fdba MS |
10291 | ctype = NULL_TREE; |
10292 | } | |
10293 | break; | |
10294 | ||
10295 | case CALL_EXPR: | |
10296 | { | |
10297 | tree arg_types; | |
f376e137 | 10298 | int funcdecl_p; |
43f887f9 | 10299 | tree inner_parms = CALL_DECLARATOR_PARMS (declarator); |
f376e137 | 10300 | tree inner_decl = TREE_OPERAND (declarator, 0); |
8d08fdba MS |
10301 | |
10302 | /* Declaring a function type. | |
10303 | Make sure we have a valid type for the function to return. */ | |
8d08fdba | 10304 | |
91063b51 | 10305 | /* We now know that the TYPE_QUALS don't apply to the |
14ae7e7d | 10306 | decl, but to its return type. */ |
91063b51 | 10307 | type_quals = TYPE_UNQUALIFIED; |
8d08fdba MS |
10308 | |
10309 | /* Warn about some types functions can't return. */ | |
10310 | ||
10311 | if (TREE_CODE (type) == FUNCTION_TYPE) | |
10312 | { | |
8251199e | 10313 | error ("`%s' declared as function returning a function", name); |
8d08fdba MS |
10314 | type = integer_type_node; |
10315 | } | |
10316 | if (TREE_CODE (type) == ARRAY_TYPE) | |
10317 | { | |
8251199e | 10318 | error ("`%s' declared as function returning an array", name); |
8d08fdba MS |
10319 | type = integer_type_node; |
10320 | } | |
10321 | ||
f376e137 MS |
10322 | if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF) |
10323 | inner_decl = TREE_OPERAND (inner_decl, 1); | |
10324 | ||
68642fb6 | 10325 | if (inner_decl && TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR) |
386b8a85 JM |
10326 | inner_decl = dname; |
10327 | ||
b7484fbe | 10328 | /* Pick up type qualifiers which should be applied to `this'. */ |
43f887f9 | 10329 | quals = CALL_DECLARATOR_QUALS (declarator); |
b7484fbe | 10330 | |
c11b6f21 | 10331 | /* Pick up the exception specifications. */ |
43f887f9 | 10332 | raises = CALL_DECLARATOR_EXCEPTION_SPEC (declarator); |
c11b6f21 | 10333 | |
f376e137 MS |
10334 | /* Say it's a definition only for the CALL_EXPR |
10335 | closest to the identifier. */ | |
beb53fb8 | 10336 | funcdecl_p |
68642fb6 | 10337 | = inner_decl |
386b8a85 | 10338 | && (TREE_CODE (inner_decl) == IDENTIFIER_NODE |
68642fb6 | 10339 | || TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR |
386b8a85 | 10340 | || TREE_CODE (inner_decl) == BIT_NOT_EXPR); |
68642fb6 | 10341 | |
8d08fdba MS |
10342 | if (ctype == NULL_TREE |
10343 | && decl_context == FIELD | |
f376e137 | 10344 | && funcdecl_p |
8d08fdba MS |
10345 | && (friendp == 0 || dname == current_class_name)) |
10346 | ctype = current_class_type; | |
10347 | ||
51c184be | 10348 | if (ctype && return_type == return_conversion) |
8d08fdba MS |
10349 | TYPE_HAS_CONVERSION (ctype) = 1; |
10350 | if (ctype && constructor_name (ctype) == dname) | |
10351 | { | |
10352 | /* We are within a class's scope. If our declarator name | |
10353 | is the same as the class name, and we are defining | |
10354 | a function, then it is a constructor/destructor, and | |
10355 | therefore returns a void type. */ | |
10356 | ||
10357 | if (flags == DTOR_FLAG) | |
10358 | { | |
cab1f180 ML |
10359 | /* ISO C++ 12.4/2. A destructor may not be |
10360 | declared const or volatile. A destructor may | |
10361 | not be static. */ | |
8d08fdba | 10362 | if (staticp == 2) |
8251199e | 10363 | error ("destructor cannot be static member function"); |
b7484fbe | 10364 | if (quals) |
8d08fdba | 10365 | { |
4f70a846 MM |
10366 | cp_error ("destructors may not be `%s'", |
10367 | IDENTIFIER_POINTER (TREE_VALUE (quals))); | |
10368 | quals = NULL_TREE; | |
8d08fdba MS |
10369 | } |
10370 | if (decl_context == FIELD) | |
10371 | { | |
4dacf5bd KG |
10372 | if (! member_function_or_else (ctype, |
10373 | current_class_type, | |
10374 | flags)) | |
8d08fdba MS |
10375 | return void_type_node; |
10376 | } | |
10377 | } | |
2c73f9f5 | 10378 | else /* It's a constructor. */ |
8d08fdba | 10379 | { |
db5ae43f MS |
10380 | if (explicitp == 1) |
10381 | explicitp = 2; | |
cab1f180 ML |
10382 | /* ISO C++ 12.1. A constructor may not be |
10383 | declared const or volatile. A constructor may | |
10384 | not be virtual. A constructor may not be | |
10385 | static. */ | |
8d08fdba | 10386 | if (staticp == 2) |
8251199e | 10387 | error ("constructor cannot be static member function"); |
8d08fdba MS |
10388 | if (virtualp) |
10389 | { | |
8251199e | 10390 | pedwarn ("constructors cannot be declared virtual"); |
8d08fdba MS |
10391 | virtualp = 0; |
10392 | } | |
b7484fbe | 10393 | if (quals) |
8d08fdba | 10394 | { |
4f70a846 MM |
10395 | cp_error ("constructors may not be `%s'", |
10396 | IDENTIFIER_POINTER (TREE_VALUE (quals))); | |
10397 | quals = NULL_TREE; | |
8d08fdba | 10398 | } |
8d08fdba | 10399 | { |
51c184be | 10400 | RID_BIT_TYPE tmp_bits; |
fc378698 | 10401 | bcopy ((void*)&specbits, (void*)&tmp_bits, sizeof (RID_BIT_TYPE)); |
51c184be MS |
10402 | RIDBIT_RESET (RID_INLINE, tmp_bits); |
10403 | RIDBIT_RESET (RID_STATIC, tmp_bits); | |
10404 | if (RIDBIT_ANY_SET (tmp_bits)) | |
8251199e | 10405 | error ("return value type specifier for constructor ignored"); |
8d08fdba | 10406 | } |
f30432d7 | 10407 | type = build_pointer_type (ctype); |
6eabb241 | 10408 | if (decl_context == FIELD) |
8d08fdba | 10409 | { |
4dacf5bd KG |
10410 | if (! member_function_or_else (ctype, |
10411 | current_class_type, | |
10412 | flags)) | |
8d08fdba MS |
10413 | return void_type_node; |
10414 | TYPE_HAS_CONSTRUCTOR (ctype) = 1; | |
10415 | if (return_type != return_ctor) | |
10416 | return NULL_TREE; | |
10417 | } | |
10418 | } | |
10419 | if (decl_context == FIELD) | |
10420 | staticp = 0; | |
10421 | } | |
b7484fbe | 10422 | else if (friendp) |
8d08fdba | 10423 | { |
b7484fbe | 10424 | if (initialized) |
8251199e | 10425 | error ("can't initialize friend function `%s'", name); |
b7484fbe MS |
10426 | if (virtualp) |
10427 | { | |
10428 | /* Cannot be both friend and virtual. */ | |
8251199e | 10429 | error ("virtual functions cannot be friends"); |
b7484fbe MS |
10430 | RIDBIT_RESET (RID_FRIEND, specbits); |
10431 | friendp = 0; | |
10432 | } | |
28cbf42c | 10433 | if (decl_context == NORMAL) |
8251199e | 10434 | error ("friend declaration not in class definition"); |
28cbf42c | 10435 | if (current_function_decl && funcdef_flag) |
8251199e | 10436 | cp_error ("can't define friend function `%s' in a local class definition", |
28cbf42c | 10437 | name); |
8d08fdba MS |
10438 | } |
10439 | ||
8d08fdba MS |
10440 | /* Construct the function type and go to the next |
10441 | inner layer of declarator. */ | |
10442 | ||
f376e137 | 10443 | declarator = TREE_OPERAND (declarator, 0); |
8d08fdba | 10444 | |
f376e137 MS |
10445 | /* FIXME: This is where default args should be fully |
10446 | processed. */ | |
8d08fdba | 10447 | |
f376e137 | 10448 | arg_types = grokparms (inner_parms, funcdecl_p ? funcdef_flag : 0); |
8d08fdba | 10449 | |
4f70a846 | 10450 | if (declarator && flags == DTOR_FLAG) |
8d08fdba | 10451 | { |
4f70a846 MM |
10452 | /* A destructor declared in the body of a class will |
10453 | be represented as a BIT_NOT_EXPR. But, we just | |
10454 | want the underlying IDENTIFIER. */ | |
8d08fdba | 10455 | if (TREE_CODE (declarator) == BIT_NOT_EXPR) |
4f70a846 | 10456 | declarator = TREE_OPERAND (declarator, 0); |
68642fb6 | 10457 | |
4f70a846 MM |
10458 | if (strict_prototype == 0 && arg_types == NULL_TREE) |
10459 | arg_types = void_list_node; | |
10460 | else if (arg_types == NULL_TREE | |
10461 | || arg_types != void_list_node) | |
8d08fdba | 10462 | { |
4f70a846 MM |
10463 | cp_error ("destructors may not have parameters"); |
10464 | arg_types = void_list_node; | |
10465 | last_function_parms = NULL_TREE; | |
8d08fdba MS |
10466 | } |
10467 | } | |
10468 | ||
d22c8596 | 10469 | /* ANSI says that `const int foo ();' |
8d08fdba | 10470 | does not make the function foo const. */ |
d22c8596 | 10471 | type = build_function_type (type, arg_types); |
42976354 BK |
10472 | |
10473 | { | |
10474 | tree t; | |
10475 | for (t = arg_types; t; t = TREE_CHAIN (t)) | |
10476 | if (TREE_PURPOSE (t) | |
10477 | && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG) | |
10478 | { | |
10479 | add_defarg_fn (type); | |
10480 | break; | |
10481 | } | |
10482 | } | |
8d08fdba MS |
10483 | } |
10484 | break; | |
10485 | ||
10486 | case ADDR_EXPR: | |
10487 | case INDIRECT_REF: | |
10488 | /* Filter out pointers-to-references and references-to-references. | |
10489 | We can get these if a TYPE_DECL is used. */ | |
10490 | ||
10491 | if (TREE_CODE (type) == REFERENCE_TYPE) | |
10492 | { | |
8251199e | 10493 | error ("cannot declare %s to references", |
8d08fdba MS |
10494 | TREE_CODE (declarator) == ADDR_EXPR |
10495 | ? "references" : "pointers"); | |
10496 | declarator = TREE_OPERAND (declarator, 0); | |
10497 | continue; | |
10498 | } | |
10499 | ||
a5894242 MS |
10500 | if (TREE_CODE (type) == OFFSET_TYPE |
10501 | && (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE | |
10502 | || TREE_CODE (TREE_TYPE (type)) == REFERENCE_TYPE)) | |
10503 | { | |
8251199e | 10504 | cp_error ("cannot declare pointer to `%#T' member", |
a5894242 MS |
10505 | TREE_TYPE (type)); |
10506 | type = TREE_TYPE (type); | |
10507 | } | |
10508 | ||
61a127b3 MM |
10509 | /* Merge any constancy or volatility into the target type |
10510 | for the pointer. */ | |
10511 | ||
91063b51 MM |
10512 | /* We now know that the TYPE_QUALS don't apply to the decl, |
10513 | but to the target of the pointer. */ | |
10514 | type_quals = TYPE_UNQUALIFIED; | |
8d08fdba | 10515 | |
6eabb241 | 10516 | if (TREE_CODE (declarator) == ADDR_EXPR) |
8d08fdba | 10517 | { |
69851283 MM |
10518 | if (TREE_CODE (type) == VOID_TYPE) |
10519 | error ("invalid type: `void &'"); | |
8d08fdba | 10520 | else |
69851283 | 10521 | type = build_reference_type (type); |
8d08fdba MS |
10522 | } |
10523 | else if (TREE_CODE (type) == METHOD_TYPE) | |
d8f8dca1 | 10524 | type = build_ptrmemfunc_type (build_pointer_type (type)); |
8d08fdba MS |
10525 | else |
10526 | type = build_pointer_type (type); | |
10527 | ||
10528 | /* Process a list of type modifier keywords (such as | |
10529 | const or volatile) that were given inside the `*' or `&'. */ | |
10530 | ||
10531 | if (TREE_TYPE (declarator)) | |
10532 | { | |
10533 | register tree typemodlist; | |
10534 | int erred = 0; | |
91063b51 MM |
10535 | |
10536 | constp = 0; | |
10537 | volatilep = 0; | |
10538 | restrictp = 0; | |
8d08fdba MS |
10539 | for (typemodlist = TREE_TYPE (declarator); typemodlist; |
10540 | typemodlist = TREE_CHAIN (typemodlist)) | |
10541 | { | |
91063b51 MM |
10542 | tree qualifier = TREE_VALUE (typemodlist); |
10543 | ||
10544 | if (qualifier == ridpointers[(int) RID_CONST]) | |
8d08fdba | 10545 | constp++; |
91063b51 | 10546 | else if (qualifier == ridpointers[(int) RID_VOLATILE]) |
8d08fdba | 10547 | volatilep++; |
91063b51 MM |
10548 | else if (qualifier == ridpointers[(int) RID_RESTRICT]) |
10549 | restrictp++; | |
8d08fdba MS |
10550 | else if (!erred) |
10551 | { | |
10552 | erred = 1; | |
91063b51 | 10553 | error ("invalid type modifier within pointer declarator"); |
8d08fdba MS |
10554 | } |
10555 | } | |
10556 | if (constp > 1) | |
8251199e | 10557 | pedwarn ("duplicate `const'"); |
8d08fdba | 10558 | if (volatilep > 1) |
8251199e | 10559 | pedwarn ("duplicate `volatile'"); |
91063b51 MM |
10560 | if (restrictp > 1) |
10561 | pedwarn ("duplicate `restrict'"); | |
10562 | ||
10563 | type_quals = ((constp ? TYPE_QUAL_CONST : 0) | |
10564 | | (restrictp ? TYPE_QUAL_RESTRICT : 0) | |
10565 | | (volatilep ? TYPE_QUAL_VOLATILE : 0)); | |
a0a33927 MS |
10566 | if (TREE_CODE (declarator) == ADDR_EXPR |
10567 | && (constp || volatilep)) | |
10568 | { | |
10569 | if (constp) | |
8251199e | 10570 | pedwarn ("discarding `const' applied to a reference"); |
a0a33927 | 10571 | if (volatilep) |
8251199e | 10572 | pedwarn ("discarding `volatile' applied to a reference"); |
91063b51 | 10573 | type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE); |
a0a33927 | 10574 | } |
91063b51 | 10575 | type = cp_build_qualified_type (type, type_quals); |
8d08fdba MS |
10576 | } |
10577 | declarator = TREE_OPERAND (declarator, 0); | |
10578 | ctype = NULL_TREE; | |
10579 | break; | |
10580 | ||
10581 | case SCOPE_REF: | |
10582 | { | |
10583 | /* We have converted type names to NULL_TREE if the | |
10584 | name was bogus, or to a _TYPE node, if not. | |
10585 | ||
10586 | The variable CTYPE holds the type we will ultimately | |
10587 | resolve to. The code here just needs to build | |
10588 | up appropriate member types. */ | |
10589 | tree sname = TREE_OPERAND (declarator, 1); | |
386b8a85 JM |
10590 | tree t; |
10591 | ||
8d08fdba MS |
10592 | /* Destructors can have their visibilities changed as well. */ |
10593 | if (TREE_CODE (sname) == BIT_NOT_EXPR) | |
10594 | sname = TREE_OPERAND (sname, 0); | |
10595 | ||
10596 | if (TREE_COMPLEXITY (declarator) == 0) | |
10597 | /* This needs to be here, in case we are called | |
10598 | multiple times. */ ; | |
9a68c51f JM |
10599 | else if (TREE_COMPLEXITY (declarator) == -1) |
10600 | /* Namespace member. */ | |
10601 | pop_decl_namespace (); | |
8d08fdba | 10602 | else if (friendp && (TREE_COMPLEXITY (declarator) < 2)) |
2c73f9f5 | 10603 | /* Don't fall out into global scope. Hides real bug? --eichin */ ; |
5566b478 MS |
10604 | else if (! IS_AGGR_TYPE_CODE |
10605 | (TREE_CODE (TREE_OPERAND (declarator, 0)))) | |
10606 | ; | |
8d08fdba MS |
10607 | else if (TREE_COMPLEXITY (declarator) == current_class_depth) |
10608 | { | |
9a3b49ac MS |
10609 | /* Resolve any TYPENAME_TYPEs from the decl-specifier-seq |
10610 | that refer to ctype. They couldn't be resolved earlier | |
10611 | because we hadn't pushed into the class yet. | |
10612 | Example: resolve 'B<T>::type' in | |
10613 | 'B<typename B<T>::type> B<T>::f () { }'. */ | |
10614 | if (current_template_parms | |
10615 | && uses_template_parms (type) | |
10616 | && uses_template_parms (current_class_type)) | |
10617 | { | |
10618 | tree args = current_template_args (); | |
4393e105 | 10619 | type = tsubst (type, args, /*complain=*/1, NULL_TREE); |
9a3b49ac MS |
10620 | } |
10621 | ||
a28e3c7f MS |
10622 | /* This pop_nested_class corresponds to the |
10623 | push_nested_class used to push into class scope for | |
10624 | parsing the argument list of a function decl, in | |
10625 | qualified_id. */ | |
b74a0560 | 10626 | pop_nested_class (); |
8d08fdba MS |
10627 | TREE_COMPLEXITY (declarator) = current_class_depth; |
10628 | } | |
10629 | else | |
10630 | my_friendly_abort (16); | |
10631 | ||
10632 | if (TREE_OPERAND (declarator, 0) == NULL_TREE) | |
10633 | { | |
10634 | /* We had a reference to a global decl, or | |
10635 | perhaps we were given a non-aggregate typedef, | |
10636 | in which case we cleared this out, and should just | |
10637 | keep going as though it wasn't there. */ | |
10638 | declarator = sname; | |
10639 | continue; | |
10640 | } | |
10641 | ctype = TREE_OPERAND (declarator, 0); | |
10642 | ||
386b8a85 | 10643 | t = ctype; |
68642fb6 | 10644 | while (t != NULL_TREE && CLASS_TYPE_P (t)) |
386b8a85 | 10645 | { |
75650646 MM |
10646 | if (CLASSTYPE_TEMPLATE_INFO (t) && |
10647 | !CLASSTYPE_TEMPLATE_SPECIALIZATION (t)) | |
386b8a85 JM |
10648 | template_count += 1; |
10649 | t = TYPE_MAIN_DECL (t); | |
10650 | if (DECL_LANG_SPECIFIC (t)) | |
4f1c5b7d | 10651 | t = DECL_CONTEXT (t); |
386b8a85 JM |
10652 | else |
10653 | t = NULL_TREE; | |
10654 | } | |
10655 | ||
8d08fdba MS |
10656 | if (sname == NULL_TREE) |
10657 | goto done_scoping; | |
10658 | ||
10659 | if (TREE_CODE (sname) == IDENTIFIER_NODE) | |
10660 | { | |
10661 | /* This is the `standard' use of the scoping operator: | |
10662 | basetype :: member . */ | |
10663 | ||
db5ae43f | 10664 | if (ctype == current_class_type) |
28cbf42c MS |
10665 | { |
10666 | /* class A { | |
10667 | void A::f (); | |
10668 | }; | |
10669 | ||
10670 | Is this ill-formed? */ | |
10671 | ||
10672 | if (pedantic) | |
8251199e | 10673 | cp_pedwarn ("extra qualification `%T::' on member `%s' ignored", |
28cbf42c MS |
10674 | ctype, name); |
10675 | } | |
db5ae43f | 10676 | else if (TREE_CODE (type) == FUNCTION_TYPE) |
8d08fdba MS |
10677 | { |
10678 | if (current_class_type == NULL_TREE | |
8d08fdba | 10679 | || friendp) |
14ae7e7d JM |
10680 | type = build_cplus_method_type (ctype, TREE_TYPE (type), |
10681 | TYPE_ARG_TYPES (type)); | |
8d08fdba MS |
10682 | else |
10683 | { | |
8251199e | 10684 | cp_error ("cannot declare member function `%T::%s' within `%T'", |
a3203465 | 10685 | ctype, name, current_class_type); |
8d08fdba MS |
10686 | return void_type_node; |
10687 | } | |
10688 | } | |
5566b478 MS |
10689 | else if (RIDBIT_SETP (RID_TYPEDEF, specbits) |
10690 | || TYPE_SIZE (complete_type (ctype)) != NULL_TREE) | |
8d08fdba | 10691 | { |
2c73f9f5 | 10692 | /* Have to move this code elsewhere in this function. |
db5ae43f MS |
10693 | this code is used for i.e., typedef int A::M; M *pm; |
10694 | ||
10695 | It is? How? jason 10/2/94 */ | |
8d08fdba | 10696 | |
8d08fdba MS |
10697 | if (current_class_type) |
10698 | { | |
8251199e | 10699 | cp_error ("cannot declare member `%T::%s' within `%T'", |
db5ae43f MS |
10700 | ctype, name, current_class_type); |
10701 | return void_type_node; | |
8d08fdba MS |
10702 | } |
10703 | type = build_offset_type (ctype, type); | |
10704 | } | |
10705 | else if (uses_template_parms (ctype)) | |
10706 | { | |
8d08fdba | 10707 | if (TREE_CODE (type) == FUNCTION_TYPE) |
beb53fb8 | 10708 | type |
14ae7e7d | 10709 | = build_cplus_method_type (ctype, TREE_TYPE (type), |
beb53fb8 | 10710 | TYPE_ARG_TYPES (type)); |
8d08fdba MS |
10711 | } |
10712 | else | |
10713 | { | |
8251199e | 10714 | cp_error ("structure `%T' not yet defined", ctype); |
8d08fdba MS |
10715 | return error_mark_node; |
10716 | } | |
10717 | ||
10718 | declarator = sname; | |
10719 | } | |
8d08fdba MS |
10720 | else if (TREE_CODE (sname) == SCOPE_REF) |
10721 | my_friendly_abort (17); | |
10722 | else | |
10723 | { | |
10724 | done_scoping: | |
10725 | declarator = TREE_OPERAND (declarator, 1); | |
10726 | if (declarator && TREE_CODE (declarator) == CALL_EXPR) | |
10727 | /* In this case, we will deal with it later. */ | |
10728 | ; | |
10729 | else | |
10730 | { | |
10731 | if (TREE_CODE (type) == FUNCTION_TYPE) | |
14ae7e7d JM |
10732 | type = build_cplus_method_type (ctype, TREE_TYPE (type), |
10733 | TYPE_ARG_TYPES (type)); | |
8d08fdba MS |
10734 | else |
10735 | type = build_offset_type (ctype, type); | |
10736 | } | |
10737 | } | |
10738 | } | |
10739 | break; | |
10740 | ||
10741 | case BIT_NOT_EXPR: | |
10742 | declarator = TREE_OPERAND (declarator, 0); | |
10743 | break; | |
10744 | ||
8d08fdba MS |
10745 | case RECORD_TYPE: |
10746 | case UNION_TYPE: | |
10747 | case ENUMERAL_TYPE: | |
10748 | declarator = NULL_TREE; | |
10749 | break; | |
10750 | ||
10751 | case ERROR_MARK: | |
10752 | declarator = NULL_TREE; | |
10753 | break; | |
10754 | ||
10755 | default: | |
10756 | my_friendly_abort (158); | |
10757 | } | |
10758 | } | |
10759 | ||
1eab9b56 JM |
10760 | /* See the comment for the TREE_LIST case, above. */ |
10761 | if (inner_attrs) | |
10762 | { | |
10763 | if (! ignore_attrs) | |
10764 | decl_attributes (type, inner_attrs, NULL_TREE); | |
10765 | else if (attrlist) | |
10766 | TREE_VALUE (attrlist) = chainon (inner_attrs, TREE_VALUE (attrlist)); | |
10767 | else | |
10768 | attrlist = build_decl_list (NULL_TREE, inner_attrs); | |
10769 | } | |
10770 | ||
419c6212 JM |
10771 | /* Now TYPE has the actual type. */ |
10772 | ||
34fc957d | 10773 | if (explicitp == 1 || (explicitp && friendp)) |
db5ae43f | 10774 | { |
34fc957d NS |
10775 | /* [dcl.fct.spec] The explicit specifier shall only be used in |
10776 | declarations of constructors within a class definition. */ | |
10777 | error ("only declarations of constructors can be `explicit'"); | |
db5ae43f MS |
10778 | explicitp = 0; |
10779 | } | |
10780 | ||
f30432d7 MS |
10781 | if (RIDBIT_SETP (RID_MUTABLE, specbits)) |
10782 | { | |
34fc957d NS |
10783 | if (current_class_name == NULL_TREE || decl_context == PARM || friendp) |
10784 | { | |
10785 | error ("non-member `%s' cannot be declared `mutable'", name); | |
10786 | RIDBIT_RESET (RID_MUTABLE, specbits); | |
10787 | } | |
10788 | else if (decl_context == TYPENAME || RIDBIT_SETP (RID_TYPEDEF, specbits)) | |
f30432d7 | 10789 | { |
34fc957d | 10790 | error ("non-object member `%s' cannot be declared `mutable'", name); |
f30432d7 MS |
10791 | RIDBIT_RESET (RID_MUTABLE, specbits); |
10792 | } | |
34fc957d NS |
10793 | else if (TREE_CODE (type) == FUNCTION_TYPE |
10794 | || TREE_CODE (type) == METHOD_TYPE) | |
10795 | { | |
10796 | error ("function `%s' cannot be declared `mutable'", name); | |
10797 | RIDBIT_RESET (RID_MUTABLE, specbits); | |
10798 | } | |
f30432d7 MS |
10799 | else if (staticp) |
10800 | { | |
8251199e | 10801 | error ("static `%s' cannot be declared `mutable'", name); |
f30432d7 MS |
10802 | RIDBIT_RESET (RID_MUTABLE, specbits); |
10803 | } | |
34fc957d NS |
10804 | else if (type_quals & TYPE_QUAL_CONST) |
10805 | { | |
10806 | error ("const `%s' cannot be declared `mutable'", name); | |
10807 | RIDBIT_RESET (RID_MUTABLE, specbits); | |
10808 | } | |
f30432d7 MS |
10809 | } |
10810 | ||
efe38fab JM |
10811 | if (declarator == NULL_TREE |
10812 | || TREE_CODE (declarator) == IDENTIFIER_NODE | |
10813 | || (TREE_CODE (declarator) == TEMPLATE_ID_EXPR | |
10814 | && (TREE_CODE (type) == FUNCTION_TYPE | |
10815 | || TREE_CODE (type) == METHOD_TYPE))) | |
10816 | /* OK */; | |
10817 | else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) | |
10818 | { | |
10819 | cp_error ("template-id `%D' used as a declarator", declarator); | |
10820 | declarator = dname; | |
10821 | } | |
10822 | else | |
419c6212 | 10823 | /* Unexpected declarator format. */ |
efe38fab JM |
10824 | my_friendly_abort (990210); |
10825 | ||
419c6212 JM |
10826 | /* If this is declaring a typedef name, return a TYPE_DECL. */ |
10827 | ||
fc378698 | 10828 | if (RIDBIT_SETP (RID_TYPEDEF, specbits) && decl_context != TYPENAME) |
8d08fdba MS |
10829 | { |
10830 | tree decl; | |
10831 | ||
10832 | /* Note that the grammar rejects storage classes | |
10833 | in typenames, fields or parameters. */ | |
eff71ab0 PB |
10834 | if (current_lang_name == lang_name_java) |
10835 | TYPE_FOR_JAVA (type) = 1; | |
8d08fdba | 10836 | |
d2e5ee5c MS |
10837 | if (decl_context == FIELD) |
10838 | { | |
094fe153 | 10839 | if (declarator == constructor_name (current_class_type)) |
cb9a3ff8 | 10840 | cp_pedwarn ("ISO C++ forbids nested type `%D' with same name as enclosing class", |
d2e5ee5c MS |
10841 | declarator); |
10842 | decl = build_lang_decl (TYPE_DECL, declarator, type); | |
d2e5ee5c MS |
10843 | } |
10844 | else | |
7fb4a8f7 JM |
10845 | { |
10846 | /* Make sure this typedef lives as long as its type, | |
10847 | since it might be used as a template parameter. */ | |
9188c363 | 10848 | if (processing_template_decl) |
b0d06515 | 10849 | decl = build_lang_decl (TYPE_DECL, declarator, type); |
9188c363 MM |
10850 | else |
10851 | decl = build_decl (TYPE_DECL, declarator, type); | |
7fb4a8f7 | 10852 | } |
d2e5ee5c | 10853 | |
9188c363 MM |
10854 | /* If the user declares "typedef struct {...} foo" then the |
10855 | struct will have an anonymous name. Fill that name in now. | |
10856 | Nothing can refer to it, so nothing needs know about the name | |
10857 | change. */ | |
8d08fdba MS |
10858 | if (type != error_mark_node |
10859 | && TYPE_NAME (type) | |
10860 | && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL | |
b537d120 MM |
10861 | && ANON_AGGRNAME_P (TYPE_IDENTIFIER (type)) |
10862 | && CP_TYPE_QUALS (type) == TYPE_UNQUALIFIED) | |
8d08fdba | 10863 | { |
dcd08efc JM |
10864 | tree oldname = TYPE_NAME (type); |
10865 | tree t; | |
10866 | ||
2c73f9f5 | 10867 | /* Replace the anonymous name with the real name everywhere. */ |
8d08fdba | 10868 | lookup_tag_reverse (type, declarator); |
dcd08efc JM |
10869 | for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) |
10870 | if (TYPE_NAME (t) == oldname) | |
10871 | TYPE_NAME (t) = decl; | |
8d08fdba MS |
10872 | |
10873 | if (TYPE_LANG_SPECIFIC (type)) | |
10874 | TYPE_WAS_ANONYMOUS (type) = 1; | |
10875 | ||
33964bf4 MM |
10876 | /* If this is a typedef within a template class, the nested |
10877 | type is a (non-primary) template. The name for the | |
10878 | template needs updating as well. */ | |
10879 | if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type)) | |
68642fb6 | 10880 | DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)) |
33964bf4 MM |
10881 | = TYPE_IDENTIFIER (type); |
10882 | ||
68642fb6 | 10883 | /* XXX Temporarily set the scope. |
2c73f9f5 ML |
10884 | When returning, start_decl expects it as NULL_TREE, |
10885 | and will then then set it using pushdecl. */ | |
10886 | my_friendly_assert (DECL_CONTEXT (decl) == NULL_TREE, 980404); | |
10887 | if (current_class_type) | |
10888 | DECL_CONTEXT (decl) = current_class_type; | |
10889 | else | |
cb0dbb9a | 10890 | DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); |
2c73f9f5 | 10891 | |
d2e5ee5c MS |
10892 | DECL_ASSEMBLER_NAME (decl) = DECL_NAME (decl); |
10893 | DECL_ASSEMBLER_NAME (decl) | |
10894 | = get_identifier (build_overload_name (type, 1, 1)); | |
2c73f9f5 | 10895 | DECL_CONTEXT (decl) = NULL_TREE; |
50a6dbd7 JM |
10896 | |
10897 | /* FIXME remangle member functions; member functions of a | |
10898 | type with external linkage have external linkage. */ | |
fc378698 | 10899 | } |
fc378698 | 10900 | |
8d08fdba MS |
10901 | if (TREE_CODE (type) == OFFSET_TYPE || TREE_CODE (type) == METHOD_TYPE) |
10902 | { | |
8251199e | 10903 | cp_error_at ("typedef name may not be class-qualified", decl); |
a3203465 | 10904 | return NULL_TREE; |
8d08fdba MS |
10905 | } |
10906 | else if (quals) | |
10907 | { | |
10908 | if (ctype == NULL_TREE) | |
10909 | { | |
10910 | if (TREE_CODE (type) != METHOD_TYPE) | |
cb9a3ff8 | 10911 | cp_error_at ("invalid type qualifier for non-member function type", decl); |
8d08fdba MS |
10912 | else |
10913 | ctype = TYPE_METHOD_BASETYPE (type); | |
10914 | } | |
10915 | if (ctype != NULL_TREE) | |
10916 | grok_method_quals (ctype, decl, quals); | |
10917 | } | |
10918 | ||
10919 | if (RIDBIT_SETP (RID_SIGNED, specbits) | |
10920 | || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))) | |
10921 | C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1; | |
10922 | ||
c91a56d2 MS |
10923 | bad_specifiers (decl, "type", virtualp, quals != NULL_TREE, |
10924 | inlinep, friendp, raises != NULL_TREE); | |
10925 | ||
10926 | if (initialized) | |
8251199e | 10927 | error ("typedef declaration includes an initializer"); |
c91a56d2 | 10928 | |
8d08fdba MS |
10929 | return decl; |
10930 | } | |
10931 | ||
10932 | /* Detect the case of an array type of unspecified size | |
10933 | which came, as such, direct from a typedef name. | |
10934 | We must copy the type, so that each identifier gets | |
10935 | a distinct type, so that each identifier's size can be | |
10936 | controlled separately by its own initializer. */ | |
10937 | ||
10938 | if (type == typedef_type && TREE_CODE (type) == ARRAY_TYPE | |
10939 | && TYPE_DOMAIN (type) == NULL_TREE) | |
10940 | { | |
10941 | type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type)); | |
10942 | } | |
10943 | ||
10944 | /* If this is a type name (such as, in a cast or sizeof), | |
10945 | compute the type and return it now. */ | |
10946 | ||
10947 | if (decl_context == TYPENAME) | |
10948 | { | |
10949 | /* Note that the grammar rejects storage classes | |
10950 | in typenames, fields or parameters. */ | |
91063b51 | 10951 | if (type_quals != TYPE_UNQUALIFIED) |
6eabb241 | 10952 | type_quals = TYPE_UNQUALIFIED; |
8d08fdba MS |
10953 | |
10954 | /* Special case: "friend class foo" looks like a TYPENAME context. */ | |
10955 | if (friendp) | |
10956 | { | |
91063b51 | 10957 | if (type_quals != TYPE_UNQUALIFIED) |
b7484fbe | 10958 | { |
91063b51 MM |
10959 | cp_error ("type qualifiers specified for friend class declaration"); |
10960 | type_quals = TYPE_UNQUALIFIED; | |
b7484fbe MS |
10961 | } |
10962 | if (inlinep) | |
10963 | { | |
8251199e | 10964 | cp_error ("`inline' specified for friend class declaration"); |
b7484fbe MS |
10965 | inlinep = 0; |
10966 | } | |
10967 | ||
10968 | /* Only try to do this stuff if we didn't already give up. */ | |
10969 | if (type != integer_type_node) | |
10970 | { | |
10971 | /* A friendly class? */ | |
10972 | if (current_class_type) | |
10973 | make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type)); | |
10974 | else | |
8251199e | 10975 | error ("trying to make class `%s' a friend of global scope", |
b7484fbe MS |
10976 | TYPE_NAME_STRING (type)); |
10977 | type = void_type_node; | |
10978 | } | |
8d08fdba MS |
10979 | } |
10980 | else if (quals) | |
10981 | { | |
8d08fdba MS |
10982 | if (ctype == NULL_TREE) |
10983 | { | |
41cbc04c NS |
10984 | if (TREE_CODE (type) != METHOD_TYPE) |
10985 | cp_error ("invalid qualifiers on non-member function type"); | |
10986 | else | |
10987 | ctype = TYPE_METHOD_BASETYPE (type); | |
10988 | } | |
10989 | if (ctype) | |
10990 | { | |
10991 | tree dummy = build_decl (TYPE_DECL, declarator, type); | |
10992 | grok_method_quals (ctype, dummy, quals); | |
10993 | type = TREE_TYPE (dummy); | |
8d08fdba | 10994 | } |
8d08fdba MS |
10995 | } |
10996 | ||
10997 | return type; | |
10998 | } | |
10999 | else if (declarator == NULL_TREE && decl_context != PARM | |
db5ae43f | 11000 | && decl_context != CATCHPARM |
8d08fdba MS |
11001 | && TREE_CODE (type) != UNION_TYPE |
11002 | && ! bitfield) | |
11003 | { | |
8251199e | 11004 | cp_error ("abstract declarator `%T' used as declaration", type); |
8d08fdba MS |
11005 | declarator = make_anon_name (); |
11006 | } | |
11007 | ||
11008 | /* `void' at top level (not within pointer) | |
11009 | is allowed only in typedefs or type names. | |
11010 | We don't complain about parms either, but that is because | |
11011 | a better error message can be made later. */ | |
11012 | ||
a1774733 | 11013 | if (TREE_CODE (type) == VOID_TYPE && decl_context != PARM) |
8d08fdba | 11014 | { |
b7484fbe | 11015 | if (! declarator) |
8251199e | 11016 | error ("unnamed variable or field declared void"); |
b7484fbe | 11017 | else if (TREE_CODE (declarator) == IDENTIFIER_NODE) |
8d08fdba MS |
11018 | { |
11019 | if (IDENTIFIER_OPNAME_P (declarator)) | |
8d08fdba | 11020 | my_friendly_abort (356); |
8d08fdba | 11021 | else |
8251199e | 11022 | error ("variable or field `%s' declared void", name); |
8d08fdba MS |
11023 | } |
11024 | else | |
8251199e | 11025 | error ("variable or field declared void"); |
8d08fdba MS |
11026 | type = integer_type_node; |
11027 | } | |
11028 | ||
11029 | /* Now create the decl, which may be a VAR_DECL, a PARM_DECL | |
11030 | or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */ | |
11031 | ||
14ae7e7d JM |
11032 | if (decl_context == PARM || decl_context == CATCHPARM) |
11033 | { | |
11034 | if (ctype || in_namespace) | |
11035 | error ("cannot use `::' in parameter declaration"); | |
11036 | ||
11037 | /* A parameter declared as an array of T is really a pointer to T. | |
11038 | One declared as a function is really a pointer to a function. | |
11039 | One declared as a member is really a pointer to member. */ | |
11040 | ||
11041 | if (TREE_CODE (type) == ARRAY_TYPE) | |
11042 | { | |
11043 | /* Transfer const-ness of array into that of type pointed to. */ | |
11044 | type = build_pointer_type (TREE_TYPE (type)); | |
91063b51 | 11045 | type_quals = TYPE_UNQUALIFIED; |
14ae7e7d JM |
11046 | } |
11047 | else if (TREE_CODE (type) == FUNCTION_TYPE) | |
11048 | type = build_pointer_type (type); | |
11049 | else if (TREE_CODE (type) == OFFSET_TYPE) | |
11050 | type = build_pointer_type (type); | |
11051 | else if (TREE_CODE (type) == VOID_TYPE && declarator) | |
11052 | { | |
11053 | error ("declaration of `%s' as void", name); | |
11054 | return NULL_TREE; | |
11055 | } | |
11056 | } | |
68642fb6 | 11057 | |
8d08fdba MS |
11058 | { |
11059 | register tree decl; | |
11060 | ||
11061 | if (decl_context == PARM) | |
11062 | { | |
ff350acd | 11063 | decl = build_decl (PARM_DECL, declarator, type); |
8d08fdba MS |
11064 | |
11065 | bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE, | |
11066 | inlinep, friendp, raises != NULL_TREE); | |
8d08fdba MS |
11067 | |
11068 | /* Compute the type actually passed in the parmlist, | |
11069 | for the case where there is no prototype. | |
11070 | (For example, shorts and chars are passed as ints.) | |
11071 | When there is a prototype, this is overridden later. */ | |
11072 | ||
39211cd5 | 11073 | DECL_ARG_TYPE (decl) = type_promotes_to (type); |
8d08fdba MS |
11074 | } |
11075 | else if (decl_context == FIELD) | |
11076 | { | |
11077 | if (type == error_mark_node) | |
11078 | { | |
11079 | /* Happens when declaring arrays of sizes which | |
11080 | are error_mark_node, for example. */ | |
11081 | decl = NULL_TREE; | |
11082 | } | |
997a088c | 11083 | else if (in_namespace && !friendp) |
05008fb9 MM |
11084 | { |
11085 | /* Something like struct S { int N::j; }; */ | |
8251199e | 11086 | cp_error ("invalid use of `::'"); |
05008fb9 MM |
11087 | decl = NULL_TREE; |
11088 | } | |
8d08fdba MS |
11089 | else if (TREE_CODE (type) == FUNCTION_TYPE) |
11090 | { | |
11091 | int publicp = 0; | |
e1467ff2 | 11092 | tree function_context; |
8d08fdba | 11093 | |
72b7eeff MS |
11094 | /* We catch the others as conflicts with the builtin |
11095 | typedefs. */ | |
11096 | if (friendp && declarator == ridpointers[(int) RID_SIGNED]) | |
11097 | { | |
8251199e | 11098 | cp_error ("function `%D' cannot be declared friend", |
72b7eeff MS |
11099 | declarator); |
11100 | friendp = 0; | |
11101 | } | |
11102 | ||
8d08fdba MS |
11103 | if (friendp == 0) |
11104 | { | |
11105 | if (ctype == NULL_TREE) | |
11106 | ctype = current_class_type; | |
11107 | ||
11108 | if (ctype == NULL_TREE) | |
11109 | { | |
8251199e | 11110 | cp_error ("can't make `%D' into a method -- not in a class", |
8d08fdba MS |
11111 | declarator); |
11112 | return void_type_node; | |
11113 | } | |
11114 | ||
11115 | /* ``A union may [ ... ] not [ have ] virtual functions.'' | |
11116 | ARM 9.5 */ | |
11117 | if (virtualp && TREE_CODE (ctype) == UNION_TYPE) | |
11118 | { | |
8251199e | 11119 | cp_error ("function `%D' declared virtual inside a union", |
8d08fdba MS |
11120 | declarator); |
11121 | return void_type_node; | |
11122 | } | |
11123 | ||
11124 | if (declarator == ansi_opname[(int) NEW_EXPR] | |
a28e3c7f MS |
11125 | || declarator == ansi_opname[(int) VEC_NEW_EXPR] |
11126 | || declarator == ansi_opname[(int) DELETE_EXPR] | |
11127 | || declarator == ansi_opname[(int) VEC_DELETE_EXPR]) | |
8d08fdba MS |
11128 | { |
11129 | if (virtualp) | |
11130 | { | |
8251199e | 11131 | cp_error ("`%D' cannot be declared virtual, since it is always static", |
8d08fdba MS |
11132 | declarator); |
11133 | virtualp = 0; | |
11134 | } | |
11135 | } | |
11136 | else if (staticp < 2) | |
14ae7e7d JM |
11137 | type = build_cplus_method_type (ctype, TREE_TYPE (type), |
11138 | TYPE_ARG_TYPES (type)); | |
8d08fdba MS |
11139 | } |
11140 | ||
11141 | /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */ | |
68642fb6 | 11142 | function_context = (ctype != NULL_TREE) ? |
4f1c5b7d | 11143 | decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE; |
e1467ff2 MM |
11144 | publicp = (! friendp || ! staticp) |
11145 | && function_context == NULL_TREE; | |
68642fb6 | 11146 | decl = grokfndecl (ctype, type, |
386b8a85 JM |
11147 | TREE_CODE (declarator) != TEMPLATE_ID_EXPR |
11148 | ? declarator : dname, | |
11149 | declarator, | |
7a8f9fa9 | 11150 | virtualp, flags, quals, raises, |
386b8a85 | 11151 | friendp ? -1 : 0, friendp, publicp, inlinep, |
2c73f9f5 | 11152 | funcdef_flag, template_count, in_namespace); |
20496fa2 | 11153 | if (decl == NULL_TREE) |
3ddfb0e6 | 11154 | return decl; |
6125f3be DE |
11155 | #if 0 |
11156 | /* This clobbers the attrs stored in `decl' from `attrlist'. */ | |
11157 | /* The decl and setting of decl_machine_attr is also turned off. */ | |
71851aaa | 11158 | decl = build_decl_attribute_variant (decl, decl_machine_attr); |
6125f3be | 11159 | #endif |
f0e01782 | 11160 | |
cc804e51 MM |
11161 | /* [class.conv.ctor] |
11162 | ||
11163 | A constructor declared without the function-specifier | |
11164 | explicit that can be called with a single parameter | |
11165 | specifies a conversion from the type of its first | |
11166 | parameter to the type of its class. Such a constructor | |
11167 | is called a converting constructor. */ | |
db5ae43f MS |
11168 | if (explicitp == 2) |
11169 | DECL_NONCONVERTING_P (decl) = 1; | |
cc804e51 MM |
11170 | else if (DECL_CONSTRUCTOR_P (decl)) |
11171 | { | |
11172 | /* The constructor can be called with exactly one | |
11173 | parameter if there is at least one parameter, and | |
11174 | any subsequent parameters have default arguments. | |
11175 | We don't look at the first parameter, which is | |
11176 | really just the `this' parameter for the new | |
11177 | object. */ | |
68642fb6 | 11178 | tree arg_types = |
cc804e51 MM |
11179 | TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl))); |
11180 | ||
11181 | /* Skip the `in_chrg' argument too, if present. */ | |
11182 | if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (decl))) | |
11183 | arg_types = TREE_CHAIN (arg_types); | |
11184 | ||
11185 | if (arg_types == void_list_node | |
68642fb6 UD |
11186 | || (arg_types |
11187 | && TREE_CHAIN (arg_types) | |
cc804e51 MM |
11188 | && TREE_CHAIN (arg_types) != void_list_node |
11189 | && !TREE_PURPOSE (TREE_CHAIN (arg_types)))) | |
11190 | DECL_NONCONVERTING_P (decl) = 1; | |
11191 | } | |
8d08fdba MS |
11192 | } |
11193 | else if (TREE_CODE (type) == METHOD_TYPE) | |
11194 | { | |
faae18ab MS |
11195 | /* We only get here for friend declarations of |
11196 | members of other classes. */ | |
8d08fdba MS |
11197 | /* All method decls are public, so tell grokfndecl to set |
11198 | TREE_PUBLIC, also. */ | |
386b8a85 | 11199 | decl = grokfndecl (ctype, type, declarator, declarator, |
7a8f9fa9 | 11200 | virtualp, flags, quals, raises, |
386b8a85 | 11201 | friendp ? -1 : 0, friendp, 1, 0, funcdef_flag, |
2c73f9f5 | 11202 | template_count, in_namespace); |
f0e01782 MS |
11203 | if (decl == NULL_TREE) |
11204 | return NULL_TREE; | |
8d08fdba | 11205 | } |
5566b478 MS |
11206 | else if (!staticp && ! processing_template_decl |
11207 | && TYPE_SIZE (complete_type (type)) == NULL_TREE | |
8d08fdba MS |
11208 | && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0)) |
11209 | { | |
b7484fbe | 11210 | if (declarator) |
8251199e | 11211 | cp_error ("field `%D' has incomplete type", declarator); |
b7484fbe | 11212 | else |
8251199e | 11213 | cp_error ("name `%T' has incomplete type", type); |
8d08fdba MS |
11214 | |
11215 | /* If we're instantiating a template, tell them which | |
11216 | instantiation made the field's type be incomplete. */ | |
11217 | if (current_class_type | |
11218 | && TYPE_NAME (current_class_type) | |
d2e5ee5c | 11219 | && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type)) |
8d08fdba MS |
11220 | && declspecs && TREE_VALUE (declspecs) |
11221 | && TREE_TYPE (TREE_VALUE (declspecs)) == type) | |
8251199e | 11222 | cp_error (" in instantiation of template `%T'", |
db5ae43f MS |
11223 | current_class_type); |
11224 | ||
8d08fdba MS |
11225 | type = error_mark_node; |
11226 | decl = NULL_TREE; | |
11227 | } | |
11228 | else | |
11229 | { | |
11230 | if (friendp) | |
11231 | { | |
cb9a3ff8 | 11232 | error ("`%s' is neither function nor member function; cannot be declared friend", |
8d08fdba MS |
11233 | IDENTIFIER_POINTER (declarator)); |
11234 | friendp = 0; | |
11235 | } | |
11236 | decl = NULL_TREE; | |
11237 | } | |
11238 | ||
11239 | if (friendp) | |
11240 | { | |
11241 | /* Friends are treated specially. */ | |
11242 | if (ctype == current_class_type) | |
8251199e | 11243 | warning ("member functions are implicitly friends of their class"); |
8d08fdba MS |
11244 | else |
11245 | { | |
11246 | tree t = NULL_TREE; | |
11247 | if (decl && DECL_NAME (decl)) | |
36a117a5 MM |
11248 | { |
11249 | if (template_class_depth (current_class_type) == 0) | |
6c30752f | 11250 | { |
68642fb6 UD |
11251 | decl |
11252 | = check_explicit_specialization | |
6c30752f MM |
11253 | (declarator, decl, |
11254 | template_count, 2 * (funcdef_flag != 0) + 4); | |
11255 | if (decl == error_mark_node) | |
11256 | return error_mark_node; | |
11257 | } | |
11258 | ||
36a117a5 | 11259 | t = do_friend (ctype, declarator, decl, |
7a8f9fa9 | 11260 | last_function_parms, attrlist, flags, quals, |
36a117a5 MM |
11261 | funcdef_flag); |
11262 | } | |
8d08fdba MS |
11263 | if (t && funcdef_flag) |
11264 | return t; | |
68642fb6 | 11265 | |
8d08fdba MS |
11266 | return void_type_node; |
11267 | } | |
11268 | } | |
11269 | ||
11270 | /* Structure field. It may not be a function, except for C++ */ | |
11271 | ||
11272 | if (decl == NULL_TREE) | |
11273 | { | |
8d08fdba MS |
11274 | if (initialized) |
11275 | { | |
3ac3d9ea MM |
11276 | if (!staticp) |
11277 | { | |
11278 | /* An attempt is being made to initialize a non-static | |
11279 | member. But, from [class.mem]: | |
68642fb6 | 11280 | |
3ac3d9ea MM |
11281 | 4 A member-declarator can contain a |
11282 | constant-initializer only if it declares a static | |
11283 | member (_class.static_) of integral or enumeration | |
68642fb6 | 11284 | type, see _class.static.data_. |
3ac3d9ea MM |
11285 | |
11286 | This used to be relatively common practice, but | |
11287 | the rest of the compiler does not correctly | |
11288 | handle the initialization unless the member is | |
11289 | static so we make it static below. */ | |
cab1f180 | 11290 | cp_pedwarn ("ISO C++ forbids initialization of member `%D'", |
3ac3d9ea | 11291 | declarator); |
8251199e | 11292 | cp_pedwarn ("making `%D' static", declarator); |
3ac3d9ea MM |
11293 | staticp = 1; |
11294 | } | |
11295 | ||
6ba89f8e MM |
11296 | if (uses_template_parms (type)) |
11297 | /* We'll check at instantiation time. */ | |
11298 | ; | |
11299 | else if (check_static_variable_definition (declarator, | |
11300 | type)) | |
11301 | /* If we just return the declaration, crashes | |
11302 | will sometimes occur. We therefore return | |
72a93143 JM |
11303 | void_type_node, as if this was a friend |
11304 | declaration, to cause callers to completely | |
11305 | ignore this declaration. */ | |
6ba89f8e | 11306 | return void_type_node; |
8d08fdba MS |
11307 | } |
11308 | ||
8ebeee52 | 11309 | /* 9.2p13 [class.mem] */ |
4d7614e9 JM |
11310 | if (declarator == constructor_name (current_class_type) |
11311 | /* Divergence from the standard: In extern "C", we | |
11312 | allow non-static data members here, because C does | |
11313 | and /usr/include/netinet/in.h uses that. */ | |
1f901793 | 11314 | && (staticp || ! in_system_header)) |
cb9a3ff8 | 11315 | cp_pedwarn ("ISO C++ forbids data member `%D' with same name as enclosing class", |
8ebeee52 JM |
11316 | declarator); |
11317 | ||
3ac3d9ea | 11318 | if (staticp) |
8d08fdba | 11319 | { |
f18a14bc MM |
11320 | /* C++ allows static class members. All other work |
11321 | for this is done by grokfield. */ | |
4ce3d537 | 11322 | decl = build_lang_decl (VAR_DECL, declarator, type); |
5b605f68 MS |
11323 | TREE_STATIC (decl) = 1; |
11324 | /* In class context, 'static' means public access. */ | |
3ac3d9ea | 11325 | TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = 1; |
8d08fdba MS |
11326 | } |
11327 | else | |
11328 | { | |
4ce3d537 | 11329 | decl = build_lang_decl (FIELD_DECL, declarator, type); |
8d08fdba MS |
11330 | if (RIDBIT_SETP (RID_MUTABLE, specbits)) |
11331 | { | |
11332 | DECL_MUTABLE_P (decl) = 1; | |
11333 | RIDBIT_RESET (RID_MUTABLE, specbits); | |
11334 | } | |
11335 | } | |
11336 | ||
11337 | bad_specifiers (decl, "field", virtualp, quals != NULL_TREE, | |
11338 | inlinep, friendp, raises != NULL_TREE); | |
11339 | } | |
11340 | } | |
11341 | else if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE) | |
11342 | { | |
386b8a85 | 11343 | tree original_name; |
8d08fdba MS |
11344 | int publicp = 0; |
11345 | ||
11346 | if (! declarator) | |
11347 | return NULL_TREE; | |
11348 | ||
386b8a85 JM |
11349 | if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) |
11350 | original_name = dname; | |
11351 | else | |
11352 | original_name = declarator; | |
11353 | ||
8926095f | 11354 | if (RIDBIT_SETP (RID_AUTO, specbits)) |
8251199e | 11355 | error ("storage class `auto' invalid for function `%s'", name); |
8926095f | 11356 | else if (RIDBIT_SETP (RID_REGISTER, specbits)) |
8251199e | 11357 | error ("storage class `register' invalid for function `%s'", name); |
8d08fdba MS |
11358 | |
11359 | /* Function declaration not at top level. | |
11360 | Storage classes other than `extern' are not allowed | |
11361 | and `extern' makes no difference. */ | |
a9aedbc2 | 11362 | if (! toplevel_bindings_p () |
8926095f MS |
11363 | && (RIDBIT_SETP (RID_STATIC, specbits) |
11364 | || RIDBIT_SETP (RID_INLINE, specbits)) | |
8d08fdba | 11365 | && pedantic) |
8926095f MS |
11366 | { |
11367 | if (RIDBIT_SETP (RID_STATIC, specbits)) | |
8251199e | 11368 | pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name); |
8926095f | 11369 | else |
8251199e | 11370 | pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name); |
8926095f | 11371 | } |
68642fb6 | 11372 | |
8d08fdba MS |
11373 | if (ctype == NULL_TREE) |
11374 | { | |
11375 | if (virtualp) | |
11376 | { | |
8251199e | 11377 | error ("virtual non-class function `%s'", name); |
8d08fdba MS |
11378 | virtualp = 0; |
11379 | } | |
8d08fdba MS |
11380 | } |
11381 | else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2) | |
14ae7e7d JM |
11382 | type = build_cplus_method_type (ctype, TREE_TYPE (type), |
11383 | TYPE_ARG_TYPES (type)); | |
8d08fdba | 11384 | |
eb66be0e | 11385 | /* Record presence of `static'. */ |
faae18ab | 11386 | publicp = (ctype != NULL_TREE |
a9aedbc2 | 11387 | || RIDBIT_SETP (RID_EXTERN, specbits) |
eb66be0e | 11388 | || !RIDBIT_SETP (RID_STATIC, specbits)); |
8d08fdba | 11389 | |
386b8a85 | 11390 | decl = grokfndecl (ctype, type, original_name, declarator, |
7a8f9fa9 | 11391 | virtualp, flags, quals, raises, |
75650646 | 11392 | 1, friendp, |
68642fb6 | 11393 | publicp, inlinep, funcdef_flag, |
2c73f9f5 | 11394 | template_count, in_namespace); |
f0e01782 MS |
11395 | if (decl == NULL_TREE) |
11396 | return NULL_TREE; | |
8d08fdba | 11397 | |
8d08fdba MS |
11398 | if (staticp == 1) |
11399 | { | |
11400 | int illegal_static = 0; | |
11401 | ||
11402 | /* Don't allow a static member function in a class, and forbid | |
11403 | declaring main to be static. */ | |
11404 | if (TREE_CODE (type) == METHOD_TYPE) | |
11405 | { | |
8251199e | 11406 | cp_pedwarn ("cannot declare member function `%D' to have static linkage", decl); |
8d08fdba MS |
11407 | illegal_static = 1; |
11408 | } | |
8d08fdba MS |
11409 | else if (current_function_decl) |
11410 | { | |
11411 | /* FIXME need arm citation */ | |
8251199e | 11412 | error ("cannot declare static function inside another function"); |
8d08fdba MS |
11413 | illegal_static = 1; |
11414 | } | |
11415 | ||
11416 | if (illegal_static) | |
11417 | { | |
11418 | staticp = 0; | |
11419 | RIDBIT_RESET (RID_STATIC, specbits); | |
11420 | } | |
11421 | } | |
8d08fdba MS |
11422 | } |
11423 | else | |
11424 | { | |
11425 | /* It's a variable. */ | |
11426 | ||
11427 | /* An uninitialized decl with `extern' is a reference. */ | |
68642fb6 UD |
11428 | decl = grokvardecl (type, declarator, &specbits, |
11429 | initialized, | |
11430 | (type_quals & TYPE_QUAL_CONST) != 0, | |
950ad3c3 | 11431 | in_namespace); |
8d08fdba MS |
11432 | bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE, |
11433 | inlinep, friendp, raises != NULL_TREE); | |
11434 | ||
11435 | if (ctype) | |
11436 | { | |
f0e01782 | 11437 | DECL_CONTEXT (decl) = ctype; |
8d08fdba MS |
11438 | if (staticp == 1) |
11439 | { | |
8251199e | 11440 | cp_pedwarn ("static member `%D' re-declared as static", decl); |
8d08fdba MS |
11441 | staticp = 0; |
11442 | RIDBIT_RESET (RID_STATIC, specbits); | |
11443 | } | |
b7484fbe MS |
11444 | if (RIDBIT_SETP (RID_REGISTER, specbits) && TREE_STATIC (decl)) |
11445 | { | |
8251199e | 11446 | cp_error ("static member `%D' declared `register'", decl); |
b7484fbe MS |
11447 | RIDBIT_RESET (RID_REGISTER, specbits); |
11448 | } | |
f30432d7 | 11449 | if (RIDBIT_SETP (RID_EXTERN, specbits) && pedantic) |
8d08fdba | 11450 | { |
8251199e | 11451 | cp_pedwarn ("cannot explicitly declare member `%#D' to have extern linkage", |
f30432d7 | 11452 | decl); |
8d08fdba MS |
11453 | RIDBIT_RESET (RID_EXTERN, specbits); |
11454 | } | |
11455 | } | |
11456 | } | |
11457 | ||
34fc957d | 11458 | my_friendly_assert (!RIDBIT_SETP (RID_MUTABLE, specbits), 19990927); |
8d08fdba MS |
11459 | |
11460 | /* Record `register' declaration for warnings on & | |
11461 | and in case doing stupid register allocation. */ | |
11462 | ||
11463 | if (RIDBIT_SETP (RID_REGISTER, specbits)) | |
11464 | DECL_REGISTER (decl) = 1; | |
11465 | ||
8926095f MS |
11466 | if (RIDBIT_SETP (RID_EXTERN, specbits)) |
11467 | DECL_THIS_EXTERN (decl) = 1; | |
11468 | ||
faae18ab MS |
11469 | if (RIDBIT_SETP (RID_STATIC, specbits)) |
11470 | DECL_THIS_STATIC (decl) = 1; | |
11471 | ||
adecb3f4 MM |
11472 | /* Record constancy and volatility. There's no need to do this |
11473 | when processing a template; we'll do this for the instantiated | |
11474 | declaration based on the type of DECL. */ | |
11475 | if (!processing_template_decl) | |
11476 | c_apply_type_quals_to_decl (type_quals, decl); | |
8d08fdba MS |
11477 | |
11478 | return decl; | |
11479 | } | |
11480 | } | |
11481 | \f | |
11482 | /* Tell if a parmlist/exprlist looks like an exprlist or a parmlist. | |
11483 | An empty exprlist is a parmlist. An exprlist which | |
11484 | contains only identifiers at the global level | |
11485 | is a parmlist. Otherwise, it is an exprlist. */ | |
e92cc029 | 11486 | |
8d08fdba MS |
11487 | int |
11488 | parmlist_is_exprlist (exprs) | |
11489 | tree exprs; | |
11490 | { | |
11491 | if (exprs == NULL_TREE || TREE_PARMLIST (exprs)) | |
11492 | return 0; | |
11493 | ||
a9aedbc2 | 11494 | if (toplevel_bindings_p ()) |
8d08fdba MS |
11495 | { |
11496 | /* At the global level, if these are all identifiers, | |
11497 | then it is a parmlist. */ | |
11498 | while (exprs) | |
11499 | { | |
11500 | if (TREE_CODE (TREE_VALUE (exprs)) != IDENTIFIER_NODE) | |
11501 | return 1; | |
11502 | exprs = TREE_CHAIN (exprs); | |
11503 | } | |
11504 | return 0; | |
11505 | } | |
11506 | return 1; | |
11507 | } | |
11508 | ||
f181d4ae MM |
11509 | /* Subroutine of start_function. Ensure that each of the parameter |
11510 | types (as listed in PARMS) is complete, as is required for a | |
11511 | function definition. */ | |
e92cc029 | 11512 | |
8d08fdba MS |
11513 | static void |
11514 | require_complete_types_for_parms (parms) | |
11515 | tree parms; | |
11516 | { | |
07c88314 | 11517 | for (; parms; parms = TREE_CHAIN (parms)) |
8d08fdba MS |
11518 | { |
11519 | tree type = TREE_TYPE (parms); | |
07c88314 MM |
11520 | |
11521 | /* Try to complete the TYPE. */ | |
11522 | type = complete_type (type); | |
11523 | ||
11524 | if (type == error_mark_node) | |
11525 | continue; | |
11526 | ||
11527 | if (TYPE_SIZE (type) == NULL_TREE) | |
8d08fdba MS |
11528 | { |
11529 | if (DECL_NAME (parms)) | |
8251199e | 11530 | error ("parameter `%s' has incomplete type", |
8d08fdba MS |
11531 | IDENTIFIER_POINTER (DECL_NAME (parms))); |
11532 | else | |
8251199e | 11533 | error ("parameter has incomplete type"); |
8d08fdba MS |
11534 | TREE_TYPE (parms) = error_mark_node; |
11535 | } | |
42f989ff JM |
11536 | else |
11537 | layout_decl (parms, 0); | |
8d08fdba MS |
11538 | } |
11539 | } | |
11540 | ||
46e8c075 | 11541 | /* Returns non-zero if T is a local variable. */ |
297e73d8 | 11542 | |
46e8c075 MM |
11543 | int |
11544 | local_variable_p (t) | |
11545 | tree t; | |
297e73d8 | 11546 | { |
68642fb6 | 11547 | if ((TREE_CODE (t) == VAR_DECL |
297e73d8 MM |
11548 | /* A VAR_DECL with a context that is a _TYPE is a static data |
11549 | member. */ | |
11550 | && !TYPE_P (CP_DECL_CONTEXT (t)) | |
11551 | /* Any other non-local variable must be at namespace scope. */ | |
46e8c075 | 11552 | && !DECL_NAMESPACE_SCOPE_P (t)) |
297e73d8 | 11553 | || (TREE_CODE (t) == PARM_DECL)) |
46e8c075 | 11554 | return 1; |
297e73d8 | 11555 | |
46e8c075 MM |
11556 | return 0; |
11557 | } | |
11558 | ||
11559 | /* Returns non-zero if T is an automatic local variable or a label. | |
11560 | (These are the declarations that need to be remapped when the code | |
11561 | containing them is duplicated.) */ | |
11562 | ||
11563 | int | |
11564 | nonstatic_local_decl_p (t) | |
11565 | tree t; | |
11566 | { | |
11567 | return ((local_variable_p (t) && !TREE_STATIC (t)) | |
11568 | || TREE_CODE (t) == LABEL_DECL | |
11569 | || TREE_CODE (t) == RESULT_DECL); | |
11570 | } | |
11571 | ||
11572 | /* Like local_variable_p, but suitable for use as a tree-walking | |
11573 | function. */ | |
11574 | ||
11575 | static tree | |
11576 | local_variable_p_walkfn (tp, walk_subtrees, data) | |
11577 | tree *tp; | |
11578 | int *walk_subtrees ATTRIBUTE_UNUSED; | |
11579 | void *data ATTRIBUTE_UNUSED; | |
11580 | { | |
68642fb6 | 11581 | return ((local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp)) |
46e8c075 | 11582 | ? *tp : NULL_TREE); |
297e73d8 MM |
11583 | } |
11584 | ||
11585 | /* Check that ARG, which is a default-argument expression for a | |
11586 | parameter DECL, is legal. Returns ARG, or ERROR_MARK_NODE, if | |
11587 | something goes wrong. DECL may also be a _TYPE node, rather than a | |
11588 | DECL, if there is no DECL available. */ | |
11589 | ||
11590 | tree | |
11591 | check_default_argument (decl, arg) | |
11592 | tree decl; | |
11593 | tree arg; | |
11594 | { | |
11595 | tree var; | |
11596 | tree decl_type; | |
11597 | ||
11598 | if (TREE_CODE (arg) == DEFAULT_ARG) | |
11599 | /* We get a DEFAULT_ARG when looking at an in-class declaration | |
11600 | with a default argument. Ignore the argument for now; we'll | |
11601 | deal with it after the class is complete. */ | |
11602 | return arg; | |
11603 | ||
11604 | if (processing_template_decl || uses_template_parms (arg)) | |
11605 | /* We don't do anything checking until instantiation-time. Note | |
11606 | that there may be uninstantiated arguments even for an | |
11607 | instantiated function, since default arguments are not | |
11608 | instantiated until they are needed. */ | |
11609 | return arg; | |
11610 | ||
11611 | if (TYPE_P (decl)) | |
11612 | { | |
11613 | decl_type = decl; | |
11614 | decl = NULL_TREE; | |
11615 | } | |
11616 | else | |
11617 | decl_type = TREE_TYPE (decl); | |
11618 | ||
68642fb6 | 11619 | if (arg == error_mark_node |
297e73d8 MM |
11620 | || decl == error_mark_node |
11621 | || TREE_TYPE (arg) == error_mark_node | |
11622 | || decl_type == error_mark_node) | |
11623 | /* Something already went wrong. There's no need to check | |
11624 | further. */ | |
11625 | return error_mark_node; | |
11626 | ||
11627 | /* [dcl.fct.default] | |
68642fb6 | 11628 | |
297e73d8 MM |
11629 | A default argument expression is implicitly converted to the |
11630 | parameter type. */ | |
11631 | if (!TREE_TYPE (arg) | |
11632 | || !can_convert_arg (decl_type, TREE_TYPE (arg), arg)) | |
11633 | { | |
11634 | if (decl) | |
68642fb6 | 11635 | cp_error ("default argument for `%#D' has type `%T'", |
297e73d8 MM |
11636 | decl, TREE_TYPE (arg)); |
11637 | else | |
8614a1d6 | 11638 | cp_error ("default argument for parameter of type `%T' has type `%T'", |
297e73d8 MM |
11639 | decl_type, TREE_TYPE (arg)); |
11640 | ||
11641 | return error_mark_node; | |
11642 | } | |
11643 | ||
11644 | /* [dcl.fct.default] | |
11645 | ||
11646 | Local variables shall not be used in default argument | |
68642fb6 | 11647 | expressions. |
297e73d8 MM |
11648 | |
11649 | The keyword `this' shall not be used in a default argument of a | |
11650 | member function. */ | |
46e8c075 | 11651 | var = walk_tree (&arg, local_variable_p_walkfn, NULL); |
297e73d8 MM |
11652 | if (var) |
11653 | { | |
11654 | cp_error ("default argument `%E' uses local variable `%D'", | |
11655 | arg, var); | |
11656 | return error_mark_node; | |
11657 | } | |
11658 | ||
11659 | /* All is well. */ | |
11660 | return arg; | |
11661 | } | |
11662 | ||
8d08fdba MS |
11663 | /* Decode the list of parameter types for a function type. |
11664 | Given the list of things declared inside the parens, | |
11665 | return a list of types. | |
11666 | ||
11667 | The list we receive can have three kinds of elements: | |
11668 | an IDENTIFIER_NODE for names given without types, | |
11669 | a TREE_LIST node for arguments given as typespecs or names with typespecs, | |
11670 | or void_type_node, to mark the end of an argument list | |
11671 | when additional arguments are not permitted (... was not used). | |
11672 | ||
11673 | FUNCDEF_FLAG is nonzero for a function definition, 0 for | |
11674 | a mere declaration. A nonempty identifier-list gets an error message | |
11675 | when FUNCDEF_FLAG is zero. | |
11676 | If FUNCDEF_FLAG is 1, then parameter types must be complete. | |
11677 | If FUNCDEF_FLAG is -1, then parameter types may be incomplete. | |
11678 | ||
11679 | If all elements of the input list contain types, | |
11680 | we return a list of the types. | |
11681 | If all elements contain no type (except perhaps a void_type_node | |
11682 | at the end), we return a null list. | |
11683 | If some have types and some do not, it is an error, and we | |
11684 | return a null list. | |
11685 | ||
11686 | Also set last_function_parms to either | |
11687 | a list of names (IDENTIFIER_NODEs) or a chain of PARM_DECLs. | |
11688 | A list of names is converted to a chain of PARM_DECLs | |
11689 | by store_parm_decls so that ultimately it is always a chain of decls. | |
11690 | ||
11691 | Note that in C++, parameters can take default values. These default | |
11692 | values are in the TREE_PURPOSE field of the TREE_LIST. It is | |
11693 | an error to specify default values which are followed by parameters | |
11694 | that have no default values, or an ELLIPSES. For simplicities sake, | |
11695 | only parameters which are specified with their types can take on | |
11696 | default values. */ | |
11697 | ||
11698 | static tree | |
11699 | grokparms (first_parm, funcdef_flag) | |
11700 | tree first_parm; | |
11701 | int funcdef_flag; | |
11702 | { | |
11703 | tree result = NULL_TREE; | |
11704 | tree decls = NULL_TREE; | |
11705 | ||
11706 | if (first_parm != NULL_TREE | |
11707 | && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE) | |
11708 | { | |
11709 | if (! funcdef_flag) | |
8251199e | 11710 | pedwarn ("parameter names (without types) in function declaration"); |
8d08fdba MS |
11711 | last_function_parms = first_parm; |
11712 | return NULL_TREE; | |
11713 | } | |
11714 | else if (first_parm != NULL_TREE | |
11715 | && TREE_CODE (TREE_VALUE (first_parm)) != TREE_LIST | |
a1774733 | 11716 | && TREE_CODE (TREE_VALUE (first_parm)) != VOID_TYPE) |
8d08fdba MS |
11717 | my_friendly_abort (145); |
11718 | else | |
11719 | { | |
11720 | /* Types were specified. This is a list of declarators | |
11721 | each represented as a TREE_LIST node. */ | |
11722 | register tree parm, chain; | |
5566b478 | 11723 | int any_init = 0, any_error = 0; |
8d08fdba MS |
11724 | |
11725 | if (first_parm != NULL_TREE) | |
11726 | { | |
11727 | tree last_result = NULL_TREE; | |
11728 | tree last_decl = NULL_TREE; | |
11729 | ||
11730 | for (parm = first_parm; parm != NULL_TREE; parm = chain) | |
11731 | { | |
a703fb38 | 11732 | tree type = NULL_TREE, list_node = parm; |
8d08fdba MS |
11733 | register tree decl = TREE_VALUE (parm); |
11734 | tree init = TREE_PURPOSE (parm); | |
11735 | ||
11736 | chain = TREE_CHAIN (parm); | |
11737 | /* @@ weak defense against parse errors. */ | |
68642fb6 | 11738 | if (TREE_CODE (decl) != VOID_TYPE |
a1774733 | 11739 | && TREE_CODE (decl) != TREE_LIST) |
8d08fdba MS |
11740 | { |
11741 | /* Give various messages as the need arises. */ | |
11742 | if (TREE_CODE (decl) == STRING_CST) | |
8251199e | 11743 | cp_error ("invalid string constant `%E'", decl); |
8d08fdba | 11744 | else if (TREE_CODE (decl) == INTEGER_CST) |
8251199e | 11745 | error ("invalid integer constant in parameter list, did you forget to give parameter name?"); |
8d08fdba MS |
11746 | continue; |
11747 | } | |
11748 | ||
a1774733 | 11749 | if (TREE_CODE (decl) != VOID_TYPE) |
8d08fdba | 11750 | { |
8d08fdba MS |
11751 | decl = grokdeclarator (TREE_VALUE (decl), |
11752 | TREE_PURPOSE (decl), | |
f30432d7 | 11753 | PARM, init != NULL_TREE, |
c11b6f21 | 11754 | NULL_TREE); |
3bfdc719 | 11755 | if (! decl || TREE_TYPE (decl) == error_mark_node) |
8d08fdba | 11756 | continue; |
01240200 MM |
11757 | |
11758 | /* Top-level qualifiers on the parameters are | |
11759 | ignored for function types. */ | |
11760 | type = TYPE_MAIN_VARIANT (TREE_TYPE (decl)); | |
11761 | ||
a1774733 | 11762 | if (TREE_CODE (type) == VOID_TYPE) |
8d08fdba MS |
11763 | decl = void_type_node; |
11764 | else if (TREE_CODE (type) == METHOD_TYPE) | |
11765 | { | |
11766 | if (DECL_NAME (decl)) | |
fc378698 | 11767 | /* Cannot use the decl here because |
8d08fdba | 11768 | we don't have DECL_CONTEXT set up yet. */ |
8251199e | 11769 | cp_error ("parameter `%D' invalidly declared method type", |
fc378698 | 11770 | DECL_NAME (decl)); |
8d08fdba | 11771 | else |
8251199e | 11772 | error ("parameter invalidly declared method type"); |
8d08fdba MS |
11773 | type = build_pointer_type (type); |
11774 | TREE_TYPE (decl) = type; | |
11775 | } | |
11776 | else if (TREE_CODE (type) == OFFSET_TYPE) | |
11777 | { | |
11778 | if (DECL_NAME (decl)) | |
8251199e | 11779 | cp_error ("parameter `%D' invalidly declared offset type", |
fc378698 | 11780 | DECL_NAME (decl)); |
8d08fdba | 11781 | else |
8251199e | 11782 | error ("parameter invalidly declared offset type"); |
8d08fdba MS |
11783 | type = build_pointer_type (type); |
11784 | TREE_TYPE (decl) = type; | |
11785 | } | |
a7a64a77 MM |
11786 | else if (abstract_virtuals_error (decl, type)) |
11787 | any_error = 1; /* Seems like a good idea. */ | |
482b883f JM |
11788 | else if (POINTER_TYPE_P (type)) |
11789 | { | |
11790 | tree t = type; | |
11791 | while (POINTER_TYPE_P (t) | |
11792 | || (TREE_CODE (t) == ARRAY_TYPE | |
11793 | && TYPE_DOMAIN (t) != NULL_TREE)) | |
11794 | t = TREE_TYPE (t); | |
11795 | if (TREE_CODE (t) == ARRAY_TYPE) | |
57af8358 MM |
11796 | cp_error ("parameter type `%T' includes %s to array of unknown bound", |
11797 | type, | |
11798 | TYPE_PTR_P (type) ? "pointer" : "reference"); | |
482b883f | 11799 | } |
8d08fdba MS |
11800 | } |
11801 | ||
a1774733 | 11802 | if (TREE_CODE (decl) == VOID_TYPE) |
8d08fdba MS |
11803 | { |
11804 | if (result == NULL_TREE) | |
11805 | { | |
11806 | result = void_list_node; | |
11807 | last_result = result; | |
11808 | } | |
11809 | else | |
11810 | { | |
11811 | TREE_CHAIN (last_result) = void_list_node; | |
11812 | last_result = void_list_node; | |
11813 | } | |
8d08fdba MS |
11814 | if (chain |
11815 | && (chain != void_list_node || TREE_CHAIN (chain))) | |
8251199e | 11816 | error ("`void' in parameter list must be entire list"); |
8d08fdba MS |
11817 | break; |
11818 | } | |
11819 | ||
11820 | /* Since there is a prototype, args are passed in their own types. */ | |
11821 | DECL_ARG_TYPE (decl) = TREE_TYPE (decl); | |
fa56377d JJ |
11822 | if (PROMOTE_PROTOTYPES |
11823 | && (TREE_CODE (type) == INTEGER_TYPE | |
11824 | || TREE_CODE (type) == ENUMERAL_TYPE) | |
8d08fdba MS |
11825 | && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) |
11826 | DECL_ARG_TYPE (decl) = integer_type_node; | |
297e73d8 | 11827 | if (!any_error && init) |
8d08fdba | 11828 | { |
297e73d8 MM |
11829 | any_init++; |
11830 | init = check_default_argument (decl, init); | |
8d08fdba MS |
11831 | } |
11832 | else | |
11833 | init = NULL_TREE; | |
11834 | ||
11835 | if (decls == NULL_TREE) | |
11836 | { | |
11837 | decls = decl; | |
11838 | last_decl = decls; | |
11839 | } | |
11840 | else | |
11841 | { | |
11842 | TREE_CHAIN (last_decl) = decl; | |
11843 | last_decl = decl; | |
11844 | } | |
5868eb4e | 11845 | list_node = tree_cons (init, type, NULL_TREE); |
8d08fdba MS |
11846 | if (result == NULL_TREE) |
11847 | { | |
11848 | result = list_node; | |
11849 | last_result = result; | |
11850 | } | |
11851 | else | |
11852 | { | |
11853 | TREE_CHAIN (last_result) = list_node; | |
11854 | last_result = list_node; | |
11855 | } | |
11856 | } | |
11857 | if (last_result) | |
11858 | TREE_CHAIN (last_result) = NULL_TREE; | |
11859 | /* If there are no parameters, and the function does not end | |
11860 | with `...', then last_decl will be NULL_TREE. */ | |
11861 | if (last_decl != NULL_TREE) | |
11862 | TREE_CHAIN (last_decl) = NULL_TREE; | |
11863 | } | |
11864 | } | |
11865 | ||
11866 | last_function_parms = decls; | |
11867 | ||
8d08fdba MS |
11868 | return result; |
11869 | } | |
42976354 BK |
11870 | |
11871 | /* Called from the parser to update an element of TYPE_ARG_TYPES for some | |
11872 | FUNCTION_TYPE with the newly parsed version of its default argument, which | |
11873 | was previously digested as text. See snarf_defarg et al in lex.c. */ | |
11874 | ||
11875 | void | |
11876 | replace_defarg (arg, init) | |
11877 | tree arg, init; | |
11878 | { | |
aa5f3bad | 11879 | if (! processing_template_decl |
42976354 | 11880 | && ! can_convert_arg (TREE_VALUE (arg), TREE_TYPE (init), init)) |
8251199e | 11881 | cp_pedwarn ("invalid type `%T' for default argument to `%T'", |
42976354 BK |
11882 | TREE_TYPE (init), TREE_VALUE (arg)); |
11883 | TREE_PURPOSE (arg) = init; | |
11884 | } | |
8d08fdba | 11885 | \f |
4f1c5b7d MM |
11886 | /* D is a constructor or overloaded `operator='. Returns non-zero if |
11887 | D's arguments allow it to be a copy constructor, or copy assignment | |
11888 | operator. */ | |
11889 | ||
c11b6f21 MS |
11890 | int |
11891 | copy_args_p (d) | |
11892 | tree d; | |
11893 | { | |
4f1c5b7d MM |
11894 | tree t; |
11895 | ||
11896 | if (!DECL_FUNCTION_MEMBER_P (d)) | |
11897 | return 0; | |
11898 | ||
11899 | t = FUNCTION_ARG_CHAIN (d); | |
67437d5b JM |
11900 | if (DECL_CONSTRUCTOR_P (d) |
11901 | && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (d))) | |
11902 | t = TREE_CHAIN (t); | |
c11b6f21 MS |
11903 | if (t && TREE_CODE (TREE_VALUE (t)) == REFERENCE_TYPE |
11904 | && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (t))) | |
4f1c5b7d | 11905 | == DECL_CONTEXT (d)) |
c11b6f21 MS |
11906 | && (TREE_CHAIN (t) == NULL_TREE |
11907 | || TREE_CHAIN (t) == void_list_node | |
11908 | || TREE_PURPOSE (TREE_CHAIN (t)))) | |
11909 | return 1; | |
11910 | return 0; | |
11911 | } | |
11912 | ||
8d08fdba MS |
11913 | /* These memoizing functions keep track of special properties which |
11914 | a class may have. `grok_ctor_properties' notices whether a class | |
11915 | has a constructor of the form X(X&), and also complains | |
11916 | if the class has a constructor of the form X(X). | |
11917 | `grok_op_properties' takes notice of the various forms of | |
11918 | operator= which are defined, as well as what sorts of type conversion | |
11919 | may apply. Both functions take a FUNCTION_DECL as an argument. */ | |
e92cc029 | 11920 | |
a0a33927 | 11921 | int |
8d08fdba MS |
11922 | grok_ctor_properties (ctype, decl) |
11923 | tree ctype, decl; | |
11924 | { | |
11925 | tree parmtypes = FUNCTION_ARG_CHAIN (decl); | |
11926 | tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node; | |
11927 | ||
11928 | /* When a type has virtual baseclasses, a magical first int argument is | |
11929 | added to any ctor so we can tell if the class has been initialized | |
11930 | yet. This could screw things up in this function, so we deliberately | |
11931 | ignore the leading int if we're in that situation. */ | |
711734a9 | 11932 | if (TYPE_USES_VIRTUAL_BASECLASSES (ctype)) |
8d08fdba | 11933 | { |
711734a9 JM |
11934 | my_friendly_assert (parmtypes |
11935 | && TREE_VALUE (parmtypes) == integer_type_node, | |
11936 | 980529); | |
8d08fdba MS |
11937 | parmtypes = TREE_CHAIN (parmtypes); |
11938 | parmtype = TREE_VALUE (parmtypes); | |
11939 | } | |
11940 | ||
f181d4ae MM |
11941 | /* [class.copy] |
11942 | ||
11943 | A non-template constructor for class X is a copy constructor if | |
11944 | its first parameter is of type X&, const X&, volatile X& or const | |
11945 | volatile X&, and either there are no other parameters or else all | |
11946 | other parameters have default arguments. */ | |
8d08fdba | 11947 | if (TREE_CODE (parmtype) == REFERENCE_TYPE |
0b41abe6 JM |
11948 | && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == ctype |
11949 | && (TREE_CHAIN (parmtypes) == NULL_TREE | |
8d08fdba | 11950 | || TREE_CHAIN (parmtypes) == void_list_node |
f181d4ae MM |
11951 | || TREE_PURPOSE (TREE_CHAIN (parmtypes))) |
11952 | && !(DECL_TEMPLATE_INSTANTIATION (decl) | |
11953 | && is_member_template (DECL_TI_TEMPLATE (decl)))) | |
0b41abe6 JM |
11954 | { |
11955 | TYPE_HAS_INIT_REF (ctype) = 1; | |
91063b51 | 11956 | if (CP_TYPE_CONST_P (TREE_TYPE (parmtype))) |
0b41abe6 | 11957 | TYPE_HAS_CONST_INIT_REF (ctype) = 1; |
8d08fdba | 11958 | } |
f181d4ae MM |
11959 | /* [class.copy] |
11960 | ||
11961 | A declaration of a constructor for a class X is ill-formed if its | |
11962 | first parameter is of type (optionally cv-qualified) X and either | |
11963 | there are no other parameters or else all other parameters have | |
68642fb6 | 11964 | default arguments. |
f181d4ae MM |
11965 | |
11966 | We *don't* complain about member template instantiations that | |
11967 | have this form, though; they can occur as we try to decide what | |
11968 | constructor to use during overload resolution. Since overload | |
11969 | resolution will never prefer such a constructor to the | |
11970 | non-template copy constructor (which is either explicitly or | |
11971 | implicitly defined), there's no need to worry about their | |
11972 | existence. Theoretically, they should never even be | |
11973 | instantiated, but that's hard to forestall. */ | |
0b41abe6 | 11974 | else if (TYPE_MAIN_VARIANT (parmtype) == ctype |
59e76fc6 JM |
11975 | && (TREE_CHAIN (parmtypes) == NULL_TREE |
11976 | || TREE_CHAIN (parmtypes) == void_list_node | |
f181d4ae MM |
11977 | || TREE_PURPOSE (TREE_CHAIN (parmtypes))) |
11978 | && !(DECL_TEMPLATE_INSTANTIATION (decl) | |
11979 | && is_member_template (DECL_TI_TEMPLATE (decl)))) | |
8d08fdba | 11980 | { |
8251199e | 11981 | cp_error ("invalid constructor; you probably meant `%T (const %T&)'", |
0b41abe6 JM |
11982 | ctype, ctype); |
11983 | SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype); | |
11984 | return 0; | |
8d08fdba MS |
11985 | } |
11986 | else if (TREE_CODE (parmtype) == VOID_TYPE | |
11987 | || TREE_PURPOSE (parmtypes) != NULL_TREE) | |
11988 | TYPE_HAS_DEFAULT_CONSTRUCTOR (ctype) = 1; | |
a0a33927 MS |
11989 | |
11990 | return 1; | |
8d08fdba MS |
11991 | } |
11992 | ||
11993 | /* An operator with this name can be either unary or binary. */ | |
e92cc029 | 11994 | |
a28e3c7f MS |
11995 | static int |
11996 | ambi_op_p (name) | |
8d08fdba MS |
11997 | tree name; |
11998 | { | |
11999 | return (name == ansi_opname [(int) INDIRECT_REF] | |
12000 | || name == ansi_opname [(int) ADDR_EXPR] | |
12001 | || name == ansi_opname [(int) NEGATE_EXPR] | |
12002 | || name == ansi_opname[(int) POSTINCREMENT_EXPR] | |
12003 | || name == ansi_opname[(int) POSTDECREMENT_EXPR] | |
12004 | || name == ansi_opname [(int) CONVERT_EXPR]); | |
12005 | } | |
12006 | ||
12007 | /* An operator with this name can only be unary. */ | |
e92cc029 | 12008 | |
a28e3c7f MS |
12009 | static int |
12010 | unary_op_p (name) | |
8d08fdba MS |
12011 | tree name; |
12012 | { | |
12013 | return (name == ansi_opname [(int) TRUTH_NOT_EXPR] | |
12014 | || name == ansi_opname [(int) BIT_NOT_EXPR] | |
12015 | || name == ansi_opname [(int) COMPONENT_REF] | |
4c571114 | 12016 | || IDENTIFIER_TYPENAME_P (name)); |
8d08fdba MS |
12017 | } |
12018 | ||
12019 | /* Do a little sanity-checking on how they declared their operator. */ | |
e92cc029 | 12020 | |
5566b478 | 12021 | void |
8d08fdba MS |
12022 | grok_op_properties (decl, virtualp, friendp) |
12023 | tree decl; | |
12024 | int virtualp, friendp; | |
12025 | { | |
12026 | tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl)); | |
12027 | int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE); | |
12028 | tree name = DECL_NAME (decl); | |
8d08fdba | 12029 | |
a28e3c7f MS |
12030 | if (current_class_type == NULL_TREE) |
12031 | friendp = 1; | |
8d08fdba | 12032 | |
a28e3c7f MS |
12033 | if (! friendp) |
12034 | { | |
f181d4ae MM |
12035 | /* [class.copy] |
12036 | ||
12037 | A user-declared copy assignment operator X::operator= is a | |
12038 | non-static non-template member function of class X with | |
12039 | exactly one parameter of type X, X&, const X&, volatile X& or | |
12040 | const volatile X&. */ | |
12041 | if (name == ansi_opname[(int) MODIFY_EXPR] | |
12042 | && !(DECL_TEMPLATE_INSTANTIATION (decl) | |
12043 | && is_member_template (DECL_TI_TEMPLATE (decl)))) | |
6a629cac | 12044 | ; |
a28e3c7f MS |
12045 | else if (name == ansi_opname[(int) CALL_EXPR]) |
12046 | TYPE_OVERLOADS_CALL_EXPR (current_class_type) = 1; | |
12047 | else if (name == ansi_opname[(int) ARRAY_REF]) | |
12048 | TYPE_OVERLOADS_ARRAY_REF (current_class_type) = 1; | |
12049 | else if (name == ansi_opname[(int) COMPONENT_REF] | |
12050 | || name == ansi_opname[(int) MEMBER_REF]) | |
12051 | TYPE_OVERLOADS_ARROW (current_class_type) = 1; | |
12052 | else if (name == ansi_opname[(int) NEW_EXPR]) | |
834c6dff | 12053 | TYPE_HAS_NEW_OPERATOR (current_class_type) = 1; |
a28e3c7f MS |
12054 | else if (name == ansi_opname[(int) DELETE_EXPR]) |
12055 | TYPE_GETS_DELETE (current_class_type) |= 1; | |
12056 | else if (name == ansi_opname[(int) VEC_NEW_EXPR]) | |
834c6dff | 12057 | TYPE_HAS_ARRAY_NEW_OPERATOR (current_class_type) = 1; |
a28e3c7f MS |
12058 | else if (name == ansi_opname[(int) VEC_DELETE_EXPR]) |
12059 | TYPE_GETS_DELETE (current_class_type) |= 2; | |
12060 | } | |
12061 | ||
12062 | if (name == ansi_opname[(int) NEW_EXPR] | |
12063 | || name == ansi_opname[(int) VEC_NEW_EXPR]) | |
8d08fdba | 12064 | { |
8d08fdba MS |
12065 | /* When the compiler encounters the definition of A::operator new, it |
12066 | doesn't look at the class declaration to find out if it's static. */ | |
a28e3c7f | 12067 | if (methodp) |
3afb32a4 | 12068 | revert_static_member_fn (decl); |
68642fb6 | 12069 | |
8d08fdba MS |
12070 | /* Take care of function decl if we had syntax errors. */ |
12071 | if (argtypes == NULL_TREE) | |
beb53fb8 JM |
12072 | TREE_TYPE (decl) |
12073 | = build_function_type (ptr_type_node, | |
12074 | hash_tree_chain (integer_type_node, | |
12075 | void_list_node)); | |
8d08fdba | 12076 | else |
a28e3c7f | 12077 | TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl)); |
8d08fdba | 12078 | } |
a28e3c7f MS |
12079 | else if (name == ansi_opname[(int) DELETE_EXPR] |
12080 | || name == ansi_opname[(int) VEC_DELETE_EXPR]) | |
8d08fdba | 12081 | { |
a28e3c7f | 12082 | if (methodp) |
3afb32a4 | 12083 | revert_static_member_fn (decl); |
68642fb6 | 12084 | |
8d08fdba | 12085 | if (argtypes == NULL_TREE) |
beb53fb8 JM |
12086 | TREE_TYPE (decl) |
12087 | = build_function_type (void_type_node, | |
12088 | hash_tree_chain (ptr_type_node, | |
12089 | void_list_node)); | |
8d08fdba | 12090 | else |
834c6dff | 12091 | TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl)); |
8d08fdba | 12092 | } |
8d08fdba MS |
12093 | else |
12094 | { | |
12095 | /* An operator function must either be a non-static member function | |
12096 | or have at least one parameter of a class, a reference to a class, | |
12097 | an enumeration, or a reference to an enumeration. 13.4.0.6 */ | |
700f8a87 | 12098 | if (! methodp || DECL_STATIC_FUNCTION_P (decl)) |
8d08fdba | 12099 | { |
4c571114 | 12100 | if (IDENTIFIER_TYPENAME_P (name) |
8d08fdba MS |
12101 | || name == ansi_opname[(int) CALL_EXPR] |
12102 | || name == ansi_opname[(int) MODIFY_EXPR] | |
12103 | || name == ansi_opname[(int) COMPONENT_REF] | |
12104 | || name == ansi_opname[(int) ARRAY_REF]) | |
8251199e | 12105 | cp_error ("`%D' must be a nonstatic member function", decl); |
8d08fdba MS |
12106 | else |
12107 | { | |
12108 | tree p = argtypes; | |
12109 | ||
700f8a87 | 12110 | if (DECL_STATIC_FUNCTION_P (decl)) |
8251199e | 12111 | cp_error ("`%D' must be either a non-static member function or a non-member function", decl); |
700f8a87 | 12112 | |
8d08fdba | 12113 | if (p) |
a1774733 | 12114 | for (; TREE_CODE (TREE_VALUE (p)) != VOID_TYPE ; p = TREE_CHAIN (p)) |
8d08fdba MS |
12115 | { |
12116 | tree arg = TREE_VALUE (p); | |
12117 | if (TREE_CODE (arg) == REFERENCE_TYPE) | |
12118 | arg = TREE_TYPE (arg); | |
12119 | ||
12120 | /* This lets bad template code slip through. */ | |
12121 | if (IS_AGGR_TYPE (arg) | |
12122 | || TREE_CODE (arg) == ENUMERAL_TYPE | |
73b0fce8 KL |
12123 | || TREE_CODE (arg) == TEMPLATE_TYPE_PARM |
12124 | || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) | |
8d08fdba MS |
12125 | goto foundaggr; |
12126 | } | |
8251199e JM |
12127 | cp_error |
12128 | ("`%D' must have an argument of class or enumerated type", | |
8d08fdba MS |
12129 | decl); |
12130 | foundaggr: | |
12131 | ; | |
12132 | } | |
12133 | } | |
68642fb6 | 12134 | |
277294d7 | 12135 | if (name == ansi_opname[(int) CALL_EXPR]) |
2c73f9f5 | 12136 | return; /* No restrictions on args. */ |
8d08fdba | 12137 | |
9a3b49ac | 12138 | if (IDENTIFIER_TYPENAME_P (name) && ! DECL_TEMPLATE_INFO (decl)) |
a0a33927 MS |
12139 | { |
12140 | tree t = TREE_TYPE (name); | |
17708e90 | 12141 | if (! friendp) |
a0a33927 MS |
12142 | { |
12143 | int ref = (TREE_CODE (t) == REFERENCE_TYPE); | |
d8e178a0 | 12144 | const char *what = 0; |
17708e90 | 12145 | |
a0a33927 MS |
12146 | if (ref) |
12147 | t = TYPE_MAIN_VARIANT (TREE_TYPE (t)); | |
12148 | ||
17708e90 NS |
12149 | if (TREE_CODE (t) == VOID_TYPE) |
12150 | what = "void"; | |
12151 | else if (t == current_class_type) | |
a0a33927 | 12152 | what = "the same type"; |
9a3b49ac | 12153 | /* Don't force t to be complete here. */ |
a0a33927 | 12154 | else if (IS_AGGR_TYPE (t) |
9a3b49ac | 12155 | && TYPE_SIZE (t) |
a0a33927 MS |
12156 | && DERIVED_FROM_P (t, current_class_type)) |
12157 | what = "a base class"; | |
12158 | ||
12159 | if (what) | |
8251199e | 12160 | warning ("conversion to %s%s will never use a type conversion operator", |
a0a33927 MS |
12161 | ref ? "a reference to " : "", what); |
12162 | } | |
12163 | } | |
12164 | ||
8d08fdba MS |
12165 | if (name == ansi_opname[(int) MODIFY_EXPR]) |
12166 | { | |
12167 | tree parmtype; | |
12168 | ||
12169 | if (list_length (argtypes) != 3 && methodp) | |
12170 | { | |
8251199e | 12171 | cp_error ("`%D' must take exactly one argument", decl); |
8d08fdba MS |
12172 | return; |
12173 | } | |
12174 | parmtype = TREE_VALUE (TREE_CHAIN (argtypes)); | |
12175 | ||
f0e01782 | 12176 | if (copy_assignment_arg_p (parmtype, virtualp) |
a28e3c7f | 12177 | && ! friendp) |
8d08fdba MS |
12178 | { |
12179 | TYPE_HAS_ASSIGN_REF (current_class_type) = 1; | |
f0e01782 | 12180 | if (TREE_CODE (parmtype) != REFERENCE_TYPE |
91063b51 | 12181 | || CP_TYPE_CONST_P (TREE_TYPE (parmtype))) |
8d08fdba MS |
12182 | TYPE_HAS_CONST_ASSIGN_REF (current_class_type) = 1; |
12183 | } | |
12184 | } | |
12185 | else if (name == ansi_opname[(int) COND_EXPR]) | |
12186 | { | |
12187 | /* 13.4.0.3 */ | |
cb9a3ff8 | 12188 | cp_error ("ISO C++ prohibits overloading operator ?:"); |
68642fb6 | 12189 | } |
8d08fdba MS |
12190 | else if (ambi_op_p (name)) |
12191 | { | |
12192 | if (list_length (argtypes) == 2) | |
12193 | /* prefix */; | |
12194 | else if (list_length (argtypes) == 3) | |
12195 | { | |
12196 | if ((name == ansi_opname[(int) POSTINCREMENT_EXPR] | |
12197 | || name == ansi_opname[(int) POSTDECREMENT_EXPR]) | |
5156628f | 12198 | && ! processing_template_decl |
007e5fea | 12199 | && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node)) |
8d08fdba MS |
12200 | { |
12201 | if (methodp) | |
8251199e | 12202 | cp_error ("postfix `%D' must take `int' as its argument", |
8d08fdba MS |
12203 | decl); |
12204 | else | |
8251199e JM |
12205 | cp_error |
12206 | ("postfix `%D' must take `int' as its second argument", | |
12207 | decl); | |
8d08fdba MS |
12208 | } |
12209 | } | |
12210 | else | |
12211 | { | |
12212 | if (methodp) | |
8251199e | 12213 | cp_error ("`%D' must take either zero or one argument", decl); |
8d08fdba | 12214 | else |
8251199e | 12215 | cp_error ("`%D' must take either one or two arguments", decl); |
8d08fdba | 12216 | } |
824b9a4c MS |
12217 | |
12218 | /* More Effective C++ rule 6. */ | |
eb448459 | 12219 | if (warn_ecpp |
824b9a4c MS |
12220 | && (name == ansi_opname[(int) POSTINCREMENT_EXPR] |
12221 | || name == ansi_opname[(int) POSTDECREMENT_EXPR])) | |
12222 | { | |
12223 | tree arg = TREE_VALUE (argtypes); | |
12224 | tree ret = TREE_TYPE (TREE_TYPE (decl)); | |
12225 | if (methodp || TREE_CODE (arg) == REFERENCE_TYPE) | |
12226 | arg = TREE_TYPE (arg); | |
12227 | arg = TYPE_MAIN_VARIANT (arg); | |
12228 | if (list_length (argtypes) == 2) | |
12229 | { | |
12230 | if (TREE_CODE (ret) != REFERENCE_TYPE | |
3bfdc719 MM |
12231 | || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), |
12232 | arg)) | |
8251199e | 12233 | cp_warning ("prefix `%D' should return `%T'", decl, |
824b9a4c MS |
12234 | build_reference_type (arg)); |
12235 | } | |
12236 | else | |
12237 | { | |
3bfdc719 | 12238 | if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg)) |
8251199e | 12239 | cp_warning ("postfix `%D' should return `%T'", decl, arg); |
824b9a4c MS |
12240 | } |
12241 | } | |
8d08fdba MS |
12242 | } |
12243 | else if (unary_op_p (name)) | |
12244 | { | |
12245 | if (list_length (argtypes) != 2) | |
12246 | { | |
12247 | if (methodp) | |
8251199e | 12248 | cp_error ("`%D' must take `void'", decl); |
8d08fdba | 12249 | else |
8251199e | 12250 | cp_error ("`%D' must take exactly one argument", decl); |
8d08fdba MS |
12251 | } |
12252 | } | |
12253 | else /* if (binary_op_p (name)) */ | |
12254 | { | |
12255 | if (list_length (argtypes) != 3) | |
12256 | { | |
12257 | if (methodp) | |
8251199e | 12258 | cp_error ("`%D' must take exactly one argument", decl); |
8d08fdba | 12259 | else |
8251199e | 12260 | cp_error ("`%D' must take exactly two arguments", decl); |
8d08fdba | 12261 | } |
824b9a4c MS |
12262 | |
12263 | /* More Effective C++ rule 7. */ | |
eb448459 | 12264 | if (warn_ecpp |
824b9a4c MS |
12265 | && (name == ansi_opname [TRUTH_ANDIF_EXPR] |
12266 | || name == ansi_opname [TRUTH_ORIF_EXPR] | |
12267 | || name == ansi_opname [COMPOUND_EXPR])) | |
8251199e | 12268 | cp_warning ("user-defined `%D' always evaluates both arguments", |
824b9a4c MS |
12269 | decl); |
12270 | } | |
12271 | ||
12272 | /* Effective C++ rule 23. */ | |
eb448459 | 12273 | if (warn_ecpp |
824b9a4c MS |
12274 | && list_length (argtypes) == 3 |
12275 | && (name == ansi_opname [PLUS_EXPR] | |
12276 | || name == ansi_opname [MINUS_EXPR] | |
12277 | || name == ansi_opname [TRUNC_DIV_EXPR] | |
12278 | || name == ansi_opname [MULT_EXPR]) | |
12279 | && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE) | |
8251199e | 12280 | cp_warning ("`%D' should return by value", decl); |
8d08fdba MS |
12281 | |
12282 | /* 13.4.0.8 */ | |
12283 | if (argtypes) | |
12284 | for (; argtypes != void_list_node ; argtypes = TREE_CHAIN (argtypes)) | |
12285 | if (TREE_PURPOSE (argtypes)) | |
12286 | { | |
12287 | TREE_PURPOSE (argtypes) = NULL_TREE; | |
12288 | if (name == ansi_opname[(int) POSTINCREMENT_EXPR] | |
12289 | || name == ansi_opname[(int) POSTDECREMENT_EXPR]) | |
12290 | { | |
12291 | if (pedantic) | |
8251199e | 12292 | cp_pedwarn ("`%D' cannot have default arguments", decl); |
8d08fdba MS |
12293 | } |
12294 | else | |
8251199e | 12295 | cp_error ("`%D' cannot have default arguments", decl); |
8d08fdba MS |
12296 | } |
12297 | } | |
12298 | } | |
12299 | \f | |
d8e178a0 | 12300 | static const char * |
094fe153 JM |
12301 | tag_name (code) |
12302 | enum tag_types code; | |
12303 | { | |
12304 | switch (code) | |
12305 | { | |
12306 | case record_type: | |
12307 | return "struct"; | |
12308 | case class_type: | |
12309 | return "class"; | |
12310 | case union_type: | |
12311 | return "union "; | |
12312 | case enum_type: | |
12313 | return "enum"; | |
094fe153 JM |
12314 | default: |
12315 | my_friendly_abort (981122); | |
12316 | } | |
12317 | } | |
12318 | ||
8d08fdba MS |
12319 | /* Get the struct, enum or union (CODE says which) with tag NAME. |
12320 | Define the tag as a forward-reference if it is not defined. | |
12321 | ||
12322 | C++: If a class derivation is given, process it here, and report | |
12323 | an error if multiple derivation declarations are not identical. | |
12324 | ||
12325 | If this is a definition, come in through xref_tag and only look in | |
12326 | the current frame for the name (since C++ allows new names in any | |
12327 | scope.) */ | |
12328 | ||
8d08fdba | 12329 | tree |
ca107ded | 12330 | xref_tag (code_type_node, name, globalize) |
8d08fdba | 12331 | tree code_type_node; |
ca107ded | 12332 | tree name; |
8d08fdba MS |
12333 | int globalize; |
12334 | { | |
12335 | enum tag_types tag_code; | |
12336 | enum tree_code code; | |
8d08fdba | 12337 | register tree ref, t; |
8f032717 | 12338 | struct binding_level *b = current_binding_level; |
a80e4195 | 12339 | int got_type = 0; |
dc8263bc | 12340 | tree attributes = NULL_TREE; |
25aab5d0 | 12341 | tree context = NULL_TREE; |
dc8263bc JM |
12342 | |
12343 | /* If we are called from the parser, code_type_node will sometimes be a | |
12344 | TREE_LIST. This indicates that the user wrote | |
12345 | "class __attribute__ ((foo)) bar". Extract the attributes so we can | |
12346 | use them later. */ | |
12347 | if (TREE_CODE (code_type_node) == TREE_LIST) | |
12348 | { | |
12349 | attributes = TREE_PURPOSE (code_type_node); | |
12350 | code_type_node = TREE_VALUE (code_type_node); | |
12351 | } | |
8d08fdba | 12352 | |
665f2503 | 12353 | tag_code = (enum tag_types) tree_low_cst (code_type_node, 1); |
8d08fdba MS |
12354 | switch (tag_code) |
12355 | { | |
12356 | case record_type: | |
12357 | case class_type: | |
8d08fdba | 12358 | code = RECORD_TYPE; |
8d08fdba MS |
12359 | break; |
12360 | case union_type: | |
12361 | code = UNION_TYPE; | |
8d08fdba MS |
12362 | break; |
12363 | case enum_type: | |
12364 | code = ENUMERAL_TYPE; | |
12365 | break; | |
12366 | default: | |
12367 | my_friendly_abort (18); | |
12368 | } | |
12369 | ||
12370 | /* If a cross reference is requested, look up the type | |
12371 | already defined for this tag and return it. */ | |
be99da77 MS |
12372 | if (TREE_CODE_CLASS (TREE_CODE (name)) == 't') |
12373 | { | |
12374 | t = name; | |
a80e4195 MS |
12375 | name = TYPE_IDENTIFIER (t); |
12376 | got_type = 1; | |
be99da77 MS |
12377 | } |
12378 | else | |
12379 | t = IDENTIFIER_TYPE_VALUE (name); | |
61a127b3 | 12380 | |
73b0fce8 KL |
12381 | if (t && TREE_CODE (t) != code && TREE_CODE (t) != TEMPLATE_TYPE_PARM |
12382 | && TREE_CODE (t) != TEMPLATE_TEMPLATE_PARM) | |
a0a33927 MS |
12383 | t = NULL_TREE; |
12384 | ||
8ccc31eb | 12385 | if (! globalize) |
8d08fdba | 12386 | { |
f3400fe2 JM |
12387 | /* If we know we are defining this tag, only look it up in |
12388 | this scope and don't try to find it as a type. */ | |
12389 | ref = lookup_tag (code, name, b, 1); | |
8d08fdba MS |
12390 | } |
12391 | else | |
12392 | { | |
25aab5d0 | 12393 | if (t) |
36a117a5 | 12394 | { |
25aab5d0 MM |
12395 | /* [dcl.type.elab] If the identifier resolves to a |
12396 | typedef-name or a template type-parameter, the | |
12397 | elaborated-type-specifier is ill-formed. */ | |
12398 | if (t != TYPE_MAIN_VARIANT (t) | |
12399 | || (CLASS_TYPE_P (t) && TYPE_WAS_ANONYMOUS (t))) | |
12400 | cp_pedwarn ("using typedef-name `%D' after `%s'", | |
12401 | TYPE_NAME (t), tag_name (tag_code)); | |
12402 | else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM) | |
12403 | cp_error ("using template type parameter `%T' after `%s'", | |
12404 | t, tag_name (tag_code)); | |
12405 | ||
12406 | ref = t; | |
12407 | } | |
12408 | else | |
12409 | ref = lookup_tag (code, name, b, 0); | |
68642fb6 | 12410 | |
25aab5d0 MM |
12411 | if (! ref) |
12412 | { | |
12413 | /* Try finding it as a type declaration. If that wins, | |
68642fb6 | 12414 | use it. */ |
25aab5d0 MM |
12415 | ref = lookup_name (name, 1); |
12416 | ||
12417 | if (ref != NULL_TREE | |
12418 | && processing_template_decl | |
12419 | && DECL_CLASS_TEMPLATE_P (ref) | |
12420 | && template_class_depth (current_class_type) == 0) | |
12421 | /* Since GLOBALIZE is true, we're declaring a global | |
6757edfe | 12422 | template, so we want this type. */ |
25aab5d0 | 12423 | ref = DECL_RESULT (ref); |
6757edfe | 12424 | |
25aab5d0 MM |
12425 | if (ref && TREE_CODE (ref) == TYPE_DECL |
12426 | && TREE_CODE (TREE_TYPE (ref)) == code) | |
12427 | ref = TREE_TYPE (ref); | |
12428 | else | |
12429 | ref = NULL_TREE; | |
12430 | } | |
12431 | ||
68642fb6 UD |
12432 | if (ref && current_class_type |
12433 | && template_class_depth (current_class_type) | |
12434 | && PROCESSING_REAL_TEMPLATE_DECL_P ()) | |
25aab5d0 MM |
12435 | { |
12436 | /* Since GLOBALIZE is non-zero, we are not looking at a | |
12437 | definition of this tag. Since, in addition, we are currently | |
12438 | processing a (member) template declaration of a template | |
12439 | class, we must be very careful; consider: | |
12440 | ||
12441 | template <class X> | |
12442 | struct S1 | |
12443 | ||
12444 | template <class U> | |
12445 | struct S2 | |
12446 | { template <class V> | |
12447 | friend struct S1; }; | |
12448 | ||
12449 | Here, the S2::S1 declaration should not be confused with the | |
12450 | outer declaration. In particular, the inner version should | |
12451 | have a template parameter of level 2, not level 1. This | |
12452 | would be particularly important if the member declaration | |
12453 | were instead: | |
12454 | ||
12455 | template <class V = U> friend struct S1; | |
12456 | ||
12457 | say, when we should tsubst into `U' when instantiating | |
12458 | S2. On the other hand, when presented with: | |
12459 | ||
12460 | template <class T> | |
12461 | struct S1 { | |
12462 | template <class U> | |
12463 | struct S2 {}; | |
12464 | template <class U> | |
12465 | friend struct S2; | |
12466 | }; | |
12467 | ||
12468 | we must find the inner binding eventually. We | |
12469 | accomplish this by making sure that the new type we | |
12470 | create to represent this declaration has the right | |
12471 | TYPE_CONTEXT. */ | |
12472 | context = TYPE_CONTEXT (ref); | |
12473 | ref = NULL_TREE; | |
8d08fdba MS |
12474 | } |
12475 | } | |
12476 | ||
8d08fdba MS |
12477 | if (! ref) |
12478 | { | |
12479 | /* If no such tag is yet defined, create a forward-reference node | |
12480 | and record it as the "definition". | |
12481 | When a real declaration of this type is found, | |
12482 | the forward-reference will be altered into a real type. */ | |
8d08fdba MS |
12483 | if (code == ENUMERAL_TYPE) |
12484 | { | |
8251199e | 12485 | cp_error ("use of enum `%#D' without previous declaration", name); |
fc378698 | 12486 | |
8d08fdba MS |
12487 | ref = make_node (ENUMERAL_TYPE); |
12488 | ||
12489 | /* Give the type a default layout like unsigned int | |
12490 | to avoid crashing if it does not get defined. */ | |
12491 | TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node); | |
12492 | TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node); | |
12493 | TREE_UNSIGNED (ref) = 1; | |
12494 | TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node); | |
12495 | TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node); | |
12496 | TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node); | |
12497 | ||
12498 | /* Enable us to recognize when a type is created in class context. | |
12499 | To do nested classes correctly, this should probably be cleared | |
12500 | out when we leave this classes scope. Currently this in only | |
12501 | done in `start_enum'. */ | |
12502 | ||
12503 | pushtag (name, ref, globalize); | |
8d08fdba | 12504 | } |
8d08fdba MS |
12505 | else |
12506 | { | |
8d08fdba | 12507 | struct binding_level *old_b = class_binding_level; |
8d08fdba | 12508 | |
33848bb0 | 12509 | ref = make_aggr_type (code); |
25aab5d0 | 12510 | TYPE_CONTEXT (ref) = context; |
8d08fdba | 12511 | |
8d08fdba MS |
12512 | #ifdef NONNESTED_CLASSES |
12513 | /* Class types don't nest the way enums do. */ | |
12514 | class_binding_level = (struct binding_level *)0; | |
12515 | #endif | |
12516 | pushtag (name, ref, globalize); | |
12517 | class_binding_level = old_b; | |
8d08fdba MS |
12518 | } |
12519 | } | |
12520 | else | |
12521 | { | |
7fe6899f | 12522 | if (!globalize && processing_template_decl && IS_AGGR_TYPE (ref)) |
6757edfe | 12523 | redeclare_class_template (ref, current_template_parms); |
8d08fdba MS |
12524 | } |
12525 | ||
8d08fdba MS |
12526 | /* Until the type is defined, tentatively accept whatever |
12527 | structure tag the user hands us. */ | |
12528 | if (TYPE_SIZE (ref) == NULL_TREE | |
12529 | && ref != current_class_type | |
12530 | /* Have to check this, in case we have contradictory tag info. */ | |
12531 | && IS_AGGR_TYPE_CODE (TREE_CODE (ref))) | |
12532 | { | |
12533 | if (tag_code == class_type) | |
12534 | CLASSTYPE_DECLARED_CLASS (ref) = 1; | |
6eabb241 | 12535 | else if (tag_code == record_type) |
8d08fdba MS |
12536 | CLASSTYPE_DECLARED_CLASS (ref) = 0; |
12537 | } | |
12538 | ||
dc8263bc JM |
12539 | TREE_TYPE (ref) = attributes; |
12540 | ||
8d08fdba MS |
12541 | return ref; |
12542 | } | |
8ccc31eb | 12543 | |
fc378698 MS |
12544 | tree |
12545 | xref_tag_from_type (old, id, globalize) | |
12546 | tree old, id; | |
12547 | int globalize; | |
12548 | { | |
12549 | tree code_type_node; | |
12550 | ||
12551 | if (TREE_CODE (old) == RECORD_TYPE) | |
12552 | code_type_node = (CLASSTYPE_DECLARED_CLASS (old) | |
12553 | ? class_type_node : record_type_node); | |
12554 | else | |
12555 | code_type_node = union_type_node; | |
12556 | ||
12557 | if (id == NULL_TREE) | |
12558 | id = TYPE_IDENTIFIER (old); | |
12559 | ||
ca107ded | 12560 | return xref_tag (code_type_node, id, globalize); |
fc378698 MS |
12561 | } |
12562 | ||
3fd71a52 MM |
12563 | /* REF is a type (named NAME), for which we have just seen some |
12564 | baseclasses. BINFO is a list of those baseclasses; the | |
12565 | TREE_PURPOSE is an access_* node, and the TREE_VALUE is the type of | |
12566 | the base-class. CODE_TYPE_NODE indicates whether REF is a class, | |
12567 | struct, or union. */ | |
12568 | ||
8ccc31eb MS |
12569 | void |
12570 | xref_basetypes (code_type_node, name, ref, binfo) | |
12571 | tree code_type_node; | |
12572 | tree name, ref; | |
12573 | tree binfo; | |
12574 | { | |
12575 | /* In the declaration `A : X, Y, ... Z' we mark all the types | |
12576 | (A, X, Y, ..., Z) so we can check for duplicates. */ | |
12577 | tree binfos; | |
d6479fe7 MM |
12578 | tree base; |
12579 | ||
8ccc31eb | 12580 | int i, len; |
665f2503 | 12581 | enum tag_types tag_code = (enum tag_types) tree_low_cst (code_type_node, 1); |
8ccc31eb MS |
12582 | |
12583 | if (tag_code == union_type) | |
12584 | { | |
8251199e | 12585 | cp_error ("derived union `%T' invalid", ref); |
8ccc31eb MS |
12586 | return; |
12587 | } | |
12588 | ||
12589 | len = list_length (binfo); | |
8ccc31eb | 12590 | |
d6479fe7 MM |
12591 | /* First, make sure that any templates in base-classes are |
12592 | instantiated. This ensures that if we call ourselves recursively | |
12593 | we do not get confused about which classes are marked and which | |
12594 | are not. */ | |
12595 | for (base = binfo; base; base = TREE_CHAIN (base)) | |
12596 | complete_type (TREE_VALUE (base)); | |
12597 | ||
8ccc31eb MS |
12598 | SET_CLASSTYPE_MARKED (ref); |
12599 | BINFO_BASETYPES (TYPE_BINFO (ref)) = binfos = make_tree_vec (len); | |
12600 | ||
12601 | for (i = 0; binfo; binfo = TREE_CHAIN (binfo)) | |
12602 | { | |
12603 | /* The base of a derived struct is public by default. */ | |
12604 | int via_public | |
be99da77 MS |
12605 | = (TREE_PURPOSE (binfo) == access_public_node |
12606 | || TREE_PURPOSE (binfo) == access_public_virtual_node | |
8ccc31eb | 12607 | || (tag_code != class_type |
be99da77 MS |
12608 | && (TREE_PURPOSE (binfo) == access_default_node |
12609 | || TREE_PURPOSE (binfo) == access_default_virtual_node))); | |
d8b55a76 JM |
12610 | int via_protected |
12611 | = (TREE_PURPOSE (binfo) == access_protected_node | |
12612 | || TREE_PURPOSE (binfo) == access_protected_virtual_node); | |
8ccc31eb | 12613 | int via_virtual |
be99da77 | 12614 | = (TREE_PURPOSE (binfo) == access_private_virtual_node |
d8b55a76 | 12615 | || TREE_PURPOSE (binfo) == access_protected_virtual_node |
be99da77 MS |
12616 | || TREE_PURPOSE (binfo) == access_public_virtual_node |
12617 | || TREE_PURPOSE (binfo) == access_default_virtual_node); | |
12618 | tree basetype = TREE_VALUE (binfo); | |
8ccc31eb MS |
12619 | tree base_binfo; |
12620 | ||
8ccc31eb MS |
12621 | if (basetype && TREE_CODE (basetype) == TYPE_DECL) |
12622 | basetype = TREE_TYPE (basetype); | |
5566b478 MS |
12623 | if (!basetype |
12624 | || (TREE_CODE (basetype) != RECORD_TYPE | |
a80e4195 | 12625 | && TREE_CODE (basetype) != TYPENAME_TYPE |
73b0fce8 KL |
12626 | && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM |
12627 | && TREE_CODE (basetype) != TEMPLATE_TEMPLATE_PARM)) | |
8ccc31eb | 12628 | { |
8251199e | 12629 | cp_error ("base type `%T' fails to be a struct or class type", |
8ccc31eb MS |
12630 | TREE_VALUE (binfo)); |
12631 | continue; | |
12632 | } | |
2b9dc906 | 12633 | |
11b5139c | 12634 | GNU_xref_hier (name, basetype, via_public, via_virtual, 0); |
2b9dc906 | 12635 | |
8adf5b5e JM |
12636 | /* This code replaces similar code in layout_basetypes. |
12637 | We put the complete_type first for implicit `typename'. */ | |
d6479fe7 | 12638 | if (TYPE_SIZE (basetype) == NULL_TREE |
2b9dc906 | 12639 | && ! (current_template_parms && uses_template_parms (basetype))) |
8ccc31eb | 12640 | { |
8251199e | 12641 | cp_error ("base class `%T' has incomplete type", basetype); |
8ccc31eb MS |
12642 | continue; |
12643 | } | |
8ccc31eb MS |
12644 | else |
12645 | { | |
12646 | if (CLASSTYPE_MARKED (basetype)) | |
12647 | { | |
12648 | if (basetype == ref) | |
8251199e | 12649 | cp_error ("recursive type `%T' undefined", basetype); |
8ccc31eb | 12650 | else |
8251199e | 12651 | cp_error ("duplicate base type `%T' invalid", basetype); |
8ccc31eb MS |
12652 | continue; |
12653 | } | |
12654 | ||
eff71ab0 | 12655 | if (TYPE_FOR_JAVA (basetype) |
68642fb6 | 12656 | && (current_lang_stack |
9cd64686 | 12657 | == &VARRAY_TREE (current_lang_base, 0))) |
eff71ab0 PB |
12658 | TYPE_FOR_JAVA (ref) = 1; |
12659 | ||
8ccc31eb MS |
12660 | /* Note that the BINFO records which describe individual |
12661 | inheritances are *not* shared in the lattice! They | |
12662 | cannot be shared because a given baseclass may be | |
12663 | inherited with different `accessibility' by different | |
12664 | derived classes. (Each BINFO record describing an | |
12665 | individual inheritance contains flags which say what | |
12666 | the `accessibility' of that particular inheritance is.) */ | |
68642fb6 UD |
12667 | |
12668 | base_binfo | |
fed3cef0 | 12669 | = make_binfo (size_zero_node, basetype, |
7ddedda4 MM |
12670 | CLASS_TYPE_P (basetype) |
12671 | ? TYPE_BINFO_VTABLE (basetype) : NULL_TREE, | |
12672 | CLASS_TYPE_P (basetype) | |
12673 | ? TYPE_BINFO_VIRTUALS (basetype) : NULL_TREE); | |
68642fb6 | 12674 | |
8ccc31eb MS |
12675 | TREE_VEC_ELT (binfos, i) = base_binfo; |
12676 | TREE_VIA_PUBLIC (base_binfo) = via_public; | |
12677 | TREE_VIA_PROTECTED (base_binfo) = via_protected; | |
12678 | TREE_VIA_VIRTUAL (base_binfo) = via_virtual; | |
12679 | BINFO_INHERITANCE_CHAIN (base_binfo) = TYPE_BINFO (ref); | |
12680 | ||
dfbcd65a JM |
12681 | /* We need to unshare the binfos now so that lookups during class |
12682 | definition work. */ | |
12683 | unshare_base_binfos (base_binfo); | |
12684 | ||
8ccc31eb | 12685 | SET_CLASSTYPE_MARKED (basetype); |
9e9ff709 | 12686 | |
8ccc31eb MS |
12687 | /* We are free to modify these bits because they are meaningless |
12688 | at top level, and BASETYPE is a top-level type. */ | |
12689 | if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype)) | |
12690 | { | |
12691 | TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1; | |
4c6b7393 MM |
12692 | /* Converting to a virtual base class requires looking |
12693 | up the offset of the virtual base. */ | |
12694 | TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1; | |
8ccc31eb MS |
12695 | } |
12696 | ||
7ddedda4 MM |
12697 | if (CLASS_TYPE_P (basetype)) |
12698 | { | |
834c6dff MM |
12699 | TYPE_HAS_NEW_OPERATOR (ref) |
12700 | |= TYPE_HAS_NEW_OPERATOR (basetype); | |
12701 | TYPE_HAS_ARRAY_NEW_OPERATOR (ref) | |
12702 | |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype); | |
7ddedda4 | 12703 | TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype); |
4c6b7393 | 12704 | /* If the base-class uses multiple inheritance, so do we. */ |
68642fb6 | 12705 | TYPE_USES_MULTIPLE_INHERITANCE (ref) |
4c6b7393 MM |
12706 | |= TYPE_USES_MULTIPLE_INHERITANCE (basetype); |
12707 | /* Likewise, if converting to a base of the base may require | |
12708 | code, then we may need to generate code to convert to a | |
12709 | base as well. */ | |
68642fb6 | 12710 | TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) |
4c6b7393 | 12711 | |= TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype); |
7ddedda4 MM |
12712 | } |
12713 | ||
8ccc31eb MS |
12714 | i += 1; |
12715 | } | |
12716 | } | |
12717 | if (i) | |
12718 | TREE_VEC_LENGTH (binfos) = i; | |
12719 | else | |
12720 | BINFO_BASETYPES (TYPE_BINFO (ref)) = NULL_TREE; | |
12721 | ||
12722 | if (i > 1) | |
7ddedda4 | 12723 | { |
4c6b7393 MM |
12724 | TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1; |
12725 | /* If there is more than one non-empty they cannot be at the same | |
12726 | address. */ | |
12727 | TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1; | |
7ddedda4 MM |
12728 | } |
12729 | ||
8ccc31eb MS |
12730 | /* Unmark all the types. */ |
12731 | while (--i >= 0) | |
12732 | CLEAR_CLASSTYPE_MARKED (BINFO_TYPE (TREE_VEC_ELT (binfos, i))); | |
12733 | CLEAR_CLASSTYPE_MARKED (ref); | |
12734 | ||
70c532b5 MM |
12735 | /* Now that we know all the base-classes, set up the list of virtual |
12736 | bases. */ | |
23381155 | 12737 | get_vbase_types (ref); |
8ccc31eb | 12738 | } |
68642fb6 | 12739 | |
8d08fdba | 12740 | \f |
8d08fdba MS |
12741 | /* Begin compiling the definition of an enumeration type. |
12742 | NAME is its name (or null if anonymous). | |
12743 | Returns the type object, as yet incomplete. | |
12744 | Also records info about it so that build_enumerator | |
12745 | may be used to declare the individual values as they are read. */ | |
12746 | ||
12747 | tree | |
12748 | start_enum (name) | |
12749 | tree name; | |
12750 | { | |
12751 | register tree enumtype = NULL_TREE; | |
8f032717 | 12752 | struct binding_level *b = current_binding_level; |
8d08fdba MS |
12753 | |
12754 | /* If this is the real definition for a previous forward reference, | |
12755 | fill in the contents in the same object that used to be the | |
12756 | forward reference. */ | |
12757 | ||
12758 | if (name != NULL_TREE) | |
12759 | enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1); | |
12760 | ||
12761 | if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE) | |
30ff8252 NS |
12762 | { |
12763 | cp_error ("multiple definition of `%#T'", enumtype); | |
12764 | cp_error_at ("previous definition here", enumtype); | |
58595203 MM |
12765 | /* Clear out TYPE_VALUES, and start again. */ |
12766 | TYPE_VALUES (enumtype) = NULL_TREE; | |
30ff8252 | 12767 | } |
8d08fdba MS |
12768 | else |
12769 | { | |
12770 | enumtype = make_node (ENUMERAL_TYPE); | |
12771 | pushtag (name, enumtype, 0); | |
12772 | } | |
12773 | ||
12774 | if (current_class_type) | |
12775 | TREE_ADDRESSABLE (b->tags) = 1; | |
c91a56d2 | 12776 | |
8d08fdba MS |
12777 | GNU_xref_decl (current_function_decl, enumtype); |
12778 | return enumtype; | |
12779 | } | |
12780 | ||
12781 | /* After processing and defining all the values of an enumeration type, | |
12782 | install their decls in the enumeration type and finish it off. | |
12783 | ENUMTYPE is the type object and VALUES a list of name-value pairs. | |
12784 | Returns ENUMTYPE. */ | |
12785 | ||
12786 | tree | |
dbfe2124 MM |
12787 | finish_enum (enumtype) |
12788 | tree enumtype; | |
8d08fdba | 12789 | { |
a703fb38 | 12790 | register tree minnode = NULL_TREE, maxnode = NULL_TREE; |
8d08fdba MS |
12791 | /* Calculate the maximum value of any enumerator in this type. */ |
12792 | ||
dbfe2124 | 12793 | tree values = TYPE_VALUES (enumtype); |
8d08fdba MS |
12794 | if (values) |
12795 | { | |
ed44da02 | 12796 | tree pair; |
5566b478 | 12797 | |
ed44da02 | 12798 | for (pair = values; pair; pair = TREE_CHAIN (pair)) |
8d08fdba | 12799 | { |
ed44da02 MM |
12800 | tree decl; |
12801 | tree value; | |
12802 | ||
12803 | /* The TREE_VALUE is a CONST_DECL for this enumeration | |
12804 | constant. */ | |
12805 | decl = TREE_VALUE (pair); | |
12806 | ||
58595203 MM |
12807 | /* [dcl.enum] |
12808 | ||
12809 | Following the closing brace of an enum-specifier, each | |
12810 | enumerator has the type of its enumeration. Prior to the | |
12811 | closing brace, the type of each enumerator is the type of | |
12812 | its initializing value. */ | |
12813 | TREE_TYPE (decl) = enumtype; | |
12814 | ||
ed44da02 MM |
12815 | /* The DECL_INITIAL will be NULL if we are processing a |
12816 | template declaration and this enumeration constant had no | |
12817 | explicit initializer. */ | |
12818 | value = DECL_INITIAL (decl); | |
079e1098 | 12819 | if (value && !processing_template_decl) |
5566b478 | 12820 | { |
079e1098 MM |
12821 | /* Set the TREE_TYPE for the VALUE as well. That's so |
12822 | that when we call decl_constant_value we get an | |
12823 | entity of the right type (but with the constant | |
12824 | value). Since we shouldn't ever call | |
12825 | decl_constant_value on a template type, there's no | |
12826 | reason to do that when processing_template_decl. | |
12827 | And, if the expression is something like a | |
12828 | TEMPLATE_PARM_INDEX or a CAST_EXPR doing so will | |
68642fb6 | 12829 | wreak havoc on the intended type of the expression. |
079e1098 MM |
12830 | |
12831 | Of course, there's also no point in trying to compute | |
12832 | minimum or maximum values if we're in a template. */ | |
5566b478 | 12833 | TREE_TYPE (value) = enumtype; |
ed44da02 | 12834 | |
079e1098 MM |
12835 | if (!minnode) |
12836 | minnode = maxnode = value; | |
12837 | else if (tree_int_cst_lt (maxnode, value)) | |
12838 | maxnode = value; | |
12839 | else if (tree_int_cst_lt (value, minnode)) | |
12840 | minnode = value; | |
5566b478 | 12841 | } |
ed44da02 | 12842 | |
68642fb6 | 12843 | if (processing_template_decl) |
72f2bd78 MM |
12844 | /* If this is just a template, leave the CONST_DECL |
12845 | alone. That way tsubst_copy will find CONST_DECLs for | |
12846 | CONST_DECLs, and not INTEGER_CSTs. */ | |
12847 | ; | |
12848 | else | |
12849 | /* In the list we're building up, we want the enumeration | |
12850 | values, not the CONST_DECLs. */ | |
12851 | TREE_VALUE (pair) = value; | |
8d08fdba MS |
12852 | } |
12853 | } | |
f376e137 MS |
12854 | else |
12855 | maxnode = minnode = integer_zero_node; | |
8d08fdba | 12856 | |
de22184b | 12857 | TYPE_VALUES (enumtype) = nreverse (values); |
2986ae00 | 12858 | |
5156628f | 12859 | if (processing_template_decl) |
b87692e5 | 12860 | { |
9360ac70 MM |
12861 | tree scope = current_scope (); |
12862 | if (scope && TREE_CODE (scope) == FUNCTION_DECL) | |
cbf882af | 12863 | add_tree (build_min (TAG_DEFN, enumtype)); |
b87692e5 | 12864 | } |
cbf882af MM |
12865 | else |
12866 | { | |
12867 | int unsignedp = tree_int_cst_sgn (minnode) >= 0; | |
12868 | int lowprec = min_precision (minnode, unsignedp); | |
12869 | int highprec = min_precision (maxnode, unsignedp); | |
12870 | int precision = MAX (lowprec, highprec); | |
12871 | tree tem; | |
5566b478 | 12872 | |
cbf882af | 12873 | TYPE_SIZE (enumtype) = NULL_TREE; |
8d08fdba | 12874 | |
cbf882af | 12875 | /* Set TYPE_MIN_VALUE and TYPE_MAX_VALUE according to `precision'. */ |
8ccc31eb | 12876 | |
cbf882af MM |
12877 | TYPE_PRECISION (enumtype) = precision; |
12878 | if (unsignedp) | |
12879 | fixup_unsigned_type (enumtype); | |
12880 | else | |
12881 | fixup_signed_type (enumtype); | |
8ccc31eb | 12882 | |
cbf882af MM |
12883 | if (flag_short_enums || (precision > TYPE_PRECISION (integer_type_node))) |
12884 | /* Use the width of the narrowest normal C type which is wide | |
68642fb6 | 12885 | enough. */ |
cbf882af MM |
12886 | TYPE_PRECISION (enumtype) = TYPE_PRECISION (type_for_size |
12887 | (precision, 1)); | |
12888 | else | |
12889 | TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node); | |
8d08fdba | 12890 | |
cbf882af MM |
12891 | TYPE_SIZE (enumtype) = 0; |
12892 | layout_type (enumtype); | |
68642fb6 | 12893 | |
cbf882af MM |
12894 | /* Fix up all variant types of this enum type. */ |
12895 | for (tem = TYPE_MAIN_VARIANT (enumtype); tem; | |
12896 | tem = TYPE_NEXT_VARIANT (tem)) | |
12897 | { | |
12898 | TYPE_VALUES (tem) = TYPE_VALUES (enumtype); | |
12899 | TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype); | |
12900 | TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype); | |
12901 | TYPE_SIZE (tem) = TYPE_SIZE (enumtype); | |
12902 | TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype); | |
12903 | TYPE_MODE (tem) = TYPE_MODE (enumtype); | |
12904 | TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype); | |
12905 | TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype); | |
12906 | TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype); | |
12907 | } | |
8d08fdba | 12908 | |
cbf882af MM |
12909 | /* Finish debugging output for this type. */ |
12910 | rest_of_type_compilation (enumtype, namespace_bindings_p ()); | |
12911 | } | |
12912 | ||
8d08fdba MS |
12913 | return enumtype; |
12914 | } | |
12915 | ||
079e1098 | 12916 | /* Build and install a CONST_DECL for an enumeration constant of the |
58595203 | 12917 | enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided. |
8d08fdba MS |
12918 | Assignment of sequential values by default is handled here. */ |
12919 | ||
58595203 MM |
12920 | void |
12921 | build_enumerator (name, value, enumtype) | |
079e1098 MM |
12922 | tree name; |
12923 | tree value; | |
58595203 | 12924 | tree enumtype; |
8d08fdba | 12925 | { |
58595203 | 12926 | tree decl; |
e8bd800e | 12927 | tree context; |
58595203 MM |
12928 | tree type; |
12929 | tree values; | |
8d08fdba MS |
12930 | |
12931 | /* Remove no-op casts from the value. */ | |
12932 | if (value) | |
12933 | STRIP_TYPE_NOPS (value); | |
12934 | ||
58595203 MM |
12935 | if (! processing_template_decl) |
12936 | { | |
12937 | /* Validate and default VALUE. */ | |
12938 | if (value != NULL_TREE) | |
12939 | { | |
12940 | if (TREE_READONLY_DECL_P (value)) | |
12941 | value = decl_constant_value (value); | |
12942 | ||
12943 | if (TREE_CODE (value) == INTEGER_CST) | |
12944 | { | |
12945 | value = default_conversion (value); | |
12946 | constant_expression_warning (value); | |
12947 | } | |
12948 | else | |
12949 | { | |
12950 | cp_error ("enumerator value for `%D' not integer constant", name); | |
12951 | value = NULL_TREE; | |
12952 | } | |
12953 | } | |
12954 | ||
12955 | /* Default based on previous value. */ | |
12956 | if (value == NULL_TREE && ! processing_template_decl) | |
12957 | { | |
12958 | tree prev_value; | |
12959 | ||
12960 | if (TYPE_VALUES (enumtype)) | |
12961 | { | |
12962 | /* The next value is the previous value ... */ | |
12963 | prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype))); | |
12964 | /* ... plus one. */ | |
c7e266a6 MM |
12965 | value = build_binary_op (PLUS_EXPR, |
12966 | prev_value, | |
12967 | integer_one_node); | |
68642fb6 | 12968 | |
58595203 MM |
12969 | if (tree_int_cst_lt (value, prev_value)) |
12970 | cp_error ("overflow in enumeration values at `%D'", name); | |
12971 | } | |
12972 | else | |
12973 | value = integer_zero_node; | |
12974 | } | |
12975 | ||
12976 | /* Remove no-op casts from the value. */ | |
12977 | if (value) | |
12978 | STRIP_TYPE_NOPS (value); | |
013bc8af | 12979 | #if 0 |
58595203 MM |
12980 | /* To fix MAX_VAL enum consts. (bkoz) */ |
12981 | TREE_TYPE (value) = integer_type_node; | |
013bc8af | 12982 | #endif |
58595203 | 12983 | } |
8d08fdba | 12984 | |
58595203 MM |
12985 | /* We always have to copy here; not all INTEGER_CSTs are unshared. |
12986 | Even in other cases, we will later (in finish_enum) be setting | |
12987 | the type of VALUE. But, we don't need to make a copy if this | |
12988 | VALUE is one of the enumeration constants for this same | |
12989 | enumeration type. */ | |
12990 | for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values)) | |
12991 | if (TREE_VALUE (values) == value) | |
12992 | break; | |
12993 | /* If we didn't break out of the loop, then we do need a copy. */ | |
12994 | if (!values && value) | |
12995 | value = copy_node (value); | |
ed44da02 | 12996 | |
8d08fdba | 12997 | /* C++ associates enums with global, function, or class declarations. */ |
58595203 MM |
12998 | context = current_scope (); |
12999 | ||
13000 | /* Build the actual enumeration constant. Note that the enumeration | |
13001 | constants have the type of their initializers until the | |
13002 | enumeration is complete: | |
13003 | ||
13004 | [ dcl.enum ] | |
13005 | ||
13006 | Following the closing brace of an enum-specifier, each enumer- | |
13007 | ator has the type of its enumeration. Prior to the closing | |
13008 | brace, the type of each enumerator is the type of its | |
13009 | initializing value. | |
13010 | ||
13011 | In finish_enum we will reset the type. Of course, if we're | |
13012 | processing a template, there may be no value. */ | |
13013 | type = value ? TREE_TYPE (value) : NULL_TREE; | |
13014 | ||
13015 | if (context && context == current_class_type) | |
13016 | /* This enum declaration is local to the class. We need the full | |
13017 | lang_decl so that we can record DECL_CLASS_CONTEXT, for example. */ | |
13018 | decl = build_lang_decl (CONST_DECL, name, type); | |
13019 | else | |
13020 | /* It's a global enum, or it's local to a function. (Note local to | |
e8bd800e | 13021 | a function could mean local to a class method. */ |
58595203 | 13022 | decl = build_decl (CONST_DECL, name, type); |
e8bd800e | 13023 | |
58595203 MM |
13024 | DECL_CONTEXT (decl) = FROB_CONTEXT (context); |
13025 | DECL_INITIAL (decl) = value; | |
13026 | TREE_READONLY (decl) = 1; | |
e8bd800e | 13027 | |
58595203 MM |
13028 | if (context && context == current_class_type) |
13029 | /* In something like `struct S { enum E { i = 7 }; };' we put `i' | |
8f032717 MM |
13030 | on the TYPE_FIELDS list for `S'. (That's so that you can say |
13031 | things like `S::i' later.) */ | |
58595203 MM |
13032 | finish_member_declaration (decl); |
13033 | else | |
13034 | { | |
13035 | pushdecl (decl); | |
13036 | GNU_xref_decl (current_function_decl, decl); | |
13037 | } | |
13038 | ||
13039 | /* Add this enumeration constant to the list for this type. */ | |
13040 | TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype)); | |
8d08fdba MS |
13041 | } |
13042 | ||
8d08fdba | 13043 | \f |
5566b478 MS |
13044 | static int function_depth; |
13045 | ||
a8f73d4b MM |
13046 | /* We're defining DECL. Make sure that it's type is OK. */ |
13047 | ||
13048 | static void | |
13049 | check_function_type (decl) | |
13050 | tree decl; | |
13051 | { | |
13052 | tree fntype = TREE_TYPE (decl); | |
13053 | ||
13054 | /* In a function definition, arg types must be complete. */ | |
13055 | require_complete_types_for_parms (current_function_parms); | |
13056 | ||
13057 | if (TYPE_SIZE (complete_type (TREE_TYPE (fntype))) == NULL_TREE) | |
13058 | { | |
13059 | cp_error ("return type `%#T' is incomplete", TREE_TYPE (fntype)); | |
13060 | ||
13061 | /* Make it return void instead, but don't change the | |
13062 | type of the DECL_RESULT, in case we have a named return value. */ | |
13063 | if (TREE_CODE (fntype) == METHOD_TYPE) | |
13064 | { | |
13065 | tree ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype))); | |
13066 | TREE_TYPE (decl) | |
13067 | = build_cplus_method_type (ctype, | |
13068 | void_type_node, | |
13069 | FUNCTION_ARG_CHAIN (decl)); | |
13070 | } | |
13071 | else | |
13072 | TREE_TYPE (decl) | |
13073 | = build_function_type (void_type_node, | |
13074 | TYPE_ARG_TYPES (TREE_TYPE (decl))); | |
68642fb6 | 13075 | TREE_TYPE (decl) |
a8f73d4b MM |
13076 | = build_exception_variant (fntype, |
13077 | TYPE_RAISES_EXCEPTIONS (fntype)); | |
13078 | } | |
13079 | else | |
13080 | abstract_virtuals_error (decl, TREE_TYPE (fntype)); | |
13081 | } | |
13082 | ||
8d08fdba MS |
13083 | /* Create the FUNCTION_DECL for a function definition. |
13084 | DECLSPECS and DECLARATOR are the parts of the declaration; | |
13085 | they describe the function's name and the type it returns, | |
13086 | but twisted together in a fashion that parallels the syntax of C. | |
13087 | ||
a8f73d4b MM |
13088 | FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the |
13089 | DECLARATOR is really the DECL for the function we are about to | |
13090 | process and that DECLSPECS should be ignored), SF_INCLASS_INLINE | |
13091 | indicating that the function is an inline defined in-class, and | |
13092 | SF_EXPAND indicating that we should generate RTL for this | |
68642fb6 UD |
13093 | function. |
13094 | ||
8d08fdba MS |
13095 | This function creates a binding context for the function body |
13096 | as well as setting up the FUNCTION_DECL in current_function_decl. | |
13097 | ||
13098 | Returns 1 on success. If the DECLARATOR is not suitable for a function | |
13099 | (it defines a datum instead), we return 0, which tells | |
13100 | yyparse to report a parse error. | |
13101 | ||
13102 | For C++, we must first check whether that datum makes any sense. | |
13103 | For example, "class A local_a(1,2);" means that variable local_a | |
13104 | is an aggregate of type A, which should have a constructor | |
87e3dbc9 | 13105 | applied to it with the argument list [1, 2]. */ |
8d08fdba MS |
13106 | |
13107 | int | |
a8f73d4b | 13108 | start_function (declspecs, declarator, attrs, flags) |
c11b6f21 | 13109 | tree declspecs, declarator, attrs; |
a8f73d4b | 13110 | int flags; |
8d08fdba | 13111 | { |
5566b478 | 13112 | tree decl1; |
8d08fdba MS |
13113 | tree ctype = NULL_TREE; |
13114 | tree fntype; | |
13115 | tree restype; | |
13116 | extern int have_extern_spec; | |
13117 | extern int used_extern_spec; | |
13118 | int doing_friend = 0; | |
a8f73d4b | 13119 | struct binding_level *bl; |
8d08fdba | 13120 | |
8d08fdba | 13121 | /* Sanity check. */ |
a1774733 | 13122 | my_friendly_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE, 160); |
8d08fdba MS |
13123 | my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161); |
13124 | ||
e92cc029 | 13125 | /* This should only be done once on the top most decl. */ |
8d08fdba MS |
13126 | if (have_extern_spec && !used_extern_spec) |
13127 | { | |
13128 | declspecs = decl_tree_cons (NULL_TREE, get_identifier ("extern"), declspecs); | |
13129 | used_extern_spec = 1; | |
13130 | } | |
13131 | ||
a8f73d4b | 13132 | if (flags & SF_PRE_PARSED) |
8d08fdba MS |
13133 | { |
13134 | decl1 = declarator; | |
13135 | ||
8d08fdba MS |
13136 | fntype = TREE_TYPE (decl1); |
13137 | if (TREE_CODE (fntype) == METHOD_TYPE) | |
13138 | ctype = TYPE_METHOD_BASETYPE (fntype); | |
13139 | ||
cab1f180 ML |
13140 | /* ISO C++ 11.4/5. A friend function defined in a class is in |
13141 | the (lexical) scope of the class in which it is defined. */ | |
8d08fdba MS |
13142 | if (!ctype && DECL_FRIEND_P (decl1)) |
13143 | { | |
4f1c5b7d | 13144 | ctype = DECL_FRIEND_CONTEXT (decl1); |
8d08fdba MS |
13145 | |
13146 | /* CTYPE could be null here if we're dealing with a template; | |
13147 | for example, `inline friend float foo()' inside a template | |
13148 | will have no CTYPE set. */ | |
13149 | if (ctype && TREE_CODE (ctype) != RECORD_TYPE) | |
13150 | ctype = NULL_TREE; | |
13151 | else | |
13152 | doing_friend = 1; | |
13153 | } | |
13154 | ||
5566b478 MS |
13155 | last_function_parms = DECL_ARGUMENTS (decl1); |
13156 | last_function_parm_tags = NULL_TREE; | |
8d08fdba MS |
13157 | } |
13158 | else | |
13159 | { | |
c11b6f21 | 13160 | decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL_TREE); |
8d08fdba MS |
13161 | /* If the declarator is not suitable for a function definition, |
13162 | cause a syntax error. */ | |
13163 | if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL) return 0; | |
13164 | ||
13165 | fntype = TREE_TYPE (decl1); | |
13166 | ||
13167 | restype = TREE_TYPE (fntype); | |
7ddedda4 | 13168 | if (CLASS_TYPE_P (restype) && !CLASSTYPE_GOT_SEMICOLON (restype)) |
8d08fdba | 13169 | { |
8251199e | 13170 | cp_error ("semicolon missing after declaration of `%#T'", restype); |
051e6fd7 | 13171 | shadow_tag (build_tree_list (NULL_TREE, restype)); |
8d08fdba MS |
13172 | CLASSTYPE_GOT_SEMICOLON (restype) = 1; |
13173 | if (TREE_CODE (fntype) == FUNCTION_TYPE) | |
13174 | fntype = build_function_type (integer_type_node, | |
13175 | TYPE_ARG_TYPES (fntype)); | |
13176 | else | |
13177 | fntype = build_cplus_method_type (build_type_variant (TYPE_METHOD_BASETYPE (fntype), TREE_READONLY (decl1), TREE_SIDE_EFFECTS (decl1)), | |
13178 | integer_type_node, | |
13179 | TYPE_ARG_TYPES (fntype)); | |
13180 | TREE_TYPE (decl1) = fntype; | |
13181 | } | |
13182 | ||
13183 | if (TREE_CODE (fntype) == METHOD_TYPE) | |
13184 | ctype = TYPE_METHOD_BASETYPE (fntype); | |
35680744 | 13185 | else if (DECL_MAIN_P (decl1)) |
8d08fdba MS |
13186 | { |
13187 | /* If this doesn't return integer_type, complain. */ | |
13188 | if (TREE_TYPE (TREE_TYPE (decl1)) != integer_type_node) | |
13189 | { | |
a28e3c7f | 13190 | if (pedantic || warn_return_type) |
8251199e | 13191 | pedwarn ("return type for `main' changed to `int'"); |
8d08fdba MS |
13192 | TREE_TYPE (decl1) = fntype = default_function_type; |
13193 | } | |
8d08fdba MS |
13194 | } |
13195 | } | |
68642fb6 | 13196 | |
b35d4555 MM |
13197 | /* Sometimes we don't notice that a function is a static member, and |
13198 | build a METHOD_TYPE for it. Fix that up now. */ | |
13199 | if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1) | |
13200 | && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE) | |
13201 | { | |
3afb32a4 | 13202 | revert_static_member_fn (decl1); |
b35d4555 MM |
13203 | last_function_parms = TREE_CHAIN (last_function_parms); |
13204 | ctype = NULL_TREE; | |
13205 | } | |
8d08fdba MS |
13206 | |
13207 | /* Warn if function was previously implicitly declared | |
13208 | (but not if we warned then). */ | |
13209 | if (! warn_implicit | |
13210 | && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE) | |
8251199e | 13211 | cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1))); |
8d08fdba | 13212 | |
f181d4ae MM |
13213 | /* Set up current_class_type, and enter the scope of the class, if |
13214 | appropriate. */ | |
13215 | if (ctype) | |
13216 | push_nested_class (ctype, 1); | |
13217 | else if (DECL_STATIC_FUNCTION_P (decl1)) | |
13218 | push_nested_class (DECL_CONTEXT (decl1), 2); | |
13219 | ||
13220 | /* Now that we have entered the scope of the class, we must restore | |
13221 | the bindings for any template parameters surrounding DECL1, if it | |
13222 | is an inline member template. (Order is important; consider the | |
13223 | case where a template parameter has the same name as a field of | |
13224 | the class.) It is not until after this point that | |
13225 | PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */ | |
a8f73d4b | 13226 | if (flags & SF_INCLASS_INLINE) |
f181d4ae MM |
13227 | maybe_begin_member_template_processing (decl1); |
13228 | ||
9188c363 MM |
13229 | /* Effective C++ rule 15. See also c_expand_return. */ |
13230 | if (warn_ecpp | |
13231 | && DECL_NAME (decl1) == ansi_opname[(int) MODIFY_EXPR] | |
13232 | && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE) | |
13233 | cp_warning ("`operator=' should return a reference to `*this'"); | |
13234 | ||
13235 | /* Make the init_value nonzero so pushdecl knows this is not tentative. | |
13236 | error_mark_node is replaced below (in poplevel) with the BLOCK. */ | |
b35d4555 MM |
13237 | if (!DECL_INITIAL (decl1)) |
13238 | DECL_INITIAL (decl1) = error_mark_node; | |
9188c363 MM |
13239 | |
13240 | #ifdef SET_DEFAULT_DECL_ATTRIBUTES | |
13241 | SET_DEFAULT_DECL_ATTRIBUTES (decl1, attrs); | |
13242 | #endif | |
68642fb6 | 13243 | |
9188c363 MM |
13244 | /* This function exists in static storage. |
13245 | (This does not mean `static' in the C sense!) */ | |
13246 | TREE_STATIC (decl1) = 1; | |
13247 | ||
13248 | /* We must call push_template_decl after current_class_type is set | |
13249 | up. (If we are processing inline definitions after exiting a | |
13250 | class scope, current_class_type will be NULL_TREE until set above | |
13251 | by push_nested_class.) */ | |
13252 | if (processing_template_decl) | |
13253 | decl1 = push_template_decl (decl1); | |
13254 | ||
f181d4ae | 13255 | /* We are now in the scope of the function being defined. */ |
8d08fdba | 13256 | current_function_decl = decl1; |
f181d4ae | 13257 | |
5566b478 MS |
13258 | /* Save the parm names or decls from this function's declarator |
13259 | where store_parm_decls will find them. */ | |
13260 | current_function_parms = last_function_parms; | |
13261 | current_function_parm_tags = last_function_parm_tags; | |
8d08fdba | 13262 | |
a8f73d4b MM |
13263 | /* Make sure the parameter and return types are reasonable. When |
13264 | you declare a function, these types can be incomplete, but they | |
13265 | must be complete when you define the function. */ | |
5156628f | 13266 | if (! processing_template_decl) |
a8f73d4b | 13267 | check_function_type (decl1); |
f181d4ae | 13268 | |
a8f73d4b MM |
13269 | /* Build the return declaration for the function. */ |
13270 | restype = TREE_TYPE (fntype); | |
13271 | if (!processing_template_decl) | |
13272 | { | |
13273 | if (!DECL_RESULT (decl1)) | |
5566b478 | 13274 | { |
5566b478 | 13275 | DECL_RESULT (decl1) |
a8f73d4b | 13276 | = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype)); |
68642fb6 UD |
13277 | c_apply_type_quals_to_decl (CP_TYPE_QUALS (restype), |
13278 | DECL_RESULT (decl1)); | |
5566b478 | 13279 | } |
5566b478 | 13280 | } |
a8f73d4b MM |
13281 | else |
13282 | /* Just use `void'. Nobody will ever look at this anyhow. */ | |
13283 | DECL_RESULT (decl1) = build_decl (RESULT_DECL, 0, void_type_node); | |
13284 | ||
13285 | /* Initialize RTL machinery. We cannot do this until | |
13286 | CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this | |
13287 | even when processing a template; this is how we get | |
01d939e8 | 13288 | CFUN set up, and our per-function variables initialized. */ |
a8f73d4b MM |
13289 | bl = current_binding_level; |
13290 | init_function_start (decl1, input_filename, lineno); | |
13291 | current_binding_level = bl; | |
13292 | expanding_p = (flags & SF_EXPAND) != 0; | |
13293 | ||
13294 | /* Even though we're inside a function body, we still don't want to | |
13295 | call expand_expr to calculate the size of a variable-sized array. | |
13296 | We haven't necessarily assigned RTL to all variables yet, so it's | |
13297 | not safe to try to expand expressions involving them. */ | |
13298 | immediate_size_expand = 0; | |
01d939e8 | 13299 | cfun->x_dont_save_pending_sizes_p = 1; |
a8f73d4b | 13300 | |
6f80451c MM |
13301 | /* If we're building a statement-tree, start the tree now. */ |
13302 | if (processing_template_decl || !expanding_p) | |
13303 | begin_stmt_tree (&DECL_SAVED_TREE (decl1)); | |
13304 | ||
a8f73d4b | 13305 | /* Let the user know we're compiling this function. */ |
24bef158 | 13306 | if (processing_template_decl || !building_stmt_tree ()) |
a8f73d4b | 13307 | announce_function (decl1); |
b7484fbe | 13308 | |
878cd289 MS |
13309 | /* Record the decl so that the function name is defined. |
13310 | If we already have a decl for this name, and it is a FUNCTION_DECL, | |
13311 | use the old decl. */ | |
a8f73d4b | 13312 | if (!processing_template_decl && !(flags & SF_PRE_PARSED)) |
878cd289 | 13313 | { |
75650646 | 13314 | /* A specialization is not used to guide overload resolution. */ |
68642fb6 | 13315 | if ((flag_guiding_decls |
959d8796 JM |
13316 | || !DECL_TEMPLATE_SPECIALIZATION (decl1)) |
13317 | && ! DECL_FUNCTION_MEMBER_P (decl1)) | |
75650646 | 13318 | decl1 = pushdecl (decl1); |
2c73f9f5 | 13319 | else |
b7698cf0 JM |
13320 | { |
13321 | /* We need to set the DECL_CONTEXT. */ | |
13322 | if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1)) | |
13323 | DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1)); | |
13324 | /* And make sure we have enough default args. */ | |
13325 | check_default_args (decl1); | |
13326 | } | |
878cd289 MS |
13327 | DECL_MAIN_VARIANT (decl1) = decl1; |
13328 | fntype = TREE_TYPE (decl1); | |
13329 | } | |
5566b478 | 13330 | |
a8f73d4b | 13331 | /* Reset these in case the call to pushdecl changed them. */ |
5566b478 | 13332 | current_function_decl = decl1; |
01d939e8 | 13333 | cfun->decl = decl1; |
878cd289 | 13334 | |
b35d4555 MM |
13335 | /* Initialize the per-function data. */ |
13336 | if (!DECL_PENDING_INLINE_P (decl1) && DECL_SAVED_FUNCTION_DATA (decl1)) | |
13337 | { | |
13338 | /* If we already parsed this function, and we're just expanding it | |
13339 | now, restore saved state. */ | |
13340 | struct binding_level *bl = current_binding_level; | |
13341 | *cp_function_chain = *DECL_SAVED_FUNCTION_DATA (decl1); | |
13342 | current_binding_level = bl; | |
13343 | ||
b35d4555 MM |
13344 | /* This function is being processed in whole-function mode; we |
13345 | already did semantic analysis. */ | |
01d939e8 | 13346 | cfun->x_whole_function_mode_p = 1; |
b35d4555 | 13347 | |
914653a2 MM |
13348 | /* If we decided that we didn't want to inline this function, |
13349 | make sure the back-end knows that. */ | |
13350 | if (!current_function_cannot_inline) | |
13351 | current_function_cannot_inline = cp_function_chain->cannot_inline; | |
13352 | ||
b35d4555 MM |
13353 | /* We don't need the saved data anymore. */ |
13354 | free (DECL_SAVED_FUNCTION_DATA (decl1)); | |
13355 | DECL_SAVED_FUNCTION_DATA (decl1) = NULL; | |
13356 | } | |
13357 | else if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1)) | |
13358 | { | |
13359 | /* We know that this was set up by `grokclassfn'. We do not | |
13360 | wait until `store_parm_decls', since evil parse errors may | |
13361 | never get us to that point. Here we keep the consistency | |
13362 | between `current_class_type' and `current_class_ptr'. */ | |
13363 | tree t = DECL_ARGUMENTS (decl1); | |
68642fb6 UD |
13364 | |
13365 | my_friendly_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL, | |
b35d4555 MM |
13366 | 162); |
13367 | my_friendly_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE, | |
13368 | 19990811); | |
68642fb6 UD |
13369 | |
13370 | cp_function_chain->x_current_class_ref | |
b35d4555 MM |
13371 | = build_indirect_ref (t, NULL_PTR); |
13372 | cp_function_chain->x_current_class_ptr = t; | |
13373 | ||
018fc244 MM |
13374 | /* Constructors and destructors need to know whether they're "in |
13375 | charge" of initializing virtual base classes. */ | |
b35d4555 MM |
13376 | if (DECL_DESTRUCTOR_P (decl1)) |
13377 | current_in_charge_parm = TREE_CHAIN (t); | |
018fc244 MM |
13378 | else if (DECL_CONSTRUCTOR_P (decl1) |
13379 | && TREE_CHAIN (t) | |
13380 | && DECL_ARTIFICIAL (TREE_CHAIN (t)) | |
13381 | && (DECL_NAME (TREE_CHAIN (t)) | |
13382 | == in_charge_identifier)) | |
13383 | current_in_charge_parm = TREE_CHAIN (t); | |
b35d4555 MM |
13384 | } |
13385 | ||
db5ae43f | 13386 | if (DECL_INTERFACE_KNOWN (decl1)) |
faae18ab | 13387 | { |
4f1c5b7d | 13388 | tree ctx = decl_function_context (decl1); |
86052cc3 | 13389 | |
faae18ab MS |
13390 | if (DECL_NOT_REALLY_EXTERN (decl1)) |
13391 | DECL_EXTERNAL (decl1) = 0; | |
86052cc3 | 13392 | |
68642fb6 | 13393 | if (ctx != NULL_TREE && DECL_THIS_INLINE (ctx) |
86052cc3 JM |
13394 | && TREE_PUBLIC (ctx)) |
13395 | /* This is a function in a local class in an extern inline | |
13396 | function. */ | |
13397 | comdat_linkage (decl1); | |
faae18ab | 13398 | } |
8d08fdba MS |
13399 | /* If this function belongs to an interface, it is public. |
13400 | If it belongs to someone else's interface, it is also external. | |
1f901793 | 13401 | This only affects inlines and template instantiations. */ |
5566b478 MS |
13402 | else if (interface_unknown == 0 |
13403 | && (! DECL_TEMPLATE_INSTANTIATION (decl1) | |
d11ad92e | 13404 | || flag_alt_external_templates)) |
8d08fdba | 13405 | { |
5566b478 | 13406 | if (DECL_THIS_INLINE (decl1) || DECL_TEMPLATE_INSTANTIATION (decl1) |
5156628f | 13407 | || processing_template_decl) |
1f901793 JM |
13408 | { |
13409 | DECL_EXTERNAL (decl1) | |
13410 | = (interface_only | |
9c73ec84 MS |
13411 | || (DECL_THIS_INLINE (decl1) && ! flag_implement_inlines |
13412 | && !DECL_VINDEX (decl1))); | |
1f901793 JM |
13413 | |
13414 | /* For WIN32 we also want to put these in linkonce sections. */ | |
13415 | maybe_make_one_only (decl1); | |
13416 | } | |
db5ae43f | 13417 | else |
893de33c | 13418 | DECL_EXTERNAL (decl1) = 0; |
e8abc66f | 13419 | DECL_NOT_REALLY_EXTERN (decl1) = 0; |
db5ae43f | 13420 | DECL_INTERFACE_KNOWN (decl1) = 1; |
8d08fdba | 13421 | } |
c16c47fb JM |
13422 | else if (interface_unknown && interface_only |
13423 | && (! DECL_TEMPLATE_INSTANTIATION (decl1) | |
13424 | || flag_alt_external_templates)) | |
13425 | { | |
13426 | /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma | |
13427 | interface, we will have interface_only set but not | |
13428 | interface_known. In that case, we don't want to use the normal | |
13429 | heuristics because someone will supply a #pragma implementation | |
13430 | elsewhere, and deducing it here would produce a conflict. */ | |
13431 | comdat_linkage (decl1); | |
13432 | DECL_EXTERNAL (decl1) = 0; | |
13433 | DECL_INTERFACE_KNOWN (decl1) = 1; | |
13434 | DECL_DEFER_OUTPUT (decl1) = 1; | |
13435 | } | |
8d08fdba | 13436 | else |
a0a33927 MS |
13437 | { |
13438 | /* This is a definition, not a reference. | |
b7484fbe MS |
13439 | So clear DECL_EXTERNAL. */ |
13440 | DECL_EXTERNAL (decl1) = 0; | |
faae18ab | 13441 | |
5566b478 MS |
13442 | if ((DECL_THIS_INLINE (decl1) || DECL_TEMPLATE_INSTANTIATION (decl1)) |
13443 | && ! DECL_INTERFACE_KNOWN (decl1) | |
13444 | /* Don't try to defer nested functions for now. */ | |
4f1c5b7d | 13445 | && ! decl_function_context (decl1)) |
878cd289 MS |
13446 | DECL_DEFER_OUTPUT (decl1) = 1; |
13447 | else | |
893de33c | 13448 | DECL_INTERFACE_KNOWN (decl1) = 1; |
db5ae43f | 13449 | } |
a9aedbc2 | 13450 | |
b35d4555 | 13451 | if (doing_semantic_analysis_p ()) |
8d08fdba | 13452 | { |
b35d4555 MM |
13453 | pushlevel (0); |
13454 | current_binding_level->parm_flag = 1; | |
8d08fdba | 13455 | } |
8d08fdba | 13456 | |
f30432d7 MS |
13457 | if (attrs) |
13458 | cplus_decl_attributes (decl1, NULL_TREE, attrs); | |
68642fb6 | 13459 | |
558475f0 MM |
13460 | if (!building_stmt_tree ()) |
13461 | { | |
13462 | GNU_xref_function (decl1, current_function_parms); | |
13463 | make_function_rtl (decl1); | |
13464 | } | |
db5ae43f | 13465 | |
8d08fdba MS |
13466 | /* Promote the value to int before returning it. */ |
13467 | if (C_PROMOTING_INTEGER_TYPE_P (restype)) | |
5566b478 MS |
13468 | restype = type_promotes_to (restype); |
13469 | ||
13470 | /* If this fcn was already referenced via a block-scope `extern' decl | |
13471 | (or an implicit decl), propagate certain information about the usage. */ | |
13472 | if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (decl1))) | |
13473 | TREE_ADDRESSABLE (decl1) = 1; | |
13474 | ||
8d08fdba | 13475 | if (DECL_RESULT (decl1) == NULL_TREE) |
f30432d7 MS |
13476 | { |
13477 | DECL_RESULT (decl1) | |
13478 | = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype)); | |
91063b51 MM |
13479 | TREE_READONLY (DECL_RESULT (decl1)) = CP_TYPE_CONST_P (restype); |
13480 | TREE_THIS_VOLATILE (DECL_RESULT (decl1)) = CP_TYPE_VOLATILE_P (restype); | |
f30432d7 | 13481 | } |
8d08fdba | 13482 | |
5566b478 MS |
13483 | ++function_depth; |
13484 | ||
f30432d7 MS |
13485 | if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl1)) |
13486 | && DECL_LANGUAGE (decl1) == lang_cplusplus) | |
46e8c075 MM |
13487 | { |
13488 | dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE); | |
13489 | DECL_CONTEXT (dtor_label) = current_function_decl; | |
13490 | } | |
b35d4555 | 13491 | else if (DECL_CONSTRUCTOR_P (decl1)) |
46e8c075 MM |
13492 | { |
13493 | ctor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE); | |
13494 | DECL_CONTEXT (ctor_label) = current_function_decl; | |
13495 | } | |
8d08fdba | 13496 | |
8d08fdba MS |
13497 | return 1; |
13498 | } | |
13499 | \f | |
3e4010c4 | 13500 | /* Called after store_parm_decls for a function-try-block. */ |
6467930b MS |
13501 | |
13502 | void | |
13503 | expand_start_early_try_stmts () | |
13504 | { | |
6467930b | 13505 | expand_start_try_stmts (); |
f30432d7 MS |
13506 | } |
13507 | ||
8d08fdba MS |
13508 | /* Store the parameter declarations into the current function declaration. |
13509 | This is called after parsing the parameter declarations, before | |
13510 | digesting the body of the function. | |
13511 | ||
13512 | Also install to binding contour return value identifier, if any. */ | |
13513 | ||
13514 | void | |
13515 | store_parm_decls () | |
13516 | { | |
13517 | register tree fndecl = current_function_decl; | |
13518 | register tree parm; | |
13519 | int parms_have_cleanups = 0; | |
eb66be0e | 13520 | tree cleanups = NULL_TREE; |
8d08fdba | 13521 | |
8d08fdba MS |
13522 | /* This is a list of types declared among parms in a prototype. */ |
13523 | tree parmtags = current_function_parm_tags; | |
13524 | ||
13525 | /* This is a chain of any other decls that came in among the parm | |
13526 | declarations. If a parm is declared with enum {foo, bar} x; | |
13527 | then CONST_DECLs for foo and bar are put here. */ | |
13528 | tree nonparms = NULL_TREE; | |
13529 | ||
8d08fdba | 13530 | /* Create a binding level for the parms. */ |
b35d4555 MM |
13531 | if (!building_stmt_tree ()) |
13532 | expand_start_bindings (2); | |
8d08fdba | 13533 | |
b35d4555 | 13534 | if (current_function_parms) |
8d08fdba MS |
13535 | { |
13536 | /* This case is when the function was defined with an ANSI prototype. | |
13537 | The parms already have decls, so we need not do anything here | |
13538 | except record them as in effect | |
13539 | and complain if any redundant old-style parm decls were written. */ | |
13540 | ||
b35d4555 MM |
13541 | tree specparms = current_function_parms; |
13542 | tree next; | |
13543 | ||
13544 | if (doing_semantic_analysis_p ()) | |
13545 | { | |
13546 | /* Must clear this because it might contain TYPE_DECLs declared | |
13547 | at class level. */ | |
13548 | storedecls (NULL_TREE); | |
8d08fdba | 13549 | |
b35d4555 MM |
13550 | /* If we're doing semantic analysis, then we'll call pushdecl |
13551 | for each of these. We must do them in reverse order so that | |
13552 | they end in the correct forward order. */ | |
13553 | specparms = nreverse (specparms); | |
13554 | } | |
5566b478 | 13555 | |
b35d4555 | 13556 | for (parm = specparms; parm; parm = next) |
8d08fdba MS |
13557 | { |
13558 | next = TREE_CHAIN (parm); | |
13559 | if (TREE_CODE (parm) == PARM_DECL) | |
13560 | { | |
fe1b3b96 MM |
13561 | tree type = TREE_TYPE (parm); |
13562 | ||
b35d4555 | 13563 | if (doing_semantic_analysis_p ()) |
8d08fdba | 13564 | { |
fe1b3b96 | 13565 | tree cleanup; |
68642fb6 | 13566 | |
b35d4555 | 13567 | if (DECL_NAME (parm) == NULL_TREE |
fe1b3b96 | 13568 | || TREE_CODE (parm) != VOID_TYPE) |
b35d4555 MM |
13569 | pushdecl (parm); |
13570 | else | |
13571 | cp_error ("parameter `%D' declared void", parm); | |
eb66be0e | 13572 | |
ff47d094 MM |
13573 | cleanup = (processing_template_decl |
13574 | ? NULL_TREE | |
13575 | : maybe_build_cleanup (parm)); | |
68642fb6 | 13576 | |
fe1b3b96 MM |
13577 | if (cleanup) |
13578 | cleanups = tree_cons (parm, cleanup, cleanups); | |
8d08fdba | 13579 | } |
fe1b3b96 | 13580 | else if (type != error_mark_node |
834c6dff | 13581 | && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) |
fe1b3b96 | 13582 | parms_have_cleanups = 1; |
8d08fdba MS |
13583 | } |
13584 | else | |
13585 | { | |
13586 | /* If we find an enum constant or a type tag, | |
13587 | put it aside for the moment. */ | |
13588 | TREE_CHAIN (parm) = NULL_TREE; | |
13589 | nonparms = chainon (nonparms, parm); | |
13590 | } | |
13591 | } | |
13592 | ||
b35d4555 MM |
13593 | if (doing_semantic_analysis_p ()) |
13594 | { | |
13595 | /* Get the decls in their original chain order | |
13596 | and record in the function. This is all and only the | |
13597 | PARM_DECLs that were pushed into scope by the loop above. */ | |
13598 | DECL_ARGUMENTS (fndecl) = getdecls (); | |
13599 | storetags (chainon (parmtags, gettags ())); | |
b35d4555 | 13600 | } |
8d08fdba MS |
13601 | } |
13602 | else | |
13603 | DECL_ARGUMENTS (fndecl) = NULL_TREE; | |
13604 | ||
13605 | /* Now store the final chain of decls for the arguments | |
13606 | as the decl-chain of the current lexical scope. | |
13607 | Put the enumerators in as well, at the front so that | |
13608 | DECL_ARGUMENTS is not modified. */ | |
b35d4555 MM |
13609 | if (doing_semantic_analysis_p ()) |
13610 | storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl))); | |
8d08fdba MS |
13611 | |
13612 | /* Initialize the RTL code for the function. */ | |
49ad7cfa | 13613 | DECL_SAVED_INSNS (fndecl) = 0; |
558475f0 | 13614 | if (! building_stmt_tree ()) |
5566b478 | 13615 | expand_function_start (fndecl, parms_have_cleanups); |
8d08fdba | 13616 | |
eb448459 MS |
13617 | current_function_parms_stored = 1; |
13618 | ||
13619 | /* If this function is `main', emit a call to `__main' | |
13620 | to run global initializers, etc. */ | |
558475f0 | 13621 | if (DECL_MAIN_P (fndecl) && !building_stmt_tree ()) |
35680744 | 13622 | expand_main_function (); |
eb448459 | 13623 | |
eb66be0e MS |
13624 | /* Now that we have initialized the parms, we can start their |
13625 | cleanups. We cannot do this before, since expand_decl_cleanup | |
13626 | should not be called before the parm can be used. */ | |
fe1b3b96 MM |
13627 | while (cleanups) |
13628 | { | |
68642fb6 | 13629 | finish_decl_cleanup (TREE_PURPOSE (cleanups), |
fe1b3b96 MM |
13630 | TREE_VALUE (cleanups)); |
13631 | cleanups = TREE_CHAIN (cleanups); | |
13632 | } | |
eb66be0e | 13633 | |
8d08fdba MS |
13634 | /* Create a binding contour which can be used to catch |
13635 | cleanup-generated temporaries. Also, if the return value needs or | |
13636 | has initialization, deal with that now. */ | |
13637 | if (parms_have_cleanups) | |
13638 | { | |
13639 | pushlevel (0); | |
558475f0 | 13640 | if (!building_stmt_tree ()) |
b35d4555 | 13641 | expand_start_bindings (2); |
8d08fdba MS |
13642 | } |
13643 | ||
b35d4555 | 13644 | /* Do the starting of the exception specifications, if we have any. */ |
68642fb6 | 13645 | if (flag_exceptions && !processing_template_decl |
1660cb3a | 13646 | && flag_enforce_eh_specs |
68642fb6 | 13647 | && building_stmt_tree () |
b35d4555 MM |
13648 | && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl))) |
13649 | current_eh_spec_try_block = expand_start_eh_spec (); | |
8d08fdba MS |
13650 | } |
13651 | ||
13652 | /* Bind a name and initialization to the return value of | |
13653 | the current function. */ | |
e92cc029 | 13654 | |
8d08fdba | 13655 | void |
558475f0 MM |
13656 | store_return_init (decl) |
13657 | tree decl; | |
8d08fdba | 13658 | { |
558475f0 MM |
13659 | /* If this named return value comes in a register, put it in a |
13660 | pseudo-register. */ | |
13661 | if (DECL_REGISTER (decl)) | |
8d08fdba | 13662 | { |
558475f0 | 13663 | original_result_rtx = DECL_RTL (decl); |
f0ad3f46 MM |
13664 | /* Note that the mode of the old DECL_RTL may be wider than the |
13665 | mode of DECL_RESULT, depending on the calling conventions for | |
13666 | the processor. For example, on the Alpha, a 32-bit integer | |
13667 | is returned in a DImode register -- the DECL_RESULT has | |
13668 | SImode but the DECL_RTL for the DECL_RESULT has DImode. So, | |
13669 | here, we use the mode the back-end has already assigned for | |
13670 | the return value. */ | |
13671 | DECL_RTL (decl) = gen_reg_rtx (GET_MODE (original_result_rtx)); | |
8d08fdba MS |
13672 | } |
13673 | } | |
13674 | ||
8d08fdba | 13675 | \f |
59026e79 MM |
13676 | /* We have finished doing semantic analysis on DECL, but have not yet |
13677 | generated RTL for its body. Save away our current state, so that | |
13678 | when we want to generate RTL later we know what to do. */ | |
13679 | ||
13680 | static void | |
13681 | save_function_data (decl) | |
13682 | tree decl; | |
13683 | { | |
13684 | struct language_function *f; | |
13685 | ||
13686 | /* Save the language-specific per-function data so that we can | |
13687 | get it back when we really expand this function. */ | |
13688 | my_friendly_assert (!DECL_PENDING_INLINE_P (decl), | |
13689 | 19990908); | |
68642fb6 | 13690 | |
59026e79 | 13691 | /* Make a copy. */ |
68642fb6 | 13692 | f = ((struct language_function *) |
59026e79 MM |
13693 | xmalloc (sizeof (struct language_function))); |
13694 | bcopy ((char *) cp_function_chain, (char *) f, | |
13695 | sizeof (struct language_function)); | |
13696 | DECL_SAVED_FUNCTION_DATA (decl) = f; | |
13697 | ||
13698 | /* Clear out the bits we don't need. */ | |
13699 | f->x_base_init_list = NULL_TREE; | |
13700 | f->x_member_init_list = NULL_TREE; | |
6f80451c MM |
13701 | f->x_stmt_tree.x_last_stmt = NULL_TREE; |
13702 | f->x_stmt_tree.x_last_expr_type = NULL_TREE; | |
59026e79 MM |
13703 | f->x_result_rtx = NULL_RTX; |
13704 | f->x_named_label_uses = NULL; | |
13705 | f->bindings = NULL; | |
13706 | ||
13707 | /* When we get back here again, we will be expanding. */ | |
13708 | f->x_expanding_p = 1; | |
914653a2 MM |
13709 | |
13710 | /* If we've already decided that we cannot inline this function, we | |
13711 | must remember that fact when we actually go to expand the | |
13712 | function. */ | |
13713 | f->cannot_inline = current_function_cannot_inline; | |
59026e79 MM |
13714 | } |
13715 | ||
efee38a9 MM |
13716 | /* At the end of every constructor we generate to code to return |
13717 | `this'. Do that now. */ | |
13718 | ||
13719 | static void | |
13720 | finish_constructor_body () | |
13721 | { | |
13722 | /* Any return from a constructor will end up here. */ | |
13723 | add_tree (build_min_nt (LABEL_STMT, ctor_label)); | |
13724 | ||
13725 | /* Clear CTOR_LABEL so that finish_return_stmt knows to really | |
13726 | generate the return, rather than a goto to CTOR_LABEL. */ | |
13727 | ctor_label = NULL_TREE; | |
13728 | /* In check_return_expr we translate an empty return from a | |
13729 | constructor to a return of `this'. */ | |
13730 | finish_return_stmt (NULL_TREE); | |
46e8c075 MM |
13731 | /* Mark the end of the constructor. */ |
13732 | add_tree (build_min_nt (CTOR_STMT)); | |
efee38a9 MM |
13733 | } |
13734 | ||
9bfadf57 MM |
13735 | /* At the end of every destructor we generate code to restore virtual |
13736 | function tables to the values desired by base classes and to call | |
efee38a9 | 13737 | to base class destructors. Do that now. */ |
9bfadf57 MM |
13738 | |
13739 | static void | |
13740 | finish_destructor_body () | |
13741 | { | |
13742 | tree compound_stmt; | |
13743 | tree in_charge; | |
13744 | tree virtual_size; | |
13745 | tree exprstmt; | |
13746 | ||
13747 | /* Create a block to contain all the extra code. */ | |
13748 | compound_stmt = begin_compound_stmt (/*has_no_scope=*/0); | |
13749 | ||
efee38a9 MM |
13750 | /* Any return from a destructor will end up here. */ |
13751 | add_tree (build_min_nt (LABEL_STMT, dtor_label)); | |
13752 | ||
9bfadf57 MM |
13753 | /* Generate the code to call destructor on base class. If this |
13754 | destructor belongs to a class with virtual functions, then set | |
13755 | the virtual function table pointer to represent the type of our | |
13756 | base class. */ | |
13757 | ||
13758 | /* This side-effect makes call to `build_delete' generate the code | |
13759 | we have to have at the end of this destructor. `build_delete' | |
13760 | will set the flag again. */ | |
13761 | TYPE_HAS_DESTRUCTOR (current_class_type) = 0; | |
13762 | ||
13763 | /* These are two cases where we cannot delegate deletion. */ | |
13764 | if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type) | |
13765 | || TYPE_GETS_REG_DELETE (current_class_type)) | |
13766 | in_charge = integer_zero_node; | |
13767 | else | |
13768 | in_charge = current_in_charge_parm; | |
13769 | ||
13770 | exprstmt = build_delete (current_class_type, | |
68642fb6 | 13771 | current_class_ref, |
9bfadf57 | 13772 | in_charge, |
68642fb6 | 13773 | LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL, |
9bfadf57 MM |
13774 | 0); |
13775 | ||
13776 | if (exprstmt != error_mark_node | |
13777 | && (TREE_CODE (exprstmt) != NOP_EXPR | |
13778 | || TREE_OPERAND (exprstmt, 0) != integer_zero_node | |
13779 | || TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))) | |
13780 | { | |
9bfadf57 MM |
13781 | if (exprstmt != void_zero_node) |
13782 | /* Don't call `expand_expr_stmt' if we're not going to do | |
13783 | anything, since -Wall will give a diagnostic. */ | |
13784 | finish_expr_stmt (exprstmt); | |
13785 | ||
efee38a9 | 13786 | /* Run destructors for all virtual baseclasses. */ |
9bfadf57 MM |
13787 | if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type)) |
13788 | { | |
13789 | tree vbases = nreverse (copy_list (CLASSTYPE_VBASECLASSES (current_class_type))); | |
13790 | tree if_stmt = begin_if_stmt (); | |
13791 | finish_if_stmt_cond (build (BIT_AND_EXPR, integer_type_node, | |
68642fb6 | 13792 | current_in_charge_parm, |
9bfadf57 MM |
13793 | integer_two_node), |
13794 | if_stmt); | |
13795 | ||
13796 | while (vbases) | |
13797 | { | |
834c6dff | 13798 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (vbases))) |
9bfadf57 MM |
13799 | { |
13800 | tree vb = get_vbase | |
13801 | (BINFO_TYPE (vbases), | |
13802 | TYPE_BINFO (current_class_type)); | |
13803 | finish_expr_stmt | |
13804 | (build_scoped_method_call | |
13805 | (current_class_ref, vb, dtor_identifier, | |
051e6fd7 | 13806 | build_tree_list (NULL_TREE, integer_zero_node))); |
9bfadf57 MM |
13807 | } |
13808 | vbases = TREE_CHAIN (vbases); | |
13809 | } | |
13810 | ||
13811 | finish_then_clause (if_stmt); | |
13812 | finish_if_stmt (); | |
13813 | } | |
13814 | } | |
68642fb6 | 13815 | |
9bfadf57 MM |
13816 | virtual_size = c_sizeof (current_class_type); |
13817 | ||
13818 | /* At the end, call delete if that's what's requested. */ | |
68642fb6 | 13819 | |
9bfadf57 MM |
13820 | /* FDIS sez: At the point of definition of a virtual destructor |
13821 | (including an implicit definition), non-placement operator delete | |
13822 | shall be looked up in the scope of the destructor's class and if | |
13823 | found shall be accessible and unambiguous. | |
68642fb6 | 13824 | |
9bfadf57 MM |
13825 | This is somewhat unclear, but I take it to mean that if the class |
13826 | only defines placement deletes we don't do anything here. So we | |
13827 | pass LOOKUP_SPECULATIVELY; delete_sanity will complain for us if | |
13828 | they ever try to delete one of these. */ | |
13829 | if (TYPE_GETS_REG_DELETE (current_class_type) | |
13830 | || TYPE_USES_VIRTUAL_BASECLASSES (current_class_type)) | |
13831 | { | |
13832 | tree if_stmt; | |
13833 | ||
13834 | exprstmt = build_op_delete_call | |
13835 | (DELETE_EXPR, current_class_ptr, virtual_size, | |
13836 | LOOKUP_NORMAL | LOOKUP_SPECULATIVELY, NULL_TREE); | |
13837 | ||
13838 | if_stmt = begin_if_stmt (); | |
13839 | finish_if_stmt_cond (build (BIT_AND_EXPR, integer_type_node, | |
13840 | current_in_charge_parm, | |
13841 | integer_one_node), | |
13842 | if_stmt); | |
13843 | finish_expr_stmt (exprstmt); | |
13844 | finish_then_clause (if_stmt); | |
13845 | finish_if_stmt (); | |
13846 | } | |
13847 | ||
13848 | /* Close the block we started above. */ | |
13849 | finish_compound_stmt (/*has_no_scope=*/0, compound_stmt); | |
13850 | } | |
13851 | ||
8d08fdba MS |
13852 | /* Finish up a function declaration and compile that function |
13853 | all the way to assembler language output. The free the storage | |
13854 | for the function definition. | |
13855 | ||
13856 | This is called after parsing the body of the function definition. | |
13857 | LINENO is the current line number. | |
13858 | ||
68642fb6 UD |
13859 | FLAGS is a bitwise or of the following values: |
13860 | 1 - CALL_POPLEVEL | |
f181d4ae MM |
13861 | An extra call to poplevel (and expand_end_bindings) must be |
13862 | made to take care of the binding contour for the base | |
13863 | initializers. This is only relevant for constructors. | |
13864 | 2 - INCLASS_INLINE | |
13865 | We just finished processing the body of an in-class inline | |
13866 | function definition. (This processing will have taken place | |
87e3dbc9 | 13867 | after the class definition is complete.) */ |
8d08fdba | 13868 | |
4d6abc1c | 13869 | tree |
87e3dbc9 | 13870 | finish_function (lineno, flags) |
8d08fdba | 13871 | int lineno; |
f181d4ae | 13872 | int flags; |
8d08fdba MS |
13873 | { |
13874 | register tree fndecl = current_function_decl; | |
13875 | tree fntype, ctype = NULL_TREE; | |
8d08fdba MS |
13876 | /* Label to use if this function is supposed to return a value. */ |
13877 | tree no_return_label = NULL_TREE; | |
f181d4ae MM |
13878 | int call_poplevel = (flags & 1) != 0; |
13879 | int inclass_inline = (flags & 2) != 0; | |
558475f0 | 13880 | int expand_p; |
87e3dbc9 | 13881 | int nested; |
8d08fdba MS |
13882 | |
13883 | /* When we get some parse errors, we can end up without a | |
13884 | current_function_decl, so cope. */ | |
13885 | if (fndecl == NULL_TREE) | |
4d6abc1c | 13886 | return error_mark_node; |
8d08fdba | 13887 | |
87e3dbc9 | 13888 | nested = function_depth > 1; |
8d08fdba MS |
13889 | fntype = TREE_TYPE (fndecl); |
13890 | ||
9bfadf57 MM |
13891 | /* TREE_READONLY (fndecl) = 1; |
13892 | This caused &foo to be of type ptr-to-const-function | |
13893 | which then got a warning when stored in a ptr-to-function variable. */ | |
8d08fdba MS |
13894 | |
13895 | /* This happens on strange parse errors. */ | |
13896 | if (! current_function_parms_stored) | |
13897 | { | |
13898 | call_poplevel = 0; | |
13899 | store_parm_decls (); | |
13900 | } | |
13901 | ||
558475f0 | 13902 | if (building_stmt_tree ()) |
8d08fdba | 13903 | { |
efee38a9 MM |
13904 | if (DECL_CONSTRUCTOR_P (fndecl)) |
13905 | { | |
13906 | finish_constructor_body (); | |
13907 | if (call_poplevel) | |
13908 | do_poplevel (); | |
13909 | } | |
9bfadf57 MM |
13910 | else if (DECL_DESTRUCTOR_P (fndecl) && !processing_template_decl) |
13911 | finish_destructor_body (); | |
efee38a9 MM |
13912 | else if (DECL_MAIN_P (fndecl)) |
13913 | { | |
13914 | /* Make it so that `main' always returns 0 by default. */ | |
13915 | #ifdef VMS | |
13916 | finish_return_stmt (integer_one_node); | |
13917 | #else | |
13918 | finish_return_stmt (integer_zero_node); | |
13919 | #endif | |
13920 | } | |
87e3dbc9 | 13921 | |
b35d4555 MM |
13922 | /* Finish dealing with exception specifiers. */ |
13923 | if (flag_exceptions && !processing_template_decl | |
1660cb3a | 13924 | && flag_enforce_eh_specs |
b35d4555 | 13925 | && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl))) |
68642fb6 | 13926 | expand_end_eh_spec (TYPE_RAISES_EXCEPTIONS |
b35d4555 MM |
13927 | (TREE_TYPE (current_function_decl)), |
13928 | current_eh_spec_try_block); | |
5566b478 MS |
13929 | } |
13930 | else | |
13931 | { | |
c95cd22e | 13932 | #if 0 |
5566b478 | 13933 | if (write_symbols != NO_DEBUG /*&& TREE_CODE (fntype) != METHOD_TYPE*/) |
8d08fdba | 13934 | { |
ae673f14 JM |
13935 | /* Keep this code around in case we later want to control debug info |
13936 | based on whether a type is "used". (jason 1999-11-11) */ | |
13937 | ||
c95cd22e JM |
13938 | tree ttype = target_type (fntype); |
13939 | tree parmdecl; | |
13940 | ||
8d08fdba MS |
13941 | if (IS_AGGR_TYPE (ttype)) |
13942 | /* Let debugger know it should output info for this type. */ | |
13943 | note_debug_info_needed (ttype); | |
5566b478 MS |
13944 | |
13945 | for (parmdecl = DECL_ARGUMENTS (fndecl); parmdecl; parmdecl = TREE_CHAIN (parmdecl)) | |
13946 | { | |
13947 | ttype = target_type (TREE_TYPE (parmdecl)); | |
13948 | if (IS_AGGR_TYPE (ttype)) | |
13949 | /* Let debugger know it should output info for this type. */ | |
13950 | note_debug_info_needed (ttype); | |
13951 | } | |
8d08fdba | 13952 | } |
c95cd22e | 13953 | #endif |
8d08fdba | 13954 | |
5566b478 MS |
13955 | /* Clean house because we will need to reorder insns here. */ |
13956 | do_pending_stack_adjust (); | |
8d08fdba | 13957 | |
5566b478 | 13958 | if (dtor_label) |
9bfadf57 | 13959 | ; |
b35d4555 | 13960 | else if (DECL_CONSTRUCTOR_P (fndecl)) |
8d08fdba | 13961 | { |
8d08fdba | 13962 | if (call_poplevel) |
b35d4555 | 13963 | do_poplevel (); |
8d08fdba | 13964 | } |
5566b478 | 13965 | else if (return_label != NULL_RTX |
9bfadf57 MM |
13966 | && flag_this_is_variable <= 0 |
13967 | && current_function_return_value == NULL_TREE | |
13968 | && ! DECL_NAME (DECL_RESULT (current_function_decl))) | |
5566b478 | 13969 | no_return_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE); |
8d08fdba | 13970 | |
eb448459 MS |
13971 | if (flag_exceptions) |
13972 | expand_exception_blocks (); | |
13973 | ||
5566b478 MS |
13974 | /* If this function is supposed to return a value, ensure that |
13975 | we do not fall into the cleanups by mistake. The end of our | |
13976 | function will look like this: | |
68642fb6 | 13977 | |
5566b478 MS |
13978 | user code (may have return stmt somewhere) |
13979 | goto no_return_label | |
13980 | cleanup_label: | |
13981 | cleanups | |
13982 | goto return_label | |
13983 | no_return_label: | |
13984 | NOTE_INSN_FUNCTION_END | |
13985 | return_label: | |
13986 | things for return | |
68642fb6 | 13987 | |
5566b478 MS |
13988 | If the user omits a return stmt in the USER CODE section, we |
13989 | will have a control path which reaches NOTE_INSN_FUNCTION_END. | |
13990 | Otherwise, we won't. */ | |
13991 | if (no_return_label) | |
8d08fdba | 13992 | { |
5566b478 MS |
13993 | DECL_CONTEXT (no_return_label) = fndecl; |
13994 | DECL_INITIAL (no_return_label) = error_mark_node; | |
13995 | DECL_SOURCE_FILE (no_return_label) = input_filename; | |
13996 | DECL_SOURCE_LINE (no_return_label) = lineno; | |
13997 | expand_goto (no_return_label); | |
8d08fdba MS |
13998 | } |
13999 | ||
5566b478 | 14000 | if (cleanup_label) |
a9aedbc2 | 14001 | { |
2c73f9f5 | 14002 | /* Remove the binding contour which is used |
5566b478 MS |
14003 | to catch cleanup-generated temporaries. */ |
14004 | expand_end_bindings (0, 0, 0); | |
14005 | poplevel (0, 0, 0); | |
8d08fdba | 14006 | |
eb448459 MS |
14007 | /* Emit label at beginning of cleanup code for parameters. */ |
14008 | emit_label (cleanup_label); | |
14009 | } | |
b7484fbe | 14010 | |
a8f73d4b MM |
14011 | /* Get return value into register if that's where it's supposed |
14012 | to be. */ | |
5566b478 MS |
14013 | if (original_result_rtx) |
14014 | fixup_result_decl (DECL_RESULT (fndecl), original_result_rtx); | |
8d08fdba | 14015 | |
5566b478 MS |
14016 | /* Finish building code that will trigger warnings if users forget |
14017 | to make their functions return values. */ | |
14018 | if (no_return_label || cleanup_label) | |
14019 | emit_jump (return_label); | |
14020 | if (no_return_label) | |
8d08fdba | 14021 | { |
5566b478 MS |
14022 | /* We don't need to call `expand_*_return' here because we |
14023 | don't need any cleanups here--this path of code is only | |
14024 | for error checking purposes. */ | |
14025 | expand_label (no_return_label); | |
8d08fdba MS |
14026 | } |
14027 | ||
078a76c8 MM |
14028 | /* We hard-wired immediate_size_expand to zero in |
14029 | start_function. Expand_function_end will decrement this | |
14030 | variable. So, we set the variable to one here, so that after | |
14031 | the decrement it will remain zero. */ | |
4ca7ba86 | 14032 | immediate_size_expand = 1; |
078a76c8 | 14033 | |
5566b478 MS |
14034 | /* Generate rtl for function exit. */ |
14035 | expand_function_end (input_filename, lineno, 1); | |
8d08fdba | 14036 | } |
558475f0 MM |
14037 | |
14038 | /* We have to save this value here in case | |
14039 | maybe_end_member_template_processing decides to pop all the | |
14040 | template parameters. */ | |
14041 | expand_p = !building_stmt_tree (); | |
68642fb6 | 14042 | |
558475f0 MM |
14043 | /* If we're saving up tree structure, tie off the function now. */ |
14044 | if (!expand_p) | |
6f80451c | 14045 | finish_stmt_tree (&DECL_SAVED_TREE (fndecl)); |
8d2733ca | 14046 | |
8d08fdba MS |
14047 | /* This must come after expand_function_end because cleanups might |
14048 | have declarations (from inline functions) that need to go into | |
14049 | this function's blocks. */ | |
b35d4555 MM |
14050 | if (doing_semantic_analysis_p ()) |
14051 | { | |
14052 | if (current_binding_level->parm_flag != 1) | |
14053 | my_friendly_abort (122); | |
14054 | poplevel (1, 0, 1); | |
14055 | } | |
8d08fdba | 14056 | |
a8f73d4b | 14057 | /* Remember that we were in class scope. */ |
db5ae43f | 14058 | if (current_class_name) |
a8f73d4b | 14059 | ctype = current_class_type; |
db5ae43f | 14060 | |
1caa11d3 MM |
14061 | /* Must mark the RESULT_DECL as being in this function. */ |
14062 | DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl; | |
14063 | ||
14064 | /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point | |
14065 | to the FUNCTION_DECL node itself. */ | |
14066 | BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl; | |
14067 | ||
59026e79 MM |
14068 | /* Save away current state, if appropriate. */ |
14069 | if (!expanding_p && !processing_template_decl) | |
14070 | save_function_data (fndecl); | |
14071 | ||
95fabfd3 MM |
14072 | /* If this function calls `setjmp' it cannot be inlined. When |
14073 | `longjmp' is called it is not guaranteed to restore the value of | |
14074 | local variables that have been modified since the call to | |
14075 | `setjmp'. So, if were to inline this function into some caller | |
14076 | `c', then when we `longjmp', we might not restore all variables | |
14077 | in `c'. (It might seem, at first blush, that there's no way for | |
14078 | this function to modify local variables in `c', but their | |
14079 | addresses may have been stored somewhere accessible to this | |
14080 | function.) */ | |
14081 | if (!expanding_p && !processing_template_decl && calls_setjmp_p (fndecl)) | |
14082 | DECL_UNINLINABLE (fndecl) = 1; | |
14083 | ||
558475f0 | 14084 | if (expand_p) |
5566b478 | 14085 | { |
a8f73d4b MM |
14086 | int returns_null; |
14087 | int returns_value; | |
75650646 | 14088 | |
5566b478 MS |
14089 | /* So we can tell if jump_optimize sets it to 1. */ |
14090 | can_reach_end = 0; | |
8d08fdba | 14091 | |
a8f73d4b MM |
14092 | /* Before we call rest_of_compilation (which will pop the |
14093 | CURRENT_FUNCTION), we must save these values. */ | |
14094 | returns_null = current_function_returns_null; | |
14095 | returns_value = current_function_returns_value; | |
14096 | ||
87e3dbc9 MM |
14097 | /* If this is a nested function (like a template instantiation |
14098 | that we're compiling in the midst of compiling something | |
14099 | else), push a new GC context. That will keep local variables | |
14100 | on the stack from being collected while we're doing the | |
14101 | compilation of this function. */ | |
14102 | if (function_depth > 1) | |
14103 | ggc_push_context (); | |
14104 | ||
6633d636 MS |
14105 | /* Run the optimizers and output the assembler code for this |
14106 | function. */ | |
6633d636 MS |
14107 | if (DECL_ARTIFICIAL (fndecl)) |
14108 | { | |
14109 | /* Do we really *want* to inline this synthesized method? */ | |
14110 | ||
14111 | int save_fif = flag_inline_functions; | |
14112 | flag_inline_functions = 1; | |
14113 | ||
14114 | /* Turn off DECL_INLINE for the moment so function_cannot_inline_p | |
14115 | will check our size. */ | |
14116 | DECL_INLINE (fndecl) = 0; | |
14117 | ||
14118 | rest_of_compilation (fndecl); | |
14119 | flag_inline_functions = save_fif; | |
14120 | } | |
14121 | else | |
14122 | rest_of_compilation (fndecl); | |
8d08fdba | 14123 | |
87e3dbc9 MM |
14124 | /* Undo the call to ggc_push_context above. */ |
14125 | if (function_depth > 1) | |
14126 | ggc_pop_context (); | |
14127 | ||
5566b478 MS |
14128 | if (DECL_SAVED_INSNS (fndecl) && ! TREE_ASM_WRITTEN (fndecl)) |
14129 | { | |
14130 | /* Set DECL_EXTERNAL so that assemble_external will be called as | |
14131 | necessary. We'll clear it again in finish_file. */ | |
14132 | if (! DECL_EXTERNAL (fndecl)) | |
14133 | DECL_NOT_REALLY_EXTERN (fndecl) = 1; | |
14134 | DECL_EXTERNAL (fndecl) = 1; | |
14135 | mark_inline_for_output (fndecl); | |
14136 | } | |
8926095f | 14137 | |
ae673f14 JM |
14138 | #if 0 |
14139 | /* Keep this code around in case we later want to control debug info | |
14140 | based on whether a type is "used". (jason 1999-11-11) */ | |
14141 | ||
d2e5ee5c MS |
14142 | if (ctype && TREE_ASM_WRITTEN (fndecl)) |
14143 | note_debug_info_needed (ctype); | |
ae673f14 | 14144 | #endif |
d2e5ee5c | 14145 | |
a8f73d4b | 14146 | returns_null |= can_reach_end; |
8d08fdba | 14147 | |
5566b478 MS |
14148 | /* Since we don't normally go through c_expand_return for constructors, |
14149 | this normally gets the wrong value. | |
14150 | Also, named return values have their return codes emitted after | |
14151 | NOTE_INSN_FUNCTION_END, confusing jump.c. */ | |
14152 | if (DECL_CONSTRUCTOR_P (fndecl) | |
14153 | || DECL_NAME (DECL_RESULT (fndecl)) != NULL_TREE) | |
a8f73d4b | 14154 | returns_null = 0; |
8d08fdba | 14155 | |
a8f73d4b | 14156 | if (TREE_THIS_VOLATILE (fndecl) && returns_null) |
8251199e | 14157 | cp_warning ("`noreturn' function `%D' does return", fndecl); |
5566b478 | 14158 | else if ((warn_return_type || pedantic) |
a8f73d4b | 14159 | && returns_null |
a1774733 | 14160 | && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE) |
5566b478 MS |
14161 | { |
14162 | /* If this function returns non-void and control can drop through, | |
14163 | complain. */ | |
8251199e | 14164 | cp_warning ("control reaches end of non-void function `%D'", fndecl); |
5566b478 MS |
14165 | } |
14166 | /* With just -W, complain only if function returns both with | |
14167 | and without a value. */ | |
a8f73d4b | 14168 | else if (extra_warnings && returns_value && returns_null) |
8251199e | 14169 | warning ("this function may return with or without a value"); |
8d08fdba | 14170 | } |
a8f73d4b MM |
14171 | else |
14172 | { | |
f7faca78 | 14173 | /* Clear out memory we no longer need. */ |
01d939e8 | 14174 | free_after_parsing (cfun); |
a8f73d4b | 14175 | /* Since we never call rest_of_compilation, we never clear |
01d939e8 BS |
14176 | CFUN. Do so explicitly. */ |
14177 | free_after_compilation (cfun); | |
14178 | cfun = NULL; | |
a8f73d4b MM |
14179 | } |
14180 | ||
14181 | /* If this is a in-class inline definition, we may have to pop the | |
14182 | bindings for the template parameters that we added in | |
14183 | maybe_begin_member_template_processing when start_function was | |
14184 | called. */ | |
14185 | if (inclass_inline) | |
14186 | maybe_end_member_template_processing (); | |
14187 | ||
14188 | /* Leave the scope of the class. */ | |
14189 | if (ctype) | |
14190 | pop_nested_class (); | |
5566b478 MS |
14191 | |
14192 | --function_depth; | |
8d08fdba | 14193 | |
d9b2d9da MM |
14194 | if (!DECL_SAVED_INSNS (fndecl) && !DECL_SAVED_FUNCTION_DATA (fndecl) |
14195 | && !(flag_inline_trees && DECL_INLINE (fndecl))) | |
8d08fdba | 14196 | { |
d2e5ee5c MS |
14197 | tree t; |
14198 | ||
8d08fdba MS |
14199 | /* Stop pointing to the local nodes about to be freed. */ |
14200 | /* But DECL_INITIAL must remain nonzero so we know this | |
14201 | was an actual function definition. */ | |
14202 | DECL_INITIAL (fndecl) = error_mark_node; | |
d2e5ee5c | 14203 | for (t = DECL_ARGUMENTS (fndecl); t; t = TREE_CHAIN (t)) |
de22184b | 14204 | DECL_RTL (t) = DECL_INCOMING_RTL (t) = NULL_RTX; |
8d08fdba MS |
14205 | } |
14206 | ||
e1cd6e56 | 14207 | if (DECL_STATIC_CONSTRUCTOR (fndecl)) |
e1b3e07d | 14208 | static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors); |
e1cd6e56 | 14209 | if (DECL_STATIC_DESTRUCTOR (fndecl)) |
e1b3e07d | 14210 | static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors); |
e1cd6e56 | 14211 | |
4d6abc1c | 14212 | /* Clean up. */ |
28cbf42c MS |
14213 | if (! nested) |
14214 | { | |
14215 | /* Let the error reporting routines know that we're outside a | |
14216 | function. For a nested function, this value is used in | |
14217 | pop_cp_function_context and then reset via pop_function_context. */ | |
14218 | current_function_decl = NULL_TREE; | |
a09ba2e0 MM |
14219 | /* We don't really care about obstacks, but the middle-end |
14220 | sometimes cares on what obstck things are located. */ | |
14221 | permanent_allocation (1); | |
28cbf42c | 14222 | } |
4d6abc1c MM |
14223 | |
14224 | return fndecl; | |
8d08fdba MS |
14225 | } |
14226 | \f | |
14227 | /* Create the FUNCTION_DECL for a function definition. | |
8d08fdba MS |
14228 | DECLSPECS and DECLARATOR are the parts of the declaration; |
14229 | they describe the return type and the name of the function, | |
14230 | but twisted together in a fashion that parallels the syntax of C. | |
14231 | ||
14232 | This function creates a binding context for the function body | |
14233 | as well as setting up the FUNCTION_DECL in current_function_decl. | |
14234 | ||
14235 | Returns a FUNCTION_DECL on success. | |
14236 | ||
14237 | If the DECLARATOR is not suitable for a function (it defines a datum | |
14238 | instead), we return 0, which tells yyparse to report a parse error. | |
14239 | ||
14240 | May return void_type_node indicating that this method is actually | |
14241 | a friend. See grokfield for more details. | |
14242 | ||
14243 | Came here with a `.pushlevel' . | |
14244 | ||
14245 | DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING | |
14246 | CHANGES TO CODE IN `grokfield'. */ | |
e92cc029 | 14247 | |
8d08fdba | 14248 | tree |
acf82af2 JM |
14249 | start_method (declspecs, declarator, attrlist) |
14250 | tree declarator, declspecs, attrlist; | |
8d08fdba | 14251 | { |
c11b6f21 | 14252 | tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0, |
acf82af2 | 14253 | attrlist); |
8d08fdba MS |
14254 | |
14255 | /* Something too ugly to handle. */ | |
14256 | if (fndecl == NULL_TREE) | |
14257 | return NULL_TREE; | |
14258 | ||
14259 | /* Pass friends other than inline friend functions back. */ | |
a1774733 | 14260 | if (fndecl == void_type_node) |
8d08fdba MS |
14261 | return fndecl; |
14262 | ||
14263 | if (TREE_CODE (fndecl) != FUNCTION_DECL) | |
14264 | /* Not a function, tell parser to report parse error. */ | |
14265 | return NULL_TREE; | |
14266 | ||
8d08fdba MS |
14267 | if (DECL_IN_AGGR_P (fndecl)) |
14268 | { | |
14269 | if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type) | |
14270 | { | |
68642fb6 | 14271 | if (DECL_CONTEXT (fndecl) |
2c73f9f5 | 14272 | && TREE_CODE( DECL_CONTEXT (fndecl)) != NAMESPACE_DECL) |
8251199e JM |
14273 | cp_error ("`%D' is already defined in class %s", fndecl, |
14274 | TYPE_NAME_STRING (DECL_CONTEXT (fndecl))); | |
8d08fdba MS |
14275 | } |
14276 | return void_type_node; | |
14277 | } | |
14278 | ||
f3400fe2 JM |
14279 | check_template_shadow (fndecl); |
14280 | ||
faae18ab MS |
14281 | DECL_THIS_INLINE (fndecl) = 1; |
14282 | ||
8926095f | 14283 | if (flag_default_inline) |
8d08fdba MS |
14284 | DECL_INLINE (fndecl) = 1; |
14285 | ||
36a117a5 MM |
14286 | /* We process method specializations in finish_struct_1. */ |
14287 | if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl)) | |
3ac3d9ea | 14288 | fndecl = push_template_decl (fndecl); |
a0a33927 | 14289 | |
8d08fdba MS |
14290 | /* We read in the parameters on the maybepermanent_obstack, |
14291 | but we won't be getting back to them until after we | |
14292 | may have clobbered them. So the call to preserve_data | |
14293 | will keep them safe. */ | |
14294 | preserve_data (); | |
14295 | ||
14296 | if (! DECL_FRIEND_P (fndecl)) | |
14297 | { | |
8d08fdba MS |
14298 | if (TREE_CHAIN (fndecl)) |
14299 | { | |
14300 | fndecl = copy_node (fndecl); | |
14301 | TREE_CHAIN (fndecl) = NULL_TREE; | |
14302 | } | |
14303 | ||
14304 | if (DECL_CONSTRUCTOR_P (fndecl)) | |
a0a33927 MS |
14305 | { |
14306 | if (! grok_ctor_properties (current_class_type, fndecl)) | |
14307 | return void_type_node; | |
14308 | } | |
8d08fdba MS |
14309 | else if (IDENTIFIER_OPNAME_P (DECL_NAME (fndecl))) |
14310 | grok_op_properties (fndecl, DECL_VIRTUAL_P (fndecl), 0); | |
14311 | } | |
14312 | ||
cd9f6678 | 14313 | cp_finish_decl (fndecl, NULL_TREE, NULL_TREE, 0); |
8d08fdba MS |
14314 | |
14315 | /* Make a place for the parms */ | |
14316 | pushlevel (0); | |
14317 | current_binding_level->parm_flag = 1; | |
68642fb6 | 14318 | |
8d08fdba MS |
14319 | DECL_IN_AGGR_P (fndecl) = 1; |
14320 | return fndecl; | |
14321 | } | |
14322 | ||
14323 | /* Go through the motions of finishing a function definition. | |
14324 | We don't compile this method until after the whole class has | |
14325 | been processed. | |
14326 | ||
14327 | FINISH_METHOD must return something that looks as though it | |
14328 | came from GROKFIELD (since we are defining a method, after all). | |
14329 | ||
14330 | This is called after parsing the body of the function definition. | |
14331 | STMTS is the chain of statements that makes up the function body. | |
14332 | ||
14333 | DECL is the ..._DECL that `start_method' provided. */ | |
14334 | ||
14335 | tree | |
14336 | finish_method (decl) | |
14337 | tree decl; | |
14338 | { | |
14339 | register tree fndecl = decl; | |
14340 | tree old_initial; | |
8d08fdba MS |
14341 | |
14342 | register tree link; | |
14343 | ||
a1774733 | 14344 | if (decl == void_type_node) |
8d08fdba MS |
14345 | return decl; |
14346 | ||
14347 | old_initial = DECL_INITIAL (fndecl); | |
14348 | ||
14349 | /* Undo the level for the parms (from start_method). | |
14350 | This is like poplevel, but it causes nothing to be | |
14351 | saved. Saving information here confuses symbol-table | |
14352 | output routines. Besides, this information will | |
14353 | be correctly output when this method is actually | |
14354 | compiled. */ | |
14355 | ||
14356 | /* Clear out the meanings of the local variables of this level; | |
14357 | also record in each decl which block it belongs to. */ | |
14358 | ||
14359 | for (link = current_binding_level->names; link; link = TREE_CHAIN (link)) | |
14360 | { | |
14361 | if (DECL_NAME (link) != NULL_TREE) | |
d8f8dca1 | 14362 | pop_binding (DECL_NAME (link), link); |
8d08fdba MS |
14363 | my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163); |
14364 | DECL_CONTEXT (link) = NULL_TREE; | |
14365 | } | |
14366 | ||
8d08fdba MS |
14367 | GNU_xref_end_scope ((HOST_WIDE_INT) current_binding_level, |
14368 | (HOST_WIDE_INT) current_binding_level->level_chain, | |
14369 | current_binding_level->parm_flag, | |
5566b478 | 14370 | current_binding_level->keep); |
8d08fdba MS |
14371 | |
14372 | poplevel (0, 0, 0); | |
14373 | ||
14374 | DECL_INITIAL (fndecl) = old_initial; | |
14375 | ||
14376 | /* We used to check if the context of FNDECL was different from | |
14377 | current_class_type as another way to get inside here. This didn't work | |
14378 | for String.cc in libg++. */ | |
14379 | if (DECL_FRIEND_P (fndecl)) | |
14380 | { | |
14381 | CLASSTYPE_INLINE_FRIENDS (current_class_type) | |
14382 | = tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type)); | |
14383 | decl = void_type_node; | |
14384 | } | |
14385 | ||
14386 | return decl; | |
14387 | } | |
14388 | \f | |
14389 | /* Called when a new struct TYPE is defined. | |
14390 | If this structure or union completes the type of any previous | |
14391 | variable declaration, lay it out and output its rtl. */ | |
14392 | ||
14393 | void | |
14394 | hack_incomplete_structures (type) | |
14395 | tree type; | |
14396 | { | |
f30432d7 | 14397 | tree *list; |
70adf8a9 | 14398 | struct binding_level *level; |
8d08fdba MS |
14399 | |
14400 | if (!type) /* Don't do this for class templates. */ | |
14401 | return; | |
14402 | ||
70adf8a9 JM |
14403 | if (namespace_bindings_p ()) |
14404 | { | |
14405 | level = 0; | |
14406 | list = &namespace_scope_incomplete; | |
14407 | } | |
14408 | else | |
14409 | { | |
14410 | level = innermost_nonclass_level (); | |
14411 | list = &level->incomplete; | |
14412 | } | |
14413 | ||
14414 | while (1) | |
14415 | { | |
14416 | while (*list) | |
14417 | { | |
14418 | tree decl = TREE_VALUE (*list); | |
14419 | if ((decl && TREE_TYPE (decl) == type) | |
14420 | || (TREE_TYPE (decl) | |
14421 | && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE | |
14422 | && TREE_TYPE (TREE_TYPE (decl)) == type)) | |
f30432d7 | 14423 | { |
70adf8a9 JM |
14424 | int toplevel = toplevel_bindings_p (); |
14425 | if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE | |
14426 | && TREE_TYPE (TREE_TYPE (decl)) == type) | |
14427 | layout_type (TREE_TYPE (decl)); | |
14428 | layout_decl (decl, 0); | |
14429 | rest_of_decl_compilation (decl, NULL_PTR, toplevel, 0); | |
14430 | if (! toplevel) | |
14431 | { | |
14432 | tree cleanup; | |
14433 | expand_decl (decl); | |
14434 | cleanup = maybe_build_cleanup (decl); | |
14435 | expand_decl_init (decl); | |
14436 | if (! expand_decl_cleanup (decl, cleanup)) | |
14437 | cp_error ("parser lost in parsing declaration of `%D'", | |
14438 | decl); | |
14439 | } | |
14440 | *list = TREE_CHAIN (*list); | |
f30432d7 | 14441 | } |
70adf8a9 JM |
14442 | else |
14443 | list = &TREE_CHAIN (*list); | |
14444 | } | |
14445 | ||
14446 | /* Keep looking through artificial binding levels generated | |
14447 | for local variables. */ | |
14448 | if (level && level->keep == 2) | |
14449 | { | |
14450 | level = level->level_chain; | |
14451 | list = &level->incomplete; | |
f30432d7 MS |
14452 | } |
14453 | else | |
70adf8a9 | 14454 | break; |
f30432d7 | 14455 | } |
8d08fdba MS |
14456 | } |
14457 | ||
8d08fdba | 14458 | /* If DECL is of a type which needs a cleanup, build that cleanup here. |
c73964b2 | 14459 | See build_delete for information about AUTO_DELETE. |
8d08fdba MS |
14460 | |
14461 | Don't build these on the momentary obstack; they must live | |
14462 | the life of the binding contour. */ | |
e92cc029 | 14463 | |
c73964b2 MS |
14464 | static tree |
14465 | maybe_build_cleanup_1 (decl, auto_delete) | |
14466 | tree decl, auto_delete; | |
8d08fdba MS |
14467 | { |
14468 | tree type = TREE_TYPE (decl); | |
834c6dff | 14469 | if (type != error_mark_node && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) |
8d08fdba | 14470 | { |
80048418 | 14471 | int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR; |
8d08fdba | 14472 | tree rval; |
8d08fdba | 14473 | |
8d08fdba MS |
14474 | if (TREE_CODE (type) == ARRAY_TYPE) |
14475 | rval = decl; | |
14476 | else | |
14477 | { | |
14478 | mark_addressable (decl); | |
14479 | rval = build_unary_op (ADDR_EXPR, decl, 0); | |
14480 | } | |
14481 | ||
14482 | /* Optimize for space over speed here. */ | |
14483 | if (! TYPE_USES_VIRTUAL_BASECLASSES (type) | |
14484 | || flag_expensive_optimizations) | |
14485 | flags |= LOOKUP_NONVIRTUAL; | |
14486 | ||
c73964b2 | 14487 | rval = build_delete (TREE_TYPE (rval), rval, auto_delete, flags, 0); |
8d08fdba MS |
14488 | |
14489 | if (TYPE_USES_VIRTUAL_BASECLASSES (type) | |
14490 | && ! TYPE_HAS_DESTRUCTOR (type)) | |
e1b3e07d | 14491 | rval = build_compound_expr (tree_cons (NULL_TREE, rval, |
051e6fd7 | 14492 | build_tree_list (NULL_TREE, build_vbase_delete (type, decl)))); |
8d08fdba | 14493 | |
8d08fdba MS |
14494 | return rval; |
14495 | } | |
14496 | return 0; | |
14497 | } | |
c73964b2 MS |
14498 | |
14499 | /* If DECL is of a type which needs a cleanup, build that cleanup | |
14500 | here. The cleanup does free the storage with a call to delete. */ | |
14501 | ||
14502 | tree | |
14503 | maybe_build_cleanup_and_delete (decl) | |
14504 | tree decl; | |
14505 | { | |
14506 | return maybe_build_cleanup_1 (decl, integer_three_node); | |
14507 | } | |
14508 | ||
14509 | /* If DECL is of a type which needs a cleanup, build that cleanup | |
14510 | here. The cleanup does not free the storage with a call a delete. */ | |
14511 | ||
14512 | tree | |
14513 | maybe_build_cleanup (decl) | |
14514 | tree decl; | |
14515 | { | |
14516 | return maybe_build_cleanup_1 (decl, integer_two_node); | |
14517 | } | |
8d08fdba MS |
14518 | \f |
14519 | /* Expand a C++ expression at the statement level. | |
14520 | This is needed to ferret out nodes which have UNKNOWN_TYPE. | |
14521 | The C++ type checker should get all of these out when | |
14522 | expressions are combined with other, type-providing, expressions, | |
14523 | leaving only orphan expressions, such as: | |
14524 | ||
e92cc029 | 14525 | &class::bar; / / takes its address, but does nothing with it. */ |
8d08fdba | 14526 | |
8d08fdba MS |
14527 | void |
14528 | cplus_expand_expr_stmt (exp) | |
14529 | tree exp; | |
14530 | { | |
02cac427 NS |
14531 | if (stmts_are_full_exprs_p) |
14532 | exp = convert_to_void (exp, "statement"); | |
68642fb6 | 14533 | |
63c68bb7 | 14534 | #if 0 |
66543169 NS |
14535 | /* We should do this eventually, but right now this causes regex.o from |
14536 | libg++ to miscompile, and tString to core dump. */ | |
14537 | exp = build1 (CLEANUP_POINT_EXPR, TREE_TYPE (exp), exp); | |
63c68bb7 | 14538 | #endif |
b3e401c2 | 14539 | |
66543169 NS |
14540 | /* If we don't do this, we end up down inside expand_expr |
14541 | trying to do TYPE_MODE on the ERROR_MARK, and really | |
14542 | go outside the bounds of the type. */ | |
14543 | if (exp != error_mark_node) | |
364460b6 | 14544 | expand_expr_stmt (exp); |
8d08fdba MS |
14545 | } |
14546 | ||
558475f0 | 14547 | /* When a stmt has been parsed, this function is called. */ |
8d08fdba MS |
14548 | |
14549 | void | |
14550 | finish_stmt () | |
14551 | { | |
558475f0 MM |
14552 | /* Always assume this statement was not an expression statement. If |
14553 | it actually was an expression statement, its our callers | |
14554 | responsibility to fix this up. */ | |
14555 | last_expr_type = NULL_TREE; | |
8d08fdba MS |
14556 | } |
14557 | ||
3afb32a4 MM |
14558 | /* DECL was originally constructed as a non-static member function, |
14559 | but turned out to be static. Update it accordingly. */ | |
700f8a87 | 14560 | |
8857f91e | 14561 | void |
3afb32a4 MM |
14562 | revert_static_member_fn (decl) |
14563 | tree decl; | |
8d08fdba | 14564 | { |
700f8a87 | 14565 | tree tmp; |
3afb32a4 MM |
14566 | tree function = TREE_TYPE (decl); |
14567 | tree args = TYPE_ARG_TYPES (function); | |
8d08fdba | 14568 | |
68642fb6 | 14569 | if (CP_TYPE_QUALS (TREE_TYPE (TREE_VALUE (args))) |
91063b51 | 14570 | != TYPE_UNQUALIFIED) |
68642fb6 | 14571 | cp_error ("static member function `%#D' declared with type qualifiers", |
91063b51 | 14572 | *decl); |
f30432d7 | 14573 | |
700f8a87 MS |
14574 | args = TREE_CHAIN (args); |
14575 | tmp = build_function_type (TREE_TYPE (function), args); | |
91063b51 | 14576 | tmp = build_qualified_type (tmp, CP_TYPE_QUALS (function)); |
f30432d7 | 14577 | tmp = build_exception_variant (tmp, |
8d08fdba | 14578 | TYPE_RAISES_EXCEPTIONS (function)); |
3afb32a4 MM |
14579 | TREE_TYPE (decl) = tmp; |
14580 | if (DECL_ARGUMENTS (decl)) | |
14581 | DECL_ARGUMENTS (decl) = TREE_CHAIN (DECL_ARGUMENTS (decl)); | |
14582 | DECL_STATIC_FUNCTION_P (decl) = 1; | |
8d08fdba | 14583 | } |
a4443a08 | 14584 | |
68642fb6 UD |
14585 | /* Initialize the variables used during compilation of a C++ |
14586 | function. */ | |
db5ae43f | 14587 | |
99dccabc MM |
14588 | static void |
14589 | push_cp_function_context (f) | |
14590 | struct function *f; | |
14591 | { | |
68642fb6 UD |
14592 | struct language_function *p |
14593 | = ((struct language_function *) | |
be91309c | 14594 | xcalloc (1, sizeof (struct language_function))); |
99dccabc | 14595 | f->language = p; |
db5ae43f | 14596 | |
b35d4555 MM |
14597 | /* It takes an explicit call to expand_body to generate RTL for a |
14598 | function. */ | |
14599 | expanding_p = 0; | |
f1dedc31 MM |
14600 | |
14601 | /* Whenever we start a new function, we destroy temporaries in the | |
14602 | usual way. */ | |
14603 | stmts_are_full_exprs_p = 1; | |
db5ae43f MS |
14604 | } |
14605 | ||
a8f73d4b MM |
14606 | /* Free the language-specific parts of F, now that we've finished |
14607 | compiling the function. */ | |
db5ae43f | 14608 | |
99dccabc MM |
14609 | static void |
14610 | pop_cp_function_context (f) | |
14611 | struct function *f; | |
db5ae43f | 14612 | { |
d658cd4c MM |
14613 | if (f->language) |
14614 | free (f->language); | |
99dccabc | 14615 | f->language = 0; |
db5ae43f | 14616 | } |
ebfc180f | 14617 | |
59026e79 | 14618 | /* Mark P for GC. */ |
4519c0a8 | 14619 | |
59026e79 MM |
14620 | static void |
14621 | mark_lang_function (p) | |
14622 | struct language_function *p; | |
14623 | { | |
87e3dbc9 MM |
14624 | if (!p) |
14625 | return; | |
14626 | ||
4519c0a8 MM |
14627 | ggc_mark_tree (p->x_named_labels); |
14628 | ggc_mark_tree (p->x_ctor_label); | |
14629 | ggc_mark_tree (p->x_dtor_label); | |
14630 | ggc_mark_tree (p->x_base_init_list); | |
14631 | ggc_mark_tree (p->x_member_init_list); | |
4519c0a8 MM |
14632 | ggc_mark_tree (p->x_current_class_ptr); |
14633 | ggc_mark_tree (p->x_current_class_ref); | |
b35d4555 MM |
14634 | ggc_mark_tree (p->x_eh_spec_try_block); |
14635 | ggc_mark_tree (p->x_scope_stmt_stack); | |
4519c0a8 | 14636 | |
4519c0a8 MM |
14637 | ggc_mark_rtx (p->x_result_rtx); |
14638 | ||
6f80451c | 14639 | mark_stmt_tree (&p->x_stmt_tree); |
a8f73d4b | 14640 | mark_binding_level (&p->bindings); |
4519c0a8 MM |
14641 | } |
14642 | ||
59026e79 MM |
14643 | /* Mark the language-specific data in F for GC. */ |
14644 | ||
a09ba2e0 | 14645 | static void |
59026e79 MM |
14646 | mark_cp_function_context (f) |
14647 | struct function *f; | |
14648 | { | |
14649 | mark_lang_function (f->language); | |
14650 | } | |
4519c0a8 | 14651 | |
5566b478 MS |
14652 | int |
14653 | in_function_p () | |
14654 | { | |
14655 | return function_depth != 0; | |
14656 | } | |
4519c0a8 MM |
14657 | |
14658 | ||
14659 | void | |
b4b8bee7 MM |
14660 | lang_mark_false_label_stack (l) |
14661 | struct label_node *l; | |
4519c0a8 MM |
14662 | { |
14663 | /* C++ doesn't use false_label_stack. It better be NULL. */ | |
87e3dbc9 | 14664 | my_friendly_assert (l == NULL, 19990904); |
4519c0a8 MM |
14665 | } |
14666 | ||
14667 | void | |
14668 | lang_mark_tree (t) | |
14669 | tree t; | |
14670 | { | |
14671 | enum tree_code code = TREE_CODE (t); | |
14672 | if (code == IDENTIFIER_NODE) | |
14673 | { | |
14674 | struct lang_identifier *li = (struct lang_identifier *) t; | |
14675 | struct lang_id2 *li2 = li->x; | |
14676 | ggc_mark_tree (li->namespace_bindings); | |
a8f73d4b | 14677 | ggc_mark_tree (li->bindings); |
4519c0a8 MM |
14678 | ggc_mark_tree (li->class_value); |
14679 | ggc_mark_tree (li->class_template_info); | |
14680 | ||
14681 | if (li2) | |
14682 | { | |
14683 | ggc_mark_tree (li2->label_value); | |
14684 | ggc_mark_tree (li2->implicit_decl); | |
14685 | ggc_mark_tree (li2->error_locus); | |
14686 | } | |
14687 | } | |
87e3dbc9 MM |
14688 | else if (code == CPLUS_BINDING) |
14689 | { | |
14690 | if (BINDING_HAS_LEVEL_P (t)) | |
14691 | mark_binding_level (&BINDING_LEVEL (t)); | |
14692 | else | |
14693 | ggc_mark_tree (BINDING_SCOPE (t)); | |
14694 | ggc_mark_tree (BINDING_VALUE (t)); | |
14695 | } | |
14696 | else if (code == OVERLOAD) | |
14697 | ggc_mark_tree (OVL_FUNCTION (t)); | |
14698 | else if (code == TEMPLATE_PARM_INDEX) | |
14699 | ggc_mark_tree (TEMPLATE_PARM_DECL (t)); | |
4519c0a8 MM |
14700 | else if (TREE_CODE_CLASS (code) == 'd') |
14701 | { | |
14702 | struct lang_decl *ld = DECL_LANG_SPECIFIC (t); | |
14703 | ||
14704 | if (ld) | |
14705 | { | |
d60f72ae | 14706 | ggc_mark (ld); |
af3b4e59 MM |
14707 | if (!DECL_GLOBAL_CTOR_P (t) && !DECL_GLOBAL_DTOR_P (t)) |
14708 | ggc_mark_tree (ld->decl_flags.u2.access); | |
4519c0a8 MM |
14709 | ggc_mark_tree (ld->decl_flags.context); |
14710 | if (TREE_CODE (t) != NAMESPACE_DECL) | |
14711 | ggc_mark_tree (ld->decl_flags.u.template_info); | |
16bb3387 JM |
14712 | else |
14713 | mark_binding_level (&NAMESPACE_LEVEL (t)); | |
4519c0a8 MM |
14714 | if (CAN_HAVE_FULL_LANG_DECL_P (t)) |
14715 | { | |
14716 | ggc_mark_tree (ld->main_decl_variant); | |
14717 | ggc_mark_tree (ld->befriending_classes); | |
14718 | ggc_mark_tree (ld->saved_tree); | |
14719 | if (TREE_CODE (t) == TYPE_DECL) | |
14720 | ggc_mark_tree (ld->u.sorted_fields); | |
59026e79 MM |
14721 | else if (TREE_CODE (t) == FUNCTION_DECL |
14722 | && !DECL_PENDING_INLINE_P (t)) | |
14723 | mark_lang_function (DECL_SAVED_FUNCTION_DATA (t)); | |
4519c0a8 MM |
14724 | } |
14725 | } | |
14726 | } | |
14727 | else if (TREE_CODE_CLASS (code) == 't') | |
14728 | { | |
14729 | struct lang_type *lt = TYPE_LANG_SPECIFIC (t); | |
14730 | ||
68642fb6 | 14731 | if (lt && !(TREE_CODE (t) == POINTER_TYPE |
87e3dbc9 | 14732 | && TREE_CODE (TREE_TYPE (t)) == METHOD_TYPE)) |
4519c0a8 | 14733 | { |
c27db0df | 14734 | ggc_mark (lt); |
4519c0a8 MM |
14735 | ggc_mark_tree (lt->vfields); |
14736 | ggc_mark_tree (lt->vbases); | |
14737 | ggc_mark_tree (lt->tags); | |
14738 | ggc_mark_tree (lt->search_slot); | |
14739 | ggc_mark_tree (lt->size); | |
fee7654e | 14740 | ggc_mark_tree (lt->pure_virtuals); |
4519c0a8 MM |
14741 | ggc_mark_tree (lt->friend_classes); |
14742 | ggc_mark_tree (lt->rtti); | |
14743 | ggc_mark_tree (lt->methods); | |
14744 | ggc_mark_tree (lt->template_info); | |
87e3dbc9 | 14745 | ggc_mark_tree (lt->befriending_classes); |
4519c0a8 | 14746 | } |
87e3dbc9 MM |
14747 | else if (lt) |
14748 | /* In the case of pointer-to-member function types, the | |
14749 | TYPE_LANG_SPECIFIC is really just a tree. */ | |
14750 | ggc_mark_tree ((tree) lt); | |
4519c0a8 MM |
14751 | } |
14752 | } |