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