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