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