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]

C++ PATCH for dumps/docs on global initializers



This patch shouldn't change the behavior of the compiler at all, but
does make it possible for a source-browser or other back-end to figure
out which functions are global constructors and destructors.

--
Mark Mitchell                   mark@codesourcery.com
CodeSourcery, LLC               http://www.codesourcery.com

1999-09-30  Mark Mitchell  <mark@codesourcery.com>

	* cp-tree.h (lang_decl_flags): Add global_ctor_p and
	global_dtor_p.  Add init_priority.
	(DECL_ACCESS): Adjust accordingly.
	(DECL_GLOBAL_CTOR_P, DECL_GLOBAL_DTOR_P): New macros.
	(GLOBAL_INIT_PRIORITY): Likewise.
	* decl.c (lang_mark_tree): Adjust accordingly.
	(start_objects): Set DECL_GLOBAL_CTOR_P, DECL_GLOBAL_DTOR_P, 
	and GLOBAL_INIT_PRIORITY.
	* dump.c (dequeue_and_dump): Print them.
	* ir.texi: Document them.

Index: cp-tree.h
===================================================================
RCS file: /egcs/carton/cvsfiles/egcs/gcc/cp/cp-tree.h,v
retrieving revision 1.325
diff -c -p -r1.325 cp-tree.h
*** cp-tree.h	1999/09/30 06:15:52	1.325
--- cp-tree.h	1999/09/30 17:40:21
*************** struct lang_decl_flags
*** 1585,1593 ****
    unsigned bitfield : 1;
    unsigned defined_in_class : 1;
    unsigned pending_inline_p : 1;
!   unsigned dummy : 5;
  
-   tree access;
    tree context;
  
    union {
--- 1585,1594 ----
    unsigned bitfield : 1;
    unsigned defined_in_class : 1;
    unsigned pending_inline_p : 1;
!   unsigned global_ctor_p : 1;
!   unsigned global_dtor_p : 1;
!   unsigned dummy : 3;
  
    tree context;
  
    union {
*************** struct lang_decl_flags
*** 1597,1602 ****
--- 1598,1612 ----
      /* In a NAMESPACE_DECL, this is NAMESPACE_LEVEL.  */
      struct binding_level *level;
    } u;
+ 
+   union {
+     /* This is DECL_ACCESS.  */
+     tree access;
+ 
+     /* In a namespace-scope FUNCTION_DECL, this is
+        GLOBAL_INIT_PRIORITY.  */
+     int init_priority;
+   } u2;
  };
  
  struct lang_decl
*************** extern int flag_new_for_scope;
*** 2347,2353 ****
     For example, if a member that would normally be public in a
     derived class is made protected, then the derived class and the
     protected_access_node will appear in the DECL_ACCESS for the node.  */
! #define DECL_ACCESS(NODE) (DECL_LANG_SPECIFIC(NODE)->decl_flags.access)
  
  /* Accessor macros for C++ template decl nodes.  */
  
--- 2357,2378 ----
     For example, if a member that would normally be public in a
     derived class is made protected, then the derived class and the
     protected_access_node will appear in the DECL_ACCESS for the node.  */
! #define DECL_ACCESS(NODE) (DECL_LANG_SPECIFIC(NODE)->decl_flags.u2.access)
! 
! /* Nonzero if the FUNCTION_DECL is a global constructor.  */
! #define DECL_GLOBAL_CTOR_P(NODE) \
!   (DECL_LANG_SPECIFIC ((NODE))->decl_flags.global_ctor_p)
! 
! /* Nonzero if the FUNCTION_DECL is a global destructor.  */
! #define DECL_GLOBAL_DTOR_P(NODE) \
!   (DECL_LANG_SPECIFIC ((NODE))->decl_flags.global_dtor_p)
! 
! /* If DECL_GLOBAL_CTOR_P or DECL_GLOBAL_DTOR_P holds, this macro
!    returns the initialization priority for the function.  Constructors
!    with lower numbers should be run first.  Destructors should be run
!    in the reverse order of constructors.  */
! #define GLOBAL_INIT_PRIORITY(NODE) \
!   (DECL_LANG_SPECIFIC ((NODE))->decl_flags.u2.init_priority)
  
  /* Accessor macros for C++ template decl nodes.  */
  
