The
ALIAS directive
associates the name of a subroutine, function, entry, or common
block with an external name and specifies the parameter-passing
conventions of routines written in other languages.
Syntax
!$HP$ ALIAS name [= external-name ] [(arg-pass-mode-list)]
- name
is the name used by the program to refer to a subroutine,
function, or procedure entry point—but not to an internal
subroutine. If name is enclosed by slashes,
it is a common block name.
- external-name
is a character constant that specifies a standard
symbolic name.
- arg-pass-mode-list
is used only when name
is that of a procedure that takes arguments. The items in the list
specify how the corresponding actual argument are to be passed.
The items can be either of the following built-in functions:
%VAL:
pass the value of the actual argument
%REF:
pass the address of the actual argument
There must be as many items in the list as there are arguments
in the procedure, they must be separated by commas, and they must
correspond positionally to the arguments.
Description and restrictions
The $HP$ ALIAS
directive serves two purposes:
It provides a way to associate the
name used by your program to refer to a subroutine, function, entry,
or common block with a distinct external name. This feature is especially
useful when you want to access a variety of different graphics device
drivers from the same source code so that different hardware configurations
can be supported.
When
used in conjunction with the %VAL
and %REF built-in
functions, it provides a way to direct the compiler to use the appropriate
parameter passing conventions to communicate with routines written
in other high-level languages.
external-name should never conflict
with the name of an HP-UX system routine (described in sections
2 and 3 of the HP-UX Reference) or with a
Fortran 90 library routine (for example, OPEN,
READ, or CLOSE).
The $HP$ ALIAS
directive applies to subroutines, entries, and functions that are
used externally. It does not apply to the main program unit.
%VAL is
a built-in function that specifies that the value of the actual
argument is to be passed to the called procedure. You can use this
parameter with all types of arguments. However, when used with a
procedure name, it has no effect; a pointer to the procedure is
still passed.
%REF specifies
that the address of the actual argument is to be passed to the called
procedure. For non-character arguments, this is the default. For
character arguments, %REF
disables the passing of the hidden length parameter.
When %VAL
and %REF are
used with the CALL
statement, they override the specification in the $HP$ ALIAS
directive. For more information about these built-in functions,
see the description of the CALL
statement in Chapters 7 and 10.
Note the following restrictions:
Attempts to redefine $HP$ ALIAS
names generate warning messages.
The compiler always uses external-name
exactly as it is entered. No case transformations occur, and no
underscore is appended. The +ppu
and +uppercase
compile-line options do not apply to external names specified by
the $HP$ ALIAS
directive; see Chapter 13 for information about compile-line options.
Local and global usage
The $HP$ ALIAS
directive can be used either locally or globally, as follows:
The $HP$ ALIAS
directive has local application only—that is, its effect
is limited to a particular program unit—if it appears within
the boundaries of that program unit. To have local application only,
the directive must appear after any PROGRAM,
SUBROUTINE, or
FUNCTION statement
and before the first occurrence of name
in the target program unit.
The $HP$ ALIAS
directive has global application—that is, it applies to
all subsequent program units—if it appears outside and
before the boundaries of those program units to which it is to apply.
Examples
The $HP$ ALIAS
directive is especially useful when calling a routine in a language
that uses different conventions than Fortran. The following examples
illustrate how to use the $HP$ ALIAS
directive to resolve differences with:
Argument-passing conventions
Case sensitivity
Names in HP Fortran 90 are not
case sensitive; that is, the compiler converts all names to lowercase.
This means that if you reference a routine in a language that is
case sensitive and the routine name contains uppercase letters,
a call to that routine in HP Fortran 90 will result in an unresolved
reference—unless you use the $HP$ ALIAS
directive to redefine the name in all lowercase letters, as in the
following example:
!$HP$ ALIAS printnames = 'PrintNames' |
Argument-passing conventions
By
default, HP Fortran 90 assumes that all parameters in a subroutine
or function call are passed by reference; that is, the call passes
the addresses of the parameters, not their values. On the other
hand, C code assumes that parameters are passed by value; that is,
the current value of the actual parameter is passed to the called
routine. Without the $HP$ ALIAS
directive, it would be difficult to call a C routine from a Fortran
program.
For
example, suppose you want to call the system routine calloc
(see the malloc(3C) man page) to obtain dynamic
memory. The man page describes the calling sequence as:
char *calloc(unsigned nelem, unsigned elsize); |
It would be difficult, using standard Fortran 90 constructs,
to provide actual parameters corresponding to nelem
and elsize because
HP Fortran 90 always passes addresses. The $HP$ ALIAS
directive can solve this problem by directing the compiler to generate
call-by-value actual parameters:
!$HP$ ALIAS calloc(%VAL, %VAL) |
Strings
Programs written in C expect
strings to be terminated with the null character ('\0').
But HP Fortran 90 programs pass a hidden length parameter to indicate
the end of a string argument. Thus, if you want to pass a string
from HP Fortran 90 to a C language function, you must explicitly
append the null to the string and suppress the hidden length parameter.
The $HP$ ALIAS
directive enables you to pass the string from Fortran to C. For
example, consider the following C routine:
The ALIAS
directive in the following HP Fortran 90 program enables the string
to be passed to c_rout:
! Append a null to the string so that C can |
CALL c_rout(name//char(0)) |
For more information
For more information about the %REF
and %VAL built-in
functions, see the description of the CALL
statement in Chapter 10.