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