Skip to content

Commit 30d9ca1

Browse files
committed
[clang][AST] Encapsulate DeclarationNameLoc, NFCI
This change makes `DeclarationNameLoc` a proper class and refactors its users to use getter methods instead of accessing the members directly. The change also makes `DeclarationNameLoc` immutable (i.e., it cannot be modified once constructed). Reviewed By: aprantl Differential Revision: https://reviews.llvm.org/D94596
1 parent 48ecba3 commit 30d9ca1

File tree

9 files changed

+110
-74
lines changed

9 files changed

+110
-74
lines changed

clang/include/clang/AST/DeclarationName.h

Lines changed: 76 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ class DeclarationNameTable {
647647
/// DeclarationNameLoc - Additional source/type location info
648648
/// for a declaration name. Needs a DeclarationName in order
649649
/// to be interpreted correctly.
650-
struct DeclarationNameLoc {
650+
class DeclarationNameLoc {
651651
// The source location for identifier stored elsewhere.
652652
// struct {} Identifier;
653653

@@ -679,10 +679,78 @@ struct DeclarationNameLoc {
679679
struct CXXLitOpName CXXLiteralOperatorName;
680680
};
681681

682-
DeclarationNameLoc(DeclarationName Name);
682+
void setNamedTypeLoc(TypeSourceInfo *TInfo) { NamedType.TInfo = TInfo; }
683+
684+
void setCXXOperatorNameRange(SourceRange Range) {
685+
CXXOperatorName.BeginOpNameLoc = Range.getBegin().getRawEncoding();
686+
CXXOperatorName.EndOpNameLoc = Range.getEnd().getRawEncoding();
687+
}
683688

689+
void setCXXLiteralOperatorNameLoc(SourceLocation Loc) {
690+
CXXLiteralOperatorName.OpNameLoc = Loc.getRawEncoding();
691+
}
692+
693+
public:
694+
DeclarationNameLoc(DeclarationName Name);
684695
// FIXME: this should go away once all DNLocs are properly initialized.
685696
DeclarationNameLoc() { memset((void*) this, 0, sizeof(*this)); }
697+
698+
/// Returns the source type info. Assumes that the object stores location
699+
/// information of a constructor, destructor or conversion operator.
700+
TypeSourceInfo *getNamedTypeInfo() const { return NamedType.TInfo; }
701+
702+
/// Return the beginning location of the getCXXOperatorNameRange() range.
703+
SourceLocation getCXXOperatorNameBeginLoc() const {
704+
return SourceLocation::getFromRawEncoding(CXXOperatorName.BeginOpNameLoc);
705+
}
706+
707+
/// Return the end location of the getCXXOperatorNameRange() range.
708+
SourceLocation getCXXOperatorNameEndLoc() const {
709+
return SourceLocation::getFromRawEncoding(CXXOperatorName.EndOpNameLoc);
710+
}
711+
712+
/// Return the range of the operator name (without the operator keyword).
713+
/// Assumes that the object stores location information of a (non-literal)
714+
/// operator.
715+
SourceRange getCXXOperatorNameRange() const {
716+
return SourceRange(getCXXOperatorNameBeginLoc(),
717+
getCXXOperatorNameEndLoc());
718+
}
719+
720+
/// Return the location of the literal operator name (without the operator
721+
/// keyword). Assumes that the object stores location information of a literal
722+
/// operator.
723+
SourceLocation getCXXLiteralOperatorNameLoc() const {
724+
return SourceLocation::getFromRawEncoding(CXXLiteralOperatorName.OpNameLoc);
725+
}
726+
727+
/// Construct location information for a constructor, destructor or conversion
728+
/// operator.
729+
static DeclarationNameLoc makeNamedTypeLoc(TypeSourceInfo *TInfo) {
730+
DeclarationNameLoc DNL;
731+
DNL.setNamedTypeLoc(TInfo);
732+
return DNL;
733+
}
734+
735+
/// Construct location information for a non-literal C++ operator.
736+
static DeclarationNameLoc makeCXXOperatorNameLoc(SourceLocation BeginLoc,
737+
SourceLocation EndLoc) {
738+
return makeCXXOperatorNameLoc(SourceRange(BeginLoc, EndLoc));
739+
}
740+
741+
/// Construct location information for a non-literal C++ operator.
742+
static DeclarationNameLoc makeCXXOperatorNameLoc(SourceRange Range) {
743+
DeclarationNameLoc DNL;
744+
DNL.setCXXOperatorNameRange(Range);
745+
return DNL;
746+
}
747+
748+
/// Construct location information for a literal C++ operator.
749+
static DeclarationNameLoc makeCXXLiteralOperatorNameLoc(SourceLocation Loc) {
750+
DeclarationNameLoc DNL;
751+
DNL.setCXXLiteralOperatorNameLoc(Loc);
752+
return DNL;
753+
}
686754
};
687755

688756
/// DeclarationNameInfo - A collector data type for bundling together
@@ -722,7 +790,6 @@ struct DeclarationNameInfo {
722790
void setLoc(SourceLocation L) { NameLoc = L; }
723791

724792
const DeclarationNameLoc &getInfo() const { return LocInfo; }
725-
DeclarationNameLoc &getInfo() { return LocInfo; }
726793
void setInfo(const DeclarationNameLoc &Info) { LocInfo = Info; }
727794

728795
/// getNamedTypeInfo - Returns the source type info associated to
@@ -732,7 +799,7 @@ struct DeclarationNameInfo {
732799
Name.getNameKind() != DeclarationName::CXXDestructorName &&
733800
Name.getNameKind() != DeclarationName::CXXConversionFunctionName)
734801
return nullptr;
735-
return LocInfo.NamedType.TInfo;
802+
return LocInfo.getNamedTypeInfo();
736803
}
737804

738805
/// setNamedTypeInfo - Sets the source type info associated to
@@ -741,26 +808,22 @@ struct DeclarationNameInfo {
741808
assert(Name.getNameKind() == DeclarationName::CXXConstructorName ||
742809
Name.getNameKind() == DeclarationName::CXXDestructorName ||
743810
Name.getNameKind() == DeclarationName::CXXConversionFunctionName);
744-
LocInfo.NamedType.TInfo = TInfo;
811+
LocInfo = DeclarationNameLoc::makeNamedTypeLoc(TInfo);
745812
}
746813

747814
/// getCXXOperatorNameRange - Gets the range of the operator name
748815
/// (without the operator keyword). Assumes it is a (non-literal) operator.
749816
SourceRange getCXXOperatorNameRange() const {
750817
if (Name.getNameKind() != DeclarationName::CXXOperatorName)
751818
return SourceRange();
752-
return SourceRange(
753-
SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.BeginOpNameLoc),
754-
SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.EndOpNameLoc)
755-
);
819+
return LocInfo.getCXXOperatorNameRange();
756820
}
757821

758822
/// setCXXOperatorNameRange - Sets the range of the operator name
759823
/// (without the operator keyword). Assumes it is a C++ operator.
760824
void setCXXOperatorNameRange(SourceRange R) {
761825
assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
762-
LocInfo.CXXOperatorName.BeginOpNameLoc = R.getBegin().getRawEncoding();
763-
LocInfo.CXXOperatorName.EndOpNameLoc = R.getEnd().getRawEncoding();
826+
LocInfo = DeclarationNameLoc::makeCXXOperatorNameLoc(R);
764827
}
765828

766829
/// getCXXLiteralOperatorNameLoc - Returns the location of the literal
@@ -769,16 +832,15 @@ struct DeclarationNameInfo {
769832
SourceLocation getCXXLiteralOperatorNameLoc() const {
770833
if (Name.getNameKind() != DeclarationName::CXXLiteralOperatorName)
771834
return SourceLocation();
772-
return SourceLocation::
773-
getFromRawEncoding(LocInfo.CXXLiteralOperatorName.OpNameLoc);
835+
return LocInfo.getCXXLiteralOperatorNameLoc();
774836
}
775837

776838
/// setCXXLiteralOperatorNameLoc - Sets the location of the literal
777839
/// operator name (not the operator keyword).
778840
/// Assumes it is a literal operator.
779841
void setCXXLiteralOperatorNameLoc(SourceLocation Loc) {
780842
assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName);
781-
LocInfo.CXXLiteralOperatorName.OpNameLoc = Loc.getRawEncoding();
843+
LocInfo = DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(Loc);
782844
}
783845

784846
/// Determine whether this name involves a template parameter.

clang/lib/AST/ASTContext.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5840,9 +5840,8 @@ ASTContext::getNameForTemplate(TemplateName Name,
58405840
} else {
58415841
DName = DeclarationNames.getCXXOperatorName(DTN->getOperator());
58425842
// DNInfo work in progress: FIXME: source locations?
5843-
DeclarationNameLoc DNLoc;
5844-
DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding();
5845-
DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding();
5843+
DeclarationNameLoc DNLoc =
5844+
DeclarationNameLoc::makeCXXOperatorNameLoc(SourceRange());
58465845
return DeclarationNameInfo(DName, NameLoc, DNLoc);
58475846
}
58485847
}

clang/lib/AST/DeclarationName.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -392,14 +392,13 @@ DeclarationNameLoc::DeclarationNameLoc(DeclarationName Name) {
392392
case DeclarationName::CXXConstructorName:
393393
case DeclarationName::CXXDestructorName:
394394
case DeclarationName::CXXConversionFunctionName:
395-
NamedType.TInfo = nullptr;
395+
setNamedTypeLoc(nullptr);
396396
break;
397397
case DeclarationName::CXXOperatorName:
398-
CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding();
399-
CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding();
398+
setCXXOperatorNameRange(SourceRange());
400399
break;
401400
case DeclarationName::CXXLiteralOperatorName:
402-
CXXLiteralOperatorName.OpNameLoc = SourceLocation().getRawEncoding();
401+
setCXXLiteralOperatorNameLoc(SourceLocation());
403402
break;
404403
case DeclarationName::ObjCZeroArgSelector:
405404
case DeclarationName::ObjCOneArgSelector:
@@ -426,7 +425,7 @@ bool DeclarationNameInfo::containsUnexpandedParameterPack() const {
426425
case DeclarationName::CXXConstructorName:
427426
case DeclarationName::CXXDestructorName:
428427
case DeclarationName::CXXConversionFunctionName:
429-
if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo)
428+
if (TypeSourceInfo *TInfo = LocInfo.getNamedTypeInfo())
430429
return TInfo->getType()->containsUnexpandedParameterPack();
431430

432431
return Name.getCXXNameType()->containsUnexpandedParameterPack();
@@ -449,7 +448,7 @@ bool DeclarationNameInfo::isInstantiationDependent() const {
449448
case DeclarationName::CXXConstructorName:
450449
case DeclarationName::CXXDestructorName:
451450
case DeclarationName::CXXConversionFunctionName:
452-
if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo)
451+
if (TypeSourceInfo *TInfo = LocInfo.getNamedTypeInfo())
453452
return TInfo->getType()->isInstantiationDependentType();
454453

455454
return Name.getCXXNameType()->isInstantiationDependentType();
@@ -486,7 +485,7 @@ void DeclarationNameInfo::printName(raw_ostream &OS, PrintingPolicy Policy) cons
486485
case DeclarationName::CXXConstructorName:
487486
case DeclarationName::CXXDestructorName:
488487
case DeclarationName::CXXConversionFunctionName:
489-
if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo) {
488+
if (TypeSourceInfo *TInfo = LocInfo.getNamedTypeInfo()) {
490489
if (Name.getNameKind() == DeclarationName::CXXDestructorName)
491490
OS << '~';
492491
else if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName)
@@ -508,20 +507,16 @@ SourceLocation DeclarationNameInfo::getEndLocPrivate() const {
508507
case DeclarationName::CXXDeductionGuideName:
509508
return NameLoc;
510509

511-
case DeclarationName::CXXOperatorName: {
512-
unsigned raw = LocInfo.CXXOperatorName.EndOpNameLoc;
513-
return SourceLocation::getFromRawEncoding(raw);
514-
}
510+
case DeclarationName::CXXOperatorName:
511+
return LocInfo.getCXXOperatorNameEndLoc();
515512

516-
case DeclarationName::CXXLiteralOperatorName: {
517-
unsigned raw = LocInfo.CXXLiteralOperatorName.OpNameLoc;
518-
return SourceLocation::getFromRawEncoding(raw);
519-
}
513+
case DeclarationName::CXXLiteralOperatorName:
514+
return LocInfo.getCXXLiteralOperatorNameLoc();
520515

521516
case DeclarationName::CXXConstructorName:
522517
case DeclarationName::CXXDestructorName:
523518
case DeclarationName::CXXConversionFunctionName:
524-
if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo)
519+
if (TypeSourceInfo *TInfo = LocInfo.getNamedTypeInfo())
525520
return TInfo->getTypeLoc().getEndLoc();
526521
else
527522
return NameLoc;

clang/lib/Sema/SemaDecl.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5351,10 +5351,8 @@ Sema::GetNameFromUnqualifiedId(const UnqualifiedId &Name) {
53515351
case UnqualifiedIdKind::IK_OperatorFunctionId:
53525352
NameInfo.setName(Context.DeclarationNames.getCXXOperatorName(
53535353
Name.OperatorFunctionId.Operator));
5354-
NameInfo.getInfo().CXXOperatorName.BeginOpNameLoc =
5355-
Name.OperatorFunctionId.SymbolLocations[0].getRawEncoding();
5356-
NameInfo.getInfo().CXXOperatorName.EndOpNameLoc
5357-
= Name.EndLocation.getRawEncoding();
5354+
NameInfo.setCXXOperatorNameRange(SourceRange(
5355+
Name.OperatorFunctionId.SymbolLocations[0], Name.EndLocation));
53585356
return NameInfo;
53595357

53605358
case UnqualifiedIdKind::IK_LiteralOperatorId:

clang/lib/Sema/SemaLambda.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -386,11 +386,8 @@ CXXMethodDecl *Sema::startLambdaDefinition(CXXRecordDecl *Class,
386386
// trailing-return-type respectively.
387387
DeclarationName MethodName
388388
= Context.DeclarationNames.getCXXOperatorName(OO_Call);
389-
DeclarationNameLoc MethodNameLoc;
390-
MethodNameLoc.CXXOperatorName.BeginOpNameLoc
391-
= IntroducerRange.getBegin().getRawEncoding();
392-
MethodNameLoc.CXXOperatorName.EndOpNameLoc
393-
= IntroducerRange.getEnd().getRawEncoding();
389+
DeclarationNameLoc MethodNameLoc =
390+
DeclarationNameLoc::makeCXXOperatorNameLoc(IntroducerRange);
394391
CXXMethodDecl *Method = CXXMethodDecl::Create(
395392
Context, Class, EndLoc,
396393
DeclarationNameInfo(MethodName, IntroducerRange.getBegin(),
@@ -1378,7 +1375,6 @@ static void addFunctionPointerConversion(Sema &S, SourceRange IntroducerRange,
13781375
DeclarationName ConversionName
13791376
= S.Context.DeclarationNames.getCXXConversionFunctionName(
13801377
S.Context.getCanonicalType(PtrToFunctionTy));
1381-
DeclarationNameLoc ConvNameLoc;
13821378
// Construct a TypeSourceInfo for the conversion function, and wire
13831379
// all the parameters appropriately for the FunctionProtoTypeLoc
13841380
// so that everything works during transformation/instantiation of
@@ -1397,7 +1393,8 @@ static void addFunctionPointerConversion(Sema &S, SourceRange IntroducerRange,
13971393
// operators ParmVarDecls below.
13981394
TypeSourceInfo *ConvNamePtrToFunctionTSI =
13991395
S.Context.getTrivialTypeSourceInfo(PtrToFunctionTy, Loc);
1400-
ConvNameLoc.NamedType.TInfo = ConvNamePtrToFunctionTSI;
1396+
DeclarationNameLoc ConvNameLoc =
1397+
DeclarationNameLoc::makeNamedTypeLoc(ConvNamePtrToFunctionTSI);
14011398

14021399
// The conversion function is a conversion to a pointer-to-function.
14031400
TypeSourceInfo *ConvTSI = S.Context.getTrivialTypeSourceInfo(ConvTy, Loc);
@@ -1548,8 +1545,8 @@ static void addBlockPointerConversion(Sema &S,
15481545
DeclarationName Name
15491546
= S.Context.DeclarationNames.getCXXConversionFunctionName(
15501547
S.Context.getCanonicalType(BlockPtrTy));
1551-
DeclarationNameLoc NameLoc;
1552-
NameLoc.NamedType.TInfo = S.Context.getTrivialTypeSourceInfo(BlockPtrTy, Loc);
1548+
DeclarationNameLoc NameLoc = DeclarationNameLoc::makeNamedTypeLoc(
1549+
S.Context.getTrivialTypeSourceInfo(BlockPtrTy, Loc));
15531550
CXXConversionDecl *Conversion = CXXConversionDecl::Create(
15541551
S.Context, Class, Loc, DeclarationNameInfo(Name, Loc, NameLoc), ConvTy,
15551552
S.Context.getTrivialTypeSourceInfo(ConvTy, Loc),

clang/lib/Sema/TreeTransform.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14334,11 +14334,9 @@ TreeTransform<Derived>::RebuildCXXOperatorCallExpr(OverloadedOperatorKind Op,
1433414334
SourceLocation RBrace;
1433514335

1433614336
if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Callee)) {
14337-
DeclarationNameLoc NameLoc = DRE->getNameInfo().getInfo();
14338-
LBrace = SourceLocation::getFromRawEncoding(
14339-
NameLoc.CXXOperatorName.BeginOpNameLoc);
14340-
RBrace = SourceLocation::getFromRawEncoding(
14341-
NameLoc.CXXOperatorName.EndOpNameLoc);
14337+
DeclarationNameLoc NameLoc = DRE->getNameInfo().getInfo();
14338+
LBrace = NameLoc.getCXXOperatorNameBeginLoc();
14339+
RBrace = NameLoc.getCXXOperatorNameEndLoc();
1434214340
} else {
1434314341
LBrace = Callee->getBeginLoc();
1434414342
RBrace = OpLoc;

clang/lib/Serialization/ASTReader.cpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8737,25 +8737,18 @@ ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
87378737

87388738
DeclarationNameLoc
87398739
ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
8740-
DeclarationNameLoc DNLoc;
87418740
switch (Name.getNameKind()) {
87428741
case DeclarationName::CXXConstructorName:
87438742
case DeclarationName::CXXDestructorName:
87448743
case DeclarationName::CXXConversionFunctionName:
8745-
DNLoc.NamedType.TInfo = readTypeSourceInfo();
8746-
break;
8744+
return DeclarationNameLoc::makeNamedTypeLoc(readTypeSourceInfo());
87478745

87488746
case DeclarationName::CXXOperatorName:
8749-
DNLoc.CXXOperatorName.BeginOpNameLoc
8750-
= readSourceLocation().getRawEncoding();
8751-
DNLoc.CXXOperatorName.EndOpNameLoc
8752-
= readSourceLocation().getRawEncoding();
8753-
break;
8747+
return DeclarationNameLoc::makeCXXOperatorNameLoc(readSourceRange());
87548748

87558749
case DeclarationName::CXXLiteralOperatorName:
8756-
DNLoc.CXXLiteralOperatorName.OpNameLoc
8757-
= readSourceLocation().getRawEncoding();
8758-
break;
8750+
return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(
8751+
readSourceLocation());
87598752

87608753
case DeclarationName::Identifier:
87618754
case DeclarationName::ObjCZeroArgSelector:
@@ -8765,7 +8758,7 @@ ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
87658758
case DeclarationName::CXXDeductionGuideName:
87668759
break;
87678760
}
8768-
return DNLoc;
8761+
return DeclarationNameLoc();
87698762
}
87708763

87718764
DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5413,19 +5413,15 @@ void ASTRecordWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
54135413
case DeclarationName::CXXConstructorName:
54145414
case DeclarationName::CXXDestructorName:
54155415
case DeclarationName::CXXConversionFunctionName:
5416-
AddTypeSourceInfo(DNLoc.NamedType.TInfo);
5416+
AddTypeSourceInfo(DNLoc.getNamedTypeInfo());
54175417
break;
54185418

54195419
case DeclarationName::CXXOperatorName:
5420-
AddSourceLocation(SourceLocation::getFromRawEncoding(
5421-
DNLoc.CXXOperatorName.BeginOpNameLoc));
5422-
AddSourceLocation(
5423-
SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc));
5420+
AddSourceRange(DNLoc.getCXXOperatorNameRange());
54245421
break;
54255422

54265423
case DeclarationName::CXXLiteralOperatorName:
5427-
AddSourceLocation(SourceLocation::getFromRawEncoding(
5428-
DNLoc.CXXLiteralOperatorName.OpNameLoc));
5424+
AddSourceLocation(DNLoc.getCXXLiteralOperatorNameLoc());
54295425
break;
54305426

54315427
case DeclarationName::Identifier:

clang/tools/libclang/CIndex.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3351,10 +3351,8 @@ RefNamePieces buildPieces(unsigned NameFlags, bool IsMemberRefExpr,
33513351
Pieces.push_back(*TemplateArgsLoc);
33523352

33533353
if (Kind == DeclarationName::CXXOperatorName) {
3354-
Pieces.push_back(SourceLocation::getFromRawEncoding(
3355-
NI.getInfo().CXXOperatorName.BeginOpNameLoc));
3356-
Pieces.push_back(SourceLocation::getFromRawEncoding(
3357-
NI.getInfo().CXXOperatorName.EndOpNameLoc));
3354+
Pieces.push_back(NI.getInfo().getCXXOperatorNameBeginLoc());
3355+
Pieces.push_back(NI.getInfo().getCXXOperatorNameEndLoc());
33583356
}
33593357

33603358
if (WantSinglePiece) {

0 commit comments

Comments
 (0)