1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 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 org.eclipse.jdt.internal.compiler.ast;
13 import org.eclipse.jdt.internal.compiler.ASTVisitor;
14 import org.eclipse.jdt.internal.compiler.codegen.*;
15 import org.eclipse.jdt.internal.compiler.flow.*;
16 import org.eclipse.jdt.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;
25 public Block(int explicitDeclarations) {
26 this.explicitDeclarations = explicitDeclarations;
29 public FlowInfo analyseCode(
30 BlockScope currentScope,
31 FlowContext flowContext,
35 if (statements == null) return flowInfo;
36 boolean didAlreadyComplain = false;
37 for (int i = 0, max = statements.length; i < max; i++) {
38 Statement stat = statements[i];
39 if (!stat.complainIfUnreachable(flowInfo, scope, didAlreadyComplain)) {
40 flowInfo = stat.analyseCode(scope, flowContext, flowInfo);
42 didAlreadyComplain = true;
48 * Code generation for a block
50 public void generateCode(BlockScope currentScope, CodeStream codeStream) {
52 if ((bits & IsReachableMASK) == 0) {
55 int pc = codeStream.position;
56 if (statements != null) {
57 for (int i = 0, max = statements.length; i < max; i++) {
58 statements[i].generateCode(scope, codeStream);
60 } // for local variable debug attributes
61 if (scope != currentScope) { // was really associated with its own scope
62 codeStream.exitUserScope(scope);
64 codeStream.recordPositionsFrom(pc, this.sourceStart);
67 public boolean isEmptyBlock() {
69 return statements == null;
72 public StringBuffer printBody(int indent, StringBuffer output) {
74 if (this.statements == null) return output;
75 for (int i = 0; i < statements.length; i++) {
76 statements[i].printStatement(indent + 1, output);
82 public StringBuffer printStatement(int indent, StringBuffer output) {
84 printIndent(indent, output);
85 output.append("{\n"); //$NON-NLS-1$
86 printBody(indent, output);
87 return printIndent(indent, output).append('}');
90 public void resolve(BlockScope upperScope) {
92 if ((this.bits & UndocumentedEmptyBlockMASK) != 0) {
93 upperScope.problemReporter().undocumentedEmptyBlock(this.sourceStart, this.sourceEnd);
95 if (statements != null) {
97 explicitDeclarations == 0
99 : new BlockScope(upperScope, explicitDeclarations);
100 for (int i = 0, length = statements.length; i < length; i++) {
101 statements[i].resolve(scope);
106 public void resolveUsing(BlockScope givenScope) {
108 if ((this.bits & UndocumentedEmptyBlockMASK) != 0) {
109 givenScope.problemReporter().undocumentedEmptyBlock(this.sourceStart, this.sourceEnd);
111 // this optimized resolve(...) is sent only on none empty blocks
113 if (statements != null) {
114 for (int i = 0, length = statements.length; i < length; i++) {
115 statements[i].resolve(scope);
120 public void traverse(
122 BlockScope blockScope) {
124 if (visitor.visit(this, blockScope)) {
125 if (statements != null) {
126 for (int i = 0, length = statements.length; i < length; i++)
127 statements[i].traverse(visitor, scope);
130 visitor.endVisit(this, blockScope);
134 * Dispatch the call on its last statement.
136 public void branchChainTo(Label label) {
137 if (this.statements != null) {
138 this.statements[statements.length - 1].branchChainTo(label);