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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.ast;
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;
19 public class Block extends Statement {
20 public Statement[] statements; // The array of statements found with this block
22 public int explicitDeclarations;
24 public BlockScope scope; // The number of explicit declaration , used to create scope
26 public static final Block None = new Block(0);
28 public Block(int explicitDeclarations) {
29 this.explicitDeclarations = explicitDeclarations;
32 public FlowInfo analyseCode(BlockScope currentScope,
33 FlowContext flowContext, FlowInfo flowInfo) {
36 if (statements == null)
38 boolean didAlreadyComplain = false;
39 for (int i = 0, max = statements.length; i < max; i++) {
41 if (!flowInfo.complainIfUnreachable(stat = statements[i], scope,
42 didAlreadyComplain)) {
43 flowInfo = stat.analyseCode(scope, flowContext, flowInfo);
45 didAlreadyComplain = true;
51 public static final Block EmptyWith(int sourceStart, int sourceEnd) {
53 // return an empty block which position is s and e
54 Block bk = new Block(0);
55 bk.sourceStart = sourceStart;
56 bk.sourceEnd = sourceEnd;
61 * Code generation for a block
63 // public void generateCode(BlockScope currentScope, CodeStream codeStream)
66 // if ((bits & IsReachableMASK) == 0) {
69 // int pc = codeStream.position;
70 // if (statements != null) {
71 // for (int i = 0, max = statements.length; i < max; i++) {
72 // statements[i].generateCode(scope, codeStream);
74 // } // for local variable debug attributes
75 // if (scope != currentScope) { // was really associated with its own scope
76 // codeStream.exitUserScope(scope);
78 // codeStream.recordPositionsFrom(pc, this.sourceStart);
80 public boolean isEmptyBlock() {
82 return statements == null;
85 public StringBuffer printBody(int indent, StringBuffer output) {
87 if (this.statements == null)
89 for (int i = 0; i < statements.length; i++) {
90 statements[i].printStatement(indent + 1, output);
96 public StringBuffer printStatement(int indent, StringBuffer output) {
98 printIndent(indent, output);
99 output.append("{\n"); //$NON-NLS-1$
100 printBody(indent, output);
101 return printIndent(indent, output).append('}');
104 public void resolve(BlockScope upperScope) {
106 if (statements != null) {
107 scope = explicitDeclarations == 0 ? upperScope : new BlockScope(
108 upperScope, explicitDeclarations);
109 int i = 0, length = statements.length;
111 statements[i++].resolve(scope);
115 public void resolveUsing(BlockScope givenScope) {
117 // this optimized resolve(...) is sent only on none empty blocks
119 if (statements != null) {
120 int i = 0, length = statements.length;
122 statements[i++].resolve(scope);
126 public String toString(int tab) {
128 String s = tabString(tab);
129 if (this.statements == null) {
130 s += "{\n"; //$NON-NLS-1$
132 s += "}"; //$NON-NLS-1$
135 s += "{\n"; //$NON-NLS-1$
136 s += this.toStringStatements(tab);
138 s += "}"; //$NON-NLS-1$
142 public String toStringStatements(int tab) {
144 if (this.statements == null)
145 return ""; //$NON-NLS-1$
146 StringBuffer buffer = new StringBuffer();
147 for (int i = 0; i < statements.length; i++) {
148 buffer.append(statements[i].toString(tab + 1));
149 if (statements[i] instanceof Block) {
150 buffer.append("\n"); //$NON-NLS-1$
152 buffer.append(";\n"); //$NON-NLS-1$
156 return buffer.toString();
159 public void traverse(ASTVisitor visitor, BlockScope blockScope) {
161 if (visitor.visit(this, blockScope)) {
162 if (statements != null) {
163 int statementLength = statements.length;
164 for (int i = 0; i < statementLength; i++)
165 statements[i].traverse(visitor, scope);
168 visitor.endVisit(this, blockScope);
172 * Dispatch the call on its last statement.
174 public void branchChainTo(Label label) {
175 if (this.statements != null) {
176 this.statements[statements.length - 1].branchChainTo(label);