ASTContext.h revision ea1471e0e967548c596a71469702f8846dbaf3c0
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::SmallVectorImpl<const Expr *> &Layout); 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 std::pair<CharUnits, CharUnits> getTypeInfoInChars(const Type *T); 914 std::pair<CharUnits, CharUnits> getTypeInfoInChars(QualType T); 915 916 /// getPreferredTypeAlign - Return the "preferred" alignment of the specified 917 /// type for the current target in bits. This can be different than the ABI 918 /// alignment in cases where it is beneficial for performance to overalign 919 /// a data type. 920 unsigned getPreferredTypeAlign(const Type *T); 921 922 /// getDeclAlign - Return a conservative estimate of the alignment of 923 /// the specified decl. Note that bitfields do not have a valid alignment, so 924 /// this method will assert on them. 925 /// If @p RefAsPointee, references are treated like their underlying type 926 /// (for alignof), else they're treated like pointers (for CodeGen). 927 CharUnits getDeclAlign(const Decl *D, bool RefAsPointee = false); 928 929 /// getASTRecordLayout - Get or compute information about the layout of the 930 /// specified record (struct/union/class), which indicates its size and field 931 /// position information. 932 const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D); 933 934 /// getASTObjCInterfaceLayout - Get or compute information about the 935 /// layout of the specified Objective-C interface. 936 const ASTRecordLayout &getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D); 937 938 void DumpRecordLayout(const RecordDecl *RD, llvm::raw_ostream &OS); 939 940 /// getASTObjCImplementationLayout - Get or compute information about 941 /// the layout of the specified Objective-C implementation. This may 942 /// differ from the interface if synthesized ivars are present. 943 const ASTRecordLayout & 944 getASTObjCImplementationLayout(const ObjCImplementationDecl *D); 945 946 /// getKeyFunction - Get the key function for the given record decl. 947 /// The key function is, according to the Itanium C++ ABI section 5.2.3: 948 /// 949 /// ...the first non-pure virtual function that is not inline at the point 950 /// of class definition. 951 const CXXMethodDecl *getKeyFunction(const CXXRecordDecl *RD); 952 953 void CollectObjCIvars(const ObjCInterfaceDecl *OI, 954 llvm::SmallVectorImpl<FieldDecl*> &Fields); 955 956 void ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI, 957 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars); 958 void CollectNonClassIvars(const ObjCInterfaceDecl *OI, 959 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars); 960 unsigned CountNonClassIvars(const ObjCInterfaceDecl *OI); 961 void CollectInheritedProtocols(const Decl *CDecl, 962 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols); 963 964 //===--------------------------------------------------------------------===// 965 // Type Operators 966 //===--------------------------------------------------------------------===// 967 968 /// getCanonicalType - Return the canonical (structural) type corresponding to 969 /// the specified potentially non-canonical type. The non-canonical version 970 /// of a type may have many "decorated" versions of types. Decorators can 971 /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed 972 /// to be free of any of these, allowing two canonical types to be compared 973 /// for exact equality with a simple pointer comparison. 974 CanQualType getCanonicalType(QualType T); 975 const Type *getCanonicalType(const Type *T) { 976 return T->getCanonicalTypeInternal().getTypePtr(); 977 } 978 979 /// getCanonicalParamType - Return the canonical parameter type 980 /// corresponding to the specific potentially non-canonical one. 981 /// Qualifiers are stripped off, functions are turned into function 982 /// pointers, and arrays decay one level into pointers. 983 CanQualType getCanonicalParamType(QualType T); 984 985 /// \brief Determine whether the given types are equivalent. 986 bool hasSameType(QualType T1, QualType T2) { 987 return getCanonicalType(T1) == getCanonicalType(T2); 988 } 989 990 /// \brief Returns this type as a completely-unqualified array type, 991 /// capturing the qualifiers in Quals. This will remove the minimal amount of 992 /// sugaring from the types, similar to the behavior of 993 /// QualType::getUnqualifiedType(). 994 /// 995 /// \param T is the qualified type, which may be an ArrayType 996 /// 997 /// \param Quals will receive the full set of qualifiers that were 998 /// applied to the array. 999 /// 1000 /// \returns if this is an array type, the completely unqualified array type 1001 /// that corresponds to it. Otherwise, returns T.getUnqualifiedType(). 1002 QualType getUnqualifiedArrayType(QualType T, Qualifiers &Quals); 1003 1004 /// \brief Determine whether the given types are equivalent after 1005 /// cvr-qualifiers have been removed. 1006 bool hasSameUnqualifiedType(QualType T1, QualType T2) { 1007 CanQualType CT1 = getCanonicalType(T1); 1008 CanQualType CT2 = getCanonicalType(T2); 1009 1010 Qualifiers Quals; 1011 QualType UnqualT1 = getUnqualifiedArrayType(CT1, Quals); 1012 QualType UnqualT2 = getUnqualifiedArrayType(CT2, Quals); 1013 return UnqualT1 == UnqualT2; 1014 } 1015 1016 /// \brief Retrieves the "canonical" declaration of 1017 1018 /// \brief Retrieves the "canonical" nested name specifier for a 1019 /// given nested name specifier. 1020 /// 1021 /// The canonical nested name specifier is a nested name specifier 1022 /// that uniquely identifies a type or namespace within the type 1023 /// system. For example, given: 1024 /// 1025 /// \code 1026 /// namespace N { 1027 /// struct S { 1028 /// template<typename T> struct X { typename T* type; }; 1029 /// }; 1030 /// } 1031 /// 1032 /// template<typename T> struct Y { 1033 /// typename N::S::X<T>::type member; 1034 /// }; 1035 /// \endcode 1036 /// 1037 /// Here, the nested-name-specifier for N::S::X<T>:: will be 1038 /// S::X<template-param-0-0>, since 'S' and 'X' are uniquely defined 1039 /// by declarations in the type system and the canonical type for 1040 /// the template type parameter 'T' is template-param-0-0. 1041 NestedNameSpecifier * 1042 getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS); 1043 1044 /// \brief Retrieves the canonical representation of the given 1045 /// calling convention. 1046 CallingConv getCanonicalCallConv(CallingConv CC) { 1047 if (CC == CC_C) 1048 return CC_Default; 1049 return CC; 1050 } 1051 1052 /// \brief Determines whether two calling conventions name the same 1053 /// calling convention. 1054 bool isSameCallConv(CallingConv lcc, CallingConv rcc) { 1055 return (getCanonicalCallConv(lcc) == getCanonicalCallConv(rcc)); 1056 } 1057 1058 /// \brief Retrieves the "canonical" template name that refers to a 1059 /// given template. 1060 /// 1061 /// The canonical template name is the simplest expression that can 1062 /// be used to refer to a given template. For most templates, this 1063 /// expression is just the template declaration itself. For example, 1064 /// the template std::vector can be referred to via a variety of 1065 /// names---std::vector, ::std::vector, vector (if vector is in 1066 /// scope), etc.---but all of these names map down to the same 1067 /// TemplateDecl, which is used to form the canonical template name. 1068 /// 1069 /// Dependent template names are more interesting. Here, the 1070 /// template name could be something like T::template apply or 1071 /// std::allocator<T>::template rebind, where the nested name 1072 /// specifier itself is dependent. In this case, the canonical 1073 /// template name uses the shortest form of the dependent 1074 /// nested-name-specifier, which itself contains all canonical 1075 /// types, values, and templates. 1076 TemplateName getCanonicalTemplateName(TemplateName Name); 1077 1078 /// \brief Determine whether the given template names refer to the same 1079 /// template. 1080 bool hasSameTemplateName(TemplateName X, TemplateName Y); 1081 1082 /// \brief Retrieve the "canonical" template argument. 1083 /// 1084 /// The canonical template argument is the simplest template argument 1085 /// (which may be a type, value, expression, or declaration) that 1086 /// expresses the value of the argument. 1087 TemplateArgument getCanonicalTemplateArgument(const TemplateArgument &Arg); 1088 1089 /// Type Query functions. If the type is an instance of the specified class, 1090 /// return the Type pointer for the underlying maximally pretty type. This 1091 /// is a member of ASTContext because this may need to do some amount of 1092 /// canonicalization, e.g. to move type qualifiers into the element type. 1093 const ArrayType *getAsArrayType(QualType T); 1094 const ConstantArrayType *getAsConstantArrayType(QualType T) { 1095 return dyn_cast_or_null<ConstantArrayType>(getAsArrayType(T)); 1096 } 1097 const VariableArrayType *getAsVariableArrayType(QualType T) { 1098 return dyn_cast_or_null<VariableArrayType>(getAsArrayType(T)); 1099 } 1100 const IncompleteArrayType *getAsIncompleteArrayType(QualType T) { 1101 return dyn_cast_or_null<IncompleteArrayType>(getAsArrayType(T)); 1102 } 1103 const DependentSizedArrayType *getAsDependentSizedArrayType(QualType T) { 1104 return dyn_cast_or_null<DependentSizedArrayType>(getAsArrayType(T)); 1105 } 1106 1107 /// getBaseElementType - Returns the innermost element type of an array type. 1108 /// For example, will return "int" for int[m][n] 1109 QualType getBaseElementType(const ArrayType *VAT); 1110 1111 /// getBaseElementType - Returns the innermost element type of a type 1112 /// (which needn't actually be an array type). 1113 QualType getBaseElementType(QualType QT); 1114 1115 /// getConstantArrayElementCount - Returns number of constant array elements. 1116 uint64_t getConstantArrayElementCount(const ConstantArrayType *CA) const; 1117 1118 /// getArrayDecayedType - Return the properly qualified result of decaying the 1119 /// specified array type to a pointer. This operation is non-trivial when 1120 /// handling typedefs etc. The canonical type of "T" must be an array type, 1121 /// this returns a pointer to a properly qualified element of the array. 1122 /// 1123 /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. 1124 QualType getArrayDecayedType(QualType T); 1125 1126 /// getPromotedIntegerType - Returns the type that Promotable will 1127 /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable 1128 /// integer type. 1129 QualType getPromotedIntegerType(QualType PromotableType); 1130 1131 /// \brief Whether this is a promotable bitfield reference according 1132 /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). 1133 /// 1134 /// \returns the type this bit-field will promote to, or NULL if no 1135 /// promotion occurs. 1136 QualType isPromotableBitField(Expr *E); 1137 1138 /// getIntegerTypeOrder - Returns the highest ranked integer type: 1139 /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If 1140 /// LHS < RHS, return -1. 1141 int getIntegerTypeOrder(QualType LHS, QualType RHS); 1142 1143 /// getFloatingTypeOrder - Compare the rank of the two specified floating 1144 /// point types, ignoring the domain of the type (i.e. 'double' == 1145 /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If 1146 /// LHS < RHS, return -1. 1147 int getFloatingTypeOrder(QualType LHS, QualType RHS); 1148 1149 /// getFloatingTypeOfSizeWithinDomain - Returns a real floating 1150 /// point or a complex type (based on typeDomain/typeSize). 1151 /// 'typeDomain' is a real floating point or complex type. 1152 /// 'typeSize' is a real floating point or complex type. 1153 QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize, 1154 QualType typeDomain) const; 1155 1156private: 1157 // Helper for integer ordering 1158 unsigned getIntegerRank(Type* T); 1159 1160public: 1161 1162 //===--------------------------------------------------------------------===// 1163 // Type Compatibility Predicates 1164 //===--------------------------------------------------------------------===// 1165 1166 /// Compatibility predicates used to check assignment expressions. 1167 bool typesAreCompatible(QualType, QualType); // C99 6.2.7p1 1168 1169 bool typesAreBlockPointerCompatible(QualType, QualType); 1170 1171 bool isObjCIdType(QualType T) const { 1172 return T == ObjCIdTypedefType; 1173 } 1174 bool isObjCClassType(QualType T) const { 1175 return T == ObjCClassTypedefType; 1176 } 1177 bool isObjCSelType(QualType T) const { 1178 return T == ObjCSelTypedefType; 1179 } 1180 bool QualifiedIdConformsQualifiedId(QualType LHS, QualType RHS); 1181 bool ObjCQualifiedIdTypesAreCompatible(QualType LHS, QualType RHS, 1182 bool ForCompare); 1183 1184 // Check the safety of assignment from LHS to RHS 1185 bool canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, 1186 const ObjCObjectPointerType *RHSOPT); 1187 bool canAssignObjCInterfaces(const ObjCObjectType *LHS, 1188 const ObjCObjectType *RHS); 1189 bool canAssignObjCInterfacesInBlockPointer( 1190 const ObjCObjectPointerType *LHSOPT, 1191 const ObjCObjectPointerType *RHSOPT); 1192 bool areComparableObjCPointerTypes(QualType LHS, QualType RHS); 1193 QualType areCommonBaseCompatible(const ObjCObjectPointerType *LHSOPT, 1194 const ObjCObjectPointerType *RHSOPT); 1195 1196 // Functions for calculating composite types 1197 QualType mergeTypes(QualType, QualType, bool OfBlockPointer=false); 1198 QualType mergeFunctionTypes(QualType, QualType, bool OfBlockPointer=false); 1199 1200 QualType mergeObjCGCQualifiers(QualType, QualType); 1201 1202 /// UsualArithmeticConversionsType - handles the various conversions 1203 /// that are common to binary operators (C99 6.3.1.8, C++ [expr]p9) 1204 /// and returns the result type of that conversion. 1205 QualType UsualArithmeticConversionsType(QualType lhs, QualType rhs); 1206 1207 //===--------------------------------------------------------------------===// 1208 // Integer Predicates 1209 //===--------------------------------------------------------------------===// 1210 1211 // The width of an integer, as defined in C99 6.2.6.2. This is the number 1212 // of bits in an integer type excluding any padding bits. 1213 unsigned getIntWidth(QualType T); 1214 1215 // Per C99 6.2.5p6, for every signed integer type, there is a corresponding 1216 // unsigned integer type. This method takes a signed type, and returns the 1217 // corresponding unsigned integer type. 1218 QualType getCorrespondingUnsignedType(QualType T); 1219 1220 //===--------------------------------------------------------------------===// 1221 // Type Iterators. 1222 //===--------------------------------------------------------------------===// 1223 1224 typedef std::vector<Type*>::iterator type_iterator; 1225 typedef std::vector<Type*>::const_iterator const_type_iterator; 1226 1227 type_iterator types_begin() { return Types.begin(); } 1228 type_iterator types_end() { return Types.end(); } 1229 const_type_iterator types_begin() const { return Types.begin(); } 1230 const_type_iterator types_end() const { return Types.end(); } 1231 1232 //===--------------------------------------------------------------------===// 1233 // Integer Values 1234 //===--------------------------------------------------------------------===// 1235 1236 /// MakeIntValue - Make an APSInt of the appropriate width and 1237 /// signedness for the given \arg Value and integer \arg Type. 1238 llvm::APSInt MakeIntValue(uint64_t Value, QualType Type) { 1239 llvm::APSInt Res(getIntWidth(Type), !Type->isSignedIntegerType()); 1240 Res = Value; 1241 return Res; 1242 } 1243 1244 /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. 1245 ObjCImplementationDecl *getObjCImplementation(ObjCInterfaceDecl *D); 1246 /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. 1247 ObjCCategoryImplDecl *getObjCImplementation(ObjCCategoryDecl *D); 1248 1249 /// \brief Set the implementation of ObjCInterfaceDecl. 1250 void setObjCImplementation(ObjCInterfaceDecl *IFaceD, 1251 ObjCImplementationDecl *ImplD); 1252 /// \brief Set the implementation of ObjCCategoryDecl. 1253 void setObjCImplementation(ObjCCategoryDecl *CatD, 1254 ObjCCategoryImplDecl *ImplD); 1255 1256 /// \brief Allocate an uninitialized TypeSourceInfo. 1257 /// 1258 /// The caller should initialize the memory held by TypeSourceInfo using 1259 /// the TypeLoc wrappers. 1260 /// 1261 /// \param T the type that will be the basis for type source info. This type 1262 /// should refer to how the declarator was written in source code, not to 1263 /// what type semantic analysis resolved the declarator to. 1264 /// 1265 /// \param Size the size of the type info to create, or 0 if the size 1266 /// should be calculated based on the type. 1267 TypeSourceInfo *CreateTypeSourceInfo(QualType T, unsigned Size = 0); 1268 1269 /// \brief Allocate a TypeSourceInfo where all locations have been 1270 /// initialized to a given location, which defaults to the empty 1271 /// location. 1272 TypeSourceInfo * 1273 getTrivialTypeSourceInfo(QualType T, SourceLocation Loc = SourceLocation()); 1274 1275private: 1276 ASTContext(const ASTContext&); // DO NOT IMPLEMENT 1277 void operator=(const ASTContext&); // DO NOT IMPLEMENT 1278 1279 void InitBuiltinTypes(); 1280 void InitBuiltinType(CanQualType &R, BuiltinType::Kind K); 1281 1282 // Return the ObjC type encoding for a given type. 1283 void getObjCEncodingForTypeImpl(QualType t, std::string &S, 1284 bool ExpandPointedToStructures, 1285 bool ExpandStructures, 1286 const FieldDecl *Field, 1287 bool OutermostType = false, 1288 bool EncodingProperty = false); 1289 1290 const ASTRecordLayout &getObjCLayout(const ObjCInterfaceDecl *D, 1291 const ObjCImplementationDecl *Impl); 1292 1293private: 1294 // FIXME: This currently contains the set of StoredDeclMaps used 1295 // by DeclContext objects. This probably should not be in ASTContext, 1296 // but we include it here so that ASTContext can quickly deallocate them. 1297 llvm::PointerIntPair<StoredDeclsMap*,1> LastSDM; 1298 friend class DeclContext; 1299 friend class DeclarationNameTable; 1300 void ReleaseDeclContextMaps(); 1301}; 1302 1303/// @brief Utility function for constructing a nullary selector. 1304static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) { 1305 IdentifierInfo* II = &Ctx.Idents.get(name); 1306 return Ctx.Selectors.getSelector(0, &II); 1307} 1308 1309/// @brief Utility function for constructing an unary selector. 1310static inline Selector GetUnarySelector(const char* name, ASTContext& Ctx) { 1311 IdentifierInfo* II = &Ctx.Idents.get(name); 1312 return Ctx.Selectors.getSelector(1, &II); 1313} 1314 1315} // end namespace clang 1316 1317// operator new and delete aren't allowed inside namespaces. 1318// The throw specifications are mandated by the standard. 1319/// @brief Placement new for using the ASTContext's allocator. 1320/// 1321/// This placement form of operator new uses the ASTContext's allocator for 1322/// obtaining memory. It is a non-throwing new, which means that it returns 1323/// null on error. (If that is what the allocator does. The current does, so if 1324/// this ever changes, this operator will have to be changed, too.) 1325/// Usage looks like this (assuming there's an ASTContext 'Context' in scope): 1326/// @code 1327/// // Default alignment (8) 1328/// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments); 1329/// // Specific alignment 1330/// IntegerLiteral *Ex2 = new (Context, 4) IntegerLiteral(arguments); 1331/// @endcode 1332/// Please note that you cannot use delete on the pointer; it must be 1333/// deallocated using an explicit destructor call followed by 1334/// @c Context.Deallocate(Ptr). 1335/// 1336/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 1337/// @param C The ASTContext that provides the allocator. 1338/// @param Alignment The alignment of the allocated memory (if the underlying 1339/// allocator supports it). 1340/// @return The allocated memory. Could be NULL. 1341inline void *operator new(size_t Bytes, clang::ASTContext &C, 1342 size_t Alignment) throw () { 1343 return C.Allocate(Bytes, Alignment); 1344} 1345/// @brief Placement delete companion to the new above. 1346/// 1347/// This operator is just a companion to the new above. There is no way of 1348/// invoking it directly; see the new operator for more details. This operator 1349/// is called implicitly by the compiler if a placement new expression using 1350/// the ASTContext throws in the object constructor. 1351inline void operator delete(void *Ptr, clang::ASTContext &C, size_t) 1352 throw () { 1353 C.Deallocate(Ptr); 1354} 1355 1356/// This placement form of operator new[] uses the ASTContext's allocator for 1357/// obtaining memory. It is a non-throwing new[], which means that it returns 1358/// null on error. 1359/// Usage looks like this (assuming there's an ASTContext 'Context' in scope): 1360/// @code 1361/// // Default alignment (8) 1362/// char *data = new (Context) char[10]; 1363/// // Specific alignment 1364/// char *data = new (Context, 4) char[10]; 1365/// @endcode 1366/// Please note that you cannot use delete on the pointer; it must be 1367/// deallocated using an explicit destructor call followed by 1368/// @c Context.Deallocate(Ptr). 1369/// 1370/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 1371/// @param C The ASTContext that provides the allocator. 1372/// @param Alignment The alignment of the allocated memory (if the underlying 1373/// allocator supports it). 1374/// @return The allocated memory. Could be NULL. 1375inline void *operator new[](size_t Bytes, clang::ASTContext& C, 1376 size_t Alignment = 8) throw () { 1377 return C.Allocate(Bytes, Alignment); 1378} 1379 1380/// @brief Placement delete[] companion to the new[] above. 1381/// 1382/// This operator is just a companion to the new[] above. There is no way of 1383/// invoking it directly; see the new[] operator for more details. This operator 1384/// is called implicitly by the compiler if a placement new[] expression using 1385/// the ASTContext throws in the object constructor. 1386inline void operator delete[](void *Ptr, clang::ASTContext &C, size_t) 1387 throw () { 1388 C.Deallocate(Ptr); 1389} 1390 1391#endif 1392