1 /*******************************************************************************
2 * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v0.5
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v05.html
9 * IBM Corporation - initial API and implementation
10 ******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.ast;
13 import net.sourceforge.phpdt.internal.compiler.IAbstractSyntaxTreeVisitor;
14 import net.sourceforge.phpdt.internal.compiler.*;
15 import net.sourceforge.phpdt.internal.compiler.codegen.*;
16 import net.sourceforge.phpdt.internal.compiler.flow.*;
17 import net.sourceforge.phpdt.internal.compiler.lookup.*;
18 import net.sourceforge.phpdt.internal.compiler.parser.*;
19 import net.sourceforge.phpdt.internal.compiler.problem.*;
21 public class Clinit extends AbstractMethodDeclaration {
23 public final static char[] ConstantPoolName = "<clinit>".toCharArray(); //$NON-NLS-1$
25 private FieldBinding assertionSyntheticFieldBinding = null;
26 private FieldBinding classLiteralSyntheticField = null;
28 public Clinit(CompilationResult compilationResult) {
29 super(compilationResult);
31 selector = ConstantPoolName;
34 public void analyseCode(
35 ClassScope classScope,
36 InitializationFlowContext staticInitializerFlowContext,
39 if (ignoreFurtherInvestigation)
42 ExceptionHandlingFlowContext clinitContext =
43 new ExceptionHandlingFlowContext(
44 staticInitializerFlowContext.parent,
50 // check for missing returning path
52 !((flowInfo == FlowInfo.DeadEnd) || flowInfo.isFakeReachable());
54 // check missing blank final field initializations
55 flowInfo = flowInfo.mergedWith(staticInitializerFlowContext.initsOnReturn);
56 FieldBinding[] fields = scope.enclosingSourceType().fields();
57 for (int i = 0, count = fields.length; i < count; i++) {
59 if ((field = fields[i]).isStatic()
61 && (!flowInfo.isDefinitelyAssigned(fields[i]))) {
62 scope.problemReporter().uninitializedBlankFinalField(
64 scope.referenceType().declarationOf(field));
65 // can complain against the field decl, since only one <clinit>
68 // check static initializers thrown exceptions
69 staticInitializerFlowContext.checkInitializerExceptions(
73 } catch (AbortMethod e) {
74 this.ignoreFurtherInvestigation = true;
79 * Bytecode generation for a <clinit> method
81 * @param classScope org.eclipse.jdt.internal.compiler.lookup.ClassScope
82 * @param classFile org.eclipse.jdt.internal.compiler.codegen.ClassFile
84 public void generateCode(ClassScope classScope, ClassFile classFile) {
87 if (ignoreFurtherInvestigation) {
88 // should never have to add any <clinit> problem method
92 clinitOffset = classFile.contentsOffset;
93 this.generateCode(classScope, classFile, clinitOffset);
94 } catch (AbortMethod e) {
96 // the clinit referenceContext is the type declaration
97 // All clinit problems will be reported against the type: AbortType instead of AbortMethod
98 // reset the contentsOffset to the value before generating the clinit code
99 // decrement the number of method info as well.
100 // This is done in the addProblemMethod and addProblemConstructor for other
102 if (e.compilationResult == CodeStream.RESTART_IN_WIDE_MODE) {
103 // a branch target required a goto_w, restart code gen in wide mode.
105 if (statements != null) {
106 for (int i = 0, max = statements.length; i < max; i++)
107 statements[i].resetStateForCodeGeneration();
109 classFile.contentsOffset = clinitOffset;
110 classFile.methodCount--;
111 classFile.codeStream.wideMode = true; // request wide mode
112 this.generateCode(classScope, classFile, clinitOffset);
113 // restart method generation
114 } catch (AbortMethod e2) {
115 classFile.contentsOffset = clinitOffset;
116 classFile.methodCount--;
119 // produce a problem method accounting for this fatal error
120 classFile.contentsOffset = clinitOffset;
121 classFile.methodCount--;
127 * Bytecode generation for a <clinit> method
129 * @param classScope org.eclipse.jdt.internal.compiler.lookup.ClassScope
130 * @param classFile org.eclipse.jdt.internal.compiler.codegen.ClassFile
132 private void generateCode(
133 ClassScope classScope,
137 ConstantPool constantPool = classFile.constantPool;
138 int constantPoolOffset = constantPool.currentOffset;
139 int constantPoolIndex = constantPool.currentIndex;
140 classFile.generateMethodInfoHeaderForClinit();
141 int codeAttributeOffset = classFile.contentsOffset;
142 classFile.generateCodeAttributeHeader();
143 CodeStream codeStream = classFile.codeStream;
144 this.resolve(classScope);
146 codeStream.reset(this, classFile);
147 TypeDeclaration declaringType = classScope.referenceContext;
149 // initialize local positions - including initializer scope.
150 scope.computeLocalVariablePositions(0, codeStream); // should not be necessary
151 MethodScope staticInitializerScope = declaringType.staticInitializerScope;
152 staticInitializerScope.computeLocalVariablePositions(0, codeStream);
153 // offset by the argument size
156 // This has to be done before any other initialization
157 if (this.assertionSyntheticFieldBinding != null) {
158 // generate code related to the activation of assertion for this class
159 codeStream.generateClassLiteralAccessForType(
160 classScope.enclosingSourceType(),
161 classLiteralSyntheticField);
162 codeStream.invokeJavaLangClassDesiredAssertionStatus();
163 Label falseLabel = new Label(codeStream);
164 codeStream.ifne(falseLabel);
165 codeStream.iconst_1();
166 Label jumpLabel = new Label(codeStream);
167 codeStream.goto_(jumpLabel);
169 codeStream.iconst_0();
171 codeStream.putstatic(this.assertionSyntheticFieldBinding);
173 // generate initializers
174 if (declaringType.fields != null) {
175 for (int i = 0, max = declaringType.fields.length; i < max; i++) {
176 FieldDeclaration fieldDecl;
177 if ((fieldDecl = declaringType.fields[i]).isStatic()) {
178 fieldDecl.generateCode(staticInitializerScope, codeStream);
182 if (codeStream.position == 0) {
183 // do not need to output a Clinit if no bytecodes
184 // so we reset the offset inside the byte array contents.
185 classFile.contentsOffset = clinitOffset;
186 // like we don't addd a method we need to undo the increment on the method count
187 classFile.methodCount--;
188 // reset the constant pool to its state before the clinit
189 constantPool.resetForClinit(constantPoolIndex, constantPoolOffset);
191 if (needFreeReturn) {
192 int oldPosition = codeStream.position;
193 codeStream.return_();
194 codeStream.updateLocalVariablesAttribute(oldPosition);
196 // Record the end of the clinit: point to the declaration of the class
197 codeStream.recordPositionsFrom(0, declaringType.sourceStart);
198 classFile.completeCodeAttributeForClinit(codeAttributeOffset);
202 public boolean isClinit() {
207 public boolean isInitializationMethod() {
212 public boolean isStatic() {
217 public void parseStatements(Parser parser, CompilationUnitDeclaration unit) {
218 //the clinit is filled by hand ....
221 public void resolve(ClassScope scope) {
223 this.scope = new MethodScope(scope, scope.referenceContext, true);
226 public String toString(int tab) {
228 String s = ""; //$NON-NLS-1$
229 s = s + tabString(tab);
230 s = s + "<clinit>()"; //$NON-NLS-1$
231 s = s + toStringStatements(tab + 1);
235 public void traverse(
236 IAbstractSyntaxTreeVisitor visitor,
237 ClassScope classScope) {
239 visitor.visit(this, classScope);
240 visitor.endVisit(this, classScope);
244 public void addSupportForAssertion(FieldBinding assertionSyntheticFieldBinding) {
246 this.assertionSyntheticFieldBinding = assertionSyntheticFieldBinding;
248 // we need to add the field right now, because the field infos are generated before the methods
249 SourceTypeBinding sourceType =
250 this.scope.outerMostMethodScope().enclosingSourceType();
251 this.classLiteralSyntheticField =
252 sourceType.addSyntheticField(sourceType, scope);