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