syntax.carbon 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 [template]
  34. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  35. // CHECK:STDOUT: %int_0.1: Core.IntLiteral = int_value 0 [template]
  36. // CHECK:STDOUT: %Convert.type.2: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  37. // CHECK:STDOUT: %Convert.type.10: type = fn_type @Convert.2, @impl.1(%int_32) [template]
  38. // CHECK:STDOUT: %Convert.10: %Convert.type.10 = struct_value () [template]
  39. // CHECK:STDOUT: %interface.19: <witness> = interface_witness (%Convert.10) [template]
  40. // CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %int_0.1, %Convert.10 [template]
  41. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.bound, @Convert.2(%int_32) [template]
  42. // CHECK:STDOUT: %int_0.2: %i32 = int_value 0 [template]
  43. // CHECK:STDOUT: }
  44. // CHECK:STDOUT:
  45. // CHECK:STDOUT: imports {
  46. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  47. // CHECK:STDOUT: .Int = %import_ref.1
  48. // CHECK:STDOUT: .ImplicitAs = %import_ref.5
  49. // CHECK:STDOUT: import Core//prelude
  50. // CHECK:STDOUT: import Core//prelude/...
  51. // CHECK:STDOUT: }
  52. // CHECK:STDOUT: }
  53. // CHECK:STDOUT:
  54. // CHECK:STDOUT: file {
  55. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  56. // CHECK:STDOUT: .Core = imports.%Core
  57. // CHECK:STDOUT: .x = %x
  58. // CHECK:STDOUT: .y = %y
  59. // CHECK:STDOUT: }
  60. // CHECK:STDOUT: %Core.import = import Core
  61. // CHECK:STDOUT: %x.var: ref %i32 = var x
  62. // CHECK:STDOUT: %x: ref %i32 = bind_name x, %x.var
  63. // CHECK:STDOUT: %y.var: ref %i32 = var y
  64. // CHECK:STDOUT: %y: ref %i32 = bind_name y, %y.var
  65. // CHECK:STDOUT: }
  66. // CHECK:STDOUT:
  67. // CHECK:STDOUT: fn @__global_init() {
  68. // CHECK:STDOUT: !entry:
  69. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [template = constants.%int_0.1]
  70. // CHECK:STDOUT: %impl.elem0: %Convert.type.2 = interface_witness_access constants.%interface.19, element0 [template = constants.%Convert.10]
  71. // CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %int_0, %impl.elem0 [template = constants.%Convert.bound]
  72. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.bound, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn]
  73. // CHECK:STDOUT: %int.convert_checked: init %i32 = call %Convert.specific_fn(%int_0) [template = constants.%int_0.2]
  74. // CHECK:STDOUT: %.loc4: init %i32 = converted %int_0, %int.convert_checked [template = constants.%int_0.2]
  75. // CHECK:STDOUT: assign file.%x.var, %.loc4
  76. // CHECK:STDOUT: %package.ref: <namespace> = name_ref package, package [template = package]
  77. // CHECK:STDOUT: %x.ref: ref %i32 = name_ref x, file.%x
  78. // CHECK:STDOUT: %.loc5: %i32 = bind_value %x.ref
  79. // CHECK:STDOUT: assign file.%y.var, %.loc5
  80. // CHECK:STDOUT: return
  81. // CHECK:STDOUT: }
  82. // CHECK:STDOUT:
  83. // CHECK:STDOUT: --- inside_fn.carbon
  84. // CHECK:STDOUT:
  85. // CHECK:STDOUT: constants {
  86. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  87. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  88. // CHECK:STDOUT: %int_0.1: Core.IntLiteral = int_value 0 [template]
  89. // CHECK:STDOUT: %Convert.type.2: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  90. // CHECK:STDOUT: %Convert.type.10: type = fn_type @Convert.2, @impl.1(%int_32) [template]
  91. // CHECK:STDOUT: %Convert.10: %Convert.type.10 = struct_value () [template]
  92. // CHECK:STDOUT: %interface.19: <witness> = interface_witness (%Convert.10) [template]
  93. // CHECK:STDOUT: %Convert.bound.1: <bound method> = bound_method %int_0.1, %Convert.10 [template]
  94. // CHECK:STDOUT: %Convert.specific_fn.1: <specific function> = specific_function %Convert.bound.1, @Convert.2(%int_32) [template]
  95. // CHECK:STDOUT: %int_0.2: %i32 = int_value 0 [template]
  96. // CHECK:STDOUT: %Main.type: type = fn_type @Main [template]
  97. // CHECK:STDOUT: %Main: %Main.type = struct_value () [template]
  98. // CHECK:STDOUT: %int_1.1: Core.IntLiteral = int_value 1 [template]
  99. // CHECK:STDOUT: %Convert.bound.2: <bound method> = bound_method %int_1.1, %Convert.10 [template]
  100. // CHECK:STDOUT: %Convert.specific_fn.2: <specific function> = specific_function %Convert.bound.2, @Convert.2(%int_32) [template]
  101. // CHECK:STDOUT: %int_1.2: %i32 = int_value 1 [template]
  102. // CHECK:STDOUT: }
  103. // CHECK:STDOUT:
  104. // CHECK:STDOUT: imports {
  105. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  106. // CHECK:STDOUT: .Int = %import_ref.1
  107. // CHECK:STDOUT: .ImplicitAs = %import_ref.5
  108. // CHECK:STDOUT: import Core//prelude
  109. // CHECK:STDOUT: import Core//prelude/...
  110. // CHECK:STDOUT: }
  111. // CHECK:STDOUT: }
  112. // CHECK:STDOUT:
  113. // CHECK:STDOUT: file {
  114. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  115. // CHECK:STDOUT: .Core = imports.%Core
  116. // CHECK:STDOUT: .x = %x
  117. // CHECK:STDOUT: .Main = %Main.decl
  118. // CHECK:STDOUT: }
  119. // CHECK:STDOUT: %Core.import = import Core
  120. // CHECK:STDOUT: %x.var: ref %i32 = var x
  121. // CHECK:STDOUT: %x: ref %i32 = bind_name x, %x.var
  122. // CHECK:STDOUT: %Main.decl: %Main.type = fn_decl @Main [template = constants.%Main] {} {}
  123. // CHECK:STDOUT: }
  124. // CHECK:STDOUT:
  125. // CHECK:STDOUT: fn @Main() {
  126. // CHECK:STDOUT: !entry:
  127. // CHECK:STDOUT: %x.var: ref %i32 = var x
  128. // CHECK:STDOUT: %x: ref %i32 = bind_name x, %x.var
  129. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [template = constants.%int_1.1]
  130. // CHECK:STDOUT: %impl.elem0: %Convert.type.2 = interface_witness_access constants.%interface.19, element0 [template = constants.%Convert.10]
  131. // CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %int_1, %impl.elem0 [template = constants.%Convert.bound.2]
  132. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.bound, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.2]
  133. // CHECK:STDOUT: %int.convert_checked: init %i32 = call %Convert.specific_fn(%int_1) [template = constants.%int_1.2]
  134. // CHECK:STDOUT: %.loc7: init %i32 = converted %int_1, %int.convert_checked [template = constants.%int_1.2]
  135. // CHECK:STDOUT: assign %x.var, %.loc7
  136. // CHECK:STDOUT: %y.var: ref %i32 = var y
  137. // CHECK:STDOUT: %y: ref %i32 = bind_name y, %y.var
  138. // CHECK:STDOUT: %package.ref: <namespace> = name_ref package, package [template = package]
  139. // CHECK:STDOUT: %x.ref: ref %i32 = name_ref x, file.%x
  140. // CHECK:STDOUT: %.loc9: %i32 = bind_value %x.ref
  141. // CHECK:STDOUT: assign %y.var, %.loc9
  142. // CHECK:STDOUT: return
  143. // CHECK:STDOUT: }
  144. // CHECK:STDOUT:
  145. // CHECK:STDOUT: fn @__global_init() {
  146. // CHECK:STDOUT: !entry:
  147. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [template = constants.%int_0.1]
  148. // CHECK:STDOUT: %impl.elem0: %Convert.type.2 = interface_witness_access constants.%interface.19, element0 [template = constants.%Convert.10]
  149. // CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %int_0, %impl.elem0 [template = constants.%Convert.bound.1]
  150. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.bound, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.1]
  151. // CHECK:STDOUT: %int.convert_checked: init %i32 = call %Convert.specific_fn(%int_0) [template = constants.%int_0.2]
  152. // CHECK:STDOUT: %.loc4: init %i32 = converted %int_0, %int.convert_checked [template = constants.%int_0.2]
  153. // CHECK:STDOUT: assign file.%x.var, %.loc4
  154. // CHECK:STDOUT: return
  155. // CHECK:STDOUT: }
  156. // CHECK:STDOUT:
  157. // CHECK:STDOUT: --- namespace.carbon
  158. // CHECK:STDOUT:
  159. // CHECK:STDOUT: constants {
  160. // CHECK:STDOUT: %C: type = class_type @C [template]
  161. // CHECK:STDOUT: %Foo.type: type = fn_type @Foo [template]
  162. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [template]
  163. // CHECK:STDOUT: %Foo: %Foo.type = struct_value () [template]
  164. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [template]
  165. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template]
  166. // CHECK:STDOUT: %Main.type: type = fn_type @Main [template]
  167. // CHECK:STDOUT: %Main: %Main.type = struct_value () [template]
  168. // CHECK:STDOUT: }
  169. // CHECK:STDOUT:
  170. // CHECK:STDOUT: imports {
  171. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  172. // CHECK:STDOUT: import Core//prelude
  173. // CHECK:STDOUT: import Core//prelude/...
  174. // CHECK:STDOUT: }
  175. // CHECK:STDOUT: }
  176. // CHECK:STDOUT:
  177. // CHECK:STDOUT: file {
  178. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  179. // CHECK:STDOUT: .Core = imports.%Core
  180. // CHECK:STDOUT: .NS = %NS
  181. // CHECK:STDOUT: .Main = %Main.decl
  182. // CHECK:STDOUT: }
  183. // CHECK:STDOUT: %Core.import = import Core
  184. // CHECK:STDOUT: %NS: <namespace> = namespace [template] {
  185. // CHECK:STDOUT: .C = %C.decl
  186. // CHECK:STDOUT: }
  187. // CHECK:STDOUT: %C.decl: type = class_decl @C [template = constants.%C] {} {}
  188. // CHECK:STDOUT: %Main.decl: %Main.type = fn_decl @Main [template = constants.%Main] {} {}
  189. // CHECK:STDOUT: }
  190. // CHECK:STDOUT:
  191. // CHECK:STDOUT: class @C {
  192. // CHECK:STDOUT: %Foo.decl: %Foo.type = fn_decl @Foo [template = constants.%Foo] {} {}
  193. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template = constants.%complete_type]
  194. // CHECK:STDOUT:
  195. // CHECK:STDOUT: !members:
  196. // CHECK:STDOUT: .Self = constants.%C
  197. // CHECK:STDOUT: .Foo = %Foo.decl
  198. // CHECK:STDOUT: complete_type_witness = %complete_type
  199. // CHECK:STDOUT: }
  200. // CHECK:STDOUT:
  201. // CHECK:STDOUT: fn @Foo() {
  202. // CHECK:STDOUT: !entry:
  203. // CHECK:STDOUT: return
  204. // CHECK:STDOUT: }
  205. // CHECK:STDOUT:
  206. // CHECK:STDOUT: fn @Main() {
  207. // CHECK:STDOUT: !entry:
  208. // CHECK:STDOUT: %package.ref: <namespace> = name_ref package, package [template = package]
  209. // CHECK:STDOUT: %NS.ref: <namespace> = name_ref NS, file.%NS [template = file.%NS]
  210. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [template = constants.%C]
  211. // CHECK:STDOUT: %Foo.ref: %Foo.type = name_ref Foo, @C.%Foo.decl [template = constants.%Foo]
  212. // CHECK:STDOUT: %Foo.call: init %empty_tuple.type = call %Foo.ref()
  213. // CHECK:STDOUT: return
  214. // CHECK:STDOUT: }
  215. // CHECK:STDOUT: