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