driver.h 2.5 KB

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