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