previous next top complete contents complete index framed top this page unframed
The attributes supplied by a declaration do not change; unless the identifier is redeclared, they are associated with the identifier for the remainder of the compilation, and they may not be changed at runtime.
Things that may be declared are simple variables, ARRAY variables (dynamic and inplace), PROCEDUREs, CLASSes, and MODULEs. Any declaration may occur as an outer declaration, i.e., a declaration between the initial BEGIN and final END of a MODULE outside of any PROCEDURE within the MODULE. In addition, simple variables and ARRAY variables may be declared within a PROCEDURE after the initial BEGIN of a PROCEDURE and before the first statement in the PROCEDURE; such declarations are called local declarations, since they are local to the PROCEDURE.
Macros are considered to be defined rather than declared. A macro definition both declares the macro identifier to the compiler and gives it a value. Macro definitions may appear where ordinary declarations may not. See Chapter 15 for a complete discussion of macros.
Declarations are separated from one another with semicolons. A declaration followed by a statement in a PROCEDURE body (see Section 7.1) is separated from the statement with a semicolon; see Figure 6–1.
An empty declaration consists of nothing at all and may occur wherever other declarations may occur. Empty declarations permit extra semicolons to appear in program text to make it easier to read.
Figure 6–1. Where Declarations May Occur
| BEGIN "modNam" class declaration; # outer declaration ; # empty declaration PROCEDURE p; # (outer) procedure declaration BEGIN variable declaration; # local variable # a class, module, or procedure declaration is illegal # here inside the procedure statements of procedure body # the scope of the local declarations for p ends here END; module declaration; # outer declaration class declaration; # outer declaration procedure declaration; # procedure declaration; may contain local declarations variable declaration; # outer declaration procedure declaration # outer declaration END "modNam" |
This chapter describes simple variable declarations, the scope of identifiers, and qualifiers that may be used in declarations to provide additional information about the entities being declared. ARRAY declarations are described in Section 12.3, PROCEDURE declarations in Section 7.1, CLASS declarations in Section 9.1, and MODULE declarations in Section 11.2.
6.1. Scope of Identifiers
A MODULE's outer declarations
associate each declared identifier with an entity
(variable, PROCEDURE, CLASS, or MODULE)
that can be accessed
throughout the rest of the MODULE.
Variables declared among a MODULE's
outer declarations are referred to as outer variables.
Each entity declared in an outer declaration must have a unique name among all entities declared in outer declarations in the same MODULE.
Identifiers can be obtained from special symbol tables called intmods. Identifiers obtained from intmods act like outer declarations; see Chapter 13.
An entity declared with an identifier v inside a PROCEDURE cannot be accessed outside the PROCEDURE. Such an entity is said to be local to the PROCEDURE, and variables declared locally are called local variables. If the identifier v is also declared in the outer declarations, the v accessed within the PROCEDURE is the one declared in the PROCEDURE. When the end of the PROCEDURE body occurs, all locally declared entities such as v “disappear”, and if v had been declared in the outer declarations, the outer v is once again visible throughout the rest of the MODULE.
Each entity declared in a local declaration must have a unique name
within the PROCEDURE.
6.2. Simple Variable Declarations
A simple variable declaration declares one or more variables.
The values of the variables
may be used during program execution according to their data
types. The general form of a simple variable declaration is:
type v1, ..., vn
where the vi are identifiers, and type is BOOLEAN, INTEGER, LONG INTEGER, REAL, LONG REAL, BITS, LONG BITS, STRING, POINTER, $PROCVAR, $RECORD ($RECORD must be followed by a parenthesized CLASS name), ADDRESS, or CHARADR. ADDRESS and POINTER may be followed by a parenthesized CLASS name as described in Section 9.2, and $PROCVAR by a parenthesized model PROCEDURE name as described in Section 8.1.
For example:
INTEGER i,t,num;
declares three variables (i, t, and num) that may be assigned INTEGER values.
The qualifier OWN or $SHARED
may precede a variable declaration, in which case
the effect is as described in Section 6.4 or Section 6.5.
6.3. Qualifiers
A qualifier is used in a declaration to provide additional information
about the entity being declared.
If any qualifiers are used in a declaration, they precede all other parts of the declaration. When more than one qualifier is used, the order of the qualifiers themselves is unimportant.
The OWN and $SHARED qualifiers (described below) may be used only in simple variable and ARRAY declarations.
The following qualifiers may be used only in PROCEDURE declarations: FORWARD, INITIAL, FINAL, GENERIC, COMPILETIME, INLINE, SPECIAL, and $ALWAYS. They are described in Section 7.11.
The following qualifiers may be used only in PROCEDURE parameter
declarations: USES, PRODUCES, MODIFIES,
OPTIONAL,
REPEATABLE, and $REFERENCE.
They are described in Section 7.5.
6.4. OWN Qualifier
An OWN variable
is a variable of which the value is retained
until the data section of the MODULE in which it is declared
is deallocated (see Section 11.6).
All outer variables are therefore OWN variables.
The values of local variables declared without the OWN qualifier
are lost when the associated invocation of the PROCEDURE in which
they are declared is terminated.
A local variable declared with the OWN qualifier retains its value from execution to execution of the PROCEDURE, like an outer variable, but its identifier may not be used in the source text outside the PROCEDURE in which it is declared. Such local variables are referred to as local OWN variables.
OWN variables are initialized to Zero when the data section they are in is allocated. Local variables of the types POINTER, STRING, $PROCVAR, inplace record, and inplace ARRAY are also initialized to Zero when the PROCEDURE in which they are declared is invoked; local variables (other than local OWN variables) of other data types are not automatically initialized. Use of an uninitialized variable's value has undefined effects.
In this code:
PROCEDURE p;
BEGIN
OWN INTEGER n;
n .+ 1;
...
END
n counts how many times the PROCEDURE p has been called. If n's declaration were not qualified with OWN, its value would be lost whenever the PROCEDURE was exited. n could have been declared outside the PROCEDURE, which would achieve the same effect of preserving the variable's value across PROCEDURE invocations, but that would obscure the fact that n is used only within the PROCEDURE.
Declaring an outer variable with the OWN qualifier is legal but
has no effect on the way the variable is treated.
6.5. $SHARED Qualifier
The declaration of an outer (non-interface) variable or local OWN
variable may be preceded with the keyword $SHARED.
The value of such a variable is shared among all data sections (both
bound and nonbound) for the MODULE during a given execution.
Like outer variables, $SHARED variables initially have the value
Zero.
$SHARED variables are allocated when the first data section of the MODULE is allocated. They are allocated in a structure called the $SHARED data section, which is shared by all instances of the MODULE (and is not to be confused with a normal data section; it contains only the MODULE's $SHARED variables). The $SHARED data section (and the $SHARED variable values in it) persists until the end of the MAINSAIL execution or until the MODULE or STRING form of dispose is used to dispose the MODULE (disposing or unbinding any single data section of the MODULE does not deallocate the $SHARED data section).
A local OWN variable declared with just the keyword $SHARED is syntactically like an OWN variable; i.e., its identifier can be used only within the PROCEDURE, even though its value persists between invocations of the PROCEDURE.
If a variable is declared with both the $SHARED and OWN qualifiers, the effect is the same as if it were declared with just the $SHARED qualifier.
An example of a $SHARED variable declaration:
$SHARED INTEGER i; # Accessible from all instances of
# the current MODULE
MAINSAIL Language Manual, Chapter 6