adapt.carbon 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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/adapt.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/adapt.carbon
  10. // --- basic.carbon
  11. library "basic";
  12. class SomeClass {
  13. var a: i32;
  14. var b: i32;
  15. }
  16. class SomeClassAdapter {
  17. adapt SomeClass;
  18. }
  19. class StructAdapter {
  20. adapt {.a: i32, .b: i32};
  21. }
  22. // --- fail_not_extend.carbon
  23. library "fail_not_extend";
  24. class Adapted {
  25. fn F();
  26. }
  27. class AdaptNotExtend {
  28. adapt Adapted;
  29. }
  30. fn F(a: AdaptNotExtend) {
  31. // `Adapted` is not extended, so lookup for `F` finds nothing.
  32. // CHECK:STDERR: fail_not_extend.carbon:[[@LINE+3]]:3: ERROR: Name `F` not found.
  33. // CHECK:STDERR: a.F();
  34. // CHECK:STDERR: ^~~
  35. a.F();
  36. }
  37. // CHECK:STDOUT: --- basic.carbon
  38. // CHECK:STDOUT:
  39. // CHECK:STDOUT: constants {
  40. // CHECK:STDOUT: %SomeClass: type = class_type @SomeClass [template]
  41. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  42. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  43. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  44. // CHECK:STDOUT: %.2: type = unbound_element_type %SomeClass, i32 [template]
  45. // CHECK:STDOUT: %.3: type = struct_type {.a: i32, .b: i32} [template]
  46. // CHECK:STDOUT: %SomeClassAdapter: type = class_type @SomeClassAdapter [template]
  47. // CHECK:STDOUT: %.4: type = ptr_type %.3 [template]
  48. // CHECK:STDOUT: %StructAdapter: type = class_type @StructAdapter [template]
  49. // CHECK:STDOUT: }
  50. // CHECK:STDOUT:
  51. // CHECK:STDOUT: file {
  52. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  53. // CHECK:STDOUT: .Core = %Core
  54. // CHECK:STDOUT: .SomeClass = %SomeClass.decl
  55. // CHECK:STDOUT: .SomeClassAdapter = %SomeClassAdapter.decl
  56. // CHECK:STDOUT: .StructAdapter = %StructAdapter.decl
  57. // CHECK:STDOUT: }
  58. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  59. // CHECK:STDOUT: %SomeClass.decl: type = class_decl @SomeClass [template = constants.%SomeClass] {}
  60. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  61. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  62. // CHECK:STDOUT: %SomeClassAdapter.decl: type = class_decl @SomeClassAdapter [template = constants.%SomeClassAdapter] {}
  63. // CHECK:STDOUT: %StructAdapter.decl: type = class_decl @StructAdapter [template = constants.%StructAdapter] {}
  64. // CHECK:STDOUT: %import_ref.3: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  65. // CHECK:STDOUT: %import_ref.4: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  66. // CHECK:STDOUT: }
  67. // CHECK:STDOUT:
  68. // CHECK:STDOUT: class @SomeClass {
  69. // CHECK:STDOUT: %int.make_type_32.loc5: init type = call constants.%Int32() [template = i32]
  70. // CHECK:STDOUT: %.loc5_10.1: type = value_of_initializer %int.make_type_32.loc5 [template = i32]
  71. // CHECK:STDOUT: %.loc5_10.2: type = converted %int.make_type_32.loc5, %.loc5_10.1 [template = i32]
  72. // CHECK:STDOUT: %.loc5_8: %.2 = field_decl a, element0 [template]
  73. // CHECK:STDOUT: %int.make_type_32.loc6: init type = call constants.%Int32() [template = i32]
  74. // CHECK:STDOUT: %.loc6_10.1: type = value_of_initializer %int.make_type_32.loc6 [template = i32]
  75. // CHECK:STDOUT: %.loc6_10.2: type = converted %int.make_type_32.loc6, %.loc6_10.1 [template = i32]
  76. // CHECK:STDOUT: %.loc6_8: %.2 = field_decl b, element1 [template]
  77. // CHECK:STDOUT:
  78. // CHECK:STDOUT: !members:
  79. // CHECK:STDOUT: .Self = constants.%SomeClass
  80. // CHECK:STDOUT: .a = %.loc5_8
  81. // CHECK:STDOUT: .b = %.loc6_8
  82. // CHECK:STDOUT: }
  83. // CHECK:STDOUT:
  84. // CHECK:STDOUT: class @SomeClassAdapter {
  85. // CHECK:STDOUT: %SomeClass.ref: type = name_ref SomeClass, file.%SomeClass.decl [template = constants.%SomeClass]
  86. // CHECK:STDOUT: adapt_decl %SomeClass
  87. // CHECK:STDOUT:
  88. // CHECK:STDOUT: !members:
  89. // CHECK:STDOUT: .Self = constants.%SomeClassAdapter
  90. // CHECK:STDOUT: }
  91. // CHECK:STDOUT:
  92. // CHECK:STDOUT: class @StructAdapter {
  93. // CHECK:STDOUT: %int.make_type_32.loc14_14: init type = call constants.%Int32() [template = i32]
  94. // CHECK:STDOUT: %.loc14_14.1: type = value_of_initializer %int.make_type_32.loc14_14 [template = i32]
  95. // CHECK:STDOUT: %.loc14_14.2: type = converted %int.make_type_32.loc14_14, %.loc14_14.1 [template = i32]
  96. // CHECK:STDOUT: %int.make_type_32.loc14_23: init type = call constants.%Int32() [template = i32]
  97. // CHECK:STDOUT: %.loc14_23.1: type = value_of_initializer %int.make_type_32.loc14_23 [template = i32]
  98. // CHECK:STDOUT: %.loc14_23.2: type = converted %int.make_type_32.loc14_23, %.loc14_23.1 [template = i32]
  99. // CHECK:STDOUT: %.loc14_26: type = struct_type {.a: i32, .b: i32} [template = constants.%.3]
  100. // CHECK:STDOUT: adapt_decl %.3
  101. // CHECK:STDOUT:
  102. // CHECK:STDOUT: !members:
  103. // CHECK:STDOUT: .Self = constants.%StructAdapter
  104. // CHECK:STDOUT: }
  105. // CHECK:STDOUT:
  106. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  107. // CHECK:STDOUT:
  108. // CHECK:STDOUT: --- fail_not_extend.carbon
  109. // CHECK:STDOUT:
  110. // CHECK:STDOUT: constants {
  111. // CHECK:STDOUT: %Adapted: type = class_type @Adapted [template]
  112. // CHECK:STDOUT: %F.type.1: type = fn_type @F.1 [template]
  113. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  114. // CHECK:STDOUT: %F.1: %F.type.1 = struct_value () [template]
  115. // CHECK:STDOUT: %.2: type = struct_type {} [template]
  116. // CHECK:STDOUT: %AdaptNotExtend: type = class_type @AdaptNotExtend [template]
  117. // CHECK:STDOUT: %.3: type = ptr_type %.2 [template]
  118. // CHECK:STDOUT: %F.type.2: type = fn_type @F.2 [template]
  119. // CHECK:STDOUT: %F.2: %F.type.2 = struct_value () [template]
  120. // CHECK:STDOUT: }
  121. // CHECK:STDOUT:
  122. // CHECK:STDOUT: file {
  123. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  124. // CHECK:STDOUT: .Core = %Core
  125. // CHECK:STDOUT: .Adapted = %Adapted.decl
  126. // CHECK:STDOUT: .AdaptNotExtend = %AdaptNotExtend.decl
  127. // CHECK:STDOUT: .F = %F.decl
  128. // CHECK:STDOUT: }
  129. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  130. // CHECK:STDOUT: %Adapted.decl: type = class_decl @Adapted [template = constants.%Adapted] {}
  131. // CHECK:STDOUT: %AdaptNotExtend.decl: type = class_decl @AdaptNotExtend [template = constants.%AdaptNotExtend] {}
  132. // CHECK:STDOUT: %F.decl: %F.type.2 = fn_decl @F.2 [template = constants.%F.2] {
  133. // CHECK:STDOUT: %AdaptNotExtend.ref: type = name_ref AdaptNotExtend, %AdaptNotExtend.decl [template = constants.%AdaptNotExtend]
  134. // CHECK:STDOUT: %a.loc12_6.1: %AdaptNotExtend = param a
  135. // CHECK:STDOUT: @F.2.%a: %AdaptNotExtend = bind_name a, %a.loc12_6.1
  136. // CHECK:STDOUT: }
  137. // CHECK:STDOUT: }
  138. // CHECK:STDOUT:
  139. // CHECK:STDOUT: class @Adapted {
  140. // CHECK:STDOUT: %F.decl: %F.type.1 = fn_decl @F.1 [template = constants.%F.1] {}
  141. // CHECK:STDOUT:
  142. // CHECK:STDOUT: !members:
  143. // CHECK:STDOUT: .Self = constants.%Adapted
  144. // CHECK:STDOUT: .F = %F.decl
  145. // CHECK:STDOUT: }
  146. // CHECK:STDOUT:
  147. // CHECK:STDOUT: class @AdaptNotExtend {
  148. // CHECK:STDOUT: %Adapted.ref: type = name_ref Adapted, file.%Adapted.decl [template = constants.%Adapted]
  149. // CHECK:STDOUT: adapt_decl %Adapted
  150. // CHECK:STDOUT:
  151. // CHECK:STDOUT: !members:
  152. // CHECK:STDOUT: .Self = constants.%AdaptNotExtend
  153. // CHECK:STDOUT: }
  154. // CHECK:STDOUT:
  155. // CHECK:STDOUT: fn @F.1();
  156. // CHECK:STDOUT:
  157. // CHECK:STDOUT: fn @F.2(%a: %AdaptNotExtend) {
  158. // CHECK:STDOUT: !entry:
  159. // CHECK:STDOUT: %a.ref: %AdaptNotExtend = name_ref a, %a
  160. // CHECK:STDOUT: %F.ref: <error> = name_ref F, <error> [template = <error>]
  161. // CHECK:STDOUT: return
  162. // CHECK:STDOUT: }
  163. // CHECK:STDOUT: