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