driver.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. class CompilationUnit;
  37. // Delegates to the command line library to parse the arguments and store the
  38. // results in a custom `Options` structure that the rest of the driver uses.
  39. auto ParseArgs(llvm::ArrayRef<llvm::StringRef> args, Options& options)
  40. -> CommandLine::ParseResult;
  41. // Does custom validation of the compile-subcommand options structure beyond
  42. // what the command line parsing library supports.
  43. auto ValidateCompileOptions(const CompileOptions& options) const -> bool;
  44. // Implements the compile subcommand of the driver.
  45. auto Compile(const CompileOptions& options) -> bool;
  46. llvm::vfs::FileSystem& fs_;
  47. llvm::raw_pwrite_stream& output_stream_;
  48. llvm::raw_pwrite_stream& error_stream_;
  49. llvm::raw_pwrite_stream* vlog_stream_ = nullptr;
  50. };
  51. } // namespace Carbon
  52. #endif // CARBON_TOOLCHAIN_DRIVER_DRIVER_H_