codon/compiler/sir/analyze/analysis.h

49 lines
1.1 KiB
C++

#pragma once
#include <memory>
#include "sir/module.h"
#include "sir/transform/pass.h"
namespace codon {
namespace ir {
namespace analyze {
/// Analysis result base struct.
struct Result {
virtual ~Result() noexcept = default;
};
/// Base class for IR analyses.
class Analysis {
private:
transform::PassManager *manager = nullptr;
public:
virtual ~Analysis() noexcept = default;
/// @return a unique key for this pass
virtual std::string getKey() const = 0;
/// Execute the analysis.
/// @param module the module
virtual std::unique_ptr<Result> run(const Module *module) = 0;
/// Sets the manager.
/// @param mng the new manager
void setManager(transform::PassManager *mng) { manager = mng; }
/// Returns the result of a given analysis.
/// @param key the analysis key
template <typename AnalysisType>
const AnalysisType *getAnalysisResult(const std::string &key) {
return static_cast<const AnalysisType *>(doGetAnalysis(key));
}
private:
const analyze::Result *doGetAnalysis(const std::string &key);
};
} // namespace analyze
} // namespace ir
} // namespace codon