1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-29 20:05:35 +02:00

cleaned up TemplateParameterManager a bit

This commit is contained in:
Mike Kucera 2008-02-12 16:48:44 +00:00
parent 5688a0ac9f
commit 45dc1738ed
2 changed files with 48 additions and 45 deletions

View file

@ -175,7 +175,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
protected CPPASTTranslationUnit translationUnit; protected CPPASTTranslationUnit translationUnit;
private static class ScopeStack { private final static class ScopeStack {
private int[] stack; private int[] stack;
private int index = -1; private int index = -1;
@ -190,27 +190,28 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
stack = newStack; stack = newStack;
} }
final public void push(int i) { public void push(int i) {
if (++index == stack.length) if (++index == stack.length)
grow(); grow();
stack[index] = i; stack[index] = i;
} }
final public int pop() { public int pop() {
if (index >= 0) if (index >= 0)
return stack[index--]; return stack[index--];
return -1; return -1;
} }
final public int peek() { public int peek() {
if (index >= 0) if (index >= 0)
return stack[index]; return stack[index];
return -1; return -1;
} }
final public int size() { public int size() {
return index + 1; return index + 1;
} }
} }
/** /**
@ -822,8 +823,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
* @param expression * @param expression
* @throws BacktrackException * @throws BacktrackException
*/ */
protected IASTExpression multiplicativeExpression() protected IASTExpression multiplicativeExpression() throws BacktrackException, EndOfFileException {
throws BacktrackException, EndOfFileException {
IASTExpression firstExpression = pmExpression(); IASTExpression firstExpression = pmExpression();
for (;;) { for (;;) {
switch (LT(1)) { switch (LT(1)) {
@ -858,8 +858,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
* @param expression * @param expression
* @throws BacktrackException * @throws BacktrackException
*/ */
protected IASTExpression pmExpression() throws EndOfFileException, protected IASTExpression pmExpression() throws EndOfFileException, BacktrackException {
BacktrackException {
IASTExpression firstExpression = castExpression(); IASTExpression firstExpression = castExpression();
for (;;) { for (;;) {
@ -890,15 +889,16 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
/** /**
* castExpression : unaryExpression | "(" typeId ")" castExpression * castExpression : unaryExpression | "(" typeId ")" castExpression
*/ */
protected IASTExpression castExpression() throws EndOfFileException, protected IASTExpression castExpression() throws EndOfFileException, BacktrackException {
BacktrackException {
// TO DO: we need proper symbol checkint to ensure type name // TO DO: we need proper symbol checkint to ensure type name
if (LT(1) == IToken.tLPAREN) { if (LT(1) == IToken.tLPAREN) {
IToken la = LA(1); IToken la = LA(1);
int startingOffset = la.getOffset(); int startingOffset = la.getOffset();
IToken mark = mark(); IToken mark = mark();
consume(); consume();
if (templateIdScopes.size() > 0) { templateIdScopes.push(IToken.tLPAREN); }
if (templateIdScopes.size() > 0)
templateIdScopes.push(IToken.tLPAREN);
boolean popped = false; boolean popped = false;
IASTTypeId typeId = null; IASTTypeId typeId = null;

View file

@ -14,44 +14,52 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import org.eclipse.cdt.core.dom.ast.IASTNode;
public final class TemplateParameterManager /**
{ * Manages lists of template parameter nodes during the parsing
protected void reset() * of names. The purpose of this class is performance, as these
{ * lists are managed using lazy initialization and object pooling.
list = Collections.EMPTY_LIST; * This class is basically as substitute for List<List<IASTNode>>.
*
* When using the object pool code must be wrapped in a try-finally
* block to ensure the object is returned to the pool.
*
* TODO: How much of a performance improvement are we talking about here?
* It might make sense just to get rid of this class. The extra complexity
* might not be worth it.
*/
public final class TemplateParameterManager {
protected void reset() {
list = Collections.emptyList();
emptySegmentCount = 0; emptySegmentCount = 0;
} }
private TemplateParameterManager(int i) private TemplateParameterManager(int i) {
{
reset(); reset();
counterId = i; counterId = i;
} }
private final int counterId; private final int counterId;
private List list; private List<List<IASTNode>> list;
private int emptySegmentCount; private int emptySegmentCount;
public List getTemplateArgumentsList() public List<List<IASTNode>> getTemplateArgumentsList() {
{
return list; return list;
} }
public void addSegment( List inputSegment ) public void addSegment(List<IASTNode> inputSegment) {
{ // avoid creating an actual ArrayList instance for as long as possible
if( inputSegment == null ) if(inputSegment == null) {
{ if(list.isEmpty())
if( list == Collections.EMPTY_LIST )
++emptySegmentCount; ++emptySegmentCount;
else else
list.add( null ); list.add( null );
} }
else else {
{ if(list.isEmpty()) {
if( list == Collections.EMPTY_LIST ) list = new ArrayList<List<IASTNode>>();
{
list = new ArrayList();
for( int i = 0; i < emptySegmentCount; ++i ) for( int i = 0; i < emptySegmentCount; ++i )
list.add( null ); list.add( null );
} }
@ -59,21 +67,19 @@ public final class TemplateParameterManager
} }
} }
// An object pool
private static final int NUMBER_OF_INSTANCES = 8; private static final int NUMBER_OF_INSTANCES = 8;
private static final boolean [] instancesUsed = new boolean[ NUMBER_OF_INSTANCES ]; private static final boolean [] instancesUsed = new boolean[ NUMBER_OF_INSTANCES ];
private static final TemplateParameterManager [] counters = new TemplateParameterManager[ NUMBER_OF_INSTANCES ]; private static final TemplateParameterManager [] counters = new TemplateParameterManager[ NUMBER_OF_INSTANCES ];
private static int counter = 8; private static int counter = 8;
static static {
{ for( int i = 0; i < NUMBER_OF_INSTANCES; ++i ) {
for( int i = 0; i < NUMBER_OF_INSTANCES; ++i )
{
instancesUsed[ i ] = false;
counters[ i ] = new TemplateParameterManager( i ); counters[ i ] = new TemplateParameterManager( i );
} }
} }
/**
* @return
*/
public synchronized static TemplateParameterManager getInstance() { public synchronized static TemplateParameterManager getInstance() {
int index = findFreeCounter(); int index = findFreeCounter();
if( index == -1 ) if( index == -1 )
@ -82,16 +88,13 @@ public final class TemplateParameterManager
return counters[ index ]; return counters[ index ];
} }
public synchronized static void returnInstance( TemplateParameterManager c ) public synchronized static void returnInstance( TemplateParameterManager c ) {
{
if( c.counterId > 0 && c.counterId < NUMBER_OF_INSTANCES ) if( c.counterId > 0 && c.counterId < NUMBER_OF_INSTANCES )
instancesUsed[ c.counterId ] = false; instancesUsed[ c.counterId ] = false;
c.reset(); c.reset();
} }
/**
* @return
*/
private static int findFreeCounter() { private static int findFreeCounter() {
for( int i = 0; i < NUMBER_OF_INSTANCES; ++i ) for( int i = 0; i < NUMBER_OF_INSTANCES; ++i )
if( instancesUsed[i] == false ) if( instancesUsed[i] == false )