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

View file

@ -14,66 +14,72 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.eclipse.cdt.core.dom.ast.IASTNode;
public final class TemplateParameterManager
{
protected void reset()
{
list = Collections.EMPTY_LIST;
/**
* Manages lists of template parameter nodes during the parsing
* of names. The purpose of this class is performance, as these
* lists are managed using lazy initialization and object pooling.
* 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;
}
private TemplateParameterManager(int i)
{
private TemplateParameterManager(int i) {
reset();
counterId = i;
}
private final int counterId;
private List list;
private List<List<IASTNode>> list;
private int emptySegmentCount;
public List getTemplateArgumentsList()
{
public List<List<IASTNode>> getTemplateArgumentsList() {
return list;
}
public void addSegment( List inputSegment )
{
if( inputSegment == null )
{
if( list == Collections.EMPTY_LIST )
public void addSegment(List<IASTNode> inputSegment) {
// avoid creating an actual ArrayList instance for as long as possible
if(inputSegment == null) {
if(list.isEmpty())
++emptySegmentCount;
else
list.add( null );
}
else
{
if( list == Collections.EMPTY_LIST )
{
list = new ArrayList();
else {
if(list.isEmpty()) {
list = new ArrayList<List<IASTNode>>();
for( int i = 0; i < emptySegmentCount; ++i )
list.add( null );
}
list.add( inputSegment );
}
}
// An object pool
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;
static {
for( int i = 0; i < NUMBER_OF_INSTANCES; ++i ) {
counters[ i ] = new TemplateParameterManager( i );
}
}
/**
* @return
*/
public synchronized static TemplateParameterManager getInstance() {
int index = findFreeCounter();
if( index == -1 )
@ -82,16 +88,13 @@ public final class TemplateParameterManager
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 )
instancesUsed[ c.counterId ] = false;
c.reset();
}
/**
* @return
*/
private static int findFreeCounter() {
for( int i = 0; i < NUMBER_OF_INSTANCES; ++i )
if( instancesUsed[i] == false )