Merged xdebug from 1.3.x
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / Clinit.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.ast;
12
13 import net.sourceforge.phpdt.internal.compiler.CompilationResult;
14 import net.sourceforge.phpdt.internal.compiler.IAbstractSyntaxTreeVisitor;
15 import net.sourceforge.phpdt.internal.compiler.flow.ExceptionHandlingFlowContext;
16 import net.sourceforge.phpdt.internal.compiler.flow.FlowInfo;
17 import net.sourceforge.phpdt.internal.compiler.flow.InitializationFlowContext;
18 import net.sourceforge.phpdt.internal.compiler.lookup.ClassScope;
19 import net.sourceforge.phpdt.internal.compiler.lookup.FieldBinding;
20 import net.sourceforge.phpdt.internal.compiler.lookup.MethodScope;
21 import net.sourceforge.phpdt.internal.compiler.lookup.SourceTypeBinding;
22 import net.sourceforge.phpdt.internal.compiler.parser.UnitParser;
23 import net.sourceforge.phpdt.internal.compiler.problem.AbortMethod;
24
25 public class Clinit extends AbstractMethodDeclaration {
26
27         public final static char[] ConstantPoolName = "<clinit>".toCharArray(); //$NON-NLS-1$
28
29         private FieldBinding assertionSyntheticFieldBinding = null;
30
31         private FieldBinding classLiteralSyntheticField = null;
32
33         public Clinit(CompilationResult compilationResult) {
34                 super(compilationResult);
35                 modifiers = 0;
36                 selector = ConstantPoolName;
37         }
38
39         public void analyseCode(ClassScope classScope,
40                         InitializationFlowContext staticInitializerFlowContext,
41                         FlowInfo flowInfo) {
42
43                 if (ignoreFurtherInvestigation)
44                         return;
45                 try {
46                         ExceptionHandlingFlowContext clinitContext = new ExceptionHandlingFlowContext(
47                                         staticInitializerFlowContext.parent, this, NoExceptions,
48                                         scope, FlowInfo.DEAD_END);
49
50                         // check for missing returning path
51                         this.needFreeReturn = flowInfo.isReachable();
52
53                         // check missing blank final field initializations
54                         flowInfo = flowInfo
55                                         .mergedWith(staticInitializerFlowContext.initsOnReturn);
56                         FieldBinding[] fields = scope.enclosingSourceType().fields();
57                         for (int i = 0, count = fields.length; i < count; i++) {
58                                 FieldBinding field;
59                                 if ((field = fields[i]).isStatic() && field.isFinal()
60                                                 && (!flowInfo.isDefinitelyAssigned(fields[i]))) {
61                                         scope.problemReporter().uninitializedBlankFinalField(field,
62                                                         scope.referenceType().declarationOf(field));
63                                         // can complain against the field decl, since only one
64                                         // <clinit>
65                                 }
66                         }
67                         // check static initializers thrown exceptions
68                         staticInitializerFlowContext.checkInitializerExceptions(scope,
69                                         clinitContext, flowInfo);
70                 } catch (AbortMethod e) {
71                         this.ignoreFurtherInvestigation = true;
72                 }
73         }
74
75         /**
76          * Bytecode generation for a <clinit> method
77          * 
78          * @param classScope
79          *            net.sourceforge.phpdt.internal.compiler.lookup.ClassScope
80          * @param classFile
81          *            net.sourceforge.phpdt.internal.compiler.codegen.ClassFile
82          */
83         // public void generateCode(ClassScope classScope, ClassFile classFile) {
84         //
85         // int clinitOffset = 0;
86         // if (ignoreFurtherInvestigation) {
87         // // should never have to add any <clinit> problem method
88         // return;
89         // }
90         // try {
91         // clinitOffset = classFile.contentsOffset;
92         // this.generateCode(classScope, classFile, clinitOffset);
93         // } catch (AbortMethod e) {
94         // // should never occur
95         // // the clinit referenceContext is the type declaration
96         // // All clinit problems will be reported against the type: AbortType
97         // instead of AbortMethod
98         // // reset the contentsOffset to the value before generating the clinit
99         // code
100         // // decrement the number of method info as well.
101         // // This is done in the addProblemMethod and addProblemConstructor for
102         // other
103         // // cases.
104         // if (e.compilationResult == CodeStream.RESTART_IN_WIDE_MODE) {
105         // // a branch target required a goto_w, restart code gen in wide mode.
106         // try {
107         // if (statements != null) {
108         // for (int i = 0, max = statements.length; i < max; i++)
109         // statements[i].resetStateForCodeGeneration();
110         // }
111         // classFile.contentsOffset = clinitOffset;
112         // classFile.methodCount--;
113         // classFile.codeStream.wideMode = true; // request wide mode
114         // this.generateCode(classScope, classFile, clinitOffset);
115         // // restart method generation
116         // } catch (AbortMethod e2) {
117         // classFile.contentsOffset = clinitOffset;
118         // classFile.methodCount--;
119         // }
120         // } else {
121         // // produce a problem method accounting for this fatal error
122         // classFile.contentsOffset = clinitOffset;
123         // classFile.methodCount--;
124         // }
125         // }
126         // }
127         /**
128          * Bytecode generation for a <clinit> method
129          * 
130          * @param classScope
131          *            net.sourceforge.phpdt.internal.compiler.lookup.ClassScope
132          * @param classFile
133          *            net.sourceforge.phpdt.internal.compiler.codegen.ClassFile
134          */
135         // private void generateCode(
136         // ClassScope classScope,
137         // ClassFile classFile,
138         // int clinitOffset) {
139         //
140         // ConstantPool constantPool = classFile.constantPool;
141         // int constantPoolOffset = constantPool.currentOffset;
142         // int constantPoolIndex = constantPool.currentIndex;
143         // classFile.generateMethodInfoHeaderForClinit();
144         // int codeAttributeOffset = classFile.contentsOffset;
145         // classFile.generateCodeAttributeHeader();
146         // CodeStream codeStream = classFile.codeStream;
147         // this.resolve(classScope);
148         //
149         // codeStream.reset(this, classFile);
150         // TypeDeclaration declaringType = classScope.referenceContext;
151         //
152         // // initialize local positions - including initializer scope.
153         // MethodScope staticInitializerScope =
154         // declaringType.staticInitializerScope;
155         // staticInitializerScope.computeLocalVariablePositions(0, codeStream);
156         //
157         // // 1.4 feature
158         // // This has to be done before any other initialization
159         // if (this.assertionSyntheticFieldBinding != null) {
160         // // generate code related to the activation of assertion for this class
161         // codeStream.generateClassLiteralAccessForType(
162         // classScope.enclosingSourceType(),
163         // classLiteralSyntheticField);
164         // codeStream.invokeJavaLangClassDesiredAssertionStatus();
165         // Label falseLabel = new Label(codeStream);
166         // codeStream.ifne(falseLabel);
167         // codeStream.iconst_1();
168         // Label jumpLabel = new Label(codeStream);
169         // codeStream.goto_(jumpLabel);
170         // falseLabel.place();
171         // codeStream.iconst_0();
172         // jumpLabel.place();
173         // codeStream.putstatic(this.assertionSyntheticFieldBinding);
174         // }
175         // // generate initializers
176         // if (declaringType.fields != null) {
177         // for (int i = 0, max = declaringType.fields.length; i < max; i++) {
178         // FieldDeclaration fieldDecl;
179         // if ((fieldDecl = declaringType.fields[i]).isStatic()) {
180         // fieldDecl.generateCode(staticInitializerScope, codeStream);
181         // }
182         // }
183         // }
184         // if (codeStream.position == 0) {
185         // // do not need to output a Clinit if no bytecodes
186         // // so we reset the offset inside the byte array contents.
187         // classFile.contentsOffset = clinitOffset;
188         // // like we don't addd a method we need to undo the increment on the
189         // method count
190         // classFile.methodCount--;
191         // // reset the constant pool to its state before the clinit
192         // constantPool.resetForClinit(constantPoolIndex, constantPoolOffset);
193         // } else {
194         // if (this.needFreeReturn) {
195         // int oldPosition = codeStream.position;
196         // codeStream.return_();
197         // codeStream.updateLocalVariablesAttribute(oldPosition);
198         // }
199         // // Record the end of the clinit: point to the declaration of the class
200         // codeStream.recordPositionsFrom(0, declaringType.sourceStart);
201         // classFile.completeCodeAttributeForClinit(codeAttributeOffset);
202         // }
203         // }
204         public boolean isClinit() {
205
206                 return true;
207         }
208
209         public boolean isInitializationMethod() {
210
211                 return true;
212         }
213
214         public boolean isStatic() {
215
216                 return true;
217         }
218
219         public void parseStatements(UnitParser parser,
220                         CompilationUnitDeclaration unit) {
221                 // the clinit is filled by hand ....
222         }
223
224         public StringBuffer print(int tab, StringBuffer output) {
225
226                 printIndent(tab, output).append("<clinit>()"); //$NON-NLS-1$
227                 printBody(tab + 1, output);
228                 return output;
229         }
230
231         public void resolve(ClassScope scope) {
232
233                 this.scope = new MethodScope(scope, scope.referenceContext, true);
234         }
235
236         public String toString(int tab) {
237
238                 String s = ""; //$NON-NLS-1$
239                 s = s + tabString(tab);
240                 s = s + "<clinit>()"; //$NON-NLS-1$
241                 s = s + toStringStatements(tab + 1);
242                 return s;
243         }
244
245         public void traverse(IAbstractSyntaxTreeVisitor visitor,
246                         ClassScope classScope) {
247
248                 visitor.visit(this, classScope);
249                 visitor.endVisit(this, classScope);
250         }
251
252         // 1.4 feature
253         public void addSupportForAssertion(
254                         FieldBinding assertionSyntheticFieldBinding) {
255
256                 this.assertionSyntheticFieldBinding = assertionSyntheticFieldBinding;
257
258                 // we need to add the field right now, because the field infos are
259                 // generated before the methods
260                 SourceTypeBinding sourceType = this.scope.outerMostMethodScope()
261                                 .enclosingSourceType();
262                 this.classLiteralSyntheticField = sourceType.addSyntheticField(
263                                 sourceType, scope);
264         }
265
266 }