ASTContext.h revision fb523e16dd1f860ff02a3ae03e5e3e25327a5860
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 291 QualType getTemplateTypeParmType(unsigned Depth, unsigned Index, 292 IdentifierInfo *Name = 0); 293 294 QualType getTemplateSpecializationType(TemplateName T, 295 const TemplateArgument *Args, 296 unsigned NumArgs, 297 QualType Canon = QualType()); 298 299 QualType getQualifiedNameType(NestedNameSpecifier *NNS, 300 QualType NamedType); 301 QualType getTypenameType(NestedNameSpecifier *NNS, 302 const IdentifierInfo *Name, 303 QualType Canon = QualType()); 304 QualType getTypenameType(NestedNameSpecifier *NNS, 305 const TemplateSpecializationType *TemplateId, 306 QualType Canon = QualType()); 307 308 /// getObjCQualifiedInterfaceType - Return a 309 /// ObjCQualifiedInterfaceType type for the given interface decl and 310 /// the conforming protocol list. 311 QualType getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl, 312 ObjCProtocolDecl **ProtocolList, 313 unsigned NumProtocols); 314 315 /// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for a 316 /// given 'id' and conforming protocol list. 317 QualType getObjCQualifiedIdType(ObjCProtocolDecl **ProtocolList, 318 unsigned NumProtocols); 319 320 321 /// getTypeOfType - GCC extension. 322 QualType getTypeOfExprType(Expr *e); 323 QualType getTypeOfType(QualType t); 324 325 /// getTagDeclType - Return the unique reference to the type for the 326 /// specified TagDecl (struct/union/class/enum) decl. 327 QualType getTagDeclType(TagDecl *Decl); 328 329 /// getSizeType - Return the unique type for "size_t" (C99 7.17), defined 330 /// in <stddef.h>. The sizeof operator requires this (C99 6.5.3.4p4). 331 QualType getSizeType() const; 332 333 /// getWCharType - In C++, this returns the unique wchar_t type. In C99, this 334 /// returns a type compatible with the type defined in <stddef.h> as defined 335 /// by the target. 336 QualType getWCharType() const { return WCharTy; } 337 338 /// getSignedWCharType - Return the type of "signed wchar_t". 339 /// Used when in C++, as a GCC extension. 340 QualType getSignedWCharType() const; 341 342 /// getUnsignedWCharType - Return the type of "unsigned wchar_t". 343 /// Used when in C++, as a GCC extension. 344 QualType getUnsignedWCharType() const; 345 346 /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) 347 /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). 348 QualType getPointerDiffType() const; 349 350 // getCFConstantStringType - Return the C structure type used to represent 351 // constant CFStrings. 352 QualType getCFConstantStringType(); 353 354 // This setter/getter represents the ObjC type for an NSConstantString. 355 void setObjCConstantStringInterface(ObjCInterfaceDecl *Decl); 356 QualType getObjCConstantStringInterface() const { 357 return ObjCConstantStringType; 358 } 359 360 //// This gets the struct used to keep track of fast enumerations. 361 QualType getObjCFastEnumerationStateType(); 362 363 /// getObjCEncodingForType - Emit the ObjC type encoding for the 364 /// given type into \arg S. If \arg NameFields is specified then 365 /// record field names are also encoded. 366 void getObjCEncodingForType(QualType t, std::string &S, 367 FieldDecl *Field=NULL) const; 368 369 void getLegacyIntegralTypeEncoding(QualType &t) const; 370 371 // Put the string version of type qualifiers into S. 372 void getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, 373 std::string &S) const; 374 375 /// getObjCEncodingForMethodDecl - Return the encoded type for this method 376 /// declaration. 377 void getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, std::string &S); 378 379 /// getObjCEncodingForPropertyDecl - Return the encoded type for 380 /// this method declaration. If non-NULL, Container must be either 381 /// an ObjCCategoryImplDecl or ObjCImplementationDecl; it should 382 /// only be NULL when getting encodings for protocol properties. 383 void getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, 384 const Decl *Container, 385 std::string &S); 386 387 /// getObjCEncodingTypeSize returns size of type for objective-c encoding 388 /// purpose. 389 int getObjCEncodingTypeSize(QualType t); 390 391 /// This setter/getter represents the ObjC 'id' type. It is setup lazily, by 392 /// Sema. id is always a (typedef for a) pointer type, a pointer to a struct. 393 QualType getObjCIdType() const { return ObjCIdType; } 394 void setObjCIdType(TypedefDecl *Decl); 395 396 void setObjCSelType(TypedefDecl *Decl); 397 QualType getObjCSelType() const { return ObjCSelType; } 398 399 void setObjCProtoType(QualType QT); 400 QualType getObjCProtoType() const { return ObjCProtoType; } 401 402 /// This setter/getter repreents the ObjC 'Class' type. It is setup lazily, by 403 /// Sema. 'Class' is always a (typedef for a) pointer type, a pointer to a 404 /// struct. 405 QualType getObjCClassType() const { return ObjCClassType; } 406 void setObjCClassType(TypedefDecl *Decl); 407 408 void setBuiltinVaListType(QualType T); 409 QualType getBuiltinVaListType() const { return BuiltinVaListType; } 410 411 QualType getFixedWidthIntType(unsigned Width, bool Signed); 412 413 TemplateName getQualifiedTemplateName(NestedNameSpecifier *NNS, 414 bool TemplateKeyword, 415 TemplateDecl *Template); 416 417 TemplateName getDependentTemplateName(NestedNameSpecifier *NNS, 418 const IdentifierInfo *Name); 419 420private: 421 QualType getFromTargetType(unsigned Type) const; 422 423 //===--------------------------------------------------------------------===// 424 // Type Predicates. 425 //===--------------------------------------------------------------------===// 426 427public: 428 /// isObjCObjectPointerType - Returns true if type is an Objective-C pointer 429 /// to an object type. This includes "id" and "Class" (two 'special' pointers 430 /// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified 431 /// ID type). 432 bool isObjCObjectPointerType(QualType Ty) const; 433 434 /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's 435 /// garbage collection attribute. 436 /// 437 QualType::GCAttrTypes getObjCGCAttrKind(const QualType &Ty) const; 438 439 /// isObjCNSObjectType - Return true if this is an NSObject object with 440 /// its NSObject attribute set. 441 bool isObjCNSObjectType(QualType Ty) const; 442 443 //===--------------------------------------------------------------------===// 444 // Type Sizing and Analysis 445 //===--------------------------------------------------------------------===// 446 447 /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified 448 /// scalar floating point type. 449 const llvm::fltSemantics &getFloatTypeSemantics(QualType T) const; 450 451 /// getTypeInfo - Get the size and alignment of the specified complete type in 452 /// bits. 453 std::pair<uint64_t, unsigned> getTypeInfo(const Type *T); 454 std::pair<uint64_t, unsigned> getTypeInfo(QualType T) { 455 return getTypeInfo(T.getTypePtr()); 456 } 457 458 /// getTypeSize - Return the size of the specified type, in bits. This method 459 /// does not work on incomplete types. 460 uint64_t getTypeSize(QualType T) { 461 return getTypeInfo(T).first; 462 } 463 uint64_t getTypeSize(const Type *T) { 464 return getTypeInfo(T).first; 465 } 466 467 /// getTypeAlign - Return the ABI-specified alignment of a type, in bits. 468 /// This method does not work on incomplete types. 469 unsigned getTypeAlign(QualType T) { 470 return getTypeInfo(T).second; 471 } 472 unsigned getTypeAlign(const Type *T) { 473 return getTypeInfo(T).second; 474 } 475 476 /// getPreferredTypeAlign - Return the "preferred" alignment of the specified 477 /// type for the current target in bits. This can be different than the ABI 478 /// alignment in cases where it is beneficial for performance to overalign 479 /// a data type. 480 unsigned getPreferredTypeAlign(const Type *T); 481 482 /// getDeclAlignInBytes - Return the alignment of the specified decl 483 /// that should be returned by __alignof(). Note that bitfields do 484 /// not have a valid alignment, so this method will assert on them. 485 unsigned getDeclAlignInBytes(const Decl *D); 486 487 /// getASTRecordLayout - Get or compute information about the layout of the 488 /// specified record (struct/union/class), which indicates its size and field 489 /// position information. 490 const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D); 491 492 const ASTRecordLayout &getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D); 493 const RecordDecl *addRecordToClass(const ObjCInterfaceDecl *D); 494 void CollectObjCIvars(const ObjCInterfaceDecl *OI, 495 llvm::SmallVectorImpl<FieldDecl*> &Fields) const; 496 const FieldDecl *getFieldDecl(const ObjCIvarRefExpr *MRef) { 497 llvm::DenseMap<const ObjCIvarRefExpr *, const FieldDecl*>::iterator I 498 = ASTFieldForIvarRef.find(MRef); 499 assert (I != ASTFieldForIvarRef.end() && "Unable to find field_decl"); 500 return I->second; 501 } 502 void setFieldDecl(const ObjCInterfaceDecl *OI, 503 const ObjCIvarDecl *Ivar, 504 const ObjCIvarRefExpr *MRef); 505 //===--------------------------------------------------------------------===// 506 // Type Operators 507 //===--------------------------------------------------------------------===// 508 509 /// getCanonicalType - Return the canonical (structural) type corresponding to 510 /// the specified potentially non-canonical type. The non-canonical version 511 /// of a type may have many "decorated" versions of types. Decorators can 512 /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed 513 /// to be free of any of these, allowing two canonical types to be compared 514 /// for exact equality with a simple pointer comparison. 515 QualType getCanonicalType(QualType T); 516 const Type *getCanonicalType(const Type *T) { 517 return T->getCanonicalTypeInternal().getTypePtr(); 518 } 519 520 /// \brief Determine whether the given types are equivalent. 521 bool hasSameType(QualType T1, QualType T2) { 522 return getCanonicalType(T1) == getCanonicalType(T2); 523 } 524 525 /// \brief Determine whether the given types are equivalent after 526 /// cvr-qualifiers have been removed. 527 bool hasSameUnqualifiedType(QualType T1, QualType T2) { 528 T1 = getCanonicalType(T1); 529 T2 = getCanonicalType(T2); 530 return T1.getUnqualifiedType() == T2.getUnqualifiedType(); 531 } 532 533 /// \brief Retrieves the "canonical" declaration of the given tag 534 /// declaration. 535 /// 536 /// The canonical declaration for the given tag declaration is 537 /// either the definition of the tag (if it is a complete type) or 538 /// the first declaration of that tag. 539 TagDecl *getCanonicalDecl(TagDecl *Tag) { 540 QualType T = getTagDeclType(Tag); 541 return cast<TagDecl>(cast<TagType>(T.getTypePtr()->CanonicalType) 542 ->getDecl()); 543 } 544 545 /// \brief Retrieves the "canonical" nested name specifier for a 546 /// given nested name specifier. 547 /// 548 /// The canonical nested name specifier is a nested name specifier 549 /// that uniquely identifies a type or namespace within the type 550 /// system. For example, given: 551 /// 552 /// \code 553 /// namespace N { 554 /// struct S { 555 /// template<typename T> struct X { typename T* type; }; 556 /// }; 557 /// } 558 /// 559 /// template<typename T> struct Y { 560 /// typename N::S::X<T>::type member; 561 /// }; 562 /// \endcode 563 /// 564 /// Here, the nested-name-specifier for N::S::X<T>:: will be 565 /// S::X<template-param-0-0>, since 'S' and 'X' are uniquely defined 566 /// by declarations in the type system and the canonical type for 567 /// the template type parameter 'T' is template-param-0-0. 568 NestedNameSpecifier * 569 getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS); 570 571 /// Type Query functions. If the type is an instance of the specified class, 572 /// return the Type pointer for the underlying maximally pretty type. This 573 /// is a member of ASTContext because this may need to do some amount of 574 /// canonicalization, e.g. to move type qualifiers into the element type. 575 const ArrayType *getAsArrayType(QualType T); 576 const ConstantArrayType *getAsConstantArrayType(QualType T) { 577 return dyn_cast_or_null<ConstantArrayType>(getAsArrayType(T)); 578 } 579 const VariableArrayType *getAsVariableArrayType(QualType T) { 580 return dyn_cast_or_null<VariableArrayType>(getAsArrayType(T)); 581 } 582 const IncompleteArrayType *getAsIncompleteArrayType(QualType T) { 583 return dyn_cast_or_null<IncompleteArrayType>(getAsArrayType(T)); 584 } 585 586 /// getBaseElementType - Returns the innermost element type of a variable 587 /// length array type. For example, will return "int" for int[m][n] 588 QualType getBaseElementType(const VariableArrayType *VAT); 589 590 /// getArrayDecayedType - Return the properly qualified result of decaying the 591 /// specified array type to a pointer. This operation is non-trivial when 592 /// handling typedefs etc. The canonical type of "T" must be an array type, 593 /// this returns a pointer to a properly qualified element of the array. 594 /// 595 /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. 596 QualType getArrayDecayedType(QualType T); 597 598 /// getIntegerTypeOrder - Returns the highest ranked integer type: 599 /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If 600 /// LHS < RHS, return -1. 601 int getIntegerTypeOrder(QualType LHS, QualType RHS); 602 603 /// getFloatingTypeOrder - Compare the rank of the two specified floating 604 /// point types, ignoring the domain of the type (i.e. 'double' == 605 /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If 606 /// LHS < RHS, return -1. 607 int getFloatingTypeOrder(QualType LHS, QualType RHS); 608 609 /// getFloatingTypeOfSizeWithinDomain - Returns a real floating 610 /// point or a complex type (based on typeDomain/typeSize). 611 /// 'typeDomain' is a real floating point or complex type. 612 /// 'typeSize' is a real floating point or complex type. 613 QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize, 614 QualType typeDomain) const; 615 616private: 617 // Helper for integer ordering 618 unsigned getIntegerRank(Type* T); 619 620public: 621 622 //===--------------------------------------------------------------------===// 623 // Type Compatibility Predicates 624 //===--------------------------------------------------------------------===// 625 626 /// Compatibility predicates used to check assignment expressions. 627 bool typesAreCompatible(QualType, QualType); // C99 6.2.7p1 628 bool typesAreBlockCompatible(QualType lhs, QualType rhs); 629 630 bool isObjCIdType(QualType T) const { 631 return T == ObjCIdType; 632 } 633 bool isObjCIdStructType(QualType T) const { 634 if (!IdStructType) // ObjC isn't enabled 635 return false; 636 return T->getAsStructureType() == IdStructType; 637 } 638 bool isObjCClassType(QualType T) const { 639 return T == ObjCClassType; 640 } 641 bool isObjCClassStructType(QualType T) const { 642 if (!ClassStructType) // ObjC isn't enabled 643 return false; 644 return T->getAsStructureType() == ClassStructType; 645 } 646 bool isObjCSelType(QualType T) const { 647 assert(SelStructType && "isObjCSelType used before 'SEL' type is built"); 648 return T->getAsStructureType() == SelStructType; 649 } 650 651 // Check the safety of assignment from LHS to RHS 652 bool canAssignObjCInterfaces(const ObjCInterfaceType *LHS, 653 const ObjCInterfaceType *RHS); 654 bool areComparableObjCPointerTypes(QualType LHS, QualType RHS); 655 656 // Functions for calculating composite types 657 QualType mergeTypes(QualType, QualType); 658 QualType mergeFunctionTypes(QualType, QualType); 659 660 //===--------------------------------------------------------------------===// 661 // Integer Predicates 662 //===--------------------------------------------------------------------===// 663 664 // The width of an integer, as defined in C99 6.2.6.2. This is the number 665 // of bits in an integer type excluding any padding bits. 666 unsigned getIntWidth(QualType T); 667 668 // Per C99 6.2.5p6, for every signed integer type, there is a corresponding 669 // unsigned integer type. This method takes a signed type, and returns the 670 // corresponding unsigned integer type. 671 QualType getCorrespondingUnsignedType(QualType T); 672 673 //===--------------------------------------------------------------------===// 674 // Type Iterators. 675 //===--------------------------------------------------------------------===// 676 677 typedef std::vector<Type*>::iterator type_iterator; 678 typedef std::vector<Type*>::const_iterator const_type_iterator; 679 680 type_iterator types_begin() { return Types.begin(); } 681 type_iterator types_end() { return Types.end(); } 682 const_type_iterator types_begin() const { return Types.begin(); } 683 const_type_iterator types_end() const { return Types.end(); } 684 685 //===--------------------------------------------------------------------===// 686 // Serialization 687 //===--------------------------------------------------------------------===// 688 689 void EmitASTBitcodeBuffer(std::vector<unsigned char> &Buffer) const; 690 static ASTContext *ReadASTBitcodeBuffer(llvm::MemoryBuffer &MBuffer, 691 FileManager &FMgr); 692 693 void Emit(llvm::Serializer& S) const; 694 static ASTContext *Create(llvm::Deserializer& D); 695 696 //===--------------------------------------------------------------------===// 697 // Integer Values 698 //===--------------------------------------------------------------------===// 699 700 /// MakeIntValue - Make an APSInt of the appropriate width and 701 /// signedness for the given \arg Value and integer \arg Type. 702 llvm::APSInt MakeIntValue(uint64_t Value, QualType Type) { 703 llvm::APSInt Res(getIntWidth(Type), !Type->isSignedIntegerType()); 704 Res = Value; 705 return Res; 706 } 707 708private: 709 ASTContext(const ASTContext&); // DO NOT IMPLEMENT 710 void operator=(const ASTContext&); // DO NOT IMPLEMENT 711 712 void InitBuiltinTypes(); 713 void InitBuiltinType(QualType &R, BuiltinType::Kind K); 714 715 // Return the ObjC type encoding for a given type. 716 void getObjCEncodingForTypeImpl(QualType t, std::string &S, 717 bool ExpandPointedToStructures, 718 bool ExpandStructures, 719 FieldDecl *Field, 720 bool OutermostType = false, 721 bool EncodingProperty = false) const; 722 723}; 724 725} // end namespace clang 726 727// operator new and delete aren't allowed inside namespaces. 728// The throw specifications are mandated by the standard. 729/// @brief Placement new for using the ASTContext's allocator. 730/// 731/// This placement form of operator new uses the ASTContext's allocator for 732/// obtaining memory. It is a non-throwing new, which means that it returns 733/// null on error. (If that is what the allocator does. The current does, so if 734/// this ever changes, this operator will have to be changed, too.) 735/// Usage looks like this (assuming there's an ASTContext 'Context' in scope): 736/// @code 737/// // Default alignment (16) 738/// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments); 739/// // Specific alignment 740/// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments); 741/// @endcode 742/// Please note that you cannot use delete on the pointer; it must be 743/// deallocated using an explicit destructor call followed by 744/// @c Context.Deallocate(Ptr). 745/// 746/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 747/// @param C The ASTContext that provides the allocator. 748/// @param Alignment The alignment of the allocated memory (if the underlying 749/// allocator supports it). 750/// @return The allocated memory. Could be NULL. 751inline void *operator new(size_t Bytes, clang::ASTContext &C, 752 size_t Alignment = 16) throw () { 753 return C.Allocate(Bytes, Alignment); 754} 755/// @brief Placement delete companion to the new above. 756/// 757/// This operator is just a companion to the new above. There is no way of 758/// invoking it directly; see the new operator for more details. This operator 759/// is called implicitly by the compiler if a placement new expression using 760/// the ASTContext throws in the object constructor. 761inline void operator delete(void *Ptr, clang::ASTContext &C, size_t) 762 throw () { 763 C.Deallocate(Ptr); 764} 765 766/// This placement form of operator new[] uses the ASTContext's allocator for 767/// obtaining memory. It is a non-throwing new[], which means that it returns 768/// null on error. 769/// Usage looks like this (assuming there's an ASTContext 'Context' in scope): 770/// @code 771/// // Default alignment (16) 772/// char *data = new (Context) char[10]; 773/// // Specific alignment 774/// char *data = new (Context, 8) char[10]; 775/// @endcode 776/// Please note that you cannot use delete on the pointer; it must be 777/// deallocated using an explicit destructor call followed by 778/// @c Context.Deallocate(Ptr). 779/// 780/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 781/// @param C The ASTContext that provides the allocator. 782/// @param Alignment The alignment of the allocated memory (if the underlying 783/// allocator supports it). 784/// @return The allocated memory. Could be NULL. 785inline void *operator new[](size_t Bytes, clang::ASTContext& C, 786 size_t Alignment = 16) throw () { 787 return C.Allocate(Bytes, Alignment); 788} 789 790/// @brief Placement delete[] companion to the new[] above. 791/// 792/// This operator is just a companion to the new[] above. There is no way of 793/// invoking it directly; see the new[] operator for more details. This operator 794/// is called implicitly by the compiler if a placement new[] expression using 795/// the ASTContext throws in the object constructor. 796inline void operator delete[](void *Ptr, clang::ASTContext &C) throw () { 797 C.Deallocate(Ptr); 798} 799 800#endif 801