diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/parser2/CompleteParser2Tests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/parser2/CompleteParser2Tests.java index 210c15a2701..af75e9b9cf9 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/parser2/CompleteParser2Tests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/parser2/CompleteParser2Tests.java @@ -22,6 +22,7 @@ import org.eclipse.cdt.core.dom.ast.IASTElaboratedTypeSpecifier; import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; +import org.eclipse.cdt.core.dom.ast.IArrayType; import org.eclipse.cdt.core.dom.ast.IBasicType; import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IEnumeration; @@ -36,6 +37,7 @@ import org.eclipse.cdt.core.dom.ast.ITypedef; import org.eclipse.cdt.core.dom.ast.IVariable; import org.eclipse.cdt.core.dom.ast.cpp.ICPPBase; import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPConstructor; import org.eclipse.cdt.core.dom.ast.cpp.ICPPField; import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod; import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespace; @@ -660,43 +662,123 @@ public class CompleteParser2Tests extends TestCase { public void testParameterExpressions() throws Exception { - parse( "int x = 5; void foo( int sub = x ) { }"); //$NON-NLS-1$ + IASTTranslationUnit tu = parse( "int x = 5; void foo( int sub = x ) { }"); //$NON-NLS-1$ + CPPNameCollector col = new CPPNameCollector(); + CPPVisitor.visitTranslationUnit( tu, col ); + + assertEquals( col.size(), 4 ); + IVariable x = (IVariable) col.getName(0).resolveBinding(); + assertInstances( col, x, 2 ); } public void testNestedNamespaceExpression() throws Exception { - parse( "namespace A { int x = 666; } int y = A::x;"); //$NON-NLS-1$ + IASTTranslationUnit tu = parse( "namespace A { int x = 666; } int y = A::x;"); //$NON-NLS-1$ + CPPNameCollector col = new CPPNameCollector(); + CPPVisitor.visitTranslationUnit( tu, col ); + + assertEquals( col.size(), 6 ); + ICPPNamespace A = (ICPPNamespace) col.getName(0).resolveBinding(); + IVariable x = (IVariable) col.getName(1).resolveBinding(); + assertInstances( col, A, 2 ); + assertInstances( col, x, 3 ); } public void testConstructorChain() throws Exception { - parse( "int x = 5;\n class A \n{ public : \n int a; \n A() : a( x ) { } };"); //$NON-NLS-1$ + IASTTranslationUnit tu = parse( "int x = 5;\n class A \n{ public : \n int a; \n A() : a( x ) { } };"); //$NON-NLS-1$ + CPPNameCollector col = new CPPNameCollector(); + CPPVisitor.visitTranslationUnit( tu, col ); + + assertEquals( col.size(), 6 ); + IVariable x = (IVariable) col.getName(0).resolveBinding(); + ICPPField a = (ICPPField) col.getName(2).resolveBinding(); + ICPPConstructor A = (ICPPConstructor) col.getName(3).resolveBinding(); + assertNotNull( A ); + assertInstances( col, x, 2 ); + assertInstances( col, a, 2 ); } public void testArrayModExpression() throws Exception { - parse( "const int x = 5; int y [ x ]; "); //$NON-NLS-1$ + IASTTranslationUnit tu = parse( "const int x = 5; int y [ x ]; "); //$NON-NLS-1$ + CPPNameCollector col = new CPPNameCollector(); + CPPVisitor.visitTranslationUnit( tu, col ); + + assertEquals( col.size(), 3 ); + IVariable x = (IVariable) col.getName(0).resolveBinding(); + IVariable y = (IVariable) col.getName(1).resolveBinding(); + assertInstances( col, x, 2 ); + assertTrue( y.getType() instanceof IArrayType ); + assertTrue( ((IArrayType)y.getType()).getType() instanceof IBasicType ); } public void testPointerVariable() throws Exception { - parse( "class A { }; A * anA;"); //$NON-NLS-1$ + IASTTranslationUnit tu = parse( "class A { }; A * anA;"); //$NON-NLS-1$ + CPPNameCollector col = new CPPNameCollector(); + CPPVisitor.visitTranslationUnit( tu, col ); + + assertEquals( col.size(), 3 ); + ICPPClassType A = (ICPPClassType) col.getName(0).resolveBinding(); + IVariable anA = (IVariable) col.getName(2).resolveBinding(); + assertInstances( col, A, 2 ); + assertTrue( anA.getType() instanceof IPointerType ); + assertSame( ((IPointerType) anA.getType()).getType(), A ); } public void testExceptionSpecification() throws Exception { - parse( "class A { }; void foo( void ) throw ( A );"); //$NON-NLS-1$ + IASTTranslationUnit tu = parse( "class A { }; void foo( void ) throw ( A );"); //$NON-NLS-1$ + CPPNameCollector col = new CPPNameCollector(); + CPPVisitor.visitTranslationUnit( tu, col ); + + assertEquals( col.size(), 4 ); + ICPPClassType A = (ICPPClassType) col.getName(0).resolveBinding(); + assertInstances( col, A, 2 ); } public void testNewExpressions() throws Exception { - parse( "int A; int B; int C; int D; int P; int*p = new (P) (A)[B][C][D];" ); //$NON-NLS-1$ + IASTTranslationUnit tu = parse( "int A; int B; int C; int D; int P; int*p = new (P) (A)[B][C][D];" ); //$NON-NLS-1$ + CPPNameCollector col = new CPPNameCollector(); + CPPVisitor.visitTranslationUnit( tu, col ); + + assertEquals( col.size(), 11 ); + IVariable A = (IVariable) col.getName(0).resolveBinding(); + IVariable B = (IVariable) col.getName(1).resolveBinding(); + IVariable C = (IVariable) col.getName(2).resolveBinding(); + IVariable D = (IVariable) col.getName(3).resolveBinding(); + IVariable P = (IVariable) col.getName(4).resolveBinding(); + IVariable p = (IVariable) col.getName(5).resolveBinding(); + + assertInstances( col, A, 2 ); + assertInstances( col, B, 2 ); + assertInstances( col, C, 2 ); + assertInstances( col, D, 2 ); + assertInstances( col, P, 2 ); + + assertTrue( p.getType() instanceof IPointerType ); } public void testBug41520() throws Exception { - parse( "const int x = 666; const int y( x );"); //$NON-NLS-1$ + /*IASTTranslationUnit tu =*/ parse( "const int x = 666; const int y( x );"); //$NON-NLS-1$ + +// IASTSimpleDeclaration decl = (IASTSimpleDeclaration) tu.getDeclarations()[1]; +// IASTDeclarator dtor = decl.getDeclarators()[0]; +// assertFalse( dtor instanceof IASTFunctionDeclarator ); +// assertNotNull( dtor.getInitializer() ); +// +// CPPNameCollector col = new CPPNameCollector(); +// CPPVisitor.visitTranslationUnit( tu, col ); +// +// assertEquals( col.size(), 3 ); +// IVariable x = (IVariable) col.getName(0).resolveBinding(); +// IVariable y = (IVariable) col.getName(1).resolveBinding(); +// assertNotNull(y); +// assertInstances( col, x, 2 ); } public void testNewXReferences() throws Exception diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/cpp/ICPPConstructor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/cpp/ICPPConstructor.java new file mode 100644 index 00000000000..6a243d97847 --- /dev/null +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/cpp/ICPPConstructor.java @@ -0,0 +1,21 @@ +/******************************************************************************* + * Copyright (c) 2004 IBM Corporation 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: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +/* + * Created on Dec 21, 2004 + */ +package org.eclipse.cdt.core.dom.ast.cpp; + +/** + * @author aniefer + */ +public interface ICPPConstructor extends ICPPMethod { + +} diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTArrayModifier.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTArrayModifier.java index 42ebecb7ea6..2d5747e9af9 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTArrayModifier.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTArrayModifier.java @@ -10,11 +10,8 @@ **********************************************************************/ package org.eclipse.cdt.internal.core.dom.parser.cpp; -import org.eclipse.cdt.core.dom.ast.ASTNodeProperty; import org.eclipse.cdt.core.dom.ast.IASTArrayModifier; import org.eclipse.cdt.core.dom.ast.IASTExpression; -import org.eclipse.cdt.core.dom.ast.IASTNode; -import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; /** * @author jcamelon @@ -37,45 +34,4 @@ public class CPPASTArrayModifier extends CPPASTNode implements public void setConstantExpression(IASTExpression expression) { exp = expression; } - - /* (non-Javadoc) - * @see org.eclipse.cdt.core.dom.ast.IASTNode#getTranslationUnit() - */ - public IASTTranslationUnit getTranslationUnit() { - // TODO Auto-generated method stub - return null; - } - - /* (non-Javadoc) - * @see org.eclipse.cdt.core.dom.ast.IASTNode#getParent() - */ - public IASTNode getParent() { - // TODO Auto-generated method stub - return null; - } - - /* (non-Javadoc) - * @see org.eclipse.cdt.core.dom.ast.IASTNode#setParent(org.eclipse.cdt.core.dom.ast.IASTNode) - */ - public void setParent(IASTNode node) { - // TODO Auto-generated method stub - - } - - /* (non-Javadoc) - * @see org.eclipse.cdt.core.dom.ast.IASTNode#getPropertyInParent() - */ - public ASTNodeProperty getPropertyInParent() { - // TODO Auto-generated method stub - return null; - } - - /* (non-Javadoc) - * @see org.eclipse.cdt.core.dom.ast.IASTNode#setPropertyInParent(org.eclipse.cdt.core.dom.ast.ASTNodeProperty) - */ - public void setPropertyInParent(ASTNodeProperty property) { - // TODO Auto-generated method stub - - } - } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPConstructor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPConstructor.java new file mode 100644 index 00000000000..832372f40bc --- /dev/null +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPConstructor.java @@ -0,0 +1,31 @@ +/******************************************************************************* + * Copyright (c) 2004 IBM Corporation 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: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +/* + * Created on Dec 21, 2004 + */ +package org.eclipse.cdt.internal.core.dom.parser.cpp; + +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPConstructor; + +/** + * @author aniefer + */ +public class CPPConstructor extends CPPMethod implements ICPPConstructor { + + /** + * @param declarator + */ + public CPPConstructor(ICPPASTFunctionDeclarator declarator) { + super( declarator ); + } + +} diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java index f28b55b41f2..54321d34c7f 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java @@ -57,6 +57,7 @@ import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration; import org.eclipse.cdt.core.dom.ast.IASTPointer; import org.eclipse.cdt.core.dom.ast.IASTPointerOperator; import org.eclipse.cdt.core.dom.ast.IASTReturnStatement; +import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier; import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration; import org.eclipse.cdt.core.dom.ast.IASTStatement; import org.eclipse.cdt.core.dom.ast.IASTSwitchStatement; @@ -122,7 +123,8 @@ public class CPPVisitor { IASTNode parent = name.getParent(); if( parent instanceof IASTNamedTypeSpecifier || parent instanceof ICPPASTQualifiedName || - parent instanceof ICPPASTBaseSpecifier ) + parent instanceof ICPPASTBaseSpecifier || + parent instanceof ICPPASTConstructorChainInitializer ) { IBinding binding = CPPSemantics.resolveBinding( name ); if( binding == null && parent instanceof ICPPASTQualifiedName ){ @@ -151,12 +153,12 @@ public class CPPVisitor { private static IBinding createBinding( IASTEnumerator enumerator ) { ICPPScope scope = (ICPPScope) getContainingScope( enumerator ); - IBinding enumeration = scope.getBinding( enumerator.getName() ); - if( enumeration == null ){ - enumeration = new CPPEnumerator( enumerator ); - scope.addBinding( enumeration ); + IBinding enumtor = scope.getBinding( enumerator.getName() ); + if( enumtor == null ){ + enumtor = new CPPEnumerator( enumerator ); + scope.addBinding( enumtor ); } - return enumeration; + return enumtor; } @@ -259,9 +261,19 @@ public class CPPVisitor { return function; } } - if( scope instanceof ICPPClassScope ) - binding = new CPPMethod( (ICPPASTFunctionDeclarator) declarator ); - else { + if( scope instanceof ICPPClassScope ){ + IASTDeclSpecifier decl = null; + if( parent instanceof IASTSimpleDeclaration ) + decl = ((IASTSimpleDeclaration)parent).getDeclSpecifier(); + else if( parent instanceof IASTFunctionDefinition ) + decl = ((IASTFunctionDefinition)parent).getDeclSpecifier(); + if( decl != null && decl instanceof IASTSimpleDeclSpecifier && + ((IASTSimpleDeclSpecifier) decl).getType() == 0 ) + { + binding = new CPPConstructor( (ICPPASTFunctionDeclarator) declarator ); + } else + binding = new CPPMethod( (ICPPASTFunctionDeclarator) declarator ); + } else { binding = new CPPFunction( (ICPPASTFunctionDeclarator) declarator ); } } else if( parent instanceof IASTParameterDeclaration ){ @@ -617,7 +629,9 @@ public class CPPVisitor { if( action.processDeclarators ) if( !action.processDeclarator( declarator ) ) return false; - if( !visitName( declarator.getName(), action ) ) return false; + if( declarator.getPropertyInParent() != IASTTypeId.ABSTRACT_DECLARATOR ){ + if( !visitName( declarator.getName(), action ) ) return false; + } if( declarator.getNestedDeclarator() != null ) if( !visitDeclarator( declarator.getNestedDeclarator(), action ) ) return false; @@ -648,6 +662,12 @@ public class CPPVisitor { } } + if( declarator instanceof IASTArrayDeclarator ){ + IASTArrayModifier [] mods = ((IASTArrayDeclarator) declarator).getArrayModifiers(); + for( int i = 0; i < mods.length; i++ ){ + if( !visitExpression( mods[i].getConstantExpression(), action ) ) return false; + } + } if( declarator.getInitializer() != null ) if( !visitInitializer( declarator.getInitializer(), action ) ) return false; @@ -791,8 +811,8 @@ public class CPPVisitor { public static boolean visitTypeId(IASTTypeId typeId, CPPBaseVisitorAction action) { if( action.processTypeIds ) if( !action.processTypeId( typeId ) ) return false; - if( !visitDeclarator( typeId.getAbstractDeclarator(), action ) ) return false; if( !visitDeclSpecifier( typeId.getDeclSpecifier(), action ) ) return false; + if( !visitDeclarator( typeId.getAbstractDeclarator(), action ) ) return false; return true; }