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