From owner-sc22wg5+sc22wg5-dom8=www.open-std.org@open-std.org  Thu Sep  1 03:02:20 2011
Return-Path: <owner-sc22wg5+sc22wg5-dom8=www.open-std.org@open-std.org>
X-Original-To: sc22wg5-dom8
Delivered-To: sc22wg5-dom8@www.open-std.org
Received: by www.open-std.org (Postfix, from userid 521)
	id AC17E3568D5; Thu,  1 Sep 2011 03:02:20 +0200 (CEST)
Delivered-To: sc22wg5@open-std.org
Received: from ns.nag-j.co.jp (218-42-159-107.cust.bit-drive.ne.jp [218.42.159.107])
	by www.open-std.org (Postfix) with ESMTP id 98C80356889
	for <sc22wg5@open-std.org>; Thu,  1 Sep 2011 03:02:16 +0200 (CEST)
Received: from 218-42-159-108.cust.bit-drive.ne.jp ([218.42.159.108] helo=Maru6)
	by ns.nag-j.co.jp with smtp (Exim 4.50)
	id 1QyvfJ-00053x-Iw; Thu, 01 Sep 2011 10:01:57 +0900
Message-ID: <49220BED34F845569C5241136077C8DD@Maru6>
From: "Malcolm Cohen" <malcolm@nag-j.co.jp>
To: "WG5" <sc22wg5@open-std.org>
Cc: <rabenseifner@hlrs.de>
References: <20110831195648.75A493568AE@www.open-std.org>
In-Reply-To: <20110831195648.75A493568AE@www.open-std.org>
Subject: Re: [ukfortran] (SC22WG5.4520) Callback functions with polymorphicarguments
Date: Thu, 1 Sep 2011 10:02:09 +0900
Organization: =?UTF-8?B?5pel5pysTkFH?=
MIME-Version: 1.0
Content-Type: text/plain;
	format=flowed;
	charset="UTF-8";
	reply-type=original
Content-Transfer-Encoding: 8bit
X-Priority: 3
X-MSMail-Priority: Normal
Importance: Normal
X-Mailer: Microsoft Windows Live Mail 15.4.3538.513
X-MimeOLE: Produced By Microsoft MimeOLE V15.4.3538.513
Sender: owner-sc22wg5@open-std.org
Precedence: bulk

No, the code is not conforming.

The call to FOO passes a
   subroutine callback1(data)
     class(bar) :: data(:)         ! (C)
actual argument, but FOO expects
  subroutine callback(data)
    class(*), dimension(:) :: data    ! (A)

Note that the characteristics of the actual argument differ from those of the 
dummy argument: the actual has an argument of CLASS(BAR) whereas the dummy has 
an argument of CLASS(*).

This is a violation of 12.5.2.9 paragraph 1 sentence 1.

This is not a constraint and not required to be diagnosed.  Since the program is 
not Fortran, it is not very surprising that compilers differ.  (NAG only detects 
the error at runtime, not compile time, which is a shame - I'll have to fix 
that!)

Changing the actual argument to

subroutine callback1(data)
  class(*) :: data(:)         ! (C)
  integer :: i
  select type(data)
  class is (bar)
  do i=1,size(data)
    data(i)%i = i
  end do
  class default
  stop 'WRONG CALLBACK'
  end select
end subroutine

fixes the problem.

-----Original Message----- 
From: Bader, Reinhold
Date: 平成 23年9月1日 4:41
To: j3@j3-fortran.org ; WG5
Cc: Rolf Rabenseifner (rabenseifner@hlrs.de)
Subject: [ukfortran] (SC22WG5.4520) Callback functions with polymorphicarguments

Hello all,

consider the following module:

module mod_callback_up_01
  implicit none
  abstract interface
    subroutine callback(data)
      class(*), dimension(:) :: data    ! (A)
    end subroutine
  end interface
contains
  subroutine foo(data, proc)
    class(*), dimension(:) :: data    ! (B)
    procedure(callback) :: proc
    call proc(data)
  end subroutine
end module

and then the following code which uses this module:

module mod_impl
  implicit none
  type :: bar
    integer :: i
  end type
contains
  subroutine callback1(data)
    class(bar) :: data(:)         ! (C)
    integer :: i
    do i=1,size(data)
      data(i)%i = i
    end do
  end subroutine
end module
program prog
  use mod_callback_up_01
  use mod_impl
  integer, parameter :: dim=5
  type(bar) :: x(dim)
  call foo(x, callback1)
end program

Questions:

(1) Is the above code standard conforming?

(2) Assume that at (C), the declaration is replaced by
       type(bar) :: data(:)
     Is the resulting code standard conforming?

(3) Assume that at (A) and (B), class(*) is replaced by
     class(base), where base is some known type, and
    bar is an extension of base.
     Is the resulting code standard conforming?

(4) Apply both assumptions in (2) and (3) - is the
     resulting code then standard conforming?

The background for this is the issue of handling callbacks in MPI-3.
For backward compatibility it is considered desirable to have
abstract interfaces for callbacks which use arguments declared
as TYPE(*), DIMENSION(*). The dummy arguments of the callbacks
of course will have some definite type in existing code. (The
programmer may still need to add BIND(C), but this is considered
sufficiently low-intrusive). Depending on the answers to above questions,
something may need to be done about this in the TR.

(For the above code, different compilers give widely varying results
with respect  to both compilation and run time behaviour).

Regards
Reinhold



_______________________________________________
ukfortran mailing list
http://lists.accu.org/mailman/listinfo/ukfortran

-- 
................................Malcolm Cohen, Nihon NAG, Tokyo. 

