OSDN Git Service

2004-08-17 Paul Brook <paul@codesourcery.com>
[pf3gnuchains/gcc-fork.git] / gcc / c-semantics.c
index 42cdf08..f011cb9 100644 (file)
 /* This file contains the definitions and documentation for the common
    tree codes used in the GNU C and C++ compilers (see c-common.def
-   for the standard codes).  
-   Copyright (C) 2000 Free Software Foundation, Inc.  Written by
-   Benjamin Chelf (chelf@codesourcery.com).
+   for the standard codes).
+   Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
+   Written by Benjamin Chelf (chelf@codesourcery.com).
 
-This file is part of GNU CC.
+This file is part of GCC.
 
-GNU CC is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2, or (at your option)
-any later version.
+GCC is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 2, or (at your option) any later
+version.
 
-GNU CC is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
 
 You should have received a copy of the GNU General Public License
-along with GNU CC; see the file COPYING.  If not, write to
-the Free Software Foundation, 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  */
+along with GCC; see the file COPYING.  If not, write to the Free
+Software Foundation, 59 Temple Place - Suite 330, Boston, MA
+02111-1307, USA.  */
 
 #include "config.h"
 #include "system.h"
+#include "coretypes.h"
+#include "tm.h"
 #include "tree.h"
 #include "function.h"
 #include "splay-tree.h"
 #include "varray.h"
 #include "c-common.h"
 #include "except.h"
+/* In order for the format checking to accept the C frontend
+   diagnostic framework extensions, you must define this token before
+   including toplev.h.  */
+#define GCC_DIAG_STYLE __gcc_cdiag__
 #include "toplev.h"
 #include "flags.h"
 #include "ggc.h"
 #include "rtl.h"
 #include "output.h"
 #include "timevar.h"
+#include "predict.h"
+#include "tree-inline.h"
+#include "tree-gimple.h"
+#include "langhooks.h"
 
-/* Some statements, like for-statements or if-statements, require a
-   condition.  This condition can be a declaration.  If T is such a
-   declaration it is processed, and an expression appropriate to use
-   as the condition is returned.  Otherwise, T itself is returned.  */
+/* Create an empty statement tree rooted at T.  */
 
 tree
-expand_cond (t)
-     tree t;
+push_stmt_list (void)
 {
-  if (t && TREE_CODE (t) == TREE_LIST)
-    {
-      expand_stmt (TREE_PURPOSE (t));
-      return TREE_VALUE (t);
-    }
-  else 
-    return t;
-}
-
-/* Create RTL for the local static variable DECL.  */
-
-void
-make_rtl_for_local_static (decl)
-     tree decl;
-{
-  const char *asmspec = NULL;
-
-  /* If we inlined this variable, we could see it's declaration
-     again.  */
-  if (DECL_RTL (decl))
-    return;
-
-  if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
-    {
-      /* The only way this situaton can occur is if the
-        user specified a name for this DECL using the
-        `attribute' syntax.  */
-      asmspec = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
-      DECL_ASSEMBLER_NAME (decl) = DECL_NAME (decl);
-    }
-
-  rest_of_decl_compilation (decl, asmspec, /*top_level=*/0, /*at_end=*/0);
-}
-
-/* Let the back-end know about DECL.  */
-
-void
-emit_local_var (decl)
-     tree decl;
-{
-  /* Create RTL for this variable.  */
-  if (!DECL_RTL (decl))
-    {
-      if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
-       /* The user must have specified an assembler name for this
-          variable.  Set that up now.  */
-       rest_of_decl_compilation
-         (decl, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
-          /*top_level=*/0, /*at_end=*/0);
-      else
-       expand_decl (decl);
-    }
-
-  /* Actually do the initialization.  */
-  if (stmts_are_full_exprs_p ())
-    expand_start_target_temps ();
-
-  expand_decl_init (decl);
-
-  if (stmts_are_full_exprs_p ())
-    expand_end_target_temps ();
-}
-
-/* Helper for generating the RTL at the beginning of a scope. */
-
-void
-genrtl_do_pushlevel ()
-{
-  emit_line_note (input_filename, lineno);
-  clear_last_expr ();
-}
-
-/* Helper for generating the RTL. */
-
-void
-genrtl_clear_out_block ()
-{
-  /* If COND wasn't a declaration, clear out the
-     block we made for it and start a new one here so the
-     optimization in expand_end_loop will work.  */
-  if (getdecls () == NULL_TREE)
-    genrtl_do_pushlevel ();
+  tree t;
+  t = alloc_stmt_list ();
+  TREE_CHAIN (t) = cur_stmt_list;
+  cur_stmt_list = t;
+  return t;
 }
 
-/* Generate the RTL for DESTINATION, which is a GOTO_STMT. */
+/* Similarly, except that T may have already been pushed/popped, and
+   thus may already contain statement(s).  Arrage for new statements
+   to be appended.  */
 
-void
-genrtl_goto_stmt (destination)
-     tree destination;
+tree
+re_push_stmt_list (tree t)
 {
-  if (TREE_CODE (destination) == IDENTIFIER_NODE)
-    abort ();
-  
-  /* We warn about unused labels with -Wunused.  That means we have to
-     mark the used labels as used.  */
-  if (TREE_CODE (destination) == LABEL_DECL)
-    TREE_USED (destination) = 1;
-  
-  emit_line_note (input_filename, lineno);
-  
-  if (TREE_CODE (destination) == LABEL_DECL)
+  if (t)
     {
-      label_rtx (destination);
-      expand_goto (destination); 
+      if (TREE_CODE (t) != STATEMENT_LIST)
+       {
+         tree u = alloc_stmt_list ();
+         append_to_statement_list_force (t, &u);
+         t = u;
+       }
     }
   else
-    expand_computed_goto (destination);
+    t = alloc_stmt_list ();
+  TREE_CHAIN (t) = cur_stmt_list;
+  cur_stmt_list = t;
+  return t;
 }
 
-/* Generate the RTL for EXPR, which is an EXPR_STMT. */
+/* Finish the statement tree rooted at T.  */
 
-void 
-genrtl_expr_stmt (expr)
-     tree expr;
+tree
+pop_stmt_list (tree t)
 {
-  if (expr != NULL_TREE)
-    {
-      emit_line_note (input_filename, lineno);
-      
-      if (stmts_are_full_exprs_p ())
-       expand_start_target_temps ();
-      
-      lang_expand_expr_stmt (expr);
-      
-      if (stmts_are_full_exprs_p ())
-       expand_end_target_temps ();
-    }
-}
+  tree u = cur_stmt_list, chain;
 
-/* Generate the RTL for T, which is a DECL_STMT. */
-
-void
-genrtl_decl_stmt (t)
-     tree t;
-{
-  tree decl;
-  emit_line_note (input_filename, lineno);
-  decl = DECL_STMT_DECL (t);
-  /* If this is a declaration for an automatic local
-     variable, initialize it.  Note that we might also see a
-     declaration for a namespace-scope object (declared with
-     `extern').  We don't have to handle the initialization
-     of those objects here; they can only be declarations,
-     rather than definitions.  */
-  if (TREE_CODE (decl) == VAR_DECL 
-      && !TREE_STATIC (decl)
-      && !DECL_EXTERNAL (decl))
+  /* Pop statement lists until we reach the target level.  The extra
+     nestings will be due to outstanding cleanups.  */
+  while (1)
     {
-      /* Let the back-end know about this variable.  */
-      if (!anon_aggr_type_p (TREE_TYPE (decl)))
-       emit_local_var (decl);
-      else
-       expand_anon_union_decl (decl, NULL_TREE, 
-                               DECL_ANON_UNION_ELEMS (decl));
+      chain = TREE_CHAIN (u);
+      TREE_CHAIN (u) = NULL_TREE;
+      if (t == u)
+       break;
+      u = chain;
     }
-  else if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
+  cur_stmt_list = chain;
+
+  /* If the statement list is completely empty, just return it.  This is
+     just as good small as build_empty_stmt, with the advantage that 
+     statement lists are merged when they appended to one another.  So
+     using the STATEMENT_LIST avoids pathological buildup of EMPTY_STMT_P
+     statements.  */
+  if (TREE_SIDE_EFFECTS (t))
     {
-      if (DECL_ARTIFICIAL (decl) && ! TREE_USED (decl))
-       /* Do not emit unused decls. This is not just an
-          optimization. We really do not want to emit
-          __PRETTY_FUNCTION__ etc, if they're never used.  */
-       DECL_IGNORED_P (decl) = 1;
-      else
-       make_rtl_for_local_static (decl);
+      tree_stmt_iterator i = tsi_start (t);
+
+      /* If the statement list contained exactly one statement, then
+        extract it immediately.  */
+      if (tsi_one_before_end_p (i))
+       {
+         u = tsi_stmt (i);
+         tsi_delink (&i);
+         free_stmt_list (t);
+         t = u;
+       }
     }
-}
-
-/* Generate the RTL for T, which is an IF_STMT. */
 
-void
-genrtl_if_stmt (t)
-     tree t;
-{
-  tree cond;
-  genrtl_do_pushlevel ();
-  cond = expand_cond (IF_COND (t));
-  emit_line_note (input_filename, lineno);
-  expand_start_cond (cond, 0);
-  if (THEN_CLAUSE (t))
-    expand_stmt (THEN_CLAUSE (t));
-  if (ELSE_CLAUSE (t))
-    {
-      expand_start_else ();
-      expand_stmt (ELSE_CLAUSE (t));
-    }
-  expand_end_cond ();
+  return t;
 }
 
-/* Generate the RTL for T, which is a WHILE_STMT. */
+/* T is a statement.  Add it to the statement-tree.  */
 
-void
-genrtl_while_stmt (t)
-     tree t;
-{
-  tree cond;
-  emit_nop ();
-  emit_line_note (input_filename, lineno);
-  expand_start_loop (1); 
-  genrtl_do_pushlevel ();
-
-  cond = expand_cond (WHILE_COND (t));
-  emit_line_note (input_filename, lineno);
-  expand_exit_loop_if_false (0, cond);
-  genrtl_clear_out_block ();
-  
-  expand_stmt (WHILE_BODY (t));
-
-  expand_end_loop ();
-}
-
-/* Generate the RTL for T, which is a DO_STMT. */
-
-void
-genrtl_do_stmt (t)
-     tree t;
+tree
+add_stmt (tree t)
 {
-  tree cond;
-  emit_nop ();
-  emit_line_note (input_filename, lineno);
-  expand_start_loop_continue_elsewhere (1);
+  enum tree_code code = TREE_CODE (t);
 
-  expand_stmt (DO_BODY (t));
-
-  expand_loop_continue_here ();
-
-  cond = expand_cond (DO_COND (t));
-  emit_line_note (input_filename, lineno);
-  expand_exit_loop_if_false (0, cond);
-  expand_end_loop ();
-}
+  if ((EXPR_P (t) || STATEMENT_CODE_P (code)) && code != LABEL_EXPR)
+    {
+      if (!EXPR_HAS_LOCATION (t))
+       SET_EXPR_LOCATION (t, input_location);
 
-/* Generate the RTL for EXPR, which is a RETURN_STMT. */
+      /* When we expand a statement-tree, we must know whether or not the
+        statements are full-expressions.  We record that fact here.  */
+      STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
+    }
 
-void
-genrtl_return_stmt (expr)
-     tree expr;
-{
-  emit_line_note (input_filename, lineno);
-  c_expand_return (expr);
-}
+  if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
+    STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
 
-/* Generate the RTL for T, which is a FOR_STMT. */
+  /* Add T to the statement-tree.  Non-side-effect statements need to be
+     recorded during statement expressions.  */
+  append_to_statement_list_force (t, &cur_stmt_list);
 
