xref: /openbsd/gnu/usr.bin/gcc/gcc/cp/semantics.c (revision 4e43c760)
1 /* Perform the semantic phase of parsing, i.e., the process of
2    building tree structure, checking semantic consistency, and
3    building RTL.  These routines are used both during actual parsing
4    and during the instantiation of template functions.
5 
6    Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
7    Written by Mark Mitchell (mmitchell@usa.net) based on code found
8    formerly in parse.y and pt.c.
9 
10    This file is part of GNU CC.
11 
12    GNU CC is free software; you can redistribute it and/or modify it
13    under the terms of the GNU General Public License as published by
14    the Free Software Foundation; either version 2, or (at your option)
15    any later version.
16 
17    GNU CC is distributed in the hope that it will be useful, but
18    WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20    General Public License for more details.
21 
22    You should have received a copy of the GNU General Public License
23    along with GNU CC; see the file COPYING.  If not, write to the Free
24    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25    02111-1307, USA.  */
26 
27 #include "config.h"
28 #include "system.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "tree-inline.h"
32 #include "except.h"
33 #include "lex.h"
34 #include "toplev.h"
35 #include "flags.h"
36 #include "ggc.h"
37 #include "rtl.h"
38 #include "expr.h"
39 #include "output.h"
40 #include "timevar.h"
41 #include "debug.h"
42 
43 /* There routines provide a modular interface to perform many parsing
44    operations.  They may therefore be used during actual parsing, or
45    during template instantiation, which may be regarded as a
46    degenerate form of parsing.  Since the current g++ parser is
47    lacking in several respects, and will be reimplemented, we are
48    attempting to move most code that is not directly related to
49    parsing into this file; that will make implementing the new parser
50    much easier since it will be able to make use of these routines.  */
51 
52 static tree maybe_convert_cond PARAMS ((tree));
53 static tree simplify_aggr_init_exprs_r PARAMS ((tree *, int *, void *));
54 static void deferred_type_access_control PARAMS ((void));
55 static void emit_associated_thunks PARAMS ((tree));
56 static void genrtl_try_block PARAMS ((tree));
57 static void genrtl_eh_spec_block PARAMS ((tree));
58 static void genrtl_handler PARAMS ((tree));
59 static void genrtl_named_return_value PARAMS ((void));
60 static void cp_expand_stmt PARAMS ((tree));
61 static void genrtl_start_function PARAMS ((tree));
62 static void genrtl_finish_function PARAMS ((tree));
63 static tree clear_decl_rtl PARAMS ((tree *, int *, void *));
64 
65 /* Finish processing the COND, the SUBSTMT condition for STMT.  */
66 
67 #define FINISH_COND(COND, STMT, SUBSTMT) 		\
68   do {							\
69     if (last_tree != (STMT))				\
70       {							\
71         RECHAIN_STMTS (STMT, SUBSTMT);			\
72         if (!processing_template_decl)			\
73           {						\
74 	    (COND) = build_tree_list (SUBSTMT, COND);	\
75 	    (SUBSTMT) = (COND);				\
76           }						\
77       }							\
78     else						\
79       (SUBSTMT) = (COND);				\
80   } while (0)
81 
82 /* Returns nonzero if the current statement is a full expression,
83    i.e. temporaries created during that statement should be destroyed
84    at the end of the statement.  */
85 
86 int
stmts_are_full_exprs_p()87 stmts_are_full_exprs_p ()
88 {
89   return current_stmt_tree ()->stmts_are_full_exprs_p;
90 }
91 
92 /* Returns the stmt_tree (if any) to which statements are currently
93    being added.  If there is no active statement-tree, NULL is
94    returned.  */
95 
96 stmt_tree
current_stmt_tree()97 current_stmt_tree ()
98 {
99   return (cfun
100 	  ? &cfun->language->base.x_stmt_tree
101 	  : &scope_chain->x_stmt_tree);
102 }
103 
104 /* Nonzero if TYPE is an anonymous union or struct type.  We have to use a
105    flag for this because "A union for which objects or pointers are
106    declared is not an anonymous union" [class.union].  */
107 
108 int
anon_aggr_type_p(node)109 anon_aggr_type_p (node)
110      tree node;
111 {
112   return ANON_AGGR_TYPE_P (node);
113 }
114 
115 /* Finish a scope.  */
116 
117 tree
do_poplevel()118 do_poplevel ()
119 {
120   tree block = NULL_TREE;
121 
122   if (stmts_are_full_exprs_p ())
123     {
124       tree scope_stmts = NULL_TREE;
125 
126       if (!processing_template_decl)
127 	scope_stmts = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/0);
128 
129       block = poplevel (kept_level_p (), 1, 0);
130       if (block && !processing_template_decl)
131 	{
132 	  SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
133 	  SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
134 	}
135     }
136 
137   return block;
138 }
139 
140 /* Begin a new scope.  */
141 
142 void
do_pushlevel()143 do_pushlevel ()
144 {
145   if (stmts_are_full_exprs_p ())
146     {
147       pushlevel (0);
148       if (!processing_template_decl)
149 	add_scope_stmt (/*begin_p=*/1, /*partial_p=*/0);
150     }
151 }
152 
153 /* Finish a goto-statement.  */
154 
155 tree
finish_goto_stmt(destination)156 finish_goto_stmt (destination)
157      tree destination;
158 {
159   if (TREE_CODE (destination) == IDENTIFIER_NODE)
160     destination = lookup_label (destination);
161 
162   /* We warn about unused labels with -Wunused.  That means we have to
163      mark the used labels as used.  */
164   if (TREE_CODE (destination) == LABEL_DECL)
165     TREE_USED (destination) = 1;
166 
167   if (TREE_CODE (destination) != LABEL_DECL)
168     /* We don't inline calls to functions with computed gotos.
169        Those functions are typically up to some funny business,
170        and may be depending on the labels being at particular
171        addresses, or some such.  */
172     DECL_UNINLINABLE (current_function_decl) = 1;
173 
174   check_goto (destination);
175 
176   return add_stmt (build_stmt (GOTO_STMT, destination));
177 }
178 
179 /* COND is the condition-expression for an if, while, etc.,
180    statement.  Convert it to a boolean value, if appropriate.  */
181 
182 tree
maybe_convert_cond(cond)183 maybe_convert_cond (cond)
184      tree cond;
185 {
186   /* Empty conditions remain empty.  */
187   if (!cond)
188     return NULL_TREE;
189 
190   /* Wait until we instantiate templates before doing conversion.  */
191   if (processing_template_decl)
192     return cond;
193 
194   /* Do the conversion.  */
195   cond = convert_from_reference (cond);
196   return condition_conversion (cond);
197 }
198 
199 /* Finish an expression-statement, whose EXPRESSION is as indicated.  */
200 
201 tree
finish_expr_stmt(expr)202 finish_expr_stmt (expr)
203      tree expr;
204 {
205   tree r = NULL_TREE;
206   tree expr_type = NULL_TREE;;
207 
208   if (expr != NULL_TREE)
209     {
210       if (!processing_template_decl
211 	  && !(stmts_are_full_exprs_p ())
212 	  && ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
213 	       && lvalue_p (expr))
214 	      || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE))
215 	expr = default_conversion (expr);
216 
217       /* Remember the type of the expression.  */
218       expr_type = TREE_TYPE (expr);
219 
220       if (stmts_are_full_exprs_p ())
221 	expr = convert_to_void (expr, "statement");
222 
223       r = add_stmt (build_stmt (EXPR_STMT, expr));
224     }
225 
226   finish_stmt ();
227 
228   /* This was an expression-statement, so we save the type of the
229      expression.  */
230   last_expr_type = expr_type;
231 
232   return r;
233 }
234 
235 
236 /* Begin an if-statement.  Returns a newly created IF_STMT if
237    appropriate.  */
238 
239 tree
begin_if_stmt()240 begin_if_stmt ()
241 {
242   tree r;
243   do_pushlevel ();
244   r = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
245   add_stmt (r);
246   return r;
247 }
248 
249 /* Process the COND of an if-statement, which may be given by
250    IF_STMT.  */
251 
252 void
finish_if_stmt_cond(cond,if_stmt)253 finish_if_stmt_cond (cond, if_stmt)
254      tree cond;
255      tree if_stmt;
256 {
257   cond = maybe_convert_cond (cond);
258   FINISH_COND (cond, if_stmt, IF_COND (if_stmt));
259 }
260 
261 /* Finish the then-clause of an if-statement, which may be given by
262    IF_STMT.  */
263 
264 tree
finish_then_clause(if_stmt)265 finish_then_clause (if_stmt)
266      tree if_stmt;
267 {
268   RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
269   return if_stmt;
270 }
271 
272 /* Begin the else-clause of an if-statement.  */
273 
274 void
begin_else_clause()275 begin_else_clause ()
276 {
277 }
278 
279 /* Finish the else-clause of an if-statement, which may be given by
280    IF_STMT.  */
281 
282 void
finish_else_clause(if_stmt)283 finish_else_clause (if_stmt)
284      tree if_stmt;
285 {
286   RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
287 }
288 
289 /* Finish an if-statement.  */
290 
291 void
finish_if_stmt()292 finish_if_stmt ()
293 {
294   finish_stmt ();
295   do_poplevel ();
296 }
297 
298 /* Begin a while-statement.  Returns a newly created WHILE_STMT if
299    appropriate.  */
300 
301 tree
begin_while_stmt()302 begin_while_stmt ()
303 {
304   tree r;
305   r = build_stmt (WHILE_STMT, NULL_TREE, NULL_TREE);
306   add_stmt (r);
307   do_pushlevel ();
308   return r;
309 }
310 
311 /* Process the COND of a while-statement, which may be given by
312    WHILE_STMT.  */
313 
314 void
finish_while_stmt_cond(cond,while_stmt)315 finish_while_stmt_cond (cond, while_stmt)
316      tree cond;
317      tree while_stmt;
318 {
319   cond = maybe_convert_cond (cond);
320   if (processing_template_decl)
321     /* Don't mess with condition decls in a template.  */
322     FINISH_COND (cond, while_stmt, WHILE_COND (while_stmt));
323   else if (getdecls () == NULL_TREE)
324     /* It was a simple condition; install it.  */
325     WHILE_COND (while_stmt) = cond;
326   else
327     {
328       /* If there was a declaration in the condition, we can't leave it
329 	 there; transform
330 	    while (A x = 42) { }
331 	 to
332 	    while (true) { A x = 42; if (!x) break; }  */
333       tree if_stmt;
334       WHILE_COND (while_stmt) = boolean_true_node;
335 
336       if_stmt = begin_if_stmt ();
337       cond = build_unary_op (TRUTH_NOT_EXPR, cond, 0);
338       finish_if_stmt_cond (cond, if_stmt);
339       finish_break_stmt ();
340       finish_then_clause (if_stmt);
341       finish_if_stmt ();
342     }
343 }
344 
345 /* Finish a while-statement, which may be given by WHILE_STMT.  */
346 
347 void
finish_while_stmt(while_stmt)348 finish_while_stmt (while_stmt)
349      tree while_stmt;
350 {
351   do_poplevel ();
352   RECHAIN_STMTS (while_stmt, WHILE_BODY (while_stmt));
353   finish_stmt ();
354 }
355 
356 /* Begin a do-statement.  Returns a newly created DO_STMT if
357    appropriate.  */
358 
359 tree
begin_do_stmt()360 begin_do_stmt ()
361 {
362   tree r = build_stmt (DO_STMT, NULL_TREE, NULL_TREE);
363   add_stmt (r);
364   return r;
365 }
366 
367 /* Finish the body of a do-statement, which may be given by DO_STMT.  */
368 
369 void
finish_do_body(do_stmt)370 finish_do_body (do_stmt)
371      tree do_stmt;
372 {
373   RECHAIN_STMTS (do_stmt, DO_BODY (do_stmt));
374 }
375 
376 /* Finish a do-statement, which may be given by DO_STMT, and whose
377    COND is as indicated.  */
378 
379 void
finish_do_stmt(cond,do_stmt)380 finish_do_stmt (cond, do_stmt)
381      tree cond;
382      tree do_stmt;
383 {
384   cond = maybe_convert_cond (cond);
385   DO_COND (do_stmt) = cond;
386   finish_stmt ();
387 }
388 
389 /* Finish a return-statement.  The EXPRESSION returned, if any, is as
390    indicated.  */
391 
392 tree
finish_return_stmt(expr)393 finish_return_stmt (expr)
394      tree expr;
395 {
396   tree r;
397 
398   if (!processing_template_decl)
399     expr = check_return_expr (expr);
400   if (!processing_template_decl)
401     {
402       if (DECL_DESTRUCTOR_P (current_function_decl))
403 	{
404 	  /* Similarly, all destructors must run destructors for
405 	     base-classes before returning.  So, all returns in a
406 	     destructor get sent to the DTOR_LABEL; finish_function emits
407 	     code to return a value there.  */
408 	  return finish_goto_stmt (dtor_label);
409 	}
410     }
411   r = add_stmt (build_stmt (RETURN_STMT, expr));
412   finish_stmt ();
413 
414   return r;
415 }
416 
417 /* Begin a for-statement.  Returns a new FOR_STMT if appropriate.  */
418 
419 tree
begin_for_stmt()420 begin_for_stmt ()
421 {
422   tree r;
423 
424   r = build_stmt (FOR_STMT, NULL_TREE, NULL_TREE,
425 		  NULL_TREE, NULL_TREE);
426   NEW_FOR_SCOPE_P (r) = flag_new_for_scope > 0;
427   if (NEW_FOR_SCOPE_P (r))
428     {
429       do_pushlevel ();
430       note_level_for_for ();
431     }
432   add_stmt (r);
433 
434   return r;
435 }
436 
437 /* Finish the for-init-statement of a for-statement, which may be
438    given by FOR_STMT.  */
439 
440 void
finish_for_init_stmt(for_stmt)441 finish_for_init_stmt (for_stmt)
442      tree for_stmt;
443 {
444   if (last_tree != for_stmt)
445     RECHAIN_STMTS (for_stmt, FOR_INIT_STMT (for_stmt));
446   do_pushlevel ();
447 }
448 
449 /* Finish the COND of a for-statement, which may be given by
450    FOR_STMT.  */
451 
452 void
finish_for_cond(cond,for_stmt)453 finish_for_cond (cond, for_stmt)
454      tree cond;
455      tree for_stmt;
456 {
457   cond = maybe_convert_cond (cond);
458   if (processing_template_decl)
459     /* Don't mess with condition decls in a template.  */
460     FINISH_COND (cond, for_stmt, FOR_COND (for_stmt));
461   else if (getdecls () == NULL_TREE)
462     /* It was a simple condition; install it.  */
463     FOR_COND (for_stmt) = cond;
464   else
465     {
466       /* If there was a declaration in the condition, we can't leave it
467 	 there; transform
468 	    for (; A x = 42;) { }
469 	 to
470 	    for (;;) { A x = 42; if (!x) break; }  */
471       tree if_stmt;
472       FOR_COND (for_stmt) = NULL_TREE;
473 
474       if_stmt = begin_if_stmt ();
475       cond = build_unary_op (TRUTH_NOT_EXPR, cond, 0);
476       finish_if_stmt_cond (cond, if_stmt);
477       finish_break_stmt ();
478       finish_then_clause (if_stmt);
479       finish_if_stmt ();
480     }
481 }
482 
483 /* Finish the increment-EXPRESSION in a for-statement, which may be
484    given by FOR_STMT.  */
485 
486 void
finish_for_expr(expr,for_stmt)487 finish_for_expr (expr, for_stmt)
488      tree expr;
489      tree for_stmt;
490 {
491   FOR_EXPR (for_stmt) = expr;
492 }
493 
494 /* Finish the body of a for-statement, which may be given by
495    FOR_STMT.  The increment-EXPR for the loop must be
496    provided.  */
497 
498 void
finish_for_stmt(for_stmt)499 finish_for_stmt (for_stmt)
500      tree for_stmt;
501 {
502   /* Pop the scope for the body of the loop.  */
503   do_poplevel ();
504   RECHAIN_STMTS (for_stmt, FOR_BODY (for_stmt));
505   if (NEW_FOR_SCOPE_P (for_stmt))
506     do_poplevel ();
507   finish_stmt ();
508 }
509 
510 /* Finish a break-statement.  */
511 
512 tree
finish_break_stmt()513 finish_break_stmt ()
514 {
515   return add_stmt (build_break_stmt ());
516 }
517 
518 /* Finish a continue-statement.  */
519 
520 tree
finish_continue_stmt()521 finish_continue_stmt ()
522 {
523   return add_stmt (build_continue_stmt ());
524 }
525 
526 /* Begin a switch-statement.  Returns a new SWITCH_STMT if
527    appropriate.  */
528 
529 tree
begin_switch_stmt()530 begin_switch_stmt ()
531 {
532   tree r;
533   do_pushlevel ();
534   r = build_stmt (SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
535   add_stmt (r);
536   return r;
537 }
538 
539 /* Finish the cond of a switch-statement.  */
540 
541 void
finish_switch_cond(cond,switch_stmt)542 finish_switch_cond (cond, switch_stmt)
543      tree cond;
544      tree switch_stmt;
545 {
546   tree orig_type = NULL;
547   if (!processing_template_decl)
548     {
549       tree index;
550 
551       /* Convert the condition to an integer or enumeration type.  */
552       cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, 1);
553       if (cond == NULL_TREE)
554 	{
555 	  error ("switch quantity not an integer");
556 	  cond = error_mark_node;
557 	}
558       orig_type = TREE_TYPE (cond);
559       if (cond != error_mark_node)
560 	{
561 	  cond = default_conversion (cond);
562 	  cond = fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (cond), cond));
563 	}
564 
565       if (cond != error_mark_node)
566 	{
567 	  index = get_unwidened (cond, NULL_TREE);
568 	  /* We can't strip a conversion from a signed type to an unsigned,
569 	     because if we did, int_fits_type_p would do the wrong thing
570 	     when checking case values for being in range,
571 	     and it's too hard to do the right thing.  */
572 	  if (TREE_UNSIGNED (TREE_TYPE (cond))
573 	      == TREE_UNSIGNED (TREE_TYPE (index)))
574 	    cond = index;
575 	}
576     }
577   FINISH_COND (cond, switch_stmt, SWITCH_COND (switch_stmt));
578   SWITCH_TYPE (switch_stmt) = orig_type;
579   push_switch (switch_stmt);
580 }
581 
582 /* Finish the body of a switch-statement, which may be given by
583    SWITCH_STMT.  The COND to switch on is indicated.  */
584 
585 void
finish_switch_stmt(switch_stmt)586 finish_switch_stmt (switch_stmt)
587      tree switch_stmt;
588 {
589   RECHAIN_STMTS (switch_stmt, SWITCH_BODY (switch_stmt));
590   pop_switch ();
591   finish_stmt ();
592   do_poplevel ();
593 }
594 
595 /* Generate the RTL for T, which is a TRY_BLOCK.  */
596 
597 static void
genrtl_try_block(t)598 genrtl_try_block (t)
599      tree t;
600 {
601   if (CLEANUP_P (t))
602     {
603       expand_eh_region_start ();
604       expand_stmt (TRY_STMTS (t));
605       expand_eh_region_end_cleanup (TRY_HANDLERS (t));
606     }
607   else
608     {
609       if (!FN_TRY_BLOCK_P (t))
610 	emit_line_note (input_filename, lineno);
611 
612       expand_eh_region_start ();
613       expand_stmt (TRY_STMTS (t));
614 
615       if (FN_TRY_BLOCK_P (t))
616 	{
617 	  expand_start_all_catch ();
618 	  in_function_try_handler = 1;
619 	  expand_stmt (TRY_HANDLERS (t));
620 	  in_function_try_handler = 0;
621 	  expand_end_all_catch ();
622 	}
623       else
624 	{
625 	  expand_start_all_catch ();
626 	  expand_stmt (TRY_HANDLERS (t));
627 	  expand_end_all_catch ();
628 	}
629     }
630 }
631 
632 /* Generate the RTL for T, which is an EH_SPEC_BLOCK.  */
633 
634 static void
genrtl_eh_spec_block(t)635 genrtl_eh_spec_block (t)
636      tree t;
637 {
638   expand_eh_region_start ();
639   expand_stmt (EH_SPEC_STMTS (t));
640   expand_eh_region_end_allowed (EH_SPEC_RAISES (t),
641 				build_call (call_unexpected_node,
642 					    tree_cons (NULL_TREE,
643 						       build_exc_ptr (),
644 						       NULL_TREE)));
645 }
646 
647 /* Begin a try-block.  Returns a newly-created TRY_BLOCK if
648    appropriate.  */
649 
650 tree
begin_try_block()651 begin_try_block ()
652 {
653   tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
654   add_stmt (r);
655   return r;
656 }
657 
658 /* Likewise, for a function-try-block.  */
659 
660 tree
begin_function_try_block()661 begin_function_try_block ()
662 {
663   tree r = build_stmt (TRY_BLOCK, NULL_TREE, NULL_TREE);
664   FN_TRY_BLOCK_P (r) = 1;
665   add_stmt (r);
666   return r;
667 }
668 
669 /* Finish a try-block, which may be given by TRY_BLOCK.  */
670 
671 void
finish_try_block(try_block)672 finish_try_block (try_block)
673      tree try_block;
674 {
675   RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
676 }
677 
678 /* Finish the body of a cleanup try-block, which may be given by
679    TRY_BLOCK.  */
680 
681 void
finish_cleanup_try_block(try_block)682 finish_cleanup_try_block (try_block)
683      tree try_block;
684 {
685   RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
686 }
687 
688 /* Finish an implicitly generated try-block, with a cleanup is given
689    by CLEANUP.  */
690 
691 void
finish_cleanup(cleanup,try_block)692 finish_cleanup (cleanup, try_block)
693      tree cleanup;
694      tree try_block;
695 {
696   TRY_HANDLERS (try_block) = cleanup;
697   CLEANUP_P (try_block) = 1;
698 }
699 
700 /* Likewise, for a function-try-block.  */
701 
702 void
finish_function_try_block(try_block)703 finish_function_try_block (try_block)
704      tree try_block;
705 {
706   if (TREE_CHAIN (try_block)
707       && TREE_CODE (TREE_CHAIN (try_block)) == CTOR_INITIALIZER)
708     {
709       /* Chain the compound statement after the CTOR_INITIALIZER.  */
710       TREE_CHAIN (TREE_CHAIN (try_block)) = last_tree;
711       /* And make the CTOR_INITIALIZER the body of the try-block.  */
712       RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
713     }
714   else
715     RECHAIN_STMTS (try_block, TRY_STMTS (try_block));
716   in_function_try_handler = 1;
717 }
718 
719 /* Finish a handler-sequence for a try-block, which may be given by
720    TRY_BLOCK.  */
721 
722 void
finish_handler_sequence(try_block)723 finish_handler_sequence (try_block)
724      tree try_block;
725 {
726   RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
727   check_handlers (TRY_HANDLERS (try_block));
728 }
729 
730 /* Likewise, for a function-try-block.  */
731 
732 void
finish_function_handler_sequence(try_block)733 finish_function_handler_sequence (try_block)
734      tree try_block;
735 {
736   in_function_try_handler = 0;
737   RECHAIN_STMTS (try_block, TRY_HANDLERS (try_block));
738   check_handlers (TRY_HANDLERS (try_block));
739 }
740 
741 /* Generate the RTL for T, which is a HANDLER.  */
742 
743 static void
genrtl_handler(t)744 genrtl_handler (t)
745      tree t;
746 {
747   genrtl_do_pushlevel ();
748   if (!processing_template_decl)
749     expand_start_catch (HANDLER_TYPE (t));
750   expand_stmt (HANDLER_BODY (t));
751   if (!processing_template_decl)
752     expand_end_catch ();
753 }
754 
755 /* Begin a handler.  Returns a HANDLER if appropriate.  */
756 
757 tree
begin_handler()758 begin_handler ()
759 {
760   tree r;
761   r = build_stmt (HANDLER, NULL_TREE, NULL_TREE);
762   add_stmt (r);
763   /* Create a binding level for the eh_info and the exception object
764      cleanup.  */
765   do_pushlevel ();
766   note_level_for_catch ();
767   return r;
768 }
769 
770 /* Finish the handler-parameters for a handler, which may be given by
771    HANDLER.  DECL is the declaration for the catch parameter, or NULL
772    if this is a `catch (...)' clause.  */
773 
774 void
finish_handler_parms(decl,handler)775 finish_handler_parms (decl, handler)
776      tree decl;
777      tree handler;
778 {
779   tree type = NULL_TREE;
780   if (processing_template_decl)
781     {
782       if (decl)
783 	{
784 	  decl = pushdecl (decl);
785 	  decl = push_template_decl (decl);
786 	  add_decl_stmt (decl);
787 	  RECHAIN_STMTS (handler, HANDLER_PARMS (handler));
788 	  type = TREE_TYPE (decl);
789 	}
790     }
791   else
792     type = expand_start_catch_block (decl);
793 
794   HANDLER_TYPE (handler) = type;
795 }
796 
797 /* Finish a handler, which may be given by HANDLER.  The BLOCKs are
798    the return value from the matching call to finish_handler_parms.  */
799 
800 void
finish_handler(handler)801 finish_handler (handler)
802      tree handler;
803 {
804   if (!processing_template_decl)
805     expand_end_catch_block ();
806   do_poplevel ();
807   RECHAIN_STMTS (handler, HANDLER_BODY (handler));
808 }
809 
810 /* Begin a compound-statement.  If HAS_NO_SCOPE is nonzero, the
811    compound-statement does not define a scope.  Returns a new
812    COMPOUND_STMT if appropriate.  */
813 
814 tree
begin_compound_stmt(has_no_scope)815 begin_compound_stmt (has_no_scope)
816      int has_no_scope;
817 {
818   tree r;
819   int is_try = 0;
820 
821   r = build_stmt (COMPOUND_STMT, NULL_TREE);
822 
823   if (last_tree && TREE_CODE (last_tree) == TRY_BLOCK)
824     is_try = 1;
825 
826   add_stmt (r);
827   if (has_no_scope)
828     COMPOUND_STMT_NO_SCOPE (r) = 1;
829 
830   last_expr_type = NULL_TREE;
831 
832   if (!has_no_scope)
833     {
834       do_pushlevel ();
835       if (is_try)
836       	note_level_for_try ();
837     }
838   else
839     /* Normally, we try hard to keep the BLOCK for a
840        statement-expression.  But, if it's a statement-expression with
841        a scopeless block, there's nothing to keep, and we don't want
842        to accidentally keep a block *inside* the scopeless block.  */
843     keep_next_level (0);
844 
845   return r;
846 }
847 
848 /* Finish a compound-statement, which may be given by COMPOUND_STMT.
849    If HAS_NO_SCOPE is nonzero, the compound statement does not define
850    a scope.  */
851 
852 tree
finish_compound_stmt(has_no_scope,compound_stmt)853 finish_compound_stmt (has_no_scope, compound_stmt)
854      int has_no_scope;
855      tree compound_stmt;
856 {
857   tree r;
858   tree t;
859 
860   if (!has_no_scope)
861     r = do_poplevel ();
862   else
863     r = NULL_TREE;
864 
865   RECHAIN_STMTS (compound_stmt, COMPOUND_BODY (compound_stmt));
866 
867   /* When we call finish_stmt we will lose LAST_EXPR_TYPE.  But, since
868      the precise purpose of that variable is store the type of the
869      last expression statement within the last compound statement, we
870      preserve the value.  */
871   t = last_expr_type;
872   finish_stmt ();
873   last_expr_type = t;
874 
875   return r;
876 }
877 
878 /* Finish an asm-statement, whose components are a CV_QUALIFIER, a
879    STRING, some OUTPUT_OPERANDS, some INPUT_OPERANDS, and some
880    CLOBBERS.  */
881 
882 tree
finish_asm_stmt(cv_qualifier,string,output_operands,input_operands,clobbers)883 finish_asm_stmt (cv_qualifier, string, output_operands,
884 		 input_operands, clobbers)
885      tree cv_qualifier;
886      tree string;
887      tree output_operands;
888      tree input_operands;
889      tree clobbers;
890 {
891   tree r;
892   tree t;
893 
894   if (cv_qualifier != NULL_TREE
895       && cv_qualifier != ridpointers[(int) RID_VOLATILE])
896     {
897       warning ("%s qualifier ignored on asm",
898 		  IDENTIFIER_POINTER (cv_qualifier));
899       cv_qualifier = NULL_TREE;
900     }
901 
902   if (!processing_template_decl)
903     {
904       int i;
905       int ninputs;
906       int noutputs;
907 
908       for (t = input_operands; t; t = TREE_CHAIN (t))
909 	{
910 	  tree converted_operand
911 	    = decay_conversion (TREE_VALUE (t));
912 
913 	  /* If the type of the operand hasn't been determined (e.g.,
914 	     because it involves an overloaded function), then issue
915 	     an error message.  There's no context available to
916 	     resolve the overloading.  */
917 	  if (TREE_TYPE (converted_operand) == unknown_type_node)
918 	    {
919 	      error ("type of asm operand `%E' could not be determined",
920 			TREE_VALUE (t));
921 	      converted_operand = error_mark_node;
922 	    }
923 	  TREE_VALUE (t) = converted_operand;
924 	}
925 
926       ninputs = list_length (input_operands);
927       noutputs = list_length (output_operands);
928 
929       for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
930 	{
931 	  bool allows_mem;
932 	  bool allows_reg;
933 	  bool is_inout;
934 	  const char *constraint;
935 	  tree operand;
936 
937 	  constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
938 	  operand = TREE_VALUE (t);
939 
940 	  if (!parse_output_constraint (&constraint,
941 					i, ninputs, noutputs,
942 					&allows_mem,
943 					&allows_reg,
944 					&is_inout))
945 	    {
946 	      /* By marking the type as erroneous, we will not try to
947 		 process this operand again in expand_asm_operands.  */
948 	      TREE_TYPE (operand) = error_mark_node;
949 	      continue;
950 	    }
951 
952 	  /* If the operand is a DECL that is going to end up in
953 	     memory, assume it is addressable.  This is a bit more
954 	     conservative than it would ideally be; the exact test is
955 	     buried deep in expand_asm_operands and depends on the
956 	     DECL_RTL for the OPERAND -- which we don't have at this
957 	     point.  */
958 	  if (!allows_reg && DECL_P (operand))
959 	    cxx_mark_addressable (operand);
960 	}
961     }
962 
963   r = build_stmt (ASM_STMT, cv_qualifier, string,
964 		  output_operands, input_operands,
965 		  clobbers);
966   return add_stmt (r);
967 }
968 
969 /* Finish a label with the indicated NAME.  */
970 
971 void
finish_label_stmt(name)972 finish_label_stmt (name)
973      tree name;
974 {
975   tree decl = define_label (input_filename, lineno, name);
976   add_stmt (build_stmt (LABEL_STMT, decl));
977 }
978 
979 /* Finish a series of declarations for local labels.  G++ allows users
980    to declare "local" labels, i.e., labels with scope.  This extension
981    is useful when writing code involving statement-expressions.  */
982 
983 void
finish_label_decl(name)984 finish_label_decl (name)
985      tree name;
986 {
987   tree decl = declare_local_label (name);
988   add_decl_stmt (decl);
989 }
990 
991 /* When DECL goes out of scope, make sure that CLEANUP is executed.  */
992 
993 void
finish_decl_cleanup(decl,cleanup)994 finish_decl_cleanup (decl, cleanup)
995      tree decl;
996      tree cleanup;
997 {
998   add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
999 }
1000 
1001 /* If the current scope exits with an exception, run CLEANUP.  */
1002 
1003 void
finish_eh_cleanup(cleanup)1004 finish_eh_cleanup (cleanup)
1005      tree cleanup;
1006 {
1007   tree r = build_stmt (CLEANUP_STMT, NULL_TREE, cleanup);
1008   CLEANUP_EH_ONLY (r) = 1;
1009   add_stmt (r);
1010 }
1011 
1012 /* Generate the RTL for a RETURN_INIT.  */
1013 
1014 static void
genrtl_named_return_value()1015 genrtl_named_return_value ()
1016 {
1017   tree decl = DECL_RESULT (current_function_decl);
1018 
1019   /* If this named return value comes in a register, put it in a
1020      pseudo-register.  */
1021   if (DECL_REGISTER (decl))
1022     {
1023       /* Note that the mode of the old DECL_RTL may be wider than the
1024 	 mode of DECL_RESULT, depending on the calling conventions for
1025 	 the processor.  For example, on the Alpha, a 32-bit integer
1026 	 is returned in a DImode register -- the DECL_RESULT has
1027 	 SImode but the DECL_RTL for the DECL_RESULT has DImode.  So,
1028 	 here, we use the mode the back-end has already assigned for
1029 	 the return value.  */
1030       SET_DECL_RTL (decl, gen_reg_rtx (GET_MODE (DECL_RTL (decl))));
1031       if (TREE_ADDRESSABLE (decl))
1032 	put_var_into_stack (decl, /*rescan=*/true);
1033     }
1034 
1035   emit_local_var (decl);
1036 }
1037 
1038 /* Bind a name and initialization to the return value of
1039    the current function.  */
1040 
1041 void
finish_named_return_value(return_id,init)1042 finish_named_return_value (return_id, init)
1043      tree return_id, init;
1044 {
1045   tree decl = DECL_RESULT (current_function_decl);
1046 
1047   /* Give this error as many times as there are occurrences, so that
1048      users can use Emacs compilation buffers to find and fix all such
1049      places.  */
1050   if (pedantic)
1051     pedwarn ("ISO C++ does not permit named return values");
1052   cp_deprecated ("the named return value extension");
1053 
1054   if (return_id != NULL_TREE)
1055     {
1056       if (DECL_NAME (decl) == NULL_TREE)
1057 	DECL_NAME (decl) = return_id;
1058       else
1059 	{
1060 	  error ("return identifier `%D' already in place", return_id);
1061 	  return;
1062 	}
1063     }
1064 
1065   /* Can't let this happen for constructors.  */
1066   if (DECL_CONSTRUCTOR_P (current_function_decl))
1067     {
1068       error ("can't redefine default return value for constructors");
1069       return;
1070     }
1071 
1072   /* If we have a named return value, put that in our scope as well.  */
1073   if (DECL_NAME (decl) != NULL_TREE)
1074     {
1075       /* Let `cp_finish_decl' know that this initializer is ok.  */
1076       DECL_INITIAL (decl) = init;
1077       if (doing_semantic_analysis_p ())
1078 	pushdecl (decl);
1079       if (!processing_template_decl)
1080 	{
1081 	  cp_finish_decl (decl, init, NULL_TREE, 0);
1082 	  add_stmt (build_stmt (RETURN_INIT, NULL_TREE, NULL_TREE));
1083 	}
1084       else
1085 	add_stmt (build_stmt (RETURN_INIT, return_id, init));
1086     }
1087 
1088   /* Don't use tree-inlining for functions with named return values.
1089      That doesn't work properly because we don't do any translation of
1090      the RETURN_INITs when they are copied.  */
1091   DECL_UNINLINABLE (current_function_decl) = 1;
1092 }
1093 
1094 /* Begin processing a mem-initializer-list.  */
1095 
1096 void
begin_mem_initializers()1097 begin_mem_initializers ()
1098 {
1099   if (! DECL_CONSTRUCTOR_P (current_function_decl))
1100     error ("only constructors take base initializers");
1101 }
1102 
1103 /* The MEM_INITS is a list of mem-initializers, in reverse of the
1104    order they were written by the user.  Each node is as for
1105    emit_mem_initializers.  */
1106 
1107 void
finish_mem_initializers(tree mem_inits)1108 finish_mem_initializers (tree mem_inits)
1109 {
1110   /* Reorder the MEM_INITS so that they are in the order they appeared
1111      in the source program.  */
1112   mem_inits = nreverse (mem_inits);
1113 
1114   if (processing_template_decl)
1115     add_stmt (build_min_nt (CTOR_INITIALIZER, mem_inits));
1116   else
1117     emit_mem_initializers (mem_inits);
1118 }
1119 
1120 /* Returns the stack of SCOPE_STMTs for the current function.  */
1121 
1122 tree *
current_scope_stmt_stack()1123 current_scope_stmt_stack ()
1124 {
1125   return &cfun->language->base.x_scope_stmt_stack;
1126 }
1127 
1128 /* Finish a parenthesized expression EXPR.  */
1129 
1130 tree
finish_parenthesized_expr(expr)1131 finish_parenthesized_expr (expr)
1132      tree expr;
1133 {
1134   if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expr))))
1135     /* This inhibits warnings in c_common_truthvalue_conversion.  */
1136     C_SET_EXP_ORIGINAL_CODE (expr, ERROR_MARK);
1137 
1138   if (TREE_CODE (expr) == OFFSET_REF)
1139     /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
1140        enclosed in parentheses.  */
1141     PTRMEM_OK_P (expr) = 0;
1142   return expr;
1143 }
1144 
1145 /* Begin a statement-expression.  The value returned must be passed to
1146    finish_stmt_expr.  */
1147 
1148 tree
begin_stmt_expr()1149 begin_stmt_expr ()
1150 {
1151   /* If we're outside a function, we won't have a statement-tree to
1152      work with.  But, if we see a statement-expression we need to
1153      create one.  */
1154   if (! cfun && !last_tree)
1155     begin_stmt_tree (&scope_chain->x_saved_tree);
1156 
1157   keep_next_level (1);
1158   /* If we're building a statement tree, then the upcoming compound
1159      statement will be chained onto the tree structure, starting at
1160      last_tree.  We return last_tree so that we can later unhook the
1161      compound statement.  */
1162   return last_tree;
1163 }
1164 
1165 /* Used when beginning a statement-expression outside function scope.
1166    For example, when handling a file-scope initializer, we use this
1167    function.  */
1168 
1169 tree
begin_global_stmt_expr()1170 begin_global_stmt_expr ()
1171 {
1172   if (! cfun && !last_tree)
1173     begin_stmt_tree (&scope_chain->x_saved_tree);
1174 
1175   keep_next_level (1);
1176 
1177   return last_tree ? last_tree : expand_start_stmt_expr(/*has_scope=*/1);
1178 }
1179 
1180 /* Finish the STMT_EXPR last begun with begin_global_stmt_expr.  */
1181 
1182 tree
finish_global_stmt_expr(stmt_expr)1183 finish_global_stmt_expr (stmt_expr)
1184      tree stmt_expr;
1185 {
1186   stmt_expr = expand_end_stmt_expr (stmt_expr);
1187 
1188   if (! cfun
1189       && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1190     finish_stmt_tree (&scope_chain->x_saved_tree);
1191 
1192   return stmt_expr;
1193 }
1194 
1195 /* Finish a statement-expression.  RTL_EXPR should be the value
1196    returned by the previous begin_stmt_expr; EXPR is the
1197    statement-expression.  Returns an expression representing the
1198    statement-expression.  */
1199 
1200 tree
finish_stmt_expr(rtl_expr)1201 finish_stmt_expr (rtl_expr)
1202      tree rtl_expr;
1203 {
1204   tree result;
1205 
1206   /* If the last thing in the statement-expression was not an
1207      expression-statement, then it has type `void'.  */
1208   if (!last_expr_type)
1209     last_expr_type = void_type_node;
1210   result = build_min (STMT_EXPR, last_expr_type, last_tree);
1211   TREE_SIDE_EFFECTS (result) = 1;
1212 
1213   /* Remove the compound statement from the tree structure; it is
1214      now saved in the STMT_EXPR.  */
1215   last_tree = rtl_expr;
1216   TREE_CHAIN (last_tree) = NULL_TREE;
1217 
1218   /* If we created a statement-tree for this statement-expression,
1219      remove it now.  */
1220   if (! cfun
1221       && TREE_CHAIN (scope_chain->x_saved_tree) == NULL_TREE)
1222     finish_stmt_tree (&scope_chain->x_saved_tree);
1223 
1224   return result;
1225 }
1226 
1227 /* Generate an expression for `FN (ARGS)'.
1228 
1229    If DISALLOW_VIRTUAL is true, the call to FN will be not generated
1230    as a virtual call, even if FN is virtual.  (This flag is set when
1231    encountering an expression where the function name is explicitly
1232    qualified.  For example a call to `X::f' never generates a virtual
1233    call.)
1234 
1235    Returns code for the call.  */
1236 
1237 tree
finish_call_expr(tree fn,tree args,bool disallow_virtual)1238 finish_call_expr (tree fn, tree args, bool disallow_virtual)
1239 {
1240   if (fn == error_mark_node || args == error_mark_node)
1241     return error_mark_node;
1242 
1243   if (processing_template_decl)
1244     return build_nt (CALL_EXPR, fn, args, NULL_TREE);
1245 
1246   /* ARGS should be a list of arguments.  */
1247   my_friendly_assert (!args || TREE_CODE (args) == TREE_LIST,
1248 		      20020712);
1249 
1250   if (BASELINK_P (fn))
1251     {
1252       tree object;
1253 
1254       /* A call to a member function.  From [over.call.func]:
1255 
1256 	   If the keyword this is in scope and refers to the class of
1257 	   that member function, or a derived class thereof, then the
1258 	   function call is transformed into a qualified function call
1259 	   using (*this) as the postfix-expression to the left of the
1260 	   . operator.... [Otherwise] a contrived object of type T
1261 	   becomes the implied object argument.
1262 
1263         This paragraph is unclear about this situation:
1264 
1265 	  struct A { void f(); };
1266 	  struct B : public A {};
1267 	  struct C : public A { void g() { B::f(); }};
1268 
1269 	In particular, for `B::f', this paragraph does not make clear
1270 	whether "the class of that member function" refers to `A' or
1271 	to `B'.  We believe it refers to `B'.  */
1272       if (current_class_type
1273 	  && DERIVED_FROM_P (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
1274 			     current_class_type)
1275 	  && current_class_ref)
1276 	object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
1277 				     NULL);
1278       else
1279 	{
1280 	  tree representative_fn;
1281 
1282 	  representative_fn = BASELINK_FUNCTIONS (fn);
1283 	  if (TREE_CODE (representative_fn) == TEMPLATE_ID_EXPR)
1284 	    representative_fn = TREE_OPERAND (representative_fn, 0);
1285 	  representative_fn = get_first_fn (representative_fn);
1286 	  object = build_dummy_object (DECL_CONTEXT (representative_fn));
1287 	}
1288 
1289       return build_new_method_call (object, fn, args, NULL_TREE,
1290 				    (disallow_virtual
1291 				     ? LOOKUP_NONVIRTUAL : 0));
1292     }
1293   else if (is_overloaded_fn (fn))
1294     /* A call to a namespace-scope function.  */
1295     return build_new_function_call (fn, args);
1296   else if (CLASS_TYPE_P (TREE_TYPE (fn)))
1297     {
1298       /* If the "function" is really an object of class type, it might
1299 	 have an overloaded `operator ()'.  */
1300       tree result;
1301       result = build_opfncall (CALL_EXPR, LOOKUP_NORMAL, fn, args, NULL_TREE);
1302       if (result)
1303 	return result;
1304     }
1305 
1306   /* A call where the function is unknown.  */
1307   return build_function_call (fn, args);
1308 }
1309 
1310 /* Finish a call to a postfix increment or decrement or EXPR.  (Which
1311    is indicated by CODE, which should be POSTINCREMENT_EXPR or
1312    POSTDECREMENT_EXPR.)  */
1313 
1314 tree
finish_increment_expr(expr,code)1315 finish_increment_expr (expr, code)
1316      tree expr;
1317      enum tree_code code;
1318 {
1319   /* If we get an OFFSET_REF, turn it into what it really means (e.g.,
1320      a COMPONENT_REF).  This way if we've got, say, a reference to a
1321      static member that's being operated on, we don't end up trying to
1322      find a member operator for the class it's in.  */
1323 
1324   if (TREE_CODE (expr) == OFFSET_REF)
1325     expr = resolve_offset_ref (expr);
1326   return build_x_unary_op (code, expr);
1327 }
1328 
1329 /* Finish a use of `this'.  Returns an expression for `this'.  */
1330 
1331 tree
finish_this_expr()1332 finish_this_expr ()
1333 {
1334   tree result;
1335 
1336   if (current_class_ptr)
1337     {
1338       result = current_class_ptr;
1339     }
1340   else if (current_function_decl
1341 	   && DECL_STATIC_FUNCTION_P (current_function_decl))
1342     {
1343       error ("`this' is unavailable for static member functions");
1344       result = error_mark_node;
1345     }
1346   else
1347     {
1348       if (current_function_decl)
1349 	error ("invalid use of `this' in non-member function");
1350       else
1351 	error ("invalid use of `this' at top level");
1352       result = error_mark_node;
1353     }
1354 
1355   return result;
1356 }
1357 
1358 /* Finish a member function call using OBJECT and ARGS as arguments to
1359    FN.  Returns an expression for the call.  */
1360 
1361 tree
finish_object_call_expr(fn,object,args)1362 finish_object_call_expr (fn, object, args)
1363      tree fn;
1364      tree object;
1365      tree args;
1366 {
1367   if (DECL_DECLARES_TYPE_P (fn))
1368     {
1369       if (processing_template_decl)
1370 	/* This can happen on code like:
1371 
1372 	   class X;
1373 	   template <class T> void f(T t) {
1374 	     t.X();
1375 	   }
1376 
1377 	   We just grab the underlying IDENTIFIER.  */
1378 	fn = DECL_NAME (fn);
1379       else
1380 	{
1381 	  error ("calling type `%T' like a method", fn);
1382 	  return error_mark_node;
1383 	}
1384     }
1385 
1386   if (processing_template_decl || name_p (fn))
1387     return build_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1388   else
1389     return build_new_method_call (object, fn, args, NULL_TREE, LOOKUP_NORMAL);
1390 }
1391 
1392 /* Finish a qualified member function call using OBJECT and ARGS as
1393    arguments to FN.  Returns an expression for the call.  */
1394 
1395 tree
finish_qualified_object_call_expr(fn,object,args)1396 finish_qualified_object_call_expr (fn, object, args)
1397      tree fn;
1398      tree object;
1399      tree args;
1400 {
1401   return build_scoped_method_call (object, TREE_OPERAND (fn, 0),
1402 				   TREE_OPERAND (fn, 1), args);
1403 }
1404 
1405 /* Finish a pseudo-destructor call expression of OBJECT, with SCOPE
1406    being the scope, if any, of DESTRUCTOR.  Returns an expression for
1407    the call.  */
1408 
1409 tree
finish_pseudo_destructor_call_expr(object,scope,destructor)1410 finish_pseudo_destructor_call_expr (object, scope, destructor)
1411      tree object;
1412      tree scope;
1413      tree destructor;
1414 {
1415   if (processing_template_decl)
1416     return build_min_nt (PSEUDO_DTOR_EXPR, object, scope, destructor);
1417 
1418   if (scope && scope != destructor)
1419     error ("destructor specifier `%T::~%T()' must have matching names",
1420 	      scope, destructor);
1421 
1422   if ((scope == NULL_TREE || IDENTIFIER_GLOBAL_VALUE (destructor))
1423       && (TREE_CODE (TREE_TYPE (object)) !=
1424 	  TREE_CODE (TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (destructor)))))
1425     error ("`%E' is not of type `%T'", object, destructor);
1426 
1427   return cp_convert (void_type_node, object);
1428 }
1429 
1430 /* Finish an expression of the form CODE EXPR.  */
1431 
1432 tree
finish_unary_op_expr(code,expr)1433 finish_unary_op_expr (code, expr)
1434      enum tree_code code;
1435      tree expr;
1436 {
1437   tree result = build_x_unary_op (code, expr);
1438   /* Inside a template, build_x_unary_op does not fold the
1439      expression. So check whether the result is folded before
1440      setting TREE_NEGATED_INT.  */
1441   if (code == NEGATE_EXPR && TREE_CODE (expr) == INTEGER_CST
1442       && TREE_CODE (result) == INTEGER_CST
1443       && !TREE_UNSIGNED (TREE_TYPE (result))
1444       && INT_CST_LT (result, integer_zero_node))
1445     TREE_NEGATED_INT (result) = 1;
1446   overflow_warning (result);
1447   return result;
1448 }
1449 
1450 /* Finish an id-expression.  */
1451 
1452 tree
finish_id_expr(expr)1453 finish_id_expr (expr)
1454      tree expr;
1455 {
1456   if (TREE_CODE (expr) == IDENTIFIER_NODE)
1457     expr = do_identifier (expr, 1, NULL_TREE);
1458 
1459   if (TREE_TYPE (expr) == error_mark_node)
1460     expr = error_mark_node;
1461   return expr;
1462 }
1463 
1464 /* Return the declaration for the function-name variable indicated by
1465    ID.  */
1466 
1467 tree
finish_fname(tree id)1468 finish_fname (tree id)
1469 {
1470   tree decl;
1471 
1472   decl = fname_decl (C_RID_CODE (id), id);
1473   if (processing_template_decl)
1474     decl = build_min_nt (LOOKUP_EXPR, DECL_NAME (decl));
1475   return decl;
1476 }
1477 
1478 static tree current_type_lookups;
1479 
1480 /* Perform deferred access control for types used in the type of a
1481    declaration.  */
1482 
1483 static void
deferred_type_access_control()1484 deferred_type_access_control ()
1485 {
1486   tree lookup = type_lookups;
1487 
1488   if (lookup == error_mark_node)
1489     return;
1490 
1491   for (; lookup; lookup = TREE_CHAIN (lookup))
1492     enforce_access (TREE_PURPOSE (lookup), TREE_VALUE (lookup));
1493 }
1494 
1495 void
decl_type_access_control(decl)1496 decl_type_access_control (decl)
1497      tree decl;
1498 {
1499   tree save_fn;
1500 
1501   if (type_lookups == error_mark_node)
1502     return;
1503 
1504   save_fn = current_function_decl;
1505 
1506   if (decl && TREE_CODE (decl) == FUNCTION_DECL)
1507     current_function_decl = decl;
1508 
1509   deferred_type_access_control ();
1510 
1511   current_function_decl = save_fn;
1512 
1513   /* Now strip away the checks for the current declarator; they were
1514      added to type_lookups after typed_declspecs saved the copy that
1515      ended up in current_type_lookups.  */
1516   type_lookups = current_type_lookups;
1517 }
1518 
1519 void
save_type_access_control(lookups)1520 save_type_access_control (lookups)
1521      tree lookups;
1522 {
1523   current_type_lookups = lookups;
1524 }
1525 
1526 /* Reset the deferred access control.  */
1527 
1528 void
reset_type_access_control()1529 reset_type_access_control ()
1530 {
1531   type_lookups = NULL_TREE;
1532   current_type_lookups = NULL_TREE;
1533 }
1534 
1535 /* Begin a function definition declared with DECL_SPECS, ATTRIBUTES,
1536    and DECLARATOR.  Returns nonzero if the function-declaration is
1537    valid.  */
1538 
1539 int
begin_function_definition(decl_specs,attributes,declarator)1540 begin_function_definition (decl_specs, attributes, declarator)
1541      tree decl_specs;
1542      tree attributes;
1543      tree declarator;
1544 {
1545   if (!start_function (decl_specs, declarator, attributes, SF_DEFAULT))
1546     return 0;
1547 
1548   deferred_type_access_control ();
1549   type_lookups = error_mark_node;
1550 
1551   /* The things we're about to see are not directly qualified by any
1552      template headers we've seen thus far.  */
1553   reset_specialization ();
1554 
1555   return 1;
1556 }
1557 
1558 /* Begin a constructor declarator of the form `SCOPE::NAME'.  Returns
1559    a SCOPE_REF.  */
1560 
1561 tree
begin_constructor_declarator(scope,name)1562 begin_constructor_declarator (scope, name)
1563      tree scope;
1564      tree name;
1565 {
1566   tree result = build_nt (SCOPE_REF, scope, name);
1567   enter_scope_of (result);
1568   return result;
1569 }
1570 
1571 /* Finish an init-declarator.  Returns a DECL.  */
1572 
1573 tree
finish_declarator(declarator,declspecs,attributes,prefix_attributes,initialized)1574 finish_declarator (declarator, declspecs, attributes,
1575 		   prefix_attributes, initialized)
1576      tree declarator;
1577      tree declspecs;
1578      tree attributes;
1579      tree prefix_attributes;
1580      int initialized;
1581 {
1582   return start_decl (declarator, declspecs, initialized, attributes,
1583 		     prefix_attributes);
1584 }
1585 
1586 /* Finish a translation unit.  */
1587 
1588 void
finish_translation_unit()1589 finish_translation_unit ()
1590 {
1591   /* In case there were missing closebraces,
1592      get us back to the global binding level.  */
1593   pop_everything ();
1594   while (current_namespace != global_namespace)
1595     pop_namespace ();
1596 
1597   /* Do file scope __FUNCTION__ et al.  */
1598   finish_fname_decls ();
1599 
1600   finish_file ();
1601 }
1602 
1603 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
1604    Returns the parameter.  */
1605 
1606 tree
finish_template_type_parm(aggr,identifier)1607 finish_template_type_parm (aggr, identifier)
1608      tree aggr;
1609      tree identifier;
1610 {
1611   if (aggr != class_type_node)
1612     {
1613       pedwarn ("template type parameters must use the keyword `class' or `typename'");
1614       aggr = class_type_node;
1615     }
1616 
1617   return build_tree_list (aggr, identifier);
1618 }
1619 
1620 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
1621    Returns the parameter.  */
1622 
1623 tree
finish_template_template_parm(aggr,identifier)1624 finish_template_template_parm (aggr, identifier)
1625      tree aggr;
1626      tree identifier;
1627 {
1628   tree decl = build_decl (TYPE_DECL, identifier, NULL_TREE);
1629   tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
1630   DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
1631   DECL_TEMPLATE_RESULT (tmpl) = decl;
1632   DECL_ARTIFICIAL (decl) = 1;
1633   end_template_decl ();
1634 
1635   my_friendly_assert (DECL_TEMPLATE_PARMS (tmpl), 20010110);
1636 
1637   return finish_template_type_parm (aggr, tmpl);
1638 }
1639 
1640 /* ARGUMENT is the default-argument value for a template template
1641    parameter.  If ARGUMENT is invalid, issue error messages and return
1642    the ERROR_MARK_NODE.  Otherwise, ARGUMENT itself is returned.  */
1643 
1644 tree
check_template_template_default_arg(tree argument)1645 check_template_template_default_arg (tree argument)
1646 {
1647   if (TREE_CODE (argument) != TEMPLATE_DECL
1648       && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
1649       && TREE_CODE (argument) != TYPE_DECL
1650       && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
1651     {
1652       error ("invalid default template argument");
1653       return error_mark_node;
1654     }
1655 
1656   return argument;
1657 }
1658 
1659 /* Finish a parameter list, indicated by PARMS.  If ELLIPSIS is
1660    nonzero, the parameter list was terminated by a `...'.  */
1661 
1662 tree
finish_parmlist(parms,ellipsis)1663 finish_parmlist (parms, ellipsis)
1664      tree parms;
1665      int ellipsis;
1666 {
1667   if (parms)
1668     {
1669       /* We mark the PARMS as a parmlist so that declarator processing can
1670          disambiguate certain constructs.  */
1671       TREE_PARMLIST (parms) = 1;
1672       /* We do not append void_list_node here, but leave it to grokparms
1673          to do that.  */
1674       PARMLIST_ELLIPSIS_P (parms) = ellipsis;
1675     }
1676   return parms;
1677 }
1678 
1679 /* Begin a class definition, as indicated by T.  */
1680 
1681 tree
begin_class_definition(t)1682 begin_class_definition (t)
1683      tree t;
1684 {
1685   if (t == error_mark_node)
1686     return error_mark_node;
1687 
1688   /* Check the bases are accessible.  */
1689   decl_type_access_control (TYPE_NAME (t));
1690   reset_type_access_control ();
1691 
1692   if (processing_template_parmlist)
1693     {
1694       error ("definition of `%#T' inside template parameter list", t);
1695       return error_mark_node;
1696     }
1697 
1698   /* In a definition of a member class template, we will get here with
1699      an implicit typename.  */
1700   if (IMPLICIT_TYPENAME_P (t))
1701     t = TREE_TYPE (t);
1702   /* A non-implicit typename comes from code like:
1703 
1704        template <typename T> struct A {
1705          template <typename U> struct A<T>::B ...
1706 
1707      This is erroneous.  */
1708   else if (TREE_CODE (t) == TYPENAME_TYPE)
1709     {
1710       error ("invalid definition of qualified type `%T'", t);
1711       t = error_mark_node;
1712     }
1713 
1714   if (t == error_mark_node || ! IS_AGGR_TYPE (t))
1715     {
1716       t = make_aggr_type (RECORD_TYPE);
1717       pushtag (make_anon_name (), t, 0);
1718     }
1719 
1720   /* If we generated a partial instantiation of this type, but now
1721      we're seeing a real definition, we're actually looking at a
1722      partial specialization.  Consider:
1723 
1724        template <class T, class U>
1725        struct Y {};
1726 
1727        template <class T>
1728        struct X {};
1729 
1730        template <class T, class U>
1731        void f()
1732        {
1733 	 typename X<Y<T, U> >::A a;
1734        }
1735 
1736        template <class T, class U>
1737        struct X<Y<T, U> >
1738        {
1739        };
1740 
1741      We have to undo the effects of the previous partial
1742      instantiation.  */
1743   if (PARTIAL_INSTANTIATION_P (t))
1744     {
1745       if (!pedantic)
1746 	{
1747 	  /* Unfortunately, when we're not in pedantic mode, we
1748 	     attempt to actually fill in some of the fields of the
1749 	     partial instantiation, in order to support the implicit
1750 	     typename extension.  Clear those fields now, in
1751 	     preparation for the definition here.  The fields cleared
1752 	     here must match those set in instantiate_class_template.
1753 	     Look for a comment mentioning begin_class_definition
1754 	     there.  */
1755 	  TYPE_BINFO_BASETYPES (t) = NULL_TREE;
1756 	  TYPE_FIELDS (t) = NULL_TREE;
1757 	  TYPE_METHODS (t) = NULL_TREE;
1758 	  CLASSTYPE_DECL_LIST (t) = NULL_TREE;
1759 	  CLASSTYPE_NESTED_UDTS (t) = NULL;
1760 	  CLASSTYPE_VBASECLASSES (t) = NULL_TREE;
1761 	  TYPE_SIZE (t) = NULL_TREE;
1762 	}
1763 
1764       /* This isn't a partial instantiation any more.  */
1765       PARTIAL_INSTANTIATION_P (t) = 0;
1766     }
1767   /* If this type was already complete, and we see another definition,
1768      that's an error.  */
1769   else if (COMPLETE_TYPE_P (t))
1770     duplicate_tag_error (t);
1771 
1772   /* Update the location of the decl.  */
1773   DECL_SOURCE_FILE (TYPE_NAME (t)) = input_filename;
1774   DECL_SOURCE_LINE (TYPE_NAME (t)) = lineno;
1775 
1776   if (TYPE_BEING_DEFINED (t))
1777     {
1778       t = make_aggr_type (TREE_CODE (t));
1779       pushtag (TYPE_IDENTIFIER (t), t, 0);
1780     }
1781   maybe_process_partial_specialization (t);
1782   pushclass (t, 1);
1783   TYPE_BEING_DEFINED (t) = 1;
1784   TYPE_PACKED (t) = flag_pack_struct;
1785   /* Reset the interface data, at the earliest possible
1786      moment, as it might have been set via a class foo;
1787      before.  */
1788   if (! TYPE_ANONYMOUS_P (t))
1789     {
1790       CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1791       SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1792 	(t, interface_unknown);
1793     }
1794   reset_specialization();
1795 
1796   /* Make a declaration for this class in its own scope.  */
1797   build_self_reference ();
1798 
1799   return t;
1800 }
1801 
1802 /* Finish the member declaration given by DECL.  */
1803 
1804 void
finish_member_declaration(decl)1805 finish_member_declaration (decl)
1806      tree decl;
1807 {
1808   if (decl == error_mark_node || decl == NULL_TREE)
1809     return;
1810 
1811   if (decl == void_type_node)
1812     /* The COMPONENT was a friend, not a member, and so there's
1813        nothing for us to do.  */
1814     return;
1815 
1816   /* We should see only one DECL at a time.  */
1817   my_friendly_assert (TREE_CHAIN (decl) == NULL_TREE, 0);
1818 
1819   /* Set up access control for DECL.  */
1820   TREE_PRIVATE (decl)
1821     = (current_access_specifier == access_private_node);
1822   TREE_PROTECTED (decl)
1823     = (current_access_specifier == access_protected_node);
1824   if (TREE_CODE (decl) == TEMPLATE_DECL)
1825     {
1826       TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
1827       TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
1828     }
1829 
1830   /* Mark the DECL as a member of the current class.  */
1831   DECL_CONTEXT (decl) = current_class_type;
1832 
1833   /* [dcl.link]
1834 
1835      A C language linkage is ignored for the names of class members
1836      and the member function type of class member functions.  */
1837   if (DECL_LANG_SPECIFIC (decl) && DECL_LANGUAGE (decl) == lang_c)
1838     SET_DECL_LANGUAGE (decl, lang_cplusplus);
1839 
1840   /* Put functions on the TYPE_METHODS list and everything else on the
1841      TYPE_FIELDS list.  Note that these are built up in reverse order.
1842      We reverse them (to obtain declaration order) in finish_struct.  */
1843   if (TREE_CODE (decl) == FUNCTION_DECL
1844       || DECL_FUNCTION_TEMPLATE_P (decl))
1845     {
1846       /* We also need to add this function to the
1847 	 CLASSTYPE_METHOD_VEC.  */
1848       add_method (current_class_type, decl, /*error_p=*/0);
1849 
1850       TREE_CHAIN (decl) = TYPE_METHODS (current_class_type);
1851       TYPE_METHODS (current_class_type) = decl;
1852 
1853       maybe_add_class_template_decl_list (current_class_type, decl,
1854 					  /*friend_p=*/0);
1855     }
1856   /* Enter the DECL into the scope of the class.  */
1857   else if (TREE_CODE (decl) == USING_DECL || pushdecl_class_level (decl))
1858     {
1859       /* All TYPE_DECLs go at the end of TYPE_FIELDS.  Ordinary fields
1860 	 go at the beginning.  The reason is that lookup_field_1
1861 	 searches the list in order, and we want a field name to
1862 	 override a type name so that the "struct stat hack" will
1863 	 work.  In particular:
1864 
1865 	   struct S { enum E { }; int E } s;
1866 	   s.E = 3;
1867 
1868 	 is valid.  In addition, the FIELD_DECLs must be maintained in
1869 	 declaration order so that class layout works as expected.
1870 	 However, we don't need that order until class layout, so we
1871 	 save a little time by putting FIELD_DECLs on in reverse order
1872 	 here, and then reversing them in finish_struct_1.  (We could
1873 	 also keep a pointer to the correct insertion points in the
1874 	 list.)  */
1875 
1876       if (TREE_CODE (decl) == TYPE_DECL)
1877 	TYPE_FIELDS (current_class_type)
1878 	  = chainon (TYPE_FIELDS (current_class_type), decl);
1879       else
1880 	{
1881 	  TREE_CHAIN (decl) = TYPE_FIELDS (current_class_type);
1882 	  TYPE_FIELDS (current_class_type) = decl;
1883 	}
1884 
1885       maybe_add_class_template_decl_list (current_class_type, decl,
1886 					  /*friend_p=*/0);
1887     }
1888 }
1889 
1890 /* Finish a class definition T with the indicate ATTRIBUTES.  If SEMI,
1891    the definition is immediately followed by a semicolon.  Returns the
1892    type.  */
1893 
1894 tree
finish_class_definition(t,attributes,semi,pop_scope_p)1895 finish_class_definition (t, attributes, semi, pop_scope_p)
1896      tree t;
1897      tree attributes;
1898      int semi;
1899      int pop_scope_p;
1900 {
1901   if (t == error_mark_node)
1902     return error_mark_node;
1903 
1904   /* finish_struct nukes this anyway; if finish_exception does too,
1905      then it can go.  */
1906   if (semi)
1907     note_got_semicolon (t);
1908 
1909   /* If we got any attributes in class_head, xref_tag will stick them in
1910      TREE_TYPE of the type.  Grab them now.  */
1911   attributes = chainon (TYPE_ATTRIBUTES (t), attributes);
1912   TYPE_ATTRIBUTES (t) = NULL_TREE;
1913 
1914   if (TREE_CODE (t) == ENUMERAL_TYPE)
1915     ;
1916   else
1917     {
1918       t = finish_struct (t, attributes);
1919       if (semi)
1920 	note_got_semicolon (t);
1921     }
1922 
1923   if (! semi)
1924     check_for_missing_semicolon (t);
1925   if (pop_scope_p)
1926     pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (t)));
1927   if (current_scope () == current_function_decl)
1928     do_pending_defargs ();
1929 
1930   return t;
1931 }
1932 
1933 /* Finish processing the default argument expressions cached during
1934    the processing of a class definition.  */
1935 
1936 void
begin_inline_definitions()1937 begin_inline_definitions ()
1938 {
1939   if (current_scope () == current_function_decl)
1940     do_pending_inlines ();
1941 }
1942 
1943 /* Finish processing the declaration of a member class template
1944    TYPES whose template parameters are given by PARMS.  */
1945 
1946 tree
finish_member_class_template(types)1947 finish_member_class_template (types)
1948      tree types;
1949 {
1950   tree t;
1951 
1952   /* If there are declared, but undefined, partial specializations
1953      mixed in with the typespecs they will not yet have passed through
1954      maybe_process_partial_specialization, so we do that here.  */
1955   for (t = types; t != NULL_TREE; t = TREE_CHAIN (t))
1956     if (IS_AGGR_TYPE_CODE (TREE_CODE (TREE_VALUE (t))))
1957       maybe_process_partial_specialization (TREE_VALUE (t));
1958 
1959   note_list_got_semicolon (types);
1960   grok_x_components (types);
1961   if (TYPE_CONTEXT (TREE_VALUE (types)) != current_class_type)
1962     /* The component was in fact a friend declaration.  We avoid
1963        finish_member_template_decl performing certain checks by
1964        unsetting TYPES.  */
1965     types = NULL_TREE;
1966 
1967   finish_member_template_decl (types);
1968 
1969   /* As with other component type declarations, we do
1970      not store the new DECL on the list of
1971      component_decls.  */
1972   return NULL_TREE;
1973 }
1974 
1975 /* Finish processing a complete template declaration.  The PARMS are
1976    the template parameters.  */
1977 
1978 void
finish_template_decl(parms)1979 finish_template_decl (parms)
1980      tree parms;
1981 {
1982   if (parms)
1983     end_template_decl ();
1984   else
1985     end_specialization ();
1986 }
1987 
1988 /* Finish processing a template-id (which names a type) of the form
1989    NAME < ARGS >.  Return the TYPE_DECL for the type named by the
1990    template-id.  If ENTERING_SCOPE is nonzero we are about to enter
1991    the scope of template-id indicated.  */
1992 
1993 tree
finish_template_type(name,args,entering_scope)1994 finish_template_type (name, args, entering_scope)
1995      tree name;
1996      tree args;
1997      int entering_scope;
1998 {
1999   tree decl;
2000 
2001   decl = lookup_template_class (name, args,
2002 				NULL_TREE, NULL_TREE,
2003 	                        entering_scope, /*complain=*/1);
2004   if (decl != error_mark_node)
2005     decl = TYPE_STUB_DECL (decl);
2006 
2007   return decl;
2008 }
2009 
2010 /* SR is a SCOPE_REF node.  Enter the scope of SR, whether it is a
2011    namespace scope or a class scope.  */
2012 
2013 void
enter_scope_of(sr)2014 enter_scope_of (sr)
2015      tree sr;
2016 {
2017   tree scope = TREE_OPERAND (sr, 0);
2018 
2019   if (TREE_CODE (scope) == NAMESPACE_DECL)
2020     {
2021       push_decl_namespace (scope);
2022       TREE_COMPLEXITY (sr) = -1;
2023     }
2024   else if (scope != current_class_type)
2025     {
2026       if (TREE_CODE (scope) == TYPENAME_TYPE)
2027 	{
2028 	  /* In a declarator for a template class member, the scope will
2029 	     get here as an implicit typename, a TYPENAME_TYPE with a type.  */
2030 	  scope = TREE_TYPE (scope);
2031 	  TREE_OPERAND (sr, 0) = scope;
2032 	}
2033       push_nested_class (scope, 3);
2034       TREE_COMPLEXITY (sr) = current_class_depth;
2035     }
2036 }
2037 
2038 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
2039    Return a TREE_LIST containing the ACCESS_SPECIFIER and the
2040    BASE_CLASS, or NULL_TREE if an error occurred.  The
2041    ACCESS_SPECIFIER is one of
2042    access_{default,public,protected_private}[_virtual]_node.*/
2043 
2044 tree
finish_base_specifier(access_specifier,base_class)2045 finish_base_specifier (access_specifier, base_class)
2046      tree access_specifier;
2047      tree base_class;
2048 {
2049   tree result;
2050 
2051   if (base_class == error_mark_node)
2052     {
2053       error ("invalid base-class specification");
2054       result = NULL_TREE;
2055     }
2056   else if (! is_aggr_type (base_class, 1))
2057     result = NULL_TREE;
2058   else
2059     {
2060       if (cp_type_quals (base_class) != 0)
2061         {
2062           error ("base class `%T' has cv qualifiers", base_class);
2063           base_class = TYPE_MAIN_VARIANT (base_class);
2064         }
2065       result = build_tree_list (access_specifier, base_class);
2066     }
2067 
2068   return result;
2069 }
2070 
2071 /* Called when multiple declarators are processed.  If that is not
2072    premitted in this context, an error is issued.  */
2073 
2074 void
check_multiple_declarators()2075 check_multiple_declarators ()
2076 {
2077   /* [temp]
2078 
2079      In a template-declaration, explicit specialization, or explicit
2080      instantiation the init-declarator-list in the declaration shall
2081      contain at most one declarator.
2082 
2083      We don't just use PROCESSING_TEMPLATE_DECL for the first
2084      condition since that would disallow the perfectly valid code,
2085      like `template <class T> struct S { int i, j; };'.  */
2086   if (at_function_scope_p ())
2087     /* It's OK to write `template <class T> void f() { int i, j;}'.  */
2088     return;
2089 
2090   if (PROCESSING_REAL_TEMPLATE_DECL_P ()
2091       || processing_explicit_instantiation
2092       || processing_specialization)
2093     error ("multiple declarators in template declaration");
2094 }
2095 
2096 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
2097    use as a type-specifier.  */
2098 
2099 tree
finish_typeof(expr)2100 finish_typeof (expr)
2101      tree expr;
2102 {
2103   tree type;
2104 
2105   if (processing_template_decl)
2106     {
2107       type = make_aggr_type (TYPEOF_TYPE);
2108       TYPE_FIELDS (type) = expr;
2109 
2110       return type;
2111     }
2112 
2113   if (TREE_CODE (expr) == OFFSET_REF)
2114     expr = resolve_offset_ref (expr);
2115 
2116   type = TREE_TYPE (expr);
2117 
2118   if (!type || type == unknown_type_node)
2119     {
2120       error ("type of `%E' is unknown", expr);
2121       return error_mark_node;
2122     }
2123 
2124   return type;
2125 }
2126 
2127 /* Compute the value of the `sizeof' operator.  */
2128 
2129 tree
finish_sizeof(t)2130 finish_sizeof (t)
2131      tree t;
2132 {
2133   if (processing_template_decl)
2134     return build_min_nt (SIZEOF_EXPR, t);
2135 
2136   return TYPE_P (t) ? cxx_sizeof (t) : expr_sizeof (t);
2137 }
2138 
2139 /* Implement the __alignof keyword: Return the minimum required
2140    alignment of T, measured in bytes.  */
2141 
2142 tree
finish_alignof(t)2143 finish_alignof (t)
2144      tree t;
2145 {
2146   if (processing_template_decl)
2147     return build_min_nt (ALIGNOF_EXPR, t);
2148 
2149   return TYPE_P (t) ? cxx_alignof (t) : c_alignof_expr (t);
2150 }
2151 
2152 /* Generate RTL for the statement T, and its substatements, and any
2153    other statements at its nesting level.  */
2154 
2155 static void
cp_expand_stmt(t)2156 cp_expand_stmt (t)
2157      tree t;
2158 {
2159   switch (TREE_CODE (t))
2160     {
2161     case TRY_BLOCK:
2162       genrtl_try_block (t);
2163       break;
2164 
2165     case EH_SPEC_BLOCK:
2166       genrtl_eh_spec_block (t);
2167       break;
2168 
2169     case HANDLER:
2170       genrtl_handler (t);
2171       break;
2172 
2173     case RETURN_INIT:
2174       genrtl_named_return_value ();
2175       break;
2176 
2177     case USING_STMT:
2178       break;
2179 
2180     default:
2181       abort ();
2182       break;
2183     }
2184 }
2185 
2186 /* Called from expand_body via walk_tree.  Replace all AGGR_INIT_EXPRs
2187    will equivalent CALL_EXPRs.  */
2188 
2189 static tree
simplify_aggr_init_exprs_r(tp,walk_subtrees,data)2190 simplify_aggr_init_exprs_r (tp, walk_subtrees, data)
2191      tree *tp;
2192      int *walk_subtrees ATTRIBUTE_UNUSED;
2193      void *data ATTRIBUTE_UNUSED;
2194 {
2195   tree aggr_init_expr;
2196   tree call_expr;
2197   tree fn;
2198   tree args;
2199   tree slot;
2200   tree type;
2201   int copy_from_buffer_p;
2202 
2203   aggr_init_expr = *tp;
2204   /* We don't need to walk into types; there's nothing in a type that
2205      needs simplification.  (And, furthermore, there are places we
2206      actively don't want to go.  For example, we don't want to wander
2207      into the default arguments for a FUNCTION_DECL that appears in a
2208      CALL_EXPR.)  */
2209   if (TYPE_P (aggr_init_expr))
2210     {
2211       *walk_subtrees = 0;
2212       return NULL_TREE;
2213     }
2214   /* Only AGGR_INIT_EXPRs are interesting.  */
2215   else if (TREE_CODE (aggr_init_expr) != AGGR_INIT_EXPR)
2216     return NULL_TREE;
2217 
2218   /* Form an appropriate CALL_EXPR.  */
2219   fn = TREE_OPERAND (aggr_init_expr, 0);
2220   args = TREE_OPERAND (aggr_init_expr, 1);
2221   slot = TREE_OPERAND (aggr_init_expr, 2);
2222   type = TREE_TYPE (aggr_init_expr);
2223   if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
2224     {
2225       /* Replace the first argument with the address of the third
2226 	 argument to the AGGR_INIT_EXPR.  */
2227       cxx_mark_addressable (slot);
2228       args = tree_cons (NULL_TREE,
2229 			build1 (ADDR_EXPR,
2230 				build_pointer_type (TREE_TYPE (slot)),
2231 				slot),
2232 			TREE_CHAIN (args));
2233     }
2234   call_expr = build (CALL_EXPR,
2235 		     TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
2236 		     fn, args, NULL_TREE);
2237   TREE_SIDE_EFFECTS (call_expr) = 1;
2238 
2239   /* If we're using the non-reentrant PCC calling convention, then we
2240      need to copy the returned value out of the static buffer into the
2241      SLOT.  */
2242   copy_from_buffer_p = 0;
2243 #ifdef PCC_STATIC_STRUCT_RETURN
2244   if (!AGGR_INIT_VIA_CTOR_P (aggr_init_expr) && aggregate_value_p (type))
2245     {
2246       int old_ac = flag_access_control;
2247 
2248       flag_access_control = 0;
2249       call_expr = build_aggr_init (slot, call_expr,
2250 				   DIRECT_BIND | LOOKUP_ONLYCONVERTING);
2251       flag_access_control = old_ac;
2252       copy_from_buffer_p = 1;
2253     }
2254 #endif
2255 
2256   /* If this AGGR_INIT_EXPR indicates the value returned by a
2257      function, then we want to use the value of the initialized
2258      location as the result.  */
2259   if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr) || copy_from_buffer_p)
2260     {
2261       call_expr = build (COMPOUND_EXPR, type,
2262 			 call_expr, slot);
2263       TREE_SIDE_EFFECTS (call_expr) = 1;
2264     }
2265 
2266   /* Replace the AGGR_INIT_EXPR with the CALL_EXPR.  */
2267   TREE_CHAIN (call_expr) = TREE_CHAIN (aggr_init_expr);
2268   *tp = call_expr;
2269 
2270   /* Keep iterating.  */
2271   return NULL_TREE;
2272 }
2273 
2274 /* Emit all thunks to FN that should be emitted when FN is emitted.  */
2275 
2276 static void
emit_associated_thunks(fn)2277 emit_associated_thunks (fn)
2278      tree fn;
2279 {
2280   /* When we use vcall offsets, we emit thunks with the virtual
2281      functions to which they thunk. The whole point of vcall offsets
2282      is so that you can know statically the entire set of thunks that
2283      will ever be needed for a given virtual function, thereby
2284      enabling you to output all the thunks with the function itself.  */
2285   if (DECL_VIRTUAL_P (fn))
2286     {
2287       tree thunk;
2288       for (thunk = DECL_THUNKS (fn); thunk; thunk = TREE_CHAIN (thunk))
2289 	use_thunk (thunk, /*emit_p=*/1);
2290     }
2291 }
2292 
2293 /* Generate RTL for FN.  */
2294 
2295 void
expand_body(fn)2296 expand_body (fn)
2297      tree fn;
2298 {
2299   int saved_lineno;
2300   const char *saved_input_filename;
2301   tree saved_function;
2302 
2303   /* When the parser calls us after finishing the body of a template
2304      function, we don't really want to expand the body.  When we're
2305      processing an in-class definition of an inline function,
2306      PROCESSING_TEMPLATE_DECL will no longer be set here, so we have
2307      to look at the function itself.  */
2308   if (processing_template_decl
2309       || (DECL_LANG_SPECIFIC (fn)
2310 	  && DECL_TEMPLATE_INFO (fn)
2311 	  && uses_template_parms (DECL_TI_ARGS (fn))))
2312     {
2313       /* Normally, collection only occurs in rest_of_compilation.  So,
2314 	 if we don't collect here, we never collect junk generated
2315 	 during the processing of templates until we hit a
2316 	 non-template function.  */
2317       ggc_collect ();
2318       return;
2319     }
2320 
2321   /* Replace AGGR_INIT_EXPRs with appropriate CALL_EXPRs.  */
2322   walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
2323 				simplify_aggr_init_exprs_r,
2324 				NULL);
2325 
2326   /* If this is a constructor or destructor body, we have to clone
2327      it.  */
2328   if (maybe_clone_body (fn))
2329     {
2330       /* We don't want to process FN again, so pretend we've written
2331 	 it out, even though we haven't.  */
2332       TREE_ASM_WRITTEN (fn) = 1;
2333       return;
2334     }
2335 
2336   /* There's no reason to do any of the work here if we're only doing
2337      semantic analysis; this code just generates RTL.  */
2338   if (flag_syntax_only)
2339     return;
2340 
2341   /* If possible, avoid generating RTL for this function.  Instead,
2342      just record it as an inline function, and wait until end-of-file
2343      to decide whether to write it out or not.  */
2344   if (/* We have to generate RTL if it's not an inline function.  */
2345       (DECL_INLINE (fn) || DECL_COMDAT (fn))
2346       /* Or if we have to emit code for inline functions anyhow.  */
2347       && !flag_keep_inline_functions
2348       /* Or if we actually have a reference to the function.  */
2349       && !DECL_NEEDED_P (fn))
2350     {
2351       /* Set DECL_EXTERNAL so that assemble_external will be called as
2352 	 necessary.  We'll clear it again in finish_file.  */
2353       if (!DECL_EXTERNAL (fn))
2354 	{
2355 	  DECL_NOT_REALLY_EXTERN (fn) = 1;
2356 	  DECL_EXTERNAL (fn) = 1;
2357 	}
2358       /* Remember this function.  In finish_file we'll decide if
2359 	 we actually need to write this function out.  */
2360       defer_fn (fn);
2361       /* Let the back-end know that this function exists.  */
2362       (*debug_hooks->deferred_inline_function) (fn);
2363       return;
2364     }
2365 
2366   /* Compute the appropriate object-file linkage for inline
2367      functions.  */
2368   if (DECL_DECLARED_INLINE_P (fn))
2369     import_export_decl (fn);
2370 
2371   /* If FN is external, then there's no point in generating RTL for
2372      it.  This situation can arise with an inline function under
2373      `-fexternal-templates'; we instantiate the function, even though
2374      we're not planning on emitting it, in case we get a chance to
2375      inline it.  */
2376   if (DECL_EXTERNAL (fn))
2377     return;
2378 
2379   /* Save the current file name and line number.  When we expand the
2380      body of the function, we'll set LINENO and INPUT_FILENAME so that
2381      error-mesages come out in the right places.  */
2382   saved_lineno = lineno;
2383   saved_input_filename = input_filename;
2384   saved_function = current_function_decl;
2385   lineno = DECL_SOURCE_LINE (fn);
2386   input_filename = DECL_SOURCE_FILE (fn);
2387   current_function_decl = fn;
2388 
2389   timevar_push (TV_INTEGRATION);
2390 
2391   /* Optimize the body of the function before expanding it.  */
2392   optimize_function (fn);
2393 
2394   timevar_pop (TV_INTEGRATION);
2395   timevar_push (TV_EXPAND);
2396 
2397   genrtl_start_function (fn);
2398   current_function_is_thunk = DECL_THUNK_P (fn);
2399 
2400   /* Expand the body.  */
2401   expand_stmt (DECL_SAVED_TREE (fn));
2402 
2403   /* Statements should always be full-expressions at the outermost set
2404      of curly braces for a function.  */
2405   my_friendly_assert (stmts_are_full_exprs_p (), 19990831);
2406 
2407   /* The outermost statement for a function contains the line number
2408      recorded when we finished processing the function.  */
2409   lineno = STMT_LINENO (DECL_SAVED_TREE (fn));
2410 
2411   /* Generate code for the function.  */
2412   genrtl_finish_function (fn);
2413 
2414   /* If possible, obliterate the body of the function so that it can
2415      be garbage collected.  */
2416   if (dump_enabled_p (TDI_all))
2417     /* Keep the body; we're going to dump it.  */
2418     ;
2419   else if (DECL_INLINE (fn) && flag_inline_trees)
2420     /* We might need the body of this function so that we can expand
2421        it inline somewhere else.  */
2422     ;
2423   else
2424     /* We don't need the body; blow it away.  */
2425     DECL_SAVED_TREE (fn) = NULL_TREE;
2426 
2427   /* And restore the current source position.  */
2428   current_function_decl = saved_function;
2429   lineno = saved_lineno;
2430   input_filename = saved_input_filename;
2431   extract_interface_info ();
2432 
2433   timevar_pop (TV_EXPAND);
2434 
2435   /* Emit any thunks that should be emitted at the same time as FN.  */
2436   emit_associated_thunks (fn);
2437 }
2438 
2439 /* Helper function for walk_tree, used by finish_function to override all
2440    the RETURN_STMTs and pertinent CLEANUP_STMTs for the named return
2441    value optimization.  */
2442 
2443 tree
nullify_returns_r(tp,walk_subtrees,data)2444 nullify_returns_r (tp, walk_subtrees, data)
2445      tree *tp;
2446      int *walk_subtrees;
2447      void *data;
2448 {
2449   tree nrv = (tree) data;
2450 
2451   /* No need to walk into types.  There wouldn't be any need to walk into
2452      non-statements, except that we have to consider STMT_EXPRs.  */
2453   if (TYPE_P (*tp))
2454     *walk_subtrees = 0;
2455   else if (TREE_CODE (*tp) == RETURN_STMT)
2456     RETURN_STMT_EXPR (*tp) = NULL_TREE;
2457   else if (TREE_CODE (*tp) == CLEANUP_STMT
2458 	   && CLEANUP_DECL (*tp) == nrv)
2459     CLEANUP_EH_ONLY (*tp) = 1;
2460   /* Replace the DECL_STMT for the NRV with an initialization of the
2461      RESULT_DECL, if needed.  */
2462   else if (TREE_CODE (*tp) == DECL_STMT
2463 	   && DECL_STMT_DECL (*tp) == nrv)
2464     {
2465       tree init;
2466       if (DECL_INITIAL (nrv)
2467 	  && DECL_INITIAL (nrv) != error_mark_node)
2468 	{
2469 	  init = build (INIT_EXPR, void_type_node,
2470 			DECL_RESULT (current_function_decl),
2471 			DECL_INITIAL (nrv));
2472 	  DECL_INITIAL (nrv) = error_mark_node;
2473 	}
2474       else
2475 	init = NULL_TREE;
2476       init = build_stmt (EXPR_STMT, init);
2477       TREE_CHAIN (init) = TREE_CHAIN (*tp);
2478       STMT_LINENO (init) = STMT_LINENO (*tp);
2479       *tp = init;
2480     }
2481 
2482   /* Keep iterating.  */
2483   return NULL_TREE;
2484 }
2485 
2486 /* Start generating the RTL for FN.  */
2487 
2488 static void
genrtl_start_function(fn)2489 genrtl_start_function (fn)
2490      tree fn;
2491 {
2492   /* Tell everybody what function we're processing.  */
2493   current_function_decl = fn;
2494   /* Get the RTL machinery going for this function.  */
2495   init_function_start (fn, DECL_SOURCE_FILE (fn), DECL_SOURCE_LINE (fn));
2496   /* Let everybody know that we're expanding this function, not doing
2497      semantic analysis.  */
2498   expanding_p = 1;
2499 
2500   /* Even though we're inside a function body, we still don't want to
2501      call expand_expr to calculate the size of a variable-sized array.
2502      We haven't necessarily assigned RTL to all variables yet, so it's
2503      not safe to try to expand expressions involving them.  */
2504   immediate_size_expand = 0;
2505   cfun->x_dont_save_pending_sizes_p = 1;
2506 
2507   /* Let the user know we're compiling this function.  */
2508   announce_function (fn);
2509 
2510   /* Initialize the per-function data.  */
2511   my_friendly_assert (!DECL_PENDING_INLINE_P (fn), 20000911);
2512   if (DECL_SAVED_FUNCTION_DATA (fn))
2513     {
2514       /* If we already parsed this function, and we're just expanding it
2515 	 now, restore saved state.  */
2516       *cp_function_chain = *DECL_SAVED_FUNCTION_DATA (fn);
2517 
2518       /* This function is being processed in whole-function mode; we
2519 	 already did semantic analysis.  */
2520       cfun->x_whole_function_mode_p = 1;
2521 
2522       /* If we decided that we didn't want to inline this function,
2523 	 make sure the back-end knows that.  */
2524       if (!current_function_cannot_inline)
2525 	current_function_cannot_inline = cp_function_chain->cannot_inline;
2526 
2527       /* We don't need the saved data anymore.  Unless this is an inline
2528          function; we need the named return value info for
2529          cp_copy_res_decl_for_inlining.  */
2530       if (! DECL_INLINE (fn))
2531 	DECL_SAVED_FUNCTION_DATA (fn) = NULL;
2532     }
2533 
2534   /* Keep track of how many functions we're presently expanding.  */
2535   ++function_depth;
2536 
2537   /* Create a binding level for the parameters.  */
2538   expand_function_start (fn, /*parms_have_cleanups=*/0);
2539   /* If this function is `main'.  */
2540   if (DECL_MAIN_P (fn))
2541     expand_main_function ();
2542 
2543   /* Give our named return value the same RTL as our RESULT_DECL.  */
2544   if (current_function_return_value)
2545     COPY_DECL_RTL (DECL_RESULT (fn), current_function_return_value);
2546 }
2547 
2548 /* Finish generating the RTL for FN.  */
2549 
2550 static void
genrtl_finish_function(fn)2551 genrtl_finish_function (fn)
2552      tree fn;
2553 {
2554   tree t;
2555 
2556 #if 0
2557   if (write_symbols != NO_DEBUG)
2558     {
2559       /* Keep this code around in case we later want to control debug info
2560 	 based on whether a type is "used".  (jason 1999-11-11) */
2561 
2562       tree ttype = target_type (fntype);
2563       tree parmdecl;
2564 
2565       if (IS_AGGR_TYPE (ttype))
2566 	/* Let debugger know it should output info for this type.  */
2567 	note_debug_info_needed (ttype);
2568 
2569       for (parmdecl = DECL_ARGUMENTS (fndecl); parmdecl; parmdecl = TREE_CHAIN (parmdecl))
2570 	{
2571 	  ttype = target_type (TREE_TYPE (parmdecl));
2572 	  if (IS_AGGR_TYPE (ttype))
2573 	    /* Let debugger know it should output info for this type.  */
2574 	    note_debug_info_needed (ttype);
2575 	}
2576     }
2577 #endif
2578 
2579   /* Clean house because we will need to reorder insns here.  */
2580   do_pending_stack_adjust ();
2581 
2582   /* If we have a named return value, we need to force a return so that
2583      the return register is USEd.  */
2584   if (DECL_NAME (DECL_RESULT (fn)))
2585     emit_jump (return_label);
2586 
2587   /* We hard-wired immediate_size_expand to zero in start_function.
2588      Expand_function_end will decrement this variable.  So, we set the
2589      variable to one here, so that after the decrement it will remain
2590      zero.  */
2591   immediate_size_expand = 1;
2592 
2593   /* Generate rtl for function exit.  */
2594   expand_function_end (input_filename, lineno, 0);
2595 
2596   /* If this is a nested function (like a template instantiation that
2597      we're compiling in the midst of compiling something else), push a
2598      new GC context.  That will keep local variables on the stack from
2599      being collected while we're doing the compilation of this
2600      function.  */
2601   if (function_depth > 1)
2602     ggc_push_context ();
2603 
2604   /* There's no need to defer outputting this function any more; we
2605      know we want to output it.  */
2606   DECL_DEFER_OUTPUT (fn) = 0;
2607 
2608   /* Run the optimizers and output the assembler code for this
2609      function.  */
2610   rest_of_compilation (fn);
2611 
2612   /* Undo the call to ggc_push_context above.  */
2613   if (function_depth > 1)
2614     ggc_pop_context ();
2615 
2616 #if 0
2617   /* Keep this code around in case we later want to control debug info
2618      based on whether a type is "used".  (jason 1999-11-11) */
2619 
2620   if (ctype && TREE_ASM_WRITTEN (fn))
2621     note_debug_info_needed (ctype);
2622 #endif
2623 
2624   /* If this function is marked with the constructor attribute, add it
2625      to the list of functions to be called along with constructors
2626      from static duration objects.  */
2627   if (DECL_STATIC_CONSTRUCTOR (fn))
2628     static_ctors = tree_cons (NULL_TREE, fn, static_ctors);
2629 
2630   /* If this function is marked with the destructor attribute, add it
2631      to the list of functions to be called along with destructors from
2632      static duration objects.  */
2633   if (DECL_STATIC_DESTRUCTOR (fn))
2634     static_dtors = tree_cons (NULL_TREE, fn, static_dtors);
2635 
2636   --function_depth;
2637 
2638   /* In C++, we should never be saving RTL for the function.  */
2639   my_friendly_assert (!DECL_SAVED_INSNS (fn), 20010903);
2640 
2641   /* Since we don't need the RTL for this function anymore, stop
2642      pointing to it.  That's especially important for LABEL_DECLs,
2643      since you can reach all the instructions in the function from the
2644      CODE_LABEL stored in the DECL_RTL for the LABEL_DECL.  Walk the
2645      BLOCK-tree, clearing DECL_RTL for LABEL_DECLs and non-static
2646      local variables.  */
2647   walk_tree_without_duplicates (&DECL_SAVED_TREE (fn),
2648 				clear_decl_rtl,
2649 				NULL);
2650 
2651   /* Clear out the RTL for the arguments.  */
2652   for (t = DECL_ARGUMENTS (fn); t; t = TREE_CHAIN (t))
2653     {
2654       SET_DECL_RTL (t, NULL_RTX);
2655       DECL_INCOMING_RTL (t) = NULL_RTX;
2656     }
2657 
2658   if (!(flag_inline_trees && DECL_INLINE (fn)))
2659     /* DECL_INITIAL must remain nonzero so we know this was an
2660        actual function definition.  */
2661     DECL_INITIAL (fn) = error_mark_node;
2662 
2663   /* Let the error reporting routines know that we're outside a
2664      function.  For a nested function, this value is used in
2665      pop_cp_function_context and then reset via pop_function_context.  */
2666   current_function_decl = NULL_TREE;
2667 }
2668 
2669 /* Clear out the DECL_RTL for the non-static variables in BLOCK and
2670    its sub-blocks.  */
2671 
2672 static tree
clear_decl_rtl(tp,walk_subtrees,data)2673 clear_decl_rtl (tp, walk_subtrees, data)
2674      tree *tp;
2675      int *walk_subtrees ATTRIBUTE_UNUSED;
2676      void *data ATTRIBUTE_UNUSED;
2677 {
2678   if (nonstatic_local_decl_p (*tp))
2679     SET_DECL_RTL (*tp, NULL_RTX);
2680 
2681   return NULL_TREE;
2682 }
2683 
2684 /* Perform initialization related to this module.  */
2685 
2686 void
init_cp_semantics()2687 init_cp_semantics ()
2688 {
2689   lang_expand_stmt = cp_expand_stmt;
2690 }
2691