next up previous contents index
Next: Type casts Up: Statements Previous: Use the pattern-matching facilities

Fortran declarations

Each fortran code contains several objects ; variables of course but also labels, functions and subroutines for example. A simple name is not sufficient to describe one of those objects since some homonyms can exist in several units. We use the undocumented type fsym to reference those objects. An fsym is composed of two parts; a name and an unit. The unit is taken in the current forlib. The name can be a string or an integer (i.e. for labels). An fsym can be used as an entry in the symbol table associated to the fortran unit. A simple string can be used instead of a fsym when the unit is $cunit.

  $\blacktriangleright$ u.FSYM(n)
 
The expression   returns the fsym that associates the name n to the unit u. The attribute SYMBOL   can be used instead of FSYM (this usage of SYMBOL is obsolete).
  $\blacktriangleright$ u.SYMTABSIZE
 
The expression   returns the number of entries in the symbol table associated to the unit u.
  $\blacktriangleright$ u.SYMTABENTRY(k)
 
The expression returns the fsym that describes the entry of rank k in the symbol table associated to the unit u. The rank of the first entry is 0 and the last one is u.SYMTABSIZE-1. The example 22.

$\vartriangleright$ Example 22:  Displays the name of all the variables used in the current unit.

   SCRIPT ShowSymbolTable() 
     nb := $cunit.SYMTABSIZE 
     cpt := 0 
     WHILE(cpt < nb) 
       fsym := $cunit.SYMTABENTRY(cpt)
       IF (fsym.TYPEOFSYMBOL == "variable") THEN 
         PRINT fsym.LOWERNAME
       ENDIF 
       cpt := cpt + 1 
     ENDWHILE
   ENDSCRIPT

  $\blacktriangleright$ f.LOWERNAME
 
The expression returns a string that contains the lowercase name associated to the fsym f (see example 22). The value FALSE is returned when f does not reference a valid declaration in the unit.
  $\blacktriangleright$ f.NATUREOFSYMBOL
 
The expression   returns a string that contains the nature of the symbol f (see example 22). The value FALSE is returned when f does not reference a valid declaration. The most common natures are "variable", "parameter", "label", "function" and "subroutine".
  $\blacktriangleright$ f.ISPOINTER
 
The expression   returns the value TRUE if the fsym s references a pointer else returns FALSE.
  $\blacktriangleright$ f.FUNKIND
 
The expression   returns the nature of the function defined by the fsym f. The string "intrinsic" is returned for the intrinsic functions.
  $\blacktriangleright$ f.SCALARVALUE
 
The expression returns the declared scalar value for the symbol table entry corresponding to the fsym f. The value FALSE if f does not reference a parameter or a scalar variable with a predefined value.
  $\blacktriangleright$ s.INSTATUS
 
The expression returns a string that indicates if the symbol s that references a parameter of the unit, is read. The strings "y", "n" and "?" means respectively yes, no and maybe. The value FALSE is returned if s does not reference a parameter of the unit.
  $\blacktriangleright$ s.OUTSTATUS
 
The expression returns a string that indicates if the symbol s that references a parameter of the unit, is written. The strings "y", "n" and "?" means respectively yes, no and maybe. The value FALSE is returned if s does not reference a parameter of the unit.

  $\blacktriangleright$ DECLARE(decl)
 
The   statement inserts the fortran declaration specified by the string decl into the current unit.

$\vartriangleright$ Example 23:A set of declarations

 

   DECLARE("include 'mpif.h'") 
   DECLARE("integer ") 
   DECLARE("include 'mpif.h'")

  $\blacktriangleright$ REBUILDSYMTAB(unit)
 
The   statement rebuild the internal symbol table associated to the specified unit. This symbol table contains all informations about the fortran declarations. It must be regenerated to validate all changes in the declarations.

