scale.clef.expr
Class SubscriptOp

java.lang.Object
  extended by scale.common.Root
      extended by scale.clef.Node
          extended by scale.clef.expr.Expression
              extended by scale.clef.expr.SubscriptOp
All Implemented Interfaces:
AnnotationInterface, DisplayNode
Direct Known Subclasses:
SubscriptAddressOp, SubscriptValueOp

public abstract class SubscriptOp
extends Expression

This is the base class for subscripting operations.

$Id: SubscriptOp.java,v 1.64 2007-10-04 19:58:06 burrill Exp $

Copyright 2008 by the Scale Compiler Group,
Department of Computer Science
University of Massachusetts,
Amherst MA. 01003, USA
All Rights Reserved.

Regardless of whether the source language uses row major or column major array ordering, the subscripts represented by an instance of this class are always in row major (C style) ordering.

There is a difference between how subscripting is represented between C and Fortran.

Array DefReferenceCFortran
int x1[10] x1 pointer to int pointer to 1-D array of int
int (*p1)[10] p1 pointer to 1-D array of int NA
int x2[10][10] x2 pointer to 1-D array of int pointer to 2-D array of int
int (*p2)[10][10] p2 pointer to 2-D array of int NA

The C representation is necessary in order for pointer-arithmetic and subscripting to be interchangeable. It also requires that all C arrays be 0-origin indexed.

For Fortran, arrays can be defined using any integer origin. Therefore, in order for the proper array offset to be calculated, it is necessary for the complete array information to be available from the type of the array operand of a SubscriptOp instance.

An identical representation, as is used after parsing (SubscriptExpr, ArrayIndexExpr), cannot be used by the C parser because the C language allows both subscripting notation and pointer arithmetic to be combined in the same array access expression.

See Also:
setFortranArray(), isFortranArray()

Constructor Summary
SubscriptOp(Type type, Expression array, Vector<Expression> subscripts)
           
 
Method Summary
 void addIndex(Expression index)
          Add another index to the subscript expression.
 void addToIndex(int i, Expression add)
          Add another index to the subscript expression.
 boolean containsDeclaration(Declaration decl)
          Return true if this expression contains a reference to the variable.
 boolean equivalent(java.lang.Object exp)
          Return true if the two expressions are equivalent.
 Expression getArray()
          Return the array address expression.
 Node getChild(int i)
          Return the specified AST child of this node.
 long getConstantIndex()
          Return the constant linear index or a negative value if not constant.
 void getDeclList(java.util.AbstractCollection<Declaration> varList)
          Add all declarations referenced in this expression to the collection.
 Expression getSubscript(int i)
          Return the specified subscript.
protected  Vector<Expression> getSubscripts()
          Return the vector of index expressions.
 boolean isFortranArray()
          Return true if this instance is a Fortran array subscript.
 int numChildren()
          Return the number of AST children of this node.
 int numSubscripts()
          Return the number of subscripts.
protected  void setArray(Expression array)
          Specify the array address expression.
 void setFortranArray()
          Specify that this instance is a Fortran array subscript.
 void visit(Predicate p)
          Process a node by calling its associated routine.
 
Methods inherited from class scale.clef.expr.Expression
canonical, getConstantValue, getCoreType, getDisplayColorHint, getDisplayLabel, getDisplayShapeHint, getPointedToCore, getType, hasTrueFalseResult, isSimpleOp, setType, toStringSpecial
 
Methods inherited from class scale.clef.Node
getDecl, getSourceLineNumber, setAnnotationLevel, setReportLevel, setSourceLineNumber, toString, toString, toStringChildren
 
Methods inherited from class scale.common.Root
addAnnotation, allAnnotations, allMatchingAnnotations, getAnnotation, getDisplayName, getDisplayString, getNodeCount, getNodeID, hasAnnotation, hasEqualAnnotation, hashCode, removeAnnotation, removeAnnotations, toStringAnnotations, toStringClass, trace, trace, trace
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SubscriptOp

public SubscriptOp(Type type,
                   Expression array,
                   Vector<Expression> subscripts)
Method Detail

isFortranArray

public final boolean isFortranArray()
Return true if this instance is a Fortran array subscript.


setFortranArray

public final void setFortranArray()
Specify that this instance is a Fortran array subscript.


equivalent

public boolean equivalent(java.lang.Object exp)
Return true if the two expressions are equivalent.

Overrides:
equivalent in class Expression

visit

public void visit(Predicate p)
Description copied from class: Node
Process a node by calling its associated routine. See the "visitor" design pattern in Design Patterns: Elements of Reusable Object-Oriented Software by E. Gamma, et al, Addison Wesley, ISBN 0-201-63361-2.

Each class has a visit(Predicate p) method. For example, in class ABC:

   public void visit(Predicate p)
   {
     p.visitABC(this);
   }
 
and the class that implements Predicate has a method
   public void visitABC(Node n)
   {
     ABC a = (ABC) n;
     ...
   }
 
Thus, the class that implements Predicate can call
   n.visit(this);
 
where n is a Node sub-class without determining which specific sub-class n is. The visit pattern basically avoids implementing a large switch statement or defining different methods in each class for some purpose.

Overrides:
visit in class Expression
See Also:
Predicate

getArray

public final Expression getArray()
Return the array address expression.


getSubscripts

protected Vector<Expression> getSubscripts()
Return the vector of index expressions.


setArray

protected final void setArray(Expression array)
Specify the array address expression.


getChild

public Node getChild(int i)
Return the specified AST child of this node.

Overrides:
getChild in class Node

numChildren

public int numChildren()
Return the number of AST children of this node.

Overrides:
numChildren in class Node

numSubscripts

public final int numSubscripts()
Return the number of subscripts.


getSubscript

public Expression getSubscript(int i)
Return the specified subscript. Regardless of whether the source language uses row major or column major array ordering, the subscripts represented by this expression are always in row major (C style) ordering.


addIndex

public final void addIndex(Expression index)
Add another index to the subscript expression.


addToIndex

public final void addToIndex(int i,
                             Expression add)
Add another index to the subscript expression.


getConstantIndex

public long getConstantIndex()
Return the constant linear index or a negative value if not constant.


containsDeclaration

public boolean containsDeclaration(Declaration decl)
Return true if this expression contains a reference to the variable.

Overrides:
containsDeclaration in class Expression

getDeclList

public void getDeclList(java.util.AbstractCollection<Declaration> varList)
Description copied from class: Expression
Add all declarations referenced in this expression to the collection.

Specified by:
getDeclList in class Expression