driver.h 3.4 KB

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