souffle  2.0.2-371-g6315b36
BranchInit.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2020 The Souffle Developers. 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 BranchInit.h
12  *
13  * Defines an argument covering the branch initialization of ADTs.
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "ast/Argument.h"
20 #include "ast/Node.h"
21 #include "ast/Term.h"
22 #include "parser/SrcLocation.h"
26 #include <iosfwd>
27 #include <string>
28 #include <utility>
29 #include <vector>
30 
31 namespace souffle::ast {
32 
33 /**
34  * @class BranchInit
35  * @brief Initialization of ADT instance.
36  *
37  * @param constructor An entity used to create a variant type. Can be though of as a name of the branch.
38  *
39  * Initializes one of the branches of ADT. The syntax for branches initialization is
40  * $Constructor(args...)
41  * In case of the branch with no arguments it is simplified to $Constructor.
42  */
43 class BranchInit : public Term {
44 public:
45  BranchInit(std::string constructor, VecOwn<Argument> args, SrcLocation loc = {})
46  : Term(std::move(args), std::move(loc)), constructor(std::move(constructor)) {}
47 
48  const std::string& getConstructor() const {
49  return constructor;
50  }
51 
52  BranchInit* clone() const override {
54  }
55 
56 protected:
57  void print(std::ostream& os) const override {
58  os << tfm::format("$%s(%s)", constructor, join(args, ", "));
59  }
60 
61  /** Implements the node comparison for this node type */
62  bool equal(const Node& node) const override {
63  const auto& other = dynamic_cast<const BranchInit&>(node);
64  return (constructor == other.constructor) && equal_targets(args, other.args);
65  }
66 
67 private:
68  /** The adt branch constructor */
69  std::string constructor;
70 };
71 
72 } // namespace souffle::ast
souffle::ast::Term::args
VecOwn< Argument > args
Arguments.
Definition: Term.h:86
tinyformat::format
void format(std::ostream &out, const char *fmt)
Definition: tinyformat.h:1089
souffle::ast::BranchInit::print
void print(std::ostream &os) const override
Output to a given output stream.
Definition: BranchInit.h:63
SrcLocation.h
tinyformat.h
souffle::ast::BranchInit::clone
BranchInit * clone() const override
Create clone.
Definition: BranchInit.h:58
Argument.h
souffle::ast::BranchInit::constructor
std::string constructor
The adt branch constructor.
Definition: BranchInit.h:75
souffle::clone
auto clone(const std::vector< A * > &xs)
Definition: ContainerUtil.h:172
ContainerUtil.h
Term.h
souffle::ast::BranchInit::getConstructor
const std::string & getConstructor() const
Definition: BranchInit.h:54
souffle::ast::BranchInit::equal
bool equal(const Node &node) const override
Implements the node comparison for this node type.
Definition: BranchInit.h:68
souffle::join
detail::joined_sequence< Iter, Printer > join(const Iter &a, const Iter &b, const std::string &sep, const Printer &p)
Creates an object to be forwarded to some output stream for printing sequences of elements interspers...
Definition: StreamUtil.h:175
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::BranchInit::BranchInit
BranchInit(std::string constructor, VecOwn< Argument > args, SrcLocation loc={})
Definition: BranchInit.h:51
Node.h
souffle::ast::Node
Abstract class for syntactic elements in an input program.
Definition: Node.h:40
StreamUtil.h
souffle::ast::BranchInit
Initialization of ADT instance.
Definition: BranchInit.h:49
souffle::ast::Node::getSrcLoc
const SrcLocation & getSrcLoc() const
Return source location of the Node.
Definition: Node.h:46
souffle::ast
Definition: Aggregator.h:35
souffle::ast::Term::Term
Term(Operands &&... operands)
Definition: Term.h:45