diff --git a/lib/semantic.js b/lib/semantic.js index d54d054..0751b4f 100644 --- a/lib/semantic.js +++ b/lib/semantic.js @@ -2369,27 +2369,43 @@ class TypeChecker { visitInstanceCall(ast, env) { assert.equal(ast.left.type, 'instance_call'); - let moduleName; - if (ast.left.id.type === Tag.ID) { - const moduleType = env.local.get(ast.left.id.lexeme); - moduleName = moduleType.name; - } else { - const type = this.getVariableType(ast.left.id, env); + const type = this.getVariableType(ast.left.id, env); + let moduleName = type.name; + if(isBasicType(type.type)) { + moduleName = this.getBuiltinModule(type.type); + ast.builtinModule = moduleName; + ast.left.id.moduleType = { + type: 'basic', + name: type.type, + }; + } else if(type.type === 'basic' && isBasicType(type.name)) { + moduleName = this.getBuiltinModule(type.name); + ast.builtinModule = moduleName; + ast.left.id.moduleType = { + type: 'basic', + name: type.name, + }; + } else if(type.type === 'model') { + moduleName = '$ModelInstance'; + ast.builtinModule = moduleName; + ast.left.id.moduleType = { + type: 'model', + name: type.name, + }; + } else if(builtin.has(moduleName)) { + ast.builtinModule = moduleName; + ast.left.id.moduleType = { + type: 'builtin_module', + name: moduleName, + }; + } else if (ast.left.id.tag === Tag.ID) { moduleName = type.name; - if(isBasicType(type.type)) { - moduleName = this.getBuiltinModule(type.type); - ast.builtinModule = moduleName; - } else if(type.type === 'basic' && isBasicType(type.name)) { - moduleName = this.getBuiltinModule(type.name); - ast.builtinModule = moduleName; - } else if(type.type === 'model') { - moduleName = '$ModelInstance'; - ast.builtinModule = moduleName; - } else if(builtin.has(moduleName)) { - ast.builtinModule = moduleName; - } + ast.left.id.moduleType = { + type: 'module', + name: moduleName + }; } - + const checker = this.getChecker(moduleName); const method = ast.left.propertyPath[0]; const name = method.lexeme; diff --git a/test/fixtures/module_instance/main.dara b/test/fixtures/module_instance/main.dara index 615202d..6f73c86 100644 --- a/test/fixtures/module_instance/main.dara +++ b/test/fixtures/module_instance/main.dara @@ -1,5 +1,7 @@ import OSS static function call(): OSS { - return new OSS(); + var oss = new OSS(); + oss.putObject(); + return oss; } diff --git a/test/import.test.js b/test/import.test.js index 651e192..c9dd248 100644 --- a/test/import.test.js +++ b/test/import.test.js @@ -355,29 +355,41 @@ function callOSS(): string { it('module instance should ok', function () { const ast = readAndParse('fixtures/module_instance/main.dara'); + console.log('%j', ast); const [f1] = ast.moduleBody.nodes; const [s1] = f1.functionBody.stmts.stmts; expect(s1).to.be.eql({ + 'expectedType': undefined, + 'type': 'declare', + 'id': { + 'tag': 2, + 'loc': loc(4, 7, 4 ,10), + 'lexeme': 'oss', + 'index': 12 + }, 'expr': { + 'type': 'construct', 'aliasId': { + 'tag': 2, + 'loc': loc(4, 17, 4 ,20), 'lexeme': 'OSS', - 'index': 13, - 'loc': loc(4, 14, 4, 17), - 'tag': 2 + 'index': 15 }, 'args': [], + 'tokenRange': [ + 14, + 18 + ], 'inferred': { + 'type': 'module_instance', 'name': 'OSS', - 'parentModuleIds': [], - 'type': 'module_instance' - }, - 'tokenRange': [12, 16], - 'type': 'construct' + 'parentModuleIds': [] + } }, - 'loc': loc(4, 3, 5, 1), - 'needCast': false, - 'tokenRange': [11, 16], - 'type': 'return' + 'tokenRange': [ + 11, + 18 + ] }); }); }); diff --git a/test/semantic.test.js b/test/semantic.test.js index 053602b..5d67def 100644 --- a/test/semantic.test.js +++ b/test/semantic.test.js @@ -7617,4 +7617,30 @@ describe('semantic', function () { readAndParse('fixtures/multi_catch/main.dara'); }).to.not.throwException(); }); + + it('module instance_call left type shoule be ok', function(){ + let ast = readAndParse('fixtures/module_instance/main.dara'); + const [f1] = ast.moduleBody.nodes; + const [, s2] = f1.functionBody.stmts.stmts; + expect(s2.left.id).to.be.eql({ + 'tag': 2, + 'loc': { + 'start': { + 'line': 5, + 'column': 3 + }, + 'end': { + 'line': 5, + 'column': 6 + } + }, + 'lexeme': 'oss', + 'index': 19, + 'type': 'variable', + 'moduleType': { + 'type': 'module', + 'name': 'OSS' + } + }); + }); }); \ No newline at end of file