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