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.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;
25 public class Clinit extends AbstractMethodDeclaration {
27 public final static char[] ConstantPoolName = "<clinit>".toCharArray(); //$NON-NLS-1$
29 private FieldBinding assertionSyntheticFieldBinding = null;
30 private FieldBinding classLiteralSyntheticField = null;
32 public Clinit(CompilationResult compilationResult) {
33 super(compilationResult);
35 selector = ConstantPoolName;
38 public void analyseCode(
39 ClassScope classScope,
40 InitializationFlowContext staticInitializerFlowContext,
43 if (ignoreFurtherInvestigation)
46 ExceptionHandlingFlowContext clinitContext =
47 new ExceptionHandlingFlowContext(
48 staticInitializerFlowContext.parent,
54 // check for missing returning path
55 this.needFreeReturn = flowInfo.isReachable();
57 // check missing blank final field initializations
58 flowInfo = flowInfo.mergedWith(staticInitializerFlowContext.initsOnReturn);
59 FieldBinding[] fields = scope.enclosingSourceType().fields();
60 for (int i = 0, count = fields.length; i < count; i++) {
62 if ((field = fields[i]).isStatic()
64 && (!flowInfo.isDefinitelyAssigned(fields[i]))) {
65 scope.problemReporter().uninitializedBlankFinalField(
67 scope.referenceType().declarationOf(field));
68 // can complain against the field decl, since only one <clinit>
71 // check static initializers thrown exceptions
72 staticInitializerFlowContext.checkInitializerExceptions(
76 } catch (AbortMethod e) {
77 this.ignoreFurtherInvestigation = true;
82 * Bytecode generation for a <clinit> method
84 * @param classScope org.eclipse.jdt.internal.compiler.lookup.ClassScope
85 * @param classFile org.eclipse.jdt.internal.compiler.codegen.ClassFile
87 // public void generateCode(ClassScope classScope, ClassFile classFile) {
89 // int clinitOffset = 0;
90 // if (ignoreFurtherInvestigation) {
91 // // should never have to add any <clinit> problem method
95 // clinitOffset = classFile.contentsOffset;
96 // this.generateCode(classScope, classFile, clinitOffset);
97 // } catch (AbortMethod e) {
98 // // should never occur
99 // // the clinit referenceContext is the type declaration
100 // // All clinit problems will be reported against the type: AbortType instead of AbortMethod
101 // // reset the contentsOffset to the value before generating the clinit code
102 // // decrement the number of method info as well.
103 // // This is done in the addProblemMethod and addProblemConstructor for other
105 // if (e.compilationResult == CodeStream.RESTART_IN_WIDE_MODE) {
106 // // a branch target required a goto_w, restart code gen in wide mode.
108 // if (statements != null) {
109 // for (int i = 0, max = statements.length; i < max; i++)
110 // statements[i].resetStateForCodeGeneration();
112 // classFile.contentsOffset = clinitOffset;
113 // classFile.methodCount--;
114 // classFile.codeStream.wideMode = true; // request wide mode
115 // this.generateCode(classScope, classFile, clinitOffset);
116 // // restart method generation
117 // } catch (AbortMethod e2) {
118 // classFile.contentsOffset = clinitOffset;
119 // classFile.methodCount--;
122 // // produce a problem method accounting for this fatal error
123 // classFile.contentsOffset = clinitOffset;
124 // classFile.methodCount--;
130 * Bytecode generation for a <clinit> method
132 * @param classScope org.eclipse.jdt.internal.compiler.lookup.ClassScope
133 * @param classFile org.eclipse.jdt.internal.compiler.codegen.ClassFile
135 // private void generateCode(
136 // ClassScope classScope,
137 // ClassFile classFile,
138 // int clinitOffset) {
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);
149 // codeStream.reset(this, classFile);
150 // TypeDeclaration declaringType = classScope.referenceContext;
152 // // initialize local positions - including initializer scope.
153 // MethodScope staticInitializerScope = declaringType.staticInitializerScope;
154 // staticInitializerScope.computeLocalVariablePositions(0, codeStream);
157 // // This has to be done before any other initialization
158 // if (this.assertionSyntheticFieldBinding != null) {
159 // // generate code related to the activation of assertion for this class
160 // codeStream.generateClassLiteralAccessForType(
161 // classScope.enclosingSourceType(),
162 // classLiteralSyntheticField);
163 // codeStream.invokeJavaLangClassDesiredAssertionStatus();
164 // Label falseLabel = new Label(codeStream);
165 // codeStream.ifne(falseLabel);
166 // codeStream.iconst_1();
167 // Label jumpLabel = new Label(codeStream);
168 // codeStream.goto_(jumpLabel);
169 // falseLabel.place();
170 // codeStream.iconst_0();
171 // jumpLabel.place();
172 // codeStream.putstatic(this.assertionSyntheticFieldBinding);
174 // // generate initializers
175 // if (declaringType.fields != null) {
176 // for (int i = 0, max = declaringType.fields.length; i < max; i++) {
177 // FieldDeclaration fieldDecl;
178 // if ((fieldDecl = declaringType.fields[i]).isStatic()) {
179 // fieldDecl.generateCode(staticInitializerScope, codeStream);
183 // if (codeStream.position == 0) {
184 // // do not need to output a Clinit if no bytecodes
185 // // so we reset the offset inside the byte array contents.
186 // classFile.contentsOffset = clinitOffset;
187 // // like we don't addd a method we need to undo the increment on the method count
188 // classFile.methodCount--;
189 // // reset the constant pool to its state before the clinit
190 // constantPool.resetForClinit(constantPoolIndex, constantPoolOffset);
192 // if (this.needFreeReturn) {
193 // int oldPosition = codeStream.position;
194 // codeStream.return_();
195 // codeStream.updateLocalVariablesAttribute(oldPosition);
197 // // Record the end of the clinit: point to the declaration of the class
198 // codeStream.recordPositionsFrom(0, declaringType.sourceStart);
199 // classFile.completeCodeAttributeForClinit(codeAttributeOffset);
203 public boolean isClinit() {
208 public boolean isInitializationMethod() {
213 public boolean isStatic() {
218 public void parseStatements(UnitParser parser, CompilationUnitDeclaration unit) {
219 //the clinit is filled by hand ....
222 public void resolve(ClassScope scope) {
224 this.scope = new MethodScope(scope, scope.referenceContext, true);
227 public String toString(int tab) {
229 String s = ""; //$NON-NLS-1$
230 s = s + tabString(tab);
231 s = s + "<clinit>()"; //$NON-NLS-1$
232 s = s + toStringStatements(tab + 1);
236 public void traverse(
237 IAbstractSyntaxTreeVisitor visitor,
238 ClassScope classScope) {
240 visitor.visit(this, classScope);
241 visitor.endVisit(this, classScope);
245 public void addSupportForAssertion(FieldBinding assertionSyntheticFieldBinding) {
247 this.assertionSyntheticFieldBinding = assertionSyntheticFieldBinding;
249 // we need to add the field right now, because the field infos are generated before the methods
250 SourceTypeBinding sourceType =
251 this.scope.outerMostMethodScope().enclosingSourceType();
252 this.classLiteralSyntheticField =
253 sourceType.addSyntheticField(sourceType, scope);