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