]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
fortran: C++ support for generating C prototypes
authorJanne Blomqvist <jb@gcc.gnu.org>
Sun, 12 May 2019 08:26:18 +0000 (11:26 +0300)
committerJanne Blomqvist <jb@gcc.gnu.org>
Sun, 12 May 2019 08:26:18 +0000 (11:26 +0300)
When generating C prototypes for Fortran procedures with the
-fc-prototypes and -fc-prototypes-external options, print a snippet
defining macros for complex types, and add C++ support by suppressing
mangling.

fortran/ChangeLog:

2019-05-12  Janne Blomqvist  <jb@gcc.gnu.org>

* dump-parse-tree.c (get_c_type_name): Use macros for complex type
names.
* parse.c (gfc_parse_file): Define complex macros, add CPP support
when printing C prototypes.

From-SVN: r271106

gcc/fortran/ChangeLog
gcc/fortran/dump-parse-tree.c
gcc/fortran/parse.c

index 0198637f366c8db7088394d9e1181caa0571a397..512e6322f3a2b991d24ba1f4a3a6501b7ac8a2af 100644 (file)
@@ -1,3 +1,10 @@
+2019-05-12  Janne Blomqvist  <jb@gcc.gnu.org>
+
+       * dump-parse-tree.c (get_c_type_name): Use macros for complex type
+       names.
+       * parse.c (gfc_parse_file): Define complex macros, add CPP support
+       when printing C prototypes.
+
 2019-05-10  Thomas Koenig  <tkoenig@gcc.gnu.org>
 
        PR fortran/61968
index 54af5dfd50dacbd8dee351653b58c9a75eb952f2..41b2c88265c18872241c3105c9d66837b8f5df4a 100644 (file)
@@ -3143,11 +3143,11 @@ get_c_type_name (gfc_typespec *ts, gfc_array_spec *as, const char **pre,
          else if (strcmp (*type_name, "size_t") == 0)
            *type_name = "ssize_t";
          else if (strcmp (*type_name, "float_complex") == 0)
-           *type_name = "float complex";
+           *type_name = "__GFORTRAN_FLOAT_COMPLEX";
          else if (strcmp (*type_name, "double_complex") == 0)
-           *type_name = "double complex";
+           *type_name = "__GFORTRAN_DOUBLE_COMPLEX";
          else if (strcmp (*type_name, "long_double_complex") == 0)
-           *type_name = "long double complex";
+           *type_name = "__GFORTRAN_LONG_DOUBLE_COMPLEX";
 
          ret = T_OK;
        }
@@ -3166,11 +3166,11 @@ get_c_type_name (gfc_typespec *ts, gfc_array_spec *as, const char **pre,
                  else if (strcmp (*type_name, "size_t") == 0)
                    *type_name = "ssize_t";
                  else if (strcmp (*type_name, "float_complex") == 0)
-                   *type_name = "float complex";
+                   *type_name = "__GFORTRAN_FLOAT_COMPLEX";
                  else if (strcmp (*type_name, "double_complex") == 0)
-                   *type_name = "double complex";
+                   *type_name = "__GFORTRAN_DOUBLE_COMPLEX";
                  else if (strcmp (*type_name, "long_double_complex") == 0)
-                   *type_name = "long double complex";
+                   *type_name = "__GFORTRAN_LONG_DOUBLE_COMPLEX";
 
                  ret = T_WARN;
                  break;
index 9d693595e207015a1c6f0f10c9d8fea3a9e04e8d..155534cd8224d86f9088db0ecdc82dd4df55428e 100644 (file)
@@ -6331,6 +6331,24 @@ done:
       }
 
   /* Dump C prototypes.  */
+  if (flag_c_prototypes || flag_c_prototypes_external)
+    {
+      fprintf (stdout,
+              _("#include <stddef.h>\n"
+                "#ifdef __cplusplus\n"
+                "#include <complex>\n"
+                "#define __GFORTRAN_FLOAT_COMPLEX std::complex<float>\n"
+                "#define __GFORTRAN_DOUBLE_COMPLEX std::complex<double>\n"
+                "#define __GFORTRAN_LONG_DOUBLE_COMPLEX std::complex<long double>\n"
+                "extern \"C\" {\n"
+                "#else\n"
+                "#define __GFORTRAN_FLOAT_COMPLEX float _Complex\n"
+                "#define __GFORTRAN_DOUBLE_COMPLEX double _Complex\n"
+                "#define __GFORTRAN_LONG_DOUBLE_COMPLEX long double _Complex\n"
+                "#endif\n\n"));
+    }
+
+  /* First dump BIND(C) prototypes.  */
   if (flag_c_prototypes)
     {
       for (gfc_current_ns = gfc_global_ns_list; gfc_current_ns;
@@ -6342,6 +6360,10 @@ done:
   if (flag_c_prototypes_external)
     gfc_dump_external_c_prototypes (stdout);
 
+  if (flag_c_prototypes || flag_c_prototypes_external)
+    fprintf (stdout,
+            _("\n#ifdef __cplusplus\n}\n#endif\n"));
+
   /* Do the translation.  */
   translate_all_program_units (gfc_global_ns_list);