[Patch, fortran] PR52846 - [F2008] Support submodules

Paul Richard Thomas paul.richard.thomas@gmail.com
Mon Jun 22 12:41:00 GMT 2015


Dear All,

This patch enables submodule support in gfortran. Submodules are a
feature of F2008 but are fully described in ISO/IEC TR 19767:2004(E).

The patch has one significant non-conformance (that I know about,
anyway!); whilst private derived type components are correctly dealt
with, symbols whose access is private within the parent module are
not. They should effectively be host associated in descendant
submodules. At present gfortran handles private access at the module
write stage. This means that when a submodule reads the module file,
there is no information present about symbols whose access was
private. Since this modification might cause significant fall-out to
existing code, I propose to submit a separate patch later on to sort
out the non-conformance. However, as required private and public
statements are not allowed in submodules.

The patch makes maximum possible leverage of existing code to handle
modules. Once the submodule is matched, the ancestor module and
submodules are first "used" and then all the symbols are set host
associated and private derived type components set public.

Most of the work involved matching module procedures, with both the
traditional form of declaration and the abbreviated one. I have chosen
to treat MODULE as a prefix like PURE or ELEMENTAL. This is logical
both because of the form of the declaration and because the
identification of module procedures is most easily done with an
attribute bit. With traditional procedure declarations, the procedure,
result and dummy characteristics are compared with those of the
interface declaration. The comparison of the dummy characteristics is
a bit cobbled together and might be better done by copying the
formal_namespace and it's contents to the new symbol and retaining the
old for the interface symbol. This patch leaves the old dummy symbols
in the formal namespace in the new ones in the formal arglist. I have
checked that cleanup occurs for all objects.

Note the comment in submodule_1.f90 about the possibility of
undetected recursion between procedures in different submodules. I am
not at all sure that I know how to deal with this and am open to
suggestions.

In addition, it should be noted that collisions between the names of
entities and procedures, other than module procedures are detected by
the linker at present.

Apart from this, all is very straightforward and follows the the ChangeLogs.

Thanks for testing of an early version of the patch by Damian Rouson,
Salvatore Filippone and Tobias Burnus.

Bootstrapped and regtested on FC21/x86_64 - OK for trunk?

Cheers

Paul

2015-06-22  Paul Thomas  <pault@gcc.gnu.org>

    PR fortran/52846
    * decl.c (get_proc_name): Make a partially populated interface
    symbol to carry the characteristics of a module procedure and
    its result.
    (match_attr_spec): Submodule variables have implicit save
    attribute for F2008 onwards.
    (gfc_match_prefix): Add 'module' as the a prefix and set the
    module_procedure attribute.
    (gfc_match_formal_arglist): For a module procedure keep the
    interface formal_arglist from the interface, match new the
    formal arguments and then compare the number and names of each.
    (gfc_match_procedure): Add case COMP_SUBMODULE.
    (gfc_match_function_decl, gfc_match_subroutine_decl): Set the
    module_procedure attribute.
    (gfc_match_entry, gfc_match_end):  Add case COMP_SUBMODULE.
    (gfc_match_submod_proc): New function to match the abbreviated
    style of submodule declaration.
    * gfortran.h : Add ST_SUBMODULE and ST_END_SUBMODULE. Add the
    attribute bits 'used_in_submodule' and 'module_procedure'. Add
    prototypes for the functions 'gfc_check_dummy_characteristics'
    and 'gfc_check_result_characteristics'.
    * interface.c : Add the prefix 'gfc_' to the names of functions
    'check_dummy(result)_characteristics' and all their references.
    * match.h : Add prototype for 'gfc_match_submod_proc' and
    'gfc_match_submodule'.
    * module.c (gfc_match_submodule): New function. Add handling
    for the 'module_procedure' attribute bit.
    * parse.c (decode_statement): Handle a match occurring in
    'gfc_match_submod_proc' and a match for 'submodule'.
    (gfc_enclosing_unit): Include the state COMP_SUBMODULE.
    (gfc_ascii_statement): Add END SUBMODULE.
    (accept_statement): Add ST_SUBMODULE.
    (parse_spec): Disallow statement functions in a submodule
    specification part.
    (parse_contained): Add ST_END_SUBMODULE and COMP_SUBMODULE
    twice each.
    (set_syms_host_assoc): Make symbols from the ancestor module
    and submodules use associated, as required by the standard and
    set all private components public. Module procedures 'external'
    attribute bit is reset and the 'used_in_submodule' bit is set.
    (parse_module): If this is a submodule, use the ancestor module
    and submodules. Traverse the namespace, calling
    'set_syms_host_assoc'. Add ST_END_SUBMODULE and COMP_SUBMODULE.
    * parse.h : Add COMP_SUBMODULE.
    * primary.c (match_variable): Add COMP_SUBMODULE.
    * resolve.c (compare_fsyms): New function to compare the dummy
    characteristics of a module procedure with its interface.
    (resolve_fl_procedure): Compare the procedure, result and dummy
    characteristics of a module_procedure with its interface, using
    'compare_fsyms' for the dummy arguments.
    * symbol.c (gfc_add_procedure): Suppress the check for existing
    procedures in the case of a module procedure.
    (gfc_add_explicit_interface): Skip checks that must fail for
    module procedures.
    (gfc_add_type): Allow a new type to be added to module
    procedures, their results or their dummy arguments.
    * trans-decl.c (gfc_sym_mangled_function_id): Module procedures
    must always have their names mangled as if they are symbols
    coming from a declaration in a module.
    (gfc_finish_var_decl): Symbols with the 'used_in_submodule' bit
    set are set DECL_EXTERNAL as if they were use associated.

2015-06-22  Paul Thomas  <pault@gcc.gnu.org>

    PR fortran/52846
    * gfortran.dg/submodule_1.f90: New test
    * gfortran.dg/submodule_2.f90: New test
    * gfortran.dg/submodule_3.f90: New test
    * gfortran.dg/submodule_4.f90: New test
    * gfortran.dg/submodule_5.f90: New test
-------------- next part --------------
Index: gcc/fortran/decl.c
===================================================================
*** gcc/fortran/decl.c	(revision 224724)
--- gcc/fortran/decl.c	(working copy)
*************** get_proc_name (const char *name, gfc_sym
*** 903,909 ****
  
    sym = *result;
  
!   if (sym && !sym->gfc_new && gfc_current_state () != COMP_INTERFACE)
      {
        /* Trap another encompassed procedure with the same name.  All
  	 these conditions are necessary to avoid picking up an entry
--- 903,937 ----
  
    sym = *result;
  
!   if (sym->attr.module_procedure
!       && sym->attr.if_source == IFSRC_IFBODY)
!     {
!       /* Create a partially populated interface symbol to carry the
! 	 characteristics of the procedure and the result.  */
!       sym->ts.interface = gfc_new_symbol (name, sym->ns);
!       gfc_add_type (sym->ts.interface, &(sym->ts),
! 		    &gfc_current_locus);
!       gfc_copy_attr (&sym->ts.interface->attr, &sym->attr, NULL);
!       if (sym->attr.dimension)
! 	sym->ts.interface->as = gfc_copy_array_spec (sym->as);
! 
!       /* Ideally, at this point, a copy would be made of the formal
! 	 arguments and their namespace. However, this does not appear
! 	 to be necessary, albeit at the expense of not being able to
! 	 use gfc_compare_interfaces directly.  */
! 
!       if (sym->result && sym->result != sym)
! 	{
! 	  sym->ts.interface->result = sym->result;
! 	  sym->result = NULL;
! 	}
!       else if (sym->result)
! 	{
! 	  sym->ts.interface->result = sym->ts.interface;
! 	}
!     }
!   else if (sym && !sym->gfc_new
! 	   && gfc_current_state () != COMP_INTERFACE)
      {
        /* Trap another encompassed procedure with the same name.  All
  	 these conditions are necessary to avoid picking up an entry
*************** match_attr_spec (void)
*** 3925,3931 ****
      }
  
    /* Since Fortran 2008 module variables implicitly have the SAVE attribute.  */
!   if (gfc_current_state () == COMP_MODULE && !current_attr.save
        && (gfc_option.allow_std & GFC_STD_F2008) != 0)
      current_attr.save = SAVE_IMPLICIT;
  
--- 3953,3961 ----
      }
  
    /* Since Fortran 2008 module variables implicitly have the SAVE attribute.  */
!   if ((gfc_current_state () == COMP_MODULE
!        || gfc_current_state () == COMP_SUBMODULE)
!       && !current_attr.save
        && (gfc_option.allow_std & GFC_STD_F2008) != 0)
      current_attr.save = SAVE_IMPLICIT;
  
*************** gfc_match_prefix (gfc_typespec *ts)
*** 4513,4518 ****
--- 4543,4564 ----
  
    /* At this point, the next item is not a prefix.  */
    gcc_assert (gfc_matching_prefix);
+ 
+   /* MODULE should be the last prefix before FUNCTION or SUBROUTINE.
+      Since this is a prefix like PURE, ELEMENTAL, etc., having a
+      corresponding attribute seems natural and distinguishes these
+      procedures from procedure types of PROC_MODULE, which these are
+      as well.  */
+   if ((gfc_current_state () == COMP_INTERFACE
+        || gfc_current_state () == COMP_CONTAINS)
+       && gfc_match ("module% ") == MATCH_YES)
+     {
+       if (!gfc_notify_std (GFC_STD_F2008, "MODULE prefix at %C"))
+ 	goto error;
+       else
+ 	current_attr.module_procedure = 1;
+     }
+ 
    gfc_matching_prefix = false;
    return MATCH_YES;
  
*************** gfc_match_formal_arglist (gfc_symbol *pr
*** 4550,4558 ****
--- 4596,4619 ----
    char name[GFC_MAX_SYMBOL_LEN + 1];
    gfc_symbol *sym;
    match m;
+   gfc_formal_arglist *formal = NULL;
  
    head = tail = NULL;
  
+   /* Keep the interface formal argument list and null it so that the
+      matching for the new declaration can be done.  The numbers and
+      names of the arguments are checked here. The interface formal
+      arguments are retained in formal_arglist and the characteristics
+      are compared in resolve.c(resolve_fl_procedure).  See the remark
+      in get_proc_name about the eventual need to copy the formal_arglist
+      and populate the formal namespace of the interface symbol.  */
+   if (progname->attr.module_procedure
+       && progname->attr.host_assoc)
+     {
+       formal = progname->formal;
+       progname->formal = NULL;
+     }
+ 
    if (gfc_match_char ('(') != MATCH_YES)
      {
        if (null_flag)
*************** ok:
*** 4658,4663 ****
--- 4719,4742 ----
        goto cleanup;
      }
  
+   if (formal)
+     {
+       for (p = formal, q = head; p && q; p = p->next, q = q->next)
+ 	{
+ 	  if ((p->next != NULL && q->next == NULL)
+ 	      || (p->next == NULL && q->next != NULL))
+ 	    gfc_error_now ("Mismatch in number of MODULE PROCEDURE "
+ 		           "formal arguments at %C");
+ 	  else if ((p->sym == NULL && q->sym == NULL)
+ 		    || strcmp (p->sym->name, q->sym->name) == 0)
+ 	    continue;
+ 	  else
+ 	    gfc_error_now ("Mismatch in MODULE PROCEDURE formal "
+ 			   "argument names (%s/%s) at %C",
+ 			   p->sym->name, q->sym->name);
+ 	}
+     }
+ 
    return MATCH_YES;
  
  cleanup:
*************** gfc_match_procedure (void)
*** 5271,5276 ****
--- 5350,5356 ----
      case COMP_NONE:
      case COMP_PROGRAM:
      case COMP_MODULE:
+     case COMP_SUBMODULE:
      case COMP_SUBROUTINE:
      case COMP_FUNCTION:
      case COMP_BLOCK:
*************** do_warn_intrinsic_shadow (const gfc_symb
*** 5309,5315 ****
    bool in_module;
  
    in_module = (gfc_state_stack->previous
! 	       && gfc_state_stack->previous->state == COMP_MODULE);
  
    gfc_warn_intrinsic_shadow (sym, in_module, func);
  }
--- 5389,5396 ----
    bool in_module;
  
    in_module = (gfc_state_stack->previous
! 	       && (gfc_state_stack->previous->state == COMP_MODULE
! 		   || gfc_state_stack->previous->state == COMP_SUBMODULE));
  
    gfc_warn_intrinsic_shadow (sym, in_module, func);
  }
*************** gfc_match_function_decl (void)
*** 5348,5359 ****
--- 5429,5444 ----
        gfc_current_locus = old_loc;
        return MATCH_NO;
      }
+ 
    if (get_proc_name (name, &sym, false))
      return MATCH_ERROR;
  
    if (add_hidden_procptr_result (sym))
      sym = sym->result;
  
+   if (current_attr.module_procedure)
+     sym->attr.module_procedure = 1;
+ 
    gfc_new_block = sym;
  
    m = gfc_match_formal_arglist (sym, 0, 0);
*************** gfc_match_entry (void)
*** 5547,5552 ****
--- 5632,5640 ----
  	  case COMP_MODULE:
  	    gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
  	    break;
+ 	  case COMP_SUBMODULE:
+ 	    gfc_error ("ENTRY statement at %C cannot appear within a SUBMODULE");
+ 	    break;
  	  case COMP_BLOCK_DATA:
  	    gfc_error ("ENTRY statement at %C cannot appear within "
  		       "a BLOCK DATA");
*************** gfc_match_subroutine (void)
*** 5791,5796 ****
--- 5879,5887 ----
       the symbol existed before.  */
    sym->declared_at = gfc_current_locus;
  
+   if (current_attr.module_procedure)
+     sym->attr.module_procedure = 1;
+ 
    if (add_hidden_procptr_result (sym))
      sym = sym->result;
  
*************** gfc_match_end (gfc_statement *st)
*** 6175,6180 ****
--- 6266,6277 ----
        eos_ok = 1;
        break;
  
+     case COMP_SUBMODULE:
+       *st = ST_END_SUBMODULE;
+       target = " submodule";
+       eos_ok = 1;
+       break;
+ 
      case COMP_INTERFACE:
        *st = ST_END_INTERFACE;
        target = " interface";
*************** syntax:
*** 7417,7422 ****
--- 7514,7625 ----
  }
  
  
+ /* Match a module procedure statement in a submodule.  */
+ 
+ match
+ gfc_match_submod_proc (void)
+ {
+   char name[GFC_MAX_SYMBOL_LEN + 1];
+   gfc_symbol *sym, *fsym;
+   match m;
+   gfc_formal_arglist *formal, *head, *tail;
+   int rc;
+ 
+   if (gfc_current_state () != COMP_CONTAINS
+       || !(gfc_state_stack->previous
+ 	   && gfc_state_stack->previous->state == COMP_SUBMODULE))
+     return MATCH_NO;
+ 
+   m = gfc_match (" module% procedure% %n", name);
+   if (m != MATCH_YES)
+     return m;
+ 
+   if (!gfc_notify_std (GFC_STD_F2008, "MODULE PROCEDURE declaration "
+ 				      "at %C"))
+     return MATCH_ERROR;
+ 
+   if (get_proc_name (name, &sym, false))
+     return MATCH_ERROR;
+ 
+   if (sym->ts.interface->attr.function
+       && sym->ts.interface->result
+       && sym->ts.interface->result != sym->ts.interface)
+     sym->result= sym->ts.interface->result;
+   else if (sym->ts.interface->attr.function
+ 	   && sym->ts.interface->result)
+     sym->result = sym;
+ 
+   /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
+      the symbol existed before.  */
+   sym->declared_at = gfc_current_locus;
+ 
+   if (!sym->attr.module_procedure)
+     return MATCH_ERROR;
+ 
+   /* Change from IFSRC_IFBODY coming from the interface declaration.  */
+   sym->attr.if_source = IFSRC_DECL;
+ 
+   gfc_new_block = sym;
+ 
+   /* Make a new formal arglist with the symbols in the procedure
+       namespace.  */
+   head = tail = NULL;
+   for (formal = sym->formal; formal && formal->sym; formal = formal->next)
+     {
+       if (formal == sym->formal)
+ 	head = tail = gfc_get_formal_arglist ();
+       else
+ 	{
+ 	  tail->next = gfc_get_formal_arglist ();
+ 	  tail = tail->next;
+ 	}
+ 
+       rc = gfc_get_symbol (formal->sym->name, NULL, &fsym);
+       if (rc)
+ 	goto cleanup;
+ 
+       if (!gfc_add_type (fsym, &(formal->sym->ts), &gfc_current_locus))
+ 	  goto cleanup;
+ 
+       if (!gfc_copy_attr (&(fsym->attr), &(formal->sym->attr),
+ 	  &gfc_current_locus))
+ 	goto cleanup;
+ 
+       if (fsym->attr.dimension)
+ 	fsym->as = gfc_copy_array_spec (formal->sym->as);
+ 
+       fsym->attr.class_ok = formal->sym->attr.class_ok;
+ 
+       if (fsym != NULL
+ 	  && (!gfc_add_dummy(&fsym->attr, fsym->name, NULL)
+ 	      || !gfc_missing_attr (&fsym->attr, NULL)))
+ 	goto cleanup;
+ 
+       tail->sym = fsym;
+       gfc_set_sym_referenced (fsym);
+     }
+ 
+   /* The dummy symbols get cleaned up, when the formal_namespace of the
+      interface declaration is cleared.  This allows us to add the
+      explicit interface as is done for other type of procedure.  */
+   if (!gfc_add_explicit_interface (sym, IFSRC_DECL, head,
+ 				   &gfc_current_locus))
+     return MATCH_ERROR;
+ 
+   if (gfc_match_eos () != MATCH_YES)
+     {
+       gfc_syntax_error (ST_MODULE_PROC);
+       return MATCH_ERROR;
+     }
+ 
+   return MATCH_YES;
+ 
+ cleanup:
+   gfc_free_formal_arglist (head);
+   return MATCH_ERROR;
+ }
+ 
+ 
  /* Match a module procedure statement.  Note that we have to modify
     symbols in the parent's namespace because the current one was there
     to receive symbols that are in an interface's formal argument list.  */
Index: gcc/fortran/gfortran.h
===================================================================
*** gcc/fortran/gfortran.h	(revision 224724)
--- gcc/fortran/gfortran.h	(working copy)
*************** typedef enum
*** 201,219 ****
    ST_CALL, ST_CASE, ST_CLOSE, ST_COMMON, ST_CONTINUE, ST_CONTAINS, ST_CYCLE,
    ST_DATA, ST_DATA_DECL, ST_DEALLOCATE, ST_DO, ST_ELSE, ST_ELSEIF,
    ST_ELSEWHERE, ST_END_ASSOCIATE, ST_END_BLOCK, ST_END_BLOCK_DATA,
