next up previous
Next: Scope of a simple Up: Name Analysis Previous: Name Analysis

Packages

A package consists of a number of compilation units. A compilation unit automatically has access to all types declared in its package. To model this behavior, we associate a definition table key with each package, and make that key available in each compilation unit via the compilation unit's PackageDeclarationOpt child:

Packages[4]:
TREE SYMBOL PackageDeclarationOpt: PkgKey: DefTableKey;

TREE SYMBOL PackageName INHERITS PkgIdDefScope END;

RULE: PackageDeclarationOpt ::= 'package' PackageName ';' COMPUTE
  PackageDeclarationOpt.PkgKey=PackageName.PkgKey;
END;
This macro is defined in definitions 4, 5, 8, and 9.
This macro is invoked in definition 2.

A compilation unit that has no package declaration is part of an unnamed package. We have chosen to support an arbitrary number of unnamed packages, each containing a single compilation unit:

Packages[5]:
RULE: PackageDeclarationOpt ::= COMPUTE
  PackageDeclarationOpt.PkgKey=NewKey();
END;
This macro is defined in definitions 4, 5, 8, and 9.
This macro is invoked in definition 2.

Instantiate required modules[6]:

$/Name/AlgScope.gnrc +instance=Pkg +referto=Pkg :inst
This macro is defined in definitions 3, 6, 7, 27, 30, and 36.
This macro is invoked in definition 1.

The members of a package are class and interface types, which are declared in compilation units of the package. We model this behavior by the Pty name space.

Instantiate required modules[7]:

$/Name/AlgScope.gnrc +instance=Pty +referto=Pty :inst
This macro is defined in definitions 3, 6, 7, 27, 30, and 36.
This macro is invoked in definition 1.

All of the declarations of package members are children of the TypeDeclarationsOpt child of a compilation unit. The Pty name space must, however, define the full set of type scopes to hide lower-level definitions:

Packages[8]:
TREE SYMBOL TypeDeclarationsOpt  INHERITS PtyRangeScope END;
TREE SYMBOL ClassBody            INHERITS PtyRangeScope END;
TREE SYMBOL InterfaceBody        INHERITS PtyRangeScope END;
TREE SYMBOL MethodDeclaration    INHERITS PtyRangeScope END;
TREE SYMBOL Block                INHERITS PtyRangeScope END;
TREE SYMBOL SwitchBlockStatement INHERITS PtyRangeScope END;

TREE SYMBOL TypeIdDef            INHERITS PtyIdDefScope END;
This macro is defined in definitions 4, 5, 8, and 9.
This macro is invoked in definition 2.

A single environment is used for all compilation units in a single package. The package key is used to retrieve that environment:

Packages[9]:
RULE: CompilationUnit ::=
        PackageDeclarationOpt
        ImportJavaLang ImportDeclarationsOpt TypeDeclarationsOpt
COMPUTE
  TypeDeclarationsOpt.PtyEnv=
    PackagePtyScope(PackageDeclarationOpt.PkgKey,INCLUDING Goal.PtyEnv);
END;
This macro is defined in definitions 4, 5, 8, and 9.
This macro is invoked in definition 2.

PackagePtyScope obtains the package's environment from the definition table, if it exists. Otherwise, it returns a new environment and stores that environment as the package's environment:

Name.pdl[10]:

PtyScope: Environment [Package];        "envmod.h"

Environment Package(DefTableKey key, Environment env)
{ if (key == NoKey) return NoEnv;
  if (!ACCESS) VALUE = NewScope(env);
  return VALUE;
}
This macro is attached to a product file.


next up previous
Next: Scope of a simple Up: Name Analysis Previous: Name Analysis
2008-09-11