mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-23 08:55:25 +02:00
Follow up for 169860, consolidating visitors for the BTree
This commit is contained in:
parent
69b0273dcf
commit
22d4aeede2
6 changed files with 79 additions and 125 deletions
|
@ -59,7 +59,19 @@ public interface IString {
|
|||
* @throws CoreException
|
||||
*/
|
||||
public int compare(char[] chars) throws CoreException;
|
||||
|
||||
|
||||
/**
|
||||
* Compare this IString record and the specified character array
|
||||
* @param chars
|
||||
* @return <ul><li> -1 if this < chars
|
||||
* <li> 0 if this has a prefix chars
|
||||
* <li> 1 if this > chars and does not have the prefix
|
||||
* </ul>
|
||||
* @throws CoreException
|
||||
*/
|
||||
public int comparePrefix(char[] name) throws CoreException;
|
||||
|
||||
/**
|
||||
* Get an equivalent character array to this IString record<p>
|
||||
* <b>N.B. This method can be expensive: compare and equals can be used for
|
||||
|
|
|
@ -153,6 +153,10 @@ public class LongString implements IString {
|
|||
throw new PDOMNotImplementedError();
|
||||
}
|
||||
|
||||
public int comparePrefix(char[] name) {
|
||||
throw new PDOMNotImplementedError();
|
||||
}
|
||||
|
||||
private interface IReader {
|
||||
public void appendChar(char c);
|
||||
}
|
||||
|
|
|
@ -223,6 +223,33 @@ public class ShortString implements IString {
|
|||
return 0;
|
||||
}
|
||||
|
||||
public int comparePrefix(char[] other) throws CoreException {
|
||||
Chunk chunk = db.getChunk(record);
|
||||
|
||||
int i1 = record + CHARS;
|
||||
int i2 = 0;
|
||||
int n1 = i1 + chunk.getInt(record + LENGTH) * 2;
|
||||
int n2 = other.length;
|
||||
|
||||
while (i1 < n1 && i2 < n2) {
|
||||
char c1 = chunk.getChar(i1);
|
||||
char c2 = other[i2];
|
||||
|
||||
if (c1 < c2)
|
||||
return -1;
|
||||
if (c1 > c2)
|
||||
return 1;
|
||||
|
||||
i1 += 2;
|
||||
++i2;
|
||||
}
|
||||
|
||||
if (i1 == n1 && i2 != n2)
|
||||
return -1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int compare(String other) throws CoreException {
|
||||
Chunk chunk = db.getChunk(record);
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2006, 2007 QNX Software Systems and others.
|
||||
* Copyright (c) 2006 QNX Software Systems and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
|
@ -14,74 +14,42 @@ import java.util.ArrayList;
|
|||
import java.util.List;
|
||||
|
||||
import org.eclipse.cdt.core.dom.ast.IBinding;
|
||||
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
|
||||
import org.eclipse.cdt.core.index.IndexFilter;
|
||||
import org.eclipse.cdt.internal.core.pdom.db.IBTreeVisitor;
|
||||
import org.eclipse.cdt.internal.core.pdom.db.IString;
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
|
||||
public final class FindBindingsInBTree implements IBTreeVisitor {
|
||||
protected final PDOMLinkage linkage;
|
||||
protected final char[] name;
|
||||
protected final boolean prefixLookup;
|
||||
private final PDOMLinkage linkage;
|
||||
private final char[] name;
|
||||
private final boolean prefixLookup;
|
||||
private IndexFilter filter;
|
||||
|
||||
private List bindings = new ArrayList();
|
||||
|
||||
/**
|
||||
* Collects all bindings with given name.
|
||||
*/
|
||||
public FindBindingsInBTree(PDOMLinkage linkage, char[] name) {
|
||||
this(linkage, name, null, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects all bindings with given name, passing the filter. If prefixLookup is set to
|
||||
* <code>true</code> a binding is considered if its name starts with the given prefix.
|
||||
*/
|
||||
public FindBindingsInBTree(PDOMLinkage linkage, char[] name, IndexFilter filter, boolean prefixLookup) {
|
||||
this.name = name;
|
||||
this.linkage= linkage;
|
||||
this.filter= filter;
|
||||
this.prefixLookup = prefixLookup;
|
||||
}
|
||||
|
||||
public int compare(int record) throws CoreException {
|
||||
PDOMNamedNode node = ((PDOMNamedNode)linkage.getNode(record));
|
||||
IString n = node.getDBName();
|
||||
if (prefixLookup && n.getString().startsWith(new String(name))) {
|
||||
return 0;
|
||||
if (prefixLookup) {
|
||||
return node.getDBName().comparePrefix(name);
|
||||
}
|
||||
return n.compare(name);
|
||||
}
|
||||
|
||||
private List bindings = new ArrayList();
|
||||
private final int[] desiredType;
|
||||
|
||||
/**
|
||||
* Matches all types.
|
||||
*
|
||||
* @param pdom
|
||||
* @param name
|
||||
*/
|
||||
public FindBindingsInBTree(PDOMLinkage linkage, char[] name) {
|
||||
this(linkage, name, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Match a specific type.
|
||||
*
|
||||
* @param pdom
|
||||
* @param name
|
||||
* @param desiredType
|
||||
*/
|
||||
public FindBindingsInBTree(PDOMLinkage linkage, char[] name, int desiredType) {
|
||||
this(linkage, name, new int[] { desiredType });
|
||||
}
|
||||
|
||||
/**
|
||||
* Match a collection of types.
|
||||
*
|
||||
* @param pdom
|
||||
* @param name
|
||||
* @param desiredType
|
||||
*/
|
||||
public FindBindingsInBTree(PDOMLinkage linkage, char[] name, int[] desiredType) {
|
||||
this(linkage, name, desiredType, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Match a collection of types.
|
||||
*
|
||||
* @param pdom
|
||||
* @param name
|
||||
* @param desiredType
|
||||
* @param prefixLookup
|
||||
*/
|
||||
public FindBindingsInBTree(PDOMLinkage linkage, char[] name, int[] desiredType, boolean prefixLookup) {
|
||||
this.name = name;
|
||||
this.desiredType = desiredType;
|
||||
this.linkage= linkage;
|
||||
this.prefixLookup = prefixLookup;
|
||||
return node.getDBName().compare(name);
|
||||
}
|
||||
|
||||
public boolean visit(int record) throws CoreException {
|
||||
|
@ -89,29 +57,13 @@ public final class FindBindingsInBTree implements IBTreeVisitor {
|
|||
return true;
|
||||
|
||||
PDOMBinding tBinding = linkage.getPDOM().getBinding(record);
|
||||
if ((!prefixLookup && !tBinding.hasName(name))
|
||||
|| (prefixLookup && !CharArrayUtils.equals(
|
||||
tBinding.getNameCharArray(),
|
||||
0, name.length, name, false)))
|
||||
return false;
|
||||
|
||||
if (desiredType == null) {
|
||||
if (filter == null || filter.acceptBinding(tBinding)) {
|
||||
bindings.add(tBinding);
|
||||
return true; // look for more
|
||||
} else {
|
||||
int nodeType = tBinding.getNodeType();
|
||||
for (int i = 0; i < desiredType.length; ++i)
|
||||
if (nodeType == desiredType[i]) {
|
||||
bindings.add(tBinding);
|
||||
return true; // look for more
|
||||
}
|
||||
}
|
||||
|
||||
// wrong type, try again
|
||||
return true;
|
||||
}
|
||||
return true; // look for more
|
||||
}
|
||||
|
||||
public IBinding[] getBinding() {
|
||||
public IBinding[] getBindings() {
|
||||
return (IBinding[])bindings.toArray(new IBinding[bindings.size()]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,9 +14,6 @@
|
|||
|
||||
package org.eclipse.cdt.internal.core.pdom.dom;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.eclipse.cdt.core.dom.ILinkage;
|
||||
import org.eclipse.cdt.core.dom.IName;
|
||||
import org.eclipse.cdt.core.dom.IPDOMVisitor;
|
||||
|
@ -32,7 +29,6 @@ import org.eclipse.cdt.core.dom.ast.IPointerType;
|
|||
import org.eclipse.cdt.core.dom.ast.IQualifierType;
|
||||
import org.eclipse.cdt.core.dom.ast.IScope;
|
||||
import org.eclipse.cdt.core.dom.ast.IType;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespaceScope;
|
||||
import org.eclipse.cdt.core.index.IIndexBinding;
|
||||
import org.eclipse.cdt.core.index.IIndexLinkage;
|
||||
|
@ -230,48 +226,11 @@ public abstract class PDOMLinkage extends PDOMNamedNode implements IIndexLinkage
|
|||
FindBindingsInBTree visitor= new FindBindingsInBTree(this, name);
|
||||
getIndex().accept(visitor);
|
||||
|
||||
return visitor.getBinding();
|
||||
}
|
||||
|
||||
private static class PrefixedBindingFinder implements IBTreeVisitor {
|
||||
private PDOMLinkage linkage;
|
||||
private String prefix;
|
||||
private IndexFilter filter;
|
||||
private List bindings = new ArrayList();
|
||||
|
||||
PrefixedBindingFinder(PDOMLinkage linkage, String prefix, IndexFilter filter) {
|
||||
this.linkage = linkage;
|
||||
this.prefix = prefix;
|
||||
this.filter = filter;
|
||||
}
|
||||
|
||||
public int compare(int record) throws CoreException {
|
||||
PDOMNamedNode node = (PDOMNamedNode) linkage.getNode(record);
|
||||
IString name = node.getDBName();
|
||||
if (name.getString().startsWith(prefix)) {
|
||||
return 0;
|
||||
}
|
||||
return name.compare(prefix);
|
||||
}
|
||||
|
||||
public boolean visit(int record) throws CoreException {
|
||||
PDOMBinding binding = linkage.getPDOM().getBinding(record);
|
||||
if (filter.acceptImplicitMethods() || !(binding instanceof ICPPMethod) ||
|
||||
!((ICPPMethod)binding).isImplicit()) {
|
||||
if (filter.acceptBinding(binding)) {
|
||||
bindings.add(binding);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public IBinding[] getBindings() {
|
||||
return (IBinding[]) bindings.toArray(new IBinding[bindings.size()]);
|
||||
}
|
||||
return visitor.getBindings();
|
||||
}
|
||||
|
||||
public IBinding[] findBindingsForPrefix(String prefix, IndexFilter filter) throws CoreException {
|
||||
PrefixedBindingFinder visitor = new PrefixedBindingFinder(this, prefix, filter);
|
||||
FindBindingsInBTree visitor = new FindBindingsInBTree(this, prefix.toCharArray(), filter, true);
|
||||
getIndex().accept(visitor);
|
||||
|
||||
return visitor.getBindings();
|
||||
|
|
|
@ -109,7 +109,7 @@ class PDOMCPPNamespace extends PDOMCPPBinding implements ICPPNamespace, ICPPName
|
|||
try {
|
||||
FindBindingsInBTree visitor = new FindBindingsInBTree(getLinkageImpl(), name.toCharArray(), null, prefixLookup);
|
||||
getIndex().accept(visitor);
|
||||
return visitor.getBinding();
|
||||
return visitor.getBindings();
|
||||
} catch (CoreException e) {
|
||||
CCorePlugin.log(e);
|
||||
}
|
||||
|
@ -121,7 +121,7 @@ class PDOMCPPNamespace extends PDOMCPPBinding implements ICPPNamespace, ICPPName
|
|||
FindBindingsInBTree visitor= new FindBindingsInBTree(getLinkageImpl(), name.toCharArray());
|
||||
getIndex().accept(visitor);
|
||||
|
||||
IBinding[] bindings= visitor.getBinding();
|
||||
IBinding[] bindings= visitor.getBindings();
|
||||
return CPPSemantics.resolveAmbiguities(name, bindings);
|
||||
} catch (CoreException e) {
|
||||
CCorePlugin.log(e);
|
||||
|
|
Loading…
Add table
Reference in a new issue