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_TYPESCIRPT_CORE_TYPE_ELABORATION_CONTEXT_H 17 #define ES2PANDA_TYPESCIRPT_CORE_TYPE_ELABORATION_CONTEXT_H 18 19 #include <typescript/checker.h> 20 #include <ir/expression.h> 21 22 #include <macros.h> 23 24 namespace panda::es2panda::ir { 25 class Expression; 26 class SpreadElement; 27 } // namespace panda::es2panda::ir 28 29 namespace panda::es2panda::checker { 30 class Type; 31 32 class ElaborationContext { 33 public: ElaborationContext(Checker * checker,Type * targetType,Type * sourceType,const ir::Expression * sourceNode,const lexer::SourcePosition & startPos)34 ElaborationContext(Checker *checker, Type *targetType, Type *sourceType, const 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, const ir::Expression *sourceNode); 49 50 protected: 51 Checker *checker_; 52 Type *targetType_; 53 Type *sourceType_; 54 const ir::Expression *sourceNode_; 55 const lexer::SourcePosition startPos_; 56 ArenaVector<Type *> potentialTypes_; 57 }; 58 59 class ArrayElaborationContext : public ElaborationContext { 60 public: ArrayElaborationContext(Checker * checker,Type * targetType,Type * sourceType,const ir::Expression * sourceNode,const lexer::SourcePosition & startPos)61 ArrayElaborationContext(Checker *checker, Type *targetType, Type *sourceType, const ir::Expression *sourceNode, 62 const lexer::SourcePosition &startPos) 63 : ElaborationContext(checker, targetType, sourceType, sourceNode, startPos) 64 { 65 } 66 67 void Start() override; 68 void RemoveUnnecessaryTypes() override; 69 70 private: 71 uint32_t index_ {0}; 72 }; 73 74 class ObjectElaborationContext : public ElaborationContext { 75 public: ObjectElaborationContext(Checker * checker,Type * targetType,Type * sourceType,const ir::Expression * sourceNode,const lexer::SourcePosition & startPos)76 ObjectElaborationContext(Checker *checker, Type *targetType, Type *sourceType, const ir::Expression *sourceNode, 77 const lexer::SourcePosition &startPos) 78 : ElaborationContext(checker, targetType, sourceType, sourceNode, startPos) 79 { 80 } 81 82 void Start() override; 83 void RemoveUnnecessaryTypes() override; 84 }; 85 } // namespace panda::es2panda::checker 86 87 #endif 88