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