ASTContext.h revision 5d484e8cf710207010720589d89602233de61d01
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 ObjCSelTypedefType; 124 125 /// ObjCProtoType - another pseudo built-in typedef type (set by Sema). 126 QualType ObjCProtoType; 127 const RecordType *ProtoStructType; 128 129 /// ObjCClassType - another pseudo built-in typedef type (set by Sema). 130 QualType ObjCClassTypedefType; 131 132 QualType ObjCConstantStringType; 133 RecordDecl *CFConstantStringTypeDecl; 134 135 RecordDecl *ObjCFastEnumerationStateTypeDecl; 136 137 /// \brief The type for the C FILE type. 138 TypeDecl *FILEDecl; 139 140 /// \brief The type for the C jmp_buf type. 141 TypeDecl *jmp_bufDecl; 142 143 /// \brief The type for the C sigjmp_buf type. 144 TypeDecl *sigjmp_bufDecl; 145 146 /// \brief Type for the Block descriptor for Blocks CodeGen. 147 RecordDecl *BlockDescriptorType; 148 149 /// \brief Type for the Block descriptor for Blocks CodeGen. 150 RecordDecl *BlockDescriptorExtendedType; 151 152 /// \brief Keeps track of all declaration attributes. 153 /// 154 /// Since so few decls have attrs, we keep them in a hash map instead of 155 /// wasting space in the Decl class. 156 llvm::DenseMap<const Decl*, Attr*> DeclAttrs; 157 158 /// \brief Keeps track of the static data member templates from which 159 /// static data members of class template specializations were instantiated. 160 /// 161 /// This data structure stores the mapping from instantiations of static 162 /// data members to the static data member representations within the 163 /// class template from which they were instantiated along with the kind 164 /// of instantiation or specialization (a TemplateSpecializationKind - 1). 165 /// 166 /// Given the following example: 167 /// 168 /// \code 169 /// template<typename T> 170 /// struct X { 171 /// static T value; 172 /// }; 173 /// 174 /// template<typename T> 175 /// T X<T>::value = T(17); 176 /// 177 /// int *x = &X<int>::value; 178 /// \endcode 179 /// 180 /// This mapping will contain an entry that maps from the VarDecl for 181 /// X<int>::value to the corresponding VarDecl for X<T>::value (within the 182 /// class template X) and will be marked TSK_ImplicitInstantiation. 183 llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *> 184 InstantiatedFromStaticDataMember; 185 186 /// \brief Keeps track of the UnresolvedUsingDecls from which UsingDecls 187 /// where created during instantiation. 188 /// 189 /// For example: 190 /// \code 191 /// template<typename T> 192 /// struct A { 193 /// void f(); 194 /// }; 195 /// 196 /// template<typename T> 197 /// struct B : A<T> { 198 /// using A<T>::f; 199 /// }; 200 /// 201 /// template struct B<int>; 202 /// \endcode 203 /// 204 /// This mapping will contain an entry that maps from the UsingDecl in 205 /// B<int> to the UnresolvedUsingDecl in B<T>. 206 llvm::DenseMap<UsingDecl *, NamedDecl *> 207 InstantiatedFromUnresolvedUsingDecl; 208 209 llvm::DenseMap<FieldDecl *, FieldDecl *> InstantiatedFromUnnamedFieldDecl; 210 211 TranslationUnitDecl *TUDecl; 212 213 /// SourceMgr - The associated SourceManager object. 214 SourceManager &SourceMgr; 215 216 /// LangOpts - The language options used to create the AST associated with 217 /// this ASTContext object. 218 LangOptions LangOpts; 219 220 /// \brief Whether we have already loaded comment source ranges from an 221 /// external source. 222 bool LoadedExternalComments; 223 224 /// MallocAlloc/BumpAlloc - The allocator objects used to create AST objects. 225 bool FreeMemory; 226 llvm::MallocAllocator MallocAlloc; 227 llvm::BumpPtrAllocator BumpAlloc; 228 229 /// \brief Mapping from declarations to their comments, once we have 230 /// already looked up the comment associated with a given declaration. 231 llvm::DenseMap<const Decl *, std::string> DeclComments; 232 233public: 234 const TargetInfo &Target; 235 IdentifierTable &Idents; 236 SelectorTable &Selectors; 237 Builtin::Context &BuiltinInfo; 238 DeclarationNameTable DeclarationNames; 239 llvm::OwningPtr<ExternalASTSource> ExternalSource; 240 clang::PrintingPolicy PrintingPolicy; 241 242 // Typedefs which may be provided defining the structure of Objective-C 243 // pseudo-builtins 244 QualType ObjCIdRedefinitionType; 245 QualType ObjCClassRedefinitionType; 246 QualType ObjCSELRedefinitionType; 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, ObjCBuiltinSelTy; 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 TemplateArgumentListInfo &Args, 536 QualType Canon = QualType()); 537 538 QualType getQualifiedNameType(NestedNameSpecifier *NNS, 539 QualType NamedType); 540 QualType getTypenameType(NestedNameSpecifier *NNS, 541 const IdentifierInfo *Name, 542 QualType Canon = QualType()); 543 QualType getTypenameType(NestedNameSpecifier *NNS, 544 const TemplateSpecializationType *TemplateId, 545 QualType Canon = QualType()); 546 QualType getElaboratedType(QualType UnderlyingType, 547 ElaboratedType::TagKind Tag); 548 549 QualType getObjCInterfaceType(const ObjCInterfaceDecl *Decl, 550 ObjCProtocolDecl **Protocols = 0, 551 unsigned NumProtocols = 0); 552 553 /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for the 554 /// given interface decl and the conforming protocol list. 555 QualType getObjCObjectPointerType(QualType OIT, 556 ObjCProtocolDecl **ProtocolList = 0, 557 unsigned NumProtocols = 0); 558 559 /// getTypeOfType - GCC extension. 560 QualType getTypeOfExprType(Expr *e); 561 QualType getTypeOfType(QualType t); 562 563 /// getDecltypeType - C++0x decltype. 564 QualType getDecltypeType(Expr *e); 565 566 /// getTagDeclType - Return the unique reference to the type for the 567 /// specified TagDecl (struct/union/class/enum) decl. 568 QualType getTagDeclType(const TagDecl *Decl); 569 570 /// getSizeType - Return the unique type for "size_t" (C99 7.17), defined 571 /// in <stddef.h>. The sizeof operator requires this (C99 6.5.3.4p4). 572 QualType getSizeType() const; 573 574 /// getWCharType - In C++, this returns the unique wchar_t type. In C99, this 575 /// returns a type compatible with the type defined in <stddef.h> as defined 576 /// by the target. 577 QualType getWCharType() const { return WCharTy; } 578 579 /// getSignedWCharType - Return the type of "signed wchar_t". 580 /// Used when in C++, as a GCC extension. 581 QualType getSignedWCharType() const; 582 583 /// getUnsignedWCharType - Return the type of "unsigned wchar_t". 584 /// Used when in C++, as a GCC extension. 585 QualType getUnsignedWCharType() const; 586 587 /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) 588 /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). 589 QualType getPointerDiffType() const; 590 591 // getCFConstantStringType - Return the C structure type used to represent 592 // constant CFStrings. 593 QualType getCFConstantStringType(); 594 595 /// Get the structure type used to representation CFStrings, or NULL 596 /// if it hasn't yet been built. 597 QualType getRawCFConstantStringType() { 598 if (CFConstantStringTypeDecl) 599 return getTagDeclType(CFConstantStringTypeDecl); 600 return QualType(); 601 } 602 void setCFConstantStringType(QualType T); 603 604 // This setter/getter represents the ObjC type for an NSConstantString. 605 void setObjCConstantStringInterface(ObjCInterfaceDecl *Decl); 606 QualType getObjCConstantStringInterface() const { 607 return ObjCConstantStringType; 608 } 609 610 //// This gets the struct used to keep track of fast enumerations. 611 QualType getObjCFastEnumerationStateType(); 612 613 /// Get the ObjCFastEnumerationState type, or NULL if it hasn't yet 614 /// been built. 615 QualType getRawObjCFastEnumerationStateType() { 616 if (ObjCFastEnumerationStateTypeDecl) 617 return getTagDeclType(ObjCFastEnumerationStateTypeDecl); 618 return QualType(); 619 } 620 621 void setObjCFastEnumerationStateType(QualType T); 622 623 /// \brief Set the type for the C FILE type. 624 void setFILEDecl(TypeDecl *FILEDecl) { this->FILEDecl = FILEDecl; } 625 626 /// \brief Retrieve the C FILE type. 627 QualType getFILEType() { 628 if (FILEDecl) 629 return getTypeDeclType(FILEDecl); 630 return QualType(); 631 } 632 633 /// \brief Set the type for the C jmp_buf type. 634 void setjmp_bufDecl(TypeDecl *jmp_bufDecl) { 635 this->jmp_bufDecl = jmp_bufDecl; 636 } 637 638 /// \brief Retrieve the C jmp_buf type. 639 QualType getjmp_bufType() { 640 if (jmp_bufDecl) 641 return getTypeDeclType(jmp_bufDecl); 642 return QualType(); 643 } 644 645 /// \brief Set the type for the C sigjmp_buf type. 646 void setsigjmp_bufDecl(TypeDecl *sigjmp_bufDecl) { 647 this->sigjmp_bufDecl = sigjmp_bufDecl; 648 } 649 650 /// \brief Retrieve the C sigjmp_buf type. 651 QualType getsigjmp_bufType() { 652 if (sigjmp_bufDecl) 653 return getTypeDeclType(sigjmp_bufDecl); 654 return QualType(); 655 } 656 657 /// getObjCEncodingForType - Emit the ObjC type encoding for the 658 /// given type into \arg S. If \arg NameFields is specified then 659 /// record field names are also encoded. 660 void getObjCEncodingForType(QualType t, std::string &S, 661 const FieldDecl *Field=0); 662 663 void getLegacyIntegralTypeEncoding(QualType &t) const; 664 665 // Put the string version of type qualifiers into S. 666 void getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, 667 std::string &S) const; 668 669 /// getObjCEncodingForMethodDecl - Return the encoded type for this method 670 /// declaration. 671 void getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, std::string &S); 672 673 /// getObjCEncodingForBlockDecl - Return the encoded type for this block 674 /// declaration. 675 void getObjCEncodingForBlock(const BlockExpr *Expr, std::string& S); 676 677 /// getObjCEncodingForPropertyDecl - Return the encoded type for 678 /// this method declaration. If non-NULL, Container must be either 679 /// an ObjCCategoryImplDecl or ObjCImplementationDecl; it should 680 /// only be NULL when getting encodings for protocol properties. 681 void getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, 682 const Decl *Container, 683 std::string &S); 684 685 bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, 686 ObjCProtocolDecl *rProto); 687 688 /// getObjCEncodingTypeSize returns size of type for objective-c encoding 689 /// purpose. 690 int getObjCEncodingTypeSize(QualType t); 691 692 /// This setter/getter represents the ObjC 'id' type. It is setup lazily, by 693 /// Sema. id is always a (typedef for a) pointer type, a pointer to a struct. 694 QualType getObjCIdType() const { return ObjCIdTypedefType; } 695 void setObjCIdType(QualType T); 696 697 void setObjCSelType(QualType T); 698 QualType getObjCSelType() const { return ObjCSelTypedefType; } 699 700 void setObjCProtoType(QualType QT); 701 QualType getObjCProtoType() const { return ObjCProtoType; } 702 703 /// This setter/getter repreents the ObjC 'Class' type. It is setup lazily, by 704 /// Sema. 'Class' is always a (typedef for a) pointer type, a pointer to a 705 /// struct. 706 QualType getObjCClassType() const { return ObjCClassTypedefType; } 707 void setObjCClassType(QualType T); 708 709 void setBuiltinVaListType(QualType T); 710 QualType getBuiltinVaListType() const { return BuiltinVaListType; } 711 712 QualType getFixedWidthIntType(unsigned Width, bool Signed); 713 714 /// getCVRQualifiedType - Returns a type with additional const, 715 /// volatile, or restrict qualifiers. 716 QualType getCVRQualifiedType(QualType T, unsigned CVR) { 717 return getQualifiedType(T, Qualifiers::fromCVRMask(CVR)); 718 } 719 720 /// getQualifiedType - Returns a type with additional qualifiers. 721 QualType getQualifiedType(QualType T, Qualifiers Qs) { 722 if (!Qs.hasNonFastQualifiers()) 723 return T.withFastQualifiers(Qs.getFastQualifiers()); 724 QualifierCollector Qc(Qs); 725 const Type *Ptr = Qc.strip(T); 726 return getExtQualType(Ptr, Qc); 727 } 728 729 /// getQualifiedType - Returns a type with additional qualifiers. 730 QualType getQualifiedType(const Type *T, Qualifiers Qs) { 731 if (!Qs.hasNonFastQualifiers()) 732 return QualType(T, Qs.getFastQualifiers()); 733 return getExtQualType(T, Qs); 734 } 735 736 TemplateName getQualifiedTemplateName(NestedNameSpecifier *NNS, 737 bool TemplateKeyword, 738 TemplateDecl *Template); 739 TemplateName getQualifiedTemplateName(NestedNameSpecifier *NNS, 740 bool TemplateKeyword, 741 OverloadedFunctionDecl *Template); 742 743 TemplateName getDependentTemplateName(NestedNameSpecifier *NNS, 744 const IdentifierInfo *Name); 745 TemplateName getDependentTemplateName(NestedNameSpecifier *NNS, 746 OverloadedOperatorKind Operator); 747 748 enum GetBuiltinTypeError { 749 GE_None, //< No error 750 GE_Missing_stdio, //< Missing a type from <stdio.h> 751 GE_Missing_setjmp //< Missing a type from <setjmp.h> 752 }; 753 754 /// GetBuiltinType - Return the type for the specified builtin. 755 QualType GetBuiltinType(unsigned ID, GetBuiltinTypeError &Error); 756 757private: 758 CanQualType getFromTargetType(unsigned Type) const; 759 760 //===--------------------------------------------------------------------===// 761 // Type Predicates. 762 //===--------------------------------------------------------------------===// 763 764public: 765 /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's 766 /// garbage collection attribute. 767 /// 768 Qualifiers::GC getObjCGCAttrKind(const QualType &Ty) const; 769 770 /// isObjCNSObjectType - Return true if this is an NSObject object with 771 /// its NSObject attribute set. 772 bool isObjCNSObjectType(QualType Ty) const; 773 774 //===--------------------------------------------------------------------===// 775 // Type Sizing and Analysis 776 //===--------------------------------------------------------------------===// 777 778 /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified 779 /// scalar floating point type. 780 const llvm::fltSemantics &getFloatTypeSemantics(QualType T) const; 781 782 /// getTypeInfo - Get the size and alignment of the specified complete type in 783 /// bits. 784 std::pair<uint64_t, unsigned> getTypeInfo(const Type *T); 785 std::pair<uint64_t, unsigned> getTypeInfo(QualType T) { 786 return getTypeInfo(T.getTypePtr()); 787 } 788 789 /// getTypeSize - Return the size of the specified type, in bits. This method 790 /// does not work on incomplete types. 791 uint64_t getTypeSize(QualType T) { 792 return getTypeInfo(T).first; 793 } 794 uint64_t getTypeSize(const Type *T) { 795 return getTypeInfo(T).first; 796 } 797 798 /// getTypeAlign - Return the ABI-specified alignment of a type, in bits. 799 /// This method does not work on incomplete types. 800 unsigned getTypeAlign(QualType T) { 801 return getTypeInfo(T).second; 802 } 803 unsigned getTypeAlign(const Type *T) { 804 return getTypeInfo(T).second; 805 } 806 807 /// getPreferredTypeAlign - Return the "preferred" alignment of the specified 808 /// type for the current target in bits. This can be different than the ABI 809 /// alignment in cases where it is beneficial for performance to overalign 810 /// a data type. 811 unsigned getPreferredTypeAlign(const Type *T); 812 813 unsigned getDeclAlignInBytes(const Decl *D, bool RefAsPointee = false); 814 815 /// getASTRecordLayout - Get or compute information about the layout of the 816 /// specified record (struct/union/class), which indicates its size and field 817 /// position information. 818 const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D); 819 820 /// getASTObjCInterfaceLayout - Get or compute information about the 821 /// layout of the specified Objective-C interface. 822 const ASTRecordLayout &getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D); 823 824 /// getASTObjCImplementationLayout - Get or compute information about 825 /// the layout of the specified Objective-C implementation. This may 826 /// differ from the interface if synthesized ivars are present. 827 const ASTRecordLayout & 828 getASTObjCImplementationLayout(const ObjCImplementationDecl *D); 829 830 void CollectObjCIvars(const ObjCInterfaceDecl *OI, 831 llvm::SmallVectorImpl<FieldDecl*> &Fields); 832 833 void ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI, 834 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars, 835 bool CollectSynthesized = true); 836 void CollectSynthesizedIvars(const ObjCInterfaceDecl *OI, 837 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars); 838 void CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD, 839 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars); 840 unsigned CountSynthesizedIvars(const ObjCInterfaceDecl *OI); 841 unsigned CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD); 842 void CollectInheritedProtocols(const Decl *CDecl, 843 llvm::SmallVectorImpl<ObjCProtocolDecl*> &Protocols); 844 845 //===--------------------------------------------------------------------===// 846 // Type Operators 847 //===--------------------------------------------------------------------===// 848 849 /// getCanonicalType - Return the canonical (structural) type corresponding to 850 /// the specified potentially non-canonical type. The non-canonical version 851 /// of a type may have many "decorated" versions of types. Decorators can 852 /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed 853 /// to be free of any of these, allowing two canonical types to be compared 854 /// for exact equality with a simple pointer comparison. 855 CanQualType getCanonicalType(QualType T); 856 const Type *getCanonicalType(const Type *T) { 857 return T->getCanonicalTypeInternal().getTypePtr(); 858 } 859 860 /// getCanonicalParamType - Return the canonical parameter type 861 /// corresponding to the specific potentially non-canonical one. 862 /// Qualifiers are stripped off, functions are turned into function 863 /// pointers, and arrays decay one level into pointers. 864 CanQualType getCanonicalParamType(QualType T); 865 866 /// \brief Determine whether the given types are equivalent. 867 bool hasSameType(QualType T1, QualType T2) { 868 return getCanonicalType(T1) == getCanonicalType(T2); 869 } 870 871 /// \brief Determine whether the given types are equivalent after 872 /// cvr-qualifiers have been removed. 873 bool hasSameUnqualifiedType(QualType T1, QualType T2) { 874 CanQualType CT1 = getCanonicalType(T1); 875 CanQualType CT2 = getCanonicalType(T2); 876 return CT1.getUnqualifiedType() == CT2.getUnqualifiedType(); 877 } 878 879 /// \brief Retrieves the "canonical" declaration of 880 881 /// \brief Retrieves the "canonical" nested name specifier for a 882 /// given nested name specifier. 883 /// 884 /// The canonical nested name specifier is a nested name specifier 885 /// that uniquely identifies a type or namespace within the type 886 /// system. For example, given: 887 /// 888 /// \code 889 /// namespace N { 890 /// struct S { 891 /// template<typename T> struct X { typename T* type; }; 892 /// }; 893 /// } 894 /// 895 /// template<typename T> struct Y { 896 /// typename N::S::X<T>::type member; 897 /// }; 898 /// \endcode 899 /// 900 /// Here, the nested-name-specifier for N::S::X<T>:: will be 901 /// S::X<template-param-0-0>, since 'S' and 'X' are uniquely defined 902 /// by declarations in the type system and the canonical type for 903 /// the template type parameter 'T' is template-param-0-0. 904 NestedNameSpecifier * 905 getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS); 906 907 /// \brief Retrieves the "canonical" template name that refers to a 908 /// given template. 909 /// 910 /// The canonical template name is the simplest expression that can 911 /// be used to refer to a given template. For most templates, this 912 /// expression is just the template declaration itself. For example, 913 /// the template std::vector can be referred to via a variety of 914 /// names---std::vector, ::std::vector, vector (if vector is in 915 /// scope), etc.---but all of these names map down to the same 916 /// TemplateDecl, which is used to form the canonical template name. 917 /// 918 /// Dependent template names are more interesting. Here, the 919 /// template name could be something like T::template apply or 920 /// std::allocator<T>::template rebind, where the nested name 921 /// specifier itself is dependent. In this case, the canonical 922 /// template name uses the shortest form of the dependent 923 /// nested-name-specifier, which itself contains all canonical 924 /// types, values, and templates. 925 TemplateName getCanonicalTemplateName(TemplateName Name); 926 927 /// \brief Determine whether the given template names refer to the same 928 /// template. 929 bool hasSameTemplateName(TemplateName X, TemplateName Y); 930 931 /// \brief Retrieve the "canonical" template argument. 932 /// 933 /// The canonical template argument is the simplest template argument 934 /// (which may be a type, value, expression, or declaration) that 935 /// expresses the value of the argument. 936 TemplateArgument getCanonicalTemplateArgument(const TemplateArgument &Arg); 937 938 /// Type Query functions. If the type is an instance of the specified class, 939 /// return the Type pointer for the underlying maximally pretty type. This 940 /// is a member of ASTContext because this may need to do some amount of 941 /// canonicalization, e.g. to move type qualifiers into the element type. 942 const ArrayType *getAsArrayType(QualType T); 943 const ConstantArrayType *getAsConstantArrayType(QualType T) { 944 return dyn_cast_or_null<ConstantArrayType>(getAsArrayType(T)); 945 } 946 const VariableArrayType *getAsVariableArrayType(QualType T) { 947 return dyn_cast_or_null<VariableArrayType>(getAsArrayType(T)); 948 } 949 const IncompleteArrayType *getAsIncompleteArrayType(QualType T) { 950 return dyn_cast_or_null<IncompleteArrayType>(getAsArrayType(T)); 951 } 952 953 /// getBaseElementType - Returns the innermost element type of an array type. 954 /// For example, will return "int" for int[m][n] 955 QualType getBaseElementType(const ArrayType *VAT); 956 957 /// getBaseElementType - Returns the innermost element type of a type 958 /// (which needn't actually be an array type). 959 QualType getBaseElementType(QualType QT); 960 961 /// getConstantArrayElementCount - Returns number of constant array elements. 962 uint64_t getConstantArrayElementCount(const ConstantArrayType *CA) const; 963 964 /// getArrayDecayedType - Return the properly qualified result of decaying the 965 /// specified array type to a pointer. This operation is non-trivial when 966 /// handling typedefs etc. The canonical type of "T" must be an array type, 967 /// this returns a pointer to a properly qualified element of the array. 968 /// 969 /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. 970 QualType getArrayDecayedType(QualType T); 971 972 /// getPromotedIntegerType - Returns the type that Promotable will 973 /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable 974 /// integer type. 975 QualType getPromotedIntegerType(QualType PromotableType); 976 977 /// \brief Whether this is a promotable bitfield reference according 978 /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). 979 /// 980 /// \returns the type this bit-field will promote to, or NULL if no 981 /// promotion occurs. 982 QualType isPromotableBitField(Expr *E); 983 984 /// getIntegerTypeOrder - Returns the highest ranked integer type: 985 /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If 986 /// LHS < RHS, return -1. 987 int getIntegerTypeOrder(QualType LHS, QualType RHS); 988 989 /// getFloatingTypeOrder - Compare the rank of the two specified floating 990 /// point types, ignoring the domain of the type (i.e. 'double' == 991 /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If 992 /// LHS < RHS, return -1. 993 int getFloatingTypeOrder(QualType LHS, QualType RHS); 994 995 /// getFloatingTypeOfSizeWithinDomain - Returns a real floating 996 /// point or a complex type (based on typeDomain/typeSize). 997 /// 'typeDomain' is a real floating point or complex type. 998 /// 'typeSize' is a real floating point or complex type. 999 QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize, 1000 QualType typeDomain) const; 1001 1002private: 1003 // Helper for integer ordering 1004 unsigned getIntegerRank(Type* T); 1005 1006public: 1007 1008 //===--------------------------------------------------------------------===// 1009 // Type Compatibility Predicates 1010 //===--------------------------------------------------------------------===// 1011 1012 /// Compatibility predicates used to check assignment expressions. 1013 bool typesAreCompatible(QualType, QualType); // C99 6.2.7p1 1014 1015 bool isObjCIdType(QualType T) const { 1016 return T == ObjCIdTypedefType; 1017 } 1018 bool isObjCClassType(QualType T) const { 1019 return T == ObjCClassTypedefType; 1020 } 1021 bool isObjCSelType(QualType T) const { 1022 return T == ObjCSelTypedefType; 1023 } 1024 bool QualifiedIdConformsQualifiedId(QualType LHS, QualType RHS); 1025 bool ObjCQualifiedIdTypesAreCompatible(QualType LHS, QualType RHS, 1026 bool ForCompare); 1027 1028 // Check the safety of assignment from LHS to RHS 1029 bool canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, 1030 const ObjCObjectPointerType *RHSOPT); 1031 bool canAssignObjCInterfaces(const ObjCInterfaceType *LHS, 1032 const ObjCInterfaceType *RHS); 1033 bool areComparableObjCPointerTypes(QualType LHS, QualType RHS); 1034 QualType areCommonBaseCompatible(const ObjCObjectPointerType *LHSOPT, 1035 const ObjCObjectPointerType *RHSOPT); 1036 1037 // Functions for calculating composite types 1038 QualType mergeTypes(QualType, QualType); 1039 QualType mergeFunctionTypes(QualType, QualType); 1040 1041 /// UsualArithmeticConversionsType - handles the various conversions 1042 /// that are common to binary operators (C99 6.3.1.8, C++ [expr]p9) 1043 /// and returns the result type of that conversion. 1044 QualType UsualArithmeticConversionsType(QualType lhs, QualType rhs); 1045 1046 //===--------------------------------------------------------------------===// 1047 // Integer Predicates 1048 //===--------------------------------------------------------------------===// 1049 1050 // The width of an integer, as defined in C99 6.2.6.2. This is the number 1051 // of bits in an integer type excluding any padding bits. 1052 unsigned getIntWidth(QualType T); 1053 1054 // Per C99 6.2.5p6, for every signed integer type, there is a corresponding 1055 // unsigned integer type. This method takes a signed type, and returns the 1056 // corresponding unsigned integer type. 1057 QualType getCorrespondingUnsignedType(QualType T); 1058 1059 //===--------------------------------------------------------------------===// 1060 // Type Iterators. 1061 //===--------------------------------------------------------------------===// 1062 1063 typedef std::vector<Type*>::iterator type_iterator; 1064 typedef std::vector<Type*>::const_iterator const_type_iterator; 1065 1066 type_iterator types_begin() { return Types.begin(); } 1067 type_iterator types_end() { return Types.end(); } 1068 const_type_iterator types_begin() const { return Types.begin(); } 1069 const_type_iterator types_end() const { return Types.end(); } 1070 1071 //===--------------------------------------------------------------------===// 1072 // Integer Values 1073 //===--------------------------------------------------------------------===// 1074 1075 /// MakeIntValue - Make an APSInt of the appropriate width and 1076 /// signedness for the given \arg Value and integer \arg Type. 1077 llvm::APSInt MakeIntValue(uint64_t Value, QualType Type) { 1078 llvm::APSInt Res(getIntWidth(Type), !Type->isSignedIntegerType()); 1079 Res = Value; 1080 return Res; 1081 } 1082 1083 /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. 1084 ObjCImplementationDecl *getObjCImplementation(ObjCInterfaceDecl *D); 1085 /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. 1086 ObjCCategoryImplDecl *getObjCImplementation(ObjCCategoryDecl *D); 1087 1088 /// \brief Set the implementation of ObjCInterfaceDecl. 1089 void setObjCImplementation(ObjCInterfaceDecl *IFaceD, 1090 ObjCImplementationDecl *ImplD); 1091 /// \brief Set the implementation of ObjCCategoryDecl. 1092 void setObjCImplementation(ObjCCategoryDecl *CatD, 1093 ObjCCategoryImplDecl *ImplD); 1094 1095 /// \brief Allocate an uninitialized DeclaratorInfo. 1096 /// 1097 /// The caller should initialize the memory held by DeclaratorInfo using 1098 /// the TypeLoc wrappers. 1099 /// 1100 /// \param T the type that will be the basis for type source info. This type 1101 /// should refer to how the declarator was written in source code, not to 1102 /// what type semantic analysis resolved the declarator to. 1103 /// 1104 /// \param Size the size of the type info to create, or 0 if the size 1105 /// should be calculated based on the type. 1106 DeclaratorInfo *CreateDeclaratorInfo(QualType T, unsigned Size = 0); 1107 1108 /// \brief Allocate a DeclaratorInfo where all locations have been 1109 /// initialized to a given location, which defaults to the empty 1110 /// location. 1111 DeclaratorInfo * 1112 getTrivialDeclaratorInfo(QualType T, SourceLocation Loc = SourceLocation()); 1113 1114private: 1115 ASTContext(const ASTContext&); // DO NOT IMPLEMENT 1116 void operator=(const ASTContext&); // DO NOT IMPLEMENT 1117 1118 void InitBuiltinTypes(); 1119 void InitBuiltinType(CanQualType &R, BuiltinType::Kind K); 1120 1121 // Return the ObjC type encoding for a given type. 1122 void getObjCEncodingForTypeImpl(QualType t, std::string &S, 1123 bool ExpandPointedToStructures, 1124 bool ExpandStructures, 1125 const FieldDecl *Field, 1126 bool OutermostType = false, 1127 bool EncodingProperty = false); 1128 1129 const ASTRecordLayout &getObjCLayout(const ObjCInterfaceDecl *D, 1130 const ObjCImplementationDecl *Impl); 1131}; 1132 1133/// @brief Utility function for constructing a nullary selector. 1134static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) { 1135 IdentifierInfo* II = &Ctx.Idents.get(name); 1136 return Ctx.Selectors.getSelector(0, &II); 1137} 1138 1139/// @brief Utility function for constructing an unary selector. 1140static inline Selector GetUnarySelector(const char* name, ASTContext& Ctx) { 1141 IdentifierInfo* II = &Ctx.Idents.get(name); 1142 return Ctx.Selectors.getSelector(1, &II); 1143} 1144 1145} // end namespace clang 1146 1147// operator new and delete aren't allowed inside namespaces. 1148// The throw specifications are mandated by the standard. 1149/// @brief Placement new for using the ASTContext's allocator. 1150/// 1151/// This placement form of operator new uses the ASTContext's allocator for 1152/// obtaining memory. It is a non-throwing new, which means that it returns 1153/// null on error. (If that is what the allocator does. The current does, so if 1154/// this ever changes, this operator will have to be changed, too.) 1155/// Usage looks like this (assuming there's an ASTContext 'Context' in scope): 1156/// @code 1157/// // Default alignment (16) 1158/// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments); 1159/// // Specific alignment 1160/// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments); 1161/// @endcode 1162/// Please note that you cannot use delete on the pointer; it must be 1163/// deallocated using an explicit destructor call followed by 1164/// @c Context.Deallocate(Ptr). 1165/// 1166/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 1167/// @param C The ASTContext that provides the allocator. 1168/// @param Alignment The alignment of the allocated memory (if the underlying 1169/// allocator supports it). 1170/// @return The allocated memory. Could be NULL. 1171inline void *operator new(size_t Bytes, clang::ASTContext &C, 1172 size_t Alignment) throw () { 1173 return C.Allocate(Bytes, Alignment); 1174} 1175/// @brief Placement delete companion to the new above. 1176/// 1177/// This operator is just a companion to the new above. There is no way of 1178/// invoking it directly; see the new operator for more details. This operator 1179/// is called implicitly by the compiler if a placement new expression using 1180/// the ASTContext throws in the object constructor. 1181inline void operator delete(void *Ptr, clang::ASTContext &C, size_t) 1182 throw () { 1183 C.Deallocate(Ptr); 1184} 1185 1186/// This placement form of operator new[] uses the ASTContext's allocator for 1187/// obtaining memory. It is a non-throwing new[], which means that it returns 1188/// null on error. 1189/// Usage looks like this (assuming there's an ASTContext 'Context' in scope): 1190/// @code 1191/// // Default alignment (16) 1192/// char *data = new (Context) char[10]; 1193/// // Specific alignment 1194/// char *data = new (Context, 8) char[10]; 1195/// @endcode 1196/// Please note that you cannot use delete on the pointer; it must be 1197/// deallocated using an explicit destructor call followed by 1198/// @c Context.Deallocate(Ptr). 1199/// 1200/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 1201/// @param C The ASTContext that provides the allocator. 1202/// @param Alignment The alignment of the allocated memory (if the underlying 1203/// allocator supports it). 1204/// @return The allocated memory. Could be NULL. 1205inline void *operator new[](size_t Bytes, clang::ASTContext& C, 1206 size_t Alignment = 16) throw () { 1207 return C.Allocate(Bytes, Alignment); 1208} 1209 1210/// @brief Placement delete[] companion to the new[] above. 1211/// 1212/// This operator is just a companion to the new[] above. There is no way of 1213/// invoking it directly; see the new[] operator for more details. This operator 1214/// is called implicitly by the compiler if a placement new[] expression using 1215/// the ASTContext throws in the object constructor. 1216inline void operator delete[](void *Ptr, clang::ASTContext &C) throw () { 1217 C.Deallocate(Ptr); 1218} 1219 1220#endif 1221