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