mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-06-08 18:26:01 +02:00
PDOM - Extended resolveBinding and openDeclaration to C.
This commit is contained in:
parent
58f26dfed6
commit
e39bead08b
4 changed files with 91 additions and 57 deletions
|
@ -12,7 +12,10 @@
|
||||||
package org.eclipse.cdt.internal.core.pdom.dom.c;
|
package org.eclipse.cdt.internal.core.pdom.dom.c;
|
||||||
|
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
|
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.IASTFunctionCallExpression;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTName;
|
import org.eclipse.cdt.core.dom.ast.IASTName;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.IASTNamedTypeSpecifier;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTNode;
|
import org.eclipse.cdt.core.dom.ast.IASTNode;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
||||||
import org.eclipse.cdt.core.dom.ast.IBinding;
|
import org.eclipse.cdt.core.dom.ast.IBinding;
|
||||||
|
@ -23,6 +26,7 @@ import org.eclipse.cdt.core.dom.ast.IParameter;
|
||||||
import org.eclipse.cdt.core.dom.ast.IProblemBinding;
|
import org.eclipse.cdt.core.dom.ast.IProblemBinding;
|
||||||
import org.eclipse.cdt.core.dom.ast.IScope;
|
import org.eclipse.cdt.core.dom.ast.IScope;
|
||||||
import org.eclipse.cdt.core.dom.ast.IVariable;
|
import org.eclipse.cdt.core.dom.ast.IVariable;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.c.ICASTElaboratedTypeSpecifier;
|
||||||
import org.eclipse.cdt.core.dom.ast.gnu.c.GCCLanguage;
|
import org.eclipse.cdt.core.dom.ast.gnu.c.GCCLanguage;
|
||||||
import org.eclipse.cdt.internal.core.pdom.PDOMDatabase;
|
import org.eclipse.cdt.internal.core.pdom.PDOMDatabase;
|
||||||
import org.eclipse.cdt.internal.core.pdom.dom.PDOMBinding;
|
import org.eclipse.cdt.internal.core.pdom.dom.PDOMBinding;
|
||||||
|
@ -108,44 +112,46 @@ public class PDOMCLinkage extends PDOMLinkage {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final class FindBinding extends PDOMNode.NodeVisitor {
|
private static final class FindBinding extends PDOMNode.NodeVisitor {
|
||||||
private final IBinding binding;
|
PDOMBinding pdomBinding;
|
||||||
public PDOMBinding pdomBinding;
|
final int desiredType;
|
||||||
public FindBinding(PDOMDatabase pdom, IBinding binding) {
|
public FindBinding(PDOMDatabase pdom, char[] name, int desiredType) {
|
||||||
super(pdom, binding.getNameCharArray());
|
super(pdom, name);
|
||||||
this.binding = binding;
|
this.desiredType = desiredType;
|
||||||
}
|
}
|
||||||
public boolean visit(int record) throws CoreException {
|
public boolean visit(int record) throws CoreException {
|
||||||
if (record == 0)
|
if (record == 0)
|
||||||
return true;
|
return true;
|
||||||
PDOMBinding tBinding = pdom.getBinding(record);
|
PDOMBinding tBinding = pdom.getBinding(record);
|
||||||
if (!tBinding.hasName(name))
|
if (!tBinding.hasName(name))
|
||||||
|
// no more bindings with our desired name
|
||||||
return false;
|
return false;
|
||||||
switch (tBinding.getBindingType()) {
|
if (tBinding.getBindingType() != desiredType)
|
||||||
case CVARIABLE:
|
// wrong type, try again
|
||||||
if (binding instanceof IVariable)
|
return true;
|
||||||
pdomBinding = tBinding;
|
|
||||||
break;
|
// got it
|
||||||
case CFUNCTION:
|
pdomBinding = tBinding;
|
||||||
if (binding instanceof IFunction)
|
return false;
|
||||||
pdomBinding = tBinding;
|
|
||||||
break;
|
|
||||||
case CSTRUCTURE:
|
|
||||||
if (binding instanceof ICompositeType)
|
|
||||||
pdomBinding = tBinding;
|
|
||||||
break;
|
|
||||||
case CFIELD:
|
|
||||||
if (binding instanceof IField)
|
|
||||||
pdomBinding = tBinding;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return pdomBinding == null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected int getBindingType(IBinding binding) {
|
||||||
|
if (binding instanceof IVariable)
|
||||||
|
return CVARIABLE;
|
||||||
|
else if (binding instanceof IFunction)
|
||||||
|
return CFUNCTION;
|
||||||
|
else if (binding instanceof ICompositeType)
|
||||||
|
return CSTRUCTURE;
|
||||||
|
else if (binding instanceof IField)
|
||||||
|
return CFIELD;
|
||||||
|
else
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
public PDOMBinding adaptBinding(IBinding binding) throws CoreException {
|
public PDOMBinding adaptBinding(IBinding binding) throws CoreException {
|
||||||
PDOMNode parent = getParent(binding);
|
PDOMNode parent = getParent(binding);
|
||||||
if (parent == this) {
|
if (parent == this) {
|
||||||
FindBinding visitor = new FindBinding(pdom, binding);
|
FindBinding visitor = new FindBinding(pdom, binding.getNameCharArray(), getBindingType(binding));
|
||||||
getIndex().visit(visitor);
|
getIndex().visit(visitor);
|
||||||
return visitor.pdomBinding;
|
return visitor.pdomBinding;
|
||||||
} else if (parent instanceof PDOMMemberOwner) {
|
} else if (parent instanceof PDOMMemberOwner) {
|
||||||
|
@ -179,6 +185,24 @@ public class PDOMCLinkage extends PDOMLinkage {
|
||||||
}
|
}
|
||||||
|
|
||||||
public PDOMBinding resolveBinding(IASTName name) throws CoreException {
|
public PDOMBinding resolveBinding(IASTName name) throws CoreException {
|
||||||
|
IASTNode parent = name.getParent();
|
||||||
|
if (parent instanceof IASTIdExpression) {
|
||||||
|
// reference
|
||||||
|
IASTNode eParent = parent.getParent();
|
||||||
|
if (eParent instanceof IASTFunctionCallExpression) {
|
||||||
|
FindBinding visitor = new FindBinding(pdom, name.toCharArray(), CFUNCTION);
|
||||||
|
getIndex().visit(visitor);
|
||||||
|
return visitor.pdomBinding;
|
||||||
|
} else {
|
||||||
|
FindBinding visitor = new FindBinding(pdom, name.toCharArray(), CVARIABLE);
|
||||||
|
getIndex().visit(visitor);
|
||||||
|
return visitor.pdomBinding;
|
||||||
|
}
|
||||||
|
} else if (parent instanceof ICASTElaboratedTypeSpecifier) {
|
||||||
|
FindBinding visitor = new FindBinding(pdom, name.toCharArray(), CSTRUCTURE);
|
||||||
|
getIndex().visit(visitor);
|
||||||
|
return visitor.pdomBinding;
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
|
|
||||||
package org.eclipse.cdt.internal.core.pdom.dom.c;
|
package org.eclipse.cdt.internal.core.pdom.dom.c;
|
||||||
|
|
||||||
|
import org.eclipse.cdt.core.CCorePlugin;
|
||||||
import org.eclipse.cdt.core.dom.ast.DOMException;
|
import org.eclipse.cdt.core.dom.ast.DOMException;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTName;
|
import org.eclipse.cdt.core.dom.ast.IASTName;
|
||||||
import org.eclipse.cdt.core.dom.ast.ICompositeType;
|
import org.eclipse.cdt.core.dom.ast.ICompositeType;
|
||||||
|
@ -18,6 +19,7 @@ import org.eclipse.cdt.core.dom.ast.IField;
|
||||||
import org.eclipse.cdt.core.dom.ast.IScope;
|
import org.eclipse.cdt.core.dom.ast.IScope;
|
||||||
import org.eclipse.cdt.core.dom.ast.IType;
|
import org.eclipse.cdt.core.dom.ast.IType;
|
||||||
import org.eclipse.cdt.internal.core.pdom.PDOMDatabase;
|
import org.eclipse.cdt.internal.core.pdom.PDOMDatabase;
|
||||||
|
import org.eclipse.cdt.internal.core.pdom.dom.PDOMMember;
|
||||||
import org.eclipse.cdt.internal.core.pdom.dom.PDOMMemberOwner;
|
import org.eclipse.cdt.internal.core.pdom.dom.PDOMMemberOwner;
|
||||||
import org.eclipse.cdt.internal.core.pdom.dom.PDOMNode;
|
import org.eclipse.cdt.internal.core.pdom.dom.PDOMNode;
|
||||||
import org.eclipse.cdt.internal.core.pdom.dom.PDOMNotImplementedError;
|
import org.eclipse.cdt.internal.core.pdom.dom.PDOMNotImplementedError;
|
||||||
|
@ -50,7 +52,13 @@ public class PDOMCStructure extends PDOMMemberOwner implements ICompositeType {
|
||||||
}
|
}
|
||||||
|
|
||||||
public IField findField(String name) throws DOMException {
|
public IField findField(String name) throws DOMException {
|
||||||
throw new PDOMNotImplementedError();
|
try {
|
||||||
|
PDOMMember[] members = findMembers(name.toCharArray());
|
||||||
|
return (PDOMCField)members[0];
|
||||||
|
} catch (CoreException e) {
|
||||||
|
CCorePlugin.log(e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IScope getCompositeScope() throws DOMException {
|
public IScope getCompositeScope() throws DOMException {
|
||||||
|
|
|
@ -139,7 +139,7 @@ public class PDOMCPPLinkage extends PDOMLinkage {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getBindingType(IBinding binding) {
|
protected int getBindingType(IBinding binding) {
|
||||||
if (binding instanceof ICPPVariable)
|
if (binding instanceof ICPPVariable)
|
||||||
return CPPVARIABLE;
|
return CPPVARIABLE;
|
||||||
else if (binding instanceof ICPPFunction)
|
else if (binding instanceof ICPPFunction)
|
||||||
|
|
|
@ -350,38 +350,40 @@ public class DOMSearchUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
IBinding binding = searchName.resolveBinding();
|
IBinding binding = searchName.resolveBinding();
|
||||||
names = getNames(tu, binding, limitTo);
|
if (binding instanceof PDOMBinding) {
|
||||||
|
try {
|
||||||
if (names == null || names.length == 0) { // try alternate strategies
|
ArrayList pdomNames = new ArrayList();
|
||||||
try {
|
// First decls
|
||||||
// fix for 86829, 95224
|
PDOMName name = ((PDOMBinding)binding).getFirstDeclaration();
|
||||||
if ((binding instanceof ICPPConstructor || (binding instanceof ICPPMethod && ((ICPPMethod)binding).isDestructor()))
|
while (name != null) {
|
||||||
&& binding.getScope() instanceof ICPPClassScope) {
|
pdomNames.add(name);
|
||||||
binding = ((ICPPClassScope)binding.getScope()).getClassType();
|
name = name.getNextInBinding();
|
||||||
names = getNames(tu, binding, limitTo);
|
|
||||||
} else if (binding instanceof PDOMBinding) {
|
|
||||||
try {
|
|
||||||
ArrayList pdomNames = new ArrayList();
|
|
||||||
// First decls
|
|
||||||
PDOMName name = ((PDOMBinding)binding).getFirstDeclaration();
|
|
||||||
while (name != null) {
|
|
||||||
pdomNames.add(name);
|
|
||||||
name = name.getNextInBinding();
|
|
||||||
}
|
|
||||||
// Next defs
|
|
||||||
name = ((PDOMBinding)binding).getFirstDefinition();
|
|
||||||
while (name != null) {
|
|
||||||
pdomNames.add(name);
|
|
||||||
name = name.getNextInBinding();
|
|
||||||
}
|
|
||||||
names = (IASTName[])pdomNames.toArray(new IASTName[pdomNames.size()]);
|
|
||||||
} catch (CoreException e) {
|
|
||||||
CCorePlugin.log(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (DOMException e) {}
|
// Next defs
|
||||||
|
name = ((PDOMBinding)binding).getFirstDefinition();
|
||||||
|
while (name != null) {
|
||||||
|
pdomNames.add(name);
|
||||||
|
name = name.getNextInBinding();
|
||||||
|
}
|
||||||
|
names = (IASTName[])pdomNames.toArray(new IASTName[pdomNames.size()]);
|
||||||
|
} catch (CoreException e) {
|
||||||
|
CCorePlugin.log(e);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
names = getNames(tu, binding, limitTo);
|
||||||
|
|
||||||
|
if (names == null || names.length == 0) { // try alternate strategies
|
||||||
|
try {
|
||||||
|
// fix for 86829, 95224
|
||||||
|
if ((binding instanceof ICPPConstructor || (binding instanceof ICPPMethod && ((ICPPMethod)binding).isDestructor()))
|
||||||
|
&& binding.getScope() instanceof ICPPClassScope) {
|
||||||
|
binding = ((ICPPClassScope)binding.getScope()).getClassType();
|
||||||
|
names = getNames(tu, binding, limitTo);
|
||||||
|
}
|
||||||
|
} catch (DOMException e) {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return names;
|
return names;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue