Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / runtime / onert / core / src / ir / OperationCloner.cc
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "OperationCloner.h"
18
19 #include <assert.h>
20
21 namespace onert
22 {
23 namespace ir
24 {
25
26 namespace
27 {
28
29 class OperationCloner : public OperationVisitor
30 {
31 public:
32 #define OP(Name) void visit(const operation::Name &o) override;
33 #include "ir/Operations.lst"
34 #undef OP
35
36 public:
37   std::unique_ptr<Operation> releaseClone();
38
39 private:
40   std::unique_ptr<Operation> _return_op;
41 };
42
43 #define OP(Name)                                        \
44   void OperationCloner::visit(const operation::Name &o) \
45   {                                                     \
46     assert(!_return_op);                                \
47     _return_op = std::make_unique<operation::Name>(o);  \
48   }
49 #include "ir/Operations.lst"
50 #undef OP
51
52 std::unique_ptr<Operation> OperationCloner::releaseClone()
53 {
54   assert(_return_op);
55   return std::move(_return_op);
56 }
57
58 } // namespace
59
60 std::unique_ptr<Operation> clone(const IOperation &operation)
61 {
62   OperationCloner cloner;
63   operation.accept(cloner);
64   return cloner.releaseClone();
65 }
66
67 } // namespace ir
68 } // namespace onert