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