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