*** empty log message ***
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / Initializer.java
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
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.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;
21
22 public class Initializer extends FieldDeclaration {
23         
24         public Block block;
25         public int lastFieldID;
26         public int bodyStart;
27         public Initializer(Block block, int modifiers) {
28                 this.block = block;
29                 this.modifiers = modifiers;
30
31                 declarationSourceStart = sourceStart = block.sourceStart;
32         }
33
34         public FlowInfo analyseCode(
35                 MethodScope currentScope,
36                 FlowContext flowContext,
37                 FlowInfo flowInfo) {
38
39                 return block.analyseCode(currentScope, flowContext, flowInfo);
40         }
41
42         /**
43          * Code generation for a non-static initializer.
44          *      i.e. normal block code gen
45          *
46          * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
47          * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
48          */
49         public void generateCode(BlockScope currentScope, CodeStream codeStream) {
50
51                 if ((bits & IsReachableMASK) == 0) {
52                         return;
53                 }
54                 int pc = codeStream.position;
55                 block.generateCode(currentScope, codeStream);
56                 codeStream.recordPositionsFrom(pc, this.sourceStart);
57         }
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(
70                 Parser parser,
71                 TypeDeclaration type,
72                 CompilationUnitDeclaration unit) {
73
74                 //fill up the method body with statement
75                 parser.parse(this, type, unit);
76         }
77
78         public void resolve(MethodScope scope) {
79
80                 int previous = scope.fieldDeclarationIndex;
81                 try {
82                         scope.fieldDeclarationIndex = lastFieldID;
83                         if (isStatic()) {
84                                 ReferenceBinding declaringType = scope.enclosingSourceType();
85                                 if (declaringType.isNestedType() && !declaringType.isStatic())
86                                         scope.problemReporter().innerTypesCannotDeclareStaticInitializers(
87                                                 declaringType,
88                                                 this);
89                         }
90                         block.resolve(scope);
91                 } finally {
92                         scope.fieldDeclarationIndex = previous;
93                 }
94         }
95
96         public String toString(int tab) {
97
98                 if (modifiers != 0) {
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();
107                 } else {
108                         return block.toString(tab);
109                 }
110         }
111
112         public void traverse(IAbstractSyntaxTreeVisitor visitor, MethodScope scope) {
113
114                 if (visitor.visit(this, scope)) {
115                         block.traverse(visitor, scope);
116                 }
117                 visitor.visit(this, scope);
118         }
119 }