Files
carbon-lang/explorer/testdata/class/virtual_method_shadowed_attr.carbon
T
Adrien Leravat 8301258ef8 Explorer: support virtual class methods (#2462)
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
2023-01-02 12:37:49 -08:00

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;
}