public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
* [Patch, Fortran, OOP] PR 46313: OOP-ABI issue, ALLOCATE issue, CLASS renaming issue
@ 2010-11-06 20:11 Janus Weil
  2010-11-06 21:03 ` Thomas Koenig
  2010-11-07 16:52 ` Tobias Burnus
  0 siblings, 2 replies; 23+ messages in thread
From: Janus Weil @ 2010-11-06 20:11 UTC (permalink / raw)
  To: gfortran, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 1298 bytes --]

Hi all,

with the test cases in this PR Tobias demonstrated that our naming
scheme for class containers and vtables is insufficient. It currently
is based only on the type name. As shown in the PR, naming ambiguities
can be created, e.g. by setting up two derived types with identical
names in different modules, and use-renaming them in the main program.

The patch avoids these naming ambiguities by including the module name
in the naming scheme for class containers and vtabs. Example:

module mo
type :: dt
  ! ...
end type
class(dt), pointer :: cp
end module

Without the patch, the class container name is "class$dt", with the
patch it will be "class$mo$dt". This makes sure that we get one unique
class container and vtab for each derived type, even with renamed
derived types.

The patch was regtested on x86_64-unknown-linux-gnu. Ok for trunk?

Cheers,
Janus


2010-11-06  Janus Weil  <janus@gcc.gnu.org>

	PR fortran/46313
	* class.c (get_unique_type_string): New function.
	(gfc_build_class_symbol): Use 'get_unique_type_string' to construct
	uniques names for the class containers.
	(gfc_find_derived_vtab): Use 'get_unique_type_string' to construct
	uniques names for the vtab symbols.

2010-11-06  Janus Weil  <janus@gcc.gnu.org>

	PR fortran/46313
	* gfortran.dg/class_28.f03: New.

[-- Attachment #2: pr46313.diff --]
[-- Type: application/octet-stream, Size: 4820 bytes --]

Index: gcc/fortran/class.c
===================================================================
--- gcc/fortran/class.c	(revision 166404)
+++ gcc/fortran/class.c	(working copy)
@@ -36,11 +36,11 @@ along with GCC; see the file COPYING3.  If not see
     
    For each derived type we set up a "vtable" entry, i.e. a structure with the
    following fields:
-    * $hash: A hash value serving as a unique identifier for this type.
-    * $size: The size in bytes of the derived type.
-    * $extends: A pointer to the vtable entry of the parent derived type.
+    * $hash:     A hash value serving as a unique identifier for this type.
+    * $size:     The size in bytes of the derived type.
+    * $extends:  A pointer to the vtable entry of the parent derived type.
     * $def_init: A pointer to a default initialized variable of this type.
-    * $copy: A procedure pointer to a copying procedure.
+    * $copy:     A procedure pointer to a copying procedure.
    After these follow procedure pointer components for the specific
    type-bound procedures.  */
 
@@ -107,6 +107,20 @@ gfc_class_null_initializer (gfc_typespec *ts)
 }
 
 
+/* Create a unique string identifier for a derived type, composed of its name
+   and module name. This is used to construct unique names for the class
+   containers and vtab symbols.  */
+
+static void
+get_unique_type_string (char *string, gfc_symbol *derived)
+{  
+  if (derived->module)
+    sprintf (string, "%s$%s", derived->module, derived->name);
+  else
+    sprintf (string, "%s$%s", derived->ns->proc_name->name, derived->name);
+}
+
+
 /* Build a polymorphic CLASS entity, using the symbol that comes from
    build_sym. A CLASS entity is represented by an encapsulating type,
    which contains the declared type as '$data' component, plus a pointer
@@ -116,22 +130,23 @@ gfc_try
 gfc_build_class_symbol (gfc_typespec *ts, symbol_attribute *attr,
 			gfc_array_spec **as, bool delayed_vtab)
 {
-  char name[GFC_MAX_SYMBOL_LEN + 5];
+  char name[GFC_MAX_SYMBOL_LEN], tname[GFC_MAX_SYMBOL_LEN];
   gfc_symbol *fclass;
   gfc_symbol *vtab;
   gfc_component *c;
 
   /* Determine the name of the encapsulating type.  */
+  get_unique_type_string (tname, ts->u.derived);
   if ((*as) && (*as)->rank && attr->allocatable)
-    sprintf (name, "class$%s_%d_a", ts->u.derived->name, (*as)->rank);
+    sprintf (name, "class$%s_%d_a", tname, (*as)->rank);
   else if ((*as) && (*as)->rank)
-    sprintf (name, "class$%s_%d", ts->u.derived->name, (*as)->rank);
+    sprintf (name, "class$%s_%d", tname, (*as)->rank);
   else if (attr->pointer)
-    sprintf (name, "class$%s_p", ts->u.derived->name);
+    sprintf (name, "class$%s_p", tname);
   else if (attr->allocatable)
-    sprintf (name, "class$%s_a", ts->u.derived->name);
+    sprintf (name, "class$%s_a", tname);
   else
-    sprintf (name, "class$%s", ts->u.derived->name);
+    sprintf (name, "class$%s", tname);
 
   gfc_find_symbol (name, ts->u.derived->ns, 0, &fclass);
   if (fclass == NULL)
@@ -316,7 +331,6 @@ gfc_find_derived_vtab (gfc_symbol *derived)
   gfc_namespace *ns;
   gfc_symbol *vtab = NULL, *vtype = NULL, *found_sym = NULL, *def_init = NULL;
   gfc_symbol *copy = NULL, *src = NULL, *dst = NULL;
