Generic function type aliases

This commit is contained in:
dcodeIO
2018-03-12 17:44:09 +01:00
parent 423533c6b0
commit 7870e3ac18
14 changed files with 380 additions and 470 deletions

View File

@ -102,6 +102,12 @@ class QueuedImport {
declaration: ImportDeclaration;
}
/** Represents a type alias. */
class TypeAlias {
typeParameters: TypeParameterNode[] | null;
type: CommonTypeNode;
}
const noTypesYet = new Map<string,Type>();
/** Represents an AssemblyScript program. */
@ -118,7 +124,7 @@ export class Program extends DiagnosticEmitter {
/** Types by internal name. */
types: Map<string,Type> = noTypesYet;
/** Declared type aliases. */
typeAliases: Map<string,CommonTypeNode> = new Map();
typeAliases: Map<string,TypeAlias> = new Map();
/** Exports of individual files by exported name. Not global exports. */
exports: Map<string,Element> = new Map();
@ -1257,7 +1263,10 @@ export class Program extends DiagnosticEmitter {
);
return;
}
this.typeAliases.set(name, declaration.alias);
var alias = new TypeAlias();
alias.typeParameters = declaration.typeParameters;
alias.type = declaration.type;
this.typeAliases.set(name, alias);
}
private initializeVariables(statement: VariableStatement, namespace: Element | null = null): void {
@ -1386,8 +1395,9 @@ export class Program extends DiagnosticEmitter {
return Type.u32.asFunction(signature);
}
var typeNode = <TypeNode>node;
var globalName = typeNode.name.text;
var localName = typeNode.range.source.internalPath + PATH_DELIMITER + typeNode.name.text;
var simpleName = typeNode.name.text;
var globalName = simpleName;
var localName = typeNode.range.source.internalPath + PATH_DELIMITER + simpleName;
var element: Element | null;
@ -1405,6 +1415,10 @@ export class Program extends DiagnosticEmitter {
}
}
// check (global) type alias
var alias = this.typeAliases.get(simpleName);
if (alias) return this.resolveType(alias.type, contextualTypeArguments, reportNotFound);
// resolve parameters
if (typeNode.typeArguments) {
var k = typeNode.typeArguments.length;
@ -1438,12 +1452,6 @@ export class Program extends DiagnosticEmitter {
return type;
}
// check type alias
var alias = this.typeAliases.get(globalName);
if (alias && (type = this.resolveType(alias, null, reportNotFound))) {
return type;
}
if (reportNotFound) {
this.error(
DiagnosticCode.Cannot_find_name_0,