001: // This file is part of KeY - Integrated Deductive Software Design
002: // Copyright (C) 2001-2007 Universitaet Karlsruhe, Germany
003: // Universitaet Koblenz-Landau, Germany
004: // Chalmers University of Technology, Sweden
005: //
006: // The KeY system is protected by the GNU General Public License.
007: // See LICENSE.TXT for details.
008: //
009: //
010:
011: package de.uka.ilkd.key.java.reference;
012:
013: import de.uka.ilkd.key.java.*;
014: import de.uka.ilkd.key.java.visitor.Visitor;
015: import de.uka.ilkd.key.logic.op.ProgramVariable;
016: import de.uka.ilkd.key.util.ExtList;
017:
018: /**
019: * Field reference.
020: * @author <TT>AutoDoc</TT>
021: */
022: public class FieldReference extends VariableReference implements
023: MemberReference, ReferenceSuffix, TypeReferenceContainer,
024: ExpressionContainer {
025:
026: /**
027: * Reference prefix.
028: */
029: protected ReferencePrefix prefix;
030:
031: protected FieldReference() {
032: }
033:
034: public FieldReference(ProgramVariable pv, ReferencePrefix prefix) {
035: super (pv);
036: this .prefix = prefix;
037: }
038:
039: public FieldReference(ExtList children, ReferencePrefix prefix) {
040: super (children);
041: this .prefix = prefix;
042: }
043:
044: public FieldReference(ProgramVariable pv, ReferencePrefix prefix,
045: PositionInfo pi) {
046: super (pv, pi);
047: this .prefix = prefix;
048:
049: }
050:
051: /**
052: * Returns the number of children of this node.
053: * @return an int giving the number of children of this node
054: */
055: public int getChildCount() {
056: int result = 0;
057: if (prefix != null)
058: result++;
059: if (variable != null)
060: result++;
061: return result;
062: }
063:
064: /**
065: * Returns the child at the specified index in this node's "virtual"
066: * child array
067: * @param index an index into this node's "virtual" child array
068: * @return the program element at the given position
069: * @exception ArrayIndexOutOfBoundsException if <tt>index</tt> is out
070: * of bounds
071: */
072: public ProgramElement getChildAt(int index) {
073: if (prefix != null) {
074: if (index == 0)
075: return prefix;
076: index--;
077: }
078: if (variable != null) {
079: if (index == 0)
080: return variable;
081: }
082: throw new ArrayIndexOutOfBoundsException();
083: }
084:
085: /**
086: * Get reference prefix.
087: * @return the reference prefix.
088: */
089: public ReferencePrefix getReferencePrefix() {
090: return prefix;
091: }
092:
093: /*
094: * returns true if the reference prefix is an explicit or implicit
095: * this reference this field reference does not refer to a static field
096: */
097: public boolean referencesOwnInstanceField() {
098: return (prefix == null || prefix instanceof ThisReference)
099: && !getProgramVariable().isStatic();
100: }
101:
102: /**
103: * Set reference prefix.
104: * @author VK
105: */
106: public ReferencePrefix setReferencePrefix(ReferencePrefix rp) {
107: return new FieldReference(variable, rp);
108: }
109:
110: /**
111: * Get the number of type references in this container.
112: * @return the number of type references.
113: */
114:
115: public int getTypeReferenceCount() {
116: return (prefix instanceof TypeReference) ? 1 : 0;
117: }
118:
119: /*
120: Return the type reference at the specified index in this node's
121: "virtual" type reference array.
122: @param index an index for a type reference.
123: @return the type reference with the given index.
124: @exception ArrayIndexOutOfBoundsException if <tt>index</tt> is out
125: of bounds.
126: */
127: public TypeReference getTypeReferenceAt(int index) {
128: if (prefix instanceof TypeReference && index == 0) {
129: return (TypeReference) prefix;
130: }
131: throw new ArrayIndexOutOfBoundsException();
132: }
133:
134: /**
135: * Get the number of expressions in this container.
136: * @return the number of expressions.
137: */
138: public int getExpressionCount() {
139: return (prefix instanceof Expression) ? 1 : 0;
140: }
141:
142: /*
143: Return the expression at the specified index in this node's
144: "virtual" expression array.
145: @param index an index for an expression.
146: @return the expression with the given index.
147: @exception ArrayIndexOutOfBoundsException if <tt>index</tt> is out
148: of bounds.
149: */
150: public Expression getExpressionAt(int index) {
151: if (prefix instanceof Expression && index == 0) {
152: return (Expression) prefix;
153: }
154: throw new ArrayIndexOutOfBoundsException();
155: }
156:
157: public SourceElement getFirstElement() {
158: return (prefix == null) ? variable : prefix.getFirstElement();
159: }
160:
161: /** pretty print */
162: public void prettyPrint(PrettyPrinter p) throws java.io.IOException {
163: p.printFieldReference(this );
164: }
165:
166: /** calls the corresponding method of a visitor in order to
167: * perform some action/transformation on this element
168: * @param v the Visitor
169: */
170: public void visit(Visitor v) {
171: v.performActionOnFieldReference(this );
172: }
173:
174: /** are there "dots" in the prefix? */
175: public boolean isSingleDeref() {
176: return prefix.getReferencePrefix() == null;
177: }
178:
179: }
|