A.2 Type m4 Macros

The main documentation of the Type m4 Macros contains additional explanation of this code listing.

dnl
dnl Author: Michael L. Hall
dnl         P.O. Box 1663, MS-D409, LANL
dnl         Los Alamos, NM 87545
dnl         ph: 505-665-4312
dnl         email: hall@lanl.gov
dnl 
dnl Created on: 11/12/98
dnl Date:       03/21/00, 17:16:07
dnl Version:    4.0


dnl Define a real$kind private macro based on the desired precision.

ifdef([SINGLE],[
  define([real$kind],[1.0e0])
],[
  ifdef([UNICOS],[
    define([real$kind],[1.0e0])
  ],[
    define([real$kind],[1.0d0])
  ])
])


dnl Define a private macro to expand to the pointer and dimensioning 
dnl info. 

define([pnt$dim],
       [ifelse(
         [$1], [], 
           [],
         [$1], [0], 
           [],
         [ifelse([$2], [], [, pointer]), dimension(:forloop([i],2,$1,[,:]))])])


dnl Define a type macro which interprets real, integer, logical
dnl and character types correctly and leaves other types unchanged.
dnl
dnl Arguments to the type macro:
dnl   $1 - The main type: real, integer, logical or character. If the
dnl        type is character, the next argument is the character
dnl        length (and all the remaining argument numbers are
dnl        incremented). If another word appears as argument 1 ($1),
dnl        then no action is taken, which is correct for a derived
dnl        type.
dnl   $2 - The rank of the variable, between zero and seven (default =
dnl        zero).
dnl   $3 - If anything appears here, then the variable is *not* declared 
dnl        to be a pointer, but otherwise it is. An exception is that 
dnl        scalars are never declared to be pointers.

define([type],
       [ifelse(
         [$1], [real],
           [real (kind=KIND(m4_indir(real$kind)))m4_indir(pnt$dim,$2,$3)],
         [$1], [integer],
           [integer (kind=KIND(1))m4_indir(pnt$dim,$2,$3)],
         [$1], [logical],
           [logical (kind=KIND(.true.))m4_indir(pnt$dim,$2,$3)],
         [$1], [character],
           [character (len=$2)m4_indir(pnt$dim,$3,$4)],
         [$1], [],
           [[type]],
         [[type]($*)])])


dnl Define a changetype macro which interprets real and integer type 
dnl conversions correctly and emits an error message if incorrectly 
dnl called.

define([changetype],
       [ifelse(
         [$1], [real],
           [REAL($2, KIND(m4_indir(real$kind)))],
         [$1], [integer],
           [INT($2, KIND(1))],
         [m4_die([Error in changetype command.])])])


Michael L. Hall