souffle  2.0.2-371-g6315b36
Program.cpp
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 Program.cpp
12  *
13  * Defines the program class
14  *
15  * TODO(b-scholz): Remove ast/utility/Utils.h dependency!
16  *
17  ***********************************************************************/
18 
19 #include "ast/Program.h"
20 #include "ast/Clause.h"
21 #include "ast/Directive.h"
22 #include "ast/FunctorDeclaration.h"
23 #include "ast/Pragma.h"
24 #include "ast/QualifiedName.h"
25 #include "ast/Relation.h"
26 #include "ast/Type.h"
28 #include <cassert>
29 #include <string>
30 #include <utility>
31 #include <vector>
32 
33 namespace souffle::ast {
34 
35 void Program::addRelation(Own<Relation> relation) {
36  auto* existingRelation = getIf(getRelations(), [&](const Relation* current) {
37  return current->getQualifiedName() == relation->getQualifiedName();
38  });
39  assert(existingRelation == nullptr && "Redefinition of relation!");
40  relations.push_back(std::move(relation));
41 }
42 
44  for (auto it = relations.begin(); it != relations.end(); it++) {
45  const auto& rel = *it;
46  if (rel->getQualifiedName() == name) {
47  relations.erase(it);
48  return true;
49  }
50  }
51  return false;
52 }
53 
54 void Program::addClause(Own<Clause> clause) {
55  assert(clause != nullptr && "Undefined clause");
56  assert(clause->getHead() != nullptr && "Undefined head of the clause");
57  clauses.push_back(std::move(clause));
58 }
59 
60 bool Program::removeClause(const Clause* clause) {
61  for (auto it = clauses.begin(); it != clauses.end(); it++) {
62  if (**it == *clause) {
63  clauses.erase(it);
64  return true;
65  }
66  }
67  return false;
68 }
69 
70 bool Program::removeDirective(const Directive* directive) {
71  for (auto it = directives.begin(); it != directives.end(); it++) {
72  if (**it == *directive) {
73  directives.erase(it);
74  return true;
75  }
76  }
77  return false;
78 }
79 
81  auto* existingType = getIf(getTypes(),
82  [&](const Type* current) { return current->getQualifiedName() == type->getQualifiedName(); });
83  assert(existingType == nullptr && "Redefinition of type!");
84  types.push_back(std::move(type));
85 }
86 
87 void Program::addPragma(Own<Pragma> pragma) {
88  assert(pragma && "NULL pragma");
89  pragmas.push_back(std::move(pragma));
90 }
91 
93  auto* existingFunctorDecl = getIf(getFunctorDeclarations(),
94  [&](const FunctorDeclaration* current) { return current->getName() == f->getName(); });
95  assert(existingFunctorDecl == nullptr && "Redefinition of functor!");
96  functors.push_back(std::move(f));
97 }
98 
100  components.clear();
101  instantiations.clear();
102 }
103 
104 } // namespace souffle::ast
souffle::ast::FunctorDeclaration
User-defined functor declaration.
Definition: FunctorDeclaration.h:49
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
souffle::ast::FunctorDeclaration::getName
const std::string & getName() const
Return name.
Definition: FunctorDeclaration.h:59
souffle::Own
std::unique_ptr< A > Own
Definition: ContainerUtil.h:42
relation
Relation & relation
Definition: Reader.h:130
souffle::ast::Program::directives
VecOwn< Directive > directives
Directives.
Definition: Program.h:272
souffle::ast::Program::relations
VecOwn< Relation > relations
Program relations.
Definition: Program.h:263
souffle::ast::analysis::Type
An abstract base class for types to be covered within a type environment.
Definition: TypeSystem.h:51
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
Relation.h
souffle::ast::Program::removeRelationDecl
bool removeRelationDecl(const QualifiedName &name)
Remove relation.
Definition: Program.cpp:51
souffle::ast::Directive
a directive has a type (e.g. input/output/printsize/limitsize), qualified relation name,...
Definition: Directive.h:56
ContainerUtil.h
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::getTypes
std::vector< Type * > getTypes() const
Return types.
Definition: Program.h:55
Pragma.h
souffle::getIf
C::value_type getIf(const C &container, std::function< bool(const typename C::value_type)> pred)
Returns the first element in a container that satisfies a given predicate, nullptr otherwise.
Definition: ContainerUtil.h:101
FunctorDeclaration.h
QualifiedName.h
souffle::ast::Program::removeDirective
bool removeDirective(const Directive *directive)
Remove a directive.
Definition: Program.cpp:78
Program.h
Clause.h
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
Definition: Aggregator.h:35
souffle::ast::Program::types
VecOwn< Type > types
Program types
Definition: Program.h:260
souffle::ast::QualifiedName
Qualified Name class defines fully/partially qualified names to identify objects in components.
Definition: QualifiedName.h:39
rel
void rel(size_t limit, bool showLimit=true)
Definition: Tui.h:1086
souffle::ast::Program::removeClause
bool removeClause(const Clause *clause)
Remove a clause.
Definition: Program.cpp:68
std::type
ElementType type
Definition: span.h:640
souffle::ast::Program::instantiations
VecOwn< ComponentInit > instantiations
Component instantiations.
Definition: Program.h:278