xref: /llvm-project/mlir/include/mlir/IR/Utils.td (revision 03d1c99d99d7918115b993f14dcb6fc39cf09f72)
1//===-- Utils.td - General utilities file ------------------*- tablegen -*-===//
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 contains a number of utilities which can be used across tablegen
10// files.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef UTILS_TD
15#define UTILS_TD
16
17// Helper for marking deprecated classes or defs in TableGen. To mark a def as
18// deprecated, mix in the `Deprecate` class with a reason.
19// Usage of a deprecated def within TableGen will cause a warning with the
20// given message.
21class Deprecated<string reason> {
22  string odsDeprecated = reason;
23}
24
25// Helper for marking entities in ODS generated C++ as deprecated.
26// Usage of such an entity from C++ code will cause a warning being emitted by
27// the C++ compiler with the given message.
28//
29// Note: Support has to be implemented by the code generator of a given
30// entity.
31class CppDeprecated<string reason> {
32  string odsCppDeprecated = reason;
33}
34
35// A workaround for the inability to define functions in Tablegen.
36//
37// The template parameter defines a string that can be extracted from an
38// instance of this class by accessing the "result" member. Subclasses can take
39// their own template parameters as function "arguments" and use them to
40// populate result.
41// For example, if it didn't already exist, a concat function could be defined
42// like:
43//
44// class StrConcat<list<string> strings> :
45//     StrFunc<!foldl("", strings, prev, cur, prev # cur)>
46//
47// and then called like
48//
49// StrConcat<["a", "b", "c"]>.result
50//
51// to get the string "abc"
52class StrFunc<string r> {
53  string result = r;
54}
55
56// Marker used to identify the argument list.
57def ins;
58
59// Marker used to identify the result list.
60def outs;
61
62// This class represents a typed argument with optional default value for C
63// function signatures, e.g. builders or methods.
64class CArg<string ty, string value = ""> {
65  string type = ty;
66  string defaultValue = value;
67}
68
69// Helper which makes the first letter of a string uppercase.
70// e.g. cat -> Cat
71class firstCharToUpper<string str>
72{
73  string ret = !if(!gt(!size(str), 0),
74    !toupper(!substr(str, 0, 1)) # !substr(str, 1),
75    "");
76}
77
78class _snakeCaseHelper<string str> {
79  int idx = !find(str, "_");
80  string ret = !if(!ge(idx, 0),
81    !substr(str, 0, idx) # firstCharToUpper<!substr(str, !add(idx, 1))>.ret,
82    str);
83}
84
85// Converts a snake_case string to CamelCase.
86// TODO: Replace with a !tocamelcase bang operator.
87class snakeCaseToCamelCase<string str>
88{
89  string ret = !foldl(firstCharToUpper<str>.ret,
90    !range(0, !size(str)), acc, idx, _snakeCaseHelper<acc>.ret);
91}
92
93#endif // UTILS_TD
94