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