From 6adc78e0df73d1f7dca4f0a39fe1473b4c2bd346 Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Mon, 18 Feb 2013 22:06:02 +0000 Subject: [PATCH] Replace TypeLoc llvm::cast support to be well-defined. The TypeLoc hierarchy used the llvm::cast machinery to perform undefined behavior by casting pointers/references to TypeLoc objects to derived types and then using the derived copy constructors (or even returning pointers to derived types that actually point to the original TypeLoc object). Some context is in this thread: http://lists.cs.uiuc.edu/pipermail/llvmdev/2012-December/056804.html Though it's spread over a few months which can be hard to read in the mail archive. llvm-svn: 175462 --- clang/include/clang/AST/RecursiveASTVisitor.h | 5 +- clang/include/clang/AST/TypeLoc.h | 78 +++++++++++++++++--------- clang/include/clang/AST/TypeLocVisitor.h | 2 +- clang/lib/ARCMigrate/TransGCAttrs.cpp | 28 ++++----- clang/lib/AST/ASTContext.cpp | 4 +- clang/lib/AST/Comment.cpp | 26 ++++----- clang/lib/AST/MicrosoftMangle.cpp | 2 +- clang/lib/AST/TemplateBase.cpp | 4 +- clang/lib/AST/TypeLoc.cpp | 13 +++-- clang/lib/Sema/SemaChecking.cpp | 9 ++- clang/lib/Sema/SemaCodeComplete.cpp | 35 ++++++------ clang/lib/Sema/SemaDecl.cpp | 46 +++++++-------- clang/lib/Sema/SemaDeclAttr.cpp | 4 +- clang/lib/Sema/SemaDeclCXX.cpp | 21 +++---- clang/lib/Sema/SemaExceptionSpec.cpp | 4 +- clang/lib/Sema/SemaExpr.cpp | 7 +-- clang/lib/Sema/SemaInit.cpp | 6 +- clang/lib/Sema/SemaStmt.cpp | 2 +- clang/lib/Sema/SemaTemplate.cpp | 16 +++--- clang/lib/Sema/SemaTemplateInstantiate.cpp | 15 +++-- clang/lib/Sema/SemaTemplateInstantiateDecl.cpp | 25 ++++----- clang/lib/Sema/SemaTemplateVariadic.cpp | 3 +- clang/lib/Sema/SemaType.cpp | 25 ++++----- clang/lib/Sema/TreeTransform.h | 35 ++++++------ clang/lib/Sema/TypeLocBuilder.h | 6 +- clang/tools/libclang/CIndex.cpp | 18 +++--- clang/tools/libclang/CXCursor.cpp | 2 +- clang/tools/libclang/IndexingContext.cpp | 20 +++---- clang/tools/libclang/RecursiveASTVisitor.h | 5 +- 29 files changed, 245 insertions(+), 221 deletions(-) diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h index cc62278..faec5eb 100644 --- a/clang/include/clang/AST/RecursiveASTVisitor.h +++ b/clang/include/clang/AST/RecursiveASTVisitor.h @@ -594,7 +594,7 @@ bool RecursiveASTVisitor::TraverseTypeLoc(TypeLoc TL) { #define ABSTRACT_TYPELOC(CLASS, BASE) #define TYPELOC(CLASS, BASE) \ case TypeLoc::CLASS: \ - return getDerived().Traverse##CLASS##TypeLoc(*cast(&TL)); + return getDerived().Traverse##CLASS##TypeLoc(TL.castAs()); #include "clang/AST/TypeLocNodes.def" } @@ -2100,8 +2100,7 @@ bool RecursiveASTVisitor::TraverseLambdaExpr(LambdaExpr *S) { if (S->hasExplicitParameters() && S->hasExplicitResultType()) { // Visit the whole type. TRY_TO(TraverseTypeLoc(TL)); - } else if (isa(TL)) { - FunctionProtoTypeLoc Proto = cast(TL); + } else if (FunctionProtoTypeLoc Proto = TL.getAs()) { if (S->hasExplicitParameters()) { // Visit parameters. for (unsigned I = 0, N = Proto.getNumArgs(); I != N; ++I) { diff --git a/clang/include/clang/AST/TypeLoc.h b/clang/include/clang/AST/TypeLoc.h index 20584c8..d58b79a 100644 --- a/clang/include/clang/AST/TypeLoc.h +++ b/clang/include/clang/AST/TypeLoc.h @@ -44,6 +44,28 @@ protected: void *Data; public: + template + T castAs() const { + assert(T::isType(this)); + T t; + TypeLoc& tl = t; + tl = *this; + return t; + } + template + T getAs() const { + if (!T::isType(this)) + return T(); + T t; + TypeLoc& tl = t; + tl = *this; + return t; + } + + static bool isType(const TypeLoc*) { + return true; + } + /// The kinds of TypeLocs. Equivalent to the Type::TypeClass enum, /// except it also defines a Qualified enum that corresponds to the /// QualifiedLoc class. @@ -119,11 +141,7 @@ public: /// \brief Skips past any qualifiers, if this is qualified. UnqualTypeLoc getUnqualifiedLoc() const; // implemented in this header - TypeLoc IgnoreParens() const { - if (isa(this)) - return IgnoreParensImpl(*this); - return *this; - } + TypeLoc IgnoreParens() const; /// \brief Initializes this to state that every location in this /// type is the given location. @@ -187,7 +205,9 @@ public: return (TypeLocClass) getTypePtr()->getTypeClass(); } - static bool classof(const TypeLoc *TL) { +private: + friend class TypeLoc; + static bool isType(const TypeLoc *TL) { return !TL->getType().hasLocalQualifiers(); } }; @@ -231,15 +251,17 @@ public: getFullDataSizeForType(getType().getLocalUnqualifiedType()); } - static bool classof(const TypeLoc *TL) { +private: + friend class TypeLoc; + static bool isType(const TypeLoc *TL) { return TL->getType().hasLocalQualifiers(); } }; inline UnqualTypeLoc TypeLoc::getUnqualifiedLoc() const { - if (isa(this)) - return cast(this)->getUnqualifiedLoc(); - return cast(*this); + if (QualifiedTypeLoc Loc = getAs()) + return Loc.getUnqualifiedLoc(); + return castAs(); } /// A metaprogramming base class for TypeLoc classes which correspond @@ -280,24 +302,22 @@ class ConcreteTypeLoc : public Base { return static_cast(this); } -public: - unsigned getLocalDataSize() const { - return sizeof(LocalData) + asDerived()->getExtraLocalDataSize(); - } - // Give a default implementation that's useful for leaf types. - unsigned getFullDataSize() const { - return asDerived()->getLocalDataSize() + getInnerTypeSize(); + friend class TypeLoc; + static bool isType(const TypeLoc *TL) { + return Derived::classofType(TL->getTypePtr()); } static bool classofType(const Type *Ty) { return TypeClass::classof(Ty); } - static bool classof(const TypeLoc *TL) { - return Derived::classofType(TL->getTypePtr()); +public: + unsigned getLocalDataSize() const { + return sizeof(LocalData) + asDerived()->getExtraLocalDataSize(); } - static bool classof(const UnqualTypeLoc *TL) { - return Derived::classofType(TL->getTypePtr()); + // Give a default implementation that's useful for leaf types. + unsigned getFullDataSize() const { + return asDerived()->getLocalDataSize() + getInnerTypeSize(); } TypeLoc getNextTypeLoc() const { @@ -362,18 +382,19 @@ private: /// information. See the note on ConcreteTypeLoc. template class InheritingConcreteTypeLoc : public Base { -public: + friend class TypeLoc; static bool classofType(const Type *Ty) { return TypeClass::classof(Ty); } - static bool classof(const TypeLoc *TL) { + static bool isType(const TypeLoc *TL) { return Derived::classofType(TL->getTypePtr()); } - static bool classof(const UnqualTypeLoc *TL) { + static bool isType(const UnqualTypeLoc *TL) { return Derived::classofType(TL->getTypePtr()); } +public: const TypeClass *getTypePtr() const { return cast(Base::getTypePtr()); } @@ -406,7 +427,9 @@ public: setNameLoc(Loc); } - static bool classof(const TypeLoc *TL); +private: + friend class TypeLoc; + static bool isType(const TypeLoc *TL); }; @@ -899,6 +922,11 @@ public: } }; +inline TypeLoc TypeLoc::IgnoreParens() const { + if (ParenTypeLoc::isType(this)) + return IgnoreParensImpl(*this); + return *this; +} struct PointerLikeLocInfo { SourceLocation StarLoc; diff --git a/clang/include/clang/AST/TypeLocVisitor.h b/clang/include/clang/AST/TypeLocVisitor.h index 50fc439..db5775a 100644 --- a/clang/include/clang/AST/TypeLocVisitor.h +++ b/clang/include/clang/AST/TypeLocVisitor.h @@ -21,7 +21,7 @@ namespace clang { #define DISPATCH(CLASSNAME) \ return static_cast(this)-> \ - Visit##CLASSNAME(cast(TyLoc)) + Visit##CLASSNAME(TyLoc.castAs()) template class TypeLocVisitor { diff --git a/clang/lib/ARCMigrate/TransGCAttrs.cpp b/clang/lib/ARCMigrate/TransGCAttrs.cpp index eec7306..d8be1ae 100644 --- a/clang/lib/ARCMigrate/TransGCAttrs.cpp +++ b/clang/lib/ARCMigrate/TransGCAttrs.cpp @@ -63,19 +63,18 @@ public: return; TypeLoc TL = TInfo->getTypeLoc(); while (TL) { - if (const QualifiedTypeLoc *QL = dyn_cast(&TL)) { - TL = QL->getUnqualifiedLoc(); - } else if (const AttributedTypeLoc * - Attr = dyn_cast(&TL)) { - if (handleAttr(*Attr, D)) + if (QualifiedTypeLoc QL = TL.getAs()) { + TL = QL.getUnqualifiedLoc(); + } else if (AttributedTypeLoc Attr = TL.getAs()) { + if (handleAttr(Attr, D)) break; - TL = Attr->getModifiedLoc(); - } else if (const ArrayTypeLoc *Arr = dyn_cast(&TL)) { - TL = Arr->getElementLoc(); - } else if (const PointerTypeLoc *PT = dyn_cast(&TL)) { - TL = PT->getPointeeLoc(); - } else if (const ReferenceTypeLoc *RT = dyn_cast(&TL)) - TL = RT->getPointeeLoc(); + TL = Attr.getModifiedLoc(); + } else if (ArrayTypeLoc Arr = TL.getAs()) { + TL = Arr.getElementLoc(); + } else if (PointerTypeLoc PT = TL.getAs()) { + TL = PT.getPointeeLoc(); + } else if (ReferenceTypeLoc RT = TL.getAs()) + TL = RT.getPointeeLoc(); else break; } @@ -249,8 +248,9 @@ static void checkAllAtProps(MigrationContext &MigrateCtx, if (!TInfo) return; TypeLoc TL = TInfo->getTypeLoc(); - if (AttributedTypeLoc *ATL = dyn_cast(&TL)) { - ATLs.push_back(std::make_pair(*ATL, PD)); + if (AttributedTypeLoc ATL = + TL.getAs()) { + ATLs.push_back(std::make_pair(ATL, PD)); if (TInfo->getType().getObjCLifetime() == Qualifiers::OCL_Weak) { hasWeak = true; } else if (TInfo->getType().getObjCLifetime() == Qualifiers::OCL_Strong) diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp index 62fd9da..952162e 100644 --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -2957,8 +2957,8 @@ ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, QualType TST = getTemplateSpecializationType(Name, Args, Underlying); TypeSourceInfo *DI = CreateTypeSourceInfo(TST); - TemplateSpecializationTypeLoc TL - = cast(DI->getTypeLoc()); + TemplateSpecializationTypeLoc TL = + DI->getTypeLoc().castAs(); TL.setTemplateKeywordLoc(SourceLocation()); TL.setTemplateNameLoc(NameLoc); TL.setLAngleLoc(Args.getLAngleLoc()); diff --git a/clang/lib/AST/Comment.cpp b/clang/lib/AST/Comment.cpp index ea1a3f7..db55c04 100644 --- a/clang/lib/AST/Comment.cpp +++ b/clang/lib/AST/Comment.cpp @@ -241,32 +241,32 @@ void DeclInfo::fill() { while (true) { TL = TL.IgnoreParens(); // Look through qualified types. - if (QualifiedTypeLoc *QualifiedTL = dyn_cast(&TL)) { - TL = QualifiedTL->getUnqualifiedLoc(); + if (QualifiedTypeLoc QualifiedTL = TL.getAs()) { + TL = QualifiedTL.getUnqualifiedLoc(); continue; } // Look through pointer types. - if (PointerTypeLoc *PointerTL = dyn_cast(&TL)) { - TL = PointerTL->getPointeeLoc().getUnqualifiedLoc(); + if (PointerTypeLoc PointerTL = TL.getAs()) { + TL = PointerTL.getPointeeLoc().getUnqualifiedLoc(); continue; } - if (BlockPointerTypeLoc *BlockPointerTL = - dyn_cast(&TL)) { - TL = BlockPointerTL->getPointeeLoc().getUnqualifiedLoc(); + if (BlockPointerTypeLoc BlockPointerTL = + TL.getAs()) { + TL = BlockPointerTL.getPointeeLoc().getUnqualifiedLoc(); continue; } - if (MemberPointerTypeLoc *MemberPointerTL = - dyn_cast(&TL)) { - TL = MemberPointerTL->getPointeeLoc().getUnqualifiedLoc(); + if (MemberPointerTypeLoc MemberPointerTL = + TL.getAs()) { + TL = MemberPointerTL.getPointeeLoc().getUnqualifiedLoc(); continue; } // Is this a typedef for a function type? - if (FunctionTypeLoc *FTL = dyn_cast(&TL)) { + if (FunctionTypeLoc FTL = TL.getAs()) { Kind = FunctionKind; - ArrayRef Params = FTL->getParams(); + ArrayRef Params = FTL.getParams(); ParamVars = ArrayRef(Params.data(), Params.size()); - ResultType = FTL->getResultLoc().getType(); + ResultType = FTL.getResultLoc().getType(); break; } break; diff --git a/clang/lib/AST/MicrosoftMangle.cpp b/clang/lib/AST/MicrosoftMangle.cpp index baa16b0..b0fc25c 100644 --- a/clang/lib/AST/MicrosoftMangle.cpp +++ b/clang/lib/AST/MicrosoftMangle.cpp @@ -396,7 +396,7 @@ isTemplate(const NamedDecl *ND, TypeSourceInfo *TSI = Spec->getTypeAsWritten(); if (TSI) { TemplateSpecializationTypeLoc TSTL = - cast(TSI->getTypeLoc()); + TSI->getTypeLoc().castAs(); TemplateArgumentListInfo LI(TSTL.getLAngleLoc(), TSTL.getRAngleLoc()); for (unsigned i = 0, e = TSTL.getNumArgs(); i != e; ++i) TemplateArgs.push_back(TSTL.getArgLoc(i)); diff --git a/clang/lib/AST/TemplateBase.cpp b/clang/lib/AST/TemplateBase.cpp index 6519340..f1b5e4b 100644 --- a/clang/lib/AST/TemplateBase.cpp +++ b/clang/lib/AST/TemplateBase.cpp @@ -466,8 +466,8 @@ TemplateArgumentLoc::getPackExpansionPattern(SourceLocation &Ellipsis, ExpansionTSInfo = Context.getTrivialTypeSourceInfo( getArgument().getAsType(), Ellipsis); - PackExpansionTypeLoc Expansion - = cast(ExpansionTSInfo->getTypeLoc()); + PackExpansionTypeLoc Expansion = + ExpansionTSInfo->getTypeLoc().castAs(); Ellipsis = Expansion.getEllipsisLoc(); TypeLoc Pattern = Expansion.getPatternLoc(); diff --git a/clang/lib/AST/TypeLoc.cpp b/clang/lib/AST/TypeLoc.cpp index 010ea2d..fe66714 100644 --- a/clang/lib/AST/TypeLoc.cpp +++ b/clang/lib/AST/TypeLoc.cpp @@ -86,7 +86,7 @@ void TypeLoc::initializeImpl(ASTContext &Context, TypeLoc TL, #define ABSTRACT_TYPELOC(CLASS, PARENT) #define TYPELOC(CLASS, PARENT) \ case CLASS: { \ - CLASS##TypeLoc TLCasted = cast(TL); \ + CLASS##TypeLoc TLCasted = TL.castAs(); \ TLCasted.initializeLocal(Context, Loc); \ TL = TLCasted.getNextTypeLoc(); \ if (!TL) return; \ @@ -106,7 +106,8 @@ SourceLocation TypeLoc::getBeginLoc() const { LeftMost = Cur; break; case FunctionProto: - if (cast(&Cur)->getTypePtr()->hasTrailingReturn()) { + if (Cur.castAs().getTypePtr() + ->hasTrailingReturn()) { LeftMost = Cur; break; } @@ -151,7 +152,7 @@ SourceLocation TypeLoc::getEndLoc() const { Last = Cur; break; case FunctionProto: - if (cast(&Cur)->getTypePtr()->hasTrailingReturn()) + if (Cur.castAs().getTypePtr()->hasTrailingReturn()) Last = TypeLoc(); else Last = Cur; @@ -198,7 +199,7 @@ namespace { /// because it's a convenient base class. Ideally we would not accept /// those here, but ideally we would have better implementations for /// them. -bool TypeSpecTypeLoc::classof(const TypeLoc *TL) { +bool TypeSpecTypeLoc::isType(const TypeLoc *TL) { if (TL->getType().hasLocalQualifiers()) return false; return TSTChecker().Visit(*TL); } @@ -278,8 +279,8 @@ TypeSpecifierType BuiltinTypeLoc::getWrittenTypeSpec() const { } TypeLoc TypeLoc::IgnoreParensImpl(TypeLoc TL) { - while (ParenTypeLoc* PTL = dyn_cast(&TL)) - TL = PTL->getInnerLoc(); + while (ParenTypeLoc PTL = TL.getAs()) + TL = PTL.getInnerLoc(); return TL; } diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index d765924..f3a290c 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -5772,14 +5772,13 @@ static bool IsTailPaddedMemberArray(Sema &S, llvm::APInt Size, while (TInfo) { TypeLoc TL = TInfo->getTypeLoc(); // Look through typedefs. - const TypedefTypeLoc *TTL = dyn_cast(&TL); - if (TTL) { - const TypedefNameDecl *TDL = TTL->getTypedefNameDecl(); + if (TypedefTypeLoc TTL = TL.getAs()) { + const TypedefNameDecl *TDL = TTL.getTypedefNameDecl(); TInfo = TDL->getTypeSourceInfo(); continue; } - if (const ConstantArrayTypeLoc *CTL = dyn_cast(&TL)) { - const Expr *SizeExpr = dyn_cast(CTL->getSizeExpr()); + if (ConstantArrayTypeLoc CTL = TL.getAs()) { + const Expr *SizeExpr = dyn_cast(CTL.getSizeExpr()); if (!SizeExpr || SizeExpr->getExprLoc().isMacroID()) return false; } diff --git a/clang/lib/Sema/SemaCodeComplete.cpp b/clang/lib/Sema/SemaCodeComplete.cpp index e145d3f..f894a0b 100644 --- a/clang/lib/Sema/SemaCodeComplete.cpp +++ b/clang/lib/Sema/SemaCodeComplete.cpp @@ -2147,36 +2147,35 @@ static std::string FormatFunctionParameter(ASTContext &Context, // The argument for a block pointer parameter is a block literal with // the appropriate type. - FunctionTypeLoc *Block = 0; - FunctionProtoTypeLoc *BlockProto = 0; + FunctionTypeLoc Block; + FunctionProtoTypeLoc BlockProto; TypeLoc TL; if (TypeSourceInfo *TSInfo = Param->getTypeSourceInfo()) { TL = TSInfo->getTypeLoc().getUnqualifiedLoc(); while (true) { // Look through typedefs. if (!SuppressBlock) { - if (TypedefTypeLoc *TypedefTL = dyn_cast(&TL)) { - if (TypeSourceInfo *InnerTSInfo - = TypedefTL->getTypedefNameDecl()->getTypeSourceInfo()) { + if (TypedefTypeLoc TypedefTL = TL.getAs()) { + if (TypeSourceInfo *InnerTSInfo = + TypedefTL.getTypedefNameDecl()->getTypeSourceInfo()) { TL = InnerTSInfo->getTypeLoc().getUnqualifiedLoc(); continue; } } // Look through qualified types - if (QualifiedTypeLoc *QualifiedTL = dyn_cast(&TL)) { - TL = QualifiedTL->getUnqualifiedLoc(); + if (QualifiedTypeLoc QualifiedTL = TL.getAs()) { + TL = QualifiedTL.getUnqualifiedLoc(); continue; } } // Try to get the function prototype behind the block pointer type, // then we're done. - if (BlockPointerTypeLoc *BlockPtr - = dyn_cast(&TL)) { - TL = BlockPtr->getPointeeLoc().IgnoreParens(); - Block = dyn_cast(&TL); - BlockProto = dyn_cast(&TL); + if (BlockPointerTypeLoc BlockPtr = TL.getAs()) { + TL = BlockPtr.getPointeeLoc().IgnoreParens(); + Block = TL.getAs(); + BlockProto = TL.getAs(); } break; } @@ -2204,27 +2203,27 @@ static std::string FormatFunctionParameter(ASTContext &Context, // We have the function prototype behind the block pointer type, as it was // written in the source. std::string Result; - QualType ResultType = Block->getTypePtr()->getResultType(); + QualType ResultType = Block.getTypePtr()->getResultType(); if (!ResultType->isVoidType() || SuppressBlock) ResultType.getAsStringInternal(Result, Policy); // Format the parameter list. std::string Params; - if (!BlockProto || Block->getNumArgs() == 0) { - if (BlockProto && BlockProto->getTypePtr()->isVariadic()) + if (!BlockProto || Block.getNumArgs() == 0) { + if (BlockProto && BlockProto.getTypePtr()->isVariadic()) Params = "(...)"; else Params = "(void)"; } else { Params += "("; - for (unsigned I = 0, N = Block->getNumArgs(); I != N; ++I) { + for (unsigned I = 0, N = Block.getNumArgs(); I != N; ++I) { if (I) Params += ", "; - Params += FormatFunctionParameter(Context, Policy, Block->getArg(I), + Params += FormatFunctionParameter(Context, Policy, Block.getArg(I), /*SuppressName=*/false, /*SuppressBlock=*/true); - if (I == N - 1 && BlockProto->getTypePtr()->isVariadic()) + if (I == N - 1 && BlockProto.getTypePtr()->isVariadic()) Params += ", ..."; } Params += ")"; diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index d425902..7667681 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -4066,29 +4066,29 @@ static QualType TryToFixInvalidVariablyModifiedType(QualType T, static void FixInvalidVariablyModifiedTypeLoc(TypeLoc SrcTL, TypeLoc DstTL) { - if (PointerTypeLoc* SrcPTL = dyn_cast(&SrcTL)) { - PointerTypeLoc* DstPTL = cast(&DstTL); - FixInvalidVariablyModifiedTypeLoc(SrcPTL->getPointeeLoc(), - DstPTL->getPointeeLoc()); - DstPTL->setStarLoc(SrcPTL->getStarLoc()); + if (PointerTypeLoc SrcPTL = SrcTL.getAs()) { + PointerTypeLoc DstPTL = DstTL.castAs(); + FixInvalidVariablyModifiedTypeLoc(SrcPTL.getPointeeLoc(), + DstPTL.getPointeeLoc()); + DstPTL.setStarLoc(SrcPTL.getStarLoc()); return; } - if (ParenTypeLoc* SrcPTL = dyn_cast(&SrcTL)) { - ParenTypeLoc* DstPTL = cast(&DstTL); - FixInvalidVariablyModifiedTypeLoc(SrcPTL->getInnerLoc(), - DstPTL->getInnerLoc()); - DstPTL->setLParenLoc(SrcPTL->getLParenLoc()); - DstPTL->setRParenLoc(SrcPTL->getRParenLoc()); + if (ParenTypeLoc SrcPTL = SrcTL.getAs()) { + ParenTypeLoc DstPTL = DstTL.castAs(); + FixInvalidVariablyModifiedTypeLoc(SrcPTL.getInnerLoc(), + DstPTL.getInnerLoc()); + DstPTL.setLParenLoc(SrcPTL.getLParenLoc()); + DstPTL.setRParenLoc(SrcPTL.getRParenLoc()); return; } - ArrayTypeLoc* SrcATL = cast(&SrcTL); - ArrayTypeLoc* DstATL = cast(&DstTL); - TypeLoc SrcElemTL = SrcATL->getElementLoc(); - TypeLoc DstElemTL = DstATL->getElementLoc(); + ArrayTypeLoc SrcATL = SrcTL.castAs(); + ArrayTypeLoc DstATL = DstTL.castAs(); + TypeLoc SrcElemTL = SrcATL.getElementLoc(); + TypeLoc DstElemTL = DstATL.getElementLoc(); DstElemTL.initializeFullCopy(SrcElemTL); - DstATL->setLBracketLoc(SrcATL->getLBracketLoc()); - DstATL->setSizeExpr(SrcATL->getSizeExpr()); - DstATL->setRBracketLoc(SrcATL->getRBracketLoc()); + DstATL.setLBracketLoc(SrcATL.getLBracketLoc()); + DstATL.setSizeExpr(SrcATL.getSizeExpr()); + DstATL.setRBracketLoc(SrcATL.getRBracketLoc()); } /// Helper method to turn variable array types into constant array @@ -6600,12 +6600,12 @@ static SourceRange getResultSourceRange(const FunctionDecl *FD) { return SourceRange(); TypeLoc TL = TSI->getTypeLoc(); - FunctionTypeLoc *FunctionTL = dyn_cast(&TL); + FunctionTypeLoc FunctionTL = TL.getAs(); if (!FunctionTL) return SourceRange(); - TypeLoc ResultTL = FunctionTL->getResultLoc(); - if (isa(ResultTL.getUnqualifiedLoc())) + TypeLoc ResultTL = FunctionTL.getResultLoc(); + if (ResultTL.getUnqualifiedLoc().getAs()) return ResultTL.getSourceRange(); return SourceRange(); @@ -8291,11 +8291,11 @@ Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Decl *D) { // but that could be a zero-parameter prototype TypeSourceInfo* TI = PossibleZeroParamPrototype->getTypeSourceInfo(); TypeLoc TL = TI->getTypeLoc(); - if (FunctionNoProtoTypeLoc* FTL = dyn_cast(&TL)) + if (FunctionNoProtoTypeLoc FTL = TL.getAs()) Diag(PossibleZeroParamPrototype->getLocation(), diag::note_declaration_not_a_prototype) << PossibleZeroParamPrototype - << FixItHint::CreateInsertion(FTL->getRParenLoc(), "void"); + << FixItHint::CreateInsertion(FTL.getRParenLoc(), "void"); } } diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp index 97d1ea2..2f3634a 100644 --- a/clang/lib/Sema/SemaDeclAttr.cpp +++ b/clang/lib/Sema/SemaDeclAttr.cpp @@ -3715,10 +3715,10 @@ static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { FunctionDecl *FD = cast(D); if (!FD->getResultType()->isVoidType()) { TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); - if (FunctionTypeLoc* FTL = dyn_cast(&TL)) { + if (FunctionTypeLoc FTL = TL.getAs()) { S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) << FD->getType() - << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(), + << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(), "void"); } else { S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index 763a4ce..1711c87 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -3757,7 +3757,7 @@ struct CheckAbstractUsage { switch (TL.getTypeLocClass()) { #define ABSTRACT_TYPELOC(CLASS, PARENT) #define TYPELOC(CLASS, PARENT) \ - case TypeLoc::CLASS: Check(cast(TL), Sel); break; + case TypeLoc::CLASS: Check(TL.castAs(), Sel); break; #include "clang/AST/TypeLocNodes.def" } } @@ -10432,15 +10432,16 @@ Decl *Sema::ActOnTemplatedFriendTag(Scope *S, SourceLocation FriendLoc, TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); if (isa(T)) { - DependentNameTypeLoc TL = cast(TSI->getTypeLoc()); + DependentNameTypeLoc TL = + TSI->getTypeLoc().castAs(); TL.setElaboratedKeywordLoc(TagLoc); TL.setQualifierLoc(QualifierLoc); TL.setNameLoc(NameLoc); } else { - ElaboratedTypeLoc TL = cast(TSI->getTypeLoc()); + ElaboratedTypeLoc TL = TSI->getTypeLoc().castAs(); TL.setElaboratedKeywordLoc(TagLoc); TL.setQualifierLoc(QualifierLoc); - cast(TL.getNamedTypeLoc()).setNameLoc(NameLoc); + TL.getNamedTypeLoc().castAs().setNameLoc(NameLoc); } FriendDecl *Friend = FriendDecl::Create(Context, CurContext, NameLoc, @@ -10460,7 +10461,7 @@ Decl *Sema::ActOnTemplatedFriendTag(Scope *S, SourceLocation FriendLoc, ElaboratedTypeKeyword ETK = TypeWithKeyword::getKeywordForTagTypeKind(Kind); QualType T = Context.getDependentNameType(ETK, SS.getScopeRep(), Name); TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); - DependentNameTypeLoc TL = cast(TSI->getTypeLoc()); + DependentNameTypeLoc TL = TSI->getTypeLoc().castAs(); TL.setElaboratedKeywordLoc(TagLoc); TL.setQualifierLoc(SS.getWithLocInContext(Context)); TL.setNameLoc(NameLoc); @@ -11538,7 +11539,7 @@ bool Sema::checkThisInStaticMemberFunctionType(CXXMethodDecl *Method) { return false; TypeLoc TL = TSInfo->getTypeLoc(); - FunctionProtoTypeLoc *ProtoTL = dyn_cast(&TL); + FunctionProtoTypeLoc ProtoTL = TL.getAs(); if (!ProtoTL) return false; @@ -11549,12 +11550,12 @@ bool Sema::checkThisInStaticMemberFunctionType(CXXMethodDecl *Method) { // within a static member function as they are within a non-static member // function). [ Note: this is because declaration matching does not occur // until the complete declarator is known. - end note ] - const FunctionProtoType *Proto = ProtoTL->getTypePtr(); + const FunctionProtoType *Proto = ProtoTL.getTypePtr(); FindCXXThisExpr Finder(*this); // If the return type came after the cv-qualifier-seq, check it now. if (Proto->hasTrailingReturn() && - !Finder.TraverseTypeLoc(ProtoTL->getResultLoc())) + !Finder.TraverseTypeLoc(ProtoTL.getResultLoc())) return true; // Check the exception specification. @@ -11570,11 +11571,11 @@ bool Sema::checkThisInStaticMemberFunctionExceptionSpec(CXXMethodDecl *Method) { return false; TypeLoc TL = TSInfo->getTypeLoc(); - FunctionProtoTypeLoc *ProtoTL = dyn_cast(&TL); + FunctionProtoTypeLoc ProtoTL = TL.getAs(); if (!ProtoTL) return false; - const FunctionProtoType *Proto = ProtoTL->getTypePtr(); + const FunctionProtoType *Proto = ProtoTL.getTypePtr(); FindCXXThisExpr Finder(*this); switch (Proto->getExceptionSpecType()) { diff --git a/clang/lib/Sema/SemaExceptionSpec.cpp b/clang/lib/Sema/SemaExceptionSpec.cpp index 937b577..29ce7db 100644 --- a/clang/lib/Sema/SemaExceptionSpec.cpp +++ b/clang/lib/Sema/SemaExceptionSpec.cpp @@ -294,8 +294,8 @@ bool Sema::CheckEquivalentExceptionSpec(FunctionDecl *Old, FunctionDecl *New) { SourceLocation FixItLoc; if (TypeSourceInfo *TSInfo = New->getTypeSourceInfo()) { TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens(); - if (const FunctionTypeLoc *FTLoc = dyn_cast(&TL)) - FixItLoc = PP.getLocForEndOfToken(FTLoc->getLocalRangeEnd()); + if (FunctionTypeLoc FTLoc = TL.getAs()) + FixItLoc = PP.getLocForEndOfToken(FTLoc.getLocalRangeEnd()); } if (FixItLoc.isInvalid()) diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index 86bd7b8..4ddbdd2 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -3836,9 +3836,9 @@ bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, static void DiagnoseCalleeStaticArrayParam(Sema &S, ParmVarDecl *PVD) { TypeLoc TL = PVD->getTypeSourceInfo()->getTypeLoc(); - if (ArrayTypeLoc *ATL = dyn_cast(&TL)) + if (ArrayTypeLoc ATL = TL.getAs()) S.Diag(PVD->getLocation(), diag::note_callee_static_array) - << ATL->getLocalSourceRange(); + << ATL.getLocalSourceRange(); } /// CheckStaticArrayArgument - If the given argument corresponds to a static @@ -9437,8 +9437,7 @@ void Sema::ActOnBlockArguments(SourceLocation CaretLoc, Declarator &ParamInfo, FunctionProtoTypeLoc ExplicitSignature; TypeLoc tmp = Sig->getTypeLoc().IgnoreParens(); - if (isa(tmp)) { - ExplicitSignature = cast(tmp); + if ((ExplicitSignature = tmp.getAs())) { // Check whether that explicit signature was synthesized by // GetTypeForDeclarator. If so, don't save that as part of the diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp index 0de5b8c..baa5243 100644 --- a/clang/lib/Sema/SemaInit.cpp +++ b/clang/lib/Sema/SemaInit.cpp @@ -4899,9 +4899,9 @@ InitializationSequence::Perform(Sema &S, if (DeclaratorDecl *DD = Entity.getDecl()) { if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { TypeLoc TL = TInfo->getTypeLoc(); - if (IncompleteArrayTypeLoc *ArrayLoc - = dyn_cast(&TL)) - Brackets = ArrayLoc->getBracketsRange(); + if (IncompleteArrayTypeLoc ArrayLoc = + TL.getAs()) + Brackets = ArrayLoc.getBracketsRange(); } } diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp index dcb86b7..3b6d073 100644 --- a/clang/lib/Sema/SemaStmt.cpp +++ b/clang/lib/Sema/SemaStmt.cpp @@ -237,7 +237,7 @@ void Sema::DiagnoseUnusedExprResult(const Stmt *S) { // We really do want to use the non-canonical type here. if (T == Context.VoidPtrTy) { - PointerTypeLoc TL = cast(TI->getTypeLoc()); + PointerTypeLoc TL = TI->getTypeLoc().castAs(); Diag(Loc, diag::warn_unused_voidptr) << FixItHint::CreateRemoval(TL.getStarLoc()); diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp index 86af8c3..74a6da1 100644 --- a/clang/lib/Sema/SemaTemplate.cpp +++ b/clang/lib/Sema/SemaTemplate.cpp @@ -6956,15 +6956,15 @@ Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); if (isa(T)) { - DependentNameTypeLoc TL = cast(TSI->getTypeLoc()); + DependentNameTypeLoc TL = TSI->getTypeLoc().castAs(); TL.setElaboratedKeywordLoc(TypenameLoc); TL.setQualifierLoc(QualifierLoc); TL.setNameLoc(IdLoc); } else { - ElaboratedTypeLoc TL = cast(TSI->getTypeLoc()); + ElaboratedTypeLoc TL = TSI->getTypeLoc().castAs(); TL.setElaboratedKeywordLoc(TypenameLoc); TL.setQualifierLoc(QualifierLoc); - cast(TL.getNamedTypeLoc()).setNameLoc(IdLoc); + TL.getNamedTypeLoc().castAs().setNameLoc(IdLoc); } return CreateParsedType(T, TSI); @@ -7054,12 +7054,12 @@ static bool isEnableIf(NestedNameSpecifierLoc NNS, const IdentifierInfo &II, if (!NNS || !NNS.getNestedNameSpecifier()->getAsType()) return false; TypeLoc EnableIfTy = NNS.getTypeLoc(); - TemplateSpecializationTypeLoc *EnableIfTSTLoc = - dyn_cast(&EnableIfTy); - if (!EnableIfTSTLoc || EnableIfTSTLoc->getNumArgs() == 0) + TemplateSpecializationTypeLoc EnableIfTSTLoc = + EnableIfTy.getAs(); + if (!EnableIfTSTLoc || EnableIfTSTLoc.getNumArgs() == 0) return false; const TemplateSpecializationType *EnableIfTST = - cast(EnableIfTSTLoc->getTypePtr()); + cast(EnableIfTSTLoc.getTypePtr()); // ... which names a complete class template declaration... const TemplateDecl *EnableIfDecl = @@ -7074,7 +7074,7 @@ static bool isEnableIf(NestedNameSpecifierLoc NNS, const IdentifierInfo &II, return false; // Assume the first template argument is the condition. - CondRange = EnableIfTSTLoc->getArgLoc(0).getSourceRange(); + CondRange = EnableIfTSTLoc.getArgLoc(0).getSourceRange(); return true; } diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp b/clang/lib/Sema/SemaTemplateInstantiate.cpp index 04aa79b..7363bc0 100644 --- a/clang/lib/Sema/SemaTemplateInstantiate.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp @@ -1566,10 +1566,10 @@ static bool NeedsInstantiationAsFunctionType(TypeSourceInfo *T) { return true; TypeLoc TL = T->getTypeLoc().IgnoreParens(); - if (!isa(TL)) + if (!TL.getAs()) return false; - FunctionProtoTypeLoc FP = cast(TL); + FunctionProtoTypeLoc FP = TL.castAs(); for (unsigned I = 0, E = FP.getNumArgs(); I != E; ++I) { ParmVarDecl *P = FP.getArg(I); @@ -1613,9 +1613,9 @@ TypeSourceInfo *Sema::SubstFunctionDeclType(TypeSourceInfo *T, TLB.reserve(TL.getFullDataSize()); QualType Result; - - if (FunctionProtoTypeLoc *Proto = dyn_cast(&TL)) { - Result = Instantiator.TransformFunctionProtoType(TLB, *Proto, ThisContext, + + if (FunctionProtoTypeLoc Proto = TL.getAs()) { + Result = Instantiator.TransformFunctionProtoType(TLB, Proto, ThisContext, ThisTypeQuals); } else { Result = Instantiator.TransformType(TLB, TL); @@ -1635,9 +1635,8 @@ ParmVarDecl *Sema::SubstParmVarDecl(ParmVarDecl *OldParm, TypeSourceInfo *NewDI = 0; TypeLoc OldTL = OldDI->getTypeLoc(); - if (isa(OldTL)) { - PackExpansionTypeLoc ExpansionTL = cast(OldTL); - + if (PackExpansionTypeLoc ExpansionTL = OldTL.getAs()) { + // We have a function parameter pack. Substitute into the pattern of the // expansion. NewDI = SubstType(ExpansionTL.getPatternLoc(), TemplateArgs, diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp index 2d3d0bd..d854f7a 100644 --- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -1706,7 +1706,7 @@ Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl( // The non-type template parameter pack's type is a pack expansion of types. // Determine whether we need to expand this parameter pack into separate // types. - PackExpansionTypeLoc Expansion = cast(TL); + PackExpansionTypeLoc Expansion = TL.castAs(); TypeLoc Pattern = Expansion.getPatternLoc(); SmallVector Unexpanded; SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded); @@ -2335,15 +2335,14 @@ TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D, if (NewTInfo != OldTInfo) { // Get parameters from the new type info. TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens(); - if (FunctionProtoTypeLoc *OldProtoLoc - = dyn_cast(&OldTL)) { + if (FunctionProtoTypeLoc OldProtoLoc = + OldTL.getAs()) { TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens(); - FunctionProtoTypeLoc *NewProtoLoc = cast(&NewTL); - assert(NewProtoLoc && "Missing prototype?"); + FunctionProtoTypeLoc NewProtoLoc = NewTL.castAs(); unsigned NewIdx = 0; - for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc->getNumArgs(); + for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc.getNumArgs(); OldIdx != NumOldParams; ++OldIdx) { - ParmVarDecl *OldParam = OldProtoLoc->getArg(OldIdx); + ParmVarDecl *OldParam = OldProtoLoc.getArg(OldIdx); LocalInstantiationScope *Scope = SemaRef.CurrentInstantiationScope; llvm::Optional NumArgumentsInExpansion; @@ -2354,14 +2353,14 @@ TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D, if (!NumArgumentsInExpansion) { // Simple case: normal parameter, or a parameter pack that's // instantiated to a (still-dependent) parameter pack. - ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++); + ParmVarDecl *NewParam = NewProtoLoc.getArg(NewIdx++); Params.push_back(NewParam); Scope->InstantiatedLocal(OldParam, NewParam); } else { // Parameter pack expansion: make the instantiation an argument pack. Scope->MakeInstantiatedLocalArgPack(OldParam); for (unsigned I = 0; I != *NumArgumentsInExpansion; ++I) { - ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++); + ParmVarDecl *NewParam = NewProtoLoc.getArg(NewIdx++); Params.push_back(NewParam); Scope->InstantiatedLocalPackArg(OldParam, NewParam); } @@ -2373,10 +2372,10 @@ TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D, // substitution occurred. However, we still need to instantiate // the function parameters themselves. TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens(); - if (FunctionProtoTypeLoc *OldProtoLoc - = dyn_cast(&OldTL)) { - for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) { - ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i)); + if (FunctionProtoTypeLoc OldProtoLoc = + OldTL.getAs()) { + for (unsigned i = 0, i_end = OldProtoLoc.getNumArgs(); i != i_end; ++i) { + ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc.getArg(i)); if (!Parm) return 0; Params.push_back(Parm); diff --git a/clang/lib/Sema/SemaTemplateVariadic.cpp b/clang/lib/Sema/SemaTemplateVariadic.cpp index c13a2f6..f250e30 100644 --- a/clang/lib/Sema/SemaTemplateVariadic.cpp +++ b/clang/lib/Sema/SemaTemplateVariadic.cpp @@ -462,7 +462,8 @@ TypeSourceInfo *Sema::CheckPackExpansion(TypeSourceInfo *Pattern, return 0; TypeSourceInfo *TSResult = Context.CreateTypeSourceInfo(Result); - PackExpansionTypeLoc TL = cast(TSResult->getTypeLoc()); + PackExpansionTypeLoc TL = + TSResult->getTypeLoc().castAs(); TL.setEllipsisLoc(EllipsisLoc); // Copy over the source-location information from the type. diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp index 109c8e4..dcbaa51 100644 --- a/clang/lib/Sema/SemaType.cpp +++ b/clang/lib/Sema/SemaType.cpp @@ -3208,13 +3208,13 @@ namespace { TypeLoc OldTL = TInfo->getTypeLoc(); if (TInfo->getType()->getAs()) { - ElaboratedTypeLoc ElabTL = cast(OldTL); - TemplateSpecializationTypeLoc NamedTL = - cast(ElabTL.getNamedTypeLoc()); + ElaboratedTypeLoc ElabTL = OldTL.castAs(); + TemplateSpecializationTypeLoc NamedTL = ElabTL.getNamedTypeLoc() + .castAs(); TL.copy(NamedTL); } else - TL.copy(cast(OldTL)); + TL.copy(OldTL.castAs()); } void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr); @@ -3262,7 +3262,7 @@ namespace { TypeSourceInfo *TInfo = 0; Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); if (TInfo) { - TL.copy(cast(TInfo->getTypeLoc())); + TL.copy(TInfo->getTypeLoc().castAs()); return; } } @@ -3278,7 +3278,7 @@ namespace { TypeSourceInfo *TInfo = 0; Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); assert(TInfo); - TL.copy(cast(TInfo->getTypeLoc())); + TL.copy(TInfo->getTypeLoc().castAs()); } void VisitDependentTemplateSpecializationTypeLoc( DependentTemplateSpecializationTypeLoc TL) { @@ -3286,8 +3286,8 @@ namespace { TypeSourceInfo *TInfo = 0; Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); assert(TInfo); - TL.copy(cast( - TInfo->getTypeLoc())); + TL.copy( + TInfo->getTypeLoc().castAs()); } void VisitTagTypeLoc(TagTypeLoc TL) { TL.setNameLoc(DS.getTypeSpecTypeNameLoc()); @@ -3348,7 +3348,7 @@ namespace { case NestedNameSpecifier::Identifier: assert(isa(ClsTy) && "Unexpected TypeLoc"); { - DependentNameTypeLoc DNTLoc = cast(ClsTL); + DependentNameTypeLoc DNTLoc = ClsTL.castAs(); DNTLoc.setElaboratedKeywordLoc(SourceLocation()); DNTLoc.setQualifierLoc(NNSLoc.getPrefix()); DNTLoc.setNameLoc(NNSLoc.getLocalBeginLoc()); @@ -3358,7 +3358,7 @@ namespace { case NestedNameSpecifier::TypeSpec: case NestedNameSpecifier::TypeSpecWithTemplate: if (isa(ClsTy)) { - ElaboratedTypeLoc ETLoc = *cast(&ClsTL); + ElaboratedTypeLoc ETLoc = ClsTL.castAs(); ETLoc.setElaboratedKeywordLoc(SourceLocation()); ETLoc.setQualifierLoc(NNSLoc.getPrefix()); TypeLoc NamedTL = ETLoc.getNamedTypeLoc(); @@ -3437,13 +3437,12 @@ Sema::GetTypeSourceInfoForDeclarator(Declarator &D, QualType T, // Handle parameter packs whose type is a pack expansion. if (isa(T)) { - cast(CurrTL).setEllipsisLoc(D.getEllipsisLoc()); + CurrTL.castAs().setEllipsisLoc(D.getEllipsisLoc()); CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc(); } for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { - while (isa(CurrTL)) { - AttributedTypeLoc TL = cast(CurrTL); + while (AttributedTypeLoc TL = CurrTL.getAs()) { fillAttributedTypeLoc(TL, D.getTypeObject(i).getAttrs()); CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc(); } diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h index bacf6d5..986e432 100644 --- a/clang/lib/Sema/TreeTransform.h +++ b/clang/lib/Sema/TreeTransform.h @@ -2817,8 +2817,8 @@ TreeTransform::TransformNestedNameSpecifierLoc( } // If the nested-name-specifier is an invalid type def, don't emit an // error because a previous error should have already been emitted. - TypedefTypeLoc* TTL = dyn_cast(&TL); - if (!TTL || !TTL->getTypedefNameDecl()->isInvalidDecl()) { + TypedefTypeLoc TTL = TL.getAs(); + if (!TTL || !TTL.getTypedefNameDecl()->isInvalidDecl()) { SemaRef.Diag(TL.getBeginLoc(), diag::err_nested_name_spec_non_tag) << TL.getType() << SS.getRange(); } @@ -3326,9 +3326,10 @@ QualType TreeTransform::TransformType(TypeLocBuilder &TLB, TypeLoc T) { switch (T.getTypeLocClass()) { #define ABSTRACT_TYPELOC(CLASS, PARENT) -#define TYPELOC(CLASS, PARENT) \ - case TypeLoc::CLASS: \ - return getDerived().Transform##CLASS##Type(TLB, cast(T)); +#define TYPELOC(CLASS, PARENT) \ + case TypeLoc::CLASS: \ + return getDerived().Transform##CLASS##Type(TLB, \ + T.castAs()); #include "clang/AST/TypeLocNodes.def" } @@ -3421,8 +3422,8 @@ TreeTransform::TransformTypeInObjectScope(TypeLoc TL, QualType Result; if (isa(T)) { - TemplateSpecializationTypeLoc SpecTL - = cast(TL); + TemplateSpecializationTypeLoc SpecTL = + TL.castAs(); TemplateName Template = getDerived().TransformTemplateName(SS, @@ -3435,8 +3436,8 @@ TreeTransform::TransformTypeInObjectScope(TypeLoc TL, Result = getDerived().TransformTemplateSpecializationType(TLB, SpecTL, Template); } else if (isa(T)) { - DependentTemplateSpecializationTypeLoc SpecTL - = cast(TL); + DependentTemplateSpecializationTypeLoc SpecTL = + TL.castAs(); TemplateName Template = getDerived().RebuildTemplateName(SS, @@ -3478,8 +3479,8 @@ TreeTransform::TransformTypeInObjectScope(TypeSourceInfo *TSInfo, TypeLoc TL = TSInfo->getTypeLoc(); if (isa(T)) { - TemplateSpecializationTypeLoc SpecTL - = cast(TL); + TemplateSpecializationTypeLoc SpecTL = + TL.castAs(); TemplateName Template = getDerived().TransformTemplateName(SS, @@ -3492,8 +3493,8 @@ TreeTransform::TransformTypeInObjectScope(TypeSourceInfo *TSInfo, Result = getDerived().TransformTemplateSpecializationType(TLB, SpecTL, Template); } else if (isa(T)) { - DependentTemplateSpecializationTypeLoc SpecTL - = cast(TL); + DependentTemplateSpecializationTypeLoc SpecTL = + TL.castAs(); TemplateName Template = getDerived().RebuildTemplateName(SS, @@ -3959,7 +3960,7 @@ TreeTransform::TransformFunctionTypeParam(ParmVarDecl *OldParm, // If we're substituting into a pack expansion type and we know the // length we want to expand to, just substitute for the pattern. TypeLoc OldTL = OldDI->getTypeLoc(); - PackExpansionTypeLoc OldExpansionTL = cast(OldTL); + PackExpansionTypeLoc OldExpansionTL = OldTL.castAs(); TypeLocBuilder TLB; TypeLoc NewTL = OldDI->getTypeLoc(); @@ -4025,7 +4026,7 @@ bool TreeTransform:: // Find the parameter packs that could be expanded. TypeLoc TL = OldParm->getTypeSourceInfo()->getTypeLoc(); - PackExpansionTypeLoc ExpansionTL = cast(TL); + PackExpansionTypeLoc ExpansionTL = TL.castAs(); TypeLoc Pattern = ExpansionTL.getPatternLoc(); SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded); assert(Unexpanded.size() > 0 && "Could not find parameter packs!"); @@ -7608,7 +7609,7 @@ TreeTransform::TransformTypeTraitExpr(TypeTraitExpr *E) { for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) { TypeSourceInfo *From = E->getArg(I); TypeLoc FromTL = From->getTypeLoc(); - if (!isa(FromTL)) { + if (!FromTL.getAs()) { TypeLocBuilder TLB; TLB.reserve(FromTL.getFullDataSize()); QualType To = getDerived().TransformType(TLB, FromTL); @@ -7627,7 +7628,7 @@ TreeTransform::TransformTypeTraitExpr(TypeTraitExpr *E) { ArgChanged = true; // We have a pack expansion. Instantiate it. - PackExpansionTypeLoc ExpansionTL = cast(FromTL); + PackExpansionTypeLoc ExpansionTL = FromTL.castAs(); TypeLoc PatternTL = ExpansionTL.getPatternLoc(); SmallVector Unexpanded; SemaRef.collectUnexpandedParameterPacks(PatternTL, Unexpanded); diff --git a/clang/lib/Sema/TypeLocBuilder.h b/clang/lib/Sema/TypeLocBuilder.h index 3da316d..f36ec9f 100644 --- a/clang/lib/Sema/TypeLocBuilder.h +++ b/clang/lib/Sema/TypeLocBuilder.h @@ -75,7 +75,7 @@ class TypeLocBuilder { /// previously retrieved from this builder. TypeSpecTypeLoc pushTypeSpec(QualType T) { size_t LocalSize = TypeSpecTypeLoc::LocalDataSize; - return cast(pushImpl(T, LocalSize)); + return pushImpl(T, LocalSize).castAs(); } /// Resets this builder to the newly-initialized state. @@ -97,8 +97,8 @@ class TypeLocBuilder { /// Pushes space for a new TypeLoc of the given type. Invalidates /// any TypeLocs previously retrieved from this builder. template TyLocType push(QualType T) { - size_t LocalSize = cast(TypeLoc(T, 0)).getLocalDataSize(); - return cast(pushImpl(T, LocalSize)); + size_t LocalSize = TypeLoc(T, 0).castAs().getLocalDataSize(); + return pushImpl(T, LocalSize).castAs(); } /// Creates a TypeSourceInfo for the given type. diff --git a/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp index 1a19a00..e8e3f1d 100644 --- a/clang/tools/libclang/CIndex.cpp +++ b/clang/tools/libclang/CIndex.cpp @@ -671,10 +671,10 @@ bool CursorVisitor::VisitClassTemplateSpecializationDecl( // Visit the template arguments used in the specialization. if (TypeSourceInfo *SpecType = D->getTypeAsWritten()) { TypeLoc TL = SpecType->getTypeLoc(); - if (TemplateSpecializationTypeLoc *TSTLoc - = dyn_cast(&TL)) { - for (unsigned I = 0, N = TSTLoc->getNumArgs(); I != N; ++I) - if (VisitTemplateArgumentLoc(TSTLoc->getArgLoc(I))) + if (TemplateSpecializationTypeLoc TSTLoc = + TL.getAs()) { + for (unsigned I = 0, N = TSTLoc.getNumArgs(); I != N; ++I) + if (VisitTemplateArgumentLoc(TSTLoc.getArgLoc(I))) return true; } } @@ -750,12 +750,12 @@ bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) { // Visit the function declaration's syntactic components in the order // written. This requires a bit of work. TypeLoc TL = TSInfo->getTypeLoc().IgnoreParens(); - FunctionTypeLoc *FTL = dyn_cast(&TL); + FunctionTypeLoc FTL = TL.getAs(); // If we have a function declared directly (without the use of a typedef), // visit just the return type. Otherwise, just visit the function's type // now. - if ((FTL && !isa(ND) && Visit(FTL->getResultLoc())) || + if ((FTL && !isa(ND) && Visit(FTL.getResultLoc())) || (!FTL && Visit(TL))) return true; @@ -771,7 +771,7 @@ bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) { // FIXME: Visit explicitly-specified template arguments! // Visit the function parameters, if we have a function type. - if (FTL && VisitFunctionTypeLoc(*FTL, true)) + if (FTL && VisitFunctionTypeLoc(FTL, true)) return true; // FIXME: Attributes? @@ -2360,8 +2360,8 @@ bool CursorVisitor::RunVisitorWorkList(VisitorWorkList &WL) { // Visit the whole type. if (Visit(TL)) return true; - } else if (isa(TL)) { - FunctionProtoTypeLoc Proto = cast(TL); + } else if (FunctionProtoTypeLoc Proto = + TL.getAs()) { if (E->hasExplicitParameters()) { // Visit parameters. for (unsigned I = 0, N = Proto.getNumArgs(); I != N; ++I) diff --git a/clang/tools/libclang/CXCursor.cpp b/clang/tools/libclang/CXCursor.cpp index e6cf68a..73d11f9 100644 --- a/clang/tools/libclang/CXCursor.cpp +++ b/clang/tools/libclang/CXCursor.cpp @@ -890,7 +890,7 @@ CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) { if (const ElaboratedType *ElabT = Ty->getAs()) { Ty = ElabT->getNamedType(); - ElaboratedTypeLoc ElabTL = cast(TL); + ElaboratedTypeLoc ElabTL = TL.castAs(); Loc = ElabTL.getNamedTypeLoc().getBeginLoc(); } diff --git a/clang/tools/libclang/IndexingContext.cpp b/clang/tools/libclang/IndexingContext.cpp index 9129e92..3368922 100644 --- a/clang/tools/libclang/IndexingContext.cpp +++ b/clang/tools/libclang/IndexingContext.cpp @@ -165,16 +165,16 @@ SourceLocation IndexingContext::CXXBasesListInfo::getBaseLoc( if (TL.isNull()) return Loc; - if (const QualifiedTypeLoc *QL = dyn_cast(&TL)) - TL = QL->getUnqualifiedLoc(); - - if (const ElaboratedTypeLoc *EL = dyn_cast(&TL)) - return EL->getNamedTypeLoc().getBeginLoc(); - if (const DependentNameTypeLoc *DL = dyn_cast(&TL)) - return DL->getNameLoc(); - if (const DependentTemplateSpecializationTypeLoc * - DTL = dyn_cast(&TL)) - return DTL->getTemplateNameLoc(); + if (QualifiedTypeLoc QL = TL.getAs()) + TL = QL.getUnqualifiedLoc(); + + if (ElaboratedTypeLoc EL = TL.getAs()) + return EL.getNamedTypeLoc().getBeginLoc(); + if (DependentNameTypeLoc DL = TL.getAs()) + return DL.getNameLoc(); + if (DependentTemplateSpecializationTypeLoc DTL = + TL.getAs()) + return DTL.getTemplateNameLoc(); return Loc; } diff --git a/clang/tools/libclang/RecursiveASTVisitor.h b/clang/tools/libclang/RecursiveASTVisitor.h index 4844204..35d59d0 100644 --- a/clang/tools/libclang/RecursiveASTVisitor.h +++ b/clang/tools/libclang/RecursiveASTVisitor.h @@ -535,7 +535,7 @@ bool RecursiveASTVisitor::TraverseTypeLoc(TypeLoc TL) { #define ABSTRACT_TYPELOC(CLASS, BASE) #define TYPELOC(CLASS, BASE) \ case TypeLoc::CLASS: \ - return getDerived().Traverse##CLASS##TypeLoc(*cast(&TL)); + return getDerived().Traverse##CLASS##TypeLoc(TL.castAs()); #include "clang/AST/TypeLocNodes.def" } @@ -2027,8 +2027,7 @@ bool RecursiveASTVisitor::TraverseLambdaExpr(LambdaExpr *S) { if (S->hasExplicitParameters() && S->hasExplicitResultType()) { // Visit the whole type. TRY_TO(TraverseTypeLoc(TL)); - } else if (isa(TL)) { - FunctionProtoTypeLoc Proto = cast(TL); + } else if (FunctionProtoTypeLoc Proto = TL.getAs()) { if (S->hasExplicitParameters()) { // Visit parameters. for (unsigned I = 0, N = Proto.getNumArgs(); I != N; ++I) { -- 2.7.4