ASTContext.h revision 8e6ac1d80055fa37b9b84029c7e751624ba7f84c
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/AST/Attr.h" 20#include "clang/AST/Builtins.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/Basic/SourceLocation.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 Expr; 41 class ExternalASTSource; 42 class IdentifierTable; 43 class SelectorTable; 44 class SourceManager; 45 class TargetInfo; 46 // Decls 47 class Decl; 48 class ObjCPropertyDecl; 49 class RecordDecl; 50 class TagDecl; 51 class TranslationUnitDecl; 52 class TypeDecl; 53 class TypedefDecl; 54 class TemplateTypeParmDecl; 55 class FieldDecl; 56 class ObjCIvarRefExpr; 57 class ObjCIvarDecl; 58 59/// ASTContext - This class holds long-lived AST nodes (such as types and 60/// decls) that can be referred to throughout the semantic analysis of a file. 61class ASTContext { 62 std::vector<Type*> Types; 63 llvm::FoldingSet<ExtQualType> ExtQualTypes; 64 llvm::FoldingSet<ComplexType> ComplexTypes; 65 llvm::FoldingSet<PointerType> PointerTypes; 66 llvm::FoldingSet<BlockPointerType> BlockPointerTypes; 67 llvm::FoldingSet<LValueReferenceType> LValueReferenceTypes; 68 llvm::FoldingSet<RValueReferenceType> RValueReferenceTypes; 69 llvm::FoldingSet<MemberPointerType> MemberPointerTypes; 70 llvm::FoldingSet<ConstantArrayType> ConstantArrayTypes; 71 llvm::FoldingSet<IncompleteArrayType> IncompleteArrayTypes; 72 std::vector<VariableArrayType*> VariableArrayTypes; 73 std::vector<DependentSizedArrayType*> DependentSizedArrayTypes; 74 llvm::FoldingSet<VectorType> VectorTypes; 75 llvm::FoldingSet<FunctionNoProtoType> FunctionNoProtoTypes; 76 llvm::FoldingSet<FunctionProtoType> FunctionProtoTypes; 77 llvm::FoldingSet<TemplateTypeParmType> TemplateTypeParmTypes; 78 llvm::FoldingSet<TemplateSpecializationType> TemplateSpecializationTypes; 79 llvm::FoldingSet<QualifiedNameType> QualifiedNameTypes; 80 llvm::FoldingSet<TypenameType> TypenameTypes; 81 llvm::FoldingSet<ObjCQualifiedInterfaceType> ObjCQualifiedInterfaceTypes; 82 llvm::FoldingSet<ObjCQualifiedIdType> ObjCQualifiedIdTypes; 83 84 llvm::FoldingSet<QualifiedTemplateName> QualifiedTemplateNames; 85 llvm::FoldingSet<DependentTemplateName> DependentTemplateNames; 86 87 /// \brief The set of nested name specifiers. 88 /// 89 /// This set is managed by the NestedNameSpecifier class. 90 llvm::FoldingSet<NestedNameSpecifier> NestedNameSpecifiers; 91 NestedNameSpecifier *GlobalNestedNameSpecifier; 92 friend class NestedNameSpecifier; 93 94 /// ASTRecordLayouts - A cache mapping from RecordDecls to ASTRecordLayouts. 95 /// This is lazily created. This is intentionally not serialized. 96 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*> ASTRecordLayouts; 97 llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*> ObjCLayouts; 98 99 llvm::DenseMap<unsigned, FixedWidthIntType*> SignedFixedWidthIntTypes; 100 llvm::DenseMap<unsigned, FixedWidthIntType*> UnsignedFixedWidthIntTypes; 101 102 /// BuiltinVaListType - built-in va list type. 103 /// This is initially null and set by Sema::LazilyCreateBuiltin when 104 /// a builtin that takes a valist is encountered. 105 QualType BuiltinVaListType; 106 107 /// ObjCIdType - a pseudo built-in typedef type (set by Sema). 108 QualType ObjCIdType; 109 const RecordType *IdStructType; 110 111 /// ObjCSelType - another pseudo built-in typedef type (set by Sema). 112 QualType ObjCSelType; 113 const RecordType *SelStructType; 114 115 /// ObjCProtoType - another pseudo built-in typedef type (set by Sema). 116 QualType ObjCProtoType; 117 const RecordType *ProtoStructType; 118 119 /// ObjCClassType - another pseudo built-in typedef type (set by Sema). 120 QualType ObjCClassType; 121 const RecordType *ClassStructType; 122 123 QualType ObjCConstantStringType; 124 RecordDecl *CFConstantStringTypeDecl; 125 126 RecordDecl *ObjCFastEnumerationStateTypeDecl; 127 128 TranslationUnitDecl *TUDecl; 129 130 /// SourceMgr - The associated SourceManager object. 131 SourceManager &SourceMgr; 132 133 /// LangOpts - The language options used to create the AST associated with 134 /// this ASTContext object. 135 LangOptions LangOpts; 136 137 /// MallocAlloc/BumpAlloc - The allocator objects used to create AST objects. 138 bool FreeMemory; 139 llvm::MallocAllocator MallocAlloc; 140 llvm::BumpPtrAllocator BumpAlloc; 141public: 142 TargetInfo &Target; 143 IdentifierTable &Idents; 144 SelectorTable &Selectors; 145 DeclarationNameTable DeclarationNames; 146 llvm::OwningPtr<ExternalASTSource> ExternalSource; 147 clang::PrintingPolicy PrintingPolicy; 148 149 SourceManager& getSourceManager() { return SourceMgr; } 150 const SourceManager& getSourceManager() const { return SourceMgr; } 151 void *Allocate(unsigned Size, unsigned Align = 8) { 152 return FreeMemory ? MallocAlloc.Allocate(Size, Align) : 153 BumpAlloc.Allocate(Size, Align); 154 } 155 void Deallocate(void *Ptr) { 156 if (FreeMemory) 157 MallocAlloc.Deallocate(Ptr); 158 } 159 const LangOptions& getLangOptions() const { return LangOpts; } 160 161 FullSourceLoc getFullLoc(SourceLocation Loc) const { 162 return FullSourceLoc(Loc,SourceMgr); 163 } 164 165 TranslationUnitDecl *getTranslationUnitDecl() const { return TUDecl; } 166 167 Builtin::Context BuiltinInfo; 168 169 // Builtin Types. 170 QualType VoidTy; 171 QualType BoolTy; 172 QualType CharTy; 173 QualType WCharTy; // [C++ 3.9.1p5], integer type in C99. 174 QualType SignedCharTy, ShortTy, IntTy, LongTy, LongLongTy, Int128Ty; 175 QualType UnsignedCharTy, UnsignedShortTy, UnsignedIntTy, UnsignedLongTy; 176 QualType UnsignedLongLongTy, UnsignedInt128Ty; 177 QualType FloatTy, DoubleTy, LongDoubleTy; 178 QualType FloatComplexTy, DoubleComplexTy, LongDoubleComplexTy; 179 QualType VoidPtrTy, NullPtrTy; 180 QualType OverloadTy; 181 QualType DependentTy; 182 183 ASTContext(const LangOptions& LOpts, SourceManager &SM, TargetInfo &t, 184 IdentifierTable &idents, SelectorTable &sels, 185 bool FreeMemory = true, unsigned size_reserve=0, 186 bool InitializeBuiltins = true); 187 188 ~ASTContext(); 189 190 /// \brief Initialize builtins. 191 /// 192 /// Typically, this routine will be called automatically by the 193 /// constructor. However, in certain cases (e.g., when there is a 194 /// PCH file to be loaded), the constructor does not perform 195 /// initialization for builtins. This routine can be called to 196 /// perform the initialization. 197 void InitializeBuiltins(IdentifierTable &idents); 198 199 /// \brief Attach an external AST source to the AST context. 200 /// 201 /// The external AST source provides the ability to load parts of 202 /// the abstract syntax tree as needed from some external storage, 203 /// e.g., a precompiled header. 204 void setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source); 205 206 /// \brief Retrieve a pointer to the external AST source associated 207 /// with this AST context, if any. 208 ExternalASTSource *getExternalSource() const { return ExternalSource.get(); } 209 210 void PrintStats() const; 211 const std::vector<Type*>& getTypes() const { return Types; } 212 213 //===--------------------------------------------------------------------===// 214 // Type Constructors 215 //===--------------------------------------------------------------------===// 216 217 /// getAddSpaceQualType - Return the uniqued reference to the type for an 218 /// address space qualified type with the specified type and address space. 219 /// The resulting type has a union of the qualifiers from T and the address 220 /// space. If T already has an address space specifier, it is silently 221 /// replaced. 222 QualType getAddrSpaceQualType(QualType T, unsigned AddressSpace); 223 224 /// getObjCGCQualType - Returns the uniqued reference to the type for an 225 /// objc gc qualified type. The retulting type has a union of the qualifiers 226 /// from T and the gc attribute. 227 QualType getObjCGCQualType(QualType T, QualType::GCAttrTypes gcAttr); 228 229 /// getComplexType - Return the uniqued reference to the type for a complex 230 /// number with the specified element type. 231 QualType getComplexType(QualType T); 232 233 /// getPointerType - Return the uniqued reference to the type for a pointer to 234 /// the specified type. 235 QualType getPointerType(QualType T); 236 237 /// getBlockPointerType - Return the uniqued reference to the type for a block 238 /// of the specified type. 239 QualType getBlockPointerType(QualType T); 240 241 /// getLValueReferenceType - Return the uniqued reference to the type for an 242 /// lvalue reference to the specified type. 243 QualType getLValueReferenceType(QualType T); 244 245 /// getRValueReferenceType - Return the uniqued reference to the type for an 246 /// rvalue reference to the specified type. 247 QualType getRValueReferenceType(QualType T); 248 249 /// getMemberPointerType - Return the uniqued reference to the type for a 250 /// member pointer to the specified type in the specified class. The class 251 /// is a Type because it could be a dependent name. 252 QualType getMemberPointerType(QualType T, const Type *Cls); 253 254 /// getVariableArrayType - Returns a non-unique reference to the type for a 255 /// variable array of the specified element type. 256 QualType getVariableArrayType(QualType EltTy, Expr *NumElts, 257 ArrayType::ArraySizeModifier ASM, 258 unsigned EltTypeQuals); 259 260 /// getDependentSizedArrayType - Returns a non-unique reference to 261 /// the type for a dependently-sized array of the specified element 262 /// type. FIXME: We will need these to be uniqued, or at least 263 /// comparable, at some point. 264 QualType getDependentSizedArrayType(QualType EltTy, Expr *NumElts, 265 ArrayType::ArraySizeModifier ASM, 266 unsigned EltTypeQuals); 267 268 /// getIncompleteArrayType - Returns a unique reference to the type for a 269 /// incomplete array of the specified element type. 270 QualType getIncompleteArrayType(QualType EltTy, 271 ArrayType::ArraySizeModifier ASM, 272 unsigned EltTypeQuals); 273 274 /// getConstantArrayType - Return the unique reference to the type for a 275 /// constant array of the specified element type. 276 QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize, 277 ArrayType::ArraySizeModifier ASM, 278 unsigned EltTypeQuals); 279 280 /// getVectorType - Return the unique reference to a vector type of 281 /// the specified element type and size. VectorType must be a built-in type. 282 QualType getVectorType(QualType VectorType, unsigned NumElts); 283 284 /// getExtVectorType - Return the unique reference to an extended vector type 285 /// of the specified element type and size. VectorType must be a built-in 286 /// type. 287 QualType getExtVectorType(QualType VectorType, unsigned NumElts); 288 289 /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. 290 /// 291 QualType getFunctionNoProtoType(QualType ResultTy); 292 293 /// getFunctionType - Return a normal function type with a typed argument 294 /// list. isVariadic indicates whether the argument list includes '...'. 295 QualType getFunctionType(QualType ResultTy, const QualType *ArgArray, 296 unsigned NumArgs, bool isVariadic, 297 unsigned TypeQuals, bool hasExceptionSpec = false, 298 bool hasAnyExceptionSpec = false, 299 unsigned NumExs = 0, const QualType *ExArray = 0); 300 301 /// getTypeDeclType - Return the unique reference to the type for 302 /// the specified type declaration. 303 QualType getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl=0); 304 305 /// getTypedefType - Return the unique reference to the type for the 306 /// specified typename decl. 307 QualType getTypedefType(TypedefDecl *Decl); 308 QualType getObjCInterfaceType(const ObjCInterfaceDecl *Decl); 309 310 QualType getTemplateTypeParmType(unsigned Depth, unsigned Index, 311 IdentifierInfo *Name = 0); 312 313 QualType getTemplateSpecializationType(TemplateName T, 314 const TemplateArgument *Args, 315 unsigned NumArgs, 316 QualType Canon = QualType()); 317 318 QualType getQualifiedNameType(NestedNameSpecifier *NNS, 319 QualType NamedType); 320 QualType getTypenameType(NestedNameSpecifier *NNS, 321 const IdentifierInfo *Name, 322 QualType Canon = QualType()); 323 QualType getTypenameType(NestedNameSpecifier *NNS, 324 const TemplateSpecializationType *TemplateId, 325 QualType Canon = QualType()); 326 327 /// getObjCQualifiedInterfaceType - Return a 328 /// ObjCQualifiedInterfaceType type for the given interface decl and 329 /// the conforming protocol list. 330 QualType getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl, 331 ObjCProtocolDecl **ProtocolList, 332 unsigned NumProtocols); 333 334 /// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for a 335 /// given 'id' and conforming protocol list. 336 QualType getObjCQualifiedIdType(ObjCProtocolDecl **ProtocolList, 337 unsigned NumProtocols); 338 339 340 /// getTypeOfType - GCC extension. 341 QualType getTypeOfExprType(Expr *e); 342 QualType getTypeOfType(QualType t); 343 344 /// getTagDeclType - Return the unique reference to the type for the 345 /// specified TagDecl (struct/union/class/enum) decl. 346 QualType getTagDeclType(TagDecl *Decl); 347 348 /// getSizeType - Return the unique type for "size_t" (C99 7.17), defined 349 /// in <stddef.h>. The sizeof operator requires this (C99 6.5.3.4p4). 350 QualType getSizeType() const; 351 352 /// getWCharType - In C++, this returns the unique wchar_t type. In C99, this 353 /// returns a type compatible with the type defined in <stddef.h> as defined 354 /// by the target. 355 QualType getWCharType() const { return WCharTy; } 356 357 /// getSignedWCharType - Return the type of "signed wchar_t". 358 /// Used when in C++, as a GCC extension. 359 QualType getSignedWCharType() const; 360 361 /// getUnsignedWCharType - Return the type of "unsigned wchar_t". 362 /// Used when in C++, as a GCC extension. 363 QualType getUnsignedWCharType() const; 364 365 /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) 366 /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). 367 QualType getPointerDiffType() const; 368 369 // getCFConstantStringType - Return the C structure type used to represent 370 // constant CFStrings. 371 QualType getCFConstantStringType(); 372 373 /// Get the structure type used to representation CFStrings, or NULL 374 /// if it hasn't yet been built. 375 QualType getRawCFConstantStringType() { 376 if (CFConstantStringTypeDecl) 377 return getTagDeclType(CFConstantStringTypeDecl); 378 return QualType(); 379 } 380 void setCFConstantStringType(QualType T); 381 382 // This setter/getter represents the ObjC type for an NSConstantString. 383 void setObjCConstantStringInterface(ObjCInterfaceDecl *Decl); 384 QualType getObjCConstantStringInterface() const { 385 return ObjCConstantStringType; 386 } 387 388 //// This gets the struct used to keep track of fast enumerations. 389 QualType getObjCFastEnumerationStateType(); 390 391 /// Get the ObjCFastEnumerationState type, or NULL if it hasn't yet 392 /// been built. 393 QualType getRawObjCFastEnumerationStateType() { 394 if (ObjCFastEnumerationStateTypeDecl) 395 return getTagDeclType(ObjCFastEnumerationStateTypeDecl); 396 return QualType(); 397 } 398 399 void setObjCFastEnumerationStateType(QualType T); 400 401 /// getObjCEncodingForType - Emit the ObjC type encoding for the 402 /// given type into \arg S. If \arg NameFields is specified then 403 /// record field names are also encoded. 404 void getObjCEncodingForType(QualType t, std::string &S, 405 const FieldDecl *Field=0); 406 407 void getLegacyIntegralTypeEncoding(QualType &t) const; 408 409 // Put the string version of type qualifiers into S. 410 void getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, 411 std::string &S) const; 412 413 /// getObjCEncodingForMethodDecl - Return the encoded type for this method 414 /// declaration. 415 void getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, std::string &S); 416 417 /// getObjCEncodingForPropertyDecl - Return the encoded type for 418 /// this method declaration. If non-NULL, Container must be either 419 /// an ObjCCategoryImplDecl or ObjCImplementationDecl; it should 420 /// only be NULL when getting encodings for protocol properties. 421 void getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, 422 const Decl *Container, 423 std::string &S); 424 425 /// getObjCEncodingTypeSize returns size of type for objective-c encoding 426 /// purpose. 427 int getObjCEncodingTypeSize(QualType t); 428 429 /// This setter/getter represents the ObjC 'id' type. It is setup lazily, by 430 /// Sema. id is always a (typedef for a) pointer type, a pointer to a struct. 431 QualType getObjCIdType() const { return ObjCIdType; } 432 void setObjCIdType(QualType T); 433 434 void setObjCSelType(QualType T); 435 QualType getObjCSelType() const { return ObjCSelType; } 436 437 void setObjCProtoType(QualType QT); 438 QualType getObjCProtoType() const { return ObjCProtoType; } 439 440 /// This setter/getter repreents the ObjC 'Class' type. It is setup lazily, by 441 /// Sema. 'Class' is always a (typedef for a) pointer type, a pointer to a 442 /// struct. 443 QualType getObjCClassType() const { return ObjCClassType; } 444 void setObjCClassType(QualType T); 445 446 void setBuiltinVaListType(QualType T); 447 QualType getBuiltinVaListType() const { return BuiltinVaListType; } 448 449 QualType getFixedWidthIntType(unsigned Width, bool Signed); 450 451 TemplateName getQualifiedTemplateName(NestedNameSpecifier *NNS, 452 bool TemplateKeyword, 453 TemplateDecl *Template); 454 455 TemplateName getDependentTemplateName(NestedNameSpecifier *NNS, 456 const IdentifierInfo *Name); 457 458private: 459 QualType getFromTargetType(unsigned Type) const; 460 461 //===--------------------------------------------------------------------===// 462 // Type Predicates. 463 //===--------------------------------------------------------------------===// 464 465public: 466 /// isObjCObjectPointerType - Returns true if type is an Objective-C pointer 467 /// to an object type. This includes "id" and "Class" (two 'special' pointers 468 /// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified 469 /// ID type). 470 bool isObjCObjectPointerType(QualType Ty) const; 471 472 /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's 473 /// garbage collection attribute. 474 /// 475 QualType::GCAttrTypes getObjCGCAttrKind(const QualType &Ty) const; 476 477 /// isObjCNSObjectType - Return true if this is an NSObject object with 478 /// its NSObject attribute set. 479 bool isObjCNSObjectType(QualType Ty) const; 480 481 //===--------------------------------------------------------------------===// 482 // Type Sizing and Analysis 483 //===--------------------------------------------------------------------===// 484 485 /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified 486 /// scalar floating point type. 487 const llvm::fltSemantics &getFloatTypeSemantics(QualType T) const; 488 489 /// getTypeInfo - Get the size and alignment of the specified complete type in 490 /// bits. 491 std::pair<uint64_t, unsigned> getTypeInfo(const Type *T); 492 std::pair<uint64_t, unsigned> getTypeInfo(QualType T) { 493 return getTypeInfo(T.getTypePtr()); 494 } 495 496 /// getTypeSize - Return the size of the specified type, in bits. This method 497 /// does not work on incomplete types. 498 uint64_t getTypeSize(QualType T) { 499 return getTypeInfo(T).first; 500 } 501 uint64_t getTypeSize(const Type *T) { 502 return getTypeInfo(T).first; 503 } 504 505 /// getTypeAlign - Return the ABI-specified alignment of a type, in bits. 506 /// This method does not work on incomplete types. 507 unsigned getTypeAlign(QualType T) { 508 return getTypeInfo(T).second; 509 } 510 unsigned getTypeAlign(const Type *T) { 511 return getTypeInfo(T).second; 512 } 513 514 /// getPreferredTypeAlign - Return the "preferred" alignment of the specified 515 /// type for the current target in bits. This can be different than the ABI 516 /// alignment in cases where it is beneficial for performance to overalign 517 /// a data type. 518 unsigned getPreferredTypeAlign(const Type *T); 519 520 /// getDeclAlignInBytes - Return the alignment of the specified decl 521 /// that should be returned by __alignof(). Note that bitfields do 522 /// not have a valid alignment, so this method will assert on them. 523 unsigned getDeclAlignInBytes(const Decl *D); 524 525 /// getASTRecordLayout - Get or compute information about the layout of the 526 /// specified record (struct/union/class), which indicates its size and field 527 /// position information. 528 const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D); 529 530 /// getASTObjCInterfaceLayout - Get or compute information about the 531 /// layout of the specified Objective-C interface. 532 const ASTRecordLayout &getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D); 533 534 /// getASTObjCImplementationLayout - Get or compute information about 535 /// the layout of the specified Objective-C implementation. This may 536 /// differ from the interface if synthesized ivars are present. 537 const ASTRecordLayout & 538 getASTObjCImplementationLayout(const ObjCImplementationDecl *D); 539 540 void CollectObjCIvars(const ObjCInterfaceDecl *OI, 541 llvm::SmallVectorImpl<FieldDecl*> &Fields); 542 543 void ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI, 544 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars, 545 bool CollectSynthesized = true); 546 void CollectSynthesizedIvars(const ObjCInterfaceDecl *OI, 547 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars); 548 void CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD, 549 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars); 550 unsigned CountSynthesizedIvars(const ObjCInterfaceDecl *OI); 551 unsigned CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD); 552 553 //===--------------------------------------------------------------------===// 554 // Type Operators 555 //===--------------------------------------------------------------------===// 556 557 /// getCanonicalType - Return the canonical (structural) type corresponding to 558 /// the specified potentially non-canonical type. The non-canonical version 559 /// of a type may have many "decorated" versions of types. Decorators can 560 /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed 561 /// to be free of any of these, allowing two canonical types to be compared 562 /// for exact equality with a simple pointer comparison. 563 QualType getCanonicalType(QualType T); 564 const Type *getCanonicalType(const Type *T) { 565 return T->getCanonicalTypeInternal().getTypePtr(); 566 } 567 568 /// \brief Determine whether the given types are equivalent. 569 bool hasSameType(QualType T1, QualType T2) { 570 return getCanonicalType(T1) == getCanonicalType(T2); 571 } 572 573 /// \brief Determine whether the given types are equivalent after 574 /// cvr-qualifiers have been removed. 575 bool hasSameUnqualifiedType(QualType T1, QualType T2) { 576 T1 = getCanonicalType(T1); 577 T2 = getCanonicalType(T2); 578 return T1.getUnqualifiedType() == T2.getUnqualifiedType(); 579 } 580 581 /// \brief Retrieves the "canonical" declaration of the given declaration. 582 Decl *getCanonicalDecl(Decl *D); 583 584 /// \brief Retrieves the "canonical" declaration of the given tag 585 /// declaration. 586 /// 587 /// The canonical declaration for the given tag declaration is 588 /// either the definition of the tag (if it is a complete type) or 589 /// the first declaration of that tag. 590 TagDecl *getCanonicalDecl(TagDecl *Tag) { 591 return cast<TagDecl>(getCanonicalDecl((Decl *)Tag)); 592 } 593 594 /// \brief Retrieves the "canonical" declaration of 595 596 /// \brief Retrieves the "canonical" nested name specifier for a 597 /// given nested name specifier. 598 /// 599 /// The canonical nested name specifier is a nested name specifier 600 /// that uniquely identifies a type or namespace within the type 601 /// system. For example, given: 602 /// 603 /// \code 604 /// namespace N { 605 /// struct S { 606 /// template<typename T> struct X { typename T* type; }; 607 /// }; 608 /// } 609 /// 610 /// template<typename T> struct Y { 611 /// typename N::S::X<T>::type member; 612 /// }; 613 /// \endcode 614 /// 615 /// Here, the nested-name-specifier for N::S::X<T>:: will be 616 /// S::X<template-param-0-0>, since 'S' and 'X' are uniquely defined 617 /// by declarations in the type system and the canonical type for 618 /// the template type parameter 'T' is template-param-0-0. 619 NestedNameSpecifier * 620 getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS); 621 622 /// \brief Retrieves the "canonical" template name that refers to a 623 /// given template. 624 /// 625 /// The canonical template name is the simplest expression that can 626 /// be used to refer to a given template. For most templates, this 627 /// expression is just the template declaration itself. For example, 628 /// the template std::vector can be referred to via a variety of 629 /// names---std::vector, ::std::vector, vector (if vector is in 630 /// scope), etc.---but all of these names map down to the same 631 /// TemplateDecl, which is used to form the canonical template name. 632 /// 633 /// Dependent template names are more interesting. Here, the 634 /// template name could be something like T::template apply or 635 /// std::allocator<T>::template rebind, where the nested name 636 /// specifier itself is dependent. In this case, the canonical 637 /// template name uses the shortest form of the dependent 638 /// nested-name-specifier, which itself contains all canonical 639 /// types, values, and templates. 640 TemplateName getCanonicalTemplateName(TemplateName Name); 641 642 /// Type Query functions. If the type is an instance of the specified class, 643 /// return the Type pointer for the underlying maximally pretty type. This 644 /// is a member of ASTContext because this may need to do some amount of 645 /// canonicalization, e.g. to move type qualifiers into the element type. 646 const ArrayType *getAsArrayType(QualType T); 647 const ConstantArrayType *getAsConstantArrayType(QualType T) { 648 return dyn_cast_or_null<ConstantArrayType>(getAsArrayType(T)); 649 } 650 const VariableArrayType *getAsVariableArrayType(QualType T) { 651 return dyn_cast_or_null<VariableArrayType>(getAsArrayType(T)); 652 } 653 const IncompleteArrayType *getAsIncompleteArrayType(QualType T) { 654 return dyn_cast_or_null<IncompleteArrayType>(getAsArrayType(T)); 655 } 656 657 /// getBaseElementType - Returns the innermost element type of a variable 658 /// length array type. For example, will return "int" for int[m][n] 659 QualType getBaseElementType(const VariableArrayType *VAT); 660 661 /// getArrayDecayedType - Return the properly qualified result of decaying the 662 /// specified array type to a pointer. This operation is non-trivial when 663 /// handling typedefs etc. The canonical type of "T" must be an array type, 664 /// this returns a pointer to a properly qualified element of the array. 665 /// 666 /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. 667 QualType getArrayDecayedType(QualType T); 668 669 /// getIntegerTypeOrder - Returns the highest ranked integer type: 670 /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If 671 /// LHS < RHS, return -1. 672 int getIntegerTypeOrder(QualType LHS, QualType RHS); 673 674 /// getFloatingTypeOrder - Compare the rank of the two specified floating 675 /// point types, ignoring the domain of the type (i.e. 'double' == 676 /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If 677 /// LHS < RHS, return -1. 678 int getFloatingTypeOrder(QualType LHS, QualType RHS); 679 680 /// getFloatingTypeOfSizeWithinDomain - Returns a real floating 681 /// point or a complex type (based on typeDomain/typeSize). 682 /// 'typeDomain' is a real floating point or complex type. 683 /// 'typeSize' is a real floating point or complex type. 684 QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize, 685 QualType typeDomain) const; 686 687private: 688 // Helper for integer ordering 689 unsigned getIntegerRank(Type* T); 690 691public: 692 693 //===--------------------------------------------------------------------===// 694 // Type Compatibility Predicates 695 //===--------------------------------------------------------------------===// 696 697 /// Compatibility predicates used to check assignment expressions. 698 bool typesAreCompatible(QualType, QualType); // C99 6.2.7p1 699 bool typesAreBlockCompatible(QualType lhs, QualType rhs); 700 701 bool isObjCIdType(QualType T) const { 702 return T == ObjCIdType; 703 } 704 bool isObjCIdStructType(QualType T) const { 705 if (!IdStructType) // ObjC isn't enabled 706 return false; 707 return T->getAsStructureType() == IdStructType; 708 } 709 bool isObjCClassType(QualType T) const { 710 return T == ObjCClassType; 711 } 712 bool isObjCClassStructType(QualType T) const { 713 if (!ClassStructType) // ObjC isn't enabled 714 return false; 715 return T->getAsStructureType() == ClassStructType; 716 } 717 bool isObjCSelType(QualType T) const { 718 assert(SelStructType && "isObjCSelType used before 'SEL' type is built"); 719 return T->getAsStructureType() == SelStructType; 720 } 721 722 // Check the safety of assignment from LHS to RHS 723 bool canAssignObjCInterfaces(const ObjCInterfaceType *LHS, 724 const ObjCInterfaceType *RHS); 725 bool areComparableObjCPointerTypes(QualType LHS, QualType RHS); 726 727 // Functions for calculating composite types 728 QualType mergeTypes(QualType, QualType); 729 QualType mergeFunctionTypes(QualType, QualType); 730 731 //===--------------------------------------------------------------------===// 732 // Integer Predicates 733 //===--------------------------------------------------------------------===// 734 735 // The width of an integer, as defined in C99 6.2.6.2. This is the number 736 // of bits in an integer type excluding any padding bits. 737 unsigned getIntWidth(QualType T); 738 739 // Per C99 6.2.5p6, for every signed integer type, there is a corresponding 740 // unsigned integer type. This method takes a signed type, and returns the 741 // corresponding unsigned integer type. 742 QualType getCorrespondingUnsignedType(QualType T); 743 744 //===--------------------------------------------------------------------===// 745 // Type Iterators. 746 //===--------------------------------------------------------------------===// 747 748 typedef std::vector<Type*>::iterator type_iterator; 749 typedef std::vector<Type*>::const_iterator const_type_iterator; 750 751 type_iterator types_begin() { return Types.begin(); } 752 type_iterator types_end() { return Types.end(); } 753 const_type_iterator types_begin() const { return Types.begin(); } 754 const_type_iterator types_end() const { return Types.end(); } 755 756 //===--------------------------------------------------------------------===// 757 // Integer Values 758 //===--------------------------------------------------------------------===// 759 760 /// MakeIntValue - Make an APSInt of the appropriate width and 761 /// signedness for the given \arg Value and integer \arg Type. 762 llvm::APSInt MakeIntValue(uint64_t Value, QualType Type) { 763 llvm::APSInt Res(getIntWidth(Type), !Type->isSignedIntegerType()); 764 Res = Value; 765 return Res; 766 } 767 768private: 769 ASTContext(const ASTContext&); // DO NOT IMPLEMENT 770 void operator=(const ASTContext&); // DO NOT IMPLEMENT 771 772 void InitBuiltinTypes(); 773 void InitBuiltinType(QualType &R, BuiltinType::Kind K); 774 775 // Return the ObjC type encoding for a given type. 776 void getObjCEncodingForTypeImpl(QualType t, std::string &S, 777 bool ExpandPointedToStructures, 778 bool ExpandStructures, 779 const FieldDecl *Field, 780 bool OutermostType = false, 781 bool EncodingProperty = false); 782 783 const ASTRecordLayout &getObjCLayout(const ObjCInterfaceDecl *D, 784 const ObjCImplementationDecl *Impl); 785}; 786 787} // end namespace clang 788 789// operator new and delete aren't allowed inside namespaces. 790// The throw specifications are mandated by the standard. 791/// @brief Placement new for using the ASTContext's allocator. 792/// 793/// This placement form of operator new uses the ASTContext's allocator for 794/// obtaining memory. It is a non-throwing new, which means that it returns 795/// null on error. (If that is what the allocator does. The current does, so if 796/// this ever changes, this operator will have to be changed, too.) 797/// Usage looks like this (assuming there's an ASTContext 'Context' in scope): 798/// @code 799/// // Default alignment (16) 800/// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments); 801/// // Specific alignment 802/// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments); 803/// @endcode 804/// Please note that you cannot use delete on the pointer; it must be 805/// deallocated using an explicit destructor call followed by 806/// @c Context.Deallocate(Ptr). 807/// 808/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 809/// @param C The ASTContext that provides the allocator. 810/// @param Alignment The alignment of the allocated memory (if the underlying 811/// allocator supports it). 812/// @return The allocated memory. Could be NULL. 813inline void *operator new(size_t Bytes, clang::ASTContext &C, 814 size_t Alignment) throw () { 815 return C.Allocate(Bytes, Alignment); 816} 817/// @brief Placement delete companion to the new above. 818/// 819/// This operator is just a companion to the new above. There is no way of 820/// invoking it directly; see the new operator for more details. This operator 821/// is called implicitly by the compiler if a placement new expression using 822/// the ASTContext throws in the object constructor. 823inline void operator delete(void *Ptr, clang::ASTContext &C, size_t) 824 throw () { 825 C.Deallocate(Ptr); 826} 827 828/// This placement form of operator new[] uses the ASTContext's allocator for 829/// obtaining memory. It is a non-throwing new[], which means that it returns 830/// null on error. 831/// Usage looks like this (assuming there's an ASTContext 'Context' in scope): 832/// @code 833/// // Default alignment (16) 834/// char *data = new (Context) char[10]; 835/// // Specific alignment 836/// char *data = new (Context, 8) char[10]; 837/// @endcode 838/// Please note that you cannot use delete on the pointer; it must be 839/// deallocated using an explicit destructor call followed by 840/// @c Context.Deallocate(Ptr). 841/// 842/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 843/// @param C The ASTContext that provides the allocator. 844/// @param Alignment The alignment of the allocated memory (if the underlying 845/// allocator supports it). 846/// @return The allocated memory. Could be NULL. 847inline void *operator new[](size_t Bytes, clang::ASTContext& C, 848 size_t Alignment = 16) throw () { 849 return C.Allocate(Bytes, Alignment); 850} 851 852/// @brief Placement delete[] companion to the new[] above. 853/// 854/// This operator is just a companion to the new[] above. There is no way of 855/// invoking it directly; see the new[] operator for more details. This operator 856/// is called implicitly by the compiler if a placement new[] expression using 857/// the ASTContext throws in the object constructor. 858inline void operator delete[](void *Ptr, clang::ASTContext &C) throw () { 859 C.Deallocate(Ptr); 860} 861 862#endif 863