ASTContext.h revision a42286486c85402c65f9d30df17e6b1b037a6ade
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 408public: 409 /// getAddSpaceQualType - Return the uniqued reference to the type for an 410 /// address space qualified type with the specified type and address space. 411 /// The resulting type has a union of the qualifiers from T and the address 412 /// space. If T already has an address space specifier, it is silently 413 /// replaced. 414 QualType getAddrSpaceQualType(QualType T, unsigned AddressSpace); 415 416 /// getObjCGCQualType - Returns the uniqued reference to the type for an 417 /// objc gc qualified type. The retulting type has a union of the qualifiers 418 /// from T and the gc attribute. 419 QualType getObjCGCQualType(QualType T, Qualifiers::GC gcAttr); 420 421 /// getRestrictType - Returns the uniqued reference to the type for a 422 /// 'restrict' qualified type. The resulting type has a union of the 423 /// qualifiers from T and 'restrict'. 424 QualType getRestrictType(QualType T) { 425 return T.withFastQualifiers(Qualifiers::Restrict); 426 } 427 428 /// getVolatileType - Returns the uniqued reference to the type for a 429 /// 'volatile' qualified type. The resulting type has a union of the 430 /// qualifiers from T and 'volatile'. 431 QualType getVolatileType(QualType T); 432 433 /// getConstType - Returns the uniqued reference to the type for a 434 /// 'const' qualified type. The resulting type has a union of the 435 /// qualifiers from T and 'const'. 436 /// 437 /// It can be reasonably expected that this will always be 438 /// equivalent to calling T.withConst(). 439 QualType getConstType(QualType T) { return T.withConst(); } 440 441 /// getNoReturnType - Add or remove the noreturn attribute to the given type 442 /// which must be a FunctionType or a pointer to an allowable type or a 443 /// BlockPointer. 444 QualType getNoReturnType(QualType T, bool AddNoReturn = true); 445 446 /// getCallConvType - Adds the specified calling convention attribute to 447 /// the given type, which must be a FunctionType or a pointer to an 448 /// allowable type. 449 QualType getCallConvType(QualType T, CallingConv CallConv); 450 451 /// getComplexType - Return the uniqued reference to the type for a complex 452 /// number with the specified element type. 453 QualType getComplexType(QualType T); 454 CanQualType getComplexType(CanQualType T) { 455 return CanQualType::CreateUnsafe(getComplexType((QualType) T)); 456 } 457 458 /// getPointerType - Return the uniqued reference to the type for a pointer to 459 /// the specified type. 460 QualType getPointerType(QualType T); 461 CanQualType getPointerType(CanQualType T) { 462 return CanQualType::CreateUnsafe(getPointerType((QualType) T)); 463 } 464 465 /// getBlockPointerType - Return the uniqued reference to the type for a block 466 /// of the specified type. 467 QualType getBlockPointerType(QualType T); 468 469 /// This gets the struct used to keep track of the descriptor for pointer to 470 /// blocks. 471 QualType getBlockDescriptorType(); 472 473 // Set the type for a Block descriptor type. 474 void setBlockDescriptorType(QualType T); 475 /// Get the BlockDescriptorType type, or NULL if it hasn't yet been built. 476 QualType getRawBlockdescriptorType() { 477 if (BlockDescriptorType) 478 return getTagDeclType(BlockDescriptorType); 479 return QualType(); 480 } 481 482 /// This gets the struct used to keep track of the extended descriptor for 483 /// pointer to blocks. 484 QualType getBlockDescriptorExtendedType(); 485 486 // Set the type for a Block descriptor extended type. 487 void setBlockDescriptorExtendedType(QualType T); 488 /// Get the BlockDescriptorExtendedType type, or NULL if it hasn't yet been 489 /// built. 490 QualType getRawBlockdescriptorExtendedType() { 491 if (BlockDescriptorExtendedType) 492 return getTagDeclType(BlockDescriptorExtendedType); 493 return QualType(); 494 } 495 496 /// This gets the struct used to keep track of pointer to blocks, complete 497 /// with captured variables. 498 QualType getBlockParmType(bool BlockHasCopyDispose, 499 llvm::SmallVector<const Expr *, 8> &BDRDs); 500 501 /// This builds the struct used for __block variables. 502 QualType BuildByRefType(const char *DeclName, QualType Ty); 503 504 /// Returns true iff we need copy/dispose helpers for the given type. 505 bool BlockRequiresCopying(QualType Ty); 506 507 /// getLValueReferenceType - Return the uniqued reference to the type for an 508 /// lvalue reference to the specified type. 509 QualType getLValueReferenceType(QualType T, bool SpelledAsLValue = true); 510 511 /// getRValueReferenceType - Return the uniqued reference to the type for an 512 /// rvalue reference to the specified type. 513 QualType getRValueReferenceType(QualType T); 514 515 /// getMemberPointerType - Return the uniqued reference to the type for a 516 /// member pointer to the specified type in the specified class. The class 517 /// is a Type because it could be a dependent name. 518 QualType getMemberPointerType(QualType T, const Type *Cls); 519 520 /// getVariableArrayType - Returns a non-unique reference to the type for a 521 /// variable array of the specified element type. 522 QualType getVariableArrayType(QualType EltTy, Expr *NumElts, 523 ArrayType::ArraySizeModifier ASM, 524 unsigned EltTypeQuals, 525 SourceRange Brackets); 526 527 /// getDependentSizedArrayType - Returns a non-unique reference to 528 /// the type for a dependently-sized array of the specified element 529 /// type. FIXME: We will need these to be uniqued, or at least 530 /// comparable, at some point. 531 QualType getDependentSizedArrayType(QualType EltTy, Expr *NumElts, 532 ArrayType::ArraySizeModifier ASM, 533 unsigned EltTypeQuals, 534 SourceRange Brackets); 535 536 /// getIncompleteArrayType - Returns a unique reference to the type for a 537 /// incomplete array of the specified element type. 538 QualType getIncompleteArrayType(QualType EltTy, 539 ArrayType::ArraySizeModifier ASM, 540 unsigned EltTypeQuals); 541 542 /// getConstantArrayType - Return the unique reference to the type for a 543 /// constant array of the specified element type. 544 QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize, 545 ArrayType::ArraySizeModifier ASM, 546 unsigned EltTypeQuals); 547 548 /// getVectorType - Return the unique reference to a vector type of 549 /// the specified element type and size. VectorType must be a built-in type. 550 QualType getVectorType(QualType VectorType, unsigned NumElts, 551 bool AltiVec, bool IsPixel); 552 553 /// getExtVectorType - Return the unique reference to an extended vector type 554 /// of the specified element type and size. VectorType must be a built-in 555 /// type. 556 QualType getExtVectorType(QualType VectorType, unsigned NumElts); 557 558 /// getDependentSizedExtVectorType - Returns a non-unique reference to 559 /// the type for a dependently-sized vector of the specified element 560 /// type. FIXME: We will need these to be uniqued, or at least 561 /// comparable, at some point. 562 QualType getDependentSizedExtVectorType(QualType VectorType, 563 Expr *SizeExpr, 564 SourceLocation AttrLoc); 565 566 /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. 567 /// 568 QualType getFunctionNoProtoType(QualType ResultTy, bool NoReturn = false, 569 CallingConv CallConv = CC_Default); 570 571 /// getFunctionType - Return a normal function type with a typed argument 572 /// list. isVariadic indicates whether the argument list includes '...'. 573 QualType getFunctionType(QualType ResultTy, const QualType *ArgArray, 574 unsigned NumArgs, bool isVariadic, 575 unsigned TypeQuals, bool hasExceptionSpec, 576 bool hasAnyExceptionSpec, 577 unsigned NumExs, const QualType *ExArray, 578 bool NoReturn, 579 CallingConv CallConv); 580 581 /// getTypeDeclType - Return the unique reference to the type for 582 /// the specified type declaration. 583 QualType getTypeDeclType(const TypeDecl *Decl, const TypeDecl* PrevDecl=0); 584 585 /// getTypedefType - Return the unique reference to the type for the 586 /// specified typename decl. 587 QualType getTypedefType(const TypedefDecl *Decl); 588 589 QualType getSubstTemplateTypeParmType(const TemplateTypeParmType *Replaced, 590 QualType Replacement); 591 592 QualType getTemplateTypeParmType(unsigned Depth, unsigned Index, 593 bool ParameterPack, 594 IdentifierInfo *Name = 0); 595 596 QualType getTemplateSpecializationType(TemplateName T, 597 const TemplateArgument *Args, 598 unsigned NumArgs, 599 QualType Canon = QualType()); 600 601 QualType getTemplateSpecializationType(TemplateName T, 602 const TemplateArgumentListInfo &Args, 603 QualType Canon = QualType()); 604 605 QualType getQualifiedNameType(NestedNameSpecifier *NNS, 606 QualType NamedType); 607 QualType getTypenameType(NestedNameSpecifier *NNS, 608 const IdentifierInfo *Name, 609 QualType Canon = QualType()); 610 QualType getTypenameType(NestedNameSpecifier *NNS, 611 const TemplateSpecializationType *TemplateId, 612 QualType Canon = QualType()); 613 QualType getElaboratedType(QualType UnderlyingType, 614 ElaboratedType::TagKind Tag); 615 616 QualType getObjCInterfaceType(const ObjCInterfaceDecl *Decl, 617 ObjCProtocolDecl **Protocols = 0, 618 unsigned NumProtocols = 0); 619 620 /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for the 621 /// given interface decl and the conforming protocol list. 622 QualType getObjCObjectPointerType(QualType OIT, 623 ObjCProtocolDecl **ProtocolList = 0, 624 unsigned NumProtocols = 0, 625 unsigned Quals = 0); 626 627 /// getTypeOfType - GCC extension. 628 QualType getTypeOfExprType(Expr *e); 629 QualType getTypeOfType(QualType t); 630 631 /// getDecltypeType - C++0x decltype. 632 QualType getDecltypeType(Expr *e); 633 634 /// getTagDeclType - Return the unique reference to the type for the 635 /// specified TagDecl (struct/union/class/enum) decl. 636 QualType getTagDeclType(const TagDecl *Decl); 637 638 /// getSizeType - Return the unique type for "size_t" (C99 7.17), defined 639 /// in <stddef.h>. The sizeof operator requires this (C99 6.5.3.4p4). 640 CanQualType getSizeType() const; 641 642 /// getWCharType - In C++, this returns the unique wchar_t type. In C99, this 643 /// returns a type compatible with the type defined in <stddef.h> as defined 644 /// by the target. 645 QualType getWCharType() const { return WCharTy; } 646 647 /// getSignedWCharType - Return the type of "signed wchar_t". 648 /// Used when in C++, as a GCC extension. 649 QualType getSignedWCharType() const; 650 651 /// getUnsignedWCharType - Return the type of "unsigned wchar_t". 652 /// Used when in C++, as a GCC extension. 653 QualType getUnsignedWCharType() const; 654 655 /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) 656 /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). 657 QualType getPointerDiffType() const; 658 659 // getCFConstantStringType - Return the C structure type used to represent 660 // constant CFStrings. 661 QualType getCFConstantStringType(); 662 663 /// Get the structure type used to representation CFStrings, or NULL 664 /// if it hasn't yet been built. 665 QualType getRawCFConstantStringType() { 666 if (CFConstantStringTypeDecl) 667 return getTagDeclType(CFConstantStringTypeDecl); 668 return QualType(); 669 } 670 void setCFConstantStringType(QualType T); 671 672 // This setter/getter represents the ObjC type for an NSConstantString. 673 void setObjCConstantStringInterface(ObjCInterfaceDecl *Decl); 674 QualType getObjCConstantStringInterface() const { 675 return ObjCConstantStringType; 676 } 677 678 //// This gets the struct used to keep track of fast enumerations. 679 QualType getObjCFastEnumerationStateType(); 680 681 /// Get the ObjCFastEnumerationState type, or NULL if it hasn't yet 682 /// been built. 683 QualType getRawObjCFastEnumerationStateType() { 684 if (ObjCFastEnumerationStateTypeDecl) 685 return getTagDeclType(ObjCFastEnumerationStateTypeDecl); 686 return QualType(); 687 } 688 689 void setObjCFastEnumerationStateType(QualType T); 690 691 /// \brief Set the type for the C FILE type. 692 void setFILEDecl(TypeDecl *FILEDecl) { this->FILEDecl = FILEDecl; } 693 694 /// \brief Retrieve the C FILE type. 695 QualType getFILEType() { 696 if (FILEDecl) 697 return getTypeDeclType(FILEDecl); 698 return QualType(); 699 } 700 701 /// \brief Set the type for the C jmp_buf type. 702 void setjmp_bufDecl(TypeDecl *jmp_bufDecl) { 703 this->jmp_bufDecl = jmp_bufDecl; 704 } 705 706 /// \brief Retrieve the C jmp_buf type. 707 QualType getjmp_bufType() { 708 if (jmp_bufDecl) 709 return getTypeDeclType(jmp_bufDecl); 710 return QualType(); 711 } 712 713 /// \brief Set the type for the C sigjmp_buf type. 714 void setsigjmp_bufDecl(TypeDecl *sigjmp_bufDecl) { 715 this->sigjmp_bufDecl = sigjmp_bufDecl; 716 } 717 718 /// \brief Retrieve the C sigjmp_buf type. 719 QualType getsigjmp_bufType() { 720 if (sigjmp_bufDecl) 721 return getTypeDeclType(sigjmp_bufDecl); 722 return QualType(); 723 } 724 725 /// getObjCEncodingForType - Emit the ObjC type encoding for the 726 /// given type into \arg S. If \arg NameFields is specified then 727 /// record field names are also encoded. 728 void getObjCEncodingForType(QualType t, std::string &S, 729 const FieldDecl *Field=0); 730 731 void getLegacyIntegralTypeEncoding(QualType &t) const; 732 733 // Put the string version of type qualifiers into S. 734 void getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, 735 std::string &S) const; 736 737 /// getObjCEncodingForMethodDecl - Return the encoded type for this method 738 /// declaration. 739 void getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, std::string &S); 740 741 /// getObjCEncodingForBlockDecl - Return the encoded type for this block 742 /// declaration. 743 void getObjCEncodingForBlock(const BlockExpr *Expr, std::string& S); 744 745 /// getObjCEncodingForPropertyDecl - Return the encoded type for 746 /// this method declaration. If non-NULL, Container must be either 747 /// an ObjCCategoryImplDecl or ObjCImplementationDecl; it should 748 /// only be NULL when getting encodings for protocol properties. 749 void getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, 750 const Decl *Container, 751 std::string &S); 752 753 bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, 754 ObjCProtocolDecl *rProto); 755 756 /// getObjCEncodingTypeSize returns size of type for objective-c encoding 757 /// purpose in characters. 758 CharUnits getObjCEncodingTypeSize(QualType t); 759 760 /// This setter/getter represents the ObjC 'id' type. It is setup lazily, by 761 /// Sema. id is always a (typedef for a) pointer type, a pointer to a struct. 762 QualType getObjCIdType() const { return ObjCIdTypedefType; } 763 void setObjCIdType(QualType T); 764 765 void setObjCSelType(QualType T); 766 QualType getObjCSelType() const { return ObjCSelTypedefType; } 767 768 void setObjCProtoType(QualType QT); 769 QualType getObjCProtoType() const { return ObjCProtoType; } 770 771 /// This setter/getter repreents the ObjC 'Class' type. It is setup lazily, by 772 /// Sema. 'Class' is always a (typedef for a) pointer type, a pointer to a 773 /// struct. 774 QualType getObjCClassType() const { return ObjCClassTypedefType; } 775 void setObjCClassType(QualType T); 776 777 void setBuiltinVaListType(QualType T); 778 QualType getBuiltinVaListType() const { return BuiltinVaListType; } 779 780 /// getCVRQualifiedType - Returns a type with additional const, 781 /// volatile, or restrict qualifiers. 782 QualType getCVRQualifiedType(QualType T, unsigned CVR) { 783 return getQualifiedType(T, Qualifiers::fromCVRMask(CVR)); 784 } 785 786 /// getQualifiedType - Returns a type with additional qualifiers. 787 QualType getQualifiedType(QualType T, Qualifiers Qs) { 788 if (!Qs.hasNonFastQualifiers()) 789 return T.withFastQualifiers(Qs.getFastQualifiers()); 790 QualifierCollector Qc(Qs); 791 const Type *Ptr = Qc.strip(T); 792 return getExtQualType(Ptr, Qc); 793 } 794 795 /// getQualifiedType - Returns a type with additional qualifiers. 796 QualType getQualifiedType(const Type *T, Qualifiers Qs) { 797 if (!Qs.hasNonFastQualifiers()) 798 return QualType(T, Qs.getFastQualifiers()); 799 return getExtQualType(T, Qs); 800 } 801 802 DeclarationName getNameForTemplate(TemplateName Name); 803 804 TemplateName getOverloadedTemplateName(UnresolvedSetIterator Begin, 805 UnresolvedSetIterator End); 806 807 TemplateName getQualifiedTemplateName(NestedNameSpecifier *NNS, 808 bool TemplateKeyword, 809 TemplateDecl *Template); 810 811 TemplateName getDependentTemplateName(NestedNameSpecifier *NNS, 812 const IdentifierInfo *Name); 813 TemplateName getDependentTemplateName(NestedNameSpecifier *NNS, 814 OverloadedOperatorKind Operator); 815 816 enum GetBuiltinTypeError { 817 GE_None, //< No error 818 GE_Missing_stdio, //< Missing a type from <stdio.h> 819 GE_Missing_setjmp //< Missing a type from <setjmp.h> 820 }; 821 822 /// GetBuiltinType - Return the type for the specified builtin. 823 QualType GetBuiltinType(unsigned ID, GetBuiltinTypeError &Error); 824 825private: 826 CanQualType getFromTargetType(unsigned Type) const; 827 828 //===--------------------------------------------------------------------===// 829 // Type Predicates. 830 //===--------------------------------------------------------------------===// 831 832public: 833 /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's 834 /// garbage collection attribute. 835 /// 836 Qualifiers::GC getObjCGCAttrKind(const QualType &Ty) const; 837 838 /// isObjCNSObjectType - Return true if this is an NSObject object with 839 /// its NSObject attribute set. 840 bool isObjCNSObjectType(QualType Ty) const; 841 842 //===--------------------------------------------------------------------===// 843 // Type Sizing and Analysis 844 //===--------------------------------------------------------------------===// 845 846 /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified 847 /// scalar floating point type. 848 const llvm::fltSemantics &getFloatTypeSemantics(QualType T) const; 849 850 /// getTypeInfo - Get the size and alignment of the specified complete type in 851 /// bits. 852 std::pair<uint64_t, unsigned> getTypeInfo(const Type *T); 853 std::pair<uint64_t, unsigned> getTypeInfo(QualType T) { 854 return getTypeInfo(T.getTypePtr()); 855 } 856 857 /// getTypeSize - Return the size of the specified type, in bits. This method 858 /// does not work on incomplete types. 859 uint64_t getTypeSize(QualType T) { 860 return getTypeInfo(T).first; 861 } 862 uint64_t getTypeSize(const Type *T) { 863 return getTypeInfo(T).first; 864 } 865 866 /// getCharWidth - Return the size of the character type, in bits 867 uint64_t getCharWidth() { 868 return getTypeSize(CharTy); 869 } 870 871 /// getTypeSizeInChars - Return the size of the specified type, in characters. 872 /// This method does not work on incomplete types. 873 CharUnits getTypeSizeInChars(QualType T); 874 CharUnits getTypeSizeInChars(const Type *T); 875 876 /// getTypeAlign - Return the ABI-specified alignment of a type, in bits. 877 /// This method does not work on incomplete types. 878 unsigned getTypeAlign(QualType T) { 879 return getTypeInfo(T).second; 880 } 881 unsigned getTypeAlign(const Type *T) { 882 return getTypeInfo(T).second; 883 } 884 885 /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in 886 /// characters. This method does not work on incomplete types. 887 CharUnits getTypeAlignInChars(QualType T); 888 CharUnits getTypeAlignInChars(const Type *T); 889 890 /// getPreferredTypeAlign - Return the "preferred" alignment of the specified 891 /// type for the current target in bits. This can be different than the ABI 892 /// alignment in cases where it is beneficial for performance to overalign 893 /// a data type. 894 unsigned getPreferredTypeAlign(const Type *T); 895 896 /// getDeclAlign - Return a conservative estimate of the alignment of 897 /// the specified decl. Note that bitfields do not have a valid alignment, so 898 /// this method will assert on them. 899 /// If @p RefAsPointee, references are treated like their underlying type 900 /// (for alignof), else they're treated like pointers (for CodeGen). 901 CharUnits getDeclAlign(const Decl *D, bool RefAsPointee = false); 902 903 /// getASTRecordLayout - Get or compute information about the layout of the 904 /// specified record (struct/union/class), which indicates its size and field 905 /// position information. 906 const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D); 907 908 /// getASTObjCInterfaceLayout - Get or compute information about the 909 /// layout of the specified Objective-C interface. 910 const ASTRecordLayout &getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D); 911 912 /// getASTObjCImplementationLayout - Get or compute information about 913 /// the layout of the specified Objective-C implementation. This may 914 /// differ from the interface if synthesized ivars are present. 915 const ASTRecordLayout & 916 getASTObjCImplementationLayout(const ObjCImplementationDecl *D); 917 918 /// getKeyFunction - Get the key function for the given record decl. 919 /// The key function is, according to the Itanium C++ ABI section 5.2.3: 920 /// 921 /// ...the first non-pure virtual function that is not inline at the point 922 /// of class definition. 923 const CXXMethodDecl *getKeyFunction(const CXXRecordDecl *RD); 924 925 void CollectObjCIvars(const ObjCInterfaceDecl *OI, 926 llvm::SmallVectorImpl<FieldDecl*> &Fields); 927 928 void ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI, 929 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars); 930 void CollectNonClassIvars(const ObjCInterfaceDecl *OI, 931 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars); 932 void CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD, 933 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars); 934 unsigned CountSynthesizedIvars(const ObjCInterfaceDecl *OI); 935 unsigned CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD); 936 void CollectInheritedProtocols(const Decl *CDecl, 937 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols); 938 939 //===--------------------------------------------------------------------===// 940 // Type Operators 941 //===--------------------------------------------------------------------===// 942 943 /// getCanonicalType - Return the canonical (structural) type corresponding to 944 /// the specified potentially non-canonical type. The non-canonical version 945 /// of a type may have many "decorated" versions of types. Decorators can 946 /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed 947 /// to be free of any of these, allowing two canonical types to be compared 948 /// for exact equality with a simple pointer comparison. 949 CanQualType getCanonicalType(QualType T); 950 const Type *getCanonicalType(const Type *T) { 951 return T->getCanonicalTypeInternal().getTypePtr(); 952 } 953 954 /// getCanonicalParamType - Return the canonical parameter type 955 /// corresponding to the specific potentially non-canonical one. 956 /// Qualifiers are stripped off, functions are turned into function 957 /// pointers, and arrays decay one level into pointers. 958 CanQualType getCanonicalParamType(QualType T); 959 960 /// \brief Determine whether the given types are equivalent. 961 bool hasSameType(QualType T1, QualType T2) { 962 return getCanonicalType(T1) == getCanonicalType(T2); 963 } 964 965 /// \brief Returns this type as a completely-unqualified array type, 966 /// capturing the qualifiers in Quals. This will remove the minimal amount of 967 /// sugaring from the types, similar to the behavior of 968 /// QualType::getUnqualifiedType(). 969 /// 970 /// \param T is the qualified type, which may be an ArrayType 971 /// 972 /// \param Quals will receive the full set of qualifiers that were 973 /// applied to the array. 974 /// 975 /// \returns if this is an array type, the completely unqualified array type 976 /// that corresponds to it. Otherwise, returns T.getUnqualifiedType(). 977 QualType getUnqualifiedArrayType(QualType T, Qualifiers &Quals); 978 979 /// \brief Determine whether the given types are equivalent after 980 /// cvr-qualifiers have been removed. 981 bool hasSameUnqualifiedType(QualType T1, QualType T2) { 982 CanQualType CT1 = getCanonicalType(T1); 983 CanQualType CT2 = getCanonicalType(T2); 984 985 Qualifiers Quals; 986 QualType UnqualT1 = getUnqualifiedArrayType(CT1, Quals); 987 QualType UnqualT2 = getUnqualifiedArrayType(CT2, Quals); 988 return UnqualT1 == UnqualT2; 989 } 990 991 /// \brief Retrieves the "canonical" declaration of 992 993 /// \brief Retrieves the "canonical" nested name specifier for a 994 /// given nested name specifier. 995 /// 996 /// The canonical nested name specifier is a nested name specifier 997 /// that uniquely identifies a type or namespace within the type 998 /// system. For example, given: 999 /// 1000 /// \code 1001 /// namespace N { 1002 /// struct S { 1003 /// template<typename T> struct X { typename T* type; }; 1004 /// }; 1005 /// } 1006 /// 1007 /// template<typename T> struct Y { 1008 /// typename N::S::X<T>::type member; 1009 /// }; 1010 /// \endcode 1011 /// 1012 /// Here, the nested-name-specifier for N::S::X<T>:: will be 1013 /// S::X<template-param-0-0>, since 'S' and 'X' are uniquely defined 1014 /// by declarations in the type system and the canonical type for 1015 /// the template type parameter 'T' is template-param-0-0. 1016 NestedNameSpecifier * 1017 getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS); 1018 1019 /// \brief Retrieves the canonical representation of the given 1020 /// calling convention. 1021 CallingConv getCanonicalCallConv(CallingConv CC) { 1022 if (CC == CC_C) 1023 return CC_Default; 1024 return CC; 1025 } 1026 1027 /// \brief Determines whether two calling conventions name the same 1028 /// calling convention. 1029 bool isSameCallConv(CallingConv lcc, CallingConv rcc) { 1030 return (getCanonicalCallConv(lcc) == getCanonicalCallConv(rcc)); 1031 } 1032 1033 /// \brief Retrieves the "canonical" template name that refers to a 1034 /// given template. 1035 /// 1036 /// The canonical template name is the simplest expression that can 1037 /// be used to refer to a given template. For most templates, this 1038 /// expression is just the template declaration itself. For example, 1039 /// the template std::vector can be referred to via a variety of 1040 /// names---std::vector, ::std::vector, vector (if vector is in 1041 /// scope), etc.---but all of these names map down to the same 1042 /// TemplateDecl, which is used to form the canonical template name. 1043 /// 1044 /// Dependent template names are more interesting. Here, the 1045 /// template name could be something like T::template apply or 1046 /// std::allocator<T>::template rebind, where the nested name 1047 /// specifier itself is dependent. In this case, the canonical 1048 /// template name uses the shortest form of the dependent 1049 /// nested-name-specifier, which itself contains all canonical 1050 /// types, values, and templates. 1051 TemplateName getCanonicalTemplateName(TemplateName Name); 1052 1053 /// \brief Determine whether the given template names refer to the same 1054 /// template. 1055 bool hasSameTemplateName(TemplateName X, TemplateName Y); 1056 1057 /// \brief Retrieve the "canonical" template argument. 1058 /// 1059 /// The canonical template argument is the simplest template argument 1060 /// (which may be a type, value, expression, or declaration) that 1061 /// expresses the value of the argument. 1062 TemplateArgument getCanonicalTemplateArgument(const TemplateArgument &Arg); 1063 1064 /// Type Query functions. If the type is an instance of the specified class, 1065 /// return the Type pointer for the underlying maximally pretty type. This 1066 /// is a member of ASTContext because this may need to do some amount of 1067 /// canonicalization, e.g. to move type qualifiers into the element type. 1068 const ArrayType *getAsArrayType(QualType T); 1069 const ConstantArrayType *getAsConstantArrayType(QualType T) { 1070 return dyn_cast_or_null<ConstantArrayType>(getAsArrayType(T)); 1071 } 1072 const VariableArrayType *getAsVariableArrayType(QualType T) { 1073 return dyn_cast_or_null<VariableArrayType>(getAsArrayType(T)); 1074 } 1075 const IncompleteArrayType *getAsIncompleteArrayType(QualType T) { 1076 return dyn_cast_or_null<IncompleteArrayType>(getAsArrayType(T)); 1077 } 1078 const DependentSizedArrayType *getAsDependentSizedArrayType(QualType T) { 1079 return dyn_cast_or_null<DependentSizedArrayType>(getAsArrayType(T)); 1080 } 1081 1082 /// getBaseElementType - Returns the innermost element type of an array type. 1083 /// For example, will return "int" for int[m][n] 1084 QualType getBaseElementType(const ArrayType *VAT); 1085 1086 /// getBaseElementType - Returns the innermost element type of a type 1087 /// (which needn't actually be an array type). 1088 QualType getBaseElementType(QualType QT); 1089 1090 /// getConstantArrayElementCount - Returns number of constant array elements. 1091 uint64_t getConstantArrayElementCount(const ConstantArrayType *CA) const; 1092 1093 /// getArrayDecayedType - Return the properly qualified result of decaying the 1094 /// specified array type to a pointer. This operation is non-trivial when 1095 /// handling typedefs etc. The canonical type of "T" must be an array type, 1096 /// this returns a pointer to a properly qualified element of the array. 1097 /// 1098 /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. 1099 QualType getArrayDecayedType(QualType T); 1100 1101 /// getPromotedIntegerType - Returns the type that Promotable will 1102 /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable 1103 /// integer type. 1104 QualType getPromotedIntegerType(QualType PromotableType); 1105 1106 /// \brief Whether this is a promotable bitfield reference according 1107 /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). 1108 /// 1109 /// \returns the type this bit-field will promote to, or NULL if no 1110 /// promotion occurs. 1111 QualType isPromotableBitField(Expr *E); 1112 1113 /// getIntegerTypeOrder - Returns the highest ranked integer type: 1114 /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If 1115 /// LHS < RHS, return -1. 1116 int getIntegerTypeOrder(QualType LHS, QualType RHS); 1117 1118 /// getFloatingTypeOrder - Compare the rank of the two specified floating 1119 /// point types, ignoring the domain of the type (i.e. 'double' == 1120 /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If 1121 /// LHS < RHS, return -1. 1122 int getFloatingTypeOrder(QualType LHS, QualType RHS); 1123 1124 /// getFloatingTypeOfSizeWithinDomain - Returns a real floating 1125 /// point or a complex type (based on typeDomain/typeSize). 1126 /// 'typeDomain' is a real floating point or complex type. 1127 /// 'typeSize' is a real floating point or complex type. 1128 QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize, 1129 QualType typeDomain) const; 1130 1131private: 1132 // Helper for integer ordering 1133 unsigned getIntegerRank(Type* T); 1134 1135public: 1136 1137 //===--------------------------------------------------------------------===// 1138 // Type Compatibility Predicates 1139 //===--------------------------------------------------------------------===// 1140 1141 /// Compatibility predicates used to check assignment expressions. 1142 bool typesAreCompatible(QualType, QualType); // C99 6.2.7p1 1143 1144 bool isObjCIdType(QualType T) const { 1145 return T == ObjCIdTypedefType; 1146 } 1147 bool isObjCClassType(QualType T) const { 1148 return T == ObjCClassTypedefType; 1149 } 1150 bool isObjCSelType(QualType T) const { 1151 return T == ObjCSelTypedefType; 1152 } 1153 bool QualifiedIdConformsQualifiedId(QualType LHS, QualType RHS); 1154 bool ObjCQualifiedIdTypesAreCompatible(QualType LHS, QualType RHS, 1155 bool ForCompare); 1156 1157 // Check the safety of assignment from LHS to RHS 1158 bool canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, 1159 const ObjCObjectPointerType *RHSOPT); 1160 bool canAssignObjCInterfaces(const ObjCInterfaceType *LHS, 1161 const ObjCInterfaceType *RHS); 1162 bool areComparableObjCPointerTypes(QualType LHS, QualType RHS); 1163 QualType areCommonBaseCompatible(const ObjCObjectPointerType *LHSOPT, 1164 const ObjCObjectPointerType *RHSOPT); 1165 1166 // Functions for calculating composite types 1167 QualType mergeTypes(QualType, QualType); 1168 QualType mergeFunctionTypes(QualType, QualType); 1169 1170 /// UsualArithmeticConversionsType - handles the various conversions 1171 /// that are common to binary operators (C99 6.3.1.8, C++ [expr]p9) 1172 /// and returns the result type of that conversion. 1173 QualType UsualArithmeticConversionsType(QualType lhs, QualType rhs); 1174 1175 //===--------------------------------------------------------------------===// 1176 // Integer Predicates 1177 //===--------------------------------------------------------------------===// 1178 1179 // The width of an integer, as defined in C99 6.2.6.2. This is the number 1180 // of bits in an integer type excluding any padding bits. 1181 unsigned getIntWidth(QualType T); 1182 1183 // Per C99 6.2.5p6, for every signed integer type, there is a corresponding 1184 // unsigned integer type. This method takes a signed type, and returns the 1185 // corresponding unsigned integer type. 1186 QualType getCorrespondingUnsignedType(QualType T); 1187 1188 //===--------------------------------------------------------------------===// 1189 // Type Iterators. 1190 //===--------------------------------------------------------------------===// 1191 1192 typedef std::vector<Type*>::iterator type_iterator; 1193 typedef std::vector<Type*>::const_iterator const_type_iterator; 1194 1195 type_iterator types_begin() { return Types.begin(); } 1196 type_iterator types_end() { return Types.end(); } 1197 const_type_iterator types_begin() const { return Types.begin(); } 1198 const_type_iterator types_end() const { return Types.end(); } 1199 1200 //===--------------------------------------------------------------------===// 1201 // Integer Values 1202 //===--------------------------------------------------------------------===// 1203 1204 /// MakeIntValue - Make an APSInt of the appropriate width and 1205 /// signedness for the given \arg Value and integer \arg Type. 1206 llvm::APSInt MakeIntValue(uint64_t Value, QualType Type) { 1207 llvm::APSInt Res(getIntWidth(Type), !Type->isSignedIntegerType()); 1208 Res = Value; 1209 return Res; 1210 } 1211 1212 /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. 1213 ObjCImplementationDecl *getObjCImplementation(ObjCInterfaceDecl *D); 1214 /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. 1215 ObjCCategoryImplDecl *getObjCImplementation(ObjCCategoryDecl *D); 1216 1217 /// \brief Set the implementation of ObjCInterfaceDecl. 1218 void setObjCImplementation(ObjCInterfaceDecl *IFaceD, 1219 ObjCImplementationDecl *ImplD); 1220 /// \brief Set the implementation of ObjCCategoryDecl. 1221 void setObjCImplementation(ObjCCategoryDecl *CatD, 1222 ObjCCategoryImplDecl *ImplD); 1223 1224 /// \brief Allocate an uninitialized TypeSourceInfo. 1225 /// 1226 /// The caller should initialize the memory held by TypeSourceInfo using 1227 /// the TypeLoc wrappers. 1228 /// 1229 /// \param T the type that will be the basis for type source info. This type 1230 /// should refer to how the declarator was written in source code, not to 1231 /// what type semantic analysis resolved the declarator to. 1232 /// 1233 /// \param Size the size of the type info to create, or 0 if the size 1234 /// should be calculated based on the type. 1235 TypeSourceInfo *CreateTypeSourceInfo(QualType T, unsigned Size = 0); 1236 1237 /// \brief Allocate a TypeSourceInfo where all locations have been 1238 /// initialized to a given location, which defaults to the empty 1239 /// location. 1240 TypeSourceInfo * 1241 getTrivialTypeSourceInfo(QualType T, SourceLocation Loc = SourceLocation()); 1242 1243private: 1244 ASTContext(const ASTContext&); // DO NOT IMPLEMENT 1245 void operator=(const ASTContext&); // DO NOT IMPLEMENT 1246 1247 void InitBuiltinTypes(); 1248 void InitBuiltinType(CanQualType &R, BuiltinType::Kind K); 1249 1250 // Return the ObjC type encoding for a given type. 1251 void getObjCEncodingForTypeImpl(QualType t, std::string &S, 1252 bool ExpandPointedToStructures, 1253 bool ExpandStructures, 1254 const FieldDecl *Field, 1255 bool OutermostType = false, 1256 bool EncodingProperty = false); 1257 1258 const ASTRecordLayout &getObjCLayout(const ObjCInterfaceDecl *D, 1259 const ObjCImplementationDecl *Impl); 1260 1261private: 1262 // FIXME: This currently contains the set of StoredDeclMaps used 1263 // by DeclContext objects. This probably should not be in ASTContext, 1264 // but we include it here so that ASTContext can quickly deallocate them. 1265 std::vector<void*> SDMs; 1266 friend class DeclContext; 1267 void *CreateStoredDeclsMap(); 1268 void ReleaseDeclContextMaps(); 1269}; 1270 1271/// @brief Utility function for constructing a nullary selector. 1272static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) { 1273 IdentifierInfo* II = &Ctx.Idents.get(name); 1274 return Ctx.Selectors.getSelector(0, &II); 1275} 1276 1277/// @brief Utility function for constructing an unary selector. 1278static inline Selector GetUnarySelector(const char* name, ASTContext& Ctx) { 1279 IdentifierInfo* II = &Ctx.Idents.get(name); 1280 return Ctx.Selectors.getSelector(1, &II); 1281} 1282 1283} // end namespace clang 1284 1285// operator new and delete aren't allowed inside namespaces. 1286// The throw specifications are mandated by the standard. 1287/// @brief Placement new for using the ASTContext's allocator. 1288/// 1289/// This placement form of operator new uses the ASTContext's allocator for 1290/// obtaining memory. It is a non-throwing new, which means that it returns 1291/// null on error. (If that is what the allocator does. The current does, so if 1292/// this ever changes, this operator will have to be changed, too.) 1293/// Usage looks like this (assuming there's an ASTContext 'Context' in scope): 1294/// @code 1295/// // Default alignment (16) 1296/// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments); 1297/// // Specific alignment 1298/// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments); 1299/// @endcode 1300/// Please note that you cannot use delete on the pointer; it must be 1301/// deallocated using an explicit destructor call followed by 1302/// @c Context.Deallocate(Ptr). 1303/// 1304/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 1305/// @param C The ASTContext that provides the allocator. 1306/// @param Alignment The alignment of the allocated memory (if the underlying 1307/// allocator supports it). 1308/// @return The allocated memory. Could be NULL. 1309inline void *operator new(size_t Bytes, clang::ASTContext &C, 1310 size_t Alignment) throw () { 1311 return C.Allocate(Bytes, Alignment); 1312} 1313/// @brief Placement delete companion to the new above. 1314/// 1315/// This operator is just a companion to the new above. There is no way of 1316/// invoking it directly; see the new operator for more details. This operator 1317/// is called implicitly by the compiler if a placement new expression using 1318/// the ASTContext throws in the object constructor. 1319inline void operator delete(void *Ptr, clang::ASTContext &C, size_t) 1320 throw () { 1321 C.Deallocate(Ptr); 1322} 1323 1324/// This placement form of operator new[] uses the ASTContext's allocator for 1325/// obtaining memory. It is a non-throwing new[], which means that it returns 1326/// null on error. 1327/// Usage looks like this (assuming there's an ASTContext 'Context' in scope): 1328/// @code 1329/// // Default alignment (16) 1330/// char *data = new (Context) char[10]; 1331/// // Specific alignment 1332/// char *data = new (Context, 8) char[10]; 1333/// @endcode 1334/// Please note that you cannot use delete on the pointer; it must be 1335/// deallocated using an explicit destructor call followed by 1336/// @c Context.Deallocate(Ptr). 1337/// 1338/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 1339/// @param C The ASTContext that provides the allocator. 1340/// @param Alignment The alignment of the allocated memory (if the underlying 1341/// allocator supports it). 1342/// @return The allocated memory. Could be NULL. 1343inline void *operator new[](size_t Bytes, clang::ASTContext& C, 1344 size_t Alignment = 16) throw () { 1345 return C.Allocate(Bytes, Alignment); 1346} 1347 1348/// @brief Placement delete[] companion to the new[] above. 1349/// 1350/// This operator is just a companion to the new[] above. There is no way of 1351/// invoking it directly; see the new[] operator for more details. This operator 1352/// is called implicitly by the compiler if a placement new[] expression using 1353/// the ASTContext throws in the object constructor. 1354inline void operator delete[](void *Ptr, clang::ASTContext &C, size_t) 1355 throw () { 1356 C.Deallocate(Ptr); 1357} 1358 1359#endif 1360