souffle  2.0.2-371-g6315b36
FunctorDeclaration.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2018 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 FunctorDeclaration.h
12  *
13  * Defines the external functor class
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "ast/Node.h"
20 #include "parser/SrcLocation.h"
21 #include "souffle/TypeAttribute.h"
26 #include <cassert>
27 #include <cstdlib>
28 #include <ostream>
29 #include <string>
30 #include <utility>
31 #include <vector>
32 
33 namespace souffle::ast {
34 
35 /**
36  * @class FunctorDeclaration
37  * @brief User-defined functor declaration
38  *
39  * Example:
40  * .declfun foo(x:number, y:number):number
41  */
42 
43 class FunctorDeclaration : public Node {
44 public:
45  FunctorDeclaration(std::string name, std::vector<TypeAttribute> argsTypes, TypeAttribute returnType,
46  bool stateful, SrcLocation loc = {})
47  : Node(std::move(loc)), name(std::move(name)), argsTypes(std::move(argsTypes)),
49  assert(this->name.length() > 0 && "functor name is empty");
50  }
51 
52  /** Return name */
53  const std::string& getName() const {
54  return name;
55  }
56 
57  /** Return type */
58  const std::vector<TypeAttribute>& getArgsTypes() const {
59  return argsTypes;
60  }
61 
62  /** Get return type */
64  return returnType;
65  }
66 
67  /** Return number of arguments */
68  size_t getArity() const {
69  return argsTypes.size();
70  }
71 
72  /** Check whether functor is stateful */
73  bool isStateful() const {
74  return stateful;
75  }
76 
77  FunctorDeclaration* clone() const override {
79  }
80 
81 protected:
82  void print(std::ostream& out) const override {
83  auto convert = [&](TypeAttribute type) {
84  switch (type) {
85  case TypeAttribute::Signed: return "number";
86  case TypeAttribute::Symbol: return "symbol";
87  case TypeAttribute::Float: return "float";
88  case TypeAttribute::Unsigned: return "unsigned";
89  case TypeAttribute::Record: break;
90  case TypeAttribute::ADT: break;
91  }
92  fatal("unhandled `TypeAttribute`");
93  };
94 
96  out, ".declfun %s(%s): %s", name, join(map(argsTypes, convert), ","), convert(returnType));
97  if (stateful) {
98  out << " stateful";
99  }
100  out << std::endl;
101  }
102 
103  bool equal(const Node& node) const override {
104  const auto& other = static_cast<const FunctorDeclaration&>(node);
105  return name == other.name && argsTypes == other.argsTypes && returnType == other.returnType &&
106  stateful == other.stateful;
107  }
108 
109  /** Name of functor */
110  const std::string name;
111 
112  /** Types of arguments */
113  const std::vector<TypeAttribute> argsTypes;
114 
115  /** Type of the return value */
117 
118  /** Stateful flag */
119  const bool stateful;
120 };
121 
122 } // namespace souffle::ast
souffle::TypeAttribute::Record
@ Record
TypeAttribute
Type attribute class.
souffle::ast::FunctorDeclaration
User-defined functor declaration.
Definition: FunctorDeclaration.h:49
tinyformat::format
void format(std::ostream &out, const char *fmt)
Definition: tinyformat.h:1089
SrcLocation.h
souffle::ast::FunctorDeclaration::name
const std::string name
Name of functor.
Definition: FunctorDeclaration.h:116
souffle::TypeAttribute::Symbol
@ Symbol
souffle::ast::FunctorDeclaration::getName
const std::string & getName() const
Return name.
Definition: FunctorDeclaration.h:59
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
tinyformat.h
souffle::TypeAttribute::Signed
@ Signed
souffle::ast::FunctorDeclaration::argsTypes
const std::vector< TypeAttribute > argsTypes
Types of arguments.
Definition: FunctorDeclaration.h:119
souffle::ast::FunctorDeclaration::equal
bool equal(const Node &node) const override
Abstract equality check for two AST nodes.
Definition: FunctorDeclaration.h:109
souffle::TypeAttribute::Unsigned
@ Unsigned
ContainerUtil.h
souffle::ast::FunctorDeclaration::clone
FunctorDeclaration * clone() const override
Create a clone (i.e.
Definition: FunctorDeclaration.h:83
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::FunctorDeclaration::FunctorDeclaration
FunctorDeclaration(std::string name, std::vector< TypeAttribute > argsTypes, TypeAttribute returnType, bool stateful, SrcLocation loc={})
Definition: FunctorDeclaration.h:51
souffle::ast::FunctorDeclaration::getArity
size_t getArity() const
Return number of arguments.
Definition: FunctorDeclaration.h:74
souffle::TypeAttribute::ADT
@ ADT
souffle::ast::FunctorDeclaration::getArgsTypes
const std::vector< TypeAttribute > & getArgsTypes() const
Return type.
Definition: FunctorDeclaration.h:64
Node.h
souffle::ast::FunctorDeclaration::isStateful
bool isStateful() const
Check whether functor is stateful.
Definition: FunctorDeclaration.h:79
souffle::ast::FunctorDeclaration::print
void print(std::ostream &out) const override
Output to a given output stream.
Definition: FunctorDeclaration.h:88
StreamUtil.h
souffle::ast::FunctorDeclaration::returnType
const TypeAttribute returnType
Type of the return value.
Definition: FunctorDeclaration.h:122
souffle::fatal
void fatal(const char *format, const Args &... args)
Definition: MiscUtil.h:198
souffle::ast::FunctorDeclaration::stateful
const bool stateful
Stateful flag.
Definition: FunctorDeclaration.h:125
souffle::ast::Node::Node
Node(SrcLocation loc={})
Definition: Node.h:42
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::TypeAttribute::Float
@ Float
souffle::ast::FunctorDeclaration::getReturnType
TypeAttribute getReturnType() const
Get return type.
Definition: FunctorDeclaration.h:69
std::type
ElementType type
Definition: span.h:640
TypeAttribute.h