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