souffle  2.0.2-371-g6315b36
While.h
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 While.h
12  *
13  ***********************************************************************/
14 
15 #pragma once
16 
17 #include "ast/TranslationUnit.h"
19 #include "ast/transform/Meta.h"
20 #include "ast/transform/Null.h"
23 #include <functional>
24 #include <memory>
25 #include <set>
26 #include <string>
27 #include <utility>
28 #include <vector>
29 
30 namespace souffle::ast::transform {
31 
32 /**
33  * Transformer that repeatedly executes a sub-transformer while a condition is met
34  */
35 class WhileTransformer : public MetaTransformer {
36 public:
37  WhileTransformer(std::function<bool()> cond, Own<Transformer> transformer)
38  : condition(std::move(cond)), transformer(std::move(transformer)) {}
39 
41  : condition([=]() { return cond; }), transformer(std::move(transformer)) {}
42 
43  std::vector<Transformer*> getSubtransformers() const override {
44  return {transformer.get()};
45  }
46  void setDebugReport() override {
47  if (auto* mt = dynamic_cast<MetaTransformer*>(transformer.get())) {
48  mt->setDebugReport();
49  } else {
50  transformer = mk<DebugReporter>(std::move(transformer));
51  }
52  }
53 
54  void setVerbosity(bool verbose) override {
55  this->verbose = verbose;
56  if (auto* mt = dynamic_cast<MetaTransformer*>(transformer.get())) {
57  mt->setVerbosity(verbose);
58  }
59  }
60 
61  void disableTransformers(const std::set<std::string>& transforms) override {
62  if (auto* mt = dynamic_cast<MetaTransformer*>(transformer.get())) {
63  mt->disableTransformers(transforms);
64  } else if (transforms.find(transformer->getName()) != transforms.end()) {
65  transformer = mk<NullTransformer>();
66  }
67  }
68 
69  std::string getName() const override {
70  return "WhileTransformer";
71  }
72 
73  WhileTransformer* clone() const override {
75  }
76 
77 private:
78  std::function<bool()> condition;
80 
81  bool transform(TranslationUnit& translationUnit) override {
82  bool changed = false;
83  while (condition()) {
84  changed |= applySubtransformer(translationUnit, transformer.get());
85  }
86  return changed;
87  }
88 };
89 
90 } // namespace souffle::ast::transform
TranslationUnit.h
souffle::ast::transform::WhileTransformer::WhileTransformer
WhileTransformer(std::function< bool()> cond, Own< Transformer > transformer)
Definition: While.h:45
souffle::ast::transform::WhileTransformer::clone
WhileTransformer * clone() const override
Definition: While.h:81
souffle::ast::transform::WhileTransformer::setVerbosity
void setVerbosity(bool verbose) override
Definition: While.h:62
souffle::ast::transform::MetaTransformer
Transformer that coordinates other sub-transformations.
Definition: Meta.h:36
souffle::Own
std::unique_ptr< A > Own
Definition: ContainerUtil.h:42
MiscUtil.h
Transformer.h
souffle::ast::transform::WhileTransformer::getName
std::string getName() const override
Definition: While.h:77
Null.h
souffle::ast::transform::WhileTransformer::disableTransformers
void disableTransformers(const std::set< std::string > &transforms) override
Definition: While.h:69
souffle::clone
auto clone(const std::vector< A * > &xs)
Definition: ContainerUtil.h:172
souffle::ast::transform::MetaTransformer::verbose
bool verbose
Definition: Meta.h:44
souffle::ast::transform::WhileTransformer::transformer
Own< Transformer > transformer
Definition: While.h:87
souffle::ast::TranslationUnit
Translation unit class for the translation pipeline.
Definition: TranslationUnit.h:51
souffle::ast::transform
Definition: Program.h:45
souffle::ast::transform::WhileTransformer::transform
bool transform(TranslationUnit &translationUnit) override
Definition: While.h:89
souffle::ast::transform::WhileTransformer
Transformer that repeatedly executes a sub-transformer while a condition is met.
Definition: While.h:39
DebugReporter.h
std
Definition: Brie.h:3053
souffle::ast::transform::WhileTransformer::setDebugReport
void setDebugReport() override
Definition: While.h:54
souffle::ast::transform::WhileTransformer::getSubtransformers
std::vector< Transformer * > getSubtransformers() const override
Definition: While.h:51
souffle::ast::transform::WhileTransformer::condition
std::function< bool()> condition
Definition: While.h:86
souffle::ast::transform::MetaTransformer::applySubtransformer
bool applySubtransformer(TranslationUnit &translationUnit, Transformer *transformer)
Definition: Meta.cpp:30
Meta.h