added -J option to preserve unmodified files in preexisting jarfile
[org.ibex.tool.git] / src / org / eclipse / jdt / internal / compiler / ast / QualifiedThisReference.java
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.*;
16 import org.eclipse.jdt.internal.compiler.lookup.*;
17
18 public class QualifiedThisReference extends ThisReference {
19         
20         public TypeReference qualification;
21         ReferenceBinding currentCompatibleType;
22         
23         public QualifiedThisReference(TypeReference name, int sourceStart, int sourceEnd) {
24                 super(sourceStart, sourceEnd);
25                 qualification = name;
26                 this.sourceStart = name.sourceStart;
27         }
28
29         public FlowInfo analyseCode(
30                 BlockScope currentScope,
31                 FlowContext flowContext,
32                 FlowInfo flowInfo) {
33
34                 return flowInfo;
35         }
36
37         public FlowInfo analyseCode(
38                 BlockScope currentScope,
39                 FlowContext flowContext,
40                 FlowInfo flowInfo,
41                 boolean valueRequired) {
42
43                 return flowInfo;
44         }
45
46         /**
47          * Code generation for QualifiedThisReference
48          *
49          * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
50          * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
51          * @param valueRequired boolean
52          */
53         public void generateCode(
54                 BlockScope currentScope,
55                 CodeStream codeStream,
56                 boolean valueRequired) {
57
58                 int pc = codeStream.position;
59                 if (valueRequired) {
60                         if ((bits & DepthMASK) != 0) {
61                                 Object[] emulationPath =
62                                         currentScope.getEmulationPath(this.currentCompatibleType, true /*only exact match*/, false/*consider enclosing arg*/);
63                                 codeStream.generateOuterAccess(emulationPath, this, this.currentCompatibleType, currentScope);
64                         } else {
65                                 // nothing particular after all
66                                 codeStream.aload_0();
67                         }
68                 }
69                 codeStream.recordPositionsFrom(pc, this.sourceStart);
70         }
71
72         public TypeBinding resolveType(BlockScope scope) {
73
74                 constant = NotAConstant;
75                 TypeBinding type = this.resolvedType = this.qualification.resolveType(scope, true /* check bounds*/);
76                 if (type == null) return null;
77                 // X.this is not a raw type as denoting enclosing instance
78                 if (type.isRawType()) {
79                     RawTypeBinding rawType = (RawTypeBinding) type;
80                     type = this.resolvedType = rawType.type; // unwrap
81                 }
82                 // the qualification MUST exactly match some enclosing type name
83                 // Its possible to qualify 'this' by the name of the current class
84                 int depth = 0;
85                 this.currentCompatibleType = scope.referenceType().binding;
86                 while (this.currentCompatibleType != null
87                         && this.currentCompatibleType != type) {
88                         depth++;
89                         this.currentCompatibleType = this.currentCompatibleType.isStatic() ? null : this.currentCompatibleType.enclosingType();
90                 }
91                 bits &= ~DepthMASK; // flush previous depth if any                      
92                 bits |= (depth & 0xFF) << DepthSHIFT; // encoded depth into 8 bits
93
94                 if (this.currentCompatibleType == null) {
95                         scope.problemReporter().noSuchEnclosingInstance(type, this, false);
96                         return type;
97                 }
98
99                 // Ensure one cannot write code like: B() { super(B.this); }
100                 if (depth == 0) {
101                         checkAccess(scope.methodScope());
102                 } // if depth>0, path emulation will diagnose bad scenarii
103                 return type;
104         }
105
106         public StringBuffer printExpression(int indent, StringBuffer output) {
107
108                 return qualification.print(0, output).append(".this"); //$NON-NLS-1$
109         }
110
111         public void traverse(
112                 ASTVisitor visitor,
113                 BlockScope blockScope) {
114
115                 if (visitor.visit(this, blockScope)) {
116                         qualification.traverse(visitor, blockScope);
117                 }
118                 visitor.endVisit(this, blockScope);
119         }
120 }