souffle  2.0.2-371-g6315b36
BinaryConstraint.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 BinaryConstraint.h
12  *
13  * Defines the binary constraint class
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "ast/Argument.h"
20 #include "ast/Constraint.h"
21 #include "ast/Node.h"
22 #include "ast/utility/NodeMapper.h"
23 #include "parser/SrcLocation.h"
27 #include <cassert>
28 #include <iostream>
29 #include <memory>
30 #include <optional>
31 #include <string>
32 #include <utility>
33 #include <vector>
34 
35 namespace souffle::ast {
36 
37 /**
38  * @class BinaryConstraint
39  * @brief Binary constraint class
40  *
41  * Example:
42  * x = y
43  *
44  * A binary constraint has a constraint operator, a left-hand side
45  * expression, and right-hand side expression.
46  */
47 class BinaryConstraint : public Constraint {
48 public:
49  BinaryConstraint(BinaryConstraintOp o, Own<Argument> ls, Own<Argument> rs, SrcLocation loc = {})
50  : Constraint(std::move(loc)), operation(o), lhs(std::move(ls)), rhs(std::move(rs)) {}
51 
52  /** Return left-hand side argument */
53  Argument* getLHS() const {
54  return lhs.get();
55  }
56 
57  /** Return right-hand side argument */
58  Argument* getRHS() const {
59  return rhs.get();
60  }
61 
62  /** Return binary operator */
64  return operation;
65  }
66 
67  /** Set binary operator */
69  operation = op;
70  }
71 
72  BinaryConstraint* clone() const override {
73  auto* copy = new BinaryConstraint(operation, souffle::clone(lhs), souffle::clone(rhs), getSrcLoc());
74  if (finalTranslatorType.has_value()) {
75  copy->setFinalType(finalTranslatorType.value());
76  }
77  return copy;
78  }
79 
80  void apply(const NodeMapper& map) override {
81  lhs = map(std::move(lhs));
82  rhs = map(std::move(rhs));
83  }
84 
85  std::vector<const Node*> getChildNodes() const override {
86  return {lhs.get(), rhs.get()};
87  }
88 
89  void setFinalType(BinaryConstraintOp newType) {
90  finalTranslatorType = newType;
91  }
92 
93  std::optional<BinaryConstraintOp> getFinalType() const {
94  return finalTranslatorType;
95  }
96 
97 protected:
98  void print(std::ostream& os) const override {
100  os << *lhs << " " << operation << " " << *rhs;
101  } else {
102  os << operation << "(" << *lhs << ", " << *rhs << ")";
103  }
104  }
105 
106  bool equal(const Node& node) const override {
107  assert(isA<BinaryConstraint>(&node));
108  const auto& other = static_cast<const BinaryConstraint&>(node);
109  return operation == other.operation && equal_ptr(lhs, other.lhs) && equal_ptr(rhs, other.rhs);
110  }
111 
112  /** Constraint (base) operator */
114 
115  /** Left-hand side argument of binary constraint */
117 
118  /** Right-hand side argument of binary constraint */
120 
121  // TODO (azreika): remove after refactoring translator
122  std::optional<BinaryConstraintOp> finalTranslatorType;
123 };
124 
125 } // namespace souffle::ast
BinaryConstraintOps.h
souffle::ast::BinaryConstraint::getFinalType
std::optional< BinaryConstraintOp > getFinalType() const
Definition: BinaryConstraint.h:99
souffle::ast::BinaryConstraint::getLHS
Argument * getLHS() const
Return left-hand side argument.
Definition: BinaryConstraint.h:59
SrcLocation.h
souffle::ast::BinaryConstraint::getRHS
Argument * getRHS() const
Return right-hand side argument.
Definition: BinaryConstraint.h:64
souffle::ast::NodeMapper
An abstract class for manipulating AST Nodes by substitution.
Definition: NodeMapper.h:36
souffle::ast::BinaryConstraint::lhs
Own< Argument > lhs
Left-hand side argument of binary constraint.
Definition: BinaryConstraint.h:122
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
MiscUtil.h
Constraint.h
NodeMapper.h
souffle::ast::Argument
An abstract class for arguments.
Definition: Argument.h:33
Argument.h
souffle::clone
auto clone(const std::vector< A * > &xs)
Definition: ContainerUtil.h:172
souffle::ast::BinaryConstraint::apply
void apply(const NodeMapper &map) override
Definition: BinaryConstraint.h:86
ContainerUtil.h
souffle::ast::BinaryConstraint::setFinalType
void setFinalType(BinaryConstraintOp newType)
Definition: BinaryConstraint.h:95
o
var o
Definition: htmlJsChartistMin.h:15
souffle::ast::BinaryConstraint::getBaseOperator
BinaryConstraintOp getBaseOperator() const
Return binary operator.
Definition: BinaryConstraint.h:69
souffle::ast::BinaryConstraint::equal
bool equal(const Node &node) const override
Definition: BinaryConstraint.h:112
souffle::equal_ptr
bool equal_ptr(const T *a, const T *b)
Compares two values referenced by a pointer where the case where both pointers are null is also consi...
Definition: MiscUtil.h:130
souffle::BinaryConstraintOp
BinaryConstraintOp
Binary Constraint Operators.
Definition: BinaryConstraintOps.h:41
souffle::ast::BinaryConstraint::getChildNodes
std::vector< const Node * > getChildNodes() const override
Definition: BinaryConstraint.h:91
souffle::ast::BinaryConstraint::setBaseOperator
void setBaseOperator(BinaryConstraintOp op)
Set binary operator.
Definition: BinaryConstraint.h:74
souffle::ast::BinaryConstraint::rhs
Own< Argument > rhs
Right-hand side argument of binary constraint.
Definition: BinaryConstraint.h:125
Node.h
souffle::ast::BinaryConstraint
Binary constraint class.
Definition: BinaryConstraint.h:53
souffle::ast::BinaryConstraint::print
void print(std::ostream &os) const override
Adds print support for constraints (debugging)
Definition: BinaryConstraint.h:104
souffle::detail::brie::copy
auto copy(span< A, arity > s)
Definition: Brie.h:98
souffle::ast::Node
Abstract class for syntactic elements in an input program.
Definition: Node.h:40
souffle::ast::BinaryConstraint::BinaryConstraint
BinaryConstraint(BinaryConstraintOp o, Own< Argument > ls, Own< Argument > rs, SrcLocation loc={})
Definition: BinaryConstraint.h:55
souffle::ast::BinaryConstraint::finalTranslatorType
std::optional< BinaryConstraintOp > finalTranslatorType
Definition: BinaryConstraint.h:128
souffle::ast::BinaryConstraint::clone
BinaryConstraint * clone() const override
Definition: BinaryConstraint.h:78
souffle::ast::BinaryConstraint::operation
BinaryConstraintOp operation
Constraint (base) operator.
Definition: BinaryConstraint.h:119
souffle::isInfixFunctorOp
bool isInfixFunctorOp(std::string_view symbol)
Determines whether a functor should be written using infix notation (e.g.
Definition: FunctorOps.cpp:252
souffle::ast
Definition: Aggregator.h:35