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

Implement deduction guide template #580

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 @@ -631,13 +631,7 @@ public void setUp() throws Exception {

setTestSpecificFlags(sourceContents);

IFile file = TestSourceReader.createFile(cproject.getProject(), new Path("header.h"), headerContents);
CCorePlugin.getIndexManager().setIndexerId(cproject, IPDOMManager.ID_FAST_INDEXER);
waitForIndexer(cproject);

IFile cppfile = TestSourceReader.createFile(cproject.getProject(),
new Path("references.c" + (cpp ? "pp" : "")), sourceContents);
waitForIndexer(cproject);
IFile cppfile = createSourceFilesAndIndex(headerContents, sourceContents);

if (DEBUG) {
System.out.println("Project PDOM: " + getName());
Expand All @@ -650,6 +644,18 @@ public void setUp() throws Exception {
ast = TestSourceReader.createIndexBasedAST(index, cproject, cppfile);
}

IFile createSourceFilesAndIndex(String headerContents, String sourceContents) throws Exception {
IFile file = TestSourceReader.createFile(cproject.getProject(), new Path("header.h"), headerContents);
CCorePlugin.getIndexManager().setIndexerId(cproject, IPDOMManager.ID_FAST_INDEXER);
waitForIndexer(cproject);

IFile cppfile = TestSourceReader.createFile(cproject.getProject(),
new Path("references.c" + (cpp ? "pp" : "")), sourceContents);
waitForIndexer(cproject);

return cppfile;
}

@Override
public void tearDown() throws Exception {
if (index != null) {
Expand All @@ -670,6 +676,35 @@ public IIndex getIndex() {
public boolean isCompositeIndex() {
return false;
}

protected boolean getIsCPP() {
return cpp;
}
}

/**
* This strategy is similar to SinglePDOMTestStrategy but it does reindex project after adding all files.
*/
protected class SinglePDOMReindexedTestStrategy extends SinglePDOMTestStrategy {

public SinglePDOMReindexedTestStrategy(boolean cpp) {
super(cpp);
}

@Override
IFile createSourceFilesAndIndex(String headerContents, String sourceContents) throws Exception {
CCorePlugin.getIndexManager().setIndexerId(getCProject(), IPDOMManager.ID_FAST_INDEXER);
waitForIndexer(getCProject());

TestSourceReader.createFile(getCProject().getProject(), new Path("header.h"), headerContents);
IFile cppfile = TestSourceReader.createFile(getCProject().getProject(),
new Path("references.c" + (getIsCPP() ? "pp" : "")), sourceContents);

CCorePlugin.getIndexManager().reindex(getCProject());
waitForIndexer(getCProject());

return cppfile;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,71 +18,36 @@
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();
public static class IndexDeductionGuideTestSingleProject extends IndexDeductionGuideTest {
public IndexDeductionGuideTestSingleProject() {
setStrategy(new SinglePDOMTestStrategy(true /* cpp */));
}

@Override
public void tearDown() throws Exception {
super.tearDown();
cxx17TearDown();
public static TestSuite suite() {
return suite(IndexDeductionGuideTestSingleProject.class);
}
}

public static class IndexDeductionGuideTestSingleProject extends IndexDeductionGuideTest {
public IndexDeductionGuideTestSingleProject() {
setStrategy(new Cxx17SinglePDOMTestStrategy());
public static class IndexDeductionGuideTestSingleProjectReindexed extends IndexDeductionGuideTest {
public IndexDeductionGuideTestSingleProjectReindexed() {
setStrategy(new SinglePDOMReindexedTestStrategy(true /* cpp */));
}

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

public static class IndexDeductionGuideTestProjectWithDepProj extends IndexDeductionGuideTest {
public IndexDeductionGuideTestProjectWithDepProj() {
setStrategy(new Cxx17ReferencedProject());
setStrategy(new ReferencedProject(true /* cpp */));
}

public static TestSuite suite() {
Expand All @@ -92,6 +57,7 @@ public static TestSuite suite() {

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

Expand Down Expand Up @@ -135,4 +101,41 @@ public void testDeductionGuideBasicHeader() throws Exception {

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

// #pragma once
//
// namespace test
// {
// typedef double result_t;
// template<typename _Tp = void> struct Dependent;
// }
//
// template<typename _Key, typename _Equal> struct Intermediate
// {
// public:
// typedef test::result_t result_type;
// };
//
// template<typename _Key, typename _Dependent = test::Dependent<_Key>>
// class Base {
// typedef Intermediate<_Key, _Dependent> _Intermediate;
//
// public:
// typedef typename _Intermediate::result_type result_type;
// };

// using BaseType = Base<int>::result_type; // test marker
//
// namespace test_std_alternative {
// template<typename A, typename B>
// class Unrelated;
//
// using namespace test;
// template<typename _KeyInUnrelated>
// Unrelated(_KeyInUnrelated)
// -> Unrelated<_KeyInUnrelated, Dependent<_KeyInUnrelated>>;
// }
public void testDeductionGuideTemplateIssue438() throws Exception {
getBindingFromASTName("Base<int>::result_type; // test marker", 22);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*******************************************************************************
* 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.core.dom.parser.cpp;

import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateDefinition;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateParameter;

/**
* Represents c++17 deduction guide template.
*/
public class CPPDeductionGuideTemplate extends CPPDeductionGuide
implements ICPPTemplateDefinition, ICPPTemplateParameterOwner {

public CPPDeductionGuideTemplate(IASTDeclarator fnDecl, ICPPFunction functionBinding) {
super(fnDecl, functionBinding);
}

@Override
public ICPPTemplateParameter[] getTemplateParameters() {
if (functionBinding instanceof ICPPTemplateDefinition template) {
return template.getTemplateParameters();
}
return null;
}

@Override
public IBinding resolveTemplateParameter(ICPPTemplateParameter param) {
if (functionBinding instanceof ICPPTemplateParameterOwner owner) {
return owner.resolveTemplateParameter(param);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPConstructor;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPConstructorTemplate;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPDeductionGuide;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPDeductionGuideTemplate;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPEnumeration;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPEnumerator;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPField;
Expand Down Expand Up @@ -999,7 +1000,8 @@ private static IBinding createBinding(IASTDeclarator declarator) {
}

if (isDeductionGuide) {
binding = new CPPDeductionGuide(typeRelevantDtor, (ICPPFunction) binding);
binding = template ? new CPPDeductionGuideTemplate(typeRelevantDtor, (ICPPFunction) binding)
: new CPPDeductionGuide(typeRelevantDtor, (ICPPFunction) binding);
} else {
binding = CPPSemantics.checkDeclSpecifier(binding, name, parent);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ PDOMBinding createBinding(PDOMNode parent, IBinding binding, long fileLocalRec)
pdomBinding = new PDOMCPPMethod(this, parent, (ICPPMethod) binding);
}
} else if (binding instanceof ICPPDeductionGuide guide) {
if (guide.getFunctionBinding() instanceof ICPPFunctionTemplate) {
if (guide instanceof ICPPTemplateDefinition) {
pdomBinding = new PDOMCPPDeductionGuideTemplate(this, parent, guide);
} else {
pdomBinding = new PDOMCPPDeductionGuide(this, parent, guide);
Expand Down Expand Up @@ -1145,7 +1145,7 @@ else if (binding instanceof ICPPTemplateNonTypeParameter)
// this must be before functions
return CPPMETHOD;
} else if (binding instanceof ICPPDeductionGuide guide) {
if (guide.getFunctionBinding() instanceof ICPPFunctionTemplate) {
if (guide instanceof ICPPTemplateDefinition) {
return CPP_DEDUCTION_GUIDE_TEMPLATE;
} else {
return CPP_DEDUCTION_GUIDE;
Expand Down
Loading