1/* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16#ifndef ES2PANDA_COMPILER_CORE_AST_VERIFIER_CHECKCONTEXT_H 17#define ES2PANDA_COMPILER_CORE_AST_VERIFIER_CHECKCONTEXT_H 18 19#include "ir/astNode.h" 20#include "utils/json_builder.h" 21 22namespace ark::es2panda::compiler::ast_verifier { 23 24enum class CheckDecision { CORRECT, INCORRECT }; 25enum class CheckAction { CONTINUE, SKIP_SUBTREE }; 26using CheckResult = std::tuple<CheckDecision, CheckAction>; 27 28enum class CheckSeverity { ERROR, WARNING, UNKNOWN }; 29inline std::string CheckSeverityString(CheckSeverity value) 30{ 31 switch (value) { 32 case CheckSeverity::ERROR: 33 return "error"; 34 case CheckSeverity::WARNING: 35 return "warning"; 36 default: 37 UNREACHABLE(); 38 } 39} 40 41class CheckMessage { 42public: 43 explicit CheckMessage(util::StringView name, util::StringView cause, util::StringView message, size_t line) 44 : invariantName_ {name}, cause_ {cause}, message_ {message}, line_ {line} 45 { 46 } 47 48 std::string Invariant() const 49 { 50 return invariantName_; 51 } 52 53 std::string Cause() const 54 { 55 return cause_; 56 } 57 58 std::function<void(JsonObjectBuilder &)> DumpJSON(CheckSeverity severity, const std::string &sourceName, 59 const std::string &phaseName) const 60 { 61 return [sourceName, phaseName, severity, this](JsonObjectBuilder &body) { 62 body.AddProperty("severity", CheckSeverityString(severity)); 63 body.AddProperty("invariant", invariantName_); 64 body.AddProperty("cause", cause_); 65 body.AddProperty("ast", message_); 66 body.AddProperty("line", line_ + 1); 67 body.AddProperty("source", sourceName); 68 body.AddProperty("phase", phaseName); 69 }; 70 } 71 72private: 73 std::string invariantName_; 74 std::string cause_; 75 std::string message_; 76 size_t line_; 77}; 78 79using Messages = std::vector<CheckMessage>; 80 81class CheckContext { 82public: 83 explicit CheckContext() : checkName_ {"Invalid"} {} 84 85 void AddCheckMessage(const std::string &cause, const ir::AstNode &node, const lexer::SourcePosition &from); 86 void SetCheckName(util::StringView checkName); 87 Messages GetMessages(); 88 89private: 90 Messages messages_; 91 util::StringView checkName_; 92}; 93 94} // namespace ark::es2panda::compiler::ast_verifier 95 96#endif // ES2PANDA_COMPILER_CORE_AST_VERIFIER_CHECKCONTEXT_H 97