ASTContext.h revision d5617eeafc93209a26b9f88276c88cf997c3a0a7
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 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 the unique type for "ptrdiff_t" (C99 7.17) defined in 1112 /// <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). 1113 QualType getPointerDiffType() const; 1114 1115 /// \brief Return the unique type for "pid_t" defined in 1116 /// <sys/types.h>. We need this to compute the correct type for vfork(). 1117 QualType getProcessIDType() const; 1118 1119 /// \brief Return the C structure type used to represent constant CFStrings. 1120 QualType getCFConstantStringType() const; 1121 1122 /// \brief Returns the C struct type for objc_super 1123 QualType getObjCSuperType() const; 1124 void setObjCSuperType(QualType ST) { ObjCSuperType = ST; } 1125 1126 /// Get the structure type used to representation CFStrings, or NULL 1127 /// if it hasn't yet been built. 1128 QualType getRawCFConstantStringType() const { 1129 if (CFConstantStringTypeDecl) 1130 return getTagDeclType(CFConstantStringTypeDecl); 1131 return QualType(); 1132 } 1133 void setCFConstantStringType(QualType T); 1134 1135 // This setter/getter represents the ObjC type for an NSConstantString. 1136 void setObjCConstantStringInterface(ObjCInterfaceDecl *Decl); 1137 QualType getObjCConstantStringInterface() const { 1138 return ObjCConstantStringType; 1139 } 1140 1141 QualType getObjCNSStringType() const { 1142 return ObjCNSStringType; 1143 } 1144 1145 void setObjCNSStringType(QualType T) { 1146 ObjCNSStringType = T; 1147 } 1148 1149 /// \brief Retrieve the type that \c id has been defined to, which may be 1150 /// different from the built-in \c id if \c id has been typedef'd. 1151 QualType getObjCIdRedefinitionType() const { 1152 if (ObjCIdRedefinitionType.isNull()) 1153 return getObjCIdType(); 1154 return ObjCIdRedefinitionType; 1155 } 1156 1157 /// \brief Set the user-written type that redefines \c id. 1158 void setObjCIdRedefinitionType(QualType RedefType) { 1159 ObjCIdRedefinitionType = RedefType; 1160 } 1161 1162 /// \brief Retrieve the type that \c Class has been defined to, which may be 1163 /// different from the built-in \c Class if \c Class has been typedef'd. 1164 QualType getObjCClassRedefinitionType() const { 1165 if (ObjCClassRedefinitionType.isNull()) 1166 return getObjCClassType(); 1167 return ObjCClassRedefinitionType; 1168 } 1169 1170 /// \brief Set the user-written type that redefines 'SEL'. 1171 void setObjCClassRedefinitionType(QualType RedefType) { 1172 ObjCClassRedefinitionType = RedefType; 1173 } 1174 1175 /// \brief Retrieve the type that 'SEL' has been defined to, which may be 1176 /// different from the built-in 'SEL' if 'SEL' has been typedef'd. 1177 QualType getObjCSelRedefinitionType() const { 1178 if (ObjCSelRedefinitionType.isNull()) 1179 return getObjCSelType(); 1180 return ObjCSelRedefinitionType; 1181 } 1182 1183 1184 /// \brief Set the user-written type that redefines 'SEL'. 1185 void setObjCSelRedefinitionType(QualType RedefType) { 1186 ObjCSelRedefinitionType = RedefType; 1187 } 1188 1189 /// \brief Retrieve the Objective-C "instancetype" type, if already known; 1190 /// otherwise, returns a NULL type; 1191 QualType getObjCInstanceType() { 1192 return getTypeDeclType(getObjCInstanceTypeDecl()); 1193 } 1194 1195 /// \brief Retrieve the typedef declaration corresponding to the Objective-C 1196 /// "instancetype" type. 1197 TypedefDecl *getObjCInstanceTypeDecl(); 1198 1199 /// \brief Set the type for the C FILE type. 1200 void setFILEDecl(TypeDecl *FILEDecl) { this->FILEDecl = FILEDecl; } 1201 1202 /// \brief Retrieve the C FILE type. 1203 QualType getFILEType() const { 1204 if (FILEDecl) 1205 return getTypeDeclType(FILEDecl); 1206 return QualType(); 1207 } 1208 1209 /// \brief Set the type for the C jmp_buf type. 1210 void setjmp_bufDecl(TypeDecl *jmp_bufDecl) { 1211 this->jmp_bufDecl = jmp_bufDecl; 1212 } 1213 1214 /// \brief Retrieve the C jmp_buf type. 1215 QualType getjmp_bufType() const { 1216 if (jmp_bufDecl) 1217 return getTypeDeclType(jmp_bufDecl); 1218 return QualType(); 1219 } 1220 1221 /// \brief Set the type for the C sigjmp_buf type. 1222 void setsigjmp_bufDecl(TypeDecl *sigjmp_bufDecl) { 1223 this->sigjmp_bufDecl = sigjmp_bufDecl; 1224 } 1225 1226 /// \brief Retrieve the C sigjmp_buf type. 1227 QualType getsigjmp_bufType() const { 1228 if (sigjmp_bufDecl) 1229 return getTypeDeclType(sigjmp_bufDecl); 1230 return QualType(); 1231 } 1232 1233 /// \brief Set the type for the C ucontext_t type. 1234 void setucontext_tDecl(TypeDecl *ucontext_tDecl) { 1235 this->ucontext_tDecl = ucontext_tDecl; 1236 } 1237 1238 /// \brief Retrieve the C ucontext_t type. 1239 QualType getucontext_tType() const { 1240 if (ucontext_tDecl) 1241 return getTypeDeclType(ucontext_tDecl); 1242 return QualType(); 1243 } 1244 1245 /// \brief The result type of logical operations, '<', '>', '!=', etc. 1246 QualType getLogicalOperationType() const { 1247 return getLangOpts().CPlusPlus ? BoolTy : IntTy; 1248 } 1249 1250 /// \brief Emit the Objective-CC type encoding for the given type \p T into 1251 /// \p S. 1252 /// 1253 /// If \p Field is specified then record field names are also encoded. 1254 void getObjCEncodingForType(QualType T, std::string &S, 1255 const FieldDecl *Field=0) const; 1256 1257 void getLegacyIntegralTypeEncoding(QualType &t) const; 1258 1259 /// \brief Put the string version of the type qualifiers \p QT into \p S. 1260 void getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, 1261 std::string &S) const; 1262 1263 /// \brief Emit the encoded type for the function \p Decl into \p S. 1264 /// 1265 /// This is in the same format as Objective-C method encodings. 1266 /// 1267 /// \returns true if an error occurred (e.g., because one of the parameter 1268 /// types is incomplete), false otherwise. 1269 bool getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, std::string& S); 1270 1271 /// \brief Emit the encoded type for the method declaration \p Decl into 1272 /// \p S. 1273 /// 1274 /// \returns true if an error occurred (e.g., because one of the parameter 1275 /// types is incomplete), false otherwise. 1276 bool getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, std::string &S, 1277 bool Extended = false) 1278 const; 1279 1280 /// \brief Return the encoded type for this block declaration. 1281 std::string getObjCEncodingForBlock(const BlockExpr *blockExpr) const; 1282 1283 /// getObjCEncodingForPropertyDecl - Return the encoded type for 1284 /// this method declaration. If non-NULL, Container must be either 1285 /// an ObjCCategoryImplDecl or ObjCImplementationDecl; it should 1286 /// only be NULL when getting encodings for protocol properties. 1287 void getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, 1288 const Decl *Container, 1289 std::string &S) const; 1290 1291 bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, 1292 ObjCProtocolDecl *rProto) const; 1293 1294 /// \brief Return the size of type \p T for Objective-C encoding purpose, 1295 /// in characters. 1296 CharUnits getObjCEncodingTypeSize(QualType T) const; 1297 1298 /// \brief Retrieve the typedef corresponding to the predefined \c id type 1299 /// in Objective-C. 1300 TypedefDecl *getObjCIdDecl() const; 1301 1302 /// \brief Represents the Objective-CC \c id type. 1303 /// 1304 /// This is set up lazily, by Sema. \c id is always a (typedef for a) 1305 /// pointer type, a pointer to a struct. 1306 QualType getObjCIdType() const { 1307 return getTypeDeclType(getObjCIdDecl()); 1308 } 1309 1310 /// \brief Retrieve the typedef corresponding to the predefined 'SEL' type 1311 /// in Objective-C. 1312 TypedefDecl *getObjCSelDecl() const; 1313 1314 /// \brief Retrieve the type that corresponds to the predefined Objective-C 1315 /// 'SEL' type. 1316 QualType getObjCSelType() const { 1317 return getTypeDeclType(getObjCSelDecl()); 1318 } 1319 1320 /// \brief Retrieve the typedef declaration corresponding to the predefined 1321 /// Objective-C 'Class' type. 1322 TypedefDecl *getObjCClassDecl() const; 1323 1324 /// \brief Represents the Objective-C \c Class type. 1325 /// 1326 /// This is set up lazily, by Sema. \c Class is always a (typedef for a) 1327 /// pointer type, a pointer to a struct. 1328 QualType getObjCClassType() const { 1329 return getTypeDeclType(getObjCClassDecl()); 1330 } 1331 1332 /// \brief Retrieve the Objective-C class declaration corresponding to 1333 /// the predefined \c Protocol class. 1334 ObjCInterfaceDecl *getObjCProtocolDecl() const; 1335 1336 /// \brief Retrieve declaration of 'BOOL' typedef 1337 TypedefDecl *getBOOLDecl() const { 1338 return BOOLDecl; 1339 } 1340 1341 /// \brief Save declaration of 'BOOL' typedef 1342 void setBOOLDecl(TypedefDecl *TD) { 1343 BOOLDecl = TD; 1344 } 1345 1346 /// \brief type of 'BOOL' type. 1347 QualType getBOOLType() const { 1348 return getTypeDeclType(getBOOLDecl()); 1349 } 1350 1351 /// \brief Retrieve the type of the Objective-C \c Protocol class. 1352 QualType getObjCProtoType() const { 1353 return getObjCInterfaceType(getObjCProtocolDecl()); 1354 } 1355 1356 /// \brief Retrieve the C type declaration corresponding to the predefined 1357 /// \c __builtin_va_list type. 1358 TypedefDecl *getBuiltinVaListDecl() const; 1359 1360 /// \brief Retrieve the type of the \c __builtin_va_list type. 1361 QualType getBuiltinVaListType() const { 1362 return getTypeDeclType(getBuiltinVaListDecl()); 1363 } 1364 1365 /// \brief Retrieve the C type declaration corresponding to the predefined 1366 /// \c __va_list_tag type used to help define the \c __builtin_va_list type 1367 /// for some targets. 1368 QualType getVaListTagType() const; 1369 1370 /// \brief Return a type with additional \c const, \c volatile, or 1371 /// \c restrict qualifiers. 1372 QualType getCVRQualifiedType(QualType T, unsigned CVR) const { 1373 return getQualifiedType(T, Qualifiers::fromCVRMask(CVR)); 1374 } 1375 1376 /// \brief Un-split a SplitQualType. 1377 QualType getQualifiedType(SplitQualType split) const { 1378 return getQualifiedType(split.Ty, split.Quals); 1379 } 1380 1381 /// \brief Return a type with additional qualifiers. 1382 QualType getQualifiedType(QualType T, Qualifiers Qs) const { 1383 if (!Qs.hasNonFastQualifiers()) 1384 return T.withFastQualifiers(Qs.getFastQualifiers()); 1385 QualifierCollector Qc(Qs); 1386 const Type *Ptr = Qc.strip(T); 1387 return getExtQualType(Ptr, Qc); 1388 } 1389 1390 /// \brief Return a type with additional qualifiers. 1391 QualType getQualifiedType(const Type *T, Qualifiers Qs) const { 1392 if (!Qs.hasNonFastQualifiers()) 1393 return QualType(T, Qs.getFastQualifiers()); 1394 return getExtQualType(T, Qs); 1395 } 1396 1397 /// \brief Return a type with the given lifetime qualifier. 1398 /// 1399 /// \pre Neither type.ObjCLifetime() nor \p lifetime may be \c OCL_None. 1400 QualType getLifetimeQualifiedType(QualType type, 1401 Qualifiers::ObjCLifetime lifetime) { 1402 assert(type.getObjCLifetime() == Qualifiers::OCL_None); 1403 assert(lifetime != Qualifiers::OCL_None); 1404 1405 Qualifiers qs; 1406 qs.addObjCLifetime(lifetime); 1407 return getQualifiedType(type, qs); 1408 } 1409 1410 DeclarationNameInfo getNameForTemplate(TemplateName Name, 1411 SourceLocation NameLoc) const; 1412 1413 TemplateName getOverloadedTemplateName(UnresolvedSetIterator Begin, 1414 UnresolvedSetIterator End) const; 1415 1416 TemplateName getQualifiedTemplateName(NestedNameSpecifier *NNS, 1417 bool TemplateKeyword, 1418 TemplateDecl *Template) const; 1419 1420 TemplateName getDependentTemplateName(NestedNameSpecifier *NNS, 1421 const IdentifierInfo *Name) const; 1422 TemplateName getDependentTemplateName(NestedNameSpecifier *NNS, 1423 OverloadedOperatorKind Operator) const; 1424 TemplateName getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param, 1425 TemplateName replacement) const; 1426 TemplateName getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param, 1427 const TemplateArgument &ArgPack) const; 1428 1429 enum GetBuiltinTypeError { 1430 GE_None, ///< No error 1431 GE_Missing_stdio, ///< Missing a type from <stdio.h> 1432 GE_Missing_setjmp, ///< Missing a type from <setjmp.h> 1433 GE_Missing_ucontext ///< Missing a type from <ucontext.h> 1434 }; 1435 1436 /// \brief Return the type for the specified builtin. 1437 /// 1438 /// If \p IntegerConstantArgs is non-null, it is filled in with a bitmask of 1439 /// arguments to the builtin that are required to be integer constant 1440 /// expressions. 1441 QualType GetBuiltinType(unsigned ID, GetBuiltinTypeError &Error, 1442 unsigned *IntegerConstantArgs = 0) const; 1443 1444private: 1445 CanQualType getFromTargetType(unsigned Type) const; 1446 std::pair<uint64_t, unsigned> getTypeInfoImpl(const Type *T) const; 1447 1448 //===--------------------------------------------------------------------===// 1449 // Type Predicates. 1450 //===--------------------------------------------------------------------===// 1451 1452public: 1453 /// \brief Return one of the GCNone, Weak or Strong Objective-C garbage 1454 /// collection attributes. 1455 Qualifiers::GC getObjCGCAttrKind(QualType Ty) const; 1456 1457 /// \brief Return true if the given vector types are of the same unqualified 1458 /// type or if they are equivalent to the same GCC vector type. 1459 /// 1460 /// \note This ignores whether they are target-specific (AltiVec or Neon) 1461 /// types. 1462 bool areCompatibleVectorTypes(QualType FirstVec, QualType SecondVec); 1463 1464 /// \brief Return true if this is an \c NSObject object with its \c NSObject 1465 /// attribute set. 1466 static bool isObjCNSObjectType(QualType Ty) { 1467 return Ty->isObjCNSObjectType(); 1468 } 1469 1470 //===--------------------------------------------------------------------===// 1471 // Type Sizing and Analysis 1472 //===--------------------------------------------------------------------===// 1473 1474 /// \brief Return the APFloat 'semantics' for the specified scalar floating 1475 /// point type. 1476 const llvm::fltSemantics &getFloatTypeSemantics(QualType T) const; 1477 1478 /// \brief Get the size and alignment of the specified complete type in bits. 1479 std::pair<uint64_t, unsigned> getTypeInfo(const Type *T) const; 1480 std::pair<uint64_t, unsigned> getTypeInfo(QualType T) const { 1481 return getTypeInfo(T.getTypePtr()); 1482 } 1483 1484 /// \brief Return the size of the specified (complete) type \p T, in bits. 1485 uint64_t getTypeSize(QualType T) const { 1486 return getTypeInfo(T).first; 1487 } 1488 uint64_t getTypeSize(const Type *T) const { 1489 return getTypeInfo(T).first; 1490 } 1491 1492 /// \brief Return the size of the character type, in bits. 1493 uint64_t getCharWidth() const { 1494 return getTypeSize(CharTy); 1495 } 1496 1497 /// \brief Convert a size in bits to a size in characters. 1498 CharUnits toCharUnitsFromBits(int64_t BitSize) const; 1499 1500 /// \brief Convert a size in characters to a size in bits. 1501 int64_t toBits(CharUnits CharSize) const; 1502 1503 /// \brief Return the size of the specified (complete) type \p T, in 1504 /// characters. 1505 CharUnits getTypeSizeInChars(QualType T) const; 1506 CharUnits getTypeSizeInChars(const Type *T) const; 1507 1508 /// \brief Return the ABI-specified alignment of a (complete) type \p T, in 1509 /// bits. 1510 unsigned getTypeAlign(QualType T) const { 1511 return getTypeInfo(T).second; 1512 } 1513 unsigned getTypeAlign(const Type *T) const { 1514 return getTypeInfo(T).second; 1515 } 1516 1517 /// \brief Return the ABI-specified alignment of a (complete) type \p T, in 1518 /// characters. 1519 CharUnits getTypeAlignInChars(QualType T) const; 1520 CharUnits getTypeAlignInChars(const Type *T) const; 1521 1522 // getTypeInfoDataSizeInChars - Return the size of a type, in chars. If the 1523 // type is a record, its data size is returned. 1524 std::pair<CharUnits, CharUnits> getTypeInfoDataSizeInChars(QualType T) const; 1525 1526 std::pair<CharUnits, CharUnits> getTypeInfoInChars(const Type *T) const; 1527 std::pair<CharUnits, CharUnits> getTypeInfoInChars(QualType T) const; 1528 1529 /// \brief Return the "preferred" alignment of the specified type \p T for 1530 /// the current target, in bits. 1531 /// 1532 /// This can be different than the ABI alignment in cases where it is 1533 /// beneficial for performance to overalign a data type. 1534 unsigned getPreferredTypeAlign(const Type *T) const; 1535 1536 /// \brief Return a conservative estimate of the alignment of the specified 1537 /// decl \p D. 1538 /// 1539 /// \pre \p D must not be a bitfield type, as bitfields do not have a valid 1540 /// alignment. 1541 /// 1542 /// If \p RefAsPointee, references are treated like their underlying type 1543 /// (for alignof), else they're treated like pointers (for CodeGen). 1544 CharUnits getDeclAlign(const Decl *D, bool RefAsPointee = false) const; 1545 1546 /// \brief Get or compute information about the layout of the specified 1547 /// record (struct/union/class) \p D, which indicates its size and field 1548 /// position information. 1549 const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D) const; 1550 1551 /// \brief Get or compute information about the layout of the specified 1552 /// Objective-C interface. 1553 const ASTRecordLayout &getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) 1554 const; 1555 1556 void DumpRecordLayout(const RecordDecl *RD, raw_ostream &OS, 1557 bool Simple = false) const; 1558 1559 /// \brief Get or compute information about the layout of the specified 1560 /// Objective-C implementation. 1561 /// 1562 /// This may differ from the interface if synthesized ivars are present. 1563 const ASTRecordLayout & 1564 getASTObjCImplementationLayout(const ObjCImplementationDecl *D) const; 1565 1566 /// \brief Get our current best idea for the key function of the 1567 /// given record decl, or NULL if there isn't one. 1568 /// 1569 /// The key function is, according to the Itanium C++ ABI section 5.2.3: 1570 /// ...the first non-pure virtual function that is not inline at the 1571 /// point of class definition. 1572 /// 1573 /// Other ABIs use the same idea. However, the ARM C++ ABI ignores 1574 /// virtual functions that are defined 'inline', which means that 1575 /// the result of this computation can change. 1576 const CXXMethodDecl *getCurrentKeyFunction(const CXXRecordDecl *RD); 1577 1578 /// \brief Observe that the given method cannot be a key function. 1579 /// Checks the key-function cache for the method's class and clears it 1580 /// if matches the given declaration. 1581 /// 1582 /// This is used in ABIs where out-of-line definitions marked 1583 /// inline are not considered to be key functions. 1584 /// 1585 /// \param method should be the declaration from the class definition 1586 void setNonKeyFunction(const CXXMethodDecl *method); 1587 1588 /// Get the offset of a FieldDecl or IndirectFieldDecl, in bits. 1589 uint64_t getFieldOffset(const ValueDecl *FD) const; 1590 1591 bool isNearlyEmpty(const CXXRecordDecl *RD) const; 1592 1593 MangleContext *createMangleContext(); 1594 1595 void DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, bool leafClass, 1596 SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const; 1597 1598 unsigned CountNonClassIvars(const ObjCInterfaceDecl *OI) const; 1599 void CollectInheritedProtocols(const Decl *CDecl, 1600 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols); 1601 1602 //===--------------------------------------------------------------------===// 1603 // Type Operators 1604 //===--------------------------------------------------------------------===// 1605 1606 /// \brief Return the canonical (structural) type corresponding to the 1607 /// specified potentially non-canonical type \p T. 1608 /// 1609 /// The non-canonical version of a type may have many "decorated" versions of 1610 /// types. Decorators can include typedefs, 'typeof' operators, etc. The 1611 /// returned type is guaranteed to be free of any of these, allowing two 1612 /// canonical types to be compared for exact equality with a simple pointer 1613 /// comparison. 1614 CanQualType getCanonicalType(QualType T) const { 1615 return CanQualType::CreateUnsafe(T.getCanonicalType()); 1616 } 1617 1618 const Type *getCanonicalType(const Type *T) const { 1619 return T->getCanonicalTypeInternal().getTypePtr(); 1620 } 1621 1622 /// \brief Return the canonical parameter type corresponding to the specific 1623 /// potentially non-canonical one. 1624 /// 1625 /// Qualifiers are stripped off, functions are turned into function 1626 /// pointers, and arrays decay one level into pointers. 1627 CanQualType getCanonicalParamType(QualType T) const; 1628 1629 /// \brief Determine whether the given types \p T1 and \p T2 are equivalent. 1630 bool hasSameType(QualType T1, QualType T2) const { 1631 return getCanonicalType(T1) == getCanonicalType(T2); 1632 } 1633 1634 /// \brief Return this type as a completely-unqualified array type, 1635 /// capturing the qualifiers in \p Quals. 1636 /// 1637 /// This will remove the minimal amount of sugaring from the types, similar 1638 /// to the behavior of QualType::getUnqualifiedType(). 1639 /// 1640 /// \param T is the qualified type, which may be an ArrayType 1641 /// 1642 /// \param Quals will receive the full set of qualifiers that were 1643 /// applied to the array. 1644 /// 1645 /// \returns if this is an array type, the completely unqualified array type 1646 /// that corresponds to it. Otherwise, returns T.getUnqualifiedType(). 1647 QualType getUnqualifiedArrayType(QualType T, Qualifiers &Quals); 1648 1649 /// \brief Determine whether the given types are equivalent after 1650 /// cvr-qualifiers have been removed. 1651 bool hasSameUnqualifiedType(QualType T1, QualType T2) const { 1652 return getCanonicalType(T1).getTypePtr() == 1653 getCanonicalType(T2).getTypePtr(); 1654 } 1655 1656 bool UnwrapSimilarPointerTypes(QualType &T1, QualType &T2); 1657 1658 /// \brief Retrieves the "canonical" nested name specifier for a 1659 /// given nested name specifier. 1660 /// 1661 /// The canonical nested name specifier is a nested name specifier 1662 /// that uniquely identifies a type or namespace within the type 1663 /// system. For example, given: 1664 /// 1665 /// \code 1666 /// namespace N { 1667 /// struct S { 1668 /// template<typename T> struct X { typename T* type; }; 1669 /// }; 1670 /// } 1671 /// 1672 /// template<typename T> struct Y { 1673 /// typename N::S::X<T>::type member; 1674 /// }; 1675 /// \endcode 1676 /// 1677 /// Here, the nested-name-specifier for N::S::X<T>:: will be 1678 /// S::X<template-param-0-0>, since 'S' and 'X' are uniquely defined 1679 /// by declarations in the type system and the canonical type for 1680 /// the template type parameter 'T' is template-param-0-0. 1681 NestedNameSpecifier * 1682 getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const; 1683 1684 /// \brief Retrieves the default calling convention to use for 1685 /// C++ instance methods. 1686 CallingConv getDefaultCXXMethodCallConv(bool isVariadic); 1687 1688 /// \brief Retrieves the canonical representation of the given 1689 /// calling convention. 1690 CallingConv getCanonicalCallConv(CallingConv CC) const; 1691 1692 /// \brief Determines whether two calling conventions name the same 1693 /// calling convention. 1694 bool isSameCallConv(CallingConv lcc, CallingConv rcc) { 1695 return (getCanonicalCallConv(lcc) == getCanonicalCallConv(rcc)); 1696 } 1697 1698 /// \brief Retrieves the "canonical" template name that refers to a 1699 /// given template. 1700 /// 1701 /// The canonical template name is the simplest expression that can 1702 /// be used to refer to a given template. For most templates, this 1703 /// expression is just the template declaration itself. For example, 1704 /// the template std::vector can be referred to via a variety of 1705 /// names---std::vector, \::std::vector, vector (if vector is in 1706 /// scope), etc.---but all of these names map down to the same 1707 /// TemplateDecl, which is used to form the canonical template name. 1708 /// 1709 /// Dependent template names are more interesting. Here, the 1710 /// template name could be something like T::template apply or 1711 /// std::allocator<T>::template rebind, where the nested name 1712 /// specifier itself is dependent. In this case, the canonical 1713 /// template name uses the shortest form of the dependent 1714 /// nested-name-specifier, which itself contains all canonical 1715 /// types, values, and templates. 1716 TemplateName getCanonicalTemplateName(TemplateName Name) const; 1717 1718 /// \brief Determine whether the given template names refer to the same 1719 /// template. 1720 bool hasSameTemplateName(TemplateName X, TemplateName Y); 1721 1722 /// \brief Retrieve the "canonical" template argument. 1723 /// 1724 /// The canonical template argument is the simplest template argument 1725 /// (which may be a type, value, expression, or declaration) that 1726 /// expresses the value of the argument. 1727 TemplateArgument getCanonicalTemplateArgument(const TemplateArgument &Arg) 1728 const; 1729 1730 /// Type Query functions. If the type is an instance of the specified class, 1731 /// return the Type pointer for the underlying maximally pretty type. This 1732 /// is a member of ASTContext because this may need to do some amount of 1733 /// canonicalization, e.g. to move type qualifiers into the element type. 1734 const ArrayType *getAsArrayType(QualType T) const; 1735 const ConstantArrayType *getAsConstantArrayType(QualType T) const { 1736 return dyn_cast_or_null<ConstantArrayType>(getAsArrayType(T)); 1737 } 1738 const VariableArrayType *getAsVariableArrayType(QualType T) const { 1739 return dyn_cast_or_null<VariableArrayType>(getAsArrayType(T)); 1740 } 1741 const IncompleteArrayType *getAsIncompleteArrayType(QualType T) const { 1742 return dyn_cast_or_null<IncompleteArrayType>(getAsArrayType(T)); 1743 } 1744 const DependentSizedArrayType *getAsDependentSizedArrayType(QualType T) 1745 const { 1746 return dyn_cast_or_null<DependentSizedArrayType>(getAsArrayType(T)); 1747 } 1748 1749 /// \brief Return the innermost element type of an array type. 1750 /// 1751 /// For example, will return "int" for int[m][n] 1752 QualType getBaseElementType(const ArrayType *VAT) const; 1753 1754 /// \brief Return the innermost element type of a type (which needn't 1755 /// actually be an array type). 1756 QualType getBaseElementType(QualType QT) const; 1757 1758 /// \brief Return number of constant array elements. 1759 uint64_t getConstantArrayElementCount(const ConstantArrayType *CA) const; 1760 1761 /// \brief Perform adjustment on the parameter type of a function. 1762 /// 1763 /// This routine adjusts the given parameter type @p T to the actual 1764 /// parameter type used by semantic analysis (C99 6.7.5.3p[7,8], 1765 /// C++ [dcl.fct]p3). The adjusted parameter type is returned. 1766 QualType getAdjustedParameterType(QualType T) const; 1767 1768 /// \brief Retrieve the parameter type as adjusted for use in the signature 1769 /// of a function, decaying array and function types and removing top-level 1770 /// cv-qualifiers. 1771 QualType getSignatureParameterType(QualType T) const; 1772 1773 /// \brief Return the properly qualified result of decaying the specified 1774 /// array type to a pointer. 1775 /// 1776 /// This operation is non-trivial when handling typedefs etc. The canonical 1777 /// type of \p T must be an array type, this returns a pointer to a properly 1778 /// qualified element of the array. 1779 /// 1780 /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. 1781 QualType getArrayDecayedType(QualType T) const; 1782 1783 /// \brief Return the type that \p PromotableType will promote to: C99 1784 /// 6.3.1.1p2, assuming that \p PromotableType is a promotable integer type. 1785 QualType getPromotedIntegerType(QualType PromotableType) const; 1786 1787 /// \brief Recurses in pointer/array types until it finds an Objective-C 1788 /// retainable type and returns its ownership. 1789 Qualifiers::ObjCLifetime getInnerObjCOwnership(QualType T) const; 1790 1791 /// \brief Whether this is a promotable bitfield reference according 1792 /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). 1793 /// 1794 /// \returns the type this bit-field will promote to, or NULL if no 1795 /// promotion occurs. 1796 QualType isPromotableBitField(Expr *E) const; 1797 1798 /// \brief Return the highest ranked integer type, see C99 6.3.1.8p1. 1799 /// 1800 /// If \p LHS > \p RHS, returns 1. If \p LHS == \p RHS, returns 0. If 1801 /// \p LHS < \p RHS, return -1. 1802 int getIntegerTypeOrder(QualType LHS, QualType RHS) const; 1803 1804 /// \brief Compare the rank of the two specified floating point types, 1805 /// ignoring the domain of the type (i.e. 'double' == '_Complex double'). 1806 /// 1807 /// If \p LHS > \p RHS, returns 1. If \p LHS == \p RHS, returns 0. If 1808 /// \p LHS < \p RHS, return -1. 1809 int getFloatingTypeOrder(QualType LHS, QualType RHS) const; 1810 1811 /// \brief Return a real floating point or a complex type (based on 1812 /// \p typeDomain/\p typeSize). 1813 /// 1814 /// \param typeDomain a real floating point or complex type. 1815 /// \param typeSize a real floating point or complex type. 1816 QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize, 1817 QualType typeDomain) const; 1818 1819 unsigned getTargetAddressSpace(QualType T) const { 1820 return getTargetAddressSpace(T.getQualifiers()); 1821 } 1822 1823 unsigned getTargetAddressSpace(Qualifiers Q) const { 1824 return getTargetAddressSpace(Q.getAddressSpace()); 1825 } 1826 1827 unsigned getTargetAddressSpace(unsigned AS) const { 1828 if (AS < LangAS::Offset || AS >= LangAS::Offset + LangAS::Count) 1829 return AS; 1830 else 1831 return (*AddrSpaceMap)[AS - LangAS::Offset]; 1832 } 1833 1834private: 1835 // Helper for integer ordering 1836 unsigned getIntegerRank(const Type *T) const; 1837 1838public: 1839 1840 //===--------------------------------------------------------------------===// 1841 // Type Compatibility Predicates 1842 //===--------------------------------------------------------------------===// 1843 1844 /// Compatibility predicates used to check assignment expressions. 1845 bool typesAreCompatible(QualType T1, QualType T2, 1846 bool CompareUnqualified = false); // C99 6.2.7p1 1847 1848 bool propertyTypesAreCompatible(QualType, QualType); 1849 bool typesAreBlockPointerCompatible(QualType, QualType); 1850 1851 bool isObjCIdType(QualType T) const { 1852 return T == getObjCIdType(); 1853 } 1854 bool isObjCClassType(QualType T) const { 1855 return T == getObjCClassType(); 1856 } 1857 bool isObjCSelType(QualType T) const { 1858 return T == getObjCSelType(); 1859 } 1860 bool QualifiedIdConformsQualifiedId(QualType LHS, QualType RHS); 1861 bool ObjCQualifiedIdTypesAreCompatible(QualType LHS, QualType RHS, 1862 bool ForCompare); 1863 1864 bool ObjCQualifiedClassTypesAreCompatible(QualType LHS, QualType RHS); 1865 1866 // Check the safety of assignment from LHS to RHS 1867 bool canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, 1868 const ObjCObjectPointerType *RHSOPT); 1869 bool canAssignObjCInterfaces(const ObjCObjectType *LHS, 1870 const ObjCObjectType *RHS); 1871 bool canAssignObjCInterfacesInBlockPointer( 1872 const ObjCObjectPointerType *LHSOPT, 1873 const ObjCObjectPointerType *RHSOPT, 1874 bool BlockReturnType); 1875 bool areComparableObjCPointerTypes(QualType LHS, QualType RHS); 1876 QualType areCommonBaseCompatible(const ObjCObjectPointerType *LHSOPT, 1877 const ObjCObjectPointerType *RHSOPT); 1878 bool canBindObjCObjectType(QualType To, QualType From); 1879 1880 // Functions for calculating composite types 1881 QualType mergeTypes(QualType, QualType, bool OfBlockPointer=false, 1882 bool Unqualified = false, bool BlockReturnType = false); 1883 QualType mergeFunctionTypes(QualType, QualType, bool OfBlockPointer=false, 1884 bool Unqualified = false); 1885 QualType mergeFunctionArgumentTypes(QualType, QualType, 1886 bool OfBlockPointer=false, 1887 bool Unqualified = false); 1888 QualType mergeTransparentUnionType(QualType, QualType, 1889 bool OfBlockPointer=false, 1890 bool Unqualified = false); 1891 1892 QualType mergeObjCGCQualifiers(QualType, QualType); 1893 1894 bool FunctionTypesMatchOnNSConsumedAttrs( 1895 const FunctionProtoType *FromFunctionType, 1896 const FunctionProtoType *ToFunctionType); 1897 1898 void ResetObjCLayout(const ObjCContainerDecl *CD) { 1899 ObjCLayouts[CD] = 0; 1900 } 1901 1902 //===--------------------------------------------------------------------===// 1903 // Integer Predicates 1904 //===--------------------------------------------------------------------===// 1905 1906 // The width of an integer, as defined in C99 6.2.6.2. This is the number 1907 // of bits in an integer type excluding any padding bits. 1908 unsigned getIntWidth(QualType T) const; 1909 1910 // Per C99 6.2.5p6, for every signed integer type, there is a corresponding 1911 // unsigned integer type. This method takes a signed type, and returns the 1912 // corresponding unsigned integer type. 1913 QualType getCorrespondingUnsignedType(QualType T) const; 1914 1915 //===--------------------------------------------------------------------===// 1916 // Type Iterators. 1917 //===--------------------------------------------------------------------===// 1918 1919 typedef std::vector<Type*>::iterator type_iterator; 1920 typedef std::vector<Type*>::const_iterator const_type_iterator; 1921 1922 type_iterator types_begin() { return Types.begin(); } 1923 type_iterator types_end() { return Types.end(); } 1924 const_type_iterator types_begin() const { return Types.begin(); } 1925 const_type_iterator types_end() const { return Types.end(); } 1926 1927 //===--------------------------------------------------------------------===// 1928 // Integer Values 1929 //===--------------------------------------------------------------------===// 1930 1931 /// \brief Make an APSInt of the appropriate width and signedness for the 1932 /// given \p Value and integer \p Type. 1933 llvm::APSInt MakeIntValue(uint64_t Value, QualType Type) const { 1934 llvm::APSInt Res(getIntWidth(Type), 1935 !Type->isSignedIntegerOrEnumerationType()); 1936 Res = Value; 1937 return Res; 1938 } 1939 1940 bool isSentinelNullExpr(const Expr *E); 1941 1942 /// \brief Get the implementation of the ObjCInterfaceDecl \p D, or NULL if 1943 /// none exists. 1944 ObjCImplementationDecl *getObjCImplementation(ObjCInterfaceDecl *D); 1945 /// \brief Get the implementation of the ObjCCategoryDecl \p D, or NULL if 1946 /// none exists. 1947 ObjCCategoryImplDecl *getObjCImplementation(ObjCCategoryDecl *D); 1948 1949 /// \brief Return true if there is at least one \@implementation in the TU. 1950 bool AnyObjCImplementation() { 1951 return !ObjCImpls.empty(); 1952 } 1953 1954 /// \brief Set the implementation of ObjCInterfaceDecl. 1955 void setObjCImplementation(ObjCInterfaceDecl *IFaceD, 1956 ObjCImplementationDecl *ImplD); 1957 /// \brief Set the implementation of ObjCCategoryDecl. 1958 void setObjCImplementation(ObjCCategoryDecl *CatD, 1959 ObjCCategoryImplDecl *ImplD); 1960 1961 /// \brief Get the duplicate declaration of a ObjCMethod in the same 1962 /// interface, or null if none exists. 1963 const ObjCMethodDecl *getObjCMethodRedeclaration( 1964 const ObjCMethodDecl *MD) const { 1965 return ObjCMethodRedecls.lookup(MD); 1966 } 1967 1968 void setObjCMethodRedeclaration(const ObjCMethodDecl *MD, 1969 const ObjCMethodDecl *Redecl) { 1970 assert(!getObjCMethodRedeclaration(MD) && "MD already has a redeclaration"); 1971 ObjCMethodRedecls[MD] = Redecl; 1972 } 1973 1974 /// \brief Returns the Objective-C interface that \p ND belongs to if it is 1975 /// an Objective-C method/property/ivar etc. that is part of an interface, 1976 /// otherwise returns null. 1977 ObjCInterfaceDecl *getObjContainingInterface(NamedDecl *ND) const; 1978 1979 /// \brief Set the copy inialization expression of a block var decl. 1980 void setBlockVarCopyInits(VarDecl*VD, Expr* Init); 1981 /// \brief Get the copy initialization expression of the VarDecl \p VD, or 1982 /// NULL if none exists. 1983 Expr *getBlockVarCopyInits(const VarDecl* VD); 1984 1985 /// \brief Allocate an uninitialized TypeSourceInfo. 1986 /// 1987 /// The caller should initialize the memory held by TypeSourceInfo using 1988 /// the TypeLoc wrappers. 1989 /// 1990 /// \param T the type that will be the basis for type source info. This type 1991 /// should refer to how the declarator was written in source code, not to 1992 /// what type semantic analysis resolved the declarator to. 1993 /// 1994 /// \param Size the size of the type info to create, or 0 if the size 1995 /// should be calculated based on the type. 1996 TypeSourceInfo *CreateTypeSourceInfo(QualType T, unsigned Size = 0) const; 1997 1998 /// \brief Allocate a TypeSourceInfo where all locations have been 1999 /// initialized to a given location, which defaults to the empty 2000 /// location. 2001 TypeSourceInfo * 2002 getTrivialTypeSourceInfo(QualType T, 2003 SourceLocation Loc = SourceLocation()) const; 2004 2005 TypeSourceInfo *getNullTypeSourceInfo() { return &NullTypeSourceInfo; } 2006 2007 /// \brief Add a deallocation callback that will be invoked when the 2008 /// ASTContext is destroyed. 2009 /// 2010 /// \param Callback A callback function that will be invoked on destruction. 2011 /// 2012 /// \param Data Pointer data that will be provided to the callback function 2013 /// when it is called. 2014 void AddDeallocation(void (*Callback)(void*), void *Data); 2015 2016 GVALinkage GetGVALinkageForFunction(const FunctionDecl *FD); 2017 GVALinkage GetGVALinkageForVariable(const VarDecl *VD); 2018 2019 /// \brief Determines if the decl can be CodeGen'ed or deserialized from PCH 2020 /// lazily, only when used; this is only relevant for function or file scoped 2021 /// var definitions. 2022 /// 2023 /// \returns true if the function/var must be CodeGen'ed/deserialized even if 2024 /// it is not used. 2025 bool DeclMustBeEmitted(const Decl *D); 2026 2027 void addUnnamedTag(const TagDecl *Tag); 2028 int getUnnamedTagManglingNumber(const TagDecl *Tag) const; 2029 2030 /// \brief Retrieve the lambda mangling number for a lambda expression. 2031 unsigned getLambdaManglingNumber(CXXMethodDecl *CallOperator); 2032 2033 /// \brief Used by ParmVarDecl to store on the side the 2034 /// index of the parameter when it exceeds the size of the normal bitfield. 2035 void setParameterIndex(const ParmVarDecl *D, unsigned index); 2036 2037 /// \brief Used by ParmVarDecl to retrieve on the side the 2038 /// index of the parameter when it exceeds the size of the normal bitfield. 2039 unsigned getParameterIndex(const ParmVarDecl *D) const; 2040 2041 //===--------------------------------------------------------------------===// 2042 // Statistics 2043 //===--------------------------------------------------------------------===// 2044 2045 /// \brief The number of implicitly-declared default constructors. 2046 static unsigned NumImplicitDefaultConstructors; 2047 2048 /// \brief The number of implicitly-declared default constructors for 2049 /// which declarations were built. 2050 static unsigned NumImplicitDefaultConstructorsDeclared; 2051 2052 /// \brief The number of implicitly-declared copy constructors. 2053 static unsigned NumImplicitCopyConstructors; 2054 2055 /// \brief The number of implicitly-declared copy constructors for 2056 /// which declarations were built. 2057 static unsigned NumImplicitCopyConstructorsDeclared; 2058 2059 /// \brief The number of implicitly-declared move constructors. 2060 static unsigned NumImplicitMoveConstructors; 2061 2062 /// \brief The number of implicitly-declared move constructors for 2063 /// which declarations were built. 2064 static unsigned NumImplicitMoveConstructorsDeclared; 2065 2066 /// \brief The number of implicitly-declared copy assignment operators. 2067 static unsigned NumImplicitCopyAssignmentOperators; 2068 2069 /// \brief The number of implicitly-declared copy assignment operators for 2070 /// which declarations were built. 2071 static unsigned NumImplicitCopyAssignmentOperatorsDeclared; 2072 2073 /// \brief The number of implicitly-declared move assignment operators. 2074 static unsigned NumImplicitMoveAssignmentOperators; 2075 2076 /// \brief The number of implicitly-declared move assignment operators for 2077 /// which declarations were built. 2078 static unsigned NumImplicitMoveAssignmentOperatorsDeclared; 2079 2080 /// \brief The number of implicitly-declared destructors. 2081 static unsigned NumImplicitDestructors; 2082 2083 /// \brief The number of implicitly-declared destructors for which 2084 /// declarations were built. 2085 static unsigned NumImplicitDestructorsDeclared; 2086 2087private: 2088 ASTContext(const ASTContext &) LLVM_DELETED_FUNCTION; 2089 void operator=(const ASTContext &) LLVM_DELETED_FUNCTION; 2090 2091public: 2092 /// \brief Initialize built-in types. 2093 /// 2094 /// This routine may only be invoked once for a given ASTContext object. 2095 /// It is normally invoked by the ASTContext constructor. However, the 2096 /// constructor can be asked to delay initialization, which places the burden 2097 /// of calling this function on the user of that object. 2098 /// 2099 /// \param Target The target 2100 void InitBuiltinTypes(const TargetInfo &Target); 2101 2102private: 2103 void InitBuiltinType(CanQualType &R, BuiltinType::Kind K); 2104 2105 // Return the Objective-C type encoding for a given type. 2106 void getObjCEncodingForTypeImpl(QualType t, std::string &S, 2107 bool ExpandPointedToStructures, 2108 bool ExpandStructures, 2109 const FieldDecl *Field, 2110 bool OutermostType = false, 2111 bool EncodingProperty = false, 2112 bool StructField = false, 2113 bool EncodeBlockParameters = false, 2114 bool EncodeClassNames = false) const; 2115 2116 // Adds the encoding of the structure's members. 2117 void getObjCEncodingForStructureImpl(RecordDecl *RD, std::string &S, 2118 const FieldDecl *Field, 2119 bool includeVBases = true) const; 2120 2121 // Adds the encoding of a method parameter or return type. 2122 void getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT, 2123 QualType T, std::string& S, 2124 bool Extended) const; 2125 2126 const ASTRecordLayout & 2127 getObjCLayout(const ObjCInterfaceDecl *D, 2128 const ObjCImplementationDecl *Impl) const; 2129 2130private: 2131 /// \brief A set of deallocations that should be performed when the 2132 /// ASTContext is destroyed. 2133 SmallVector<std::pair<void (*)(void*), void *>, 16> Deallocations; 2134 2135 // FIXME: This currently contains the set of StoredDeclMaps used 2136 // by DeclContext objects. This probably should not be in ASTContext, 2137 // but we include it here so that ASTContext can quickly deallocate them. 2138 llvm::PointerIntPair<StoredDeclsMap*,1> LastSDM; 2139 2140 /// \brief A counter used to uniquely identify "blocks". 2141 mutable unsigned int UniqueBlockByRefTypeID; 2142 2143 friend class DeclContext; 2144 friend class DeclarationNameTable; 2145 void ReleaseDeclContextMaps(); 2146}; 2147 2148/// \brief Utility function for constructing a nullary selector. 2149static inline Selector GetNullarySelector(StringRef name, ASTContext& Ctx) { 2150 IdentifierInfo* II = &Ctx.Idents.get(name); 2151 return Ctx.Selectors.getSelector(0, &II); 2152} 2153 2154/// \brief Utility function for constructing an unary selector. 2155static inline Selector GetUnarySelector(StringRef name, ASTContext& Ctx) { 2156 IdentifierInfo* II = &Ctx.Idents.get(name); 2157 return Ctx.Selectors.getSelector(1, &II); 2158} 2159 2160} // end namespace clang 2161 2162// operator new and delete aren't allowed inside namespaces. 2163 2164/// @brief Placement new for using the ASTContext's allocator. 2165/// 2166/// This placement form of operator new uses the ASTContext's allocator for 2167/// obtaining memory. 2168/// 2169/// IMPORTANT: These are also declared in clang/AST/AttrIterator.h! Any changes 2170/// here need to also be made there. 2171/// 2172/// We intentionally avoid using a nothrow specification here so that the calls 2173/// to this operator will not perform a null check on the result -- the 2174/// underlying allocator never returns null pointers. 2175/// 2176/// Usage looks like this (assuming there's an ASTContext 'Context' in scope): 2177/// @code 2178/// // Default alignment (8) 2179/// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments); 2180/// // Specific alignment 2181/// IntegerLiteral *Ex2 = new (Context, 4) IntegerLiteral(arguments); 2182/// @endcode 2183/// Please note that you cannot use delete on the pointer; it must be 2184/// deallocated using an explicit destructor call followed by 2185/// @c Context.Deallocate(Ptr). 2186/// 2187/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 2188/// @param C The ASTContext that provides the allocator. 2189/// @param Alignment The alignment of the allocated memory (if the underlying 2190/// allocator supports it). 2191/// @return The allocated memory. Could be NULL. 2192inline void *operator new(size_t Bytes, const clang::ASTContext &C, 2193 size_t Alignment) { 2194 return C.Allocate(Bytes, Alignment); 2195} 2196/// @brief Placement delete companion to the new above. 2197/// 2198/// This operator is just a companion to the new above. There is no way of 2199/// invoking it directly; see the new operator for more details. This operator 2200/// is called implicitly by the compiler if a placement new expression using 2201/// the ASTContext throws in the object constructor. 2202inline void operator delete(void *Ptr, const clang::ASTContext &C, size_t) { 2203 C.Deallocate(Ptr); 2204} 2205 2206/// This placement form of operator new[] uses the ASTContext's allocator for 2207/// obtaining memory. 2208/// 2209/// We intentionally avoid using a nothrow specification here so that the calls 2210/// to this operator will not perform a null check on the result -- the 2211/// underlying allocator never returns null pointers. 2212/// 2213/// Usage looks like this (assuming there's an ASTContext 'Context' in scope): 2214/// @code 2215/// // Default alignment (8) 2216/// char *data = new (Context) char[10]; 2217/// // Specific alignment 2218/// char *data = new (Context, 4) char[10]; 2219/// @endcode 2220/// Please note that you cannot use delete on the pointer; it must be 2221/// deallocated using an explicit destructor call followed by 2222/// @c Context.Deallocate(Ptr). 2223/// 2224/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 2225/// @param C The ASTContext that provides the allocator. 2226/// @param Alignment The alignment of the allocated memory (if the underlying 2227/// allocator supports it). 2228/// @return The allocated memory. Could be NULL. 2229inline void *operator new[](size_t Bytes, const clang::ASTContext& C, 2230 size_t Alignment = 8) { 2231 return C.Allocate(Bytes, Alignment); 2232} 2233 2234/// @brief Placement delete[] companion to the new[] above. 2235/// 2236/// This operator is just a companion to the new[] above. There is no way of 2237/// invoking it directly; see the new[] operator for more details. This operator 2238/// is called implicitly by the compiler if a placement new[] expression using 2239/// the ASTContext throws in the object constructor. 2240inline void operator delete[](void *Ptr, const clang::ASTContext &C, size_t) { 2241 C.Deallocate(Ptr); 2242} 2243 2244#endif 2245