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.flow.FlowContext;
15 import org.eclipse.jdt.internal.compiler.flow.FlowInfo;
16 import org.eclipse.jdt.internal.compiler.lookup.*;
17
18 public abstract class TypeReference extends Expression {
19
TypeReference()20 public TypeReference() {
21 super () ;
22 }
23
analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo)24 public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {
25 return flowInfo;
26 }
27
28 // allows us to trap completion & selection nodes
aboutToResolve(Scope scope)29 public void aboutToResolve(Scope scope) {
30 // default implementation: do nothing
31 }
32 /*
33 * Answer a base type reference (can be an array of base type).
34 */
baseTypeReference(int baseType, int dim)35 public static final TypeReference baseTypeReference(int baseType, int dim) {
36
37 if (dim == 0) {
38 switch (baseType) {
39 case (T_void) :
40 return new SingleTypeReference(VoidBinding.simpleName, 0);
41 case (T_boolean) :
42 return new SingleTypeReference(BooleanBinding.simpleName, 0);
43 case (T_char) :
44 return new SingleTypeReference(CharBinding.simpleName, 0);
45 case (T_float) :
46 return new SingleTypeReference(FloatBinding.simpleName, 0);
47 case (T_double) :
48 return new SingleTypeReference(DoubleBinding.simpleName, 0);
49 case (T_byte) :
50 return new SingleTypeReference(ByteBinding.simpleName, 0);
51 case (T_short) :
52 return new SingleTypeReference(ShortBinding.simpleName, 0);
53 case (T_int) :
54 return new SingleTypeReference(IntBinding.simpleName, 0);
55 default : //T_long
56 return new SingleTypeReference(LongBinding.simpleName, 0);
57 }
58 }
59 switch (baseType) {
60 case (T_void) :
61 return new ArrayTypeReference(VoidBinding.simpleName, dim, 0);
62 case (T_boolean) :
63 return new ArrayTypeReference(BooleanBinding.simpleName, dim, 0);
64 case (T_char) :
65 return new ArrayTypeReference(CharBinding.simpleName, dim, 0);
66 case (T_float) :
67 return new ArrayTypeReference(FloatBinding.simpleName, dim, 0);
68 case (T_double) :
69 return new ArrayTypeReference(DoubleBinding.simpleName, dim, 0);
70 case (T_byte) :
71 return new ArrayTypeReference(ByteBinding.simpleName, dim, 0);
72 case (T_short) :
73 return new ArrayTypeReference(ShortBinding.simpleName, dim, 0);
74 case (T_int) :
75 return new ArrayTypeReference(IntBinding.simpleName, dim, 0);
76 default : //T_long
77 return new ArrayTypeReference(LongBinding.simpleName, dim, 0);
78 }
79 }
checkBounds(Scope scope)80 public void checkBounds(Scope scope) {
81 // only parameterized type references have bounds
82 }
copyDims(int dim)83 public abstract TypeReference copyDims(int dim);
dimensions()84 public int dimensions() {
85 return 0;
86 }
87 /**
88 * @return char[][]
89 * TODO (jerome) should merge back into #getTypeName()
90 */
getParameterizedTypeName()91 public char [][] getParameterizedTypeName(){
92 return getTypeName();
93 }
getTypeBinding(Scope scope)94 protected abstract TypeBinding getTypeBinding(Scope scope);
95 /**
96 * @return char[][]
97 */
getTypeName()98 public abstract char [][] getTypeName() ;
isTypeReference()99 public boolean isTypeReference() {
100 return true;
101 }
resolveSuperType(ClassScope scope)102 public TypeBinding resolveSuperType(ClassScope scope) {
103 // assumes the implementation of resolveType(ClassScope) will call back to detect cycles
104 if (resolveType(scope) == null) return null;
105
106 if (this.resolvedType.isTypeVariable()) {
107 this.resolvedType = new ProblemReferenceBinding(getTypeName(), (ReferenceBinding) this.resolvedType, ProblemReasons.IllegalSuperTypeVariable);
108 reportInvalidType(scope);
109 return null;
110 }
111 return this.resolvedType;
112 }
resolveType(BlockScope blockScope)113 public TypeBinding resolveType(BlockScope blockScope) {
114 // handle the error here
115 this.constant = NotAConstant;
116 if (this.resolvedType != null) // is a shared type reference which was already resolved
117 return this.resolvedType.isValidBinding() ? this.resolvedType : null; // already reported error
118
119 this.resolvedType = getTypeBinding(blockScope);
120 if (this.resolvedType == null)
121 return null; // detected cycle while resolving hierarchy
122 if (!this.resolvedType.isValidBinding()) {
123 reportInvalidType(blockScope);
124 return null;
125 }
126 if (isTypeUseDeprecated(this.resolvedType, blockScope))
127 reportDeprecatedType(blockScope);
128 return this.resolvedType = blockScope.convertToRawType(this.resolvedType);
129 }
resolveType(ClassScope classScope)130 public TypeBinding resolveType(ClassScope classScope) {
131 // handle the error here
132 this.constant = NotAConstant;
133 if (this.resolvedType != null) // is a shared type reference which was already resolved
134 return this.resolvedType.isValidBinding() ? this.resolvedType : null; // already reported error
135
136 this.resolvedType = getTypeBinding(classScope);
137 if (this.resolvedType == null)
138 return null; // detected cycle while resolving hierarchy
139 if (!this.resolvedType.isValidBinding()) {
140 reportInvalidType(classScope);
141 return null;
142 }
143 if (isTypeUseDeprecated(this.resolvedType, classScope))
144 reportDeprecatedType(classScope);
145 return this.resolvedType = classScope.convertToRawType(this.resolvedType);
146 }
147
resolveTypeArgument(BlockScope blockScope, ReferenceBinding genericType, int rank)148 public TypeBinding resolveTypeArgument(BlockScope blockScope, ReferenceBinding genericType, int rank) {
149 return resolveType(blockScope);
150 }
151
resolveTypeArgument(ClassScope classScope, ReferenceBinding genericType, int rank)152 public TypeBinding resolveTypeArgument(ClassScope classScope, ReferenceBinding genericType, int rank) {
153 return resolveType(classScope);
154 }
155
reportInvalidType(Scope scope)156 protected void reportInvalidType(Scope scope) {
157 scope.problemReporter().invalidType(this, this.resolvedType);
158 }
reportDeprecatedType(Scope scope)159 protected void reportDeprecatedType(Scope scope) {
160 scope.problemReporter().deprecatedType(this.resolvedType, this);
161 }
traverse(ASTVisitor visitor, ClassScope classScope)162 public abstract void traverse(ASTVisitor visitor, ClassScope classScope);
traverse(ASTVisitor visitor, BlockScope classScope)163 public abstract void traverse(ASTVisitor visitor, BlockScope classScope);
164 }
165