+Init *
+TypedInit::convertInitializerTo(RecTy *Ty) const {
+ if (isa<IntRecTy>(Ty)) {
+ if (getType()->typeIsConvertibleTo(Ty))
+ return const_cast<TypedInit *>(this);
+ return nullptr;
+ }
+
+ if (isa<StringRecTy>(Ty)) {
+ if (isa<StringRecTy>(getType()))
+ return const_cast<TypedInit *>(this);
+ return nullptr;
+ }
+
+ if (isa<BitRecTy>(Ty)) {
+ // Accept variable if it is already of bit type!
+ if (isa<BitRecTy>(getType()))
+ return const_cast<TypedInit *>(this);
+ if (auto *BitsTy = dyn_cast<BitsRecTy>(getType())) {
+ // Accept only bits<1> expression.
+ if (BitsTy->getNumBits() == 1)
+ return const_cast<TypedInit *>(this);
+ return nullptr;
+ }
+ // Ternary !if can be converted to bit, but only if both sides are
+ // convertible to a bit.
+ if (const auto *TOI = dyn_cast<TernOpInit>(this)) {
+ if (TOI->getOpcode() == TernOpInit::TernaryOp::IF &&
+ TOI->getMHS()->convertInitializerTo(BitRecTy::get()) &&
+ TOI->getRHS()->convertInitializerTo(BitRecTy::get()))
+ return const_cast<TypedInit *>(this);
+ return nullptr;
+ }
+ return nullptr;
+ }
+
+ if (auto *BRT = dyn_cast<BitsRecTy>(Ty)) {
+ if (BRT->getNumBits() == 1 && isa<BitRecTy>(getType()))
+ return BitsInit::get(const_cast<TypedInit *>(this));
+
+ if (getType()->typeIsConvertibleTo(BRT)) {
+ SmallVector<Init *, 16> NewBits(BRT->getNumBits());
+
+ for (unsigned i = 0; i != BRT->getNumBits(); ++i)
+ NewBits[i] = VarBitInit::get(const_cast<TypedInit *>(this), i);
+ return BitsInit::get(NewBits);
+ }
+
+ return nullptr;
+ }
+
+ if (auto *DLRT = dyn_cast<ListRecTy>(Ty)) {
+ if (auto *SLRT = dyn_cast<ListRecTy>(getType()))
+ if (SLRT->getElementType()->typeIsConvertibleTo(DLRT->getElementType()))
+ return const_cast<TypedInit *>(this);
+ return nullptr;
+ }
+
+ if (auto *DRT = dyn_cast<DagRecTy>(Ty)) {
+ if (getType()->typeIsConvertibleTo(DRT))
+ return const_cast<TypedInit *>(this);
+ return nullptr;
+ }
+
+ if (auto *SRRT = dyn_cast<RecordRecTy>(Ty)) {
+ // Ensure that this is compatible with Rec.
+ if (RecordRecTy *DRRT = dyn_cast<RecordRecTy>(getType()))
+ if (DRRT->getRecord()->isSubClassOf(SRRT->getRecord()) ||
+ DRRT->getRecord() == SRRT->getRecord())
+ return const_cast<TypedInit *>(this);
+ return nullptr;
+ }
+
+ return nullptr;
+}
+