1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.compiler.ast;
12 
13 import org.eclipse.jdt.internal.compiler.ASTVisitor;
14 import org.eclipse.jdt.internal.compiler.codegen.*;
15 import org.eclipse.jdt.internal.compiler.flow.FlowContext;
16 import org.eclipse.jdt.internal.compiler.flow.FlowInfo;
17 import org.eclipse.jdt.internal.compiler.lookup.*;
18 
19 public class ThisReference extends Reference {
20 
implicitThis()21 	public static ThisReference implicitThis(){
22 
23 		ThisReference implicitThis = new ThisReference(0, 0);
24 		implicitThis.bits |= IsImplicitThisMask;
25 		return implicitThis;
26 	}
27 
ThisReference(int sourceStart, int sourceEnd)28 	public ThisReference(int sourceStart, int sourceEnd) {
29 
30 		this.sourceStart = sourceStart;
31 		this.sourceEnd = sourceEnd;
32 	}
33 
34 	/*
35 	 * @see Reference#analyseAssignment(...)
36 	 */
analyseAssignment(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo, Assignment assignment, boolean isCompound)37 	public FlowInfo analyseAssignment(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo, Assignment assignment, boolean isCompound) {
38 
39 		return flowInfo; // this cannot be assigned
40 	}
41 
checkAccess(MethodScope methodScope)42 	public boolean checkAccess(MethodScope methodScope) {
43 
44 		// this/super cannot be used in constructor call
45 		if (methodScope.isConstructorCall) {
46 			methodScope.problemReporter().fieldsOrThisBeforeConstructorInvocation(this);
47 			return false;
48 		}
49 
50 		// static may not refer to this/super
51 		if (methodScope.isStatic) {
52 			methodScope.problemReporter().errorThisSuperInStatic(this);
53 			return false;
54 		}
55 		return true;
56 	}
57 
58 	/*
59 	 * @see Reference#generateAssignment(...)
60 	 */
generateAssignment(BlockScope currentScope, CodeStream codeStream, Assignment assignment, boolean valueRequired)61 	public void generateAssignment(BlockScope currentScope, CodeStream codeStream, Assignment assignment, boolean valueRequired) {
62 
63 		 // this cannot be assigned
64 	}
65 
generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired)66 	public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired) {
67 
68 		int pc = codeStream.position;
69 		if (valueRequired)
70 			codeStream.aload_0();
71 		if ((this.bits & IsImplicitThisMask) == 0) codeStream.recordPositionsFrom(pc, this.sourceStart);
72 	}
73 
74 	/*
75 	 * @see Reference#generateCompoundAssignment(...)
76 	 */
generateCompoundAssignment(BlockScope currentScope, CodeStream codeStream, Expression expression, int operator, int assignmentImplicitConversion, boolean valueRequired)77 	public void generateCompoundAssignment(BlockScope currentScope, CodeStream codeStream, Expression expression, int operator, int assignmentImplicitConversion,  boolean valueRequired) {
78 
79 		 // this cannot be assigned
80 	}
81 
82 	/*
83 	 * @see org.eclipse.jdt.internal.compiler.ast.Reference#generatePostIncrement()
84 	 */
generatePostIncrement(BlockScope currentScope, CodeStream codeStream, CompoundAssignment postIncrement, boolean valueRequired)85 	public void generatePostIncrement(BlockScope currentScope, CodeStream codeStream, CompoundAssignment postIncrement, boolean valueRequired) {
86 
87 		 // this cannot be assigned
88 	}
89 
isImplicitThis()90 	public boolean isImplicitThis() {
91 
92 		return (this.bits & IsImplicitThisMask) != 0;
93 	}
94 
isThis()95 	public boolean isThis() {
96 
97 		return true ;
98 	}
99 
printExpression(int indent, StringBuffer output)100 	public StringBuffer printExpression(int indent, StringBuffer output){
101 
102 		if (this.isImplicitThis()) return output;
103 		return output.append("this"); //$NON-NLS-1$
104 	}
105 
resolveType(BlockScope scope)106 	public TypeBinding resolveType(BlockScope scope) {
107 
108 		constant = NotAConstant;
109 		if (!this.isImplicitThis() &&!checkAccess(scope.methodScope())) {
110 			return null;
111 		}
112 		return this.resolvedType = scope.enclosingSourceType();
113 	}
114 
traverse(ASTVisitor visitor, BlockScope blockScope)115 	public void traverse(ASTVisitor visitor, BlockScope blockScope) {
116 
117 		visitor.visit(this, blockScope);
118 		visitor.endVisit(this, blockScope);
119 	}
120 }
121