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