Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions tslang/docs/classes-as-values-design.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,12 @@ between the value and that tuple, or to the value of a base class, lowers to not
by name. A class without a constructor of its own, whose base is a library's, takes the base's through
that pointer. Through the value, a library class's static field is read where the library's vtable slot
points: the field itself, not the importer's pointer to it.
- **Not yet:** a @value class or a @cpp class can't be a value.
- **A @cpp class implemented here** (its methods have bodies, `isCppVTableImplementation`) is a value: its
vtable (C++'s layout), `.new` and constructor are as any class's. It has no rtti, so its identity, the
value's last part, is its vtable, which each such class has of its own. A @cpp class has no statics, so
nothing but `new` and `==` is reached through its value. One whose objects C++ makes has no `.new` or
constructor here, and can't be a value (an error).
- **Not yet:** a @value class can't be a value.

**Using one:**
- **`new c(args)`** (`NewClassInstanceFromClassValue`): the arguments are cast to `A`'s constructor's
Expand All @@ -142,8 +147,8 @@ through it is a fresh owned result.

**Local classes (option C).** A method, constructor or accessor of a class declared in a function that reads
one of the function's locals is an error naming the variable, in place of "Expected 1 arguments, but got
0". A field initializer of a class without a constructor still fails as it did ("value defined outside the
region"): the initializers reach the implicit constructor after captures are found.
0". So is a field initializer's: the discovery pass of a constructor, implicit or not, is given the field
initializers (`generatedStatements`) with its body, so what they read is found as the body's reads are.

**Tests:**
- **`00class_values`:** a factory, an array, a return, a field, a class without a constructor, a subclass
Expand All @@ -152,4 +157,7 @@ region"): the initializers reach the implicit constructor after captures are fou
gc, rc, none and own.
- **`import_class_values`** + **`export_class_values`:** a library's classes, and one of this module extending
one, as values both ways; linked, -shared and -jit -shared, under every memory model.
- **`class_value_constructor_error`** and **`class_local_capture_error`:** the errors.
- **`00cpp_class_values`:** @cpp classes implemented here, made through a value and called through their
C++ slots (one by hand, as C++ would), in an array, compared; under gc, rc, none and own.
- **`class_value_constructor_error`**, **`class_local_capture_error`**, **`class_field_capture_error`** and
**`cpp_class_value_error`:** the errors.
20 changes: 17 additions & 3 deletions tslang/lib/TypeScript/MLIRGenClasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4331,12 +4331,21 @@ genContext);
return mlir::failure();
}

if (classInfo->isValue || classInfo->isCppVTable)
if (classInfo->isValue)
{
emitError(location) << "class '" << classInfo->fullName << "' can't be used as a value yet";
return mlir::failure();
}

// a @cpp class implemented here has its vtable (C++'s layout), `.new` and constructor; one made
// by C++ only has none of them here
if (classInfo->isCppVTable && !classInfo->isCppVTableImplementation)
{
emitError(location) << "the @cpp class '" << classInfo->fullName
<< "' can't be used as a value: its objects are made by C++";
return mlir::failure();
}

auto opaqueType = getOpaqueType();
auto nullOpaque = [&]() -> mlir::Value {
return builder.create<mlir_ts::CastOp>(location, opaqueType, builder.create<mlir_ts::NullOp>(location, getNullType()));
Expand Down Expand Up @@ -4416,10 +4425,15 @@ genContext);
auto newFunc = functionAddress(classInfo->isAbstract ? nullptr : classInfo->findMethod(NEW_METHOD_NAME));
auto constructorFunc = functionAddress(classInfo->getHasConstructor() ? classInfo->findMethod(CONSTRUCTOR_NAME) : nullptr);

// the rtti: every class has one, and no two the same
// the rtti: every class has one, and no two the same - but a @cpp class, whose identity is its
// vtable, which each class implementing C++ methods has of its own
mlir::Value rtti;
auto rttiName = concat(classInfo->fullName, RTTI_NAME);
if (classInfo->isDynamicImport)
if (classInfo->isCppVTable)
{
rtti = vtable;
}
else if (classInfo->isDynamicImport)
{
rtti = loadedOpaque(rttiName);
}
Expand Down
8 changes: 8 additions & 0 deletions tslang/lib/TypeScript/MLIRGenFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,14 @@ namespace mlirgen
genContextWithPassResult.typeAliasMap = genContext.typeAliasMap;
genContextWithPassResult.typeParamsWithArgs = genContext.typeParamsWithArgs;
genContextWithPassResult.postponedMessages = genContext.postponedMessages;
// a constructor of a class declared in a function: its field initializers
// (generateConstructorStatements) are discovered with its body, so what they read of the
// function is found as the body's reads are. This pass drains its copy; the real one
// still has them. (A class at the top level has nothing of a function to read.)
if (genContext.funcOp)
{
genContextWithPassResult.generatedStatements = genContext.generatedStatements;
}

registerNamespace(funcProto->getNameWithoutNamespace(), true);

Expand Down
4 changes: 4 additions & 0 deletions tslang/lib/TypeScript/MLIRGenImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,10 @@ class MLIRGenImpl
// NOTE: upward mailbox into caller context (process-once drain) - see GenContext
const_cast<GenContext&>(genContext).generatedStatements.clear();

// a constructor's discovery pass generated them too (they are the field initializers):
// cleared as the body's statements are below
clearState(generatedStatements);

// auto generated code
for (auto statement : generatedStatements)
{
Expand Down
15 changes: 15 additions & 0 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1691,6 +1691,16 @@ foreach(class_value_mm rc none own)
# ...and of a shared library's classes
tslang_add_test(NAME test-jit-shared-export-import-class-values-${class_value_mm} COMMAND test-runner -jit -shared -mm=${class_value_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/import_class_values.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_class_values.ts")
endforeach()
# ...and of @cpp classes implemented here (their vtables C++'s), under every memory model
tslang_add_test(NAME test-compile-00-cpp-class-values COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00cpp_class_values.ts")
foreach(class_value_mm gc rc none own)
tslang_add_test(NAME test-jit-00-cpp-class-values-${class_value_mm} COMMAND test-runner -jit -mm=${class_value_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/00cpp_class_values.ts")
endforeach()
# ...a @cpp class C++ makes can't be one
add_test(NAME test-compile-cpp-class-value-error
COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/cpp_class_value_error.ts")
set_tests_properties(test-compile-cpp-class-value-error PROPERTIES PASS_REGULAR_EXPRESSION "the @cpp class 'Native' can't be used as a value: its objects are made by C\\+\\+"
FAIL_REGULAR_EXPRESSION "Stack dump|Assertion failed")
# ...a class whose constructor takes other arguments than its base's is no value of the base's type
add_test(NAME test-compile-class-value-constructor-error
COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/class_value_constructor_error.ts")
Expand All @@ -1701,6 +1711,11 @@ add_test(NAME test-compile-class-local-capture-error
COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/class_local_capture_error.ts")
set_tests_properties(test-compile-class-local-capture-error PROPERTIES PASS_REGULAR_EXPRESSION "a member of class 'Counter' reads 'start', a local of the function the class is declared in"
FAIL_REGULAR_EXPRESSION "Stack dump|Assertion failed")
# ...a field initializer's too, in a class without a constructor of its own
add_test(NAME test-compile-class-field-capture-error
COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/class_field_capture_error.ts")
set_tests_properties(test-compile-class-field-capture-error PROPERTIES PASS_REGULAR_EXPRESSION "a member of class 'Counter' reads 'start', a local of the function the class is declared in"
FAIL_REGULAR_EXPRESSION "Stack dump|Assertion failed|defined outside the region")
# @keeps and @borrows across the boundary: the importer reads them from __decls (-mm=own), and the
# other models ignore them
foreach(markers_mm gc rc own)
Expand Down
82 changes: 82 additions & 0 deletions tslang/test/tester/tests/00cpp_class_values.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// A @cpp class implemented here (its methods have bodies) as a value (`typeof A`): `new k(n)` through
// it makes an object of the class the value holds, whose vtable is laid out as C++'s, and the value's
// identity is that vtable (a @cpp class has no rtti). A @cpp class made by C++ only can't be a value.

// a C++ interface
@cpp
class Shape {
@slot(0) area(): s32;
@slot(1) scaled(k: s32): s32;
}

@cpp
class Square extends Shape {
constructor(public side: s32) {
super();
}

area(): s32 {
return this.side * this.side;
}

scaled(k: s32): s32 {
return k * this.area();
}
}

@cpp
class Triple extends Square {
area(): s32 {
return 3 * this.side;
}
}

@cpp
class Unit extends Shape {
area(): s32 {
return 1;
}

scaled(k: s32): s32 {
return k;
}
}

// as C++ would call it: slot 0 of the object's vtable, the object first
type ObjectHead = [vtable: Ref<Opaque>];

function callSlot0(object: Opaque): s32 {
const vtable = Deref(<Ref<ObjectHead>>object).vtable;
const slot0 = <(self: Opaque) => s32>Deref(vtable);
return slot0(object);
}

function make(k: typeof Square, side: s32): Shape {
return new k(side);
}

function main() {
const a = make(Square, 4);
assert(a.area() == 16 && a.scaled(2) == 32, "made through the value, called through its slots");
const b = make(Triple, 4);
assert(b.area() == 12 && b.scaled(2) == 24, "a subclass's slots");
assert(callSlot0(<Opaque>b) == 12, "its vtable is C++'s");

const kinds: (typeof Square)[] = [Square, Triple];
let total: s32 = 0;
for (const k of kinds) {
total += new k(2).area();
}

assert(total == 4 + 6, "an array of classes");

const s: typeof Square = Square;
const t: typeof Square = Triple;
const u: typeof Square = Square;
assert(s == u && s != t && t == Triple, "equality: the vtable");

const one: typeof Shape = Unit;
assert(new one().scaled(7) == 7, "a C++ interface's value");

print("done.");
}
14 changes: 14 additions & 0 deletions tslang/test/tester/tests/class_field_capture_error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// A field initializer of a class declared in a function, without a constructor of its own, that reads
// one of the function's locals: the class can't capture yet, and the error names the variable.
function make(start: number) {
class Counter {
value = start;
}

return new Counter().value;
}

function main() {
print(make(1));
print("done.");
}
10 changes: 10 additions & 0 deletions tslang/test/tester/tests/cpp_class_value_error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// A @cpp class whose objects C++ makes has no `.new` or constructor here: it can't be a value.
@cpp
class Native {
@slot(0) first(): s32;
}

function main() {
const k: typeof Native = Native;
print("done.");
}
4 changes: 3 additions & 1 deletion tslang/test/tester/verify-ownership.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ set(not_compilable_alone
import_value_types_structural.ts
# errors by design (test-compile-class-value-constructor-error, test-compile-class-local-capture-error)
class_value_constructor_error.ts
class_local_capture_error.ts)
class_local_capture_error.ts
class_field_capture_error.ts
cpp_class_value_error.ts)

file(GLOB corpus "${TESTS_DIR}/*.ts")
list(SORT corpus)
Expand Down
Loading