From c412ae6b6a8bae21acd0d055d3a8ee9c0f3ce049 Mon Sep 17 00:00:00 2001 From: neu_sjq <1745564873@qq.com> Date: Sat, 28 Sep 2024 20:03:48 +0800 Subject: [PATCH 1/3] =?UTF-8?q?add:=20=E6=A0=B9=E6=8D=AEreturn=E8=AF=AD?= =?UTF-8?q?=E5=8F=A5=E6=8E=A8=E6=96=AD=E5=87=BD=E6=95=B0=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: neu_sjq <1745564873@qq.com> --- src/core/common/TypeInference.ts | 42 +++++++++++++++++++++++--------- src/core/model/ArkMethod.ts | 12 +++++++-- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/core/common/TypeInference.ts b/src/core/common/TypeInference.ts index cd2e0312..cf84c234 100644 --- a/src/core/common/TypeInference.ts +++ b/src/core/common/TypeInference.ts @@ -25,7 +25,7 @@ import { ArkStaticFieldRef, ArkThisRef, } from '../base/Ref'; -import { ArkAssignStmt, ArkInvokeStmt, Stmt } from '../base/Stmt'; +import { ArkAssignStmt, ArkInvokeStmt, ArkReturnStmt, Stmt } from '../base/Stmt'; import { AliasType, AnnotationNamespaceType, @@ -56,7 +56,6 @@ import { ArkNamespace } from '../model/ArkNamespace'; import { CONSTRUCTOR_NAME, SUPER_NAME } from './TSConst'; import { ModelUtils } from './ModelUtils'; import { Builtin } from './Builtin'; -import { MethodSignature, MethodSubSignature } from '../model/ArkSignature'; import { UNKNOWN_FILE_NAME } from './Const'; import { EMPTY_STRING } from './ValueUtil'; @@ -377,14 +376,9 @@ export class TypeInference { } public static inferMethodReturnType(method: ArkMethod) { - const oldMethodSignature = method.getSignature(); - const oldMethodSubSignature = oldMethodSignature.getMethodSubSignature(); if (method.getName() === CONSTRUCTOR_NAME) { const newReturnType = new ClassType(method.getDeclaringArkClass().getSignature()); - const newMethodSubSignature = new MethodSubSignature(oldMethodSubSignature.getMethodName(), - oldMethodSubSignature.getParameters(), newReturnType, oldMethodSubSignature.isStatic()); - method.setSignature( - new MethodSignature(oldMethodSignature.getDeclaringClassSignature(), newMethodSubSignature)); + method.setReturnType(newReturnType); return; } const returnType = method.getReturnType(); @@ -393,10 +387,34 @@ export class TypeInference { inferType = this.inferUnclearReferenceType(returnType.getName(), method.getDeclaringArkClass()); } if (inferType) { - const newMethodSubSignature = new MethodSubSignature(oldMethodSubSignature.getMethodName(), - oldMethodSubSignature.getParameters(), inferType, oldMethodSubSignature.isStatic()); - method.setSignature( - new MethodSignature(oldMethodSignature.getDeclaringClassSignature(), newMethodSubSignature)); + method.setReturnType(returnType); + return; + } + + // 基本数据类型, 根据return语句推断返回值类型 + const returnStmts: Stmt[] = method.getReturnStmt(); + if (returnStmts.length === 0) { + method.setReturnType(VoidType.getInstance()); + } else if(returnStmts.length === 1) { + method.setReturnType((returnStmts[0] as ArkReturnStmt).getOp().getType()); + } else { + let typeMap: Map = new Map(); + for(let returnStmt of returnStmts) { + if(returnStmt instanceof ArkReturnStmt) { + typeMap.set(returnStmt.getOp().getType().toString(), returnStmt.getOp().getType()); + } + } + if (typeMap.size === 1) { + for(let type of typeMap.values()) { + method.setReturnType(type); + } + } else { + let types: Type[] = []; + for(let type of typeMap.values()) { + types.push(type); + } + method.setReturnType(new UnionType(types)); + } } } diff --git a/src/core/model/ArkMethod.ts b/src/core/model/ArkMethod.ts index a95170cf..73949840 100644 --- a/src/core/model/ArkMethod.ts +++ b/src/core/model/ArkMethod.ts @@ -15,13 +15,13 @@ import { ArkParameterRef, ArkThisRef } from '../base/Ref'; import { ArkAssignStmt, ArkReturnStmt, Stmt } from '../base/Stmt'; -import { GenericType } from '../base/Type'; +import { GenericType, Type } from '../base/Type'; import { Value } from '../base/Value'; import { Cfg } from '../graph/Cfg'; import { ViewTree } from '../graph/ViewTree'; import { ArkBody } from './ArkBody'; import { ArkClass } from './ArkClass'; -import { MethodSignature } from './ArkSignature'; +import { MethodSignature, MethodSubSignature } from './ArkSignature'; import { Decorator } from '../base/Decorator'; import { BodyBuilder } from '../common/BodyBuilder'; import { ArkExport, ExportType } from './ArkExport'; @@ -127,6 +127,14 @@ export class ArkMethod implements ArkExport { return this.methodSignature.getType(); } + public setReturnType(newReturnType: Type) { + const oldMethodSignature = this.getSignature(); + const oldMethodSubSignature = oldMethodSignature.getMethodSubSignature(); + const newMethodSubSignature = new MethodSubSignature(oldMethodSubSignature.getMethodName(), + oldMethodSubSignature.getParameters(), newReturnType, oldMethodSubSignature.isStatic()); + this.setSignature(new MethodSignature(oldMethodSignature.getDeclaringClassSignature(), newMethodSubSignature)); + } + public getSignature() { return this.methodSignature; } -- Gitee From 79e92466295cd0ddaa8cd939671940ea6be34327 Mon Sep 17 00:00:00 2001 From: neu_sjq <1745564873@qq.com> Date: Sat, 28 Sep 2024 20:39:22 +0800 Subject: [PATCH 2/3] =?UTF-8?q?format:=20=E4=BF=AE=E6=94=B9=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: neu_sjq <1745564873@qq.com> --- src/core/common/TypeInference.ts | 10 +++++----- src/core/model/ArkMethod.ts | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/core/common/TypeInference.ts b/src/core/common/TypeInference.ts index cf84c234..fe727de6 100644 --- a/src/core/common/TypeInference.ts +++ b/src/core/common/TypeInference.ts @@ -395,22 +395,22 @@ export class TypeInference { const returnStmts: Stmt[] = method.getReturnStmt(); if (returnStmts.length === 0) { method.setReturnType(VoidType.getInstance()); - } else if(returnStmts.length === 1) { + } else if (returnStmts.length === 1) { method.setReturnType((returnStmts[0] as ArkReturnStmt).getOp().getType()); } else { let typeMap: Map = new Map(); - for(let returnStmt of returnStmts) { - if(returnStmt instanceof ArkReturnStmt) { + for (let returnStmt of returnStmts) { + if (returnStmt instanceof ArkReturnStmt) { typeMap.set(returnStmt.getOp().getType().toString(), returnStmt.getOp().getType()); } } if (typeMap.size === 1) { - for(let type of typeMap.values()) { + for (let type of typeMap.values()) { method.setReturnType(type); } } else { let types: Type[] = []; - for(let type of typeMap.values()) { + for (let type of typeMap.values()) { types.push(type); } method.setReturnType(new UnionType(types)); diff --git a/src/core/model/ArkMethod.ts b/src/core/model/ArkMethod.ts index 73949840..c238c333 100644 --- a/src/core/model/ArkMethod.ts +++ b/src/core/model/ArkMethod.ts @@ -127,7 +127,7 @@ export class ArkMethod implements ArkExport { return this.methodSignature.getType(); } - public setReturnType(newReturnType: Type) { + public setReturnType(newReturnType: Type): void { const oldMethodSignature = this.getSignature(); const oldMethodSubSignature = oldMethodSignature.getMethodSubSignature(); const newMethodSubSignature = new MethodSubSignature(oldMethodSubSignature.getMethodName(), -- Gitee From 47a039766bcd499beb14baf8ed9517b935a9bbca Mon Sep 17 00:00:00 2001 From: neu_sjq <1745564873@qq.com> Date: Sun, 29 Sep 2024 10:51:04 +0800 Subject: [PATCH 3/3] =?UTF-8?q?update:=20=E6=94=B9=E7=94=A8getReturnValues?= =?UTF-8?q?()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: neu_sjq <1745564873@qq.com> --- src/core/common/TypeInference.ts | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/core/common/TypeInference.ts b/src/core/common/TypeInference.ts index fe727de6..ad199128 100644 --- a/src/core/common/TypeInference.ts +++ b/src/core/common/TypeInference.ts @@ -25,7 +25,7 @@ import { ArkStaticFieldRef, ArkThisRef, } from '../base/Ref'; -import { ArkAssignStmt, ArkInvokeStmt, ArkReturnStmt, Stmt } from '../base/Stmt'; +import { ArkAssignStmt, ArkInvokeStmt, Stmt } from '../base/Stmt'; import { AliasType, AnnotationNamespaceType, @@ -392,17 +392,15 @@ export class TypeInference { } // 基本数据类型, 根据return语句推断返回值类型 - const returnStmts: Stmt[] = method.getReturnStmt(); - if (returnStmts.length === 0) { + const returnValues: Value[] = method.getReturnValues(); + if (returnValues.length === 0) { method.setReturnType(VoidType.getInstance()); - } else if (returnStmts.length === 1) { - method.setReturnType((returnStmts[0] as ArkReturnStmt).getOp().getType()); + } else if (returnValues.length === 1) { + method.setReturnType(returnValues[0].getType()); } else { let typeMap: Map = new Map(); - for (let returnStmt of returnStmts) { - if (returnStmt instanceof ArkReturnStmt) { - typeMap.set(returnStmt.getOp().getType().toString(), returnStmt.getOp().getType()); - } + for (let returnValue of returnValues) { + typeMap.set(returnValue.getType().toString(), returnValue.getType()); } if (typeMap.size === 1) { for (let type of typeMap.values()) { -- Gitee