field.carbon 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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/int.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/lower/testdata/class/field.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/class/field.carbon
  12. // --- basic.carbon
  13. library "[[@TEST_NAME]]";
  14. class C {
  15. var a: i32;
  16. var b: C*;
  17. }
  18. fn F(c: C) -> i32 {
  19. return (*c.b).a;
  20. }
  21. fn Run() -> i32 {
  22. var c: C;
  23. c.a = 1;
  24. c.b = &c;
  25. return F(c);
  26. }
  27. // --- inplace_init_with_nonconst.carbon
  28. library "[[@TEST_NAME]]";
  29. class Other { }
  30. class Use {
  31. var a: Other*;
  32. var b: Other;
  33. }
  34. fn Run() {
  35. var o: Other;
  36. // .a is initialized with a non-constant expression
  37. // .b is initialized with in-place class_init
  38. var u: Use = {.a = &o, .b = {}};
  39. }
  40. // --- implicit_init_with_nonempty_nonconst.carbon
  41. library "[[@TEST_NAME]]";
  42. class Other {
  43. var v: i32;
  44. }
  45. class Use {
  46. var a: Other*;
  47. var b: Other;
  48. }
  49. fn Run() {
  50. var o: Other;
  51. // .a is initialized with a non-constant expression
  52. // .b is initialized with in-place class_init
  53. var u: Use = {.a = &o, .b = {.v = 42}};
  54. }
  55. // CHECK:STDOUT: ; ModuleID = 'basic.carbon'
  56. // CHECK:STDOUT: source_filename = "basic.carbon"
  57. // CHECK:STDOUT:
  58. // CHECK:STDOUT: ; Function Attrs: nounwind
  59. // CHECK:STDOUT: define i32 @_CF.Main(ptr %c) #0 !dbg !4 {
  60. // CHECK:STDOUT: entry:
  61. // CHECK:STDOUT: %.loc10_13.1.b = getelementptr inbounds nuw { i32, ptr }, ptr %c, i32 0, i32 1, !dbg !7
  62. // CHECK:STDOUT: %.loc10_13.2 = load ptr, ptr %.loc10_13.1.b, align 8, !dbg !7
  63. // CHECK:STDOUT: %.loc10_16.1.a = getelementptr inbounds nuw { i32, ptr }, ptr %.loc10_13.2, i32 0, i32 0, !dbg !8
  64. // CHECK:STDOUT: %.loc10_16.2 = load i32, ptr %.loc10_16.1.a, align 4, !dbg !8
  65. // CHECK:STDOUT: ret i32 %.loc10_16.2, !dbg !9
  66. // CHECK:STDOUT: }
  67. // CHECK:STDOUT:
  68. // CHECK:STDOUT: ; Function Attrs: nounwind
  69. // CHECK:STDOUT: define i32 @main() #0 !dbg !10 {
  70. // CHECK:STDOUT: entry:
  71. // CHECK:STDOUT: %c.var = alloca { i32, ptr }, align 8, !dbg !11
  72. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %c.var), !dbg !11
  73. // CHECK:STDOUT: %.loc15_4.a = getelementptr inbounds nuw { i32, ptr }, ptr %c.var, i32 0, i32 0, !dbg !12
  74. // CHECK:STDOUT: store i32 1, ptr %.loc15_4.a, align 4, !dbg !12
  75. // CHECK:STDOUT: %.loc16.b = getelementptr inbounds nuw { i32, ptr }, ptr %c.var, i32 0, i32 1, !dbg !13
  76. // CHECK:STDOUT: store ptr %c.var, ptr %.loc16.b, align 8, !dbg !13
  77. // CHECK:STDOUT: %F.call = call i32 @_CF.Main(ptr %c.var), !dbg !14
  78. // CHECK:STDOUT: ret i32 %F.call, !dbg !15
  79. // CHECK:STDOUT: }
  80. // CHECK:STDOUT:
  81. // CHECK:STDOUT: ; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
  82. // CHECK:STDOUT: declare void @llvm.lifetime.start.p0(ptr captures(none)) #1
  83. // CHECK:STDOUT:
  84. // CHECK:STDOUT: attributes #0 = { nounwind }
  85. // CHECK:STDOUT: attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
  86. // CHECK:STDOUT:
  87. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1}
  88. // CHECK:STDOUT: !llvm.dbg.cu = !{!2}
  89. // CHECK:STDOUT:
  90. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  91. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  92. // CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  93. // CHECK:STDOUT: !3 = !DIFile(filename: "basic.carbon", directory: "")
  94. // CHECK:STDOUT: !4 = distinct !DISubprogram(name: "F", linkageName: "_CF.Main", scope: null, file: !3, line: 9, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  95. // CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
  96. // CHECK:STDOUT: !6 = !{}
  97. // CHECK:STDOUT: !7 = !DILocation(line: 10, column: 12, scope: !4)
  98. // CHECK:STDOUT: !8 = !DILocation(line: 10, column: 10, scope: !4)
  99. // CHECK:STDOUT: !9 = !DILocation(line: 10, column: 3, scope: !4)
  100. // CHECK:STDOUT: !10 = distinct !DISubprogram(name: "Run", linkageName: "main", scope: null, file: !3, line: 13, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  101. // CHECK:STDOUT: !11 = !DILocation(line: 14, column: 3, scope: !10)
  102. // CHECK:STDOUT: !12 = !DILocation(line: 15, column: 3, scope: !10)
  103. // CHECK:STDOUT: !13 = !DILocation(line: 16, column: 3, scope: !10)
  104. // CHECK:STDOUT: !14 = !DILocation(line: 17, column: 10, scope: !10)
  105. // CHECK:STDOUT: !15 = !DILocation(line: 17, column: 3, scope: !10)
  106. // CHECK:STDOUT: ; ModuleID = 'inplace_init_with_nonconst.carbon'
  107. // CHECK:STDOUT: source_filename = "inplace_init_with_nonconst.carbon"
  108. // CHECK:STDOUT:
  109. // CHECK:STDOUT: @Other.val.loc15_33.5 = internal constant {} zeroinitializer
  110. // CHECK:STDOUT:
  111. // CHECK:STDOUT: ; Function Attrs: nounwind
  112. // CHECK:STDOUT: define void @main() #0 !dbg !4 {
  113. // CHECK:STDOUT: entry:
  114. // CHECK:STDOUT: %o.var = alloca {}, align 8, !dbg !7
  115. // CHECK:STDOUT: %u.var = alloca { ptr, {} }, align 8, !dbg !8
  116. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %o.var), !dbg !7
  117. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %u.var), !dbg !8
  118. // CHECK:STDOUT: %.loc15_33.2.a = getelementptr inbounds nuw { ptr, {} }, ptr %u.var, i32 0, i32 0, !dbg !9
  119. // CHECK:STDOUT: store ptr %o.var, ptr %.loc15_33.2.a, align 8, !dbg !9
  120. // CHECK:STDOUT: %.loc15_33.4.b = getelementptr inbounds nuw { ptr, {} }, ptr %u.var, i32 0, i32 1, !dbg !9
  121. // CHECK:STDOUT: call void @llvm.memcpy.p0.p0.i64(ptr align 1 %.loc15_33.4.b, ptr align 1 @Other.val.loc15_33.5, i64 0, i1 false), !dbg !9
  122. // CHECK:STDOUT: ret void, !dbg !10
  123. // CHECK:STDOUT: }
  124. // CHECK:STDOUT:
  125. // CHECK:STDOUT: ; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
  126. // CHECK:STDOUT: declare void @llvm.lifetime.start.p0(ptr captures(none)) #1
  127. // CHECK:STDOUT:
  128. // CHECK:STDOUT: ; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
  129. // CHECK:STDOUT: declare void @llvm.memcpy.p0.p0.i64(ptr noalias writeonly captures(none), ptr noalias readonly captures(none), i64, i1 immarg) #2
  130. // CHECK:STDOUT:
  131. // CHECK:STDOUT: ; uselistorder directives
  132. // CHECK:STDOUT: uselistorder ptr @llvm.lifetime.start.p0, { 1, 0 }
  133. // CHECK:STDOUT:
  134. // CHECK:STDOUT: attributes #0 = { nounwind }
  135. // CHECK:STDOUT: attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
  136. // CHECK:STDOUT: attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
  137. // CHECK:STDOUT:
  138. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1}
  139. // CHECK:STDOUT: !llvm.dbg.cu = !{!2}
  140. // CHECK:STDOUT:
  141. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  142. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  143. // CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  144. // CHECK:STDOUT: !3 = !DIFile(filename: "inplace_init_with_nonconst.carbon", directory: "")
  145. // CHECK:STDOUT: !4 = distinct !DISubprogram(name: "Run", linkageName: "main", scope: null, file: !3, line: 11, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  146. // CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
  147. // CHECK:STDOUT: !6 = !{}
  148. // CHECK:STDOUT: !7 = !DILocation(line: 12, column: 3, scope: !4)
  149. // CHECK:STDOUT: !8 = !DILocation(line: 15, column: 3, scope: !4)
  150. // CHECK:STDOUT: !9 = !DILocation(line: 15, column: 16, scope: !4)
  151. // CHECK:STDOUT: !10 = !DILocation(line: 11, column: 1, scope: !4)
  152. // CHECK:STDOUT: ; ModuleID = 'implicit_init_with_nonempty_nonconst.carbon'
  153. // CHECK:STDOUT: source_filename = "implicit_init_with_nonempty_nonconst.carbon"
  154. // CHECK:STDOUT:
  155. // CHECK:STDOUT: @Other.val.loc17_40.5 = internal constant { i32 } { i32 42 }
  156. // CHECK:STDOUT:
  157. // CHECK:STDOUT: ; Function Attrs: nounwind
  158. // CHECK:STDOUT: define void @main() #0 !dbg !4 {
  159. // CHECK:STDOUT: entry:
  160. // CHECK:STDOUT: %o.var = alloca { i32 }, align 8, !dbg !7
  161. // CHECK:STDOUT: %u.var = alloca { ptr, { i32 } }, align 8, !dbg !8
  162. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %o.var), !dbg !7
  163. // CHECK:STDOUT: call void @llvm.lifetime.start.p0(ptr %u.var), !dbg !8
  164. // CHECK:STDOUT: %.loc17_40.2.a = getelementptr inbounds nuw { ptr, { i32 } }, ptr %u.var, i32 0, i32 0, !dbg !9
  165. // CHECK:STDOUT: store ptr %o.var, ptr %.loc17_40.2.a, align 8, !dbg !9
  166. // CHECK:STDOUT: %.loc17_40.4.b = getelementptr inbounds nuw { ptr, { i32 } }, ptr %u.var, i32 0, i32 1, !dbg !9
  167. // CHECK:STDOUT: %.loc17_39.3.v = getelementptr inbounds nuw { i32 }, ptr %.loc17_40.4.b, i32 0, i32 0, !dbg !10
  168. // CHECK:STDOUT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 %.loc17_40.4.b, ptr align 4 @Other.val.loc17_40.5, i64 4, i1 false), !dbg !9
  169. // CHECK:STDOUT: ret void, !dbg !11
  170. // CHECK:STDOUT: }
  171. // CHECK:STDOUT:
  172. // CHECK:STDOUT: ; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
  173. // CHECK:STDOUT: declare void @llvm.lifetime.start.p0(ptr captures(none)) #1
  174. // CHECK:STDOUT:
  175. // CHECK:STDOUT: ; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
  176. // CHECK:STDOUT: declare void @llvm.memcpy.p0.p0.i64(ptr noalias writeonly captures(none), ptr noalias readonly captures(none), i64, i1 immarg) #2
  177. // CHECK:STDOUT:
  178. // CHECK:STDOUT: ; uselistorder directives
  179. // CHECK:STDOUT: uselistorder ptr @llvm.lifetime.start.p0, { 1, 0 }
  180. // CHECK:STDOUT:
  181. // CHECK:STDOUT: attributes #0 = { nounwind }
  182. // CHECK:STDOUT: attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
  183. // CHECK:STDOUT: attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
  184. // CHECK:STDOUT:
  185. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1}
  186. // CHECK:STDOUT: !llvm.dbg.cu = !{!2}
  187. // CHECK:STDOUT:
  188. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  189. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  190. // CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  191. // CHECK:STDOUT: !3 = !DIFile(filename: "implicit_init_with_nonempty_nonconst.carbon", directory: "")
  192. // CHECK:STDOUT: !4 = distinct !DISubprogram(name: "Run", linkageName: "main", scope: null, file: !3, line: 13, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  193. // CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
  194. // CHECK:STDOUT: !6 = !{}
  195. // CHECK:STDOUT: !7 = !DILocation(line: 14, column: 3, scope: !4)
  196. // CHECK:STDOUT: !8 = !DILocation(line: 17, column: 3, scope: !4)
  197. // CHECK:STDOUT: !9 = !DILocation(line: 17, column: 16, scope: !4)
  198. // CHECK:STDOUT: !10 = !DILocation(line: 17, column: 31, scope: !4)
  199. // CHECK:STDOUT: !11 = !DILocation(line: 13, column: 1, scope: !4)