Makefile fixup
[org.ibex.tool.git] / src / org / eclipse / jdt / internal / compiler / parser / RecoveredLocalVariable.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.compiler.parser;
12
13 /**
14  * Internal local variable structure for parsing recovery 
15  */
16 import org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference;
17 import org.eclipse.jdt.internal.compiler.ast.ASTNode;
18 import org.eclipse.jdt.internal.compiler.ast.Expression;
19 import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration;
20 import org.eclipse.jdt.internal.compiler.ast.Statement;
21
22 public class RecoveredLocalVariable extends RecoveredStatement {
23
24         public LocalDeclaration localDeclaration;
25         boolean alreadyCompletedLocalInitialization;
26 public RecoveredLocalVariable(LocalDeclaration localDeclaration, RecoveredElement parent, int bracketBalance){
27         super(localDeclaration, parent, bracketBalance);
28         this.localDeclaration = localDeclaration;
29         this.alreadyCompletedLocalInitialization = localDeclaration.initialization != null;
30 }
31 /*
32  * Record an expression statement if local variable is expecting an initialization expression. 
33  */
34 public RecoveredElement add(Statement stmt, int bracketBalanceValue) {
35
36         if (this.alreadyCompletedLocalInitialization || !(stmt instanceof Expression)) {
37                 return super.add(stmt, bracketBalanceValue);
38         } else {
39                 this.alreadyCompletedLocalInitialization = true;
40                 this.localDeclaration.initialization = (Expression)stmt;
41                 this.localDeclaration.declarationSourceEnd = stmt.sourceEnd;
42                 this.localDeclaration.declarationEnd = stmt.sourceEnd;
43                 return this;
44         }
45 }
46 /* 
47  * Answer the associated parsed structure
48  */
49 public ASTNode parseTree(){
50         return localDeclaration;
51 }
52 /*
53  * Answer the very source end of the corresponding parse node
54  */
55 public int sourceEnd(){
56         return this.localDeclaration.declarationSourceEnd;
57 }
58 public String toString(int tab) {
59         return tabString(tab) + "Recovered local variable:\n" + localDeclaration.print(tab + 1, new StringBuffer(10)); //$NON-NLS-1$
60 }
61 public Statement updatedStatement(){
62         return localDeclaration;
63 }
64 /*
65  * A closing brace got consumed, might have closed the current element,
66  * in which case both the currentElement is exited.
67  *
68  * Fields have no associated braces, thus if matches, then update parent.
69  */
70 public RecoveredElement updateOnClosingBrace(int braceStart, int braceEnd){
71         if (bracketBalance > 0){ // was an array initializer
72                 bracketBalance--;
73                 if (bracketBalance == 0) alreadyCompletedLocalInitialization = true;
74                 return this;
75         }
76         if (parent != null){
77                 return parent.updateOnClosingBrace(braceStart, braceEnd);
78         }
79         return this;
80 }
81 /*
82  * An opening brace got consumed, might be the expected opening one of the current element,
83  * in which case the bodyStart is updated.
84  */
85 public RecoveredElement updateOnOpeningBrace(int braceStart, int braceEnd){
86         if (localDeclaration.declarationSourceEnd == 0 
87                 && localDeclaration.type instanceof ArrayTypeReference
88                 && !alreadyCompletedLocalInitialization){
89                 bracketBalance++;
90                 return null; // no update is necessary  (array initializer)
91         }
92         // might be an array initializer
93         this.updateSourceEndIfNecessary(braceStart - 1, braceEnd - 1);  
94         return this.parent.updateOnOpeningBrace(braceStart, braceEnd);  
95 }
96 public void updateParseTree(){
97         this.updatedStatement();
98 }
99 /*
100  * Update the declarationSourceEnd of the corresponding parse node
101  */
102 public void updateSourceEndIfNecessary(int bodyStart, int bodyEnd){
103         if (this.localDeclaration.declarationSourceEnd == 0) {
104                 this.localDeclaration.declarationSourceEnd = bodyEnd;
105                 this.localDeclaration.declarationEnd = bodyEnd; 
106         }
107 }
108 }