This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix PR c++/3882


Hello,

This is a (minor) regression from 2.95.3, present on the 3.1 issues list.
GCC doesn't support self-referencing variable initialization inside a
template function.

No (new) regression on i586-pc-linux-gnu.


2002-03-17  Eric Botcazou  <ebotcazou@multimania.com>

*cp/pt.c (tsubst_expr): Record variable declaration earlier.


--- gcc/cp/pt.orig.c Sat Mar 16 21:41:06 2002
+++ gcc/cp/pt.c Sat Mar 16 21:38:21 2002
@@ -7353,25 +7318,31 @@
    {
      init = DECL_INITIAL (decl);
      decl = tsubst (decl, args, complain, in_decl);
-     if (DECL_PRETTY_FUNCTION_P (decl))
-       init = DECL_INITIAL (decl);
-     else
-       init = tsubst_expr (init, args, complain, in_decl);
      if (decl != error_mark_node)
        {
                 if (TREE_CODE (decl) != TYPE_DECL)
                   /* Make sure the type is instantiated now. */
                   complete_type (TREE_TYPE (decl));
-         if (init)
-           DECL_INITIAL (decl) = error_mark_node;
          /* By marking the declaration as instantiated, we avoid
             trying to instantiate it.  Since instantiate_decl can't
             handle local variables, and since we've already done
             all that needs to be done, that's the right thing to
             do.  */
-         if (TREE_CODE (decl) == VAR_DECL)
+         if (TREE_CODE (decl) == VAR_DECL) {
            DECL_TEMPLATE_INSTANTIATED (decl) = 1;
-         maybe_push_decl (decl);
+           /* Variable initialization may be self-referencing,
+              so we record the decl early.  */
+           maybe_push_decl (decl);
+         }
+         if (DECL_PRETTY_FUNCTION_P (decl))
+           init = DECL_INITIAL (decl);
+         else
+           init = tsubst_expr (init, args, complain, in_decl);
+         if (init)
+           DECL_INITIAL (decl) = error_mark_node;
+         /* Record the decl here unless already done. */
+         if (TREE_CODE (decl) != VAR_DECL)
+           maybe_push_decl (decl);
          cp_finish_decl (decl, init, NULL_TREE, 0);
        }
    }


/* PR c++/3882 */
/* { dg-do compile } */
/* { dg-options "" } */

/* Verify that variable initialization can be
   self-referencing inside a template function. */

int foo(int);

template <typename T>
void bar(const T&)
{
  int i = foo(i);
}

void quus()
{
   bar(0);
}

--
Eric Botcazou
ebotcazou@multimania.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]