001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.compiler.ast;
011:
012: import org.eclipse.jdt.internal.compiler.ASTVisitor;
013: import org.eclipse.jdt.internal.compiler.codegen.*;
014: import org.eclipse.jdt.internal.compiler.flow.FlowContext;
015: import org.eclipse.jdt.internal.compiler.flow.FlowInfo;
016: import org.eclipse.jdt.internal.compiler.impl.Constant;
017: import org.eclipse.jdt.internal.compiler.lookup.*;
018:
019: public class ThisReference extends Reference {
020:
021: public static ThisReference implicitThis() {
022:
023: ThisReference implicitThis = new ThisReference(0, 0);
024: implicitThis.bits |= IsImplicitThis;
025: return implicitThis;
026: }
027:
028: public ThisReference(int sourceStart, int sourceEnd) {
029:
030: this .sourceStart = sourceStart;
031: this .sourceEnd = sourceEnd;
032: }
033:
034: /*
035: * @see Reference#analyseAssignment(...)
036: */
037: public FlowInfo analyseAssignment(BlockScope currentScope,
038: FlowContext flowContext, FlowInfo flowInfo,
039: Assignment assignment, boolean isCompound) {
040:
041: return flowInfo; // this cannot be assigned
042: }
043:
044: public boolean checkAccess(MethodScope methodScope) {
045:
046: // this/super cannot be used in constructor call
047: if (methodScope.isConstructorCall) {
048: methodScope.problemReporter()
049: .fieldsOrThisBeforeConstructorInvocation(this );
050: return false;
051: }
052:
053: // static may not refer to this/super
054: if (methodScope.isStatic) {
055: methodScope.problemReporter().errorThisSuperInStatic(this );
056: return false;
057: }
058: return true;
059: }
060:
061: /*
062: * @see Reference#generateAssignment(...)
063: */
064: public void generateAssignment(BlockScope currentScope,
065: CodeStream codeStream, Assignment assignment,
066: boolean valueRequired) {
067:
068: // this cannot be assigned
069: }
070:
071: public void generateCode(BlockScope currentScope,
072: CodeStream codeStream, boolean valueRequired) {
073:
074: int pc = codeStream.position;
075: if (valueRequired)
076: codeStream.aload_0();
077: if ((this .bits & IsImplicitThis) == 0)
078: codeStream.recordPositionsFrom(pc, this .sourceStart);
079: }
080:
081: /*
082: * @see Reference#generateCompoundAssignment(...)
083: */
084: public void generateCompoundAssignment(BlockScope currentScope,
085: CodeStream codeStream, Expression expression, int operator,
086: int assignmentImplicitConversion, boolean valueRequired) {
087:
088: // this cannot be assigned
089: }
090:
091: /*
092: * @see org.eclipse.jdt.internal.compiler.ast.Reference#generatePostIncrement()
093: */
094: public void generatePostIncrement(BlockScope currentScope,
095: CodeStream codeStream, CompoundAssignment postIncrement,
096: boolean valueRequired) {
097:
098: // this cannot be assigned
099: }
100:
101: public boolean isImplicitThis() {
102:
103: return (this .bits & IsImplicitThis) != 0;
104: }
105:
106: public boolean isThis() {
107:
108: return true;
109: }
110:
111: public int nullStatus(FlowInfo flowInfo) {
112: return FlowInfo.NON_NULL;
113: }
114:
115: public StringBuffer printExpression(int indent, StringBuffer output) {
116:
117: if (this .isImplicitThis())
118: return output;
119: return output.append("this"); //$NON-NLS-1$
120: }
121:
122: public TypeBinding resolveType(BlockScope scope) {
123:
124: constant = Constant.NotAConstant;
125: if (!this .isImplicitThis() && !checkAccess(scope.methodScope())) {
126: return null;
127: }
128: return this .resolvedType = scope.enclosingReceiverType();
129: }
130:
131: public void traverse(ASTVisitor visitor, BlockScope blockScope) {
132:
133: visitor.visit(this , blockScope);
134: visitor.endVisit(this , blockScope);
135: }
136:
137: public void traverse(ASTVisitor visitor, ClassScope blockScope) {
138:
139: visitor.visit(this, blockScope);
140: visitor.endVisit(this, blockScope);
141: }
142: }
|