[prev in list] [next in list] [prev in thread] [next in thread] 

List:       gcc-fortran
Subject:    [gfortran,patch] F2003 USE syntax and OpenMP module
From:       FX Coudert <fxcoudert () gmail ! com>
Date:       2006-10-30 6:56:26
Message-ID: 6DB3B344-02EA-4B6D-951F-90103C3F6FC1 () gmail ! com
[Download RAW message or body]

:ADDPATCH fortran:

Attached patch allow gfortran to use the Fortran 2003 syntax for the  
USE statement, i.e. with specification of the module nature  
(INTRINSIC, NON_INTRINSIC or unspecified). I changed the way the pre- 
compiled intrinsic modules are found so that they do not get confused  
with non-intrinsic modules (the only such pre-compiled intrinsic  
module right now is the OpenMP module, omp_lib; I plan to add the  
IEEE modules as pre-compiled modules after that patch is commited).

With this patch, instead of adding "-I $finclude" to the f951 command  
arguments, I propose that we add "-fintrinsic-modules-path  
$finclude", where -fintrinsic-modules-path is a new option that adds  
the given path to the list of include directories, but explicitly  
notes that it will not be used during non-intrinsic modules search.  
It also adds the path to a new list of directories where intrinsic  
pre-compiled modules are expected to be found.

It allows things like the following to work:

module omp_lib
   integer x
end

subroutine foo
   use omp_lib
   implicit none
   print *, x
end

subroutine foo2
   use, intrinsic :: omp_lib
   implicit none
   print *, omp_integer_kind
end


I'll soon provide some more testcases about all possible F2003-syntax  
possibilities. Bootstrapped and regtested on i686-linux. OK for  
mainline?

FX



["include_path.ChangeLog" (include_path.ChangeLog)]

2006-10-29  Francois-Xavier Coudert  <coudert@clipper.ens.fr>

	* gfortran.h (gfc_add_intrinsic_modules_path,
	gfc_open_intrinsic_module): New prototypes.
	(gfc_add_include_path, gfc_open_included_file): Update prototypes.
	* lang.opt: Add -fintrinsic-modules-path option.
	* module.c (gfc_match_use): Match the Fortran 2003 form of
	USE statement.
	(gfc_use_module): Also handle intrinsic modules. 
	* scanner.c (gfc_directorylist): Add use_for_modules for field.
	(intrinsic_modules_dirs): New static variable.
	(add_path_to_list, gfc_add_intrinsic_modules_path): New functions.
	(gfc_add_include_path): Use the new add_path_to_list helper
	function.
	(gfc_release_include_path): Free memory for intrinsic_modules_dirs.
	(open_included_file, gfc_open_intrinsic_module): New functions.
	(gfc_open_included_file): Use the new open_included_file
	helper function.
	* lang-specs.h: Use the new -fintrinsic-modules-path option.
	* parse.c (decode_statement): Do not match the required space
	after USE here.
	* options.c (gfc_handle_option): Handle the new option. Use new
	prototype for gfc_add_include_path.
	(gfc_post_options): Use new prototype for gfc_add_include_path.


["include_path.diff" (include_path.diff)]

Index: gcc/fortran/gfortran.h
===================================================================
--- gcc/fortran/gfortran.h	(revision 118151)
+++ gcc/fortran/gfortran.h	(working copy)
@@ -1712,9 +1712,11 @@
 void gfc_scanner_done_1 (void);
 void gfc_scanner_init_1 (void);
 
-void gfc_add_include_path (const char *);
+void gfc_add_include_path (const char *, bool);
+void gfc_add_intrinsic_modules_path (const char *);
 void gfc_release_include_path (void);
-FILE *gfc_open_included_file (const char *, bool);
+FILE *gfc_open_included_file (const char *, bool, bool);
+FILE *gfc_open_intrinsic_module (const char *);
 
 int gfc_at_end (void);
 int gfc_at_eof (void);
Index: gcc/fortran/lang.opt
===================================================================
--- gcc/fortran/lang.opt	(revision 118151)
+++ gcc/fortran/lang.opt	(working copy)
@@ -153,6 +153,10 @@
 Fortran
 Specify that no implicit typing is allowed, unless overridden by explicit IMPLICIT \
