ASTContext.h revision dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7
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 bool Extended = false) 1012 const; 1013 1014 /// getObjCEncodingForBlock - Return the encoded type for this block 1015 /// declaration. 1016 std::string getObjCEncodingForBlock(const BlockExpr *blockExpr) const; 1017 1018 /// getObjCEncodingForPropertyDecl - Return the encoded type for 1019 /// this method declaration. If non-NULL, Container must be either 1020 /// an ObjCCategoryImplDecl or ObjCImplementationDecl; it should 1021 /// only be NULL when getting encodings for protocol properties. 1022 void getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, 1023 const Decl *Container, 1024 std::string &S) const; 1025 1026 bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, 1027 ObjCProtocolDecl *rProto) const; 1028 1029 /// getObjCEncodingTypeSize returns size of type for objective-c encoding 1030 /// purpose in characters. 1031 CharUnits getObjCEncodingTypeSize(QualType t) const; 1032 1033 /// \brief Retrieve the typedef corresponding to the predefined 'id' type 1034 /// in Objective-C. 1035 TypedefDecl *getObjCIdDecl() const; 1036 1037 /// This setter/getter represents the ObjC 'id' type. It is setup lazily, by 1038 /// Sema. id is always a (typedef for a) pointer type, a pointer to a struct. 1039 QualType getObjCIdType() const { 1040 return getTypeDeclType(getObjCIdDecl()); 1041 } 1042 1043 /// \brief Retrieve the typedef corresponding to the predefined 'SEL' type 1044 /// in Objective-C. 1045 TypedefDecl *getObjCSelDecl() const; 1046 1047 /// \brief Retrieve the type that corresponds to the predefined Objective-C 1048 /// 'SEL' type. 1049 QualType getObjCSelType() const { 1050 return getTypeDeclType(getObjCSelDecl()); 1051 } 1052 1053 void setObjCProtoType(QualType QT); 1054 QualType getObjCProtoType() const { return ObjCProtoType; } 1055 1056 /// \brief Retrieve the typedef declaration corresponding to the predefined 1057 /// Objective-C 'Class' type. 1058 TypedefDecl *getObjCClassDecl() const; 1059 1060 /// This setter/getter repreents the ObjC 'Class' type. It is setup lazily, by 1061 /// Sema. 'Class' is always a (typedef for a) pointer type, a pointer to a 1062 /// struct. 1063 QualType getObjCClassType() const { 1064 return getTypeDeclType(getObjCClassDecl()); 1065 } 1066 1067 void setBuiltinVaListType(QualType T); 1068 QualType getBuiltinVaListType() const { return BuiltinVaListType; } 1069 1070 /// getCVRQualifiedType - Returns a type with additional const, 1071 /// volatile, or restrict qualifiers. 1072 QualType getCVRQualifiedType(QualType T, unsigned CVR) const { 1073 return getQualifiedType(T, Qualifiers::fromCVRMask(CVR)); 1074 } 1075 1076 /// getQualifiedType - Returns a type with additional qualifiers. 1077 QualType getQualifiedType(QualType T, Qualifiers Qs) const { 1078 if (!Qs.hasNonFastQualifiers()) 1079 return T.withFastQualifiers(Qs.getFastQualifiers()); 1080 QualifierCollector Qc(Qs); 1081 const Type *Ptr = Qc.strip(T); 1082 return getExtQualType(Ptr, Qc); 1083 } 1084 1085 /// getQualifiedType - Returns a type with additional qualifiers. 1086 QualType getQualifiedType(const Type *T, Qualifiers Qs) const { 1087 if (!Qs.hasNonFastQualifiers()) 1088 return QualType(T, Qs.getFastQualifiers()); 1089 return getExtQualType(T, Qs); 1090 } 1091 1092 /// getLifetimeQualifiedType - Returns a type with the given 1093 /// lifetime qualifier. 1094 QualType getLifetimeQualifiedType(QualType type, 1095 Qualifiers::ObjCLifetime lifetime) { 1096 assert(type.getObjCLifetime() == Qualifiers::OCL_None); 1097 assert(lifetime != Qualifiers::OCL_None); 1098 1099 Qualifiers qs; 1100 qs.addObjCLifetime(lifetime); 1101 return getQualifiedType(type, qs); 1102 } 1103 1104 DeclarationNameInfo getNameForTemplate(TemplateName Name, 1105 SourceLocation NameLoc) const; 1106 1107 TemplateName getOverloadedTemplateName(UnresolvedSetIterator Begin, 1108 UnresolvedSetIterator End) const; 1109 1110 TemplateName getQualifiedTemplateName(NestedNameSpecifier *NNS, 1111 bool TemplateKeyword, 1112 TemplateDecl *Template) const; 1113 1114 TemplateName getDependentTemplateName(NestedNameSpecifier *NNS, 1115 const IdentifierInfo *Name) const; 1116 TemplateName getDependentTemplateName(NestedNameSpecifier *NNS, 1117 OverloadedOperatorKind Operator) const; 1118 TemplateName getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param, 1119 TemplateName replacement) const; 1120 TemplateName getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param, 1121 const TemplateArgument &ArgPack) const; 1122 1123 enum GetBuiltinTypeError { 1124 GE_None, //< No error 1125 GE_Missing_stdio, //< Missing a type from <stdio.h> 1126 GE_Missing_setjmp, //< Missing a type from <setjmp.h> 1127 GE_Missing_ucontext //< Missing a type from <ucontext.h> 1128 }; 1129 1130 /// GetBuiltinType - Return the type for the specified builtin. If 1131 /// IntegerConstantArgs is non-null, it is filled in with a bitmask of 1132 /// arguments to the builtin that are required to be integer constant 1133 /// expressions. 1134 QualType GetBuiltinType(unsigned ID, GetBuiltinTypeError &Error, 1135 unsigned *IntegerConstantArgs = 0) const; 1136 1137private: 1138 CanQualType getFromTargetType(unsigned Type) const; 1139 1140 //===--------------------------------------------------------------------===// 1141 // Type Predicates. 1142 //===--------------------------------------------------------------------===// 1143 1144public: 1145 /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's 1146 /// garbage collection attribute. 1147 /// 1148 Qualifiers::GC getObjCGCAttrKind(QualType Ty) const; 1149 1150 /// areCompatibleVectorTypes - Return true if the given vector types 1151 /// are of the same unqualified type or if they are equivalent to the same 1152 /// GCC vector type, ignoring whether they are target-specific (AltiVec or 1153 /// Neon) types. 1154 bool areCompatibleVectorTypes(QualType FirstVec, QualType SecondVec); 1155 1156 /// isObjCNSObjectType - Return true if this is an NSObject object with 1157 /// its NSObject attribute set. 1158 static bool isObjCNSObjectType(QualType Ty) { 1159 return Ty->isObjCNSObjectType(); 1160 } 1161 1162 //===--------------------------------------------------------------------===// 1163 // Type Sizing and Analysis 1164 //===--------------------------------------------------------------------===// 1165 1166 /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified 1167 /// scalar floating point type. 1168 const llvm::fltSemantics &getFloatTypeSemantics(QualType T) const; 1169 1170 /// getTypeInfo - Get the size and alignment of the specified complete type in 1171 /// bits. 1172 std::pair<uint64_t, unsigned> getTypeInfo(const Type *T) const; 1173 std::pair<uint64_t, unsigned> getTypeInfo(QualType T) const { 1174 return getTypeInfo(T.getTypePtr()); 1175 } 1176 1177 /// getTypeSize - Return the size of the specified type, in bits. This method 1178 /// does not work on incomplete types. 1179 uint64_t getTypeSize(QualType T) const { 1180 return getTypeInfo(T).first; 1181 } 1182 uint64_t getTypeSize(const Type *T) const { 1183 return getTypeInfo(T).first; 1184 } 1185 1186 /// getCharWidth - Return the size of the character type, in bits 1187 uint64_t getCharWidth() const { 1188 return getTypeSize(CharTy); 1189 } 1190 1191 /// toCharUnitsFromBits - Convert a size in bits to a size in characters. 1192 CharUnits toCharUnitsFromBits(int64_t BitSize) const; 1193 1194 /// toBits - Convert a size in characters to a size in bits. 1195 int64_t toBits(CharUnits CharSize) const; 1196 1197 /// getTypeSizeInChars - Return the size of the specified type, in characters. 1198 /// This method does not work on incomplete types. 1199 CharUnits getTypeSizeInChars(QualType T) const; 1200 CharUnits getTypeSizeInChars(const Type *T) const; 1201 1202 /// getTypeAlign - Return the ABI-specified alignment of a type, in bits. 1203 /// This method does not work on incomplete types. 1204 unsigned getTypeAlign(QualType T) const { 1205 return getTypeInfo(T).second; 1206 } 1207 unsigned getTypeAlign(const Type *T) const { 1208 return getTypeInfo(T).second; 1209 } 1210 1211 /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in 1212 /// characters. This method does not work on incomplete types. 1213 CharUnits getTypeAlignInChars(QualType T) const; 1214 CharUnits getTypeAlignInChars(const Type *T) const; 1215 1216 std::pair<CharUnits, CharUnits> getTypeInfoInChars(const Type *T) const; 1217 std::pair<CharUnits, CharUnits> getTypeInfoInChars(QualType T) const; 1218 1219 /// getPreferredTypeAlign - Return the "preferred" alignment of the specified 1220 /// type for the current target in bits. This can be different than the ABI 1221 /// alignment in cases where it is beneficial for performance to overalign 1222 /// a data type. 1223 unsigned getPreferredTypeAlign(const Type *T) const; 1224 1225 /// getDeclAlign - Return a conservative estimate of the alignment of 1226 /// the specified decl. Note that bitfields do not have a valid alignment, so 1227 /// this method will assert on them. 1228 /// If @p RefAsPointee, references are treated like their underlying type 1229 /// (for alignof), else they're treated like pointers (for CodeGen). 1230 CharUnits getDeclAlign(const Decl *D, bool RefAsPointee = false) const; 1231 1232 /// getASTRecordLayout - Get or compute information about the layout of the 1233 /// specified record (struct/union/class), which indicates its size and field 1234 /// position information. 1235 const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D) const; 1236 1237 /// getASTObjCInterfaceLayout - Get or compute information about the 1238 /// layout of the specified Objective-C interface. 1239 const ASTRecordLayout &getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) 1240 const; 1241 1242 void DumpRecordLayout(const RecordDecl *RD, raw_ostream &OS) const; 1243 1244 /// getASTObjCImplementationLayout - Get or compute information about 1245 /// the layout of the specified Objective-C implementation. This may 1246 /// differ from the interface if synthesized ivars are present. 1247 const ASTRecordLayout & 1248 getASTObjCImplementationLayout(const ObjCImplementationDecl *D) const; 1249 1250 /// getKeyFunction - Get the key function for the given record decl, or NULL 1251 /// if there isn't one. The key function is, according to the Itanium C++ ABI 1252 /// section 5.2.3: 1253 /// 1254 /// ...the first non-pure virtual function that is not inline at the point 1255 /// of class definition. 1256 const CXXMethodDecl *getKeyFunction(const CXXRecordDecl *RD); 1257 1258 bool isNearlyEmpty(const CXXRecordDecl *RD) const; 1259 1260 MangleContext *createMangleContext(); 1261 1262 void DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, bool leafClass, 1263 SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const; 1264 1265 unsigned CountNonClassIvars(const ObjCInterfaceDecl *OI) const; 1266 void CollectInheritedProtocols(const Decl *CDecl, 1267 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols); 1268 1269 //===--------------------------------------------------------------------===// 1270 // Type Operators 1271 //===--------------------------------------------------------------------===// 1272 1273 /// getCanonicalType - Return the canonical (structural) type corresponding to 1274 /// the specified potentially non-canonical type. The non-canonical version 1275 /// of a type may have many "decorated" versions of types. Decorators can 1276 /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed 1277 /// to be free of any of these, allowing two canonical types to be compared 1278 /// for exact equality with a simple pointer comparison. 1279 CanQualType getCanonicalType(QualType T) const { 1280 return CanQualType::CreateUnsafe(T.getCanonicalType()); 1281 } 1282 1283 const Type *getCanonicalType(const Type *T) const { 1284 return T->getCanonicalTypeInternal().getTypePtr(); 1285 } 1286 1287 /// getCanonicalParamType - Return the canonical parameter type 1288 /// corresponding to the specific potentially non-canonical one. 1289 /// Qualifiers are stripped off, functions are turned into function 1290 /// pointers, and arrays decay one level into pointers. 1291 CanQualType getCanonicalParamType(QualType T) const; 1292 1293 /// \brief Determine whether the given types are equivalent. 1294 bool hasSameType(QualType T1, QualType T2) const { 1295 return getCanonicalType(T1) == getCanonicalType(T2); 1296 } 1297 1298 /// \brief Returns this type as a completely-unqualified array type, 1299 /// capturing the qualifiers in Quals. This will remove the minimal amount of 1300 /// sugaring from the types, similar to the behavior of 1301 /// QualType::getUnqualifiedType(). 1302 /// 1303 /// \param T is the qualified type, which may be an ArrayType 1304 /// 1305 /// \param Quals will receive the full set of qualifiers that were 1306 /// applied to the array. 1307 /// 1308 /// \returns if this is an array type, the completely unqualified array type 1309 /// that corresponds to it. Otherwise, returns T.getUnqualifiedType(). 1310 QualType getUnqualifiedArrayType(QualType T, Qualifiers &Quals); 1311 1312 /// \brief Determine whether the given types are equivalent after 1313 /// cvr-qualifiers have been removed. 1314 bool hasSameUnqualifiedType(QualType T1, QualType T2) const { 1315 return getCanonicalType(T1).getTypePtr() == 1316 getCanonicalType(T2).getTypePtr(); 1317 } 1318 1319 bool UnwrapSimilarPointerTypes(QualType &T1, QualType &T2); 1320 1321 /// \brief Retrieves the "canonical" nested name specifier for a 1322 /// given nested name specifier. 1323 /// 1324 /// The canonical nested name specifier is a nested name specifier 1325 /// that uniquely identifies a type or namespace within the type 1326 /// system. For example, given: 1327 /// 1328 /// \code 1329 /// namespace N { 1330 /// struct S { 1331 /// template<typename T> struct X { typename T* type; }; 1332 /// }; 1333 /// } 1334 /// 1335 /// template<typename T> struct Y { 1336 /// typename N::S::X<T>::type member; 1337 /// }; 1338 /// \endcode 1339 /// 1340 /// Here, the nested-name-specifier for N::S::X<T>:: will be 1341 /// S::X<template-param-0-0>, since 'S' and 'X' are uniquely defined 1342 /// by declarations in the type system and the canonical type for 1343 /// the template type parameter 'T' is template-param-0-0. 1344 NestedNameSpecifier * 1345 getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const; 1346 1347 /// \brief Retrieves the default calling convention to use for 1348 /// C++ instance methods. 1349 CallingConv getDefaultMethodCallConv(); 1350 1351 /// \brief Retrieves the canonical representation of the given 1352 /// calling convention. 1353 CallingConv getCanonicalCallConv(CallingConv CC) const { 1354 if (!LangOpts.MRTD && CC == CC_C) 1355 return CC_Default; 1356 return CC; 1357 } 1358 1359 /// \brief Determines whether two calling conventions name the same 1360 /// calling convention. 1361 bool isSameCallConv(CallingConv lcc, CallingConv rcc) { 1362 return (getCanonicalCallConv(lcc) == getCanonicalCallConv(rcc)); 1363 } 1364 1365 /// \brief Retrieves the "canonical" template name that refers to a 1366 /// given template. 1367 /// 1368 /// The canonical template name is the simplest expression that can 1369 /// be used to refer to a given template. For most templates, this 1370 /// expression is just the template declaration itself. For example, 1371 /// the template std::vector can be referred to via a variety of 1372 /// names---std::vector, ::std::vector, vector (if vector is in 1373 /// scope), etc.---but all of these names map down to the same 1374 /// TemplateDecl, which is used to form the canonical template name. 1375 /// 1376 /// Dependent template names are more interesting. Here, the 1377 /// template name could be something like T::template apply or 1378 /// std::allocator<T>::template rebind, where the nested name 1379 /// specifier itself is dependent. In this case, the canonical 1380 /// template name uses the shortest form of the dependent 1381 /// nested-name-specifier, which itself contains all canonical 1382 /// types, values, and templates. 1383 TemplateName getCanonicalTemplateName(TemplateName Name) const; 1384 1385 /// \brief Determine whether the given template names refer to the same 1386 /// template. 1387 bool hasSameTemplateName(TemplateName X, TemplateName Y); 1388 1389 /// \brief Retrieve the "canonical" template argument. 1390 /// 1391 /// The canonical template argument is the simplest template argument 1392 /// (which may be a type, value, expression, or declaration) that 1393 /// expresses the value of the argument. 1394 TemplateArgument getCanonicalTemplateArgument(const TemplateArgument &Arg) 1395 const; 1396 1397 /// Type Query functions. If the type is an instance of the specified class, 1398 /// return the Type pointer for the underlying maximally pretty type. This 1399 /// is a member of ASTContext because this may need to do some amount of 1400 /// canonicalization, e.g. to move type qualifiers into the element type. 1401 const ArrayType *getAsArrayType(QualType T) const; 1402 const ConstantArrayType *getAsConstantArrayType(QualType T) const { 1403 return dyn_cast_or_null<ConstantArrayType>(getAsArrayType(T)); 1404 } 1405 const VariableArrayType *getAsVariableArrayType(QualType T) const { 1406 return dyn_cast_or_null<VariableArrayType>(getAsArrayType(T)); 1407 } 1408 const IncompleteArrayType *getAsIncompleteArrayType(QualType T) const { 1409 return dyn_cast_or_null<IncompleteArrayType>(getAsArrayType(T)); 1410 } 1411 const DependentSizedArrayType *getAsDependentSizedArrayType(QualType T) 1412 const { 1413 return dyn_cast_or_null<DependentSizedArrayType>(getAsArrayType(T)); 1414 } 1415 1416 /// getBaseElementType - Returns the innermost element type of an array type. 1417 /// For example, will return "int" for int[m][n] 1418 QualType getBaseElementType(const ArrayType *VAT) const; 1419 1420 /// getBaseElementType - Returns the innermost element type of a type 1421 /// (which needn't actually be an array type). 1422 QualType getBaseElementType(QualType QT) const; 1423 1424 /// getConstantArrayElementCount - Returns number of constant array elements. 1425 uint64_t getConstantArrayElementCount(const ConstantArrayType *CA) const; 1426 1427 /// \brief Perform adjustment on the parameter type of a function. 1428 /// 1429 /// This routine adjusts the given parameter type @p T to the actual 1430 /// parameter type used by semantic analysis (C99 6.7.5.3p[7,8], 1431 /// C++ [dcl.fct]p3). The adjusted parameter type is returned. 1432 QualType getAdjustedParameterType(QualType T); 1433 1434 /// \brief Retrieve the parameter type as adjusted for use in the signature 1435 /// of a function, decaying array and function types and removing top-level 1436 /// cv-qualifiers. 1437 QualType getSignatureParameterType(QualType T); 1438 1439 /// getArrayDecayedType - Return the properly qualified result of decaying the 1440 /// specified array type to a pointer. This operation is non-trivial when 1441 /// handling typedefs etc. The canonical type of "T" must be an array type, 1442 /// this returns a pointer to a properly qualified element of the array. 1443 /// 1444 /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. 1445 QualType getArrayDecayedType(QualType T) const; 1446 1447 /// getPromotedIntegerType - Returns the type that Promotable will 1448 /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable 1449 /// integer type. 1450 QualType getPromotedIntegerType(QualType PromotableType) const; 1451 1452 /// \brief Recurses in pointer/array types until it finds an objc retainable 1453 /// type and returns its ownership. 1454 Qualifiers::ObjCLifetime getInnerObjCOwnership(QualType T) const; 1455 1456 /// \brief Whether this is a promotable bitfield reference according 1457 /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). 1458 /// 1459 /// \returns the type this bit-field will promote to, or NULL if no 1460 /// promotion occurs. 1461 QualType isPromotableBitField(Expr *E) const; 1462 1463 /// getIntegerTypeOrder - Returns the highest ranked integer type: 1464 /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If 1465 /// LHS < RHS, return -1. 1466 int getIntegerTypeOrder(QualType LHS, QualType RHS) const; 1467 1468 /// getFloatingTypeOrder - Compare the rank of the two specified floating 1469 /// point types, ignoring the domain of the type (i.e. 'double' == 1470 /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If 1471 /// LHS < RHS, return -1. 1472 int getFloatingTypeOrder(QualType LHS, QualType RHS) const; 1473 1474 /// getFloatingTypeOfSizeWithinDomain - Returns a real floating 1475 /// point or a complex type (based on typeDomain/typeSize). 1476 /// 'typeDomain' is a real floating point or complex type. 1477 /// 'typeSize' is a real floating point or complex type. 1478 QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize, 1479 QualType typeDomain) const; 1480 1481 unsigned getTargetAddressSpace(QualType T) const { 1482 return getTargetAddressSpace(T.getQualifiers()); 1483 } 1484 1485 unsigned getTargetAddressSpace(Qualifiers Q) const { 1486 return getTargetAddressSpace(Q.getAddressSpace()); 1487 } 1488 1489 unsigned getTargetAddressSpace(unsigned AS) const { 1490 if (AS < LangAS::Offset || AS >= LangAS::Offset + LangAS::Count) 1491 return AS; 1492 else 1493 return (*AddrSpaceMap)[AS - LangAS::Offset]; 1494 } 1495 1496private: 1497 // Helper for integer ordering 1498 unsigned getIntegerRank(const Type *T) const; 1499 1500public: 1501 1502 //===--------------------------------------------------------------------===// 1503 // Type Compatibility Predicates 1504 //===--------------------------------------------------------------------===// 1505 1506 /// Compatibility predicates used to check assignment expressions. 1507 bool typesAreCompatible(QualType T1, QualType T2, 1508 bool CompareUnqualified = false); // C99 6.2.7p1 1509 1510 bool propertyTypesAreCompatible(QualType, QualType); 1511 bool typesAreBlockPointerCompatible(QualType, QualType); 1512 1513 bool isObjCIdType(QualType T) const { 1514 return T == getObjCIdType(); 1515 } 1516 bool isObjCClassType(QualType T) const { 1517 return T == getObjCClassType(); 1518 } 1519 bool isObjCSelType(QualType T) const { 1520 return T == getObjCSelType(); 1521 } 1522 bool QualifiedIdConformsQualifiedId(QualType LHS, QualType RHS); 1523 bool ObjCQualifiedIdTypesAreCompatible(QualType LHS, QualType RHS, 1524 bool ForCompare); 1525 1526 bool ObjCQualifiedClassTypesAreCompatible(QualType LHS, QualType RHS); 1527 1528 // Check the safety of assignment from LHS to RHS 1529 bool canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, 1530 const ObjCObjectPointerType *RHSOPT); 1531 bool canAssignObjCInterfaces(const ObjCObjectType *LHS, 1532 const ObjCObjectType *RHS); 1533 bool canAssignObjCInterfacesInBlockPointer( 1534 const ObjCObjectPointerType *LHSOPT, 1535 const ObjCObjectPointerType *RHSOPT, 1536 bool BlockReturnType); 1537 bool areComparableObjCPointerTypes(QualType LHS, QualType RHS); 1538 QualType areCommonBaseCompatible(const ObjCObjectPointerType *LHSOPT, 1539 const ObjCObjectPointerType *RHSOPT); 1540 bool canBindObjCObjectType(QualType To, QualType From); 1541 1542 // Functions for calculating composite types 1543 QualType mergeTypes(QualType, QualType, bool OfBlockPointer=false, 1544 bool Unqualified = false, bool BlockReturnType = false); 1545 QualType mergeFunctionTypes(QualType, QualType, bool OfBlockPointer=false, 1546 bool Unqualified = false); 1547 QualType mergeFunctionArgumentTypes(QualType, QualType, 1548 bool OfBlockPointer=false, 1549 bool Unqualified = false); 1550 QualType mergeTransparentUnionType(QualType, QualType, 1551 bool OfBlockPointer=false, 1552 bool Unqualified = false); 1553 1554 QualType mergeObjCGCQualifiers(QualType, QualType); 1555 1556 bool FunctionTypesMatchOnNSConsumedAttrs( 1557 const FunctionProtoType *FromFunctionType, 1558 const FunctionProtoType *ToFunctionType); 1559 1560 void ResetObjCLayout(const ObjCContainerDecl *CD) { 1561 ObjCLayouts[CD] = 0; 1562 } 1563 1564 //===--------------------------------------------------------------------===// 1565 // Integer Predicates 1566 //===--------------------------------------------------------------------===// 1567 1568 // The width of an integer, as defined in C99 6.2.6.2. This is the number 1569 // of bits in an integer type excluding any padding bits. 1570 unsigned getIntWidth(QualType T) const; 1571 1572 // Per C99 6.2.5p6, for every signed integer type, there is a corresponding 1573 // unsigned integer type. This method takes a signed type, and returns the 1574 // corresponding unsigned integer type. 1575 QualType getCorrespondingUnsignedType(QualType T); 1576 1577 //===--------------------------------------------------------------------===// 1578 // Type Iterators. 1579 //===--------------------------------------------------------------------===// 1580 1581 typedef std::vector<Type*>::iterator type_iterator; 1582 typedef std::vector<Type*>::const_iterator const_type_iterator; 1583 1584 type_iterator types_begin() { return Types.begin(); } 1585 type_iterator types_end() { return Types.end(); } 1586 const_type_iterator types_begin() const { return Types.begin(); } 1587 const_type_iterator types_end() const { return Types.end(); } 1588 1589 //===--------------------------------------------------------------------===// 1590 // Integer Values 1591 //===--------------------------------------------------------------------===// 1592 1593 /// MakeIntValue - Make an APSInt of the appropriate width and 1594 /// signedness for the given \arg Value and integer \arg Type. 1595 llvm::APSInt MakeIntValue(uint64_t Value, QualType Type) const { 1596 llvm::APSInt Res(getIntWidth(Type), 1597 !Type->isSignedIntegerOrEnumerationType()); 1598 Res = Value; 1599 return Res; 1600 } 1601 1602 /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. 1603 ObjCImplementationDecl *getObjCImplementation(ObjCInterfaceDecl *D); 1604 /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. 1605 ObjCCategoryImplDecl *getObjCImplementation(ObjCCategoryDecl *D); 1606 1607 /// \brief returns true if there is at lease one @implementation in TU. 1608 bool AnyObjCImplementation() { 1609 return !ObjCImpls.empty(); 1610 } 1611 1612 /// \brief Set the implementation of ObjCInterfaceDecl. 1613 void setObjCImplementation(ObjCInterfaceDecl *IFaceD, 1614 ObjCImplementationDecl *ImplD); 1615 /// \brief Set the implementation of ObjCCategoryDecl. 1616 void setObjCImplementation(ObjCCategoryDecl *CatD, 1617 ObjCCategoryImplDecl *ImplD); 1618 1619 /// \brief Get the duplicate declaration of a ObjCMethod in the same 1620 /// interface, or null if non exists. 1621 const ObjCMethodDecl *getObjCMethodRedeclaration( 1622 const ObjCMethodDecl *MD) const { 1623 llvm::DenseMap<const ObjCMethodDecl*, const ObjCMethodDecl*>::const_iterator 1624 I = ObjCMethodRedecls.find(MD); 1625 if (I == ObjCMethodRedecls.end()) 1626 return 0; 1627 return I->second; 1628 } 1629 1630 void setObjCMethodRedeclaration(const ObjCMethodDecl *MD, 1631 const ObjCMethodDecl *Redecl) { 1632 ObjCMethodRedecls[MD] = Redecl; 1633 } 1634 1635 /// \brief Returns the objc interface that \arg ND belongs to if it is a 1636 /// objc method/property/ivar etc. that is part of an interface, 1637 /// otherwise returns null. 1638 ObjCInterfaceDecl *getObjContainingInterface(NamedDecl *ND) const; 1639 1640 /// \brief Set the copy inialization expression of a block var decl. 1641 void setBlockVarCopyInits(VarDecl*VD, Expr* Init); 1642 /// \brief Get the copy initialization expression of VarDecl,or NULL if 1643 /// none exists. 1644 Expr *getBlockVarCopyInits(const VarDecl*VD); 1645 1646 /// \brief Allocate an uninitialized TypeSourceInfo. 1647 /// 1648 /// The caller should initialize the memory held by TypeSourceInfo using 1649 /// the TypeLoc wrappers. 1650 /// 1651 /// \param T the type that will be the basis for type source info. This type 1652 /// should refer to how the declarator was written in source code, not to 1653 /// what type semantic analysis resolved the declarator to. 1654 /// 1655 /// \param Size the size of the type info to create, or 0 if the size 1656 /// should be calculated based on the type. 1657 TypeSourceInfo *CreateTypeSourceInfo(QualType T, unsigned Size = 0) const; 1658 1659 /// \brief Allocate a TypeSourceInfo where all locations have been 1660 /// initialized to a given location, which defaults to the empty 1661 /// location. 1662 TypeSourceInfo * 1663 getTrivialTypeSourceInfo(QualType T, 1664 SourceLocation Loc = SourceLocation()) const; 1665 1666 TypeSourceInfo *getNullTypeSourceInfo() { return &NullTypeSourceInfo; } 1667 1668 /// \brief Add a deallocation callback that will be invoked when the 1669 /// ASTContext is destroyed. 1670 /// 1671 /// \brief Callback A callback function that will be invoked on destruction. 1672 /// 1673 /// \brief Data Pointer data that will be provided to the callback function 1674 /// when it is called. 1675 void AddDeallocation(void (*Callback)(void*), void *Data); 1676 1677 GVALinkage GetGVALinkageForFunction(const FunctionDecl *FD); 1678 GVALinkage GetGVALinkageForVariable(const VarDecl *VD); 1679 1680 /// \brief Determines if the decl can be CodeGen'ed or deserialized from PCH 1681 /// lazily, only when used; this is only relevant for function or file scoped 1682 /// var definitions. 1683 /// 1684 /// \returns true if the function/var must be CodeGen'ed/deserialized even if 1685 /// it is not used. 1686 bool DeclMustBeEmitted(const Decl *D); 1687 1688 1689 /// \brief Used by ParmVarDecl to store on the side the 1690 /// index of the parameter when it exceeds the size of the normal bitfield. 1691 void setParameterIndex(const ParmVarDecl *D, unsigned index); 1692 1693 /// \brief Used by ParmVarDecl to retrieve on the side the 1694 /// index of the parameter when it exceeds the size of the normal bitfield. 1695 unsigned getParameterIndex(const ParmVarDecl *D) const; 1696 1697 //===--------------------------------------------------------------------===// 1698 // Statistics 1699 //===--------------------------------------------------------------------===// 1700 1701 /// \brief The number of implicitly-declared default constructors. 1702 static unsigned NumImplicitDefaultConstructors; 1703 1704 /// \brief The number of implicitly-declared default constructors for 1705 /// which declarations were built. 1706 static unsigned NumImplicitDefaultConstructorsDeclared; 1707 1708 /// \brief The number of implicitly-declared copy constructors. 1709 static unsigned NumImplicitCopyConstructors; 1710 1711 /// \brief The number of implicitly-declared copy constructors for 1712 /// which declarations were built. 1713 static unsigned NumImplicitCopyConstructorsDeclared; 1714 1715 /// \brief The number of implicitly-declared move constructors. 1716 static unsigned NumImplicitMoveConstructors; 1717 1718 /// \brief The number of implicitly-declared move constructors for 1719 /// which declarations were built. 1720 static unsigned NumImplicitMoveConstructorsDeclared; 1721 1722 /// \brief The number of implicitly-declared copy assignment operators. 1723 static unsigned NumImplicitCopyAssignmentOperators; 1724 1725 /// \brief The number of implicitly-declared copy assignment operators for 1726 /// which declarations were built. 1727 static unsigned NumImplicitCopyAssignmentOperatorsDeclared; 1728 1729 /// \brief The number of implicitly-declared move assignment operators. 1730 static unsigned NumImplicitMoveAssignmentOperators; 1731 1732 /// \brief The number of implicitly-declared move assignment operators for 1733 /// which declarations were built. 1734 static unsigned NumImplicitMoveAssignmentOperatorsDeclared; 1735 1736 /// \brief The number of implicitly-declared destructors. 1737 static unsigned NumImplicitDestructors; 1738 1739 /// \brief The number of implicitly-declared destructors for which 1740 /// declarations were built. 1741 static unsigned NumImplicitDestructorsDeclared; 1742 1743private: 1744 ASTContext(const ASTContext&); // DO NOT IMPLEMENT 1745 void operator=(const ASTContext&); // DO NOT IMPLEMENT 1746 1747public: 1748 /// \brief Initialize built-in types. 1749 /// 1750 /// This routine may only be invoked once for a given ASTContext object. 1751 /// It is normally invoked by the ASTContext constructor. However, the 1752 /// constructor can be asked to delay initialization, which places the burden 1753 /// of calling this function on the user of that object. 1754 /// 1755 /// \param Target The target 1756 void InitBuiltinTypes(const TargetInfo &Target); 1757 1758private: 1759 void InitBuiltinType(CanQualType &R, BuiltinType::Kind K); 1760 1761 // Return the ObjC type encoding for a given type. 1762 void getObjCEncodingForTypeImpl(QualType t, std::string &S, 1763 bool ExpandPointedToStructures, 1764 bool ExpandStructures, 1765 const FieldDecl *Field, 1766 bool OutermostType = false, 1767 bool EncodingProperty = false, 1768 bool StructField = false, 1769 bool EncodeBlockParameters = false, 1770 bool EncodeClassNames = false) const; 1771 1772 // Adds the encoding of the structure's members. 1773 void getObjCEncodingForStructureImpl(RecordDecl *RD, std::string &S, 1774 const FieldDecl *Field, 1775 bool includeVBases = true) const; 1776 1777 // Adds the encoding of a method parameter or return type. 1778 void getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT, 1779 QualType T, std::string& S, 1780 bool Extended) const; 1781 1782 const ASTRecordLayout & 1783 getObjCLayout(const ObjCInterfaceDecl *D, 1784 const ObjCImplementationDecl *Impl) const; 1785 1786private: 1787 /// \brief A set of deallocations that should be performed when the 1788 /// ASTContext is destroyed. 1789 SmallVector<std::pair<void (*)(void*), void *>, 16> Deallocations; 1790 1791 // FIXME: This currently contains the set of StoredDeclMaps used 1792 // by DeclContext objects. This probably should not be in ASTContext, 1793 // but we include it here so that ASTContext can quickly deallocate them. 1794 llvm::PointerIntPair<StoredDeclsMap*,1> LastSDM; 1795 1796 /// \brief A counter used to uniquely identify "blocks". 1797 mutable unsigned int UniqueBlockByRefTypeID; 1798 1799 friend class DeclContext; 1800 friend class DeclarationNameTable; 1801 void ReleaseDeclContextMaps(); 1802}; 1803 1804/// @brief Utility function for constructing a nullary selector. 1805static inline Selector GetNullarySelector(StringRef name, ASTContext& Ctx) { 1806 IdentifierInfo* II = &Ctx.Idents.get(name); 1807 return Ctx.Selectors.getSelector(0, &II); 1808} 1809 1810/// @brief Utility function for constructing an unary selector. 1811static inline Selector GetUnarySelector(StringRef name, ASTContext& Ctx) { 1812 IdentifierInfo* II = &Ctx.Idents.get(name); 1813 return Ctx.Selectors.getSelector(1, &II); 1814} 1815 1816} // end namespace clang 1817 1818// operator new and delete aren't allowed inside namespaces. 1819// The throw specifications are mandated by the standard. 1820/// @brief Placement new for using the ASTContext's allocator. 1821/// 1822/// This placement form of operator new uses the ASTContext's allocator for 1823/// obtaining memory. It is a non-throwing new, which means that it returns 1824/// null on error. (If that is what the allocator does. The current does, so if 1825/// this ever changes, this operator will have to be changed, too.) 1826/// Usage looks like this (assuming there's an ASTContext 'Context' in scope): 1827/// @code 1828/// // Default alignment (8) 1829/// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments); 1830/// // Specific alignment 1831/// IntegerLiteral *Ex2 = new (Context, 4) IntegerLiteral(arguments); 1832/// @endcode 1833/// Please note that you cannot use delete on the pointer; it must be 1834/// deallocated using an explicit destructor call followed by 1835/// @c Context.Deallocate(Ptr). 1836/// 1837/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 1838/// @param C The ASTContext that provides the allocator. 1839/// @param Alignment The alignment of the allocated memory (if the underlying 1840/// allocator supports it). 1841/// @return The allocated memory. Could be NULL. 1842inline void *operator new(size_t Bytes, const clang::ASTContext &C, 1843 size_t Alignment) throw () { 1844 return C.Allocate(Bytes, Alignment); 1845} 1846/// @brief Placement delete companion to the new above. 1847/// 1848/// This operator is just a companion to the new above. There is no way of 1849/// invoking it directly; see the new operator for more details. This operator 1850/// is called implicitly by the compiler if a placement new expression using 1851/// the ASTContext throws in the object constructor. 1852inline void operator delete(void *Ptr, const clang::ASTContext &C, size_t) 1853 throw () { 1854 C.Deallocate(Ptr); 1855} 1856 1857/// This placement form of operator new[] uses the ASTContext's allocator for 1858/// obtaining memory. It is a non-throwing new[], which means that it returns 1859/// null on error. 1860/// Usage looks like this (assuming there's an ASTContext 'Context' in scope): 1861/// @code 1862/// // Default alignment (8) 1863/// char *data = new (Context) char[10]; 1864/// // Specific alignment 1865/// char *data = new (Context, 4) char[10]; 1866/// @endcode 1867/// Please note that you cannot use delete on the pointer; it must be 1868/// deallocated using an explicit destructor call followed by 1869/// @c Context.Deallocate(Ptr). 1870/// 1871/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 1872/// @param C The ASTContext that provides the allocator. 1873/// @param Alignment The alignment of the allocated memory (if the underlying 1874/// allocator supports it). 1875/// @return The allocated memory. Could be NULL. 1876inline void *operator new[](size_t Bytes, const clang::ASTContext& C, 1877 size_t Alignment = 8) throw () { 1878 return C.Allocate(Bytes, Alignment); 1879} 1880 1881/// @brief Placement delete[] companion to the new[] above. 1882/// 1883/// This operator is just a companion to the new[] above. There is no way of 1884/// invoking it directly; see the new[] operator for more details. This operator 1885/// is called implicitly by the compiler if a placement new[] expression using 1886/// the ASTContext throws in the object constructor. 1887inline void operator delete[](void *Ptr, const clang::ASTContext &C, size_t) 1888 throw () { 1889 C.Deallocate(Ptr); 1890} 1891 1892#endif 1893