Skip to content

Commit 965e66c

Browse files
committed
Use snakeCaseToCamelCase() helper for generating getter names.
A !tocamelcase bang would nice in future :)
1 parent b4660dc commit 965e66c

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

mlir/include/mlir/IR/OpBase.td

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -568,20 +568,13 @@ class TypesMatchWith<string summary, string lhsArg, string rhsArg,
568568
string transformer = transform;
569569
}
570570

571-
// Helper which makes the first letter of a string uppercase.
572-
// e.g. cat -> Cat
573-
class first_char_to_upper<string str>
574-
{
575-
string ret = !toupper(!substr(str, 0, 1)) # !substr(str, 1);
576-
}
577-
578571
// The same as TypesMatchWith but if either `lhsArg` or `rhsArg` are optional
579572
// and not present returns success.
580573
class OptionalTypesMatchWith<string summary, string lhsArg, string rhsArg,
581574
string transform, string comparator = "std::equal_to<>()">
582575
: TypesMatchWith<summary, lhsArg, rhsArg, transform,
583-
"!get" # first_char_to_upper<lhsArg>.ret # "()"
584-
# " || !get" # first_char_to_upper<rhsArg>.ret # "() || " # comparator>;
576+
"!get" # snakeCaseToCamelCase<lhsArg>.ret # "()"
577+
# " || !get" # snakeCaseToCamelCase<rhsArg>.ret # "() || " # comparator>;
585578

586579
// Special variant of `TypesMatchWith` that provides a comparator suitable for
587580
// ranged arguments.

mlir/include/mlir/IR/Utils.td

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,28 @@ class CArg<string ty, string value = ""> {
6666
string defaultValue = value;
6767
}
6868

69+
// Helper which makes the first letter of a string uppercase.
70+
// e.g. cat -> Cat
71+
class firstCharToUpper<string str>
72+
{
73+
string ret = !if(!gt(!size(str), 0),
74+
!toupper(!substr(str, 0, 1)) # !substr(str, 1),
75+
"");
76+
}
77+
78+
class _snakeCaseHeper<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.
87+
class snakeCaseToCamelCase<string str>
88+
{
89+
string ret = !foldl(firstCharToUpper<str>.ret,
90+
!range(0, !size(str)), acc, idx, _snakeCaseHeper<acc>.ret);
91+
}
92+
6993
#endif // UTILS_TD

mlir/test/mlir-tblgen/utils.td

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: mlir-tblgen -I %S/../../include %s | FileCheck %s
2+
3+
include "mlir/IR/Utils.td"
4+
5+
// CHECK-DAG: string value = "CamelCaseTest"
6+
class already_camel_case {
7+
string value = snakeCaseToCamelCase<"CamelCaseTest">.ret;
8+
}
9+
10+
// CHECK-DAG: string value = "Foo"
11+
class single_word {
12+
string value = snakeCaseToCamelCase<"foo">.ret;
13+
}
14+
15+
// CHECK-DAG: string value = "ThisIsATest"
16+
class snake_case {
17+
string value = snakeCaseToCamelCase<"this_is_a_test">.ret;
18+
}
19+
20+
// CHECK-DAG: string value = "ThisIsATestAgain"
21+
class extra_underscores {
22+
string value = snakeCaseToCamelCase<"__this__is_a_test__again__">.ret;
23+
}

0 commit comments

Comments
 (0)