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