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