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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,12 @@ declare function dist(p: Reference<Point> | null): f64;

A pointer parameter or struct field is nullable (`| null`), as C lets it be null; a C++ reference (`const Point &`) and a result aren't.

A C function taking a callback is declared `@cdecl`: its function-typed parameters are then C function pointers, given a function or an arrow that captures nothing (a closure or a bound method is a compile error):

```typescript
@cdecl declare function qsort(base: Opaque, count: u64, size: u64, compare: (a: Opaque, b: Opaque) => s32): void;
```

A struct is a named tuple with its fields where C puts them; tslang lays a nested tuple out in place, as C does a struct member:

- **an array** `T x[N]` is a tuple of its elements, `x: [_0: T, _1: T, ...]` (tslang has no fixed-size array type; one of more than 1024 elements, or of unknown size, is skipped);
Expand Down
17 changes: 16 additions & 1 deletion TSLANG_ISSUES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ should be a complete file that was run (`tslang file.ts`, JIT, Windows x64), wit

## Open

Items 1-28 (collected 2026-10-12 to pre-alpha118) are fixed or answered; their write-ups are in this
Items 1-30 (collected 2026-10-12 to pre-alpha119) are fixed or answered; their write-ups are in this
file's git history. 24-26 were fixed by #775 (regression tests
`tslang/test/tester/tests/00tslang_issues_24_26.ts` and `ref_element_layout_error.ts`): 24 (an array
literal holding `const X = 1 << n` widened to 64-bit elements: shifts and bitwise operators now give an
Expand All @@ -20,3 +20,18 @@ Regression test `tslang/test/tester/tests/00tslang_issue_27.ts`.
28 (a `readonly` constructor parameter without `public`, `private` or `protected` made no parameter
property, so `t.width` didn't resolve): `readonly` alone now makes a public one, as in TypeScript.
Regression test `tslang/test/tester/tests/00tslang_issue_28.ts`.
29 (a function-typed parameter of a `declare function` was a TypeScript function value, `{ ptr, ptr, ptr }`,
so `_beginthreadex(..., entry, arg, ...)` shifted `arg` and every argument after the callback): a
function declared `@cdecl` takes each function-typed parameter as a C function pointer. A function or
an arrow that captures nothing passes; a capturing arrow or a bound method passed straight to it is a
compile error ("a closure or a bound method can't be used as a plain function pointer"). Not checked
yet: a closure already stored in a variable of function type, passed on to a `@cdecl` parameter, still
loses its captures at run time. Regression tests `tslang/test/tester/tests/00tslang_issue_29.ts`,
`00tslang_issue_29_thread.ts` (Windows), `00tslang_issue_29_pthread.ts` (Linux) and
`cdecl_closure_error.ts`.
30 (a generic's body, compiled where it is first instantiated, resolved a name to a local of the
instantiating function: `const total = 5` in `main` hid the module's `total()` from `Bag<T>.sum()`, and
a local `values` broke the default library's `Array<T>`): a generic declared outside every function
now sees no local of another function; one declared in a function still reads that function's locals,
and lambdas in a generic still capture the generic's own. Regression test
`tslang/test/tester/tests/00tslang_issue_30.ts`.
2 changes: 1 addition & 1 deletion docs/c-cpp-header-import.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ compiler):
| `enum E { A = 1 }` | `enum E { A = 1 }` | Underlying width from clang. |
| `#define NAME 42` (object-like macro, literal) | `const NAME = 42` | Via clang's preprocessing record; only literal/`constexpr`-foldable macros; skip function-like macros (report them in a comment). |
| `typedef` | `type` alias | |
| Function pointers | function type `(a: T) => R` | Already representable; C callbacks into tslang functions work if the closure has no captured environment (thick-vs-thin function pointer distinction must be checked per site). |
| Function pointers | function type `(a: T) => R` on a `@cdecl declare function` | `@cdecl` makes each function-typed parameter a C function pointer. A function or an arrow capturing nothing passes; a capturing arrow or bound method is a compile error. Without `@cdecl` the parameter is a TypeScript function value (three pointers), which C can't call. |
| `...` varargs | `...args` + `func.varargs` | Already supported. |
| Bitfields | not representable | Emit the containing struct as opaque + generated accessor functions (phase 2), or skip with a diagnostic. |

Expand Down
4 changes: 4 additions & 0 deletions tslang/include/TypeScript/Defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,10 @@
// declaration can give it: never on one that may run TypeScript code (it takes a callback) or
// one re-parsed from a TypeScript library, whose functions may destroy what they are given.
#define NO_DROPS_DECORATOR "nodrops"
// `@cdecl declare function f(cb: (x: T) => R)`: f is a C function, so a parameter of a function type is
// a C function pointer - one code pointer, not the code-and-context a TypeScript function value is.
// Only a function that captures nothing can be passed there (TSLANG_ISSUES 29).
#define CDECL_DECORATOR "cdecl"
// The memory model a module is compiled for, a string literal: `if (__MEMORY_MODEL__ === "gc")`
// generates the branch taken only (MLIRGenImpl::foldLiteralCompare)
#define MEMORY_MODEL_IDENTIFIER "__MEMORY_MODEL__"
Expand Down
7 changes: 7 additions & 0 deletions tslang/include/TypeScript/MLIRLogic/MLIRGenStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ struct GenericFunctionInfo
bool processing;
bool processed;

// declared inside a function, whose locals its body may read; otherwise an instantiation sees no
// local of the function it is instantiated from (MLIRGenImpl::isolateGeneratedBodies)
bool declaredInFunction = false;

GenericFunctionInfo() = default;
};

Expand Down Expand Up @@ -1301,6 +1305,9 @@ struct GenericClassInfo
SourceFile sourceFile;
StringRef fileName;

// see GenericFunctionInfo::declaredInFunction
bool declaredInFunction = false;

GenericClassInfo()
{
}
Expand Down
2 changes: 2 additions & 0 deletions tslang/lib/TypeScript/MLIRGenClasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ namespace mlirgen
newGenericClassPtr->elementNamespace = currentNamespace;
newGenericClassPtr->sourceFile = sourceFile;
newGenericClassPtr->fileName = mainSourceFileName;
newGenericClassPtr->declaredInFunction =
(genContext.funcOp && genContext.funcOp != genContext.rootStatementsFuncOp) || !lexicalFuncOps.empty();

mlirGenClassType(newGenericClassPtr, genContext);

Expand Down
31 changes: 30 additions & 1 deletion tslang/lib/TypeScript/MLIRGenFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,19 @@ namespace mlirgen
if (typeParameter)
{
type = getType(typeParameter, genContext);

// a C function's callback is a bare code pointer (CDECL_DECORATOR)
if (auto hybridFuncType = dyn_cast_or_null<mlir_ts::HybridFunctionType>(type))
{
if (MLIRHelper::hasDecorator(parametersContextAST, CDECL_DECORATOR))
{
type = mlir_ts::FunctionType::get(builder.getContext(), hybridFuncType.getInputs(),
hybridFuncType.getResults(), hybridFuncType.isVarArg());
}
}
}

// special case, setup 'this' and type provided
// special case, setup 'this' and type provided
if (namePtr == THIS_NAME && type)
{
// NOTE: upward mailbox: explicit this-parameter type must reach the prototype chain - see GenContext
Expand Down Expand Up @@ -1928,6 +1938,25 @@ namespace mlirgen
loopLabelAliases = std::move(outerLoopLabelAliases);
});

