zcov: / lib/Sema/SemaTemplateInstantiate.cpp


Files: 1 Branches Taken: 73.8% 284 / 385
Generated: 2010-02-10 01:31 Branches Executed: 93.2% 359 / 385
Line Coverage: 94.0% 529 / 563


Programs: 2 Runs 3018


       1                 : //===------- SemaTemplateInstantiate.cpp - C++ Template Instantiation ------===/
       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                 : //
       9                 : //  This file implements C++ template instantiation.
      10                 : //
      11                 : //===----------------------------------------------------------------------===/
      12                 : 
      13                 : #include "Sema.h"
      14                 : #include "TreeTransform.h"
      15                 : #include "Lookup.h"
      16                 : #include "clang/AST/ASTConsumer.h"
      17                 : #include "clang/AST/ASTContext.h"
      18                 : #include "clang/AST/Expr.h"
      19                 : #include "clang/AST/DeclTemplate.h"
      20                 : #include "clang/Parse/DeclSpec.h"
      21                 : #include "clang/Basic/LangOptions.h"
      22                 : 
      23                 : using namespace clang;
      24                 : 
      25                 : //===----------------------------------------------------------------------===/
      26                 : // Template Instantiation Support
      27                 : //===----------------------------------------------------------------------===/
      28                 : 
      29                 : /// \brief Retrieve the template argument list(s) that should be used to
      30                 : /// instantiate the definition of the given declaration.
      31                 : ///
      32                 : /// \param D the declaration for which we are computing template instantiation
      33                 : /// arguments.
      34                 : ///
      35                 : /// \param Innermost if non-NULL, the innermost template argument list.
      36                 : ///
      37                 : /// \param RelativeToPrimary true if we should get the template
      38                 : /// arguments relative to the primary template, even when we're
      39                 : /// dealing with a specialization. This is only relevant for function
      40                 : /// template specializations.
      41                 : MultiLevelTemplateArgumentList
      42                 : Sema::getTemplateInstantiationArgs(NamedDecl *D, 
      43                 :                                    const TemplateArgumentList *Innermost,
      44             2354:                                    bool RelativeToPrimary) {
      45                 :   // Accumulate the set of template argument lists in this structure.
      46             2354:   MultiLevelTemplateArgumentList Result;
      47                 : 
                       65: branch 0 taken
                     2289: branch 1 taken
      48             2354:   if (Innermost)
      49               65:     Result.addOuterTemplateArguments(Innermost);
      50                 :   
      51             2354:   DeclContext *Ctx = dyn_cast<DeclContext>(D);
                       97: branch 0 taken
                     2257: branch 1 taken
      52             2354:   if (!Ctx)
      53               97:     Ctx = D->getDeclContext();
      54                 : 
                     2895: branch 1 taken
                     2349: branch 2 taken
      55             7598:   while (!Ctx->isFileContext()) {
      56                 :     // Add template arguments from a class template instantiation.
                     2169: branch 0 taken
                      726: branch 1 taken
      57             2895:     if (ClassTemplateSpecializationDecl *Spec
      58             2895:           = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) {
      59                 :       // We're done when we hit an explicit specialization.
                        0: branch 1 not taken
                     2169: branch 2 taken
      60             2169:       if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization)
      61                0:         break;
      62                 : 
      63             2169:       Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs());
      64                 :       
      65                 :       // If this class template specialization was instantiated from a 
      66                 :       // specialized member that is a class template, we're done.
                     2169: branch 1 taken
                        0: branch 2 not taken
      67             2169:       assert(Spec->getSpecializedTemplate() && "No class template?");
                        2: branch 2 taken
                     2167: branch 3 taken
      68             2169:       if (Spec->getSpecializedTemplate()->isMemberSpecialization())
      69                2:         break;
      70                 :     }
      71                 :     // Add template arguments from a function template specialization.
                      593: branch 1 taken
                      133: branch 2 taken
      72              726:     else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) {
                      557: branch 0 taken
                       36: branch 1 taken
                        0: branch 3 not taken
                      557: branch 4 taken
                        0: branch 5 not taken
                      593: branch 6 taken
      73              593:       if (!RelativeToPrimary &&
      74                 :           Function->getTemplateSpecializationKind() 
      75                 :                                                   == TSK_ExplicitSpecialization)
      76                0:         break;
      77                 :           
                      214: branch 0 taken
                      379: branch 1 taken
      78              593:       if (const TemplateArgumentList *TemplateArgs
      79              593:             = Function->getTemplateSpecializationArgs()) {
      80                 :         // Add the template arguments for this specialization.
      81              214:         Result.addOuterTemplateArguments(TemplateArgs);
      82                 : 
      83                 :         // If this function was instantiated from a specialized member that is
      84                 :         // a function template, we're done.
                      214: branch 1 taken
                        0: branch 2 not taken
      85              214:         assert(Function->getPrimaryTemplate() && "No function template?");
                        3: branch 2 taken
                      211: branch 3 taken
      86              214:         if (Function->getPrimaryTemplate()->isMemberSpecialization())
      87                3:           break;
      88                 :       }
      89                 :       
      90                 :       // If this is a friend declaration and it declares an entity at
      91                 :       // namespace scope, take arguments from its lexical parent
      92                 :       // instead of its semantic parent.
                        2: branch 1 taken
                      588: branch 2 taken
                        2: branch 5 taken
                        0: branch 6 not taken
                        2: branch 7 taken
                      588: branch 8 taken
      93              590:       if (Function->getFriendObjectKind() &&
      94                 :           Function->getDeclContext()->isFileContext()) {
      95                2:         Ctx = Function->getLexicalDeclContext();
      96                2:         RelativeToPrimary = false;
      97                2:         continue;
      98                 :       }
      99                 :     }
     100                 : 
     101             2888:     Ctx = Ctx->getParent();
     102             2888:     RelativeToPrimary = false;
     103                 :   }
     104                 : 
     105                 :   return Result;
     106                 : }
     107                 : 
     108             5469: bool Sema::ActiveTemplateInstantiation::isInstantiationRecord() const {
                     2596: branch 0 taken
                     2873: branch 1 taken
                        0: branch 2 not taken
     109             5469:   switch (Kind) {
     110                 :   case TemplateInstantiation:
     111                 :   case DefaultTemplateArgumentInstantiation:
     112                 :   case DefaultFunctionArgumentInstantiation:
     113             2596:     return true;
     114                 :       
     115                 :   case ExplicitTemplateArgumentSubstitution:
     116                 :   case DeducedTemplateArgumentSubstitution:
     117                 :   case PriorTemplateArgumentSubstitution:
     118                 :   case DefaultTemplateArgumentChecking:
     119             2873:     return false;
     120                 :   }
     121                 :   
     122                0:   return true;
     123                 : }
     124                 : 
     125                 : Sema::InstantiatingTemplate::
     126                 : InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
     127                 :                       Decl *Entity,
     128             2023:                       SourceRange InstantiationRange)
     129             2023:   :  SemaRef(SemaRef) {
     130                 : 
     131                 :   Invalid = CheckInstantiationDepth(PointOfInstantiation,
     132             2023:                                     InstantiationRange);
                     2022: branch 0 taken
                        1: branch 1 taken
                        1: branch 2 taken
                        1: branch 3 taken
     133             2023:   if (!Invalid) {
     134             2022:     ActiveTemplateInstantiation Inst;
     135             2022:     Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation;
     136             2022:     Inst.PointOfInstantiation = PointOfInstantiation;
     137             2022:     Inst.Entity = reinterpret_cast<uintptr_t>(Entity);
     138             2022:     Inst.TemplateArgs = 0;
     139             2022:     Inst.NumTemplateArgs = 0;
     140             2022:     Inst.InstantiationRange = InstantiationRange;
     141             2022:     SemaRef.ActiveTemplateInstantiations.push_back(Inst);
     142                 :   }
     143             2023: }
     144                 : 
     145                 : Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
     146                 :                                          SourceLocation PointOfInstantiation,
     147                 :                                          TemplateDecl *Template,
     148                 :                                          const TemplateArgument *TemplateArgs,
     149                 :                                          unsigned NumTemplateArgs,
     150               65:                                          SourceRange InstantiationRange)
     151               65:   : SemaRef(SemaRef) {
     152                 : 
     153                 :   Invalid = CheckInstantiationDepth(PointOfInstantiation,
     154               65:                                     InstantiationRange);
                       65: branch 0 taken
                        0: branch 1 not taken
                        0: branch 2 not taken
                        0: branch 3 not taken
     155               65:   if (!Invalid) {
     156               65:     ActiveTemplateInstantiation Inst;
     157                 :     Inst.Kind
     158               65:       = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation;
     159               65:     Inst.PointOfInstantiation = PointOfInstantiation;
     160               65:     Inst.Entity = reinterpret_cast<uintptr_t>(Template);
     161               65:     Inst.TemplateArgs = TemplateArgs;
     162               65:     Inst.NumTemplateArgs = NumTemplateArgs;
     163               65:     Inst.InstantiationRange = InstantiationRange;
     164               65:     SemaRef.ActiveTemplateInstantiations.push_back(Inst);
     165                 :   }
     166               65: }
     167                 : 
     168                 : Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
     169                 :                                          SourceLocation PointOfInstantiation,
     170                 :                                       FunctionTemplateDecl *FunctionTemplate,
     171                 :                                         const TemplateArgument *TemplateArgs,
     172                 :                                                    unsigned NumTemplateArgs,
     173                 :                          ActiveTemplateInstantiation::InstantiationKind Kind,
     174             1252:                                               SourceRange InstantiationRange)
     175             1252: : SemaRef(SemaRef) {
     176                 : 
     177                 :   Invalid = CheckInstantiationDepth(PointOfInstantiation,
     178             1252:                                     InstantiationRange);
                     1252: branch 0 taken
                        0: branch 1 not taken
                        0: branch 2 not taken
                        0: branch 3 not taken
     179             1252:   if (!Invalid) {
     180             1252:     ActiveTemplateInstantiation Inst;
     181             1252:     Inst.Kind = Kind;
     182             1252:     Inst.PointOfInstantiation = PointOfInstantiation;
     183             1252:     Inst.Entity = reinterpret_cast<uintptr_t>(FunctionTemplate);
     184             1252:     Inst.TemplateArgs = TemplateArgs;
     185             1252:     Inst.NumTemplateArgs = NumTemplateArgs;
     186             1252:     Inst.InstantiationRange = InstantiationRange;
     187             1252:     SemaRef.ActiveTemplateInstantiations.push_back(Inst);
     188                 :     
                     1252: branch 1 taken
                        0: branch 2 not taken
                        0: branch 4 not taken
                        0: branch 5 not taken
     189             1252:     if (!Inst.isInstantiationRecord())
     190             1252:       ++SemaRef.NonInstantiationEntries;
     191                 :   }
     192             1252: }
     193                 : 
     194                 : Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
     195                 :                                          SourceLocation PointOfInstantiation,
     196                 :                           ClassTemplatePartialSpecializationDecl *PartialSpec,
     197                 :                                          const TemplateArgument *TemplateArgs,
     198                 :                                          unsigned NumTemplateArgs,
     199              244:                                          SourceRange InstantiationRange)
     200              244:   : SemaRef(SemaRef) {
     201                 : 
     202              244:   Invalid = false;
     203                 :     
     204              244:   ActiveTemplateInstantiation Inst;
     205              244:   Inst.Kind = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution;
     206              244:   Inst.PointOfInstantiation = PointOfInstantiation;
     207              244:   Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec);
     208              244:   Inst.TemplateArgs = TemplateArgs;
     209              244:   Inst.NumTemplateArgs = NumTemplateArgs;
     210              244:   Inst.InstantiationRange = InstantiationRange;
     211              244:   SemaRef.ActiveTemplateInstantiations.push_back(Inst);
     212                 :       
                        0: branch 1 not taken
                      244: branch 2 taken
                        0: branch 5 not taken
                        0: branch 6 not taken
     213              244:   assert(!Inst.isInstantiationRecord());
     214              244:   ++SemaRef.NonInstantiationEntries;
     215              244: }
     216                 : 
     217                 : Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef,
     218                 :                                           SourceLocation PointOfInstantiation,
     219                 :                                           ParmVarDecl *Param,
     220                 :                                           const TemplateArgument *TemplateArgs,
     221                 :                                           unsigned NumTemplateArgs,
     222               36:                                           SourceRange InstantiationRange)
     223               36:   : SemaRef(SemaRef) {
     224                 : 
     225               36:   Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange);
     226                 : 
                       36: branch 0 taken
                        0: branch 1 not taken
                        0: branch 2 not taken
                        0: branch 3 not taken
     227               36:   if (!Invalid) {
     228               36:     ActiveTemplateInstantiation Inst;
     229                 :     Inst.Kind
     230               36:       = ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation;
     231               36:     Inst.PointOfInstantiation = PointOfInstantiation;
     232               36:     Inst.Entity = reinterpret_cast<uintptr_t>(Param);
     233               36:     Inst.TemplateArgs = TemplateArgs;
     234               36:     Inst.NumTemplateArgs = NumTemplateArgs;
     235               36:     Inst.InstantiationRange = InstantiationRange;
     236               36:     SemaRef.ActiveTemplateInstantiations.push_back(Inst);
     237                 :   }
     238               36: }
     239                 : 
     240                 : Sema::InstantiatingTemplate::
     241                 : InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
     242                 :                       TemplateDecl *Template,
     243                 :                       NonTypeTemplateParmDecl *Param,
     244                 :                       const TemplateArgument *TemplateArgs,
     245                 :                       unsigned NumTemplateArgs,
     246               36:                       SourceRange InstantiationRange) : SemaRef(SemaRef) {
     247               36:   Invalid = false;
     248                 :   
     249               36:   ActiveTemplateInstantiation Inst;
     250               36:   Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
     251               36:   Inst.PointOfInstantiation = PointOfInstantiation;
     252               36:   Inst.Template = Template;
     253               36:   Inst.Entity = reinterpret_cast<uintptr_t>(Param);
     254               36:   Inst.TemplateArgs = TemplateArgs;
     255               36:   Inst.NumTemplateArgs = NumTemplateArgs;
     256               36:   Inst.InstantiationRange = InstantiationRange;
     257               36:   SemaRef.ActiveTemplateInstantiations.push_back(Inst);
     258                 :   
                        0: branch 1 not taken
                       36: branch 2 taken
                        0: branch 5 not taken
                        0: branch 6 not taken
     259               36:   assert(!Inst.isInstantiationRecord());
     260               36:   ++SemaRef.NonInstantiationEntries;
     261               36: }
     262                 : 
     263                 : Sema::InstantiatingTemplate::
     264                 : InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
     265                 :                       TemplateDecl *Template,
     266                 :                       TemplateTemplateParmDecl *Param,
     267                 :                       const TemplateArgument *TemplateArgs,
     268                 :                       unsigned NumTemplateArgs,
     269               41:                       SourceRange InstantiationRange) : SemaRef(SemaRef) {
     270               41:   Invalid = false;
     271               41:   ActiveTemplateInstantiation Inst;
     272               41:   Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution;
     273               41:   Inst.PointOfInstantiation = PointOfInstantiation;
     274               41:   Inst.Template = Template;
     275               41:   Inst.Entity = reinterpret_cast<uintptr_t>(Param);
     276               41:   Inst.TemplateArgs = TemplateArgs;
     277               41:   Inst.NumTemplateArgs = NumTemplateArgs;
     278               41:   Inst.InstantiationRange = InstantiationRange;
     279               41:   SemaRef.ActiveTemplateInstantiations.push_back(Inst);
     280                 :   
                        0: branch 1 not taken
                       41: branch 2 taken
                        0: branch 5 not taken
                        0: branch 6 not taken
     281               41:   assert(!Inst.isInstantiationRecord());
     282               41:   ++SemaRef.NonInstantiationEntries;
     283               41: }
     284                 : 
     285                 : Sema::InstantiatingTemplate::
     286                 : InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
     287                 :                       TemplateDecl *Template,
     288                 :                       NamedDecl *Param,
     289                 :                       const TemplateArgument *TemplateArgs,
     290                 :                       unsigned NumTemplateArgs,
     291              100:                       SourceRange InstantiationRange) : SemaRef(SemaRef) {
     292              100:   Invalid = false;
     293                 :   
     294              100:   ActiveTemplateInstantiation Inst;
     295              100:   Inst.Kind = ActiveTemplateInstantiation::DefaultTemplateArgumentChecking;
     296              100:   Inst.PointOfInstantiation = PointOfInstantiation;
     297              100:   Inst.Template = Template;
     298              100:   Inst.Entity = reinterpret_cast<uintptr_t>(Param);
     299              100:   Inst.TemplateArgs = TemplateArgs;
     300              100:   Inst.NumTemplateArgs = NumTemplateArgs;
     301              100:   Inst.InstantiationRange = InstantiationRange;
     302              100:   SemaRef.ActiveTemplateInstantiations.push_back(Inst);
     303                 :   
                        0: branch 1 not taken
                      100: branch 2 taken
                        0: branch 5 not taken
                        0: branch 6 not taken
     304              100:   assert(!Inst.isInstantiationRecord());
     305              100:   ++SemaRef.NonInstantiationEntries;
     306              100: }
     307                 : 
     308             3797: void Sema::InstantiatingTemplate::Clear() {
                     3796: branch 0 taken
                        1: branch 1 taken
     309             3797:   if (!Invalid) {
                     1200: branch 2 taken
                     2596: branch 3 taken
     310             3796:     if (!SemaRef.ActiveTemplateInstantiations.back().isInstantiationRecord()) {
                        0: branch 0 not taken
                     1200: branch 1 taken
     311             1200:       assert(SemaRef.NonInstantiationEntries > 0);
     312             1200:       --SemaRef.NonInstantiationEntries;
     313                 :     }
     314                 :     
     315             3796:     SemaRef.ActiveTemplateInstantiations.pop_back();
     316             3796:     Invalid = true;
     317                 :   }
     318             3797: }
     319                 : 
     320                 : bool Sema::InstantiatingTemplate::CheckInstantiationDepth(
     321                 :                                         SourceLocation PointOfInstantiation,
     322             3376:                                            SourceRange InstantiationRange) {
     323                 :   assert(SemaRef.NonInstantiationEntries <=
                        0: branch 1 not taken
                     3376: branch 2 taken
     324             3376:                                    SemaRef.ActiveTemplateInstantiations.size());
                     3375: branch 2 taken
                        1: branch 3 taken
     325             3376:   if ((SemaRef.ActiveTemplateInstantiations.size() - 
     326                 :           SemaRef.NonInstantiationEntries)
     327                 :         <= SemaRef.getLangOptions().InstantiationDepth)
     328             3375:     return false;
     329                 : 
     330                 :   SemaRef.Diag(PointOfInstantiation,
     331                 :                diag::err_template_recursion_depth_exceeded)
     332                 :     << SemaRef.getLangOptions().InstantiationDepth
     333                1:     << InstantiationRange;
     334                 :   SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth)
     335                1:     << SemaRef.getLangOptions().InstantiationDepth;
     336                1:   return true;
     337                 : }
     338                 : 
     339                 : /// \brief Prints the current instantiation stack through a series of
     340                 : /// notes.
     341              217: void Sema::PrintInstantiationStack() {
     342                 :   // FIXME: In all of these cases, we need to show the template arguments
                      236: branch 2 taken
                      217: branch 3 taken
     343              453:   for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator
     344              217:          Active = ActiveTemplateInstantiations.rbegin(),
     345              217:          ActiveEnd = ActiveTemplateInstantiations.rend();
     346                 :        Active != ActiveEnd;
     347                 :        ++Active) {
                      219: branch 1 taken
                        4: branch 2 taken
                        0: branch 3 not taken
                        0: branch 4 not taken
                        9: branch 5 taken
                        2: branch 6 taken
                        2: branch 7 taken
                        0: branch 8 not taken
     348              236:     switch (Active->Kind) {
     349                 :     case ActiveTemplateInstantiation::TemplateInstantiation: {
     350              219:       Decl *D = reinterpret_cast<Decl *>(Active->Entity);
                       85: branch 1 taken
                      134: branch 2 taken
     351              219:       if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
     352               85:         unsigned DiagID = diag::note_template_member_class_here;
                       76: branch 1 taken
                        9: branch 2 taken
     353               85:         if (isa<ClassTemplateSpecializationDecl>(Record))
     354               76:           DiagID = diag::note_template_class_instantiation_here;
     355                 :         Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
     356                 :                      DiagID)
     357                 :           << Context.getTypeDeclType(Record)
     358               85:           << Active->InstantiationRange;
                      123: branch 1 taken
                       11: branch 2 taken
     359              134:       } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
     360                 :         unsigned DiagID;
                       39: branch 1 taken
                       84: branch 2 taken
     361              123:         if (Function->getPrimaryTemplate())
     362               39:           DiagID = diag::note_function_template_spec_here;
     363                 :         else
     364               84:           DiagID = diag::note_template_member_function_here;
     365                 :         Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
     366                 :                      DiagID)
     367                 :           << Function
     368              123:           << Active->InstantiationRange;
     369                 :       } else {
     370                 :         Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
     371                 :                      diag::note_template_static_data_member_def_here)
     372                 :           << cast<VarDecl>(D)
     373               11:           << Active->InstantiationRange;
     374                 :       }
     375              219:       break;
     376                 :     }
     377                 : 
     378                 :     case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: {
     379                4:       TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity);
     380                 :       std::string TemplateArgsStr
     381                 :         = TemplateSpecializationType::PrintTemplateArgumentList(
     382                 :                                                          Active->TemplateArgs,
     383                 :                                                       Active->NumTemplateArgs,
     384                4:                                                       Context.PrintingPolicy);
     385                 :       Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
     386                 :                    diag::note_default_arg_instantiation_here)
     387                 :         << (Template->getNameAsString() + TemplateArgsStr)
     388                4:         << Active->InstantiationRange;
     389                4:       break;
     390                 :     }
     391                 : 
     392                 :     case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: {
     393                 :       FunctionTemplateDecl *FnTmpl
     394                0:         = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
     395                 :       Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
     396                 :                    diag::note_explicit_template_arg_substitution_here)
     397                0:         << FnTmpl << Active->InstantiationRange;
     398                0:       break;
     399                 :     }
     400                 : 
     401                 :     case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
                        0: branch 0 not taken
                        0: branch 1 not taken
     402                0:       if (ClassTemplatePartialSpecializationDecl *PartialSpec
     403                 :             = dyn_cast<ClassTemplatePartialSpecializationDecl>(
     404                0:                                                     (Decl *)Active->Entity)) {
     405                 :         Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
     406                 :                      diag::note_partial_spec_deduct_instantiation_here)
     407                 :           << Context.getTypeDeclType(PartialSpec)
     408                0:           << Active->InstantiationRange;
     409                 :       } else {
     410                 :         FunctionTemplateDecl *FnTmpl
     411                0:           = cast<FunctionTemplateDecl>((Decl *)Active->Entity);
     412                 :         Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
     413                 :                      diag::note_function_template_deduction_instantiation_here)
     414                0:           << FnTmpl << Active->InstantiationRange;
     415                 :       }
     416                0:       break;
     417                 : 
     418                 :     case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: {
     419                9:       ParmVarDecl *Param = cast<ParmVarDecl>((Decl *)Active->Entity);
     420                9:       FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext());
     421                 : 
     422                 :       std::string TemplateArgsStr
     423                 :         = TemplateSpecializationType::PrintTemplateArgumentList(
     424                 :                                                          Active->TemplateArgs,
     425                 :                                                       Active->NumTemplateArgs,
     426                9:                                                       Context.PrintingPolicy);
     427                 :       Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
     428                 :                    diag::note_default_function_arg_instantiation_here)
     429                 :         << (FD->getNameAsString() + TemplateArgsStr)
     430                9:         << Active->InstantiationRange;
     431                9:       break;
     432                 :     }
     433                 : 
     434                 :     case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution: {
     435                2:       NamedDecl *Parm = cast<NamedDecl>((Decl *)Active->Entity);
     436                2:       std::string Name;
                        2: branch 2 taken
                        0: branch 3 not taken
     437                2:       if (!Parm->getName().empty())
     438                2:         Name = std::string(" '") + Parm->getName().str() + "'";
     439                 :                                         
     440                 :       Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
     441                 :                    diag::note_prior_template_arg_substitution)
     442                 :         << isa<TemplateTemplateParmDecl>(Parm)
     443                 :         << Name
     444                 :         << getTemplateArgumentBindingsText(
     445                 :                                     Active->Template->getTemplateParameters(), 
     446                 :                                            Active->TemplateArgs, 
     447                 :                                            Active->NumTemplateArgs)
     448                2:         << Active->InstantiationRange;
     449                2:       break;
     450                 :     }
     451                 : 
     452                 :     case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking: {
     453                 :       Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
     454                 :                    diag::note_template_default_arg_checking)
     455                 :         << getTemplateArgumentBindingsText(
     456                 :                                      Active->Template->getTemplateParameters(), 
     457                 :                                            Active->TemplateArgs, 
     458                 :                                            Active->NumTemplateArgs)
     459                2:         << Active->InstantiationRange;
     460                 :       break;
     461                 :     }
     462                 :     }
     463                 :   }
     464              217: }
     465                 : 
     466            15682: bool Sema::isSFINAEContext() const {
     467                 :   using llvm::SmallVector;
                      435: branch 2 taken
                    15260: branch 3 taken
     468            15695:   for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator
     469            15682:          Active = ActiveTemplateInstantiations.rbegin(),
     470            15682:          ActiveEnd = ActiveTemplateInstantiations.rend();
     471                 :        Active != ActiveEnd;
     472                 :        ++Active) 
     473                 :   {
                      378: branch 1 taken
                       13: branch 2 taken
                       44: branch 3 taken
                        0: branch 4 not taken
     474              435:     switch(Active->Kind) {
     475                 :     case ActiveTemplateInstantiation::TemplateInstantiation:
     476                 :     case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation:
     477                 :       // This is a template instantiation, so there is no SFINAE.
     478              378:       return false;
     479                 : 
     480                 :     case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation:
     481                 :     case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution:
     482                 :     case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking:
     483                 :       // A default template argument instantiation and substitution into
     484                 :       // template parameters with arguments for prior parameters may or may 
     485                 :       // not be a SFINAE context; look further up the stack.
     486               13:       break;
     487                 : 
     488                 :     case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution:
     489                 :     case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution:
     490                 :       // We're either substitution explicitly-specified template arguments
     491                 :       // or deduced template arguments, so SFINAE applies.
     492               44:       return true;
     493                 :     }
     494                 :   }
     495                 : 
     496            15260:   return false;
     497                 : }
     498                 : 
     499                 : //===----------------------------------------------------------------------===/
     500                 : // Template Instantiation for Types
     501                 : //===----------------------------------------------------------------------===/
     502                 : namespace {
     503                 :   class TemplateInstantiator
     504                 :     : public TreeTransform<TemplateInstantiator> {
     505                 :     const MultiLevelTemplateArgumentList &TemplateArgs;
     506                 :     SourceLocation Loc;
     507                 :     DeclarationName Entity;
     508                 : 
     509                 :   public:
     510                 :     typedef TreeTransform<TemplateInstantiator> inherited;
     511                 : 
     512                 :     TemplateInstantiator(Sema &SemaRef,
     513                 :                          const MultiLevelTemplateArgumentList &TemplateArgs,
     514                 :                          SourceLocation Loc,
     515             4149:                          DeclarationName Entity)
     516                 :       : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc),
     517             4149:         Entity(Entity) { }
     518                 : 
     519                 :     /// \brief Determine whether the given type \p T has already been
     520                 :     /// transformed.
     521                 :     ///
     522                 :     /// For the purposes of template instantiation, a type has already been
     523                 :     /// transformed if it is NULL or if it is not dependent.
     524             6394:     bool AlreadyTransformed(QualType T) {
                     6394: branch 1 taken
                        0: branch 2 not taken
                      148: branch 5 taken
                     6246: branch 6 taken
     525             6394:       return T.isNull() || !T->isDependentType();
     526                 :     }
     527                 : 
     528                 :     /// \brief Returns the location of the entity being instantiated, if known.
     529             3748:     SourceLocation getBaseLocation() { return Loc; }
     530                 : 
     531                 :     /// \brief Returns the name of the entity being instantiated, if any.
     532             1976:     DeclarationName getBaseEntity() { return Entity; }
     533                 : 
     534                 :     /// \brief Sets the "base" location and entity when that
     535                 :     /// information is known based on another transformation.
     536             2266:     void setBase(SourceLocation Loc, DeclarationName Entity) {
     537             2266:       this->Loc = Loc;
     538             2266:       this->Entity = Entity;
     539             2266:     }
     540                 :       
     541                 :     /// \brief Transform the given declaration by instantiating a reference to
     542                 :     /// this declaration.
     543                 :     Decl *TransformDecl(Decl *D);
     544                 : 
     545                 :     /// \brief Transform the definition of the given declaration by
     546                 :     /// instantiating it.
     547                 :     Decl *TransformDefinition(Decl *D);
     548                 : 
     549                 :     /// \bried Transform the first qualifier within a scope by instantiating the
     550                 :     /// declaration.
     551                 :     NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc);
     552                 :       
     553                 :     /// \brief Rebuild the exception declaration and register the declaration
     554                 :     /// as an instantiated local.
     555                 :     VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, QualType T,
     556                 :                                   TypeSourceInfo *Declarator,
     557                 :                                   IdentifierInfo *Name,
     558                 :                                   SourceLocation Loc, SourceRange TypeRange);
     559                 : 
     560                 :     /// \brief Check for tag mismatches when instantiating an
     561                 :     /// elaborated type.
     562                 :     QualType RebuildElaboratedType(QualType T, ElaboratedType::TagKind Tag);
     563                 : 
     564                 :     Sema::OwningExprResult TransformPredefinedExpr(PredefinedExpr *E);
     565                 :     Sema::OwningExprResult TransformDeclRefExpr(DeclRefExpr *E);
     566                 :     Sema::OwningExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E);
     567                 :     Sema::OwningExprResult TransformTemplateParmRefExpr(DeclRefExpr *E,
     568                 :                                                 NonTypeTemplateParmDecl *D);
     569                 : 
     570                 :     /// \brief Transforms a template type parameter type by performing
     571                 :     /// substitution of the corresponding template type argument.
     572                 :     QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB,
     573                 :                                            TemplateTypeParmTypeLoc TL);
     574                 :   };
     575                 : }
     576                 : 
     577             2286: Decl *TemplateInstantiator::TransformDecl(Decl *D) {
                       84: branch 0 taken
                     2202: branch 1 taken
     578             2286:   if (!D)
     579               84:     return 0;
     580                 : 
                       20: branch 1 taken
                     2182: branch 2 taken
     581             2202:   if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) {
                       19: branch 2 taken
                        1: branch 3 taken
     582               20:     if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
     583                 :       // If the corresponding template argument is NULL or non-existent, it's
     584                 :       // because we are performing instantiation from explicitly-specified
     585                 :       // template arguments in a function template, but there were some
     586                 :       // arguments left unspecified.
                        0: branch 3 not taken
                       19: branch 4 taken
     587               19:       if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(),
     588                 :                                             TTP->getPosition()))
     589                0:         return D;
     590                 : 
     591                 :       TemplateName Template
     592               19:         = TemplateArgs(TTP->getDepth(), TTP->getPosition()).getAsTemplate();
     593                 :       assert(!Template.isNull() && Template.getAsTemplateDecl() &&
                       19: branch 1 taken
                        0: branch 2 not taken
                       19: branch 4 taken
                        0: branch 5 not taken
     594               19:              "Wrong kind of template template argument");
     595               19:       return Template.getAsTemplateDecl();
     596                 :     }
     597                 : 
     598                 :     // Fall through to find the instantiated declaration for this template
     599                 :     // template parameter.
     600                 :   }
     601                 : 
     602             2183:   return SemaRef.FindInstantiatedDecl(cast<NamedDecl>(D), TemplateArgs);
     603                 : }
     604                 : 
     605              203: Decl *TemplateInstantiator::TransformDefinition(Decl *D) {
     606              203:   Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs);
                        7: branch 0 taken
                      196: branch 1 taken
     607              203:   if (!Inst)
     608                7:     return 0;
     609                 : 
     610              196:   getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst);
     611              196:   return Inst;
     612                 : }
     613                 : 
     614                 : NamedDecl *
     615                 : TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D, 
     616               96:                                                      SourceLocation Loc) {
     617                 :   // If the first part of the nested-name-specifier was a template type 
     618                 :   // parameter, instantiate that type parameter down to a tag type.
                        4: branch 1 taken
                       92: branch 2 taken
     619               96:   if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
     620                 :     const TemplateTypeParmType *TTP 
     621                4:       = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD));
                        3: branch 2 taken
                        1: branch 3 taken
     622                4:     if (TTP->getDepth() < TemplateArgs.getNumLevels()) {
     623                3:       QualType T = TemplateArgs(TTP->getDepth(), TTP->getIndex()).getAsType();
                        0: branch 1 not taken
                        3: branch 2 taken
     624                3:       if (T.isNull())
     625                0:         return cast_or_null<NamedDecl>(TransformDecl(D));
     626                 :       
                        3: branch 2 taken
                        0: branch 3 not taken
     627                3:       if (const TagType *Tag = T->getAs<TagType>())
     628                3:         return Tag->getDecl();
     629                 :       
     630                 :       // The resulting type is not a tag; complain.
     631                0:       getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T;
     632                0:       return 0;
     633                 :     }
     634                 :   }
     635                 :   
     636               93:   return cast_or_null<NamedDecl>(TransformDecl(D));
     637                 : }
     638                 : 
     639                 : VarDecl *
     640                 : TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl,
     641                 :                                            QualType T,
     642                 :                                            TypeSourceInfo *Declarator,
     643                 :                                            IdentifierInfo *Name,
     644                 :                                            SourceLocation Loc,
     645                6:                                            SourceRange TypeRange) {
     646                 :   VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, T, Declarator,
     647                6:                                                  Name, Loc, TypeRange);
                        6: branch 0 taken
                        0: branch 1 not taken
                        2: branch 3 taken
                        4: branch 4 taken
                        2: branch 5 taken
                        4: branch 6 taken
     648                6:   if (Var && !Var->isInvalidDecl())
     649                2:     getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var);
     650                6:   return Var;
     651                 : }
     652                 : 
     653                 : QualType
     654                 : TemplateInstantiator::RebuildElaboratedType(QualType T,
     655               22:                                             ElaboratedType::TagKind Tag) {
                       22: branch 2 taken
                        0: branch 3 not taken
     656               22:   if (const TagType *TT = T->getAs<TagType>()) {
     657               22:     TagDecl* TD = TT->getDecl();
     658                 : 
     659                 :     // FIXME: this location is very wrong;  we really need typelocs.
     660               22:     SourceLocation TagLocation = TD->getTagKeywordLoc();
     661                 : 
     662                 :     // FIXME: type might be anonymous.
     663               22:     IdentifierInfo *Id = TD->getIdentifier();
     664                 : 
     665                 :     // TODO: should we even warn on struct/class mismatches for this?  Seems
     666                 :     // like it's likely to produce a lot of spurious errors.
                        1: branch 1 taken
                       21: branch 2 taken
     667               22:     if (!SemaRef.isAcceptableTagRedeclaration(TD, Tag, TagLocation, *Id)) {
     668                 :       SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag)
     669                 :         << Id
     670                 :         << CodeModificationHint::CreateReplacement(SourceRange(TagLocation),
     671                1:                                                    TD->getKindName());
     672                1:       SemaRef.Diag(TD->getLocation(), diag::note_previous_use);
     673                 :     }
     674                 :   }
     675                 : 
     676               22:   return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(T, Tag);
     677                 : }
     678                 : 
     679                 : Sema::OwningExprResult 
     680               17: TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) {
                        0: branch 1 not taken
                       17: branch 2 taken
     681               17:   if (!E->isTypeDependent())
     682                0:     return SemaRef.Owned(E->Retain());
     683                 : 
     684               17:   FunctionDecl *currentDecl = getSema().getCurFunctionDecl();
     685                 :   assert(currentDecl && "Must have current function declaration when "
                        0: branch 0 not taken
                       17: branch 1 taken
     686               17:                         "instantiating.");
     687                 : 
     688               17:   PredefinedExpr::IdentType IT = E->getIdentType();
     689                 : 
     690                 :   unsigned Length =
     691               17:     PredefinedExpr::ComputeName(getSema().Context, IT, currentDecl).length();
     692                 : 
     693               17:   llvm::APInt LengthI(32, Length + 1);
     694               17:   QualType ResTy = getSema().Context.CharTy.withConst();
     695                 :   ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI, 
     696               17:                                                  ArrayType::Normal, 0);
     697                 :   PredefinedExpr *PE =
                       17: branch 3 taken
                        0: branch 4 not taken
     698               17:     new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT);
     699               17:   return getSema().Owned(PE);
     700                 : }
     701                 : 
     702                 : Sema::OwningExprResult
     703                 : TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E,
     704             1103:                                                NonTypeTemplateParmDecl *NTTP) {
     705                 :   // If the corresponding template argument is NULL or non-existent, it's
     706                 :   // because we are performing instantiation from explicitly-specified
     707                 :   // template arguments in a function template, but there were some
     708                 :   // arguments left unspecified.
                        0: branch 3 not taken
                     1103: branch 4 taken
     709             1103:   if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(),
     710                 :                                         NTTP->getPosition()))
     711                0:     return SemaRef.Owned(E->Retain());
     712                 : 
     713                 :   const TemplateArgument &Arg = TemplateArgs(NTTP->getDepth(),
     714             1103:                                              NTTP->getPosition());
     715                 : 
     716                 :   // The template argument itself might be an expression, in which
     717                 :   // case we just return that expression.
                        1: branch 1 taken
                     1102: branch 2 taken
     718             1103:   if (Arg.getKind() == TemplateArgument::Expression)
     719                1:     return SemaRef.Owned(Arg.getAsExpr()->Retain());
     720                 : 
                       15: branch 1 taken
                     1087: branch 2 taken
     721             1102:   if (Arg.getKind() == TemplateArgument::Declaration) {
     722               15:     ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
     723                 : 
     724                 :     // Find the instantiation of the template argument.  This is
     725                 :     // required for nested templates.
     726                 :     VD = cast_or_null<ValueDecl>(
     727               15:                               getSema().FindInstantiatedDecl(VD, TemplateArgs));
                        0: branch 0 not taken
                       15: branch 1 taken
     728               15:     if (!VD)
     729                0:       return SemaRef.ExprError();
     730                 : 
     731                 :     // Derive the type we want the substituted decl to have.  This had
     732                 :     // better be non-dependent, or these checks will have serious problems.
     733                 :     QualType TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs,
     734                 :                                             E->getLocation(), 
     735               15:                                             DeclarationName());
                       15: branch 1 taken
                        0: branch 2 not taken
     736               15:     assert(!TargetType.isNull() && "type substitution failed for param type");
                       15: branch 2 taken
                        0: branch 3 not taken
     737               15:     assert(!TargetType->isDependentType() && "param type still dependent");
     738                 : 
                        4: branch 2 taken
                       11: branch 3 taken
                        4: branch 5 taken
                        0: branch 6 not taken
                        4: branch 8 taken
                        0: branch 9 not taken
                        4: branch 10 taken
                       11: branch 11 taken
     739               19:     if (VD->getDeclContext()->isRecord() && 
     740                 :         (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
     741                 :       // If the value is a class member, we might have a pointer-to-member.
     742                 :       // Determine whether the non-type template template parameter is of
     743                 :       // pointer-to-member type. If so, we need to build an appropriate
     744                 :       // expression for a pointer-to-member, since a "normal" DeclRefExpr
     745                 :       // would refer to the member itself.
                        4: branch 2 taken
                        0: branch 3 not taken
     746                4:       if (TargetType->isMemberPointerType()) {
     747                 :         QualType ClassType
     748                 :           = SemaRef.Context.getTypeDeclType(
     749                4:                                         cast<RecordDecl>(VD->getDeclContext()));
     750                 :         NestedNameSpecifier *Qualifier
     751                 :           = NestedNameSpecifier::Create(SemaRef.Context, 0, false,
     752                4:                                         ClassType.getTypePtr());
     753                4:         CXXScopeSpec SS;
     754                4:         SS.setScopeRep(Qualifier);
     755                 :         OwningExprResult RefExpr 
     756                 :           = SemaRef.BuildDeclRefExpr(VD, 
     757                 :                                      VD->getType().getNonReferenceType(), 
     758                 :                                      E->getLocation(), 
     759                4:                                      &SS);
                        0: branch 1 not taken
                        4: branch 2 taken
     760                4:         if (RefExpr.isInvalid())
     761                0:           return SemaRef.ExprError();
     762                 : 
     763                 :         RefExpr = SemaRef.CreateBuiltinUnaryOp(E->getLocation(), 
     764                 :                                                UnaryOperator::AddrOf, 
     765                4:                                                move(RefExpr));
     766                 :         assert(!RefExpr.isInvalid() &&
     767                 :                SemaRef.Context.hasSameType(((Expr*) RefExpr.get())->getType(),
                        4: branch 1 taken
                        0: branch 2 not taken
                        0: branch 6 not taken
                        4: branch 7 taken
     768                4:                                            TargetType));
     769                4:         return move(RefExpr);
     770                 :       }
     771                 :     }
     772                 : 
     773               11:     QualType T = VD->getType().getNonReferenceType();
     774                 : 
                        7: branch 2 taken
                        4: branch 3 taken
     775               11:     if (TargetType->isPointerType()) {
     776                 :       // C++03 [temp.arg.nontype]p5:
     777                 :       //  - For a non-type template-parameter of type pointer to
     778                 :       //    object, qualification conversions and the array-to-pointer
     779                 :       //    conversion are applied.
     780                 :       //  - For a non-type template-parameter of type pointer to
     781                 :       //    function, only the function-to-pointer conversion is
     782                 :       //    applied.
     783                 : 
     784                 :       OwningExprResult RefExpr
     785                7:         = SemaRef.BuildDeclRefExpr(VD, T, E->getLocation());
                        0: branch 1 not taken
                        7: branch 2 taken
     786                7:       if (RefExpr.isInvalid())
     787                0:         return SemaRef.ExprError();
     788                 : 
     789                 :       // Decay functions and arrays.
     790                7:       Expr *RefE = (Expr *)RefExpr.get();
     791                7:       SemaRef.DefaultFunctionArrayConversion(RefE);
                        4: branch 1 taken
                        3: branch 2 taken
     792                7:       if (RefE != RefExpr.get()) {
     793                4:         RefExpr.release();
     794                4:         RefExpr = SemaRef.Owned(RefE);
     795                 :       }
     796                 : 
     797                 :       // Qualification conversions.
     798                7:       RefExpr.release();
     799                 :       SemaRef.ImpCastExprToType(RefE, TargetType.getUnqualifiedType(),
     800                7:                                 CastExpr::CK_NoOp);
     801                7:       return SemaRef.Owned(RefE);
     802                 :     }
     803                 : 
     804                 :     // If the non-type template parameter has reference type, qualify the
     805                 :     // resulting declaration reference with the extra qualifiers on the
     806                 :     // type that the reference refers to.
                        4: branch 2 taken
                        0: branch 3 not taken
     807                4:     if (const ReferenceType *TargetRef = TargetType->getAs<ReferenceType>())
     808                 :       T = SemaRef.Context.getQualifiedType(T, 
     809                4:                                   TargetRef->getPointeeType().getQualifiers());
     810                 :     
     811                4:     return SemaRef.BuildDeclRefExpr(VD, T, E->getLocation());
     812                 :   }
     813                 : 
                        0: branch 1 not taken
                     1087: branch 2 taken
     814             1087:   assert(Arg.getKind() == TemplateArgument::Integral);
     815             1087:   QualType T = Arg.getIntegralType();
                     1085: branch 2 taken
                        2: branch 3 taken
                        0: branch 6 not taken
                     1085: branch 7 taken
                        2: branch 8 taken
                     1085: branch 9 taken
     816             1087:   if (T->isCharType() || T->isWideCharType())
     817                 :     return SemaRef.Owned(new (SemaRef.Context) CharacterLiteral(
     818                 :                                               Arg.getAsIntegral()->getZExtValue(),
     819                 :                                               T->isWideCharType(),
     820                 :                                               T,
                        2: branch 7 taken
                        0: branch 8 not taken
     821                2:                                               E->getSourceRange().getBegin()));
                        8: branch 2 taken
                     1077: branch 3 taken
     822             1085:   if (T->isBooleanType())
     823                 :     return SemaRef.Owned(new (SemaRef.Context) CXXBoolLiteralExpr(
     824                 :                                             Arg.getAsIntegral()->getBoolValue(),
     825                 :                                             T,
                        8: branch 5 taken
                        0: branch 6 not taken
     826                8:                                             E->getSourceRange().getBegin()));
     827                 : 
                        0: branch 3 not taken
                     1077: branch 4 taken
     828             1077:   assert(Arg.getAsIntegral()->getBitWidth() == SemaRef.Context.getIntWidth(T));
     829                 :   return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral(
     830                 :                                                 *Arg.getAsIntegral(),
     831                 :                                                 T,
                     1077: branch 4 taken
                        0: branch 5 not taken
     832             1077:                                                 E->getSourceRange().getBegin()));
     833                 : }
     834                 :                                                    
     835                 : 
     836                 : Sema::OwningExprResult
     837             1682: TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) {
     838             1682:   NamedDecl *D = E->getDecl();
                     1104: branch 1 taken
                      578: branch 2 taken
     839             1682:   if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
                     1103: branch 2 taken
                        1: branch 3 taken
     840             1104:     if (NTTP->getDepth() < TemplateArgs.getNumLevels())
     841             1103:       return TransformTemplateParmRefExpr(E, NTTP);
     842                 :     
     843                 :     // We have a non-type template parameter that isn't fully substituted;
     844                 :     // FindInstantiatedDecl will find it in the local instantiation scope.
     845                 :   }
     846                 : 
     847              579:   return TreeTransform<TemplateInstantiator>::TransformDeclRefExpr(E);
     848                 : }
     849                 : 
     850                 : Sema::OwningExprResult TemplateInstantiator::TransformCXXDefaultArgExpr(
     851                3:     CXXDefaultArgExpr *E) {
     852                 :   assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())->
     853                 :              getDescribedFunctionTemplate() &&
                        3: branch 4 taken
                        0: branch 5 not taken
     854                3:          "Default arg expressions are never formed in dependent cases.");
     855                 :   return SemaRef.BuildCXXDefaultArgExpr(E->getUsedLocation(),
     856                 :                            cast<FunctionDecl>(E->getParam()->getDeclContext()), 
     857                3:                                         E->getParam());
     858                 : }
     859                 : 
     860                 : 
     861                 : QualType
     862                 : TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB,
     863             2801:                                                 TemplateTypeParmTypeLoc TL) {
     864             2801:   TemplateTypeParmType *T = TL.getTypePtr();
                     2733: branch 2 taken
                       68: branch 3 taken
     865             2801:   if (T->getDepth() < TemplateArgs.getNumLevels()) {
     866                 :     // Replace the template type parameter with its corresponding
     867                 :     // template argument.
     868                 : 
     869                 :     // If the corresponding template argument is NULL or doesn't exist, it's
     870                 :     // because we are performing instantiation from explicitly-specified
     871                 :     // template arguments in a function template class, but there were some
     872                 :     // arguments left unspecified.
                       36: branch 3 taken
                     2697: branch 4 taken
     873             2733:     if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) {
     874                 :       TemplateTypeParmTypeLoc NewTL
     875               36:         = TLB.push<TemplateTypeParmTypeLoc>(TL.getType());
     876               36:       NewTL.setNameLoc(TL.getNameLoc());
     877               36:       return TL.getType();
     878                 :     }
     879                 : 
     880                 :     assert(TemplateArgs(T->getDepth(), T->getIndex()).getKind()
     881                 :              == TemplateArgument::Type &&
                     2697: branch 4 taken
                        0: branch 5 not taken
     882             2697:            "Template argument kind mismatch");
     883                 : 
     884                 :     QualType Replacement
     885             2697:       = TemplateArgs(T->getDepth(), T->getIndex()).getAsType();
     886                 : 
     887                 :     // TODO: only do this uniquing once, at the start of instantiation.
     888                 :     QualType Result
     889             2697:       = getSema().Context.getSubstTemplateTypeParmType(T, Replacement);
     890                 :     SubstTemplateTypeParmTypeLoc NewTL
     891             2697:       = TLB.push<SubstTemplateTypeParmTypeLoc>(Result);
     892             2697:     NewTL.setNameLoc(TL.getNameLoc());
     893             2697:     return Result;
     894                 :   }
     895                 : 
     896                 :   // The template type parameter comes from an inner template (e.g.,
     897                 :   // the template parameter list of a member template inside the
     898                 :   // template we are instantiating). Create a new template type
     899                 :   // parameter with the template "level" reduced by one.
     900                 :   QualType Result
     901                 :     = getSema().Context.getTemplateTypeParmType(T->getDepth()
     902                 :                                                  - TemplateArgs.getNumLevels(),
     903                 :                                                 T->getIndex(),
     904                 :                                                 T->isParameterPack(),
     905               68:                                                 T->getName());
     906               68:   TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
     907               68:   NewTL.setNameLoc(TL.getNameLoc());
     908               68:   return Result;
     909                 : }
     910                 : 
     911                 : /// \brief Perform substitution on the type T with a given set of template
     912                 : /// arguments.
     913                 : ///
     914                 : /// This routine substitutes the given template arguments into the
     915                 : /// type T and produces the instantiated type.
     916                 : ///
     917                 : /// \param T the type into which the template arguments will be
     918                 : /// substituted. If this type is not dependent, it will be returned
     919                 : /// immediately.
     920                 : ///
     921                 : /// \param TemplateArgs the template arguments that will be
     922                 : /// substituted for the top-level template parameters within T.
     923                 : ///
     924                 : /// \param Loc the location in the source code where this substitution
     925                 : /// is being performed. It will typically be the location of the
     926                 : /// declarator (if we're instantiating the type of some declaration)
     927                 : /// or the location of the type in the source code (if, e.g., we're
     928                 : /// instantiating the type of a cast expression).
     929                 : ///
     930                 : /// \param Entity the name of the entity associated with a declaration
     931                 : /// being instantiated (if any). May be empty to indicate that there
     932                 : /// is no such entity (if, e.g., this is a type that occurs as part of
     933                 : /// a cast expression) or that the entity has no name (e.g., an
     934                 : /// unnamed function parameter).
     935                 : ///
     936                 : /// \returns If the instantiation succeeds, the instantiated
     937                 : /// type. Otherwise, produces diagnostics and returns a NULL type.
     938                 : TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T,
     939                 :                                 const MultiLevelTemplateArgumentList &Args,
     940                 :                                 SourceLocation Loc,
     941             1867:                                 DeclarationName Entity) {
     942                 :   assert(!ActiveTemplateInstantiations.empty() &&
     943                 :          "Cannot perform an instantiation without some context on the "
                     1867: branch 1 taken
                        0: branch 2 not taken
     944             1867:          "instantiation stack");
     945                 :   
                      362: branch 3 taken
                     1505: branch 4 taken
     946             1867:   if (!T->getType()->isDependentType())
     947              362:     return T;
     948                 : 
     949             1505:   TemplateInstantiator Instantiator(*this, Args, Loc, Entity);
     950             1505:   return Instantiator.TransformType(T);
     951                 : }
     952                 : 
     953                 : /// Deprecated form of the above.
     954                 : QualType Sema::SubstType(QualType T,
     955                 :                          const MultiLevelTemplateArgumentList &TemplateArgs,
     956             1789:                          SourceLocation Loc, DeclarationName Entity) {
     957                 :   assert(!ActiveTemplateInstantiations.empty() &&
     958                 :          "Cannot perform an instantiation without some context on the "
                     1789: branch 1 taken
                        0: branch 2 not taken
     959             1789:          "instantiation stack");
     960                 : 
     961                 :   // If T is not a dependent type, there is nothing to do.
                      972: branch 2 taken
                      817: branch 3 taken
     962             1789:   if (!T->isDependentType())
     963              972:     return T;
     964                 : 
     965              817:   TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity);
     966              817:   return Instantiator.TransformType(T);
     967                 : }
     968                 : 
     969                 : /// \brief Perform substitution on the base class specifiers of the
     970                 : /// given class template specialization.
     971                 : ///
     972                 : /// Produces a diagnostic and returns true on error, returns false and
     973                 : /// attaches the instantiated base classes to the class template
     974                 : /// specialization if successful.
     975                 : bool
     976                 : Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation,
     977                 :                           CXXRecordDecl *Pattern,
     978             1441:                           const MultiLevelTemplateArgumentList &TemplateArgs) {
     979             1441:   bool Invalid = false;
     980             1441:   llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases;
                      100: branch 0 taken
                     1441: branch 1 taken
     981             1541:   for (ClassTemplateSpecializationDecl::base_class_iterator
     982             1441:          Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end();
     983                 :        Base != BaseEnd; ++Base) {
                       28: branch 3 taken
                       72: branch 4 taken
     984              100:     if (!Base->getType()->isDependentType()) {
     985                 :       const CXXRecordDecl *BaseDecl =
     986               28:         cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
     987                 :       
     988                 :       // Make sure to set the attributes from the base.
     989                 :       SetClassDeclAttributesFromBase(Instantiation, BaseDecl, 
     990               28:                                      Base->isVirtual());
     991                 :       
                       28: branch 1 taken
                        0: branch 2 not taken
     992               28:       InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base));
     993               28:       continue;
     994                 :     }
     995                 : 
     996                 :     QualType BaseType = SubstType(Base->getType(),
     997                 :                                   TemplateArgs,
     998                 :                                   Base->getSourceRange().getBegin(),
     999               72:                                   DeclarationName());
                        0: branch 1 not taken
                       72: branch 2 taken
    1000               72:     if (BaseType.isNull()) {
    1001                0:       Invalid = true;
    1002                0:       continue;
    1003                 :     }
    1004                 : 
                       57: branch 0 taken
                       15: branch 1 taken
    1005               72:     if (CXXBaseSpecifier *InstantiatedBase
    1006                 :           = CheckBaseSpecifier(Instantiation,
    1007                 :                                Base->getSourceRange(),
    1008                 :                                Base->isVirtual(),
    1009                 :                                Base->getAccessSpecifierAsWritten(),
    1010                 :                                BaseType,
    1011                 :                                /*FIXME: Not totally accurate */
    1012               72:                                Base->getSourceRange().getBegin()))
    1013               57:       InstantiatedBases.push_back(InstantiatedBase);
    1014                 :     else
    1015               15:       Invalid = true;
    1016                 :   }
    1017                 : 
                     1427: branch 0 taken
                       14: branch 1 taken
                        0: branch 5 not taken
                     1427: branch 6 taken
                        0: branch 7 not taken
                     1441: branch 8 taken
    1018             1441:   if (!Invalid &&
    1019                 :       AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(),
    1020                 :                            InstantiatedBases.size()))
    1021                0:     Invalid = true;
    1022                 : 
    1023             1441:   return Invalid;
    1024                 : }
    1025                 : 
    1026                 : /// \brief Instantiate the definition of a class from a given pattern.
    1027                 : ///
    1028                 : /// \param PointOfInstantiation The point of instantiation within the
    1029                 : /// source code.
    1030                 : ///
    1031                 : /// \param Instantiation is the declaration whose definition is being
    1032                 : /// instantiated. This will be either a class template specialization
    1033                 : /// or a member class of a class template specialization.
    1034                 : ///
    1035                 : /// \param Pattern is the pattern from which the instantiation
    1036                 : /// occurs. This will be either the declaration of a class template or
    1037                 : /// the declaration of a member class of a class template.
    1038                 : ///
    1039                 : /// \param TemplateArgs The template arguments to be substituted into
    1040                 : /// the pattern.
    1041                 : ///
    1042                 : /// \param TSK the kind of implicit or explicit instantiation to perform.
    1043                 : ///
    1044                 : /// \param Complain whether to complain if the class cannot be instantiated due
    1045                 : /// to the lack of a definition.
    1046                 : ///
    1047                 : /// \returns true if an error occurred, false otherwise.
    1048                 : bool
    1049                 : Sema::InstantiateClass(SourceLocation PointOfInstantiation,
    1050                 :                        CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern,
    1051                 :                        const MultiLevelTemplateArgumentList &TemplateArgs,
    1052                 :                        TemplateSpecializationKind TSK,
    1053             1462:                        bool Complain) {
    1054             1462:   bool Invalid = false;
    1055                 : 
    1056                 :   CXXRecordDecl *PatternDef
    1057             1462:     = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context));
                       20: branch 0 taken
                     1442: branch 1 taken
    1058             1462:   if (!PatternDef) {
                       18: branch 0 taken
                        2: branch 1 taken
    1059               20:     if (!Complain) {
    1060                 :       // Say nothing
                        1: branch 1 taken
                       17: branch 2 taken
    1061               18:     } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) {
    1062                 :       Diag(PointOfInstantiation,
    1063                 :            diag::err_implicit_instantiate_member_undefined)
    1064                1:         << Context.getTypeDeclType(Instantiation);
    1065                1:       Diag(Pattern->getLocation(), diag::note_member_of_template_here);
    1066                 :     } else {
    1067                 :       Diag(PointOfInstantiation, diag::err_template_instantiate_undefined)
    1068                 :         << (TSK != TSK_ImplicitInstantiation)
    1069               17:         << Context.getTypeDeclType(Instantiation);
    1070               17:       Diag(Pattern->getLocation(), diag::note_template_decl_here);
    1071                 :     }
    1072               20:     return true;
    1073                 :   }
    1074             1442:   Pattern = PatternDef;
    1075                 : 
    1076                 :   // \brief Record the point of instantiation.
                       61: branch 0 taken
                     1381: branch 1 taken
    1077             1442:   if (MemberSpecializationInfo *MSInfo 
    1078             1442:         = Instantiation->getMemberSpecializationInfo()) {
    1079               61:     MSInfo->setTemplateSpecializationKind(TSK);
    1080               61:     MSInfo->setPointOfInstantiation(PointOfInstantiation);
                     1381: branch 0 taken
                        0: branch 1 not taken
    1081             1381:   } else if (ClassTemplateSpecializationDecl *Spec 
    1082             1381:                = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) {
    1083             1381:     Spec->setTemplateSpecializationKind(TSK);
    1084             1381:     Spec->setPointOfInstantiation(PointOfInstantiation);
    1085                 :   }
    1086                 :   
    1087             1442:   InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation);
                        1: branch 1 taken
                     1441: branch 2 taken
    1088             1442:   if (Inst)
    1089                1:     return true;
    1090                 : 
    1091                 :   // Enter the scope of this instantiation. We don't use
    1092                 :   // PushDeclContext because we don't have a scope.
    1093             1441:   DeclContext *PreviousContext = CurContext;
                     1441: branch 0 taken
                        0: branch 1 not taken
    1094             1441:   CurContext = Instantiation;
    1095                 : 
    1096                 :   // Start the definition of this instantiation.
    1097             1441:   Instantiation->startDefinition();
    1098                 : 
    1099                 :   // Do substitution on the base class specifiers.
                       14: branch 1 taken
                     1427: branch 2 taken
    1100             1441:   if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs))
    1101               14:     Invalid = true;
    1102                 : 
    1103             1441:   llvm::SmallVector<DeclPtrTy, 4> Fields;
                     3204: branch 3 taken
                     1441: branch 4 taken
    1104             6086:   for (RecordDecl::decl_iterator Member = Pattern->decls_begin(),
    1105             1441:          MemberEnd = Pattern->decls_end();
    1106                 :        Member != MemberEnd; ++Member) {
                     3204: branch 0 taken
                        0: branch 1 not taken
    1107             3204:     Decl *NewMember = SubstDecl(*Member, Instantiation, TemplateArgs);
                     3179: branch 0 taken
                       25: branch 1 taken
    1108             3204:     if (NewMember) {
                      164: branch 1 taken
                     3015: branch 2 taken
    1109             3179:       if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember))
    1110              164:         Fields.push_back(DeclPtrTy::make(Field));
                       26: branch 1 taken
                     2989: branch 2 taken
    1111             3015:       else if (NewMember->isInvalidDecl())
    1112               26:         Invalid = true;
    1113                 :     } else {
    1114                 :       // FIXME: Eventually, a NULL return will mean that one of the
    1115                 :       // instantiations was a semantic disaster, and we'll want to set Invalid =
    1116                 :       // true. For now, we expect to skip some members that we can't yet handle.
    1117                 :     }
    1118                 :   }
    1119                 : 
    1120                 :   // Finish checking fields.
    1121                 :   ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation),
    1122                 :               Fields.data(), Fields.size(), SourceLocation(), SourceLocation(),
    1123             1441:               0);
    1124             1441:   CheckCompletedCXXClass(Instantiation);
                       28: branch 1 taken
                     1413: branch 2 taken
    1125             1441:   if (Instantiation->isInvalidDecl())
    1126               28:     Invalid = true;
    1127                 :   
    1128                 :   // Exit the scope of this instantiation.
    1129             1441:   CurContext = PreviousContext;
    1130                 : 
    1131                 :   // If this is a polymorphic C++ class without a key function, we'll
    1132                 :   // have to mark all of the virtual members to allow emission of a vtable
    1133                 :   // in this translation unit.
                       27: branch 1 taken
                     1414: branch 2 taken
                       16: branch 4 taken
                       11: branch 5 taken
                       16: branch 6 taken
                     1425: branch 7 taken
    1134             1441:   if (Instantiation->isDynamicClass() && !Context.getKeyFunction(Instantiation))
    1135                 :       ClassesWithUnmarkedVirtualMembers.push_back(std::make_pair(Instantiation,
    1136               16:                                                        PointOfInstantiation));
    1137                 : 
                     1378: branch 0 taken
                       63: branch 1 taken
    1138             1441:   if (!Invalid)
    1139             1378:     Consumer.HandleTagDeclDefinition(Instantiation);
    1140                 : 
    1141             1441:   return Invalid;
    1142                 : }
    1143                 : 
    1144                 : bool
    1145                 : Sema::InstantiateClassTemplateSpecialization(
    1146                 :                            SourceLocation PointOfInstantiation,
    1147                 :                            ClassTemplateSpecializationDecl *ClassTemplateSpec,
    1148                 :                            TemplateSpecializationKind TSK,
    1149             1401:                            bool Complain) {
    1150                 :   // Perform the actual instantiation on the canonical declaration.
    1151                 :   ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>(
    1152             1401:                                          ClassTemplateSpec->getCanonicalDecl());
    1153                 : 
    1154                 :   // Check whether we have already instantiated or specialized this class
    1155                 :   // template specialization.
                        0: branch 1 not taken
                     1401: branch 2 taken
    1156             1401:   if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) {
                        0: branch 1 not taken
                        0: branch 2 not taken
                        0: branch 3 not taken
                        0: branch 4 not taken
                        0: branch 5 not taken
                        0: branch 6 not taken
    1157                0:     if (ClassTemplateSpec->getSpecializationKind() == 
    1158                 :           TSK_ExplicitInstantiationDeclaration &&
    1159                 :         TSK == TSK_ExplicitInstantiationDefinition) {
    1160                 :       // An explicit instantiation definition follows an explicit instantiation
    1161                 :       // declaration (C++0x [temp.explicit]p10); go ahead and perform the
    1162                 :       // explicit instantiation.
    1163                0:       ClassTemplateSpec->setSpecializationKind(TSK);
    1164                0:       return false;
    1165                 :     }
    1166                 :     
    1167                 :     // We can only instantiate something that hasn't already been
    1168                 :     // instantiated or specialized. Fail without any diagnostics: our
    1169                 :     // caller will provide an error message.    
    1170                0:     return true;
    1171                 :   }
    1172                 : 
                        0: branch 1 not taken
                     1401: branch 2 taken
    1173             1401:   if (ClassTemplateSpec->isInvalidDecl())
    1174                0:     return true;
    1175                 :   
    1176             1401:   ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate();
    1177             1401:   CXXRecordDecl *Pattern = 0;
    1178                 : 
    1179                 :   // C++ [temp.class.spec.match]p1:
    1180                 :   //   When a class template is used in a context that requires an
    1181                 :   //   instantiation of the class, it is necessary to determine
    1182                 :   //   whether the instantiation is to be generated using the primary
    1183                 :   //   template or one of the partial specializations. This is done by
    1184                 :   //   matching the template arguments of the class template
    1185                 :   //   specialization with the template argument lists of the partial
    1186                 :   //   specializations.
    1187                 :   typedef std::pair<ClassTemplatePartialSpecializationDecl *,
    1188                 :                     TemplateArgumentList *> MatchResult;
    1189             1401:   llvm::SmallVector<MatchResult, 4> Matched;
                      898: branch 3 taken
                     1401: branch 4 taken
    1190             2299:   for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator
    1191             1401:          Partial = Template->getPartialSpecializations().begin(),
    1192             1401:          PartialEnd = Template->getPartialSpecializations().end();
    1193                 :        Partial != PartialEnd;
    1194                 :        ++Partial) {
    1195              898:     TemplateDeductionInfo Info(Context, PointOfInstantiation);
                      241: branch 0 taken
                      657: branch 1 taken
    1196              898:     if (TemplateDeductionResult Result
    1197                 :           = DeduceTemplateArguments(&*Partial,
    1198                 :                                     ClassTemplateSpec->getTemplateArgs(),
    1199              898:                                     Info)) {
    1200                 :       // FIXME: Store the failed-deduction information for use in
    1201                 :       // diagnostics, later.
    1202                 :       (void)Result;
    1203                 :     } else {
    1204              241:       Matched.push_back(std::make_pair(&*Partial, Info.take()));
    1205                 :     }
    1206                 :   }
    1207                 : 
                      230: branch 1 taken
                     1171: branch 2 taken
    1208             1401:   if (Matched.size() >= 1) {
    1209              230:     llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin();
                        9: branch 1 taken
                      221: branch 2 taken
    1210              230:     if (Matched.size() == 1) {
    1211                 :       //   -- If exactly one matching specialization is found, the
    1212                 :       //      instantiation is generated from that specialization.
    1213                 :       // We don't need to do anything for this.
    1214                 :     } else {
    1215                 :       //   -- If more than one matching specialization is found, the
    1216                 :       //      partial order rules (14.5.4.2) are used to determine
    1217                 :       //      whether one of the specializations is more specialized
    1218                 :       //      than the others. If none of the specializations is more
    1219                 :       //      specialized than all of the other matching
    1220                 :       //      specializations, then the use of the class template is
    1221                 :       //      ambiguous and the program is ill-formed.
                       11: branch 0 taken
                        9: branch 1 taken
    1222               29:       for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1,
    1223                9:                                                     PEnd = Matched.end();
    1224                 :            P != PEnd; ++P) {
                        6: branch 1 taken
                        5: branch 2 taken
    1225               11:         if (getMoreSpecializedPartialSpecialization(P->first, Best->first,
    1226                 :                                                     PointOfInstantiation) 
    1227                 :               == P->first)
    1228                6:           Best = P;
    1229                 :       }
    1230                 :       
    1231                 :       // Determine if the best partial specialization is more specialized than
    1232                 :       // the others.
    1233                9:       bool Ambiguous = false;
                       20: branch 1 taken
                        8: branch 2 taken
    1234               37:       for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
    1235                9:                                                     PEnd = Matched.end();
    1236                 :            P != PEnd; ++P) {
                       11: branch 0 taken
                        9: branch 1 taken
                        1: branch 3 taken
                       10: branch 4 taken
                        1: branch 5 taken
                       19: branch 6 taken
    1237               20:         if (P != Best &&
    1238                 :             getMoreSpecializedPartialSpecialization(P->first, Best->first,
    1239                 :                                                     PointOfInstantiation)
    1240                 :               != Best->first) {
    1241                1:           Ambiguous = true;
    1242                1:           break;
    1243                 :         }
    1244                 :       }
    1245                 :        
                        1: branch 0 taken
                        8: branch 1 taken
    1246                9:       if (Ambiguous) {
    1247                 :         // Partial ordering did not produce a clear winner. Complain.
    1248                1:         ClassTemplateSpec->setInvalidDecl();
    1249                 :         Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous)
    1250                1:           << ClassTemplateSpec;
    1251                 :         
    1252                 :         // Print the matching partial specializations.
                        2: branch 1 taken
                        1: branch 2 taken
    1253                4:         for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(),
    1254                1:                                                       PEnd = Matched.end();
    1255                 :              P != PEnd; ++P)
    1256                 :           Diag(P->first->getLocation(), diag::note_partial_spec_match)
    1257                 :             << getTemplateArgumentBindingsText(P->first->getTemplateParameters(),
    1258                2:                                                *P->second);
    1259                 : 
    1260                1:         return true;
    1261                 :       }
    1262                 :     }
    1263                 :     
    1264                 :     // Instantiate using the best class template partial specialization.
    1265              229:     ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->first;
                        5: branch 1 taken
                      228: branch 2 taken
    1266              462:     while (OrigPartialSpec->getInstantiatedFromMember()) {
    1267                 :       // If we've found an explicit specialization of this class template,
    1268                 :       // stop here and use that as the pattern.
                        1: branch 1 taken
                        4: branch 2 taken
    1269                5:       if (OrigPartialSpec->isMemberSpecialization())
    1270                1:         break;
    1271                 :       
    1272                4:       OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember();
    1273                 :     }
    1274                 :     
    1275              229:     Pattern = OrigPartialSpec;
    1276              229:     ClassTemplateSpec->setInstantiationOf(Best->first, Best->second);
    1277                 :   } else {
    1278                 :     //   -- If no matches are found, the instantiation is generated
    1279                 :     //      from the primary template.
    1280             1171:     ClassTemplateDecl *OrigTemplate = Template;
                       36: branch 1 taken
                     1169: branch 2 taken
    1281             2376:     while (OrigTemplate->getInstantiatedFromMemberTemplate()) {
    1282                 :       // If we've found an explicit specialization of this class template,
    1283                 :       // stop here and use that as the pattern.
                        2: branch 1 taken
                       34: branch 2 taken
    1284               36:       if (OrigTemplate->isMemberSpecialization())
    1285                2:         break;
    1286                 :       
    1287               34:       OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate();
    1288                 :     }
    1289                 :     
    1290             1171:     Pattern = OrigTemplate->getTemplatedDecl();
    1291                 :   }
    1292                 : 
    1293                 :   bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec, 
    1294                 :                                  Pattern,
    1295                 :                                 getTemplateInstantiationArgs(ClassTemplateSpec),
    1296                 :                                  TSK,
    1297             1400:                                  Complain);
    1298                 : 
                      239: branch 1 taken
                     1400: branch 2 taken
    1299             1400:   for (unsigned I = 0, N = Matched.size(); I != N; ++I) {
    1300                 :     // FIXME: Implement TemplateArgumentList::Destroy!
    1301                 :     //    if (Matched[I].first != Pattern)
    1302                 :     //      Matched[I].second->Destroy(Context);
    1303                 :   }
    1304                 : 
    1305             1400:   return Result;
    1306                 : }
    1307                 : 
    1308                 : /// \brief Instantiates the definitions of all of the member
    1309                 : /// of the given class, which is an instantiation of a class template
    1310                 : /// or a member class of a template.
    1311                 : void
    1312                 : Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
    1313                 :                               CXXRecordDecl *Instantiation,
    1314                 :                         const MultiLevelTemplateArgumentList &TemplateArgs,
    1315              251:                               TemplateSpecializationKind TSK) {
                     1553: branch 3 taken
                      251: branch 4 taken
    1316             2055:   for (DeclContext::decl_iterator D = Instantiation->decls_begin(),
    1317              251:                                DEnd = Instantiation->decls_end();
    1318                 :        D != DEnd; ++D) {
    1319             1553:     bool SuppressNew = false;
                     1198: branch 2 taken
                      355: branch 3 taken
    1320             1553:     if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) {
                      212: branch 0 taken
                      986: branch 1 taken
    1321             1198:       if (FunctionDecl *Pattern
    1322             1198:             = Function->getInstantiatedFromMemberFunction()) {
    1323                 :         MemberSpecializationInfo *MSInfo 
    1324              212:           = Function->getMemberSpecializationInfo();
                        0: branch 0 not taken
                      212: branch 1 taken
    1325              212:         assert(MSInfo && "No member specialization information?");
                      212: branch 3 taken
                        0: branch 4 not taken
                        1: branch 5 taken
                      211: branch 6 taken
                        1: branch 7 taken
                      211: branch 8 taken
    1326              212:         if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, 
    1327                 :                                                    Function, 
    1328                 :                                         MSInfo->getTemplateSpecializationKind(),
    1329                 :                                               MSInfo->getPointOfInstantiation(), 
    1330                 :                                                    SuppressNew) ||
    1331                 :             SuppressNew)
    1332                1:           continue;
    1333                 :         
                        0: branch 1 not taken
                      211: branch 2 taken
    1334              211:         if (Function->getBody())
    1335                0:           continue;
    1336                 : 
                      194: branch 0 taken
                       17: branch 1 taken
    1337              211:         if (TSK == TSK_ExplicitInstantiationDefinition) {
    1338                 :           // C++0x [temp.explicit]p8:
    1339                 :           //   An explicit instantiation definition that names a class template
    1340                 :           //   specialization explicitly instantiates the class template 
    1341                 :           //   specialization and is only an explicit instantiation definition 
    1342                 :           //   of members whose definition is visible at the point of 
    1343                 :           //   instantiation.
                       12: branch 1 taken
                      182: branch 2 taken
    1344              194:           if (!Pattern->getBody())
    1345               12:             continue;
    1346                 :         
    1347              182:           Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
    1348                 :                       
    1349              182:           InstantiateFunctionDefinition(PointOfInstantiation, Function);
    1350                 :         } else {
    1351               17:           Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
    1352                 :         }
    1353                 :       }
                       24: branch 2 taken
                      331: branch 3 taken
    1354              355:     } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) {
                       24: branch 1 taken
                        0: branch 2 not taken
    1355               24:       if (Var->isStaticDataMember()) {
    1356               24:         MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo();
                        0: branch 0 not taken
                       24: branch 1 taken
    1357               24:         assert(MSInfo && "No member specialization information?");
                       24: branch 3 taken
                        0: branch 4 not taken
                        1: branch 5 taken
                       23: branch 6 taken
                        1: branch 7 taken
                       23: branch 8 taken
    1358               24:         if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, 
    1359                 :                                                    Var, 
    1360                 :                                         MSInfo->getTemplateSpecializationKind(),
    1361                 :                                               MSInfo->getPointOfInstantiation(), 
    1362                 :                                                    SuppressNew) ||
    1363                 :             SuppressNew)
    1364                1:           continue;
    1365                 :         
                       20: branch 0 taken
                        3: branch 1 taken
    1366               23:         if (TSK == TSK_ExplicitInstantiationDefinition) {
    1367                 :           // C++0x [temp.explicit]p8:
    1368                 :           //   An explicit instantiation definition that names a class template
    1369                 :           //   specialization explicitly instantiates the class template 
    1370                 :           //   specialization and is only an explicit instantiation definition 
    1371                 :           //   of members whose definition is visible at the point of 
    1372                 :           //   instantiation.
                       15: branch 2 taken
                        5: branch 3 taken
    1373               20:           if (!Var->getInstantiatedFromStaticDataMember()
    1374                 :                                                      ->getOutOfLineDefinition())
    1375               15:             continue;
    1376                 :           
    1377                5:           Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
    1378                5:           InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var);
    1379                 :         } else {
    1380                3:           Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
    1381                 :         }
    1382                 :       }      
                      276: branch 2 taken
                       55: branch 3 taken
    1383              331:     } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) {
                      251: branch 1 taken
                       25: branch 2 taken
    1384              276:       if (Record->isInjectedClassName())
    1385              251:         continue;
    1386                 :       
    1387               25:       MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo();
                        0: branch 0 not taken
                       25: branch 1 taken
    1388               25:       assert(MSInfo && "No member specialization information?");
                       25: branch 3 taken
                        0: branch 4 not taken
                        1: branch 5 taken
                       24: branch 6 taken
                        1: branch 7 taken
                       24: branch 8 taken
    1389               25:       if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, 
    1390                 :                                                  Record, 
    1391                 :                                         MSInfo->getTemplateSpecializationKind(),
    1392                 :                                               MSInfo->getPointOfInstantiation(), 
    1393                 :                                                  SuppressNew) ||
    1394                 :           SuppressNew)
    1395                1:         continue;
    1396                 :       
    1397               24:       CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
                        0: branch 0 not taken
                       24: branch 1 taken
    1398               24:       assert(Pattern && "Missing instantiated-from-template information");
    1399                 :       
                       21: branch 1 taken
                        3: branch 2 taken
    1400               24:       if (!Record->getDefinition(Context)) {
                        2: branch 1 taken
                       19: branch 2 taken
    1401               21:         if (!Pattern->getDefinition(Context)) {
    1402                 :           // C++0x [temp.explicit]p8:
    1403                 :           //   An explicit instantiation definition that names a class template
    1404                 :           //   specialization explicitly instantiates the class template 
    1405                 :           //   specialization and is only an explicit instantiation definition 
    1406                 :           //   of members whose definition is visible at the point of 
    1407                 :           //   instantiation.
                        0: branch 0 not taken
                        2: branch 1 taken
    1408                2:           if (TSK == TSK_ExplicitInstantiationDeclaration) {
    1409                0:             MSInfo->setTemplateSpecializationKind(TSK);
    1410                0:             MSInfo->setPointOfInstantiation(PointOfInstantiation);
    1411                 :           }
    1412                 :           
    1413                2:           continue;
    1414                 :         }
    1415                 :         
    1416                 :         InstantiateClass(PointOfInstantiation, Record, Pattern,
    1417                 :                          TemplateArgs,
    1418               19:                          TSK);
    1419                 :       }
    1420                 :       
    1421               22:       Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition(Context));
                       22: branch 0 taken
                        0: branch 1 not taken
    1422               22:       if (Pattern)
    1423                 :         InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs, 
    1424               22:                                 TSK);
    1425                 :     }
    1426                 :   }
    1427              251: }
    1428                 : 
    1429                 : /// \brief Instantiate the definitions of all of the members of the
    1430                 : /// given class template specialization, which was named as part of an
    1431                 : /// explicit instantiation.
    1432                 : void
    1433                 : Sema::InstantiateClassTemplateSpecializationMembers(
    1434                 :                                            SourceLocation PointOfInstantiation,
    1435                 :                             ClassTemplateSpecializationDecl *ClassTemplateSpec,
    1436              221:                                                TemplateSpecializationKind TSK) {
    1437                 :   // C++0x [temp.explicit]p7:
    1438                 :   //   An explicit instantiation that names a class template
    1439                 :   //   specialization is an explicit instantion of the same kind
    1440                 :   //   (declaration or definition) of each of its members (not
    1441                 :   //   including members inherited from base classes) that has not
    1442                 :   //   been previously explicitly specialized in the translation unit
    1443                 :   //   containing the explicit instantiation, except as described
    1444                 :   //   below.
    1445                 :   InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec,
    1446                 :                           getTemplateInstantiationArgs(ClassTemplateSpec),
    1447              221:                           TSK);
    1448              221: }
    1449                 : 
    1450                 : Sema::OwningStmtResult
    1451              549: Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) {
                        0: branch 0 not taken
                      549: branch 1 taken
    1452              549:   if (!S)
    1453                0:     return Owned(S);
    1454                 : 
    1455                 :   TemplateInstantiator Instantiator(*this, TemplateArgs,
    1456                 :                                     SourceLocation(),
    1457              549:                                     DeclarationName());
    1458              549:   return Instantiator.TransformStmt(S);
    1459                 : }
    1460                 : 
    1461                 : Sema::OwningExprResult
    1462              803: Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) {
                        0: branch 0 not taken
                      803: branch 1 taken
    1463              803:   if (!E)
    1464                0:     return Owned(E);
    1465                 : 
    1466                 :   TemplateInstantiator Instantiator(*this, TemplateArgs,
    1467                 :                                     SourceLocation(),
    1468              803:                                     DeclarationName());
    1469              803:   return Instantiator.TransformExpr(E);
    1470                 : }
    1471                 : 
    1472                 : /// \brief Do template substitution on a nested-name-specifier.
    1473                 : NestedNameSpecifier *
    1474                 : Sema::SubstNestedNameSpecifier(NestedNameSpecifier *NNS,
    1475                 :                                SourceRange Range,
    1476               16:                          const MultiLevelTemplateArgumentList &TemplateArgs) {
    1477                 :   TemplateInstantiator Instantiator(*this, TemplateArgs, Range.getBegin(),
    1478               16:                                     DeclarationName());
    1479               16:   return Instantiator.TransformNestedNameSpecifier(NNS, Range);
    1480                 : }
    1481                 : 
    1482                 : TemplateName
    1483                 : Sema::SubstTemplateName(TemplateName Name, SourceLocation Loc,
    1484                5:                         const MultiLevelTemplateArgumentList &TemplateArgs) {
    1485                 :   TemplateInstantiator Instantiator(*this, TemplateArgs, Loc,
    1486                5:                                     DeclarationName());
    1487                5:   return Instantiator.TransformTemplateName(Name);
    1488                 : }
    1489                 : 
    1490                 : bool Sema::Subst(const TemplateArgumentLoc &Input, TemplateArgumentLoc &Output,
    1491              454:                  const MultiLevelTemplateArgumentList &TemplateArgs) {
    1492                 :   TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(),
    1493              454:                                     DeclarationName());
    1494                 : 
    1495              454:   return Instantiator.TransformTemplateArgument(Input, Output);
    1496                 : }

Generated: 2010-02-10 01:31 by zcov