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