• Aucun résultat trouvé

Array Constructors

Dans le document The Fortran 2003 Handbook (Page 118-125)

4 Data Types

4.5 Array Constructors

An array constructor is a mechanism that is used to construct a rank-one array from a sequence of values. Syntactically, it is a sequence of scalar values, arrays, and implied-do specifications enclosed in either square brackets or in parentheses and slashes. For example:

REAL VECTOR_X(3), VECTOR_Y(2), RESULT(100) . . .

RESULT (1:8) = [ 1.3, 5.6, VECTOR_X, 2.35, VECTOR_Y ]

The value of the first eight elements of RESULT is constructed from the values of VECTOR_X and VECTOR_Y and three real constants in the specified order. If an array appears in the value list, the values of its elements are taken in array element order. If it is necessary to construct an array of rank greater than one, the RESHAPE intrinsic function may be applied to an array constructor.

112 Chapter 4

The form for an array constructor (R465) is one of:

[ [ type-spec:: ] [ ac-value-list ] ] (/ [ type-spec:: ] [ ac-value-list ] /)

The outermost square brackets [ ] in the first syntax form are literal square brackets rather than indications of optionality.

An ac-value is one of expression

ac-implied-do

where the expression can be either a scalar or an array.

The form for an ac-implied-do (R470) is:

( ac-value-list , ac-do-variable = scalar-integer-expression , &

scalar-integer-expression [ , scalar-integer-expression ] ) Rules and restrictions:

1. The type specifier and the ac-value list may not both be omitted.

2. If the type specification is omitted, each ac-value expression in the array construc-tor must have the same type and type parameters, including length parameters; the type and type parameters of the constructor are those of the expressions.

3. If the type specification is included, each ac-value expression must be of a type and type parameters compatible with intrinsic assignment to the specified type. The constructor has the specified type and type parameters.

4. An ac-do-variable must be a scalar integer named variable. This variable has the scope of this ac-implied-do.

5. If an ac-implied-do is contained within another ac-implied-do, they must not have the same ac-do-variable.

If an ac-value is an array expression, the values of the elements of the expression in array element order (6.6.6) become the values of the array constructor.

If an ac-value is an implied-do specification, it is expanded to form a sequence of values under control of the ac-do-variable as in the DO construct (8.7.2.1).

If every expression in an array constructor is an initialization expression, the array constructor is an initialization expression as in the example above. Such an array con-structor may be used to give a value to a named constant, for example:

REAL X(3), BIGGER_X(4)

PARAMETER (X = [ (I, I = 2, 6, 2) ] ) PARAMETER (BIGGER_X = [ 0.0, X ] )

Following are several examples of array constructors.

Data Types 113

Example 1. To create a value for an array of rank greater than one, the RESHAPE in-trinsic function must be used. With this function, a one-dimensional array may be re-shaped into any allowable array shape.

Y = RESHAPE (SOURCE = [ 2.0, [ 4.5, 4.0 ], Z ], SHAPE = [ 3, 2 ] ) If Z has the value [1.2 3.5 1.1], Y is a 3 × 2 array with the elements:

2.0 1.2 4.5 3.5 4.0 1.1

Example 2. It may be necessary to construct an array value of derived type.

TYPE PERSON INTEGER AGE

CHARACTER (LEN = 40) NAME END TYPE PERSON

TYPE (PERSON) CAR_POOL (3)

CAR_POOL = [ PERSON (35, "SCHMITT"), &

PERSON (57, "LOPEZ"), PERSON (26, "YUNG") ]

Example 3. A type specifier in an array constructor can be used to coerce all elements to the same type and type parameters, as in:

[real:: 42, 1.234, 57]

[character(16):: 'Tom', 'Dick', 'Harry']

It also provides a simple way to write a zero-sized constructor as in:

[integer:: ]

Without the type specifier, this form would be invalid because there would be no spec-ification of the type of the array.

4.6 Enumerations

An enumeration is a set of named integer constants, each of which is called an enumer-ator. Enumerations are designed primarily to facilitate C interoperability. Although an enumeration can be used independently of C, it then provides little utility that could not be achieved using other syntax. The main functionality of enumerations is in their automatic selection of the appropriate integer kinds to be compatible with correspond-ing C enums.

114 Chapter 4

The form of an enumeration is ENUM, BIND(C)

enumerator-definition-statement [ enumerator-definition-statement ] ...

END ENUM

The BIND(C) is mandatory. The form of an enumerator definition statement is ENUMERATOR [ :: ] enumerator-list

where the form of an enumerator is

named-constant [ =scalar-integer-initialization-expression ]

If any enumerator in a list includes the optional initialization expression, then the dou-ble colon in that enumerator definition statement is required.

The enumeration declares all of its enumerators to be integer named constants with values as described below. The kind of the named constants is automatically selected so that they are interoperable with a C enumeration type that specified the same values in the same order. Note that an enumeration does not define a distinct type; it just fa-cilitates the selection of an appropriate integer kind. The effect of an enumeration is identical to that of a corresponding set of integer parameter declarations.

The value of an enumerator is determined as follows:

1. If the enumerator definition has an initialization expression, that expression gives the value.

2. If the enumerator definition does not have an initialization expression, the value is 1 greater than the value of the previous enumerator, or is 0 if it is the first enumer-ator.

Example:

enum, bind(c)

enumerator :: red=4, blue=9 enumerator yellow

end enum

In this example, the named constant red will have the value 4, blue will be 9, and yel-low will be 10. Note that the effect is the same whether the enumerators of an enumer-ation are declared all in a single statement or in multiple ones.

Although the main functionality of an enumeration is to automatically select the appropriate integer kind, the syntax provides no way to directly find what kind was selected. The only way to find that is to use the KIND intrinsic on one of the resulting named constants. For example, the following is a way to declare an integer variable of the kind selected by the above enumeration.

integer(kind(red)) :: x

5

Declarations

Declarations are used to specify the attributes and relationships of the entities in a program.

• The declared Type of a variable, function, or named constant is specified explicitly by a type declaration or implicitly by the first letter of the entity’s name. An IMPLICIT statement associates a type with specific letters or disables implicit typ-ing.

• A Polymorphic entity is one whose dynamic type can change during program exe-cution. For a polymorphic entity, the dynamic type may be different from the declared type. A nonpolymorphic entity also has a dynamic type, but it is always the same as the declared type.

• The DIMENSION attribute specifies an array. The array may have explicit shape (with all bounds specified), deferred shape (if it also has the ALLOCATABLE or POINTER attribute), or assumed shape/size (if it is a dummy argument).

• The ALLOCATABLE or POINTER attribute specifies an entity that may be dynam-ically allocated during program execution. Alternatively, a pointer variable may be associated with an existing target. The TARGET attribute specifies that a variable may be the target of a pointer.

Initialization of a variable may be specified in a type declaration or in a DATA statement. Pointers may be initially disassociated.

• The EXTERNAL or INTRINSIC attribute specifies the nature of a procedure.

• The INTENT, VALUE, or OPTIONAL attribute specifies properties of a dummy argument.

• The PARAMETER attribute specifies a named constant.

• The PUBLIC, PRIVATE, or PROTECTED attribute allows a programmer to control the accessibility and use of entities specified in modules.

• The BIND(C) attribute facilitates interoperation with C data and functions.

• The ASYNCHRONOUS or VOLATILE attribute specifies that a variableʹs value might be referenced or redefined outside of the normal flow of program execution.

Declarations are used to specify the type and other attributes of program entities. The attributes that an entity possesses determine how the entity may be used in a program.

Every variable and function has a type, which is the most important of the attributes;

type is discussed in 4. However, type is only one of a number of attributes that an en-tity may possess. Attributes may be specified in type declaration or procedure declara-J.C. Adams et al., The Fortran 2003 Handbook,

