Start the debugger through setting the url with "?DBGSESSID=1@clienthost:10001" in...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / 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.phpeclipse.internal.compiler.ast;
12
13 import net.sourceforge.phpdt.internal.compiler.IAbstractSyntaxTreeVisitor;
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
84         public void resolve(BlockScope upperScope) {
85
86                 if (statements != null) {
87                         scope =
88                                 explicitDeclarations == 0
89                                         ? upperScope
90                                         : new BlockScope(upperScope, explicitDeclarations);
91                         int i = 0, length = statements.length;
92                         while (i < length)
93                                 statements[i++].resolve(scope);
94                 }
95         }
96
97         public void resolveUsing(BlockScope givenScope) {
98
99                 // this optimized resolve(...) is sent only on none empty blocks
100                 scope = givenScope;
101                 if (statements != null) {
102                         int i = 0, length = statements.length;
103                         while (i < length)
104                                 statements[i++].resolve(scope);
105                 }
106         }
107
108         public String toString(int tab) {
109
110                 String s = tabString(tab);
111                 if (this.statements == null) {
112                         s += "{\n"; //$NON-NLS-1$
113                         s += tabString(tab);
114                         s += "}"; //$NON-NLS-1$
115                         return s;
116                 }
117                 s += "{\n"; //$NON-NLS-1$
118                 s += this.toStringStatements(tab);
119                 s += tabString(tab);
120                 s += "}"; //$NON-NLS-1$
121                 return s;
122         }
123
124         public String toStringStatements(int tab) {
125
126                 if (this.statements == null)
127                         return ""; //$NON-NLS-1$
128                 StringBuffer buffer = new StringBuffer();
129                 for (int i = 0; i < statements.length; i++) {
130                         buffer.append(statements[i].toString(tab + 1));
131                         if (statements[i] instanceof Block) {
132                                 buffer.append("\n"); //$NON-NLS-1$
133                         } else {
134                                 buffer.append(";\n"); //$NON-NLS-1$
135                         }
136                 };
137                 return buffer.toString();
138         }
139
140         public void traverse(
141                 IAbstractSyntaxTreeVisitor visitor,
142                 BlockScope blockScope) {
143
144                 if (visitor.visit(this, blockScope)) {
145                         if (statements != null) {
146                                 int statementLength = statements.length;
147                                 for (int i = 0; i < statementLength; i++)
148                                         statements[i].traverse(visitor, scope);
149                         }
150                 }
151                 visitor.endVisit(this, blockScope);
152         }
153         
154         /**
155          * Dispatch the call on its last statement.
156          */
157         public void branchChainTo(Label label) {
158                  if (this.statements != null) {
159                         this.statements[statements.length - 1].branchChainTo(label);
160                  }
161         }
162         
163 }