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