13af6ab5fSopenharmony_ci/* 23af6ab5fSopenharmony_ci * Copyright (c) 2023-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_COMPILER_CORE_ASTVERIFIER_H 173af6ab5fSopenharmony_ci#define ES2PANDA_COMPILER_CORE_ASTVERIFIER_H 183af6ab5fSopenharmony_ci 193af6ab5fSopenharmony_ci#include <algorithm> 203af6ab5fSopenharmony_ci#include <iterator> 213af6ab5fSopenharmony_ci#include <regex> 223af6ab5fSopenharmony_ci#include <string> 233af6ab5fSopenharmony_ci#include <unordered_set> 243af6ab5fSopenharmony_ci 253af6ab5fSopenharmony_ci#include "ast_verifier/checkContext.h" 263af6ab5fSopenharmony_ci 273af6ab5fSopenharmony_ci#include "ir/astNode.h" 283af6ab5fSopenharmony_ci#include "ir/statements/blockStatement.h" 293af6ab5fSopenharmony_ci#include "lexer/token/sourceLocation.h" 303af6ab5fSopenharmony_ci#include "parser/program/program.h" 313af6ab5fSopenharmony_ci#include "util/ustring.h" 323af6ab5fSopenharmony_ci#include "utils/arena_containers.h" 333af6ab5fSopenharmony_ci#include "varbinder/variable.h" 343af6ab5fSopenharmony_ci 353af6ab5fSopenharmony_cinamespace ark::es2panda::compiler::ast_verifier { 363af6ab5fSopenharmony_ci 373af6ab5fSopenharmony_ciusing InvariantCheck = std::function<CheckResult(CheckContext &ctx, const ir::AstNode *)>; 383af6ab5fSopenharmony_ciusing Invariants = std::map<std::string, InvariantCheck>; 393af6ab5fSopenharmony_ci 403af6ab5fSopenharmony_ciusing InvariantNameSet = std::set<std::string>; 413af6ab5fSopenharmony_ci 423af6ab5fSopenharmony_ciclass VerificationContext final { 433af6ab5fSopenharmony_cipublic: 443af6ab5fSopenharmony_ci void IntroduceNewInvariants(util::StringView phaseName) 453af6ab5fSopenharmony_ci { 463af6ab5fSopenharmony_ci if (phaseName == "ScopesInitPhase") { 473af6ab5fSopenharmony_ci accumulatedChecks_.insert("NodeHasParentForAll"); 483af6ab5fSopenharmony_ci accumulatedChecks_.insert("NodeHasSourceRangeForAll"); 493af6ab5fSopenharmony_ci accumulatedChecks_.insert("EveryChildHasValidParentForAll"); 503af6ab5fSopenharmony_ci accumulatedChecks_.insert("EveryChildInParentRangeForAll"); 513af6ab5fSopenharmony_ci accumulatedChecks_.insert("VariableHasScopeForAll"); 523af6ab5fSopenharmony_ci } 533af6ab5fSopenharmony_ci if (phaseName == "CheckerPhase") { 543af6ab5fSopenharmony_ci accumulatedChecks_.insert("NodeHasTypeForAll"); 553af6ab5fSopenharmony_ci accumulatedChecks_.insert("IdentifierHasVariableForAll"); 563af6ab5fSopenharmony_ci accumulatedChecks_.insert("ReferenceTypeAnnotationIsNullForAll"); 573af6ab5fSopenharmony_ci accumulatedChecks_.insert("ArithmeticOperationValidForAll"); 583af6ab5fSopenharmony_ci accumulatedChecks_.insert("SequenceExpressionHasLastTypeForAll"); 593af6ab5fSopenharmony_ci accumulatedChecks_.insert("CheckInfiniteLoopForAll"); 603af6ab5fSopenharmony_ci accumulatedChecks_.insert("ForLoopCorrectlyInitializedForAll"); 613af6ab5fSopenharmony_ci accumulatedChecks_.insert("VariableHasEnclosingScopeForAll"); 623af6ab5fSopenharmony_ci accumulatedChecks_.insert("ModifierAccessValidForAll"); 633af6ab5fSopenharmony_ci accumulatedChecks_.insert("ImportExportAccessValid"); 643af6ab5fSopenharmony_ci accumulatedChecks_.insert("VariableNameIdentifierNameSameForAll"); 653af6ab5fSopenharmony_ci accumulatedChecks_.insert("CheckAbstractMethodForAll"); 663af6ab5fSopenharmony_ci accumulatedChecks_.insert("GetterSetterValidationForAll"); 673af6ab5fSopenharmony_ci accumulatedChecks_.insert("CheckScopeDeclarationForAll"); 683af6ab5fSopenharmony_ci } 693af6ab5fSopenharmony_ci } 703af6ab5fSopenharmony_ci 713af6ab5fSopenharmony_ci const InvariantNameSet &AccumulatedChecks() const 723af6ab5fSopenharmony_ci { 733af6ab5fSopenharmony_ci return accumulatedChecks_; 743af6ab5fSopenharmony_ci } 753af6ab5fSopenharmony_ci 763af6ab5fSopenharmony_ciprivate: 773af6ab5fSopenharmony_ci InvariantNameSet accumulatedChecks_ {}; 783af6ab5fSopenharmony_ci}; 793af6ab5fSopenharmony_ci 803af6ab5fSopenharmony_ci/* 813af6ab5fSopenharmony_ci * ASTVerifier used for checking various invariants that should hold during AST transformation in lowerings 823af6ab5fSopenharmony_ci * For all available checks lookup the constructor 833af6ab5fSopenharmony_ci */ 843af6ab5fSopenharmony_ciclass ASTVerifier final { 853af6ab5fSopenharmony_cipublic: 863af6ab5fSopenharmony_ci NO_COPY_SEMANTIC(ASTVerifier); 873af6ab5fSopenharmony_ci NO_MOVE_SEMANTIC(ASTVerifier); 883af6ab5fSopenharmony_ci 893af6ab5fSopenharmony_ci explicit ASTVerifier(ArenaAllocator *allocator); 903af6ab5fSopenharmony_ci ~ASTVerifier() = default; 913af6ab5fSopenharmony_ci 923af6ab5fSopenharmony_ci /** 933af6ab5fSopenharmony_ci * @brief Run all existing invariants on some ast node (and consequently it's children) 943af6ab5fSopenharmony_ci * @param ast AstNode which will be analyzed 953af6ab5fSopenharmony_ci * @return Messages report of analysis 963af6ab5fSopenharmony_ci */ 973af6ab5fSopenharmony_ci Messages VerifyFull(const ir::AstNode *ast); 983af6ab5fSopenharmony_ci 993af6ab5fSopenharmony_ci /** 1003af6ab5fSopenharmony_ci * @brief Run some particular invariants on some ast node 1013af6ab5fSopenharmony_ci * @note invariants must be supplied as strings to invariant_set, additionally invariant 1023af6ab5fSopenharmony_ci * name can be suffixed by `ForAll` string to include recursive analysis of provided node 1033af6ab5fSopenharmony_ci * I.e. 'HasParent' invariant can be named 'HasParentRecursive' to traverse all child nodes as well 1043af6ab5fSopenharmony_ci * @param ast AstNode which will be analyzed 1053af6ab5fSopenharmony_ci * @param invariantSet Set of invariants to check 1063af6ab5fSopenharmony_ci * @return Messages report of analysis 1073af6ab5fSopenharmony_ci */ 1083af6ab5fSopenharmony_ci Messages Verify(const ir::AstNode *ast, const InvariantNameSet &invariantSet); 1093af6ab5fSopenharmony_ci 1103af6ab5fSopenharmony_ciprivate: 1113af6ab5fSopenharmony_ci static constexpr const char *RECURSIVE_SUFFIX = "ForAll"; 1123af6ab5fSopenharmony_ci 1133af6ab5fSopenharmony_ci static InvariantCheck RecursiveInvariant(const InvariantCheck &func) 1143af6ab5fSopenharmony_ci { 1153af6ab5fSopenharmony_ci return [func](CheckContext &ctx, const ir::AstNode *ast) -> CheckResult { 1163af6ab5fSopenharmony_ci std::function<void(const ir::AstNode *)> aux; 1173af6ab5fSopenharmony_ci auto finalDecision = CheckDecision::CORRECT; 1183af6ab5fSopenharmony_ci aux = [&ctx, func, &aux, &finalDecision](const ir::AstNode *child) -> void { 1193af6ab5fSopenharmony_ci const auto [decision, action] = func(ctx, child); 1203af6ab5fSopenharmony_ci if (decision == CheckDecision::INCORRECT) { 1213af6ab5fSopenharmony_ci finalDecision = CheckDecision::INCORRECT; 1223af6ab5fSopenharmony_ci } 1233af6ab5fSopenharmony_ci if (action == CheckAction::SKIP_SUBTREE) { 1243af6ab5fSopenharmony_ci return; 1253af6ab5fSopenharmony_ci } 1263af6ab5fSopenharmony_ci child->Iterate(aux); 1273af6ab5fSopenharmony_ci }; 1283af6ab5fSopenharmony_ci aux(ast); 1293af6ab5fSopenharmony_ci return {finalDecision, CheckAction::CONTINUE}; 1303af6ab5fSopenharmony_ci }; 1313af6ab5fSopenharmony_ci } 1323af6ab5fSopenharmony_ci 1333af6ab5fSopenharmony_ci template <typename T> 1343af6ab5fSopenharmony_ci void AddInvariant(ArenaAllocator *allocator, const std::string &name) 1353af6ab5fSopenharmony_ci { 1363af6ab5fSopenharmony_ci auto check = *allocator->New<T>(*allocator); 1373af6ab5fSopenharmony_ci invariantsChecks_[name] = check; 1383af6ab5fSopenharmony_ci invariantsNames_.insert(name); 1393af6ab5fSopenharmony_ci invariantsChecks_[name + RECURSIVE_SUFFIX] = RecursiveInvariant(check); 1403af6ab5fSopenharmony_ci invariantsNames_.insert(name + RECURSIVE_SUFFIX); 1413af6ab5fSopenharmony_ci } 1423af6ab5fSopenharmony_ci 1433af6ab5fSopenharmony_ci Invariants invariantsChecks_; 1443af6ab5fSopenharmony_ci InvariantNameSet invariantsNames_; 1453af6ab5fSopenharmony_ci}; 1463af6ab5fSopenharmony_ci 1473af6ab5fSopenharmony_ci} // namespace ark::es2panda::compiler::ast_verifier 1483af6ab5fSopenharmony_ci 1493af6ab5fSopenharmony_ci#endif // ES2PANDA_COMPILER_CORE_ASTVERIFIER_H 150