@@ -426,6 +426,11 @@ APValue ConstantExpr::getAPValueResult() const {
426426 llvm_unreachable (" invalid ResultKind" );
427427}
428428
429+ bool DeclRefExpr::needsDeclTypeStorage (ValueDecl *VD, QualType DeclType) {
430+ return !DeclType.isNull () &&
431+ (DeclType != VD->getType () || VD->getType ()->isUndeducedType ());
432+ }
433+
429434DeclRefExpr::DeclRefExpr (const ASTContext &Ctx, ValueDecl *D,
430435 bool RefersToEnclosingVariableOrCapture, QualType T,
431436 ExprValueKind VK, SourceLocation L,
@@ -442,6 +447,7 @@ DeclRefExpr::DeclRefExpr(const ASTContext &Ctx, ValueDecl *D,
442447 DeclRefExprBits.CapturedByCopyInLambdaWithExplicitObjectParameter = false ;
443448 DeclRefExprBits.NonOdrUseReason = NOUR;
444449 DeclRefExprBits.IsImmediateEscalating = false ;
450+ DeclRefExprBits.HasResugaredDeclType = false ;
445451 DeclRefExprBits.Loc = L;
446452 setDependence (computeDependence (this , Ctx));
447453}
@@ -453,7 +459,8 @@ DeclRefExpr::DeclRefExpr(const ASTContext &Ctx,
453459 const DeclarationNameInfo &NameInfo, NamedDecl *FoundD,
454460 const TemplateArgumentListInfo *TemplateArgs,
455461 const TemplateArgumentList *ConvertedArgs, QualType T,
456- ExprValueKind VK, NonOdrUseReason NOUR)
462+ ExprValueKind VK, QualType DeclType,
463+ NonOdrUseReason NOUR)
457464 : Expr(DeclRefExprClass, T, VK, OK_Ordinary), D(D),
458465 ConvertedArgs(ConvertedArgs), DNLoc(NameInfo.getInfo()) {
459466 assert (!TemplateArgs || ConvertedArgs);
@@ -472,6 +479,7 @@ DeclRefExpr::DeclRefExpr(const ASTContext &Ctx,
472479 RefersToEnclosingVariableOrCapture;
473480 DeclRefExprBits.CapturedByCopyInLambdaWithExplicitObjectParameter = false ;
474481 DeclRefExprBits.NonOdrUseReason = NOUR;
482+ DeclRefExprBits.HasResugaredDeclType = needsDeclTypeStorage (D, DeclType);
475483 if (TemplateArgs) {
476484 auto Deps = TemplateArgumentDependence::None;
477485 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom (
@@ -483,72 +491,92 @@ DeclRefExpr::DeclRefExpr(const ASTContext &Ctx,
483491 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom (
484492 TemplateKWLoc);
485493 }
494+ if (HasResugaredDeclType ()) {
495+ assert (Ctx.hasSameType (DeclType, D->getType ()));
496+ *getTrailingObjects<QualType>() =
497+ DeclType.isNull () ? D->getType () : DeclType;
498+ }
486499 DeclRefExprBits.IsImmediateEscalating = false ;
487500 DeclRefExprBits.HadMultipleCandidates = 0 ;
488501 setDependence (computeDependence (this , Ctx));
489502}
490503
504+ DeclRefExpr *DeclRefExpr::Create (
505+ const ASTContext &Context, NestedNameSpecifierLoc QualifierLoc,
506+ SourceLocation TemplateKWLoc, ValueDecl *D,
507+ bool RefersToEnclosingVariableOrCapture, SourceLocation NameLoc, QualType T,
508+ ExprValueKind VK, QualType DeclType, NamedDecl *FoundD,
509+ const TemplateArgumentListInfo *TemplateArgs,
510+ const TemplateArgumentList *ConvertedArgs, NonOdrUseReason NOUR) {
511+ return Create (Context, QualifierLoc, TemplateKWLoc, D,
512+ RefersToEnclosingVariableOrCapture,
513+ DeclarationNameInfo (D->getDeclName (), NameLoc), T, VK, DeclType,
514+ FoundD, TemplateArgs, ConvertedArgs, NOUR);
515+ }
516+
491517DeclRefExpr *DeclRefExpr::Create (const ASTContext &Context,
492518 NestedNameSpecifierLoc QualifierLoc,
493519 SourceLocation TemplateKWLoc, ValueDecl *D,
494520 bool RefersToEnclosingVariableOrCapture,
495- SourceLocation NameLoc, QualType T,
496- ExprValueKind VK, NamedDecl *FoundD,
521+ const DeclarationNameInfo &NameInfo,
522+ QualType T, ExprValueKind VK,
523+ QualType DeclType, NamedDecl *FoundD,
497524 const TemplateArgumentListInfo *TemplateArgs,
498525 const TemplateArgumentList *ConvertedArgs,
499526 NonOdrUseReason NOUR) {
500- return Create (Context, QualifierLoc, TemplateKWLoc, D,
501- RefersToEnclosingVariableOrCapture,
502- DeclarationNameInfo (D->getDeclName (), NameLoc), T, VK, FoundD,
503- TemplateArgs, ConvertedArgs, NOUR);
504- }
505-
506- DeclRefExpr *DeclRefExpr::Create (
507- const ASTContext &Context, NestedNameSpecifierLoc QualifierLoc,
508- SourceLocation TemplateKWLoc, ValueDecl *D,
509- bool RefersToEnclosingVariableOrCapture,
510- const DeclarationNameInfo &NameInfo, QualType T, ExprValueKind VK,
511- NamedDecl *FoundD, const TemplateArgumentListInfo *TemplateArgs,
512- const TemplateArgumentList *ConvertedArgs, NonOdrUseReason NOUR) {
513527 // Filter out cases where the found Decl is the same as the value refenenced.
514528 if (D == FoundD)
515529 FoundD = nullptr ;
516530
517531 bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid ();
518532 std::size_t Size =
519533 totalSizeToAlloc<NestedNameSpecifierLoc, NamedDecl *,
520- ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
534+ ASTTemplateKWAndArgsInfo, TemplateArgumentLoc, QualType >(
521535 QualifierLoc ? 1 : 0 , FoundD ? 1 : 0 ,
522536 HasTemplateKWAndArgsInfo ? 1 : 0 ,
523- TemplateArgs ? TemplateArgs->size () : 0 );
537+ TemplateArgs ? TemplateArgs->size () : 0 ,
538+ needsDeclTypeStorage (D, DeclType) ? 1 : 0 );
524539
525540 void *Mem = Context.Allocate (Size, alignof (DeclRefExpr));
526541 return new (Mem)
527542 DeclRefExpr (Context, QualifierLoc, TemplateKWLoc, D,
528543 RefersToEnclosingVariableOrCapture, NameInfo, FoundD,
529- TemplateArgs, ConvertedArgs, T, VK, NOUR);
544+ TemplateArgs, ConvertedArgs, T, VK, DeclType, NOUR);
530545}
531546
532547DeclRefExpr *DeclRefExpr::CreateEmpty (const ASTContext &Context,
533- bool HasQualifier,
534- bool HasFoundDecl,
548+ bool HasQualifier, bool HasFoundDecl,
535549 bool HasTemplateKWAndArgsInfo,
536- unsigned NumTemplateArgs) {
550+ unsigned NumTemplateArgs,
551+ bool HasResugaredDeclType) {
537552 assert (NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
538553 std::size_t Size =
539554 totalSizeToAlloc<NestedNameSpecifierLoc, NamedDecl *,
540- ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
555+ ASTTemplateKWAndArgsInfo, TemplateArgumentLoc, QualType >(
541556 HasQualifier ? 1 : 0 , HasFoundDecl ? 1 : 0 , HasTemplateKWAndArgsInfo,
542- NumTemplateArgs);
557+ NumTemplateArgs, HasResugaredDeclType ? 1 : 0 );
543558 void *Mem = Context.Allocate (Size, alignof (DeclRefExpr));
544559 return new (Mem) DeclRefExpr (EmptyShell ());
545560}
546561
547562void DeclRefExpr::setDecl (ValueDecl *NewD) {
563+ assert (D != NewD);
564+ assert (declaresSameEntity (D, NewD));
565+ assert (!HasResugaredDeclType () ||
566+ D->getASTContext ().hasSameType (NewD->getType (),
567+ *getTrailingObjects<QualType>()));
548568 D = NewD;
549- if (getType ()->isUndeducedType ())
550- setType (NewD->getType ());
551- setDependence (computeDependence (this , NewD->getASTContext ()));
569+ recomputeDependency ();
570+ }
571+
572+ void DeclRefExpr::recomputeDependency () {
573+ setDependence (computeDependence (this , D->getASTContext ()));
574+ }
575+
576+ void DeclRefExpr::setDeclType (QualType T) {
577+ assert (!T.isNull ());
578+ if (HasResugaredDeclType ())
579+ *getTrailingObjects<QualType>() = T;
552580}
553581
554582SourceLocation DeclRefExpr::getBeginLoc () const {
0 commit comments