mjc

org.multijava.mjc
Interface CContextType

All Known Subinterfaces:
CClassContextType, CCompilationUnitContextType, CConstructorContextType, CExpressionContextType, CFlowControlContextType, CInitializerContextType, CInterfaceContextType, CMethodContextType
All Known Implementing Classes:
CClassContext, CCompilationUnitContext, CConstructorContext, CContext, CExpressionContext, CFinallyContext, CFlowControlContext, CInitializerContext, CInterfaceContext, CMethodContext

public interface CContextType

Descendents of this class represent local contexts during checking passes (checkInterface, checkInitializers, typecheck). The context classes follow the control flow and maintain information about variables (initialized, used, allocated) and exceptions (thrown, caught). They also verify that a context is still reachable. The contexts are used for name resolution (JLS2, 6 and 14.4), checking for unreachable statements (JLS2 14.20), checking for definite assignment (JLS2, 16), and for checking for proper subclassing and overriding (JLS2, 8).

The contexts are hierarchical. To wit, a compilation unit context is created and passed to the method that checks a class. This method creates a class context by passing the compilation unit context to the CClassContext constructor. When checking methods of the class the CClassContext instance is passed to the CMethodContext constructor. These context classes are stored in fields of the AST nodes.

During typechecking the context hierarchy is mutated in a way that mimics the actual run-time control flow. For example, variable declarations cause the context to be mutated to record the variable's name and type. Assignment to a variable causes the context to be mutated to record that the variable is definitely assigned. Reference a variable's value triggers a check that the variable has been definitely assigned.


