File: enums-gen.td

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (103 lines) | stat: -rw-r--r-- 3,843 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// RUN: mlir-tblgen -gen-enum-decls -I %S/../../include %s | FileCheck %s --check-prefix=DECL
// RUN: mlir-tblgen -gen-enum-defs -I %S/../../include %s | FileCheck %s --check-prefix=DEF

include "mlir/IR/EnumAttr.td"
include "mlir/IR/OpBase.td"

// Test bit enums
def None: I32BitEnumAttrCaseNone<"None", "none">;
def Bit0: I32BitEnumAttrCaseBit<"Bit0", 0, "tagged">;
def Bit1: I32BitEnumAttrCaseBit<"Bit1", 1>;
def Bit2: I32BitEnumAttrCaseBit<"Bit2", 2>;
def Bit3: I32BitEnumAttrCaseBit<"Bit3", 3>;
def BitGroup: I32BitEnumAttrCaseGroup<"BitGroup", [
  Bit0, Bit1
]>;

def MyBitEnum: I32BitEnumAttr<"MyBitEnum", "An example bit enum",
                           [None, Bit0, Bit1, Bit2, Bit3, BitGroup]> {
  let genSpecializedAttr = 0;
}

// DECL-LABEL: enum class MyBitEnum : uint32_t
// DECL: None = 0,
// DECL: Bit0 = 1,
// DECL: Bit1 = 2,
// DECL: Bit2 = 4,
// DECL: Bit3 = 8,
// DECL: }

// DECL: ::std::optional<MyBitEnum> symbolizeMyBitEnum(uint32_t);
// DECL: std::string stringifyMyBitEnum(MyBitEnum);
// DECL: ::std::optional<MyBitEnum> symbolizeMyBitEnum(::llvm::StringRef);

// DECL: struct FieldParser<::MyBitEnum, ::MyBitEnum> {
// DECL:   template <typename ParserT>
// DECL:   static FailureOr<::MyBitEnum> parse(ParserT &parser) {
// DECL:     // Parse the keyword/string containing the enum.
// DECL:     std::string enumKeyword;
// DECL:     auto loc = parser.getCurrentLocation();
// DECL:     if (failed(parser.parseOptionalKeywordOrString(&enumKeyword)))
// DECL:       return parser.emitError(loc, "expected keyword for An example bit enum");
// DECL:     // Symbolize the keyword.
// DECL:     if (::std::optional<::MyBitEnum> attr = ::symbolizeEnum<::MyBitEnum>(enumKeyword))
// DECL:       return *attr;
// DECL:     return parser.emitError(loc, "invalid An example bit enum specification: ") << enumKeyword;
// DECL:   }

// DECL: inline ::llvm::raw_ostream &operator<<(::llvm::raw_ostream &p, ::MyBitEnum value) {
// DECL:   auto valueStr = stringifyEnum(value);
// DECL:   switch (value) {
// DECL:   case ::MyBitEnum::BitGroup:
// DECL:     return p << valueStr;
// DECL:   default:
// DECL:     break;
// DECL:   }
// DECL:   auto underlyingValue = static_cast<std::make_unsigned_t<::MyBitEnum>>(value);
// DECL:   if (underlyingValue && !llvm::has_single_bit(underlyingValue))
// DECL:     return p << '"' << valueStr << '"';
// DECL:   return p << valueStr;

// DEF-LABEL: std::string stringifyMyBitEnum
// DEF: auto val = static_cast<uint32_t>
// DEF: if (val == 0) return "none";
// DEF: if (1u == (1u & val))
// DEF-NEXT: push_back("tagged")
// DEF: if (2u == (2u & val))
// DEF-NEXT: push_back("Bit1")

// DEF-LABEL: ::std::optional<MyBitEnum> symbolizeMyBitEnum(::llvm::StringRef str)
// DEF: if (str == "none") return MyBitEnum::None;
// DEF: .Case("tagged", 1)
// DEF: .Case("Bit1", 2)

// Test enum printer generation for non non-keyword enums.

def NonKeywordBit: I32BitEnumAttrCaseBit<"Bit0", 0, "tag-ged">;
def MyMixedNonKeywordBitEnum: I32BitEnumAttr<"MyMixedNonKeywordBitEnum", "An example bit enum", [
    NonKeywordBit,
    Bit1
  ]> {
  let genSpecializedAttr = 0;
}

def MyNonKeywordBitEnum: I32BitEnumAttr<"MyNonKeywordBitEnum", "An example bit enum", [
    NonKeywordBit
  ]> {
  let genSpecializedAttr = 0;
}

// DECL: inline ::llvm::raw_ostream &operator<<(::llvm::raw_ostream &p, ::MyMixedNonKeywordBitEnum value) {
// DECL: auto valueStr = stringifyEnum(value);
// DECL:   switch (value) {
// DECL:   case ::MyMixedNonKeywordBitEnum::Bit1:
// DECL:     break;
// DECL:   default:
// DECL:     return p << '"' << valueStr << '"';
// DECL:   }
// DECL:   return p << valueStr;
// DECL: }

// DECL: inline ::llvm::raw_ostream &operator<<(::llvm::raw_ostream &p, ::MyNonKeywordBitEnum value) {
// DECL:   auto valueStr = stringifyEnum(value);
// DECL:   return p << '"' << valueStr << '"';