souffle  2.0.2-371-g6315b36
Program.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 Program.h
12  *
13  * Defines the program class
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "ast/Clause.h"
20 #include "ast/Component.h"
21 #include "ast/ComponentInit.h"
22 #include "ast/Directive.h"
23 #include "ast/FunctorDeclaration.h"
24 #include "ast/Node.h"
25 #include "ast/Pragma.h"
26 #include "ast/QualifiedName.h"
27 #include "ast/Relation.h"
28 #include "ast/Type.h"
29 #include "ast/utility/NodeMapper.h"
32 #include <algorithm>
33 #include <cassert>
34 #include <iosfwd>
35 #include <memory>
36 #include <ostream>
37 #include <utility>
38 #include <vector>
39 
40 namespace souffle {
41 class ParserDriver;
42 }
43 namespace souffle::ast {
44 
45 namespace transform {
47 }
48 /**
49  * @class Program
50  * @brief The program class consists of relations, clauses and types.
51  */
52 class Program : public Node {
53 public:
54  /** Return types */
55  std::vector<Type*> getTypes() const {
56  return toPtrVector(types);
57  }
58 
59  /** Return relations */
60  std::vector<Relation*> getRelations() const {
61  return toPtrVector(relations);
62  }
63 
64  /** Return clauses */
65  std::vector<Clause*> getClauses() const {
66  return toPtrVector(clauses);
67  }
68 
69  /** Return functor declarations */
70  std::vector<FunctorDeclaration*> getFunctorDeclarations() const {
71  return toPtrVector(functors);
72  }
73 
74  /** Return relation directives */
75  std::vector<Directive*> getDirectives() const {
76  return toPtrVector(directives);
77  }
78 
79  /** Add relation directive */
80  void addDirective(Own<Directive> directive) {
81  assert(directive && "NULL directive");
82  directives.push_back(std::move(directive));
83  }
84 
85  /** Return pragma directives */
87  return pragmas;
88  }
89 
90  /* Add relation */
92 
93  /** Remove relation */
94  bool removeRelationDecl(const QualifiedName& name);
95 
96  /** Set clauses */
97  void setClauses(VecOwn<Clause> newClauses) {
98  clauses = std::move(newClauses);
99  }
100 
101  /** Add a clause */
102  void addClause(Own<Clause> clause);
103 
104  /** Add a type declaration */
105  void addType(Own<Type> type);
106 
107  /** Remove a clause */
108  bool removeClause(const Clause* clause);
109 
110  /** Remove a directive */
111  bool removeDirective(const Directive* directive);
112 
113  /** Return components */
114  std::vector<Component*> getComponents() const {
115  return toPtrVector(components);
116  }
117 
118  /** Return component instantiation */
119  std::vector<ComponentInit*> getComponentInstantiations() const {
120  return toPtrVector(instantiations);
121  }
122 
123  /** Remove components and components' instantiations */
124  void clearComponents();
125 
126  Program* clone() const override {
127  auto res = new Program();
128  res->pragmas = souffle::clone(pragmas);
129  res->components = souffle::clone(components);
130  res->instantiations = souffle::clone(instantiations);
131  res->types = souffle::clone(types);
132  res->functors = souffle::clone(functors);
133  res->relations = souffle::clone(relations);
134  res->clauses = souffle::clone(clauses);
135  res->directives = souffle::clone(directives);
136  return res;
137  }
138 
139  void apply(const NodeMapper& map) override {
140  for (auto& cur : pragmas) {
141  cur = map(std::move(cur));
142  }
143  for (auto& cur : components) {
144  cur = map(std::move(cur));
145  }
146  for (auto& cur : instantiations) {
147  cur = map(std::move(cur));
148  }
149  for (auto& cur : functors) {
150  cur = map(std::move(cur));
151  }
152  for (auto& cur : types) {
153  cur = map(std::move(cur));
154  }
155  for (auto& cur : relations) {
156  cur = map(std::move(cur));
157  }
158  for (auto& cur : clauses) {
159  cur = map(std::move(cur));
160  }
161  for (auto& cur : directives) {
162  cur = map(std::move(cur));
163  }
164  }
165 
166  std::vector<const Node*> getChildNodes() const override {
167  std::vector<const Node*> res;
168  for (const auto& cur : pragmas) {
169  res.push_back(cur.get());
170  }
171  for (const auto& cur : components) {
172  res.push_back(cur.get());
173  }
174  for (const auto& cur : instantiations) {
175  res.push_back(cur.get());
176  }
177  for (const auto& cur : functors) {
178  res.push_back(cur.get());
179  }
180  for (const auto& cur : types) {
181  res.push_back(cur.get());
182  }
183  for (const auto& cur : relations) {
184  res.push_back(cur.get());
185  }
186  for (const auto& cur : clauses) {
187  res.push_back(cur.get());
188  }
189  for (const auto& cur : directives) {
190  res.push_back(cur.get());
191  }
192  return res;
193  }
194 
195 protected:
196  void print(std::ostream& os) const override {
197  auto show = [&](auto&& xs, char const* sep = "\n") {
198  if (!xs.empty()) os << join(xs, sep) << "\n";
199  };
200 
201  show(pragmas, "\n\n");
202  show(components);
203  show(instantiations);
204  show(types);
205  show(functors);
206  show(relations);
207  show(clauses, "\n\n");
208  show(directives, "\n\n");
209  }
210 
211  bool equal(const Node& node) const override {
212  const auto& other = static_cast<const Program&>(node);
213  if (!equal_targets(pragmas, other.pragmas)) {
214  return false;
215  }
216  if (!equal_targets(components, other.components)) {
217  return false;
218  }
219  if (!equal_targets(instantiations, other.instantiations)) {
220  return false;
221  }
222  if (!equal_targets(functors, other.functors)) {
223  return false;
224  }
225  if (!equal_targets(types, other.types)) {
226  return false;
227  }
228  if (!equal_targets(relations, other.relations)) {
229  return false;
230  }
231  if (!equal_targets(clauses, other.clauses)) {
232  return false;
233  }
234  if (!equal_targets(directives, other.directives)) {
235  return false;
236  }
237  return true;
238  }
239 
240 protected:
241  friend class souffle::ParserDriver;
242 
243  void addPragma(Own<Pragma> pragma);
244 
246 
247  /** Add component */
248  void addComponent(Own<Component> component) {
249  assert(component && "NULL component");
250  components.push_back(std::move(component));
251  }
252 
253  /** Add component instantiation */
254  void addInstantiation(Own<ComponentInit> instantiation) {
255  assert(instantiation && "NULL instantiation");
256  instantiations.push_back(std::move(instantiation));
257  }
258 
259  /** Program types */
261 
262  /** Program relations */
264 
265  /** External Functors */
267 
268  /** Program clauses */
270 
271  /** Directives */
273 
274  /** Component definitions */
276 
277  /** Component instantiations */
279 
280  /** Pragmas */
282 };
283 
284 } // namespace souffle::ast
souffle::ast::Program::getComponents
std::vector< Component * > getComponents() const
Return components.
Definition: Program.h:114
souffle::ast::Program::equal
bool equal(const Node &node) const override
Abstract equality check for two AST nodes.
Definition: Program.h:211
souffle::ast::Program::getFunctorDeclarations
std::vector< FunctorDeclaration * > getFunctorDeclarations() const
Return functor declarations.
Definition: Program.h:70
Directive.h
souffle::ast::Program::pragmas
VecOwn< Pragma > pragmas
Pragmas.
Definition: Program.h:281
Component.h
souffle::ast::NodeMapper
An abstract class for manipulating AST Nodes by substitution.
Definition: NodeMapper.h:36
souffle::Own
std::unique_ptr< A > Own
Definition: ContainerUtil.h:42
relation
Relation & relation
Definition: Reader.h:130
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
souffle::ast::Program::directives
VecOwn< Directive > directives
Directives.
Definition: Program.h:272
souffle::ast::Clause
Intermediate representation of a horn clause.
Definition: Clause.h:51
souffle::ast::Program::getClauses
std::vector< Clause * > getClauses() const
Return clauses.
Definition: Program.h:65
souffle::ast::Program::relations
VecOwn< Relation > relations
Program relations.
Definition: Program.h:263
souffle::ast::Program::clauses
VecOwn< Clause > clauses
Program clauses.
Definition: Program.h:269
souffle::ast::Program::addPragma
void addPragma(Own< Pragma > pragma)
Definition: Program.cpp:95
ComponentInit.h
Relation.h
souffle::ast::Program::addComponent
void addComponent(Own< Component > component)
Add component.
Definition: Program.h:248
souffle::ParserDriver
Definition: ParserDriver.h:57
souffle::ast::Program::removeRelationDecl
bool removeRelationDecl(const QualifiedName &name)
Remove relation.
Definition: Program.cpp:51
NodeMapper.h
souffle::ast::Program
The program class consists of relations, clauses and types.
Definition: Program.h:52
souffle::ast::Program::setClauses
void setClauses(VecOwn< Clause > newClauses)
Set clauses.
Definition: Program.h:97
souffle::ast::Program::getPragmaDirectives
const VecOwn< Pragma > & getPragmaDirectives() const
Return pragma directives.
Definition: Program.h:86
souffle::ast::Directive
a directive has a type (e.g. input/output/printsize/limitsize), qualified relation name,...
Definition: Directive.h:56
souffle::clone
auto clone(const std::vector< A * > &xs)
Definition: ContainerUtil.h:172
ContainerUtil.h
souffle::ast::Program::getChildNodes
std::vector< const Node * > getChildNodes() const override
Obtain a list of all embedded AST child nodes.
Definition: Program.h:166
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::transform::ComponentInstantiationTransformer
Definition: ComponentInstantiation.h:27
souffle::ast::Program::addType
void addType(Own< Type > type)
Add a type declaration.
Definition: Program.cpp:88
souffle::ast::Program::addClause
void addClause(Own< Clause > clause)
Add a clause.
Definition: Program.cpp:62
souffle::ast::Program::functors
VecOwn< FunctorDeclaration > functors
External Functors.
Definition: Program.h:266
souffle::ast::Program::getRelations
std::vector< Relation * > getRelations() const
Return relations.
Definition: Program.h:60
souffle::ast::Program::clearComponents
void clearComponents()
Remove components and components' instantiations.
Definition: Program.cpp:107
Type.h
souffle::ast::Program::addRelation
void addRelation(Own< Relation > relation)
Definition: Program.cpp:43
souffle::ast::Program::getDirectives
std::vector< Directive * > getDirectives() const
Return relation directives.
Definition: Program.h:75
Node.h
souffle::ast::Program::getTypes
std::vector< Type * > getTypes() const
Return types.
Definition: Program.h:55
souffle::ast::Program::clone
Program * clone() const override
Create a clone (i.e.
Definition: Program.h:126
Pragma.h
FunctorDeclaration.h
souffle::ast::Node
Abstract class for syntactic elements in an input program.
Definition: Node.h:40
QualifiedName.h
souffle::ast::Program::removeDirective
bool removeDirective(const Directive *directive)
Remove a directive.
Definition: Program.cpp:78
souffle::ast::Program::print
void print(std::ostream &os) const override
Output to a given output stream.
Definition: Program.h:196
StreamUtil.h
souffle::ast::Program::getComponentInstantiations
std::vector< ComponentInit * > getComponentInstantiations() const
Return component instantiation.
Definition: Program.h:119
Clause.h
souffle
Definition: AggregateOp.h:25
souffle::ast::Program::addFunctorDeclaration
void addFunctorDeclaration(Own< FunctorDeclaration > functor)
Definition: Program.cpp:100
souffle::ast::Program::components
VecOwn< Component > components
Component definitions.
Definition: Program.h:275
souffle::ast::Program::addInstantiation
void addInstantiation(Own< ComponentInit > instantiation)
Add component instantiation.
Definition: Program.h:254
souffle::ast
Definition: Aggregator.h:35
souffle::ast::Program::types
VecOwn< Type > types
Program types
Definition: Program.h:260
souffle::ast::Program::addDirective
void addDirective(Own< Directive > directive)
Add relation directive.
Definition: Program.h:80
souffle::ast::QualifiedName
Qualified Name class defines fully/partially qualified names to identify objects in components.
Definition: QualifiedName.h:39
souffle::ast::Program::apply
void apply(const NodeMapper &map) override
Apply the mapper to all child nodes.
Definition: Program.h:139
souffle::ast::Program::removeClause
bool removeClause(const Clause *clause)
Remove a clause.
Definition: Program.cpp:68
souffle::toPtrVector
std::vector< T * > toPtrVector(const std::vector< std::unique_ptr< T >> &v)
A utility function enabling the creation of a vector of pointers.
Definition: ContainerUtil.h:146
souffle::VecOwn
std::vector< Own< A > > VecOwn
Definition: ContainerUtil.h:45
std::type
ElementType type
Definition: span.h:640
souffle::ast::Program::instantiations
VecOwn< ComponentInit > instantiations
Component instantiations.
Definition: Program.h:278