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

Made significant footprint improvements to the parser.

This commit is contained in:
John Camelon 2004-05-18 20:41:15 +00:00
parent 245a435785
commit 4abe159ab8
25 changed files with 393 additions and 638 deletions

View file

@ -1832,4 +1832,9 @@ public class CompleteParseASTTest extends CompleteParseBaseTest
assertFalse( localVariables.hasNext() );
assertAllReferences( 4, createTaskList( new Task( ABC, 2 ), new Task( variable ), new Task( destructor )));
}
public void testBug39676_tough() throws Exception
{
parse( "int widths[] = { [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 };", true, ParserLanguage.C ); //$NON-NLS-1$
}
}

View file

@ -2222,10 +2222,6 @@ public class QuickParseASTTests extends BaseASTTest
parse("struct file_operations driver_fops = { open: device_open, release: device_release };", true, true, ParserLanguage.C ).getDeclarations(); //$NON-NLS-1$
}
public void testBug39676_tough() throws Exception
{
parse( "int widths[] = { [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 };", true, true, ParserLanguage.C ); //$NON-NLS-1$
}
public void testBug60142() throws Exception
{

View file

@ -1,20 +0,0 @@
/*******************************************************************************
* Copyright (c) 2000 - 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 Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.core.parser;
/**
* @author jcamelon
*/
public interface INumericToken extends IToken {
public long getIntegerValue();
}

View file

@ -116,8 +116,6 @@ public interface IToken {
static public final int tEQUAL = 37;
static public final int tASSIGN = 38;
static public final int tHEXINT = 39;
static public final int tSHIFTL = 40;

View file

@ -121,11 +121,6 @@ public interface IASTFactory
IASTExpression thirdExpression,
IASTTypeId typeId,
ITokenDuple idExpression, String literal, IASTNewExpressionDescriptor newDescriptor) throws ASTSemanticException;
public IASTExpression createExpression(
IASTExpression.Kind kind,
long literal,
boolean isHex) throws ASTSemanticException;
public IASTExpression.IASTNewExpressionDescriptor createNewDescriptor(List newPlacementExpressions,List newTypeIdExpressions,List newInitializerExpressions);
@ -277,5 +272,6 @@ public interface IASTFactory
*/
public boolean validateDirectMemberOperation(IASTNode node);
public void constructExpressions( boolean flag );
}

View file

@ -30,6 +30,7 @@ import org.eclipse.cdt.core.parser.ast.IASTScope;
*/
public class Declarator implements IParameterCollection, IDeclaratorOwner, IDeclarator
{
private static final int DEFAULT_ARRAYLIST_SIZE = 4;
private boolean hasFunctionTryBlock;
private ITokenDuple pointerOperatorNameDuple;
private ITokenDuple namedDuple;
@ -41,17 +42,16 @@ public class Declarator implements IParameterCollection, IDeclaratorOwner, IDecl
private Declarator ownedDeclarator = null;
private String name = ""; //$NON-NLS-1$
private IASTInitializerClause initializerClause = null;
private List ptrOps = new ArrayList();
private List parameters = new ArrayList();
private List arrayModifiers = new ArrayList();
private List constructorMemberInitializers = new ArrayList();
private IASTExceptionSpecification exceptionSpecification = null;
private IASTExpression bitFieldExpression = null;
private boolean isConst = false;
private boolean isVolatile = false;
private boolean isKandR = false;
private List ptrOps = Collections.EMPTY_LIST;
private List parameters = Collections.EMPTY_LIST;
private List arrayModifiers = Collections.EMPTY_LIST;
private List constructorMemberInitializers = Collections.EMPTY_LIST;
private int nameStartOffset, nameEndOffset;
private boolean varArgs;
private int nameLine;
@ -132,6 +132,9 @@ public class Declarator implements IParameterCollection, IDeclaratorOwner, IDecl
public void addPointerOperator( ASTPointerOperator ptrOp )
{
if( ptrOps == Collections.EMPTY_LIST )
ptrOps = new ArrayList( DEFAULT_ARRAYLIST_SIZE );
ptrOps.add( ptrOp );
}
/**
@ -144,6 +147,9 @@ public class Declarator implements IParameterCollection, IDeclaratorOwner, IDecl
public void addParameter( DeclarationWrapper param )
{
if( parameters == Collections.EMPTY_LIST )
parameters = new ArrayList( DEFAULT_ARRAYLIST_SIZE );
parameters.add( param );
}
/**
@ -271,6 +277,8 @@ public class Declarator implements IParameterCollection, IDeclaratorOwner, IDecl
*/
public void addArrayModifier(IASTArrayModifier arrayMod)
{
if( arrayModifiers == Collections.EMPTY_LIST )
arrayModifiers = new ArrayList( DEFAULT_ARRAYLIST_SIZE );
arrayModifiers.add( arrayMod );
}
@ -319,6 +327,9 @@ public class Declarator implements IParameterCollection, IDeclaratorOwner, IDecl
*/
public void addConstructorMemberInitializer(IASTConstructorMemberInitializer initializer)
{
if( constructorMemberInitializers == Collections.EMPTY_LIST )
constructorMemberInitializers = new ArrayList( DEFAULT_ARRAYLIST_SIZE );
constructorMemberInitializers.add( initializer );
}

View file

@ -17,7 +17,6 @@ import java.util.Stack;
import org.eclipse.cdt.core.parser.BacktrackException;
import org.eclipse.cdt.core.parser.EndOfFileException;
import org.eclipse.cdt.core.parser.INumericToken;
import org.eclipse.cdt.core.parser.IParserLogService;
import org.eclipse.cdt.core.parser.IScanner;
import org.eclipse.cdt.core.parser.IToken;
@ -388,76 +387,85 @@ public class ExpressionParser implements IExpressionParser, IParserData {
*/
protected ITokenDuple name(IASTScope scope, IASTCompletionNode.CompletionKind kind, Key key ) throws BacktrackException, EndOfFileException {
IToken first = LA(1);
IToken last = null;
IToken mark = mark();
List argumentList = new LinkedList();
boolean hasTemplateId = false;
boolean startsWithColonColon = false;
if (LT(1) == IToken.tCOLONCOLON){
argumentList.add( null );
last = consume( IToken.tCOLONCOLON );
setCompletionValues( scope, kind, Key.EMPTY, getCompliationUnit() );
startsWithColonColon = true;
}
if (LT(1) == IToken.tCOMPL)
consume();
switch (LT(1))
{
case IToken.tIDENTIFIER :
IToken prev = last;
last = consume(IToken.tIDENTIFIER);
if( startsWithColonColon )
setCompletionValues( scope, kind, getCompliationUnit() );
else if( prev != null )
setCompletionValues(scope, kind, first, prev, Key.EMPTY );
else
setCompletionValuesNoContext(scope, kind, key );
last = consumeTemplateArguments(scope, last, argumentList);
if( last.getType() == IToken.tGT )
hasTemplateId = true;
break;
default :
backup(mark);
throw backtrack;
}
while (LT(1) == IToken.tCOLONCOLON)
{
IToken prev = last;
last = consume(IToken.tCOLONCOLON);
setCompletionValues( scope, kind, first, prev, Key.EMPTY );
if (queryLookaheadCapability() && LT(1) == IToken.t_template)
consume();
if (queryLookaheadCapability() && LT(1) == IToken.tCOMPL)
consume();
switch (LT(1))
{
case IToken.t_operator :
backup(mark);
throw backtrack;
case IToken.tIDENTIFIER :
prev = last;
last = consume();
setCompletionValues( scope, kind, first, prev, Key.EMPTY );
last = consumeTemplateArguments(scope, last, argumentList);
if( last.getType() == IToken.tGT )
hasTemplateId = true;
}
}
ITokenDuple tokenDuple = new TokenDuple(first, last, ( hasTemplateId ? argumentList : null ) );
setGreaterNameContext( tokenDuple );
return tokenDuple;
TemplateParameterManager argumentList = TemplateParameterManager.getInstance();
try
{
IToken first = LA(1);
IToken last = null;
IToken mark = mark();
boolean hasTemplateId = false;
boolean startsWithColonColon = false;
if (LT(1) == IToken.tCOLONCOLON){
argumentList.addSegment( null );
last = consume( IToken.tCOLONCOLON );
setCompletionValues( scope, kind, Key.EMPTY, getCompliationUnit() );
startsWithColonColon = true;
}
if (LT(1) == IToken.tCOMPL)
consume();
switch (LT(1))
{
case IToken.tIDENTIFIER :
IToken prev = last;
last = consume(IToken.tIDENTIFIER);
if( startsWithColonColon )
setCompletionValues( scope, kind, getCompliationUnit() );
else if( prev != null )
setCompletionValues(scope, kind, first, prev, Key.EMPTY );
else
setCompletionValuesNoContext(scope, kind, key );
last = consumeTemplateArguments(scope, last, argumentList);
if( last.getType() == IToken.tGT )
hasTemplateId = true;
break;
default :
backup(mark);
throw backtrack;
}
while (LT(1) == IToken.tCOLONCOLON)
{
IToken prev = last;
last = consume(IToken.tCOLONCOLON);
setCompletionValues( scope, kind, first, prev, Key.EMPTY );
if (queryLookaheadCapability() && LT(1) == IToken.t_template)
consume();
if (queryLookaheadCapability() && LT(1) == IToken.tCOMPL)
consume();
switch (LT(1))
{
case IToken.t_operator :
backup(mark);
throw backtrack;
case IToken.tIDENTIFIER :
prev = last;
last = consume();
setCompletionValues( scope, kind, first, prev, Key.EMPTY );
last = consumeTemplateArguments(scope, last, argumentList);
if( last.getType() == IToken.tGT )
hasTemplateId = true;
}
}
ITokenDuple tokenDuple = new TokenDuple(first, last, ( hasTemplateId ? argumentList.getTemplateArgumentsList() : null ) );
setGreaterNameContext( tokenDuple );
return tokenDuple;
}
finally
{
TemplateParameterManager.returnInstance( argumentList );
}
}
@ -514,22 +522,22 @@ public class ExpressionParser implements IExpressionParser, IParserData {
* @throws EndOfFileException
* @throws BacktrackException
*/
protected IToken consumeTemplateArguments(IASTScope scope, IToken last, List argumentList) throws EndOfFileException, BacktrackException {
protected IToken consumeTemplateArguments(IASTScope scope, IToken last, TemplateParameterManager argumentList) throws EndOfFileException, BacktrackException {
if( LT(1) == IToken.tLT ){
IToken secondMark = mark();
consume( IToken.tLT );
try
{
List list = templateArgumentList( scope );
argumentList.add( list );
argumentList.addSegment( list );
last = consume( IToken.tGT );
} catch( BacktrackException bt )
{
argumentList.add( null );
argumentList.addSegment( null );
backup( secondMark );
}
} else {
argumentList.add( null );
argumentList.addSegment( null );
}
return last;
}
@ -608,7 +616,7 @@ public class ExpressionParser implements IExpressionParser, IParserData {
}
}
protected void operatorId(Declarator d, IToken originalToken, List templateArgs) throws BacktrackException, EndOfFileException {
protected void operatorId(Declarator d, IToken originalToken, TemplateParameterManager templateArgs) throws BacktrackException, EndOfFileException {
// we know this is an operator
IToken operatorToken = consume(IToken.t_operator);
IToken toSend = null;
@ -648,18 +656,31 @@ public class ExpressionParser implements IExpressionParser, IParserData {
toSend = lastToken;
}
List args = ( templateArgs != null ) ? templateArgs : new LinkedList();
boolean hasTemplateId = ( templateArgs != null );
toSend = consumeTemplateArguments( d.getDeclarationWrapper().getScope(), toSend, args );
if( toSend.getType() == IToken.tGT ){
hasTemplateId = true;
boolean grabbedNewInstance = false;
if( templateArgs == null )
{
templateArgs = TemplateParameterManager.getInstance();
grabbedNewInstance = true;
}
ITokenDuple duple =
new TokenDuple( originalToken == null ? operatorToken : originalToken, toSend, (hasTemplateId ? args : null ) );
d.setName(duple);
try
{
toSend = consumeTemplateArguments( d.getDeclarationWrapper().getScope(), toSend, templateArgs );
if( toSend.getType() == IToken.tGT ){
hasTemplateId = true;
}
ITokenDuple duple =
new TokenDuple( originalToken == null ? operatorToken : originalToken, toSend, (hasTemplateId ? templateArgs.getTemplateArgumentsList() : null ) );
d.setName(duple);
}
finally
{
if( grabbedNewInstance )
TemplateParameterManager.returnInstance( templateArgs );
}
}
/**
@ -1495,6 +1516,9 @@ public class ExpressionParser implements IExpressionParser, IParserData {
private CompletionKind getCastExpressionKind(CompletionKind kind) {
return ((kind == CompletionKind.SINGLE_NAME_REFERENCE || kind == CompletionKind.FUNCTION_REFERENCE)? kind : CompletionKind.TYPE_REFERENCE);
}
/**
* @param completionKind TODO
* @throws BacktrackException
@ -1658,7 +1682,7 @@ public class ExpressionParser implements IExpressionParser, IParserData {
if( kind == null )
throw backtrack;
TypeId id = new TypeId(scope);
TypeId id = TypeId.getInstance(scope);
IToken last = lastToken;
//template parameters are consumed as part of name
@ -2013,7 +2037,7 @@ public class ExpressionParser implements IExpressionParser, IParserData {
{
unaryExpression = unaryExpression(scope,kind, key);
}
if (d != null & unaryExpression == null)
if (unaryExpression == null)
try
{
return astFactory.createExpression(
@ -2033,28 +2057,25 @@ public class ExpressionParser implements IExpressionParser, IParserData {
logException( "unaryExpression_1::createExpression()", e ); //$NON-NLS-1$
throw backtrack;
}
else if (unaryExpression != null && d == null)
try
{
return astFactory.createExpression(
scope,
IASTExpression.Kind.UNARY_SIZEOF_UNARYEXPRESSION,
unaryExpression,
null,
null,
null,
null, EMPTY_STRING, null);
}
catch (ASTSemanticException e1)
{
throw backtrack;
} catch (Exception e)
{
logException( "unaryExpression_1::createExpression()", e ); //$NON-NLS-1$
throw backtrack;
}
else
throw backtrack;
try
{
return astFactory.createExpression(
scope,
IASTExpression.Kind.UNARY_SIZEOF_UNARYEXPRESSION,
unaryExpression,
null,
null,
null,
null, EMPTY_STRING, null);
}
catch (ASTSemanticException e1)
{
throw backtrack;
} catch (Exception e)
{
logException( "unaryExpression_1::createExpression()", e ); //$NON-NLS-1$
throw backtrack;
}
case IToken.t_new :
return newExpression(scope,key);
case IToken.t_delete :
@ -2535,15 +2556,9 @@ public class ExpressionParser implements IExpressionParser, IParserData {
{
// TO DO: we need more literals...
case IToken.tINTEGER :
case IToken.tHEXINT:
t = consume();
boolean isHex = ( t.getType() == IToken.tHEXINT );
try
{
if( t instanceof INumericToken )
return astFactory.createExpression(
IASTExpression.Kind.PRIMARY_INTEGER_LITERAL,
((INumericToken)t).getIntegerValue(), isHex);
return astFactory.createExpression( scope, IASTExpression.Kind.PRIMARY_INTEGER_LITERAL, null, null, null, null, null, t.getImage(), null );
}
catch (ASTSemanticException e1)

View file

@ -436,7 +436,6 @@ public class GCCParserExtension implements IParserExtension {
Map extensionParms = new Hashtable();
extensionParms.put( IASTGCCDesignator.SECOND_EXRESSION, constantExpression2 );
return parserData.getAstFactory().createDesignator( IASTGCCDesignator.DesignatorKind.SUBSCRIPT_RANGE, constantExpression1, null, extensionParms );
}
}
catch( EndOfFileException eof )

View file

@ -12,7 +12,6 @@ package org.eclipse.cdt.internal.core.parser;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.cdt.core.parser.BacktrackException;
@ -75,7 +74,8 @@ import org.eclipse.cdt.internal.core.parser.util.TraceUtil;
*/
public abstract class Parser extends ExpressionParser implements IParser
{
protected ISourceElementRequestor requestor = null;
private static final int DEFAULT_DESIGNATOR_LIST_SIZE = 4;
protected ISourceElementRequestor requestor = null;
/**
* This is the standard cosntructor that we expect the Parser to be instantiated
@ -1810,12 +1810,21 @@ public abstract class Parser extends ExpressionParser implements IParser
throws EndOfFileException, BacktrackException
{
Declarator d = declarator(sdw, sdw.getScope(), strategy, kind );
if( language == ParserLanguage.CPP )
optionalCPPInitializer(d, constructInitializers);
else if( language == ParserLanguage.C )
optionalCInitializer(d, constructInitializers);
sdw.addDeclarator(d);
return d;
try
{
astFactory.constructExpressions(constructInitializers);
if( language == ParserLanguage.CPP )
optionalCPPInitializer(d, constructInitializers);
else if( language == ParserLanguage.C )
optionalCInitializer(d, constructInitializers);
sdw.addDeclarator(d);
return d;
}
finally
{
astFactory.constructExpressions( true );
}
}
protected void optionalCPPInitializer(Declarator d, boolean constructInitializers)
@ -2046,7 +2055,7 @@ public abstract class Parser extends ExpressionParser implements IParser
protected List designatorList(IASTScope scope) throws EndOfFileException, BacktrackException
{
List designatorList = new ArrayList();
List designatorList = Collections.EMPTY_LIST;
// designated initializers for C
if( LT(1) == IToken.tDOT || LT(1) == IToken.tLBRACKET )
@ -2075,7 +2084,11 @@ public abstract class Parser extends ExpressionParser implements IParser
{
IASTDesignator d = extension.parseDesignator( this, scope );
if( d != null )
{
if( designatorList == Collections.EMPTY_LIST )
designatorList = new ArrayList( DEFAULT_DESIGNATOR_LIST_SIZE );
designatorList.add( d );
}
break;
}
}
@ -2085,6 +2098,8 @@ public abstract class Parser extends ExpressionParser implements IParser
IASTDesignator d =
astFactory.createDesignator( kind, constantExpression, id, null );
if( designatorList == Collections.EMPTY_LIST )
designatorList = new ArrayList( DEFAULT_DESIGNATOR_LIST_SIZE );
designatorList.add( d );
}
@ -2095,7 +2110,11 @@ public abstract class Parser extends ExpressionParser implements IParser
{
IASTDesignator d = extension.parseDesignator( this, scope );
if( d != null )
{
if( designatorList == Collections.EMPTY_LIST )
designatorList = new ArrayList( DEFAULT_DESIGNATOR_LIST_SIZE );
designatorList.add( d );
}
}
}
return designatorList;
@ -2338,58 +2357,65 @@ public abstract class Parser extends ExpressionParser implements IParser
protected void consumeTemplatedOperatorName(Declarator d, CompletionKind kind)
throws EndOfFileException, BacktrackException
{
if (LT(1) == IToken.t_operator)
operatorId(d, null, null);
else
{
try
{
ITokenDuple duple = name(d.getDeclarationWrapper().getScope(), kind, Key.EMPTY );
d.setName(duple);
}
catch (BacktrackException bt)
{
Declarator d1 = d;
Declarator d11 = d1;
IToken start = null;
List argumentList = new LinkedList();
boolean hasTemplateId = false;
IToken mark = mark();
if (LT(1) == IToken.tCOLONCOLON
|| LT(1) == IToken.tIDENTIFIER)
{
start = consume();
IToken end = null;
if (start.getType() == IToken.tIDENTIFIER){
end = consumeTemplateArguments(d.getDeclarationWrapper().getScope(), end, argumentList);
if( end != null && end.getType() == IToken.tGT )
hasTemplateId = true;
}
while (LT(1) == IToken.tCOLONCOLON
|| LT(1) == IToken.tIDENTIFIER)
{
end = consume();
if (end.getType() == IToken.tIDENTIFIER){
end = consumeTemplateArguments(d.getDeclarationWrapper().getScope(), end, argumentList);
if( end.getType() == IToken.tGT )
hasTemplateId = true;
}
}
if (LT(1) == IToken.t_operator)
operatorId(d11, start, ( hasTemplateId ? argumentList : null ) );
else
{
backup(mark);
throw backtrack;
}
}
}
}
TemplateParameterManager argumentList = TemplateParameterManager.getInstance();
try
{
if (LT(1) == IToken.t_operator)
operatorId(d, null, null);
else
{
try
{
ITokenDuple duple = name(d.getDeclarationWrapper().getScope(), kind, Key.EMPTY );
d.setName(duple);
}
catch (BacktrackException bt)
{
Declarator d1 = d;
Declarator d11 = d1;
IToken start = null;
boolean hasTemplateId = false;
IToken mark = mark();
if (LT(1) == IToken.tCOLONCOLON
|| LT(1) == IToken.tIDENTIFIER)
{
start = consume();
IToken end = null;
if (start.getType() == IToken.tIDENTIFIER){
end = consumeTemplateArguments(d.getDeclarationWrapper().getScope(), end, argumentList);
if( end != null && end.getType() == IToken.tGT )
hasTemplateId = true;
}
while (LT(1) == IToken.tCOLONCOLON
|| LT(1) == IToken.tIDENTIFIER)
{
end = consume();
if (end.getType() == IToken.tIDENTIFIER){
end = consumeTemplateArguments(d.getDeclarationWrapper().getScope(), end, argumentList);
if( end.getType() == IToken.tGT )
hasTemplateId = true;
}
}
if (LT(1) == IToken.t_operator)
operatorId(d11, start, ( hasTemplateId ? argumentList : null ) );
else
{
backup(mark);
throw backtrack;
}
}
}
}
} finally
{
TemplateParameterManager.returnInstance(argumentList );
}
}
/**
* Parse an enumeration specifier, as according to the ANSI specs in C & C++.

View file

@ -0,0 +1,102 @@
/**********************************************************************
* Copyright (c) 2002-2004 Rational Software Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v0.5
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v05.html
*
* Contributors:
* IBM Rational Software - Initial API and implementation
***********************************************************************/
package org.eclipse.cdt.internal.core.parser;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
final class TemplateParameterManager
{
protected void reset()
{
list = Collections.EMPTY_LIST;
emptySegmentCount = 0;
}
private TemplateParameterManager(int i)
{
reset();
counterId = i;
}
private final int counterId;
private List list;
private int emptySegmentCount;
public List getTemplateArgumentsList()
{
return list;
}
public void addSegment( List inputSegment )
{
if( inputSegment == null )
{
if( list == Collections.EMPTY_LIST )
++emptySegmentCount;
else
list.add( null );
}
else
{
if( list == Collections.EMPTY_LIST )
{
list = new ArrayList();
for( int i = 0; i < emptySegmentCount; ++i )
list.add( null );
}
list.add( inputSegment );
}
}
private static final int NUMBER_OF_INSTANCES = 8;
private static final boolean [] instancesUsed = new boolean[ NUMBER_OF_INSTANCES ];
private static final TemplateParameterManager [] counters = new TemplateParameterManager[ NUMBER_OF_INSTANCES ];
private static int counter = 8;
static
{
for( int i = 0; i < NUMBER_OF_INSTANCES; ++i )
{
instancesUsed[ i ] = false;
counters[ i ] = new TemplateParameterManager( i );
}
}
/**
* @return
*/
public static TemplateParameterManager getInstance() {
int index = findFreeCounter();
if( index == -1 )
return new TemplateParameterManager(++counter);
instancesUsed[ index ] = true;
return counters[ index ];
}
public static void returnInstance( TemplateParameterManager c )
{
if( c.counterId > 0 && c.counterId < NUMBER_OF_INSTANCES )
instancesUsed[ c.counterId ] = false;
c.reset();
}
/**
* @return
*/
private static int findFreeCounter() {
for( int i = 0; i < NUMBER_OF_INSTANCES; ++i )
if( instancesUsed[i] == false )
return i;
return -1;
}
}

View file

@ -11,6 +11,7 @@
package org.eclipse.cdt.internal.core.parser;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.eclipse.cdt.core.parser.ITokenDuple;
@ -24,16 +25,33 @@ import org.eclipse.cdt.core.parser.ast.IASTScope;
*/
public class TypeId implements IDeclarator
{
private static final int DEFAULT_ARRAYLIST_SIZE = 4;
private ITokenDuple name;
private List arrayModifiers = new ArrayList();
private List pointerOperators = new ArrayList();
private final IASTScope scope;
private List arrayModifiers;
private List pointerOperators;
private IASTScope scope;
private static TypeId instance = new TypeId();
public static TypeId getInstance(IASTScope scope)
{
instance.reset(scope);
return instance;
}
/**
* @param scope2
*/
private void reset(IASTScope s) {
this.scope = s;
arrayModifiers = Collections.EMPTY_LIST;
pointerOperators = Collections.EMPTY_LIST;
name = null;
}
/**
*
*/
public TypeId(IASTScope scope )
private TypeId()
{
this.scope = scope;
reset( null );
}
/* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.parser.IDeclarator#getPointerOperators()
@ -47,6 +65,8 @@ public class TypeId implements IDeclarator
*/
public void addPointerOperator(ASTPointerOperator ptrOp)
{
if( pointerOperators == Collections.EMPTY_LIST )
pointerOperators = new ArrayList( DEFAULT_ARRAYLIST_SIZE );
pointerOperators.add( ptrOp );
}
/* (non-Javadoc)
@ -54,6 +74,8 @@ public class TypeId implements IDeclarator
*/
public void addArrayModifier(IASTArrayModifier arrayMod)
{
if( arrayModifiers == Collections.EMPTY_LIST )
arrayModifiers = new ArrayList( DEFAULT_ARRAYLIST_SIZE );
arrayModifiers.add( arrayMod );
}
/* (non-Javadoc)

View file

@ -1,41 +0,0 @@
/*******************************************************************************
* Copyright (c) 2000 - 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 Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.core.parser.ast.complete;
import java.util.Collections;
import org.eclipse.cdt.core.parser.ast.IASTExpression;
/**
* @author jcamelon
*/
public class ASTLiteralIntegerExpression extends ASTExpression implements IASTExpression {
private final boolean isHex;
private final long literal;
/**
* @param kind
* @param literal
* @param isHex
*/
public ASTLiteralIntegerExpression(Kind kind, long literal, boolean isHex) {
super( kind, Collections.EMPTY_LIST );
this.literal = literal;
this.isHex = isHex;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ast.IASTExpression#getLiteralString()
*/
public String getLiteralString() {
if( isHex )
return Long.toHexString( literal );
return Long.toString( literal );
}
}

View file

@ -2641,7 +2641,9 @@ public class CompleteParseASTFactory extends BaseASTFactory implements IASTFacto
{
ISymbol variableSymbol = ((ASTVariable)clause.getOwnerVariableDeclaration()).getSymbol();
ISymbol currentSymbol = variableSymbol.getTypeSymbol();
if( currentSymbol == null )
return;
TypeInfo currentTypeInfo = new TypeInfo( currentSymbol.getTypeInfo() );
Iterator designators = clause.getDesignators();
while( designators.hasNext() )
@ -3552,21 +3554,10 @@ public class CompleteParseASTFactory extends BaseASTFactory implements IASTFacto
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ast.IASTFactory#createExpression(org.eclipse.cdt.core.parser.ast.IASTExpression.Kind, long, boolean)
* @see org.eclipse.cdt.core.parser.ast.IASTFactory#constructExpressions(boolean)
*/
public IASTExpression createExpression(Kind kind, long literal, boolean isHex) throws ASTSemanticException {
// Try to figure out the result that this expression evaluates to
ExpressionResult expressionResult = getExpressionResultType(null, kind, null, null, null, null, Long.toString(literal), null);
// expression results could be empty, but should not be null
// assert expressionResult != null : expressionResult; //throw new ASTSemanticException();
// create the ASTExpression
ASTExpression expression = (ASTExpression) ExpressionFactory.createExpression( kind, literal, isHex );
// Assign the result to the created expression
expression.setResultType (expressionResult);
return expression;
public void constructExpressions(boolean flag) {
//ignore
}
}

View file

@ -52,14 +52,4 @@ public class ExpressionFactory {
return new ASTEmptyExpression( kind, references );
}
/**
* @param kind
* @param literal
* @param isHex
* @return
*/
public static IASTExpression createExpression(Kind kind, long literal, boolean isHex) {
return new ASTLiteralIntegerExpression( kind, literal, isHex );
}
}

View file

@ -1,60 +0,0 @@
/*******************************************************************************
* Copyright (c) 2000 - 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 Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.core.parser.ast.expression;
import org.eclipse.cdt.core.parser.ast.ASTExpressionEvaluationException;
import org.eclipse.cdt.core.parser.ast.IASTExpression;
/**
* @author jcamelon
*/
public class ASTLiteralIntegerExpression extends ASTExpression
implements
IASTExpression {
private final long literal;
private final boolean isHex;
/**
* @param kind
* @param literal
*/
public ASTLiteralIntegerExpression(Kind kind, long literal, boolean isHex) {
super( kind );
this.literal = literal;
this.isHex = isHex;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ast.IASTExpression#getLiteralString()
*/
public String getLiteralString() {
if( isHex )
{
StringBuffer x = new StringBuffer( "0x"); //$NON-NLS-1$
x.append( Long.toHexString(literal));
return x.toString();
}
return Long.toString( literal );
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ast.IASTExpression#evaluateExpression()
*/
public long evaluateExpression() throws ASTExpressionEvaluationException {
if( getExpressionKind() == IASTExpression.Kind.PRIMARY_INTEGER_LITERAL )
return literal;
return super.evaluateExpression();
}
}

View file

@ -26,7 +26,6 @@ public class ExpressionFactory {
*/
public ExpressionFactory() {
super();
// TODO Auto-generated constructor stub
}
/**
@ -68,19 +67,4 @@ public class ExpressionFactory {
return new ASTEmptyExpression( kind );
}
/**
* @param kind
* @param lhs
* @param rhs
* @param thirdExpression
* @param typeId
* @param string
* @param literal
* @param newDescriptor
* @return
*/
public static IASTExpression createExpression(Kind kind, long literal, boolean isHex) {
return new ASTLiteralIntegerExpression( kind, literal, isHex );
}
}

View file

@ -908,10 +908,10 @@ public class ExpressionParseASTFactory extends BaseASTFactory implements IASTFac
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ast.IASTFactory#createExpression(org.eclipse.cdt.core.parser.ast.IASTScope, org.eclipse.cdt.core.parser.ast.IASTExpression.Kind, org.eclipse.cdt.core.parser.ast.IASTExpression, org.eclipse.cdt.core.parser.ast.IASTExpression, org.eclipse.cdt.core.parser.ast.IASTExpression, org.eclipse.cdt.core.parser.ast.IASTTypeId, org.eclipse.cdt.core.parser.ITokenDuple, int, org.eclipse.cdt.core.parser.ast.IASTExpression.IASTNewExpressionDescriptor)
* @see org.eclipse.cdt.core.parser.ast.IASTFactory#constructExpressions(boolean)
*/
public IASTExpression createExpression(Kind kind, long literal, boolean isHex) throws ASTSemanticException {
return ExpressionFactory.createExpression( kind, literal, isHex );
public void constructExpressions(boolean flag) {
//ignore
}
}

View file

@ -10,8 +10,8 @@
***********************************************************************/
package org.eclipse.cdt.internal.core.parser.ast.quick;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.cdt.core.parser.ISourceElementRequestor;
@ -19,6 +19,7 @@ import org.eclipse.cdt.core.parser.ast.ASTNotImplementedException;
import org.eclipse.cdt.core.parser.ast.IASTDeclaration;
import org.eclipse.cdt.core.parser.ast.IASTScope;
import org.eclipse.cdt.core.parser.ast.IASTTemplateDeclaration;
import org.eclipse.cdt.internal.core.parser.ast.EmptyIterator;
import org.eclipse.cdt.internal.core.parser.ast.Offsets;
/**
@ -47,7 +48,7 @@ public class ASTTemplateDeclaration extends ASTDeclaration implements IASTTempla
*/
public Iterator getTemplateParameters()
{
return ( templateParameters != null ) ? templateParameters.iterator() : new LinkedList().iterator();
return ( templateParameters != null ) ? templateParameters.iterator() : EmptyIterator.EMPTY_ITERATOR;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ast.IASTTemplateDeclaration#getOwnedDeclaration()
@ -157,7 +158,7 @@ public class ASTTemplateDeclaration extends ASTDeclaration implements IASTTempla
* @see org.eclipse.cdt.core.parser.ast.IASTScope#getDeclarations()
*/
public Iterator getDeclarations() throws ASTNotImplementedException {
List decls = new LinkedList();
List decls = new ArrayList(1);
decls.add( getOwnedDeclaration() );
return decls.iterator();
}

View file

@ -18,7 +18,6 @@ import org.eclipse.cdt.core.parser.ast.ASTAccessVisibility;
import org.eclipse.cdt.core.parser.ast.ASTClassKind;
import org.eclipse.cdt.core.parser.ast.ASTNotImplementedException;
import org.eclipse.cdt.core.parser.ast.ASTPointerOperator;
import org.eclipse.cdt.core.parser.ast.ASTSemanticException;
import org.eclipse.cdt.core.parser.ast.IASTASMDefinition;
import org.eclipse.cdt.core.parser.ast.IASTAbstractDeclaration;
import org.eclipse.cdt.core.parser.ast.IASTAbstractTypeSpecifierDeclaration;
@ -70,6 +69,8 @@ import org.eclipse.cdt.internal.core.parser.ast.expression.ExpressionFactory;
*/
public class QuickParseASTFactory extends BaseASTFactory implements IASTFactory {
private boolean temporarilyDisableNodeConstruction = true;
public QuickParseASTFactory( IASTFactoryExtension extension )
{
super(extension);
@ -158,7 +159,7 @@ public class QuickParseASTFactory extends BaseASTFactory implements IASTFactory
* @see org.eclipse.cdt.core.parser.ast.IASTFactory#createExpression(org.eclipse.cdt.core.parser.ast.IASTExpression.ExpressionKind, org.eclipse.cdt.core.parser.ast.IASTExpression, org.eclipse.cdt.core.parser.ast.IASTExpression, java.lang.String, java.lang.String, java.lang.String)
*/
public IASTExpression createExpression(IASTScope scope, Kind kind, IASTExpression lhs, IASTExpression rhs, IASTExpression thirdExpression, IASTTypeId typeId, ITokenDuple idExpression, String literal, IASTNewExpressionDescriptor newDescriptor) {
return ExpressionFactory.createExpression( kind, lhs, rhs, thirdExpression, typeId, idExpression == null ? "" : idExpression.toString(), literal, newDescriptor ); //$NON-NLS-1$
return temporarilyDisableNodeConstruction ? ExpressionFactory.createExpression( kind, lhs, rhs, thirdExpression, typeId, idExpression == null ? "" : idExpression.toString(), literal, newDescriptor ) : null; //$NON-NLS-1$
}
/* (non-Javadoc)
@ -323,8 +324,8 @@ public class QuickParseASTFactory extends BaseASTFactory implements IASTFactory
public IASTTypeId createTypeId(IASTScope scope, Type kind, boolean isConst, boolean isVolatile, boolean isShort,
boolean isLong, boolean isSigned, boolean isUnsigned, boolean isTypename, ITokenDuple name, List pointerOps, List arrayMods, String completeSignature)
{
return new ASTTypeId( kind, name == null ? "" : name.toString(), pointerOps, arrayMods, isConst, //$NON-NLS-1$
isVolatile, isUnsigned, isSigned, isShort, isLong, isTypename, completeSignature );
return ( temporarilyDisableNodeConstruction ? new ASTTypeId( kind, name == null ? "" : name.toString(), pointerOps, arrayMods, isConst, //$NON-NLS-1$
isVolatile, isUnsigned, isSigned, isShort, isLong, isTypename, completeSignature ) : null );
}
/* (non-Javadoc)
@ -385,9 +386,9 @@ public class QuickParseASTFactory extends BaseASTFactory implements IASTFactory
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ast.IASTFactory#createExpression(org.eclipse.cdt.core.parser.ast.IASTExpression.Kind, long, boolean)
* @see org.eclipse.cdt.core.parser.ast.IASTFactory#constructExpressions(boolean)
*/
public IASTExpression createExpression(Kind kind, long literal, boolean isHex) throws ASTSemanticException {
return ExpressionFactory.createExpression(kind, literal, isHex );
public void constructExpressions(boolean flag) {
temporarilyDisableNodeConstruction = flag;
}
}

View file

@ -470,13 +470,7 @@ public class Scanner implements IScanner {
}
protected IToken newToken(int t, String i) {
IToken token = null;
if( t == IToken.tINTEGER )
token = TokenFactory.createIntegerToken( i, scannerData );
else if( t == IToken.tHEXINT )
token = TokenFactory.createHexadecimalIntegerToken( i, scannerData );
else
token = TokenFactory.createUniquelyImagedToken(t, i, scannerData );
IToken token = TokenFactory.createUniquelyImagedToken(t, i, scannerData );
setCurrentToken(token);
return currentToken;
}
@ -1122,12 +1116,11 @@ public class Scanner implements IScanner {
int tokenType = floatingPoint ? IToken.tFLOATINGPT : IToken.tINTEGER;
if( tokenType == IToken.tINTEGER && hex )
{
if( result.equals( HEX_PREFIX) )
if( result.equals( HEX_PREFIX ) )
{
handleProblem( IProblem.SCANNER_BAD_HEX_FORMAT, HEX_PREFIX, beginOffset, false, true );
return null;
}
tokenType = IToken.tHEXINT;
}
return newToken(

View file

@ -1,67 +0,0 @@
/*******************************************************************************
* Copyright (c) 2000 - 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 Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.core.parser.token;
import org.eclipse.cdt.core.parser.INumericToken;
import org.eclipse.cdt.internal.core.parser.scanner.ContextStack;
/**
* @author jcamelon
*/
public class HexIntegerExpansionToken extends SimpleExpansionToken implements
INumericToken {
private final long intValue;
/**
* @param i
* @param value
* @param stack
*/
public HexIntegerExpansionToken(int type, String value, ContextStack stack) {
super( type, stack );
int minIndex = findMinIndex(value);
if( minIndex == -1 )
intValue = Long.parseLong(value.substring(2), 16 );
else
intValue = Long.parseLong(value.substring(2, minIndex), 16 );
setOffsetAndLength(stack.getCurrentContext());
}
/**
* @param value
* @return
*/
private int findMinIndex(String value) {
int endIndex = value.indexOf( "U"); //$NON-NLS-1$
int endIndex2 = value.indexOf( "L"); //$NON-NLS-1$
int minIndex = endIndex < endIndex2 ? endIndex : endIndex2;
return minIndex;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.IToken#getImage()
*/
public String getImage() {
StringBuffer buffer = new StringBuffer( "0x" ); //$NON-NLS-1$
buffer.append( Long.toHexString(intValue) );
return buffer.toString();
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.INumericToken#getIntegerValue()
*/
public long getIntegerValue() {
return intValue;
}
}

View file

@ -1,68 +0,0 @@
/*******************************************************************************
* Copyright (c) 2000 - 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 Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.core.parser.token;
import org.eclipse.cdt.core.parser.INumericToken;
import org.eclipse.cdt.internal.core.parser.scanner.ContextStack;
/**
* @author jcamelon
*/
public class HexIntegerToken extends SimpleToken implements INumericToken {
private final long intValue;
/**
* @param i
* @param value
* @param stack
*/
public HexIntegerToken(int type, String value, ContextStack stack) {
super( type, stack );
int maxIndex = findMinIndex(value);
if( maxIndex > 2 )
intValue = Long.parseLong(value.substring(2, maxIndex), 16 );
else
intValue = Long.parseLong(value.substring(2), 16 );
setOffsetAndLength(stack.getCurrentContext());
}
/**
* @param value
* @return
*/
private int findMinIndex(String value) {
int endIndex = value.indexOf( "U"); //$NON-NLS-1$
int endIndex2 = value.indexOf( "L"); //$NON-NLS-1$
int minIndex = endIndex < endIndex2 ? endIndex : endIndex2;
return minIndex;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.IToken#getImage()
*/
public String getImage() {
StringBuffer buffer = new StringBuffer( "0x" ); //$NON-NLS-1$
buffer.append( Long.toHexString(intValue) );
return buffer.toString();
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.INumericToken#getIntegerValue()
*/
public long getIntegerValue() {
return intValue;
}
}

View file

@ -1,50 +0,0 @@
/*******************************************************************************
* Copyright (c) 2000 - 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 Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.core.parser.token;
import org.eclipse.cdt.core.parser.INumericToken;
import org.eclipse.cdt.internal.core.parser.scanner.ContextStack;
/**
* @author jcamelon
*/
public class IntegerExpansionToken extends SimpleExpansionToken implements INumericToken
{
private final long value;
/**
* @param tokenType
* @param value
* @param stack
*/
public IntegerExpansionToken(int tokenType, long l, ContextStack stack) {
super( tokenType, stack );
this.value = l;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.IToken#getImage()
*/
public String getImage() {
return Long.toString( value );
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.INumericToken#getIntegerValue()
*/
public long getIntegerValue() {
return value;
}
}

View file

@ -1,47 +0,0 @@
/*******************************************************************************
* Copyright (c) 2000 - 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 Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.core.parser.token;
import org.eclipse.cdt.core.parser.INumericToken;
import org.eclipse.cdt.internal.core.parser.scanner.ContextStack;
/**
* @author jcamelon
*/
public class IntegerToken extends SimpleToken implements INumericToken {
private final long value;
/**
* @param tokenType
* @param value
* @param stack
*/
public IntegerToken(int tokenType, long l, ContextStack stack) {
super( tokenType, stack );
this.value = l;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.IToken#getImage()
*/
public String getImage() {
return Long.toString( value );
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.INumericToken#getIntegerValue()
*/
public long getIntegerValue() {
return value;
}
}

View file

@ -22,29 +22,7 @@ public class TokenFactory {
static final String MAX_LONG_STRING = Long.toString( Long.MAX_VALUE );
static final int MAX_LONG_STRING_LENGTH = MAX_LONG_STRING.length();
static final String MAX_HEX_LONG_STRING = "0x" + Long.toString( Long.MAX_VALUE, 16 ); //$NON-NLS-1$
static final int MAX_HEX_LONG_STRING_LENGTH = MAX_HEX_LONG_STRING.length();
public static IToken createIntegerToken( String value, IScannerData scannerData )
{
if( value.length() > MAX_LONG_STRING_LENGTH || value.compareTo( MAX_LONG_STRING ) > 0 )
return createUniquelyImagedToken( IToken.tINTEGER, value, scannerData );
if( scannerData.getContextStack().getCurrentContext().getKind() == IScannerContext.ContextKind.MACROEXPANSION )
return new IntegerExpansionToken( IToken.tINTEGER, Long.parseLong(value ), scannerData.getContextStack() );
return new IntegerToken( IToken.tINTEGER, Long.parseLong( value ), scannerData.getContextStack() );
}
public static IToken createHexadecimalIntegerToken( String value, IScannerData scannerData )
{
if( value.length() > MAX_HEX_LONG_STRING_LENGTH || value.compareTo( MAX_HEX_LONG_STRING ) > 0 )
return createUniquelyImagedToken( IToken.tHEXINT, value, scannerData );
if( scannerData.getContextStack().getCurrentContext().getKind() == IScannerContext.ContextKind.MACROEXPANSION )
return new HexIntegerExpansionToken( IToken.tHEXINT, value, scannerData.getContextStack() );
return new HexIntegerToken( IToken.tHEXINT, value, scannerData.getContextStack() );
}
static final int MAX_HEX_LONG_STRING_LENGTH = MAX_HEX_LONG_STRING.length();
public static IToken createToken( int tokenType, IScannerData scannerData )
{