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