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