souffle  2.0.2-371-g6315b36
NodeMapper.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved
4  * Licensed under the Universal Permissive License v 1.0 as shown at:
5  * - https://opensource.org/licenses/UPL
6  * - <souffle root>/licenses/SOUFFLE-UPL.txt
7  */
8 
9 /************************************************************************
10  *
11  * @file NodeMapper.h
12  *
13  * Defines the node mapper class
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
21 #include <cassert>
22 #include <memory>
23 
24 namespace souffle::ast {
25 class Node;
26 
27 /**
28  * An abstract class for manipulating AST Nodes by substitution
29  */
30 class NodeMapper {
31 public:
32  virtual ~NodeMapper() = default;
33 
34  /**
35  * Abstract replacement method for a node.
36  *
37  * If the given nodes is to be replaced, the handed in node
38  * will be destroyed by the mapper and the returned node
39  * will become owned by the caller.
40  */
41  virtual Own<Node> operator()(Own<Node> node) const = 0;
42 
43  /**
44  * Wrapper for any subclass of the AST node hierarchy performing type casts.
45  */
46  template <typename T>
47  Own<T> operator()(Own<T> node) const {
48  Own<Node> resPtr = (*this)(Own<Node>(static_cast<Node*>(node.release())));
49  assert(isA<T>(resPtr.get()) && "Invalid target node!");
50  return Own<T>(dynamic_cast<T*>(resPtr.release()));
51  }
52 };
53 
54 } // namespace souffle::ast
souffle::ast::NodeMapper::operator()
virtual Own< Node > operator()(Own< Node > node) const =0
Abstract replacement method for a node.
souffle::Own
std::unique_ptr< A > Own
Definition: ContainerUtil.h:42
MiscUtil.h
ContainerUtil.h
souffle::ast::Node
Abstract class for syntactic elements in an input program.
Definition: Node.h:40
souffle::ast
Definition: Aggregator.h:35
souffle::ast::NodeMapper::~NodeMapper
virtual ~NodeMapper()=default