ASTContext.h revision e6a365d772a6b455f1e23ac9ae5f40d65a55a18c
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 /// adjustFunctionType - Change the ExtInfo on a function type. 483 const FunctionType *adjustFunctionType(const FunctionType *Fn, 484 FunctionType::ExtInfo EInfo); 485 486 /// getNoReturnType - Add or remove the noreturn attribute to the given type 487 /// which must be a FunctionType or a pointer to an allowable type or a 488 /// BlockPointer. 489 QualType getNoReturnType(QualType T, bool AddNoReturn = true); 490 491 /// getComplexType - Return the uniqued reference to the type for a complex 492 /// number with the specified element type. 493 QualType getComplexType(QualType T); 494 CanQualType getComplexType(CanQualType T) { 495 return CanQualType::CreateUnsafe(getComplexType((QualType) T)); 496 } 497 498 /// getPointerType - Return the uniqued reference to the type for a pointer to 499 /// the specified type. 500 QualType getPointerType(QualType T); 501 CanQualType getPointerType(CanQualType T) { 502 return CanQualType::CreateUnsafe(getPointerType((QualType) T)); 503 } 504 505 /// getBlockPointerType - Return the uniqued reference to the type for a block 506 /// of the specified type. 507 QualType getBlockPointerType(QualType T); 508 509 /// This gets the struct used to keep track of the descriptor for pointer to 510 /// blocks. 511 QualType getBlockDescriptorType(); 512 513 // Set the type for a Block descriptor type. 514 void setBlockDescriptorType(QualType T); 515 /// Get the BlockDescriptorType type, or NULL if it hasn't yet been built. 516 QualType getRawBlockdescriptorType() { 517 if (BlockDescriptorType) 518 return getTagDeclType(BlockDescriptorType); 519 return QualType(); 520 } 521 522 /// This gets the struct used to keep track of the extended descriptor for 523 /// pointer to blocks. 524 QualType getBlockDescriptorExtendedType(); 525 526 // Set the type for a Block descriptor extended type. 527 void setBlockDescriptorExtendedType(QualType T); 528 /// Get the BlockDescriptorExtendedType type, or NULL if it hasn't yet been 529 /// built. 530 QualType getRawBlockdescriptorExtendedType() { 531 if (BlockDescriptorExtendedType) 532 return getTagDeclType(BlockDescriptorExtendedType); 533 return QualType(); 534 } 535 536 /// This gets the struct used to keep track of pointer to blocks, complete 537 /// with captured variables. 538 QualType getBlockParmType(bool BlockHasCopyDispose, 539 llvm::SmallVectorImpl<const Expr *> &Layout); 540 541 /// This builds the struct used for __block variables. 542 QualType BuildByRefType(llvm::StringRef DeclName, QualType Ty); 543 544 /// Returns true iff we need copy/dispose helpers for the given type. 545 bool BlockRequiresCopying(QualType Ty); 546 547 /// getLValueReferenceType - Return the uniqued reference to the type for an 548 /// lvalue reference to the specified type. 549 QualType getLValueReferenceType(QualType T, bool SpelledAsLValue = true); 550 551 /// getRValueReferenceType - Return the uniqued reference to the type for an 552 /// rvalue reference to the specified type. 553 QualType getRValueReferenceType(QualType T); 554 555 /// getMemberPointerType - Return the uniqued reference to the type for a 556 /// member pointer to the specified type in the specified class. The class 557 /// is a Type because it could be a dependent name. 558 QualType getMemberPointerType(QualType T, const Type *Cls); 559 560 /// getVariableArrayType - Returns a non-unique reference to the type for a 561 /// variable array of the specified element type. 562 QualType getVariableArrayType(QualType EltTy, Expr *NumElts, 563 ArrayType::ArraySizeModifier ASM, 564 unsigned EltTypeQuals, 565 SourceRange Brackets); 566 567 /// getDependentSizedArrayType - Returns a non-unique reference to 568 /// the type for a dependently-sized array of the specified element 569 /// type. FIXME: We will need these to be uniqued, or at least 570 /// comparable, at some point. 571 QualType getDependentSizedArrayType(QualType EltTy, Expr *NumElts, 572 ArrayType::ArraySizeModifier ASM, 573 unsigned EltTypeQuals, 574 SourceRange Brackets); 575 576 /// getIncompleteArrayType - Returns a unique reference to the type for a 577 /// incomplete array of the specified element type. 578 QualType getIncompleteArrayType(QualType EltTy, 579 ArrayType::ArraySizeModifier ASM, 580 unsigned EltTypeQuals); 581 582 /// getConstantArrayType - Return the unique reference to the type for a 583 /// constant array of the specified element type. 584 QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize, 585 ArrayType::ArraySizeModifier ASM, 586 unsigned EltTypeQuals); 587 588 /// getUnknownSizeVariableArrayType - Return a variable array type with 589 /// all variable indices replaced with unknow [*] size. 590 QualType getUnknownSizeVariableArrayType(QualType Ty); 591 592 /// getVariableArrayDecayedType - Returns a vla type where known sizes 593 /// are replaced with [*] 594 QualType getVariableArrayDecayedType(QualType Ty); 595 596 /// getVectorType - Return the unique reference to a vector type of 597 /// the specified element type and size. VectorType must be a built-in type. 598 QualType getVectorType(QualType VectorType, unsigned NumElts, 599 VectorType::VectorKind VecKind); 600 601 /// getExtVectorType - Return the unique reference to an extended vector type 602 /// of the specified element type and size. VectorType must be a built-in 603 /// type. 604 QualType getExtVectorType(QualType VectorType, unsigned NumElts); 605 606 /// getDependentSizedExtVectorType - Returns a non-unique reference to 607 /// the type for a dependently-sized vector of the specified element 608 /// type. FIXME: We will need these to be uniqued, or at least 609 /// comparable, at some point. 610 QualType getDependentSizedExtVectorType(QualType VectorType, 611 Expr *SizeExpr, 612 SourceLocation AttrLoc); 613 614 /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. 615 /// 616 QualType getFunctionNoProtoType(QualType ResultTy, 617 const FunctionType::ExtInfo &Info); 618 619 QualType getFunctionNoProtoType(QualType ResultTy) { 620 return getFunctionNoProtoType(ResultTy, FunctionType::ExtInfo()); 621 } 622 623 /// getFunctionType - Return a normal function type with a typed 624 /// argument list. 625 QualType getFunctionType(QualType ResultTy, 626 const QualType *Args, unsigned NumArgs, 627 const FunctionProtoType::ExtProtoInfo &EPI); 628 629 /// getTypeDeclType - Return the unique reference to the type for 630 /// the specified type declaration. 631 QualType getTypeDeclType(const TypeDecl *Decl, 632 const TypeDecl *PrevDecl = 0) { 633 assert(Decl && "Passed null for Decl param"); 634 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); 635 636 if (PrevDecl) { 637 assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl"); 638 Decl->TypeForDecl = PrevDecl->TypeForDecl; 639 return QualType(PrevDecl->TypeForDecl, 0); 640 } 641 642 return getTypeDeclTypeSlow(Decl); 643 } 644 645 /// getTypedefType - Return the unique reference to the type for the 646 /// specified typename decl. 647 QualType getTypedefType(const TypedefDecl *Decl, QualType Canon = QualType()); 648 649 QualType getRecordType(const RecordDecl *Decl); 650 651 QualType getEnumType(const EnumDecl *Decl); 652 653 QualType getInjectedClassNameType(CXXRecordDecl *Decl, QualType TST); 654 655 QualType getSubstTemplateTypeParmType(const TemplateTypeParmType *Replaced, 656 QualType Replacement); 657 658 QualType getTemplateTypeParmType(unsigned Depth, unsigned Index, 659 bool ParameterPack, 660 IdentifierInfo *Name = 0); 661 662 QualType getTemplateSpecializationType(TemplateName T, 663 const TemplateArgument *Args, 664 unsigned NumArgs, 665 QualType Canon = QualType()); 666 667 QualType getCanonicalTemplateSpecializationType(TemplateName T, 668 const TemplateArgument *Args, 669 unsigned NumArgs); 670 671 QualType getTemplateSpecializationType(TemplateName T, 672 const TemplateArgumentListInfo &Args, 673 QualType Canon = QualType()); 674 675 TypeSourceInfo * 676 getTemplateSpecializationTypeInfo(TemplateName T, SourceLocation TLoc, 677 const TemplateArgumentListInfo &Args, 678 QualType Canon = QualType()); 679 680 QualType getParenType(QualType NamedType); 681 682 QualType getElaboratedType(ElaboratedTypeKeyword Keyword, 683 NestedNameSpecifier *NNS, 684 QualType NamedType); 685 QualType getDependentNameType(ElaboratedTypeKeyword Keyword, 686 NestedNameSpecifier *NNS, 687 const IdentifierInfo *Name, 688 QualType Canon = QualType()); 689 690 QualType getDependentTemplateSpecializationType(ElaboratedTypeKeyword Keyword, 691 NestedNameSpecifier *NNS, 692 const IdentifierInfo *Name, 693 const TemplateArgumentListInfo &Args); 694 QualType getDependentTemplateSpecializationType(ElaboratedTypeKeyword Keyword, 695 NestedNameSpecifier *NNS, 696 const IdentifierInfo *Name, 697 unsigned NumArgs, 698 const TemplateArgument *Args); 699 700 QualType getObjCInterfaceType(const ObjCInterfaceDecl *Decl); 701 702 QualType getObjCObjectType(QualType Base, 703 ObjCProtocolDecl * const *Protocols, 704 unsigned NumProtocols); 705 706 /// getObjCObjectPointerType - Return a ObjCObjectPointerType type 707 /// for the given ObjCObjectType. 708 QualType getObjCObjectPointerType(QualType OIT); 709 710 /// getTypeOfType - GCC extension. 711 QualType getTypeOfExprType(Expr *e); 712 QualType getTypeOfType(QualType t); 713 714 /// getDecltypeType - C++0x decltype. 715 QualType getDecltypeType(Expr *e); 716 717 /// getTagDeclType - Return the unique reference to the type for the 718 /// specified TagDecl (struct/union/class/enum) decl. 719 QualType getTagDeclType(const TagDecl *Decl); 720 721 /// getSizeType - Return the unique type for "size_t" (C99 7.17), defined 722 /// in <stddef.h>. The sizeof operator requires this (C99 6.5.3.4p4). 723 CanQualType getSizeType() const; 724 725 /// getWCharType - In C++, this returns the unique wchar_t type. In C99, this 726 /// returns a type compatible with the type defined in <stddef.h> as defined 727 /// by the target. 728 QualType getWCharType() const { return WCharTy; } 729 730 /// getSignedWCharType - Return the type of "signed wchar_t". 731 /// Used when in C++, as a GCC extension. 732 QualType getSignedWCharType() const; 733 734 /// getUnsignedWCharType - Return the type of "unsigned wchar_t". 735 /// Used when in C++, as a GCC extension. 736 QualType getUnsignedWCharType() const; 737 738 /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) 739 /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). 740 QualType getPointerDiffType() const; 741 742 // getCFConstantStringType - Return the C structure type used to represent 743 // constant CFStrings. 744 QualType getCFConstantStringType(); 745 746 // getNSConstantStringType - Return the C structure type used to represent 747 // constant NSStrings. 748 QualType getNSConstantStringType(); 749 /// Get the structure type used to representation NSStrings, or NULL 750 /// if it hasn't yet been built. 751 QualType getRawNSConstantStringType() { 752 if (NSConstantStringTypeDecl) 753 return getTagDeclType(NSConstantStringTypeDecl); 754 return QualType(); 755 } 756 void setNSConstantStringType(QualType T); 757 758 759 /// Get the structure type used to representation CFStrings, or NULL 760 /// if it hasn't yet been built. 761 QualType getRawCFConstantStringType() { 762 if (CFConstantStringTypeDecl) 763 return getTagDeclType(CFConstantStringTypeDecl); 764 return QualType(); 765 } 766 void setCFConstantStringType(QualType T); 767 768 // This setter/getter represents the ObjC type for an NSConstantString. 769 void setObjCConstantStringInterface(ObjCInterfaceDecl *Decl); 770 QualType getObjCConstantStringInterface() const { 771 return ObjCConstantStringType; 772 } 773 774 //// This gets the struct used to keep track of fast enumerations. 775 QualType getObjCFastEnumerationStateType(); 776 777 /// Get the ObjCFastEnumerationState type, or NULL if it hasn't yet 778 /// been built. 779 QualType getRawObjCFastEnumerationStateType() { 780 if (ObjCFastEnumerationStateTypeDecl) 781 return getTagDeclType(ObjCFastEnumerationStateTypeDecl); 782 return QualType(); 783 } 784 785 void setObjCFastEnumerationStateType(QualType T); 786 787 /// \brief Set the type for the C FILE type. 788 void setFILEDecl(TypeDecl *FILEDecl) { this->FILEDecl = FILEDecl; } 789 790 /// \brief Retrieve the C FILE type. 791 QualType getFILEType() { 792 if (FILEDecl) 793 return getTypeDeclType(FILEDecl); 794 return QualType(); 795 } 796 797 /// \brief Set the type for the C jmp_buf type. 798 void setjmp_bufDecl(TypeDecl *jmp_bufDecl) { 799 this->jmp_bufDecl = jmp_bufDecl; 800 } 801 802 /// \brief Retrieve the C jmp_buf type. 803 QualType getjmp_bufType() { 804 if (jmp_bufDecl) 805 return getTypeDeclType(jmp_bufDecl); 806 return QualType(); 807 } 808 809 /// \brief Set the type for the C sigjmp_buf type. 810 void setsigjmp_bufDecl(TypeDecl *sigjmp_bufDecl) { 811 this->sigjmp_bufDecl = sigjmp_bufDecl; 812 } 813 814 /// \brief Retrieve the C sigjmp_buf type. 815 QualType getsigjmp_bufType() { 816 if (sigjmp_bufDecl) 817 return getTypeDeclType(sigjmp_bufDecl); 818 return QualType(); 819 } 820 821 /// getObjCEncodingForType - Emit the ObjC type encoding for the 822 /// given type into \arg S. If \arg NameFields is specified then 823 /// record field names are also encoded. 824 void getObjCEncodingForType(QualType t, std::string &S, 825 const FieldDecl *Field=0); 826 827 void getLegacyIntegralTypeEncoding(QualType &t) const; 828 829 // Put the string version of type qualifiers into S. 830 void getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, 831 std::string &S) const; 832 833 /// getObjCEncodingForMethodDecl - Return the encoded type for this method 834 /// declaration. 835 void getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, std::string &S); 836 837 /// getObjCEncodingForBlockDecl - Return the encoded type for this block 838 /// declaration. 839 void getObjCEncodingForBlock(const BlockExpr *Expr, std::string& S); 840 841 /// getObjCEncodingForPropertyDecl - Return the encoded type for 842 /// this method declaration. If non-NULL, Container must be either 843 /// an ObjCCategoryImplDecl or ObjCImplementationDecl; it should 844 /// only be NULL when getting encodings for protocol properties. 845 void getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, 846 const Decl *Container, 847 std::string &S); 848 849 bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, 850 ObjCProtocolDecl *rProto); 851 852 /// getObjCEncodingTypeSize returns size of type for objective-c encoding 853 /// purpose in characters. 854 CharUnits getObjCEncodingTypeSize(QualType t); 855 856 /// \brief Whether __[u]int128_t identifier is installed. 857 bool isInt128Installed() const { return IsInt128Installed; } 858 void setInt128Installed() { IsInt128Installed = true; } 859 860 /// This setter/getter represents the ObjC 'id' type. It is setup lazily, by 861 /// Sema. id is always a (typedef for a) pointer type, a pointer to a struct. 862 QualType getObjCIdType() const { return ObjCIdTypedefType; } 863 void setObjCIdType(QualType T); 864 865 void setObjCSelType(QualType T); 866 QualType getObjCSelType() const { return ObjCSelTypedefType; } 867 868 void setObjCProtoType(QualType QT); 869 QualType getObjCProtoType() const { return ObjCProtoType; } 870 871 /// This setter/getter repreents the ObjC 'Class' type. It is setup lazily, by 872 /// Sema. 'Class' is always a (typedef for a) pointer type, a pointer to a 873 /// struct. 874 QualType getObjCClassType() const { return ObjCClassTypedefType; } 875 void setObjCClassType(QualType T); 876 877 void setBuiltinVaListType(QualType T); 878 QualType getBuiltinVaListType() const { return BuiltinVaListType; } 879 880 /// getCVRQualifiedType - Returns a type with additional const, 881 /// volatile, or restrict qualifiers. 882 QualType getCVRQualifiedType(QualType T, unsigned CVR) { 883 return getQualifiedType(T, Qualifiers::fromCVRMask(CVR)); 884 } 885 886 /// getQualifiedType - Returns a type with additional qualifiers. 887 QualType getQualifiedType(QualType T, Qualifiers Qs) { 888 if (!Qs.hasNonFastQualifiers()) 889 return T.withFastQualifiers(Qs.getFastQualifiers()); 890 QualifierCollector Qc(Qs); 891 const Type *Ptr = Qc.strip(T); 892 return getExtQualType(Ptr, Qc); 893 } 894 895 /// getQualifiedType - Returns a type with additional qualifiers. 896 QualType getQualifiedType(const Type *T, Qualifiers Qs) { 897 if (!Qs.hasNonFastQualifiers()) 898 return QualType(T, Qs.getFastQualifiers()); 899 return getExtQualType(T, Qs); 900 } 901 902 DeclarationNameInfo getNameForTemplate(TemplateName Name, 903 SourceLocation NameLoc); 904 905 TemplateName getOverloadedTemplateName(UnresolvedSetIterator Begin, 906 UnresolvedSetIterator End); 907 908 TemplateName getQualifiedTemplateName(NestedNameSpecifier *NNS, 909 bool TemplateKeyword, 910 TemplateDecl *Template); 911 912 TemplateName getDependentTemplateName(NestedNameSpecifier *NNS, 913 const IdentifierInfo *Name); 914 TemplateName getDependentTemplateName(NestedNameSpecifier *NNS, 915 OverloadedOperatorKind Operator); 916 917 enum GetBuiltinTypeError { 918 GE_None, //< No error 919 GE_Missing_stdio, //< Missing a type from <stdio.h> 920 GE_Missing_setjmp //< Missing a type from <setjmp.h> 921 }; 922 923 /// GetBuiltinType - Return the type for the specified builtin. If 924 /// IntegerConstantArgs is non-null, it is filled in with a bitmask of 925 /// arguments to the builtin that are required to be integer constant 926 /// expressions. 927 QualType GetBuiltinType(unsigned ID, GetBuiltinTypeError &Error, 928 unsigned *IntegerConstantArgs = 0); 929 930private: 931 CanQualType getFromTargetType(unsigned Type) const; 932 933 //===--------------------------------------------------------------------===// 934 // Type Predicates. 935 //===--------------------------------------------------------------------===// 936 937public: 938 /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's 939 /// garbage collection attribute. 940 /// 941 Qualifiers::GC getObjCGCAttrKind(const QualType &Ty) const; 942 943 /// areCompatibleVectorTypes - Return true if the given vector types 944 /// are of the same unqualified type or if they are equivalent to the same 945 /// GCC vector type, ignoring whether they are target-specific (AltiVec or 946 /// Neon) types. 947 bool areCompatibleVectorTypes(QualType FirstVec, QualType SecondVec); 948 949 /// isObjCNSObjectType - Return true if this is an NSObject object with 950 /// its NSObject attribute set. 951 bool isObjCNSObjectType(QualType Ty) const; 952 953 //===--------------------------------------------------------------------===// 954 // Type Sizing and Analysis 955 //===--------------------------------------------------------------------===// 956 957 /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified 958 /// scalar floating point type. 959 const llvm::fltSemantics &getFloatTypeSemantics(QualType T) const; 960 961 /// getTypeInfo - Get the size and alignment of the specified complete type in 962 /// bits. 963 std::pair<uint64_t, unsigned> getTypeInfo(const Type *T); 964 std::pair<uint64_t, unsigned> getTypeInfo(QualType T) { 965 return getTypeInfo(T.getTypePtr()); 966 } 967 968 /// getTypeSize - Return the size of the specified type, in bits. This method 969 /// does not work on incomplete types. 970 uint64_t getTypeSize(QualType T) { 971 return getTypeInfo(T).first; 972 } 973 uint64_t getTypeSize(const Type *T) { 974 return getTypeInfo(T).first; 975 } 976 977 /// getCharWidth - Return the size of the character type, in bits 978 uint64_t getCharWidth() { 979 return getTypeSize(CharTy); 980 } 981 982 /// getTypeSizeInChars - Return the size of the specified type, in characters. 983 /// This method does not work on incomplete types. 984 CharUnits getTypeSizeInChars(QualType T); 985 CharUnits getTypeSizeInChars(const Type *T); 986 987 /// getTypeAlign - Return the ABI-specified alignment of a type, in bits. 988 /// This method does not work on incomplete types. 989 unsigned getTypeAlign(QualType T) { 990 return getTypeInfo(T).second; 991 } 992 unsigned getTypeAlign(const Type *T) { 993 return getTypeInfo(T).second; 994 } 995 996 /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in 997 /// characters. This method does not work on incomplete types. 998 CharUnits getTypeAlignInChars(QualType T); 999 CharUnits getTypeAlignInChars(const Type *T); 1000 1001 std::pair<CharUnits, CharUnits> getTypeInfoInChars(const Type *T); 1002 std::pair<CharUnits, CharUnits> getTypeInfoInChars(QualType T); 1003 1004 /// getPreferredTypeAlign - Return the "preferred" alignment of the specified 1005 /// type for the current target in bits. This can be different than the ABI 1006 /// alignment in cases where it is beneficial for performance to overalign 1007 /// a data type. 1008 unsigned getPreferredTypeAlign(const Type *T); 1009 1010 /// getDeclAlign - Return a conservative estimate of the alignment of 1011 /// the specified decl. Note that bitfields do not have a valid alignment, so 1012 /// this method will assert on them. 1013 /// If @p RefAsPointee, references are treated like their underlying type 1014 /// (for alignof), else they're treated like pointers (for CodeGen). 1015 CharUnits getDeclAlign(const Decl *D, bool RefAsPointee = false); 1016 1017 /// getASTRecordLayout - Get or compute information about the layout of the 1018 /// specified record (struct/union/class), which indicates its size and field 1019 /// position information. 1020 const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D); 1021 1022 /// getASTObjCInterfaceLayout - Get or compute information about the 1023 /// layout of the specified Objective-C interface. 1024 const ASTRecordLayout &getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D); 1025 1026 void DumpRecordLayout(const RecordDecl *RD, llvm::raw_ostream &OS); 1027 1028 /// getASTObjCImplementationLayout - Get or compute information about 1029 /// the layout of the specified Objective-C implementation. This may 1030 /// differ from the interface if synthesized ivars are present. 1031 const ASTRecordLayout & 1032 getASTObjCImplementationLayout(const ObjCImplementationDecl *D); 1033 1034 /// getKeyFunction - Get the key function for the given record decl, or NULL 1035 /// if there isn't one. The key function is, according to the Itanium C++ ABI 1036 /// section 5.2.3: 1037 /// 1038 /// ...the first non-pure virtual function that is not inline at the point 1039 /// of class definition. 1040 const CXXMethodDecl *getKeyFunction(const CXXRecordDecl *RD); 1041 1042 bool isNearlyEmpty(const CXXRecordDecl *RD); 1043 1044 void ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI, 1045 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars); 1046 1047 void DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, bool leafClass, 1048 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars); 1049 1050 unsigned CountNonClassIvars(const ObjCInterfaceDecl *OI); 1051 void CollectInheritedProtocols(const Decl *CDecl, 1052 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols); 1053 1054 //===--------------------------------------------------------------------===// 1055 // Type Operators 1056 //===--------------------------------------------------------------------===// 1057 1058 /// getCanonicalType - Return the canonical (structural) type corresponding to 1059 /// the specified potentially non-canonical type. The non-canonical version 1060 /// of a type may have many "decorated" versions of types. Decorators can 1061 /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed 1062 /// to be free of any of these, allowing two canonical types to be compared 1063 /// for exact equality with a simple pointer comparison. 1064 CanQualType getCanonicalType(QualType T); 1065 const Type *getCanonicalType(const Type *T) { 1066 return T->getCanonicalTypeInternal().getTypePtr(); 1067 } 1068 1069 /// getCanonicalParamType - Return the canonical parameter type 1070 /// corresponding to the specific potentially non-canonical one. 1071 /// Qualifiers are stripped off, functions are turned into function 1072 /// pointers, and arrays decay one level into pointers. 1073 CanQualType getCanonicalParamType(QualType T); 1074 1075 /// \brief Determine whether the given types are equivalent. 1076 bool hasSameType(QualType T1, QualType T2) { 1077 return getCanonicalType(T1) == getCanonicalType(T2); 1078 } 1079 1080 /// \brief Returns this type as a completely-unqualified array type, 1081 /// capturing the qualifiers in Quals. This will remove the minimal amount of 1082 /// sugaring from the types, similar to the behavior of 1083 /// QualType::getUnqualifiedType(). 1084 /// 1085 /// \param T is the qualified type, which may be an ArrayType 1086 /// 1087 /// \param Quals will receive the full set of qualifiers that were 1088 /// applied to the array. 1089 /// 1090 /// \returns if this is an array type, the completely unqualified array type 1091 /// that corresponds to it. Otherwise, returns T.getUnqualifiedType(). 1092 QualType getUnqualifiedArrayType(QualType T, Qualifiers &Quals); 1093 1094 /// \brief Determine whether the given types are equivalent after 1095 /// cvr-qualifiers have been removed. 1096 bool hasSameUnqualifiedType(QualType T1, QualType T2) { 1097 CanQualType CT1 = getCanonicalType(T1); 1098 CanQualType CT2 = getCanonicalType(T2); 1099 1100 Qualifiers Quals; 1101 QualType UnqualT1 = getUnqualifiedArrayType(CT1, Quals); 1102 QualType UnqualT2 = getUnqualifiedArrayType(CT2, Quals); 1103 return UnqualT1 == UnqualT2; 1104 } 1105 1106 bool UnwrapSimilarPointerTypes(QualType &T1, QualType &T2); 1107 1108 /// \brief Retrieves the "canonical" nested name specifier for a 1109 /// given nested name specifier. 1110 /// 1111 /// The canonical nested name specifier is a nested name specifier 1112 /// that uniquely identifies a type or namespace within the type 1113 /// system. For example, given: 1114 /// 1115 /// \code 1116 /// namespace N { 1117 /// struct S { 1118 /// template<typename T> struct X { typename T* type; }; 1119 /// }; 1120 /// } 1121 /// 1122 /// template<typename T> struct Y { 1123 /// typename N::S::X<T>::type member; 1124 /// }; 1125 /// \endcode 1126 /// 1127 /// Here, the nested-name-specifier for N::S::X<T>:: will be 1128 /// S::X<template-param-0-0>, since 'S' and 'X' are uniquely defined 1129 /// by declarations in the type system and the canonical type for 1130 /// the template type parameter 'T' is template-param-0-0. 1131 NestedNameSpecifier * 1132 getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS); 1133 1134 /// \brief Retrieves the default calling convention to use for 1135 /// C++ instance methods. 1136 CallingConv getDefaultMethodCallConv(); 1137 1138 /// \brief Retrieves the canonical representation of the given 1139 /// calling convention. 1140 CallingConv getCanonicalCallConv(CallingConv CC) { 1141 if (CC == CC_C) 1142 return CC_Default; 1143 return CC; 1144 } 1145 1146 /// \brief Determines whether two calling conventions name the same 1147 /// calling convention. 1148 bool isSameCallConv(CallingConv lcc, CallingConv rcc) { 1149 return (getCanonicalCallConv(lcc) == getCanonicalCallConv(rcc)); 1150 } 1151 1152 /// \brief Retrieves the "canonical" template name that refers to a 1153 /// given template. 1154 /// 1155 /// The canonical template name is the simplest expression that can 1156 /// be used to refer to a given template. For most templates, this 1157 /// expression is just the template declaration itself. For example, 1158 /// the template std::vector can be referred to via a variety of 1159 /// names---std::vector, ::std::vector, vector (if vector is in 1160 /// scope), etc.---but all of these names map down to the same 1161 /// TemplateDecl, which is used to form the canonical template name. 1162 /// 1163 /// Dependent template names are more interesting. Here, the 1164 /// template name could be something like T::template apply or 1165 /// std::allocator<T>::template rebind, where the nested name 1166 /// specifier itself is dependent. In this case, the canonical 1167 /// template name uses the shortest form of the dependent 1168 /// nested-name-specifier, which itself contains all canonical 1169 /// types, values, and templates. 1170 TemplateName getCanonicalTemplateName(TemplateName Name); 1171 1172 /// \brief Determine whether the given template names refer to the same 1173 /// template. 1174 bool hasSameTemplateName(TemplateName X, TemplateName Y); 1175 1176 /// \brief Retrieve the "canonical" template argument. 1177 /// 1178 /// The canonical template argument is the simplest template argument 1179 /// (which may be a type, value, expression, or declaration) that 1180 /// expresses the value of the argument. 1181 TemplateArgument getCanonicalTemplateArgument(const TemplateArgument &Arg); 1182 1183 /// Type Query functions. If the type is an instance of the specified class, 1184 /// return the Type pointer for the underlying maximally pretty type. This 1185 /// is a member of ASTContext because this may need to do some amount of 1186 /// canonicalization, e.g. to move type qualifiers into the element type. 1187 const ArrayType *getAsArrayType(QualType T); 1188 const ConstantArrayType *getAsConstantArrayType(QualType T) { 1189 return dyn_cast_or_null<ConstantArrayType>(getAsArrayType(T)); 1190 } 1191 const VariableArrayType *getAsVariableArrayType(QualType T) { 1192 return dyn_cast_or_null<VariableArrayType>(getAsArrayType(T)); 1193 } 1194 const IncompleteArrayType *getAsIncompleteArrayType(QualType T) { 1195 return dyn_cast_or_null<IncompleteArrayType>(getAsArrayType(T)); 1196 } 1197 const DependentSizedArrayType *getAsDependentSizedArrayType(QualType T) { 1198 return dyn_cast_or_null<DependentSizedArrayType>(getAsArrayType(T)); 1199 } 1200 1201 /// getBaseElementType - Returns the innermost element type of an array type. 1202 /// For example, will return "int" for int[m][n] 1203 QualType getBaseElementType(const ArrayType *VAT); 1204 1205 /// getBaseElementType - Returns the innermost element type of a type 1206 /// (which needn't actually be an array type). 1207 QualType getBaseElementType(QualType QT); 1208 1209 /// getConstantArrayElementCount - Returns number of constant array elements. 1210 uint64_t getConstantArrayElementCount(const ConstantArrayType *CA) const; 1211 1212 /// getArrayDecayedType - Return the properly qualified result of decaying the 1213 /// specified array type to a pointer. This operation is non-trivial when 1214 /// handling typedefs etc. The canonical type of "T" must be an array type, 1215 /// this returns a pointer to a properly qualified element of the array. 1216 /// 1217 /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. 1218 QualType getArrayDecayedType(QualType T); 1219 1220 /// getPromotedIntegerType - Returns the type that Promotable will 1221 /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable 1222 /// integer type. 1223 QualType getPromotedIntegerType(QualType PromotableType); 1224 1225 /// \brief Whether this is a promotable bitfield reference according 1226 /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). 1227 /// 1228 /// \returns the type this bit-field will promote to, or NULL if no 1229 /// promotion occurs. 1230 QualType isPromotableBitField(Expr *E); 1231 1232 /// getIntegerTypeOrder - Returns the highest ranked integer type: 1233 /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If 1234 /// LHS < RHS, return -1. 1235 int getIntegerTypeOrder(QualType LHS, QualType RHS); 1236 1237 /// getFloatingTypeOrder - Compare the rank of the two specified floating 1238 /// point types, ignoring the domain of the type (i.e. 'double' == 1239 /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If 1240 /// LHS < RHS, return -1. 1241 int getFloatingTypeOrder(QualType LHS, QualType RHS); 1242 1243 /// getFloatingTypeOfSizeWithinDomain - Returns a real floating 1244 /// point or a complex type (based on typeDomain/typeSize). 1245 /// 'typeDomain' is a real floating point or complex type. 1246 /// 'typeSize' is a real floating point or complex type. 1247 QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize, 1248 QualType typeDomain) const; 1249 1250private: 1251 // Helper for integer ordering 1252 unsigned getIntegerRank(Type* T); 1253 1254public: 1255 1256 //===--------------------------------------------------------------------===// 1257 // Type Compatibility Predicates 1258 //===--------------------------------------------------------------------===// 1259 1260 /// Compatibility predicates used to check assignment expressions. 1261 bool typesAreCompatible(QualType T1, QualType T2, 1262 bool CompareUnqualified = false); // C99 6.2.7p1 1263 1264 bool typesAreBlockPointerCompatible(QualType, QualType); 1265 1266 bool isObjCIdType(QualType T) const { 1267 return T == ObjCIdTypedefType; 1268 } 1269 bool isObjCClassType(QualType T) const { 1270 return T == ObjCClassTypedefType; 1271 } 1272 bool isObjCSelType(QualType T) const { 1273 return T == ObjCSelTypedefType; 1274 } 1275 bool QualifiedIdConformsQualifiedId(QualType LHS, QualType RHS); 1276 bool ObjCQualifiedIdTypesAreCompatible(QualType LHS, QualType RHS, 1277 bool ForCompare); 1278 1279 bool ObjCQualifiedClassTypesAreCompatible(QualType LHS, QualType RHS); 1280 1281 // Check the safety of assignment from LHS to RHS 1282 bool canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, 1283 const ObjCObjectPointerType *RHSOPT); 1284 bool canAssignObjCInterfaces(const ObjCObjectType *LHS, 1285 const ObjCObjectType *RHS); 1286 bool canAssignObjCInterfacesInBlockPointer( 1287 const ObjCObjectPointerType *LHSOPT, 1288 const ObjCObjectPointerType *RHSOPT); 1289 bool areComparableObjCPointerTypes(QualType LHS, QualType RHS); 1290 QualType areCommonBaseCompatible(const ObjCObjectPointerType *LHSOPT, 1291 const ObjCObjectPointerType *RHSOPT); 1292 bool canBindObjCObjectType(QualType To, QualType From); 1293 1294 // Functions for calculating composite types 1295 QualType mergeTypes(QualType, QualType, bool OfBlockPointer=false, 1296 bool Unqualified = false); 1297 QualType mergeFunctionTypes(QualType, QualType, bool OfBlockPointer=false, 1298 bool Unqualified = false); 1299 QualType mergeFunctionArgumentTypes(QualType, QualType, 1300 bool OfBlockPointer=false, 1301 bool Unqualified = false); 1302 QualType mergeTransparentUnionType(QualType, QualType, 1303 bool OfBlockPointer=false, 1304 bool Unqualified = false); 1305 1306 QualType mergeObjCGCQualifiers(QualType, QualType); 1307 1308 void ResetObjCLayout(const ObjCContainerDecl *CD) { 1309 ObjCLayouts[CD] = 0; 1310 } 1311 1312 //===--------------------------------------------------------------------===// 1313 // Integer Predicates 1314 //===--------------------------------------------------------------------===// 1315 1316 // The width of an integer, as defined in C99 6.2.6.2. This is the number 1317 // of bits in an integer type excluding any padding bits. 1318 unsigned getIntWidth(QualType T); 1319 1320 // Per C99 6.2.5p6, for every signed integer type, there is a corresponding 1321 // unsigned integer type. This method takes a signed type, and returns the 1322 // corresponding unsigned integer type. 1323 QualType getCorrespondingUnsignedType(QualType T); 1324 1325 //===--------------------------------------------------------------------===// 1326 // Type Iterators. 1327 //===--------------------------------------------------------------------===// 1328 1329 typedef std::vector<Type*>::iterator type_iterator; 1330 typedef std::vector<Type*>::const_iterator const_type_iterator; 1331 1332 type_iterator types_begin() { return Types.begin(); } 1333 type_iterator types_end() { return Types.end(); } 1334 const_type_iterator types_begin() const { return Types.begin(); } 1335 const_type_iterator types_end() const { return Types.end(); } 1336 1337 //===--------------------------------------------------------------------===// 1338 // Integer Values 1339 //===--------------------------------------------------------------------===// 1340 1341 /// MakeIntValue - Make an APSInt of the appropriate width and 1342 /// signedness for the given \arg Value and integer \arg Type. 1343 llvm::APSInt MakeIntValue(uint64_t Value, QualType Type) { 1344 llvm::APSInt Res(getIntWidth(Type), !Type->isSignedIntegerType()); 1345 Res = Value; 1346 return Res; 1347 } 1348 1349 /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. 1350 ObjCImplementationDecl *getObjCImplementation(ObjCInterfaceDecl *D); 1351 /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. 1352 ObjCCategoryImplDecl *getObjCImplementation(ObjCCategoryDecl *D); 1353 1354 /// \brief Set the implementation of ObjCInterfaceDecl. 1355 void setObjCImplementation(ObjCInterfaceDecl *IFaceD, 1356 ObjCImplementationDecl *ImplD); 1357 /// \brief Set the implementation of ObjCCategoryDecl. 1358 void setObjCImplementation(ObjCCategoryDecl *CatD, 1359 ObjCCategoryImplDecl *ImplD); 1360 1361 /// \brief Set the copy inialization expression of a block var decl. 1362 void setBlockVarCopyInits(VarDecl*VD, Expr* Init); 1363 /// \brief Get the copy initialization expression of VarDecl,or NULL if 1364 /// none exists. 1365 Expr *getBlockVarCopyInits(const VarDecl*VD); 1366 1367 /// \brief Allocate an uninitialized TypeSourceInfo. 1368 /// 1369 /// The caller should initialize the memory held by TypeSourceInfo using 1370 /// the TypeLoc wrappers. 1371 /// 1372 /// \param T the type that will be the basis for type source info. This type 1373 /// should refer to how the declarator was written in source code, not to 1374 /// what type semantic analysis resolved the declarator to. 1375 /// 1376 /// \param Size the size of the type info to create, or 0 if the size 1377 /// should be calculated based on the type. 1378 TypeSourceInfo *CreateTypeSourceInfo(QualType T, unsigned Size = 0); 1379 1380 /// \brief Allocate a TypeSourceInfo where all locations have been 1381 /// initialized to a given location, which defaults to the empty 1382 /// location. 1383 TypeSourceInfo * 1384 getTrivialTypeSourceInfo(QualType T, SourceLocation Loc = SourceLocation()); 1385 1386 TypeSourceInfo *getNullTypeSourceInfo() { return &NullTypeSourceInfo; } 1387 1388 /// \brief Add a deallocation callback that will be invoked when the 1389 /// ASTContext is destroyed. 1390 /// 1391 /// \brief Callback A callback function that will be invoked on destruction. 1392 /// 1393 /// \brief Data Pointer data that will be provided to the callback function 1394 /// when it is called. 1395 void AddDeallocation(void (*Callback)(void*), void *Data); 1396 1397 GVALinkage GetGVALinkageForFunction(const FunctionDecl *FD); 1398 GVALinkage GetGVALinkageForVariable(const VarDecl *VD); 1399 1400 /// \brief Determines if the decl can be CodeGen'ed or deserialized from PCH 1401 /// lazily, only when used; this is only relevant for function or file scoped 1402 /// var definitions. 1403 /// 1404 /// \returns true if the function/var must be CodeGen'ed/deserialized even if 1405 /// it is not used. 1406 bool DeclMustBeEmitted(const Decl *D); 1407 1408 //===--------------------------------------------------------------------===// 1409 // Statistics 1410 //===--------------------------------------------------------------------===// 1411 1412 /// \brief The number of implicitly-declared default constructors. 1413 static unsigned NumImplicitDefaultConstructors; 1414 1415 /// \brief The number of implicitly-declared default constructors for 1416 /// which declarations were built. 1417 static unsigned NumImplicitDefaultConstructorsDeclared; 1418 1419 /// \brief The number of implicitly-declared copy constructors. 1420 static unsigned NumImplicitCopyConstructors; 1421 1422 /// \brief The number of implicitly-declared copy constructors for 1423 /// which declarations were built. 1424 static unsigned NumImplicitCopyConstructorsDeclared; 1425 1426 /// \brief The number of implicitly-declared copy assignment operators. 1427 static unsigned NumImplicitCopyAssignmentOperators; 1428 1429 /// \brief The number of implicitly-declared copy assignment operators for 1430 /// which declarations were built. 1431 static unsigned NumImplicitCopyAssignmentOperatorsDeclared; 1432 1433 /// \brief The number of implicitly-declared destructors. 1434 static unsigned NumImplicitDestructors; 1435 1436 /// \brief The number of implicitly-declared destructors for which 1437 /// declarations were built. 1438 static unsigned NumImplicitDestructorsDeclared; 1439 1440private: 1441 ASTContext(const ASTContext&); // DO NOT IMPLEMENT 1442 void operator=(const ASTContext&); // DO NOT IMPLEMENT 1443 1444 void InitBuiltinTypes(); 1445 void InitBuiltinType(CanQualType &R, BuiltinType::Kind K); 1446 1447 // Return the ObjC type encoding for a given type. 1448 void getObjCEncodingForTypeImpl(QualType t, std::string &S, 1449 bool ExpandPointedToStructures, 1450 bool ExpandStructures, 1451 const FieldDecl *Field, 1452 bool OutermostType = false, 1453 bool EncodingProperty = false); 1454 1455 const ASTRecordLayout &getObjCLayout(const ObjCInterfaceDecl *D, 1456 const ObjCImplementationDecl *Impl); 1457 1458private: 1459 /// \brief A set of deallocations that should be performed when the 1460 /// ASTContext is destroyed. 1461 llvm::SmallVector<std::pair<void (*)(void*), void *>, 16> Deallocations; 1462 1463 // FIXME: This currently contains the set of StoredDeclMaps used 1464 // by DeclContext objects. This probably should not be in ASTContext, 1465 // but we include it here so that ASTContext can quickly deallocate them. 1466 llvm::PointerIntPair<StoredDeclsMap*,1> LastSDM; 1467 1468 /// \brief A counter used to uniquely identify "blocks". 1469 unsigned int UniqueBlockByRefTypeID; 1470 unsigned int UniqueBlockParmTypeID; 1471 1472 friend class DeclContext; 1473 friend class DeclarationNameTable; 1474 void ReleaseDeclContextMaps(); 1475}; 1476 1477/// @brief Utility function for constructing a nullary selector. 1478static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) { 1479 IdentifierInfo* II = &Ctx.Idents.get(name); 1480 return Ctx.Selectors.getSelector(0, &II); 1481} 1482 1483/// @brief Utility function for constructing an unary selector. 1484static inline Selector GetUnarySelector(const char* name, ASTContext& Ctx) { 1485 IdentifierInfo* II = &Ctx.Idents.get(name); 1486 return Ctx.Selectors.getSelector(1, &II); 1487} 1488 1489} // end namespace clang 1490 1491// operator new and delete aren't allowed inside namespaces. 1492// The throw specifications are mandated by the standard. 1493/// @brief Placement new for using the ASTContext's allocator. 1494/// 1495/// This placement form of operator new uses the ASTContext's allocator for 1496/// obtaining memory. It is a non-throwing new, which means that it returns 1497/// null on error. (If that is what the allocator does. The current does, so if 1498/// this ever changes, this operator will have to be changed, too.) 1499/// Usage looks like this (assuming there's an ASTContext 'Context' in scope): 1500/// @code 1501/// // Default alignment (8) 1502/// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments); 1503/// // Specific alignment 1504/// IntegerLiteral *Ex2 = new (Context, 4) IntegerLiteral(arguments); 1505/// @endcode 1506/// Please note that you cannot use delete on the pointer; it must be 1507/// deallocated using an explicit destructor call followed by 1508/// @c Context.Deallocate(Ptr). 1509/// 1510/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 1511/// @param C The ASTContext that provides the allocator. 1512/// @param Alignment The alignment of the allocated memory (if the underlying 1513/// allocator supports it). 1514/// @return The allocated memory. Could be NULL. 1515inline void *operator new(size_t Bytes, clang::ASTContext &C, 1516 size_t Alignment) throw () { 1517 return C.Allocate(Bytes, Alignment); 1518} 1519/// @brief Placement delete companion to the new above. 1520/// 1521/// This operator is just a companion to the new above. There is no way of 1522/// invoking it directly; see the new operator for more details. This operator 1523/// is called implicitly by the compiler if a placement new expression using 1524/// the ASTContext throws in the object constructor. 1525inline void operator delete(void *Ptr, clang::ASTContext &C, size_t) 1526 throw () { 1527 C.Deallocate(Ptr); 1528} 1529 1530/// This placement form of operator new[] uses the ASTContext's allocator for 1531/// obtaining memory. It is a non-throwing new[], which means that it returns 1532/// null on error. 1533/// Usage looks like this (assuming there's an ASTContext 'Context' in scope): 1534/// @code 1535/// // Default alignment (8) 1536/// char *data = new (Context) char[10]; 1537/// // Specific alignment 1538/// char *data = new (Context, 4) char[10]; 1539/// @endcode 1540/// Please note that you cannot use delete on the pointer; it must be 1541/// deallocated using an explicit destructor call followed by 1542/// @c Context.Deallocate(Ptr). 1543/// 1544/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 1545/// @param C The ASTContext that provides the allocator. 1546/// @param Alignment The alignment of the allocated memory (if the underlying 1547/// allocator supports it). 1548/// @return The allocated memory. Could be NULL. 1549inline void *operator new[](size_t Bytes, clang::ASTContext& C, 1550 size_t Alignment = 8) throw () { 1551 return C.Allocate(Bytes, Alignment); 1552} 1553 1554/// @brief Placement delete[] companion to the new[] above. 1555/// 1556/// This operator is just a companion to the new[] above. There is no way of 1557/// invoking it directly; see the new[] operator for more details. This operator 1558/// is called implicitly by the compiler if a placement new[] expression using 1559/// the ASTContext throws in the object constructor. 1560inline void operator delete[](void *Ptr, clang::ASTContext &C, size_t) 1561 throw () { 1562 C.Deallocate(Ptr); 1563} 1564 1565#endif 1566