syntax.carbon 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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/package_expr/syntax.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/package_expr/syntax.carbon
  10. // --- global.carbon
  11. package Global;
  12. var x: i32 = 0;
  13. var y: i32 = package.x;
  14. // --- inside_fn.carbon
  15. package InsideFn;
  16. var x: i32 = 0;
  17. fn Main() {
  18. var x: i32 = 1;
  19. var y: i32 = package.x;
  20. }
  21. // --- namespace.carbon
  22. package Namespace;
  23. namespace NS;
  24. class NS.C {
  25. fn Foo() {}
  26. };
  27. fn Main() {
  28. package.NS.C.Foo();
  29. }
  30. // CHECK:STDOUT: --- global.carbon
  31. // CHECK:STDOUT:
  32. // CHECK:STDOUT: constants {
  33. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  34. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  35. // CHECK:STDOUT: %int_0.5c6: Core.IntLiteral = int_value 0 [concrete]
  36. // CHECK:STDOUT: %ImplicitAs.type.205: type = facet_type <@ImplicitAs, @ImplicitAs(%i32)> [concrete]
  37. // CHECK:STDOUT: %Convert.type.1b6: type = fn_type @Convert.1, @ImplicitAs(%i32) [concrete]
  38. // CHECK:STDOUT: %ImplicitAs.impl_witness_table.a2f = impl_witness_table (imports.%Core.import_ref.a5b), @impl.4f9 [concrete]
  39. // CHECK:STDOUT: %ImplicitAs.impl_witness.c75: <witness> = impl_witness %ImplicitAs.impl_witness_table.a2f, @impl.4f9(%int_32) [concrete]
  40. // CHECK:STDOUT: %Convert.type.035: type = fn_type @Convert.2, @impl.4f9(%int_32) [concrete]
  41. // CHECK:STDOUT: %Convert.956: %Convert.type.035 = struct_value () [concrete]
  42. // CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.205 = facet_value Core.IntLiteral, (%ImplicitAs.impl_witness.c75) [concrete]
  43. // CHECK:STDOUT: %.9c3: type = fn_type_with_self_type %Convert.type.1b6, %ImplicitAs.facet [concrete]
  44. // CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %int_0.5c6, %Convert.956 [concrete]
  45. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.956, @Convert.2(%int_32) [concrete]
  46. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %int_0.5c6, %Convert.specific_fn [concrete]
  47. // CHECK:STDOUT: %int_0.6a9: %i32 = int_value 0 [concrete]
  48. // CHECK:STDOUT: }
  49. // CHECK:STDOUT:
  50. // CHECK:STDOUT: imports {
  51. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  52. // CHECK:STDOUT: .Int = %Core.Int
  53. // CHECK:STDOUT: .ImplicitAs = %Core.ImplicitAs
  54. // CHECK:STDOUT: import Core//prelude
  55. // CHECK:STDOUT: import Core//prelude/...
  56. // CHECK:STDOUT: }
  57. // CHECK:STDOUT: }
  58. // CHECK:STDOUT:
  59. // CHECK:STDOUT: file {
  60. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  61. // CHECK:STDOUT: .Core = imports.%Core
  62. // CHECK:STDOUT: .x = %x
  63. // CHECK:STDOUT: .y = %y
  64. // CHECK:STDOUT: }
  65. // CHECK:STDOUT: %Core.import = import Core
  66. // CHECK:STDOUT: name_binding_decl {
  67. // CHECK:STDOUT: %x.patt: %i32 = binding_pattern x
  68. // CHECK:STDOUT: %.loc4_1: %i32 = var_pattern %x.patt
  69. // CHECK:STDOUT: }
  70. // CHECK:STDOUT: %x.var: ref %i32 = var x
  71. // CHECK:STDOUT: %.loc4_8: type = splice_block %i32.loc4 [concrete = constants.%i32] {
  72. // CHECK:STDOUT: %int_32.loc4: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  73. // CHECK:STDOUT: %i32.loc4: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  74. // CHECK:STDOUT: }
  75. // CHECK:STDOUT: %x: ref %i32 = bind_name x, %x.var
  76. // CHECK:STDOUT: name_binding_decl {
  77. // CHECK:STDOUT: %y.patt: %i32 = binding_pattern y
  78. // CHECK:STDOUT: %.loc5_1: %i32 = var_pattern %y.patt
  79. // CHECK:STDOUT: }
  80. // CHECK:STDOUT: %y.var: ref %i32 = var y
  81. // CHECK:STDOUT: %.loc5_8: type = splice_block %i32.loc5 [concrete = constants.%i32] {
  82. // CHECK:STDOUT: %int_32.loc5: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  83. // CHECK:STDOUT: %i32.loc5: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  84. // CHECK:STDOUT: }
  85. // CHECK:STDOUT: %y: ref %i32 = bind_name y, %y.var
  86. // CHECK:STDOUT: }
  87. // CHECK:STDOUT:
  88. // CHECK:STDOUT: fn @__global_init() {
  89. // CHECK:STDOUT: !entry:
  90. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [concrete = constants.%int_0.5c6]
  91. // CHECK:STDOUT: %impl.elem0: %.9c3 = impl_witness_access constants.%ImplicitAs.impl_witness.c75, element0 [concrete = constants.%Convert.956]
  92. // CHECK:STDOUT: %bound_method.loc4_1.1: <bound method> = bound_method %int_0, %impl.elem0 [concrete = constants.%Convert.bound]
  93. // CHECK:STDOUT: %specific_fn: <specific function> = specific_function %impl.elem0, @Convert.2(constants.%int_32) [concrete = constants.%Convert.specific_fn]
  94. // CHECK:STDOUT: %bound_method.loc4_1.2: <bound method> = bound_method %int_0, %specific_fn [concrete = constants.%bound_method]
  95. // CHECK:STDOUT: %int.convert_checked: init %i32 = call %bound_method.loc4_1.2(%int_0) [concrete = constants.%int_0.6a9]
  96. // CHECK:STDOUT: %.loc4: init %i32 = converted %int_0, %int.convert_checked [concrete = constants.%int_0.6a9]
  97. // CHECK:STDOUT: assign file.%x.var, %.loc4
  98. // CHECK:STDOUT: %package.ref: <namespace> = name_ref package, package [concrete = package]
  99. // CHECK:STDOUT: %x.ref: ref %i32 = name_ref x, file.%x
  100. // CHECK:STDOUT: %.loc5: %i32 = bind_value %x.ref
  101. // CHECK:STDOUT: assign file.%y.var, %.loc5
  102. // CHECK:STDOUT: return
  103. // CHECK:STDOUT: }
  104. // CHECK:STDOUT:
  105. // CHECK:STDOUT: --- inside_fn.carbon
  106. // CHECK:STDOUT:
  107. // CHECK:STDOUT: constants {
  108. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  109. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  110. // CHECK:STDOUT: %int_0.5c6: Core.IntLiteral = int_value 0 [concrete]
  111. // CHECK:STDOUT: %ImplicitAs.type.205: type = facet_type <@ImplicitAs, @ImplicitAs(%i32)> [concrete]
  112. // CHECK:STDOUT: %Convert.type.1b6: type = fn_type @Convert.1, @ImplicitAs(%i32) [concrete]
  113. // CHECK:STDOUT: %ImplicitAs.impl_witness_table.a2f = impl_witness_table (imports.%Core.import_ref.a5b), @impl.4f9 [concrete]
  114. // CHECK:STDOUT: %ImplicitAs.impl_witness.c75: <witness> = impl_witness %ImplicitAs.impl_witness_table.a2f, @impl.4f9(%int_32) [concrete]
  115. // CHECK:STDOUT: %Convert.type.035: type = fn_type @Convert.2, @impl.4f9(%int_32) [concrete]
  116. // CHECK:STDOUT: %Convert.956: %Convert.type.035 = struct_value () [concrete]
  117. // CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.205 = facet_value Core.IntLiteral, (%ImplicitAs.impl_witness.c75) [concrete]
  118. // CHECK:STDOUT: %.9c3: type = fn_type_with_self_type %Convert.type.1b6, %ImplicitAs.facet [concrete]
  119. // CHECK:STDOUT: %Convert.bound.d04: <bound method> = bound_method %int_0.5c6, %Convert.956 [concrete]
  120. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.956, @Convert.2(%int_32) [concrete]
  121. // CHECK:STDOUT: %bound_method.b6e: <bound method> = bound_method %int_0.5c6, %Convert.specific_fn [concrete]
  122. // CHECK:STDOUT: %int_0.6a9: %i32 = int_value 0 [concrete]
  123. // CHECK:STDOUT: %Main.type: type = fn_type @Main [concrete]
  124. // CHECK:STDOUT: %Main: %Main.type = struct_value () [concrete]
  125. // CHECK:STDOUT: %int_1.5b8: Core.IntLiteral = int_value 1 [concrete]
  126. // CHECK:STDOUT: %Convert.bound.ab5: <bound method> = bound_method %int_1.5b8, %Convert.956 [concrete]
  127. // CHECK:STDOUT: %bound_method.9a1: <bound method> = bound_method %int_1.5b8, %Convert.specific_fn [concrete]
  128. // CHECK:STDOUT: %int_1.5d2: %i32 = int_value 1 [concrete]
  129. // CHECK:STDOUT: }
  130. // CHECK:STDOUT:
  131. // CHECK:STDOUT: imports {
  132. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  133. // CHECK:STDOUT: .Int = %Core.Int
  134. // CHECK:STDOUT: .ImplicitAs = %Core.ImplicitAs
  135. // CHECK:STDOUT: import Core//prelude
  136. // CHECK:STDOUT: import Core//prelude/...
  137. // CHECK:STDOUT: }
  138. // CHECK:STDOUT: }
  139. // CHECK:STDOUT:
  140. // CHECK:STDOUT: file {
  141. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  142. // CHECK:STDOUT: .Core = imports.%Core
  143. // CHECK:STDOUT: .x = %x
  144. // CHECK:STDOUT: .Main = %Main.decl
  145. // CHECK:STDOUT: }
  146. // CHECK:STDOUT: %Core.import = import Core
  147. // CHECK:STDOUT: name_binding_decl {
  148. // CHECK:STDOUT: %x.patt: %i32 = binding_pattern x
  149. // CHECK:STDOUT: %.loc4_1: %i32 = var_pattern %x.patt
  150. // CHECK:STDOUT: }
  151. // CHECK:STDOUT: %x.var: ref %i32 = var x
  152. // CHECK:STDOUT: %.loc4_8: type = splice_block %i32 [concrete = constants.%i32] {
  153. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  154. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  155. // CHECK:STDOUT: }
  156. // CHECK:STDOUT: %x: ref %i32 = bind_name x, %x.var
  157. // CHECK:STDOUT: %Main.decl: %Main.type = fn_decl @Main [concrete = constants.%Main] {} {}
  158. // CHECK:STDOUT: }
  159. // CHECK:STDOUT:
  160. // CHECK:STDOUT: fn @Main() {
  161. // CHECK:STDOUT: !entry:
  162. // CHECK:STDOUT: name_binding_decl {
  163. // CHECK:STDOUT: %x.patt: %i32 = binding_pattern x
  164. // CHECK:STDOUT: %.loc7_3.1: %i32 = var_pattern %x.patt
  165. // CHECK:STDOUT: }
  166. // CHECK:STDOUT: %x.var: ref %i32 = var x
  167. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete = constants.%int_1.5b8]
  168. // CHECK:STDOUT: %impl.elem0: %.9c3 = impl_witness_access constants.%ImplicitAs.impl_witness.c75, element0 [concrete = constants.%Convert.956]
  169. // CHECK:STDOUT: %bound_method.loc7_3.1: <bound method> = bound_method %int_1, %impl.elem0 [concrete = constants.%Convert.bound.ab5]
  170. // CHECK:STDOUT: %specific_fn: <specific function> = specific_function %impl.elem0, @Convert.2(constants.%int_32) [concrete = constants.%Convert.specific_fn]
  171. // CHECK:STDOUT: %bound_method.loc7_3.2: <bound method> = bound_method %int_1, %specific_fn [concrete = constants.%bound_method.9a1]
  172. // CHECK:STDOUT: %int.convert_checked: init %i32 = call %bound_method.loc7_3.2(%int_1) [concrete = constants.%int_1.5d2]
  173. // CHECK:STDOUT: %.loc7_3.2: init %i32 = converted %int_1, %int.convert_checked [concrete = constants.%int_1.5d2]
  174. // CHECK:STDOUT: assign %x.var, %.loc7_3.2
  175. // CHECK:STDOUT: %.loc7_10: type = splice_block %i32.loc7 [concrete = constants.%i32] {
  176. // CHECK:STDOUT: %int_32.loc7: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  177. // CHECK:STDOUT: %i32.loc7: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  178. // CHECK:STDOUT: }
  179. // CHECK:STDOUT: %x: ref %i32 = bind_name x, %x.var
  180. // CHECK:STDOUT: name_binding_decl {
  181. // CHECK:STDOUT: %y.patt: %i32 = binding_pattern y
  182. // CHECK:STDOUT: %.loc9_3: %i32 = var_pattern %y.patt
  183. // CHECK:STDOUT: }
  184. // CHECK:STDOUT: %y.var: ref %i32 = var y
  185. // CHECK:STDOUT: %package.ref: <namespace> = name_ref package, package [concrete = package]
  186. // CHECK:STDOUT: %x.ref: ref %i32 = name_ref x, file.%x
  187. // CHECK:STDOUT: %.loc9_23: %i32 = bind_value %x.ref
  188. // CHECK:STDOUT: assign %y.var, %.loc9_23
  189. // CHECK:STDOUT: %.loc9_10: type = splice_block %i32.loc9 [concrete = constants.%i32] {
  190. // CHECK:STDOUT: %int_32.loc9: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  191. // CHECK:STDOUT: %i32.loc9: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  192. // CHECK:STDOUT: }
  193. // CHECK:STDOUT: %y: ref %i32 = bind_name y, %y.var
  194. // CHECK:STDOUT: return
  195. // CHECK:STDOUT: }
  196. // CHECK:STDOUT:
  197. // CHECK:STDOUT: fn @__global_init() {
  198. // CHECK:STDOUT: !entry:
  199. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [concrete = constants.%int_0.5c6]
  200. // CHECK:STDOUT: %impl.elem0: %.9c3 = impl_witness_access constants.%ImplicitAs.impl_witness.c75, element0 [concrete = constants.%Convert.956]
  201. // CHECK:STDOUT: %bound_method.loc4_1.1: <bound method> = bound_method %int_0, %impl.elem0 [concrete = constants.%Convert.bound.d04]
  202. // CHECK:STDOUT: %specific_fn: <specific function> = specific_function %impl.elem0, @Convert.2(constants.%int_32) [concrete = constants.%Convert.specific_fn]
  203. // CHECK:STDOUT: %bound_method.loc4_1.2: <bound method> = bound_method %int_0, %specific_fn [concrete = constants.%bound_method.b6e]
  204. // CHECK:STDOUT: %int.convert_checked: init %i32 = call %bound_method.loc4_1.2(%int_0) [concrete = constants.%int_0.6a9]
  205. // CHECK:STDOUT: %.loc4: init %i32 = converted %int_0, %int.convert_checked [concrete = constants.%int_0.6a9]
  206. // CHECK:STDOUT: assign file.%x.var, %.loc4
  207. // CHECK:STDOUT: return
  208. // CHECK:STDOUT: }
  209. // CHECK:STDOUT:
  210. // CHECK:STDOUT: --- namespace.carbon
  211. // CHECK:STDOUT:
  212. // CHECK:STDOUT: constants {
  213. // CHECK:STDOUT: %C: type = class_type @C [concrete]
  214. // CHECK:STDOUT: %Foo.type: type = fn_type @Foo [concrete]
  215. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  216. // CHECK:STDOUT: %Foo: %Foo.type = struct_value () [concrete]
  217. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
  218. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete]
  219. // CHECK:STDOUT: %Main.type: type = fn_type @Main [concrete]
  220. // CHECK:STDOUT: %Main: %Main.type = struct_value () [concrete]
  221. // CHECK:STDOUT: }
  222. // CHECK:STDOUT:
  223. // CHECK:STDOUT: imports {
  224. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  225. // CHECK:STDOUT: import Core//prelude
  226. // CHECK:STDOUT: import Core//prelude/...
  227. // CHECK:STDOUT: }
  228. // CHECK:STDOUT: }
  229. // CHECK:STDOUT:
  230. // CHECK:STDOUT: file {
  231. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  232. // CHECK:STDOUT: .Core = imports.%Core
  233. // CHECK:STDOUT: .NS = %NS
  234. // CHECK:STDOUT: .Main = %Main.decl
  235. // CHECK:STDOUT: }
  236. // CHECK:STDOUT: %Core.import = import Core
  237. // CHECK:STDOUT: %NS: <namespace> = namespace [concrete] {
  238. // CHECK:STDOUT: .C = %C.decl
  239. // CHECK:STDOUT: }
  240. // CHECK:STDOUT: %C.decl: type = class_decl @C [concrete = constants.%C] {} {}
  241. // CHECK:STDOUT: %Main.decl: %Main.type = fn_decl @Main [concrete = constants.%Main] {} {}
  242. // CHECK:STDOUT: }
  243. // CHECK:STDOUT:
  244. // CHECK:STDOUT: class @C {
  245. // CHECK:STDOUT: %Foo.decl: %Foo.type = fn_decl @Foo [concrete = constants.%Foo] {} {}
  246. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete = constants.%empty_struct_type]
  247. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete = constants.%complete_type]
  248. // CHECK:STDOUT: complete_type_witness = %complete_type
  249. // CHECK:STDOUT:
  250. // CHECK:STDOUT: !members:
  251. // CHECK:STDOUT: .Self = constants.%C
  252. // CHECK:STDOUT: .Foo = %Foo.decl
  253. // CHECK:STDOUT: }
  254. // CHECK:STDOUT:
  255. // CHECK:STDOUT: fn @Foo() {
  256. // CHECK:STDOUT: !entry:
  257. // CHECK:STDOUT: return
  258. // CHECK:STDOUT: }
  259. // CHECK:STDOUT:
  260. // CHECK:STDOUT: fn @Main() {
  261. // CHECK:STDOUT: !entry:
  262. // CHECK:STDOUT: %package.ref: <namespace> = name_ref package, package [concrete = package]
  263. // CHECK:STDOUT: %NS.ref: <namespace> = name_ref NS, file.%NS [concrete = file.%NS]
  264. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [concrete = constants.%C]
  265. // CHECK:STDOUT: %Foo.ref: %Foo.type = name_ref Foo, @C.%Foo.decl [concrete = constants.%Foo]
  266. // CHECK:STDOUT: %Foo.call: init %empty_tuple.type = call %Foo.ref()
  267. // CHECK:STDOUT: return
  268. // CHECK:STDOUT: }
  269. // CHECK:STDOUT: