Silverfrost Logo About Us | Contact Us
 

Fortran in an object oriented environment

In Fortran 95, a structure is defined as a derived type. The components of a derived type are data items and a derived type is simply a record consisting of components that are named and have a specified data type. Each instance of a derived type forms a new record.

In object-oriented languages, a structure becomes a class, with the defining feature that it can include methods (i.e. functions) in addition to data. An instance of a class is called an object (this is not to say that .NET objects are always instances of some class). When a method is called, the call is attached to an object and (when necessary) the compiler passes information about the object as an additional argument. In this respect Fortran cannot be described as an object-oriented language.

Again in object-oriented languages, the class data usually varies from one object to another. Such data is called instance data. In many languages, a class can also have static data. Such data is shared by all objects belonging to the class. Furthermore, a call to a method is usually dependent on the instance data and as such is called an instance method. In exceptional cases a method depends only on the static data and can be defined as a static method. In static methods there is no need for an additional argument because the instance data is not used in the call.

The .NET environment is primarily designed for object-oriented languages with the added constraint that all non-local variables must be members of a class. Fortran can be accommodated in this environment by identifying a project with a unique class and by making all the data and all the methods static members of this class (each instance of a derived type takes the form of static data).

When using FTN95 to compile a program for .NET, a class name is provided by the linker DBK_LINK or DBK_LINK2 and all of the data and the routines are placed in this class. By default this name is the name of the assembly (i.e. the executable or DLL) with the extension removed. This name is case sensitive. The DBK_LINK or DBK_LINK2 command line switch /CONTAINING_CLASS allows you to provide a different name. Similarly, when /LINK is used on the FTN95 command line and FTN95 calls DBK_LINK or DBK_LINK2 implicitly, the FTN95 command line switch /CONTAINING_CLASS can be used to provide an alternative name. Typically this name will contain a namespace component as well as a class name. For example:

FTN95 MYCODE /CLR /LINK /CONTAING_CLASS BloggsManufacturing.DesignRoutines

This creates the namespace 'BloggsManufacturing', the class 'DesignRoutines' within it, and makes all the Fortran routines into static methods of this class. Nested namespaces can also be specified using several dots in the name.

The name of the class becomes significant if the resulting library is called from other .NET languages, such as C#. A single Fortran project can only contain code for one class and the linker DBK_LINK or DBK_LINK2 can only be used to link code that is in the same class. If you required more than one class then you must have a separate project for each class.

Fortran data that appears in a MODULE or COMMON block is also defined as static data. This means for example that an executable and a library cannot share this kind of data. This is analogous to the situation on a Win32 platform where (by default) a Fortran executable cannot share this kind of data with a DLL.

Note that the structure of a .NET assembly can be displayed using the Microsoft ILDASM utility. This expands an assembly as a tree of information and it enables you to explore the namespaces and class names used in your code. It also enables you to study the intermediate language (IL) code generated by FTN95. However, in many cases several IL instructions are converted by the JIT compiler into just one native instruction. This means that the end result is often more efficient than the IL code implies.

The IL code is also listed by FTN95 when /EXPLIST is used on the command line. This listing is usually easier to follow because it is presented together with the Fortran code used to generate it. However, it does not specify where COMMON variables are stored.

 

 

Copyright © 1999-2017 Silverfrost Limited