1// RUN: llvm-tblgen -gen-searchable-tables -I %p/../../include %s | FileCheck %s
2// RUN: not llvm-tblgen -gen-searchable-tables -I %p/../../include -DERROR1 %s 2>&1 | FileCheck --check-prefix=ERROR1 %s
3// XFAIL: vg_leak
4
5include "llvm/TableGen/SearchableTable.td"
6
7// CHECK-LABEL: GET_BValues_DECL
8// CHECK: enum BValues {
9// CHECK:   BAlice = 172,
10// CHECK:   BBob = 20,
11// CHECK:   BCharlie = 128,
12// CHECK:   BEve = 76,
13// CHECK: }
14
15// CHECK-LABEL: GET_CEnum_DECL
16// CHECK: enum CEnum {
17// CHECK:   CBar
18// CHECK:   CBaz
19// CHECK:   CFoo
20// CHECK: }
21
22// CHECK-LABEL: GET_ATable_DECL
23// CHECK: const AEntry *lookupATableByValues(uint8_t Val1, uint16_t Val2);
24
25// CHECK-LABEL: GET_ATable_IMPL
26// CHECK: constexpr AEntry ATable[] = {
27// CHECK:   { "baz"
28// CHECK:   { "foo"
29// CHECK:   { "foobar"
30// CHECK:   { "bar"
31// CHECK: };
32
33// CHECK: const AEntry *lookupATableByValues(uint8_t Val1, uint16_t Val2) {
34// CHECK:   return &*Idx;
35// CHECK: }
36
37class AEntry<string str, int val1, int val2> {
38  string Str = str;
39  bits<8> Val1 = val1;
40  bits<10> Val2 = val2;
41}
42
43def : AEntry<"bar",    5, 3>;
44def : AEntry<"baz",    2, 6>;
45def : AEntry<"foo",    4, 4>;
46def : AEntry<"foobar", 4, 5>;
47
48def ATable : GenericTable {
49  let FilterClass = "AEntry";
50  let Fields = ["Str", "Val1", "Val2"];
51
52  let PrimaryKey = ["Val1", "Val2"];
53  let PrimaryKeyName = "lookupATableByValues";
54}
55
56
57// CHECK-LABEL: GET_BTable_IMPL
58// CHECK: constexpr BTypeName BTable[] = {
59// CHECK:   { "BAlice", 0xAC,  },
60// CHECK:   { "BBob", 0x14, Bob == 13 },
61// CHECK:   { "BCharlie", 0x80, Charlie == 42 },
62// CHECK:   { "BEve", 0x4C, Eve == 108 },
63// CHECK:  };
64// CHECK: const BTypeName *lookupBTableByName(StringRef Name) {
65// CHECK:   return &BTable[Idx->_index];
66// CHECK: }
67
68class BEntry<bits<16> enc, code test = [{}]> {
69  string Name = NAME;
70  bits<16> Encoding = enc;
71  code Test = test;
72}
73
74def BAlice   : BEntry<0xac>;
75def BBob     : BEntry<0x14, [{Bob == 13}]>;
76def BCharlie : BEntry<0x80, "Charlie == 42">;
77def BEve     : BEntry<0x4c, [{Eve == }] # 108>;
78
79def BValues : GenericEnum {
80  let FilterClass = "BEntry";
81  let NameField = "Name";
82  let ValueField = "Encoding";
83}
84
85def BTable : GenericTable {
86  let FilterClass = "BEntry";
87  string CppTypeName = "BTypeName";
88  let Fields = ["Name", "Encoding", "Test"];
89  string TypeOf_Test = "code";
90}
91
92def lookupBTableByName : SearchIndex {
93  let Table = BTable;
94  let Key = ["Name"];
95}
96
97
98// CHECK-LABEL: GET_CTable_DECL
99// CHECK: const CEntry *lookupCEntryByEncoding(uint16_t Encoding);
100// CHECK: const CEntry *lookupCEntry(StringRef Name, unsigned Kind);
101// CHECK-LABEL: GET_CTable_IMPL
102// CHECK: const CEntry *lookupCEntryByEncoding(uint16_t Encoding) {
103// CHECK:   if ((Encoding < 0xA) ||
104// CHECK:       (Encoding > 0xF))
105// CHECK:     return nullptr;
106
107// CHECK: const CEntry *lookupCEntry(StringRef Name, unsigned Kind) {
108// CHECK: Index[] = {
109// CHECK:   { "ALICE", CBar, 1 },
110// CHECK:   { "ALICE", CFoo, 0 },
111// CHECK:   { "BOB", CBaz, 2 },
112
113class CEnum;
114
115def CFoo : CEnum;
116def CBar : CEnum;
117def CBaz : CEnum;
118
119def CEnum : GenericEnum {
120  let FilterClass = "CEnum";
121}
122
123class CEntry<string name, CEnum kind, int enc> {
124  string Name = name;
125  CEnum Kind = kind;
126  bits<16> Encoding = enc;
127}
128
129def : CEntry<"alice", CFoo, 10>;
130def : CEntry<"alice", CBar, 13>;
131def : CEntry<"bob",   CBaz, 15>;
132
133def CTable : GenericTable {
134  let FilterClass = "CEntry";
135  let Fields = ["Name", "Kind", "Encoding"];
136
137  string TypeOf_Kind = "CEnum";
138
139  let PrimaryKey = ["Encoding"];
140  let PrimaryKeyName = "lookupCEntryByEncoding";
141  let PrimaryKeyEarlyOut = 1;
142}
143
144def lookupCEntry : SearchIndex {
145  let Table = CTable;
146  let Key = ["Name", "Kind"];
147}
148
149#ifdef ERROR1
150
151class DEntry<string str, int val1> {
152  string Str = str;
153  bits<8> Val1 = val1;
154}
155
156def DFoo : DEntry<"foo", 1>;
157// ERROR1: [[@LINE+1]]:1: error: Record 'DBar' for table 'DTable' is missing field 'Val1'
158def DBar : DEntry<"bar", ?>;
159
160def DTable : GenericTable {
161  let FilterClass = "DEntry";
162  let Fields = ["Str", "Val1"];
163}
164
165#endif // ERROR1
166