souffle  2.0.2-371-g6315b36
IntrinsicFunctor.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 IntrinsicFunctor.h
12  *
13  * Defines the intrinsic functor class
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "FunctorOps.h"
20 #include "ast/Argument.h"
21 #include "ast/Functor.h"
22 #include "ast/Node.h"
23 #include "parser/SrcLocation.h"
24 #include "souffle/TypeAttribute.h"
27 #include <cassert>
28 #include <cstddef>
29 #include <optional>
30 #include <ostream>
31 #include <string>
32 #include <utility>
33 #include <vector>
34 
35 namespace souffle::ast {
36 
37 /**
38  * @class IntrinsicFunctor
39  * @brief Intrinsic Functor class for functors are in-built.
40  */
41 class IntrinsicFunctor : public Functor {
42 public:
43  template <typename... Operands>
44  IntrinsicFunctor(std::string op, Operands&&... operands)
45  : Functor(std::forward<Operands>(operands)...), function(std::move(op)) {}
46 
47  template <typename... Operands>
48  IntrinsicFunctor(SrcLocation loc, std::string op, Operands&&... operands)
49  : Functor(std::move(loc), std::forward<Operands>(operands)...), function(std::move(op)) {}
50 
51  IntrinsicFunctor(std::string op, VecOwn<Argument> args, SrcLocation loc = {})
52  : Functor(std::move(args), std::move(loc)), function(std::move(op)) {}
53 
54  /** Get (base type) function */
55  const std::string& getBaseFunctionOp() const {
56  return function;
57  }
58 
59  /** Set function */
60  void setFunction(std::string functor) {
61  function = std::move(functor);
62  }
63 
64  IntrinsicFunctor* clone() const override {
65  auto* copy = new IntrinsicFunctor(function, souffle::clone(args), getSrcLoc());
66  if (finalTranslatorOpType.has_value()) {
67  copy->setFinalOpType(finalTranslatorOpType.value());
68  }
69  if (finalTranslatorReturnType.has_value()) {
70  copy->setFinalReturnType(finalTranslatorReturnType.value());
71  }
72  return copy;
73  }
74 
75  void setFinalOpType(FunctorOp newType) {
76  finalTranslatorOpType = newType;
77  }
78 
79  void setFinalReturnType(TypeAttribute newType) {
80  finalTranslatorReturnType = newType;
81  }
82 
83  std::optional<FunctorOp> getFinalOpType() const {
84  return finalTranslatorOpType;
85  }
86 
87  std::optional<TypeAttribute> getFinalReturnType() const {
89  }
90 
91 protected:
92  void print(std::ostream& os) const override {
93  if (isInfixFunctorOp(function)) {
94  os << "(" << join(args, function) << ")";
95  } else {
96  // Negation is handled differently to all other functors so we need a special case.
97  if (function == FUNCTOR_INTRINSIC_PREFIX_NEGATE_NAME) {
98  os << "-";
99  } else {
100  os << function;
101  }
102  os << "(" << join(args) << ")";
103  }
104  }
105 
106  bool equal(const Node& node) const override {
107  const auto& other = static_cast<const IntrinsicFunctor&>(node);
108  return function == other.function && Functor::equal(node);
109  }
110 
111  /** Function */
112  std::string function;
113 
114  // TODO (azreika): remove after refactoring translator
115  std::optional<FunctorOp> finalTranslatorOpType;
116  std::optional<TypeAttribute> finalTranslatorReturnType;
117 };
118 
119 } // namespace souffle::ast
souffle::ast::Term::args
VecOwn< Argument > args
Arguments.
Definition: Term.h:86
Functor.h
TypeAttribute
Type attribute class.
SrcLocation.h
souffle::ast::IntrinsicFunctor::getFinalOpType
std::optional< FunctorOp > getFinalOpType() const
Definition: IntrinsicFunctor.h:89
souffle::ast::IntrinsicFunctor::setFinalReturnType
void setFinalReturnType(TypeAttribute newType)
Definition: IntrinsicFunctor.h:85
souffle::ast::IntrinsicFunctor::getBaseFunctionOp
const std::string & getBaseFunctionOp() const
Get (base type) function.
Definition: IntrinsicFunctor.h:61
Argument.h
souffle::clone
auto clone(const std::vector< A * > &xs)
Definition: ContainerUtil.h:172
souffle::ast::IntrinsicFunctor::function
std::string function
Function.
Definition: IntrinsicFunctor.h:118
ContainerUtil.h
souffle::ast::IntrinsicFunctor::finalTranslatorOpType
std::optional< FunctorOp > finalTranslatorOpType
Definition: IntrinsicFunctor.h:121
souffle::ast::IntrinsicFunctor::IntrinsicFunctor
IntrinsicFunctor(std::string op, Operands &&... operands)
Definition: IntrinsicFunctor.h:50
souffle::ast::IntrinsicFunctor::equal
bool equal(const Node &node) const override
Abstract equality check for two AST nodes.
Definition: IntrinsicFunctor.h:112
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::ast::IntrinsicFunctor
Intrinsic Functor class for functors are in-built.
Definition: IntrinsicFunctor.h:47
souffle::ast::IntrinsicFunctor::print
void print(std::ostream &os) const override
Output to a given output stream.
Definition: IntrinsicFunctor.h:98
souffle::ast::Functor
Abstract functor class.
Definition: Functor.h:36
Node.h
souffle::FunctorOp
FunctorOp
Definition: FunctorOps.h:35
souffle::ast::IntrinsicFunctor::clone
IntrinsicFunctor * clone() const override
Create clone.
Definition: IntrinsicFunctor.h:70
souffle::detail::brie::copy
auto copy(span< A, arity > s)
Definition: Brie.h:98
std
Definition: Brie.h:3053
souffle::ast::IntrinsicFunctor::setFinalOpType
void setFinalOpType(FunctorOp newType)
Definition: IntrinsicFunctor.h:81
souffle::SrcLocation
A class describing a range in an input file.
Definition: SrcLocation.h:32
StreamUtil.h
souffle::ast::Node::Node
Node(SrcLocation loc={})
Definition: Node.h:42
FunctorOps.h
souffle::ast::Node::getSrcLoc
const SrcLocation & getSrcLoc() const
Return source location of the Node.
Definition: Node.h:46
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
souffle::ast::Term::equal
bool equal(const Node &node) const override
Abstract equality check for two AST nodes.
Definition: Term.h:80
souffle::VecOwn
std::vector< Own< A > > VecOwn
Definition: ContainerUtil.h:45
souffle::ast::IntrinsicFunctor::setFunction
void setFunction(std::string functor)
Set function.
Definition: IntrinsicFunctor.h:66
souffle::ast::IntrinsicFunctor::getFinalReturnType
std::optional< TypeAttribute > getFinalReturnType() const
Definition: IntrinsicFunctor.h:93
TypeAttribute.h
souffle::ast::IntrinsicFunctor::finalTranslatorReturnType
std::optional< TypeAttribute > finalTranslatorReturnType
Definition: IntrinsicFunctor.h:122
souffle::FUNCTOR_INTRINSIC_PREFIX_NEGATE_NAME
constexpr char FUNCTOR_INTRINSIC_PREFIX_NEGATE_NAME[]
Definition: FunctorOps.h:147