Class CompilationUnit

  • All Implemented Interfaces:
    CodeElement
    Direct Known Subclasses:
    ClassUnit, InterfaceUnit

    public abstract class CompilationUnit
    extends ProgramUnit
    An unit of the program to analyze that is part of a hierarchical structure.
    • Field Detail

      • instances

        protected final java.util.Collection<Unit> instances
        The lazily computed collection of instances of this unit, that is, the collection of compilation units that directly or indirectly inherit from this unit.
    • Constructor Detail

      • CompilationUnit

        protected CompilationUnit​(CodeLocation location,
                                  Program program,
                                  java.lang.String name,
                                  boolean sealed)
        Builds an unit with super unit.
        Parameters:
        location - the location where the unit is define within the source file
        program - the program where this unit is defined
        name - the name of the unit
        sealed - whether or not this unit can be inherited from
    • Method Detail

      • isSealed

        public boolean isSealed()
        Yields whether or not this unit is sealed, meaning that it cannot be used as super unit of other compilation units.
        Returns:
        true if this unit is sealed
      • addAncestor

        public abstract boolean addAncestor​(CompilationUnit unit)
        Adds a new CompilationUnit as direct inheritance ancestor (i.e., superclass, interface, or superinterface) of this unit.
        Parameters:
        unit - the unit to add
        Returns:
        true if the collection of ancestors changed as a result of the call
      • addInstance

        public abstract void addInstance​(Unit unit)
                                  throws ProgramValidationException
        Adds the given unit as an instance of this one, thus marking the former as a type that inherits from the latter.
        Parameters:
        unit - the unit to be added
        Throws:
        ProgramValidationException - if the given unit cannot be added
      • getCodeMembersRecursively

        public java.util.Collection<CodeMember> getCodeMembersRecursively()
        Yields the collection of all the CodeMembers defined in this unit. This method returns the same result as Unit.getCodeMembers(), but subclasses are likely to re-implement it to add additional ones (e.g., instance members).

        This method also returns all the instance code members defined in this unit.
        Overrides:
        getCodeMembersRecursively in class Unit
        Returns:
        the collection of the code members
      • getGlobalsRecursively

        public java.util.Collection<Global> getGlobalsRecursively()
        Yields the collection of all the Globals defined in this unit.

        This method also returns all the instance globals defined in this unit.
        Overrides:
        getGlobalsRecursively in class Unit
        Returns:
        the collection of the globals
      • getInstanceCodeMembers

        public java.util.Collection<CodeMember> getInstanceCodeMembers​(boolean traverseHierarchy)
        Yields the collection of instance CodeMembers defined in this unit.
        Parameters:
        traverseHierarchy - if true, also returns instance code members from superunits, transitively
        Returns:
        the collection of instance code members
      • getInstanceGlobals

        public java.util.Collection<Global> getInstanceGlobals​(boolean traverseHierarchy)
        Yields the collection of instance Globals defined in this unit. Each global is uniquely identified by its name, meaning that there are no two instance globals having the same name in each unit.
        Parameters:
        traverseHierarchy - if true, also returns instance globals from superunits, transitively
        Returns:
        the collection of instance globals
      • getInstanceCFGs

        public java.util.Collection<CFG> getInstanceCFGs​(boolean traverseHierarchy)
        Yields the collection of instance CFGs defined in this unit. Each cfg is uniquely identified by its signature (CodeMemberDescriptor.getSignature()), meaning that there are no two instance cfgs having the same signature in each unit. Instance cfgs can be overridden inside subunits, according to CodeMemberDescriptor.isOverridable().
        Parameters:
        traverseHierarchy - if true, also returns instance cfgs from superunits, transitively
        Returns:
        the collection of instance cfgs
      • getAbstractCodeMembers

        public java.util.Collection<AbstractCodeMember> getAbstractCodeMembers​(boolean traverseHierarchy)
        Yields the collection of instance AbstractCodeMembers defined in this unit. Each cfg is uniquely identified by its signature (CodeMemberDescriptor.getSignature()), meaning that there are no two signature cfgs having the same signature in each unit. Signature cfgs must be overridden inside subunits, according to CodeMemberDescriptor.isOverridable().
        Parameters:
        traverseHierarchy - if true, also returns signature cfgs from superunits, transitively
        Returns:
        the collection of signature cfgs
      • getInstanceConstructs

        public java.util.Collection<NativeCFG> getInstanceConstructs​(boolean traverseHierarchy)
        Yields the collection of instance constructs (NativeCFGs) defined in this unit. Each construct is uniquely identified by its signature (CodeMemberDescriptor.getSignature()), meaning that there are no two instance constructs having the same signature in each unit. Instance constructs can be overridden inside subunits, according to CodeMemberDescriptor.isOverridable().
        Parameters:
        traverseHierarchy - if true, also returns instance constructs from superunits, transitively
        Returns:
        the collection of instance constructs
      • getInstances

        public java.util.Collection<Unit> getInstances()
        Yields the collection of ClassUnits that are instances of this one, including itself. In other words, this method returns the collection of compilation units that directly or indirectly, inherit from this one.

        Note that this method returns an empty collection, until the Program has been validated by a ProgramValidationLogic.validateAndFinalize(Program) call.
        Returns:
        the collection of units that are instances of this one, including this unit itself
      • getAnnotations

        public Annotations getAnnotations()
        Yields the annotations of this compilation unit.
        Returns:
        the annotations of this compilation unit
      • addAnnotation

        public void addAnnotation​(Annotation ann)
        Adds an annotation to the annotations of this compilation unit.
        Parameters:
        ann - the annotation to be added
      • getImmediateAncestors

        public abstract java.util.Collection<CompilationUnit> getImmediateAncestors()
        Yields the collection of CompilationUnits that are this unit directly inherits from, regardless of their type.
        Returns:
        the collection of units that are direct ancestors of this one
      • isInstanceOf

        public abstract boolean isInstanceOf​(CompilationUnit unit)
        Yields true if and only if this unit is an instance of the given one. This method works correctly even if ProgramValidationLogic.validateAndFinalize(Program) has not been invoked yet, and thus the if collection of instances of the given unit is not yet available.
        Parameters:
        unit - the other unit
        Returns:
        true only if that condition holds
      • searchCodeMembers

        public <T extends CodeMember> java.util.Collection<T> searchCodeMembers​(java.util.function.Predicate<CodeMember> filter,
                                                                                boolean traverseHierarchy)
        Searches among instance code members, returning a collection containing all members that satisfy the given condition.
        Type Parameters:
        T - the concrete type of elements that this method returns
        Parameters:
        filter - the filtering condition to use for selecting which code members to return
        traverseHierarchy - if true, also returns instance code members from superunits, transitively
        Returns:
        the collection of matching code members
      • searchGlobals

        public java.util.Collection<Global> searchGlobals​(java.util.function.Predicate<Global> filter,
                                                          boolean traverseHierarchy)
        Searches among instance globals, returning a collection containing all globals that satisfy the given condition.
        Parameters:
        filter - the filtering condition to use for selecting which globals to return
        traverseHierarchy - if true, also returns instance globals from superunits, transitively
        Returns:
        the collection of matching globals
      • addInstanceGlobal

        public boolean addInstanceGlobal​(Global global)
        Adds a new instance Global, identified by its name (Global.getName()), to this unit.
        Parameters:
        global - the global to add
        Returns:
        true if there was no instance global previously associated with the same name, false otherwise. If this method returns false, the given global is discarded.
      • addInstanceCodeMember

        public boolean addInstanceCodeMember​(CodeMember cm)
        Adds a new instance CodeMember, identified by its signature (CodeMemberDescriptor.getSignature()), to this unit. Instance code members can be overridden inside subunits, according to CodeMemberDescriptor.isOverridable().
        Parameters:
        cm - the cfg to add
        Returns:
        true if there was no instance member previously associated with the same signature, false otherwise. If this method returns false, the given code member is discarded.
      • getInstanceCodeMember

        public CodeMember getInstanceCodeMember​(java.lang.String signature,
                                                boolean traverseHierarchy)
        Yields the first instance CodeMember defined in this unit having the given signature (CodeMemberDescriptor.getSignature()), if any.
        Parameters:
        signature - the signature of the member to find
        traverseHierarchy - if true, also returns instance members from superunits, transitively
        Returns:
        the instance code member with the given signature, or null
      • getInstanceGlobal

        public Global getInstanceGlobal​(java.lang.String name,
                                        boolean traverseHierarchy)
        Yields the first instance Global defined in this unit having the given name (Global.getName()), if any.
        Parameters:
        name - the name of the global to find
        traverseHierarchy - if true, also returns instance globals from superunits, transitively
        Returns:
        the instance global with the given name, or null
      • getInstanceCodeMembersByName

        public java.util.Collection<CodeMember> getInstanceCodeMembersByName​(java.lang.String name,
                                                                             boolean traverseHierarchy)
        Yields the collection of all instance CodeMembers defined in this unit that have the given name.
        Parameters:
        name - the name of the members to include
        traverseHierarchy - if true, also returns instance members from superunits, transitively
        Returns:
        the collection of instance members with the given name
      • getMatchingInstanceCodeMembers

        public java.util.Collection<CodeMember> getMatchingInstanceCodeMembers​(CodeMemberDescriptor signature,
                                                                               boolean traverseHierarchy)
        Finds all the instance code members whose signature matches the one of the given CodeMemberDescriptor, according to CodeMemberDescriptor.matchesSignature(CodeMemberDescriptor).
        Parameters:
        signature - the descriptor providing the signature to match
        traverseHierarchy - if true, also returns instance code members from superunits, transitively
        Returns:
        the collection of instance code members that match the given signature