]>
Commit | Line | Data |
---|---|---|
8d08fdba MS |
1 | /* Report error messages, build initializers, and perform |
2 | some front-end optimizations for C++ compiler. | |
d6a8bdff | 3 | Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, |
94e6e4c4 | 4 | 1999, 2000, 2001, 2002 Free Software Foundation, Inc. |
8d08fdba MS |
5 | Hacked by Michael Tiemann (tiemann@cygnus.com) |
6 | ||
f5adbb8d | 7 | This file is part of GCC. |
8d08fdba | 8 | |
f5adbb8d | 9 | GCC is free software; you can redistribute it and/or modify |
8d08fdba MS |
10 | it under the terms of the GNU General Public License as published by |
11 | the Free Software Foundation; either version 2, or (at your option) | |
12 | any later version. | |
13 | ||
f5adbb8d | 14 | GCC is distributed in the hope that it will be useful, |
8d08fdba MS |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
f5adbb8d | 20 | along with GCC; see the file COPYING. If not, write to |
6bc06b8f RK |
21 | the Free Software Foundation, 59 Temple Place - Suite 330, |
22 | Boston, MA 02111-1307, USA. */ | |
8d08fdba MS |
23 | |
24 | ||
25 | /* This file is part of the C++ front end. | |
26 | It contains routines to build C++ expressions given their operands, | |
27 | including computing the types of the result, C and C++ specific error | |
28 | checks, and some optimization. | |
29 | ||
30 | There are also routines to build RETURN_STMT nodes and CASE_STMT nodes, | |
31 | and to process initializations in declarations (since they work | |
32 | like a strange sort of assignment). */ | |
33 | ||
34 | #include "config.h" | |
8d052bc7 | 35 | #include "system.h" |
4977bab6 ZW |
36 | #include "coretypes.h" |
37 | #include "tm.h" | |
8d08fdba MS |
38 | #include "tree.h" |
39 | #include "cp-tree.h" | |
40 | #include "flags.h" | |
12027a89 | 41 | #include "toplev.h" |
71144a65 | 42 | #include "output.h" |
2a2b2d43 | 43 | #include "diagnostic.h" |
8d08fdba | 44 | |
158991b7 | 45 | static tree process_init_constructor PARAMS ((tree, tree, tree *)); |
8d08fdba | 46 | |
8d08fdba MS |
47 | /* Print an error message stemming from an attempt to use |
48 | BASETYPE as a base class for TYPE. */ | |
e92cc029 | 49 | |
8d08fdba MS |
50 | tree |
51 | error_not_base_type (basetype, type) | |
52 | tree basetype, type; | |
53 | { | |
54 | if (TREE_CODE (basetype) == FUNCTION_DECL) | |
4f1c5b7d | 55 | basetype = DECL_CONTEXT (basetype); |
33bd39a2 | 56 | error ("type `%T' is not a base type for type `%T'", basetype, type); |
8d08fdba MS |
57 | return error_mark_node; |
58 | } | |
59 | ||
60 | tree | |
2db1ab2d NS |
61 | binfo_or_else (base, type) |
62 | tree base, type; | |
8d08fdba | 63 | { |
2db1ab2d NS |
64 | tree binfo = lookup_base (type, base, ba_ignore, NULL); |
65 | ||
66 | if (binfo == error_mark_node) | |
67 | return NULL_TREE; | |
68 | else if (!binfo) | |
69 | error_not_base_type (base, type); | |
70 | return binfo; | |
8d08fdba MS |
71 | } |
72 | ||
8d08fdba MS |
73 | /* According to ARM $7.1.6, "A `const' object may be initialized, but its |
74 | value may not be changed thereafter. Thus, we emit hard errors for these, | |
75 | rather than just pedwarns. If `SOFT' is 1, then we just pedwarn. (For | |
76 | example, conversions to references.) */ | |
e92cc029 | 77 | |
8d08fdba MS |
78 | void |
79 | readonly_error (arg, string, soft) | |
80 | tree arg; | |
d8e178a0 | 81 | const char *string; |
8d08fdba MS |
82 | int soft; |
83 | { | |
d8e178a0 | 84 | const char *fmt; |
158991b7 | 85 | void (*fn) PARAMS ((const char *, ...)); |
8d08fdba MS |
86 | |
87 | if (soft) | |
33bd39a2 | 88 | fn = pedwarn; |
8d08fdba | 89 | else |
33bd39a2 | 90 | fn = error; |
8d08fdba MS |
91 | |
92 | if (TREE_CODE (arg) == COMPONENT_REF) | |
93 | { | |
94 | if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0)))) | |
7e4d7898 | 95 | fmt = "%s of data-member `%D' in read-only structure"; |
8d08fdba | 96 | else |
7e4d7898 | 97 | fmt = "%s of read-only data-member `%D'"; |
8251199e | 98 | (*fn) (fmt, string, TREE_OPERAND (arg, 1)); |
8d08fdba MS |
99 | } |
100 | else if (TREE_CODE (arg) == VAR_DECL) | |
101 | { | |
102 | if (DECL_LANG_SPECIFIC (arg) | |
103 | && DECL_IN_AGGR_P (arg) | |
104 | && !TREE_STATIC (arg)) | |
8251199e | 105 | fmt = "%s of constant field `%D'"; |
8d08fdba | 106 | else |
8251199e JM |
107 | fmt = "%s of read-only variable `%D'"; |
108 | (*fn) (fmt, string, arg); | |
8d08fdba MS |
109 | } |
110 | else if (TREE_CODE (arg) == PARM_DECL) | |
8251199e | 111 | (*fn) ("%s of read-only parameter `%D'", string, arg); |
8d08fdba MS |
112 | else if (TREE_CODE (arg) == INDIRECT_REF |
113 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REFERENCE_TYPE | |
114 | && (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL | |
115 | || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL)) | |
8251199e | 116 | (*fn) ("%s of read-only reference `%D'", string, TREE_OPERAND (arg, 0)); |
8d08fdba | 117 | else if (TREE_CODE (arg) == RESULT_DECL) |
8251199e | 118 | (*fn) ("%s of read-only named return value `%D'", string, arg); |
69851283 MM |
119 | else if (TREE_CODE (arg) == FUNCTION_DECL) |
120 | (*fn) ("%s of function `%D'", string, arg); | |
121 | else | |
8251199e | 122 | (*fn) ("%s of read-only location", string); |
8d08fdba MS |
123 | } |
124 | ||
a7a64a77 MM |
125 | /* If TYPE has abstract virtual functions, issue an error about trying |
126 | to create an object of that type. DECL is the object declared, or | |
127 | NULL_TREE if the declaration is unavailable. Returns 1 if an error | |
128 | occurred; zero if all was well. */ | |
e92cc029 | 129 | |
a7a64a77 | 130 | int |
8d08fdba MS |
131 | abstract_virtuals_error (decl, type) |
132 | tree decl; | |
133 | tree type; | |
134 | { | |
a7a64a77 | 135 | tree u; |
4a67c9e9 MH |
136 | tree tu; |
137 | ||
fee7654e | 138 | if (!CLASS_TYPE_P (type) || !CLASSTYPE_PURE_VIRTUALS (type)) |
a7a64a77 MM |
139 | return 0; |
140 | ||
e60505a5 NS |
141 | if (!TYPE_SIZE (type)) |
142 | /* TYPE is being defined, and during that time | |
143 | CLASSTYPE_PURE_VIRTUALS holds the inline friends. */ | |
144 | return 0; | |
145 | ||
fee7654e | 146 | u = CLASSTYPE_PURE_VIRTUALS (type); |
8d08fdba MS |
147 | if (decl) |
148 | { | |
149 | if (TREE_CODE (decl) == RESULT_DECL) | |
a7a64a77 | 150 | return 0; |
8d08fdba MS |
151 | |
152 | if (TREE_CODE (decl) == VAR_DECL) | |
33bd39a2 | 153 | error ("cannot declare variable `%D' to be of type `%T'", |
8d08fdba MS |
154 | decl, type); |
155 | else if (TREE_CODE (decl) == PARM_DECL) | |
33bd39a2 | 156 | error ("cannot declare parameter `%D' to be of type `%T'", |
8d08fdba MS |
157 | decl, type); |
158 | else if (TREE_CODE (decl) == FIELD_DECL) | |
33bd39a2 | 159 | error ("cannot declare field `%D' to be of type `%T'", |
8d08fdba MS |
160 | decl, type); |
161 | else if (TREE_CODE (decl) == FUNCTION_DECL | |
162 | && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE) | |
33bd39a2 | 163 | error ("invalid return type for member function `%#D'", decl); |
8d08fdba | 164 | else if (TREE_CODE (decl) == FUNCTION_DECL) |
33bd39a2 | 165 | error ("invalid return type for function `%#D'", decl); |
8d08fdba | 166 | } |
4a67c9e9 | 167 | else |
33bd39a2 | 168 | error ("cannot allocate an object of type `%T'", type); |
4a67c9e9 | 169 | |
8d08fdba MS |
170 | /* Only go through this once. */ |
171 | if (TREE_PURPOSE (u) == NULL_TREE) | |
172 | { | |
8d08fdba | 173 | TREE_PURPOSE (u) = error_mark_node; |
4a67c9e9 | 174 | |
2e44948d | 175 | error (" because the following virtual functions are abstract:"); |
8ebeee52 | 176 | for (tu = u; tu; tu = TREE_CHAIN (tu)) |
83f660b7 | 177 | cp_error_at ("\t%#D", TREE_VALUE (tu)); |
8d08fdba | 178 | } |
4a67c9e9 | 179 | else |
33bd39a2 | 180 | error (" since type `%T' has abstract virtual functions", type); |
a7a64a77 MM |
181 | |
182 | return 1; | |
8d08fdba MS |
183 | } |
184 | ||
8d08fdba MS |
185 | /* Print an error message for invalid use of an incomplete type. |
186 | VALUE is the expression that was used (or 0 if that isn't known) | |
5aa3396c JM |
187 | and TYPE is the type that was invalid. DIAG_TYPE indicates the |
188 | type of diagnostic: 0 for an error, 1 for a warning, 2 for a | |
189 | pedwarn. */ | |
8d08fdba MS |
190 | |
191 | void | |
5aa3396c | 192 | cxx_incomplete_type_diagnostic (value, type, diag_type) |
8d08fdba MS |
193 | tree value; |
194 | tree type; | |
5aa3396c | 195 | int diag_type; |
8d08fdba | 196 | { |
146c8d60 | 197 | int decl = 0; |
23b4deba AO |
198 | void (*p_msg) PARAMS ((const char *, ...)); |
199 | void (*p_msg_at) PARAMS ((const char *, ...)); | |
200 | ||
5aa3396c | 201 | if (diag_type == 1) |
23b4deba AO |
202 | { |
203 | p_msg = warning; | |
204 | p_msg_at = cp_warning_at; | |
205 | } | |
5aa3396c JM |
206 | else if (diag_type == 2) |
207 | { | |
208 | p_msg = pedwarn; | |
209 | p_msg_at = cp_pedwarn_at; | |
210 | } | |
23b4deba AO |
211 | else |
212 | { | |
213 | p_msg = error; | |
214 | p_msg_at = cp_error_at; | |
215 | } | |
146c8d60 | 216 | |
8d08fdba MS |
217 | /* Avoid duplicate error message. */ |
218 | if (TREE_CODE (type) == ERROR_MARK) | |
219 | return; | |
220 | ||
146c8d60 | 221 | if (value != 0 && (TREE_CODE (value) == VAR_DECL |
17f29637 KL |
222 | || TREE_CODE (value) == PARM_DECL |
223 | || TREE_CODE (value) == FIELD_DECL)) | |
146c8d60 | 224 | { |
23b4deba | 225 | (*p_msg_at) ("`%D' has incomplete type", value); |
146c8d60 NS |
226 | decl = 1; |
227 | } | |
66543169 NS |
228 | retry: |
229 | /* We must print an error message. Be clever about what it says. */ | |
230 | ||
231 | switch (TREE_CODE (type)) | |
8d08fdba | 232 | { |
66543169 NS |
233 | case RECORD_TYPE: |
234 | case UNION_TYPE: | |
235 | case ENUMERAL_TYPE: | |
146c8d60 | 236 | if (!decl) |
23b4deba | 237 | (*p_msg) ("invalid use of undefined type `%#T'", type); |
17f29637 KL |
238 | if (!TYPE_TEMPLATE_INFO (type)) |
239 | (*p_msg_at) ("forward declaration of `%#T'", type); | |
240 | else | |
04c4491d | 241 | (*p_msg_at) ("declaration of `%#T'", type); |
66543169 NS |
242 | break; |
243 | ||
244 | case VOID_TYPE: | |
23b4deba | 245 | (*p_msg) ("invalid use of `%T'", type); |
66543169 NS |
246 | break; |
247 | ||
248 | case ARRAY_TYPE: | |
249 | if (TYPE_DOMAIN (type)) | |
250 | { | |
251 | type = TREE_TYPE (type); | |
252 | goto retry; | |
253 | } | |
23b4deba | 254 | (*p_msg) ("invalid use of array with unspecified bounds"); |
66543169 NS |
255 | break; |
256 | ||
257 | case OFFSET_TYPE: | |
258 | bad_member: | |
23b4deba | 259 | (*p_msg) ("invalid use of member (did you forget the `&' ?)"); |
66543169 NS |
260 | break; |
261 | ||
262 | case TEMPLATE_TYPE_PARM: | |
23b4deba | 263 | (*p_msg) ("invalid use of template type parameter"); |
66543169 NS |
264 | break; |
265 | ||
266 | case UNKNOWN_TYPE: | |
267 | if (value && TREE_CODE (value) == COMPONENT_REF) | |
268 | goto bad_member; | |
269 | else if (value && TREE_CODE (value) == ADDR_EXPR) | |
23b4deba | 270 | (*p_msg) ("address of overloaded function with no contextual type information"); |
66543169 | 271 | else if (value && TREE_CODE (value) == OVERLOAD) |
23b4deba | 272 | (*p_msg) ("overloaded function with no contextual type information"); |
66543169 | 273 | else |
23b4deba | 274 | (*p_msg) ("insufficient contextual information to determine type"); |
66543169 NS |
275 | break; |
276 | ||
277 | default: | |
a98facb0 | 278 | abort (); |
8d08fdba MS |
279 | } |
280 | } | |
281 | ||
23b4deba AO |
282 | /* Backward-compatibility interface to incomplete_type_diagnostic; |
283 | required by ../tree.c. */ | |
284 | #undef cxx_incomplete_type_error | |
285 | void | |
286 | cxx_incomplete_type_error (value, type) | |
287 | tree value; | |
288 | tree type; | |
289 | { | |
290 | cxx_incomplete_type_diagnostic (value, type, 0); | |
291 | } | |
292 | ||
8d08fdba | 293 | \f |
8d08fdba MS |
294 | /* Perform appropriate conversions on the initial value of a variable, |
295 | store it in the declaration DECL, | |
296 | and print any error messages that are appropriate. | |
297 | If the init is invalid, store an ERROR_MARK. | |
298 | ||
299 | C++: Note that INIT might be a TREE_LIST, which would mean that it is | |
300 | a base class initializer for some aggregate type, hopefully compatible | |
301 | with DECL. If INIT is a single element, and DECL is an aggregate | |
302 | type, we silently convert INIT into a TREE_LIST, allowing a constructor | |
303 | to be called. | |
304 | ||
305 | If INIT is a TREE_LIST and there is no constructor, turn INIT | |
306 | into a CONSTRUCTOR and use standard initialization techniques. | |
307 | Perhaps a warning should be generated? | |
308 | ||
309 | Returns value of initializer if initialization could not be | |
310 | performed for static variable. In that case, caller must do | |
311 | the storing. */ | |
312 | ||
313 | tree | |
314 | store_init_value (decl, init) | |
315 | tree decl, init; | |
316 | { | |
317 | register tree value, type; | |
318 | ||
319 | /* If variable's type was invalidly declared, just ignore it. */ | |
320 | ||
321 | type = TREE_TYPE (decl); | |
322 | if (TREE_CODE (type) == ERROR_MARK) | |
323 | return NULL_TREE; | |
324 | ||
e8abc66f | 325 | if (IS_AGGR_TYPE (type)) |
8d08fdba | 326 | { |
e8abc66f MS |
327 | if (! TYPE_HAS_TRIVIAL_INIT_REF (type) |
328 | && TREE_CODE (init) != CONSTRUCTOR) | |
a98facb0 | 329 | abort (); |
e8abc66f | 330 | |
6eabb241 | 331 | if (TREE_CODE (init) == TREE_LIST) |
8d08fdba | 332 | { |
33bd39a2 | 333 | error ("constructor syntax used, but no constructor declared for type `%T'", type); |
8d08fdba MS |
334 | init = build_nt (CONSTRUCTOR, NULL_TREE, nreverse (init)); |
335 | } | |
8d08fdba MS |
336 | } |
337 | else if (TREE_CODE (init) == TREE_LIST | |
338 | && TREE_TYPE (init) != unknown_type_node) | |
339 | { | |
340 | if (TREE_CODE (decl) == RESULT_DECL) | |
341 | { | |
342 | if (TREE_CHAIN (init)) | |
343 | { | |
8251199e | 344 | warning ("comma expression used to initialize return value"); |
8d08fdba MS |
345 | init = build_compound_expr (init); |
346 | } | |
347 | else | |
348 | init = TREE_VALUE (init); | |
349 | } | |
8d08fdba MS |
350 | else if (TREE_CODE (init) == TREE_LIST |
351 | && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE) | |
352 | { | |
8251199e | 353 | error ("cannot initialize arrays using this syntax"); |
8d08fdba MS |
354 | return NULL_TREE; |
355 | } | |
356 | else | |
357 | { | |
358 | /* We get here with code like `int a (2);' */ | |
359 | ||
360 | if (TREE_CHAIN (init) != NULL_TREE) | |
361 | { | |
8251199e | 362 | pedwarn ("initializer list being treated as compound expression"); |
8d08fdba MS |
363 | init = build_compound_expr (init); |
364 | } | |
365 | else | |
366 | init = TREE_VALUE (init); | |
367 | } | |
368 | } | |
369 | ||
370 | /* End of special C++ code. */ | |
371 | ||
8e3df2de MM |
372 | /* Digest the specified initializer into an expression. */ |
373 | value = digest_init (type, init, (tree *) 0); | |
8d08fdba MS |
374 | |
375 | /* Store the expression if valid; else report error. */ | |
376 | ||
377 | if (TREE_CODE (value) == ERROR_MARK) | |
378 | ; | |
7834ab39 MS |
379 | /* Other code expects that initializers for objects of types that need |
380 | constructing never make it into DECL_INITIAL, and passes 'init' to | |
f1dedc31 | 381 | build_aggr_init without checking DECL_INITIAL. So just return. */ |
7834ab39 MS |
382 | else if (TYPE_NEEDS_CONSTRUCTING (type)) |
383 | return value; | |
8d08fdba MS |
384 | else if (TREE_STATIC (decl) |
385 | && (! TREE_CONSTANT (value) | |
8ccc31eb | 386 | || ! initializer_constant_valid_p (value, TREE_TYPE (value)) |
8926095f MS |
387 | #if 0 |
388 | /* A STATIC PUBLIC int variable doesn't have to be | |
389 | run time inited when doing pic. (mrs) */ | |
8d08fdba MS |
390 | /* Since ctors and dtors are the only things that can |
391 | reference vtables, and they are always written down | |
38e01259 | 392 | the vtable definition, we can leave the |
8d08fdba MS |
393 | vtables in initialized data space. |
394 | However, other initialized data cannot be initialized | |
395 | this way. Instead a global file-level initializer | |
396 | must do the job. */ | |
8926095f MS |
397 | || (flag_pic && !DECL_VIRTUAL_P (decl) && TREE_PUBLIC (decl)) |
398 | #endif | |
399 | )) | |
400 | ||
8d08fdba | 401 | return value; |
f376e137 | 402 | #if 0 /* No, that's C. jason 9/19/94 */ |
8d08fdba MS |
403 | else |
404 | { | |
6eabb241 | 405 | if (pedantic && TREE_CODE (value) == CONSTRUCTOR) |
8d08fdba MS |
406 | { |
407 | if (! TREE_CONSTANT (value) || ! TREE_STATIC (value)) | |
c725bd79 | 408 | pedwarn ("ISO C++ forbids non-constant aggregate initializer expressions"); |
8d08fdba MS |
409 | } |
410 | } | |
f376e137 | 411 | #endif |
4d6abc1c MM |
412 | |
413 | /* Store the VALUE in DECL_INITIAL. If we're building a | |
414 | statement-tree we will actually expand the initialization later | |
415 | when we output this function. */ | |
8d08fdba MS |
416 | DECL_INITIAL (decl) = value; |
417 | return NULL_TREE; | |
418 | } | |
94e6e4c4 | 419 | |
8d08fdba MS |
420 | \f |
421 | /* Digest the parser output INIT as an initializer for type TYPE. | |
422 | Return a C expression of type TYPE to represent the initial value. | |
423 | ||
424 | If TAIL is nonzero, it points to a variable holding a list of elements | |
425 | of which INIT is the first. We update the list stored there by | |
426 | removing from the head all the elements that we use. | |
427 | Normally this is only one; we use more than one element only if | |
428 | TYPE is an aggregate and INIT is not a constructor. */ | |
429 | ||
430 | tree | |
431 | digest_init (type, init, tail) | |
432 | tree type, init, *tail; | |
433 | { | |
434 | enum tree_code code = TREE_CODE (type); | |
00595019 | 435 | tree element = NULL_TREE; |
a703fb38 | 436 | tree old_tail_contents = NULL_TREE; |
8e3df2de | 437 | /* Nonzero if INIT is a braced grouping. */ |
8d08fdba MS |
438 | int raw_constructor; |
439 | ||
440 | /* By default, assume we use one element from a list. | |
441 | We correct this later in the sole case where it is not true. */ | |
442 | ||
443 | if (tail) | |
444 | { | |
445 | old_tail_contents = *tail; | |
446 | *tail = TREE_CHAIN (*tail); | |
447 | } | |
448 | ||
449 | if (init == error_mark_node || (TREE_CODE (init) == TREE_LIST | |
450 | && TREE_VALUE (init) == error_mark_node)) | |
451 | return error_mark_node; | |
452 | ||
0ba8a114 NS |
453 | if (TREE_CODE (init) == ERROR_MARK) |
454 | /* __PRETTY_FUNCTION__'s initializer is a bogus expression inside | |
c6002625 | 455 | a template function. This gets substituted during instantiation. */ |
0ba8a114 | 456 | return init; |
b8b98c66 NS |
457 | |
458 | /* We must strip the outermost array type when completing the type, | |
459 | because the its bounds might be incomplete at the moment. */ | |
460 | if (!complete_type_or_else (TREE_CODE (type) == ARRAY_TYPE | |
461 | ? TREE_TYPE (type) : type, NULL_TREE)) | |
462 | return error_mark_node; | |
0ba8a114 | 463 | |
8d08fdba MS |
464 | /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ |
465 | if (TREE_CODE (init) == NON_LVALUE_EXPR) | |
466 | init = TREE_OPERAND (init, 0); | |
467 | ||
8e3df2de MM |
468 | raw_constructor = (TREE_CODE (init) == CONSTRUCTOR |
469 | && TREE_HAS_CONSTRUCTOR (init)); | |
8d08fdba | 470 | |
dc26f471 | 471 | if (raw_constructor |
8d08fdba MS |
472 | && CONSTRUCTOR_ELTS (init) != 0 |
473 | && TREE_CHAIN (CONSTRUCTOR_ELTS (init)) == 0) | |
474 | { | |
475 | element = TREE_VALUE (CONSTRUCTOR_ELTS (init)); | |
476 | /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ | |
477 | if (element && TREE_CODE (element) == NON_LVALUE_EXPR) | |
478 | element = TREE_OPERAND (element, 0); | |
479 | if (element == error_mark_node) | |
480 | return element; | |
481 | } | |
482 | ||
8d08fdba MS |
483 | /* Initialization of an array of chars from a string constant |
484 | optionally enclosed in braces. */ | |
485 | ||
486 | if (code == ARRAY_TYPE) | |
487 | { | |
8c90d611 JM |
488 | tree typ1; |
489 | ||
490 | if (TREE_CODE (init) == TREE_LIST) | |
491 | { | |
8251199e | 492 | error ("initializing array with parameter list"); |
8c90d611 JM |
493 | return error_mark_node; |
494 | } | |
495 | ||
496 | typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type)); | |
7b019c19 | 497 | if (char_type_p (typ1) |
8d08fdba MS |
498 | && ((init && TREE_CODE (init) == STRING_CST) |
499 | || (element && TREE_CODE (element) == STRING_CST))) | |
500 | { | |
501 | tree string = element ? element : init; | |
502 | ||
503 | if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string))) | |
504 | != char_type_node) | |
505 | && TYPE_PRECISION (typ1) == BITS_PER_UNIT) | |
506 | { | |
8251199e | 507 | error ("char-array initialized from wide string"); |
8d08fdba MS |
508 | return error_mark_node; |
509 | } | |
510 | if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string))) | |
511 | == char_type_node) | |
512 | && TYPE_PRECISION (typ1) != BITS_PER_UNIT) | |
513 | { | |
8251199e | 514 | error ("int-array initialized from non-wide string"); |
8d08fdba MS |
515 | return error_mark_node; |
516 | } | |
517 | ||
8d08fdba MS |
518 | TREE_TYPE (string) = type; |
519 | if (TYPE_DOMAIN (type) != 0 | |
520 | && TREE_CONSTANT (TYPE_SIZE (type))) | |
521 | { | |
522 | register int size | |
523 | = TREE_INT_CST_LOW (TYPE_SIZE (type)); | |
524 | size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT; | |
525 | /* In C it is ok to subtract 1 from the length of the string | |
526 | because it's ok to ignore the terminating null char that is | |
527 | counted in the length of the constant, but in C++ this would | |
528 | be invalid. */ | |
529 | if (size < TREE_STRING_LENGTH (string)) | |
8251199e | 530 | pedwarn ("initializer-string for array of chars is too long"); |
8d08fdba MS |
531 | } |
532 | return string; | |
533 | } | |
534 | } | |
535 | ||
536 | /* Handle scalar types, including conversions, | |
537 | and signature pointers and references. */ | |
538 | ||
539 | if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE | |
540 | || code == ENUMERAL_TYPE || code == REFERENCE_TYPE | |
ca4feb54 | 541 | || code == BOOLEAN_TYPE || code == COMPLEX_TYPE |
6eabb241 | 542 | || TYPE_PTRMEMFUNC_P (type)) |
8d08fdba MS |
543 | { |
544 | if (raw_constructor) | |
545 | { | |
546 | if (element == 0) | |
547 | { | |
8251199e | 548 | error ("initializer for scalar variable requires one element"); |
8d08fdba MS |
549 | return error_mark_node; |
550 | } | |
551 | init = element; | |
552 | } | |
dc26f471 | 553 | while (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init)) |
b7484fbe | 554 | { |
33bd39a2 | 555 | pedwarn ("braces around scalar initializer for `%T'", type); |
b7484fbe MS |
556 | init = CONSTRUCTOR_ELTS (init); |
557 | if (TREE_CHAIN (init)) | |
33bd39a2 | 558 | pedwarn ("ignoring extra initializers for `%T'", type); |
b7484fbe MS |
559 | init = TREE_VALUE (init); |
560 | } | |
8d08fdba MS |
561 | |
562 | return convert_for_initialization (0, type, init, LOOKUP_NORMAL, | |
563 | "initialization", NULL_TREE, 0); | |
564 | } | |
565 | ||
566 | /* Come here only for records and arrays (and unions with constructors). */ | |
567 | ||
d0f062fb | 568 | if (COMPLETE_TYPE_P (type) && ! TREE_CONSTANT (TYPE_SIZE (type))) |
8d08fdba | 569 | { |
33bd39a2 | 570 | error ("variable-sized object of type `%T' may not be initialized", |
7177d104 | 571 | type); |
8d08fdba MS |
572 | return error_mark_node; |
573 | } | |
574 | ||
ca4feb54 | 575 | if (code == ARRAY_TYPE || code == VECTOR_TYPE || IS_AGGR_TYPE_CODE (code)) |
8d08fdba | 576 | { |
f30efcb7 JM |
577 | if (raw_constructor && TYPE_NON_AGGREGATE_CLASS (type) |
578 | && TREE_HAS_CONSTRUCTOR (init)) | |
f30432d7 | 579 | { |
33bd39a2 | 580 | error ("subobject of type `%T' must be initialized by constructor, not by `%E'", |
f30432d7 MS |
581 | type, init); |
582 | return error_mark_node; | |
583 | } | |
584 | else if (raw_constructor) | |
8d08fdba | 585 | return process_init_constructor (type, init, (tree *)0); |
dc26f471 JM |
586 | else if (can_convert_arg (type, TREE_TYPE (init), init) |
587 | || TYPE_NON_AGGREGATE_CLASS (type)) | |
588 | /* These are never initialized from multiple constructor elements. */; | |
8d08fdba MS |
589 | else if (tail != 0) |
590 | { | |
591 | *tail = old_tail_contents; | |
592 | return process_init_constructor (type, 0, tail); | |
593 | } | |
d22c8596 | 594 | |
bb26522d | 595 | if (code != ARRAY_TYPE) |
dc26f471 JM |
596 | { |
597 | int flags = LOOKUP_NORMAL; | |
598 | /* Initialization from { } is copy-initialization. */ | |
599 | if (tail) | |
600 | flags |= LOOKUP_ONLYCONVERTING; | |
601 | ||
602 | return convert_for_initialization (NULL_TREE, type, init, flags, | |
603 | "initialization", NULL_TREE, 0); | |
604 | } | |
8d08fdba MS |
605 | } |
606 | ||
8251199e | 607 | error ("invalid initializer"); |
8d08fdba MS |
608 | return error_mark_node; |
609 | } | |
610 | \f | |
611 | /* Process a constructor for a variable of type TYPE. | |
612 | The constructor elements may be specified either with INIT or with ELTS, | |
613 | only one of which should be non-null. | |
614 | ||
615 | If INIT is specified, it is a CONSTRUCTOR node which is specifically | |
616 | and solely for initializing this datum. | |
617 | ||
618 | If ELTS is specified, it is the address of a variable containing | |
619 | a list of expressions. We take as many elements as we need | |
620 | from the head of the list and update the list. | |
621 | ||
622 | In the resulting constructor, TREE_CONSTANT is set if all elts are | |
623 | constant, and TREE_STATIC is set if, in addition, all elts are simple enough | |
624 | constants that the assembler and linker can compute them. */ | |
625 | ||
626 | static tree | |
627 | process_init_constructor (type, init, elts) | |
628 | tree type, init, *elts; | |
629 | { | |
8d08fdba MS |
630 | register tree tail; |
631 | /* List of the elements of the result constructor, | |
632 | in reverse order. */ | |
633 | register tree members = NULL; | |
e6267549 | 634 | register tree next1; |
8d08fdba MS |
635 | tree result; |
636 | int allconstant = 1; | |
637 | int allsimple = 1; | |
638 | int erroneous = 0; | |
639 | ||
640 | /* Make TAIL be the list of elements to use for the initialization, | |
641 | no matter how the data was given to us. */ | |
642 | ||
643 | if (elts) | |
644 | { | |
645 | if (warn_missing_braces) | |
8251199e | 646 | warning ("aggregate has a partly bracketed initializer"); |
8d08fdba MS |
647 | tail = *elts; |
648 | } | |
649 | else | |
650 | tail = CONSTRUCTOR_ELTS (init); | |
651 | ||
652 | /* Gobble as many elements as needed, and make a constructor or initial value | |
653 | for each element of this aggregate. Chain them together in result. | |
654 | If there are too few, use 0 for each scalar ultimate component. */ | |
655 | ||
ca4feb54 | 656 | if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE) |
8d08fdba | 657 | { |
8d08fdba MS |
658 | register long len; |
659 | register int i; | |
660 | ||
ca4feb54 JM |
661 | if (TREE_CODE (type) == ARRAY_TYPE) |
662 | { | |
663 | tree domain = TYPE_DOMAIN (type); | |
664 | if (domain) | |
665 | len = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain)) | |
666 | - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain)) | |
667 | + 1); | |
668 | else | |
669 | len = -1; /* Take as many as there are */ | |
670 | } | |
8d08fdba | 671 | else |
ca4feb54 JM |
672 | { |
673 | /* Vectors are like simple fixed-size arrays. */ | |
674 | len = TYPE_VECTOR_SUBPARTS (type); | |
675 | } | |
8d08fdba | 676 | |
e6267549 | 677 | for (i = 0; len < 0 || i < len; i++) |
8d08fdba | 678 | { |
e6267549 | 679 | if (tail) |
8d08fdba | 680 | { |
e6267549 JM |
681 | if (TREE_PURPOSE (tail) |
682 | && (TREE_CODE (TREE_PURPOSE (tail)) != INTEGER_CST | |
05bccae2 | 683 | || compare_tree_int (TREE_PURPOSE (tail), i) != 0)) |
e6267549 JM |
684 | sorry ("non-trivial labeled initializers"); |
685 | ||
686 | if (TREE_VALUE (tail) != 0) | |
a5894242 | 687 | { |
e6267549 JM |
688 | tree tail1 = tail; |
689 | next1 = digest_init (TREE_TYPE (type), | |
690 | TREE_VALUE (tail), &tail1); | |
0db982be ML |
691 | if (next1 == error_mark_node) |
692 | return next1; | |
7e2067ca | 693 | my_friendly_assert |
9edc3913 MM |
694 | (same_type_ignoring_top_level_qualifiers_p |
695 | (TREE_TYPE (type), TREE_TYPE (next1)), | |
7e2067ca | 696 | 981123); |
e6267549 JM |
697 | my_friendly_assert (tail1 == 0 |
698 | || TREE_CODE (tail1) == TREE_LIST, 319); | |
699 | if (tail == tail1 && len < 0) | |
700 | { | |
701 | error ("non-empty initializer for array of empty elements"); | |
702 | /* Just ignore what we were supposed to use. */ | |
703 | tail1 = NULL_TREE; | |
704 | } | |
705 | tail = tail1; | |
a5894242 | 706 | } |
e6267549 | 707 | else |
8d08fdba | 708 | { |
e6267549 JM |
709 | next1 = error_mark_node; |
710 | tail = TREE_CHAIN (tail); | |
8d08fdba | 711 | } |
8d08fdba | 712 | } |
e6267549 JM |
713 | else if (len < 0) |
714 | /* We're done. */ | |
715 | break; | |
716 | else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type))) | |
8d08fdba | 717 | { |
e6267549 JM |
718 | /* If this type needs constructors run for |
719 | default-initialization, we can't rely on the backend to do it | |
720 | for us, so build up TARGET_EXPRs. If the type in question is | |
721 | a class, just build one up; if it's an array, recurse. */ | |
722 | ||
723 | if (IS_AGGR_TYPE (TREE_TYPE (type))) | |
724 | next1 = build_functional_cast (TREE_TYPE (type), NULL_TREE); | |
725 | else | |
726 | next1 = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, NULL_TREE); | |
727 | next1 = digest_init (TREE_TYPE (type), next1, 0); | |
8d08fdba | 728 | } |
94e6e4c4 | 729 | else if (! zero_init_p (TREE_TYPE (type))) |
17bbb839 MM |
730 | next1 = build_zero_init (TREE_TYPE (type), |
731 | /*static_storage_p=*/false); | |
e6267549 JM |
732 | else |
733 | /* The default zero-initialization is fine for us; don't | |
734 | add anything to the CONSTRUCTOR. */ | |
735 | break; | |
8d08fdba MS |
736 | |
737 | if (next1 == error_mark_node) | |
738 | erroneous = 1; | |
739 | else if (!TREE_CONSTANT (next1)) | |
740 | allconstant = 0; | |
8ccc31eb | 741 | else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1))) |
8d08fdba | 742 | allsimple = 0; |
96756199 | 743 | members = tree_cons (size_int (i), next1, members); |
8d08fdba MS |
744 | } |
745 | } | |
8a72a046 | 746 | else if (TREE_CODE (type) == RECORD_TYPE) |
8d08fdba MS |
747 | { |
748 | register tree field; | |
749 | ||
750 | if (tail) | |
751 | { | |
752 | if (TYPE_USES_VIRTUAL_BASECLASSES (type)) | |
753 | { | |
7e4d7898 | 754 | sorry ("initializer list for object of class with virtual base classes"); |
8d08fdba MS |
755 | return error_mark_node; |
756 | } | |
757 | ||
758 | if (TYPE_BINFO_BASETYPES (type)) | |
759 | { | |
7e4d7898 | 760 | sorry ("initializer list for object of class with base classes"); |
8d08fdba MS |
761 | return error_mark_node; |
762 | } | |
763 | ||
4c6b7393 | 764 | if (TYPE_POLYMORPHIC_P (type)) |
8d08fdba MS |
765 | { |
766 | sorry ("initializer list for object using virtual functions"); | |
767 | return error_mark_node; | |
768 | } | |
769 | } | |
770 | ||
e6267549 | 771 | for (field = TYPE_FIELDS (type); field; |
8d08fdba MS |
772 | field = TREE_CHAIN (field)) |
773 | { | |
c8c133cd | 774 | if (! DECL_NAME (field) && DECL_C_BIT_FIELD (field)) |
8d08fdba | 775 | { |
96756199 | 776 | members = tree_cons (field, integer_zero_node, members); |
8d08fdba MS |
777 | continue; |
778 | } | |
779 | ||
17bbb839 | 780 | if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field)) |
8d08fdba MS |
781 | continue; |
782 | ||
e6267549 JM |
783 | if (tail) |
784 | { | |
785 | if (TREE_PURPOSE (tail) | |
786 | && TREE_PURPOSE (tail) != field | |
787 | && TREE_PURPOSE (tail) != DECL_NAME (field)) | |
788 | sorry ("non-trivial labeled initializers"); | |
789 | ||
790 | if (TREE_VALUE (tail) != 0) | |
791 | { | |
792 | tree tail1 = tail; | |
c8fcb331 | 793 | |
e6267549 JM |
794 | next1 = digest_init (TREE_TYPE (field), |
795 | TREE_VALUE (tail), &tail1); | |
796 | my_friendly_assert (tail1 == 0 | |
797 | || TREE_CODE (tail1) == TREE_LIST, 320); | |
798 | tail = tail1; | |
799 | } | |
800 | else | |
801 | { | |
802 | next1 = error_mark_node; | |
803 | tail = TREE_CHAIN (tail); | |
804 | } | |
805 | } | |
806 | else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field))) | |
8d08fdba | 807 | { |
e6267549 JM |
808 | /* If this type needs constructors run for |
809 | default-initialization, we can't rely on the backend to do it | |
810 | for us, so build up TARGET_EXPRs. If the type in question is | |
811 | a class, just build one up; if it's an array, recurse. */ | |
812 | ||
813 | if (IS_AGGR_TYPE (TREE_TYPE (field))) | |
814 | next1 = build_functional_cast (TREE_TYPE (field), | |
815 | NULL_TREE); | |
816 | else | |
45abaea8 NS |
817 | { |
818 | next1 = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, | |
819 | NULL_TREE); | |
820 | if (init) | |
821 | TREE_HAS_CONSTRUCTOR (next1) | |
822 | = TREE_HAS_CONSTRUCTOR (init); | |
823 | } | |
e6267549 | 824 | next1 = digest_init (TREE_TYPE (field), next1, 0); |
8d08fdba | 825 | |
e6267549 | 826 | /* Warn when some struct elements are implicitly initialized. */ |
45abaea8 NS |
827 | if (extra_warnings |
828 | && (!init || TREE_HAS_CONSTRUCTOR (init))) | |
33bd39a2 | 829 | warning ("missing initializer for member `%D'", field); |
8d08fdba MS |
830 | } |
831 | else | |
832 | { | |
e6267549 | 833 | if (TREE_READONLY (field)) |
33bd39a2 | 834 | error ("uninitialized const member `%D'", field); |
6eb35968 | 835 | else if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field))) |
33bd39a2 | 836 | error ("member `%D' with uninitialized const fields", |
e6267549 JM |
837 | field); |
838 | else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE) | |
33bd39a2 | 839 | error ("member `%D' is uninitialized reference", field); |
e6267549 JM |
840 | |
841 | /* Warn when some struct elements are implicitly initialized | |
842 | to zero. */ | |
45abaea8 NS |
843 | if (extra_warnings |
844 | && (!init || TREE_HAS_CONSTRUCTOR (init))) | |
33bd39a2 | 845 | warning ("missing initializer for member `%D'", field); |
e6267549 | 846 | |
94e6e4c4 | 847 | if (! zero_init_p (TREE_TYPE (field))) |
17bbb839 MM |
848 | next1 = build_zero_init (TREE_TYPE (field), |
849 | /*static_storage_p=*/false); | |
94e6e4c4 AO |
850 | else |
851 | /* The default zero-initialization is fine for us; don't | |
852 | add anything to the CONSTRUCTOR. */ | |
853 | continue; | |
8d08fdba MS |
854 | } |
855 | ||
856 | if (next1 == error_mark_node) | |
857 | erroneous = 1; | |
858 | else if (!TREE_CONSTANT (next1)) | |
859 | allconstant = 0; | |
8ccc31eb | 860 | else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1))) |
8d08fdba | 861 | allsimple = 0; |
96756199 | 862 | members = tree_cons (field, next1, members); |
8d08fdba | 863 | } |
8d08fdba | 864 | } |
a59f8640 R |
865 | else if (TREE_CODE (type) == UNION_TYPE |
866 | /* If the initializer was empty, use default zero initialization. */ | |
867 | && tail) | |
8d08fdba MS |
868 | { |
869 | register tree field = TYPE_FIELDS (type); | |
8d08fdba MS |
870 | |
871 | /* Find the first named field. ANSI decided in September 1990 | |
872 | that only named fields count here. */ | |
17bbb839 | 873 | while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL)) |
8d08fdba MS |
874 | field = TREE_CHAIN (field); |
875 | ||
876 | /* If this element specifies a field, initialize via that field. */ | |
877 | if (TREE_PURPOSE (tail) != NULL_TREE) | |
878 | { | |
879 | int win = 0; | |
880 | ||
881 | if (TREE_CODE (TREE_PURPOSE (tail)) == FIELD_DECL) | |
882 | /* Handle the case of a call by build_c_cast. */ | |
883 | field = TREE_PURPOSE (tail), win = 1; | |
884 | else if (TREE_CODE (TREE_PURPOSE (tail)) != IDENTIFIER_NODE) | |
8251199e | 885 | error ("index value instead of field name in union initializer"); |
8d08fdba MS |
886 | else |
887 | { | |
888 | tree temp; | |
889 | for (temp = TYPE_FIELDS (type); | |
890 | temp; | |
891 | temp = TREE_CHAIN (temp)) | |
892 | if (DECL_NAME (temp) == TREE_PURPOSE (tail)) | |
893 | break; | |
894 | if (temp) | |
895 | field = temp, win = 1; | |
896 | else | |
33bd39a2 | 897 | error ("no field `%D' in union being initialized", |
e6267549 | 898 | TREE_PURPOSE (tail)); |
8d08fdba MS |
899 | } |
900 | if (!win) | |
901 | TREE_VALUE (tail) = error_mark_node; | |
902 | } | |
7177d104 MS |
903 | else if (field == 0) |
904 | { | |
33bd39a2 | 905 | error ("union `%T' with no named members cannot be initialized", |
7177d104 MS |
906 | type); |
907 | TREE_VALUE (tail) = error_mark_node; | |
908 | } | |
8d08fdba MS |
909 | |
910 | if (TREE_VALUE (tail) != 0) | |
911 | { | |
912 | tree tail1 = tail; | |
913 | ||
914 | next1 = digest_init (TREE_TYPE (field), | |
915 | TREE_VALUE (tail), &tail1); | |
916 | if (tail1 != 0 && TREE_CODE (tail1) != TREE_LIST) | |
a98facb0 | 917 | abort (); |
8d08fdba MS |
918 | tail = tail1; |
919 | } | |
920 | else | |
921 | { | |
922 | next1 = error_mark_node; | |
923 | tail = TREE_CHAIN (tail); | |
924 | } | |
925 | ||
926 | if (next1 == error_mark_node) | |
927 | erroneous = 1; | |
928 | else if (!TREE_CONSTANT (next1)) | |
929 | allconstant = 0; | |
8ccc31eb | 930 | else if (initializer_constant_valid_p (next1, TREE_TYPE (next1)) == 0) |
8d08fdba | 931 | allsimple = 0; |
96756199 | 932 | members = tree_cons (field, next1, members); |
8d08fdba MS |
933 | } |
934 | ||
935 | /* If arguments were specified as a list, just remove the ones we used. */ | |
936 | if (elts) | |
937 | *elts = tail; | |
938 | /* If arguments were specified as a constructor, | |
939 | complain unless we used all the elements of the constructor. */ | |
940 | else if (tail) | |
8251199e | 941 | pedwarn ("excess elements in aggregate initializer"); |
8d08fdba MS |
942 | |
943 | if (erroneous) | |
944 | return error_mark_node; | |
945 | ||
946 | result = build (CONSTRUCTOR, type, NULL_TREE, nreverse (members)); | |
8c081e84 MM |
947 | if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE) |
948 | complete_array_type (type, result, /*do_default=*/0); | |
8d08fdba MS |
949 | if (init) |
950 | TREE_HAS_CONSTRUCTOR (result) = TREE_HAS_CONSTRUCTOR (init); | |
951 | if (allconstant) TREE_CONSTANT (result) = 1; | |
952 | if (allconstant && allsimple) TREE_STATIC (result) = 1; | |
953 | return result; | |
954 | } | |
955 | \f | |
956 | /* Given a structure or union value DATUM, construct and return | |
957 | the structure or union component which results from narrowing | |
a29e1034 | 958 | that value to the base specified in BASETYPE. For example, given the |
8d08fdba MS |
959 | hierarchy |
960 | ||
961 | class L { int ii; }; | |
962 | class A : L { ... }; | |
963 | class B : L { ... }; | |
964 | class C : A, B { ... }; | |
965 | ||
966 | and the declaration | |
967 | ||
968 | C x; | |
969 | ||
970 | then the expression | |
971 | ||
be99da77 | 972 | x.A::ii refers to the ii member of the L part of |
38e01259 | 973 | the A part of the C object named by X. In this case, |
aa52c1ff JM |
974 | DATUM would be x, and BASETYPE would be A. |
975 | ||
477f6664 JM |
976 | I used to think that this was nonconformant, that the standard specified |
977 | that first we look up ii in A, then convert x to an L& and pull out the | |
978 | ii part. But in fact, it does say that we convert x to an A&; A here | |
a29e1034 JM |
979 | is known as the "naming class". (jason 2000-12-19) |
980 | ||
981 | BINFO_P points to a variable initialized either to NULL_TREE or to the | |
982 | binfo for the specific base subobject we want to convert to. */ | |
8d08fdba MS |
983 | |
984 | tree | |
a29e1034 | 985 | build_scoped_ref (datum, basetype, binfo_p) |
8d08fdba | 986 | tree datum; |
be99da77 | 987 | tree basetype; |
a29e1034 | 988 | tree *binfo_p; |
8d08fdba | 989 | { |
338d90b8 | 990 | tree binfo; |
8d08fdba MS |
991 | |
992 | if (datum == error_mark_node) | |
993 | return error_mark_node; | |
a29e1034 JM |
994 | if (*binfo_p) |
995 | binfo = *binfo_p; | |
996 | else | |
997 | binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check, NULL); | |
8d08fdba | 998 | |
a29e1034 JM |
999 | if (!binfo || binfo == error_mark_node) |
1000 | { | |
1001 | *binfo_p = NULL_TREE; | |
1002 | if (!binfo) | |
1003 | error_not_base_type (basetype, TREE_TYPE (datum)); | |
1004 | return error_mark_node; | |
1005 | } | |
8d08fdba | 1006 | |
a29e1034 JM |
1007 | *binfo_p = binfo; |
1008 | return build_base_path (PLUS_EXPR, datum, binfo, 1); | |
8d08fdba MS |
1009 | } |
1010 | ||
1011 | /* Build a reference to an object specified by the C++ `->' operator. | |
1012 | Usually this just involves dereferencing the object, but if the | |
1013 | `->' operator is overloaded, then such overloads must be | |
1014 | performed until an object which does not have the `->' operator | |
1015 | overloaded is found. An error is reported when circular pointer | |
1016 | delegation is detected. */ | |
e92cc029 | 1017 | |
8d08fdba MS |
1018 | tree |
1019 | build_x_arrow (datum) | |
1020 | tree datum; | |
1021 | { | |
1022 | tree types_memoized = NULL_TREE; | |
1023 | register tree rval = datum; | |
1024 | tree type = TREE_TYPE (rval); | |
a703fb38 | 1025 | tree last_rval = NULL_TREE; |
8d08fdba MS |
1026 | |
1027 | if (type == error_mark_node) | |
1028 | return error_mark_node; | |
1029 | ||
5156628f | 1030 | if (processing_template_decl) |
5566b478 MS |
1031 | return build_min_nt (ARROW_EXPR, rval); |
1032 | ||
a0a33927 MS |
1033 | if (TREE_CODE (rval) == OFFSET_REF) |
1034 | { | |
1035 | rval = resolve_offset_ref (datum); | |
1036 | type = TREE_TYPE (rval); | |
1037 | } | |
1038 | ||
8d08fdba MS |
1039 | if (TREE_CODE (type) == REFERENCE_TYPE) |
1040 | { | |
1041 | rval = convert_from_reference (rval); | |
1042 | type = TREE_TYPE (rval); | |
1043 | } | |
1044 | ||
3c215895 | 1045 | if (IS_AGGR_TYPE (type)) |
8d08fdba | 1046 | { |
14d22dd6 MM |
1047 | while ((rval = build_new_op (COMPONENT_REF, LOOKUP_NORMAL, rval, |
1048 | NULL_TREE, NULL_TREE))) | |
8d08fdba MS |
1049 | { |
1050 | if (rval == error_mark_node) | |
1051 | return error_mark_node; | |
1052 | ||
1053 | if (value_member (TREE_TYPE (rval), types_memoized)) | |
1054 | { | |
8251199e | 1055 | error ("circular pointer delegation detected"); |
8d08fdba MS |
1056 | return error_mark_node; |
1057 | } | |
1058 | else | |
1059 | { | |
1060 | types_memoized = tree_cons (NULL_TREE, TREE_TYPE (rval), | |
1061 | types_memoized); | |
1062 | } | |
1063 | last_rval = rval; | |
1064 | } | |
297dcfb3 MM |
1065 | |
1066 | if (last_rval == NULL_TREE) | |
1067 | { | |
33bd39a2 | 1068 | error ("base operand of `->' has non-pointer type `%T'", type); |
297dcfb3 MM |
1069 | return error_mark_node; |
1070 | } | |
1071 | ||
8d08fdba MS |
1072 | if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE) |
1073 | last_rval = convert_from_reference (last_rval); | |
1074 | } | |
1075 | else | |
1076 | last_rval = default_conversion (rval); | |
1077 | ||
8d08fdba | 1078 | if (TREE_CODE (TREE_TYPE (last_rval)) == POINTER_TYPE) |
3e411c3f | 1079 | return build_indirect_ref (last_rval, NULL); |
8d08fdba | 1080 | |
8d08fdba | 1081 | if (types_memoized) |
8251199e | 1082 | error ("result of `operator->()' yields non-pointer result"); |
8d08fdba | 1083 | else |
8251199e | 1084 | error ("base operand of `->' is not a pointer"); |
8d08fdba MS |
1085 | return error_mark_node; |
1086 | } | |
1087 | ||
1088 | /* Make an expression to refer to the COMPONENT field of | |
1089 | structure or union value DATUM. COMPONENT is an arbitrary | |
1090 | expression. DATUM has not already been checked out to be of | |
1091 | aggregate type. | |
1092 | ||
1093 | For C++, COMPONENT may be a TREE_LIST. This happens when we must | |
1094 | return an object of member type to a method of the current class, | |
1095 | but there is not yet enough typing information to know which one. | |
1096 | As a special case, if there is only one method by that name, | |
1097 | it is returned. Otherwise we return an expression which other | |
1098 | routines will have to know how to deal with later. */ | |
e92cc029 | 1099 | |
8d08fdba MS |
1100 | tree |
1101 | build_m_component_ref (datum, component) | |
1102 | tree datum, component; | |
1103 | { | |
1104 | tree type; | |
c3e899c1 JM |
1105 | tree objtype; |
1106 | tree field_type; | |
1107 | int type_quals; | |
71851aaa | 1108 | tree binfo; |
8d08fdba | 1109 | |
5156628f | 1110 | if (processing_template_decl) |
5566b478 MS |
1111 | return build_min_nt (DOTSTAR_EXPR, datum, component); |
1112 | ||
c3e899c1 | 1113 | datum = decay_conversion (datum); |
f1a3f197 MM |
1114 | |
1115 | if (datum == error_mark_node || component == error_mark_node) | |
1116 | return error_mark_node; | |
1117 | ||
c3e899c1 JM |
1118 | objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum)); |
1119 | ||
8d08fdba MS |
1120 | if (TYPE_PTRMEMFUNC_P (TREE_TYPE (component))) |
1121 | { | |
1122 | type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (component))); | |
c3e899c1 | 1123 | field_type = type; |
8d08fdba | 1124 | } |
f1a3f197 | 1125 | else if (TYPE_PTRMEM_P (TREE_TYPE (component))) |
8d08fdba | 1126 | { |
162bc98d | 1127 | type = TREE_TYPE (TREE_TYPE (component)); |
c3e899c1 | 1128 | field_type = TREE_TYPE (type); |
3ad97789 NS |
1129 | |
1130 | /* Compute the type of the field, as described in [expr.ref]. */ | |
1131 | type_quals = TYPE_UNQUALIFIED; | |
1132 | if (TREE_CODE (field_type) == REFERENCE_TYPE) | |
1133 | /* The standard says that the type of the result should be the | |
1134 | type referred to by the reference. But for now, at least, | |
1135 | we do the conversion from reference type later. */ | |
1136 | ; | |
1137 | else | |
1138 | { | |
1139 | type_quals = (cp_type_quals (field_type) | |
1140 | | cp_type_quals (TREE_TYPE (datum))); | |
1141 | ||
1142 | /* There's no such thing as a mutable pointer-to-member, so | |
50ad9642 MM |
1143 | things are not as complex as they are for references to |
1144 | non-static data members. */ | |
3ad97789 NS |
1145 | field_type = cp_build_qualified_type (field_type, type_quals); |
1146 | } | |
8d08fdba | 1147 | } |
f1a3f197 | 1148 | else |
8d08fdba | 1149 | { |
33bd39a2 | 1150 | error ("`%E' cannot be used as a member pointer, since it is of type `%T'", |
f1a3f197 | 1151 | component, TREE_TYPE (component)); |
8d08fdba MS |
1152 | return error_mark_node; |
1153 | } | |
1154 | ||
51c184be MS |
1155 | if (! IS_AGGR_TYPE (objtype)) |
1156 | { | |
3ad97789 NS |
1157 | error ("cannot apply member pointer `%E' to `%E', which is of non-aggregate type `%T'", |
1158 | component, datum, objtype); | |
51c184be MS |
1159 | return error_mark_node; |
1160 | } | |
71851aaa | 1161 | |
2db1ab2d NS |
1162 | binfo = lookup_base (objtype, TYPE_METHOD_BASETYPE (type), |
1163 | ba_check, NULL); | |
1164 | if (!binfo) | |
8d08fdba | 1165 | { |
33bd39a2 | 1166 | error ("member type `%T::' incompatible with object type `%T'", |
8d08fdba MS |
1167 | TYPE_METHOD_BASETYPE (type), objtype); |
1168 | return error_mark_node; | |
1169 | } | |
71851aaa MS |
1170 | else if (binfo == error_mark_node) |
1171 | return error_mark_node; | |
8d08fdba | 1172 | |
c3e899c1 | 1173 | component = build (OFFSET_REF, field_type, datum, component); |
d2e5ee5c MS |
1174 | if (TREE_CODE (type) == OFFSET_TYPE) |
1175 | component = resolve_offset_ref (component); | |
1176 | return component; | |
8d08fdba MS |
1177 | } |
1178 | ||
fc378698 | 1179 | /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */ |
e92cc029 | 1180 | |
8d08fdba MS |
1181 | tree |
1182 | build_functional_cast (exp, parms) | |
1183 | tree exp; | |
1184 | tree parms; | |
1185 | { | |
1186 | /* This is either a call to a constructor, | |
1187 | or a C cast in C++'s `functional' notation. */ | |
fc378698 | 1188 | tree type; |
8d08fdba MS |
1189 | |
1190 | if (exp == error_mark_node || parms == error_mark_node) | |
1191 | return error_mark_node; | |
1192 | ||
1193 | if (TREE_CODE (exp) == IDENTIFIER_NODE) | |
1194 | { | |
8d08fdba MS |
1195 | if (IDENTIFIER_HAS_TYPE_VALUE (exp)) |
1196 | /* Either an enum or an aggregate type. */ | |
1197 | type = IDENTIFIER_TYPE_VALUE (exp); | |
1198 | else | |
1199 | { | |
1200 | type = lookup_name (exp, 1); | |
1201 | if (!type || TREE_CODE (type) != TYPE_DECL) | |
1202 | { | |
33bd39a2 | 1203 | error ("`%T' fails to be a typedef or built-in type", exp); |
8d08fdba MS |
1204 | return error_mark_node; |
1205 | } | |
1206 | type = TREE_TYPE (type); | |
1207 | } | |
1208 | } | |
45537677 MS |
1209 | else if (TREE_CODE (exp) == TYPE_DECL) |
1210 | type = TREE_TYPE (exp); | |
8d08fdba MS |
1211 | else |
1212 | type = exp; | |
1213 | ||
5156628f | 1214 | if (processing_template_decl) |
5566b478 MS |
1215 | return build_min (CAST_EXPR, type, parms); |
1216 | ||
8d08fdba MS |
1217 | if (! IS_AGGR_TYPE (type)) |
1218 | { | |
1219 | /* this must build a C cast */ | |
1220 | if (parms == NULL_TREE) | |
d18c083e | 1221 | parms = integer_zero_node; |
8ccc31eb | 1222 | else |
a0a33927 | 1223 | { |
8ccc31eb | 1224 | if (TREE_CHAIN (parms) != NULL_TREE) |
8251199e | 1225 | pedwarn ("initializer list being treated as compound expression"); |
a0a33927 MS |
1226 | parms = build_compound_expr (parms); |
1227 | } | |
8ccc31eb | 1228 | |
faf5394a | 1229 | return build_c_cast (type, parms); |
8d08fdba MS |
1230 | } |
1231 | ||
45537677 MS |
1232 | /* Prepare to evaluate as a call to a constructor. If this expression |
1233 | is actually used, for example, | |
1234 | ||
1235 | return X (arg1, arg2, ...); | |
1236 | ||
1237 | then the slot being initialized will be filled in. */ | |
1238 | ||
d0f062fb NS |
1239 | if (!complete_type_or_else (type, NULL_TREE)) |
1240 | return error_mark_node; | |
a7a64a77 MM |
1241 | if (abstract_virtuals_error (NULL_TREE, type)) |
1242 | return error_mark_node; | |
8d08fdba MS |
1243 | |
1244 | if (parms && TREE_CHAIN (parms) == NULL_TREE) | |
faf5394a | 1245 | return build_c_cast (type, TREE_VALUE (parms)); |
8d08fdba | 1246 | |
3551c177 JM |
1247 | /* We need to zero-initialize POD types. Let's do that for everything |
1248 | that doesn't need a constructor. */ | |
1249 | if (parms == NULL_TREE && !TYPE_NEEDS_CONSTRUCTING (type) | |
1250 | && TYPE_HAS_DEFAULT_CONSTRUCTOR (type)) | |
1251 | { | |
1252 | exp = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE); | |
1253 | return get_target_expr (exp); | |
1254 | } | |
1255 | ||
4ba126e4 MM |
1256 | exp = build_special_member_call (NULL_TREE, complete_ctor_identifier, parms, |
1257 | TYPE_BINFO (type), LOOKUP_NORMAL); | |
8d08fdba | 1258 | |
fc378698 | 1259 | if (exp == error_mark_node) |
a0a33927 | 1260 | return error_mark_node; |
8d08fdba | 1261 | |
fc378698 | 1262 | return build_cplus_new (type, exp); |
8d08fdba MS |
1263 | } |
1264 | \f | |
46b02c6d | 1265 | |
4cc1d462 NS |
1266 | /* Add new exception specifier SPEC, to the LIST we currently have. |
1267 | If it's already in LIST then do nothing. | |
1268 | Moan if it's bad and we're allowed to. COMPLAIN < 0 means we | |
1269 | know what we're doing. */ | |
1270 | ||
1271 | tree | |
1272 | add_exception_specifier (list, spec, complain) | |
1273 | tree list, spec; | |
1274 | int complain; | |
1275 | { | |
1276 | int ok; | |
1277 | tree core = spec; | |
1278 | int is_ptr; | |
5aa3396c | 1279 | int diag_type = -1; /* none */ |
4cc1d462 NS |
1280 | |
1281 | if (spec == error_mark_node) | |
1282 | return list; | |
1283 | ||
1284 | my_friendly_assert (spec && (!list || TREE_VALUE (list)), 19990317); | |
1285 | ||
1286 | /* [except.spec] 1, type in an exception specifier shall not be | |
1287 | incomplete, or pointer or ref to incomplete other than pointer | |
1288 | to cv void. */ | |
1289 | is_ptr = TREE_CODE (core) == POINTER_TYPE; | |
1290 | if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE) | |
1291 | core = TREE_TYPE (core); | |
1292 | if (complain < 0) | |
1293 | ok = 1; | |
b72801e2 | 1294 | else if (VOID_TYPE_P (core)) |
4cc1d462 NS |
1295 | ok = is_ptr; |
1296 | else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM) | |
1297 | ok = 1; | |
baeb4732 JJ |
1298 | else if (processing_template_decl) |
1299 | ok = 1; | |
4cc1d462 | 1300 | else |
5aa3396c JM |
1301 | { |
1302 | ok = 1; | |
1303 | /* 15.4/1 says that types in an exception specifier must be complete, | |
1304 | but it seems more reasonable to only require this on definitions | |
1305 | and calls. So just give a pedwarn at this point; we will give an | |
1306 | error later if we hit one of those two cases. */ | |
1307 | if (!COMPLETE_TYPE_P (complete_type (core))) | |
1308 | diag_type = 2; /* pedwarn */ | |
1309 | } | |
baeb4732 | 1310 | |
4cc1d462 NS |
1311 | if (ok) |
1312 | { | |
1313 | tree probe; | |
1314 | ||
1315 | for (probe = list; probe; probe = TREE_CHAIN (probe)) | |
1316 | if (same_type_p (TREE_VALUE (probe), spec)) | |
1317 | break; | |
1318 | if (!probe) | |
80b1331c | 1319 | list = tree_cons (NULL_TREE, spec, list); |
4cc1d462 | 1320 | } |
5aa3396c JM |
1321 | else |
1322 | diag_type = 0; /* error */ | |
1323 | ||
1324 | if (diag_type >= 0 && complain) | |
1325 | cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type); | |
1326 | ||
4cc1d462 NS |
1327 | return list; |
1328 | } | |
03378143 NS |
1329 | |
1330 | /* Combine the two exceptions specifier lists LIST and ADD, and return | |
c6002625 | 1331 | their union. */ |
03378143 NS |
1332 | |
1333 | tree | |
1334 | merge_exception_specifiers (list, add) | |
1335 | tree list, add; | |
1336 | { | |
1337 | if (!list || !add) | |
1338 | return NULL_TREE; | |
1339 | else if (!TREE_VALUE (list)) | |
1340 | return add; | |
1341 | else if (!TREE_VALUE (add)) | |
1342 | return list; | |
1343 | else | |
1344 | { | |
1345 | tree orig_list = list; | |
1346 | ||
1347 | for (; add; add = TREE_CHAIN (add)) | |
1348 | { | |
1349 | tree spec = TREE_VALUE (add); | |
1350 | tree probe; | |
1351 | ||
1352 | for (probe = orig_list; probe; probe = TREE_CHAIN (probe)) | |
1353 | if (same_type_p (TREE_VALUE (probe), spec)) | |
1354 | break; | |
1355 | if (!probe) | |
1356 | { | |
1357 | spec = build_tree_list (NULL_TREE, spec); | |
1358 | TREE_CHAIN (spec) = list; | |
1359 | list = spec; | |
1360 | } | |
1361 | } | |
1362 | } | |
1363 | return list; | |
1364 | } | |
5aa3396c JM |
1365 | |
1366 | /* Subroutine of build_call. Ensure that each of the types in the | |
1367 | exception specification is complete. Technically, 15.4/1 says that | |
1368 | they need to be complete when we see a declaration of the function, | |
1369 | but we should be able to get away with only requiring this when the | |
1370 | function is defined or called. See also add_exception_specifier. */ | |
1371 | ||
1372 | void | |
1373 | require_complete_eh_spec_types (fntype, decl) | |
1374 | tree fntype, decl; | |
1375 | { | |
1376 | tree raises; | |
1377 | /* Don't complain about calls to op new. */ | |
1378 | if (decl && DECL_ARTIFICIAL (decl)) | |
1379 | return; | |
1380 | for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises; | |
1381 | raises = TREE_CHAIN (raises)) | |
1382 | { | |
1383 | tree type = TREE_VALUE (raises); | |
1384 | if (type && !COMPLETE_TYPE_P (type)) | |
1385 | { | |
1386 | if (decl) | |
1387 | error | |
1388 | ("call to function `%D' which throws incomplete type `%#T'", | |
1389 | decl, type); | |
1390 | else | |
1391 | error ("call to function which throws incomplete type `%#T'", | |
1392 | decl); | |
1393 | } | |
1394 | } | |
1395 | } |