// nor are the locals of the function a generic is instantiated from this body's: it starts a new
// lexical root, and a function generated within it joins it (isolateGeneratedBodies)
auto outerLexicalFuncOps = lexicalFuncOps;
auto outerIsolateGeneratedBodies = isolateGeneratedBodies;
if (isolateGeneratedBodies)
{
lexicalFuncOps.clear();
lexicalFuncOps.push_back(funcOp);
isolateGeneratedBodies = false;
}
else if (!lexicalFuncOps.empty())
{
lexicalFuncOps.push_back(funcOp);
}
auto restoreLexicalRoot = llvm::make_scope_exit([&]() {
lexicalFuncOps = std::move(outerLexicalFuncOps);
isolateGeneratedBodies = outerIsolateGeneratedBodies;
});

// nor is a @value type a body needs one the field being typed around it waits for (an
// initializer's return type inferred from this body)
auto outerValueLayoutFrames = std::move(valueLayoutFrames);
Expand Down
21 changes: 21 additions & 0 deletions tslang/lib/TypeScript/MLIRGenGenerics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,9 @@ namespace mlirgen
currentNamespace = functionGenericTypeInfo->elementNamespace;

SourceFileScope sourceFileScope(*this, functionGenericTypeInfo->sourceFile, functionGenericTypeInfo->fileName);
// the generic sees the locals of no function it is not declared in (TSLANG_ISSUES 30)
MLIRValueGuard<bool> isolateGuard(isolateGeneratedBodies);
isolateGeneratedBodies = !functionGenericTypeInfo->declaredInFunction;

return instantiateSpecializedFunctionTypeHelper(location, functionGenericTypeInfo->functionDeclaration,
recieverType, discoverReturnType, genContext, capturesFirst);
Expand Down Expand Up @@ -666,6 +669,9 @@ namespace mlirgen
currentNamespace = functionGenericTypeInfo->elementNamespace;

SourceFileScope sourceFileScope(*this, functionGenericTypeInfo->sourceFile, functionGenericTypeInfo->fileName);
// the generic sees the locals of no function it is not declared in (TSLANG_ISSUES 30)
MLIRValueGuard<bool> isolateGuard(isolateGeneratedBodies);
isolateGeneratedBodies = !functionGenericTypeInfo->declaredInFunction;

auto [result, specFuncOp, specFuncName, isGeneric] =
mlirGenFunctionLikeDeclaration(functionGenericTypeInfo->functionDeclaration, funcGenContext);
Expand Down Expand Up @@ -1037,6 +1043,9 @@ namespace mlirgen
currentNamespace = functionGenericTypeInfo->elementNamespace;

SourceFileScope sourceFileScope(*this, functionGenericTypeInfo->sourceFile, functionGenericTypeInfo->fileName);
// the generic sees the locals of no function it is not declared in (TSLANG_ISSUES 30)
MLIRValueGuard<bool> isolateGuard(isolateGeneratedBodies);
isolateGeneratedBodies = !functionGenericTypeInfo->declaredInFunction;

GenContext genericTypeGenContext(genContext);
genericTypeGenContext.specialization = true;
Expand Down Expand Up @@ -1098,6 +1107,9 @@ namespace mlirgen
currentNamespace = functionGenericTypeInfo->elementNamespace;

SourceFileScope sourceFileScope(*this, functionGenericTypeInfo->sourceFile, functionGenericTypeInfo->fileName);
// the generic sees the locals of no function it is not declared in (TSLANG_ISSUES 30)
MLIRValueGuard<bool> isolateGuard(isolateGeneratedBodies);
isolateGeneratedBodies = !functionGenericTypeInfo->declaredInFunction;

auto anyNamedGenericType = IsGeneric::False;

Expand Down Expand Up @@ -1405,6 +1417,9 @@ namespace mlirgen
currentNamespace = genericClassInfo->elementNamespace;

SourceFileScope sourceFileScope(*this, genericClassInfo->sourceFile, genericClassInfo->fileName);
// the generic sees the locals of no function it is not declared in (TSLANG_ISSUES 30)
MLIRValueGuard<bool> isolateGuard(isolateGeneratedBodies);
isolateGeneratedBodies = !genericClassInfo->declaredInFunction;

GenContext genericTypeGenContext(genContext);
genericTypeGenContext.instantiateSpecializedFunction = false;
Expand Down Expand Up @@ -1481,6 +1496,9 @@ namespace mlirgen
currentNamespace = genericClassInfo->elementNamespace;

SourceFileScope sourceFileScope(*this, genericClassInfo->sourceFile, genericClassInfo->fileName);
// the generic sees the locals of no function it is not declared in (TSLANG_ISSUES 30)
MLIRValueGuard<bool> isolateGuard(isolateGeneratedBodies);
isolateGeneratedBodies = !genericClassInfo->declaredInFunction;

GenContext genericTypeGenContext(genContext);
genericTypeGenContext.instantiateSpecializedFunction = false;
Expand Down Expand Up @@ -1603,6 +1621,9 @@ namespace mlirgen
currentNamespace = genericClassInfo->elementNamespace;

SourceFileScope sourceFileScope(*this, genericClassInfo->sourceFile, genericClassInfo->fileName);
// the generic sees the locals of no function it is not declared in (TSLANG_ISSUES 30)
MLIRValueGuard<bool> isolateGuard(isolateGeneratedBodies);
isolateGeneratedBodies = !genericClassInfo->declaredInFunction;

GenContext templateGenContext(genContext);
for (auto &typeParam : genericClassInfo->typeParams)
Expand Down
40 changes: 40 additions & 0 deletions tslang/lib/TypeScript/MLIRGenImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -3797,6 +3797,8 @@ class MLIRGenImpl
newGenericFunctionPtr->thisClassType = genContext.thisClassType;
newGenericFunctionPtr->sourceFile = sourceFile;
newGenericFunctionPtr->fileName = mainSourceFileName;
newGenericFunctionPtr->declaredInFunction =
(genContext.funcOp && genContext.funcOp != genContext.rootStatementsFuncOp) || !lexicalFuncOps.empty();

// TODO: review it, ignore in case of ArrowFunction,
if (!ignoreFunctionArgsDetection)
Expand Down Expand Up @@ -10787,7 +10789,35 @@ class MLIRGenImpl

if (value.getType() != argTypeDestFuncType)
{
// A closure or a bound method is a function and the value it runs with (its captures,
// its `this`); a plain function type parameter - a C callback (@cdecl, TSLANG_ISSUES 29)
// - is only a code pointer, and the cast drops the value: the function would read its
// captures through whatever the caller passed first. An arrow with untyped parameters
// becomes a closure only as the cast instantiates it.
auto closureAsFunctionPointer = [&](mlir::Type valueType) {
if (isa<mlir_ts::BoundFunctionType>(valueType) && isa<mlir_ts::FunctionType>(argTypeDestFuncType))
{
emitError(location, "a closure or a bound method can't be used as a plain function pointer: "
"the pointer carries no captured variables and no 'this'");
return true;
}

return false;
};

if (closureAsFunctionPointer(value.getType()))
{
return mlir::failure();
}

CAST_A(castValue, location, argTypeDestFuncType, value, genContext);

if (auto castOp = castValue.getDefiningOp<mlir_ts::CastOp>();
castOp && closureAsFunctionPointer(castOp.getIn().getType()))
{
return mlir::failure();
}

operands[i] = castValue;
}

Expand Down Expand Up @@ -17161,6 +17191,16 @@ class MLIRGenImpl
// can't leave a function.
SmallVector<std::string> activeLabels;

// A generic is instantiated where it is first used, so the locals of the function using it are
// still in symbolTable while its body is generated; a name in the body resolved to a local of its
// caller (TSLANG_ISSUES 30). While a generic declared outside every function is instantiated,
// isolateGeneratedBodies is set, and each function body generated then (the generic function, or
// each method of the generic class) starts lexicalFuncOps over: that body, and the functions
// generated within it (its lambdas and nested generics, which redeclare what they capture), see
// no local of any other function (resolveIdentifierAsVariable).
bool isolateGeneratedBodies = false;
SmallVector<mlir_ts::FuncOp> lexicalFuncOps;

// The yields standing where nothing is computed before them, marked by the statement around
// them (the AST has no parent links to look up): yield node -> its value is used. See
// SupportedYieldScope.
Expand Down
25 changes: 24 additions & 1 deletion tslang/lib/TypeScript/MLIRGenVariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1125,8 +1125,31 @@ namespace mlirgen
{
//LLVM_DEBUG(dbgs() << "\n!! resolveIdentifierAsVariable: " << name << " type: " << value.second->getType() << " value: " << value.first;);

// begin of logic: outer vars
auto valueRegion = value.first.getParentRegion();

// a local of a function this one is not declared in - the caller a generic is instantiated
// from - is no name here: the name is the module's (lexicalFuncOps, TSLANG_ISSUES 30)
if (!lexicalFuncOps.empty() && valueRegion && valueRegion->getParentOp() &&
(isa<mlir_ts::FuncOp>(valueRegion->getParentOp()) || valueRegion->getParentOp()->getParentOfType<mlir_ts::FuncOp>()))
{
auto inRoot = llvm::any_of(lexicalFuncOps, [&](mlir_ts::FuncOp lexicalFuncOp) {
return lexicalFuncOp.getCallableRegion() && lexicalFuncOp.getCallableRegion()->isAncestor(valueRegion);
});
mlir_ts::FuncOp contextFuncOp = genContext.funcOp;
auto inFunction = contextFuncOp && contextFuncOp.getCallableRegion() &&
contextFuncOp.getCallableRegion()->isAncestor(valueRegion);
// a module's top-level code is a function of its own (rootStatementsFuncOp), whose
// locals are the module's variables
mlir_ts::FuncOp rootStatementsFuncOp = genContext.rootStatementsFuncOp;
auto inModuleCode = rootStatementsFuncOp && rootStatementsFuncOp.getCallableRegion() &&
rootStatementsFuncOp.getCallableRegion()->isAncestor(valueRegion);
if (!inRoot && !inFunction && !inModuleCode)
{
return mlir::Value();
}
}

// begin of logic: outer vars
auto isOuterVar = false;
// TODO: review code "valueRegion && valueRegion->getParentOp()" is to support async.execute
if (genContext.funcOp && genContext.funcOp != tempFuncOp && valueRegion &&
Expand Down
Loading
Loading