Method Summary
 void addFANonNull(Object expr)
          Mark expr (or member) as NonNull in this context
 void addFANonNulls(Object[] exprs)
          adds exprs (or members) as NonNull in this context
 void addFANull(Object expr)
           
 void addFANulls(Object[] exprs)
           
 void adoptNullityInfo(CContextType other)
           
 MJMathMode arithmeticMode()
          Indicates the integral arithmetic mode that should be used.
 void catchUp(JTypeDeclarationType decl)
          Calls back to the compiler for this context and requests that the compiler catch-up decl to the same pass as the currently active task.
 boolean check(boolean expr, MessageDescription mess)
          Verifies an expression and if false signals an error.
 boolean check(boolean expr, MessageDescription mess, Object param1)
          Verifies an expression and if false signals an error.
 boolean check(boolean expr, MessageDescription mess, Object param1, Object param2)
          Verifies an expression and if false signals an error.
 boolean check(boolean expr, MessageDescription mess, Object[] params)
           
 void classToGenerate(CSourceClass clazz)
          Adds a source class to be generated
 CClassContextType createClassContext(CClass self)
          Creates a class context with this context as its parent.
 CExtMethodContext createExtMethodContext(CSourceClass host, CClass owner)
          Create a new child of this context representing the context in which an external method declaration is typechecked.
 CInterfaceContextType createInterfaceContext(CClass self)
          Creates an interface context with this context as its parent.
 boolean declaredOutsideOfLoop(JLocalVariable var)
          Indicates whether this context is enclosed in a loop and the given variable is declared outside the inner-most loop context.
 boolean declares(JLocalVariable var)
          Returns true if the given local variable is declared exactly in this context, i.e., it is not declared in an outer context.
 void dumpNonNulls(String msg)
           
 void fail(MessageDescription mess, Object param1, Object param2)
          Generates an UnpositionedError with a given message.
 void fail(MessageDescription mess, Object[] params)
           
 CMemberHost findNearestHost()
          Returns the signature of the nearest lexically enclosing context that can host member declarations (i.e., a CClass or a CCompilationUnit).
 CClassContextType getClassContext()
          Returns the class context for this context.
 CMethod getCMethod()
          Returns the signature of the method declaration in which this context is enclosed, or null if this context is not enclosed in a method declaration.
 CCompilationUnitContextType getCompilationUnit()
          Returns the compilation unit context for this context.
 Main getCompiler()
          Gets the compiler for this context
 Object[] getFANonNulls()
          returns the JPhyla that are known to be non-null in this context.
 Object[] getFANulls()
           
 CFlowControlContextType getFlowControlContext()
          Returns the nearest expression context.
 CMethodContextType getMethodContext()
          Returns the method context for this context.
 CContextType getParentContext()
          Returns the parent context for this context
 void initializeField(VariableDescriptor varDesc)
          Marks the field with the given descriptor as definitely assigned to in this context.
 boolean isBeforeSuperConstructorCall()
          Indicates whether this context is enclosed in a constructor and occurs before the constructor has invoked the superclass constructor.
 boolean isFANonNull(Object expr)
          Indicates whether expr (or member) is conditionally NonNull is this context.
 boolean isFieldDefinitelyAssigned(int pos)
          Indicates whether the field in the given position is definitely assigned to in this context.
 boolean isInConstructor()
          Indicates whether this context is enclosed in a constructor.
 boolean isInInitializer()
          Indicates whether this context is enclosed in an instance or static initializer.
 boolean isInLoop()
          Indicates whether this context is enclosed in a loop.
 boolean isPure()
          Indicates whether this context is "pure".
 boolean isStatic()
          Indicates whether this context is "static".
 CClass lookupClass(String name)
          Searches for a class with the given simple name according the procedure in JLS2 6.5.5.
 CFieldAccessor lookupField(String ident)
          searches for a field with the given identifier
 CFieldAccessor lookupField(String ident, CExpressionContextType context)
          searches for a field with the given identifier
 JLocalVariable lookupLocalVariable(String ident)
          searches for a local variable with the given identifier
 CMethod lookupMethod(String name, CType[] params)
          Searches for the most specific method in the current context that is applicable to the given identifier and argument type tuple.
 CMethod lookupMethod(String name, CType[] params, CClassContextType context)
          Searches for the most specific method in the current context that is applicable to the given identifier and argument type tuple.
 CMethodSet lookupMethodOrSet(String name, CType[] params)
          Searches for the most specific method(s) in the current context that is applicable to the given identifier and argument type tuple.
 CMethodSet lookupMethodOrSet(String name, CType[] params, CClassContextType context)
          Searches for the most specific method(s) in the current context that is applicable to the given identifier and argument type tuple.
 CFieldAccessor lookupOuterField(String ident)
          Searches for a field of the given name in the context surrounding the current lexical contour.
 CFieldAccessor lookupOuterField(String ident, CExpressionContextType context)
          Searches for a field of the given name in the context surrounding the current lexical contour.
 JExpression lookupOuterLocalVariable(TokenReference ref, String ident)
          Finds a local variable with the given name that appears outside the current lexical contour.
 CTypeVariable lookupTypeVariable(String Ident)
          search for type variable that are accessible
 void mergeNullityInfo(CContextType other)
          Merge the list of nulls in this context with that of the given context.
 ModifierUtility modUtil()
          Returns the modifier utility for this.
 void registerGFDecl(String methodIdent, CGenericFunctionCollection coll)
          Registers the declaration of an external generic function in this context.
 void registerVisibleMethod(CMethod method)
          Registers with the surrounding context that a declaration of the given method occurs in this context.
 void registerVisibleType(CType type)
          Registers with the surrounding context that a reference to the given type occurs in this context.
 void removeAllFANullity()
           
 void removeFANonNull(Object expr)
           
 void replaceFieldInfoUpTo(int pos, CVariableInfoTable replacement)
          Replaces the local field info for fields in positions 0 up to pos with the info in replacement.
 void reportTrouble(Exception trouble)
          Reports the given error and the "swallows" it, allowing compilation to continue.
 void resolveMaybeExtMethodRef(String ident)
          Searches for any imported external generic functions.
 

Method Detail

lookupClass

public CClass lookupClass(String name)
                            throws UnpositionedError
