ASTContext.h revision 6b5a61b6dc400027fd793dcadceeb9da944a37ea
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/IdentifierTable.h" 18#include "clang/Basic/LangOptions.h" 19#include "clang/Basic/OperatorKinds.h" 20#include "clang/Basic/PartialDiagnostic.h" 21#include "clang/AST/Decl.h" 22#include "clang/AST/NestedNameSpecifier.h" 23#include "clang/AST/PrettyPrinter.h" 24#include "clang/AST/TemplateName.h" 25#include "clang/AST/Type.h" 26#include "clang/AST/CanonicalType.h" 27#include "clang/AST/UsuallyTinyPtrVector.h" 28#include "llvm/ADT/DenseMap.h" 29#include "llvm/ADT/FoldingSet.h" 30#include "llvm/ADT/OwningPtr.h" 31#include "llvm/ADT/SmallPtrSet.h" 32#include "llvm/Support/Allocator.h" 33#include <vector> 34 35namespace llvm { 36 struct fltSemantics; 37 class raw_ostream; 38} 39 40namespace clang { 41 class FileManager; 42 class ASTRecordLayout; 43 class BlockExpr; 44 class CharUnits; 45 class Diagnostic; 46 class Expr; 47 class ExternalASTSource; 48 class ASTMutationListener; 49 class IdentifierTable; 50 class SelectorTable; 51 class SourceManager; 52 class TargetInfo; 53 class CXXABI; 54 // Decls 55 class DeclContext; 56 class CXXMethodDecl; 57 class CXXRecordDecl; 58 class Decl; 59 class FieldDecl; 60 class MangleContext; 61 class ObjCIvarDecl; 62 class ObjCIvarRefExpr; 63 class ObjCPropertyDecl; 64 class RecordDecl; 65 class StoredDeclsMap; 66 class TagDecl; 67 class TemplateTemplateParmDecl; 68 class TemplateTypeParmDecl; 69 class TranslationUnitDecl; 70 class TypeDecl; 71 class TypedefDecl; 72 class UsingDecl; 73 class UsingShadowDecl; 74 class UnresolvedSetIterator; 75 76 namespace Builtin { class Context; } 77 78/// ASTContext - This class holds long-lived AST nodes (such as types and 79/// decls) that can be referred to throughout the semantic analysis of a file. 80class ASTContext { 81 ASTContext &this_() { return *this; } 82 83 mutable std::vector<Type*> Types; 84 mutable llvm::FoldingSet<ExtQuals> ExtQualNodes; 85 mutable llvm::FoldingSet<ComplexType> ComplexTypes; 86 mutable llvm::FoldingSet<PointerType> PointerTypes; 87 mutable llvm::FoldingSet<BlockPointerType> BlockPointerTypes; 88 mutable llvm::FoldingSet<LValueReferenceType> LValueReferenceTypes; 89 mutable llvm::FoldingSet<RValueReferenceType> RValueReferenceTypes; 90 mutable llvm::FoldingSet<MemberPointerType> MemberPointerTypes; 91 mutable llvm::FoldingSet<ConstantArrayType> ConstantArrayTypes; 92 mutable llvm::FoldingSet<IncompleteArrayType> IncompleteArrayTypes; 93 mutable std::vector<VariableArrayType*> VariableArrayTypes; 94 mutable llvm::FoldingSet<DependentSizedArrayType> DependentSizedArrayTypes; 95 mutable llvm::FoldingSet<DependentSizedExtVectorType> 96 DependentSizedExtVectorTypes; 97 mutable llvm::FoldingSet<VectorType> VectorTypes; 98 mutable llvm::FoldingSet<FunctionNoProtoType> FunctionNoProtoTypes; 99 mutable llvm::FoldingSet<FunctionProtoType> FunctionProtoTypes; 100 mutable llvm::FoldingSet<DependentTypeOfExprType> DependentTypeOfExprTypes; 101 mutable llvm::FoldingSet<DependentDecltypeType> DependentDecltypeTypes; 102 mutable llvm::FoldingSet<TemplateTypeParmType> TemplateTypeParmTypes; 103 mutable llvm::FoldingSet<SubstTemplateTypeParmType> 104 SubstTemplateTypeParmTypes; 105 mutable llvm::FoldingSet<SubstTemplateTypeParmPackType> 106 SubstTemplateTypeParmPackTypes; 107 mutable llvm::ContextualFoldingSet<TemplateSpecializationType, ASTContext&> 108 TemplateSpecializationTypes; 109 mutable llvm::FoldingSet<ParenType> ParenTypes; 110 mutable llvm::FoldingSet<ElaboratedType> ElaboratedTypes; 111 mutable llvm::FoldingSet<DependentNameType> DependentNameTypes; 112 mutable llvm::ContextualFoldingSet<DependentTemplateSpecializationType, 113 ASTContext&> 114 DependentTemplateSpecializationTypes; 115 llvm::FoldingSet<PackExpansionType> PackExpansionTypes; 116 mutable llvm::FoldingSet<ObjCObjectTypeImpl> ObjCObjectTypes; 117 mutable llvm::FoldingSet<ObjCObjectPointerType> ObjCObjectPointerTypes; 118 llvm::FoldingSet<AttributedType> AttributedTypes; 119 120 mutable llvm::FoldingSet<QualifiedTemplateName> QualifiedTemplateNames; 121 mutable llvm::FoldingSet<DependentTemplateName> DependentTemplateNames; 122 mutable llvm::FoldingSet<SubstTemplateTemplateParmPackStorage> 123 SubstTemplateTemplateParmPacks; 124 125 /// \brief The set of nested name specifiers. 126 /// 127 /// This set is managed by the NestedNameSpecifier class. 128 mutable llvm::FoldingSet<NestedNameSpecifier> NestedNameSpecifiers; 129 mutable NestedNameSpecifier *GlobalNestedNameSpecifier; 130 friend class NestedNameSpecifier; 131 132 /// ASTRecordLayouts - A cache mapping from RecordDecls to ASTRecordLayouts. 133 /// This is lazily created. This is intentionally not serialized. 134 mutable llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*> 135 ASTRecordLayouts; 136 mutable llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*> 137 ObjCLayouts; 138 139 /// KeyFunctions - A cache mapping from CXXRecordDecls to key functions. 140 llvm::DenseMap<const CXXRecordDecl*, const CXXMethodDecl*> KeyFunctions; 141 142 /// \brief Mapping from ObjCContainers to their ObjCImplementations. 143 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*> ObjCImpls; 144 145 /// \brief Mapping from __block VarDecls to their copy initialization expr. 146 llvm::DenseMap<const VarDecl*, Expr*> BlockVarCopyInits; 147 148 /// \brief Representation of a "canonical" template template parameter that 149 /// is used in canonical template names. 150 class CanonicalTemplateTemplateParm : public llvm::FoldingSetNode { 151 TemplateTemplateParmDecl *Parm; 152 153 public: 154 CanonicalTemplateTemplateParm(TemplateTemplateParmDecl *Parm) 155 : Parm(Parm) { } 156 157 TemplateTemplateParmDecl *getParam() const { return Parm; } 158 159 void Profile(llvm::FoldingSetNodeID &ID) { Profile(ID, Parm); } 160 161 static void Profile(llvm::FoldingSetNodeID &ID, 162 TemplateTemplateParmDecl *Parm); 163 }; 164 mutable llvm::FoldingSet<CanonicalTemplateTemplateParm> 165 CanonTemplateTemplateParms; 166 167 TemplateTemplateParmDecl * 168 getCanonicalTemplateTemplateParmDecl(TemplateTemplateParmDecl *TTP) const; 169 170 /// \brief Whether __[u]int128_t identifier is installed. 171 bool IsInt128Installed; 172 173 /// BuiltinVaListType - built-in va list type. 174 /// This is initially null and set by Sema::LazilyCreateBuiltin when 175 /// a builtin that takes a valist is encountered. 176 QualType BuiltinVaListType; 177 178 /// ObjCIdType - a pseudo built-in typedef type (set by Sema). 179 QualType ObjCIdTypedefType; 180 181 /// ObjCSelType - another pseudo built-in typedef type (set by Sema). 182 QualType ObjCSelTypedefType; 183 184 /// ObjCProtoType - another pseudo built-in typedef type (set by Sema). 185 QualType ObjCProtoType; 186 const RecordType *ProtoStructType; 187 188 /// ObjCClassType - another pseudo built-in typedef type (set by Sema). 189 QualType ObjCClassTypedefType; 190 191 QualType ObjCConstantStringType; 192 mutable RecordDecl *CFConstantStringTypeDecl; 193 194 mutable RecordDecl *NSConstantStringTypeDecl; 195 196 mutable RecordDecl *ObjCFastEnumerationStateTypeDecl; 197 198 /// \brief The type for the C FILE type. 199 TypeDecl *FILEDecl; 200 201 /// \brief The type for the C jmp_buf type. 202 TypeDecl *jmp_bufDecl; 203 204 /// \brief The type for the C sigjmp_buf type. 205 TypeDecl *sigjmp_bufDecl; 206 207 /// \brief Type for the Block descriptor for Blocks CodeGen. 208 mutable RecordDecl *BlockDescriptorType; 209 210 /// \brief Type for the Block descriptor for Blocks CodeGen. 211 mutable RecordDecl *BlockDescriptorExtendedType; 212 213 TypeSourceInfo NullTypeSourceInfo; 214 215 /// \brief Keeps track of all declaration attributes. 216 /// 217 /// Since so few decls have attrs, we keep them in a hash map instead of 218 /// wasting space in the Decl class. 219 llvm::DenseMap<const Decl*, AttrVec*> DeclAttrs; 220 221 /// \brief Keeps track of the static data member templates from which 222 /// static data members of class template specializations were instantiated. 223 /// 224 /// This data structure stores the mapping from instantiations of static 225 /// data members to the static data member representations within the 226 /// class template from which they were instantiated along with the kind 227 /// of instantiation or specialization (a TemplateSpecializationKind - 1). 228 /// 229 /// Given the following example: 230 /// 231 /// \code 232 /// template<typename T> 233 /// struct X { 234 /// static T value; 235 /// }; 236 /// 237 /// template<typename T> 238 /// T X<T>::value = T(17); 239 /// 240 /// int *x = &X<int>::value; 241 /// \endcode 242 /// 243 /// This mapping will contain an entry that maps from the VarDecl for 244 /// X<int>::value to the corresponding VarDecl for X<T>::value (within the 245 /// class template X) and will be marked TSK_ImplicitInstantiation. 246 llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *> 247 InstantiatedFromStaticDataMember; 248 249 /// \brief Keeps track of the declaration from which a UsingDecl was 250 /// created during instantiation. The source declaration is always 251 /// a UsingDecl, an UnresolvedUsingValueDecl, or an 252 /// UnresolvedUsingTypenameDecl. 253 /// 254 /// For example: 255 /// \code 256 /// template<typename T> 257 /// struct A { 258 /// void f(); 259 /// }; 260 /// 261 /// template<typename T> 262 /// struct B : A<T> { 263 /// using A<T>::f; 264 /// }; 265 /// 266 /// template struct B<int>; 267 /// \endcode 268 /// 269 /// This mapping will contain an entry that maps from the UsingDecl in 270 /// B<int> to the UnresolvedUsingDecl in B<T>. 271 llvm::DenseMap<UsingDecl *, NamedDecl *> InstantiatedFromUsingDecl; 272 273 llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*> 274 InstantiatedFromUsingShadowDecl; 275 276 llvm::DenseMap<FieldDecl *, FieldDecl *> InstantiatedFromUnnamedFieldDecl; 277 278 /// \brief Mapping that stores the methods overridden by a given C++ 279 /// member function. 280 /// 281 /// Since most C++ member functions aren't virtual and therefore 282 /// don't override anything, we store the overridden functions in 283 /// this map on the side rather than within the CXXMethodDecl structure. 284 typedef UsuallyTinyPtrVector<const CXXMethodDecl> CXXMethodVector; 285 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector> OverriddenMethods; 286 287 TranslationUnitDecl *TUDecl; 288 289 /// SourceMgr - The associated SourceManager object. 290 SourceManager &SourceMgr; 291 292 /// LangOpts - The language options used to create the AST associated with 293 /// this ASTContext object. 294 LangOptions LangOpts; 295 296 /// \brief The allocator used to create AST objects. 297 /// 298 /// AST objects are never destructed; rather, all memory associated with the 299 /// AST objects will be released when the ASTContext itself is destroyed. 300 mutable llvm::BumpPtrAllocator BumpAlloc; 301 302 /// \brief Allocator for partial diagnostics. 303 PartialDiagnostic::StorageAllocator DiagAllocator; 304 305 /// \brief The current C++ ABI. 306 llvm::OwningPtr<CXXABI> ABI; 307 CXXABI *createCXXABI(const TargetInfo &T); 308 309 friend class ASTDeclReader; 310 311public: 312 const TargetInfo &Target; 313 IdentifierTable &Idents; 314 SelectorTable &Selectors; 315 Builtin::Context &BuiltinInfo; 316 mutable DeclarationNameTable DeclarationNames; 317 llvm::OwningPtr<ExternalASTSource> ExternalSource; 318 ASTMutationListener *Listener; 319 clang::PrintingPolicy PrintingPolicy; 320 321 // Typedefs which may be provided defining the structure of Objective-C 322 // pseudo-builtins 323 QualType ObjCIdRedefinitionType; 324 QualType ObjCClassRedefinitionType; 325 QualType ObjCSelRedefinitionType; 326 327 SourceManager& getSourceManager() { return SourceMgr; } 328 const SourceManager& getSourceManager() const { return SourceMgr; } 329 void *Allocate(unsigned Size, unsigned Align = 8) const { 330 return BumpAlloc.Allocate(Size, Align); 331 } 332 void Deallocate(void *Ptr) const { } 333 334 PartialDiagnostic::StorageAllocator &getDiagAllocator() { 335 return DiagAllocator; 336 } 337 338 const LangOptions& getLangOptions() const { return LangOpts; } 339 340 Diagnostic &getDiagnostics() const; 341 342 FullSourceLoc getFullLoc(SourceLocation Loc) const { 343 return FullSourceLoc(Loc,SourceMgr); 344 } 345 346 /// \brief Retrieve the attributes for the given declaration. 347 AttrVec& getDeclAttrs(const Decl *D); 348 349 /// \brief Erase the attributes corresponding to the given declaration. 350 void eraseDeclAttrs(const Decl *D); 351 352 /// \brief If this variable is an instantiated static data member of a 353 /// class template specialization, returns the templated static data member 354 /// from which it was instantiated. 355 MemberSpecializationInfo *getInstantiatedFromStaticDataMember( 356 const VarDecl *Var); 357 358 /// \brief Note that the static data member \p Inst is an instantiation of 359 /// the static data member template \p Tmpl of a class template. 360 void setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, 361 TemplateSpecializationKind TSK, 362 SourceLocation PointOfInstantiation = SourceLocation()); 363 364 /// \brief If the given using decl is an instantiation of a 365 /// (possibly unresolved) using decl from a template instantiation, 366 /// return it. 367 NamedDecl *getInstantiatedFromUsingDecl(UsingDecl *Inst); 368 369 /// \brief Remember that the using decl \p Inst is an instantiation 370 /// of the using decl \p Pattern of a class template. 371 void setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern); 372 373 void setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, 374 UsingShadowDecl *Pattern); 375 UsingShadowDecl *getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst); 376 377 FieldDecl *getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field); 378 379 void setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, FieldDecl *Tmpl); 380 381 // Access to the set of methods overridden by the given C++ method. 382 typedef CXXMethodVector::iterator overridden_cxx_method_iterator; 383 overridden_cxx_method_iterator 384 overridden_methods_begin(const CXXMethodDecl *Method) const; 385 386 overridden_cxx_method_iterator 387 overridden_methods_end(const CXXMethodDecl *Method) const; 388 389 unsigned overridden_methods_size(const CXXMethodDecl *Method) const; 390 391 /// \brief Note that the given C++ \p Method overrides the given \p 392 /// Overridden method. 393 void addOverriddenMethod(const CXXMethodDecl *Method, 394 const CXXMethodDecl *Overridden); 395 396 TranslationUnitDecl *getTranslationUnitDecl() const { return TUDecl; } 397 398 399 // Builtin Types. 400 CanQualType VoidTy; 401 CanQualType BoolTy; 402 CanQualType CharTy; 403 CanQualType WCharTy; // [C++ 3.9.1p5], integer type in C99. 404 CanQualType Char16Ty; // [C++0x 3.9.1p5], integer type in C99. 405 CanQualType Char32Ty; // [C++0x 3.9.1p5], integer type in C99. 406 CanQualType SignedCharTy, ShortTy, IntTy, LongTy, LongLongTy, Int128Ty; 407 CanQualType UnsignedCharTy, UnsignedShortTy, UnsignedIntTy, UnsignedLongTy; 408 CanQualType UnsignedLongLongTy, UnsignedInt128Ty; 409 CanQualType FloatTy, DoubleTy, LongDoubleTy; 410 CanQualType FloatComplexTy, DoubleComplexTy, LongDoubleComplexTy; 411 CanQualType VoidPtrTy, NullPtrTy; 412 CanQualType OverloadTy, UndeducedAutoTy; 413 CanQualType DependentTy; 414 CanQualType ObjCBuiltinIdTy, ObjCBuiltinClassTy, ObjCBuiltinSelTy; 415 416 ASTContext(const LangOptions& LOpts, SourceManager &SM, const TargetInfo &t, 417 IdentifierTable &idents, SelectorTable &sels, 418 Builtin::Context &builtins, 419 unsigned size_reserve); 420 421 ~ASTContext(); 422 423 /// \brief Attach an external AST source to the AST context. 424 /// 425 /// The external AST source provides the ability to load parts of 426 /// the abstract syntax tree as needed from some external storage, 427 /// e.g., a precompiled header. 428 void setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source); 429 430 /// \brief Retrieve a pointer to the external AST source associated 431 /// with this AST context, if any. 432 ExternalASTSource *getExternalSource() const { return ExternalSource.get(); } 433 434 /// \brief Attach an AST mutation listener to the AST context. 435 /// 436 /// The AST mutation listener provides the ability to track modifications to 437 /// the abstract syntax tree entities committed after they were initially 438 /// created. 439 void setASTMutationListener(ASTMutationListener *Listener) { 440 this->Listener = Listener; 441 } 442 443 /// \brief Retrieve a pointer to the AST mutation listener associated 444 /// with this AST context, if any. 445 ASTMutationListener *getASTMutationListener() const { return Listener; } 446 447 void PrintStats() const; 448 const std::vector<Type*>& getTypes() const { return Types; } 449 450 //===--------------------------------------------------------------------===// 451 // Type Constructors 452 //===--------------------------------------------------------------------===// 453 454private: 455 /// getExtQualType - Return a type with extended qualifiers. 456 QualType getExtQualType(const Type *Base, Qualifiers Quals) const; 457 458 QualType getTypeDeclTypeSlow(const TypeDecl *Decl) const; 459 460public: 461 /// getAddSpaceQualType - Return the uniqued reference to the type for an 462 /// address space qualified type with the specified type and address space. 463 /// The resulting type has a union of the qualifiers from T and the address 464 /// space. If T already has an address space specifier, it is silently 465 /// replaced. 466 QualType getAddrSpaceQualType(QualType T, unsigned AddressSpace) const; 467 468 /// getObjCGCQualType - Returns the uniqued reference to the type for an 469 /// objc gc qualified type. The retulting type has a union of the qualifiers 470 /// from T and the gc attribute. 471 QualType getObjCGCQualType(QualType T, Qualifiers::GC gcAttr) const; 472 473 /// getRestrictType - Returns the uniqued reference to the type for a 474 /// 'restrict' qualified type. The resulting type has a union of the 475 /// qualifiers from T and 'restrict'. 476 QualType getRestrictType(QualType T) const { 477 return T.withFastQualifiers(Qualifiers::Restrict); 478 } 479 480 /// getVolatileType - Returns the uniqued reference to the type for a 481 /// 'volatile' qualified type. The resulting type has a union of the 482 /// qualifiers from T and 'volatile'. 483 QualType getVolatileType(QualType T) const { 484 return T.withFastQualifiers(Qualifiers::Volatile); 485 } 486 487 /// getConstType - Returns the uniqued reference to the type for a 488 /// 'const' qualified type. The resulting type has a union of the 489 /// qualifiers from T and 'const'. 490 /// 491 /// It can be reasonably expected that this will always be 492 /// equivalent to calling T.withConst(). 493 QualType getConstType(QualType T) const { return T.withConst(); } 494 495 /// adjustFunctionType - Change the ExtInfo on a function type. 496 const FunctionType *adjustFunctionType(const FunctionType *Fn, 497 FunctionType::ExtInfo EInfo); 498 499 /// getComplexType - Return the uniqued reference to the type for a complex 500 /// number with the specified element type. 501 QualType getComplexType(QualType T) const; 502 CanQualType getComplexType(CanQualType T) const { 503 return CanQualType::CreateUnsafe(getComplexType((QualType) T)); 504 } 505 506 /// getPointerType - Return the uniqued reference to the type for a pointer to 507 /// the specified type. 508 QualType getPointerType(QualType T) const; 509 CanQualType getPointerType(CanQualType T) const { 510 return CanQualType::CreateUnsafe(getPointerType((QualType) T)); 511 } 512 513 /// getBlockPointerType - Return the uniqued reference to the type for a block 514 /// of the specified type. 515 QualType getBlockPointerType(QualType T) const; 516 517 /// This gets the struct used to keep track of the descriptor for pointer to 518 /// blocks. 519 QualType getBlockDescriptorType() const; 520 521 // Set the type for a Block descriptor type. 522 void setBlockDescriptorType(QualType T); 523 /// Get the BlockDescriptorType type, or NULL if it hasn't yet been built. 524 QualType getRawBlockdescriptorType() { 525 if (BlockDescriptorType) 526 return getTagDeclType(BlockDescriptorType); 527 return QualType(); 528 } 529 530 /// This gets the struct used to keep track of the extended descriptor for 531 /// pointer to blocks. 532 QualType getBlockDescriptorExtendedType() const; 533 534 // Set the type for a Block descriptor extended type. 535 void setBlockDescriptorExtendedType(QualType T); 536 /// Get the BlockDescriptorExtendedType type, or NULL if it hasn't yet been 537 /// built. 538 QualType getRawBlockdescriptorExtendedType() const { 539 if (BlockDescriptorExtendedType) 540 return getTagDeclType(BlockDescriptorExtendedType); 541 return QualType(); 542 } 543 544 /// This gets the struct used to keep track of pointer to blocks, complete 545 /// with captured variables. 546 QualType getBlockParmType(bool BlockHasCopyDispose, 547 llvm::SmallVectorImpl<const Expr *> &Layout) const; 548 549 /// This builds the struct used for __block variables. 550 QualType BuildByRefType(llvm::StringRef DeclName, QualType Ty) const; 551 552 /// Returns true iff we need copy/dispose helpers for the given type. 553 bool BlockRequiresCopying(QualType Ty) const; 554 555 /// getLValueReferenceType - Return the uniqued reference to the type for an 556 /// lvalue reference to the specified type. 557 QualType getLValueReferenceType(QualType T, bool SpelledAsLValue = true) 558 const; 559 560 /// getRValueReferenceType - Return the uniqued reference to the type for an 561 /// rvalue reference to the specified type. 562 QualType getRValueReferenceType(QualType T) const; 563 564 /// getMemberPointerType - Return the uniqued reference to the type for a 565 /// member pointer to the specified type in the specified class. The class 566 /// is a Type because it could be a dependent name. 567 QualType getMemberPointerType(QualType T, const Type *Cls) const; 568 569 /// getVariableArrayType - Returns a non-unique reference to the type for a 570 /// variable array of the specified element type. 571 QualType getVariableArrayType(QualType EltTy, Expr *NumElts, 572 ArrayType::ArraySizeModifier ASM, 573 unsigned IndexTypeQuals, 574 SourceRange Brackets) const; 575 576 /// getDependentSizedArrayType - Returns a non-unique reference to 577 /// the type for a dependently-sized array of the specified element 578 /// type. FIXME: We will need these to be uniqued, or at least 579 /// comparable, at some point. 580 QualType getDependentSizedArrayType(QualType EltTy, Expr *NumElts, 581 ArrayType::ArraySizeModifier ASM, 582 unsigned IndexTypeQuals, 583 SourceRange Brackets) const; 584 585 /// getIncompleteArrayType - Returns a unique reference to the type for a 586 /// incomplete array of the specified element type. 587 QualType getIncompleteArrayType(QualType EltTy, 588 ArrayType::ArraySizeModifier ASM, 589 unsigned IndexTypeQuals) const; 590 591 /// getConstantArrayType - Return the unique reference to the type for a 592 /// constant array of the specified element type. 593 QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize, 594 ArrayType::ArraySizeModifier ASM, 595 unsigned IndexTypeQuals) const; 596 597 /// getVariableArrayDecayedType - Returns a vla type where known sizes 598 /// are replaced with [*]. 599 QualType getVariableArrayDecayedType(QualType Ty) const; 600 601 /// getVectorType - Return the unique reference to a vector type of 602 /// the specified element type and size. VectorType must be a built-in type. 603 QualType getVectorType(QualType VectorType, unsigned NumElts, 604 VectorType::VectorKind VecKind) const; 605 606 /// getExtVectorType - Return the unique reference to an extended vector type 607 /// of the specified element type and size. VectorType must be a built-in 608 /// type. 609 QualType getExtVectorType(QualType VectorType, unsigned NumElts) const; 610 611 /// getDependentSizedExtVectorType - Returns a non-unique reference to 612 /// the type for a dependently-sized vector of the specified element 613 /// type. FIXME: We will need these to be uniqued, or at least 614 /// comparable, at some point. 615 QualType getDependentSizedExtVectorType(QualType VectorType, 616 Expr *SizeExpr, 617 SourceLocation AttrLoc) const; 618 619 /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. 620 /// 621 QualType getFunctionNoProtoType(QualType ResultTy, 622 const FunctionType::ExtInfo &Info) const; 623 624 QualType getFunctionNoProtoType(QualType ResultTy) const { 625 return getFunctionNoProtoType(ResultTy, FunctionType::ExtInfo()); 626 } 627 628 /// getFunctionType - Return a normal function type with a typed 629 /// argument list. 630 QualType getFunctionType(QualType ResultTy, 631 const QualType *Args, unsigned NumArgs, 632 const FunctionProtoType::ExtProtoInfo &EPI) const; 633 634 /// getTypeDeclType - Return the unique reference to the type for 635 /// the specified type declaration. 636 QualType getTypeDeclType(const TypeDecl *Decl, 637 const TypeDecl *PrevDecl = 0) const { 638 assert(Decl && "Passed null for Decl param"); 639 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); 640 641 if (PrevDecl) { 642 assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl"); 643 Decl->TypeForDecl = PrevDecl->TypeForDecl; 644 return QualType(PrevDecl->TypeForDecl, 0); 645 } 646 647 return getTypeDeclTypeSlow(Decl); 648 } 649 650 /// getTypedefType - Return the unique reference to the type for the 651 /// specified typename decl. 652 QualType getTypedefType(const TypedefDecl *Decl, QualType Canon = QualType()) 653 const; 654 655 QualType getRecordType(const RecordDecl *Decl) const; 656 657 QualType getEnumType(const EnumDecl *Decl) const; 658 659 QualType getInjectedClassNameType(CXXRecordDecl *Decl, QualType TST) const; 660 661 QualType getAttributedType(AttributedType::Kind attrKind, 662 QualType modifiedType, 663 QualType equivalentType); 664 665 QualType getSubstTemplateTypeParmType(const TemplateTypeParmType *Replaced, 666 QualType Replacement) const; 667 QualType getSubstTemplateTypeParmPackType( 668 const TemplateTypeParmType *Replaced, 669 const TemplateArgument &ArgPack); 670 671 QualType getTemplateTypeParmType(unsigned Depth, unsigned Index, 672 bool ParameterPack, 673 IdentifierInfo *Name = 0) const; 674 675 QualType getTemplateSpecializationType(TemplateName T, 676 const TemplateArgument *Args, 677 unsigned NumArgs, 678 QualType Canon = QualType()) const; 679 680 QualType getCanonicalTemplateSpecializationType(TemplateName T, 681 const TemplateArgument *Args, 682 unsigned NumArgs) const; 683 684 QualType getTemplateSpecializationType(TemplateName T, 685 const TemplateArgumentListInfo &Args, 686 QualType Canon = QualType()) const; 687 688 TypeSourceInfo * 689 getTemplateSpecializationTypeInfo(TemplateName T, SourceLocation TLoc, 690 const TemplateArgumentListInfo &Args, 691 QualType Canon = QualType()) const; 692 693 QualType getParenType(QualType NamedType) const; 694 695 QualType getElaboratedType(ElaboratedTypeKeyword Keyword, 696 NestedNameSpecifier *NNS, 697 QualType NamedType) const; 698 QualType getDependentNameType(ElaboratedTypeKeyword Keyword, 699 NestedNameSpecifier *NNS, 700 const IdentifierInfo *Name, 701 QualType Canon = QualType()) const; 702 703 QualType getDependentTemplateSpecializationType(ElaboratedTypeKeyword Keyword, 704 NestedNameSpecifier *NNS, 705 const IdentifierInfo *Name, 706 const TemplateArgumentListInfo &Args) const; 707 QualType getDependentTemplateSpecializationType(ElaboratedTypeKeyword Keyword, 708 NestedNameSpecifier *NNS, 709 const IdentifierInfo *Name, 710 unsigned NumArgs, 711 const TemplateArgument *Args) const; 712 713 QualType getPackExpansionType(QualType Pattern, 714 llvm::Optional<unsigned> NumExpansions); 715 716 QualType getObjCInterfaceType(const ObjCInterfaceDecl *Decl) const; 717 718 QualType getObjCObjectType(QualType Base, 719 ObjCProtocolDecl * const *Protocols, 720 unsigned NumProtocols) const; 721 722 /// getObjCObjectPointerType - Return a ObjCObjectPointerType type 723 /// for the given ObjCObjectType. 724 QualType getObjCObjectPointerType(QualType OIT) const; 725 726 /// getTypeOfType - GCC extension. 727 QualType getTypeOfExprType(Expr *e) const; 728 QualType getTypeOfType(QualType t) const; 729 730 /// getDecltypeType - C++0x decltype. 731 QualType getDecltypeType(Expr *e) const; 732 733 /// getTagDeclType - Return the unique reference to the type for the 734 /// specified TagDecl (struct/union/class/enum) decl. 735 QualType getTagDeclType(const TagDecl *Decl) const; 736 737 /// getSizeType - Return the unique type for "size_t" (C99 7.17), defined 738 /// in <stddef.h>. The sizeof operator requires this (C99 6.5.3.4p4). 739 CanQualType getSizeType() const; 740 741 /// getWCharType - In C++, this returns the unique wchar_t type. In C99, this 742 /// returns a type compatible with the type defined in <stddef.h> as defined 743 /// by the target. 744 QualType getWCharType() const { return WCharTy; } 745 746 /// getSignedWCharType - Return the type of "signed wchar_t". 747 /// Used when in C++, as a GCC extension. 748 QualType getSignedWCharType() const; 749 750 /// getUnsignedWCharType - Return the type of "unsigned wchar_t". 751 /// Used when in C++, as a GCC extension. 752 QualType getUnsignedWCharType() const; 753 754 /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) 755 /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). 756 QualType getPointerDiffType() const; 757 758 // getCFConstantStringType - Return the C structure type used to represent 759 // constant CFStrings. 760 QualType getCFConstantStringType() const; 761 762 // getNSConstantStringType - Return the C structure type used to represent 763 // constant NSStrings. 764 QualType getNSConstantStringType() const; 765 /// Get the structure type used to representation NSStrings, or NULL 766 /// if it hasn't yet been built. 767 QualType getRawNSConstantStringType() const { 768 if (NSConstantStringTypeDecl) 769 return getTagDeclType(NSConstantStringTypeDecl); 770 return QualType(); 771 } 772 void setNSConstantStringType(QualType T); 773 774 775 /// Get the structure type used to representation CFStrings, or NULL 776 /// if it hasn't yet been built. 777 QualType getRawCFConstantStringType() const { 778 if (CFConstantStringTypeDecl) 779 return getTagDeclType(CFConstantStringTypeDecl); 780 return QualType(); 781 } 782 void setCFConstantStringType(QualType T); 783 784 // This setter/getter represents the ObjC type for an NSConstantString. 785 void setObjCConstantStringInterface(ObjCInterfaceDecl *Decl); 786 QualType getObjCConstantStringInterface() const { 787 return ObjCConstantStringType; 788 } 789 790 //// This gets the struct used to keep track of fast enumerations. 791 QualType getObjCFastEnumerationStateType() const; 792 793 /// Get the ObjCFastEnumerationState type, or NULL if it hasn't yet 794 /// been built. 795 QualType getRawObjCFastEnumerationStateType() const { 796 if (ObjCFastEnumerationStateTypeDecl) 797 return getTagDeclType(ObjCFastEnumerationStateTypeDecl); 798 return QualType(); 799 } 800 801 void setObjCFastEnumerationStateType(QualType T); 802 803 /// \brief Set the type for the C FILE type. 804 void setFILEDecl(TypeDecl *FILEDecl) { this->FILEDecl = FILEDecl; } 805 806 /// \brief Retrieve the C FILE type. 807 QualType getFILEType() const { 808 if (FILEDecl) 809 return getTypeDeclType(FILEDecl); 810 return QualType(); 811 } 812 813 /// \brief Set the type for the C jmp_buf type. 814 void setjmp_bufDecl(TypeDecl *jmp_bufDecl) { 815 this->jmp_bufDecl = jmp_bufDecl; 816 } 817 818 /// \brief Retrieve the C jmp_buf type. 819 QualType getjmp_bufType() const { 820 if (jmp_bufDecl) 821 return getTypeDeclType(jmp_bufDecl); 822 return QualType(); 823 } 824 825 /// \brief Set the type for the C sigjmp_buf type. 826 void setsigjmp_bufDecl(TypeDecl *sigjmp_bufDecl) { 827 this->sigjmp_bufDecl = sigjmp_bufDecl; 828 } 829 830 /// \brief Retrieve the C sigjmp_buf type. 831 QualType getsigjmp_bufType() const { 832 if (sigjmp_bufDecl) 833 return getTypeDeclType(sigjmp_bufDecl); 834 return QualType(); 835 } 836 837 /// getObjCEncodingForType - Emit the ObjC type encoding for the 838 /// given type into \arg S. If \arg NameFields is specified then 839 /// record field names are also encoded. 840 void getObjCEncodingForType(QualType t, std::string &S, 841 const FieldDecl *Field=0) const; 842 843 void getLegacyIntegralTypeEncoding(QualType &t) const; 844 845 // Put the string version of type qualifiers into S. 846 void getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, 847 std::string &S) const; 848 849 /// getObjCEncodingForFunctionDecl - Returns the encoded type for this 850 //function. This is in the same format as Objective-C method encodings. 851 void getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, std::string& S); 852 853 /// getObjCEncodingForMethodDecl - Return the encoded type for this method 854 /// declaration. 855 void getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, std::string &S) 856 const; 857 858 /// getObjCEncodingForBlock - Return the encoded type for this block 859 /// declaration. 860 std::string getObjCEncodingForBlock(const BlockExpr *blockExpr) const; 861 862 /// getObjCEncodingForPropertyDecl - Return the encoded type for 863 /// this method declaration. If non-NULL, Container must be either 864 /// an ObjCCategoryImplDecl or ObjCImplementationDecl; it should 865 /// only be NULL when getting encodings for protocol properties. 866 void getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, 867 const Decl *Container, 868 std::string &S) const; 869 870 bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, 871 ObjCProtocolDecl *rProto) const; 872 873 /// getObjCEncodingTypeSize returns size of type for objective-c encoding 874 /// purpose in characters. 875 CharUnits getObjCEncodingTypeSize(QualType t) const; 876 877 /// \brief Whether __[u]int128_t identifier is installed. 878 bool isInt128Installed() const { return IsInt128Installed; } 879 void setInt128Installed() { IsInt128Installed = true; } 880 881 /// This setter/getter represents the ObjC 'id' type. It is setup lazily, by 882 /// Sema. id is always a (typedef for a) pointer type, a pointer to a struct. 883 QualType getObjCIdType() const { return ObjCIdTypedefType; } 884 void setObjCIdType(QualType T); 885 886 void setObjCSelType(QualType T); 887 QualType getObjCSelType() const { return ObjCSelTypedefType; } 888 889 void setObjCProtoType(QualType QT); 890 QualType getObjCProtoType() const { return ObjCProtoType; } 891 892 /// This setter/getter repreents the ObjC 'Class' type. It is setup lazily, by 893 /// Sema. 'Class' is always a (typedef for a) pointer type, a pointer to a 894 /// struct. 895 QualType getObjCClassType() const { return ObjCClassTypedefType; } 896 void setObjCClassType(QualType T); 897 898 void setBuiltinVaListType(QualType T); 899 QualType getBuiltinVaListType() const { return BuiltinVaListType; } 900 901 /// getCVRQualifiedType - Returns a type with additional const, 902 /// volatile, or restrict qualifiers. 903 QualType getCVRQualifiedType(QualType T, unsigned CVR) const { 904 return getQualifiedType(T, Qualifiers::fromCVRMask(CVR)); 905 } 906 907 /// getQualifiedType - Returns a type with additional qualifiers. 908 QualType getQualifiedType(QualType T, Qualifiers Qs) const { 909 if (!Qs.hasNonFastQualifiers()) 910 return T.withFastQualifiers(Qs.getFastQualifiers()); 911 QualifierCollector Qc(Qs); 912 const Type *Ptr = Qc.strip(T); 913 return getExtQualType(Ptr, Qc); 914 } 915 916 /// getQualifiedType - Returns a type with additional qualifiers. 917 QualType getQualifiedType(const Type *T, Qualifiers Qs) const { 918 if (!Qs.hasNonFastQualifiers()) 919 return QualType(T, Qs.getFastQualifiers()); 920 return getExtQualType(T, Qs); 921 } 922 923 DeclarationNameInfo getNameForTemplate(TemplateName Name, 924 SourceLocation NameLoc) const; 925 926 TemplateName getOverloadedTemplateName(UnresolvedSetIterator Begin, 927 UnresolvedSetIterator End) const; 928 929 TemplateName getQualifiedTemplateName(NestedNameSpecifier *NNS, 930 bool TemplateKeyword, 931 TemplateDecl *Template) const; 932 933 TemplateName getDependentTemplateName(NestedNameSpecifier *NNS, 934 const IdentifierInfo *Name) const; 935 TemplateName getDependentTemplateName(NestedNameSpecifier *NNS, 936 OverloadedOperatorKind Operator) const; 937 TemplateName getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param, 938 const TemplateArgument &ArgPack) const; 939 940 enum GetBuiltinTypeError { 941 GE_None, //< No error 942 GE_Missing_stdio, //< Missing a type from <stdio.h> 943 GE_Missing_setjmp //< Missing a type from <setjmp.h> 944 }; 945 946 /// GetBuiltinType - Return the type for the specified builtin. If 947 /// IntegerConstantArgs is non-null, it is filled in with a bitmask of 948 /// arguments to the builtin that are required to be integer constant 949 /// expressions. 950 QualType GetBuiltinType(unsigned ID, GetBuiltinTypeError &Error, 951 unsigned *IntegerConstantArgs = 0) const; 952 953private: 954 CanQualType getFromTargetType(unsigned Type) const; 955 956 //===--------------------------------------------------------------------===// 957 // Type Predicates. 958 //===--------------------------------------------------------------------===// 959 960public: 961 /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's 962 /// garbage collection attribute. 963 /// 964 Qualifiers::GC getObjCGCAttrKind(QualType Ty) const; 965 966 /// areCompatibleVectorTypes - Return true if the given vector types 967 /// are of the same unqualified type or if they are equivalent to the same 968 /// GCC vector type, ignoring whether they are target-specific (AltiVec or 969 /// Neon) types. 970 bool areCompatibleVectorTypes(QualType FirstVec, QualType SecondVec); 971 972 /// isObjCNSObjectType - Return true if this is an NSObject object with 973 /// its NSObject attribute set. 974 bool isObjCNSObjectType(QualType Ty) const; 975 976 //===--------------------------------------------------------------------===// 977 // Type Sizing and Analysis 978 //===--------------------------------------------------------------------===// 979 980 /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified 981 /// scalar floating point type. 982 const llvm::fltSemantics &getFloatTypeSemantics(QualType T) const; 983 984 /// getTypeInfo - Get the size and alignment of the specified complete type in 985 /// bits. 986 std::pair<uint64_t, unsigned> getTypeInfo(const Type *T) const; 987 std::pair<uint64_t, unsigned> getTypeInfo(QualType T) const { 988 return getTypeInfo(T.getTypePtr()); 989 } 990 991 /// getTypeSize - Return the size of the specified type, in bits. This method 992 /// does not work on incomplete types. 993 uint64_t getTypeSize(QualType T) const { 994 return getTypeInfo(T).first; 995 } 996 uint64_t getTypeSize(const Type *T) const { 997 return getTypeInfo(T).first; 998 } 999 1000 /// getCharWidth - Return the size of the character type, in bits 1001 uint64_t getCharWidth() const { 1002 return getTypeSize(CharTy); 1003 } 1004 1005 /// toCharUnitsFromBits - Convert a size in bits to a size in characters. 1006 CharUnits toCharUnitsFromBits(int64_t BitSize) const; 1007 1008 /// getTypeSizeInChars - Return the size of the specified type, in characters. 1009 /// This method does not work on incomplete types. 1010 CharUnits getTypeSizeInChars(QualType T) const; 1011 CharUnits getTypeSizeInChars(const Type *T) const; 1012 1013 /// getTypeAlign - Return the ABI-specified alignment of a type, in bits. 1014 /// This method does not work on incomplete types. 1015 unsigned getTypeAlign(QualType T) const { 1016 return getTypeInfo(T).second; 1017 } 1018 unsigned getTypeAlign(const Type *T) const { 1019 return getTypeInfo(T).second; 1020 } 1021 1022 /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in 1023 /// characters. This method does not work on incomplete types. 1024 CharUnits getTypeAlignInChars(QualType T) const; 1025 CharUnits getTypeAlignInChars(const Type *T) const; 1026 1027 std::pair<CharUnits, CharUnits> getTypeInfoInChars(const Type *T); 1028 std::pair<CharUnits, CharUnits> getTypeInfoInChars(QualType T); 1029 1030 /// getPreferredTypeAlign - Return the "preferred" alignment of the specified 1031 /// type for the current target in bits. This can be different than the ABI 1032 /// alignment in cases where it is beneficial for performance to overalign 1033 /// a data type. 1034 unsigned getPreferredTypeAlign(const Type *T) const; 1035 1036 /// getDeclAlign - Return a conservative estimate of the alignment of 1037 /// the specified decl. Note that bitfields do not have a valid alignment, so 1038 /// this method will assert on them. 1039 /// If @p RefAsPointee, references are treated like their underlying type 1040 /// (for alignof), else they're treated like pointers (for CodeGen). 1041 CharUnits getDeclAlign(const Decl *D, bool RefAsPointee = false) const; 1042 1043 /// getASTRecordLayout - Get or compute information about the layout of the 1044 /// specified record (struct/union/class), which indicates its size and field 1045 /// position information. 1046 const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D) const; 1047 1048 /// getASTObjCInterfaceLayout - Get or compute information about the 1049 /// layout of the specified Objective-C interface. 1050 const ASTRecordLayout &getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) 1051 const; 1052 1053 void DumpRecordLayout(const RecordDecl *RD, llvm::raw_ostream &OS) const; 1054 1055 /// getASTObjCImplementationLayout - Get or compute information about 1056 /// the layout of the specified Objective-C implementation. This may 1057 /// differ from the interface if synthesized ivars are present. 1058 const ASTRecordLayout & 1059 getASTObjCImplementationLayout(const ObjCImplementationDecl *D) const; 1060 1061 /// getKeyFunction - Get the key function for the given record decl, or NULL 1062 /// if there isn't one. The key function is, according to the Itanium C++ ABI 1063 /// section 5.2.3: 1064 /// 1065 /// ...the first non-pure virtual function that is not inline at the point 1066 /// of class definition. 1067 const CXXMethodDecl *getKeyFunction(const CXXRecordDecl *RD); 1068 1069 bool isNearlyEmpty(const CXXRecordDecl *RD) const; 1070 1071 MangleContext *createMangleContext(); 1072 1073 void ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI, 1074 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) 1075 const; 1076 1077 void DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, bool leafClass, 1078 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) const; 1079 1080 unsigned CountNonClassIvars(const ObjCInterfaceDecl *OI) const; 1081 void CollectInheritedProtocols(const Decl *CDecl, 1082 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols); 1083 1084 //===--------------------------------------------------------------------===// 1085 // Type Operators 1086 //===--------------------------------------------------------------------===// 1087 1088 /// getCanonicalType - Return the canonical (structural) type corresponding to 1089 /// the specified potentially non-canonical type. The non-canonical version 1090 /// of a type may have many "decorated" versions of types. Decorators can 1091 /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed 1092 /// to be free of any of these, allowing two canonical types to be compared 1093 /// for exact equality with a simple pointer comparison. 1094 CanQualType getCanonicalType(QualType T) const { 1095 return CanQualType::CreateUnsafe(T.getCanonicalType()); 1096 } 1097 1098 const Type *getCanonicalType(const Type *T) const { 1099 return T->getCanonicalTypeInternal().getTypePtr(); 1100 } 1101 1102 /// getCanonicalParamType - Return the canonical parameter type 1103 /// corresponding to the specific potentially non-canonical one. 1104 /// Qualifiers are stripped off, functions are turned into function 1105 /// pointers, and arrays decay one level into pointers. 1106 CanQualType getCanonicalParamType(QualType T) const; 1107 1108 /// \brief Determine whether the given types are equivalent. 1109 bool hasSameType(QualType T1, QualType T2) { 1110 return getCanonicalType(T1) == getCanonicalType(T2); 1111 } 1112 1113 /// \brief Returns this type as a completely-unqualified array type, 1114 /// capturing the qualifiers in Quals. This will remove the minimal amount of 1115 /// sugaring from the types, similar to the behavior of 1116 /// QualType::getUnqualifiedType(). 1117 /// 1118 /// \param T is the qualified type, which may be an ArrayType 1119 /// 1120 /// \param Quals will receive the full set of qualifiers that were 1121 /// applied to the array. 1122 /// 1123 /// \returns if this is an array type, the completely unqualified array type 1124 /// that corresponds to it. Otherwise, returns T.getUnqualifiedType(). 1125 QualType getUnqualifiedArrayType(QualType T, Qualifiers &Quals); 1126 1127 /// \brief Determine whether the given types are equivalent after 1128 /// cvr-qualifiers have been removed. 1129 bool hasSameUnqualifiedType(QualType T1, QualType T2) { 1130 return getCanonicalType(T1).getTypePtr() == 1131 getCanonicalType(T2).getTypePtr(); 1132 } 1133 1134 bool UnwrapSimilarPointerTypes(QualType &T1, QualType &T2); 1135 1136 /// \brief Retrieves the "canonical" nested name specifier for a 1137 /// given nested name specifier. 1138 /// 1139 /// The canonical nested name specifier is a nested name specifier 1140 /// that uniquely identifies a type or namespace within the type 1141 /// system. For example, given: 1142 /// 1143 /// \code 1144 /// namespace N { 1145 /// struct S { 1146 /// template<typename T> struct X { typename T* type; }; 1147 /// }; 1148 /// } 1149 /// 1150 /// template<typename T> struct Y { 1151 /// typename N::S::X<T>::type member; 1152 /// }; 1153 /// \endcode 1154 /// 1155 /// Here, the nested-name-specifier for N::S::X<T>:: will be 1156 /// S::X<template-param-0-0>, since 'S' and 'X' are uniquely defined 1157 /// by declarations in the type system and the canonical type for 1158 /// the template type parameter 'T' is template-param-0-0. 1159 NestedNameSpecifier * 1160 getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const; 1161 1162 /// \brief Retrieves the default calling convention to use for 1163 /// C++ instance methods. 1164 CallingConv getDefaultMethodCallConv(); 1165 1166 /// \brief Retrieves the canonical representation of the given 1167 /// calling convention. 1168 CallingConv getCanonicalCallConv(CallingConv CC) const { 1169 if (CC == CC_C) 1170 return CC_Default; 1171 return CC; 1172 } 1173 1174 /// \brief Determines whether two calling conventions name the same 1175 /// calling convention. 1176 bool isSameCallConv(CallingConv lcc, CallingConv rcc) { 1177 return (getCanonicalCallConv(lcc) == getCanonicalCallConv(rcc)); 1178 } 1179 1180 /// \brief Retrieves the "canonical" template name that refers to a 1181 /// given template. 1182 /// 1183 /// The canonical template name is the simplest expression that can 1184 /// be used to refer to a given template. For most templates, this 1185 /// expression is just the template declaration itself. For example, 1186 /// the template std::vector can be referred to via a variety of 1187 /// names---std::vector, ::std::vector, vector (if vector is in 1188 /// scope), etc.---but all of these names map down to the same 1189 /// TemplateDecl, which is used to form the canonical template name. 1190 /// 1191 /// Dependent template names are more interesting. Here, the 1192 /// template name could be something like T::template apply or 1193 /// std::allocator<T>::template rebind, where the nested name 1194 /// specifier itself is dependent. In this case, the canonical 1195 /// template name uses the shortest form of the dependent 1196 /// nested-name-specifier, which itself contains all canonical 1197 /// types, values, and templates. 1198 TemplateName getCanonicalTemplateName(TemplateName Name) const; 1199 1200 /// \brief Determine whether the given template names refer to the same 1201 /// template. 1202 bool hasSameTemplateName(TemplateName X, TemplateName Y); 1203 1204 /// \brief Retrieve the "canonical" template argument. 1205 /// 1206 /// The canonical template argument is the simplest template argument 1207 /// (which may be a type, value, expression, or declaration) that 1208 /// expresses the value of the argument. 1209 TemplateArgument getCanonicalTemplateArgument(const TemplateArgument &Arg) 1210 const; 1211 1212 /// Type Query functions. If the type is an instance of the specified class, 1213 /// return the Type pointer for the underlying maximally pretty type. This 1214 /// is a member of ASTContext because this may need to do some amount of 1215 /// canonicalization, e.g. to move type qualifiers into the element type. 1216 const ArrayType *getAsArrayType(QualType T) const; 1217 const ConstantArrayType *getAsConstantArrayType(QualType T) const { 1218 return dyn_cast_or_null<ConstantArrayType>(getAsArrayType(T)); 1219 } 1220 const VariableArrayType *getAsVariableArrayType(QualType T) const { 1221 return dyn_cast_or_null<VariableArrayType>(getAsArrayType(T)); 1222 } 1223 const IncompleteArrayType *getAsIncompleteArrayType(QualType T) const { 1224 return dyn_cast_or_null<IncompleteArrayType>(getAsArrayType(T)); 1225 } 1226 const DependentSizedArrayType *getAsDependentSizedArrayType(QualType T) 1227 const { 1228 return dyn_cast_or_null<DependentSizedArrayType>(getAsArrayType(T)); 1229 } 1230 1231 /// getBaseElementType - Returns the innermost element type of an array type. 1232 /// For example, will return "int" for int[m][n] 1233 QualType getBaseElementType(const ArrayType *VAT) const; 1234 1235 /// getBaseElementType - Returns the innermost element type of a type 1236 /// (which needn't actually be an array type). 1237 QualType getBaseElementType(QualType QT) const; 1238 1239 /// getConstantArrayElementCount - Returns number of constant array elements. 1240 uint64_t getConstantArrayElementCount(const ConstantArrayType *CA) const; 1241 1242 /// getArrayDecayedType - Return the properly qualified result of decaying the 1243 /// specified array type to a pointer. This operation is non-trivial when 1244 /// handling typedefs etc. The canonical type of "T" must be an array type, 1245 /// this returns a pointer to a properly qualified element of the array. 1246 /// 1247 /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. 1248 QualType getArrayDecayedType(QualType T) const; 1249 1250 /// getPromotedIntegerType - Returns the type that Promotable will 1251 /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable 1252 /// integer type. 1253 QualType getPromotedIntegerType(QualType PromotableType) const; 1254 1255 /// \brief Whether this is a promotable bitfield reference according 1256 /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). 1257 /// 1258 /// \returns the type this bit-field will promote to, or NULL if no 1259 /// promotion occurs. 1260 QualType isPromotableBitField(Expr *E) const; 1261 1262 /// getIntegerTypeOrder - Returns the highest ranked integer type: 1263 /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If 1264 /// LHS < RHS, return -1. 1265 int getIntegerTypeOrder(QualType LHS, QualType RHS) const; 1266 1267 /// getFloatingTypeOrder - Compare the rank of the two specified floating 1268 /// point types, ignoring the domain of the type (i.e. 'double' == 1269 /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If 1270 /// LHS < RHS, return -1. 1271 int getFloatingTypeOrder(QualType LHS, QualType RHS) const; 1272 1273 /// getFloatingTypeOfSizeWithinDomain - Returns a real floating 1274 /// point or a complex type (based on typeDomain/typeSize). 1275 /// 'typeDomain' is a real floating point or complex type. 1276 /// 'typeSize' is a real floating point or complex type. 1277 QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize, 1278 QualType typeDomain) const; 1279 1280private: 1281 // Helper for integer ordering 1282 unsigned getIntegerRank(const Type *T) const; 1283 1284public: 1285 1286 //===--------------------------------------------------------------------===// 1287 // Type Compatibility Predicates 1288 //===--------------------------------------------------------------------===// 1289 1290 /// Compatibility predicates used to check assignment expressions. 1291 bool typesAreCompatible(QualType T1, QualType T2, 1292 bool CompareUnqualified = false); // C99 6.2.7p1 1293 1294 bool typesAreBlockPointerCompatible(QualType, QualType); 1295 1296 bool isObjCIdType(QualType T) const { 1297 return T == ObjCIdTypedefType; 1298 } 1299 bool isObjCClassType(QualType T) const { 1300 return T == ObjCClassTypedefType; 1301 } 1302 bool isObjCSelType(QualType T) const { 1303 return T == ObjCSelTypedefType; 1304 } 1305 bool QualifiedIdConformsQualifiedId(QualType LHS, QualType RHS); 1306 bool ObjCQualifiedIdTypesAreCompatible(QualType LHS, QualType RHS, 1307 bool ForCompare); 1308 1309 bool ObjCQualifiedClassTypesAreCompatible(QualType LHS, QualType RHS); 1310 1311 // Check the safety of assignment from LHS to RHS 1312 bool canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, 1313 const ObjCObjectPointerType *RHSOPT); 1314 bool canAssignObjCInterfaces(const ObjCObjectType *LHS, 1315 const ObjCObjectType *RHS); 1316 bool canAssignObjCInterfacesInBlockPointer( 1317 const ObjCObjectPointerType *LHSOPT, 1318 const ObjCObjectPointerType *RHSOPT); 1319 bool areComparableObjCPointerTypes(QualType LHS, QualType RHS); 1320 QualType areCommonBaseCompatible(const ObjCObjectPointerType *LHSOPT, 1321 const ObjCObjectPointerType *RHSOPT); 1322 bool canBindObjCObjectType(QualType To, QualType From); 1323 1324 // Functions for calculating composite types 1325 QualType mergeTypes(QualType, QualType, bool OfBlockPointer=false, 1326 bool Unqualified = false); 1327 QualType mergeFunctionTypes(QualType, QualType, bool OfBlockPointer=false, 1328 bool Unqualified = false); 1329 QualType mergeFunctionArgumentTypes(QualType, QualType, 1330 bool OfBlockPointer=false, 1331 bool Unqualified = false); 1332 QualType mergeTransparentUnionType(QualType, QualType, 1333 bool OfBlockPointer=false, 1334 bool Unqualified = false); 1335 1336 QualType mergeObjCGCQualifiers(QualType, QualType); 1337 1338 void ResetObjCLayout(const ObjCContainerDecl *CD) { 1339 ObjCLayouts[CD] = 0; 1340 } 1341 1342 //===--------------------------------------------------------------------===// 1343 // Integer Predicates 1344 //===--------------------------------------------------------------------===// 1345 1346 // The width of an integer, as defined in C99 6.2.6.2. This is the number 1347 // of bits in an integer type excluding any padding bits. 1348 unsigned getIntWidth(QualType T) const; 1349 1350 // Per C99 6.2.5p6, for every signed integer type, there is a corresponding 1351 // unsigned integer type. This method takes a signed type, and returns the 1352 // corresponding unsigned integer type. 1353 QualType getCorrespondingUnsignedType(QualType T); 1354 1355 //===--------------------------------------------------------------------===// 1356 // Type Iterators. 1357 //===--------------------------------------------------------------------===// 1358 1359 typedef std::vector<Type*>::iterator type_iterator; 1360 typedef std::vector<Type*>::const_iterator const_type_iterator; 1361 1362 type_iterator types_begin() { return Types.begin(); } 1363 type_iterator types_end() { return Types.end(); } 1364 const_type_iterator types_begin() const { return Types.begin(); } 1365 const_type_iterator types_end() const { return Types.end(); } 1366 1367 //===--------------------------------------------------------------------===// 1368 // Integer Values 1369 //===--------------------------------------------------------------------===// 1370 1371 /// MakeIntValue - Make an APSInt of the appropriate width and 1372 /// signedness for the given \arg Value and integer \arg Type. 1373 llvm::APSInt MakeIntValue(uint64_t Value, QualType Type) const { 1374 llvm::APSInt Res(getIntWidth(Type), !Type->isSignedIntegerType()); 1375 Res = Value; 1376 return Res; 1377 } 1378 1379 /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. 1380 ObjCImplementationDecl *getObjCImplementation(ObjCInterfaceDecl *D); 1381 /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. 1382 ObjCCategoryImplDecl *getObjCImplementation(ObjCCategoryDecl *D); 1383 1384 /// \brief returns true if there is at lease one @implementation in TU. 1385 bool AnyObjCImplementation() { 1386 return !ObjCImpls.empty(); 1387 } 1388 1389 /// \brief Set the implementation of ObjCInterfaceDecl. 1390 void setObjCImplementation(ObjCInterfaceDecl *IFaceD, 1391 ObjCImplementationDecl *ImplD); 1392 /// \brief Set the implementation of ObjCCategoryDecl. 1393 void setObjCImplementation(ObjCCategoryDecl *CatD, 1394 ObjCCategoryImplDecl *ImplD); 1395 1396 /// \brief Set the copy inialization expression of a block var decl. 1397 void setBlockVarCopyInits(VarDecl*VD, Expr* Init); 1398 /// \brief Get the copy initialization expression of VarDecl,or NULL if 1399 /// none exists. 1400 Expr *getBlockVarCopyInits(const VarDecl*VD); 1401 1402 /// \brief Allocate an uninitialized TypeSourceInfo. 1403 /// 1404 /// The caller should initialize the memory held by TypeSourceInfo using 1405 /// the TypeLoc wrappers. 1406 /// 1407 /// \param T the type that will be the basis for type source info. This type 1408 /// should refer to how the declarator was written in source code, not to 1409 /// what type semantic analysis resolved the declarator to. 1410 /// 1411 /// \param Size the size of the type info to create, or 0 if the size 1412 /// should be calculated based on the type. 1413 TypeSourceInfo *CreateTypeSourceInfo(QualType T, unsigned Size = 0) const; 1414 1415 /// \brief Allocate a TypeSourceInfo where all locations have been 1416 /// initialized to a given location, which defaults to the empty 1417 /// location. 1418 TypeSourceInfo * 1419 getTrivialTypeSourceInfo(QualType T, 1420 SourceLocation Loc = SourceLocation()) const; 1421 1422 TypeSourceInfo *getNullTypeSourceInfo() { return &NullTypeSourceInfo; } 1423 1424 /// \brief Add a deallocation callback that will be invoked when the 1425 /// ASTContext is destroyed. 1426 /// 1427 /// \brief Callback A callback function that will be invoked on destruction. 1428 /// 1429 /// \brief Data Pointer data that will be provided to the callback function 1430 /// when it is called. 1431 void AddDeallocation(void (*Callback)(void*), void *Data); 1432 1433 GVALinkage GetGVALinkageForFunction(const FunctionDecl *FD); 1434 GVALinkage GetGVALinkageForVariable(const VarDecl *VD); 1435 1436 /// \brief Determines if the decl can be CodeGen'ed or deserialized from PCH 1437 /// lazily, only when used; this is only relevant for function or file scoped 1438 /// var definitions. 1439 /// 1440 /// \returns true if the function/var must be CodeGen'ed/deserialized even if 1441 /// it is not used. 1442 bool DeclMustBeEmitted(const Decl *D); 1443 1444 //===--------------------------------------------------------------------===// 1445 // Statistics 1446 //===--------------------------------------------------------------------===// 1447 1448 /// \brief The number of implicitly-declared default constructors. 1449 static unsigned NumImplicitDefaultConstructors; 1450 1451 /// \brief The number of implicitly-declared default constructors for 1452 /// which declarations were built. 1453 static unsigned NumImplicitDefaultConstructorsDeclared; 1454 1455 /// \brief The number of implicitly-declared copy constructors. 1456 static unsigned NumImplicitCopyConstructors; 1457 1458 /// \brief The number of implicitly-declared copy constructors for 1459 /// which declarations were built. 1460 static unsigned NumImplicitCopyConstructorsDeclared; 1461 1462 /// \brief The number of implicitly-declared copy assignment operators. 1463 static unsigned NumImplicitCopyAssignmentOperators; 1464 1465 /// \brief The number of implicitly-declared copy assignment operators for 1466 /// which declarations were built. 1467 static unsigned NumImplicitCopyAssignmentOperatorsDeclared; 1468 1469 /// \brief The number of implicitly-declared destructors. 1470 static unsigned NumImplicitDestructors; 1471 1472 /// \brief The number of implicitly-declared destructors for which 1473 /// declarations were built. 1474 static unsigned NumImplicitDestructorsDeclared; 1475 1476private: 1477 ASTContext(const ASTContext&); // DO NOT IMPLEMENT 1478 void operator=(const ASTContext&); // DO NOT IMPLEMENT 1479 1480 void InitBuiltinTypes(); 1481 void InitBuiltinType(CanQualType &R, BuiltinType::Kind K); 1482 1483 // Return the ObjC type encoding for a given type. 1484 void getObjCEncodingForTypeImpl(QualType t, std::string &S, 1485 bool ExpandPointedToStructures, 1486 bool ExpandStructures, 1487 const FieldDecl *Field, 1488 bool OutermostType = false, 1489 bool EncodingProperty = false) const; 1490 1491 const ASTRecordLayout & 1492 getObjCLayout(const ObjCInterfaceDecl *D, 1493 const ObjCImplementationDecl *Impl) const; 1494 1495private: 1496 /// \brief A set of deallocations that should be performed when the 1497 /// ASTContext is destroyed. 1498 llvm::SmallVector<std::pair<void (*)(void*), void *>, 16> Deallocations; 1499 1500 // FIXME: This currently contains the set of StoredDeclMaps used 1501 // by DeclContext objects. This probably should not be in ASTContext, 1502 // but we include it here so that ASTContext can quickly deallocate them. 1503 llvm::PointerIntPair<StoredDeclsMap*,1> LastSDM; 1504 1505 /// \brief A counter used to uniquely identify "blocks". 1506 mutable unsigned int UniqueBlockByRefTypeID; 1507 mutable unsigned int UniqueBlockParmTypeID; 1508 1509 friend class DeclContext; 1510 friend class DeclarationNameTable; 1511 void ReleaseDeclContextMaps(); 1512}; 1513 1514/// @brief Utility function for constructing a nullary selector. 1515static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) { 1516 IdentifierInfo* II = &Ctx.Idents.get(name); 1517 return Ctx.Selectors.getSelector(0, &II); 1518} 1519 1520/// @brief Utility function for constructing an unary selector. 1521static inline Selector GetUnarySelector(const char* name, ASTContext& Ctx) { 1522 IdentifierInfo* II = &Ctx.Idents.get(name); 1523 return Ctx.Selectors.getSelector(1, &II); 1524} 1525 1526} // end namespace clang 1527 1528// operator new and delete aren't allowed inside namespaces. 1529// The throw specifications are mandated by the standard. 1530/// @brief Placement new for using the ASTContext's allocator. 1531/// 1532/// This placement form of operator new uses the ASTContext's allocator for 1533/// obtaining memory. It is a non-throwing new, which means that it returns 1534/// null on error. (If that is what the allocator does. The current does, so if 1535/// this ever changes, this operator will have to be changed, too.) 1536/// Usage looks like this (assuming there's an ASTContext 'Context' in scope): 1537/// @code 1538/// // Default alignment (8) 1539/// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments); 1540/// // Specific alignment 1541/// IntegerLiteral *Ex2 = new (Context, 4) IntegerLiteral(arguments); 1542/// @endcode 1543/// Please note that you cannot use delete on the pointer; it must be 1544/// deallocated using an explicit destructor call followed by 1545/// @c Context.Deallocate(Ptr). 1546/// 1547/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 1548/// @param C The ASTContext that provides the allocator. 1549/// @param Alignment The alignment of the allocated memory (if the underlying 1550/// allocator supports it). 1551/// @return The allocated memory. Could be NULL. 1552inline void *operator new(size_t Bytes, const clang::ASTContext &C, 1553 size_t Alignment) throw () { 1554 return C.Allocate(Bytes, Alignment); 1555} 1556/// @brief Placement delete companion to the new above. 1557/// 1558/// This operator is just a companion to the new above. There is no way of 1559/// invoking it directly; see the new operator for more details. This operator 1560/// is called implicitly by the compiler if a placement new expression using 1561/// the ASTContext throws in the object constructor. 1562inline void operator delete(void *Ptr, const clang::ASTContext &C, size_t) 1563 throw () { 1564 C.Deallocate(Ptr); 1565} 1566 1567/// This placement form of operator new[] uses the ASTContext's allocator for 1568/// obtaining memory. It is a non-throwing new[], which means that it returns 1569/// null on error. 1570/// Usage looks like this (assuming there's an ASTContext 'Context' in scope): 1571/// @code 1572/// // Default alignment (8) 1573/// char *data = new (Context) char[10]; 1574/// // Specific alignment 1575/// char *data = new (Context, 4) char[10]; 1576/// @endcode 1577/// Please note that you cannot use delete on the pointer; it must be 1578/// deallocated using an explicit destructor call followed by 1579/// @c Context.Deallocate(Ptr). 1580/// 1581/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 1582/// @param C The ASTContext that provides the allocator. 1583/// @param Alignment The alignment of the allocated memory (if the underlying 1584/// allocator supports it). 1585/// @return The allocated memory. Could be NULL. 1586inline void *operator new[](size_t Bytes, const clang::ASTContext& C, 1587 size_t Alignment = 8) throw () { 1588 return C.Allocate(Bytes, Alignment); 1589} 1590 1591/// @brief Placement delete[] companion to the new[] above. 1592/// 1593/// This operator is just a companion to the new[] above. There is no way of 1594/// invoking it directly; see the new[] operator for more details. This operator 1595/// is called implicitly by the compiler if a placement new[] expression using 1596/// the ASTContext throws in the object constructor. 1597inline void operator delete[](void *Ptr, const clang::ASTContext &C, size_t) 1598 throw () { 1599 C.Deallocate(Ptr); 1600} 1601 1602#endif 1603