160 likes | 252 Views
ICoCSIS. Fortran: Specification Statements. Session Six. Outline. Implicit Typing Declaring Entities of Different Shapes Named Constants and Constants Expressions Initial Values for Variables Public and Private Attributes The Pointer, Target, and Allocatable Statement
E N D
ICoCSIS Fortran: Specification Statements Session Six
Outline • Implicit Typing • Declaring Entities of Different Shapes • Named Constants and Constants Expressions • Initial Values for Variables • Public and Private Attributes • The Pointer, Target, and Allocatable Statement • The intent and Optional Statement • The save Attribute • The use Statement • Derived-type Definition • The Type Declaration Statement • Type and Type Parameter Specification • Specification Expression • The Namelist Statement
Introduction The compiler requires information about the entities involved. This information is provided at the beginning of each program unit or subprogram by specification statements.
Implicit Typing strong typingVs. implicit typing In FORTRAN implcit type is assigned according to the initial letter of its name: Entities whose names begin with one of the letters i, j, ..., n are of type default integer, and variables beginning with the letters a, b, ..., h or o, p, ..., z are of type default real. For no implicit typing the statement implicit none is available.
Declaring entities of differing shapes integer :: a, b integer, dimension(10) :: c, d integer, dimension(8,7) :: e Fortran permits using a single statement. Whether or not there is a dimension attribute present, arrays may be declared by placing the shape information after the name of the array: integer :: a, b, c(10), d(10), e(8, 7) or (with dimension for default shape) integer, dimension(10) :: c, d, e(8, 7)
Named constants and constant expressions Inside a program, we often need to define a constant for multiple usage pi = 3.14159 or to use a constant in declarations and executions, which later need to change. named constants: real, parameter :: pi = 3.14159
Initial values for variables • Initialization in type declaration statements real :: a = 0.0 real, dimension(3) :: b = (/ 0.0, 1.2, 4.5 /) • The data statement data object-list /value-list/ [[,] object-list /value-list/]... real :: a, b, c integer :: i, j, k data a,b,c/1.,2.,3./, i,j,k/1,2,3/ • Pointer initialization and the function null real, pointer, dimension(:) :: vector => null() • Default initialization of components: any object of a derived type is given a default initial value for a component type entry real :: value = 2.0 integer :: index type(entry), pointer :: next => null() end type entry
The public and private attributes If the entities in a module are to be accessible elsewhere, thayhave the publicattribute. If access is limited to only the procedures of the module, there is no possibility of an accidental corruption of these data by another procedure and design changes can be made within the module without affecting the rest of the program. In cases where entities are not to be accessible outside their own module, they may be given the privateattribute. real, public :: x, y, z integer, private :: u, v, w or public :: x, y, z, operator(.add.) private :: u, v, w, assignment(=), operator(*)
The pointer, target, and allocatable statements There are statements for specifying the pointer, target, and allocatable attributes of entities. They take the forms: pointer [::] object-name[(array-spec)] [,object-name [(array-spec)]]... target [::] object-name[(array-spec)] [,object-name [(array-spec)]]... and allocatable [::] array-name[(array-spec)] [,array-name [(array-spec)]]... as in real :: a, son, y allocatable :: a(:,:) pointer :: son target :: a, y(10)
The intent and optional statements The intent attribute for a dummy argument that is not a dummy procedure or pointer may be specified in a type declaration statement or in an intent statement of the form intent( inout ) [::] dummy-argument-name-list subroutine solve (a, b, c, x, y, z) real :: a, b, c, x, y, z intent(in) :: a, b, c intent(out) :: x, y, z optional [::] dummy-argument-name-list
The save attribute To retain the value of a local variable in a subprogram, for example to count the number of times the subprogram is entered. real, save :: a A similar situation arises with the use of variables in modules. In theory, on return from a subprogram that accesses a variable whose scope is a module, the variable becomes undefined unless the main program accesses the module, another subprogram in execution accesses the module, or the variable has the save attribute. In practice, compilers treat module variables as having the save attribute. save [ [::] variable-name-list ] A save statement with no list is equivalent to a list containing all possible names.
The use statement use module-name provides access to all the public named data objects, derived types, interface blocks, procedures, generic identifiers, and namelist groups in the module named. Any use statements must precede other specification statements in a scoping unit. The only attribute of an accessed entity that may be specified afresh is public or private (and this only in a module), but the entity may be included in one or more namelistgroups.
Derived-type definitions type, public :: lock private integer, pointer :: key(:) logical :: state end type lock type [[,access]:: ] type-name [ private ] component-def-stmt [component-def-stmt]... end type [ type-name ] Each component-def-stmthas the form type [ [ ,component-attr-list] :: ]component-decl-list where type specifies the type and type parameters. Each component-attris allocatable, pointer, or dimension(bounds-list). Each component-declis component-name [ (bounds-list)][ *char-len ] or component-name [ (bounds-list)][ *char-len ] [ comp-int ]
The type declaration statement • Simple examples of the declarations of named entities by • integer, • real, • complex, • logical, • character, and • type(type-name) statements. • Thegeneral form is • type [ [ , attribute]... :: ] entity-list
Specification expressions • subroutine sample(arr, n, string) • use definitions ! Contains the real a and the integer datasetsize • integer, intent(in) :: n • real, dimension(n), intent(out) :: arr ! Explicit-shape array • character(len=*), intent(in) :: string ! Assumed length • real, dimension(datasetsize+5) :: x ! Automatic array • character(len=n+len(string)) :: cc ! Automatic object • integer, parameter :: pa2 = selected_real_kind(2*precision(a)) • real(kind=pa2) :: z ! Precision of z is at least twice • ! the precision of a
The namelist statement • It is sometimes convenient to gather a set of variables into a single group, in order to facilitate input/output (I/O) operations on the group as a whole. The method by which a group is declared is via the nameliststatement • namelistnamelist-spec • where namelist-specis • /namelist-group-name/ variable-name-list • The namelist-group-name is the name given to the group for subsequent use in the I/O • statements.