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