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