KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > ast > TypeReference


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-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.flow.FlowContext;
15 import org.eclipse.jdt.internal.compiler.flow.FlowInfo;
16 import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
17 import org.eclipse.jdt.internal.compiler.impl.Constant;
18 import org.eclipse.jdt.internal.compiler.lookup.ArrayBinding;
19 import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
20 import org.eclipse.jdt.internal.compiler.lookup.ClassScope;
21 import org.eclipse.jdt.internal.compiler.lookup.ProblemReasons;
22 import org.eclipse.jdt.internal.compiler.lookup.ProblemReferenceBinding;
23 import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
24 import org.eclipse.jdt.internal.compiler.lookup.Scope;
25 import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
26 import org.eclipse.jdt.internal.compiler.lookup.TypeIds;
27 import org.eclipse.jdt.internal.compiler.problem.ProblemSeverities;
28
29 public abstract class TypeReference extends Expression {
30
31 public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {
32     return flowInfo;
33 }
34
35 // allows us to trap completion & selection nodes
36
public void aboutToResolve(Scope scope) {
37     // default implementation: do nothing
38
}
39 /*
40  * Answer a base type reference (can be an array of base type).
41  */

42 public static final TypeReference baseTypeReference(int baseType, int dim) {
43     
44     if (dim == 0) {
45         switch (baseType) {
46             case (TypeIds.T_void) :
47                 return new SingleTypeReference(TypeBinding.VOID.simpleName, 0);
48             case (TypeIds.T_boolean) :
49                 return new SingleTypeReference(TypeBinding.BOOLEAN.simpleName, 0);
50             case (TypeIds.T_char) :
51                 return new SingleTypeReference(TypeBinding.CHAR.simpleName, 0);
52             case (TypeIds.T_float) :
53                 return new SingleTypeReference(TypeBinding.FLOAT.simpleName, 0);
54             case (TypeIds.T_double) :
55                 return new SingleTypeReference(TypeBinding.DOUBLE.simpleName, 0);
56             case (TypeIds.T_byte) :
57                 return new SingleTypeReference(TypeBinding.BYTE.simpleName, 0);
58             case (TypeIds.T_short) :
59                 return new SingleTypeReference(TypeBinding.SHORT.simpleName, 0);
60             case (TypeIds.T_int) :
61                 return new SingleTypeReference(TypeBinding.INT.simpleName, 0);
62             default : //T_long
63
return new SingleTypeReference(TypeBinding.LONG.simpleName, 0);
64         }
65     }
66     switch (baseType) {
67         case (TypeIds.T_void) :
68             return new ArrayTypeReference(TypeBinding.VOID.simpleName, dim, 0);
69         case (TypeIds.T_boolean) :
70             return new ArrayTypeReference(TypeBinding.BOOLEAN.simpleName, dim, 0);
71         case (TypeIds.T_char) :
72             return new ArrayTypeReference(TypeBinding.CHAR.simpleName, dim, 0);
73         case (TypeIds.T_float) :
74             return new ArrayTypeReference(TypeBinding.FLOAT.simpleName, dim, 0);
75         case (TypeIds.T_double) :
76             return new ArrayTypeReference(TypeBinding.DOUBLE.simpleName, dim, 0);
77         case (TypeIds.T_byte) :
78             return new ArrayTypeReference(TypeBinding.BYTE.simpleName, dim, 0);
79         case (TypeIds.T_short) :
80             return new ArrayTypeReference(TypeBinding.SHORT.simpleName, dim, 0);
81         case (TypeIds.T_int) :
82             return new ArrayTypeReference(TypeBinding.INT.simpleName, dim, 0);
83         default : //T_long
84
return new ArrayTypeReference(TypeBinding.LONG.simpleName, dim, 0);
85     }
86 }
87 public void checkBounds(Scope scope) {
88     // only parameterized type references have bounds
89
}
90 public abstract TypeReference copyDims(int dim);
91 public int dimensions() {
92     return 0;
93 }
94
95 public abstract char[] getLastToken();
96
97 /**
98  * @return char[][]
99  * TODO (jerome) should merge back into #getTypeName()
100  */

101 public char [][] getParameterizedTypeName(){
102     return getTypeName();
103 }
104 protected abstract TypeBinding getTypeBinding(Scope scope);
105 /**
106  * @return char[][]
107  */

108 public abstract char [][] getTypeName() ;
109 public boolean isTypeReference() {
110     return true;
111 }
112 public TypeBinding resolveSuperType(ClassScope scope) {
113     // assumes the implementation of resolveType(ClassScope) will call back to detect cycles
114
if (resolveType(scope) == null) return null;
115
116     if (this.resolvedType.isTypeVariable()) {
117         this.resolvedType = new ProblemReferenceBinding(getTypeName(), (ReferenceBinding) this.resolvedType, ProblemReasons.IllegalSuperTypeVariable);
118         reportInvalidType(scope);
119         return null;
120     }
121     return this.resolvedType;
122 }
123
124 public final TypeBinding resolveType(BlockScope blockScope) {
125     return resolveType(blockScope, true /* checkbounds if any */);
126 }
127
128 public TypeBinding resolveType(BlockScope scope, boolean checkBounds) {
129     // handle the error here
130
this.constant = Constant.NotAConstant;
131     if (this.resolvedType != null) // is a shared type reference which was already resolved
132
return this.resolvedType.isValidBinding() ? this.resolvedType : null; // already reported error
133

134     TypeBinding type = this.resolvedType = getTypeBinding(scope);
135     if (type == null)
136         return null; // detected cycle while resolving hierarchy
137
if (!type.isValidBinding()) {
138         reportInvalidType(scope);
139         return null;
140     }
141     if (type.isArrayType() && ((ArrayBinding) type).leafComponentType == TypeBinding.VOID) {
142         scope.problemReporter().cannotAllocateVoidArray(this);
143         return null;
144     }
145
146     if (isTypeUseDeprecated(type, scope))
147         reportDeprecatedType(type, scope);
148     
149     type = scope.environment().convertToRawType(type);
150     if (type.leafComponentType().isRawType()
151             && (this.bits & ASTNode.IgnoreRawTypeCheck) == 0
152             && scope.compilerOptions().getSeverity(CompilerOptions.RawTypeReference) != ProblemSeverities.Ignore) {
153         scope.problemReporter().rawTypeReference(this, type);
154     }
155     return this.resolvedType = type;
156 }
157 public TypeBinding resolveType(ClassScope scope) {
158     // handle the error here
159
this.constant = Constant.NotAConstant;
160     if (this.resolvedType != null) // is a shared type reference which was already resolved
161
return this.resolvedType.isValidBinding() ? this.resolvedType : null; // already reported error
162

163     TypeBinding type = this.resolvedType = getTypeBinding(scope);
164     if (type == null)
165         return null; // detected cycle while resolving hierarchy
166
if (!type.isValidBinding()) {
167         reportInvalidType(scope);
168         return null;
169     }
170     if (type.isArrayType() && ((ArrayBinding) type).leafComponentType == TypeBinding.VOID) {
171         scope.problemReporter().cannotAllocateVoidArray(this);
172         return null;
173     }
174     if (isTypeUseDeprecated(type, scope))
175         reportDeprecatedType(type, scope);
176     
177     type = scope.environment().convertToRawType(type);
178     if (type.leafComponentType().isRawType()
179             && (this.bits & ASTNode.IgnoreRawTypeCheck) == 0
180             && scope.compilerOptions().getSeverity(CompilerOptions.RawTypeReference) != ProblemSeverities.Ignore) {
181         scope.problemReporter().rawTypeReference(this, type);
182     }
183     return this.resolvedType = type;
184 }
185
186 public TypeBinding resolveTypeArgument(BlockScope blockScope, ReferenceBinding genericType, int rank) {
187     return resolveType(blockScope, true /* check bounds*/);
188 }
189
190 public TypeBinding resolveTypeArgument(ClassScope classScope, ReferenceBinding genericType, int rank) {
191     return resolveType(classScope);
192 }
193     
194 protected void reportInvalidType(Scope scope) {
195     scope.problemReporter().invalidType(this, this.resolvedType);
196 }
197 protected void reportDeprecatedType(TypeBinding type, Scope scope) {
198     scope.problemReporter().deprecatedType(type, this);
199 }
200 public abstract void traverse(ASTVisitor visitor, BlockScope scope);
201 public abstract void traverse(ASTVisitor visitor, ClassScope scope);
202 }
203
Popular Tags