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