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