• 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_CHECKER_TS_TYPE_ELABORATION_CONTEXT_H
17 #define ES2PANDA_CHECKER_TS_TYPE_ELABORATION_CONTEXT_H
18 
19 #include "checker/TSchecker.h"
20 #include "ir/expression.h"
21 
22 #include <macros.h>
23 
24 namespace ark::es2panda::ir {
25 class Expression;
26 class SpreadElement;
27 }  // namespace ark::es2panda::ir
28 
29 namespace ark::es2panda::checker {
30 class Type;
31 
32 class ElaborationContext {
33 public:
ElaborationContext(TSChecker * checker,Type * targetType,Type * sourceType,ir::Expression * sourceNode,const lexer::SourcePosition & startPos)34     ElaborationContext(TSChecker *checker, Type *targetType, Type *sourceType, ir::Expression *sourceNode,
35                        const lexer::SourcePosition &startPos)
36         : checker_(checker),
37           targetType_(targetType),
38           sourceType_(sourceType),
39           sourceNode_(sourceNode),
40           startPos_(startPos),
41           potentialTypes_(checker->Allocator()->Adapter())
42     {
43     }
44 
45     virtual void Start() = 0;
46     virtual void RemoveUnnecessaryTypes() = 0;
47 
48     Type *GetBestMatchingType(Type *indexType, ir::Expression *sourceNode);
49 
50 protected:
51     // NOLINTBEGIN(misc-non-private-member-variables-in-classes)
52     TSChecker *checker_;
53     Type *targetType_;
54     Type *sourceType_;
55     ir::Expression *sourceNode_;
56     const lexer::SourcePosition startPos_;
57     ArenaVector<Type *> potentialTypes_;
58     // NOLINTEND(misc-non-private-member-variables-in-classes)
59 };
60 
61 class ArrayElaborationContext : public ElaborationContext {
62 public:
ArrayElaborationContext(TSChecker * checker,Type * targetType,Type * sourceType,ir::Expression * sourceNode,const lexer::SourcePosition & startPos)63     ArrayElaborationContext(TSChecker *checker, Type *targetType, Type *sourceType, ir::Expression *sourceNode,
64                             const lexer::SourcePosition &startPos)
65         : ElaborationContext(checker, targetType, sourceType, sourceNode, startPos)
66     {
67     }
68 
69     void Start() override;
70     void RemoveUnnecessaryTypes() override;
71 
72 private:
73     uint32_t index_ {0};
74 };
75 
76 class ObjectElaborationContext : public ElaborationContext {
77 public:
ObjectElaborationContext(TSChecker * checker,Type * targetType,Type * sourceType,ir::Expression * sourceNode,const lexer::SourcePosition & startPos)78     ObjectElaborationContext(TSChecker *checker, Type *targetType, Type *sourceType, ir::Expression *sourceNode,
79                              const lexer::SourcePosition &startPos)
80         : ElaborationContext(checker, targetType, sourceType, sourceNode, startPos)
81     {
82     }
83 
84     Type *NonComputedPropKeyType(ir::Property *prop);
85     void Start() override;
86     void RemoveUnnecessaryTypes() override;
87 };
88 }  // namespace ark::es2panda::checker
89 
90 #endif
91