syntax.carbon 8.4 KB

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