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  * Define a RAM translation unit
14  *
15  ***********************************************************************/
16 
17 #pragma once
18 
19 #include "Global.h"
20 #include "ram/Program.h"
21 #include "ram/analysis/Analysis.h"
22 #include "reports/DebugReport.h"
23 #include "reports/ErrorReport.h"
24 #include "souffle/SymbolTable.h"
25 #include <cassert>
26 #include <iosfwd>
27 #include <map>
28 #include <memory>
29 #include <set>
30 #include <string>
31 #include <utility>
32 
33 namespace souffle {
34 
35 namespace ram {
36 
37 /**
38  * @class TranslationUnit
39  * @brief Translating a RAM program
40  *
41  * Comprises the program, symbol table, error report, debug report, and analyses
42  */
43 class TranslationUnit {
44 public:
45  TranslationUnit(Own<Program> prog, SymbolTable sym, ErrorReport& e, DebugReport& d)
46  : program(std::move(prog)), symbolTable(std::move(sym)), errorReport(e), debugReport(d) {
47  assert(program != nullptr && "program is a null-pointer");
48  }
49 
50  virtual ~TranslationUnit() = default;
51 
52  /** @brief Get an analysis */
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  auto analysis = mk<Analysis>(Analysis::name);
61  analysis->run(*this);
62  // output analysis in debug report
63  if (debug) {
64  std::stringstream ramAnalysisStr;
65  ramAnalysisStr << *analysis;
66  if (!ramAnalysisStr.str().empty()) {
68  analysis->getName(), "RAM Analysis " + analysis->getName(), ramAnalysisStr.str());
69  }
70  }
71  // check it hasn't been created by someone else, and insert if not
72  it = analyses.find(name);
73  if (it == analyses.end()) {
74  analyses[name] = std::move(analysis);
75  }
76  }
77  return dynamic_cast<Analysis*>(analyses[name].get());
78  }
79 
80  /** @brief Get all alive analyses */
81  std::set<const analysis::Analysis*> getAliveAnalyses() const {
82  std::set<const analysis::Analysis*> result;
83  for (auto const& a : analyses) {
84  result.insert(a.second.get());
85  }
86  return result;
87  }
88 
89  /** @brief Invalidate all alive analyses of the translation unit */
90  void invalidateAnalyses() {
91  analyses.clear();
92  }
93 
94  /** @brief Get the RAM Program of the translation unit */
95  Program& getProgram() const {
96  return *program;
97  }
98 
99  /** @brief Obtain symbol table */
101  return symbolTable;
102  }
103 
104  /** @brief Obtain error report */
106  return errorReport;
107  }
108 
109  /** @brief Obtain debug report */
111  return debugReport;
112  }
113 
114 protected:
115  /* Cached analyses */
116  mutable std::map<std::string, Own<analysis::Analysis>> analyses;
117 
118  /* RAM program */
120 
121  /* Symbol table for the RAM program */
123 
124  /* Error report for raising errors and warnings */
126 
127  /* Debug report for logging information */
129 };
130 
131 } // namespace ram
132 } // end of namespace souffle
souffle::ram::TranslationUnit::errorReport
ErrorReport & errorReport
Definition: TranslationUnit.h:137
souffle::ram::Program
RAM program relation declaration and functions.
Definition: Program.h:58
souffle::ram::TranslationUnit::getAnalysis
Analysis * getAnalysis() const
Get an analysis.
Definition: TranslationUnit.h:66
DebugReport.h
souffle::ram::TranslationUnit::getAliveAnalyses
std::set< const analysis::Analysis * > getAliveAnalyses() const
Get all alive analyses.
Definition: TranslationUnit.h:93
SymbolTable.h
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::ram::TranslationUnit::TranslationUnit
TranslationUnit(Own< Program > prog, SymbolTable sym, ErrorReport &e, DebugReport &d)
Definition: TranslationUnit.h:57
souffle::DebugReport::addSection
void addSection(DebugReportSection section)
Definition: DebugReport.h:93
souffle::ram::TranslationUnit::getSymbolTable
souffle::SymbolTable & getSymbolTable()
Obtain symbol table
Definition: TranslationUnit.h:112
Program.h
Global.h
souffle::ram::TranslationUnit::getProgram
Program & getProgram() const
Get the RAM Program of the translation unit
Definition: TranslationUnit.h:107
souffle::ram::TranslationUnit::symbolTable
souffle::SymbolTable symbolTable
Definition: TranslationUnit.h:134
souffle::ram::TranslationUnit::program
Own< Program > program
Definition: TranslationUnit.h:131
souffle::DebugReport
Class representing a HTML report, consisting of a list of sections.
Definition: DebugReport.h:87
souffle::ram::TranslationUnit::invalidateAnalyses
void invalidateAnalyses()
Invalidate all alive analyses of the translation unit.
Definition: TranslationUnit.h:102
souffle::SymbolTable
Definition: SymbolTable.h:48
souffle::ram::TranslationUnit::debugReport
DebugReport & debugReport
Definition: TranslationUnit.h:140
souffle::ram::TranslationUnit::analyses
std::map< std::string, Own< analysis::Analysis > > analyses
Definition: TranslationUnit.h:128
souffle::ram::TranslationUnit::~TranslationUnit
virtual ~TranslationUnit()=default
Analysis.h
souffle::Global::config
static MainConfig & config()
Definition: Global.h:141
souffle::ram::TranslationUnit::getErrorReport
ErrorReport & getErrorReport()
Obtain error report.
Definition: TranslationUnit.h:117
std
Definition: Brie.h:3053
souffle
Definition: AggregateOp.h:25
d
else d
Definition: htmlJsChartistMin.h:15
souffle::ram::TranslationUnit::getDebugReport
DebugReport & getDebugReport()
Obtain debug report.
Definition: TranslationUnit.h:122
souffle::ErrorReport
Definition: ErrorReport.h:152
ErrorReport.h