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