17#ifndef IR_IR_INLINE_H_ 
   18#define IR_IR_INLINE_H_ 
   21#include "ir/indexed_vector.h" 
   22#include "ir/json_generator.h" 
   23#include "ir/namemap.h" 
   24#include "ir/nodemap.h" 
   25#include "ir/visitor.h" 
   26#define DEFINE_APPLY_FUNCTIONS(CLASS, TEMPLATE, TT, INLINE)                                       \ 
   27    TEMPLATE INLINE bool IR::CLASS TT::apply_visitor_preorder(Modifier &v) {                      \ 
   28        Node::traceVisit("Mod pre");                                                              \ 
   29        return v.preorder(this);                                                                  \ 
   31    TEMPLATE INLINE void IR::CLASS TT::apply_visitor_postorder(Modifier &v) {                     \ 
   32        Node::traceVisit("Mod post");                                                             \ 
   35    TEMPLATE INLINE void IR::CLASS TT::apply_visitor_revisit(Modifier &v, const Node *n) const {  \ 
   36        Node::traceVisit("Mod revisit");                                                          \ 
   39    TEMPLATE INLINE void IR::CLASS TT::apply_visitor_loop_revisit(Modifier &v) const {            \ 
   40        Node::traceVisit("Mod loop_revisit");                                                     \ 
   41        v.loop_revisit(this);                                                                     \ 
   43    TEMPLATE INLINE bool IR::CLASS TT::apply_visitor_preorder(Inspector &v) const {               \ 
   44        Node::traceVisit("Insp pre");                                                             \ 
   45        return v.preorder(this);                                                                  \ 
   47    TEMPLATE INLINE void IR::CLASS TT::apply_visitor_postorder(Inspector &v) const {              \ 
   48        Node::traceVisit("Insp post");                                                            \ 
   51    TEMPLATE INLINE void IR::CLASS TT::apply_visitor_revisit(Inspector &v) const {                \ 
   52        Node::traceVisit("Insp revisit");                                                         \ 
   55    TEMPLATE INLINE void IR::CLASS TT::apply_visitor_loop_revisit(Inspector &v) const {           \ 
   56        Node::traceVisit("Insp loop_revisit");                                                    \ 
   57        v.loop_revisit(this);                                                                     \ 
   59    TEMPLATE INLINE const IR::Node *IR::CLASS TT::apply_visitor_preorder(Transform &v) {          \ 
   60        Node::traceVisit("Trans pre");                                                            \ 
   61        return v.preorder(this);                                                                  \ 
   63    TEMPLATE INLINE const IR::Node *IR::CLASS TT::apply_visitor_postorder(Transform &v) {         \ 
   64        Node::traceVisit("Trans post");                                                           \ 
   65        return v.postorder(this);                                                                 \ 
   67    TEMPLATE INLINE void IR::CLASS TT::apply_visitor_revisit(Transform &v, const Node *n) const { \ 
   68        Node::traceVisit("Trans revisit");                                                        \ 
   71    TEMPLATE INLINE void IR::CLASS TT::apply_visitor_loop_revisit(Transform &v) const {           \ 
   72        Node::traceVisit("Trans loop_revisit");                                                   \ 
   73        v.loop_revisit(this);                                                                     \ 
   76IRNODE_ALL_TEMPLATES(DEFINE_APPLY_FUNCTIONS, 
inline)
 
   79void IR::Vector<T>::visit_children(
Visitor &v) {
 
   80    for (
auto i = vec.begin(); i != vec.end();) {
 
   81        const IR::Node *n = v.apply_visitor(*i);
 
   91        if (
auto l = n->
to<Vector<T>>()) {
 
   93            i = insert(i, l->vec.begin(), l->vec.end());
 
   97        if (
const auto *v = n->
to<VectorBase>()) {
 
  101                i = insert(i, v->size() - 1, 
nullptr);
 
  102                for (
const auto *el : *v) {
 
  104                    if (
auto e = el->template to<T>()) {
 
  107                        BUG(
"visitor returned invalid type %s for Vector<%s>", el->node_type_name(),
 
  108                            T::static_type_name());
 
  114        if (
auto e = n->
to<T>()) {
 
  118        BUG(
"visitor returned invalid type %s for Vector<%s>", n->node_type_name(),
 
  119            T::static_type_name());
 
  124    for (
auto &a : vec) v.visit(a);
 
  134IRNODE_DEFINE_APPLY_OVERLOAD(Vector, template <class T>, <T>)
 
  137    const char *sep = 
"";
 
  139    json << 
"," << std::endl << json.indent++ << 
"\"vec\" : [";
 
  140    for (
auto &k : vec) {
 
  141        json << sep << std::endl << json.indent << k;
 
  146        json << std::endl << json.indent;
 
  155    for (
auto i = begin(); i != end();) {
 
  156        auto n = v.apply_visitor(*i);
 
  166        if (
auto l = n->template to<Vector<T>>()) {
 
  168            i = insert(i, l->begin(), l->end());
 
  169            i += l->Vector<T>::size();
 
  172        if (
auto e = n->template to<T>()) {
 
  176        BUG(
"visitor returned invalid type %s for IndexedVector<%s>", n->node_type_name(),
 
  177            T::static_type_name());
 
  182    for (
auto &a : *this) v.visit(a);
 
  186    const char *sep = 
"";
 
  187    Vector<T>::toJSON(json);
 
  188    json << 
"," << std::endl << json.indent++ << 
"\"declarations\" : {";
 
  189    for (
const auto &k : declarations) {
 
  190        json << sep << std::endl << json.indent << k.first << 
" : " << k.second;
 
  195        json << std::endl << json.indent;
 
  199IRNODE_DEFINE_APPLY_OVERLOAD(IndexedVector, template <class T>, <T>)
 
  201#include "lib/ordered_map.h" 
  203static inline void namemap_insert_helper(
typename MAP::iterator, 
typename MAP::key_type k,
 
  204                                         typename MAP::mapped_type v, MAP &, MAP &new_symbols) {
 
  205    new_symbols.emplace(std::move(k), std::move(v));
 
  208template <
class MAP, 
class InputIterator>
 
  209static inline void namemap_insert_helper(
typename MAP::iterator, InputIterator b, InputIterator e,
 
  210                                         MAP &, MAP &new_symbols) {
 
  211    new_symbols.insert(b, e);
 
  215static inline void namemap_insert_helper(
typename ordered_map<cstring, T>::iterator it, 
cstring k,
 
  218    symbols.emplace_hint(it, std::move(k), std::move(v));
 
  221template <
class T, 
class InputIterator>
 
  222static inline void namemap_insert_helper(
typename ordered_map<cstring, T>::iterator it,
 
  223                                         InputIterator b, InputIterator e,
 
  226    symbols.insert(it, b, e);
 
  229template <
class T, 
template <
class K, 
class V, 
class COMP, 
class ALLOC> 
class MAP ,
 
  234    for (
auto i = symbols.begin(); i != symbols.end();) {
 
  235        const IR::Node *n = v.apply_visitor(i->second, i->first);
 
  236        if (!n && i->second) {
 
  237            i = symbols.erase(i);
 
  241        if (n == i->second) {
 
  245        if (
auto m = n->
to<NameMap>()) {
 
  246            namemap_insert_helper(i, m->symbols.begin(), m->symbols.end(), symbols, new_symbols);
 
  247            i = symbols.erase(i);
 
  250        if (
auto s = n->
to<T>()) {
 
  251            if (match_name(i->first, s)) {
 
  255                namemap_insert_helper(i, 
cstring(obj_name(s)), std::move(s), symbols, new_symbols);
 
  256                i = symbols.erase(i);
 
  260        BUG(
"visitor returned invalid type %s for NameMap<%s>", n->node_type_name(),
 
  261            T::static_type_name());
 
  263    symbols.insert(new_symbols.begin(), new_symbols.end());
 
  265template <
class T, 
template <
class K, 
class V, 
class COMP, 
class ALLOC> 
class MAP ,
 
  269    for (
auto &k : symbols) {
 
  270        v.visit(k.second, k.first);
 
  273template <
class T, 
template <
class K, 
class V, 
class COMP, 
class ALLOC> 
class MAP ,
 
  277    const char *sep = 
"";
 
  279    json << 
"," << std::endl << json.indent++ << 
"\"symbols\" : {";
 
  280    for (
auto &k : symbols) {
 
  281        json << sep << std::endl << json.indent << k.first << 
" : " << k.second;
 
  286        json << std::endl << json.indent;
 
  291template <
class KEY, 
class VALUE,
 
  292          template <
class K, 
class V, 
class COMP, 
class ALLOC> 
class MAP ,
 
  297    for (
auto i = symbols.begin(); i != symbols.end();) {
 
  300        if (!nk && i->first) {
 
  301            i = symbols.erase(i);
 
  302        } 
else if (nk == i->first) {
 
  307                i = symbols.erase(i);
 
  312            if (nv) new_symbols.emplace(nk, nv);
 
  313            i = symbols.erase(i);
 
  316    symbols.insert(new_symbols.begin(), new_symbols.end());
 
  318template <
class KEY, 
class VALUE,
 
  319          template <
class K, 
class V, 
class COMP, 
class ALLOC> 
class MAP ,
 
  323    for (
auto &k : symbols) {
 
Definition json_generator.h:34
 
Definition ordered_map.h:30
 
T * to() noexcept
Definition rtti.h:226