Package iota

Class Context

java.lang.Object
iota.Context
Direct Known Subclasses:
CompilationUnitContext, LocalContext

class Context extends Object
A Context encapsulates the environment in which an AST is analyzed. It represents a scope; the scope of a variable is captured by its context. It's the symbol table.

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 Details

    • compilationUnitContext

      protected CompilationUnitContext compilationUnitContext
      The compilation unit context (for the whole source program or file).
    • surroundingContext

      protected Context surroundingContext
      The surrounding context (scope).
    • entries

      protected Map<String,Defn> entries
      Map of (local variable, formal parameters, type) names to their definitions.
  • Constructor Details

    • Context

      protected Context(Context surrounding, CompilationUnitContext compilationUnitContext)
      Constructs a Context.
      Parameters:
      surrounding - the surrounding context (scope).
      compilationUnitContext - the compilation unit context (for the whole source program or file).
  • Method Details

    • addEntry

      public void addEntry(int line, String name, Defn definition)
      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

      public Defn lookup(String name)
      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

      public void addType(int line, Type type)
      Adds the given type to the compilation unit context.
      Parameters:
      line - line number of type declaration.
      type - the type we are declaring.
    • definingType

      public Type definingType()
      Returns the type that defines this context.
      Returns:
      the type that defines this context.
    • surroundingContext

      public Context surroundingContext()
      Returns the surrounding context (scope) in the stack of contexts.
      Returns:
      the surrounding context.
    • compilationUnitContext

      public CompilationUnitContext compilationUnitContext()
      Returns the surrounding compilation unit context.
      Returns:
      the compilation unit context.
    • methodContext

      public MethodContext 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

      public Set<String> names()
      Returns a set containing the names declared in this context.
      Returns:
      a set containing the names declared in this context.
    • toJSON

      public void toJSON(JSONElement json)
      Adds information pertaining to this context to the given JSON element.
      Parameters:
      json - JSON element.