1/*
2 * Copyright (c) 2022 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 ECMASCRIPT_COMPILER_DEPEND_CHAIN_HELPER_H
17#define ECMASCRIPT_COMPILER_DEPEND_CHAIN_HELPER_H
18
19#include "ecmascript/compiler/circuit_builder.h"
20#include "ecmascript/compiler/gate_accessor.h"
21#include "ecmascript/mem/chunk_containers.h"
22
23namespace panda::ecmascript::kungfu {
24class DependChains : public ChunkObject {
25public:
26    struct Node {
27        Node(GateRef gate, Node* next) : gate(gate), next(next) {}
28        GateRef gate;
29        Node *next;
30    };
31
32    struct DependChainIterator {
33    public:
34        DependChainIterator(Node* node) : node_(node) {}
35
36        DependChainIterator& operator++()
37        {
38            ASSERT(node_ != nullptr);
39            node_ = node_->next;
40            return *this;
41        }
42
43        bool operator!=(const DependChainIterator& that) const
44        {
45            return node_ != that.node_;
46        }
47
48        GateRef GetCurrentGate()
49        {
50            return node_->gate;
51        }
52    private:
53        Node* node_;
54    };
55
56    DependChains(Chunk* chunk) : chunk_(chunk) {}
57    ~DependChains() = default;
58
59    DependChains* UpdateNode(GateRef gate);
60    bool Equals(DependChains* that);
61    void Merge(DependChains* that);
62    void CopyFrom(DependChains *other)
63    {
64        head_ = other->head_;
65        size_ = other->size_;
66    }
67
68    GateRef GetHeadGate()
69    {
70        return head_->gate;
71    }
72
73    DependChainIterator begin()
74    {
75        return DependChainIterator(head_);
76    }
77
78    DependChainIterator end()
79    {
80        return DependChainIterator(nullptr);
81    }
82
83private:
84    Node *head_{nullptr};
85    size_t size_ {0};
86    Chunk* chunk_;
87};
88}  // panda::ecmascript::kungfu
89#endif  // ECMASCRIPT_COMPILER_DEPEND_CHAIN_HELPER_H