-  char name[2 * GFC_MAX_SYMBOL_LEN + 8];
   
   /* Find the top-level namespace (MODULE or PROGRAM).  */
   for (ns = gfc_current_ns; ns; ns = ns->parent)
@@ -329,7 +343,10 @@ gfc_find_derived_vtab (gfc_symbol *derived)
     
   if (ns)
     {
-      sprintf (name, "vtab$%s", derived->name);
+      char name[GFC_MAX_SYMBOL_LEN], tname[GFC_MAX_SYMBOL_LEN];
+      
+      get_unique_type_string (tname, derived);
+      sprintf (name, "vtab$%s", tname);
 
       /* Look for the vtab symbol in various namespaces.  */
       gfc_find_symbol (name, gfc_current_ns, 0, &vtab);
@@ -350,7 +367,7 @@ gfc_find_derived_vtab (gfc_symbol *derived)
 	  vtab->attr.vtab = 1;
 	  vtab->attr.access = ACCESS_PUBLIC;
 	  gfc_set_sym_referenced (vtab);
-	  sprintf (name, "vtype$%s", derived->name);
+	  sprintf (name, "vtype$%s", tname);
 	  
 	  gfc_find_symbol (name, ns, 0, &vtype);
 	  if (vtype == NULL)
@@ -431,7 +448,7 @@ gfc_find_derived_vtab (gfc_symbol *derived)
 	      else
 		{
 		  /* Construct default initialization variable.  */
-		  sprintf (name, "def_init$%s", derived->name);
+		  sprintf (name, "def_init$%s", tname);
 		  gfc_get_symbol (name, ns, &def_init);
 		  def_init->attr.target = 1;
 		  def_init->attr.save = SAVE_EXPLICIT;
@@ -462,7 +479,7 @@ gfc_find_derived_vtab (gfc_symbol *derived)
 		  ns->contained = sub_ns;
 		  sub_ns->resolved = 1;
 		  /* Set up procedure symbol.  */
-		  sprintf (name, "copy$%s", derived->name);
+		  sprintf (name, "copy$%s", tname);
 		  gfc_get_symbol (name, sub_ns, &copy);
 		  sub_ns->proc_name = copy;
 		  copy->attr.flavor = FL_PROCEDURE;

[-- Attachment #3: class_28.f03 --]
[-- Type: application/octet-stream, Size: 528 bytes --]

! { dg-do compile }
!
! PR 46313: [OOP] OOP-ABI issue, ALLOCATE issue, CLASS renaming issue
!
! Contributed by Tobias Burnus <burnus@gcc.gnu.org>

module m1
  type mytype
    real :: a(10) = 2
  end type
end module m1

module m2
  type mytype
    real :: b(10) = 8
  end type
end module m2

program p
use m1, t1 => mytype
use m2, t2 => mytype
implicit none

class(t1), allocatable :: x
class(t2), allocatable :: y

allocate (t1 :: x)
allocate (t2 :: y)

print *, x%a
print *, y%b
end

! { dg-final { cleanup-modules "m1 m2" } }

^ permalink raw reply	[flat|nested] 23+ messages in thread
* Re: [Patch, Fortran, OOP] PR 46313: OOP-ABI issue, ALLOCATE issue, CLASS renaming issue
@ 2010-11-06 23:34 Dominique Dhumieres
  2010-11-06 23:56 ` Janus Weil
  0 siblings, 1 reply; 23+ messages in thread
From: Dominique Dhumieres @ 2010-11-06 23:34 UTC (permalink / raw)
  To: fortran; +Cc: gcc-patches, janus, tkoenig

> If we are going to change the naming of the OOP stuff anyway, what about
> the possible name collisions with -fdollar-ok ?

With the patch for PR 46313, the test compiled with -fdollar-ok is
compiled without problem on x86_64-apple-darwin10.

Otherwise, the patch has not disturbed my pet bugs!-)

Thanks,

Dominique

^ permalink raw reply	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2018-09-20 19:36 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-11-06 20:11 [Patch, Fortran, OOP] PR 46313: OOP-ABI issue, ALLOCATE issue, CLASS renaming issue Janus Weil
2010-11-06 21:03 ` Thomas Koenig
2010-11-06 21:23   ` Janus Weil
2010-11-07 16:52 ` Tobias Burnus
2010-11-07 18:44   ` Janus Weil
2010-11-08 13:27     ` Tobias Burnus
2010-11-09 10:41       ` Janus Weil
2018-09-17  8:59         ` Bernhard Reutner-Fischer
2018-09-17 19:22           ` Janus Weil
2018-09-17 20:25             ` Janus Weil
2018-09-19 14:50               ` Bernhard Reutner-Fischer
2018-09-20 19:36                 ` Janus Weil
2010-11-06 23:34 Dominique Dhumieres
2010-11-06 23:56 ` Janus Weil
2010-11-07  7:55   ` Tobias Burnus
2010-11-07 12:04     ` Janus Weil
2010-11-07 12:11       ` Tobias Schlüter
2010-11-07 13:19         ` Tobias Burnus
2010-11-07 14:21           ` Janus Weil
2010-11-07 15:34           ` Tobias Schlüter
2010-11-07 15:50             ` Janus Weil
2010-11-07 16:39               ` Tobias Schlüter
2010-11-07 16:30       ` Steve Kargl

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).