field_access.carbon 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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: %.1: type = tuple_type () [template]
  27. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  28. // CHECK:STDOUT: %.2: type = unbound_element_type %Class, i32 [template]
  29. // CHECK:STDOUT: %.3: type = struct_type {.j: i32, .k: i32} [template]
  30. // CHECK:STDOUT: %Run.type: type = fn_type @Run [template]
  31. // CHECK:STDOUT: %Run: %Run.type = struct_value () [template]
  32. // CHECK:STDOUT: %.4: type = ptr_type %.3 [template]
  33. // CHECK:STDOUT: %.5: i32 = int_literal 1 [template]
  34. // CHECK:STDOUT: %.6: i32 = int_literal 2 [template]
  35. // CHECK:STDOUT: }
  36. // CHECK:STDOUT:
  37. // CHECK:STDOUT: file {
  38. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  39. // CHECK:STDOUT: .Core = %Core
  40. // CHECK:STDOUT: .Class = %Class.decl
  41. // CHECK:STDOUT: .Run = %Run.decl
  42. // CHECK:STDOUT: }
  43. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  44. // CHECK:STDOUT: %Class.decl: type = class_decl @Class [template = constants.%Class] {}
  45. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  46. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  47. // CHECK:STDOUT: %Run.decl: %Run.type = fn_decl @Run [template = constants.%Run] {}
  48. // CHECK:STDOUT: %import_ref.3: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  49. // CHECK:STDOUT: %import_ref.4: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  50. // CHECK:STDOUT: }
  51. // CHECK:STDOUT:
  52. // CHECK:STDOUT: class @Class {
  53. // CHECK:STDOUT: %int.make_type_32.loc12: init type = call constants.%Int32() [template = i32]
  54. // CHECK:STDOUT: %.loc12_10.1: type = value_of_initializer %int.make_type_32.loc12 [template = i32]
  55. // CHECK:STDOUT: %.loc12_10.2: type = converted %int.make_type_32.loc12, %.loc12_10.1 [template = i32]
  56. // CHECK:STDOUT: %.loc12_8: %.2 = field_decl j, element0 [template]
  57. // CHECK:STDOUT: %int.make_type_32.loc13: init type = call constants.%Int32() [template = i32]
  58. // CHECK:STDOUT: %.loc13_10.1: type = value_of_initializer %int.make_type_32.loc13 [template = i32]
  59. // CHECK:STDOUT: %.loc13_10.2: type = converted %int.make_type_32.loc13, %.loc13_10.1 [template = i32]
  60. // CHECK:STDOUT: %.loc13_8: %.2 = field_decl k, element1 [template]
  61. // CHECK:STDOUT:
  62. // CHECK:STDOUT: !members:
  63. // CHECK:STDOUT: .Self = constants.%Class
  64. // CHECK:STDOUT: .j = %.loc12_8
  65. // CHECK:STDOUT: .k = %.loc13_8
  66. // CHECK:STDOUT: }
  67. // CHECK:STDOUT:
  68. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  69. // CHECK:STDOUT:
  70. // CHECK:STDOUT: fn @Run() {
  71. // CHECK:STDOUT: !entry:
  72. // CHECK:STDOUT: %Class.ref: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  73. // CHECK:STDOUT: %c.var: ref %Class = var c
  74. // CHECK:STDOUT: %c: ref %Class = bind_name c, %c.var
  75. // CHECK:STDOUT: %c.ref.loc18: ref %Class = name_ref c, %c
  76. // CHECK:STDOUT: %j.ref.loc18: %.2 = name_ref j, @Class.%.loc12_8 [template = @Class.%.loc12_8]
  77. // CHECK:STDOUT: %.loc18_4: ref i32 = class_element_access %c.ref.loc18, element0
  78. // CHECK:STDOUT: %.loc18_9: i32 = int_literal 1 [template = constants.%.5]
  79. // CHECK:STDOUT: assign %.loc18_4, %.loc18_9
  80. // CHECK:STDOUT: %c.ref.loc19: ref %Class = name_ref c, %c
  81. // CHECK:STDOUT: %k.ref.loc19: %.2 = name_ref k, @Class.%.loc13_8 [template = @Class.%.loc13_8]
  82. // CHECK:STDOUT: %.loc19_4: ref i32 = class_element_access %c.ref.loc19, element1
  83. // CHECK:STDOUT: %.loc19_9: i32 = int_literal 2 [template = constants.%.6]
  84. // CHECK:STDOUT: assign %.loc19_4, %.loc19_9
  85. // CHECK:STDOUT: %int.make_type_32.loc20: init type = call constants.%Int32() [template = i32]
  86. // CHECK:STDOUT: %.loc20_11.1: type = value_of_initializer %int.make_type_32.loc20 [template = i32]
  87. // CHECK:STDOUT: %.loc20_11.2: type = converted %int.make_type_32.loc20, %.loc20_11.1 [template = i32]
  88. // CHECK:STDOUT: %cj.var: ref i32 = var cj
  89. // CHECK:STDOUT: %cj: ref i32 = bind_name cj, %cj.var
  90. // CHECK:STDOUT: %c.ref.loc20: ref %Class = name_ref c, %c
  91. // CHECK:STDOUT: %j.ref.loc20: %.2 = name_ref j, @Class.%.loc12_8 [template = @Class.%.loc12_8]
  92. // CHECK:STDOUT: %.loc20_18.1: ref i32 = class_element_access %c.ref.loc20, element0
  93. // CHECK:STDOUT: %.loc20_18.2: i32 = bind_value %.loc20_18.1
  94. // CHECK:STDOUT: assign %cj.var, %.loc20_18.2
  95. // CHECK:STDOUT: %int.make_type_32.loc21: init type = call constants.%Int32() [template = i32]
  96. // CHECK:STDOUT: %.loc21_11.1: type = value_of_initializer %int.make_type_32.loc21 [template = i32]
  97. // CHECK:STDOUT: %.loc21_11.2: type = converted %int.make_type_32.loc21, %.loc21_11.1 [template = i32]
  98. // CHECK:STDOUT: %ck.var: ref i32 = var ck
  99. // CHECK:STDOUT: %ck: ref i32 = bind_name ck, %ck.var
  100. // CHECK:STDOUT: %c.ref.loc21: ref %Class = name_ref c, %c
  101. // CHECK:STDOUT: %k.ref.loc21: %.2 = name_ref k, @Class.%.loc13_8 [template = @Class.%.loc13_8]
  102. // CHECK:STDOUT: %.loc21_18.1: ref i32 = class_element_access %c.ref.loc21, element1
  103. // CHECK:STDOUT: %.loc21_18.2: i32 = bind_value %.loc21_18.1
  104. // CHECK:STDOUT: assign %ck.var, %.loc21_18.2
  105. // CHECK:STDOUT: return
  106. // CHECK:STDOUT: }
  107. // CHECK:STDOUT: