Two internal compiler errors

Arjen Markus arjen.markus895@gmail.com
Sun Feb 13 10:59:00 GMT 2011


Hello,

while experimenting with the object-oriented features of Fortran 2003
I ran into a few ICEs.
This was with gfortran 4.6.0 20110101 on Windows XP.

Here is the first program:


! classes_geom.f90 --
!     Classes of geometrical objects as an illustration
!     of object-oriented programming in Fortran
!
module geometrical_objects

    implicit none

    !
    ! General shape
    !
    type, abstract :: shape
        ! No data
    contains
        procedure(get_shape_area), deferred :: get_area
        !procedure                          :: size  -- does not work
    end type shape

    abstract interface
        real function get_shape_area( this )
            import                   :: shape
            class(shape), intent(in) :: this
        end function get_shape_area
    end interface

    !
    ! Rectangle
    !
    type, extends(shape) :: rectangle
        real :: width, height
    contains
        procedure :: get_area => get_rectangle_area
        procedure :: size     => rectangle_size
    end type rectangle

    !
    ! Square
    ! Note:
    ! square_size must have the same interface as its rectangle parent!
    !
    type, extends(rectangle) :: square
    contains
        procedure :: size => square_size
    end type square

contains

!
! The various routines and functions we need
!
real function get_rectangle_area( this )
    class(rectangle), intent(in) :: this

    get_rectangle_area = this%width * this%height

end function get_rectangle_area

subroutine rectangle_size( this, width, height )
    class(rectangle), intent(inout) :: this
    real, intent(in)                :: width
    real, intent(in), optional      :: height

    this%width  = width
    if ( present(height) ) then
        this%height = height
    else
        this%height = width
    endif

end subroutine rectangle_size

subroutine square_size( this, width, height )
    class(square), intent(inout) :: this
    real, intent(in)             :: width
    real, intent(in), optional   :: height  ! Ignored

    this%width  = width
    this%height = width

end subroutine square_size

end module geometrical_objects

!
! Small test program
program test_objects

    use geometrical_objects

    implicit none

    class(shape), dimension(2) :: object

    object(1) = rectangle( 1.0, 2.0 )
    object(2) = square( 1.5 )

    do i = 1,size(object)
        write(*,*) 'Area: ', object(i)%get_area()
    enddo

end program test_objects

The compiler stops with a message that there was a segmentation fault,
the reported line is line 56.
(No syntax errors or other messages of any kind are reported)

After I fixed the more or less obvious flaws, the program does work.

(I will report the second version separately)

Regards,

Arjen



More information about the Fortran mailing list