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