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