Warning, /include/llvm/Option/OptParser.td is written in an unsupported language. File is not indexed.
0001 //===--- OptParser.td - Common Option Parsing Interfaces ------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines the common interfaces used by the option parsing TableGen
0010 // backend.
0011 //
0012 //===----------------------------------------------------------------------===//
0013
0014 #ifndef LLVM_OPTION_OPTPARSER_TD
0015 #define LLVM_OPTION_OPTPARSER_TD
0016
0017 // Define the kinds of options.
0018
0019 class OptionKind<string name, int precedence = 0, bit sentinel = false> {
0020 string Name = name;
0021 // The kind precedence, kinds with lower precedence are matched first.
0022 int Precedence = precedence;
0023 // Indicate a sentinel option.
0024 bit Sentinel = sentinel;
0025 }
0026
0027 // An option group.
0028 def KIND_GROUP : OptionKind<"Group">;
0029 // The input option kind.
0030 def KIND_INPUT : OptionKind<"Input", 1, true>;
0031 // The unknown option kind.
0032 def KIND_UNKNOWN : OptionKind<"Unknown", 2, true>;
0033 // A flag with no values.
0034 def KIND_FLAG : OptionKind<"Flag">;
0035 // An option which prefixes its (single) value.
0036 def KIND_JOINED : OptionKind<"Joined", 1>;
0037 // An option which is followed by its value.
0038 def KIND_SEPARATE : OptionKind<"Separate">;
0039 // An option followed by its values, which are separated by commas.
0040 def KIND_COMMAJOINED : OptionKind<"CommaJoined">;
0041 // An option which is which takes multiple (separate) arguments.
0042 def KIND_MULTIARG : OptionKind<"MultiArg">;
0043 // An option which is either joined to its (non-empty) value, or followed by its
0044 // value.
0045 def KIND_JOINED_OR_SEPARATE : OptionKind<"JoinedOrSeparate">;
0046 // An option which is both joined to its (first) value, and followed by its
0047 // (second) value.
0048 def KIND_JOINED_AND_SEPARATE : OptionKind<"JoinedAndSeparate">;
0049 // An option which consumes all remaining arguments if there are any.
0050 def KIND_REMAINING_ARGS : OptionKind<"RemainingArgs">;
0051 // An option which consumes an optional joined argument and any other remaining
0052 // arguments.
0053 def KIND_REMAINING_ARGS_JOINED : OptionKind<"RemainingArgsJoined">;
0054
0055 // Define the option flags.
0056
0057 class OptionFlag {}
0058
0059 // HelpHidden - The option should not be displayed in --help, even if it has
0060 // help text. Clients *can* use this in conjunction with the OptTable::PrintHelp
0061 // arguments to implement hidden help groups.
0062 def HelpHidden : OptionFlag;
0063
0064 // RenderAsInput - The option should not render the name when rendered as an
0065 // input (i.e., the option is rendered as values).
0066 def RenderAsInput : OptionFlag;
0067
0068 // RenderJoined - The option should be rendered joined, even if separate (only
0069 // sensible on single value separate options).
0070 def RenderJoined : OptionFlag;
0071
0072 // RenderSeparate - The option should be rendered separately, even if joined
0073 // (only sensible on joined options).
0074 def RenderSeparate : OptionFlag;
0075
0076 // Define Visibility categories
0077
0078 class OptionVisibility {}
0079
0080 // Explicit specifier for default visibility
0081 def DefaultVis : OptionVisibility;
0082
0083 // Define the option group class.
0084
0085 class OptionGroup<string name> {
0086 string EnumName = ?; // Uses the def name if undefined.
0087 string Name = name;
0088 string HelpText = ?;
0089 OptionGroup Group = ?;
0090 list<OptionFlag> Flags = [];
0091 list<OptionVisibility> Visibility = [];
0092 }
0093
0094 // Define the option class.
0095
0096 class HelpTextVariant<list<OptionVisibility> visibilities, string text> {
0097 list<OptionVisibility> Visibilities = visibilities;
0098 string Text = text;
0099 }
0100
0101 class Option<list<string> prefixes, string name, OptionKind kind> {
0102 string EnumName = ?; // Uses the def name if undefined.
0103 list<string> Prefixes = prefixes;
0104 string Name = name;
0105 OptionKind Kind = kind;
0106 // Used by MultiArg option kind.
0107 int NumArgs = 0;
0108 string HelpText = ?;
0109 list<HelpTextVariant> HelpTextsForVariants = [];
0110 string MetaVarName = ?;
0111 string Values = ?;
0112 code ValuesCode = ?;
0113 list<OptionFlag> Flags = [];
0114 list<OptionVisibility> Visibility = [DefaultVis];
0115 OptionGroup Group = ?;
0116 Option Alias = ?;
0117 list<string> AliasArgs = [];
0118 code MacroPrefix = "";
0119 code KeyPath = ?;
0120 code DefaultValue = ?;
0121 code ImpliedValue = ?;
0122 code ImpliedCheck = "false";
0123 code ShouldParse = "true";
0124 bit ShouldAlwaysEmit = false;
0125 code NormalizerRetTy = ?;
0126 code NormalizedValuesScope = "";
0127 code Normalizer = "";
0128 code Denormalizer = "";
0129 code ValueMerger = "mergeForwardValue";
0130 code ValueExtractor = "extractForwardValue";
0131 list<code> NormalizedValues = ?;
0132 }
0133
0134 // Helpers for defining options.
0135
0136 class Flag<list<string> prefixes, string name>
0137 : Option<prefixes, name, KIND_FLAG>;
0138 class Joined<list<string> prefixes, string name>
0139 : Option<prefixes, name, KIND_JOINED>;
0140 class Separate<list<string> prefixes, string name>
0141 : Option<prefixes, name, KIND_SEPARATE>;
0142 class CommaJoined<list<string> prefixes, string name>
0143 : Option<prefixes, name, KIND_COMMAJOINED>;
0144 class MultiArg<list<string> prefixes, string name, int numargs>
0145 : Option<prefixes, name, KIND_MULTIARG> {
0146 int NumArgs = numargs;
0147 }
0148 class JoinedOrSeparate<list<string> prefixes, string name>
0149 : Option<prefixes, name, KIND_JOINED_OR_SEPARATE>;
0150 class JoinedAndSeparate<list<string> prefixes, string name>
0151 : Option<prefixes, name, KIND_JOINED_AND_SEPARATE>;
0152
0153 // Mix-ins for adding optional attributes.
0154
0155 class Alias<Option alias> { Option Alias = alias; }
0156 class AliasArgs<list<string> aliasargs> { list<string> AliasArgs = aliasargs; }
0157 class EnumName<string name> { string EnumName = name; }
0158 class Flags<list<OptionFlag> flags> { list<OptionFlag> Flags = flags; }
0159 class Visibility<list<OptionVisibility> visibility> {
0160 list<OptionVisibility> Visibility = visibility;
0161 }
0162 class Group<OptionGroup group> { OptionGroup Group = group; }
0163 class HelpText<string text> { string HelpText = text; }
0164 class HelpTextForVariants<list<OptionVisibility> Visibilities, string text> {
0165 list<HelpTextVariant> HelpTextsForVariants = [
0166 HelpTextVariant<Visibilities, text>
0167 ];
0168 }
0169
0170 class MetaVarName<string name> { string MetaVarName = name; }
0171 class Values<string value> { string Values = value; }
0172 class ValuesCode<code valuecode> { code ValuesCode = valuecode; }
0173
0174 // Helpers for defining marshalling information (typically used in Clang's -cc1
0175 // frontend).
0176
0177 // The key path to the mapped field and the macro prefix for the resulting
0178 // definition database.
0179 class KeyPathAndMacro<string key_path_prefix, string key_path_base,
0180 string macro_prefix = ""> {
0181 code KeyPath = !strconcat(key_path_prefix, key_path_base);
0182 code MacroPrefix = macro_prefix;
0183 }
0184
0185 // Mixin that implies the specified value for the current option when any of the
0186 // given key paths evaluates to true.
0187 class ImpliedByAnyOf<list<string> key_paths, code value = "true"> {
0188 code ImpliedCheck = !foldl("false", key_paths, accumulator, key_path,
0189 !strconcat(accumulator, " || ", key_path));
0190 code ImpliedValue = value;
0191 }
0192
0193 // Parent class for marshalled options (typically used in Clang's -cc1 frontend).
0194 class MarshallingInfo<KeyPathAndMacro kpm, code defaultvalue> {
0195 code KeyPath = kpm.KeyPath;
0196 code MacroPrefix = kpm.MacroPrefix;
0197 code DefaultValue = defaultvalue;
0198 }
0199
0200 // Marshalled option accepting a string argument.
0201 class MarshallingInfoString<KeyPathAndMacro kpm, code defaultvalue="std::string()">
0202 : MarshallingInfo<kpm, defaultvalue> {
0203 code Normalizer = "normalizeString";
0204 code Denormalizer = "denormalizeString";
0205 }
0206
0207 // Marshalled option accepting an integer argument.
0208 class MarshallingInfoInt<KeyPathAndMacro kpm, code defaultvalue="0", code type="unsigned">
0209 : MarshallingInfo<kpm, defaultvalue> {
0210 code Normalizer = "normalizeStringIntegral<"#type#">";
0211 code Denormalizer = "denormalizeString<"#type#">";
0212 }
0213
0214 // Marshalled option accepting vector of strings.
0215 class MarshallingInfoStringVector<KeyPathAndMacro kpm>
0216 : MarshallingInfo<kpm, "std::vector<std::string>({})"> {
0217 code Normalizer = "normalizeStringVector";
0218 code Denormalizer = "denormalizeStringVector";
0219 }
0220
0221 // Marshalled option - single positive flag.
0222 class MarshallingInfoFlag<KeyPathAndMacro kpm, code defaultvalue = "false">
0223 : MarshallingInfo<kpm, defaultvalue> {
0224 code Normalizer = "normalizeSimpleFlag";
0225 code Denormalizer = "denormalizeSimpleFlag";
0226 }
0227
0228 // Marshalled option - single negative flag.
0229 class MarshallingInfoNegativeFlag<KeyPathAndMacro kpm, code defaultvalue = "true">
0230 : MarshallingInfo<kpm, defaultvalue> {
0231 code Normalizer = "normalizeSimpleNegativeFlag";
0232 code Denormalizer = "denormalizeSimpleFlag";
0233 }
0234
0235 // Marshalled option - single flag contributing to a bitfield.
0236 class MarshallingInfoBitfieldFlag<KeyPathAndMacro kpm, code value>
0237 : MarshallingInfoFlag<kpm, "0u"> {
0238 code Normalizer = "makeFlagToValueNormalizer("#value#")";
0239 code ValueMerger = "mergeMaskValue";
0240 code ValueExtractor = "(extractMaskValue<unsigned, decltype("#value#"), "#value#">)";
0241 }
0242
0243 // Implementation detail of BoolOption.
0244 class MarshallingInfoBooleanFlag<KeyPathAndMacro kpm, code defaultvalue, code value,
0245 code other_value, code other_name>
0246 : MarshallingInfoFlag<kpm, defaultvalue> {
0247 code Normalizer = "makeBooleanOptionNormalizer("#value#", "#other_value#", OPT_"#other_name#")";
0248 code Denormalizer = "makeBooleanOptionDenormalizer("#value#")";
0249 }
0250
0251 // Marshalled option accepting any of the specified enum values.
0252 // Typically used with `Values`, `NormalizedValues` and `NormalizedValuesScope`.
0253 class MarshallingInfoEnum<KeyPathAndMacro kpm, code defaultvalue>
0254 : MarshallingInfo<kpm, defaultvalue> {
0255 code Normalizer = "normalizeSimpleEnum";
0256 code Denormalizer = "denormalizeSimpleEnum";
0257 }
0258
0259 // Mixins for additional marshalling attributes.
0260
0261 class ShouldParseIf<code condition> { code ShouldParse = condition; }
0262 class AlwaysEmit { bit ShouldAlwaysEmit = true; }
0263 class Normalizer<code normalizer> { code Normalizer = normalizer; }
0264 class Denormalizer<code denormalizer> { code Denormalizer = denormalizer; }
0265 class NormalizedValuesScope<code scope> { code NormalizedValuesScope = scope; }
0266 class NormalizedValues<list<code> definitions> { list<code> NormalizedValues = definitions; }
0267 class ValueMerger<code merger> { code ValueMerger = merger; }
0268 class ValueExtractor<code extractor> { code ValueExtractor = extractor; }
0269
0270 // Predefined options.
0271
0272 // FIXME: Have generator validate that these appear in correct position (and
0273 // aren't duplicated).
0274 def INPUT : Option<[], "<input>", KIND_INPUT>;
0275 def UNKNOWN : Option<[], "<unknown>", KIND_UNKNOWN>;
0276
0277 #endif // LLVM_OPTION_OPTPARSER_TD