Namespaced imports

This commit is contained in:
dcodeIO
2017-12-27 22:38:32 +01:00
parent eccac129ad
commit 7795d48d98
13 changed files with 157 additions and 74 deletions

View File

@ -88,48 +88,48 @@ export function initialize(program: Program): void {
i64Func: FunctionPrototype,
u64Func: FunctionPrototype;
addFunction(program, "i8").members = new Map([
[ "MIN_VALUE", new Global(program, "i8.MIN_VALUE", null, Type.i8).withConstantIntegerValue(-128, -1) ],
[ "MAX_VALUE", new Global(program, "i8.MAX_VALUE", null, Type.i8).withConstantIntegerValue(127, 0) ]
[ "MIN_VALUE", new Global(program, "MIN_VALUE", "i8.MIN_VALUE", null, Type.i8).withConstantIntegerValue(-128, -1) ],
[ "MAX_VALUE", new Global(program, "MAX_VALUE", "i8.MAX_VALUE", null, Type.i8).withConstantIntegerValue(127, 0) ]
]);
addFunction(program, "i16").members = new Map([
[ "MIN_VALUE", new Global(program, "i16.MIN_VALUE", null, Type.i16).withConstantIntegerValue(-32768, -1) ],
[ "MAX_VALUE", new Global(program, "i16.MAX_VALUE", null, Type.i16).withConstantIntegerValue(32767, 0) ]
[ "MIN_VALUE", new Global(program, "MIN_VALUE", "i16.MIN_VALUE", null, Type.i16).withConstantIntegerValue(-32768, -1) ],
[ "MAX_VALUE", new Global(program, "MAX_VALUE", "i16.MAX_VALUE", null, Type.i16).withConstantIntegerValue(32767, 0) ]
]);
(i32Func = addFunction(program, "i32")).members = new Map([
[ "MIN_VALUE", new Global(program, "i32.MIN_VALUE", null, Type.i32).withConstantIntegerValue(-2147483648, -1) ],
[ "MAX_VALUE", new Global(program, "i32.MAX_VALUE", null, Type.i32).withConstantIntegerValue(2147483647, 0) ]
[ "MIN_VALUE", new Global(program, "MIN_VALUE", "i32.MIN_VALUE", null, Type.i32).withConstantIntegerValue(-2147483648, -1) ],
[ "MAX_VALUE", new Global(program, "MAX_VALUE", "i32.MAX_VALUE", null, Type.i32).withConstantIntegerValue(2147483647, 0) ]
]);
(i64Func = addFunction(program, "i64")).members = new Map([
[ "MIN_VALUE", new Global(program, "i64.MIN_VALUE", null, Type.i64).withConstantIntegerValue(0, -2147483648) ],
[ "MAX_VALUE", new Global(program, "i64.MAX_VALUE", null, Type.i64).withConstantIntegerValue(-1, 2147483647) ]
[ "MIN_VALUE", new Global(program, "MIN_VALUE", "i64.MIN_VALUE", null, Type.i64).withConstantIntegerValue(0, -2147483648) ],
[ "MAX_VALUE", new Global(program, "MAX_VALUE", "i64.MAX_VALUE", null, Type.i64).withConstantIntegerValue(-1, 2147483647) ]
]);
addFunction(program, "u8").members = new Map([
[ "MIN_VALUE", new Global(program, "u8.MIN_VALUE", null, Type.u8).withConstantIntegerValue(0, 0) ],
[ "MAX_VALUE", new Global(program, "u8.MAX_VALUE", null, Type.u8).withConstantIntegerValue(255, 0) ]
[ "MIN_VALUE", new Global(program, "MIN_VALUE", "u8.MIN_VALUE", null, Type.u8).withConstantIntegerValue(0, 0) ],
[ "MAX_VALUE", new Global(program, "MAX_VALUE", "u8.MAX_VALUE", null, Type.u8).withConstantIntegerValue(255, 0) ]
]);
addFunction(program, "u16").members = new Map([
[ "MIN_VALUE", new Global(program, "u16.MIN_VALUE", null, Type.u16).withConstantIntegerValue(0, 0) ],
[ "MAX_VALUE", new Global(program, "u16.MAX_VALUE", null, Type.u16).withConstantIntegerValue(65535, 0) ]
[ "MIN_VALUE", new Global(program, "MIN_VALUE", "u16.MIN_VALUE", null, Type.u16).withConstantIntegerValue(0, 0) ],
[ "MAX_VALUE", new Global(program, "MAX_VALUE", "u16.MAX_VALUE", null, Type.u16).withConstantIntegerValue(65535, 0) ]
]);
(u32Func = addFunction(program, "u32")).members = new Map([
[ "MIN_VALUE", new Global(program, "u32.MIN_VALUE", null, Type.u32).withConstantIntegerValue(0, 0) ],
[ "MAX_VALUE", new Global(program, "u32.MAX_VALUE", null, Type.u32).withConstantIntegerValue(-1, 0) ]
[ "MIN_VALUE", new Global(program, "MIN_VALUE", "u32.MIN_VALUE", null, Type.u32).withConstantIntegerValue(0, 0) ],
[ "MAX_VALUE", new Global(program, "MAX_VALUE", "u32.MAX_VALUE", null, Type.u32).withConstantIntegerValue(-1, 0) ]
]);
(u64Func = addFunction(program, "u64")).members = new Map([
[ "MIN_VALUE", new Global(program, "u64.MIN_VALUE", null, Type.u64).withConstantIntegerValue(0, 0) ],
[ "MAX_VALUE", new Global(program, "u64.MAX_VALUE", null, Type.i64).withConstantIntegerValue(-1, -1) ]
[ "MIN_VALUE", new Global(program, "MIN_VALUE", "u64.MIN_VALUE", null, Type.u64).withConstantIntegerValue(0, 0) ],
[ "MAX_VALUE", new Global(program, "MAX_VALUE", "u64.MAX_VALUE", null, Type.i64).withConstantIntegerValue(-1, -1) ]
]);
addFunction(program, "bool").members = new Map([
[ "MIN_VALUE", new Global(program, "bool.MIN_VALUE", null, Type.bool).withConstantIntegerValue(0, 0) ],
[ "MAX_VALUE", new Global(program, "bool.MAX_VALUE", null, Type.bool).withConstantIntegerValue(1, 0) ]
[ "MIN_VALUE", new Global(program, "MIN_VALUE", "bool.MIN_VALUE", null, Type.bool).withConstantIntegerValue(0, 0) ],
[ "MAX_VALUE", new Global(program, "MAX_VALUE", "bool.MAX_VALUE", null, Type.bool).withConstantIntegerValue(1, 0) ]
]);
addFunction(program, "f32").members = new Map([
[ "MIN_SAFE_INTEGER", new Global(program, "f32.MIN_SAFE_INTEGER", null, Type.f32).withConstantFloatValue(-16777215) ],
[ "MAX_SAFE_INTEGER", new Global(program, "f32.MAX_SAFE_INTEGER", null, Type.f32).withConstantFloatValue(16777215) ]
[ "MIN_SAFE_INTEGER", new Global(program, "MIN_SAFE_INTEGER", "f32.MIN_SAFE_INTEGER", null, Type.f32).withConstantFloatValue(-16777215) ],
[ "MAX_SAFE_INTEGER", new Global(program, "MAX_SAFE_INTEGER", "f32.MAX_SAFE_INTEGER", null, Type.f32).withConstantFloatValue(16777215) ]
]);
addFunction(program, "f64").members = new Map([
[ "MIN_SAFE_INTEGER", new Global(program, "f64.MIN_SAFE_INTEGER", null, Type.f64).withConstantFloatValue(-9007199254740991) ],
[ "MAX_SAFE_INTEGER", new Global(program, "f64.MAX_SAFE_INTEGER", null, Type.f64).withConstantFloatValue(9007199254740991) ]
[ "MIN_SAFE_INTEGER", new Global(program, "MIN_SAFE_INTEGER", "f64.MIN_SAFE_INTEGER", null, Type.f64).withConstantFloatValue(-9007199254740991) ],
[ "MAX_SAFE_INTEGER", new Global(program, "MAX_SAFE_INTEGER", "f64.MAX_SAFE_INTEGER", null, Type.f64).withConstantFloatValue(9007199254740991) ]
]);
if (program.target == Target.WASM64) {
program.elements.set("isize", i64Func);
@ -144,7 +144,7 @@ export function initialize(program: Program): void {
/** Adds a built-in constant to the specified program. */
function addConstant(program: Program, name: string, type: Type): Global {
const global: Global = new Global(program, name, null, null);
const global: Global = new Global(program, name, name, null);
global.isBuiltIn = true;
global.isConstant = true;
global.type = type;

View File

@ -363,6 +363,18 @@ export class Compiler extends DiagnosticEmitter {
}
const nativeType = global.type.toNativeType();
if (global.isDeclared) {
if (global.isConstant) {
this.module.addGlobalImport(global.internalName, global.namespace ? global.namespace.simpleName : "env", global.simpleName, nativeType);
global.isCompiled = true;
return true;
} else if (declaration) {
this.error(DiagnosticCode.Operation_not_supported, declaration.range);
}
return false;
}
let initializeInStart = false;
if (global.hasConstantValue) {
@ -569,8 +581,8 @@ export class Compiler extends DiagnosticEmitter {
typeRef = this.module.addFunctionType(signatureNameParts.join(""), nativeResultType, nativeParamTypes);
// create the function
if (instance.isDeclared) { // TODO: use parent namespace as externalModuleName, if applicable
this.module.addFunctionImport(instance.internalName, "env", declaration.name.name, typeRef);
if (instance.isDeclared) {
this.module.addFunctionImport(instance.internalName, instance.prototype.namespace ? instance.prototype.namespace.simpleName : "env", declaration.name.name, typeRef);
} else {
this.module.addFunction(instance.internalName, typeRef, typesToNativeTypes(instance.additionalLocals), this.module.createBlock(null, <ExpressionRef[]>stmts, NativeType.None));
}

View File

@ -420,9 +420,10 @@ export class Parser extends DiagnosticEmitter {
if (!initializer)
return null;
} else {
if (hasModifier(ModifierKind.CONST, parentModifiers))
this.error(DiagnosticCode._const_declarations_must_be_initialized, identifier.range);
else if (!type) // neither type nor initializer
if (hasModifier(ModifierKind.CONST, parentModifiers)) {
if (!hasModifier(ModifierKind.DECLARE, parentModifiers))
this.error(DiagnosticCode._const_declarations_must_be_initialized, identifier.range);
} else if (!type) // neither type nor initializer
this.error(DiagnosticCode.Type_expected, tn.range(tn.pos)); // recoverable
}
return Node.createVariableDeclaration(identifier, type, initializer, parentModifiers, parentDecorators, Range.join(identifier.range, tn.range()));

View File

@ -317,7 +317,7 @@ export class Program extends DiagnosticEmitter {
}
} else
classPrototype.members = new Map();
const staticField = new Global(this, internalName, declaration, null);
const staticField = new Global(this, name, internalName, declaration, null);
classPrototype.members.set(name, staticField);
this.elements.set(internalName, staticField);
@ -330,7 +330,7 @@ export class Program extends DiagnosticEmitter {
}
} else
classPrototype.instanceMembers = new Map();
const instanceField = new FieldPrototype(classPrototype, internalName, declaration);
const instanceField = new FieldPrototype(classPrototype, name, internalName, declaration);
classPrototype.instanceMembers.set(name, instanceField);
}
}
@ -433,7 +433,7 @@ export class Program extends DiagnosticEmitter {
this.error(DiagnosticCode.Duplicate_identifier_0, declaration.name.range, internalName);
return;
}
const enm = new Enum(this, internalName, declaration);
const enm = new Enum(this, declaration.name.name, internalName, declaration);
enm.namespace = namespace;
this.elements.set(internalName, enm);
@ -469,7 +469,7 @@ export class Program extends DiagnosticEmitter {
}
} else
enm.members = new Map();
const value = new EnumValue(enm, this, internalName, declaration);
const value = new EnumValue(enm, this, name, internalName, declaration);
enm.members.set(name, value);
}
@ -699,7 +699,7 @@ export class Program extends DiagnosticEmitter {
let namespace = this.elements.get(internalName);
if (!namespace) {
namespace = new Namespace(this, internalName, declaration);
namespace = new Namespace(this, declaration.name.name, internalName, declaration);
namespace.namespace = parentNamespace;
this.elements.set(internalName, namespace);
}
@ -780,7 +780,7 @@ export class Program extends DiagnosticEmitter {
continue;
}
const global = new Global(this, internalName, declaration, null);
const global = new Global(this, declaration.name.name, internalName, declaration, null);
global.namespace = namespace;
this.elements.set(internalName, global);
@ -1041,6 +1041,8 @@ export abstract class Element {
kind: ElementKind;
/** Containing {@link Program}. */
program: Program;
/** Simple name. */
simpleName: string;
/** Internal name referring to this element. */
internalName: string;
/** Element flags. */
@ -1051,8 +1053,9 @@ export abstract class Element {
namespace: Element | null = null;
/** Constructs a new element, linking it to its containing {@link Program}. */
protected constructor(program: Program, internalName: string) {
protected constructor(program: Program, simpleName: string, internalName: string) {
this.program = program;
this.simpleName = simpleName;
this.internalName = internalName;
}
@ -1111,8 +1114,8 @@ export class Namespace extends Element {
declaration: NamespaceDeclaration | null; // more specific
/** Constructs a new namespace. */
constructor(program: Program, internalName: string, declaration: NamespaceDeclaration | null = null) {
super(program, internalName);
constructor(program: Program, simpleName: string, internalName: string, declaration: NamespaceDeclaration | null = null) {
super(program, simpleName, internalName);
if ((this.declaration = declaration) && this.declaration.modifiers) {
for (let i = 0, k = this.declaration.modifiers.length; i < k; ++i) {
switch (this.declaration.modifiers[i].modifierKind) {
@ -1135,8 +1138,8 @@ export class Enum extends Element {
declaration: EnumDeclaration | null;
/** Constructs a new enum. */
constructor(program: Program, internalName: string, declaration: EnumDeclaration | null = null) {
super(program, internalName);
constructor(program: Program, simpleName: string, internalName: string, declaration: EnumDeclaration | null = null) {
super(program, simpleName, internalName);
if ((this.declaration = declaration) && this.declaration.modifiers) {
for (let i = 0, k = this.declaration.modifiers.length; i < k; ++i) {
switch (this.declaration.modifiers[i].modifierKind) {
@ -1163,8 +1166,8 @@ export class EnumValue extends Element {
/** Constant value, if applicable. */
constantValue: i32 = 0;
constructor(enm: Enum, program: Program, internalName: string, declaration: EnumValueDeclaration | null = null) {
super(program, internalName);
constructor(enm: Enum, program: Program, simpleName: string, internalName: string, declaration: EnumValueDeclaration | null = null) {
super(program, simpleName, internalName);
this.enum = enm;
this.declaration = declaration;
}
@ -1184,8 +1187,8 @@ export class Global extends Element {
/** Constant float value, if applicable. */
constantFloatValue: f64 = 0;
constructor(program: Program, internalName: string, declaration: VariableLikeDeclarationStatement | null = null, type: Type | null = null) {
super(program, internalName);
constructor(program: Program, simpleName: string, internalName: string, declaration: VariableLikeDeclarationStatement | null = null, type: Type | null = null) {
super(program, simpleName, internalName);
if (this.declaration = declaration) {
if (this.declaration.modifiers) {
for (let i = 0, k = this.declaration.modifiers.length; i < k; ++i) {
@ -1250,8 +1253,8 @@ export class Local extends Element {
/** Local type. */
type: Type;
constructor(program: Program, internalName: string, index: i32, type: Type) {
super(program, internalName);
constructor(program: Program, simpleName: string, index: i32, type: Type) {
super(program, simpleName, simpleName);
this.index = index;
this.type = type;
}
@ -1268,13 +1271,10 @@ export class FunctionPrototype extends Element {
classPrototype: ClassPrototype | null;
/** Resolved instances. */
instances: Map<string,Function> = new Map();
/** Simple name. */
simpleName: string;
/** Constructs a new function prototype. */
constructor(program: Program, simpleName: string, internalName: string, declaration: FunctionDeclaration | null, classPrototype: ClassPrototype | null = null) {
super(program, internalName);
this.simpleName = simpleName;
super(program, simpleName, internalName);
if (this.declaration = declaration) {
if (this.declaration.modifiers)
for (let i = 0, k = this.declaration.modifiers.length; i < k; ++i) {
@ -1420,7 +1420,7 @@ export class Function extends Element {
/** Constructs a new concrete function. */
constructor(prototype: FunctionPrototype, internalName: string, typeArguments: Type[] | null, parameters: Parameter[], returnType: Type, instanceMethodOf: Class | null) {
super(prototype.program, internalName);
super(prototype.program, prototype.simpleName, internalName);
this.prototype = prototype;
this.typeArguments = typeArguments;
this.parameters = parameters;
@ -1559,8 +1559,8 @@ export class FieldPrototype extends Element {
classPrototype: ClassPrototype;
/** Constructs a new field prototype. */
constructor(classPrototype: ClassPrototype, internalName: string, declaration: FieldDeclaration | null = null) {
super(classPrototype.program, internalName);
constructor(classPrototype: ClassPrototype, simpleName: string, internalName: string, declaration: FieldDeclaration | null = null) {
super(classPrototype.program, simpleName, internalName);
this.classPrototype = classPrototype;
if ((this.declaration = declaration) && this.declaration.modifiers) {
for (let i = 0, k = this.declaration.modifiers.length; i < k; ++i) {
@ -1598,7 +1598,7 @@ export class Field extends Element {
/** Constructs a new field. */
constructor(prototype: FieldPrototype, internalName: string, type: Type) {
super(prototype.program, internalName);
super(prototype.program, prototype.simpleName, internalName);
this.flags = prototype.flags;
this.type = type;
}
@ -1609,8 +1609,6 @@ export class Property extends Element {
kind = ElementKind.PROPERTY;
/** Simple name. */
simpleName: string;
/** Parent class prototype. */
parent: ClassPrototype;
/** Getter prototype. */
@ -1620,8 +1618,7 @@ export class Property extends Element {
/** Constructs a new property prototype. */
constructor(program: Program, simpleName: string, internalName: string, parent: ClassPrototype) {
super(program, internalName);
this.simpleName = simpleName;
super(program, simpleName, internalName);
this.parent = parent;
}
}
@ -1637,12 +1634,9 @@ export class ClassPrototype extends Element {
instances: Map<string,Class> = new Map();
/** Instance member prototypes. */
instanceMembers: Map<string,Element> | null = null;
/** Simple name. */
simpleName: string;
constructor(program: Program, simpleName: string, internalName: string, declaration: ClassDeclaration | null = null) {
super(program, internalName);
this.simpleName = simpleName;
super(program, simpleName, internalName);
if (this.declaration = declaration) {
if (this.declaration.modifiers) {
for (let i = 0, k = this.declaration.modifiers.length; i < k; ++i) {
@ -1736,7 +1730,7 @@ export class Class extends Element {
/** Constructs a new class. */
constructor(prototype: ClassPrototype, internalName: string, typeArguments: Type[] = [], base: Class | null = null) {
super(prototype.program, internalName);
super(prototype.program, prototype.simpleName, internalName);
this.prototype = prototype;
this.flags = prototype.flags;
this.typeArguments = typeArguments;

View File

@ -211,10 +211,11 @@ export class Type {
// export class ClassType extends Type {
// constructor(cls: Class) {
// super(TypeKind.USIZE, clz.size);
// super(TypeKind.USIZE, /* clz.size */ 4); // TODO
// }
// }
// // TODO: what about 'type fun = <T>(a: T) => T;' ?
// export class FunctionType extends Type {
// constructor(fun: Function) {
// super(TypeKind.USIZE, fun.program.target == Target.WASM64 ? 8 : 4);