mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Bug 327223: [C++0x] Range-based for loop.
This commit is contained in:
parent
befb4d9e04
commit
7129cfccb0
13 changed files with 354 additions and 80 deletions
|
@ -9254,4 +9254,13 @@ public class AST2CPPTests extends AST2BaseTest {
|
|||
public void testCVQualifiedClassName_328063() throws Exception {
|
||||
parseAndCheckBindings();
|
||||
}
|
||||
|
||||
// void test() {
|
||||
// int array[5] = { 1, 2, 3, 4, 5 };
|
||||
// for (int& x : array)
|
||||
// x *= 2;
|
||||
// }
|
||||
public void testRangeBasedForLoop_327223() throws Exception {
|
||||
parseAndCheckBindings();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,73 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2010 Wind River Systems, Inc. and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* Markus Schorn - initial API and implementation
|
||||
*******************************************************************************/
|
||||
|
||||
package org.eclipse.cdt.core.dom.ast.cpp;
|
||||
|
||||
import org.eclipse.cdt.core.dom.ast.ASTNodeProperty;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTInitializerClause;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.IScope;
|
||||
|
||||
/**
|
||||
* Represents a range-based for loop.
|
||||
*
|
||||
* @noextend This interface is not intended to be extended by clients.
|
||||
* @noimplement This interface is not intended to be implemented by clients.
|
||||
* @since 5.3
|
||||
*/
|
||||
public interface ICPPASTRangeBasedForStatement extends IASTStatement {
|
||||
public static final ASTNodeProperty DECLARATION = new ASTNodeProperty(
|
||||
"ICPPASTRangeBasedForStatement.DECLARATION [IASTDeclaration]"); //$NON-NLS-1$
|
||||
public static final ASTNodeProperty INITIALIZER = new ASTNodeProperty(
|
||||
"ICPPASTRangeBasedForStatement.INITIALIZER [IASTInitializerClause]"); //$NON-NLS-1$
|
||||
public static final ASTNodeProperty BODY = new ASTNodeProperty(
|
||||
"ICPPASTRangeBasedForStatement.BODY [IASTStatement]"); //$NON-NLS-1$
|
||||
|
||||
|
||||
/**
|
||||
* Returns the for-range-declaration
|
||||
*/
|
||||
IASTDeclaration getDeclaration();
|
||||
|
||||
/**
|
||||
* Returns the for-range-initializer.
|
||||
*/
|
||||
IASTInitializerClause getInitializerClause();
|
||||
|
||||
/**
|
||||
* Returns the statement of this for-loop.
|
||||
*/
|
||||
IASTStatement getBody();
|
||||
|
||||
/**
|
||||
* Returns the scope defined by this for-loop.
|
||||
*/
|
||||
public IScope getScope();
|
||||
|
||||
public ICPPASTRangeBasedForStatement copy();
|
||||
|
||||
|
||||
/**
|
||||
* Not allowed on frozen AST.
|
||||
*/
|
||||
void setDeclaration(IASTDeclaration decl);
|
||||
|
||||
/**
|
||||
* Not allowed on frozen AST.
|
||||
*/
|
||||
void setInitializerClause(IASTInitializerClause statement);
|
||||
|
||||
/**
|
||||
* Not allowed on frozen AST.
|
||||
*/
|
||||
public void setBody(IASTStatement statement);
|
||||
}
|
|
@ -214,6 +214,12 @@ public interface ICPPNodeFactory extends INodeFactory {
|
|||
|
||||
public ICPPASTQualifiedName newQualifiedName();
|
||||
|
||||
/**
|
||||
* Creates a range based for statement.
|
||||
* @since 5.3
|
||||
*/
|
||||
public ICPPASTRangeBasedForStatement newRangeBasedForStatement();
|
||||
|
||||
/**
|
||||
* @deprecated Replaced by {@link #newReferenceOperator(boolean)}.
|
||||
*/
|
||||
|
|
|
@ -28,6 +28,7 @@ public class DeclarationOptions {
|
|||
final public static int REQUIRE_SIMPLE_NAME= 0x800;
|
||||
final public static int ALLOW_FOLLOWED_BY_BRACE= 0x1000;
|
||||
final public static int ALLOW_OPAQUE_ENUM= 0x2000;
|
||||
final public static int SINGLE_DTOR= 0x4000;
|
||||
|
||||
public static final DeclarationOptions
|
||||
GLOBAL= new DeclarationOptions(ALLOW_EMPTY_SPECIFIER | ALLOW_OPAQUE_ENUM),
|
||||
|
@ -42,7 +43,8 @@ public class DeclarationOptions {
|
|||
TYPEID_CONVERSION= new DeclarationOptions(REQUIRE_ABSTRACT | NO_INITIALIZER | NO_FUNCTIONS | NO_NESTED),
|
||||
EXCEPTION= new DeclarationOptions(ALLOW_ABSTRACT | NO_INITIALIZER),
|
||||
CONDITION= new DeclarationOptions(NO_CTOR_STYLE_INITIALIZER),
|
||||
C_PARAMETER_NON_ABSTRACT= new DeclarationOptions(ALLOW_ABSTRACT | ALLOW_EMPTY_SPECIFIER);
|
||||
C_PARAMETER_NON_ABSTRACT= new DeclarationOptions(ALLOW_ABSTRACT | ALLOW_EMPTY_SPECIFIER),
|
||||
RANGE_BASED_FOR = new DeclarationOptions(NO_INITIALIZER | REQUIRE_SIMPLE_NAME | SINGLE_DTOR);
|
||||
|
||||
final public boolean fAllowEmptySpecifier;
|
||||
final public boolean fAllowAbstract;
|
||||
|
@ -57,6 +59,7 @@ public class DeclarationOptions {
|
|||
final public boolean fAllowParameterPacks;
|
||||
final public boolean fRequireSimpleName;
|
||||
final public boolean fAllowOpaqueEnum;
|
||||
final public boolean fSingleDtor;
|
||||
|
||||
public DeclarationOptions(int options) {
|
||||
fAllowEmptySpecifier= (options & ALLOW_EMPTY_SPECIFIER) != 0;
|
||||
|
@ -72,5 +75,6 @@ public class DeclarationOptions {
|
|||
fRequireSimpleName= (options & REQUIRE_SIMPLE_NAME) != 0;
|
||||
fCanBeFollowedByBrace= fAllowBracedInitializer || (options & ALLOW_FOLLOWED_BY_BRACE) != 0;
|
||||
fAllowOpaqueEnum= (options & ALLOW_OPAQUE_ENUM) != 0;
|
||||
fSingleDtor= (options & SINGLE_DTOR) != 0;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,6 +41,7 @@ import org.eclipse.cdt.core.dom.ast.IBinding;
|
|||
import org.eclipse.cdt.core.dom.ast.IFunctionType;
|
||||
import org.eclipse.cdt.core.dom.ast.IType;
|
||||
import org.eclipse.cdt.core.dom.ast.IVariable;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTRangeBasedForStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.gnu.IGNUASTCompoundStatementExpression;
|
||||
import org.eclipse.cdt.internal.core.pdom.dom.PDOMName;
|
||||
|
||||
|
@ -197,11 +198,14 @@ public abstract class VariableReadWriteFlags {
|
|||
if (parent instanceof IGNUASTCompoundStatementExpression) {
|
||||
return rwAnyNode(parent, indirection);
|
||||
}
|
||||
}
|
||||
else if (stmt instanceof IASTForStatement) {
|
||||
} else if (stmt instanceof IASTForStatement) {
|
||||
if (node.getPropertyInParent() == IASTForStatement.CONDITION) {
|
||||
return READ;
|
||||
}
|
||||
} else if (stmt instanceof ICPPASTRangeBasedForStatement) {
|
||||
if (node.getPropertyInParent() == ICPPASTRangeBasedForStatement.INITIALIZER) {
|
||||
return READ;
|
||||
}
|
||||
}
|
||||
else if (stmt instanceof IASTIfStatement) {
|
||||
if (node.getPropertyInParent() == IASTIfStatement.CONDITION) {
|
||||
|
|
|
@ -0,0 +1,119 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2010 Wind River Systems, Inc. and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* Markus Schorn - Initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.internal.core.dom.parser.cpp;
|
||||
|
||||
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTInitializerClause;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTNode;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.IScope;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTRangeBasedForStatement;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguityParent;
|
||||
|
||||
/**
|
||||
* Range based for loop in c++.
|
||||
*/
|
||||
public class CPPASTRangeBasedForStatement extends ASTNode implements ICPPASTRangeBasedForStatement, IASTAmbiguityParent {
|
||||
private IScope fScope;
|
||||
private IASTDeclaration fDeclaration;
|
||||
private IASTInitializerClause fInitClause;
|
||||
private IASTStatement fBody;
|
||||
|
||||
public CPPASTRangeBasedForStatement() {
|
||||
}
|
||||
|
||||
public CPPASTRangeBasedForStatement copy() {
|
||||
CPPASTRangeBasedForStatement copy = new CPPASTRangeBasedForStatement();
|
||||
copy.setDeclaration(fDeclaration == null ? null : fDeclaration.copy());
|
||||
copy.setInitializerClause(fInitClause == null ? null : fInitClause.copy());
|
||||
copy.setBody(fBody == null ? null : fBody.copy());
|
||||
copy.setOffsetAndLength(this);
|
||||
return copy;
|
||||
}
|
||||
|
||||
public IASTDeclaration getDeclaration() {
|
||||
return fDeclaration;
|
||||
}
|
||||
|
||||
public void setDeclaration(IASTDeclaration declaration) {
|
||||
assertNotFrozen();
|
||||
this.fDeclaration = declaration;
|
||||
if (declaration != null) {
|
||||
declaration.setParent(this);
|
||||
declaration.setPropertyInParent(DECLARATION);
|
||||
}
|
||||
}
|
||||
|
||||
public IASTInitializerClause getInitializerClause() {
|
||||
return fInitClause;
|
||||
}
|
||||
|
||||
public void setInitializerClause(IASTInitializerClause initClause) {
|
||||
assertNotFrozen();
|
||||
fInitClause = initClause;
|
||||
if (initClause != null) {
|
||||
initClause.setParent(this);
|
||||
initClause.setPropertyInParent(INITIALIZER);
|
||||
}
|
||||
}
|
||||
|
||||
public IASTStatement getBody() {
|
||||
return fBody;
|
||||
}
|
||||
|
||||
public void setBody(IASTStatement statement) {
|
||||
assertNotFrozen();
|
||||
fBody = statement;
|
||||
if (statement != null) {
|
||||
statement.setParent(this);
|
||||
statement.setPropertyInParent(BODY);
|
||||
}
|
||||
}
|
||||
|
||||
public IScope getScope() {
|
||||
if (fScope == null)
|
||||
fScope = new CPPBlockScope(this);
|
||||
return fScope;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean accept( ASTVisitor action ){
|
||||
if (action.shouldVisitStatements) {
|
||||
switch (action.visit(this)) {
|
||||
case ASTVisitor.PROCESS_ABORT : return false;
|
||||
case ASTVisitor.PROCESS_SKIP : return true;
|
||||
default : break;
|
||||
}
|
||||
}
|
||||
if (fDeclaration != null && !fDeclaration.accept(action))
|
||||
return false;
|
||||
if (fInitClause != null && !fInitClause.accept(action))
|
||||
return false;
|
||||
if (fBody != null && !fBody.accept(action))
|
||||
return false;
|
||||
|
||||
if (action.shouldVisitStatements && action.leave(this) == ASTVisitor.PROCESS_ABORT)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void replace(IASTNode child, IASTNode other) {
|
||||
if (child == fDeclaration) {
|
||||
setDeclaration((IASTDeclaration) other);
|
||||
} else if (child == fInitClause) {
|
||||
setInitializerClause((IASTInitializerClause) other);
|
||||
} else if (child == fBody) {
|
||||
setBody((IASTStatement) other);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -86,6 +86,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTPackExpansionExpression;
|
|||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTParameterDeclaration;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTPointerToMember;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTRangeBasedForStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTReferenceOperator;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleDeclSpecifier;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleTypeConstructorExpression;
|
||||
|
@ -458,6 +459,10 @@ public class CPPNodeFactory extends NodeFactory implements ICPPNodeFactory {
|
|||
return new CPPASTQualifiedName();
|
||||
}
|
||||
|
||||
public ICPPASTRangeBasedForStatement newRangeBasedForStatement() {
|
||||
return new CPPASTRangeBasedForStatement();
|
||||
}
|
||||
|
||||
public ICPPASTReferenceOperator newReferenceOperator() {
|
||||
return new CPPASTReferenceOperator(false);
|
||||
}
|
||||
|
|
|
@ -34,6 +34,7 @@ import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
|
|||
import org.eclipse.cdt.core.dom.ast.IASTElaboratedTypeSpecifier;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTEqualsInitializer;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTExpression;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTForStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTFunctionCallExpression;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
|
||||
|
@ -95,6 +96,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTOperatorName;
|
|||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTPackExpandable;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTParameterDeclaration;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTRangeBasedForStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTReferenceOperator;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleDeclSpecifier;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleTypeConstructorExpression;
|
||||
|
@ -2036,19 +2038,21 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
|
|||
IASTDeclarator[] declarators= IASTDeclarator.EMPTY_DECLARATOR_ARRAY;
|
||||
if (dtor != null) {
|
||||
declarators= new IASTDeclarator[]{dtor};
|
||||
while (LTcatchEOF(1) == IToken.tCOMMA) {
|
||||
consume();
|
||||
try {
|
||||
dtor= initDeclarator(declSpec, declOption);
|
||||
} catch (FoundAggregateInitializer e) {
|
||||
// scalability: don't keep references to tokens, initializer may be large
|
||||
declarationMark= null;
|
||||
markBeforDtor= null;
|
||||
dtor= addInitializer(e, declOption);
|
||||
if (!declOption.fSingleDtor) {
|
||||
while (LTcatchEOF(1) == IToken.tCOMMA) {
|
||||
consume();
|
||||
try {
|
||||
dtor= initDeclarator(declSpec, declOption);
|
||||
} catch (FoundAggregateInitializer e) {
|
||||
// scalability: don't keep references to tokens, initializer may be large
|
||||
declarationMark= null;
|
||||
markBeforDtor= null;
|
||||
dtor= addInitializer(e, declOption);
|
||||
}
|
||||
declarators = (IASTDeclarator[]) ArrayUtil.append(IASTDeclarator.class, declarators, dtor);
|
||||
}
|
||||
declarators = (IASTDeclarator[]) ArrayUtil.append(IASTDeclarator.class, declarators, dtor);
|
||||
declarators = (IASTDeclarator[]) ArrayUtil.removeNulls(IASTDeclarator.class, declarators);
|
||||
}
|
||||
declarators = (IASTDeclarator[]) ArrayUtil.removeNulls(IASTDeclarator.class, declarators);
|
||||
}
|
||||
|
||||
final int lt1= LTcatchEOF(1);
|
||||
|
@ -2059,8 +2063,13 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
|
|||
case IToken.tSEMI:
|
||||
endOffset= consume().getEndOffset();
|
||||
break;
|
||||
case IToken.t_try:
|
||||
case IToken.tCOLON:
|
||||
if (declOption == DeclarationOptions.RANGE_BASED_FOR) {
|
||||
endOffset= figureEndOffset(declSpec, declarators);
|
||||
break;
|
||||
}
|
||||
//$FALL-THROUGH$
|
||||
case IToken.t_try:
|
||||
case IToken.tLBRACE:
|
||||
case IToken.tASSIGN: // defaulted or deleted function definition
|
||||
if (declarators.length != 1)
|
||||
|
@ -4232,70 +4241,85 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
|
|||
}
|
||||
|
||||
protected IASTStatement parseForStatement() throws EndOfFileException, BacktrackException {
|
||||
int startOffset;
|
||||
startOffset = consume().getOffset();
|
||||
final int offset= consume(IToken.t_for).getOffset();
|
||||
consume(IToken.tLPAREN);
|
||||
IASTStatement init = forInitStatement();
|
||||
IASTNode for_condition = null;
|
||||
switch (LT(1)) {
|
||||
case IToken.tSEMI:
|
||||
case IToken.tEOC:
|
||||
break;
|
||||
default:
|
||||
for_condition = cppStyleCondition(IToken.tSEMI);
|
||||
IToken mark= mark();
|
||||
IASTStatement forStmt;
|
||||
try {
|
||||
forStmt= startRangeBasedForLoop();
|
||||
} catch (BacktrackException e) {
|
||||
backup(mark);
|
||||
forStmt= startTraditionalForLoop();
|
||||
}
|
||||
switch (LT(1)) {
|
||||
case IToken.tSEMI:
|
||||
consume();
|
||||
break;
|
||||
case IToken.tEOC:
|
||||
break;
|
||||
default:
|
||||
throw backtrack;
|
||||
}
|
||||
IASTExpression iterationExpression = null;
|
||||
switch (LT(1)) {
|
||||
case IToken.tRPAREN:
|
||||
case IToken.tEOC:
|
||||
break;
|
||||
default:
|
||||
iterationExpression = expression();
|
||||
}
|
||||
switch (LT(1)) {
|
||||
case IToken.tRPAREN:
|
||||
consume();
|
||||
break;
|
||||
case IToken.tEOC:
|
||||
break;
|
||||
default:
|
||||
throw backtrack;
|
||||
}
|
||||
ICPPASTForStatement for_statement = nodeFactory.newForStatement();
|
||||
IASTStatement for_body = null;
|
||||
mark= null;
|
||||
int endOffset= consumeOrEOC(IToken.tRPAREN).getEndOffset();
|
||||
|
||||
if (LT(1) != IToken.tEOC) {
|
||||
for_body = statement();
|
||||
((ASTNode) for_statement).setOffsetAndLength(startOffset, calculateEndOffset(for_body) - startOffset);
|
||||
IASTStatement body = statement();
|
||||
if (forStmt instanceof ICPPASTRangeBasedForStatement) {
|
||||
((ICPPASTRangeBasedForStatement) forStmt).setBody(body);
|
||||
} else {
|
||||
((IASTForStatement) forStmt).setBody(body);
|
||||
}
|
||||
endOffset= calculateEndOffset(body);
|
||||
}
|
||||
|
||||
for_statement.setInitializerStatement(init);
|
||||
|
||||
if (for_condition != null) {
|
||||
if (for_condition instanceof IASTExpression) {
|
||||
for_statement.setConditionExpression((IASTExpression) for_condition);
|
||||
} else if (for_condition instanceof IASTDeclaration) {
|
||||
for_statement.setConditionDeclaration((IASTDeclaration) for_condition);
|
||||
}
|
||||
}
|
||||
if (iterationExpression != null) {
|
||||
for_statement.setIterationExpression(iterationExpression);
|
||||
}
|
||||
if (for_body != null) {
|
||||
for_statement.setBody(for_body);
|
||||
}
|
||||
return for_statement;
|
||||
return setRange(forStmt, offset, endOffset);
|
||||
}
|
||||
|
||||
@Override
|
||||
// Look for "for-range-declaration : for-range-initializer"
|
||||
// for-range-declaration:
|
||||
// attribute-specifier? type-specifier-seq declarator
|
||||
// for-range-initializer:
|
||||
// expression
|
||||
// braced-init-list
|
||||
private ICPPASTRangeBasedForStatement startRangeBasedForLoop() throws EndOfFileException, BacktrackException {
|
||||
IASTDeclaration decl= simpleDeclaration(DeclarationOptions.RANGE_BASED_FOR);
|
||||
consume(IToken.tCOLON);
|
||||
IASTInitializerClause init= null;
|
||||
switch (LT(1)) {
|
||||
case IToken.tEOC:
|
||||
break;
|
||||
case IToken.tLBRACE:
|
||||
init= bracedInitList(false);
|
||||
break;
|
||||
default:
|
||||
init= expression();
|
||||
}
|
||||
|
||||
ICPPASTRangeBasedForStatement result = nodeFactory.newRangeBasedForStatement();
|
||||
result.setDeclaration(decl);
|
||||
result.setInitializerClause(init);
|
||||
return result;
|
||||
}
|
||||
|
||||
private IASTForStatement startTraditionalForLoop() throws BacktrackException, EndOfFileException {
|
||||
final IASTStatement initStmt = forInitStatement();
|
||||
IASTNode condition= null;
|
||||
IASTExpression iterExpr= null;
|
||||
|
||||
int lt1 = LT(1);
|
||||
if (lt1 != IToken.tSEMI && lt1 != IToken.tEOC) {
|
||||
condition = cppStyleCondition(IToken.tSEMI);
|
||||
}
|
||||
consumeOrEOC(IToken.tSEMI);
|
||||
|
||||
lt1 = LT(1);
|
||||
if (lt1 != IToken.tRPAREN && lt1 != IToken.tEOC) {
|
||||
iterExpr = expression();
|
||||
}
|
||||
|
||||
ICPPASTForStatement result = nodeFactory.newForStatement();
|
||||
result.setInitializerStatement(initStmt);
|
||||
if (condition instanceof IASTExpression) {
|
||||
result.setConditionExpression((IASTExpression) condition);
|
||||
} else if (condition instanceof IASTDeclaration) {
|
||||
result.setConditionDeclaration((IASTDeclaration) condition);
|
||||
}
|
||||
result.setIterationExpression(iterExpr);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected IASTStatement parseReturnStatement() throws EndOfFileException, BacktrackException {
|
||||
final int offset= consume(IToken.t_return).getOffset(); // t_return
|
||||
|
||||
|
|
|
@ -109,6 +109,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceAlias;
|
|||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNewExpression;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTRangeBasedForStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleTypeConstructorExpression;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSwitchStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateDeclaration;
|
||||
|
@ -1379,6 +1380,10 @@ public class CPPSemantics {
|
|||
} else {
|
||||
nodes= new IASTNode[] {initDeclaration};
|
||||
}
|
||||
} else if (parent instanceof ICPPASTRangeBasedForStatement) {
|
||||
ICPPASTRangeBasedForStatement forStatement = (ICPPASTRangeBasedForStatement) parent;
|
||||
final IASTDeclaration decl = forStatement.getDeclaration();
|
||||
nodes= new IASTNode[] {decl};
|
||||
} else if (parent instanceof ICPPASTEnumerationSpecifier) {
|
||||
// The enumeration scope contains the enumeration items
|
||||
for (IASTEnumerator enumerator : ((ICPPASTEnumerationSpecifier) parent).getEnumerators()) {
|
||||
|
|
|
@ -111,6 +111,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNewExpression;
|
|||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTParameterDeclaration;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTPointerToMember;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTRangeBasedForStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTReferenceOperator;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleDeclSpecifier;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleTypeTemplateParameter;
|
||||
|
@ -842,6 +843,8 @@ public class CPPVisitor extends ASTQueries {
|
|||
return getContainingScope((IASTStatement) parent);
|
||||
} else if (parent instanceof IASTForStatement) {
|
||||
return ((IASTForStatement) parent).getScope();
|
||||
} else if (parent instanceof ICPPASTRangeBasedForStatement) {
|
||||
return ((ICPPASTRangeBasedForStatement) parent).getScope();
|
||||
} else if (parent instanceof IASTCompositeTypeSpecifier) {
|
||||
return ((IASTCompositeTypeSpecifier) parent).getScope();
|
||||
} else if (parent instanceof ICPPASTNamespaceDefinition) {
|
||||
|
@ -930,6 +933,8 @@ public class CPPVisitor extends ASTQueries {
|
|||
IASTNode parent = node.getParent();
|
||||
if (parent instanceof IASTForStatement) {
|
||||
return ((IASTForStatement) parent).getScope();
|
||||
} else if (parent instanceof ICPPASTRangeBasedForStatement) {
|
||||
return ((ICPPASTRangeBasedForStatement) parent).getScope();
|
||||
} else if (parent instanceof ICPPASTIfStatement) {
|
||||
return ((ICPPASTIfStatement) parent).getScope();
|
||||
} else if (parent instanceof ICPPASTSwitchStatement) {
|
||||
|
@ -1125,6 +1130,8 @@ public class CPPVisitor extends ASTQueries {
|
|||
scope = compound.getScope();
|
||||
} else if (parent instanceof IASTForStatement) {
|
||||
scope = ((IASTForStatement) parent).getScope();
|
||||
} else if (parent instanceof ICPPASTRangeBasedForStatement) {
|
||||
scope= ((ICPPASTRangeBasedForStatement) parent).getScope();
|
||||
} else if (parent instanceof ICPPASTSwitchStatement) {
|
||||
scope = ((ICPPASTSwitchStatement) parent).getScope();
|
||||
} else if (parent instanceof ICPPASTIfStatement) {
|
||||
|
@ -1164,6 +1171,8 @@ public class CPPVisitor extends ASTQueries {
|
|||
IASTNode p = parent.getParent();
|
||||
if (p instanceof IASTForStatement)
|
||||
return parent;
|
||||
if (p instanceof ICPPASTRangeBasedForStatement)
|
||||
return parent;
|
||||
if (p instanceof IASTStatement)
|
||||
return p;
|
||||
} else if (parent instanceof IASTStatement || parent instanceof IASTTranslationUnit) {
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
*******************************************************************************/
|
||||
package org.eclipse.cdt.internal.core.dom.rewrite.astwriter;
|
||||
|
||||
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTASMDeclaration;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
|
||||
|
@ -20,7 +21,6 @@ import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
|
|||
import org.eclipse.cdt.core.dom.ast.IASTProblemDeclaration;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCatchHandler;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTConstructorChainInitializer;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTExplicitTemplateInstantiation;
|
||||
|
@ -61,7 +61,7 @@ public class DeclarationWriter extends NodeWriter{
|
|||
private static final String USING = "using "; //$NON-NLS-1$
|
||||
private boolean printSemicolon;
|
||||
|
||||
public DeclarationWriter(Scribe scribe, CPPASTVisitor visitor, NodeCommentMap commentMap) {
|
||||
public DeclarationWriter(Scribe scribe, ASTVisitor visitor, NodeCommentMap commentMap) {
|
||||
super(scribe, visitor, commentMap);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik
|
||||
* Copyright (c) 2008, 2010 Institute for Software, HSR Hochschule fuer Technik
|
||||
* Rapperswil, University of applied sciences and others
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
|
@ -7,7 +7,7 @@
|
|||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* Institute for Software - initial API and implementation
|
||||
* Institute for Software - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.internal.core.dom.rewrite.astwriter;
|
||||
|
||||
|
@ -58,6 +58,7 @@ public class NodeWriter {
|
|||
protected static final String CLASS_SPACE = "class "; //$NON-NLS-1$
|
||||
protected static final String VAR_ARGS = "..."; //$NON-NLS-1$
|
||||
protected static final String COLON_COLON = "::"; //$NON-NLS-1$
|
||||
protected static final String COLON_SPACE = ": "; //$NON-NLS-1$
|
||||
|
||||
public NodeWriter(Scribe scribe, ASTVisitor visitor, NodeCommentMap commentMap) {
|
||||
super();
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik
|
||||
* Copyright (c) 2008, 2010 Institute for Software, HSR Hochschule fuer Technik
|
||||
* Rapperswil, University of applied sciences and others
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
|
@ -32,10 +32,10 @@ import org.eclipse.cdt.core.dom.ast.IASTReturnStatement;
|
|||
import org.eclipse.cdt.core.dom.ast.IASTStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTSwitchStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTWhileStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCatchHandler;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTForStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTIfStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTRangeBasedForStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSwitchStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTryBlockStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTWhileStatement;
|
||||
|
@ -77,7 +77,7 @@ public class StatementWriter extends NodeWriter{
|
|||
private boolean decrementIndentationLevelOneMore = false;
|
||||
private final DeclarationWriter declWriter;
|
||||
|
||||
public StatementWriter(Scribe scribe, CPPASTVisitor visitor, NodeCommentMap commentMap) {
|
||||
public StatementWriter(Scribe scribe, ASTVisitor visitor, NodeCommentMap commentMap) {
|
||||
super(scribe, visitor, commentMap);
|
||||
declWriter = new DeclarationWriter(scribe, visitor, commentMap);
|
||||
}
|
||||
|
@ -140,6 +140,9 @@ public class StatementWriter extends NodeWriter{
|
|||
} else if (statement instanceof IASTForStatement) {
|
||||
writeForStatement((IASTForStatement) statement);
|
||||
newLine = false;
|
||||
} else if (statement instanceof ICPPASTRangeBasedForStatement) {
|
||||
writeForStatement((ICPPASTRangeBasedForStatement) statement);
|
||||
newLine = false;
|
||||
} else if (statement instanceof IASTDoStatement) {
|
||||
writeDoStatement((IASTDoStatement) statement);
|
||||
newLine = true;
|
||||
|
@ -204,6 +207,18 @@ public class StatementWriter extends NodeWriter{
|
|||
writeBodyStatement(forStatment.getBody(), false);
|
||||
}
|
||||
|
||||
private void writeForStatement(ICPPASTRangeBasedForStatement forStatment) {
|
||||
scribe.noNewLines();
|
||||
scribe.print(FOR);
|
||||
writeDeclarationWithoutSemicolon(forStatment.getDeclaration());
|
||||
scribe.print(COLON_SPACE);
|
||||
visitNodeIfNotNull(forStatment.getInitializerClause());
|
||||
scribe.print(')');
|
||||
scribe.newLines();
|
||||
nextCompoundNoNewLine();
|
||||
writeBodyStatement(forStatment.getBody(), false);
|
||||
}
|
||||
|
||||
private void writeIfStatement(IASTIfStatement ifStatement) {
|
||||
scribe.print(IF);
|
||||
scribe.noNewLines();
|
||||
|
|
Loading…
Add table
Reference in a new issue