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 PARSER_STATUS_CONTEXT_H 173af6ab5fSopenharmony_ci#define PARSER_STATUS_CONTEXT_H 183af6ab5fSopenharmony_ci 193af6ab5fSopenharmony_ci#include "parserImpl.h" 203af6ab5fSopenharmony_ci 213af6ab5fSopenharmony_cinamespace ark::es2panda::parser { 223af6ab5fSopenharmony_ci 233af6ab5fSopenharmony_citemplate <ParserStatus STATUS> 243af6ab5fSopenharmony_ciclass SavedStatusContext { 253af6ab5fSopenharmony_cipublic: 263af6ab5fSopenharmony_ci explicit SavedStatusContext(ParserContext *ctx) 273af6ab5fSopenharmony_ci // NOLINTNEXTLINE(readability-magic-numbers) 283af6ab5fSopenharmony_ci : ctx_(ctx), savedStatus_(static_cast<ParserStatus>(ctx->Status() & STATUS)) 293af6ab5fSopenharmony_ci { 303af6ab5fSopenharmony_ci // NOLINTNEXTLINE(readability-magic-numbers) 313af6ab5fSopenharmony_ci ctx->Status() |= STATUS; 323af6ab5fSopenharmony_ci } 333af6ab5fSopenharmony_ci 343af6ab5fSopenharmony_ci NO_COPY_SEMANTIC(SavedStatusContext); 353af6ab5fSopenharmony_ci NO_MOVE_SEMANTIC(SavedStatusContext); 363af6ab5fSopenharmony_ci 373af6ab5fSopenharmony_ci ~SavedStatusContext() 383af6ab5fSopenharmony_ci { 393af6ab5fSopenharmony_ci if (savedStatus_ == ParserStatus::NO_OPTS) { 403af6ab5fSopenharmony_ci ctx_->Status() &= ~savedStatus_; 413af6ab5fSopenharmony_ci } 423af6ab5fSopenharmony_ci } 433af6ab5fSopenharmony_ci 443af6ab5fSopenharmony_ciprivate: 453af6ab5fSopenharmony_ci ParserContext *ctx_; 463af6ab5fSopenharmony_ci ParserStatus savedStatus_; 473af6ab5fSopenharmony_ci}; 483af6ab5fSopenharmony_ci 493af6ab5fSopenharmony_ciclass SwitchContext : public SavedStatusContext<ParserStatus::IN_SWITCH> { 503af6ab5fSopenharmony_cipublic: 513af6ab5fSopenharmony_ci explicit SwitchContext(ParserContext *ctx) : SavedStatusContext(ctx) {} 523af6ab5fSopenharmony_ci NO_COPY_SEMANTIC(SwitchContext); 533af6ab5fSopenharmony_ci NO_MOVE_SEMANTIC(SwitchContext); 543af6ab5fSopenharmony_ci ~SwitchContext() = default; 553af6ab5fSopenharmony_ci}; 563af6ab5fSopenharmony_ci 573af6ab5fSopenharmony_ciclass IterationContext : public SavedStatusContext<ParserStatus::IN_ITERATION> { 583af6ab5fSopenharmony_cipublic: 593af6ab5fSopenharmony_ci explicit IterationContext(ParserContext *ctx) : SavedStatusContext(ctx) {} 603af6ab5fSopenharmony_ci 613af6ab5fSopenharmony_ci NO_COPY_SEMANTIC(IterationContext); 623af6ab5fSopenharmony_ci NO_MOVE_SEMANTIC(IterationContext); 633af6ab5fSopenharmony_ci ~IterationContext() = default; 643af6ab5fSopenharmony_ci}; 653af6ab5fSopenharmony_ci 663af6ab5fSopenharmony_ciclass FunctionParameterContext : public SavedStatusContext<ParserStatus::FUNCTION_PARAM> { 673af6ab5fSopenharmony_cipublic: 683af6ab5fSopenharmony_ci explicit FunctionParameterContext(ParserContext *ctx) : SavedStatusContext(ctx) {} 693af6ab5fSopenharmony_ci 703af6ab5fSopenharmony_ci NO_COPY_SEMANTIC(FunctionParameterContext); 713af6ab5fSopenharmony_ci NO_MOVE_SEMANTIC(FunctionParameterContext); 723af6ab5fSopenharmony_ci ~FunctionParameterContext() = default; 733af6ab5fSopenharmony_ci}; 743af6ab5fSopenharmony_ci 753af6ab5fSopenharmony_ciclass SavedParserContext { 763af6ab5fSopenharmony_cipublic: 773af6ab5fSopenharmony_ci template <typename... Args> 783af6ab5fSopenharmony_ci explicit SavedParserContext(ParserImpl *parser, Args &&...args) : parser_(parser), prev_(parser->context_) 793af6ab5fSopenharmony_ci { 803af6ab5fSopenharmony_ci parser_->context_ = ParserContext(&prev_, std::forward<Args>(args)...); 813af6ab5fSopenharmony_ci } 823af6ab5fSopenharmony_ci 833af6ab5fSopenharmony_ci NO_COPY_SEMANTIC(SavedParserContext); 843af6ab5fSopenharmony_ci DEFAULT_MOVE_SEMANTIC(SavedParserContext); 853af6ab5fSopenharmony_ci 863af6ab5fSopenharmony_ci ~SavedParserContext() 873af6ab5fSopenharmony_ci { 883af6ab5fSopenharmony_ci parser_->context_ = prev_; 893af6ab5fSopenharmony_ci } 903af6ab5fSopenharmony_ci 913af6ab5fSopenharmony_ciprivate: 923af6ab5fSopenharmony_ci ParserImpl *parser_; 933af6ab5fSopenharmony_ci ParserContext prev_; 943af6ab5fSopenharmony_ci}; 953af6ab5fSopenharmony_ci 963af6ab5fSopenharmony_ciclass SavedClassPrivateContext { 973af6ab5fSopenharmony_cipublic: 983af6ab5fSopenharmony_ci explicit SavedClassPrivateContext(ParserImpl *parser) : parser_(parser), prev_(parser->classPrivateContext_) 993af6ab5fSopenharmony_ci { 1003af6ab5fSopenharmony_ci parser_->classPrivateContext_ = ClassPrivateContext(&prev_); 1013af6ab5fSopenharmony_ci } 1023af6ab5fSopenharmony_ci 1033af6ab5fSopenharmony_ci NO_COPY_SEMANTIC(SavedClassPrivateContext); 1043af6ab5fSopenharmony_ci DEFAULT_MOVE_SEMANTIC(SavedClassPrivateContext); 1053af6ab5fSopenharmony_ci 1063af6ab5fSopenharmony_ci ~SavedClassPrivateContext() 1073af6ab5fSopenharmony_ci { 1083af6ab5fSopenharmony_ci parser_->classPrivateContext_ = prev_; 1093af6ab5fSopenharmony_ci } 1103af6ab5fSopenharmony_ci 1113af6ab5fSopenharmony_ciprivate: 1123af6ab5fSopenharmony_ci ParserImpl *parser_; 1133af6ab5fSopenharmony_ci ClassPrivateContext prev_; 1143af6ab5fSopenharmony_ci}; 1153af6ab5fSopenharmony_ci 1163af6ab5fSopenharmony_ciclass FunctionContext : public SavedParserContext { 1173af6ab5fSopenharmony_cipublic: 1183af6ab5fSopenharmony_ci explicit FunctionContext(ParserImpl *parser, ParserStatus newStatus) : SavedParserContext(parser, newStatus) 1193af6ab5fSopenharmony_ci { 1203af6ab5fSopenharmony_ci if ((newStatus & ParserStatus::GENERATOR_FUNCTION) != 0) { 1213af6ab5fSopenharmony_ci flags_ |= ir::ScriptFunctionFlags::GENERATOR; 1223af6ab5fSopenharmony_ci } 1233af6ab5fSopenharmony_ci 1243af6ab5fSopenharmony_ci if ((newStatus & ParserStatus::ASYNC_FUNCTION) != 0) { 1253af6ab5fSopenharmony_ci flags_ |= ir::ScriptFunctionFlags::ASYNC; 1263af6ab5fSopenharmony_ci } 1273af6ab5fSopenharmony_ci 1283af6ab5fSopenharmony_ci if ((newStatus & ParserStatus::CONSTRUCTOR_FUNCTION) != 0) { 1293af6ab5fSopenharmony_ci flags_ |= ir::ScriptFunctionFlags::CONSTRUCTOR; 1303af6ab5fSopenharmony_ci } 1313af6ab5fSopenharmony_ci } 1323af6ab5fSopenharmony_ci 1333af6ab5fSopenharmony_ci ir::ScriptFunctionFlags Flags() const 1343af6ab5fSopenharmony_ci { 1353af6ab5fSopenharmony_ci return flags_; 1363af6ab5fSopenharmony_ci } 1373af6ab5fSopenharmony_ci 1383af6ab5fSopenharmony_ci bool IsAsync() const 1393af6ab5fSopenharmony_ci { 1403af6ab5fSopenharmony_ci return (flags_ & ir::ScriptFunctionFlags::ASYNC) != 0; 1413af6ab5fSopenharmony_ci } 1423af6ab5fSopenharmony_ci 1433af6ab5fSopenharmony_ci void AddFlag(ir::ScriptFunctionFlags flags) 1443af6ab5fSopenharmony_ci { 1453af6ab5fSopenharmony_ci flags_ |= flags; 1463af6ab5fSopenharmony_ci } 1473af6ab5fSopenharmony_ci 1483af6ab5fSopenharmony_ci NO_COPY_SEMANTIC(FunctionContext); 1493af6ab5fSopenharmony_ci NO_MOVE_SEMANTIC(FunctionContext); 1503af6ab5fSopenharmony_ci ~FunctionContext() = default; 1513af6ab5fSopenharmony_ci 1523af6ab5fSopenharmony_ciprivate: 1533af6ab5fSopenharmony_ci ir::ScriptFunctionFlags flags_ {ir::ScriptFunctionFlags::NONE}; 1543af6ab5fSopenharmony_ci}; 1553af6ab5fSopenharmony_ci 1563af6ab5fSopenharmony_ciclass ArrowFunctionContext : public FunctionContext { 1573af6ab5fSopenharmony_cipublic: 1583af6ab5fSopenharmony_ci explicit ArrowFunctionContext(ParserImpl *parser, bool isAsync) 1593af6ab5fSopenharmony_ci : FunctionContext(parser, InitialFlags(parser->context_.Status())) 1603af6ab5fSopenharmony_ci { 1613af6ab5fSopenharmony_ci if (isAsync) { 1623af6ab5fSopenharmony_ci AddFlag(ir::ScriptFunctionFlags::ASYNC); 1633af6ab5fSopenharmony_ci } 1643af6ab5fSopenharmony_ci 1653af6ab5fSopenharmony_ci AddFlag(ir::ScriptFunctionFlags::ARROW); 1663af6ab5fSopenharmony_ci } 1673af6ab5fSopenharmony_ci 1683af6ab5fSopenharmony_ci NO_COPY_SEMANTIC(ArrowFunctionContext); 1693af6ab5fSopenharmony_ci NO_MOVE_SEMANTIC(ArrowFunctionContext); 1703af6ab5fSopenharmony_ci ~ArrowFunctionContext() = default; 1713af6ab5fSopenharmony_ci 1723af6ab5fSopenharmony_ciprivate: 1733af6ab5fSopenharmony_ci static ParserStatus InitialFlags(ParserStatus currentStatus) 1743af6ab5fSopenharmony_ci { 1753af6ab5fSopenharmony_ci return ParserStatus::FUNCTION | ParserStatus::ARROW_FUNCTION | 1763af6ab5fSopenharmony_ci static_cast<ParserStatus>(currentStatus & (ParserStatus::ALLOW_SUPER | ParserStatus::ALLOW_SUPER_CALL)); 1773af6ab5fSopenharmony_ci } 1783af6ab5fSopenharmony_ci}; 1793af6ab5fSopenharmony_ci} // namespace ark::es2panda::parser 1803af6ab5fSopenharmony_ci#endif 181