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