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