Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions clang/include/clang/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -1266,7 +1266,7 @@ class DeclRefExpr final
: public Expr,
private llvm::TrailingObjects<DeclRefExpr, NestedNameSpecifierLoc,
NamedDecl *, ASTTemplateKWAndArgsInfo,
TemplateArgumentLoc> {
TemplateArgumentLoc, QualType> {
friend class ASTStmtReader;
friend class ASTStmtWriter;
friend TrailingObjects;
Expand All @@ -1292,17 +1292,27 @@ class DeclRefExpr final
return hasTemplateKWAndArgsInfo();
}

size_t numTrailingObjects(OverloadToken<TemplateArgumentLoc>) const {
return getNumTemplateArgs();
}

size_t numTrailingObjects(OverloadToken<QualType>) const {
return HasResugaredDeclType();
}

/// Test whether there is a distinct FoundDecl attached to the end of
/// this DRE.
bool hasFoundDecl() const { return DeclRefExprBits.HasFoundDecl; }

static bool needsDeclTypeStorage(ValueDecl *VD, QualType DeclType);

DeclRefExpr(const ASTContext &Ctx, NestedNameSpecifierLoc QualifierLoc,
SourceLocation TemplateKWLoc, ValueDecl *D,
bool RefersToEnclosingVariableOrCapture,
const DeclarationNameInfo &NameInfo, NamedDecl *FoundD,
const TemplateArgumentListInfo *TemplateArgs,
const TemplateArgumentList *ConvertedArgs, QualType T,
ExprValueKind VK, NonOdrUseReason NOUR);
ExprValueKind VK, QualType DeclType, NonOdrUseReason NOUR);

/// Construct an empty declaration reference expression.
explicit DeclRefExpr(EmptyShell Empty) : Expr(DeclRefExprClass, Empty) {}
Expand All @@ -1318,7 +1328,8 @@ class DeclRefExpr final
Create(const ASTContext &Context, NestedNameSpecifierLoc QualifierLoc,
SourceLocation TemplateKWLoc, ValueDecl *D,
bool RefersToEnclosingVariableOrCapture, SourceLocation NameLoc,
QualType T, ExprValueKind VK, NamedDecl *FoundD = nullptr,
QualType T, ExprValueKind VK, QualType DeclType = QualType(),
NamedDecl *FoundD = nullptr,
const TemplateArgumentListInfo *TemplateArgs = nullptr,
const TemplateArgumentList *ConvertedArgs = nullptr,
NonOdrUseReason NOUR = NOUR_None);
Expand All @@ -1328,7 +1339,7 @@ class DeclRefExpr final
SourceLocation TemplateKWLoc, ValueDecl *D,
bool RefersToEnclosingVariableOrCapture,
const DeclarationNameInfo &NameInfo, QualType T, ExprValueKind VK,
NamedDecl *FoundD = nullptr,
QualType DeclType = QualType(), NamedDecl *FoundD = nullptr,
const TemplateArgumentListInfo *TemplateArgs = nullptr,
const TemplateArgumentList *ConvertedArgs = nullptr,
NonOdrUseReason NOUR = NOUR_None);
Expand All @@ -1337,11 +1348,22 @@ class DeclRefExpr final
static DeclRefExpr *CreateEmpty(const ASTContext &Context, bool HasQualifier,
bool HasFoundDecl,
bool HasTemplateKWAndArgsInfo,
unsigned NumTemplateArgs);
unsigned NumTemplateArgs,
bool HasResugaredDeclType);

ValueDecl *getDecl() { return D; }
const ValueDecl *getDecl() const { return D; }
void setDecl(ValueDecl *NewD);
void recomputeDependency();

bool HasResugaredDeclType() const {
return DeclRefExprBits.HasResugaredDeclType;
}
QualType getDeclType() const {
return HasResugaredDeclType() ? *getTrailingObjects<QualType>()
: D->getType();
}
void setDeclType(QualType T);

