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