mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 21:30:12 +01:00
Explorer: Support class extension (#1946)
Support class extension with `extends` Relates to #1881 Add support for: * Class extension with `extends` * Using base class methods and members
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
// 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: Class C, call 0
|
||||
// CHECK:STDOUT: Class D, call 0
|
||||
// CHECK:STDOUT: Class C, call 1
|
||||
// CHECK:STDOUT: Class C, call 2
|
||||
// CHECK:STDOUT: Class D, call 2
|
||||
// CHECK:STDOUT: result: 0
|
||||
|
||||
package ExplorerTest api;
|
||||
|
||||
base class C {
|
||||
fn FunctionC(var i: i32) {
|
||||
Print("Class C, call {0}", i);
|
||||
}
|
||||
}
|
||||
|
||||
class D extends C {
|
||||
fn FunctionD(var i: i32) {
|
||||
C.FunctionC(i);
|
||||
Print("Class D, call {0}", i);
|
||||
}
|
||||
}
|
||||
|
||||
fn Main() -> i32 {
|
||||
// Calling function from class type
|
||||
D.FunctionD(0);
|
||||
// Note: D.FunctionC(0); is not accessible
|
||||
|
||||
// Calling function from class object
|
||||
var d: D = {};
|
||||
d.FunctionC(1);
|
||||
d.FunctionD(2);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
// 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: Class 1
|
||||
// CHECK:STDOUT: Class 2
|
||||
// CHECK:STDOUT: result: 0
|
||||
|
||||
package ExplorerTest api;
|
||||
|
||||
base class C {
|
||||
fn BasePrint(v: i32) {
|
||||
Print("Class {0}", v);
|
||||
}
|
||||
fn Method1[me:Self]() {
|
||||
me.BasePrint(me.value_c);
|
||||
}
|
||||
|
||||
var value_c: i32;
|
||||
}
|
||||
|
||||
class D extends C {
|
||||
fn Method2[me:Self]() {
|
||||
me.BasePrint(me.value_d);
|
||||
}
|
||||
|
||||
var value_d: i32;
|
||||
}
|
||||
|
||||
fn Main() -> i32 {
|
||||
var d: D = {.value_c = 1, .value_d = 2};
|
||||
d.Method1();
|
||||
d.Method2();
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// 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: result: 0
|
||||
|
||||
package ExplorerTest api;
|
||||
|
||||
base class A {
|
||||
fn FunctionA() {}
|
||||
var var_a: i32;
|
||||
}
|
||||
|
||||
base class B extends A {
|
||||
fn FunctionB() {}
|
||||
var var_b: i32;
|
||||
}
|
||||
|
||||
class C extends B {
|
||||
fn FunctionC() {}
|
||||
var var_c: i32;
|
||||
}
|
||||
|
||||
fn Main() -> i32 {
|
||||
var c: C = {.var_a=1, .var_b=2, .var_c=3};
|
||||
c.FunctionA();
|
||||
c.FunctionB();
|
||||
c.FunctionC();
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
// 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: Class D
|
||||
// CHECK:STDOUT: d.value = 2
|
||||
// CHECK:STDOUT: d.GetValue() = 2
|
||||
// CHECK:STDOUT: result: 0
|
||||
|
||||
package ExplorerTest api;
|
||||
|
||||
base class C {
|
||||
fn Method1() {
|
||||
Print("Class C");
|
||||
}
|
||||
fn GetValue[me: Self]() -> i32 {
|
||||
return me.value;
|
||||
}
|
||||
var value: i32;
|
||||
}
|
||||
|
||||
class D extends C {
|
||||
fn Method1() {
|
||||
Print("Class D");
|
||||
}
|
||||
var value: i32;
|
||||
}
|
||||
|
||||
fn Main() -> i32 {
|
||||
// Initialize derived value first, base value second
|
||||
var d: D = {.value = 2, .value = 1};
|
||||
d.Method1();
|
||||
|
||||
Print("d.value = {0}", d.value);
|
||||
Print("d.GetValue() = {0}", d.GetValue());
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// 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: Read var_c=1
|
||||
// CHECK:STDOUT: Read var_d=2
|
||||
// CHECK:STDOUT: Assign var_c=3
|
||||
// CHECK:STDOUT: Assign var_d=4
|
||||
// CHECK:STDOUT: result: 0
|
||||
|
||||
package ExplorerTest api;
|
||||
|
||||
base class C {
|
||||
var var_c: i32;
|
||||
}
|
||||
|
||||
class D extends C {
|
||||
var var_d: i32;
|
||||
}
|
||||
|
||||
fn Main() -> i32 {
|
||||
// Initialization
|
||||
var d: D = {.var_c= 1, .var_d= 2};
|
||||
|
||||
// Read
|
||||
Print("Read var_c={0}", d.var_c);
|
||||
Print("Read var_d={0}", d.var_d);
|
||||
|
||||
// Assignment
|
||||
d.var_c = 3;
|
||||
d.var_d = 4;
|
||||
Print("Assign var_c={0}", d.var_c);
|
||||
Print("Assign var_d={0}", d.var_d);
|
||||
|
||||
return 0;
|
||||
}
|
||||
+1
-1
@@ -10,7 +10,7 @@ package ExplorerTest api;
|
||||
|
||||
abstract class C {
|
||||
fn F() {}
|
||||
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/class/fail_abstract_class.carbon:[[@LINE+1]]: Class prefixes `base` and `abstract` are not supported yet
|
||||
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/class/fail_abstract_class.carbon:[[@LINE+1]]: Class prefix `abstract` is not supported yet
|
||||
}
|
||||
|
||||
fn Main() -> i32 {
|
||||
|
||||
+4
-4
@@ -8,13 +8,13 @@
|
||||
|
||||
package ExplorerTest api;
|
||||
|
||||
class A {
|
||||
class C {
|
||||
fn F() {}
|
||||
}
|
||||
|
||||
class B extends A {
|
||||
fn F() {}
|
||||
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/class/fail_class_extends.carbon:[[@LINE+1]]: Class extension with `extends` is not supported yet
|
||||
class D extends C {
|
||||
fn G() {}
|
||||
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/class/fail_extends_final_class.carbon:[[@LINE+1]]: Base class `C` is `final` and cannot inherited. Add the `base` or `abstract` class prefix to `C` to allow it to be inherited
|
||||
}
|
||||
|
||||
fn Main() -> i32 {
|
||||
+4
-3
@@ -8,9 +8,10 @@
|
||||
|
||||
package ExplorerTest api;
|
||||
|
||||
base class C {
|
||||
fn F() {}
|
||||
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/class/fail_base_class.carbon:[[@LINE+1]]: Class prefixes `base` and `abstract` are not supported yet
|
||||
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/class/fail_extends_non_class.carbon:[[@LINE+1]]: Expected a type, but got 3
|
||||
class C extends 3 {
|
||||
var x: i32;
|
||||
var y: i32;
|
||||
}
|
||||
|
||||
fn Main() -> i32 {
|
||||
Reference in New Issue
Block a user