souffle  2.0.2-371-g6315b36
Term.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2013, 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 Term.h
12  *
13  * Defines the abstract term class
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "ast/Argument.h"
20 #include "ast/Node.h"
21 #include "ast/utility/NodeMapper.h"
22 #include "parser/SrcLocation.h"
24 #include <algorithm>
25 #include <memory>
26 #include <string>
27 #include <utility>
28 #include <vector>
29 
30 namespace souffle::ast {
31 
32 /**
33  * @class Term
34  * @brief Defines an abstract term class used for functors and other constructors
35  */
36 class Term : public Argument {
37 protected:
38  template <typename... Operands>
39  Term(Operands&&... operands) : Term(asVec(std::forward<Operands>(operands)...)) {}
40 
41  template <typename... Operands>
42  Term(SrcLocation loc, Operands&&... operands)
43  : Term(asVec(std::forward<Operands>(operands)...), std::move(loc)) {}
44 
45  Term(VecOwn<Argument> operands, SrcLocation loc = {})
46  : Argument(std::move(loc)), args(std::move(operands)) {}
47 
48 public:
49  /** Get arguments */
50  std::vector<Argument*> getArguments() const {
51  return toPtrVector(args);
52  }
53 
54  /** Add argument to argument list */
55  void addArgument(Own<Argument> arg) {
56  args.push_back(std::move(arg));
57  }
58 
59  std::vector<const Node*> getChildNodes() const override {
60  auto res = Argument::getChildNodes();
61  for (auto& cur : args) {
62  res.push_back(cur.get());
63  }
64  return res;
65  }
66 
67  void apply(const NodeMapper& map) override {
68  for (auto& arg : args) {
69  arg = map(std::move(arg));
70  }
71  }
72 
73 protected:
74  bool equal(const Node& node) const override {
75  const auto& other = static_cast<const Term&>(node);
76  return equal_targets(args, other.args);
77  }
78 
79  /** Arguments */
81 
82 private:
83  template <typename... Operands>
84  static VecOwn<Argument> asVec(Operands... ops) {
85  Own<Argument> ary[] = {std::move(ops)...};
87  for (auto&& x : ary) {
88  xs.push_back(std::move(x));
89  }
90  return xs;
91  }
92 };
93 
94 } // namespace souffle::ast
souffle::ast::Term::args
VecOwn< Argument > args
Arguments.
Definition: Term.h:86
souffle::ast::Term::getChildNodes
std::vector< const Node * > getChildNodes() const override
Obtain a list of all embedded AST child nodes.
Definition: Term.h:65
souffle::ast::Term::apply
void apply(const NodeMapper &map) override
Apply the mapper to all child nodes.
Definition: Term.h:73
SrcLocation.h
souffle::ast::NodeMapper
An abstract class for manipulating AST Nodes by substitution.
Definition: NodeMapper.h:36
souffle::Own
std::unique_ptr< A > Own
Definition: ContainerUtil.h:42
souffle::map
auto map(const std::vector< A > &xs, F &&f)
Applies a function to each element of a vector and returns the results.
Definition: ContainerUtil.h:158
NodeMapper.h
souffle::ast::Argument
An abstract class for arguments.
Definition: Argument.h:33
Argument.h
ContainerUtil.h
souffle::equal_targets
bool equal_targets(const Container &a, const Container &b, const Comparator &comp)
A function testing whether two containers are equal with the given Comparator.
Definition: ContainerUtil.h:433
souffle::ast::Term::addArgument
void addArgument(Own< Argument > arg)
Add argument to argument list.
Definition: Term.h:61
souffle::ast::Term::asVec
static VecOwn< Argument > asVec(Operands... ops)
Definition: Term.h:90
souffle::ast::Term::getArguments
std::vector< Argument * > getArguments() const
Get arguments.
Definition: Term.h:56
Node.h
souffle::ast::Node::getChildNodes
virtual std::vector< const Node * > getChildNodes() const
Obtain a list of all embedded AST child nodes.
Definition: Node.h:82
souffle::ast::Node
Abstract class for syntactic elements in an input program.
Definition: Node.h:40
souffle::ast::Term
Defines an abstract term class used for functors and other constructors.
Definition: Term.h:42
std
Definition: Brie.h:3053
souffle::SrcLocation
A class describing a range in an input file.
Definition: SrcLocation.h:32
souffle::ast
Definition: Aggregator.h:35
souffle::ast::Term::equal
bool equal(const Node &node) const override
Abstract equality check for two AST nodes.
Definition: Term.h:80
souffle::toPtrVector
std::vector< T * > toPtrVector(const std::vector< std::unique_ptr< T >> &v)
A utility function enabling the creation of a vector of pointers.
Definition: ContainerUtil.h:146
souffle::VecOwn
std::vector< Own< A > > VecOwn
Definition: ContainerUtil.h:45
souffle::ast::Term::Term
Term(Operands &&... operands)
Definition: Term.h:45