Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

named enum, bind(c)

Jeffrey_H_Intel
Employee
1,937 Views

Both http://fortranwiki.org/fortran/show/Typed+enumerators and http://docs.cray.com/books/S-3693-36/html-S-3693-36/z1018297010.html show an example of a named enum in Fortran with ISO_C_BINDING.

I cannot build this with Intel 17 or GCC 6.2.  Can you please help me understand what I am doing wrong?

! enum.F 
      module enumtest
      use iso_c_binding
#if NAMED_ENUM
      enum, bind(c) :: color
        enumerator :: red = 4, blue = 9
        enumerator yellow
      end enum
#else
      enum, bind(c)
        enumerator :: red = 4, blue = 9
        enumerator yellow
      end enum
#endif
      end module enumtest
$ ifort -std08 -free -g -warn all -traceback -c enum.F
$ ifort -std08 -free -g -warn all -traceback -DNAMED_ENUM -c enum.F
enum.F(4): error #5082: Syntax error, found '::' when expecting one of: <END-OF-STATEMENT> ;
      enum, bind(c) :: color
--------------------^
enum.F(5): error #6163: This statement is not in the correct order.
        enumerator :: red = 4, blue = 9
--------^
enum.F(6): error #6163: This statement is not in the correct order.
        enumerator yellow
--------^
enum.F(7): error #8131: An END ENUM statement occurred without a corresponding ENUM statement.
      end enum
------^
compilation aborted for enum.F (code 1)
$ gfortran-6 -std=f2008 -c enum.F 
$ gfortran-6 -std=f2008 -DNAMED_ENUM -c enum.F 
enum.F:4:6:

       enum, bind(c) :: color
      1
Error: Unclassifiable statement at (1)
enum.F:5:22:

         enumerator :: red = 4, blue = 9
                      1
Error: ENUM definition statement expected before (1)
enum.F:6:18:

         enumerator yellow
                  1
Error: ENUM definition statement expected before (1)
enum.F:7:9:

       end enum
         1
Error: Expecting END MODULE statement at (1)

 

0 Kudos
1 Solution
IanH
Honored Contributor II
1,937 Views

Named enumerations are not part of standard Fortran (2008 or 2015 draft).  That wiki page is just a suggestion for future language revisions.

View solution in original post

0 Kudos
10 Replies
IanH
Honored Contributor II
1,938 Views

Named enumerations are not part of standard Fortran (2008 or 2015 draft).  That wiki page is just a suggestion for future language revisions.

0 Kudos
FortranFan
Honored Contributor II
1,937 Views

@Jeff,

Fyi, another thread at comp.lang.fortran - https://groups.google.com/forum/#!msg/comp.lang.fortran/iWUgXY0UJ4s/BlGjDBhlEgAJ;context-place=forum/comp.lang.fortran

 

 

0 Kudos
jimdempseyatthecove
Honored Contributor III
1,937 Views

Scoped enumerators would be nice, though I do not see a distinction between:

ENUM_TYPE::ENUM_PARAMETER_VAL

and

ENUM_TYPE%ENUM_PARAMETER_VAL

If you wish to remove the scoping, then you could use module scoped parameters

SUBROUTINE ...
  USE COLORS

or

SUBROUTINE...
...
  BLOCK
    USE COLORS
    CALL FOO(Red, Green, Blue)
  END BLOCK

or

SUBROUTINE...
  USE COLORS, RED=>colorRED, ...
 

Jim Dempsey

0 Kudos
Steven_L_Intel1
Employee
1,937 Views

At the risk of sounding like a broken record - er - a corrupted MP3? - ISO_C_BINDING has nothing whatsoever to do with ENUM, BIND(C) or BIND(C) in general, other than the module and the BIND attribute are both part of C interoperability. Though they have some letters in common, they are not the same thing.

0 Kudos
jimdempseyatthecove
Honored Contributor III
1,937 Views

By the way, your command line for ifort did not include the -fpp option. I am surprised the compiler did not complain about the fpp #'s too.

Jim Dempsey

0 Kudos
Steven_L_Intel1
Employee
1,937 Views

The .F (capital F) file type causes fpp to be run implicitly on Linux and MacOS. Similar for .F90 (vs .f90)

0 Kudos
Jeffrey_H_Intel
Employee
1,937 Views

Steve Lionel (Intel) wrote:

At the risk of sounding like a broken record - er - a corrupted MP3? - ISO_C_BINDING has nothing whatsoever to do with ENUM, BIND(C) or BIND(C) in general, other than the module and the BIND attribute are both part of C interoperability. Though they have some letters in common, they are not the same thing.

Sorry, Steve, I keep using ISO_C_BINDING as incorrect shorthand for "all the C interoperability stuff in Fortran 2003."  I will try to be better about that.

0 Kudos
Jeffrey_H_Intel
Employee
1,937 Views

jimdempseyatthecove wrote:

By the way, your command line for ifort did not include the -fpp option. I am surprised the compiler did not complain about the fpp #'s too.

Jim Dempsey

I use .F rather than -fpp because other Fortran compilers support the former.

On Mac is that the filesystem is case-insensitive, so one can get into truly horrible situations when trying to rename files from .f to .F and vice versa, so I default to .F since it is obnoxious to change it later, especially when Git is involved.

0 Kudos
jimdempseyatthecove
Honored Contributor III
1,937 Views

Steve Lionel (Intel) wrote:

The .F (capital F) file type causes fpp to be run implicitly on Linux and MacOS. Similar for .F90 (vs .f90)

Bad habit (assumption) learned from my earlier Fortran (FORTRAN) programming in Windows.

Jim Dempsey

0 Kudos
mecej4
Honored Contributor III
1,937 Views

With some Windows compilers, even if the file is named xyz.f in the directory listing, referring to it as xyz.F in the compiler command line causes the preprocessor to be run. This can be convenient, but it can also be troublesome!

0 Kudos
Reply