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// Define the kinds of options. 15 16class OptionKind<string name, int precedence = 0, bit sentinel = 0> { 17 string Name = name; 18 // The kind precedence, kinds with lower precedence are matched first. 19 int Precedence = precedence; 20 // Indicate a sentinel option. 21 bit Sentinel = sentinel; 22} 23 24// An option group. 25def KIND_GROUP : OptionKind<"Group">; 26// The input option kind. 27def KIND_INPUT : OptionKind<"Input", 1, 1>; 28// The unknown option kind. 29def KIND_UNKNOWN : OptionKind<"Unknown", 2, 1>; 30// A flag with no values. 31def KIND_FLAG : OptionKind<"Flag">; 32// An option which prefixes its (single) value. 33def KIND_JOINED : OptionKind<"Joined", 1>; 34// An option which is followed by its value. 35def KIND_SEPARATE : OptionKind<"Separate">; 36// An option followed by its values, which are separated by commas. 37def KIND_COMMAJOINED : OptionKind<"CommaJoined">; 38// An option which is which takes multiple (separate) arguments. 39def KIND_MULTIARG : OptionKind<"MultiArg">; 40// An option which is either joined to its (non-empty) value, or followed by its 41// value. 42def KIND_JOINED_OR_SEPARATE : OptionKind<"JoinedOrSeparate">; 43// An option which is both joined to its (first) value, and followed by its 44// (second) value. 45def KIND_JOINED_AND_SEPARATE : OptionKind<"JoinedAndSeparate">; 46// An option which consumes all remaining arguments if there are any. 47def KIND_REMAINING_ARGS : OptionKind<"RemainingArgs">; 48// An option which consumes an optional joined argument and any other remaining 49// arguments. 50def KIND_REMAINING_ARGS_JOINED : OptionKind<"RemainingArgsJoined">; 51 52// Define the option flags. 53 54class OptionFlag {} 55 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. 59def HelpHidden : OptionFlag; 60 61// RenderAsInput - The option should not render the name when rendered as an 62// input (i.e., the option is rendered as values). 63def RenderAsInput : OptionFlag; 64 65// RenderJoined - The option should be rendered joined, even if separate (only 66// sensible on single value separate options). 67def RenderJoined : OptionFlag; 68 69// RenderSeparate - The option should be rendered separately, even if joined 70// (only sensible on joined options). 71def RenderSeparate : OptionFlag; 72 73// Define the option group class. 74 75class OptionGroup<string name> { 76 string EnumName = ?; // Uses the def name if undefined. 77 string Name = name; 78 string HelpText = ?; 79 OptionGroup Group = ?; 80 list<OptionFlag> Flags = []; 81} 82 83// Define the option class. 84 85class Option<list<string> prefixes, string name, OptionKind kind> { 86 string EnumName = ?; // Uses the def name if undefined. 87 list<string> Prefixes = prefixes; 88 string Name = name; 89 OptionKind Kind = kind; 90 // Used by MultiArg option kind. 91 int NumArgs = 0; 92 string HelpText = ?; 93 string MetaVarName = ?; 94 string Values = ?; 95 code ValuesCode = ?; 96 list<OptionFlag> Flags = []; 97 OptionGroup Group = ?; 98 Option Alias = ?; 99 list<string> AliasArgs = []; 100 string MarshallingKind = ?; 101 code KeyPath = ?; 102 code DefaultValue = ?; 103 bit ShouldAlwaysEmit = 0; 104 // Used by the Flag option kind. 105 bit IsPositive = 1; 106 // Used by the String option kind. 107 code NormalizerRetTy = ?; 108 code NormalizedValuesScope = ""; 109 code Normalizer = ""; 110 code Denormalizer = ""; 111 list<code> NormalizedValues = ?; 112} 113 114// Helpers for defining options. 115 116class Flag<list<string> prefixes, string name> 117 : Option<prefixes, name, KIND_FLAG>; 118class Joined<list<string> prefixes, string name> 119 : Option<prefixes, name, KIND_JOINED>; 120class Separate<list<string> prefixes, string name> 121 : Option<prefixes, name, KIND_SEPARATE>; 122class CommaJoined<list<string> prefixes, string name> 123 : Option<prefixes, name, KIND_COMMAJOINED>; 124class MultiArg<list<string> prefixes, string name, int numargs> 125 : Option<prefixes, name, KIND_MULTIARG> { 126 int NumArgs = numargs; 127} 128class JoinedOrSeparate<list<string> prefixes, string name> 129 : Option<prefixes, name, KIND_JOINED_OR_SEPARATE>; 130class JoinedAndSeparate<list<string> prefixes, string name> 131 : Option<prefixes, name, KIND_JOINED_AND_SEPARATE>; 132 133// Mix-ins for adding optional attributes. 134 135class Alias<Option alias> { Option Alias = alias; } 136class AliasArgs<list<string> aliasargs> { list<string> AliasArgs = aliasargs; } 137class EnumName<string name> { string EnumName = name; } 138class Flags<list<OptionFlag> flags> { list<OptionFlag> Flags = flags; } 139class Group<OptionGroup group> { OptionGroup Group = group; } 140class HelpText<string text> { string HelpText = text; } 141class MetaVarName<string name> { string MetaVarName = name; } 142class Values<string value> { string Values = value; } 143class ValuesCode<code valuecode> { code ValuesCode = valuecode; } 144 145// Helpers for defining marshalling information. 146 147class MarshallingInfo<code keypath, code defaultvalue> { 148 code KeyPath = keypath; 149 code DefaultValue = defaultvalue; 150} 151class MarshallingInfoString<code keypath, code defaultvalue, code normalizerretty> 152 : MarshallingInfo<keypath, defaultvalue> { 153 string MarshallingKind = "string"; 154 code NormalizerRetTy = normalizerretty; 155} 156 157class MarshallingInfoFlag<code keypath, code defaultvalue> 158 : MarshallingInfo<keypath, defaultvalue> { 159 string MarshallingKind = "flag"; 160} 161 162// Mixins for additional marshalling attributes. 163 164class IsNegative { bit IsPositive = 0; } 165class AlwaysEmit { bit ShouldAlwaysEmit = 1; } 166class Normalizer<code normalizer> { code Normalizer = normalizer; } 167class Denormalizer<code denormalizer> { code Denormalizer = denormalizer; } 168class NormalizedValuesScope<code scope> { code NormalizedValuesScope = scope; } 169class NormalizedValues<list<code> definitions> { list<code> NormalizedValues = definitions; } 170class DenormalizeString { code Denormalizer = "denormalizeString"; } 171class AutoNormalizeEnum { 172 code Normalizer = "normalizeSimpleEnum"; 173 code Denormalizer = "denormalizeSimpleEnum"; 174} 175 176// Predefined options. 177 178// FIXME: Have generator validate that these appear in correct position (and 179// aren't duplicated). 180def INPUT : Option<[], "<input>", KIND_INPUT>; 181def UNKNOWN : Option<[], "<unknown>", KIND_UNKNOWN>; 182