Files
carbon-lang/toolchain/check/testdata/class/method.carbon
T
Richard SmithandJon Ross-Perkins 57f3c553b8 Support for type-checking and lowering method calls. (#3343)
Adds a `BoundMethod` SemIR node to represent an `x.F` bound method, with
a new builtin type `BoundMethodType`. Reorganized conversion of call
expression arguments to also check and convert a `self` parameter in the
implicit parameters list.

In passing, improved diagnostics and error recovery for bad call
expressions. We now build a `call` node with the appropriate type and
value category, but with invalid arguments, if the argument conversion
failed, and diagnose calls to non-callable expressions.

`addr self` methods don't work properly yet; the `addr` is ignored for
now.

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
2023-10-27 20:41:13 +00:00

61 lines
2.2 KiB
Plaintext

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
class Class {
fn F[self: Class]() -> i32;
var k: i32;
}
fn Class.F[self: Class]() -> i32 {
return self.k;
}
fn Call(c: Class) -> i32 {
// TODO: The sem-ir for this call doesn't distinguish the `self` argument from
// the explicit arguments.
return c.F();
}
// CHECK:STDOUT: file "method.carbon" {
// CHECK:STDOUT: class_declaration @Class, ()
// CHECK:STDOUT: %Class: type = class_type @Class
// CHECK:STDOUT: %.loc11: type = struct_type {.k: i32}
// CHECK:STDOUT: %F: <function> = fn_decl @F
// CHECK:STDOUT: %Call: <function> = fn_decl @Call
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class @Class {
// CHECK:STDOUT: %F: <function> = fn_decl @F
// CHECK:STDOUT: %.loc10_8.1: type = unbound_field_type Class, i32
// CHECK:STDOUT: %.loc10_8.2: <unbound field of class Class> = field "k", member0
// CHECK:STDOUT: %k: <unbound field of class Class> = bind_name "k", %.loc10_8.2
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .F = %F
// CHECK:STDOUT: .k = %k
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @F[%self: Class]() -> i32 {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %.loc7: type = ptr_type {.k: i32}
// CHECK:STDOUT: %self.ref: Class = name_reference "self", %self
// CHECK:STDOUT: %.loc14_14.1: ref i32 = class_field_access %self.ref, member0
// CHECK:STDOUT: %.loc14_14.2: i32 = bind_value %.loc14_14.1
// CHECK:STDOUT: return %.loc14_14.2
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @Call(%c: Class) -> i32 {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %c.ref: Class = name_reference "c", %c
// CHECK:STDOUT: %.loc20_11: <bound method> = bound_method %c.ref, @Class.%F
// CHECK:STDOUT: %.loc20_13.1: init i32 = call %.loc20_11(%c.ref)
// CHECK:STDOUT: %.loc20_13.2: ref i32 = temporary_storage
// CHECK:STDOUT: %.loc20_13.3: ref i32 = temporary %.loc20_13.2, %.loc20_13.1
// CHECK:STDOUT: %.loc20_13.4: i32 = bind_value %.loc20_13.3
// CHECK:STDOUT: return %.loc20_13.4
// CHECK:STDOUT: }