basic.carbon 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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/class/basic.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/basic.carbon
  10. class Class {
  11. fn F(n: i32) -> i32 {
  12. return n;
  13. }
  14. fn G(n: i32) -> i32;
  15. var k: i32;
  16. }
  17. fn Class.G(n: i32) -> i32 {
  18. return n;
  19. }
  20. fn Run() -> i32 {
  21. return Class.F(4);
  22. }
  23. // CHECK:STDOUT: --- basic.carbon
  24. // CHECK:STDOUT:
  25. // CHECK:STDOUT: constants {
  26. // CHECK:STDOUT: %Class: type = class_type @Class [template]
  27. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  28. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  29. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  30. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  31. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  32. // CHECK:STDOUT: %G.type: type = fn_type @G [template]
  33. // CHECK:STDOUT: %G: %G.type = struct_value () [template]
  34. // CHECK:STDOUT: %.2: type = unbound_element_type %Class, i32 [template]
  35. // CHECK:STDOUT: %.3: type = struct_type {.k: i32} [template]
  36. // CHECK:STDOUT: %.4: <witness> = complete_type_witness %.3 [template]
  37. // CHECK:STDOUT: %Run.type: type = fn_type @Run [template]
  38. // CHECK:STDOUT: %Run: %Run.type = struct_value () [template]
  39. // CHECK:STDOUT: %.5: type = ptr_type %.3 [template]
  40. // CHECK:STDOUT: %.6: i32 = int_literal 4 [template]
  41. // CHECK:STDOUT: }
  42. // CHECK:STDOUT:
  43. // CHECK:STDOUT: imports {
  44. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  45. // CHECK:STDOUT: .Int32 = %import_ref
  46. // CHECK:STDOUT: import Core//prelude
  47. // CHECK:STDOUT: import Core//prelude/operators
  48. // CHECK:STDOUT: import Core//prelude/types
  49. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  50. // CHECK:STDOUT: import Core//prelude/operators/as
  51. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  52. // CHECK:STDOUT: import Core//prelude/operators/comparison
  53. // CHECK:STDOUT: import Core//prelude/types/bool
  54. // CHECK:STDOUT: }
  55. // CHECK:STDOUT: %import_ref: %Int32.type = import_ref Core//prelude/types, inst+4, loaded [template = constants.%Int32]
  56. // CHECK:STDOUT: }
  57. // CHECK:STDOUT:
  58. // CHECK:STDOUT: file {
  59. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  60. // CHECK:STDOUT: .Core = imports.%Core
  61. // CHECK:STDOUT: .Class = %Class.decl
  62. // CHECK:STDOUT: .Run = %Run.decl
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT: %Core.import = import Core
  65. // CHECK:STDOUT: %Class.decl: type = class_decl @Class [template = constants.%Class] {} {}
  66. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  67. // CHECK:STDOUT: %n.patt: i32 = binding_pattern n
  68. // CHECK:STDOUT: %n.param_patt: i32 = param_pattern %n.patt, runtime_param0
  69. // CHECK:STDOUT: } {
  70. // CHECK:STDOUT: %int.make_type_32.loc21_15: init type = call constants.%Int32() [template = i32]
  71. // CHECK:STDOUT: %.loc21_15.1: type = value_of_initializer %int.make_type_32.loc21_15 [template = i32]
  72. // CHECK:STDOUT: %.loc21_15.2: type = converted %int.make_type_32.loc21_15, %.loc21_15.1 [template = i32]
  73. // CHECK:STDOUT: %int.make_type_32.loc21_23: init type = call constants.%Int32() [template = i32]
  74. // CHECK:STDOUT: %.loc21_23.1: type = value_of_initializer %int.make_type_32.loc21_23 [template = i32]
  75. // CHECK:STDOUT: %.loc21_23.2: type = converted %int.make_type_32.loc21_23, %.loc21_23.1 [template = i32]
  76. // CHECK:STDOUT: %return: ref i32 = var <return slot>
  77. // CHECK:STDOUT: %param.loc21: i32 = param runtime_param0
  78. // CHECK:STDOUT: %n.loc21: i32 = bind_name n, %param.loc21
  79. // CHECK:STDOUT: }
  80. // CHECK:STDOUT: %Run.decl: %Run.type = fn_decl @Run [template = constants.%Run] {} {
  81. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  82. // CHECK:STDOUT: %.loc25_13.1: type = value_of_initializer %int.make_type_32 [template = i32]
  83. // CHECK:STDOUT: %.loc25_13.2: type = converted %int.make_type_32, %.loc25_13.1 [template = i32]
  84. // CHECK:STDOUT: %return: ref i32 = var <return slot>
  85. // CHECK:STDOUT: }
  86. // CHECK:STDOUT: }
  87. // CHECK:STDOUT:
  88. // CHECK:STDOUT: class @Class {
  89. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  90. // CHECK:STDOUT: %n.patt: i32 = binding_pattern n
  91. // CHECK:STDOUT: %n.param_patt: i32 = param_pattern %n.patt, runtime_param0
  92. // CHECK:STDOUT: } {
  93. // CHECK:STDOUT: %int.make_type_32.loc12_11: init type = call constants.%Int32() [template = i32]
  94. // CHECK:STDOUT: %.loc12_11.1: type = value_of_initializer %int.make_type_32.loc12_11 [template = i32]
  95. // CHECK:STDOUT: %.loc12_11.2: type = converted %int.make_type_32.loc12_11, %.loc12_11.1 [template = i32]
  96. // CHECK:STDOUT: %int.make_type_32.loc12_19: init type = call constants.%Int32() [template = i32]
  97. // CHECK:STDOUT: %.loc12_19.1: type = value_of_initializer %int.make_type_32.loc12_19 [template = i32]
  98. // CHECK:STDOUT: %.loc12_19.2: type = converted %int.make_type_32.loc12_19, %.loc12_19.1 [template = i32]
  99. // CHECK:STDOUT: %return: ref i32 = var <return slot>
  100. // CHECK:STDOUT: %param: i32 = param runtime_param0
  101. // CHECK:STDOUT: %n: i32 = bind_name n, %param
  102. // CHECK:STDOUT: }
  103. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  104. // CHECK:STDOUT: %n.patt: i32 = binding_pattern n
  105. // CHECK:STDOUT: %n.param_patt: i32 = param_pattern %n.patt, runtime_param0
  106. // CHECK:STDOUT: } {
  107. // CHECK:STDOUT: %int.make_type_32.loc16_11: init type = call constants.%Int32() [template = i32]
  108. // CHECK:STDOUT: %.loc16_11.1: type = value_of_initializer %int.make_type_32.loc16_11 [template = i32]
  109. // CHECK:STDOUT: %.loc16_11.2: type = converted %int.make_type_32.loc16_11, %.loc16_11.1 [template = i32]
  110. // CHECK:STDOUT: %int.make_type_32.loc16_19: init type = call constants.%Int32() [template = i32]
  111. // CHECK:STDOUT: %.loc16_19.1: type = value_of_initializer %int.make_type_32.loc16_19 [template = i32]
  112. // CHECK:STDOUT: %.loc16_19.2: type = converted %int.make_type_32.loc16_19, %.loc16_19.1 [template = i32]
  113. // CHECK:STDOUT: %return.var.loc16: ref i32 = var <return slot>
  114. // CHECK:STDOUT: %param.loc16: i32 = param runtime_param0
  115. // CHECK:STDOUT: %n.loc16: i32 = bind_name n, %param.loc16
  116. // CHECK:STDOUT: }
  117. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  118. // CHECK:STDOUT: %.loc18_10.1: type = value_of_initializer %int.make_type_32 [template = i32]
  119. // CHECK:STDOUT: %.loc18_10.2: type = converted %int.make_type_32, %.loc18_10.1 [template = i32]
  120. // CHECK:STDOUT: %.loc18_8: %.2 = field_decl k, element0 [template]
  121. // CHECK:STDOUT: %.loc19: <witness> = complete_type_witness %.3 [template = constants.%.4]
  122. // CHECK:STDOUT:
  123. // CHECK:STDOUT: !members:
  124. // CHECK:STDOUT: .Self = constants.%Class
  125. // CHECK:STDOUT: .F = %F.decl
  126. // CHECK:STDOUT: .G = %G.decl
  127. // CHECK:STDOUT: .k = %.loc18_8
  128. // CHECK:STDOUT: }
  129. // CHECK:STDOUT:
  130. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  131. // CHECK:STDOUT:
  132. // CHECK:STDOUT: fn @F(%n.param_patt: i32) -> i32 {
  133. // CHECK:STDOUT: !entry:
  134. // CHECK:STDOUT: %n.ref: i32 = name_ref n, %n
  135. // CHECK:STDOUT: return %n.ref
  136. // CHECK:STDOUT: }
  137. // CHECK:STDOUT:
  138. // CHECK:STDOUT: fn @G(%n.param_patt: i32) -> i32 {
  139. // CHECK:STDOUT: !entry:
  140. // CHECK:STDOUT: %n.ref: i32 = name_ref n, %n.loc21
  141. // CHECK:STDOUT: return %n.ref
  142. // CHECK:STDOUT: }
  143. // CHECK:STDOUT:
  144. // CHECK:STDOUT: fn @Run() -> i32 {
  145. // CHECK:STDOUT: !entry:
  146. // CHECK:STDOUT: %Class.ref: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  147. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, @Class.%F.decl [template = constants.%F]
  148. // CHECK:STDOUT: %.loc26_18: i32 = int_literal 4 [template = constants.%.6]
  149. // CHECK:STDOUT: %F.call: init i32 = call %F.ref(%.loc26_18)
  150. // CHECK:STDOUT: %.loc26_20.1: i32 = value_of_initializer %F.call
  151. // CHECK:STDOUT: %.loc26_20.2: i32 = converted %F.call, %.loc26_20.1
  152. // CHECK:STDOUT: return %.loc26_20.2
  153. // CHECK:STDOUT: }
  154. // CHECK:STDOUT: