xref: /freebsd/contrib/llvm-project/llvm/include/llvm/Option/OptParser.td (revision b1879975794772ee51f0b4865753364c7d7626c3)
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 Visibility categories
77
78class OptionVisibility {}
79
80// Explicit specifier for default visibility
81def DefaultVis : OptionVisibility;
82
83// Define the option group class.
84
85class OptionGroup<string name> {
86  string EnumName = ?; // Uses the def name if undefined.
87  string Name = name;
88  string HelpText = ?;
89  OptionGroup Group = ?;
90  list<OptionFlag> Flags = [];
91  list<OptionVisibility> Visibility = [];
92}
93
94// Define the option class.
95
96class HelpTextVariant<list<OptionVisibility> visibilities, string text> {
97  list<OptionVisibility> Visibilities = visibilities;
98  string Text = text;
99}
100
101class Option<list<string> prefixes, string name, OptionKind kind> {
102  string EnumName = ?; // Uses the def name if undefined.
103  list<string> Prefixes = prefixes;
104  string Name = name;
105  OptionKind Kind = kind;
106  // Used by MultiArg option kind.
107  int NumArgs = 0;
108  string HelpText = ?;
109  list<HelpTextVariant> HelpTextsForVariants = [];
110  string MetaVarName = ?;
111  string Values = ?;
112  code ValuesCode = ?;
113  list<OptionFlag> Flags = [];
114  list<OptionVisibility> Visibility = [DefaultVis];
115  OptionGroup Group = ?;
116  Option Alias = ?;
117  list<string> AliasArgs = [];
118  code MacroPrefix = "";
119  code KeyPath = ?;
120  code DefaultValue = ?;
121  code ImpliedValue = ?;
122  code ImpliedCheck = "false";
123  code ShouldParse = "true";
124  bit ShouldAlwaysEmit = false;
125  code NormalizerRetTy = ?;
126  code NormalizedValuesScope = "";
127  code Normalizer = "";
128  code Denormalizer = "";
129  code ValueMerger = "mergeForwardValue";
130  code ValueExtractor = "extractForwardValue";
131  list<code> NormalizedValues = ?;
132}
133
134// Helpers for defining options.
135
136class Flag<list<string> prefixes, string name>
137  : Option<prefixes, name, KIND_FLAG>;
138class Joined<list<string> prefixes, string name>
139  : Option<prefixes, name, KIND_JOINED>;
140class Separate<list<string> prefixes, string name>
141  : Option<prefixes, name, KIND_SEPARATE>;
142class CommaJoined<list<string> prefixes, string name>
143  : Option<prefixes, name, KIND_COMMAJOINED>;
144class MultiArg<list<string> prefixes, string name, int numargs>
145  : Option<prefixes, name, KIND_MULTIARG> {
146  int NumArgs = numargs;
147}
148class JoinedOrSeparate<list<string> prefixes, string name>
149  : Option<prefixes, name, KIND_JOINED_OR_SEPARATE>;
150class JoinedAndSeparate<list<string> prefixes, string name>
151  : Option<prefixes, name, KIND_JOINED_AND_SEPARATE>;
152
153// Mix-ins for adding optional attributes.
154
155class Alias<Option alias> { Option Alias = alias; }
156class AliasArgs<list<string> aliasargs> { list<string> AliasArgs = aliasargs; }
157class EnumName<string name> { string EnumName = name; }
158class Flags<list<OptionFlag> flags> { list<OptionFlag> Flags = flags; }
159class Visibility<list<OptionVisibility> visibility> {
160  list<OptionVisibility> Visibility = visibility;
161}
162class Group<OptionGroup group> { OptionGroup Group = group; }
163class HelpText<string text> { string HelpText = text; }
164class HelpTextForVariants<list<OptionVisibility> Visibilities, string text> {
165  list<HelpTextVariant> HelpTextsForVariants = [
166    HelpTextVariant<Visibilities, text>
167  ];
168}
169
170class MetaVarName<string name> { string MetaVarName = name; }
171class Values<string value> { string Values = value; }
172class ValuesCode<code valuecode> { code ValuesCode = valuecode; }
173
174// Helpers for defining marshalling information (typically used in Clang's -cc1
175// frontend).
176
177// The key path to the mapped field and the macro prefix for the resulting
178// definition database.
179class KeyPathAndMacro<string key_path_prefix, string key_path_base,
180                      string macro_prefix = ""> {
181  code KeyPath = !strconcat(key_path_prefix, key_path_base);
182  code MacroPrefix = macro_prefix;
183}
184
185// Mixin that implies the specified value for the current option when any of the
186// given key paths evaluates to true.
187class ImpliedByAnyOf<list<string> key_paths, code value = "true"> {
188  code ImpliedCheck = !foldl("false", key_paths, accumulator, key_path,
189                             !strconcat(accumulator, " || ", key_path));
190  code ImpliedValue = value;
191}
192
193// Parent class for marshalled options (typically used in Clang's -cc1 frontend).
194class MarshallingInfo<KeyPathAndMacro kpm, code defaultvalue> {
195  code KeyPath = kpm.KeyPath;
196  code MacroPrefix = kpm.MacroPrefix;
197  code DefaultValue = defaultvalue;
198}
199
200// Marshalled option accepting a string argument.
201class MarshallingInfoString<KeyPathAndMacro kpm, code defaultvalue="std::string()">
202  : MarshallingInfo<kpm, defaultvalue> {
203  code Normalizer = "normalizeString";
204  code Denormalizer = "denormalizeString";
205}
206
207// Marshalled option accepting an integer argument.
208class MarshallingInfoInt<KeyPathAndMacro kpm, code defaultvalue="0", code type="unsigned">
209  : MarshallingInfo<kpm, defaultvalue> {
210  code Normalizer = "normalizeStringIntegral<"#type#">";
211  code Denormalizer = "denormalizeString<"#type#">";
212}
213
214// Marshalled option accepting vector of strings.
215class MarshallingInfoStringVector<KeyPathAndMacro kpm>
216  : MarshallingInfo<kpm, "std::vector<std::string>({})"> {
217  code Normalizer = "normalizeStringVector";
218  code Denormalizer = "denormalizeStringVector";
219}
220
221// Marshalled option - single positive flag.
222class MarshallingInfoFlag<KeyPathAndMacro kpm, code defaultvalue = "false">
223  : MarshallingInfo<kpm, defaultvalue> {
224  code Normalizer = "normalizeSimpleFlag";
225  code Denormalizer = "denormalizeSimpleFlag";
226}
227
228// Marshalled option - single negative flag.
229class MarshallingInfoNegativeFlag<KeyPathAndMacro kpm, code defaultvalue = "true">
230  : MarshallingInfo<kpm, defaultvalue> {
231  code Normalizer = "normalizeSimpleNegativeFlag";
232  code Denormalizer = "denormalizeSimpleFlag";
233}
234
235// Marshalled option - single flag contributing to a bitfield.
236class MarshallingInfoBitfieldFlag<KeyPathAndMacro kpm, code value>
237  : MarshallingInfoFlag<kpm, "0u"> {
238  code Normalizer = "makeFlagToValueNormalizer("#value#")";
239  code ValueMerger = "mergeMaskValue";
240  code ValueExtractor = "(extractMaskValue<unsigned, decltype("#value#"), "#value#">)";
241}
242
243// Implementation detail of BoolOption.
244class MarshallingInfoBooleanFlag<KeyPathAndMacro kpm, code defaultvalue, code value,
245                                 code other_value, code other_name>
246  : MarshallingInfoFlag<kpm, defaultvalue> {
247  code Normalizer = "makeBooleanOptionNormalizer("#value#", "#other_value#", OPT_"#other_name#")";
248  code Denormalizer = "makeBooleanOptionDenormalizer("#value#")";
249}
250
251// Marshalled option accepting any of the specified enum values.
252// Typically used with `Values`, `NormalizedValues` and `NormalizedValuesScope`.
253class MarshallingInfoEnum<KeyPathAndMacro kpm, code defaultvalue>
254  : MarshallingInfo<kpm, defaultvalue> {
255  code Normalizer = "normalizeSimpleEnum";
256  code Denormalizer = "denormalizeSimpleEnum";
257}
258
259// Mixins for additional marshalling attributes.
260
261class ShouldParseIf<code condition> { code ShouldParse = condition; }
262class AlwaysEmit { bit ShouldAlwaysEmit = true; }
263class Normalizer<code normalizer> { code Normalizer = normalizer; }
264class Denormalizer<code denormalizer> { code Denormalizer = denormalizer; }
265class NormalizedValuesScope<code scope> { code NormalizedValuesScope = scope; }
266class NormalizedValues<list<code> definitions> { list<code> NormalizedValues = definitions; }
267class ValueMerger<code merger> { code ValueMerger = merger; }
268class ValueExtractor<code extractor> { code ValueExtractor = extractor; }
269
270// Predefined options.
271
272// FIXME: Have generator validate that these appear in correct position (and
273// aren't duplicated).
274def INPUT : Option<[], "<input>", KIND_INPUT>;
275def UNKNOWN : Option<[], "<unknown>", KIND_UNKNOWN>;
276
277#endif // LLVM_OPTION_OPTPARSER_TD
278