1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Added ICPPASTTryBlockStatement and implementation.

This commit is contained in:
John Camelon 2004-11-24 04:51:04 +00:00
parent 03de1e32fd
commit 922006b670
3 changed files with 173 additions and 5 deletions

View file

@ -0,0 +1,41 @@
/**********************************************************************
* Copyright (c) 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM - Initial API and implementation
**********************************************************************/
package org.eclipse.cdt.core.dom.ast.cpp;
import java.util.List;
import org.eclipse.cdt.core.dom.ast.ASTNodeProperty;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
/**
* @author jcamelon
*/
public interface ICPPASTTryBlockStatement extends IASTStatement {
public static final ASTNodeProperty BODY = new ASTNodeProperty( "Body"); //$NON-NLS-1$
/**
* @param tryBlock
*/
public void setTryBody(IASTStatement tryBlock);
public IASTStatement getTryBody();
public static final ASTNodeProperty CATCH_HANDLER = new ASTNodeProperty( "Catch Handler"); //$NON-NLS-1$
/**
* @param handler
*/
public void addCatchHandler(ICPPASTCatchHandler handler);
public List getCatchHandlers();
}

View file

@ -0,0 +1,90 @@
/**********************************************************************
* Copyright (c) 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM - Initial API and implementation
**********************************************************************/
package org.eclipse.cdt.internal.core.parser2.cpp;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCatchHandler;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTryBlockStatement;
/**
* @author jcamelon
*/
public class CPPASTTryBlockStatement extends CPPASTNode implements
ICPPASTTryBlockStatement {
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionTryBlockDeclarator#addCatchHandler(org.eclipse.cdt.core.dom.ast.IASTStatement)
*/
public void addCatchHandler(ICPPASTCatchHandler statement) {
if( catchHandlers == null )
{
catchHandlers = new IASTStatement[ DEFAULT_CATCH_HANDLER_LIST_SIZE ];
currentIndex = 0;
}
if( catchHandlers.length == currentIndex )
{
IASTStatement [] old = catchHandlers;
catchHandlers = new IASTStatement[ old.length * 2 ];
for( int i = 0; i < old.length; ++i )
catchHandlers[i] = old[i];
}
catchHandlers[ currentIndex++ ] = statement; }
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionTryBlockDeclarator#getCatchHandlers()
*/
public List getCatchHandlers() {
if( catchHandlers == null ) return Collections.EMPTY_LIST;
removeNullCatchHandlers();
return Arrays.asList( catchHandlers );
}
private void removeNullCatchHandlers() {
int nullCount = 0;
for( int i = 0; i < catchHandlers.length; ++i )
if( catchHandlers[i] == null )
++nullCount;
if( nullCount == 0 ) return;
IASTStatement [] old = catchHandlers;
int newSize = old.length - nullCount;
catchHandlers = new IASTStatement[ newSize ];
for( int i = 0; i < newSize; ++i )
catchHandlers[i] = old[i];
currentIndex = newSize;
}
private int currentIndex = 0;
private IASTStatement [] catchHandlers = null;
private static final int DEFAULT_CATCH_HANDLER_LIST_SIZE = 4;
private IASTStatement tryBody;
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTryBlockStatement#setTryBody(org.eclipse.cdt.core.dom.ast.IASTStatement)
*/
public void setTryBody(IASTStatement tryBlock) {
tryBody = tryBlock;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTryBlockStatement#getTryBody()
*/
public IASTStatement getTryBody() {
return tryBody;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTryBlockStatement#addCatchHandler(org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCatchHandler)
*/
}

View file

@ -98,6 +98,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateId;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateSpecialization;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplatedTypeTemplateParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTryBlockStatement;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTypeIdExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTypenameExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUnaryExpression;
@ -139,6 +140,7 @@ import org.eclipse.cdt.internal.core.parser2.IProblemRequestor;
*/
public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
private static final int DEFAULT_CATCH_HANDLER_LIST_SIZE = 4;
private ScopeStack templateIdScopes = new ScopeStack();
protected IASTTranslationUnit translationUnit;
private static class ScopeStack {
@ -2557,7 +2559,15 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
if( hasFunctionTryBlock && declarator instanceof ICPPASTFunctionTryBlockDeclarator )
{
catchHandlerSequence( ((ICPPASTFunctionTryBlockDeclarator)declarator));
List handlers = new ArrayList( DEFAULT_CATCH_HANDLER_LIST_SIZE );
catchHandlerSequence( handlers);
for( int i = 0; i < handlers.size(); ++i )
{
ICPPASTCatchHandler handler = (ICPPASTCatchHandler) handlers.get(i);
((ICPPASTFunctionTryBlockDeclarator)declarator).addCatchHandler( handler );
handler.setParent( declarator );
handler.setPropertyInParent( ICPPASTFunctionTryBlockDeclarator.CATCH_HANDLER );
}
}
return funcDefinition;
}
@ -3952,7 +3962,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
return new CPPASTBaseSpecifier();
}
protected void catchHandlerSequence(ICPPASTFunctionTryBlockDeclarator declarator)
protected void catchHandlerSequence(List collection)
throws EndOfFileException, BacktrackException {
if (LT(1) != IToken.t_catch) {
IToken la = LA(1);
@ -3993,9 +4003,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
compoundStatement.setParent( handler );
compoundStatement.setPropertyInParent( ICPPASTCatchHandler.CATCH_BODY );
}
declarator.addCatchHandler( handler );
handler.setParent( declarator );
handler.setPropertyInParent( ICPPASTFunctionTryBlockDeclarator.CATCH_HANDLER );
collection.add( handler );
} catch (BacktrackException bte) {
failParse(bte);
failParseWithErrorHandling();
@ -4579,7 +4587,29 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
cleanupLastToken();
IASTNullStatement null_statement = createNullStatement();
((ASTNode)null_statement).setOffset( startOffset );
cleanupLastToken();
return null_statement;
case IToken.t_try :
int startO = consume().getOffset();
IASTStatement tryBlock = compoundStatement();
List catchHandlers = new ArrayList( DEFAULT_CATCH_HANDLER_LIST_SIZE );
catchHandlerSequence(catchHandlers);
cleanupLastToken();
ICPPASTTryBlockStatement tryStatement = createTryBlockStatement();
((ASTNode)tryStatement).setOffset( startO );
tryStatement.setTryBody( tryBlock );
tryBlock.setParent( tryStatement );
tryBlock.setPropertyInParent( ICPPASTTryBlockStatement.BODY );
for( int i = 0; i < catchHandlers.size(); ++i )
{
ICPPASTCatchHandler handler = (ICPPASTCatchHandler) catchHandlers.get(i);
tryStatement.addCatchHandler( handler );
handler.setParent( tryStatement );
handler.setPropertyInParent( ICPPASTTryBlockStatement.CATCH_HANDLER );
}
return tryStatement;
default:
// can be many things:
// label
@ -4624,5 +4654,12 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
}
/**
* @return
*/
protected ICPPASTTryBlockStatement createTryBlockStatement() {
return new CPPASTTryBlockStatement();
}
}