field_access_in_value.carbon 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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: %Int32.type: type = fn_type @Int32 [template]
  27. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  28. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  29. // CHECK:STDOUT: %.2: type = unbound_element_type %Class, i32 [template]
  30. // CHECK:STDOUT: %.3: type = struct_type {.j: i32, .k: i32} [template]
  31. // CHECK:STDOUT: %.4: <witness> = complete_type_witness %.3 [template]
  32. // CHECK:STDOUT: %Test.type: type = fn_type @Test [template]
  33. // CHECK:STDOUT: %Test: %Test.type = struct_value () [template]
  34. // CHECK:STDOUT: %.5: type = ptr_type %.3 [template]
  35. // CHECK:STDOUT: %.6: i32 = int_literal 1 [template]
  36. // CHECK:STDOUT: %.7: i32 = int_literal 2 [template]
  37. // CHECK:STDOUT: }
  38. // CHECK:STDOUT:
  39. // CHECK:STDOUT: imports {
  40. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  41. // CHECK:STDOUT: .Int32 = %import_ref
  42. // CHECK:STDOUT: import Core//prelude
  43. // CHECK:STDOUT: import Core//prelude/operators
  44. // CHECK:STDOUT: import Core//prelude/types
  45. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  46. // CHECK:STDOUT: import Core//prelude/operators/as
  47. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  48. // CHECK:STDOUT: import Core//prelude/operators/comparison
  49. // CHECK:STDOUT: import Core//prelude/types/bool
  50. // CHECK:STDOUT: }
  51. // CHECK:STDOUT: %import_ref: %Int32.type = import_ref Core//prelude/types, inst+9, loaded [template = constants.%Int32]
  52. // CHECK:STDOUT: }
  53. // CHECK:STDOUT:
  54. // CHECK:STDOUT: file {
  55. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  56. // CHECK:STDOUT: .Core = imports.%Core
  57. // CHECK:STDOUT: .Class = %Class.decl
  58. // CHECK:STDOUT: .Test = %Test.decl
  59. // CHECK:STDOUT: }
  60. // CHECK:STDOUT: %Core.import = import Core
  61. // CHECK:STDOUT: %Class.decl: type = class_decl @Class [template = constants.%Class] {} {}
  62. // CHECK:STDOUT: %Test.decl: %Test.type = fn_decl @Test [template = constants.%Test] {} {}
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT:
  65. // CHECK:STDOUT: class @Class {
  66. // CHECK:STDOUT: %int.make_type_32.loc12: init type = call constants.%Int32() [template = i32]
  67. // CHECK:STDOUT: %.loc12_10.1: type = value_of_initializer %int.make_type_32.loc12 [template = i32]
  68. // CHECK:STDOUT: %.loc12_10.2: type = converted %int.make_type_32.loc12, %.loc12_10.1 [template = i32]
  69. // CHECK:STDOUT: %.loc12_8: %.2 = field_decl j, element0 [template]
  70. // CHECK:STDOUT: %int.make_type_32.loc13: init type = call constants.%Int32() [template = i32]
  71. // CHECK:STDOUT: %.loc13_10.1: type = value_of_initializer %int.make_type_32.loc13 [template = i32]
  72. // CHECK:STDOUT: %.loc13_10.2: type = converted %int.make_type_32.loc13, %.loc13_10.1 [template = i32]
  73. // CHECK:STDOUT: %.loc13_8: %.2 = field_decl k, element1 [template]
  74. // CHECK:STDOUT: %.loc14: <witness> = complete_type_witness %.3 [template = constants.%.4]
  75. // CHECK:STDOUT:
  76. // CHECK:STDOUT: !members:
  77. // CHECK:STDOUT: .Self = constants.%Class
  78. // CHECK:STDOUT: .j = %.loc12_8
  79. // CHECK:STDOUT: .k = %.loc13_8
  80. // CHECK:STDOUT: }
  81. // CHECK:STDOUT:
  82. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  83. // CHECK:STDOUT:
  84. // CHECK:STDOUT: fn @Test() {
  85. // CHECK:STDOUT: !entry:
  86. // CHECK:STDOUT: %Class.ref.loc17: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  87. // CHECK:STDOUT: %cv.var: ref %Class = var cv
  88. // CHECK:STDOUT: %cv: ref %Class = bind_name cv, %cv.var
  89. // CHECK:STDOUT: %cv.ref.loc18: ref %Class = name_ref cv, %cv
  90. // CHECK:STDOUT: %j.ref.loc18: %.2 = name_ref j, @Class.%.loc12_8 [template = @Class.%.loc12_8]
  91. // CHECK:STDOUT: %.loc18_5: ref i32 = class_element_access %cv.ref.loc18, element0
  92. // CHECK:STDOUT: %.loc18_10: i32 = int_literal 1 [template = constants.%.6]
  93. // CHECK:STDOUT: assign %.loc18_5, %.loc18_10
  94. // CHECK:STDOUT: %cv.ref.loc19: ref %Class = name_ref cv, %cv
  95. // CHECK:STDOUT: %k.ref.loc19: %.2 = name_ref k, @Class.%.loc13_8 [template = @Class.%.loc13_8]
  96. // CHECK:STDOUT: %.loc19_5: ref i32 = class_element_access %cv.ref.loc19, element1
  97. // CHECK:STDOUT: %.loc19_10: i32 = int_literal 2 [template = constants.%.7]
  98. // CHECK:STDOUT: assign %.loc19_5, %.loc19_10
  99. // CHECK:STDOUT: %Class.ref.loc20: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  100. // CHECK:STDOUT: %cv.ref.loc20: ref %Class = name_ref cv, %cv
  101. // CHECK:STDOUT: %.loc20: %Class = bind_value %cv.ref.loc20
  102. // CHECK:STDOUT: %c: %Class = bind_name c, %.loc20
  103. // CHECK:STDOUT: %int.make_type_32.loc21: init type = call constants.%Int32() [template = i32]
  104. // CHECK:STDOUT: %.loc21_11.1: type = value_of_initializer %int.make_type_32.loc21 [template = i32]
  105. // CHECK:STDOUT: %.loc21_11.2: type = converted %int.make_type_32.loc21, %.loc21_11.1 [template = i32]
  106. // CHECK:STDOUT: %cj.var: ref i32 = var cj
  107. // CHECK:STDOUT: %cj: ref i32 = bind_name cj, %cj.var
  108. // CHECK:STDOUT: %c.ref.loc21: %Class = name_ref c, %c
  109. // CHECK:STDOUT: %j.ref.loc21: %.2 = name_ref j, @Class.%.loc12_8 [template = @Class.%.loc12_8]
  110. // CHECK:STDOUT: %.loc21_18.1: ref i32 = class_element_access %c.ref.loc21, element0
  111. // CHECK:STDOUT: %.loc21_18.2: i32 = bind_value %.loc21_18.1
  112. // CHECK:STDOUT: assign %cj.var, %.loc21_18.2
  113. // CHECK:STDOUT: %int.make_type_32.loc22: init type = call constants.%Int32() [template = i32]
  114. // CHECK:STDOUT: %.loc22_11.1: type = value_of_initializer %int.make_type_32.loc22 [template = i32]
  115. // CHECK:STDOUT: %.loc22_11.2: type = converted %int.make_type_32.loc22, %.loc22_11.1 [template = i32]
  116. // CHECK:STDOUT: %ck.var: ref i32 = var ck
  117. // CHECK:STDOUT: %ck: ref i32 = bind_name ck, %ck.var
  118. // CHECK:STDOUT: %c.ref.loc22: %Class = name_ref c, %c
  119. // CHECK:STDOUT: %k.ref.loc22: %.2 = name_ref k, @Class.%.loc13_8 [template = @Class.%.loc13_8]
  120. // CHECK:STDOUT: %.loc22_18.1: ref i32 = class_element_access %c.ref.loc22, element1
  121. // CHECK:STDOUT: %.loc22_18.2: i32 = bind_value %.loc22_18.1
  122. // CHECK:STDOUT: assign %ck.var, %.loc22_18.2
  123. // CHECK:STDOUT: return
  124. // CHECK:STDOUT: }
  125. // CHECK:STDOUT: