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