improved PHP parser
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / 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.phpeclipse.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         private FieldBinding classLiteralSyntheticField = null;
31
32         public Clinit(CompilationResult compilationResult) {
33                 super(compilationResult);
34                 modifiers = 0;
35                 selector = ConstantPoolName;
36         }
37
38         public void analyseCode(
39                 ClassScope classScope,
40                 InitializationFlowContext staticInitializerFlowContext,
41                 FlowInfo flowInfo) {
42
43                 if (ignoreFurtherInvestigation)
44                         return;
45                 try {
46                         ExceptionHandlingFlowContext clinitContext =
47                                 new ExceptionHandlingFlowContext(
48                                         staticInitializerFlowContext.parent,
49                                         this,
50                                         NoExceptions,
51                                         scope,
52                                         FlowInfo.DEAD_END);
53
54                         // check for missing returning path
55                         this.needFreeReturn = flowInfo.isReachable();
56
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++) {
61                                 FieldBinding field;
62                                 if ((field = fields[i]).isStatic()
63                                         && field.isFinal()
64                                         && (!flowInfo.isDefinitelyAssigned(fields[i]))) {
65                                         scope.problemReporter().uninitializedBlankFinalField(
66                                                 field,
67                                                 scope.referenceType().declarationOf(field));
68                                         // can complain against the field decl, since only one <clinit>
69                                 }
70                         }
71                         // check static initializers thrown exceptions
72                         staticInitializerFlowContext.checkInitializerExceptions(
73                                 scope,
74                                 clinitContext,
75                                 flowInfo);
76                 } catch (AbortMethod e) {
77                         this.ignoreFurtherInvestigation = true;
78                 }
79         }
80
81         /**
82          * Bytecode generation for a <clinit> method
83          *
84          * @param classScope net.sourceforge.phpdt.internal.compiler.lookup.ClassScope
85          * @param classFile net.sourceforge.phpdt.internal.compiler.codegen.ClassFile
86          */
87 //      public void generateCode(ClassScope classScope, ClassFile classFile) {
88 //
89 //              int clinitOffset = 0;
90 //              if (ignoreFurtherInvestigation) {
91 //                      // should never have to add any <clinit> problem method
92 //                      return;
93 //              }
94 //              try {
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
104 //                      // cases.
105 //                      if (e.compilationResult == CodeStream.RESTART_IN_WIDE_MODE) {
106 //                              // a branch target required a goto_w, restart code gen in wide mode.
107 //                              try {
108 //                                      if (statements != null) {
109 //                                              for (int i = 0, max = statements.length; i < max; i++)
110 //                                                      statements[i].resetStateForCodeGeneration();
111 //                                      }
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--;
120 //                              }
121 //                      } else {
122 //                              // produce a problem method accounting for this fatal error
123 //                              classFile.contentsOffset = clinitOffset;
124 //                              classFile.methodCount--;
125 //                      }
126 //              }
127 //      }
128
129         /**
130          * Bytecode generation for a <clinit> method
131          *
132          * @param classScope net.sourceforge.phpdt.internal.compiler.lookup.ClassScope
133          * @param classFile 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 = declaringType.staticInitializerScope;
154 //              staticInitializerScope.computeLocalVariablePositions(0, codeStream);
155 //
156 //              // 1.4 feature
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);
173 //              }
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);
180 //                              }
181 //                      }
182 //              }
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);
191 //              } else {
192 //                      if (this.needFreeReturn) {
193 //                              int oldPosition = codeStream.position;
194 //                              codeStream.return_();
195 //                              codeStream.updateLocalVariablesAttribute(oldPosition);
196 //                      }
197 //                      // Record the end of the clinit: point to the declaration of the class
198 //                      codeStream.recordPositionsFrom(0, declaringType.sourceStart);
199 //                      classFile.completeCodeAttributeForClinit(codeAttributeOffset);
200 //              }
201 //      }
202
203         public boolean isClinit() {
204
205                 return true;
206         }
207
208         public boolean isInitializationMethod() {
209
210                 return true;
211         }
212
213         public boolean isStatic() {
214
215                 return true;
216         }
217
218         public void parseStatements(UnitParser parser, CompilationUnitDeclaration unit) {
219                 //the clinit is filled by hand .... 
220         }
221         public StringBuffer print(int tab, StringBuffer output) {
222
223                 printIndent(tab, output).append("<clinit>()"); //$NON-NLS-1$
224                 printBody(tab + 1, output);
225                 return output;
226         }
227         public void resolve(ClassScope scope) {
228
229                 this.scope = new MethodScope(scope, scope.referenceContext, true);
230         }
231
232         public String toString(int tab) {
233
234                 String s = ""; //$NON-NLS-1$
235                 s = s + tabString(tab);
236                 s = s + "<clinit>()"; //$NON-NLS-1$
237                 s = s + toStringStatements(tab + 1);
238                 return s;
239         }
240
241         public void traverse(
242                 IAbstractSyntaxTreeVisitor visitor,
243                 ClassScope classScope) {
244
245                 visitor.visit(this, classScope);
246                 visitor.endVisit(this, classScope);
247         }
248
249         // 1.4 feature
250         public void addSupportForAssertion(FieldBinding assertionSyntheticFieldBinding) {
251
252                 this.assertionSyntheticFieldBinding = assertionSyntheticFieldBinding;
253
254                 // we need to add the field right now, because the field infos are generated before the methods
255                 SourceTypeBinding sourceType =
256                         this.scope.outerMostMethodScope().enclosingSourceType();
257                 this.classLiteralSyntheticField =
258                         sourceType.addSyntheticField(sourceType, scope);
259         }
260
261 }