ASTContext.h revision 6b96742ceedebb5eb8f1299ab67cb3ecd6d92aaf
1//===--- ASTContext.h - Context to hold long-lived AST nodes ----*- C++ -*-===// 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/// 10/// \file 11/// \brief Defines the clang::ASTContext interface. 12/// 13//===----------------------------------------------------------------------===// 14 15#ifndef LLVM_CLANG_AST_ASTCONTEXT_H 16#define LLVM_CLANG_AST_ASTCONTEXT_H 17 18#include "clang/AST/ASTTypeTraits.h" 19#include "clang/AST/CanonicalType.h" 20#include "clang/AST/CommentCommandTraits.h" 21#include "clang/AST/Decl.h" 22#include "clang/AST/LambdaMangleContext.h" 23#include "clang/AST/NestedNameSpecifier.h" 24#include "clang/AST/PrettyPrinter.h" 25#include "clang/AST/RawCommentList.h" 26#include "clang/AST/TemplateName.h" 27#include "clang/AST/Type.h" 28#include "clang/Basic/AddressSpaces.h" 29#include "clang/Basic/IdentifierTable.h" 30#include "clang/Basic/LangOptions.h" 31#include "clang/Basic/OperatorKinds.h" 32#include "clang/Basic/PartialDiagnostic.h" 33#include "clang/Basic/VersionTuple.h" 34#include "llvm/ADT/DenseMap.h" 35#include "llvm/ADT/FoldingSet.h" 36#include "llvm/ADT/IntrusiveRefCntPtr.h" 37#include "llvm/ADT/OwningPtr.h" 38#include "llvm/ADT/SmallPtrSet.h" 39#include "llvm/ADT/TinyPtrVector.h" 40#include "llvm/Support/Allocator.h" 41#include <vector> 42 43namespace llvm { 44 struct fltSemantics; 45} 46 47namespace clang { 48 class FileManager; 49 class AtomicExpr; 50 class ASTRecordLayout; 51 class BlockExpr; 52 class CharUnits; 53 class DiagnosticsEngine; 54 class Expr; 55 class ExternalASTSource; 56 class ASTMutationListener; 57 class IdentifierTable; 58 class MaterializeTemporaryExpr; 59 class SelectorTable; 60 class TargetInfo; 61 class CXXABI; 62 // Decls 63 class MangleContext; 64 class ObjCIvarDecl; 65 class ObjCPropertyDecl; 66 class UnresolvedSetIterator; 67 class UsingDecl; 68 class UsingShadowDecl; 69 70 namespace Builtin { class Context; } 71 72 namespace comments { 73 class FullComment; 74 } 75 76/// \brief Holds long-lived AST nodes (such as types and decls) that can be 77/// referred to throughout the semantic analysis of a file. 78class ASTContext : public RefCountedBase<ASTContext> { 79 ASTContext &this_() { return *this; } 80 81 mutable SmallVector<Type *, 0> Types; 82 mutable llvm::FoldingSet<ExtQuals> ExtQualNodes; 83 mutable llvm::FoldingSet<ComplexType> ComplexTypes; 84 mutable llvm::FoldingSet<PointerType> PointerTypes; 85 mutable llvm::FoldingSet<BlockPointerType> BlockPointerTypes; 86 mutable llvm::FoldingSet<LValueReferenceType> LValueReferenceTypes; 87 mutable llvm::FoldingSet<RValueReferenceType> RValueReferenceTypes; 88 mutable llvm::FoldingSet<MemberPointerType> MemberPointerTypes; 89 mutable llvm::FoldingSet<ConstantArrayType> ConstantArrayTypes; 90 mutable llvm::FoldingSet<IncompleteArrayType> IncompleteArrayTypes; 91 mutable std::vector<VariableArrayType*> VariableArrayTypes; 92 mutable llvm::FoldingSet<DependentSizedArrayType> DependentSizedArrayTypes; 93 mutable llvm::FoldingSet<DependentSizedExtVectorType> 94 DependentSizedExtVectorTypes; 95 mutable llvm::FoldingSet<VectorType> VectorTypes; 96 mutable llvm::FoldingSet<FunctionNoProtoType> FunctionNoProtoTypes; 97 mutable llvm::ContextualFoldingSet<FunctionProtoType, ASTContext&> 98 FunctionProtoTypes; 99 mutable llvm::FoldingSet<DependentTypeOfExprType> DependentTypeOfExprTypes; 100 mutable llvm::FoldingSet<DependentDecltypeType> DependentDecltypeTypes; 101 mutable llvm::FoldingSet<TemplateTypeParmType> TemplateTypeParmTypes; 102 mutable llvm::FoldingSet<SubstTemplateTypeParmType> 103 SubstTemplateTypeParmTypes; 104 mutable llvm::FoldingSet<SubstTemplateTypeParmPackType> 105 SubstTemplateTypeParmPackTypes; 106 mutable llvm::ContextualFoldingSet<TemplateSpecializationType, ASTContext&> 107 TemplateSpecializationTypes; 108 mutable llvm::FoldingSet<ParenType> ParenTypes; 109 mutable llvm::FoldingSet<ElaboratedType> ElaboratedTypes; 110 mutable llvm::FoldingSet<DependentNameType> DependentNameTypes; 111 mutable llvm::ContextualFoldingSet<DependentTemplateSpecializationType, 112 ASTContext&> 113 DependentTemplateSpecializationTypes; 114 llvm::FoldingSet<PackExpansionType> PackExpansionTypes; 115 mutable llvm::FoldingSet<ObjCObjectTypeImpl> ObjCObjectTypes; 116 mutable llvm::FoldingSet<ObjCObjectPointerType> ObjCObjectPointerTypes; 117 mutable llvm::FoldingSet<AutoType> AutoTypes; 118 mutable llvm::FoldingSet<AtomicType> AtomicTypes; 119 llvm::FoldingSet<AttributedType> AttributedTypes; 120 121 mutable llvm::FoldingSet<QualifiedTemplateName> QualifiedTemplateNames; 122 mutable llvm::FoldingSet<DependentTemplateName> DependentTemplateNames; 123 mutable llvm::FoldingSet<SubstTemplateTemplateParmStorage> 124 SubstTemplateTemplateParms; 125 mutable llvm::ContextualFoldingSet<SubstTemplateTemplateParmPackStorage, 126 ASTContext&> 127 SubstTemplateTemplateParmPacks; 128 129 /// \brief The set of nested name specifiers. 130 /// 131 /// This set is managed by the NestedNameSpecifier class. 132 mutable llvm::FoldingSet<NestedNameSpecifier> NestedNameSpecifiers; 133 mutable NestedNameSpecifier *GlobalNestedNameSpecifier; 134 friend class NestedNameSpecifier; 135 136 /// \brief A cache mapping from RecordDecls to ASTRecordLayouts. 137 /// 138 /// This is lazily created. This is intentionally not serialized. 139 mutable llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*> 140 ASTRecordLayouts; 141 mutable llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*> 142 ObjCLayouts; 143 144 /// \brief A cache from types to size and alignment information. 145 typedef llvm::DenseMap<const Type*, 146 std::pair<uint64_t, unsigned> > TypeInfoMap; 147 mutable TypeInfoMap MemoizedTypeInfo; 148 149 /// \brief A cache mapping from CXXRecordDecls to key functions. 150 llvm::DenseMap<const CXXRecordDecl*, const CXXMethodDecl*> KeyFunctions; 151 152 /// \brief Mapping from ObjCContainers to their ObjCImplementations. 153 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*> ObjCImpls; 154 155 /// \brief Mapping from ObjCMethod to its duplicate declaration in the same 156 /// interface. 157 llvm::DenseMap<const ObjCMethodDecl*,const ObjCMethodDecl*> ObjCMethodRedecls; 158 159 /// \brief Mapping from __block VarDecls to their copy initialization expr. 160 llvm::DenseMap<const VarDecl*, Expr*> BlockVarCopyInits; 161 162 /// \brief Mapping from class scope functions specialization to their 163 /// template patterns. 164 llvm::DenseMap<const FunctionDecl*, FunctionDecl*> 165 ClassScopeSpecializationPattern; 166 167 /// \brief Mapping from materialized temporaries with static storage duration 168 /// that appear in constant initializers to their evaluated values. 169 llvm::DenseMap<const MaterializeTemporaryExpr*, APValue> 170 MaterializedTemporaryValues; 171 172 /// \brief Representation of a "canonical" template template parameter that 173 /// is used in canonical template names. 174 class CanonicalTemplateTemplateParm : public llvm::FoldingSetNode { 175 TemplateTemplateParmDecl *Parm; 176 177 public: 178 CanonicalTemplateTemplateParm(TemplateTemplateParmDecl *Parm) 179 : Parm(Parm) { } 180 181 TemplateTemplateParmDecl *getParam() const { return Parm; } 182 183 void Profile(llvm::FoldingSetNodeID &ID) { Profile(ID, Parm); } 184 185 static void Profile(llvm::FoldingSetNodeID &ID, 186 TemplateTemplateParmDecl *Parm); 187 }; 188 mutable llvm::FoldingSet<CanonicalTemplateTemplateParm> 189 CanonTemplateTemplateParms; 190 191 TemplateTemplateParmDecl * 192 getCanonicalTemplateTemplateParmDecl(TemplateTemplateParmDecl *TTP) const; 193 194 /// \brief The typedef for the __int128_t type. 195 mutable TypedefDecl *Int128Decl; 196 197 /// \brief The typedef for the __uint128_t type. 198 mutable TypedefDecl *UInt128Decl; 199 200 /// \brief The typedef for the target specific predefined 201 /// __builtin_va_list type. 202 mutable TypedefDecl *BuiltinVaListDecl; 203 204 /// \brief The typedef for the predefined \c id type. 205 mutable TypedefDecl *ObjCIdDecl; 206 207 /// \brief The typedef for the predefined \c SEL type. 208 mutable TypedefDecl *ObjCSelDecl; 209 210 /// \brief The typedef for the predefined \c Class type. 211 mutable TypedefDecl *ObjCClassDecl; 212 213 /// \brief The typedef for the predefined \c Protocol class in Objective-C. 214 mutable ObjCInterfaceDecl *ObjCProtocolClassDecl; 215 216 /// \brief The typedef for the predefined 'BOOL' type. 217 mutable TypedefDecl *BOOLDecl; 218 219 // Typedefs which may be provided defining the structure of Objective-C 220 // pseudo-builtins 221 QualType ObjCIdRedefinitionType; 222 QualType ObjCClassRedefinitionType; 223 QualType ObjCSelRedefinitionType; 224 225 QualType ObjCConstantStringType; 226 mutable RecordDecl *CFConstantStringTypeDecl; 227 228 mutable QualType ObjCSuperType; 229 230 QualType ObjCNSStringType; 231 232 /// \brief The typedef declaration for the Objective-C "instancetype" type. 233 TypedefDecl *ObjCInstanceTypeDecl; 234 235 /// \brief The type for the C FILE type. 236 TypeDecl *FILEDecl; 237 238 /// \brief The type for the C jmp_buf type. 239 TypeDecl *jmp_bufDecl; 240 241 /// \brief The type for the C sigjmp_buf type. 242 TypeDecl *sigjmp_bufDecl; 243 244 /// \brief The type for the C ucontext_t type. 245 TypeDecl *ucontext_tDecl; 246 247 /// \brief Type for the Block descriptor for Blocks CodeGen. 248 /// 249 /// Since this is only used for generation of debug info, it is not 250 /// serialized. 251 mutable RecordDecl *BlockDescriptorType; 252 253 /// \brief Type for the Block descriptor for Blocks CodeGen. 254 /// 255 /// Since this is only used for generation of debug info, it is not 256 /// serialized. 257 mutable RecordDecl *BlockDescriptorExtendedType; 258 259 /// \brief Declaration for the CUDA cudaConfigureCall function. 260 FunctionDecl *cudaConfigureCallDecl; 261 262 TypeSourceInfo NullTypeSourceInfo; 263 264 /// \brief Keeps track of all declaration attributes. 265 /// 266 /// Since so few decls have attrs, we keep them in a hash map instead of 267 /// wasting space in the Decl class. 268 llvm::DenseMap<const Decl*, AttrVec*> DeclAttrs; 269 270 /// \brief Keeps track of the static data member templates from which 271 /// static data members of class template specializations were instantiated. 272 /// 273 /// This data structure stores the mapping from instantiations of static 274 /// data members to the static data member representations within the 275 /// class template from which they were instantiated along with the kind 276 /// of instantiation or specialization (a TemplateSpecializationKind - 1). 277 /// 278 /// Given the following example: 279 /// 280 /// \code 281 /// template<typename T> 282 /// struct X { 283 /// static T value; 284 /// }; 285 /// 286 /// template<typename T> 287 /// T X<T>::value = T(17); 288 /// 289 /// int *x = &X<int>::value; 290 /// \endcode 291 /// 292 /// This mapping will contain an entry that maps from the VarDecl for 293 /// X<int>::value to the corresponding VarDecl for X<T>::value (within the 294 /// class template X) and will be marked TSK_ImplicitInstantiation. 295 llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *> 296 InstantiatedFromStaticDataMember; 297 298 /// \brief Keeps track of the declaration from which a UsingDecl was 299 /// created during instantiation. 300 /// 301 /// The source declaration is always a UsingDecl, an UnresolvedUsingValueDecl, 302 /// or an UnresolvedUsingTypenameDecl. 303 /// 304 /// For example: 305 /// \code 306 /// template<typename T> 307 /// struct A { 308 /// void f(); 309 /// }; 310 /// 311 /// template<typename T> 312 /// struct B : A<T> { 313 /// using A<T>::f; 314 /// }; 315 /// 316 /// template struct B<int>; 317 /// \endcode 318 /// 319 /// This mapping will contain an entry that maps from the UsingDecl in 320 /// B<int> to the UnresolvedUsingDecl in B<T>. 321 llvm::DenseMap<UsingDecl *, NamedDecl *> InstantiatedFromUsingDecl; 322 323 llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*> 324 InstantiatedFromUsingShadowDecl; 325 326 llvm::DenseMap<FieldDecl *, FieldDecl *> InstantiatedFromUnnamedFieldDecl; 327 328 /// \brief Mapping that stores the methods overridden by a given C++ 329 /// member function. 330 /// 331 /// Since most C++ member functions aren't virtual and therefore 332 /// don't override anything, we store the overridden functions in 333 /// this map on the side rather than within the CXXMethodDecl structure. 334 typedef llvm::TinyPtrVector<const CXXMethodDecl*> CXXMethodVector; 335 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector> OverriddenMethods; 336 337 /// \brief Mapping from each declaration context to its corresponding lambda 338 /// mangling context. 339 llvm::DenseMap<const DeclContext *, LambdaMangleContext> LambdaMangleContexts; 340 341 llvm::DenseMap<const DeclContext *, unsigned> UnnamedMangleContexts; 342 llvm::DenseMap<const TagDecl *, unsigned> UnnamedMangleNumbers; 343 344 /// \brief Mapping that stores parameterIndex values for ParmVarDecls when 345 /// that value exceeds the bitfield size of ParmVarDeclBits.ParameterIndex. 346 typedef llvm::DenseMap<const VarDecl *, unsigned> ParameterIndexTable; 347 ParameterIndexTable ParamIndices; 348 349 ImportDecl *FirstLocalImport; 350 ImportDecl *LastLocalImport; 351 352 TranslationUnitDecl *TUDecl; 353 354 /// \brief The associated SourceManager object.a 355 SourceManager &SourceMgr; 356 357 /// \brief The language options used to create the AST associated with 358 /// this ASTContext object. 359 LangOptions &LangOpts; 360 361 /// \brief The allocator used to create AST objects. 362 /// 363 /// AST objects are never destructed; rather, all memory associated with the 364 /// AST objects will be released when the ASTContext itself is destroyed. 365 mutable llvm::BumpPtrAllocator BumpAlloc; 366 367 /// \brief Allocator for partial diagnostics. 368 PartialDiagnostic::StorageAllocator DiagAllocator; 369 370 /// \brief The current C++ ABI. 371 OwningPtr<CXXABI> ABI; 372 CXXABI *createCXXABI(const TargetInfo &T); 373 374 /// \brief The logical -> physical address space map. 375 const LangAS::Map *AddrSpaceMap; 376 377 friend class ASTDeclReader; 378 friend class ASTReader; 379 friend class ASTWriter; 380 friend class CXXRecordDecl; 381 382 const TargetInfo *Target; 383 clang::PrintingPolicy PrintingPolicy; 384 385public: 386 IdentifierTable &Idents; 387 SelectorTable &Selectors; 388 Builtin::Context &BuiltinInfo; 389 mutable DeclarationNameTable DeclarationNames; 390 OwningPtr<ExternalASTSource> ExternalSource; 391 ASTMutationListener *Listener; 392 393 /// \brief Contains parents of a node. 394 typedef llvm::SmallVector<ast_type_traits::DynTypedNode, 1> ParentVector; 395 396 /// \brief Maps from a node to its parents. 397 typedef llvm::DenseMap<const void *, ParentVector> ParentMap; 398 399 /// \brief Returns the parents of the given node. 400 /// 401 /// Note that this will lazily compute the parents of all nodes 402 /// and store them for later retrieval. Thus, the first call is O(n) 403 /// in the number of AST nodes. 404 /// 405 /// Caveats and FIXMEs: 406 /// Calculating the parent map over all AST nodes will need to load the 407 /// full AST. This can be undesirable in the case where the full AST is 408 /// expensive to create (for example, when using precompiled header 409 /// preambles). Thus, there are good opportunities for optimization here. 410 /// One idea is to walk the given node downwards, looking for references 411 /// to declaration contexts - once a declaration context is found, compute 412 /// the parent map for the declaration context; if that can satisfy the 413 /// request, loading the whole AST can be avoided. Note that this is made 414 /// more complex by statements in templates having multiple parents - those 415 /// problems can be solved by building closure over the templated parts of 416 /// the AST, which also avoids touching large parts of the AST. 417 /// Additionally, we will want to add an interface to already give a hint 418 /// where to search for the parents, for example when looking at a statement 419 /// inside a certain function. 420 /// 421 /// 'NodeT' can be one of Decl, Stmt, Type, TypeLoc, 422 /// NestedNameSpecifier or NestedNameSpecifierLoc. 423 template <typename NodeT> 424 ParentVector getParents(const NodeT &Node) { 425 return getParents(ast_type_traits::DynTypedNode::create(Node)); 426 } 427 428 ParentVector getParents(const ast_type_traits::DynTypedNode &Node); 429 430 const clang::PrintingPolicy &getPrintingPolicy() const { 431 return PrintingPolicy; 432 } 433 434 void setPrintingPolicy(const clang::PrintingPolicy &Policy) { 435 PrintingPolicy = Policy; 436 } 437 438 SourceManager& getSourceManager() { return SourceMgr; } 439 const SourceManager& getSourceManager() const { return SourceMgr; } 440 441 llvm::BumpPtrAllocator &getAllocator() const { 442 return BumpAlloc; 443 } 444 445 void *Allocate(size_t Size, unsigned Align = 8) const { 446 return BumpAlloc.Allocate(Size, Align); 447 } 448 void Deallocate(void *Ptr) const { } 449 450 /// Return the total amount of physical memory allocated for representing 451 /// AST nodes and type information. 452 size_t getASTAllocatedMemory() const { 453 return BumpAlloc.getTotalMemory(); 454 } 455 /// Return the total memory used for various side tables. 456 size_t getSideTableAllocatedMemory() const; 457 458 PartialDiagnostic::StorageAllocator &getDiagAllocator() { 459 return DiagAllocator; 460 } 461 462 const TargetInfo &getTargetInfo() const { return *Target; } 463 464 bool AtomicUsesUnsupportedLibcall(const AtomicExpr *E) const; 465 466 const LangOptions& getLangOpts() const { return LangOpts; } 467 468 DiagnosticsEngine &getDiagnostics() const; 469 470 FullSourceLoc getFullLoc(SourceLocation Loc) const { 471 return FullSourceLoc(Loc,SourceMgr); 472 } 473 474 /// \brief All comments in this translation unit. 475 RawCommentList Comments; 476 477 /// \brief True if comments are already loaded from ExternalASTSource. 478 mutable bool CommentsLoaded; 479 480 class RawCommentAndCacheFlags { 481 public: 482 enum Kind { 483 /// We searched for a comment attached to the particular declaration, but 484 /// didn't find any. 485 /// 486 /// getRaw() == 0. 487 NoCommentInDecl = 0, 488 489 /// We have found a comment attached to this particular declaration. 490 /// 491 /// getRaw() != 0. 492 FromDecl, 493 494 /// This declaration does not have an attached comment, and we have 495 /// searched the redeclaration chain. 496 /// 497 /// If getRaw() == 0, the whole redeclaration chain does not have any 498 /// comments. 499 /// 500 /// If getRaw() != 0, it is a comment propagated from other 501 /// redeclaration. 502 FromRedecl 503 }; 504 505 Kind getKind() const LLVM_READONLY { 506 return Data.getInt(); 507 } 508 509 void setKind(Kind K) { 510 Data.setInt(K); 511 } 512 513 const RawComment *getRaw() const LLVM_READONLY { 514 return Data.getPointer(); 515 } 516 517 void setRaw(const RawComment *RC) { 518 Data.setPointer(RC); 519 } 520 521 const Decl *getOriginalDecl() const LLVM_READONLY { 522 return OriginalDecl; 523 } 524 525 void setOriginalDecl(const Decl *Orig) { 526 OriginalDecl = Orig; 527 } 528 529 private: 530 llvm::PointerIntPair<const RawComment *, 2, Kind> Data; 531 const Decl *OriginalDecl; 532 }; 533 534 /// \brief Mapping from declarations to comments attached to any 535 /// redeclaration. 536 /// 537 /// Raw comments are owned by Comments list. This mapping is populated 538 /// lazily. 539 mutable llvm::DenseMap<const Decl *, RawCommentAndCacheFlags> RedeclComments; 540 541 /// \brief Mapping from declarations to parsed comments attached to any 542 /// redeclaration. 543 mutable llvm::DenseMap<const Decl *, comments::FullComment *> ParsedComments; 544 545 /// \brief Return the documentation comment attached to a given declaration, 546 /// without looking into cache. 547 RawComment *getRawCommentForDeclNoCache(const Decl *D) const; 548 549public: 550 RawCommentList &getRawCommentList() { 551 return Comments; 552 } 553 554 void addComment(const RawComment &RC) { 555 assert(LangOpts.RetainCommentsFromSystemHeaders || 556 !SourceMgr.isInSystemHeader(RC.getSourceRange().getBegin())); 557 Comments.addComment(RC, BumpAlloc); 558 } 559 560 /// \brief Return the documentation comment attached to a given declaration. 561 /// Returns NULL if no comment is attached. 562 /// 563 /// \param OriginalDecl if not NULL, is set to declaration AST node that had 564 /// the comment, if the comment we found comes from a redeclaration. 565 const RawComment *getRawCommentForAnyRedecl( 566 const Decl *D, 567 const Decl **OriginalDecl = NULL) const; 568 569 /// Return parsed documentation comment attached to a given declaration. 570 /// Returns NULL if no comment is attached. 571 /// 572 /// \param PP the Preprocessor used with this TU. Could be NULL if 573 /// preprocessor is not available. 574 comments::FullComment *getCommentForDecl(const Decl *D, 575 const Preprocessor *PP) const; 576 577 /// Return parsed documentation comment attached to a given declaration. 578 /// Returns NULL if no comment is attached. Does not look at any 579 /// redeclarations of the declaration. 580 comments::FullComment *getLocalCommentForDeclUncached(const Decl *D) const; 581 582 comments::FullComment *cloneFullComment(comments::FullComment *FC, 583 const Decl *D) const; 584 585private: 586 mutable comments::CommandTraits CommentCommandTraits; 587 588public: 589 comments::CommandTraits &getCommentCommandTraits() const { 590 return CommentCommandTraits; 591 } 592 593 /// \brief Retrieve the attributes for the given declaration. 594 AttrVec& getDeclAttrs(const Decl *D); 595 596 /// \brief Erase the attributes corresponding to the given declaration. 597 void eraseDeclAttrs(const Decl *D); 598 599 /// \brief If this variable is an instantiated static data member of a 600 /// class template specialization, returns the templated static data member 601 /// from which it was instantiated. 602 MemberSpecializationInfo *getInstantiatedFromStaticDataMember( 603 const VarDecl *Var); 604 605 FunctionDecl *getClassScopeSpecializationPattern(const FunctionDecl *FD); 606 607 void setClassScopeSpecializationPattern(FunctionDecl *FD, 608 FunctionDecl *Pattern); 609 610 /// \brief Note that the static data member \p Inst is an instantiation of 611 /// the static data member template \p Tmpl of a class template. 612 void setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, 613 TemplateSpecializationKind TSK, 614 SourceLocation PointOfInstantiation = SourceLocation()); 615 616 /// \brief If the given using decl \p Inst is an instantiation of a 617 /// (possibly unresolved) using decl from a template instantiation, 618 /// return it. 619 NamedDecl *getInstantiatedFromUsingDecl(UsingDecl *Inst); 620 621 /// \brief Remember that the using decl \p Inst is an instantiation 622 /// of the using decl \p Pattern of a class template. 623 void setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern); 624 625 void setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, 626 UsingShadowDecl *Pattern); 627 UsingShadowDecl *getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst); 628 629 FieldDecl *getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field); 630 631 void setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, FieldDecl *Tmpl); 632 633 /// \brief Return \c true if \p FD is a zero-length bitfield which follows 634 /// the non-bitfield \p LastFD. 635 bool ZeroBitfieldFollowsNonBitfield(const FieldDecl *FD, 636 const FieldDecl *LastFD) const; 637 638 /// \brief Return \c true if \p FD is a zero-length bitfield which follows 639 /// the bitfield \p LastFD. 640 bool ZeroBitfieldFollowsBitfield(const FieldDecl *FD, 641 const FieldDecl *LastFD) const; 642 643 /// \brief Return \c true if \p FD is a bitfield which follows the bitfield 644 /// \p LastFD. 645 bool BitfieldFollowsBitfield(const FieldDecl *FD, 646 const FieldDecl *LastFD) const; 647 648 /// \brief Return \c true if \p FD is not a bitfield which follows the 649 /// bitfield \p LastFD. 650 bool NonBitfieldFollowsBitfield(const FieldDecl *FD, 651 const FieldDecl *LastFD) const; 652 653 /// \brief Return \c true if \p FD is a bitfield which follows the 654 /// non-bitfield \p LastFD. 655 bool BitfieldFollowsNonBitfield(const FieldDecl *FD, 656 const FieldDecl *LastFD) const; 657 658 // Access to the set of methods overridden by the given C++ method. 659 typedef CXXMethodVector::const_iterator overridden_cxx_method_iterator; 660 overridden_cxx_method_iterator 661 overridden_methods_begin(const CXXMethodDecl *Method) const; 662 663 overridden_cxx_method_iterator 664 overridden_methods_end(const CXXMethodDecl *Method) const; 665 666 unsigned overridden_methods_size(const CXXMethodDecl *Method) const; 667 668 /// \brief Note that the given C++ \p Method overrides the given \p 669 /// Overridden method. 670 void addOverriddenMethod(const CXXMethodDecl *Method, 671 const CXXMethodDecl *Overridden); 672 673 /// \brief Return C++ or ObjC overridden methods for the given \p Method. 674 /// 675 /// An ObjC method is considered to override any method in the class's 676 /// base classes, its protocols, or its categories' protocols, that has 677 /// the same selector and is of the same kind (class or instance). 678 /// A method in an implementation is not considered as overriding the same 679 /// method in the interface or its categories. 680 void getOverriddenMethods( 681 const NamedDecl *Method, 682 SmallVectorImpl<const NamedDecl *> &Overridden) const; 683 684 /// \brief Notify the AST context that a new import declaration has been 685 /// parsed or implicitly created within this translation unit. 686 void addedLocalImportDecl(ImportDecl *Import); 687 688 static ImportDecl *getNextLocalImport(ImportDecl *Import) { 689 return Import->NextLocalImport; 690 } 691 692 /// \brief Iterator that visits import declarations. 693 class import_iterator { 694 ImportDecl *Import; 695 696 public: 697 typedef ImportDecl *value_type; 698 typedef ImportDecl *reference; 699 typedef ImportDecl *pointer; 700 typedef int difference_type; 701 typedef std::forward_iterator_tag iterator_category; 702 703 import_iterator() : Import() { } 704 explicit import_iterator(ImportDecl *Import) : Import(Import) { } 705 706 reference operator*() const { return Import; } 707 pointer operator->() const { return Import; } 708 709 import_iterator &operator++() { 710 Import = ASTContext::getNextLocalImport(Import); 711 return *this; 712 } 713 714 import_iterator operator++(int) { 715 import_iterator Other(*this); 716 ++(*this); 717 return Other; 718 } 719 720 friend bool operator==(import_iterator X, import_iterator Y) { 721 return X.Import == Y.Import; 722 } 723 724 friend bool operator!=(import_iterator X, import_iterator Y) { 725 return X.Import != Y.Import; 726 } 727 }; 728 729 import_iterator local_import_begin() const { 730 return import_iterator(FirstLocalImport); 731 } 732 import_iterator local_import_end() const { return import_iterator(); } 733 734 TranslationUnitDecl *getTranslationUnitDecl() const { return TUDecl; } 735 736 737 // Builtin Types. 738 CanQualType VoidTy; 739 CanQualType BoolTy; 740 CanQualType CharTy; 741 CanQualType WCharTy; // [C++ 3.9.1p5]. 742 CanQualType WideCharTy; // Same as WCharTy in C++, integer type in C99. 743 CanQualType WIntTy; // [C99 7.24.1], integer type unchanged by default promotions. 744 CanQualType Char16Ty; // [C++0x 3.9.1p5], integer type in C99. 745 CanQualType Char32Ty; // [C++0x 3.9.1p5], integer type in C99. 746 CanQualType SignedCharTy, ShortTy, IntTy, LongTy, LongLongTy, Int128Ty; 747 CanQualType UnsignedCharTy, UnsignedShortTy, UnsignedIntTy, UnsignedLongTy; 748 CanQualType UnsignedLongLongTy, UnsignedInt128Ty; 749 CanQualType FloatTy, DoubleTy, LongDoubleTy; 750 CanQualType HalfTy; // [OpenCL 6.1.1.1], ARM NEON 751 CanQualType FloatComplexTy, DoubleComplexTy, LongDoubleComplexTy; 752 CanQualType VoidPtrTy, NullPtrTy; 753 CanQualType DependentTy, OverloadTy, BoundMemberTy, UnknownAnyTy; 754 CanQualType BuiltinFnTy; 755 CanQualType PseudoObjectTy, ARCUnbridgedCastTy; 756 CanQualType ObjCBuiltinIdTy, ObjCBuiltinClassTy, ObjCBuiltinSelTy; 757 CanQualType ObjCBuiltinBoolTy; 758 CanQualType OCLImage1dTy, OCLImage1dArrayTy, OCLImage1dBufferTy; 759 CanQualType OCLImage2dTy, OCLImage2dArrayTy; 760 CanQualType OCLImage3dTy; 761 CanQualType OCLSamplerTy, OCLEventTy; 762 763 // Types for deductions in C++0x [stmt.ranged]'s desugaring. Built on demand. 764 mutable QualType AutoDeductTy; // Deduction against 'auto'. 765 mutable QualType AutoRRefDeductTy; // Deduction against 'auto &&'. 766 767 // Type used to help define __builtin_va_list for some targets. 768 // The type is built when constructing 'BuiltinVaListDecl'. 769 mutable QualType VaListTagTy; 770 771 ASTContext(LangOptions& LOpts, SourceManager &SM, const TargetInfo *t, 772 IdentifierTable &idents, SelectorTable &sels, 773 Builtin::Context &builtins, 774 unsigned size_reserve, 775 bool DelayInitialization = false); 776 777 ~ASTContext(); 778 779 /// \brief Attach an external AST source to the AST context. 780 /// 781 /// The external AST source provides the ability to load parts of 782 /// the abstract syntax tree as needed from some external storage, 783 /// e.g., a precompiled header. 784 void setExternalSource(OwningPtr<ExternalASTSource> &Source); 785 786 /// \brief Retrieve a pointer to the external AST source associated 787 /// with this AST context, if any. 788 ExternalASTSource *getExternalSource() const { return ExternalSource.get(); } 789 790 /// \brief Attach an AST mutation listener to the AST context. 791 /// 792 /// The AST mutation listener provides the ability to track modifications to 793 /// the abstract syntax tree entities committed after they were initially 794 /// created. 795 void setASTMutationListener(ASTMutationListener *Listener) { 796 this->Listener = Listener; 797 } 798 799 /// \brief Retrieve a pointer to the AST mutation listener associated 800 /// with this AST context, if any. 801 ASTMutationListener *getASTMutationListener() const { return Listener; } 802 803 void PrintStats() const; 804 const SmallVectorImpl<Type *>& getTypes() const { return Types; } 805 806 /// \brief Retrieve the declaration for the 128-bit signed integer type. 807 TypedefDecl *getInt128Decl() const; 808 809 /// \brief Retrieve the declaration for the 128-bit unsigned integer type. 810 TypedefDecl *getUInt128Decl() const; 811 812 //===--------------------------------------------------------------------===// 813 // Type Constructors 814 //===--------------------------------------------------------------------===// 815 816private: 817 /// \brief Return a type with extended qualifiers. 818 QualType getExtQualType(const Type *Base, Qualifiers Quals) const; 819 820 QualType getTypeDeclTypeSlow(const TypeDecl *Decl) const; 821 822public: 823 /// \brief Return the uniqued reference to the type for an address space 824 /// qualified type with the specified type and address space. 825 /// 826 /// The resulting type has a union of the qualifiers from T and the address 827 /// space. If T already has an address space specifier, it is silently 828 /// replaced. 829 QualType getAddrSpaceQualType(QualType T, unsigned AddressSpace) const; 830 831 /// \brief Return the uniqued reference to the type for an Objective-C 832 /// gc-qualified type. 833 /// 834 /// The retulting type has a union of the qualifiers from T and the gc 835 /// attribute. 836 QualType getObjCGCQualType(QualType T, Qualifiers::GC gcAttr) const; 837 838 /// \brief Return the uniqued reference to the type for a \c restrict 839 /// qualified type. 840 /// 841 /// The resulting type has a union of the qualifiers from \p T and 842 /// \c restrict. 843 QualType getRestrictType(QualType T) const { 844 return T.withFastQualifiers(Qualifiers::Restrict); 845 } 846 847 /// \brief Return the uniqued reference to the type for a \c volatile 848 /// qualified type. 849 /// 850 /// The resulting type has a union of the qualifiers from \p T and 851 /// \c volatile. 852 QualType getVolatileType(QualType T) const { 853 return T.withFastQualifiers(Qualifiers::Volatile); 854 } 855 856 /// \brief Return the uniqued reference to the type for a \c const 857 /// qualified type. 858 /// 859 /// The resulting type has a union of the qualifiers from \p T and \c const. 860 /// 861 /// It can be reasonably expected that this will always be equivalent to 862 /// calling T.withConst(). 863 QualType getConstType(QualType T) const { return T.withConst(); } 864 865 /// \brief Change the ExtInfo on a function type. 866 const FunctionType *adjustFunctionType(const FunctionType *Fn, 867 FunctionType::ExtInfo EInfo); 868 869 /// \brief Change the result type of a function type once it is deduced. 870 void adjustDeducedFunctionResultType(FunctionDecl *FD, QualType ResultType); 871 872 /// \brief Return the uniqued reference to the type for a complex 873 /// number with the specified element type. 874 QualType getComplexType(QualType T) const; 875 CanQualType getComplexType(CanQualType T) const { 876 return CanQualType::CreateUnsafe(getComplexType((QualType) T)); 877 } 878 879 /// \brief Return the uniqued reference to the type for a pointer to 880 /// the specified type. 881 QualType getPointerType(QualType T) const; 882 CanQualType getPointerType(CanQualType T) const { 883 return CanQualType::CreateUnsafe(getPointerType((QualType) T)); 884 } 885 886 /// \brief Return the uniqued reference to the atomic type for the specified 887 /// type. 888 QualType getAtomicType(QualType T) const; 889 890 /// \brief Return the uniqued reference to the type for a block of the 891 /// specified type. 892 QualType getBlockPointerType(QualType T) const; 893 894 /// Gets the struct used to keep track of the descriptor for pointer to 895 /// blocks. 896 QualType getBlockDescriptorType() const; 897 898 /// Gets the struct used to keep track of the extended descriptor for 899 /// pointer to blocks. 900 QualType getBlockDescriptorExtendedType() const; 901 902 void setcudaConfigureCallDecl(FunctionDecl *FD) { 903 cudaConfigureCallDecl = FD; 904 } 905 FunctionDecl *getcudaConfigureCallDecl() { 906 return cudaConfigureCallDecl; 907 } 908 909 /// Returns true iff we need copy/dispose helpers for the given type. 910 bool BlockRequiresCopying(QualType Ty, const VarDecl *D); 911 912 913 /// Returns true, if given type has a known lifetime. HasByrefExtendedLayout is set 914 /// to false in this case. If HasByrefExtendedLayout returns true, byref variable 915 /// has extended lifetime. 916 bool getByrefLifetime(QualType Ty, 917 Qualifiers::ObjCLifetime &Lifetime, 918 bool &HasByrefExtendedLayout) const; 919 920 /// \brief Return the uniqued reference to the type for an lvalue reference 921 /// to the specified type. 922 QualType getLValueReferenceType(QualType T, bool SpelledAsLValue = true) 923 const; 924 925 /// \brief Return the uniqued reference to the type for an rvalue reference 926 /// to the specified type. 927 QualType getRValueReferenceType(QualType T) const; 928 929 /// \brief Return the uniqued reference to the type for a member pointer to 930 /// the specified type in the specified class. 931 /// 932 /// The class \p Cls is a \c Type because it could be a dependent name. 933 QualType getMemberPointerType(QualType T, const Type *Cls) const; 934 935 /// \brief Return a non-unique reference to the type for a variable array of 936 /// the specified element type. 937 QualType getVariableArrayType(QualType EltTy, Expr *NumElts, 938 ArrayType::ArraySizeModifier ASM, 939 unsigned IndexTypeQuals, 940 SourceRange Brackets) const; 941 942 /// \brief Return a non-unique reference to the type for a dependently-sized 943 /// array of the specified element type. 944 /// 945 /// FIXME: We will need these to be uniqued, or at least comparable, at some 946 /// point. 947 QualType getDependentSizedArrayType(QualType EltTy, Expr *NumElts, 948 ArrayType::ArraySizeModifier ASM, 949 unsigned IndexTypeQuals, 950 SourceRange Brackets) const; 951 952 /// \brief Return a unique reference to the type for an incomplete array of 953 /// the specified element type. 954 QualType getIncompleteArrayType(QualType EltTy, 955 ArrayType::ArraySizeModifier ASM, 956 unsigned IndexTypeQuals) const; 957 958 /// \brief Return the unique reference to the type for a constant array of 959 /// the specified element type. 960 QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize, 961 ArrayType::ArraySizeModifier ASM, 962 unsigned IndexTypeQuals) const; 963 964 /// \brief Returns a vla type where known sizes are replaced with [*]. 965 QualType getVariableArrayDecayedType(QualType Ty) const; 966 967 /// \brief Return the unique reference to a vector type of the specified 968 /// element type and size. 969 /// 970 /// \pre \p VectorType must be a built-in type. 971 QualType getVectorType(QualType VectorType, unsigned NumElts, 972 VectorType::VectorKind VecKind) const; 973 974 /// \brief Return the unique reference to an extended vector type 975 /// of the specified element type and size. 976 /// 977 /// \pre \p VectorType must be a built-in type. 978 QualType getExtVectorType(QualType VectorType, unsigned NumElts) const; 979 980 /// \pre Return a non-unique reference to the type for a dependently-sized 981 /// vector of the specified element type. 982 /// 983 /// FIXME: We will need these to be uniqued, or at least comparable, at some 984 /// point. 985 QualType getDependentSizedExtVectorType(QualType VectorType, 986 Expr *SizeExpr, 987 SourceLocation AttrLoc) const; 988 989 /// \brief Return a K&R style C function type like 'int()'. 990 QualType getFunctionNoProtoType(QualType ResultTy, 991 const FunctionType::ExtInfo &Info) const; 992 993 QualType getFunctionNoProtoType(QualType ResultTy) const { 994 return getFunctionNoProtoType(ResultTy, FunctionType::ExtInfo()); 995 } 996 997 /// \brief Return a normal function type with a typed argument list. 998 QualType getFunctionType(QualType ResultTy, ArrayRef<QualType> Args, 999 const FunctionProtoType::ExtProtoInfo &EPI) const; 1000 1001 /// \brief Return the unique reference to the type for the specified type 1002 /// declaration. 1003 QualType getTypeDeclType(const TypeDecl *Decl, 1004 const TypeDecl *PrevDecl = 0) const { 1005 assert(Decl && "Passed null for Decl param"); 1006 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); 1007 1008 if (PrevDecl) { 1009 assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl"); 1010 Decl->TypeForDecl = PrevDecl->TypeForDecl; 1011 return QualType(PrevDecl->TypeForDecl, 0); 1012 } 1013 1014 return getTypeDeclTypeSlow(Decl); 1015 } 1016 1017 /// \brief Return the unique reference to the type for the specified 1018 /// typedef-name decl. 1019 QualType getTypedefType(const TypedefNameDecl *Decl, 1020 QualType Canon = QualType()) const; 1021 1022 QualType getRecordType(const RecordDecl *Decl) const; 1023 1024 QualType getEnumType(const EnumDecl *Decl) const; 1025 1026 QualType getInjectedClassNameType(CXXRecordDecl *Decl, QualType TST) const; 1027 1028 QualType getAttributedType(AttributedType::Kind attrKind, 1029 QualType modifiedType, 1030 QualType equivalentType); 1031 1032 QualType getSubstTemplateTypeParmType(const TemplateTypeParmType *Replaced, 1033 QualType Replacement) const; 1034 QualType getSubstTemplateTypeParmPackType( 1035 const TemplateTypeParmType *Replaced, 1036 const TemplateArgument &ArgPack); 1037 1038 QualType getTemplateTypeParmType(unsigned Depth, unsigned Index, 1039 bool ParameterPack, 1040 TemplateTypeParmDecl *ParmDecl = 0) const; 1041 1042 QualType getTemplateSpecializationType(TemplateName T, 1043 const TemplateArgument *Args, 1044 unsigned NumArgs, 1045 QualType Canon = QualType()) const; 1046 1047 QualType getCanonicalTemplateSpecializationType(TemplateName T, 1048 const TemplateArgument *Args, 1049 unsigned NumArgs) const; 1050 1051 QualType getTemplateSpecializationType(TemplateName T, 1052 const TemplateArgumentListInfo &Args, 1053 QualType Canon = QualType()) const; 1054 1055 TypeSourceInfo * 1056 getTemplateSpecializationTypeInfo(TemplateName T, SourceLocation TLoc, 1057 const TemplateArgumentListInfo &Args, 1058 QualType Canon = QualType()) const; 1059 1060 QualType getParenType(QualType NamedType) const; 1061 1062 QualType getElaboratedType(ElaboratedTypeKeyword Keyword, 1063 NestedNameSpecifier *NNS, 1064 QualType NamedType) const; 1065 QualType getDependentNameType(ElaboratedTypeKeyword Keyword, 1066 NestedNameSpecifier *NNS, 1067 const IdentifierInfo *Name, 1068 QualType Canon = QualType()) const; 1069 1070 QualType getDependentTemplateSpecializationType(ElaboratedTypeKeyword Keyword, 1071 NestedNameSpecifier *NNS, 1072 const IdentifierInfo *Name, 1073 const TemplateArgumentListInfo &Args) const; 1074 QualType getDependentTemplateSpecializationType(ElaboratedTypeKeyword Keyword, 1075 NestedNameSpecifier *NNS, 1076 const IdentifierInfo *Name, 1077 unsigned NumArgs, 1078 const TemplateArgument *Args) const; 1079 1080 QualType getPackExpansionType(QualType Pattern, 1081 Optional<unsigned> NumExpansions); 1082 1083 QualType getObjCInterfaceType(const ObjCInterfaceDecl *Decl, 1084 ObjCInterfaceDecl *PrevDecl = 0) const; 1085 1086 QualType getObjCObjectType(QualType Base, 1087 ObjCProtocolDecl * const *Protocols, 1088 unsigned NumProtocols) const; 1089 1090 /// \brief Return a ObjCObjectPointerType type for the given ObjCObjectType. 1091 QualType getObjCObjectPointerType(QualType OIT) const; 1092 1093 /// \brief GCC extension. 1094 QualType getTypeOfExprType(Expr *e) const; 1095 QualType getTypeOfType(QualType t) const; 1096 1097 /// \brief C++11 decltype. 1098 QualType getDecltypeType(Expr *e, QualType UnderlyingType) const; 1099 1100 /// \brief Unary type transforms 1101 QualType getUnaryTransformType(QualType BaseType, QualType UnderlyingType, 1102 UnaryTransformType::UTTKind UKind) const; 1103 1104 /// \brief C++11 deduced auto type. 1105 QualType getAutoType(QualType DeducedType, bool IsDecltypeAuto, 1106 bool IsDependent = false) const; 1107 1108 /// \brief C++11 deduction pattern for 'auto' type. 1109 QualType getAutoDeductType() const; 1110 1111 /// \brief C++11 deduction pattern for 'auto &&' type. 1112 QualType getAutoRRefDeductType() const; 1113 1114 /// \brief Return the unique reference to the type for the specified TagDecl 1115 /// (struct/union/class/enum) decl. 1116 QualType getTagDeclType(const TagDecl *Decl) const; 1117 1118 /// \brief Return the unique type for "size_t" (C99 7.17), defined in 1119 /// <stddef.h>. 1120 /// 1121 /// The sizeof operator requires this (C99 6.5.3.4p4). 1122 CanQualType getSizeType() const; 1123 1124 /// \brief Return the unique type for "intmax_t" (C99 7.18.1.5), defined in 1125 /// <stdint.h>. 1126 CanQualType getIntMaxType() const; 1127 1128 /// \brief Return the unique type for "uintmax_t" (C99 7.18.1.5), defined in 1129 /// <stdint.h>. 1130 CanQualType getUIntMaxType() const; 1131 1132 /// \brief Return the unique wchar_t type available in C++ (and available as 1133 /// __wchar_t as a Microsoft extension). 1134 QualType getWCharType() const { return WCharTy; } 1135 1136 /// \brief Return the type of wide characters. In C++, this returns the 1137 /// unique wchar_t type. In C99, this returns a type compatible with the type 1138 /// defined in <stddef.h> as defined by the target. 1139 QualType getWideCharType() const { return WideCharTy; } 1140 1141 /// \brief Return the type of "signed wchar_t". 1142 /// 1143 /// Used when in C++, as a GCC extension. 1144 QualType getSignedWCharType() const; 1145 1146 /// \brief Return the type of "unsigned wchar_t". 1147 /// 1148 /// Used when in C++, as a GCC extension. 1149 QualType getUnsignedWCharType() const; 1150 1151 /// \brief In C99, this returns a type compatible with the type 1152 /// defined in <stddef.h> as defined by the target. 1153 QualType getWIntType() const { return WIntTy; } 1154 1155 /// \brief Return a type compatible with "intptr_t" (C99 7.18.1.4), 1156 /// as defined by the target. 1157 QualType getIntPtrType() const; 1158 1159 /// \brief Return a type compatible with "uintptr_t" (C99 7.18.1.4), 1160 /// as defined by the target. 1161 QualType getUIntPtrType() const; 1162 1163 /// \brief Return the unique type for "ptrdiff_t" (C99 7.17) defined in 1164 /// <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). 1165 QualType getPointerDiffType() const; 1166 1167 /// \brief Return the unique type for "pid_t" defined in 1168 /// <sys/types.h>. We need this to compute the correct type for vfork(). 1169 QualType getProcessIDType() const; 1170 1171 /// \brief Return the C structure type used to represent constant CFStrings. 1172 QualType getCFConstantStringType() const; 1173 1174 /// \brief Returns the C struct type for objc_super 1175 QualType getObjCSuperType() const; 1176 void setObjCSuperType(QualType ST) { ObjCSuperType = ST; } 1177 1178 /// Get the structure type used to representation CFStrings, or NULL 1179 /// if it hasn't yet been built. 1180 QualType getRawCFConstantStringType() const { 1181 if (CFConstantStringTypeDecl) 1182 return getTagDeclType(CFConstantStringTypeDecl); 1183 return QualType(); 1184 } 1185 void setCFConstantStringType(QualType T); 1186 1187 // This setter/getter represents the ObjC type for an NSConstantString. 1188 void setObjCConstantStringInterface(ObjCInterfaceDecl *Decl); 1189 QualType getObjCConstantStringInterface() const { 1190 return ObjCConstantStringType; 1191 } 1192 1193 QualType getObjCNSStringType() const { 1194 return ObjCNSStringType; 1195 } 1196 1197 void setObjCNSStringType(QualType T) { 1198 ObjCNSStringType = T; 1199 } 1200 1201 /// \brief Retrieve the type that \c id has been defined to, which may be 1202 /// different from the built-in \c id if \c id has been typedef'd. 1203 QualType getObjCIdRedefinitionType() const { 1204 if (ObjCIdRedefinitionType.isNull()) 1205 return getObjCIdType(); 1206 return ObjCIdRedefinitionType; 1207 } 1208 1209 /// \brief Set the user-written type that redefines \c id. 1210 void setObjCIdRedefinitionType(QualType RedefType) { 1211 ObjCIdRedefinitionType = RedefType; 1212 } 1213 1214 /// \brief Retrieve the type that \c Class has been defined to, which may be 1215 /// different from the built-in \c Class if \c Class has been typedef'd. 1216 QualType getObjCClassRedefinitionType() const { 1217 if (ObjCClassRedefinitionType.isNull()) 1218 return getObjCClassType(); 1219 return ObjCClassRedefinitionType; 1220 } 1221 1222 /// \brief Set the user-written type that redefines 'SEL'. 1223 void setObjCClassRedefinitionType(QualType RedefType) { 1224 ObjCClassRedefinitionType = RedefType; 1225 } 1226 1227 /// \brief Retrieve the type that 'SEL' has been defined to, which may be 1228 /// different from the built-in 'SEL' if 'SEL' has been typedef'd. 1229 QualType getObjCSelRedefinitionType() const { 1230 if (ObjCSelRedefinitionType.isNull()) 1231 return getObjCSelType(); 1232 return ObjCSelRedefinitionType; 1233 } 1234 1235 1236 /// \brief Set the user-written type that redefines 'SEL'. 1237 void setObjCSelRedefinitionType(QualType RedefType) { 1238 ObjCSelRedefinitionType = RedefType; 1239 } 1240 1241 /// \brief Retrieve the Objective-C "instancetype" type, if already known; 1242 /// otherwise, returns a NULL type; 1243 QualType getObjCInstanceType() { 1244 return getTypeDeclType(getObjCInstanceTypeDecl()); 1245 } 1246 1247 /// \brief Retrieve the typedef declaration corresponding to the Objective-C 1248 /// "instancetype" type. 1249 TypedefDecl *getObjCInstanceTypeDecl(); 1250 1251 /// \brief Set the type for the C FILE type. 1252 void setFILEDecl(TypeDecl *FILEDecl) { this->FILEDecl = FILEDecl; } 1253 1254 /// \brief Retrieve the C FILE type. 1255 QualType getFILEType() const { 1256 if (FILEDecl) 1257 return getTypeDeclType(FILEDecl); 1258 return QualType(); 1259 } 1260 1261 /// \brief Set the type for the C jmp_buf type. 1262 void setjmp_bufDecl(TypeDecl *jmp_bufDecl) { 1263 this->jmp_bufDecl = jmp_bufDecl; 1264 } 1265 1266 /// \brief Retrieve the C jmp_buf type. 1267 QualType getjmp_bufType() const { 1268 if (jmp_bufDecl) 1269 return getTypeDeclType(jmp_bufDecl); 1270 return QualType(); 1271 } 1272 1273 /// \brief Set the type for the C sigjmp_buf type. 1274 void setsigjmp_bufDecl(TypeDecl *sigjmp_bufDecl) { 1275 this->sigjmp_bufDecl = sigjmp_bufDecl; 1276 } 1277 1278 /// \brief Retrieve the C sigjmp_buf type. 1279 QualType getsigjmp_bufType() const { 1280 if (sigjmp_bufDecl) 1281 return getTypeDeclType(sigjmp_bufDecl); 1282 return QualType(); 1283 } 1284 1285 /// \brief Set the type for the C ucontext_t type. 1286 void setucontext_tDecl(TypeDecl *ucontext_tDecl) { 1287 this->ucontext_tDecl = ucontext_tDecl; 1288 } 1289 1290 /// \brief Retrieve the C ucontext_t type. 1291 QualType getucontext_tType() const { 1292 if (ucontext_tDecl) 1293 return getTypeDeclType(ucontext_tDecl); 1294 return QualType(); 1295 } 1296 1297 /// \brief The result type of logical operations, '<', '>', '!=', etc. 1298 QualType getLogicalOperationType() const { 1299 return getLangOpts().CPlusPlus ? BoolTy : IntTy; 1300 } 1301 1302 /// \brief Emit the Objective-CC type encoding for the given type \p T into 1303 /// \p S. 1304 /// 1305 /// If \p Field is specified then record field names are also encoded. 1306 void getObjCEncodingForType(QualType T, std::string &S, 1307 const FieldDecl *Field=0) const; 1308 1309 void getLegacyIntegralTypeEncoding(QualType &t) const; 1310 1311 /// \brief Put the string version of the type qualifiers \p QT into \p S. 1312 void getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, 1313 std::string &S) const; 1314 1315 /// \brief Emit the encoded type for the function \p Decl into \p S. 1316 /// 1317 /// This is in the same format as Objective-C method encodings. 1318 /// 1319 /// \returns true if an error occurred (e.g., because one of the parameter 1320 /// types is incomplete), false otherwise. 1321 bool getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, std::string& S); 1322 1323 /// \brief Emit the encoded type for the method declaration \p Decl into 1324 /// \p S. 1325 /// 1326 /// \returns true if an error occurred (e.g., because one of the parameter 1327 /// types is incomplete), false otherwise. 1328 bool getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, std::string &S, 1329 bool Extended = false) 1330 const; 1331 1332 /// \brief Return the encoded type for this block declaration. 1333 std::string getObjCEncodingForBlock(const BlockExpr *blockExpr) const; 1334 1335 /// getObjCEncodingForPropertyDecl - Return the encoded type for 1336 /// this method declaration. If non-NULL, Container must be either 1337 /// an ObjCCategoryImplDecl or ObjCImplementationDecl; it should 1338 /// only be NULL when getting encodings for protocol properties. 1339 void getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, 1340 const Decl *Container, 1341 std::string &S) const; 1342 1343 bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, 1344 ObjCProtocolDecl *rProto) const; 1345 1346 /// \brief Return the size of type \p T for Objective-C encoding purpose, 1347 /// in characters. 1348 CharUnits getObjCEncodingTypeSize(QualType T) const; 1349 1350 /// \brief Retrieve the typedef corresponding to the predefined \c id type 1351 /// in Objective-C. 1352 TypedefDecl *getObjCIdDecl() const; 1353 1354 /// \brief Represents the Objective-CC \c id type. 1355 /// 1356 /// This is set up lazily, by Sema. \c id is always a (typedef for a) 1357 /// pointer type, a pointer to a struct. 1358 QualType getObjCIdType() const { 1359 return getTypeDeclType(getObjCIdDecl()); 1360 } 1361 1362 /// \brief Retrieve the typedef corresponding to the predefined 'SEL' type 1363 /// in Objective-C. 1364 TypedefDecl *getObjCSelDecl() const; 1365 1366 /// \brief Retrieve the type that corresponds to the predefined Objective-C 1367 /// 'SEL' type. 1368 QualType getObjCSelType() const { 1369 return getTypeDeclType(getObjCSelDecl()); 1370 } 1371 1372 /// \brief Retrieve the typedef declaration corresponding to the predefined 1373 /// Objective-C 'Class' type. 1374 TypedefDecl *getObjCClassDecl() const; 1375 1376 /// \brief Represents the Objective-C \c Class type. 1377 /// 1378 /// This is set up lazily, by Sema. \c Class is always a (typedef for a) 1379 /// pointer type, a pointer to a struct. 1380 QualType getObjCClassType() const { 1381 return getTypeDeclType(getObjCClassDecl()); 1382 } 1383 1384 /// \brief Retrieve the Objective-C class declaration corresponding to 1385 /// the predefined \c Protocol class. 1386 ObjCInterfaceDecl *getObjCProtocolDecl() const; 1387 1388 /// \brief Retrieve declaration of 'BOOL' typedef 1389 TypedefDecl *getBOOLDecl() const { 1390 return BOOLDecl; 1391 } 1392 1393 /// \brief Save declaration of 'BOOL' typedef 1394 void setBOOLDecl(TypedefDecl *TD) { 1395 BOOLDecl = TD; 1396 } 1397 1398 /// \brief type of 'BOOL' type. 1399 QualType getBOOLType() const { 1400 return getTypeDeclType(getBOOLDecl()); 1401 } 1402 1403 /// \brief Retrieve the type of the Objective-C \c Protocol class. 1404 QualType getObjCProtoType() const { 1405 return getObjCInterfaceType(getObjCProtocolDecl()); 1406 } 1407 1408 /// \brief Retrieve the C type declaration corresponding to the predefined 1409 /// \c __builtin_va_list type. 1410 TypedefDecl *getBuiltinVaListDecl() const; 1411 1412 /// \brief Retrieve the type of the \c __builtin_va_list type. 1413 QualType getBuiltinVaListType() const { 1414 return getTypeDeclType(getBuiltinVaListDecl()); 1415 } 1416 1417 /// \brief Retrieve the C type declaration corresponding to the predefined 1418 /// \c __va_list_tag type used to help define the \c __builtin_va_list type 1419 /// for some targets. 1420 QualType getVaListTagType() const; 1421 1422 /// \brief Return a type with additional \c const, \c volatile, or 1423 /// \c restrict qualifiers. 1424 QualType getCVRQualifiedType(QualType T, unsigned CVR) const { 1425 return getQualifiedType(T, Qualifiers::fromCVRMask(CVR)); 1426 } 1427 1428 /// \brief Un-split a SplitQualType. 1429 QualType getQualifiedType(SplitQualType split) const { 1430 return getQualifiedType(split.Ty, split.Quals); 1431 } 1432 1433 /// \brief Return a type with additional qualifiers. 1434 QualType getQualifiedType(QualType T, Qualifiers Qs) const { 1435 if (!Qs.hasNonFastQualifiers()) 1436 return T.withFastQualifiers(Qs.getFastQualifiers()); 1437 QualifierCollector Qc(Qs); 1438 const Type *Ptr = Qc.strip(T); 1439 return getExtQualType(Ptr, Qc); 1440 } 1441 1442 /// \brief Return a type with additional qualifiers. 1443 QualType getQualifiedType(const Type *T, Qualifiers Qs) const { 1444 if (!Qs.hasNonFastQualifiers()) 1445 return QualType(T, Qs.getFastQualifiers()); 1446 return getExtQualType(T, Qs); 1447 } 1448 1449 /// \brief Return a type with the given lifetime qualifier. 1450 /// 1451 /// \pre Neither type.ObjCLifetime() nor \p lifetime may be \c OCL_None. 1452 QualType getLifetimeQualifiedType(QualType type, 1453 Qualifiers::ObjCLifetime lifetime) { 1454 assert(type.getObjCLifetime() == Qualifiers::OCL_None); 1455 assert(lifetime != Qualifiers::OCL_None); 1456 1457 Qualifiers qs; 1458 qs.addObjCLifetime(lifetime); 1459 return getQualifiedType(type, qs); 1460 } 1461 1462 /// getUnqualifiedObjCPointerType - Returns version of 1463 /// Objective-C pointer type with lifetime qualifier removed. 1464 QualType getUnqualifiedObjCPointerType(QualType type) const { 1465 if (!type.getTypePtr()->isObjCObjectPointerType() || 1466 !type.getQualifiers().hasObjCLifetime()) 1467 return type; 1468 Qualifiers Qs = type.getQualifiers(); 1469 Qs.removeObjCLifetime(); 1470 return getQualifiedType(type.getUnqualifiedType(), Qs); 1471 } 1472 1473 DeclarationNameInfo getNameForTemplate(TemplateName Name, 1474 SourceLocation NameLoc) const; 1475 1476 TemplateName getOverloadedTemplateName(UnresolvedSetIterator Begin, 1477 UnresolvedSetIterator End) const; 1478 1479 TemplateName getQualifiedTemplateName(NestedNameSpecifier *NNS, 1480 bool TemplateKeyword, 1481 TemplateDecl *Template) const; 1482 1483 TemplateName getDependentTemplateName(NestedNameSpecifier *NNS, 1484 const IdentifierInfo *Name) const; 1485 TemplateName getDependentTemplateName(NestedNameSpecifier *NNS, 1486 OverloadedOperatorKind Operator) const; 1487 TemplateName getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param, 1488 TemplateName replacement) const; 1489 TemplateName getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param, 1490 const TemplateArgument &ArgPack) const; 1491 1492 enum GetBuiltinTypeError { 1493 GE_None, ///< No error 1494 GE_Missing_stdio, ///< Missing a type from <stdio.h> 1495 GE_Missing_setjmp, ///< Missing a type from <setjmp.h> 1496 GE_Missing_ucontext ///< Missing a type from <ucontext.h> 1497 }; 1498 1499 /// \brief Return the type for the specified builtin. 1500 /// 1501 /// If \p IntegerConstantArgs is non-null, it is filled in with a bitmask of 1502 /// arguments to the builtin that are required to be integer constant 1503 /// expressions. 1504 QualType GetBuiltinType(unsigned ID, GetBuiltinTypeError &Error, 1505 unsigned *IntegerConstantArgs = 0) const; 1506 1507private: 1508 CanQualType getFromTargetType(unsigned Type) const; 1509 std::pair<uint64_t, unsigned> getTypeInfoImpl(const Type *T) const; 1510 1511 //===--------------------------------------------------------------------===// 1512 // Type Predicates. 1513 //===--------------------------------------------------------------------===// 1514 1515public: 1516 /// \brief Return one of the GCNone, Weak or Strong Objective-C garbage 1517 /// collection attributes. 1518 Qualifiers::GC getObjCGCAttrKind(QualType Ty) const; 1519 1520 /// \brief Return true if the given vector types are of the same unqualified 1521 /// type or if they are equivalent to the same GCC vector type. 1522 /// 1523 /// \note This ignores whether they are target-specific (AltiVec or Neon) 1524 /// types. 1525 bool areCompatibleVectorTypes(QualType FirstVec, QualType SecondVec); 1526 1527 /// \brief Return true if this is an \c NSObject object with its \c NSObject 1528 /// attribute set. 1529 static bool isObjCNSObjectType(QualType Ty) { 1530 return Ty->isObjCNSObjectType(); 1531 } 1532 1533 //===--------------------------------------------------------------------===// 1534 // Type Sizing and Analysis 1535 //===--------------------------------------------------------------------===// 1536 1537 /// \brief Return the APFloat 'semantics' for the specified scalar floating 1538 /// point type. 1539 const llvm::fltSemantics &getFloatTypeSemantics(QualType T) const; 1540 1541 /// \brief Get the size and alignment of the specified complete type in bits. 1542 std::pair<uint64_t, unsigned> getTypeInfo(const Type *T) const; 1543 std::pair<uint64_t, unsigned> getTypeInfo(QualType T) const { 1544 return getTypeInfo(T.getTypePtr()); 1545 } 1546 1547 /// \brief Return the size of the specified (complete) type \p T, in bits. 1548 uint64_t getTypeSize(QualType T) const { 1549 return getTypeInfo(T).first; 1550 } 1551 uint64_t getTypeSize(const Type *T) const { 1552 return getTypeInfo(T).first; 1553 } 1554 1555 /// \brief Return the size of the character type, in bits. 1556 uint64_t getCharWidth() const { 1557 return getTypeSize(CharTy); 1558 } 1559 1560 /// \brief Convert a size in bits to a size in characters. 1561 CharUnits toCharUnitsFromBits(int64_t BitSize) const; 1562 1563 /// \brief Convert a size in characters to a size in bits. 1564 int64_t toBits(CharUnits CharSize) const; 1565 1566 /// \brief Return the size of the specified (complete) type \p T, in 1567 /// characters. 1568 CharUnits getTypeSizeInChars(QualType T) const; 1569 CharUnits getTypeSizeInChars(const Type *T) const; 1570 1571 /// \brief Return the ABI-specified alignment of a (complete) type \p T, in 1572 /// bits. 1573 unsigned getTypeAlign(QualType T) const { 1574 return getTypeInfo(T).second; 1575 } 1576 unsigned getTypeAlign(const Type *T) const { 1577 return getTypeInfo(T).second; 1578 } 1579 1580 /// \brief Return the ABI-specified alignment of a (complete) type \p T, in 1581 /// characters. 1582 CharUnits getTypeAlignInChars(QualType T) const; 1583 CharUnits getTypeAlignInChars(const Type *T) const; 1584 1585 // getTypeInfoDataSizeInChars - Return the size of a type, in chars. If the 1586 // type is a record, its data size is returned. 1587 std::pair<CharUnits, CharUnits> getTypeInfoDataSizeInChars(QualType T) const; 1588 1589 std::pair<CharUnits, CharUnits> getTypeInfoInChars(const Type *T) const; 1590 std::pair<CharUnits, CharUnits> getTypeInfoInChars(QualType T) const; 1591 1592 /// \brief Return the "preferred" alignment of the specified type \p T for 1593 /// the current target, in bits. 1594 /// 1595 /// This can be different than the ABI alignment in cases where it is 1596 /// beneficial for performance to overalign a data type. 1597 unsigned getPreferredTypeAlign(const Type *T) const; 1598 1599 /// \brief Return the alignment in bits that should be given to a 1600 /// global variable with type \p T. 1601 unsigned getAlignOfGlobalVar(QualType T) const; 1602 1603 /// \brief Return the alignment in characters that should be given to a 1604 /// global variable with type \p T. 1605 CharUnits getAlignOfGlobalVarInChars(QualType T) const; 1606 1607 /// \brief Return a conservative estimate of the alignment of the specified 1608 /// decl \p D. 1609 /// 1610 /// \pre \p D must not be a bitfield type, as bitfields do not have a valid 1611 /// alignment. 1612 /// 1613 /// If \p RefAsPointee, references are treated like their underlying type 1614 /// (for alignof), else they're treated like pointers (for CodeGen). 1615 CharUnits getDeclAlign(const Decl *D, bool RefAsPointee = false) const; 1616 1617 /// \brief Get or compute information about the layout of the specified 1618 /// record (struct/union/class) \p D, which indicates its size and field 1619 /// position information. 1620 const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D) const; 1621 1622 /// \brief Get or compute information about the layout of the specified 1623 /// Objective-C interface. 1624 const ASTRecordLayout &getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) 1625 const; 1626 1627 void DumpRecordLayout(const RecordDecl *RD, raw_ostream &OS, 1628 bool Simple = false) const; 1629 1630 /// \brief Get or compute information about the layout of the specified 1631 /// Objective-C implementation. 1632 /// 1633 /// This may differ from the interface if synthesized ivars are present. 1634 const ASTRecordLayout & 1635 getASTObjCImplementationLayout(const ObjCImplementationDecl *D) const; 1636 1637 /// \brief Get our current best idea for the key function of the 1638 /// given record decl, or NULL if there isn't one. 1639 /// 1640 /// The key function is, according to the Itanium C++ ABI section 5.2.3: 1641 /// ...the first non-pure virtual function that is not inline at the 1642 /// point of class definition. 1643 /// 1644 /// Other ABIs use the same idea. However, the ARM C++ ABI ignores 1645 /// virtual functions that are defined 'inline', which means that 1646 /// the result of this computation can change. 1647 const CXXMethodDecl *getCurrentKeyFunction(const CXXRecordDecl *RD); 1648 1649 /// \brief Observe that the given method cannot be a key function. 1650 /// Checks the key-function cache for the method's class and clears it 1651 /// if matches the given declaration. 1652 /// 1653 /// This is used in ABIs where out-of-line definitions marked 1654 /// inline are not considered to be key functions. 1655 /// 1656 /// \param method should be the declaration from the class definition 1657 void setNonKeyFunction(const CXXMethodDecl *method); 1658 1659 /// Get the offset of a FieldDecl or IndirectFieldDecl, in bits. 1660 uint64_t getFieldOffset(const ValueDecl *FD) const; 1661 1662 bool isNearlyEmpty(const CXXRecordDecl *RD) const; 1663 1664 MangleContext *createMangleContext(); 1665 1666 void DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, bool leafClass, 1667 SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const; 1668 1669 unsigned CountNonClassIvars(const ObjCInterfaceDecl *OI) const; 1670 void CollectInheritedProtocols(const Decl *CDecl, 1671 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols); 1672 1673 //===--------------------------------------------------------------------===// 1674 // Type Operators 1675 //===--------------------------------------------------------------------===// 1676 1677 /// \brief Return the canonical (structural) type corresponding to the 1678 /// specified potentially non-canonical type \p T. 1679 /// 1680 /// The non-canonical version of a type may have many "decorated" versions of 1681 /// types. Decorators can include typedefs, 'typeof' operators, etc. The 1682 /// returned type is guaranteed to be free of any of these, allowing two 1683 /// canonical types to be compared for exact equality with a simple pointer 1684 /// comparison. 1685 CanQualType getCanonicalType(QualType T) const { 1686 return CanQualType::CreateUnsafe(T.getCanonicalType()); 1687 } 1688 1689 const Type *getCanonicalType(const Type *T) const { 1690 return T->getCanonicalTypeInternal().getTypePtr(); 1691 } 1692 1693 /// \brief Return the canonical parameter type corresponding to the specific 1694 /// potentially non-canonical one. 1695 /// 1696 /// Qualifiers are stripped off, functions are turned into function 1697 /// pointers, and arrays decay one level into pointers. 1698 CanQualType getCanonicalParamType(QualType T) const; 1699 1700 /// \brief Determine whether the given types \p T1 and \p T2 are equivalent. 1701 bool hasSameType(QualType T1, QualType T2) const { 1702 return getCanonicalType(T1) == getCanonicalType(T2); 1703 } 1704 1705 /// \brief Return this type as a completely-unqualified array type, 1706 /// capturing the qualifiers in \p Quals. 1707 /// 1708 /// This will remove the minimal amount of sugaring from the types, similar 1709 /// to the behavior of QualType::getUnqualifiedType(). 1710 /// 1711 /// \param T is the qualified type, which may be an ArrayType 1712 /// 1713 /// \param Quals will receive the full set of qualifiers that were 1714 /// applied to the array. 1715 /// 1716 /// \returns if this is an array type, the completely unqualified array type 1717 /// that corresponds to it. Otherwise, returns T.getUnqualifiedType(). 1718 QualType getUnqualifiedArrayType(QualType T, Qualifiers &Quals); 1719 1720 /// \brief Determine whether the given types are equivalent after 1721 /// cvr-qualifiers have been removed. 1722 bool hasSameUnqualifiedType(QualType T1, QualType T2) const { 1723 return getCanonicalType(T1).getTypePtr() == 1724 getCanonicalType(T2).getTypePtr(); 1725 } 1726 1727 bool UnwrapSimilarPointerTypes(QualType &T1, QualType &T2); 1728 1729 /// \brief Retrieves the "canonical" nested name specifier for a 1730 /// given nested name specifier. 1731 /// 1732 /// The canonical nested name specifier is a nested name specifier 1733 /// that uniquely identifies a type or namespace within the type 1734 /// system. For example, given: 1735 /// 1736 /// \code 1737 /// namespace N { 1738 /// struct S { 1739 /// template<typename T> struct X { typename T* type; }; 1740 /// }; 1741 /// } 1742 /// 1743 /// template<typename T> struct Y { 1744 /// typename N::S::X<T>::type member; 1745 /// }; 1746 /// \endcode 1747 /// 1748 /// Here, the nested-name-specifier for N::S::X<T>:: will be 1749 /// S::X<template-param-0-0>, since 'S' and 'X' are uniquely defined 1750 /// by declarations in the type system and the canonical type for 1751 /// the template type parameter 'T' is template-param-0-0. 1752 NestedNameSpecifier * 1753 getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const; 1754 1755 /// \brief Retrieves the default calling convention to use for 1756 /// C++ instance methods. 1757 CallingConv getDefaultCXXMethodCallConv(bool isVariadic); 1758 1759 /// \brief Retrieves the canonical representation of the given 1760 /// calling convention. 1761 CallingConv getCanonicalCallConv(CallingConv CC) const; 1762 1763 /// \brief Determines whether two calling conventions name the same 1764 /// calling convention. 1765 bool isSameCallConv(CallingConv lcc, CallingConv rcc) { 1766 return (getCanonicalCallConv(lcc) == getCanonicalCallConv(rcc)); 1767 } 1768 1769 /// \brief Retrieves the "canonical" template name that refers to a 1770 /// given template. 1771 /// 1772 /// The canonical template name is the simplest expression that can 1773 /// be used to refer to a given template. For most templates, this 1774 /// expression is just the template declaration itself. For example, 1775 /// the template std::vector can be referred to via a variety of 1776 /// names---std::vector, \::std::vector, vector (if vector is in 1777 /// scope), etc.---but all of these names map down to the same 1778 /// TemplateDecl, which is used to form the canonical template name. 1779 /// 1780 /// Dependent template names are more interesting. Here, the 1781 /// template name could be something like T::template apply or 1782 /// std::allocator<T>::template rebind, where the nested name 1783 /// specifier itself is dependent. In this case, the canonical 1784 /// template name uses the shortest form of the dependent 1785 /// nested-name-specifier, which itself contains all canonical 1786 /// types, values, and templates. 1787 TemplateName getCanonicalTemplateName(TemplateName Name) const; 1788 1789 /// \brief Determine whether the given template names refer to the same 1790 /// template. 1791 bool hasSameTemplateName(TemplateName X, TemplateName Y); 1792 1793 /// \brief Retrieve the "canonical" template argument. 1794 /// 1795 /// The canonical template argument is the simplest template argument 1796 /// (which may be a type, value, expression, or declaration) that 1797 /// expresses the value of the argument. 1798 TemplateArgument getCanonicalTemplateArgument(const TemplateArgument &Arg) 1799 const; 1800 1801 /// Type Query functions. If the type is an instance of the specified class, 1802 /// return the Type pointer for the underlying maximally pretty type. This 1803 /// is a member of ASTContext because this may need to do some amount of 1804 /// canonicalization, e.g. to move type qualifiers into the element type. 1805 const ArrayType *getAsArrayType(QualType T) const; 1806 const ConstantArrayType *getAsConstantArrayType(QualType T) const { 1807 return dyn_cast_or_null<ConstantArrayType>(getAsArrayType(T)); 1808 } 1809 const VariableArrayType *getAsVariableArrayType(QualType T) const { 1810 return dyn_cast_or_null<VariableArrayType>(getAsArrayType(T)); 1811 } 1812 const IncompleteArrayType *getAsIncompleteArrayType(QualType T) const { 1813 return dyn_cast_or_null<IncompleteArrayType>(getAsArrayType(T)); 1814 } 1815 const DependentSizedArrayType *getAsDependentSizedArrayType(QualType T) 1816 const { 1817 return dyn_cast_or_null<DependentSizedArrayType>(getAsArrayType(T)); 1818 } 1819 1820 /// \brief Return the innermost element type of an array type. 1821 /// 1822 /// For example, will return "int" for int[m][n] 1823 QualType getBaseElementType(const ArrayType *VAT) const; 1824 1825 /// \brief Return the innermost element type of a type (which needn't 1826 /// actually be an array type). 1827 QualType getBaseElementType(QualType QT) const; 1828 1829 /// \brief Return number of constant array elements. 1830 uint64_t getConstantArrayElementCount(const ConstantArrayType *CA) const; 1831 1832 /// \brief Perform adjustment on the parameter type of a function. 1833 /// 1834 /// This routine adjusts the given parameter type @p T to the actual 1835 /// parameter type used by semantic analysis (C99 6.7.5.3p[7,8], 1836 /// C++ [dcl.fct]p3). The adjusted parameter type is returned. 1837 QualType getAdjustedParameterType(QualType T) const; 1838 1839 /// \brief Retrieve the parameter type as adjusted for use in the signature 1840 /// of a function, decaying array and function types and removing top-level 1841 /// cv-qualifiers. 1842 QualType getSignatureParameterType(QualType T) const; 1843 1844 /// \brief Return the properly qualified result of decaying the specified 1845 /// array type to a pointer. 1846 /// 1847 /// This operation is non-trivial when handling typedefs etc. The canonical 1848 /// type of \p T must be an array type, this returns a pointer to a properly 1849 /// qualified element of the array. 1850 /// 1851 /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. 1852 QualType getArrayDecayedType(QualType T) const; 1853 1854 /// \brief Return the type that \p PromotableType will promote to: C99 1855 /// 6.3.1.1p2, assuming that \p PromotableType is a promotable integer type. 1856 QualType getPromotedIntegerType(QualType PromotableType) const; 1857 1858 /// \brief Recurses in pointer/array types until it finds an Objective-C 1859 /// retainable type and returns its ownership. 1860 Qualifiers::ObjCLifetime getInnerObjCOwnership(QualType T) const; 1861 1862 /// \brief Whether this is a promotable bitfield reference according 1863 /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). 1864 /// 1865 /// \returns the type this bit-field will promote to, or NULL if no 1866 /// promotion occurs. 1867 QualType isPromotableBitField(Expr *E) const; 1868 1869 /// \brief Return the highest ranked integer type, see C99 6.3.1.8p1. 1870 /// 1871 /// If \p LHS > \p RHS, returns 1. If \p LHS == \p RHS, returns 0. If 1872 /// \p LHS < \p RHS, return -1. 1873 int getIntegerTypeOrder(QualType LHS, QualType RHS) const; 1874 1875 /// \brief Compare the rank of the two specified floating point types, 1876 /// ignoring the domain of the type (i.e. 'double' == '_Complex double'). 1877 /// 1878 /// If \p LHS > \p RHS, returns 1. If \p LHS == \p RHS, returns 0. If 1879 /// \p LHS < \p RHS, return -1. 1880 int getFloatingTypeOrder(QualType LHS, QualType RHS) const; 1881 1882 /// \brief Return a real floating point or a complex type (based on 1883 /// \p typeDomain/\p typeSize). 1884 /// 1885 /// \param typeDomain a real floating point or complex type. 1886 /// \param typeSize a real floating point or complex type. 1887 QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize, 1888 QualType typeDomain) const; 1889 1890 unsigned getTargetAddressSpace(QualType T) const { 1891 return getTargetAddressSpace(T.getQualifiers()); 1892 } 1893 1894 unsigned getTargetAddressSpace(Qualifiers Q) const { 1895 return getTargetAddressSpace(Q.getAddressSpace()); 1896 } 1897 1898 unsigned getTargetAddressSpace(unsigned AS) const { 1899 if (AS < LangAS::Offset || AS >= LangAS::Offset + LangAS::Count) 1900 return AS; 1901 else 1902 return (*AddrSpaceMap)[AS - LangAS::Offset]; 1903 } 1904 1905private: 1906 // Helper for integer ordering 1907 unsigned getIntegerRank(const Type *T) const; 1908 1909public: 1910 1911 //===--------------------------------------------------------------------===// 1912 // Type Compatibility Predicates 1913 //===--------------------------------------------------------------------===// 1914 1915 /// Compatibility predicates used to check assignment expressions. 1916 bool typesAreCompatible(QualType T1, QualType T2, 1917 bool CompareUnqualified = false); // C99 6.2.7p1 1918 1919 bool propertyTypesAreCompatible(QualType, QualType); 1920 bool typesAreBlockPointerCompatible(QualType, QualType); 1921 1922 bool isObjCIdType(QualType T) const { 1923 return T == getObjCIdType(); 1924 } 1925 bool isObjCClassType(QualType T) const { 1926 return T == getObjCClassType(); 1927 } 1928 bool isObjCSelType(QualType T) const { 1929 return T == getObjCSelType(); 1930 } 1931 bool QualifiedIdConformsQualifiedId(QualType LHS, QualType RHS); 1932 bool ObjCQualifiedIdTypesAreCompatible(QualType LHS, QualType RHS, 1933 bool ForCompare); 1934 1935 bool ObjCQualifiedClassTypesAreCompatible(QualType LHS, QualType RHS); 1936 1937 // Check the safety of assignment from LHS to RHS 1938 bool canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, 1939 const ObjCObjectPointerType *RHSOPT); 1940 bool canAssignObjCInterfaces(const ObjCObjectType *LHS, 1941 const ObjCObjectType *RHS); 1942 bool canAssignObjCInterfacesInBlockPointer( 1943 const ObjCObjectPointerType *LHSOPT, 1944 const ObjCObjectPointerType *RHSOPT, 1945 bool BlockReturnType); 1946 bool areComparableObjCPointerTypes(QualType LHS, QualType RHS); 1947 QualType areCommonBaseCompatible(const ObjCObjectPointerType *LHSOPT, 1948 const ObjCObjectPointerType *RHSOPT); 1949 bool canBindObjCObjectType(QualType To, QualType From); 1950 1951 // Functions for calculating composite types 1952 QualType mergeTypes(QualType, QualType, bool OfBlockPointer=false, 1953 bool Unqualified = false, bool BlockReturnType = false); 1954 QualType mergeFunctionTypes(QualType, QualType, bool OfBlockPointer=false, 1955 bool Unqualified = false); 1956 QualType mergeFunctionArgumentTypes(QualType, QualType, 1957 bool OfBlockPointer=false, 1958 bool Unqualified = false); 1959 QualType mergeTransparentUnionType(QualType, QualType, 1960 bool OfBlockPointer=false, 1961 bool Unqualified = false); 1962 1963 QualType mergeObjCGCQualifiers(QualType, QualType); 1964 1965 bool FunctionTypesMatchOnNSConsumedAttrs( 1966 const FunctionProtoType *FromFunctionType, 1967 const FunctionProtoType *ToFunctionType); 1968 1969 void ResetObjCLayout(const ObjCContainerDecl *CD) { 1970 ObjCLayouts[CD] = 0; 1971 } 1972 1973 //===--------------------------------------------------------------------===// 1974 // Integer Predicates 1975 //===--------------------------------------------------------------------===// 1976 1977 // The width of an integer, as defined in C99 6.2.6.2. This is the number 1978 // of bits in an integer type excluding any padding bits. 1979 unsigned getIntWidth(QualType T) const; 1980 1981 // Per C99 6.2.5p6, for every signed integer type, there is a corresponding 1982 // unsigned integer type. This method takes a signed type, and returns the 1983 // corresponding unsigned integer type. 1984 QualType getCorrespondingUnsignedType(QualType T) const; 1985 1986 //===--------------------------------------------------------------------===// 1987 // Type Iterators. 1988 //===--------------------------------------------------------------------===// 1989 1990 typedef SmallVectorImpl<Type *>::iterator type_iterator; 1991 typedef SmallVectorImpl<Type *>::const_iterator const_type_iterator; 1992 1993 type_iterator types_begin() { return Types.begin(); } 1994 type_iterator types_end() { return Types.end(); } 1995 const_type_iterator types_begin() const { return Types.begin(); } 1996 const_type_iterator types_end() const { return Types.end(); } 1997 1998 //===--------------------------------------------------------------------===// 1999 // Integer Values 2000 //===--------------------------------------------------------------------===// 2001 2002 /// \brief Make an APSInt of the appropriate width and signedness for the 2003 /// given \p Value and integer \p Type. 2004 llvm::APSInt MakeIntValue(uint64_t Value, QualType Type) const { 2005 llvm::APSInt Res(getIntWidth(Type), 2006 !Type->isSignedIntegerOrEnumerationType()); 2007 Res = Value; 2008 return Res; 2009 } 2010 2011 bool isSentinelNullExpr(const Expr *E); 2012 2013 /// \brief Get the implementation of the ObjCInterfaceDecl \p D, or NULL if 2014 /// none exists. 2015 ObjCImplementationDecl *getObjCImplementation(ObjCInterfaceDecl *D); 2016 /// \brief Get the implementation of the ObjCCategoryDecl \p D, or NULL if 2017 /// none exists. 2018 ObjCCategoryImplDecl *getObjCImplementation(ObjCCategoryDecl *D); 2019 2020 /// \brief Return true if there is at least one \@implementation in the TU. 2021 bool AnyObjCImplementation() { 2022 return !ObjCImpls.empty(); 2023 } 2024 2025 /// \brief Set the implementation of ObjCInterfaceDecl. 2026 void setObjCImplementation(ObjCInterfaceDecl *IFaceD, 2027 ObjCImplementationDecl *ImplD); 2028 /// \brief Set the implementation of ObjCCategoryDecl. 2029 void setObjCImplementation(ObjCCategoryDecl *CatD, 2030 ObjCCategoryImplDecl *ImplD); 2031 2032 /// \brief Get the duplicate declaration of a ObjCMethod in the same 2033 /// interface, or null if none exists. 2034 const ObjCMethodDecl *getObjCMethodRedeclaration( 2035 const ObjCMethodDecl *MD) const { 2036 return ObjCMethodRedecls.lookup(MD); 2037 } 2038 2039 void setObjCMethodRedeclaration(const ObjCMethodDecl *MD, 2040 const ObjCMethodDecl *Redecl) { 2041 assert(!getObjCMethodRedeclaration(MD) && "MD already has a redeclaration"); 2042 ObjCMethodRedecls[MD] = Redecl; 2043 } 2044 2045 /// \brief Returns the Objective-C interface that \p ND belongs to if it is 2046 /// an Objective-C method/property/ivar etc. that is part of an interface, 2047 /// otherwise returns null. 2048 const ObjCInterfaceDecl *getObjContainingInterface(const NamedDecl *ND) const; 2049 2050 /// \brief Set the copy inialization expression of a block var decl. 2051 void setBlockVarCopyInits(VarDecl*VD, Expr* Init); 2052 /// \brief Get the copy initialization expression of the VarDecl \p VD, or 2053 /// NULL if none exists. 2054 Expr *getBlockVarCopyInits(const VarDecl* VD); 2055 2056 /// \brief Allocate an uninitialized TypeSourceInfo. 2057 /// 2058 /// The caller should initialize the memory held by TypeSourceInfo using 2059 /// the TypeLoc wrappers. 2060 /// 2061 /// \param T the type that will be the basis for type source info. This type 2062 /// should refer to how the declarator was written in source code, not to 2063 /// what type semantic analysis resolved the declarator to. 2064 /// 2065 /// \param Size the size of the type info to create, or 0 if the size 2066 /// should be calculated based on the type. 2067 TypeSourceInfo *CreateTypeSourceInfo(QualType T, unsigned Size = 0) const; 2068 2069 /// \brief Allocate a TypeSourceInfo where all locations have been 2070 /// initialized to a given location, which defaults to the empty 2071 /// location. 2072 TypeSourceInfo * 2073 getTrivialTypeSourceInfo(QualType T, 2074 SourceLocation Loc = SourceLocation()) const; 2075 2076 TypeSourceInfo *getNullTypeSourceInfo() { return &NullTypeSourceInfo; } 2077 2078 /// \brief Add a deallocation callback that will be invoked when the 2079 /// ASTContext is destroyed. 2080 /// 2081 /// \param Callback A callback function that will be invoked on destruction. 2082 /// 2083 /// \param Data Pointer data that will be provided to the callback function 2084 /// when it is called. 2085 void AddDeallocation(void (*Callback)(void*), void *Data); 2086 2087 GVALinkage GetGVALinkageForFunction(const FunctionDecl *FD); 2088 GVALinkage GetGVALinkageForVariable(const VarDecl *VD); 2089 2090 /// \brief Determines if the decl can be CodeGen'ed or deserialized from PCH 2091 /// lazily, only when used; this is only relevant for function or file scoped 2092 /// var definitions. 2093 /// 2094 /// \returns true if the function/var must be CodeGen'ed/deserialized even if 2095 /// it is not used. 2096 bool DeclMustBeEmitted(const Decl *D); 2097 2098 void addUnnamedTag(const TagDecl *Tag); 2099 int getUnnamedTagManglingNumber(const TagDecl *Tag) const; 2100 2101 /// \brief Retrieve the lambda mangling number for a lambda expression. 2102 unsigned getLambdaManglingNumber(CXXMethodDecl *CallOperator); 2103 2104 /// \brief Used by ParmVarDecl to store on the side the 2105 /// index of the parameter when it exceeds the size of the normal bitfield. 2106 void setParameterIndex(const ParmVarDecl *D, unsigned index); 2107 2108 /// \brief Used by ParmVarDecl to retrieve on the side the 2109 /// index of the parameter when it exceeds the size of the normal bitfield. 2110 unsigned getParameterIndex(const ParmVarDecl *D) const; 2111 2112 /// \brief Get the storage for the constant value of a materialized temporary 2113 /// of static storage duration. 2114 APValue *getMaterializedTemporaryValue(const MaterializeTemporaryExpr *E, 2115 bool MayCreate); 2116 2117 //===--------------------------------------------------------------------===// 2118 // Statistics 2119 //===--------------------------------------------------------------------===// 2120 2121 /// \brief The number of implicitly-declared default constructors. 2122 static unsigned NumImplicitDefaultConstructors; 2123 2124 /// \brief The number of implicitly-declared default constructors for 2125 /// which declarations were built. 2126 static unsigned NumImplicitDefaultConstructorsDeclared; 2127 2128 /// \brief The number of implicitly-declared copy constructors. 2129 static unsigned NumImplicitCopyConstructors; 2130 2131 /// \brief The number of implicitly-declared copy constructors for 2132 /// which declarations were built. 2133 static unsigned NumImplicitCopyConstructorsDeclared; 2134 2135 /// \brief The number of implicitly-declared move constructors. 2136 static unsigned NumImplicitMoveConstructors; 2137 2138 /// \brief The number of implicitly-declared move constructors for 2139 /// which declarations were built. 2140 static unsigned NumImplicitMoveConstructorsDeclared; 2141 2142 /// \brief The number of implicitly-declared copy assignment operators. 2143 static unsigned NumImplicitCopyAssignmentOperators; 2144 2145 /// \brief The number of implicitly-declared copy assignment operators for 2146 /// which declarations were built. 2147 static unsigned NumImplicitCopyAssignmentOperatorsDeclared; 2148 2149 /// \brief The number of implicitly-declared move assignment operators. 2150 static unsigned NumImplicitMoveAssignmentOperators; 2151 2152 /// \brief The number of implicitly-declared move assignment operators for 2153 /// which declarations were built. 2154 static unsigned NumImplicitMoveAssignmentOperatorsDeclared; 2155 2156 /// \brief The number of implicitly-declared destructors. 2157 static unsigned NumImplicitDestructors; 2158 2159 /// \brief The number of implicitly-declared destructors for which 2160 /// declarations were built. 2161 static unsigned NumImplicitDestructorsDeclared; 2162 2163private: 2164 ASTContext(const ASTContext &) LLVM_DELETED_FUNCTION; 2165 void operator=(const ASTContext &) LLVM_DELETED_FUNCTION; 2166 2167public: 2168 /// \brief Initialize built-in types. 2169 /// 2170 /// This routine may only be invoked once for a given ASTContext object. 2171 /// It is normally invoked by the ASTContext constructor. However, the 2172 /// constructor can be asked to delay initialization, which places the burden 2173 /// of calling this function on the user of that object. 2174 /// 2175 /// \param Target The target 2176 void InitBuiltinTypes(const TargetInfo &Target); 2177 2178private: 2179 void InitBuiltinType(CanQualType &R, BuiltinType::Kind K); 2180 2181 // Return the Objective-C type encoding for a given type. 2182 void getObjCEncodingForTypeImpl(QualType t, std::string &S, 2183 bool ExpandPointedToStructures, 2184 bool ExpandStructures, 2185 const FieldDecl *Field, 2186 bool OutermostType = false, 2187 bool EncodingProperty = false, 2188 bool StructField = false, 2189 bool EncodeBlockParameters = false, 2190 bool EncodeClassNames = false, 2191 bool EncodePointerToObjCTypedef = false) const; 2192 2193 // Adds the encoding of the structure's members. 2194 void getObjCEncodingForStructureImpl(RecordDecl *RD, std::string &S, 2195 const FieldDecl *Field, 2196 bool includeVBases = true) const; 2197 2198 // Adds the encoding of a method parameter or return type. 2199 void getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT, 2200 QualType T, std::string& S, 2201 bool Extended) const; 2202 2203 const ASTRecordLayout & 2204 getObjCLayout(const ObjCInterfaceDecl *D, 2205 const ObjCImplementationDecl *Impl) const; 2206 2207private: 2208 /// \brief A set of deallocations that should be performed when the 2209 /// ASTContext is destroyed. 2210 typedef llvm::SmallDenseMap<void(*)(void*), llvm::SmallVector<void*, 16> > 2211 DeallocationMap; 2212 DeallocationMap Deallocations; 2213 2214 // FIXME: This currently contains the set of StoredDeclMaps used 2215 // by DeclContext objects. This probably should not be in ASTContext, 2216 // but we include it here so that ASTContext can quickly deallocate them. 2217 llvm::PointerIntPair<StoredDeclsMap*,1> LastSDM; 2218 2219 friend class DeclContext; 2220 friend class DeclarationNameTable; 2221 void ReleaseDeclContextMaps(); 2222 2223 llvm::OwningPtr<ParentMap> AllParents; 2224}; 2225 2226/// \brief Utility function for constructing a nullary selector. 2227static inline Selector GetNullarySelector(StringRef name, ASTContext& Ctx) { 2228 IdentifierInfo* II = &Ctx.Idents.get(name); 2229 return Ctx.Selectors.getSelector(0, &II); 2230} 2231 2232/// \brief Utility function for constructing an unary selector. 2233static inline Selector GetUnarySelector(StringRef name, ASTContext& Ctx) { 2234 IdentifierInfo* II = &Ctx.Idents.get(name); 2235 return Ctx.Selectors.getSelector(1, &II); 2236} 2237 2238} // end namespace clang 2239 2240// operator new and delete aren't allowed inside namespaces. 2241 2242/// @brief Placement new for using the ASTContext's allocator. 2243/// 2244/// This placement form of operator new uses the ASTContext's allocator for 2245/// obtaining memory. 2246/// 2247/// IMPORTANT: These are also declared in clang/AST/AttrIterator.h! Any changes 2248/// here need to also be made there. 2249/// 2250/// We intentionally avoid using a nothrow specification here so that the calls 2251/// to this operator will not perform a null check on the result -- the 2252/// underlying allocator never returns null pointers. 2253/// 2254/// Usage looks like this (assuming there's an ASTContext 'Context' in scope): 2255/// @code 2256/// // Default alignment (8) 2257/// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments); 2258/// // Specific alignment 2259/// IntegerLiteral *Ex2 = new (Context, 4) IntegerLiteral(arguments); 2260/// @endcode 2261/// Please note that you cannot use delete on the pointer; it must be 2262/// deallocated using an explicit destructor call followed by 2263/// @c Context.Deallocate(Ptr). 2264/// 2265/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 2266/// @param C The ASTContext that provides the allocator. 2267/// @param Alignment The alignment of the allocated memory (if the underlying 2268/// allocator supports it). 2269/// @return The allocated memory. Could be NULL. 2270inline void *operator new(size_t Bytes, const clang::ASTContext &C, 2271 size_t Alignment) { 2272 return C.Allocate(Bytes, Alignment); 2273} 2274/// @brief Placement delete companion to the new above. 2275/// 2276/// This operator is just a companion to the new above. There is no way of 2277/// invoking it directly; see the new operator for more details. This operator 2278/// is called implicitly by the compiler if a placement new expression using 2279/// the ASTContext throws in the object constructor. 2280inline void operator delete(void *Ptr, const clang::ASTContext &C, size_t) { 2281 C.Deallocate(Ptr); 2282} 2283 2284/// This placement form of operator new[] uses the ASTContext's allocator for 2285/// obtaining memory. 2286/// 2287/// We intentionally avoid using a nothrow specification here so that the calls 2288/// to this operator will not perform a null check on the result -- the 2289/// underlying allocator never returns null pointers. 2290/// 2291/// Usage looks like this (assuming there's an ASTContext 'Context' in scope): 2292/// @code 2293/// // Default alignment (8) 2294/// char *data = new (Context) char[10]; 2295/// // Specific alignment 2296/// char *data = new (Context, 4) char[10]; 2297/// @endcode 2298/// Please note that you cannot use delete on the pointer; it must be 2299/// deallocated using an explicit destructor call followed by 2300/// @c Context.Deallocate(Ptr). 2301/// 2302/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 2303/// @param C The ASTContext that provides the allocator. 2304/// @param Alignment The alignment of the allocated memory (if the underlying 2305/// allocator supports it). 2306/// @return The allocated memory. Could be NULL. 2307inline void *operator new[](size_t Bytes, const clang::ASTContext& C, 2308 size_t Alignment = 8) { 2309 return C.Allocate(Bytes, Alignment); 2310} 2311 2312/// @brief Placement delete[] companion to the new[] above. 2313/// 2314/// This operator is just a companion to the new[] above. There is no way of 2315/// invoking it directly; see the new[] operator for more details. This operator 2316/// is called implicitly by the compiler if a placement new[] expression using 2317/// the ASTContext throws in the object constructor. 2318inline void operator delete[](void *Ptr, const clang::ASTContext &C, size_t) { 2319 C.Deallocate(Ptr); 2320} 2321 2322#endif 2323