statements  
+fintrinsic-modules-path
+Fortran RejectNegative Joined Separate
+Specify where to find the compiled intrinsic modules
+
 ffixed-line-length-none
 Fortran RejectNegative
 Allow arbitrary character line width in fixed mode
Index: gcc/fortran/module.c
===================================================================
--- gcc/fortran/module.c	(revision 118151)
+++ gcc/fortran/module.c	(working copy)
@@ -173,6 +173,9 @@
 /* The name of the module we're reading (USE'ing) or writing.  */
 static char module_name[GFC_MAX_SYMBOL_LEN + 1];
 
+/* The way the module we're reading was specified.  */
+static bool specified_nonint, specified_int;
+
 static int module_line, module_column, only_flag;
 static enum
 { IO_INPUT, IO_OUTPUT }
@@ -483,12 +486,56 @@
 match
 gfc_match_use (void)
 {
-  char name[GFC_MAX_SYMBOL_LEN + 1];
+  char name[GFC_MAX_SYMBOL_LEN + 1], module_nature[GFC_MAX_SYMBOL_LEN + 1];
   gfc_use_rename *tail = NULL, *new;
   interface_type type;
   gfc_intrinsic_op operator;
   match m;
 
+  specified_int = false;
+  specified_nonint = false;
+
+  if (gfc_match (" , ") == MATCH_YES)
+    {
+      if ((m = gfc_match (" %n ::", module_nature)) == MATCH_YES)
+       {
+         if (gfc_notify_std (GFC_STD_F2003, "New in Fortran 2003: module"
+                             "nature in USE statement at %C") == FAILURE)
+           return MATCH_ERROR;
+
+         if (strcmp (module_nature, "intrinsic") == 0)
+           specified_int = true;
+         else
+           {
+             if (strcmp (module_nature, "non_intrinsic") == 0)
+               specified_nonint = true;
+             else
+               {
+                 gfc_error ("Module nature in USE statement at %C should "
+                            "be either INTRINSIC or NON_INTRINSIC");
+                 return MATCH_ERROR;
+               }
+           }
+       }
+      else
+       return m;
+    }
+  else
+    {
+      m = gfc_match (" ::");
+      if (m == MATCH_YES &&
+	  gfc_notify_std (GFC_STD_F2003, "New in Fortran 2003: "
+			  "\"USE :: module\" at %C") == FAILURE)
+       return MATCH_ERROR;
+
+      if (m != MATCH_YES)
+       {
+         m = gfc_match ("% ");
+         if (m != MATCH_YES)
+           return m;
+       }
+    }
+
   m = gfc_match_name (module_name);
   if (m != MATCH_YES)
     return m;
@@ -3797,7 +3844,33 @@
   strcpy (filename, module_name);
   strcat (filename, MODULE_EXTENSION);
 
-  module_fp = gfc_open_included_file (filename, true);
+  /* First, try to find an non-intrinsic module, unless the USE statement
+     specified that the module is intrinsic.  */
+  module_fp = NULL;
+  if (!specified_int)
+    module_fp = gfc_open_included_file (filename, true, true);
+
+  /* Then, see if it's an intrinsic one, unless the USE statement
+     specified that the module is non-intrinsic.  */
+  if (module_fp == NULL && !specified_nonint)
+    {
+#if 0
+      if (strcmp (module_name, "iso_fortran_env") == 0
+         && gfc_notify_std (GFC_STD_F2003, "New in Fortran 2003: "
+                            "ISO_FORTRAN_ENV intrinsic module at %C") != FAILURE)
+       {
+         use_iso_fortran_env_module ();
+         return;
+       }
+#endif
+
+      module_fp = gfc_open_intrinsic_module (filename);
+
+      if (module_fp == NULL && specified_int)
+       gfc_fatal_error ("Can't find an intrinsic module named '%s' at %C",
+                        module_name);
+    }
+
   if (module_fp == NULL)
     gfc_fatal_error ("Can't open module file '%s' for reading at %C: %s",
 		     filename, strerror (errno));
Index: gcc/fortran/scanner.c
===================================================================
--- gcc/fortran/scanner.c	(revision 118151)
+++ gcc/fortran/scanner.c	(working copy)
@@ -51,12 +51,13 @@
 typedef struct gfc_directorylist
 {
   char *path;
+  bool use_for_modules;
   struct gfc_directorylist *next;
 }
 gfc_directorylist;
 
 /* List of include file search directories.  */
-static gfc_directorylist *include_dirs;
+static gfc_directorylist *include_dirs, *intrinsic_modules_dirs;
 
 static gfc_file *file_head, *current_file;
 
@@ -118,22 +119,21 @@
 
 /* Adds path to the list pointed to by list.  */
 
-void
-gfc_add_include_path (const char *path)
+static void
+add_path_to_list (gfc_directorylist **list, const char *path,
+		  bool use_for_modules)
 {
   gfc_directorylist *dir;
   const char *p;
 
   p = path;
-  while (*p == ' ' || *p == '\t')  /* someone might do 'gfortran "-I include"' */
+  while (*p == ' ' || *p == '\t')  /* someone might do "-I include" */
     if (*p++ == '\0')
       return;
 
-  dir = include_dirs;
+  dir = *list;
   if (!dir)
-    {
-      dir = include_dirs = gfc_getmem (sizeof (gfc_directorylist));
-    }
+    dir = *list = gfc_getmem (sizeof (gfc_directorylist));
   else
     {
       while (dir->next)
@@ -144,12 +144,27 @@
     }
 
   dir->next = NULL;
+  dir->use_for_modules = use_for_modules;
   dir->path = gfc_getmem (strlen (p) + 2);
   strcpy (dir->path, p);
   strcat (dir->path, "/");	/* make '/' last character */
 }
 
 
+void
+gfc_add_include_path (const char *path, bool use_for_modules)
+{
+  add_path_to_list (&include_dirs, path, use_for_modules);
+}
+
+
+void
+gfc_add_intrinsic_modules_path (const char *path)
+{
+  add_path_to_list (&intrinsic_modules_dirs, path, true);
+}
+
+
 /* Release resources allocated for options.  */
 
 void
@@ -165,28 +180,30 @@
       gfc_free (p->path);
       gfc_free (p);
     }
