Package it.unive.lisa.program
Class CompilationUnit
- java.lang.Object
-
- it.unive.lisa.program.Unit
-
- it.unive.lisa.program.CompilationUnit
-
- All Implemented Interfaces:
CodeElement
public class CompilationUnit extends Unit
A compilation unit of the program to analyze. A compilation unit is aUnitthat also defines instance members, that can be inherited by subunits.
-
-
Constructor Summary
Constructors Constructor Description CompilationUnit(CodeLocation location, java.lang.String name, boolean sealed)Builds a compilation unit, defined at the given program point.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanaddInstanceCFG(CFG cfg)Adds a new instanceCFG, identified by its signature (CFGDescriptor.getSignature()), to this unit.booleanaddInstanceConstruct(NativeCFG construct)Adds a new instanceNativeCFG, identified by its signature (CFGDescriptor.getSignature()), to this unit.booleanaddInstanceGlobal(Global global)Adds a new instanceGlobal, identified by its name (Global.getName()), to this unit.booleanaddSuperUnit(CompilationUnit unit)Adds a newCompilationUnitas superunit of this unit.java.util.Collection<CFG>getAllCFGs()Yields the collection of all theCFGs defined in this unit.java.util.Collection<NativeCFG>getAllConstructs()Yields the collection of all theNativeCFGs defined in this unit.java.util.Collection<Global>getAllGlobals()Yields the collection of all theGlobals defined in this unit.CFGgetInstanceCFG(java.lang.String signature, boolean traverseHierarchy)Yields the instanceCFGdefined in this unit having the given signature (CFGDescriptor.getSignature()), if any.java.util.Collection<CFG>getInstanceCFGs(boolean traverseHierarchy)Yields the collection of instanceCFGs defined in this unit.java.util.Collection<CFG>getInstanceCFGsByName(java.lang.String name, boolean traverseHierarchy)Yields the collection of all instanceCFGs defined in this unit that have the given name.CodeMembergetInstanceCodeMember(java.lang.String signature, boolean traverseHierarchy)Yields the instanceCodeMemberdefined in this unit having the given signature (CFGDescriptor.getSignature()), if any.java.util.Collection<CodeMember>getInstanceCodeMembers(boolean traverseHierarchy)Yields the collection of instanceCodeMembers defined in this unit.java.util.Collection<CodeMember>getInstanceCodeMembersByName(java.lang.String name, boolean traverseHierarchy)Yields the collection of all instanceCodeMembers defined in this unit that have the given name.NativeCFGgetInstanceConstruct(java.lang.String signature, boolean traverseHierarchy)Yields the instanceNativeCFGdefined in this unit having the given signature (CFGDescriptor.getSignature()), if any.java.util.Collection<NativeCFG>getInstanceConstructs(boolean traverseHierarchy)Yields the collection of instance constructs (NativeCFGs) defined in this unit.java.util.Collection<NativeCFG>getInstanceConstructsByName(java.lang.String name, boolean traverseHierarchy)Yields the collection of all instanceNativeCFGs defined in this unit that have the given name.GlobalgetInstanceGlobal(java.lang.String name, boolean traverseHierarchy)Yields the instanceGlobaldefined in this unit having the given name (Global.getName()), if any.java.util.Collection<Global>getInstanceGlobals(boolean traverseHierarchy)Yields the collection of instanceGlobals defined in this unit.java.util.Collection<CompilationUnit>getInstances()Yields the collection ofCompilationUnits that are instances of this one, including itself.java.util.Collection<CodeMember>getMatchingInstanceCodeMembers(CFGDescriptor signature, boolean traverseHierarchy)Finds all the instance code members whose signature matches the one of the givenCFGDescriptor, according toCFGDescriptor.matchesSignature(CFGDescriptor).java.util.Collection<CompilationUnit>getSuperUnits()Yields the collection ofCompilationUnits that this unit directly inherits from.booleanisInstanceOf(CompilationUnit unit)Yieldstrueif and only if this unit is an instance of the given one.booleanisSealed()Yields whether or not this unit is sealed, meaning that it cannot be used as super unit of other compilation units.voidvalidateAndFinalize()Validates this unit, ensuring its consistency.-
Methods inherited from class it.unive.lisa.program.Unit
addCFG, addConstruct, addGlobal, equals, getAllCodeMembers, getCFG, getCFGs, getCFGsByName, getCodeMember, getCodeMembers, getCodeMembersByName, getConstruct, getConstructs, getConstructsByName, getGlobal, getGlobals, getLocation, getMatchingCodeMember, getName, hashCode, toString
-
-
-
-
Constructor Detail
-
CompilationUnit
public CompilationUnit(CodeLocation location, java.lang.String name, boolean sealed)
Builds a compilation unit, defined at the given program point.- Parameters:
location- the location where the unit is define within the source filename- the name of the unitsealed- whether or not this unit is sealed, meaning that it cannot be used as super unit of other compilation units
-
-
Method Detail
-
isSealed
public final boolean isSealed()
Yields whether or not this unit is sealed, meaning that it cannot be used as super unit of other compilation units.- Returns:
trueif this unit is sealed
-
getSuperUnits
public final java.util.Collection<CompilationUnit> getSuperUnits()
Yields the collection ofCompilationUnits that this unit directly inherits from. The returned collection does not include units that are transitively inherited.- Returns:
- the collection of direct super units
-
getInstances
public final java.util.Collection<CompilationUnit> getInstances()
Yields the collection ofCompilationUnits 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, untilvalidateAndFinalize()has been called.- Returns:
- the collection of units that are instances of this one, including this unit itself
-
getInstanceGlobals
public final java.util.Collection<Global> getInstanceGlobals(boolean traverseHierarchy)
Yields the collection of instanceGlobals 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 final java.util.Collection<CFG> getInstanceCFGs(boolean traverseHierarchy)
Yields the collection of instanceCFGs defined in this unit. Each cfg is uniquely identified by its signature (CFGDescriptor.getSignature()), meaning that there are no two instance cfgs having the same signature in each unit. Instance cfgs can be overridden inside subunits, according toCFGDescriptor.isOverridable().- Parameters:
traverseHierarchy- iftrue, also returns instance cfgs from superunits, transitively- Returns:
- the collection of instance cfgs
-
getInstanceConstructs
public final 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 (CFGDescriptor.getSignature()), meaning that there are no two instance constructs having the same signature in each unit. Instance constructs can be overridden inside subunits, according toCFGDescriptor.isOverridable().- Parameters:
traverseHierarchy- iftrue, also returns instance constructs from superunits, transitively- Returns:
- the collection of instance constructs
-
addSuperUnit
public final boolean addSuperUnit(CompilationUnit unit)
Adds a newCompilationUnitas superunit of this unit.- Parameters:
unit- the unit to add- Returns:
trueif the collection of superunits changed as a result of the call
-
addInstanceGlobal
public final boolean addInstanceGlobal(Global global)
Adds a new instanceGlobal, identified by its name (Global.getName()), to this unit.- Parameters:
global- the global to add- Returns:
trueif there was no instance global previously associated with the same name,falseotherwise. If this method returnsfalse, the given global is discarded.
-
addInstanceCFG
public final boolean addInstanceCFG(CFG cfg)
Adds a new instanceCFG, identified by its signature (CFGDescriptor.getSignature()), to this unit. Instance cfgs can be overridden inside subunits, according toCFGDescriptor.isOverridable().- Parameters:
cfg- the cfg to add- Returns:
trueif there was no instance cfg previously associated with the same signature,falseotherwise. If this method returnsfalse, the given cfg is discarded.
-
addInstanceConstruct
public final boolean addInstanceConstruct(NativeCFG construct)
Adds a new instanceNativeCFG, identified by its signature (CFGDescriptor.getSignature()), to this unit. Instance constructs can be overridden inside subunits, according toCFGDescriptor.isOverridable().- Parameters:
construct- the construct to add- Returns:
trueif there was no instance construct previously associated with the same signature,falseotherwise. If this method returnsfalse, the given construct is discarded.
-
getInstanceCFG
public final CFG getInstanceCFG(java.lang.String signature, boolean traverseHierarchy)
Yields the instanceCFGdefined in this unit having the given signature (CFGDescriptor.getSignature()), if any.- Parameters:
signature- the signature of the cfg to findtraverseHierarchy- iftrue, also returns instance cfgs from superunits, transitively- Returns:
- the instance cfg with the given signature, or
null
-
getInstanceConstruct
public final NativeCFG getInstanceConstruct(java.lang.String signature, boolean traverseHierarchy)
Yields the instanceNativeCFGdefined in this unit having the given signature (CFGDescriptor.getSignature()), if any.- Parameters:
signature- the signature of the construct to findtraverseHierarchy- iftrue, also returns instance constructs from superunits, transitively- Returns:
- the instance construct with the given signature, or
null
-
getInstanceGlobal
public final Global getInstanceGlobal(java.lang.String name, boolean traverseHierarchy)
Yields the instanceGlobaldefined 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
-
getInstanceCodeMember
public final CodeMember getInstanceCodeMember(java.lang.String signature, boolean traverseHierarchy)
Yields the instanceCodeMemberdefined in this unit having the given signature (CFGDescriptor.getSignature()), if any. This method searches the code member both among the instance cfgs and instance constructs defined in this unit.- Parameters:
signature- the signature of the code member to findtraverseHierarchy- iftrue, also returns instance code members from superunits, transitively- Returns:
- the instance code member with the given signature, or
null
-
getInstanceCFGsByName
public final java.util.Collection<CFG> getInstanceCFGsByName(java.lang.String name, boolean traverseHierarchy)
Yields the collection of all instanceCFGs defined in this unit that have the given name.- Parameters:
name- the name of the constructs to includetraverseHierarchy- iftrue, also returns instance cfgs from superunits, transitively- Returns:
- the collection of instance cfgs with the given name
-
getInstanceConstructsByName
public final java.util.Collection<NativeCFG> getInstanceConstructsByName(java.lang.String name, boolean traverseHierarchy)
Yields the collection of all instanceNativeCFGs defined in this unit that have the given name.- Parameters:
name- the name of the constructs to includetraverseHierarchy- iftrue, also returns instance constructs from superunits, transitively- Returns:
- the collection of instance constructs with the given name
-
getInstanceCodeMembersByName
public final java.util.Collection<CodeMember> getInstanceCodeMembersByName(java.lang.String name, boolean traverseHierarchy)
Yields the collection of all instanceCodeMembers defined in this unit that have the given name. This method searches the code member both among the instance cfgs and instance constructs defined in this unit.- Parameters:
name- the name of the code members to includetraverseHierarchy- iftrue, also returns instance code members from superunits, transitively- Returns:
- the collection of code members with the given name
-
getMatchingInstanceCodeMembers
public final java.util.Collection<CodeMember> getMatchingInstanceCodeMembers(CFGDescriptor signature, boolean traverseHierarchy)
Finds all the instance code members whose signature matches the one of the givenCFGDescriptor, according toCFGDescriptor.matchesSignature(CFGDescriptor).- 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
-
getAllCFGs
public java.util.Collection<CFG> getAllCFGs()
Yields the collection of all theCFGs defined in this unit.
This method also returns all the instance cfgs defined in this unit.- Overrides:
getAllCFGsin classUnit- Returns:
- the collection of the cfgs
-
getAllGlobals
public java.util.Collection<Global> getAllGlobals()
Yields the collection of all theGlobals defined in this unit.
This method also returns all the instance globals defined in this unit.- Overrides:
getAllGlobalsin classUnit- Returns:
- the collection of the globals
-
getAllConstructs
public java.util.Collection<NativeCFG> getAllConstructs()
Yields the collection of all theNativeCFGs defined in this unit.
This method also returns all the instance constructs defined in this unit.- Overrides:
getAllConstructsin classUnit- Returns:
- the collection of the cfgs
-
getInstanceCodeMembers
public final java.util.Collection<CodeMember> getInstanceCodeMembers(boolean traverseHierarchy)
Yields the collection of instanceCodeMembers defined in this unit. This method returns the union ofgetInstanceCFGs(boolean)andgetInstanceConstructs(boolean).- Parameters:
traverseHierarchy- iftrue, also returns instance code members from superunits, transitively- Returns:
- the collection of instance code members
-
isInstanceOf
public final boolean isInstanceOf(CompilationUnit unit)
Yieldstrueif and only if this unit is an instance of the given one. This method works correctly even ifvalidateAndFinalize()has not been called yet, and thus the if collection of instances of the given unit is not yet available.- Parameters:
unit- the other unit- Returns:
trueonly if that condition holds
-
validateAndFinalize
public final void validateAndFinalize() throws ProgramValidationExceptionValidates this unit, ensuring its consistency. This ensures that no two code members exist in this unit whose signatures matches one another, according toCFGDescriptor.matchesSignature(CFGDescriptor). This avoids ambiguous call resolution. Moreover, this ensures that allCFGs are valid, according toCFG.validate().
Validating a compilation unit causes the validation of all its super units, and the population of the set of instances (getInstances()) of each element in its hierarchy. Moreover, the validation ensures that no duplicate instance code members are defined in the compilation unit, according toCFGDescriptor.matchesSignature(CFGDescriptor), to avoid ambiguous call resolutions. Instance code members are also linked to other ones in the hierarchy, populating the collectionsCFGDescriptor.overriddenBy()andCFGDescriptor.overrides().- Overrides:
validateAndFinalizein classUnit- Throws:
ProgramValidationException- if the program has an invalid structure
-
-