basic.carbon 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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: %int.make_type_32.loc21_15: init type = call constants.%Int32() [template = i32]
  68. // CHECK:STDOUT: %.loc21_15.1: type = value_of_initializer %int.make_type_32.loc21_15 [template = i32]
  69. // CHECK:STDOUT: %.loc21_15.2: type = converted %int.make_type_32.loc21_15, %.loc21_15.1 [template = i32]
  70. // CHECK:STDOUT: %n.loc21_12.1: i32 = param n, runtime_param0
  71. // CHECK:STDOUT: @G.%n: i32 = bind_name n, %n.loc21_12.1
  72. // CHECK:STDOUT: %int.make_type_32.loc21_23: init type = call constants.%Int32() [template = i32]
  73. // CHECK:STDOUT: %.loc21_23.1: type = value_of_initializer %int.make_type_32.loc21_23 [template = i32]
  74. // CHECK:STDOUT: %.loc21_23.2: type = converted %int.make_type_32.loc21_23, %.loc21_23.1 [template = i32]
  75. // CHECK:STDOUT: @G.%return: ref i32 = var <return slot>
  76. // CHECK:STDOUT: }
  77. // CHECK:STDOUT: %Run.decl: %Run.type = fn_decl @Run [template = constants.%Run] {
  78. // CHECK:STDOUT: %int.make_type_32.loc25: init type = call constants.%Int32() [template = i32]
  79. // CHECK:STDOUT: %.loc25_13.1: type = value_of_initializer %int.make_type_32.loc25 [template = i32]
  80. // CHECK:STDOUT: %.loc25_13.2: type = converted %int.make_type_32.loc25, %.loc25_13.1 [template = i32]
  81. // CHECK:STDOUT: @Run.%return: ref i32 = var <return slot>
  82. // CHECK:STDOUT: }
  83. // CHECK:STDOUT: }
  84. // CHECK:STDOUT:
  85. // CHECK:STDOUT: class @Class {
  86. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  87. // CHECK:STDOUT: %int.make_type_32.loc12_11: init type = call constants.%Int32() [template = i32]
  88. // CHECK:STDOUT: %.loc12_11.1: type = value_of_initializer %int.make_type_32.loc12_11 [template = i32]
  89. // CHECK:STDOUT: %.loc12_11.2: type = converted %int.make_type_32.loc12_11, %.loc12_11.1 [template = i32]
  90. // CHECK:STDOUT: %n.loc12_8.1: i32 = param n, runtime_param0
  91. // CHECK:STDOUT: %n.loc12_8.2: i32 = bind_name n, %n.loc12_8.1
  92. // CHECK:STDOUT: %int.make_type_32.loc12_19: init type = call constants.%Int32() [template = i32]
  93. // CHECK:STDOUT: %.loc12_19.1: type = value_of_initializer %int.make_type_32.loc12_19 [template = i32]
  94. // CHECK:STDOUT: %.loc12_19.2: type = converted %int.make_type_32.loc12_19, %.loc12_19.1 [template = i32]
  95. // CHECK:STDOUT: %return.var.loc12: ref i32 = var <return slot>
  96. // CHECK:STDOUT: }
  97. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  98. // CHECK:STDOUT: %int.make_type_32.loc16_11: init type = call constants.%Int32() [template = i32]
  99. // CHECK:STDOUT: %.loc16_11.1: type = value_of_initializer %int.make_type_32.loc16_11 [template = i32]
  100. // CHECK:STDOUT: %.loc16_11.2: type = converted %int.make_type_32.loc16_11, %.loc16_11.1 [template = i32]
  101. // CHECK:STDOUT: %n.loc16_8.1: i32 = param n, runtime_param0
  102. // CHECK:STDOUT: %n.loc16_8.2: i32 = bind_name n, %n.loc16_8.1
  103. // CHECK:STDOUT: %int.make_type_32.loc16_19: init type = call constants.%Int32() [template = i32]
  104. // CHECK:STDOUT: %.loc16_19.1: type = value_of_initializer %int.make_type_32.loc16_19 [template = i32]
  105. // CHECK:STDOUT: %.loc16_19.2: type = converted %int.make_type_32.loc16_19, %.loc16_19.1 [template = i32]
  106. // CHECK:STDOUT: %return.var.loc16: ref i32 = var <return slot>
  107. // CHECK:STDOUT: }
  108. // CHECK:STDOUT: %int.make_type_32.loc18: init type = call constants.%Int32() [template = i32]
  109. // CHECK:STDOUT: %.loc18_10.1: type = value_of_initializer %int.make_type_32.loc18 [template = i32]
  110. // CHECK:STDOUT: %.loc18_10.2: type = converted %int.make_type_32.loc18, %.loc18_10.1 [template = i32]
  111. // CHECK:STDOUT: %.loc18_8: %.2 = field_decl k, element0 [template]
  112. // CHECK:STDOUT: %.loc19: <witness> = complete_type_witness %.3 [template = constants.%.4]
  113. // CHECK:STDOUT:
  114. // CHECK:STDOUT: !members:
  115. // CHECK:STDOUT: .Self = constants.%Class
  116. // CHECK:STDOUT: .F = %F.decl
  117. // CHECK:STDOUT: .G = %G.decl
  118. // CHECK:STDOUT: .k = %.loc18_8
  119. // CHECK:STDOUT: }
  120. // CHECK:STDOUT:
  121. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  122. // CHECK:STDOUT:
  123. // CHECK:STDOUT: fn @F(@Class.%n.loc12_8.2: i32) -> i32 {
  124. // CHECK:STDOUT: !entry:
  125. // CHECK:STDOUT: %n.ref: i32 = name_ref n, @Class.%n.loc12_8.2
  126. // CHECK:STDOUT: return %n.ref
  127. // CHECK:STDOUT: }
  128. // CHECK:STDOUT:
  129. // CHECK:STDOUT: fn @G(%n: i32) -> i32 {
  130. // CHECK:STDOUT: !entry:
  131. // CHECK:STDOUT: %n.ref: i32 = name_ref n, %n
  132. // CHECK:STDOUT: return %n.ref
  133. // CHECK:STDOUT: }
  134. // CHECK:STDOUT:
  135. // CHECK:STDOUT: fn @Run() -> i32 {
  136. // CHECK:STDOUT: !entry:
  137. // CHECK:STDOUT: %Class.ref: type = name_ref Class, file.%Class.decl [template = constants.%Class]
  138. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, @Class.%F.decl [template = constants.%F]
  139. // CHECK:STDOUT: %.loc26_18: i32 = int_literal 4 [template = constants.%.6]
  140. // CHECK:STDOUT: %F.call: init i32 = call %F.ref(%.loc26_18)
  141. // CHECK:STDOUT: %.loc26_20.1: i32 = value_of_initializer %F.call
  142. // CHECK:STDOUT: %.loc26_20.2: i32 = converted %F.call, %.loc26_20.1
  143. // CHECK:STDOUT: return %.loc26_20.2
  144. // CHECK:STDOUT: }
  145. // CHECK:STDOUT: