souffle  2.0.2-371-g6315b36
Directive.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 Directive.h
12  *
13  * Defines a directive for a relation
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "ast/Node.h"
20 #include "ast/QualifiedName.h"
21 #include "parser/SrcLocation.h"
24 #include <map>
25 #include <ostream>
26 #include <string>
27 #include <utility>
28 
29 namespace souffle::ast {
30 
31 enum class DirectiveType { input, output, printsize, limitsize };
32 
33 // FIXME: I'm going crazy defining these. There has to be a library that does this boilerplate for us.
34 inline std::ostream& operator<<(std::ostream& os, DirectiveType e) {
35  switch (e) {
36  case DirectiveType::input: return os << "input";
37  case DirectiveType::output: return os << "output";
38  case DirectiveType::printsize: return os << "printsize";
39  case DirectiveType::limitsize: return os << "limitsize";
40  }
41 
43 }
44 
45 /**
46  * @class Directive
47  * @brief a directive has a type (e.g. input/output/printsize/limitsize), qualified relation name, and a key
48  * value map for storing parameters of the directive.
49  */
50 class Directive : public Node {
51 public:
52  Directive(DirectiveType type, QualifiedName name, SrcLocation loc = {})
53  : Node(std::move(loc)), type(type), name(std::move(name)) {}
54 
55  /** Get directive type */
57  return type;
58  }
59 
60  /** Set directive type */
61  void setType(DirectiveType type) {
62  this->type = type;
63  }
64 
65  /** Get relation name */
66  const QualifiedName& getQualifiedName() const {
67  return name;
68  }
69 
70  /** Set relation name */
72  this->name = std::move(name);
73  }
74 
75  /** Get parameter */
76  const std::string& getParameter(const std::string& key) const {
77  return parameters.at(key);
78  }
79 
80  /** Add new parameter */
81  void addParameter(const std::string& key, std::string value) {
82  parameters[key] = std::move(value);
83  }
84 
85  /** Check for a parameter */
86  bool hasParameter(const std::string& key) const {
87  return parameters.find(key) != parameters.end();
88  }
89 
90  /** Get parameters */
91  const std::map<std::string, std::string>& getParameters() const {
92  return parameters;
93  }
94 
95  Directive* clone() const override {
96  auto res = new Directive(type, name, getSrcLoc());
97  res->parameters = parameters;
98  return res;
99  }
100 
101 protected:
102  void print(std::ostream& os) const override {
103  os << "." << type << " " << name;
104  if (!parameters.empty()) {
105  os << "(" << join(parameters, ",", [](std::ostream& out, const auto& arg) {
106  out << arg.first << "=\"" << arg.second << "\"";
107  }) << ")";
108  }
109  }
110 
111  bool equal(const Node& node) const override {
112  const auto& other = static_cast<const Directive&>(node);
113  return other.type == type && other.name == name && other.parameters == parameters;
114  }
115 
116  /** Type of directive */
118 
119  /** Relation name of the directive */
121 
122  /** Parameters of directive */
123  std::map<std::string, std::string> parameters;
124 };
125 
126 } // namespace souffle::ast
souffle::ast::Directive::type
DirectiveType type
Type of directive.
Definition: Directive.h:123
souffle::ast::Directive::getQualifiedName
const QualifiedName & getQualifiedName() const
Get relation name.
Definition: Directive.h:72
UNREACHABLE_BAD_CASE_ANALYSIS
#define UNREACHABLE_BAD_CASE_ANALYSIS
Definition: MiscUtil.h:206
souffle::ast::Directive::name
QualifiedName name
Relation name of the directive.
Definition: Directive.h:126
souffle::ast::Directive::clone
Directive * clone() const override
Create a clone (i.e.
Definition: Directive.h:101
SrcLocation.h
e
l j a showGridBackground &&c b raw series this eventEmitter e
Definition: htmlJsChartistMin.h:15
souffle::ast::Directive::Directive
Directive(DirectiveType type, QualifiedName name, SrcLocation loc={})
Definition: Directive.h:58
souffle::ast::DirectiveType
DirectiveType
Definition: Directive.h:37
souffle::ast::Directive::setQualifiedName
void setQualifiedName(QualifiedName name)
Set relation name.
Definition: Directive.h:77
MiscUtil.h
souffle::ast::Directive::hasParameter
bool hasParameter(const std::string &key) const
Check for a parameter.
Definition: Directive.h:92
souffle::ast::Directive::print
void print(std::ostream &os) const override
Output to a given output stream.
Definition: Directive.h:108
souffle::ast::Directive
a directive has a type (e.g. input/output/printsize/limitsize), qualified relation name,...
Definition: Directive.h:56
souffle::ast::Directive::equal
bool equal(const Node &node) const override
Abstract equality check for two AST nodes.
Definition: Directive.h:117
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::Directive::addParameter
void addParameter(const std::string &key, std::string value)
Add new parameter.
Definition: Directive.h:87
souffle::ast::DirectiveType::printsize
@ printsize
souffle::ast::operator<<
std::ostream & operator<<(std::ostream &os, DirectiveType e)
Definition: Directive.h:40
souffle::ast::Directive::parameters
std::map< std::string, std::string > parameters
Parameters of directive.
Definition: Directive.h:129
souffle::ast::Directive::setType
void setType(DirectiveType type)
Set directive type.
Definition: Directive.h:67
souffle::ast::DirectiveType::input
@ input
Node.h
souffle::ast::Directive::getType
DirectiveType getType() const
Get directive type.
Definition: Directive.h:62
souffle::ast::Directive::getParameter
const std::string & getParameter(const std::string &key) const
Get parameter.
Definition: Directive.h:82
souffle::ast::Directive::getParameters
const std::map< std::string, std::string > & getParameters() const
Get parameters.
Definition: Directive.h:97
QualifiedName.h
souffle::ast::DirectiveType::output
@ output
StreamUtil.h
souffle::ast::DirectiveType::limitsize
@ limitsize
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::ast::QualifiedName
Qualified Name class defines fully/partially qualified names to identify objects in components.
Definition: QualifiedName.h:39