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