Skip to content

Commit 37ca029

Browse files
committed
implement
1 parent 83fa58a commit 37ca029

File tree

5 files changed

+49
-3
lines changed

5 files changed

+49
-3
lines changed

src/ast.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export const enum NodeKind {
5151
// types
5252
NamedType,
5353
FunctionType,
54+
InferredType,
5455
TypeName,
5556
TypeParameter,
5657
Parameter,
@@ -168,6 +169,10 @@ export abstract class Node {
168169
return new NamedTypeNode(Node.createSimpleTypeName("", range), null, false, range);
169170
}
170171

172+
static createInferredType(expr: Expression): InferredTypeNode {
173+
return new InferredTypeNode(expr, expr.range);
174+
}
175+
171176
static createTypeParameter(
172177
name: IdentifierExpression,
173178
extendsType: NamedTypeNode | null,
@@ -929,6 +934,17 @@ export class FunctionTypeNode extends TypeNode {
929934
}
930935
}
931936

937+
export class InferredTypeNode extends TypeNode {
938+
constructor(
939+
/** Function parameters. */
940+
public expr: Expression,
941+
/** Source range. */
942+
range: Range
943+
) {
944+
super(NodeKind.InferredType, false, range);
945+
}
946+
}
947+
932948
/** Represents a type parameter. */
933949
export class TypeParameterNode extends Node {
934950
constructor(

src/diagnosticMessages.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"Initializer, definitive assignment or nullable type expected.": 238,
5353
"Definitive assignment has no effect on local variables.": 239,
5454
"Ambiguous operator overload '{0}' (conflicting overloads '{1}' and '{2}').": 240,
55+
"Cannot infer type.": 241,
5556

5657
"Importing the table disables some indirect call optimizations.": 901,
5758
"Exporting the table disables some indirect call optimizations.": 902,

src/diagnostics.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,6 @@ export abstract class DiagnosticEmitter {
454454
arg1: string | null = null,
455455
arg2: string | null = null
456456
): void {
457-
console.trace()
458457
this.emitDiagnostic(code, DiagnosticCategory.Error, range, null, arg0, arg1, arg2);
459458
}
460459

src/program.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3999,7 +3999,8 @@ export class PropertyPrototype extends DeclaredElement {
39993999
// override stub at the interface needs to handle both interchangeably.
40004000
let nativeRange = Source.native.range;
40014001
let typeNode = fieldDeclaration.type;
4002-
if (!typeNode) typeNode = Node.createOmittedType(fieldDeclaration.name.range.atEnd);
4002+
let initializer = fieldDeclaration.initializer;
4003+
if (!typeNode) typeNode = initializer ? Node.createInferredType(initializer) : Node.createOmittedType(fieldDeclaration.name.range.atEnd)
40034004
let getterDeclaration = new MethodDeclaration( // get name(): type
40044005
fieldDeclaration.name,
40054006
fieldDeclaration.decorators,

src/resolver.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ import {
7676
NewExpression,
7777
ArrayLiteralExpression,
7878
ArrowKind,
79-
ExpressionStatement
79+
ExpressionStatement,
80+
InferredTypeNode
8081
} from "./ast";
8182

8283
import {
@@ -169,6 +170,10 @@ export class Resolver extends DiagnosticEmitter {
169170
resolved = this.resolveFunctionType(<FunctionTypeNode>node, flow, ctxElement, ctxTypes, reportMode);
170171
break;
171172
}
173+
case NodeKind.InferredType: {
174+
resolved = this.inferExpressionType((<InferredTypeNode>node).expr);
175+
break;
176+
}
172177
default: assert(false);
173178
}
174179
node.currentlyResolving = false;
@@ -3742,4 +3747,28 @@ export class Resolver extends DiagnosticEmitter {
37423747
}
37433748
return typeArgumentNodes[0];
37443749
}
3750+
3751+
private inferExpressionType(expr: Expression): Type | null {
3752+
switch(expr.kind) {
3753+
case NodeKind.Literal:
3754+
switch((<LiteralExpression>expr).literalKind) {
3755+
case LiteralKind.Integer:
3756+
return this.determineIntegerLiteralType(<IntegerLiteralExpression>expr, false, Type.auto);
3757+
case LiteralKind.Float:
3758+
return Type.f64;
3759+
}
3760+
break;
3761+
case NodeKind.Binary:
3762+
switch((<BinaryExpression>expr).operator) {
3763+
case Token.Equals_Equals:
3764+
case Token.Equals_Equals_Equals:
3765+
case Token.Exclamation_Equals:
3766+
case Token.Exclamation_Equals_Equals:
3767+
return Type.bool;
3768+
}
3769+
break;
3770+
}
3771+
this.error(DiagnosticCode.Cannot_infer_type, expr.range);
3772+
return null;
3773+
}
37453774
}

0 commit comments

Comments
 (0)