File: generic-function-execution.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (228 lines) | stat: -rw-r--r-- 6,413 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// RUN: %empty-directory(%t)

// RUN: %target-swift-frontend %S/generic-function-in-cxx.swift -typecheck -module-name Functions -clang-header-expose-decls=all-public -emit-clang-header-path %t/functions.h

// RUN: %target-interop-build-clangxx -std=gnu++20 -c %s -I %t -o %t/swift-functions-execution.o
// RUN: %target-interop-build-swift %S/generic-function-in-cxx.swift -o %t/swift-functions-execution -Xlinker %t/swift-functions-execution.o -module-name Functions -Xfrontend -entry-point-function-name -Xfrontend swiftMain

// RUN: %target-codesign %t/swift-functions-execution
// RUN: %target-run %t/swift-functions-execution | %FileCheck %s

// REQUIRES: executable_test

#include <cassert>
#include "functions.h"

int main() {
  using namespace Functions;

  static_assert(noexcept(genericPrintFunctionTwoArg<int>(0, 0)), "noexcept function");

  {
    int i = 2;
    genericPrintFunctionTwoArg(i, 3);
// CHECK:      X: 2
// CHECK-NEXT: Y: 3
  }

  {
    int8_t x = -1;
    genericPrintFunction(x);
// CHECK-NEXT: Int8 value=-1
  }

  {
    uint8_t x = 0xff;
    genericPrintFunction(x);
// CHECK-NEXT: UInt8 value=255
  }

  {
    int16_t x = -9856;
    genericPrintFunction(x);
// CHECK-NEXT: Int16 value=-9856
  }

  {
    uint16_t x = 3248;
    genericPrintFunction(x);
// CHECK-NEXT: UInt16 value=3248
  }

  {
    int32_t x = -0x12343;
    genericPrintFunction(x);
// CHECK-NEXT: Int32 value=-74563
  }

  {
    uint32_t x = 0xC0011334;
    genericPrintFunction(x);
// CHECK-NEXT: UInt32 value=3221295924
  }

  {
    int64_t x = -123456789123;
    genericPrintFunction(x);
// CHECK-NEXT: Int64 value=-123456789123
  }

  {
    uint64_t x = 0x12345678abcd1234;
    genericPrintFunction(x);
// CHECK-NEXT: UInt64 value=1311768467750064692
  }

  {
    float x = -54.0f;
    genericPrintFunction(x);
// CHECK-NEXT: Float value=-54.0
  }

  {
    double x = 1.25;
    genericPrintFunction(x);
// CHECK-NEXT: Double value=1.25
  }

  {
    bool x = true;
    genericPrintFunction(x);
// CHECK-NEXT: Bool value=true
  }

  {
    int i = 0;
    void *x = &i;
    genericPrintFunction(x);
// CHECK-NEXT: OpaquePointer value
  }

  {
    int i = 55;
    int j = 66;
    bool x = false;
    genericPrintFunctionMultiGeneric<int, bool>(-78, i, j, 45, x);
// CHECK-NEXT: Int32 value 1=55
// CHECK-NEXT: Int32 value 2=66
// CHECK-NEXT: Bool value 1=false
// CHECK-NEXT: other values=-78,45
  }

  {
    int x = 42;
    int y = -13;
    genericSwap(x, y);
    assert(x == -13);
    assert(y == 42);
  }

  {
    int x = 4;
    assert(genericRet(x) == 4);
  }

  {
    double x = -19.75;
    assert(genericRet(x) == -19.75);
  }

  {
    auto tc = createTestClass();
    genericPrintFunction(tc);
  }
// CHECK-NEXT: TestClass value=Functions.TestClass
// CHECK-NEXT: deinit TestClass

  {
    auto tc = createTestClass();
    auto tc2 = genericRet(tc);
    assert(swift::_impl::_impl_RefCountedClass::getOpaquePointer(tc) ==
           swift::_impl::_impl_RefCountedClass::getOpaquePointer(tc2));
    genericPrintFunction(tc2);
  }
// CHECK-NEXT: TestClass value=Functions.TestClass
// CHECK-NEXT: deinit TestClass

  {
    auto tc = createTestClass();
    auto tc2 = createTestClass();
    const auto p1 = swift::_impl::_impl_RefCountedClass::getOpaquePointer(tc);
    const auto p2 = swift::_impl::_impl_RefCountedClass::getOpaquePointer(tc2);
    assert(p1 != p2);
    genericSwap(tc, tc2);
    assert(p2 == swift::_impl::_impl_RefCountedClass::getOpaquePointer(tc));
    assert(p1 == swift::_impl::_impl_RefCountedClass::getOpaquePointer(tc2));
  }
// CHECK-NEXT: deinit TestClass
// CHECK-NEXT: deinit TestClass

  {
    auto x = createTestLargeStruct(0);
    genericPrintFunction(x);
  }
// CHECK-NEXT: TestLargeStruct value=TestLargeStruct(x1: 0, x2: 1, x3: -1, x4: 0, x5: 2, x6: -2)

  {
    auto x = createTestLargeStruct(11);
    auto y = createTestLargeStruct(-9);
    genericPrintFunction(x);
    genericPrintFunction(y);
    genericSwap(x, y);
    genericPrintFunction(x);
    genericPrintFunction(y);
    auto xy = genericRet(x);
    genericPrintFunction(xy);
    xy.mut();
    genericPrintFunction(xy);
    genericPrintFunction(x);
  }
// CHECK-NEXT: TestLargeStruct value=TestLargeStruct(x1: 11, x2: 12, x3: 10, x4: 11, x5: 13, x6: 9)
// CHECK-NEXT: TestLargeStruct value=TestLargeStruct(x1: -9, x2: -8, x3: -10, x4: -9, x5: -7, x6: -11)
// CHECK-NEXT: TestLargeStruct value=TestLargeStruct(x1: -9, x2: -8, x3: -10, x4: -9, x5: -7, x6: -11)
// CHECK-NEXT: TestLargeStruct value=TestLargeStruct(x1: 11, x2: 12, x3: 10, x4: 11, x5: 13, x6: 9)
// CHECK-NEXT: TestLargeStruct value=TestLargeStruct(x1: -9, x2: -8, x3: -10, x4: -9, x5: -7, x6: -11)
// CHECK-NEXT: TestLargeStruct value=TestLargeStruct(x1: 9, x2: -8, x3: -10, x4: -9, x5: -7, x6: -7)
// CHECK-NEXT: TestLargeStruct value=TestLargeStruct(x1: -9, x2: -8, x3: -10, x4: -9, x5: -7, x6: -11)

  {
    auto x = createTestSmallStruct(45);
    auto y = createTestSmallStruct(0xFed1);
    genericPrintFunction(x);
    genericPrintFunction(y);
    genericSwap(y, x);
    genericPrintFunction(x);
    genericPrintFunction(y);
    auto xy = genericRet(x);
    genericPrintFunction(xy);
    xy.mut();
    genericPrintFunction(xy);
    genericPrintFunction(x);
  }
// CHECK-NEXT: TestSmallStruct value=TestSmallStruct(x1: 45)
// CHECK-NEXT: TestSmallStruct value=TestSmallStruct(x1: 65233)
// CHECK-NEXT: TestSmallStruct value=TestSmallStruct(x1: 65233)
// CHECK-NEXT: TestSmallStruct value=TestSmallStruct(x1: 45)
// CHECK-NEXT: TestSmallStruct value=TestSmallStruct(x1: 65233)
// CHECK-NEXT: TestSmallStruct value=TestSmallStruct(x1: 4294902062)
// CHECK-NEXT: TestSmallStruct value=TestSmallStruct(x1: 65233)
  {
    auto x = createTestSmallStruct(42);
    int passThru = x.genericMethodPassThrough((int)555);
    assert(passThru == 555);
    auto xprime = x.genericMethodPassThrough(x);
    genericPrintFunction(xprime);
    x.genericMethodMutTake((uint32_t)16);
    genericPrintFunction(xprime);
    genericPrintFunction(x);
    x.genericMethodMutTake(xprime);
    genericPrintFunction(xprime);
    genericPrintFunction(x);
  }
// CHECK-NEXT: TestSmallStruct value=TestSmallStruct(x1: 42)
// CHECK-NEXT: TestSmallStruct value=TestSmallStruct(x1: 42)
// CHECK-NEXT: TestSmallStruct value=TestSmallStruct(x1: 58)
// CHECK-NEXT: TestSmallStruct value=TestSmallStruct(x1: 42)
// CHECK-NEXT: TestSmallStruct value=TestSmallStruct(x1: 57)
  return 0;
}