diff --git a/ets2panda/BUILD.gn b/ets2panda/BUILD.gn index 596f03163ccfa9da9069f87a7683f7359b2a5526..821ef9f1ad48e0261bf791681b1fec4eaa8ed20d 100644 --- a/ets2panda/BUILD.gn +++ b/ets2panda/BUILD.gn @@ -206,7 +206,6 @@ libes2panda_sources = [ "compiler/lowering/ets/capturedVariables.cpp", "compiler/lowering/ets/cfgBuilderPhase.cpp", "compiler/lowering/ets/constantExpressionLowering.cpp", - "compiler/lowering/ets/convertPrimitiveCastMethodCall.cpp", "compiler/lowering/ets/declareOverloadLowering.cpp", "compiler/lowering/ets/defaultParametersInConstructorLowering.cpp", "compiler/lowering/ets/defaultParametersLowering.cpp", diff --git a/ets2panda/CMakeLists.txt b/ets2panda/CMakeLists.txt index d40f60c4f49d5841cc4005e2f7a4ad4be8e0bbe6..f79bf16f5dedda13e1a82562ac8506a9c075f477 100644 --- a/ets2panda/CMakeLists.txt +++ b/ets2panda/CMakeLists.txt @@ -281,7 +281,6 @@ set(ES2PANDA_LIB_SRC compiler/lowering/ets/capturedVariables.cpp compiler/lowering/ets/cfgBuilderPhase.cpp compiler/lowering/ets/constantExpressionLowering.cpp - compiler/lowering/ets/convertPrimitiveCastMethodCall.cpp compiler/lowering/ets/declareOverloadLowering.cpp compiler/lowering/ets/defaultParametersInConstructorLowering.cpp compiler/lowering/ets/defaultParametersLowering.cpp diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index 3aa4109c2eb8eb2d86c89482000a32026599fdd0..71c2a3db8b9ce32bdb03964a7a31de700c58eaef 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -1768,29 +1768,6 @@ checker::Type *ETSAnalyzer::ResolveMemberExpressionByBaseType(ETSChecker *checke return expr->AdjustType(checker, expr->CheckUnionMember(checker, baseType)); } - // NOTE(mshimenkov): temporary workaround to deliver 'primitives refactoring' patch - // To be removed after complete refactoring - if (baseType->IsETSPrimitiveType()) { - static std::array castMethods {{ - "toChar", - "toByte", - "toShort", - "toInt", - "toLong", - "toFloat", - "toDouble", - }}; - auto method = expr->Property()->AsIdentifier()->Name().Utf8(); - auto res = std::find(castMethods.begin(), castMethods.end(), method); - if (res != castMethods.end()) { - auto type = checker->MaybeBoxType(baseType); - expr->SetAstNodeFlags(ir::AstNodeFlags::TMP_CONVERT_PRIMITIVE_CAST_METHOD_CALL); - checker->ETSObjectTypeDeclNode(checker, type->AsETSObjectType()); - return expr->SetTsType(TransformTypeForMethodReference( - checker, expr, expr->SetAndAdjustType(checker, type->AsETSObjectType()))); - } - } - TypeErrorOnMissingProperty(expr, baseType, checker); return expr->TsType(); } diff --git a/ets2panda/compiler/lowering/ets/convertPrimitiveCastMethodCall.cpp b/ets2panda/compiler/lowering/ets/convertPrimitiveCastMethodCall.cpp deleted file mode 100644 index 0009d6ef8f8b3b5a78edc909e83d3ba7d9bd16ac..0000000000000000000000000000000000000000 --- a/ets2panda/compiler/lowering/ets/convertPrimitiveCastMethodCall.cpp +++ /dev/null @@ -1,65 +0,0 @@ -/** - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "convertPrimitiveCastMethodCall.h" - -#include "checker/ETSchecker.h" -#include "compiler/lowering/util.h" - -namespace ark::es2panda::compiler { - -std::string_view ConvertPrimitiveCastMethodCall::Name() const -{ - return "ConvertPrimitiveCastMethodCall"; -} - -// Convert all MemberExpressions like 'a.toInt()' where 'a' has any primitive type into AsExpression like 'a as int' -static ir::AstNode *ConvertMemberExpressionToAsExpression(ir::CallExpression *call, checker::ETSChecker *checker) -{ - auto allocator = checker->Allocator(); - auto me = call->Callee()->AsMemberExpression(); - auto toType = call->Signature()->ReturnType(); - - auto res = util::NodeAllocator::ForceSetParent( - allocator, me->Object()->Clone(allocator, nullptr)->AsExpression(), - checker->AllocNode(toType, allocator), false); - res->SetParent(call->Parent()); - - { - auto scope = varbinder::LexicalScope::Enter(checker->VarBinder(), NearestScope(me)); - CheckLoweredNode(checker->VarBinder()->AsETSBinder(), checker, res); - } - return res; -} - -bool ConvertPrimitiveCastMethodCall::PerformForModule(public_lib::Context *const ctx, parser::Program *const program) -{ - auto checker = ctx->GetChecker()->AsETSChecker(); - program->Ast()->TransformChildrenRecursively( - // CC-OFFNXT(G.FMT.14-CPP) project code style - [checker](checker::AstNodePtr ast) -> checker::AstNodePtr { - if (ast->IsCallExpression() && ast->AsCallExpression()->Callee()->IsMemberExpression() && - ast->AsCallExpression()->Callee()->HasAstNodeFlags( - ir::AstNodeFlags::TMP_CONVERT_PRIMITIVE_CAST_METHOD_CALL)) { - return ConvertMemberExpressionToAsExpression(ast->AsCallExpression(), checker); - } - return ast; - }, - Name()); - - return true; -} - -} // namespace ark::es2panda::compiler diff --git a/ets2panda/compiler/lowering/ets/convertPrimitiveCastMethodCall.h b/ets2panda/compiler/lowering/ets/convertPrimitiveCastMethodCall.h deleted file mode 100644 index 64ac95a07493bf1ed87d46d8080e168428f2bdc4..0000000000000000000000000000000000000000 --- a/ets2panda/compiler/lowering/ets/convertPrimitiveCastMethodCall.h +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef ES2PANDA_COMPILER_LOWERING_CONVERT_MEMBEREXPRESSION_H -#define ES2PANDA_COMPILER_LOWERING_CONVERT_MEMBEREXPRESSION_H - -#include "compiler/lowering/phase.h" - -namespace ark::es2panda::compiler { - -// NOTE(mshimenkov): To be removed after delivering 'primitive types refactoring' patch -// -// The purpose of this phase is to convert all primitive type cast method calls into 'as' expressions -// Example: -// let d: double = 3.14 -// let i: int = d.toInt() -// -// After phase: -// let d: double = 3.14 -// let i: int = d as int -// -class ConvertPrimitiveCastMethodCall : public PhaseForBodies { -public: - std::string_view Name() const override; - bool PerformForModule(public_lib::Context *ctx, parser::Program *program) override; -}; - -} // namespace ark::es2panda::compiler - -#endif // ES2PANDA_COMPILER_LOWERING_CONVERT_MEMBEREXPRESSION_H diff --git a/ets2panda/compiler/lowering/phase.cpp b/ets2panda/compiler/lowering/phase.cpp index c321ed36b8b664e093983492e3613f3e40d3ed51..d6a6a19083a85aa0e2c251b4d11b37495c432bf2 100644 --- a/ets2panda/compiler/lowering/phase.cpp +++ b/ets2panda/compiler/lowering/phase.cpp @@ -25,7 +25,6 @@ #include "compiler/lowering/ets/boxingForLocals.h" #include "compiler/lowering/ets/capturedVariables.h" #include "compiler/lowering/ets/constantExpressionLowering.h" -#include "compiler/lowering/ets/convertPrimitiveCastMethodCall.h" #include "compiler/lowering/ets/declareOverloadLowering.h" #include "compiler/lowering/ets/cfgBuilderPhase.h" #include "compiler/lowering/ets/defaultParametersInConstructorLowering.h" @@ -123,7 +122,6 @@ std::vector GetETSPhaseList() new CheckerPhase, // pluginsAfterCheck has to go right after checkerPhase, nothing should be between them new PluginPhase {g_pluginsAfterCheck, ES2PANDA_STATE_CHECKED, &util::Plugin::AfterCheck}, - // new ConvertPrimitiveCastMethodCall, new AnnotationCopyPostLowering, new AsyncMethodLowering, new DeclareOverloadLowering, diff --git a/ets2panda/docs/lowering-phases.md b/ets2panda/docs/lowering-phases.md index 392626e8408435fb42fa8f91749b55e71cc58fcd..2866f49f0145384b44feec40b99b808bb71a9c12 100644 --- a/ets2panda/docs/lowering-phases.md +++ b/ets2panda/docs/lowering-phases.md @@ -672,20 +672,3 @@ class C$partial { ``` - `plugins-after-lowering` call compiler plugins that work at the end of the lowering process, if any. - -- `ConvertPrimitiveCastMethodCall` replaces all primitive cast method calls with corresponding 'as' expressions. -NOTE: To be removed after delivering 'primitive types refactoring' patch - -Before phase: - -``` -let d: double = 3.14 -let i: int = d.toInt() -``` - -After phase: - -``` -let d: double = 3.14 -let i: int = d as int -``` diff --git a/ets2panda/test/runtime/ets/enum_const_variable.ets b/ets2panda/test/runtime/ets/enum_const_variable.ets index a830ab1dd53df0229d229ca2f42bdc3956eaca88..31ed4f9a26b23a8f61df16b79caa10e19f7131b8 100644 --- a/ets2panda/test/runtime/ets/enum_const_variable.ets +++ b/ets2panda/test/runtime/ets/enum_const_variable.ets @@ -15,12 +15,12 @@ const b = 1 + 1 * 2 const a = b + 10 -const c = 177.141513 +const c = 177 enum Color { Red = b, Blue = a, Yellow, - Purple = c as int + Purple = c } function main() {