From dd464d51a30120042622a42aa4e3f18d31138607 Mon Sep 17 00:00:00 2001 From: Markus Schorn Date: Wed, 22 Nov 2006 12:44:33 +0000 Subject: [PATCH] CModel: Extension to represent names from the index --- .../org.eclipse.cdt.core/META-INF/MANIFEST.MF | 3 + .../cdt/internal/core/model/CElement.java | 50 ++-- .../core/model/FunctionDeclaration.java | 51 ++-- .../core/model/MethodDeclaration.java | 23 +- .../core/model/ext/CElementHandle.java | 277 ++++++++++++++++++ .../core/model/ext/CElementHandleFactory.java | 111 +++++++ .../core/model/ext/EnumerationHandle.java | 22 ++ .../core/model/ext/EnumeratorHandle.java | 26 ++ .../internal/core/model/ext/FieldHandle.java | 22 ++ .../core/model/ext/FunctionHandle.java | 48 +++ .../core/model/ext/ICElementHandle.java | 17 ++ .../internal/core/model/ext/MethodHandle.java | 49 ++++ .../core/model/ext/NamespaceHandle.java | 28 ++ .../internal/core/model/ext/SourceRange.java | 49 ++++ .../core/model/ext/StructureHandle.java | 67 +++++ .../core/model/ext/VariableHandle.java | 21 ++ .../cdt/core/IPositionTrackerManager.java | 10 + .../internal/core/PositionTrackerManager.java | 16 + 18 files changed, 844 insertions(+), 46 deletions(-) create mode 100644 core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/CElementHandle.java create mode 100644 core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/CElementHandleFactory.java create mode 100644 core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/EnumerationHandle.java create mode 100644 core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/EnumeratorHandle.java create mode 100644 core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/FieldHandle.java create mode 100644 core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/FunctionHandle.java create mode 100644 core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/ICElementHandle.java create mode 100644 core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/MethodHandle.java create mode 100644 core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/NamespaceHandle.java create mode 100644 core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/SourceRange.java create mode 100644 core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/StructureHandle.java create mode 100644 core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/VariableHandle.java diff --git a/core/org.eclipse.cdt.core/META-INF/MANIFEST.MF b/core/org.eclipse.cdt.core/META-INF/MANIFEST.MF index d5be7033a7c..f071ab7bbd7 100644 --- a/core/org.eclipse.cdt.core/META-INF/MANIFEST.MF +++ b/core/org.eclipse.cdt.core/META-INF/MANIFEST.MF @@ -28,11 +28,13 @@ Export-Package: org.eclipse.cdt.core, org.eclipse.cdt.internal.core;x-friends:="org.eclipse.cdt.ui", org.eclipse.cdt.internal.core.browser.util;x-internal:=true, org.eclipse.cdt.internal.core.dom;x-friends:="org.eclipse.cdt.ui,org.eclipse.cdt.refactoring", + org.eclipse.cdt.internal.core.dom.bid, org.eclipse.cdt.internal.core.dom.parser;x-friends:="org.eclipse.cdt.refactoring", org.eclipse.cdt.internal.core.dom.parser.c;x-friends:="org.eclipse.cdt.refactoring", org.eclipse.cdt.internal.core.dom.parser.cpp;x-friends:="org.eclipse.cdt.ui,org.eclipse.cdt.refactoring", org.eclipse.cdt.internal.core.index;x-internal:=true, org.eclipse.cdt.internal.core.model;x-friends:="org.eclipse.cdt.ui,org.eclipse.cdt.refactoring", + org.eclipse.cdt.internal.core.model.ext;x-friends:="org.eclipse.cdt.ui", org.eclipse.cdt.internal.core.parser;x-internal:=true, org.eclipse.cdt.internal.core.parser.ast;x-internal:=true, org.eclipse.cdt.internal.core.parser.ast.complete;x-internal:=true, @@ -49,6 +51,7 @@ Export-Package: org.eclipse.cdt.core, org.eclipse.cdt.internal.core.pdom.dom;x-friends:="org.eclipse.cdt.ui", org.eclipse.cdt.internal.core.pdom.dom.c;x-internal:=true, org.eclipse.cdt.internal.core.pdom.dom.cpp;x-internal:=true, + org.eclipse.cdt.internal.core.pdom.indexer;x-internal:=true, org.eclipse.cdt.internal.core.pdom.indexer.fast;x-internal:=true, org.eclipse.cdt.internal.core.pdom.indexer.full;x-internal:=true, org.eclipse.cdt.internal.core.pdom.indexer.nulli;x-internal:=true, diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CElement.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CElement.java index 8908e9544ea..71da027537f 100644 --- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CElement.java +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CElement.java @@ -234,24 +234,31 @@ public abstract class CElement extends PlatformObject implements ICElement { public boolean equals (Object o) { if (this == o) return true; - if (o instanceof CElement) { - CElement other = (CElement) o; - if( fName == null || other.fName == null ) - return false; - if( fName.length() == 0 || other.fName.length() == 0 ) - return false; - if (fType != other.fType) - return false; - if (fName.equals(other.fName)) { - if (fParent != null && fParent.equals(other.fParent)) { - return true; - } - if (fParent == null && other.fParent == null) - return true; - } + if (o instanceof ICElement) { + return equals(this, (ICElement) o); } return false; } + + public static boolean equals(ICElement lhs, ICElement rhs) { + if (lhs.getElementType() != rhs.getElementType()) { + return false; + } + String lhsName= lhs.getElementName(); + String rhsName= rhs.getElementName(); + if( lhsName == null || rhsName == null || lhsName.length() == 0 || + !lhsName.equals(rhsName)) { + return false; + } + + ICElement lhsParent= lhs.getParent(); + ICElement rhsParent= rhs.getParent(); + if (lhsParent == rhsParent) { + return true; + } + + return lhsParent != null && lhsParent.equals(rhsParent); + } public CElementInfo getElementInfo() throws CModelException { return getElementInfo(null); @@ -453,15 +460,22 @@ public abstract class CElement extends PlatformObject implements ICElement { // CHECKPOINT: making not equal objects seem equal // What elements should override this? public int hashCode() { - if (fParent == null) return super.hashCode(); - return Util.combineHashCodes(fName.hashCode(), fParent.hashCode()); + return hashCode(this); + } + + public static int hashCode(ICElement elem) { + ICElement parent= elem.getParent(); + if (parent == null) { + return System.identityHashCode(elem); + } + return Util.combineHashCodes(elem.getElementName().hashCode(), parent.hashCode()); } /* * Test to see if two objects are identical * Subclasses should override accordingly */ - public boolean isIdentical( CElement otherElement){ + public boolean isIdentical(CElement otherElement){ return this.equals(otherElement); } diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/FunctionDeclaration.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/FunctionDeclaration.java index c48bf8e2a6f..7de25d219ea 100644 --- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/FunctionDeclaration.java +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/FunctionDeclaration.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2002, 2005 IBM Corporation and others. + * Copyright (c) 2002, 2006 IBM Corporation 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 @@ -7,6 +7,7 @@ * * Contributors: * Rational Software - Initial API and implementation + * Markus Schorn (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.internal.core.model; @@ -53,22 +54,32 @@ public class FunctionDeclaration extends SourceManipulation implements IFunction } public String getSignature() throws CModelException{ - StringBuffer sig = new StringBuffer(getElementName()); - sig.append(getParameterClause()); - if(isConst()) - sig.append(" const"); //$NON-NLS-1$ - if(isVolatile()) { - sig.append(" volatile"); //$NON-NLS-1$ - } + return getSignature(this); + } + + public static String getSignature(IFunctionDeclaration func) { + StringBuffer sig = new StringBuffer(func.getElementName()); + sig.append(getParameterClause(func.getParameterTypes())); + try { + if(func.isConst()) + sig.append(" const"); //$NON-NLS-1$ + if(func.isVolatile()) { + sig.append(" volatile"); //$NON-NLS-1$ + } + } catch (CModelException e) { + } return sig.toString(); } - public String getParameterClause(){ + public String getParameterClause() { + return getParameterClause(getParameterTypes()); + } + + public static String getParameterClause(String[] paramTypes){ StringBuffer sig = new StringBuffer(); - if(getNumberOfParameters() > 0){ + if(paramTypes.length > 0){ sig.append("("); //$NON-NLS-1$ - String[] paramTypes = getParameterTypes(); int i = 0; sig.append(paramTypes[i++]); while (i < paramTypes.length){ @@ -100,14 +111,16 @@ public class FunctionDeclaration extends SourceManipulation implements IFunction } public boolean equals(Object other) { - // Two function declarations are equal if - // Their parents and names are equal and - return ( super.equals(other) - // their parameter types are equal and - && Util.equalArraysOrNull(fParameterTypes, ((FunctionDeclaration)other).fParameterTypes) - // their return types are equal - && getReturnType().equals(((FunctionDeclaration)other).getReturnType()) - ); + if (other instanceof IFunctionDeclaration) { + return equals(this, (IFunctionDeclaration) other); + } + return false; + } + + public static boolean equals(IFunctionDeclaration lhs, IFunctionDeclaration rhs) { + return CElement.equals(lhs, rhs) && + Util.equalArraysOrNull(lhs.getParameterTypes(), rhs.getParameterTypes()) && + lhs.getReturnType().equals(rhs.getReturnType()); } /** diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/MethodDeclaration.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/MethodDeclaration.java index 24a48313f0c..6b2f7317894 100644 --- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/MethodDeclaration.java +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/MethodDeclaration.java @@ -7,6 +7,7 @@ * * Contributors: * Rational Software - Initial API and implementation + * Markus Schorn (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.internal.core.model; @@ -111,16 +112,20 @@ public class MethodDeclaration extends FunctionDeclaration implements IMethodDec return (MethodInfo) getElementInfo(); } - /* - * See if we need anything else to put in equals here - */ public boolean equals(Object other) { - // Two methods are equal if - // their parents, names, parameter types and return types are equal and - return ( super.equals(other) - // their constant directive is the same - && isConst() == ((MethodDeclaration)other).isConst() - ); + if (other instanceof IMethodDeclaration) { + return equals(this, (IMethodDeclaration) other); + } + return false; + } + + public static boolean equals(IMethodDeclaration lhs, IMethodDeclaration rhs) { + try { + return lhs.isConst() == rhs.isConst() && + FunctionDeclaration.equals(lhs, rhs); + } catch (CModelException e) { + return false; + } } diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/CElementHandle.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/CElementHandle.java new file mode 100644 index 00000000000..8bcbe8a3186 --- /dev/null +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/CElementHandle.java @@ -0,0 +1,277 @@ +/******************************************************************************* + * Copyright (c) 2006 Wind River Systems, Inc. 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: + * Markus Schorn - initial API and implementation + *******************************************************************************/ + +package org.eclipse.cdt.internal.core.model.ext; + +import java.util.Collections; +import java.util.List; + +import org.eclipse.cdt.core.CCorePlugin; +import org.eclipse.cdt.core.IPositionConverter; +import org.eclipse.cdt.core.dom.ast.ASTTypeUtil; +import org.eclipse.cdt.core.dom.ast.DOMException; +import org.eclipse.cdt.core.dom.ast.IFunction; +import org.eclipse.cdt.core.dom.ast.IParameter; +import org.eclipse.cdt.core.model.CModelException; +import org.eclipse.cdt.core.model.ICElement; +import org.eclipse.cdt.core.model.ICElementVisitor; +import org.eclipse.cdt.core.model.ICModel; +import org.eclipse.cdt.core.model.ICProject; +import org.eclipse.cdt.core.model.ISourceRange; +import org.eclipse.cdt.core.model.ISourceReference; +import org.eclipse.cdt.core.model.ITranslationUnit; +import org.eclipse.cdt.core.parser.ast.ASTAccessVisibility; +import org.eclipse.cdt.internal.core.model.CElement; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.jface.text.IRegion; + +abstract class CElementHandle implements ICElementHandle, ISourceReference { + protected static final String[] EMPTY_STRING_ARRAY = new String[0]; + private static final ICElement[] NO_CHILDREN = new ICElement[0]; + + private ICElement fParent; + private String fName; + private int fType; + + private IRegion fRegion; + private long fTimestamp; + + public CElementHandle(ICElement parent, int type, String name) { + fParent= parent; + fType= type; + fName= name; + } + + public boolean equals(Object obj) { + if (obj instanceof ICElement) { + return CElement.equals(this, (ICElement) obj); + } + return false; + } + + public int hashCode() { + return CElement.hashCode(this); + } + + + public Object getAdapter(Class adapter) { + return null; + } + + public void setRangeOfID(IRegion region, long timestamp) { + fRegion= region; + fTimestamp= timestamp; + } + + public ISourceRange getSourceRange() throws CModelException { + IRegion region= fRegion; + ITranslationUnit tu= getTranslationUnit(); + if (tu != null) { + IPositionConverter converter= CCorePlugin.getPositionTrackerManager().findPositionConverter(tu, fTimestamp); + if (converter != null) { + region= converter.historicToActual(region); + } + } + int startpos= region.getOffset(); + int length= region.getLength(); + return new SourceRange(startpos, length); + } + + public String getSource() throws CModelException { + return null; + } + + public ITranslationUnit getTranslationUnit() { + ICElement parent= fParent; + do { + if (parent instanceof ITranslationUnit) { + return (ITranslationUnit) parent; + } + parent= parent.getParent(); + } + while (parent != null); + return null; + } + + public void accept(ICElementVisitor visitor) throws CoreException { + } + + public boolean exists() { + return true; + } + + public ICElement getAncestor(int ancestorType) { + return null; + } + + public ICModel getCModel() { + return fParent.getCModel(); + } + + public ICProject getCProject() { + return fParent.getCProject(); + } + + public String getElementName() { + return fName; + } + + public int getElementType() { + return fType; + } + + public ICElement getParent() { + return fParent; + } + + public IPath getPath() { + return getTranslationUnit().getPath(); + } + + public IResource getResource() { + return getTranslationUnit().getResource(); + } + + public IResource getUnderlyingResource() { + return getResource(); + } + + public boolean isReadOnly() { + return true; + } + + public boolean isStructureKnown() throws CModelException { + return false; + } + + public void copy(ICElement container, ICElement sibling, String rename, boolean replace, IProgressMonitor monitor) throws CModelException { + } + + public void delete(boolean force, IProgressMonitor monitor) throws CModelException { + } + + public void move(ICElement container, ICElement sibling, String rename, boolean replace, IProgressMonitor monitor) throws CModelException { + } + + public void rename(String name, boolean replace, IProgressMonitor monitor) throws CModelException { + } + + public void setTypeName(String type) throws CModelException { + } + + public String getTypeName() { + return ""; //$NON-NLS-1$ + } + + public ICElement[] getChildren() throws CModelException { + return NO_CHILDREN; + } + + public List getChildrenOfType(int type) throws CModelException { + return Collections.EMPTY_LIST; + } + + public boolean hasChildren() { + return false; + } + + public String[] getExceptions() { + return EMPTY_STRING_ARRAY; + } + + public String getParameterInitializer(int pos) { + return ""; //$NON-NLS-1$ + } + + public String getReturnType() { + return ""; //$NON-NLS-1$ + } + + public boolean isConst() { + return false; + } + + public boolean isStatic() throws CModelException { + return false; + } + + public boolean isVolatile() throws CModelException { + return false; + } + + public boolean isFriend() throws CModelException { + return false; + } + + public boolean isConstructor() throws CModelException { + return false; + } + + public boolean isDestructor() throws CModelException { + return false; + } + + public boolean isInline() throws CModelException { + return false; + } + + public boolean isOperator() throws CModelException { + return false; + } + + public boolean isPureVirtual() throws CModelException { + return false; + } + + public boolean isVirtual() throws CModelException { + return false; + } + + public ASTAccessVisibility getVisibility() throws CModelException { + return ASTAccessVisibility.PUBLIC; + } + + public boolean isMutable() throws CModelException { + return false; + } + + public String getInitializer() { + return null; + } + + public boolean isAbstract() throws CModelException { + return false; + } + + public ASTAccessVisibility getSuperClassAccess(String name) { + return ASTAccessVisibility.PUBLIC; + } + + public String[] getSuperClassesNames() { + return EMPTY_STRING_ARRAY; + } + + protected String[] extractParameterTypes(IFunction func) throws DOMException { + IParameter[] params= func.getParameters(); + String[] parameterTypes= new String[params.length]; + for (int i = 0; i < params.length; i++) { + IParameter param = params[i]; + parameterTypes[i]= ASTTypeUtil.getType(param.getType()); + } + if (parameterTypes.length == 1 && parameterTypes[0].equals("void")) { //$NON-NLS-1$ + return EMPTY_STRING_ARRAY; + } + return parameterTypes; + } +} diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/CElementHandleFactory.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/CElementHandleFactory.java new file mode 100644 index 00000000000..68871332582 --- /dev/null +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/CElementHandleFactory.java @@ -0,0 +1,111 @@ +/******************************************************************************* + * Copyright (c) 2006 Wind River Systems, Inc. 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: + * Markus Schorn - initial API and implementation + *******************************************************************************/ + +package org.eclipse.cdt.internal.core.model.ext; + +import org.eclipse.cdt.core.dom.IName; +import org.eclipse.cdt.core.dom.ast.DOMException; +import org.eclipse.cdt.core.dom.ast.IBinding; +import org.eclipse.cdt.core.dom.ast.ICompositeType; +import org.eclipse.cdt.core.dom.ast.IEnumerator; +import org.eclipse.cdt.core.dom.ast.IField; +import org.eclipse.cdt.core.dom.ast.IFunction; +import org.eclipse.cdt.core.dom.ast.IScope; +import org.eclipse.cdt.core.dom.ast.IVariable; +import org.eclipse.cdt.core.dom.ast.c.ICCompositeTypeScope; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassScope; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespace; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespaceScope; +import org.eclipse.cdt.core.model.ICElement; +import org.eclipse.cdt.core.model.ITranslationUnit; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.jface.text.IRegion; + +/** + * Factory for creating CElement handles. These are a minimal implementation + * of the ICElement interface and can be used for displaying information about + * the index. + * @since 4.0 + */ +public class CElementHandleFactory { + private CElementHandleFactory() {} + + public static ICElement create(ITranslationUnit tu, IBinding binding, + IRegion region, long timestamp) throws CoreException, DOMException { + + ICElement parentElement= create(tu, binding.getScope()); + if (parentElement == null) { + return null; + } + + CElementHandle element= null; + if (binding instanceof ICPPMethod) { + element= new MethodHandle(parentElement, (ICPPMethod) binding); + } + else if (binding instanceof IFunction) { + element= new FunctionHandle(parentElement, (IFunction) binding); + } + else if (binding instanceof IField) { + element= new FieldHandle(parentElement, (IField) binding); + } + else if (binding instanceof IVariable) { + element= new VariableHandle(parentElement, (IVariable) binding); + } + else if (binding instanceof IEnumerator) { + element= new EnumeratorHandle(parentElement, (IEnumerator) binding); + } + else if (binding instanceof ICompositeType) { + element= new StructureHandle(parentElement, (ICompositeType) binding); + } + else if (binding instanceof ICPPNamespace) { + element= new NamespaceHandle(parentElement, (ICPPNamespace) binding); + } + if (element != null && region != null) { + element.setRangeOfID(region, timestamp); + } + return element; + } + + private static ICElement create(ITranslationUnit tu, IScope scope) throws DOMException { + if (scope == null) { + return tu; + } + + IName scopeName= scope.getScopeName(); + if (scopeName == null) { + if (scope.getParent() == null) { + return tu; + } + return null; // unnamed namespace + } + + ICElement parentElement= create(tu, scope.getParent()); + if (parentElement == null) { + return null; + } + + CElementHandle element= null; + if (scope instanceof ICPPClassScope) { + ICPPClassType type= ((ICPPClassScope) scope).getClassType(); + element= new StructureHandle(parentElement, type); + } + else if (scope instanceof ICCompositeTypeScope) { + ICompositeType type= ((ICCompositeTypeScope) scope).getCompositeType(); + element= new StructureHandle(parentElement, type); + } + else if (scope instanceof ICPPNamespaceScope) { + element= new NamespaceHandle(parentElement, new String(scopeName.toCharArray())); + } + return element; + } +} diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/EnumerationHandle.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/EnumerationHandle.java new file mode 100644 index 00000000000..df821630d4f --- /dev/null +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/EnumerationHandle.java @@ -0,0 +1,22 @@ +/******************************************************************************* + * Copyright (c) 2006 Wind River Systems, Inc. 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: + * Markus Schorn - initial API and implementation + *******************************************************************************/ + +package org.eclipse.cdt.internal.core.model.ext; + +import org.eclipse.cdt.core.dom.ast.IEnumeration; +import org.eclipse.cdt.core.model.ICElement; + +public class EnumerationHandle extends CElementHandle implements org.eclipse.cdt.core.model.IEnumeration, ICElementHandle { + + public EnumerationHandle(ICElement parent, IEnumeration enumeration) { + super(parent, ICElement.C_ENUMERATION, enumeration.getName()); + } +} diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/EnumeratorHandle.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/EnumeratorHandle.java new file mode 100644 index 00000000000..4114ae59e26 --- /dev/null +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/EnumeratorHandle.java @@ -0,0 +1,26 @@ +/******************************************************************************* + * Copyright (c) 2006 Wind River Systems, Inc. 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: + * Markus Schorn - initial API and implementation + *******************************************************************************/ + +package org.eclipse.cdt.internal.core.model.ext; + +import org.eclipse.cdt.core.dom.ast.IEnumerator; +import org.eclipse.cdt.core.model.ICElement; + +public class EnumeratorHandle extends CElementHandle implements org.eclipse.cdt.core.model.IEnumerator { + + public EnumeratorHandle(ICElement parent, IEnumerator enumerator) { + super(parent, ICElement.C_ENUMERATOR, enumerator.getName()); + } + + public String getConstantExpression() { + return null; + } +} diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/FieldHandle.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/FieldHandle.java new file mode 100644 index 00000000000..806ad06f90b --- /dev/null +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/FieldHandle.java @@ -0,0 +1,22 @@ +/******************************************************************************* + * Copyright (c) 2006 Wind River Systems, Inc. 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: + * Markus Schorn - initial API and implementation + *******************************************************************************/ + +package org.eclipse.cdt.internal.core.model.ext; + +import org.eclipse.cdt.core.dom.ast.IField; +import org.eclipse.cdt.core.model.ICElement; + +public class FieldHandle extends CElementHandle implements org.eclipse.cdt.core.model.IField { + + public FieldHandle(ICElement parent, IField field) { + super(parent, ICElement.C_FIELD, field.getName()); + } +} diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/FunctionHandle.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/FunctionHandle.java new file mode 100644 index 00000000000..bf85bd6f908 --- /dev/null +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/FunctionHandle.java @@ -0,0 +1,48 @@ +/******************************************************************************* + * Copyright (c) 2006 Wind River Systems, Inc. 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: + * Markus Schorn - initial API and implementation + *******************************************************************************/ + +package org.eclipse.cdt.internal.core.model.ext; + +import org.eclipse.cdt.core.dom.ast.DOMException; +import org.eclipse.cdt.core.dom.ast.IFunction; +import org.eclipse.cdt.core.model.CModelException; +import org.eclipse.cdt.core.model.ICElement; +import org.eclipse.cdt.core.model.IFunctionDeclaration; +import org.eclipse.cdt.internal.core.model.FunctionDeclaration; + +public class FunctionHandle extends CElementHandle implements org.eclipse.cdt.core.model.IFunction { + + private String[] fParameterTypes; + + public FunctionHandle(ICElement parent, IFunction func) throws DOMException { + super(parent, ICElement.C_FUNCTION, func.getName()); + fParameterTypes= extractParameterTypes(func); + } + + public boolean equals(Object obj) { + if (obj instanceof IFunctionDeclaration) { + return FunctionDeclaration.equals(this, (IFunctionDeclaration) obj); + } + return false; + } + + public int getNumberOfParameters() { + return fParameterTypes.length; + } + + public String[] getParameterTypes() { + return fParameterTypes; + } + + public String getSignature() throws CModelException { + return FunctionDeclaration.getSignature(this); + } +} diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/ICElementHandle.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/ICElementHandle.java new file mode 100644 index 00000000000..da083f986b9 --- /dev/null +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/ICElementHandle.java @@ -0,0 +1,17 @@ +/******************************************************************************* + * Copyright (c) 2006 Wind River Systems, Inc. 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: + * Markus Schorn - initial API and implementation + *******************************************************************************/ + +package org.eclipse.cdt.internal.core.model.ext; + +import org.eclipse.cdt.core.model.ICElement; + +public interface ICElementHandle extends ICElement { +} diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/MethodHandle.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/MethodHandle.java new file mode 100644 index 00000000000..3fa61a7f773 --- /dev/null +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/MethodHandle.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * Copyright (c) 2006 Wind River Systems, Inc. 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: + * Markus Schorn - initial API and implementation + *******************************************************************************/ + +package org.eclipse.cdt.internal.core.model.ext; + +import org.eclipse.cdt.core.dom.ast.DOMException; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod; +import org.eclipse.cdt.core.model.CModelException; +import org.eclipse.cdt.core.model.ICElement; +import org.eclipse.cdt.core.model.IMethod; +import org.eclipse.cdt.core.model.IMethodDeclaration; +import org.eclipse.cdt.internal.core.model.FunctionDeclaration; +import org.eclipse.cdt.internal.core.model.MethodDeclaration; + +public class MethodHandle extends CElementHandle implements IMethod { + private String[] fParameterTypes; + + public MethodHandle(ICElement parent, ICPPMethod method) throws DOMException { + super(parent, ICElement.C_METHOD, method.getName()); + fParameterTypes= extractParameterTypes(method); + } + + public boolean equals(Object obj) { + if (obj instanceof IMethodDeclaration) { + return MethodDeclaration.equals(this, (IMethodDeclaration) obj); + } + return false; + } + + public int getNumberOfParameters() { + return fParameterTypes.length; + } + + public String[] getParameterTypes() { + return fParameterTypes; + } + + public String getSignature() throws CModelException { + return FunctionDeclaration.getSignature(this); + } +} diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/NamespaceHandle.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/NamespaceHandle.java new file mode 100644 index 00000000000..226031664cf --- /dev/null +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/NamespaceHandle.java @@ -0,0 +1,28 @@ +/******************************************************************************* + * Copyright (c) 2006 Wind River Systems, Inc. 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: + * Markus Schorn - initial API and implementation + *******************************************************************************/ + +package org.eclipse.cdt.internal.core.model.ext; + +import org.eclipse.cdt.core.dom.ast.DOMException; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespace; +import org.eclipse.cdt.core.model.ICElement; +import org.eclipse.cdt.core.model.INamespace; + +public class NamespaceHandle extends CElementHandle implements INamespace { + + public NamespaceHandle(ICElement parent, ICPPNamespace ns) throws DOMException { + super(parent, ICElement.C_NAMESPACE, ns.getName()); + } + + public NamespaceHandle(ICElement parent, String name) { + super(parent, ICElement.C_NAMESPACE, name); + } +} diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/SourceRange.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/SourceRange.java new file mode 100644 index 00000000000..53a7f6013a2 --- /dev/null +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/SourceRange.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * Copyright (c) 2006 Wind River Systems, Inc. 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: + * Markus Schorn - initial API and implementation + *******************************************************************************/ + +package org.eclipse.cdt.internal.core.model.ext; + +import org.eclipse.cdt.core.model.ISourceRange; + +public class SourceRange implements ISourceRange { + + private int fOffset; + private int fLength; + + public SourceRange(int offset, int length) { + fOffset= offset; + fLength= length; + } + + public int getEndLine() { + return -1; + } + + public int getIdLength() { + return fLength; + } + + public int getIdStartPos() { + return fOffset; + } + + public int getLength() { + return fLength; + } + + public int getStartLine() { + return -1; + } + + public int getStartPos() { + return fOffset; + } +} diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/StructureHandle.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/StructureHandle.java new file mode 100644 index 00000000000..d9cceb1318a --- /dev/null +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/StructureHandle.java @@ -0,0 +1,67 @@ +/******************************************************************************* + * Copyright (c) 2006 Wind River Systems, Inc. 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: + * Markus Schorn - initial API and implementation + *******************************************************************************/ + +package org.eclipse.cdt.internal.core.model.ext; + +import org.eclipse.cdt.core.dom.ast.DOMException; +import org.eclipse.cdt.core.dom.ast.ICompositeType; +import org.eclipse.cdt.core.model.CModelException; +import org.eclipse.cdt.core.model.ICElement; +import org.eclipse.cdt.core.model.IField; +import org.eclipse.cdt.core.model.IMethodDeclaration; +import org.eclipse.cdt.core.model.IStructure; + +public class StructureHandle extends CElementHandle implements IStructure { + private static final IField[] EMPTY_FIELDS = new IField[0]; + private static final IMethodDeclaration[] EMPTY_METHODS = new IMethodDeclaration[0]; + + public StructureHandle(ICElement parent, ICompositeType type) throws DOMException { + super(parent, convertKey(type.getKey()), type.getName()); + } + + private static int convertKey(int astKey) { + switch(astKey) { + case ICompositeType.k_struct: + return ICElement.C_STRUCT; + case ICompositeType.k_union: + return ICElement.C_UNION; + } + return ICElement.C_CLASS; + } + + public IField getField(String name) { + return null; + } + + public IField[] getFields() throws CModelException { + return EMPTY_FIELDS; + } + + public IMethodDeclaration getMethod(String name) { + return null; + } + + public IMethodDeclaration[] getMethods() throws CModelException { + return EMPTY_METHODS; + } + + public boolean isClass() throws CModelException { + return getElementType() == ICElement.C_CLASS; + } + + public boolean isStruct() throws CModelException { + return getElementType() == ICElement.C_STRUCT; + } + + public boolean isUnion() throws CModelException { + return getElementType() == ICElement.C_UNION; + } +} diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/VariableHandle.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/VariableHandle.java new file mode 100644 index 00000000000..19d8d9d1a08 --- /dev/null +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/ext/VariableHandle.java @@ -0,0 +1,21 @@ +/******************************************************************************* + * Copyright (c) 2006 Wind River Systems, Inc. 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: + * Markus Schorn - initial API and implementation + *******************************************************************************/ + +package org.eclipse.cdt.internal.core.model.ext; + +import org.eclipse.cdt.core.dom.ast.IVariable; +import org.eclipse.cdt.core.model.ICElement; + +public class VariableHandle extends CElementHandle implements org.eclipse.cdt.core.model.IVariable { + public VariableHandle(ICElement parent, IVariable var) { + super(parent, ICElement.C_VARIABLE, var.getName()); + } +} diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/IPositionTrackerManager.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/IPositionTrackerManager.java index 06ce3f29bbd..e3fab3d600a 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/IPositionTrackerManager.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/IPositionTrackerManager.java @@ -11,6 +11,7 @@ package org.eclipse.cdt.core; +import org.eclipse.cdt.core.model.ITranslationUnit; import org.eclipse.core.resources.IFile; import org.eclipse.core.runtime.IPath; @@ -30,6 +31,15 @@ import org.eclipse.core.runtime.IPath; * @since 4.0 */ public interface IPositionTrackerManager { + /** + * Returns the position converter suitable for mapping character offsets of the + * given translation unit to the current version of it. + * + * @param file a file for which the position adapter is requested. + * @param timestamp identifies the version of the file stored on disk. + * @return the requested position converter or null. + */ + public IPositionConverter findPositionConverter(ITranslationUnit tu, long timestamp); /** * Returns the position converter suitable for mapping character offsets of the * given file/timestamp to the current version of it. diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/PositionTrackerManager.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/PositionTrackerManager.java index 3c14691fe54..7adf0b156dd 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/PositionTrackerManager.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/PositionTrackerManager.java @@ -20,6 +20,7 @@ import java.util.TreeMap; import org.eclipse.cdt.core.IPositionConverter; import org.eclipse.cdt.core.IPositionTrackerManager; +import org.eclipse.cdt.core.model.ITranslationUnit; import org.eclipse.core.filebuffers.FileBuffers; import org.eclipse.core.filebuffers.IFileBuffer; import org.eclipse.core.filebuffers.IFileBufferListener; @@ -189,4 +190,19 @@ public class PositionTrackerManager implements IPositionTrackerManager, IFileBuf } return null; } + + /** + * {@inheritDoc} + */ + public synchronized IPositionConverter findPositionConverter(ITranslationUnit tu, long timestamp) { + IFile file= (IFile) tu.getResource(); + if (file != null) { + return findPositionConverter(file, timestamp); + } + IPath location= tu.getLocation(); + if (location != null) { + return findPositionConverter(location, timestamp); + } + return null; + } }