Index: decl.c
===================================================================
RCS file: /egcs/carton/cvsfiles/egcs/gcc/cp/decl.c,v
retrieving revision 1.474
diff -c -p -r1.474 decl.c
*** decl.c	1999/09/30 00:50:28	1.474
--- decl.c	1999/09/30 17:40:31
*************** lang_mark_tree (t)
*** 14293,14299 ****
        if (ld)
  	{
  	  ggc_mark (ld);
! 	  ggc_mark_tree (ld->decl_flags.access);
  	  ggc_mark_tree (ld->decl_flags.context);
  	  if (TREE_CODE (t) != NAMESPACE_DECL)
  	    ggc_mark_tree (ld->decl_flags.u.template_info);
--- 14293,14300 ----
        if (ld)
  	{
  	  ggc_mark (ld);
! 	  if (!DECL_GLOBAL_CTOR_P (t) && !DECL_GLOBAL_DTOR_P (t))
! 	    ggc_mark_tree (ld->decl_flags.u2.access);
  	  ggc_mark_tree (ld->decl_flags.context);
  	  if (TREE_CODE (t) != NAMESPACE_DECL)
  	    ggc_mark_tree (ld->decl_flags.u.template_info);
Index: decl2.c
===================================================================
RCS file: /egcs/carton/cvsfiles/egcs/gcc/cp/decl2.c,v
retrieving revision 1.271
diff -c -p -r1.271 decl2.c
*** decl2.c	1999/09/30 15:48:19	1.271
--- decl2.c	1999/09/30 17:40:34
*************** start_objects (method_type, initp)
*** 2811,2816 ****
--- 2811,2823 ----
    /* Mark this declaration as used to avoid spurious warnings.  */
    TREE_USED (current_function_decl) = 1;
  
+   /* Mark this function as a global constructor or destructor.  */
+   if (method_type == 'I')
+     DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
+   else
+     DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
+   GLOBAL_INIT_PRIORITY (current_function_decl) = initp;
+ 
    body = begin_compound_stmt (/*has_no_scope=*/0);
  
    /* We cannot allow these functions to be elided, even if they do not
Index: dump.c
===================================================================
RCS file: /egcs/carton/cvsfiles/egcs/gcc/cp/dump.c,v
retrieving revision 1.16
diff -c -p -r1.16 dump.c
*** dump.c	1999/09/29 20:07:56	1.16
--- dump.c	1999/09/30 17:40:34
*************** dequeue_and_dump (di)
*** 625,630 ****
--- 625,639 ----
  	    dump_string (di, "operator");
  	  if (DECL_CONV_FN_P (t))
  	    dump_string (di, "conversion");
+ 	  if (DECL_GLOBAL_CTOR_P (t) || DECL_GLOBAL_DTOR_P (t))
+ 	    {
+ 	      if (DECL_GLOBAL_CTOR_P (t))
+ 		dump_string (di, "global init");
+ 	      if (DECL_GLOBAL_DTOR_P (t))
+ 		dump_string (di, "global fini");
+ 	      dump_int (di, "prio", GLOBAL_INIT_PRIORITY (t));
+ 	    }
+ 
  	  if (dump_children_p)
  	    dump_child ("body", DECL_SAVED_TREE (t));
  	}
Index: ir.texi
===================================================================
RCS file: /egcs/carton/cvsfiles/egcs/gcc/cp/ir.texi,v
retrieving revision 1.11
diff -c -p -r1.11 ir.texi
*** ir.texi	1999/09/29 20:07:56	1.11
--- ir.texi	1999/09/30 17:40:36
*************** the @code{DECL_REAL_CONTEXT} for @code{f
*** 948,953 ****
--- 948,956 ----
  @findex DECL_OVERLOADED_OPERATOR_P
  @findex DECL_CONV_FN_P
  @findex DECL_ARTIFIICIAL
+ @findex DECL_GLOBAL_CTOR_P
+ @findex DECL_GLOBAL_DTOR_P
+ @findex GLOBAL_INIT_PRIORITY
  
  The following macros and functions can be used on a @code{FUNCTION_DECL}:
  @ftable @code
*************** This macro holds if the function is an o
*** 1005,1010 ****
--- 1008,1029 ----
  
  @item DECL_CONV_FN_P
  This macro holds if the function is a type-conversion operator.
+ 
+ @item DECL_GLOBAL_CTOR_P
+ This predicate holds if the function is a file-scope initialization
+ function.
+ 
+ @item DECL_GLOBAL_DTOR_P
+ This predicate holds if the function is a file-scope finalization
+ function.
+ 
+ @item GLOBAL_INIT_PRIORITY
+ If either @code{DECL_GLOBAL_CTOR_P} or @code{DECL_GLOBAL_DTOR_P} holds,
+ then this gives the initialization priority for the function.  The
+ linker will arrange that all functions for which
+ @code{DECL_GLOBAL_CTOR_P} holds are run in increasing order of priority
+ before @code{main} is called.  When the program exits, all functions for
+ which @code{DECL_GLOBAL_DTOR_P} holds are run in the reverse order.
  
  @item DECL_ARTIFICIAL
  This macro holds if the function was implicitly generated by the


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