next up previous
Next: Operational Specifications Up: Identifier classification Previous: Nested environments

Procedure parameters

According to Section 6.1.2.1 of the standard, an identifier declared in the parameter list of a function of a function has block scope, which terminates at the } closing the function definition. That means we need to create an environment at the beginning of the parameter list and discard it at the end of the function definition.

The problem is that when the beginning of the parameter list is encountered there is no way to tell whether the declarator is part of a function definition or not.

If the declarator is part of a nested declaration, then its parameter list is a function prototype and the environment can be discarded at the end of the parameter list.

If the declarator is at the outermost level of an external declaration, then its parameter list may be the parameter list of a function and the environment cannot be discarded at the end of the parameter list. It must be discarded at the end of the declaration.

A particular external declaration may or may not have a declarator specifying a parameter list. The current environment should be discarded at the end of an external declaration if and only if the declaration actually has a parameter list.

The necessary information can be provided by a variable that distinguishes four possible states:

State variable definitions[51]:

typedef enum {
  External,
  NoParameters,
  HasParameters,
  IsNested
} DeclarationStateValues;

static int DeclarationState = External;
This macro is defined in definitions 51 and 54.
This macro is invoked in definition 55.

Arbitrary nesting is handled without a stack by incrementing the current value of DeclarationState by IsNested: A value greater than or equal to IsNested indicates the nested state, but the state at the top level is also preserved. This is the reason that DeclarationState cannot be declared as a variable of type DeclarationStateValues; with arbitrary nesting, DeclarationState takes on values that are not valid DeclarationStateValues.

Parameters?[52] \ensuremath{(\diamond1)}:
BeginParameters( \ensuremath{\diamond1});
This macro is invoked in definitions 16 and 17.
End parameters[53]:
EndDeclaration();
This macro is invoked in definitions 12, 17, and 27.

State variable definitions[54]:

void
BeginParameters(int p)
{
  if (DeclarationState != External) DeclarationState += IsNested;
  else if (!p) DeclarationState = NoParameters;
  else {
    DeclarationState = HasParameters;
    New region[49]
    Nest[34](`identifier',`1')
  }
}

void
EndDeclaration()
{
  if (DeclarationState > IsNested) DeclarationState -= IsNested;
  else {
    if (DeclarationState == HasParameters) {
      Restore[36]
      End region[50]
    }
    DeclarationState = External;
  }
}
This macro is defined in definitions 51 and 54.
This macro is invoked in definition 55.


next up previous
Next: Operational Specifications Up: Identifier classification Previous: Nested environments
2008-08-30