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