-void
-genrtl_for_stmt (t)
-     tree t;
-{
-  tree tmp;
-  tree cond;
-  if (NEW_FOR_SCOPE_P (t))
-    genrtl_do_pushlevel ();
-
-  expand_stmt (FOR_INIT_STMT (t));
-
-  emit_nop ();
-  emit_line_note (input_filename, lineno);
-  expand_start_loop_continue_elsewhere (1); 
-  genrtl_do_pushlevel ();
-  cond = expand_cond (FOR_COND (t));
-  emit_line_note (input_filename, lineno);
-  if (cond)
-    expand_exit_loop_if_false (0, cond);
-  genrtl_clear_out_block ();
-  tmp = FOR_EXPR (t);
-
-  expand_stmt (FOR_BODY (t));
-
-  emit_line_note (input_filename, lineno);
-  expand_loop_continue_here ();
-  if (tmp) 
-    genrtl_expr_stmt (tmp);
-  expand_end_loop ();
+  return t;
 }
 
-/* Generate the RTL for a BREAK_STMT. */
+/* Build a generic statement based on the given type of node and
+   arguments. Similar to `build_nt', except that we set
+   EXPR_LOCATION to be the current source location.  */
+/* ??? This should be obsolete with the lineno_stmt productions
+   in the grammar.  */
 
-void
-genrtl_break_stmt ()
+tree
+build_stmt (enum tree_code code, ...)
 {
-  emit_line_note (input_filename, lineno);
-  if ( ! expand_exit_something ())
-    error ("break statement not within loop or switch");
-}
-
-/* Generate the RTL for a CONTINUE_STMT. */
+  tree ret;
+  int length, i;
+  va_list p;
+  bool side_effects;
 
-void
-genrtl_continue_stmt ()
-{
-  emit_line_note (input_filename, lineno);
-  if (! expand_continue_loop (0))
-    error ("continue statement not within a loop");   
-}
+  va_start (p, code);
 
-/* Generate the RTL for T, which is a SCOPE_STMT. */
+  ret = make_node (code);
+  TREE_TYPE (ret) = void_type_node;
+  length = TREE_CODE_LENGTH (code);
+  SET_EXPR_LOCATION (ret, input_location);
 
-void
-genrtl_scope_stmt (t)
-     tree t;
-{
-  if (!SCOPE_NO_CLEANUPS_P (t))
-    {
-      if (SCOPE_BEGIN_P (t))
-       expand_start_bindings_and_block (2 * SCOPE_NULLIFIED_P (t),
-                                        SCOPE_STMT_BLOCK (t));
-      else if (SCOPE_END_P (t))
-       expand_end_bindings (NULL_TREE, !SCOPE_NULLIFIED_P (t), 0);
-    }
-  else if (!SCOPE_NULLIFIED_P (t))
+  /* Most statements have implicit side effects all on their own, 
+     such as control transfer.  For those that do, we'll compute
+     the real value of TREE_SIDE_EFFECTS from its arguments.  */
+  switch (code)
     {
-      rtx note = emit_note (NULL,
-                           (SCOPE_BEGIN_P (t) 
-                            ? NOTE_INSN_BLOCK_BEG
-                            : NOTE_INSN_BLOCK_END));
-      NOTE_BLOCK (note) = SCOPE_STMT_BLOCK (t);
+    case EXPR_STMT:
+      side_effects = false;
+      break;
+    default:
+      side_effects = true;
+      break;
     }
-}
-
-/* Generate the RTL for T, which is a SWITCH_STMT. */
 
-void
-genrtl_switch_stmt (t)
-     tree t;
-{
-  tree cond;
-  genrtl_do_pushlevel ();
-  cond = expand_cond (SWITCH_COND (t));
-  if (cond != error_mark_node)
+  for (i = 0; i < length; i++)
     {
-      emit_line_note (input_filename, lineno);
-      c_expand_start_case (cond);
+      tree t = va_arg (p, tree);
+      if (t && IS_NON_TYPE_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (t))))
+        side_effects |= TREE_SIDE_EFFECTS (t);
+      TREE_OPERAND (ret, i) = t;
     }
-  else
-    /* The code is in error, but we don't want expand_end_case to
-       crash. */
-    c_expand_start_case (boolean_false_node);
 
