driver.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 <cstdint>
  7. #include "common/command_line.h"
  8. #include "llvm/ADT/ArrayRef.h"
  9. #include "llvm/ADT/StringRef.h"
  10. #include "llvm/Support/Debug.h"
  11. #include "llvm/Support/VirtualFileSystem.h"
  12. #include "llvm/Support/raw_ostream.h"
  13. #include "toolchain/diagnostics/diagnostic_emitter.h"
  14. namespace Carbon {
  15. // Command line interface driver.
  16. //
  17. // Provides simple API to parse and run command lines for Carbon. It is
  18. // generally expected to be used to implement command line tools for working
  19. // with the language.
  20. class Driver {
  21. public:
  22. // Constructs a driver with any error or informational output directed to a
  23. // specified stream.
  24. Driver(llvm::vfs::FileSystem& fs, llvm::raw_pwrite_stream& output_stream,
  25. llvm::raw_pwrite_stream& error_stream)
  26. : fs_(fs), output_stream_(output_stream), error_stream_(error_stream) {
  27. (void)fs_;
  28. }
  29. // Parses the given arguments into both a subcommand to select the operation
  30. // to perform and any arguments to that subcommand.
  31. //
  32. // Returns true if the operation succeeds. If the operation fails, returns
  33. // false and any information about the failure is printed to the registered
  34. // error stream (stderr by default).
  35. auto RunCommand(llvm::ArrayRef<llvm::StringRef> args) -> bool;
  36. private:
  37. struct Options;
  38. struct CompileOptions;
  39. // Delegates to the command line library to parse the arguments and store the
  40. // results in a custom `Options` structure that the rest of the driver uses.
  41. auto ParseArgs(llvm::ArrayRef<llvm::StringRef> args, Options& options)
  42. -> CommandLine::ParseResult;
  43. // Does custom validation of the compile-subcommand options structure beyond
  44. // what the command line parsing library supports.
  45. auto ValidateCompileOptions(const CompileOptions& options) const -> bool;
  46. // Implements the compile subcommand of the driver.
  47. auto Compile(const CompileOptions& options) -> bool;
  48. llvm::vfs::FileSystem& fs_;
  49. llvm::raw_pwrite_stream& output_stream_;
  50. llvm::raw_pwrite_stream& error_stream_;
  51. llvm::raw_pwrite_stream* vlog_stream_ = nullptr;
  52. };
  53. } // namespace Carbon
  54. #endif // CARBON_TOOLCHAIN_DRIVER_DRIVER_H_