DeclarationNameInfo getNameInfo() const {
return DeclarationNameInfo(getDecl()->getDeclName(), getLocation(), DNLoc);
Expand Down
2 changes: 2 additions & 0 deletions clang/include/clang/AST/Stmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,8 @@ class alignas(void *) Stmt {
unsigned NonOdrUseReason : 2;
LLVM_PREFERRED_TYPE(bool)
unsigned IsImmediateEscalating : 1;
LLVM_PREFERRED_TYPE(bool)
unsigned HasResugaredDeclType : 1;

/// The location of the declaration name itself.
SourceLocation Loc;
Expand Down
20 changes: 11 additions & 9 deletions clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -6849,22 +6849,23 @@ class Sema final : public SemaBase {

DeclRefExpr *BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK,
SourceLocation Loc,
QualType DeclType = QualType(),
const CXXScopeSpec *SS = nullptr);
DeclRefExpr *
BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK,
const DeclarationNameInfo &NameInfo,
const CXXScopeSpec *SS = nullptr,
NamedDecl *FoundD = nullptr,
SourceLocation TemplateKWLoc = SourceLocation(),
const TemplateArgumentListInfo *TemplateArgs = nullptr,
const TemplateArgumentList *ConvertArgs = nullptr);
DeclRefExpr *BuildDeclRefExpr(
ValueDecl *D, QualType Ty, ExprValueKind VK,
const DeclarationNameInfo &NameInfo, QualType DeclType = QualType(),
const CXXScopeSpec *SS = nullptr, NamedDecl *FoundD = nullptr,
SourceLocation TemplateKWLoc = SourceLocation(),
const TemplateArgumentListInfo *TemplateArgs = nullptr,
const TemplateArgumentList *ConvertArgs = nullptr);

/// BuildDeclRefExpr - Build an expression that references a
/// declaration that does not require a closure capture.
DeclRefExpr *
BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK,
const DeclarationNameInfo &NameInfo,
NestedNameSpecifierLoc NNS, NamedDecl *FoundD = nullptr,
NestedNameSpecifierLoc NNS, QualType DeclType = QualType(),
NamedDecl *FoundD = nullptr,
SourceLocation TemplateKWLoc = SourceLocation(),
const TemplateArgumentListInfo *TemplateArgs = nullptr,
const TemplateArgumentList *ConvertArgs = nullptr);
Expand Down Expand Up @@ -14011,6 +14012,7 @@ class Sema final : public SemaBase {
QualType resugar(const Type *Base, QualType T);
QualType resugar(const Type *Base, NamedDecl *ND,
ArrayRef<TemplateArgument> Args, QualType T);
QualType resugar(DeclRefExpr *DRE, ValueDecl *VD);

/// Performs template instantiation for all implicit template
/// instantiations we have seen until this point.
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/AST/ASTImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7496,6 +7496,7 @@ ExpectedStmt ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
auto ToConvertedArgs = importChecked(Err, E->getConvertedArgs());
auto ToLocation = importChecked(Err, E->getLocation());
auto ToType = importChecked(Err, E->getType());
auto ToDeclType = importChecked(Err, E->getDeclType());
if (Err)
return std::move(Err);

Expand All @@ -7520,7 +7521,7 @@ ExpectedStmt ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
auto *ToE = DeclRefExpr::Create(
Importer.getToContext(), ToQualifierLoc, ToTemplateKeywordLoc, ToDecl,
E->refersToEnclosingVariableOrCapture(), ToLocation, ToType,
E->getValueKind(), ToFoundD, ToResInfo, ToConvertedArgs,
E->getValueKind(), ToDeclType, ToFoundD, ToResInfo, ToConvertedArgs,
E->isNonOdrUse());
if (E->hadMultipleCandidates())
ToE->setHadMultipleCandidates(true);
Expand Down
82 changes: 55 additions & 27 deletions clang/lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,11 @@ APValue ConstantExpr::getAPValueResult() const {
llvm_unreachable("invalid ResultKind");
}

bool DeclRefExpr::needsDeclTypeStorage(ValueDecl *VD, QualType DeclType) {
return !DeclType.isNull() &&
(DeclType != VD->getType() || VD->getType()->isUndeducedType());
}

DeclRefExpr::DeclRefExpr(const ASTContext &Ctx, ValueDecl *D,
bool RefersToEnclosingVariableOrCapture, QualType T,
ExprValueKind VK, SourceLocation L,
Expand All @@ -442,6 +447,7 @@ DeclRefExpr::DeclRefExpr(const ASTContext &Ctx, ValueDecl *D,
DeclRefExprBits.CapturedByCopyInLambdaWithExplicitObjectParameter = false;
DeclRefExprBits.NonOdrUseReason = NOUR;
DeclRefExprBits.IsImmediateEscalating = false;
DeclRefExprBits.HasResugaredDeclType = false;
DeclRefExprBits.Loc = L;
setDependence(computeDependence(this, Ctx));
}
Expand All @@ -453,7 +459,8 @@ DeclRefExpr::DeclRefExpr(const ASTContext &Ctx,
const DeclarationNameInfo &NameInfo, NamedDecl *FoundD,
const TemplateArgumentListInfo *TemplateArgs,
const TemplateArgumentList *ConvertedArgs, QualType T,
ExprValueKind VK, NonOdrUseReason NOUR)
ExprValueKind VK, QualType DeclType,
NonOdrUseReason NOUR)
: Expr(DeclRefExprClass, T, VK, OK_Ordinary), D(D),
ConvertedArgs(ConvertedArgs), DNLoc(NameInfo.getInfo()) {
assert(!TemplateArgs || ConvertedArgs);
Expand All @@ -472,6 +479,7 @@ DeclRefExpr::DeclRefExpr(const ASTContext &Ctx,
RefersToEnclosingVariableOrCapture;
DeclRefExprBits.CapturedByCopyInLambdaWithExplicitObjectParameter = false;
DeclRefExprBits.NonOdrUseReason = NOUR;
DeclRefExprBits.HasResugaredDeclType = needsDeclTypeStorage(D, DeclType);
if (TemplateArgs) {
auto Deps = TemplateArgumentDependence::None;
getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
Expand All @@ -483,72 +491,92 @@ DeclRefExpr::DeclRefExpr(const ASTContext &Ctx,
getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
TemplateKWLoc);
}
if (HasResugaredDeclType()) {
assert(Ctx.hasSameType(DeclType, D->getType()));
*getTrailingObjects<QualType>() =
DeclType.isNull() ? D->getType() : DeclType;
}
DeclRefExprBits.IsImmediateEscalating = false;
DeclRefExprBits.HadMultipleCandidates = 0;
setDependence(computeDependence(this, Ctx));
}

DeclRefExpr *DeclRefExpr::Create(
const ASTContext &Context, NestedNameSpecifierLoc QualifierLoc,
SourceLocation TemplateKWLoc, ValueDecl *D,
bool RefersToEnclosingVariableOrCapture, SourceLocation NameLoc, QualType T,
ExprValueKind VK, QualType DeclType, NamedDecl *FoundD,
const TemplateArgumentListInfo *TemplateArgs,
const TemplateArgumentList *ConvertedArgs, NonOdrUseReason NOUR) {
return Create(Context, QualifierLoc, TemplateKWLoc, D,
RefersToEnclosingVariableOrCapture,
DeclarationNameInfo(D->getDeclName(), NameLoc), T, VK, DeclType,
FoundD, TemplateArgs, ConvertedArgs, NOUR);
}

DeclRefExpr *DeclRefExpr::Create(const ASTContext &Context,
NestedNameSpecifierLoc QualifierLoc,
SourceLocation TemplateKWLoc, ValueDecl *D,
bool RefersToEnclosingVariableOrCapture,
SourceLocation NameLoc, QualType T,
ExprValueKind VK, NamedDecl *FoundD,
const DeclarationNameInfo &NameInfo,
QualType T, ExprValueKind VK,
QualType DeclType, NamedDecl *FoundD,
const TemplateArgumentListInfo *TemplateArgs,
const TemplateArgumentList *ConvertedArgs,
NonOdrUseReason NOUR) {
return Create(Context, QualifierLoc, TemplateKWLoc, D,
RefersToEnclosingVariableOrCapture,
DeclarationNameInfo(D->getDeclName(), NameLoc), T, VK, FoundD,
TemplateArgs, ConvertedArgs, NOUR);
}

DeclRefExpr *DeclRefExpr::Create(
const ASTContext &Context, NestedNameSpecifierLoc QualifierLoc,
SourceLocation TemplateKWLoc, ValueDecl *D,
bool RefersToEnclosingVariableOrCapture,
const DeclarationNameInfo &NameInfo, QualType T, ExprValueKind VK,
NamedDecl *FoundD, const TemplateArgumentListInfo *TemplateArgs,
const TemplateArgumentList *ConvertedArgs, NonOdrUseReason NOUR) {
// Filter out cases where the found Decl is the same as the value refenenced.
if (D == FoundD)
FoundD = nullptr;

bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
std::size_t Size =
totalSizeToAlloc<NestedNameSpecifierLoc, NamedDecl *,
ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
ASTTemplateKWAndArgsInfo, TemplateArgumentLoc, QualType>(
QualifierLoc ? 1 : 0, FoundD ? 1 : 0,
HasTemplateKWAndArgsInfo ? 1 : 0,
TemplateArgs ? TemplateArgs->size() : 0);
TemplateArgs ? TemplateArgs->size() : 0,
needsDeclTypeStorage(D, DeclType) ? 1 : 0);

void *Mem = Context.Allocate(Size, alignof(DeclRefExpr));
return new (Mem)
DeclRefExpr(Context, QualifierLoc, TemplateKWLoc, D,
RefersToEnclosingVariableOrCapture, NameInfo, FoundD,
TemplateArgs, ConvertedArgs, T, VK, NOUR);
TemplateArgs, ConvertedArgs, T, VK, DeclType, NOUR);
}

DeclRefExpr *DeclRefExpr::CreateEmpty(const ASTContext &Context,
bool HasQualifier,
bool HasFoundDecl,
bool HasQualifier, bool HasFoundDecl,
bool HasTemplateKWAndArgsInfo,
unsigned NumTemplateArgs) {
unsigned NumTemplateArgs,
bool HasResugaredDeclType) {
assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
std::size_t Size =
totalSizeToAlloc<NestedNameSpecifierLoc, NamedDecl *,
ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
ASTTemplateKWAndArgsInfo, TemplateArgumentLoc, QualType>(
HasQualifier ? 1 : 0, HasFoundDecl ? 1 : 0, HasTemplateKWAndArgsInfo,
NumTemplateArgs);
NumTemplateArgs, HasResugaredDeclType ? 1 : 0);
void *Mem = Context.Allocate(Size, alignof(DeclRefExpr));
return new (Mem) DeclRefExpr(EmptyShell());
}

void DeclRefExpr::setDecl(ValueDecl *NewD) {
assert(D != NewD);
assert(declaresSameEntity(D, NewD));
assert(!HasResugaredDeclType() ||
D->getASTContext().hasSameType(NewD->getType(),
*getTrailingObjects<QualType>()));
D = NewD;
if (getType()->isUndeducedType())
setType(NewD->getType());
setDependence(computeDependence(this, NewD->getASTContext()));
recomputeDependency();
}

void DeclRefExpr::recomputeDependency() {
setDependence(computeDependence(this, D->getASTContext()));
}

void DeclRefExpr::setDeclType(QualType T) {
assert(!T.isNull());
if (HasResugaredDeclType())
*getTrailingObjects<QualType>() = T;
}

SourceLocation DeclRefExpr::getBeginLoc() const {
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/CodeGen/CGExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1867,8 +1867,8 @@ static DeclRefExpr *tryToConvertMemberExprToDeclRefExpr(CodeGenFunction &CGF,
return DeclRefExpr::Create(
CGF.getContext(), NestedNameSpecifierLoc(), SourceLocation(), VD,
/*RefersToEnclosingVariableOrCapture=*/false, ME->getExprLoc(),
ME->getType(), ME->getValueKind(), nullptr, nullptr, nullptr,
ME->isNonOdrUse());
ME->getType(), ME->getValueKind(), QualType(), nullptr, nullptr,
nullptr, ME->isNonOdrUse());
}
return nullptr;
}
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Sema/SemaChecking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4632,7 +4632,8 @@ ExprResult Sema::BuiltinAtomicOverloaded(ExprResult TheCallResult) {
DeclRefExpr *NewDRE = DeclRefExpr::Create(
Context, DRE->getQualifierLoc(), SourceLocation(), NewBuiltinDecl,
/*enclosing*/ false, DRE->getLocation(), Context.BuiltinFnTy,
DRE->getValueKind(), nullptr, nullptr, nullptr, DRE->isNonOdrUse());
DRE->getValueKind(), QualType(), nullptr, nullptr, nullptr,
DRE->isNonOdrUse());

// Set the callee in the CallExpr.
// FIXME: This loses syntactic information.
Expand Down
19 changes: 8 additions & 11 deletions clang/lib/Sema/SemaDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4920,11 +4920,9 @@ BuildImplicitBaseInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor,
ParmVarDecl *Param = Constructor->getParamDecl(0);
QualType ParamType = Param->getType().getNonReferenceType();

Expr *CopyCtorArg =
DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(),
SourceLocation(), Param, false,
Constructor->getLocation(), ParamType,
VK_LValue, nullptr);
Expr *CopyCtorArg = DeclRefExpr::Create(
SemaRef.Context, NestedNameSpecifierLoc(), SourceLocation(), Param,
false, Constructor->getLocation(), ParamType, VK_LValue);

SemaRef.MarkDeclRefReferenced(cast<DeclRefExpr>(CopyCtorArg));

Expand Down Expand Up @@ -4994,10 +4992,9 @@ BuildImplicitMemberInitializer(Sema &SemaRef, CXXConstructorDecl *Constructor,
if (Field->isZeroLengthBitField())
return false;

Expr *MemberExprBase =
DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(),
SourceLocation(), Param, false,
Loc, ParamType, VK_LValue, nullptr);
Expr *MemberExprBase = DeclRefExpr::Create(
SemaRef.Context, NestedNameSpecifierLoc(), SourceLocation(), Param,
false, Loc, ParamType, VK_LValue);

SemaRef.MarkDeclRefReferenced(cast<DeclRefExpr>(MemberExprBase));

Expand Down Expand Up @@ -14676,8 +14673,8 @@ buildMemcpyForAssignmentOp(Sema &S, SourceLocation Loc, QualType T,
// about it.
return StmtError();

ExprResult MemCpyRef = S.BuildDeclRefExpr(MemCpy, S.Context.BuiltinFnTy,
VK_PRValue, Loc, nullptr);
ExprResult MemCpyRef =
S.BuildDeclRefExpr(MemCpy, S.Context.BuiltinFnTy, VK_PRValue, Loc);
assert(MemCpyRef.isUsable() && "Builtin reference cannot fail");

Expr *CallArgs[] = {
Expand Down
Loading
Loading