1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 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 *******************************************************************************/
11 package net.sourceforge.phpeclipse.internal.compiler.ast;
13 import net.sourceforge.phpdt.core.compiler.CharOperation;
14 import net.sourceforge.phpdt.internal.compiler.CompilationResult;
15 import net.sourceforge.phpdt.internal.compiler.IAbstractSyntaxTreeVisitor;
16 import net.sourceforge.phpdt.internal.compiler.flow.ExceptionHandlingFlowContext;
17 import net.sourceforge.phpdt.internal.compiler.flow.FlowInfo;
18 import net.sourceforge.phpdt.internal.compiler.flow.InitializationFlowContext;
19 import net.sourceforge.phpdt.internal.compiler.lookup.ClassScope;
20 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
21 import net.sourceforge.phpdt.internal.compiler.parser.UnitParser;
22 import net.sourceforge.phpdt.internal.compiler.problem.AbortMethod;
24 public class MethodDeclaration extends AbstractMethodDeclaration {
26 public TypeReference returnType;
29 * MethodDeclaration constructor comment.
31 public MethodDeclaration(CompilationResult compilationResult) {
32 super(compilationResult);
35 public void analyseCode(ClassScope classScope, InitializationFlowContext initializationContext, FlowInfo flowInfo) {
37 // starting of the code analysis for methods
38 if (ignoreFurtherInvestigation)
44 if (this.binding.isPrivate() && !this.binding.isPrivateUsed()) {
45 if (!classScope.referenceCompilationUnit().compilationResult.hasSyntaxError()) {
46 scope.problemReporter().unusedPrivateMethod(this);
50 // may be in a non necessary <clinit> for innerclass with static final constant fields
51 if (binding.isAbstract()) // || binding.isNative())
54 ExceptionHandlingFlowContext methodContext =
55 new ExceptionHandlingFlowContext(initializationContext, this, binding.thrownExceptions, scope, FlowInfo.DEAD_END);
57 // propagate to statements
58 if (statements != null) {
59 boolean didAlreadyComplain = false;
60 for (int i = 0, count = statements.length; i < count; i++) {
62 if (!flowInfo.complainIfUnreachable((stat = statements[i]), scope, didAlreadyComplain)) {
63 flowInfo = stat.analyseCode(scope, methodContext, flowInfo);
65 didAlreadyComplain = true;
69 // check for missing returning path
70 TypeBinding returnType = binding.returnType;
71 if ((returnType == VoidBinding) || isAbstract()) {
72 this.needFreeReturn = flowInfo.isReachable();
74 if (flowInfo != FlowInfo.DEAD_END) {
75 scope.problemReporter().shouldReturn(returnType, this);
78 } catch (AbortMethod e) {
79 this.ignoreFurtherInvestigation = true;
83 public void parseStatements(UnitParser parser, CompilationUnitDeclaration unit) {
85 //fill up the method body with statement
86 if (ignoreFurtherInvestigation)
88 parser.parse(this, unit);
91 public void resolveStatements() {
93 // ========= abort on fatal error =============
94 if (this.returnType != null && this.binding != null) {
95 this.returnType.resolvedType = this.binding.returnType;
96 // record the return type binding
98 // look if the name of the method is correct
99 if (binding != null && isTypeUseDeprecated(binding.returnType, scope))
100 scope.problemReporter().deprecatedType(binding.returnType, returnType);
103 if (CharOperation.equals(scope.enclosingSourceType().sourceName, selector))
104 scope.problemReporter().methodWithConstructorName(this);
106 // by grammatical construction, interface methods are always abstract
107 if (!scope.enclosingSourceType().isInterface()) {
109 // if a method has an semicolon body and is not declared as abstract==>error
110 // native methods may have a semicolon body
111 // if ((modifiers & AccSemicolonBody) != 0) {
112 // if ((modifiers & AccNative) == 0)
113 // if ((modifiers & AccAbstract) == 0)
114 // scope.problemReporter().methodNeedingAbstractModifier(this);
116 // // the method HAS a body --> abstract native modifiers are forbiden
117 // if (((modifiers & AccNative) != 0) || ((modifiers & AccAbstract) != 0))
118 // scope.problemReporter().methodNeedingNoBody(this);
122 super.resolveStatements();
125 public String returnTypeToString(int tab) {
127 if (returnType == null)
128 return ""; //$NON-NLS-1$
129 return returnType.toString(tab) + " "; //$NON-NLS-1$
132 public void traverse(IAbstractSyntaxTreeVisitor visitor, ClassScope classScope) {
134 if (visitor.visit(this, classScope)) {
135 if (returnType != null)
136 returnType.traverse(visitor, scope);
137 if (arguments != null) {
138 int argumentLength = arguments.length;
139 for (int i = 0; i < argumentLength; i++)
140 arguments[i].traverse(visitor, scope);
142 if (thrownExceptions != null) {
143 int thrownExceptionsLength = thrownExceptions.length;
144 for (int i = 0; i < thrownExceptionsLength; i++)
145 thrownExceptions[i].traverse(visitor, scope);
147 if (statements != null) {
148 int statementsLength = statements.length;
149 for (int i = 0; i < statementsLength; i++)
150 statements[i].traverse(visitor, scope);
153 visitor.endVisit(this, classScope);