+
+  gfc_free (gfc_option.module_dir);
+  while (intrinsic_modules_dirs != NULL)
+    {
+      p = intrinsic_modules_dirs;
+      intrinsic_modules_dirs = intrinsic_modules_dirs->next;
+      gfc_free (p->path);
+      gfc_free (p);
+    }
 }
 
-/* Opens file for reading, searching through the include directories
-   given if necessary.  If the include_cwd argument is true, we try
-   to open the file in the current directory first.  */
 
-FILE *
-gfc_open_included_file (const char *name, const bool include_cwd)
+static FILE *
+open_included_file (const char *name, gfc_directorylist *list, bool module)
 {
   char *fullname;
   gfc_directorylist *p;
   FILE *f;
 
-  if (include_cwd)
+  for (p = list; p; p = p->next)
     {
-      f = gfc_open_file (name);
-      if (f != NULL)
-	return f;
-    }
+      if (module && !p->use_for_modules)
+	continue;
 
-  for (p = include_dirs; p; p = p->next)
-    {
       fullname = (char *) alloca(strlen (p->path) + strlen (name) + 1);
       strcpy (fullname, p->path);
       strcat (fullname, name);
@@ -199,6 +216,32 @@
   return NULL;
 }
 
+
+/* Opens file for reading, searching through the include directories
+   given if necessary.  If the include_cwd argument is true, we try
+   to open the file in the current directory first.  */
+
+FILE *
+gfc_open_included_file (const char *name, bool include_cwd, bool module)
+{
+  FILE *f;
+
+  if (include_cwd)
+    {
+      f = gfc_open_file (name);
+      if (f != NULL)
+	return f;
+    }
+
+  return open_included_file (name, include_dirs, module);
+}
+
+FILE *
+gfc_open_intrinsic_module (const char *name)
+{
+  return open_included_file (name, intrinsic_modules_dirs, true);
+}
+
 /* Test to see if we're at the end of the main source file.  */
 
 int
@@ -1400,7 +1443,7 @@
     }
   else
     {
-      input = gfc_open_included_file (filename, false);
+      input = gfc_open_included_file (filename, false, false);
       if (input == NULL)
 	{
 	  gfc_error_now ("Can't open included file '%s'", filename);
Index: gcc/fortran/lang-specs.h
===================================================================
--- gcc/fortran/lang-specs.h	(revision 118151)
+++ gcc/fortran/lang-specs.h	(working copy)
@@ -15,7 +15,7 @@
       %{E|M|MM:%(cpp_debug_options)}\
       %{!M:%{!MM:%{!E: -o %|.f |\n\
     f951 %|.f %{!ffree-form:-ffixed-form} %(cc1_options) %{J*} %{I*}\
-      -fpreprocessed %{!nostdinc:-I finclude%s} %{!fsyntax-only:%(invoke_as)}}}}", \
0, 0, 0}, +      -fpreprocessed %{!nostdinc:-fintrinsic-modules-path finclude%s} \
%{!fsyntax-only:%(invoke_as)}}}}", 0, 0, 0},  {".F90", "@f95-cpp-input", 0, 0, 0},
 {".F95", "@f95-cpp-input", 0, 0, 0},
 {"@f95-cpp-input",
@@ -23,13 +23,13 @@
       %{E|M|MM:%(cpp_debug_options)}\
       %{!M:%{!MM:%{!E: -o %|.f95 |\n\
     f951 %|.f95 %{!ffixed-form:-ffree-form} %(cc1_options) %{J*} %{I*}\
-      -fpreprocessed %{!nostdinc:-I finclude%s} %{!fsyntax-only:%(invoke_as)}}}}", \
0, 0, 0}, +      -fpreprocessed %{!nostdinc:-fintrinsic-modules-path finclude%s} \
%{!fsyntax-only:%(invoke_as)}}}}", 0, 0, 0},  {".f90", "@f95", 0, 0, 0},
 {".f95", "@f95", 0, 0, 0},
 {"@f95", "%{!E:f951 %i %(cc1_options) %{J*} %{I*}\
-         %{!nostdinc:-I finclude%s} %{!fsyntax-only:%(invoke_as)}}", 0, 0, 0},
+         %{!nostdinc:-fintrinsic-modules-path finclude%s} \
%{!fsyntax-only:%(invoke_as)}}", 0, 0, 0},  {".f",   "@f77", 0, 0, 0},
 {".for", "@f77", 0, 0, 0},
 {".FOR", "@f77", 0, 0, 0},
 {"@f77", "%{!E:f951 %i %{!ffree-form:-ffixed-form} %(cc1_options) %{J*} %{I*}\
-         %{!nostdinc:-I finclude%s} %{!fsyntax-only:%(invoke_as)}}", 0, 0, 0},
+         %{!nostdinc:-fintrinsic-modules-path finclude%s} \
                %{!fsyntax-only:%(invoke_as)}}", 0, 0, 0},
