Files
carbon-lang/toolchain/check/testdata/class/static_method.carbon
T
Richard Smith 67d5446b03 Add Self to the name scope for a class. (#3736)
Rather than just adding `Self` to the lexical scope, add it to the
class's name scope so that it is visible in later lexical scopes for the
same class -- in particular, for out-of-line definitions of members.
Also switch some tests in `check/testdata/class` over to making
idiomatic use of `Self` both inside a class and out-of-line, now that it
works more consistently.

Note that this does not permit using `Class.Self`, but only because we
don't yet support keyword names after `.` at all. If that changed, one
could use `Class.Self` to redundantly refer to `Class`. Whether we allow
that is left to a future decision.
2024-02-29 21:33:47 +00:00

61 lines
2.1 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() -> i32;
}
fn Run() -> i32 {
var c: Class;
return c.F();
}
// CHECK:STDOUT: --- static_method.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %Class: type = class_type @Class [template]
// CHECK:STDOUT: %.1: type = struct_type {} [template]
// CHECK:STDOUT: %.2: type = tuple_type () [template]
// CHECK:STDOUT: %.3: type = ptr_type {} [template]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace {
// CHECK:STDOUT: .Class = %Class.decl
// CHECK:STDOUT: .Run = %Run
// CHECK:STDOUT: } [template]
// CHECK:STDOUT: %Class.decl = class_decl @Class {} [template = constants.%Class]
// CHECK:STDOUT: %Run: <function> = fn_decl @Run {
// CHECK:STDOUT: %return.var: ref i32 = var <return slot>
// CHECK:STDOUT: } [template]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class @Class {
// CHECK:STDOUT: %F: <function> = fn_decl @F {
// CHECK:STDOUT: %return.var: ref i32 = var <return slot>
// CHECK:STDOUT: } [template]
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = constants.%Class
// CHECK:STDOUT: .F = %F
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @F() -> i32;
// CHECK:STDOUT:
// CHECK:STDOUT: fn @Run() -> i32 {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %Class.ref: type = name_ref Class, file.%Class.decl [template = constants.%Class]
// CHECK:STDOUT: %c.var: ref Class = var c
// CHECK:STDOUT: %c: ref Class = bind_name c, %c.var
// CHECK:STDOUT: %c.ref: ref Class = name_ref c, %c
// CHECK:STDOUT: %F.ref: <function> = name_ref F, @Class.%F [template = @Class.%F]
// CHECK:STDOUT: %.loc13_13.1: init i32 = call %F.ref()
// CHECK:STDOUT: %.loc13_15: i32 = value_of_initializer %.loc13_13.1
// CHECK:STDOUT: %.loc13_13.2: i32 = converted %.loc13_13.1, %.loc13_15
// CHECK:STDOUT: return %.loc13_13.2
// CHECK:STDOUT: }
// CHECK:STDOUT: