diff options
author | ed <ed@FreeBSD.org> | 2009-06-06 08:21:31 +0000 |
---|---|---|
committer | ed <ed@FreeBSD.org> | 2009-06-06 08:21:31 +0000 |
commit | 265c92560db8af7e64dc328cb612076086a62bd1 (patch) | |
tree | 06d57bb7679a2140aef96db7105a0bd5f16a4358 /lib/Sema/SemaTemplate.cpp | |
parent | 9e262ca77e924f9d84a864b031a1b931d03c5e38 (diff) | |
download | FreeBSD-src-265c92560db8af7e64dc328cb612076086a62bd1.zip FreeBSD-src-265c92560db8af7e64dc328cb612076086a62bd1.tar.gz |
Import clang, at r72995.
Diffstat (limited to 'lib/Sema/SemaTemplate.cpp')
-rw-r--r-- | lib/Sema/SemaTemplate.cpp | 192 |
1 files changed, 58 insertions, 134 deletions
diff --git a/lib/Sema/SemaTemplate.cpp b/lib/Sema/SemaTemplate.cpp index 782a0d8..f9176ca 100644 --- a/lib/Sema/SemaTemplate.cpp +++ b/lib/Sema/SemaTemplate.cpp @@ -750,6 +750,10 @@ static void CanonicalizeTemplateArguments(const TemplateArgument *TemplateArgs, Canonical.reserve(NumTemplateArgs); for (unsigned Idx = 0; Idx < NumTemplateArgs; ++Idx) { switch (TemplateArgs[Idx].getKind()) { + case TemplateArgument::Null: + assert(false && "Should never see a NULL template argument here"); + break; + case TemplateArgument::Expression: // FIXME: Build canonical expression (!) Canonical.push_back(TemplateArgs[Idx]); @@ -765,11 +769,13 @@ static void CanonicalizeTemplateArguments(const TemplateArgument *TemplateArgs, Canonical.push_back(TemplateArgument(SourceLocation(), *TemplateArgs[Idx].getAsIntegral(), TemplateArgs[Idx].getIntegralType())); + break; case TemplateArgument::Type: { QualType CanonType = Context.getCanonicalType(TemplateArgs[Idx].getAsType()); Canonical.push_back(TemplateArgument(SourceLocation(), CanonType)); + break; } } } @@ -805,7 +811,7 @@ QualType Sema::CheckTemplateIdType(TemplateName Name, // Check that the template argument list is well-formed for this // template. - llvm::SmallVector<TemplateArgument, 16> ConvertedTemplateArgs; + TemplateArgumentListBuilder ConvertedTemplateArgs(Context); if (CheckTemplateArgumentList(Template, TemplateLoc, LAngleLoc, TemplateArgs, NumTemplateArgs, RAngleLoc, ConvertedTemplateArgs)) @@ -829,15 +835,16 @@ QualType Sema::CheckTemplateIdType(TemplateName Name, // template<typename T, typename U = T> struct A; TemplateName CanonName = Context.getCanonicalTemplateName(Name); CanonType = Context.getTemplateSpecializationType(CanonName, - &ConvertedTemplateArgs[0], - ConvertedTemplateArgs.size()); + ConvertedTemplateArgs.getFlatArgumentList(), + ConvertedTemplateArgs.flatSize()); } else if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(Template)) { // Find the class template specialization declaration that // corresponds to these arguments. llvm::FoldingSetNodeID ID; - ClassTemplateSpecializationDecl::Profile(ID, &ConvertedTemplateArgs[0], - ConvertedTemplateArgs.size()); + ClassTemplateSpecializationDecl::Profile(ID, + ConvertedTemplateArgs.getFlatArgumentList(), + ConvertedTemplateArgs.flatSize()); void *InsertPos = 0; ClassTemplateSpecializationDecl *Decl = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); @@ -846,12 +853,10 @@ QualType Sema::CheckTemplateIdType(TemplateName Name, // specialization. Create the canonical declaration and add it to // the set of specializations. Decl = ClassTemplateSpecializationDecl::Create(Context, - ClassTemplate->getDeclContext(), - TemplateLoc, - ClassTemplate, - &ConvertedTemplateArgs[0], - ConvertedTemplateArgs.size(), - 0); + ClassTemplate->getDeclContext(), + TemplateLoc, + ClassTemplate, + ConvertedTemplateArgs, 0); ClassTemplate->getSpecializations().InsertNode(Decl, InsertPos); Decl->setLexicalDeclContext(CurContext); } @@ -949,7 +954,7 @@ bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, const TemplateArgument *TemplateArgs, unsigned NumTemplateArgs, SourceLocation RAngleLoc, - llvm::SmallVectorImpl<TemplateArgument> &Converted) { + TemplateArgumentListBuilder &Converted) { TemplateParameterList *Params = Template->getTemplateParameters(); unsigned NumParams = Params->size(); unsigned NumArgs = NumTemplateArgs; @@ -998,13 +1003,13 @@ bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, // on the previously-computed template arguments. if (ArgType->isDependentType()) { InstantiatingTemplate Inst(*this, TemplateLoc, - Template, &Converted[0], - Converted.size(), + Template, Converted.getFlatArgumentList(), + Converted.flatSize(), SourceRange(TemplateLoc, RAngleLoc)); - TemplateArgumentList TemplateArgs(Context, &Converted[0], - Converted.size(), - /*CopyArgs=*/false); + TemplateArgumentList TemplateArgs(Context, Converted, + /*CopyArgs=*/false, + /*FlattenArgs=*/false); ArgType = InstantiateType(ArgType, TemplateArgs, TTP->getDefaultArgumentLoc(), TTP->getDeclName()); @@ -1069,13 +1074,13 @@ bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, if (NTTPType->isDependentType()) { // Instantiate the type of the non-type template parameter. InstantiatingTemplate Inst(*this, TemplateLoc, - Template, &Converted[0], - Converted.size(), + Template, Converted.getFlatArgumentList(), + Converted.flatSize(), SourceRange(TemplateLoc, RAngleLoc)); - TemplateArgumentList TemplateArgs(Context, &Converted[0], - Converted.size(), - /*CopyArgs=*/false); + TemplateArgumentList TemplateArgs(Context, Converted, + /*CopyArgs=*/false, + /*FlattenArgs=*/false); NTTPType = InstantiateType(NTTPType, TemplateArgs, NTTP->getLocation(), NTTP->getDeclName()); @@ -1092,6 +1097,10 @@ bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, } switch (Arg.getKind()) { + case TemplateArgument::Null: + assert(false && "Should never see a NULL template argument here"); + break; + case TemplateArgument::Expression: { Expr *E = Arg.getAsExpr(); if (CheckTemplateArgument(NTTP, NTTPType, E, &Converted)) @@ -1131,6 +1140,10 @@ bool Sema::CheckTemplateArgumentList(TemplateDecl *Template, = cast<TemplateTemplateParmDecl>(*Param); switch (Arg.getKind()) { + case TemplateArgument::Null: + assert(false && "Should never see a NULL template argument here"); + break; + case TemplateArgument::Expression: { Expr *ArgExpr = Arg.getAsExpr(); if (ArgExpr && isa<DeclRefExpr>(ArgExpr) && @@ -1379,7 +1392,7 @@ Sema::CheckTemplateArgumentPointerToMember(Expr *Arg, NamedDecl *&Member) { /// of this argument will be added to the end of the Converted vector. bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, QualType InstantiatedParamType, Expr *&Arg, - llvm::SmallVectorImpl<TemplateArgument> *Converted) { + TemplateArgumentListBuilder *Converted) { SourceLocation StartLoc = Arg->getSourceRange().getBegin(); // If either the parameter has a dependent type or the argument is @@ -2051,7 +2064,7 @@ Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, TagKind TK, // Check that the template argument list is well-formed for this // template. - llvm::SmallVector<TemplateArgument, 16> ConvertedTemplateArgs; + TemplateArgumentListBuilder ConvertedTemplateArgs(Context); if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, LAngleLoc, &TemplateArgs[0], TemplateArgs.size(), RAngleLoc, ConvertedTemplateArgs)) @@ -2066,11 +2079,13 @@ Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, TagKind TK, llvm::FoldingSetNodeID ID; if (isPartialSpecialization) // FIXME: Template parameter list matters, too - ClassTemplatePartialSpecializationDecl::Profile(ID, &ConvertedTemplateArgs[0], - ConvertedTemplateArgs.size()); + ClassTemplatePartialSpecializationDecl::Profile(ID, + ConvertedTemplateArgs.getFlatArgumentList(), + ConvertedTemplateArgs.flatSize()); else - ClassTemplateSpecializationDecl::Profile(ID, &ConvertedTemplateArgs[0], - ConvertedTemplateArgs.size()); + ClassTemplateSpecializationDecl::Profile(ID, + ConvertedTemplateArgs.getFlatArgumentList(), + ConvertedTemplateArgs.flatSize()); void *InsertPos = 0; ClassTemplateSpecializationDecl *PrevDecl = 0; @@ -2111,12 +2126,11 @@ Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, TagKind TK, ClassTemplatePartialSpecializationDecl *Partial = ClassTemplatePartialSpecializationDecl::Create(Context, ClassTemplate->getDeclContext(), - TemplateNameLoc, - TemplateParams, - ClassTemplate, - &ConvertedTemplateArgs[0], - ConvertedTemplateArgs.size(), - PrevPartial); + TemplateNameLoc, + TemplateParams, + ClassTemplate, + ConvertedTemplateArgs, + PrevPartial); if (PrevPartial) { ClassTemplate->getPartialSpecializations().RemoveNode(PrevPartial); @@ -2132,9 +2146,8 @@ Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, TagKind TK, = ClassTemplateSpecializationDecl::Create(Context, ClassTemplate->getDeclContext(), TemplateNameLoc, - ClassTemplate, - &ConvertedTemplateArgs[0], - ConvertedTemplateArgs.size(), + ClassTemplate, + ConvertedTemplateArgs, PrevDecl); if (PrevDecl) { @@ -2255,9 +2268,9 @@ Sema::ActOnExplicitInstantiation(Scope *S, SourceLocation TemplateLoc, // Check that the template argument list is well-formed for this // template. - llvm::SmallVector<TemplateArgument, 16> ConvertedTemplateArgs; + TemplateArgumentListBuilder ConvertedTemplateArgs(Context); if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, LAngleLoc, - &TemplateArgs[0], TemplateArgs.size(), + TemplateArgs.data(), TemplateArgs.size(), RAngleLoc, ConvertedTemplateArgs)) return true; @@ -2268,8 +2281,9 @@ Sema::ActOnExplicitInstantiation(Scope *S, SourceLocation TemplateLoc, // Find the class template specialization declaration that // corresponds to these arguments. llvm::FoldingSetNodeID ID; - ClassTemplateSpecializationDecl::Profile(ID, &ConvertedTemplateArgs[0], - ConvertedTemplateArgs.size()); + ClassTemplateSpecializationDecl::Profile(ID, + ConvertedTemplateArgs.getFlatArgumentList(), + ConvertedTemplateArgs.flatSize()); void *InsertPos = 0; ClassTemplateSpecializationDecl *PrevDecl = ClassTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos); @@ -2312,9 +2326,7 @@ Sema::ActOnExplicitInstantiation(Scope *S, SourceLocation TemplateLoc, ClassTemplate->getDeclContext(), TemplateNameLoc, ClassTemplate, - &ConvertedTemplateArgs[0], - ConvertedTemplateArgs.size(), - 0); + ConvertedTemplateArgs, 0); Specialization->setLexicalDeclContext(CurContext); CurContext->addDecl(Context, Specialization); return DeclPtrTy::make(Specialization); @@ -2340,9 +2352,7 @@ Sema::ActOnExplicitInstantiation(Scope *S, SourceLocation TemplateLoc, ClassTemplate->getDeclContext(), TemplateNameLoc, ClassTemplate, - &ConvertedTemplateArgs[0], - ConvertedTemplateArgs.size(), - 0); + ConvertedTemplateArgs, 0); ClassTemplate->getSpecializations().InsertNode(Specialization, InsertPos); @@ -2357,7 +2367,7 @@ Sema::ActOnExplicitInstantiation(Scope *S, SourceLocation TemplateLoc, // arguments in the specialization. QualType WrittenTy = Context.getTemplateSpecializationType(Name, - &TemplateArgs[0], + TemplateArgs.data(), TemplateArgs.size(), Context.getTypeDeclType(Specialization)); Specialization->setTypeAsWritten(WrittenTy); @@ -2563,89 +2573,3 @@ Sema::CheckTypenameType(NestedNameSpecifier *NNS, const IdentifierInfo &II, << Name; return QualType(); } - -// FIXME: Move to SemaTemplateDeduction.cpp -bool -Sema::DeduceTemplateArguments(QualType Param, QualType Arg, - llvm::SmallVectorImpl<TemplateArgument> &Deduced) { - // We only want to look at the canonical types, since typedefs and - // sugar are not part of template argument deduction. - Param = Context.getCanonicalType(Param); - Arg = Context.getCanonicalType(Arg); - - // If the parameter type is not dependent, just compare the types - // directly. - if (!Param->isDependentType()) - return Param == Arg; - - // FIXME: Use a visitor or switch to handle all of the kinds of - // types that the parameter may be. - if (const TemplateTypeParmType *TemplateTypeParm - = Param->getAsTemplateTypeParmType()) { - (void)TemplateTypeParm; // FIXME: use this - // The argument type can not be less qualified than the parameter - // type. - if (Param.isMoreQualifiedThan(Arg)) - return false; - - unsigned Quals = Arg.getCVRQualifiers() & ~Param.getCVRQualifiers(); - QualType DeducedType = Arg.getQualifiedType(Quals); - // FIXME: actually save the deduced type, and check that this - // deduction is consistent. - return true; - } - - if (Param.getCVRQualifiers() != Arg.getCVRQualifiers()) - return false; - - if (const PointerType *PointerParam = Param->getAsPointerType()) { - const PointerType *PointerArg = Arg->getAsPointerType(); - if (!PointerArg) - return false; - - return DeduceTemplateArguments(PointerParam->getPointeeType(), - PointerArg->getPointeeType(), - Deduced); - } - - // FIXME: Many more cases to go (to go). - return false; -} - -bool -Sema::DeduceTemplateArguments(const TemplateArgument &Param, - const TemplateArgument &Arg, - llvm::SmallVectorImpl<TemplateArgument> &Deduced) { - assert(Param.getKind() == Arg.getKind() && - "Template argument kind mismatch during deduction"); - switch (Param.getKind()) { - case TemplateArgument::Type: - return DeduceTemplateArguments(Param.getAsType(), Arg.getAsType(), - Deduced); - - default: - return false; - } -} - -bool -Sema::DeduceTemplateArguments(const TemplateArgumentList &ParamList, - const TemplateArgumentList &ArgList, - llvm::SmallVectorImpl<TemplateArgument> &Deduced) { - assert(ParamList.size() == ArgList.size()); - for (unsigned I = 0, N = ParamList.size(); I != N; ++I) { - if (!DeduceTemplateArguments(ParamList[I], ArgList[I], Deduced)) - return false; - } - return true; -} - - -bool -Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial, - const TemplateArgumentList &TemplateArgs) { - llvm::SmallVector<TemplateArgument, 4> Deduced; - Deduced.resize(Partial->getTemplateParameters()->size()); - return DeduceTemplateArguments(Partial->getTemplateArgs(), TemplateArgs, - Deduced); -} |