ASTContext.h revision eec51cf1ba5f0e62c9cdb81b5c63babdd6e649ab
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 /// getPreferredTypeAlign - Return the "preferred" alignment of the specified 839 /// type for the current target in bits. This can be different than the ABI 840 /// alignment in cases where it is beneficial for performance to overalign 841 /// a data type. 842 unsigned getPreferredTypeAlign(const Type *T); 843 844 unsigned getDeclAlignInBytes(const Decl *D, bool RefAsPointee = false); 845 846 /// getASTRecordLayout - Get or compute information about the layout of the 847 /// specified record (struct/union/class), which indicates its size and field 848 /// position information. 849 const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D); 850 851 /// getASTObjCInterfaceLayout - Get or compute information about the 852 /// layout of the specified Objective-C interface. 853 const ASTRecordLayout &getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D); 854 855 /// getASTObjCImplementationLayout - Get or compute information about 856 /// the layout of the specified Objective-C implementation. This may 857 /// differ from the interface if synthesized ivars are present. 858 const ASTRecordLayout & 859 getASTObjCImplementationLayout(const ObjCImplementationDecl *D); 860 861 /// getKeyFunction - Get the key function for the given record decl. 862 /// The key function is, according to the Itanium C++ ABI section 5.2.3: 863 /// 864 /// ...the first non-pure virtual function that is not inline at the point 865 /// of class definition. 866 const CXXMethodDecl *getKeyFunction(const CXXRecordDecl *RD); 867 868 void CollectObjCIvars(const ObjCInterfaceDecl *OI, 869 llvm::SmallVectorImpl<FieldDecl*> &Fields); 870 871 void ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI, 872 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars, 873 bool CollectSynthesized = true); 874 void CollectSynthesizedIvars(const ObjCInterfaceDecl *OI, 875 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars); 876 void CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD, 877 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars); 878 unsigned CountSynthesizedIvars(const ObjCInterfaceDecl *OI); 879 unsigned CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD); 880 void CollectInheritedProtocols(const Decl *CDecl, 881 llvm::SmallVectorImpl<ObjCProtocolDecl*> &Protocols); 882 883 //===--------------------------------------------------------------------===// 884 // Type Operators 885 //===--------------------------------------------------------------------===// 886 887 /// getCanonicalType - Return the canonical (structural) type corresponding to 888 /// the specified potentially non-canonical type. The non-canonical version 889 /// of a type may have many "decorated" versions of types. Decorators can 890 /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed 891 /// to be free of any of these, allowing two canonical types to be compared 892 /// for exact equality with a simple pointer comparison. 893 CanQualType getCanonicalType(QualType T); 894 const Type *getCanonicalType(const Type *T) { 895 return T->getCanonicalTypeInternal().getTypePtr(); 896 } 897 898 /// getCanonicalParamType - Return the canonical parameter type 899 /// corresponding to the specific potentially non-canonical one. 900 /// Qualifiers are stripped off, functions are turned into function 901 /// pointers, and arrays decay one level into pointers. 902 CanQualType getCanonicalParamType(QualType T); 903 904 /// \brief Determine whether the given types are equivalent. 905 bool hasSameType(QualType T1, QualType T2) { 906 return getCanonicalType(T1) == getCanonicalType(T2); 907 } 908 909 /// \brief Returns this type as a completely-unqualified array type, 910 /// capturing the qualifiers in Quals. This will remove the minimal amount of 911 /// sugaring from the types, similar to the behavior of 912 /// QualType::getUnqualifiedType(). 913 /// 914 /// \param T is the qualified type, which may be an ArrayType 915 /// 916 /// \param Quals will receive the full set of qualifiers that were 917 /// applied to the array. 918 /// 919 /// \returns if this is an array type, the completely unqualified array type 920 /// that corresponds to it. Otherwise, returns T.getUnqualifiedType(). 921 QualType getUnqualifiedArrayType(QualType T, Qualifiers &Quals); 922 923 /// \brief Determine whether the given types are equivalent after 924 /// cvr-qualifiers have been removed. 925 bool hasSameUnqualifiedType(QualType T1, QualType T2) { 926 CanQualType CT1 = getCanonicalType(T1); 927 CanQualType CT2 = getCanonicalType(T2); 928 929 Qualifiers Quals; 930 QualType UnqualT1 = getUnqualifiedArrayType(CT1, Quals); 931 QualType UnqualT2 = getUnqualifiedArrayType(CT2, Quals); 932 return UnqualT1 == UnqualT2; 933 } 934 935 /// \brief Retrieves the "canonical" declaration of 936 937 /// \brief Retrieves the "canonical" nested name specifier for a 938 /// given nested name specifier. 939 /// 940 /// The canonical nested name specifier is a nested name specifier 941 /// that uniquely identifies a type or namespace within the type 942 /// system. For example, given: 943 /// 944 /// \code 945 /// namespace N { 946 /// struct S { 947 /// template<typename T> struct X { typename T* type; }; 948 /// }; 949 /// } 950 /// 951 /// template<typename T> struct Y { 952 /// typename N::S::X<T>::type member; 953 /// }; 954 /// \endcode 955 /// 956 /// Here, the nested-name-specifier for N::S::X<T>:: will be 957 /// S::X<template-param-0-0>, since 'S' and 'X' are uniquely defined 958 /// by declarations in the type system and the canonical type for 959 /// the template type parameter 'T' is template-param-0-0. 960 NestedNameSpecifier * 961 getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS); 962 963 /// \brief Retrieves the "canonical" template name that refers to a 964 /// given template. 965 /// 966 /// The canonical template name is the simplest expression that can 967 /// be used to refer to a given template. For most templates, this 968 /// expression is just the template declaration itself. For example, 969 /// the template std::vector can be referred to via a variety of 970 /// names---std::vector, ::std::vector, vector (if vector is in 971 /// scope), etc.---but all of these names map down to the same 972 /// TemplateDecl, which is used to form the canonical template name. 973 /// 974 /// Dependent template names are more interesting. Here, the 975 /// template name could be something like T::template apply or 976 /// std::allocator<T>::template rebind, where the nested name 977 /// specifier itself is dependent. In this case, the canonical 978 /// template name uses the shortest form of the dependent 979 /// nested-name-specifier, which itself contains all canonical 980 /// types, values, and templates. 981 TemplateName getCanonicalTemplateName(TemplateName Name); 982 983 /// \brief Determine whether the given template names refer to the same 984 /// template. 985 bool hasSameTemplateName(TemplateName X, TemplateName Y); 986 987 /// \brief Retrieve the "canonical" template argument. 988 /// 989 /// The canonical template argument is the simplest template argument 990 /// (which may be a type, value, expression, or declaration) that 991 /// expresses the value of the argument. 992 TemplateArgument getCanonicalTemplateArgument(const TemplateArgument &Arg); 993 994 /// Type Query functions. If the type is an instance of the specified class, 995 /// return the Type pointer for the underlying maximally pretty type. This 996 /// is a member of ASTContext because this may need to do some amount of 997 /// canonicalization, e.g. to move type qualifiers into the element type. 998 const ArrayType *getAsArrayType(QualType T); 999 const ConstantArrayType *getAsConstantArrayType(QualType T) { 1000 return dyn_cast_or_null<ConstantArrayType>(getAsArrayType(T)); 1001 } 1002 const VariableArrayType *getAsVariableArrayType(QualType T) { 1003 return dyn_cast_or_null<VariableArrayType>(getAsArrayType(T)); 1004 } 1005 const IncompleteArrayType *getAsIncompleteArrayType(QualType T) { 1006 return dyn_cast_or_null<IncompleteArrayType>(getAsArrayType(T)); 1007 } 1008 const DependentSizedArrayType *getAsDependentSizedArrayType(QualType T) { 1009 return dyn_cast_or_null<DependentSizedArrayType>(getAsArrayType(T)); 1010 } 1011 1012 /// getBaseElementType - Returns the innermost element type of an array type. 1013 /// For example, will return "int" for int[m][n] 1014 QualType getBaseElementType(const ArrayType *VAT); 1015 1016 /// getBaseElementType - Returns the innermost element type of a type 1017 /// (which needn't actually be an array type). 1018 QualType getBaseElementType(QualType QT); 1019 1020 /// getConstantArrayElementCount - Returns number of constant array elements. 1021 uint64_t getConstantArrayElementCount(const ConstantArrayType *CA) const; 1022 1023 /// getArrayDecayedType - Return the properly qualified result of decaying the 1024 /// specified array type to a pointer. This operation is non-trivial when 1025 /// handling typedefs etc. The canonical type of "T" must be an array type, 1026 /// this returns a pointer to a properly qualified element of the array. 1027 /// 1028 /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. 1029 QualType getArrayDecayedType(QualType T); 1030 1031 /// getPromotedIntegerType - Returns the type that Promotable will 1032 /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable 1033 /// integer type. 1034 QualType getPromotedIntegerType(QualType PromotableType); 1035 1036 /// \brief Whether this is a promotable bitfield reference according 1037 /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). 1038 /// 1039 /// \returns the type this bit-field will promote to, or NULL if no 1040 /// promotion occurs. 1041 QualType isPromotableBitField(Expr *E); 1042 1043 /// getIntegerTypeOrder - Returns the highest ranked integer type: 1044 /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If 1045 /// LHS < RHS, return -1. 1046 int getIntegerTypeOrder(QualType LHS, QualType RHS); 1047 1048 /// getFloatingTypeOrder - Compare the rank of the two specified floating 1049 /// point types, ignoring the domain of the type (i.e. 'double' == 1050 /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If 1051 /// LHS < RHS, return -1. 1052 int getFloatingTypeOrder(QualType LHS, QualType RHS); 1053 1054 /// getFloatingTypeOfSizeWithinDomain - Returns a real floating 1055 /// point or a complex type (based on typeDomain/typeSize). 1056 /// 'typeDomain' is a real floating point or complex type. 1057 /// 'typeSize' is a real floating point or complex type. 1058 QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize, 1059 QualType typeDomain) const; 1060 1061private: 1062 // Helper for integer ordering 1063 unsigned getIntegerRank(Type* T); 1064 1065public: 1066 1067 //===--------------------------------------------------------------------===// 1068 // Type Compatibility Predicates 1069 //===--------------------------------------------------------------------===// 1070 1071 /// Compatibility predicates used to check assignment expressions. 1072 bool typesAreCompatible(QualType, QualType); // C99 6.2.7p1 1073 1074 bool isObjCIdType(QualType T) const { 1075 return T == ObjCIdTypedefType; 1076 } 1077 bool isObjCClassType(QualType T) const { 1078 return T == ObjCClassTypedefType; 1079 } 1080 bool isObjCSelType(QualType T) const { 1081 return T == ObjCSelTypedefType; 1082 } 1083 bool QualifiedIdConformsQualifiedId(QualType LHS, QualType RHS); 1084 bool ObjCQualifiedIdTypesAreCompatible(QualType LHS, QualType RHS, 1085 bool ForCompare); 1086 1087 // Check the safety of assignment from LHS to RHS 1088 bool canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, 1089 const ObjCObjectPointerType *RHSOPT); 1090 bool canAssignObjCInterfaces(const ObjCInterfaceType *LHS, 1091 const ObjCInterfaceType *RHS); 1092 bool areComparableObjCPointerTypes(QualType LHS, QualType RHS); 1093 QualType areCommonBaseCompatible(const ObjCObjectPointerType *LHSOPT, 1094 const ObjCObjectPointerType *RHSOPT); 1095 1096 // Functions for calculating composite types 1097 QualType mergeTypes(QualType, QualType); 1098 QualType mergeFunctionTypes(QualType, QualType); 1099 1100 /// UsualArithmeticConversionsType - handles the various conversions 1101 /// that are common to binary operators (C99 6.3.1.8, C++ [expr]p9) 1102 /// and returns the result type of that conversion. 1103 QualType UsualArithmeticConversionsType(QualType lhs, QualType rhs); 1104 1105 //===--------------------------------------------------------------------===// 1106 // Integer Predicates 1107 //===--------------------------------------------------------------------===// 1108 1109 // The width of an integer, as defined in C99 6.2.6.2. This is the number 1110 // of bits in an integer type excluding any padding bits. 1111 unsigned getIntWidth(QualType T); 1112 1113 // Per C99 6.2.5p6, for every signed integer type, there is a corresponding 1114 // unsigned integer type. This method takes a signed type, and returns the 1115 // corresponding unsigned integer type. 1116 QualType getCorrespondingUnsignedType(QualType T); 1117 1118 //===--------------------------------------------------------------------===// 1119 // Type Iterators. 1120 //===--------------------------------------------------------------------===// 1121 1122 typedef std::vector<Type*>::iterator type_iterator; 1123 typedef std::vector<Type*>::const_iterator const_type_iterator; 1124 1125 type_iterator types_begin() { return Types.begin(); } 1126 type_iterator types_end() { return Types.end(); } 1127 const_type_iterator types_begin() const { return Types.begin(); } 1128 const_type_iterator types_end() const { return Types.end(); } 1129 1130 //===--------------------------------------------------------------------===// 1131 // Integer Values 1132 //===--------------------------------------------------------------------===// 1133 1134 /// MakeIntValue - Make an APSInt of the appropriate width and 1135 /// signedness for the given \arg Value and integer \arg Type. 1136 llvm::APSInt MakeIntValue(uint64_t Value, QualType Type) { 1137 llvm::APSInt Res(getIntWidth(Type), !Type->isSignedIntegerType()); 1138 Res = Value; 1139 return Res; 1140 } 1141 1142 /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. 1143 ObjCImplementationDecl *getObjCImplementation(ObjCInterfaceDecl *D); 1144 /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. 1145 ObjCCategoryImplDecl *getObjCImplementation(ObjCCategoryDecl *D); 1146 1147 /// \brief Set the implementation of ObjCInterfaceDecl. 1148 void setObjCImplementation(ObjCInterfaceDecl *IFaceD, 1149 ObjCImplementationDecl *ImplD); 1150 /// \brief Set the implementation of ObjCCategoryDecl. 1151 void setObjCImplementation(ObjCCategoryDecl *CatD, 1152 ObjCCategoryImplDecl *ImplD); 1153 1154 /// \brief Allocate an uninitialized TypeSourceInfo. 1155 /// 1156 /// The caller should initialize the memory held by TypeSourceInfo using 1157 /// the TypeLoc wrappers. 1158 /// 1159 /// \param T the type that will be the basis for type source info. This type 1160 /// should refer to how the declarator was written in source code, not to 1161 /// what type semantic analysis resolved the declarator to. 1162 /// 1163 /// \param Size the size of the type info to create, or 0 if the size 1164 /// should be calculated based on the type. 1165 TypeSourceInfo *CreateTypeSourceInfo(QualType T, unsigned Size = 0); 1166 1167 /// \brief Allocate a TypeSourceInfo where all locations have been 1168 /// initialized to a given location, which defaults to the empty 1169 /// location. 1170 TypeSourceInfo * 1171 getTrivialTypeSourceInfo(QualType T, SourceLocation Loc = SourceLocation()); 1172 1173private: 1174 ASTContext(const ASTContext&); // DO NOT IMPLEMENT 1175 void operator=(const ASTContext&); // DO NOT IMPLEMENT 1176 1177 void InitBuiltinTypes(); 1178 void InitBuiltinType(CanQualType &R, BuiltinType::Kind K); 1179 1180 // Return the ObjC type encoding for a given type. 1181 void getObjCEncodingForTypeImpl(QualType t, std::string &S, 1182 bool ExpandPointedToStructures, 1183 bool ExpandStructures, 1184 const FieldDecl *Field, 1185 bool OutermostType = false, 1186 bool EncodingProperty = false); 1187 1188 const ASTRecordLayout &getObjCLayout(const ObjCInterfaceDecl *D, 1189 const ObjCImplementationDecl *Impl); 1190}; 1191 1192/// @brief Utility function for constructing a nullary selector. 1193static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) { 1194 IdentifierInfo* II = &Ctx.Idents.get(name); 1195 return Ctx.Selectors.getSelector(0, &II); 1196} 1197 1198/// @brief Utility function for constructing an unary selector. 1199static inline Selector GetUnarySelector(const char* name, ASTContext& Ctx) { 1200 IdentifierInfo* II = &Ctx.Idents.get(name); 1201 return Ctx.Selectors.getSelector(1, &II); 1202} 1203 1204} // end namespace clang 1205 1206// operator new and delete aren't allowed inside namespaces. 1207// The throw specifications are mandated by the standard. 1208/// @brief Placement new for using the ASTContext's allocator. 1209/// 1210/// This placement form of operator new uses the ASTContext's allocator for 1211/// obtaining memory. It is a non-throwing new, which means that it returns 1212/// null on error. (If that is what the allocator does. The current does, so if 1213/// this ever changes, this operator will have to be changed, too.) 1214/// Usage looks like this (assuming there's an ASTContext 'Context' in scope): 1215/// @code 1216/// // Default alignment (16) 1217/// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments); 1218/// // Specific alignment 1219/// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments); 1220/// @endcode 1221/// Please note that you cannot use delete on the pointer; it must be 1222/// deallocated using an explicit destructor call followed by 1223/// @c Context.Deallocate(Ptr). 1224/// 1225/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 1226/// @param C The ASTContext that provides the allocator. 1227/// @param Alignment The alignment of the allocated memory (if the underlying 1228/// allocator supports it). 1229/// @return The allocated memory. Could be NULL. 1230inline void *operator new(size_t Bytes, clang::ASTContext &C, 1231 size_t Alignment) throw () { 1232 return C.Allocate(Bytes, Alignment); 1233} 1234/// @brief Placement delete companion to the new above. 1235/// 1236/// This operator is just a companion to the new above. There is no way of 1237/// invoking it directly; see the new operator for more details. This operator 1238/// is called implicitly by the compiler if a placement new expression using 1239/// the ASTContext throws in the object constructor. 1240inline void operator delete(void *Ptr, clang::ASTContext &C, size_t) 1241 throw () { 1242 C.Deallocate(Ptr); 1243} 1244 1245/// This placement form of operator new[] uses the ASTContext's allocator for 1246/// obtaining memory. It is a non-throwing new[], which means that it returns 1247/// null on error. 1248/// Usage looks like this (assuming there's an ASTContext 'Context' in scope): 1249/// @code 1250/// // Default alignment (16) 1251/// char *data = new (Context) char[10]; 1252/// // Specific alignment 1253/// char *data = new (Context, 8) char[10]; 1254/// @endcode 1255/// Please note that you cannot use delete on the pointer; it must be 1256/// deallocated using an explicit destructor call followed by 1257/// @c Context.Deallocate(Ptr). 1258/// 1259/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 1260/// @param C The ASTContext that provides the allocator. 1261/// @param Alignment The alignment of the allocated memory (if the underlying 1262/// allocator supports it). 1263/// @return The allocated memory. Could be NULL. 1264inline void *operator new[](size_t Bytes, clang::ASTContext& C, 1265 size_t Alignment = 16) throw () { 1266 return C.Allocate(Bytes, Alignment); 1267} 1268 1269/// @brief Placement delete[] companion to the new[] above. 1270/// 1271/// This operator is just a companion to the new[] above. There is no way of 1272/// invoking it directly; see the new[] operator for more details. This operator 1273/// is called implicitly by the compiler if a placement new[] expression using 1274/// the ASTContext throws in the object constructor. 1275inline void operator delete[](void *Ptr, clang::ASTContext &C, size_t) 1276 throw () { 1277 C.Deallocate(Ptr); 1278} 1279 1280#endif 1281