Package org.codehaus.groovy.transform.stc

# Class TypeCheckingContext

java.lang.Object
org.codehaus.groovy.transform.stc.TypeCheckingContext

public class TypeCheckingContext extends Object
Holds mutable state shared across a static type-checking visit.

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static class`
  `TypeCheckingContext.EnclosingClosure`
  Represents the context of an enclosing closure.

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `protected Set<MethodNode>`
  `alreadyVisitedMethods`
  Methods already visited during the current checking pass.
  `protected final Map<VariableExpression,List<ClassNode>>`
  `closureSharedVariablesAssignmentTypes`
  A map used to store every type used in closure shared variable assignments.
  `protected CompilationUnit`
  `compilationUnit`
  Compilation unit associated with the current type-checking run.
  `protected Map<Parameter,ClassNode>`
  `controlStructureVariables`
  Types tracked for variables introduced by control structures.
  `protected org.codehaus.groovy.transform.stc.DelegationMetadata`
  `delegationMetadata`
  Whenever a method using a closure as argument (typically, "with") is detected, this list is updated with the receiver type of the with method.
  `protected final LinkedList<BinaryExpression>`
  `enclosingBinaryExpressions`
  Stack of enclosing binary expressions.
  `protected final LinkedList<ClassNode>`
  `enclosingClassNodes`
  Stack of enclosing class nodes.
  `protected final LinkedList<TypeCheckingContext.EnclosingClosure>`
  `enclosingClosures`
  Stack of enclosing closures.
  `protected final LinkedList<Expression>`
  `enclosingMethodCalls`
  Stack of enclosing method-call expressions.
  `protected final LinkedList<MethodNode>`
  `enclosingMethods`
  Stack of enclosing methods.
  `protected final LinkedList<ErrorCollector>`
  `errorCollectors`
  Error collectors stacked for nested type-checking operations.
  `protected Map<VariableExpression,List<ClassNode>>`
  `ifElseForWhileAssignmentTracker`
  This field is used to track assignments in if/else branches, for loops and while loops.
  `protected boolean`
  `isInStaticContext`
  Indicates whether the current visit happens in a static context.
  `protected ClassNode`
  `lastImplicitItType`
  The type of the last encountered "it" implicit parameter.
  `protected Set<MethodNode>`
  `methodsToBeVisited`
  Optional filter restricting which methods should be visited.
  `protected final Set<Long>`
  `reportedErrors`
  Source positions that already produced a type-checking error.
  `protected final LinkedHashSet<org.codehaus.groovy.transform.stc.SecondPassExpression>`
  `secondPassExpressions`
  Some expressions need to be visited twice, because type information may be insufficient at some point.
  `protected SourceUnit`
  `source`
  Source unit currently being type checked.
  `protected final LinkedList<SwitchExpression>`
  `switchExpressions`
  Stack of enclosing switch expressions (GROOVY-12255).
  `protected final LinkedList<List<ClassNode>>`
  `switchExpressionYieldTypes`
  Yield types collected for the current switch expression (parallel to `switchExpressions`).
  `protected final LinkedList<Expression>`
  `switchSelectors`
  Innermost-first stack of switch selectors (statement or expression).
  `protected final LinkedList<SwitchStatement>`
  `switchStatements`
  Stack of enclosing switch statements.
  `protected Stack<Map<Object,List<ClassNode>>>`
  `temporaryIfBranchTypeInformation`
  Stores information which is valid in the "then" branch of an if-then-else statement.
  `protected final StaticTypeCheckingVisitor`
  `visitor`
  Visitor that owns this context.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `TypeCheckingContext(StaticTypeCheckingVisitor visitor)`
  Creates a context for the supplied type-checking visitor.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `CompilationUnit`
  `getCompilationUnit()`
  Returns the compilation unit associated with this context.
  `BinaryExpression`
  `getEnclosingBinaryExpression()`
  Returns the binary expression which is on the top of the stack, or null if there's no such element.
  `List<BinaryExpression>`
  `getEnclosingBinaryExpressionStack()`
  Returns the current stack of enclosing binary expressions.
  `ClassNode`
  `getEnclosingClassNode()`
  Returns the class node which is on the top of the stack, or null if there's no such element.
  `List<ClassNode>`
  `getEnclosingClassNodes()`
  Returns the current stack of enclosing classes.
  `TypeCheckingContext.EnclosingClosure`
  `getEnclosingClosure()`
  Returns the closure expression which is on the top of the stack, or null if there's no such element.
  `List<TypeCheckingContext.EnclosingClosure>`
  `getEnclosingClosureStack()`
  Returns the current stack of enclosing closure expressions.
  `MethodNode`
  `getEnclosingMethod()`
  Returns the method node which is on the top of the stack, or null if there's no such element.
  `Expression`
  `getEnclosingMethodCall()`
  Returns the method call which is on the top of the stack, or null if there's no such element.
  `List<Expression>`
  `getEnclosingMethodCalls()`
  Returns the current stack of enclosing method calls.
  `List<MethodNode>`
  `getEnclosingMethods()`
  Returns the current stack of enclosing methods.
  `SwitchExpression`
  `getEnclosingSwitchExpression()`
  Returns the switch expression on top of the stack, or `null` (GROOVY-12255).
  `List<ClassNode>`
  `getEnclosingSwitchExpressionYieldTypes()`
  Returns the list collecting yield types for the current switch expression (GROOVY-12255).
  `Expression`
  `getEnclosingSwitchSelector()`
  Selector of the innermost enclosing switch statement or expression (GROOVY-12255).
  `SwitchStatement`
  `getEnclosingSwitchStatement()`
  Returns the switch statement which is on the top of the stack, or null if there's no such element.
  `List<SwitchStatement>`
  `getEnclosingSwitchStatements()`
  Returns the current stack of enclosing switch statements.
  `ErrorCollector`
  `getErrorCollector()`
  Returns the current error collector, or `null` if none is active.
  `List<ErrorCollector>`
  `getErrorCollectors()`
  Returns the active error collector stack.
  `SourceUnit`
  `getSource()`
  Returns the source unit currently being type checked.
  `boolean`
  `isTargetOfEnclosingAssignment(Expression expression)`
  Indicates whether the expression is the left-hand side of the current assignment.
  `BinaryExpression`
  `popEnclosingBinaryExpression()`
  Pops a binary expression from the binary expression stack.
  `ClassNode`
  `popEnclosingClassNode()`
  Pops a class from the enclosing classes stack.
  `TypeCheckingContext.EnclosingClosure`
  `popEnclosingClosure()`
  Pops a closure expression from the closure expression stack.
  `MethodNode`
  `popEnclosingMethod()`
  Pops a method from the enclosing methods stack.
  `Expression`
  `popEnclosingMethodCall()`
  Pops a method call from the enclosing method call stack.
  `SwitchExpression`
  `popEnclosingSwitchExpression()`
  Pops the current switch expression (GROOVY-12255).
  `SwitchStatement`
  `popEnclosingSwitchStatement()`
  Pops a switch statement from the enclosing switch statements stack.
  `ErrorCollector`
  `popErrorCollector()`
  Pops the current error collector.
  `void`
  `popTemporaryTypeInfo()`
  Pops the current temporary type-information frame.
  `void`
  `pushEnclosingBinaryExpression(BinaryExpression binaryExpression)`
  Pushes a binary expression into the binary expression stack.
  `void`
  `pushEnclosingClassNode(ClassNode classNode)`
  Pushes a class into the classes stack.
  `void`
  `pushEnclosingClosureExpression(ClosureExpression closureExpression)`
  Pushes a closure expression into the closure expression stack.
  `void`
  `pushEnclosingMethod(MethodNode methodNode)`
  Pushes a method into the method stack.
  `void`
  `pushEnclosingMethodCall(Expression call)`
  Pushes a method call into the method call stack.
  `void`
  `pushEnclosingSwitchExpression(SwitchExpression switchExpression)`
  Pushes a switch expression onto the enclosing-expression stack (GROOVY-12255).
  `void`
  `pushEnclosingSwitchStatement(SwitchStatement switchStatement)`
  Pushes a switch statement into the switch statement stack.
  `ErrorCollector`
  `pushErrorCollector()`
  Pushes a fresh error collector derived from the current compiler configuration.
  `void`
  `pushErrorCollector(ErrorCollector collector)`
  Pushes the supplied error collector.
  `void`
  `pushTemporaryTypeInfo()`
  Pushes a temporary type-information frame.
  `void`
  `setCompilationUnit(CompilationUnit compilationUnit)`
  Sets the compilation unit associated with this context.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### visitor

    protected final StaticTypeCheckingVisitor visitor
    Visitor that owns this context.

  - ### source

    protected SourceUnit source
    Source unit currently being type checked.

  - ### compilationUnit

    protected CompilationUnit compilationUnit
    Compilation unit associated with the current type-checking run.

  - ### errorCollectors

    protected final LinkedList<ErrorCollector> errorCollectors
    Error collectors stacked for nested type-checking operations.

  - ### temporaryIfBranchTypeInformation

    protected Stack<Map<Object,List<ClassNode>>> temporaryIfBranchTypeInformation
    Stores information which is valid in the "then" branch of an if-then-else statement. This is used when the if condition expression makes use of an `instanceof` check.

  - ### delegationMetadata

    protected org.codehaus.groovy.transform.stc.DelegationMetadata delegationMetadata
    Whenever a method using a closure as argument (typically, "with") is detected, this list is updated with the receiver type of the with method.

  - ### isInStaticContext

    protected boolean isInStaticContext
    Indicates whether the current visit happens in a static context.

  - ### lastImplicitItType

    protected ClassNode lastImplicitItType
    The type of the last encountered "it" implicit parameter.

  - ### methodsToBeVisited

    protected Set<MethodNode> methodsToBeVisited
    Optional filter restricting which methods should be visited.

  - ### ifElseForWhileAssignmentTracker

    protected Map<VariableExpression,List<ClassNode>> ifElseForWhileAssignmentTracker
    This field is used to track assignments in if/else branches, for loops and while loops. For example, in the following code: `if (cond) { x = 1 } else { x = '123' }` the inferred type of x after the if/else statement should be the LUB of int and String.

  - ### alreadyVisitedMethods

    protected Set<MethodNode> alreadyVisitedMethods
    Methods already visited during the current checking pass.

  - ### secondPassExpressions

    protected final LinkedHashSet<org.codehaus.groovy.transform.stc.SecondPassExpression> secondPassExpressions
    Some expressions need to be visited twice, because type information may be insufficient at some point. For example, for closure shared variables, we need a first pass to collect every type which is assigned to a closure shared variable, then a second pass to ensure that every method call on such a variable is made on a LUB.

  - ### closureSharedVariablesAssignmentTypes

    protected final Map<VariableExpression,List<ClassNode>> closureSharedVariablesAssignmentTypes
    A map used to store every type used in closure shared variable assignments. In a second pass, we will compute the LUB of each type and check that method calls on those variables are valid.

  - ### controlStructureVariables

    protected Map<Parameter,ClassNode> controlStructureVariables
    Types tracked for variables introduced by control structures.

  - ### reportedErrors

    protected final Set<Long> reportedErrors
    Source positions that already produced a type-checking error.

  - ### enclosingClassNodes

    protected final LinkedList<ClassNode> enclosingClassNodes
    Stack of enclosing class nodes.

  - ### enclosingMethods

    protected final LinkedList<MethodNode> enclosingMethods
    Stack of enclosing methods.

  - ### enclosingMethodCalls

    protected final LinkedList<Expression> enclosingMethodCalls
    Stack of enclosing method-call expressions.

  - ### switchStatements

    protected final LinkedList<SwitchStatement> switchStatements
    Stack of enclosing switch statements.

  - ### switchExpressions

    protected final LinkedList<SwitchExpression> switchExpressions
    Stack of enclosing switch expressions (GROOVY-12255).

  - ### switchExpressionYieldTypes

    protected final LinkedList<List<ClassNode>> switchExpressionYieldTypes
    Yield types collected for the current switch expression (parallel to `switchExpressions`).

  - ### switchSelectors

    protected final LinkedList<Expression> switchSelectors
    Innermost-first stack of switch selectors (statement or expression). Case-label typing must use this rather than preferring statements over expressions, or a nested switch expression would see the outer selector (GROOVY-12255).

  - ### enclosingClosures

    protected final LinkedList<TypeCheckingContext.EnclosingClosure> enclosingClosures
    Stack of enclosing closures.

  - ### enclosingBinaryExpressions

    protected final LinkedList<BinaryExpression> enclosingBinaryExpressions
    Stack of enclosing binary expressions.

- ## Constructor Details

  - ### TypeCheckingContext

    public TypeCheckingContext(StaticTypeCheckingVisitor visitor)
    Creates a context for the supplied type-checking visitor.

- ## Method Details

  - ### getSource

    public SourceUnit getSource()
    Returns the source unit currently being type checked.

  - ### getCompilationUnit

    public CompilationUnit getCompilationUnit()
    Returns the compilation unit associated with this context.

  - ### setCompilationUnit

    public void setCompilationUnit(CompilationUnit compilationUnit)
    Sets the compilation unit associated with this context.

  - ### pushErrorCollector

    public ErrorCollector pushErrorCollector()
    Pushes a fresh error collector derived from the current compiler configuration.

  - ### pushErrorCollector

    public void pushErrorCollector(ErrorCollector collector)
    Pushes the supplied error collector.

  - ### popErrorCollector

    public ErrorCollector popErrorCollector()
    Pops the current error collector.

  - ### getErrorCollector

    public ErrorCollector getErrorCollector()
    Returns the current error collector, or `null` if none is active.

  - ### getErrorCollectors

    public List<ErrorCollector> getErrorCollectors()
    Returns the active error collector stack.

  - ### pushTemporaryTypeInfo

    public void pushTemporaryTypeInfo()
    Pushes a temporary type-information frame.

  - ### popTemporaryTypeInfo

    public void popTemporaryTypeInfo()
    Pops the current temporary type-information frame.

  - ### pushEnclosingBinaryExpression

    public void pushEnclosingBinaryExpression(BinaryExpression binaryExpression)
    Pushes a binary expression into the binary expression stack.

  - ### popEnclosingBinaryExpression

    public BinaryExpression popEnclosingBinaryExpression()
    Pops a binary expression from the binary expression stack.

  - ### getEnclosingBinaryExpression

    public BinaryExpression getEnclosingBinaryExpression()
    Returns the binary expression which is on the top of the stack, or null if there's no such element.

  - ### getEnclosingBinaryExpressionStack

    public List<BinaryExpression> getEnclosingBinaryExpressionStack()
    Returns the current stack of enclosing binary expressions. The first element is the top of the stack.

  - ### isTargetOfEnclosingAssignment

    public boolean isTargetOfEnclosingAssignment(Expression expression)
    Indicates whether the expression is the left-hand side of the current assignment.

  - ### pushEnclosingClosureExpression

    public void pushEnclosingClosureExpression(ClosureExpression closureExpression)
    Pushes a closure expression into the closure expression stack.

  - ### popEnclosingClosure

    public TypeCheckingContext.EnclosingClosure popEnclosingClosure()
    Pops a closure expression from the closure expression stack.

  - ### getEnclosingClosure

    public TypeCheckingContext.EnclosingClosure getEnclosingClosure()
    Returns the closure expression which is on the top of the stack, or null if there's no such element.

  - ### getEnclosingClosureStack

    public List<TypeCheckingContext.EnclosingClosure> getEnclosingClosureStack()
    Returns the current stack of enclosing closure expressions. The first element is the top of the stack.

  - ### pushEnclosingClassNode

    public void pushEnclosingClassNode(ClassNode classNode)
    Pushes a class into the classes stack.

  - ### popEnclosingClassNode

    public ClassNode popEnclosingClassNode()
    Pops a class from the enclosing classes stack.

  - ### getEnclosingClassNode

    public ClassNode getEnclosingClassNode()
    Returns the class node which is on the top of the stack, or null if there's no such element.

  - ### getEnclosingClassNodes

    public List<ClassNode> getEnclosingClassNodes()
    Returns the current stack of enclosing classes. The first element is the top of the stack, that is to say the currently visited class.

  - ### pushEnclosingMethod

    public void pushEnclosingMethod(MethodNode methodNode)
    Pushes a method into the method stack.

  - ### popEnclosingMethod

    public MethodNode popEnclosingMethod()
    Pops a method from the enclosing methods stack.

  - ### getEnclosingMethod

    public MethodNode getEnclosingMethod()
    Returns the method node which is on the top of the stack, or null if there's no such element.

  - ### getEnclosingMethods

    public List<MethodNode> getEnclosingMethods()
    Returns the current stack of enclosing methods. The first element is the top of the stack, that is to say the last visited method.

  - ### pushEnclosingMethodCall

    public void pushEnclosingMethodCall(Expression call)
    Pushes a method call into the method call stack.
    Parameters:
      `call` - the call expression to be pushed, either a `MethodCallExpression` or a `StaticMethodCallExpression`

  - ### popEnclosingMethodCall

    public Expression popEnclosingMethodCall()
    Pops a method call from the enclosing method call stack.

  - ### getEnclosingMethodCall

    public Expression getEnclosingMethodCall()
    Returns the method call which is on the top of the stack, or null if there's no such element.

  - ### getEnclosingMethodCalls

    public List<Expression> getEnclosingMethodCalls()
    Returns the current stack of enclosing method calls. The first element is the top of the stack, that is to say the currently visited method call.

  - ### pushEnclosingSwitchStatement

    public void pushEnclosingSwitchStatement(SwitchStatement switchStatement)
    Pushes a switch statement into the switch statement stack.

  - ### popEnclosingSwitchStatement

    public SwitchStatement popEnclosingSwitchStatement()
    Pops a switch statement from the enclosing switch statements stack.

  - ### getEnclosingSwitchStatement

    public SwitchStatement getEnclosingSwitchStatement()
    Returns the switch statement which is on the top of the stack, or null if there's no such element.

  - ### getEnclosingSwitchStatements

    public List<SwitchStatement> getEnclosingSwitchStatements()
    Returns the current stack of enclosing switch statements. The first element is the top of the stack, that is to say the last visited switch statement.

  - ### pushEnclosingSwitchExpression

    public void pushEnclosingSwitchExpression(SwitchExpression switchExpression)
    Pushes a switch expression onto the enclosing-expression stack (GROOVY-12255).
    Since:
      6.0.0

  - ### popEnclosingSwitchExpression

    public SwitchExpression popEnclosingSwitchExpression()
    Pops the current switch expression (GROOVY-12255).
    Since:
      6.0.0

  - ### getEnclosingSwitchExpression

    public SwitchExpression getEnclosingSwitchExpression()
    Returns the switch expression on top of the stack, or `null` (GROOVY-12255).
    Since:
      6.0.0

  - ### getEnclosingSwitchExpressionYieldTypes

    public List<ClassNode> getEnclosingSwitchExpressionYieldTypes()
    Returns the list collecting yield types for the current switch expression (GROOVY-12255).
    Since:
      6.0.0

  - ### getEnclosingSwitchSelector

    public Expression getEnclosingSwitchSelector()
    Selector of the innermost enclosing switch statement or expression (GROOVY-12255).
    Since:
      6.0.0