driver.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 "toolchain/driver/driver_env.h"
  10. namespace Carbon {
  11. // Command line interface driver.
  12. //
  13. // Provides simple API to parse and run command lines for Carbon. It is
  14. // generally expected to be used to implement command line tools for working
  15. // with the language.
  16. class Driver {
  17. public:
  18. // The result of RunCommand().
  19. struct RunResult {
  20. // Overall success result.
  21. bool success;
  22. // Per-file success results. May be empty if files aren't individually
  23. // processed.
  24. llvm::SmallVector<std::pair<std::string, bool>> per_file_success;
  25. };
  26. // Constructs a driver with any error or informational output directed to a
  27. // specified stream.
  28. Driver(llvm::vfs::FileSystem& fs, const InstallPaths* installation,
  29. llvm::raw_pwrite_stream& output_stream,
  30. llvm::raw_pwrite_stream& error_stream)
  31. : driver_env_{.fs = fs,
  32. .installation = installation,
  33. .output_stream = output_stream,
  34. .error_stream = error_stream} {}
  35. // Parses the given arguments into both a subcommand to select the operation
  36. // to perform and any arguments to that subcommand.
  37. //
  38. // Returns true if the operation succeeds. If the operation fails, returns
  39. // false and any information about the failure is printed to the registered
  40. // error stream (stderr by default).
  41. auto RunCommand(llvm::ArrayRef<llvm::StringRef> args) -> RunResult;
  42. private:
  43. struct Options;
  44. struct CodegenOptions;
  45. struct CompileOptions;
  46. struct LinkOptions;
  47. class CompilationUnit;
  48. // Delegates to the command line library to parse the arguments and store the
  49. // results in a custom `Options` structure that the rest of the driver uses.
  50. auto ParseArgs(llvm::ArrayRef<llvm::StringRef> args, Options& options)
  51. -> CommandLine::ParseResult;
  52. // Does custom validation of the compile-subcommand options structure beyond
  53. // what the command line parsing library supports.
  54. auto ValidateCompileOptions(const CompileOptions& options) const -> bool;
  55. // Implements the compile subcommand of the driver.
  56. auto Compile(const CompileOptions& options,
  57. const CodegenOptions& codegen_options) -> RunResult;
  58. // Implements the link subcommand of the driver.
  59. auto Link(const LinkOptions& options, const CodegenOptions& codegen_options)
  60. -> RunResult;
  61. DriverEnv driver_env_;
  62. };
  63. } // namespace Carbon
  64. #endif // CARBON_TOOLCHAIN_DRIVER_DRIVER_H_