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