diff --git a/clang/include/clang/AST/DeclarationName.h b/clang/include/clang/AST/DeclarationName.h --- a/clang/include/clang/AST/DeclarationName.h +++ b/clang/include/clang/AST/DeclarationName.h @@ -647,7 +647,7 @@ /// 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 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ /// 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 --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -5840,9 +5840,8 @@ } 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 --- a/clang/lib/AST/DeclarationName.cpp +++ b/clang/lib/AST/DeclarationName.cpp @@ -392,14 +392,13 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -5351,10 +5351,8 @@ 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 --- a/clang/lib/Sema/SemaLambda.cpp +++ b/clang/lib/Sema/SemaLambda.cpp @@ -386,11 +386,8 @@ // 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 @@ 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 @@ // 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 @@ 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 --- a/clang/lib/Sema/TreeTransform.h +++ b/clang/lib/Sema/TreeTransform.h @@ -14334,11 +14334,9 @@ SourceLocation RBrace; if (DeclRefExpr *DRE = dyn_cast(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 --- a/clang/lib/Serialization/ASTReader.cpp +++ b/clang/lib/Serialization/ASTReader.cpp @@ -8737,25 +8737,18 @@ 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 @@ 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 --- a/clang/lib/Serialization/ASTWriter.cpp +++ b/clang/lib/Serialization/ASTWriter.cpp @@ -5413,19 +5413,15 @@ 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 --- a/clang/tools/libclang/CIndex.cpp +++ b/clang/tools/libclang/CIndex.cpp @@ -3351,10 +3351,8 @@ 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) {