@harmoniclabs/pebble 0.1.10 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/IR/CompilationCtx.d.ts +40 -0
- package/dist/IR/CompilationCtx.js +54 -0
- package/dist/IR/IRHash.d.ts +23 -2
- package/dist/IR/IRHash.js +10 -60
- package/dist/IR/IRNodes/IRConst.js +35 -4
- package/dist/IR/IRNodes/IRHoisted.d.ts +0 -1
- package/dist/IR/IRNodes/IRHoisted.js +4 -6
- package/dist/IR/IRNodes/IRLetted.d.ts +0 -1
- package/dist/IR/IRNodes/IRLetted.js +4 -6
- package/dist/IR/IRNodes/IRNative/IRNativeTag.d.ts +22 -2
- package/dist/IR/IRNodes/IRNative/IRNativeTag.js +26 -2
- package/dist/IR/IRNodes/IRNative/index.d.ts +16 -1
- package/dist/IR/IRNodes/IRNative/index.js +31 -2
- package/dist/IR/IRNodes/utils/hashVarSym.d.ts +0 -1
- package/dist/IR/IRNodes/utils/hashVarSym.js +27 -33
- package/dist/IR/toUPLC/CompilerOptions.d.ts +34 -7
- package/dist/IR/toUPLC/CompilerOptions.js +19 -10
- package/dist/IR/toUPLC/compileIRToUPLC.js +39 -3
- package/dist/IR/toUPLC/subRoutines/inlineSingleUseLetBindingsAndReturnRoot.d.ts +23 -0
- package/dist/IR/toUPLC/subRoutines/inlineSingleUseLetBindingsAndReturnRoot.js +263 -0
- package/dist/IR/toUPLC/subRoutines/introduceCaseForDualHeadTailAndReturnRoot.d.ts +35 -0
- package/dist/IR/toUPLC/subRoutines/introduceCaseForDualHeadTailAndReturnRoot.js +169 -0
- package/dist/IR/toUPLC/subRoutines/replaceHoistedWithLetted.d.ts +0 -1
- package/dist/IR/toUPLC/subRoutines/replaceHoistedWithLetted.js +6 -6
- package/dist/IR/toUPLC/subRoutines/replaceNatives/nativeToIR.d.ts +2 -3
- package/dist/IR/toUPLC/subRoutines/replaceNatives/nativeToIR.js +106 -65
- package/dist/IR/toUPLC/subRoutines/rewriteHeadTailInCaseConsAndReturnRoot.d.ts +30 -0
- package/dist/IR/toUPLC/subRoutines/rewriteHeadTailInCaseConsAndReturnRoot.js +95 -0
- package/dist/IR/toUPLC/subRoutines/rewriteNativesAppliedToConstantsAndReturnRoot.js +36 -5
- package/dist/IR/toUPLC/subRoutines/rewriteToCaseOverConstAndReturnRoot.d.ts +35 -0
- package/dist/IR/toUPLC/subRoutines/rewriteToCaseOverConstAndReturnRoot.js +169 -0
- package/dist/IR/tree_utils/_ir_caseList.d.ts +15 -0
- package/dist/IR/tree_utils/_ir_caseList.js +19 -0
- package/dist/IR/tree_utils/bytesToHex.d.ts +8 -0
- package/dist/IR/tree_utils/bytesToHex.js +69 -0
- package/dist/ast/nodes/expr/functions/FuncExpr.d.ts +16 -2
- package/dist/ast/nodes/expr/functions/FuncExpr.js +17 -0
- package/dist/ast/nodes/expr/litteral/LitteralExpr.d.ts +2 -1
- package/dist/ast/nodes/expr/litteral/LitteralExpr.js +2 -0
- package/dist/ast/nodes/expr/litteral/TemplateStrExpr.d.ts +30 -0
- package/dist/ast/nodes/expr/litteral/TemplateStrExpr.js +35 -0
- package/dist/ast/nodes/statements/ExportStmt.d.ts +3 -3
- package/dist/ast/nodes/statements/PebbleStmt.d.ts +4 -3
- package/dist/ast/nodes/statements/PebbleStmt.js +6 -2
- package/dist/ast/nodes/statements/TestParam.d.ts +18 -0
- package/dist/ast/nodes/statements/TestParam.js +18 -0
- package/dist/ast/nodes/statements/TestStmt.d.ts +5 -3
- package/dist/ast/nodes/statements/TestStmt.js +3 -1
- package/dist/ast/nodes/statements/UsingStmt.d.ts +32 -2
- package/dist/ast/nodes/statements/UsingStmt.js +39 -3
- package/dist/ast/nodes/statements/declarations/NamespaceDecl.d.ts +21 -0
- package/dist/ast/nodes/statements/declarations/NamespaceDecl.js +31 -0
- package/dist/ast/nodes/statements/declarations/StructDecl.d.ts +16 -2
- package/dist/ast/nodes/statements/declarations/StructDecl.js +15 -1
- package/dist/compiler/AstCompiler/AstCompiler.d.ts +27 -0
- package/dist/compiler/AstCompiler/AstCompiler.js +244 -7
- package/dist/compiler/AstCompiler/internal/_deriveContractBody/_deriveContractBody.js +16 -5
- package/dist/compiler/AstCompiler/internal/exprs/_compileCallExpr.js +97 -6
- package/dist/compiler/AstCompiler/internal/exprs/_compileCaseExpr.js +31 -0
- package/dist/compiler/AstCompiler/internal/exprs/_compileFuncExpr.js +12 -5
- package/dist/compiler/AstCompiler/internal/exprs/_compileIsExpr.js +12 -0
- package/dist/compiler/AstCompiler/internal/exprs/_compileLitteralExpr.js +59 -0
- package/dist/compiler/AstCompiler/internal/exprs/_compilePropAccessExpr.d.ts +2 -3
- package/dist/compiler/AstCompiler/internal/exprs/_compilePropAccessExpr.js +64 -0
- package/dist/compiler/AstCompiler/internal/exprs/_compileUnaryPrefixExpr.js +13 -1
- package/dist/compiler/AstCompiler/internal/exprs/_compileVarAccessExpr.js +2 -0
- package/dist/compiler/AstCompiler/internal/exprs/binary/_compileAddExpr.js +18 -5
- package/dist/compiler/AstCompiler/internal/exprs/binary/_compileEqualExpr.js +3 -1
- package/dist/compiler/AstCompiler/internal/exprs/binary/_compileGreaterThanEqualExpr.js +2 -1
- package/dist/compiler/AstCompiler/internal/exprs/binary/_compileGreaterThanExpr.js +2 -1
- package/dist/compiler/AstCompiler/internal/exprs/binary/_compileLessThanEqualExpr.js +2 -1
- package/dist/compiler/AstCompiler/internal/exprs/binary/_compileLessThanExpr.js +2 -1
- package/dist/compiler/AstCompiler/internal/exprs/binary/_compileMultExpr.js +24 -6
- package/dist/compiler/AstCompiler/internal/exprs/binary/_compileNotEqualExpr.js +2 -1
- package/dist/compiler/AstCompiler/internal/exprs/binary/_compileSubExpr.js +16 -5
- package/dist/compiler/AstCompiler/internal/statements/_compileMatchStmt.js +33 -20
- package/dist/compiler/AstCompiler/internal/statements/_compileStatement.js +4 -1
- package/dist/compiler/AstCompiler/internal/statements/_compileTestStmt.d.ts +15 -1
- package/dist/compiler/AstCompiler/internal/statements/_compileTestStmt.js +70 -30
- package/dist/compiler/AstCompiler/internal/statements/_compileUsingAliasStmt.d.ts +11 -0
- package/dist/compiler/AstCompiler/internal/statements/_compileUsingAliasStmt.js +26 -0
- package/dist/compiler/AstCompiler/internal/statements/_compileUsingStmt.d.ts +9 -4
- package/dist/compiler/AstCompiler/internal/statements/_compileUsingStmt.js +51 -10
- package/dist/compiler/AstCompiler/internal/types/_compileDataEncodedConcreteType.js +21 -2
- package/dist/compiler/AstCompiler/internal/types/_compileSopEncodedConcreteType.js +17 -2
- package/dist/compiler/AstCompiler/scope/AstScope.d.ts +70 -1
- package/dist/compiler/AstCompiler/scope/AstScope.js +91 -0
- package/dist/compiler/AstCompiler/utils/getPropAccessReturnType.js +36 -1
- package/dist/compiler/AstCompiler/utils/monomorphizeGeneric.d.ts +36 -0
- package/dist/compiler/AstCompiler/utils/monomorphizeGeneric.js +123 -0
- package/dist/compiler/AstCompiler/utils/resolveNamespaceChain.d.ts +28 -0
- package/dist/compiler/AstCompiler/utils/resolveNamespaceChain.js +95 -0
- package/dist/compiler/AstCompiler/utils/resolveNamespacePath.d.ts +37 -0
- package/dist/compiler/AstCompiler/utils/resolveNamespacePath.js +93 -0
- package/dist/compiler/Compiler.d.ts +9 -1
- package/dist/compiler/Compiler.js +218 -30
- package/dist/compiler/TirCompiler/expressify/ExpressifyCtx.js +1 -1
- package/dist/compiler/TirCompiler/expressify/expressify.js +30 -2
- package/dist/compiler/TirCompiler/expressify/expressifyForStmt.d.ts +2 -1
- package/dist/compiler/TirCompiler/expressify/expressifyForStmt.js +45 -7
- package/dist/compiler/TirCompiler/expressify/expressifyVars.d.ts +0 -1
- package/dist/compiler/TirCompiler/expressify/expressifyVars.js +49 -15
- package/dist/compiler/test/TestResult.d.ts +38 -0
- package/dist/compiler/test/TestResult.js +6 -0
- package/dist/compiler/test/fuzz/PRNG.d.ts +26 -0
- package/dist/compiler/test/fuzz/PRNG.js +59 -0
- package/dist/compiler/tir/expressions/TirCaseExpr.d.ts +9 -0
- package/dist/compiler/tir/expressions/TirCaseExpr.js +144 -122
- package/dist/compiler/tir/expressions/TirElemAccessExpr.js +2 -2
- package/dist/compiler/tir/expressions/TirExpr.d.ts +2 -1
- package/dist/compiler/tir/expressions/TirExpr.js +2 -0
- package/dist/compiler/tir/expressions/TirFromDataExpr.js +102 -67
- package/dist/compiler/tir/expressions/TirIsExpr.js +14 -1
- package/dist/compiler/tir/expressions/TirNativeFunc.d.ts +18 -2
- package/dist/compiler/tir/expressions/TirNativeFunc.js +55 -118
- package/dist/compiler/tir/expressions/TirShowExpr.d.ts +52 -0
- package/dist/compiler/tir/expressions/TirShowExpr.js +199 -0
- package/dist/compiler/tir/expressions/TirToDataExpr.js +3 -0
- package/dist/compiler/tir/expressions/TirTraceExpr.js +11 -7
- package/dist/compiler/tir/expressions/TirTypeConversionExpr.js +10 -0
- package/dist/compiler/tir/expressions/TirVariableAccessExpr.d.ts +2 -3
- package/dist/compiler/tir/expressions/TirVariableAccessExpr.js +1 -4
- package/dist/compiler/tir/expressions/ToIRTermCtx.d.ts +20 -3
- package/dist/compiler/tir/expressions/ToIRTermCtx.js +48 -3
- package/dist/compiler/tir/expressions/binary/TirBinaryExpr.d.ts +2 -2
- package/dist/compiler/tir/expressions/binary/TirBinaryExpr.js +45 -8
- package/dist/compiler/tir/expressions/litteral/TirLitEnumMemberExpr.d.ts +19 -0
- package/dist/compiler/tir/expressions/litteral/TirLitEnumMemberExpr.js +24 -0
- package/dist/compiler/tir/expressions/litteral/TirLitteralExpr.d.ts +2 -1
- package/dist/compiler/tir/expressions/litteral/TirLitteralExpr.js +2 -0
- package/dist/compiler/tir/expressions/unary/TirUnaryMinus.js +4 -1
- package/dist/compiler/tir/program/TypedProgram.d.ts +101 -0
- package/dist/compiler/tir/program/TypedProgram.js +43 -0
- package/dist/compiler/tir/program/stdScope/populateBuiltinInterfaces.d.ts +17 -0
- package/dist/compiler/tir/program/stdScope/populateBuiltinInterfaces.js +70 -0
- package/dist/compiler/tir/program/stdScope/populateStdNamespace.d.ts +22 -0
- package/dist/compiler/tir/program/stdScope/populateStdNamespace.js +619 -0
- package/dist/compiler/tir/program/stdScope/prelude/preludeTypesSrc.js +35 -2
- package/dist/compiler/tir/program/stdScope/stdScope.d.ts +8 -0
- package/dist/compiler/tir/program/stdScope/stdScope.js +84 -41
- package/dist/compiler/tir/statements/TirStmt.js +0 -1
- package/dist/compiler/tir/statements/TirTestStmt.d.ts +46 -0
- package/dist/compiler/tir/statements/TirTestStmt.js +35 -0
- package/dist/compiler/tir/types/TirEnumType.d.ts +21 -0
- package/dist/compiler/tir/types/TirEnumType.js +36 -0
- package/dist/compiler/tir/types/TirNativeType/TirNativeType.d.ts +53 -2
- package/dist/compiler/tir/types/TirNativeType/TirNativeType.js +58 -1
- package/dist/compiler/tir/types/TirNativeType/native/array.d.ts +16 -0
- package/dist/compiler/tir/types/TirNativeType/native/array.js +38 -0
- package/dist/compiler/tir/types/TirNativeType/native/index.d.ts +2 -0
- package/dist/compiler/tir/types/TirNativeType/native/index.js +2 -0
- package/dist/compiler/tir/types/TirNativeType/native/value.d.ts +18 -0
- package/dist/compiler/tir/types/TirNativeType/native/value.js +17 -0
- package/dist/compiler/tir/types/TirStructType.js +6 -1
- package/dist/compiler/tir/types/TirType.d.ts +3 -2
- package/dist/compiler/tir/types/TirType.js +7 -2
- package/dist/compiler/tir/types/utils/canAssignTo.js +36 -1
- package/dist/compiler/tir/types/utils/canCastTo.js +14 -1
- package/dist/compiler/tir/types/utils/getDeconstructableType.d.ts +2 -1
- package/dist/compiler/tir/types/utils/getDeconstructableType.js +2 -0
- package/dist/compiler/tir/types/utils/inferTypeArgs.d.ts +19 -0
- package/dist/compiler/tir/types/utils/inferTypeArgs.js +83 -0
- package/dist/compiler/tir/types/utils/normalizeEnumToInt.d.ts +10 -0
- package/dist/compiler/tir/types/utils/normalizeEnumToInt.js +17 -0
- package/dist/compiler/tir/types/utils/substituteTypeParams.d.ts +9 -0
- package/dist/compiler/tir/types/utils/substituteTypeParams.js +67 -0
- package/dist/diagnostics/diagnosticMessages.generated.d.ts +10 -0
- package/dist/diagnostics/diagnosticMessages.generated.js +20 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/parser/Parser.d.ts +73 -3
- package/dist/parser/Parser.js +362 -46
- package/dist/tokenizer/Token.d.ts +106 -102
- package/dist/tokenizer/Token.js +111 -109
- package/dist/tokenizer/utils/tokenFromKeyword.js +11 -6
- package/dist/utils/semverSatisfies.d.ts +1 -0
- package/dist/utils/semverSatisfies.js +161 -0
- package/dist/version.generated.d.ts +1 -0
- package/dist/version.generated.js +2 -0
- package/package.json +5 -4
- package/dist/IR/tree_utils/_ir_lazyChooseList.d.ts +0 -3
- package/dist/IR/tree_utils/_ir_lazyChooseList.js +0 -7
|
@@ -14,6 +14,7 @@ import { LitIntExpr } from "../../../../ast/nodes/expr/litteral/LitIntExpr.js";
|
|
|
14
14
|
import { LitNamedObjExpr } from "../../../../ast/nodes/expr/litteral/LitNamedObjExpr.js";
|
|
15
15
|
import { LitObjExpr } from "../../../../ast/nodes/expr/litteral/LitObjExpr.js";
|
|
16
16
|
import { LitStrExpr } from "../../../../ast/nodes/expr/litteral/LitStrExpr.js";
|
|
17
|
+
import { TemplateStrExpr } from "../../../../ast/nodes/expr/litteral/TemplateStrExpr.js";
|
|
17
18
|
import { LitThisExpr } from "../../../../ast/nodes/expr/litteral/LitThisExpr.js";
|
|
18
19
|
import { LitTrueExpr } from "../../../../ast/nodes/expr/litteral/LitTrueExpr.js";
|
|
19
20
|
import { LitUndefExpr } from "../../../../ast/nodes/expr/litteral/LitUndefExpr.js";
|
|
@@ -44,7 +45,7 @@ import { IfStmt } from "../../../../ast/nodes/statements/IfStmt.js";
|
|
|
44
45
|
import { MatchStmtCase, MatchStmt, MatchStmtElseCase } from "../../../../ast/nodes/statements/MatchStmt.js";
|
|
45
46
|
import { ReturnStmt } from "../../../../ast/nodes/statements/ReturnStmt.js";
|
|
46
47
|
import { TraceStmt } from "../../../../ast/nodes/statements/TraceStmt.js";
|
|
47
|
-
import { UsingStmt } from "../../../../ast/nodes/statements/UsingStmt.js";
|
|
48
|
+
import { UsingAliasStmt, UsingStmt } from "../../../../ast/nodes/statements/UsingStmt.js";
|
|
48
49
|
import { VarStmt } from "../../../../ast/nodes/statements/VarStmt.js";
|
|
49
50
|
import { WhileStmt } from "../../../../ast/nodes/statements/WhileStmt.js";
|
|
50
51
|
import { SourceRange } from "../../../../ast/Source/SourceRange.js";
|
|
@@ -457,7 +458,8 @@ function _getMatchedRedeemerBlockStatements(compiler, stmts, paramsInternalNames
|
|
|
457
458
|
if (stmt instanceof BreakStmt
|
|
458
459
|
|| stmt instanceof ContinueStmt
|
|
459
460
|
|| stmt instanceof EmptyStmt
|
|
460
|
-
|| stmt instanceof UsingStmt
|
|
461
|
+
|| stmt instanceof UsingStmt
|
|
462
|
+
|| stmt instanceof UsingAliasStmt)
|
|
461
463
|
continue;
|
|
462
464
|
if (isImplicitAssignmentStmt(stmt))
|
|
463
465
|
continue;
|
|
@@ -766,6 +768,15 @@ function _exprReplaceParamsAndAssertNoLitContext(compiler, expr, paramsInternalN
|
|
|
766
768
|
expr.body = new BlockStmt(nextBodyStmts, expr.body.range);
|
|
767
769
|
return expr;
|
|
768
770
|
}
|
|
771
|
+
if (expr instanceof TemplateStrExpr) {
|
|
772
|
+
for (let i = 0; i < expr.exprs.length; i++) {
|
|
773
|
+
const newSub = _exprReplaceParamsAndAssertNoLitContext(compiler, expr.exprs[i], paramsInternalNamesMap, renamedVariables);
|
|
774
|
+
if (!newSub)
|
|
775
|
+
return undefined;
|
|
776
|
+
expr.exprs[i] = newSub;
|
|
777
|
+
}
|
|
778
|
+
return expr;
|
|
779
|
+
}
|
|
769
780
|
const tsEnsureExhaustiveCheck = expr;
|
|
770
781
|
console.error(expr);
|
|
771
782
|
throw new Error("unreachable::_exprReplaceParamsAndAssertNoLitContext");
|
|
@@ -773,7 +784,7 @@ function _exprReplaceParamsAndAssertNoLitContext(compiler, expr, paramsInternalN
|
|
|
773
784
|
function _deriveContractDatumTypeDef(contractName, stateDecls, contractRange) {
|
|
774
785
|
let defFlags = StructDeclAstFlags.onlyDataEncoding;
|
|
775
786
|
if (stateDecls.length <= 1)
|
|
776
|
-
defFlags |= StructDeclAstFlags.
|
|
787
|
+
defFlags |= StructDeclAstFlags.shortcutSingleConstructor;
|
|
777
788
|
return new StructDecl(new Identifier(contractName, contractRange), [], // typeParams
|
|
778
789
|
stateDecls.map(s => new StructConstrDecl(new Identifier(s.name.text, s.name.range), s.fields, s.range)), defFlags, contractRange);
|
|
779
790
|
}
|
|
@@ -844,7 +855,7 @@ function _buildSpendCaseBlock(compiler, contractDecl, paramsInternalNamesMap, ba
|
|
|
844
855
|
const perStateStructDecl = new StructDecl(new Identifier(getUniqueInternalName(`${contractDecl.name.text}_${stateDecl.name.text}`), mockRange), [], // typeParams
|
|
845
856
|
[
|
|
846
857
|
new StructConstrDecl(new Identifier(stateDecl.name.text, mockRange), stateDecl.fields, stateDecl.range)
|
|
847
|
-
], StructDeclAstFlags.onlyDataEncoding | StructDeclAstFlags.
|
|
858
|
+
], StructDeclAstFlags.onlyDataEncoding | StructDeclAstFlags.shortcutSingleConstructor, stateDecl.range);
|
|
848
859
|
compiler.registerInternalTypeDecl(perStateStructDecl);
|
|
849
860
|
const perStateStructName = perStateStructDecl.name.text;
|
|
850
861
|
// synthesize: const state = StateStruct{ field1: _f1, ... };
|
|
@@ -910,7 +921,7 @@ function _buildSpendCaseBlock(compiler, contractDecl, paramsInternalNamesMap, ba
|
|
|
910
921
|
function _deriveRedeemerTypeDef(redeemerName, methods, contractRange) {
|
|
911
922
|
let defFlags = StructDeclAstFlags.onlyDataEncoding;
|
|
912
923
|
if (methods.length <= 1)
|
|
913
|
-
defFlags |= StructDeclAstFlags.
|
|
924
|
+
defFlags |= StructDeclAstFlags.shortcutSingleConstructor;
|
|
914
925
|
const uniqueName = getUniqueInternalName(redeemerName);
|
|
915
926
|
return new StructDecl(new Identifier(uniqueName, SourceRange.mock), [], // typeParams
|
|
916
927
|
methods.map(m => {
|
|
@@ -2,12 +2,13 @@ import { DiagnosticCode } from "../../../../diagnostics/diagnosticMessages.gener
|
|
|
2
2
|
import { TirCallExpr } from "../../../tir/expressions/TirCallExpr.js";
|
|
3
3
|
import { TirFuncT } from "../../../tir/types/TirNativeType/native/function.js";
|
|
4
4
|
import { canAssignTo } from "../../../tir/types/utils/canAssignTo.js";
|
|
5
|
+
import { inferTypeArgs } from "../../../tir/types/utils/inferTypeArgs.js";
|
|
6
|
+
import { substituteTypeParams } from "../../../tir/types/utils/substituteTypeParams.js";
|
|
7
|
+
import { TirVariableAccessExpr } from "../../../tir/expressions/TirVariableAccessExpr.js";
|
|
8
|
+
import { monomorphizeGeneric } from "../../utils/monomorphizeGeneric.js";
|
|
9
|
+
import { _compileDataEncodedConcreteType } from "../types/_compileDataEncodedConcreteType.js";
|
|
5
10
|
import { _compileExpr } from "./_compileExpr.js";
|
|
6
11
|
export function _compileCallExpr(ctx, expr, typeHint) {
|
|
7
|
-
// TODO: expr.genericTypeArgs
|
|
8
|
-
// expr.funcExpr;
|
|
9
|
-
// expr.args;
|
|
10
|
-
// expr.range;
|
|
11
12
|
if (!(typeHint instanceof TirFuncT))
|
|
12
13
|
typeHint = undefined;
|
|
13
14
|
const funcExpr = _compileExpr(ctx, expr.funcExpr, typeHint);
|
|
@@ -15,7 +16,97 @@ export function _compileCallExpr(ctx, expr, typeHint) {
|
|
|
15
16
|
return undefined;
|
|
16
17
|
if (!(funcExpr.type instanceof TirFuncT))
|
|
17
18
|
return ctx.error(DiagnosticCode.Expression_is_not_callable, expr.funcExpr.range);
|
|
18
|
-
|
|
19
|
+
// ---- Generic-callee handling --------------------------------------
|
|
20
|
+
// If the callee resolved to a generic placeholder, the funcExpr is a
|
|
21
|
+
// `TirVariableAccessExpr` whose `variableInfos.genericTemplateName` names
|
|
22
|
+
// a template registered on `program.genericTemplates`. We instantiate it
|
|
23
|
+
// here with explicit or inferred type arguments and replace `funcExpr`
|
|
24
|
+
// with a synthetic access pointing at the concrete monomorphized instance.
|
|
25
|
+
let callee = funcExpr;
|
|
26
|
+
let funcType = funcExpr.type;
|
|
27
|
+
if (funcExpr instanceof TirVariableAccessExpr) {
|
|
28
|
+
const templateName = funcExpr.resolvedValue.variableInfos.genericTemplateName;
|
|
29
|
+
if (typeof templateName === "string") {
|
|
30
|
+
const template = ctx.program.genericTemplates.get(templateName);
|
|
31
|
+
if (!template)
|
|
32
|
+
return ctx.error(DiagnosticCode._0_is_not_defined, expr.funcExpr.range, templateName);
|
|
33
|
+
const nParams = template.typeParams.length;
|
|
34
|
+
// 1) Collect explicit type-args, if any
|
|
35
|
+
const explicitArgs = (Array.isArray(expr.genericTypeArgs)
|
|
36
|
+
? new Array()
|
|
37
|
+
: undefined);
|
|
38
|
+
if (explicitArgs && expr.genericTypeArgs) {
|
|
39
|
+
for (const tArgExpr of expr.genericTypeArgs) {
|
|
40
|
+
const t = _compileDataEncodedConcreteType(ctx, tArgExpr, true);
|
|
41
|
+
if (!t)
|
|
42
|
+
return undefined;
|
|
43
|
+
explicitArgs.push(t);
|
|
44
|
+
}
|
|
45
|
+
if (explicitArgs.length !== nParams) {
|
|
46
|
+
return ctx.error(DiagnosticCode.Expected_0_type_arguments_but_got_1, expr.funcExpr.range, String(nParams), String(explicitArgs.length));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
// 2) Compile arguments. We need them anyway, and they're used for
|
|
50
|
+
// inference when no explicit args were given.
|
|
51
|
+
const tirArgs = expr.args.map((arg, i) => {
|
|
52
|
+
const expected = explicitArgs
|
|
53
|
+
? substituteTypeParams(funcType.argTypes[i], new Map(template.typeParams.map((tp, idx) => [tp.symbol, explicitArgs[idx]])))
|
|
54
|
+
: undefined;
|
|
55
|
+
return _compileExpr(ctx, arg, expected);
|
|
56
|
+
});
|
|
57
|
+
for (const a of tirArgs)
|
|
58
|
+
if (!a)
|
|
59
|
+
return undefined;
|
|
60
|
+
// 3) Determine final type arguments
|
|
61
|
+
let resolvedArgs;
|
|
62
|
+
if (explicitArgs) {
|
|
63
|
+
resolvedArgs = explicitArgs;
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
const env = new Map();
|
|
67
|
+
const usable = Math.min(tirArgs.length, funcType.argTypes.length);
|
|
68
|
+
for (let i = 0; i < usable; i++) {
|
|
69
|
+
if (!inferTypeArgs(funcType.argTypes[i], tirArgs[i].type, env)) {
|
|
70
|
+
return ctx.error(DiagnosticCode.Type_0_is_not_assignable_to_type_1, expr.args[i].range, tirArgs[i].type.toString(), funcType.argTypes[i].toString());
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
resolvedArgs = new Array(nParams);
|
|
74
|
+
for (let i = 0; i < nParams; i++) {
|
|
75
|
+
const bound = env.get(template.typeParams[i].symbol);
|
|
76
|
+
if (!bound) {
|
|
77
|
+
return ctx.error(DiagnosticCode.The_type_argument_for_type_parameter_0_cannot_be_inferred_from_the_usage_Consider_specifying_the_type_arguments_explicitly, expr.funcExpr.range, template.typeParams[i].name);
|
|
78
|
+
}
|
|
79
|
+
resolvedArgs[i] = bound;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
// 4) Monomorphize
|
|
83
|
+
const mono = monomorphizeGeneric(ctx, template, resolvedArgs, expr.range);
|
|
84
|
+
if (!mono)
|
|
85
|
+
return undefined;
|
|
86
|
+
funcType = mono.concreteFuncType;
|
|
87
|
+
// 5) Synthesize a TirVariableAccessExpr for the concrete instance.
|
|
88
|
+
// The IR layer looks up by name on `program.functions`, so it
|
|
89
|
+
// matches the entry registered by `monomorphizeGeneric`.
|
|
90
|
+
callee = new TirVariableAccessExpr({
|
|
91
|
+
variableInfos: {
|
|
92
|
+
name: mono.tirFuncName,
|
|
93
|
+
type: funcType,
|
|
94
|
+
isConstant: true,
|
|
95
|
+
},
|
|
96
|
+
isDefinedOutsideFuncScope: true,
|
|
97
|
+
}, expr.funcExpr.range);
|
|
98
|
+
// 6) Re-validate argument assignability against concrete signature
|
|
99
|
+
const finalCallExprType = funcType.argTypes.length === tirArgs.length
|
|
100
|
+
? funcType.returnType
|
|
101
|
+
: new TirFuncT(funcType.argTypes.slice(tirArgs.length), funcType.returnType);
|
|
102
|
+
for (let i = 0; i < tirArgs.length && i < funcType.argTypes.length; i++) {
|
|
103
|
+
if (!canAssignTo(tirArgs[i].type, funcType.argTypes[i]))
|
|
104
|
+
return ctx.error(DiagnosticCode.Type_0_is_not_assignable_to_type_1, expr.args[i].range, tirArgs[i].type.toString(), funcType.argTypes[i].toString());
|
|
105
|
+
}
|
|
106
|
+
return new TirCallExpr(callee, tirArgs, finalCallExprType, expr.range);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
// ---- End generic-callee handling ----------------------------------
|
|
19
110
|
for (let i = funcType.argTypes.length; i < expr.args.length; i++) {
|
|
20
111
|
ctx.warning(DiagnosticCode.Unexpected_argument, expr.args[i].range); // not a big deal
|
|
21
112
|
}
|
|
@@ -32,5 +123,5 @@ export function _compileCallExpr(ctx, expr, typeHint) {
|
|
|
32
123
|
if (!canAssignTo(arg.type, funcType.argTypes[i]))
|
|
33
124
|
return ctx.error(DiagnosticCode.Type_0_is_not_assignable_to_type_1, expr.args[i].range, arg.type.toString(), funcType.argTypes[i].toString());
|
|
34
125
|
}
|
|
35
|
-
return new TirCallExpr(
|
|
126
|
+
return new TirCallExpr(callee, args, finalCallExprType, expr.range);
|
|
36
127
|
}
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { Identifier } from "../../../../ast/nodes/common/Identifier.js";
|
|
2
2
|
import { ParentesizedExpr } from "../../../../ast/nodes/expr/ParentesizedExpr.js";
|
|
3
|
+
import { NamedDeconstructVarDecl as AstNamedDeconstructVarDecl } from "../../../../ast/nodes/statements/declarations/VarDecl/NamedDeconstructVarDecl.js";
|
|
4
|
+
import { SimpleVarDecl as AstSimpleVarDecl } from "../../../../ast/nodes/statements/declarations/VarDecl/SimpleVarDecl.js";
|
|
3
5
|
import { DiagnosticCode } from "../../../../diagnostics/diagnosticMessages.generated.js";
|
|
4
6
|
import { TirCaseExpr, TirCaseMatcher, TirWildcardCaseMatcher } from "../../../tir/expressions/TirCaseExpr.js";
|
|
5
7
|
import { TirNamedDeconstructVarDecl } from "../../../tir/statements/TirVarDecl/TirNamedDeconstructVarDecl.js";
|
|
6
8
|
import { TirSimpleVarDecl } from "../../../tir/statements/TirVarDecl/TirSimpleVarDecl.js";
|
|
7
9
|
import { TirDataStructType, TirSoPStructType } from "../../../tir/types/TirStructType.js";
|
|
10
|
+
import { getEnumType } from "../../../tir/types/TirEnumType.js";
|
|
8
11
|
import { canAssignTo, getStructType } from "../../../tir/types/utils/canAssignTo.js";
|
|
9
12
|
import { _compileVarDecl } from "../statements/_compileVarStmt.js";
|
|
10
13
|
import { _compileExpr } from "./_compileExpr.js";
|
|
@@ -29,6 +32,34 @@ export function _compileCaseExpr(ctx, expr, typeHint) {
|
|
|
29
32
|
return new TirCaseExpr(matchExpr, cases, wildcardCase, returnType, expr.range);
|
|
30
33
|
}
|
|
31
34
|
export function _compileCaseExprMatcher(ctx, matcher, patternType, returnTypeHint, matchedVarName) {
|
|
35
|
+
const enumType = getEnumType(patternType);
|
|
36
|
+
if (enumType) {
|
|
37
|
+
const astPattern = matcher.pattern;
|
|
38
|
+
let memberName;
|
|
39
|
+
let patternRange = astPattern.range;
|
|
40
|
+
let ctorNameRange = astPattern.range;
|
|
41
|
+
if (astPattern instanceof AstSimpleVarDecl) {
|
|
42
|
+
memberName = astPattern.name.text;
|
|
43
|
+
patternRange = astPattern.name.range;
|
|
44
|
+
ctorNameRange = astPattern.name.range;
|
|
45
|
+
}
|
|
46
|
+
else if (astPattern instanceof AstNamedDeconstructVarDecl) {
|
|
47
|
+
if (astPattern.fields.size > 0 || astPattern.rest)
|
|
48
|
+
return ctx.error(DiagnosticCode.Enum_member_pattern_cannot_have_fields, astPattern.range);
|
|
49
|
+
memberName = astPattern.name.text;
|
|
50
|
+
ctorNameRange = astPattern.name.range;
|
|
51
|
+
}
|
|
52
|
+
else
|
|
53
|
+
return ctx.error(DiagnosticCode._case_expression_must_decontructed_the_inspected_value, astPattern.range);
|
|
54
|
+
if (enumType.indexOf(memberName) < 0)
|
|
55
|
+
return ctx.error(DiagnosticCode.Constructor_0_is_not_part_of_the_definition_of_1, ctorNameRange, memberName, enumType.toString());
|
|
56
|
+
const body = _compileExpr(ctx, matcher.body, returnTypeHint);
|
|
57
|
+
if (!body)
|
|
58
|
+
return undefined;
|
|
59
|
+
if (returnTypeHint && !canAssignTo(body.type, returnTypeHint))
|
|
60
|
+
return ctx.error(DiagnosticCode.Type_0_is_not_assignable_to_type_1, matcher.body.range, body.type.toString(), returnTypeHint.toString());
|
|
61
|
+
return new TirCaseMatcher(new TirNamedDeconstructVarDecl(memberName, new Map(), undefined, enumType, undefined, true, patternRange, ctorNameRange), body, matcher.range);
|
|
62
|
+
}
|
|
32
63
|
const pattern = _compileVarDecl(ctx, matcher.pattern, patternType);
|
|
33
64
|
if (!pattern)
|
|
34
65
|
return undefined;
|
|
@@ -9,7 +9,6 @@ import { TirReturnStmt } from "../../../tir/statements/TirReturnStmt.js";
|
|
|
9
9
|
import { TirSimpleVarDecl } from "../../../tir/statements/TirVarDecl/TirSimpleVarDecl.js";
|
|
10
10
|
import { TirFuncT } from "../../../tir/types/TirNativeType/native/function.js";
|
|
11
11
|
import { void_t } from "../../../tir/program/stdScope/stdScope.js";
|
|
12
|
-
import { TirTypeParam } from "../../../tir/types/TirTypeParam.js";
|
|
13
12
|
import { getUnaliased } from "../../../tir/types/utils/getUnaliased.js";
|
|
14
13
|
import { _compileBlockStmt } from "../statements/_compileBlockStmt.js";
|
|
15
14
|
import { _compileVarDecl } from "../statements/_compileVarStmt.js";
|
|
@@ -91,8 +90,13 @@ export function _compileFuncExpr(ctx, expr, expectedFuncType, isMethod = false)
|
|
|
91
90
|
isConstant: true,
|
|
92
91
|
});
|
|
93
92
|
// if( _hasDuplicateTypeParams( ctx, expr.typeParams ) ) return undefined;
|
|
93
|
+
// Generic functions are compiled per-instantiation by `monomorphizeGeneric`.
|
|
94
|
+
// The cloned `FuncExpr` it produces has `typeParams = []`, so we only need
|
|
95
|
+
// to refuse the (rare) case where someone directly calls `_compileFuncExpr`
|
|
96
|
+
// on a still-generic AST node (e.g. an anonymous lambda annotated with type
|
|
97
|
+
// params, which is not supported yet).
|
|
94
98
|
if (expr.typeParams.length > 0)
|
|
95
|
-
return ctx.error(DiagnosticCode.Not_implemented_0, expr.typeParams[0].range, "generic functions");
|
|
99
|
+
return ctx.error(DiagnosticCode.Not_implemented_0, expr.typeParams[0].range, "generic lambda expressions (top-level generic functions are supported)");
|
|
96
100
|
const destructuredParamsResult = _getDestructuredParamsAsVarDecls(funcCtx, expr, expectedFuncType);
|
|
97
101
|
if (!destructuredParamsResult)
|
|
98
102
|
return undefined;
|
|
@@ -140,8 +144,10 @@ function _compileFuncExprInferReturnType(ctx, expr, isMethod) {
|
|
|
140
144
|
type: tempFuncType,
|
|
141
145
|
isConstant: true,
|
|
142
146
|
});
|
|
147
|
+
// Inferred-return-type variant: generics are not supported here because
|
|
148
|
+
// signature inference depends on knowing the type-param substitution.
|
|
143
149
|
if (expr.typeParams.length > 0)
|
|
144
|
-
return ctx.error(DiagnosticCode.Not_implemented_0, expr.typeParams[0].range, "generic functions");
|
|
150
|
+
return ctx.error(DiagnosticCode.Not_implemented_0, expr.typeParams[0].range, "generic functions with inferred return type — annotate the return type");
|
|
145
151
|
const destructuredParamsResult = _getDestructuredParamsAsVarDecls(funcCtx, expr, tempFuncType);
|
|
146
152
|
if (!destructuredParamsResult)
|
|
147
153
|
return undefined;
|
|
@@ -234,8 +240,9 @@ export function getDataFuncSignature(ctx, signature) {
|
|
|
234
240
|
const returnType = signature.returnType instanceof AstFuncType ?
|
|
235
241
|
getDataFuncSignature(ctx, signature.returnType) :
|
|
236
242
|
_compileSopEncodedConcreteType(ctx, signature.returnType);
|
|
237
|
-
if (!returnType
|
|
238
|
-
|| returnType instanceof TirTypeParam)
|
|
243
|
+
if (!returnType)
|
|
239
244
|
return undefined;
|
|
245
|
+
// NOTE: a bare `TirTypeParam` return type is valid here — it marks the
|
|
246
|
+
// template position. `monomorphizeGeneric` substitutes it at call sites.
|
|
240
247
|
return new TirFuncT(paramTypes, returnType);
|
|
241
248
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { DiagnosticCode } from "../../../../diagnostics/diagnosticMessages.generated.js";
|
|
2
2
|
import { TirIsExpr } from "../../../tir/expressions/TirIsExpr.js";
|
|
3
|
+
import { getEnumType } from "../../../tir/types/TirEnumType.js";
|
|
3
4
|
import { getStructType } from "../../../tir/types/utils/canAssignTo.js";
|
|
4
5
|
import { _compileExpr } from "./_compileExpr.js";
|
|
5
6
|
export function _compileIsExpr(ctx, expr, _typeHint) {
|
|
@@ -7,6 +8,17 @@ export function _compileIsExpr(ctx, expr, _typeHint) {
|
|
|
7
8
|
const target = _compileExpr(ctx, expr.instanceExpr, undefined);
|
|
8
9
|
if (!target)
|
|
9
10
|
return undefined;
|
|
11
|
+
const enumType = getEnumType(target.type);
|
|
12
|
+
if (enumType) {
|
|
13
|
+
const memberName = expr.ofConstr.text;
|
|
14
|
+
const memberIdx = enumType.indexOf(memberName);
|
|
15
|
+
if (memberIdx < 0)
|
|
16
|
+
return ctx.error(DiagnosticCode.Constructor_0_is_not_part_of_the_definition_of_1, expr.ofConstr.range, memberName, enumType.toString());
|
|
17
|
+
if (enumType.members.length === 1) {
|
|
18
|
+
ctx.warning(DiagnosticCode.This_check_is_redundant_Struct_0_has_only_one_possible_constructor, expr.range, enumType.toString());
|
|
19
|
+
}
|
|
20
|
+
return new TirIsExpr(target, memberName, memberIdx, expr.range, bool_t);
|
|
21
|
+
}
|
|
10
22
|
const structType = getStructType(target.type);
|
|
11
23
|
if (!structType)
|
|
12
24
|
return ctx.error(DiagnosticCode.Cannot_use_is_operator_on_a_value_that_is_not_a_struct_type, expr.instanceExpr.range);
|
|
@@ -7,6 +7,7 @@ import { LitIntExpr } from "../../../../ast/nodes/expr/litteral/LitIntExpr.js";
|
|
|
7
7
|
import { LitNamedObjExpr } from "../../../../ast/nodes/expr/litteral/LitNamedObjExpr.js";
|
|
8
8
|
import { LitObjExpr } from "../../../../ast/nodes/expr/litteral/LitObjExpr.js";
|
|
9
9
|
import { LitStrExpr } from "../../../../ast/nodes/expr/litteral/LitStrExpr.js";
|
|
10
|
+
import { TemplateStrExpr } from "../../../../ast/nodes/expr/litteral/TemplateStrExpr.js";
|
|
10
11
|
import { LitThisExpr } from "../../../../ast/nodes/expr/litteral/LitThisExpr.js";
|
|
11
12
|
import { LitTrueExpr } from "../../../../ast/nodes/expr/litteral/LitTrueExpr.js";
|
|
12
13
|
import { LitUndefExpr } from "../../../../ast/nodes/expr/litteral/LitUndefExpr.js";
|
|
@@ -66,6 +67,8 @@ export function _compileLitteralExpr(ctx, expr, typeHint) {
|
|
|
66
67
|
return _compileLitteralNamedObjExpr(ctx, expr, typeHint);
|
|
67
68
|
if (expr instanceof LitFailExpr)
|
|
68
69
|
return new TirLitFailExpr(expr.range);
|
|
70
|
+
if (expr instanceof TemplateStrExpr)
|
|
71
|
+
return _compileTemplateStrExpr(ctx, expr);
|
|
69
72
|
if (expr instanceof LitContextExpr)
|
|
70
73
|
throw new Error("Litteral `context` should have been handled while desugaring.");
|
|
71
74
|
const tsEnsureExhautstiveCheck = expr;
|
|
@@ -226,3 +229,59 @@ export function _compileLitteralArrayExpr(ctx, expr, typeHint) {
|
|
|
226
229
|
return undefined;
|
|
227
230
|
return new TirLitArrExpr([fstCompiledExpr, ...compiledRestElems], listType, expr.range);
|
|
228
231
|
}
|
|
232
|
+
/**
|
|
233
|
+
* Compile a template string `` `text ${e1} more ${e2} ...` `` into a chain
|
|
234
|
+
* of `appendByteString` calls.
|
|
235
|
+
*
|
|
236
|
+
* Each text fragment becomes a `TirLitHexBytesExpr` containing its UTF-8
|
|
237
|
+
* encoding. Each interpolation is compiled and, if its type is not already
|
|
238
|
+
* `bytes`, wrapped in a `TirShowExpr` so it lowers via the built-in `Show`
|
|
239
|
+
* interface (or the user's `type X implements Show { ... }` override).
|
|
240
|
+
*
|
|
241
|
+
* Empty fragments collapse out — we don't emit `appendByteString("", x)`.
|
|
242
|
+
*/
|
|
243
|
+
function _compileTemplateStrExpr(ctx, expr) {
|
|
244
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
245
|
+
const { fromUtf8 } = require("@harmoniclabs/uint8array-utils");
|
|
246
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
247
|
+
const { TirShowExpr } = require("../../../tir/expressions/TirShowExpr.js");
|
|
248
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
249
|
+
const { TirCallExpr } = require("../../../tir/expressions/TirCallExpr.js");
|
|
250
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
251
|
+
const { TirNativeFunc } = require("../../../tir/expressions/TirNativeFunc.js");
|
|
252
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
253
|
+
const { TirBytesT } = require("../../../tir/types/TirNativeType/native/bytes.js");
|
|
254
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
255
|
+
const { bytes_t } = require("../../../tir/program/stdScope/stdScope.js");
|
|
256
|
+
const pieces = [];
|
|
257
|
+
for (let i = 0; i < expr.parts.length; i++) {
|
|
258
|
+
const text = expr.parts[i];
|
|
259
|
+
if (text.length > 0) {
|
|
260
|
+
pieces.push(new TirLitHexBytesExpr(fromUtf8(text), expr.range));
|
|
261
|
+
}
|
|
262
|
+
if (i < expr.exprs.length) {
|
|
263
|
+
const sub = _compileExpr(ctx, expr.exprs[i], undefined);
|
|
264
|
+
if (!sub)
|
|
265
|
+
return undefined;
|
|
266
|
+
const subType = getUnaliased(sub.type);
|
|
267
|
+
if (subType instanceof TirBytesT) {
|
|
268
|
+
// Already valid UTF-8 by convention — pass through.
|
|
269
|
+
pieces.push(sub);
|
|
270
|
+
}
|
|
271
|
+
else {
|
|
272
|
+
// Auto-show via the built-in Show interface dispatch.
|
|
273
|
+
pieces.push(new TirShowExpr(sub, expr.exprs[i].range));
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
if (pieces.length === 0) {
|
|
278
|
+
// Empty template — emit empty bytes.
|
|
279
|
+
return new TirLitHexBytesExpr(new Uint8Array(0), expr.range);
|
|
280
|
+
}
|
|
281
|
+
// Left-fold appendByteString.
|
|
282
|
+
let acc = pieces[0];
|
|
283
|
+
for (let i = 1; i < pieces.length; i++) {
|
|
284
|
+
acc = new TirCallExpr(TirNativeFunc.appendByteString, [acc, pieces[i]], bytes_t, expr.range);
|
|
285
|
+
}
|
|
286
|
+
return acc;
|
|
287
|
+
}
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { PropAccessExpr, OptionalPropAccessExpr, NonNullPropAccessExpr, DotPropAccessExpr } from "../../../../ast/nodes/expr/PropAccessExpr.js";
|
|
2
2
|
import { TirExpr } from "../../../tir/expressions/TirExpr.js";
|
|
3
|
-
import { TirPropAccessExpr } from "../../../tir/expressions/TirPropAccessExpr.js";
|
|
4
3
|
import { TirType } from "../../../tir/types/TirType.js";
|
|
5
4
|
import { AstCompilationCtx } from "../../AstCompilationCtx.js";
|
|
6
5
|
export declare function _compilePropAccessExpr(ctx: AstCompilationCtx, expr: PropAccessExpr, typeHint: TirType | undefined): TirExpr | undefined;
|
|
7
6
|
export declare function _compileOptionalPropAccessExpr(ctx: AstCompilationCtx, expr: OptionalPropAccessExpr, _typeHint: TirType | undefined): TirExpr | undefined;
|
|
8
|
-
export declare function _compileNonNullPropAccessExpr(ctx: AstCompilationCtx, expr: NonNullPropAccessExpr, _typeHint: TirType | undefined):
|
|
9
|
-
export declare function _compileDotPropAccessExpr(ctx: AstCompilationCtx, expr: DotPropAccessExpr, _typeHint: TirType | undefined):
|
|
7
|
+
export declare function _compileNonNullPropAccessExpr(ctx: AstCompilationCtx, expr: NonNullPropAccessExpr, _typeHint: TirType | undefined): TirExpr | undefined;
|
|
8
|
+
export declare function _compileDotPropAccessExpr(ctx: AstCompilationCtx, expr: DotPropAccessExpr, _typeHint: TirType | undefined): TirExpr | undefined;
|
|
@@ -1,18 +1,24 @@
|
|
|
1
1
|
import { OptionalPropAccessExpr, NonNullPropAccessExpr, DotPropAccessExpr } from "../../../../ast/nodes/expr/PropAccessExpr.js";
|
|
2
2
|
import { NonNullExpr } from "../../../../ast/nodes/expr/unary/NonNullExpr.js";
|
|
3
3
|
import { DiagnosticCode } from "../../../../diagnostics/diagnosticMessages.generated.js";
|
|
4
|
+
import { TirCallExpr } from "../../../tir/expressions/TirCallExpr.js";
|
|
4
5
|
import { TirCaseExpr, TirCaseMatcher } from "../../../tir/expressions/TirCaseExpr.js";
|
|
6
|
+
import { TirNativeFunc } from "../../../tir/expressions/TirNativeFunc.js";
|
|
5
7
|
import { TirPropAccessExpr } from "../../../tir/expressions/TirPropAccessExpr.js";
|
|
6
8
|
import { TirVariableAccessExpr } from "../../../tir/expressions/TirVariableAccessExpr.js";
|
|
7
9
|
import { TirNamedDeconstructVarDecl } from "../../../tir/statements/TirVarDecl/TirNamedDeconstructVarDecl.js";
|
|
8
10
|
import { TirSimpleVarDecl } from "../../../tir/statements/TirVarDecl/TirSimpleVarDecl.js";
|
|
11
|
+
import { TirUnConstrDataResultT } from "../../../tir/types/TirNativeType/index.js";
|
|
9
12
|
import { getOptTypeArg } from "../../../tir/types/utils/getOptTypeArg.js";
|
|
10
13
|
import { getPropAccessReturnType } from "../../utils/getPropAccessReturnType.js";
|
|
14
|
+
import { tryResolveNamespaceChain } from "../../utils/resolveNamespaceChain.js";
|
|
11
15
|
import { _compileExpr } from "./_compileExpr.js";
|
|
12
16
|
import { _compileNonNullExpr } from "./_compileNonNullExpr.js";
|
|
13
17
|
import { TirLitNamedObjExpr } from "../../../tir/expressions/litteral/TirLitNamedObjExpr.js";
|
|
18
|
+
import { TirLitEnumMemberExpr } from "../../../tir/expressions/litteral/TirLitEnumMemberExpr.js";
|
|
14
19
|
import { Identifier } from "../../../../ast/nodes/common/Identifier.js";
|
|
15
20
|
import { TirSopOptT } from "../../../tir/types/TirNativeType/native/Optional/sop.js";
|
|
21
|
+
import { TirEnumType } from "../../../tir/types/TirEnumType.js";
|
|
16
22
|
export function _compilePropAccessExpr(ctx, expr, typeHint) {
|
|
17
23
|
if (expr instanceof OptionalPropAccessExpr)
|
|
18
24
|
return _compileOptionalPropAccessExpr(ctx, expr, typeHint);
|
|
@@ -91,12 +97,70 @@ export function _compileNonNullPropAccessExpr(ctx, expr, _typeHint) {
|
|
|
91
97
|
return _compileDotPropAccessExpr(ctx, new DotPropAccessExpr(nonNullObjExpr, expr.prop, expr.range), _typeHint);
|
|
92
98
|
}
|
|
93
99
|
export function _compileDotPropAccessExpr(ctx, expr, _typeHint) {
|
|
100
|
+
// Enum member access: `EnumName.Member`
|
|
101
|
+
if (expr.object instanceof Identifier) {
|
|
102
|
+
const enumRes = _tryResolveEnumMemberAccess(ctx, expr.object, expr.prop, expr);
|
|
103
|
+
if (enumRes)
|
|
104
|
+
return enumRes;
|
|
105
|
+
}
|
|
106
|
+
// if the LHS is a (chain of) identifier(s) rooted at a namespace, resolve
|
|
107
|
+
// the entire dotted chain through namespaces.
|
|
108
|
+
const nsRes = tryResolveNamespaceChain(ctx, expr);
|
|
109
|
+
if (nsRes) {
|
|
110
|
+
if (nsRes.kind === "value")
|
|
111
|
+
return nsRes.expr;
|
|
112
|
+
if (nsRes.kind === "namespace")
|
|
113
|
+
return ctx.error(DiagnosticCode.Namespace_path_is_incomplete_expected_a_value_type_function_or_interface, expr.range);
|
|
114
|
+
// "incomplete" — diagnostic already emitted
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
94
117
|
const objExpr = _compileExpr(ctx, expr.object, undefined);
|
|
95
118
|
if (!objExpr)
|
|
96
119
|
return undefined;
|
|
97
120
|
const objType = objExpr.type;
|
|
121
|
+
// RawConstr (TirUnConstrDataResultT) field access lowers directly to
|
|
122
|
+
// fstPair / sndPair on the underlying pair(int, list(data)).
|
|
123
|
+
if (objType instanceof TirUnConstrDataResultT) {
|
|
124
|
+
if (expr.prop.text === "index") {
|
|
125
|
+
const fn = TirNativeFunc.unConstrDataResultIndex;
|
|
126
|
+
return new TirCallExpr(fn, [objExpr], fn.type.returnType, expr.range);
|
|
127
|
+
}
|
|
128
|
+
if (expr.prop.text === "fields") {
|
|
129
|
+
const fn = TirNativeFunc.unConstrDataResultFields;
|
|
130
|
+
return new TirCallExpr(fn, [objExpr], fn.type.returnType, expr.range);
|
|
131
|
+
}
|
|
132
|
+
return ctx.error(DiagnosticCode.Property_0_does_not_exist_on_type_1, expr.prop.range, expr.prop.text, "RawConstr");
|
|
133
|
+
}
|
|
98
134
|
const returnType = getPropAccessReturnType(ctx, objType, expr.prop);
|
|
99
135
|
if (!returnType)
|
|
100
136
|
return ctx.error(DiagnosticCode.Property_0_does_not_exist_on_type_1, expr.prop.range, expr.prop.text, objType.toString());
|
|
101
137
|
return new TirPropAccessExpr(objExpr, expr.prop, returnType, expr.range);
|
|
102
138
|
}
|
|
139
|
+
/**
|
|
140
|
+
* Returns a `TirLitEnumMemberExpr` if `objId` resolves to an enum type
|
|
141
|
+
* in scope and `propId` is one of its members.
|
|
142
|
+
* Emits a diagnostic and returns a placeholder failure (undefined) if the
|
|
143
|
+
* type is an enum but `propId` is not a member.
|
|
144
|
+
* Returns `undefined` (silently) if `objId` is not an enum-typed identifier
|
|
145
|
+
* — caller falls back to other resolution strategies.
|
|
146
|
+
*/
|
|
147
|
+
function _tryResolveEnumMemberAccess(ctx, objId, propId, fullExpr) {
|
|
148
|
+
// skip if the identifier names a value in scope (variables shadow types)
|
|
149
|
+
if (ctx.scope.resolveValue(objId.text))
|
|
150
|
+
return undefined;
|
|
151
|
+
const possibleTirTypes = ctx.scope.resolveType(objId.text);
|
|
152
|
+
if (!possibleTirTypes)
|
|
153
|
+
return undefined;
|
|
154
|
+
const tirType = ctx.program.types.get(possibleTirTypes.sopTirName)
|
|
155
|
+
?? (possibleTirTypes.dataTirName
|
|
156
|
+
? ctx.program.types.get(possibleTirTypes.dataTirName)
|
|
157
|
+
: undefined);
|
|
158
|
+
if (!(tirType instanceof TirEnumType))
|
|
159
|
+
return undefined;
|
|
160
|
+
const memberIdx = tirType.indexOf(propId.text);
|
|
161
|
+
if (memberIdx < 0) {
|
|
162
|
+
ctx.error(DiagnosticCode.Property_0_is_not_a_member_of_enum_1, propId.range, propId.text, tirType.name);
|
|
163
|
+
return undefined;
|
|
164
|
+
}
|
|
165
|
+
return new TirLitEnumMemberExpr(tirType, memberIdx, fullExpr.range);
|
|
166
|
+
}
|
|
@@ -7,7 +7,9 @@ import { TirUnaryExclamation } from "../../../tir/expressions/unary/TirUnaryExcl
|
|
|
7
7
|
import { TirUnaryMinus } from "../../../tir/expressions/unary/TirUnaryMinus.js";
|
|
8
8
|
import { TirUnaryPlus } from "../../../tir/expressions/unary/TirUnaryPlus.js";
|
|
9
9
|
import { TirUnaryTilde } from "../../../tir/expressions/unary/TirUnaryTilde.js";
|
|
10
|
+
import { TirValueT } from "../../../tir/types/TirNativeType/native/value.js";
|
|
10
11
|
import { canAssignTo, canAssignToOptional } from "../../../tir/types/utils/canAssignTo.js";
|
|
12
|
+
import { getUnaliased } from "../../../tir/types/utils/getUnaliased.js";
|
|
11
13
|
import { _compileExpr } from "./_compileExpr.js";
|
|
12
14
|
export function _compileUnaryPrefixExpr(ctx, expr, _typeHint) {
|
|
13
15
|
const bool_t = ctx.program.stdTypes.bool;
|
|
@@ -26,10 +28,20 @@ export function _compileUnaryPrefixExpr(ctx, expr, _typeHint) {
|
|
|
26
28
|
}
|
|
27
29
|
else if (expr instanceof UnaryPlus
|
|
28
30
|
|| expr instanceof UnaryMinus) {
|
|
29
|
-
|
|
31
|
+
// Probe without hint so we can spot `-Value` (TirValueT) before
|
|
32
|
+
// failing the int check.
|
|
33
|
+
const operand = _compileExpr(ctx, expr.operand, undefined);
|
|
30
34
|
if (!operand)
|
|
31
35
|
return undefined;
|
|
32
36
|
const operandType = operand.type;
|
|
37
|
+
const unaliasedTy = getUnaliased(operandType);
|
|
38
|
+
if (unaliasedTy instanceof TirValueT) {
|
|
39
|
+
// unary plus on Value is the identity; unary minus negates it.
|
|
40
|
+
if (expr instanceof UnaryPlus)
|
|
41
|
+
return new TirUnaryPlus(operand, operandType, expr.range);
|
|
42
|
+
if (expr instanceof UnaryMinus)
|
|
43
|
+
return new TirUnaryMinus(operand, operandType, expr.range);
|
|
44
|
+
}
|
|
33
45
|
if (!canAssignTo(operandType, int_t)) {
|
|
34
46
|
return ctx.error(DiagnosticCode.Type_0_is_not_assignable_to_type_1, expr.operand.range, operand.type.toString(), "int");
|
|
35
47
|
}
|
|
@@ -3,6 +3,8 @@ import { TirVariableAccessExpr } from "../../../tir/expressions/TirVariableAcces
|
|
|
3
3
|
export function _compileVarAccessExpr(ctx, expr, typeHint) {
|
|
4
4
|
const resolvedValue = ctx.scope.resolveValue(expr.text);
|
|
5
5
|
if (!resolvedValue) {
|
|
6
|
+
if (ctx.scope.resolveNamespace(expr.text))
|
|
7
|
+
return ctx.error(DiagnosticCode.Namespace_path_is_incomplete_expected_a_value_type_function_or_interface, expr.range);
|
|
6
8
|
console.trace(ctx.scope.allVariables(), expr.text);
|
|
7
9
|
return ctx.error(DiagnosticCode._0_is_not_defined, expr.range, expr.text);
|
|
8
10
|
}
|
|
@@ -1,20 +1,33 @@
|
|
|
1
1
|
import { DiagnosticCode } from "../../../../../diagnostics/diagnosticMessages.generated.js";
|
|
2
2
|
import { TirAddExpr } from "../../../../tir/expressions/binary/TirBinaryExpr.js";
|
|
3
|
+
import { TirValueT } from "../../../../tir/types/TirNativeType/native/value.js";
|
|
3
4
|
import { canAssignTo } from "../../../../tir/types/utils/canAssignTo.js";
|
|
5
|
+
import { getUnaliased } from "../../../../tir/types/utils/getUnaliased.js";
|
|
4
6
|
import { _compileExpr } from "../_compileExpr.js";
|
|
5
7
|
export function _compileAddExpr(ctx, expr, _typeHint) {
|
|
6
8
|
const int_t = ctx.program.stdTypes.int;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
+
// Probe the left first with no hint; if it turns out to be a Value, we
|
|
10
|
+
// accept the same on the right and lower to `unionValue` in TirAddExpr.
|
|
11
|
+
const leftProbe = _compileExpr(ctx, expr.left, undefined);
|
|
12
|
+
if (!leftProbe)
|
|
9
13
|
return undefined;
|
|
10
|
-
|
|
11
|
-
|
|
14
|
+
const leftTy = getUnaliased(leftProbe.type);
|
|
15
|
+
if (leftTy instanceof TirValueT) {
|
|
16
|
+
const right = _compileExpr(ctx, expr.right, leftProbe.type);
|
|
17
|
+
if (!right)
|
|
18
|
+
return undefined;
|
|
19
|
+
if (!(getUnaliased(right.type) instanceof TirValueT))
|
|
20
|
+
return ctx.error(DiagnosticCode.Type_0_is_not_assignable_to_type_1, expr.right.range, right.type.toString(), "Value");
|
|
21
|
+
return new TirAddExpr(leftProbe, right, expr.range);
|
|
22
|
+
}
|
|
23
|
+
if (!canAssignTo(leftProbe.type, int_t))
|
|
24
|
+
return ctx.error(DiagnosticCode.Type_0_is_not_assignable_to_type_1, expr.left.range, leftProbe.type.toString(), int_t.toString());
|
|
12
25
|
const right = _compileExpr(ctx, expr.right, int_t);
|
|
13
26
|
if (!right)
|
|
14
27
|
return undefined;
|
|
15
28
|
if (!canAssignTo(right.type, int_t))
|
|
16
29
|
return ctx.error(DiagnosticCode.Type_0_is_not_assignable_to_type_1, expr.right.range, right.type.toString(), int_t.toString());
|
|
17
|
-
return new TirAddExpr(
|
|
30
|
+
return new TirAddExpr(leftProbe, right,
|
|
18
31
|
// implicit int type,
|
|
19
32
|
expr.range);
|
|
20
33
|
}
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { DiagnosticCode } from "../../../../../diagnostics/diagnosticMessages.generated.js";
|
|
2
2
|
import { TirEqualExpr } from "../../../../tir/expressions/binary/TirBinaryExpr.js";
|
|
3
3
|
import { canAssignTo } from "../../../../tir/types/utils/canAssignTo.js";
|
|
4
|
+
import { normalizeEnumToInt } from "../../../../tir/types/utils/normalizeEnumToInt.js";
|
|
4
5
|
import { _compileExpr } from "../_compileExpr.js";
|
|
5
6
|
export function _compileEqualExpr(ctx, expr, typeHint) {
|
|
6
7
|
const left = _compileExpr(ctx, expr.left, typeHint);
|
|
7
8
|
if (!left)
|
|
8
9
|
return undefined;
|
|
9
|
-
|
|
10
|
+
// enums lower to ints at runtime, so allow comparing against ints
|
|
11
|
+
const leftType = normalizeEnumToInt(left.type, ctx.program.stdTypes.int);
|
|
10
12
|
const right = _compileExpr(ctx, expr.right, leftType);
|
|
11
13
|
if (!right)
|
|
12
14
|
return undefined;
|
|
@@ -2,6 +2,7 @@ import { DiagnosticCode } from "../../../../../diagnostics/diagnosticMessages.ge
|
|
|
2
2
|
import { TirGreaterThanEqualExpr } from "../../../../tir/expressions/binary/TirBinaryExpr.js";
|
|
3
3
|
import { TirAliasType } from "../../../../tir/types/TirAliasType.js";
|
|
4
4
|
import { canAssignTo } from "../../../../tir/types/utils/canAssignTo.js";
|
|
5
|
+
import { normalizeEnumToInt } from "../../../../tir/types/utils/normalizeEnumToInt.js";
|
|
5
6
|
import { _compileExpr } from "../_compileExpr.js";
|
|
6
7
|
// only aviable for ints and bytes
|
|
7
8
|
export function _compileGreaterThanEqualExpr(ctx, expr, typeHint) {
|
|
@@ -13,7 +14,7 @@ export function _compileGreaterThanEqualExpr(ctx, expr, typeHint) {
|
|
|
13
14
|
if (!canAssignTo(left.type, int_t)
|
|
14
15
|
&& !canAssignTo(left.type, bytes_t))
|
|
15
16
|
return ctx.error(DiagnosticCode.Type_0_is_not_assignable_to_type_1, expr.left.range, left.type.toString(), int_t.toString());
|
|
16
|
-
let leftType = left.type;
|
|
17
|
+
let leftType = normalizeEnumToInt(left.type, int_t);
|
|
17
18
|
while (leftType instanceof TirAliasType)
|
|
18
19
|
leftType = leftType.aliased;
|
|
19
20
|
const right = _compileExpr(ctx, expr.right, leftType);
|
|
@@ -2,6 +2,7 @@ import { DiagnosticCode } from "../../../../../diagnostics/diagnosticMessages.ge
|
|
|
2
2
|
import { TirGreaterThanExpr } from "../../../../tir/expressions/binary/TirBinaryExpr.js";
|
|
3
3
|
import { TirAliasType } from "../../../../tir/types/TirAliasType.js";
|
|
4
4
|
import { canAssignTo } from "../../../../tir/types/utils/canAssignTo.js";
|
|
5
|
+
import { normalizeEnumToInt } from "../../../../tir/types/utils/normalizeEnumToInt.js";
|
|
5
6
|
import { _compileExpr } from "../_compileExpr.js";
|
|
6
7
|
export function _compileGreaterThanExpr(ctx, expr, typeHint) {
|
|
7
8
|
const int_t = ctx.program.stdTypes.int;
|
|
@@ -12,7 +13,7 @@ export function _compileGreaterThanExpr(ctx, expr, typeHint) {
|
|
|
12
13
|
if (!canAssignTo(left.type, int_t)
|
|
13
14
|
&& !canAssignTo(left.type, bytes_t))
|
|
14
15
|
return ctx.error(DiagnosticCode.Type_0_is_not_assignable_to_type_1, expr.left.range, left.type.toString(), int_t.toString());
|
|
15
|
-
let leftType = left.type;
|
|
16
|
+
let leftType = normalizeEnumToInt(left.type, int_t);
|
|
16
17
|
while (leftType instanceof TirAliasType)
|
|
17
18
|
leftType = leftType.aliased;
|
|
18
19
|
const right = _compileExpr(ctx, expr.right, leftType);
|