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