Fix import
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / Initializer.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.phpdt.internal.compiler.ast;
12
13 import net.sourceforge.phpdt.internal.compiler.ASTVisitor;
14 import net.sourceforge.phpdt.internal.compiler.flow.FlowContext;
15 import net.sourceforge.phpdt.internal.compiler.flow.FlowInfo;
16 import net.sourceforge.phpdt.internal.compiler.lookup.MethodScope;
17 import net.sourceforge.phpdt.internal.compiler.lookup.ReferenceBinding;
18 import net.sourceforge.phpdt.internal.compiler.parser.UnitParser;
19
20 public class Initializer extends FieldDeclaration {
21
22         public Block block;
23
24         public int lastFieldID;
25
26         public int bodyStart;
27
28         public Initializer(Block block, int modifiers) {
29                 this.block = block;
30                 this.modifiers = modifiers;
31
32                 declarationSourceStart = sourceStart = bodyStart = block.sourceStart;
33         }
34
35         public FlowInfo analyseCode(MethodScope currentScope,
36                         FlowContext flowContext, FlowInfo flowInfo) {
37
38                 return block.analyseCode(currentScope, flowContext, flowInfo);
39         }
40
41         /**
42          * Code generation for a non-static initializer: standard block code gen
43          * 
44          * @param currentScope
45          *            net.sourceforge.phpdt.internal.compiler.lookup.BlockScope
46          * @param codeStream
47          *            net.sourceforge.phpdt.internal.compiler.codegen.CodeStream
48          */
49         // public void generateCode(BlockScope currentScope, CodeStream codeStream)
50         // {
51         //
52         // if ((bits & IsReachableMASK) == 0) {
53         // return;
54         // }
55         // int pc = codeStream.position;
56         // block.generateCode(currentScope, codeStream);
57         // codeStream.recordPositionsFrom(pc, this.sourceStart);
58         // }
59         public boolean isField() {
60
61                 return false;
62         }
63
64         public boolean isStatic() {
65
66                 return (modifiers & AccStatic) != 0;
67         }
68
69         public void parseStatements(UnitParser parser, TypeDeclaration type,
70                         CompilationUnitDeclaration unit) {
71
72                 // fill up the method body with statement
73                 parser.parse(this, type, unit);
74         }
75
76         public void resolve(MethodScope scope) {
77
78                 int previous = scope.fieldDeclarationIndex;
79                 try {
80                         scope.fieldDeclarationIndex = lastFieldID;
81                         if (isStatic()) {
82                                 ReferenceBinding declaringType = scope.enclosingSourceType();
83                                 if (declaringType.isNestedType() && !declaringType.isStatic())
84                                         scope.problemReporter()
85                                                         .innerTypesCannotDeclareStaticInitializers(
86                                                                         declaringType, this);
87                         }
88                         block.resolve(scope);
89                 } finally {
90                         scope.fieldDeclarationIndex = previous;
91                 }
92         }
93
94         public String toString(int tab) {
95
96                 if (modifiers != 0) {
97                         StringBuffer buffer = new StringBuffer();
98                         buffer.append(tabString(tab));
99                         buffer.append(modifiersString(modifiers));
100                         buffer.append("{\n"); //$NON-NLS-1$
101                         buffer.append(block.toStringStatements(tab));
102                         buffer.append(tabString(tab));
103                         buffer.append("}"); //$NON-NLS-1$
104                         return buffer.toString();
105                 } else {
106                         return block.toString(tab);
107                 }
108         }
109
110         public void traverse(ASTVisitor visitor, MethodScope scope) {
111
112                 if (visitor.visit(this, scope)) {
113                         block.traverse(visitor, scope);
114                 }
115                 visitor.endVisit(this, scope);
116         }
117 }