Class Context
- Direct Known Subclasses:
ClassContext
,CompilationUnitContext
,LocalContext
Because scopes are lexically nested in Java (and so in j--), the environment can be seen as a stack of contexts, each of which is a mapping from names to their definitions (IDefns). A Context keeps track of its (most closely) surrounding context, its surrounding class context, and its surrounding compilation unit context, and as well as a map from names to definitions in the level of scope that the Context represents. Contexts are created for the compilation unit (a CompilationUnitContext), a class (a ClassContext), each method (a MethodContext), and each block (a LocalContext). If we were to add the for-statement to j--, we would necessarily create a (local) context.
From the outside, the structure looks like a tree strung over the AST. But from any location on the AST, that is from any point along a particular branch, it looks like a stack of context objects leading back to the root of the AST, that is, back to the JCompilationUnit object at the root.
Part of this structure is built during pre-analysis; pre-analysis reaches only into the type (for example a class) declaration for typing the members; pre-analysis does not reach into the method bodies. The rest of it is built during analysis.
-
Field Summary
Modifier and TypeFieldDescriptionprotected ClassContext
The surrounding class context.protected CompilationUnitContext
The compilation unit context (for the whole source program or file).Map of (local variable, formal parameters, type) names to their definitions.protected Context
The surrounding context (scope). -
Constructor Summary
ModifierConstructorDescriptionprotected
Context
(Context surrounding, ClassContext classContext, CompilationUnitContext compilationUnitContext) Constructs a Context. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds an entry to the symbol table, binding a name to its definition in the current context.void
Adds the given type to the compilation unit context.Returns the surrounding class context.Returns the surrounding compilation unit context.Returns the type that defines this context (used principally for checking accessibility).Returns the definition for a name in the current (or surrounding) context, or null.lookupType
(String name) Returns the definition for a type name in the compilation unit context, or null.Returns the closest surrounding method context, or null (if we are not within a method).names()
Returns a set containing the names declared in this context.Returns the surrounding context (scope) in the stack of contexts.void
toJSON
(JSONElement json) Adds information pertaining to this context to the given JSON element.
-
Field Details
-
surroundingContext
The surrounding context (scope). -
classContext
The surrounding class context. -
compilationUnitContext
The compilation unit context (for the whole source program or file). -
entries
Map of (local variable, formal parameters, type) names to their definitions.
-
-
Constructor Details
-
Context
protected Context(Context surrounding, ClassContext classContext, CompilationUnitContext compilationUnitContext) Constructs a Context.- Parameters:
surrounding
- the surrounding context (scope).classContext
- the surrounding class context.compilationUnitContext
- the compilation unit context (for the whole source program or file).
-
-
Method Details
-
addEntry
Adds an entry to the symbol table, binding a name to its definition in the current context.- Parameters:
line
- the line number of the entry.name
- the name being declared.definition
- and its definition.
-
lookup
Returns the definition for a name in the current (or surrounding) context, or null.- Parameters:
name
- the name whose definition we're looking for.- Returns:
- the definition for a name in the current (or surrounding) context, or null.
-
lookupType
Returns the definition for a type name in the compilation unit context, or null.- Parameters:
name
- the name of the type whose definition we're looking for.- Returns:
- the definition for a type name in the compilation unit context, or null.
-
addType
Adds the given type to the compilation unit context.- Parameters:
line
- line number of type declaration.type
- the type we are declaring.
-
definingType
Returns the type that defines this context (used principally for checking accessibility).- Returns:
- the type that defines this context.
-
surroundingContext
Returns the surrounding context (scope) in the stack of contexts.- Returns:
- the surrounding context.
-
classContext
Returns the surrounding class context.- Returns:
- the surrounding class context.
-
compilationUnitContext
Returns the surrounding compilation unit context. This is where imported types and other types defined in the compilation unit are declared.- Returns:
- the compilation unit context.
-
methodContext
Returns the closest surrounding method context, or null (if we are not within a method).- Returns:
- the closest surrounding method context, or null.
-
names
Returns a set containing the names declared in this context.- Returns:
- a set containing the names declared in this context.
-
toJSON
Adds information pertaining to this context to the given JSON element.- Parameters:
json
- JSON element.
-