souffle  2.0.2-371-g6315b36
TranslationUnit.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2015, Oracle and/or its affiliates. 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 TranslationUnit.h
12  *
13  * Defines the translation unit class
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "Global.h"
20 #include "ast/Program.h"
21 #include "ast/analysis/Analysis.h"
23 #include "ast/analysis/SCCGraph.h"
25 #include "ast/analysis/Type.h"
26 #include "reports/DebugReport.h"
27 #include "reports/ErrorReport.h"
28 #include <map>
29 #include <memory>
30 #include <sstream>
31 #include <string>
32 #include <utility>
33 
34 namespace souffle::ast {
35 
36 /**
37  * @class TranslationUnit
38  * @brief Translation unit class for the translation pipeline
39  *
40  * The translation unit class consisting of
41  * an symbol table, AST program, error reports, and
42  * cached analysis results.
43  */
44 
45 class TranslationUnit {
46 public:
47  TranslationUnit(Own<Program> program, ErrorReport& e, DebugReport& d)
48  : program(std::move(program)), errorReport(e), debugReport(d) {}
49 
50  virtual ~TranslationUnit() = default;
51 
52  /** get analysis: analysis is generated on the fly if not present */
53  template <class Analysis>
54  Analysis* getAnalysis() const {
55  static const bool debug = Global::config().has("debug-report");
56  std::string name = Analysis::name;
57  auto it = analyses.find(name);
58  if (it == analyses.end()) {
59  // analysis does not exist yet, create instance and run it.
60  analyses[name] = mk<Analysis>();
61  analyses[name]->run(*this);
62  if (debug) {
63  std::stringstream ss;
64  analyses[name]->print(ss);
65  if (!isA<analysis::PrecedenceGraphAnalysis>(analyses[name].get()) &&
66  !isA<analysis::SCCGraphAnalysis>(analyses[name].get())) {
67  debugReport.addSection(name, "Ast Analysis [" + name + "]", ss.str());
68  } else {
70  DebugReportSection(name, "Ast Analysis [" + name + "]", {}, ss.str()));
71  }
72  }
73  }
74  return dynamic_cast<Analysis*>(analyses[name].get());
75  }
76 
77  /** Return the program */
78  Program& getProgram() const {
79  assert(program && "NULL program");
80  return *program.get();
81  }
82 
83  /** Return error report */
85  return errorReport;
86  }
87 
88  /** Destroy all cached analyses of translation unit */
89  void invalidateAnalyses() const {
90  analyses.clear();
91  }
92 
93  /** Return debug report */
95  return debugReport;
96  }
97 
98 private:
99  /** Cached analyses */
100  mutable std::map<std::string, Own<analysis::Analysis>> analyses;
101 
102  /** AST program */
104 
105  /** Error report capturing errors while compiling */
107 
108  /** HTML debug report */
110 };
111 
112 } // namespace souffle::ast
DebugReport.h
souffle::DebugReportSection
Class representing a section of a HTML report.
Definition: DebugReport.h:39
souffle::ast::TranslationUnit::invalidateAnalyses
void invalidateAnalyses() const
Destroy all cached analyses of translation unit.
Definition: TranslationUnit.h:95
e
l j a showGridBackground &&c b raw series this eventEmitter e
Definition: htmlJsChartistMin.h:15
souffle::Own
std::unique_ptr< A > Own
Definition: ContainerUtil.h:42
souffle::ast::TranslationUnit::program
Own< Program > program
AST program.
Definition: TranslationUnit.h:109
souffle::DebugReport::addSection
void addSection(DebugReportSection section)
Definition: DebugReport.h:93
SCCGraph.h
souffle::ast::TranslationUnit::~TranslationUnit
virtual ~TranslationUnit()=default
Global.h
souffle::ast::TranslationUnit::getErrorReport
ErrorReport & getErrorReport()
Return error report.
Definition: TranslationUnit.h:90
souffle::DebugReport
Class representing a HTML report, consisting of a list of sections.
Definition: DebugReport.h:87
TCB_SPAN_NAMESPACE_NAME::get
constexpr auto get(span< E, S > s) -> decltype(s[N])
Definition: span.h:599
souffle::ast::TranslationUnit::getDebugReport
DebugReport & getDebugReport()
Return debug report.
Definition: TranslationUnit.h:100
souffle::ast::TranslationUnit::analyses
std::map< std::string, Own< analysis::Analysis > > analyses
Cached analyses.
Definition: TranslationUnit.h:106
souffle::ast::analysis::Analysis
Abstract class for a AST Analysis.
Definition: Analysis.h:38
souffle::ast::TranslationUnit::getAnalysis
Analysis * getAnalysis() const
get analysis: analysis is generated on the fly if not present
Definition: TranslationUnit.h:60
souffle::Global::config
static MainConfig & config()
Definition: Global.h:141
std
Definition: Brie.h:3053
Program.h
SumTypeBranches.h
Type.h
d
else d
Definition: htmlJsChartistMin.h:15
PrecedenceGraph.h
souffle::ast
Definition: Aggregator.h:35
souffle::ErrorReport
Definition: ErrorReport.h:152
souffle::ast::TranslationUnit::getProgram
Program & getProgram() const
Return the program.
Definition: TranslationUnit.h:84
Analysis.h
ErrorReport.h
souffle::ast::TranslationUnit::debugReport
DebugReport & debugReport
HTML debug report.
Definition: TranslationUnit.h:115
souffle::ast::TranslationUnit::errorReport
ErrorReport & errorReport
Error report capturing errors while compiling.
Definition: TranslationUnit.h:112
souffle::profile::ss
class souffle::profile::Tui ss
Definition: Tui.h:336
souffle::ast::TranslationUnit::TranslationUnit
TranslationUnit(Own< Program > program, ErrorReport &e, DebugReport &d)
Definition: TranslationUnit.h:53