aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMikhail Maltsev <mikhail.maltsev@arm.com>2021-01-27 11:21:01 +0000
committerMikhail Maltsev <mikhail.maltsev@arm.com>2021-01-27 11:21:01 +0000
commit30d9ca1bd9de8286539577d0abfc812ad122861c (patch)
treef7e0af5375b51b310486017c8cc897bc6b955f75
parent48ecba350ed6477ae194720b263eff194bf0271c (diff)
downloadllvm-30d9ca1bd9de8286539577d0abfc812ad122861c.zip
llvm-30d9ca1bd9de8286539577d0abfc812ad122861c.tar.gz
llvm-30d9ca1bd9de8286539577d0abfc812ad122861c.tar.bz2
[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
-rw-r--r--clang/include/clang/AST/DeclarationName.h90
-rw-r--r--clang/lib/AST/ASTContext.cpp5
-rw-r--r--clang/lib/AST/DeclarationName.cpp27
-rw-r--r--clang/lib/Sema/SemaDecl.cpp6
-rw-r--r--clang/lib/Sema/SemaLambda.cpp15
-rw-r--r--clang/lib/Sema/TreeTransform.h8
-rw-r--r--clang/lib/Serialization/ASTReader.cpp17
-rw-r--r--clang/lib/Serialization/ASTWriter.cpp10
-rw-r--r--clang/tools/libclang/CIndex.cpp6
9 files changed, 110 insertions, 74 deletions
diff --git a/clang/include/clang/AST/DeclarationName.h b/clang/include/clang/AST/DeclarationName.h
index 3cb0a02..acf7e24 100644
--- a/clang/include/clang/AST/DeclarationName.h
+++ b/clang/include/clang/AST/DeclarationName.h
@@ -647,7 +647,7 @@ public:
/// DeclarationNameLoc - Additional source/type location info
/// for a declaration name. Needs a DeclarationName in order
/// to be interpreted correctly.
-struct DeclarationNameLoc {
+class DeclarationNameLoc {
// The source location for identifier stored elsewhere.
// struct {} Identifier;
@@ -679,10 +679,78 @@ struct DeclarationNameLoc {
struct CXXLitOpName CXXLiteralOperatorName;
};
- DeclarationNameLoc(DeclarationName Name);
+ void setNamedTypeLoc(TypeSourceInfo *TInfo) { NamedType.TInfo = TInfo; }
+
+ void setCXXOperatorNameRange(SourceRange Range) {
+ CXXOperatorName.BeginOpNameLoc = Range.getBegin().getRawEncoding();
+ CXXOperatorName.EndOpNameLoc = Range.getEnd().getRawEncoding();
+ }
+ void setCXXLiteralOperatorNameLoc(SourceLocation Loc) {
+ CXXLiteralOperatorName.OpNameLoc = Loc.getRawEncoding();
+ }
+
+public:
+ DeclarationNameLoc(DeclarationName Name);
// FIXME: this should go away once all DNLocs are properly initialized.
DeclarationNameLoc() { memset((void*) this, 0, sizeof(*this)); }
+
+ /// Returns the source type info. Assumes that the object stores location
+ /// information of a constructor, destructor or conversion operator.
+ TypeSourceInfo *getNamedTypeInfo() const { return NamedType.TInfo; }
+
+ /// Return the beginning location of the getCXXOperatorNameRange() range.
+ SourceLocation getCXXOperatorNameBeginLoc() const {
+ return SourceLocation::getFromRawEncoding(CXXOperatorName.BeginOpNameLoc);
+ }
+
+ /// Return the end location of the getCXXOperatorNameRange() range.
+ SourceLocation getCXXOperatorNameEndLoc() const {
+ return SourceLocation::getFromRawEncoding(CXXOperatorName.EndOpNameLoc);
+ }
+
+ /// Return the range of the operator name (without the operator keyword).
+ /// Assumes that the object stores location information of a (non-literal)
+ /// operator.
+ SourceRange getCXXOperatorNameRange() const {
+ return SourceRange(getCXXOperatorNameBeginLoc(),
+ getCXXOperatorNameEndLoc());
+ }
+
+ /// Return the location of the literal operator name (without the operator
+ /// keyword). Assumes that the object stores location information of a literal
+ /// operator.
+ SourceLocation getCXXLiteralOperatorNameLoc() const {
+ return SourceLocation::getFromRawEncoding(CXXLiteralOperatorName.OpNameLoc);
+ }
+
+ /// Construct location information for a constructor, destructor or conversion
+ /// operator.
+ static DeclarationNameLoc makeNamedTypeLoc(TypeSourceInfo *TInfo) {
+ DeclarationNameLoc DNL;
+ DNL.setNamedTypeLoc(TInfo);
+ return DNL;
+ }
+
+ /// Construct location information for a non-literal C++ operator.
+ static DeclarationNameLoc makeCXXOperatorNameLoc(SourceLocation BeginLoc,
+ SourceLocation EndLoc) {
+ return makeCXXOperatorNameLoc(SourceRange(BeginLoc, EndLoc));
+ }
+
+ /// Construct location information for a non-literal C++ operator.
+ static DeclarationNameLoc makeCXXOperatorNameLoc(SourceRange Range) {
+ DeclarationNameLoc DNL;
+ DNL.setCXXOperatorNameRange(Range);
+ return DNL;
+ }
+
+ /// Construct location information for a literal C++ operator.
+ static DeclarationNameLoc makeCXXLiteralOperatorNameLoc(SourceLocation Loc) {
+ DeclarationNameLoc DNL;
+ DNL.setCXXLiteralOperatorNameLoc(Loc);
+ return DNL;
+ }
};
/// DeclarationNameInfo - A collector data type for bundling together
@@ -722,7 +790,6 @@ public:
void setLoc(SourceLocation L) { NameLoc = L; }
const DeclarationNameLoc &getInfo() const { return LocInfo; }
- DeclarationNameLoc &getInfo() { return LocInfo; }
void setInfo(const DeclarationNameLoc &Info) { LocInfo = Info; }
/// getNamedTypeInfo - Returns the source type info associated to
@@ -732,7 +799,7 @@ public:
Name.getNameKind() != DeclarationName::CXXDestructorName &&
Name.getNameKind() != DeclarationName::CXXConversionFunctionName)
return nullptr;
- return LocInfo.NamedType.TInfo;
+ return LocInfo.getNamedTypeInfo();
}
/// setNamedTypeInfo - Sets the source type info associated to
@@ -741,7 +808,7 @@ public:
assert(Name.getNameKind() == DeclarationName::CXXConstructorName ||
Name.getNameKind() == DeclarationName::CXXDestructorName ||
Name.getNameKind() == DeclarationName::CXXConversionFunctionName);
- LocInfo.NamedType.TInfo = TInfo;
+ LocInfo = DeclarationNameLoc::makeNamedTypeLoc(TInfo);
}
/// getCXXOperatorNameRange - Gets the range of the operator name
@@ -749,18 +816,14 @@ public:
SourceRange getCXXOperatorNameRange() const {
if (Name.getNameKind() != DeclarationName::CXXOperatorName)
return SourceRange();
- return SourceRange(
- SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.BeginOpNameLoc),
- SourceLocation::getFromRawEncoding(LocInfo.CXXOperatorName.EndOpNameLoc)
- );
+ return LocInfo.getCXXOperatorNameRange();
}
/// setCXXOperatorNameRange - Sets the range of the operator name
/// (without the operator keyword). Assumes it is a C++ operator.
void setCXXOperatorNameRange(SourceRange R) {
assert(Name.getNameKind() == DeclarationName::CXXOperatorName);
- LocInfo.CXXOperatorName.BeginOpNameLoc = R.getBegin().getRawEncoding();
- LocInfo.CXXOperatorName.EndOpNameLoc = R.getEnd().getRawEncoding();
+ LocInfo = DeclarationNameLoc::makeCXXOperatorNameLoc(R);
}
/// getCXXLiteralOperatorNameLoc - Returns the location of the literal
@@ -769,8 +832,7 @@ public:
SourceLocation getCXXLiteralOperatorNameLoc() const {
if (Name.getNameKind() != DeclarationName::CXXLiteralOperatorName)
return SourceLocation();
- return SourceLocation::
- getFromRawEncoding(LocInfo.CXXLiteralOperatorName.OpNameLoc);
+ return LocInfo.getCXXLiteralOperatorNameLoc();
}
/// setCXXLiteralOperatorNameLoc - Sets the location of the literal
@@ -778,7 +840,7 @@ public:
/// Assumes it is a literal operator.
void setCXXLiteralOperatorNameLoc(SourceLocation Loc) {
assert(Name.getNameKind() == DeclarationName::CXXLiteralOperatorName);
- LocInfo.CXXLiteralOperatorName.OpNameLoc = Loc.getRawEncoding();
+ LocInfo = DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(Loc);
}
/// Determine whether this name involves a template parameter.
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index cb7f00a..6b8425f 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -5840,9 +5840,8 @@ ASTContext::getNameForTemplate(TemplateName Name,
} else {
DName = DeclarationNames.getCXXOperatorName(DTN->getOperator());
// DNInfo work in progress: FIXME: source locations?
- DeclarationNameLoc DNLoc;
- DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding();
- DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding();
+ DeclarationNameLoc DNLoc =
+ DeclarationNameLoc::makeCXXOperatorNameLoc(SourceRange());
return DeclarationNameInfo(DName, NameLoc, DNLoc);
}
}
diff --git a/clang/lib/AST/DeclarationName.cpp b/clang/lib/AST/DeclarationName.cpp
index ecf676c..56cf4b4 100644
--- a/clang/lib/AST/DeclarationName.cpp
+++ b/clang/lib/AST/DeclarationName.cpp
@@ -392,14 +392,13 @@ DeclarationNameLoc::DeclarationNameLoc(DeclarationName Name) {
case DeclarationName::CXXConstructorName:
case DeclarationName::CXXDestructorName:
case DeclarationName::CXXConversionFunctionName:
- NamedType.TInfo = nullptr;
+ setNamedTypeLoc(nullptr);
break;
case DeclarationName::CXXOperatorName:
- CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding();
- CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding();
+ setCXXOperatorNameRange(SourceRange());
break;
case DeclarationName::CXXLiteralOperatorName:
- CXXLiteralOperatorName.OpNameLoc = SourceLocation().getRawEncoding();
+ setCXXLiteralOperatorNameLoc(SourceLocation());
break;
case DeclarationName::ObjCZeroArgSelector:
case DeclarationName::ObjCOneArgSelector:
@@ -426,7 +425,7 @@ bool DeclarationNameInfo::containsUnexpandedParameterPack() const {
case DeclarationName::CXXConstructorName:
case DeclarationName::CXXDestructorName:
case DeclarationName::CXXConversionFunctionName:
- if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo)
+ if (TypeSourceInfo *TInfo = LocInfo.getNamedTypeInfo())
return TInfo->getType()->containsUnexpandedParameterPack();
return Name.getCXXNameType()->containsUnexpandedParameterPack();
@@ -449,7 +448,7 @@ bool DeclarationNameInfo::isInstantiationDependent() const {
case DeclarationName::CXXConstructorName:
case DeclarationName::CXXDestructorName:
case DeclarationName::CXXConversionFunctionName:
- if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo)
+ if (TypeSourceInfo *TInfo = LocInfo.getNamedTypeInfo())
return TInfo->getType()->isInstantiationDependentType();
return Name.getCXXNameType()->isInstantiationDependentType();
@@ -486,7 +485,7 @@ void DeclarationNameInfo::printName(raw_ostream &OS, PrintingPolicy Policy) cons
case DeclarationName::CXXConstructorName:
case DeclarationName::CXXDestructorName:
case DeclarationName::CXXConversionFunctionName:
- if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo) {
+ if (TypeSourceInfo *TInfo = LocInfo.getNamedTypeInfo()) {
if (Name.getNameKind() == DeclarationName::CXXDestructorName)
OS << '~';
else if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName)
@@ -508,20 +507,16 @@ SourceLocation DeclarationNameInfo::getEndLocPrivate() const {
case DeclarationName::CXXDeductionGuideName:
return NameLoc;
- case DeclarationName::CXXOperatorName: {
- unsigned raw = LocInfo.CXXOperatorName.EndOpNameLoc;
- return SourceLocation::getFromRawEncoding(raw);
- }
+ case DeclarationName::CXXOperatorName:
+ return LocInfo.getCXXOperatorNameEndLoc();
- case DeclarationName::CXXLiteralOperatorName: {
- unsigned raw = LocInfo.CXXLiteralOperatorName.OpNameLoc;
- return SourceLocation::getFromRawEncoding(raw);
- }
+ case DeclarationName::CXXLiteralOperatorName:
+ return LocInfo.getCXXLiteralOperatorNameLoc();
case DeclarationName::CXXConstructorName:
case DeclarationName::CXXDestructorName:
case DeclarationName::CXXConversionFunctionName:
- if (TypeSourceInfo *TInfo = LocInfo.NamedType.TInfo)
+ if (TypeSourceInfo *TInfo = LocInfo.getNamedTypeInfo())
return TInfo->getTypeLoc().getEndLoc();
else
return NameLoc;
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 3ee0c43..ef4947b 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -5351,10 +5351,8 @@ Sema::GetNameFromUnqualifiedId(const UnqualifiedId &Name) {
case UnqualifiedIdKind::IK_OperatorFunctionId:
NameInfo.setName(Context.DeclarationNames.getCXXOperatorName(
Name.OperatorFunctionId.Operator));
- NameInfo.getInfo().CXXOperatorName.BeginOpNameLoc =
- Name.OperatorFunctionId.SymbolLocations[0].getRawEncoding();
- NameInfo.getInfo().CXXOperatorName.EndOpNameLoc
- = Name.EndLocation.getRawEncoding();
+ NameInfo.setCXXOperatorNameRange(SourceRange(
+ Name.OperatorFunctionId.SymbolLocations[0], Name.EndLocation));
return NameInfo;
case UnqualifiedIdKind::IK_LiteralOperatorId:
diff --git a/clang/lib/Sema/SemaLambda.cpp b/clang/lib/Sema/SemaLambda.cpp
index af61c82..f066acf 100644
--- a/clang/lib/Sema/SemaLambda.cpp
+++ b/clang/lib/Sema/SemaLambda.cpp
@@ -386,11 +386,8 @@ CXXMethodDecl *Sema::startLambdaDefinition(CXXRecordDecl *Class,
// trailing-return-type respectively.
DeclarationName MethodName
= Context.DeclarationNames.getCXXOperatorName(OO_Call);
- DeclarationNameLoc MethodNameLoc;
- MethodNameLoc.CXXOperatorName.BeginOpNameLoc
- = IntroducerRange.getBegin().getRawEncoding();
- MethodNameLoc.CXXOperatorName.EndOpNameLoc
- = IntroducerRange.getEnd().getRawEncoding();
+ DeclarationNameLoc MethodNameLoc =
+ DeclarationNameLoc::makeCXXOperatorNameLoc(IntroducerRange);
CXXMethodDecl *Method = CXXMethodDecl::Create(
Context, Class, EndLoc,
DeclarationNameInfo(MethodName, IntroducerRange.getBegin(),
@@ -1378,7 +1375,6 @@ static void addFunctionPointerConversion(Sema &S, SourceRange IntroducerRange,
DeclarationName ConversionName
= S.Context.DeclarationNames.getCXXConversionFunctionName(
S.Context.getCanonicalType(PtrToFunctionTy));
- DeclarationNameLoc ConvNameLoc;
// Construct a TypeSourceInfo for the conversion function, and wire
// all the parameters appropriately for the FunctionProtoTypeLoc
// so that everything works during transformation/instantiation of
@@ -1397,7 +1393,8 @@ static void addFunctionPointerConversion(Sema &S, SourceRange IntroducerRange,
// operators ParmVarDecls below.
TypeSourceInfo *ConvNamePtrToFunctionTSI =
S.Context.getTrivialTypeSourceInfo(PtrToFunctionTy, Loc);
- ConvNameLoc.NamedType.TInfo = ConvNamePtrToFunctionTSI;
+ DeclarationNameLoc ConvNameLoc =
+ DeclarationNameLoc::makeNamedTypeLoc(ConvNamePtrToFunctionTSI);
// The conversion function is a conversion to a pointer-to-function.
TypeSourceInfo *ConvTSI = S.Context.getTrivialTypeSourceInfo(ConvTy, Loc);
@@ -1548,8 +1545,8 @@ static void addBlockPointerConversion(Sema &S,
DeclarationName Name
= S.Context.DeclarationNames.getCXXConversionFunctionName(
S.Context.getCanonicalType(BlockPtrTy));
- DeclarationNameLoc NameLoc;
- NameLoc.NamedType.TInfo = S.Context.getTrivialTypeSourceInfo(BlockPtrTy, Loc);
+ DeclarationNameLoc NameLoc = DeclarationNameLoc::makeNamedTypeLoc(
+ S.Context.getTrivialTypeSourceInfo(BlockPtrTy, Loc));
CXXConversionDecl *Conversion = CXXConversionDecl::Create(
S.Context, Class, Loc, DeclarationNameInfo(Name, Loc, NameLoc), ConvTy,
S.Context.getTrivialTypeSourceInfo(ConvTy, Loc),
diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index 0a596e5..9da07a1 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -14334,11 +14334,9 @@ TreeTransform<Derived>::RebuildCXXOperatorCallExpr(OverloadedOperatorKind Op,
SourceLocation RBrace;
if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Callee)) {
- DeclarationNameLoc NameLoc = DRE->getNameInfo().getInfo();
- LBrace = SourceLocation::getFromRawEncoding(
- NameLoc.CXXOperatorName.BeginOpNameLoc);
- RBrace = SourceLocation::getFromRawEncoding(
- NameLoc.CXXOperatorName.EndOpNameLoc);
+ DeclarationNameLoc NameLoc = DRE->getNameInfo().getInfo();
+ LBrace = NameLoc.getCXXOperatorNameBeginLoc();
+ RBrace = NameLoc.getCXXOperatorNameEndLoc();
} else {
LBrace = Callee->getBeginLoc();
RBrace = OpLoc;
diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp
index 1f68f6bc..85add2e 100644
--- a/clang/lib/Serialization/ASTReader.cpp
+++ b/clang/lib/Serialization/ASTReader.cpp
@@ -8737,25 +8737,18 @@ ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
DeclarationNameLoc
ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
- DeclarationNameLoc DNLoc;
switch (Name.getNameKind()) {
case DeclarationName::CXXConstructorName:
case DeclarationName::CXXDestructorName:
case DeclarationName::CXXConversionFunctionName:
- DNLoc.NamedType.TInfo = readTypeSourceInfo();
- break;
+ return DeclarationNameLoc::makeNamedTypeLoc(readTypeSourceInfo());
case DeclarationName::CXXOperatorName:
- DNLoc.CXXOperatorName.BeginOpNameLoc
- = readSourceLocation().getRawEncoding();
- DNLoc.CXXOperatorName.EndOpNameLoc
- = readSourceLocation().getRawEncoding();
- break;
+ return DeclarationNameLoc::makeCXXOperatorNameLoc(readSourceRange());
case DeclarationName::CXXLiteralOperatorName:
- DNLoc.CXXLiteralOperatorName.OpNameLoc
- = readSourceLocation().getRawEncoding();
- break;
+ return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(
+ readSourceLocation());
case DeclarationName::Identifier:
case DeclarationName::ObjCZeroArgSelector:
@@ -8765,7 +8758,7 @@ ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
case DeclarationName::CXXDeductionGuideName:
break;
}
- return DNLoc;
+ return DeclarationNameLoc();
}
DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp
index 6bfa7b0..b940020 100644
--- a/clang/lib/Serialization/ASTWriter.cpp
+++ b/clang/lib/Serialization/ASTWriter.cpp
@@ -5413,19 +5413,15 @@ void ASTRecordWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
case DeclarationName::CXXConstructorName:
case DeclarationName::CXXDestructorName:
case DeclarationName::CXXConversionFunctionName:
- AddTypeSourceInfo(DNLoc.NamedType.TInfo);
+ AddTypeSourceInfo(DNLoc.getNamedTypeInfo());
break;
case DeclarationName::CXXOperatorName:
- AddSourceLocation(SourceLocation::getFromRawEncoding(
- DNLoc.CXXOperatorName.BeginOpNameLoc));
- AddSourceLocation(
- SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc));
+ AddSourceRange(DNLoc.getCXXOperatorNameRange());
break;
case DeclarationName::CXXLiteralOperatorName:
- AddSourceLocation(SourceLocation::getFromRawEncoding(
- DNLoc.CXXLiteralOperatorName.OpNameLoc));
+ AddSourceLocation(DNLoc.getCXXLiteralOperatorNameLoc());
break;
case DeclarationName::Identifier:
diff --git a/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp
index f100831..3ae30c1 100644
--- a/clang/tools/libclang/CIndex.cpp
+++ b/clang/tools/libclang/CIndex.cpp
@@ -3351,10 +3351,8 @@ RefNamePieces buildPieces(unsigned NameFlags, bool IsMemberRefExpr,
Pieces.push_back(*TemplateArgsLoc);
if (Kind == DeclarationName::CXXOperatorName) {
- Pieces.push_back(SourceLocation::getFromRawEncoding(
- NI.getInfo().CXXOperatorName.BeginOpNameLoc));
- Pieces.push_back(SourceLocation::getFromRawEncoding(
- NI.getInfo().CXXOperatorName.EndOpNameLoc));
+ Pieces.push_back(NI.getInfo().getCXXOperatorNameBeginLoc());
+ Pieces.push_back(NI.getInfo().getCXXOperatorNameEndLoc());
}
if (WantSinglePiece) {