diff --git a/core/org.eclipse.cdt.core/browser/ChangeLog-browser b/core/org.eclipse.cdt.core/browser/ChangeLog-browser index 9202ccbbb09..72c2a566b89 100644 --- a/core/org.eclipse.cdt.core/browser/ChangeLog-browser +++ b/core/org.eclipse.cdt.core/browser/ChangeLog-browser @@ -1,3 +1,8 @@ +2004-09-15 Chris Wiebe + + add getTypes and getAllTypes methods + * browser/org/eclipse/cdt/core/browser/TypeUtil.java + 2004-09-02 Chris Wiebe add method to get global namespace diff --git a/core/org.eclipse.cdt.core/browser/org/eclipse/cdt/core/browser/TypeUtil.java b/core/org.eclipse.cdt.core/browser/org/eclipse/cdt/core/browser/TypeUtil.java index 4032d357239..54928be3229 100644 --- a/core/org.eclipse.cdt.core/browser/org/eclipse/cdt/core/browser/TypeUtil.java +++ b/core/org.eclipse.cdt.core/browser/org/eclipse/cdt/core/browser/TypeUtil.java @@ -1,9 +1,13 @@ -/* - * Created on Jul 5, 2004 - * - * TODO To change the template for this generated file go to - * Window - Preferences - Java - Code Style - Code Templates - */ +/******************************************************************************* + * Copyright (c) 2004 QNX Software Systems and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * QNX Software Systems - initial API and implementation + *******************************************************************************/ package org.eclipse.cdt.core.browser; import java.util.ArrayList; @@ -12,45 +16,55 @@ import java.util.List; import org.eclipse.cdt.core.browser.typehierarchy.ITypeHierarchy; 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.IEnumeration; import org.eclipse.cdt.core.model.IMember; import org.eclipse.cdt.core.model.IMethodDeclaration; -import org.eclipse.cdt.core.model.INamespace; import org.eclipse.cdt.core.model.IParent; import org.eclipse.cdt.core.model.IStructure; +import org.eclipse.cdt.core.model.ITranslationUnit; import org.eclipse.cdt.core.parser.ast.ASTAccessVisibility; -import org.eclipse.core.runtime.CoreException; -/** - * @author CWiebe - * - * TODO To change the template for this generated type comment go to - * Window - Preferences - Java - Code Style - Code Templates - */ public class TypeUtil { - public static boolean isDeclaredType(ICElement elem) { + public static boolean isDeclaringType(ICElement elem) { int type = elem.getElementType(); return (type == ICElement.C_CLASS - || type == ICElement.C_STRUCT - || type == ICElement.C_ENUMERATION - || type == ICElement.C_UNION - || type == ICElement.C_TYPEDEF - || type == ICElement.C_NAMESPACE); + || type == ICElement.C_STRUCT + || type == ICElement.C_ENUMERATION + || type == ICElement.C_UNION + || type == ICElement.C_TYPEDEF + || type == ICElement.C_NAMESPACE); } - public static ICElement getDeclaringContainerType(ICElement elem) { - while (elem != null) { - if (elem instanceof IStructure - || elem instanceof INamespace - || elem instanceof IEnumeration) { - return elem; - } - elem = elem.getParent(); - } + public static boolean isMemberType(ICElement elem) { + int type = elem.getElementType(); + if (type == ICElement.C_CLASS + || type == ICElement.C_STRUCT + || type == ICElement.C_ENUMERATION + || type == ICElement.C_UNION + || type == ICElement.C_TYPEDEF + || type == ICElement.C_NAMESPACE) + return true; + return elem instanceof IMember; + } - return null; + /** + * Returns the type in which this member is declared, or null + * if this member is not declared in a type (for example, a top-level type). + * This is a handle-only method. + * + * @return the type in which this member is declared, or null + * if this member is not declared in a type (for example, a top-level type) + */ + public static ICElement getDeclaringType(ICElement elem) { + if (!isMemberType(elem)) + return null; + ICElement parent = elem.getParent(); + while (parent != null && !(parent instanceof ITranslationUnit)) { + if (isDeclaringType(parent)) + return parent; + parent = parent.getParent(); + } + return null; } public static ICElement getDeclaringClass(ICElement type) { @@ -89,21 +103,84 @@ public class TypeUtil { return (type.getElementType() == ICElement.C_NAMESPACE); } - public static ICElement[] getTypes(ICElement elem) { - final List typeList = new ArrayList(3); - try { - elem.accept(new ICElementVisitor() { - public boolean visit(ICElement element) throws CoreException { - // TODO Auto-generated method stub - if (element instanceof IStructure) { - typeList.add(element); - } - return false; - }}); - } catch (CoreException e) { + /** + * Returns the top-level types declared in the given translation unit + * in the order in which they appear in the source. + * + * @param tu the translation unit + * @return the top-level types declared in the given translation unit + * @throws CModelException if this element does not exist or if an + * exception occurs while accessing its corresponding resource + */ + public static ICElement[] getTypes(ITranslationUnit tu) throws CModelException { + List typeList = new ArrayList(); + ICElement[] children = tu.getChildren(); + for (int i = 0; i < children.length; ++i) { + if (isDeclaringType(children[i])) + typeList.add(children[i]); } return (ICElement[])typeList.toArray(new ICElement[typeList.size()]); } + + /** + * Returns all types declared in the given translation unit in the order + * in which they appear in the source. + * This includes all top-level types and nested member types. + * It does NOT include local types (types defined in methods). + * + * @return the array of top-level and member types defined in the given translation unit, in declaration order. + * @throws CModelException if this element does not exist or if an + * exception occurs while accessing its corresponding resource + */ + public static ICElement[] getAllTypes(ITranslationUnit tu) throws CModelException { + ICElement[] types = getTypes(tu); + ArrayList allTypes = new ArrayList(types.length); + ArrayList typesToTraverse = new ArrayList(types.length); + for (int i = 0; i < types.length; i++) { + typesToTraverse.add(types[i]); + } + while (!typesToTraverse.isEmpty()) { + ICElement type = (ICElement) typesToTraverse.get(0); + typesToTraverse.remove(type); + allTypes.add(type); + types = getTypes(type); + for (int i = 0; i < types.length; i++) { + typesToTraverse.add(types[i]); + } + } + return (ICElement[])allTypes.toArray(new ICElement[allTypes.size()]); + } + + + /** + * Returns the immediate member types declared by the given element. + * The results are listed in the order in which they appear in the source file. + * + * @param elem the element + * @exception CModelException if this element does not exist or if an + * exception occurs while accessing its corresponding resource. + * @return the immediate member types declared by this type + */ + public static ICElement[] getTypes(ICElement elem) throws CModelException { + List typeList = new ArrayList(); + if (isDeclaringType(elem) && elem instanceof IParent) { + ICElement[] children = ((IParent)elem).getChildren(); + for (int i = 0; i < children.length; ++i) { + if (isDeclaringType(children[i])) + typeList.add(children[i]); + } + } + return (ICElement[])typeList.toArray(new ICElement[typeList.size()]); + } + + public static ITranslationUnit getTranslationUnit(ICElement elem) { + while (elem != null) { + if (elem instanceof ITranslationUnit) + return (ITranslationUnit)elem; + elem = elem.getParent(); + } + return null; + } // TODO move method to CModelUtil public static IQualifiedTypeName getFullyQualifiedName(ICElement type) {