initial attempt to support type variable arguments in methods

This commit is contained in:
Dave Holoway
2020-06-09 15:01:58 +01:00
parent 74a21ecbf8
commit 41adfbe53e
6 changed files with 121 additions and 83 deletions

View File

@@ -1183,8 +1183,8 @@ function isTypeAssignable(dest_type, value_type) {
if (!is_assignable) { if (!is_assignable) {
// generic types are also assignable to compatible wildcard type bounds // generic types are also assignable to compatible wildcard type bounds
const raw_type = valid_raw_types.find(rt => rt.rawTypeSignature === dest_type.rawTypeSignature); const raw_type = valid_raw_types.find(rt => rt.rawTypeSignature === dest_type.rawTypeSignature);
if (raw_type instanceof CEIType && raw_type.typevars.length === value_type.typevars.length) { if (raw_type instanceof CEIType && raw_type.typeVariables.length === value_type.typeVariables.length) {
is_assignable = dest_type.typevars.every((dest_tv, idx) => isTypeArgumentCompatible(dest_tv, value_type.typevars[idx].type)); is_assignable = dest_type.typeVariables.every((dest_tv, idx) => isTypeArgumentCompatible(dest_tv, value_type.typeVariables[idx].type));
} }
} }
} }
@@ -1260,7 +1260,7 @@ function isTypeComparable(lhs_type, rhs_type) {
is_comparable = lhs_types.includes(rhs_type) || rhs_types.includes(lhs_type); is_comparable = lhs_types.includes(rhs_type) || rhs_types.includes(lhs_type);
if (!is_comparable) { if (!is_comparable) {
if (lhs_type.rawTypeSignature === rhs_type.rawTypeSignature) { if (lhs_type.rawTypeSignature === rhs_type.rawTypeSignature) {
is_comparable = lhs_type.typevars.every((tv, idx) => isTypeArgumentComparable(tv, rhs_type.typevars[idx])); is_comparable = lhs_type.typeVariables.every((tv, idx) => isTypeArgumentComparable(tv, rhs_type.typeVariables[idx]));
} }
} }
} }
@@ -1596,9 +1596,9 @@ function rootTerm(tokens, locals, method, imports, typemap) {
case 'object-literal': case 'object-literal':
// this, super or null // this, super or null
if (tokens.current.value === 'this') { if (tokens.current.value === 'this') {
matches = new ResolvedIdent(tokens.current.value, [new Value(tokens.current.value, method._owner)]); matches = new ResolvedIdent(tokens.current.value, [new Value(tokens.current.value, method.owner)]);
} else if (tokens.current.value === 'super') { } else if (tokens.current.value === 'super') {
const supertype = method._owner.supers.find(s => s.typeKind === 'class') || typemap.get('java/lang/Object'); const supertype = method.owner.supers.find(s => s.typeKind === 'class') || typemap.get('java/lang/Object');
matches = new ResolvedIdent(tokens.current.value, [new Value(tokens.current.value, supertype)]); matches = new ResolvedIdent(tokens.current.value, [new Value(tokens.current.value, supertype)]);
} else { } else {
matches = new ResolvedIdent(tokens.current.value, [new LiteralValue(tokens.current.value, new NullType())]); matches = new ResolvedIdent(tokens.current.value, [new LiteralValue(tokens.current.value, new NullType())]);
@@ -1917,7 +1917,7 @@ function qualifiers(matches, tokens, locals, method, imports, typemap) {
return matches; return matches;
} }
tokens.inc(); tokens.inc();
genericTypeArgs(tokens, matches.types, method._owner, imports, typemap); genericTypeArgs(tokens, matches.types, method, imports, typemap);
break; break;
default: default:
return matches; return matches;
@@ -2081,7 +2081,7 @@ function findIdentifier(ident, locals, method, imports, typemap) {
matches.variables = [local || param]; matches.variables = [local || param];
} else { } else {
// is it a field or method in the current type (or any of the superclasses) // is it a field or method in the current type (or any of the superclasses)
const types = getTypeInheritanceList(method._owner); const types = getTypeInheritanceList(method.owner);
const method_sigs = new Set(); const method_sigs = new Set();
types.forEach(type => { types.forEach(type => {
if (!matches.variables[0]) { if (!matches.variables[0]) {
@@ -2102,7 +2102,7 @@ function findIdentifier(ident, locals, method, imports, typemap) {
}); });
} }
const { types, package_name } = resolveTypeOrPackage(ident, method._owner, imports, typemap); const { types, package_name } = resolveTypeOrPackage(ident, method, imports, typemap);
matches.types = types; matches.types = types;
matches.package_name = package_name; matches.package_name = package_name;

View File

@@ -1,4 +1,4 @@
const { JavaType, CEIType, PrimitiveType, Constructor, Method, MethodBase, Field, Parameter, TypeVariable, UnresolvedType } = require('java-mti'); const { JavaType, CEIType, PrimitiveType, Constructor, Method, MethodBase, Field, Parameter, TypeVariable, UnresolvedType, signatureToType } = require('java-mti');
const { ModuleBlock, TypeDeclBlock, FieldBlock, ConstructorBlock, MethodBlock, InitialiserBlock, ParameterBlock, TextBlock } = require('./parser9'); const { ModuleBlock, TypeDeclBlock, FieldBlock, ConstructorBlock, MethodBlock, InitialiserBlock, ParameterBlock, TextBlock } = require('./parser9');
/** /**
@@ -38,29 +38,45 @@ function extractTypeList(decl) {
class SourceType extends CEIType { class SourceType extends CEIType {
/** /**
* @param {ModuleBlock} mod * @param {ModuleBlock} mod
* @param {TypeDeclBlock} type * @param {TypeDeclBlock} decl
* @param {string} qualified_type_name qualified $-separated type name * @param {string} qualified_type_name qualified $-separated type name
* @param {Map<string,JavaType>} typemap * @param {Map<string,JavaType>} typemap
*/ */
constructor(mod, type, qualified_type_name, typemap) { constructor(mod, decl, qualified_type_name, typemap) {
super(type.shortSignature, type.kind(), mapmods(type), type.docs); super(decl.shortSignature, decl.kind(), mapmods(decl), decl.docs);
this._decl = type; this._typemap = typemap;
this._decl = decl;
this._dottedTypeName = qualified_type_name.replace(/\$/g, '.'); this._dottedTypeName = qualified_type_name.replace(/\$/g, '.');
this.extends_types = type.extends_decl ? extractTypeList(type.extends_decl) : []; this.extends_types = decl.extends_decl ? extractTypeList(decl.extends_decl) : [];
this.implements_types = type.implements_decl ? extractTypeList(type.implements_decl) : []; this.implements_types = decl.implements_decl ? extractTypeList(decl.implements_decl) : [];
this.implicit_extend = !this.extends_types.length && !this.implements_types.length ? [typemap.get('java/lang/Object')] : []; this.implicit_extend = !this.extends_types.length && !this.implements_types.length ? [typemap.get('java/lang/Object')] : [];
this.fields = type.fields.map(f => new SourceField(this, f)); this.fields = decl.fields.map(f => new SourceField(this, f));
this.methods = type.methods.map(m => new SourceMethod(this, m)); this.methods = decl.methods.map(m => new SourceMethod(this, m));
/** @type {Constructor[]} */
this.constructors = type.constructors.map(c => new SourceConstructor(this, c)); /**
if (!type.constructors[0] && type.kind() === 'class') { * constructors coded in the source
*/
this.declaredConstructors = decl.constructors.map(c => new SourceConstructor(this, c));
/**
* Callable constructors for the type - if the type does not explicitly declare
* any constructors, an implicit default constructor is included
* @type {Constructor[]}
* */
this.constructors = this.declaredConstructors;
if (!decl.constructors[0] && decl.kind() === 'class') {
// add a default public constructor if this is a class with no explicit constructors // add a default public constructor if this is a class with no explicit constructors
this.constructors.push(new DefaultConstructor(this)); this.constructors = [new DefaultConstructor(this)];
} }
this.initers = type.initialisers.map(i => new SourceInitialiser(this, i));
super.typevars = type.typevars.map(tv => { /**
* The class initialisers
*/
this.initers = decl.initialisers.map(i => new SourceInitialiser(this, i));
super.typeVariables = decl.typevars.map(tv => {
const typevar = new TypeVariable(this, tv.name); const typevar = new TypeVariable(this, tv.name);
// automatically add the Object bound // automatically add the Object bound
typevar.bounds.push(new TypeVariable.Bound(this, 'Ljava/lang/Object;', false)); typevar.bounds.push(new TypeVariable.Bound(this, 'Ljava/lang/Object;', false));
@@ -88,23 +104,13 @@ class SourceType extends CEIType {
]; ];
} }
getAllResolvableTypes() { /**
/** @type {ResolvableType[]} */ * @param {string} signature
const res = [ * @param {TypeVariable[]} [typevars]
...this.extends_types, * @returns {JavaType}
...this.implements_types, */
]; resolveType(signature, typevars = []) {
this.fields.forEach(f => res.push(f._type)); return signatureToType(signature, this._typemap, [...typevars, ...this.typeVariables]);
this.methods.forEach(m => {
res.push(m._returnType);
m.parameters.forEach(p => res.push(p._paramType));
});
this.constructors.forEach(c => {
if (c instanceof SourceConstructor) {
c.parameters.forEach(p => res.push(p._paramType));
}
});
return res;
} }
} }
@@ -115,7 +121,6 @@ class SourceField extends Field {
*/ */
constructor(owner, decl) { constructor(owner, decl) {
super(mapmods(decl), decl.docs); super(mapmods(decl), decl.docs);
this._owner = owner;
this._decl = decl; this._decl = decl;
this._type = new ResolvableType(decl); this._type = new ResolvableType(decl);
} }
@@ -135,7 +140,7 @@ class SourceConstructor extends Constructor {
* @param {ConstructorBlock} decl * @param {ConstructorBlock} decl
*/ */
constructor(owner, decl) { constructor(owner, decl) {
super(mapmods(decl), decl.docs); super(owner, mapmods(decl), decl.docs);
this._owner = owner; this._owner = owner;
this._decl = decl; this._decl = decl;
this._parameters = decl.parameters.map((p,i) => new SourceParameter(p)); this._parameters = decl.parameters.map((p,i) => new SourceParameter(p));
@@ -165,8 +170,8 @@ class DefaultConstructor extends Constructor {
* @param {SourceType} owner * @param {SourceType} owner
*/ */
constructor(owner) { constructor(owner) {
super(['public']); super(owner, ['public'], '');
this._owner = owner; this.owner = owner;
} }
get methodSignature() { get methodSignature() {
@@ -177,7 +182,7 @@ class DefaultConstructor extends Constructor {
* @returns {SourceType} * @returns {SourceType}
*/ */
get returnType() { get returnType() {
return this._owner; return this.owner;
} }
} }
@@ -188,8 +193,8 @@ class SourceInitialiser extends MethodBase {
* @param {InitialiserBlock} decl * @param {InitialiserBlock} decl
*/ */
constructor(owner, decl) { constructor(owner, decl) {
super(mapmods(decl), decl.docs); super(owner, mapmods(decl), decl.docs);
this._owner = owner; this.owner = owner;
this._decl = decl; this._decl = decl;
} }
@@ -204,8 +209,8 @@ class SourceMethod extends Method {
* @param {MethodBlock} decl * @param {MethodBlock} decl
*/ */
constructor(owner, decl) { constructor(owner, decl) {
super(decl.name, mapmods(decl), decl.docs); super(owner, decl.name, mapmods(decl), decl.docs);
this._owner = owner; this.owner = owner;
this._decl = decl; this._decl = decl;
this._parameters = decl.parameters.map((p,i) => new SourceParameter(p)); this._parameters = decl.parameters.map((p,i) => new SourceParameter(p));
this._returnType = new ResolvableType(decl); this._returnType = new ResolvableType(decl);
@@ -280,3 +285,4 @@ exports.SourceParameter = SourceParameter;
exports.SourceConstructor = SourceConstructor; exports.SourceConstructor = SourceConstructor;
exports.DefaultConstructor = DefaultConstructor; exports.DefaultConstructor = DefaultConstructor;
exports.SourceInitialiser = SourceInitialiser; exports.SourceInitialiser = SourceInitialiser;
exports.ResolvableType = ResolvableType;

View File

@@ -1,7 +1,7 @@
/** /**
* @typedef {Map<string,JavaType>} TypeMap * @typedef {Map<string,JavaType>} TypeMap
*/ */
const { JavaType, PrimitiveType, ArrayType, CEIType } = require('java-mti'); const { JavaType, PrimitiveType, ArrayType, CEIType, MethodBase } = require('java-mti');
const { ResolvedImport } = require('./import-resolver'); const { ResolvedImport } = require('./import-resolver');
const ResolvedType = require('./parsetypes/resolved-type'); const ResolvedType = require('./parsetypes/resolved-type');
@@ -260,16 +260,25 @@ function resolveTypeIdents(types, fully_qualified_scope, resolved_imports, typem
/** /**
* *
* @param {string} ident * @param {string} ident
* @param {CEIType} scoped_type * @param {CEIType|MethodBase} scope
* @param {ResolvedImport[]} imports * @param {ResolvedImport[]} imports
* @param {Map<string,JavaType>} typemap * @param {Map<string,JavaType>} typemap
*/ */
function resolveTypeOrPackage(ident, scoped_type, imports, typemap) { function resolveTypeOrPackage(ident, scope, imports, typemap) {
const types = []; const types = [];
let package_name = ''; let package_name = '';
// is it an enclosed type of the currently scoped type or any outer type if (scope instanceof MethodBase) {
if (scoped_type) { // is it a type variable in the current scope
const tv = scope.typeVariables.find(tv => tv.name === ident);
if (tv) {
types.push(tv.type);
}
}
if (scope) {
// is it an enclosed type of the currently scoped type or any outer type
const scoped_type = scope instanceof CEIType ? scope : scope.owner;
const scopes = scoped_type.shortSignature.split('$'); const scopes = scoped_type.shortSignature.split('$');
while (scopes.length) { while (scopes.length) {
const enc_type = typemap.get(`${scopes.join('$')}$${ident}`); const enc_type = typemap.get(`${scopes.join('$')}$${ident}`);
@@ -281,6 +290,14 @@ function resolveTypeOrPackage(ident, scoped_type, imports, typemap) {
} }
} }
if (scope instanceof CEIType) {
// is it a type variable of the currently scoped type
const tv = scope.typeVariables.find(tv => tv.name === ident);
if (tv) {
types.push(tv.type);
}
}
if (!types[0]) { if (!types[0]) {
// is it a top-level type from the imports // is it a top-level type from the imports
const top_level_type = '/' + ident; const top_level_type = '/' + ident;

View File

@@ -1,4 +1,4 @@
const { ArrayType, CEIType, JavaType, PrimitiveType, WildcardType } = require('java-mti'); const { ArrayType, CEIType, JavaType, PrimitiveType, MethodBase, WildcardType } = require('java-mti');
const { SourceMethod, SourceConstructor, SourceInitialiser } = require('./source-type'); const { SourceMethod, SourceConstructor, SourceInitialiser } = require('./source-type');
const ResolvedImport = require('./parsetypes/resolved-import'); const ResolvedImport = require('./parsetypes/resolved-import');
const { resolveTypeOrPackage, resolveNextTypeOrPackage } = require('./type-resolver'); const { resolveTypeOrPackage, resolveNextTypeOrPackage } = require('./type-resolver');
@@ -12,16 +12,16 @@ const { AnyType } = require("./body-types");
/** /**
* @param {TokenList} tokens * @param {TokenList} tokens
* @param {CEIType} scoped_type * @param {CEIType|MethodBase} scope
* @param {ResolvedImport[]} imports * @param {ResolvedImport[]} imports
* @param {Map<string,JavaType>} typemap * @param {Map<string,JavaType>} typemap
*/ */
function typeIdentList(tokens, scoped_type, imports, typemap) { function typeIdentList(tokens, scope, imports, typemap) {
let type = typeIdent(tokens, scoped_type, imports, typemap); let type = typeIdent(tokens, scope, imports, typemap);
const types = [type]; const types = [type];
while (tokens.current.value === ',') { while (tokens.current.value === ',') {
tokens.inc(); tokens.inc();
type = typeIdent(tokens, scoped_type, imports, typemap); type = typeIdent(tokens, scope, imports, typemap);
types.push(type); types.push(type);
} }
return types; return types;
@@ -29,22 +29,22 @@ function typeIdentList(tokens, scoped_type, imports, typemap) {
/** /**
* @param {TokenList} tokens * @param {TokenList} tokens
* @param {CEIType} scoped_type * @param {CEIType|MethodBase} scope
* @param {ResolvedImport[]} imports * @param {ResolvedImport[]} imports
* @param {Map<string,JavaType>} typemap * @param {Map<string,JavaType>} typemap
*/ */
function typeIdent(tokens, scoped_type, imports, typemap) { function typeIdent(tokens, scope, imports, typemap) {
let types = [], package_name = ''; let types = [], package_name = '';
switch(tokens.current.kind) { switch(tokens.current.kind) {
case 'ident': case 'ident':
({ types, package_name } = resolveTypeOrPackage(tokens.current.value, scoped_type, imports, typemap)); ({ types, package_name } = resolveTypeOrPackage(tokens.current.value, scope, imports, typemap));
break; break;
case 'primitive-type': case 'primitive-type':
types.push(PrimitiveType.fromName(tokens.current.value)); types.push(PrimitiveType.fromName(tokens.current.value));
break; break;
default: default:
return tokens.current.value === '?' return tokens.current.value === '?'
? wildcardTypeArgument(tokens, scoped_type, imports, typemap) ? wildcardTypeArgument(tokens, scope, imports, typemap)
: AnyType.Instance; : AnyType.Instance;
} }
tokens.inc(); tokens.inc();
@@ -56,7 +56,7 @@ function typeIdent(tokens, scoped_type, imports, typemap) {
({ types, package_name } = resolveNextTypeOrPackage(tokens.current.value, types, package_name, typemap)); ({ types, package_name } = resolveNextTypeOrPackage(tokens.current.value, types, package_name, typemap));
tokens.inc(); tokens.inc();
} else if (tokens.isValue('<')) { } else if (tokens.isValue('<')) {
genericTypeArgs(tokens, types, scoped_type, imports, typemap); genericTypeArgs(tokens, types, scope, imports, typemap);
} else if (tokens.isValue('[')) { } else if (tokens.isValue('[')) {
let arrdims = 0; let arrdims = 0;
for(;;) { for(;;) {
@@ -82,13 +82,13 @@ function typeIdent(tokens, scoped_type, imports, typemap) {
* *
* @param {TokenList} tokens * @param {TokenList} tokens
* @param {JavaType[]} types * @param {JavaType[]} types
* @param {CEIType} scoped_type * @param {CEIType|MethodBase} scope
* @param {ResolvedImport[]} imports * @param {ResolvedImport[]} imports
* @param {Map<string,JavaType>} typemap * @param {Map<string,JavaType>} typemap
*/ */
function genericTypeArgs(tokens, types, scoped_type, imports, typemap) { function genericTypeArgs(tokens, types, scope, imports, typemap) {
if (!tokens.isValue('>')) { if (!tokens.isValue('>')) {
const type_arguments = typeIdentList(tokens, scoped_type, imports, typemap); const type_arguments = typeIdentList(tokens, scope, imports, typemap);
types.forEach((t,i,arr) => { types.forEach((t,i,arr) => {
if (t instanceof CEIType) { if (t instanceof CEIType) {
let specialised = t.specialise(type_arguments); let specialised = t.specialise(type_arguments);
@@ -111,12 +111,12 @@ function genericTypeArgs(tokens, types, scoped_type, imports, typemap) {
/** /**
* @param {TokenList} tokens * @param {TokenList} tokens
* @param {CEIType} scoped_type * @param {CEIType|MethodBase} scope
* @param {ResolvedImport[]} imports * @param {ResolvedImport[]} imports
* @param {Map<string,JavaType>} typemap * @param {Map<string,JavaType>} typemap
* @returns {WildcardType} * @returns {WildcardType}
*/ */
function wildcardTypeArgument(tokens, scoped_type, imports, typemap) { function wildcardTypeArgument(tokens, scope, imports, typemap) {
tokens.expectValue('?'); tokens.expectValue('?');
let bound = null; let bound = null;
switch (tokens.current.value) { switch (tokens.current.value) {
@@ -126,7 +126,7 @@ function wildcardTypeArgument(tokens, scoped_type, imports, typemap) {
tokens.inc(); tokens.inc();
bound = { bound = {
kind, kind,
type: typeIdent(tokens, scoped_type, imports, typemap), type: typeIdent(tokens, scope, imports, typemap),
} }
break; break;
} }

View File

@@ -2,8 +2,7 @@ const { JavaType } = require('java-mti');
const { ModuleBlock, TypeDeclBlock } = require('./parser9'); const { ModuleBlock, TypeDeclBlock } = require('./parser9');
const { resolveImports } = require('../java/import-resolver'); const { resolveImports } = require('../java/import-resolver');
const ResolvedImport = require('../java/parsetypes/resolved-import'); const ResolvedImport = require('../java/parsetypes/resolved-import');
const { resolveType } = require('../java/type-resolver'); const { SourceType, SourceMethod, SourceConstructor, ResolvableType } = require('./source-type');
const { SourceType, SourceConstructor } = require('./source-type');
const { parseBody, flattenBlocks } = require('./body-parser3'); const { parseBody, flattenBlocks } = require('./body-parser3');
const { TokenList } = require('./TokenList'); const { TokenList } = require('./TokenList');
const { typeIdent } = require('./typeident'); const { typeIdent } = require('./typeident');
@@ -28,6 +27,17 @@ function getSourceTypes(mod, owner_typename, parent, source_types, typemap) {
}); });
} }
/**
* @param {ResolvableType} rt
* @param {SourceType|SourceMethod|SourceConstructor} scope
* @param {ResolvedImport[]} resolved_imports
* @param {Map<string,JavaType>} typemap
*/
function resolveResolvableType(rt, scope, resolved_imports, typemap) {
const tokens = new TokenList(flattenBlocks(rt.typeTokens, false));
rt._resolved = typeIdent(tokens, scope, resolved_imports, typemap);
}
/** /**
* *
* @param {SourceType} source_type * @param {SourceType} source_type
@@ -35,11 +45,20 @@ function getSourceTypes(mod, owner_typename, parent, source_types, typemap) {
* @param {Map<string,JavaType>} typemap * @param {Map<string,JavaType>} typemap
*/ */
function resolveResolvableTypes(source_type, resolved_imports, typemap) { function resolveResolvableTypes(source_type, resolved_imports, typemap) {
const resolvableTypes = source_type.getAllResolvableTypes(); source_type.extends_types.forEach(rt => resolveResolvableType(rt, source_type, resolved_imports, typemap));
resolvableTypes.forEach(rt => { source_type.implements_types.forEach(rt => resolveResolvableType(rt, source_type, resolved_imports, typemap));
const tokens = new TokenList(flattenBlocks(rt.typeTokens, false));
rt._resolved = typeIdent(tokens, source_type, resolved_imports, typemap); source_type.fields.forEach(f => resolveResolvableType(f._type, source_type, resolved_imports, typemap));
})
// methods and constructors can have parameterized types
source_type.methods.forEach(m => {
resolveResolvableType(m._returnType, m, resolved_imports, typemap);
m.parameters.forEach(p => resolveResolvableType(p._paramType, m, resolved_imports, typemap));
});
source_type.declaredConstructors.forEach(c => {
c.parameters.forEach(p => resolveResolvableType(p._paramType, c, resolved_imports, typemap));
});
} }
/** /**
@@ -67,11 +86,7 @@ function validate(mod, androidLibrary) {
if (parsed) if (parsed)
probs = probs.concat(parsed.problems) probs = probs.concat(parsed.problems)
}) })
t.constructors.forEach(c => { t.declaredConstructors.forEach(c => {
// ignore any default constructors
if (!(c instanceof SourceConstructor)) {
return;
}
console.log(c.label); console.log(c.label);
const parsed = parseBody(c, imports.resolved, imports.typemap); const parsed = parseBody(c, imports.resolved, imports.typemap);
if (parsed) if (parsed)

View File

@@ -12,7 +12,7 @@ function checkType(type, typeTokens, probs) {
return; return;
} }
if (type instanceof CEIType) { if (type instanceof CEIType) {
type.typevars.forEach(tv => { type.typeVariables.forEach(tv => {
if (tv instanceof TypeArgument) { if (tv instanceof TypeArgument) {
checkType(tv.type, typeTokens, probs); checkType(tv.type, typeTokens, probs);
} }