• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021 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_CHECKER_CHECKER_CONTEXT_H
17 #define ES2PANDA_CHECKER_CHECKER_CONTEXT_H
18 
19 #include <macros.h>
20 #include "varbinder/variable.h"
21 #include "util/enumbitops.h"
22 
23 #include <vector>
24 
25 namespace panda::es2panda::checker {
26 
27 class ETSObjectType;
28 class Signature;
29 
30 enum class CheckerStatus : uint32_t {
31     NO_OPTS = 0U,
32     FORCE_TUPLE = 1U << 0U,
33     IN_CONST_CONTEXT = 1U << 1U,
34     KEEP_LITERAL_TYPE = 1U << 2U,
35     IN_PARAMETER = 1U << 3U,
36     IN_CLASS = 1U << 4U,
37     IN_INTERFACE = 1U << 5U,
38     IN_ABSTRACT = 1U << 6U,
39     IN_STATIC_CONTEXT = 1U << 7U,
40     IN_CONSTRUCTOR = 1U << 8U,
41     IN_STATIC_BLOCK = 1U << 9U,
42     INNER_CLASS = 1U << 10U,
43     IN_ENUM = 1U << 11U,
44     BUILTINS_INITIALIZED = 1U << 12U,
45     IN_LAMBDA = 1U << 13U,
46     IGNORE_VISIBILITY = 1U << 14U,
47     IN_INSTANCE_EXTENSION_METHOD = 1U << 15U,
48 };
49 
50 DEFINE_BITOPS(CheckerStatus)
51 
52 using CapturedVarsMap = ArenaUnorderedMap<varbinder::Variable *, lexer::SourcePosition>;
53 
54 class CheckerContext {
55 public:
CheckerContext(ArenaAllocator * allocator,CheckerStatus newStatus)56     explicit CheckerContext(ArenaAllocator *allocator, CheckerStatus newStatus)
57         : CheckerContext(allocator, newStatus, nullptr)
58     {
59     }
60 
CheckerContext(ArenaAllocator * allocator,CheckerStatus newStatus,ETSObjectType * containingClass)61     explicit CheckerContext(ArenaAllocator *allocator, CheckerStatus newStatus, ETSObjectType *containingClass)
62         : CheckerContext(allocator, newStatus, containingClass, nullptr)
63     {
64     }
65 
CheckerContext(ArenaAllocator * allocator,CheckerStatus newStatus,ETSObjectType * containingClass,Signature * containingSignature)66     explicit CheckerContext(ArenaAllocator *allocator, CheckerStatus newStatus, ETSObjectType *containingClass,
67                             Signature *containingSignature)
68         : status_(newStatus),
69           capturedVars_(allocator->Adapter()),
70           containingClass_(containingClass),
71           containingSignature_(containingSignature)
72     {
73     }
74 
CapturedVars()75     const CapturedVarsMap &CapturedVars() const
76     {
77         return capturedVars_;
78     }
79 
CapturedVars()80     CapturedVarsMap &CapturedVars()
81     {
82         return capturedVars_;
83     }
84 
Status()85     const CheckerStatus &Status() const
86     {
87         return status_;
88     }
89 
ContainingClass()90     ETSObjectType *ContainingClass() const
91     {
92         return containingClass_;
93     }
94 
ContainingSignature()95     Signature *ContainingSignature() const
96     {
97         return containingSignature_;
98     }
99 
Status()100     CheckerStatus &Status()
101     {
102         return status_;
103     }
104 
SetContainingSignature(Signature * containingSignature)105     void SetContainingSignature(Signature *containingSignature)
106     {
107         containingSignature_ = containingSignature;
108     }
109 
SetContainingClass(ETSObjectType * containingClass)110     void SetContainingClass(ETSObjectType *containingClass)
111     {
112         containingClass_ = containingClass;
113     }
114 
AddCapturedVar(varbinder::Variable * var,const lexer::SourcePosition & pos)115     void AddCapturedVar(varbinder::Variable *var, const lexer::SourcePosition &pos)
116     {
117         capturedVars_.emplace(var, pos);
118     }
119 
120     DEFAULT_COPY_SEMANTIC(CheckerContext);
121     DEFAULT_MOVE_SEMANTIC(CheckerContext);
122     ~CheckerContext() = default;
123 
124 private:
125     CheckerStatus status_;
126     CapturedVarsMap capturedVars_;
127     ETSObjectType *containingClass_ {nullptr};
128     Signature *containingSignature_ {nullptr};
129 };
130 }  // namespace panda::es2panda::checker
131 
132 #endif
133