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
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;
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.*;
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;
26 public long nameSourcePosition ; //(start<<32)+end
28 MethodBinding syntheticAccessor;
30 public TypeBinding receiverType, qualifyingType;
32 public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {
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();
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);
46 manageSyntheticAccessIfNecessary(currentScope, flowInfo);
51 * MessageSend code generation
53 * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
54 * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
55 * @param valueRequired boolean
57 public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired) {
59 int pc = codeStream.position;
61 // generate receiver/enclosing instance access
62 boolean isStatic = codegenBinding.isStatic();
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);
70 receiver.generateCode(currentScope, codeStream, !isStatic);
73 if (arguments != null){
74 for (int i = 0, max = arguments.length; i < max; i++){
75 arguments[i].generateCode(currentScope, codeStream, true);
78 // actual message invocation
79 if (syntheticAccessor == null){
81 codeStream.invokestatic(codegenBinding);
83 if( (receiver.isSuper()) || codegenBinding.isPrivate()){
84 codeStream.invokespecial(codegenBinding);
86 if (codegenBinding.declaringClass.isInterface()){
87 codeStream.invokeinterface(codegenBinding);
89 codeStream.invokevirtual(codegenBinding);
94 codeStream.invokestatic(syntheticAccessor);
96 // operation on the returned value
98 // implicit conversion if necessary
99 codeStream.generateImplicitConversion(implicitConversion);
101 // pop return value if any
102 switch(binding.returnType.id){
113 codeStream.recordPositionsFrom(pc, (int)(this.nameSourcePosition >>> 32)); // highlight selector
115 public boolean isSuperAccess() {
116 return receiver.isSuper();
118 public boolean isTypeAccess() {
119 return receiver != null && receiver.isTypeReference();
121 public void manageSyntheticAccessIfNecessary(BlockScope currentScope, FlowInfo flowInfo){
123 if (!flowInfo.isReachable()) return;
124 if (binding.isPrivate()){
126 // depth is set for both implicit and explicit access (see MethodBinding#canBeSeenBy)
127 if (currentScope.enclosingSourceType() != binding.declaringClass){
129 syntheticAccessor = ((SourceTypeBinding)binding.declaringClass).addSyntheticMethod(binding, isSuperAccess());
130 currentScope.problemReporter().needToEmulateMethodAccess(binding, this);
134 } else if (receiver instanceof QualifiedSuperReference){ // qualified super
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);
142 } else if (binding.isProtected()){
144 SourceTypeBinding enclosingSourceType;
145 if (((bits & DepthMASK) != 0)
146 && binding.declaringClass.getPackage()
147 != (enclosingSourceType = currentScope.enclosingSourceType()).getPackage()){
149 SourceTypeBinding currentCompatibleType = (SourceTypeBinding)enclosingSourceType.enclosingTypeAt((bits & DepthMASK) >> DepthSHIFT);
150 syntheticAccessor = currentCompatibleType.addSyntheticMethod(binding, isSuperAccess());
151 currentScope.problemReporter().needToEmulateMethodAccess(binding, this);
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))) {
166 this.codegenBinding = currentScope.enclosingSourceType().getUpdatedMethodBinding(binding, (ReferenceBinding) this.qualifyingType);
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)
173 public StringBuffer printExpression(int indent, StringBuffer output){
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);
183 return output.append(')');
186 public TypeBinding resolveType(BlockScope scope) {
187 // Answer the signature return type
188 // Base type promotion
190 constant = NotAConstant;
191 boolean receiverCast = false, argumentsCast = false;
192 if (this.receiver instanceof CastExpression) {
193 this.receiver.bits |= IgnoreNeedForCastCheckMASK; // will check later on
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);
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;
215 if ((argumentTypes[i] = argument.resolveType(scope)) == null){
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);
227 if (this.receiverType == null)
230 // base type cannot receive any message
231 if (this.receiverType.isBaseType()) {
232 scope.problemReporter().errorNoMethodFor(this, this.receiverType, argumentTypes);
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;
244 scope.problemReporter().errorNoMethodFor(this, this.receiverType, argumentTypes);
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;
254 return this.resolvedType = this.binding == null ? null : this.binding.returnType;
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);
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);
270 if (!receiver.isImplicitThis() && binding.declaringClass != receiverType) {
271 scope.problemReporter().indirectAccessToStaticMethod(this, binding);
274 if (arguments != null) {
275 for (int i = 0; i < arguments.length; i++) {
276 arguments[i].implicitWidening(binding.parameters[i], argumentTypes[i]);
279 CastExpression.checkNeedForArgumentCasts(scope, this.receiver, receiverType, binding, this.arguments, argumentTypes, this);
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);
287 // abstract private methods cannot occur nor abstract static............
289 if (isMethodUseDeprecated(binding, scope))
290 scope.problemReporter().deprecatedMethod(binding, this);
292 return this.resolvedType = binding.returnType;
294 public void setActualReceiverType(ReferenceBinding receiverType) {
295 this.qualifyingType = receiverType;
297 public void setDepth(int depth) {
298 bits &= ~DepthMASK; // flush previous depth if any
300 bits |= (depth & 0xFF) << DepthSHIFT; // encoded on 8 bits
303 public void setFieldIndex(int depth) {
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);
316 visitor.endVisit(this, blockScope);