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.abstraction.KeYJavaType;
015: import de.uka.ilkd.key.java.visitor.Visitor;
016: import de.uka.ilkd.key.logic.ProgramElementName;
017: import de.uka.ilkd.key.logic.op.ProgramVariable;
018: import de.uka.ilkd.key.util.ExtList;
019:
020: /**
021: * Variable reference.
022: * @author <TT>AutoDoc</TT>
023: */
024: public class VariableReference extends JavaNonTerminalProgramElement
025: implements NameReference, Expression, ReferencePrefix {
026:
027: protected ProgramVariable variable;
028:
029: protected VariableReference() {
030: }
031:
032: public VariableReference(ExtList children) {
033: super (children);
034: variable = (ProgramVariable) children
035: .get(ProgramVariable.class);
036: }
037:
038: public VariableReference(ProgramVariable variable, PositionInfo pi) {
039: super (pi);
040: this .variable = variable;
041: }
042:
043: public VariableReference(ProgramVariable variable) {
044: this (variable, PositionInfo.UNDEFINED);
045: }
046:
047: public ProgramElementName getProgramElementName() {
048: return (ProgramElementName) variable.name();
049: }
050:
051: public int getChildCount() {
052: return (variable != null) ? 1 : 0;
053: }
054:
055: /**
056: * Returns the child at the specified index in this node's "virtual"
057: * child array
058: * @param index an index into this node's "virtual" child array
059: * @return the program element at the given position
060: * @exception ArrayIndexOutOfBoundsException if <tt>index</tt> is out
061: * of bounds
062: */
063:
064: public ProgramElement getChildAt(int index) {
065: if (variable != null) {
066: if (index == 0)
067: return variable;
068: }
069: throw new ArrayIndexOutOfBoundsException();
070: }
071:
072: public ProgramElementName getIdentifier() {
073: return (ProgramElementName) variable.name();
074: }
075:
076: /**
077: * Get name.
078: * @return the string.
079: */
080:
081: public final String getName() {
082: return (variable == null) ? null : variable.toString();
083: }
084:
085: public ProgramVariable getProgramVariable() {
086: return variable;
087: }
088:
089: public SourceElement getFirstElement() {
090: return variable;
091: }
092:
093: public void prettyPrint(PrettyPrinter p) throws java.io.IOException {
094: variable.prettyPrint(p);
095: }
096:
097: /**
098: * calls the corresponding method of a visitor in order to
099: * perform some action/transformation on this element
100: * @param v the Visitor
101: */
102: public void visit(Visitor v) {
103: v.performActionOnVariableReference(this );
104: }
105:
106: /**
107: * We do not have a prefix, so fake it!
108: * This way we implement ReferencePrefix
109: * @author VK
110: */
111: public ReferencePrefix getReferencePrefix() {
112: return null;
113: }
114:
115: public ReferencePrefix setReferencePrefix(ReferencePrefix r) {
116: return this ;
117: }
118:
119: public KeYJavaType getKeYJavaType(Services javaServ,
120: ExecutionContext ec) {
121: return getKeYJavaType();
122: }
123:
124: public KeYJavaType getKeYJavaType(Services javaServ) {
125: return getKeYJavaType();
126: }
127:
128: public KeYJavaType getKeYJavaType() {
129: return variable != null ? variable.getKeYJavaType() : null;
130: }
131:
132: }
|