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