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