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.codegen.*;
 
  15 import net.sourceforge.phpdt.internal.compiler.flow.*;
 
  16 import net.sourceforge.phpdt.internal.compiler.lookup.*;
 
  17 import net.sourceforge.phpdt.internal.compiler.parser.*;
 
  19 public class Initializer extends FieldDeclaration {
 
  22         public int lastFieldID;
 
  24         public Initializer(Block block, int modifiers) {
 
  26                 this.modifiers = modifiers;
 
  28                 declarationSourceStart = sourceStart = block.sourceStart;
 
  31         public FlowInfo analyseCode(
 
  32                 MethodScope currentScope,
 
  33                 FlowContext flowContext,
 
  36                 return block.analyseCode(currentScope, flowContext, flowInfo);
 
  40          * Code generation for a non-static initializer.
 
  41          *      i.e. normal block code gen
 
  43          * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
 
  44          * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
 
  46         public void generateCode(BlockScope currentScope, CodeStream codeStream) {
 
  48                 if ((bits & IsReachableMASK) == 0) {
 
  51                 int pc = codeStream.position;
 
  52                 block.generateCode(currentScope, codeStream);
 
  53                 codeStream.recordPositionsFrom(pc, this.sourceStart);
 
  56         public boolean isField() {
 
  61         public boolean isStatic() {
 
  63                 return (modifiers & AccStatic) != 0;
 
  66         public void parseStatements(
 
  69                 CompilationUnitDeclaration unit) {
 
  71                 //fill up the method body with statement
 
  72                 parser.parse(this, type, unit);
 
  75         public void resolve(MethodScope scope) {
 
  77                 int previous = scope.fieldDeclarationIndex;
 
  79                         scope.fieldDeclarationIndex = lastFieldID;
 
  81                                 ReferenceBinding declaringType = scope.enclosingSourceType();
 
  82                                 if (declaringType.isNestedType() && !declaringType.isStatic())
 
  83                                         scope.problemReporter().innerTypesCannotDeclareStaticInitializers(
 
  89                         scope.fieldDeclarationIndex = previous;
 
  93         public String toString(int tab) {
 
  96                         StringBuffer buffer = new StringBuffer();
 
  97                         buffer.append(tabString(tab));
 
  98                         buffer.append(modifiersString(modifiers));
 
  99                         buffer.append("{\n"); //$NON-NLS-1$
 
 100                         buffer.append(block.toStringStatements(tab));
 
 101                         buffer.append(tabString(tab));
 
 102                         buffer.append("}"); //$NON-NLS-1$
 
 103                         return buffer.toString();
 
 105                         return block.toString(tab);
 
 109         public void traverse(IAbstractSyntaxTreeVisitor visitor, MethodScope scope) {
 
 111                 if (visitor.visit(this, scope)) {
 
 112                         block.traverse(visitor, scope);
 
 114                 visitor.visit(this, scope);