Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic support for c++17 deduction guides #344

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3096,14 +3096,14 @@ public void testNonAmbiguityCase_229942() throws Exception {
// B b1;
// B<> b2; // error - no default args
//
// C c1;
// C c1; // OK since C++17 via implicit deduction guide using default template argument
// C<> c2; // ok - default args
public void testMissingTemplateArgumentLists() throws Exception {
BindingAssertionHelper ba = new AST2AssertionHelper(getAboveComment(), CPP);
ba.assertProblem("B b1", 1);
ba.assertNonProblem("B<> b2", 1, ICPPTemplateDefinition.class, ICPPClassType.class);
ba.assertProblem("B<> b2", 3);
ba.assertProblem("C c1", 1);
ba.assertNonProblem("C c1", 1); // OK since C++17
ba.assertNonProblem("C<> c2", 1, ICPPTemplateDefinition.class, ICPPClassType.class);
ba.assertNonProblem("C<> c2", 3, ICPPTemplateInstance.class, ICPPClassType.class);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
/*******************************************************************************
* Copyright (c) 2023 Igor V. Kovalenko.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Igor V. Kovalenko - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.core.parser.tests.ast2.cxx17;

import static org.eclipse.cdt.core.parser.tests.ast2.CommonCPPTypes.char_;
import static org.eclipse.cdt.core.parser.tests.ast2.CommonCPPTypes.double_;
import static org.eclipse.cdt.core.parser.tests.ast2.CommonCPPTypes.int_;

import org.eclipse.cdt.core.dom.ast.IPointerType;
import org.eclipse.cdt.core.dom.ast.IProblemType;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.IVariable;
import org.eclipse.cdt.core.parser.ParserLanguage;
import org.eclipse.cdt.core.parser.tests.ast2.AST2CPPTestBase;

/**
* AST tests for C++17 deduction guides.
*/
public class DeductionGuideTest extends AST2CPPTestBase {

// template<typename T> struct S {
// template<typename U> S(S<U> s) : value(s.value) {}
// S(T v) : value(v) {}
// T value;
// };
//
// //template<typename T> S(T t) -> S<double>;
// template<typename T> S(char) -> S<char>; // invalid candidate, T cannot be deduced
// S(int) -> S<double>;
//
// void f() {
// auto dchar = S('1').value;
// auto dint = S(1).value;
// double dv = S<double>(1).value;
// int iv = S(1).value;
//
// auto sdouble = S<double>();
// auto ddouble = S(1);
//
// S copy = ddouble;
// S init{ddouble};
// S convert(ddouble);
// }
public void testDeductionGuideBasic() throws Exception {
BindingAssertionHelper bh = getAssertionHelper(ParserLanguage.CPP, ScannerKind.STD);
assertType(bh.assertNonProblem("dchar = S", 5), char_);
assertType(bh.assertNonProblem("dint = S", 4), double_);
assertType(bh.assertNonProblem("dv = S", 2), double_);
assertType(bh.assertNonProblem("iv = S", 2), int_);

IVariable varDouble = bh.assertNonProblem("sdouble = S", 7);
assertType(bh.assertNonProblem("ddouble = S", 7), varDouble.getType());

assertType(bh.assertNonProblem("copy = ddouble", 4), varDouble.getType());

assertType(bh.assertNonProblem("init{", 4), varDouble.getType());

assertType(bh.assertNonProblem("convert(", 7), varDouble.getType());
}

// template<typename T, typename U = bool> struct S {
// S(T v) : value(v) {}
// T value;
// };
//
// //template<typename T> S(T t) -> S<double>;
// template<typename T> S(char) -> S<char>;
// S(int) -> S<double>;
//
// void f() {
// auto dchar = S('1').value;
// auto dint = S(1).value;
// double dv = S<double>(1).value;
// int iv = S(1).value;
// }
public void testDeductionGuideWithDefaultTemplateArg() throws Exception {
BindingAssertionHelper bh = getAssertionHelper(ParserLanguage.CPP, ScannerKind.STD);
assertType(bh.assertNonProblem("dchar = S", 5), char_);
assertType(bh.assertNonProblem("dint = S", 4), double_);
assertType(bh.assertNonProblem("dv = S", 2), double_);
assertType(bh.assertNonProblem("iv = S", 2), int_);
}

// template<typename T> struct S; // No definition
//
// S(char) -> S<char>;
// S(int) -> S<double>;
//
// void f() {
// auto schar = S<char>();
// auto dchar = S('1');
// auto sdouble = S<double>();
// auto ddouble = S(1);
// S copy = ddouble;
// S init{ddouble};
// S convert(ddouble);
// }
public void testDeductionGuideWithTemplateDeclaration() throws Exception {
BindingAssertionHelper bh = getAssertionHelper(ParserLanguage.CPP, ScannerKind.STD);

IVariable varChar = bh.assertNonProblem("schar = S", 5);
assertType(bh.assertNonProblem("dchar = S", 5), varChar.getType());

IVariable varDouble = bh.assertNonProblem("sdouble = S", 7);
assertType(bh.assertNonProblem("ddouble = S", 7), varDouble.getType());

assertType(bh.assertNonProblem("copy = ddouble", 4), varDouble.getType());

assertType(bh.assertNonProblem("init{", 4), varDouble.getType());

assertType(bh.assertNonProblem("convert(", 7), varDouble.getType());
}

// template<class T>
// struct UniquePtr
// {
// UniquePtr(T* t);
// };
//
// UniquePtr dp{new auto(2.0)};
// auto da = dp;
public void testMinimal() throws Exception {
parseAndCheckBindings(ScannerKind.STD);
}

// template<class T> struct S {
// S();
// };
//
// S<int> sInt;
//
// S s1;
//
// S() -> S<int>;
//
// S s2;
public void testDeduceFromEmptyInitializer() throws Exception {
BindingAssertionHelper bh = getAssertionHelper(ParserLanguage.CPP, ScannerKind.STD);
IVariable varSInt = bh.assertNonProblem("sInt", 4);
bh.assertProblem("S s1", 1);
bh.assertNonProblem("S s2", 1);
assertType(bh.assertNonProblem("s2", 2), varSInt.getType());
}

// template<class T>
// struct S
// {
// S(T t);
// template <typename U> S(U u);
// };
//
// S(char c) -> S<long>;
// S(int i) -> S<double>;
//
// auto v = S(1);
public void testViaFunctionSetFromConstructors() throws Exception {
parseAndCheckBindings(ScannerKind.STD);
}

// template<class T> struct S{};
//
// S* pointer;
public void testNoDeductionForPointer() throws Exception {
BindingAssertionHelper bh = getAssertionHelper(ParserLanguage.CPP, ScannerKind.STD);
IType pointedType = ((IPointerType) ((IVariable) bh.assertNonProblem("pointer")).getType()).getType();
assertTrue(pointedType instanceof IProblemType);
bh.assertProblem("S*", 1);
}

// template<class T> struct S;
//
// S* pointer;
public void testNoDeductionForPointerNoDefinition() throws Exception {
BindingAssertionHelper bh = getAssertionHelper(ParserLanguage.CPP, ScannerKind.STD);
IType pointedType = ((IPointerType) ((IVariable) bh.assertNonProblem("pointer")).getType()).getType();
assertTrue(pointedType instanceof IProblemType);
bh.assertProblem("S*", 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*******************************************************************************
* Copyright (c) 2023 Igor V. Kovalenko.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Igor V. Kovalenko - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.index.tests;

import static org.eclipse.cdt.core.parser.tests.ast2.CommonCPPTypes.char_;
import static org.eclipse.cdt.core.parser.tests.ast2.CommonCPPTypes.double_;
import static org.eclipse.cdt.core.parser.tests.ast2.CommonCPPTypes.int_;

import org.eclipse.cdt.core.dom.ast.IVariable;
import org.eclipse.cdt.core.testplugin.TestScannerProvider;

import junit.framework.TestSuite;

/**
* AST tests for C++17 deduction guides via PDOM.
*/
public abstract class IndexDeductionGuideTest extends IndexBindingResolutionTestBase {
private static void cxx17SetUp() {
// Deduction guides are now enabled unconditionally
}

private static void cxx17TearDown() {
TestScannerProvider.clear();
}

public class Cxx17ReferencedProject extends ReferencedProject {
public Cxx17ReferencedProject() {
super(true /* cpp */);
}

@Override
public void setUp() throws Exception {
cxx17SetUp();
super.setUp();
}

@Override
public void tearDown() throws Exception {
super.tearDown();
cxx17TearDown();
}
}

public class Cxx17SinglePDOMTestStrategy extends SinglePDOMTestStrategy {
public Cxx17SinglePDOMTestStrategy() {
super(true /* cpp */);
}

@Override
public void setUp() throws Exception {
cxx17SetUp();
super.setUp();
}

@Override
public void tearDown() throws Exception {
super.tearDown();
cxx17TearDown();
}
}

public static class IndexDeductionGuideTestSingleProject extends IndexDeductionGuideTest {
public IndexDeductionGuideTestSingleProject() {
setStrategy(new Cxx17SinglePDOMTestStrategy());
}

public static TestSuite suite() {
return suite(IndexDeductionGuideTestSingleProject.class);
}
}

public static class IndexDeductionGuideTestProjectWithDepProj extends IndexDeductionGuideTest {
public IndexDeductionGuideTestProjectWithDepProj() {
setStrategy(new Cxx17ReferencedProject());
}

public static TestSuite suite() {
return suite(IndexDeductionGuideTestProjectWithDepProj.class);
}
}

public static void addTests(TestSuite suite) {
suite.addTest(IndexDeductionGuideTestSingleProject.suite());
suite.addTest(IndexDeductionGuideTestProjectWithDepProj.suite());
}

// template<typename T> struct S {
// template<typename U> S(S<U> s) : value(s.value) {}
// S(T v) : value(v) {}
// T value;
// };
//
// //template<typename T> S(T t) -> S<double>;
// template<typename T> S(char) -> S<char>; // invalid candidate, T cannot be deduced
// S(int) -> S<double>;

// void f() {
// auto dchar = S('1').value;
// auto dint = S(1).value;
// double dv = S<double>(1).value;
// int iv = S(1).value;
//
// auto sdouble = S<double>();
// auto ddouble = S(1);
//
// S copy = ddouble;
// S init{ddouble};
// S convert(ddouble);
// }
//
// constexpr size_t value = sizeof(ddouble.value);
public void testDeductionGuideBasicHeader() throws Exception {
assertType(getBindingFromASTName("dchar = S", 5), char_);
assertType(getBindingFromASTName("dint = S", 4), double_);
assertType(getBindingFromASTName("dv = S", 2), double_);
assertType(getBindingFromASTName("iv = S", 2), int_);

IVariable varDouble = (IVariable) getBindingFromASTName("sdouble = S", 7);
assertType(getBindingFromASTName("ddouble = S", 7), varDouble.getType());

assertType(getBindingFromASTName("copy = ddouble", 4), varDouble.getType());

assertType(getBindingFromASTName("init{", 4), varDouble.getType());

assertType(getBindingFromASTName("convert(", 7), varDouble.getType());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDeductionGuide;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTElaboratedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTExplicitTemplateInstantiation;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator;
Expand Down Expand Up @@ -544,7 +545,10 @@ private CElement createTypedefOrFunctionOrVariable(Parent parent, IASTDeclSpecif
return createTypeDef(parent, declSpecifier, declarator);
}
IASTDeclarator typeRelevant = ASTQueries.findTypeRelevantDeclarator(declarator);
if (typeRelevant instanceof IASTFunctionDeclarator) {
if (typeRelevant instanceof ICPPASTDeductionGuide) {
// TODO [cmodel] deduction guide
return null;
} else if (typeRelevant instanceof IASTFunctionDeclarator) {
return createFunctionDeclaration(parent, declSpecifier, (IASTFunctionDeclarator) typeRelevant, isTemplate);
}
return createVariable(parent, declSpecifier, declarator, isTemplate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public static class ScopeLookupData {
private boolean fPrefixLookup;
private boolean fIgnorePointOfDeclaration;
private boolean fArgumentDependent;
private boolean fDeductionGuidesOnly = false;

public ScopeLookupData(IASTName name, boolean resolve, boolean prefixLookup) {
if (name == null)
Expand Down Expand Up @@ -172,6 +173,11 @@ public final void setArgumentDependent(boolean argumentDependent) {
fArgumentDependent = argumentDependent;
}

/** @since 8.1 */
public final void setDeductionGuidesOnly(boolean deductionGuidesOnly) {
fDeductionGuidesOnly = deductionGuidesOnly;
}

public final void setLookupKey(char[] key) {
fLookupKey = key;
}
Expand Down Expand Up @@ -201,6 +207,11 @@ public final boolean isArgumentDependent() {
return fArgumentDependent;
}

/** @since 8.1 */
public final boolean isDeductionGuidesOnly() {
return fDeductionGuidesOnly;
}

public final IIndexFileSet getIncludedFiles() {
return fTu == null ? IIndexFileSet.EMPTY : fTu.getIndexFileSet();
}
Expand Down
Loading