Package jminusminus

Class Context

java.lang.Object
jminusminus.Context
Direct Known Subclasses:
ClassContext, 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 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 Details

    • surroundingContext

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

      protected ClassContext classContext
      The surrounding class context.
    • compilationUnitContext

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

      protected Map<String,IDefn> 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

      public void addEntry(int line, String name, IDefn 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 IDefn 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.
    • lookupType

      public Type lookupType(String name)
      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

      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 (used principally for checking accessibility).
      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.
    • classContext

      public ClassContext classContext()
      Returns the surrounding class context.
      Returns:
      the surrounding class context.
    • compilationUnitContext

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

      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.