Deleted Added
full compact
SemaTemplate.cpp (280031) SemaTemplate.cpp (283526)
1//===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//

--- 3341 unchanged lines hidden (view full) ---

3350}
3351
3352/// \brief Check that the given template argument corresponds to the given
3353/// template parameter.
3354///
3355/// \param Param The template parameter against which the argument will be
3356/// checked.
3357///
1//===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//

--- 3341 unchanged lines hidden (view full) ---

3350}
3351
3352/// \brief Check that the given template argument corresponds to the given
3353/// template parameter.
3354///
3355/// \param Param The template parameter against which the argument will be
3356/// checked.
3357///
3358/// \param Arg The template argument.
3358/// \param Arg The template argument, which may be updated due to conversions.
3359///
3360/// \param Template The template in which the template argument resides.
3361///
3362/// \param TemplateLoc The location of the template name for the template
3363/// whose argument list we're matching.
3364///
3365/// \param RAngleLoc The location of the right angle bracket ('>') that closes
3366/// the template argument list.

--- 61 unchanged lines hidden (view full) ---

3428 case TemplateArgument::Expression: {
3429 TemplateArgument Result;
3430 ExprResult Res =
3431 CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(),
3432 Result, CTAK);
3433 if (Res.isInvalid())
3434 return true;
3435
3359///
3360/// \param Template The template in which the template argument resides.
3361///
3362/// \param TemplateLoc The location of the template name for the template
3363/// whose argument list we're matching.
3364///
3365/// \param RAngleLoc The location of the right angle bracket ('>') that closes
3366/// the template argument list.

--- 61 unchanged lines hidden (view full) ---

3428 case TemplateArgument::Expression: {
3429 TemplateArgument Result;
3430 ExprResult Res =
3431 CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(),
3432 Result, CTAK);
3433 if (Res.isInvalid())
3434 return true;
3435
3436 // If the resulting expression is new, then use it in place of the
3437 // old expression in the template argument.
3438 if (Res.get() != Arg.getArgument().getAsExpr()) {
3439 TemplateArgument TA(Res.get());
3440 Arg = TemplateArgumentLoc(TA, Res.get());
3441 }
3442
3436 Converted.push_back(Result);
3437 break;
3438 }
3439
3440 case TemplateArgument::Declaration:
3441 case TemplateArgument::Integral:
3442 case TemplateArgument::NullPtr:
3443 // We've already checked this template argument, so just copy

--- 191 unchanged lines hidden (view full) ---

3635
3636/// \brief Check that the given template argument list is well-formed
3637/// for specializing the given template.
3638bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
3639 SourceLocation TemplateLoc,
3640 TemplateArgumentListInfo &TemplateArgs,
3641 bool PartialTemplateArgs,
3642 SmallVectorImpl<TemplateArgument> &Converted) {
3443 Converted.push_back(Result);
3444 break;
3445 }
3446
3447 case TemplateArgument::Declaration:
3448 case TemplateArgument::Integral:
3449 case TemplateArgument::NullPtr:
3450 // We've already checked this template argument, so just copy

--- 191 unchanged lines hidden (view full) ---

