Class 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 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 type
        dimensions - the length expressions, may be null
        initializer - the initializer, may be null
        type - the expression type; null derives an array type from the element type and dimension count
        location - 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 type
        dimensions - 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 type
        initializer - 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: Expression
        Gets the inferred type of this expression.
        Specified by:
        getType in interface Expression
        Returns:
        the type
      • getLocation

        public SourceLocation getLocation()
        Description copied from interface: ASTNode
        Gets the source location of this node.
        Specified by:
        getLocation in interface ASTNode
        Returns:
        the location
      • getParent

        public ASTNode getParent()
        Description copied from interface: ASTNode
        Gets the parent node in the AST tree.
        Specified by:
        getParent in interface ASTNode
        Returns:
        the parent
      • setParent

        public void setParent​(ASTNode parent)
        Description copied from interface: ASTNode
        Sets the parent node in the AST tree.
        Specified by:
        setParent in interface ASTNode
        Parameters:
        parent - the enclosing node
      • withSize

        public static NewArrayExpr withSize​(SourceType elementType,
                                            Expression size)
        Creates a one-dimensional allocation with an explicit length.
        Parameters:
        elementType - the element type
        size - 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 type
        init - 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: ASTNode
        Lists the direct children a node holds, in source order.
        Specified by:
        getChildren in interface ASTNode
        Returns:
        the direct children, empty for a leaf
      • accept

        public <T> T accept​(SourceVisitor<T> visitor)
        Description copied from interface: ASTNode
        Accepts a visitor for this node.
        Specified by:
        accept in interface ASTNode
        Type Parameters:
        T - the return type of the visitor
        Parameters:
        visitor - the visitor to accept
        Returns:
        the result from the visitor