This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


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

#pragma GCC java_exceptions - now with documentation!


This is a revision of the patch being mooted under the "libjava test
suite keeps getting stuck" thread.  It adds a #pragma to C++ to force
it to generate Java-style exceptions.  We can use that #pragma in
libjava to break its dependency on libsupc++.

The major change from the previous revision is the inclusion of
documentation.  I'd like to have an example, but I'm not good enough
with C++ to know when this pragma is necessary.  Also, the changes to
avoid C++'s operator new (_Znwj) in libjava have already gone in.

zw

gcc/cp:
	* except.c: Export choose_personality_routine.
	* cp-tree.h: Prototype choose_personality_routine.
	* lex.c (handle_pragma_java_exceptions): New function.
	(init_cp_pragma): Register #pragma GCC java_exceptions.
gcc:
	* extend.texi: Document new #pragma GCC java_exceptions.

libjava:
	* include/jvm.h: Insert #pragma GCC java_exceptions at top of file.
	* Makefile.am (libgcj_la_OBJECTS): Remove libsupc++convenience.la.
	* Makefile.in: Regenerate (by hand).
	* doc/cni.sgml: Document #pragma GCC java_exceptions here too.

===================================================================
Index: gcc/extend.texi
--- gcc/extend.texi	2001/03/23 01:49:08	1.95
+++ gcc/extend.texi	2001/04/28 00:34:27
@@ -3796,6 +3796,7 @@ Predefined Macros,cpp.info,The C Preproc
 * Bound member functions:: You can extract a function pointer to the
                         method denoted by a @samp{->*} or @samp{.*} expression.
 * C++ Attributes::      Variable, function, and type attributes for C++ only.
+* Java Exceptions::     Tweaking exception handling to work with Java.
 * Deprecated Features:: Things might disappear from g++.
 * Backwards Compatibility:: Compatibilities with earlier definitions of C++.
 @end menu
@@ -4364,6 +4365,23 @@ Calls to methods declared in this interf
 interface table mechanism, instead of regular virtual table dispatch.  
 
 @end table
+
+@node Java Exceptions
+@section Java Exceptions
+
+The Java language uses a slightly different exception handling model
+from C++.  Normally, GNU C++ will automatically detect when you are
+writing C++ code that uses Java exceptions, and handle them
+appropriately.  However, under some circumstances it is not able to
+tell.  The usual effect of an incorrect guess is a link failure,
+complaining of a missing routine @samp{__gxx_personality_v0}.
+
+You can inform the compiler that Java exceptions are to be used in a
+translation unit, irrespective of what it might think, by writing
+@samp{@w{#pragma GCC java_exceptions}} at the head of the file.  This
+@samp{#pragma} must appear before any functions that throw or catch
+exceptions, or run destructors when exceptions are thrown through them.
+@c FIXME: Add example.
 
 @node Deprecated Features
 @section Deprecated Features
===================================================================
Index: gcc/cp/cp-tree.h
--- gcc/cp/cp-tree.h	2001/04/27 10:47:34	1.605
+++ gcc/cp/cp-tree.h	2001/04/28 00:34:27
@@ -4008,6 +4008,7 @@ extern tree build_throw				PARAMS ((tree
 extern void mark_all_runtime_matches            PARAMS ((void));
 extern int nothrow_libfn_p			PARAMS ((tree));
 extern void check_handlers			PARAMS ((tree));
+extern void choose_personality_routine		PARAMS ((bool));
 
 /* in expr.c */
 extern void init_cplus_expand			PARAMS ((void));
===================================================================
Index: gcc/cp/except.c
--- gcc/cp/except.c	2001/04/27 13:33:41	1.130
+++ gcc/cp/except.c	2001/04/28 00:34:27
@@ -43,7 +43,6 @@ static int dtor_nothrow PARAMS ((tree));
 static tree do_end_catch PARAMS ((tree));
 static void push_eh_cleanup PARAMS ((tree));
 static bool decl_is_java_type PARAMS ((tree decl, int err));
-static void choose_personality_routine PARAMS ((bool));
 static void initialize_handler_parm PARAMS ((tree, tree));
 static tree do_allocate_exception PARAMS ((tree));
 static int complete_ptr_ref_or_void_ptr_p PARAMS ((tree, tree));
@@ -259,7 +258,7 @@ decl_is_java_type (decl, err)
   return r;
 }
 
-static void
+void
 choose_personality_routine (is_java)
      bool is_java;
 {
===================================================================
Index: gcc/cp/lex.c
--- gcc/cp/lex.c	2001/04/24 08:22:05	1.243
+++ gcc/cp/lex.c	2001/04/28 00:34:27
@@ -61,6 +61,7 @@ static void handle_pragma_vtable PARAMS 
 static void handle_pragma_unit PARAMS ((cpp_reader *));
 static void handle_pragma_interface PARAMS ((cpp_reader *));
 static void handle_pragma_implementation PARAMS ((cpp_reader *));
+static void handle_pragma_java_exceptions PARAMS ((cpp_reader *));
 static void cxx_init PARAMS ((void));
 static void cxx_finish PARAMS ((void));
 static void cxx_init_options PARAMS ((void));
@@ -687,6 +688,8 @@ init_cp_pragma ()
   cpp_register_pragma (parse_in, "GCC", "interface", handle_pragma_interface);
   cpp_register_pragma (parse_in, "GCC", "implementation",
 		       handle_pragma_implementation);
+  cpp_register_pragma (parse_in, "GCC", "java_exceptions",
+		       handle_pragma_java_exceptions);
 }
 
 const char *
@@ -1179,6 +1182,18 @@ handle_pragma_implementation (dfile)
       ifiles->next = impl_file_chain;
       impl_file_chain = ifiles;
     }
+}
+
+/* Indicate that this file uses Java-personality exception handling.  */
+static void
+handle_pragma_java_exceptions (dfile)
+     cpp_reader *dfile ATTRIBUTE_UNUSED;
+{
+  tree x;
+  if (c_lex (&x) != CPP_EOF)
+    warning ("junk at end of #pragma GCC java_exceptions");
+
+  choose_personality_routine (/* is_java = */ true);
 }
 
 void
===================================================================
Index: libjava/Makefile.am
--- libjava/Makefile.am	2001/04/25 15:45:10	1.140
+++ libjava/Makefile.am	2001/04/28 00:34:28
@@ -260,7 +260,6 @@ libgcj.la: $(libgcj_la_OBJECTS) $(libgcj
 	@echo $(libgcj_la_OBJECTS) > libgcj.objectlist;
 	@echo $(libgcj_la_LIBADD) >> libgcj.objectlist;
 	$(libgcj_la_LINK) -objectlist libgcj.objectlist \
-	../libstdc++-v3/libsupc++/libsupc++convenience.la \
 	../libffi/libfficonvenience.la \
 	-rpath $(toolexeclibdir) $(libgcj_la_LDFLAGS) $(LIBS)
 
===================================================================
Index: libjava/Makefile.in
--- libjava/Makefile.in	2001/04/25 15:45:08	1.150
+++ libjava/Makefile.in	2001/04/28 00:34:28
@@ -2450,7 +2450,6 @@ libgcj.la: $(libgcj_la_OBJECTS) $(libgcj
 	@echo $(libgcj_la_OBJECTS) > libgcj.objectlist;
 	@echo $(libgcj_la_LIBADD) >> libgcj.objectlist;
 	$(libgcj_la_LINK) -objectlist libgcj.objectlist \
-	../libstdc++-v3/libsupc++/libsupc++convenience.la \
 	../libffi/libfficonvenience.la \
 	-rpath $(toolexeclibdir) $(libgcj_la_LDFLAGS) $(LIBS)
 
===================================================================
Index: libjava/doc/cni.sgml
--- libjava/doc/cni.sgml	2000/09/05 17:48:57	1.4
+++ libjava/doc/cni.sgml	2001/04/28 00:34:28
@@ -779,6 +779,21 @@ if (i >= count)
    throw new java::lang::IndexOutOfBoundsException();
 </programlisting>
 </para>
+<para>
+Normally, GNU C++ will automatically detect when you are writing C++
+code that uses Java exceptions, and handle them appropriately.
+However, under some circumstances it is not able to tell.  The usual
+effect of an incorrect guess is a link failure, complaining of a
+missing routine <literal>__gxx_personality_v0</literal>.
+</para>
+<para>
+You can inform the compiler that Java exceptions are to be used in a
+translation unit, irrespective of what it might think, by writing
+<literal>#pragma GCC java_exceptions</literal> at the head of the
+file.  This <literal>#pragma</literal> must appear before any
+functions that throw or catch exceptions, or run destructors when
+exceptions are thrown through them.</para>
+<!-- FIXME: Add example. -->
 </sect1>
 
 <sect1><title>Synchronization</title>
===================================================================
Index: libjava/include/jvm.h
--- libjava/include/jvm.h	2001/03/23 19:15:43	1.32
+++ libjava/include/jvm.h	2001/04/28 00:34:28
@@ -11,6 +11,9 @@ details.  */
 #ifndef __JAVA_JVM_H__
 #define __JAVA_JVM_H__
 
+// Force C++ compiler to use Java-style exceptions.
+#pragma GCC java_exceptions
+
 #include <gcj/javaprims.h>
 
 #include <java-assert.h>


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