1 | /** |
2 | * Copyright (c) Glow Contributors. See CONTRIBUTORS file. |
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 | #ifndef GLOW_EXPORTER_COMMONOPERATORWRITER_H |
18 | #define GLOW_EXPORTER_COMMONOPERATORWRITER_H |
19 | |
20 | #include "glow/Exporter/ProtobufWriter.h" |
21 | |
22 | namespace glow { |
23 | /// Declares writer methods for all operators. Every writer method serializes |
24 | /// Glow node into the provided protobuf. |
25 | template <typename Traits> class CommonOperatorWriter : public ProtobufWriter { |
26 | protected: |
27 | virtual ~CommonOperatorWriter() = default; |
28 | |
29 | /// Declare pure virtual methods, one per each node kind. |
30 | /// Derived class must to implement all of it. |
31 | #define DEF_NODE(CLASS, NAME) \ |
32 | virtual Error write##NAME(const CLASS *node, \ |
33 | typename Traits::GraphProto &graph) = 0; |
34 | #include "glow/AutoGenNodes.def" |
35 | |
36 | /// Function invokes the correspondent virtual method according to \p node |
37 | /// type to serialize node information into \p graph (protobuf), reports |
38 | /// visited intermediate nodes through \p reporter, \returns Error. |
39 | Error writeOperator(const Node *node, typename Traits::GraphProto &graph) { |
40 | switch (node->getKind()) { |
41 | #define DEF_NODE(CLASS, NAME) \ |
42 | case glow::Kinded::Kind::CLASS##Kind: \ |
43 | return write##NAME(llvm::cast<CLASS>(node), graph); |
44 | #include "glow/AutoGenNodes.def" |
45 | default: |
46 | llvm_unreachable( |
47 | "Not reachable, values and instructions are not handled here" ); |
48 | return Error::success(); |
49 | } |
50 | } |
51 | |
52 | using ProtobufWriter::ProtobufWriter; |
53 | }; |
54 | } // namespace glow |
55 | |
56 | #endif // GLOW_EXPORTER_COMMONOPERATORWRITER_H |
57 | |