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