diff --git a/core/org.eclipse.cdt.core/ChangeLog b/core/org.eclipse.cdt.core/ChangeLog index 457382cb8f3..febf78161d2 100644 --- a/core/org.eclipse.cdt.core/ChangeLog +++ b/core/org.eclipse.cdt.core/ChangeLog @@ -1,3 +1,19 @@ +2005-06-01 Vladimir Hirsl + Modified DOM indexer to use IIndexEntry hierarchy to store index entries. + This enables more information to be stored during indexing (modifiers, function sigantures,...) + Also should address definition/declaration issues raised by Devin. + + * index/org/eclipse/cdt/internal/core/index/FunctionEntry.java + * index/org/eclipse/cdt/internal/core/index/IFunctionEntry.java + * index/org/eclipse/cdt/internal/core/index/IIndex.java + * index/org/eclipse/cdt/internal/core/index/ITypeEntry.java + * index/org/eclipse/cdt/internal/core/index/TypeEntry.java + * index/org/eclipse/cdt/internal/core/index/domsourceindexer/CGenerateIndexVisitor.java + * index/org/eclipse/cdt/internal/core/index/domsourceindexer/CPPGenerateIndexVisitor.java + * index/org/eclipse/cdt/internal/core/index/domsourceindexer/DOMSourceIndexerRunner.java + * index/org/eclipse/cdt/internal/core/index/domsourceindexer/IndexerOutputWrapper.java + + index/org/eclipse/cdt/internal/core/index/domsourceindexer/IndexVisitorUtil.java + 2005-05-25 David Inglis CModel performance improvement: only need to test project existance and nature for ICElement.exists() diff --git a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/FunctionEntry.java b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/FunctionEntry.java index 1249d13fedc..a6c8982fe57 100644 --- a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/FunctionEntry.java +++ b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/FunctionEntry.java @@ -26,12 +26,18 @@ public class FunctionEntry extends NamedEntry implements IFunctionEntry { public char[][] getSignature() { return signature; - } + public void setReturnType(char[] returnType) { + this.returnString = returnType; + } + + public char[] getReturnType() { + return returnString; + } + public void serialize(IIndexerOutput output) { output.addIndexEntry(this); } - } diff --git a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/IFunctionEntry.java b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/IFunctionEntry.java index 2b880db8e50..1b7a49ba721 100644 --- a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/IFunctionEntry.java +++ b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/IFunctionEntry.java @@ -16,4 +16,6 @@ public interface IFunctionEntry extends INamedEntry { public char[][] getSignature(); + public char[] getReturnType(); + } diff --git a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/IIndex.java b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/IIndex.java index 2c3d419490b..75a1f034596 100644 --- a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/IIndex.java +++ b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/IIndex.java @@ -56,6 +56,7 @@ public interface IIndex { final static int TYPE_FWD_UNION = 11; // type + final static int UNKNOWN = 0; final static int DECLARATION = 1; final static int REFERENCE = 2; final static int DEFINITION = 3; diff --git a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/ITypeEntry.java b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/ITypeEntry.java index 385286aa705..df3bb3d07fc 100644 --- a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/ITypeEntry.java +++ b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/ITypeEntry.java @@ -39,8 +39,12 @@ public interface ITypeEntry extends INamedEntry { public int getTypeKind(); /** * Returns the types that are inherited - * @return an array of char arrays - each representing a separate type that this entry inherits from + * @return an array of index entries - each representing a separate type that this entry inherits from */ public IIndexEntry[] getBaseTypes(); - + /** + * Return friend types + * @return an array of index entries - each representing a friend to this type + */ + public IIndexEntry[] getFriends(); } diff --git a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/TypeEntry.java b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/TypeEntry.java index 54609db036f..0b029c08099 100644 --- a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/TypeEntry.java +++ b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/TypeEntry.java @@ -12,11 +12,12 @@ package org.eclipse.cdt.internal.core.index; -public class TypeEntry extends NamedEntry implements ITypeEntry { +public class TypeEntry extends NamedEntry implements ITypeEntry { int type_kind; IIndexEntry[] baseTypes; - + IIndexEntry[] friends; + public TypeEntry(int type_kind, int entry_type, char[][] fullName, int modifiers, int fileNumber){ super(IIndex.TYPE, entry_type, fullName, modifiers, fileNumber); this.type_kind = type_kind; @@ -38,4 +39,12 @@ public class TypeEntry extends NamedEntry implements ITypeEntry { return baseTypes; } + public IIndexEntry[] getFriends() { + return friends; + } + + public void setFriends(IIndexEntry[] friends) { + this.friends = friends; + } + } diff --git a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/domsourceindexer/CGenerateIndexVisitor.java b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/domsourceindexer/CGenerateIndexVisitor.java index d7b718abeb5..ca8f3d9eeed 100644 --- a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/domsourceindexer/CGenerateIndexVisitor.java +++ b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/domsourceindexer/CGenerateIndexVisitor.java @@ -27,8 +27,10 @@ import org.eclipse.cdt.core.dom.ast.IProblemBinding; import org.eclipse.cdt.core.dom.ast.ITypedef; import org.eclipse.cdt.core.dom.ast.IVariable; import org.eclipse.cdt.core.dom.ast.c.CASTVisitor; +import org.eclipse.cdt.internal.core.index.FunctionEntry; import org.eclipse.cdt.internal.core.index.IIndex; -import org.eclipse.cdt.internal.core.index.domsourceindexer.IndexerOutputWrapper.EntryType; +import org.eclipse.cdt.internal.core.index.NamedEntry; +import org.eclipse.cdt.internal.core.index.TypeEntry; public class CGenerateIndexVisitor extends CASTVisitor { private DOMSourceIndexerRunner indexer; @@ -123,57 +125,100 @@ public class CGenerateIndexVisitor extends CASTVisitor { * @throws DOMException */ private void processNameBinding(IASTName name, IBinding binding, IASTFileLocation loc, int fileNumber) throws DOMException { + char[][] qualifiedName = getFullyQualifiedName(name); + IASTFileLocation fileLoc = IndexEncoderUtil.getFileLocation(name); + + // determine entryKind + int entryKind = IIndex.UNKNOWN; + if (name.isDefinition()){ + entryKind = IIndex.DEFINITION; + } + else if (name.isDeclaration()) { + entryKind = IIndex.DECLARATION; + } + else if (name.isReference()) { + entryKind = IIndex.REFERENCE; + } + // determine type - EntryType entryType = null; if (binding instanceof ICompositeType) { + int iEntryType = 0; int compositeKey = ((ICompositeType) binding).getKey(); ASTNodeProperty prop = name.getPropertyInParent(); switch (compositeKey) { case ICompositeType.k_struct: - entryType = IndexerOutputWrapper.STRUCT; + iEntryType = IIndex.TYPE_STRUCT; if (name.isDeclaration() && prop == IASTElaboratedTypeSpecifier.TYPE_NAME) - entryType = IndexerOutputWrapper.FWD_STRUCT; + iEntryType = IIndex.TYPE_FWD_STRUCT; break; case ICompositeType.k_union: - entryType = IndexerOutputWrapper.UNION; + iEntryType = IIndex.TYPE_UNION; if (name.isDeclaration() && prop == IASTElaboratedTypeSpecifier.TYPE_NAME) - entryType = IndexerOutputWrapper.FWD_UNION; + iEntryType = IIndex.TYPE_FWD_UNION; break; } - } - else if (binding instanceof IEnumeration) - entryType = IndexerOutputWrapper.ENUM; - else if (binding instanceof ITypedef) - entryType = IndexerOutputWrapper.TYPEDEF; - else if (binding instanceof IEnumerator) - entryType = IndexerOutputWrapper.ENUMERATOR; - else if (binding instanceof IField) - entryType = IndexerOutputWrapper.FIELD; - else if (binding instanceof IParameter || - binding instanceof IVariable) - entryType = IndexerOutputWrapper.VAR; - else if (binding instanceof IFunction) - entryType = IndexerOutputWrapper.FUNCTION; - - if (entryType != null) { - int entryKind=0; - if (name.isDeclaration()) { - entryKind=IIndex.DECLARATION; - }/* else if (name.isDefinition()){ - entryKind=IIndex.DEFINITION; - }*/ - else if (name.isReference()) { - entryKind=IIndex.REFERENCE; + int modifiers = 0; + if (entryKind != IIndex.REFERENCE) { + modifiers = IndexVisitorUtil.getModifiers(name, binding); } - - IndexerOutputWrapper.addIndexEntry(indexer.getOutput(), - getFullyQualifiedName(name), - entryType, - entryKind, - fileNumber, - loc.getNodeOffset(), - loc.getNodeLength(), - IIndex.OFFSET); + TypeEntry indexEntry = new TypeEntry(iEntryType, entryKind, qualifiedName, modifiers, fileNumber); + indexEntry.setNameOffset(fileLoc.getNodeOffset(), fileLoc.getNodeLength(), IIndex.OFFSET); + + indexEntry.serialize(indexer.getOutput()); + } + else if (binding instanceof IEnumeration){ + int modifiers = 0; + if (entryKind != IIndex.REFERENCE) { + modifiers = IndexVisitorUtil.getModifiers(name, binding); + } + TypeEntry indexEntry = new TypeEntry(IIndex.TYPE_ENUM, entryKind, qualifiedName, modifiers, fileNumber); + indexEntry.setNameOffset(fileLoc.getNodeOffset(), fileLoc.getNodeLength(), IIndex.OFFSET); + + indexEntry.serialize(indexer.getOutput()); + } + else if (binding instanceof ITypedef) { + TypeEntry indexEntry = new TypeEntry(IIndex.TYPE_TYPEDEF, entryKind, qualifiedName, 0, fileNumber); + indexEntry.setNameOffset(fileLoc.getNodeOffset(), fileLoc.getNodeLength(), IIndex.OFFSET); + + indexEntry.serialize(indexer.getOutput()); + } + else if (binding instanceof IEnumerator) { + NamedEntry indexEntry = new NamedEntry(IIndex.ENUMTOR, entryKind, qualifiedName, 0, fileNumber); + indexEntry.setNameOffset(fileLoc.getNodeOffset(), fileLoc.getNodeLength(), IIndex.OFFSET); + + indexEntry.serialize(indexer.getOutput()); + } + else if (binding instanceof IField) { + int modifiers = 0; + if (entryKind != IIndex.REFERENCE) { + modifiers = IndexVisitorUtil.getModifiers(name, binding); + } + NamedEntry indexEntry = new NamedEntry(IIndex.FIELD, entryKind, qualifiedName, modifiers, fileNumber); + indexEntry.setNameOffset(fileLoc.getNodeOffset(), fileLoc.getNodeLength(), IIndex.OFFSET); + + indexEntry.serialize(indexer.getOutput()); + } + else if (binding instanceof IParameter || + binding instanceof IVariable) { + int modifiers = 0; + if (entryKind != IIndex.REFERENCE) { + modifiers = IndexVisitorUtil.getModifiers(name, binding); + } + TypeEntry indexEntry = new TypeEntry(IIndex.TYPE_VAR, entryKind, qualifiedName, modifiers, fileNumber); + indexEntry.setNameOffset(fileLoc.getNodeOffset(), fileLoc.getNodeLength(), IIndex.OFFSET); + + indexEntry.serialize(indexer.getOutput()); + } + else if (binding instanceof IFunction) { + int modifiers = 0; + if (entryKind != IIndex.REFERENCE) { + modifiers = IndexVisitorUtil.getModifiers(name, binding); + } + FunctionEntry indexEntry = new FunctionEntry(IIndex.FUNCTION, entryKind, qualifiedName, modifiers, fileNumber); + indexEntry.setNameOffset(fileLoc.getNodeOffset(), fileLoc.getNodeLength(), IIndex.OFFSET); + indexEntry.setSignature(IndexVisitorUtil.getParameters((IFunction) binding)); + + indexEntry.serialize(indexer.getOutput()); } } diff --git a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/domsourceindexer/CPPGenerateIndexVisitor.java b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/domsourceindexer/CPPGenerateIndexVisitor.java index 20e57d4e2d0..242956f7199 100644 --- a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/domsourceindexer/CPPGenerateIndexVisitor.java +++ b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/domsourceindexer/CPPGenerateIndexVisitor.java @@ -10,12 +10,19 @@ ***********************************************************************/ package org.eclipse.cdt.internal.core.index.domsourceindexer; +import java.util.ArrayList; +import java.util.List; + import org.eclipse.cdt.core.dom.ast.ASTNodeProperty; import org.eclipse.cdt.core.dom.ast.DOMException; +import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier; +import org.eclipse.cdt.core.dom.ast.IASTDeclaration; import org.eclipse.cdt.core.dom.ast.IASTElaboratedTypeSpecifier; import org.eclipse.cdt.core.dom.ast.IASTFileLocation; import org.eclipse.cdt.core.dom.ast.IASTName; +import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTProblem; +import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration; import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.ICompositeType; import org.eclipse.cdt.core.dom.ast.IEnumeration; @@ -27,8 +34,10 @@ import org.eclipse.cdt.core.dom.ast.IProblemBinding; import org.eclipse.cdt.core.dom.ast.ITypedef; import org.eclipse.cdt.core.dom.ast.IVariable; import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; -import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDeclSpecifier; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTElaboratedTypeSpecifier; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPBase; import org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding; import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType; import org.eclipse.cdt.core.dom.ast.cpp.ICPPDelegate; @@ -36,10 +45,11 @@ 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.ICPPUsingDeclaration; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier; -import org.eclipse.cdt.core.search.ICSearchConstants; -import org.eclipse.cdt.core.search.ICSearchConstants.LimitTo; +import org.eclipse.cdt.internal.core.index.FunctionEntry; import org.eclipse.cdt.internal.core.index.IIndex; -import org.eclipse.cdt.internal.core.index.domsourceindexer.IndexerOutputWrapper.EntryType; +import org.eclipse.cdt.internal.core.index.IIndexEntry; +import org.eclipse.cdt.internal.core.index.NamedEntry; +import org.eclipse.cdt.internal.core.index.TypeEntry; public class CPPGenerateIndexVisitor extends CPPASTVisitor { private DOMSourceIndexerRunner indexer; @@ -125,7 +135,7 @@ public class CPPGenerateIndexVisitor extends CPPASTVisitor { //or if it occurs in another file int indexFlag = IndexEncoderUtil.calculateIndexFlags(indexer, loc); - processNameBinding(name, binding, loc, indexFlag, null); // function will determine limitTo + processNameBinding(name, binding, loc, indexFlag, IIndex.UNKNOWN); // function will determine limitTo } } @@ -137,73 +147,137 @@ public class CPPGenerateIndexVisitor extends CPPASTVisitor { * @param limitTo * @throws DOMException */ - private void processNameBinding(IASTName name, IBinding binding, IASTFileLocation loc, int fileNumber, LimitTo limitTo) throws DOMException { - // determine LimitTo - if (limitTo == null) { - if (name.isDeclaration()) { - limitTo = ICSearchConstants.DECLARATIONS; + private void processNameBinding(IASTName name, IBinding binding, IASTFileLocation loc, int fileNumber, int entryKind) throws DOMException { + char[][] qualifiedName = getFullyQualifiedName(binding); + IASTFileLocation fileLoc = IndexEncoderUtil.getFileLocation(name); + + // determine entryKind + if (entryKind == IIndex.UNKNOWN) { + if (name.isDefinition()){ + entryKind = IIndex.DEFINITION; } - else if (name.isDefinition()){ - limitTo = ICSearchConstants.DEFINITIONS; + else if (name.isDeclaration()) { + entryKind = IIndex.DECLARATION; } else if (name.isReference()) { - limitTo = ICSearchConstants.REFERENCES; - } - else { - limitTo = ICSearchConstants.UNKNOWN_LIMIT_TO; + entryKind = IIndex.REFERENCE; } } // determine type - EntryType entryType = null; if (binding instanceof ICompositeType) { + int iEntryType = 0; ICompositeType compBinding = (ICompositeType) binding; int compositeKey = compBinding.getKey(); ASTNodeProperty prop = name.getPropertyInParent(); switch (compositeKey) { case ICPPClassType.k_class: - entryType = IndexerOutputWrapper.CLASS; - if (name.isDeclaration() && prop == IASTElaboratedTypeSpecifier.TYPE_NAME) - entryType = IndexerOutputWrapper.FWD_CLASS; + iEntryType = IIndex.TYPE_CLASS; + if (name.isDeclaration() && prop == IASTElaboratedTypeSpecifier.TYPE_NAME) { + iEntryType = IIndex.TYPE_FWD_CLASS; + } break; case ICompositeType.k_struct: - entryType = IndexerOutputWrapper.STRUCT; - if (name.isDeclaration() && prop == IASTElaboratedTypeSpecifier.TYPE_NAME) - entryType = IndexerOutputWrapper.FWD_STRUCT; + iEntryType = IIndex.TYPE_STRUCT; + if (name.isDeclaration() && prop == IASTElaboratedTypeSpecifier.TYPE_NAME) { + iEntryType = IIndex.TYPE_FWD_STRUCT; + } break; case ICompositeType.k_union: - entryType = IndexerOutputWrapper.UNION; - if (name.isDeclaration() && prop == IASTElaboratedTypeSpecifier.TYPE_NAME) - entryType = IndexerOutputWrapper.FWD_UNION; + iEntryType = IIndex.TYPE_UNION; + if (name.isDeclaration() && prop == IASTElaboratedTypeSpecifier.TYPE_NAME) { + iEntryType = IIndex.TYPE_FWD_UNION; + } break; } - addDerivedDeclaratiion(name, compBinding, loc, fileNumber); - if (isFriendDeclaration(name, binding)) { - entryType = IndexerOutputWrapper.FRIEND; - } + int modifiers = 0; + if (entryKind != IIndex.REFERENCE) { + modifiers = IndexVisitorUtil.getModifiers(name, binding); + } + TypeEntry indexEntry = new TypeEntry(iEntryType, entryKind, qualifiedName, modifiers, fileNumber); + indexEntry.setNameOffset(fileLoc.getNodeOffset(), fileLoc.getNodeLength(), IIndex.OFFSET); + if (entryKind == IIndex.DEFINITION && binding instanceof ICPPClassType) { + addDerivedDeclarations(name, (ICPPClassType)binding, indexEntry, fileNumber); + addFriendDeclarations(name, (ICPPClassType)binding, indexEntry, fileNumber); + } + + indexEntry.serialize(indexer.getOutput()); + } + else if (binding instanceof IEnumeration) { + int modifiers = 0; + if (entryKind != IIndex.REFERENCE) { + modifiers = IndexVisitorUtil.getModifiers(name, binding); + } + TypeEntry indexEntry = new TypeEntry(IIndex.TYPE_ENUM, entryKind, qualifiedName, modifiers, fileNumber); + indexEntry.setNameOffset(fileLoc.getNodeOffset(), fileLoc.getNodeLength(), IIndex.OFFSET); + + indexEntry.serialize(indexer.getOutput()); + } + else if (binding instanceof ITypedef) { + TypeEntry indexEntry = new TypeEntry(IIndex.TYPE_TYPEDEF, entryKind, qualifiedName, 0, fileNumber); + indexEntry.setNameOffset(fileLoc.getNodeOffset(), fileLoc.getNodeLength(), IIndex.OFFSET); + + indexEntry.serialize(indexer.getOutput()); + } + else if (binding instanceof ICPPNamespace) { + NamedEntry indexEntry = new NamedEntry(IIndex.NAMESPACE, entryKind, qualifiedName, 0, fileNumber); + indexEntry.setNameOffset(fileLoc.getNodeOffset(), fileLoc.getNodeLength(), IIndex.OFFSET); + + indexEntry.serialize(indexer.getOutput()); + } + else if (binding instanceof IEnumerator) { + NamedEntry indexEntry = new NamedEntry(IIndex.ENUMTOR, entryKind, qualifiedName, 0, fileNumber); + indexEntry.setNameOffset(fileLoc.getNodeOffset(), fileLoc.getNodeLength(), IIndex.OFFSET); + + indexEntry.serialize(indexer.getOutput()); + } + else if (binding instanceof IField) { + int modifiers = 0; + if (entryKind != IIndex.REFERENCE) { + modifiers = IndexVisitorUtil.getModifiers(name, binding); + } + NamedEntry indexEntry = new NamedEntry(IIndex.FIELD, entryKind, qualifiedName, modifiers, fileNumber); + indexEntry.setNameOffset(fileLoc.getNodeOffset(), fileLoc.getNodeLength(), IIndex.OFFSET); + + indexEntry.serialize(indexer.getOutput()); } - else if (binding instanceof IEnumeration) - entryType = IndexerOutputWrapper.ENUM; - else if (binding instanceof ITypedef) - entryType = IndexerOutputWrapper.TYPEDEF; - else if (binding instanceof ICPPNamespace) - entryType = IndexerOutputWrapper.NAMESPACE; - else if (binding instanceof IEnumerator) - entryType = IndexerOutputWrapper.ENUMERATOR; - else if (binding instanceof IField) - entryType = IndexerOutputWrapper.FIELD; else if (binding instanceof IParameter || - binding instanceof IVariable) - entryType = IndexerOutputWrapper.VAR; + binding instanceof IVariable) { + int modifiers = 0; + if (entryKind != IIndex.REFERENCE) { + modifiers = IndexVisitorUtil.getModifiers(name, binding); + } + TypeEntry indexEntry = new TypeEntry(IIndex.TYPE_VAR, entryKind, qualifiedName, modifiers, fileNumber); + indexEntry.setNameOffset(fileLoc.getNodeOffset(), fileLoc.getNodeLength(), IIndex.OFFSET); + + indexEntry.serialize(indexer.getOutput()); + } else if (binding instanceof ICPPMethod) { - entryType = IndexerOutputWrapper.METHOD; + int modifiers = 0; + if (entryKind != IIndex.REFERENCE) { + modifiers = IndexVisitorUtil.getModifiers(name, binding); + } + FunctionEntry indexEntry = new FunctionEntry(IIndex.METHOD, entryKind, qualifiedName, modifiers, fileNumber); + indexEntry.setNameOffset(fileLoc.getNodeOffset(), fileLoc.getNodeLength(), IIndex.OFFSET); + indexEntry.setSignature(IndexVisitorUtil.getParameters((IFunction) binding)); + indexEntry.setReturnType(IndexVisitorUtil.getReturnType((IFunction) binding)); + + indexEntry.serialize(indexer.getOutput()); // TODO In case we want to add friend method declarations to index // if (isFriendDeclaration(name, binding)) { // entryType = IndexerOutputWrapper.FRIEND; // } } else if (binding instanceof IFunction) { - entryType = IndexerOutputWrapper.FUNCTION; + int modifiers = 0; + if (entryKind != IIndex.REFERENCE) { + modifiers = IndexVisitorUtil.getModifiers(name, binding); + } + FunctionEntry indexEntry = new FunctionEntry(IIndex.FUNCTION, entryKind, qualifiedName, modifiers, fileNumber); + indexEntry.setNameOffset(fileLoc.getNodeOffset(), fileLoc.getNodeLength(), IIndex.OFFSET); + indexEntry.setSignature(IndexVisitorUtil.getParameters((IFunction) binding)); + + indexEntry.serialize(indexer.getOutput()); // TODO In case we want to add friend function declarations to index // if (isFriendDeclaration(name, binding)) { // entryType = IndexerOutputWrapper.FRIEND; @@ -213,102 +287,129 @@ public class CPPGenerateIndexVisitor extends CPPASTVisitor { ICPPDelegate[] delegates = ((ICPPUsingDeclaration)binding).getDelegates(); for (int i = 0; i < delegates.length; i++) { IBinding orig = delegates[i].getBinding(); - processNameBinding(name, orig, loc, fileNumber, ICSearchConstants.REFERENCES); // reference to the original binding - processNameBinding(name, delegates[i], loc, fileNumber, ICSearchConstants.DECLARATIONS); // declaration of the new name + processNameBinding(name, orig, loc, fileNumber, IIndex.REFERENCE); // reference to the original binding + processNameBinding(name, delegates[i], loc, fileNumber, IIndex.DECLARATION); // declaration of the new name } return; } - if (entryType != null) { - int entryKind =0; - if (limitTo == ICSearchConstants.DECLARATIONS) { - entryKind = IIndex.DECLARATION; - } - /*else if (limitTo == ICSearchConstants.DEFINITIONS) { - entryKind = IIndex.DEFINITION; - }*/ - else if (limitTo == ICSearchConstants.REFERENCES) { - entryKind = IIndex.REFERENCE; - } - - IndexerOutputWrapper.addIndexEntry(indexer.getOutput(), - getFullyQualifiedName(binding), - entryType, - entryKind, - fileNumber, - loc.getNodeOffset(), - loc.getNodeLength(), - IIndex.OFFSET); - +// if (entryType != null) { +// IndexerOutputWrapper.addIndexEntry(indexer.getOutput(), +// getFullyQualifiedName(binding), +// entryType, +// entryKind, +// fileNumber, +// loc.getNodeOffset(), +// loc.getNodeLength(), +// IIndex.OFFSET); +// } + } + + /** + * @param name + * @param cppClassBinding + * @param typeEntry + * @param fileNumber + * @throws DOMException + */ + private void addDerivedDeclarations(IASTName name, ICPPClassType cppClassBinding, TypeEntry typeEntry, int fileNumber) throws DOMException { + List baseEntries = new ArrayList(); + ICPPBase[] baseClasses = cppClassBinding.getBases(); + for (int i = 0; i < baseClasses.length; i++) { + ICPPBase base = baseClasses[i]; + ICPPClassType baseClass = baseClasses[i].getBaseClass(); + int modifiers = 0; + int vis = base.getVisibility(); + if (vis == ICPPBase.v_public) { + modifiers |= IIndex.publicAccessSpecifier; + } + else if (vis == ICPPBase.v_protected) { + modifiers |= IIndex.protectedAccessSpecifier; + } + else if (vis == ICPPBase.v_private) { + modifiers |= IIndex.privateAccessSpecifier; + } + if (base.isVirtual()) { + modifiers |= IIndex.virtualSpecifier; + } + NamedEntry namedEntry = new NamedEntry(IIndex.TYPE_DERIVED, IIndex.DECLARATION, + baseClass.getQualifiedNameCharArray(), modifiers, fileNumber); + // tricky part - get the location of base specifier name + IASTNode parent = name.getParent(); + if (parent instanceof ICPPASTCompositeTypeSpecifier) { + ICPPASTCompositeTypeSpecifier typeParent = (ICPPASTCompositeTypeSpecifier) parent; + ICPPASTBaseSpecifier[] baseSpecs = typeParent.getBaseSpecifiers(); + IASTFileLocation baseLoc = null; + for (int j = 0; j < baseSpecs.length; j++) { + ICPPASTBaseSpecifier baseSpec = baseSpecs[j]; + IASTName baseName = baseSpec.getName(); + if (baseName.resolveBinding().equals(baseClass)) { + baseLoc = IndexEncoderUtil.getFileLocation(baseName); + break; + } + } + if (baseLoc != null) { + namedEntry.setNameOffset(baseLoc.getNodeOffset(), baseLoc.getNodeLength(), IIndex.OFFSET); + baseEntries.add(namedEntry); + } + } + } + if (baseEntries.size() > 0) { + typeEntry.setBaseTypes((IIndexEntry[]) baseEntries.toArray(new IIndexEntry[baseEntries.size()])); } } /** * @param name - * @param compBinding - * @param loc + * @param cppClassBinding + * @param typeEntry * @param fileNumber - * @throws DOMException */ - private void addDerivedDeclaratiion(IASTName name, ICompositeType compBinding, IASTFileLocation loc, int fileNumber) throws DOMException { - ASTNodeProperty prop = name.getPropertyInParent(); - int compositeKey = compBinding.getKey(); - if (compositeKey == ICPPClassType.k_class || compositeKey == ICompositeType.k_struct) { - if (prop == ICPPASTBaseSpecifier.NAME) { - // base class - IndexerOutputWrapper.addIndexEntry(indexer.getOutput(), getFullyQualifiedName(compBinding), - IndexerOutputWrapper.DERIVED, - IIndex.DECLARATION, - fileNumber, - loc.getNodeOffset(), - loc.getNodeLength(), - IIndex.OFFSET); - } - } - } - - /** - * @param name - * @param fileNumber - * @param loc - * @param binding - * @throws DOMException - */ - private boolean isFriendDeclaration(IASTName name, IBinding binding) throws DOMException { - boolean rc = false; - if (!name.isDeclaration()) - return rc; - ASTNodeProperty prop = name.getPropertyInParent(); - if (binding instanceof ICompositeType) { - ICompositeType compBinding = (ICompositeType) binding; - int compositeKey = compBinding.getKey(); - if (compositeKey == ICPPClassType.k_class || compositeKey == ICompositeType.k_struct) { - if (prop == IASTElaboratedTypeSpecifier.TYPE_NAME) { - IASTElaboratedTypeSpecifier elaboratedTypeSpec = (IASTElaboratedTypeSpecifier) name.getParent(); - if (elaboratedTypeSpec instanceof ICPPASTDeclSpecifier) { - ICPPASTDeclSpecifier cppDeclSpec = (ICPPASTDeclSpecifier) elaboratedTypeSpec; - rc = cppDeclSpec.isFriend(); - } + private void addFriendDeclarations(IASTName name, ICPPClassType cppClassBinding, TypeEntry typeEntry, int fileNumber) { + try { + IBinding[] friends = cppClassBinding.getFriends(); + if (friends.length > 0) { + IASTNode parent = name.getParent(); + if (parent instanceof ICPPASTCompositeTypeSpecifier) { + ICPPASTCompositeTypeSpecifier parentClass = (ICPPASTCompositeTypeSpecifier) parent; + List friendEntries = new ArrayList(); + IASTDeclaration[] members = parentClass.getMembers(); + for (int j = 0; j < members.length; j++) { + IASTDeclaration decl = members[j]; + if (decl instanceof IASTSimpleDeclaration) { + IASTSimpleDeclaration simplDecl = (IASTSimpleDeclaration) decl; + IASTDeclSpecifier declSpec = simplDecl.getDeclSpecifier(); + if (declSpec instanceof ICPPASTElaboratedTypeSpecifier) { + ICPPASTElaboratedTypeSpecifier elabTypeSpec = (ICPPASTElaboratedTypeSpecifier) declSpec; + // sanity check + if (elabTypeSpec.isFriend()) { + IASTName friendName = elabTypeSpec.getName(); + + for (int i = 0; i < friends.length; i++) { + IBinding friend = friends[i]; + if (friend.equals(friendName.resolveBinding())) { + int modifiers = IndexVisitorUtil.getModifiers(friendName, friend); + + NamedEntry namedEntry = new NamedEntry(IIndex.TYPE_FRIEND, IIndex.DECLARATION, + getFullyQualifiedName(friend), modifiers, fileNumber); + IASTFileLocation fileLoc = IndexEncoderUtil.getFileLocation(friendName); + namedEntry.setNameOffset(fileLoc.getNodeOffset(), fileLoc.getNodeLength(), IIndex.OFFSET); + friendEntries.add(namedEntry); + break; + } + } + } + } + } + } + if (friendEntries.size() > 0) { + typeEntry.setFriends((IIndexEntry[]) friendEntries.toArray(new IIndexEntry[friendEntries.size()])); + } } } } - // TODO In case we want to add friend function declarations to index -// else if (binding instanceof IFunction) { -// IFunction funBinding = (IFunction) binding; -// if (prop == IASTFunctionDeclarator.DECLARATOR_NAME) { -// IASTFunctionDeclarator fDecl = (IASTFunctionDeclarator) name.getParent(); -// IASTNode fDeclParent = fDecl.getParent(); -// if (fDeclParent instanceof IASTSimpleDeclaration) { -// IASTSimpleDeclaration sDecl = (IASTSimpleDeclaration) fDeclParent; -// IASTDeclSpecifier declSpec = sDecl.getDeclSpecifier(); -// if (declSpec instanceof ICPPASTSimpleDeclSpecifier) { -// ICPPASTSimpleDeclSpecifier fDeclSpec = (ICPPASTSimpleDeclSpecifier) declSpec; -// rc = fDeclSpec.isFriend(); -// } -// } -// } -// } - return rc; + catch (DOMException e) { + } } /** diff --git a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/domsourceindexer/DOMSourceIndexerRunner.java b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/domsourceindexer/DOMSourceIndexerRunner.java index ad9c6b3afd1..1d6c21e4517 100644 --- a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/domsourceindexer/DOMSourceIndexerRunner.java +++ b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/domsourceindexer/DOMSourceIndexerRunner.java @@ -40,6 +40,7 @@ import org.eclipse.cdt.core.parser.IScannerInfoProvider; import org.eclipse.cdt.core.parser.ParseError; import org.eclipse.cdt.core.parser.ParserLanguage; import org.eclipse.cdt.internal.core.index.IIndex; +import org.eclipse.cdt.internal.core.index.NamedEntry; import org.eclipse.cdt.internal.core.index.impl.IndexDelta; import org.eclipse.cdt.internal.core.index.sourceindexer.AbstractIndexer; import org.eclipse.cdt.internal.core.index.sourceindexer.SourceIndexer; @@ -259,15 +260,10 @@ public class DOMSourceIndexerRunner extends AbstractIndexer { getOutput().addRelatives(fileNumber, include, (parent != null) ? parent.getIncludeDirective().getPath() : null); - IndexerOutputWrapper.addIndexEntry(getOutput(), - new char[][] {include.toCharArray()}, - IndexerOutputWrapper.INCLUDE, - IIndex.REFERENCE, - fileNumber, - 1, - 1, - IIndex.OFFSET); - + NamedEntry namedEntry = new NamedEntry(IIndex.INCLUDE, IIndex.REFERENCE, new char[][] {include.toCharArray()}, 0, fileNumber); + namedEntry.setNameOffset(1, 1, IIndex.OFFSET); + namedEntry.serialize(getOutput()); + /* See if this file has been encountered before */ indexer.haveEncounteredHeader(resourceFile.getProject().getFullPath(), new Path(include), true); @@ -289,16 +285,11 @@ public class DOMSourceIndexerRunner extends AbstractIndexer { // Get the location IASTFileLocation loc = IndexEncoderUtil.getFileLocation(macro); int fileNumber = IndexEncoderUtil.calculateIndexFlags(this, loc); - IndexerOutputWrapper.addIndexEntry(getOutput(), - new char[][] {macro.toCharArray()}, - IndexerOutputWrapper.MACRO, - IIndex.DECLARATION, - fileNumber, - loc.getNodeOffset(), - loc.getNodeLength(), - IIndex.OFFSET); + + NamedEntry namedEntry = new NamedEntry(IIndex.MACRO, IIndex.DECLARATION, new char[][] {macro.toCharArray()}, 0, fileNumber); + namedEntry.setNameOffset(loc.getNodeOffset(), loc.getNodeLength(), IIndex.OFFSET); + namedEntry.serialize(getOutput()); } - } /** diff --git a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/domsourceindexer/IndexVisitorUtil.java b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/domsourceindexer/IndexVisitorUtil.java new file mode 100644 index 00000000000..94d92dffadd --- /dev/null +++ b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/domsourceindexer/IndexVisitorUtil.java @@ -0,0 +1,279 @@ +/*********************************************************************** + * Copyright (c) 2004 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM - Initial API and implementation + ***********************************************************************/ +package org.eclipse.cdt.internal.core.index.domsourceindexer; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.cdt.core.dom.ast.DOMException; +import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier; +import org.eclipse.cdt.core.dom.ast.IASTName; +import org.eclipse.cdt.core.dom.ast.IASTNode; +import org.eclipse.cdt.core.dom.ast.IBinding; +import org.eclipse.cdt.core.dom.ast.ICompositeType; +import org.eclipse.cdt.core.dom.ast.IEnumeration; +import org.eclipse.cdt.core.dom.ast.IFunction; +import org.eclipse.cdt.core.dom.ast.IFunctionType; +import org.eclipse.cdt.core.dom.ast.IType; +import org.eclipse.cdt.core.dom.ast.IVariable; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDeclSpecifier; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPConstructor; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPMember; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPVariable; +import org.eclipse.cdt.internal.core.index.IIndex; + +/** + * Utility methods used by both C and C++ visitors + * + * @author vhirsl + */ +public class IndexVisitorUtil { + private IndexVisitorUtil() {} + + /** + * @param declSpec + * @return + */ +// static int getModifiers(IASTDeclSpecifier declSpec) { +// int modifiers = 0; +// if (declSpec.isConst()) { +// modifiers |= IIndex.constQualifier; +// } +// else if (declSpec.isVolatile()) { +// modifiers |= IIndex.volatileQualifier; +// } +// if (declSpec.isInline()) { +// modifiers |= IIndex.inlineSpecifier; +// } +// if (declSpec instanceof ICPPASTDeclSpecifier) { +// ICPPASTDeclSpecifier cppDeclSpec = (ICPPASTDeclSpecifier) declSpec; +// if (cppDeclSpec.isExplicit()) { +// modifiers |= IIndex.explicitSpecifier; +// } +// if (cppDeclSpec.isVirtual()) { +// modifiers |= IIndex.virtualSpecifier; +// } +// } +// return modifiers; +// } + + /** + * @param variableBinding + * @return + */ +// static int getModifiers(IVariable variableBinding) { +// int modifiers = 0; +// try { +// if (variableBinding.isAuto()) { +// modifiers |= IIndex.autoSpecifier; +// } +// else if (variableBinding.isExtern()) { +// modifiers |= IIndex.externSpecifier; +// } +// else if (variableBinding.isRegister()) { +// modifiers |= IIndex.registerSpecifier; +// } +// else if (variableBinding.isStatic()) { +// modifiers |= IIndex.staticSpecifier; +// } +// if (variableBinding instanceof ICPPVariable) { +// ICPPVariable cppVariable = (ICPPVariable) variableBinding; +// if (cppVariable.isMutable()) { +// modifiers |= IIndex.mutableSpecifier; +// } +// } +// if (variableBinding instanceof ICPPMember) { +// ICPPMember member = (ICPPMember) variableBinding; +// int vis = member.getVisibility(); +// if (vis == ICPPMember.v_public) { +// modifiers |= IIndex.publicAccessSpecifier; +// } +// else if (vis == ICPPMember.v_private) { +// modifiers |= IIndex.privateAccessSpecifier; +// } +// else if (vis == ICPPMember.v_protected) { +// modifiers |= IIndex.protectedAccessSpecifier; +// } +// } +// } +// catch (DOMException e) { +// } +// return modifiers; +// } + + /** + * @param name + * @param functionBinding + * @return + */ + static int getModifiers(IASTName name, IBinding binding) { + int modifiers = 0; + try { + if (binding instanceof ICPPMember) { + ICPPMember member = (ICPPMember) binding; + int vis = member.getVisibility(); + if (vis == ICPPMember.v_public) { + modifiers |= IIndex.publicAccessSpecifier; + } + else if (vis == ICPPMember.v_private) { + modifiers |= IIndex.privateAccessSpecifier; + } + else if (vis == ICPPMember.v_protected) { + modifiers |= IIndex.protectedAccessSpecifier; + } + } + if (binding instanceof ICompositeType || + binding instanceof IEnumeration) { + IASTNode parent = name.getParent(); + if (parent instanceof IASTDeclSpecifier) { + IASTDeclSpecifier declSpec = (IASTDeclSpecifier) parent; + + if (declSpec.isConst()) { + modifiers |= IIndex.constQualifier; + } + else if (declSpec.isVolatile()) { + modifiers |= IIndex.volatileQualifier; + } + if (declSpec.isInline()) { + modifiers |= IIndex.inlineSpecifier; + } + if (declSpec instanceof ICPPASTDeclSpecifier) { + ICPPASTDeclSpecifier cppDeclSpec = (ICPPASTDeclSpecifier) declSpec; + if (cppDeclSpec.isExplicit()) { + modifiers |= IIndex.explicitSpecifier; + } + if (cppDeclSpec.isVirtual()) { + modifiers |= IIndex.virtualSpecifier; + } + } + } + } + else if (binding instanceof IVariable) { + IVariable variableBinding = (IVariable) binding; + + if (variableBinding.isAuto()) { + modifiers |= IIndex.autoSpecifier; + } + else if (variableBinding.isExtern()) { + modifiers |= IIndex.externSpecifier; + } + else if (variableBinding.isRegister()) { + modifiers |= IIndex.registerSpecifier; + } + else if (variableBinding.isStatic()) { + modifiers |= IIndex.staticSpecifier; + } + if (variableBinding instanceof ICPPVariable) { + ICPPVariable cppVariable = (ICPPVariable) variableBinding; + if (cppVariable.isMutable()) { + modifiers |= IIndex.mutableSpecifier; + } + } + } + else if (binding instanceof IFunction) { + IFunction functionBinding = (IFunction) binding; + + if (functionBinding.isAuto()) { + modifiers |= IIndex.autoSpecifier; + } + else if (functionBinding.isExtern()) { + modifiers |= IIndex.externSpecifier; + } + else if (functionBinding.isRegister()) { + modifiers |= IIndex.registerSpecifier; + } + else if (functionBinding.isStatic()) { + modifiers |= IIndex.staticSpecifier; + } + else if (functionBinding.isInline()) { + + + } + if (functionBinding instanceof ICPPFunction) { + ICPPFunction cppFunction = (ICPPFunction) functionBinding; + if (cppFunction.isMutable()) { + modifiers |= IIndex.mutableSpecifier; + } + if (cppFunction instanceof ICPPMethod) { + ICPPMethod cppMethod = (ICPPMethod) cppFunction; + if (cppMethod.isVirtual()) { + modifiers |= IIndex.virtualSpecifier; + } + if (cppMethod instanceof ICPPConstructor) { + ICPPConstructor constructor = (ICPPConstructor) cppMethod; + if (constructor.isExplicit()) { + modifiers |= IIndex.explicitSpecifier; + } + } + } + } + // check parent node for const, pure virtual and volatile + IASTNode parent = name.getParent(); + if (parent instanceof ICPPASTFunctionDeclarator) { + ICPPASTFunctionDeclarator cppFunDecl = (ICPPASTFunctionDeclarator) parent; + if (cppFunDecl.isConst()) { + modifiers |= IIndex.constQualifier; + } + else if (cppFunDecl.isVolatile()) { + modifiers |= IIndex.volatileQualifier; + } + if (cppFunDecl.isPureVirtual()) { + modifiers |= IIndex.pureVirtualSpecifier; + } + } + } // IFunction + } + catch (DOMException e) { + } + return modifiers; + } + + /** + * @param function + * @return + */ + static char[][] getParameters(IFunction function) { + List parameterList = new ArrayList(); + try { + IFunctionType functionType = function.getType(); + IType[] parameterTypes = functionType.getParameterTypes(); + for (int i = 0; i < parameterTypes.length; i++) { + IType parameterType = parameterTypes[i]; + parameterList.add(parameterType.toString().toCharArray()); + } + if (parameterList.isEmpty()) { + parameterList.add("void".toCharArray()); //$NON-NLS-1$ + } + } + catch (DOMException e) { + } + return (char[][]) parameterList.toArray(new char[parameterList.size()][]); + } + + /** + * @param function + * @return + */ + static char[] getReturnType(IFunction function) { + try { + IFunctionType functionType = function.getType(); + IType returnType = functionType.getReturnType(); + return returnType.toString().toCharArray(); + } + catch (DOMException e) { + } + return new char[0]; + } + +} diff --git a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/domsourceindexer/IndexerOutputWrapper.java b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/domsourceindexer/IndexerOutputWrapper.java index 9e06cc18dc3..bb56aa5f9aa 100644 --- a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/domsourceindexer/IndexerOutputWrapper.java +++ b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/domsourceindexer/IndexerOutputWrapper.java @@ -22,6 +22,7 @@ import org.eclipse.cdt.internal.core.index.TypeEntry; * (in anticipation that the interface is going to change) * * @author vhirsl + * @deprecated Obsolete with introduction of IIndexEntry hierarchy */ class IndexerOutputWrapper { static class EntryType {