ASTContext.h revision 6a6aa53ec6c89ae0881309b0a0dd84b6868b576b
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, 559 CallingConv CallConv); 560 QualType getFunctionNoProtoType(QualType ResultTy) { 561 return getFunctionNoProtoType(ResultTy, false, CC_Default); 562 } 563 564 /// getFunctionType - Return a normal function type with a typed argument 565 /// list. isVariadic indicates whether the argument list includes '...'. 566 QualType getFunctionType(QualType ResultTy, const QualType *ArgArray, 567 unsigned NumArgs, bool isVariadic, 568 unsigned TypeQuals, bool hasExceptionSpec, 569 bool hasAnyExceptionSpec, 570 unsigned NumExs, const QualType *ExArray, 571 bool NoReturn, 572 CallingConv CallConv); 573 574 /// getTypeDeclType - Return the unique reference to the type for 575 /// the specified type declaration. 576 QualType getTypeDeclType(const TypeDecl *Decl, 577 const TypeDecl *PrevDecl = 0) { 578 assert(Decl && "Passed null for Decl param"); 579 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); 580 581 if (PrevDecl) { 582 assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl"); 583 Decl->TypeForDecl = PrevDecl->TypeForDecl; 584 return QualType(PrevDecl->TypeForDecl, 0); 585 } 586 587 return getTypeDeclTypeSlow(Decl); 588 } 589 590 /// getTypedefType - Return the unique reference to the type for the 591 /// specified typename decl. 592 QualType getTypedefType(const TypedefDecl *Decl); 593 594 QualType getInjectedClassNameType(CXXRecordDecl *Decl, QualType TST); 595 596 QualType getSubstTemplateTypeParmType(const TemplateTypeParmType *Replaced, 597 QualType Replacement); 598 599 QualType getTemplateTypeParmType(unsigned Depth, unsigned Index, 600 bool ParameterPack, 601 IdentifierInfo *Name = 0); 602 603 QualType getTemplateSpecializationType(TemplateName T, 604 const TemplateArgument *Args, 605 unsigned NumArgs, 606 QualType Canon = QualType()); 607 608 QualType getTemplateSpecializationType(TemplateName T, 609 const TemplateArgumentListInfo &Args, 610 QualType Canon = QualType()); 611 612 TypeSourceInfo * 613 getTemplateSpecializationTypeInfo(TemplateName T, SourceLocation TLoc, 614 const TemplateArgumentListInfo &Args, 615 QualType Canon = QualType()); 616 617 QualType getQualifiedNameType(NestedNameSpecifier *NNS, 618 QualType NamedType); 619 QualType getTypenameType(NestedNameSpecifier *NNS, 620 const IdentifierInfo *Name, 621 QualType Canon = QualType()); 622 QualType getTypenameType(NestedNameSpecifier *NNS, 623 const TemplateSpecializationType *TemplateId, 624 QualType Canon = QualType()); 625 QualType getElaboratedType(QualType UnderlyingType, 626 ElaboratedType::TagKind Tag); 627 628 QualType getObjCInterfaceType(const ObjCInterfaceDecl *Decl, 629 ObjCProtocolDecl **Protocols = 0, 630 unsigned NumProtocols = 0); 631 632 /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for the 633 /// given interface decl and the conforming protocol list. 634 QualType getObjCObjectPointerType(QualType OIT, 635 ObjCProtocolDecl **ProtocolList = 0, 636 unsigned NumProtocols = 0, 637 unsigned Quals = 0); 638 639 /// getTypeOfType - GCC extension. 640 QualType getTypeOfExprType(Expr *e); 641 QualType getTypeOfType(QualType t); 642 643 /// getDecltypeType - C++0x decltype. 644 QualType getDecltypeType(Expr *e); 645 646 /// getTagDeclType - Return the unique reference to the type for the 647 /// specified TagDecl (struct/union/class/enum) decl. 648 QualType getTagDeclType(const TagDecl *Decl); 649 650 /// getSizeType - Return the unique type for "size_t" (C99 7.17), defined 651 /// in <stddef.h>. The sizeof operator requires this (C99 6.5.3.4p4). 652 CanQualType getSizeType() const; 653 654 /// getWCharType - In C++, this returns the unique wchar_t type. In C99, this 655 /// returns a type compatible with the type defined in <stddef.h> as defined 656 /// by the target. 657 QualType getWCharType() const { return WCharTy; } 658 659 /// getSignedWCharType - Return the type of "signed wchar_t". 660 /// Used when in C++, as a GCC extension. 661 QualType getSignedWCharType() const; 662 663 /// getUnsignedWCharType - Return the type of "unsigned wchar_t". 664 /// Used when in C++, as a GCC extension. 665 QualType getUnsignedWCharType() const; 666 667 /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) 668 /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). 669 QualType getPointerDiffType() const; 670 671 // getCFConstantStringType - Return the C structure type used to represent 672 // constant CFStrings. 673 QualType getCFConstantStringType(); 674 675 /// Get the structure type used to representation CFStrings, or NULL 676 /// if it hasn't yet been built. 677 QualType getRawCFConstantStringType() { 678 if (CFConstantStringTypeDecl) 679 return getTagDeclType(CFConstantStringTypeDecl); 680 return QualType(); 681 } 682 void setCFConstantStringType(QualType T); 683 684 // This setter/getter represents the ObjC type for an NSConstantString. 685 void setObjCConstantStringInterface(ObjCInterfaceDecl *Decl); 686 QualType getObjCConstantStringInterface() const { 687 return ObjCConstantStringType; 688 } 689 690 //// This gets the struct used to keep track of fast enumerations. 691 QualType getObjCFastEnumerationStateType(); 692 693 /// Get the ObjCFastEnumerationState type, or NULL if it hasn't yet 694 /// been built. 695 QualType getRawObjCFastEnumerationStateType() { 696 if (ObjCFastEnumerationStateTypeDecl) 697 return getTagDeclType(ObjCFastEnumerationStateTypeDecl); 698 return QualType(); 699 } 700 701 void setObjCFastEnumerationStateType(QualType T); 702 703 /// \brief Set the type for the C FILE type. 704 void setFILEDecl(TypeDecl *FILEDecl) { this->FILEDecl = FILEDecl; } 705 706 /// \brief Retrieve the C FILE type. 707 QualType getFILEType() { 708 if (FILEDecl) 709 return getTypeDeclType(FILEDecl); 710 return QualType(); 711 } 712 713 /// \brief Set the type for the C jmp_buf type. 714 void setjmp_bufDecl(TypeDecl *jmp_bufDecl) { 715 this->jmp_bufDecl = jmp_bufDecl; 716 } 717 718 /// \brief Retrieve the C jmp_buf type. 719 QualType getjmp_bufType() { 720 if (jmp_bufDecl) 721 return getTypeDeclType(jmp_bufDecl); 722 return QualType(); 723 } 724 725 /// \brief Set the type for the C sigjmp_buf type. 726 void setsigjmp_bufDecl(TypeDecl *sigjmp_bufDecl) { 727 this->sigjmp_bufDecl = sigjmp_bufDecl; 728 } 729 730 /// \brief Retrieve the C sigjmp_buf type. 731 QualType getsigjmp_bufType() { 732 if (sigjmp_bufDecl) 733 return getTypeDeclType(sigjmp_bufDecl); 734 return QualType(); 735 } 736 737 /// getObjCEncodingForType - Emit the ObjC type encoding for the 738 /// given type into \arg S. If \arg NameFields is specified then 739 /// record field names are also encoded. 740 void getObjCEncodingForType(QualType t, std::string &S, 741 const FieldDecl *Field=0); 742 743 void getLegacyIntegralTypeEncoding(QualType &t) const; 744 745 // Put the string version of type qualifiers into S. 746 void getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, 747 std::string &S) const; 748 749 /// getObjCEncodingForMethodDecl - Return the encoded type for this method 750 /// declaration. 751 void getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, std::string &S); 752 753 /// getObjCEncodingForBlockDecl - Return the encoded type for this block 754 /// declaration. 755 void getObjCEncodingForBlock(const BlockExpr *Expr, std::string& S); 756 757 /// getObjCEncodingForPropertyDecl - Return the encoded type for 758 /// this method declaration. If non-NULL, Container must be either 759 /// an ObjCCategoryImplDecl or ObjCImplementationDecl; it should 760 /// only be NULL when getting encodings for protocol properties. 761 void getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, 762 const Decl *Container, 763 std::string &S); 764 765 bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, 766 ObjCProtocolDecl *rProto); 767 768 /// getObjCEncodingTypeSize returns size of type for objective-c encoding 769 /// purpose in characters. 770 CharUnits getObjCEncodingTypeSize(QualType t); 771 772 /// This setter/getter represents the ObjC 'id' type. It is setup lazily, by 773 /// Sema. id is always a (typedef for a) pointer type, a pointer to a struct. 774 QualType getObjCIdType() const { return ObjCIdTypedefType; } 775 void setObjCIdType(QualType T); 776 777 void setObjCSelType(QualType T); 778 QualType getObjCSelType() const { return ObjCSelTypedefType; } 779 780 void setObjCProtoType(QualType QT); 781 QualType getObjCProtoType() const { return ObjCProtoType; } 782 783 /// This setter/getter repreents the ObjC 'Class' type. It is setup lazily, by 784 /// Sema. 'Class' is always a (typedef for a) pointer type, a pointer to a 785 /// struct. 786 QualType getObjCClassType() const { return ObjCClassTypedefType; } 787 void setObjCClassType(QualType T); 788 789 void setBuiltinVaListType(QualType T); 790 QualType getBuiltinVaListType() const { return BuiltinVaListType; } 791 792 /// getCVRQualifiedType - Returns a type with additional const, 793 /// volatile, or restrict qualifiers. 794 QualType getCVRQualifiedType(QualType T, unsigned CVR) { 795 return getQualifiedType(T, Qualifiers::fromCVRMask(CVR)); 796 } 797 798 /// getQualifiedType - Returns a type with additional qualifiers. 799 QualType getQualifiedType(QualType T, Qualifiers Qs) { 800 if (!Qs.hasNonFastQualifiers()) 801 return T.withFastQualifiers(Qs.getFastQualifiers()); 802 QualifierCollector Qc(Qs); 803 const Type *Ptr = Qc.strip(T); 804 return getExtQualType(Ptr, Qc); 805 } 806 807 /// getQualifiedType - Returns a type with additional qualifiers. 808 QualType getQualifiedType(const Type *T, Qualifiers Qs) { 809 if (!Qs.hasNonFastQualifiers()) 810 return QualType(T, Qs.getFastQualifiers()); 811 return getExtQualType(T, Qs); 812 } 813 814 DeclarationName getNameForTemplate(TemplateName Name); 815 816 TemplateName getOverloadedTemplateName(UnresolvedSetIterator Begin, 817 UnresolvedSetIterator End); 818 819 TemplateName getQualifiedTemplateName(NestedNameSpecifier *NNS, 820 bool TemplateKeyword, 821 TemplateDecl *Template); 822 823 TemplateName getDependentTemplateName(NestedNameSpecifier *NNS, 824 const IdentifierInfo *Name); 825 TemplateName getDependentTemplateName(NestedNameSpecifier *NNS, 826 OverloadedOperatorKind Operator); 827 828 enum GetBuiltinTypeError { 829 GE_None, //< No error 830 GE_Missing_stdio, //< Missing a type from <stdio.h> 831 GE_Missing_setjmp //< Missing a type from <setjmp.h> 832 }; 833 834 /// GetBuiltinType - Return the type for the specified builtin. 835 QualType GetBuiltinType(unsigned ID, GetBuiltinTypeError &Error); 836 837private: 838 CanQualType getFromTargetType(unsigned Type) const; 839 840 //===--------------------------------------------------------------------===// 841 // Type Predicates. 842 //===--------------------------------------------------------------------===// 843 844public: 845 /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's 846 /// garbage collection attribute. 847 /// 848 Qualifiers::GC getObjCGCAttrKind(const QualType &Ty) const; 849 850 /// isObjCNSObjectType - Return true if this is an NSObject object with 851 /// its NSObject attribute set. 852 bool isObjCNSObjectType(QualType Ty) const; 853 854 //===--------------------------------------------------------------------===// 855 // Type Sizing and Analysis 856 //===--------------------------------------------------------------------===// 857 858 /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified 859 /// scalar floating point type. 860 const llvm::fltSemantics &getFloatTypeSemantics(QualType T) const; 861 862 /// getTypeInfo - Get the size and alignment of the specified complete type in 863 /// bits. 864 std::pair<uint64_t, unsigned> getTypeInfo(const Type *T); 865 std::pair<uint64_t, unsigned> getTypeInfo(QualType T) { 866 return getTypeInfo(T.getTypePtr()); 867 } 868 869 /// getTypeSize - Return the size of the specified type, in bits. This method 870 /// does not work on incomplete types. 871 uint64_t getTypeSize(QualType T) { 872 return getTypeInfo(T).first; 873 } 874 uint64_t getTypeSize(const Type *T) { 875 return getTypeInfo(T).first; 876 } 877 878 /// getCharWidth - Return the size of the character type, in bits 879 uint64_t getCharWidth() { 880 return getTypeSize(CharTy); 881 } 882 883 /// getTypeSizeInChars - Return the size of the specified type, in characters. 884 /// This method does not work on incomplete types. 885 CharUnits getTypeSizeInChars(QualType T); 886 CharUnits getTypeSizeInChars(const Type *T); 887 888 /// getTypeAlign - Return the ABI-specified alignment of a type, in bits. 889 /// This method does not work on incomplete types. 890 unsigned getTypeAlign(QualType T) { 891 return getTypeInfo(T).second; 892 } 893 unsigned getTypeAlign(const Type *T) { 894 return getTypeInfo(T).second; 895 } 896 897 /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in 898 /// characters. This method does not work on incomplete types. 899 CharUnits getTypeAlignInChars(QualType T); 900 CharUnits getTypeAlignInChars(const Type *T); 901 902 /// getPreferredTypeAlign - Return the "preferred" alignment of the specified 903 /// type for the current target in bits. This can be different than the ABI 904 /// alignment in cases where it is beneficial for performance to overalign 905 /// a data type. 906 unsigned getPreferredTypeAlign(const Type *T); 907 908 /// getDeclAlign - Return a conservative estimate of the alignment of 909 /// the specified decl. Note that bitfields do not have a valid alignment, so 910 /// this method will assert on them. 911 /// If @p RefAsPointee, references are treated like their underlying type 912 /// (for alignof), else they're treated like pointers (for CodeGen). 913 CharUnits getDeclAlign(const Decl *D, bool RefAsPointee = false); 914 915 /// getASTRecordLayout - Get or compute information about the layout of the 916 /// specified record (struct/union/class), which indicates its size and field 917 /// position information. 918 const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D); 919 920 /// getASTObjCInterfaceLayout - Get or compute information about the 921 /// layout of the specified Objective-C interface. 922 const ASTRecordLayout &getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D); 923 924 /// getASTObjCImplementationLayout - Get or compute information about 925 /// the layout of the specified Objective-C implementation. This may 926 /// differ from the interface if synthesized ivars are present. 927 const ASTRecordLayout & 928 getASTObjCImplementationLayout(const ObjCImplementationDecl *D); 929 930 /// getKeyFunction - Get the key function for the given record decl. 931 /// The key function is, according to the Itanium C++ ABI section 5.2.3: 932 /// 933 /// ...the first non-pure virtual function that is not inline at the point 934 /// of class definition. 935 const CXXMethodDecl *getKeyFunction(const CXXRecordDecl *RD); 936 937 void CollectObjCIvars(const ObjCInterfaceDecl *OI, 938 llvm::SmallVectorImpl<FieldDecl*> &Fields); 939 940 void ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI, 941 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars); 942 void CollectNonClassIvars(const ObjCInterfaceDecl *OI, 943 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars); 944 unsigned CountNonClassIvars(const ObjCInterfaceDecl *OI); 945 void CollectInheritedProtocols(const Decl *CDecl, 946 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols); 947 948 //===--------------------------------------------------------------------===// 949 // Type Operators 950 //===--------------------------------------------------------------------===// 951 952 /// getCanonicalType - Return the canonical (structural) type corresponding to 953 /// the specified potentially non-canonical type. The non-canonical version 954 /// of a type may have many "decorated" versions of types. Decorators can 955 /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed 956 /// to be free of any of these, allowing two canonical types to be compared 957 /// for exact equality with a simple pointer comparison. 958 CanQualType getCanonicalType(QualType T); 959 const Type *getCanonicalType(const Type *T) { 960 return T->getCanonicalTypeInternal().getTypePtr(); 961 } 962 963 /// getCanonicalParamType - Return the canonical parameter type 964 /// corresponding to the specific potentially non-canonical one. 965 /// Qualifiers are stripped off, functions are turned into function 966 /// pointers, and arrays decay one level into pointers. 967 CanQualType getCanonicalParamType(QualType T); 968 969 /// \brief Determine whether the given types are equivalent. 970 bool hasSameType(QualType T1, QualType T2) { 971 return getCanonicalType(T1) == getCanonicalType(T2); 972 } 973 974 /// \brief Returns this type as a completely-unqualified array type, 975 /// capturing the qualifiers in Quals. This will remove the minimal amount of 976 /// sugaring from the types, similar to the behavior of 977 /// QualType::getUnqualifiedType(). 978 /// 979 /// \param T is the qualified type, which may be an ArrayType 980 /// 981 /// \param Quals will receive the full set of qualifiers that were 982 /// applied to the array. 983 /// 984 /// \returns if this is an array type, the completely unqualified array type 985 /// that corresponds to it. Otherwise, returns T.getUnqualifiedType(). 986 QualType getUnqualifiedArrayType(QualType T, Qualifiers &Quals); 987 988 /// \brief Determine whether the given types are equivalent after 989 /// cvr-qualifiers have been removed. 990 bool hasSameUnqualifiedType(QualType T1, QualType T2) { 991 CanQualType CT1 = getCanonicalType(T1); 992 CanQualType CT2 = getCanonicalType(T2); 993 994 Qualifiers Quals; 995 QualType UnqualT1 = getUnqualifiedArrayType(CT1, Quals); 996 QualType UnqualT2 = getUnqualifiedArrayType(CT2, Quals); 997 return UnqualT1 == UnqualT2; 998 } 999 1000 /// \brief Retrieves the "canonical" declaration of 1001 1002 /// \brief Retrieves the "canonical" nested name specifier for a 1003 /// given nested name specifier. 1004 /// 1005 /// The canonical nested name specifier is a nested name specifier 1006 /// that uniquely identifies a type or namespace within the type 1007 /// system. For example, given: 1008 /// 1009 /// \code 1010 /// namespace N { 1011 /// struct S { 1012 /// template<typename T> struct X { typename T* type; }; 1013 /// }; 1014 /// } 1015 /// 1016 /// template<typename T> struct Y { 1017 /// typename N::S::X<T>::type member; 1018 /// }; 1019 /// \endcode 1020 /// 1021 /// Here, the nested-name-specifier for N::S::X<T>:: will be 1022 /// S::X<template-param-0-0>, since 'S' and 'X' are uniquely defined 1023 /// by declarations in the type system and the canonical type for 1024 /// the template type parameter 'T' is template-param-0-0. 1025 NestedNameSpecifier * 1026 getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS); 1027 1028 /// \brief Retrieves the canonical representation of the given 1029 /// calling convention. 1030 CallingConv getCanonicalCallConv(CallingConv CC) { 1031 if (CC == CC_C) 1032 return CC_Default; 1033 return CC; 1034 } 1035 1036 /// \brief Determines whether two calling conventions name the same 1037 /// calling convention. 1038 bool isSameCallConv(CallingConv lcc, CallingConv rcc) { 1039 return (getCanonicalCallConv(lcc) == getCanonicalCallConv(rcc)); 1040 } 1041 1042 /// \brief Retrieves the "canonical" template name that refers to a 1043 /// given template. 1044 /// 1045 /// The canonical template name is the simplest expression that can 1046 /// be used to refer to a given template. For most templates, this 1047 /// expression is just the template declaration itself. For example, 1048 /// the template std::vector can be referred to via a variety of 1049 /// names---std::vector, ::std::vector, vector (if vector is in 1050 /// scope), etc.---but all of these names map down to the same 1051 /// TemplateDecl, which is used to form the canonical template name. 1052 /// 1053 /// Dependent template names are more interesting. Here, the 1054 /// template name could be something like T::template apply or 1055 /// std::allocator<T>::template rebind, where the nested name 1056 /// specifier itself is dependent. In this case, the canonical 1057 /// template name uses the shortest form of the dependent 1058 /// nested-name-specifier, which itself contains all canonical 1059 /// types, values, and templates. 1060 TemplateName getCanonicalTemplateName(TemplateName Name); 1061 1062 /// \brief Determine whether the given template names refer to the same 1063 /// template. 1064 bool hasSameTemplateName(TemplateName X, TemplateName Y); 1065 1066 /// \brief Retrieve the "canonical" template argument. 1067 /// 1068 /// The canonical template argument is the simplest template argument 1069 /// (which may be a type, value, expression, or declaration) that 1070 /// expresses the value of the argument. 1071 TemplateArgument getCanonicalTemplateArgument(const TemplateArgument &Arg); 1072 1073 /// Type Query functions. If the type is an instance of the specified class, 1074 /// return the Type pointer for the underlying maximally pretty type. This 1075 /// is a member of ASTContext because this may need to do some amount of 1076 /// canonicalization, e.g. to move type qualifiers into the element type. 1077 const ArrayType *getAsArrayType(QualType T); 1078 const ConstantArrayType *getAsConstantArrayType(QualType T) { 1079 return dyn_cast_or_null<ConstantArrayType>(getAsArrayType(T)); 1080 } 1081 const VariableArrayType *getAsVariableArrayType(QualType T) { 1082 return dyn_cast_or_null<VariableArrayType>(getAsArrayType(T)); 1083 } 1084 const IncompleteArrayType *getAsIncompleteArrayType(QualType T) { 1085 return dyn_cast_or_null<IncompleteArrayType>(getAsArrayType(T)); 1086 } 1087 const DependentSizedArrayType *getAsDependentSizedArrayType(QualType T) { 1088 return dyn_cast_or_null<DependentSizedArrayType>(getAsArrayType(T)); 1089 } 1090 1091 /// getBaseElementType - Returns the innermost element type of an array type. 1092 /// For example, will return "int" for int[m][n] 1093 QualType getBaseElementType(const ArrayType *VAT); 1094 1095 /// getBaseElementType - Returns the innermost element type of a type 1096 /// (which needn't actually be an array type). 1097 QualType getBaseElementType(QualType QT); 1098 1099 /// getConstantArrayElementCount - Returns number of constant array elements. 1100 uint64_t getConstantArrayElementCount(const ConstantArrayType *CA) const; 1101 1102 /// getArrayDecayedType - Return the properly qualified result of decaying the 1103 /// specified array type to a pointer. This operation is non-trivial when 1104 /// handling typedefs etc. The canonical type of "T" must be an array type, 1105 /// this returns a pointer to a properly qualified element of the array. 1106 /// 1107 /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. 1108 QualType getArrayDecayedType(QualType T); 1109 1110 /// getPromotedIntegerType - Returns the type that Promotable will 1111 /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable 1112 /// integer type. 1113 QualType getPromotedIntegerType(QualType PromotableType); 1114 1115 /// \brief Whether this is a promotable bitfield reference according 1116 /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). 1117 /// 1118 /// \returns the type this bit-field will promote to, or NULL if no 1119 /// promotion occurs. 1120 QualType isPromotableBitField(Expr *E); 1121 1122 /// getIntegerTypeOrder - Returns the highest ranked integer type: 1123 /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If 1124 /// LHS < RHS, return -1. 1125 int getIntegerTypeOrder(QualType LHS, QualType RHS); 1126 1127 /// getFloatingTypeOrder - Compare the rank of the two specified floating 1128 /// point types, ignoring the domain of the type (i.e. 'double' == 1129 /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If 1130 /// LHS < RHS, return -1. 1131 int getFloatingTypeOrder(QualType LHS, QualType RHS); 1132 1133 /// getFloatingTypeOfSizeWithinDomain - Returns a real floating 1134 /// point or a complex type (based on typeDomain/typeSize). 1135 /// 'typeDomain' is a real floating point or complex type. 1136 /// 'typeSize' is a real floating point or complex type. 1137 QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize, 1138 QualType typeDomain) const; 1139 1140private: 1141 // Helper for integer ordering 1142 unsigned getIntegerRank(Type* T); 1143 1144public: 1145 1146 //===--------------------------------------------------------------------===// 1147 // Type Compatibility Predicates 1148 //===--------------------------------------------------------------------===// 1149 1150 /// Compatibility predicates used to check assignment expressions. 1151 bool typesAreCompatible(QualType, QualType); // C99 6.2.7p1 1152 1153 bool typesAreBlockPointerCompatible(QualType, QualType); 1154 1155 bool isObjCIdType(QualType T) const { 1156 return T == ObjCIdTypedefType; 1157 } 1158 bool isObjCClassType(QualType T) const { 1159 return T == ObjCClassTypedefType; 1160 } 1161 bool isObjCSelType(QualType T) const { 1162 return T == ObjCSelTypedefType; 1163 } 1164 bool QualifiedIdConformsQualifiedId(QualType LHS, QualType RHS); 1165 bool ObjCQualifiedIdTypesAreCompatible(QualType LHS, QualType RHS, 1166 bool ForCompare); 1167 1168 // Check the safety of assignment from LHS to RHS 1169 bool canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, 1170 const ObjCObjectPointerType *RHSOPT); 1171 bool canAssignObjCInterfaces(const ObjCInterfaceType *LHS, 1172 const ObjCInterfaceType *RHS); 1173 bool canAssignObjCInterfacesInBlockPointer( 1174 const ObjCObjectPointerType *LHSOPT, 1175 const ObjCObjectPointerType *RHSOPT); 1176 bool areComparableObjCPointerTypes(QualType LHS, QualType RHS); 1177 QualType areCommonBaseCompatible(const ObjCObjectPointerType *LHSOPT, 1178 const ObjCObjectPointerType *RHSOPT); 1179 1180 // Functions for calculating composite types 1181 QualType mergeTypes(QualType, QualType, bool OfBlockPointer=false); 1182 QualType mergeFunctionTypes(QualType, QualType, bool OfBlockPointer=false); 1183 1184 /// UsualArithmeticConversionsType - handles the various conversions 1185 /// that are common to binary operators (C99 6.3.1.8, C++ [expr]p9) 1186 /// and returns the result type of that conversion. 1187 QualType UsualArithmeticConversionsType(QualType lhs, QualType rhs); 1188 1189 //===--------------------------------------------------------------------===// 1190 // Integer Predicates 1191 //===--------------------------------------------------------------------===// 1192 1193 // The width of an integer, as defined in C99 6.2.6.2. This is the number 1194 // of bits in an integer type excluding any padding bits. 1195 unsigned getIntWidth(QualType T); 1196 1197 // Per C99 6.2.5p6, for every signed integer type, there is a corresponding 1198 // unsigned integer type. This method takes a signed type, and returns the 1199 // corresponding unsigned integer type. 1200 QualType getCorrespondingUnsignedType(QualType T); 1201 1202 //===--------------------------------------------------------------------===// 1203 // Type Iterators. 1204 //===--------------------------------------------------------------------===// 1205 1206 typedef std::vector<Type*>::iterator type_iterator; 1207 typedef std::vector<Type*>::const_iterator const_type_iterator; 1208 1209 type_iterator types_begin() { return Types.begin(); } 1210 type_iterator types_end() { return Types.end(); } 1211 const_type_iterator types_begin() const { return Types.begin(); } 1212 const_type_iterator types_end() const { return Types.end(); } 1213 1214 //===--------------------------------------------------------------------===// 1215 // Integer Values 1216 //===--------------------------------------------------------------------===// 1217 1218 /// MakeIntValue - Make an APSInt of the appropriate width and 1219 /// signedness for the given \arg Value and integer \arg Type. 1220 llvm::APSInt MakeIntValue(uint64_t Value, QualType Type) { 1221 llvm::APSInt Res(getIntWidth(Type), !Type->isSignedIntegerType()); 1222 Res = Value; 1223 return Res; 1224 } 1225 1226 /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. 1227 ObjCImplementationDecl *getObjCImplementation(ObjCInterfaceDecl *D); 1228 /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. 1229 ObjCCategoryImplDecl *getObjCImplementation(ObjCCategoryDecl *D); 1230 1231 /// \brief Set the implementation of ObjCInterfaceDecl. 1232 void setObjCImplementation(ObjCInterfaceDecl *IFaceD, 1233 ObjCImplementationDecl *ImplD); 1234 /// \brief Set the implementation of ObjCCategoryDecl. 1235 void setObjCImplementation(ObjCCategoryDecl *CatD, 1236 ObjCCategoryImplDecl *ImplD); 1237 1238 /// \brief Allocate an uninitialized TypeSourceInfo. 1239 /// 1240 /// The caller should initialize the memory held by TypeSourceInfo using 1241 /// the TypeLoc wrappers. 1242 /// 1243 /// \param T the type that will be the basis for type source info. This type 1244 /// should refer to how the declarator was written in source code, not to 1245 /// what type semantic analysis resolved the declarator to. 1246 /// 1247 /// \param Size the size of the type info to create, or 0 if the size 1248 /// should be calculated based on the type. 1249 TypeSourceInfo *CreateTypeSourceInfo(QualType T, unsigned Size = 0); 1250 1251 /// \brief Allocate a TypeSourceInfo where all locations have been 1252 /// initialized to a given location, which defaults to the empty 1253 /// location. 1254 TypeSourceInfo * 1255 getTrivialTypeSourceInfo(QualType T, SourceLocation Loc = SourceLocation()); 1256 1257private: 1258 ASTContext(const ASTContext&); // DO NOT IMPLEMENT 1259 void operator=(const ASTContext&); // DO NOT IMPLEMENT 1260 1261 void InitBuiltinTypes(); 1262 void InitBuiltinType(CanQualType &R, BuiltinType::Kind K); 1263 1264 // Return the ObjC type encoding for a given type. 1265 void getObjCEncodingForTypeImpl(QualType t, std::string &S, 1266 bool ExpandPointedToStructures, 1267 bool ExpandStructures, 1268 const FieldDecl *Field, 1269 bool OutermostType = false, 1270 bool EncodingProperty = false); 1271 1272 const ASTRecordLayout &getObjCLayout(const ObjCInterfaceDecl *D, 1273 const ObjCImplementationDecl *Impl); 1274 1275private: 1276 // FIXME: This currently contains the set of StoredDeclMaps used 1277 // by DeclContext objects. This probably should not be in ASTContext, 1278 // but we include it here so that ASTContext can quickly deallocate them. 1279 llvm::PointerIntPair<StoredDeclsMap*,1> LastSDM; 1280 friend class DeclContext; 1281 void ReleaseDeclContextMaps(); 1282}; 1283 1284/// @brief Utility function for constructing a nullary selector. 1285static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) { 1286 IdentifierInfo* II = &Ctx.Idents.get(name); 1287 return Ctx.Selectors.getSelector(0, &II); 1288} 1289 1290/// @brief Utility function for constructing an unary selector. 1291static inline Selector GetUnarySelector(const char* name, ASTContext& Ctx) { 1292 IdentifierInfo* II = &Ctx.Idents.get(name); 1293 return Ctx.Selectors.getSelector(1, &II); 1294} 1295 1296} // end namespace clang 1297 1298// operator new and delete aren't allowed inside namespaces. 1299// The throw specifications are mandated by the standard. 1300/// @brief Placement new for using the ASTContext's allocator. 1301/// 1302/// This placement form of operator new uses the ASTContext's allocator for 1303/// obtaining memory. It is a non-throwing new, which means that it returns 1304/// null on error. (If that is what the allocator does. The current does, so if 1305/// this ever changes, this operator will have to be changed, too.) 1306/// Usage looks like this (assuming there's an ASTContext 'Context' in scope): 1307/// @code 1308/// // Default alignment (8) 1309/// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments); 1310/// // Specific alignment 1311/// IntegerLiteral *Ex2 = new (Context, 4) IntegerLiteral(arguments); 1312/// @endcode 1313/// Please note that you cannot use delete on the pointer; it must be 1314/// deallocated using an explicit destructor call followed by 1315/// @c Context.Deallocate(Ptr). 1316/// 1317/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 1318/// @param C The ASTContext that provides the allocator. 1319/// @param Alignment The alignment of the allocated memory (if the underlying 1320/// allocator supports it). 1321/// @return The allocated memory. Could be NULL. 1322inline void *operator new(size_t Bytes, clang::ASTContext &C, 1323 size_t Alignment) throw () { 1324 return C.Allocate(Bytes, Alignment); 1325} 1326/// @brief Placement delete companion to the new above. 1327/// 1328/// This operator is just a companion to the new above. There is no way of 1329/// invoking it directly; see the new operator for more details. This operator 1330/// is called implicitly by the compiler if a placement new expression using 1331/// the ASTContext throws in the object constructor. 1332inline void operator delete(void *Ptr, clang::ASTContext &C, size_t) 1333 throw () { 1334 C.Deallocate(Ptr); 1335} 1336 1337/// This placement form of operator new[] uses the ASTContext's allocator for 1338/// obtaining memory. It is a non-throwing new[], which means that it returns 1339/// null on error. 1340/// Usage looks like this (assuming there's an ASTContext 'Context' in scope): 1341/// @code 1342/// // Default alignment (8) 1343/// char *data = new (Context) char[10]; 1344/// // Specific alignment 1345/// char *data = new (Context, 4) char[10]; 1346/// @endcode 1347/// Please note that you cannot use delete on the pointer; it must be 1348/// deallocated using an explicit destructor call followed by 1349/// @c Context.Deallocate(Ptr). 1350/// 1351/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 1352/// @param C The ASTContext that provides the allocator. 1353/// @param Alignment The alignment of the allocated memory (if the underlying 1354/// allocator supports it). 1355/// @return The allocated memory. Could be NULL. 1356inline void *operator new[](size_t Bytes, clang::ASTContext& C, 1357 size_t Alignment = 8) throw () { 1358 return C.Allocate(Bytes, Alignment); 1359} 1360 1361/// @brief Placement delete[] companion to the new[] above. 1362/// 1363/// This operator is just a companion to the new[] above. There is no way of 1364/// invoking it directly; see the new[] operator for more details. This operator 1365/// is called implicitly by the compiler if a placement new[] expression using 1366/// the ASTContext throws in the object constructor. 1367inline void operator delete[](void *Ptr, clang::ASTContext &C, size_t) 1368 throw () { 1369 C.Deallocate(Ptr); 1370} 1371 1372#endif 1373