syntax.carbon 8.2 KB

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