DOI: 10.1007/978-1-84628-746-6_5, © Springer-Verlag London Limited 2009

116 Chapter 5 tion statements (entity-oriented form), in separate attribute declaration statements (attribute-oriented form), or in a mix of these forms. Attributes of a procedure may be specified in an interface body, which is discussed in 12.5.2. Some entities, such as sub-routines and namelist groups, do not have a type but may possess other attributes.

In addition, there are relationships among objects that can be specified by EQUIV-ALENCE, COMMON, and NAMELIST statements. A NAMELIST statement specifies a name for a list of objects that may be referenced in an input/output statement. The EQUIVALENCE statement indicates that some variables share storage. The COMMON statement specifies a name for a block of storage and the names of objects in the block;

this block can then be shared among different program units. COMMON and EQUIV-ALENCE are provided primarily for compatibility with older versions of the language;

they are seldom needed in new programs, where modules can provide replacement functionality in a more structured way.

In general, Fortran keywords are used to declare the attributes for an entity. The following list summarizes these keywords:

Type INTEGER

REAL (and DOUBLE PRECISION) COMPLEX

LOGICAL CHARACTER

TYPE (user-defined name)

Array properties DIMENSION

Allocatable property ALLOCATABLE

Pointer properties POINTER

TARGET Value definition properties DATA

PARAMETER SAVE

ASYNCHRONOUS VOLATILE

Module entity properties PUBLIC PRIVATE PROTECTED BIND Dummy argument properties INTENT

OPTIONAL VALUE Procedure properties EXTERNAL

INTRINSIC

Declarations 117 In earlier versions of the language, it was necessary to use a different statement for each attribute given to a variable or a collection of variables, for example:

INTEGER A, B, C SAVE A, B, C

In later versions, for objects that have a type, the other attributes may be included in the type declaration statement. For example:

INTEGER, SAVE :: A, B, C

Collecting the attributes into a single statement is sometimes more convenient for read-ers of programs. It eliminates searching through many declaration statements to locate all the attributes of a particular object. Emphasis can be placed on an object and its at-tributes (entity-oriented declaration) or on an attribute and the objects that possess the attribute (attribute-oriented declaration), whichever is preferred by a programmer.

It is also allowed to use a mixed form, specifying some attributes for an entity to-gether and some separately. The terms “entity-oriented” and “attribute-oriented” are used here for expository purposes, but are not actually distinctions made by the stan-dard. The attributes of an entity are collected from those specified for it by all forms of specification. Unfortunately, there is no way within the language for the programmer to specify enforcement of an entity-oriented form.

The same attribute must not be specified explicitly for an entity more than once, re-gardless of the form of specification.

The following are examples of entity-oriented and attribute-oriented forms:

• entity-oriented declarations

REAL, DIMENSION(20), SAVE :: X or

REAL, SAVE :: X(20)

• attribute-oriented declarations REAL X

DIMENSION X(20) SAVE X

or

REAL X (20) SAVE X

Although most attributes are determined statically at compilation time, some at-tributes can be specified to vary during program execution. A variable that has such a varying attribute is called dynamic. The attributes that can be dynamic are the type, length type parameters, and array bounds. There are four categories of dynamic vari-ables: automatic, allocatable, pointer, and polymorphic. An automatic variable is one whose dynamic attributes are automatically determined on entry to a procedure, but

118 Chapter 5 which is not a dummy argument or function result. The exception for dummy argu-ments and function results is largely historical in that they predate dynamic allocation and can be implemented without it, so the standard does not refer to them as dynamic.

Length type parameters and array bounds are the attributes that can be automatic. Al-locatable, pointer, and polymorphic variables can have their dynamic attributes speci-fied by executable statements and can be dummy arguments, function results, or other variables.

Dans le document The Fortran 2003 Handbook (Page 118-125)