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.CodeStream;
15 import net.sourceforge.phpdt.internal.compiler.flow.FlowContext;
16 import net.sourceforge.phpdt.internal.compiler.flow.FlowInfo;
17 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
18 import net.sourceforge.phpdt.internal.compiler.lookup.MethodScope;
19 import net.sourceforge.phpdt.internal.compiler.lookup.ReferenceBinding;
20 import net.sourceforge.phpdt.internal.compiler.parser.Parser;
22 public class Initializer extends FieldDeclaration {
25 public int lastFieldID;
27 public Initializer(Block block, int modifiers) {
29 this.modifiers = modifiers;
31 declarationSourceStart = sourceStart = block.sourceStart;
34 public FlowInfo analyseCode(
35 MethodScope currentScope,
36 FlowContext flowContext,
39 return block.analyseCode(currentScope, flowContext, flowInfo);
43 * Code generation for a non-static initializer.
44 * i.e. normal block code gen
46 * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
47 * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
49 public void generateCode(BlockScope currentScope, CodeStream codeStream) {
51 if ((bits & IsReachableMASK) == 0) {
54 int pc = codeStream.position;
55 block.generateCode(currentScope, codeStream);
56 codeStream.recordPositionsFrom(pc, this.sourceStart);
59 public boolean isField() {
64 public boolean isStatic() {
66 return (modifiers & AccStatic) != 0;
69 public void parseStatements(
72 CompilationUnitDeclaration unit) {
74 //fill up the method body with statement
75 parser.parse(this, type, unit);
78 public void resolve(MethodScope scope) {
80 int previous = scope.fieldDeclarationIndex;
82 scope.fieldDeclarationIndex = lastFieldID;
84 ReferenceBinding declaringType = scope.enclosingSourceType();
85 if (declaringType.isNestedType() && !declaringType.isStatic())
86 scope.problemReporter().innerTypesCannotDeclareStaticInitializers(
92 scope.fieldDeclarationIndex = previous;
96 public String toString(int tab) {
99 StringBuffer buffer = new StringBuffer();
100 buffer.append(tabString(tab));
101 buffer.append(modifiersString(modifiers));
102 buffer.append("{\n"); //$NON-NLS-1$
103 buffer.append(block.toStringStatements(tab));
104 buffer.append(tabString(tab));
105 buffer.append("}"); //$NON-NLS-1$
106 return buffer.toString();
108 return block.toString(tab);
112 public void traverse(IAbstractSyntaxTreeVisitor visitor, MethodScope scope) {
114 if (visitor.visit(this, scope)) {
115 block.traverse(visitor, scope);
117 visitor.visit(this, scope);