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