Searches for a class with the given simple name according the procedure in JLS2 6.5.5.

Parameters:
name - the class name, without qualifiers
Returns:
the class if found, null otherwise
Throws:
UnpositionedError - if search fails

lookupTypeVariable

public CTypeVariable lookupTypeVariable(String Ident)
                                          throws UnpositionedError
search for type variable that are accessible

Throws:
UnpositionedError

lookupMethod

public CMethod lookupMethod(String name,
                            CType[] params)
                              throws UnpositionedError
Searches for the most specific method in the current context that is applicable to the given identifier and argument type tuple.

Parameters:
name - method name
params - method parameter types
Throws:
UnpositionedError - if the result is ambiguous

lookupMethod

public CMethod lookupMethod(String name,
                            CType[] params,
                            CClassContextType context)
                              throws UnpositionedError
Searches for the most specific method in the current context that is applicable to the given identifier and argument type tuple.

Parameters:
name - method name
params - method parameter types
context - the context of the class containing the method call
Throws:
UnpositionedError - if the result is ambiguous

lookupMethodOrSet

public CMethodSet lookupMethodOrSet(String name,
                                    CType[] params)
                                      throws UnpositionedError
Searches for the most specific method(s) in the current context that is applicable to the given identifier and argument type tuple.

Parameters:
name - method name
params - method parameter types
Throws:
UnpositionedError - if the result is ambiguous

lookupMethodOrSet

public CMethodSet lookupMethodOrSet(String name,
                                    CType[] params,
                                    CClassContextType context)
                                      throws UnpositionedError
Searches for the most specific method(s) in the current context that is applicable to the given identifier and argument type tuple.

Parameters:
name - method name
params - method parameter types
context - the context of the class containing the method call
Throws:
UnpositionedError - if the result is ambiguous

lookupField

public CFieldAccessor lookupField(String ident)
                                    throws UnpositionedError
searches for a field with the given identifier

Returns:
a field from an ident in current context
Throws:
UnpositionedError - this error will be positioned soon

lookupField

public CFieldAccessor lookupField(String ident,
                                  CExpressionContextType context)
                                    throws UnpositionedError
searches for a field with the given identifier

Returns:
a field from an ident in current context
Throws:
UnpositionedError - this error will be positioned soon

lookupLocalVariable

public JLocalVariable lookupLocalVariable(String ident)
searches for a local variable with the given identifier

Parameters:
ident - the name of the local variable
Returns:
a variable from an ident in current context

lookupOuterLocalVariable

public JExpression lookupOuterLocalVariable(TokenReference ref,
                                            String ident)
Finds a local variable with the given name that appears outside the current lexical contour.

Parameters:
ref - a token reference used to build a new JOuterLocalVariableExpression
ident - the name of the outer variable
Returns:
a variable from an ident in the surrounding context, or null if not found

lookupOuterField

public CFieldAccessor lookupOuterField(String ident)
                                         throws UnpositionedError
Searches for a field of the given name in the context surrounding the current lexical contour.

Parameters:
ident - the name of the field
Returns:
a variable from a field in the surrounding context, or null if none is found
Throws:
UnpositionedError - this error will be positioned soon

lookupOuterField

public CFieldAccessor lookupOuterField(String ident,
                                       CExpressionContextType context)
                                         throws UnpositionedError
Searches for a field of the given name in the context surrounding the current lexical contour.

Parameters:
ident - the name of the field
context - the context of the field access
Returns:
a variable from a field in the surrounding context, or null if none is found
Throws:
UnpositionedError - this error will be positioned soon

replaceFieldInfoUpTo

public void replaceFieldInfoUpTo(int pos,
                                 CVariableInfoTable replacement)
Replaces the local field info for fields in positions 0 up to pos with the info in replacement.


initializeField

public void initializeField(VariableDescriptor varDesc)
Marks the field with the given descriptor as definitely assigned to in this context.


