Type Declaration

Purpose

A type declaration statement specifies the type, length, and attributes of objects and functions. You can assign initial values to objects.

Fortran 2003 beginsA declaration type specification (declaration_type_spec) is used in a nonexecutable statement. Fortran 2003 ends

Syntax

Read syntax diagramSkip visual syntax diagram
Type Declaration Statement (type_declaration_stmt)

>>-declaration_type_spec--+-----------------------+------------->
                          +-::--------------------+   
                          '-,--attr_spec_list--::-'   

>--entity_decl_list--------------------------------------------><

where declaration_type_spec is as follows:
Read syntax diagramSkip visual syntax diagram
>>-+-intrinsic_type_spec-----------------+---------------------><
   |                                 (1) |   
   +-TYPE--(--intrinsic_type_spec--)-----+   
   +-TYPE--(--derived_type_spec--)-------+   
   |               (2)                   |   
   +-TYPE--(--*--)-----------------------+   
   |                                (3)  |   
   +-CLASS--(--derived_type_spec--)------+   
   |                (4)                  |   
   '-CLASS--(--*--)----------------------'   

Notes:
  1. Fortran 2008
  2. TS29113
  3. Fortran 2003
  4. Fortran 2003

Parameters

  • intrinsic_type_spec is any of the following types:
BYTE  1  INTEGER [kind_selector]
CHARACTER [char_selector] LOGICAL [kind_selector]
COMPLEX [kind_selector] REAL [kind_selector]
DOUBLE COMPLEX VECTOR (vector_type_spec)  1 
DOUBLE PRECISION  
 1  IBM extension
kind_selector

represents one of the permissible length specifications for its associated type. IBM extension begins int_literal_constant cannot specify a kind type parameter. IBM extension ends

Read syntax diagramSkip visual syntax diagram
>>-+-(--+-----------+--int_constant_expr--)-+------------------><
   |    '-KIND-- = -'                       |   
   |                           (1)          |   
   '- * --int_literal_constant--------------'   

Notes:
  1. IBM extension.
vector_type_spec
must specify PIXEL, REAL of kind 4 or 8, or INTEGER or UNSIGNED of kind 1, 2, 4, or 8.
char_selector

specifies the character length .IBM extension begins In XL Fortran, this is the number of characters between 0 and 256 MB. Values exceeding 256 MB are set to 256 MB, while negative values result in a length of zero. If not specified, the default length is 1. The kind type parameter, if specified, must be 1, which specifies the ASCII character representation. IBM extension ends

Read syntax diagramSkip visual syntax diagram
>>-+-(--+-LEN--=--type_param_value--,--KIND--=--int_constant_expr-----+--)-+-><
   |    +-type_param_value--,--+---------+--int_constant_expr---------+    |   
   |    |                      '-KIND--=-'                            |    |   
   |    +-KIND--=--int_constant_expr--+-----------------------------+-+    |   
   |    |                             '-,--LEN--=--type_param_value-' |    |   
   |    '-+--------+--type_param_value--------------------------------'    |   
   |      '-LEN--=-'                                                       |   
   '-*--char_length--+---+-------------------------------------------------'   
                     '-,-'                                                     

 
int_constant_expr
is a scalar integer constant expression that must evaluate to 1
char_length
is either a scalar integer literal constant (which cannot specify a kind type parameter) or a type_param_value enclosed in parentheses
  • derived_type_spec is used to specify a particular derived type and type parameters.
Read syntax diagramSkip visual syntax diagram
>>-type_name--+----------------------------+-------------------><
              '-(--type_param_spec_list--)-'   

where type_param_spec is:
Read syntax diagramSkip visual syntax diagram
>>-+------------+----type_param_value--------------------------><
   '-keyword--=-'                         

type_param_spec
is used to list type parameter values in the derived_type_spec.
keyword
is a name of the parameter of the derived type listed in the derived type definition statement. Each parameter name cannot appear more than once in a type_param_spec_list. Fortran 2003 beginsWhen used with the CLASS keyword, the type must be extensible. If used with the TYPE keyword, the type must not be abstract. Fortran 2003 ends
type_param_value
is a colon (:), an asterisk (*), or an integer scalar expression. For a kind type parameter, the corresponding type_param_value must be an integer constant expression.

Within a derived_type_spec, the type_param_value of an integer expression for a length type parameter must be a specification expression. A colon that specifies a deferred length type parameter, can only be used for a length parameter of an entity that has either a POINTER or ALLOCATABLE attribute. In this case, the value of a deferred type parameter is determined during program execution through either an ALLOCATE statement, an intrinsic assignment or a pointer assignment statement. An asterisk as a type_param_value specifies an assumed length type parameter. A derived_type_spec with an assumed length type parameter specifies a dummy argument, and the value of the assumed type parameter is that of the corresponding actual argument.

Note: type_param_value is also used in type_spec that appears in SELECT TYPE constructs, ALLOCATE statements, or array constructors. In type_spec, a type_param_value that specifies a value for a length type parameter is not required to be a specification expression.
  • attr_spec is any of the following attributes. For detailed rules about a particular attribute, refer to the statement of the same name.
ALLOCATABLE  1  PARAMETER
ASYNCHRONOUS POINTER
AUTOMATIC  2  PRIVATE
BIND (C[, NAME=binding_label])  1  PROTECTED  1 
CONTIGUOUS  3  PUBLIC
DIMENSION (array_spec) SAVE
EXTERNAL STATIC  2 
INTENT (intent_spec) TARGET
INTRINSIC VALUE  1 
OPTIONAL VOLATILE
Notes:
  •  1  Fortran 2003
  •  2  IBM extension
  •  3  Fortran 2008
intent_spec
is either IN, OUT, or INOUT
*
is the asterisk indicator. An entity declared with TS begins TYPE (*) TS ends or CLASS(*) is unlimited polymorphic. An unlimited polymorphic entity is not declared to have a type, and is not considered to have the same declared type as any other entity, including another unlimited polymorphic entity. TS begins TYPE (*) is used to declare assumed-type objects to interoperate with C functions.TS ends
::
is the double colon separator. Use the double colon separator when you specify attributes, =constant_expr, or => NULL().
array_spec
is a list of dimension bounds.
entity_decl
Read syntax diagramSkip visual syntax diagram
>>-a--+-+------------------+--+------------------+-+------------>
      | '- * --char_length-'  '-(--array_spec--)-' |   
      '-(--array_spec--)-- * --char_length---------'   

>--+----------------------------------+------------------------><
   |                            (1)   |   
   '-+-/--initial_value_list--/-----+-'   
     +- = --constant_expr-----------+     
     '- => --NULL()-----------------'     

Notes:
  1. IBM extension
a
is an object name or function name. array_spec cannot be specified for a function with an implicit interface.
char_length (IBM extension)
overrides the length as specified in kind_selector and char_selector, and is only permitted in statements where the length can be specified with the initial keyword. A character entity can specify char_length, as defined above. A noncharacter entity can only specify an integer literal constant that represents one of the permissible length specifications for its associated type.
initial_value (IBM extension)
provides an initial value for the entity specified by the immediately preceding name.
constant_expr
provides a constant expression for the entity specified by the immediately preceding name.
=> NULL()
provides the initial value for the pointer object.

Rules

Within the context of a derived type definition:
  • If => appears in a component initialization, the POINTER attribute must appear in the attr_spec_list.
  • If = appears in a component initialization, the POINTER attribute cannot appear in the component attr_spec_list.
  • The compiler will evaluate constant_expr within the scoping unit of the type definition.

If => appears for a variable, the object must have the POINTER attribute.

If constant_expr appears for a variable, the object cannot have the POINTER attribute.

Entities in type declaration statements are constrained by the rules of any attributes specified for the entities, as detailed in the corresponding attribute statements.

The type declaration statement overrides the implicit type rules in effect. You can use a type declaration statement that confirms the type of an intrinsic function. The appearance of a generic or specific intrinsic function name in a type declaration statement does not cause the name to lose its intrinsic property.

An object cannot be initialized in a type declaration statement if it is a dummy argument, allocatable object, function result, object in a blank common block, integer pointer, external name, intrinsic name, or automatic object. Nor can an object be initialized if it has the AUTOMATIC attribute. The object may be initialized if it appears in a named common block in a block data program unit IBM extension beginsor if it appears in a named common block in a module or submoduleIBM extension ends.

In Fortran 95, a pointer can be initialized. Pointers can only be initialized by the use of => NULL().

The specification expression of a type_param_value or an array_spec can be a nonconstant expression if the specification expression appears in an interface body or in the specification part of a subprogram. Any object being declared that uses this nonconstant expression and is not a dummy argument or a pointee is called an automatic object.

An attribute cannot be repeated in a given type declaration statement, nor can an entity be explicitly given the same attribute more than once in a scoping unit.

constant_expr must be specified if the statement contains the PARAMETER attribute. If the entity you are declaring is a variable, and constant_expr or NULL() is specified, the variable is initially defined.

If the entity you are declaring is a derived type component, and constant_expr or NULL() is specified, the derived type has default initialization.

a becomes defined with the value determined by constant_expr, in accordance with the rules for intrinsic assignment. If the entity is an array, its shape must be specified either in the type declaration statement or in a previous specification statement in the same scoping unit. A variable or variable subobject cannot be initialized more than once. If a is a variable, the presence of constant_expr or NULL() implies that a is a saved object, except for an object in a named common block. The initialization of an object could affect the fundamental storage class of an object.

An array_spec specified in an entity_decl takes precedence over the array_spec in the DIMENSION attribute.

An array function result that does not have the ALLOCATABLE or POINTER attribute must have an explicit-shape array specification.

If the entity declared is a function, it must not have an accessible explicit interface unless it is an intrinsic function.

IBM extension begins If T or F, defined previously as the name of a constant, appears in a type declaration statement, it is no longer an abbreviated logical constant but the name of the named constant. IBM extension ends

The optional comma after char_length in a CHARACTER type declaration statement is permitted only if no double colon separator (::) appears in the statement.

Fortran 2003 begins If the CHARACTER type declaration statement specifies a length of a colon, the length type parameter is a deferred type parameter. An entity or component with a deferred type parameter must specify the ALLOCATABLE or POINTER attribute. A deferred type parameter is a length type parameter whose value can change during the execution of the program. Fortran 2003 ends

If the CHARACTER type declaration statement is in the scope of a module, Fortran 2008 beginssubmoduleFortran 2008 ends, block data program unit, or main program, and you specify the length of the entity as an inherited length, the entity must be the name of a named character constant. The character constant assumes the length of its corresponding expression defined by the PARAMETER attribute.

If the CHARACTER type declaration statement is in the scope of a procedure and the length of the entity is inherited, the entity name must be the name of a dummy argument or a named character constant. If the statement is in the scope of an external function, it can also be the function or entry name in a FUNCTION or ENTRY statement in the same program unit. If the entity name is the name of a dummy argument, the dummy argument assumes the length of the associated actual argument for each reference to the procedure. If the entity name is the name of a character constant, the character constant assumes the length of its corresponding expression defined by the PARAMETER attribute. If the entity name is a function or entry name, the entity assumes the length specified in the calling scoping unit.

The length of a character function can be a specification expression (which must be a constant expression if the function type is not declared in an interface block) or it is a colon, or an asterisk, indicating the length of a dummy procedure name. The length cannot be an asterisk if the function is an internal or module function, if it is recursive, or if it returns array or pointer values.

Examples

CHARACTER(KIND=1,LEN=6) APPLES /'APPLES'/
CHARACTER*7, TARGET :: ORANGES = 'ORANGES'
CALL TEST(APPLES)

SUBROUTINE TEST(VARBL)
  CHARACTER*(*), OPTIONAL :: VARBL   ! VARBL inherits a length of 6

  COMPLEX, DIMENSION (2,3) :: ABC(3) ! ABC has 3 (not 6) array elements
  REAL, POINTER :: XCONST

  TYPE PEOPLE                        ! Defining derived type PEOPLE
    INTEGER AGE
    CHARACTER*20 NAME
  END TYPE PEOPLE
  TYPE(PEOPLE) :: SMITH = PEOPLE(25,'John Smith')
END SUBROUTINE
The following example illustrates the use of derived types parameters in a declaration with a derived type. See the examples in Derived Type for the type definitions themselves.
! Use of the types declared in the example in section Derived type.
TYPE(MULTIDIM(8,3)) :: LOCATION = MULTIDIM(8,3)([1.1_8,2.2_8,3.3_8])
TYPE(NAMED_MULTI(8,3,12)) :: MY_SPOT 
    = NAMED_MULTI(8,3,12)([REAL(8):: 1.1,2.2,3.3],"You are here")


! "PEOPLE" can be defined using type parameters:
TYPE PEOPLE (AGE_KIND, NAME_LEN)
  INTEGER, KIND  :: AGE_KIND
  INTEGER, LEN   :: NAME_LEN
  INTEGER(AGE_KIND) :: AGE
  CHARACTER(NAME_LEN) :: NAME
END TYPE PEOPLE

! Use integer(2) for age, character(20) for name:
TYPE (PEOPLE(2,20)) :: SMITH = PEOPLE(2,20)(25,'John Smith')

! Use integer(1) for age, deferred length for name:
TYPE (PEOPLE(1,:)), ALLOCATABLE :: JDOE
! Actually allocate JDOE with a name of length 8 using implicit allocation:
JDOE = PEOPLE(1,8)(22, "John Doe")
! Explicitly deallocate and reallocate JDOE with a different length:
DEALLOCATE(JDOE)
ALLOCATE(PEOPLE(1,15) :: JDOE)

The following example illustrates the declaration of a vector.

VECTOR (REAL(4)) :: vector_object


Voice your opinion on getting help information Ask IBM compiler experts a technical question in the IBM XL compilers forum Reach out to us