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]

Re: patch: disallow initializing certain aggregates


First, I am unsure if I should extend this concept to initializers in general, not just the aggregate ones (caught at really_start_incremental_init). Right now I am only handling aggregate types because it's (a) simple (b) that's all that's needed at the moment.

I briefly talked with Zack about this. Since this is only to be used for aggregates, neither of us see a reason to extend the functionality to scalars.


Second, depending on my above comments, I'll have to come up with an equivalent patch to the C++ front end, because I just noticed it doesn't use really_start_incremental_init.

...and I've done just that. This brought about a few changes in the patch, because now the target hook can no longer call error() because the C++ front end has cp_parser_error(). So, I'm now just having the target hook return bool, and erroring out in the respective front end.


I included "target.h" from cp/parser.c as needed, but I did not update the dependencies in cp/Make-lang.in as I didn't find any other dependencies listed there:

cp/parser.o: cp/parser.c $(CXX_TREE_H) $(TM_H) diagnostic.h gt-cp-parser.h

OK?

2003-02-19 Aldy Hernandez <aldyh at redhat dot com>

* doc/tm.texi: Document TARGET_ALLOW_INITIALIZER.

* testsuite/gcc.dg/20030218-2.c: New.

* hooks.c (hook_bool_tree_true): New.

* hooks.h (hook_bool_tree_true): Protoize.

	* cp/parser.c (cp_parser_init_declarator): Call allow_initializer
	target hook.
	Include target.h.

	* config/rs6000/rs6000.c (rs6000_allow_initializer): New.
	(TARGET_ALLOW_INITIALIZER): Define.

* target.h (struct gcc_target): Add allow_initializer.

	* target-def.h (TARGET_ALLOW_INITIALIZER): New macro.
	(TARGET_INITIALIZER): Add TARGET_ALLOW_INITIALIZER.

	* c-typeck.c (really_start_incremental_init): Call
	allow_initializer target hook.

Index: doc/tm.texi
===================================================================
RCS file: /cvs/gcc/gcc/gcc/doc/tm.texi,v
retrieving revision 1.201
diff -c -p -r1.201 tm.texi
*** doc/tm.texi	19 Feb 2003 00:51:16 -0000	1.201
--- doc/tm.texi	19 Feb 2003 23:55:02 -0000
*************** copying a vector value of type @var{type
*** 1458,1463 ****
--- 1458,1469 ----
  type @var{type2}.  The default is that there are no such types.
  @end deftypefn

+ @deftypefn {Target Hook} bool TARGET_ALLOW_INITIALIZER (tree @var{type})
+ This target hook should return @code{false} if it is invalid to
+ initialize the given aggregate @code{type}. The default is to return
+ @code{true}.
+ @end deftypefn
+
@deftypefn {Target Hook} bool TARGET_MS_BITFIELD_LAYOUT_P (tree @var{record_type})
This target hook returns @code{true} if bit-fields in the given
@var{record_type} are to be laid out following the rules of Microsoft
Index: testsuite/gcc.dg/20030218-2.c
===================================================================
RCS file: testsuite/gcc.dg/20030218-2.c
diff -N testsuite/gcc.dg/20030218-2.c
*** /dev/null 1 Jan 1970 00:00:00 -0000
--- testsuite/gcc.dg/20030218-2.c 19 Feb 2003 23:55:02 -0000
***************
*** 0 ****
--- 1,8 ----
+ /* { dg-do compile { target powerpc-*-eabi* } } */
+ /* { dg-options "-mcpu=8540" } */
+
+ /* Test that __ev64_opaque__ types cannot be initialized. */
+
+ typedef int __ev64_opaque__ __attribute__((mode(V2SI)));
+
+ __ev64_opaque__ george = {1, 2}; /* { dg-error "type cannot be initialized" } */
Index: hooks.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/hooks.c,v
retrieving revision 1.12
diff -c -p -r1.12 hooks.c
*** hooks.c 3 Feb 2003 19:40:57 -0000 1.12
--- hooks.c 19 Feb 2003 23:55:02 -0000
*************** hook_bool_tree_false (a)
*** 125,130 ****
--- 125,137 ----
}


  bool
