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