Save PHPPartitionScanner for refactoring
[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         public int lastFieldID;
24         public int bodyStart;
25         public Initializer(Block block, int modifiers) {
26                 this.block = block;
27                 this.modifiers = modifiers;
28
29                 declarationSourceStart = sourceStart = bodyStart = block.sourceStart;
30         }
31
32         public FlowInfo analyseCode(
33                 MethodScope currentScope,
34                 FlowContext flowContext,
35                 FlowInfo flowInfo) {
36
37                 return block.analyseCode(currentScope, flowContext, flowInfo);
38         }
39
40         /**
41          * Code generation for a non-static initializer: 
42          *    standard block code gen
43          *
44          * @param currentScope net.sourceforge.phpdt.internal.compiler.lookup.BlockScope
45          * @param codeStream net.sourceforge.phpdt.internal.compiler.codegen.CodeStream
46          */
47 //      public void generateCode(BlockScope currentScope, CodeStream codeStream) {
48 //
49 //              if ((bits & IsReachableMASK) == 0) {
50 //                      return;
51 //              }
52 //              int pc = codeStream.position;
53 //              block.generateCode(currentScope, codeStream);
54 //              codeStream.recordPositionsFrom(pc, this.sourceStart);
55 //      }
56
57         public boolean isField() {
58
59                 return false;
60         }
61
62         public boolean isStatic() {
63
64                 return (modifiers & AccStatic) != 0;
65         }
66
67         public void parseStatements(
68                 UnitParser parser,
69                 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().innerTypesCannotDeclareStaticInitializers(
85                                                 declaringType,
86                                                 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 }