field_access.carbon 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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: %Int32.type: type = fn_type @Int32 [template]
  26. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  27. // CHECK:STDOUT: %.1: type = unbound_element_type %Class, i32 [template]
  28. // CHECK:STDOUT: %.2: type = struct_type {.j: i32, .k: i32} [template]
  29. // CHECK:STDOUT: %.3: <witness> = complete_type_witness %.2 [template]
  30. // CHECK:STDOUT: %Run.type: type = fn_type @Run [template]
  31. // CHECK:STDOUT: %Run: %Run.type = struct_value () [template]
  32. // CHECK:STDOUT: %.5: Core.IntLiteral = int_value 1 [template]
  33. // CHECK:STDOUT: %Convert.type.2: type = fn_type @Convert.1, @ImplicitAs(i32) [template]
  34. // CHECK:STDOUT: %Convert.type.15: type = fn_type @Convert.11 [template]
  35. // CHECK:STDOUT: %Convert.15: %Convert.type.15 = struct_value () [template]
  36. // CHECK:STDOUT: %.29: <witness> = interface_witness (%Convert.15) [template]
  37. // CHECK:STDOUT: %.30: <bound method> = bound_method %.5, %Convert.15 [template]
  38. // CHECK:STDOUT: %.31: i32 = int_value 1 [template]
  39. // CHECK:STDOUT: %.32: Core.IntLiteral = int_value 2 [template]
  40. // CHECK:STDOUT: %.33: <bound method> = bound_method %.32, %Convert.15 [template]
  41. // CHECK:STDOUT: %.34: i32 = int_value 2 [template]
  42. // CHECK:STDOUT: }
  43. // CHECK:STDOUT:
  44. // CHECK:STDOUT: imports {
  45. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  46. // CHECK:STDOUT: .Int32 = %import_ref.1
  47. // CHECK:STDOUT: .ImplicitAs = %import_ref.2
  48. // CHECK:STDOUT: import Core//prelude
  49. // CHECK:STDOUT: import Core//prelude/...
  50. // CHECK:STDOUT: }
  51. // CHECK:STDOUT: }
  52. // CHECK:STDOUT:
  53. // CHECK:STDOUT: file {
  54. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  55. // CHECK:STDOUT: .Core = imports.%Core
  56. // CHECK:STDOUT: .Class = %Class.decl
  57. // CHECK:STDOUT: .Run = %Run.decl
  58. // CHECK:STDOUT: }
  59. // CHECK:STDOUT: %Core.import = import Core
  60. // CHECK:STDOUT: %Class.decl: type = class_decl @Class [template = constants.%Class] {} {}
  61. // CHECK:STDOUT: %Run.decl: %Run.type = fn_decl @Run [template = constants.%Run] {} {}
  62. // CHECK:STDOUT: }
  63. // CHECK:STDOUT:
  64. // CHECK:STDOUT: class @Class {
  65. // CHECK:STDOUT: %int.make_type_32.loc12: init type = call constants.%Int32() [template = i32]
  66. // CHECK:STDOUT: %.loc12_10.1: type = value_of_initializer %int.make_type_32.loc12 [template = i32]
  67. // CHECK:STDOUT: %.loc12_10.2: type = converted %int.make_type_32.loc12, %.loc12_10.1 [template = i32]
  68. // CHECK:STDOUT: %.loc12_8: %.1 = field_decl j, element0 [template]
  69. // CHECK:STDOUT: %int.make_type_32.loc13: init type = call constants.%Int32() [template = i32]
  70. // CHECK:STDOUT: %.loc13_10.1: type = value_of_initializer %int.make_type_32.loc13 [template = i32]
  71. // CHECK:STDOUT: %.loc13_10.2: type = converted %int.make_type_32.loc13, %.loc13_10.1 [template = i32]
  72. // CHECK:STDOUT: %.loc13_8: %.1 = field_decl k, element1 [template]
  73. // CHECK:STDOUT: %.loc14: <witness> = complete_type_witness %.2 [template = constants.%.3]
  74. // CHECK:STDOUT:
  75. // CHECK:STDOUT: !members:
  76. // CHECK:STDOUT: .Self = constants.%Class
  77. // CHECK:STDOUT: .j = %.loc12_8
  78. // CHECK:STDOUT: .k = %.loc13_8
  79. // CHECK:STDOUT: }
  80. // CHECK:STDOUT:
  81. // CHECK:STDOUT: fn @Run() {
  82. // CHECK:STDOUT: !entry:
  83. // CHECK:STDOUT: %Class.ref: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  84. // CHECK:STDOUT: %c.var: ref %Class = var c
  85. // CHECK:STDOUT: %c: ref %Class = bind_name c, %c.var
  86. // CHECK:STDOUT: %c.ref.loc18: ref %Class = name_ref c, %c
  87. // CHECK:STDOUT: %j.ref.loc18: %.1 = name_ref j, @Class.%.loc12_8 [template = @Class.%.loc12_8]
  88. // CHECK:STDOUT: %.loc18_4: ref i32 = class_element_access %c.ref.loc18, element0
  89. // CHECK:STDOUT: %.loc18_9: Core.IntLiteral = int_value 1 [template = constants.%.5]
  90. // CHECK:STDOUT: %.loc18_7.1: %Convert.type.2 = interface_witness_access constants.%.29, element0 [template = constants.%Convert.15]
  91. // CHECK:STDOUT: %.loc18_7.2: <bound method> = bound_method %.loc18_9, %.loc18_7.1 [template = constants.%.30]
  92. // CHECK:STDOUT: %int.convert_checked.loc18: init i32 = call %.loc18_7.2(%.loc18_9) [template = constants.%.31]
  93. // CHECK:STDOUT: %.loc18_7.3: init i32 = converted %.loc18_9, %int.convert_checked.loc18 [template = constants.%.31]
  94. // CHECK:STDOUT: assign %.loc18_4, %.loc18_7.3
  95. // CHECK:STDOUT: %c.ref.loc19: ref %Class = name_ref c, %c
  96. // CHECK:STDOUT: %k.ref.loc19: %.1 = name_ref k, @Class.%.loc13_8 [template = @Class.%.loc13_8]
  97. // CHECK:STDOUT: %.loc19_4: ref i32 = class_element_access %c.ref.loc19, element1
  98. // CHECK:STDOUT: %.loc19_9: Core.IntLiteral = int_value 2 [template = constants.%.32]
  99. // CHECK:STDOUT: %.loc19_7.1: %Convert.type.2 = interface_witness_access constants.%.29, element0 [template = constants.%Convert.15]
  100. // CHECK:STDOUT: %.loc19_7.2: <bound method> = bound_method %.loc19_9, %.loc19_7.1 [template = constants.%.33]
  101. // CHECK:STDOUT: %int.convert_checked.loc19: init i32 = call %.loc19_7.2(%.loc19_9) [template = constants.%.34]
  102. // CHECK:STDOUT: %.loc19_7.3: init i32 = converted %.loc19_9, %int.convert_checked.loc19 [template = constants.%.34]
  103. // CHECK:STDOUT: assign %.loc19_4, %.loc19_7.3
  104. // CHECK:STDOUT: %int.make_type_32.loc20: init type = call constants.%Int32() [template = i32]
  105. // CHECK:STDOUT: %.loc20_11.1: type = value_of_initializer %int.make_type_32.loc20 [template = i32]
  106. // CHECK:STDOUT: %.loc20_11.2: type = converted %int.make_type_32.loc20, %.loc20_11.1 [template = i32]
  107. // CHECK:STDOUT: %cj.var: ref i32 = var cj
  108. // CHECK:STDOUT: %cj: ref i32 = bind_name cj, %cj.var
  109. // CHECK:STDOUT: %c.ref.loc20: ref %Class = name_ref c, %c
  110. // CHECK:STDOUT: %j.ref.loc20: %.1 = name_ref j, @Class.%.loc12_8 [template = @Class.%.loc12_8]
  111. // CHECK:STDOUT: %.loc20_18.1: ref i32 = class_element_access %c.ref.loc20, element0
  112. // CHECK:STDOUT: %.loc20_18.2: i32 = bind_value %.loc20_18.1
  113. // CHECK:STDOUT: assign %cj.var, %.loc20_18.2
  114. // CHECK:STDOUT: %int.make_type_32.loc21: init type = call constants.%Int32() [template = i32]
  115. // CHECK:STDOUT: %.loc21_11.1: type = value_of_initializer %int.make_type_32.loc21 [template = i32]
  116. // CHECK:STDOUT: %.loc21_11.2: type = converted %int.make_type_32.loc21, %.loc21_11.1 [template = i32]
  117. // CHECK:STDOUT: %ck.var: ref i32 = var ck
  118. // CHECK:STDOUT: %ck: ref i32 = bind_name ck, %ck.var
  119. // CHECK:STDOUT: %c.ref.loc21: ref %Class = name_ref c, %c
  120. // CHECK:STDOUT: %k.ref.loc21: %.1 = name_ref k, @Class.%.loc13_8 [template = @Class.%.loc13_8]
  121. // CHECK:STDOUT: %.loc21_18.1: ref i32 = class_element_access %c.ref.loc21, element1
  122. // CHECK:STDOUT: %.loc21_18.2: i32 = bind_value %.loc21_18.1
  123. // CHECK:STDOUT: assign %ck.var, %.loc21_18.2
  124. // CHECK:STDOUT: return
  125. // CHECK:STDOUT: }
  126. // CHECK:STDOUT: