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