1 //===- SubElementInterfaces.cpp - Attr and Type SubElement Interfaces -----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "mlir/IR/SubElementInterfaces.h"
10 
11 using namespace mlir;
12 
13 template <typename InterfaceT>
14 static void walkSubElementsImpl(InterfaceT interface,
15                                 function_ref<void(Attribute)> walkAttrsFn,
16                                 function_ref<void(Type)> walkTypesFn) {
17   interface.walkImmediateSubElements(
18       [&](Attribute attr) {
19         // Guard against potentially null inputs. This removes the need for the
20         // derived attribute/type to do it.
21         if (!attr)
22           return;
23 
24         // Walk any sub elements first.
25         if (auto interface = attr.dyn_cast<SubElementAttrInterface>())
26           walkSubElementsImpl(interface, walkAttrsFn, walkTypesFn);
27 
28         // Walk this attribute.
29         walkAttrsFn(attr);
30       },
31       [&](Type type) {
32         // Guard against potentially null inputs. This removes the need for the
33         // derived attribute/type to do it.
34         if (!type)
35           return;
36 
37         // Walk any sub elements first.
38         if (auto interface = type.dyn_cast<SubElementTypeInterface>())
39           walkSubElementsImpl(interface, walkAttrsFn, walkTypesFn);
40 
41         // Walk this type.
42         walkTypesFn(type);
43       });
44 }
45 
46 void SubElementAttrInterface::walkSubElements(
47     function_ref<void(Attribute)> walkAttrsFn,
48     function_ref<void(Type)> walkTypesFn) {
49   assert(walkAttrsFn && walkTypesFn && "expected valid walk functions");
50   walkSubElementsImpl(*this, walkAttrsFn, walkTypesFn);
51 }
52 
53 void SubElementTypeInterface::walkSubElements(
54     function_ref<void(Attribute)> walkAttrsFn,
55     function_ref<void(Type)> walkTypesFn) {
56   assert(walkAttrsFn && walkTypesFn && "expected valid walk functions");
57   walkSubElementsImpl(*this, walkAttrsFn, walkTypesFn);
58 }
59 
60 //===----------------------------------------------------------------------===//
61 // SubElementInterface Tablegen definitions
62 //===----------------------------------------------------------------------===//
63 
64 #include "mlir/IR/SubElementAttrInterfaces.cpp.inc"
65 #include "mlir/IR/SubElementTypeInterfaces.cpp.inc"
66