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