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