ASTContext.h revision 2390a72a3ebd37737fec5ba1385db9c3bb22fc59
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/Basic/OperatorKinds.h" 20#include "clang/Basic/PartialDiagnostic.h" 21#include "clang/AST/Attr.h" 22#include "clang/AST/Decl.h" 23#include "clang/AST/NestedNameSpecifier.h" 24#include "clang/AST/PrettyPrinter.h" 25#include "clang/AST/TemplateName.h" 26#include "clang/AST/Type.h" 27#include "clang/AST/CanonicalType.h" 28#include "clang/AST/UsuallyTinyPtrVector.h" 29#include "llvm/ADT/DenseMap.h" 30#include "llvm/ADT/FoldingSet.h" 31#include "llvm/ADT/OwningPtr.h" 32#include "llvm/ADT/SmallPtrSet.h" 33#include "llvm/Support/Allocator.h" 34#include <vector> 35 36namespace llvm { 37 struct fltSemantics; 38 class raw_ostream; 39} 40 41namespace clang { 42 class FileManager; 43 class ASTRecordLayout; 44 class BlockExpr; 45 class CharUnits; 46 class Diagnostic; 47 class Expr; 48 class ExternalASTSource; 49 class IdentifierTable; 50 class SelectorTable; 51 class SourceManager; 52 class TargetInfo; 53 // Decls 54 class DeclContext; 55 class CXXMethodDecl; 56 class CXXRecordDecl; 57 class Decl; 58 class FieldDecl; 59 class ObjCIvarDecl; 60 class ObjCIvarRefExpr; 61 class ObjCPropertyDecl; 62 class RecordDecl; 63 class StoredDeclsMap; 64 class TagDecl; 65 class TemplateTypeParmDecl; 66 class TranslationUnitDecl; 67 class TypeDecl; 68 class TypedefDecl; 69 class UsingDecl; 70 class UsingShadowDecl; 71 class UnresolvedSetIterator; 72 73 namespace Builtin { class Context; } 74 75/// ASTContext - This class holds long-lived AST nodes (such as types and 76/// decls) that can be referred to throughout the semantic analysis of a file. 77class ASTContext { 78 std::vector<Type*> Types; 79 llvm::FoldingSet<ExtQuals> ExtQualNodes; 80 llvm::FoldingSet<ComplexType> ComplexTypes; 81 llvm::FoldingSet<PointerType> PointerTypes; 82 llvm::FoldingSet<BlockPointerType> BlockPointerTypes; 83 llvm::FoldingSet<LValueReferenceType> LValueReferenceTypes; 84 llvm::FoldingSet<RValueReferenceType> RValueReferenceTypes; 85 llvm::FoldingSet<MemberPointerType> MemberPointerTypes; 86 llvm::FoldingSet<ConstantArrayType> ConstantArrayTypes; 87 llvm::FoldingSet<IncompleteArrayType> IncompleteArrayTypes; 88 std::vector<VariableArrayType*> VariableArrayTypes; 89 llvm::FoldingSet<DependentSizedArrayType> DependentSizedArrayTypes; 90 llvm::FoldingSet<DependentSizedExtVectorType> DependentSizedExtVectorTypes; 91 llvm::FoldingSet<VectorType> VectorTypes; 92 llvm::FoldingSet<FunctionNoProtoType> FunctionNoProtoTypes; 93 llvm::FoldingSet<FunctionProtoType> FunctionProtoTypes; 94 llvm::FoldingSet<DependentTypeOfExprType> DependentTypeOfExprTypes; 95 llvm::FoldingSet<DependentDecltypeType> DependentDecltypeTypes; 96 llvm::FoldingSet<TemplateTypeParmType> TemplateTypeParmTypes; 97 llvm::FoldingSet<SubstTemplateTypeParmType> SubstTemplateTypeParmTypes; 98 llvm::FoldingSet<TemplateSpecializationType> TemplateSpecializationTypes; 99 llvm::FoldingSet<ElaboratedType> ElaboratedTypes; 100 llvm::FoldingSet<DependentNameType> DependentNameTypes; 101 llvm::FoldingSet<ObjCObjectTypeImpl> ObjCObjectTypes; 102 llvm::FoldingSet<ObjCObjectPointerType> ObjCObjectPointerTypes; 103 104 llvm::FoldingSet<QualifiedTemplateName> QualifiedTemplateNames; 105 llvm::FoldingSet<DependentTemplateName> DependentTemplateNames; 106 107 /// \brief The set of nested name specifiers. 108 /// 109 /// This set is managed by the NestedNameSpecifier class. 110 llvm::FoldingSet<NestedNameSpecifier> NestedNameSpecifiers; 111 NestedNameSpecifier *GlobalNestedNameSpecifier; 112 friend class NestedNameSpecifier; 113 114 /// ASTRecordLayouts - A cache mapping from RecordDecls to ASTRecordLayouts. 115 /// This is lazily created. This is intentionally not serialized. 116 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*> ASTRecordLayouts; 117 llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*> ObjCLayouts; 118 119 /// KeyFunctions - A cache mapping from CXXRecordDecls to key functions. 120 llvm::DenseMap<const CXXRecordDecl*, const CXXMethodDecl*> KeyFunctions; 121 122 /// \brief Mapping from ObjCContainers to their ObjCImplementations. 123 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*> ObjCImpls; 124 125 /// BuiltinVaListType - built-in va list type. 126 /// This is initially null and set by Sema::LazilyCreateBuiltin when 127 /// a builtin that takes a valist is encountered. 128 QualType BuiltinVaListType; 129 130 /// ObjCIdType - a pseudo built-in typedef type (set by Sema). 131 QualType ObjCIdTypedefType; 132 133 /// ObjCSelType - another pseudo built-in typedef type (set by Sema). 134 QualType ObjCSelTypedefType; 135 136 /// ObjCProtoType - another pseudo built-in typedef type (set by Sema). 137 QualType ObjCProtoType; 138 const RecordType *ProtoStructType; 139 140 /// ObjCClassType - another pseudo built-in typedef type (set by Sema). 141 QualType ObjCClassTypedefType; 142 143 QualType ObjCConstantStringType; 144 RecordDecl *CFConstantStringTypeDecl; 145 146 RecordDecl *NSConstantStringTypeDecl; 147 148 RecordDecl *ObjCFastEnumerationStateTypeDecl; 149 150 /// \brief The type for the C FILE type. 151 TypeDecl *FILEDecl; 152 153 /// \brief The type for the C jmp_buf type. 154 TypeDecl *jmp_bufDecl; 155 156 /// \brief The type for the C sigjmp_buf type. 157 TypeDecl *sigjmp_bufDecl; 158 159 /// \brief Type for the Block descriptor for Blocks CodeGen. 160 RecordDecl *BlockDescriptorType; 161 162 /// \brief Type for the Block descriptor for Blocks CodeGen. 163 RecordDecl *BlockDescriptorExtendedType; 164 165 /// \brief Keeps track of all declaration attributes. 166 /// 167 /// Since so few decls have attrs, we keep them in a hash map instead of 168 /// wasting space in the Decl class. 169 llvm::DenseMap<const Decl*, Attr*> DeclAttrs; 170 171 /// \brief Keeps track of the static data member templates from which 172 /// static data members of class template specializations were instantiated. 173 /// 174 /// This data structure stores the mapping from instantiations of static 175 /// data members to the static data member representations within the 176 /// class template from which they were instantiated along with the kind 177 /// of instantiation or specialization (a TemplateSpecializationKind - 1). 178 /// 179 /// Given the following example: 180 /// 181 /// \code 182 /// template<typename T> 183 /// struct X { 184 /// static T value; 185 /// }; 186 /// 187 /// template<typename T> 188 /// T X<T>::value = T(17); 189 /// 190 /// int *x = &X<int>::value; 191 /// \endcode 192 /// 193 /// This mapping will contain an entry that maps from the VarDecl for 194 /// X<int>::value to the corresponding VarDecl for X<T>::value (within the 195 /// class template X) and will be marked TSK_ImplicitInstantiation. 196 llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *> 197 InstantiatedFromStaticDataMember; 198 199 /// \brief Keeps track of the declaration from which a UsingDecl was 200 /// created during instantiation. The source declaration is always 201 /// a UsingDecl, an UnresolvedUsingValueDecl, or an 202 /// UnresolvedUsingTypenameDecl. 203 /// 204 /// For example: 205 /// \code 206 /// template<typename T> 207 /// struct A { 208 /// void f(); 209 /// }; 210 /// 211 /// template<typename T> 212 /// struct B : A<T> { 213 /// using A<T>::f; 214 /// }; 215 /// 216 /// template struct B<int>; 217 /// \endcode 218 /// 219 /// This mapping will contain an entry that maps from the UsingDecl in 220 /// B<int> to the UnresolvedUsingDecl in B<T>. 221 llvm::DenseMap<UsingDecl *, NamedDecl *> InstantiatedFromUsingDecl; 222 223 llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*> 224 InstantiatedFromUsingShadowDecl; 225 226 llvm::DenseMap<FieldDecl *, FieldDecl *> InstantiatedFromUnnamedFieldDecl; 227 228 /// \brief Mapping that stores the methods overridden by a given C++ 229 /// member function. 230 /// 231 /// Since most C++ member functions aren't virtual and therefore 232 /// don't override anything, we store the overridden functions in 233 /// this map on the side rather than within the CXXMethodDecl structure. 234 typedef UsuallyTinyPtrVector<const CXXMethodDecl> CXXMethodVector; 235 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector> OverriddenMethods; 236 237 TranslationUnitDecl *TUDecl; 238 239 /// SourceMgr - The associated SourceManager object. 240 SourceManager &SourceMgr; 241 242 /// LangOpts - The language options used to create the AST associated with 243 /// this ASTContext object. 244 LangOptions LangOpts; 245 246 /// MallocAlloc/BumpAlloc - The allocator objects used to create AST objects. 247 bool FreeMemory; 248 llvm::MallocAllocator MallocAlloc; 249 llvm::BumpPtrAllocator BumpAlloc; 250 251 /// \brief Allocator for partial diagnostics. 252 PartialDiagnostic::StorageAllocator DiagAllocator; 253 254public: 255 const TargetInfo &Target; 256 IdentifierTable &Idents; 257 SelectorTable &Selectors; 258 Builtin::Context &BuiltinInfo; 259 DeclarationNameTable DeclarationNames; 260 llvm::OwningPtr<ExternalASTSource> ExternalSource; 261 clang::PrintingPolicy PrintingPolicy; 262 263 // Typedefs which may be provided defining the structure of Objective-C 264 // pseudo-builtins 265 QualType ObjCIdRedefinitionType; 266 QualType ObjCClassRedefinitionType; 267 QualType ObjCSelRedefinitionType; 268 269 SourceManager& getSourceManager() { return SourceMgr; } 270 const SourceManager& getSourceManager() const { return SourceMgr; } 271 void *Allocate(unsigned Size, unsigned Align = 8) { 272 return FreeMemory ? MallocAlloc.Allocate(Size, Align) : 273 BumpAlloc.Allocate(Size, Align); 274 } 275 void Deallocate(void *Ptr) { 276 if (FreeMemory) 277 MallocAlloc.Deallocate(Ptr); 278 } 279 280 PartialDiagnostic::StorageAllocator &getDiagAllocator() { 281 return DiagAllocator; 282 } 283 284 const LangOptions& getLangOptions() const { return LangOpts; } 285 286 FullSourceLoc getFullLoc(SourceLocation Loc) const { 287 return FullSourceLoc(Loc,SourceMgr); 288 } 289 290 /// \brief Retrieve the attributes for the given declaration. 291 Attr*& getDeclAttrs(const Decl *D) { return DeclAttrs[D]; } 292 293 /// \brief Erase the attributes corresponding to the given declaration. 294 void eraseDeclAttrs(const Decl *D) { DeclAttrs.erase(D); } 295 296 /// \brief If this variable is an instantiated static data member of a 297 /// class template specialization, returns the templated static data member 298 /// from which it was instantiated. 299 MemberSpecializationInfo *getInstantiatedFromStaticDataMember( 300 const VarDecl *Var); 301 302 /// \brief Note that the static data member \p Inst is an instantiation of 303 /// the static data member template \p Tmpl of a class template. 304 void setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, 305 TemplateSpecializationKind TSK); 306 307 /// \brief If the given using decl is an instantiation of a 308 /// (possibly unresolved) using decl from a template instantiation, 309 /// return it. 310 NamedDecl *getInstantiatedFromUsingDecl(UsingDecl *Inst); 311 312 /// \brief Remember that the using decl \p Inst is an instantiation 313 /// of the using decl \p Pattern of a class template. 314 void setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern); 315 316 void setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, 317 UsingShadowDecl *Pattern); 318 UsingShadowDecl *getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst); 319 320 FieldDecl *getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field); 321 322 void setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, FieldDecl *Tmpl); 323 324 // Access to the set of methods overridden by the given C++ method. 325 typedef CXXMethodVector::iterator overridden_cxx_method_iterator; 326 overridden_cxx_method_iterator 327 overridden_methods_begin(const CXXMethodDecl *Method) const; 328 329 overridden_cxx_method_iterator 330 overridden_methods_end(const CXXMethodDecl *Method) const; 331 332 /// \brief Note that the given C++ \p Method overrides the given \p 333 /// Overridden method. 334 void addOverriddenMethod(const CXXMethodDecl *Method, 335 const CXXMethodDecl *Overridden); 336 337 TranslationUnitDecl *getTranslationUnitDecl() const { return TUDecl; } 338 339 340 // Builtin Types. 341 CanQualType VoidTy; 342 CanQualType BoolTy; 343 CanQualType CharTy; 344 CanQualType WCharTy; // [C++ 3.9.1p5], integer type in C99. 345 CanQualType Char16Ty; // [C++0x 3.9.1p5], integer type in C99. 346 CanQualType Char32Ty; // [C++0x 3.9.1p5], integer type in C99. 347 CanQualType SignedCharTy, ShortTy, IntTy, LongTy, LongLongTy, Int128Ty; 348 CanQualType UnsignedCharTy, UnsignedShortTy, UnsignedIntTy, UnsignedLongTy; 349 CanQualType UnsignedLongLongTy, UnsignedInt128Ty; 350 CanQualType FloatTy, DoubleTy, LongDoubleTy; 351 CanQualType FloatComplexTy, DoubleComplexTy, LongDoubleComplexTy; 352 CanQualType VoidPtrTy, NullPtrTy; 353 CanQualType OverloadTy; 354 CanQualType DependentTy; 355 CanQualType UndeducedAutoTy; 356 CanQualType ObjCBuiltinIdTy, ObjCBuiltinClassTy, ObjCBuiltinSelTy; 357 358 ASTContext(const LangOptions& LOpts, SourceManager &SM, const TargetInfo &t, 359 IdentifierTable &idents, SelectorTable &sels, 360 Builtin::Context &builtins, 361 bool FreeMemory = true, unsigned size_reserve=0); 362 363 ~ASTContext(); 364 365 /// \brief Attach an external AST source to the AST context. 366 /// 367 /// The external AST source provides the ability to load parts of 368 /// the abstract syntax tree as needed from some external storage, 369 /// e.g., a precompiled header. 370 void setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source); 371 372 /// \brief Retrieve a pointer to the external AST source associated 373 /// with this AST context, if any. 374 ExternalASTSource *getExternalSource() const { return ExternalSource.get(); } 375 376 void PrintStats() const; 377 const std::vector<Type*>& getTypes() const { return Types; } 378 379 //===--------------------------------------------------------------------===// 380 // Type Constructors 381 //===--------------------------------------------------------------------===// 382 383private: 384 /// getExtQualType - Return a type with extended qualifiers. 385 QualType getExtQualType(const Type *Base, Qualifiers Quals); 386 387 QualType getTypeDeclTypeSlow(const TypeDecl *Decl); 388 389public: 390 /// getAddSpaceQualType - Return the uniqued reference to the type for an 391 /// address space qualified type with the specified type and address space. 392 /// The resulting type has a union of the qualifiers from T and the address 393 /// space. If T already has an address space specifier, it is silently 394 /// replaced. 395 QualType getAddrSpaceQualType(QualType T, unsigned AddressSpace); 396 397 /// getObjCGCQualType - Returns the uniqued reference to the type for an 398 /// objc gc qualified type. The retulting type has a union of the qualifiers 399 /// from T and the gc attribute. 400 QualType getObjCGCQualType(QualType T, Qualifiers::GC gcAttr); 401 402 /// getRestrictType - Returns the uniqued reference to the type for a 403 /// 'restrict' qualified type. The resulting type has a union of the 404 /// qualifiers from T and 'restrict'. 405 QualType getRestrictType(QualType T) { 406 return T.withFastQualifiers(Qualifiers::Restrict); 407 } 408 409 /// getVolatileType - Returns the uniqued reference to the type for a 410 /// 'volatile' qualified type. The resulting type has a union of the 411 /// qualifiers from T and 'volatile'. 412 QualType getVolatileType(QualType T); 413 414 /// getConstType - Returns the uniqued reference to the type for a 415 /// 'const' qualified type. The resulting type has a union of the 416 /// qualifiers from T and 'const'. 417 /// 418 /// It can be reasonably expected that this will always be 419 /// equivalent to calling T.withConst(). 420 QualType getConstType(QualType T) { return T.withConst(); } 421 422 /// getNoReturnType - Add or remove the noreturn attribute to the given type 423 /// which must be a FunctionType or a pointer to an allowable type or a 424 /// BlockPointer. 425 QualType getNoReturnType(QualType T, bool AddNoReturn = true); 426 427 /// getCallConvType - Adds the specified calling convention attribute to 428 /// the given type, which must be a FunctionType or a pointer to an 429 /// allowable type. 430 QualType getCallConvType(QualType T, CallingConv CallConv); 431 432 /// getRegParmType - Sets the specified regparm attribute to 433 /// the given type, which must be a FunctionType or a pointer to an 434 /// allowable type. 435 QualType getRegParmType(QualType T, unsigned RegParm); 436 437 /// getComplexType - Return the uniqued reference to the type for a complex 438 /// number with the specified element type. 439 QualType getComplexType(QualType T); 440 CanQualType getComplexType(CanQualType T) { 441 return CanQualType::CreateUnsafe(getComplexType((QualType) T)); 442 } 443 444 /// getPointerType - Return the uniqued reference to the type for a pointer to 445 /// the specified type. 446 QualType getPointerType(QualType T); 447 CanQualType getPointerType(CanQualType T) { 448 return CanQualType::CreateUnsafe(getPointerType((QualType) T)); 449 } 450 451 /// getBlockPointerType - Return the uniqued reference to the type for a block 452 /// of the specified type. 453 QualType getBlockPointerType(QualType T); 454 455 /// This gets the struct used to keep track of the descriptor for pointer to 456 /// blocks. 457 QualType getBlockDescriptorType(); 458 459 // Set the type for a Block descriptor type. 460 void setBlockDescriptorType(QualType T); 461 /// Get the BlockDescriptorType type, or NULL if it hasn't yet been built. 462 QualType getRawBlockdescriptorType() { 463 if (BlockDescriptorType) 464 return getTagDeclType(BlockDescriptorType); 465 return QualType(); 466 } 467 468 /// This gets the struct used to keep track of the extended descriptor for 469 /// pointer to blocks. 470 QualType getBlockDescriptorExtendedType(); 471 472 // Set the type for a Block descriptor extended type. 473 void setBlockDescriptorExtendedType(QualType T); 474 /// Get the BlockDescriptorExtendedType type, or NULL if it hasn't yet been 475 /// built. 476 QualType getRawBlockdescriptorExtendedType() { 477 if (BlockDescriptorExtendedType) 478 return getTagDeclType(BlockDescriptorExtendedType); 479 return QualType(); 480 } 481 482 /// This gets the struct used to keep track of pointer to blocks, complete 483 /// with captured variables. 484 QualType getBlockParmType(bool BlockHasCopyDispose, 485 llvm::SmallVector<const Expr *, 8> &BDRDs); 486 487 /// This builds the struct used for __block variables. 488 QualType BuildByRefType(const char *DeclName, QualType Ty); 489 490 /// Returns true iff we need copy/dispose helpers for the given type. 491 bool BlockRequiresCopying(QualType Ty); 492 493 /// getLValueReferenceType - Return the uniqued reference to the type for an 494 /// lvalue reference to the specified type. 495 QualType getLValueReferenceType(QualType T, bool SpelledAsLValue = true); 496 497 /// getRValueReferenceType - Return the uniqued reference to the type for an 498 /// rvalue reference to the specified type. 499 QualType getRValueReferenceType(QualType T); 500 501 /// getMemberPointerType - Return the uniqued reference to the type for a 502 /// member pointer to the specified type in the specified class. The class 503 /// is a Type because it could be a dependent name. 504 QualType getMemberPointerType(QualType T, const Type *Cls); 505 506 /// getVariableArrayType - Returns a non-unique reference to the type for a 507 /// variable array of the specified element type. 508 QualType getVariableArrayType(QualType EltTy, Expr *NumElts, 509 ArrayType::ArraySizeModifier ASM, 510 unsigned EltTypeQuals, 511 SourceRange Brackets); 512 513 /// getDependentSizedArrayType - Returns a non-unique reference to 514 /// the type for a dependently-sized array of the specified element 515 /// type. FIXME: We will need these to be uniqued, or at least 516 /// comparable, at some point. 517 QualType getDependentSizedArrayType(QualType EltTy, Expr *NumElts, 518 ArrayType::ArraySizeModifier ASM, 519 unsigned EltTypeQuals, 520 SourceRange Brackets); 521 522 /// getIncompleteArrayType - Returns a unique reference to the type for a 523 /// incomplete array of the specified element type. 524 QualType getIncompleteArrayType(QualType EltTy, 525 ArrayType::ArraySizeModifier ASM, 526 unsigned EltTypeQuals); 527 528 /// getConstantArrayType - Return the unique reference to the type for a 529 /// constant array of the specified element type. 530 QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize, 531 ArrayType::ArraySizeModifier ASM, 532 unsigned EltTypeQuals); 533 534 /// getVectorType - Return the unique reference to a vector type of 535 /// the specified element type and size. VectorType must be a built-in type. 536 QualType getVectorType(QualType VectorType, unsigned NumElts, 537 bool AltiVec, bool IsPixel); 538 539 /// getExtVectorType - Return the unique reference to an extended vector type 540 /// of the specified element type and size. VectorType must be a built-in 541 /// type. 542 QualType getExtVectorType(QualType VectorType, unsigned NumElts); 543 544 /// getDependentSizedExtVectorType - Returns a non-unique reference to 545 /// the type for a dependently-sized vector of the specified element 546 /// type. FIXME: We will need these to be uniqued, or at least 547 /// comparable, at some point. 548 QualType getDependentSizedExtVectorType(QualType VectorType, 549 Expr *SizeExpr, 550 SourceLocation AttrLoc); 551 552 /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. 553 /// 554 QualType getFunctionNoProtoType(QualType ResultTy, 555 const FunctionType::ExtInfo &Info); 556 557 QualType getFunctionNoProtoType(QualType ResultTy) { 558 return getFunctionNoProtoType(ResultTy, FunctionType::ExtInfo()); 559 } 560 561 /// getFunctionType - Return a normal function type with a typed argument 562 /// list. isVariadic indicates whether the argument list includes '...'. 563 QualType getFunctionType(QualType ResultTy, const QualType *ArgArray, 564 unsigned NumArgs, bool isVariadic, 565 unsigned TypeQuals, bool hasExceptionSpec, 566 bool hasAnyExceptionSpec, 567 unsigned NumExs, const QualType *ExArray, 568 const FunctionType::ExtInfo &Info); 569 570 /// getTypeDeclType - Return the unique reference to the type for 571 /// the specified type declaration. 572 QualType getTypeDeclType(const TypeDecl *Decl, 573 const TypeDecl *PrevDecl = 0) { 574 assert(Decl && "Passed null for Decl param"); 575 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); 576 577 if (PrevDecl) { 578 assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl"); 579 Decl->TypeForDecl = PrevDecl->TypeForDecl; 580 return QualType(PrevDecl->TypeForDecl, 0); 581 } 582 583 return getTypeDeclTypeSlow(Decl); 584 } 585 586 /// getTypedefType - Return the unique reference to the type for the 587 /// specified typename decl. 588 QualType getTypedefType(const TypedefDecl *Decl); 589 590 QualType getInjectedClassNameType(CXXRecordDecl *Decl, QualType TST); 591 592 QualType getSubstTemplateTypeParmType(const TemplateTypeParmType *Replaced, 593 QualType Replacement); 594 595 QualType getTemplateTypeParmType(unsigned Depth, unsigned Index, 596 bool ParameterPack, 597 IdentifierInfo *Name = 0); 598 599 QualType getTemplateSpecializationType(TemplateName T, 600 const TemplateArgument *Args, 601 unsigned NumArgs, 602 QualType Canon = QualType(), 603 bool IsCurrentInstantiation = false); 604 605 QualType getTemplateSpecializationType(TemplateName T, 606 const TemplateArgumentListInfo &Args, 607 QualType Canon = QualType(), 608 bool IsCurrentInstantiation = false); 609 610 TypeSourceInfo * 611 getTemplateSpecializationTypeInfo(TemplateName T, SourceLocation TLoc, 612 const TemplateArgumentListInfo &Args, 613 QualType Canon = QualType()); 614 615 QualType getElaboratedType(ElaboratedTypeKeyword Keyword, 616 NestedNameSpecifier *NNS, 617 QualType NamedType); 618 QualType getDependentNameType(ElaboratedTypeKeyword Keyword, 619 NestedNameSpecifier *NNS, 620 const IdentifierInfo *Name, 621 QualType Canon = QualType()); 622 QualType getDependentNameType(ElaboratedTypeKeyword Keyword, 623 NestedNameSpecifier *NNS, 624 const TemplateSpecializationType *TemplateId, 625 QualType Canon = QualType()); 626 627 QualType getObjCInterfaceType(const ObjCInterfaceDecl *Decl); 628 629 QualType getObjCObjectType(QualType Base, 630 ObjCProtocolDecl * const *Protocols, 631 unsigned NumProtocols); 632 633 /// getObjCObjectPointerType - Return a ObjCObjectPointerType type 634 /// for the given ObjCObjectType. 635 QualType getObjCObjectPointerType(QualType OIT); 636 637 /// getTypeOfType - GCC extension. 638 QualType getTypeOfExprType(Expr *e); 639 QualType getTypeOfType(QualType t); 640 641 /// getDecltypeType - C++0x decltype. 642 QualType getDecltypeType(Expr *e); 643 644 /// getTagDeclType - Return the unique reference to the type for the 645 /// specified TagDecl (struct/union/class/enum) decl. 646 QualType getTagDeclType(const TagDecl *Decl); 647 648 /// getSizeType - Return the unique type for "size_t" (C99 7.17), defined 649 /// in <stddef.h>. The sizeof operator requires this (C99 6.5.3.4p4). 650 CanQualType getSizeType() const; 651 652 /// getWCharType - In C++, this returns the unique wchar_t type. In C99, this 653 /// returns a type compatible with the type defined in <stddef.h> as defined 654 /// by the target. 655 QualType getWCharType() const { return WCharTy; } 656 657 /// getSignedWCharType - Return the type of "signed wchar_t". 658 /// Used when in C++, as a GCC extension. 659 QualType getSignedWCharType() const; 660 661 /// getUnsignedWCharType - Return the type of "unsigned wchar_t". 662 /// Used when in C++, as a GCC extension. 663 QualType getUnsignedWCharType() const; 664 665 /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) 666 /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). 667 QualType getPointerDiffType() const; 668 669 // getCFConstantStringType - Return the C structure type used to represent 670 // constant CFStrings. 671 QualType getCFConstantStringType(); 672 673 // getNSConstantStringType - Return the C structure type used to represent 674 // constant NSStrings. 675 QualType getNSConstantStringType(); 676 /// Get the structure type used to representation NSStrings, or NULL 677 /// if it hasn't yet been built. 678 QualType getRawNSConstantStringType() { 679 if (NSConstantStringTypeDecl) 680 return getTagDeclType(NSConstantStringTypeDecl); 681 return QualType(); 682 } 683 void setNSConstantStringType(QualType T); 684 685 686 /// Get the structure type used to representation CFStrings, or NULL 687 /// if it hasn't yet been built. 688 QualType getRawCFConstantStringType() { 689 if (CFConstantStringTypeDecl) 690 return getTagDeclType(CFConstantStringTypeDecl); 691 return QualType(); 692 } 693 void setCFConstantStringType(QualType T); 694 695 // This setter/getter represents the ObjC type for an NSConstantString. 696 void setObjCConstantStringInterface(ObjCInterfaceDecl *Decl); 697 QualType getObjCConstantStringInterface() const { 698 return ObjCConstantStringType; 699 } 700 701 //// This gets the struct used to keep track of fast enumerations. 702 QualType getObjCFastEnumerationStateType(); 703 704 /// Get the ObjCFastEnumerationState type, or NULL if it hasn't yet 705 /// been built. 706 QualType getRawObjCFastEnumerationStateType() { 707 if (ObjCFastEnumerationStateTypeDecl) 708 return getTagDeclType(ObjCFastEnumerationStateTypeDecl); 709 return QualType(); 710 } 711 712 void setObjCFastEnumerationStateType(QualType T); 713 714 /// \brief Set the type for the C FILE type. 715 void setFILEDecl(TypeDecl *FILEDecl) { this->FILEDecl = FILEDecl; } 716 717 /// \brief Retrieve the C FILE type. 718 QualType getFILEType() { 719 if (FILEDecl) 720 return getTypeDeclType(FILEDecl); 721 return QualType(); 722 } 723 724 /// \brief Set the type for the C jmp_buf type. 725 void setjmp_bufDecl(TypeDecl *jmp_bufDecl) { 726 this->jmp_bufDecl = jmp_bufDecl; 727 } 728 729 /// \brief Retrieve the C jmp_buf type. 730 QualType getjmp_bufType() { 731 if (jmp_bufDecl) 732 return getTypeDeclType(jmp_bufDecl); 733 return QualType(); 734 } 735 736 /// \brief Set the type for the C sigjmp_buf type. 737 void setsigjmp_bufDecl(TypeDecl *sigjmp_bufDecl) { 738 this->sigjmp_bufDecl = sigjmp_bufDecl; 739 } 740 741 /// \brief Retrieve the C sigjmp_buf type. 742 QualType getsigjmp_bufType() { 743 if (sigjmp_bufDecl) 744 return getTypeDeclType(sigjmp_bufDecl); 745 return QualType(); 746 } 747 748 /// getObjCEncodingForType - Emit the ObjC type encoding for the 749 /// given type into \arg S. If \arg NameFields is specified then 750 /// record field names are also encoded. 751 void getObjCEncodingForType(QualType t, std::string &S, 752 const FieldDecl *Field=0); 753 754 void getLegacyIntegralTypeEncoding(QualType &t) const; 755 756 // Put the string version of type qualifiers into S. 757 void getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, 758 std::string &S) const; 759 760 /// getObjCEncodingForMethodDecl - Return the encoded type for this method 761 /// declaration. 762 void getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, std::string &S); 763 764 /// getObjCEncodingForBlockDecl - Return the encoded type for this block 765 /// declaration. 766 void getObjCEncodingForBlock(const BlockExpr *Expr, std::string& S); 767 768 /// getObjCEncodingForPropertyDecl - Return the encoded type for 769 /// this method declaration. If non-NULL, Container must be either 770 /// an ObjCCategoryImplDecl or ObjCImplementationDecl; it should 771 /// only be NULL when getting encodings for protocol properties. 772 void getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, 773 const Decl *Container, 774 std::string &S); 775 776 bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, 777 ObjCProtocolDecl *rProto); 778 779 /// getObjCEncodingTypeSize returns size of type for objective-c encoding 780 /// purpose in characters. 781 CharUnits getObjCEncodingTypeSize(QualType t); 782 783 /// This setter/getter represents the ObjC 'id' type. It is setup lazily, by 784 /// Sema. id is always a (typedef for a) pointer type, a pointer to a struct. 785 QualType getObjCIdType() const { return ObjCIdTypedefType; } 786 void setObjCIdType(QualType T); 787 788 void setObjCSelType(QualType T); 789 QualType getObjCSelType() const { return ObjCSelTypedefType; } 790 791 void setObjCProtoType(QualType QT); 792 QualType getObjCProtoType() const { return ObjCProtoType; } 793 794 /// This setter/getter repreents the ObjC 'Class' type. It is setup lazily, by 795 /// Sema. 'Class' is always a (typedef for a) pointer type, a pointer to a 796 /// struct. 797 QualType getObjCClassType() const { return ObjCClassTypedefType; } 798 void setObjCClassType(QualType T); 799 800 void setBuiltinVaListType(QualType T); 801 QualType getBuiltinVaListType() const { return BuiltinVaListType; } 802 803 /// getCVRQualifiedType - Returns a type with additional const, 804 /// volatile, or restrict qualifiers. 805 QualType getCVRQualifiedType(QualType T, unsigned CVR) { 806 return getQualifiedType(T, Qualifiers::fromCVRMask(CVR)); 807 } 808 809 /// getQualifiedType - Returns a type with additional qualifiers. 810 QualType getQualifiedType(QualType T, Qualifiers Qs) { 811 if (!Qs.hasNonFastQualifiers()) 812 return T.withFastQualifiers(Qs.getFastQualifiers()); 813 QualifierCollector Qc(Qs); 814 const Type *Ptr = Qc.strip(T); 815 return getExtQualType(Ptr, Qc); 816 } 817 818 /// getQualifiedType - Returns a type with additional qualifiers. 819 QualType getQualifiedType(const Type *T, Qualifiers Qs) { 820 if (!Qs.hasNonFastQualifiers()) 821 return QualType(T, Qs.getFastQualifiers()); 822 return getExtQualType(T, Qs); 823 } 824 825 DeclarationName getNameForTemplate(TemplateName Name); 826 827 TemplateName getOverloadedTemplateName(UnresolvedSetIterator Begin, 828 UnresolvedSetIterator End); 829 830 TemplateName getQualifiedTemplateName(NestedNameSpecifier *NNS, 831 bool TemplateKeyword, 832 TemplateDecl *Template); 833 834 TemplateName getDependentTemplateName(NestedNameSpecifier *NNS, 835 const IdentifierInfo *Name); 836 TemplateName getDependentTemplateName(NestedNameSpecifier *NNS, 837 OverloadedOperatorKind Operator); 838 839 enum GetBuiltinTypeError { 840 GE_None, //< No error 841 GE_Missing_stdio, //< Missing a type from <stdio.h> 842 GE_Missing_setjmp //< Missing a type from <setjmp.h> 843 }; 844 845 /// GetBuiltinType - Return the type for the specified builtin. 846 QualType GetBuiltinType(unsigned ID, GetBuiltinTypeError &Error); 847 848private: 849 CanQualType getFromTargetType(unsigned Type) const; 850 851 //===--------------------------------------------------------------------===// 852 // Type Predicates. 853 //===--------------------------------------------------------------------===// 854 855public: 856 /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's 857 /// garbage collection attribute. 858 /// 859 Qualifiers::GC getObjCGCAttrKind(const QualType &Ty) const; 860 861 /// isObjCNSObjectType - Return true if this is an NSObject object with 862 /// its NSObject attribute set. 863 bool isObjCNSObjectType(QualType Ty) const; 864 865 //===--------------------------------------------------------------------===// 866 // Type Sizing and Analysis 867 //===--------------------------------------------------------------------===// 868 869 /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified 870 /// scalar floating point type. 871 const llvm::fltSemantics &getFloatTypeSemantics(QualType T) const; 872 873 /// getTypeInfo - Get the size and alignment of the specified complete type in 874 /// bits. 875 std::pair<uint64_t, unsigned> getTypeInfo(const Type *T); 876 std::pair<uint64_t, unsigned> getTypeInfo(QualType T) { 877 return getTypeInfo(T.getTypePtr()); 878 } 879 880 /// getTypeSize - Return the size of the specified type, in bits. This method 881 /// does not work on incomplete types. 882 uint64_t getTypeSize(QualType T) { 883 return getTypeInfo(T).first; 884 } 885 uint64_t getTypeSize(const Type *T) { 886 return getTypeInfo(T).first; 887 } 888 889 /// getCharWidth - Return the size of the character type, in bits 890 uint64_t getCharWidth() { 891 return getTypeSize(CharTy); 892 } 893 894 /// getTypeSizeInChars - Return the size of the specified type, in characters. 895 /// This method does not work on incomplete types. 896 CharUnits getTypeSizeInChars(QualType T); 897 CharUnits getTypeSizeInChars(const Type *T); 898 899 /// getTypeAlign - Return the ABI-specified alignment of a type, in bits. 900 /// This method does not work on incomplete types. 901 unsigned getTypeAlign(QualType T) { 902 return getTypeInfo(T).second; 903 } 904 unsigned getTypeAlign(const Type *T) { 905 return getTypeInfo(T).second; 906 } 907 908 /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in 909 /// characters. This method does not work on incomplete types. 910 CharUnits getTypeAlignInChars(QualType T); 911 CharUnits getTypeAlignInChars(const Type *T); 912 913 /// getPreferredTypeAlign - Return the "preferred" alignment of the specified 914 /// type for the current target in bits. This can be different than the ABI 915 /// alignment in cases where it is beneficial for performance to overalign 916 /// a data type. 917 unsigned getPreferredTypeAlign(const Type *T); 918 919 /// getDeclAlign - Return a conservative estimate of the alignment of 920 /// the specified decl. Note that bitfields do not have a valid alignment, so 921 /// this method will assert on them. 922 /// If @p RefAsPointee, references are treated like their underlying type 923 /// (for alignof), else they're treated like pointers (for CodeGen). 924 CharUnits getDeclAlign(const Decl *D, bool RefAsPointee = false); 925 926 /// getASTRecordLayout - Get or compute information about the layout of the 927 /// specified record (struct/union/class), which indicates its size and field 928 /// position information. 929 const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D); 930 931 /// getASTObjCInterfaceLayout - Get or compute information about the 932 /// layout of the specified Objective-C interface. 933 const ASTRecordLayout &getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D); 934 935 void DumpRecordLayout(const RecordDecl *RD, llvm::raw_ostream &OS); 936 937 /// getASTObjCImplementationLayout - Get or compute information about 938 /// the layout of the specified Objective-C implementation. This may 939 /// differ from the interface if synthesized ivars are present. 940 const ASTRecordLayout & 941 getASTObjCImplementationLayout(const ObjCImplementationDecl *D); 942 943 /// getKeyFunction - Get the key function for the given record decl. 944 /// The key function is, according to the Itanium C++ ABI section 5.2.3: 945 /// 946 /// ...the first non-pure virtual function that is not inline at the point 947 /// of class definition. 948 const CXXMethodDecl *getKeyFunction(const CXXRecordDecl *RD); 949 950 void CollectObjCIvars(const ObjCInterfaceDecl *OI, 951 llvm::SmallVectorImpl<FieldDecl*> &Fields); 952 953 void ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI, 954 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars); 955 void CollectNonClassIvars(const ObjCInterfaceDecl *OI, 956 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars); 957 unsigned CountNonClassIvars(const ObjCInterfaceDecl *OI); 958 void CollectInheritedProtocols(const Decl *CDecl, 959 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols); 960 961 //===--------------------------------------------------------------------===// 962 // Type Operators 963 //===--------------------------------------------------------------------===// 964 965 /// getCanonicalType - Return the canonical (structural) type corresponding to 966 /// the specified potentially non-canonical type. The non-canonical version 967 /// of a type may have many "decorated" versions of types. Decorators can 968 /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed 969 /// to be free of any of these, allowing two canonical types to be compared 970 /// for exact equality with a simple pointer comparison. 971 CanQualType getCanonicalType(QualType T); 972 const Type *getCanonicalType(const Type *T) { 973 return T->getCanonicalTypeInternal().getTypePtr(); 974 } 975 976 /// getCanonicalParamType - Return the canonical parameter type 977 /// corresponding to the specific potentially non-canonical one. 978 /// Qualifiers are stripped off, functions are turned into function 979 /// pointers, and arrays decay one level into pointers. 980 CanQualType getCanonicalParamType(QualType T); 981 982 /// \brief Determine whether the given types are equivalent. 983 bool hasSameType(QualType T1, QualType T2) { 984 return getCanonicalType(T1) == getCanonicalType(T2); 985 } 986 987 /// \brief Returns this type as a completely-unqualified array type, 988 /// capturing the qualifiers in Quals. This will remove the minimal amount of 989 /// sugaring from the types, similar to the behavior of 990 /// QualType::getUnqualifiedType(). 991 /// 992 /// \param T is the qualified type, which may be an ArrayType 993 /// 994 /// \param Quals will receive the full set of qualifiers that were 995 /// applied to the array. 996 /// 997 /// \returns if this is an array type, the completely unqualified array type 998 /// that corresponds to it. Otherwise, returns T.getUnqualifiedType(). 999 QualType getUnqualifiedArrayType(QualType T, Qualifiers &Quals); 1000 1001 /// \brief Determine whether the given types are equivalent after 1002 /// cvr-qualifiers have been removed. 1003 bool hasSameUnqualifiedType(QualType T1, QualType T2) { 1004 CanQualType CT1 = getCanonicalType(T1); 1005 CanQualType CT2 = getCanonicalType(T2); 1006 1007 Qualifiers Quals; 1008 QualType UnqualT1 = getUnqualifiedArrayType(CT1, Quals); 1009 QualType UnqualT2 = getUnqualifiedArrayType(CT2, Quals); 1010 return UnqualT1 == UnqualT2; 1011 } 1012 1013 /// \brief Retrieves the "canonical" declaration of 1014 1015 /// \brief Retrieves the "canonical" nested name specifier for a 1016 /// given nested name specifier. 1017 /// 1018 /// The canonical nested name specifier is a nested name specifier 1019 /// that uniquely identifies a type or namespace within the type 1020 /// system. For example, given: 1021 /// 1022 /// \code 1023 /// namespace N { 1024 /// struct S { 1025 /// template<typename T> struct X { typename T* type; }; 1026 /// }; 1027 /// } 1028 /// 1029 /// template<typename T> struct Y { 1030 /// typename N::S::X<T>::type member; 1031 /// }; 1032 /// \endcode 1033 /// 1034 /// Here, the nested-name-specifier for N::S::X<T>:: will be 1035 /// S::X<template-param-0-0>, since 'S' and 'X' are uniquely defined 1036 /// by declarations in the type system and the canonical type for 1037 /// the template type parameter 'T' is template-param-0-0. 1038 NestedNameSpecifier * 1039 getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS); 1040 1041 /// \brief Retrieves the canonical representation of the given 1042 /// calling convention. 1043 CallingConv getCanonicalCallConv(CallingConv CC) { 1044 if (CC == CC_C) 1045 return CC_Default; 1046 return CC; 1047 } 1048 1049 /// \brief Determines whether two calling conventions name the same 1050 /// calling convention. 1051 bool isSameCallConv(CallingConv lcc, CallingConv rcc) { 1052 return (getCanonicalCallConv(lcc) == getCanonicalCallConv(rcc)); 1053 } 1054 1055 /// \brief Retrieves the "canonical" template name that refers to a 1056 /// given template. 1057 /// 1058 /// The canonical template name is the simplest expression that can 1059 /// be used to refer to a given template. For most templates, this 1060 /// expression is just the template declaration itself. For example, 1061 /// the template std::vector can be referred to via a variety of 1062 /// names---std::vector, ::std::vector, vector (if vector is in 1063 /// scope), etc.---but all of these names map down to the same 1064 /// TemplateDecl, which is used to form the canonical template name. 1065 /// 1066 /// Dependent template names are more interesting. Here, the 1067 /// template name could be something like T::template apply or 1068 /// std::allocator<T>::template rebind, where the nested name 1069 /// specifier itself is dependent. In this case, the canonical 1070 /// template name uses the shortest form of the dependent 1071 /// nested-name-specifier, which itself contains all canonical 1072 /// types, values, and templates. 1073 TemplateName getCanonicalTemplateName(TemplateName Name); 1074 1075 /// \brief Determine whether the given template names refer to the same 1076 /// template. 1077 bool hasSameTemplateName(TemplateName X, TemplateName Y); 1078 1079 /// \brief Retrieve the "canonical" template argument. 1080 /// 1081 /// The canonical template argument is the simplest template argument 1082 /// (which may be a type, value, expression, or declaration) that 1083 /// expresses the value of the argument. 1084 TemplateArgument getCanonicalTemplateArgument(const TemplateArgument &Arg); 1085 1086 /// Type Query functions. If the type is an instance of the specified class, 1087 /// return the Type pointer for the underlying maximally pretty type. This 1088 /// is a member of ASTContext because this may need to do some amount of 1089 /// canonicalization, e.g. to move type qualifiers into the element type. 1090 const ArrayType *getAsArrayType(QualType T); 1091 const ConstantArrayType *getAsConstantArrayType(QualType T) { 1092 return dyn_cast_or_null<ConstantArrayType>(getAsArrayType(T)); 1093 } 1094 const VariableArrayType *getAsVariableArrayType(QualType T) { 1095 return dyn_cast_or_null<VariableArrayType>(getAsArrayType(T)); 1096 } 1097 const IncompleteArrayType *getAsIncompleteArrayType(QualType T) { 1098 return dyn_cast_or_null<IncompleteArrayType>(getAsArrayType(T)); 1099 } 1100 const DependentSizedArrayType *getAsDependentSizedArrayType(QualType T) { 1101 return dyn_cast_or_null<DependentSizedArrayType>(getAsArrayType(T)); 1102 } 1103 1104 /// getBaseElementType - Returns the innermost element type of an array type. 1105 /// For example, will return "int" for int[m][n] 1106 QualType getBaseElementType(const ArrayType *VAT); 1107 1108 /// getBaseElementType - Returns the innermost element type of a type 1109 /// (which needn't actually be an array type). 1110 QualType getBaseElementType(QualType QT); 1111 1112 /// getConstantArrayElementCount - Returns number of constant array elements. 1113 uint64_t getConstantArrayElementCount(const ConstantArrayType *CA) const; 1114 1115 /// getArrayDecayedType - Return the properly qualified result of decaying the 1116 /// specified array type to a pointer. This operation is non-trivial when 1117 /// handling typedefs etc. The canonical type of "T" must be an array type, 1118 /// this returns a pointer to a properly qualified element of the array. 1119 /// 1120 /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. 1121 QualType getArrayDecayedType(QualType T); 1122 1123 /// getPromotedIntegerType - Returns the type that Promotable will 1124 /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable 1125 /// integer type. 1126 QualType getPromotedIntegerType(QualType PromotableType); 1127 1128 /// \brief Whether this is a promotable bitfield reference according 1129 /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). 1130 /// 1131 /// \returns the type this bit-field will promote to, or NULL if no 1132 /// promotion occurs. 1133 QualType isPromotableBitField(Expr *E); 1134 1135 /// getIntegerTypeOrder - Returns the highest ranked integer type: 1136 /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If 1137 /// LHS < RHS, return -1. 1138 int getIntegerTypeOrder(QualType LHS, QualType RHS); 1139 1140 /// getFloatingTypeOrder - Compare the rank of the two specified floating 1141 /// point types, ignoring the domain of the type (i.e. 'double' == 1142 /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If 1143 /// LHS < RHS, return -1. 1144 int getFloatingTypeOrder(QualType LHS, QualType RHS); 1145 1146 /// getFloatingTypeOfSizeWithinDomain - Returns a real floating 1147 /// point or a complex type (based on typeDomain/typeSize). 1148 /// 'typeDomain' is a real floating point or complex type. 1149 /// 'typeSize' is a real floating point or complex type. 1150 QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize, 1151 QualType typeDomain) const; 1152 1153private: 1154 // Helper for integer ordering 1155 unsigned getIntegerRank(Type* T); 1156 1157public: 1158 1159 //===--------------------------------------------------------------------===// 1160 // Type Compatibility Predicates 1161 //===--------------------------------------------------------------------===// 1162 1163 /// Compatibility predicates used to check assignment expressions. 1164 bool typesAreCompatible(QualType, QualType); // C99 6.2.7p1 1165 1166 bool typesAreBlockPointerCompatible(QualType, QualType); 1167 1168 bool isObjCIdType(QualType T) const { 1169 return T == ObjCIdTypedefType; 1170 } 1171 bool isObjCClassType(QualType T) const { 1172 return T == ObjCClassTypedefType; 1173 } 1174 bool isObjCSelType(QualType T) const { 1175 return T == ObjCSelTypedefType; 1176 } 1177 bool QualifiedIdConformsQualifiedId(QualType LHS, QualType RHS); 1178 bool ObjCQualifiedIdTypesAreCompatible(QualType LHS, QualType RHS, 1179 bool ForCompare); 1180 1181 // Check the safety of assignment from LHS to RHS 1182 bool canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, 1183 const ObjCObjectPointerType *RHSOPT); 1184 bool canAssignObjCInterfaces(const ObjCObjectType *LHS, 1185 const ObjCObjectType *RHS); 1186 bool canAssignObjCInterfacesInBlockPointer( 1187 const ObjCObjectPointerType *LHSOPT, 1188 const ObjCObjectPointerType *RHSOPT); 1189 bool areComparableObjCPointerTypes(QualType LHS, QualType RHS); 1190 QualType areCommonBaseCompatible(const ObjCObjectPointerType *LHSOPT, 1191 const ObjCObjectPointerType *RHSOPT); 1192 1193 // Functions for calculating composite types 1194 QualType mergeTypes(QualType, QualType, bool OfBlockPointer=false); 1195 QualType mergeFunctionTypes(QualType, QualType, bool OfBlockPointer=false); 1196 1197 QualType mergeObjCGCQualifiers(QualType, QualType); 1198 1199 /// UsualArithmeticConversionsType - handles the various conversions 1200 /// that are common to binary operators (C99 6.3.1.8, C++ [expr]p9) 1201 /// and returns the result type of that conversion. 1202 QualType UsualArithmeticConversionsType(QualType lhs, QualType rhs); 1203 1204 //===--------------------------------------------------------------------===// 1205 // Integer Predicates 1206 //===--------------------------------------------------------------------===// 1207 1208 // The width of an integer, as defined in C99 6.2.6.2. This is the number 1209 // of bits in an integer type excluding any padding bits. 1210 unsigned getIntWidth(QualType T); 1211 1212 // Per C99 6.2.5p6, for every signed integer type, there is a corresponding 1213 // unsigned integer type. This method takes a signed type, and returns the 1214 // corresponding unsigned integer type. 1215 QualType getCorrespondingUnsignedType(QualType T); 1216 1217 //===--------------------------------------------------------------------===// 1218 // Type Iterators. 1219 //===--------------------------------------------------------------------===// 1220 1221 typedef std::vector<Type*>::iterator type_iterator; 1222 typedef std::vector<Type*>::const_iterator const_type_iterator; 1223 1224 type_iterator types_begin() { return Types.begin(); } 1225 type_iterator types_end() { return Types.end(); } 1226 const_type_iterator types_begin() const { return Types.begin(); } 1227 const_type_iterator types_end() const { return Types.end(); } 1228 1229 //===--------------------------------------------------------------------===// 1230 // Integer Values 1231 //===--------------------------------------------------------------------===// 1232 1233 /// MakeIntValue - Make an APSInt of the appropriate width and 1234 /// signedness for the given \arg Value and integer \arg Type. 1235 llvm::APSInt MakeIntValue(uint64_t Value, QualType Type) { 1236 llvm::APSInt Res(getIntWidth(Type), !Type->isSignedIntegerType()); 1237 Res = Value; 1238 return Res; 1239 } 1240 1241 /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. 1242 ObjCImplementationDecl *getObjCImplementation(ObjCInterfaceDecl *D); 1243 /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. 1244 ObjCCategoryImplDecl *getObjCImplementation(ObjCCategoryDecl *D); 1245 1246 /// \brief Set the implementation of ObjCInterfaceDecl. 1247 void setObjCImplementation(ObjCInterfaceDecl *IFaceD, 1248 ObjCImplementationDecl *ImplD); 1249 /// \brief Set the implementation of ObjCCategoryDecl. 1250 void setObjCImplementation(ObjCCategoryDecl *CatD, 1251 ObjCCategoryImplDecl *ImplD); 1252 1253 /// \brief Allocate an uninitialized TypeSourceInfo. 1254 /// 1255 /// The caller should initialize the memory held by TypeSourceInfo using 1256 /// the TypeLoc wrappers. 1257 /// 1258 /// \param T the type that will be the basis for type source info. This type 1259 /// should refer to how the declarator was written in source code, not to 1260 /// what type semantic analysis resolved the declarator to. 1261 /// 1262 /// \param Size the size of the type info to create, or 0 if the size 1263 /// should be calculated based on the type. 1264 TypeSourceInfo *CreateTypeSourceInfo(QualType T, unsigned Size = 0); 1265 1266 /// \brief Allocate a TypeSourceInfo where all locations have been 1267 /// initialized to a given location, which defaults to the empty 1268 /// location. 1269 TypeSourceInfo * 1270 getTrivialTypeSourceInfo(QualType T, SourceLocation Loc = SourceLocation()); 1271 1272private: 1273 ASTContext(const ASTContext&); // DO NOT IMPLEMENT 1274 void operator=(const ASTContext&); // DO NOT IMPLEMENT 1275 1276 void InitBuiltinTypes(); 1277 void InitBuiltinType(CanQualType &R, BuiltinType::Kind K); 1278 1279 // Return the ObjC type encoding for a given type. 1280 void getObjCEncodingForTypeImpl(QualType t, std::string &S, 1281 bool ExpandPointedToStructures, 1282 bool ExpandStructures, 1283 const FieldDecl *Field, 1284 bool OutermostType = false, 1285 bool EncodingProperty = false); 1286 1287 const ASTRecordLayout &getObjCLayout(const ObjCInterfaceDecl *D, 1288 const ObjCImplementationDecl *Impl); 1289 1290private: 1291 // FIXME: This currently contains the set of StoredDeclMaps used 1292 // by DeclContext objects. This probably should not be in ASTContext, 1293 // but we include it here so that ASTContext can quickly deallocate them. 1294 llvm::PointerIntPair<StoredDeclsMap*,1> LastSDM; 1295 friend class DeclContext; 1296 friend class DeclarationNameTable; 1297 void ReleaseDeclContextMaps(); 1298}; 1299 1300/// @brief Utility function for constructing a nullary selector. 1301static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) { 1302 IdentifierInfo* II = &Ctx.Idents.get(name); 1303 return Ctx.Selectors.getSelector(0, &II); 1304} 1305 1306/// @brief Utility function for constructing an unary selector. 1307static inline Selector GetUnarySelector(const char* name, ASTContext& Ctx) { 1308 IdentifierInfo* II = &Ctx.Idents.get(name); 1309 return Ctx.Selectors.getSelector(1, &II); 1310} 1311 1312} // end namespace clang 1313 1314// operator new and delete aren't allowed inside namespaces. 1315// The throw specifications are mandated by the standard. 1316/// @brief Placement new for using the ASTContext's allocator. 1317/// 1318/// This placement form of operator new uses the ASTContext's allocator for 1319/// obtaining memory. It is a non-throwing new, which means that it returns 1320/// null on error. (If that is what the allocator does. The current does, so if 1321/// this ever changes, this operator will have to be changed, too.) 1322/// Usage looks like this (assuming there's an ASTContext 'Context' in scope): 1323/// @code 1324/// // Default alignment (8) 1325/// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments); 1326/// // Specific alignment 1327/// IntegerLiteral *Ex2 = new (Context, 4) IntegerLiteral(arguments); 1328/// @endcode 1329/// Please note that you cannot use delete on the pointer; it must be 1330/// deallocated using an explicit destructor call followed by 1331/// @c Context.Deallocate(Ptr). 1332/// 1333/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 1334/// @param C The ASTContext that provides the allocator. 1335/// @param Alignment The alignment of the allocated memory (if the underlying 1336/// allocator supports it). 1337/// @return The allocated memory. Could be NULL. 1338inline void *operator new(size_t Bytes, clang::ASTContext &C, 1339 size_t Alignment) throw () { 1340 return C.Allocate(Bytes, Alignment); 1341} 1342/// @brief Placement delete companion to the new above. 1343/// 1344/// This operator is just a companion to the new above. There is no way of 1345/// invoking it directly; see the new operator for more details. This operator 1346/// is called implicitly by the compiler if a placement new expression using 1347/// the ASTContext throws in the object constructor. 1348inline void operator delete(void *Ptr, clang::ASTContext &C, size_t) 1349 throw () { 1350 C.Deallocate(Ptr); 1351} 1352 1353/// This placement form of operator new[] uses the ASTContext's allocator for 1354/// obtaining memory. It is a non-throwing new[], which means that it returns 1355/// null on error. 1356/// Usage looks like this (assuming there's an ASTContext 'Context' in scope): 1357/// @code 1358/// // Default alignment (8) 1359/// char *data = new (Context) char[10]; 1360/// // Specific alignment 1361/// char *data = new (Context, 4) char[10]; 1362/// @endcode 1363/// Please note that you cannot use delete on the pointer; it must be 1364/// deallocated using an explicit destructor call followed by 1365/// @c Context.Deallocate(Ptr). 1366/// 1367/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 1368/// @param C The ASTContext that provides the allocator. 1369/// @param Alignment The alignment of the allocated memory (if the underlying 1370/// allocator supports it). 1371/// @return The allocated memory. Could be NULL. 1372inline void *operator new[](size_t Bytes, clang::ASTContext& C, 1373 size_t Alignment = 8) throw () { 1374 return C.Allocate(Bytes, Alignment); 1375} 1376 1377/// @brief Placement delete[] companion to the new[] above. 1378/// 1379/// This operator is just a companion to the new[] above. There is no way of 1380/// invoking it directly; see the new[] operator for more details. This operator 1381/// is called implicitly by the compiler if a placement new[] expression using 1382/// the ASTContext throws in the object constructor. 1383inline void operator delete[](void *Ptr, clang::ASTContext &C, size_t) 1384 throw () { 1385 C.Deallocate(Ptr); 1386} 1387 1388#endif 1389