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