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