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.*;
15 import net.sourceforge.phpdt.internal.compiler.flow.*;
16 import net.sourceforge.phpdt.internal.compiler.lookup.*;
18 public class Block extends Statement {
20 public Statement[] statements;
21 public int explicitDeclarations;
22 // the number of explicit declaration , used to create scope
23 public BlockScope scope;
24 public static final Block None = new Block(0);
26 public Block(int explicitDeclarations) {
27 this.explicitDeclarations = explicitDeclarations;
30 public FlowInfo analyseCode(
31 BlockScope currentScope,
32 FlowContext flowContext,
36 if (statements == null)
38 for (int i = 0, max = statements.length; i < max; i++) {
40 if (!flowInfo.complainIfUnreachable((stat = statements[i]), scope)) {
41 flowInfo = stat.analyseCode(scope, flowContext, flowInfo);
47 public static final Block EmptyWith(int sourceStart, int sourceEnd) {
49 //return an empty block which position is s and e
50 Block bk = new Block(0);
51 bk.sourceStart = sourceStart;
52 bk.sourceEnd = sourceEnd;
57 * Code generation for a block
59 public void generateCode(BlockScope currentScope, CodeStream codeStream) {
61 if ((bits & IsReachableMASK) == 0) {
64 int pc = codeStream.position;
65 if (statements != null) {
66 for (int i = 0, max = statements.length; i < max; i++) {
67 statements[i].generateCode(scope, codeStream);
69 } // for local variable debug attributes
70 if (scope != currentScope) { // was really associated with its own scope
71 codeStream.exitUserScope(scope);
73 codeStream.recordPositionsFrom(pc, this.sourceStart);
76 public boolean isEmptyBlock() {
78 return statements == null;
81 public void resolve(BlockScope upperScope) {
83 if (statements != null) {
85 explicitDeclarations == 0
87 : new BlockScope(upperScope, explicitDeclarations);
88 int i = 0, length = statements.length;
90 statements[i++].resolve(scope);
94 public void resolveUsing(BlockScope givenScope) {
96 // this optimized resolve(...) is sent only on none empty blocks
98 if (statements != null) {
99 int i = 0, length = statements.length;
101 statements[i++].resolve(scope);
105 public String toString(int tab) {
107 String s = tabString(tab);
108 if (this.statements == null) {
109 s += "{\n"; //$NON-NLS-1$
111 s += "}"; //$NON-NLS-1$
114 s += "{\n"; //$NON-NLS-1$
115 s += this.toStringStatements(tab);
117 s += "}"; //$NON-NLS-1$
121 public String toStringStatements(int tab) {
123 if (this.statements == null)
124 return ""; //$NON-NLS-1$
125 StringBuffer buffer = new StringBuffer();
126 for (int i = 0; i < statements.length; i++) {
127 buffer.append(statements[i].toString(tab + 1));
128 if (statements[i] instanceof Block) {
129 buffer.append("\n"); //$NON-NLS-1$
131 buffer.append(";\n"); //$NON-NLS-1$
134 return buffer.toString();
137 public void traverse(
138 IAbstractSyntaxTreeVisitor visitor,
139 BlockScope blockScope) {
141 if (visitor.visit(this, blockScope)) {
142 if (statements != null) {
143 int statementLength = statements.length;
144 for (int i = 0; i < statementLength; i++)
145 statements[i].traverse(visitor, scope);
148 visitor.endVisit(this, blockScope);
152 * Dispatch the call on its last statement.
154 public void branchChainTo(Label label) {
155 if (this.statements != null) {
156 this.statements[statements.length - 1].branchChainTo(label);