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