diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/model/ICElementWrapper.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/model/ICElementWrapper.java index d82c5d1a8b9..a2964472fbe 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/model/ICElementWrapper.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/model/ICElementWrapper.java @@ -1,5 +1,6 @@ package org.eclipse.cdt.internal.core.model; +import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.IParent; /** @@ -12,6 +13,7 @@ import org.eclipse.cdt.core.model.IParent; */ public interface ICElementWrapper { - public IParent getElement(); - public void setElement (IParent item); + public ICElement getElement(); + public void setElement (ICElement item); + public IParent getParent(); } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/model/NamespaceWrapper.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/model/NamespaceWrapper.java new file mode 100644 index 00000000000..fad09c2d29d --- /dev/null +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/model/NamespaceWrapper.java @@ -0,0 +1,67 @@ +package org.eclipse.cdt.internal.core.model; + +import org.eclipse.cdt.core.model.IParent; +import org.eclipse.cdt.core.model.ICElement; +import org.eclipse.cdt.internal.core.parser.util.Name; + +/********************************************************************** + * Created on Apr 1, 2003 + * + * Copyright (c) 2002,2003 Rational Software Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v0.5 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v05.html + * + * Contributors: + * Rational Software - Initial API and implementation +***********************************************************************/ +public class NamespaceWrapper implements ICElementWrapper{ + private Name name; + private final IParent parent; + private ICElement element; + + public NamespaceWrapper( IParent incoming) + { + this.parent= incoming; + } + + /** + * Returns the name. + * @return Name + */ + public Name getName() { + return name; + } + + /** + * Returns the parent. + * @return IParent + */ + public IParent getParent() { + return parent; + } + + /** + * Sets the name. + * @param name The name to set + */ + public void setName(Name name) { + this.name = name; + } + + /** + * @see org.eclipse.cdt.internal.core.model.ICElementWrapper#getElement() + */ + public ICElement getElement() { + return element; + } + + /** + * @see org.eclipse.cdt.internal.core.model.ICElementWrapper#setElement(org.eclipse.cdt.core.model.IParent) + */ + public void setElement(ICElement item) { + element = item; + } + +} diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/model/NewModelBuilder.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/model/NewModelBuilder.java index 7d19adce1fe..655b1c7eac1 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/model/NewModelBuilder.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/model/NewModelBuilder.java @@ -15,6 +15,7 @@ import java.util.LinkedList; import java.util.List; import org.eclipse.cdt.core.model.ICElement; +import org.eclipse.cdt.core.model.IParent; import org.eclipse.cdt.internal.core.parser.IParserCallback; import org.eclipse.cdt.internal.core.parser.Token; import org.eclipse.cdt.internal.core.parser.util.AccessSpecifier; @@ -135,14 +136,15 @@ public class NewModelBuilder implements IParserCallback { org.eclipse.cdt.internal.core.newparser.IParserCallback#beginSimpleDeclaration(Token) */ public Object simpleDeclarationBegin(Object container) { - ICElementWrapper wrapper = (ICElementWrapper)container; - Object parent = wrapper.getElement(); + ICElementWrapper wrapper = (ICElementWrapper)container; + // Assuming that the parent is the container's element + IParent parent = (IParent)wrapper.getElement(); SimpleDeclarationWrapper result = new SimpleDeclarationWrapper(); + result.setParent( parent ); + // A special case to transfere the visibility if( wrapper instanceof SimpleDeclarationWrapper ){ - result.setParent( wrapper.getElement() ); result.setCurrentVisibility(((SimpleDeclarationWrapper)wrapper).getCurrentVisibility()); - } else if ( wrapper instanceof TranslationUnitWrapper ) - result.setParent( (TranslationUnit)wrapper.getElement()); + } return result; } @@ -354,8 +356,9 @@ org.eclipse.cdt.internal.core.newparser.IParserCallback#beginSimpleDeclaration(T elem.setElementName( elementName ); if( wrapper.getName() != null ) { - elem.setIdPos(wrapper.getName().getStartOffset(), wrapper.getName().length()); - elem.setPos(wrapper.getName().getStartOffset(), wrapper.getName().length()); + elem.setTypeName( wrapper.getClassKind().getImage() ); + elem.setIdPos(wrapper.getName().getStartOffset(), elementName.length()); + elem.setPos(wrapper.getName().getStartOffset(), elementName.length()); } else { @@ -549,21 +552,37 @@ org.eclipse.cdt.internal.core.newparser.IParserCallback#beginSimpleDeclaration(T * @see org.eclipse.cdt.internal.core.parser.IParserCallback#namespaceDeclarationBegin(java.lang.Object) */ public Object namespaceDefinitionBegin(Object container) { - // until Namespaces are made part of the code model, just return the container object - return container; + + ICElementWrapper c = (ICElementWrapper)container; + NamespaceWrapper wrapper = new NamespaceWrapper((IParent)c.getElement()); + return wrapper; } /* (non-Javadoc) * @see org.eclipse.cdt.internal.core.parser.IParserCallback#namespaceDeclarationId(java.lang.Object) */ public void namespaceDefinitionId(Object namespace) { - // until namespaceDefinitionBegin is updated to do more than return its container, do nothing + // set wrapper name to current name + NamespaceWrapper wrapper = (NamespaceWrapper)namespace; + wrapper.setName( currName ); + + // create the new element + String namespaceName = wrapper.getName().toString(); + Parent realParent = (Parent)wrapper.getParent(); + Namespace newNameSpace = new Namespace( (ICElement)realParent, namespaceName ); + wrapper.setElement(newNameSpace); + realParent.addChild( newNameSpace ); + + // set the positions + newNameSpace.setIdPos(wrapper.getName().getStartOffset(), namespaceName.length()); + newNameSpace.setPos(wrapper.getName().getStartOffset(), namespaceName.length()); } /* (non-Javadoc) * @see org.eclipse.cdt.internal.core.parser.IParserCallback#namespaceDeclarationAbort(java.lang.Object) */ public void namespaceDefinitionAbort(Object namespace) { + namespace = null; } /* (non-Javadoc) diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/model/SimpleDeclarationWrapper.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/model/SimpleDeclarationWrapper.java index ad4bb7dcfc8..7a3d86a7082 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/model/SimpleDeclarationWrapper.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/model/SimpleDeclarationWrapper.java @@ -3,7 +3,9 @@ package org.eclipse.cdt.internal.core.model; import java.util.LinkedList; import java.util.List; +import org.eclipse.cdt.core.model.INamespace; import org.eclipse.cdt.core.model.IParent; +import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.IStructure; import org.eclipse.cdt.core.model.ITranslationUnit; import org.eclipse.cdt.internal.core.parser.Token; @@ -17,7 +19,7 @@ import org.eclipse.cdt.internal.core.parser.util.Name; */ public class SimpleDeclarationWrapper extends DeclSpecifier implements DeclSpecifier.Container, ICElementWrapper { - private IParent element = null; + private ICElement element = null; private IParent parent = null; private Name name = null; @@ -36,7 +38,7 @@ public class SimpleDeclarationWrapper extends DeclSpecifier implements DeclSpeci * Returns the item. * @return CElement */ - public IParent getElement() { + public ICElement getElement() { return element; } @@ -44,8 +46,8 @@ public class SimpleDeclarationWrapper extends DeclSpecifier implements DeclSpeci * Sets the item. * @param item The item to set */ - public void setElement (IParent item) { - this.element = (IParent)item; + public void setElement (ICElement item) { + this.element = item; } /** @@ -90,7 +92,8 @@ public class SimpleDeclarationWrapper extends DeclSpecifier implements DeclSpeci { declaration = createField( parentElement, declaratorName ); } - else if( parentElement instanceof ITranslationUnit ) + else if(( parentElement instanceof ITranslationUnit ) + || ( parentElement instanceof INamespace )) { if(isExtern()) { @@ -112,10 +115,18 @@ public class SimpleDeclarationWrapper extends DeclSpecifier implements DeclSpeci // this is a function or a method if( parentElement instanceof IStructure ) { - declaration = createMethodDeclaration( parentElement, declaratorName, parameters ); + if (isFunctionDefinition()) + { + declaration = createMethod( parentElement, declaratorName, parameters ); + } + else + { + declaration = createMethodDeclaration( parentElement, declaratorName, parameters ); + } } - else if( parentElement instanceof ITranslationUnit ) + else if(( parentElement instanceof ITranslationUnit ) + || ( parentElement instanceof INamespace )) { if (isFunctionDefinition()) { diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/model/TranslationUnitWrapper.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/model/TranslationUnitWrapper.java index dce4f9b733c..38e85c1ec63 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/model/TranslationUnitWrapper.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/model/TranslationUnitWrapper.java @@ -1,5 +1,6 @@ package org.eclipse.cdt.internal.core.model; +import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.IParent; /** @@ -17,13 +18,13 @@ public class TranslationUnitWrapper implements ICElementWrapper { /** * @see org.eclipse.cdt.internal.core.model.IWrapper#getElement() */ - public IParent getElement() { + public ICElement getElement() { return unit; } /** * @see org.eclipse.cdt.internal.core.model.IWrapper#setElement(java.lang.Object) */ - public void setElement(IParent item) { + public void setElement(ICElement item) { unit = (TranslationUnit)item; } @@ -31,4 +32,11 @@ public class TranslationUnitWrapper implements ICElementWrapper { { } + /** + * @see org.eclipse.cdt.internal.core.model.ICElementWrapper#getParent() + */ + public IParent getParent() { + return null; + } + } diff --git a/core/org.eclipse.cdt.ui.tests/model/org/eclipse/cdt/core/model/tests/CModelElementsTests.java b/core/org.eclipse.cdt.ui.tests/model/org/eclipse/cdt/core/model/tests/CModelElementsTests.java new file mode 100644 index 00000000000..f00c22e37e2 --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/model/org/eclipse/cdt/core/model/tests/CModelElementsTests.java @@ -0,0 +1,113 @@ +package org.eclipse.cdt.core.model.tests; + +/********************************************************************** + * Copyright (c) 2002,2003 Rational Software Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v0.5 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v05.html + * + * Contributors: + * Rational Software - Initial API and implementation +***********************************************************************/ +import java.io.FileInputStream; +import java.util.ArrayList; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import org.eclipse.cdt.core.CCorePlugin; +import org.eclipse.cdt.core.model.ICElement; +import org.eclipse.cdt.core.model.ICProject; +import org.eclipse.cdt.core.model.IField; +import org.eclipse.cdt.core.model.IMember; +import org.eclipse.cdt.core.model.IMethod; +import org.eclipse.cdt.core.model.INamespace; +import org.eclipse.cdt.core.model.IStructure; +import org.eclipse.cdt.internal.core.model.TranslationUnit; +import org.eclipse.cdt.testplugin.CProjectHelper; +import org.eclipse.cdt.testplugin.TestPluginLauncher; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.core.runtime.Path; + +public class CModelElementsTests extends TestCase { + private ICProject fCProject; + private IFile headerFile; + private NullProgressMonitor monitor; + + public static void main(String[] args) { + TestPluginLauncher.run(TestPluginLauncher.getLocationFromProperties(), WorkingCopyTests.class, args); + } + + public static Test suite() { + TestSuite suite= new TestSuite(); + suite.addTest(new CModelElementsTests("testCModelElements")); + return suite; + } + + public CModelElementsTests(String name) { + super(name); + } + + protected void setUp() throws Exception { + monitor = new NullProgressMonitor(); + String pluginRoot=org.eclipse.core.runtime.Platform.getPlugin("org.eclipse.cdt.ui.tests").find(new Path("/")).getFile(); + + fCProject= CProjectHelper.createCProject("TestProject1", "bin"); + headerFile = fCProject.getProject().getFile("CModelElementsTest.h"); + if (!headerFile.exists()) { + try{ + FileInputStream fileIn = new FileInputStream(pluginRoot+ "model/org/eclipse/cdt/core/model/tests/resources/cfiles/CModelElementsTestStart.h"); + headerFile.create(fileIn,false, monitor); + } catch (CoreException e) { + e.printStackTrace(); + } + } + CCorePlugin.getDefault().setUseNewParser(true); + + } + + protected void tearDown() throws Exception { + CProjectHelper.delete(fCProject); + } + + public void testCModelElements(){ + TranslationUnit tu = new TranslationUnit(fCProject, headerFile); + // parse the translation unit to get the elements tree + tu.parse(); + + // tu ---> namespace MyPackage + ArrayList tuPackages = tu.getChildrenOfType(ICElement.C_NAMESPACE); + INamespace namespace = (INamespace) tuPackages.get(0); + assertEquals(namespace.getElementName(), new String("MyPackage")); + + // MyPackage ---> class Hello + ArrayList nsClassChildren = namespace.getChildrenOfType(ICElement.C_CLASS); + IStructure classHello = (IStructure) nsClassChildren.get(0); + assertEquals(classHello.getElementName(), new String("Hello")); + + // Hello --> int x + ArrayList helloFieldChildren = classHello.getChildrenOfType(ICElement.C_FIELD); + IField intX = (IField) helloFieldChildren.get(0); + assertEquals(intX.getElementName(), new String("x")); + int visibility = intX.getVisibility(); + if (visibility != IMember.V_PROTECTED) + fail("visibility should be protected!"); + + + // Hello ---> void setX(int X) + ArrayList helloMethodChildren = classHello.getChildrenOfType(ICElement.C_METHOD); + IMethod setX = (IMethod) helloMethodChildren.get(0); + assertEquals(setX.getElementName(), new String("setX")); + int setXNumOfParam = setX.getNumberOfParameters(); + if(setXNumOfParam != 1) + fail("setX should have one parameter!"); + String[] setXParamTypes = setX.getParameterTypes(); + String firstParamType = setXParamTypes[0]; + assertEquals(firstParamType, new String("int")); + } + +} diff --git a/core/org.eclipse.cdt.ui.tests/model/org/eclipse/cdt/core/model/tests/resources/cfiles/CModelElementsTestStart.h b/core/org.eclipse.cdt.ui.tests/model/org/eclipse/cdt/core/model/tests/resources/cfiles/CModelElementsTestStart.h new file mode 100644 index 00000000000..7c82da3c94b --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/model/org/eclipse/cdt/core/model/tests/resources/cfiles/CModelElementsTestStart.h @@ -0,0 +1,11 @@ +#include + +namespace MyPackage{ + class Hello{ + protected: + int x; + void setX(int X){ + x = X; + }; + }; +}; \ No newline at end of file diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/CElementImageProvider.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/CElementImageProvider.java index d7be94f2b03..a2b5fa48511 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/CElementImageProvider.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/CElementImageProvider.java @@ -251,6 +251,9 @@ public class CElementImageProvider { case ICElement.C_FUNCTION: return CPluginImages.DESC_OBJS_FUNCTION; + case ICElement.C_VARIABLE_DECLARATION: + return CPluginImages.DESC_OBJS_VAR_DECLARARION; + case ICElement.C_FUNCTION_DECLARATION: return CPluginImages.DESC_OBJS_DECLARARION; @@ -259,59 +262,13 @@ public class CElementImageProvider { case ICElement.C_MACRO: return CPluginImages.DESC_OBJS_MACRO; + + case ICElement.C_NAMESPACE: + return CPluginImages.DESC_OBJS_CONTAINER; + } return null; - } - - public ImageDescriptor getCElementImageDescriptor(int type) { - switch (type) { - case ICElement.C_VCONTAINER: - return CPluginImages.DESC_OBJS_CONTAINER; - - case ICElement.C_UNIT: - return CPluginImages.DESC_OBJS_TUNIT; - - case ICElement.C_STRUCT: - return CPluginImages.DESC_OBJS_STRUCT; - - case ICElement.C_CLASS: - return CPluginImages.DESC_OBJS_CLASS; - - case ICElement.C_UNION: - return CPluginImages.DESC_OBJS_UNION; - - case ICElement.C_TYPEDEF: - return CPluginImages.DESC_OBJS_TYPEDEF; - - case ICElement.C_ENUMERATION: - return CPluginImages.DESC_OBJS_ENUMERATION; - - case ICElement.C_ENUMERATOR: - return CPluginImages.DESC_OBJS_ENUMERATOR; - - case ICElement.C_FIELD: - return CPluginImages.DESC_OBJS_PUBLIC_FIELD; - - case ICElement.C_VARIABLE: - return CPluginImages.DESC_OBJS_FIELD; - - case ICElement.C_METHOD: // assumed public - return CPluginImages.DESC_OBJS_PUBLIC_METHOD; - - case ICElement.C_FUNCTION: - case ICElement.C_FUNCTION_DECLARATION: - return CPluginImages.DESC_OBJS_FUNCTION; - - case ICElement.C_INCLUDE: - return CPluginImages.DESC_OBJS_INCLUDE; - - case ICElement.C_MACRO: - return CPluginImages.DESC_OBJS_MACRO; - } - System.out.println("Unknown base object ype " + type); - return CPluginImages.DESC_OBJS_MACRO; - //return null; - } + } // ---- Methods to compute the adornments flags --------------------------------- diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/CPluginImages.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/CPluginImages.java index 72628e94871..67109d5fedf 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/CPluginImages.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/CPluginImages.java @@ -58,6 +58,7 @@ public class CPluginImages { public static final String IMG_OBJS_PROTECTED_FIELD= NAME_PREFIX + "field_protected_obj.gif"; public static final String IMG_OBJS_PRIVATE_FIELD= NAME_PREFIX + "field_private_obj.gif"; public static final String IMG_OBJS_DECLARATION= NAME_PREFIX + "cdeclaration_obj.gif"; + public static final String IMG_OBJS_VAR_DECLARATION= NAME_PREFIX + "var_declaration_obj.gif"; public static final String IMG_OBJS_INCLUDE= NAME_PREFIX + "include_obj.gif"; public static final String IMG_OBJS_MACRO= NAME_PREFIX + "define_obj.gif"; public static final String IMG_OBJS_TUNIT= NAME_PREFIX + "c_file_obj.gif"; @@ -90,6 +91,7 @@ public class CPluginImages { public static final ImageDescriptor DESC_OBJS_PROTECTED_FIELD= createManaged(T_OBJ, IMG_OBJS_PROTECTED_FIELD); public static final ImageDescriptor DESC_OBJS_PRIVATE_FIELD= createManaged(T_OBJ, IMG_OBJS_PRIVATE_FIELD); public static final ImageDescriptor DESC_OBJS_DECLARARION= createManaged(T_OBJ, IMG_OBJS_DECLARATION); + public static final ImageDescriptor DESC_OBJS_VAR_DECLARARION= createManaged(T_OBJ, IMG_OBJS_VAR_DECLARATION); public static final ImageDescriptor DESC_OBJS_INCLUDE= createManaged(T_OBJ, IMG_OBJS_INCLUDE); public static final ImageDescriptor DESC_OBJS_MACRO= createManaged(T_OBJ, IMG_OBJS_MACRO); public static final ImageDescriptor DESC_OBJS_TUNIT= createManaged(T_OBJ, IMG_OBJS_TUNIT); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/CElementLabelProvider.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/CElementLabelProvider.java index 313dffefe20..976598bd277 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/CElementLabelProvider.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/CElementLabelProvider.java @@ -76,6 +76,7 @@ public class CElementLabelProvider extends LabelProvider { name = fDecl.getSignature(); break; case ICElement.C_STRUCT: + case ICElement.C_UNION: case ICElement.C_ENUMERATION: if((celem.getElementName() != null) && (celem.getElementName().length() > 0)){ name = celem.getElementName();