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