driver.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. // Constructs a driver with any error or informational output directed to a
  20. // specified stream.
  21. Driver(llvm::vfs::FileSystem& fs, llvm::raw_pwrite_stream& output_stream,
  22. llvm::raw_pwrite_stream& error_stream)
  23. : fs_(fs), output_stream_(output_stream), error_stream_(error_stream) {
  24. (void)fs_;
  25. }
  26. // Parses the given arguments into both a subcommand to select the operation
  27. // to perform and any arguments to that subcommand.
  28. //
  29. // Returns true if the operation succeeds. If the operation fails, returns
  30. // false and any information about the failure is printed to the registered
  31. // error stream (stderr by default).
  32. auto RunCommand(llvm::ArrayRef<llvm::StringRef> args) -> bool;
  33. private:
  34. struct Options;
  35. struct CompileOptions;
  36. // Delegates to the command line library to parse the arguments and store the
  37. // results in a custom `Options` structure that the rest of the driver uses.
  38. auto ParseArgs(llvm::ArrayRef<llvm::StringRef> args, Options& options)
  39. -> CommandLine::ParseResult;
  40. // Does custom validation of the compile-subcommand options structure beyond
  41. // what the command line parsing library supports.
  42. auto ValidateCompileOptions(const CompileOptions& options) const -> bool;
  43. // Implements the compile subcommand of the driver.
  44. auto Compile(const CompileOptions& options) -> bool;
  45. llvm::vfs::FileSystem& fs_;
  46. llvm::raw_pwrite_stream& output_stream_;
  47. llvm::raw_pwrite_stream& error_stream_;
  48. llvm::raw_pwrite_stream* vlog_stream_ = nullptr;
  49. };
  50. } // namespace Carbon
  51. #endif // CARBON_TOOLCHAIN_DRIVER_DRIVER_H_