field.carbon 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. //
  5. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/primitives.carbon
  6. //
  7. // AUTOUPDATE
  8. // TIP: To test this file alone, run:
  9. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/interop/cpp/class/field.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/class/field.carbon
  12. // --- struct.h
  13. struct Struct {
  14. int a;
  15. int b;
  16. int* _Nonnull p;
  17. };
  18. // --- use_struct_fields.carbon
  19. library "[[@TEST_NAME]]";
  20. import Cpp library "struct.h";
  21. fn F(s: Cpp.Struct) -> (i32, i32, i32) {
  22. //@dump-sem-ir-begin
  23. return (s.a, s.b, *s.p);
  24. //@dump-sem-ir-end
  25. }
  26. // --- union.h
  27. union Union {
  28. int a;
  29. int b;
  30. int* _Nonnull p;
  31. };
  32. // --- use_union_fields.carbon
  33. library "[[@TEST_NAME]]";
  34. import Cpp library "union.h";
  35. fn F(u: Cpp.Union) -> i32 {
  36. //@dump-sem-ir-begin
  37. return u.a;
  38. //@dump-sem-ir-end
  39. }
  40. fn G(u: Cpp.Union) -> i32 {
  41. //@dump-sem-ir-begin
  42. return u.b;
  43. //@dump-sem-ir-end
  44. }
  45. fn H(u: Cpp.Union) -> i32 {
  46. //@dump-sem-ir-begin
  47. return *u.p;
  48. //@dump-sem-ir-end
  49. }
  50. // --- anon_struct_union.h
  51. struct A {
  52. union {
  53. struct {
  54. short a_0;
  55. short b_2;
  56. int c_4;
  57. };
  58. struct {
  59. char d_0;
  60. char e_1;
  61. int f_4;
  62. short g_8;
  63. };
  64. };
  65. int h_12;
  66. };
  67. // --- use_anon_struct_union.carbon
  68. library "[[@TEST_NAME]]";
  69. import Cpp library "anon_struct_union.h";
  70. fn GetF(a: Cpp.A) -> i32 { return a.f_4; }
  71. // --- with_bitfields.h
  72. struct Struct {
  73. int a;
  74. int b : 2;
  75. };
  76. union Union {
  77. int c;
  78. int d : 3;
  79. };
  80. // --- use_non_bitfields_in_type_with_bitfields.carbon
  81. library "[[@TEST_NAME]]";
  82. import Cpp library "with_bitfields.h";
  83. fn F(s: Cpp.Struct) -> i32 {
  84. //@dump-sem-ir-begin
  85. return s.a;
  86. //@dump-sem-ir-end
  87. }
  88. fn G(s: Cpp.Union) -> i32 {
  89. //@dump-sem-ir-begin
  90. return s.c;
  91. //@dump-sem-ir-end
  92. }
  93. // --- fail_todo_use_bitfields.carbon
  94. library "[[@TEST_NAME]]";
  95. // CHECK:STDERR: fail_todo_use_bitfields.carbon:[[@LINE+4]]:10: in file included here [InCppInclude]
  96. // CHECK:STDERR: ./with_bitfields.h:4:7: error: semantics TODO: `Unsupported: field declaration has unhandled type or kind` [SemanticsTodo]
  97. // CHECK:STDERR: int b : 2;
  98. // CHECK:STDERR: ^
  99. import Cpp library "with_bitfields.h";
  100. fn F(s: Cpp.Struct) -> i32 {
  101. //@dump-sem-ir-begin
  102. // CHECK:STDERR: fail_todo_use_bitfields.carbon:[[@LINE+8]]:10: note: in `Cpp` name lookup for `b` [InCppNameLookup]
  103. // CHECK:STDERR: return s.b;
  104. // CHECK:STDERR: ^~~
  105. // CHECK:STDERR:
  106. // CHECK:STDERR: fail_todo_use_bitfields.carbon:[[@LINE-8]]:10: in file included here [InCppInclude]
  107. // CHECK:STDERR: ./with_bitfields.h:9:7: error: semantics TODO: `Unsupported: field declaration has unhandled type or kind` [SemanticsTodo]
  108. // CHECK:STDERR: int d : 3;
  109. // CHECK:STDERR: ^
  110. return s.b;
  111. //@dump-sem-ir-end
  112. }
  113. fn G(s: Cpp.Union) -> i32 {
  114. //@dump-sem-ir-begin
  115. // CHECK:STDERR: fail_todo_use_bitfields.carbon:[[@LINE+4]]:10: note: in `Cpp` name lookup for `d` [InCppNameLookup]
  116. // CHECK:STDERR: return s.d;
  117. // CHECK:STDERR: ^~~
  118. // CHECK:STDERR:
  119. return s.d;
  120. //@dump-sem-ir-end
  121. }
  122. // --- unsupported_members.h
  123. struct UnsupportedMembers {
  124. // Volatile is not supported.
  125. volatile int is_volatile;
  126. // Nullable pointers are not supported.
  127. int *is_nullable;
  128. // References are not supported.
  129. int &is_reference;
  130. // But this should be fine.
  131. int integer;
  132. };
  133. void Consume(UnsupportedMembers);
  134. // --- supported_members_in_type_with_unsupported_members.carbon
  135. library "[[@TEST_NAME]]";
  136. import Cpp library "unsupported_members.h";
  137. fn Test(m: Cpp.UnsupportedMembers*) {
  138. // OK: Can still access supported members.
  139. m->integer = 42;
  140. // OK: Can still pass the class to a C++ function.
  141. Cpp.Consume(*m);
  142. }
  143. // --- fail_use_unsupported_members.carbon
  144. library "[[@TEST_NAME]]";
  145. // CHECK:STDERR: fail_use_unsupported_members.carbon:[[@LINE+4]]:10: in file included here [InCppInclude]
  146. // CHECK:STDERR: ./unsupported_members.h:4:16: error: semantics TODO: `Unsupported: field declaration has unhandled type or kind` [SemanticsTodo]
  147. // CHECK:STDERR: volatile int is_volatile;
  148. // CHECK:STDERR: ^
  149. import Cpp library "unsupported_members.h";
  150. fn Test(m: Cpp.UnsupportedMembers*) {
  151. // CHECK:STDERR: fail_use_unsupported_members.carbon:[[@LINE+8]]:16: note: in `Cpp` name lookup for `is_volatile` [InCppNameLookup]
  152. // CHECK:STDERR: let a: i32 = m->is_volatile;
  153. // CHECK:STDERR: ^~~~~~~~~~~~~~
  154. // CHECK:STDERR:
  155. // CHECK:STDERR: fail_use_unsupported_members.carbon:[[@LINE-7]]:10: in file included here [InCppInclude]
  156. // CHECK:STDERR: ./unsupported_members.h:6:8: error: semantics TODO: `Unsupported: field declaration has unhandled type or kind` [SemanticsTodo]
  157. // CHECK:STDERR: int *is_nullable;
  158. // CHECK:STDERR: ^
  159. let a: i32 = m->is_volatile;
  160. // CHECK:STDERR: fail_use_unsupported_members.carbon:[[@LINE+8]]:17: note: in `Cpp` name lookup for `is_nullable` [InCppNameLookup]
  161. // CHECK:STDERR: let b: i32 = *m->is_nullable;
  162. // CHECK:STDERR: ^~~~~~~~~~~~~~
  163. // CHECK:STDERR:
  164. // CHECK:STDERR: fail_use_unsupported_members.carbon:[[@LINE-17]]:10: in file included here [InCppInclude]
  165. // CHECK:STDERR: ./unsupported_members.h:8:8: error: semantics TODO: `Unsupported: field declaration has unhandled type or kind` [SemanticsTodo]
  166. // CHECK:STDERR: int &is_reference;
  167. // CHECK:STDERR: ^
  168. let b: i32 = *m->is_nullable;
  169. // CHECK:STDERR: fail_use_unsupported_members.carbon:[[@LINE+4]]:16: note: in `Cpp` name lookup for `is_reference` [InCppNameLookup]
  170. // CHECK:STDERR: let c: i32 = m->is_reference;
  171. // CHECK:STDERR: ^~~~~~~~~~~~~~~
  172. // CHECK:STDERR:
  173. let c: i32 = m->is_reference;
  174. }
  175. // CHECK:STDOUT: --- use_struct_fields.carbon
  176. // CHECK:STDOUT:
  177. // CHECK:STDOUT: constants {
  178. // CHECK:STDOUT: %Struct: type = class_type @Struct [concrete]
  179. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  180. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  181. // CHECK:STDOUT: %tuple.type.189: type = tuple_type (%i32, %i32, %i32) [concrete]
  182. // CHECK:STDOUT: %Struct.elem.86b: type = unbound_element_type %Struct, %i32 [concrete]
  183. // CHECK:STDOUT: %ptr.235: type = ptr_type %i32 [concrete]
  184. // CHECK:STDOUT: %Struct.elem.765: type = unbound_element_type %Struct, %ptr.235 [concrete]
  185. // CHECK:STDOUT: }
  186. // CHECK:STDOUT:
  187. // CHECK:STDOUT: imports {
  188. // CHECK:STDOUT: }
  189. // CHECK:STDOUT:
  190. // CHECK:STDOUT: fn @F(%s.param: %Struct) -> %return.param: %tuple.type.189 {
  191. // CHECK:STDOUT: !entry:
  192. // CHECK:STDOUT: %s.ref.loc8_11: %Struct = name_ref s, %s
  193. // CHECK:STDOUT: %a.ref: %Struct.elem.86b = name_ref a, @Struct.%.1 [concrete = @Struct.%.1]
  194. // CHECK:STDOUT: %.loc8_12.1: ref %i32 = class_element_access %s.ref.loc8_11, element0
  195. // CHECK:STDOUT: %.loc8_12.2: %i32 = bind_value %.loc8_12.1
  196. // CHECK:STDOUT: %s.ref.loc8_16: %Struct = name_ref s, %s
  197. // CHECK:STDOUT: %b.ref: %Struct.elem.86b = name_ref b, @Struct.%.2 [concrete = @Struct.%.2]
  198. // CHECK:STDOUT: %.loc8_17.1: ref %i32 = class_element_access %s.ref.loc8_16, element1
  199. // CHECK:STDOUT: %.loc8_17.2: %i32 = bind_value %.loc8_17.1
  200. // CHECK:STDOUT: %s.ref.loc8_22: %Struct = name_ref s, %s
  201. // CHECK:STDOUT: %p.ref: %Struct.elem.765 = name_ref p, @Struct.%.3 [concrete = @Struct.%.3]
  202. // CHECK:STDOUT: %.loc8_23.1: ref %ptr.235 = class_element_access %s.ref.loc8_22, element2
  203. // CHECK:STDOUT: %.loc8_23.2: %ptr.235 = bind_value %.loc8_23.1
  204. // CHECK:STDOUT: %.loc8_21.1: ref %i32 = deref %.loc8_23.2
  205. // CHECK:STDOUT: %.loc8_25.1: %tuple.type.189 = tuple_literal (%.loc8_12.2, %.loc8_17.2, %.loc8_21.1)
  206. // CHECK:STDOUT: %tuple.elem0: ref %i32 = tuple_access %return, element0
  207. // CHECK:STDOUT: %.loc8_25.2: init %i32 = initialize_from %.loc8_12.2 to %tuple.elem0
  208. // CHECK:STDOUT: %tuple.elem1: ref %i32 = tuple_access %return, element1
  209. // CHECK:STDOUT: %.loc8_25.3: init %i32 = initialize_from %.loc8_17.2 to %tuple.elem1
  210. // CHECK:STDOUT: %.loc8_21.2: %i32 = bind_value %.loc8_21.1
  211. // CHECK:STDOUT: %tuple.elem2: ref %i32 = tuple_access %return, element2
  212. // CHECK:STDOUT: %.loc8_25.4: init %i32 = initialize_from %.loc8_21.2 to %tuple.elem2
  213. // CHECK:STDOUT: %.loc8_25.5: init %tuple.type.189 = tuple_init (%.loc8_25.2, %.loc8_25.3, %.loc8_25.4) to %return
  214. // CHECK:STDOUT: %.loc8_26: init %tuple.type.189 = converted %.loc8_25.1, %.loc8_25.5
  215. // CHECK:STDOUT: return %.loc8_26 to %return
  216. // CHECK:STDOUT: }
  217. // CHECK:STDOUT:
  218. // CHECK:STDOUT: --- use_union_fields.carbon
  219. // CHECK:STDOUT:
  220. // CHECK:STDOUT: constants {
  221. // CHECK:STDOUT: %Union: type = class_type @Union [concrete]
  222. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  223. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  224. // CHECK:STDOUT: %Union.elem.041: type = unbound_element_type %Union, %i32 [concrete]
  225. // CHECK:STDOUT: %ptr.235: type = ptr_type %i32 [concrete]
  226. // CHECK:STDOUT: %Union.elem.92a: type = unbound_element_type %Union, %ptr.235 [concrete]
  227. // CHECK:STDOUT: }
  228. // CHECK:STDOUT:
  229. // CHECK:STDOUT: imports {
  230. // CHECK:STDOUT: }
  231. // CHECK:STDOUT:
  232. // CHECK:STDOUT: fn @F(%u.param: %Union) -> %i32 {
  233. // CHECK:STDOUT: !entry:
  234. // CHECK:STDOUT: %u.ref: %Union = name_ref u, %u
  235. // CHECK:STDOUT: %a.ref: %Union.elem.041 = name_ref a, @Union.%.1 [concrete = @Union.%.1]
  236. // CHECK:STDOUT: %.loc8_11.1: ref %i32 = class_element_access %u.ref, element0
  237. // CHECK:STDOUT: %.loc8_11.2: %i32 = bind_value %.loc8_11.1
  238. // CHECK:STDOUT: return %.loc8_11.2
  239. // CHECK:STDOUT: }
  240. // CHECK:STDOUT:
  241. // CHECK:STDOUT: fn @G(%u.param: %Union) -> %i32 {
  242. // CHECK:STDOUT: !entry:
  243. // CHECK:STDOUT: %u.ref: %Union = name_ref u, %u
  244. // CHECK:STDOUT: %b.ref: %Union.elem.041 = name_ref b, @Union.%.2 [concrete = @Union.%.2]
  245. // CHECK:STDOUT: %.loc14_11.1: ref %i32 = class_element_access %u.ref, element1
  246. // CHECK:STDOUT: %.loc14_11.2: %i32 = bind_value %.loc14_11.1
  247. // CHECK:STDOUT: return %.loc14_11.2
  248. // CHECK:STDOUT: }
  249. // CHECK:STDOUT:
  250. // CHECK:STDOUT: fn @H(%u.param: %Union) -> %i32 {
  251. // CHECK:STDOUT: !entry:
  252. // CHECK:STDOUT: %u.ref: %Union = name_ref u, %u
  253. // CHECK:STDOUT: %p.ref: %Union.elem.92a = name_ref p, @Union.%.3 [concrete = @Union.%.3]
  254. // CHECK:STDOUT: %.loc20_12.1: ref %ptr.235 = class_element_access %u.ref, element2
  255. // CHECK:STDOUT: %.loc20_12.2: %ptr.235 = bind_value %.loc20_12.1
  256. // CHECK:STDOUT: %.loc20_10.1: ref %i32 = deref %.loc20_12.2
  257. // CHECK:STDOUT: %.loc20_10.2: %i32 = bind_value %.loc20_10.1
  258. // CHECK:STDOUT: return %.loc20_10.2
  259. // CHECK:STDOUT: }
  260. // CHECK:STDOUT:
  261. // CHECK:STDOUT: --- use_non_bitfields_in_type_with_bitfields.carbon
  262. // CHECK:STDOUT:
  263. // CHECK:STDOUT: constants {
  264. // CHECK:STDOUT: %Struct: type = class_type @Struct [concrete]
  265. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  266. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  267. // CHECK:STDOUT: %Struct.elem: type = unbound_element_type %Struct, %i32 [concrete]
  268. // CHECK:STDOUT: %Union: type = class_type @Union [concrete]
  269. // CHECK:STDOUT: %Union.elem: type = unbound_element_type %Union, %i32 [concrete]
  270. // CHECK:STDOUT: }
  271. // CHECK:STDOUT:
  272. // CHECK:STDOUT: imports {
  273. // CHECK:STDOUT: }
  274. // CHECK:STDOUT:
  275. // CHECK:STDOUT: fn @F(%s.param: %Struct) -> %i32 {
  276. // CHECK:STDOUT: !entry:
  277. // CHECK:STDOUT: %s.ref: %Struct = name_ref s, %s
  278. // CHECK:STDOUT: %a.ref: %Struct.elem = name_ref a, @Struct.%.1 [concrete = @Struct.%.1]
  279. // CHECK:STDOUT: %.loc8_11.1: ref %i32 = class_element_access %s.ref, element0
  280. // CHECK:STDOUT: %.loc8_11.2: %i32 = bind_value %.loc8_11.1
  281. // CHECK:STDOUT: return %.loc8_11.2
  282. // CHECK:STDOUT: }
  283. // CHECK:STDOUT:
  284. // CHECK:STDOUT: fn @G(%s.param: %Union) -> %i32 {
  285. // CHECK:STDOUT: !entry:
  286. // CHECK:STDOUT: %s.ref: %Union = name_ref s, %s
  287. // CHECK:STDOUT: %c.ref: %Union.elem = name_ref c, @Union.%.1 [concrete = @Union.%.1]
  288. // CHECK:STDOUT: %.loc14_11.1: ref %i32 = class_element_access %s.ref, element0
  289. // CHECK:STDOUT: %.loc14_11.2: %i32 = bind_value %.loc14_11.1
  290. // CHECK:STDOUT: return %.loc14_11.2
  291. // CHECK:STDOUT: }
  292. // CHECK:STDOUT:
  293. // CHECK:STDOUT: --- fail_todo_use_bitfields.carbon
  294. // CHECK:STDOUT:
  295. // CHECK:STDOUT: constants {
  296. // CHECK:STDOUT: %Struct: type = class_type @Struct [concrete]
  297. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  298. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  299. // CHECK:STDOUT: %Union: type = class_type @Union [concrete]
  300. // CHECK:STDOUT: }
  301. // CHECK:STDOUT:
  302. // CHECK:STDOUT: imports {
  303. // CHECK:STDOUT: }
  304. // CHECK:STDOUT:
  305. // CHECK:STDOUT: fn @F(%s.param: %Struct) -> %i32 {
  306. // CHECK:STDOUT: !entry:
  307. // CHECK:STDOUT: %s.ref: %Struct = name_ref s, %s
  308. // CHECK:STDOUT: %b.ref: <error> = name_ref b, <error> [concrete = <error>]
  309. // CHECK:STDOUT: return <error>
  310. // CHECK:STDOUT: }
  311. // CHECK:STDOUT:
  312. // CHECK:STDOUT: fn @G(%s.param: %Union) -> %i32 {
  313. // CHECK:STDOUT: !entry:
  314. // CHECK:STDOUT: %s.ref: %Union = name_ref s, %s
  315. // CHECK:STDOUT: %d.ref: <error> = name_ref d, <error> [concrete = <error>]
  316. // CHECK:STDOUT: return <error>
  317. // CHECK:STDOUT: }
  318. // CHECK:STDOUT: