KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > ast > Block


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.compiler.ast;
12
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.*;
17
18 public class Block extends Statement {
19     
20     public Statement[] statements;
21     public int explicitDeclarations;
22     // the number of explicit declaration , used to create scope
23
public BlockScope scope;
24     
25     public Block(int explicitDeclarations) {
26         this.explicitDeclarations = explicitDeclarations;
27     }
28     
29     public FlowInfo analyseCode(
30         BlockScope currentScope,
31         FlowContext flowContext,
32         FlowInfo flowInfo) {
33
34         // empty block
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);
41             } else {
42                 didAlreadyComplain = true;
43             }
44         }
45         return flowInfo;
46     }
47     /**
48      * Code generation for a block
49      */

50     public void generateCode(BlockScope currentScope, CodeStream codeStream) {
51
52         if ((bits & IsReachable) == 0) {
53             return;
54         }
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);
59             }
60         } // for local variable debug attributes
61
if (scope != currentScope) { // was really associated with its own scope
62
codeStream.exitUserScope(scope);
63         }
64         codeStream.recordPositionsFrom(pc, this.sourceStart);
65     }
66
67     public boolean isEmptyBlock() {
68
69         return statements == null;
70     }
71
72     public StringBuffer JavaDoc printBody(int indent, StringBuffer JavaDoc output) {
73
74         if (this.statements == null) return output;
75         for (int i = 0; i < statements.length; i++) {
76             statements[i].printStatement(indent + 1, output);
77             output.append('\n');
78         }
79         return output;
80     }
81
82     public StringBuffer JavaDoc printStatement(int indent, StringBuffer JavaDoc output) {
83
84         printIndent(indent, output);
85         output.append("{\n"); //$NON-NLS-1$
86
printBody(indent, output);
87         return printIndent(indent, output).append('}');
88     }
89
90     public void resolve(BlockScope upperScope) {
91
92         if ((this.bits & UndocumentedEmptyBlock) != 0) {
93             upperScope.problemReporter().undocumentedEmptyBlock(this.sourceStart, this.sourceEnd);
94         }
95         if (statements != null) {
96             scope =
97                 explicitDeclarations == 0
98                     ? upperScope
99                     : new BlockScope(upperScope, explicitDeclarations);
100             for (int i = 0, length = statements.length; i < length; i++) {
101                 statements[i].resolve(scope);
102             }
103         }
104     }
105
106     public void resolveUsing(BlockScope givenScope) {
107
108         if ((this.bits & UndocumentedEmptyBlock) != 0) {
109             givenScope.problemReporter().undocumentedEmptyBlock(this.sourceStart, this.sourceEnd);
110         }
111         // this optimized resolve(...) is sent only on none empty blocks
112
scope = givenScope;
113         if (statements != null) {
114             for (int i = 0, length = statements.length; i < length; i++) {
115                 statements[i].resolve(scope);
116             }
117         }
118     }
119
120     public void traverse(
121         ASTVisitor visitor,
122         BlockScope blockScope) {
123
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);
128             }
129         }
130         visitor.endVisit(this, blockScope);
131     }
132     
133     /**
134      * Dispatch the call on its last statement.
135      */

136     public void branchChainTo(BranchLabel label) {
137          if (this.statements != null) {
138             this.statements[statements.length - 1].branchChainTo(label);
139          }
140     }
141     
142 }
143
Popular Tags