• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "classPrivateContext.h"
17 
18 #include "ir/expression.h"
19 #include "ir/expressions/identifier.h"
20 #include "ir/base/classElement.h"
21 #include "ir/base/methodDefinition.h"
22 
23 namespace ark::es2panda::parser {
AddElement(const ir::ClassElement * elem)24 bool ClassPrivateContext::AddElement(const ir::ClassElement *elem)
25 {
26     bool newPropIsStatic = elem->IsStatic();
27     auto id = elem->Id();
28     ES2PANDA_ASSERT(id != nullptr);
29     util::StringView newPropName = id->Name();
30     ir::MethodDefinitionKind newPropMethodKind = ir::MethodDefinitionKind::METHOD;
31 
32     if (elem->IsMethodDefinition()) {
33         newPropMethodKind = elem->AsMethodDefinition()->Kind();
34     }
35 
36     for (const auto *prop : elements_) {
37         const ir::Identifier *ident = prop->Id();
38         ir::MethodDefinitionKind methodKind = ir::MethodDefinitionKind::METHOD;
39         bool isStatic = prop->IsStatic();
40 
41         if (prop->IsMethodDefinition()) {
42             methodKind = prop->AsMethodDefinition()->Kind();
43         }
44 
45         if (ident == nullptr || !ident->IsPrivateIdent() || ident->Name() != newPropName ||
46             isStatic != newPropIsStatic) {
47             continue;
48         }
49 
50         if ((newPropMethodKind == ir::MethodDefinitionKind::GET && methodKind == ir::MethodDefinitionKind::SET) ||
51             (newPropMethodKind == ir::MethodDefinitionKind::SET && methodKind == ir::MethodDefinitionKind::GET)) {
52             continue;
53         }
54 
55         return false;
56     }
57 
58     elements_.push_back(elem);
59 
60     return true;
61 }
62 
FindElement(const ir::Identifier * elem) const63 bool ClassPrivateContext::FindElement(const ir::Identifier *elem) const
64 {
65     for (const auto *it : elements_) {
66         auto id = it->Id();
67         ES2PANDA_ASSERT(id != nullptr);
68         if (id->Name().Compare(elem->Name()) == 0) {
69             return true;
70         }
71     }
72 
73     if (prev_ != nullptr) {
74         return prev_->FindElement(elem);
75     }
76 
77     return false;
78 }
79 }  // namespace ark::es2panda::parser
80