Class NewArrayExpr
- java.lang.Object
-
- com.tonic.analysis.source.ast.expr.NewArrayExpr
-
- All Implemented Interfaces:
ASTNode,Expression
public final class NewArrayExpr extends Object implements Expression
An array allocation - either sized by dimension expressions or by an initializer.
-
-
Constructor Summary
Constructors Constructor Description NewArrayExpr(SourceType elementType, ArrayInitExpr initializer)Creates an allocation with no explicit dimensions, sized by its initializer.NewArrayExpr(SourceType elementType, List<Expression> dimensions)Creates an allocation with explicit dimensions and no initializer.NewArrayExpr(SourceType elementType, List<Expression> dimensions, ArrayInitExpr initializer, SourceType type, SourceLocation location)Creates an array allocation and adopts the dimensions and initializer as children.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> Taccept(SourceVisitor<T> visitor)Accepts a visitor for this node.voidaddDimension(Expression dim)Appends a dimension expression and adopts it as a child.List<ASTNode>getChildren()Lists the direct children a node holds, in source order.intgetDimensionCount()List<Expression>getDimensions()SourceTypegetElementType()ArrayInitExprgetInitializer()SourceLocationgetLocation()Gets the source location of this node.ASTNodegetParent()Gets the parent node in the AST tree.SourceTypegetType()Gets the inferred type of this expression.booleanhasInitializer()voidsetInitializer(ArrayInitExpr initializer)voidsetParent(ASTNode parent)Sets the parent node in the AST tree.StringtoString()static NewArrayExprwithInit(SourceType elementType, ArrayInitExpr init)Creates an allocation whose length comes from an initializer.NewArrayExprwithInitializer(ArrayInitExpr initializer)Replaces the initializer in place, reparenting the new one and releasing the old one.static NewArrayExprwithSize(SourceType elementType, Expression size)Creates a one-dimensional allocation with an explicit length.-
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.tonic.analysis.source.ast.ASTNode
deepClone, findAll, findAll, findAncestor, findFirst, findFirst, getRoot, isDescendantOf, remove, replaceWith, stream, stream, walk
-
-
-
-
Constructor Detail
-
NewArrayExpr
public NewArrayExpr(SourceType elementType, List<Expression> dimensions, ArrayInitExpr initializer, SourceType type, SourceLocation location)
Creates an array allocation and adopts the dimensions and initializer as children.- Parameters:
elementType- the element typedimensions- the length expressions, may be nullinitializer- the initializer, may be nulltype- the expression type; null derives an array type from the element type and dimension countlocation- the source location, null becomes UNKNOWN- Throws:
NullPointerException- if the element type is null
-
NewArrayExpr
public NewArrayExpr(SourceType elementType, List<Expression> dimensions)
Creates an allocation with explicit dimensions and no initializer.- Parameters:
elementType- the element typedimensions- the length expressions, may be null
-
NewArrayExpr
public NewArrayExpr(SourceType elementType, ArrayInitExpr initializer)
Creates an allocation with no explicit dimensions, sized by its initializer.- Parameters:
elementType- the element typeinitializer- the initializer
-
-
Method Detail
-
getElementType
public SourceType getElementType()
- Returns:
- the element type
-
getDimensions
public List<Expression> getDimensions()
- Returns:
- the dimensions
-
getInitializer
public ArrayInitExpr getInitializer()
- Returns:
- the initializer
-
setInitializer
public void setInitializer(ArrayInitExpr initializer)
- Parameters:
initializer- the new initializer, may be null
-
getType
public SourceType getType()
Description copied from interface:ExpressionGets the inferred type of this expression.- Specified by:
getTypein interfaceExpression- Returns:
- the type
-
getLocation
public SourceLocation getLocation()
Description copied from interface:ASTNodeGets the source location of this node.- Specified by:
getLocationin interfaceASTNode- Returns:
- the location
-
getParent
public ASTNode getParent()
Description copied from interface:ASTNodeGets the parent node in the AST tree.
-
setParent
public void setParent(ASTNode parent)
Description copied from interface:ASTNodeSets the parent node in the AST tree.
-
withSize
public static NewArrayExpr withSize(SourceType elementType, Expression size)
Creates a one-dimensional allocation with an explicit length.- Parameters:
elementType- the element typesize- the length expression- Returns:
- the expression
-
withInit
public static NewArrayExpr withInit(SourceType elementType, ArrayInitExpr init)
Creates an allocation whose length comes from an initializer.- Parameters:
elementType- the element typeinit- the initializer- Returns:
- the expression
-
addDimension
public void addDimension(Expression dim)
Appends a dimension expression and adopts it as a child.- Parameters:
dim- the dimension expression
-
getDimensionCount
public int getDimensionCount()
- Returns:
- the number of dimension expressions
-
hasInitializer
public boolean hasInitializer()
- Returns:
- true if an initializer is attached
-
withInitializer
public NewArrayExpr withInitializer(ArrayInitExpr initializer)
Replaces the initializer in place, reparenting the new one and releasing the old one.- Parameters:
initializer- the new initializer, may be null- Returns:
- this expression
-
getChildren
public List<ASTNode> getChildren()
Description copied from interface:ASTNodeLists the direct children a node holds, in source order.- Specified by:
getChildrenin interfaceASTNode- Returns:
- the direct children, empty for a leaf
-
accept
public <T> T accept(SourceVisitor<T> visitor)
Description copied from interface:ASTNodeAccepts a visitor for this node.
-
-