[RFC] unifying gfc_symbol and gfc_component

Janus Weil janus@gcc.gnu.org
Mon Nov 14 11:43:00 GMT 2016


Hi all,

here is an idea I first had quite some time ago and which recently
struck me again ...

In general symbols and components in gfortran are represented by the
types gfc_symbol and gfc_component, respectively. Conceptually symbols
and components have a lot in common, as one can also see in the
corresponding type defintions in gfortran.h. Both of them have a name,
a type-spec, attributes, an array-spec, etc.

In the past I have come across a couple of situations, where I thought
it would be nice to have a way to operate on symbols and components in
the same way. Just to mention two concrete examples (I'm sure one can
find more):
1) gfc_build_class_symbol
2) resolve_procedure_interface vs. resolve_component (which have quite
a bit of code duplication)

Now that C++ is officially the implementation language of GCC (and
thus gfortran), it is actually very simple to implement the above
idea: One can just introduce a common base type (I'll call it
gfc_entity for now, but that name is debatable of course), and make
gfc_symbol and gfc_component inherit from that common base type.

Attached is a patch which does that with just very few changes and
compiles and regtests cleanly. This is just a first draft. One could
certainly share more properties if one accepts slight naming changes
(e.g. locus etc). Also the patch does not make any use of the new
possibilities yet.

Before I continue with that, I'd like to know what others think about
this idea. Do you think this is useful, or could it cause any harm?

Cheers,
Janus
-------------- next part --------------
Index: gcc/fortran/gfortran.h
===================================================================
--- gcc/fortran/gfortran.h	(Revision 242381)
+++ gcc/fortran/gfortran.h	(Arbeitskopie)
@@ -1033,16 +1033,24 @@ gfc_array_spec;
 #define gfc_get_array_spec() XCNEW (gfc_array_spec)
 
 
-/* Components of derived types.  */
-typedef struct gfc_component
+/* Some entity with a name, typespec, attributes etc.
+ * Base type for gfc_component and gfc_symbol. */
+typedef struct gfc_entity
 {
-  const char *name;
-  gfc_typespec ts;
+  const char *name;       /* Primary name, before renaming */
+  gfc_typespec ts;        /* Type specification */
 
-  symbol_attribute attr;
-  gfc_array_spec *as;
+  symbol_attribute attr;  /* Attributes */
+  gfc_array_spec *as;     /* Array specification */
 
   tree backend_decl;
+}
+gfc_entity;
+
+
+/* Components of derived types.  */
+typedef struct gfc_component : gfc_entity
+{
   /* Used to cache a FIELD_DECL matching this same component
      but applied to a different backend containing type that was
      generated by gfc_nonrestricted_type.  */
@@ -1481,15 +1489,11 @@ gfc_typebound_proc;
    symtree structures that is balanced by the red-black method-- more
    than one symtree node can point to any given symbol.  */
 
-typedef struct gfc_symbol
+typedef struct gfc_symbol : gfc_entity
 {
-  const char *name;	/* Primary name, before renaming */
   const char *module;	/* Module this symbol came from */
   locus declared_at;
 
-  gfc_typespec ts;
-  symbol_attribute attr;
-
   /* The formal member points to the formal argument list if the
      symbol is a function or subroutine name.  If the symbol is a
      generic name, the generic member points to the list of
@@ -1503,7 +1507,6 @@ gfc_typebound_proc;
   struct gfc_namespace *f2k_derived;
 
   struct gfc_expr *value;	/* Parameter/Initializer value */
-  gfc_array_spec *as;
   struct gfc_symbol *result;	/* function result symbol */
   gfc_component *components;	/* Derived type components */
 
@@ -1554,8 +1557,6 @@ gfc_typebound_proc;
   int refs;
   struct gfc_namespace *ns;	/* namespace containing this symbol */
 
-  tree backend_decl;
-
   /* Identity of the intrinsic module the symbol comes from, or
      INTMOD_NONE if it's not imported from a intrinsic module.  */
   intmod_id from_intmod;


More information about the Fortran mailing list