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