driver.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_TOOLCHAIN_DRIVER_DRIVER_H_
  5. #define CARBON_TOOLCHAIN_DRIVER_DRIVER_H_
  6. #include "common/command_line.h"
  7. #include "llvm/ADT/ArrayRef.h"
  8. #include "llvm/ADT/StringRef.h"
  9. #include "llvm/Support/VirtualFileSystem.h"
  10. #include "llvm/Support/raw_ostream.h"
  11. #include "toolchain/install/install_paths.h"
  12. namespace Carbon {
  13. // Command line interface driver.
  14. //
  15. // Provides simple API to parse and run command lines for Carbon. It is
  16. // generally expected to be used to implement command line tools for working
  17. // with the language.
  18. class Driver {
  19. public:
  20. // The result of RunCommand().
  21. struct RunResult {
  22. // Overall success result.
  23. bool success;
  24. // Per-file success results. May be empty if files aren't individually
  25. // processed.
  26. llvm::SmallVector<std::pair<std::string, bool>> per_file_success;
  27. };
  28. // Constructs a driver with any error or informational output directed to a
  29. // specified stream.
  30. Driver(llvm::vfs::FileSystem& fs, const InstallPaths* installation,
  31. llvm::raw_pwrite_stream& output_stream,
  32. llvm::raw_pwrite_stream& error_stream)
  33. : fs_(fs),
  34. installation_(installation),
  35. output_stream_(output_stream),
  36. error_stream_(error_stream) {}
  37. // Parses the given arguments into both a subcommand to select the operation
  38. // to perform and any arguments to that subcommand.
  39. //
  40. // Returns true if the operation succeeds. If the operation fails, returns
  41. // false and any information about the failure is printed to the registered
  42. // error stream (stderr by default).
  43. auto RunCommand(llvm::ArrayRef<llvm::StringRef> args) -> RunResult;
  44. // Finds the source files that define the prelude and returns a list of their
  45. // filenames. On error, writes a message to `error_stream` and returns an
  46. // empty list.
  47. static auto FindPreludeFiles(llvm::StringRef core_package_dir,
  48. llvm::raw_ostream& error_stream)
  49. -> llvm::SmallVector<std::string>;
  50. private:
  51. struct Options;
  52. struct CodegenOptions;
  53. struct CompileOptions;
  54. struct LinkOptions;
  55. class CompilationUnit;
  56. // Delegates to the command line library to parse the arguments and store the
  57. // results in a custom `Options` structure that the rest of the driver uses.
  58. auto ParseArgs(llvm::ArrayRef<llvm::StringRef> args, Options& options)
  59. -> CommandLine::ParseResult;
  60. // Does custom validation of the compile-subcommand options structure beyond
  61. // what the command line parsing library supports.
  62. auto ValidateCompileOptions(const CompileOptions& options) const -> bool;
  63. // Implements the compile subcommand of the driver.
  64. auto Compile(const CompileOptions& options,
  65. const CodegenOptions& codegen_options) -> RunResult;
  66. // Implements the link subcommand of the driver.
  67. auto Link(const LinkOptions& options, const CodegenOptions& codegen_options)
  68. -> RunResult;
  69. // The filesystem for source code.
  70. llvm::vfs::FileSystem& fs_;
  71. // Helper to locate the toolchain installation's files.
  72. const InstallPaths* installation_;
  73. // Standard output; stdout.
  74. llvm::raw_pwrite_stream& output_stream_;
  75. // Error output; stderr.
  76. llvm::raw_pwrite_stream& error_stream_;
  77. // For CARBON_VLOG.
  78. llvm::raw_pwrite_stream* vlog_stream_ = nullptr;
  79. };
  80. } // namespace Carbon
  81. #endif // CARBON_TOOLCHAIN_DRIVER_DRIVER_H_