-  expand_stmt (SWITCH_BODY (t));
+  TREE_SIDE_EFFECTS (ret) = side_effects;
 
-  expand_end_case (cond);
+  va_end (p);
+  return ret;
 }
 
-/* Generate the RTL for a CASE_LABEL. */
-
-void 
-genrtl_case_label (low_value, high_value)
-     tree low_value;
-     tree high_value;
-{
-  do_case (low_value, high_value);
-}
-
-/* Generate the RTL for the start of a COMPOUND_STMT. */
+/* Let the back-end know about DECL.  */
 
-tree 
-genrtl_begin_compound_stmt (has_no_scope)
-     int has_no_scope;
+void
+emit_local_var (tree decl)
 {
-  /* If this is the outermost block of the function, declare the
-     variables __FUNCTION__, __PRETTY_FUNCTION__, and so forth.  */
-  if (cfun
-      && !current_function_name_declared () 
-      && !has_no_scope)
+  /* Create RTL for this variable.  */
+  if (!DECL_RTL_SET_P (decl))
     {
-      set_current_function_name_declared (1);
-      declare_function_name ();
+      if (DECL_HARD_REGISTER (decl))
+       /* The user specified an assembler name for this variable.
+          Set that up now.  */
+       rest_of_decl_compilation (decl, 0, 0);
+      else
+       expand_decl (decl);
     }
-
-  return NULL_TREE;
 }
 
-/* Generate the RTL for the end of a COMPOUND_STMT. */
+/* Build a break statement node and return it.  */
 
-tree genrtl_finish_compound_stmt (has_no_scope)
-     int has_no_scope;
-{
-  return NULL_TREE;
-}
-
-/* Generate the RTL for T, which is a COMPOUND_STMT. */
-
-tree 
-genrtl_compound_stmt (t)
-    tree t;
+tree
+build_break_stmt (void)
 {
-  genrtl_begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t));
-  expand_stmt (COMPOUND_BODY (t));
-  return (genrtl_finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t)));
+  return (build_stmt (BREAK_STMT));
 }
 
-/* Generate the RTL for an ASM_STMT. */
+/* Build a continue statement node and return it.  */
 
-void
-genrtl_asm_stmt (cv_qualifier, string, output_operands,
-                input_operands, clobbers)
-     tree cv_qualifier;
-     tree string;
-     tree output_operands;
-     tree input_operands;
-     tree clobbers;
+tree
+build_continue_stmt (void)
 {
-  if (TREE_CHAIN (string))
-    string = combine_strings (string);
-
-  if (cv_qualifier != NULL_TREE
-      && cv_qualifier != ridpointers[(int) RID_VOLATILE])
-    {
-      warning ("%s qualifier ignored on asm",
-              IDENTIFIER_POINTER (cv_qualifier));
-      cv_qualifier = NULL_TREE;
-    }
-
-  emit_line_note (input_filename, lineno);
-  if (output_operands != NULL_TREE || input_operands != NULL_TREE
-      || clobbers != NULL_TREE)
-      c_expand_asm_operands (string, output_operands,
-                            input_operands, 
-                            clobbers,
-                            cv_qualifier != NULL_TREE,
-                            input_filename, lineno);
-  else
-    expand_asm (string);
+  return (build_stmt (CONTINUE_STMT));
 }
 
-/* Generate the RTL for a DECL_CLEANUP. */
-
-void 
-genrtl_decl_cleanup (decl, cleanup)
-     tree decl;
-     tree cleanup;
-{
-  if (!decl || (DECL_SIZE (decl) && TREE_TYPE (decl) != error_mark_node))
-    expand_decl_cleanup (decl, cleanup);
-}
+/* Create a CASE_LABEL_EXPR tree node and return it.  */
 
 tree
-expand_stmt (t)
-     tree t;
+build_case_label (tree low_value, tree high_value, tree label_decl)
 {
-  tree rval;
-  rval = lang_expand_stmt (t);
-  return rval;
+  return build_stmt (CASE_LABEL_EXPR, low_value, high_value, label_decl);
 }