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