Index: gcc/fortran/parse.c
===================================================================
--- gcc/fortran/parse.c	(revision 118151)
+++ gcc/fortran/parse.c	(working copy)
@@ -279,7 +279,7 @@
       break;
 
     case 'u':
-      match ("use% ", gfc_match_use, ST_USE);
+      match ("use", gfc_match_use, ST_USE);
       break;
 
     case 'w':
Index: gcc/fortran/options.c
===================================================================
--- gcc/fortran/options.c	(revision 118151)
+++ gcc/fortran/options.c	(working copy)
@@ -220,10 +220,10 @@
       source_path = alloca (i + 1);
       memcpy (source_path, canon_source_file, i);
       source_path[i] = 0;
-      gfc_add_include_path (source_path);
+      gfc_add_include_path (source_path, true);
     }
   else
-    gfc_add_include_path (".");
+    gfc_add_include_path (".", true);
 
   if (canon_source_file != gfc_source_file)
     gfc_free ((void *) canon_source_file);
@@ -514,6 +514,11 @@
       gfc_option.flag_implicit_none = value;
       break;
 
+    case OPT_fintrinsic_modules_path:
+      gfc_add_include_path (arg, false);
+      gfc_add_intrinsic_modules_path (arg);
+      break;
+
     case OPT_fmax_stack_var_size_:
       gfc_option.flag_max_stack_var_size = value;
       break;
@@ -568,7 +573,7 @@
       break;
 
     case OPT_I:
-      gfc_add_include_path (arg);
+      gfc_add_include_path (arg, true);
       break;
 
     case OPT_J:



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic