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
25 changes: 19 additions & 6 deletions TSLANG_ISSUES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2166,12 +2166,24 @@ Tests: `00object_dictionary.ts`, `00own_param_exclusive_moves.ts` (compile, JIT,
undefined or any) is an error at the `return`: next() gives `{ value: T }`, and `return "end"` in a
`Generator<number, string>` was cast to a number without a word (it read 0).

- next(value) of a generator typed `Generator<T, TReturn, TNext>`: its next() takes `value?: TNext`, and the
`yield` it resumes evaluates to the value, a TNext (TNext's default - 0, null - when next() is given none: the
first one, a for...of). It was undefined.
- An object-literal method given as an interface's function member of another arity - fewer parameters (`next()`
for `next: (value?: number) => ...`, the rest ignored, as JavaScript does) or more optional ones (`next(v?)` for
`next: () => ...`): "can't assign '() => ...' to '(p1: number | undefined) => ...': it takes 0 argument(s), 2
expected". The vtable's slot holds a thunk giving the method as many arguments as it takes
(isArityAdaptableObjectMethod, generateSynthMethodToCallField), as for a field holding a closure. So a generator
whose next() takes no value is a Generator whose next takes one, and the other way round. The library's
`Generator` declares `next: (value?: TNext)` once a compiler with this is released (a released one without it
can't give a plain generator as one).

Still open, looked at:
- next(value) / TNext: next() would take `value?: TNext` and the resumed `yield` evaluate to it - tried, and it works
for a generator cast to an interface declaring `next(value?)` - but a generator's next() is then cast to
interfaces declaring `next: () => ...` (a program's own, the library's Iterable), and an object's method of
another arity has no adapter in the interface's vtable (castObjectToInterface; a class has them, 150). Needs that
adapter, then `Generator`'s next in the library. return(v) / throw(e) likewise.
- return(v) / throw(e) of a generator.
- -mm=own: next(value) of a generator sending it an object (`Generator<T, void, Box>`) is refused ("'this value'
is a parameter, kept here, but -mm=own cannot make the callers of this function expression give it up"): next()
keeps what it is given, and a method used other than by a call can't make its callers give it up. A number or a
string works.
- -mm=own: a generator typed as an interface other than the library-instantiated Iterator<T> - a program's own, or
Iterable<T> - is "a value takes a second reference; -mm=own cannot prove a move or a borrow here yet" at its
`for...of` (before these changes too), and so is an array given as an Iterable<T>.
Expand All @@ -2182,4 +2194,5 @@ Still open, looked at:
`[Symbol.asyncIterator]` of an async generator.

Tests: `00iterables.ts`, `00generator_typed_yield.ts` (compile, JIT, rc),
diagnostic-location `generator_return_of_other_type.ts`.
diagnostic-location `generator_return_of_other_type.ts`; `00generator_next_value.ts`, `00object_method_arity.ts`
(compile, JIT, rc, own).
3 changes: 3 additions & 0 deletions tslang/include/TypeScript/Defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,9 @@
// `{ [k]: v }` with a key known only at run time). Outside the class, `d.name` and `d["name"]` are its
// entries, never its members (ClassInfo::isDictionary).
#define DICTIONARY_DECORATOR "dictionary"
// next(value) of a generator typed Generator<T, TReturn, TNext>: the value, the parameter of its next(), is what
// the `yield` it resumes evaluates to
#define GENERATOR_SENT_NAME ".sent"
#define OBJECT_DICTIONARY_CLASS_NAME "ObjectDictionary"
// the default library's function a spread of a dictionary into a new one calls
#define OBJECT_DICTIONARY_ASSIGN_NAME "objectDictionaryAssign"
Expand Down
40 changes: 40 additions & 0 deletions tslang/include/TypeScript/MLIRLogic/MLIRTypeHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -1370,6 +1370,39 @@ class MLIRTypeHelper
return false;
}

// An object-literal method (`this` first) an interface's function member can call through a thunk that gives
// it as many arguments as it takes: it takes fewer parameters than the member (`next()` for `next(value?)`,
// the rest ignored, as JavaScript does), or more, each of them optional (`next(value?)` for `next()`). The
// parameters both have, and the results, match. A generator's `next` is given as an iterator's this way.
bool isArityAdaptableObjectMethod(mlir::Location location, mlir::Type methodType, mlir::Type memberType)
{
auto srcType = dyn_cast<mlir_ts::FunctionType>(methodType);
auto destType = dyn_cast<mlir_ts::FunctionType>(memberType);
if (!srcType || !destType || srcType == destType || isObjectFieldFunctionWithoutThis(srcType)
|| destType.getNumInputs() == 0 || srcType.isVarArg() || destType.isVarArg()
|| srcType.getNumInputs() == destType.getNumInputs())
{
return false;
}

auto common = std::min(srcType.getNumInputs(), destType.getNumInputs());
for (auto extra : srcType.getInputs().drop_front(common))
{
auto unionType = dyn_cast<mlir_ts::UnionType>(extra);
if (!isa<mlir_ts::OptionalType, mlir_ts::UndefinedType, mlir_ts::AnyType, mlir_ts::UnknownType>(extra)
&& !(unionType && llvm::any_of(unionType.getTypes(), [](mlir::Type t) { return isa<mlir_ts::UndefinedType>(t); })))
{
return false;
}
}

// the method as if it took the member's parameters past the common ones: then it has to match
SmallVector<mlir::Type> inputs(srcType.getInputs().take_front(common));
inputs.append(destType.getInputs().begin() + common, destType.getInputs().end());
auto aligned = mlir_ts::FunctionType::get(context, inputs, srcType.getResults(), false);
return TestFunctionTypesMatchWithObjectMethods(location, aligned, destType).result == MatchResultType::Match;
}

MatchResult TestFunctionTypesMatchWithObjectMethods(mlir::Location location, mlir::Type inFuncType, mlir::Type resFuncType, unsigned startParamIn = 0,
unsigned startParamRes = 0)
{
Expand Down Expand Up @@ -1682,6 +1715,13 @@ class MLIRTypeHelper
}
}

// a method taking fewer parameters, or more optional ones, is called through a thunk too
// (isArityAdaptableObjectMethod)
if (isArityAdaptableObjectMethod(location, foundField.type, fieldType))
{
return {mlir_ts::FieldInfo{id, fieldType, false, mlir_ts::AccessLevel::Public}, mlir::success()};
}

auto test = isa<mlir_ts::FunctionType>(foundField.type) && isa<mlir_ts::FunctionType>(fieldType)
? TestFunctionTypesMatchWithObjectMethods(location, foundField.type, fieldType).result == MatchResultType::Match
: stripLiteralType(fieldType) == stripLiteralType(foundField.type);
Expand Down
18 changes: 16 additions & 2 deletions tslang/lib/TypeScript/MLIRGenExpressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,22 @@ namespace mlirgen
label << GENERATOR_STATELABELPREFIX << state;
builder.create<mlir_ts::StateLabelOp>(location, label.str());

// TODO: the value passed to next(value); next() takes no argument yet, so a yield evaluates to undefined.
// It is made after the label: the turn that resumes here is the one that uses it.
// The value passed to next(value) - next()'s parameter, which a generator typed Generator<T, TReturn,
// TNext> has - or undefined. It is read after the label: the turn that resumes here is the one that uses it.
if (auto sent = resolveIdentifier(location, GENERATOR_SENT_NAME, genContext))
{
// a TNext, as TypeScript types the `yield`: the parameter is optional (the first next(), a for...of,
// give none), and a turn given none reads TNext's default (0 for a number, null for a class)
if (auto optType = dyn_cast<mlir_ts::OptionalType>(sent.getType()))
{
auto valueOrDefault = builder.create<mlir_ts::ValueOrDefaultOp>(location, optType.getElementType(), sent);
valueOrDefault->setAttr(EXPLICIT_CAST_ATTR_NAME, builder.getUnitAttr());
return V(valueOrDefault);
}

return sent;
}

return V(builder.create<mlir_ts::UndefOp>(location, getUndefinedType()));
}

Expand Down
11 changes: 10 additions & 1 deletion tslang/lib/TypeScript/MLIRGenFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,7 @@ namespace mlirgen
// s32 the literal is), as TypeScript types it; without it the yields' own type was the element's, and
// `yield 1` could not be a Generator<number> ("can't assign '() => {value:s32, ...}'")
TypeNode nextResultType = undefined;
NodeArray<ParameterDeclaration> nextParameters;
if (auto declaredType = functionLikeDeclarationBaseAST->type;
declaredType && declaredType == SyntaxKind::TypeReference && !hasModifier(functionLikeDeclarationBaseAST, SyntaxKind::AsyncKeyword))
{
Expand All @@ -1285,14 +1286,22 @@ namespace mlirgen
members.push_back(nf.createPropertySignature(undefined, nf.createIdentifier(S("done")), undefined,
nf.createToken(SyntaxKind::BooleanKeyword).as<TypeNode>()));
nextResultType = nf.createTypeLiteralNode(members);

// Generator<T, TReturn, TNext>: next(value?: TNext), the value what the resumed `yield` is
if (typeName == "Generator" && typeReference->typeArguments.size() >= 3)
{
nextParameters.push_back(nf.createParameterDeclaration(
undefined, undefined, nf.createIdentifier(S(GENERATOR_SENT_NAME)),
nf.createToken(SyntaxKind::QuestionToken), typeReference->typeArguments[2]));
}
}
}
}

// create method next in object
auto nextMethodDecl =
nf.createMethodDeclaration(undefined, undefined, nf.createIdentifier(S(ITERATOR_NEXT)), undefined,
undefined, undefined, nextResultType, nextBody);
undefined, nextParameters, nextResultType, nextBody);
nextMethodDecl->internalFlags |= InternalFlags::VarsInObjectContext | InternalFlags::GeneratorNext;

// copy location info, to fix issue with names of anonymous functions
Expand Down
7 changes: 5 additions & 2 deletions tslang/lib/TypeScript/MLIRGenImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -17110,7 +17110,8 @@ class MLIRGenImpl
// mlirGenClassVirtualTableDefinitionForInterface, mlirGenObjectVirtualTableDefinitionForInterface)
mlir::LogicalResult generateSynthMethodToCallField(mlir::Location location, mlir::Type thisType,
StringRef fieldName, StringRef thunkName,
mlir_ts::FunctionType funcType, const GenContext &genContext)
mlir_ts::FunctionType funcType, const GenContext &genContext,
unsigned maxArguments = ~0u)
{
auto retType = mth.getReturnTypeFromFuncRef(funcType);

Expand All @@ -17136,7 +17137,9 @@ class MLIRGenImpl
NodeFactory nf(NodeFactoryFlags::None);

NodeArray<Expression> argumentsArray;
for (unsigned index = 1; index < funcType.getNumInputs(); index++)
// as many as the field's function takes (maxArguments): a method with fewer parameters than the
// interface's member ignores the rest
for (unsigned index = 1; index < funcType.getNumInputs() && index - 1 < maxArguments; index++)
{
std::string paramName("p");
paramName += std::to_string(index - 1);
Expand Down
10 changes: 7 additions & 3 deletions tslang/lib/TypeScript/MLIRGenInterfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,8 @@ namespace mlirgen
// a field the interface calls through a thunk has the thunk in its slot already
auto fieldInfo = mth.getFieldInfoByIndex(storeType, index);
if (lookupObjectLiteralMethodSymbol(fieldInfo.type, fieldId).empty()
&& !mth.isObjectFieldFunctionWithoutThis(fieldInfo.type))
&& !mth.isObjectFieldFunctionWithoutThis(fieldInfo.type)
&& !mth.isArityAdaptableObjectMethod(location, fieldInfo.type, method.funcType))
{
methodsNeedingPatch.push_back({methodPtr, vtableOffset});
}
Expand Down Expand Up @@ -860,7 +861,9 @@ namespace mlirgen
}

auto storageFieldType = tupleStorageType.getFieldInfo(storageIndex).type;
if (storageFieldType == funcType || !mth.isObjectFieldFunctionWithoutThis(storageFieldType))
// a method of another arity (isArityAdaptableObjectMethod) is given as many arguments as it takes
auto arityAdapted = mth.isArityAdaptableObjectMethod(location, storageFieldType, funcType);
if (storageFieldType == funcType || !mth.isObjectFieldFunctionWithoutThis(storageFieldType) && !arityAdapted)
{
continue;
}
Expand All @@ -872,8 +875,9 @@ namespace mlirgen
}