!   ST_ENDDO, ST_IMPLIED_ENDDO,
!   ST_END_FILE, ST_FINAL, ST_FLUSH, ST_END_FORALL, ST_END_FUNCTION, ST_ENDIF,
!   ST_END_INTERFACE, ST_END_MODULE, ST_END_PROGRAM, ST_END_SELECT,
!   ST_END_SUBROUTINE, ST_END_WHERE, ST_END_TYPE, ST_ENTRY, ST_EQUIVALENCE,
!   ST_ERROR_STOP, ST_EXIT, ST_FORALL, ST_FORALL_BLOCK, ST_FORMAT, ST_FUNCTION,
!   ST_GOTO, ST_IF_BLOCK, ST_IMPLICIT, ST_IMPLICIT_NONE, ST_IMPORT,
!   ST_INQUIRE, ST_INTERFACE, ST_SYNC_ALL, ST_SYNC_MEMORY, ST_SYNC_IMAGES,
!   ST_PARAMETER, ST_MODULE, ST_MODULE_PROC, ST_NAMELIST, ST_NULLIFY, ST_OPEN,
!   ST_PAUSE, ST_PRIVATE, ST_PROGRAM, ST_PUBLIC, ST_READ, ST_RETURN, ST_REWIND,
!   ST_STOP, ST_SUBROUTINE, ST_TYPE, ST_USE, ST_WHERE_BLOCK, ST_WHERE, ST_WAIT,
!   ST_WRITE, ST_ASSIGNMENT, ST_POINTER_ASSIGNMENT, ST_SELECT_CASE, ST_SEQUENCE,
!   ST_SIMPLE_IF, ST_STATEMENT_FUNCTION, ST_DERIVED_DECL, ST_LABEL_ASSIGNMENT,
!   ST_ENUM, ST_ENUMERATOR, ST_END_ENUM, ST_SELECT_TYPE, ST_TYPE_IS, ST_CLASS_IS,
    ST_OACC_PARALLEL_LOOP, ST_OACC_END_PARALLEL_LOOP, ST_OACC_PARALLEL,
    ST_OACC_END_PARALLEL, ST_OACC_KERNELS, ST_OACC_END_KERNELS, ST_OACC_DATA,
    ST_OACC_END_DATA, ST_OACC_HOST_DATA, ST_OACC_END_HOST_DATA, ST_OACC_LOOP,
--- 201,219 ----
    ST_CALL, ST_CASE, ST_CLOSE, ST_COMMON, ST_CONTINUE, ST_CONTAINS, ST_CYCLE,
    ST_DATA, ST_DATA_DECL, ST_DEALLOCATE, ST_DO, ST_ELSE, ST_ELSEIF,
    ST_ELSEWHERE, ST_END_ASSOCIATE, ST_END_BLOCK, ST_END_BLOCK_DATA,
!   ST_ENDDO, ST_IMPLIED_ENDDO, ST_END_FILE, ST_FINAL, ST_FLUSH, ST_END_FORALL,
!   ST_END_FUNCTION, ST_ENDIF, ST_END_INTERFACE, ST_END_MODULE, ST_END_SUBMODULE,
!   ST_END_PROGRAM, ST_END_SELECT, ST_END_SUBROUTINE, ST_END_WHERE, ST_END_TYPE,
!   ST_ENTRY, ST_EQUIVALENCE, ST_ERROR_STOP, ST_EXIT, ST_FORALL, ST_FORALL_BLOCK,
!   ST_FORMAT, ST_FUNCTION, ST_GOTO, ST_IF_BLOCK, ST_IMPLICIT, ST_IMPLICIT_NONE,
!   ST_IMPORT, ST_INQUIRE, ST_INTERFACE, ST_SYNC_ALL, ST_SYNC_MEMORY,
!   ST_SYNC_IMAGES, ST_PARAMETER, ST_MODULE, ST_SUBMODULE, ST_MODULE_PROC,
!   ST_NAMELIST, ST_NULLIFY, ST_OPEN, ST_PAUSE, ST_PRIVATE, ST_PROGRAM, ST_PUBLIC,
!   ST_READ, ST_RETURN, ST_REWIND, ST_STOP, ST_SUBROUTINE, ST_TYPE, ST_USE,
!   ST_WHERE_BLOCK, ST_WHERE, ST_WAIT, ST_WRITE, ST_ASSIGNMENT,
!   ST_POINTER_ASSIGNMENT, ST_SELECT_CASE, ST_SEQUENCE, ST_SIMPLE_IF,
!   ST_STATEMENT_FUNCTION, ST_DERIVED_DECL, ST_LABEL_ASSIGNMENT, ST_ENUM,
!   ST_ENUMERATOR, ST_END_ENUM, ST_SELECT_TYPE, ST_TYPE_IS, ST_CLASS_IS,
    ST_OACC_PARALLEL_LOOP, ST_OACC_END_PARALLEL_LOOP, ST_OACC_PARALLEL,
    ST_OACC_END_PARALLEL, ST_OACC_KERNELS, ST_OACC_END_KERNELS, ST_OACC_DATA,
    ST_OACC_END_DATA, ST_OACC_HOST_DATA, ST_OACC_END_HOST_DATA, ST_OACC_LOOP,
*************** typedef struct
*** 751,756 ****
--- 751,759 ----
    unsigned data:1,		/* Symbol is named in a DATA statement.  */
      is_protected:1,		/* Symbol has been marked as protected.  */
      use_assoc:1,		/* Symbol has been use-associated.  */
+     used_in_submodule:1,	/* Symbol has been use-associated in a
+ 				   submodule. Needed since these entities must
+ 				   be set host associated to be compliant.  */
      use_only:1,			/* Symbol has been use-associated, with ONLY.  */
      use_rename:1,		/* Symbol has been use-associated and renamed.  */
      imported:1,			/* Symbol has been associated by IMPORT.  */
*************** typedef struct
*** 778,783 ****
--- 781,790 ----
    /* Function/subroutine attributes */
    unsigned sequence:1, elemental:1, pure:1, recursive:1;
    unsigned unmaskable:1, masked:1, contained:1, mod_proc:1, abstract:1;
+   /* Set if this is a module function or subroutine. Note that it is an
+      attribute because it appears as a prefix in the declaration like
+      PURE, etc..  */
+   unsigned module_procedure:1;
  
    /* Set if a (public) symbol [e.g. generic name] exposes this symbol,
       which is relevant for private module procedures.  */
*************** bool gfc_ref_dimen_size (gfc_array_ref *
*** 3087,3092 ****
--- 3094,3103 ----
  void gfc_free_interface (gfc_interface *);
  int gfc_compare_derived_types (gfc_symbol *, gfc_symbol *);
  int gfc_compare_types (gfc_typespec *, gfc_typespec *);
+ bool gfc_check_dummy_characteristics (gfc_symbol *, gfc_symbol *,
+ 				      bool, char *, int);
+ bool gfc_check_result_characteristics (gfc_symbol *, gfc_symbol *,
+ 				       char *, int);
  int gfc_compare_interfaces (gfc_symbol*, gfc_symbol*, const char *, int, int,
  			    char *, int, const char *, const char *);
  void gfc_check_interfaces (gfc_namespace *);
Index: gcc/fortran/interface.c
===================================================================
*** gcc/fortran/interface.c	(revision 224724)
--- gcc/fortran/interface.c	(working copy)
*************** symbol_rank (gfc_symbol *sym)
*** 1066,1074 ****
  /* Check if the characteristics of two dummy arguments match,
     cf. F08:12.3.2.  */
  
! static bool
! check_dummy_characteristics (gfc_symbol *s1, gfc_symbol *s2,
! 			     bool type_must_agree, char *errmsg, int err_len)
  {
    if (s1 == NULL || s2 == NULL)
      return s1 == s2 ? true : false;
--- 1066,1075 ----
  /* Check if the characteristics of two dummy arguments match,
     cf. F08:12.3.2.  */
  
! bool
! gfc_check_dummy_characteristics (gfc_symbol *s1, gfc_symbol *s2,
! 				 bool type_must_agree, char *errmsg,
! 				 int err_len)
  {
    if (s1 == NULL || s2 == NULL)
      return s1 == s2 ? true : false;
*************** check_dummy_characteristics (gfc_symbol
*** 1275,1282 ****
  /* Check if the characteristics of two function results match,
     cf. F08:12.3.3.  */
  
! static bool
! check_result_characteristics (gfc_symbol *s1, gfc_symbol *s2,
  			      char *errmsg, int err_len)
  {
    gfc_symbol *r1, *r2;
--- 1276,1283 ----
  /* Check if the characteristics of two function results match,
     cf. F08:12.3.3.  */
  
! bool
! gfc_check_result_characteristics (gfc_symbol *s1, gfc_symbol *s2,
  				  char *errmsg, int err_len)
  {
    gfc_symbol *r1, *r2;
*************** gfc_compare_interfaces (gfc_symbol *s1,
*** 1472,1479 ****
        if (s1->attr.function && s2->attr.function)
  	{
  	  /* If both are functions, check result characteristics.  */
! 	  if (!check_result_characteristics (s1, s2, errmsg, err_len)
! 	      || !check_result_characteristics (s2, s1, errmsg, err_len))
  	    return 0;
  	}
  
--- 1473,1480 ----
        if (s1->attr.function && s2->attr.function)
  	{
  	  /* If both are functions, check result characteristics.  */
! 	  if (!gfc_check_result_characteristics (s1, s2, errmsg, err_len)
! 	      || !gfc_check_result_characteristics (s2, s1, errmsg, err_len))
  	    return 0;
  	}
  
*************** gfc_compare_interfaces (gfc_symbol *s1,
*** 1533,1539 ****
  	if (strict_flag)
  	  {
  	    /* Check all characteristics.  */
! 	    if (!check_dummy_characteristics (f1->sym, f2->sym, true, 
  					      errmsg, err_len))
  	      return 0;
  	  }
--- 1534,1540 ----
  	if (strict_flag)
  	  {
  	    /* Check all characteristics.  */
! 	    if (!gfc_check_dummy_characteristics (f1->sym, f2->sym, true,
  						  errmsg, err_len))
  	      return 0;
  	  }
*************** gfc_check_typebound_override (gfc_symtre
*** 4241,4248 ****
  	  return false;
  	}
  
!       if (!check_result_characteristics (proc_target, old_target, err, 
! 					 sizeof(err)))
  	{
  	  gfc_error ("Result mismatch for the overriding procedure "
  		     "%qs at %L: %s", proc->name, &where, err);
--- 4242,4249 ----
  	  return false;
  	}
  
!       if (!gfc_check_result_characteristics (proc_target, old_target,
! 					     err, sizeof(err)))
  	{
  	  gfc_error ("Result mismatch for the overriding procedure "
  		     "%qs at %L: %s", proc->name, &where, err);
*************** gfc_check_typebound_override (gfc_symtre
*** 4293,4299 ****
  	}
  
        check_type = proc_pass_arg != argpos && old_pass_arg != argpos;
!       if (!check_dummy_characteristics (proc_formal->sym, old_formal->sym, 
  					check_type, err, sizeof(err)))
  	{
  	  gfc_error ("Argument mismatch for the overriding procedure "
--- 4294,4300 ----
  	}
  
        check_type = proc_pass_arg != argpos && old_pass_arg != argpos;
!       if (!gfc_check_dummy_characteristics (proc_formal->sym, old_formal->sym,
  					    check_type, err, sizeof(err)))
  	{
  	  gfc_error ("Argument mismatch for the overriding procedure "
Index: gcc/fortran/match.h
===================================================================
*** gcc/fortran/match.h	(revision 224724)
--- gcc/fortran/match.h	(working copy)
*************** match gfc_match_generic (void);
*** 203,208 ****
--- 203,209 ----
  match gfc_match_function_decl (void);
  match gfc_match_entry (void);
  match gfc_match_subroutine (void);
+ match gfc_match_submod_proc (void);
  match gfc_match_derived_decl (void);
  match gfc_match_final_decl (void);
  
*************** match gfc_match_expr (gfc_expr **);
*** 291,296 ****
--- 292,298 ----
  
  /* module.c.  */
  match gfc_match_use (void);
+ match gfc_match_submodule (void);
  void gfc_use_modules (void);
  
  #endif  /* GFC_MATCH_H  */
Index: gcc/fortran/module.c
===================================================================
*** gcc/fortran/module.c	(revision 224724)
--- gcc/fortran/module.c	(working copy)
*************** cleanup:
*** 716,721 ****
--- 716,782 ----
  }
  
  
+ /* Match a SUBMODULE statement.  */
+ 
+ match
+ gfc_match_submodule (void)
+ {
+   match m;
+   char name[GFC_MAX_SYMBOL_LEN + 1];
+   gfc_use_list *use_list;
+ 
+   if (!gfc_notify_std (GFC_STD_F2008, "SUBMODULE declaration at %C"))
+     return MATCH_ERROR;
+ 
+   gfc_new_block = NULL;
+   gcc_assert (module_list == NULL);
+ 
+   if (gfc_match_char ('(') != MATCH_YES)
+     goto syntax;
+ 
+   while (1)
+     {
+       m = gfc_match (" %n", name);
+       if (m != MATCH_YES)
+ 	goto syntax;
+ 
+       use_list = gfc_get_use_list ();
+       use_list->module_name = gfc_get_string (name);
+       use_list->where = gfc_current_locus;
+ 
+       if (module_list)
+ 	{
+ 	  gfc_use_list *last = module_list;
+ 	  while (last->next)
+ 	    last = last->next;
+ 	  last->next = use_list;
+ 	}
+       else
+ 	module_list = use_list;
+ 
+       if (gfc_match_char (')') == MATCH_YES)
+ 	break;
+ 
+       if (gfc_match_char (':') != MATCH_YES)
+ 	goto syntax;
+     }
+ 
+   m = gfc_match (" %s%t", &gfc_new_block);
+   if (m != MATCH_YES)
+     goto syntax;
+ 
+   if (!gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
+ 		       gfc_new_block->name, NULL))
+     return MATCH_ERROR;
+ 
+   return MATCH_YES;
+ 
+ syntax:
+   gfc_error ("Syntax error in SUBMODULE statement at %C");
+   return MATCH_ERROR;
+ }
+ 
+ 
  /* Given a name and a number, inst, return the inst name
     under which to load this symbol. Returns NULL if this
     symbol shouldn't be loaded. If inst is zero, returns
*************** typedef enum
*** 1887,1893 ****
    AB_IS_CLASS, AB_PROCEDURE, AB_PROC_POINTER, AB_ASYNCHRONOUS, AB_CODIMENSION,
    AB_COARRAY_COMP, AB_VTYPE, AB_VTAB, AB_CONTIGUOUS, AB_CLASS_POINTER,
    AB_IMPLICIT_PURE, AB_ARTIFICIAL, AB_UNLIMITED_POLY, AB_OMP_DECLARE_TARGET,
!   AB_ARRAY_OUTER_DEPENDENCY
  }
  ab_attribute;
  
--- 1948,1954 ----
    AB_IS_CLASS, AB_PROCEDURE, AB_PROC_POINTER, AB_ASYNCHRONOUS, AB_CODIMENSION,
    AB_COARRAY_COMP, AB_VTYPE, AB_VTAB, AB_CONTIGUOUS, AB_CLASS_POINTER,
    AB_IMPLICIT_PURE, AB_ARTIFICIAL, AB_UNLIMITED_POLY, AB_OMP_DECLARE_TARGET,
!   AB_ARRAY_OUTER_DEPENDENCY, AB_MODULE_PROCEDURE
  }
  ab_attribute;
  
*************** static const mstring attr_bits[] =
*** 1944,1949 ****
--- 2005,2011 ----
      minit ("UNLIMITED_POLY", AB_UNLIMITED_POLY),
      minit ("OMP_DECLARE_TARGET", AB_OMP_DECLARE_TARGET),
      minit ("ARRAY_OUTER_DEPENDENCY", AB_ARRAY_OUTER_DEPENDENCY),
+     minit ("MODULE_PROCEDURE", AB_MODULE_PROCEDURE),
      minit (NULL, -1)
  };
  
*************** mio_symbol_attribute (symbol_attribute *
*** 2126,2131 ****
--- 2188,2195 ----
  	MIO_NAME (ab_attribute) (AB_OMP_DECLARE_TARGET, attr_bits);
        if (attr->array_outer_dependency)
  	MIO_NAME (ab_attribute) (AB_ARRAY_OUTER_DEPENDENCY, attr_bits);
+       if (attr->module_procedure)
+ 	MIO_NAME (ab_attribute) (AB_MODULE_PROCEDURE, attr_bits);
  
        mio_rparen ();
  
*************** mio_symbol_attribute (symbol_attribute *
*** 2295,2300 ****
--- 2359,2367 ----
  	    case AB_ARRAY_OUTER_DEPENDENCY:
  	      attr->array_outer_dependency =1;
  	      break;
+ 	    case AB_MODULE_PROCEDURE:
+ 	      attr->module_procedure =1;
+ 	      break;
  	    }
  	}
      }
*************** gfc_use_module (gfc_use_list *module)
*** 6757,6764 ****
  
    /* Make sure we're not reading the same module that we may be building.  */
    for (p = gfc_state_stack; p; p = p->previous)
!     if (p->state == COMP_MODULE && strcmp (p->sym->name, module_name) == 0)
!       gfc_fatal_error ("Can't USE the same module we're building!");
  
    init_pi_tree ();
    init_true_name_tree ();
--- 6824,6833 ----
  
    /* Make sure we're not reading the same module that we may be building.  */
    for (p = gfc_state_stack; p; p = p->previous)
!     if ((p->state == COMP_MODULE || p->state == COMP_SUBMODULE)
! 	 && strcmp (p->sym->name, module_name) == 0)
!       gfc_fatal_error ("Can't USE the same %smodule we're building!",
! 		       p->state == COMP_SUBMODULE ? "sub" : "");
  
    init_pi_tree ();
    init_true_name_tree ();
Index: gcc/fortran/parse.c
===================================================================
*** gcc/fortran/parse.c	(revision 224724)
--- gcc/fortran/parse.c	(working copy)
*************** decode_statement (void)
*** 369,374 ****
--- 369,384 ----
    gfc_undo_symbols ();
    gfc_current_locus = old_locus;
  
+   if (gfc_match_submod_proc () == MATCH_YES)
+     {
+       if (gfc_new_block->attr.subroutine)
+ 	return ST_SUBROUTINE;
+       else if (gfc_new_block->attr.function)
+ 	return ST_FUNCTION;
+     }
+   gfc_undo_symbols ();
+   gfc_current_locus = old_locus;
+ 
    /* Check for the IF, DO, SELECT, WHERE, FORALL, CRITICAL, BLOCK and ASSOCIATE
       statements, which might begin with a block label.  The match functions for
       these statements are unusual in that their keyword is not seen before
*************** decode_statement (void)
*** 522,527 ****
--- 532,538 ----
        match ("sequence", gfc_match_eos, ST_SEQUENCE);
        match ("stop", gfc_match_stop, ST_STOP);
        match ("save", gfc_match_save, ST_ATTR_DECL);
+       match ("submodule", gfc_match_submodule, ST_SUBMODULE);
        match ("sync all", gfc_match_sync_all, ST_SYNC_ALL);
        match ("sync images", gfc_match_sync_images, ST_SYNC_IMAGES);
        match ("sync memory", gfc_match_sync_memory, ST_SYNC_MEMORY);
*************** gfc_enclosing_unit (gfc_compile_state *
*** 1534,1541 ****
  
    for (p = gfc_state_stack; p; p = p->previous)
      if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
! 	|| p->state == COMP_MODULE || p->state == COMP_BLOCK_DATA
! 	|| p->state == COMP_PROGRAM)
        {
  
  	if (result != NULL)
--- 1545,1552 ----
  
    for (p = gfc_state_stack; p; p = p->previous)
      if (p->state == COMP_FUNCTION || p->state == COMP_SUBROUTINE
! 	|| p->state == COMP_MODULE || p->state == COMP_SUBMODULE
! 	|| p->state == COMP_BLOCK_DATA || p->state == COMP_PROGRAM)
        {
  
  	if (result != NULL)
*************** gfc_ascii_statement (gfc_statement st)
*** 1660,1665 ****
--- 1671,1679 ----
      case ST_END_MODULE:
        p = "END MODULE";
        break;
+     case ST_END_SUBMODULE:
+       p = "END SUBMODULE";
+       break;
      case ST_END_PROGRAM:
        p = "END PROGRAM";
        break;
*************** gfc_ascii_statement (gfc_statement st)
*** 1742,1747 ****
--- 1756,1764 ----
      case ST_MODULE:
        p = "MODULE";
        break;
+     case ST_SUBMODULE:
+       p = "SUBMODULE";
+       break;
      case ST_PAUSE:
        p = "PAUSE";
        break;
*************** accept_statement (gfc_statement st)
*** 2186,2191 ****
--- 2203,2209 ----
      case ST_FUNCTION:
      case ST_SUBROUTINE:
      case ST_MODULE:
+     case ST_SUBMODULE:
        gfc_current_ns->proc_name = gfc_new_block;
        break;
  
*************** declSt:
*** 3280,3286 ****
  	  break;
  
  	case ST_STATEMENT_FUNCTION:
! 	  if (gfc_current_state () == COMP_MODULE)
  	    {
  	      unexpected_statement (st);
  	      break;
--- 3298,3305 ----
  	  break;
  
  	case ST_STATEMENT_FUNCTION:
! 	  if (gfc_current_state () == COMP_MODULE
! 	      || gfc_current_state () == COMP_SUBMODULE)
  	    {
  	      unexpected_statement (st);
  	      break;
*************** parse_contained (int module)
*** 4903,4908 ****
--- 4922,4928 ----
  	/* These statements are associated with the end of the host unit.  */
  	case ST_END_FUNCTION:
  	case ST_END_MODULE:
+ 	case ST_END_SUBMODULE:
  	case ST_END_PROGRAM:
  	case ST_END_SUBROUTINE:
  	  accept_statement (st);
*************** parse_contained (int module)
*** 4919,4925 ****
  	}
      }
    while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
! 	 && st != ST_END_MODULE && st != ST_END_PROGRAM);
  
    /* The first namespace in the list is guaranteed to not have
       anything (worthwhile) in it.  */
--- 4939,4946 ----
  	}
      }
    while (st != ST_END_FUNCTION && st != ST_END_SUBROUTINE
! 	 && st != ST_END_MODULE && st != ST_END_SUBMODULE
! 	 && st != ST_END_PROGRAM);
  
    /* The first namespace in the list is guaranteed to not have
       anything (worthwhile) in it.  */
*************** contains:
*** 5006,5012 ****
      if (p->state == COMP_CONTAINS)
        n++;
  
!   if (gfc_find_state (COMP_MODULE) == true)
      n--;
  
    if (n > 0)
--- 5027,5034 ----
      if (p->state == COMP_CONTAINS)
        n++;
  
!   if (gfc_find_state (COMP_MODULE) == true
!       || gfc_find_state (COMP_SUBMODULE) == true)
      n--;
  
    if (n > 0)
*************** done:
*** 5024,5029 ****
--- 5046,5052 ----
    gfc_current_ns->code = gfc_state_stack->head;
    if (gfc_state_stack->state == COMP_PROGRAM
        || gfc_state_stack->state == COMP_MODULE
+       || gfc_state_stack->state == COMP_SUBMODULE
        || gfc_state_stack->state == COMP_SUBROUTINE
        || gfc_state_stack->state == COMP_FUNCTION
        || gfc_state_stack->state == COMP_BLOCK)
*************** parse_block_data (void)
*** 5127,5132 ****
--- 5150,5185 ----
  }
  
  
+ /* Following the association of the ancestor (sub)module symbols, they
+    must be set host rather than use associated and all must be public.
+    They are flagged up by 'used_in_submodule' so that they can be set
+    DECL_EXTERNAL in trans_decl.c(gfc_finish_var_decl).  Otherwise the
+    linker chokes on multiple symbol definitions.  */
+ 
+ static void
+ set_syms_host_assoc (gfc_symbol *sym)
+ {
+   gfc_component *c;
+ 
+   if (sym == NULL)
+     return;
+ 
+   if (sym->attr.module_procedure)
+     sym->attr.external = 0;
+ 
+ /*  sym->attr.access = ACCESS_PUBLIC;  */
+ 
+   sym->attr.use_assoc = 0;
+   sym->attr.host_assoc = 1;
+   sym->attr.used_in_submodule =1;
+ 
+   if (sym->attr.flavor == FL_DERIVED)
+     {
+       for (c = sym->components; c; c = c->next)
+ 	c->attr.access = ACCESS_PUBLIC;
+     }
+ }
+ 
  /* Parse a module subprogram.  */
  
  static void
*************** parse_module (void)
*** 5146,5151 ****
--- 5199,5213 ----
        s->defined = 1;
      }
  
+   /* Something is nulling the module_list after this point. This is good
+      since it allows us to 'USE' the parent modules that the submodule
+      inherits and to set (most) of the symbols as host associated.  */
+   if (gfc_current_state () == COMP_SUBMODULE)
+     {
+       use_modules ();
+       gfc_traverse_ns (gfc_current_ns, set_syms_host_assoc);
+     }
+ 
    st = parse_spec (ST_NONE);
  
    error = false;
*************** loop:
*** 5160,5165 ****
--- 5222,5228 ----
        break;
  
      case ST_END_MODULE:
+     case ST_END_SUBMODULE:
        accept_statement (st);
        break;
  
*************** loop:
*** 5455,5460 ****
--- 5518,5531 ----
        parse_module ();
        break;
  
+     case ST_SUBMODULE:
+       push_state (&s, COMP_SUBMODULE, gfc_new_block);
+       accept_statement (st);
+ 
+       gfc_get_errors (NULL, &errors_before);
+       parse_module ();
+       break;
+ 
      /* Anything else starts a nameless main program block.  */
      default:
        if (seen_program)
*************** loop:
*** 5479,5485 ****
      gfc_dump_parse_tree (gfc_current_ns, stdout);
  
    gfc_get_errors (NULL, &errors);
!   if (s.state == COMP_MODULE)
      {
        gfc_dump_module (s.sym->name, errors_before == errors);
        gfc_current_ns->derived_types = gfc_derived_types;
--- 5550,5556 ----
      gfc_dump_parse_tree (gfc_current_ns, stdout);
  
    gfc_get_errors (NULL, &errors);
!   if (s.state == COMP_MODULE || s.state == COMP_SUBMODULE)
      {
        gfc_dump_module (s.sym->name, errors_before == errors);
        gfc_current_ns->derived_types = gfc_derived_types;
Index: gcc/fortran/parse.h
===================================================================
*** gcc/fortran/parse.h	(revision 224724)
--- gcc/fortran/parse.h	(working copy)
*************** along with GCC; see the file COPYING3.
*** 25,33 ****
  /* Enum for what the compiler is currently doing.  */
  typedef enum
  {
!   COMP_NONE, COMP_PROGRAM, COMP_MODULE, COMP_SUBROUTINE, COMP_FUNCTION,
!   COMP_BLOCK_DATA, COMP_INTERFACE, COMP_DERIVED, COMP_DERIVED_CONTAINS,
!   COMP_BLOCK, COMP_ASSOCIATE, COMP_IF,
    COMP_DO, COMP_SELECT, COMP_FORALL, COMP_WHERE, COMP_CONTAINS, COMP_ENUM,
    COMP_SELECT_TYPE, COMP_OMP_STRUCTURED_BLOCK, COMP_CRITICAL, COMP_DO_CONCURRENT
  }
--- 25,33 ----
  /* Enum for what the compiler is currently doing.  */
  typedef enum
  {
!   COMP_NONE, COMP_PROGRAM, COMP_MODULE, COMP_SUBMODULE, COMP_SUBROUTINE,
!   COMP_FUNCTION, COMP_BLOCK_DATA, COMP_INTERFACE, COMP_DERIVED,
!   COMP_DERIVED_CONTAINS, COMP_BLOCK, COMP_ASSOCIATE, COMP_IF,
    COMP_DO, COMP_SELECT, COMP_FORALL, COMP_WHERE, COMP_CONTAINS, COMP_ENUM,
    COMP_SELECT_TYPE, COMP_OMP_STRUCTURED_BLOCK, COMP_CRITICAL, COMP_DO_CONCURRENT
  }
Index: gcc/fortran/primary.c
===================================================================
*** gcc/fortran/primary.c	(revision 224724)
--- gcc/fortran/primary.c	(working copy)
*************** match_variable (gfc_expr **result, int e
*** 3264,3269 ****
--- 3264,3270 ----
       of keywords, such as 'end', being turned into variables by
       failed matching to assignments for, e.g., END INTERFACE.  */
    if (gfc_current_state () == COMP_MODULE
+       || gfc_current_state () == COMP_SUBMODULE
        || gfc_current_state () == COMP_INTERFACE
        || gfc_current_state () == COMP_CONTAINS)
      host_flag = 0;
Index: gcc/fortran/resolve.c
===================================================================
*** gcc/fortran/resolve.c	(revision 224724)
--- gcc/fortran/resolve.c	(working copy)
*************** no_init_error:
*** 11301,11306 ****
--- 11301,11332 ----
  }
  
  
+ /* Compare the dummy characteristics of a module procedure interface
+    declaration with the corresponding declaration in a submodule.  */
+ static gfc_formal_arglist *new_formal;
+ static char errmsg[200];
+ 
+ static void
+ compare_fsyms (gfc_symbol *sym)
+ {
+   gfc_symbol *fsym;
+ 
+   if (sym == NULL || new_formal == NULL)
+     return;
+ 
+   fsym = new_formal->sym;
+ 
+   if (sym == fsym)
+     return;
+ 
+   if (strcmp (sym->name, fsym->name) == 0)
+     {
+       if (!gfc_check_dummy_characteristics (fsym, sym, true, errmsg, 200))
+ 	gfc_error ("%s at %L", errmsg, &fsym->declared_at);
+     }
+ }
+ 
+ 
  /* Resolve a procedure.  */
  
  static bool
*************** resolve_fl_procedure (gfc_symbol *sym, i
*** 11565,11570 ****
--- 11591,11661 ----
    if (sym->attr.if_source != IFSRC_DECL)
      sym->attr.array_outer_dependency = 1;
  
+   /* Compare the characteristics of a module procedure with the
+      interface declaration. Ideally this would be done with
+      gfc_compare_interfaces but, at present, the formal interface
+      cannot be copied to the ts.interface.  */
+   if (sym->attr.module_procedure
+       && sym->attr.if_source == IFSRC_DECL)
+     {
+       gfc_symbol *iface;
+ 
+       /* Stop the dummy characteristics test from using the interface
+ 	 symbol instead of 'sym'.  */
+       iface = sym->ts.interface;
+       sym->ts.interface = NULL;
+ 
+       if (iface == NULL)
+ 	goto check_formal;
+ 
+       /* Check the procedure characteristics.  */
+       if (sym->attr.pure != iface->attr.pure)
+ 	{
+ 	  gfc_error ("Mismatch in PURE attribute between MODULE "
+ 		     "PROCEDURE at %L and its interface in %s",
+ 		     &sym->declared_at, iface->module);
+ 	  return false;
+ 	}
+ 
+       if (sym->attr.elemental != iface->attr.elemental)
+ 	{
+ 	  gfc_error ("Mismatch in ELEMENTAL attribute between MODULE "
+ 		     "PROCEDURE at %L and its interface in %s",
+ 		     &sym->declared_at, iface->module);
+ 	  return false;
+ 	}
+ 
+       if (sym->attr.recursive != iface->attr.recursive)
+ 	{
+ 	  gfc_error ("Mismatch in RECURSIVE attribute between MODULE "
+ 		     "PROCEDURE at %L and its interface in %s",
+ 		     &sym->declared_at, iface->module);
+ 	  return false;
+ 	}
+ 
+       /* Check the result characteristics.  */
+       if (!gfc_check_result_characteristics (sym, iface, errmsg, 200))
+ 	{
+ 	  gfc_error ("%s between the MODULE PROCEDURE declaration "
+ 		     "in module %s and the declaration at %L in "
+ 		     "SUBMODULE %s", errmsg, iface->module,
+ 		     &sym->declared_at, sym->ns->proc_name->name);
+ 	  return false;
+ 	}
+ 
+ check_formal:
+       /* Check the charcateristics of the formal arguments.  */
+       if (sym->formal && sym->formal_ns)
+ 	{
+ 	  for (arg = sym->formal; arg && arg->sym; arg = arg->next)
+ 	    {
+ 	      new_formal = arg;
+ 	      gfc_traverse_ns (sym->formal_ns, compare_fsyms);
+ 	    }
+ 	}
+ 
+       sym->ts.interface = iface;
+     }
    return true;
  }
  
Index: gcc/fortran/symbol.c
===================================================================
*** gcc/fortran/symbol.c	(revision 224724)
--- gcc/fortran/symbol.c	(working copy)
*************** gfc_add_procedure (symbol_attribute *att
*** 1539,1545 ****
    if (where == NULL)
      where = &gfc_current_locus;
  
!   if (attr->proc != PROC_UNKNOWN)
      {
        gfc_error ("%s procedure at %L is already declared as %s procedure",
  		 gfc_code2string (procedures, t), where,
--- 1539,1545 ----
    if (where == NULL)
      where = &gfc_current_locus;
  
!   if (attr->proc != PROC_UNKNOWN && !attr->module_procedure)
      {
        gfc_error ("%s procedure at %L is already declared as %s procedure",
  		 gfc_code2string (procedures, t), where,
*************** bool
*** 1655,1664 ****
  gfc_add_explicit_interface (gfc_symbol *sym, ifsrc source,
  			    gfc_formal_arglist * formal, locus *where)
  {
- 
    if (check_used (&sym->attr, sym->name, where))
      return false;
  
    if (where == NULL)
      where = &gfc_current_locus;
  
--- 1655,1669 ----
  gfc_add_explicit_interface (gfc_symbol *sym, ifsrc source,
  			    gfc_formal_arglist * formal, locus *where)
  {
    if (check_used (&sym->attr, sym->name, where))
      return false;
  
+   /* Skip the following checks in the case of a module_procedures in a
+      submodule since they will manifestly fail.  */
+   if (sym->attr.module_procedure == 1
+       && source == IFSRC_DECL)
+     goto finish;
+ 
    if (where == NULL)
      where = &gfc_current_locus;
  
*************** gfc_add_explicit_interface (gfc_symbol *
*** 1677,1682 ****
--- 1682,1688 ----
        return false;
      }
  
+ finish:
    sym->formal = formal;
    sym->attr.if_source = source;
  
*************** gfc_add_type (gfc_symbol *sym, gfc_types
*** 1703,1709 ****
    if (sym->attr.result && type == BT_UNKNOWN && sym->ns->proc_name)
      type = sym->ns->proc_name->ts.type;
  
!   if (type != BT_UNKNOWN && !(sym->attr.function && sym->attr.implicit_type))
      {
        if (sym->attr.use_assoc)
  	gfc_error ("Symbol %qs at %L conflicts with symbol from module %qs, "
--- 1709,1718 ----
    if (sym->attr.result && type == BT_UNKNOWN && sym->ns->proc_name)
      type = sym->ns->proc_name->ts.type;
  
!   if (type != BT_UNKNOWN && !(sym->attr.function && sym->attr.implicit_type)
!       && !(gfc_state_stack->previous && gfc_state_stack->previous->previous
! 	   && gfc_state_stack->previous->previous->state == COMP_SUBMODULE)
!       && !sym->attr.module_procedure)
      {
        if (sym->attr.use_assoc)
  	gfc_error ("Symbol %qs at %L conflicts with symbol from module %qs, "
Index: gcc/fortran/trans-decl.c
===================================================================
*** gcc/fortran/trans-decl.c	(revision 224724)
--- gcc/fortran/trans-decl.c	(working copy)
*************** gfc_sym_mangled_function_id (gfc_symbol
*** 379,387 ****
      /* use the binding label rather than the mangled name */
      return get_identifier (sym->binding_label);
  
!   if (sym->module == NULL || sym->attr.proc == PROC_EXTERNAL
        || (sym->module != NULL && (sym->attr.external
  	    || sym->attr.if_source == IFSRC_IFBODY)))
      {
        /* Main program is mangled into MAIN__.  */
        if (sym->attr.is_main_program)
--- 379,388 ----
      /* use the binding label rather than the mangled name */
      return get_identifier (sym->binding_label);
  
!   if ((sym->module == NULL || sym->attr.proc == PROC_EXTERNAL
        || (sym->module != NULL && (sym->attr.external
  	    || sym->attr.if_source == IFSRC_IFBODY)))
+       && !sym->attr.module_procedure)
      {
        /* Main program is mangled into MAIN__.  */
        if (sym->attr.is_main_program)
*************** gfc_finish_var_decl (tree decl, gfc_symb
*** 601,607 ****
      }
  
    /* If a variable is USE associated, it's always external.  */
!   if (sym->attr.use_assoc)
      {
        DECL_EXTERNAL (decl) = 1;
        TREE_PUBLIC (decl) = 1;
--- 602,608 ----
      }
  
    /* If a variable is USE associated, it's always external.  */
!   if (sym->attr.use_assoc || sym->attr.used_in_submodule)
      {
        DECL_EXTERNAL (decl) = 1;
        TREE_PUBLIC (decl) = 1;
Index: gcc/testsuite/gfortran.dg/submodule_1.f90
===================================================================
*** gcc/testsuite/gfortran.dg/submodule_1.f90	(revision 0)
--- gcc/testsuite/gfortran.dg/submodule_1.f90	(working copy)
***************
*** 0 ****
--- 1,177 ----
+ ! { dg-do run }
+ !
+ ! Basic test of submodule functionality.
+ !
+ ! Contributed by Paul Thomas  <pault@gcc.gnu.org>
+ !
+  module foo_interface
+    implicit none
+    character(len = 100) :: message
+    character(len = 100) :: message2
+ 
+    type foo
+      character(len=15) :: greeting = "Hello, world!  "
+      character(len=15), private :: byebye = "adieu, world!  "
+    contains
+      procedure :: greet => say_hello
+      procedure :: farewell => bye
+      procedure, private :: adieu => byebye
+    end type foo
+ 
+    interface
+      module subroutine say_hello(this)
+        import foo
+        class(foo), intent(in) :: this
+      end subroutine
+ 
+      module subroutine bye(this)
+        import foo
+        class(foo), intent(in) :: this
+      end subroutine
+ 
+      module subroutine byebye(this, that)
+        import foo
+        class(foo), intent(in) :: this
+        class(foo), intent(inOUT), allocatable :: that
+      end subroutine
+ 
+      module function realf (arg) result (res)
+        real :: arg, res
+      end function
+ 
+      integer module function intf (arg)
+        integer :: arg
+      end function
+ 
+      real module function realg (arg)
+        real :: arg
+      end function
+ 
+      integer module function intg (arg)
+        integer :: arg
+      end function
+ 
+    end interface
+ 
+    integer :: factor = 5
+ 
+  contains
+ 
+    subroutine smurf
+      class(foo), allocatable :: this
+      allocate (this)
+      message = "say_hello from SMURF --->"
+      call say_hello (this)
+    end subroutine
+  end module
+ 
+ !
+   SUBMODULE (foo_interface) foo_interface_son
+ !
+   contains
+ ! Test module procedure with conventional specification part for dummies
+      module subroutine say_hello(this)
+        class(foo), intent(in) :: this
+        class(foo), allocatable :: that
+        allocate (that, source = this)
+ !       call this%farewell         ! NOTE WELL: This compiles and causes a crash in run-time
+ !                                               due to recursion through the call to this procedure from
+ !                                               say hello.
+        message = that%greeting
+ 
+ ! Check that descendant module procedure is correctly processed
+        if (intf (77) .ne. factor*77) call abort
+      end subroutine
+ 
+      module function realf (arg) result (res)
+        real :: arg, res
+        res = 2*arg
+      end function
+ 
+   end SUBMODULE foo_interface_son
+ 
+ !
+ ! Check that multiple generations of submodules are OK
+   SUBMODULE (foo_interface:foo_interface_son) foo_interface_grandson
+ !
+   contains
+ 
+      module procedure intf
+        intf = factor*arg
+      end function
+ 
+   end SUBMODULE foo_interface_grandson
+ 
+ !
+   SUBMODULE (foo_interface) foo_interface_daughter
+ !
+   contains
+ ! Test module procedure with abbreviated declaration and no specification of dummies
+      module procedure bye
+        class(foo), allocatable :: that
+        call say_hello (this)
+ ! check access to a PRIVATE procedure pointer that accesses a private component
+        call this%adieu (that)
+        message2 = that%greeting
+      end subroutine
+ 
+ ! Test module procedure pointed to by PRIVATE component of foo
+      module procedure byebye
+        allocate (that, source = this)
+ ! Access a PRIVATE component of foo
+        that%greeting = that%byebye
+      end subroutine
+ 
+      module procedure intg
+        intg = 3*arg
+      end function
+ 
+      module procedure realg
+        realg = 3*arg
+      end function
+ 
+   end SUBMODULE foo_interface_daughter
+ 
+ !
+  program try
+    use foo_interface
+    implicit none
+    type(foo) :: bar
+ 
+    call clear_messages
+    call bar%greet ! typebound call
+    if (trim (message) .ne. "Hello, world!") call abort
+ 
+    call clear_messages
+    bar%greeting = "G'day, world!"
+    call say_hello(bar) ! Checks use association of 'say_hello'
+    if (trim (message) .ne. "G'day, world!") call abort
+ 
+    call clear_messages
+    bar%greeting = "Hi, world!"
+    call bye(bar) ! Checks use association in another submodule
+    if (trim (message) .ne. "Hi, world!") call abort
+    if (trim (message2) .ne. "adieu, world!") call abort
+ 
+    call clear_messages
+    call smurf ! Checks host association of 'say_hello'
+    if (trim (message) .ne. "Hello, world!") call abort
+ 
+    call clear_messages
+    bar%greeting = "farewell     "
+    call bar%farewell
+    if (trim (message) .ne. "farewell") call abort
+    if (trim (message2) .ne. "adieu, world!") call abort
+ 
+    if (realf(2.0) .ne. 4.0) call abort ! Check module procedure with explicit result
+    if (intf(2) .ne. 10) call abort     ! ditto
+    if (realg(3.0) .ne. 9.0) call abort ! Check module procedure with function declaration result
+    if (intg(3) .ne. 9) call abort      ! ditto
+  contains
+    subroutine clear_messages
+      message = ""
+      message2 = ""
+    end subroutine
+  end program
+ !
+ 
Index: gcc/testsuite/gfortran.dg/submodule_2.f90
===================================================================
*** gcc/testsuite/gfortran.dg/submodule_2.f90	(revision 0)
--- gcc/testsuite/gfortran.dg/submodule_2.f90	(working copy)
***************
*** 0 ****
--- 1,106 ----
+ ! { dg-do run }
+ !
+ ! Test dummy and result arrays in module procedures
+ !
+ ! Contributed by Paul Thomas  <pault@gcc.gnu.org>
+ !
+  module foo_interface
+    implicit none
+    type foo
+      character(len=16) :: greeting = "Hello, world!   "
+      character(len=16), private :: byebye = "adieu, world!   "
+    end type foo
+ 
+    interface
+      module function array1(this) result (that)
+        import foo
+        type(foo), intent(in), dimension(:) :: this
+        type(foo), allocatable, dimension(:) :: that
+      end function
+      character(16) module function array2(this, that)
+        import foo
+        type(foo), intent(in), dimension(:) :: this
+        type(foo), allocatable, dimension(:) :: that
+      end function
+      module subroutine array3(this, that)
+        import foo
+        type(foo), intent(in), dimension(:) :: this
+        type(foo), intent(inOUT), allocatable, dimension(:) :: that
+      end subroutine
+      module subroutine array4(this, that)
+        import foo
+        type(foo), intent(in), dimension(:) :: this
+        type(foo), intent(inOUT), allocatable, dimension(:) :: that
+      end subroutine
+    end interface
+  end module
+ 
+ !
+   SUBMODULE (foo_interface) foo_interface_son
+ !
+   contains
+ 
+ ! Test array characteristics for dummy and result are OK
+      module function array1 (this) result(that)
+        type(foo), intent(in), dimension(:) :: this
+        type(foo), allocatable, dimension(:) :: that
+        allocate (that(size(this)), source = this)
+        that%greeting = that%byebye
+      end function
+ 
+ ! Test array characteristics for dummy and result are OK for
+ ! abbreviated module procedure declaration.
+      module procedure array2
+        allocate (that(size(this)), source = this)
+        that%greeting = that%byebye
+        array2 = trim (that(size (that))%greeting(1:5))//", people!"
+      end function
+ 
+   end SUBMODULE foo_interface_son
+ 
+ !
+   SUBMODULE (foo_interface) foo_interface_daughter
+ !
+   contains
+ 
+ ! Test array characteristics for dummies are OK
+      module subroutine array3(this, that)
+        type(foo), intent(in), dimension(:) :: this
+        type(foo), intent(inOUT), allocatable, dimension(:) :: that
+        allocate (that(size(this)), source = this)
+        that%greeting = that%byebye
+      end subroutine
+ 
+ ! Test array characteristics for dummies are OK for
+ ! abbreviated module procedure declaration.
+      module procedure array4
+        integer :: i
+        allocate (that(size(this)), source = this)
+        that%greeting = that%byebye
+        do i = 1, size (that)
+          that(i)%greeting = trim (that(i)%greeting(1:5))//", people!"
+        end do
+      end subroutine
+ 
+   end SUBMODULE foo_interface_daughter
+ 
+ !
+  program try
+    use foo_interface
+    implicit none
+    type(foo), dimension(2) :: bar
+    type (foo), dimension(:), allocatable :: arg
+ 
+    arg = array1(bar) ! typebound call
+    if (any (arg%greeting .ne. ["adieu, world!   ", "adieu, world!   "])) call abort
+    deallocate (arg)
+    if (trim (array2 (bar, arg)) .ne. "adieu, people!") call abort
+    deallocate (arg)
+    call array3 (bar, arg) ! typebound call
+    if (any (arg%greeting .ne. ["adieu, world!   ", "adieu, world!   "])) call abort
+    deallocate (arg)
+    call array4 (bar, arg) ! typebound call
+    if (any (arg%greeting .ne. ["adieu, people!", "adieu, people!"])) call abort
+  contains
+  end program
+ !
Index: gcc/testsuite/gfortran.dg/submodule_3.f90
===================================================================
*** gcc/testsuite/gfortran.dg/submodule_3.f90	(revision 0)
--- gcc/testsuite/gfortran.dg/submodule_3.f90	(working copy)
***************
*** 0 ****
--- 1,36 ----
+ ! { dg-do compile }
+ ! { dg-options "-std=f2003" }
+ !
+ ! Check enforcement of F2008 standard for MODULE PROCEDURES and SUBMODULES
+ ! This is rather bare-bones to reduce the number of error messages too the
+ ! essential minimum.
+ !
+ ! Contributed by Paul Thomas  <pault@gcc.gnu.org>
+ !
+  module foo_interface
+    implicit none
+ 
+    interface
+      module function array1(this) result (that) ! { dg-error "MODULE prefix" }
+      end function ! { dg-error "Expecting END INTERFACE" }
+      character(16) module function array2(this, that) ! { dg-error "MODULE prefix" }
+      end function ! { dg-error "Expecting END INTERFACE" }
+    end interface
+  end module
+ 
+ !
+   SUBMODULE (foo_interface) foo_interface_son ! { dg-error "SUBMODULE declaration" }
+ !
+   contains
+ 
+      module function array1 (this) result(that) ! { dg-error "MODULE prefix" }
+      end function ! { dg-error "Expecting END PROGRAM" }
+ 
+ ! Test array characteristics for dummy and result are OK for
+ ! abbreviated module procedure declaration.
+      module procedure array2 ! { dg-error "must be in a generic module interface" }
+      end function ! { dg-error "Expecting END PROGRAM" }
+ 
+   end SUBMODULE foo_interface_son ! { dg-error "Expecting END PROGRAM" }
+ 
+ end ! { dg-error "CONTAINS statement without FUNCTION or SUBROUTINE" }
Index: gcc/testsuite/gfortran.dg/submodule_4.f90
===================================================================
*** gcc/testsuite/gfortran.dg/submodule_4.f90	(revision 0)
--- gcc/testsuite/gfortran.dg/submodule_4.f90	(working copy)
***************
*** 0 ****
--- 1,139 ----
+ ! { dg-do compile }
+ !
+ ! Tests comparisons of MODULE PROCEDURE characteristics and
+ ! the characteristics of their dummies.
+ !
+ ! Contributed by Paul Thomas  <pault@gcc.gnu.org>
+ !
+  module foo_interface
+    implicit none
+    type foo
+      character(len=16) :: greeting = "Hello, world!   "
+      character(len=16), private :: byebye = "adieu, world!   "
+    end type foo
+ 
+    interface
+      module function array1(this) result (that)
+        import foo
+        type(foo), intent(in), dimension(:) :: this
+        type(foo), allocatable, dimension(:) :: that
+      end function
+      character(16) module function array2(this, that)
+        import foo
+        type(foo), intent(in), dimension(:) :: this
+        type(foo), allocatable, dimension(:) :: that
+      end function
+      module subroutine array3(this, that)
+        import foo
+        type(foo), intent(in), dimension(:) :: this
+        type(foo), intent(inOUT), allocatable, dimension(:) :: that
+      end subroutine
+      module subroutine array4(this, that)
+        import foo
+        type(foo), intent(in), dimension(:) :: this
+        type(foo), intent(inOUT), allocatable, dimension(:) :: that
+      end subroutine
+      integer module function scalar1 (arg)
+         real, intent(in) :: arg
+      end function
+      module function scalar2 (arg) result(res)
+         real, intent(in) :: arg
+         real :: res
+      end function
+       module function scalar3 (arg) result(res)
+         real, intent(in) :: arg
+         real :: res
+      end function
+       module function scalar4 (arg) result(res)
+         real, intent(in) :: arg
+         complex :: res
+      end function
+       module function scalar5 (arg) result(res)
+         real, intent(in) :: arg
+         real, allocatable :: res
+      end function
+       module function scalar6 (arg) result(res)
+         real, intent(in) :: arg
+         real, allocatable :: res
+      end function
+    end interface
+  end module
+ 
+ !
+   SUBMODULE (foo_interface) foo_interface_son
+ !
+   contains
+ 
+      module function array1 (this) result(that) ! { dg-error "Rank mismatch in function result" }
+        type(foo), intent(in), dimension(:) :: this
+        type(foo), allocatable :: that
+      end function
+ 
+      character(16) module function array2(this) ! { dg-error "Mismatch in number of MODULE PROCEDURE formal arguments" }
+        type(foo), intent(in), dimension(:) :: this
+        type(foo), allocatable, dimension(:) :: that
+        allocate (that(2), source = this(1))
+        that%greeting = that%byebye
+        array2 = trim (that(size (that))%greeting(1:5))//", people!"
+      end function
+ 
+      module subroutine array3(thiss, that) ! { dg-error "Mismatch in MODULE PROCEDURE formal argument names" }
+        type(foo), intent(in), dimension(:) :: thiss
+        type(foo), intent(inOUT), allocatable, dimension(:) :: that
+        allocate (that(size(thiss)), source = thiss)
+        that%greeting = that%byebye
+      end subroutine
+ 
+      module subroutine array4(this, that, the_other) ! { dg-error "Mismatch in number of MODULE PROCEDURE formal arguments" }
+        type(foo), intent(in), dimension(:) :: this
+        type(foo), intent(inOUT), allocatable, dimension(:) :: that, the_other
+        integer :: i
+        allocate (that(size(this)), source = this)
+        that%greeting = that%byebye
+        do i = 1, size (that)
+          that(i)%greeting = trim (that(i)%greeting(1:5))//", people!"
+        end do
+      end subroutine
+ 
+      recursive module function scalar1 (arg) ! { dg-error "Mismatch in RECURSIVE" }
+         real, intent(in) :: arg
+      end function
+ 
+      pure module function scalar2 (arg) result(res) ! { dg-error "Mismatch in PURE" }
+         real, intent(in) :: arg
+         real :: res
+      end function
+ 
+   end SUBMODULE foo_interface_son
+ 
+ !
+   SUBMODULE (foo_interface) foo_interface_daughter
+ !
+   contains
+ 
+       module function scalar3 (arg) result(res) ! { dg-error "Type mismatch in argument" }
+         integer, intent(in) :: arg
+         real :: res
+      end function
+ 
+       module function scalar4 (arg) result(res) ! { dg-error "Type mismatch in function result" }
+         real, intent(in) :: arg
+         real :: res
+      end function
+ 
+       module function scalar5 (arg) result(res) ! { dg-error "ALLOCATABLE attribute mismatch in function result " }
+         real, intent(in) :: arg
+         real :: res
+      end function
+ 
+       module function scalar6 (arg) result(res) ! { dg-error "Rank mismatch in argument" }
+         real, intent(in), dimension(2) :: arg
+         real, allocatable :: res
+      end function
+   end SUBMODULE foo_interface_daughter
+ 
+ !
+  program try
+    use foo_interface
+  end program
+ !
Index: gcc/testsuite/gfortran.dg/submodule_5.f90
===================================================================
*** gcc/testsuite/gfortran.dg/submodule_5.f90	(revision 0)
--- gcc/testsuite/gfortran.dg/submodule_5.f90	(working copy)
***************
*** 0 ****
--- 1,38 ----
+ ! { dg-do compile }
+ !
+ ! Checks that PRIVATE/PUBLIC not allowed in submodules.
+ !
+ ! Contributed by Paul Thomas  <pault@gcc.gnu.org>
+ !
+ module foo_interface
+   implicit none
+   type foo
+     character(len=16), private :: byebye = "adieu, world!   "
+   end type foo
+ end module
+ 
+ SUBMODULE (foo_interface) foo_interface_son
+   private ! { dg-error "PRIVATE statement" }
+   public ! { dg-error "PUBLIC statement" }
+   integer, public :: i ! { dg-error "PUBLIC attribute" }
+   integer, private :: j ! { dg-error "PRIVATE attribute" }
+   type :: bar
+     private ! { dg-error "PRIVATE statement" }
+     public ! { dg-error "PUBLIC statement" }
+     integer, private :: i ! { dg-error "PRIVATE attribute" }
+     integer, public :: j ! { dg-error "PUBLIC attribute" }
+   end type bar
+ contains
+ !
+ end submodule foo_interface_son
+ 
+ SUBMODULE (foo_interface) foo_interface_daughter
+ !
+ contains
+   subroutine foobar (arg)
+     type(foo) :: arg
+     arg%byebye = "hello, world!   " ! Access to private component is OK
+   end subroutine
+ end SUBMODULE foo_interface_daughter
+ 
+ end


More information about the Gcc-patches mailing list