+ hook_bool_tree_true (a)
+      tree a ATTRIBUTE_UNUSED;
+ {
+   return true;
+ }
+
+ bool
  hook_bool_tree_tree_false (a, b)
       tree a ATTRIBUTE_UNUSED;
       tree b ATTRIBUTE_UNUSED;
Index: hooks.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/hooks.h,v
retrieving revision 1.12
diff -c -p -r1.12 hooks.h
*** hooks.h	3 Feb 2003 19:40:57 -0000	1.12
--- hooks.h	19 Feb 2003 23:55:02 -0000
*************** Foundation, 59 Temple Place - Suite 330,
*** 24,29 ****
--- 24,30 ----

bool hook_bool_void_false PARAMS ((void));
bool hook_bool_tree_false PARAMS ((tree));
+ bool hook_bool_tree_true PARAMS ((tree));
bool hook_bool_tree_hwi_hwi_tree_false
PARAMS ((tree, HOST_WIDE_INT, HOST_WIDE_INT, tree));
bool hook_bool_tree_hwi_hwi_tree_true
Index: target.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/target.h,v
retrieving revision 1.51
diff -c -p -r1.51 target.h
*** target.h 19 Feb 2003 00:51:16 -0000 1.51
--- target.h 19 Feb 2003 23:55:02 -0000
*************** struct gcc_target
*** 323,328 ****
--- 323,331 ----
/* True if two vector types can be copied without an explicit cast. */
bool (* vector_types_compatible) PARAMS ((tree, tree));


+   /* True if a type can be initialized.  */
+   bool (* allow_initializer) PARAMS ((tree));
+
    /* Compute a (partial) cost for rtx X.  Return true if the complete
       cost has been computed, and false if subexpressions should be
       scanned.  In either case, *TOTAL contains the cost result.  */
Index: target-def.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/target-def.h,v
retrieving revision 1.45
diff -c -p -r1.45 target-def.h
*** target-def.h	19 Feb 2003 00:51:16 -0000	1.45
--- target-def.h	19 Feb 2003 23:55:02 -0000
*************** Foundation, 59 Temple Place - Suite 330,
*** 260,265 ****
--- 260,269 ----
  #define TARGET_VECTOR_TYPES_COMPATIBLE hook_bool_tree_tree_false
  #endif

+ #ifndef TARGET_ALLOW_INITIALIZER
+ #define TARGET_ALLOW_INITIALIZER hook_bool_tree_true
+ #endif
+
  /* In hook.c.  */
  #define TARGET_CANNOT_MODIFY_JUMPS_P hook_bool_void_false
  #define TARGET_CANNOT_FORCE_CONST_MEM hook_bool_rtx_false
*************** Foundation, 59 Temple Place - Suite 330,
*** 308,313 ****
--- 312,318 ----
    TARGET_STRIP_NAME_ENCODING,			\
    TARGET_VALID_POINTER_MODE,                    \
    TARGET_VECTOR_TYPES_COMPATIBLE,		\
+   TARGET_ALLOW_INITIALIZER,                     \
    TARGET_RTX_COSTS,				\
    TARGET_ADDRESS_COST,				\
    TARGET_HAVE_NAMED_SECTIONS,			\
Index: c-typeck.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-typeck.c,v
retrieving revision 1.220
diff -c -p -r1.220 c-typeck.c
*** c-typeck.c	19 Feb 2003 00:51:16 -0000	1.220
--- c-typeck.c	19 Feb 2003 23:55:07 -0000
*************** really_start_incremental_init (type)
*** 5158,5163 ****
--- 5158,5166 ----
    if (type == 0)
      type = TREE_TYPE (constructor_decl);

