ASTContext.h revision 86df27bbdbb98c39ec2184695c0561209f91bedd
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 458 enum GetBuiltinTypeError { 459 GE_None, //< No error 460 GE_Missing_FILE //< Missing the FILE type from <stdio.h> 461 }; 462 463 /// GetBuiltinType - Return the type for the specified builtin. 464 QualType GetBuiltinType(unsigned ID, GetBuiltinTypeError &Error); 465 466private: 467 QualType getFromTargetType(unsigned Type) const; 468 469 //===--------------------------------------------------------------------===// 470 // Type Predicates. 471 //===--------------------------------------------------------------------===// 472 473public: 474 /// isObjCObjectPointerType - Returns true if type is an Objective-C pointer 475 /// to an object type. This includes "id" and "Class" (two 'special' pointers 476 /// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified 477 /// ID type). 478 bool isObjCObjectPointerType(QualType Ty) const; 479 480 /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's 481 /// garbage collection attribute. 482 /// 483 QualType::GCAttrTypes getObjCGCAttrKind(const QualType &Ty) const; 484 485 /// isObjCNSObjectType - Return true if this is an NSObject object with 486 /// its NSObject attribute set. 487 bool isObjCNSObjectType(QualType Ty) const; 488 489 //===--------------------------------------------------------------------===// 490 // Type Sizing and Analysis 491 //===--------------------------------------------------------------------===// 492 493 /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified 494 /// scalar floating point type. 495 const llvm::fltSemantics &getFloatTypeSemantics(QualType T) const; 496 497 /// getTypeInfo - Get the size and alignment of the specified complete type in 498 /// bits. 499 std::pair<uint64_t, unsigned> getTypeInfo(const Type *T); 500 std::pair<uint64_t, unsigned> getTypeInfo(QualType T) { 501 return getTypeInfo(T.getTypePtr()); 502 } 503 504 /// getTypeSize - Return the size of the specified type, in bits. This method 505 /// does not work on incomplete types. 506 uint64_t getTypeSize(QualType T) { 507 return getTypeInfo(T).first; 508 } 509 uint64_t getTypeSize(const Type *T) { 510 return getTypeInfo(T).first; 511 } 512 513 /// getTypeAlign - Return the ABI-specified alignment of a type, in bits. 514 /// This method does not work on incomplete types. 515 unsigned getTypeAlign(QualType T) { 516 return getTypeInfo(T).second; 517 } 518 unsigned getTypeAlign(const Type *T) { 519 return getTypeInfo(T).second; 520 } 521 522 /// getPreferredTypeAlign - Return the "preferred" alignment of the specified 523 /// type for the current target in bits. This can be different than the ABI 524 /// alignment in cases where it is beneficial for performance to overalign 525 /// a data type. 526 unsigned getPreferredTypeAlign(const Type *T); 527 528 /// getDeclAlignInBytes - Return the alignment of the specified decl 529 /// that should be returned by __alignof(). Note that bitfields do 530 /// not have a valid alignment, so this method will assert on them. 531 unsigned getDeclAlignInBytes(const Decl *D); 532 533 /// getASTRecordLayout - Get or compute information about the layout of the 534 /// specified record (struct/union/class), which indicates its size and field 535 /// position information. 536 const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D); 537 538 /// getASTObjCInterfaceLayout - Get or compute information about the 539 /// layout of the specified Objective-C interface. 540 const ASTRecordLayout &getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D); 541 542 /// getASTObjCImplementationLayout - Get or compute information about 543 /// the layout of the specified Objective-C implementation. This may 544 /// differ from the interface if synthesized ivars are present. 545 const ASTRecordLayout & 546 getASTObjCImplementationLayout(const ObjCImplementationDecl *D); 547 548 void CollectObjCIvars(const ObjCInterfaceDecl *OI, 549 llvm::SmallVectorImpl<FieldDecl*> &Fields); 550 551 void ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI, 552 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars, 553 bool CollectSynthesized = true); 554 void CollectSynthesizedIvars(const ObjCInterfaceDecl *OI, 555 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars); 556 void CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD, 557 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars); 558 unsigned CountSynthesizedIvars(const ObjCInterfaceDecl *OI); 559 unsigned CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD); 560 561 //===--------------------------------------------------------------------===// 562 // Type Operators 563 //===--------------------------------------------------------------------===// 564 565 /// getCanonicalType - Return the canonical (structural) type corresponding to 566 /// the specified potentially non-canonical type. The non-canonical version 567 /// of a type may have many "decorated" versions of types. Decorators can 568 /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed 569 /// to be free of any of these, allowing two canonical types to be compared 570 /// for exact equality with a simple pointer comparison. 571 QualType getCanonicalType(QualType T); 572 const Type *getCanonicalType(const Type *T) { 573 return T->getCanonicalTypeInternal().getTypePtr(); 574 } 575 576 /// \brief Determine whether the given types are equivalent. 577 bool hasSameType(QualType T1, QualType T2) { 578 return getCanonicalType(T1) == getCanonicalType(T2); 579 } 580 581 /// \brief Determine whether the given types are equivalent after 582 /// cvr-qualifiers have been removed. 583 bool hasSameUnqualifiedType(QualType T1, QualType T2) { 584 T1 = getCanonicalType(T1); 585 T2 = getCanonicalType(T2); 586 return T1.getUnqualifiedType() == T2.getUnqualifiedType(); 587 } 588 589 /// \brief Retrieves the "canonical" declaration of the given declaration. 590 Decl *getCanonicalDecl(Decl *D); 591 592 /// \brief Retrieves the "canonical" declaration of the given tag 593 /// declaration. 594 /// 595 /// The canonical declaration for the given tag declaration is 596 /// either the definition of the tag (if it is a complete type) or 597 /// the first declaration of that tag. 598 TagDecl *getCanonicalDecl(TagDecl *Tag) { 599 return cast<TagDecl>(getCanonicalDecl((Decl *)Tag)); 600 } 601 602 /// \brief Retrieves the "canonical" declaration of 603 604 /// \brief Retrieves the "canonical" nested name specifier for a 605 /// given nested name specifier. 606 /// 607 /// The canonical nested name specifier is a nested name specifier 608 /// that uniquely identifies a type or namespace within the type 609 /// system. For example, given: 610 /// 611 /// \code 612 /// namespace N { 613 /// struct S { 614 /// template<typename T> struct X { typename T* type; }; 615 /// }; 616 /// } 617 /// 618 /// template<typename T> struct Y { 619 /// typename N::S::X<T>::type member; 620 /// }; 621 /// \endcode 622 /// 623 /// Here, the nested-name-specifier for N::S::X<T>:: will be 624 /// S::X<template-param-0-0>, since 'S' and 'X' are uniquely defined 625 /// by declarations in the type system and the canonical type for 626 /// the template type parameter 'T' is template-param-0-0. 627 NestedNameSpecifier * 628 getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS); 629 630 /// \brief Retrieves the "canonical" template name that refers to a 631 /// given template. 632 /// 633 /// The canonical template name is the simplest expression that can 634 /// be used to refer to a given template. For most templates, this 635 /// expression is just the template declaration itself. For example, 636 /// the template std::vector can be referred to via a variety of 637 /// names---std::vector, ::std::vector, vector (if vector is in 638 /// scope), etc.---but all of these names map down to the same 639 /// TemplateDecl, which is used to form the canonical template name. 640 /// 641 /// Dependent template names are more interesting. Here, the 642 /// template name could be something like T::template apply or 643 /// std::allocator<T>::template rebind, where the nested name 644 /// specifier itself is dependent. In this case, the canonical 645 /// template name uses the shortest form of the dependent 646 /// nested-name-specifier, which itself contains all canonical 647 /// types, values, and templates. 648 TemplateName getCanonicalTemplateName(TemplateName Name); 649 650 /// Type Query functions. If the type is an instance of the specified class, 651 /// return the Type pointer for the underlying maximally pretty type. This 652 /// is a member of ASTContext because this may need to do some amount of 653 /// canonicalization, e.g. to move type qualifiers into the element type. 654 const ArrayType *getAsArrayType(QualType T); 655 const ConstantArrayType *getAsConstantArrayType(QualType T) { 656 return dyn_cast_or_null<ConstantArrayType>(getAsArrayType(T)); 657 } 658 const VariableArrayType *getAsVariableArrayType(QualType T) { 659 return dyn_cast_or_null<VariableArrayType>(getAsArrayType(T)); 660 } 661 const IncompleteArrayType *getAsIncompleteArrayType(QualType T) { 662 return dyn_cast_or_null<IncompleteArrayType>(getAsArrayType(T)); 663 } 664 665 /// getBaseElementType - Returns the innermost element type of a variable 666 /// length array type. For example, will return "int" for int[m][n] 667 QualType getBaseElementType(const VariableArrayType *VAT); 668 669 /// getArrayDecayedType - Return the properly qualified result of decaying the 670 /// specified array type to a pointer. This operation is non-trivial when 671 /// handling typedefs etc. The canonical type of "T" must be an array type, 672 /// this returns a pointer to a properly qualified element of the array. 673 /// 674 /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. 675 QualType getArrayDecayedType(QualType T); 676 677 /// getIntegerTypeOrder - Returns the highest ranked integer type: 678 /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If 679 /// LHS < RHS, return -1. 680 int getIntegerTypeOrder(QualType LHS, QualType RHS); 681 682 /// getFloatingTypeOrder - Compare the rank of the two specified floating 683 /// point types, ignoring the domain of the type (i.e. 'double' == 684 /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If 685 /// LHS < RHS, return -1. 686 int getFloatingTypeOrder(QualType LHS, QualType RHS); 687 688 /// getFloatingTypeOfSizeWithinDomain - Returns a real floating 689 /// point or a complex type (based on typeDomain/typeSize). 690 /// 'typeDomain' is a real floating point or complex type. 691 /// 'typeSize' is a real floating point or complex type. 692 QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize, 693 QualType typeDomain) const; 694 695private: 696 // Helper for integer ordering 697 unsigned getIntegerRank(Type* T); 698 699public: 700 701 //===--------------------------------------------------------------------===// 702 // Type Compatibility Predicates 703 //===--------------------------------------------------------------------===// 704 705 /// Compatibility predicates used to check assignment expressions. 706 bool typesAreCompatible(QualType, QualType); // C99 6.2.7p1 707 708 bool isObjCIdType(QualType T) const { 709 return T == ObjCIdType; 710 } 711 bool isObjCIdStructType(QualType T) const { 712 if (!IdStructType) // ObjC isn't enabled 713 return false; 714 return T->getAsStructureType() == IdStructType; 715 } 716 bool isObjCClassType(QualType T) const { 717 return T == ObjCClassType; 718 } 719 bool isObjCClassStructType(QualType T) const { 720 if (!ClassStructType) // ObjC isn't enabled 721 return false; 722 return T->getAsStructureType() == ClassStructType; 723 } 724 bool isObjCSelType(QualType T) const { 725 assert(SelStructType && "isObjCSelType used before 'SEL' type is built"); 726 return T->getAsStructureType() == SelStructType; 727 } 728 729 // Check the safety of assignment from LHS to RHS 730 bool canAssignObjCInterfaces(const ObjCInterfaceType *LHS, 731 const ObjCInterfaceType *RHS); 732 bool areComparableObjCPointerTypes(QualType LHS, QualType RHS); 733 734 // Functions for calculating composite types 735 QualType mergeTypes(QualType, QualType); 736 QualType mergeFunctionTypes(QualType, QualType); 737 738 //===--------------------------------------------------------------------===// 739 // Integer Predicates 740 //===--------------------------------------------------------------------===// 741 742 // The width of an integer, as defined in C99 6.2.6.2. This is the number 743 // of bits in an integer type excluding any padding bits. 744 unsigned getIntWidth(QualType T); 745 746 // Per C99 6.2.5p6, for every signed integer type, there is a corresponding 747 // unsigned integer type. This method takes a signed type, and returns the 748 // corresponding unsigned integer type. 749 QualType getCorrespondingUnsignedType(QualType T); 750 751 //===--------------------------------------------------------------------===// 752 // Type Iterators. 753 //===--------------------------------------------------------------------===// 754 755 typedef std::vector<Type*>::iterator type_iterator; 756 typedef std::vector<Type*>::const_iterator const_type_iterator; 757 758 type_iterator types_begin() { return Types.begin(); } 759 type_iterator types_end() { return Types.end(); } 760 const_type_iterator types_begin() const { return Types.begin(); } 761 const_type_iterator types_end() const { return Types.end(); } 762 763 //===--------------------------------------------------------------------===// 764 // Integer Values 765 //===--------------------------------------------------------------------===// 766 767 /// MakeIntValue - Make an APSInt of the appropriate width and 768 /// signedness for the given \arg Value and integer \arg Type. 769 llvm::APSInt MakeIntValue(uint64_t Value, QualType Type) { 770 llvm::APSInt Res(getIntWidth(Type), !Type->isSignedIntegerType()); 771 Res = Value; 772 return Res; 773 } 774 775private: 776 ASTContext(const ASTContext&); // DO NOT IMPLEMENT 777 void operator=(const ASTContext&); // DO NOT IMPLEMENT 778 779 void InitBuiltinTypes(); 780 void InitBuiltinType(QualType &R, BuiltinType::Kind K); 781 782 // Return the ObjC type encoding for a given type. 783 void getObjCEncodingForTypeImpl(QualType t, std::string &S, 784 bool ExpandPointedToStructures, 785 bool ExpandStructures, 786 const FieldDecl *Field, 787 bool OutermostType = false, 788 bool EncodingProperty = false); 789 790 const ASTRecordLayout &getObjCLayout(const ObjCInterfaceDecl *D, 791 const ObjCImplementationDecl *Impl); 792}; 793 794} // end namespace clang 795 796// operator new and delete aren't allowed inside namespaces. 797// The throw specifications are mandated by the standard. 798/// @brief Placement new for using the ASTContext's allocator. 799/// 800/// This placement form of operator new uses the ASTContext's allocator for 801/// obtaining memory. It is a non-throwing new, which means that it returns 802/// null on error. (If that is what the allocator does. The current does, so if 803/// this ever changes, this operator will have to be changed, too.) 804/// Usage looks like this (assuming there's an ASTContext 'Context' in scope): 805/// @code 806/// // Default alignment (16) 807/// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments); 808/// // Specific alignment 809/// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments); 810/// @endcode 811/// Please note that you cannot use delete on the pointer; it must be 812/// deallocated using an explicit destructor call followed by 813/// @c Context.Deallocate(Ptr). 814/// 815/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 816/// @param C The ASTContext that provides the allocator. 817/// @param Alignment The alignment of the allocated memory (if the underlying 818/// allocator supports it). 819/// @return The allocated memory. Could be NULL. 820inline void *operator new(size_t Bytes, clang::ASTContext &C, 821 size_t Alignment) throw () { 822 return C.Allocate(Bytes, Alignment); 823} 824/// @brief Placement delete companion to the new above. 825/// 826/// This operator is just a companion to the new above. There is no way of 827/// invoking it directly; see the new operator for more details. This operator 828/// is called implicitly by the compiler if a placement new expression using 829/// the ASTContext throws in the object constructor. 830inline void operator delete(void *Ptr, clang::ASTContext &C, size_t) 831 throw () { 832 C.Deallocate(Ptr); 833} 834 835/// This placement form of operator new[] uses the ASTContext's allocator for 836/// obtaining memory. It is a non-throwing new[], which means that it returns 837/// null on error. 838/// Usage looks like this (assuming there's an ASTContext 'Context' in scope): 839/// @code 840/// // Default alignment (16) 841/// char *data = new (Context) char[10]; 842/// // Specific alignment 843/// char *data = new (Context, 8) char[10]; 844/// @endcode 845/// Please note that you cannot use delete on the pointer; it must be 846/// deallocated using an explicit destructor call followed by 847/// @c Context.Deallocate(Ptr). 848/// 849/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 850/// @param C The ASTContext that provides the allocator. 851/// @param Alignment The alignment of the allocated memory (if the underlying 852/// allocator supports it). 853/// @return The allocated memory. Could be NULL. 854inline void *operator new[](size_t Bytes, clang::ASTContext& C, 855 size_t Alignment = 16) throw () { 856 return C.Allocate(Bytes, Alignment); 857} 858 859/// @brief Placement delete[] companion to the new[] above. 860/// 861/// This operator is just a companion to the new[] above. There is no way of 862/// invoking it directly; see the new[] operator for more details. This operator 863/// is called implicitly by the compiler if a placement new[] expression using 864/// the ASTContext throws in the object constructor. 865inline void operator delete[](void *Ptr, clang::ASTContext &C) throw () { 866 C.Deallocate(Ptr); 867} 868 869#endif 870