mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-22 22:22:11 +02:00
fix bug 95425 (& 95415) - make sure implicit constructor A() matches both A() and A(void)
also, type of sizeof expression is size_t (if we can find it, otherwise use unsigned long int)
This commit is contained in:
parent
b920965dab
commit
8fd55c89b6
5 changed files with 87 additions and 22 deletions
|
@ -1035,10 +1035,15 @@ public class AST2CPPTests extends AST2BaseTest {
|
|||
assertNotNull(ctors);
|
||||
assertEquals(ctors.length, 2);
|
||||
|
||||
assertEquals(ctors[0].getParameters().length, 0);
|
||||
assertEquals(ctors[0].getParameters().length, 1);
|
||||
|
||||
IType t = ctors[0].getParameters()[0].getType();
|
||||
assertTrue( t instanceof IBasicType );
|
||||
assertEquals( ((IBasicType)t).getType(), IBasicType.t_void );
|
||||
|
||||
assertEquals(ctors[1].getParameters().length, 1);
|
||||
|
||||
IType t = ctors[1].getParameters()[0].getType();
|
||||
t = ctors[1].getParameters()[0].getType();
|
||||
assertTrue(t instanceof ICPPReferenceType);
|
||||
assertTrue(((ICPPReferenceType) t).getType() instanceof IQualifierType);
|
||||
IQualifierType qt = (IQualifierType) ((ICPPReferenceType) t).getType();
|
||||
|
@ -1062,7 +1067,7 @@ public class AST2CPPTests extends AST2BaseTest {
|
|||
assertNotNull(ctors);
|
||||
assertEquals(ctors.length, 2);
|
||||
|
||||
assertEquals(ctors[0].getParameters().length, 0);
|
||||
assertEquals(ctors[0].getParameters().length, 1);
|
||||
assertEquals(ctors[1].getParameters().length, 1);
|
||||
|
||||
IType t = ctors[1].getParameters()[0].getType();
|
||||
|
@ -3637,10 +3642,8 @@ public class AST2CPPTests extends AST2BaseTest {
|
|||
CPPNameCollector col = new CPPNameCollector();
|
||||
tu.accept(col);
|
||||
|
||||
ICPPConstructor ctor1 = (ICPPConstructor) col.getName(1)
|
||||
.resolveBinding();
|
||||
ICPPConstructor ctor = (ICPPConstructor) col.getName(11)
|
||||
.resolveBinding();
|
||||
ICPPConstructor ctor1 = (ICPPConstructor) col.getName(1).resolveBinding();
|
||||
ICPPConstructor ctor = (ICPPConstructor) col.getName(11).resolveBinding();
|
||||
assertSame(ctor, ctor1);
|
||||
}
|
||||
|
||||
|
@ -4041,4 +4044,28 @@ public class AST2CPPTests extends AST2BaseTest {
|
|||
assertSame( col.getName(6).resolveBinding(), f2 );
|
||||
assertSame( col.getName(9).resolveBinding(), f1 );
|
||||
}
|
||||
|
||||
public void testBug95425() throws Exception {
|
||||
IASTTranslationUnit tu = parse( "class A { A(); };", ParserLanguage.CPP ); //$NON-NLS-1$
|
||||
CPPNameCollector col = new CPPNameCollector();
|
||||
tu.accept(col);
|
||||
|
||||
ICPPClassType A = (ICPPClassType) col.getName(0).resolveBinding();
|
||||
ICPPConstructor ctor = (ICPPConstructor) col.getName(1).resolveBinding();
|
||||
|
||||
ICPPConstructor [] ctors = A.getConstructors();
|
||||
assertEquals( ctors.length, 2 );
|
||||
assertSame( ctor, ctors[0] );
|
||||
|
||||
tu = parse( "class A { A( void ); };", ParserLanguage.CPP ); //$NON-NLS-1$
|
||||
col = new CPPNameCollector();
|
||||
tu.accept(col);
|
||||
|
||||
A = (ICPPClassType) col.getName(0).resolveBinding();
|
||||
ctor = (ICPPConstructor) col.getName(1).resolveBinding();
|
||||
|
||||
ctors = A.getConstructors();
|
||||
assertEquals( ctors.length, 2 );
|
||||
assertSame( ctor, ctors[0] );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -83,7 +83,9 @@ public class CPPClassScope extends CPPScope implements ICPPClassScope {
|
|||
char [] className = name.toCharArray();
|
||||
|
||||
//default constructor: A()
|
||||
ICPPMethod m = new CPPImplicitConstructor( this, className, IParameter.EMPTY_PARAMETER_ARRAY );
|
||||
IType voidType = new CPPBasicType( IBasicType.t_void, 0 );
|
||||
IParameter [] voidPs = new IParameter [] { new CPPParameter( voidType ) };
|
||||
ICPPMethod m = new CPPImplicitConstructor( this, className, voidPs );
|
||||
implicits[0] = m;
|
||||
addBinding( m );
|
||||
|
||||
|
@ -102,7 +104,7 @@ public class CPPClassScope extends CPPScope implements ICPPClassScope {
|
|||
|
||||
//destructor: ~A()
|
||||
char [] dtorName = CharArrayUtils.concat( "~".toCharArray(), className ); //$NON-NLS-1$
|
||||
m = new CPPImplicitMethod( this, dtorName, new CPPBasicType( IBasicType.t_unspecified, 0 ), IParameter.EMPTY_PARAMETER_ARRAY );
|
||||
m = new CPPImplicitMethod( this, dtorName, new CPPBasicType( IBasicType.t_unspecified, 0 ), voidPs );
|
||||
implicits[3] = m;
|
||||
addBinding( m );
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
package org.eclipse.cdt.internal.core.dom.parser.cpp;
|
||||
|
||||
import org.eclipse.cdt.core.dom.ast.DOMException;
|
||||
import org.eclipse.cdt.core.dom.ast.IBasicType;
|
||||
import org.eclipse.cdt.core.dom.ast.IType;
|
||||
import org.eclipse.cdt.core.dom.ast.ITypedef;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunctionType;
|
||||
|
@ -54,10 +55,7 @@ public class CPPFunctionType implements ICPPFunctionType {
|
|||
} catch ( DOMException e ) {
|
||||
return false;
|
||||
}
|
||||
if( fps.length != parameters.length )
|
||||
return false;
|
||||
|
||||
try {
|
||||
try {
|
||||
//constructors & destructors have null return type
|
||||
if( ( returnType == null ) ^ ( ft.getReturnType() == null ) )
|
||||
return false;
|
||||
|
@ -66,10 +64,26 @@ public class CPPFunctionType implements ICPPFunctionType {
|
|||
} catch ( DOMException e1 ) {
|
||||
return false;
|
||||
}
|
||||
for( int i = 0; i < parameters.length; i++ ){
|
||||
if( ! parameters[i].isSameType( fps[i] ) )
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
if( parameters.length == 1 && fps.length == 0 ){
|
||||
if( !(parameters[0] instanceof IBasicType) || ((IBasicType)parameters[0]).getType() != IBasicType.t_void )
|
||||
return false;
|
||||
} else if( fps.length == 1 && parameters.length == 0 ){
|
||||
if( !(fps[0] instanceof IBasicType) || ((IBasicType)fps[0]).getType() != IBasicType.t_void )
|
||||
return false;
|
||||
} else if( parameters.length != fps.length ){
|
||||
return false;
|
||||
} else {
|
||||
for( int i = 0; i < parameters.length; i++ ){
|
||||
if( ! parameters[i].isSameType( fps[i] ) )
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} catch (DOMException e ){
|
||||
return false;
|
||||
}
|
||||
|
||||
if( isConst != ft.isConst() || isVolatile != ft.isVolatile() )
|
||||
return false;
|
||||
|
||||
|
|
|
@ -112,6 +112,7 @@ import org.eclipse.cdt.core.parser.util.ObjectMap;
|
|||
import org.eclipse.cdt.core.parser.util.ObjectSet;
|
||||
import org.eclipse.cdt.core.parser.util.ArrayUtil.ArrayWrapper;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.ITypeContainer;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.ProblemBinding;
|
||||
|
||||
/**
|
||||
|
@ -2673,11 +2674,19 @@ public class CPPSemantics {
|
|||
|
||||
IType s = getUltimateType( src, true );
|
||||
IType t = getUltimateType( trg, true );
|
||||
|
||||
if( s instanceof ICPPClassType ){
|
||||
|
||||
IType sPrev = src;
|
||||
while( sPrev instanceof ITypeContainer && ((ITypeContainer)sPrev).getType() != s )
|
||||
sPrev = ((ITypeContainer)sPrev).getType();
|
||||
|
||||
if( sPrev instanceof IPointerType && s instanceof ICPPClassType ){
|
||||
IType tPrev = trg;
|
||||
while( tPrev instanceof ITypeContainer && ((ITypeContainer)tPrev).getType() != t )
|
||||
tPrev = ((ITypeContainer)tPrev).getType();
|
||||
|
||||
//4.10-2 an rvalue of type "pointer to cv T", where T is an object type can be
|
||||
//converted to an rvalue of type "pointer to cv void"
|
||||
if( t instanceof IBasicType && ((IBasicType)t).getType() == IBasicType.t_void ){
|
||||
if( tPrev instanceof IPointerType && t instanceof IBasicType && ((IBasicType)t).getType() == IBasicType.t_void ){
|
||||
cost.rank = Cost.CONVERSION_RANK;
|
||||
cost.conversion = 1;
|
||||
cost.detail = 2;
|
||||
|
@ -2685,7 +2694,7 @@ public class CPPSemantics {
|
|||
}
|
||||
//4.10-3 An rvalue of type "pointer to cv D", where D is a class type can be converted
|
||||
//to an rvalue of type "pointer to cv B", where B is a base class of D.
|
||||
else if( t instanceof ICPPClassType ){
|
||||
else if( tPrev instanceof IPointerType && t instanceof ICPPClassType ){
|
||||
temp = hasBaseClass( (ICPPClassType)s, (ICPPClassType) t, true );
|
||||
cost.rank = ( temp > -1 ) ? Cost.CONVERSION_RANK : Cost.NO_MATCH_RANK;
|
||||
cost.conversion = ( temp > -1 ) ? temp : 0;
|
||||
|
|
|
@ -55,6 +55,7 @@ import org.eclipse.cdt.core.dom.ast.IASTStandardFunctionDeclarator;
|
|||
import org.eclipse.cdt.core.dom.ast.IASTStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTTypeId;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
|
||||
import org.eclipse.cdt.core.dom.ast.IArrayType;
|
||||
import org.eclipse.cdt.core.dom.ast.IBasicType;
|
||||
|
@ -135,7 +136,8 @@ import org.eclipse.cdt.internal.core.dom.parser.ProblemBinding;
|
|||
* @author aniefer
|
||||
*/
|
||||
public class CPPVisitor {
|
||||
|
||||
public static final String SIZE_T = "size_t"; //$NON-NLS-1$
|
||||
|
||||
/**
|
||||
* @param name
|
||||
*/
|
||||
|
@ -1722,6 +1724,17 @@ public class CPPVisitor {
|
|||
return getExpressionType( exps[ exps.length - 1 ] );
|
||||
} else if( expression instanceof ICPPASTTypeIdExpression ){
|
||||
ICPPASTTypeIdExpression typeidExp = (ICPPASTTypeIdExpression) expression;
|
||||
if( typeidExp.getOperator() == IASTTypeIdExpression.op_sizeof ){
|
||||
IScope scope = getContainingScope( typeidExp );
|
||||
try {
|
||||
IBinding [] bs = scope.find( "size_t" );//$NON-NLS-1$
|
||||
if( bs.length > 0 && bs[0] instanceof IType ){
|
||||
return (IType) bs[0];
|
||||
}
|
||||
} catch (DOMException e) {
|
||||
}
|
||||
return new CPPBasicType( IBasicType.t_int, CPPBasicType.IS_LONG | CPPBasicType.IS_UNSIGNED );
|
||||
}
|
||||
return createType( typeidExp.getTypeId() );
|
||||
}
|
||||
return null;
|
||||
|
|
Loading…
Add table
Reference in a new issue