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 {
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);
27 public Block(int explicitDeclarations) {
28 this.explicitDeclarations = explicitDeclarations;
31 public FlowInfo analyseCode(
32 BlockScope currentScope,
33 FlowContext flowContext,
37 if (statements == null) return flowInfo;
38 boolean didAlreadyComplain = false;
39 for (int i = 0, max = statements.length; i < max; i++) {
41 if (!flowInfo.complainIfUnreachable(stat = statements[i], scope, didAlreadyComplain)) {
42 flowInfo = stat.analyseCode(scope, flowContext, flowInfo);
44 didAlreadyComplain = true;
50 public static final Block EmptyWith(int sourceStart, int sourceEnd) {
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;
60 * Code generation for a block
62 // public void generateCode(BlockScope currentScope, CodeStream codeStream) {
64 // if ((bits & IsReachableMASK) == 0) {
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);
72 // } // for local variable debug attributes
73 // if (scope != currentScope) { // was really associated with its own scope
74 // codeStream.exitUserScope(scope);
76 // codeStream.recordPositionsFrom(pc, this.sourceStart);
79 public boolean isEmptyBlock() {
81 return statements == null;
83 public StringBuffer printBody(int indent, StringBuffer output) {
85 if (this.statements == null) return output;
86 for (int i = 0; i < statements.length; i++) {
87 statements[i].printStatement(indent + 1, output);
93 public StringBuffer printStatement(int indent, StringBuffer output) {
95 printIndent(indent, output);
96 output.append("{\n"); //$NON-NLS-1$
97 printBody(indent, output);
98 return printIndent(indent, output).append('}');
100 public void resolve(BlockScope upperScope) {
102 if (statements != null) {
104 explicitDeclarations == 0
106 : new BlockScope(upperScope, explicitDeclarations);
107 int i = 0, length = statements.length;
109 statements[i++].resolve(scope);
113 public void resolveUsing(BlockScope givenScope) {
115 // this optimized resolve(...) is sent only on none empty blocks
117 if (statements != null) {
118 int i = 0, length = statements.length;
120 statements[i++].resolve(scope);
124 public String toString(int tab) {
126 String s = tabString(tab);
127 if (this.statements == null) {
128 s += "{\n"; //$NON-NLS-1$
130 s += "}"; //$NON-NLS-1$
133 s += "{\n"; //$NON-NLS-1$
134 s += this.toStringStatements(tab);
136 s += "}"; //$NON-NLS-1$
140 public String toStringStatements(int tab) {
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$
150 buffer.append(";\n"); //$NON-NLS-1$
153 return buffer.toString();
156 public void traverse(
158 BlockScope blockScope) {
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);
167 visitor.endVisit(this, blockScope);
171 * Dispatch the call on its last statement.
173 public void branchChainTo(Label label) {
174 if (this.statements != null) {
175 this.statements[statements.length - 1].branchChainTo(label);