mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
fix bug 86547: default parameters and order of resolution.
This commit is contained in:
parent
2794dda812
commit
278786b79a
2 changed files with 34 additions and 70 deletions
|
@ -3812,5 +3812,23 @@ public class AST2CPPTests extends AST2BaseTest {
|
||||||
ICPPField d2 = (ICPPField) col.getName(7).resolveBinding();
|
ICPPField d2 = (ICPPField) col.getName(7).resolveBinding();
|
||||||
assertSame( d1, d2 );
|
assertSame( d1, d2 );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testBug86547() throws Exception {
|
||||||
|
StringBuffer buffer = new StringBuffer();
|
||||||
|
buffer.append("void f( int, int ); \n"); //$NON-NLS-1$
|
||||||
|
buffer.append("void f( int, int = 3); \n"); //$NON-NLS-1$
|
||||||
|
buffer.append("void f( int = 2, int ); \n"); //$NON-NLS-1$
|
||||||
|
buffer.append("void g() { \n"); //$NON-NLS-1$
|
||||||
|
buffer.append(" f( 3 ); \n"); //$NON-NLS-1$
|
||||||
|
buffer.append(" f( ); \n"); //$NON-NLS-1$
|
||||||
|
buffer.append("} \n"); //$NON-NLS-1$
|
||||||
|
|
||||||
|
IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.CPP ); //$NON-NLS-1$
|
||||||
|
CPPNameCollector col = new CPPNameCollector();
|
||||||
|
tu.accept( col );
|
||||||
|
|
||||||
|
IFunction f1 = (IFunction) col.getName(0).resolveBinding();
|
||||||
|
assertInstances( col, f1, 5 );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -41,7 +41,6 @@ import org.eclipse.cdt.core.dom.ast.IASTNamedTypeSpecifier;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTNode;
|
import org.eclipse.cdt.core.dom.ast.IASTNode;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
|
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTReturnStatement;
|
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.IASTSimpleDeclaration;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTTypeId;
|
import org.eclipse.cdt.core.dom.ast.IASTTypeId;
|
||||||
|
@ -1739,98 +1738,45 @@ public class CPPSemantics {
|
||||||
int num;
|
int num;
|
||||||
|
|
||||||
//Trim the list down to the set of viable functions
|
//Trim the list down to the set of viable functions
|
||||||
IFunction fName;
|
IFunction function = null;
|
||||||
ICPPASTFunctionDeclarator function = null;
|
|
||||||
int size = functions.length;
|
int size = functions.length;
|
||||||
for( int i = 0; i < size && functions[i] != null; i++ ){
|
for( int i = 0; i < size && functions[i] != null; i++ ){
|
||||||
fName = (IFunction) functions[i];
|
function = (IFunction) functions[i];
|
||||||
IASTNode def = ((ICPPInternalBinding)fName).getDefinition();
|
num = function.getParameters().length;
|
||||||
if( def instanceof ICPPASTFunctionDeclarator )
|
|
||||||
function = (ICPPASTFunctionDeclarator) def;
|
|
||||||
else if( def != null ){
|
|
||||||
IASTNode p = def.getParent();
|
|
||||||
if( p instanceof ICPPASTQualifiedName )
|
|
||||||
p = p.getParent();
|
|
||||||
function = (ICPPASTFunctionDeclarator) p;
|
|
||||||
} else {
|
|
||||||
function = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if( function == null ) {
|
|
||||||
IASTNode [] nodes = ((ICPPInternalBinding) fName).getDeclarations();
|
|
||||||
if( nodes != null && nodes.length > 0 ){
|
|
||||||
IASTNode node = nodes[0];
|
|
||||||
while( node instanceof IASTName )
|
|
||||||
node = node.getParent();
|
|
||||||
function = (ICPPASTFunctionDeclarator) node;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if( function == null ){
|
|
||||||
//implicit member function, for now, not supporting default values or var args
|
|
||||||
num = fName.getParameters().length;
|
|
||||||
} else {
|
|
||||||
num = function.getParameters().length;
|
|
||||||
}
|
|
||||||
|
|
||||||
//if there are m arguments in the list, all candidate functions having m parameters
|
//if there are m arguments in the list, all candidate functions having m parameters
|
||||||
//are viable
|
//are viable
|
||||||
if( num == numParameters ){
|
if( num == numParameters ){
|
||||||
if( data.forDefinition() && !isMatchingFunctionDeclaration( fName, data ) ){
|
if( data.forDefinition() && !isMatchingFunctionDeclaration( function, data ) ){
|
||||||
functions[i] = null;
|
functions[i] = null;
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
//check for void
|
//check for void
|
||||||
else if( numParameters == 0 && num == 1 ){
|
else if( numParameters == 0 && num == 1 ){
|
||||||
if( function != null ){
|
IParameter param = function.getParameters()[0];
|
||||||
IASTParameterDeclaration param = function.getParameters()[0];
|
IType t = param.getType();
|
||||||
IASTDeclSpecifier declSpec = param.getDeclSpecifier();
|
if( t instanceof IBasicType && ((IBasicType)t).getType() == IBasicType.t_void )
|
||||||
if( declSpec instanceof IASTSimpleDeclSpecifier ){
|
continue;
|
||||||
if( ((IASTSimpleDeclSpecifier)declSpec).getType() == IASTSimpleDeclSpecifier.t_void &&
|
|
||||||
param.getDeclarator().getPointerOperators().length == 0 )
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
IParameter p = fName.getParameters()[0];
|
|
||||||
IType t = p.getType();
|
|
||||||
if( t instanceof IBasicType && ((IBasicType)t).getType() == IBasicType.t_void )
|
|
||||||
continue;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//A candidate function having fewer than m parameters is viable only if it has an
|
//A candidate function having fewer than m parameters is viable only if it has an
|
||||||
//ellipsis in its parameter list.
|
//ellipsis in its parameter list.
|
||||||
if( num < numParameters ){
|
if( num < numParameters ){
|
||||||
if( (function != null && function.takesVarArgs()) || fName.takesVarArgs() ) {
|
if( function.takesVarArgs() )
|
||||||
continue;
|
continue;
|
||||||
}
|
|
||||||
//not enough parameters, remove it
|
//not enough parameters, remove it
|
||||||
functions[i] = null;
|
functions[i] = null;
|
||||||
}
|
}
|
||||||
//a candidate function having more than m parameters is viable only if the (m+1)-st
|
//a candidate function having more than m parameters is viable only if the (m+1)-st
|
||||||
//parameter has a default argument
|
//parameter has a default argument
|
||||||
else {
|
else {
|
||||||
if( function != null ) {
|
IParameter [] params = function.getParameters();
|
||||||
IASTParameterDeclaration [] params = function.getParameters();
|
for( int j = num - 1; j >= numParameters; j-- ){
|
||||||
for( int j = num - 1; j >= numParameters; j-- ){
|
if( ((ICPPParameter)params[j]).getDefaultValue() == null ){
|
||||||
if( params[j].getDeclarator().getInitializer() == null ){
|
functions[i] = null;
|
||||||
functions[i] = null;
|
size--;
|
||||||
size--;
|
break;
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
IParameter [] params = fName.getParameters();
|
|
||||||
for( int j = num - 1; j >= numParameters; j-- ){
|
|
||||||
if( ((ICPPParameter)params[j]).getDefaultValue() == null ){
|
|
||||||
functions[i] = null;
|
|
||||||
size--;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue