• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     FUNCTION_HAS_THROW_STATEMENT = 1ULL << 34ULL,
69 };
70 
71 }  // namespace ark::es2panda::parser
72 
73 template <>
74 struct enumbitops::IsAllowedType<ark::es2panda::parser::ParserStatus> : std::true_type {
75 };
76 
77 namespace ark::es2panda::parser {
78 
79 class ParserContext {
80 public:
81     // NOLINTNEXTLINE(modernize-avoid-c-arrays)
82     inline static constexpr char const DEFAULT_SOURCE_FILE[] = "<auxiliary_tmp>.sts";
83 
84     explicit ParserContext(const Program *program, ParserStatus status);
85 
86     explicit ParserContext(ParserContext *current, ParserStatus newStatus, util::StringView label = "")
87         : program_(current->program_), prev_(current), label_(label), lang_(current->lang_)
88     {
89         ParserStatus currentStatus = current->status_;
90         currentStatus &= (ParserStatus::MODULE | ParserStatus::ALLOW_NEW_TARGET | ParserStatus::IN_EXTENDS |
91                           ParserStatus::ALLOW_THIS_TYPE | ParserStatus::IN_CLASS_BODY | ParserStatus::FUNCTION |
92                           ParserStatus::IN_AMBIENT_CONTEXT);
93         status_ = currentStatus | newStatus;
94     }
95 
96     DEFAULT_COPY_SEMANTIC(ParserContext);
97     DEFAULT_MOVE_SEMANTIC(ParserContext);
98     ~ParserContext() = default;
99     ParserContext() = delete;
100 
101     [[nodiscard]] const Program *GetProgram() const noexcept
102     {
103         return program_;
104     }
105 
106     void SetProgram(Program *program) noexcept
107     {
108         program_ = program;
109     }
110 
111     [[nodiscard]] Language GetLanguage() const noexcept
112     {
113         return lang_;
114     }
115 
116     Language SetLanguage(Language lang) noexcept
117     {
118         auto res = lang_;
119         lang_ = lang;
120         return res;
121     }
122 
123     [[nodiscard]] ParserContext *Prev() const noexcept
124     {
125         return prev_;
126     }
127 
128     [[nodiscard]] const ParserStatus &Status() const noexcept
129     {
130         return status_;
131     }
132 
133     [[nodiscard]] ParserStatus &Status() noexcept
134     {
135         return status_;
136     }
137 
138     [[nodiscard]] bool IsGenerator() const noexcept
139     {
140         return (status_ & ParserStatus::GENERATOR_FUNCTION) != 0;
141     }
142 
143     [[nodiscard]] bool IsFunctionOrParam() const noexcept
144     {
145         return (status_ & (ParserStatus::FUNCTION | ParserStatus::FUNCTION_PARAM)) != 0;
146     }
147 
148     [[nodiscard]] bool IsAsync() const noexcept
149     {
150         return (status_ & ParserStatus::ASYNC_FUNCTION) != 0;
151     }
152 
153     [[nodiscard]] bool IsModule() const noexcept
154     {
155         return (status_ & ParserStatus::MODULE) != 0;
156     }
157 
158     [[nodiscard]] bool IsDynamic() const noexcept
159     {
160         return lang_.IsDynamic();
161     }
162 
163     const ParserContext *FindLabel(const util::StringView &label) const;
164 
165     [[nodiscard]] std::string_view FormattingFileName() const noexcept
166     {
167         return formattingFileName_;
168     }
169 
170     template <typename T>
171     void SetFormattingFileName(T &&fileName)
172     {
173         formattingFileName_ = std::string_view {std::forward<T>(fileName)};
174     }
175 
176 private:
177     const Program *program_;
178     ParserContext *prev_ {};
179     ParserStatus status_ {};
180     util::StringView label_ {};
181     std::string_view formattingFileName_ {DEFAULT_SOURCE_FILE};
182     Language lang_;
183 };
184 }  // namespace ark::es2panda::parser
185 
186 #endif
187