Makefile fixup
[org.ibex.tool.git] / src / org / eclipse / jdt / internal / compiler / ast / MessageSend.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  *     Nick Teryaev - fix for bug (https://bugs.eclipse.org/bugs/show_bug.cgi?id=40752)
11  *******************************************************************************/
12 package org.eclipse.jdt.internal.compiler.ast;
13
14 import org.eclipse.jdt.internal.compiler.ASTVisitor;
15 import org.eclipse.jdt.internal.compiler.flow.*;
16 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
17 import org.eclipse.jdt.internal.compiler.codegen.*;
18 import org.eclipse.jdt.internal.compiler.lookup.*;
19
20 public class MessageSend extends Expression implements InvocationSite {
21         public Expression receiver ;
22         public char[] selector ;
23         public Expression[] arguments ;
24         public MethodBinding binding, codegenBinding;
25
26         public long nameSourcePosition ; //(start<<32)+end
27
28         MethodBinding syntheticAccessor;
29
30         public TypeBinding receiverType, qualifyingType;
31         
32 public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {
33
34         flowInfo = receiver.analyseCode(currentScope, flowContext, flowInfo, !binding.isStatic()).unconditionalInits();
35         if (arguments != null) {
36                 int length = arguments.length;
37                 for (int i = 0; i < length; i++) {
38                         flowInfo = arguments[i].analyseCode(currentScope, flowContext, flowInfo).unconditionalInits();
39                 }
40         }
41         ReferenceBinding[] thrownExceptions;
42         if ((thrownExceptions = binding.thrownExceptions) != NoExceptions) {
43                 // must verify that exceptions potentially thrown by this expression are caught in the method
44                 flowContext.checkExceptionHandlers(thrownExceptions, this, flowInfo, currentScope);
45         }
46         manageSyntheticAccessIfNecessary(currentScope, flowInfo);       
47         return flowInfo;
48 }
49
50 /**
51  * MessageSend code generation
52  *
53  * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
54  * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
55  * @param valueRequired boolean
56  */ 
57 public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired) {
58
59         int pc = codeStream.position;
60
61         // generate receiver/enclosing instance access
62         boolean isStatic = codegenBinding.isStatic();
63         // outer access ?
64         if (!isStatic && ((bits & DepthMASK) != 0) && receiver.isImplicitThis()){
65                 // outer method can be reached through emulation if implicit access
66                 ReferenceBinding targetType = currentScope.enclosingSourceType().enclosingTypeAt((bits & DepthMASK) >> DepthSHIFT);             
67                 Object[] path = currentScope.getEmulationPath(targetType, true /*only exact match*/, false/*consider enclosing arg*/);
68                 codeStream.generateOuterAccess(path, this, targetType, currentScope);
69         } else {
70                 receiver.generateCode(currentScope, codeStream, !isStatic);
71         }
72         // generate arguments
73         if (arguments != null){
74                 for (int i = 0, max = arguments.length; i < max; i++){
75                         arguments[i].generateCode(currentScope, codeStream, true);
76                 }
77         }
78         // actual message invocation
79         if (syntheticAccessor == null){
80                 if (isStatic){
81                         codeStream.invokestatic(codegenBinding);
82                 } else {
83                         if( (receiver.isSuper()) || codegenBinding.isPrivate()){
84                                 codeStream.invokespecial(codegenBinding);
85                         } else {
86                                 if (codegenBinding.declaringClass.isInterface()){
87                                         codeStream.invokeinterface(codegenBinding);
88                                 } else {
89                                         codeStream.invokevirtual(codegenBinding);
90                                 }
91                         }
92                 }
93         } else {
94                 codeStream.invokestatic(syntheticAccessor);
95         }
96         // operation on the returned value
97         if (valueRequired){
98                 // implicit conversion if necessary
99                 codeStream.generateImplicitConversion(implicitConversion);
100         } else {
101                 // pop return value if any
102                 switch(binding.returnType.id){
103                         case T_long :
104                         case T_double :
105                                 codeStream.pop2();
106                                 break;
107                         case T_void :
108                                 break;
109                         default:
110                                 codeStream.pop();
111                 }
112         }
113         codeStream.recordPositionsFrom(pc, (int)(this.nameSourcePosition >>> 32)); // highlight selector
114 }
115 public boolean isSuperAccess() {        
116         return receiver.isSuper();
117 }
118 public boolean isTypeAccess() { 
119         return receiver != null && receiver.isTypeReference();
120 }
121 public void manageSyntheticAccessIfNecessary(BlockScope currentScope, FlowInfo flowInfo){
122
123         if (!flowInfo.isReachable()) return;
124         if (binding.isPrivate()){
125
126                 // depth is set for both implicit and explicit access (see MethodBinding#canBeSeenBy)           
127                 if (currentScope.enclosingSourceType() != binding.declaringClass){
128                 
129                         syntheticAccessor = ((SourceTypeBinding)binding.declaringClass).addSyntheticMethod(binding, isSuperAccess());
130                         currentScope.problemReporter().needToEmulateMethodAccess(binding, this);
131                         return;
132                 }
133
134         } else if (receiver instanceof QualifiedSuperReference){ // qualified super
135
136                 // qualified super need emulation always
137                 SourceTypeBinding destinationType = (SourceTypeBinding)(((QualifiedSuperReference)receiver).currentCompatibleType);
138                 syntheticAccessor = destinationType.addSyntheticMethod(binding, isSuperAccess());
139                 currentScope.problemReporter().needToEmulateMethodAccess(binding, this);
140                 return;
141
142         } else if (binding.isProtected()){
143
144                 SourceTypeBinding enclosingSourceType;
145                 if (((bits & DepthMASK) != 0) 
146                                 && binding.declaringClass.getPackage() 
147                                         != (enclosingSourceType = currentScope.enclosingSourceType()).getPackage()){
148
149                         SourceTypeBinding currentCompatibleType = (SourceTypeBinding)enclosingSourceType.enclosingTypeAt((bits & DepthMASK) >> DepthSHIFT);
150                         syntheticAccessor = currentCompatibleType.addSyntheticMethod(binding, isSuperAccess());
151                         currentScope.problemReporter().needToEmulateMethodAccess(binding, this);
152                         return;
153                 }
154         }
155         // if the binding declaring class is not visible, need special action
156         // for runtime compatibility on 1.2 VMs : change the declaring class of the binding
157         // NOTE: from target 1.2 on, method's declaring class is touched if any different from receiver type
158         // and not from Object or implicit static method call.  
159         if (binding.declaringClass != this.qualifyingType
160                 && !this.qualifyingType.isArrayType()
161                 && ((currentScope.environment().options.targetJDK >= ClassFileConstants.JDK1_2
162                                 && (!receiver.isImplicitThis() || !binding.isStatic())
163                                 && binding.declaringClass.id != T_Object) // no change for Object methods
164                         || !binding.declaringClass.canBeSeenBy(currentScope))) {
165
166                 this.codegenBinding = currentScope.enclosingSourceType().getUpdatedMethodBinding(binding, (ReferenceBinding) this.qualifyingType);
167
168                 // Post 1.4.0 target, array clone() invocations are qualified with array type 
169                 // This is handled in array type #clone method binding resolution (see Scope and UpdatedMethodBinding)
170         }
171 }
172
173 public StringBuffer printExpression(int indent, StringBuffer output){
174         
175         if (!receiver.isImplicitThis()) receiver.printExpression(0, output).append('.');
176         output.append(selector).append('(') ; //$NON-NLS-1$
177         if (arguments != null) {
178                 for (int i = 0; i < arguments.length ; i ++) {  
179                         if (i > 0) output.append(", "); //$NON-NLS-1$
180                         arguments[i].printExpression(0, output);
181                 }
182         }
183         return output.append(')');
184 }
185
186 public TypeBinding resolveType(BlockScope scope) {
187         // Answer the signature return type
188         // Base type promotion
189
190         constant = NotAConstant;
191         boolean receiverCast = false, argumentsCast = false; 
192         if (this.receiver instanceof CastExpression) {
193                 this.receiver.bits |= IgnoreNeedForCastCheckMASK; // will check later on
194                 receiverCast = true;
195         }
196         this.qualifyingType = this.receiverType = receiver.resolveType(scope); 
197         if (receiverCast && this.receiverType != null) {
198                  // due to change of declaring class with receiver type, only identity cast should be notified
199                 if (((CastExpression)this.receiver).expression.resolvedType == this.receiverType) { 
200                                         scope.problemReporter().unnecessaryCast((CastExpression)this.receiver);         
201                 }
202         }
203         // will check for null after args are resolved
204         TypeBinding[] argumentTypes = NoParameters;
205         if (arguments != null) {
206                 boolean argHasError = false; // typeChecks all arguments 
207                 int length = arguments.length;
208                 argumentTypes = new TypeBinding[length];
209                 for (int i = 0; i < length; i++){
210                         Expression argument = arguments[i];
211                         if (argument instanceof CastExpression) {
212                                 argument.bits |= IgnoreNeedForCastCheckMASK; // will check later on
213                                 argumentsCast = true;
214                         }
215                         if ((argumentTypes[i] = argument.resolveType(scope)) == null){
216                                 argHasError = true;
217                         }
218                 }
219                 if (argHasError) {
220                         if(receiverType instanceof ReferenceBinding) {
221                                 // record any selector match, for clients who may still need hint about possible method match
222                                 this.codegenBinding = this.binding = scope.findMethod((ReferenceBinding)receiverType, selector, new TypeBinding[]{}, this);
223                         }                       
224                         return null;
225                 }
226         }
227         if (this.receiverType == null)
228                 return null;
229
230         // base type cannot receive any message
231         if (this.receiverType.isBaseType()) {
232                 scope.problemReporter().errorNoMethodFor(this, this.receiverType, argumentTypes);
233                 return null;
234         }
235         this.codegenBinding = this.binding = 
236                 receiver.isImplicitThis()
237                         ? scope.getImplicitMethod(selector, argumentTypes, this)
238                         : scope.getMethod(this.receiverType, selector, argumentTypes, this); 
239         if (!binding.isValidBinding()) {
240                 if (binding.declaringClass == null) {
241                         if (this.receiverType instanceof ReferenceBinding) {
242                                 binding.declaringClass = (ReferenceBinding) this.receiverType;
243                         } else { 
244                                 scope.problemReporter().errorNoMethodFor(this, this.receiverType, argumentTypes);
245                                 return null;
246                         }
247                 }
248                 scope.problemReporter().invalidMethod(this, binding);
249                 // record the closest match, for clients who may still need hint about possible method match
250                 if (binding instanceof ProblemMethodBinding){
251                         MethodBinding closestMatch = ((ProblemMethodBinding)binding).closestMatch;
252                         if (closestMatch != null) this.codegenBinding = this.binding = closestMatch;
253                 }
254                 return this.resolvedType = this.binding == null ? null : this.binding.returnType;
255         }
256         if (!binding.isStatic()) {
257                 // the "receiver" must not be a type, in other words, a NameReference that the TC has bound to a Type
258                 if (receiver instanceof NameReference 
259                                 && (((NameReference) receiver).bits & BindingIds.TYPE) != 0) {
260                         scope.problemReporter().mustUseAStaticMethod(this, binding);
261                 }
262         } else {
263                 // static message invoked through receiver? legal but unoptimal (optional warning).
264                 if (!(receiver.isImplicitThis()
265                                 || receiver.isSuper()
266                                 || (receiver instanceof NameReference 
267                                         && (((NameReference) receiver).bits & BindingIds.TYPE) != 0))) {
268                         scope.problemReporter().nonStaticAccessToStaticMethod(this, binding);
269                 }
270                 if (!receiver.isImplicitThis() && binding.declaringClass != receiverType) {
271                         scope.problemReporter().indirectAccessToStaticMethod(this, binding);
272                 }               
273         }
274         if (arguments != null) {
275                 for (int i = 0; i < arguments.length; i++) {
276                         arguments[i].implicitWidening(binding.parameters[i], argumentTypes[i]);
277                 }
278                 if (argumentsCast) {
279                         CastExpression.checkNeedForArgumentCasts(scope, this.receiver, receiverType, binding, this.arguments, argumentTypes, this);
280                 }
281         }
282         //-------message send that are known to fail at compile time-----------
283         if (binding.isAbstract()) {
284                 if (receiver.isSuper()) {
285                         scope.problemReporter().cannotDireclyInvokeAbstractMethod(this, binding);
286                 }
287                 // abstract private methods cannot occur nor abstract static............
288         }
289         if (isMethodUseDeprecated(binding, scope))
290                 scope.problemReporter().deprecatedMethod(binding, this);
291
292         return this.resolvedType = binding.returnType;
293 }
294 public void setActualReceiverType(ReferenceBinding receiverType) {
295         this.qualifyingType = receiverType;
296 }
297 public void setDepth(int depth) {
298         bits &= ~DepthMASK; // flush previous depth if any
299         if (depth > 0) {
300                 bits |= (depth & 0xFF) << DepthSHIFT; // encoded on 8 bits
301         }
302 }
303 public void setFieldIndex(int depth) {
304         // ignore for here
305 }
306
307 public void traverse(ASTVisitor visitor, BlockScope blockScope) {
308         if (visitor.visit(this, blockScope)) {
309                 receiver.traverse(visitor, blockScope);
310                 if (arguments != null) {
311                         int argumentsLength = arguments.length;
312                         for (int i = 0; i < argumentsLength; i++)
313                                 arguments[i].traverse(visitor, blockScope);
314                 }
315         }
316         visitor.endVisit(this, blockScope);
317 }
318 }