souffle  2.0.2-371-g6315b36
NameUnnamedVariables.cpp
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 NameUnnamedVariables.cpp
12  *
13  ***********************************************************************/
14 
16 #include "ast/Clause.h"
17 #include "ast/Node.h"
18 #include "ast/Program.h"
19 #include "ast/Relation.h"
20 #include "ast/TranslationUnit.h"
21 #include "ast/UnnamedVariable.h"
22 #include "ast/Variable.h"
23 #include "ast/utility/NodeMapper.h"
24 #include "ast/utility/Utils.h"
25 #include <cstddef>
26 #include <memory>
27 #include <ostream>
28 #include <vector>
29 
30 namespace souffle::ast::transform {
31 
32 bool NameUnnamedVariablesTransformer::transform(TranslationUnit& translationUnit) {
33  bool changed = false;
34  static constexpr const char* boundPrefix = "+underscore";
35  static size_t underscoreCount = 0;
36 
37  struct nameVariables : public NodeMapper {
38  mutable bool changed = false;
39  nameVariables() = default;
40 
41  Own<Node> operator()(Own<Node> node) const override {
42  if (isA<UnnamedVariable>(node.get())) {
43  changed = true;
44  std::stringstream name;
45  name << boundPrefix << "_" << underscoreCount++;
46  return mk<ast::Variable>(name.str());
47  }
48  node->apply(*this);
49  return node;
50  }
51  };
52 
53  Program& program = translationUnit.getProgram();
54  for (Relation* rel : program.getRelations()) {
55  for (Clause* clause : getClauses(program, *rel)) {
56  nameVariables update;
57  clause->apply(update);
58  changed |= update.changed;
59  }
60  }
61 
62  return changed;
63 }
64 
65 } // namespace souffle::ast::transform
TranslationUnit.h
UnnamedVariable.h
souffle::ast::transform::NameUnnamedVariablesTransformer::transform
bool transform(TranslationUnit &translationUnit) override
Definition: NameUnnamedVariables.cpp:36
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
souffle::ast::Relation
Defines a relation with a name, attributes, qualifiers, and internal representation.
Definition: Relation.h:49
souffle::ast::Clause
Intermediate representation of a horn clause.
Definition: Clause.h:51
Relation.h
Utils.h
NodeMapper.h
NameUnnamedVariables.h
souffle::ast::Program
The program class consists of relations, clauses and types.
Definition: Program.h:52
souffle::ast::transform
Definition: Program.h:45
souffle::ast::Program::getRelations
std::vector< Relation * > getRelations() const
Return relations.
Definition: Program.h:60
souffle::ast::getClauses
std::vector< Clause * > getClauses(const Program &program, const QualifiedName &relationName)
Returns a vector of clauses in the program describing the relation with the given name.
Definition: Utils.cpp:77
Node.h
Program.h
Clause.h
rel
void rel(size_t limit, bool showLimit=true)
Definition: Tui.h:1086
souffle::ast::Program::apply
void apply(const NodeMapper &map) override
Apply the mapper to all child nodes.
Definition: Program.h:139
Variable.h