12 #include <string_view>
13 #include <unordered_map>
14 #include <unordered_set>
18 #include <hilti/rt/filesystem.h>
20 #include <hilti/base/logger.h>
21 #include <hilti/base/result.h>
22 #include <hilti/base/timing.h>
23 #include <hilti/compiler/context.h>
24 #include <hilti/compiler/unit.h>
34 enum class Dependencies {
62 std::vector<hilti::rt::filesystem::path>
65 std::unique_ptr<Logger>
91 explicit Driver(std::string name);
100 Driver(std::string name,
const hilti::rt::filesystem::path& argv0);
128 void registerUnit(
const std::shared_ptr<Unit>& unit) { _addUnit(unit); }
137 auto i = _units.find(uid);
138 return i != _units.end() ? i->second.get() :
nullptr;
180 bool hasInputs()
const {
return ! (_units.empty() && _libraries.empty() && _external_cxxs.empty()); }
229 auto*
builder()
const {
return _builder.get(); }
232 const auto&
options()
const {
return _ctx->options(); }
382 const std::string& extension =
"tmp");
476 enum Stage { UNINITIALIZED, INITIALIZED, COMPILED, CODEGENED, LINKED, JITTED } _stage = UNINITIALIZED;
479 void _addUnit(
const std::shared_ptr<Unit>& unit);
482 Result<Nothing> _resolveUnitsWithPlugin(
const Plugin& plugin, std::vector<std::shared_ptr<Unit>> units,
int& round);
485 Result<Nothing> _transformUnitsWithPlugin(
const Plugin& plugin,
const std::vector<std::shared_ptr<Unit>>& units);
488 Result<Nothing> _resolveUnits();
491 Result<Nothing> _codegenUnits();
494 Result<Nothing> _optimizeUnits();
497 void _dumpAST(
const std::shared_ptr<Unit>& unit,
const logging::DebugStream& stream,
const Plugin& plugin,
498 const std::string& prefix,
int round);
501 void _dumpAST(
const std::shared_ptr<Unit>& unit,
const logging::DebugStream& stream,
const std::string& prefix);
504 void _dumpAST(
const std::shared_ptr<Unit>& unit, std::ostream& stream,
const Plugin& plugin,
505 const std::string& prefix,
int round);
508 void _dumpDeclarations(
const std::shared_ptr<Unit>& unit,
const Plugin& plugin);
511 void _saveIterationAST(
const std::shared_ptr<Unit>& unit,
const Plugin& plugin,
const std::string& prefix,
515 void _saveIterationAST(
const std::shared_ptr<Unit>& unit,
const Plugin& plugin,
const std::string& prefix,
516 const std::string& tag);
519 Result<Nothing> _setCxxNamespacesFromPrefix(
const char* prefix);
527 static Result<void*> _symbol(
const std::string& symbol);
530 driver::Options _driver_options;
533 std::shared_ptr<Context> _ctx;
534 std::unique_ptr<hilti::Builder> _builder;
535 std::unique_ptr<hilti::JIT> _jit;
536 std::shared_ptr<const hilti::rt::Library> _library;
538 std::map<declaration::module::UID, std::shared_ptr<Unit>> _units;
539 std::vector<CxxCode> _generated_cxxs;
540 std::unordered_map<std::string, Library> _libraries;
541 std::vector<hilti::rt::filesystem::path> _external_cxxs;
542 std::vector<linker::MetaData> _mds;
544 std::unordered_set<std::string> _processed_paths;
546 bool _runtime_initialized =
false;
547 std::set<std::string> _tmp_files;
Definition: ast-context.h:121
Definition: ast-context.h:494
Result< Nothing > compileUnits()
Definition: driver.cc:681
result::Error error(std::string_view msg, const hilti::rt::filesystem::path &p="")
Definition: driver.cc:143
Result< Nothing > linkUnits()
Definition: driver.cc:801
result::Error augmentError(const result::Error &err, const hilti::rt::filesystem::path &p="")
Definition: driver.cc:152
virtual std::string hookAugmentUsage()
Definition: driver.h:417
virtual void hookNewASTPreCompilation(const Plugin &plugin, ASTRoot *root)
Definition: driver.h:436
void printHiltiException(const hilti::rt::Exception &e)
Definition: driver.cc:932
Result< std::stringstream > readInput(const hilti::rt::filesystem::path &p)
Definition: driver.cc:184
const auto & driverOptions() const
Definition: driver.h:183
bool hasInputs() const
Definition: driver.h:180
virtual void hookAddInput(std::shared_ptr< Unit > unit)
Definition: driver.h:423
virtual bool hookProcessCommandLineOption(int opt, const char *optarg)
Definition: driver.h:411
virtual Result< Nothing > hookCompilationFinished(ASTRoot *root)
Definition: driver.h:459
Result< Nothing > finishRuntime()
Definition: driver.cc:1015
Result< Nothing > codegenUnits()
Result< Nothing > outputUnits()
Definition: driver.cc:840
const auto & options() const
Definition: driver.h:232
const auto & hiltiOptions() const
Definition: driver.h:186
Result< Nothing > jitUnits()
Definition: driver.cc:898
Result< Nothing > compile()
Definition: driver.cc:756
Result< Nothing > executeMain()
Definition: driver.cc:986
void dumpUnit(const Unit &unit)
Definition: driver.cc:229
Result< std::ofstream > openOutput(const hilti::rt::filesystem::path &p, bool binary=false, bool append=false)
Definition: driver.cc:156
void usage()
Definition: driver.cc:87
virtual void hookAddInput(const hilti::rt::filesystem::path &path)
Definition: driver.h:429
void updateProcessExtension(const declaration::module::UID &uid, const hilti::rt::filesystem::path &ext)
Definition: driver.cc:507
virtual void hookInitRuntime()
Definition: driver.h:465
const auto & context() const
Definition: driver.h:223
virtual std::unique_ptr< Builder > createBuilder(ASTContext *ctx) const
Definition: driver.cc:951
Result< Nothing > run()
Definition: driver.cc:719
Result< Nothing > addInput(const hilti::rt::filesystem::path &path)
Definition: driver.cc:560
Result< Nothing > openInput(std::ifstream &in, const hilti::rt::filesystem::path &p)
Definition: driver.cc:175
virtual std::string hookAddCommandLineOptions()
Definition: driver.h:408
Unit * lookupUnit(const declaration::module::UID &uid) const
Definition: driver.h:136
auto * builder() const
Definition: driver.h:229
void setDriverOptions(driver::Options options)
Definition: driver.cc:500
Result< hilti::rt::filesystem::path > writeToTemp(std::ifstream &in, const std::string &name_hint, const std::string &extension="tmp")
Definition: driver.cc:209
Result< Nothing > initialize()
Definition: driver.cc:470
void setCompilerOptions(hilti::Options options)
Definition: driver.cc:493
Result< Nothing > writeOutput(std::ifstream &in, const hilti::rt::filesystem::path &p)
Definition: driver.cc:197
Result< Nothing > parseOptions(int argc, char **argv)
Definition: driver.cc:270
virtual bool hookNewASTPostCompilation(const Plugin &plugin, ASTRoot *root)
Definition: driver.h:453
void registerUnit(const std::shared_ptr< Unit > &unit)
Definition: driver.h:128
Result< Nothing > initRuntime()
Definition: driver.cc:953
virtual void hookFinishRuntime()
Definition: driver.h:471
Definition: exception.h:22
Definition: module-uid.h:20
bool enable_profiling
Definition: driver.h:61
bool report_resource_usage
Definition: driver.h:58
bool report_times
Definition: driver.h:59
bool execute_code
Definition: driver.h:53
bool output_linker
Definition: driver.h:51
bool output_cxx
Definition: driver.h:49
std::vector< hilti::rt::filesystem::path > inputs
Definition: driver.h:63
bool show_backtraces
Definition: driver.h:54
hilti::rt::filesystem::path output_path
Definition: driver.h:64
bool output_hilti
Definition: driver.h:47
bool dump_code
Definition: driver.h:60
std::unique_ptr< Logger > logger
Definition: driver.h:66
bool keep_tmps
Definition: driver.h:56
std::string output_cxx_prefix
Definition: driver.h:50
bool include_linker
Definition: driver.h:46
bool abort_on_exceptions
Definition: driver.h:55
Dependencies output_dependencies
Definition: driver.h:52
bool output_prototypes
Definition: driver.h:48
bool skip_dependencies
Definition: driver.h:57