ASTContext.h revision 153bfe5795e2c1a5a738e73d3784964e082237fc
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/Builtins.h" 20#include "clang/AST/Decl.h" 21#include "clang/AST/NestedNameSpecifier.h" 22#include "clang/AST/TemplateName.h" 23#include "clang/AST/Type.h" 24#include "clang/Basic/SourceLocation.h" 25#include "llvm/ADT/DenseMap.h" 26#include "llvm/ADT/FoldingSet.h" 27#include "llvm/ADT/OwningPtr.h" 28#include "llvm/Bitcode/SerializationFwd.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 ObjCPropertyDecl; 48 class RecordDecl; 49 class TagDecl; 50 class TranslationUnitDecl; 51 class TypeDecl; 52 class TypedefDecl; 53 class TemplateTypeParmDecl; 54 class FieldDecl; 55 class ObjCIvarRefExpr; 56 class ObjCIvarDecl; 57 58/// ASTContext - This class holds long-lived AST nodes (such as types and 59/// decls) that can be referred to throughout the semantic analysis of a file. 60class ASTContext { 61 std::vector<Type*> Types; 62 llvm::FoldingSet<ExtQualType> ExtQualTypes; 63 llvm::FoldingSet<ComplexType> ComplexTypes; 64 llvm::FoldingSet<PointerType> PointerTypes; 65 llvm::FoldingSet<BlockPointerType> BlockPointerTypes; 66 llvm::FoldingSet<LValueReferenceType> LValueReferenceTypes; 67 llvm::FoldingSet<RValueReferenceType> RValueReferenceTypes; 68 llvm::FoldingSet<MemberPointerType> MemberPointerTypes; 69 llvm::FoldingSet<ConstantArrayType> ConstantArrayTypes; 70 llvm::FoldingSet<IncompleteArrayType> IncompleteArrayTypes; 71 std::vector<VariableArrayType*> VariableArrayTypes; 72 std::vector<DependentSizedArrayType*> DependentSizedArrayTypes; 73 llvm::FoldingSet<VectorType> VectorTypes; 74 llvm::FoldingSet<FunctionNoProtoType> FunctionNoProtoTypes; 75 llvm::FoldingSet<FunctionProtoType> FunctionProtoTypes; 76 llvm::FoldingSet<TemplateTypeParmType> TemplateTypeParmTypes; 77 llvm::FoldingSet<TemplateSpecializationType> TemplateSpecializationTypes; 78 llvm::FoldingSet<QualifiedNameType> QualifiedNameTypes; 79 llvm::FoldingSet<TypenameType> TypenameTypes; 80 llvm::FoldingSet<ObjCQualifiedInterfaceType> ObjCQualifiedInterfaceTypes; 81 llvm::FoldingSet<ObjCQualifiedIdType> ObjCQualifiedIdTypes; 82 83 llvm::FoldingSet<QualifiedTemplateName> QualifiedTemplateNames; 84 llvm::FoldingSet<DependentTemplateName> DependentTemplateNames; 85 86 /// \brief The set of nested name specifiers. 87 /// 88 /// This set is managed by the NestedNameSpecifier class. 89 llvm::FoldingSet<NestedNameSpecifier> NestedNameSpecifiers; 90 NestedNameSpecifier *GlobalNestedNameSpecifier; 91 friend class NestedNameSpecifier; 92 93 /// ASTRecordLayouts - A cache mapping from RecordDecls to ASTRecordLayouts. 94 /// This is lazily created. This is intentionally not serialized. 95 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*> ASTRecordLayouts; 96 llvm::DenseMap<const ObjCInterfaceDecl*, 97 const ASTRecordLayout*> ASTObjCInterfaces; 98 99 llvm::DenseMap<unsigned, FixedWidthIntType*> SignedFixedWidthIntTypes; 100 llvm::DenseMap<unsigned, FixedWidthIntType*> UnsignedFixedWidthIntTypes; 101 102 // FIXME: ASTRecordForInterface/ASTFieldForIvarRef and addRecordToClass and 103 // getFieldDecl be part of the backend (i.e. CodeGenTypes)? 104 llvm::DenseMap<const ObjCInterfaceDecl*, RecordDecl*> ASTRecordForInterface; 105 llvm::DenseMap<const ObjCIvarRefExpr*, const FieldDecl*> ASTFieldForIvarRef; 106 107 /// BuiltinVaListType - built-in va list type. 108 /// This is initially null and set by Sema::LazilyCreateBuiltin when 109 /// a builtin that takes a valist is encountered. 110 QualType BuiltinVaListType; 111 112 /// ObjCIdType - a pseudo built-in typedef type (set by Sema). 113 QualType ObjCIdType; 114 const RecordType *IdStructType; 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 ObjCClassType; 126 const RecordType *ClassStructType; 127 128 QualType ObjCConstantStringType; 129 RecordDecl *CFConstantStringTypeDecl; 130 131 RecordDecl *ObjCFastEnumerationStateTypeDecl; 132 133 TranslationUnitDecl *TUDecl; 134 135 /// SourceMgr - The associated SourceManager object. 136 SourceManager &SourceMgr; 137 138 /// LangOpts - The language options used to create the AST associated with 139 /// this ASTContext object. 140 LangOptions LangOpts; 141 142 /// MallocAlloc/BumpAlloc - The allocator objects used to create AST objects. 143 bool FreeMemory; 144 llvm::MallocAllocator MallocAlloc; 145 llvm::BumpPtrAllocator BumpAlloc; 146public: 147 TargetInfo &Target; 148 IdentifierTable &Idents; 149 SelectorTable &Selectors; 150 DeclarationNameTable DeclarationNames; 151 llvm::OwningPtr<ExternalASTSource> ExternalSource; 152 153 SourceManager& getSourceManager() { return SourceMgr; } 154 const SourceManager& getSourceManager() const { return SourceMgr; } 155 void *Allocate(unsigned Size, unsigned Align = 8) { 156 return FreeMemory ? MallocAlloc.Allocate(Size, Align) : 157 BumpAlloc.Allocate(Size, Align); 158 } 159 void Deallocate(void *Ptr) { 160 if (FreeMemory) 161 MallocAlloc.Deallocate(Ptr); 162 } 163 const LangOptions& getLangOptions() const { return LangOpts; } 164 165 FullSourceLoc getFullLoc(SourceLocation Loc) const { 166 return FullSourceLoc(Loc,SourceMgr); 167 } 168 169 TranslationUnitDecl *getTranslationUnitDecl() const { return TUDecl; } 170 171 /// This is intentionally not serialized. It is populated by the 172 /// ASTContext ctor, and there are no external pointers/references to 173 /// internal variables of BuiltinInfo. 174 Builtin::Context BuiltinInfo; 175 176 // Builtin Types. 177 QualType VoidTy; 178 QualType BoolTy; 179 QualType CharTy; 180 QualType WCharTy; // [C++ 3.9.1p5], integer type in C99. 181 QualType SignedCharTy, ShortTy, IntTy, LongTy, LongLongTy; 182 QualType UnsignedCharTy, UnsignedShortTy, UnsignedIntTy, UnsignedLongTy; 183 QualType UnsignedLongLongTy; 184 QualType FloatTy, DoubleTy, LongDoubleTy; 185 QualType FloatComplexTy, DoubleComplexTy, LongDoubleComplexTy; 186 QualType VoidPtrTy; 187 QualType OverloadTy; 188 QualType DependentTy; 189 190 ASTContext(const LangOptions& LOpts, SourceManager &SM, TargetInfo &t, 191 IdentifierTable &idents, SelectorTable &sels, 192 bool FreeMemory = true, unsigned size_reserve=0); 193 194 ~ASTContext(); 195 196 /// \brief Attach an external AST source to the AST context. 197 /// 198 /// The external AST source provides the ability to load parts of 199 /// the abstract syntax tree as needed from some external storage, 200 /// e.g., a precompiled header. 201 void setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source); 202 203 /// \brief Retrieve a pointer to the external AST source associated 204 /// with this AST context, if any. 205 ExternalASTSource *getExternalSource() const { return ExternalSource.get(); } 206 207 void PrintStats() const; 208 const std::vector<Type*>& getTypes() const { return Types; } 209 210 //===--------------------------------------------------------------------===// 211 // Type Constructors 212 //===--------------------------------------------------------------------===// 213 214 /// getAddSpaceQualType - Return the uniqued reference to the type for an 215 /// address space qualified type with the specified type and address space. 216 /// The resulting type has a union of the qualifiers from T and the address 217 /// space. If T already has an address space specifier, it is silently 218 /// replaced. 219 QualType getAddrSpaceQualType(QualType T, unsigned AddressSpace); 220 221 /// getObjCGCQualType - Returns the uniqued reference to the type for an 222 /// objc gc qualified type. The retulting type has a union of the qualifiers 223 /// from T and the gc attribute. 224 QualType getObjCGCQualType(QualType T, QualType::GCAttrTypes gcAttr); 225 226 /// getComplexType - Return the uniqued reference to the type for a complex 227 /// number with the specified element type. 228 QualType getComplexType(QualType T); 229 230 /// getPointerType - Return the uniqued reference to the type for a pointer to 231 /// the specified type. 232 QualType getPointerType(QualType T); 233 234 /// getBlockPointerType - Return the uniqued reference to the type for a block 235 /// of the specified type. 236 QualType getBlockPointerType(QualType T); 237 238 /// getLValueReferenceType - Return the uniqued reference to the type for an 239 /// lvalue reference to the specified type. 240 QualType getLValueReferenceType(QualType T); 241 242 /// getRValueReferenceType - Return the uniqued reference to the type for an 243 /// rvalue reference to the specified type. 244 QualType getRValueReferenceType(QualType T); 245 246 /// getMemberPointerType - Return the uniqued reference to the type for a 247 /// member pointer to the specified type in the specified class. The class 248 /// is a Type because it could be a dependent name. 249 QualType getMemberPointerType(QualType T, const Type *Cls); 250 251 /// getVariableArrayType - Returns a non-unique reference to the type for a 252 /// variable array of the specified element type. 253 QualType getVariableArrayType(QualType EltTy, Expr *NumElts, 254 ArrayType::ArraySizeModifier ASM, 255 unsigned EltTypeQuals); 256 257 /// getDependentSizedArrayType - Returns a non-unique reference to 258 /// the type for a dependently-sized array of the specified element 259 /// type. FIXME: We will need these to be uniqued, or at least 260 /// comparable, at some point. 261 QualType getDependentSizedArrayType(QualType EltTy, Expr *NumElts, 262 ArrayType::ArraySizeModifier ASM, 263 unsigned EltTypeQuals); 264 265 /// getIncompleteArrayType - Returns a unique reference to the type for a 266 /// incomplete array of the specified element type. 267 QualType getIncompleteArrayType(QualType EltTy, 268 ArrayType::ArraySizeModifier ASM, 269 unsigned EltTypeQuals); 270 271 /// getConstantArrayType - Return the unique reference to the type for a 272 /// constant array of the specified element type. 273 QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize, 274 ArrayType::ArraySizeModifier ASM, 275 unsigned EltTypeQuals); 276 277 /// getVectorType - Return the unique reference to a vector type of 278 /// the specified element type and size. VectorType must be a built-in type. 279 QualType getVectorType(QualType VectorType, unsigned NumElts); 280 281 /// getExtVectorType - Return the unique reference to an extended vector type 282 /// of the specified element type and size. VectorType must be a built-in 283 /// type. 284 QualType getExtVectorType(QualType VectorType, unsigned NumElts); 285 286 /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. 287 /// 288 QualType getFunctionNoProtoType(QualType ResultTy); 289 290 /// getFunctionType - Return a normal function type with a typed argument 291 /// list. isVariadic indicates whether the argument list includes '...'. 292 QualType getFunctionType(QualType ResultTy, const QualType *ArgArray, 293 unsigned NumArgs, bool isVariadic, 294 unsigned TypeQuals); 295 296 /// getTypeDeclType - Return the unique reference to the type for 297 /// the specified type declaration. 298 QualType getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl=0); 299 300 /// getTypedefType - Return the unique reference to the type for the 301 /// specified typename decl. 302 QualType getTypedefType(TypedefDecl *Decl); 303 QualType getObjCInterfaceType(ObjCInterfaceDecl *Decl); 304 305 QualType getTemplateTypeParmType(unsigned Depth, unsigned Index, 306 IdentifierInfo *Name = 0); 307 308 QualType getTemplateSpecializationType(TemplateName T, 309 const TemplateArgument *Args, 310 unsigned NumArgs, 311 QualType Canon = QualType()); 312 313 QualType getQualifiedNameType(NestedNameSpecifier *NNS, 314 QualType NamedType); 315 QualType getTypenameType(NestedNameSpecifier *NNS, 316 const IdentifierInfo *Name, 317 QualType Canon = QualType()); 318 QualType getTypenameType(NestedNameSpecifier *NNS, 319 const TemplateSpecializationType *TemplateId, 320 QualType Canon = QualType()); 321 322 /// getObjCQualifiedInterfaceType - Return a 323 /// ObjCQualifiedInterfaceType type for the given interface decl and 324 /// the conforming protocol list. 325 QualType getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl, 326 ObjCProtocolDecl **ProtocolList, 327 unsigned NumProtocols); 328 329 /// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for a 330 /// given 'id' and conforming protocol list. 331 QualType getObjCQualifiedIdType(ObjCProtocolDecl **ProtocolList, 332 unsigned NumProtocols); 333 334 335 /// getTypeOfType - GCC extension. 336 QualType getTypeOfExprType(Expr *e); 337 QualType getTypeOfType(QualType t); 338 339 /// getTagDeclType - Return the unique reference to the type for the 340 /// specified TagDecl (struct/union/class/enum) decl. 341 QualType getTagDeclType(TagDecl *Decl); 342 343 /// getSizeType - Return the unique type for "size_t" (C99 7.17), defined 344 /// in <stddef.h>. The sizeof operator requires this (C99 6.5.3.4p4). 345 QualType getSizeType() const; 346 347 /// getWCharType - In C++, this returns the unique wchar_t type. In C99, this 348 /// returns a type compatible with the type defined in <stddef.h> as defined 349 /// by the target. 350 QualType getWCharType() const { return WCharTy; } 351 352 /// getSignedWCharType - Return the type of "signed wchar_t". 353 /// Used when in C++, as a GCC extension. 354 QualType getSignedWCharType() const; 355 356 /// getUnsignedWCharType - Return the type of "unsigned wchar_t". 357 /// Used when in C++, as a GCC extension. 358 QualType getUnsignedWCharType() const; 359 360 /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) 361 /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). 362 QualType getPointerDiffType() const; 363 364 // getCFConstantStringType - Return the C structure type used to represent 365 // constant CFStrings. 366 QualType getCFConstantStringType(); 367 368 // This setter/getter represents the ObjC type for an NSConstantString. 369 void setObjCConstantStringInterface(ObjCInterfaceDecl *Decl); 370 QualType getObjCConstantStringInterface() const { 371 return ObjCConstantStringType; 372 } 373 374 //// This gets the struct used to keep track of fast enumerations. 375 QualType getObjCFastEnumerationStateType(); 376 377 /// getObjCEncodingForType - Emit the ObjC type encoding for the 378 /// given type into \arg S. If \arg NameFields is specified then 379 /// record field names are also encoded. 380 void getObjCEncodingForType(QualType t, std::string &S, 381 const FieldDecl *Field=0); 382 383 void getLegacyIntegralTypeEncoding(QualType &t) const; 384 385 // Put the string version of type qualifiers into S. 386 void getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, 387 std::string &S) const; 388 389 /// getObjCEncodingForMethodDecl - Return the encoded type for this method 390 /// declaration. 391 void getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, std::string &S); 392 393 /// getObjCEncodingForPropertyDecl - Return the encoded type for 394 /// this method declaration. If non-NULL, Container must be either 395 /// an ObjCCategoryImplDecl or ObjCImplementationDecl; it should 396 /// only be NULL when getting encodings for protocol properties. 397 void getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, 398 const Decl *Container, 399 std::string &S); 400 401 /// getObjCEncodingTypeSize returns size of type for objective-c encoding 402 /// purpose. 403 int getObjCEncodingTypeSize(QualType t); 404 405 /// This setter/getter represents the ObjC 'id' type. It is setup lazily, by 406 /// Sema. id is always a (typedef for a) pointer type, a pointer to a struct. 407 QualType getObjCIdType() const { return ObjCIdType; } 408 void setObjCIdType(TypedefDecl *Decl); 409 410 void setObjCSelType(TypedefDecl *Decl); 411 QualType getObjCSelType() const { return ObjCSelType; } 412 413 void setObjCProtoType(QualType QT); 414 QualType getObjCProtoType() const { return ObjCProtoType; } 415 416 /// This setter/getter repreents the ObjC 'Class' type. It is setup lazily, by 417 /// Sema. 'Class' is always a (typedef for a) pointer type, a pointer to a 418 /// struct. 419 QualType getObjCClassType() const { return ObjCClassType; } 420 void setObjCClassType(TypedefDecl *Decl); 421 422 void setBuiltinVaListType(QualType T); 423 QualType getBuiltinVaListType() const { return BuiltinVaListType; } 424 425 QualType getFixedWidthIntType(unsigned Width, bool Signed); 426 427 TemplateName getQualifiedTemplateName(NestedNameSpecifier *NNS, 428 bool TemplateKeyword, 429 TemplateDecl *Template); 430 431 TemplateName getDependentTemplateName(NestedNameSpecifier *NNS, 432 const IdentifierInfo *Name); 433 434private: 435 QualType getFromTargetType(unsigned Type) const; 436 437 //===--------------------------------------------------------------------===// 438 // Type Predicates. 439 //===--------------------------------------------------------------------===// 440 441public: 442 /// isObjCObjectPointerType - Returns true if type is an Objective-C pointer 443 /// to an object type. This includes "id" and "Class" (two 'special' pointers 444 /// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified 445 /// ID type). 446 bool isObjCObjectPointerType(QualType Ty) const; 447 448 /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's 449 /// garbage collection attribute. 450 /// 451 QualType::GCAttrTypes getObjCGCAttrKind(const QualType &Ty) const; 452 453 /// isObjCNSObjectType - Return true if this is an NSObject object with 454 /// its NSObject attribute set. 455 bool isObjCNSObjectType(QualType Ty) const; 456 457 //===--------------------------------------------------------------------===// 458 // Type Sizing and Analysis 459 //===--------------------------------------------------------------------===// 460 461 /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified 462 /// scalar floating point type. 463 const llvm::fltSemantics &getFloatTypeSemantics(QualType T) const; 464 465 /// getTypeInfo - Get the size and alignment of the specified complete type in 466 /// bits. 467 std::pair<uint64_t, unsigned> getTypeInfo(const Type *T); 468 std::pair<uint64_t, unsigned> getTypeInfo(QualType T) { 469 return getTypeInfo(T.getTypePtr()); 470 } 471 472 /// getTypeSize - Return the size of the specified type, in bits. This method 473 /// does not work on incomplete types. 474 uint64_t getTypeSize(QualType T) { 475 return getTypeInfo(T).first; 476 } 477 uint64_t getTypeSize(const Type *T) { 478 return getTypeInfo(T).first; 479 } 480 481 /// getTypeAlign - Return the ABI-specified alignment of a type, in bits. 482 /// This method does not work on incomplete types. 483 unsigned getTypeAlign(QualType T) { 484 return getTypeInfo(T).second; 485 } 486 unsigned getTypeAlign(const Type *T) { 487 return getTypeInfo(T).second; 488 } 489 490 /// getPreferredTypeAlign - Return the "preferred" alignment of the specified 491 /// type for the current target in bits. This can be different than the ABI 492 /// alignment in cases where it is beneficial for performance to overalign 493 /// a data type. 494 unsigned getPreferredTypeAlign(const Type *T); 495 496 /// getDeclAlignInBytes - Return the alignment of the specified decl 497 /// that should be returned by __alignof(). Note that bitfields do 498 /// not have a valid alignment, so this method will assert on them. 499 unsigned getDeclAlignInBytes(const Decl *D); 500 501 /// getASTRecordLayout - Get or compute information about the layout of the 502 /// specified record (struct/union/class), which indicates its size and field 503 /// position information. 504 const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D); 505 506 const ASTRecordLayout &getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D); 507 const RecordDecl *addRecordToClass(const ObjCInterfaceDecl *D); 508 void CollectObjCIvars(const ObjCInterfaceDecl *OI, 509 llvm::SmallVectorImpl<FieldDecl*> &Fields); 510 const FieldDecl *getFieldDecl(const ObjCIvarRefExpr *MRef) { 511 llvm::DenseMap<const ObjCIvarRefExpr *, const FieldDecl*>::iterator I 512 = ASTFieldForIvarRef.find(MRef); 513 assert (I != ASTFieldForIvarRef.end() && "Unable to find field_decl"); 514 return I->second; 515 } 516 void setFieldDecl(const ObjCInterfaceDecl *OI, 517 const ObjCIvarDecl *Ivar, 518 const ObjCIvarRefExpr *MRef); 519 //===--------------------------------------------------------------------===// 520 // Type Operators 521 //===--------------------------------------------------------------------===// 522 523 /// getCanonicalType - Return the canonical (structural) type corresponding to 524 /// the specified potentially non-canonical type. The non-canonical version 525 /// of a type may have many "decorated" versions of types. Decorators can 526 /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed 527 /// to be free of any of these, allowing two canonical types to be compared 528 /// for exact equality with a simple pointer comparison. 529 QualType getCanonicalType(QualType T); 530 const Type *getCanonicalType(const Type *T) { 531 return T->getCanonicalTypeInternal().getTypePtr(); 532 } 533 534 /// \brief Determine whether the given types are equivalent. 535 bool hasSameType(QualType T1, QualType T2) { 536 return getCanonicalType(T1) == getCanonicalType(T2); 537 } 538 539 /// \brief Determine whether the given types are equivalent after 540 /// cvr-qualifiers have been removed. 541 bool hasSameUnqualifiedType(QualType T1, QualType T2) { 542 T1 = getCanonicalType(T1); 543 T2 = getCanonicalType(T2); 544 return T1.getUnqualifiedType() == T2.getUnqualifiedType(); 545 } 546 547 /// \brief Retrieves the "canonical" declaration of the given tag 548 /// declaration. 549 /// 550 /// The canonical declaration for the given tag declaration is 551 /// either the definition of the tag (if it is a complete type) or 552 /// the first declaration of that tag. 553 TagDecl *getCanonicalDecl(TagDecl *Tag) { 554 QualType T = getTagDeclType(Tag); 555 return cast<TagDecl>(cast<TagType>(T.getTypePtr()->CanonicalType) 556 ->getDecl()); 557 } 558 559 /// \brief Retrieves the "canonical" nested name specifier for a 560 /// given nested name specifier. 561 /// 562 /// The canonical nested name specifier is a nested name specifier 563 /// that uniquely identifies a type or namespace within the type 564 /// system. For example, given: 565 /// 566 /// \code 567 /// namespace N { 568 /// struct S { 569 /// template<typename T> struct X { typename T* type; }; 570 /// }; 571 /// } 572 /// 573 /// template<typename T> struct Y { 574 /// typename N::S::X<T>::type member; 575 /// }; 576 /// \endcode 577 /// 578 /// Here, the nested-name-specifier for N::S::X<T>:: will be 579 /// S::X<template-param-0-0>, since 'S' and 'X' are uniquely defined 580 /// by declarations in the type system and the canonical type for 581 /// the template type parameter 'T' is template-param-0-0. 582 NestedNameSpecifier * 583 getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS); 584 585 /// Type Query functions. If the type is an instance of the specified class, 586 /// return the Type pointer for the underlying maximally pretty type. This 587 /// is a member of ASTContext because this may need to do some amount of 588 /// canonicalization, e.g. to move type qualifiers into the element type. 589 const ArrayType *getAsArrayType(QualType T); 590 const ConstantArrayType *getAsConstantArrayType(QualType T) { 591 return dyn_cast_or_null<ConstantArrayType>(getAsArrayType(T)); 592 } 593 const VariableArrayType *getAsVariableArrayType(QualType T) { 594 return dyn_cast_or_null<VariableArrayType>(getAsArrayType(T)); 595 } 596 const IncompleteArrayType *getAsIncompleteArrayType(QualType T) { 597 return dyn_cast_or_null<IncompleteArrayType>(getAsArrayType(T)); 598 } 599 600 /// getBaseElementType - Returns the innermost element type of a variable 601 /// length array type. For example, will return "int" for int[m][n] 602 QualType getBaseElementType(const VariableArrayType *VAT); 603 604 /// getArrayDecayedType - Return the properly qualified result of decaying the 605 /// specified array type to a pointer. This operation is non-trivial when 606 /// handling typedefs etc. The canonical type of "T" must be an array type, 607 /// this returns a pointer to a properly qualified element of the array. 608 /// 609 /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. 610 QualType getArrayDecayedType(QualType T); 611 612 /// getIntegerTypeOrder - Returns the highest ranked integer type: 613 /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If 614 /// LHS < RHS, return -1. 615 int getIntegerTypeOrder(QualType LHS, QualType RHS); 616 617 /// getFloatingTypeOrder - Compare the rank of the two specified floating 618 /// point types, ignoring the domain of the type (i.e. 'double' == 619 /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If 620 /// LHS < RHS, return -1. 621 int getFloatingTypeOrder(QualType LHS, QualType RHS); 622 623 /// getFloatingTypeOfSizeWithinDomain - Returns a real floating 624 /// point or a complex type (based on typeDomain/typeSize). 625 /// 'typeDomain' is a real floating point or complex type. 626 /// 'typeSize' is a real floating point or complex type. 627 QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize, 628 QualType typeDomain) const; 629 630private: 631 // Helper for integer ordering 632 unsigned getIntegerRank(Type* T); 633 634public: 635 636 //===--------------------------------------------------------------------===// 637 // Type Compatibility Predicates 638 //===--------------------------------------------------------------------===// 639 640 /// Compatibility predicates used to check assignment expressions. 641 bool typesAreCompatible(QualType, QualType); // C99 6.2.7p1 642 bool typesAreBlockCompatible(QualType lhs, QualType rhs); 643 644 bool isObjCIdType(QualType T) const { 645 return T == ObjCIdType; 646 } 647 bool isObjCIdStructType(QualType T) const { 648 if (!IdStructType) // ObjC isn't enabled 649 return false; 650 return T->getAsStructureType() == IdStructType; 651 } 652 bool isObjCClassType(QualType T) const { 653 return T == ObjCClassType; 654 } 655 bool isObjCClassStructType(QualType T) const { 656 if (!ClassStructType) // ObjC isn't enabled 657 return false; 658 return T->getAsStructureType() == ClassStructType; 659 } 660 bool isObjCSelType(QualType T) const { 661 assert(SelStructType && "isObjCSelType used before 'SEL' type is built"); 662 return T->getAsStructureType() == SelStructType; 663 } 664 665 // Check the safety of assignment from LHS to RHS 666 bool canAssignObjCInterfaces(const ObjCInterfaceType *LHS, 667 const ObjCInterfaceType *RHS); 668 bool areComparableObjCPointerTypes(QualType LHS, QualType RHS); 669 670 // Functions for calculating composite types 671 QualType mergeTypes(QualType, QualType); 672 QualType mergeFunctionTypes(QualType, QualType); 673 674 //===--------------------------------------------------------------------===// 675 // Integer Predicates 676 //===--------------------------------------------------------------------===// 677 678 // The width of an integer, as defined in C99 6.2.6.2. This is the number 679 // of bits in an integer type excluding any padding bits. 680 unsigned getIntWidth(QualType T); 681 682 // Per C99 6.2.5p6, for every signed integer type, there is a corresponding 683 // unsigned integer type. This method takes a signed type, and returns the 684 // corresponding unsigned integer type. 685 QualType getCorrespondingUnsignedType(QualType T); 686 687 //===--------------------------------------------------------------------===// 688 // Type Iterators. 689 //===--------------------------------------------------------------------===// 690 691 typedef std::vector<Type*>::iterator type_iterator; 692 typedef std::vector<Type*>::const_iterator const_type_iterator; 693 694 type_iterator types_begin() { return Types.begin(); } 695 type_iterator types_end() { return Types.end(); } 696 const_type_iterator types_begin() const { return Types.begin(); } 697 const_type_iterator types_end() const { return Types.end(); } 698 699 //===--------------------------------------------------------------------===// 700 // Serialization 701 //===--------------------------------------------------------------------===// 702 703 void EmitASTBitcodeBuffer(std::vector<unsigned char> &Buffer) const; 704 static ASTContext *ReadASTBitcodeBuffer(llvm::MemoryBuffer &MBuffer, 705 FileManager &FMgr); 706 707 void Emit(llvm::Serializer& S) const; 708 static ASTContext *Create(llvm::Deserializer& D); 709 710 //===--------------------------------------------------------------------===// 711 // Integer Values 712 //===--------------------------------------------------------------------===// 713 714 /// MakeIntValue - Make an APSInt of the appropriate width and 715 /// signedness for the given \arg Value and integer \arg Type. 716 llvm::APSInt MakeIntValue(uint64_t Value, QualType Type) { 717 llvm::APSInt Res(getIntWidth(Type), !Type->isSignedIntegerType()); 718 Res = Value; 719 return Res; 720 } 721 722private: 723 ASTContext(const ASTContext&); // DO NOT IMPLEMENT 724 void operator=(const ASTContext&); // DO NOT IMPLEMENT 725 726 void InitBuiltinTypes(); 727 void InitBuiltinType(QualType &R, BuiltinType::Kind K); 728 729 // Return the ObjC type encoding for a given type. 730 void getObjCEncodingForTypeImpl(QualType t, std::string &S, 731 bool ExpandPointedToStructures, 732 bool ExpandStructures, 733 const FieldDecl *Field, 734 bool OutermostType = false, 735 bool EncodingProperty = false); 736 737}; 738 739} // end namespace clang 740 741// operator new and delete aren't allowed inside namespaces. 742// The throw specifications are mandated by the standard. 743/// @brief Placement new for using the ASTContext's allocator. 744/// 745/// This placement form of operator new uses the ASTContext's allocator for 746/// obtaining memory. It is a non-throwing new, which means that it returns 747/// null on error. (If that is what the allocator does. The current does, so if 748/// this ever changes, this operator will have to be changed, too.) 749/// Usage looks like this (assuming there's an ASTContext 'Context' in scope): 750/// @code 751/// // Default alignment (16) 752/// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments); 753/// // Specific alignment 754/// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments); 755/// @endcode 756/// Please note that you cannot use delete on the pointer; it must be 757/// deallocated using an explicit destructor call followed by 758/// @c Context.Deallocate(Ptr). 759/// 760/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 761/// @param C The ASTContext that provides the allocator. 762/// @param Alignment The alignment of the allocated memory (if the underlying 763/// allocator supports it). 764/// @return The allocated memory. Could be NULL. 765inline void *operator new(size_t Bytes, clang::ASTContext &C, 766 size_t Alignment = 16) throw () { 767 return C.Allocate(Bytes, Alignment); 768} 769/// @brief Placement delete companion to the new above. 770/// 771/// This operator is just a companion to the new above. There is no way of 772/// invoking it directly; see the new operator for more details. This operator 773/// is called implicitly by the compiler if a placement new expression using 774/// the ASTContext throws in the object constructor. 775inline void operator delete(void *Ptr, clang::ASTContext &C, size_t) 776 throw () { 777 C.Deallocate(Ptr); 778} 779 780/// This placement form of operator new[] uses the ASTContext's allocator for 781/// obtaining memory. It is a non-throwing new[], which means that it returns 782/// null on error. 783/// Usage looks like this (assuming there's an ASTContext 'Context' in scope): 784/// @code 785/// // Default alignment (16) 786/// char *data = new (Context) char[10]; 787/// // Specific alignment 788/// char *data = new (Context, 8) char[10]; 789/// @endcode 790/// Please note that you cannot use delete on the pointer; it must be 791/// deallocated using an explicit destructor call followed by 792/// @c Context.Deallocate(Ptr). 793/// 794/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 795/// @param C The ASTContext that provides the allocator. 796/// @param Alignment The alignment of the allocated memory (if the underlying 797/// allocator supports it). 798/// @return The allocated memory. Could be NULL. 799inline void *operator new[](size_t Bytes, clang::ASTContext& C, 800 size_t Alignment = 16) throw () { 801 return C.Allocate(Bytes, Alignment); 802} 803 804/// @brief Placement delete[] companion to the new[] above. 805/// 806/// This operator is just a companion to the new[] above. There is no way of 807/// invoking it directly; see the new[] operator for more details. This operator 808/// is called implicitly by the compiler if a placement new[] expression using 809/// the ASTContext throws in the object constructor. 810inline void operator delete[](void *Ptr, clang::ASTContext &C) throw () { 811 C.Deallocate(Ptr); 812} 813 814#endif 815