1 /* 2 * Copyright (c) 2021-2025 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_COMPILER_LOWERING_INTERFACE_PROP_DECL_H 17 #define ES2PANDA_COMPILER_LOWERING_INTERFACE_PROP_DECL_H 18 19 #include "compiler/lowering/phase.h" 20 21 namespace ark::es2panda::compiler { 22 23 using InterfacePropertyType = std::unordered_set<std::string>; 24 using InterfacePropertyMapType = std::unordered_map<std::string, InterfacePropertyType>; 25 using VisitedInterfacesOfClass = std::unordered_set<std::string>; 26 27 class OptionalInterfacePropertyCollector { 28 public: 29 OptionalInterfacePropertyCollector() = default; 30 ~OptionalInterfacePropertyCollector() = default; 31 32 NO_MOVE_SEMANTIC(OptionalInterfacePropertyCollector); 33 NO_COPY_SEMANTIC(OptionalInterfacePropertyCollector); 34 GetInterfaceId()35 std::string &GetInterfaceId() 36 { 37 return interfaceId_; 38 } 39 SetInterfaceId(std::string id)40 void SetInterfaceId(std::string id) 41 { 42 interfaceId_ = std::move(id); 43 } 44 GetInterfaceProperty(const std::string & id)45 InterfacePropertyType &GetInterfaceProperty(const std::string &id) 46 { 47 ES2PANDA_ASSERT(interfaceProperties_.count(id) != 0); 48 return interfaceProperties_[id]; 49 } 50 InsertInterfaceProperty(const std::string & property)51 void InsertInterfaceProperty(const std::string &property) 52 { 53 ES2PANDA_ASSERT(!interfaceId_.empty() && (interfaceProperties_.count(interfaceId_) != 0U)); 54 interfaceProperties_[interfaceId_].insert(property); 55 } 56 InitInterfacePropertyMap()57 void InitInterfacePropertyMap() 58 { 59 ES2PANDA_ASSERT(!interfaceId_.empty()); 60 interfaceProperties_.insert({interfaceId_, {}}); 61 } 62 IsInterfaceHasProperty(const std::string & interId)63 bool IsInterfaceHasProperty(const std::string &interId) 64 { 65 return interfaceProperties_.count(interId) != 0U; 66 } 67 GetInterfaceParent(const std::string & id)68 InterfacePropertyType &GetInterfaceParent(const std::string &id) 69 { 70 ES2PANDA_ASSERT(interfaceParents_.count(id) != 0); 71 return interfaceParents_[id]; 72 } 73 InsertInterfaceParent(const std::string & parent)74 void InsertInterfaceParent(const std::string &parent) 75 { 76 ES2PANDA_ASSERT(!interfaceId_.empty() && (interfaceParents_.count(interfaceId_) != 0U)); 77 interfaceParents_[interfaceId_].insert(parent); 78 } 79 InitInterfaceParentMap()80 void InitInterfaceParentMap() 81 { 82 ES2PANDA_ASSERT(!interfaceId_.empty()); 83 interfaceParents_.insert({interfaceId_, {}}); 84 } 85 IsParentExists(const std::string & interId)86 bool IsParentExists(const std::string &interId) 87 { 88 return interfaceParents_.count(interId) != 0U; 89 } 90 IsVisitedInterface(const std::string & interId)91 bool IsVisitedInterface(const std::string &interId) 92 { 93 return !visitedInterfaces_.insert(interId).second; 94 } 95 InitVisitedInterfaces()96 void InitVisitedInterfaces() 97 { 98 visitedInterfaces_.clear(); 99 } 100 101 private: 102 std::string interfaceId_ {}; 103 InterfacePropertyMapType interfaceProperties_ {}; 104 InterfacePropertyMapType interfaceParents_ {}; 105 VisitedInterfacesOfClass visitedInterfaces_ {}; 106 }; 107 108 class InterfacePropertyDeclarationsPhase : public PhaseForDeclarations { 109 public: Name()110 std::string_view Name() const override 111 { 112 return "InterfacePropertyDeclarationsPhase"; 113 } 114 115 bool PerformForModule(public_lib::Context *ctx, parser::Program *program) override; 116 117 private: GetPropCollector()118 OptionalInterfacePropertyCollector &GetPropCollector() 119 { 120 return propCollector_; 121 } 122 123 void TransformOptionalFieldTypeAnnotation(public_lib::Context *ctx, ir::ClassProperty *const field, 124 bool isInterface = false); 125 126 ir::FunctionSignature GenerateGetterOrSetterSignature(public_lib::Context *ctx, varbinder::ETSBinder *varbinder, 127 ir::ClassProperty *const field, bool isSetter, 128 varbinder::FunctionParamScope *paramScope); 129 130 ir::MethodDefinition *GenerateGetterOrSetter(public_lib::Context *ctx, varbinder::ETSBinder *varbinder, 131 ir::ClassProperty *const field, bool isSetter); 132 133 void CollectPropertiesAndSuperInterfaces(ir::TSInterfaceBody *const interface); 134 135 void HandleInternalGetterOrSetterMethod(ir::AstNode *const ast); 136 137 ir::Expression *UpdateInterfaceProperties(public_lib::Context *ctx, varbinder::ETSBinder *varbinder, 138 ir::TSInterfaceBody *const interface); 139 140 void CollectSuperInterfaceProperties(InterfacePropertyType &implInterfaceProperties, const std::string &interId); 141 142 void UpdateClassProperties(public_lib::Context *ctx, ir::ClassDefinition *const klass); 143 144 private: 145 OptionalInterfacePropertyCollector propCollector_ {}; 146 }; 147 148 } // namespace ark::es2panda::compiler 149 150 #endif 151