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