• 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 "macros.h"
20 #include "util/enumbitops.h"
21 #include "util/language.h"
22 #include "util/ustring.h"
23 
24 #include <vector>
25 
26 namespace panda::es2panda::parser {
27 class Program;
28 
29 enum class ParserStatus : uint64_t {
30     NO_OPTS = 0U,
31     DIRECT_EVAL = 1U << 0U,
32 
33     FUNCTION = 1U << 1U,
34     ARROW_FUNCTION = 1U << 2U,
35     GENERATOR_FUNCTION = 1U << 3U,
36     ASYNC_FUNCTION = 1U << 4U,
37     CONSTRUCTOR_FUNCTION = 1U << 5U,
38     FUNCTION_PARAM = 1U << 6U,
39     IS_SPREAD = 1U << 7U,
40     ACCESSOR_FUNCTION = 1U << 8U,
41     FUNCTION_DECLARATION = 1U << 9U,
42 
43     ALLOW_SUPER = 1U << 10U,
44     ALLOW_SUPER_CALL = 1U << 11U,
45 
46     IN_ITERATION = 1U << 14U,
47     IN_LABELED = 1U << 15U,
48 
49     EXPORT_DEFAULT_REACHED = 1U << 16U,
50     HAS_COMPLEX_PARAM = 1U << 17U,
51     IN_SWITCH = 1U << 18U,
52 
53     MODULE = 1U << 19U,
54     ALLOW_NEW_TARGET = 1U << 20U,
55 
56     IN_EXTENDS = 1U << 21U,
57     ALLOW_THIS_TYPE = 1U << 22U,
58     IN_METHOD_DEFINITION = 1U << 23U,
59     IN_AMBIENT_CONTEXT = 1U << 24U,
60     IN_CLASS_BODY = 1U << 25U,
61     NEED_RETURN_TYPE = 1U << 26U,
62 
63     IN_EXTERNAL = 1U << 27U,
64     IN_IMPORT = 1U << 28U,
65     IN_DEFAULT_IMPORTS = 1U << 29U,
66     IN_EXTENSION_FUNCTION = 1U << 30U,
67     FUNCTION_HAS_RETURN_STATEMENT = 1U << 31U,
68     IN_NAMESPACE = 1ULL << 32ULL,
69 };
70 
DEFINE_BITOPS(ParserStatus)71 DEFINE_BITOPS(ParserStatus)
72 
73 class ParserContext {
74 public:
75     explicit ParserContext(const Program *program, ParserStatus status);
76 
77     explicit ParserContext(ParserContext *current, ParserStatus newStatus, util::StringView label = "")
78         : program_(current->program_), prev_(current), label_(label), lang_(current->lang_)
79     {
80         ParserStatus currentStatus = current->status_;
81         currentStatus &= (ParserStatus::MODULE | ParserStatus::ALLOW_NEW_TARGET | ParserStatus::IN_EXTENDS |
82                           ParserStatus::ALLOW_THIS_TYPE | ParserStatus::IN_CLASS_BODY | ParserStatus::FUNCTION |
83                           ParserStatus::IN_AMBIENT_CONTEXT);
84         status_ = currentStatus | newStatus;
85     }
86 
87     DEFAULT_COPY_SEMANTIC(ParserContext);
88     DEFAULT_MOVE_SEMANTIC(ParserContext);
89     ~ParserContext() = default;
90 
91     const Program *GetProgram() const
92     {
93         return program_;
94     }
95 
96     void SetProgram(Program *program)
97     {
98         program_ = program;
99     }
100 
101     Language GetLanguge() const
102     {
103         return lang_;
104     }
105 
106     Language SetLanguage(Language lang)
107     {
108         auto res = lang_;
109         lang_ = lang;
110         return res;
111     }
112 
113     ParserContext *Prev() const
114     {
115         return prev_;
116     }
117 
118     const ParserStatus &Status() const
119     {
120         return status_;
121     }
122 
123     ParserStatus &Status()
124     {
125         return status_;
126     }
127 
128     bool IsGenerator() const
129     {
130         return (status_ & ParserStatus::GENERATOR_FUNCTION) != 0;
131     }
132 
133     bool IsFunctionOrParam() const
134     {
135         return (status_ & (ParserStatus::FUNCTION | ParserStatus::FUNCTION_PARAM)) != 0;
136     }
137 
138     bool IsAsync() const
139     {
140         return (status_ & ParserStatus::ASYNC_FUNCTION) != 0;
141     }
142 
143     bool IsModule() const
144     {
145         return (status_ & ParserStatus::MODULE) != 0;
146     }
147 
148     bool IsDynamic() const
149     {
150         return lang_.IsDynamic();
151     }
152 
153     const ParserContext *FindLabel(const util::StringView &label) const;
154 
155 private:
156     const Program *program_;
157     ParserContext *prev_ {};
158     ParserStatus status_ {};
159     util::StringView label_ {};
160     Language lang_;
161 };
162 }  // namespace panda::es2panda::parser
163 
164 #endif
165