diff --git a/tsc/include/TypeScript/LowerToLLVM/CastLogicHelper.h b/tsc/include/TypeScript/LowerToLLVM/CastLogicHelper.h index 4931af2eb..c9322a805 100644 --- a/tsc/include/TypeScript/LowerToLLVM/CastLogicHelper.h +++ b/tsc/include/TypeScript/LowerToLLVM/CastLogicHelper.h @@ -400,7 +400,7 @@ class CastLogicHelper { MLIRTypeHelper mth(unionType.getContext()); mlir::Type baseType; - bool needTag = mth.isUnionTypeNeedsTag(unionType, baseType); + bool needTag = mth.isUnionTypeNeedsTag(loc, unionType, baseType); if (!needTag) { auto llvmBoolType = tch.convertType(boolType); @@ -512,7 +512,7 @@ class CastLogicHelper { MLIRTypeHelper mth(resUnionType.getContext()); mlir::Type baseType; - bool needTag = mth.isUnionTypeNeedsTag(resUnionType, baseType); + bool needTag = mth.isUnionTypeNeedsTag(loc, resUnionType, baseType); if (needTag) { auto typeOfValue = rewriter.create(loc, mlir_ts::StringType::get(rewriter.getContext()), in); @@ -530,7 +530,7 @@ class CastLogicHelper { MLIRTypeHelper mth(inUnionType.getContext()); mlir::Type baseType; - bool needTag = mth.isUnionTypeNeedsTag(inUnionType, baseType); + bool needTag = mth.isUnionTypeNeedsTag(loc, inUnionType, baseType); if (!needTag) { return cast(in, baseType, tch.convertType(baseType), resType, resLLVMType); @@ -983,7 +983,7 @@ class CastLogicHelper { MLIRTypeHelper mth(unionType.getContext()); mlir::Type baseType; - bool needTag = mth.isUnionTypeNeedsTag(unionType, baseType); + bool needTag = mth.isUnionTypeNeedsTag(loc, unionType, baseType); if (needTag) { typeOfValue = toh.typeOfLogic(loc, valueForBoxing, unionType); diff --git a/tsc/include/TypeScript/LowerToLLVM/LLVMDebugInfo.h b/tsc/include/TypeScript/LowerToLLVM/LLVMDebugInfo.h index cff623eab..47c4fa05f 100644 --- a/tsc/include/TypeScript/LowerToLLVM/LLVMDebugInfo.h +++ b/tsc/include/TypeScript/LowerToLLVM/LLVMDebugInfo.h @@ -94,18 +94,18 @@ class LLVMDebugInfoHelper { } - LLVM::DITypeAttr getDIType(mlir::Type llvmType, mlir::Type type, LLVM::DIFileAttr file, uint32_t line, LLVM::DIScopeAttr scope) + LLVM::DITypeAttr getDIType(mlir::Location location, mlir::Type llvmType, mlir::Type type, LLVM::DIFileAttr file, uint32_t line, LLVM::DIScopeAttr scope) { - auto diType = getDITypeScriptType(type, file, line, scope); + auto diType = getDITypeScriptType(location, type, file, line, scope); if (diType) { return diType; } - return getDILLVMType(llvmType, file, line, scope); + return getDILLVMType(location, llvmType, file, line, scope); } - LLVM::DITypeAttr getDITypeScriptType(mlir::Type type, LLVM::DIFileAttr file, uint32_t line, LLVM::DIScopeAttr scope) + LLVM::DITypeAttr getDITypeScriptType(mlir::Location location, mlir::Type type, LLVM::DIFileAttr file, uint32_t line, LLVM::DIScopeAttr scope) { if (!type) { @@ -132,7 +132,7 @@ class LLVMDebugInfoHelper // special case if (auto anyType = type.dyn_cast()) { - return getDIType(anyType, file, line, scope); + return getDIType(location, anyType, file, line, scope); } #ifdef ENABLE_DEBUGINFO_PATCH_INFO @@ -145,33 +145,33 @@ class LLVMDebugInfoHelper if (auto unionType = type.dyn_cast()) { MLIRTypeHelper mth(context); - if (mth.isUnionTypeNeedsTag(unionType)) + if (mth.isUnionTypeNeedsTag(location, unionType)) { - return getDIType(unionType, file, line, scope); + return getDIType(location, unionType, file, line, scope); } } if (auto tupleType = type.dyn_cast()) { - return getDITypeWithFields(tupleType, "tuple", true, file, line, scope); + return getDITypeWithFields(location, tupleType, "tuple", true, file, line, scope); } if (auto classType = type.dyn_cast_or_null()) { - return getDIType(classType, file, line, scope); + return getDIType(location, classType, file, line, scope); } if (auto classStorageType = type.dyn_cast()) { - return getDITypeWithFields(classStorageType, classStorageType.getName().getValue().str(), false, file, line, scope); + return getDITypeWithFields(location, classStorageType, classStorageType.getName().getValue().str(), false, file, line, scope); } if (auto enumType = type.dyn_cast()) { - return getDIType(enumType, file, line, scope); + return getDIType(location, enumType, file, line, scope); } - return getDILLVMType(llvmtch.typeConverter.convertType(type), file, line, scope); + return getDILLVMType(location, llvmtch.typeConverter.convertType(type), file, line, scope); } LLVM::DITypeAttr getDITypeScriptBasicType(mlir::Type type, LLVM::DIFileAttr file, uint32_t line, LLVM::DIScopeAttr scope) @@ -220,7 +220,7 @@ class LLVMDebugInfoHelper return diTypeAttr; } - LLVM::DITypeAttr getDILLVMType(mlir::Type llvmType, LLVM::DIFileAttr file, uint32_t line, LLVM::DIScopeAttr scope) + LLVM::DITypeAttr getDILLVMType(mlir::Location location, mlir::Type llvmType, LLVM::DIFileAttr file, uint32_t line, LLVM::DIScopeAttr scope) { LLVM::DITypeAttr diTypeAttr; @@ -260,13 +260,13 @@ class LLVMDebugInfoHelper diTypeAttr = LLVM::DIBasicTypeAttr::get(context, dwarf::DW_TAG_base_type, StringAttr::get(context, typeName), floatType.getIntOrFloatBitWidth(), dwarf::DW_ATE_float); }) .Case([&](auto structType) { - diTypeAttr = getDIStructType(structType, file, line, scope); + diTypeAttr = getDIStructType(location, structType, file, line, scope); }) .Case([&](auto llvmPointerType) { - diTypeAttr = getDIPointerType(getDILLVMType(llvmPointerType.getElementType(), file, line, scope), file, line); + diTypeAttr = getDIPointerType(getDILLVMType(location, llvmPointerType.getElementType(), file, line, scope), file, line); }) .Case([&](auto funcType) { - diTypeAttr = getDISubroutineType(funcType, file, line, scope); + diTypeAttr = getDISubroutineType(location, funcType, file, line, scope); }) .Default([&](auto type) { // TODO: review it @@ -294,9 +294,9 @@ class LLVMDebugInfoHelper return getDIPointerType(diTypeAttr, file, line); } - LLVM::DITypeAttr getDIType(mlir_ts::AnyType anyType, LLVM::DIFileAttr file, uint32_t line, LLVM::DIScopeAttr scope) + LLVM::DITypeAttr getDIType(mlir::Location location, mlir_ts::AnyType anyType, LLVM::DIFileAttr file, uint32_t line, LLVM::DIScopeAttr scope) { - auto diBodyType = getDIStructType("any", { + auto diBodyType = getDIStructType(location, "any", { {"size", mlir::IndexType::get(context)}, {"type", mlir_ts::StringType::get(context)}, }, file, line, scope); @@ -318,12 +318,12 @@ class LLVMDebugInfoHelper } #endif - LLVM::DITypeAttr getDIType(mlir_ts::UnionType unionType, LLVM::DIFileAttr file, uint32_t line, LLVM::DIScopeAttr scope) + LLVM::DITypeAttr getDIType(mlir::Location location, mlir_ts::UnionType unionType, LLVM::DIFileAttr file, uint32_t line, LLVM::DIScopeAttr scope) { auto strType = mlir_ts::StringType::get(context); - auto diStrType = getDITypeScriptType(strType, file, line, scope); + auto diStrType = getDITypeScriptType(location, strType, file, line, scope); - auto diTypeAttrUnion = getDIUnionType(unionType, file, line, scope); + auto diTypeAttrUnion = getDIUnionType(location, unionType, file, line, scope); return getDIStructType(MLIRHelper::getAnonymousName(unionType, "union"), { {"type", diStrType}, @@ -331,15 +331,15 @@ class LLVMDebugInfoHelper }, file, line, scope); } - LLVM::DITypeAttr getDIType(mlir_ts::ClassType classType, LLVM::DIFileAttr file, uint32_t line, LLVM::DIScopeAttr scope) + LLVM::DITypeAttr getDIType(mlir::Location location, mlir_ts::ClassType classType, LLVM::DIFileAttr file, uint32_t line, LLVM::DIScopeAttr scope) { - auto diTypeAttrClassType = getDIPointerType(getDITypeScriptType(classType.getStorageType(), file, line, scope), file, line); + auto diTypeAttrClassType = getDIPointerType(getDITypeScriptType(location, classType.getStorageType(), file, line, scope), file, line); return diTypeAttrClassType; } - LLVM::DITypeAttr getDIType(mlir_ts::EnumType enumType, LLVM::DIFileAttr file, uint32_t line, LLVM::DIScopeAttr scope) + LLVM::DITypeAttr getDIType(mlir::Location location, mlir_ts::EnumType enumType, LLVM::DIFileAttr file, uint32_t line, LLVM::DIScopeAttr scope) { - auto diBaseType = getDITypeScriptType(enumType.getElementType(), file, line, scope); + auto diBaseType = getDITypeScriptType(location, enumType.getElementType(), file, line, scope); //auto enumName = MLIRHelper::getAnonymousName(enumType, "enum"); @@ -364,7 +364,7 @@ class LLVMDebugInfoHelper return diBaseType; } - LLVM::DITypeAttr getDITypeWithFields(mlir::Type typeWithFields, std::string name, bool isNamePrefix, LLVM::DIFileAttr file, uint32_t line, LLVM::DIScopeAttr scope) + LLVM::DITypeAttr getDITypeWithFields(mlir::Location location, mlir::Type typeWithFields, std::string name, bool isNamePrefix, LLVM::DIFileAttr file, uint32_t line, LLVM::DIScopeAttr scope) { if (!isNamePrefix) { @@ -409,7 +409,7 @@ class LLVMDebugInfoHelper } } - auto elementDiType = getDITypeScriptType(elementType, file, line, scope); + auto elementDiType = getDITypeScriptType(location, elementType, file, line, scope); auto wrapperDiType = LLVM::DIDerivedTypeAttr::get(context, dwarf::DW_TAG_member, name, elementDiType, sizesTrack.elementSizeInBits, sizesTrack.elementAlignInBits, sizesTrack.offsetInBits); elements.push_back(wrapperDiType); @@ -438,12 +438,12 @@ class LLVMDebugInfoHelper sizeInBits, alignInBits, offsetInBits); } - LLVM::DISubroutineTypeAttr getDISubroutineType(LLVM::LLVMFunctionType funcType, LLVM::DIFileAttr file, uint32_t line, LLVM::DIScopeAttr scope) + LLVM::DISubroutineTypeAttr getDISubroutineType(mlir::Location location, LLVM::LLVMFunctionType funcType, LLVM::DIFileAttr file, uint32_t line, LLVM::DIScopeAttr scope) { llvm::SmallVector elements; for (auto retType : funcType.getReturnTypes()) { - elements.push_back(getDITypeScriptType(retType, file, line, scope)); + elements.push_back(getDITypeScriptType(location, retType, file, line, scope)); } if (funcType.getParams().size() > 0 && funcType.getReturnTypes().size() == 0) @@ -454,7 +454,7 @@ class LLVMDebugInfoHelper for (auto paramType : funcType.getParams()) { - elements.push_back(getDITypeScriptType(paramType, file, line, scope)); + elements.push_back(getDITypeScriptType(location, paramType, file, line, scope)); } auto subroutineType = LLVM::DISubroutineTypeAttr::get(context, elements); @@ -462,7 +462,7 @@ class LLVMDebugInfoHelper } // Seems LLVM::DIFlags::FwdDecl is resolving issue for me - LLVM::DITypeAttr getDIStructType(LLVM::LLVMStructType structType, LLVM::DIFileAttr file, uint32_t line, LLVM::DIScopeAttr scope) + LLVM::DITypeAttr getDIStructType(mlir::Location location, LLVM::LLVMStructType structType, LLVM::DIFileAttr file, uint32_t line, LLVM::DIScopeAttr scope) { if (structType.isIdentified()) { @@ -494,7 +494,7 @@ class LLVMDebugInfoHelper StringAttr elementName = StringAttr::get(context, std::to_string(index)); - auto elementDiType = getDILLVMType(llvmElementType, file, line, scope); + auto elementDiType = getDILLVMType(location, llvmElementType, file, line, scope); auto wrapperDiType = LLVM::DIDerivedTypeAttr::get(context, dwarf::DW_TAG_member, elementName, elementDiType, sizesTrack.elementSizeInBits, sizesTrack.elementAlignInBits, sizesTrack.offsetInBits); elements.push_back(wrapperDiType); @@ -514,7 +514,7 @@ class LLVMDebugInfoHelper return compositeType; } - LLVM::DICompositeTypeAttr getDIStructType(StringRef name, ArrayRef> fields, LLVM::DIFileAttr file, uint32_t line, LLVM::DIScopeAttr scope) + LLVM::DICompositeTypeAttr getDIStructType(mlir::Location location, StringRef name, ArrayRef> fields, LLVM::DIFileAttr file, uint32_t line, LLVM::DIScopeAttr scope) { MLIRTypeHelper mth(context); @@ -531,7 +531,7 @@ class LLVMDebugInfoHelper sizesTrack.nextElementType(llvmElementType); - auto elementDiType = getDIType(llvmElementType, elementType, file, line, scope); + auto elementDiType = getDIType(location, llvmElementType, elementType, file, line, scope); auto wrapperDiType = LLVM::DIDerivedTypeAttr::get(context, dwarf::DW_TAG_member, name, elementDiType, sizesTrack.elementSizeInBits, sizesTrack.elementAlignInBits, sizesTrack.offsetInBits); elements.push_back(wrapperDiType); @@ -566,7 +566,7 @@ class LLVMDebugInfoHelper file, line, scope, LLVM::DITypeAttr(), LLVM::DIFlags::TypePassByValue, sizesTrack.sizeInBits, sizesTrack.alignInBits, elements); } - LLVM::DICompositeTypeAttr getDIUnionType(mlir_ts::UnionType unionType, LLVM::DIFileAttr file, uint32_t line, LLVM::DIScopeAttr scope) + LLVM::DICompositeTypeAttr getDIUnionType(mlir::Location location, mlir_ts::UnionType unionType, LLVM::DIFileAttr file, uint32_t line, LLVM::DIScopeAttr scope) { auto sizeInBits = 0; @@ -584,7 +584,7 @@ class LLVMDebugInfoHelper // name auto name = mth.getLabelName(elementType); - auto elementDiType = getDITypeScriptType(elementType, file, line, scope); + auto elementDiType = getDITypeScriptType(location, elementType, file, line, scope); auto wrapperDiType = LLVM::DIDerivedTypeAttr::get(context, dwarf::DW_TAG_member, name, elementDiType, sizesTrack.elementSizeInBits, sizesTrack.elementAlignInBits, 0); elements.push_back(wrapperDiType); diff --git a/tsc/include/TypeScript/LowerToLLVM/LLVMDebugInfoFixer.h b/tsc/include/TypeScript/LowerToLLVM/LLVMDebugInfoFixer.h index 9f4c8be82..53cefbb33 100644 --- a/tsc/include/TypeScript/LowerToLLVM/LLVMDebugInfoFixer.h +++ b/tsc/include/TypeScript/LowerToLLVM/LLVMDebugInfoFixer.h @@ -53,7 +53,7 @@ class LLVMDebugInfoHelperFixer SmallVector resultTypes; for (auto resType : funcOp.getResultTypes()) { - auto diType = di.getDIType({}, resType, file, line, file); + auto diType = di.getDIType(location, {}, resType, file, line, file); resultTypes.push_back(diType); } @@ -65,7 +65,7 @@ class LLVMDebugInfoHelperFixer for (auto argType : funcOp.getArgumentTypes()) { - auto diType = di.getDIType({}, argType, file, line, file); + auto diType = di.getDIType(location, {}, argType, file, line, file); resultTypes.push_back(diType); } diff --git a/tsc/include/TypeScript/MLIRLogic/MLIRHelper.h b/tsc/include/TypeScript/MLIRLogic/MLIRHelper.h index aca548bd5..447ec136a 100644 --- a/tsc/include/TypeScript/MLIRLogic/MLIRHelper.h +++ b/tsc/include/TypeScript/MLIRLogic/MLIRHelper.h @@ -128,7 +128,7 @@ class MLIRHelper assert(line != 0 || column != 0); auto hashCode = hash_value(fileName); - ssName << 'L' << line << 'C' << column << 'FH' << hashCode; + ssName << 'L' << line << 'C' << column << "FH" << hashCode; }) .Case([&](auto loc) { getAnonymousNameStep(ssName, loc.getChildLoc()); diff --git a/tsc/include/TypeScript/MLIRLogic/MLIRTypeHelper.h b/tsc/include/TypeScript/MLIRLogic/MLIRTypeHelper.h index 1e1d8df8b..801aee26e 100644 --- a/tsc/include/TypeScript/MLIRLogic/MLIRTypeHelper.h +++ b/tsc/include/TypeScript/MLIRLogic/MLIRTypeHelper.h @@ -499,11 +499,11 @@ class MLIRTypeHelper return actualType; } - mlir::Type mergeUnionType(mlir::Type type) + mlir::Type mergeUnionType(mlir::Location location, mlir::Type type) { if (auto unionType = type.dyn_cast()) { - return getUnionTypeWithMerge(unionType); + return getUnionTypeWithMerge(location, unionType); } return type; @@ -1010,11 +1010,89 @@ class MLIRTypeHelper startParam); } + mlir_ts::FunctionType GetFunctionType(mlir_ts::HybridFunctionType hybridFuncType) { + return mlir_ts::FunctionType::get(hybridFuncType.getContext(), hybridFuncType.getInputs(), hybridFuncType.getResults(), hybridFuncType.isVarArg()); + } + + mlir_ts::FunctionType GetFunctionType(mlir_ts::BoundFunctionType boundFuncType) { + return mlir_ts::FunctionType::get(boundFuncType.getContext(), boundFuncType.getInputs(), boundFuncType.getResults(), boundFuncType.isVarArg()); + } + + mlir_ts::FunctionType GetFunctionType(mlir_ts::ExtensionFunctionType extFuncType) { + return mlir_ts::FunctionType::get(extFuncType.getContext(), extFuncType.getInputs(), extFuncType.getResults(), extFuncType.isVarArg()); + } + + mlir_ts::FunctionType GetFunctionType(mlir::Type inFuncType) { + mlir_ts::FunctionType funcType; + mlir::TypeSwitch(inFuncType) + .Case([&](auto functionType) { funcType = functionType; }) + .Case([&](auto hybridFunctionType) { funcType = GetFunctionType(hybridFunctionType); }) + .Case([&](auto boundFunctionType) { funcType = GetFunctionType(boundFunctionType); }) + .Case([&](auto extensionFunctionType) { funcType = GetFunctionType(extensionFunctionType); }) + .Default([&](auto type) { funcType = inFuncType.cast(); }); + + return funcType; + } + + bool CanCastFunctionTypeToFunctionType(mlir::Type inFuncType, mlir::Type resFuncType) { + bool result = false; + mlir::TypeSwitch(inFuncType) + .Case([&](auto functionType) { result = CanCastFunctionTypeToFunctionType(functionType, resFuncType); }) + .Case([&](auto hybridFunctionType) { result = CanCastFunctionTypeToFunctionType(hybridFunctionType, resFuncType); }) + .Case([&](auto boundFunctionType) { result = CanCastFunctionTypeToFunctionType(boundFunctionType, resFuncType); }) + .Case([&](auto extensionFunctionType) { result = CanCastFunctionTypeToFunctionType(extensionFunctionType, resFuncType); }); + + return result; + } + + bool CanCastFunctionTypeToFunctionType(mlir_ts::FunctionType inFuncType, mlir::Type resFuncType) { + bool result = false; + mlir::TypeSwitch(resFuncType) + .Case([&](auto functionType) { result = true; }) + .Case([&](auto hybridFunctionType) { result = true; }) + .Case([&](auto boundFunctionType) { result = false; }) + .Case([&](auto extensionFunctionType) { result = false; }); + + return result; + } + + bool CanCastFunctionTypeToFunctionType(mlir_ts::HybridFunctionType inFuncType, mlir::Type resFuncType) { + bool result = false; + mlir::TypeSwitch(resFuncType) + .Case([&](auto functionType) { result = true; /*TODO: extra checking at runtime should be performed */ }) + .Case([&](auto hybridFunctionType) { result = true; }) + .Case([&](auto boundFunctionType) { result = true; /*TODO: extra checking at runtime should be performed */ }) + .Case([&](auto extensionFunctionType) { result = true; /*TODO: extra checking at runtime should be performed */ }); + + return result; + } + + bool CanCastFunctionTypeToFunctionType(mlir_ts::BoundFunctionType inFuncType, mlir::Type resFuncType) { + bool result = false; + mlir::TypeSwitch(resFuncType) + .Case([&](auto functionType) { result = false; }) + .Case([&](auto hybridFunctionType) { result = true; }) + .Case([&](auto boundFunctionType) { result = true; }) + .Case([&](auto extensionFunctionType) { result = true; }); + + return result; + } + + bool CanCastFunctionTypeToFunctionType(mlir_ts::ExtensionFunctionType inFuncType, mlir::Type resFuncType) { + bool result = false; + mlir::TypeSwitch(resFuncType) + .Case([&](auto functionType) { result = false; }) + .Case([&](auto hybridFunctionType) { result = true; }) + .Case([&](auto boundFunctionType) { result = true; }) + .Case([&](auto extensionFunctionType) { result = true; }); + + return result; + } + MatchResult TestFunctionTypesMatchWithObjectMethods(mlir::Type inFuncType, mlir::Type resFuncType, unsigned startParamIn = 0, unsigned startParamRes = 0) { - return TestFunctionTypesMatchWithObjectMethods(inFuncType.cast(), resFuncType.cast(), - startParamIn, startParamRes); + return TestFunctionTypesMatchWithObjectMethods(GetFunctionType(inFuncType), GetFunctionType(resFuncType), startParamIn, startParamRes); } MatchResult TestFunctionTypesMatchWithObjectMethods(mlir_ts::FunctionType inFuncType, mlir_ts::FunctionType resFuncType, @@ -1179,11 +1257,10 @@ class MLIRTypeHelper return true; } - mlir::LogicalResult canCastTupleToInterface(mlir_ts::TupleType tupleStorageType, + mlir::LogicalResult canCastTupleToInterface(mlir::Location location, mlir_ts::TupleType tupleStorageType, InterfaceInfo::TypePtr newInterfacePtr) { SmallVector virtualTable; - auto location = mlir::UnknownLoc::get(context); return getInterfaceVirtualTableForObject(location, tupleStorageType, newInterfacePtr, virtualTable, true); } @@ -1279,7 +1356,7 @@ class MLIRTypeHelper return result; } - bool canCastFromTo(mlir::Type srcType, mlir::Type destType) + bool canCastFromTo(mlir::Location location, mlir::Type srcType, mlir::Type destType) { if (srcType == destType) { @@ -1309,7 +1386,7 @@ class MLIRTypeHelper { if (auto ifaceTypeInfo = getInterfaceInfoByFullName(ifaceType.getName().getValue())) { - return mlir::succeeded(canCastTupleToInterface(convertConstTupleTypeToTupleType(constTuple), ifaceTypeInfo)); + return mlir::succeeded(canCastTupleToInterface(location, convertConstTupleTypeToTupleType(constTuple), ifaceTypeInfo)); } else { @@ -1337,7 +1414,7 @@ class MLIRTypeHelper { if (auto ifaceTypeInfo = getInterfaceInfoByFullName(ifaceType.getName().getValue())) { - return mlir::succeeded(canCastTupleToInterface(tuple, ifaceTypeInfo)); + return mlir::succeeded(canCastTupleToInterface(location, tuple, ifaceTypeInfo)); } else { @@ -1374,7 +1451,7 @@ class MLIRTypeHelper if (auto unionType = destType.dyn_cast()) { // calculate store size - auto pred = [&](auto &item) { return canCastFromTo(item, srcType); }; + auto pred = [&](auto &item) { return canCastFromTo(location, item, srcType); }; auto types = unionType.getTypes(); if (std::find_if(types.begin(), types.end(), pred) == types.end()) { @@ -1695,20 +1772,20 @@ class MLIRTypeHelper return type; } - bool isUnionTypeNeedsTag(mlir_ts::UnionType unionType) + bool isUnionTypeNeedsTag(mlir::Location location, mlir_ts::UnionType unionType) { mlir::Type baseType; - return isUnionTypeNeedsTag(unionType, baseType); + return isUnionTypeNeedsTag(location, unionType, baseType); } - bool isUnionTypeNeedsTag(mlir_ts::UnionType unionType, mlir::Type &baseType) + bool isUnionTypeNeedsTag(mlir::Location location, mlir_ts::UnionType unionType, mlir::Type &baseType) { - auto storeType = getUnionTypeWithMerge(unionType.getTypes(), true); + auto storeType = getUnionTypeWithMerge(location, unionType.getTypes(), true); baseType = storeType; return storeType.isa(); } - ExtendsResult appendInferTypeToContext(mlir::Type srcType, mlir_ts::InferType inferType, llvm::StringMap> &typeParamsWithArgs, bool useTupleType = false) + ExtendsResult appendInferTypeToContext(mlir::Location location, mlir::Type srcType, mlir_ts::InferType inferType, llvm::StringMap> &typeParamsWithArgs, bool useTupleType = false) { auto name = inferType.getElementType().cast().getName().getValue(); auto currentType = srcType; @@ -1746,7 +1823,7 @@ class MLIRTypeHelper } else { - auto defaultUnionType = getUnionType(existType.second, currentType); + auto defaultUnionType = getUnionType(location, existType.second, currentType); LLVM_DEBUG(llvm::dbgs() << "\n!! existing type: " << existType.second << " default type: " << defaultUnionType << "\n";); @@ -2117,7 +2194,7 @@ class MLIRTypeHelper llvm_unreachable("not implemented"); } - ExtendsResult extendsTypeFuncTypes(mlir::Type srcType, mlir::Type extendType, + ExtendsResult extendsTypeFuncTypes(mlir::Location location, mlir::Type srcType, mlir::Type extendType, llvm::StringMap> &typeParamsWithArgs, int skipSrcParams = 0) { auto srcParams = getParamsFromFuncRef(srcType); @@ -2129,10 +2206,10 @@ class MLIRTypeHelper auto srcReturnType = getReturnTypeFromFuncRef(srcType); auto extReturnType = getReturnTypeFromFuncRef(extendType); - return extendsTypeFuncTypes(srcParams, extParams, extIsVarArgs, srcReturnType, extReturnType, typeParamsWithArgs, skipSrcParams); + return extendsTypeFuncTypes(location, srcParams, extParams, extIsVarArgs, srcReturnType, extReturnType, typeParamsWithArgs, skipSrcParams); } - ExtendsResult extendsTypeFuncTypes(ArrayRef srcParams, ArrayRef extParams, bool extIsVarArgs, + ExtendsResult extendsTypeFuncTypes(mlir::Location location, ArrayRef srcParams, ArrayRef extParams, bool extIsVarArgs, mlir::Type srcReturnType, mlir::Type extReturnType, llvm::StringMap> &typeParamsWithArgs, int skipSrcParams = 0) { @@ -2168,7 +2245,7 @@ class MLIRTypeHelper } } - auto extendsResult = extendsType(srcParamType, extParamType, typeParamsWithArgs, useTupleWhenMergeTypes); + auto extendsResult = extendsType(location, srcParamType, extParamType, typeParamsWithArgs, useTupleWhenMergeTypes); if (extendsResult != ExtendsResult::True) { return extendsResult; @@ -2176,11 +2253,11 @@ class MLIRTypeHelper } // compare return types - return extendsType(srcReturnType, extReturnType, typeParamsWithArgs); + return extendsType(location, srcReturnType, extReturnType, typeParamsWithArgs); } - ExtendsResult extendsType(mlir::Type srcType, mlir::Type extendType, llvm::StringMap> &typeParamsWithArgs, bool useTupleWhenMergeTypes = false) + ExtendsResult extendsType(mlir::Location location, mlir::Type srcType, mlir::Type extendType, llvm::StringMap> &typeParamsWithArgs, bool useTupleWhenMergeTypes = false) { LLVM_DEBUG(llvm::dbgs() << "\n!! is extending type: [ " << srcType << " ] extend type: [ " << extendType << " ]\n";); @@ -2216,7 +2293,7 @@ class MLIRTypeHelper getAllInferTypes(extendType, inferTypes); for (auto inferType : inferTypes) { - appendInferTypeToContext(mlir_ts::UnknownType::get(context), inferType, typeParamsWithArgs, useTupleWhenMergeTypes); + appendInferTypeToContext(location, mlir_ts::UnknownType::get(context), inferType, typeParamsWithArgs, useTupleWhenMergeTypes); } // TODO: add all infer types in extends to "unknown" @@ -2229,7 +2306,7 @@ class MLIRTypeHelper getAllInferTypes(extendType, inferTypes); for (auto inferType : inferTypes) { - appendInferTypeToContext(mlir_ts::NeverType::get(context), inferType, typeParamsWithArgs, useTupleWhenMergeTypes); + appendInferTypeToContext(location, mlir_ts::NeverType::get(context), inferType, typeParamsWithArgs, useTupleWhenMergeTypes); } // TODO: add all infer types in extends to "never" @@ -2239,7 +2316,7 @@ class MLIRTypeHelper // to support infer types if (auto inferType = extendType.dyn_cast()) { - return appendInferTypeToContext(srcType, inferType, typeParamsWithArgs, useTupleWhenMergeTypes); + return appendInferTypeToContext(location, srcType, inferType, typeParamsWithArgs, useTupleWhenMergeTypes); } if (!srcType) @@ -2251,7 +2328,7 @@ class MLIRTypeHelper { auto falseResult = ExtendsResult::False; auto pred = [&](auto &item) { - auto unionExtResult = extendsType(srcType, item, typeParamsWithArgs); + auto unionExtResult = extendsType(location, srcType, item, typeParamsWithArgs); if (unionExtResult == ExtendsResult::Never) { falseResult = unionExtResult; @@ -2270,7 +2347,7 @@ class MLIRTypeHelper if (item.id) { auto fieldType = getFieldTypeByFieldName(srcType, item.id); - auto fieldExtResult = extendsType(fieldType, item.type, typeParamsWithArgs); + auto fieldExtResult = extendsType(location, fieldType, item.type, typeParamsWithArgs); if (fieldExtResult == ExtendsResult::Never) { falseResult = fieldExtResult; @@ -2294,7 +2371,7 @@ class MLIRTypeHelper if (item.id) { auto fieldType = getFieldTypeByFieldName(srcType, item.id); - auto fieldExtResult = extendsType(fieldType, item.type, typeParamsWithArgs); + auto fieldExtResult = extendsType(location, fieldType, item.type, typeParamsWithArgs); if (fieldExtResult == ExtendsResult::Never) { falseResult = fieldExtResult; @@ -2318,14 +2395,14 @@ class MLIRTypeHelper return ExtendsResult::False; } - return extendsType(literalType.getElementType(), extendType, typeParamsWithArgs); + return extendsType(location, literalType.getElementType(), extendType, typeParamsWithArgs); } if (auto srcArray = srcType.dyn_cast()) { if (auto extArray = extendType.dyn_cast()) { - return extendsType(srcArray.getElementType(), extArray.getElementType(), typeParamsWithArgs); + return extendsType(location, srcArray.getElementType(), extArray.getElementType(), typeParamsWithArgs); } } @@ -2333,21 +2410,21 @@ class MLIRTypeHelper { if (auto extArray = extendType.dyn_cast()) { - return extendsType(srcArray.getElementType(), extArray.getElementType(), typeParamsWithArgs); + return extendsType(location, srcArray.getElementType(), extArray.getElementType(), typeParamsWithArgs); } } // Special case when we have string type (widen from Literal Type) if (auto literalType = extendType.dyn_cast()) { - return extendsType(srcType, literalType.getElementType(), typeParamsWithArgs); + return extendsType(location, srcType, literalType.getElementType(), typeParamsWithArgs); } if (auto unionType = srcType.dyn_cast()) { auto falseResult = ExtendsResult::False; auto pred = [&](auto &item) { - auto unionExtResult = extendsType(item, extendType, typeParamsWithArgs); + auto unionExtResult = extendsType(location, item, extendType, typeParamsWithArgs); if (unionExtResult == ExtendsResult::Never) { falseResult = unionExtResult; @@ -2387,7 +2464,7 @@ class MLIRTypeHelper auto srcType = srcFound->getValue().second; auto extType = extFound->getValue().second; - return extendsType(srcType, extType, typeParamsWithArgs); + return extendsType(location, srcType, extType, typeParamsWithArgs); } else { @@ -2431,7 +2508,7 @@ class MLIRTypeHelper auto srcType = srcFound->getValue().second; auto extType = extFound->getValue().second; - return extendsType(srcType, extType, typeParamsWithArgs); + return extendsType(location, srcType, extType, typeParamsWithArgs); } else { @@ -2450,7 +2527,7 @@ class MLIRTypeHelper if (isAnyFunctionType(srcType) && isAnyFunctionType(extendType)) { - return extendsTypeFuncTypes(srcType, extendType, typeParamsWithArgs); + return extendsTypeFuncTypes(location, srcType, extendType, typeParamsWithArgs); } if (auto constructType = extendType.dyn_cast()) @@ -2472,7 +2549,7 @@ class MLIRTypeHelper constrMethod->funcType.getInputs(), {srcClassInfo->classType}, constrMethod->funcType.isVarArg()); - return extendsTypeFuncTypes(constrWithRetType, extendType, typeParamsWithArgs, 1/*because of this param*/); + return extendsTypeFuncTypes(location, constrWithRetType, extendType, typeParamsWithArgs, 1/*because of this param*/); } } @@ -2486,7 +2563,7 @@ class MLIRTypeHelper auto falseResult = ExtendsResult::False; for (auto extend : interfaceInfo->extends) { - auto extResult = extendsType(extend.second->interfaceType, extendType, typeParamsWithArgs); + auto extResult = extendsType(location, extend.second->interfaceType, extendType, typeParamsWithArgs); if (isTrue(extResult)) { return extResult; @@ -2508,7 +2585,7 @@ class MLIRTypeHelper auto falseResult = ExtendsResult::False; for (auto extend : classInfo->baseClasses) { - auto extResult = extendsType(extend->classType, extendType, typeParamsWithArgs); + auto extResult = extendsType(location, extend->classType, extendType, typeParamsWithArgs); if (extResult == ExtendsResult::True) { return ExtendsResult::True; @@ -2524,7 +2601,7 @@ class MLIRTypeHelper { for (auto extend : classInfo->implements) { - auto extResult = extendsType(extend.interface->interfaceType, extendType, typeParamsWithArgs); + auto extResult = extendsType(location, extend.interface->interfaceType, extendType, typeParamsWithArgs); if (isTrue(extResult)) { return extResult; @@ -2648,14 +2725,14 @@ class MLIRTypeHelper return mlir::success(); } - mlir::Type getUnionType(mlir::Type type1, mlir::Type type2, bool mergeLiterals = true, bool mergeTypes = true) + mlir::Type getUnionType(mlir::Location location, mlir::Type type1, mlir::Type type2, bool mergeLiterals = true, bool mergeTypes = true) { - if (canCastFromTo(type1, type2)) + if (canCastFromTo(location, type1, type2)) { return type2; } - if (canCastFromTo(type2, type1)) + if (canCastFromTo(location, type2, type1)) { return type1; } @@ -2663,11 +2740,11 @@ class MLIRTypeHelper mlir::SmallVector types; types.push_back(type1); types.push_back(type2); - return getUnionTypeWithMerge(types, mergeLiterals, mergeTypes); + return getUnionTypeWithMerge(location, types, mergeLiterals, mergeTypes); } // TODO: review all union merge logic - mlir::Type getUnionTypeMergeTypes(UnionTypeProcessContext &unionContext, bool mergeLiterals = true, bool mergeTypes = true) + mlir::Type getUnionTypeMergeTypes(mlir::Location location, UnionTypeProcessContext &unionContext, bool mergeLiterals = true, bool mergeTypes = true) { // merge types with literal types for (auto literalType : unionContext.literalTypes) @@ -2726,7 +2803,7 @@ class MLIRTypeHelper if (mergeTypes && !doNotMergeLiterals) { mlir::SmallVector mergedTypesAll; - this->mergeTypes(typesAll, mergedTypesAll); + this->mergeTypes(location, typesAll, mergedTypesAll); mlir::Type retType = mergedTypesAll.size() == 1 ? mergedTypesAll.front() : getUnionType(mergedTypesAll); if (unionContext.isUndefined) @@ -2781,7 +2858,7 @@ class MLIRTypeHelper } } - mlir::Type getUnionTypeWithMerge(mlir::ArrayRef types, bool mergeLiterals = true, bool mergeTypes = true) + mlir::Type getUnionTypeWithMerge(mlir::Location location, mlir::ArrayRef types, bool mergeLiterals = true, bool mergeTypes = true) { UnionTypeProcessContext unionContext = {}; @@ -2803,7 +2880,7 @@ class MLIRTypeHelper processUnionTypeItem(type, unionContext); } - return getUnionTypeMergeTypes(unionContext, mergeLiterals, mergeTypes); + return getUnionTypeMergeTypes(location, unionContext, mergeLiterals, mergeTypes); } mlir::Type getUnionType(mlir::SmallVector &types) @@ -2923,7 +3000,7 @@ class MLIRTypeHelper }); } - void mergeTypes(mlir::ArrayRef types, mlir::SmallVector &mergedTypes) + void mergeTypes(mlir::Location location, mlir::ArrayRef types, mlir::SmallVector &mergedTypes) { for (auto typeItem : types) { @@ -2937,7 +3014,7 @@ class MLIRTypeHelper for (auto [index, mergedType] : enumerate(mergedTypes)) { auto merged = false; - auto resultType = mergeType(mergedType, typeItem, merged); + auto resultType = mergeType(location, mergedType, typeItem, merged); if (merged) { mergedTypes[index] = resultType; @@ -2953,7 +3030,7 @@ class MLIRTypeHelper } } - mlir::Type arrayMergeType(mlir::Type existType, mlir::Type currentType, bool& merged) + mlir::Type arrayMergeType(mlir::Location location, mlir::Type existType, mlir::Type currentType, bool& merged) { LLVM_DEBUG(llvm::dbgs() << "\n!! merging existing type: " << existType << " with " << currentType << "\n";); @@ -2968,14 +3045,14 @@ class MLIRTypeHelper auto existTypeArray = existType.dyn_cast_or_null(); if (currentTypeArray && existTypeArray) { - auto arrayElementMerged = mergeType(existTypeArray.getElementType(), currentTypeArray.getElementType(), merged); + auto arrayElementMerged = mergeType(location, existTypeArray.getElementType(), currentTypeArray.getElementType(), merged); return mlir_ts::ArrayType::get(arrayElementMerged); } - return mergeType(existType, currentType, merged); + return mergeType(location, existType, currentType, merged); } - mlir::Type tupleMergeType(mlir_ts::TupleType existType, mlir_ts::TupleType currentType, bool& merged) + mlir::Type tupleMergeType(mlir::Location location, mlir_ts::TupleType existType, mlir_ts::TupleType currentType, bool& merged) { merged = false; LLVM_DEBUG(llvm::dbgs() << "\n!! merging existing type: " << existType << " with " << currentType << "\n";); @@ -3006,7 +3083,7 @@ class MLIRTypeHelper // try to merge types of tuple auto merged = false; - auto mergedType = mergeType(existingIt->type, currentIt->type, merged); + auto mergedType = mergeType(location, existingIt->type, currentIt->type, merged); if (mergedType) { resultFields.push_back({ existingIt->id, mergedType, false }); @@ -3017,7 +3094,7 @@ class MLIRTypeHelper return mlir_ts::TupleType::get(context, resultFields); } - mlir::Type mergeType(mlir::Type existType, mlir::Type currentType, bool& merged) + mlir::Type mergeType(mlir::Location location, mlir::Type existType, mlir::Type currentType, bool& merged) { merged = false; LLVM_DEBUG(llvm::dbgs() << "\n!! merging existing \n\ttype: \t" << existType << "\n\twith \t" << currentType << "\n";); @@ -3028,13 +3105,13 @@ class MLIRTypeHelper return existType; } - if (canCastFromTo(currentType, existType)) + if (canCastFromTo(location, currentType, existType)) { merged = true; return existType; } - if (canCastFromTo(existType, currentType)) + if (canCastFromTo(location, existType, currentType)) { merged = true; return currentType; @@ -3057,7 +3134,7 @@ class MLIRTypeHelper if (auto currentTupleType = currentType.dyn_cast()) { auto tupleMerged = false; - auto mergedTupleType = tupleMergeType(existingTupleType, currentTupleType, tupleMerged); + auto mergedTupleType = tupleMergeType(location, existingTupleType, currentTupleType, tupleMerged); if (tupleMerged) { merged = true; @@ -3077,7 +3154,7 @@ class MLIRTypeHelper if (existType.isa()) { - defaultUnionType = getUnionTypeWithMerge(types); + defaultUnionType = getUnionTypeWithMerge(location, types); } else { diff --git a/tsc/lib/TypeScript/LowerToLLVM.cpp b/tsc/lib/TypeScript/LowerToLLVM.cpp index ef8b16d34..312d44ac1 100644 --- a/tsc/lib/TypeScript/LowerToLLVM.cpp +++ b/tsc/lib/TypeScript/LowerToLLVM.cpp @@ -1598,7 +1598,7 @@ struct CreateUnionInstanceOpLowering : public TsLlvmPattern())) + if (!mth.isUnionTypeNeedsTag(loc, op.getType().cast())) { // this is union of tuples, no need to add Tag to it // create tagged union @@ -1647,7 +1647,7 @@ struct GetValueFromUnionOpLowering : public TsLlvmPattern()); + bool needTag = mth.isUnionTypeNeedsTag(loc, op.getIn().getType().cast()); if (needTag) { auto in = transformed.getIn(); @@ -1698,7 +1698,7 @@ struct GetTypeInfoFromUnionOpLowering : public TsLlvmPatterngetLoc(); mlir::Type baseType; - bool needTag = mth.isUnionTypeNeedsTag(op.getIn().getType().cast(), baseType); + bool needTag = mth.isUnionTypeNeedsTag(loc, op.getIn().getType().cast(), baseType); if (needTag) { auto val0 = rewriter.create(loc, tch.convertType(op.getType()), transformed.getIn(), @@ -5582,7 +5582,7 @@ static void populateTypeScriptConversionPatterns(LLVMTypeConverter &converter, m MLIRTypeHelper mth(m.getContext()); mlir::Type selectedType = ltch.findMaxSizeType(type); - bool needTag = mth.isUnionTypeNeedsTag(type); + bool needTag = mth.isUnionTypeNeedsTag(mlir::UnknownLoc::get(type.getContext()), type); LLVM_DEBUG(llvm::dbgs() << "\n!! max size type in union: " << selectedType << "\n size: " << ltch.getTypeSizeEstimateInBytes(selectedType) << "\n Tag: " << (needTag ? "yes" : "no") @@ -5855,7 +5855,7 @@ static LogicalResult preserveTypesForDebugInfo(mlir::ModuleOp &module, LLVMTypeC // TODO: finish the DI logic unsigned alignInBits = llvmTypeConverter.getPointerBitwidth(); - auto diType = di.getDIType(mlir::Type(), dataType, file, line, file); + auto diType = di.getDIType(location, mlir::Type(), dataType, file, line, file); // MLIRTypeHelper mth(module.getContext()); // if ((mth.isAnyFunctionType(dataType) || dataType.isa()) && argIndex > 0) { diff --git a/tsc/lib/TypeScript/MLIRGen.cpp b/tsc/lib/TypeScript/MLIRGen.cpp index 2f7281f72..70bd7fd20 100644 --- a/tsc/lib/TypeScript/MLIRGen.cpp +++ b/tsc/lib/TypeScript/MLIRGen.cpp @@ -1563,7 +1563,7 @@ class MLIRGenImpl if (existType) { auto merged = false; - currentType = mth.mergeType(existType, currentType, merged); + currentType = mth.mergeType(location, existType, currentType, merged); LLVM_DEBUG(llvm::dbgs() << "\n!! result type: " << currentType << "\n";); results[name] = currentType; @@ -2229,7 +2229,7 @@ class MLIRGenImpl { auto argOp = genericTypeGenContext.callOperands[varArgIndex]; - accumulateArrayItemType(argOp.getType(), arrayInfo); + accumulateArrayItemType(location, argOp.getType(), arrayInfo); } mlir::Type arrayType = getArrayType(arrayInfo.accumulatedArrayElementType); @@ -6139,7 +6139,7 @@ class MLIRGenImpl #endif } - mlir::LogicalResult processReturnType(mlir::Value expressionValue, const GenContext &genContext) + mlir::LogicalResult processReturnType(mlir::Location location, mlir::Value expressionValue, const GenContext &genContext) { // TODO: rewrite it using UnionType @@ -6184,7 +6184,7 @@ class MLIRGenImpl // } auto merged = false; - auto resultReturnType = mth.mergeType(genContext.passResult->functionReturnType, type, merged); + auto resultReturnType = mth.mergeType(location, genContext.passResult->functionReturnType, type, merged); LLVM_DEBUG(dbgs() << "\n!! return type: " << resultReturnType << ""); @@ -6236,7 +6236,7 @@ class MLIRGenImpl } // record return type if not provided - processReturnType(expressionValue, genContext); + processReturnType(location, expressionValue, genContext); if (!expressionValue) { @@ -8022,7 +8022,7 @@ class MLIRGenImpl if (resultTrue && resultFalse) { - auto defaultUnionType = getUnionType(resultTrue.getType(), resultFalse.getType()); + auto defaultUnionType = getUnionType(location, resultTrue.getType(), resultFalse.getType()); auto merged = false; auto resultType = mth.findBaseType(resultTrue.getType(), resultFalse.getType(), merged, defaultUnionType); @@ -8133,7 +8133,7 @@ class MLIRGenImpl } } - auto resultType = getUnionType(resultTrue.getType(), resultFalse.getType()); + auto resultType = getUnionType(location, resultTrue.getType(), resultFalse.getType()); ifOp->getResult(0).setType(resultType); @@ -8182,7 +8182,7 @@ class MLIRGenImpl auto leftExpressionValue = V(result); auto resultWhenFalseType = evaluate(rightExpression, genContext); - auto defaultUnionType = getUnionType(leftExpressionValue.getType(), resultWhenFalseType); + auto defaultUnionType = getUnionType(location, leftExpressionValue.getType(), resultWhenFalseType); auto merged = false; auto resultType = mth.findBaseType(resultWhenFalseType, leftExpressionValue.getType(), merged, defaultUnionType); @@ -11812,7 +11812,7 @@ class MLIRGenImpl { // TODO: review it, seems it should be resolved earlier auto name = MLIRHelper::getName(typeExpression.as()); - type = findEmbeddedType(name, newExpression->typeArguments, genContext); + type = findEmbeddedType(location, name, newExpression->typeArguments, genContext); if (type) { result = V(builder.create(location, type)); @@ -11833,7 +11833,7 @@ class MLIRGenImpl { if (newExpression->typeArguments > 0 && classType.getName().getValue().starts_with("Array<")) { - auto arrayType = findEmbeddedType("Array", newExpression->typeArguments, genContext); + auto arrayType = findEmbeddedType(location, "Array", newExpression->typeArguments, genContext); if (arrayType) { return NewArray(location, arrayType, newExpression->arguments, genContext); @@ -12406,7 +12406,7 @@ class MLIRGenImpl bool isVariableSizeOfSpreadElement; }; - mlir::LogicalResult accumulateArrayItemType(mlir::Type type, struct ArrayInfo &arrayInfo) + mlir::LogicalResult accumulateArrayItemType(mlir::Location location, mlir::Type type, struct ArrayInfo &arrayInfo) { auto elementType = arrayInfo.accumulatedArrayElementType; @@ -12434,7 +12434,7 @@ class MLIRGenImpl } auto merged = false; - elementType = mth.mergeType(elementType, wideType, merged); + elementType = mth.mergeType(location, elementType, wideType, merged); } //LLVM_DEBUG(llvm::dbgs() << "\n!! result element type: " << elementType << "\n";); @@ -12469,7 +12469,7 @@ class MLIRGenImpl values.push_back({newConstVal, false, false}); } - accumulateArrayItemType(constArray.getElementType(), arrayInfo); + accumulateArrayItemType(location, constArray.getElementType(), arrayInfo); return mlir::success(); } @@ -12480,7 +12480,7 @@ class MLIRGenImpl values.push_back({itemValue, true, true}); auto arrayElementType = mth.wideStorageType(array.getElementType()); - accumulateArrayItemType(arrayElementType, arrayInfo); + accumulateArrayItemType(location, arrayElementType, arrayInfo); return mlir::success(); } @@ -12490,7 +12490,7 @@ class MLIRGenImpl // TODO: implement method to concat array with const-length array in one operation without using 'push' for each element values.push_back({itemValue, true, true}); - accumulateArrayItemType(getCharType(), arrayInfo); + accumulateArrayItemType(location, getCharType(), arrayInfo); return mlir::success(); } @@ -12519,7 +12519,7 @@ class MLIRGenImpl values.push_back({itemValue, true, true}); auto arrayElementType = mth.wideStorageType(fields.front().type); - accumulateArrayItemType(arrayElementType, arrayInfo); + accumulateArrayItemType(location, arrayElementType, arrayInfo); } else { @@ -12547,7 +12547,7 @@ class MLIRGenImpl values.push_back({newConstVal, false, false}); - accumulateArrayItemType(constTuple.getFieldInfo(index).type, arrayInfo); + accumulateArrayItemType(location, constTuple.getFieldInfo(index).type, arrayInfo); } } @@ -12559,7 +12559,7 @@ class MLIRGenImpl values.push_back({itemValue, true, false}); for (auto tupleItem : tupleType) { - accumulateArrayItemType(tupleItem.type, arrayInfo); + accumulateArrayItemType(location, tupleItem.type, arrayInfo); } return mlir::success(); @@ -12622,7 +12622,7 @@ class MLIRGenImpl } values.push_back({itemValue, false, false}); - accumulateArrayItemType(type, arrayInfo); + accumulateArrayItemType(location, type, arrayInfo); } return mlir::success(); @@ -14351,7 +14351,8 @@ class MLIRGenImpl currentEnumValue++; } - auto storeType = mth.getUnionTypeWithMerge(enumLiteralTypes); + auto location = loc(enumDeclarationAST); + auto storeType = mth.getUnionTypeWithMerge(location, enumLiteralTypes); LLVM_DEBUG(llvm::dbgs() << "\n!! enum: " << namePtr << " storage type: " << storeType << "\n"); @@ -18360,7 +18361,7 @@ genContext); if (auto unionType = type.dyn_cast()) { mlir::Type baseType; - if (mth.isUnionTypeNeedsTag(unionType, baseType)) + if (mth.isUnionTypeNeedsTag(location, unionType, baseType)) { auto types = unionType.getTypes(); if (std::find(types.begin(), types.end(), valueType) == types.end()) @@ -18368,7 +18369,7 @@ genContext); // find which type we can cast to for (auto subType : types) { - if (mth.canCastFromTo(valueType, subType)) + if (mth.canCastFromTo(location, valueType, subType)) { CAST(value, location, subType, value, genContext); return V(builder.create(location, type, value)); @@ -18407,7 +18408,7 @@ genContext); // union -> any will be done later in CastLogic auto toAny = type.dyn_cast(); mlir::Type baseType; - if (!toAny && mth.isUnionTypeNeedsTag(unionType, baseType)) + if (!toAny && mth.isUnionTypeNeedsTag(location, unionType, baseType)) { return castFromUnion(location, type, value, genContext); } @@ -18441,22 +18442,6 @@ genContext); } } - // cast ext method to bound method - if (auto extFuncType = valueType.dyn_cast()) - { - if (auto hybridFuncType = type.dyn_cast()) - { - auto boundFunc = createBoundMethodFromExtensionMethod(location, value.getDefiningOp()); - return V(builder.create(location, type, boundFunc)); - } - - if (auto boundFuncType = type.dyn_cast()) - { - auto boundFunc = createBoundMethodFromExtensionMethod(location, value.getDefiningOp()); - return V(builder.create(location, type, boundFunc)); - } - } - // opaque to hybrid func if (auto opaqueType = valueType.dyn_cast()) { @@ -18475,11 +18460,45 @@ genContext); } } - if (mth.isAnyFunctionType(valueType) && mth.isAnyFunctionType(type) && mth.isGenericType(valueType)) { - // need to instantiate generic method - auto result = instantiateSpecializedFunction(location, value, type, genContext); - EXIT_IF_FAILED(result); + if (mth.isAnyFunctionType(valueType) && mth.isAnyFunctionType(type)) { + + if (mth.isGenericType(valueType)) + { + // need to instantiate generic method + auto result = instantiateSpecializedFunction(location, value, type, genContext); + EXIT_IF_FAILED(result); + } + // fall through to finish cast operation + if (!mth.CanCastFunctionTypeToFunctionType(valueType, type)) + { + emitError(location, "invalid cast from ") << valueType << " to " << type; + return mlir::failure(); + } + + // test fun types + auto test = mth.TestFunctionTypesMatchWithObjectMethods(valueType, type).result == MatchResultType::Match; + if (!test) + { + emitError(location) << valueType << " is not matching type " << type; + return mlir::failure(); + } + } + + // cast ext method to bound method + if (auto extFuncType = valueType.dyn_cast()) + { + if (auto hybridFuncType = type.dyn_cast()) + { + auto boundFunc = createBoundMethodFromExtensionMethod(location, value.getDefiningOp()); + return V(builder.create(location, type, boundFunc)); + } + + if (auto boundFuncType = type.dyn_cast()) + { + auto boundFunc = createBoundMethodFromExtensionMethod(location, value.getDefiningOp()); + return V(builder.create(location, type, boundFunc)); + } } // wrong casts @@ -18550,7 +18569,7 @@ genContext); { if (auto unionType = dyn_cast(value.getType())) { - if (auto normalizedUnion = dyn_cast(mth.getUnionTypeWithMerge(unionType.getTypes()))) + if (auto normalizedUnion = dyn_cast(mth.getUnionTypeWithMerge(location, unionType.getTypes()))) { // info, we add "_" extra as scanner append "_" in front of "__"; auto funcName = "___cast"; @@ -18680,17 +18699,21 @@ genContext); auto inEffective = in; - if (mlir::failed(mth.canCastTupleToInterface(tupleType.cast(), interfaceInfo))) + if (mlir::failed(mth.canCastTupleToInterface(location, tupleType.cast(), interfaceInfo))) { - SmallVector fields; - if (mlir::failed(interfaceInfo->getTupleTypeFields(fields, builder.getContext()))) - { - return mlir::Value(); - } + // SmallVector fields; + // if (mlir::failed(interfaceInfo->getTupleTypeFields(fields, builder.getContext()))) + // { + // return mlir::Value(); + // } + + // auto newInterfaceTupleType = getTupleType(fields); + // CAST(inEffective, location, newInterfaceTupleType, inEffective, genContext); + // tupleType = newInterfaceTupleType; - auto newInterfaceTupleType = getTupleType(fields); - CAST(inEffective, location, newInterfaceTupleType, inEffective, genContext); - tupleType = newInterfaceTupleType; + // TODO: you can create new Tuple with set of data, as tuple can be object with 'this' and internal values which needed to run commands + // by stipping important members of Tuple you break integrity of the code(program) + return mlir::Value(); } // TODO: finish it, what to finish it? maybe optimization not to create extra object? @@ -18896,7 +18919,7 @@ genContext); } else if (kind == SyntaxKind::InferType) { - return getInferType(typeReferenceAST.as(), genContext); + return getInferType(loc(typeReferenceAST), typeReferenceAST.as(), genContext); } else if (kind == SyntaxKind::OptionalType) { @@ -18915,7 +18938,7 @@ genContext); // return getAnyType(); } - mlir::Type getInferType(InferTypeNode inferTypeNodeAST, const GenContext &genContext) + mlir::Type getInferType(mlir::Location location, InferTypeNode inferTypeNodeAST, const GenContext &genContext) { auto type = getType(inferTypeNodeAST->typeParameter, genContext); auto inferType = getInferType(type); @@ -18924,7 +18947,7 @@ genContext); // TODO: review function 'extends' in MLIRTypeHelper with the same logic adding infer types to context auto &typeParamsWithArgs = const_cast(genContext).typeParamsWithArgs; - mth.appendInferTypeToContext(type, inferType, typeParamsWithArgs); + mth.appendInferTypeToContext(location, type, inferType, typeParamsWithArgs); return inferType; } @@ -19037,7 +19060,7 @@ genContext); return {mlir::failure(), IsGeneric::False}; } - auto extendsResult = mth.extendsType(type, constraintType, pairs); + auto extendsResult = mth.extendsType(location, type, constraintType, pairs); if (extendsResult != ExtendsResult::True) { // special case when we work with generic type(which are not specialized yet) @@ -19086,11 +19109,11 @@ genContext); auto merged = false; if (arrayMerge) { - type = mth.arrayMergeType(existType.second, type, merged); + type = mth.arrayMergeType(location, existType.second, type, merged); } else { - type = mth.mergeType(existType.second, type, merged); + type = mth.mergeType(location, existType.second, type, merged); } LLVM_DEBUG(llvm::dbgs() << "\n!! result (after merge) type: " << type << "\n";); @@ -19391,7 +19414,7 @@ genContext); return interfaceType; } - if (auto embedType = findEmbeddedType(name, typeReferenceAST->typeArguments, genContext)) + if (auto embedType = findEmbeddedType(location, name, typeReferenceAST->typeArguments, genContext)) { return embedType; } @@ -19402,7 +19425,7 @@ genContext); return type; } - if (auto embedType = findEmbeddedType(name, typeReferenceAST->typeArguments, genContext)) + if (auto embedType = findEmbeddedType(location, name, typeReferenceAST->typeArguments, genContext)) { return embedType; } @@ -19410,7 +19433,7 @@ genContext); return mlir::Type(); } - mlir::Type findEmbeddedType(std::string name, NodeArray &typeArguments, const GenContext &genContext) + mlir::Type findEmbeddedType(mlir::Location location, std::string name, NodeArray &typeArguments, const GenContext &genContext) { auto typeArgumentsSize = typeArguments->size(); if (typeArgumentsSize == 0) @@ -19431,7 +19454,7 @@ genContext); if (typeArgumentsSize > 1) { - if (auto type = getEmbeddedTypeWithManyParams(name, typeArguments, genContext)) + if (auto type = getEmbeddedTypeWithManyParams(location, name, typeArguments, genContext)) { return type; } @@ -19889,27 +19912,27 @@ genContext); return translate(typeArguments, genContext); } - mlir::Type getEmbeddedTypeWithManyParams(mlir::StringRef name, NodeArray &typeArguments, + mlir::Type getEmbeddedTypeWithManyParams(mlir::Location location, mlir::StringRef name, NodeArray &typeArguments, const GenContext &genContext) { return compileOptions.enableBuiltins - ? getEmbeddedTypeWithManyParamsBuiltins(name, typeArguments, genContext) + ? getEmbeddedTypeWithManyParamsBuiltins(location, name, typeArguments, genContext) : mlir::Type(); } - mlir::Type getEmbeddedTypeWithManyParamsBuiltins(mlir::StringRef name, NodeArray &typeArguments, + mlir::Type getEmbeddedTypeWithManyParamsBuiltins(mlir::Location location, mlir::StringRef name, NodeArray &typeArguments, const GenContext &genContext) { auto translate = llvm::StringSwitch &, const GenContext &)>>(name) .Case("Exclude", [&] (auto typeArguments, auto genContext) { auto firstType = getFirstTypeFromTypeArguments(typeArguments, genContext); auto secondType = getSecondTypeFromTypeArguments(typeArguments, genContext); - return ExcludeTypes(firstType, secondType); + return ExcludeTypes(location, firstType, secondType); }) .Case("Extract", [&] (auto typeArguments, auto genContext) { auto firstType = getFirstTypeFromTypeArguments(typeArguments, genContext); auto secondType = getSecondTypeFromTypeArguments(typeArguments, genContext); - return ExtractTypes(firstType, secondType); + return ExtractTypes(location, firstType, secondType); }) .Case("Pick", [&] (auto typeArguments, auto genContext) { auto sourceType = getFirstTypeFromTypeArguments(typeArguments, genContext); @@ -19998,7 +20021,7 @@ genContext); } // TODO: remove using those types as there issue with generic types - mlir::Type ExcludeTypes(mlir::Type type, mlir::Type exclude) + mlir::Type ExcludeTypes(mlir::Location location, mlir::Type type, mlir::Type exclude) { if (mth.isGenericType(type) || mth.isGenericType(exclude)) { @@ -20016,7 +20039,9 @@ genContext); { // TODO: should I use TypeParamsWithArgs from genContext? llvm::StringMap> emptyTypeParamsWithArgs; - if (llvm::any_of(excludeTypes, [&](mlir::Type type) { return isTrue(mth.extendsType(item, type, emptyTypeParamsWithArgs)); })) + if (llvm::any_of(excludeTypes, [&](mlir::Type type) { + return isTrue(mth.extendsType(location, item, type, emptyTypeParamsWithArgs)); + })) { continue; } @@ -20027,7 +20052,7 @@ genContext); return getUnionType(resTypes); } - mlir::Type ExtractTypes(mlir::Type type, mlir::Type extract) + mlir::Type ExtractTypes(mlir::Location location, mlir::Type type, mlir::Type extract) { if (mth.isGenericType(type) || mth.isGenericType(extract)) { @@ -20045,7 +20070,9 @@ genContext); { // TODO: should I use TypeParamsWithArgs from genContext? llvm::StringMap> emptyTypeParamsWithArgs; - if (llvm::any_of(extractTypes, [&](mlir::Type type) { return isTrue(mth.extendsType(item, type, emptyTypeParamsWithArgs)); })) + if (llvm::any_of(extractTypes, [&](mlir::Type type) { + return isTrue(mth.extendsType(location, item, type, emptyTypeParamsWithArgs)); + })) { resTypes.push_back(item); } @@ -20255,7 +20282,7 @@ genContext); auto location = loc(conditionalTypeNode); mlir::Type resType; - auto extendsResult = mth.extendsType(checkType, extendsType, typeParamsWithArgs); + auto extendsResult = mth.extendsType(location, checkType, extendsType, typeParamsWithArgs); if (extendsResult == ExtendsResult::Never) { return getNeverType(); @@ -20298,7 +20325,7 @@ genContext); } else { - resType = getUnionType(resType, falseType); + resType = getUnionType(location, resType, falseType); LLVM_DEBUG(llvm::dbgs() << "\n!! condition type [TRUE | FALSE] = " << resType << "\n";); } @@ -20386,7 +20413,7 @@ genContext); if (type.isa()) { // TODO: and all methods etc - return getUnionType(getStringType(), getNumberType()); + return getUnionType(location, getStringType(), getNumberType()); } if (type.isa()) @@ -21508,10 +21535,10 @@ genContext); return getAnyType(); } - return mth.getUnionTypeMergeTypes(unionContext, false, false); + return mth.getUnionTypeMergeTypes(loc(unionTypeNode), unionContext, false, false); } - mlir::Type getUnionType(mlir::Type type1, mlir::Type type2) + mlir::Type getUnionType(mlir::Location location, mlir::Type type1, mlir::Type type2) { if (mth.isNoneType(type1) || mth.isNoneType(type2)) { @@ -21520,7 +21547,7 @@ genContext); LLVM_DEBUG(llvm::dbgs() << "\n!! join: " << type1 << " | " << type2;); - auto resType = mth.getUnionType(type1, type2, false); + auto resType = mth.getUnionType(location, type1, type2, false); LLVM_DEBUG(llvm::dbgs() << " = " << resType << "\n";); diff --git a/tsc/lib/TypeScript/TypeScriptOps.cpp b/tsc/lib/TypeScript/TypeScriptOps.cpp index 62594164d..0fa418476 100644 --- a/tsc/lib/TypeScript/TypeScriptOps.cpp +++ b/tsc/lib/TypeScript/TypeScriptOps.cpp @@ -556,7 +556,7 @@ LogicalResult mlir_ts::CastOp::verify() if (inUnionType || resUnionType) { ::typescript::MLIRTypeHelper mth(getContext()); - auto cmpTypes = [&](mlir::Type t1, mlir::Type t2) { return mth.canCastFromTo(t1, t2); }; + auto cmpTypes = [&](mlir::Type t1, mlir::Type t2) { return mth.canCastFromTo(getLoc(), t1, t2); }; if (inUnionType && !resUnionType) { @@ -568,7 +568,7 @@ LogicalResult mlir_ts::CastOp::verify() { ::typescript::MLIRTypeHelper mth(getContext()); mlir::Type baseType; - if (!mth.isUnionTypeNeedsTag(inUnionType, baseType)/* && mth.canCastFromTo(baseType, resType)*/) + if (!mth.isUnionTypeNeedsTag(getLoc(), inUnionType, baseType)/* && mth.canCastFromTo(baseType, resType)*/) { // we need to ignore this case, for example if union -> string, we need cast int to string return success(); @@ -629,6 +629,8 @@ struct NormalizeCast : public OpRewritePattern LogicalResult matchAndRewrite(mlir_ts::CastOp castOp, PatternRewriter &rewriter) const override { + auto location = castOp.getLoc(); + // TODO: finish it auto in = castOp.getIn(); auto res = castOp.getRes(); @@ -681,7 +683,7 @@ struct NormalizeCast : public OpRewritePattern if (resUnionType && !inUnionType) { ::typescript::MLIRTypeHelper mth(rewriter.getContext()); - if (mth.isUnionTypeNeedsTag(resUnionType)) + if (mth.isUnionTypeNeedsTag(location, resUnionType)) { // TODO: boxing, finish it, need to send TypeOf auto typeOfValue = rewriter.create(loc, mlir_ts::StringType::get(rewriter.getContext()), in); @@ -696,7 +698,7 @@ struct NormalizeCast : public OpRewritePattern if (inUnionType && !resUnionType) { ::typescript::MLIRTypeHelper mth(rewriter.getContext()); - if (mth.isUnionTypeNeedsTag(inUnionType)) + if (mth.isUnionTypeNeedsTag(location, inUnionType)) { auto value = rewriter.create(loc, res.getType(), in); rewriter.replaceOp(castOp, ValueRange{value});