Debian Codegraph @

suite/synctools/tablegen/include/llvm/TableGen/Record.h

raw symbols SCIP
1 //===- llvm/TableGen/Record.h - Classes for Table Records -------*- C++ -*-===//
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 main TableGen data structures, including the TableGen
10 // types, values, and high-level data structures.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_TABLEGEN_RECORD_H
15 #define LLVM_TABLEGEN_RECORD_H
16
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/DenseSet.h"
20 #include "llvm/ADT/FoldingSet.h"
21 #include "llvm/ADT/PointerIntPair.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/Support/Casting.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/SMLoc.h"
28 #include "llvm/Support/Timer.h"
29 #include "llvm/Support/TrailingObjects.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include <algorithm>
32 #include <cassert>
33 #include <cstddef>
34 #include <cstdint>
35 #include <map>
36 #include <memory>
37 #include <string>
38 #include <utility>
39 #include <vector>
40
41 namespace llvm {
42 namespace detail {
43 struct RecordContext;
44 } // namespace detail
45
46 class ListRecTy;
47 class Record;
48 class RecordKeeper;
49 class RecordVal;
50 class Resolver;
51 class StringInit;
52 class TypedInit;
53
54 //===----------------------------------------------------------------------===//
55 // Type Classes
56 //===----------------------------------------------------------------------===//
57
58 class RecTy {
59 public:
60 /// Subclass discriminator (for dyn_cast<> et al.)
61 enum RecTyKind {
62 BitRecTyKind,
63 BitsRecTyKind,
64 IntRecTyKind,
65 StringRecTyKind,
66 ListRecTyKind,
67 DagRecTyKind,
68 RecordRecTyKind
69 };
70
71 private:
72 RecTyKind Kind;
73 /// ListRecTy of the list that has elements of this type.
74 ListRecTy *ListTy = nullptr;
75
76 public:
77 RecTy(RecTyKind K) : Kind(K) {}
78 virtual ~RecTy() = default;
79
80 RecTyKind getRecTyKind() const { return Kind; }
81
82 virtual std::string getAsString() const = 0;
83 void print(raw_ostream &OS) const { OS << getAsString(); }
84 void dump() const;
85
86 /// Return true if all values of 'this' type can be converted to the specified
87 /// type.
88 virtual bool typeIsConvertibleTo(const RecTy *RHS) const;
89
90 /// Return true if 'this' type is equal to or a subtype of RHS. For example,
91 /// a bit set is not an int, but they are convertible.
92 virtual bool typeIsA(const RecTy *RHS) const;
93
94 /// Returns the type representing list<thistype>.
95 ListRecTy *getListTy();
96 };
97
98 inline raw_ostream &operator<<(raw_ostream &OS, const RecTy &Ty) {
99 Ty.print(OS);
100 return OS;
101 }
102
103 /// 'bit' - Represent a single bit
104 class BitRecTy : public RecTy {
105 friend detail::RecordContext;
106
107 BitRecTy() : RecTy(BitRecTyKind) {}
108
109 public:
110 static bool classof(const RecTy *RT) {
111 return RT->getRecTyKind() == BitRecTyKind;
112 }
113
114 static BitRecTy *get();
115
116 std::string getAsString() const override { return "bit"; }
117
118 bool typeIsConvertibleTo(const RecTy *RHS) const override;
119 };
120
121 /// 'bits<n>' - Represent a fixed number of bits
122 class BitsRecTy : public RecTy {
123 unsigned Size;
124
125 explicit BitsRecTy(unsigned Sz) : RecTy(BitsRecTyKind), Size(Sz) {}
126
127 public:
128 static bool classof(const RecTy *RT) {
129 return RT->getRecTyKind() == BitsRecTyKind;
130 }
131
132 static BitsRecTy *get(unsigned Sz);
133
134 unsigned getNumBits() const { return Size; }
135
136 std::string getAsString() const override;
137
138 bool typeIsConvertibleTo(const RecTy *RHS) const override;
139
140 bool typeIsA(const RecTy *RHS) const override;
141 };
142
143 /// 'int' - Represent an integer value of no particular size
144 class IntRecTy : public RecTy {
145 friend detail::RecordContext;
146
147 IntRecTy() : RecTy(IntRecTyKind) {}
148
149 public:
150 static bool classof(const RecTy *RT) {
151 return RT->getRecTyKind() == IntRecTyKind;
152 }
153
154 static IntRecTy *get();
155
156 std::string getAsString() const override { return "int"; }
157
158 bool typeIsConvertibleTo(const RecTy *RHS) const override;
159 };
160
161 /// 'string' - Represent an string value
162 class StringRecTy : public RecTy {
163 friend detail::RecordContext;
164
165 StringRecTy() : RecTy(StringRecTyKind) {}
166
167 public:
168 static bool classof(const RecTy *RT) {
169 return RT->getRecTyKind() == StringRecTyKind;
170 }
171
172 static StringRecTy *get();
173
174 std::string getAsString() const override;
175
176 bool typeIsConvertibleTo(const RecTy *RHS) const override;
177 };
178
179 /// 'list<Ty>' - Represent a list of element values, all of which must be of
180 /// the specified type. The type is stored in ElementTy.
181 class ListRecTy : public RecTy {
182 friend ListRecTy *RecTy::getListTy();
183
184 RecTy *ElementTy;
185
186 explicit ListRecTy(RecTy *T) : RecTy(ListRecTyKind), ElementTy(T) {}
187
188 public:
189 static bool classof(const RecTy *RT) {
190 return RT->getRecTyKind() == ListRecTyKind;
191 }
192
193 static ListRecTy *get(RecTy *T) { return T->getListTy(); }
194 RecTy *getElementType() const { return ElementTy; }
195
196 std::string getAsString() const override;
197
198 bool typeIsConvertibleTo(const RecTy *RHS) const override;
199
200 bool typeIsA(const RecTy *RHS) const override;
201 };
202
203 /// 'dag' - Represent a dag fragment
204 class DagRecTy : public RecTy {
205 friend detail::RecordContext;
206
207 DagRecTy() : RecTy(DagRecTyKind) {}
208
209 public:
210 static bool classof(const RecTy *RT) {
211 return RT->getRecTyKind() == DagRecTyKind;
212 }
213
214 static DagRecTy *get();
215
216 std::string getAsString() const override;
217 };
218
219 /// '[classname]' - Type of record values that have zero or more superclasses.
220 ///
221 /// The list of superclasses is non-redundant, i.e. only contains classes that
222 /// are not the superclass of some other listed class.
223 class RecordRecTy final : public RecTy, public FoldingSetNode,
224 public TrailingObjects<RecordRecTy, Record *> {
225 friend class Record;
226 friend detail::RecordContext;
227
228 unsigned NumClasses;
229
230 explicit RecordRecTy(unsigned Num)
231 : RecTy(RecordRecTyKind), NumClasses(Num) {}
232
233 public:
234 RecordRecTy(const RecordRecTy &) = delete;
235 RecordRecTy &operator=(const RecordRecTy &) = delete;
236
237 // Do not use sized deallocation due to trailing objects.
238 void operator delete(void *p) { ::operator delete(p); }
239
240 static bool classof(const RecTy *RT) {
241 return RT->getRecTyKind() == RecordRecTyKind;
242 }
243
244 /// Get the record type with the given non-redundant list of superclasses.
245 static RecordRecTy *get(ArrayRef<Record *> Classes);
246
247 void Profile(FoldingSetNodeID &ID) const;
248
249 ArrayRef<Record *> getClasses() const {
250 return makeArrayRef(getTrailingObjects<Record *>(), NumClasses);
251 }
252
253 using const_record_iterator = Record * const *;
254
255 const_record_iterator classes_begin() const { return getClasses().begin(); }
256 const_record_iterator classes_end() const { return getClasses().end(); }
257
258 std::string getAsString() const override;
259
260 bool isSubClassOf(Record *Class) const;
261 bool typeIsConvertibleTo(const RecTy *RHS) const override;
262
263 bool typeIsA(const RecTy *RHS) const override;
264 };
265
266 /// Find a common type that T1 and T2 convert to.
267 /// Return 0 if no such type exists.
268 RecTy *resolveTypes(RecTy *T1, RecTy *T2);
269
270 //===----------------------------------------------------------------------===//
271 // Initializer Classes
272 //===----------------------------------------------------------------------===//
273
274 class Init {
275 protected:
276 /// Discriminator enum (for isa<>, dyn_cast<>, et al.)
277 ///
278 /// This enum is laid out by a preorder traversal of the inheritance
279 /// hierarchy, and does not contain an entry for abstract classes, as per
280 /// the recommendation in docs/HowToSetUpLLVMStyleRTTI.rst.
281 ///
282 /// We also explicitly include "first" and "last" values for each
283 /// interior node of the inheritance tree, to make it easier to read the
284 /// corresponding classof().
285 ///
286 /// We could pack these a bit tighter by not having the IK_FirstXXXInit
287 /// and IK_LastXXXInit be their own values, but that would degrade
288 /// readability for really no benefit.
289 enum InitKind : uint8_t {
290 IK_First, // unused; silence a spurious warning
291 IK_FirstTypedInit,
292 IK_BitInit,
293 IK_BitsInit,
294 IK_DagInit,
295 IK_DefInit,
296 IK_FieldInit,
297 IK_IntInit,
298 IK_ListInit,
299 IK_FirstOpInit,
300 IK_BinOpInit,
301 IK_TernOpInit,
302 IK_UnOpInit,
303 IK_LastOpInit,
304 IK_CondOpInit,
305 IK_FoldOpInit,
306 IK_IsAOpInit,
307 IK_AnonymousNameInit,
308 IK_StringInit,
309 IK_VarInit,
310 IK_VarListElementInit,
311 IK_VarBitInit,
312 IK_VarDefInit,
313 IK_LastTypedInit,
314 IK_UnsetInit
315 };
316
317 private:
318 const InitKind Kind;
319
320 protected:
321 uint8_t Opc; // Used by UnOpInit, BinOpInit, and TernOpInit
322
323 private:
324 virtual void anchor();
325
326 public:
327 /// Get the kind (type) of the value.
328 InitKind getKind() const { return Kind; }
329
330 protected:
331 explicit Init(InitKind K, uint8_t Opc = 0) : Kind(K), Opc(Opc) {}
332
333 public:
334 Init(const Init &) = delete;
335 Init &operator=(const Init &) = delete;
336 virtual ~Init() = default;
337
338 /// Is this a complete value with no unset (uninitialized) subvalues?
339 virtual bool isComplete() const { return true; }
340
341 /// Is this a concrete and fully resolved value without any references or
342 /// stuck operations? Unset values are concrete.
343 virtual bool isConcrete() const { return false; }
344
345 /// Print this value.
346 void print(raw_ostream &OS) const { OS << getAsString(); }
347
348 /// Convert this value to a literal form.
349 virtual std::string getAsString() const = 0;
350
351 /// Convert this value to a literal form,
352 /// without adding quotes around a string.
353 virtual std::string getAsUnquotedString() const { return getAsString(); }
354
355 /// Debugging method that may be called through a debugger; just
356 /// invokes print on stderr.
357 void dump() const;
358
359 /// If this value is convertible to type \p Ty, return a value whose
360 /// type is \p Ty, generating a !cast operation if required.
361 /// Otherwise, return null.
362 virtual Init *getCastTo(RecTy *Ty) const = 0;
363
364 /// Convert to a value whose type is \p Ty, or return null if this
365 /// is not possible. This can happen if the value's type is convertible
366 /// to \p Ty, but there are unresolved references.
367 virtual Init *convertInitializerTo(RecTy *Ty) const = 0;
368
369 /// This function is used to implement the bit range
370 /// selection operator. Given a value, it selects the specified bits,
371 /// returning them as a new \p Init of type \p bits. If it is not legal
372 /// to use the bit selection operator on this value, null is returned.
373 virtual Init *convertInitializerBitRange(ArrayRef<unsigned> Bits) const {
374 return nullptr;
375 }
376
377 /// This function is used to implement the list slice
378 /// selection operator. Given a value, it selects the specified list
379 /// elements, returning them as a new \p Init of type \p list. If it
380 /// is not legal to use the slice operator, null is returned.
381 virtual Init *convertInitListSlice(ArrayRef<unsigned> Elements) const {
382 return nullptr;
383 }
384
385 /// This function is used to implement the FieldInit class.
386 /// Implementors of this method should return the type of the named
387 /// field if they are of type record.
388 virtual RecTy *getFieldType(StringInit *FieldName) const {
389 return nullptr;
390 }
391
392 /// This function is used by classes that refer to other
393 /// variables which may not be defined at the time the expression is formed.
394 /// If a value is set for the variable later, this method will be called on
395 /// users of the value to allow the value to propagate out.
396 virtual Init *resolveReferences(Resolver &R) const {
397 return const_cast<Init *>(this);
398 }
399
400 /// Get the \p Init value of the specified bit.
401 virtual Init *getBit(unsigned Bit) const = 0;
402 };
403
404 inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) {
405 I.print(OS); return OS;
406 }
407
408 /// This is the common superclass of types that have a specific,
409 /// explicit type, stored in ValueTy.
410 class TypedInit : public Init {
411 RecTy *ValueTy;
412
413 protected:
414 explicit TypedInit(InitKind K, RecTy *T, uint8_t Opc = 0)
415 : Init(K, Opc), ValueTy(T) {}
416
417 public:
418 TypedInit(const TypedInit &) = delete;
419 TypedInit &operator=(const TypedInit &) = delete;
420
421 static bool classof(const Init *I) {
422 return I->getKind() >= IK_FirstTypedInit &&
423 I->getKind() <= IK_LastTypedInit;
424 }
425
426 /// Get the type of the Init as a RecTy.
427 RecTy *getType() const { return ValueTy; }
428
429 Init *getCastTo(RecTy *Ty) const override;
430 Init *convertInitializerTo(RecTy *Ty) const override;
431
432 Init *convertInitializerBitRange(ArrayRef<unsigned> Bits) const override;
433 Init *convertInitListSlice(ArrayRef<unsigned> Elements) const override;
434
435 /// This method is used to implement the FieldInit class.
436 /// Implementors of this method should return the type of the named field if
437 /// they are of type record.
438 RecTy *getFieldType(StringInit *FieldName) const override;
439 };
440
441 /// '?' - Represents an uninitialized value.
442 class UnsetInit : public Init {
443 friend detail::RecordContext;
444
445 UnsetInit() : Init(IK_UnsetInit) {}
446
447 public:
448 UnsetInit(const UnsetInit &) = delete;
449 UnsetInit &operator=(const UnsetInit &) = delete;
450
451 static bool classof(const Init *I) {
452 return I->getKind() == IK_UnsetInit;
453 }
454
455 /// Get the singleton unset Init.
456 static UnsetInit *get();
457
458 Init *getCastTo(RecTy *Ty) const override;
459 Init *convertInitializerTo(RecTy *Ty) const override;
460
461 Init *getBit(unsigned Bit) const override {
462 return const_cast<UnsetInit*>(this);
463 }
464
465 /// Is this a complete value with no unset (uninitialized) subvalues?
466 bool isComplete() const override { return false; }
467
468 bool isConcrete() const override { return true; }
469
470 /// Get the string representation of the Init.
471 std::string getAsString() const override { return "?"; }
472 };
473
474 /// 'true'/'false' - Represent a concrete initializer for a bit.
475 class BitInit final : public TypedInit {
476 friend detail::RecordContext;
477
478 bool Value;
479
480 explicit BitInit(bool V, RecTy *T) : TypedInit(IK_BitInit, T), Value(V) {}
481
482 public:
483 BitInit(const BitInit &) = delete;
484 BitInit &operator=(BitInit &) = delete;
485
486 static bool classof(const Init *I) {
487 return I->getKind() == IK_BitInit;
488 }
489
490 static BitInit *get(bool V);
491
492 bool getValue() const { return Value; }
493
494 Init *convertInitializerTo(RecTy *Ty) const override;
495
496 Init *getBit(unsigned Bit) const override {
497 assert(Bit < 1 && "Bit index out of range!");
498 return const_cast<BitInit*>(this);
499 }
500
501 bool isConcrete() const override { return true; }
502 std::string getAsString() const override { return Value ? "1" : "0"; }
503 };
504
505 /// '{ a, b, c }' - Represents an initializer for a BitsRecTy value.
506 /// It contains a vector of bits, whose size is determined by the type.
507 class BitsInit final : public TypedInit, public FoldingSetNode,
508 public TrailingObjects<BitsInit, Init *> {
509 unsigned NumBits;
510
511 BitsInit(unsigned N)
512 : TypedInit(IK_BitsInit, BitsRecTy::get(N)), NumBits(N) {}
513
514 public:
515 BitsInit(const BitsInit &) = delete;
516 BitsInit &operator=(const BitsInit &) = delete;
517
518 // Do not use sized deallocation due to trailing objects.
519 void operator delete(void *p) { ::operator delete(p); }
520
521 static bool classof(const Init *I) {
522 return I->getKind() == IK_BitsInit;
523 }
524
525 static BitsInit *get(ArrayRef<Init *> Range);
526
527 void Profile(FoldingSetNodeID &ID) const;
528
529 unsigned getNumBits() const { return NumBits; }
530
531 Init *convertInitializerTo(RecTy *Ty) const override;
532 Init *convertInitializerBitRange(ArrayRef<unsigned> Bits) const override;
533
534 bool isComplete() const override {
535 for (unsigned i = 0; i != getNumBits(); ++i)
536 if (!getBit(i)->isComplete()) return false;
537 return true;
538 }
539
540 bool allInComplete() const {
541 for (unsigned i = 0; i != getNumBits(); ++i)
542 if (getBit(i)->isComplete()) return false;
543 return true;
544 }
545
546 bool isConcrete() const override;
547 std::string getAsString() const override;
548
549 Init *resolveReferences(Resolver &R) const override;
550
551 Init *getBit(unsigned Bit) const override {
552 assert(Bit < NumBits && "Bit index out of range!");
553 return getTrailingObjects<Init *>()[Bit];
554 }
555 };
556
557 /// '7' - Represent an initialization by a literal integer value.
558 class IntInit : public TypedInit {
559 int64_t Value;
560
561 explicit IntInit(int64_t V)
562 : TypedInit(IK_IntInit, IntRecTy::get()), Value(V) {}
563
564 public:
565 IntInit(const IntInit &) = delete;
566 IntInit &operator=(const IntInit &) = delete;
567
568 static bool classof(const Init *I) {
569 return I->getKind() == IK_IntInit;
570 }
571
572 static IntInit *get(int64_t V);
573
574 int64_t getValue() const { return Value; }
575
576 Init *convertInitializerTo(RecTy *Ty) const override;
577 Init *convertInitializerBitRange(ArrayRef<unsigned> Bits) const override;
578
579 bool isConcrete() const override { return true; }
580 std::string getAsString() const override;
581
582 Init *getBit(unsigned Bit) const override {
583 return BitInit::get((Value & (1ULL << Bit)) != 0);
584 }
585 };
586
587 /// "anonymous_n" - Represent an anonymous record name
588 class AnonymousNameInit : public TypedInit {
589 unsigned Value;
590
591 explicit AnonymousNameInit(unsigned V)
592 : TypedInit(IK_AnonymousNameInit, StringRecTy::get()), Value(V) {}
593
594 public:
595 AnonymousNameInit(const AnonymousNameInit &) = delete;
596 AnonymousNameInit &operator=(const AnonymousNameInit &) = delete;
597
598 static bool classof(const Init *I) {
599 return I->getKind() == IK_AnonymousNameInit;
600 }
601
602 static AnonymousNameInit *get(unsigned);
603
604 unsigned getValue() const { return Value; }
605
606 StringInit *getNameInit() const;
607
608 std::string getAsString() const override;
609
610 Init *resolveReferences(Resolver &R) const override;
611
612 Init *getBit(unsigned Bit) const override {
613 llvm_unreachable("Illegal bit reference off string");
614 }
615 };
616
617 /// "foo" - Represent an initialization by a string value.
618 class StringInit : public TypedInit {
619 public:
620 enum StringFormat {
621 SF_String, // Format as "text"
622 SF_Code, // Format as [{text}]
623 };
624
625 private:
626 StringRef Value;
627 StringFormat Format;
628
629 explicit StringInit(StringRef V, StringFormat Fmt)
630 : TypedInit(IK_StringInit, StringRecTy::get()), Value(V), Format(Fmt) {}
631
632 public:
633 StringInit(const StringInit &) = delete;
634 StringInit &operator=(const StringInit &) = delete;
635
636 static bool classof(const Init *I) {
637 return I->getKind() == IK_StringInit;
638 }
639
640 static StringInit *get(StringRef, StringFormat Fmt = SF_String);
641
642 static StringFormat determineFormat(StringFormat Fmt1, StringFormat Fmt2) {
643 return (Fmt1 == SF_Code || Fmt2 == SF_Code) ? SF_Code : SF_String;
644 }
645
646 StringRef getValue() const { return Value; }
647 StringFormat getFormat() const { return Format; }
648 bool hasCodeFormat() const { return Format == SF_Code; }
649
650 Init *convertInitializerTo(RecTy *Ty) const override;
651
652 bool isConcrete() const override { return true; }
653
654 std::string getAsString() const override {
655 if (Format == SF_String)
656 return "\"" + Value.str() + "\"";
657 else
658 return "[{" + Value.str() + "}]";
659 }
660
661 std::string getAsUnquotedString() const override {
662 return std::string(Value);
663 }
664
665 Init *getBit(unsigned Bit) const override {
666 llvm_unreachable("Illegal bit reference off string");
667 }
668 };
669
670 /// [AL, AH, CL] - Represent a list of defs
671 ///
672 class ListInit final : public TypedInit, public FoldingSetNode,
673 public TrailingObjects<ListInit, Init *> {
674 unsigned NumValues;
675
676 public:
677 using const_iterator = Init *const *;
678
679 private:
680 explicit ListInit(unsigned N, RecTy *EltTy)
681 : TypedInit(IK_ListInit, ListRecTy::get(EltTy)), NumValues(N) {}
682
683 public:
684 ListInit(const ListInit &) = delete;
685 ListInit &operator=(const ListInit &) = delete;
686
687 // Do not use sized deallocation due to trailing objects.
688 void operator delete(void *p) { ::operator delete(p); }
689
690 static bool classof(const Init *I) {
691 return I->getKind() == IK_ListInit;
692 }
693 static ListInit *get(ArrayRef<Init *> Range, RecTy *EltTy);
694
695 void Profile(FoldingSetNodeID &ID) const;
696
697 Init *getElement(unsigned i) const {
698 assert(i < NumValues && "List element index out of range!");
699 return getTrailingObjects<Init *>()[i];
700 }
701 RecTy *getElementType() const {
702 return cast<ListRecTy>(getType())->getElementType();
703 }
704
705 Record *getElementAsRecord(unsigned i) const;
706
707 Init *convertInitListSlice(ArrayRef<unsigned> Elements) const override;
708
709 Init *convertInitializerTo(RecTy *Ty) const override;
710
711 /// This method is used by classes that refer to other
712 /// variables which may not be defined at the time they expression is formed.
713 /// If a value is set for the variable later, this method will be called on
714 /// users of the value to allow the value to propagate out.
715 ///
716 Init *resolveReferences(Resolver &R) const override;
717
718 bool isComplete() const override;
719 bool isConcrete() const override;
720 std::string getAsString() const override;
721
722 ArrayRef<Init*> getValues() const {
723 return makeArrayRef(getTrailingObjects<Init *>(), NumValues);
724 }
725
726 const_iterator begin() const { return getTrailingObjects<Init *>(); }
727 const_iterator end () const { return begin() + NumValues; }
728
729 size_t size () const { return NumValues; }
730 bool empty() const { return NumValues == 0; }
731
732 Init *getBit(unsigned Bit) const override {
733 llvm_unreachable("Illegal bit reference off list");
734 }
735 };
736
737 /// Base class for operators
738 ///
739 class OpInit : public TypedInit {
740 protected:
741 explicit OpInit(InitKind K, RecTy *Type, uint8_t Opc)
742 : TypedInit(K, Type, Opc) {}
743
744 public:
745 OpInit(const OpInit &) = delete;
746 OpInit &operator=(OpInit &) = delete;
747
748 static bool classof(const Init *I) {
749 return I->getKind() >= IK_FirstOpInit &&
750 I->getKind() <= IK_LastOpInit;
751 }
752
753 // Clone - Clone this operator, replacing arguments with the new list
754 virtual OpInit *clone(ArrayRef<Init *> Operands) const = 0;
755
756 virtual unsigned getNumOperands() const = 0;
757 virtual Init *getOperand(unsigned i) const = 0;
758
759 Init *getBit(unsigned Bit) const override;
760 };
761
762 /// !op (X) - Transform an init.
763 ///
764 class UnOpInit : public OpInit, public FoldingSetNode {
765 public:
766 enum UnaryOp : uint8_t { CAST, NOT, HEAD, TAIL, SIZE, EMPTY, GETDAGOP };
767
768 private:
769 Init *LHS;
770
771 UnOpInit(UnaryOp opc, Init *lhs, RecTy *Type)
772 : OpInit(IK_UnOpInit, Type, opc), LHS(lhs) {}
773
774 public:
775 UnOpInit(const UnOpInit &) = delete;
776 UnOpInit &operator=(const UnOpInit &) = delete;
777
778 static bool classof(const Init *I) {
779 return I->getKind() == IK_UnOpInit;
780 }
781
782 static UnOpInit *get(UnaryOp opc, Init *lhs, RecTy *Type);
783
784 void Profile(FoldingSetNodeID &ID) const;
785
786 // Clone - Clone this operator, replacing arguments with the new list
787 OpInit *clone(ArrayRef<Init *> Operands) const override {
788 assert(Operands.size() == 1 &&
789 "Wrong number of operands for unary operation");
790 return UnOpInit::get(getOpcode(), *Operands.begin(), getType());
791 }
792
793 unsigned getNumOperands() const override { return 1; }
794
795 Init *getOperand(unsigned i) const override {
796 assert(i == 0 && "Invalid operand id for unary operator");
797 return getOperand();
798 }
799
800 UnaryOp getOpcode() const { return (UnaryOp)Opc; }
801 Init *getOperand() const { return LHS; }
802
803 // Fold - If possible, fold this to a simpler init. Return this if not
804 // possible to fold.
805 Init *Fold(Record *CurRec, bool IsFinal = false) const;
806
807 Init *resolveReferences(Resolver &R) const override;
808
809 std::string getAsString() const override;
810 };
811
812 /// !op (X, Y) - Combine two inits.
813 class BinOpInit : public OpInit, public FoldingSetNode {
814 public:
815 enum BinaryOp : uint8_t { ADD, SUB, MUL, AND, OR, XOR, SHL, SRA, SRL, LISTCONCAT,
816 LISTSPLAT, STRCONCAT, INTERLEAVE, CONCAT, EQ,
817 NE, LE, LT, GE, GT, SETDAGOP };
818
819 private:
820 Init *LHS, *RHS;
821
822 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type) :
823 OpInit(IK_BinOpInit, Type, opc), LHS(lhs), RHS(rhs) {}
824
825 public:
826 BinOpInit(const BinOpInit &) = delete;
827 BinOpInit &operator=(const BinOpInit &) = delete;
828
829 static bool classof(const Init *I) {
830 return I->getKind() == IK_BinOpInit;
831 }
832
833 static BinOpInit *get(BinaryOp opc, Init *lhs, Init *rhs,
834 RecTy *Type);
835 static Init *getStrConcat(Init *lhs, Init *rhs);
836 static Init *getListConcat(TypedInit *lhs, Init *rhs);
837
838 void Profile(FoldingSetNodeID &ID) const;
839
840 // Clone - Clone this operator, replacing arguments with the new list
841 OpInit *clone(ArrayRef<Init *> Operands) const override {
842 assert(Operands.size() == 2 &&
843 "Wrong number of operands for binary operation");
844 return BinOpInit::get(getOpcode(), Operands[0], Operands[1], getType());
845 }
846
847 unsigned getNumOperands() const override { return 2; }
848 Init *getOperand(unsigned i) const override {
849 switch (i) {
850 default: llvm_unreachable("Invalid operand id for binary operator");
851 case 0: return getLHS();
852 case 1: return getRHS();
853 }
854 }
855
856 BinaryOp getOpcode() const { return (BinaryOp)Opc; }
857 Init *getLHS() const { return LHS; }
858 Init *getRHS() const { return RHS; }
859
860 // Fold - If possible, fold this to a simpler init. Return this if not
861 // possible to fold.
862 Init *Fold(Record *CurRec) const;
863
864 Init *resolveReferences(Resolver &R) const override;
865
866 std::string getAsString() const override;
867 };
868
869 /// !op (X, Y, Z) - Combine two inits.
870 class TernOpInit : public OpInit, public FoldingSetNode {
871 public:
872 enum TernaryOp : uint8_t { SUBST, FOREACH, FILTER, IF, DAG, SUBSTR, FIND };
873
874 private:
875 Init *LHS, *MHS, *RHS;
876
877 TernOpInit(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs,
878 RecTy *Type) :
879 OpInit(IK_TernOpInit, Type, opc), LHS(lhs), MHS(mhs), RHS(rhs) {}
880
881 public:
882 TernOpInit(const TernOpInit &) = delete;
883 TernOpInit &operator=(const TernOpInit &) = delete;
884
885 static bool classof(const Init *I) {
886 return I->getKind() == IK_TernOpInit;
887 }
888
889 static TernOpInit *get(TernaryOp opc, Init *lhs,
890 Init *mhs, Init *rhs,
891 RecTy *Type);
892
893 void Profile(FoldingSetNodeID &ID) const;
894
895 // Clone - Clone this operator, replacing arguments with the new list
896 OpInit *clone(ArrayRef<Init *> Operands) const override {
897 assert(Operands.size() == 3 &&
898 "Wrong number of operands for ternary operation");
899 return TernOpInit::get(getOpcode(), Operands[0], Operands[1], Operands[2],
900 getType());
901 }
902
903 unsigned getNumOperands() const override { return 3; }
904 Init *getOperand(unsigned i) const override {
905 switch (i) {
906 default: llvm_unreachable("Invalid operand id for ternary operator");
907 case 0: return getLHS();
908 case 1: return getMHS();
909 case 2: return getRHS();
910 }
911 }
912
913 TernaryOp getOpcode() const { return (TernaryOp)Opc; }
914 Init *getLHS() const { return LHS; }
915 Init *getMHS() const { return MHS; }
916 Init *getRHS() const { return RHS; }
917
918 // Fold - If possible, fold this to a simpler init. Return this if not
919 // possible to fold.
920 Init *Fold(Record *CurRec) const;
921
922 bool isComplete() const override {
923 return LHS->isComplete() && MHS->isComplete() && RHS->isComplete();
924 }
925
926 Init *resolveReferences(Resolver &R) const override;
927
928 std::string getAsString() const override;
929 };
930
931 /// !cond(condition_1: value1, ... , condition_n: value)
932 /// Selects the first value for which condition is true.
933 /// Otherwise reports an error.
934 class CondOpInit final : public TypedInit, public FoldingSetNode,
935 public TrailingObjects<CondOpInit, Init *> {
936 unsigned NumConds;
937 RecTy *ValType;
938
939 CondOpInit(unsigned NC, RecTy *Type)
940 : TypedInit(IK_CondOpInit, Type),
941 NumConds(NC), ValType(Type) {}
942
943 size_t numTrailingObjects(OverloadToken<Init *>) const {
944 return 2*NumConds;
945 }
946
947 public:
948 CondOpInit(const CondOpInit &) = delete;
949 CondOpInit &operator=(const CondOpInit &) = delete;
950
951 static bool classof(const Init *I) {
952 return I->getKind() == IK_CondOpInit;
953 }
954
955 static CondOpInit *get(ArrayRef<Init*> C, ArrayRef<Init*> V,
956 RecTy *Type);
957
958 void Profile(FoldingSetNodeID &ID) const;
959
960 RecTy *getValType() const { return ValType; }
961
962 unsigned getNumConds() const { return NumConds; }
963
964 Init *getCond(unsigned Num) const {
965 assert(Num < NumConds && "Condition number out of range!");
966 return getTrailingObjects<Init *>()[Num];
967 }
968
969 Init *getVal(unsigned Num) const {
970 assert(Num < NumConds && "Val number out of range!");
971 return getTrailingObjects<Init *>()[Num+NumConds];
972 }
973
974 ArrayRef<Init *> getConds() const {
975 return makeArrayRef(getTrailingObjects<Init *>(), NumConds);
976 }
977
978 ArrayRef<Init *> getVals() const {
979 return makeArrayRef(getTrailingObjects<Init *>()+NumConds, NumConds);
980 }
981
982 Init *Fold(Record *CurRec) const;
983
984 Init *resolveReferences(Resolver &R) const override;
985
986 bool isConcrete() const override;
987 bool isComplete() const override;
988 std::string getAsString() const override;
989
990 using const_case_iterator = SmallVectorImpl<Init*>::const_iterator;
991 using const_val_iterator = SmallVectorImpl<Init*>::const_iterator;
992
993 inline const_case_iterator arg_begin() const { return getConds().begin(); }
994 inline const_case_iterator arg_end () const { return getConds().end(); }
995
996 inline size_t case_size () const { return NumConds; }
997 inline bool case_empty() const { return NumConds == 0; }
998
999 inline const_val_iterator name_begin() const { return getVals().begin();}
1000 inline const_val_iterator name_end () const { return getVals().end(); }
1001
1002 inline size_t val_size () const { return NumConds; }
1003 inline bool val_empty() const { return NumConds == 0; }
1004
1005 Init *getBit(unsigned Bit) const override;
1006 };
1007
1008 /// !foldl (a, b, expr, start, lst) - Fold over a list.
1009 class FoldOpInit : public TypedInit, public FoldingSetNode {
1010 private:
1011 Init *Start;
1012 Init *List;
1013 Init *A;
1014 Init *B;
1015 Init *Expr;
1016
1017 FoldOpInit(Init *Start, Init *List, Init *A, Init *B, Init *Expr, RecTy *Type)
1018 : TypedInit(IK_FoldOpInit, Type), Start(Start), List(List), A(A), B(B),
1019 Expr(Expr) {}
1020
1021 public:
1022 FoldOpInit(const FoldOpInit &) = delete;
1023 FoldOpInit &operator=(const FoldOpInit &) = delete;
1024
1025 static bool classof(const Init *I) { return I->getKind() == IK_FoldOpInit; }
1026
1027 static FoldOpInit *get(Init *Start, Init *List, Init *A, Init *B, Init *Expr,
1028 RecTy *Type);
1029
1030 void Profile(FoldingSetNodeID &ID) const;
1031
1032 // Fold - If possible, fold this to a simpler init. Return this if not
1033 // possible to fold.
1034 Init *Fold(Record *CurRec) const;
1035
1036 bool isComplete() const override { return false; }
1037
1038 Init *resolveReferences(Resolver &R) const override;
1039
1040 Init *getBit(unsigned Bit) const override;
1041
1042 std::string getAsString() const override;
1043 };
1044
1045 /// !isa<type>(expr) - Dynamically determine the type of an expression.
1046 class IsAOpInit : public TypedInit, public FoldingSetNode {
1047 private:
1048 RecTy *CheckType;
1049 Init *Expr;
1050
1051 IsAOpInit(RecTy *CheckType, Init *Expr)
1052 : TypedInit(IK_IsAOpInit, IntRecTy::get()), CheckType(CheckType),
1053 Expr(Expr) {}
1054
1055 public:
1056 IsAOpInit(const IsAOpInit &) = delete;
1057 IsAOpInit &operator=(const IsAOpInit &) = delete;
1058
1059 static bool classof(const Init *I) { return I->getKind() == IK_IsAOpInit; }
1060
1061 static IsAOpInit *get(RecTy *CheckType, Init *Expr);
1062
1063 void Profile(FoldingSetNodeID &ID) const;
1064
1065 // Fold - If possible, fold this to a simpler init. Return this if not
1066 // possible to fold.
1067 Init *Fold() const;
1068
1069 bool isComplete() const override { return false; }
1070
1071 Init *resolveReferences(Resolver &R) const override;
1072
1073 Init *getBit(unsigned Bit) const override;
1074
1075 std::string getAsString() const override;
1076 };
1077
1078 /// 'Opcode' - Represent a reference to an entire variable object.
1079 class VarInit : public TypedInit {
1080 Init *VarName;
1081
1082 explicit VarInit(Init *VN, RecTy *T)
1083 : TypedInit(IK_VarInit, T), VarName(VN) {}
1084
1085 public:
1086 VarInit(const VarInit &) = delete;
1087 VarInit &operator=(const VarInit &) = delete;
1088
1089 static bool classof(const Init *I) {
1090 return I->getKind() == IK_VarInit;
1091 }
1092
1093 static VarInit *get(StringRef VN, RecTy *T);
1094 static VarInit *get(Init *VN, RecTy *T);
1095
1096 StringRef getName() const;
1097 Init *getNameInit() const { return VarName; }
1098
1099 std::string getNameInitAsString() const {
1100 return getNameInit()->getAsUnquotedString();
1101 }
1102
1103 /// This method is used by classes that refer to other
1104 /// variables which may not be defined at the time they expression is formed.
1105 /// If a value is set for the variable later, this method will be called on
1106 /// users of the value to allow the value to propagate out.
1107 ///
1108 Init *resolveReferences(Resolver &R) const override;
1109
1110 Init *getBit(unsigned Bit) const override;
1111
1112 std::string getAsString() const override { return std::string(getName()); }
1113 };
1114
1115 /// Opcode{0} - Represent access to one bit of a variable or field.
1116 class VarBitInit final : public TypedInit {
1117 TypedInit *TI;
1118 unsigned Bit;
1119
1120 VarBitInit(TypedInit *T, unsigned B)
1121 : TypedInit(IK_VarBitInit, BitRecTy::get()), TI(T), Bit(B) {
1122 assert(T->getType() &&
1123 (isa<IntRecTy>(T->getType()) ||
1124 (isa<BitsRecTy>(T->getType()) &&
1125 cast<BitsRecTy>(T->getType())->getNumBits() > B)) &&
1126 "Illegal VarBitInit expression!");
1127 }
1128
1129 public:
1130 VarBitInit(const VarBitInit &) = delete;
1131 VarBitInit &operator=(const VarBitInit &) = delete;
1132
1133 static bool classof(const Init *I) {
1134 return I->getKind() == IK_VarBitInit;
1135 }
1136
1137 static VarBitInit *get(TypedInit *T, unsigned B);
1138
1139 Init *getBitVar() const { return TI; }
1140 unsigned getBitNum() const { return Bit; }
1141
1142 std::string getAsString() const override;
1143 Init *resolveReferences(Resolver &R) const override;
1144
1145 Init *getBit(unsigned B) const override {
1146 assert(B < 1 && "Bit index out of range!");
1147 return const_cast<VarBitInit*>(this);
1148 }
1149 };
1150
1151 /// List[4] - Represent access to one element of a var or
1152 /// field.
1153 class VarListElementInit : public TypedInit {
1154 TypedInit *TI;
1155 unsigned Element;
1156
1157 VarListElementInit(TypedInit *T, unsigned E)
1158 : TypedInit(IK_VarListElementInit,
1159 cast<ListRecTy>(T->getType())->getElementType()),
1160 TI(T), Element(E) {
1161 assert(T->getType() && isa<ListRecTy>(T->getType()) &&
1162 "Illegal VarBitInit expression!");
1163 }
1164
1165 public:
1166 VarListElementInit(const VarListElementInit &) = delete;
1167 VarListElementInit &operator=(const VarListElementInit &) = delete;
1168
1169 static bool classof(const Init *I) {
1170 return I->getKind() == IK_VarListElementInit;
1171 }
1172
1173 static VarListElementInit *get(TypedInit *T, unsigned E);
1174
1175 TypedInit *getVariable() const { return TI; }
1176 unsigned getElementNum() const { return Element; }
1177
1178 std::string getAsString() const override;
1179 Init *resolveReferences(Resolver &R) const override;
1180
1181 Init *getBit(unsigned Bit) const override;
1182 };
1183
1184 /// AL - Represent a reference to a 'def' in the description
1185 class DefInit : public TypedInit {
1186 friend class Record;
1187
1188 Record *Def;
1189
1190 explicit DefInit(Record *D);
1191
1192 public:
1193 DefInit(const DefInit &) = delete;
1194 DefInit &operator=(const DefInit &) = delete;
1195
1196 static bool classof(const Init *I) {
1197 return I->getKind() == IK_DefInit;
1198 }
1199
1200 static DefInit *get(Record*);
1201
1202 Init *convertInitializerTo(RecTy *Ty) const override;
1203
1204 Record *getDef() const { return Def; }
1205
1206 //virtual Init *convertInitializerBitRange(ArrayRef<unsigned> Bits);
1207
1208 RecTy *getFieldType(StringInit *FieldName) const override;
1209
1210 bool isConcrete() const override { return true; }
1211 std::string getAsString() const override;
1212
1213 Init *getBit(unsigned Bit) const override {
1214 llvm_unreachable("Illegal bit reference off def");
1215 }
1216 };
1217
1218 /// classname<targs...> - Represent an uninstantiated anonymous class
1219 /// instantiation.
1220 class VarDefInit final : public TypedInit, public FoldingSetNode,
1221 public TrailingObjects<VarDefInit, Init *> {
1222 Record *Class;
1223 DefInit *Def = nullptr; // after instantiation
1224 unsigned NumArgs;
1225
1226 explicit VarDefInit(Record *Class, unsigned N)
1227 : TypedInit(IK_VarDefInit, RecordRecTy::get(Class)), Class(Class), NumArgs(N) {}
1228
1229 DefInit *instantiate();
1230
1231 public:
1232 VarDefInit(const VarDefInit &) = delete;
1233 VarDefInit &operator=(const VarDefInit &) = delete;
1234
1235 // Do not use sized deallocation due to trailing objects.
1236 void operator delete(void *p) { ::operator delete(p); }
1237
1238 static bool classof(const Init *I) {
1239 return I->getKind() == IK_VarDefInit;
1240 }
1241 static VarDefInit *get(Record *Class, ArrayRef<Init *> Args);
1242
1243 void Profile(FoldingSetNodeID &ID) const;
1244
1245 Init *resolveReferences(Resolver &R) const override;
1246 Init *Fold() const;
1247
1248 std::string getAsString() const override;
1249
1250 Init *getArg(unsigned i) const {
1251 assert(i < NumArgs && "Argument index out of range!");
1252 return getTrailingObjects<Init *>()[i];
1253 }
1254
1255 using const_iterator = Init *const *;
1256
1257 const_iterator args_begin() const { return getTrailingObjects<Init *>(); }
1258 const_iterator args_end () const { return args_begin() + NumArgs; }
1259
1260 size_t args_size () const { return NumArgs; }
1261 bool args_empty() const { return NumArgs == 0; }
1262
1263 ArrayRef<Init *> args() const { return makeArrayRef(args_begin(), NumArgs); }
1264
1265 Init *getBit(unsigned Bit) const override {
1266 llvm_unreachable("Illegal bit reference off anonymous def");
1267 }
1268 };
1269
1270 /// X.Y - Represent a reference to a subfield of a variable
1271 class FieldInit : public TypedInit {
1272 Init *Rec; // Record we are referring to
1273 StringInit *FieldName; // Field we are accessing
1274
1275 FieldInit(Init *R, StringInit *FN)
1276 : TypedInit(IK_FieldInit, R->getFieldType(FN)), Rec(R), FieldName(FN) {
1277 #ifndef NDEBUG
1278 if (!getType()) {
1279 llvm::errs() << "In Record = " << Rec->getAsString()
1280 << ", got FieldName = " << *FieldName
1281 << " with non-record type!\n";
1282 llvm_unreachable("FieldInit with non-record type!");
1283 }
1284 #endif
1285 }
1286
1287 public:
1288 FieldInit(const FieldInit &) = delete;
1289 FieldInit &operator=(const FieldInit &) = delete;
1290
1291 static bool classof(const Init *I) {
1292 return I->getKind() == IK_FieldInit;
1293 }
1294
1295 static FieldInit *get(Init *R, StringInit *FN);
1296
1297 Init *getRecord() const { return Rec; }
1298 StringInit *getFieldName() const { return FieldName; }
1299
1300 Init *getBit(unsigned Bit) const override;
1301
1302 Init *resolveReferences(Resolver &R) const override;
1303 Init *Fold(Record *CurRec) const;
1304
1305 bool isConcrete() const override;
1306 std::string getAsString() const override {
1307 return Rec->getAsString() + "." + FieldName->getValue().str();
1308 }
1309 };
1310
1311 /// (v a, b) - Represent a DAG tree value. DAG inits are required
1312 /// to have at least one value then a (possibly empty) list of arguments. Each
1313 /// argument can have a name associated with it.
1314 class DagInit final : public TypedInit, public FoldingSetNode,
1315 public TrailingObjects<DagInit, Init *, StringInit *> {
1316 friend TrailingObjects;
1317
1318 Init *Val;
1319 StringInit *ValName;
1320 unsigned NumArgs;
1321 unsigned NumArgNames;
1322
1323 DagInit(Init *V, StringInit *VN, unsigned NumArgs, unsigned NumArgNames)
1324 : TypedInit(IK_DagInit, DagRecTy::get()), Val(V), ValName(VN),
1325 NumArgs(NumArgs), NumArgNames(NumArgNames) {}
1326
1327 size_t numTrailingObjects(OverloadToken<Init *>) const { return NumArgs; }
1328
1329 public:
1330 DagInit(const DagInit &) = delete;
1331 DagInit &operator=(const DagInit &) = delete;
1332
1333 static bool classof(const Init *I) {
1334 return I->getKind() == IK_DagInit;
1335 }
1336
1337 static DagInit *get(Init *V, StringInit *VN, ArrayRef<Init *> ArgRange,
1338 ArrayRef<StringInit*> NameRange);
1339 static DagInit *get(Init *V, StringInit *VN,
1340 ArrayRef<std::pair<Init*, StringInit*>> Args);
1341
1342 void Profile(FoldingSetNodeID &ID) const;
1343
1344 Init *getOperator() const { return Val; }
1345 Record *getOperatorAsDef(ArrayRef<SMLoc> Loc) const;
1346
1347 StringInit *getName() const { return ValName; }
1348
1349 StringRef getNameStr() const {
1350 return ValName ? ValName->getValue() : StringRef();
1351 }
1352
1353 unsigned getNumArgs() const { return NumArgs; }
1354
1355 Init *getArg(unsigned Num) const {
1356 assert(Num < NumArgs && "Arg number out of range!");
1357 return getTrailingObjects<Init *>()[Num];
1358 }
1359
1360 StringInit *getArgName(unsigned Num) const {
1361 assert(Num < NumArgNames && "Arg number out of range!");
1362 return getTrailingObjects<StringInit *>()[Num];
1363 }
1364
1365 StringRef getArgNameStr(unsigned Num) const {
1366 StringInit *Init = getArgName(Num);
1367 return Init ? Init->getValue() : StringRef();
1368 }
1369
1370 ArrayRef<Init *> getArgs() const {
1371 return makeArrayRef(getTrailingObjects<Init *>(), NumArgs);
1372 }
1373
1374 ArrayRef<StringInit *> getArgNames() const {
1375 return makeArrayRef(getTrailingObjects<StringInit *>(), NumArgNames);
1376 }
1377
1378 Init *resolveReferences(Resolver &R) const override;
1379
1380 bool isConcrete() const override;
1381 std::string getAsString() const override;
1382
1383 using const_arg_iterator = SmallVectorImpl<Init*>::const_iterator;
1384 using const_name_iterator = SmallVectorImpl<StringInit*>::const_iterator;
1385
1386 inline const_arg_iterator arg_begin() const { return getArgs().begin(); }
1387 inline const_arg_iterator arg_end () const { return getArgs().end(); }
1388
1389 inline size_t arg_size () const { return NumArgs; }
1390 inline bool arg_empty() const { return NumArgs == 0; }
1391
1392 inline const_name_iterator name_begin() const { return getArgNames().begin();}
1393 inline const_name_iterator name_end () const { return getArgNames().end(); }
1394
1395 inline size_t name_size () const { return NumArgNames; }
1396 inline bool name_empty() const { return NumArgNames == 0; }
1397
1398 Init *getBit(unsigned Bit) const override {
1399 llvm_unreachable("Illegal bit reference off dag");
1400 }
1401 };
1402
1403 //===----------------------------------------------------------------------===//
1404 // High-Level Classes
1405 //===----------------------------------------------------------------------===//
1406
1407 /// This class represents a field in a record, including its name, type,
1408 /// value, and source location.
1409 class RecordVal {
1410 friend class Record;
1411
1412 public:
1413 enum FieldKind {
1414 FK_Normal, // A normal record field.
1415 FK_NonconcreteOK, // A field that can be nonconcrete ('field' keyword).
1416 FK_TemplateArg, // A template argument.
1417 };
1418
1419 private:
1420 Init *Name;
1421 SMLoc Loc; // Source location of definition of name.
1422 PointerIntPair<RecTy *, 2, FieldKind> TyAndKind;
1423 Init *Value;
1424 bool IsUsed = false;
1425
1426 public:
1427 RecordVal(Init *N, RecTy *T, FieldKind K);
1428 RecordVal(Init *N, SMLoc Loc, RecTy *T, FieldKind K);
1429
1430 /// Get the name of the field as a StringRef.
1431 StringRef getName() const;
1432
1433 /// Get the name of the field as an Init.
1434 Init *getNameInit() const { return Name; }
1435
1436 /// Get the name of the field as a std::string.
1437 std::string getNameInitAsString() const {
1438 return getNameInit()->getAsUnquotedString();
1439 }
1440
1441 /// Get the source location of the point where the field was defined.
1442 const SMLoc &getLoc() const { return Loc; }
1443
1444 /// Is this a field where nonconcrete values are okay?
1445 bool isNonconcreteOK() const {
1446 return TyAndKind.getInt() == FK_NonconcreteOK;
1447 }
1448
1449 /// Is this a template argument?
1450 bool isTemplateArg() const {
1451 return TyAndKind.getInt() == FK_TemplateArg;
1452 }
1453
1454 /// Get the type of the field value as a RecTy.
1455 RecTy *getType() const { return TyAndKind.getPointer(); }
1456
1457 /// Get the type of the field for printing purposes.
1458 std::string getPrintType() const;
1459
1460 /// Get the value of the field as an Init.
1461 Init *getValue() const { return Value; }
1462
1463 /// Set the value of the field from an Init.
1464 bool setValue(Init *V);
1465
1466 /// Set the value and source location of the field.
1467 bool setValue(Init *V, SMLoc NewLoc);
1468
1469 /// Whether this value is used. Useful for reporting warnings, for example
1470 /// when a template argument is unused.
1471 void setUsed(bool Used) { IsUsed = Used; }
1472 bool isUsed() const { return IsUsed; }
1473
1474 void dump() const;
1475
1476 /// Print the value to an output stream, possibly with a semicolon.
1477 void print(raw_ostream &OS, bool PrintSem = true) const;
1478 };
1479
1480 inline raw_ostream &operator<<(raw_ostream &OS, const RecordVal &RV) {
1481 RV.print(OS << " ");
1482 return OS;
1483 }
1484
1485 class Record {
1486 public:
1487 struct AssertionInfo {
1488 SMLoc Loc;
1489 Init *Condition;
1490 Init *Message;
1491
1492 // User-defined constructor to support std::make_unique(). It can be
1493 // removed in C++20 when braced initialization is supported.
1494 AssertionInfo(SMLoc Loc, Init *Condition, Init *Message)
1495 : Loc(Loc), Condition(Condition), Message(Message) {}
1496 };
1497
1498 private:
1499 Init *Name;
1500 // Location where record was instantiated, followed by the location of
1501 // multiclass prototypes used.
1502 SmallVector<SMLoc, 4> Locs;
1503 SmallVector<Init *, 0> TemplateArgs;
1504 SmallVector<RecordVal, 0> Values;
1505 SmallVector<AssertionInfo, 0> Assertions;
1506
1507 // All superclasses in the inheritance forest in post-order (yes, it
1508 // must be a forest; diamond-shaped inheritance is not allowed).
1509 SmallVector<std::pair<Record *, SMRange>, 0> SuperClasses;
1510
1511 // Tracks Record instances. Not owned by Record.
1512 RecordKeeper &TrackedRecords;
1513
1514 // The DefInit corresponding to this record.
1515 DefInit *CorrespondingDefInit = nullptr;
1516
1517 // Unique record ID.
1518 unsigned ID;
1519
1520 bool IsAnonymous;
1521 bool IsClass;
1522
1523 void checkName();
1524
1525 public:
1526 // Constructs a record.
1527 explicit Record(Init *N, ArrayRef<SMLoc> locs, RecordKeeper &records,
1528 bool Anonymous = false, bool Class = false)
1529 : Name(N), Locs(locs.begin(), locs.end()), TrackedRecords(records),
1530 ID(getNewUID()), IsAnonymous(Anonymous), IsClass(Class) {
1531 checkName();
1532 }
1533
1534 explicit Record(StringRef N, ArrayRef<SMLoc> locs, RecordKeeper &records,
1535 bool Class = false)
1536 : Record(StringInit::get(N), locs, records, false, Class) {}
1537
1538 // When copy-constructing a Record, we must still guarantee a globally unique
1539 // ID number. Don't copy CorrespondingDefInit either, since it's owned by the
1540 // original record. All other fields can be copied normally.
1541 Record(const Record &O)
1542 : Name(O.Name), Locs(O.Locs), TemplateArgs(O.TemplateArgs),
1543 Values(O.Values), Assertions(O.Assertions),
1544 SuperClasses(O.SuperClasses), TrackedRecords(O.TrackedRecords),
1545 ID(getNewUID()), IsAnonymous(O.IsAnonymous), IsClass(O.IsClass) {}
1546
1547 static unsigned getNewUID();
1548
1549 unsigned getID() const { return ID; }
1550
1551 StringRef getName() const { return cast<StringInit>(Name)->getValue(); }
1552
1553 Init *getNameInit() const {
1554 return Name;
1555 }
1556
1557 const std::string getNameInitAsString() const {
1558 return getNameInit()->getAsUnquotedString();
1559 }
1560
1561 void setName(Init *Name); // Also updates RecordKeeper.
1562
1563 ArrayRef<SMLoc> getLoc() const { return Locs; }
1564 void appendLoc(SMLoc Loc) { Locs.push_back(Loc); }
1565
1566 // Make the type that this record should have based on its superclasses.
1567 RecordRecTy *getType();
1568
1569 /// get the corresponding DefInit.
1570 DefInit *getDefInit();
1571
1572 bool isClass() const { return IsClass; }
1573
1574 ArrayRef<Init *> getTemplateArgs() const {
1575 return TemplateArgs;
1576 }
1577
1578 ArrayRef<RecordVal> getValues() const { return Values; }
1579
1580 ArrayRef<AssertionInfo> getAssertions() const { return Assertions; }
1581
1582 ArrayRef<std::pair<Record *, SMRange>> getSuperClasses() const {
1583 return SuperClasses;
1584 }
1585
1586 /// Determine whether this record has the specified direct superclass.
1587 bool hasDirectSuperClass(const Record *SuperClass) const;
1588
1589 /// Append the direct superclasses of this record to Classes.
1590 void getDirectSuperClasses(SmallVectorImpl<Record *> &Classes) const;
1591
1592 bool isTemplateArg(Init *Name) const {
1593 return llvm::is_contained(TemplateArgs, Name);
1594 }
1595
1596 const RecordVal *getValue(const Init *Name) const {
1597 for (const RecordVal &Val : Values)
1598 if (Val.Name == Name) return &Val;
1599 return nullptr;
1600 }
1601
1602 const RecordVal *getValue(StringRef Name) const {
1603 return getValue(StringInit::get(Name));
1604 }
1605
1606 RecordVal *getValue(const Init *Name) {
1607 return const_cast<RecordVal *>(static_cast<const Record *>(this)->getValue(Name));
1608 }
1609
1610 RecordVal *getValue(StringRef Name) {
1611 return const_cast<RecordVal *>(static_cast<const Record *>(this)->getValue(Name));
1612 }
1613
1614 void addTemplateArg(Init *Name) {
1615 assert(!isTemplateArg(Name) && "Template arg already defined!");
1616 TemplateArgs.push_back(Name);
1617 }
1618
1619 void addValue(const RecordVal &RV) {
1620 assert(getValue(RV.getNameInit()) == nullptr && "Value already added!");
1621 Values.push_back(RV);
1622 }
1623
1624 void removeValue(Init *Name) {
1625 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1626 if (Values[i].getNameInit() == Name) {
1627 Values.erase(Values.begin()+i);
1628 return;
1629 }
1630 llvm_unreachable("Cannot remove an entry that does not exist!");
1631 }
1632
1633 void removeValue(StringRef Name) {
1634 removeValue(StringInit::get(Name));
1635 }
1636
1637 void addAssertion(SMLoc Loc, Init *Condition, Init *Message) {
1638 Assertions.push_back(AssertionInfo(Loc, Condition, Message));
1639 }
1640
1641 void appendAssertions(const Record *Rec) {
1642 Assertions.append(Rec->Assertions);
1643 }
1644
1645 void checkRecordAssertions();
1646 void checkUnusedTemplateArgs();
1647
1648 bool isSubClassOf(const Record *R) const {
1649 for (const auto &SCPair : SuperClasses)
1650 if (SCPair.first == R)
1651 return true;
1652 return false;
1653 }
1654
1655 bool isSubClassOf(StringRef Name) const {
1656 for (const auto &SCPair : SuperClasses) {
1657 if (const auto *SI = dyn_cast<StringInit>(SCPair.first->getNameInit())) {
1658 if (SI->getValue() == Name)
1659 return true;
1660 } else if (SCPair.first->getNameInitAsString() == Name) {
1661 return true;
1662 }
1663 }
1664 return false;
1665 }
1666
1667 void addSuperClass(Record *R, SMRange Range) {
1668 assert(!CorrespondingDefInit &&
1669 "changing type of record after it has been referenced");
1670 assert(!isSubClassOf(R) && "Already subclassing record!");
1671 SuperClasses.push_back(std::make_pair(R, Range));
1672 }
1673
1674 /// If there are any field references that refer to fields
1675 /// that have been filled in, we can propagate the values now.
1676 ///
1677 /// This is a final resolve: any error messages, e.g. due to undefined
1678 /// !cast references, are generated now.
1679 void resolveReferences(Init *NewName = nullptr);
1680
1681 /// Apply the resolver to the name of the record as well as to the
1682 /// initializers of all fields of the record except SkipVal.
1683 ///
1684 /// The resolver should not resolve any of the fields itself, to avoid
1685 /// recursion / infinite loops.
1686 void resolveReferences(Resolver &R, const RecordVal *SkipVal = nullptr);
1687
1688 RecordKeeper &getRecords() const {
1689 return TrackedRecords;
1690 }
1691
1692 bool isAnonymous() const {
1693 return IsAnonymous;
1694 }
1695
1696 void dump() const;
1697
1698 //===--------------------------------------------------------------------===//
1699 // High-level methods useful to tablegen back-ends
1700 //
1701
1702 ///Return the source location for the named field.
1703 SMLoc getFieldLoc(StringRef FieldName) const;
1704
1705 /// Return the initializer for a value with the specified name,
1706 /// or throw an exception if the field does not exist.
1707 Init *getValueInit(StringRef FieldName) const;
1708
1709 /// Return true if the named field is unset.
1710 bool isValueUnset(StringRef FieldName) const {
1711 return isa<UnsetInit>(getValueInit(FieldName));
1712 }
1713
1714 /// This method looks up the specified field and returns
1715 /// its value as a string, throwing an exception if the field does not exist
1716 /// or if the value is not a string.
1717 StringRef getValueAsString(StringRef FieldName) const;
1718
1719 /// This method looks up the specified field and returns
1720 /// its value as a string, throwing an exception if the field if the value is
1721 /// not a string and llvm::Optional() if the field does not exist.
1722 llvm::Optional<StringRef> getValueAsOptionalString(StringRef FieldName) const;
1723
1724 /// This method looks up the specified field and returns
1725 /// its value as a BitsInit, throwing an exception if the field does not exist
1726 /// or if the value is not the right type.
1727 BitsInit *getValueAsBitsInit(StringRef FieldName) const;
1728
1729 /// This method looks up the specified field and returns
1730 /// its value as a ListInit, throwing an exception if the field does not exist
1731 /// or if the value is not the right type.
1732 ListInit *getValueAsListInit(StringRef FieldName) const;
1733
1734 /// This method looks up the specified field and
1735 /// returns its value as a vector of records, throwing an exception if the
1736 /// field does not exist or if the value is not the right type.
1737 std::vector<Record*> getValueAsListOfDefs(StringRef FieldName) const;
1738
1739 /// This method looks up the specified field and
1740 /// returns its value as a vector of integers, throwing an exception if the
1741 /// field does not exist or if the value is not the right type.
1742 std::vector<int64_t> getValueAsListOfInts(StringRef FieldName) const;
1743
1744 /// This method looks up the specified field and
1745 /// returns its value as a vector of strings, throwing an exception if the
1746 /// field does not exist or if the value is not the right type.
1747 std::vector<StringRef> getValueAsListOfStrings(StringRef FieldName) const;
1748
1749 /// This method looks up the specified field and returns its
1750 /// value as a Record, throwing an exception if the field does not exist or if
1751 /// the value is not the right type.
1752 Record *getValueAsDef(StringRef FieldName) const;
1753
1754 /// This method looks up the specified field and returns its value as a
1755 /// Record, returning null if the field exists but is "uninitialized"
1756 /// (i.e. set to `?`), and throwing an exception if the field does not
1757 /// exist or if its value is not the right type.
1758 Record *getValueAsOptionalDef(StringRef FieldName) const;
1759
1760 /// This method looks up the specified field and returns its
1761 /// value as a bit, throwing an exception if the field does not exist or if
1762 /// the value is not the right type.
1763 bool getValueAsBit(StringRef FieldName) const;
1764
1765 /// This method looks up the specified field and
1766 /// returns its value as a bit. If the field is unset, sets Unset to true and
1767 /// returns false.
1768 bool getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const;
1769
1770 /// This method looks up the specified field and returns its
1771 /// value as an int64_t, throwing an exception if the field does not exist or
1772 /// if the value is not the right type.
1773 int64_t getValueAsInt(StringRef FieldName) const;
1774
1775 /// This method looks up the specified field and returns its
1776 /// value as an Dag, throwing an exception if the field does not exist or if
1777 /// the value is not the right type.
1778 DagInit *getValueAsDag(StringRef FieldName) const;
1779 };
1780
1781 raw_ostream &operator<<(raw_ostream &OS, const Record &R);
1782
1783 class RecordKeeper {
1784 friend class RecordRecTy;
1785
1786 using RecordMap = std::map<std::string, std::unique_ptr<Record>, std::less<>>;
1787 using GlobalMap = std::map<std::string, Init *, std::less<>>;
1788
1789 std::string InputFilename;
1790 RecordMap Classes, Defs;
1791 mutable StringMap<std::vector<Record *>> ClassRecordsMap;
1792 FoldingSet<RecordRecTy> RecordTypePool;
1793 std::map<std::string, Init *, std::less<>> ExtraGlobals;
1794 unsigned AnonCounter = 0;
1795
1796 // These members are for the phase timing feature. We need a timer group,
1797 // the last timer started, and a flag to say whether the last timer
1798 // is the special "backend overall timer."
1799 TimerGroup *TimingGroup = nullptr;
1800 Timer *LastTimer = nullptr;
1801 bool BackendTimer = false;
1802
1803 public:
1804 /// Get the main TableGen input file's name.
1805 const std::string getInputFilename() const { return InputFilename; }
1806
1807 /// Get the map of classes.
1808 const RecordMap &getClasses() const { return Classes; }
1809
1810 /// Get the map of records (defs).
1811 const RecordMap &getDefs() const { return Defs; }
1812
1813 /// Get the map of global variables.
1814 const GlobalMap &getGlobals() const { return ExtraGlobals; }
1815
1816 /// Get the class with the specified name.
1817 Record *getClass(StringRef Name) const {
1818 auto I = Classes.find(Name);
1819 return I == Classes.end() ? nullptr : I->second.get();
1820 }
1821
1822 /// Get the concrete record with the specified name.
1823 Record *getDef(StringRef Name) const {
1824 auto I = Defs.find(Name);
1825 return I == Defs.end() ? nullptr : I->second.get();
1826 }
1827
1828 /// Get the \p Init value of the specified global variable.
1829 Init *getGlobal(StringRef Name) const {
1830 if (Record *R = getDef(Name))
1831 return R->getDefInit();
1832 auto It = ExtraGlobals.find(Name);
1833 return It == ExtraGlobals.end() ? nullptr : It->second;
1834 }
1835
1836 void saveInputFilename(std::string Filename) {
1837 InputFilename = Filename;
1838 }
1839
1840 void addClass(std::unique_ptr<Record> R) {
1841 bool Ins = Classes.insert(std::make_pair(std::string(R->getName()),
1842 std::move(R))).second;
1843 (void)Ins;
1844 assert(Ins && "Class already exists");
1845 }
1846
1847 void addDef(std::unique_ptr<Record> R) {
1848 bool Ins = Defs.insert(std::make_pair(std::string(R->getName()),
1849 std::move(R))).second;
1850 (void)Ins;
1851 assert(Ins && "Record already exists");
1852 }
1853
1854 void addExtraGlobal(StringRef Name, Init *I) {
1855 bool Ins = ExtraGlobals.insert(std::make_pair(std::string(Name), I)).second;
1856 (void)Ins;
1857 assert(!getDef(Name));
1858 assert(Ins && "Global already exists");
1859 }
1860
1861 Init *getNewAnonymousName();
1862
1863 /// Start phase timing; called if the --time-phases option is specified.
1864 void startPhaseTiming() {
1865 TimingGroup = new TimerGroup("TableGen", "TableGen Phase Timing");
1866 }
1867
1868 /// Start timing a phase. Automatically stops any previous phase timer.
1869 void startTimer(StringRef Name);
1870
1871 /// Stop timing a phase.
1872 void stopTimer();
1873
1874 /// Start timing the overall backend. If the backend itself starts a timer,
1875 /// then this timer is cleared.
1876 void startBackendTimer(StringRef Name);
1877
1878 /// Stop timing the overall backend.
1879 void stopBackendTimer();
1880
1881 /// Stop phase timing and print the report.
1882 void stopPhaseTiming() {
1883 if (TimingGroup)
1884 delete TimingGroup;
1885 }
1886
1887 //===--------------------------------------------------------------------===//
1888 // High-level helper methods, useful for tablegen backends.
1889
1890 /// Get all the concrete records that inherit from the one specified
1891 /// class. The class must be defined.
1892 std::vector<Record *> getAllDerivedDefinitions(StringRef ClassName) const;
1893
1894 /// Get all the concrete records that inherit from all the specified
1895 /// classes. The classes must be defined.
1896 std::vector<Record *> getAllDerivedDefinitions(
1897 ArrayRef<StringRef> ClassNames) const;
1898
1899 void dump() const;
1900 };
1901
1902 /// Sorting predicate to sort record pointers by name.
1903 struct LessRecord {
1904 bool operator()(const Record *Rec1, const Record *Rec2) const {
1905 return StringRef(Rec1->getName()).compare_numeric(Rec2->getName()) < 0;
1906 }
1907 };
1908
1909 /// Sorting predicate to sort record pointers by their
1910 /// unique ID. If you just need a deterministic order, use this, since it
1911 /// just compares two `unsigned`; the other sorting predicates require
1912 /// string manipulation.
1913 struct LessRecordByID {
1914 bool operator()(const Record *LHS, const Record *RHS) const {
1915 return LHS->getID() < RHS->getID();
1916 }
1917 };
1918
1919 /// Sorting predicate to sort record pointers by their
1920 /// name field.
1921 struct LessRecordFieldName {
1922 bool operator()(const Record *Rec1, const Record *Rec2) const {
1923 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1924 }
1925 };
1926
1927 struct LessRecordRegister {
1928 struct RecordParts {
1929 SmallVector<std::pair< bool, StringRef>, 4> Parts;
1930
1931 RecordParts(StringRef Rec) {
1932 if (Rec.empty())
1933 return;
1934
1935 size_t Len = 0;
1936 const char *Start = Rec.data();
1937 const char *Curr = Start;
1938 bool IsDigitPart = isDigit(Curr[0]);
1939 for (size_t I = 0, E = Rec.size(); I != E; ++I, ++Len) {
1940 bool IsDigit = isDigit(Curr[I]);
1941 if (IsDigit != IsDigitPart) {
1942 Parts.push_back(std::make_pair(IsDigitPart, StringRef(Start, Len)));
1943 Len = 0;
1944 Start = &Curr[I];
1945 IsDigitPart = isDigit(Curr[I]);
1946 }
1947 }
1948 // Push the last part.
1949 Parts.push_back(std::make_pair(IsDigitPart, StringRef(Start, Len)));
1950 }
1951
1952 size_t size() { return Parts.size(); }
1953
1954 std::pair<bool, StringRef> getPart(size_t i) {
1955 assert (i < Parts.size() && "Invalid idx!");
1956 return Parts[i];
1957 }
1958 };
1959
1960 bool operator()(const Record *Rec1, const Record *Rec2) const {
1961 RecordParts LHSParts(StringRef(Rec1->getName()));
1962 RecordParts RHSParts(StringRef(Rec2->getName()));
1963
1964 size_t LHSNumParts = LHSParts.size();
1965 size_t RHSNumParts = RHSParts.size();
1966 assert (LHSNumParts && RHSNumParts && "Expected at least one part!");
1967
1968 if (LHSNumParts != RHSNumParts)
1969 return LHSNumParts < RHSNumParts;
1970
1971 // We expect the registers to be of the form [_a-zA-Z]+([0-9]*[_a-zA-Z]*)*.
1972 for (size_t I = 0, E = LHSNumParts; I < E; I+=2) {
1973 std::pair<bool, StringRef> LHSPart = LHSParts.getPart(I);
1974 std::pair<bool, StringRef> RHSPart = RHSParts.getPart(I);
1975 // Expect even part to always be alpha.
1976 assert (LHSPart.first == false && RHSPart.first == false &&
1977 "Expected both parts to be alpha.");
1978 if (int Res = LHSPart.second.compare(RHSPart.second))
1979 return Res < 0;
1980 }
1981 for (size_t I = 1, E = LHSNumParts; I < E; I+=2) {
1982 std::pair<bool, StringRef> LHSPart = LHSParts.getPart(I);
1983 std::pair<bool, StringRef> RHSPart = RHSParts.getPart(I);
1984 // Expect odd part to always be numeric.
1985 assert (LHSPart.first == true && RHSPart.first == true &&
1986 "Expected both parts to be numeric.");
1987 if (LHSPart.second.size() != RHSPart.second.size())
1988 return LHSPart.second.size() < RHSPart.second.size();
1989
1990 unsigned LHSVal, RHSVal;
1991
1992 bool LHSFailed = LHSPart.second.getAsInteger(10, LHSVal); (void)LHSFailed;
1993 assert(!LHSFailed && "Unable to convert LHS to integer.");
1994 bool RHSFailed = RHSPart.second.getAsInteger(10, RHSVal); (void)RHSFailed;
1995 assert(!RHSFailed && "Unable to convert RHS to integer.");
1996
1997 if (LHSVal != RHSVal)
1998 return LHSVal < RHSVal;
1999 }
2000 return LHSNumParts < RHSNumParts;
2001 }
2002 };
2003
2004 raw_ostream &operator<<(raw_ostream &OS, const RecordKeeper &RK);
2005
2006 //===----------------------------------------------------------------------===//
2007 // Resolvers
2008 //===----------------------------------------------------------------------===//
2009
2010 /// Interface for looking up the initializer for a variable name, used by
2011 /// Init::resolveReferences.
2012 class Resolver {
2013 Record *CurRec;
2014 bool IsFinal = false;
2015
2016 public:
2017 explicit Resolver(Record *CurRec) : CurRec(CurRec) {}
2018 virtual ~Resolver() = default;
2019
2020 Record *getCurrentRecord() const { return CurRec; }
2021
2022 /// Return the initializer for the given variable name (should normally be a
2023 /// StringInit), or nullptr if the name could not be resolved.
2024 virtual Init *resolve(Init *VarName) = 0;
2025
2026 // Whether bits in a BitsInit should stay unresolved if resolving them would
2027 // result in a ? (UnsetInit). This behavior is used to represent instruction
2028 // encodings by keeping references to unset variables within a record.
2029 virtual bool keepUnsetBits() const { return false; }
2030
2031 // Whether this is the final resolve step before adding a record to the
2032 // RecordKeeper. Error reporting during resolve and related constant folding
2033 // should only happen when this is true.
2034 bool isFinal() const { return IsFinal; }
2035
2036 void setFinal(bool Final) { IsFinal = Final; }
2037 };
2038
2039 /// Resolve arbitrary mappings.
2040 class MapResolver final : public Resolver {
2041 struct MappedValue {
2042 Init *V;
2043 bool Resolved;
2044
2045 MappedValue() : V(nullptr), Resolved(false) {}
2046 MappedValue(Init *V, bool Resolved) : V(V), Resolved(Resolved) {}
2047 };
2048
2049 DenseMap<Init *, MappedValue> Map;
2050
2051 public:
2052 explicit MapResolver(Record *CurRec = nullptr) : Resolver(CurRec) {}
2053
2054 void set(Init *Key, Init *Value) { Map[Key] = {Value, false}; }
2055
2056 bool isComplete(Init *VarName) const {
2057 auto It = Map.find(VarName);
2058 assert(It != Map.end() && "key must be present in map");
2059 return It->second.V->isComplete();
2060 }
2061
2062 Init *resolve(Init *VarName) override;
2063 };
2064
2065 /// Resolve all variables from a record except for unset variables.
2066 class RecordResolver final : public Resolver {
2067 DenseMap<Init *, Init *> Cache;
2068 SmallVector<Init *, 4> Stack;
2069 Init *Name = nullptr;
2070
2071 public:
2072 explicit RecordResolver(Record &R) : Resolver(&R) {}
2073
2074 void setName(Init *NewName) { Name = NewName; }
2075
2076 Init *resolve(Init *VarName) override;
2077
2078 bool keepUnsetBits() const override { return true; }
2079 };
2080
2081 /// Delegate resolving to a sub-resolver, but shadow some variable names.
2082 class ShadowResolver final : public Resolver {
2083 Resolver &R;
2084 DenseSet<Init *> Shadowed;
2085
2086 public:
2087 explicit ShadowResolver(Resolver &R)
2088 : Resolver(R.getCurrentRecord()), R(R) {
2089 setFinal(R.isFinal());
2090 }
2091
2092 void addShadow(Init *Key) { Shadowed.insert(Key); }
2093
2094 Init *resolve(Init *VarName) override {
2095 if (Shadowed.count(VarName))
2096 return nullptr;
2097 return R.resolve(VarName);
2098 }
2099 };
2100
2101 /// (Optionally) delegate resolving to a sub-resolver, and keep track whether
2102 /// there were unresolved references.
2103 class TrackUnresolvedResolver final : public Resolver {
2104 Resolver *R;
2105 bool FoundUnresolved = false;
2106
2107 public:
2108 explicit TrackUnresolvedResolver(Resolver *R = nullptr)
2109 : Resolver(R ? R->getCurrentRecord() : nullptr), R(R) {}
2110
2111 bool foundUnresolved() const { return FoundUnresolved; }
2112
2113 Init *resolve(Init *VarName) override;
2114 };
2115
2116 /// Do not resolve anything, but keep track of whether a given variable was
2117 /// referenced.
2118 class HasReferenceResolver final : public Resolver {
2119 Init *VarNameToTrack;
2120 bool Found = false;
2121
2122 public:
2123 explicit HasReferenceResolver(Init *VarNameToTrack)
2124 : Resolver(nullptr), VarNameToTrack(VarNameToTrack) {}
2125
2126 bool found() const { return Found; }
2127
2128 Init *resolve(Init *VarName) override;
2129 };
2130
2131 void EmitDetailedRecords(RecordKeeper &RK, raw_ostream &OS);
2132 void EmitJSON(RecordKeeper &RK, raw_ostream &OS);
2133
2134 } // end namespace llvm
2135
2136 #endif // LLVM_TABLEGEN_RECORD_H