$\vartriangleright$ Example 24:increase the first dimension of the variable X.

 

   d1 := "X".DIMENSION(1) 
   newd1 := PARSEEXPR("%e+1",d1) 
   d1 <- newd1 
   REBUILDSYMTAB($cunit) 
   PRINT "X".DIMENSION(1)

  $\blacktriangleright$ var.TYPE
 
The   expression returns a string that describes the elementary type of the fortran variable of name var.

$\vartriangleright$ Example 25:With the Fortran declaration integer X(m,n), the expression "X".TYPE returns the string "integer".

  $\blacktriangleright$ var.TYPESIZE
 
The   expression returns the size in bytes of the type given by var.TYPE.

$\vartriangleright$ Example 26:With the Fortran declaration integer X(m,n), the expression "X".TYPESIZE returns 4 (i.e. if the size of default integer is 4).

  $\blacktriangleright$ var.DIMENSION
 
The   expression returns the number of declared dimensions for the variable of name var. The value 0 is returned for scalar variables.

$\vartriangleright$ Example 27:With the Fortran declaration integer X(m,n), the expression "X".DIMENSION returns 2.

  $\blacktriangleright$ var.DIMENSION(n)
 
The  expression returns the dimension of rank n of the variable of name var. The AST returned by this attribute is not a copy ; it is the original.

$\vartriangleright$ Example 28:Exchange the 2 dimensions of the selected variable name.

         

  SCRIPT exchangeDims () 
    var := $csel 
    
    IF (var.VARIANT == "array_decl") THEN 
      // Select the name of the declaration 
      var := var.CHILD(1)  
    ENDIF 
    
    IF (var.VARIANT == "name") THEN 
      // Get the variable name because we do not 
      // to apply the transformation to an array 
      // access such as A(i,j)   
      varname := var.SYMBOL 
      dim1 := COPY(varname.DIMENSION(1))  
      dim2 := COPY(varname.DIMENSION(2))  
      varname.DIMENSION(1) <- dim2  
      varname.DIMENSION(2) <- dim1 
    ENDIF
  ENDSCRIPT

  $\blacktriangleright$ var.LOWERB
 
The   expression returns the lower bound of an array dimension (i.e. a column expression such as m:n). When the dimension is not a column expression then var.LOWERB returns the integer constant 1.

$\vartriangleright$ Example 29:Some examples of results given by the attribute LOWERB. The target is underlined in each fortran expression:


AAAA AAAAAAAAAAAAA AA AAAAAAAAA 
integer A(x+1) $\rightarrow$ 1 (the constant)
integer B(m:n) $\rightarrow$ m (a AST)
print *,C(1:n) $\rightarrow$ 1 (a AST)
print *,n $\rightarrow$ 1 (the constant)

  $\blacktriangleright$ var.UPPERB
 
The   expression returns the upper bound of an array dimension (i.e. a column expression such as m:n). When the dimension of an expression var is not a column expression then var.UPPERB returns var.
$\vartriangleright$ Example 30:Creates the fortran expression that gives the number of elements in the first dimension of the variable X. It returns b-a+1 when the variable X is declared by X(a:b)      
       varname := "X"  
       lb :=  varname.DIMENSION(1).LOWERB
       ub :=  varname.DIMENSION(1).UPPERB
       expr := PARSEEXPR("%e-%e+1",ub,lb)

  $\vartriangleright$ Warning: In the left part of a AST assignment (see the previous example), the DIMENSION attribute has an effect on the symbol table. A common error is to insert a temporary variable.
$\vartriangleright$ Example 31:Both forms are not equivalent. The second does not update the symbol table and so is incorrect.

         varname.DIMENSION(1) <- dim
         tmp := varname.DIMENSION(1) 
         tmp <- dim

  $\vartriangleright$ Remark: The attribute DIMENSION can be applied to an array acces (i.e. in a statement such as A=B(i,j)) to get or to change the indexes i or j. Then, it has no effect on the symbol table.


next up previous contents index
Next: Type casts Up: Statements Previous: Use the pattern-matching facilities
Yann Mevel
1999-04-30