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