Skip to content

[cxx-interop] Add class template tests #33420

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

Merged
merged 1 commit into from
Oct 26, 2020
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
@@ -0,0 +1,11 @@
#ifndef TEST_INTEROP_CXX_TEMPLATES_INPUTS_CLASS_TEMPLATE_NON_TYPE_PARAMETER_H
#define TEST_INTEROP_CXX_TEMPLATES_INPUTS_CLASS_TEMPLATE_NON_TYPE_PARAMETER_H

template<class T, auto Size>
struct MagicArray {
T t[Size];
};

typedef MagicArray<int, 2> MagicIntPair;

#endif // TEST_INTEROP_CXX_TEMPLATES_INPUTS_CLASS_TEMPLATE_NON_TYPE_PARAMETER_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef TEST_INTEROP_CXX_TEMPLATES_INPUTS_CLASS_TEMPLATE_TEMPLATE_PARAMETER_H
#define TEST_INTEROP_CXX_TEMPLATES_INPUTS_CLASS_TEMPLATE_TEMPLATE_PARAMETER_H

struct IntWrapper {
int value;
int getValue() const { return value; }
};

template<class T>
struct MagicWrapper {
T t;
int getValuePlusArg(int arg) const { return t.getValue() + arg; }
};

template<template <class> class V>
struct TemplatedMagicWrapper {
V<IntWrapper> i;
int getValuePlusTwiceTheArg(int arg) const { return i.getValuePlusArg(arg) + arg; }
};

typedef TemplatedMagicWrapper<MagicWrapper> TemplatedWrappedMagicInt;
typedef MagicWrapper<IntWrapper> WrappedMagicInt;
#endif // TEST_INTEROP_CXX_TEMPLATES_INPUTS_CLASS_TEMPLATE_TEMPLATE_PARAMETER_H
30 changes: 30 additions & 0 deletions test/Interop/Cxx/templates/Inputs/class-template-variadic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef TEST_INTEROP_CXX_TEMPLATES_INPUTS_CLASS_TEMPLATE_VARIADIC_H
#define TEST_INTEROP_CXX_TEMPLATES_INPUTS_CLASS_TEMPLATE_VARIADIC_H

template <class... Ts> struct Tuple {};

template <>
struct Tuple<> {
void set() {}
};

template <class T, class... Ts>
struct Tuple<T, Ts...> : Tuple<Ts...> {
Tuple(T t, Ts... ts) : Tuple<Ts...>(ts...), _t(t) {}

void set(T t, Ts... ts) { _t = t; Tuple<Ts...>::set(ts...); }

T first() { return _t; }
Tuple<Ts...> rest() { return *this; }

T _t;
};

struct IntWrapper {
int value;
int getValue() const { return value; }
};

typedef Tuple<IntWrapper, IntWrapper> Pair;

#endif // TEST_INTEROP_CXX_TEMPLATES_INPUTS_CLASS_TEMPLATE_VARIADIC_H
12 changes: 12 additions & 0 deletions test/Interop/Cxx/templates/Inputs/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,15 @@ module Mangling {
module LinkageOfSwiftSymbolsForImportedTypes {
header "linkage-of-swift-symbols-for-imported-types.h"
}

module ClassTemplateVariadic {
header "class-template-variadic.h"
}

module ClassTemplateNonTypeParameter {
header "class-template-non-type-parameter.h"
}

module ClassTemplateTemplateParameter {
header "class-template-template-parameter.h"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-cxx-interop)
//
// REQUIRES: executable_test

import ClassTemplateNonTypeParameter
import StdlibUnittest

var TemplatesTestSuite = TestSuite("TemplatesTestSuite")

TemplatesTestSuite.test("non-type-parameter") {
var pair = MagicIntPair(t: (1, 2))
expectEqual(pair.t, (1, 2))
}

runAllTests()
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-cxx-interop)
//
// REQUIRES: executable_test

import ClassTemplateTemplateParameter
import StdlibUnittest

var TemplatesTestSuite = TestSuite("TemplatesTestSuite")

TemplatesTestSuite.test("template-template-parameter") {
let myInt = IntWrapper(value: 42)
var magicInt = WrappedMagicInt(t: myInt)
var templatedWrappedMagicInt = TemplatedWrappedMagicInt(i: magicInt)
expectEqual(templatedWrappedMagicInt.getValuePlusTwiceTheArg(10), 62)
}

runAllTests()
27 changes: 27 additions & 0 deletions test/Interop/Cxx/templates/class-template-variadic.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-cxx-interop)
//
// REQUIRES: executable_test
//
// We can't yet call member functions correctly on Windows (SR-13129).
// XFAIL: OS=windows-msvc

import ClassTemplateVariadic
import StdlibUnittest

var TemplatesTestSuite = TestSuite("TemplatesTestSuite")

TemplatesTestSuite.test("variadic-class-template") {
let a = IntWrapper(value: 10)
let b = IntWrapper(value: 20)

var pair = Pair()
pair.set(a, b)

var pairA = pair.first()
var restB = pair.rest()
var pairB = restB.first()
expectEqual(pairA.getValue(), 10)
expectEqual(pairB.getValue(), 20)
}

runAllTests()
12 changes: 12 additions & 0 deletions test/Interop/Cxx/templates/demangling.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// RUN: %target-swift-emit-ir %s -I %S/Inputs -enable-cxx-interop | grep 'define.*swiftcc.*$' | grep -o '[[:alnum:]]*__CxxTemplateInst[[:alnum:]]*' | xargs %swift-demangle | %FileCheck %s

import Mangling

public func receiveInstantiation(_ i: inout WrappedMagicInt) {}

public func returnInstantiation() -> WrappedMagicInt {
return WrappedMagicInt()
}

// CHECK: $s10demangling20receiveInstantiationyySo34__CxxTemplateInst12MagicWrapperIiEVzF ---> demangling.receiveInstantiation(inout __C.__CxxTemplateInst12MagicWrapperIiE) -> ()
// CHECK: $s10demangling19returnInstantiationSo34__CxxTemplateInst12MagicWrapperIiEVyF ---> demangling.returnInstantiation() -> __C.__CxxTemplateInst12MagicWrapperIiE