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