identity.carbon 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/as/identity.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/as/identity.carbon
  10. // `expr as T` should behave the same as `expr` if `T` is the type of `expr`.
  11. class X {
  12. // ...
  13. }
  14. fn Value(n: X) {
  15. let m: X = n as X;
  16. }
  17. fn Reference(p: X*) {
  18. let q: X* = &(*p as X);
  19. }
  20. fn Make() -> X;
  21. fn Initializing() {
  22. var x: X = (Make() as X);
  23. }
  24. // CHECK:STDOUT: --- identity.carbon
  25. // CHECK:STDOUT:
  26. // CHECK:STDOUT: constants {
  27. // CHECK:STDOUT: %X: type = class_type @X [template]
  28. // CHECK:STDOUT: %.1: type = struct_type {} [template]
  29. // CHECK:STDOUT: %Value.type: type = fn_type @Value [template]
  30. // CHECK:STDOUT: %.2: type = tuple_type () [template]
  31. // CHECK:STDOUT: %Value: %Value.type = struct_value () [template]
  32. // CHECK:STDOUT: %.3: type = ptr_type %.1 [template]
  33. // CHECK:STDOUT: %.4: type = ptr_type %X [template]
  34. // CHECK:STDOUT: %Reference.type: type = fn_type @Reference [template]
  35. // CHECK:STDOUT: %Reference: %Reference.type = struct_value () [template]
  36. // CHECK:STDOUT: %Make.type: type = fn_type @Make [template]
  37. // CHECK:STDOUT: %Make: %Make.type = struct_value () [template]
  38. // CHECK:STDOUT: %Initializing.type: type = fn_type @Initializing [template]
  39. // CHECK:STDOUT: %Initializing: %Initializing.type = struct_value () [template]
  40. // CHECK:STDOUT: }
  41. // CHECK:STDOUT:
  42. // CHECK:STDOUT: file {
  43. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  44. // CHECK:STDOUT: .Core = %Core
  45. // CHECK:STDOUT: .X = %X.decl
  46. // CHECK:STDOUT: .Value = %Value.decl
  47. // CHECK:STDOUT: .Reference = %Reference.decl
  48. // CHECK:STDOUT: .Make = %Make.decl
  49. // CHECK:STDOUT: .Initializing = %Initializing.decl
  50. // CHECK:STDOUT: }
  51. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  52. // CHECK:STDOUT: %X.decl: type = class_decl @X [template = constants.%X] {}
  53. // CHECK:STDOUT: %Value.decl: %Value.type = fn_decl @Value [template = constants.%Value] {
  54. // CHECK:STDOUT: %X.ref.loc17: type = name_ref X, %X.decl [template = constants.%X]
  55. // CHECK:STDOUT: %n.loc17_10.1: %X = param n
  56. // CHECK:STDOUT: @Value.%n: %X = bind_name n, %n.loc17_10.1
  57. // CHECK:STDOUT: }
  58. // CHECK:STDOUT: %Reference.decl: %Reference.type = fn_decl @Reference [template = constants.%Reference] {
  59. // CHECK:STDOUT: %X.ref.loc21: type = name_ref X, %X.decl [template = constants.%X]
  60. // CHECK:STDOUT: %.loc21: type = ptr_type %X [template = constants.%.4]
  61. // CHECK:STDOUT: %p.loc21_14.1: %.4 = param p
  62. // CHECK:STDOUT: @Reference.%p: %.4 = bind_name p, %p.loc21_14.1
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT: %Make.decl: %Make.type = fn_decl @Make [template = constants.%Make] {
  65. // CHECK:STDOUT: %X.ref.loc25: type = name_ref X, %X.decl [template = constants.%X]
  66. // CHECK:STDOUT: @Make.%return: ref %X = var <return slot>
  67. // CHECK:STDOUT: }
  68. // CHECK:STDOUT: %Initializing.decl: %Initializing.type = fn_decl @Initializing [template = constants.%Initializing] {}
  69. // CHECK:STDOUT: }
  70. // CHECK:STDOUT:
  71. // CHECK:STDOUT: class @X {
  72. // CHECK:STDOUT: !members:
  73. // CHECK:STDOUT: .Self = constants.%X
  74. // CHECK:STDOUT: }
  75. // CHECK:STDOUT:
  76. // CHECK:STDOUT: fn @Value(%n: %X) {
  77. // CHECK:STDOUT: !entry:
  78. // CHECK:STDOUT: %X.ref.loc18_10: type = name_ref X, file.%X.decl [template = constants.%X]
  79. // CHECK:STDOUT: %n.ref: %X = name_ref n, %n
  80. // CHECK:STDOUT: %X.ref.loc18_19: type = name_ref X, file.%X.decl [template = constants.%X]
  81. // CHECK:STDOUT: %m: %X = bind_name m, %n.ref
  82. // CHECK:STDOUT: return
  83. // CHECK:STDOUT: }
  84. // CHECK:STDOUT:
  85. // CHECK:STDOUT: fn @Reference(%p: %.4) {
  86. // CHECK:STDOUT: !entry:
  87. // CHECK:STDOUT: %X.ref.loc22_10: type = name_ref X, file.%X.decl [template = constants.%X]
  88. // CHECK:STDOUT: %.loc22_11: type = ptr_type %X [template = constants.%.4]
  89. // CHECK:STDOUT: %p.ref: %.4 = name_ref p, %p
  90. // CHECK:STDOUT: %.loc22_17: ref %X = deref %p.ref
  91. // CHECK:STDOUT: %X.ref.loc22_23: type = name_ref X, file.%X.decl [template = constants.%X]
  92. // CHECK:STDOUT: %.loc22_15: %.4 = addr_of %.loc22_17
  93. // CHECK:STDOUT: %q: %.4 = bind_name q, %.loc22_15
  94. // CHECK:STDOUT: return
  95. // CHECK:STDOUT: }
  96. // CHECK:STDOUT:
  97. // CHECK:STDOUT: fn @Make() -> %X;
  98. // CHECK:STDOUT:
  99. // CHECK:STDOUT: fn @Initializing() {
  100. // CHECK:STDOUT: !entry:
  101. // CHECK:STDOUT: %X.ref.loc28_10: type = name_ref X, file.%X.decl [template = constants.%X]
  102. // CHECK:STDOUT: %x.var: ref %X = var x
  103. // CHECK:STDOUT: %x: ref %X = bind_name x, %x.var
  104. // CHECK:STDOUT: %Make.ref: %Make.type = name_ref Make, file.%Make.decl [template = constants.%Make]
  105. // CHECK:STDOUT: %.loc28: ref %X = splice_block %x.var {}
  106. // CHECK:STDOUT: %Make.call: init %X = call %Make.ref() to %.loc28
  107. // CHECK:STDOUT: %X.ref.loc28_25: type = name_ref X, file.%X.decl [template = constants.%X]
  108. // CHECK:STDOUT: assign %x.var, %Make.call
  109. // CHECK:STDOUT: return
  110. // CHECK:STDOUT: }
  111. // CHECK:STDOUT: