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
62 changes: 59 additions & 3 deletions tslang/lib/TypeScript/MLIRGenAccessCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,57 @@ namespace mlirgen
return mlir::Value();
}

// Whether `name` is a field, method, accessor or static member of the class or of a base class:
// what `obj["name"]` reaches before a string indexer does.
static bool classHasMember(ClassInfo::TypePtr classInfo, mlir::StringRef name)
{
auto id = MLIRHelper::TupleFieldName(name, classInfo->classType.getContext());
if (classInfo->getStaticFieldIndex(id) >= 0 || classInfo->getMethodIndex(name) >= 0
|| classInfo->getGenericMethodIndex(name) >= 0 || classInfo->getAccessorIndex(name) >= 0)
{
return true;
}

auto storageClass = cast<mlir_ts::ClassStorageType>(classInfo->classType.getStorageType());
if (storageClass.getIndex(id) >= 0)
{
return true;
}

for (auto &baseClass : classInfo->baseClasses)
{
if (classHasMember(baseClass, name))
{
return true;
}
}

return false;
}

// Whether the class, or a base class, declares `[key: string]: T`.
static bool classHasStringIndexer(ClassInfo::TypePtr classInfo)
{
for (auto &indexInfo : classInfo->indexes)
{
if (indexInfo.indexSignature && indexInfo.indexSignature.getNumInputs() > 0
&& isa<mlir_ts::StringType>(indexInfo.indexSignature.getInput(0)))
{
return true;
}
}

for (auto &baseClass : classInfo->baseClasses)
{
if (classHasStringIndexer(baseClass))
{
return true;
}
}

return false;
}

ValueOrLogicalResult MLIRGenImpl::mlirGenElementAccess(mlir::Location location, mlir::Value expression, mlir::Value argumentExpression, bool isConditionalAccess, const GenContext &genContext)
{
auto arrayType = expression.getType();
Expand Down Expand Up @@ -1418,10 +1469,15 @@ namespace mlirgen
if (auto fieldName = argumentExpression.getDefiningOp<mlir_ts::ConstantOp>())
{
auto attr = fieldName.getValue();
if (isa<mlir::StringAttr>(attr))
if (auto nameAttr = dyn_cast<mlir::StringAttr>(attr))
{
// TODO: implement '[string]' access here
return mlirGenPropertyAccessExpression(location, expression, attr, isConditionalAccess, genContext);
// `obj["name"]` is the member `name`, as in TypeScript; only a key that names no
// member, of a class whose indexer takes a string, is the indexer's key
auto classInfo = getClassInfoByFullName(classType.getName().getValue());
if (!classInfo || classHasMember(classInfo, nameAttr.getValue()) || !classHasStringIndexer(classInfo))
{
return mlirGenPropertyAccessExpression(location, expression, attr, isConditionalAccess, genContext);
}
}
}

Expand Down
11 changes: 11 additions & 0 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ tslang_add_test(NAME test-compile-00-class-iterator-super COMMAND test-runner "$
tslang_add_test(NAME test-compile-00-class-symbol-iterator COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_symbol_iterator.ts")
tslang_add_test(NAME test-compile-00-class-indexer COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_indexer.ts")
tslang_add_test(NAME test-compile-00-class-indexer-super COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_indexer_super.ts")
tslang_add_test(NAME test-compile-00-class-indexer-string-key COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_indexer_string_key.ts")
tslang_add_test(NAME test-compile-00-class-from-tuple COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_from_tuple.ts")
tslang_add_test(NAME test-compile-00-class-access-control COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_access_control.ts")
tslang_add_test(NAME test-compile-00-class-protected-constructor COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_protected_constr.ts")
Expand Down Expand Up @@ -1337,6 +1338,7 @@ tslang_add_test(NAME test-jit-00-class-iterator-super COMMAND test-runner -jit "
tslang_add_test(NAME test-jit-00-class-symbol-iterator COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_symbol_iterator.ts")
tslang_add_test(NAME test-jit-00-class-indexer COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_indexer.ts")
tslang_add_test(NAME test-jit-00-class-indexer-super COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_indexer_super.ts")
tslang_add_test(NAME test-jit-00-class-indexer-string-key COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_indexer_string_key.ts")
tslang_add_test(NAME test-jit-00-class-from-tuple COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_from_tuple.ts")
tslang_add_test(NAME test-jit-00-class-access-control COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_access_control.ts")
tslang_add_test(NAME test-jit-00-class-protected-constructor COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_protected_constr.ts")
Expand Down Expand Up @@ -1777,6 +1779,14 @@ 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()
# `obj["name"]` of a class with a string indexer goes to its get/set unless "name" is a member:
# in one file under own (gc, rc and none run it above and in the corpus), and across a shared
# library's boundary under gc, rc and own
tslang_add_test(NAME test-jit-00-class-indexer-string-key-own COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00class_indexer_string_key.ts")
foreach(string_indexer_mm gc rc own)
tslang_add_test(NAME test-compile-shared-export-import-class-string-indexer-${string_indexer_mm} COMMAND test-runner -shared -mm=${string_indexer_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/import_class_string_indexer.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_class_string_indexer.ts")
tslang_add_test(NAME test-jit-shared-export-import-class-string-indexer-${string_indexer_mm} COMMAND test-runner -jit -shared -mm=${string_indexer_mm} "${PROJECT_SOURCE_DIR}/test/tester/tests/import_class_string_indexer.ts" "${PROJECT_SOURCE_DIR}/test/tester/tests/export_class_string_indexer.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)
Expand Down Expand Up @@ -2476,6 +2486,7 @@ set(TSLANG_CORPUS
00class_generic.ts
00class_iface.ts
00class_indexer_super.ts
00class_indexer_string_key.ts
00class_indexer.ts
00class_iterator_super.ts
00class_iterator.ts
Expand Down
78 changes: 78 additions & 0 deletions tslang/test/tester/tests/00class_indexer_string_key.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// `obj["name"]` of a class with a string indexer: a key that names a member is that member,
// as in TypeScript; any other constant key goes to the indexer's get/set, as a variable key does.

class Base {
label = "base";
}

class Bag extends Base {
count = 0;

private keys: string[] = [];
private values: string[] = [];

[key: string]: string;

get(key: string): string {
for (let i = 0; i < this.keys.length; i++) {
if (this.keys[i] == key) {
return this.values[i];
}
}

return "";
}

set(key: string, value: string) {
let position = -1;
for (let i = 0; i < this.keys.length; i++) {
if (this.keys[i] == key) {
position = i;
}
}

if (position < 0) {
position = this.keys.length;
this.keys.push(key + "");
this.values.length = position + 1;
this.count++;
}

// in one place: under -mm=own a parameter is taken on every path or none
this.values[position] = value;
}

size() {
return this.count;
}
}

function main() {
const bag = new Bag();

// literal keys: the indexer
bag["name"] = "Ann";
bag["city"] = "Oslo";
assert(bag["name"] == "Ann");
assert(bag["city"] == "Oslo");
assert(bag["missing"] == "");

bag["name"] = "Bob";
assert(bag["name"] == "Bob");

// a const key is a constant too
const key = "city";
assert(bag[key] == "Oslo");

// a variable key, as before
let other = "name";
assert(bag[other] == "Bob");

// keys that name members still reach the members
assert(bag["count"] == 2);
assert(bag["label"] == "base");
assert(bag["size"]() == 2);

print(bag["name"], bag["city"], bag["count"]);
print("done.");
}
45 changes: 45 additions & 0 deletions tslang/test/tester/tests/export_class_string_indexer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
namespace M {

// A class with a string indexer defined in the exporting module; the importer
// reaches it with constant keys (`bag["name"]`) as well as variable ones.
// Cross-module counterpart to 00class_indexer_string_key.ts.

export class Bag {
private keys: string[] = [];
private values: string[] = [];

[key: string]: string;

get(key: string): string {
for (let i = 0; i < this.keys.length; i++) {
if (this.keys[i] == key) {
return this.values[i];
}
}

return "";
}

set(key: string, value: string) {
let position = -1;
for (let i = 0; i < this.keys.length; i++) {
if (this.keys[i] == key) {
position = i;
}
}

if (position < 0) {
position = this.keys.length;
this.keys.push(key + "");
this.values.length = position + 1;
}

// in one place: under -mm=own a parameter is taken on every path or none
this.values[position] = value;
}

get size() {
return this.keys.length;
}
}
}
17 changes: 17 additions & 0 deletions tslang/test/tester/tests/import_class_string_indexer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import './export_class_string_indexer'

function main() {
const bag = new M.Bag();
bag["name"] = "Ann";
bag["city"] = "Oslo";
bag["name"] = "Bob";

let key = "city";
assert(bag["name"] == "Bob");
assert(bag[key] == "Oslo");

// a key that names a member is the member
assert(bag["size"] == 2);

print("done.");
}
Loading