1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Bug 299911. Code streamlining.

This commit is contained in:
Sergey Prigogin 2012-08-07 11:48:02 -07:00
parent be25f45843
commit 128debaae2
7 changed files with 47 additions and 45 deletions

View file

@ -88,6 +88,7 @@ import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTNameBase;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPDeferredClassInstance; import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPDeferredClassInstance;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalUnknownScope; import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalUnknownScope;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPUnknownBinding; import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPUnknownBinding;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil;
public class AST2TemplateTests extends AST2BaseTest { public class AST2TemplateTests extends AST2BaseTest {
@ -5655,17 +5656,14 @@ public class AST2TemplateTests extends AST2BaseTest {
// typedef typename C::type pointer; // typedef typename C::type pointer;
// }; // };
// //
// void f(int*); // B<int>::pointer a;
// void f(int);
//
// void test(B<int>::pointer a) {
// f(a);
// }
public void testDependentExpressions_b() throws Exception { public void testDependentExpressions_b() throws Exception {
parseAndCheckBindings(); parseAndCheckBindings();
BindingAssertionHelper bh= new BindingAssertionHelper(getAboveComment(), CPP); BindingAssertionHelper bh= new BindingAssertionHelper(getAboveComment(), CPP);
ICPPFunction func= bh.assertNonProblem("f(a)", 1, ICPPFunction.class); ICPPVariable var= bh.assertNonProblem("a;", 1, ICPPVariable.class);
assertFalse(func instanceof ICPPUnknownBinding); IType type = var.getType();
type = SemanticUtil.getNestedType(type, TDEF);
assertEquals("int *", type.toString());
} }
// template <int> void* foo(int); // template <int> void* foo(int);

View file

@ -173,18 +173,18 @@ public class EvalComma extends CPPEvaluation {
@Override @Override
public ICPPEvaluation instantiate(ICPPTemplateParameterMap tpMap, int packOffset, public ICPPEvaluation instantiate(ICPPTemplateParameterMap tpMap, int packOffset,
ICPPClassSpecialization within, int maxdepth, IASTNode point) { ICPPClassSpecialization within, int maxdepth, IASTNode point) {
ICPPEvaluation[] args = null; ICPPEvaluation[] args = fArguments;
for (int i = 0; i < fArguments.length; i++) { for (int i = 0; i < fArguments.length; i++) {
ICPPEvaluation arg = fArguments[i].instantiate(tpMap, packOffset, within, maxdepth, point); ICPPEvaluation arg = fArguments[i].instantiate(tpMap, packOffset, within, maxdepth, point);
if (arg != fArguments[i]) { if (arg != fArguments[i]) {
if (args == null) { if (args == fArguments) {
args = new ICPPEvaluation[fArguments.length]; args = new ICPPEvaluation[fArguments.length];
System.arraycopy(fArguments, 0, args, 0, fArguments.length); System.arraycopy(fArguments, 0, args, 0, fArguments.length);
} }
args[i] = arg; args[i] = arg;
} }
} }
if (args == null) if (args == fArguments)
return this; return this;
return new EvalComma(args); return new EvalComma(args);
} }

View file

@ -20,11 +20,8 @@ import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUti
import java.util.Arrays; import java.util.Arrays;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory; import org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory;
import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IFunctionType; import org.eclipse.cdt.core.dom.ast.IFunctionType;
import org.eclipse.cdt.core.dom.ast.IPointerType; import org.eclipse.cdt.core.dom.ast.IPointerType;
import org.eclipse.cdt.core.dom.ast.ISemanticProblem; import org.eclipse.cdt.core.dom.ast.ISemanticProblem;
@ -40,7 +37,6 @@ import org.eclipse.cdt.internal.core.dom.parser.ProblemType;
import org.eclipse.cdt.internal.core.dom.parser.Value; import org.eclipse.cdt.internal.core.dom.parser.Value;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPFunction; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPFunction;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPEvaluation; import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPEvaluation;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPUnknownBinding;
import org.eclipse.cdt.internal.core.dom.parser.cpp.OverloadableOperator; import org.eclipse.cdt.internal.core.dom.parser.cpp.OverloadableOperator;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPSemantics.LookupMode; import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPSemantics.LookupMode;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
@ -187,34 +183,23 @@ public class EvalFunctionCall extends CPPEvaluation {
@Override @Override
public ICPPEvaluation instantiate(ICPPTemplateParameterMap tpMap, int packOffset, public ICPPEvaluation instantiate(ICPPTemplateParameterMap tpMap, int packOffset,
ICPPClassSpecialization within, int maxdepth, IASTNode point) { ICPPClassSpecialization within, int maxdepth, IASTNode point) {
ICPPEvaluation[] args = null; ICPPEvaluation[] args = fArguments;
for (int i = 0; i < fArguments.length; i++) { for (int i = 0; i < fArguments.length; i++) {
ICPPEvaluation arg = fArguments[i].instantiate(tpMap, packOffset, within, maxdepth, point); ICPPEvaluation arg = fArguments[i].instantiate(tpMap, packOffset, within, maxdepth, point);
if (arg != fArguments[i]) { if (arg != fArguments[i]) {
if (args == null) { if (args == fArguments) {
args = new ICPPEvaluation[fArguments.length]; args = new ICPPEvaluation[fArguments.length];
System.arraycopy(fArguments, 0, args, 0, fArguments.length); System.arraycopy(fArguments, 0, args, 0, fArguments.length);
} }
args[i] = arg; args[i] = arg;
} }
} }
if (args == null) if (args == fArguments)
return this; return this;
if (args[0] instanceof EvalFunctionSet) { if (args[0] instanceof EvalFunctionSet && getOverload(point) == null) {
// Resolve the function using the parameters of the function call. // Resolve the function using the parameters of the function call.
CPPFunctionSet functionSet = ((EvalFunctionSet) args[0]).getFunctionSet(); args[0] = ((EvalFunctionSet) args[0]).resolveFunction(Arrays.copyOfRange(args, 1, args.length), point);
ICPPFunction[] functions = functionSet.getBindings();
LookupData data = new LookupData(functions[0].getNameCharArray(),
functionSet.getTemplateArguments(), point);
data.setFunctionArguments(false, Arrays.copyOfRange(args, 1, args.length));
try {
IBinding binding = CPPSemantics.resolveFunction(data, functions, true);
if (binding instanceof ICPPFunction && !(binding instanceof ICPPUnknownBinding))
args[0] = new EvalBinding(binding, null);
} catch (DOMException e) {
CCorePlugin.log(e);
}
} }
return new EvalFunctionCall(args); return new EvalFunctionCall(args);
} }

View file

@ -15,6 +15,8 @@ import static org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory.PRVALUE;
import java.util.Arrays; import java.util.Arrays;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory; import org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory;
import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IBinding;
@ -168,6 +170,29 @@ public class EvalFunctionSet extends CPPEvaluation {
return new EvalFunctionSet(new CPPFunctionSet(functions, arguments, null), fAddressOf); return new EvalFunctionSet(new CPPFunctionSet(functions, arguments, null), fAddressOf);
} }
/**
* Attempts to resolve the function using the parameters of a function call.
*
* @param args the arguments of a function call
* @param point the name lookup context
* @return the resolved or the original evaluation depending on whether function resolution
* succeeded or not
*/
public ICPPEvaluation resolveFunction(ICPPEvaluation[] args, IASTNode point) {
ICPPFunction[] functions = fFunctionSet.getBindings();
LookupData data = new LookupData(functions[0].getNameCharArray(),
fFunctionSet.getTemplateArguments(), point);
data.setFunctionArguments(false, args);
try {
IBinding binding = CPPSemantics.resolveFunction(data, functions, true);
if (binding instanceof ICPPFunction && !(binding instanceof ICPPUnknownBinding))
return new EvalBinding(binding, null);
} catch (DOMException e) {
CCorePlugin.log(e);
}
return this;
}
@Override @Override
public int determinePackSize(ICPPTemplateParameterMap tpMap) { public int determinePackSize(ICPPTemplateParameterMap tpMap) {
int r = CPPTemplates.PACK_SIZE_NOT_FOUND; int r = CPPTemplates.PACK_SIZE_NOT_FOUND;

View file

@ -14,8 +14,6 @@ package org.eclipse.cdt.internal.core.dom.parser.cpp.semantics;
import static org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory.LVALUE; import static org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory.LVALUE;
import static org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory.PRVALUE; import static org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory.PRVALUE;
import java.util.Arrays;
import org.eclipse.cdt.core.dom.ast.DOMException; import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTExpression; import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory; import org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory;
@ -291,7 +289,6 @@ public class EvalID extends CPPEvaluation {
IBinding nameOwner = fNameOwner; IBinding nameOwner = fNameOwner;
if (nameOwner instanceof ICPPClassTemplate) { if (nameOwner instanceof ICPPClassTemplate) {
// TODO(sprigogin): Figure out why testDependentExpressions_a fails without special-casing ICPPClassTemplate
nameOwner = resolveUnknown(CPPTemplates.createDeferredInstance((ICPPClassTemplate) nameOwner), nameOwner = resolveUnknown(CPPTemplates.createDeferredInstance((ICPPClassTemplate) nameOwner),
tpMap, packOffset, within, point); tpMap, packOffset, within, point);
} else if (nameOwner instanceof IType) { } else if (nameOwner instanceof IType) {
@ -303,7 +300,7 @@ public class EvalID extends CPPEvaluation {
if (fieldOwner instanceof IProblemBinding || nameOwner instanceof IProblemBinding) if (fieldOwner instanceof IProblemBinding || nameOwner instanceof IProblemBinding)
return this; return this;
if (Arrays.equals(templateArgs, fTemplateArgs) && fieldOwner == fFieldOwner && nameOwner == fNameOwner) if (templateArgs == fTemplateArgs && fieldOwner == fFieldOwner && nameOwner == fNameOwner)
return this; return this;
ICPPEvaluation eval = resolveName((ICPPClassType) nameOwner, templateArgs, point); ICPPEvaluation eval = resolveName((ICPPClassType) nameOwner, templateArgs, point);

View file

@ -105,18 +105,18 @@ public class EvalInitList extends CPPEvaluation {
@Override @Override
public ICPPEvaluation instantiate(ICPPTemplateParameterMap tpMap, int packOffset, public ICPPEvaluation instantiate(ICPPTemplateParameterMap tpMap, int packOffset,
ICPPClassSpecialization within, int maxdepth, IASTNode point) { ICPPClassSpecialization within, int maxdepth, IASTNode point) {
ICPPEvaluation[] clauses = null; ICPPEvaluation[] clauses = fClauses;
for (int i = 0; i < fClauses.length; i++) { for (int i = 0; i < fClauses.length; i++) {
ICPPEvaluation clause = fClauses[i].instantiate(tpMap, packOffset, within, maxdepth, point); ICPPEvaluation clause = fClauses[i].instantiate(tpMap, packOffset, within, maxdepth, point);
if (clause != fClauses[i]) { if (clause != fClauses[i]) {
if (clauses == null) { if (clauses == fClauses) {
clauses = new ICPPEvaluation[fClauses.length]; clauses = new ICPPEvaluation[fClauses.length];
System.arraycopy(fClauses, 0, clauses, 0, fClauses.length); System.arraycopy(fClauses, 0, clauses, 0, fClauses.length);
} }
clauses[i] = clause; clauses[i] = clause;
} }
} }
if (clauses == null) if (clauses == fClauses)
return this; return this;
return new EvalInitList(clauses); return new EvalInitList(clauses);
} }

View file

@ -146,11 +146,11 @@ public class EvalTypeId extends CPPEvaluation {
@Override @Override
public ICPPEvaluation instantiate(ICPPTemplateParameterMap tpMap, int packOffset, public ICPPEvaluation instantiate(ICPPTemplateParameterMap tpMap, int packOffset,
ICPPClassSpecialization within, int maxdepth, IASTNode point) { ICPPClassSpecialization within, int maxdepth, IASTNode point) {
ICPPEvaluation[] args = null; ICPPEvaluation[] args = fArguments;
for (int i = 0; i < fArguments.length; i++) { for (int i = 0; i < fArguments.length; i++) {
ICPPEvaluation arg = fArguments[i].instantiate(tpMap, packOffset, within, maxdepth, point); ICPPEvaluation arg = fArguments[i].instantiate(tpMap, packOffset, within, maxdepth, point);
if (arg != fArguments[i]) { if (arg != fArguments[i]) {
if (args == null) { if (args == fArguments) {
args = new ICPPEvaluation[fArguments.length]; args = new ICPPEvaluation[fArguments.length];
System.arraycopy(fArguments, 0, args, 0, fArguments.length); System.arraycopy(fArguments, 0, args, 0, fArguments.length);
} }
@ -158,11 +158,8 @@ public class EvalTypeId extends CPPEvaluation {
} }
} }
IType type = CPPTemplates.instantiateType(fInputType, tpMap, packOffset, within, point); IType type = CPPTemplates.instantiateType(fInputType, tpMap, packOffset, within, point);
if (args == null) { if (args == fArguments && type == fInputType)
if (type == fInputType) return this;
return this;
args = fArguments;
}
return new EvalTypeId(type, args); return new EvalTypeId(type, args);
} }