Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
5.4 KiB

  1. //===--- Option.h - Abstract Driver Options ---------------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef LLVM_OPTION_OPTION_H
  10. #define LLVM_OPTION_OPTION_H
  11. #include "llvm/ADT/SmallVector.h"
  12. #include "llvm/ADT/StringRef.h"
  13. #include "llvm/Option/OptTable.h"
  14. #include "llvm/Support/ErrorHandling.h"
  15. namespace llvm {
  16. namespace opt {
  17. class Arg;
  18. class ArgList;
  19. /// ArgStringList - Type used for constructing argv lists for subprocesses.
  20. typedef SmallVector<const char*, 16> ArgStringList;
  21. /// Base flags for all options. Custom flags may be added after.
  22. enum DriverFlag {
  23. HelpHidden = (1 << 0),
  24. RenderAsInput = (1 << 1),
  25. RenderJoined = (1 << 2),
  26. RenderSeparate = (1 << 3)
  27. };
  28. /// Option - Abstract representation for a single form of driver
  29. /// argument.
  30. ///
  31. /// An Option class represents a form of option that the driver
  32. /// takes, for example how many arguments the option has and how
  33. /// they can be provided. Individual option instances store
  34. /// additional information about what group the option is a member
  35. /// of (if any), if the option is an alias, and a number of
  36. /// flags. At runtime the driver parses the command line into
  37. /// concrete Arg instances, each of which corresponds to a
  38. /// particular Option instance.
  39. class Option {
  40. public:
  41. enum OptionClass {
  42. GroupClass = 0,
  43. InputClass,
  44. UnknownClass,
  45. FlagClass,
  46. JoinedClass,
  47. SeparateClass,
  48. CommaJoinedClass,
  49. MultiArgClass,
  50. JoinedOrSeparateClass,
  51. JoinedAndSeparateClass
  52. };
  53. enum RenderStyleKind {
  54. RenderCommaJoinedStyle,
  55. RenderJoinedStyle,
  56. RenderSeparateStyle,
  57. RenderValuesStyle
  58. };
  59. protected:
  60. const OptTable::Info *Info;
  61. const OptTable *Owner;
  62. public:
  63. Option(const OptTable::Info *Info, const OptTable *Owner);
  64. ~Option();
  65. bool isValid() const {
  66. return Info != 0;
  67. }
  68. unsigned getID() const {
  69. assert(Info && "Must have a valid info!");
  70. return Info->ID;
  71. }
  72. OptionClass getKind() const {
  73. assert(Info && "Must have a valid info!");
  74. return OptionClass(Info->Kind);
  75. }
  76. /// \brief Get the name of this option without any prefix.
  77. StringRef getName() const {
  78. assert(Info && "Must have a valid info!");
  79. return Info->Name;
  80. }
  81. const Option getGroup() const {
  82. assert(Info && "Must have a valid info!");
  83. assert(Owner && "Must have a valid owner!");
  84. return Owner->getOption(Info->GroupID);
  85. }
  86. const Option getAlias() const {
  87. assert(Info && "Must have a valid info!");
  88. assert(Owner && "Must have a valid owner!");
  89. return Owner->getOption(Info->AliasID);
  90. }
  91. /// \brief Get the default prefix for this option.
  92. StringRef getPrefix() const {
  93. const char *Prefix = *Info->Prefixes;
  94. return Prefix ? Prefix : StringRef();
  95. }
  96. /// \brief Get the name of this option with the default prefix.
  97. std::string getPrefixedName() const {
  98. std::string Ret = getPrefix();
  99. Ret += getName();
  100. return Ret;
  101. }
  102. unsigned getNumArgs() const { return Info->Param; }
  103. bool hasNoOptAsInput() const { return Info->Flags & RenderAsInput;}
  104. RenderStyleKind getRenderStyle() const {
  105. if (Info->Flags & RenderJoined)
  106. return RenderJoinedStyle;
  107. if (Info->Flags & RenderSeparate)
  108. return RenderSeparateStyle;
  109. switch (getKind()) {
  110. case GroupClass:
  111. case InputClass:
  112. case UnknownClass:
  113. return RenderValuesStyle;
  114. case JoinedClass:
  115. case JoinedAndSeparateClass:
  116. return RenderJoinedStyle;
  117. case CommaJoinedClass:
  118. return RenderCommaJoinedStyle;
  119. case FlagClass:
  120. case SeparateClass:
  121. case MultiArgClass:
  122. case JoinedOrSeparateClass:
  123. return RenderSeparateStyle;
  124. }
  125. llvm_unreachable("Unexpected kind!");
  126. }
  127. /// Test if this option has the flag \a Val.
  128. bool hasFlag(unsigned Val) const {
  129. return Info->Flags & Val;
  130. }
  131. /// getUnaliasedOption - Return the final option this option
  132. /// aliases (itself, if the option has no alias).
  133. const Option getUnaliasedOption() const {
  134. const Option Alias = getAlias();
  135. if (Alias.isValid()) return Alias.getUnaliasedOption();
  136. return *this;
  137. }
  138. /// getRenderName - Return the name to use when rendering this
  139. /// option.
  140. StringRef getRenderName() const {
  141. return getUnaliasedOption().getName();
  142. }
  143. /// matches - Predicate for whether this option is part of the
  144. /// given option (which may be a group).
  145. ///
  146. /// Note that matches against options which are an alias should never be
  147. /// done -- aliases do not participate in matching and so such a query will
  148. /// always be false.
  149. bool matches(OptSpecifier ID) const;
  150. /// accept - Potentially accept the current argument, returning a
  151. /// new Arg instance, or 0 if the option does not accept this
  152. /// argument (or the argument is missing values).
  153. ///
  154. /// If the option accepts the current argument, accept() sets
  155. /// Index to the position where argument parsing should resume
  156. /// (even if the argument is missing values).
  157. ///
  158. /// \parm ArgSize The number of bytes taken up by the matched Option prefix
  159. /// and name. This is used to determine where joined values
  160. /// start.
  161. Arg *accept(const ArgList &Args, unsigned &Index, unsigned ArgSize) const;
  162. void dump() const;
  163. };
  164. } // end namespace opt
  165. } // end namespace llvm
  166. #endif