isFieldDefinitelyAssigned

public boolean isFieldDefinitelyAssigned(int pos)
Indicates whether the field in the given position is definitely assigned to in this context.


getParentContext

public CContextType getParentContext()
Returns the parent context for this context

Returns:
the parent

getCompilationUnit

public CCompilationUnitContextType getCompilationUnit()
Returns the compilation unit context for this context. This only makes sense for contexts that do not themselves represent compilation units. I.e., in classes that represent compilation unit context this should be overridden.

Returns:
the compilation unit

getClassContext

public CClassContextType getClassContext()
Returns the class context for this context. This only makes sense for contexts that can appear inside classes. I.e., This should be overridden in classes representing other sorts of contexts.

Returns:
the nearest parent of type CClassContextType.

getMethodContext

public CMethodContextType getMethodContext()
Returns the method context for this context. This only makes sense for contexts that can appear inside methods. I.e., This should be overridden in classes representing other sorts of contexts.

Returns:
the nearest parent of type CMethodContextType.

getFlowControlContext

public CFlowControlContextType getFlowControlContext()
Returns the nearest expression context.

Returns:
the nearest parent of type CExpressionContextType

getCMethod

public CMethod getCMethod()
Returns the signature of the method declaration in which this context is enclosed, or null if this context is not enclosed in a method declaration.


findNearestHost

public CMemberHost findNearestHost()
Returns the signature of the nearest lexically enclosing context that can host member declarations (i.e., a CClass or a CCompilationUnit). Used for access checks, this is slightly different than getHostClass(). The later method returns an anchor class signature for external methods, which is useful in code generation and in typechecking external method bodies. On the other hand, this method returns a CCompilationUnit for external methods, which is useful in checking access between two differently named generic functions declared in the same compilation unit. !FIXME! It may be possible to eliminate this method and just use getHostClass now that private external methods are duplciated in every anchor.

Returns:
the nearest parent that can be coerced to a CMemberHost

createClassContext

public CClassContextType createClassContext(CClass self)
Creates a class context with this context as its parent.

Parameters:
self - the corresponding class

createInterfaceContext

public CInterfaceContextType createInterfaceContext(CClass self)
Creates an interface context with this context as its parent.

Parameters:
self - the corresponding interface signature

createExtMethodContext

public CExtMethodContext createExtMethodContext(CSourceClass host,
                                                CClass owner)
Create a new child of this context representing the context in which an external method declaration is typechecked.

Parameters:
host - the host class
owner - the receiver class

classToGenerate

public void classToGenerate(CSourceClass clazz)
Adds a source class to be generated

Parameters:
clazz - the class to be generated

resolveMaybeExtMethodRef

public void resolveMaybeExtMethodRef(String ident)
Searches for any imported external generic functions.

Parameters:
ident - the name of the generic function to search for

registerGFDecl

public void registerGFDecl(String methodIdent,
                           CGenericFunctionCollection coll)
Registers the declaration of an external generic function in this context.


registerVisibleMethod

public void registerVisibleMethod(CMethod method)
Registers with the surrounding context that a declaration of the given method occurs in this context.


registerVisibleType

public void registerVisibleType(CType type)
Registers with the surrounding context that a reference to the given type occurs in this context.


reportTrouble

public void reportTrouble(Exception trouble)
Reports the given error and the "swallows" it, allowing compilation to continue. This method is used to report non-fatal errors; for fatal errors the exception is just thrown.

Parameters:
trouble - the error

fail

public void fail(MessageDescription mess,
                 Object param1,
                 Object param2)
                   throws UnpositionedError
Generates an UnpositionedError with a given message.

Parameters:
mess - the error message
param1 - the first message parameter
param2 - the second message parameter
Returns:
never, i.e., this method always throws an exception
Throws:
UnpositionedError - the error exception

fail

public void fail(MessageDescription mess,
                 Object[] params)
                   throws UnpositionedError
Throws:
UnpositionedError

