Skip to content

Commit c99becc

Browse files
authored
Add built-in indexof/valueof types (AssemblyScript#671)
1 parent 49edefc commit c99becc

21 files changed

+1195
-455
lines changed

src/ast.ts

Lines changed: 74 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ export enum NodeKind {
3030
SOURCE,
3131

3232
// types
33-
TYPE,
33+
NAMEDTYPE,
34+
FUNCTIONTYPE,
3435
TYPENAME,
3536
TYPEPARAMETER,
3637
PARAMETER,
37-
SIGNATURE,
3838

3939
// expressions
4040
IDENTIFIER,
@@ -164,24 +164,40 @@ export abstract class Node {
164164
return Node.createTypeName(Node.createIdentifierExpression(name, range), range);
165165
}
166166

167-
static createType(
167+
static createNamedType(
168168
name: TypeName,
169-
typeArguments: CommonTypeNode[] | null,
169+
typeArguments: TypeNode[] | null,
170170
isNullable: bool,
171171
range: Range
172-
): TypeNode {
173-
var type = new TypeNode();
172+
): NamedTypeNode {
173+
var type = new NamedTypeNode();
174174
type.range = range;
175175
type.name = name;
176176
type.typeArguments = typeArguments;
177177
type.isNullable = isNullable;
178178
return type;
179179
}
180180

181+
static createFunctionType(
182+
parameters: ParameterNode[],
183+
returnType: TypeNode,
184+
explicitThisType: NamedTypeNode | null,
185+
isNullable: bool,
186+
range: Range
187+
): FunctionTypeNode {
188+
var type = new FunctionTypeNode();
189+
type.range = range;
190+
type.parameters = parameters;
191+
type.returnType = returnType;
192+
type.explicitThisType = explicitThisType;
193+
type.isNullable = isNullable;
194+
return type;
195+
}
196+
181197
static createOmittedType(
182198
range: Range
183-
): TypeNode {
184-
return Node.createType(
199+
): NamedTypeNode {
200+
return Node.createNamedType(
185201
Node.createSimpleTypeName("", range),
186202
null,
187203
false,
@@ -191,8 +207,8 @@ export abstract class Node {
191207

192208
static createTypeParameter(
193209
name: IdentifierExpression,
194-
extendsType: TypeNode | null,
195-
defaultType: TypeNode | null,
210+
extendsType: NamedTypeNode | null,
211+
defaultType: NamedTypeNode | null,
196212
range: Range
197213
): TypeParameterNode {
198214
var elem = new TypeParameterNode();
@@ -205,7 +221,7 @@ export abstract class Node {
205221

206222
static createParameter(
207223
name: IdentifierExpression,
208-
type: CommonTypeNode,
224+
type: TypeNode,
209225
initializer: Expression | null,
210226
kind: ParameterKind,
211227
range: Range
@@ -219,22 +235,6 @@ export abstract class Node {
219235
return elem;
220236
}
221237

222-
static createSignature(
223-
parameters: ParameterNode[],
224-
returnType: CommonTypeNode,
225-
explicitThisType: TypeNode | null,
226-
isNullable: bool,
227-
range: Range
228-
): SignatureNode {
229-
var sig = new SignatureNode();
230-
sig.range = range;
231-
sig.parameters = parameters;
232-
sig.returnType = returnType;
233-
sig.explicitThisType = explicitThisType;
234-
sig.isNullable = isNullable;
235-
return sig;
236-
}
237-
238238
// special
239239

240240
static createDecorator(
@@ -299,7 +299,7 @@ export abstract class Node {
299299
static createAssertionExpression(
300300
assertionKind: AssertionKind,
301301
expression: Expression,
302-
toType: CommonTypeNode | null,
302+
toType: TypeNode | null,
303303
range: Range
304304
): AssertionExpression {
305305
var expr = new AssertionExpression();
@@ -326,7 +326,7 @@ export abstract class Node {
326326

327327
static createCallExpression(
328328
expression: Expression,
329-
typeArgs: CommonTypeNode[] | null,
329+
typeArgs: TypeNode[] | null,
330330
args: Expression[],
331331
range: Range
332332
): CallExpression {
@@ -406,7 +406,7 @@ export abstract class Node {
406406

407407
static createInstanceOfExpression(
408408
expression: Expression,
409-
isType: CommonTypeNode,
409+
isType: TypeNode,
410410
range: Range
411411
): InstanceOfExpression {
412412
var expr = new InstanceOfExpression();
@@ -428,7 +428,7 @@ export abstract class Node {
428428

429429
static createNewExpression(
430430
expression: Expression,
431-
typeArgs: CommonTypeNode[] | null,
431+
typeArgs: TypeNode[] | null,
432432
args: Expression[],
433433
range: Range
434434
): NewExpression {
@@ -591,8 +591,8 @@ export abstract class Node {
591591
static createClassDeclaration(
592592
identifier: IdentifierExpression,
593593
typeParameters: TypeParameterNode[] | null,
594-
extendsType: TypeNode | null, // can't be a function
595-
implementsTypes: TypeNode[] | null, // can't be functions
594+
extendsType: NamedTypeNode | null, // can't be a function
595+
implementsTypes: NamedTypeNode[] | null, // can't be functions
596596
members: DeclarationStatement[],
597597
decorators: DecoratorNode[] | null,
598598
flags: CommonFlags,
@@ -828,7 +828,7 @@ export abstract class Node {
828828
static createInterfaceDeclaration(
829829
name: IdentifierExpression,
830830
typeParameters: TypeParameterNode[] | null,
831-
extendsType: TypeNode | null, // can't be a function
831+
extendsType: NamedTypeNode | null, // can't be a function
832832
members: DeclarationStatement[],
833833
decorators: DecoratorNode[] | null,
834834
flags: CommonFlags,
@@ -847,7 +847,7 @@ export abstract class Node {
847847

848848
static createFieldDeclaration(
849849
name: IdentifierExpression,
850-
type: CommonTypeNode | null,
850+
type: TypeNode | null,
851851
initializer: Expression | null,
852852
decorators: DecoratorNode[] | null,
853853
flags: CommonFlags,
@@ -882,7 +882,7 @@ export abstract class Node {
882882
static createFunctionDeclaration(
883883
name: IdentifierExpression,
884884
typeParameters: TypeParameterNode[] | null,
885-
signature: SignatureNode,
885+
signature: FunctionTypeNode,
886886
body: Statement | null,
887887
decorators: DecoratorNode[] | null,
888888
flags: CommonFlags,
@@ -902,8 +902,8 @@ export abstract class Node {
902902
}
903903

904904
static createIndexSignatureDeclaration(
905-
keyType: TypeNode,
906-
valueType: CommonTypeNode,
905+
keyType: NamedTypeNode,
906+
valueType: TypeNode,
907907
range: Range
908908
): IndexSignatureDeclaration {
909909
var elem = new IndexSignatureDeclaration();
@@ -916,7 +916,7 @@ export abstract class Node {
916916
static createMethodDeclaration(
917917
name: IdentifierExpression,
918918
typeParameters: TypeParameterNode[] | null,
919-
signature: SignatureNode,
919+
signature: FunctionTypeNode,
920920
body: Statement | null,
921921
decorators: DecoratorNode[] | null,
922922
flags: CommonFlags,
@@ -1012,7 +1012,7 @@ export abstract class Node {
10121012
static createTypeDeclaration(
10131013
name: IdentifierExpression,
10141014
typeParameters: TypeParameterNode[] | null,
1015-
alias: CommonTypeNode,
1015+
alias: TypeNode,
10161016
decorators: DecoratorNode[] | null,
10171017
flags: CommonFlags,
10181018
range: Range
@@ -1041,7 +1041,7 @@ export abstract class Node {
10411041

10421042
static createVariableDeclaration(
10431043
name: IdentifierExpression,
1044-
type: CommonTypeNode | null,
1044+
type: TypeNode | null,
10451045
initializer: Expression | null,
10461046
decorators: DecoratorNode[] | null,
10471047
flags: CommonFlags,
@@ -1082,7 +1082,7 @@ export abstract class Node {
10821082

10831083
// types
10841084

1085-
export abstract class CommonTypeNode extends Node {
1085+
export abstract class TypeNode extends Node {
10861086
// kind varies
10871087

10881088
/** Whether nullable or not. */
@@ -1099,14 +1099,26 @@ export class TypeName extends Node {
10991099
next: TypeName | null;
11001100
}
11011101

1102-
/** Represents a type annotation. */
1103-
export class TypeNode extends CommonTypeNode {
1104-
kind = NodeKind.TYPE;
1102+
/** Represents a named type. */
1103+
export class NamedTypeNode extends TypeNode {
1104+
kind = NodeKind.NAMEDTYPE;
11051105

11061106
/** Type name. */
11071107
name: TypeName;
11081108
/** Type argument references. */
1109-
typeArguments: CommonTypeNode[] | null;
1109+
typeArguments: TypeNode[] | null;
1110+
}
1111+
1112+
/** Represents a function type. */
1113+
export class FunctionTypeNode extends TypeNode {
1114+
kind = NodeKind.FUNCTIONTYPE;
1115+
1116+
/** Accepted parameters. */
1117+
parameters: ParameterNode[];
1118+
/** Return type. */
1119+
returnType: TypeNode;
1120+
/** Explicitly provided this type, if any. */
1121+
explicitThisType: NamedTypeNode | null; // can't be a function
11101122
}
11111123

11121124
/** Represents a type parameter. */
@@ -1116,9 +1128,9 @@ export class TypeParameterNode extends Node {
11161128
/** Identifier reference. */
11171129
name: IdentifierExpression;
11181130
/** Extended type reference, if any. */
1119-
extendsType: TypeNode | null; // can't be a function
1131+
extendsType: NamedTypeNode | null; // can't be a function
11201132
/** Default type if omitted, if any. */
1121-
defaultType: TypeNode | null; // can't be a function
1133+
defaultType: NamedTypeNode | null; // can't be a function
11221134
}
11231135

11241136
/** Represents the kind of a parameter. */
@@ -1140,7 +1152,7 @@ export class ParameterNode extends Node {
11401152
/** Parameter name. */
11411153
name: IdentifierExpression;
11421154
/** Parameter type. */
1143-
type: CommonTypeNode;
1155+
type: TypeNode;
11441156
/** Initializer expression, if present. */
11451157
initializer: Expression | null;
11461158
/** Implicit field declaration, if applicable. */
@@ -1156,18 +1168,6 @@ export class ParameterNode extends Node {
11561168
set(flag: CommonFlags): void { this.flags |= flag; }
11571169
}
11581170

1159-
/** Represents a function signature. */
1160-
export class SignatureNode extends CommonTypeNode {
1161-
kind = NodeKind.SIGNATURE;
1162-
1163-
/** Accepted parameters. */
1164-
parameters: ParameterNode[];
1165-
/** Return type. */
1166-
returnType: CommonTypeNode;
1167-
/** Explicitly provided this type, if any. */
1168-
explicitThisType: TypeNode | null; // can't be a function
1169-
}
1170-
11711171
// special
11721172

11731173
/** Built-in decorator kinds. */
@@ -1347,7 +1347,7 @@ export class AssertionExpression extends Expression {
13471347
/** Expression being asserted. */
13481348
expression: Expression;
13491349
/** Target type. */
1350-
toType: CommonTypeNode | null;
1350+
toType: TypeNode | null;
13511351
}
13521352

13531353
/** Represents a binary expression. */
@@ -1369,7 +1369,7 @@ export class CallExpression extends Expression {
13691369
/** Called expression. Usually an identifier or property access expression. */
13701370
expression: Expression;
13711371
/** Provided type arguments. */
1372-
typeArguments: CommonTypeNode[] | null;
1372+
typeArguments: TypeNode[] | null;
13731373
/** Provided arguments. */
13741374
arguments: Expression[];
13751375

@@ -1450,7 +1450,7 @@ export class InstanceOfExpression extends Expression {
14501450
/** Expression being asserted. */
14511451
expression: Expression;
14521452
/** Type to test for. */
1453-
isType: CommonTypeNode;
1453+
isType: TypeNode;
14541454
}
14551455

14561456
/** Represents an integer literal expression. */
@@ -1659,16 +1659,16 @@ export class IndexSignatureDeclaration extends DeclarationStatement {
16591659
kind = NodeKind.INDEXSIGNATUREDECLARATION;
16601660

16611661
/** Key type. */
1662-
keyType: TypeNode;
1662+
keyType: NamedTypeNode;
16631663
/** Value type. */
1664-
valueType: CommonTypeNode;
1664+
valueType: TypeNode;
16651665
}
16661666

16671667
/** Base class of all variable-like declaration statements. */
16681668
export abstract class VariableLikeDeclarationStatement extends DeclarationStatement {
16691669

16701670
/** Variable type. */
1671-
type: CommonTypeNode | null;
1671+
type: TypeNode | null;
16721672
/** Variable initializer. */
16731673
initializer: Expression | null;
16741674
}
@@ -1696,9 +1696,9 @@ export class ClassDeclaration extends DeclarationStatement {
16961696
/** Accepted type parameters. */
16971697
typeParameters: TypeParameterNode[] | null;
16981698
/** Base class type being extended, if any. */
1699-
extendsType: TypeNode | null; // can't be a function
1699+
extendsType: NamedTypeNode | null; // can't be a function
17001700
/** Interface types being implemented, if any. */
1701-
implementsTypes: TypeNode[] | null; // can't be functions
1701+
implementsTypes: NamedTypeNode[] | null; // can't be functions
17021702
/** Class member declarations. */
17031703
members: DeclarationStatement[];
17041704

@@ -1842,7 +1842,7 @@ export class FunctionDeclaration extends DeclarationStatement {
18421842
/** Type parameters, if any. */
18431843
typeParameters: TypeParameterNode[] | null;
18441844
/** Function signature. */
1845-
signature: SignatureNode;
1845+
signature: FunctionTypeNode;
18461846
/** Body statement. Usually a block. */
18471847
body: Statement | null;
18481848
/** Arrow function kind, if applicable. */
@@ -1979,7 +1979,7 @@ export class TypeDeclaration extends DeclarationStatement {
19791979
/** Type parameters, if any. */
19801980
typeParameters: TypeParameterNode[] | null;
19811981
/** Type being aliased. */
1982-
type: CommonTypeNode;
1982+
type: TypeNode;
19831983
}
19841984

19851985
/** Represents a variable declaration part of a {@link VariableStatement}. */
@@ -2033,9 +2033,9 @@ export function mangleInternalPath(path: string): string {
20332033
}
20342034

20352035
/** Tests if the specified type node represents an omitted type. */
2036-
export function isTypeOmitted(type: CommonTypeNode): bool {
2037-
if (type.kind == NodeKind.TYPE) {
2038-
let name = (<TypeNode>type).name;
2036+
export function isTypeOmitted(type: TypeNode): bool {
2037+
if (type.kind == NodeKind.NAMEDTYPE) {
2038+
let name = (<NamedTypeNode>type).name;
20392039
return !(name.next || name.identifier.text.length);
20402040
}
20412041
return false;

src/common.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ export namespace CommonSymbols {
134134
export const boolean = "boolean";
135135
export const string = "string";
136136
export const native = "native";
137+
export const indexof = "indexof";
138+
export const valueof = "valueof";
137139
// aliases
138140
export const null_ = "null";
139141
export const true_ = "true";

0 commit comments

Comments
 (0)