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