field_access.carbon 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.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.carbon
  10. class Class {
  11. var j: i32;
  12. var k: i32;
  13. }
  14. fn Run() {
  15. var c: Class;
  16. c.j = 1;
  17. c.k = 2;
  18. var cj: i32 = c.j;
  19. var ck: i32 = c.k;
  20. }
  21. // CHECK:STDOUT: --- field_access.carbon
  22. // CHECK:STDOUT:
  23. // CHECK:STDOUT: constants {
  24. // CHECK:STDOUT: %Class: type = class_type @Class [template]
  25. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  26. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  27. // CHECK:STDOUT: %Class.elem: type = unbound_element_type %Class, %i32 [template]
  28. // CHECK:STDOUT: %struct_type.j.k: type = struct_type {.j: %i32, .k: %i32} [template]
  29. // CHECK:STDOUT: %complete_type.cf7: <witness> = complete_type_witness %struct_type.j.k [template]
  30. // CHECK:STDOUT: %Run.type: type = fn_type @Run [template]
  31. // CHECK:STDOUT: %Run: %Run.type = struct_value () [template]
  32. // CHECK:STDOUT: %int_1.5b8: Core.IntLiteral = int_value 1 [template]
  33. // CHECK:STDOUT: %ImplicitAs.type.205: type = facet_type <@ImplicitAs, @ImplicitAs(%i32)> [template]
  34. // CHECK:STDOUT: %Convert.type.1b6: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  35. // CHECK:STDOUT: %impl_witness.d39: <witness> = impl_witness (imports.%Core.import_ref.a5b), @impl.1(%int_32) [template]
  36. // CHECK:STDOUT: %Convert.type.035: type = fn_type @Convert.2, @impl.1(%int_32) [template]
  37. // CHECK:STDOUT: %Convert.956: %Convert.type.035 = struct_value () [template]
  38. // CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.205 = facet_value Core.IntLiteral, %impl_witness.d39 [template]
  39. // CHECK:STDOUT: %.a0b: type = fn_type_with_self_type %Convert.type.1b6, %ImplicitAs.facet [template]
  40. // CHECK:STDOUT: %Convert.bound.ab5: <bound method> = bound_method %int_1.5b8, %Convert.956 [template]
  41. // CHECK:STDOUT: %Convert.specific_fn.70c: <specific function> = specific_function %Convert.bound.ab5, @Convert.2(%int_32) [template]
  42. // CHECK:STDOUT: %int_1.5d2: %i32 = int_value 1 [template]
  43. // CHECK:STDOUT: %int_2.ecc: Core.IntLiteral = int_value 2 [template]
  44. // CHECK:STDOUT: %Convert.bound.ef9: <bound method> = bound_method %int_2.ecc, %Convert.956 [template]
  45. // CHECK:STDOUT: %Convert.specific_fn.787: <specific function> = specific_function %Convert.bound.ef9, @Convert.2(%int_32) [template]
  46. // CHECK:STDOUT: %int_2.ef8: %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 = %Core.Int
  52. // CHECK:STDOUT: .ImplicitAs = %Core.ImplicitAs
  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: .Run = %Run.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: %Run.decl: %Run.type = fn_decl @Run [template = constants.%Run] {} {}
  67. // CHECK:STDOUT: }
  68. // CHECK:STDOUT:
  69. // CHECK:STDOUT: class @Class {
  70. // CHECK:STDOUT: %.loc12_8: %Class.elem = field_decl j, element0 [template]
  71. // CHECK:STDOUT: name_binding_decl {
  72. // CHECK:STDOUT: %.loc12_3: %Class.elem = var_pattern %.loc12_8
  73. // CHECK:STDOUT: }
  74. // CHECK:STDOUT: %.var.loc12: ref %Class.elem = var <none>
  75. // CHECK:STDOUT: %.loc13_8: %Class.elem = field_decl k, element1 [template]
  76. // CHECK:STDOUT: name_binding_decl {
  77. // CHECK:STDOUT: %.loc13_3: %Class.elem = var_pattern %.loc13_8
  78. // CHECK:STDOUT: }
  79. // CHECK:STDOUT: %.var.loc13: ref %Class.elem = var <none>
  80. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %struct_type.j.k [template = constants.%complete_type.cf7]
  81. // CHECK:STDOUT: complete_type_witness = %complete_type
  82. // CHECK:STDOUT:
  83. // CHECK:STDOUT: !members:
  84. // CHECK:STDOUT: .Self = constants.%Class
  85. // CHECK:STDOUT: .j = %.loc12_8
  86. // CHECK:STDOUT: .k = %.loc13_8
  87. // CHECK:STDOUT: }
  88. // CHECK:STDOUT:
  89. // CHECK:STDOUT: fn @Run() {
  90. // CHECK:STDOUT: !entry:
  91. // CHECK:STDOUT: name_binding_decl {
  92. // CHECK:STDOUT: %c.patt: %Class = binding_pattern c
  93. // CHECK:STDOUT: %.loc17: %Class = var_pattern %c.patt
  94. // CHECK:STDOUT: }
  95. // CHECK:STDOUT: %c.var: ref %Class = var c
  96. // CHECK:STDOUT: %Class.ref: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  97. // CHECK:STDOUT: %c: ref %Class = bind_name c, %c.var
  98. // CHECK:STDOUT: %c.ref.loc18: ref %Class = name_ref c, %c
  99. // CHECK:STDOUT: %j.ref.loc18: %Class.elem = name_ref j, @Class.%.loc12_8 [template = @Class.%.loc12_8]
  100. // CHECK:STDOUT: %.loc18_4: ref %i32 = class_element_access %c.ref.loc18, element0
  101. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [template = constants.%int_1.5b8]
  102. // CHECK:STDOUT: %impl.elem0.loc18: %.a0b = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
  103. // CHECK:STDOUT: %bound_method.loc18: <bound method> = bound_method %int_1, %impl.elem0.loc18 [template = constants.%Convert.bound.ab5]
  104. // CHECK:STDOUT: %specific_fn.loc18: <specific function> = specific_function %bound_method.loc18, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.70c]
  105. // CHECK:STDOUT: %int.convert_checked.loc18: init %i32 = call %specific_fn.loc18(%int_1) [template = constants.%int_1.5d2]
  106. // CHECK:STDOUT: %.loc18_7: init %i32 = converted %int_1, %int.convert_checked.loc18 [template = constants.%int_1.5d2]
  107. // CHECK:STDOUT: assign %.loc18_4, %.loc18_7
  108. // CHECK:STDOUT: %c.ref.loc19: ref %Class = name_ref c, %c
  109. // CHECK:STDOUT: %k.ref.loc19: %Class.elem = name_ref k, @Class.%.loc13_8 [template = @Class.%.loc13_8]
  110. // CHECK:STDOUT: %.loc19_4: ref %i32 = class_element_access %c.ref.loc19, element1
  111. // CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [template = constants.%int_2.ecc]
  112. // CHECK:STDOUT: %impl.elem0.loc19: %.a0b = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
  113. // CHECK:STDOUT: %bound_method.loc19: <bound method> = bound_method %int_2, %impl.elem0.loc19 [template = constants.%Convert.bound.ef9]
  114. // CHECK:STDOUT: %specific_fn.loc19: <specific function> = specific_function %bound_method.loc19, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.787]
  115. // CHECK:STDOUT: %int.convert_checked.loc19: init %i32 = call %specific_fn.loc19(%int_2) [template = constants.%int_2.ef8]
  116. // CHECK:STDOUT: %.loc19_7: init %i32 = converted %int_2, %int.convert_checked.loc19 [template = constants.%int_2.ef8]
  117. // CHECK:STDOUT: assign %.loc19_4, %.loc19_7
  118. // CHECK:STDOUT: name_binding_decl {
  119. // CHECK:STDOUT: %cj.patt: %i32 = binding_pattern cj
  120. // CHECK:STDOUT: %.loc20_3: %i32 = var_pattern %cj.patt
  121. // CHECK:STDOUT: }
  122. // CHECK:STDOUT: %cj.var: ref %i32 = var cj
  123. // CHECK:STDOUT: %c.ref.loc20: ref %Class = name_ref c, %c
  124. // CHECK:STDOUT: %j.ref.loc20: %Class.elem = name_ref j, @Class.%.loc12_8 [template = @Class.%.loc12_8]
  125. // CHECK:STDOUT: %.loc20_18.1: ref %i32 = class_element_access %c.ref.loc20, element0
  126. // CHECK:STDOUT: %.loc20_18.2: %i32 = bind_value %.loc20_18.1
  127. // CHECK:STDOUT: assign %cj.var, %.loc20_18.2
  128. // CHECK:STDOUT: %.loc20_11: type = splice_block %i32.loc20 [template = constants.%i32] {
  129. // CHECK:STDOUT: %int_32.loc20: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  130. // CHECK:STDOUT: %i32.loc20: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  131. // CHECK:STDOUT: }
  132. // CHECK:STDOUT: %cj: ref %i32 = bind_name cj, %cj.var
  133. // CHECK:STDOUT: name_binding_decl {
  134. // CHECK:STDOUT: %ck.patt: %i32 = binding_pattern ck
  135. // CHECK:STDOUT: %.loc21_3: %i32 = var_pattern %ck.patt
  136. // CHECK:STDOUT: }
  137. // CHECK:STDOUT: %ck.var: ref %i32 = var ck
  138. // CHECK:STDOUT: %c.ref.loc21: ref %Class = name_ref c, %c
  139. // CHECK:STDOUT: %k.ref.loc21: %Class.elem = name_ref k, @Class.%.loc13_8 [template = @Class.%.loc13_8]
  140. // CHECK:STDOUT: %.loc21_18.1: ref %i32 = class_element_access %c.ref.loc21, element1
  141. // CHECK:STDOUT: %.loc21_18.2: %i32 = bind_value %.loc21_18.1
  142. // CHECK:STDOUT: assign %ck.var, %.loc21_18.2
  143. // CHECK:STDOUT: %.loc21_11: type = splice_block %i32.loc21 [template = constants.%i32] {
  144. // CHECK:STDOUT: %int_32.loc21: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  145. // CHECK:STDOUT: %i32.loc21: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  146. // CHECK:STDOUT: }
  147. // CHECK:STDOUT: %ck: ref %i32 = bind_name ck, %ck.var
  148. // CHECK:STDOUT: return
  149. // CHECK:STDOUT: }
  150. // CHECK:STDOUT: