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