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