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