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