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