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#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
23namespace ark::es2panda::parser {
24bool ClassPrivateContext::AddElement(const ir::ClassElement *elem)
25{
26    bool newPropIsStatic = elem->IsStatic();
27    util::StringView newPropName = elem->Id()->Name();
28    ir::MethodDefinitionKind newPropMethodKind = ir::MethodDefinitionKind::METHOD;
29
30    if (elem->IsMethodDefinition()) {
31        newPropMethodKind = elem->AsMethodDefinition()->Kind();
32    }
33
34    for (const auto *prop : elements_) {
35        const ir::Identifier *ident = prop->Id();
36        ir::MethodDefinitionKind methodKind = ir::MethodDefinitionKind::METHOD;
37        bool isStatic = prop->IsStatic();
38
39        if (prop->IsMethodDefinition()) {
40            methodKind = prop->AsMethodDefinition()->Kind();
41        }
42
43        if (ident == nullptr || !ident->IsPrivateIdent() || ident->Name() != newPropName ||
44            isStatic != newPropIsStatic) {
45            continue;
46        }
47
48        if ((newPropMethodKind == ir::MethodDefinitionKind::GET && methodKind == ir::MethodDefinitionKind::SET) ||
49            (newPropMethodKind == ir::MethodDefinitionKind::SET && methodKind == ir::MethodDefinitionKind::GET)) {
50            continue;
51        }
52
53        return false;
54    }
55
56    elements_.push_back(elem);
57
58    return true;
59}
60
61bool ClassPrivateContext::FindElement(const ir::Identifier *elem) const
62{
63    for (const auto *it : elements_) {
64        if (it->Id()->Name().Compare(elem->Name()) == 0) {
65            return true;
66        }
67    }
68
69    if (prev_ != nullptr) {
70        return prev_->FindElement(elem);
71    }
72
73    return false;
74}
75}  // namespace ark::es2panda::parser
76