1 //===--- OptParser.td - Common Option Parsing Interfaces ------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines the common interfaces used by the option parsing TableGen
12 //===----------------------------------------------------------------------===//
14 // Define the kinds of options.
16 class OptionKind<string name, int precedence = 0, bit sentinel = 0> {
18 // The kind precedence, kinds with lower precedence are matched first.
19 int Precedence = precedence;
20 // Indicate a sentinel option.
21 bit Sentinel = sentinel;
25 def KIND_GROUP : OptionKind<"Group">;
26 // The input option kind.
27 def KIND_INPUT : OptionKind<"Input", 1, 1>;
28 // The unknown option kind.
29 def KIND_UNKNOWN : OptionKind<"Unknown", 2, 1>;
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
42 def KIND_JOINED_OR_SEPARATE : OptionKind<"JoinedOrSeparate">;
43 // An option which is both joined to its (first) value, and followed by its
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
50 def KIND_REMAINING_ARGS_JOINED : OptionKind<"RemainingArgsJoined">;
52 // Define the option flags.
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;
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;
65 // RenderJoined - The option should be rendered joined, even if separate (only
66 // sensible on single value separate options).
67 def RenderJoined : OptionFlag;
69 // RenderSeparate - The option should be rendered separately, even if joined
70 // (only sensible on joined options).
71 def RenderSeparate : OptionFlag;
73 // Define the option group class.
75 class OptionGroup<string name> {
76 string EnumName = ?; // Uses the def name if undefined.
79 OptionGroup Group = ?;
80 list<OptionFlag> Flags = [];
83 // Define the option class.
85 class Option<list<string> prefixes, string name, OptionKind kind> {
86 string EnumName = ?; // Uses the def name if undefined.
87 list<string> Prefixes = prefixes;
89 OptionKind Kind = kind;
90 // Used by MultiArg option kind.
93 string MetaVarName = ?;
96 list<OptionFlag> Flags = [];
97 OptionGroup Group = ?;
99 list<string> AliasArgs = [];
102 // Helpers for defining options.
104 class Flag<list<string> prefixes, string name>
105 : Option<prefixes, name, KIND_FLAG>;
106 class Joined<list<string> prefixes, string name>
107 : Option<prefixes, name, KIND_JOINED>;
108 class Separate<list<string> prefixes, string name>
109 : Option<prefixes, name, KIND_SEPARATE>;
110 class CommaJoined<list<string> prefixes, string name>
111 : Option<prefixes, name, KIND_COMMAJOINED>;
112 class MultiArg<list<string> prefixes, string name, int numargs>
113 : Option<prefixes, name, KIND_MULTIARG> {
114 int NumArgs = numargs;
116 class JoinedOrSeparate<list<string> prefixes, string name>
117 : Option<prefixes, name, KIND_JOINED_OR_SEPARATE>;
118 class JoinedAndSeparate<list<string> prefixes, string name>
119 : Option<prefixes, name, KIND_JOINED_AND_SEPARATE>;
121 // Mix-ins for adding optional attributes.
123 class Alias<Option alias> { Option Alias = alias; }
124 class AliasArgs<list<string> aliasargs> { list<string> AliasArgs = aliasargs; }
125 class EnumName<string name> { string EnumName = name; }
126 class Flags<list<OptionFlag> flags> { list<OptionFlag> Flags = flags; }
127 class Group<OptionGroup group> { OptionGroup Group = group; }
128 class HelpText<string text> { string HelpText = text; }
129 class MetaVarName<string name> { string MetaVarName = name; }
130 class Values<string value> { string Values = value; }
131 class ValuesCode<code valuecode> { code ValuesCode = valuecode; }
133 // Predefined options.
135 // FIXME: Have generator validate that these appear in correct position (and
136 // aren't duplicated).
137 def INPUT : Option<[], "<input>", KIND_INPUT>;
138 def UNKNOWN : Option<[], "<unknown>", KIND_UNKNOWN>;