identity.carbon 4.8 KB

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