auto thunkName = concat(fullClassInterfaceVTableFieldName, fieldName.getValue(), "thunk");
auto maxArguments = arityAdapted ? mlir::cast<mlir_ts::FunctionType>(storageFieldType).getNumInputs() - 1 : ~0u;
if (mlir::failed(generateSynthMethodToCallField(location, objectType, fieldName.getValue(), thunkName,
funcType, genContext)))
funcType, genContext, maxArguments)))
{
return mlir::failure();
}
Expand Down
8 changes: 8 additions & 0 deletions tslang/test/tester/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,8 @@ tslang_add_test(NAME test-compile-00-own-param-exclusive-moves COMMAND test-runn
tslang_add_test(NAME test-compile-00-object-dictionary COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00object_dictionary.ts")
tslang_add_test(NAME test-compile-00-iterables COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00iterables.ts")
tslang_add_test(NAME test-compile-00-generator-typed-yield COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_typed_yield.ts")
tslang_add_test(NAME test-compile-00-object-method-arity COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00object_method_arity.ts")
tslang_add_test(NAME test-compile-00-generator-next-value COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_next_value.ts")
tslang_add_test(NAME test-compile-00-constructor-type-value COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00constructor_type_value.ts")
tslang_add_test(NAME test-compile-00-string-concat-chain COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00string_concat_chain.ts")
tslang_add_test(NAME test-compile-00-arrays COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00array.ts")
Expand Down Expand Up @@ -1300,6 +1302,12 @@ tslang_add_test(NAME test-jit-00-iterables COMMAND test-runner -jit "${PROJECT_S
tslang_add_test(NAME test-jit-rc-00-iterables COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00iterables.ts")
tslang_add_test(NAME test-jit-00-generator-typed-yield COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_typed_yield.ts")
tslang_add_test(NAME test-jit-rc-00-generator-typed-yield COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_typed_yield.ts")
tslang_add_test(NAME test-jit-00-object-method-arity COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00object_method_arity.ts")
tslang_add_test(NAME test-jit-rc-00-object-method-arity COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00object_method_arity.ts")
tslang_add_test(NAME test-jit-own-00-object-method-arity COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00object_method_arity.ts")
tslang_add_test(NAME test-jit-00-generator-next-value COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_next_value.ts")
tslang_add_test(NAME test-jit-rc-00-generator-next-value COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_next_value.ts")
tslang_add_test(NAME test-jit-own-00-generator-next-value COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_next_value.ts")
tslang_add_test(NAME test-jit-00-constructor-type-value COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00constructor_type_value.ts")
tslang_add_test(NAME test-jit-rc-00-constructor-type-value COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00constructor_type_value.ts")
tslang_add_test(NAME test-jit-own-00-constructor-type-value COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00constructor_type_value.ts")
Expand Down
34 changes: 34 additions & 0 deletions tslang/test/tester/tests/00generator_next_value.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// next(value) of a generator typed Generator<T, TReturn, TNext>: the value is what the yield it resumes
// evaluates to, a TNext (its default when next() is given none). A generator whose next() takes no value is
// a Generator too, and a for...of calls next() with none.
type IterateResult<T> = { value: T, done: boolean };
interface Generator<T = unknown, TReturn = any, TNext = any> {
next: (value?: TNext) => IterateResult<T>;
}

function* echo(): Generator<string, void, string> {
let last = "start";
while (true) {
const got = yield last;
last = "got " + got;
}
}

function* counter(): Generator<number> {
for (let i = 0; i < 3; i++) yield i;
}

function main() {
const e = echo();
print(e.next().value);
print(e.next("a").value);
print(e.next("b").value);

for (const v of counter()) print(v);

const c = counter();
print(c.next(123).value);

print("done.");
}

23 changes: 23 additions & 0 deletions tslang/test/tester/tests/00object_method_arity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// An object-literal method given as an interface's function member of another arity: next() for a member
// next(value?) (the value ignored, as JavaScript does), next(v?) for next() (v undefined). Was
// "can't assign '() => ...' to '(p1: number | undefined) => ...': it takes 0 argument(s), 2 expected".
type R = { value: number, done: boolean };
interface WithArgField {
next: (value?: number) => R;
}
interface WithArgMethod {
next(value?: number): R;
}
interface NoArgField {
next: () => R;
}

function main() {
const a: WithArgField = { next() { return { value: 1, done: false }; } };
print(a.next(3).value);
const b: WithArgMethod = { next() { return { value: 2, done: false }; } };
print(b.next(3).value);
const c: NoArgField = { next(v?: number) { return { value: v === undefined ? 30 : v, done: false }; } };
print(c.next().value);
print("done.");
}
Loading