field_access_in_value.carbon 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. // AUTOUPDATE
  6. // TIP: To test this file alone, run:
  7. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/class/field_access_in_value.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/field_access_in_value.carbon
  10. class Class {
  11. var j: i32;
  12. var k: i32;
  13. }
  14. fn Test() {
  15. var cv: Class;
  16. cv.j = 1;
  17. cv.k = 2;
  18. let c: Class = cv;
  19. var cj: i32 = c.j;
  20. var ck: i32 = c.k;
  21. }
  22. // CHECK:STDOUT: --- field_access_in_value.carbon
  23. // CHECK:STDOUT:
  24. // CHECK:STDOUT: constants {
  25. // CHECK:STDOUT: %Class: type = class_type @Class [template]
  26. // CHECK:STDOUT: %.1: Core.IntLiteral = int_value 32 [template]
  27. // CHECK:STDOUT: %Int.type: type = fn_type @Int [template]
  28. // CHECK:STDOUT: %Int: %Int.type = struct_value () [template]
  29. // CHECK:STDOUT: %i32: type = int_type signed, %.1 [template]
  30. // CHECK:STDOUT: %.2: type = unbound_element_type %Class, %i32 [template]
  31. // CHECK:STDOUT: %.3: type = struct_type {.j: %i32, .k: %i32} [template]
  32. // CHECK:STDOUT: %.4: <witness> = complete_type_witness %.3 [template]
  33. // CHECK:STDOUT: %Test.type: type = fn_type @Test [template]
  34. // CHECK:STDOUT: %Test: %Test.type = struct_value () [template]
  35. // CHECK:STDOUT: %.6: Core.IntLiteral = int_value 1 [template]
  36. // CHECK:STDOUT: %Convert.type.2: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  37. // CHECK:STDOUT: %Convert.type.14: type = fn_type @Convert.2, @impl.1(%.1) [template]
  38. // CHECK:STDOUT: %Convert.14: %Convert.type.14 = struct_value () [template]
  39. // CHECK:STDOUT: %.26: <witness> = interface_witness (%Convert.14) [template]
  40. // CHECK:STDOUT: %.27: <bound method> = bound_method %.6, %Convert.14 [template]
  41. // CHECK:STDOUT: %.28: <specific function> = specific_function %.27, @Convert.2(%.1) [template]
  42. // CHECK:STDOUT: %.29: %i32 = int_value 1 [template]
  43. // CHECK:STDOUT: %.30: Core.IntLiteral = int_value 2 [template]
  44. // CHECK:STDOUT: %.31: <bound method> = bound_method %.30, %Convert.14 [template]
  45. // CHECK:STDOUT: %.32: <specific function> = specific_function %.31, @Convert.2(%.1) [template]
  46. // CHECK:STDOUT: %.33: %i32 = int_value 2 [template]
  47. // CHECK:STDOUT: }
  48. // CHECK:STDOUT:
  49. // CHECK:STDOUT: imports {
  50. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  51. // CHECK:STDOUT: .Int = %import_ref.1
  52. // CHECK:STDOUT: .ImplicitAs = %import_ref.2
  53. // CHECK:STDOUT: import Core//prelude
  54. // CHECK:STDOUT: import Core//prelude/...
  55. // CHECK:STDOUT: }
  56. // CHECK:STDOUT: }
  57. // CHECK:STDOUT:
  58. // CHECK:STDOUT: file {
  59. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  60. // CHECK:STDOUT: .Core = imports.%Core
  61. // CHECK:STDOUT: .Class = %Class.decl
  62. // CHECK:STDOUT: .Test = %Test.decl
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT: %Core.import = import Core
  65. // CHECK:STDOUT: %Class.decl: type = class_decl @Class [template = constants.%Class] {} {}
  66. // CHECK:STDOUT: %Test.decl: %Test.type = fn_decl @Test [template = constants.%Test] {} {}
  67. // CHECK:STDOUT: }
  68. // CHECK:STDOUT:
  69. // CHECK:STDOUT: class @Class {
  70. // CHECK:STDOUT: %.loc12_10.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
  71. // CHECK:STDOUT: %int.make_type_signed.loc12: init type = call constants.%Int(%.loc12_10.1) [template = constants.%i32]
  72. // CHECK:STDOUT: %.loc12_10.2: type = value_of_initializer %int.make_type_signed.loc12 [template = constants.%i32]
  73. // CHECK:STDOUT: %.loc12_10.3: type = converted %int.make_type_signed.loc12, %.loc12_10.2 [template = constants.%i32]
  74. // CHECK:STDOUT: %.loc12_8: %.2 = field_decl j, element0 [template]
  75. // CHECK:STDOUT: %.loc13_10.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
  76. // CHECK:STDOUT: %int.make_type_signed.loc13: init type = call constants.%Int(%.loc13_10.1) [template = constants.%i32]
  77. // CHECK:STDOUT: %.loc13_10.2: type = value_of_initializer %int.make_type_signed.loc13 [template = constants.%i32]
  78. // CHECK:STDOUT: %.loc13_10.3: type = converted %int.make_type_signed.loc13, %.loc13_10.2 [template = constants.%i32]
  79. // CHECK:STDOUT: %.loc13_8: %.2 = field_decl k, element1 [template]
  80. // CHECK:STDOUT: %.loc14: <witness> = complete_type_witness %.3 [template = constants.%.4]
  81. // CHECK:STDOUT:
  82. // CHECK:STDOUT: !members:
  83. // CHECK:STDOUT: .Self = constants.%Class
  84. // CHECK:STDOUT: .j = %.loc12_8
  85. // CHECK:STDOUT: .k = %.loc13_8
  86. // CHECK:STDOUT: }
  87. // CHECK:STDOUT:
  88. // CHECK:STDOUT: fn @Test() {
  89. // CHECK:STDOUT: !entry:
  90. // CHECK:STDOUT: %Class.ref.loc17: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  91. // CHECK:STDOUT: %cv.var: ref %Class = var cv
  92. // CHECK:STDOUT: %cv: ref %Class = bind_name cv, %cv.var
  93. // CHECK:STDOUT: %cv.ref.loc18: ref %Class = name_ref cv, %cv
  94. // CHECK:STDOUT: %j.ref.loc18: %.2 = name_ref j, @Class.%.loc12_8 [template = @Class.%.loc12_8]
  95. // CHECK:STDOUT: %.loc18_5: ref %i32 = class_element_access %cv.ref.loc18, element0
  96. // CHECK:STDOUT: %.loc18_10: Core.IntLiteral = int_value 1 [template = constants.%.6]
  97. // CHECK:STDOUT: %.loc18_8.1: %Convert.type.2 = interface_witness_access constants.%.26, element0 [template = constants.%Convert.14]
  98. // CHECK:STDOUT: %.loc18_8.2: <bound method> = bound_method %.loc18_10, %.loc18_8.1 [template = constants.%.27]
  99. // CHECK:STDOUT: %.loc18_8.3: <specific function> = specific_function %.loc18_8.2, @Convert.2(constants.%.1) [template = constants.%.28]
  100. // CHECK:STDOUT: %int.convert_checked.loc18: init %i32 = call %.loc18_8.3(%.loc18_10) [template = constants.%.29]
  101. // CHECK:STDOUT: %.loc18_8.4: init %i32 = converted %.loc18_10, %int.convert_checked.loc18 [template = constants.%.29]
  102. // CHECK:STDOUT: assign %.loc18_5, %.loc18_8.4
  103. // CHECK:STDOUT: %cv.ref.loc19: ref %Class = name_ref cv, %cv
  104. // CHECK:STDOUT: %k.ref.loc19: %.2 = name_ref k, @Class.%.loc13_8 [template = @Class.%.loc13_8]
  105. // CHECK:STDOUT: %.loc19_5: ref %i32 = class_element_access %cv.ref.loc19, element1
  106. // CHECK:STDOUT: %.loc19_10: Core.IntLiteral = int_value 2 [template = constants.%.30]
  107. // CHECK:STDOUT: %.loc19_8.1: %Convert.type.2 = interface_witness_access constants.%.26, element0 [template = constants.%Convert.14]
  108. // CHECK:STDOUT: %.loc19_8.2: <bound method> = bound_method %.loc19_10, %.loc19_8.1 [template = constants.%.31]
  109. // CHECK:STDOUT: %.loc19_8.3: <specific function> = specific_function %.loc19_8.2, @Convert.2(constants.%.1) [template = constants.%.32]
  110. // CHECK:STDOUT: %int.convert_checked.loc19: init %i32 = call %.loc19_8.3(%.loc19_10) [template = constants.%.33]
  111. // CHECK:STDOUT: %.loc19_8.4: init %i32 = converted %.loc19_10, %int.convert_checked.loc19 [template = constants.%.33]
  112. // CHECK:STDOUT: assign %.loc19_5, %.loc19_8.4
  113. // CHECK:STDOUT: %Class.ref.loc20: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  114. // CHECK:STDOUT: %cv.ref.loc20: ref %Class = name_ref cv, %cv
  115. // CHECK:STDOUT: %.loc20: %Class = bind_value %cv.ref.loc20
  116. // CHECK:STDOUT: %c: %Class = bind_name c, %.loc20
  117. // CHECK:STDOUT: %.loc21_11.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
  118. // CHECK:STDOUT: %int.make_type_signed.loc21: init type = call constants.%Int(%.loc21_11.1) [template = constants.%i32]
  119. // CHECK:STDOUT: %.loc21_11.2: type = value_of_initializer %int.make_type_signed.loc21 [template = constants.%i32]
  120. // CHECK:STDOUT: %.loc21_11.3: type = converted %int.make_type_signed.loc21, %.loc21_11.2 [template = constants.%i32]
  121. // CHECK:STDOUT: %cj.var: ref %i32 = var cj
  122. // CHECK:STDOUT: %cj: ref %i32 = bind_name cj, %cj.var
  123. // CHECK:STDOUT: %c.ref.loc21: %Class = name_ref c, %c
  124. // CHECK:STDOUT: %j.ref.loc21: %.2 = name_ref j, @Class.%.loc12_8 [template = @Class.%.loc12_8]
  125. // CHECK:STDOUT: %.loc21_18.1: ref %i32 = class_element_access %c.ref.loc21, element0
  126. // CHECK:STDOUT: %.loc21_18.2: %i32 = bind_value %.loc21_18.1
  127. // CHECK:STDOUT: assign %cj.var, %.loc21_18.2
  128. // CHECK:STDOUT: %.loc22_11.1: Core.IntLiteral = int_value 32 [template = constants.%.1]
  129. // CHECK:STDOUT: %int.make_type_signed.loc22: init type = call constants.%Int(%.loc22_11.1) [template = constants.%i32]
  130. // CHECK:STDOUT: %.loc22_11.2: type = value_of_initializer %int.make_type_signed.loc22 [template = constants.%i32]
  131. // CHECK:STDOUT: %.loc22_11.3: type = converted %int.make_type_signed.loc22, %.loc22_11.2 [template = constants.%i32]
  132. // CHECK:STDOUT: %ck.var: ref %i32 = var ck
  133. // CHECK:STDOUT: %ck: ref %i32 = bind_name ck, %ck.var
  134. // CHECK:STDOUT: %c.ref.loc22: %Class = name_ref c, %c
  135. // CHECK:STDOUT: %k.ref.loc22: %.2 = name_ref k, @Class.%.loc13_8 [template = @Class.%.loc13_8]
  136. // CHECK:STDOUT: %.loc22_18.1: ref %i32 = class_element_access %c.ref.loc22, element1
  137. // CHECK:STDOUT: %.loc22_18.2: %i32 = bind_value %.loc22_18.1
  138. // CHECK:STDOUT: assign %ck.var, %.loc22_18.2
  139. // CHECK:STDOUT: return
  140. // CHECK:STDOUT: }
  141. // CHECK:STDOUT: