Commit | Line | Data |
---|---|---|
6aed4dcc C |
1 | /* Report error messages, build initializers, and perform |
2 | some front-end optimizations for C++ compiler. | |
3 | Copyright (C) 1987, 1988, 1989, 1992 Free Software Foundation, Inc. | |
4 | Hacked by Michael Tiemann (tiemann@cygnus.com) | |
5 | ||
6 | This file is part of GNU CC. | |
7 | ||
8 | GNU CC is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2, or (at your option) | |
11 | any later version. | |
12 | ||
13 | GNU CC is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with GNU CC; see the file COPYING. If not, write to | |
20 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
21 | ||
22 | ||
23 | /* This file is part of the C++ front end. | |
24 | It contains routines to build C++ expressions given their operands, | |
25 | including computing the types of the result, C and C++ specific error | |
26 | checks, and some optimization. | |
27 | ||
28 | There are also routines to build RETURN_STMT nodes and CASE_STMT nodes, | |
29 | and to process initializations in declarations (since they work | |
30 | like a strange sort of assignment). */ | |
31 | ||
32 | #include "config.h" | |
33 | #include <stdio.h> | |
34 | #include "tree.h" | |
35 | #include "cp-tree.h" | |
36 | #include "flags.h" | |
37 | ||
38 | static tree process_init_constructor (); | |
39 | tree digest_init (); | |
40 | void incomplete_type_error (); | |
41 | void readonly_warning_or_error (); | |
42 | extern tree convert_for_initialization (); | |
43 | ||
44 | extern int errorcount; | |
45 | extern int sorrycount; | |
46 | ||
47 | /* Print an error message stemming from an attempt to use | |
48 | BASETYPE as a base class for TYPE. */ | |
49 | tree | |
50 | error_not_base_type (basetype, type) | |
51 | tree basetype, type; | |
52 | { | |
53 | tree name1; | |
54 | tree name2; | |
55 | if (TREE_CODE (basetype) == FUNCTION_DECL) | |
56 | basetype = DECL_CLASS_CONTEXT (basetype); | |
57 | name1 = TYPE_NAME (basetype); | |
58 | name2 = TYPE_NAME (type); | |
59 | if (TREE_CODE (name1) == TYPE_DECL) | |
60 | name1 = DECL_NAME (name1); | |
61 | if (TREE_CODE (name2) == TYPE_DECL) | |
62 | name2 = DECL_NAME (name2); | |
63 | error ("type `%s' is not a base type for type `%s'", | |
64 | IDENTIFIER_POINTER (name1), IDENTIFIER_POINTER (name2)); | |
65 | return error_mark_node; | |
66 | } | |
67 | ||
68 | tree | |
69 | binfo_or_else (parent_or_type, type) | |
70 | tree parent_or_type, type; | |
71 | { | |
72 | tree binfo; | |
73 | if (TYPE_MAIN_VARIANT (parent_or_type) == TYPE_MAIN_VARIANT (type)) | |
74 | return parent_or_type; | |
75 | if (binfo = get_binfo (parent_or_type, TYPE_MAIN_VARIANT (type), 0)) | |
76 | { | |
77 | if (binfo == error_mark_node) | |
78 | return NULL_TREE; | |
79 | return binfo; | |
80 | } | |
81 | error_not_base_type (parent_or_type, type); | |
82 | return NULL_TREE; | |
83 | } | |
84 | ||
85 | /* Print an error message stemming from an invalid use of an | |
86 | aggregate type. | |
87 | ||
88 | TYPE is the type or binfo which draws the error. | |
89 | MSG is the message to print. | |
90 | ARG is an optional argument which may provide more information. */ | |
91 | void | |
92 | error_with_aggr_type (type, msg, arg) | |
93 | tree type; | |
94 | char *msg; | |
95 | int arg; | |
96 | { | |
97 | tree name; | |
98 | ||
99 | if (TREE_CODE (type) == TREE_VEC) | |
100 | type = BINFO_TYPE (type); | |
101 | ||
102 | name = TYPE_NAME (type); | |
103 | if (TREE_CODE (name) == TYPE_DECL) | |
104 | name = DECL_NAME (name); | |
105 | error (msg, IDENTIFIER_POINTER (name), arg); | |
106 | } | |
107 | ||
108 | /* Warn or give error about storing in something that is `const'. */ | |
109 | /* for now, because the compiler is getting pickier about const'ness | |
110 | give users a non-fatal warning instead of an error, this will | |
111 | revert back to a hard error after a release or two. This is | |
112 | to not screw the user that has valid code for which the compiler | |
113 | does not treat it right. */ | |
114 | #if 0 | |
115 | #define const_error error | |
116 | #else | |
117 | #define const_error pedwarn | |
118 | #endif | |
119 | ||
120 | void | |
121 | readonly_warning_or_error (arg, string) | |
122 | tree arg; | |
123 | char *string; | |
124 | { | |
125 | char *fmt; | |
126 | ||
127 | if (TREE_CODE (arg) == COMPONENT_REF) | |
128 | { | |
129 | if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0)))) | |
130 | fmt = "%s of member `%s' in read-only structure"; | |
131 | else | |
132 | fmt = "%s of read-only member `%s'"; | |
133 | const_error (fmt, string, lang_printable_name (TREE_OPERAND (arg, 1))); | |
134 | } | |
135 | else if (TREE_CODE (arg) == VAR_DECL) | |
136 | { | |
137 | if (DECL_LANG_SPECIFIC (arg) | |
138 | && DECL_IN_AGGR_P (arg) | |
139 | && !TREE_STATIC (arg)) | |
140 | fmt = "%s of constant field `%s'"; | |
141 | else | |
142 | fmt = "%s of read-only variable `%s'"; | |
143 | const_error (fmt, string, lang_printable_name (arg)); | |
144 | } | |
145 | else if (TREE_CODE (arg) == PARM_DECL) | |
146 | { | |
147 | const_error ("%s of read-only parameter `%s'", | |
148 | string, lang_printable_name (arg)); | |
149 | } | |
150 | else if (TREE_CODE (arg) == INDIRECT_REF | |
151 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REFERENCE_TYPE | |
152 | && (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL | |
153 | || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL)) | |
154 | { | |
155 | const_error ("%s of read-only reference `%s'", | |
156 | string, lang_printable_name (TREE_OPERAND (arg, 0))); | |
157 | } | |
158 | else | |
159 | { | |
160 | const_error ("%s of read-only location", string); | |
161 | } | |
162 | } | |
163 | ||
164 | /* Print an error message for invalid use of a type which declares | |
165 | virtual functions which are not inheritable. */ | |
166 | void | |
167 | abstract_virtuals_error (decl, type) | |
168 | tree decl; | |
169 | tree type; | |
170 | { | |
171 | char *typename = TYPE_NAME_STRING (type); | |
172 | tree u = CLASSTYPE_ABSTRACT_VIRTUALS (type); | |
173 | ||
174 | if (decl) | |
175 | { | |
176 | if (TREE_CODE (decl) == RESULT_DECL) | |
177 | return; | |
178 | ||
179 | if (TREE_CODE (decl) == VAR_DECL) | |
180 | error_with_decl (decl, "cannot declare variable `%s' to be of type `%s'", typename); | |
181 | else if (TREE_CODE (decl) == PARM_DECL) | |
182 | error_with_decl (decl, "cannot declare parameter `%s' to be of type `%s'", typename); | |
183 | else if (TREE_CODE (decl) == FIELD_DECL) | |
184 | error_with_decl (decl, "cannot declare field `%s' to be of type `%s'", typename); | |
185 | else if (TREE_CODE (decl) == FUNCTION_DECL | |
186 | && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE) | |
187 | error_with_decl (decl, "invalid return type for method `%s'"); | |
188 | else if (TREE_CODE (decl) == FUNCTION_DECL) | |
189 | error_with_decl (decl, "invalid return type for function `%s'"); | |
190 | } | |
191 | else error ("cannot allocate an object of type `%s'", typename); | |
192 | /* Only go through this once. */ | |
193 | if (TREE_PURPOSE (u) == NULL_TREE) | |
194 | { | |
195 | error ("since the following virtual functions are abstract:"); | |
196 | TREE_PURPOSE (u) = error_mark_node; | |
197 | while (u) | |
198 | { | |
199 | error_with_decl (TREE_VALUE (u), "%s"); | |
200 | u = TREE_CHAIN (u); | |
201 | } | |
202 | } | |
203 | else error ("since type `%s' has abstract virtual functions", typename); | |
204 | } | |
205 | ||
206 | /* Print an error message for invalid use of an incomplete type. | |
207 | VALUE is the expression that was used (or 0 if that isn't known) | |
208 | and TYPE is the type that was invalid. */ | |
209 | ||
210 | void | |
211 | incomplete_type_error (value, type) | |
212 | tree value; | |
213 | tree type; | |
214 | { | |
215 | char *errmsg; | |
216 | ||
217 | /* Avoid duplicate error message. */ | |
218 | if (TREE_CODE (type) == ERROR_MARK) | |
219 | return; | |
220 | ||
221 | if (value != 0 && (TREE_CODE (value) == VAR_DECL | |
222 | || TREE_CODE (value) == PARM_DECL)) | |
223 | error ("`%s' has an incomplete type", | |
224 | IDENTIFIER_POINTER (DECL_NAME (value))); | |
225 | else | |
226 | { | |
227 | retry: | |
228 | /* We must print an error message. Be clever about what it says. */ | |
229 | ||
230 | switch (TREE_CODE (type)) | |
231 | { | |
232 | case RECORD_TYPE: | |
233 | errmsg = "invalid use of undefined type `struct %s'"; | |
234 | break; | |
235 | ||
236 | case UNION_TYPE: | |
237 | errmsg = "invalid use of undefined type `union %s'"; | |
238 | break; | |
239 | ||
240 | case ENUMERAL_TYPE: | |
241 | errmsg = "invalid use of undefined type `enum %s'"; | |
242 | break; | |
243 | ||
244 | case VOID_TYPE: | |
245 | error ("invalid use of void expression"); | |
246 | return; | |
247 | ||
248 | case ARRAY_TYPE: | |
249 | if (TYPE_DOMAIN (type)) | |
250 | { | |
251 | type = TREE_TYPE (type); | |
252 | goto retry; | |
253 | } | |
254 | error ("invalid use of array with unspecified bounds"); | |
255 | return; | |
256 | ||
257 | case OFFSET_TYPE: | |
258 | error ("invalid use of member type (did you forget the `&' ?)"); | |
259 | return; | |
260 | ||
261 | default: | |
262 | my_friendly_abort (108); | |
263 | } | |
264 | ||
265 | error_with_aggr_type (type, errmsg); | |
266 | } | |
267 | } | |
268 | ||
269 | /* There are times when the compiler can get very confused, confused | |
270 | to the point of giving up by aborting, simply because of previous | |
271 | input errors. It is much better to have the user go back and | |
272 | correct those errors first, and see if it makes us happier, than it | |
273 | is to abort on him. This is because when one has a 10,000 line | |
274 | program, and the compiler comes back with ``core dump'', the user | |
275 | is left not knowing even where to begin to fix things and no place | |
276 | to even try and work around things. | |
277 | ||
278 | The parameter is to uniquely identify the problem to the user, so | |
279 | that they can say, I am having problem 59, and know that fix 7 will | |
280 | probably solve their problem. Or, we can document what problem | |
281 | 59 is, so they can understand how to work around it, should they | |
282 | ever run into it. | |
283 | ||
284 | Note, there will be no more calls in the C++ front end to abort, | |
285 | because the C++ front end is so unreliable still. The C front end | |
286 | can get away with calling abort, because for most of the calls to | |
287 | abort on most machines, it, I suspect, can be proven that it is | |
288 | impossible to ever call abort. The same is not yet true for C++, | |
289 | one day, maybe it will be. (mrs) */ | |
290 | ||
291 | /* First used: 0 (reserved), Last used: 345 */ | |
292 | ||
293 | void | |
294 | my_friendly_abort (i) | |
295 | int i; | |
296 | { | |
297 | if (i == 0) | |
298 | error ("Internal compiler error."); | |
299 | else if (errorcount + sorrycount == 1) | |
300 | fatal ("please fix above error, and try recompiling."); | |
301 | else if (errorcount > 0 || sorrycount > 0) | |
302 | fatal ("please fix above errors, and try recompiling."); | |
303 | else | |
304 | error ("Internal compiler error %d.", i); | |
305 | fatal ("Please report this to `bug-g++@prep.ai.mit.edu'."); | |
306 | } | |
307 | ||
308 | void | |
309 | my_friendly_assert (cond, where) | |
310 | int cond, where; | |
311 | { | |
312 | if (cond == 0) | |
313 | { | |
314 | /* Don't say "please fix above errors", since it's quite | |
315 | possible that we've lost somewhere unrelated to an error. */ | |
316 | errorcount = sorrycount = 0; | |
317 | my_friendly_abort (where); | |
318 | } | |
319 | } | |
320 | \f | |
321 | /* Return nonzero if VALUE is a valid constant-valued expression | |
322 | for use in initializing a static variable; one that can be an | |
323 | element of a "constant" initializer. | |
324 | ||
325 | Return 1 if the value is absolute; return 2 if it is relocatable. | |
326 | We assume that VALUE has been folded as much as possible; | |
327 | therefore, we do not need to check for such things as | |
328 | arithmetic-combinations of integers. */ | |
329 | ||
330 | static int | |
331 | initializer_constant_valid_p (value) | |
332 | tree value; | |
333 | { | |
334 | switch (TREE_CODE (value)) | |
335 | { | |
336 | case CONSTRUCTOR: | |
337 | return TREE_STATIC (value); | |
338 | ||
339 | case INTEGER_CST: | |
340 | case REAL_CST: | |
341 | case STRING_CST: | |
342 | return 1; | |
343 | ||
344 | case ADDR_EXPR: | |
345 | return 2; | |
346 | ||
347 | case CONVERT_EXPR: | |
348 | case NOP_EXPR: | |
349 | /* Allow conversions between types of the same kind. */ | |
350 | if (TREE_CODE (TREE_TYPE (value)) | |
351 | == TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0)))) | |
352 | return initializer_constant_valid_p (TREE_OPERAND (value, 0)); | |
353 | /* Allow (int) &foo. */ | |
354 | if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE | |
355 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (value, 0))) == POINTER_TYPE) | |
356 | return initializer_constant_valid_p (TREE_OPERAND (value, 0)); | |
357 | return 0; | |
358 | ||
359 | case PLUS_EXPR: | |
360 | { | |
361 | int valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0)); | |
362 | int valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1)); | |
363 | if (valid0 == 1 && valid1 == 2) | |
364 | return 2; | |
365 | if (valid0 == 2 && valid1 == 1) | |
366 | return 2; | |
367 | return 0; | |
368 | } | |
369 | ||
370 | case MINUS_EXPR: | |
371 | { | |
372 | int valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0)); | |
373 | int valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1)); | |
374 | if (valid0 == 2 && valid1 == 1) | |
375 | return 2; | |
376 | return 0; | |
377 | } | |
378 | } | |
379 | ||
380 | return 0; | |
381 | } | |
382 | \f | |
383 | /* Perform appropriate conversions on the initial value of a variable, | |
384 | store it in the declaration DECL, | |
385 | and print any error messages that are appropriate. | |
386 | If the init is invalid, store an ERROR_MARK. | |
387 | ||
388 | C++: Note that INIT might be a TREE_LIST, which would mean that it is | |
389 | a base class initializer for some aggregate type, hopefully compatible | |
390 | with DECL. If INIT is a single element, and DECL is an aggregate | |
391 | type, we silently convert INIT into a TREE_LIST, allowing a constructor | |
392 | to be called. | |
393 | ||
394 | If INIT is a TREE_LIST and there is no constructor, turn INIT | |
395 | into a CONSTRUCTOR and use standard initialization techniques. | |
396 | Perhaps a warning should be generated? | |
397 | ||
398 | Returns value of initializer if initialization could not be | |
399 | performed for static variable. In that case, caller must do | |
400 | the storing. */ | |
401 | ||
402 | tree | |
403 | store_init_value (decl, init) | |
404 | tree decl, init; | |
405 | { | |
406 | register tree value, type; | |
407 | ||
408 | /* If variable's type was invalidly declared, just ignore it. */ | |
409 | ||
410 | type = TREE_TYPE (decl); | |
411 | if (TREE_CODE (type) == ERROR_MARK) | |
412 | return NULL_TREE; | |
413 | ||
414 | /* Take care of C++ business up here. */ | |
415 | type = TYPE_MAIN_VARIANT (type); | |
416 | ||
417 | /* implicitly tests if IS_AGGR_TYPE. */ | |
418 | if (TYPE_NEEDS_CONSTRUCTING (type)) | |
419 | my_friendly_abort (109); | |
420 | else if (IS_AGGR_TYPE (type)) | |
421 | { | |
422 | /* @@ This may be wrong, but I do not know what is right. */ | |
423 | if (TREE_CODE (init) == TREE_LIST) | |
424 | { | |
425 | error_with_aggr_type (type, "constructor syntax used, but no constructor declared for type `%s'"); | |
426 | init = build_nt (CONSTRUCTOR, NULL_TREE, nreverse (init)); | |
427 | } | |
428 | } | |
429 | else if (TREE_CODE (init) == TREE_LIST | |
430 | && TREE_TYPE (init) != unknown_type_node) | |
431 | { | |
432 | if (TREE_CODE (decl) == RESULT_DECL) | |
433 | { | |
434 | if (TREE_CHAIN (init)) | |
435 | { | |
436 | warning ("comma expression used to initialize return value"); | |
437 | init = build_compound_expr (init); | |
438 | } | |
439 | else | |
440 | init = TREE_VALUE (init); | |
441 | } | |
442 | else if (TREE_TYPE (init) != 0 | |
443 | && TREE_CODE (TREE_TYPE (init)) == OFFSET_TYPE) | |
444 | { | |
445 | /* Use the type of our variable to instantiate | |
446 | the type of our initializer. */ | |
447 | init = instantiate_type (type, init, 1); | |
448 | } | |
449 | else if (TREE_CODE (init) == TREE_LIST | |
450 | && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE) | |
451 | { | |
452 | error ("cannot initialize arrays using this syntax"); | |
453 | return NULL_TREE; | |
454 | } | |
455 | else | |
456 | { | |
457 | error ("bad syntax in initialization"); | |
458 | return NULL_TREE; | |
459 | } | |
460 | } | |
461 | ||
462 | /* End of special C++ code. */ | |
463 | ||
464 | /* Digest the specified initializer into an expression. */ | |
465 | ||
466 | value = digest_init (type, init, 0); | |
467 | ||
468 | /* Store the expression if valid; else report error. */ | |
469 | ||
470 | if (TREE_CODE (value) == ERROR_MARK) | |
471 | ; | |
472 | else if (TREE_STATIC (decl) | |
473 | && (! TREE_CONSTANT (value) | |
474 | || ! initializer_constant_valid_p (value) | |
475 | /* Since ctors and dtors are the only things that can | |
476 | reference vtables, and they are always written down | |
477 | the the vtable definition, we can leave the | |
478 | vtables in initialized data space. | |
479 | However, other initialized data cannot be initialized | |
480 | this way. Instead a global file-level initializer | |
481 | must do the job. */ | |
482 | || (flag_pic && !DECL_VIRTUAL_P (decl) && TREE_PUBLIC (decl)))) | |
483 | return value; | |
484 | else | |
485 | { | |
486 | if (pedantic && TREE_CODE (value) == CONSTRUCTOR) | |
487 | { | |
488 | if (! TREE_CONSTANT (value) || ! TREE_STATIC (value)) | |
489 | pedwarn ("ANSI C++ forbids non-constant aggregate initializer expressions"); | |
490 | } | |
491 | } | |
492 | DECL_INITIAL (decl) = value; | |
493 | return NULL_TREE; | |
494 | } | |
495 | \f | |
496 | /* Digest the parser output INIT as an initializer for type TYPE. | |
497 | Return a C expression of type TYPE to represent the initial value. | |
498 | ||
499 | If TAIL is nonzero, it points to a variable holding a list of elements | |
500 | of which INIT is the first. We update the list stored there by | |
501 | removing from the head all the elements that we use. | |
502 | Normally this is only one; we use more than one element only if | |
503 | TYPE is an aggregate and INIT is not a constructor. */ | |
504 | ||
505 | tree | |
506 | digest_init (type, init, tail) | |
507 | tree type, init, *tail; | |
508 | { | |
509 | enum tree_code code = TREE_CODE (type); | |
510 | tree element = 0; | |
511 | tree old_tail_contents; | |
512 | /* Nonzero if INIT is a braced grouping, which comes in as a CONSTRUCTOR | |
513 | tree node which has no TREE_TYPE. */ | |
514 | int raw_constructor | |
515 | = TREE_CODE (init) == CONSTRUCTOR && TREE_TYPE (init) == 0; | |
516 | ||
517 | /* By default, assume we use one element from a list. | |
518 | We correct this later in the sole case where it is not true. */ | |
519 | ||
520 | if (tail) | |
521 | { | |
522 | old_tail_contents = *tail; | |
523 | *tail = TREE_CHAIN (*tail); | |
524 | } | |
525 | ||
526 | if (init == error_mark_node || (TREE_CODE (init) == TREE_LIST | |
527 | && TREE_VALUE (init) == error_mark_node)) | |
528 | return error_mark_node; | |
529 | ||
530 | /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ | |
531 | if (TREE_CODE (init) == NON_LVALUE_EXPR) | |
532 | init = TREE_OPERAND (init, 0); | |
533 | ||
534 | if (init && raw_constructor | |
535 | && CONSTRUCTOR_ELTS (init) != 0 | |
536 | && TREE_CHAIN (CONSTRUCTOR_ELTS (init)) == 0) | |
537 | { | |
538 | element = TREE_VALUE (CONSTRUCTOR_ELTS (init)); | |
539 | /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ | |
540 | if (element && TREE_CODE (element) == NON_LVALUE_EXPR) | |
541 | element = TREE_OPERAND (element, 0); | |
542 | if (element == error_mark_node) | |
543 | return element; | |
544 | } | |
545 | ||
546 | /* Any type can be initialized from an expression of the same type, | |
547 | optionally with braces. */ | |
548 | ||
549 | if (init && TREE_TYPE (init) | |
550 | && (TYPE_MAIN_VARIANT (TREE_TYPE (init)) == type | |
551 | || (code == ARRAY_TYPE && comptypes (TREE_TYPE (init), type, 1)))) | |
552 | { | |
553 | if (pedantic && code == ARRAY_TYPE | |
554 | && TREE_CODE (init) != STRING_CST) | |
555 | pedwarn ("ANSI C++ forbids initializing array from array expression"); | |
556 | if (TREE_CODE (init) == CONST_DECL) | |
557 | init = DECL_INITIAL (init); | |
558 | else if (TREE_READONLY_DECL_P (init)) | |
559 | init = decl_constant_value (init); | |
560 | return init; | |
561 | } | |
562 | ||
563 | if (element && (TREE_TYPE (element) == type | |
564 | || (code == ARRAY_TYPE && TREE_TYPE (element) | |
565 | && comptypes (TREE_TYPE (element), type, 1)))) | |
566 | { | |
567 | if (pedantic && code == ARRAY_TYPE) | |
568 | pedwarn ("ANSI C++ forbids initializing array from array expression"); | |
569 | if (pedantic && (code == RECORD_TYPE || code == UNION_TYPE)) | |
570 | pedwarn ("ANSI C++ forbids single nonscalar initializer with braces"); | |
571 | if (TREE_CODE (element) == CONST_DECL) | |
572 | element = DECL_INITIAL (element); | |
573 | else if (TREE_READONLY_DECL_P (element)) | |
574 | element = decl_constant_value (element); | |
575 | return element; | |
576 | } | |
577 | ||
578 | /* Check for initializing a union by its first field. | |
579 | Such an initializer must use braces. */ | |
580 | ||
581 | if (code == UNION_TYPE) | |
582 | { | |
583 | tree result, field = TYPE_FIELDS (type); | |
584 | ||
585 | /* Find the first named field. ANSI decided in September 1990 | |
586 | that only named fields count here. */ | |
587 | while (field && DECL_NAME (field) == 0) | |
588 | field = TREE_CHAIN (field); | |
589 | ||
590 | if (field == 0) | |
591 | { | |
592 | error ("union with no named members cannot be initialized"); | |
593 | return error_mark_node; | |
594 | } | |
595 | if (! raw_constructor) | |
596 | { | |
597 | error ("type mismatch in initialization"); | |
598 | return error_mark_node; | |
599 | } | |
600 | if (element == 0) | |
601 | { | |
602 | if (!TYPE_NEEDS_CONSTRUCTING (type)) | |
603 | { | |
604 | error ("union initializer requires one element"); | |
605 | return error_mark_node; | |
606 | } | |
607 | } | |
608 | else | |
609 | { | |
610 | /* Take just the first element from within the constructor | |
611 | and it should match the type of the first element. */ | |
612 | element = digest_init (TREE_TYPE (field), element, 0); | |
613 | result = build (CONSTRUCTOR, type, 0, build_tree_list (field, element)); | |
614 | TREE_CONSTANT (result) = TREE_CONSTANT (element); | |
615 | TREE_STATIC (result) = (initializer_constant_valid_p (element) | |
616 | && TREE_CONSTANT (element)); | |
617 | return result; | |
618 | } | |
619 | } | |
620 | ||
621 | /* Initialization of an array of chars from a string constant | |
622 | optionally enclosed in braces. */ | |
623 | ||
624 | if (code == ARRAY_TYPE) | |
625 | { | |
626 | tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type)); | |
627 | if ((typ1 == char_type_node | |
628 | || typ1 == signed_char_type_node | |
629 | || typ1 == unsigned_char_type_node | |
630 | || typ1 == unsigned_wchar_type_node | |
631 | || typ1 == signed_wchar_type_node) | |
632 | && ((init && TREE_CODE (init) == STRING_CST) | |
633 | || (element && TREE_CODE (element) == STRING_CST))) | |
634 | { | |
635 | tree string = element ? element : init; | |
636 | ||
637 | if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string))) | |
638 | != char_type_node) | |
639 | && TYPE_PRECISION (typ1) == BITS_PER_UNIT) | |
640 | { | |
641 | error ("char-array initialized from wide string"); | |
642 | return error_mark_node; | |
643 | } | |
644 | if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string))) | |
645 | == char_type_node) | |
646 | && TYPE_PRECISION (typ1) != BITS_PER_UNIT) | |
647 | { | |
648 | error ("int-array initialized from non-wide string"); | |
649 | return error_mark_node; | |
650 | } | |
651 | ||
652 | if (pedantic && typ1 != char_type_node) | |
653 | pedwarn ("ANSI C++ forbids string initializer except for `char' elements"); | |
654 | TREE_TYPE (string) = type; | |
655 | if (TYPE_DOMAIN (type) != 0 | |
656 | && TREE_CONSTANT (TYPE_SIZE (type))) | |
657 | { | |
658 | register int size | |
659 | = TREE_INT_CST_LOW (TYPE_SIZE (type)); | |
660 | size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT; | |
661 | /* In C it is ok to subtract 1 from the length of the string | |
662 | because it's ok to ignore the terminating null char that is | |
663 | counted in the length of the constant, but in C++ this would | |
664 | be invalid. */ | |
665 | if (size < TREE_STRING_LENGTH (string)) | |
666 | warning ("initializer-string for array of chars is too long"); | |
667 | } | |
668 | return string; | |
669 | } | |
670 | } | |
671 | ||
672 | /* Handle scalar types, including conversions. */ | |
673 | ||
674 | if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE | |
675 | || code == ENUMERAL_TYPE || code == REFERENCE_TYPE) | |
676 | { | |
677 | if (raw_constructor) | |
678 | { | |
679 | if (element == 0) | |
680 | { | |
681 | error ("initializer for scalar variable requires one element"); | |
682 | return error_mark_node; | |
683 | } | |
684 | init = element; | |
685 | } | |
686 | ||
687 | return convert_for_initialization (0, type, init, LOOKUP_NORMAL, | |
688 | "initialization", NULL_TREE, 0); | |
689 | } | |
690 | ||
691 | /* Come here only for records and arrays (and unions with constructors). */ | |
692 | ||
693 | if (TYPE_SIZE (type) && ! TREE_CONSTANT (TYPE_SIZE (type))) | |
694 | { | |
695 | error ("variable-sized object may not be initialized"); | |
696 | return error_mark_node; | |
697 | } | |
698 | ||
699 | if (code == ARRAY_TYPE || code == RECORD_TYPE || code == UNION_TYPE) | |
700 | { | |
701 | if (raw_constructor) | |
702 | return process_init_constructor (type, init, 0); | |
703 | else if (TYPE_NEEDS_CONSTRUCTING (type)) | |
704 | { | |
705 | /* This can only be reached when caller is initializing | |
706 | ARRAY_TYPE. In that case, we don't want to convert | |
707 | INIT to TYPE. We will let `expand_vec_init' do it. */ | |
708 | return init; | |
709 | } | |
710 | else if (tail != 0) | |
711 | { | |
712 | *tail = old_tail_contents; | |
713 | return process_init_constructor (type, 0, tail); | |
714 | } | |
715 | else if (flag_traditional) | |
716 | /* Traditionally one can say `char x[100] = 0;'. */ | |
717 | return process_init_constructor (type, | |
718 | build_nt (CONSTRUCTOR, 0, | |
719 | tree_cons (0, init, 0)), | |
720 | 0); | |
721 | if (code != ARRAY_TYPE) | |
722 | return convert_for_initialization (0, type, init, LOOKUP_NORMAL, | |
723 | "initialization", NULL_TREE, 0); | |
724 | } | |
725 | ||
726 | error ("invalid initializer"); | |
727 | return error_mark_node; | |
728 | } | |
729 | \f | |
730 | /* Process a constructor for a variable of type TYPE. | |
731 | The constructor elements may be specified either with INIT or with ELTS, | |
732 | only one of which should be non-null. | |
733 | ||
734 | If INIT is specified, it is a CONSTRUCTOR node which is specifically | |
735 | and solely for initializing this datum. | |
736 | ||
737 | If ELTS is specified, it is the address of a variable containing | |
738 | a list of expressions. We take as many elements as we need | |
739 | from the head of the list and update the list. | |
740 | ||
741 | In the resulting constructor, TREE_CONSTANT is set if all elts are | |
742 | constant, and TREE_STATIC is set if, in addition, all elts are simple enough | |
743 | constants that the assembler and linker can compute them. */ | |
744 | ||
745 | static tree | |
746 | process_init_constructor (type, init, elts) | |
747 | tree type, init, *elts; | |
748 | { | |
749 | extern tree empty_init_node; | |
750 | register tree tail; | |
751 | /* List of the elements of the result constructor, | |
752 | in reverse order. */ | |
753 | register tree members = NULL; | |
754 | tree result; | |
755 | int allconstant = 1; | |
756 | int allsimple = 1; | |
757 | int erred = 0; | |
758 | ||
759 | /* Make TAIL be the list of elements to use for the initialization, | |
760 | no matter how the data was given to us. */ | |
761 | ||
762 | if (elts) | |
763 | { | |
764 | if (extra_warnings) | |
765 | warning ("aggregate has a partly bracketed initializer"); | |
766 | tail = *elts; | |
767 | } | |
768 | else | |
769 | tail = CONSTRUCTOR_ELTS (init); | |
770 | ||
771 | /* Gobble as many elements as needed, and make a constructor or initial value | |
772 | for each element of this aggregate. Chain them together in result. | |
773 | If there are too few, use 0 for each scalar ultimate component. */ | |
774 | ||
775 | if (TREE_CODE (type) == ARRAY_TYPE) | |
776 | { | |
777 | tree domain = TYPE_DOMAIN (type); | |
778 | register long len; | |
779 | register int i; | |
780 | ||
781 | if (domain) | |
782 | len = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain)) | |
783 | - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain)) | |
784 | + 1); | |
785 | else | |
786 | len = -1; /* Take as many as there are */ | |
787 | ||
788 | for (i = 0; (len < 0 || i < len) && tail != 0; i++) | |
789 | { | |
790 | register tree next1; | |
791 | ||
792 | if (TREE_VALUE (tail) != 0) | |
793 | { | |
794 | tree tail1 = tail; | |
795 | next1 = digest_init (TYPE_MAIN_VARIANT (TREE_TYPE (type)), | |
796 | TREE_VALUE (tail), &tail1); | |
797 | my_friendly_assert (tail1 == 0 | |
798 | || TREE_CODE (tail1) == TREE_LIST, 319); | |
799 | if (tail == tail1 && len < 0) | |
800 | { | |
801 | error ("non-empty initializer for array of empty elements"); | |
802 | /* Just ignore what we were supposed to use. */ | |
803 | tail1 = 0; | |
804 | } | |
805 | tail = tail1; | |
806 | } | |
807 | else | |
808 | { | |
809 | next1 = error_mark_node; | |
810 | tail = TREE_CHAIN (tail); | |
811 | } | |
812 | ||
813 | if (next1 == error_mark_node) | |
814 | erred = 1; | |
815 | else if (!TREE_CONSTANT (next1)) | |
816 | allconstant = 0; | |
817 | else if (! initializer_constant_valid_p (next1)) | |
818 | allsimple = 0; | |
819 | members = tree_cons (NULL_TREE, next1, members); | |
820 | } | |
821 | } | |
822 | if (TREE_CODE (type) == RECORD_TYPE && init != empty_init_node) | |
823 | { | |
824 | register tree field; | |
825 | ||
826 | if (tail) | |
827 | { | |
828 | if (TYPE_USES_VIRTUAL_BASECLASSES (type)) | |
829 | { | |
830 | sorry ("initializer list for object of class with virtual baseclasses"); | |
831 | return error_mark_node; | |
832 | } | |
833 | ||
834 | if (TYPE_BINFO_BASETYPES (type)) | |
835 | { | |
836 | sorry ("initializer list for object of class with baseclasses"); | |
837 | return error_mark_node; | |
838 | } | |
839 | ||
840 | if (TYPE_VIRTUAL_P (type)) | |
841 | { | |
842 | sorry ("initializer list for object using virtual functions"); | |
843 | return error_mark_node; | |
844 | } | |
845 | } | |
846 | ||
847 | for (field = TYPE_FIELDS (type); field && tail; | |
848 | field = TREE_CHAIN (field)) | |
849 | { | |
850 | register tree next1; | |
851 | ||
852 | if (! DECL_NAME (field)) | |
853 | { | |
854 | members = tree_cons (field, integer_zero_node, members); | |
855 | continue; | |
856 | } | |
857 | ||
858 | if (TREE_CODE (field) == CONST_DECL || TREE_CODE (field) == TYPE_DECL) | |
859 | continue; | |
860 | if (TREE_CODE (field) == VAR_DECL && !TREE_STATIC (field)) | |
861 | continue; | |
862 | ||
863 | if (TREE_VALUE (tail) != 0) | |
864 | { | |
865 | tree tail1 = tail; | |
866 | next1 = digest_init (TREE_TYPE (field), | |
867 | TREE_VALUE (tail), &tail1); | |
868 | my_friendly_assert (tail1 == 0 | |
869 | || TREE_CODE (tail1) == TREE_LIST, 320); | |
870 | if (TREE_CODE (field) == VAR_DECL | |
871 | && ! global_bindings_p ()) | |
872 | warning_with_decl (field, "initialization of static member `%s'"); | |
873 | tail = tail1; | |
874 | } | |
875 | else | |
876 | { | |
877 | next1 = error_mark_node; | |
878 | tail = TREE_CHAIN (tail); | |
879 | } | |
880 | ||
881 | if (next1 == error_mark_node) | |
882 | erred = 1; | |
883 | else if (!TREE_CONSTANT (next1)) | |
884 | allconstant = 0; | |
885 | else if (! initializer_constant_valid_p (next1)) | |
886 | allsimple = 0; | |
887 | members = tree_cons (field, next1, members); | |
888 | } | |
889 | for (; field; field = TREE_CHAIN (field)) | |
890 | { | |
891 | if (TREE_CODE (field) != FIELD_DECL) | |
892 | continue; | |
893 | ||
894 | /* Does this field have a default initialization? */ | |
895 | if (DECL_INITIAL (field)) | |
896 | { | |
897 | register tree next1 = DECL_INITIAL (field); | |
898 | if (TREE_CODE (next1) == ERROR_MARK) | |
899 | erred = 1; | |
900 | else if (!TREE_CONSTANT (next1)) | |
901 | allconstant = 0; | |
902 | else if (! initializer_constant_valid_p (next1)) | |
903 | allsimple = 0; | |
904 | members = tree_cons (field, next1, members); | |
905 | } | |
906 | else if (TREE_READONLY (field)) | |
907 | error ("uninitialized const member `%s'", | |
908 | IDENTIFIER_POINTER (DECL_NAME (field))); | |
909 | else if (TYPE_LANG_SPECIFIC (TREE_TYPE (field)) | |
910 | && CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field))) | |
911 | error ("member `%s' with uninitialized const fields", | |
912 | IDENTIFIER_POINTER (DECL_NAME (field))); | |
913 | else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE) | |
914 | error ("member `%s' is uninitialized reference", | |
915 | IDENTIFIER_POINTER (DECL_NAME (field))); | |
916 | } | |
917 | } | |
918 | ||
919 | /* If arguments were specified as a list, just remove the ones we used. */ | |
920 | if (elts) | |
921 | *elts = tail; | |
922 | /* If arguments were specified as a constructor, | |
923 | complain unless we used all the elements of the constructor. */ | |
924 | else if (tail) | |
925 | warning ("excess elements in aggregate initializer"); | |
926 | ||
927 | if (erred) | |
928 | return error_mark_node; | |
929 | ||
930 | result = build (CONSTRUCTOR, type, NULL_TREE, nreverse (members)); | |
931 | if (init) | |
932 | TREE_HAS_CONSTRUCTOR (result) = TREE_HAS_CONSTRUCTOR (init); | |
933 | if (allconstant) TREE_CONSTANT (result) = 1; | |
934 | if (allconstant && allsimple) TREE_STATIC (result) = 1; | |
935 | return result; | |
936 | } | |
937 | \f | |
938 | /* Given a structure or union value DATUM, construct and return | |
939 | the structure or union component which results from narrowing | |
940 | that value by the types specified in TYPES. For example, given the | |
941 | hierarchy | |
942 | ||
943 | class L { int ii; }; | |
944 | class A : L { ... }; | |
945 | class B : L { ... }; | |
946 | class C : A, B { ... }; | |
947 | ||
948 | and the declaration | |
949 | ||
950 | C x; | |
951 | ||
952 | then the expression | |
953 | ||
954 | x::C::A::L::ii refers to the ii member of the L part of | |
955 | of A part of the C object named by X. In this case, | |
956 | DATUM would be x, and TYPES would be a SCOPE_REF consisting of | |
957 | ||
958 | SCOPE_REF | |
959 | SCOPE_REF | |
960 | C A | |
961 | L | |
962 | ||
963 | The last entry in the SCOPE_REF is always an IDENTIFIER_NODE. | |
964 | ||
965 | */ | |
966 | ||
967 | tree | |
968 | build_scoped_ref (datum, types) | |
969 | tree datum; | |
970 | tree types; | |
971 | { | |
972 | tree orig_ref, ref; | |
973 | tree type = TREE_TYPE (datum); | |
974 | ||
975 | if (datum == error_mark_node) | |
976 | return error_mark_node; | |
977 | type = TYPE_MAIN_VARIANT (type); | |
978 | ||
979 | if (TREE_CODE (types) == SCOPE_REF) | |
980 | { | |
981 | /* We have some work to do. */ | |
982 | struct type_chain { tree type; struct type_chain *next; } *chain = 0, *head = 0, scratch; | |
983 | orig_ref = ref = build_unary_op (ADDR_EXPR, datum, 0); | |
984 | while (TREE_CODE (types) == SCOPE_REF) | |
985 | { | |
986 | tree t = TREE_OPERAND (types, 1); | |
987 | if (is_aggr_typedef (t, 1)) | |
988 | { | |
989 | head = (struct type_chain *)alloca (sizeof (struct type_chain)); | |
990 | head->type = IDENTIFIER_TYPE_VALUE (t); | |
991 | head->next = chain; | |
992 | chain = head; | |
993 | types = TREE_OPERAND (types, 0); | |
994 | } | |
995 | else return error_mark_node; | |
996 | } | |
997 | if (! is_aggr_typedef (types, 1)) | |
998 | return error_mark_node; | |
999 | ||
1000 | head = &scratch; | |
1001 | head->type = IDENTIFIER_TYPE_VALUE (types); | |
1002 | head->next = chain; | |
1003 | chain = head; | |
1004 | while (chain) | |
1005 | { | |
1006 | tree binfo = chain->type; | |
1007 | type = TREE_TYPE (TREE_TYPE (ref)); | |
1008 | if (binfo != TYPE_BINFO (type)) | |
1009 | { | |
1010 | binfo = get_binfo (binfo, type, 1); | |
1011 | if (binfo == error_mark_node) | |
1012 | return error_mark_node; | |
1013 | if (binfo == 0) | |
1014 | return error_not_base_type (TYPE_NAME_STRING (chain->type), TYPE_NAME_STRING (type)); | |
1015 | ref = convert_pointer_to (binfo, ref); | |
1016 | } | |
1017 | chain = chain->next; | |
1018 | } | |
1019 | return build_indirect_ref (ref, "(compiler error in build_scoped_ref)"); | |
1020 | } | |
1021 | ||
1022 | /* This is an easy conversion. */ | |
1023 | if (is_aggr_typedef (types, 1)) | |
1024 | { | |
1025 | tree binfo = TYPE_BINFO (IDENTIFIER_TYPE_VALUE (types)); | |
1026 | if (binfo != TYPE_BINFO (type)) | |
1027 | { | |
1028 | binfo = get_binfo (binfo, type, 1); | |
1029 | if (binfo == error_mark_node) | |
1030 | return error_mark_node; | |
1031 | if (binfo == 0) | |
1032 | return error_not_base_type (IDENTIFIER_TYPE_VALUE (types), type); | |
1033 | } | |
1034 | ||
1035 | switch (TREE_CODE (datum)) | |
1036 | { | |
1037 | case NOP_EXPR: | |
1038 | case CONVERT_EXPR: | |
1039 | case FLOAT_EXPR: | |
1040 | case FIX_TRUNC_EXPR: | |
1041 | case FIX_FLOOR_EXPR: | |
1042 | case FIX_ROUND_EXPR: | |
1043 | case FIX_CEIL_EXPR: | |
1044 | ref = convert_pointer_to (binfo, | |
1045 | build_unary_op (ADDR_EXPR, TREE_OPERAND (datum, 0), 0)); | |
1046 | break; | |
1047 | default: | |
1048 | ref = convert_pointer_to (binfo, | |
1049 | build_unary_op (ADDR_EXPR, datum, 0)); | |
1050 | } | |
1051 | return build_indirect_ref (ref, "(compiler error in build_scoped_ref)"); | |
1052 | } | |
1053 | return error_mark_node; | |
1054 | } | |
1055 | ||
1056 | /* Build a reference to an object specified by the C++ `->' operator. | |
1057 | Usually this just involves dereferencing the object, but if the | |
1058 | `->' operator is overloaded, then such overloads must be | |
1059 | performed until an object which does not have the `->' operator | |
1060 | overloaded is found. An error is reported when circular pointer | |
1061 | delegation is detected. */ | |
1062 | tree | |
1063 | build_x_arrow (datum) | |
1064 | tree datum; | |
1065 | { | |
1066 | tree types_memoized = NULL_TREE; | |
1067 | register tree rval = datum; | |
1068 | tree type = TREE_TYPE (rval); | |
1069 | tree last_rval; | |
1070 | ||
1071 | if (type == error_mark_node) | |
1072 | return error_mark_node; | |
1073 | ||
1074 | if (TREE_CODE (type) == REFERENCE_TYPE) | |
1075 | { | |
1076 | rval = convert_from_reference (rval); | |
1077 | type = TREE_TYPE (rval); | |
1078 | } | |
1079 | ||
1080 | if (IS_AGGR_TYPE (type) && TYPE_OVERLOADS_ARROW (type)) | |
1081 | { | |
1082 | while (rval = build_opfncall (COMPONENT_REF, LOOKUP_NORMAL, rval)) | |
1083 | { | |
1084 | if (rval == error_mark_node) | |
1085 | return error_mark_node; | |
1086 | ||
1087 | if (value_member (TREE_TYPE (rval), types_memoized)) | |
1088 | { | |
1089 | error ("circular pointer delegation detected"); | |
1090 | return error_mark_node; | |
1091 | } | |
1092 | else | |
1093 | { | |
1094 | types_memoized = tree_cons (NULL_TREE, TREE_TYPE (rval), | |
1095 | types_memoized); | |
1096 | } | |
1097 | last_rval = rval; | |
1098 | } | |
1099 | if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE) | |
1100 | last_rval = convert_from_reference (last_rval); | |
1101 | } | |
1102 | else | |
1103 | last_rval = default_conversion (rval); | |
1104 | ||
1105 | more: | |
1106 | if (TREE_CODE (TREE_TYPE (last_rval)) == POINTER_TYPE) | |
1107 | return build_indirect_ref (last_rval, 0); | |
1108 | ||
1109 | if (TREE_CODE (TREE_TYPE (last_rval)) == OFFSET_TYPE) | |
1110 | { | |
1111 | if (TREE_CODE (last_rval) == OFFSET_REF | |
1112 | && TREE_STATIC (TREE_OPERAND (last_rval, 1))) | |
1113 | { | |
1114 | last_rval = TREE_OPERAND (last_rval, 1); | |
1115 | if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE) | |
1116 | last_rval = convert_from_reference (last_rval); | |
1117 | goto more; | |
1118 | } | |
1119 | compiler_error ("invalid member type in build_x_arrow"); | |
1120 | return error_mark_node; | |
1121 | } | |
1122 | ||
1123 | if (types_memoized) | |
1124 | error ("result of `operator->()' yields non-pointer result"); | |
1125 | else | |
1126 | error ("base operand of `->' is not a pointer"); | |
1127 | return error_mark_node; | |
1128 | } | |
1129 | ||
1130 | /* Make an expression to refer to the COMPONENT field of | |
1131 | structure or union value DATUM. COMPONENT is an arbitrary | |
1132 | expression. DATUM has already been checked out to be of | |
1133 | aggregate type. | |
1134 | ||
1135 | For C++, COMPONENT may be a TREE_LIST. This happens when we must | |
1136 | return an object of member type to a method of the current class, | |
1137 | but there is not yet enough typing information to know which one. | |
1138 | As a special case, if there is only one method by that name, | |
1139 | it is returned. Otherwise we return an expression which other | |
1140 | routines will have to know how to deal with later. */ | |
1141 | tree | |
1142 | build_m_component_ref (datum, component) | |
1143 | tree datum, component; | |
1144 | { | |
1145 | tree type = TREE_TYPE (component); | |
1146 | tree objtype = TREE_TYPE (datum); | |
1147 | ||
1148 | if (datum == error_mark_node || component == error_mark_node) | |
1149 | return error_mark_node; | |
1150 | ||
1151 | if (TREE_CODE (type) != OFFSET_TYPE && TREE_CODE (type) != METHOD_TYPE) | |
1152 | { | |
1153 | error ("non-member type composed with object"); | |
1154 | return error_mark_node; | |
1155 | } | |
1156 | ||
1157 | if (TREE_CODE (objtype) == REFERENCE_TYPE) | |
1158 | objtype = TREE_TYPE (objtype); | |
1159 | ||
1160 | if (! comptypes (TYPE_METHOD_BASETYPE (type), objtype, 0)) | |
1161 | { | |
1162 | error ("member type `%s::' incompatible with object type `%s'", | |
1163 | TYPE_NAME_STRING (TYPE_METHOD_BASETYPE (type)), | |
1164 | TYPE_NAME_STRING (objtype)); | |
1165 | return error_mark_node; | |
1166 | } | |
1167 | ||
1168 | return build (OFFSET_REF, TREE_TYPE (TREE_TYPE (component)), datum, component); | |
1169 | } | |
1170 | ||
1171 | /* Return a tree node for the expression TYPENAME '(' PARMS ')'. | |
1172 | ||
1173 | Because we cannot tell whether this construct is really | |
1174 | a function call or a call to a constructor or a request for | |
1175 | a type conversion, we try all three, and report any ambiguities | |
1176 | we find. */ | |
1177 | tree | |
1178 | build_functional_cast (exp, parms) | |
1179 | tree exp; | |
1180 | tree parms; | |
1181 | { | |
1182 | /* This is either a call to a constructor, | |
1183 | or a C cast in C++'s `functional' notation. */ | |
1184 | tree type, name = NULL_TREE; | |
1185 | tree expr_as_ctor = NULL_TREE; | |
1186 | tree expr_as_method = NULL_TREE; | |
1187 | tree expr_as_fncall = NULL_TREE; | |
1188 | tree expr_as_conversion = NULL_TREE; | |
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 | { | |
1195 | name = exp; | |
1196 | ||
1197 | if (IDENTIFIER_HAS_TYPE_VALUE (exp)) | |
1198 | /* Either an enum or an aggregate type. */ | |
1199 | type = IDENTIFIER_TYPE_VALUE (exp); | |
1200 | else | |
1201 | { | |
1202 | type = lookup_name (exp, 1); | |
1203 | if (!type || TREE_CODE (type) != TYPE_DECL) | |
1204 | { | |
1205 | error ("`%s' fails to be a typedef or built-in type", | |
1206 | IDENTIFIER_POINTER (name)); | |
1207 | return error_mark_node; | |
1208 | } | |
1209 | type = TREE_TYPE (type); | |
1210 | } | |
1211 | } | |
1212 | else type = exp; | |
1213 | ||
1214 | /* Prepare to evaluate as a call to a constructor. If this expression | |
1215 | is actually used, for example, | |
1216 | ||
1217 | return X (arg1, arg2, ...); | |
1218 | ||
1219 | then the slot being initialized will be filled in. */ | |
1220 | ||
1221 | if (name == NULL_TREE) | |
1222 | { | |
1223 | name = TYPE_NAME (type); | |
1224 | if (TREE_CODE (name) == TYPE_DECL) | |
1225 | name = DECL_NAME (name); | |
1226 | } | |
1227 | ||
1228 | /* Try evaluating as a call to a function. */ | |
1229 | if (IDENTIFIER_CLASS_VALUE (name)) | |
1230 | expr_as_method = build_method_call (current_class_decl, name, parms, | |
1231 | NULL_TREE, LOOKUP_SPECULATIVELY); | |
1232 | if (IDENTIFIER_GLOBAL_VALUE (name) | |
1233 | && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (name)) == TREE_LIST | |
1234 | || TREE_CODE (IDENTIFIER_GLOBAL_VALUE (name)) == FUNCTION_DECL)) | |
1235 | { | |
1236 | expr_as_fncall = build_overload_call (name, parms, 0, 0); | |
1237 | if (expr_as_fncall == NULL_TREE) | |
1238 | expr_as_fncall = error_mark_node; | |
1239 | } | |
1240 | ||
1241 | if (! IS_AGGR_TYPE (type)) | |
1242 | { | |
1243 | /* this must build a C cast */ | |
1244 | if (parms == NULL_TREE) | |
1245 | { | |
1246 | if (expr_as_method || expr_as_fncall) | |
1247 | goto return_function; | |
1248 | ||
1249 | error ("cannot cast null list to type `%s'", | |
1250 | IDENTIFIER_POINTER (name)); | |
1251 | return error_mark_node; | |
1252 | } | |
1253 | if (expr_as_method | |
1254 | || (expr_as_fncall && expr_as_fncall != error_mark_node)) | |
1255 | { | |
1256 | error ("ambiguity between cast to `%s' and function call", | |
1257 | IDENTIFIER_POINTER (name)); | |
1258 | return error_mark_node; | |
1259 | } | |
1260 | return build_c_cast (type, build_compound_expr (parms)); | |
1261 | } | |
1262 | ||
1263 | if (TYPE_SIZE (type) == NULL_TREE) | |
1264 | { | |
1265 | if (expr_as_method || expr_as_fncall) | |
1266 | goto return_function; | |
1267 | error ("type `%s' is not yet defined", IDENTIFIER_POINTER (name)); | |
1268 | return error_mark_node; | |
1269 | } | |
1270 | ||
1271 | if (parms && TREE_CHAIN (parms) == NULL_TREE) | |
1272 | expr_as_conversion | |
1273 | = build_type_conversion (CONVERT_EXPR, type, TREE_VALUE (parms), 0); | |
1274 | ||
1275 | if (! TYPE_NEEDS_CONSTRUCTOR (type) && parms != NULL_TREE) | |
1276 | { | |
1277 | char *msg = 0; | |
1278 | ||
1279 | if (parms == NULL_TREE) | |
1280 | msg = "argument missing in cast to `%s' type"; | |
1281 | else if (TREE_CHAIN (parms) == NULL_TREE) | |
1282 | { | |
1283 | if (expr_as_conversion == NULL_TREE) | |
1284 | msg = "conversion to type `%s' failed"; | |
1285 | } | |
1286 | else msg = "type `%s' does not have a constructor"; | |
1287 | ||
1288 | if ((expr_as_method || expr_as_fncall) && expr_as_conversion) | |
1289 | msg = "ambiguity between conversion to `%s' and function call"; | |
1290 | else if (expr_as_method || expr_as_fncall) | |
1291 | goto return_function; | |
1292 | else if (expr_as_conversion) | |
1293 | return expr_as_conversion; | |
1294 | ||
1295 | error (msg, IDENTIFIER_POINTER (name)); | |
1296 | return error_mark_node; | |
1297 | } | |
1298 | ||
1299 | if (! TYPE_HAS_CONSTRUCTOR (type)) | |
1300 | { | |
1301 | if (expr_as_method || expr_as_fncall) | |
1302 | goto return_function; | |
1303 | if (expr_as_conversion) | |
1304 | return expr_as_conversion; | |
1305 | ||
1306 | /* Look through this type until we find the | |
1307 | base type which has a constructor. */ | |
1308 | do | |
1309 | { | |
1310 | tree binfos = TYPE_BINFO_BASETYPES (type); | |
1311 | int i, index = 0; | |
1312 | ||
1313 | while (binfos && TREE_VEC_LENGTH (binfos) == 1 | |
1314 | && ! TYPE_HAS_CONSTRUCTOR (type)) | |
1315 | { | |
1316 | type = BINFO_TYPE (TREE_VEC_ELT (binfos, 0)); | |
1317 | binfos = TYPE_BINFO_BASETYPES (type); | |
1318 | } | |
1319 | if (TYPE_HAS_CONSTRUCTOR (type)) | |
1320 | break; | |
1321 | /* Hack for MI. */ | |
1322 | i = binfos ? TREE_VEC_LENGTH (binfos) : 0; | |
1323 | if (i == 0) break; | |
1324 | while (--i > 0) | |
1325 | { | |
1326 | if (TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (TREE_VEC_ELT (binfos, i)))) | |
1327 | { | |
1328 | if (index == 0) | |
1329 | index = i; | |
1330 | else | |
1331 | { | |
1332 | error ("multiple base classes with constructor, ambiguous"); | |
1333 | type = 0; | |
1334 | break; | |
1335 | } | |
1336 | } | |
1337 | } | |
1338 | if (type == 0) | |
1339 | break; | |
1340 | } while (! TYPE_HAS_CONSTRUCTOR (type)); | |
1341 | if (type == 0) | |
1342 | return error_mark_node; | |
1343 | } | |
1344 | name = TYPE_NAME (type); | |
1345 | if (TREE_CODE (name) == TYPE_DECL) | |
1346 | name = DECL_NAME (name); | |
1347 | ||
1348 | my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 321); | |
1349 | ||
1350 | { | |
1351 | int flags = LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN; | |
1352 | ||
1353 | if (parms && TREE_CHAIN (parms) == NULL_TREE) | |
1354 | flags |= LOOKUP_NO_CONVERSION; | |
1355 | ||
1356 | try_again: | |
1357 | expr_as_ctor = build_method_call (NULL_TREE, name, parms, NULL_TREE, flags); | |
1358 | ||
1359 | if (expr_as_ctor && expr_as_ctor != error_mark_node) | |
1360 | { | |
1361 | #if 0 | |
1362 | /* mrs Mar 12, 1992 I claim that if it is a constructor, it is | |
1363 | impossible to be an expr_as_method, without being a | |
1364 | constructor call. */ | |
1365 | if (expr_as_method | |
1366 | || (expr_as_fncall && expr_as_fncall != error_mark_node)) | |
1367 | #else | |
1368 | if (expr_as_fncall && expr_as_fncall != error_mark_node) | |
1369 | #endif | |
1370 | { | |
1371 | error ("ambiguity between constructor for `%s' and function call", | |
1372 | IDENTIFIER_POINTER (name)); | |
1373 | return error_mark_node; | |
1374 | } | |
1375 | else if (expr_as_conversion && expr_as_conversion != error_mark_node) | |
1376 | { | |
1377 | /* ANSI C++ June 5 1992 WP 12.3.2.6.1 */ | |
1378 | error ("ambiguity between conversion to `%s' and constructor", | |
1379 | IDENTIFIER_POINTER (name)); | |
1380 | return error_mark_node; | |
1381 | } | |
1382 | ||
1383 | if (current_function_decl) | |
1384 | return build_cplus_new (type, expr_as_ctor, 1); | |
1385 | ||
1386 | { | |
1387 | register tree parm = TREE_OPERAND (expr_as_ctor, 1); | |
1388 | ||
1389 | /* Initializers for static variables and parameters have | |
1390 | to handle doing the initialization and cleanup themselves. */ | |
1391 | my_friendly_assert (TREE_CODE (expr_as_ctor) == CALL_EXPR, 322); | |
1392 | my_friendly_assert (TREE_CALLS_NEW (TREE_VALUE (parm)), 323); | |
1393 | TREE_VALUE (parm) = NULL_TREE; | |
1394 | expr_as_ctor = build_indirect_ref (expr_as_ctor, 0); | |
1395 | TREE_HAS_CONSTRUCTOR (expr_as_ctor) = 1; | |
1396 | } | |
1397 | return expr_as_ctor; | |
1398 | } | |
1399 | ||
1400 | /* If it didn't work going through constructor, try type conversion. */ | |
1401 | if (! (flags & LOOKUP_COMPLAIN)) | |
1402 | { | |
1403 | if (expr_as_conversion) | |
1404 | return expr_as_conversion; | |
1405 | if (flags & LOOKUP_NO_CONVERSION) | |
1406 | { | |
1407 | flags = LOOKUP_NORMAL; | |
1408 | goto try_again; | |
1409 | } | |
1410 | } | |
1411 | ||
1412 | if (expr_as_conversion) | |
1413 | { | |
1414 | if (expr_as_method || expr_as_fncall) | |
1415 | { | |
1416 | error ("ambiguity between conversion to `%s' and function call", | |
1417 | IDENTIFIER_POINTER (name)); | |
1418 | return error_mark_node; | |
1419 | } | |
1420 | return expr_as_conversion; | |
1421 | } | |
1422 | return_function: | |
1423 | if (expr_as_method) | |
1424 | return build_method_call (current_class_decl, name, parms, | |
1425 | NULL_TREE, LOOKUP_NORMAL); | |
1426 | if (expr_as_fncall) | |
1427 | return expr_as_fncall == error_mark_node | |
1428 | ? build_overload_call (name, parms, 1, 0) : expr_as_fncall; | |
1429 | error ("invalid functional cast"); | |
1430 | return error_mark_node; | |
1431 | } | |
1432 | } | |
1433 | \f | |
1434 | /* Return the character string for the name that encodes the | |
1435 | enumeral value VALUE in the domain TYPE. */ | |
1436 | char * | |
1437 | enum_name_string (value, type) | |
1438 | tree value; | |
1439 | tree type; | |
1440 | { | |
1441 | register tree values = TYPE_VALUES (type); | |
1442 | register HOST_WIDE_INT intval = TREE_INT_CST_LOW (value); | |
1443 | ||
1444 | my_friendly_assert (TREE_CODE (type) == ENUMERAL_TYPE, 324); | |
1445 | while (values | |
1446 | && TREE_INT_CST_LOW (TREE_VALUE (values)) != intval) | |
1447 | values = TREE_CHAIN (values); | |
1448 | if (values == NULL_TREE) | |
1449 | { | |
1450 | char *buf = (char *)oballoc (16 + TYPE_NAME_LENGTH (type)); | |
1451 | ||
1452 | /* Value must have been cast. */ | |
1453 | sprintf (buf, "(enum %s)%d", | |
1454 | TYPE_NAME_STRING (type), intval); | |
1455 | return buf; | |
1456 | } | |
1457 | return IDENTIFIER_POINTER (TREE_PURPOSE (values)); | |
1458 | } | |
1459 | ||
1460 | /* Print out a language-specific error message for | |
1461 | (Pascal) case or (C) switch statements. | |
1462 | CODE tells what sort of message to print. | |
1463 | TYPE is the type of the switch index expression. | |
1464 | NEW is the new value that we were trying to add. | |
1465 | OLD is the old value that stopped us from adding it. */ | |
1466 | void | |
1467 | report_case_error (code, type, new_value, old_value) | |
1468 | int code; | |
1469 | tree type; | |
1470 | tree new_value, old_value; | |
1471 | { | |
1472 | if (code == 1) | |
1473 | { | |
1474 | if (new_value) | |
1475 | error ("case label not within a switch statement"); | |
1476 | else | |
1477 | error ("default label not within a switch statement"); | |
1478 | } | |
1479 | else if (code == 2) | |
1480 | { | |
1481 | if (new_value == 0) | |
1482 | { | |
1483 | error ("multiple default labels in one switch"); | |
1484 | return; | |
1485 | } | |
1486 | if (TREE_CODE (new_value) == RANGE_EXPR) | |
1487 | if (TREE_CODE (old_value) == RANGE_EXPR) | |
1488 | { | |
1489 | char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type))); | |
1490 | if (TREE_CODE (type) == ENUMERAL_TYPE) | |
1491 | sprintf (buf, "overlapping ranges [%s..%s], [%s..%s] in case expression", | |
1492 | enum_name_string (TREE_OPERAND (new_value, 0), type), | |
1493 | enum_name_string (TREE_OPERAND (new_value, 1), type), | |
1494 | enum_name_string (TREE_OPERAND (old_value, 0), type), | |
1495 | enum_name_string (TREE_OPERAND (old_value, 1), type)); | |
1496 | else | |
1497 | sprintf (buf, "overlapping ranges [%d..%d], [%d..%d] in case expression", | |
1498 | TREE_INT_CST_LOW (TREE_OPERAND (new_value, 0)), | |
1499 | TREE_INT_CST_LOW (TREE_OPERAND (new_value, 1)), | |
1500 | TREE_INT_CST_LOW (TREE_OPERAND (old_value, 0)), | |
1501 | TREE_INT_CST_LOW (TREE_OPERAND (old_value, 1))); | |
1502 | error (buf); | |
1503 | } | |
1504 | else | |
1505 | { | |
1506 | char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type))); | |
1507 | if (TREE_CODE (type) == ENUMERAL_TYPE) | |
1508 | sprintf (buf, "range [%s..%s] includes element `%s' in case expression", | |
1509 | enum_name_string (TREE_OPERAND (new_value, 0), type), | |
1510 | enum_name_string (TREE_OPERAND (new_value, 1), type), | |
1511 | enum_name_string (old_value, type)); | |
1512 | else | |
1513 | sprintf (buf, "range [%d..%d] includes (%d) in case expression", | |
1514 | TREE_INT_CST_LOW (TREE_OPERAND (new_value, 0)), | |
1515 | TREE_INT_CST_LOW (TREE_OPERAND (new_value, 1)), | |
1516 | TREE_INT_CST_LOW (old_value)); | |
1517 | error (buf); | |
1518 | } | |
1519 | else if (TREE_CODE (old_value) == RANGE_EXPR) | |
1520 | { | |
1521 | char *buf = (char *)alloca (4 * (8 + TYPE_NAME_LENGTH (type))); | |
1522 | if (TREE_CODE (type) == ENUMERAL_TYPE) | |
1523 | sprintf (buf, "range [%s..%s] includes element `%s' in case expression", | |
1524 | enum_name_string (TREE_OPERAND (old_value, 0), type), | |
1525 | enum_name_string (TREE_OPERAND (old_value, 1), type), | |
1526 | enum_name_string (new_value, type)); | |
1527 | else | |
1528 | sprintf (buf, "range [%d..%d] includes (%d) in case expression", | |
1529 | TREE_INT_CST_LOW (TREE_OPERAND (old_value, 0)), | |
1530 | TREE_INT_CST_LOW (TREE_OPERAND (old_value, 1)), | |
1531 | TREE_INT_CST_LOW (new_value)); | |
1532 | error (buf); | |
1533 | } | |
1534 | else | |
1535 | { | |
1536 | if (TREE_CODE (type) == ENUMERAL_TYPE) | |
1537 | error ("duplicate label `%s' in switch statement", | |
1538 | enum_name_string (new_value, type)); | |
1539 | else | |
1540 | error ("duplicate label (%d) in switch statement", | |
1541 | TREE_INT_CST_LOW (new_value)); | |
1542 | } | |
1543 | } | |
1544 | else if (code == 3) | |
1545 | { | |
1546 | if (TREE_CODE (type) == ENUMERAL_TYPE) | |
1547 | warning ("case value out of range for enum %s", | |
1548 | TYPE_NAME_STRING (type)); | |
1549 | else | |
1550 | warning ("case value out of range"); | |
1551 | } | |
1552 | else if (code == 4) | |
1553 | { | |
1554 | if (TREE_CODE (type) == ENUMERAL_TYPE) | |
1555 | error ("range values `%s' and `%s' reversed", | |
1556 | enum_name_string (new_value, type), | |
1557 | enum_name_string (old_value, type)); | |
1558 | else | |
1559 | error ("range values reversed"); | |
1560 | } | |
1561 | } |