field.carbon 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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/generic/field.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/generic/field.carbon
  10. class Class(T:! type) {
  11. var x: T;
  12. }
  13. fn F(c: Class(i32)) -> i32 {
  14. return c.x;
  15. }
  16. fn G(T:! type, c: Class(T)) -> T {
  17. return c.x;
  18. }
  19. fn H(U:! type, c: Class(U)) -> U {
  20. return c.x;
  21. }
  22. // CHECK:STDOUT: --- field.carbon
  23. // CHECK:STDOUT:
  24. // CHECK:STDOUT: constants {
  25. // CHECK:STDOUT: %T: type = bind_symbolic_name T 0 [symbolic]
  26. // CHECK:STDOUT: %Class.type: type = generic_class_type @Class [template]
  27. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  28. // CHECK:STDOUT: %Class.1: %Class.type = struct_value () [template]
  29. // CHECK:STDOUT: %Class.2: type = class_type @Class, @Class(%T) [symbolic]
  30. // CHECK:STDOUT: %.2: type = unbound_element_type %Class.2, %T [symbolic]
  31. // CHECK:STDOUT: %.3: type = struct_type {.x: %T} [symbolic]
  32. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  33. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  34. // CHECK:STDOUT: %Class.3: type = class_type @Class, @Class(i32) [template]
  35. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  36. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  37. // CHECK:STDOUT: %.4: type = unbound_element_type %Class.3, i32 [template]
  38. // CHECK:STDOUT: %.5: type = ptr_type %.3 [symbolic]
  39. // CHECK:STDOUT: %G.type: type = fn_type @G [template]
  40. // CHECK:STDOUT: %G: %G.type = struct_value () [template]
  41. // CHECK:STDOUT: %U: type = bind_symbolic_name U 0 [symbolic]
  42. // CHECK:STDOUT: %Class.4: type = class_type @Class, @Class(%U) [symbolic]
  43. // CHECK:STDOUT: %H.type: type = fn_type @H [template]
  44. // CHECK:STDOUT: %H: %H.type = struct_value () [template]
  45. // CHECK:STDOUT: %.6: type = unbound_element_type %Class.4, %U [symbolic]
  46. // CHECK:STDOUT: }
  47. // CHECK:STDOUT:
  48. // CHECK:STDOUT: imports {
  49. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  50. // CHECK:STDOUT: .Int32 = %import_ref
  51. // CHECK:STDOUT: import Core//prelude
  52. // CHECK:STDOUT: import Core//prelude/operators
  53. // CHECK:STDOUT: import Core//prelude/types
  54. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  55. // CHECK:STDOUT: import Core//prelude/operators/as
  56. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  57. // CHECK:STDOUT: import Core//prelude/operators/comparison
  58. // CHECK:STDOUT: import Core//prelude/types/bool
  59. // CHECK:STDOUT: }
  60. // CHECK:STDOUT: %import_ref: %Int32.type = import_ref Core//prelude/types, inst+4, loaded [template = constants.%Int32]
  61. // CHECK:STDOUT: }
  62. // CHECK:STDOUT:
  63. // CHECK:STDOUT: file {
  64. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  65. // CHECK:STDOUT: .Core = imports.%Core
  66. // CHECK:STDOUT: .Class = %Class.decl
  67. // CHECK:STDOUT: .F = %F.decl
  68. // CHECK:STDOUT: .G = %G.decl
  69. // CHECK:STDOUT: .H = %H.decl
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT: %Core.import = import Core
  72. // CHECK:STDOUT: %Class.decl: %Class.type = class_decl @Class [template = constants.%Class.1] {
  73. // CHECK:STDOUT: %T.loc11_13.1: type = param T
  74. // CHECK:STDOUT: %T.loc11_13.2: type = bind_symbolic_name T 0, %T.loc11_13.1 [symbolic = @Class.%T (constants.%T)]
  75. // CHECK:STDOUT: }
  76. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  77. // CHECK:STDOUT: %Class.ref.loc15: %Class.type = name_ref Class, %Class.decl [template = constants.%Class.1]
  78. // CHECK:STDOUT: %int.make_type_32.loc15_15: init type = call constants.%Int32() [template = i32]
  79. // CHECK:STDOUT: %.loc15_14.1: type = value_of_initializer %int.make_type_32.loc15_15 [template = i32]
  80. // CHECK:STDOUT: %.loc15_14.2: type = converted %int.make_type_32.loc15_15, %.loc15_14.1 [template = i32]
  81. // CHECK:STDOUT: %.loc15_14.3: init type = call %Class.ref.loc15(%.loc15_14.2) [template = constants.%Class.3]
  82. // CHECK:STDOUT: %.loc15_18.1: type = value_of_initializer %.loc15_14.3 [template = constants.%Class.3]
  83. // CHECK:STDOUT: %.loc15_18.2: type = converted %.loc15_14.3, %.loc15_18.1 [template = constants.%Class.3]
  84. // CHECK:STDOUT: %c.loc15_6.1: %Class.3 = param c
  85. // CHECK:STDOUT: @F.%c: %Class.3 = bind_name c, %c.loc15_6.1
  86. // CHECK:STDOUT: %int.make_type_32.loc15_24: init type = call constants.%Int32() [template = i32]
  87. // CHECK:STDOUT: %.loc15_24.1: type = value_of_initializer %int.make_type_32.loc15_24 [template = i32]
  88. // CHECK:STDOUT: %.loc15_24.2: type = converted %int.make_type_32.loc15_24, %.loc15_24.1 [template = i32]
  89. // CHECK:STDOUT: @F.%return: ref i32 = var <return slot>
  90. // CHECK:STDOUT: }
  91. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  92. // CHECK:STDOUT: %T.loc19_6.1: type = param T
  93. // CHECK:STDOUT: @G.%T.loc19: type = bind_symbolic_name T 0, %T.loc19_6.1 [symbolic = @G.%T.1 (constants.%T)]
  94. // CHECK:STDOUT: %Class.ref.loc19: %Class.type = name_ref Class, %Class.decl [template = constants.%Class.1]
  95. // CHECK:STDOUT: %T.ref.loc19_25: type = name_ref T, @G.%T.loc19 [symbolic = @G.%T.1 (constants.%T)]
  96. // CHECK:STDOUT: %.loc19_24: init type = call %Class.ref.loc19(%T.ref.loc19_25) [symbolic = @G.%Class (constants.%Class.2)]
  97. // CHECK:STDOUT: %.loc19_26.1: type = value_of_initializer %.loc19_24 [symbolic = @G.%Class (constants.%Class.2)]
  98. // CHECK:STDOUT: %.loc19_26.2: type = converted %.loc19_24, %.loc19_26.1 [symbolic = @G.%Class (constants.%Class.2)]
  99. // CHECK:STDOUT: %c.loc19_16.1: @G.%Class (%Class.2) = param c
  100. // CHECK:STDOUT: @G.%c: @G.%Class (%Class.2) = bind_name c, %c.loc19_16.1
  101. // CHECK:STDOUT: %T.ref.loc19_32: type = name_ref T, @G.%T.loc19 [symbolic = @G.%T.1 (constants.%T)]
  102. // CHECK:STDOUT: @G.%return: ref @G.%T.1 (%T) = var <return slot>
  103. // CHECK:STDOUT: }
  104. // CHECK:STDOUT: %H.decl: %H.type = fn_decl @H [template = constants.%H] {
  105. // CHECK:STDOUT: %U.loc23_6.1: type = param U
  106. // CHECK:STDOUT: @H.%U.loc23: type = bind_symbolic_name U 0, %U.loc23_6.1 [symbolic = @H.%U.1 (constants.%U)]
  107. // CHECK:STDOUT: %Class.ref.loc23: %Class.type = name_ref Class, %Class.decl [template = constants.%Class.1]
  108. // CHECK:STDOUT: %U.ref.loc23_25: type = name_ref U, @H.%U.loc23 [symbolic = @H.%U.1 (constants.%U)]
  109. // CHECK:STDOUT: %.loc23_24: init type = call %Class.ref.loc23(%U.ref.loc23_25) [symbolic = @H.%Class (constants.%Class.4)]
  110. // CHECK:STDOUT: %.loc23_26.1: type = value_of_initializer %.loc23_24 [symbolic = @H.%Class (constants.%Class.4)]
  111. // CHECK:STDOUT: %.loc23_26.2: type = converted %.loc23_24, %.loc23_26.1 [symbolic = @H.%Class (constants.%Class.4)]
  112. // CHECK:STDOUT: %c.loc23_16.1: @H.%Class (%Class.4) = param c
  113. // CHECK:STDOUT: @H.%c: @H.%Class (%Class.4) = bind_name c, %c.loc23_16.1
  114. // CHECK:STDOUT: %U.ref.loc23_32: type = name_ref U, @H.%U.loc23 [symbolic = @H.%U.1 (constants.%U)]
  115. // CHECK:STDOUT: @H.%return: ref @H.%U.1 (%U) = var <return slot>
  116. // CHECK:STDOUT: }
  117. // CHECK:STDOUT: }
  118. // CHECK:STDOUT:
  119. // CHECK:STDOUT: generic class @Class(file.%T.loc11_13.2: type) {
  120. // CHECK:STDOUT: %T: type = bind_symbolic_name T 0 [symbolic = %T (constants.%T)]
  121. // CHECK:STDOUT:
  122. // CHECK:STDOUT: !definition:
  123. // CHECK:STDOUT: %Class: type = class_type @Class, @Class(%T) [symbolic = %Class (constants.%Class.2)]
  124. // CHECK:STDOUT: %.1: type = unbound_element_type @Class.%Class (%Class.2), @Class.%T (%T) [symbolic = %.1 (constants.%.2)]
  125. // CHECK:STDOUT:
  126. // CHECK:STDOUT: class {
  127. // CHECK:STDOUT: %T.ref: type = name_ref T, file.%T.loc11_13.2 [symbolic = %T (constants.%T)]
  128. // CHECK:STDOUT: %.loc12: @Class.%.1 (%.2) = field_decl x, element0 [template]
  129. // CHECK:STDOUT:
  130. // CHECK:STDOUT: !members:
  131. // CHECK:STDOUT: .Self = constants.%Class.2
  132. // CHECK:STDOUT: .x = %.loc12
  133. // CHECK:STDOUT: }
  134. // CHECK:STDOUT: }
  135. // CHECK:STDOUT:
  136. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  137. // CHECK:STDOUT:
  138. // CHECK:STDOUT: fn @F(%c: %Class.3) -> i32 {
  139. // CHECK:STDOUT: !entry:
  140. // CHECK:STDOUT: %c.ref: %Class.3 = name_ref c, %c
  141. // CHECK:STDOUT: %x.ref: %.4 = name_ref x, @Class.%.loc12 [template = @Class.%.loc12]
  142. // CHECK:STDOUT: %.loc16_11.1: ref i32 = class_element_access %c.ref, element0
  143. // CHECK:STDOUT: %.loc16_11.2: i32 = bind_value %.loc16_11.1
  144. // CHECK:STDOUT: return %.loc16_11.2
  145. // CHECK:STDOUT: }
  146. // CHECK:STDOUT:
  147. // CHECK:STDOUT: generic fn @G(%T.loc19: type) {
  148. // CHECK:STDOUT: %T.1: type = bind_symbolic_name T 0 [symbolic = %T.1 (constants.%T)]
  149. // CHECK:STDOUT: %Class: type = class_type @Class, @Class(%T.1) [symbolic = %Class (constants.%Class.2)]
  150. // CHECK:STDOUT:
  151. // CHECK:STDOUT: !definition:
  152. // CHECK:STDOUT: %.1: type = unbound_element_type @G.%Class (%Class.2), @G.%T.1 (%T) [symbolic = %.1 (constants.%.2)]
  153. // CHECK:STDOUT:
  154. // CHECK:STDOUT: fn(%T.loc19: type, %c: @G.%Class (%Class.2)) -> @G.%T.1 (%T) {
  155. // CHECK:STDOUT: !entry:
  156. // CHECK:STDOUT: %c.ref: @G.%Class (%Class.2) = name_ref c, %c
  157. // CHECK:STDOUT: %x.ref: @G.%.1 (%.2) = name_ref x, @Class.%.loc12 [template = @Class.%.loc12]
  158. // CHECK:STDOUT: %.loc20_11.1: ref @G.%T.1 (%T) = class_element_access %c.ref, element0
  159. // CHECK:STDOUT: %.loc20_11.2: @G.%T.1 (%T) = bind_value %.loc20_11.1
  160. // CHECK:STDOUT: return %.loc20_11.2
  161. // CHECK:STDOUT: }
  162. // CHECK:STDOUT: }
  163. // CHECK:STDOUT:
  164. // CHECK:STDOUT: generic fn @H(%U.loc23: type) {
  165. // CHECK:STDOUT: %U.1: type = bind_symbolic_name U 0 [symbolic = %U.1 (constants.%U)]
  166. // CHECK:STDOUT: %Class: type = class_type @Class, @Class(%U.1) [symbolic = %Class (constants.%Class.4)]
  167. // CHECK:STDOUT:
  168. // CHECK:STDOUT: !definition:
  169. // CHECK:STDOUT: %.1: type = unbound_element_type @H.%Class (%Class.4), @H.%U.1 (%U) [symbolic = %.1 (constants.%.6)]
  170. // CHECK:STDOUT:
  171. // CHECK:STDOUT: fn(%U.loc23: type, %c: @H.%Class (%Class.4)) -> @H.%U.1 (%U) {
  172. // CHECK:STDOUT: !entry:
  173. // CHECK:STDOUT: %c.ref: @H.%Class (%Class.4) = name_ref c, %c
  174. // CHECK:STDOUT: %x.ref: @H.%.1 (%.6) = name_ref x, @Class.%.loc12 [template = @Class.%.loc12]
  175. // CHECK:STDOUT: %.loc24_11.1: ref @H.%U.1 (%U) = class_element_access %c.ref, element0
  176. // CHECK:STDOUT: %.loc24_11.2: @H.%U.1 (%U) = bind_value %.loc24_11.1
  177. // CHECK:STDOUT: return %.loc24_11.2
  178. // CHECK:STDOUT: }
  179. // CHECK:STDOUT: }
  180. // CHECK:STDOUT:
  181. // CHECK:STDOUT: specific @Class(constants.%T) {
  182. // CHECK:STDOUT: %T => constants.%T
  183. // CHECK:STDOUT:
  184. // CHECK:STDOUT: !definition:
  185. // CHECK:STDOUT: %Class => constants.%Class.2
  186. // CHECK:STDOUT: %.1 => constants.%.2
  187. // CHECK:STDOUT: }
  188. // CHECK:STDOUT:
  189. // CHECK:STDOUT: specific @Class(@Class.%T) {
  190. // CHECK:STDOUT: %T => constants.%T
  191. // CHECK:STDOUT: }
  192. // CHECK:STDOUT:
  193. // CHECK:STDOUT: specific @Class(i32) {
  194. // CHECK:STDOUT: %T => i32
  195. // CHECK:STDOUT:
  196. // CHECK:STDOUT: !definition:
  197. // CHECK:STDOUT: %Class => constants.%Class.3
  198. // CHECK:STDOUT: %.1 => constants.%.4
  199. // CHECK:STDOUT: }
  200. // CHECK:STDOUT:
  201. // CHECK:STDOUT: specific @Class(@G.%T.1) {
  202. // CHECK:STDOUT: %T => constants.%T
  203. // CHECK:STDOUT: }
  204. // CHECK:STDOUT:
  205. // CHECK:STDOUT: specific @G(constants.%T) {
  206. // CHECK:STDOUT: %T.1 => constants.%T
  207. // CHECK:STDOUT: %Class => constants.%Class.2
  208. // CHECK:STDOUT: }
  209. // CHECK:STDOUT:
  210. // CHECK:STDOUT: specific @Class(constants.%U) {
  211. // CHECK:STDOUT: %T => constants.%U
  212. // CHECK:STDOUT:
  213. // CHECK:STDOUT: !definition:
  214. // CHECK:STDOUT: %Class => constants.%Class.4
  215. // CHECK:STDOUT: %.1 => constants.%.6
  216. // CHECK:STDOUT: }
  217. // CHECK:STDOUT:
  218. // CHECK:STDOUT: specific @Class(@H.%U.1) {
  219. // CHECK:STDOUT: %T => constants.%U
  220. // CHECK:STDOUT: }
  221. // CHECK:STDOUT:
  222. // CHECK:STDOUT: specific @H(constants.%U) {
  223. // CHECK:STDOUT: %U.1 => constants.%U
  224. // CHECK:STDOUT: %Class => constants.%Class.4
  225. // CHECK:STDOUT: }
  226. // CHECK:STDOUT: