clang API Documentation
00001 //===------- SemaTemplate.h - C++ Templates ---------------------*- C++ -*-===/ 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 //===----------------------------------------------------------------------===/ 00008 // 00009 // This file provides types used in the semantic analysis of C++ templates. 00010 // 00011 //===----------------------------------------------------------------------===/ 00012 #ifndef LLVM_CLANG_SEMA_TEMPLATE_H 00013 #define LLVM_CLANG_SEMA_TEMPLATE_H 00014 00015 #include "clang/AST/DeclTemplate.h" 00016 #include "clang/AST/DeclVisitor.h" 00017 #include "llvm/ADT/SmallVector.h" 00018 #include <cassert> 00019 #include <utility> 00020 00021 namespace clang { 00022 /// \brief Data structure that captures multiple levels of template argument 00023 /// lists for use in template instantiation. 00024 /// 00025 /// Multiple levels of template arguments occur when instantiating the 00026 /// definitions of member templates. For example: 00027 /// 00028 /// \code 00029 /// template<typename T> 00030 /// struct X { 00031 /// template<T Value> 00032 /// struct Y { 00033 /// void f(); 00034 /// }; 00035 /// }; 00036 /// \endcode 00037 /// 00038 /// When instantiating X<int>::Y<17>::f, the multi-level template argument 00039 /// list will contain a template argument list (int) at depth 0 and a 00040 /// template argument list (17) at depth 1. 00041 class MultiLevelTemplateArgumentList { 00042 public: 00043 typedef std::pair<const TemplateArgument *, unsigned> ArgList; 00044 00045 private: 00046 /// \brief The template argument lists, stored from the innermost template 00047 /// argument list (first) to the outermost template argument list (last). 00048 SmallVector<ArgList, 4> TemplateArgumentLists; 00049 00050 public: 00051 /// \brief Construct an empty set of template argument lists. 00052 MultiLevelTemplateArgumentList() { } 00053 00054 /// \brief Construct a single-level template argument list. 00055 explicit 00056 MultiLevelTemplateArgumentList(const TemplateArgumentList &TemplateArgs) { 00057 addOuterTemplateArguments(&TemplateArgs); 00058 } 00059 00060 /// \brief Determine the number of levels in this template argument 00061 /// list. 00062 unsigned getNumLevels() const { return TemplateArgumentLists.size(); } 00063 00064 /// \brief Retrieve the template argument at a given depth and index. 00065 const TemplateArgument &operator()(unsigned Depth, unsigned Index) const { 00066 assert(Depth < TemplateArgumentLists.size()); 00067 assert(Index < TemplateArgumentLists[getNumLevels() - Depth - 1].second); 00068 return TemplateArgumentLists[getNumLevels() - Depth - 1].first[Index]; 00069 } 00070 00071 /// \brief Determine whether there is a non-NULL template argument at the 00072 /// given depth and index. 00073 /// 00074 /// There must exist a template argument list at the given depth. 00075 bool hasTemplateArgument(unsigned Depth, unsigned Index) const { 00076 assert(Depth < TemplateArgumentLists.size()); 00077 00078 if (Index >= TemplateArgumentLists[getNumLevels() - Depth - 1].second) 00079 return false; 00080 00081 return !(*this)(Depth, Index).isNull(); 00082 } 00083 00084 /// \brief Clear out a specific template argument. 00085 void setArgument(unsigned Depth, unsigned Index, 00086 TemplateArgument Arg) { 00087 assert(Depth < TemplateArgumentLists.size()); 00088 assert(Index < TemplateArgumentLists[getNumLevels() - Depth - 1].second); 00089 const_cast<TemplateArgument&>( 00090 TemplateArgumentLists[getNumLevels() - Depth - 1].first[Index]) 00091 = Arg; 00092 } 00093 00094 /// \brief Add a new outermost level to the multi-level template argument 00095 /// list. 00096 void addOuterTemplateArguments(const TemplateArgumentList *TemplateArgs) { 00097 TemplateArgumentLists.push_back(ArgList(TemplateArgs->data(), 00098 TemplateArgs->size())); 00099 } 00100 00101 /// \brief Add a new outmost level to the multi-level template argument 00102 /// list. 00103 void addOuterTemplateArguments(const TemplateArgument *Args, 00104 unsigned NumArgs) { 00105 TemplateArgumentLists.push_back(ArgList(Args, NumArgs)); 00106 } 00107 00108 /// \brief Retrieve the innermost template argument list. 00109 const ArgList &getInnermost() const { 00110 return TemplateArgumentLists.front(); 00111 } 00112 }; 00113 00114 /// \brief The context in which partial ordering of function templates occurs. 00115 enum TPOC { 00116 /// \brief Partial ordering of function templates for a function call. 00117 TPOC_Call, 00118 /// \brief Partial ordering of function templates for a call to a 00119 /// conversion function. 00120 TPOC_Conversion, 00121 /// \brief Partial ordering of function templates in other contexts, e.g., 00122 /// taking the address of a function template or matching a function 00123 /// template specialization to a function template. 00124 TPOC_Other 00125 }; 00126 00127 // This is lame but unavoidable in a world without forward 00128 // declarations of enums. The alternatives are to either pollute 00129 // Sema.h (by including this file) or sacrifice type safety (by 00130 // making Sema.h declare things as enums). 00131 class TemplatePartialOrderingContext { 00132 TPOC Value; 00133 public: 00134 TemplatePartialOrderingContext(TPOC Value) : Value(Value) {} 00135 operator TPOC() const { return Value; } 00136 }; 00137 00138 /// \brief Captures a template argument whose value has been deduced 00139 /// via c++ template argument deduction. 00140 class DeducedTemplateArgument : public TemplateArgument { 00141 /// \brief For a non-type template argument, whether the value was 00142 /// deduced from an array bound. 00143 bool DeducedFromArrayBound; 00144 00145 public: 00146 DeducedTemplateArgument() 00147 : TemplateArgument(), DeducedFromArrayBound(false) { } 00148 00149 DeducedTemplateArgument(const TemplateArgument &Arg, 00150 bool DeducedFromArrayBound = false) 00151 : TemplateArgument(Arg), DeducedFromArrayBound(DeducedFromArrayBound) { } 00152 00153 /// \brief Construct an integral non-type template argument that 00154 /// has been deduced, possibly from an array bound. 00155 DeducedTemplateArgument(const llvm::APSInt &Value, 00156 QualType ValueType, 00157 bool DeducedFromArrayBound) 00158 : TemplateArgument(Value, ValueType), 00159 DeducedFromArrayBound(DeducedFromArrayBound) { } 00160 00161 /// \brief For a non-type template argument, determine whether the 00162 /// template argument was deduced from an array bound. 00163 bool wasDeducedFromArrayBound() const { return DeducedFromArrayBound; } 00164 00165 /// \brief Specify whether the given non-type template argument 00166 /// was deduced from an array bound. 00167 void setDeducedFromArrayBound(bool Deduced) { 00168 DeducedFromArrayBound = Deduced; 00169 } 00170 }; 00171 00172 /// \brief A stack-allocated class that identifies which local 00173 /// variable declaration instantiations are present in this scope. 00174 /// 00175 /// A new instance of this class type will be created whenever we 00176 /// instantiate a new function declaration, which will have its own 00177 /// set of parameter declarations. 00178 class LocalInstantiationScope { 00179 public: 00180 /// \brief A set of declarations. 00181 typedef SmallVector<Decl *, 4> DeclArgumentPack; 00182 00183 private: 00184 /// \brief Reference to the semantic analysis that is performing 00185 /// this template instantiation. 00186 Sema &SemaRef; 00187 00188 typedef llvm::DenseMap<const Decl *, 00189 llvm::PointerUnion<Decl *, DeclArgumentPack *> > 00190 LocalDeclsMap; 00191 00192 /// \brief A mapping from local declarations that occur 00193 /// within a template to their instantiations. 00194 /// 00195 /// This mapping is used during instantiation to keep track of, 00196 /// e.g., function parameter and variable declarations. For example, 00197 /// given: 00198 /// 00199 /// \code 00200 /// template<typename T> T add(T x, T y) { return x + y; } 00201 /// \endcode 00202 /// 00203 /// when we instantiate add<int>, we will introduce a mapping from 00204 /// the ParmVarDecl for 'x' that occurs in the template to the 00205 /// instantiated ParmVarDecl for 'x'. 00206 /// 00207 /// For a parameter pack, the local instantiation scope may contain a 00208 /// set of instantiated parameters. This is stored as a DeclArgumentPack 00209 /// pointer. 00210 LocalDeclsMap LocalDecls; 00211 00212 /// \brief The set of argument packs we've allocated. 00213 SmallVector<DeclArgumentPack *, 1> ArgumentPacks; 00214 00215 /// \brief The outer scope, which contains local variable 00216 /// definitions from some other instantiation (that may not be 00217 /// relevant to this particular scope). 00218 LocalInstantiationScope *Outer; 00219 00220 /// \brief Whether we have already exited this scope. 00221 bool Exited; 00222 00223 /// \brief Whether to combine this scope with the outer scope, such that 00224 /// lookup will search our outer scope. 00225 bool CombineWithOuterScope; 00226 00227 /// \brief If non-NULL, the template parameter pack that has been 00228 /// partially substituted per C++0x [temp.arg.explicit]p9. 00229 NamedDecl *PartiallySubstitutedPack; 00230 00231 /// \brief If \c PartiallySubstitutedPack is non-null, the set of 00232 /// explicitly-specified template arguments in that pack. 00233 const TemplateArgument *ArgsInPartiallySubstitutedPack; 00234 00235 /// \brief If \c PartiallySubstitutedPack, the number of 00236 /// explicitly-specified template arguments in 00237 /// ArgsInPartiallySubstitutedPack. 00238 unsigned NumArgsInPartiallySubstitutedPack; 00239 00240 // This class is non-copyable 00241 LocalInstantiationScope(const LocalInstantiationScope &); 00242 LocalInstantiationScope &operator=(const LocalInstantiationScope &); 00243 00244 public: 00245 LocalInstantiationScope(Sema &SemaRef, bool CombineWithOuterScope = false) 00246 : SemaRef(SemaRef), Outer(SemaRef.CurrentInstantiationScope), 00247 Exited(false), CombineWithOuterScope(CombineWithOuterScope), 00248 PartiallySubstitutedPack(0) 00249 { 00250 SemaRef.CurrentInstantiationScope = this; 00251 } 00252 00253 ~LocalInstantiationScope() { 00254 Exit(); 00255 } 00256 00257 const Sema &getSema() const { return SemaRef; } 00258 00259 /// \brief Exit this local instantiation scope early. 00260 void Exit() { 00261 if (Exited) 00262 return; 00263 00264 for (unsigned I = 0, N = ArgumentPacks.size(); I != N; ++I) 00265 delete ArgumentPacks[I]; 00266 00267 SemaRef.CurrentInstantiationScope = Outer; 00268 Exited = true; 00269 } 00270 00271 /// \brief Clone this scope, and all outer scopes, down to the given 00272 /// outermost scope. 00273 LocalInstantiationScope *cloneScopes(LocalInstantiationScope *Outermost) { 00274 if (this == Outermost) return this; 00275 LocalInstantiationScope *newScope = 00276 new LocalInstantiationScope(SemaRef, CombineWithOuterScope); 00277 00278 newScope->Outer = 0; 00279 if (Outer) 00280 newScope->Outer = Outer->cloneScopes(Outermost); 00281 00282 newScope->PartiallySubstitutedPack = PartiallySubstitutedPack; 00283 newScope->ArgsInPartiallySubstitutedPack = ArgsInPartiallySubstitutedPack; 00284 newScope->NumArgsInPartiallySubstitutedPack = 00285 NumArgsInPartiallySubstitutedPack; 00286 00287 for (LocalDeclsMap::iterator I = LocalDecls.begin(), E = LocalDecls.end(); 00288 I != E; ++I) { 00289 const Decl *D = I->first; 00290 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = 00291 newScope->LocalDecls[D]; 00292 if (I->second.is<Decl *>()) { 00293 Stored = I->second.get<Decl *>(); 00294 } else { 00295 DeclArgumentPack *OldPack = I->second.get<DeclArgumentPack *>(); 00296 DeclArgumentPack *NewPack = new DeclArgumentPack(*OldPack); 00297 Stored = NewPack; 00298 newScope->ArgumentPacks.push_back(NewPack); 00299 } 00300 } 00301 return newScope; 00302 } 00303 00304 /// \brief deletes the given scope, and all otuer scopes, down to the 00305 /// given outermost scope. 00306 static void deleteScopes(LocalInstantiationScope *Scope, 00307 LocalInstantiationScope *Outermost) { 00308 while (Scope && Scope != Outermost) { 00309 LocalInstantiationScope *Out = Scope->Outer; 00310 delete Scope; 00311 Scope = Out; 00312 } 00313 } 00314 00315 /// \brief Find the instantiation of the declaration D within the current 00316 /// instantiation scope. 00317 /// 00318 /// \param D The declaration whose instantiation we are searching for. 00319 /// 00320 /// \returns A pointer to the declaration or argument pack of declarations 00321 /// to which the declaration \c D is instantiataed, if found. Otherwise, 00322 /// returns NULL. 00323 llvm::PointerUnion<Decl *, DeclArgumentPack *> * 00324 findInstantiationOf(const Decl *D); 00325 00326 void InstantiatedLocal(const Decl *D, Decl *Inst); 00327 void InstantiatedLocalPackArg(const Decl *D, Decl *Inst); 00328 void MakeInstantiatedLocalArgPack(const Decl *D); 00329 00330 /// \brief Note that the given parameter pack has been partially substituted 00331 /// via explicit specification of template arguments 00332 /// (C++0x [temp.arg.explicit]p9). 00333 /// 00334 /// \param Pack The parameter pack, which will always be a template 00335 /// parameter pack. 00336 /// 00337 /// \param ExplicitArgs The explicitly-specified template arguments provided 00338 /// for this parameter pack. 00339 /// 00340 /// \param NumExplicitArgs The number of explicitly-specified template 00341 /// arguments provided for this parameter pack. 00342 void SetPartiallySubstitutedPack(NamedDecl *Pack, 00343 const TemplateArgument *ExplicitArgs, 00344 unsigned NumExplicitArgs); 00345 00346 /// \brief Retrieve the partially-substitued template parameter pack. 00347 /// 00348 /// If there is no partially-substituted parameter pack, returns NULL. 00349 NamedDecl *getPartiallySubstitutedPack( 00350 const TemplateArgument **ExplicitArgs = 0, 00351 unsigned *NumExplicitArgs = 0) const; 00352 }; 00353 00354 class TemplateDeclInstantiator 00355 : public DeclVisitor<TemplateDeclInstantiator, Decl *> 00356 { 00357 Sema &SemaRef; 00358 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex; 00359 DeclContext *Owner; 00360 const MultiLevelTemplateArgumentList &TemplateArgs; 00361 Sema::LateInstantiatedAttrVec* LateAttrs; 00362 LocalInstantiationScope *StartingScope; 00363 00364 /// \brief A list of out-of-line class template partial 00365 /// specializations that will need to be instantiated after the 00366 /// enclosing class's instantiation is complete. 00367 SmallVector<std::pair<ClassTemplateDecl *, 00368 ClassTemplatePartialSpecializationDecl *>, 4> 00369 OutOfLinePartialSpecs; 00370 00371 public: 00372 TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner, 00373 const MultiLevelTemplateArgumentList &TemplateArgs) 00374 : SemaRef(SemaRef), SubstIndex(SemaRef, -1), Owner(Owner), 00375 TemplateArgs(TemplateArgs), LateAttrs(0), StartingScope(0) { } 00376 00377 // FIXME: Once we get closer to completion, replace these manually-written 00378 // declarations with automatically-generated ones from 00379 // clang/AST/DeclNodes.inc. 00380 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D); 00381 Decl *VisitLabelDecl(LabelDecl *D); 00382 Decl *VisitNamespaceDecl(NamespaceDecl *D); 00383 Decl *VisitNamespaceAliasDecl(NamespaceAliasDecl *D); 00384 Decl *VisitTypedefDecl(TypedefDecl *D); 00385 Decl *VisitTypeAliasDecl(TypeAliasDecl *D); 00386 Decl *VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D); 00387 Decl *VisitVarDecl(VarDecl *D); 00388 Decl *VisitAccessSpecDecl(AccessSpecDecl *D); 00389 Decl *VisitFieldDecl(FieldDecl *D); 00390 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D); 00391 Decl *VisitStaticAssertDecl(StaticAssertDecl *D); 00392 Decl *VisitEnumDecl(EnumDecl *D); 00393 Decl *VisitEnumConstantDecl(EnumConstantDecl *D); 00394 Decl *VisitFriendDecl(FriendDecl *D); 00395 Decl *VisitFunctionDecl(FunctionDecl *D, 00396 TemplateParameterList *TemplateParams = 0); 00397 Decl *VisitCXXRecordDecl(CXXRecordDecl *D); 00398 Decl *VisitCXXMethodDecl(CXXMethodDecl *D, 00399 TemplateParameterList *TemplateParams = 0, 00400 bool IsClassScopeSpecialization = false); 00401 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D); 00402 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D); 00403 Decl *VisitCXXConversionDecl(CXXConversionDecl *D); 00404 ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D); 00405 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D); 00406 Decl *VisitClassTemplatePartialSpecializationDecl( 00407 ClassTemplatePartialSpecializationDecl *D); 00408 Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D); 00409 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); 00410 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); 00411 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); 00412 Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D); 00413 Decl *VisitUsingDecl(UsingDecl *D); 00414 Decl *VisitUsingShadowDecl(UsingShadowDecl *D); 00415 Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D); 00416 Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D); 00417 Decl *VisitClassScopeFunctionSpecializationDecl( 00418 ClassScopeFunctionSpecializationDecl *D); 00419 00420 // Base case. FIXME: Remove once we can instantiate everything. 00421 Decl *VisitDecl(Decl *D) { 00422 unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID( 00423 DiagnosticsEngine::Error, 00424 "cannot instantiate %0 yet"); 00425 SemaRef.Diag(D->getLocation(), DiagID) 00426 << D->getDeclKindName(); 00427 00428 return 0; 00429 } 00430 00431 // Enable late instantiation of attributes. Late instantiated attributes 00432 // will be stored in LA. 00433 void enableLateAttributeInstantiation(Sema::LateInstantiatedAttrVec *LA) { 00434 LateAttrs = LA; 00435 StartingScope = SemaRef.CurrentInstantiationScope; 00436 } 00437 00438 // Disable late instantiation of attributes. 00439 void disableLateAttributeInstantiation() { 00440 LateAttrs = 0; 00441 StartingScope = 0; 00442 } 00443 00444 LocalInstantiationScope *getStartingScope() const { return StartingScope; } 00445 00446 typedef 00447 SmallVectorImpl<std::pair<ClassTemplateDecl *, 00448 ClassTemplatePartialSpecializationDecl *> > 00449 ::iterator 00450 delayed_partial_spec_iterator; 00451 00452 /// \brief Return an iterator to the beginning of the set of 00453 /// "delayed" partial specializations, which must be passed to 00454 /// InstantiateClassTemplatePartialSpecialization once the class 00455 /// definition has been completed. 00456 delayed_partial_spec_iterator delayed_partial_spec_begin() { 00457 return OutOfLinePartialSpecs.begin(); 00458 } 00459 00460 /// \brief Return an iterator to the end of the set of 00461 /// "delayed" partial specializations, which must be passed to 00462 /// InstantiateClassTemplatePartialSpecialization once the class 00463 /// definition has been completed. 00464 delayed_partial_spec_iterator delayed_partial_spec_end() { 00465 return OutOfLinePartialSpecs.end(); 00466 } 00467 00468 // Helper functions for instantiating methods. 00469 TypeSourceInfo *SubstFunctionType(FunctionDecl *D, 00470 SmallVectorImpl<ParmVarDecl *> &Params); 00471 bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl); 00472 bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl); 00473 00474 TemplateParameterList * 00475 SubstTemplateParams(TemplateParameterList *List); 00476 00477 bool SubstQualifier(const DeclaratorDecl *OldDecl, 00478 DeclaratorDecl *NewDecl); 00479 bool SubstQualifier(const TagDecl *OldDecl, 00480 TagDecl *NewDecl); 00481 00482 Decl *InstantiateTypedefNameDecl(TypedefNameDecl *D, bool IsTypeAlias); 00483 ClassTemplatePartialSpecializationDecl * 00484 InstantiateClassTemplatePartialSpecialization( 00485 ClassTemplateDecl *ClassTemplate, 00486 ClassTemplatePartialSpecializationDecl *PartialSpec); 00487 void InstantiateEnumDefinition(EnumDecl *Enum, EnumDecl *Pattern); 00488 }; 00489 } 00490 00491 #endif // LLVM_CLANG_SEMA_TEMPLATE_H