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