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