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.phpeclipse.internal.compiler.ast;
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;
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;
84 public void resolve(BlockScope upperScope) {
86 if (statements != null) {
88 explicitDeclarations == 0
90 : new BlockScope(upperScope, explicitDeclarations);
91 int i = 0, length = statements.length;
93 statements[i++].resolve(scope);
97 public void resolveUsing(BlockScope givenScope) {
99 // this optimized resolve(...) is sent only on none empty blocks
101 if (statements != null) {
102 int i = 0, length = statements.length;
104 statements[i++].resolve(scope);
108 public String toString(int tab) {
110 String s = tabString(tab);
111 if (this.statements == null) {
112 s += "{\n"; //$NON-NLS-1$
114 s += "}"; //$NON-NLS-1$
117 s += "{\n"; //$NON-NLS-1$
118 s += this.toStringStatements(tab);
120 s += "}"; //$NON-NLS-1$
124 public String toStringStatements(int tab) {
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$
134 buffer.append(";\n"); //$NON-NLS-1$
137 return buffer.toString();
140 public void traverse(
141 IAbstractSyntaxTreeVisitor visitor,
142 BlockScope blockScope) {
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);
151 visitor.endVisit(this, blockScope);
155 * Dispatch the call on its last statement.
157 public void branchChainTo(Label label) {
158 if (this.statements != null) {
159 this.statements[statements.length - 1].branchChainTo(label);