From aecb68b54990fd888aebe7a9f6189b1ab00abfc6 Mon Sep 17 00:00:00 2001 From: Nicolai Haehnle Date: Fri, 23 Feb 2018 10:46:13 +0000 Subject: [PATCH] TableGen: Fix typeIsConvertibleTo for record types Summary: Only check whether the left-hand side type is a subclass (or equal to) the right-hand side type. This requires a further fix in handling !if expressions and in type resolution. Furthermore, reverse the order of superclasses so that resolveTypes will find a least common ancestor at least in simple cases. Add a test that used to be accepted without flagging the obvious type error. Change-Id: Ib366db1a4e6a079f1a0851e469b402cddae76714 Reviewers: arsenm, craig.topper, tra, MartinO Subscribers: wdng, llvm-commits Differential Revision: https://reviews.llvm.org/D43559 llvm-svn: 325884 --- llvm/lib/TableGen/Record.cpp | 14 ++++++++++---- llvm/lib/TableGen/TGParser.cpp | 17 ++++++----------- llvm/test/TableGen/if-type.td | 11 +++++++++++ llvm/test/TableGen/if.td | 26 +++++++++++++++++++++++++- 4 files changed, 52 insertions(+), 16 deletions(-) create mode 100644 llvm/test/TableGen/if-type.td diff --git a/llvm/lib/TableGen/Record.cpp b/llvm/lib/TableGen/Record.cpp index 80c0973..c94bd59 100644 --- a/llvm/lib/TableGen/Record.cpp +++ b/llvm/lib/TableGen/Record.cpp @@ -141,10 +141,6 @@ bool RecordRecTy::typeIsConvertibleTo(const RecTy *RHS) const { if (RTy->getRecord() == Rec || Rec->isSubClassOf(RTy->getRecord())) return true; - for (const auto &SCPair : RTy->getRecord()->getSuperClasses()) - if (Rec->isSubClassOf(SCPair.first)) - return true; - return false; } @@ -173,6 +169,16 @@ RecTy *llvm::resolveTypes(RecTy *T1, RecTy *T2) { return NewType2; } } + + if (ListRecTy *ListTy1 = dyn_cast(T1)) { + if (ListRecTy *ListTy2 = dyn_cast(T2)) { + RecTy* NewType = resolveTypes(ListTy1->getElementType(), + ListTy2->getElementType()); + if (NewType) + return NewType->getListTy(); + } + } + return nullptr; } diff --git a/llvm/lib/TableGen/TGParser.cpp b/llvm/lib/TableGen/TGParser.cpp index 72ab8d3..86344d3 100644 --- a/llvm/lib/TableGen/TGParser.cpp +++ b/llvm/lib/TableGen/TGParser.cpp @@ -198,6 +198,8 @@ bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) { // Since everything went well, we can now set the "superclass" list for the // current record. + CurRec->addSuperClass(SC, SubClass.RefRange); + ArrayRef> SCs = SC->getSuperClasses(); for (const auto &SCPair : SCs) { if (CurRec->isSubClassOf(SCPair.first)) @@ -205,11 +207,6 @@ bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) { "Already subclass of '" + SCPair.first->getName() + "'!\n"); CurRec->addSuperClass(SCPair.first, SCPair.second); } - - if (CurRec->isSubClassOf(SC)) - return Error(SubClass.RefRange.Start, - "Already subclass of '" + SC->getName() + "'!\n"); - CurRec->addSuperClass(SC, SubClass.RefRange); return false; } @@ -1067,12 +1064,10 @@ Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) { return nullptr; } - if (MHSTy->typeIsConvertibleTo(RHSTy)) { - Type = RHSTy; - } else if (RHSTy->typeIsConvertibleTo(MHSTy)) { - Type = MHSTy; - } else { - TokError("inconsistent types for !if"); + Type = resolveTypes(MHSTy, RHSTy); + if (!Type) { + TokError(Twine("inconsistent types '") + MHSTy->getAsString() + + "' and '" + RHSTy->getAsString() + "' for !if"); return nullptr; } break; diff --git a/llvm/test/TableGen/if-type.td b/llvm/test/TableGen/if-type.td new file mode 100644 index 0000000..809195b --- /dev/null +++ b/llvm/test/TableGen/if-type.td @@ -0,0 +1,11 @@ +// RUN: not llvm-tblgen %s 2>&1 | FileCheck %s +// XFAIL: vg_leak + +class A {} +class B : A {} +class C : A {} + +// CHECK: Value 'x' of type 'C' is incompatible with initializer '{{.*}}' of type 'A' +class X { + C x = !if(cc, b, c); +} diff --git a/llvm/test/TableGen/if.td b/llvm/test/TableGen/if.td index 019e4dd..f2a4bc1 100644 --- a/llvm/test/TableGen/if.td +++ b/llvm/test/TableGen/if.td @@ -69,10 +69,34 @@ class I2 : I1; // CHECK-NEXT: int i = 0; def DI1: I1<1>; -// CHECK: def DI2 { // I1 I2 +// CHECK: def DI2 { // CHECK-NEXT: int i = 0; def DI2: I2<1>; +// Check that !if with operands of different subtypes can initialize a +// supertype variable. +// +// CHECK: def EXd1 { +// CHECK: E x = E1d; +// CHECK: } +// +// CHECK: def EXd2 { +// CHECK: E x = E2d; +// CHECK: } +class E {} +class E1 : E {} +class E2 : E {} + +class EX { + E x = !if(cc, b, c); +} + +def E1d : E1<0>; +def E2d : E2<0>; + +def EXd1 : EX<1, E1d, E2d>; +def EXd2 : EX<0, E1d, E2d>; + // CHECK: def One // CHECK-NEXT: list first = [1, 2, 3]; // CHECK-NEXT: list rest = [1, 2, 3]; -- 2.7.4