Files
carbon-lang/toolchain/lower/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

34 lines
916 B
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 C {
var a: i32;
fn Get[self: C]() -> i32;
fn Set[addr self: C*](n: i32);
}
fn F(p: C*) {
let n: i32 = (*p).Get();
// TODO: Support `addr self`.
// (*p).Set(n);
}
// CHECK:STDOUT: ; ModuleID = 'method.carbon'
// CHECK:STDOUT: source_filename = "method.carbon"
// CHECK:STDOUT:
// CHECK:STDOUT: declare i32 @Get(ptr)
// CHECK:STDOUT:
// CHECK:STDOUT: declare void @Set(ptr, i32)
// CHECK:STDOUT:
// CHECK:STDOUT: define void @F(ptr %p) {
// CHECK:STDOUT: %Get = call i32 @Get(ptr %p)
// CHECK:STDOUT: %temp = alloca i32, align 4
// CHECK:STDOUT: store i32 %Get, ptr %temp, align 4
// CHECK:STDOUT: %1 = load i32, ptr %temp, align 4
// CHECK:STDOUT: ret void
// CHECK:STDOUT: }