mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 22:02:52 +01:00
Features: * Add `virtual` virtual override keyword for functions * Support `virtual` class methods using dynamic dispatch Changes: * Add `vtable` in `NominalClassType`, * Add `NominalClassValue**` in class values pointing to descendant-most class * Resolve virtual methods during member lookup Limitations: * Does not include yet `impl`, `abstract` virtual override keywords, or the complete logic for virtual function declaration Depends on #2460 Relates to #1881 Relates to #2493
40 lines
918 B
Plaintext
40 lines
918 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
|
|
// RUN: %{explorer-run}
|
|
// RUN: %{explorer-run-trace}
|
|
// CHECK:STDOUT: c.Foo(): 1
|
|
// CHECK:STDOUT: d.Foo(): 2
|
|
// CHECK:STDOUT: (*cp).Foo(): 2
|
|
// CHECK:STDOUT: result: 0
|
|
|
|
package ExplorerTest api;
|
|
|
|
base class C {
|
|
var value: i32;
|
|
virtual fn Foo[self: Self]() -> i32 {
|
|
return self.value;
|
|
}
|
|
}
|
|
|
|
base class D extends C {
|
|
var value: i32;
|
|
// TODO: This should instead use `impl` when supported
|
|
virtual fn Foo[self: Self]() -> i32 {
|
|
return self.value;
|
|
}
|
|
}
|
|
|
|
fn Main() -> i32 {
|
|
var c: C = {.value = 1};
|
|
Print("c.Foo(): {0}", c.Foo());
|
|
var d: D = {.value = 2, .base = {.value = 1}};
|
|
Print("d.Foo(): {0}", d.Foo());
|
|
var cp: C* = &d;
|
|
Print("(*cp).Foo(): {0}", (*cp).Foo());
|
|
|
|
return 0;
|
|
}
|