1 /** 2 * Copyright (c) 2021-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_PARSER_CORE_PARSER_PRIVATE_CONTEXT_H 17 #define ES2PANDA_PARSER_CORE_PARSER_PRIVATE_CONTEXT_H 18 19 #include "util/enumbitops.h" 20 #include "util/language.h" 21 #include "util/ustring.h" 22 23 namespace ark::es2panda::parser { 24 class Program; 25 26 using ENUMBITOPS_OPERATORS; 27 28 enum class ParserStatus : uint64_t { 29 NO_OPTS = 0U, 30 DIRECT_EVAL = 1U << 0U, 31 32 FUNCTION = 1U << 1U, 33 ARROW_FUNCTION = 1U << 2U, 34 GENERATOR_FUNCTION = 1U << 3U, 35 ASYNC_FUNCTION = 1U << 4U, 36 CONSTRUCTOR_FUNCTION = 1U << 5U, 37 FUNCTION_PARAM = 1U << 6U, 38 IS_SPREAD = 1U << 7U, 39 ACCESSOR_FUNCTION = 1U << 8U, 40 FUNCTION_DECLARATION = 1U << 9U, 41 42 ALLOW_SUPER = 1U << 10U, 43 ALLOW_SUPER_CALL = 1U << 11U, 44 45 IN_ITERATION = 1U << 14U, 46 IN_LABELED = 1U << 15U, 47 48 EXPORT_DEFAULT_REACHED = 1U << 16U, 49 HAS_COMPLEX_PARAM = 1U << 17U, 50 IN_SWITCH = 1U << 18U, 51 52 MODULE = 1U << 19U, 53 ALLOW_NEW_TARGET = 1U << 20U, 54 55 IN_EXTENDS = 1U << 21U, 56 ALLOW_THIS_TYPE = 1U << 22U, 57 IN_METHOD_DEFINITION = 1U << 23U, 58 IN_AMBIENT_CONTEXT = 1U << 24U, 59 IN_CLASS_BODY = 1U << 25U, 60 NEED_RETURN_TYPE = 1U << 26U, 61 62 IN_DEFAULT_IMPORTS = 1U << 29U, 63 IN_EXTENSION_FUNCTION = 1U << 30U, 64 FUNCTION_HAS_RETURN_STATEMENT = 1U << 31U, 65 IN_NAMESPACE = 1ULL << 32ULL, 66 67 ALLOW_DEFAULT_VALUE = 1ULL << 33ULL, 68 }; 69 70 } // namespace ark::es2panda::parser 71 72 template <> 73 struct enumbitops::IsAllowedType<ark::es2panda::parser::ParserStatus> : std::true_type { 74 }; 75 76 namespace ark::es2panda::parser { 77 78 class ParserContext { 79 public: 80 // NOLINTNEXTLINE(modernize-avoid-c-arrays) 81 inline static constexpr char const DEFAULT_SOURCE_FILE[] = "<auxiliary_tmp>.sts"; 82 83 explicit ParserContext(const Program *program, ParserStatus status); 84 ParserContext(ParserContext *current, ParserStatus newStatus, util::StringView label = �)85 explicit ParserContext(ParserContext *current, ParserStatus newStatus, util::StringView label = "") 86 : program_(current->program_), prev_(current), label_(label), lang_(current->lang_) 87 { 88 ParserStatus currentStatus = current->status_; 89 currentStatus &= (ParserStatus::MODULE | ParserStatus::ALLOW_NEW_TARGET | ParserStatus::IN_EXTENDS | 90 ParserStatus::ALLOW_THIS_TYPE | ParserStatus::IN_CLASS_BODY | ParserStatus::FUNCTION | 91 ParserStatus::IN_AMBIENT_CONTEXT); 92 status_ = currentStatus | newStatus; 93 } 94 95 DEFAULT_COPY_SEMANTIC(ParserContext); 96 DEFAULT_MOVE_SEMANTIC(ParserContext); 97 ~ParserContext() = default; 98 ParserContext() = delete; 99 100 [[nodiscard]] const Program *GetProgram() const noexcept 101 { 102 return program_; 103 } 104 105 void SetProgram(Program *program) noexcept 106 { 107 program_ = program; 108 } 109 110 [[nodiscard]] Language GetLanguage() const noexcept 111 { 112 return lang_; 113 } 114 115 Language SetLanguage(Language lang) noexcept 116 { 117 auto res = lang_; 118 lang_ = lang; 119 return res; 120 } 121 122 [[nodiscard]] ParserContext *Prev() const noexcept 123 { 124 return prev_; 125 } 126 127 [[nodiscard]] const ParserStatus &Status() const noexcept 128 { 129 return status_; 130 } 131 132 [[nodiscard]] ParserStatus &Status() noexcept 133 { 134 return status_; 135 } 136 137 [[nodiscard]] bool IsGenerator() const noexcept 138 { 139 return (status_ & ParserStatus::GENERATOR_FUNCTION) != 0; 140 } 141 142 [[nodiscard]] bool IsFunctionOrParam() const noexcept 143 { 144 return (status_ & (ParserStatus::FUNCTION | ParserStatus::FUNCTION_PARAM)) != 0; 145 } 146 147 [[nodiscard]] bool IsAsync() const noexcept 148 { 149 return (status_ & ParserStatus::ASYNC_FUNCTION) != 0; 150 } 151 152 [[nodiscard]] bool IsModule() const noexcept 153 { 154 return (status_ & ParserStatus::MODULE) != 0; 155 } 156 157 [[nodiscard]] bool IsDynamic() const noexcept 158 { 159 return lang_.IsDynamic(); 160 } 161 162 const ParserContext *FindLabel(const util::StringView &label) const; 163 164 [[nodiscard]] std::string_view FormattingFileName() const noexcept 165 { 166 return formattingFileName_; 167 } 168 169 template <typename T> SetFormattingFileName(T &&fileName)170 void SetFormattingFileName(T &&fileName) 171 { 172 formattingFileName_ = std::string_view {std::forward<T>(fileName)}; 173 } 174 175 private: 176 const Program *program_; 177 ParserContext *prev_ {}; 178 ParserStatus status_ {}; 179 util::StringView label_ {}; 180 std::string_view formattingFileName_ {DEFAULT_SOURCE_FILE}; 181 Language lang_; 182 }; 183 } // namespace ark::es2panda::parser 184 185 #endif 186