Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support type infer for class memeber #2880

Closed
wants to merge 4 commits into from
Closed
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
16 changes: 16 additions & 0 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const enum NodeKind {
// types
NamedType,
FunctionType,
InferredType,
TypeName,
TypeParameter,
Parameter,
Expand Down Expand Up @@ -168,6 +169,10 @@ export abstract class Node {
return new NamedTypeNode(Node.createSimpleTypeName("", range), null, false, range);
}

static createInferredType(expr: Expression): InferredTypeNode {
return new InferredTypeNode(expr, expr.range);
}

static createTypeParameter(
name: IdentifierExpression,
extendsType: NamedTypeNode | null,
Expand Down Expand Up @@ -929,6 +934,17 @@ export class FunctionTypeNode extends TypeNode {
}
}

export class InferredTypeNode extends TypeNode {
constructor(
/** Function parameters. */
public expr: Expression,
/** Source range. */
range: Range
) {
super(NodeKind.InferredType, false, range);
}
}

/** Represents a type parameter. */
export class TypeParameterNode extends Node {
constructor(
Expand Down
1 change: 1 addition & 0 deletions src/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"Initializer, definitive assignment or nullable type expected.": 238,
"Definitive assignment has no effect on local variables.": 239,
"Ambiguous operator overload '{0}' (conflicting overloads '{1}' and '{2}').": 240,
"Cannot infer type.": 241,

"Importing the table disables some indirect call optimizations.": 901,
"Exporting the table disables some indirect call optimizations.": 902,
Expand Down
5 changes: 0 additions & 5 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2379,11 +2379,6 @@ export class Parser extends DiagnosticEmitter {
if (tn.skip(Token.Colon)) {
type = this.parseType(tn);
if (!type) return null;
} else {
this.error(
DiagnosticCode.Type_expected,
tn.range()
); // recoverable
}
let initializer: Expression | null = null;
if (tn.skip(Token.Equals)) {
Expand Down
6 changes: 5 additions & 1 deletion src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3999,7 +3999,11 @@ export class PropertyPrototype extends DeclaredElement {
// override stub at the interface needs to handle both interchangeably.
let nativeRange = Source.native.range;
let typeNode = fieldDeclaration.type;
if (!typeNode) typeNode = Node.createOmittedType(fieldDeclaration.name.range.atEnd);
let initializer = fieldDeclaration.initializer;
if (!typeNode) {
const defaultTypeNode = initializer ? Node.createInferredType(initializer) : Node.createOmittedType(fieldDeclaration.name.range.atEnd);
typeNode = defaultTypeNode;
}
let getterDeclaration = new MethodDeclaration( // get name(): type
fieldDeclaration.name,
fieldDeclaration.decorators,
Expand Down
34 changes: 33 additions & 1 deletion src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ import {
NewExpression,
ArrayLiteralExpression,
ArrowKind,
ExpressionStatement
ExpressionStatement,
InferredTypeNode
} from "./ast";

import {
Expand Down Expand Up @@ -169,6 +170,10 @@ export class Resolver extends DiagnosticEmitter {
resolved = this.resolveFunctionType(<FunctionTypeNode>node, flow, ctxElement, ctxTypes, reportMode);
break;
}
case NodeKind.InferredType: {
resolved = this.inferExpressionType((<InferredTypeNode>node).expr);
break;
}
default: assert(false);
}
node.currentlyResolving = false;
Expand Down Expand Up @@ -3742,4 +3747,31 @@ export class Resolver extends DiagnosticEmitter {
}
return typeArgumentNodes[0];
}

private inferExpressionType(expr: Expression): Type | null {
switch(expr.kind) {
case NodeKind.Literal:
switch((<LiteralExpression>expr).literalKind) {
case LiteralKind.Integer:
return this.determineIntegerLiteralType(<IntegerLiteralExpression>expr, false, Type.auto);
case LiteralKind.Float:
return Type.f64;
}
break;
case NodeKind.Binary:
switch((<BinaryExpression>expr).operator) {
case Token.Equals_Equals:
case Token.Equals_Equals_Equals:
case Token.Exclamation_Equals:
case Token.Exclamation_Equals_Equals:
return Type.bool;
}
break;
case NodeKind.True:
case NodeKind.False:
return Type.bool;
}
this.error(DiagnosticCode.Cannot_infer_type, expr.range);
return null;
}
}
Loading
Loading