access_modifers.carbon 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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/access_modifers.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/access_modifers.carbon
  10. // --- fail_private_field_access.carbon
  11. library "[[@TEST_NAME]]";
  12. class Circle {
  13. private var radius: i32;
  14. private let SOME_INTERNAL_CONSTANT: i32 = 5;
  15. private fn SomeInternalFunction() -> i32 {
  16. return 0;
  17. }
  18. fn Make() -> Self {
  19. return {.radius = 5};
  20. }
  21. }
  22. fn Run() {
  23. let circle: Circle = Circle.Make();
  24. // CHECK:STDERR: fail_private_field_access.carbon:[[@LINE+7]]:21: error: cannot access private member `radius` of type `Circle`
  25. // CHECK:STDERR: let radius: i32 = circle.radius;
  26. // CHECK:STDERR: ^~~~~~~~~~~~~
  27. // CHECK:STDERR: fail_private_field_access.carbon:[[@LINE-17]]:15: note: declared here
  28. // CHECK:STDERR: private var radius: i32;
  29. // CHECK:STDERR: ^~~~~~~~~~~
  30. // CHECK:STDERR:
  31. let radius: i32 = circle.radius;
  32. // CHECK:STDERR: fail_private_field_access.carbon:[[@LINE+7]]:3: error: cannot access private member `radius` of type `Circle`
  33. // CHECK:STDERR: circle.radius = 5;
  34. // CHECK:STDERR: ^~~~~~~~~~~~~
  35. // CHECK:STDERR: fail_private_field_access.carbon:[[@LINE-25]]:15: note: declared here
  36. // CHECK:STDERR: private var radius: i32;
  37. // CHECK:STDERR: ^~~~~~~~~~~
  38. // CHECK:STDERR:
  39. circle.radius = 5;
  40. // CHECK:STDERR: fail_private_field_access.carbon:[[@LINE+7]]:3: error: cannot access private member `SOME_INTERNAL_CONSTANT` of type `Circle`
  41. // CHECK:STDERR: circle.SOME_INTERNAL_CONSTANT;
  42. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  43. // CHECK:STDERR: fail_private_field_access.carbon:[[@LINE-32]]:15: note: declared here
  44. // CHECK:STDERR: private let SOME_INTERNAL_CONSTANT: i32 = 5;
  45. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~
  46. // CHECK:STDERR:
  47. circle.SOME_INTERNAL_CONSTANT;
  48. // CHECK:STDERR: fail_private_field_access.carbon:[[@LINE+7]]:3: error: cannot access private member `SomeInternalFunction` of type `Circle`
  49. // CHECK:STDERR: circle.SomeInternalFunction();
  50. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~
  51. // CHECK:STDERR: fail_private_field_access.carbon:[[@LINE-39]]:3: note: declared here
  52. // CHECK:STDERR: private fn SomeInternalFunction() -> i32 {
  53. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  54. // CHECK:STDERR:
  55. circle.SomeInternalFunction();
  56. }
  57. // --- fail_protected_field_access.carbon
  58. library "[[@TEST_NAME]]";
  59. class A {
  60. protected var x: i32;
  61. }
  62. fn Run() {
  63. // CHECK:STDERR: fail_protected_field_access.carbon:[[@LINE+7]]:16: error: cannot access protected member `x` of type `A`
  64. // CHECK:STDERR: let x: i32 = A.x;
  65. // CHECK:STDERR: ^~~
  66. // CHECK:STDERR: fail_protected_field_access.carbon:[[@LINE-7]]:17: note: declared here
  67. // CHECK:STDERR: protected var x: i32;
  68. // CHECK:STDERR: ^~~~~~
  69. // CHECK:STDERR:
  70. let x: i32 = A.x;
  71. }
  72. // --- instance_private_field_access_on_self.carbon
  73. library "[[@TEST_NAME]]";
  74. class Circle {
  75. private var radius: i32;
  76. fn GetRadius[self: Self]() -> i32 {
  77. return self.radius;
  78. }
  79. private fn SomeInternalFunction() -> i32 {
  80. return 0;
  81. }
  82. fn Compute[self: Self]() -> i32 {
  83. return self.SomeInternalFunction();
  84. }
  85. }
  86. // --- public_global_access.carbon
  87. library "[[@TEST_NAME]]";
  88. class A {
  89. let x: i32 = 5;
  90. }
  91. let x: i32 = A.x;
  92. // --- fail_global_access.carbon
  93. library "[[@TEST_NAME]]";
  94. class A {
  95. protected let x: i32 = 5;
  96. private let y: i32 = 5;
  97. }
  98. // CHECK:STDERR: fail_global_access.carbon:[[@LINE+7]]:14: error: cannot access protected member `x` of type `A`
  99. // CHECK:STDERR: let x: i32 = A.x;
  100. // CHECK:STDERR: ^~~
  101. // CHECK:STDERR: fail_global_access.carbon:[[@LINE-7]]:17: note: declared here
  102. // CHECK:STDERR: protected let x: i32 = 5;
  103. // CHECK:STDERR: ^
  104. // CHECK:STDERR:
  105. let x: i32 = A.x;
  106. // CHECK:STDERR: fail_global_access.carbon:[[@LINE+6]]:14: error: cannot access private member `y` of type `A`
  107. // CHECK:STDERR: let y: i32 = A.y;
  108. // CHECK:STDERR: ^~~
  109. // CHECK:STDERR: fail_global_access.carbon:[[@LINE-14]]:15: note: declared here
  110. // CHECK:STDERR: private let y: i32 = 5;
  111. // CHECK:STDERR: ^
  112. let y: i32 = A.y;
  113. // --- self_access.carbon
  114. library "[[@TEST_NAME]]";
  115. class A {
  116. private fn F() {}
  117. private fn G() { Self.F(); }
  118. }
  119. // CHECK:STDOUT: --- fail_private_field_access.carbon
  120. // CHECK:STDOUT:
  121. // CHECK:STDOUT: constants {
  122. // CHECK:STDOUT: %Circle: type = class_type @Circle [template]
  123. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  124. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  125. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  126. // CHECK:STDOUT: %.2: type = unbound_element_type %Circle, i32 [template]
  127. // CHECK:STDOUT: %.3: i32 = int_literal 5 [template]
  128. // CHECK:STDOUT: %SomeInternalFunction.type: type = fn_type @SomeInternalFunction [template]
  129. // CHECK:STDOUT: %SomeInternalFunction: %SomeInternalFunction.type = struct_value () [template]
  130. // CHECK:STDOUT: %Make.type: type = fn_type @Make [template]
  131. // CHECK:STDOUT: %Make: %Make.type = struct_value () [template]
  132. // CHECK:STDOUT: %.4: type = struct_type {.radius: i32} [template]
  133. // CHECK:STDOUT: %.5: <witness> = complete_type_witness %.4 [template]
  134. // CHECK:STDOUT: %.6: i32 = int_literal 0 [template]
  135. // CHECK:STDOUT: %.7: type = ptr_type %.4 [template]
  136. // CHECK:STDOUT: %struct: %Circle = struct_value (%.3) [template]
  137. // CHECK:STDOUT: %Run.type: type = fn_type @Run [template]
  138. // CHECK:STDOUT: %Run: %Run.type = struct_value () [template]
  139. // CHECK:STDOUT: }
  140. // CHECK:STDOUT:
  141. // CHECK:STDOUT: imports {
  142. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  143. // CHECK:STDOUT: .Int32 = %import_ref
  144. // CHECK:STDOUT: import Core//prelude
  145. // CHECK:STDOUT: import Core//prelude/operators
  146. // CHECK:STDOUT: import Core//prelude/types
  147. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  148. // CHECK:STDOUT: import Core//prelude/operators/as
  149. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  150. // CHECK:STDOUT: import Core//prelude/operators/comparison
  151. // CHECK:STDOUT: import Core//prelude/types/bool
  152. // CHECK:STDOUT: }
  153. // CHECK:STDOUT: %import_ref: %Int32.type = import_ref Core//prelude/types, inst+9, loaded [template = constants.%Int32]
  154. // CHECK:STDOUT: }
  155. // CHECK:STDOUT:
  156. // CHECK:STDOUT: file {
  157. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  158. // CHECK:STDOUT: .Core = imports.%Core
  159. // CHECK:STDOUT: .Circle = %Circle.decl
  160. // CHECK:STDOUT: .Run = %Run.decl
  161. // CHECK:STDOUT: }
  162. // CHECK:STDOUT: %Core.import = import Core
  163. // CHECK:STDOUT: %Circle.decl: type = class_decl @Circle [template = constants.%Circle] {} {}
  164. // CHECK:STDOUT: %Run.decl: %Run.type = fn_decl @Run [template = constants.%Run] {} {}
  165. // CHECK:STDOUT: }
  166. // CHECK:STDOUT:
  167. // CHECK:STDOUT: class @Circle {
  168. // CHECK:STDOUT: %int.make_type_32.loc5: init type = call constants.%Int32() [template = i32]
  169. // CHECK:STDOUT: %.loc5_23.1: type = value_of_initializer %int.make_type_32.loc5 [template = i32]
  170. // CHECK:STDOUT: %.loc5_23.2: type = converted %int.make_type_32.loc5, %.loc5_23.1 [template = i32]
  171. // CHECK:STDOUT: %.loc5_21: %.2 = field_decl radius, element0 [template]
  172. // CHECK:STDOUT: %int.make_type_32.loc6: init type = call constants.%Int32() [template = i32]
  173. // CHECK:STDOUT: %.loc6_39.1: type = value_of_initializer %int.make_type_32.loc6 [template = i32]
  174. // CHECK:STDOUT: %.loc6_39.2: type = converted %int.make_type_32.loc6, %.loc6_39.1 [template = i32]
  175. // CHECK:STDOUT: %.loc6_45: i32 = int_literal 5 [template = constants.%.3]
  176. // CHECK:STDOUT: %SOME_INTERNAL_CONSTANT: i32 = bind_name SOME_INTERNAL_CONSTANT, %.loc6_45
  177. // CHECK:STDOUT: %SomeInternalFunction.decl: %SomeInternalFunction.type = fn_decl @SomeInternalFunction [template = constants.%SomeInternalFunction] {} {
  178. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  179. // CHECK:STDOUT: %.loc8_40.1: type = value_of_initializer %int.make_type_32 [template = i32]
  180. // CHECK:STDOUT: %.loc8_40.2: type = converted %int.make_type_32, %.loc8_40.1 [template = i32]
  181. // CHECK:STDOUT: %return: ref i32 = var <return slot>
  182. // CHECK:STDOUT: }
  183. // CHECK:STDOUT: %Make.decl: %Make.type = fn_decl @Make [template = constants.%Make] {} {
  184. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%Circle [template = constants.%Circle]
  185. // CHECK:STDOUT: %return: ref %Circle = var <return slot>
  186. // CHECK:STDOUT: }
  187. // CHECK:STDOUT: %.loc15: <witness> = complete_type_witness %.4 [template = constants.%.5]
  188. // CHECK:STDOUT:
  189. // CHECK:STDOUT: !members:
  190. // CHECK:STDOUT: .Self = constants.%Circle
  191. // CHECK:STDOUT: .radius [private] = %.loc5_21
  192. // CHECK:STDOUT: .SOME_INTERNAL_CONSTANT [private] = %SOME_INTERNAL_CONSTANT
  193. // CHECK:STDOUT: .SomeInternalFunction [private] = %SomeInternalFunction.decl
  194. // CHECK:STDOUT: .Make = %Make.decl
  195. // CHECK:STDOUT: }
  196. // CHECK:STDOUT:
  197. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  198. // CHECK:STDOUT:
  199. // CHECK:STDOUT: fn @SomeInternalFunction() -> i32 {
  200. // CHECK:STDOUT: !entry:
  201. // CHECK:STDOUT: %.loc9: i32 = int_literal 0 [template = constants.%.6]
  202. // CHECK:STDOUT: return %.loc9
  203. // CHECK:STDOUT: }
  204. // CHECK:STDOUT:
  205. // CHECK:STDOUT: fn @Make() -> %return: %Circle {
  206. // CHECK:STDOUT: !entry:
  207. // CHECK:STDOUT: %.loc13_23: i32 = int_literal 5 [template = constants.%.3]
  208. // CHECK:STDOUT: %.loc13_24.1: %.4 = struct_literal (%.loc13_23)
  209. // CHECK:STDOUT: %.loc13_24.2: ref i32 = class_element_access %return, element0
  210. // CHECK:STDOUT: %.loc13_24.3: init i32 = initialize_from %.loc13_23 to %.loc13_24.2 [template = constants.%.3]
  211. // CHECK:STDOUT: %.loc13_24.4: init %Circle = class_init (%.loc13_24.3), %return [template = constants.%struct]
  212. // CHECK:STDOUT: %.loc13_25: init %Circle = converted %.loc13_24.1, %.loc13_24.4 [template = constants.%struct]
  213. // CHECK:STDOUT: return %.loc13_25 to %return
  214. // CHECK:STDOUT: }
  215. // CHECK:STDOUT:
  216. // CHECK:STDOUT: fn @Run() {
  217. // CHECK:STDOUT: !entry:
  218. // CHECK:STDOUT: %Circle.ref.loc18_15: type = name_ref Circle, file.%Circle.decl [template = constants.%Circle]
  219. // CHECK:STDOUT: %Circle.ref.loc18_24: type = name_ref Circle, file.%Circle.decl [template = constants.%Circle]
  220. // CHECK:STDOUT: %Make.ref: %Make.type = name_ref Make, @Circle.%Make.decl [template = constants.%Make]
  221. // CHECK:STDOUT: %.loc18_35.1: ref %Circle = temporary_storage
  222. // CHECK:STDOUT: %Make.call: init %Circle = call %Make.ref() to %.loc18_35.1
  223. // CHECK:STDOUT: %.loc18_35.2: ref %Circle = temporary %.loc18_35.1, %Make.call
  224. // CHECK:STDOUT: %.loc18_35.3: %Circle = bind_value %.loc18_35.2
  225. // CHECK:STDOUT: %circle: %Circle = bind_name circle, %.loc18_35.3
  226. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  227. // CHECK:STDOUT: %.loc26_15.1: type = value_of_initializer %int.make_type_32 [template = i32]
  228. // CHECK:STDOUT: %.loc26_15.2: type = converted %int.make_type_32, %.loc26_15.1 [template = i32]
  229. // CHECK:STDOUT: %circle.ref.loc26: %Circle = name_ref circle, %circle
  230. // CHECK:STDOUT: %radius.ref.loc26: <error> = name_ref radius, <error> [template = <error>]
  231. // CHECK:STDOUT: %radius: i32 = bind_name radius, <error>
  232. // CHECK:STDOUT: %circle.ref.loc34: %Circle = name_ref circle, %circle
  233. // CHECK:STDOUT: %radius.ref.loc34: <error> = name_ref radius, <error> [template = <error>]
  234. // CHECK:STDOUT: %.loc34: i32 = int_literal 5 [template = constants.%.3]
  235. // CHECK:STDOUT: assign %radius.ref.loc34, <error>
  236. // CHECK:STDOUT: %circle.ref.loc42: %Circle = name_ref circle, %circle
  237. // CHECK:STDOUT: %SOME_INTERNAL_CONSTANT.ref: <error> = name_ref SOME_INTERNAL_CONSTANT, <error> [template = <error>]
  238. // CHECK:STDOUT: %circle.ref.loc51: %Circle = name_ref circle, %circle
  239. // CHECK:STDOUT: %SomeInternalFunction.ref: <error> = name_ref SomeInternalFunction, <error> [template = <error>]
  240. // CHECK:STDOUT: return
  241. // CHECK:STDOUT: }
  242. // CHECK:STDOUT:
  243. // CHECK:STDOUT: --- fail_protected_field_access.carbon
  244. // CHECK:STDOUT:
  245. // CHECK:STDOUT: constants {
  246. // CHECK:STDOUT: %A: type = class_type @A [template]
  247. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  248. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  249. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  250. // CHECK:STDOUT: %.2: type = unbound_element_type %A, i32 [template]
  251. // CHECK:STDOUT: %.3: type = struct_type {.x: i32} [template]
  252. // CHECK:STDOUT: %.4: <witness> = complete_type_witness %.3 [template]
  253. // CHECK:STDOUT: %Run.type: type = fn_type @Run [template]
  254. // CHECK:STDOUT: %Run: %Run.type = struct_value () [template]
  255. // CHECK:STDOUT: %.5: type = ptr_type %.3 [template]
  256. // CHECK:STDOUT: }
  257. // CHECK:STDOUT:
  258. // CHECK:STDOUT: imports {
  259. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  260. // CHECK:STDOUT: .Int32 = %import_ref
  261. // CHECK:STDOUT: import Core//prelude
  262. // CHECK:STDOUT: import Core//prelude/operators
  263. // CHECK:STDOUT: import Core//prelude/types
  264. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  265. // CHECK:STDOUT: import Core//prelude/operators/as
  266. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  267. // CHECK:STDOUT: import Core//prelude/operators/comparison
  268. // CHECK:STDOUT: import Core//prelude/types/bool
  269. // CHECK:STDOUT: }
  270. // CHECK:STDOUT: %import_ref: %Int32.type = import_ref Core//prelude/types, inst+9, loaded [template = constants.%Int32]
  271. // CHECK:STDOUT: }
  272. // CHECK:STDOUT:
  273. // CHECK:STDOUT: file {
  274. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  275. // CHECK:STDOUT: .Core = imports.%Core
  276. // CHECK:STDOUT: .A = %A.decl
  277. // CHECK:STDOUT: .Run = %Run.decl
  278. // CHECK:STDOUT: }
  279. // CHECK:STDOUT: %Core.import = import Core
  280. // CHECK:STDOUT: %A.decl: type = class_decl @A [template = constants.%A] {} {}
  281. // CHECK:STDOUT: %Run.decl: %Run.type = fn_decl @Run [template = constants.%Run] {} {}
  282. // CHECK:STDOUT: }
  283. // CHECK:STDOUT:
  284. // CHECK:STDOUT: class @A {
  285. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  286. // CHECK:STDOUT: %.loc5_20.1: type = value_of_initializer %int.make_type_32 [template = i32]
  287. // CHECK:STDOUT: %.loc5_20.2: type = converted %int.make_type_32, %.loc5_20.1 [template = i32]
  288. // CHECK:STDOUT: %.loc5_18: %.2 = field_decl x, element0 [template]
  289. // CHECK:STDOUT: %.loc6: <witness> = complete_type_witness %.3 [template = constants.%.4]
  290. // CHECK:STDOUT:
  291. // CHECK:STDOUT: !members:
  292. // CHECK:STDOUT: .Self = constants.%A
  293. // CHECK:STDOUT: .x [protected] = %.loc5_18
  294. // CHECK:STDOUT: }
  295. // CHECK:STDOUT:
  296. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  297. // CHECK:STDOUT:
  298. // CHECK:STDOUT: fn @Run() {
  299. // CHECK:STDOUT: !entry:
  300. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  301. // CHECK:STDOUT: %.loc16_10.1: type = value_of_initializer %int.make_type_32 [template = i32]
  302. // CHECK:STDOUT: %.loc16_10.2: type = converted %int.make_type_32, %.loc16_10.1 [template = i32]
  303. // CHECK:STDOUT: %A.ref: type = name_ref A, file.%A.decl [template = constants.%A]
  304. // CHECK:STDOUT: %x.ref: <error> = name_ref x, <error> [template = <error>]
  305. // CHECK:STDOUT: %x: i32 = bind_name x, <error>
  306. // CHECK:STDOUT: return
  307. // CHECK:STDOUT: }
  308. // CHECK:STDOUT:
  309. // CHECK:STDOUT: --- instance_private_field_access_on_self.carbon
  310. // CHECK:STDOUT:
  311. // CHECK:STDOUT: constants {
  312. // CHECK:STDOUT: %Circle: type = class_type @Circle [template]
  313. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  314. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  315. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  316. // CHECK:STDOUT: %.2: type = unbound_element_type %Circle, i32 [template]
  317. // CHECK:STDOUT: %GetRadius.type: type = fn_type @GetRadius [template]
  318. // CHECK:STDOUT: %GetRadius: %GetRadius.type = struct_value () [template]
  319. // CHECK:STDOUT: %SomeInternalFunction.type: type = fn_type @SomeInternalFunction [template]
  320. // CHECK:STDOUT: %SomeInternalFunction: %SomeInternalFunction.type = struct_value () [template]
  321. // CHECK:STDOUT: %Compute.type: type = fn_type @Compute [template]
  322. // CHECK:STDOUT: %Compute: %Compute.type = struct_value () [template]
  323. // CHECK:STDOUT: %.3: type = struct_type {.radius: i32} [template]
  324. // CHECK:STDOUT: %.4: <witness> = complete_type_witness %.3 [template]
  325. // CHECK:STDOUT: %.5: type = ptr_type %.3 [template]
  326. // CHECK:STDOUT: %.6: i32 = int_literal 0 [template]
  327. // CHECK:STDOUT: }
  328. // CHECK:STDOUT:
  329. // CHECK:STDOUT: imports {
  330. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  331. // CHECK:STDOUT: .Int32 = %import_ref
  332. // CHECK:STDOUT: import Core//prelude
  333. // CHECK:STDOUT: import Core//prelude/operators
  334. // CHECK:STDOUT: import Core//prelude/types
  335. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  336. // CHECK:STDOUT: import Core//prelude/operators/as
  337. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  338. // CHECK:STDOUT: import Core//prelude/operators/comparison
  339. // CHECK:STDOUT: import Core//prelude/types/bool
  340. // CHECK:STDOUT: }
  341. // CHECK:STDOUT: %import_ref: %Int32.type = import_ref Core//prelude/types, inst+9, loaded [template = constants.%Int32]
  342. // CHECK:STDOUT: }
  343. // CHECK:STDOUT:
  344. // CHECK:STDOUT: file {
  345. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  346. // CHECK:STDOUT: .Core = imports.%Core
  347. // CHECK:STDOUT: .Circle = %Circle.decl
  348. // CHECK:STDOUT: }
  349. // CHECK:STDOUT: %Core.import = import Core
  350. // CHECK:STDOUT: %Circle.decl: type = class_decl @Circle [template = constants.%Circle] {} {}
  351. // CHECK:STDOUT: }
  352. // CHECK:STDOUT:
  353. // CHECK:STDOUT: class @Circle {
  354. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  355. // CHECK:STDOUT: %.loc5_23.1: type = value_of_initializer %int.make_type_32 [template = i32]
  356. // CHECK:STDOUT: %.loc5_23.2: type = converted %int.make_type_32, %.loc5_23.1 [template = i32]
  357. // CHECK:STDOUT: %.loc5_21: %.2 = field_decl radius, element0 [template]
  358. // CHECK:STDOUT: %GetRadius.decl: %GetRadius.type = fn_decl @GetRadius [template = constants.%GetRadius] {
  359. // CHECK:STDOUT: %self.patt: %Circle = binding_pattern self
  360. // CHECK:STDOUT: %self.param_patt: %Circle = param_pattern %self.patt, runtime_param0
  361. // CHECK:STDOUT: } {
  362. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%Circle [template = constants.%Circle]
  363. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  364. // CHECK:STDOUT: %.loc7_33.1: type = value_of_initializer %int.make_type_32 [template = i32]
  365. // CHECK:STDOUT: %.loc7_33.2: type = converted %int.make_type_32, %.loc7_33.1 [template = i32]
  366. // CHECK:STDOUT: %return: ref i32 = var <return slot>
  367. // CHECK:STDOUT: %param: %Circle = param runtime_param0
  368. // CHECK:STDOUT: %self: %Circle = bind_name self, %param
  369. // CHECK:STDOUT: }
  370. // CHECK:STDOUT: %SomeInternalFunction.decl: %SomeInternalFunction.type = fn_decl @SomeInternalFunction [template = constants.%SomeInternalFunction] {} {
  371. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  372. // CHECK:STDOUT: %.loc11_40.1: type = value_of_initializer %int.make_type_32 [template = i32]
  373. // CHECK:STDOUT: %.loc11_40.2: type = converted %int.make_type_32, %.loc11_40.1 [template = i32]
  374. // CHECK:STDOUT: %return: ref i32 = var <return slot>
  375. // CHECK:STDOUT: }
  376. // CHECK:STDOUT: %Compute.decl: %Compute.type = fn_decl @Compute [template = constants.%Compute] {
  377. // CHECK:STDOUT: %self.patt: %Circle = binding_pattern self
  378. // CHECK:STDOUT: %self.param_patt: %Circle = param_pattern %self.patt, runtime_param0
  379. // CHECK:STDOUT: } {
  380. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%Circle [template = constants.%Circle]
  381. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  382. // CHECK:STDOUT: %.loc15_31.1: type = value_of_initializer %int.make_type_32 [template = i32]
  383. // CHECK:STDOUT: %.loc15_31.2: type = converted %int.make_type_32, %.loc15_31.1 [template = i32]
  384. // CHECK:STDOUT: %return: ref i32 = var <return slot>
  385. // CHECK:STDOUT: %param: %Circle = param runtime_param0
  386. // CHECK:STDOUT: %self: %Circle = bind_name self, %param
  387. // CHECK:STDOUT: }
  388. // CHECK:STDOUT: %.loc18: <witness> = complete_type_witness %.3 [template = constants.%.4]
  389. // CHECK:STDOUT:
  390. // CHECK:STDOUT: !members:
  391. // CHECK:STDOUT: .Self = constants.%Circle
  392. // CHECK:STDOUT: .radius [private] = %.loc5_21
  393. // CHECK:STDOUT: .GetRadius = %GetRadius.decl
  394. // CHECK:STDOUT: .SomeInternalFunction [private] = %SomeInternalFunction.decl
  395. // CHECK:STDOUT: .Compute = %Compute.decl
  396. // CHECK:STDOUT: }
  397. // CHECK:STDOUT:
  398. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  399. // CHECK:STDOUT:
  400. // CHECK:STDOUT: fn @GetRadius[%self.param_patt: %Circle]() -> i32 {
  401. // CHECK:STDOUT: !entry:
  402. // CHECK:STDOUT: %self.ref: %Circle = name_ref self, %self
  403. // CHECK:STDOUT: %radius.ref: %.2 = name_ref radius, @Circle.%.loc5_21 [template = @Circle.%.loc5_21]
  404. // CHECK:STDOUT: %.loc8_16.1: ref i32 = class_element_access %self.ref, element0
  405. // CHECK:STDOUT: %.loc8_16.2: i32 = bind_value %.loc8_16.1
  406. // CHECK:STDOUT: return %.loc8_16.2
  407. // CHECK:STDOUT: }
  408. // CHECK:STDOUT:
  409. // CHECK:STDOUT: fn @SomeInternalFunction() -> i32 {
  410. // CHECK:STDOUT: !entry:
  411. // CHECK:STDOUT: %.loc12: i32 = int_literal 0 [template = constants.%.6]
  412. // CHECK:STDOUT: return %.loc12
  413. // CHECK:STDOUT: }
  414. // CHECK:STDOUT:
  415. // CHECK:STDOUT: fn @Compute[%self.param_patt: %Circle]() -> i32 {
  416. // CHECK:STDOUT: !entry:
  417. // CHECK:STDOUT: %self.ref: %Circle = name_ref self, %self
  418. // CHECK:STDOUT: %SomeInternalFunction.ref: %SomeInternalFunction.type = name_ref SomeInternalFunction, @Circle.%SomeInternalFunction.decl [template = constants.%SomeInternalFunction]
  419. // CHECK:STDOUT: %SomeInternalFunction.call: init i32 = call %SomeInternalFunction.ref()
  420. // CHECK:STDOUT: %.loc16_39.1: i32 = value_of_initializer %SomeInternalFunction.call
  421. // CHECK:STDOUT: %.loc16_39.2: i32 = converted %SomeInternalFunction.call, %.loc16_39.1
  422. // CHECK:STDOUT: return %.loc16_39.2
  423. // CHECK:STDOUT: }
  424. // CHECK:STDOUT:
  425. // CHECK:STDOUT: --- public_global_access.carbon
  426. // CHECK:STDOUT:
  427. // CHECK:STDOUT: constants {
  428. // CHECK:STDOUT: %A: type = class_type @A [template]
  429. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  430. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  431. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  432. // CHECK:STDOUT: %.2: i32 = int_literal 5 [template]
  433. // CHECK:STDOUT: %.3: type = struct_type {} [template]
  434. // CHECK:STDOUT: %.4: <witness> = complete_type_witness %.3 [template]
  435. // CHECK:STDOUT: %.5: type = ptr_type %.3 [template]
  436. // CHECK:STDOUT: }
  437. // CHECK:STDOUT:
  438. // CHECK:STDOUT: imports {
  439. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  440. // CHECK:STDOUT: .Int32 = %import_ref
  441. // CHECK:STDOUT: import Core//prelude
  442. // CHECK:STDOUT: import Core//prelude/operators
  443. // CHECK:STDOUT: import Core//prelude/types
  444. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  445. // CHECK:STDOUT: import Core//prelude/operators/as
  446. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  447. // CHECK:STDOUT: import Core//prelude/operators/comparison
  448. // CHECK:STDOUT: import Core//prelude/types/bool
  449. // CHECK:STDOUT: }
  450. // CHECK:STDOUT: %import_ref: %Int32.type = import_ref Core//prelude/types, inst+9, loaded [template = constants.%Int32]
  451. // CHECK:STDOUT: }
  452. // CHECK:STDOUT:
  453. // CHECK:STDOUT: file {
  454. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  455. // CHECK:STDOUT: .Core = imports.%Core
  456. // CHECK:STDOUT: .A = %A.decl
  457. // CHECK:STDOUT: .x = @__global_init.%x
  458. // CHECK:STDOUT: }
  459. // CHECK:STDOUT: %Core.import = import Core
  460. // CHECK:STDOUT: %A.decl: type = class_decl @A [template = constants.%A] {} {}
  461. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  462. // CHECK:STDOUT: %.loc8_8.1: type = value_of_initializer %int.make_type_32 [template = i32]
  463. // CHECK:STDOUT: %.loc8_8.2: type = converted %int.make_type_32, %.loc8_8.1 [template = i32]
  464. // CHECK:STDOUT: }
  465. // CHECK:STDOUT:
  466. // CHECK:STDOUT: class @A {
  467. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  468. // CHECK:STDOUT: %.loc5_10.1: type = value_of_initializer %int.make_type_32 [template = i32]
  469. // CHECK:STDOUT: %.loc5_10.2: type = converted %int.make_type_32, %.loc5_10.1 [template = i32]
  470. // CHECK:STDOUT: %.loc5_16: i32 = int_literal 5 [template = constants.%.2]
  471. // CHECK:STDOUT: %x: i32 = bind_name x, %.loc5_16
  472. // CHECK:STDOUT: %.loc6: <witness> = complete_type_witness %.3 [template = constants.%.4]
  473. // CHECK:STDOUT:
  474. // CHECK:STDOUT: !members:
  475. // CHECK:STDOUT: .Self = constants.%A
  476. // CHECK:STDOUT: .x = %x
  477. // CHECK:STDOUT: }
  478. // CHECK:STDOUT:
  479. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  480. // CHECK:STDOUT:
  481. // CHECK:STDOUT: fn @__global_init() {
  482. // CHECK:STDOUT: !entry:
  483. // CHECK:STDOUT: %A.ref: type = name_ref A, file.%A.decl [template = constants.%A]
  484. // CHECK:STDOUT: %x.ref: i32 = name_ref x, @A.%x
  485. // CHECK:STDOUT: %x: i32 = bind_name x, %x.ref
  486. // CHECK:STDOUT: return
  487. // CHECK:STDOUT: }
  488. // CHECK:STDOUT:
  489. // CHECK:STDOUT: --- fail_global_access.carbon
  490. // CHECK:STDOUT:
  491. // CHECK:STDOUT: constants {
  492. // CHECK:STDOUT: %A: type = class_type @A [template]
  493. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  494. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  495. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  496. // CHECK:STDOUT: %.2: i32 = int_literal 5 [template]
  497. // CHECK:STDOUT: %.3: type = struct_type {} [template]
  498. // CHECK:STDOUT: %.4: <witness> = complete_type_witness %.3 [template]
  499. // CHECK:STDOUT: %.5: type = ptr_type %.3 [template]
  500. // CHECK:STDOUT: }
  501. // CHECK:STDOUT:
  502. // CHECK:STDOUT: imports {
  503. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  504. // CHECK:STDOUT: .Int32 = %import_ref
  505. // CHECK:STDOUT: import Core//prelude
  506. // CHECK:STDOUT: import Core//prelude/operators
  507. // CHECK:STDOUT: import Core//prelude/types
  508. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  509. // CHECK:STDOUT: import Core//prelude/operators/as
  510. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  511. // CHECK:STDOUT: import Core//prelude/operators/comparison
  512. // CHECK:STDOUT: import Core//prelude/types/bool
  513. // CHECK:STDOUT: }
  514. // CHECK:STDOUT: %import_ref: %Int32.type = import_ref Core//prelude/types, inst+9, loaded [template = constants.%Int32]
  515. // CHECK:STDOUT: }
  516. // CHECK:STDOUT:
  517. // CHECK:STDOUT: file {
  518. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  519. // CHECK:STDOUT: .Core = imports.%Core
  520. // CHECK:STDOUT: .A = %A.decl
  521. // CHECK:STDOUT: .x = @__global_init.%x
  522. // CHECK:STDOUT: .y = @__global_init.%y
  523. // CHECK:STDOUT: }
  524. // CHECK:STDOUT: %Core.import = import Core
  525. // CHECK:STDOUT: %A.decl: type = class_decl @A [template = constants.%A] {} {}
  526. // CHECK:STDOUT: %int.make_type_32.loc16: init type = call constants.%Int32() [template = i32]
  527. // CHECK:STDOUT: %.loc16_8.1: type = value_of_initializer %int.make_type_32.loc16 [template = i32]
  528. // CHECK:STDOUT: %.loc16_8.2: type = converted %int.make_type_32.loc16, %.loc16_8.1 [template = i32]
  529. // CHECK:STDOUT: %int.make_type_32.loc23: init type = call constants.%Int32() [template = i32]
  530. // CHECK:STDOUT: %.loc23_8.1: type = value_of_initializer %int.make_type_32.loc23 [template = i32]
  531. // CHECK:STDOUT: %.loc23_8.2: type = converted %int.make_type_32.loc23, %.loc23_8.1 [template = i32]
  532. // CHECK:STDOUT: }
  533. // CHECK:STDOUT:
  534. // CHECK:STDOUT: class @A {
  535. // CHECK:STDOUT: %int.make_type_32.loc5: init type = call constants.%Int32() [template = i32]
  536. // CHECK:STDOUT: %.loc5_20.1: type = value_of_initializer %int.make_type_32.loc5 [template = i32]
  537. // CHECK:STDOUT: %.loc5_20.2: type = converted %int.make_type_32.loc5, %.loc5_20.1 [template = i32]
  538. // CHECK:STDOUT: %.loc5_26: i32 = int_literal 5 [template = constants.%.2]
  539. // CHECK:STDOUT: %x: i32 = bind_name x, %.loc5_26
  540. // CHECK:STDOUT: %int.make_type_32.loc6: init type = call constants.%Int32() [template = i32]
  541. // CHECK:STDOUT: %.loc6_18.1: type = value_of_initializer %int.make_type_32.loc6 [template = i32]
  542. // CHECK:STDOUT: %.loc6_18.2: type = converted %int.make_type_32.loc6, %.loc6_18.1 [template = i32]
  543. // CHECK:STDOUT: %.loc6_24: i32 = int_literal 5 [template = constants.%.2]
  544. // CHECK:STDOUT: %y: i32 = bind_name y, %.loc6_24
  545. // CHECK:STDOUT: %.loc7: <witness> = complete_type_witness %.3 [template = constants.%.4]
  546. // CHECK:STDOUT:
  547. // CHECK:STDOUT: !members:
  548. // CHECK:STDOUT: .Self = constants.%A
  549. // CHECK:STDOUT: .x [protected] = %x
  550. // CHECK:STDOUT: .y [private] = %y
  551. // CHECK:STDOUT: }
  552. // CHECK:STDOUT:
  553. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  554. // CHECK:STDOUT:
  555. // CHECK:STDOUT: fn @__global_init() {
  556. // CHECK:STDOUT: !entry:
  557. // CHECK:STDOUT: %A.ref.loc16: type = name_ref A, file.%A.decl [template = constants.%A]
  558. // CHECK:STDOUT: %x.ref: <error> = name_ref x, <error> [template = <error>]
  559. // CHECK:STDOUT: %x: i32 = bind_name x, <error>
  560. // CHECK:STDOUT: %A.ref.loc23: type = name_ref A, file.%A.decl [template = constants.%A]
  561. // CHECK:STDOUT: %y.ref: <error> = name_ref y, <error> [template = <error>]
  562. // CHECK:STDOUT: %y: i32 = bind_name y, <error>
  563. // CHECK:STDOUT: return
  564. // CHECK:STDOUT: }
  565. // CHECK:STDOUT:
  566. // CHECK:STDOUT: --- self_access.carbon
  567. // CHECK:STDOUT:
  568. // CHECK:STDOUT: constants {
  569. // CHECK:STDOUT: %A: type = class_type @A [template]
  570. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  571. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  572. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  573. // CHECK:STDOUT: %G.type: type = fn_type @G [template]
  574. // CHECK:STDOUT: %G: %G.type = struct_value () [template]
  575. // CHECK:STDOUT: %.2: type = struct_type {} [template]
  576. // CHECK:STDOUT: %.3: <witness> = complete_type_witness %.2 [template]
  577. // CHECK:STDOUT: %.4: type = ptr_type %.2 [template]
  578. // CHECK:STDOUT: }
  579. // CHECK:STDOUT:
  580. // CHECK:STDOUT: imports {
  581. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  582. // CHECK:STDOUT: import Core//prelude
  583. // CHECK:STDOUT: import Core//prelude/operators
  584. // CHECK:STDOUT: import Core//prelude/types
  585. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  586. // CHECK:STDOUT: import Core//prelude/operators/as
  587. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  588. // CHECK:STDOUT: import Core//prelude/operators/comparison
  589. // CHECK:STDOUT: import Core//prelude/types/bool
  590. // CHECK:STDOUT: }
  591. // CHECK:STDOUT: }
  592. // CHECK:STDOUT:
  593. // CHECK:STDOUT: file {
  594. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  595. // CHECK:STDOUT: .Core = imports.%Core
  596. // CHECK:STDOUT: .A = %A.decl
  597. // CHECK:STDOUT: }
  598. // CHECK:STDOUT: %Core.import = import Core
  599. // CHECK:STDOUT: %A.decl: type = class_decl @A [template = constants.%A] {} {}
  600. // CHECK:STDOUT: }
  601. // CHECK:STDOUT:
  602. // CHECK:STDOUT: class @A {
  603. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {} {}
  604. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {} {}
  605. // CHECK:STDOUT: %.loc7: <witness> = complete_type_witness %.2 [template = constants.%.3]
  606. // CHECK:STDOUT:
  607. // CHECK:STDOUT: !members:
  608. // CHECK:STDOUT: .Self = constants.%A
  609. // CHECK:STDOUT: .F [private] = %F.decl
  610. // CHECK:STDOUT: .G [private] = %G.decl
  611. // CHECK:STDOUT: }
  612. // CHECK:STDOUT:
  613. // CHECK:STDOUT: fn @F() {
  614. // CHECK:STDOUT: !entry:
  615. // CHECK:STDOUT: return
  616. // CHECK:STDOUT: }
  617. // CHECK:STDOUT:
  618. // CHECK:STDOUT: fn @G() {
  619. // CHECK:STDOUT: !entry:
  620. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%A [template = constants.%A]
  621. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, @A.%F.decl [template = constants.%F]
  622. // CHECK:STDOUT: %F.call: init %.1 = call %F.ref()
  623. // CHECK:STDOUT: return
  624. // CHECK:STDOUT: }
  625. // CHECK:STDOUT: