1
/*******************************************************************************
2
* Copyright (c) 2004, 2011 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
9
* John Camelon (IBM) - Initial API and implementation
10
* Emanuel Graf IFS - Bug 198269
11
* Markus Schorn (Wind River Systems)
12
*******************************************************************************/
13
package org.eclipse.cdt.internal.core.dom.parser.cpp;
15
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
16
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
17
import org.eclipse.cdt.core.dom.ast.IASTExpression;
18
import org.eclipse.cdt.core.dom.ast.IASTNode;
19
import org.eclipse.cdt.core.dom.ast.IASTStatement;
20
import org.eclipse.cdt.core.dom.ast.IScope;
21
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTForStatement;
22
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
23
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguityParent;
26
* For statement in C++
28
public class CPPASTForStatement extends ASTNode implements ICPPASTForStatement, IASTAmbiguityParent {
29
private IScope scope = null;
31
private IASTStatement init;
32
private IASTExpression condition;
33
private IASTDeclaration condDeclaration;
34
private IASTExpression iterationExpression;
35
private IASTStatement body;
37
public CPPASTForStatement() {
40
public CPPASTForStatement(IASTStatement init, IASTDeclaration condDeclaration,
41
IASTExpression iterationExpression, IASTStatement body) {
42
setInitializerStatement(init);
43
setConditionDeclaration(condDeclaration);
44
setIterationExpression(iterationExpression);
48
public CPPASTForStatement(IASTStatement init, IASTExpression condition,
49
IASTExpression iterationExpression, IASTStatement body) {
50
setInitializerStatement(init);
51
setConditionExpression(condition);
52
setIterationExpression(iterationExpression);
56
public CPPASTForStatement copy() {
57
return copy(CopyStyle.withoutLocations);
60
public CPPASTForStatement copy(CopyStyle style) {
61
CPPASTForStatement copy = new CPPASTForStatement();
62
copy.setInitializerStatement(init == null ? null : init.copy(style));
63
copy.setConditionDeclaration(condDeclaration == null ? null : condDeclaration.copy(style));
64
copy.setConditionExpression(condition == null ? null : condition.copy(style));
65
copy.setIterationExpression(iterationExpression == null ?
66
null : iterationExpression.copy(style));
67
copy.setBody(body == null ? null : body.copy(style));
68
copy.setOffsetAndLength(this);
69
if (style == CopyStyle.withLocations) {
70
copy.setCopyLocation(this);
75
public IASTExpression getConditionExpression() {
79
public void setConditionExpression(IASTExpression condition) {
81
this.condition = condition;
82
if (condition != null) {
83
condition.setParent(this);
84
condition.setPropertyInParent(CONDITION);
85
condDeclaration= null;
89
public IASTExpression getIterationExpression() {
90
return iterationExpression;
93
public void setIterationExpression(IASTExpression iterator) {
95
this.iterationExpression = iterator;
96
if (iterator != null) {
97
iterator.setParent(this);
98
iterator.setPropertyInParent(ITERATION);
102
public IASTStatement getBody() {
106
public void setBody(IASTStatement statement) {
109
if (statement != null) {
110
statement.setParent(this);
111
statement.setPropertyInParent(BODY);
115
public IScope getScope() {
117
scope = new CPPBlockScope(this);
122
public boolean accept(ASTVisitor action) {
123
if (action.shouldVisitStatements) {
124
switch (action.visit(this)) {
125
case ASTVisitor.PROCESS_ABORT: return false;
126
case ASTVisitor.PROCESS_SKIP: return true;
130
if (init != null && !init.accept(action)) return false;
131
if (condition != null && !condition.accept(action)) return false;
132
if (condDeclaration != null && !condDeclaration.accept(action)) return false;
133
if (iterationExpression != null && !iterationExpression.accept(action)) return false;
134
if (body != null && !body.accept(action)) return false;
136
if (action.shouldVisitStatements) {
137
switch (action.leave(this)) {
138
case ASTVisitor.PROCESS_ABORT: return false;
139
case ASTVisitor.PROCESS_SKIP: return true;
146
public void replace(IASTNode child, IASTNode other) {
148
other.setPropertyInParent(child.getPropertyInParent());
149
other.setParent(child.getParent());
150
body = (IASTStatement) other;
151
} else if (child == condition || child == condDeclaration) {
152
if (other instanceof IASTExpression) {
153
setConditionExpression((IASTExpression) other);
154
} else if (other instanceof IASTDeclaration) {
155
setConditionDeclaration((IASTDeclaration) other);
157
} else if (child == iterationExpression) {
158
other.setPropertyInParent(child.getPropertyInParent());
159
other.setParent(child.getParent());
160
iterationExpression = (IASTExpression) other;
161
} else if (child == init) {
162
other.setPropertyInParent(child.getPropertyInParent());
163
other.setParent(child.getParent());
164
init = (IASTStatement) other;
168
public IASTStatement getInitializerStatement() {
172
public void setInitializerStatement(IASTStatement statement) {
175
if (statement != null) {
176
statement.setParent(this);
177
statement.setPropertyInParent(INITIALIZER);
181
public void setConditionDeclaration(IASTDeclaration d) {
186
d.setPropertyInParent(CONDITION_DECLARATION);
191
public IASTDeclaration getConditionDeclaration() {
192
return condDeclaration;