Quick fix for the Environment Vars.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / Block.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.codegen.Label;
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
19 public class Block extends Statement {
20         
21         public Statement[] statements;
22         public int explicitDeclarations;
23         // the number of explicit declaration , used to create scope
24         public BlockScope scope;
25         public static final Block None = new Block(0);
26         
27         public Block(int explicitDeclarations) {
28                 this.explicitDeclarations = explicitDeclarations;
29         }
30         
31         public FlowInfo analyseCode(
32                 BlockScope currentScope,
33                 FlowContext flowContext,
34                 FlowInfo flowInfo) {
35
36                 // empty block
37                 if (statements == null) return flowInfo;
38                 boolean didAlreadyComplain = false;
39                 for (int i = 0, max = statements.length; i < max; i++) {
40                         Statement stat;
41                         if (!flowInfo.complainIfUnreachable(stat = statements[i], scope, didAlreadyComplain)) {
42                                 flowInfo = stat.analyseCode(scope, flowContext, flowInfo);
43                         } else {
44                                 didAlreadyComplain = true;
45                         }
46                 }
47                 return flowInfo;
48         }
49
50         public static final Block EmptyWith(int sourceStart, int sourceEnd) {
51
52                 //return an empty block which position is s and e
53                 Block bk = new Block(0);
54                 bk.sourceStart = sourceStart;
55                 bk.sourceEnd = sourceEnd;
56                 return bk;
57         }
58
59         /**
60          * Code generation for a block
61          */
62 //      public void generateCode(BlockScope currentScope, CodeStream codeStream) {
63 //
64 //              if ((bits & IsReachableMASK) == 0) {
65 //                      return;
66 //              }
67 //              int pc = codeStream.position;
68 //              if (statements != null) {
69 //                      for (int i = 0, max = statements.length; i < max; i++) {
70 //                              statements[i].generateCode(scope, codeStream);
71 //                      }
72 //              } // for local variable debug attributes
73 //              if (scope != currentScope) { // was really associated with its own scope
74 //                      codeStream.exitUserScope(scope);
75 //              }
76 //              codeStream.recordPositionsFrom(pc, this.sourceStart);
77 //      }
78
79         public boolean isEmptyBlock() {
80
81                 return statements == null;
82         }
83         public StringBuffer printBody(int indent, StringBuffer output) {
84
85                 if (this.statements == null) return output;
86                 for (int i = 0; i < statements.length; i++) {
87                         statements[i].printStatement(indent + 1, output);
88                         output.append('\n'); 
89                 }
90                 return output;
91         }
92
93         public StringBuffer printStatement(int indent, StringBuffer output) {
94
95                 printIndent(indent, output);
96                 output.append("{\n"); //$NON-NLS-1$
97                 printBody(indent, output);
98                 return printIndent(indent, output).append('}');
99         }
100         public void resolve(BlockScope upperScope) {
101
102                 if (statements != null) {
103                         scope =
104                                 explicitDeclarations == 0
105                                         ? upperScope
106                                         : new BlockScope(upperScope, explicitDeclarations);
107                         int i = 0, length = statements.length;
108                         while (i < length)
109                                 statements[i++].resolve(scope);
110                 }
111         }
112
113         public void resolveUsing(BlockScope givenScope) {
114
115                 // this optimized resolve(...) is sent only on none empty blocks
116                 scope = givenScope;
117                 if (statements != null) {
118                         int i = 0, length = statements.length;
119                         while (i < length)
120                                 statements[i++].resolve(scope);
121                 }
122         }
123
124         public String toString(int tab) {
125
126                 String s = tabString(tab);
127                 if (this.statements == null) {
128                         s += "{\n"; //$NON-NLS-1$
129                         s += tabString(tab);
130                         s += "}"; //$NON-NLS-1$
131                         return s;
132                 }
133                 s += "{\n"; //$NON-NLS-1$
134                 s += this.toStringStatements(tab);
135                 s += tabString(tab);
136                 s += "}"; //$NON-NLS-1$
137                 return s;
138         }
139
140         public String toStringStatements(int tab) {
141
142                 if (this.statements == null)
143                         return ""; //$NON-NLS-1$
144                 StringBuffer buffer = new StringBuffer();
145                 for (int i = 0; i < statements.length; i++) {
146                         buffer.append(statements[i].toString(tab + 1));
147                         if (statements[i] instanceof Block) {
148                                 buffer.append("\n"); //$NON-NLS-1$
149                         } else {
150                                 buffer.append(";\n"); //$NON-NLS-1$
151                         }
152                 };
153                 return buffer.toString();
154         }
155
156         public void traverse(
157             ASTVisitor visitor,
158                 BlockScope blockScope) {
159
160                 if (visitor.visit(this, blockScope)) {
161                         if (statements != null) {
162                                 int statementLength = statements.length;
163                                 for (int i = 0; i < statementLength; i++)
164                                         statements[i].traverse(visitor, scope);
165                         }
166                 }
167                 visitor.endVisit(this, blockScope);
168         }
169         
170         /**
171          * Dispatch the call on its last statement.
172          */
173         public void branchChainTo(Label label) {
174                  if (this.statements != null) {
175                         this.statements[statements.length - 1].branchChainTo(label);
176                  }
177         }
178         
179 }