ETS2Panda DeclGen ETS2TS Knowledge Base
Document Version: v1.0 Last Updated: 2026-05-28 Scope:
ets_frontendfrontend compiler knowledge base entry Before modifying, please read:AGENTS.md, the corresponding compiler routeAGENTS.md, and the nearest directory-levelAGENTS.md
Overview
declgen_ets2ts generates Dynamic ArkTS declaration files (.d.ets) and glue code (.ts) from a Program AST that has completed type checking (CHECKED state). It is a key component of the interoperability chain between the ArkTS static frontend and the TypeScript ecosystem.
Directory Structure and Code Map
- Upstream: ets2panda compiler pipeline (Program AST in
CHECKEDstate) +ETSChecker(type information) - Downstream:
.d.etsdeclaration files (consumed by the TypeScript side) +.tsglue code (interop runtime bridge); invoked by the declgen phase ofbuild_system(driver)
Directory Explanation
declgenEts2Ts.h/declgenEts2Ts.cpp— Core generation engine, containing theTSDeclGenclass and top-level entry functionGenerateTsDeclarations()isolatedDeclgenChecker.h/isolatedDeclgenChecker.cpp— Isolated-mode validatorIsolatedDeclgenChecker; does not depend on the global symbol table; checks whether properties/parameters/functions have explicit type annotationsmain.cpp— Standalone CLI entry point; parses--export-all,--output-dets,--output-ets, and other arguments; drives the full compilation pipeline via thees2panda_libpublic API before callingGenerateTsDeclarations()declgen_ets2ts_error.yaml— Error diagnostic definitions (IDENT_KEY_SUPPORT, UNSUPPORTED_LITERAL_TYPE, etc.; 10 entries total)declgen_ets2ts_warning.yaml— Warning diagnostic definitions (EMPTY_TYPE_NAME, UNTYPED_METHOD; 2 entries total)isolated_declgen.yaml— Diagnostic definitions for isolated declaration generation mode (variables/properties/parameters must have explicit type annotations, etc.; 8 entries total)CMakeLists.txt/BUILD.gn— Build configuration
Key Files and Responsibilities
declgenEts2Ts.h/declgenEts2Ts.cppTSDeclGenclass: Core declaration generation engine; holdsETSChecker*,IsolatedDeclgenChecker*,parser::Program*; maintainsdependencySet_(type dependencies),importSet_(import statements),exportSet_(export statements),glueCodeImportSet_(glue code imports),paramDefaultMap_(parameter default values); outputs declaration content and glue code into two string streamsoutputDts_andoutputTs_respectivelyGenerateTsDeclarations(checker, program, declgenOptions)— Top-level entry: initializesTSDeclGen, executes the full generation pipeline, callsWriteOutputFiles()to write output filesValidateDeclgenOptions()— ValidatesDeclgenOptionsparametersWriteOutputFiles()— Writes the generated declaration content to.d.ets/.d.tsand.etsfilesDeclgenOptionsstruct:exportAll(treat all top-level statements as exported),isolated(enable isolated mode),outputDeclEts(output declaration file path),outputEts(output glue code path),recordFile(declaration record file),genAnnotations(whether to generate annotations, default true)
isolatedDeclgenChecker.h/isolatedDeclgenChecker.cppIsolatedDeclgenCheckerclass: Performs isolated checks onir::ScriptFunction,ir::ClassProperty,ir::ETSParameterExpression,ir::ExportDefaultDeclaration,ir::ArrayExpression; enforces explicit type annotations; reportsisolated_declgendiagnostics for cases that cannot be inferred
main.cpp— CLI tool: filters out declgen-specific arguments, obtains the compiler API viaes2panda_GetImpl(), drives thePARSED → CHECKEDstate transition, then callsGenerateTsDeclarations()
Responsibility Boundaries
- Responsible for: executing declaration generation on a
CHECKED-state AST; generating.d.etstype declaration files and.etsinterop glue code; handling@noninteropannotation; validating explicit type annotations in isolated mode - Not responsible for: parser/lexer phase (AST construction); type checking itself (
ETSChecker); lowering/IR/emit phases of the main compiler pipeline; fixing semantic errors outsidedeclgen_ets2ts_error.yaml
Core Data Flow / Control Flow
ets2panda compiler (CHECKED state) / CLI (parse → check)
→ GenerateTsDeclarations(ETSChecker*, Program*, DeclgenOptions)
→ ValidateDeclgenOptions()
→ TSDeclGen::Generate()
1. GenGlobalDescriptorInit() (initialize ETSGLOBAL, runtime interop descriptor)
2. Traverse AST to collect dependencySet_ (type dependency collection)
3. Collect glueCodeImportSet_ (glue code runtime imports)
4. GenExportNamedDeclarations() (class/interface/type alias declaration generation)
5. Generate export statements (exportSet_)
6. GenInitModuleGlueCode() (glue code module init calls)
7. GenImportDeclarations() (import statement generation, done last to ensure correct ordering)
→ WriteOutputFiles()
→ write outputDeclEts (.d.ets / .d.ts)
→ write outputEts (.ets glue code)
Isolated mode additional flow:
IsolatedDeclgenChecker::Check()
→ Traverse functions/properties/parameters, check for explicit type annotations
→ Report isolated_declgen diagnostics (VARABLE_MUST_HAVE_EXPLICIT_TYPE_ANNOTATION, etc.)
Knowledge Routing
- Generated declaration content is incorrect or missing → this document (inspect
TSDeclGen::GenExportNamedDeclarationsand the correspondingGenType) - Isolated mode reports "must have explicit type annotation" → this document (
IsolatedDeclgenChecker, seeisolated_declgen.yaml) - Type resolution error during declaration generation → first check whether checker has completed (whether the AST is in
CHECKEDstate) - Output file path / parameter configuration issues → check
DeclgenOptionsanddeclgenV1OutPath/declgenV2OutPathpassed bybuild_system - Upstream AST issues (parse errors, checker not completed) →
Static_Frontend_Knowledge_Base.md - declgen trigger logic on the
build_systemside →Driver_Knowledge_Base.md
Expert Tips
- First confirm the input AST is in
CHECKEDstate (ctxImpl->lazyCheck = falseandProceedToState(ES2PANDA_STATE_CHECKED)); otherwise type information inETSCheckeris incomplete and the generated output will be missing or incorrect - To generate
.d.ts(TypeScript-format declarations),outputDeclEtsmust end with.d.ts(determined byIsTypeScriptDeclarationOutput());.d.etsis the ArkTS declaration format - Modules annotated with
@noninterop(NON_INTEROP_FLAG) do not participate in interop glue code generation; be aware of the distinction - In isolated mode (
isolated: true),IsolatedDeclgenCheckerrequires explicit type annotations on all properties, parameters, and function return values; this enables fast validation of whether a source file is declaration-ready without running the full global checker - When
genAnnotations: true(default), UI and local annotations (@Component,@State, etc.) are generated; if unexpected annotations appear in the declaration file, check this flag
Anti-Patterns
- Using declgen to paper over errors from upstream phases (parser or checker issues should be fixed at the respective phase; declgen assumes the input AST is correct and complete)
- Introducing dependencies on checker internal private state inside
TSDeclGen(type information should be accessed viaETSChecker's public interfaces) - Calling
GenerateTsDeclarations()in the CLI tool while bypassingProceedToState(CHECKED)(type information will be incomplete, output will be wrong)
Debugging and Verification
- Run CLI tool standalone:
declgen_ets2ts --output-dets=out.d.ets --output-ets=out.ets input.ets - Invocation via
build_system: setenableDeclgenEts2Ts: trueand configuredeclgenV1OutPath, then trigger through the build system - Related tests: declgen-related test cases under
ets2panda/test/(including.d.etsand.etsexpected output comparisons) - Diagnostic definitions:
declgen_ets2ts_error.yaml(10 errors),declgen_ets2ts_warning.yaml(2 warnings),isolated_declgen.yaml(8 isolated-mode diagnostics)
Debugging Methods
- Compare the input AST (output via the
--dump-astflag) with the generated declarations to identify which AST node corresponds to which declaration output - Enable
#define DEBUG_PRINT 1indeclgenEts2Ts.cppto turn on internal debug printing - Check
diagnosticEnginefor any diagnostics of typedeclgen_ets2ts_error/declgen_ets2ts_warning/isolated_declgen - Isolated mode debugging: call
IsolatedDeclgenChecker::Check(scriptFunction)directly and inspect the returned inferred type string
Common Issues
- Generated declaration types do not match actual types: usually caused by checker not running fully (
lazyChecknot disabled) or AST not reachingCHECKEDstate - Isolated mode reports
FUNCTION_MUST_HAVE_AN_EXPLICIT_RETURN_TYPE_ANNOTATION_WITH_ISOLATED_DECL: function is missing an explicit return type annotation; add it in the source UNSUPPORTED_LOCAL_BINDINGS(error id 9): declgen encountered a binding valid only in local scope that cannot be expressed in a declaration file- Output
.d.etsfile is empty or contains only imports: no exported top-level declarations in the source file; try the--export-allargument WriteOutputFilesfails: the output path directory does not exist; ensure the parent directories ofoutputDeclEtsandoutputEtsare created beforehand
Related Documents
docs/ets2panda/Static_Frontend_Knowledge_Base.md-- upstream parser/checker/lowering pipeline contextdocs/ets2panda/Driver_Knowledge_Base.md-- build-system trigger path for declgenets2panda/declgen_ets2ts/AGENTS.md-- directory-level architecture overview and constraintsets2panda/docs/lowering-phases.md-- compiler pipeline phase overview