+   if (!(*targetm.allow_initializer) (type))
+     error ("type cannot be initialized");
+
    p->type = constructor_type;
    p->fields = constructor_fields;
    p->index = constructor_index;
Index: cp/parser.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/parser.c,v
retrieving revision 1.43
diff -c -p -r1.43 parser.c
*** cp/parser.c	18 Feb 2003 18:16:19 -0000	1.43
--- cp/parser.c	19 Feb 2003 23:55:13 -0000
***************
*** 34,39 ****
--- 34,40 ----
  #include "diagnostic.h"
  #include "toplev.h"
  #include "output.h"
+ #include "target.h"


/* The lexer. */
*************** cp_parser_asm_definition (cp_parser* par
*** 9506,9512 ****
declarator asm-specification [opt] attributes [opt] initializer [opt]


The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
! Returns a reprsentation of the entity declared. If MEMBER_P is TRUE,
then this declarator appears in a class scope. The new DECL created
by this declarator is returned.


--- 9507,9513 ----
declarator asm-specification [opt] attributes [opt] initializer [opt]


The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
! Returns a representation of the entity declared. If MEMBER_P is TRUE,
then this declarator appears in a class scope. The new DECL created
by this declarator is returned.


*************** cp_parser_init_declarator (cp_parser* pa
*** 9705,9712 ****

    /* Parse the initializer.  */
    if (is_initialized)
!     initializer = cp_parser_initializer (parser,
! 					 &is_parenthesized_init);
    else
      {
        initializer = NULL_TREE;
--- 9706,9718 ----

    /* Parse the initializer.  */
    if (is_initialized)
!     {
!       if (!(*targetm.allow_initializer) (TREE_TYPE (decl)))
! 	cp_parser_error (parser, "type cannot be initialized");
!
!       initializer = cp_parser_initializer (parser,
! 					   &is_parenthesized_init);
!     }
    else
      {
        initializer = NULL_TREE;
Index: config/rs6000/rs6000.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/config/rs6000/rs6000.c,v
retrieving revision 1.424
diff -c -p -r1.424 rs6000.c
*** config/rs6000/rs6000.c	19 Feb 2003 00:51:16 -0000	1.424
--- config/rs6000/rs6000.c	19 Feb 2003 23:55:18 -0000
*************** static void altivec_frame_fixup PARAMS (
*** 270,275 ****
--- 270,276 ----
  static int easy_vector_constant PARAMS ((rtx));
  static int is_ev64_opaque_type PARAMS ((tree));
  static bool rs6000_spe_vector_types_compatible PARAMS ((tree, tree));
+ static bool rs6000_allow_initializer PARAMS ((tree));

/* Hash table stuff for keeping track of TOC entries. */

*************** static const char alt_reg_names[][8] =
*** 425,430 ****
--- 426,434 ----
#undef TARGET_VECTOR_TYPES_COMPATIBLE
#define TARGET_VECTOR_TYPES_COMPATIBLE rs6000_spe_vector_types_compatible


+ #undef TARGET_ALLOW_INITIALIZER
+ #define TARGET_ALLOW_INITIALIZER rs6000_allow_initializer
+
  struct gcc_target targetm = TARGET_INITIALIZER;
  
  /* Override command line options.  Mostly we process the processor
*************** rs6000_spe_vector_types_compatible (t1,
*** 13624,13629 ****
--- 13628,13649 ----
       allow inter conversion to and from V2SI modes.  We could use
       V1D1, and rewrite <spe.h> accordingly.  */
    return t1 == V2SI_type_node || t2 == V2SI_type_node;
+ }
+
+ /* Return false if we're trying to initialize an opaque type.  */
+ static bool
+ rs6000_allow_initializer (type)
+      tree type;
+ {
+   if (TARGET_SPE
+       && TYPE_NAME (type)
+       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
+       && DECL_NAME (TYPE_NAME (type))
+       && !strcmp (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))),
+ 		  "__ev64_opaque__"))
+     return false;
+
+   return true;
  }

#include "gt-rs6000.h"


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