check

public boolean check(boolean expr,
                     MessageDescription mess)
                       throws UnpositionedError
Verifies an expression and if false signals an error.

Parameters:
expr - condition to verify
mess - the message to be displayed
Returns:
true iff expr is true
Throws:
UnpositionedError - this error will be positioned soon

check

public boolean check(boolean expr,
                     MessageDescription mess,
                     Object[] params)
                       throws UnpositionedError
Throws:
UnpositionedError

check

public boolean check(boolean expr,
                     MessageDescription mess,
                     Object param1)
                       throws UnpositionedError
Verifies an expression and if false signals an error.

Parameters:
expr - condition to verify
mess - the message to be displayed
param1 - the first parameter
Returns:
true iff expr is true
Throws:
UnpositionedError - this error will be positioned soon

check

public boolean check(boolean expr,
                     MessageDescription mess,
                     Object param1,
                     Object param2)
                       throws UnpositionedError
Verifies an expression and if false signals an error.

Parameters:
expr - condition to verify
mess - the message to be displayed
param1 - the first parameter
param2 - the second parameter
Returns:
true iff expr is true
Throws:
UnpositionedError - this error will be positioned soon

getCompiler

public Main getCompiler()
Gets the compiler for this context


modUtil

public ModifierUtility modUtil()
Returns the modifier utility for this.


isInLoop

public boolean isInLoop()
Indicates whether this context is enclosed in a loop.


declaredOutsideOfLoop

public boolean declaredOutsideOfLoop(JLocalVariable var)
Indicates whether this context is enclosed in a loop and the given variable is declared outside the inner-most loop context.


declares

public boolean declares(JLocalVariable var)
Returns true if the given local variable is declared exactly in this context, i.e., it is not declared in an outer context.


isInInitializer

public boolean isInInitializer()
Indicates whether this context is enclosed in an instance or static initializer.


isInConstructor

public boolean isInConstructor()
Indicates whether this context is enclosed in a constructor.


isBeforeSuperConstructorCall

public boolean isBeforeSuperConstructorCall()
Indicates whether this context is enclosed in a constructor and occurs before the constructor has invoked the superclass constructor.


isStatic

public boolean isStatic()
Indicates whether this context is "static".

Returns:
true iff this context is enclosed in a context for a static initializer or static method.

isPure

public boolean isPure()
Indicates whether this context is "pure".

Returns:
true iff this context is enclosed in a pure method.

catchUp

public void catchUp(JTypeDeclarationType decl)
Calls back to the compiler for this context and requests that the compiler catch-up decl to the same pass as the currently active task.


arithmeticMode

public MJMathMode arithmeticMode()
Indicates the integral arithmetic mode that should be used.


isFANonNull

public boolean isFANonNull(Object expr)
Indicates whether expr (or member) is conditionally NonNull is this context.


getFANonNulls

public Object[] getFANonNulls()
returns the JPhyla that are known to be non-null in this context.


getFANulls

public Object[] getFANulls()

addFANonNull

public void addFANonNull(Object expr)
Mark expr (or member) as NonNull in this context


addFANull

public void addFANull(Object expr)

removeFANonNull

public void removeFANonNull(Object expr)

removeAllFANullity

public void removeAllFANullity()

addFANonNulls

public void addFANonNulls(Object[] exprs)
adds exprs (or members) as NonNull in this context


addFANulls

public void addFANulls(Object[] exprs)

mergeNullityInfo

public void mergeNullityInfo(CContextType other)
Merge the list of nulls in this context with that of the given context. This is done by taking a union of the two sets.


adoptNullityInfo

public void adoptNullityInfo(CContextType other)

dumpNonNulls

public void dumpNonNulls(String msg)

mjc

mjc is Copyright (C) 2000-2004 by Iowa State University and is distributed under the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. mjc is based in part on the Kopi project Copyright (C) 1990-99 DMS Decision Management Systems Ges.m.b.H.