Oberon || Library || Module Index || Search Engine || Definition || Module


Ulm's Oberon Library:
Scopes


NAME

Scopes - flat but nestable persistent name spaces

SYNOPSIS

TYPE Identifier = ConstStrings.String;
TYPE Object = CompilerObjects.Object;
TYPE Location = CompilerObjects.Location;


TYPE Scope = POINTER TO ScopeRec; TYPE ScopeRec = RECORD (CompilerObjects.ObjectRec) END;

TYPE OpenProc = PROCEDURE (scope: Scope; VAR innerScope: Scope); TYPE CloseProc = PROCEDURE (scope: Scope); TYPE AddProc = PROCEDURE (scope: Scope; ident: Identifier; loc: Location; object: Object); TYPE UseProc = PROCEDURE (scope: Scope; ident: Identifier; loc: Location; VAR object: Object); TYPE LookupProc = PROCEDURE (scope: Scope; ident: Identifier; VAR object: Object) : BOOLEAN; TYPE LocalLookupProc = PROCEDURE (scope: Scope; ident: Identifier; VAR object: Object) : BOOLEAN; TYPE CutProc = PROCEDURE (scope: Scope); TYPE GetIteratorProc = PROCEDURE (scope: Scope; VAR it: Iterators.Iterator); TYPE Interface = POINTER TO InterfaceRec; TYPE InterfaceRec = RECORD (Objects.ObjectRec) open: OpenProc; close: CloseProc; add: AddProc; use: UseProc; lookup: LookupProc; localLookup: LocalLookupProc; cut: CutProc; getIterator: GetIteratorProc; END;

PROCEDURE Init(scope: Scope; if: Interface);

PROCEDURE Open(scope: Scope; VAR innerScope: Scope); PROCEDURE Close(scope: Scope); PROCEDURE Cut(scope: Scope);

PROCEDURE Level(scope: Scope) : INTEGER; PROCEDURE Add(scope: Scope; ident: Identifier; loc: Location; object: Object); PROCEDURE Use(scope: Scope; ident: Identifier; loc: Location; VAR object: Object); PROCEDURE Lookup(scope: Scope; ident: Identifier; VAR object: Object) : BOOLEAN; PROCEDURE LocalLookup(scope: Scope; ident: Identifier; VAR object: Object) : BOOLEAN; PROCEDURE GetIterator(scope: Scope; VAR it: Iterators.Iterator);

DESCRIPTION

Scopes provides a general abstraction for flat but nestable persistent name spaces that are suited for compilers of programming languages. A standard implementation is given by StdScopes.

Init initializes scope and associates it with the given interface if. The interface procedures are expected to meet the specifications following:

open: PROCEDURE(scope: Scope; VAR innerScope: Scope);
create a nested scope of scope and return it in innerScope. Nested scopes have the property that identifiers that were not found in them will be looked up in the parent scope.

close: PROCEDURE(scope: Scope);
freeze scope (i.e. further additions to it must not be accepted), and generate error events if necessary.

add: PROCEDURE(scope: Scope; ident: Identifier; loc: Location; object: Object);
add object under the name of ident to scope. The location loc, if non-NIL, may later be used as reference in case of error messages.

use: PROCEDURE(scope: Scope; ident: Identifier; loc: Location; VAR object: Object);
perform a lookup of ident in scope and mark this identifier as used in this scope if found. Note that implementations are expected to detect scope overlaps, i.e. identifiers that have been used from an outer scope must not be later added to the same scope. Example:
PROCEDURE OuterScope;
   TYPE Outer = INTEGER;
   PROCEDURE InnerScope;
      TYPE Inner = Outer; (* using ``Outer'' in inner scope *)
      TYPE Outer = INTEGER; (* redefining ``Outer'' in inner scope *)
   END InnerScope;
END OuterScope;
object must be set to NIL if the lookup was not successful.

lookup: PROCEDURE(scope: Scope; ident: Identifier; VAR object: Object) : BOOLEAN;
lookup for ident in scope (and its outer scopes) without marking it. If successful, TRUE is to be returned and the object found to be stored into object.

localLookup: PROCEDURE(scope: Scope; ident: Identifier; VAR object: Object) : BOOLEAN;
lookup for ident in scope without considering its outer scopes and without marking it. If successful, TRUE is to be returned and the object found to be stored into object.

cut: PROCEDURE(scope: Scope);
untie the association of scope to its outer scope, i.e. lookups will be performed locally only.

getIterator: PROCEDURE(scope: Scope; VAR it: Iterators.Iterator);
store an iterator into it that allows to iterate through all objects added to scope.

Open creates a scope of the same implementation as scope that is nested into it, i.e. lookups that do not succeed in the current scope will be continued in the outer scope. Close freezes and finishes a scope, i.e. further additions of identifiers to scope are no longer permitted and error events will be generated. Cut unties a scope from its outer scope, i.e. lookups will be performed locally on scope only. Note that Cut does not change the nesting level of a scope.

Level returns the nesting level of a scope. Outmost scopes (i.e. those initialized by Init) have a nesting level of 0.

Add inserts object (declared at loc) with name ident into scope. loc may be NIL if not available. An error event is generated immediately or later on Close in case of errors.

Use looks for ident in scope (referenced at loc) and marks it as used. The object, if found, is stored into object; otherwise, it is set to NIL. loc may be NIL if not available. An error event is generated immediately or later on Close in case of errors.

Lookup looks for ident in scope and its associated outer scopes without marking it and without generating any error events. If successful, TRUE is returned and the object is stored into object. LocalLookup works like Lookup but does not consider the outer scopes of scope.

GetIterator returns an iterator in it that allows to iterate through all objects (not names!) added to scope. GetIterator must not be called before Close has been called for scope.

DIAGNOSTICS

Implementations of Scopes are expected to raise error events (usually those of CompilerErrors) and to relate them to scope if

Implementations are free to postpone the generation of error events until Close is called for a scope to allow for enhanced error reports.

SEE ALSO

CompilerErrors
events for compilation errors
CompilerObjects
base type of compiler objects
PersistentObjects
operations for persistent objects
StdScopes
standard implementation of Scopes using hashes and CompilerErrors

Edited by: borchert, last change: 2000/05/27, revision: 1.2, converted to HTML: 2000/05/27

Oberon || Library || Module Index || Search Engine || Definition || Module