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
9 * IBM Corporation - initial API and implementation
10 ******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.ast;
13 import net.sourceforge.phpdt.internal.compiler.IAbstractSyntaxTreeVisitor;
14 import net.sourceforge.phpdt.internal.compiler.codegen.CodeStream;
15 import net.sourceforge.phpdt.internal.compiler.codegen.Label;
16 import net.sourceforge.phpdt.internal.compiler.flow.FlowContext;
17 import net.sourceforge.phpdt.internal.compiler.flow.FlowInfo;
18 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
20 public class Block extends Statement {
22 public Statement[] statements;
23 public int explicitDeclarations;
24 // the number of explicit declaration , used to create scope
25 public BlockScope scope;
26 public static final Block None = new Block(0);
28 public Block(int explicitDeclarations) {
29 this.explicitDeclarations = explicitDeclarations;
32 public FlowInfo analyseCode(
33 BlockScope currentScope,
34 FlowContext flowContext,
38 if (statements == null)
40 for (int i = 0, max = statements.length; i < max; i++) {
42 if (!flowInfo.complainIfUnreachable((stat = statements[i]), scope)) {
43 flowInfo = stat.analyseCode(scope, flowContext, flowInfo);
49 public static final Block EmptyWith(int sourceStart, int sourceEnd) {
51 //return an empty block which position is s and e
52 Block bk = new Block(0);
53 bk.sourceStart = sourceStart;
54 bk.sourceEnd = sourceEnd;
59 * Code generation for a block
61 public void generateCode(BlockScope currentScope, CodeStream codeStream) {
63 if ((bits & IsReachableMASK) == 0) {
66 int pc = codeStream.position;
67 if (statements != null) {
68 for (int i = 0, max = statements.length; i < max; i++) {
69 statements[i].generateCode(scope, codeStream);
71 } // for local variable debug attributes
72 if (scope != currentScope) { // was really associated with its own scope
73 codeStream.exitUserScope(scope);
75 codeStream.recordPositionsFrom(pc, this.sourceStart);
78 public boolean isEmptyBlock() {
80 return statements == null;
83 public void resolve(BlockScope upperScope) {
85 if (statements != null) {
87 explicitDeclarations == 0
89 : new BlockScope(upperScope, explicitDeclarations);
90 int i = 0, length = statements.length;
92 statements[i++].resolve(scope);
96 public void resolveUsing(BlockScope givenScope) {
98 // this optimized resolve(...) is sent only on none empty blocks
100 if (statements != null) {
101 int i = 0, length = statements.length;
103 statements[i++].resolve(scope);
107 public String toString(int tab) {
109 String s = tabString(tab);
110 if (this.statements == null) {
111 s += "{\n"; //$NON-NLS-1$
113 s += "}"; //$NON-NLS-1$
116 s += "{\n"; //$NON-NLS-1$
117 s += this.toStringStatements(tab);
119 s += "}"; //$NON-NLS-1$
123 public String toStringStatements(int tab) {
125 if (this.statements == null)
126 return ""; //$NON-NLS-1$
127 StringBuffer buffer = new StringBuffer();
128 for (int i = 0; i < statements.length; i++) {
129 buffer.append(statements[i].toString(tab + 1));
130 if (statements[i] instanceof Block) {
131 buffer.append("\n"); //$NON-NLS-1$
133 buffer.append(";\n"); //$NON-NLS-1$
136 return buffer.toString();
139 public void traverse(
140 IAbstractSyntaxTreeVisitor visitor,
141 BlockScope blockScope) {
143 if (visitor.visit(this, blockScope)) {
144 if (statements != null) {
145 int statementLength = statements.length;
146 for (int i = 0; i < statementLength; i++)
147 statements[i].traverse(visitor, scope);
150 visitor.endVisit(this, blockScope);
154 * Dispatch the call on its last statement.
156 public void branchChainTo(Label label) {
157 if (this.statements != null) {
158 this.statements[statements.length - 1].branchChainTo(label);