13af6ab5fSopenharmony_ci/** 23af6ab5fSopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd. 33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License. 53af6ab5fSopenharmony_ci * You may obtain a copy of the License at 63af6ab5fSopenharmony_ci * 73af6ab5fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83af6ab5fSopenharmony_ci * 93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and 133af6ab5fSopenharmony_ci * limitations under the License. 143af6ab5fSopenharmony_ci */ 153af6ab5fSopenharmony_ci 163af6ab5fSopenharmony_ci#ifndef ES2PANDA_CHECKER_CHECKER_CONTEXT_H 173af6ab5fSopenharmony_ci#define ES2PANDA_CHECKER_CHECKER_CONTEXT_H 183af6ab5fSopenharmony_ci 193af6ab5fSopenharmony_ci#include "checker/types/type.h" 203af6ab5fSopenharmony_ci#include "ir/statements/loopStatement.h" 213af6ab5fSopenharmony_ci#include "varbinder/variable.h" 223af6ab5fSopenharmony_ci 233af6ab5fSopenharmony_cinamespace ark::es2panda::checker { 243af6ab5fSopenharmony_ci 253af6ab5fSopenharmony_ciclass ETSObjectType; 263af6ab5fSopenharmony_ciclass Signature; 273af6ab5fSopenharmony_ci 283af6ab5fSopenharmony_ciusing ENUMBITOPS_OPERATORS; 293af6ab5fSopenharmony_ci 303af6ab5fSopenharmony_cienum class CheckerStatus : uint32_t { 313af6ab5fSopenharmony_ci NO_OPTS = 0U, 323af6ab5fSopenharmony_ci FORCE_TUPLE = 1U << 0U, 333af6ab5fSopenharmony_ci IN_CONST_CONTEXT = 1U << 1U, 343af6ab5fSopenharmony_ci KEEP_LITERAL_TYPE = 1U << 2U, 353af6ab5fSopenharmony_ci IN_PARAMETER = 1U << 3U, 363af6ab5fSopenharmony_ci IN_CLASS = 1U << 4U, 373af6ab5fSopenharmony_ci IN_INTERFACE = 1U << 5U, 383af6ab5fSopenharmony_ci IN_ABSTRACT = 1U << 6U, 393af6ab5fSopenharmony_ci IN_STATIC_CONTEXT = 1U << 7U, 403af6ab5fSopenharmony_ci IN_CONSTRUCTOR = 1U << 8U, 413af6ab5fSopenharmony_ci IN_STATIC_BLOCK = 1U << 9U, 423af6ab5fSopenharmony_ci INNER_CLASS = 1U << 10U, 433af6ab5fSopenharmony_ci IN_ENUM = 1U << 11U, 443af6ab5fSopenharmony_ci BUILTINS_INITIALIZED = 1U << 12U, 453af6ab5fSopenharmony_ci IN_LAMBDA = 1U << 13U, 463af6ab5fSopenharmony_ci IGNORE_VISIBILITY = 1U << 14U, 473af6ab5fSopenharmony_ci IN_INSTANCE_EXTENSION_METHOD = 1U << 15U, 483af6ab5fSopenharmony_ci IN_LOCAL_CLASS = 1U << 16U, 493af6ab5fSopenharmony_ci IN_INSTANCEOF_CONTEXT = 1U << 17U, 503af6ab5fSopenharmony_ci IN_TEST_EXPRESSION = 1U << 18U, 513af6ab5fSopenharmony_ci IN_LOOP = 1U << 19U, 523af6ab5fSopenharmony_ci MEET_RETURN = 1U << 20U, 533af6ab5fSopenharmony_ci MEET_BREAK = 1U << 21U, 543af6ab5fSopenharmony_ci MEET_CONTINUE = 1U << 22U, 553af6ab5fSopenharmony_ci MEET_THROW = 1U << 23U, 563af6ab5fSopenharmony_ci IN_EXTERNAL = 1U << 24U, 573af6ab5fSopenharmony_ci IN_BRIDGE_TEST = 1U << 25U, 583af6ab5fSopenharmony_ci}; 593af6ab5fSopenharmony_ci 603af6ab5fSopenharmony_ci} // namespace ark::es2panda::checker 613af6ab5fSopenharmony_ci 623af6ab5fSopenharmony_citemplate <> 633af6ab5fSopenharmony_cistruct enumbitops::IsAllowedType<ark::es2panda::checker::CheckerStatus> : std::true_type { 643af6ab5fSopenharmony_ci}; 653af6ab5fSopenharmony_ci 663af6ab5fSopenharmony_cinamespace ark::es2panda::checker { 673af6ab5fSopenharmony_ci 683af6ab5fSopenharmony_ciusing CapturedVarsMap = ArenaUnorderedMap<varbinder::Variable *, lexer::SourcePosition>; 693af6ab5fSopenharmony_ciusing SmartCastMap = ArenaMap<varbinder::Variable const *, checker::Type *>; 703af6ab5fSopenharmony_ciusing SmartCastArray = std::vector<std::pair<varbinder::Variable const *, checker::Type *>>; 713af6ab5fSopenharmony_ciusing SmartCastTestMap = ArenaMap<varbinder::Variable const *, std::pair<checker::Type *, checker::Type *>>; 723af6ab5fSopenharmony_ciusing SmartCastTuple = std::tuple<varbinder::Variable const *, checker::Type *, checker::Type *>; 733af6ab5fSopenharmony_ciusing SmartCastTestArray = std::vector<SmartCastTuple>; 743af6ab5fSopenharmony_ciusing PreservedSmartCastsMap = ArenaMultiMap<ir::AstNode const *, SmartCastArray>; 753af6ab5fSopenharmony_ciusing SmartVariables = std::unordered_set<varbinder::Variable const *>; 763af6ab5fSopenharmony_ci 773af6ab5fSopenharmony_cistruct SmartCastCondition final { 783af6ab5fSopenharmony_ci SmartCastCondition() = default; 793af6ab5fSopenharmony_ci ~SmartCastCondition() = default; 803af6ab5fSopenharmony_ci 813af6ab5fSopenharmony_ci DEFAULT_COPY_SEMANTIC(SmartCastCondition); 823af6ab5fSopenharmony_ci DEFAULT_MOVE_SEMANTIC(SmartCastCondition); 833af6ab5fSopenharmony_ci 843af6ab5fSopenharmony_ci // NOLINTBEGIN(misc-non-private-member-variables-in-classes) 853af6ab5fSopenharmony_ci varbinder::Variable const *variable = nullptr; 863af6ab5fSopenharmony_ci checker::Type *testedType = nullptr; 873af6ab5fSopenharmony_ci bool negate = false; 883af6ab5fSopenharmony_ci bool strict = true; 893af6ab5fSopenharmony_ci // NOLINTEND(misc-non-private-member-variables-in-classes) 903af6ab5fSopenharmony_ci}; 913af6ab5fSopenharmony_ci 923af6ab5fSopenharmony_ciusing SmartCastTypes = std::optional<SmartCastTestArray>; 933af6ab5fSopenharmony_ci 943af6ab5fSopenharmony_ciclass CheckerContext final { 953af6ab5fSopenharmony_cipublic: 963af6ab5fSopenharmony_ci explicit CheckerContext(Checker *checker, CheckerStatus newStatus) : CheckerContext(checker, newStatus, nullptr) {} 973af6ab5fSopenharmony_ci 983af6ab5fSopenharmony_ci explicit CheckerContext(Checker *checker, CheckerStatus newStatus, const ETSObjectType *containingClass) 993af6ab5fSopenharmony_ci : CheckerContext(checker, newStatus, containingClass, nullptr) 1003af6ab5fSopenharmony_ci { 1013af6ab5fSopenharmony_ci } 1023af6ab5fSopenharmony_ci 1033af6ab5fSopenharmony_ci explicit CheckerContext(Checker *checker, CheckerStatus newStatus, const ETSObjectType *containingClass, 1043af6ab5fSopenharmony_ci Signature *containingSignature); 1053af6ab5fSopenharmony_ci 1063af6ab5fSopenharmony_ci CheckerContext() = delete; 1073af6ab5fSopenharmony_ci ~CheckerContext() = default; 1083af6ab5fSopenharmony_ci 1093af6ab5fSopenharmony_ci DEFAULT_COPY_SEMANTIC(CheckerContext); 1103af6ab5fSopenharmony_ci DEFAULT_MOVE_SEMANTIC(CheckerContext); 1113af6ab5fSopenharmony_ci 1123af6ab5fSopenharmony_ci [[nodiscard]] const CapturedVarsMap &CapturedVars() const noexcept 1133af6ab5fSopenharmony_ci { 1143af6ab5fSopenharmony_ci return capturedVars_; 1153af6ab5fSopenharmony_ci } 1163af6ab5fSopenharmony_ci 1173af6ab5fSopenharmony_ci [[nodiscard]] CapturedVarsMap &CapturedVars() noexcept 1183af6ab5fSopenharmony_ci { 1193af6ab5fSopenharmony_ci return capturedVars_; 1203af6ab5fSopenharmony_ci } 1213af6ab5fSopenharmony_ci 1223af6ab5fSopenharmony_ci [[nodiscard]] const CheckerStatus &Status() const noexcept 1233af6ab5fSopenharmony_ci { 1243af6ab5fSopenharmony_ci return status_; 1253af6ab5fSopenharmony_ci } 1263af6ab5fSopenharmony_ci 1273af6ab5fSopenharmony_ci [[nodiscard]] ETSObjectType *ContainingClass() const noexcept 1283af6ab5fSopenharmony_ci { 1293af6ab5fSopenharmony_ci return const_cast<ETSObjectType *>(containingClass_); 1303af6ab5fSopenharmony_ci } 1313af6ab5fSopenharmony_ci 1323af6ab5fSopenharmony_ci [[nodiscard]] Signature *ContainingSignature() const noexcept 1333af6ab5fSopenharmony_ci { 1343af6ab5fSopenharmony_ci return containingSignature_; 1353af6ab5fSopenharmony_ci } 1363af6ab5fSopenharmony_ci 1373af6ab5fSopenharmony_ci [[nodiscard]] CheckerStatus &Status() noexcept 1383af6ab5fSopenharmony_ci { 1393af6ab5fSopenharmony_ci return status_; 1403af6ab5fSopenharmony_ci } 1413af6ab5fSopenharmony_ci 1423af6ab5fSopenharmony_ci void SetContainingSignature(Signature *containingSignature) noexcept 1433af6ab5fSopenharmony_ci { 1443af6ab5fSopenharmony_ci containingSignature_ = containingSignature; 1453af6ab5fSopenharmony_ci } 1463af6ab5fSopenharmony_ci 1473af6ab5fSopenharmony_ci void SetContainingClass(ETSObjectType *containingClass) noexcept 1483af6ab5fSopenharmony_ci { 1493af6ab5fSopenharmony_ci containingClass_ = containingClass; 1503af6ab5fSopenharmony_ci } 1513af6ab5fSopenharmony_ci 1523af6ab5fSopenharmony_ci void AddCapturedVar(varbinder::Variable *var, const lexer::SourcePosition &pos) 1533af6ab5fSopenharmony_ci { 1543af6ab5fSopenharmony_ci capturedVars_.emplace(var, pos); 1553af6ab5fSopenharmony_ci } 1563af6ab5fSopenharmony_ci [[nodiscard]] ir::ArrowFunctionExpression *ContainingLambda() const noexcept 1573af6ab5fSopenharmony_ci { 1583af6ab5fSopenharmony_ci return containingLambda_; 1593af6ab5fSopenharmony_ci } 1603af6ab5fSopenharmony_ci 1613af6ab5fSopenharmony_ci void SetContainingLambda(ir::ArrowFunctionExpression *containingLambda) noexcept 1623af6ab5fSopenharmony_ci { 1633af6ab5fSopenharmony_ci containingLambda_ = containingLambda; 1643af6ab5fSopenharmony_ci } 1653af6ab5fSopenharmony_ci 1663af6ab5fSopenharmony_ci void ClearSmartCasts() noexcept 1673af6ab5fSopenharmony_ci { 1683af6ab5fSopenharmony_ci smartCasts_.clear(); 1693af6ab5fSopenharmony_ci } 1703af6ab5fSopenharmony_ci 1713af6ab5fSopenharmony_ci void RemoveSmartCast(varbinder::Variable const *const variable) noexcept 1723af6ab5fSopenharmony_ci { 1733af6ab5fSopenharmony_ci smartCasts_.erase(variable); 1743af6ab5fSopenharmony_ci } 1753af6ab5fSopenharmony_ci 1763af6ab5fSopenharmony_ci void SetSmartCast(varbinder::Variable const *const variable, checker::Type *const smartType) noexcept; 1773af6ab5fSopenharmony_ci 1783af6ab5fSopenharmony_ci [[nodiscard]] checker::Type *GetSmartCast(varbinder::Variable const *const variable) const noexcept; 1793af6ab5fSopenharmony_ci [[nodiscard]] SmartCastArray CloneSmartCasts(bool clearData = false) noexcept; 1803af6ab5fSopenharmony_ci void RestoreSmartCasts(SmartCastArray const &otherSmartCasts); 1813af6ab5fSopenharmony_ci void CombineSmartCasts(SmartCastArray const &otherSmartCasts); 1823af6ab5fSopenharmony_ci 1833af6ab5fSopenharmony_ci [[nodiscard]] SmartCastArray EnterTestExpression() noexcept 1843af6ab5fSopenharmony_ci { 1853af6ab5fSopenharmony_ci status_ |= CheckerStatus::IN_TEST_EXPRESSION; 1863af6ab5fSopenharmony_ci ClearTestSmartCasts(); 1873af6ab5fSopenharmony_ci return CloneSmartCasts(false); 1883af6ab5fSopenharmony_ci } 1893af6ab5fSopenharmony_ci 1903af6ab5fSopenharmony_ci [[nodiscard]] bool IsInTestExpression() const noexcept 1913af6ab5fSopenharmony_ci { 1923af6ab5fSopenharmony_ci return (status_ & CheckerStatus::IN_TEST_EXPRESSION) != 0; 1933af6ab5fSopenharmony_ci } 1943af6ab5fSopenharmony_ci 1953af6ab5fSopenharmony_ci SmartCastTypes ExitTestExpression() 1963af6ab5fSopenharmony_ci { 1973af6ab5fSopenharmony_ci status_ &= ~CheckerStatus::IN_TEST_EXPRESSION; 1983af6ab5fSopenharmony_ci CheckTestSmartCastCondition(lexer::TokenType::EOS); 1993af6ab5fSopenharmony_ci return CloneTestSmartCasts(true); 2003af6ab5fSopenharmony_ci } 2013af6ab5fSopenharmony_ci 2023af6ab5fSopenharmony_ci [[nodiscard]] std::pair<SmartCastArray, bool> EnterLoop(ir::LoopStatement const &loop) noexcept; 2033af6ab5fSopenharmony_ci 2043af6ab5fSopenharmony_ci [[nodiscard]] bool IsInLoop() const noexcept 2053af6ab5fSopenharmony_ci { 2063af6ab5fSopenharmony_ci return (status_ & CheckerStatus::IN_LOOP) != 0; 2073af6ab5fSopenharmony_ci } 2083af6ab5fSopenharmony_ci 2093af6ab5fSopenharmony_ci void ExitLoop(SmartCastArray &prevSmartCasts, bool clearFlag, ir::LoopStatement *loopStatement) noexcept; 2103af6ab5fSopenharmony_ci 2113af6ab5fSopenharmony_ci void EnterPath() noexcept 2123af6ab5fSopenharmony_ci { 2133af6ab5fSopenharmony_ci status_ &= ~(CheckerStatus::MEET_RETURN | CheckerStatus::MEET_BREAK | CheckerStatus::MEET_CONTINUE | 2143af6ab5fSopenharmony_ci CheckerStatus::MEET_THROW); 2153af6ab5fSopenharmony_ci } 2163af6ab5fSopenharmony_ci 2173af6ab5fSopenharmony_ci [[nodiscard]] bool ExitPath() noexcept 2183af6ab5fSopenharmony_ci { 2193af6ab5fSopenharmony_ci auto const rc = (status_ & (CheckerStatus::MEET_RETURN | CheckerStatus::MEET_BREAK | 2203af6ab5fSopenharmony_ci CheckerStatus::MEET_CONTINUE | CheckerStatus::MEET_THROW)) != 0; 2213af6ab5fSopenharmony_ci status_ &= ~(CheckerStatus::MEET_RETURN | CheckerStatus::MEET_BREAK | CheckerStatus::MEET_CONTINUE | 2223af6ab5fSopenharmony_ci CheckerStatus::MEET_THROW); 2233af6ab5fSopenharmony_ci return rc; 2243af6ab5fSopenharmony_ci } 2253af6ab5fSopenharmony_ci 2263af6ab5fSopenharmony_ci [[nodiscard]] SmartCastArray CheckTryBlock(ir::BlockStatement const &tryBlock) noexcept; 2273af6ab5fSopenharmony_ci 2283af6ab5fSopenharmony_ci void CheckTestSmartCastCondition(lexer::TokenType operatorType); 2293af6ab5fSopenharmony_ci void CheckIdentifierSmartCastCondition(ir::Identifier const *identifier) noexcept; 2303af6ab5fSopenharmony_ci void CheckUnarySmartCastCondition(ir::UnaryExpression const *unaryExpression) noexcept; 2313af6ab5fSopenharmony_ci void CheckBinarySmartCastCondition(ir::BinaryExpression *binaryExpression) noexcept; 2323af6ab5fSopenharmony_ci 2333af6ab5fSopenharmony_ci void OnBreakStatement(ir::BreakStatement const *breakStatement); 2343af6ab5fSopenharmony_ci void AddBreakSmartCasts(ir::Statement const *targetStatement, SmartCastArray &&smartCasts); 2353af6ab5fSopenharmony_ci void CombineBreakSmartCasts(ir::Statement const *targetStatement); 2363af6ab5fSopenharmony_ci 2373af6ab5fSopenharmony_ciprivate: 2383af6ab5fSopenharmony_ci Checker *parent_; 2393af6ab5fSopenharmony_ci CheckerStatus status_; 2403af6ab5fSopenharmony_ci CapturedVarsMap capturedVars_; 2413af6ab5fSopenharmony_ci SmartCastMap smartCasts_; 2423af6ab5fSopenharmony_ci const ETSObjectType *containingClass_ {nullptr}; 2433af6ab5fSopenharmony_ci ir::ArrowFunctionExpression *containingLambda_ {nullptr}; 2443af6ab5fSopenharmony_ci Signature *containingSignature_ {nullptr}; 2453af6ab5fSopenharmony_ci 2463af6ab5fSopenharmony_ci lexer::TokenType operatorType_ = lexer::TokenType::EOS; 2473af6ab5fSopenharmony_ci SmartCastCondition testCondition_ {}; 2483af6ab5fSopenharmony_ci SmartCastTestMap testSmartCasts_; 2493af6ab5fSopenharmony_ci 2503af6ab5fSopenharmony_ci PreservedSmartCastsMap breakSmartCasts_; 2513af6ab5fSopenharmony_ci 2523af6ab5fSopenharmony_ci void RemoveSmartCasts(SmartCastArray const &otherSmartCasts) noexcept; 2533af6ab5fSopenharmony_ci [[nodiscard]] checker::Type *CombineTypes(checker::Type *typeOne, checker::Type *typeTwo) const noexcept; 2543af6ab5fSopenharmony_ci [[nodiscard]] static bool IsInValidChain(ir::AstNode const *parent) noexcept; 2553af6ab5fSopenharmony_ci void CheckSmartCastEqualityCondition(ir::BinaryExpression *binaryExpression) noexcept; 2563af6ab5fSopenharmony_ci [[nodiscard]] SmartCastTypes CloneTestSmartCasts(bool clearData = true) noexcept; 2573af6ab5fSopenharmony_ci void ClearTestSmartCasts() noexcept; 2583af6ab5fSopenharmony_ci [[nodiscard]] std::optional<SmartCastTuple> ResolveSmartCastTypes(); 2593af6ab5fSopenharmony_ci [[nodiscard]] bool CheckTestOrSmartCastCondition(SmartCastTuple const &types); 2603af6ab5fSopenharmony_ci void CheckAssignments(ir::AstNode const *node, SmartVariables &changedVariables) noexcept; 2613af6ab5fSopenharmony_ci}; 2623af6ab5fSopenharmony_ci} // namespace ark::es2panda::checker 2633af6ab5fSopenharmony_ci 2643af6ab5fSopenharmony_ci#endif 265