Skip to main content
summaryrefslogtreecommitdiffstats
blob: d867c09ae4ce8d3abdc9b7d6629769ff06bd66af (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package org.eclipse.jdt.internal.compiler.ast;

/*
 * (c) Copyright IBM Corp. 2000, 2001.
 * All Rights Reserved.
 */
import org.eclipse.jdt.internal.compiler.*;
import org.eclipse.jdt.internal.compiler.impl.*;
import org.eclipse.jdt.internal.compiler.codegen.*;
import org.eclipse.jdt.internal.compiler.flow.*;
import org.eclipse.jdt.internal.compiler.lookup.*;
import org.eclipse.jdt.internal.compiler.problem.*;
import org.eclipse.jdt.internal.compiler.parser.*;

public abstract class AbstractMethodDeclaration extends AstNode implements ProblemSeverities, ReferenceContext {
	public MethodScope scope;
	//it is not relevent for constructor but it helps to have the name of the constructor here 
	//which is always the name of the class.....parsing do extra work to fill it up while it do not have to....
	public char[] selector;
	public int declarationSourceStart;
	public int declarationSourceEnd;
	public int modifiers;
	public int modifiersSourceStart;
	public Argument[] arguments;
	public TypeReference[] thrownExceptions;
	public Statement[] statements;
	public int explicitDeclarations;
	public MethodBinding binding;
	public boolean ignoreFurtherInvestigation = false;
	public boolean needFreeReturn = false;
	public LocalVariableBinding secretReturnValue;
	static final char[] SecretLocalDeclarationName = " returnValue".toCharArray(); //$NON-NLS-1$

	public int bodyStart;
	public int bodyEnd = -1;
/**
 * AbstractMethodDeclaration constructor comment.
 */
public AbstractMethodDeclaration() {
	super();
}
/*
 *	We cause the compilation task to abort to a given extent.
 */
public void abort(int abortLevel) {

	if (scope == null){
		throw new AbortCompilation(); // cannot do better
	}
	
	CompilationResult compilationResult = scope.referenceCompilationUnit().compilationResult;
	
	switch (abortLevel) {
		case AbortCompilation :
			throw new AbortCompilation(compilationResult);
		case AbortCompilationUnit :
			throw new AbortCompilationUnit(compilationResult);
		case AbortType :
			throw new AbortType(compilationResult);
		default :
			throw new AbortMethod(compilationResult);
	}
}
public void analyseCode(ClassScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {

	// starting of the code analysis for methods
	if (ignoreFurtherInvestigation)	return;
	try {
		if (binding == null) return;// may be in a non necessary <clinit> for innerclass with static final constant fields
		if (binding.isAbstract() || binding.isNative())	return;
			
		ExceptionHandlingFlowContext methodContext = new ExceptionHandlingFlowContext(flowContext, this, binding.thrownExceptions, scope, FlowInfo.DeadEnd);

		// propagate to statements
		if (statements != null) {
			for (int i = 0, count = statements.length; i < count; i++) {
				Statement stat;
				if (!flowInfo.complainIfUnreachable((stat = statements[i]), scope)) {
					flowInfo = stat.analyseCode(scope, methodContext, flowInfo);
				}
			}
		}
		// check for missing returning path
		TypeBinding returnType = binding.returnType;
		if ((returnType == VoidBinding) || isAbstract()) {
			needFreeReturn = !((flowInfo == FlowInfo.DeadEnd) || flowInfo.isFakeReachable());
		} else {
			if (flowInfo != FlowInfo.DeadEnd) {
				// special test for empty methods that should return something
				if ((statements == null) && (returnType != VoidBinding)) {
					scope.problemReporter().shouldReturn(returnType, this);
				} else {
					scope.problemReporter().shouldReturn(returnType, statements[statements.length - 1]);
				}
			}
		}
	} catch (AbortMethod e) {
		this.ignoreFurtherInvestigation = true;		
	}
}
public void bindArguments() {
	//bind and add argument's binding into the scope of the method

	if (arguments != null) {
		int length = arguments.length;
		for (int i = 0; i < length; i++) {
			Argument argument = arguments[i];
			if (argument.type != null) argument.type.binding = binding.parameters[i]; // record the resolved type into the type reference
			int modifierFlag = argument.modifiers;
			if ((argument.binding = scope.duplicateName(argument.name)) != null) {
				//the name already exist....may carry on with the first binding ....
				scope.problemReporter().redefineArgument(argument);
			} else {
				scope.addLocalVariable(argument.binding = new LocalVariableBinding(argument.name, binding.parameters[i], modifierFlag, true)); //true stand for argument instead of just local
				if (isTypeUseDeprecated(binding.parameters[i], scope))
					scope.problemReporter().deprecatedType(binding.parameters[i], argument.type);
				argument.binding.declaration = argument;
				argument.binding.used = binding.isAbstract() | binding.isNative(); // by default arguments in abstract/native methods are considered to be used (no complaint is expected)
			}
		}
	}
}
public void checkName(){
	//look if the name of the method is correct
}
public CompilationResult compilationResult(){
	return scope.referenceCompilationUnit().compilationResult;
}
/**
 * Bytecode generation for a method
 */
public void generateCode(ClassScope classScope, ClassFile classFile) {
	int problemResetPC = 0;
	if (ignoreFurtherInvestigation) {
		if (this.binding == null)
			return; // Handle methods with invalid signature or duplicates
		int problemsLength;
		IProblem[] problems = scope.referenceCompilationUnit().compilationResult.getProblems();
		IProblem[] problemsCopy = new IProblem[problemsLength = problems.length];
		System.arraycopy(problems, 0, problemsCopy, 0, problemsLength);
		classFile.addProblemMethod(this, binding, problemsCopy);
		return;
	}
	try {
		problemResetPC = classFile.contentsOffset;
		classFile.generateMethodInfoHeader(binding);
		int methodAttributeOffset = classFile.contentsOffset;
		int attributeNumber = classFile.generateMethodInfoAttribute(binding);
		if ((!binding.isNative()) && (!binding.isAbstract())) {
			int codeAttributeOffset = classFile.contentsOffset;
			classFile.generateCodeAttributeHeader();
			CodeStream codeStream = classFile.codeStream;
			codeStream.reset(this, classFile);
			// initialize local positions
			scope.computeLocalVariablePositions(binding.isStatic() ? 0 : 1, codeStream);

			// arguments initialization for local variable debug attributes
			if (arguments != null) {
				for (int i = 0, max = arguments.length; i < max; i++) {
					LocalVariableBinding argBinding;
					codeStream.addVisibleLocalVariable(argBinding = arguments[i].binding);
					argBinding.recordInitializationStartPC(0);
				}
			}
			if (statements != null) {
				for (int i = 0, max = statements.length; i < max; i++)
					statements[i].generateCode(scope, codeStream);
			}
			if (needFreeReturn) {
				codeStream.return_();
			}
			// local variable attributes
			codeStream.exitUserScope(scope);
			codeStream.recordPositionsFrom(0, this);
			classFile.completeCodeAttribute(codeAttributeOffset);
			attributeNumber++;
		}
		classFile.completeMethodInfo(methodAttributeOffset, attributeNumber);

		// if a problem got reported during code gen, then trigger problem method creation
		if (ignoreFurtherInvestigation){
			throw new AbortMethod(scope.referenceCompilationUnit().compilationResult);
		}
	} catch (AbortMethod e) {
		int problemsLength;
		IProblem[] problems = scope.referenceCompilationUnit().compilationResult.getProblems();
		IProblem[] problemsCopy = new IProblem[problemsLength = problems.length];
		System.arraycopy(problems, 0, problemsCopy, 0, problemsLength);
		classFile.addProblemMethod(this, binding, problemsCopy, problemResetPC);
	}
}
public boolean isAbstract(){

	if (binding != null) return binding.isAbstract();
	return (modifiers & AccAbstract) != 0 ;
}
public boolean isClinit() {
	return false;
}
/**
 * @return boolean
 */
public boolean isConstructor() {
	return false;
}
public boolean isDefaultConstructor() {
	return false;
}
public boolean isInitializationMethod() {
	return false;
}
public boolean isNative(){
	
	if (binding != null) return binding.isNative();
	return (modifiers & AccNative) != 0 ;
}
public boolean isStatic() {
	if (binding != null) return binding.isStatic();	
	return (modifiers & AccStatic) != 0;
}
public abstract void  parseStatements(Parser parser, CompilationUnitDeclaration unit);
	//fill up the method body with statement
public void resolve(ClassScope upperScope) {
	if (binding == null) {
		ignoreFurtherInvestigation = true;
		return;
	}

	// ========= abort on fatal error =============
	try {
		bindArguments(); //<-- shoud be done at binding/scope creation time
		checkName();

		// create secret value location
		scope.addLocalVariable(
			secretReturnValue = new LocalVariableBinding(SecretLocalDeclarationName, binding.returnType, AccDefault));
		secretReturnValue.constant = NotAConstant; // not inlinable

		// and then ....deep jump into statements.....
		if (statements != null) {
			int i = 0, length = statements.length;
			while (i < length)
				statements[i++].resolve(scope);
		}
	} catch (AbortMethod e) {
		this.ignoreFurtherInvestigation = true;		
	}
}
public String returnTypeToString(int tab) {
	/*slow code */

	return ""; //$NON-NLS-1$
}
public void tagAsHavingErrors() {
	ignoreFurtherInvestigation = true;
}
public String toString(int tab) {
	/* slow code */

	String s = tabString(tab);
	if (modifiers != AccDefault) {
		s += modifiersString(modifiers);
	}

	s += returnTypeToString(0);

	s += new String(selector) + "("; //$NON-NLS-1$
	if (arguments != null) {
		for (int i = 0; i < arguments.length; i++) {
			s += arguments[i].toString(0);
			if (i != (arguments.length - 1))
				s = s + ", "; //$NON-NLS-1$
		};
	};
	s += ")"; //$NON-NLS-1$
	if (thrownExceptions != null) {
		s += " throws "; //$NON-NLS-1$
		for (int i = 0; i < thrownExceptions.length; i++) {
			s += thrownExceptions[i].toString(0);
			if (i != (thrownExceptions.length - 1))
				s = s + ", "; //$NON-NLS-1$
		};
	};

	s += toStringStatements(tab + 1);

	return s;
}
public String toStringStatements(int tab) {
	/* slow code */

	if (isAbstract() || (this.modifiers & AccSemicolonBody) != 0) return ";"; //$NON-NLS-1$
	
	String s = " {"; //$NON-NLS-1$
	if (statements != null) {
		for (int i = 0; i < statements.length; i++){
			s = s + "\n" + statements[i].toString(tab); //$NON-NLS-1$
			if (!(statements[i] instanceof Block)){
				s += ";"; //$NON-NLS-1$
			}
		}
	}
	s += "\n" + tabString(tab == 0 ? 0 : tab - 1) + "}"; //$NON-NLS-2$ //$NON-NLS-1$
	return s;
}
public void traverse(IAbstractSyntaxTreeVisitor visitor, ClassScope classScope) {
}
}

Back to the top