]>
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, |
1da2ed5f | 4 | 1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010 |
3c2426b9 | 5 | Free Software Foundation, Inc. |
8d08fdba MS |
6 | Hacked by Michael Tiemann (tiemann@cygnus.com) |
7 | ||
f5adbb8d | 8 | This file is part of GCC. |
8d08fdba | 9 | |
f5adbb8d | 10 | GCC is free software; you can redistribute it and/or modify |
8d08fdba | 11 | it under the terms of the GNU General Public License as published by |
e77f031d | 12 | the Free Software Foundation; either version 3, or (at your option) |
8d08fdba MS |
13 | any later version. |
14 | ||
f5adbb8d | 15 | GCC is distributed in the hope that it will be useful, |
8d08fdba MS |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | GNU General Public License for more details. | |
19 | ||
20 | You should have received a copy of the GNU General Public License | |
e77f031d NC |
21 | along with GCC; see the file COPYING3. If not see |
22 | <http://www.gnu.org/licenses/>. */ | |
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 | |
5088b058 | 28 | checks, and some optimization. */ |
8d08fdba MS |
29 | |
30 | #include "config.h" | |
8d052bc7 | 31 | #include "system.h" |
4977bab6 ZW |
32 | #include "coretypes.h" |
33 | #include "tm.h" | |
8d08fdba | 34 | #include "tree.h" |
4cd5a50a | 35 | #include "intl.h" |
8d08fdba MS |
36 | #include "cp-tree.h" |
37 | #include "flags.h" | |
71144a65 | 38 | #include "output.h" |
1da2ed5f | 39 | #include "diagnostic-core.h" |
8d08fdba | 40 | |
4038c495 GB |
41 | static tree |
42 | process_init_constructor (tree type, tree init); | |
43 | ||
8d08fdba | 44 | |
8d08fdba MS |
45 | /* Print an error message stemming from an attempt to use |
46 | BASETYPE as a base class for TYPE. */ | |
e92cc029 | 47 | |
8d08fdba | 48 | tree |
0a8cb79e | 49 | error_not_base_type (tree basetype, tree type) |
8d08fdba MS |
50 | { |
51 | if (TREE_CODE (basetype) == FUNCTION_DECL) | |
4f1c5b7d | 52 | basetype = DECL_CONTEXT (basetype); |
a82e1a7d | 53 | error ("type %qT is not a base type for type %qT", basetype, type); |
8d08fdba MS |
54 | return error_mark_node; |
55 | } | |
56 | ||
57 | tree | |
0a8cb79e | 58 | binfo_or_else (tree base, tree type) |
8d08fdba | 59 | { |
18e4be85 | 60 | tree binfo = lookup_base (type, base, ba_unique, NULL); |
2db1ab2d NS |
61 | |
62 | if (binfo == error_mark_node) | |
63 | return NULL_TREE; | |
64 | else if (!binfo) | |
65 | error_not_base_type (base, type); | |
66 | return binfo; | |
8d08fdba MS |
67 | } |
68 | ||
8d08fdba | 69 | /* According to ARM $7.1.6, "A `const' object may be initialized, but its |
fabb00fc | 70 | value may not be changed thereafter. */ |
e92cc029 | 71 | |
8d08fdba | 72 | void |
4816c593 | 73 | cxx_readonly_error (tree arg, enum lvalue_use errstring) |
8d08fdba | 74 | { |
4cd5a50a PB |
75 | |
76 | /* This macro is used to emit diagnostics to ensure that all format | |
77 | strings are complete sentences, visible to gettext and checked at | |
78 | compile time. */ | |
79 | ||
80 | #define ERROR_FOR_ASSIGNMENT(AS, ASM, IN, DE, ARG) \ | |
81 | do { \ | |
82 | switch (errstring) \ | |
83 | { \ | |
4816c593 | 84 | case lv_assign: \ |
4cd5a50a PB |
85 | error(AS, ARG); \ |
86 | break; \ | |
4816c593 | 87 | case lv_asm: \ |
4cd5a50a PB |
88 | error(ASM, ARG); \ |
89 | break; \ | |
4816c593 | 90 | case lv_increment: \ |
4cd5a50a PB |
91 | error (IN, ARG); \ |
92 | break; \ | |
4816c593 | 93 | case lv_decrement: \ |
4cd5a50a PB |
94 | error (DE, ARG); \ |
95 | break; \ | |
96 | default: \ | |
97 | gcc_unreachable (); \ | |
98 | } \ | |
99 | } while (0) | |
8d08fdba | 100 | |
4816c593 NF |
101 | /* Handle C++-specific things first. */ |
102 | ||
103 | if (TREE_CODE (arg) == VAR_DECL | |
104 | && DECL_LANG_SPECIFIC (arg) | |
105 | && DECL_IN_AGGR_P (arg) | |
106 | && !TREE_STATIC (arg)) | |
4cd5a50a | 107 | ERROR_FOR_ASSIGNMENT (G_("assignment of " |
4816c593 NF |
108 | "constant field %qD"), |
109 | G_("constant field %qD " | |
110 | "used as %<asm%> output"), | |
111 | G_("increment of " | |
112 | "constant field %qD"), | |
113 | G_("decrement of " | |
114 | "constant field %qD"), | |
115 | arg); | |
8d08fdba | 116 | else if (TREE_CODE (arg) == INDIRECT_REF |
0cbd7506 MS |
117 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REFERENCE_TYPE |
118 | && (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL | |
119 | || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL)) | |
4cd5a50a PB |
120 | ERROR_FOR_ASSIGNMENT (G_("assignment of " |
121 | "read-only reference %qD"), | |
4816c593 NF |
122 | G_("read-only reference %qD " |
123 | "used as %<asm%> output"), | |
4cd5a50a PB |
124 | G_("increment of " |
125 | "read-only reference %qD"), | |
126 | G_("decrement of " | |
127 | "read-only reference %qD"), | |
128 | TREE_OPERAND (arg, 0)); | |
69851283 | 129 | else |
4816c593 | 130 | readonly_error (arg, errstring); |
8d08fdba MS |
131 | } |
132 | ||
7fb213d8 GB |
133 | \f |
134 | /* Structure that holds information about declarations whose type was | |
135 | incomplete and we could not check whether it was abstract or not. */ | |
136 | ||
d1b38208 | 137 | struct GTY((chain_next ("%h.next"))) pending_abstract_type { |
7fb213d8 GB |
138 | /* Declaration which we are checking for abstractness. It is either |
139 | a DECL node, or an IDENTIFIER_NODE if we do not have a full | |
140 | declaration available. */ | |
141 | tree decl; | |
142 | ||
143 | /* Type which will be checked for abstractness. */ | |
144 | tree type; | |
145 | ||
146 | /* Position of the declaration. This is only needed for IDENTIFIER_NODEs, | |
147 | because DECLs already carry locus information. */ | |
148 | location_t locus; | |
149 | ||
150 | /* Link to the next element in list. */ | |
151 | struct pending_abstract_type* next; | |
152 | }; | |
153 | ||
154 | ||
155 | /* Compute the hash value of the node VAL. This function is used by the | |
156 | hash table abstract_pending_vars. */ | |
157 | ||
158 | static hashval_t | |
159 | pat_calc_hash (const void* val) | |
160 | { | |
67f5655f GDR |
161 | const struct pending_abstract_type *pat = |
162 | (const struct pending_abstract_type *) val; | |
7fb213d8 GB |
163 | return (hashval_t) TYPE_UID (pat->type); |
164 | } | |
165 | ||
166 | ||
167 | /* Compare node VAL1 with the type VAL2. This function is used by the | |
168 | hash table abstract_pending_vars. */ | |
169 | ||
170 | static int | |
171 | pat_compare (const void* val1, const void* val2) | |
172 | { | |
741ac903 | 173 | const struct pending_abstract_type *const pat1 = |
67f5655f | 174 | (const struct pending_abstract_type *) val1; |
741ac903 | 175 | const_tree const type2 = (const_tree)val2; |
7fb213d8 GB |
176 | |
177 | return (pat1->type == type2); | |
178 | } | |
179 | ||
180 | /* Hash table that maintains pending_abstract_type nodes, for which we still | |
181 | need to check for type abstractness. The key of the table is the type | |
182 | of the declaration. */ | |
183 | static GTY ((param_is (struct pending_abstract_type))) | |
184 | htab_t abstract_pending_vars = NULL; | |
185 | ||
186 | ||
187 | /* This function is called after TYPE is completed, and will check if there | |
188 | are pending declarations for which we still need to verify the abstractness | |
189 | of TYPE, and emit a diagnostic (through abstract_virtuals_error) if TYPE | |
190 | turned out to be incomplete. */ | |
191 | ||
192 | void | |
193 | complete_type_check_abstract (tree type) | |
194 | { | |
195 | void **slot; | |
196 | struct pending_abstract_type *pat; | |
197 | location_t cur_loc = input_location; | |
198 | ||
50bc768d | 199 | gcc_assert (COMPLETE_TYPE_P (type)); |
7fb213d8 GB |
200 | |
201 | if (!abstract_pending_vars) | |
202 | return; | |
203 | ||
204 | /* Retrieve the list of pending declarations for this type. */ | |
205 | slot = htab_find_slot_with_hash (abstract_pending_vars, type, | |
206 | (hashval_t)TYPE_UID (type), NO_INSERT); | |
207 | if (!slot) | |
208 | return; | |
209 | pat = (struct pending_abstract_type*)*slot; | |
50bc768d | 210 | gcc_assert (pat); |
7fb213d8 GB |
211 | |
212 | /* If the type is not abstract, do not do anything. */ | |
213 | if (CLASSTYPE_PURE_VIRTUALS (type)) | |
214 | { | |
215 | struct pending_abstract_type *prev = 0, *next; | |
216 | ||
217 | /* Reverse the list to emit the errors in top-down order. */ | |
218 | for (; pat; pat = next) | |
219 | { | |
220 | next = pat->next; | |
221 | pat->next = prev; | |
222 | prev = pat; | |
223 | } | |
224 | pat = prev; | |
225 | ||
226 | /* Go through the list, and call abstract_virtuals_error for each | |
77880ae4 | 227 | element: it will issue a diagnostic if the type is abstract. */ |
7fb213d8 GB |
228 | while (pat) |
229 | { | |
50bc768d | 230 | gcc_assert (type == pat->type); |
7fb213d8 GB |
231 | |
232 | /* Tweak input_location so that the diagnostic appears at the correct | |
233 | location. Notice that this is only needed if the decl is an | |
dee15844 | 234 | IDENTIFIER_NODE. */ |
7fb213d8 GB |
235 | input_location = pat->locus; |
236 | abstract_virtuals_error (pat->decl, pat->type); | |
237 | pat = pat->next; | |
238 | } | |
239 | } | |
240 | ||
241 | htab_clear_slot (abstract_pending_vars, slot); | |
242 | ||
243 | input_location = cur_loc; | |
244 | } | |
245 | ||
246 | ||
a7a64a77 MM |
247 | /* If TYPE has abstract virtual functions, issue an error about trying |
248 | to create an object of that type. DECL is the object declared, or | |
249 | NULL_TREE if the declaration is unavailable. Returns 1 if an error | |
250 | occurred; zero if all was well. */ | |
e92cc029 | 251 | |
a7a64a77 | 252 | int |
2b8497cd | 253 | abstract_virtuals_error_sfinae (tree decl, tree type, tsubst_flags_t complain) |
8d08fdba | 254 | { |
d4e6fecb | 255 | VEC(tree,gc) *pure; |
c8094d83 | 256 | |
7fb213d8 GB |
257 | /* This function applies only to classes. Any other entity can never |
258 | be abstract. */ | |
259 | if (!CLASS_TYPE_P (type)) | |
260 | return 0; | |
98fba7f7 | 261 | type = TYPE_MAIN_VARIANT (type); |
7fb213d8 GB |
262 | |
263 | /* If the type is incomplete, we register it within a hash table, | |
264 | so that we can check again once it is completed. This makes sense | |
265 | only for objects for which we have a declaration or at least a | |
266 | name. */ | |
267 | if (!COMPLETE_TYPE_P (type)) | |
268 | { | |
269 | void **slot; | |
270 | struct pending_abstract_type *pat; | |
271 | ||
c8094d83 | 272 | gcc_assert (!decl || DECL_P (decl) |
50bc768d | 273 | || TREE_CODE (decl) == IDENTIFIER_NODE); |
7fb213d8 GB |
274 | |
275 | if (!abstract_pending_vars) | |
c8094d83 | 276 | abstract_pending_vars = htab_create_ggc (31, &pat_calc_hash, |
7fb213d8 GB |
277 | &pat_compare, NULL); |
278 | ||
279 | slot = htab_find_slot_with_hash (abstract_pending_vars, type, | |
280 | (hashval_t)TYPE_UID (type), INSERT); | |
281 | ||
a9429e29 | 282 | pat = ggc_alloc_pending_abstract_type (); |
7fb213d8 GB |
283 | pat->type = type; |
284 | pat->decl = decl; | |
285 | pat->locus = ((decl && DECL_P (decl)) | |
286 | ? DECL_SOURCE_LOCATION (decl) | |
287 | : input_location); | |
288 | ||
67f5655f | 289 | pat->next = (struct pending_abstract_type *) *slot; |
7fb213d8 GB |
290 | *slot = pat; |
291 | ||
292 | return 0; | |
293 | } | |
294 | ||
e60505a5 NS |
295 | if (!TYPE_SIZE (type)) |
296 | /* TYPE is being defined, and during that time | |
297 | CLASSTYPE_PURE_VIRTUALS holds the inline friends. */ | |
298 | return 0; | |
299 | ||
585b44d3 NS |
300 | pure = CLASSTYPE_PURE_VIRTUALS (type); |
301 | if (!pure) | |
302 | return 0; | |
303 | ||
2b8497cd JM |
304 | if (decl && TREE_CODE (decl) == RESULT_DECL) |
305 | return 0; | |
306 | ||
307 | if (!(complain & tf_error)) | |
308 | return 1; | |
309 | ||
8d08fdba MS |
310 | if (decl) |
311 | { | |
8d08fdba | 312 | if (TREE_CODE (decl) == VAR_DECL) |
dee15844 JM |
313 | error ("cannot declare variable %q+D to be of abstract " |
314 | "type %qT", decl, type); | |
8d08fdba | 315 | else if (TREE_CODE (decl) == PARM_DECL) |
dee15844 JM |
316 | error ("cannot declare parameter %q+D to be of abstract type %qT", |
317 | decl, type); | |
8d08fdba | 318 | else if (TREE_CODE (decl) == FIELD_DECL) |
dee15844 JM |
319 | error ("cannot declare field %q+D to be of abstract type %qT", |
320 | decl, type); | |
8d08fdba MS |
321 | else if (TREE_CODE (decl) == FUNCTION_DECL |
322 | && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE) | |
dee15844 | 323 | error ("invalid abstract return type for member function %q+#D", decl); |
8d08fdba | 324 | else if (TREE_CODE (decl) == FUNCTION_DECL) |
dee15844 | 325 | error ("invalid abstract return type for function %q+#D", decl); |
7fb213d8 | 326 | else if (TREE_CODE (decl) == IDENTIFIER_NODE) |
dee15844 | 327 | /* Here we do not have location information. */ |
a82e1a7d | 328 | error ("invalid abstract type %qT for %qE", type, decl); |
da291c87 | 329 | else |
dee15844 | 330 | error ("invalid abstract type for %q+D", decl); |
8d08fdba | 331 | } |
4a67c9e9 | 332 | else |
a82e1a7d | 333 | error ("cannot allocate an object of abstract type %qT", type); |
4a67c9e9 | 334 | |
8d08fdba | 335 | /* Only go through this once. */ |
585b44d3 | 336 | if (VEC_length (tree, pure)) |
8d08fdba | 337 | { |
585b44d3 NS |
338 | unsigned ix; |
339 | tree fn; | |
c8094d83 | 340 | |
c5d75364 MLI |
341 | inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)), |
342 | " because the following virtual functions are pure within %qT:", | |
343 | type); | |
da291c87 | 344 | |
ac47786e | 345 | FOR_EACH_VEC_ELT (tree, pure, ix, fn) |
1f5b3869 | 346 | inform (input_location, "\t%+#D", fn); |
585b44d3 | 347 | /* Now truncate the vector. This leaves it non-null, so we know |
0cbd7506 MS |
348 | there are pure virtuals, but empty so we don't list them out |
349 | again. */ | |
585b44d3 | 350 | VEC_truncate (tree, pure, 0); |
8d08fdba | 351 | } |
4a67c9e9 | 352 | else |
c5d75364 MLI |
353 | inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)), |
354 | " since type %qT has pure virtual functions", | |
355 | type); | |
a7a64a77 MM |
356 | |
357 | return 1; | |
8d08fdba MS |
358 | } |
359 | ||
2b8497cd JM |
360 | /* Wrapper for the above function in the common case of wanting errors. */ |
361 | ||
362 | int | |
363 | abstract_virtuals_error (tree decl, tree type) | |
364 | { | |
365 | return abstract_virtuals_error_sfinae (decl, type, tf_warning_or_error); | |
366 | } | |
367 | ||
8d08fdba MS |
368 | /* Print an error message for invalid use of an incomplete type. |
369 | VALUE is the expression that was used (or 0 if that isn't known) | |
71205d17 MLI |
370 | and TYPE is the type that was invalid. DIAG_KIND indicates the |
371 | type of diagnostic (see diagnostic.def). */ | |
8d08fdba MS |
372 | |
373 | void | |
71205d17 MLI |
374 | cxx_incomplete_type_diagnostic (const_tree value, const_tree type, |
375 | diagnostic_t diag_kind) | |
8d08fdba | 376 | { |
146c8d60 | 377 | int decl = 0; |
23b4deba | 378 | |
71205d17 MLI |
379 | gcc_assert (diag_kind == DK_WARNING |
380 | || diag_kind == DK_PEDWARN | |
381 | || diag_kind == DK_ERROR); | |
c8094d83 | 382 | |
8d08fdba MS |
383 | /* Avoid duplicate error message. */ |
384 | if (TREE_CODE (type) == ERROR_MARK) | |
385 | return; | |
386 | ||
146c8d60 | 387 | if (value != 0 && (TREE_CODE (value) == VAR_DECL |
17f29637 KL |
388 | || TREE_CODE (value) == PARM_DECL |
389 | || TREE_CODE (value) == FIELD_DECL)) | |
146c8d60 | 390 | { |
71205d17 MLI |
391 | emit_diagnostic (diag_kind, input_location, 0, |
392 | "%q+D has incomplete type", value); | |
146c8d60 NS |
393 | decl = 1; |
394 | } | |
315fb5db | 395 | retry: |
66543169 NS |
396 | /* We must print an error message. Be clever about what it says. */ |
397 | ||
398 | switch (TREE_CODE (type)) | |
8d08fdba | 399 | { |
66543169 NS |
400 | case RECORD_TYPE: |
401 | case UNION_TYPE: | |
402 | case ENUMERAL_TYPE: | |
146c8d60 | 403 | if (!decl) |
71205d17 MLI |
404 | emit_diagnostic (diag_kind, input_location, 0, |
405 | "invalid use of incomplete type %q#T", type); | |
17f29637 | 406 | if (!TYPE_TEMPLATE_INFO (type)) |
71205d17 MLI |
407 | emit_diagnostic (diag_kind, input_location, 0, |
408 | "forward declaration of %q+#T", type); | |
17f29637 | 409 | else |
71205d17 MLI |
410 | emit_diagnostic (diag_kind, input_location, 0, |
411 | "declaration of %q+#T", type); | |
66543169 NS |
412 | break; |
413 | ||
414 | case VOID_TYPE: | |
71205d17 MLI |
415 | emit_diagnostic (diag_kind, input_location, 0, |
416 | "invalid use of %qT", type); | |
66543169 NS |
417 | break; |
418 | ||
419 | case ARRAY_TYPE: | |
420 | if (TYPE_DOMAIN (type)) | |
0cbd7506 MS |
421 | { |
422 | type = TREE_TYPE (type); | |
423 | goto retry; | |
424 | } | |
71205d17 MLI |
425 | emit_diagnostic (diag_kind, input_location, 0, |
426 | "invalid use of array with unspecified bounds"); | |
66543169 NS |
427 | break; |
428 | ||
429 | case OFFSET_TYPE: | |
430 | bad_member: | |
71205d17 MLI |
431 | emit_diagnostic (diag_kind, input_location, 0, |
432 | "invalid use of member (did you forget the %<&%> ?)"); | |
66543169 NS |
433 | break; |
434 | ||
435 | case TEMPLATE_TYPE_PARM: | |
86a09a9e JM |
436 | if (is_auto (type)) |
437 | emit_diagnostic (diag_kind, input_location, 0, | |
438 | "invalid use of %<auto%>"); | |
439 | else | |
440 | emit_diagnostic (diag_kind, input_location, 0, | |
441 | "invalid use of template type parameter %qT", type); | |
7acf7efa VR |
442 | break; |
443 | ||
444 | case BOUND_TEMPLATE_TEMPLATE_PARM: | |
71205d17 MLI |
445 | emit_diagnostic (diag_kind, input_location, 0, |
446 | "invalid use of template template parameter %qT", | |
447 | TYPE_NAME (type)); | |
66543169 NS |
448 | break; |
449 | ||
8fcd79cb | 450 | case TYPENAME_TYPE: |
71205d17 MLI |
451 | emit_diagnostic (diag_kind, input_location, 0, |
452 | "invalid use of dependent type %qT", type); | |
8fcd79cb MM |
453 | break; |
454 | ||
fbfc8363 JM |
455 | case LANG_TYPE: |
456 | gcc_assert (type == unknown_type_node); | |
66543169 | 457 | if (value && TREE_CODE (value) == COMPONENT_REF) |
0cbd7506 | 458 | goto bad_member; |
66543169 | 459 | else if (value && TREE_CODE (value) == ADDR_EXPR) |
71205d17 MLI |
460 | emit_diagnostic (diag_kind, input_location, 0, |
461 | "address of overloaded function with no contextual " | |
462 | "type information"); | |
66543169 | 463 | else if (value && TREE_CODE (value) == OVERLOAD) |
71205d17 MLI |
464 | emit_diagnostic (diag_kind, input_location, 0, |
465 | "overloaded function with no contextual type information"); | |
66543169 | 466 | else |
71205d17 MLI |
467 | emit_diagnostic (diag_kind, input_location, 0, |
468 | "insufficient contextual information to determine type"); | |
66543169 | 469 | break; |
c8094d83 | 470 | |
66543169 | 471 | default: |
315fb5db | 472 | gcc_unreachable (); |
8d08fdba MS |
473 | } |
474 | } | |
475 | ||
23b4deba AO |
476 | /* Backward-compatibility interface to incomplete_type_diagnostic; |
477 | required by ../tree.c. */ | |
478 | #undef cxx_incomplete_type_error | |
479 | void | |
ac7d7749 | 480 | cxx_incomplete_type_error (const_tree value, const_tree type) |
23b4deba | 481 | { |
71205d17 | 482 | cxx_incomplete_type_diagnostic (value, type, DK_ERROR); |
23b4deba AO |
483 | } |
484 | ||
8d08fdba | 485 | \f |
25ebb82a | 486 | /* The recursive part of split_nonconstant_init. DEST is an lvalue |
325c3691 | 487 | expression to which INIT should be assigned. INIT is a CONSTRUCTOR. */ |
25ebb82a | 488 | |
325c3691 | 489 | static void |
6addabbb | 490 | split_nonconstant_init_1 (tree dest, tree *initp) |
25ebb82a | 491 | { |
4038c495 | 492 | unsigned HOST_WIDE_INT idx; |
6addabbb | 493 | tree init = *initp; |
4038c495 GB |
494 | tree field_index, value; |
495 | tree type = TREE_TYPE (dest); | |
496 | tree inner_type = NULL; | |
25ebb82a | 497 | bool array_type_p = false; |
6addabbb | 498 | HOST_WIDE_INT num_type_elements, num_initialized_elements; |
25ebb82a | 499 | |
25ebb82a RH |
500 | switch (TREE_CODE (type)) |
501 | { | |
502 | case ARRAY_TYPE: | |
503 | inner_type = TREE_TYPE (type); | |
504 | array_type_p = true; | |
505 | /* FALLTHRU */ | |
506 | ||
507 | case RECORD_TYPE: | |
508 | case UNION_TYPE: | |
509 | case QUAL_UNION_TYPE: | |
6addabbb | 510 | num_initialized_elements = 0; |
4038c495 GB |
511 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx, |
512 | field_index, value) | |
25ebb82a | 513 | { |
4038c495 GB |
514 | /* The current implementation of this algorithm assumes that |
515 | the field was set for all the elements. This is usually done | |
516 | by process_init_constructor. */ | |
517 | gcc_assert (field_index); | |
25ebb82a RH |
518 | |
519 | if (!array_type_p) | |
520 | inner_type = TREE_TYPE (field_index); | |
521 | ||
522 | if (TREE_CODE (value) == CONSTRUCTOR) | |
523 | { | |
4038c495 GB |
524 | tree sub; |
525 | ||
25ebb82a | 526 | if (array_type_p) |
0cbd7506 | 527 | sub = build4 (ARRAY_REF, inner_type, dest, field_index, |
f293ce4b | 528 | NULL_TREE, NULL_TREE); |
25ebb82a | 529 | else |
0cbd7506 | 530 | sub = build3 (COMPONENT_REF, inner_type, dest, field_index, |
f293ce4b | 531 | NULL_TREE); |
25ebb82a | 532 | |
6addabbb | 533 | split_nonconstant_init_1 (sub, &value); |
25ebb82a RH |
534 | } |
535 | else if (!initializer_constant_valid_p (value, inner_type)) | |
536 | { | |
4038c495 GB |
537 | tree code; |
538 | tree sub; | |
6addabbb | 539 | HOST_WIDE_INT inner_elements; |
4038c495 GB |
540 | |
541 | /* FIXME: Ordered removal is O(1) so the whole function is | |
542 | worst-case quadratic. This could be fixed using an aside | |
543 | bitmap to record which elements must be removed and remove | |
544 | them all at the same time. Or by merging | |
545 | split_non_constant_init into process_init_constructor_array, | |
546 | that is separating constants from non-constants while building | |
547 | the vector. */ | |
548 | VEC_ordered_remove (constructor_elt, CONSTRUCTOR_ELTS (init), | |
549 | idx); | |
550 | --idx; | |
25ebb82a RH |
551 | |
552 | if (array_type_p) | |
0cbd7506 | 553 | sub = build4 (ARRAY_REF, inner_type, dest, field_index, |
f293ce4b | 554 | NULL_TREE, NULL_TREE); |
25ebb82a | 555 | else |
0cbd7506 | 556 | sub = build3 (COMPONENT_REF, inner_type, dest, field_index, |
f293ce4b | 557 | NULL_TREE); |
25ebb82a | 558 | |
dae7ec87 | 559 | code = build2 (INIT_EXPR, inner_type, sub, value); |
c2255bc4 | 560 | code = build_stmt (input_location, EXPR_STMT, code); |
325c3691 | 561 | add_stmt (code); |
6addabbb JZ |
562 | |
563 | inner_elements = count_type_elements (inner_type, true); | |
564 | if (inner_elements < 0) | |
565 | num_initialized_elements = -1; | |
566 | else if (num_initialized_elements >= 0) | |
567 | num_initialized_elements += inner_elements; | |
25ebb82a RH |
568 | continue; |
569 | } | |
25ebb82a | 570 | } |
6addabbb JZ |
571 | |
572 | num_type_elements = count_type_elements (type, true); | |
573 | /* If all elements of the initializer are non-constant and | |
574 | have been split out, we don't need the empty CONSTRUCTOR. */ | |
575 | if (num_type_elements > 0 | |
576 | && num_type_elements == num_initialized_elements) | |
577 | *initp = NULL; | |
25ebb82a RH |
578 | break; |
579 | ||
580 | case VECTOR_TYPE: | |
581 | if (!initializer_constant_valid_p (init, type)) | |
582 | { | |
4038c495 | 583 | tree code; |
74aad7cc | 584 | tree cons = copy_node (init); |
25ebb82a | 585 | CONSTRUCTOR_ELTS (init) = NULL; |
74aad7cc | 586 | code = build2 (MODIFY_EXPR, type, dest, cons); |
c2255bc4 | 587 | code = build_stmt (input_location, EXPR_STMT, code); |
325c3691 | 588 | add_stmt (code); |
25ebb82a RH |
589 | } |
590 | break; | |
591 | ||
592 | default: | |
315fb5db | 593 | gcc_unreachable (); |
25ebb82a | 594 | } |
b7af94d8 CL |
595 | |
596 | /* The rest of the initializer is now a constant. */ | |
597 | TREE_CONSTANT (init) = 1; | |
25ebb82a RH |
598 | } |
599 | ||
c8094d83 | 600 | /* A subroutine of store_init_value. Splits non-constant static |
25ebb82a RH |
601 | initializer INIT into a constant part and generates code to |
602 | perform the non-constant part of the initialization to DEST. | |
603 | Returns the code for the runtime init. */ | |
604 | ||
605 | static tree | |
606 | split_nonconstant_init (tree dest, tree init) | |
607 | { | |
608 | tree code; | |
609 | ||
610 | if (TREE_CODE (init) == CONSTRUCTOR) | |
611 | { | |
325c3691 | 612 | code = push_stmt_list (); |
6addabbb | 613 | split_nonconstant_init_1 (dest, &init); |
325c3691 | 614 | code = pop_stmt_list (code); |
25ebb82a | 615 | DECL_INITIAL (dest) = init; |
6407bc67 | 616 | TREE_READONLY (dest) = 0; |
25ebb82a RH |
617 | } |
618 | else | |
f293ce4b | 619 | code = build2 (INIT_EXPR, TREE_TYPE (dest), dest, init); |
25ebb82a RH |
620 | |
621 | return code; | |
622 | } | |
623 | ||
8d08fdba MS |
624 | /* Perform appropriate conversions on the initial value of a variable, |
625 | store it in the declaration DECL, | |
626 | and print any error messages that are appropriate. | |
627 | If the init is invalid, store an ERROR_MARK. | |
628 | ||
629 | C++: Note that INIT might be a TREE_LIST, which would mean that it is | |
630 | a base class initializer for some aggregate type, hopefully compatible | |
631 | with DECL. If INIT is a single element, and DECL is an aggregate | |
632 | type, we silently convert INIT into a TREE_LIST, allowing a constructor | |
633 | to be called. | |
634 | ||
635 | If INIT is a TREE_LIST and there is no constructor, turn INIT | |
636 | into a CONSTRUCTOR and use standard initialization techniques. | |
637 | Perhaps a warning should be generated? | |
638 | ||
25ebb82a RH |
639 | Returns code to be executed if initialization could not be performed |
640 | for static variable. In that case, caller must emit the code. */ | |
8d08fdba MS |
641 | |
642 | tree | |
e57d93c6 | 643 | store_init_value (tree decl, tree init, int flags) |
8d08fdba | 644 | { |
926ce8bd | 645 | tree value, type; |
8d08fdba MS |
646 | |
647 | /* If variable's type was invalidly declared, just ignore it. */ | |
648 | ||
649 | type = TREE_TYPE (decl); | |
650 | if (TREE_CODE (type) == ERROR_MARK) | |
651 | return NULL_TREE; | |
652 | ||
9e1e64ec | 653 | if (MAYBE_CLASS_TYPE_P (type)) |
8d08fdba | 654 | { |
6eabb241 | 655 | if (TREE_CODE (init) == TREE_LIST) |
8d08fdba | 656 | { |
a82e1a7d | 657 | error ("constructor syntax used, but no constructor declared " |
0cbd7506 | 658 | "for type %qT", type); |
09357846 | 659 | init = build_constructor_from_list (init_list_type_node, nreverse (init)); |
8d08fdba | 660 | } |
8d08fdba MS |
661 | } |
662 | else if (TREE_CODE (init) == TREE_LIST | |
663 | && TREE_TYPE (init) != unknown_type_node) | |
664 | { | |
2f5b91f5 SZ |
665 | gcc_assert (TREE_CODE (decl) != RESULT_DECL); |
666 | ||
667 | if (TREE_CODE (init) == TREE_LIST | |
8d08fdba MS |
668 | && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE) |
669 | { | |
8251199e | 670 | error ("cannot initialize arrays using this syntax"); |
8d08fdba MS |
671 | return NULL_TREE; |
672 | } | |
673 | else | |
c7b62f14 | 674 | /* We get here with code like `int a (2);' */ |
d555b1c7 PC |
675 | init = build_x_compound_expr_from_list (init, ELK_INIT, |
676 | tf_warning_or_error); | |
8d08fdba MS |
677 | } |
678 | ||
679 | /* End of special C++ code. */ | |
680 | ||
fa2200cb JM |
681 | if (flags & LOOKUP_ALREADY_DIGESTED) |
682 | value = init; | |
683 | else | |
684 | /* Digest the specified initializer into an expression. */ | |
685 | value = digest_init_flags (type, init, flags); | |
686 | ||
687 | /* In C++0x constant expression is a semantic, not syntactic, property. | |
688 | In C++98, make sure that what we thought was a constant expression at | |
689 | template definition time is still constant. */ | |
690 | if ((cxx_dialect >= cxx0x | |
691 | || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)) | |
692 | && (decl_maybe_constant_var_p (decl) | |
693 | || TREE_STATIC (decl))) | |
694 | { | |
695 | bool const_init; | |
696 | value = fold_non_dependent_expr (value); | |
697 | value = maybe_constant_init (value); | |
698 | if (DECL_DECLARED_CONSTEXPR_P (decl)) | |
699 | /* Diagnose a non-constant initializer for constexpr. */ | |
700 | value = cxx_constant_value (value); | |
701 | const_init = (reduced_constant_expression_p (value) | |
702 | || error_operand_p (value)); | |
703 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = const_init; | |
704 | TREE_CONSTANT (decl) = const_init && decl_maybe_constant_var_p (decl); | |
705 | } | |
706 | ||
80439563 MM |
707 | /* If the initializer is not a constant, fill in DECL_INITIAL with |
708 | the bits that are constant, and then return an expression that | |
709 | will perform the dynamic initialization. */ | |
710 | if (value != error_mark_node | |
711 | && (TREE_SIDE_EFFECTS (value) | |
712 | || ! initializer_constant_valid_p (value, TREE_TYPE (value)))) | |
25ebb82a | 713 | return split_nonconstant_init (decl, value); |
80439563 MM |
714 | /* If the value is a constant, just put it in DECL_INITIAL. If DECL |
715 | is an automatic variable, the middle end will turn this into a | |
716 | dynamic initialization later. */ | |
8d08fdba MS |
717 | DECL_INITIAL (decl) = value; |
718 | return NULL_TREE; | |
719 | } | |
94e6e4c4 | 720 | |
8d08fdba | 721 | \f |
09357846 JM |
722 | /* Give errors about narrowing conversions within { }. */ |
723 | ||
724 | void | |
725 | check_narrowing (tree type, tree init) | |
726 | { | |
2d727f75 | 727 | tree ftype = unlowered_expr_type (init); |
09357846 JM |
728 | bool ok = true; |
729 | REAL_VALUE_TYPE d; | |
730 | ||
35385f99 JM |
731 | if (!ARITHMETIC_TYPE_P (type)) |
732 | return; | |
733 | ||
fa2200cb | 734 | init = maybe_constant_value (init); |
09357846 JM |
735 | |
736 | if (TREE_CODE (type) == INTEGER_TYPE | |
737 | && TREE_CODE (ftype) == REAL_TYPE) | |
738 | ok = false; | |
739 | else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype) | |
740 | && CP_INTEGRAL_TYPE_P (type)) | |
741 | { | |
742 | if (TYPE_PRECISION (type) < TYPE_PRECISION (ftype) | |
743 | && (TREE_CODE (init) != INTEGER_CST | |
744 | || !int_fits_type_p (init, type))) | |
745 | ok = false; | |
746 | } | |
747 | else if (TREE_CODE (ftype) == REAL_TYPE | |
748 | && TREE_CODE (type) == REAL_TYPE) | |
749 | { | |
750 | if (TYPE_PRECISION (type) < TYPE_PRECISION (ftype)) | |
751 | { | |
09357846 JM |
752 | if (TREE_CODE (init) == REAL_CST) |
753 | { | |
72258929 JM |
754 | /* Issue 703: Loss of precision is OK as long as the value is |
755 | within the representable range of the new type. */ | |
756 | REAL_VALUE_TYPE r; | |
09357846 | 757 | d = TREE_REAL_CST (init); |
72258929 JM |
758 | real_convert (&r, TYPE_MODE (type), &d); |
759 | if (real_isinf (&r)) | |
760 | ok = false; | |
09357846 | 761 | } |
72258929 JM |
762 | else |
763 | ok = false; | |
09357846 JM |
764 | } |
765 | } | |
766 | else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype) | |
767 | && TREE_CODE (type) == REAL_TYPE) | |
768 | { | |
769 | ok = false; | |
770 | if (TREE_CODE (init) == INTEGER_CST) | |
771 | { | |
772 | d = real_value_from_int_cst (0, init); | |
773 | if (exact_real_truncate (TYPE_MODE (type), &d)) | |
774 | ok = true; | |
775 | } | |
776 | } | |
777 | ||
778 | if (!ok) | |
2d727f75 JM |
779 | permerror (input_location, "narrowing conversion of %qE from %qT to %qT inside { }", |
780 | init, ftype, type); | |
09357846 JM |
781 | } |
782 | ||
4038c495 GB |
783 | /* Process the initializer INIT for a variable of type TYPE, emitting |
784 | diagnostics for invalid initializers and converting the initializer as | |
785 | appropriate. | |
8d08fdba | 786 | |
4038c495 | 787 | For aggregate types, it assumes that reshape_init has already run, thus the |
09357846 | 788 | initializer will have the right shape (brace elision has been undone). |
8d08fdba | 789 | |
09357846 JM |
790 | NESTED is true iff we are being called for an element of a CONSTRUCTOR. */ |
791 | ||
792 | static tree | |
e57d93c6 | 793 | digest_init_r (tree type, tree init, bool nested, int flags) |
8d08fdba MS |
794 | { |
795 | enum tree_code code = TREE_CODE (type); | |
8d08fdba | 796 | |
97471c71 | 797 | if (error_operand_p (init)) |
8d08fdba MS |
798 | return error_mark_node; |
799 | ||
4038c495 | 800 | gcc_assert (init); |
b8b98c66 NS |
801 | |
802 | /* We must strip the outermost array type when completing the type, | |
803 | because the its bounds might be incomplete at the moment. */ | |
804 | if (!complete_type_or_else (TREE_CODE (type) == ARRAY_TYPE | |
805 | ? TREE_TYPE (type) : type, NULL_TREE)) | |
806 | return error_mark_node; | |
c8094d83 | 807 | |
4038c495 GB |
808 | /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue |
809 | (g++.old-deja/g++.law/casts2.C). */ | |
8d08fdba MS |
810 | if (TREE_CODE (init) == NON_LVALUE_EXPR) |
811 | init = TREE_OPERAND (init, 0); | |
812 | ||
4038c495 GB |
813 | /* Initialization of an array of chars from a string constant. The initializer |
814 | can be optionally enclosed in braces, but reshape_init has already removed | |
815 | them if they were present. */ | |
8d08fdba MS |
816 | if (code == ARRAY_TYPE) |
817 | { | |
4038c495 | 818 | tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type)); |
7b019c19 | 819 | if (char_type_p (typ1) |
4038c495 GB |
820 | /*&& init */ |
821 | && TREE_CODE (init) == STRING_CST) | |
8d08fdba | 822 | { |
4038c495 | 823 | tree char_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init))); |
8d08fdba | 824 | |
c466b2cd | 825 | if (TYPE_PRECISION (typ1) == BITS_PER_UNIT) |
8d08fdba | 826 | { |
c466b2cd KVH |
827 | if (char_type != char_type_node) |
828 | { | |
829 | error ("char-array initialized from wide string"); | |
830 | return error_mark_node; | |
831 | } | |
8d08fdba | 832 | } |
c466b2cd | 833 | else |
8d08fdba | 834 | { |
c466b2cd KVH |
835 | if (char_type == char_type_node) |
836 | { | |
837 | error ("int-array initialized from non-wide string"); | |
838 | return error_mark_node; | |
839 | } | |
840 | else if (char_type != typ1) | |
841 | { | |
842 | error ("int-array initialized from incompatible wide string"); | |
843 | return error_mark_node; | |
844 | } | |
8d08fdba MS |
845 | } |
846 | ||
4038c495 GB |
847 | TREE_TYPE (init) = type; |
848 | if (TYPE_DOMAIN (type) != 0 && TREE_CONSTANT (TYPE_SIZE (type))) | |
8d08fdba | 849 | { |
926ce8bd | 850 | int size = TREE_INT_CST_LOW (TYPE_SIZE (type)); |
8d08fdba MS |
851 | size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT; |
852 | /* In C it is ok to subtract 1 from the length of the string | |
853 | because it's ok to ignore the terminating null char that is | |
854 | counted in the length of the constant, but in C++ this would | |
855 | be invalid. */ | |
4038c495 | 856 | if (size < TREE_STRING_LENGTH (init)) |
cbe5f3b3 | 857 | permerror (input_location, "initializer-string for array of chars is too long"); |
8d08fdba | 858 | } |
4038c495 | 859 | return init; |
8d08fdba MS |
860 | } |
861 | } | |
862 | ||
3c955a04 | 863 | /* Handle scalar types (including conversions) and references. */ |
a36c33eb JJ |
864 | if ((TREE_CODE (type) != COMPLEX_TYPE |
865 | || BRACE_ENCLOSED_INITIALIZER_P (init)) | |
b8063b29 | 866 | && (SCALAR_TYPE_P (type) || code == REFERENCE_TYPE)) |
6524147c OW |
867 | { |
868 | tree *exp; | |
869 | ||
09357846 JM |
870 | if (cxx_dialect != cxx98 && nested) |
871 | check_narrowing (type, init); | |
e57d93c6 | 872 | init = convert_for_initialization (0, type, init, flags, |
2f5b91f5 | 873 | ICR_INIT, NULL_TREE, 0, |
e57d93c6 | 874 | tf_warning_or_error); |
6524147c OW |
875 | exp = &init; |
876 | ||
877 | /* Skip any conversions since we'll be outputting the underlying | |
878 | constant. */ | |
63a906f0 | 879 | while (CONVERT_EXPR_P (*exp) |
6524147c OW |
880 | || TREE_CODE (*exp) == NON_LVALUE_EXPR) |
881 | exp = &TREE_OPERAND (*exp, 0); | |
882 | ||
883 | *exp = cplus_expand_constant (*exp); | |
884 | ||
885 | return init; | |
886 | } | |
4038c495 GB |
887 | |
888 | /* Come here only for aggregates: records, arrays, unions, complex numbers | |
889 | and vectors. */ | |
890 | gcc_assert (TREE_CODE (type) == ARRAY_TYPE | |
891 | || TREE_CODE (type) == VECTOR_TYPE | |
892 | || TREE_CODE (type) == RECORD_TYPE | |
893 | || TREE_CODE (type) == UNION_TYPE | |
894 | || TREE_CODE (type) == COMPLEX_TYPE); | |
895 | ||
fc94bfc5 JM |
896 | if (BRACE_ENCLOSED_INITIALIZER_P (init) |
897 | && !TYPE_NON_AGGREGATE_CLASS (type)) | |
09357846 | 898 | return process_init_constructor (type, init); |
4038c495 | 899 | else |
8d08fdba | 900 | { |
3b2db49f | 901 | if (COMPOUND_LITERAL_P (init) && TREE_CODE (type) == ARRAY_TYPE) |
8d08fdba | 902 | { |
4038c495 GB |
903 | error ("cannot initialize aggregate of type %qT with " |
904 | "a compound literal", type); | |
8d08fdba | 905 | |
4038c495 GB |
906 | return error_mark_node; |
907 | } | |
8e76c2bf MS |
908 | |
909 | if (TREE_CODE (type) == ARRAY_TYPE | |
c6569cd0 | 910 | && !BRACE_ENCLOSED_INITIALIZER_P (init)) |
8e76c2bf | 911 | { |
c6569cd0 JM |
912 | /* Allow the result of build_array_copy and of |
913 | build_value_init_noctor. */ | |
914 | if ((TREE_CODE (init) == TARGET_EXPR | |
915 | || TREE_CODE (init) == CONSTRUCTOR) | |
d5f4eddd JM |
916 | && (same_type_ignoring_top_level_qualifiers_p |
917 | (type, TREE_TYPE (init)))) | |
918 | return init; | |
919 | ||
8e76c2bf MS |
920 | error ("array must be initialized with a brace-enclosed" |
921 | " initializer"); | |
922 | return error_mark_node; | |
923 | } | |
924 | ||
4038c495 | 925 | return convert_for_initialization (NULL_TREE, type, init, |
e57d93c6 | 926 | flags, |
2f5b91f5 | 927 | ICR_INIT, NULL_TREE, 0, |
5ade1ed2 | 928 | tf_warning_or_error); |
8d08fdba | 929 | } |
4038c495 GB |
930 | } |
931 | ||
09357846 JM |
932 | tree |
933 | digest_init (tree type, tree init) | |
934 | { | |
e57d93c6 JM |
935 | return digest_init_r (type, init, false, LOOKUP_IMPLICIT); |
936 | } | |
937 | ||
938 | tree | |
939 | digest_init_flags (tree type, tree init, int flags) | |
940 | { | |
941 | return digest_init_r (type, init, false, flags); | |
09357846 | 942 | } |
4038c495 GB |
943 | \f |
944 | /* Set of flags used within process_init_constructor to describe the | |
945 | initializers. */ | |
946 | #define PICFLAG_ERRONEOUS 1 | |
947 | #define PICFLAG_NOT_ALL_CONSTANT 2 | |
948 | #define PICFLAG_NOT_ALL_SIMPLE 4 | |
949 | ||
950 | /* Given an initializer INIT, return the flag (PICFLAG_*) which better | |
951 | describe it. */ | |
952 | ||
953 | static int | |
954 | picflag_from_initializer (tree init) | |
955 | { | |
956 | if (init == error_mark_node) | |
957 | return PICFLAG_ERRONEOUS; | |
958 | else if (!TREE_CONSTANT (init)) | |
959 | return PICFLAG_NOT_ALL_CONSTANT; | |
960 | else if (!initializer_constant_valid_p (init, TREE_TYPE (init))) | |
961 | return PICFLAG_NOT_ALL_SIMPLE; | |
962 | return 0; | |
963 | } | |
8d08fdba | 964 | |
4038c495 | 965 | /* Subroutine of process_init_constructor, which will process an initializer |
d732e98f KH |
966 | INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*) |
967 | which describe the initializers. */ | |
8d08fdba | 968 | |
4038c495 GB |
969 | static int |
970 | process_init_constructor_array (tree type, tree init) | |
971 | { | |
972 | unsigned HOST_WIDE_INT i, len = 0; | |
973 | int flags = 0; | |
974 | bool unbounded = false; | |
975 | constructor_elt *ce; | |
976 | VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (init); | |
977 | ||
978 | gcc_assert (TREE_CODE (type) == ARRAY_TYPE | |
979 | || TREE_CODE (type) == VECTOR_TYPE); | |
980 | ||
981 | if (TREE_CODE (type) == ARRAY_TYPE) | |
8d08fdba | 982 | { |
4038c495 GB |
983 | tree domain = TYPE_DOMAIN (type); |
984 | if (domain) | |
985 | len = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain)) | |
986 | - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain)) | |
987 | + 1); | |
988 | else | |
989 | unbounded = true; /* Take as many as there are. */ | |
8d08fdba | 990 | } |
4038c495 GB |
991 | else |
992 | /* Vectors are like simple fixed-size arrays. */ | |
993 | len = TYPE_VECTOR_SUBPARTS (type); | |
8d08fdba | 994 | |
25357d1e JM |
995 | /* There must not be more initializers than needed. */ |
996 | if (!unbounded && VEC_length (constructor_elt, v) > len) | |
997 | error ("too many initializers for %qT", type); | |
4038c495 | 998 | |
ac47786e | 999 | FOR_EACH_VEC_ELT (constructor_elt, v, i, ce) |
8d08fdba | 1000 | { |
4038c495 | 1001 | if (ce->index) |
8d08fdba | 1002 | { |
4038c495 GB |
1003 | gcc_assert (TREE_CODE (ce->index) == INTEGER_CST); |
1004 | if (compare_tree_int (ce->index, i) != 0) | |
e135a637 SE |
1005 | { |
1006 | ce->value = error_mark_node; | |
1007 | sorry ("non-trivial designated initializers not supported"); | |
1008 | } | |
8d08fdba | 1009 | } |
4038c495 GB |
1010 | else |
1011 | ce->index = size_int (i); | |
1012 | gcc_assert (ce->value); | |
e57d93c6 | 1013 | ce->value = digest_init_r (TREE_TYPE (type), ce->value, true, LOOKUP_IMPLICIT); |
d22c8596 | 1014 | |
4038c495 GB |
1015 | if (ce->value != error_mark_node) |
1016 | gcc_assert (same_type_ignoring_top_level_qualifiers_p | |
1017 | (TREE_TYPE (type), TREE_TYPE (ce->value))); | |
dc26f471 | 1018 | |
4038c495 | 1019 | flags |= picflag_from_initializer (ce->value); |
8d08fdba MS |
1020 | } |
1021 | ||
4038c495 GB |
1022 | /* No more initializers. If the array is unbounded, we are done. Otherwise, |
1023 | we must add initializers ourselves. */ | |
1024 | if (!unbounded) | |
1025 | for (; i < len; ++i) | |
1026 | { | |
1027 | tree next; | |
1028 | ||
1029 | if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type))) | |
1030 | { | |
1031 | /* If this type needs constructors run for default-initialization, | |
3b426391 | 1032 | we can't rely on the back end to do it for us, so build up |
4038c495 GB |
1033 | TARGET_EXPRs. If the type in question is a class, just build |
1034 | one up; if it's an array, recurse. */ | |
9e1e64ec | 1035 | if (MAYBE_CLASS_TYPE_P (TREE_TYPE (type))) |
5ade1ed2 DG |
1036 | next = build_functional_cast (TREE_TYPE (type), NULL_TREE, |
1037 | tf_warning_or_error); | |
4038c495 | 1038 | else |
09357846 | 1039 | next = build_constructor (init_list_type_node, NULL); |
4038c495 GB |
1040 | next = digest_init (TREE_TYPE (type), next); |
1041 | } | |
1042 | else if (!zero_init_p (TREE_TYPE (type))) | |
1043 | next = build_zero_init (TREE_TYPE (type), | |
1044 | /*nelts=*/NULL_TREE, | |
1045 | /*static_storage_p=*/false); | |
1046 | else | |
1047 | /* The default zero-initialization is fine for us; don't | |
1048 | add anything to the CONSTRUCTOR. */ | |
1049 | break; | |
1050 | ||
3db45ab5 | 1051 | flags |= picflag_from_initializer (next); |
4038c495 GB |
1052 | CONSTRUCTOR_APPEND_ELT (v, size_int (i), next); |
1053 | } | |
1054 | ||
1055 | CONSTRUCTOR_ELTS (init) = v; | |
1056 | return flags; | |
8d08fdba | 1057 | } |
8d08fdba | 1058 | |
4038c495 GB |
1059 | /* Subroutine of process_init_constructor, which will process an initializer |
1060 | INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe | |
1061 | the initializers. */ | |
8d08fdba | 1062 | |
4038c495 GB |
1063 | static int |
1064 | process_init_constructor_record (tree type, tree init) | |
8d08fdba | 1065 | { |
4038c495 GB |
1066 | VEC(constructor_elt,gc) *v = NULL; |
1067 | int flags = 0; | |
1068 | tree field; | |
1069 | unsigned HOST_WIDE_INT idx = 0; | |
1070 | ||
1071 | gcc_assert (TREE_CODE (type) == RECORD_TYPE); | |
1072 | gcc_assert (!CLASSTYPE_VBASECLASSES (type)); | |
1073 | gcc_assert (!TYPE_BINFO (type) | |
1074 | || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type))); | |
1075 | gcc_assert (!TYPE_POLYMORPHIC_P (type)); | |
1076 | ||
1077 | /* Generally, we will always have an index for each initializer (which is | |
1078 | a FIELD_DECL, put by reshape_init), but compound literals don't go trough | |
1079 | reshape_init. So we need to handle both cases. */ | |
910ad8de | 1080 | for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) |
8d08fdba | 1081 | { |
4038c495 | 1082 | tree next; |
2d727f75 | 1083 | tree type; |
8d08fdba | 1084 | |
4038c495 | 1085 | if (!DECL_NAME (field) && DECL_C_BIT_FIELD (field)) |
ca4feb54 | 1086 | { |
4038c495 GB |
1087 | flags |= picflag_from_initializer (integer_zero_node); |
1088 | CONSTRUCTOR_APPEND_ELT (v, field, integer_zero_node); | |
1089 | continue; | |
ca4feb54 | 1090 | } |
8d08fdba | 1091 | |
4038c495 GB |
1092 | if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field)) |
1093 | continue; | |
1094 | ||
2d727f75 JM |
1095 | /* If this is a bitfield, first convert to the declared type. */ |
1096 | type = TREE_TYPE (field); | |
1097 | if (DECL_BIT_FIELD_TYPE (field)) | |
1098 | type = DECL_BIT_FIELD_TYPE (field); | |
1099 | ||
4038c495 | 1100 | if (idx < VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init))) |
8d08fdba | 1101 | { |
4038c495 GB |
1102 | constructor_elt *ce = VEC_index (constructor_elt, |
1103 | CONSTRUCTOR_ELTS (init), idx); | |
1104 | if (ce->index) | |
8d08fdba | 1105 | { |
4038c495 GB |
1106 | /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The |
1107 | latter case can happen in templates where lookup has to be | |
1108 | deferred. */ | |
1109 | gcc_assert (TREE_CODE (ce->index) == FIELD_DECL | |
1110 | || TREE_CODE (ce->index) == IDENTIFIER_NODE); | |
1111 | if (ce->index != field | |
3db45ab5 | 1112 | && ce->index != DECL_NAME (field)) |
e135a637 SE |
1113 | { |
1114 | ce->value = error_mark_node; | |
1115 | sorry ("non-trivial designated initializers not supported"); | |
1116 | } | |
8d08fdba | 1117 | } |
e6267549 | 1118 | |
4038c495 | 1119 | gcc_assert (ce->value); |
e57d93c6 | 1120 | next = digest_init_r (type, ce->value, true, LOOKUP_IMPLICIT); |
4038c495 GB |
1121 | ++idx; |
1122 | } | |
1123 | else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field))) | |
1124 | { | |
1125 | /* If this type needs constructors run for | |
3b426391 | 1126 | default-initialization, we can't rely on the back end to do it |
4038c495 GB |
1127 | for us, so build up TARGET_EXPRs. If the type in question is |
1128 | a class, just build one up; if it's an array, recurse. */ | |
4c9b3895 | 1129 | next = build_constructor (init_list_type_node, NULL); |
9e1e64ec | 1130 | if (MAYBE_CLASS_TYPE_P (TREE_TYPE (field))) |
7c094c11 | 1131 | { |
4c9b3895 | 1132 | next = finish_compound_literal (TREE_TYPE (field), next); |
7c094c11 DS |
1133 | /* direct-initialize the target. No temporary is going |
1134 | to be involved. */ | |
1135 | if (TREE_CODE (next) == TARGET_EXPR) | |
1136 | TARGET_EXPR_DIRECT_INIT_P (next) = true; | |
1137 | } | |
4038c495 | 1138 | |
e57d93c6 | 1139 | next = digest_init_r (TREE_TYPE (field), next, true, LOOKUP_IMPLICIT); |
4038c495 GB |
1140 | |
1141 | /* Warn when some struct elements are implicitly initialized. */ | |
1142 | warning (OPT_Wmissing_field_initializers, | |
1143 | "missing initializer for member %qD", field); | |
1144 | } | |
1145 | else | |
1146 | { | |
1147 | if (TREE_READONLY (field)) | |
1148 | error ("uninitialized const member %qD", field); | |
1149 | else if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field))) | |
1150 | error ("member %qD with uninitialized const fields", field); | |
1151 | else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE) | |
1152 | error ("member %qD is uninitialized reference", field); | |
1153 | ||
1154 | /* Warn when some struct elements are implicitly initialized | |
1155 | to zero. */ | |
1156 | warning (OPT_Wmissing_field_initializers, | |
1157 | "missing initializer for member %qD", field); | |
1158 | ||
1159 | if (!zero_init_p (TREE_TYPE (field))) | |
1160 | next = build_zero_init (TREE_TYPE (field), /*nelts=*/NULL_TREE, | |
1161 | /*static_storage_p=*/false); | |
e6267549 JM |
1162 | else |
1163 | /* The default zero-initialization is fine for us; don't | |
4038c495 GB |
1164 | add anything to the CONSTRUCTOR. */ |
1165 | continue; | |
8d08fdba | 1166 | } |
4038c495 | 1167 | |
2d727f75 JM |
1168 | /* If this is a bitfield, now convert to the lowered type. */ |
1169 | if (type != TREE_TYPE (field)) | |
1170 | next = cp_convert_and_check (TREE_TYPE (field), next); | |
4038c495 GB |
1171 | flags |= picflag_from_initializer (next); |
1172 | CONSTRUCTOR_APPEND_ELT (v, field, next); | |
8d08fdba | 1173 | } |
8d08fdba | 1174 | |
fc94bfc5 JM |
1175 | if (idx < VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init))) |
1176 | error ("too many initializers for %qT", type); | |
1177 | ||
4038c495 GB |
1178 | CONSTRUCTOR_ELTS (init) = v; |
1179 | return flags; | |
1180 | } | |
8d08fdba | 1181 | |
4038c495 | 1182 | /* Subroutine of process_init_constructor, which will process a single |
13a44ee0 | 1183 | initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*) |
4038c495 | 1184 | which describe the initializer. */ |
8d08fdba | 1185 | |
4038c495 GB |
1186 | static int |
1187 | process_init_constructor_union (tree type, tree init) | |
1188 | { | |
1189 | constructor_elt *ce; | |
fc94bfc5 | 1190 | int len; |
8d08fdba | 1191 | |
4038c495 GB |
1192 | /* If the initializer was empty, use default zero initialization. */ |
1193 | if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init))) | |
1194 | return 0; | |
1195 | ||
fc94bfc5 JM |
1196 | len = VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init)); |
1197 | if (len > 1) | |
1198 | { | |
1199 | error ("too many initializers for %qT", type); | |
1200 | VEC_block_remove (constructor_elt, CONSTRUCTOR_ELTS (init), 1, len-1); | |
1201 | } | |
1202 | ||
4038c495 GB |
1203 | ce = VEC_index (constructor_elt, CONSTRUCTOR_ELTS (init), 0); |
1204 | ||
1205 | /* If this element specifies a field, initialize via that field. */ | |
1206 | if (ce->index) | |
1207 | { | |
1208 | if (TREE_CODE (ce->index) == FIELD_DECL) | |
1209 | ; | |
1210 | else if (TREE_CODE (ce->index) == IDENTIFIER_NODE) | |
1211 | { | |
1212 | /* This can happen within a cast, see g++.dg/opt/cse2.C. */ | |
1213 | tree name = ce->index; | |
1214 | tree field; | |
1215 | for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) | |
1216 | if (DECL_NAME (field) == name) | |
1217 | break; | |
1218 | if (!field) | |
8d08fdba | 1219 | { |
4038c495 GB |
1220 | error ("no field %qD found in union being initialized", field); |
1221 | ce->value = error_mark_node; | |
8d08fdba | 1222 | } |
4038c495 GB |
1223 | ce->index = field; |
1224 | } | |
1225 | else | |
1226 | { | |
1227 | gcc_assert (TREE_CODE (ce->index) == INTEGER_CST | |
1228 | || TREE_CODE (ce->index) == RANGE_EXPR); | |
1229 | error ("index value instead of field name in union initializer"); | |
1230 | ce->value = error_mark_node; | |
8d08fdba | 1231 | } |
8d08fdba | 1232 | } |
4038c495 | 1233 | else |
8d08fdba | 1234 | { |
8d08fdba MS |
1235 | /* Find the first named field. ANSI decided in September 1990 |
1236 | that only named fields count here. */ | |
4038c495 | 1237 | tree field = TYPE_FIELDS (type); |
17bbb839 | 1238 | while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL)) |
8d08fdba | 1239 | field = TREE_CHAIN (field); |
9bfea41b JM |
1240 | if (field == NULL_TREE) |
1241 | { | |
1242 | error ("too many initializers for %qT", type); | |
1243 | ce->value = error_mark_node; | |
1244 | } | |
4038c495 GB |
1245 | ce->index = field; |
1246 | } | |
8d08fdba | 1247 | |
4038c495 | 1248 | if (ce->value && ce->value != error_mark_node) |
e57d93c6 | 1249 | ce->value = digest_init_r (TREE_TYPE (ce->index), ce->value, true, LOOKUP_IMPLICIT); |
8d08fdba | 1250 | |
4038c495 GB |
1251 | return picflag_from_initializer (ce->value); |
1252 | } | |
8d08fdba | 1253 | |
4038c495 GB |
1254 | /* Process INIT, a constructor for a variable of aggregate type TYPE. The |
1255 | constructor is a brace-enclosed initializer, and will be modified in-place. | |
1256 | ||
1257 | Each element is converted to the right type through digest_init, and | |
1258 | missing initializers are added following the language rules (zero-padding, | |
1259 | etc.). | |
8d08fdba | 1260 | |
4038c495 GB |
1261 | After the execution, the initializer will have TREE_CONSTANT if all elts are |
1262 | constant, and TREE_STATIC set if, in addition, all elts are simple enough | |
1263 | constants that the assembler and linker can compute them. | |
3db45ab5 | 1264 | |
4038c495 GB |
1265 | The function returns the initializer itself, or error_mark_node in case |
1266 | of error. */ | |
1267 | ||
1268 | static tree | |
1269 | process_init_constructor (tree type, tree init) | |
1270 | { | |
1271 | int flags; | |
1272 | ||
1273 | gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init)); | |
1274 | ||
1275 | if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE) | |
1276 | flags = process_init_constructor_array (type, init); | |
1277 | else if (TREE_CODE (type) == RECORD_TYPE) | |
1278 | flags = process_init_constructor_record (type, init); | |
1279 | else if (TREE_CODE (type) == UNION_TYPE) | |
1280 | flags = process_init_constructor_union (type, init); | |
1281 | else | |
1282 | gcc_unreachable (); | |
8d08fdba | 1283 | |
4038c495 | 1284 | if (flags & PICFLAG_ERRONEOUS) |
8d08fdba MS |
1285 | return error_mark_node; |
1286 | ||
4038c495 | 1287 | TREE_TYPE (init) = type; |
8c081e84 | 1288 | if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE) |
4038c495 GB |
1289 | cp_complete_array_type (&TREE_TYPE (init), init, /*do_default=*/0); |
1290 | if (!(flags & PICFLAG_NOT_ALL_CONSTANT)) | |
6de9cd9a | 1291 | { |
4038c495 | 1292 | TREE_CONSTANT (init) = 1; |
4038c495 GB |
1293 | if (!(flags & PICFLAG_NOT_ALL_SIMPLE)) |
1294 | TREE_STATIC (init) = 1; | |
6de9cd9a | 1295 | } |
4038c495 | 1296 | return init; |
8d08fdba MS |
1297 | } |
1298 | \f | |
1299 | /* Given a structure or union value DATUM, construct and return | |
1300 | the structure or union component which results from narrowing | |
a29e1034 | 1301 | that value to the base specified in BASETYPE. For example, given the |
8d08fdba MS |
1302 | hierarchy |
1303 | ||
1304 | class L { int ii; }; | |
1305 | class A : L { ... }; | |
1306 | class B : L { ... }; | |
1307 | class C : A, B { ... }; | |
1308 | ||
1309 | and the declaration | |
1310 | ||
1311 | C x; | |
1312 | ||
1313 | then the expression | |
1314 | ||
be99da77 | 1315 | x.A::ii refers to the ii member of the L part of |
38e01259 | 1316 | the A part of the C object named by X. In this case, |
aa52c1ff JM |
1317 | DATUM would be x, and BASETYPE would be A. |
1318 | ||
477f6664 JM |
1319 | I used to think that this was nonconformant, that the standard specified |
1320 | that first we look up ii in A, then convert x to an L& and pull out the | |
1321 | ii part. But in fact, it does say that we convert x to an A&; A here | |
a29e1034 JM |
1322 | is known as the "naming class". (jason 2000-12-19) |
1323 | ||
1324 | BINFO_P points to a variable initialized either to NULL_TREE or to the | |
1325 | binfo for the specific base subobject we want to convert to. */ | |
8d08fdba MS |
1326 | |
1327 | tree | |
0a8cb79e | 1328 | build_scoped_ref (tree datum, tree basetype, tree* binfo_p) |
8d08fdba | 1329 | { |
338d90b8 | 1330 | tree binfo; |
8d08fdba MS |
1331 | |
1332 | if (datum == error_mark_node) | |
1333 | return error_mark_node; | |
a29e1034 JM |
1334 | if (*binfo_p) |
1335 | binfo = *binfo_p; | |
1336 | else | |
1337 | binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check, NULL); | |
8d08fdba | 1338 | |
a29e1034 JM |
1339 | if (!binfo || binfo == error_mark_node) |
1340 | { | |
1341 | *binfo_p = NULL_TREE; | |
1342 | if (!binfo) | |
1343 | error_not_base_type (basetype, TREE_TYPE (datum)); | |
1344 | return error_mark_node; | |
1345 | } | |
8d08fdba | 1346 | |
a29e1034 JM |
1347 | *binfo_p = binfo; |
1348 | return build_base_path (PLUS_EXPR, datum, binfo, 1); | |
8d08fdba MS |
1349 | } |
1350 | ||
1351 | /* Build a reference to an object specified by the C++ `->' operator. | |
1352 | Usually this just involves dereferencing the object, but if the | |
1353 | `->' operator is overloaded, then such overloads must be | |
1354 | performed until an object which does not have the `->' operator | |
1355 | overloaded is found. An error is reported when circular pointer | |
1356 | delegation is detected. */ | |
e92cc029 | 1357 | |
8d08fdba | 1358 | tree |
d17811fd | 1359 | build_x_arrow (tree expr) |
8d08fdba | 1360 | { |
d17811fd | 1361 | tree orig_expr = expr; |
d17811fd | 1362 | tree type = TREE_TYPE (expr); |
a703fb38 | 1363 | tree last_rval = NULL_TREE; |
f038ec69 | 1364 | VEC(tree,gc) *types_memoized = NULL; |
8d08fdba MS |
1365 | |
1366 | if (type == error_mark_node) | |
1367 | return error_mark_node; | |
1368 | ||
5156628f | 1369 | if (processing_template_decl) |
d17811fd MM |
1370 | { |
1371 | if (type_dependent_expression_p (expr)) | |
1372 | return build_min_nt (ARROW_EXPR, expr); | |
1373 | expr = build_non_dependent_expr (expr); | |
1374 | } | |
5566b478 | 1375 | |
9e1e64ec | 1376 | if (MAYBE_CLASS_TYPE_P (type)) |
8d08fdba | 1377 | { |
d17811fd | 1378 | while ((expr = build_new_op (COMPONENT_REF, LOOKUP_NORMAL, expr, |
ec835fb2 | 1379 | NULL_TREE, NULL_TREE, |
5ade1ed2 DG |
1380 | /*overloaded_p=*/NULL, |
1381 | tf_warning_or_error))) | |
8d08fdba | 1382 | { |
d17811fd | 1383 | if (expr == error_mark_node) |
8d08fdba MS |
1384 | return error_mark_node; |
1385 | ||
bfdb7b70 NF |
1386 | if (vec_member (TREE_TYPE (expr), types_memoized)) |
1387 | { | |
1388 | error ("circular pointer delegation detected"); | |
1389 | return error_mark_node; | |
1390 | } | |
f038ec69 NF |
1391 | |
1392 | VEC_safe_push (tree, gc, types_memoized, TREE_TYPE (expr)); | |
d17811fd | 1393 | last_rval = expr; |
c8094d83 | 1394 | } |
297dcfb3 MM |
1395 | |
1396 | if (last_rval == NULL_TREE) | |
1397 | { | |
a82e1a7d | 1398 | error ("base operand of %<->%> has non-pointer type %qT", type); |
297dcfb3 MM |
1399 | return error_mark_node; |
1400 | } | |
1401 | ||
8d08fdba MS |
1402 | if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE) |
1403 | last_rval = convert_from_reference (last_rval); | |
1404 | } | |
1405 | else | |
d17811fd | 1406 | last_rval = decay_conversion (expr); |
8d08fdba | 1407 | |
8d08fdba | 1408 | if (TREE_CODE (TREE_TYPE (last_rval)) == POINTER_TYPE) |
d17811fd MM |
1409 | { |
1410 | if (processing_template_decl) | |
8e1daa34 NS |
1411 | { |
1412 | expr = build_min_non_dep (ARROW_EXPR, last_rval, orig_expr); | |
04c06002 | 1413 | /* It will be dereferenced. */ |
8e1daa34 NS |
1414 | TREE_TYPE (expr) = TREE_TYPE (TREE_TYPE (last_rval)); |
1415 | return expr; | |
1416 | } | |
d17811fd | 1417 | |
dd865ef6 | 1418 | return cp_build_indirect_ref (last_rval, RO_NULL, tf_warning_or_error); |
d17811fd | 1419 | } |
8d08fdba | 1420 | |
8d08fdba | 1421 | if (types_memoized) |
a82e1a7d | 1422 | error ("result of %<operator->()%> yields non-pointer result"); |
8d08fdba | 1423 | else |
a82e1a7d | 1424 | error ("base operand of %<->%> is not a pointer"); |
8d08fdba MS |
1425 | return error_mark_node; |
1426 | } | |
1427 | ||
d6b4ea85 MM |
1428 | /* Return an expression for "DATUM .* COMPONENT". DATUM has not |
1429 | already been checked out to be of aggregate type. */ | |
e92cc029 | 1430 | |
8d08fdba | 1431 | tree |
0a8cb79e | 1432 | build_m_component_ref (tree datum, tree component) |
8d08fdba | 1433 | { |
d6b4ea85 | 1434 | tree ptrmem_type; |
c3e899c1 | 1435 | tree objtype; |
d6b4ea85 | 1436 | tree type; |
71851aaa | 1437 | tree binfo; |
cad7e87b | 1438 | tree ctype; |
8d08fdba | 1439 | |
a8fe5a30 | 1440 | if (error_operand_p (datum) || error_operand_p (component)) |
f1a3f197 MM |
1441 | return error_mark_node; |
1442 | ||
416f380b JJ |
1443 | datum = mark_lvalue_use (datum); |
1444 | component = mark_rvalue_use (component); | |
87867ff6 | 1445 | |
d6b4ea85 | 1446 | ptrmem_type = TREE_TYPE (component); |
a5ac359a | 1447 | if (!TYPE_PTR_TO_MEMBER_P (ptrmem_type)) |
8d08fdba | 1448 | { |
a82e1a7d | 1449 | error ("%qE cannot be used as a member pointer, since it is of " |
0cbd7506 | 1450 | "type %qT", |
d6b4ea85 | 1451 | component, ptrmem_type); |
8d08fdba MS |
1452 | return error_mark_node; |
1453 | } | |
c8094d83 MS |
1454 | |
1455 | objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum)); | |
9e1e64ec | 1456 | if (! MAYBE_CLASS_TYPE_P (objtype)) |
51c184be | 1457 | { |
a82e1a7d | 1458 | error ("cannot apply member pointer %qE to %qE, which is of " |
41990f96 | 1459 | "non-class type %qT", |
0cbd7506 | 1460 | component, datum, objtype); |
51c184be MS |
1461 | return error_mark_node; |
1462 | } | |
71851aaa | 1463 | |
d6b4ea85 | 1464 | type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type); |
cad7e87b NS |
1465 | ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type)); |
1466 | ||
1467 | if (!COMPLETE_TYPE_P (ctype)) | |
8d08fdba | 1468 | { |
cad7e87b NS |
1469 | if (!same_type_p (ctype, objtype)) |
1470 | goto mismatch; | |
1471 | binfo = NULL; | |
1472 | } | |
1473 | else | |
1474 | { | |
1475 | binfo = lookup_base (objtype, ctype, ba_check, NULL); | |
c8094d83 | 1476 | |
cad7e87b NS |
1477 | if (!binfo) |
1478 | { | |
1479 | mismatch: | |
a82e1a7d | 1480 | error ("pointer to member type %qT incompatible with object " |
0cbd7506 | 1481 | "type %qT", |
cad7e87b NS |
1482 | type, objtype); |
1483 | return error_mark_node; | |
1484 | } | |
1485 | else if (binfo == error_mark_node) | |
1486 | return error_mark_node; | |
8d08fdba MS |
1487 | } |
1488 | ||
d6b4ea85 MM |
1489 | if (TYPE_PTRMEM_P (ptrmem_type)) |
1490 | { | |
b5119fa1 RG |
1491 | tree ptype; |
1492 | ||
d6b4ea85 MM |
1493 | /* Compute the type of the field, as described in [expr.ref]. |
1494 | There's no such thing as a mutable pointer-to-member, so | |
1495 | things are not as complex as they are for references to | |
1496 | non-static data members. */ | |
1497 | type = cp_build_qualified_type (type, | |
c8094d83 | 1498 | (cp_type_quals (type) |
d6b4ea85 | 1499 | | cp_type_quals (TREE_TYPE (datum)))); |
cad7e87b NS |
1500 | |
1501 | datum = build_address (datum); | |
c8094d83 | 1502 | |
cad7e87b NS |
1503 | /* Convert object to the correct base. */ |
1504 | if (binfo) | |
1505 | datum = build_base_path (PLUS_EXPR, datum, binfo, 1); | |
c8094d83 | 1506 | |
a5ac359a MM |
1507 | /* Build an expression for "object + offset" where offset is the |
1508 | value stored in the pointer-to-data-member. */ | |
b5119fa1 RG |
1509 | ptype = build_pointer_type (type); |
1510 | datum = build2 (POINTER_PLUS_EXPR, ptype, | |
1511 | fold_convert (ptype, datum), | |
1512 | build_nop (sizetype, component)); | |
7ad86035 | 1513 | return cp_build_indirect_ref (datum, RO_NULL, tf_warning_or_error); |
d6b4ea85 MM |
1514 | } |
1515 | else | |
f293ce4b | 1516 | return build2 (OFFSET_REF, type, datum, component); |
8d08fdba MS |
1517 | } |
1518 | ||
fc378698 | 1519 | /* Return a tree node for the expression TYPENAME '(' PARMS ')'. */ |
e92cc029 | 1520 | |
8d08fdba | 1521 | tree |
5ade1ed2 | 1522 | build_functional_cast (tree exp, tree parms, tsubst_flags_t complain) |
8d08fdba MS |
1523 | { |
1524 | /* This is either a call to a constructor, | |
1525 | or a C cast in C++'s `functional' notation. */ | |
0fcedd9c JM |
1526 | |
1527 | /* The type to which we are casting. */ | |
fc378698 | 1528 | tree type; |
c166b898 | 1529 | VEC(tree,gc) *parmvec; |
8d08fdba MS |
1530 | |
1531 | if (exp == error_mark_node || parms == error_mark_node) | |
1532 | return error_mark_node; | |
1533 | ||
4b0d3cbe | 1534 | if (TREE_CODE (exp) == TYPE_DECL) |
45537677 | 1535 | type = TREE_TYPE (exp); |
8d08fdba MS |
1536 | else |
1537 | type = exp; | |
1538 | ||
d67a3e2a AP |
1539 | if (TREE_CODE (type) == REFERENCE_TYPE && !parms) |
1540 | { | |
2b8497cd JM |
1541 | if (complain & tf_error) |
1542 | error ("invalid value-initialization of reference type"); | |
d67a3e2a AP |
1543 | return error_mark_node; |
1544 | } | |
1545 | ||
5156628f | 1546 | if (processing_template_decl) |
8e1daa34 NS |
1547 | { |
1548 | tree t = build_min (CAST_EXPR, type, parms); | |
1549 | /* We don't know if it will or will not have side effects. */ | |
1550 | TREE_SIDE_EFFECTS (t) = 1; | |
1551 | return t; | |
1552 | } | |
5566b478 | 1553 | |
9e1e64ec | 1554 | if (! MAYBE_CLASS_TYPE_P (type)) |
8d08fdba | 1555 | { |
8d08fdba | 1556 | if (parms == NULL_TREE) |
2b8497cd | 1557 | return build_value_init (type, complain); |
8ccc31eb | 1558 | |
f0b99d6c | 1559 | /* This must build a C cast. */ |
d555b1c7 | 1560 | parms = build_x_compound_expr_from_list (parms, ELK_FUNC_CAST, complain); |
525521b6 | 1561 | return cp_build_c_cast (type, parms, complain); |
8d08fdba MS |
1562 | } |
1563 | ||
45537677 MS |
1564 | /* Prepare to evaluate as a call to a constructor. If this expression |
1565 | is actually used, for example, | |
c8094d83 | 1566 | |
45537677 | 1567 | return X (arg1, arg2, ...); |
c8094d83 | 1568 | |
45537677 MS |
1569 | then the slot being initialized will be filled in. */ |
1570 | ||
309714d4 | 1571 | if (!complete_type_or_maybe_complain (type, NULL_TREE, complain)) |
d0f062fb | 1572 | return error_mark_node; |
2b8497cd | 1573 | if (abstract_virtuals_error_sfinae (NULL_TREE, type, complain)) |
a7a64a77 | 1574 | return error_mark_node; |
8d08fdba | 1575 | |
0fcedd9c JM |
1576 | /* [expr.type.conv] |
1577 | ||
1578 | If the expression list is a single-expression, the type | |
1579 | conversion is equivalent (in definedness, and if defined in | |
1580 | meaning) to the corresponding cast expression. */ | |
8d08fdba | 1581 | if (parms && TREE_CHAIN (parms) == NULL_TREE) |
525521b6 | 1582 | return cp_build_c_cast (type, TREE_VALUE (parms), complain); |
8d08fdba | 1583 | |
0fcedd9c JM |
1584 | /* [expr.type.conv] |
1585 | ||
1586 | The expression T(), where T is a simple-type-specifier for a | |
1587 | non-array complete object type or the (possibly cv-qualified) | |
1588 | void type, creates an rvalue of the specified type, which is | |
1589 | value-initialized. */ | |
1590 | ||
1591 | if (parms == NULL_TREE | |
1592 | /* If there's a user-defined constructor, value-initialization is | |
1593 | just calling the constructor, so fall through. */ | |
1594 | && !TYPE_HAS_USER_CONSTRUCTOR (type)) | |
3551c177 | 1595 | { |
309714d4 | 1596 | exp = build_value_init (type, complain); |
fa2200cb JM |
1597 | exp = get_target_expr (exp); |
1598 | /* FIXME this is wrong */ | |
1599 | if (literal_type_p (type)) | |
1600 | TREE_CONSTANT (exp) = true; | |
1601 | return exp; | |
3551c177 JM |
1602 | } |
1603 | ||
0fcedd9c | 1604 | /* Call the constructor. */ |
c166b898 ILT |
1605 | parmvec = make_tree_vector (); |
1606 | for (; parms != NULL_TREE; parms = TREE_CHAIN (parms)) | |
1607 | VEC_safe_push (tree, gc, parmvec, TREE_VALUE (parms)); | |
1608 | exp = build_special_member_call (NULL_TREE, complete_ctor_identifier, | |
1609 | &parmvec, type, LOOKUP_NORMAL, complain); | |
1610 | release_tree_vector (parmvec); | |
8d08fdba | 1611 | |
fc378698 | 1612 | if (exp == error_mark_node) |
a0a33927 | 1613 | return error_mark_node; |
8d08fdba | 1614 | |
fc378698 | 1615 | return build_cplus_new (type, exp); |
8d08fdba MS |
1616 | } |
1617 | \f | |
46b02c6d | 1618 | |
4cc1d462 NS |
1619 | /* Add new exception specifier SPEC, to the LIST we currently have. |
1620 | If it's already in LIST then do nothing. | |
1621 | Moan if it's bad and we're allowed to. COMPLAIN < 0 means we | |
1622 | know what we're doing. */ | |
1623 | ||
1624 | tree | |
0a8cb79e | 1625 | add_exception_specifier (tree list, tree spec, int complain) |
4cc1d462 | 1626 | { |
ef09717a | 1627 | bool ok; |
4cc1d462 | 1628 | tree core = spec; |
ef09717a | 1629 | bool is_ptr; |
71205d17 | 1630 | diagnostic_t diag_type = DK_UNSPECIFIED; /* none */ |
c8094d83 | 1631 | |
4cc1d462 NS |
1632 | if (spec == error_mark_node) |
1633 | return list; | |
c8094d83 | 1634 | |
50bc768d | 1635 | gcc_assert (spec && (!list || TREE_VALUE (list))); |
c8094d83 | 1636 | |
4cc1d462 NS |
1637 | /* [except.spec] 1, type in an exception specifier shall not be |
1638 | incomplete, or pointer or ref to incomplete other than pointer | |
1639 | to cv void. */ | |
1640 | is_ptr = TREE_CODE (core) == POINTER_TYPE; | |
1641 | if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE) | |
1642 | core = TREE_TYPE (core); | |
1643 | if (complain < 0) | |
ef09717a | 1644 | ok = true; |
b72801e2 | 1645 | else if (VOID_TYPE_P (core)) |
4cc1d462 NS |
1646 | ok = is_ptr; |
1647 | else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM) | |
ef09717a | 1648 | ok = true; |
baeb4732 | 1649 | else if (processing_template_decl) |
ef09717a | 1650 | ok = true; |
4cc1d462 | 1651 | else |
5aa3396c | 1652 | { |
ef09717a | 1653 | ok = true; |
5aa3396c | 1654 | /* 15.4/1 says that types in an exception specifier must be complete, |
0cbd7506 MS |
1655 | but it seems more reasonable to only require this on definitions |
1656 | and calls. So just give a pedwarn at this point; we will give an | |
1657 | error later if we hit one of those two cases. */ | |
5aa3396c | 1658 | if (!COMPLETE_TYPE_P (complete_type (core))) |
71205d17 | 1659 | diag_type = DK_PEDWARN; /* pedwarn */ |
5aa3396c | 1660 | } |
baeb4732 | 1661 | |
4cc1d462 NS |
1662 | if (ok) |
1663 | { | |
1664 | tree probe; | |
c8094d83 | 1665 | |
4cc1d462 | 1666 | for (probe = list; probe; probe = TREE_CHAIN (probe)) |
0cbd7506 MS |
1667 | if (same_type_p (TREE_VALUE (probe), spec)) |
1668 | break; | |
4cc1d462 | 1669 | if (!probe) |
80b1331c | 1670 | list = tree_cons (NULL_TREE, spec, list); |
4cc1d462 | 1671 | } |
5aa3396c | 1672 | else |
71205d17 | 1673 | diag_type = DK_ERROR; /* error */ |
c8094d83 | 1674 | |
71205d17 | 1675 | if (diag_type != DK_UNSPECIFIED && complain) |
5aa3396c JM |
1676 | cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type); |
1677 | ||
4cc1d462 NS |
1678 | return list; |
1679 | } | |
03378143 NS |
1680 | |
1681 | /* Combine the two exceptions specifier lists LIST and ADD, and return | |
c6002625 | 1682 | their union. */ |
03378143 NS |
1683 | |
1684 | tree | |
0a8cb79e | 1685 | merge_exception_specifiers (tree list, tree add) |
03378143 NS |
1686 | { |
1687 | if (!list || !add) | |
1688 | return NULL_TREE; | |
247078ec | 1689 | /* For merging noexcept(true) and throw(), take the more recent one (LIST). |
3a55fb4c JM |
1690 | A throw(type-list) spec takes precedence over a noexcept(false) spec. |
1691 | Any other noexcept-spec should only be merged with an equivalent one. | |
247078ec | 1692 | So the !TREE_VALUE code below is correct for all cases. */ |
03378143 NS |
1693 | else if (!TREE_VALUE (add)) |
1694 | return list; | |
247078ec JM |
1695 | else if (!TREE_VALUE (list)) |
1696 | return add; | |
03378143 NS |
1697 | else |
1698 | { | |
1699 | tree orig_list = list; | |
c8094d83 | 1700 | |
03378143 | 1701 | for (; add; add = TREE_CHAIN (add)) |
0cbd7506 MS |
1702 | { |
1703 | tree spec = TREE_VALUE (add); | |
1704 | tree probe; | |
1705 | ||
1706 | for (probe = orig_list; probe; probe = TREE_CHAIN (probe)) | |
1707 | if (same_type_p (TREE_VALUE (probe), spec)) | |
1708 | break; | |
1709 | if (!probe) | |
1710 | { | |
1711 | spec = build_tree_list (NULL_TREE, spec); | |
1712 | TREE_CHAIN (spec) = list; | |
1713 | list = spec; | |
1714 | } | |
1715 | } | |
03378143 NS |
1716 | } |
1717 | return list; | |
1718 | } | |
5aa3396c JM |
1719 | |
1720 | /* Subroutine of build_call. Ensure that each of the types in the | |
1721 | exception specification is complete. Technically, 15.4/1 says that | |
1722 | they need to be complete when we see a declaration of the function, | |
1723 | but we should be able to get away with only requiring this when the | |
1724 | function is defined or called. See also add_exception_specifier. */ | |
1725 | ||
1726 | void | |
0a8cb79e | 1727 | require_complete_eh_spec_types (tree fntype, tree decl) |
5aa3396c JM |
1728 | { |
1729 | tree raises; | |
1730 | /* Don't complain about calls to op new. */ | |
1731 | if (decl && DECL_ARTIFICIAL (decl)) | |
1732 | return; | |
1733 | for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises; | |
1734 | raises = TREE_CHAIN (raises)) | |
1735 | { | |
1736 | tree type = TREE_VALUE (raises); | |
1737 | if (type && !COMPLETE_TYPE_P (type)) | |
1738 | { | |
1739 | if (decl) | |
1740 | error | |
a82e1a7d | 1741 | ("call to function %qD which throws incomplete type %q#T", |
5aa3396c JM |
1742 | decl, type); |
1743 | else | |
a82e1a7d | 1744 | error ("call to function which throws incomplete type %q#T", |
5aa3396c JM |
1745 | decl); |
1746 | } | |
1747 | } | |
1748 | } | |
7fb213d8 GB |
1749 | |
1750 | \f | |
1751 | #include "gt-cp-typeck2.h" |