Class Context
- Direct Known Subclasses:
CompilationUnitContext
,LocalContext
Because scopes are lexically nested in iota, the environment can be seen as a stack of contexts, each of which is a mapping from names to their definitions (Defns). A Context keeps track of its (most closely) surrounding context, its surrounding compilation unit context, and a map from names to definitions in the level scope that the Context represents. Contexts are created for the compilation unit (a CompilationUnitContext), each method (a methodContext), and each block (a LocalContext). If we were to add the for-statement to iota, 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, ie, back to the JCompilationUnit object at the root.
Part of this structure is built during pre-analysis and the rest of it is built during analysis.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected 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
ConstructorsModifierConstructorDescriptionprotected
Context
(Context surrounding, 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 compilation unit context.Returns the type that defines this context.Returns the definition for a name in the current (or surrounding) 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
-
compilationUnitContext
The compilation unit context (for the whole source program or file). -
surroundingContext
The surrounding context (scope). -
entries
Map of (local variable, formal parameters, type) names to their definitions.
-
-
Constructor Details
-
Context
Constructs a Context.- Parameters:
surrounding
- the surrounding context (scope).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.
-
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.- Returns:
- the type that defines this context.
-
surroundingContext
Returns the surrounding context (scope) in the stack of contexts.- Returns:
- the surrounding context.
-
compilationUnitContext
Returns the surrounding compilation unit context.- 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.
-