Patchwork [C] fix reference to classes with -fdump-ada-spec

login
register
mail settings
Submitter Arnaud Charlet
Date Oct. 28, 2010, 8:59 a.m.
Message ID <20101028085926.GA18090@adacore.com>
Download mbox | patch
Permalink /patch/69444/
State New
Headers show

Comments

Arnaud Charlet - Oct. 28, 2010, 8:59 a.m.
This is a patch that fixes class references across header files when
generate Ada specs via -fdump-ada-spec.

Consider the following code:

-- f1.hh
#ifndef F1
#define F1
class c1 {
 c1();
 ~c1();
};
#endif

-- f2.hh
#include "f1.hh"

class c2{
   c1 *data;
   c2();
   ~c2();
};

Prior to this change, we would generate incorrect references to
c1, missing the c1_Class prefix:
<<
package f2_hh is

   package Class_c2 is
      type c2 is limited record
         data : access f1_hh.c1;  -- f2.hh:4
      end record;
>>

With this patch we now generate:

<<
package f2_hh is

   package Class_c2 is
      type c2 is limited record
         data : access f1_hh.Class_c1.c1;  -- f2.hh:4
      end record;
>>

This is fixed by using the same test used to decide whether to create
a nested package for classes, factorized in a new function
separate_class_package and called in pp_ada_tree_identifier and
print_ada_declaration.

Tested on x86_64-pc-linux-gnu, OK for trunk?

c-family:

2010-10-28  Arnaud Charlet  <charlet@adacore.com>
            Matthew Gingell  <gingell@adacore.com>

	* c-ada-spec.c (separate_class_package): New function.
	(pp_ada_tree_identifier): Prefix references to C++ classes with the
	name of their enclosing package.
	(print_ada_declaration): Use separate_class_package.
--
Joseph S. Myers - Oct. 28, 2010, 6 p.m.
On Thu, 28 Oct 2010, Arnaud Charlet wrote:

> 2010-10-28  Arnaud Charlet  <charlet@adacore.com>
>             Matthew Gingell  <gingell@adacore.com>
> 
> 	* c-ada-spec.c (separate_class_package): New function.
> 	(pp_ada_tree_identifier): Prefix references to C++ classes with the
> 	name of their enclosing package.
> 	(print_ada_declaration): Use separate_class_package.

OK with the duplicate space after "||" in

> +	&& (TYPE_METHODS (type) ||  has_static_fields (type));

fixed, unless there are any objections from C++ maintainers within 24 
hours.

Patch

--- c-ada-spec.c	(revision 166026)
+++ c-ada-spec.c	(working copy)
@@ -51,6 +51,7 @@  static void dump_ada_withs (FILE *);
 static void dump_ads (const char *, void (*)(const char *),
 		      int (*)(tree, cpp_operation));
 static char *to_ada_name (const char *, int *);
+static bool separate_class_package (tree);
 
 #define LOCATION_COL(LOC) ((expand_location (LOC)).column)
 
@@ -1152,6 +1153,23 @@  to_ada_name (const char *name, int *spac
   return s;
 }
 
+/* Return true if DECL refers to a C++ class type for which a
+   separate enclosing package has been or should be generated.  */
+
+static bool
+separate_class_package (tree decl)
+{
+  if (decl) 
+    {
+      tree type = TREE_TYPE (decl);
+      return type
+	&& TREE_CODE (type) == RECORD_TYPE
+	&& (TYPE_METHODS (type) ||  has_static_fields (type));
+    }
+  else
+    return false;
+}
+
 static bool package_prefix = true;
 
 /* Dump in BUFFER the name of an identifier NODE of type TYPE, following Ada
@@ -1209,7 +1227,15 @@  pp_ada_tree_identifier (pretty_printer *
 		  default:
 		    break;
 		}
-	    }
+              
+              if (separate_class_package (decl))
+                {
+                  pp_string (buffer, "Class_");
+                  pp_string (buffer, s);
+                  pp_string (buffer, ".");
+                }
+
+            }
 	}
     }
 
@@ -2607,8 +2633,7 @@  print_ada_declaration (pretty_printer *b
 	      {
 		dump_nested_types (buffer, t, t, false, cpp_check, spc);
 
-		if (TYPE_METHODS (TREE_TYPE (t))
-		    || has_static_fields (TREE_TYPE (t)))
+                if (separate_class_package (t))
 		  {
 		    is_class = true;
 		    pp_string (buffer, "package Class_");