Class Instruction
java.lang.Object
net.covers1624.coffeegrinder.bytecode.Instruction
- All Implemented Interfaces:
net.covers1624.quack.util.Copyable<Instruction>
- Direct Known Subclasses:
AbstractInvoke,AbstractLoop,ArrayLen,Assert,Binary,Block,BlockContainer,ClassDecl,Compare,Comparison,CompoundAssignment,DeadCode,FieldDecl,IfInstruction,InvokeDynamic,Leave,LocalVariable,LogicAnd,LogicOr,MethodDecl,MethodReference,NewArray,Reference,Return,SimpleInstruction,Store,Switch,SwitchTable,SwitchTable.SwitchSection,Synchronized,Ternary,TryCatch.TryCatchHandler,TryInstruction,Unary,Yield
public abstract class Instruction
extends Object
implements net.covers1624.quack.util.Copyable<Instruction>
Represents an Instruction within an AST tree.
Created by covers1624 on 21/9/21.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract <R,C> R accept(InsnVisitor<R, C> visitor, C ctx) Pass thisInstructionthrough the suppliedInsnVisitor.final <R> Raccept(InsnVisitor<R, None> visitor) Passes thisInstructionthrough the suppliedInsnVisitor.final voidaddRef()Increments the reference count of this instruction.final <R extends Instruction>
net.covers1624.quack.collection.FastStream<R> ancestorsOfType(Class<? extends R> type) Returns a filteredFastStreamof ancestors.protected EnumBitSet<InstructionFlag> Called to compute the exposed flags exposed viagetFlags().copy()Deep copy this instruction.final <R extends Instruction>
net.covers1624.quack.collection.FastStream<R> descendantsMatching(Function<Instruction, @Nullable R> filter) Returns a filteredFastStreamof descendants in post-order.final <R extends Instruction>
net.covers1624.quack.collection.FastStream<R> descendantsOfType(Class<? extends R> type) Returns a filteredFastStreamof descendants in post-order.final <R extends Instruction>
LinkedList<R> descendantsToList(Class<? extends R> type) Returns aLinkedListof descendants in post-order.final <R extends Instruction>
LinkedList<R> descendantsToListWhere(Predicate<Instruction> filter) Returns aLinkedListof descendants in post-order.final <R extends Instruction>
net.covers1624.quack.collection.FastStream<R> descendantsWhere(Predicate<Instruction> filter) Returns a filteredFastStreamof descendants in post-order.final <R extends Instruction>
RfirstAncestorOfType(Class<? extends R> type) Returns the first parentInstructionwith the provided type.final intGets the offset of this instruction in bytecode.final net.covers1624.quack.collection.FastStream<Instruction> Gets aFastStreamcontaining thisInstructions children.final net.covers1624.quack.collection.FastStream<Instruction> Gets aFastStreamiterating all descendants including thisInstructionin post-order.abstract EnumBitSet<InstructionFlag> Any direct flags for thisInstruction.final InstructionThisInstructions first child instruction.final @Nullable InstructionThisInstructions first child instruction.final EnumBitSet<InstructionFlag> getFlags()Gets the flags describing the behavior of thisInstruction.final InstructionThisInstructions last child instruction.final @Nullable InstructionThisInstructions last child instruction.final InstructionThisInstructions next sibling instruction.final @Nullable InstructionThisInstructions next sibling instruction.final InstructionTheInstructionwhich this instruction is a child of.final @Nullable InstructionTheInstructionwhich this instruction is a child of.final InstructionThisInstructions previous sibling instruction.final @Nullable InstructionThisInstructions previous sibling instruction.abstract ATypeGets the stack type of the value produced by thisInstruction.final intGets the captured source line as represented by the LineNumberTable.final @Nullable InsnTaggetTag()An optional data tag for this instruction.final booleanhasDirectFlag(InstructionFlag flag) Returns weather this instruction exposes this specific flag directly.final booleanhasFlag(InstructionFlag flag) Returns weather this instruction exposes this specific flag either directly, or from one of its children.final voidinsertAfter(Instruction value) Inserts anInstructionafter this one inside anInstructionCollection.final voidinsertBefore(Instruction value) Inserts anInstructionbefore this one inside anInstructionCollection.protected voidfinal booleanUsed to check if thisInstructionhas any references and is connected to a tree.final booleanisDescendantOf(@Nullable Instruction possibleAncestor) Checks if thisInstructionor one of its parents ispossibleAncestor.protected voidprotected voidprotected voidfinal voidDecrements the reference count of this instruction.final voidremove()Removes thisInstructionfrom anInstructionCollection.final <T extends Instruction>
TreplaceWith(T value) Replace thisInstructionin the tree with anotherInstruction.final voidsetBytecodeOffset(int bytecodeOffset) Set the offset for this instruction in bytecode.final voidsetOffsets(Instruction sourceInsn) Sets the bytecode offset and tracked source line of this instruction to that of the given instruction.final voidsetSourceLine(int sourceLine) Sets the captured source line.final voidSets the optional data tag for this instruction.toString()toString(DebugPrintOptions opts) final <T extends Instruction>
TwithOffsets(Instruction sourceInsn) Sets the bytecode offset and tracked source line of this instruction to that of the given instruction.
-
Constructor Details
-
Instruction
protected Instruction()
-
-
Method Details
-
getResultType
Gets the stack type of the value produced by thisInstruction.- Returns:
- The
AType.
-
copy
Deep copy this instruction.This many not be supported by all instructions.
Implementors should use copy-constructor syntax.
- Specified by:
copyin interfacenet.covers1624.quack.util.Copyable<Instruction>- Returns:
- The copied instruction.
- Throws:
UnsupportedOperationException- If this instruction does not support being copied.
-
getDirectFlags
Any direct flags for thisInstruction.The return value should be cached statically inside the
Instructionimpl.- Returns:
- An
EnumBitSetrepresenting the flags. - See Also:
-
getFlags
Gets the flags describing the behavior of thisInstruction. This method wil compute the flags on-demand and caches the result, until some changes the AST invalidate the cache.- Returns:
- An
EnumBitSetrepresenting the flags. - See Also:
-
hasFlag
Returns weather this instruction exposes this specific flag either directly, or from one of its children.- Parameters:
flag- The flag to check.- Returns:
- If the flag is present.
-
hasDirectFlag
Returns weather this instruction exposes this specific flag directly.- Parameters:
flag- The flag to check.- Returns:
- If the flag is present.
-
accept
Pass thisInstructionthrough the suppliedInsnVisitor.- Parameters:
visitor- TheInsnVisitor.ctx- The context.- Returns:
- The return result from the
InsnVisitor.
-
accept
Passes thisInstructionthrough the suppliedInsnVisitor. This method will assume the generic ofNonefor the Context generic provided byInsnVisitor- Parameters:
visitor- TheInsnVisitor.- Returns:
- The return result from the
InsnVisitor.
-
getParentOrNull
TheInstructionwhich this instruction is a child of.- Returns:
- The parent
Instructionornull - See Also:
-
getParent
TheInstructionwhich this instruction is a child of.- Returns:
- The parent
Instruction. - See Also:
-
getNextSiblingOrNull
ThisInstructions next sibling instruction.- Returns:
- The next sibling
Instructionornull. - See Also:
-
getNextSibling
ThisInstructions next sibling instruction.- Returns:
- The next sibling
Instruction. - See Also:
-
getPrevSiblingOrNull
ThisInstructions previous sibling instruction.- Returns:
- The previous sibling
Instructionornull. - See Also:
-
getPrevSibling
ThisInstructions previous sibling instruction.- Returns:
- The previous sibling
Instruction. - See Also:
-
getFirstChildOrNull
ThisInstructions first child instruction.- Returns:
- The first child
Instructionornullif this instruction has no children. - See Also:
-
getFirstChild
ThisInstructions first child instruction.- Returns:
- The first child
Instruction. - See Also:
-
getLastChildOrNull
ThisInstructions last child instruction.- Returns:
- The last child
Instructionornullif this instruction has no children. - See Also:
-
getLastChild
ThisInstructions last child instruction.- Returns:
- The last child
Instruction. - See Also:
-
getChildren
Gets aFastStreamcontaining thisInstructions children.- Returns:
- The
FastStream.
-
getDescendants
Gets aFastStreamiterating all descendants including thisInstructionin post-order.- Returns:
- The
FastStream.
-
descendantsOfType
public final <R extends Instruction> net.covers1624.quack.collection.FastStream<R> descendantsOfType(Class<? extends R> type) Returns a filteredFastStreamof descendants in post-order.- Parameters:
type- The Instruction class type to filter by.- Returns:
- The filtered
FastStream.
-
descendantsWhere
public final <R extends Instruction> net.covers1624.quack.collection.FastStream<R> descendantsWhere(Predicate<Instruction> filter) Returns a filteredFastStreamof descendants in post-order.This function is intended to be used by static matching methods.
- Parameters:
filter- The filter to apply.- Returns:
- The filtered
FastStream.
-
descendantsMatching
public final <R extends Instruction> net.covers1624.quack.collection.FastStream<R> descendantsMatching(Function<Instruction, @Nullable R> filter) Returns a filteredFastStreamof descendants in post-order.This function is intended to be used by static matching methods which return
nullto indicate a failed match.- Parameters:
filter- The filter to apply.- Returns:
- The filtered
FastStream.
-
descendantsToList
Returns aLinkedListof descendants in post-order.- Parameters:
type- The Instruction class type to filter by.- Returns:
- The
LinkedList.
-
descendantsToListWhere
public final <R extends Instruction> LinkedList<R> descendantsToListWhere(Predicate<Instruction> filter) Returns aLinkedListof descendants in post-order.This function is intended to be used by static matching methods.
- Parameters:
filter- The filter to apply.- Returns:
- The
LinkedList.
-
firstAncestorOfType
Returns the first parentInstructionwith the provided type.- Parameters:
type- The Instruction class type to filter by.- Returns:
- The first parent
Instruction.
-
ancestorsOfType
public final <R extends Instruction> net.covers1624.quack.collection.FastStream<R> ancestorsOfType(Class<? extends R> type) Returns a filteredFastStreamof ancestors.- Parameters:
type- The Instruction class type to filter by.- Returns:
- The filtered
FastStream.
-
isDescendantOf
Checks if thisInstructionor one of its parents ispossibleAncestor.- Parameters:
possibleAncestor- The ancestor to check against.- Returns:
- If this
Instructionis a descendant ofpossibleAncestor.
-
addRef
public final void addRef()Increments the reference count of this instruction.This should generally not be directly used.
- See Also:
-
releaseRef
public final void releaseRef()Decrements the reference count of this instruction.This should generally not be directly used.
- See Also:
-
isConnected
public final boolean isConnected()Used to check if thisInstructionhas any references and is connected to a tree.- Returns:
- If this
Instructionis connected.
-
remove
public final void remove()Removes thisInstructionfrom anInstructionCollection.- Throws:
UnsupportedOperationException- If thisInstructionis not contained within a collection.
-
insertBefore
Inserts anInstructionbefore this one inside anInstructionCollection.- Parameters:
value- TheInstructionto insert.- Throws:
UnsupportedOperationException- If thisInstructionis not contain
-
insertAfter
Inserts anInstructionafter this one inside anInstructionCollection.- Parameters:
value- TheInstructionto insert.- Throws:
UnsupportedOperationException- If thisInstructionis not contain
-
replaceWith
Replace thisInstructionin the tree with anotherInstruction.This function will efficiently remove and unlink the instruction from wherever else it was in the tree prior if required.
- Parameters:
value- The value to replace with.
-
getTag
An optional data tag for this instruction.- Returns:
- The tag.
-
setTag
Sets the optional data tag for this instruction.- Parameters:
tag- The data tag.nullto clear.s- Throws:
IllegalStateException- If thisInstructionalready has a tag set.
-
getBytecodeOffset
public final int getBytecodeOffset()Gets the offset of this instruction in bytecode.- Returns:
- The offset.
-
setBytecodeOffset
public final void setBytecodeOffset(int bytecodeOffset) Set the offset for this instruction in bytecode.- Parameters:
bytecodeOffset- The offset.
-
withOffsets
Sets the bytecode offset and tracked source line of this instruction to that of the given instruction.- Parameters:
sourceInsn- The source instruction to copy from.- Returns:
- This instruction.
-
setOffsets
Sets the bytecode offset and tracked source line of this instruction to that of the given instruction.- Parameters:
sourceInsn- The source instruction to copy from.
-
getSourceLine
public final int getSourceLine()Gets the captured source line as represented by the LineNumberTable.- Returns:
- The line number.
-
setSourceLine
public final void setSourceLine(int sourceLine) Sets the captured source line.- Parameters:
sourceLine- The source line.
-
toString
-
toString
-
invalidateFlags
protected void invalidateFlags() -
onConnected
@MustBeInvokedByOverriders protected void onConnected() -
onDisconnected
@MustBeInvokedByOverriders protected void onDisconnected() -
onChildModified
protected void onChildModified() -
computeFlags
Called to compute the exposed flags exposed viagetFlags().- Returns:
- The computed flags.
-