1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-09-08 11:03:28 +02:00

Got Open Type working on top of the PDOM.

This commit is contained in:
Doug Schaefer 2006-04-21 14:57:50 +00:00
parent 6870b4d629
commit 6f82c41296
5 changed files with 243 additions and 18 deletions

View file

@ -12,10 +12,26 @@ package org.eclipse.cdt.core.browser;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.IPDOMManager;
import org.eclipse.cdt.core.dom.IPDOMNode;
import org.eclipse.cdt.core.dom.IPDOMVisitor;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.gnu.c.GCCLanguage;
import org.eclipse.cdt.core.dom.ast.gnu.cpp.GPPLanguage;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICProject; import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.internal.core.browser.util.ArrayUtil; import org.eclipse.cdt.internal.core.browser.util.ArrayUtil;
import org.eclipse.cdt.internal.core.pdom.PDOM;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMBinding;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMLinkage;
import org.eclipse.cdt.internal.core.pdom.dom.c.PDOMCStructure;
import org.eclipse.cdt.internal.core.pdom.dom.cpp.PDOMCPPClassType;
import org.eclipse.core.runtime.CoreException;
/** /**
* Manages a search cache for types in the workspace. Instead of returning * Manages a search cache for types in the workspace. Instead of returning
@ -32,24 +48,117 @@ import org.eclipse.cdt.internal.core.browser.util.ArrayUtil;
*/ */
public class AllTypesCache { public class AllTypesCache {
private abstract static class TypesCollector implements IPDOMVisitor {
private final int[] kinds;
protected final List types;
protected final ICProject project;
protected TypesCollector(int[] kinds, List types, ICProject project) {
this.kinds = kinds;
this.types = types;
this.project = project;
}
protected abstract void visitKind(IPDOMNode node, int kind);
public boolean visit(IPDOMNode node) throws CoreException {
for (int i = 0; i < kinds.length; ++i)
visitKind(node, kinds[i]);
return true;
}
public List getTypes() {
return types;
}
}
private static class CTypesCollector extends TypesCollector {
public CTypesCollector(int[] kinds, List types, ICProject project) {
super(kinds, types, project);
}
protected void visitKind(IPDOMNode node, int kind) {
switch (kind) {
case ICElement.C_NAMESPACE:
return;
case ICElement.C_CLASS:
return;
case ICElement.C_STRUCT:
if (node instanceof PDOMCStructure)
types.add(new PDOMTypeInfo((PDOMBinding)node, kind, project));
return;
case ICElement.C_UNION:
return;
case ICElement.C_ENUMERATION:
return;
case ICElement.C_TYPEDEF:
return;
}
}
}
private static class CPPTypesCollector extends TypesCollector {
public CPPTypesCollector(int[] kinds, List types, ICProject project) {
super(kinds, types, project);
}
protected void visitKind(IPDOMNode node, int kind) {
try {
switch (kind) {
case ICElement.C_NAMESPACE:
return;
case ICElement.C_CLASS:
if (node instanceof PDOMCPPClassType
&& ((PDOMCPPClassType)node).getKey() == ICPPClassType.k_class)
types.add(new PDOMTypeInfo((PDOMBinding)node, kind, project));
return;
case ICElement.C_STRUCT:
if (node instanceof PDOMCPPClassType
&& ((PDOMCPPClassType)node).getKey() == ICPPClassType.k_struct)
types.add(new PDOMTypeInfo((PDOMBinding)node, kind, project));
return;
case ICElement.C_UNION:
if (node instanceof PDOMCPPClassType
&& ((PDOMCPPClassType)node).getKey() == ICPPClassType.k_union)
types.add(new PDOMTypeInfo((PDOMBinding)node, kind, project));
return;
case ICElement.C_ENUMERATION:
return;
case ICElement.C_TYPEDEF:
return;
}
} catch (DOMException e) {
CCorePlugin.log(e);
}
}
}
/** /**
* Returns all types in the workspace. * Returns all types in the workspace.
*/ */
public static ITypeInfo[] getAllTypes() { public static ITypeInfo[] getAllTypes() {
final Collection fAllTypes = new ArrayList(); try {
TypeSearchScope workspaceScope = new TypeSearchScope(true); List types = new ArrayList();
ICProject[] projects = workspaceScope.getEnclosingProjects(); IPDOMManager pdomManager = CCorePlugin.getPDOMManager();
ITypeInfoVisitor visitor = new ITypeInfoVisitor() {
public boolean visit(ITypeInfo info) { ICProject[] projects = CoreModel.getDefault().getCModel().getCProjects();
fAllTypes.add(info); for (int i = 0; i < projects.length; ++i) {
return true; ICProject project = projects[i];
CTypesCollector cCollector = new CTypesCollector(ITypeInfo.KNOWN_TYPES, types, project);
CPPTypesCollector cppCollector = new CPPTypesCollector(ITypeInfo.KNOWN_TYPES, types, project);
PDOM pdom = (PDOM)pdomManager.getPDOM(project);
PDOMLinkage cLinkage = pdom.getLinkage(GCCLanguage.getDefault());
cLinkage.accept(cCollector);
PDOMLinkage cppLinkage = pdom.getLinkage(GPPLanguage.getDefault());
cppLinkage.accept(cppCollector);
} }
public boolean shouldContinue() { return true; }
}; return (ITypeInfo[])types.toArray(new ITypeInfo[types.size()]);
for (int i = 0; i < projects.length; ++i) { } catch (CoreException e) {
// TypeCacheManager.getInstance().getCache(projects[i]).accept(visitor); CCorePlugin.log(e);
return new ITypeInfo[0];
} }
return (ITypeInfo[]) fAllTypes.toArray(new ITypeInfo[fAllTypes.size()]);
} }
/** /**

View file

@ -11,10 +11,13 @@
package org.eclipse.cdt.core.browser; package org.eclipse.cdt.core.browser;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.model.ICProject; import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.parser.ast.ASTAccessVisibility; import org.eclipse.cdt.core.parser.ast.ASTAccessVisibility;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMBinding; import org.eclipse.cdt.internal.core.pdom.dom.PDOMBinding;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMName;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMNotImplementedError; import org.eclipse.cdt.internal.core.pdom.dom.PDOMNotImplementedError;
import org.eclipse.core.runtime.CoreException;
/** /**
* @author Doug Schaefer * @author Doug Schaefer
@ -24,10 +27,12 @@ public class PDOMTypeInfo implements ITypeInfo {
private final PDOMBinding binding; private final PDOMBinding binding;
private final int elementType; private final int elementType;
private final ICProject project;
public PDOMTypeInfo(PDOMBinding binding, int elementType) { public PDOMTypeInfo(PDOMBinding binding, int elementType, ICProject project) {
this.binding = binding; this.binding = binding;
this.elementType = elementType; this.elementType = elementType;
this.project = project;
} }
public void addDerivedReference(ITypeReference location) { public void addDerivedReference(ITypeReference location) {
@ -71,11 +76,12 @@ public class PDOMTypeInfo implements ITypeInfo {
} }
public ICProject getEnclosingProject() { public ICProject getEnclosingProject() {
throw new PDOMNotImplementedError(); return project;
} }
public ITypeInfo getEnclosingType() { public ITypeInfo getEnclosingType() {
throw new PDOMNotImplementedError(); // TODO not sure
return null;
} }
public ITypeInfo getEnclosingType(int[] kinds) { public ITypeInfo getEnclosingType(int[] kinds) {
@ -83,11 +89,13 @@ public class PDOMTypeInfo implements ITypeInfo {
} }
public String getName() { public String getName() {
throw new PDOMNotImplementedError(); return binding.getName();
} }
public IQualifiedTypeName getQualifiedTypeName() { public IQualifiedTypeName getQualifiedTypeName() {
throw new PDOMNotImplementedError(); String bindingName = binding.getName();
// TODO really do qualified type names
return new QualifiedTypeName(bindingName);
} }
public ITypeReference[] getReferences() { public ITypeReference[] getReferences() {
@ -95,7 +103,13 @@ public class PDOMTypeInfo implements ITypeInfo {
} }
public ITypeReference getResolvedReference() { public ITypeReference getResolvedReference() {
throw new PDOMNotImplementedError(); try {
PDOMName name = binding.getFirstDefinition();
return name != null ? new PDOMTypeReference(name, project) : null;
} catch (CoreException e) {
CCorePlugin.log(e);
return null;
}
} }
public ITypeInfo getRootNamespace(boolean includeGlobalNamespace) { public ITypeInfo getRootNamespace(boolean includeGlobalNamespace) {

View file

@ -0,0 +1,90 @@
/*******************************************************************************
* 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
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* QNX - Initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.core.browser;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.model.IWorkingCopy;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMName;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMNotImplementedError;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
/**
* @author Doug Schaefer
*
*/
public class PDOMTypeReference implements ITypeReference {
private final PDOMName name;
private final ICProject project;
private final IPath path;
public PDOMTypeReference(PDOMName name, ICProject project) {
this.name = name;
this.project = project;
this.path = new Path(name.getFileLocation().getFileName());
}
public ICElement[] getCElements() {
throw new PDOMNotImplementedError();
}
public int getLength() {
return name.getFileLocation().getNodeLength();
}
public IPath getLocation() {
throw new PDOMNotImplementedError();
}
public int getOffset() {
return name.getFileLocation().getNodeOffset();
}
public IPath getPath() {
return path;
}
public IProject getProject() {
throw new PDOMNotImplementedError();
}
public IPath getRelativeIncludePath(IProject project) {
throw new PDOMNotImplementedError();
}
public IPath getRelativePath(IPath relativeToPath) {
throw new PDOMNotImplementedError();
}
public IResource getResource() {
throw new PDOMNotImplementedError();
}
public ITranslationUnit getTranslationUnit() {
return CoreModel.getDefault().createTranslationUnitFrom(project, path);
}
public IWorkingCopy getWorkingCopy() {
throw new PDOMNotImplementedError();
}
public boolean isLineNumber() {
return name.getFileLocation().getNodeLength() == -1;
}
}

View file

@ -61,6 +61,12 @@ public class GCCLanguage extends PlatformObject implements ILanguage {
// Must match the id in the extension // Must match the id in the extension
public static final String ID = CCorePlugin.PLUGIN_ID + ".gcc"; //$NON-NLS-1$ public static final String ID = CCorePlugin.PLUGIN_ID + ".gcc"; //$NON-NLS-1$
private static final GCCLanguage myDefault = new GCCLanguage();
public static GCCLanguage getDefault() {
return myDefault;
}
public String getId() { public String getId() {
return ID; return ID;
} }

View file

@ -59,6 +59,12 @@ public class GPPLanguage extends PlatformObject implements ILanguage {
protected static final GPPScannerExtensionConfiguration CPP_GNU_SCANNER_EXTENSION = new GPPScannerExtensionConfiguration(); protected static final GPPScannerExtensionConfiguration CPP_GNU_SCANNER_EXTENSION = new GPPScannerExtensionConfiguration();
public static final String ID = CCorePlugin.PLUGIN_ID + ".g++"; //$NON-NLS-1$ public static final String ID = CCorePlugin.PLUGIN_ID + ".g++"; //$NON-NLS-1$
private static final GPPLanguage myDefault = new GPPLanguage();
public static GPPLanguage getDefault() {
return myDefault;
}
public String getId() { public String getId() {
return ID; return ID;