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