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