Skip to content

Commit 946f371

Browse files
committed
[CIR] Make ClangIR compatible after AST representation modifications
1 parent 160f5ca commit 946f371

11 files changed

+48
-43
lines changed

clang/lib/CIR/CodeGen/CIRGenCXXExpr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ static MemberCallInfo commonBuildCXXMemberOrOperatorCall(
7575

7676
RValue CIRGenFunction::emitCXXMemberOrOperatorMemberCallExpr(
7777
const CallExpr *ce, const CXXMethodDecl *md, ReturnValueSlot returnValue,
78-
bool hasQualifier, NestedNameSpecifier *qualifier, bool isArrow,
78+
bool hasQualifier, NestedNameSpecifier qualifier, bool isArrow,
7979
const Expr *base) {
8080
assert(isa<CXXMemberCallExpr>(ce) || isa<CXXOperatorCallExpr>(ce));
8181

@@ -169,7 +169,7 @@ CIRGenFunction::emitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *e,
169169
assert(md->isInstance() &&
170170
"Trying to emit a member call expr on a static method!");
171171
return emitCXXMemberOrOperatorMemberCallExpr(
172-
e, md, returnValue, /*HasQualifier=*/false, /*Qualifier=*/nullptr,
172+
e, md, returnValue, /*HasQualifier=*/false, /*Qualifier=*/std::nullopt,
173173
/*IsArrow=*/false, e->getArg(0));
174174
}
175175

clang/lib/CIR/CodeGen/CIRGenCall.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ CanQualType CIRGenTypes::deriveThisType(const CXXRecordDecl *rd,
205205
const CXXMethodDecl *md) {
206206
QualType recTy;
207207
if (rd) {
208-
recTy = getASTContext().getTagDeclType(rd)->getCanonicalTypeInternal();
208+
recTy = getASTContext().getCanonicalTagType(rd);
209209
} else {
210210
// This can happen with the MS ABI. It shouldn't need anything more than
211211
// setting recTy to VoidTy here, but we're flagging it for now because we
@@ -267,7 +267,9 @@ void CIRGenFunction::emitDelegateCallArg(CallArgList &args,
267267
// Deactivate the cleanup for the callee-destructed param that was pushed.
268268
assert(!cir::MissingFeatures::thunks());
269269
if (type->isRecordType() &&
270-
type->castAs<RecordType>()->getDecl()->isParamDestroyedInCallee() &&
270+
type->castAs<RecordType>()
271+
->getOriginalDecl()
272+
->isParamDestroyedInCallee() &&
271273
param->needsDestruction(getContext())) {
272274
cgm.errorNYI(param->getSourceRange(),
273275
"emitDelegateCallArg: callee-destructed param");
@@ -667,8 +669,9 @@ void CIRGenFunction::emitCallArg(CallArgList &args, const clang::Expr *e,
667669
// In the Microsoft C++ ABI, aggregate arguments are destructed by the callee.
668670
// However, we still have to push an EH-only cleanup in case we unwind before
669671
// we make it to the call.
670-
if (argType->isRecordType() &&
671-
argType->castAs<RecordType>()->getDecl()->isParamDestroyedInCallee()) {
672+
if (argType->isRecordType() && argType->castAs<RecordType>()
673+
->getOriginalDecl()
674+
->isParamDestroyedInCallee()) {
672675
assert(!cir::MissingFeatures::msabi());
673676
cgm.errorNYI(e->getSourceRange(), "emitCallArg: msabi is NYI");
674677
}

clang/lib/CIR/CodeGen/CIRGenClass.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ static void emitMemberInitializer(CIRGenFunction &cgf,
8686
QualType fieldType = field->getType();
8787

8888
mlir::Value thisPtr = cgf.loadCXXThis();
89-
QualType recordTy = cgf.getContext().getTypeDeclType(classDecl);
89+
QualType recordTy = cgf.getContext().getCanonicalTypeDeclType(classDecl);
9090

9191
// If a base constructor is being emitted, create an LValue that has the
9292
// non-virtual alignment.
@@ -121,7 +121,7 @@ static void emitMemberInitializer(CIRGenFunction &cgf,
121121
static bool isInitializerOfDynamicClass(const CXXCtorInitializer *baseInit) {
122122
const Type *baseType = baseInit->getBaseClass();
123123
const auto *baseClassDecl =
124-
cast<CXXRecordDecl>(baseType->castAs<RecordType>()->getDecl());
124+
cast<CXXRecordDecl>(baseType->castAs<RecordType>()->getOriginalDecl());
125125
return baseClassDecl->isDynamicClass();
126126
}
127127

@@ -161,7 +161,7 @@ void CIRGenFunction::emitBaseInitializer(mlir::Location loc,
161161

162162
const Type *baseType = baseInit->getBaseClass();
163163
const auto *baseClassDecl =
164-
cast<CXXRecordDecl>(baseType->castAs<RecordType>()->getDecl());
164+
cast<CXXRecordDecl>(baseType->castAs<RecordType>()->getOriginalDecl());
165165

166166
bool isBaseVirtual = baseInit->isBaseVirtual();
167167

@@ -377,7 +377,7 @@ void CIRGenFunction::emitCXXAggrConstructorCall(
377377
//
378378
// Note that these are complete objects and so we don't need to
379379
// use the non-virtual size or alignment.
380-
QualType type = getContext().getTypeDeclType(ctor->getParent());
380+
QualType type = getContext().getCanonicalTypeDeclType(ctor->getParent());
381381
CharUnits eltAlignment = arrayBase.getAlignment().alignmentOfArrayElement(
382382
getContext().getTypeSizeInChars(type));
383383

@@ -484,7 +484,7 @@ void CIRGenFunction::emitImplicitAssignmentOperatorBody(FunctionArgList &args) {
484484
void CIRGenFunction::destroyCXXObject(CIRGenFunction &cgf, Address addr,
485485
QualType type) {
486486
const RecordType *rtype = type->castAs<RecordType>();
487-
const CXXRecordDecl *record = cast<CXXRecordDecl>(rtype->getDecl());
487+
const CXXRecordDecl *record = cast<CXXRecordDecl>(rtype->getOriginalDecl());
488488
const CXXDestructorDecl *dtor = record->getDestructor();
489489
// TODO(cir): Unlike traditional codegen, CIRGen should actually emit trivial
490490
// dtors which shall be removed on later CIR passes. However, only remove this

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,8 @@ LValue CIRGenFunction::emitCastLValue(const CastExpr *e) {
10131013
case CK_DerivedToBase: {
10141014
const auto *derivedClassTy =
10151015
e->getSubExpr()->getType()->castAs<clang::RecordType>();
1016-
auto *derivedClassDecl = cast<CXXRecordDecl>(derivedClassTy->getDecl());
1016+
auto *derivedClassDecl =
1017+
cast<CXXRecordDecl>(derivedClassTy->getOriginalDecl());
10171018

10181019
LValue lv = emitLValue(e->getSubExpr());
10191020
Address thisAddr = lv.getAddress();
@@ -1167,7 +1168,7 @@ static void pushTemporaryCleanup(CIRGenFunction &cgf,
11671168
->getBaseElementTypeUnsafe()
11681169
->getAs<clang::RecordType>()) {
11691170
// Get the destructor for the reference temporary.
1170-
auto *classDecl = cast<CXXRecordDecl>(rt->getDecl());
1171+
auto *classDecl = cast<CXXRecordDecl>(rt->getOriginalDecl());
11711172
if (!classDecl->hasTrivialDestructor())
11721173
referenceTemporaryDtor = classDecl->getDestructor();
11731174
}
@@ -1831,7 +1832,7 @@ RValue CIRGenFunction::emitCXXMemberCallExpr(const CXXMemberCallExpr *ce,
18311832
}
18321833

18331834
bool hasQualifier = me->hasQualifier();
1834-
NestedNameSpecifier *qualifier = hasQualifier ? me->getQualifier() : nullptr;
1835+
NestedNameSpecifier qualifier = me->getQualifier();
18351836
bool isArrow = me->isArrow();
18361837
const Expr *base = me->getBase();
18371838

clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ void AggExprEmitter::visitCXXParenListOrInitListExpr(
376376
// the disadvantage is that the generated code is more difficult for
377377
// the optimizer, especially with bitfields.
378378
unsigned numInitElements = args.size();
379-
RecordDecl *record = e->getType()->castAs<RecordType>()->getDecl();
379+
RecordDecl *record = e->getType()->castAs<RecordType>()->getOriginalDecl();
380380

381381
// We'll need to enter cleanup scopes in case any of the element
382382
// initializers throws an exception.

clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ mlir::Attribute ConstantEmitter::tryEmitPrivateForVarInit(const VarDecl &d) {
591591
// be a problem for the near future.
592592
if (cd->isTrivial() && cd->isDefaultConstructor()) {
593593
const auto *cxxrd =
594-
cast<CXXRecordDecl>(ty->getAs<RecordType>()->getDecl());
594+
cast<CXXRecordDecl>(ty->getAs<RecordType>()->getOriginalDecl());
595595
if (cxxrd->getNumBases() != 0) {
596596
// There may not be anything additional to do here, but this will
597597
// force us to pause and test this path when it is supported.

clang/lib/CIR/CodeGen/CIRGenFunction.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,8 @@ static bool mayDropFunctionReturn(const ASTContext &astContext,
356356
// destructor or a non-trivially copyable type.
357357
if (const RecordType *recordType =
358358
returnType.getCanonicalType()->getAs<RecordType>()) {
359-
if (const auto *classDecl = dyn_cast<CXXRecordDecl>(recordType->getDecl()))
359+
if (const auto *classDecl =
360+
dyn_cast<CXXRecordDecl>(recordType->getOriginalDecl()))
360361
return classDecl->hasTrivialDestructor();
361362
}
362363
return returnType.isTriviallyCopyableType(astContext);
@@ -827,7 +828,7 @@ void CIRGenFunction::emitNullInitialization(mlir::Location loc, Address destPtr,
827828
// Ignore empty classes in C++.
828829
if (getLangOpts().CPlusPlus) {
829830
if (const RecordType *rt = ty->getAs<RecordType>()) {
830-
if (cast<CXXRecordDecl>(rt->getDecl())->isEmpty())
831+
if (cast<CXXRecordDecl>(rt->getOriginalDecl())->isEmpty())
831832
return;
832833
}
833834
}
@@ -978,10 +979,6 @@ void CIRGenFunction::emitVariablyModifiedType(QualType type) {
978979
case Type::BitInt:
979980
llvm_unreachable("type class is never variably-modified!");
980981

981-
case Type::Elaborated:
982-
type = cast<clang::ElaboratedType>(ty)->getNamedType();
983-
break;
984-
985982
case Type::Adjusted:
986983
type = cast<clang::AdjustedType>(ty)->getAdjustedType();
987984
break;

clang/lib/CIR/CodeGen/CIRGenFunction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,7 @@ class CIRGenFunction : public CIRGenTypeCache {
10111011
RValue emitCXXMemberOrOperatorMemberCallExpr(
10121012
const clang::CallExpr *ce, const clang::CXXMethodDecl *md,
10131013
ReturnValueSlot returnValue, bool hasQualifier,
1014-
clang::NestedNameSpecifier *qualifier, bool isArrow,
1014+
clang::NestedNameSpecifier qualifier, bool isArrow,
10151015
const clang::Expr *base);
10161016

10171017
mlir::Value emitCXXNewExpr(const CXXNewExpr *e);

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,7 @@ static bool isVarDeclStrongDefinition(const ASTContext &astContext,
997997
return true;
998998

999999
if (const auto *rt = varType->getAs<RecordType>()) {
1000-
const RecordDecl *rd = rt->getDecl();
1000+
const RecordDecl *rd = rt->getOriginalDecl();
10011001
for (const FieldDecl *fd : rd->fields()) {
10021002
if (fd->isBitField())
10031003
continue;
@@ -2067,7 +2067,7 @@ CharUnits CIRGenModule::computeNonVirtualBaseClassOffset(
20672067
const ASTRecordLayout &layout = astContext.getASTRecordLayout(rd);
20682068

20692069
const auto *baseDecl = cast<CXXRecordDecl>(
2070-
base->getType()->castAs<clang::RecordType>()->getDecl());
2070+
base->getType()->castAs<clang::RecordType>()->getOriginalDecl());
20712071

20722072
// Add the offset.
20732073
offset += layout.getBaseClassOffset(baseDecl);

clang/lib/CIR/CodeGen/CIRGenTypes.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ std::string CIRGenTypes::getRecordTypeName(const clang::RecordDecl *recordDecl,
103103
policy.SuppressTagKeyword = true;
104104

105105
if (recordDecl->getIdentifier())
106-
astContext.getRecordType(recordDecl).print(outStream, policy);
106+
QualType(astContext.getCanonicalTagType(recordDecl))
107+
.print(outStream, policy);
107108
else if (auto *typedefNameDecl = recordDecl->getTypedefNameForAnonDecl())
108109
typedefNameDecl->printQualifiedName(outStream, policy);
109110
else
@@ -138,7 +139,7 @@ isSafeToConvert(const RecordDecl *rd, CIRGenTypes &cgt,
138139
if (!alreadyChecked.insert(rd).second)
139140
return true;
140141

141-
const Type *key = cgt.getASTContext().getTagDeclType(rd).getTypePtr();
142+
const Type *key = cgt.getASTContext().getCanonicalTagType(rd).getTypePtr();
142143

143144
// If this type is already laid out, converting it is a noop.
144145
if (cgt.isRecordLayoutComplete(key))
@@ -182,7 +183,7 @@ isSafeToConvert(QualType qt, CIRGenTypes &cgt,
182183

183184
// If this is a record, check it.
184185
if (const auto *rt = qt->getAs<RecordType>())
185-
return isSafeToConvert(rt->getDecl(), cgt, alreadyChecked);
186+
return isSafeToConvert(rt->getOriginalDecl(), cgt, alreadyChecked);
186187

187188
// If this is an array, check the elements, which are embedded inline.
188189
if (const auto *at = cgt.getASTContext().getAsArrayType(qt))
@@ -210,7 +211,7 @@ static bool isSafeToConvert(const RecordDecl *rd, CIRGenTypes &cgt) {
210211
mlir::Type CIRGenTypes::convertRecordDeclType(const clang::RecordDecl *rd) {
211212
// TagDecl's are not necessarily unique, instead use the (clang) type
212213
// connected to the decl.
213-
const Type *key = astContext.getTagDeclType(rd).getTypePtr();
214+
const Type *key = astContext.getCanonicalTagType(rd).getTypePtr();
214215
cir::RecordType entry = recordDeclTypes[key];
215216

216217
// If we don't have an entry for this record yet, create one.
@@ -242,7 +243,8 @@ mlir::Type CIRGenTypes::convertRecordDeclType(const clang::RecordDecl *rd) {
242243
for (const auto &base : cxxRecordDecl->bases()) {
243244
if (base.isVirtual())
244245
continue;
245-
convertRecordDeclType(base.getType()->castAs<RecordType>()->getDecl());
246+
convertRecordDeclType(
247+
base.getType()->castAs<RecordType>()->getOriginalDecl());
246248
}
247249
}
248250

@@ -275,7 +277,7 @@ mlir::Type CIRGenTypes::convertType(QualType type) {
275277

276278
// Process record types before the type cache lookup.
277279
if (const auto *recordType = dyn_cast<RecordType>(type))
278-
return convertRecordDeclType(recordType->getDecl());
280+
return convertRecordDeclType(recordType->getOriginalDecl());
279281

280282
// Has the type already been processed?
281283
TypeCacheTy::iterator tci = typeCache.find(ty);
@@ -457,7 +459,7 @@ mlir::Type CIRGenTypes::convertType(QualType type) {
457459
}
458460

459461
case Type::Enum: {
460-
const EnumDecl *ed = cast<EnumType>(ty)->getDecl();
462+
const EnumDecl *ed = cast<EnumType>(ty)->getOriginalDecl();
461463
if (auto integerType = ed->getIntegerType(); !integerType.isNull())
462464
return convertType(integerType);
463465
// Return a placeholder 'i32' type. This can be changed later when the
@@ -516,7 +518,7 @@ mlir::Type CIRGenTypes::convertTypeForMem(clang::QualType qualType,
516518
/// Return record layout info for the given record decl.
517519
const CIRGenRecordLayout &
518520
CIRGenTypes::getCIRGenRecordLayout(const RecordDecl *rd) {
519-
const auto *key = astContext.getTagDeclType(rd).getTypePtr();
521+
const auto *key = astContext.getCanonicalTagType(rd).getTypePtr();
520522

521523
// If we have already computed the layout, return it.
522524
auto it = cirGenRecordLayouts.find(key);
@@ -548,7 +550,7 @@ bool CIRGenTypes::isZeroInitializable(clang::QualType t) {
548550
}
549551

550552
if (const RecordType *rt = t->getAs<RecordType>()) {
551-
const RecordDecl *rd = rt->getDecl();
553+
const RecordDecl *rd = rt->getOriginalDecl();
552554
return isZeroInitializable(rd);
553555
}
554556

@@ -622,9 +624,11 @@ void CIRGenTypes::updateCompletedType(const TagDecl *td) {
622624
// a test case that meets that condition. C++ doesn't allow forward
623625
// declaration of enums, and C doesn't allow an incomplete forward
624626
// declaration with a non-default type.
625-
assert(
626-
!typeCache.count(ed->getTypeForDecl()) ||
627-
(convertType(ed->getIntegerType()) == typeCache[ed->getTypeForDecl()]));
627+
628+
CanQualType t = astContext.getCanonicalTagType(td);
629+
assert(!typeCache.count(t->getTypePtr()) ||
630+
(convertType(ed->getIntegerType()) == typeCache[t->getTypePtr()]));
631+
628632
// If necessary, provide the full definition of a type only used with a
629633
// declaration so far.
630634
assert(!cir::MissingFeatures::generateDebugInfo());
@@ -639,7 +643,7 @@ void CIRGenTypes::updateCompletedType(const TagDecl *td) {
639643

640644
// Only complete if we converted it already. If we haven't converted it yet,
641645
// we'll just do it lazily.
642-
if (recordDeclTypes.count(astContext.getTagDeclType(rd).getTypePtr()))
646+
if (recordDeclTypes.count(astContext.getCanonicalTagType(rd).getTypePtr()))
643647
convertRecordDeclType(rd);
644648

645649
// If necessary, provide the full definition of a type only used with a

0 commit comments

Comments
 (0)