Subversion Repositories QNX 8.QNX8 LLVM/Clang compiler suite

Rev

Blame | Last modification | View Log | Download | RSS feed

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