Package it.unive.lisa.program
Class CompilationUnit
- java.lang.Object
-
- it.unive.lisa.program.Unit
-
- it.unive.lisa.program.ProgramUnit
-
- it.unive.lisa.program.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.
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
CompilationUnit(CodeLocation location, Program program, java.lang.String name, boolean sealed)
Builds an unit with super unit.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract boolean
addAncestor(CompilationUnit unit)
Adds a newCompilationUnit
as direct inheritance ancestor (i.e., superclass, interface, or superinterface) of this unit.void
addAnnotation(Annotation ann)
Adds an annotation to the annotations of this compilation unit.abstract void
addInstance(Unit unit)
Adds the given unit as an instance of this one, thus marking the former as a type that inherits from the latter.boolean
addInstanceCodeMember(CodeMember cm)
Adds a new instanceCodeMember
, identified by its signature (CodeMemberDescriptor.getSignature()
), to this unit.boolean
addInstanceGlobal(Global global)
Adds a new instanceGlobal
, identified by its name (Global.getName()
), to this unit.java.util.Collection<AbstractCodeMember>
getAbstractCodeMembers(boolean traverseHierarchy)
Yields the collection of instanceAbstractCodeMember
s defined in this unit.Annotations
getAnnotations()
Yields the annotations of this compilation unit.java.util.Collection<CodeMember>
getCodeMembersRecursively()
Yields the collection of all theCodeMember
s defined in this unit.java.util.Collection<Global>
getGlobalsRecursively()
Yields the collection of all theGlobal
s defined in this unit.abstract java.util.Collection<CompilationUnit>
getImmediateAncestors()
Yields the collection ofCompilationUnit
s that are this unit directly inherits from, regardless of their type.java.util.Collection<CFG>
getInstanceCFGs(boolean traverseHierarchy)
Yields the collection of instanceCFG
s defined in this unit.CodeMember
getInstanceCodeMember(java.lang.String signature, boolean traverseHierarchy)
Yields the first instanceCodeMember
defined in this unit having the given signature (CodeMemberDescriptor.getSignature()
), if any.java.util.Collection<CodeMember>
getInstanceCodeMembers(boolean traverseHierarchy)
Yields the collection of instanceCodeMember
s defined in this unit.java.util.Collection<CodeMember>
getInstanceCodeMembersByName(java.lang.String name, boolean traverseHierarchy)
Yields the collection of all instanceCodeMember
s defined in this unit that have the given name.java.util.Collection<NativeCFG>
getInstanceConstructs(boolean traverseHierarchy)
Yields the collection of instance constructs (NativeCFG
s) defined in this unit.Global
getInstanceGlobal(java.lang.String name, boolean traverseHierarchy)
Yields the first instanceGlobal
defined in this unit having the given name (Global.getName()
), if any.java.util.Collection<Global>
getInstanceGlobals(boolean traverseHierarchy)
Yields the collection of instanceGlobal
s defined in this unit.java.util.Collection<Unit>
getInstances()
Yields the collection ofClassUnit
s that are instances of this one, including itself.java.util.Collection<CodeMember>
getMatchingInstanceCodeMembers(CodeMemberDescriptor signature, boolean traverseHierarchy)
Finds all the instance code members whose signature matches the one of the givenCodeMemberDescriptor
, according toCodeMemberDescriptor.matchesSignature(CodeMemberDescriptor)
.abstract boolean
isInstanceOf(CompilationUnit unit)
Yieldstrue
if and only if this unit is an instance of the given one.boolean
isSealed()
Yields whether or not this unit is sealed, meaning that it cannot be used as super unit of other compilation units.<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.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.-
Methods inherited from class it.unive.lisa.program.ProgramUnit
getLocation, getProgram
-
Methods inherited from class it.unive.lisa.program.Unit
addCodeMember, addGlobal, canBeInstantiated, getCodeMember, getCodeMembers, getCodeMembersByName, getGlobal, getGlobals, getMatchingCodeMember, getName, toString
-
-
-
-
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 fileprogram
- the program where this unit is definedname
- the name of the unitsealed
- 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 newCompilationUnit
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 theCodeMember
s defined in this unit. This method returns the same result asUnit.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 classUnit
- Returns:
- the collection of the code members
-
getGlobalsRecursively
public java.util.Collection<Global> getGlobalsRecursively()
Yields the collection of all theGlobal
s defined in this unit.
This method also returns all the instance globals defined in this unit.- Overrides:
getGlobalsRecursively
in classUnit
- Returns:
- the collection of the globals
-
getInstanceCodeMembers
public java.util.Collection<CodeMember> getInstanceCodeMembers(boolean traverseHierarchy)
Yields the collection of instanceCodeMember
s defined in this unit.- Parameters:
traverseHierarchy
- iftrue
, 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 instanceGlobal
s 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
- iftrue
, 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 instanceCFG
s 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 toCodeMemberDescriptor.isOverridable()
.- Parameters:
traverseHierarchy
- iftrue
, 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 instanceAbstractCodeMember
s 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 toCodeMemberDescriptor.isOverridable()
.- Parameters:
traverseHierarchy
- iftrue
, 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 (NativeCFG
s) 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 toCodeMemberDescriptor.isOverridable()
.- Parameters:
traverseHierarchy
- iftrue
, also returns instance constructs from superunits, transitively- Returns:
- the collection of instance constructs
-
getInstances
public java.util.Collection<Unit> getInstances()
Yields the collection ofClassUnit
s 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 theProgram
has been validated by aProgramValidationLogic.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 ofCompilationUnit
s 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)
Yieldstrue
if and only if this unit is an instance of the given one. This method works correctly even ifProgramValidationLogic.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 returntraverseHierarchy
- iftrue
, 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 returntraverseHierarchy
- iftrue
, also returns instance globals from superunits, transitively- Returns:
- the collection of matching globals
-
addInstanceGlobal
public boolean addInstanceGlobal(Global global)
Adds a new instanceGlobal
, 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 returnsfalse
, the given global is discarded.
-
addInstanceCodeMember
public boolean addInstanceCodeMember(CodeMember cm)
Adds a new instanceCodeMember
, identified by its signature (CodeMemberDescriptor.getSignature()
), to this unit. Instance code members can be overridden inside subunits, according toCodeMemberDescriptor.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 returnsfalse
, the given code member is discarded.
-
getInstanceCodeMember
public CodeMember getInstanceCodeMember(java.lang.String signature, boolean traverseHierarchy)
Yields the first instanceCodeMember
defined in this unit having the given signature (CodeMemberDescriptor.getSignature()
), if any.- Parameters:
signature
- the signature of the member to findtraverseHierarchy
- iftrue
, 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 instanceGlobal
defined in this unit having the given name (Global.getName()
), if any.- Parameters:
name
- the name of the global to findtraverseHierarchy
- iftrue
, 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 instanceCodeMember
s defined in this unit that have the given name.- Parameters:
name
- the name of the members to includetraverseHierarchy
- iftrue
, 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 givenCodeMemberDescriptor
, according toCodeMemberDescriptor.matchesSignature(CodeMemberDescriptor)
.- Parameters:
signature
- the descriptor providing the signature to matchtraverseHierarchy
- iftrue
, also returns instance code members from superunits, transitively- Returns:
- the collection of instance code members that match the given signature
-
-