souffle  2.0.2-371-g6315b36
IODefaults.h
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 IODefaults.h
12  *
13  * Defines AST transformation to set defaults for IO operations.
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "Global.h"
20 #include "ast/Directive.h"
21 #include "ast/Program.h"
22 #include "ast/QualifiedName.h"
23 #include "ast/TranslationUnit.h"
27 #include <string>
28 #include <vector>
29 
30 namespace souffle::ast::transform {
31 
32 /**
33  * Transformation pass to set defaults for IO operations.
34  */
35 class IODefaultsTransformer : public Transformer {
36 public:
37  std::string getName() const override {
38  return "IODefaultsTransformer";
39  }
40 
41  IODefaultsTransformer* clone() const override {
42  return new IODefaultsTransformer();
43  }
44 
45 private:
46  bool transform(TranslationUnit& translationUnit) override {
47  bool changed = false;
48 
49  changed |= setDefaults(translationUnit);
50 
51  return changed;
52  }
53 
54  /**
55  * Set IO defaults.
56  *
57  * If no IO type is specified, use 'file'
58  * If no name is set, use the relation name.
59  * Add the operation type to the directives list.
60  * If a global fact directory is specified, add to the directives list.
61  * If a global output directory is specified, add to the directives list.
62  * If stdout is requested at the command line ('-D-'), change all output to stdout.
63  * If a printsize operation is requested, set IO type and operation accordingly.
64  *
65  * @param translationUnit
66  * @return true if any changes were made
67  */
68  bool setDefaults(TranslationUnit& translationUnit) {
69  bool changed = false;
70  Program& program = translationUnit.getProgram();
71  for (Directive* io : program.getDirectives()) {
72  // Don't do anything for a directive which
73  // is not an I/O directive
74  if (io->getType() == ast::DirectiveType::limitsize) continue;
75 
76  // Set a default IO of file
77  if (!io->hasParameter("IO")) {
78  io->addParameter("IO", "file");
79  changed = true;
80  }
81 
82  // Set the relation name
83  if (!io->hasParameter("name")) {
84  io->addParameter("name", getRelationName(io));
85  changed = true;
86  }
87 
88  // Set the operation type (input/output/printsize)
89  if (!io->hasParameter("operation")) {
90  if (io->getType() == ast::DirectiveType::input) {
91  io->addParameter("operation", "input");
92  changed = true;
93  // Configure input directory
94  if (Global::config().has("fact-dir")) {
95  io->addParameter("fact-dir", Global::config().get("fact-dir"));
96  }
97  } else if (io->getType() == ast::DirectiveType::output) {
98  io->addParameter("operation", "output");
99  changed = true;
100  // Configure output directory
101  if (Global::config().has("output-dir")) {
102  if (Global::config().has("output-dir", "-")) {
103  io->addParameter("IO", "stdout");
104  io->addParameter("headers", "true");
105  } else {
106  io->addParameter("output-dir", Global::config().get("output-dir"));
107  }
108  }
109  } else if (io->getType() == ast::DirectiveType::printsize) {
110  io->addParameter("operation", "printsize");
111  io->addParameter("IO", "stdoutprintsize");
112  changed = true;
113  }
114  }
115  }
116 
117  return changed;
118  }
119 
120  /**
121  * Get the relation name from the qualified name.
122  *
123  * @return Valid relation name from the concatenated qualified name.
124  */
125  std::string getRelationName(const Directive* node) {
126  return toString(join(node->getQualifiedName().getQualifiers(), "."));
127  }
128 };
129 
130 } // namespace souffle::ast::transform
TranslationUnit.h
Directive.h
souffle::ast::transform::IODefaultsTransformer
Transformation pass to set defaults for IO operations.
Definition: IODefaults.h:41
Transformer.h
souffle::ast::transform::IODefaultsTransformer::getRelationName
std::string getRelationName(const Directive *node)
Get the relation name from the qualified name.
Definition: IODefaults.h:137
Global.h
souffle::toString
const std::string & toString(const std::string &str)
A generic function converting strings into strings (trivial case).
Definition: StringUtil.h:234
souffle::ast::Program
The program class consists of relations, clauses and types.
Definition: Program.h:52
souffle::ast::transform::IODefaultsTransformer::transform
bool transform(TranslationUnit &translationUnit) override
Definition: IODefaults.h:58
souffle::ast::Directive
a directive has a type (e.g. input/output/printsize/limitsize), qualified relation name,...
Definition: Directive.h:56
souffle::ast::transform::IODefaultsTransformer::setDefaults
bool setDefaults(TranslationUnit &translationUnit)
Set IO defaults.
Definition: IODefaults.h:80
souffle::join
detail::joined_sequence< Iter, Printer > join(const Iter &a, const Iter &b, const std::string &sep, const Printer &p)
Creates an object to be forwarded to some output stream for printing sequences of elements interspers...
Definition: StreamUtil.h:175
StringUtil.h
souffle::ast::TranslationUnit
Translation unit class for the translation pipeline.
Definition: TranslationUnit.h:51
souffle::ast::DirectiveType::printsize
@ printsize
TCB_SPAN_NAMESPACE_NAME::get
constexpr auto get(span< E, S > s) -> decltype(s[N])
Definition: span.h:599
souffle::ast::transform
Definition: Program.h:45
souffle::ast::DirectiveType::input
@ input
souffle::ast::Program::getDirectives
std::vector< Directive * > getDirectives() const
Return relation directives.
Definition: Program.h:75
souffle::Global::config
static MainConfig & config()
Definition: Global.h:141
QualifiedName.h
Program.h
souffle::ast::DirectiveType::output
@ output
StreamUtil.h
souffle::ast::DirectiveType::limitsize
@ limitsize
souffle::ast::transform::IODefaultsTransformer::clone
IODefaultsTransformer * clone() const override
Definition: IODefaults.h:53
souffle::ast::TranslationUnit::getProgram
Program & getProgram() const
Return the program.
Definition: TranslationUnit.h:84
souffle::ast::transform::IODefaultsTransformer::getName
std::string getName() const override
Definition: IODefaults.h:49