3642
3643/// \brief Check that the given template argument list is well-formed
3644/// for specializing the given template.
3645bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
3646 SourceLocation TemplateLoc,
3647 TemplateArgumentListInfo &TemplateArgs,
3648 bool PartialTemplateArgs,
3649 SmallVectorImpl<TemplateArgument> &Converted) {
3650 // Make a copy of the template arguments for processing. Only make the
3651 // changes at the end when successful in matching the arguments to the
3652 // template.
3653 TemplateArgumentListInfo NewArgs = TemplateArgs;
3654
3643 TemplateParameterList *Params = Template->getTemplateParameters();
3644
3655 TemplateParameterList *Params = Template->getTemplateParameters();
3656
3645 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
3657 SourceLocation RAngleLoc = NewArgs.getRAngleLoc();
3646
3647 // C++ [temp.arg]p1:
3648 // [...] The type and form of each template-argument specified in
3649 // a template-id shall match the type and form specified for the
3650 // corresponding parameter declared by the template in its
3651 // template-parameter-list.
3652 bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template);
3653 SmallVector<TemplateArgument, 2> ArgumentPack;
3658
3659 // C++ [temp.arg]p1:
3660 // [...] The type and form of each template-argument specified in
3661 // a template-id shall match the type and form specified for the
3662 // corresponding parameter declared by the template in its
3663 // template-parameter-list.
3664 bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template);
3665 SmallVector<TemplateArgument, 2> ArgumentPack;
3654 unsigned ArgIdx = 0, NumArgs = TemplateArgs.size();
3666 unsigned ArgIdx = 0, NumArgs = NewArgs.size();
3655 LocalInstantiationScope InstScope(*this, true);
3656 for (TemplateParameterList::iterator Param = Params->begin(),
3657 ParamEnd = Params->end();
3658 Param != ParamEnd; /* increment in loop */) {
3659 // If we have an expanded parameter pack, make sure we don't have too
3660 // many arguments.
3661 if (Optional<unsigned> Expansions = getExpandedPackSize(*Param)) {
3662 if (*Expansions == ArgumentPack.size()) {

--- 19 unchanged lines hidden (view full) ---

3682 Diag(Template->getLocation(), diag::note_template_decl_here)
3683 << Params->getSourceRange();
3684 return true;
3685 }
3686 }
3687
3688 if (ArgIdx < NumArgs) {
3689 // Check the template argument we were given.
3667 LocalInstantiationScope InstScope(*this, true);
3668 for (TemplateParameterList::iterator Param = Params->begin(),
3669 ParamEnd = Params->end();
3670 Param != ParamEnd; /* increment in loop */) {
3671 // If we have an expanded parameter pack, make sure we don't have too
3672 // many arguments.
3673 if (Optional<unsigned> Expansions = getExpandedPackSize(*Param)) {
3674 if (*Expansions == ArgumentPack.size()) {

--- 19 unchanged lines hidden (view full) ---

3694 Diag(Template->getLocation(), diag::note_template_decl_here)
3695 << Params->getSourceRange();
3696 return true;
3697 }
3698 }
3699
3700 if (ArgIdx < NumArgs) {
3701 // Check the template argument we were given.
3690 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
3702 if (CheckTemplateArgument(*Param, NewArgs[ArgIdx], Template,
3691 TemplateLoc, RAngleLoc,
3692 ArgumentPack.size(), Converted))
3693 return true;
3694
3695 bool PackExpansionIntoNonPack =
3703 TemplateLoc, RAngleLoc,
3704 ArgumentPack.size(), Converted))
3705 return true;
3706
3707 bool PackExpansionIntoNonPack =
3696 TemplateArgs[ArgIdx].getArgument().isPackExpansion() &&
3708 NewArgs[ArgIdx].getArgument().isPackExpansion() &&
3697 (!(*Param)->isTemplateParameterPack() || getExpandedPackSize(*Param));
3698 if (PackExpansionIntoNonPack && isa<TypeAliasTemplateDecl>(Template)) {
3699 // Core issue 1430: we have a pack expansion as an argument to an
3700 // alias template, and it's not part of a parameter pack. This
3701 // can't be canonicalized, so reject it now.
3709 (!(*Param)->isTemplateParameterPack() || getExpandedPackSize(*Param));
3710 if (PackExpansionIntoNonPack && isa<TypeAliasTemplateDecl>(Template)) {
3711 // Core issue 1430: we have a pack expansion as an argument to an
3712 // alias template, and it's not part of a parameter pack. This
3713 // can't be canonicalized, so reject it now.
3702 Diag(TemplateArgs[ArgIdx].getLocation(),
3714 Diag(NewArgs[ArgIdx].getLocation(),
3703 diag::err_alias_template_expansion_into_fixed_list)
3715 diag::err_alias_template_expansion_into_fixed_list)
3704 << TemplateArgs[ArgIdx].getSourceRange();
3716 << NewArgs[ArgIdx].getSourceRange();
3705 Diag((*Param)->getLocation(), diag::note_template_param_here);
3706 return true;
3707 }
3708
3709 // We're now done with this argument.
3710 ++ArgIdx;
3711
3712 if ((*Param)->isTemplateParameterPack()) {

--- 15 unchanged lines hidden (view full) ---

3728 // If we were part way through filling in an expanded parameter pack,
3729 // fall back to just producing individual arguments.
3730 Converted.insert(Converted.end(),
3731 ArgumentPack.begin(), ArgumentPack.end());
3732 ArgumentPack.clear();
3733 }
3734
3735 while (ArgIdx < NumArgs) {
3717 Diag((*Param)->getLocation(), diag::note_template_param_here);
3718 return true;
3719 }
3720
3721 // We're now done with this argument.
3722 ++ArgIdx;
3723
3724 if ((*Param)->isTemplateParameterPack()) {

--- 15 unchanged lines hidden (view full) ---

3740 // If we were part way through filling in an expanded parameter pack,
3741 // fall back to just producing individual arguments.
3742 Converted.insert(Converted.end(),
3743 ArgumentPack.begin(), ArgumentPack.end());
3744 ArgumentPack.clear();
3745 }
3746
3747 while (ArgIdx < NumArgs) {
3736 Converted.push_back(TemplateArgs[ArgIdx].getArgument());
3748 Converted.push_back(NewArgs[ArgIdx].getArgument());
3737 ++ArgIdx;
3738 }
3739
3740 return false;
3741 }
3742
3743 continue;
3744 }

--- 34 unchanged lines hidden (view full) ---

3779
3780 // Retrieve the default template argument from the template
3781 // parameter. For each kind of template parameter, we substitute the
3782 // template arguments provided thus far and any "outer" template arguments
3783 // (when the template parameter was part of a nested template) into
3784 // the default argument.
3785 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
3786 if (!TTP->hasDefaultArgument())
3749 ++ArgIdx;
3750 }
3751
3752 return false;
3753 }
3754
3755 continue;
3756 }

--- 34 unchanged lines hidden (view full) ---

3791
3792 // Retrieve the default template argument from the template
3793 // parameter. For each kind of template parameter, we substitute the
3794 // template arguments provided thus far and any "outer" template arguments
3795 // (when the template parameter was part of a nested template) into
3796 // the default argument.
3797 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
3798 if (!TTP->hasDefaultArgument())
3787 return diagnoseArityMismatch(*this, Template, TemplateLoc,
3788 TemplateArgs);
3799 return diagnoseArityMismatch(*this, Template, TemplateLoc, NewArgs);
3789
3790 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
3791 Template,
3792 TemplateLoc,
3793 RAngleLoc,
3794 TTP,
3795 Converted);
3796 if (!ArgType)
3797 return true;
3798
3799 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
3800 ArgType);
3801 } else if (NonTypeTemplateParmDecl *NTTP
3802 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
3803 if (!NTTP->hasDefaultArgument())
3800
3801 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
3802 Template,
3803 TemplateLoc,
3804 RAngleLoc,
3805 TTP,
3806 Converted);
3807 if (!ArgType)
3808 return true;
3809
3810 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
3811 ArgType);
3812 } else if (NonTypeTemplateParmDecl *NTTP
3813 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
3814 if (!NTTP->hasDefaultArgument())
3804 return diagnoseArityMismatch(*this, Template, TemplateLoc,
3805 TemplateArgs);
3815 return diagnoseArityMismatch(*this, Template, TemplateLoc, NewArgs);
3806
3807 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
3808 TemplateLoc,
3809 RAngleLoc,
3810 NTTP,
3811 Converted);
3812 if (E.isInvalid())
3813 return true;
3814
3815 Expr *Ex = E.getAs<Expr>();
3816 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
3817 } else {
3818 TemplateTemplateParmDecl *TempParm
3819 = cast<TemplateTemplateParmDecl>(*Param);
3820
3821 if (!TempParm->hasDefaultArgument())
3816
3817 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
3818 TemplateLoc,
3819 RAngleLoc,
3820 NTTP,
3821 Converted);
3822 if (E.isInvalid())
3823 return true;
3824
3825 Expr *Ex = E.getAs<Expr>();
3826 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
3827 } else {
3828 TemplateTemplateParmDecl *TempParm
3829 = cast<TemplateTemplateParmDecl>(*Param);
3830
3831 if (!TempParm->hasDefaultArgument())
3822 return diagnoseArityMismatch(*this, Template, TemplateLoc,
3823 TemplateArgs);
3832 return diagnoseArityMismatch(*this, Template, TemplateLoc, NewArgs);
3824
3825 NestedNameSpecifierLoc QualifierLoc;
3826 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
3827 TemplateLoc,
3828 RAngleLoc,
3829 TempParm,
3830 Converted,
3831 QualifierLoc);

--- 11 unchanged lines hidden (view full) ---

3843 if (Inst.isInvalid())
3844 return true;
3845
3846 // Check the default template argument.
3847 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
3848 RAngleLoc, 0, Converted))
3849 return true;
3850
3833
3834 NestedNameSpecifierLoc QualifierLoc;
3835 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
3836 TemplateLoc,
3837 RAngleLoc,
3838 TempParm,
3839 Converted,
3840 QualifierLoc);

--- 11 unchanged lines hidden (view full) ---

3852 if (Inst.isInvalid())
3853 return true;
3854
3855 // Check the default template argument.
3856 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
3857 RAngleLoc, 0, Converted))
3858 return true;
3859
3851 // Core issue 150 (assumed resolution): if this is a template template
3852 // parameter, keep track of the default template arguments from the
3860 // Core issue 150 (assumed resolution): if this is a template template
3861 // parameter, keep track of the default template arguments from the
3853 // template definition.
3854 if (isTemplateTemplateParameter)
3862 // template definition.
3863 if (isTemplateTemplateParameter)
3855 TemplateArgs.addArgument(Arg);
3856
3864 NewArgs.addArgument(Arg);
3865
3857 // Move to the next template parameter and argument.
3858 ++Param;
3859 ++ArgIdx;
3860 }
3861
3862 // If we're performing a partial argument substitution, allow any trailing
3863 // pack expansions; they might be empty. This can happen even if
3864 // PartialTemplateArgs is false (the list of arguments is complete but
3865 // still dependent).
3866 if (ArgIdx < NumArgs && CurrentInstantiationScope &&
3867 CurrentInstantiationScope->getPartiallySubstitutedPack()) {
3866 // Move to the next template parameter and argument.
3867 ++Param;
3868 ++ArgIdx;
3869 }
3870
3871 // If we're performing a partial argument substitution, allow any trailing
3872 // pack expansions; they might be empty. This can happen even if
3873 // PartialTemplateArgs is false (the list of arguments is complete but
3874 // still dependent).
3875 if (ArgIdx < NumArgs && CurrentInstantiationScope &&
3876 CurrentInstantiationScope->getPartiallySubstitutedPack()) {
3868 while (ArgIdx < NumArgs &&
3869 TemplateArgs[ArgIdx].getArgument().isPackExpansion())
3870 Converted.push_back(TemplateArgs[ArgIdx++].getArgument());
3877 while (ArgIdx < NumArgs && NewArgs[ArgIdx].getArgument().isPackExpansion())
3878 Converted.push_back(NewArgs[ArgIdx++].getArgument());
3871 }
3872
3873 // If we have any leftover arguments, then there were too many arguments.
3874 // Complain and fail.
3875 if (ArgIdx < NumArgs)
3879 }
3880
3881 // If we have any leftover arguments, then there were too many arguments.
3882 // Complain and fail.
3883 if (ArgIdx < NumArgs)
3876 return diagnoseArityMismatch(*this, Template, TemplateLoc, TemplateArgs);
3884 return diagnoseArityMismatch(*this, Template, TemplateLoc, NewArgs);
3877
3885
3886 // No problems found with the new argument list, propagate changes back
3887 // to caller.
3888 TemplateArgs = NewArgs;
3889
3878 return false;
3879}
3880
3881namespace {
3882 class UnnamedLocalNoLinkageFinder
3883 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
3884 {
3885 Sema &S;

--- 4415 unchanged lines hidden ---
3890 return false;
3891}
3892
3893namespace {
3894 class UnnamedLocalNoLinkageFinder
3895 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
3896 {
3897 Sema &S;

--- 4415 unchanged lines hidden ---