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