Makefile fixup
[org.ibex.tool.git] / src / org / eclipse / jdt / internal / compiler / ast / AbstractMethodDeclaration.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  *******************************************************************************/
11 package org.eclipse.jdt.internal.compiler.ast;
12
13 import org.eclipse.jdt.core.compiler.*;
14 import org.eclipse.jdt.internal.compiler.*;
15 import org.eclipse.jdt.internal.compiler.flow.FlowInfo;
16 import org.eclipse.jdt.internal.compiler.flow.InitializationFlowContext;
17 import org.eclipse.jdt.internal.compiler.impl.*;
18 import org.eclipse.jdt.internal.compiler.codegen.*;
19 import org.eclipse.jdt.internal.compiler.lookup.*;
20 import org.eclipse.jdt.internal.compiler.problem.*;
21 import org.eclipse.jdt.internal.compiler.parser.*;
22
23 public abstract class AbstractMethodDeclaration
24         extends ASTNode
25         implements ProblemSeverities, ReferenceContext {
26                 
27         public MethodScope scope;
28         //it is not relevent for constructor but it helps to have the name of the constructor here 
29         //which is always the name of the class.....parsing do extra work to fill it up while it do not have to....
30         public char[] selector;
31         public int declarationSourceStart;
32         public int declarationSourceEnd;
33         public int modifiers;
34         public int modifiersSourceStart;
35         public Argument[] arguments;
36         public TypeReference[] thrownExceptions;
37         public Statement[] statements;
38         public int explicitDeclarations;
39         public MethodBinding binding;
40         public boolean ignoreFurtherInvestigation = false;
41         public boolean needFreeReturn = false;
42         
43         public Javadoc javadoc;
44         
45         public int bodyStart;
46         public int bodyEnd = -1;
47         public CompilationResult compilationResult;
48         
49         public boolean errorInSignature = false; 
50         
51         AbstractMethodDeclaration(CompilationResult compilationResult){
52                 this.compilationResult = compilationResult;
53         }
54         
55         /*
56          *      We cause the compilation task to abort to a given extent.
57          */
58         public void abort(int abortLevel, IProblem problem) {
59
60                 switch (abortLevel) {
61                         case AbortCompilation :
62                                 throw new AbortCompilation(this.compilationResult, problem);
63                         case AbortCompilationUnit :
64                                 throw new AbortCompilationUnit(this.compilationResult, problem);
65                         case AbortType :
66                                 throw new AbortType(this.compilationResult, problem);
67                         default :
68                                 throw new AbortMethod(this.compilationResult, problem);
69                 }
70         }
71
72         public abstract void analyseCode(ClassScope classScope, InitializationFlowContext initializationContext, FlowInfo info);
73
74                 /**
75          * Bind and add argument's binding into the scope of the method
76          */
77         public void bindArguments() {
78
79                 if (this.arguments != null) {
80                         // by default arguments in abstract/native methods are considered to be used (no complaint is expected)
81                         boolean used = this.binding == null || this.binding.isAbstract() || this.binding.isNative();
82
83                         int length = this.arguments.length;
84                         for (int i = 0; i < length; i++) {
85                                 TypeBinding argType = this.binding == null ? null : this.binding.parameters[i];
86                                 this.arguments[i].bind(this.scope, argType, used);
87                         }
88                 }
89         }
90
91         /**
92          * Record the thrown exception type bindings in the corresponding type references.
93          */
94         public void bindThrownExceptions() {
95
96                 if (this.thrownExceptions != null
97                         && this.binding != null
98                         && this.binding.thrownExceptions != null) {
99                         int thrownExceptionLength = this.thrownExceptions.length;
100                         int length = this.binding.thrownExceptions.length;
101                         if (length == thrownExceptionLength) {
102                                 for (int i = 0; i < length; i++) {
103                                         this.thrownExceptions[i].resolvedType = this.binding.thrownExceptions[i];
104                                 }
105                         } else {
106                                 int bindingIndex = 0;
107                                 for (int i = 0; i < thrownExceptionLength && bindingIndex < length; i++) {
108                                         TypeReference thrownException = this.thrownExceptions[i];
109                                         ReferenceBinding thrownExceptionBinding = this.binding.thrownExceptions[bindingIndex];
110                                         char[][] bindingCompoundName = thrownExceptionBinding.compoundName;
111                                         if (thrownException instanceof SingleTypeReference) {
112                                                 // single type reference
113                                                 int lengthName = bindingCompoundName.length;
114                                                 char[] thrownExceptionTypeName = thrownException.getTypeName()[0];
115                                                 if (CharOperation.equals(thrownExceptionTypeName, bindingCompoundName[lengthName - 1])) {
116                                                         thrownException.resolvedType = thrownExceptionBinding;
117                                                         bindingIndex++;
118                                                 }
119                                         } else {
120                                                 // qualified type reference
121                                                 if (CharOperation.equals(thrownException.getTypeName(), bindingCompoundName)) {
122                                                         thrownException.resolvedType = thrownExceptionBinding;
123                                                         bindingIndex++;
124                                                 }                                               
125                                         }
126                                 }
127                         }
128                 }
129         }
130
131         public CompilationResult compilationResult() {
132                 
133                 return this.compilationResult;
134         }
135         
136         /**
137          * Bytecode generation for a method
138          * @param classScope
139          * @param classFile
140          */
141         public void generateCode(ClassScope classScope, ClassFile classFile) {
142                 
143                 int problemResetPC = 0;
144                 classFile.codeStream.wideMode = false; // reset wideMode to false
145                 if (this.ignoreFurtherInvestigation) {
146                         // method is known to have errors, dump a problem method
147                         if (this.binding == null)
148                                 return; // handle methods with invalid signature or duplicates
149                         int problemsLength;
150                         IProblem[] problems =
151                                 this.scope.referenceCompilationUnit().compilationResult.getProblems();
152                         IProblem[] problemsCopy = new IProblem[problemsLength = problems.length];
153                         System.arraycopy(problems, 0, problemsCopy, 0, problemsLength);
154                         classFile.addProblemMethod(this, this.binding, problemsCopy);
155                         return;
156                 }
157                 // regular code generation
158                 try {
159                         problemResetPC = classFile.contentsOffset;
160                         this.generateCode(classFile);
161                 } catch (AbortMethod e) {
162                         // a fatal error was detected during code generation, need to restart code gen if possible
163                         if (e.compilationResult == CodeStream.RESTART_IN_WIDE_MODE) {
164                                 // a branch target required a goto_w, restart code gen in wide mode.
165                                 try {
166                                         classFile.contentsOffset = problemResetPC;
167                                         classFile.methodCount--;
168                                         classFile.codeStream.wideMode = true; // request wide mode 
169                                         this.generateCode(classFile); // restart method generation
170                                 } catch (AbortMethod e2) {
171                                         int problemsLength;
172                                         IProblem[] problems =
173                                                 this.scope.referenceCompilationUnit().compilationResult.getAllProblems();
174                                         IProblem[] problemsCopy = new IProblem[problemsLength = problems.length];
175                                         System.arraycopy(problems, 0, problemsCopy, 0, problemsLength);
176                                         classFile.addProblemMethod(this, this.binding, problemsCopy, problemResetPC);
177                                 }
178                         } else {
179                                 // produce a problem method accounting for this fatal error
180                                 int problemsLength;
181                                 IProblem[] problems =
182                                         this.scope.referenceCompilationUnit().compilationResult.getAllProblems();
183                                 IProblem[] problemsCopy = new IProblem[problemsLength = problems.length];
184                                 System.arraycopy(problems, 0, problemsCopy, 0, problemsLength);
185                                 classFile.addProblemMethod(this, this.binding, problemsCopy, problemResetPC);
186                         }
187                 }
188         }
189
190         private void generateCode(ClassFile classFile) {
191
192                 classFile.generateMethodInfoHeader(this.binding);
193                 int methodAttributeOffset = classFile.contentsOffset;
194                 int attributeNumber = classFile.generateMethodInfoAttribute(this.binding);
195                 if ((!this.binding.isNative()) && (!this.binding.isAbstract())) {
196                         int codeAttributeOffset = classFile.contentsOffset;
197                         classFile.generateCodeAttributeHeader();
198                         CodeStream codeStream = classFile.codeStream;
199                         codeStream.reset(this, classFile);
200                         // initialize local positions
201                         this.scope.computeLocalVariablePositions(this.binding.isStatic() ? 0 : 1, codeStream);
202
203                         // arguments initialization for local variable debug attributes
204                         if (this.arguments != null) {
205                                 for (int i = 0, max = this.arguments.length; i < max; i++) {
206                                         LocalVariableBinding argBinding;
207                                         codeStream.addVisibleLocalVariable(argBinding = this.arguments[i].binding);
208                                         argBinding.recordInitializationStartPC(0);
209                                 }
210                         }
211                         if (this.statements != null) {
212                                 for (int i = 0, max = this.statements.length; i < max; i++)
213                                         this.statements[i].generateCode(this.scope, codeStream);
214                         }
215                         if (this.needFreeReturn) {
216                                 codeStream.return_();
217                         }
218                         // local variable attributes
219                         codeStream.exitUserScope(this.scope);
220                         codeStream.recordPositionsFrom(0, this.declarationSourceEnd);
221                         classFile.completeCodeAttribute(codeAttributeOffset);
222                         attributeNumber++;
223                 } else {
224                         checkArgumentsSize();
225                 }
226                 classFile.completeMethodInfo(methodAttributeOffset, attributeNumber);
227
228                 // if a problem got reported during code gen, then trigger problem method creation
229                 if (this.ignoreFurtherInvestigation) {
230                         throw new AbortMethod(this.scope.referenceCompilationUnit().compilationResult, null);
231                 }
232         }
233
234         private void checkArgumentsSize() {
235                 TypeBinding[] parameters = this.binding.parameters;
236                 int size = 1; // an abstact method or a native method cannot be static
237                 for (int i = 0, max = parameters.length; i < max; i++) {
238                         TypeBinding parameter = parameters[i];
239                         if (parameter == LongBinding || parameter == DoubleBinding) {
240                                 size += 2;
241                         } else {
242                                 size++;
243                         }
244                         if (size > 0xFF) {
245                                 this.scope.problemReporter().noMoreAvailableSpaceForArgument(this.scope.locals[i], this.scope.locals[i].declaration);
246                         }
247                 }
248         }
249         
250         public boolean hasErrors() {
251                 return this.ignoreFurtherInvestigation;
252         }
253
254         public boolean isAbstract() {
255
256                 if (this.binding != null)
257                         return this.binding.isAbstract();
258                 return (this.modifiers & AccAbstract) != 0;
259         }
260
261         public boolean isClinit() {
262
263                 return false;
264         }
265
266         public boolean isConstructor() {
267
268                 return false;
269         }
270
271         public boolean isDefaultConstructor() {
272
273                 return false;
274         }
275
276         public boolean isInitializationMethod() {
277
278                 return false;
279         }
280
281         public boolean isNative() {
282
283                 if (this.binding != null)
284                         return this.binding.isNative();
285                 return (this.modifiers & AccNative) != 0;
286         }
287
288         public boolean isStatic() {
289
290                 if (this.binding != null)
291                         return this.binding.isStatic();
292                 return (this.modifiers & AccStatic) != 0;
293         }
294
295         /**
296          * Fill up the method body with statement
297          * @param parser
298          * @param unit
299          */
300         public abstract void parseStatements(
301                 Parser parser,
302                 CompilationUnitDeclaration unit);
303
304         public StringBuffer print(int tab, StringBuffer output) {
305
306                 printIndent(tab, output);
307                 printModifiers(this.modifiers, output);
308                 printReturnType(0, output).append(this.selector).append('(');
309                 if (this.arguments != null) {
310                         for (int i = 0; i < this.arguments.length; i++) {
311                                 if (i > 0) output.append(", "); //$NON-NLS-1$
312                                 this.arguments[i].print(0, output);
313                         }
314                 }
315                 output.append(')');
316                 if (this.thrownExceptions != null) {
317                         output.append(" throws "); //$NON-NLS-1$
318                         for (int i = 0; i < this.thrownExceptions.length; i++) {
319                                 if (i > 0) output.append(", "); //$NON-NLS-1$
320                                 this.thrownExceptions[i].print(0, output);
321                         }
322                 }
323                 printBody(tab + 1, output);
324                 return output;
325         }
326
327         public StringBuffer printBody(int indent, StringBuffer output) {
328
329                 if (isAbstract() || (this.modifiers & AccSemicolonBody) != 0) 
330                         return output.append(';');
331
332                 output.append(" {"); //$NON-NLS-1$
333                 if (this.statements != null) {
334                         for (int i = 0; i < this.statements.length; i++) {
335                                 output.append('\n');
336                                 this.statements[i].printStatement(indent, output); 
337                         }
338                 }
339                 output.append('\n'); //$NON-NLS-1$
340                 printIndent(indent == 0 ? 0 : indent - 1, output).append('}');
341                 return output;
342         }
343
344         public StringBuffer printReturnType(int indent, StringBuffer output) {
345                 
346                 return output;
347         }
348
349         public void resolve(ClassScope upperScope) {
350
351                 if (this.binding == null) {
352                         this.ignoreFurtherInvestigation = true;
353                 }
354
355                 try {
356                         bindArguments(); 
357                         bindThrownExceptions();
358                         resolveJavadoc();
359                         resolveStatements();
360                 } catch (AbortMethod e) {       // ========= abort on fatal error =============
361                         this.ignoreFurtherInvestigation = true;
362                 } 
363         }
364
365         public void resolveJavadoc() {
366                 
367                 if (this.binding == null) return;
368                 if (this.javadoc != null) {
369                         this.javadoc.resolve(this.scope);
370                         return;
371                 }
372                 if (this.binding.declaringClass != null && !this.binding.declaringClass.isLocalType()) {
373                         this.scope.problemReporter().javadocMissing(this.sourceStart, this.sourceEnd, this.binding.modifiers);
374                 }
375         }
376
377         public void resolveStatements() {
378
379                 if (this.statements != null) {
380                         for (int i = 0, length = this.statements.length; i < length; i++) {
381                                 this.statements[i].resolve(this.scope);
382                         }
383                 } else if ((this.bits & UndocumentedEmptyBlockMASK) != 0) {
384                         this.scope.problemReporter().undocumentedEmptyBlock(this.bodyStart-1, this.bodyEnd+1);
385                 }
386         }
387
388         public void tagAsHavingErrors() {
389
390                 this.ignoreFurtherInvestigation = true;
391         }
392
393         public void traverse(
394                 ASTVisitor visitor,
395                 ClassScope classScope) {
396                 // default implementation: subclass will define it
397         }
398 }