OptParser.td 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. //===--- OptParser.td - Common Option Parsing Interfaces ------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines the common interfaces used by the option parsing TableGen
  10. // backend.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. // Define the kinds of options.
  14. class OptionKind<string name, int precedence = 0, bit sentinel = false> {
  15. string Name = name;
  16. // The kind precedence, kinds with lower precedence are matched first.
  17. int Precedence = precedence;
  18. // Indicate a sentinel option.
  19. bit Sentinel = sentinel;
  20. }
  21. // An option group.
  22. def KIND_GROUP : OptionKind<"Group">;
  23. // The input option kind.
  24. def KIND_INPUT : OptionKind<"Input", 1, true>;
  25. // The unknown option kind.
  26. def KIND_UNKNOWN : OptionKind<"Unknown", 2, true>;
  27. // A flag with no values.
  28. def KIND_FLAG : OptionKind<"Flag">;
  29. // An option which prefixes its (single) value.
  30. def KIND_JOINED : OptionKind<"Joined", 1>;
  31. // An option which is followed by its value.
  32. def KIND_SEPARATE : OptionKind<"Separate">;
  33. // An option followed by its values, which are separated by commas.
  34. def KIND_COMMAJOINED : OptionKind<"CommaJoined">;
  35. // An option which is which takes multiple (separate) arguments.
  36. def KIND_MULTIARG : OptionKind<"MultiArg">;
  37. // An option which is either joined to its (non-empty) value, or followed by its
  38. // value.
  39. def KIND_JOINED_OR_SEPARATE : OptionKind<"JoinedOrSeparate">;
  40. // An option which is both joined to its (first) value, and followed by its
  41. // (second) value.
  42. def KIND_JOINED_AND_SEPARATE : OptionKind<"JoinedAndSeparate">;
  43. // An option which consumes all remaining arguments if there are any.
  44. def KIND_REMAINING_ARGS : OptionKind<"RemainingArgs">;
  45. // An option which consumes an optional joined argument and any other remaining
  46. // arguments.
  47. def KIND_REMAINING_ARGS_JOINED : OptionKind<"RemainingArgsJoined">;
  48. // Define the option flags.
  49. class OptionFlag {}
  50. // HelpHidden - The option should not be displayed in --help, even if it has
  51. // help text. Clients *can* use this in conjunction with the OptTable::PrintHelp
  52. // arguments to implement hidden help groups.
  53. def HelpHidden : OptionFlag;
  54. // RenderAsInput - The option should not render the name when rendered as an
  55. // input (i.e., the option is rendered as values).
  56. def RenderAsInput : OptionFlag;
  57. // RenderJoined - The option should be rendered joined, even if separate (only
  58. // sensible on single value separate options).
  59. def RenderJoined : OptionFlag;
  60. // RenderSeparate - The option should be rendered separately, even if joined
  61. // (only sensible on joined options).
  62. def RenderSeparate : OptionFlag;
  63. // Define the option group class.
  64. class OptionGroup<string name> {
  65. string EnumName = ?; // Uses the def name if undefined.
  66. string Name = name;
  67. string HelpText = ?;
  68. OptionGroup Group = ?;
  69. list<OptionFlag> Flags = [];
  70. }
  71. // Define the option class.
  72. class Option<list<string> prefixes, string name, OptionKind kind> {
  73. string EnumName = ?; // Uses the def name if undefined.
  74. list<string> Prefixes = prefixes;
  75. string Name = name;
  76. OptionKind Kind = kind;
  77. // Used by MultiArg option kind.
  78. int NumArgs = 0;
  79. string HelpText = ?;
  80. string MetaVarName = ?;
  81. string Values = ?;
  82. code ValuesCode = ?;
  83. list<OptionFlag> Flags = [];
  84. OptionGroup Group = ?;
  85. Option Alias = ?;
  86. list<string> AliasArgs = [];
  87. code MacroPrefix = "";
  88. code KeyPath = ?;
  89. code DefaultValue = ?;
  90. code ImpliedValue = ?;
  91. code ImpliedCheck = "false";
  92. code ShouldParse = "true";
  93. bit ShouldAlwaysEmit = false;
  94. code NormalizerRetTy = ?;
  95. code NormalizedValuesScope = "";
  96. code Normalizer = "";
  97. code Denormalizer = "";
  98. code ValueMerger = "mergeForwardValue";
  99. code ValueExtractor = "extractForwardValue";
  100. list<code> NormalizedValues = ?;
  101. }
  102. // Helpers for defining options.
  103. class Flag<list<string> prefixes, string name>
  104. : Option<prefixes, name, KIND_FLAG>;
  105. class Joined<list<string> prefixes, string name>
  106. : Option<prefixes, name, KIND_JOINED>;
  107. class Separate<list<string> prefixes, string name>
  108. : Option<prefixes, name, KIND_SEPARATE>;
  109. class CommaJoined<list<string> prefixes, string name>
  110. : Option<prefixes, name, KIND_COMMAJOINED>;
  111. class MultiArg<list<string> prefixes, string name, int numargs>
  112. : Option<prefixes, name, KIND_MULTIARG> {
  113. int NumArgs = numargs;
  114. }
  115. class JoinedOrSeparate<list<string> prefixes, string name>
  116. : Option<prefixes, name, KIND_JOINED_OR_SEPARATE>;
  117. class JoinedAndSeparate<list<string> prefixes, string name>
  118. : Option<prefixes, name, KIND_JOINED_AND_SEPARATE>;
  119. // Mix-ins for adding optional attributes.
  120. class Alias<Option alias> { Option Alias = alias; }
  121. class AliasArgs<list<string> aliasargs> { list<string> AliasArgs = aliasargs; }
  122. class EnumName<string name> { string EnumName = name; }
  123. class Flags<list<OptionFlag> flags> { list<OptionFlag> Flags = flags; }
  124. class Group<OptionGroup group> { OptionGroup Group = group; }
  125. class HelpText<string text> { string HelpText = text; }
  126. class MetaVarName<string name> { string MetaVarName = name; }
  127. class Values<string value> { string Values = value; }
  128. class ValuesCode<code valuecode> { code ValuesCode = valuecode; }
  129. // Helpers for defining marshalling information (typically used in Clang's -cc1
  130. // frontend).
  131. // The key path to the mapped field and the macro prefix for the resulting
  132. // definition database.
  133. class KeyPathAndMacro<string key_path_prefix, string key_path_base,
  134. string macro_prefix = ""> {
  135. code KeyPath = !strconcat(key_path_prefix, key_path_base);
  136. code MacroPrefix = macro_prefix;
  137. }
  138. // Mixin that implies the specified value for the current option when any of the
  139. // given key paths evaluates to true.
  140. class ImpliedByAnyOf<list<string> key_paths, code value = "true"> {
  141. code ImpliedCheck = !foldl("false", key_paths, accumulator, key_path,
  142. !strconcat(accumulator, " || ", key_path));
  143. code ImpliedValue = value;
  144. }
  145. // Parent class for marshalled options (typically used in Clang's -cc1 frontend).
  146. class MarshallingInfo<KeyPathAndMacro kpm, code defaultvalue> {
  147. code KeyPath = kpm.KeyPath;
  148. code MacroPrefix = kpm.MacroPrefix;
  149. code DefaultValue = defaultvalue;
  150. }
  151. // Marshalled option accepting a string argument.
  152. class MarshallingInfoString<KeyPathAndMacro kpm, code defaultvalue="std::string()">
  153. : MarshallingInfo<kpm, defaultvalue> {
  154. code Normalizer = "normalizeString";
  155. code Denormalizer = "denormalizeString";
  156. }
  157. // Marshalled option accepting an integer argument.
  158. class MarshallingInfoInt<KeyPathAndMacro kpm, code defaultvalue="0", code type="unsigned">
  159. : MarshallingInfo<kpm, defaultvalue> {
  160. code Normalizer = "normalizeStringIntegral<"#type#">";
  161. code Denormalizer = "denormalizeString<"#type#">";
  162. }
  163. // Marshalled option accepting vector of strings.
  164. class MarshallingInfoStringVector<KeyPathAndMacro kpm>
  165. : MarshallingInfo<kpm, "std::vector<std::string>({})"> {
  166. code Normalizer = "normalizeStringVector";
  167. code Denormalizer = "denormalizeStringVector";
  168. }
  169. // Marshalled option - single positive flag.
  170. class MarshallingInfoFlag<KeyPathAndMacro kpm, code defaultvalue = "false">
  171. : MarshallingInfo<kpm, defaultvalue> {
  172. code Normalizer = "normalizeSimpleFlag";
  173. code Denormalizer = "denormalizeSimpleFlag";
  174. }
  175. // Marshalled option - single negative flag.
  176. class MarshallingInfoNegativeFlag<KeyPathAndMacro kpm, code defaultvalue = "true">
  177. : MarshallingInfo<kpm, defaultvalue> {
  178. code Normalizer = "normalizeSimpleNegativeFlag";
  179. code Denormalizer = "denormalizeSimpleFlag";
  180. }
  181. // Marshalled option - single flag contributing to a bitfield.
  182. class MarshallingInfoBitfieldFlag<KeyPathAndMacro kpm, code value>
  183. : MarshallingInfoFlag<kpm, "0u"> {
  184. code Normalizer = "makeFlagToValueNormalizer("#value#")";
  185. code ValueMerger = "mergeMaskValue";
  186. code ValueExtractor = "(extractMaskValue<unsigned, decltype("#value#"), "#value#">)";
  187. }
  188. // Implementation detail of BoolOption.
  189. class MarshallingInfoBooleanFlag<KeyPathAndMacro kpm, code defaultvalue, code value,
  190. code other_value, code other_name>
  191. : MarshallingInfoFlag<kpm, defaultvalue> {
  192. code Normalizer = "makeBooleanOptionNormalizer("#value#", "#other_value#", OPT_"#other_name#")";
  193. code Denormalizer = "makeBooleanOptionDenormalizer("#value#")";
  194. }
  195. // Marshalled option accepting any of the specified enum values.
  196. // Typically used with `Values`, `NormalizedValues` and `NormalizedValuesScope`.
  197. class MarshallingInfoEnum<KeyPathAndMacro kpm, code defaultvalue>
  198. : MarshallingInfo<kpm, defaultvalue> {
  199. code Normalizer = "normalizeSimpleEnum";
  200. code Denormalizer = "denormalizeSimpleEnum";
  201. }
  202. // Mixins for additional marshalling attributes.
  203. class ShouldParseIf<code condition> { code ShouldParse = condition; }
  204. class AlwaysEmit { bit ShouldAlwaysEmit = true; }
  205. class Normalizer<code normalizer> { code Normalizer = normalizer; }
  206. class Denormalizer<code denormalizer> { code Denormalizer = denormalizer; }
  207. class NormalizedValuesScope<code scope> { code NormalizedValuesScope = scope; }
  208. class NormalizedValues<list<code> definitions> { list<code> NormalizedValues = definitions; }
  209. class ValueMerger<code merger> { code ValueMerger = merger; }
  210. class ValueExtractor<code extractor> { code ValueExtractor = extractor; }
  211. // Predefined options.
  212. // FIXME: Have generator validate that these appear in correct position (and
  213. // aren't duplicated).
  214. def INPUT : Option<[], "<input>", KIND_INPUT>;
  215. def UNKNOWN : Option<[], "<unknown>", KIND_UNKNOWN>;