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