From 080e0409242f22e901050dc57ef67256257dceb9 Mon Sep 17 00:00:00 2001 From: Doug Schaefer Date: Fri, 11 Apr 2003 21:14:34 +0000 Subject: [PATCH] Patch for John Camelon: CORE Fixed Bug 36243 DomBuilder Offsetable List --- .../internal/core/dom/TranslationUnit.java | 116 ++++++++++++++++-- core/org.eclipse.cdt.core/parser/ChangeLog | 3 + .../internal/core/model/CModelBuilder.java | 3 +- 3 files changed, 110 insertions(+), 12 deletions(-) diff --git a/core/org.eclipse.cdt.core/dom/org/eclipse/cdt/internal/core/dom/TranslationUnit.java b/core/org.eclipse.cdt.core/dom/org/eclipse/cdt/internal/core/dom/TranslationUnit.java index 008241b8877..799e36593a4 100644 --- a/core/org.eclipse.cdt.core/dom/org/eclipse/cdt/internal/core/dom/TranslationUnit.java +++ b/core/org.eclipse.cdt.core/dom/org/eclipse/cdt/internal/core/dom/TranslationUnit.java @@ -2,8 +2,10 @@ package org.eclipse.cdt.internal.core.dom; import java.util.ArrayList; import java.util.Collections; +import java.util.Iterator; import java.util.LinkedList; import java.util.List; +import java.util.NoSuchElementException; /** @@ -13,11 +15,9 @@ public class TranslationUnit implements IScope { private List declarations = new LinkedList(); private List macros = new ArrayList(); private List inclusions = new ArrayList(); - private List offsetables = new ArrayList(); public void addDeclaration(Declaration declaration) { declarations.add(declaration); - offsetables.add(declaration); } public List getDeclarations() { @@ -40,21 +40,117 @@ public class TranslationUnit implements IScope { public void addMacro(Macro macro) { macros.add(macro); - offsetables.add(macro); } public void addInclusion(Inclusion inclusion) { inclusions.add(inclusion); - offsetables.add(inclusion); } - /** - * Returns the offsetables. - * @return List - */ - public List getOffsetables() { - return Collections.unmodifiableList( offsetables ); + public Iterator iterateOffsetableElements() + { + return new OffsetableIterator(); + } + + public class OffsetableIterator implements Iterator + { + private final Iterator declarationIter; + private final Iterator inclusionIter; + private final Iterator macroIter; + + private IOffsetable currentMacro = null, currentInclusion= null, currentDeclaration= null; + + public OffsetableIterator() + { + declarationIter = getDeclarations().iterator(); + inclusionIter = getInclusions().iterator(); + macroIter = getMacros().iterator(); + updateInclusionIterator(); + updateDeclarationIterator(); + updateMacroIterator(); + } + + private Object updateDeclarationIterator() + { + Object offsetable = currentDeclaration; + currentDeclaration = ( declarationIter.hasNext() ) ? (IOffsetable)declarationIter.next() : null; + return offsetable; + } + + private Object updateMacroIterator() + { + Object offsetable = currentMacro; + currentMacro = ( macroIter.hasNext() ) ? (IOffsetable)macroIter.next() : null; + return offsetable; + } + + private Object updateInclusionIterator() + { + Object offsetable = currentInclusion; + currentInclusion = ( inclusionIter.hasNext() ) ? (IOffsetable)inclusionIter.next() : null; + return offsetable; + } + /* (non-Javadoc) + * @see java.util.Iterator#hasNext() + */ + public boolean hasNext() { + return (( currentMacro == null && currentInclusion == null && currentDeclaration == null ) ? + false : true); + } + + /* (non-Javadoc) + * @see java.util.Iterator#next() + */ + public Object next() { + // case 1: all are null + if( ! hasNext() ) + throw new NoSuchElementException(); + + // case 2: two of three are null + if( currentMacro == null && currentInclusion == null ) + return updateDeclarationIterator(); + if( currentDeclaration == null && currentInclusion == null ) + return updateMacroIterator(); + if( currentMacro == null && currentDeclaration == null ) + return updateInclusionIterator(); + + // case 3: 1 is null + if( currentMacro == null ) + if( currentDeclaration.getStartingOffset() < currentInclusion.getStartingOffset() ) + return updateDeclarationIterator(); + else + return updateInclusionIterator(); + + if( currentInclusion == null ) + if( currentDeclaration.getStartingOffset() < currentMacro.getStartingOffset() ) + return updateDeclarationIterator(); + else + return updateMacroIterator(); + + if( currentDeclaration == null ) + if( currentInclusion.getStartingOffset() < currentMacro.getStartingOffset() ) + return updateInclusionIterator(); + else + return updateMacroIterator(); + + // case 4: none are null + if( currentInclusion.getStartingOffset() < currentMacro.getStartingOffset() && + currentInclusion.getStartingOffset() < currentDeclaration.getStartingOffset() ) + return updateInclusionIterator(); + + if( currentMacro.getStartingOffset() < currentInclusion.getStartingOffset() && + currentMacro.getStartingOffset() < currentDeclaration.getStartingOffset() ) + return updateMacroIterator(); + // only remaining case + return updateDeclarationIterator(); + } + + /* (non-Javadoc) + * @see java.util.Iterator#remove() + */ + public void remove() { + throw new UnsupportedOperationException( "OffsetableIterator is a const iterator"); + } } } diff --git a/core/org.eclipse.cdt.core/parser/ChangeLog b/core/org.eclipse.cdt.core/parser/ChangeLog index 9325e1f20cb..025506f9730 100644 --- a/core/org.eclipse.cdt.core/parser/ChangeLog +++ b/core/org.eclipse.cdt.core/parser/ChangeLog @@ -1,3 +1,6 @@ +2003-04-11 John Camelon + Fixed Bug 36243 DomBuilder Offsetable List + 2003-04-10 John Camelon Fixed Bug36237 Parser fails on casts in ctor initializer. Added AccessSpecifier to TemplateDeclaration. diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/model/CModelBuilder.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/model/CModelBuilder.java index 668c61fd38f..625f164de4b 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/model/CModelBuilder.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/model/CModelBuilder.java @@ -65,8 +65,7 @@ public class CModelBuilder { } protected void generateModelElements(TranslationUnit tu){ - List offsetables = tu.getOffsetables(); - Iterator i = offsetables.iterator(); + Iterator i = tu.iterateOffsetableElements(); while (i.hasNext()){ IOffsetable offsetable = (IOffsetable)i.next(); if(offsetable instanceof Inclusion){