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