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