souffle  2.0.2-371-g6315b36
Public Member Functions | Private Member Functions
souffle::ast::transform::UniqueAggregationVariablesTransformer Class Reference

Transformation pass to rename aggregation variables to make them unique. More...

#include <UniqueAggregationVariables.h>

Inheritance diagram for souffle::ast::transform::UniqueAggregationVariablesTransformer:
Inheritance graph
Collaboration diagram for souffle::ast::transform::UniqueAggregationVariablesTransformer:
Collaboration graph

Public Member Functions

UniqueAggregationVariablesTransformerclone () const override
 
std::string getName () const override
 
- Public Member Functions inherited from souffle::ast::transform::Transformer
bool apply (TranslationUnit &translationUnit)
 
virtual ~Transformer ()=default
 

Private Member Functions

bool transform (TranslationUnit &translationUnit) override
 Renames all local variables of the aggregate to something unique, so that the scope of the local variable is limited to the body of the aggregate subclause. More...
 

Detailed Description

Transformation pass to rename aggregation variables to make them unique.

Definition at line 30 of file UniqueAggregationVariables.h.

Member Function Documentation

◆ clone()

UniqueAggregationVariablesTransformer* souffle::ast::transform::UniqueAggregationVariablesTransformer::clone ( ) const
inlineoverridevirtual

Implements souffle::ast::transform::Transformer.

Definition at line 40 of file UniqueAggregationVariables.h.

◆ getName()

std::string souffle::ast::transform::UniqueAggregationVariablesTransformer::getName ( ) const
inlineoverridevirtual

Implements souffle::ast::transform::Transformer.

Definition at line 36 of file UniqueAggregationVariables.h.

36  :
37  bool transform(TranslationUnit& translationUnit) override;
38 };

◆ transform()

bool souffle::ast::transform::UniqueAggregationVariablesTransformer::transform ( TranslationUnit translationUnit)
overrideprivatevirtual

Renames all local variables of the aggregate to something unique, so that the scope of the local variable is limited to the body of the aggregate subclause.

This assumes that we have simplified the target expression to a target variable.

Implements souffle::ast::transform::Transformer.

Definition at line 38 of file UniqueAggregationVariables.cpp.

38  {
39  // find out if the target expression variable occurs elsewhere in the rule. If so, rename it
40  // to avoid naming conflicts
41  visitDepthFirst(clause, [&](const Aggregator& agg) {
42  // get the set of local variables in this aggregate and rename
43  // those that occur outside the aggregate
44  std::set<std::string> localVariables = analysis::getLocalVariables(translationUnit, clause, agg);
45  std::set<std::string> variablesOutsideAggregate =
47  for (const std::string& name : localVariables) {
48  if (variablesOutsideAggregate.find(name) != variablesOutsideAggregate.end()) {
49  // then this MUST be renamed to avoid scoping issues
50  std::string uniqueName = analysis::findUniqueVariableName(clause, name);
51  visitDepthFirst(agg, [&](const Variable& var) {
52  if (var.getName() == name) {
53  const_cast<Variable&>(var).setName(uniqueName);
54  changed = true;
55  }
56  });
57  }
58  }
59  });
60  });
61  return changed;
62 }
63 
64 } // namespace souffle::ast::transform

The documentation for this class was generated from the following files:
souffle::ast::analysis::getLocalVariables
std::set< std::string > getLocalVariables(const TranslationUnit &tu, const Clause &clause, const Aggregator &aggregate)
Computes the set of local variables in an aggregate expression.
Definition: Aggregate.cpp:55
souffle::ast::transform::UniqueAggregationVariablesTransformer::transform
bool transform(TranslationUnit &translationUnit) override
Renames all local variables of the aggregate to something unique, so that the scope of the local vari...
Definition: UniqueAggregationVariables.cpp:38
souffle::ast::analysis::getVariablesOutsideAggregate
std::set< std::string > getVariablesOutsideAggregate(const Clause &clause, const Aggregator &aggregate)
Computes the set of variables occurring outside the aggregate.
Definition: Aggregate.cpp:164
souffle::ast::analysis::findUniqueVariableName
std::string findUniqueVariableName(const Clause &clause, std::string base)
Find a variable name using base to form a string like base1 Use this when you need to limit the scope...
Definition: Aggregate.cpp:179
souffle::ast::visitDepthFirst
void visitDepthFirst(const Node &root, Visitor< R, Ps... > &visitor, Args &... args)
A utility function visiting all nodes within the ast rooted by the given node recursively in a depth-...
Definition: Visitor.h:273