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.phpdt.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;
31 private FieldBinding classLiteralSyntheticField = null;
33 public Clinit(CompilationResult compilationResult) {
34 super(compilationResult);
36 selector = ConstantPoolName;
39 public void analyseCode(ClassScope classScope,
40 InitializationFlowContext staticInitializerFlowContext,
43 if (ignoreFurtherInvestigation)
46 ExceptionHandlingFlowContext clinitContext = new ExceptionHandlingFlowContext(
47 staticInitializerFlowContext.parent, this, NoExceptions,
48 scope, FlowInfo.DEAD_END);
50 // check for missing returning path
51 this.needFreeReturn = flowInfo.isReachable();
53 // check missing blank final field initializations
55 .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() && 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
67 // check static initializers thrown exceptions
68 staticInitializerFlowContext.checkInitializerExceptions(scope,
69 clinitContext, flowInfo);
70 } catch (AbortMethod e) {
71 this.ignoreFurtherInvestigation = true;
76 * Bytecode generation for a <clinit> method
79 * net.sourceforge.phpdt.internal.compiler.lookup.ClassScope
81 * net.sourceforge.phpdt.internal.compiler.codegen.ClassFile
83 // public void generateCode(ClassScope classScope, ClassFile classFile) {
85 // int clinitOffset = 0;
86 // if (ignoreFurtherInvestigation) {
87 // // should never have to add any <clinit> problem method
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
100 // // decrement the number of method info as well.
101 // // This is done in the addProblemMethod and addProblemConstructor for
104 // if (e.compilationResult == CodeStream.RESTART_IN_WIDE_MODE) {
105 // // a branch target required a goto_w, restart code gen in wide mode.
107 // if (statements != null) {
108 // for (int i = 0, max = statements.length; i < max; i++)
109 // statements[i].resetStateForCodeGeneration();
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--;
121 // // produce a problem method accounting for this fatal error
122 // classFile.contentsOffset = clinitOffset;
123 // classFile.methodCount--;
128 * Bytecode generation for a <clinit> method
131 * net.sourceforge.phpdt.internal.compiler.lookup.ClassScope
133 * net.sourceforge.phpdt.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 =
154 // declaringType.staticInitializerScope;
155 // staticInitializerScope.computeLocalVariablePositions(0, codeStream);
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);
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);
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
190 // classFile.methodCount--;
191 // // reset the constant pool to its state before the clinit
192 // constantPool.resetForClinit(constantPoolIndex, constantPoolOffset);
194 // if (this.needFreeReturn) {
195 // int oldPosition = codeStream.position;
196 // codeStream.return_();
197 // codeStream.updateLocalVariablesAttribute(oldPosition);
199 // // Record the end of the clinit: point to the declaration of the class
200 // codeStream.recordPositionsFrom(0, declaringType.sourceStart);
201 // classFile.completeCodeAttributeForClinit(codeAttributeOffset);
204 public boolean isClinit() {
209 public boolean isInitializationMethod() {
214 public boolean isStatic() {
219 public void parseStatements(UnitParser parser,
220 CompilationUnitDeclaration unit) {
221 // the clinit is filled by hand ....
224 public StringBuffer print(int tab, StringBuffer output) {
226 printIndent(tab, output).append("<clinit>()"); //$NON-NLS-1$
227 printBody(tab + 1, output);
231 public void resolve(ClassScope scope) {
233 this.scope = new MethodScope(scope, scope.referenceContext, true);
236 public String toString(int tab) {
238 String s = ""; //$NON-NLS-1$
239 s = s + tabString(tab);
240 s = s + "<clinit>()"; //$NON-NLS-1$
241 s = s + toStringStatements(tab + 1);
245 public void traverse(IAbstractSyntaxTreeVisitor visitor,
246 ClassScope classScope) {
248 visitor.visit(this, classScope);
249 visitor.endVisit(this, classScope);
253 public void addSupportForAssertion(
254 FieldBinding assertionSyntheticFieldBinding) {
256 this.assertionSyntheticFieldBinding = assertionSyntheticFieldBinding;
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(