driver.h 3.1 KB

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