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