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