1 //===--- ASTContext.h - Context to hold long-lived AST nodes ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ASTContext interface.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_ASTCONTEXT_H
15 #define LLVM_CLANG_AST_ASTCONTEXT_H
17 #include "clang/Basic/AddressSpaces.h"
18 #include "clang/Basic/IdentifierTable.h"
19 #include "clang/Basic/LangOptions.h"
20 #include "clang/Basic/OperatorKinds.h"
21 #include "clang/Basic/PartialDiagnostic.h"
22 #include "clang/Basic/VersionTuple.h"
23 #include "clang/AST/Decl.h"
24 #include "clang/AST/NestedNameSpecifier.h"
25 #include "clang/AST/PrettyPrinter.h"
26 #include "clang/AST/TemplateName.h"
27 #include "clang/AST/Type.h"
28 #include "clang/AST/CanonicalType.h"
29 #include "clang/AST/UsuallyTinyPtrVector.h"
30 #include "llvm/ADT/DenseMap.h"
31 #include "llvm/ADT/FoldingSet.h"
32 #include "llvm/ADT/IntrusiveRefCntPtr.h"
33 #include "llvm/ADT/OwningPtr.h"
34 #include "llvm/ADT/SmallPtrSet.h"
35 #include "llvm/Support/Allocator.h"
45 class ASTRecordLayout;
50 class ExternalASTSource;
51 class ASTMutationListener;
52 class IdentifierTable;
65 class ObjCIvarRefExpr;
66 class ObjCPropertyDecl;
70 class TemplateTemplateParmDecl;
71 class TemplateTypeParmDecl;
72 class TranslationUnitDecl;
74 class TypedefNameDecl;
76 class UsingShadowDecl;
77 class UnresolvedSetIterator;
79 namespace Builtin { class Context; }
81 /// ASTContext - This class holds long-lived AST nodes (such as types and
82 /// decls) that can be referred to throughout the semantic analysis of a file.
83 class ASTContext : public llvm::RefCountedBase<ASTContext> {
84 ASTContext &this_() { return *this; }
86 mutable std::vector<Type*> Types;
87 mutable llvm::FoldingSet<ExtQuals> ExtQualNodes;
88 mutable llvm::FoldingSet<ComplexType> ComplexTypes;
89 mutable llvm::FoldingSet<PointerType> PointerTypes;
90 mutable llvm::FoldingSet<BlockPointerType> BlockPointerTypes;
91 mutable llvm::FoldingSet<LValueReferenceType> LValueReferenceTypes;
92 mutable llvm::FoldingSet<RValueReferenceType> RValueReferenceTypes;
93 mutable llvm::FoldingSet<MemberPointerType> MemberPointerTypes;
94 mutable llvm::FoldingSet<ConstantArrayType> ConstantArrayTypes;
95 mutable llvm::FoldingSet<IncompleteArrayType> IncompleteArrayTypes;
96 mutable std::vector<VariableArrayType*> VariableArrayTypes;
97 mutable llvm::FoldingSet<DependentSizedArrayType> DependentSizedArrayTypes;
98 mutable llvm::FoldingSet<DependentSizedExtVectorType>
99 DependentSizedExtVectorTypes;
100 mutable llvm::FoldingSet<VectorType> VectorTypes;
101 mutable llvm::FoldingSet<FunctionNoProtoType> FunctionNoProtoTypes;
102 mutable llvm::ContextualFoldingSet<FunctionProtoType, ASTContext&>
104 mutable llvm::FoldingSet<DependentTypeOfExprType> DependentTypeOfExprTypes;
105 mutable llvm::FoldingSet<DependentDecltypeType> DependentDecltypeTypes;
106 mutable llvm::FoldingSet<TemplateTypeParmType> TemplateTypeParmTypes;
107 mutable llvm::FoldingSet<SubstTemplateTypeParmType>
108 SubstTemplateTypeParmTypes;
109 mutable llvm::FoldingSet<SubstTemplateTypeParmPackType>
110 SubstTemplateTypeParmPackTypes;
111 mutable llvm::ContextualFoldingSet<TemplateSpecializationType, ASTContext&>
112 TemplateSpecializationTypes;
113 mutable llvm::FoldingSet<ParenType> ParenTypes;
114 mutable llvm::FoldingSet<ElaboratedType> ElaboratedTypes;
115 mutable llvm::FoldingSet<DependentNameType> DependentNameTypes;
116 mutable llvm::ContextualFoldingSet<DependentTemplateSpecializationType,
118 DependentTemplateSpecializationTypes;
119 llvm::FoldingSet<PackExpansionType> PackExpansionTypes;
120 mutable llvm::FoldingSet<ObjCObjectTypeImpl> ObjCObjectTypes;
121 mutable llvm::FoldingSet<ObjCObjectPointerType> ObjCObjectPointerTypes;
122 mutable llvm::FoldingSet<AutoType> AutoTypes;
123 llvm::FoldingSet<AttributedType> AttributedTypes;
125 mutable llvm::FoldingSet<QualifiedTemplateName> QualifiedTemplateNames;
126 mutable llvm::FoldingSet<DependentTemplateName> DependentTemplateNames;
127 mutable llvm::FoldingSet<SubstTemplateTemplateParmStorage>
128 SubstTemplateTemplateParms;
129 mutable llvm::ContextualFoldingSet<SubstTemplateTemplateParmPackStorage,
131 SubstTemplateTemplateParmPacks;
133 /// \brief The set of nested name specifiers.
135 /// This set is managed by the NestedNameSpecifier class.
136 mutable llvm::FoldingSet<NestedNameSpecifier> NestedNameSpecifiers;
137 mutable NestedNameSpecifier *GlobalNestedNameSpecifier;
138 friend class NestedNameSpecifier;
140 /// ASTRecordLayouts - A cache mapping from RecordDecls to ASTRecordLayouts.
141 /// This is lazily created. This is intentionally not serialized.
142 mutable llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>
144 mutable llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>
147 /// KeyFunctions - A cache mapping from CXXRecordDecls to key functions.
148 llvm::DenseMap<const CXXRecordDecl*, const CXXMethodDecl*> KeyFunctions;
150 /// \brief Mapping from ObjCContainers to their ObjCImplementations.
151 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*> ObjCImpls;
153 /// \brief Mapping from __block VarDecls to their copy initialization expr.
154 llvm::DenseMap<const VarDecl*, Expr*> BlockVarCopyInits;
156 /// \brief Representation of a "canonical" template template parameter that
157 /// is used in canonical template names.
158 class CanonicalTemplateTemplateParm : public llvm::FoldingSetNode {
159 TemplateTemplateParmDecl *Parm;
162 CanonicalTemplateTemplateParm(TemplateTemplateParmDecl *Parm)
165 TemplateTemplateParmDecl *getParam() const { return Parm; }
167 void Profile(llvm::FoldingSetNodeID &ID) { Profile(ID, Parm); }
169 static void Profile(llvm::FoldingSetNodeID &ID,
170 TemplateTemplateParmDecl *Parm);
172 mutable llvm::FoldingSet<CanonicalTemplateTemplateParm>
173 CanonTemplateTemplateParms;
175 TemplateTemplateParmDecl *
176 getCanonicalTemplateTemplateParmDecl(TemplateTemplateParmDecl *TTP) const;
178 /// \brief Whether __[u]int128_t identifier is installed.
179 bool IsInt128Installed;
181 /// BuiltinVaListType - built-in va list type.
182 /// This is initially null and set by Sema::LazilyCreateBuiltin when
183 /// a builtin that takes a valist is encountered.
184 QualType BuiltinVaListType;
186 /// ObjCIdType - a pseudo built-in typedef type (set by Sema).
187 QualType ObjCIdTypedefType;
189 /// ObjCSelType - another pseudo built-in typedef type (set by Sema).
190 QualType ObjCSelTypedefType;
192 /// ObjCProtoType - another pseudo built-in typedef type (set by Sema).
193 QualType ObjCProtoType;
194 const RecordType *ProtoStructType;
196 /// ObjCClassType - another pseudo built-in typedef type (set by Sema).
197 QualType ObjCClassTypedefType;
199 QualType ObjCConstantStringType;
200 mutable RecordDecl *CFConstantStringTypeDecl;
202 mutable RecordDecl *NSConstantStringTypeDecl;
204 mutable RecordDecl *ObjCFastEnumerationStateTypeDecl;
206 /// \brief The type for the C FILE type.
209 /// \brief The type for the C jmp_buf type.
210 TypeDecl *jmp_bufDecl;
212 /// \brief The type for the C sigjmp_buf type.
213 TypeDecl *sigjmp_bufDecl;
215 /// \brief Type for the Block descriptor for Blocks CodeGen.
216 mutable RecordDecl *BlockDescriptorType;
218 /// \brief Type for the Block descriptor for Blocks CodeGen.
219 mutable RecordDecl *BlockDescriptorExtendedType;
221 /// \brief Declaration for the CUDA cudaConfigureCall function.
222 FunctionDecl *cudaConfigureCallDecl;
224 TypeSourceInfo NullTypeSourceInfo;
226 /// \brief Keeps track of all declaration attributes.
228 /// Since so few decls have attrs, we keep them in a hash map instead of
229 /// wasting space in the Decl class.
230 llvm::DenseMap<const Decl*, AttrVec*> DeclAttrs;
232 /// \brief Keeps track of the static data member templates from which
233 /// static data members of class template specializations were instantiated.
235 /// This data structure stores the mapping from instantiations of static
236 /// data members to the static data member representations within the
237 /// class template from which they were instantiated along with the kind
238 /// of instantiation or specialization (a TemplateSpecializationKind - 1).
240 /// Given the following example:
243 /// template<typename T>
248 /// template<typename T>
249 /// T X<T>::value = T(17);
251 /// int *x = &X<int>::value;
254 /// This mapping will contain an entry that maps from the VarDecl for
255 /// X<int>::value to the corresponding VarDecl for X<T>::value (within the
256 /// class template X) and will be marked TSK_ImplicitInstantiation.
257 llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>
258 InstantiatedFromStaticDataMember;
260 /// \brief Keeps track of the declaration from which a UsingDecl was
261 /// created during instantiation. The source declaration is always
262 /// a UsingDecl, an UnresolvedUsingValueDecl, or an
263 /// UnresolvedUsingTypenameDecl.
267 /// template<typename T>
272 /// template<typename T>
273 /// struct B : A<T> {
277 /// template struct B<int>;
280 /// This mapping will contain an entry that maps from the UsingDecl in
281 /// B<int> to the UnresolvedUsingDecl in B<T>.
282 llvm::DenseMap<UsingDecl *, NamedDecl *> InstantiatedFromUsingDecl;
284 llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>
285 InstantiatedFromUsingShadowDecl;
287 llvm::DenseMap<FieldDecl *, FieldDecl *> InstantiatedFromUnnamedFieldDecl;
289 /// \brief Mapping that stores the methods overridden by a given C++
292 /// Since most C++ member functions aren't virtual and therefore
293 /// don't override anything, we store the overridden functions in
294 /// this map on the side rather than within the CXXMethodDecl structure.
295 typedef UsuallyTinyPtrVector<const CXXMethodDecl> CXXMethodVector;
296 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector> OverriddenMethods;
298 TranslationUnitDecl *TUDecl;
300 /// SourceMgr - The associated SourceManager object.
301 SourceManager &SourceMgr;
303 /// LangOpts - The language options used to create the AST associated with
304 /// this ASTContext object.
305 LangOptions LangOpts;
307 /// \brief The allocator used to create AST objects.
309 /// AST objects are never destructed; rather, all memory associated with the
310 /// AST objects will be released when the ASTContext itself is destroyed.
311 mutable llvm::BumpPtrAllocator BumpAlloc;
313 /// \brief Allocator for partial diagnostics.
314 PartialDiagnostic::StorageAllocator DiagAllocator;
316 /// \brief The current C++ ABI.
317 llvm::OwningPtr<CXXABI> ABI;
318 CXXABI *createCXXABI(const TargetInfo &T);
320 /// \brief The logical -> physical address space map.
321 const LangAS::Map &AddrSpaceMap;
323 friend class ASTDeclReader;
326 const TargetInfo &Target;
327 IdentifierTable &Idents;
328 SelectorTable &Selectors;
329 Builtin::Context &BuiltinInfo;
330 mutable DeclarationNameTable DeclarationNames;
331 llvm::OwningPtr<ExternalASTSource> ExternalSource;
332 ASTMutationListener *Listener;
333 clang::PrintingPolicy PrintingPolicy;
335 // Typedefs which may be provided defining the structure of Objective-C
337 QualType ObjCIdRedefinitionType;
338 QualType ObjCClassRedefinitionType;
339 QualType ObjCSelRedefinitionType;
341 SourceManager& getSourceManager() { return SourceMgr; }
342 const SourceManager& getSourceManager() const { return SourceMgr; }
343 void *Allocate(unsigned Size, unsigned Align = 8) const {
344 return BumpAlloc.Allocate(Size, Align);
346 void Deallocate(void *Ptr) const { }
348 /// Return the total amount of physical memory allocated for representing
349 /// AST nodes and type information.
350 size_t getASTAllocatedMemory() const {
351 return BumpAlloc.getTotalMemory();
353 /// Return the total memory used for various side tables.
354 size_t getSideTableAllocatedMemory() const;
356 PartialDiagnostic::StorageAllocator &getDiagAllocator() {
357 return DiagAllocator;
360 const LangOptions& getLangOptions() const { return LangOpts; }
362 Diagnostic &getDiagnostics() const;
364 FullSourceLoc getFullLoc(SourceLocation Loc) const {
365 return FullSourceLoc(Loc,SourceMgr);
368 /// \brief Retrieve the attributes for the given declaration.
369 AttrVec& getDeclAttrs(const Decl *D);
371 /// \brief Erase the attributes corresponding to the given declaration.
372 void eraseDeclAttrs(const Decl *D);
374 /// \brief If this variable is an instantiated static data member of a
375 /// class template specialization, returns the templated static data member
376 /// from which it was instantiated.
377 MemberSpecializationInfo *getInstantiatedFromStaticDataMember(
380 /// \brief Note that the static data member \p Inst is an instantiation of
381 /// the static data member template \p Tmpl of a class template.
382 void setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
383 TemplateSpecializationKind TSK,
384 SourceLocation PointOfInstantiation = SourceLocation());
386 /// \brief If the given using decl is an instantiation of a
387 /// (possibly unresolved) using decl from a template instantiation,
389 NamedDecl *getInstantiatedFromUsingDecl(UsingDecl *Inst);
391 /// \brief Remember that the using decl \p Inst is an instantiation
392 /// of the using decl \p Pattern of a class template.
393 void setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern);
395 void setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst,
396 UsingShadowDecl *Pattern);
397 UsingShadowDecl *getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst);
399 FieldDecl *getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field);
401 void setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, FieldDecl *Tmpl);
403 /// ZeroBitfieldFollowsNonBitfield - return 'true" if 'FD' is a zero-length
404 /// bitfield which follows the non-bitfield 'LastFD'.
405 bool ZeroBitfieldFollowsNonBitfield(const FieldDecl *FD,
406 const FieldDecl *LastFD) const;
408 /// ZeroBitfieldFollowsBitfield - return 'true" if 'FD' is a zero-length
409 /// bitfield which follows the bitfield 'LastFD'.
410 bool ZeroBitfieldFollowsBitfield(const FieldDecl *FD,
411 const FieldDecl *LastFD) const;
413 /// BitfieldFollowsBitfield - return 'true" if 'FD' is a
414 /// bitfield which follows the bitfield 'LastFD'.
415 bool BitfieldFollowsBitfield(const FieldDecl *FD,
416 const FieldDecl *LastFD) const;
418 /// NoneBitfieldFollowsBitfield - return 'true" if 'FD' is not a
419 /// bitfield which follows the bitfield 'LastFD'.
420 bool NoneBitfieldFollowsBitfield(const FieldDecl *FD,
421 const FieldDecl *LastFD) const;
423 /// BitfieldFollowsNoneBitfield - return 'true" if 'FD' is a
424 /// bitfield which follows the none bitfield 'LastFD'.
425 bool BitfieldFollowsNoneBitfield(const FieldDecl *FD,
426 const FieldDecl *LastFD) const;
428 // Access to the set of methods overridden by the given C++ method.
429 typedef CXXMethodVector::iterator overridden_cxx_method_iterator;
430 overridden_cxx_method_iterator
431 overridden_methods_begin(const CXXMethodDecl *Method) const;
433 overridden_cxx_method_iterator
434 overridden_methods_end(const CXXMethodDecl *Method) const;
436 unsigned overridden_methods_size(const CXXMethodDecl *Method) const;
438 /// \brief Note that the given C++ \p Method overrides the given \p
439 /// Overridden method.
440 void addOverriddenMethod(const CXXMethodDecl *Method,
441 const CXXMethodDecl *Overridden);
443 TranslationUnitDecl *getTranslationUnitDecl() const { return TUDecl; }
450 CanQualType WCharTy; // [C++ 3.9.1p5], integer type in C99.
451 CanQualType Char16Ty; // [C++0x 3.9.1p5], integer type in C99.
452 CanQualType Char32Ty; // [C++0x 3.9.1p5], integer type in C99.
453 CanQualType SignedCharTy, ShortTy, IntTy, LongTy, LongLongTy, Int128Ty;
454 CanQualType UnsignedCharTy, UnsignedShortTy, UnsignedIntTy, UnsignedLongTy;
455 CanQualType UnsignedLongLongTy, UnsignedInt128Ty;
456 CanQualType FloatTy, DoubleTy, LongDoubleTy;
457 CanQualType FloatComplexTy, DoubleComplexTy, LongDoubleComplexTy;
458 CanQualType VoidPtrTy, NullPtrTy;
459 CanQualType DependentTy, OverloadTy, BoundMemberTy, UnknownAnyTy;
460 CanQualType ObjCBuiltinIdTy, ObjCBuiltinClassTy, ObjCBuiltinSelTy;
462 // Types for deductions in C++0x [stmt.ranged]'s desugaring. Built on demand.
463 mutable QualType AutoDeductTy; // Deduction against 'auto'.
464 mutable QualType AutoRRefDeductTy; // Deduction against 'auto &&'.
466 ASTContext(const LangOptions& LOpts, SourceManager &SM, const TargetInfo &t,
467 IdentifierTable &idents, SelectorTable &sels,
468 Builtin::Context &builtins,
469 unsigned size_reserve);
473 /// \brief Attach an external AST source to the AST context.
475 /// The external AST source provides the ability to load parts of
476 /// the abstract syntax tree as needed from some external storage,
477 /// e.g., a precompiled header.
478 void setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source);
480 /// \brief Retrieve a pointer to the external AST source associated
481 /// with this AST context, if any.
482 ExternalASTSource *getExternalSource() const { return ExternalSource.get(); }
484 /// \brief Attach an AST mutation listener to the AST context.
486 /// The AST mutation listener provides the ability to track modifications to
487 /// the abstract syntax tree entities committed after they were initially
489 void setASTMutationListener(ASTMutationListener *Listener) {
490 this->Listener = Listener;
493 /// \brief Retrieve a pointer to the AST mutation listener associated
494 /// with this AST context, if any.
495 ASTMutationListener *getASTMutationListener() const { return Listener; }
497 void PrintStats() const;
498 const std::vector<Type*>& getTypes() const { return Types; }
500 //===--------------------------------------------------------------------===//
502 //===--------------------------------------------------------------------===//
505 /// getExtQualType - Return a type with extended qualifiers.
506 QualType getExtQualType(const Type *Base, Qualifiers Quals) const;
508 QualType getTypeDeclTypeSlow(const TypeDecl *Decl) const;
511 /// getAddSpaceQualType - Return the uniqued reference to the type for an
512 /// address space qualified type with the specified type and address space.
513 /// The resulting type has a union of the qualifiers from T and the address
514 /// space. If T already has an address space specifier, it is silently
516 QualType getAddrSpaceQualType(QualType T, unsigned AddressSpace) const;
518 /// getObjCGCQualType - Returns the uniqued reference to the type for an
519 /// objc gc qualified type. The retulting type has a union of the qualifiers
520 /// from T and the gc attribute.
521 QualType getObjCGCQualType(QualType T, Qualifiers::GC gcAttr) const;
523 /// getRestrictType - Returns the uniqued reference to the type for a
524 /// 'restrict' qualified type. The resulting type has a union of the
525 /// qualifiers from T and 'restrict'.
526 QualType getRestrictType(QualType T) const {
527 return T.withFastQualifiers(Qualifiers::Restrict);
530 /// getVolatileType - Returns the uniqued reference to the type for a
531 /// 'volatile' qualified type. The resulting type has a union of the
532 /// qualifiers from T and 'volatile'.
533 QualType getVolatileType(QualType T) const {
534 return T.withFastQualifiers(Qualifiers::Volatile);
537 /// getConstType - Returns the uniqued reference to the type for a
538 /// 'const' qualified type. The resulting type has a union of the
539 /// qualifiers from T and 'const'.
541 /// It can be reasonably expected that this will always be
542 /// equivalent to calling T.withConst().
543 QualType getConstType(QualType T) const { return T.withConst(); }
545 /// adjustFunctionType - Change the ExtInfo on a function type.
546 const FunctionType *adjustFunctionType(const FunctionType *Fn,
547 FunctionType::ExtInfo EInfo);
549 /// getComplexType - Return the uniqued reference to the type for a complex
550 /// number with the specified element type.
551 QualType getComplexType(QualType T) const;
552 CanQualType getComplexType(CanQualType T) const {
553 return CanQualType::CreateUnsafe(getComplexType((QualType) T));
556 /// getPointerType - Return the uniqued reference to the type for a pointer to
557 /// the specified type.
558 QualType getPointerType(QualType T) const;
559 CanQualType getPointerType(CanQualType T) const {
560 return CanQualType::CreateUnsafe(getPointerType((QualType) T));
563 /// getBlockPointerType - Return the uniqued reference to the type for a block
564 /// of the specified type.
565 QualType getBlockPointerType(QualType T) const;
567 /// This gets the struct used to keep track of the descriptor for pointer to
569 QualType getBlockDescriptorType() const;
571 // Set the type for a Block descriptor type.
572 void setBlockDescriptorType(QualType T);
573 /// Get the BlockDescriptorType type, or NULL if it hasn't yet been built.
574 QualType getRawBlockdescriptorType() {
575 if (BlockDescriptorType)
576 return getTagDeclType(BlockDescriptorType);
580 /// This gets the struct used to keep track of the extended descriptor for
581 /// pointer to blocks.
582 QualType getBlockDescriptorExtendedType() const;
584 // Set the type for a Block descriptor extended type.
585 void setBlockDescriptorExtendedType(QualType T);
586 /// Get the BlockDescriptorExtendedType type, or NULL if it hasn't yet been
588 QualType getRawBlockdescriptorExtendedType() const {
589 if (BlockDescriptorExtendedType)
590 return getTagDeclType(BlockDescriptorExtendedType);
594 void setcudaConfigureCallDecl(FunctionDecl *FD) {
595 cudaConfigureCallDecl = FD;
597 FunctionDecl *getcudaConfigureCallDecl() {
598 return cudaConfigureCallDecl;
601 /// This builds the struct used for __block variables.
602 QualType BuildByRefType(llvm::StringRef DeclName, QualType Ty) const;
604 /// Returns true iff we need copy/dispose helpers for the given type.
605 bool BlockRequiresCopying(QualType Ty) const;
607 /// getLValueReferenceType - Return the uniqued reference to the type for an
608 /// lvalue reference to the specified type.
609 QualType getLValueReferenceType(QualType T, bool SpelledAsLValue = true)
612 /// getRValueReferenceType - Return the uniqued reference to the type for an
613 /// rvalue reference to the specified type.
614 QualType getRValueReferenceType(QualType T) const;
616 /// getMemberPointerType - Return the uniqued reference to the type for a
617 /// member pointer to the specified type in the specified class. The class
618 /// is a Type because it could be a dependent name.
619 QualType getMemberPointerType(QualType T, const Type *Cls) const;
621 /// getVariableArrayType - Returns a non-unique reference to the type for a
622 /// variable array of the specified element type.
623 QualType getVariableArrayType(QualType EltTy, Expr *NumElts,
624 ArrayType::ArraySizeModifier ASM,
625 unsigned IndexTypeQuals,
626 SourceRange Brackets) const;
628 /// getDependentSizedArrayType - Returns a non-unique reference to
629 /// the type for a dependently-sized array of the specified element
630 /// type. FIXME: We will need these to be uniqued, or at least
631 /// comparable, at some point.
632 QualType getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
633 ArrayType::ArraySizeModifier ASM,
634 unsigned IndexTypeQuals,
635 SourceRange Brackets) const;
637 /// getIncompleteArrayType - Returns a unique reference to the type for a
638 /// incomplete array of the specified element type.
639 QualType getIncompleteArrayType(QualType EltTy,
640 ArrayType::ArraySizeModifier ASM,
641 unsigned IndexTypeQuals) const;
643 /// getConstantArrayType - Return the unique reference to the type for a
644 /// constant array of the specified element type.
645 QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize,
646 ArrayType::ArraySizeModifier ASM,
647 unsigned IndexTypeQuals) const;
649 /// getVariableArrayDecayedType - Returns a vla type where known sizes
650 /// are replaced with [*].
651 QualType getVariableArrayDecayedType(QualType Ty) const;
653 /// getVectorType - Return the unique reference to a vector type of
654 /// the specified element type and size. VectorType must be a built-in type.
655 QualType getVectorType(QualType VectorType, unsigned NumElts,
656 VectorType::VectorKind VecKind) const;
658 /// getExtVectorType - Return the unique reference to an extended vector type
659 /// of the specified element type and size. VectorType must be a built-in
661 QualType getExtVectorType(QualType VectorType, unsigned NumElts) const;
663 /// getDependentSizedExtVectorType - Returns a non-unique reference to
664 /// the type for a dependently-sized vector of the specified element
665 /// type. FIXME: We will need these to be uniqued, or at least
666 /// comparable, at some point.
667 QualType getDependentSizedExtVectorType(QualType VectorType,
669 SourceLocation AttrLoc) const;
671 /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
673 QualType getFunctionNoProtoType(QualType ResultTy,
674 const FunctionType::ExtInfo &Info) const;
676 QualType getFunctionNoProtoType(QualType ResultTy) const {
677 return getFunctionNoProtoType(ResultTy, FunctionType::ExtInfo());
680 /// getFunctionType - Return a normal function type with a typed
682 QualType getFunctionType(QualType ResultTy,
683 const QualType *Args, unsigned NumArgs,
684 const FunctionProtoType::ExtProtoInfo &EPI) const;
686 /// getTypeDeclType - Return the unique reference to the type for
687 /// the specified type declaration.
688 QualType getTypeDeclType(const TypeDecl *Decl,
689 const TypeDecl *PrevDecl = 0) const {
690 assert(Decl && "Passed null for Decl param");
691 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
694 assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl");
695 Decl->TypeForDecl = PrevDecl->TypeForDecl;
696 return QualType(PrevDecl->TypeForDecl, 0);
699 return getTypeDeclTypeSlow(Decl);
702 /// getTypedefType - Return the unique reference to the type for the
703 /// specified typedef-name decl.
704 QualType getTypedefType(const TypedefNameDecl *Decl,
705 QualType Canon = QualType()) const;
707 QualType getRecordType(const RecordDecl *Decl) const;
709 QualType getEnumType(const EnumDecl *Decl) const;
711 QualType getInjectedClassNameType(CXXRecordDecl *Decl, QualType TST) const;
713 QualType getAttributedType(AttributedType::Kind attrKind,
714 QualType modifiedType,
715 QualType equivalentType);
717 QualType getSubstTemplateTypeParmType(const TemplateTypeParmType *Replaced,
718 QualType Replacement) const;
719 QualType getSubstTemplateTypeParmPackType(
720 const TemplateTypeParmType *Replaced,
721 const TemplateArgument &ArgPack);
723 QualType getTemplateTypeParmType(unsigned Depth, unsigned Index,
725 TemplateTypeParmDecl *ParmDecl = 0) const;
727 QualType getTemplateSpecializationType(TemplateName T,
728 const TemplateArgument *Args,
730 QualType Canon = QualType()) const;
732 QualType getCanonicalTemplateSpecializationType(TemplateName T,
733 const TemplateArgument *Args,
734 unsigned NumArgs) const;
736 QualType getTemplateSpecializationType(TemplateName T,
737 const TemplateArgumentListInfo &Args,
738 QualType Canon = QualType()) const;
741 getTemplateSpecializationTypeInfo(TemplateName T, SourceLocation TLoc,
742 const TemplateArgumentListInfo &Args,
743 QualType Canon = QualType()) const;
745 QualType getParenType(QualType NamedType) const;
747 QualType getElaboratedType(ElaboratedTypeKeyword Keyword,
748 NestedNameSpecifier *NNS,
749 QualType NamedType) const;
750 QualType getDependentNameType(ElaboratedTypeKeyword Keyword,
751 NestedNameSpecifier *NNS,
752 const IdentifierInfo *Name,
753 QualType Canon = QualType()) const;
755 QualType getDependentTemplateSpecializationType(ElaboratedTypeKeyword Keyword,
756 NestedNameSpecifier *NNS,
757 const IdentifierInfo *Name,
758 const TemplateArgumentListInfo &Args) const;
759 QualType getDependentTemplateSpecializationType(ElaboratedTypeKeyword Keyword,
760 NestedNameSpecifier *NNS,
761 const IdentifierInfo *Name,
763 const TemplateArgument *Args) const;
765 QualType getPackExpansionType(QualType Pattern,
766 llvm::Optional<unsigned> NumExpansions);
768 QualType getObjCInterfaceType(const ObjCInterfaceDecl *Decl) const;
770 QualType getObjCObjectType(QualType Base,
771 ObjCProtocolDecl * const *Protocols,
772 unsigned NumProtocols) const;
774 /// getObjCObjectPointerType - Return a ObjCObjectPointerType type
775 /// for the given ObjCObjectType.
776 QualType getObjCObjectPointerType(QualType OIT) const;
778 /// getTypeOfType - GCC extension.
779 QualType getTypeOfExprType(Expr *e) const;
780 QualType getTypeOfType(QualType t) const;
782 /// getDecltypeType - C++0x decltype.
783 QualType getDecltypeType(Expr *e) const;
785 /// getUnaryTransformType - unary type transforms
786 QualType getUnaryTransformType(QualType BaseType, QualType UnderlyingType,
787 UnaryTransformType::UTTKind UKind) const;
789 /// getAutoType - C++0x deduced auto type.
790 QualType getAutoType(QualType DeducedType) const;
792 /// getAutoDeductType - C++0x deduction pattern for 'auto' type.
793 QualType getAutoDeductType() const;
795 /// getAutoRRefDeductType - C++0x deduction pattern for 'auto &&' type.
796 QualType getAutoRRefDeductType() const;
798 /// getTagDeclType - Return the unique reference to the type for the
799 /// specified TagDecl (struct/union/class/enum) decl.
800 QualType getTagDeclType(const TagDecl *Decl) const;
802 /// getSizeType - Return the unique type for "size_t" (C99 7.17), defined
803 /// in <stddef.h>. The sizeof operator requires this (C99 6.5.3.4p4).
804 CanQualType getSizeType() const;
806 /// getWCharType - In C++, this returns the unique wchar_t type. In C99, this
807 /// returns a type compatible with the type defined in <stddef.h> as defined
809 QualType getWCharType() const { return WCharTy; }
811 /// getSignedWCharType - Return the type of "signed wchar_t".
812 /// Used when in C++, as a GCC extension.
813 QualType getSignedWCharType() const;
815 /// getUnsignedWCharType - Return the type of "unsigned wchar_t".
816 /// Used when in C++, as a GCC extension.
817 QualType getUnsignedWCharType() const;
819 /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
820 /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
821 QualType getPointerDiffType() const;
823 // getCFConstantStringType - Return the C structure type used to represent
824 // constant CFStrings.
825 QualType getCFConstantStringType() const;
827 // getNSConstantStringType - Return the C structure type used to represent
828 // constant NSStrings.
829 QualType getNSConstantStringType() const;
830 /// Get the structure type used to representation NSStrings, or NULL
831 /// if it hasn't yet been built.
832 QualType getRawNSConstantStringType() const {
833 if (NSConstantStringTypeDecl)
834 return getTagDeclType(NSConstantStringTypeDecl);
837 void setNSConstantStringType(QualType T);
840 /// Get the structure type used to representation CFStrings, or NULL
841 /// if it hasn't yet been built.
842 QualType getRawCFConstantStringType() const {
843 if (CFConstantStringTypeDecl)
844 return getTagDeclType(CFConstantStringTypeDecl);
847 void setCFConstantStringType(QualType T);
849 // This setter/getter represents the ObjC type for an NSConstantString.
850 void setObjCConstantStringInterface(ObjCInterfaceDecl *Decl);
851 QualType getObjCConstantStringInterface() const {
852 return ObjCConstantStringType;
855 //// This gets the struct used to keep track of fast enumerations.
856 QualType getObjCFastEnumerationStateType() const;
858 /// Get the ObjCFastEnumerationState type, or NULL if it hasn't yet
860 QualType getRawObjCFastEnumerationStateType() const {
861 if (ObjCFastEnumerationStateTypeDecl)
862 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
866 void setObjCFastEnumerationStateType(QualType T);
868 /// \brief Set the type for the C FILE type.
869 void setFILEDecl(TypeDecl *FILEDecl) { this->FILEDecl = FILEDecl; }
871 /// \brief Retrieve the C FILE type.
872 QualType getFILEType() const {
874 return getTypeDeclType(FILEDecl);
878 /// \brief Set the type for the C jmp_buf type.
879 void setjmp_bufDecl(TypeDecl *jmp_bufDecl) {
880 this->jmp_bufDecl = jmp_bufDecl;
883 /// \brief Retrieve the C jmp_buf type.
884 QualType getjmp_bufType() const {
886 return getTypeDeclType(jmp_bufDecl);
890 /// \brief Set the type for the C sigjmp_buf type.
891 void setsigjmp_bufDecl(TypeDecl *sigjmp_bufDecl) {
892 this->sigjmp_bufDecl = sigjmp_bufDecl;
895 /// \brief Retrieve the C sigjmp_buf type.
896 QualType getsigjmp_bufType() const {
898 return getTypeDeclType(sigjmp_bufDecl);
902 /// \brief The result type of logical operations, '<', '>', '!=', etc.
903 QualType getLogicalOperationType() const {
904 return getLangOptions().CPlusPlus ? BoolTy : IntTy;
907 /// getObjCEncodingForType - Emit the ObjC type encoding for the
908 /// given type into \arg S. If \arg NameFields is specified then
909 /// record field names are also encoded.
910 void getObjCEncodingForType(QualType t, std::string &S,
911 const FieldDecl *Field=0) const;
913 void getLegacyIntegralTypeEncoding(QualType &t) const;
915 // Put the string version of type qualifiers into S.
916 void getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
917 std::string &S) const;
919 /// getObjCEncodingForFunctionDecl - Returns the encoded type for this
920 /// function. This is in the same format as Objective-C method encodings.
922 /// \returns true if an error occurred (e.g., because one of the parameter
923 /// types is incomplete), false otherwise.
924 bool getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, std::string& S);
926 /// getObjCEncodingForMethodDecl - Return the encoded type for this method
929 /// \returns true if an error occurred (e.g., because one of the parameter
930 /// types is incomplete), false otherwise.
931 bool getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, std::string &S)
934 /// getObjCEncodingForBlock - Return the encoded type for this block
936 std::string getObjCEncodingForBlock(const BlockExpr *blockExpr) const;
938 /// getObjCEncodingForPropertyDecl - Return the encoded type for
939 /// this method declaration. If non-NULL, Container must be either
940 /// an ObjCCategoryImplDecl or ObjCImplementationDecl; it should
941 /// only be NULL when getting encodings for protocol properties.
942 void getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
943 const Decl *Container,
944 std::string &S) const;
946 bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
947 ObjCProtocolDecl *rProto) const;
949 /// getObjCEncodingTypeSize returns size of type for objective-c encoding
950 /// purpose in characters.
951 CharUnits getObjCEncodingTypeSize(QualType t) const;
953 /// \brief Whether __[u]int128_t identifier is installed.
954 bool isInt128Installed() const { return IsInt128Installed; }
955 void setInt128Installed() { IsInt128Installed = true; }
957 /// This setter/getter represents the ObjC 'id' type. It is setup lazily, by
958 /// Sema. id is always a (typedef for a) pointer type, a pointer to a struct.
959 QualType getObjCIdType() const { return ObjCIdTypedefType; }
960 void setObjCIdType(QualType T);
962 void setObjCSelType(QualType T);
963 QualType getObjCSelType() const { return ObjCSelTypedefType; }
965 void setObjCProtoType(QualType QT);
966 QualType getObjCProtoType() const { return ObjCProtoType; }
968 /// This setter/getter repreents the ObjC 'Class' type. It is setup lazily, by
969 /// Sema. 'Class' is always a (typedef for a) pointer type, a pointer to a
971 QualType getObjCClassType() const { return ObjCClassTypedefType; }
972 void setObjCClassType(QualType T);
974 void setBuiltinVaListType(QualType T);
975 QualType getBuiltinVaListType() const { return BuiltinVaListType; }
977 /// getCVRQualifiedType - Returns a type with additional const,
978 /// volatile, or restrict qualifiers.
979 QualType getCVRQualifiedType(QualType T, unsigned CVR) const {
980 return getQualifiedType(T, Qualifiers::fromCVRMask(CVR));
983 /// getQualifiedType - Returns a type with additional qualifiers.
984 QualType getQualifiedType(QualType T, Qualifiers Qs) const {
985 if (!Qs.hasNonFastQualifiers())
986 return T.withFastQualifiers(Qs.getFastQualifiers());
987 QualifierCollector Qc(Qs);
988 const Type *Ptr = Qc.strip(T);
989 return getExtQualType(Ptr, Qc);
992 /// getQualifiedType - Returns a type with additional qualifiers.
993 QualType getQualifiedType(const Type *T, Qualifiers Qs) const {
994 if (!Qs.hasNonFastQualifiers())
995 return QualType(T, Qs.getFastQualifiers());
996 return getExtQualType(T, Qs);
999 /// getLifetimeQualifiedType - Returns a type with the given
1000 /// lifetime qualifier.
1001 QualType getLifetimeQualifiedType(QualType type,
1002 Qualifiers::ObjCLifetime lifetime) {
1003 assert(type.getObjCLifetime() == Qualifiers::OCL_None);
1004 assert(lifetime != Qualifiers::OCL_None);
1007 qs.addObjCLifetime(lifetime);
1008 return getQualifiedType(type, qs);
1011 DeclarationNameInfo getNameForTemplate(TemplateName Name,
1012 SourceLocation NameLoc) const;
1014 TemplateName getOverloadedTemplateName(UnresolvedSetIterator Begin,
1015 UnresolvedSetIterator End) const;
1017 TemplateName getQualifiedTemplateName(NestedNameSpecifier *NNS,
1018 bool TemplateKeyword,
1019 TemplateDecl *Template) const;
1021 TemplateName getDependentTemplateName(NestedNameSpecifier *NNS,
1022 const IdentifierInfo *Name) const;
1023 TemplateName getDependentTemplateName(NestedNameSpecifier *NNS,
1024 OverloadedOperatorKind Operator) const;
1025 TemplateName getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param,
1026 TemplateName replacement) const;
1027 TemplateName getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param,
1028 const TemplateArgument &ArgPack) const;
1030 enum GetBuiltinTypeError {
1031 GE_None, //< No error
1032 GE_Missing_stdio, //< Missing a type from <stdio.h>
1033 GE_Missing_setjmp //< Missing a type from <setjmp.h>
1036 /// GetBuiltinType - Return the type for the specified builtin. If
1037 /// IntegerConstantArgs is non-null, it is filled in with a bitmask of
1038 /// arguments to the builtin that are required to be integer constant
1040 QualType GetBuiltinType(unsigned ID, GetBuiltinTypeError &Error,
1041 unsigned *IntegerConstantArgs = 0) const;
1044 CanQualType getFromTargetType(unsigned Type) const;
1046 //===--------------------------------------------------------------------===//
1048 //===--------------------------------------------------------------------===//
1051 /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
1052 /// garbage collection attribute.
1054 Qualifiers::GC getObjCGCAttrKind(QualType Ty) const;
1056 /// areCompatibleVectorTypes - Return true if the given vector types
1057 /// are of the same unqualified type or if they are equivalent to the same
1058 /// GCC vector type, ignoring whether they are target-specific (AltiVec or
1060 bool areCompatibleVectorTypes(QualType FirstVec, QualType SecondVec);
1062 /// isObjCNSObjectType - Return true if this is an NSObject object with
1063 /// its NSObject attribute set.
1064 static bool isObjCNSObjectType(QualType Ty) {
1065 return Ty->isObjCNSObjectType();
1068 //===--------------------------------------------------------------------===//
1069 // Type Sizing and Analysis
1070 //===--------------------------------------------------------------------===//
1072 /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
1073 /// scalar floating point type.
1074 const llvm::fltSemantics &getFloatTypeSemantics(QualType T) const;
1076 /// getTypeInfo - Get the size and alignment of the specified complete type in
1078 std::pair<uint64_t, unsigned> getTypeInfo(const Type *T) const;
1079 std::pair<uint64_t, unsigned> getTypeInfo(QualType T) const {
1080 return getTypeInfo(T.getTypePtr());
1083 /// getTypeSize - Return the size of the specified type, in bits. This method
1084 /// does not work on incomplete types.
1085 uint64_t getTypeSize(QualType T) const {
1086 return getTypeInfo(T).first;
1088 uint64_t getTypeSize(const Type *T) const {
1089 return getTypeInfo(T).first;
1092 /// getCharWidth - Return the size of the character type, in bits
1093 uint64_t getCharWidth() const {
1094 return getTypeSize(CharTy);
1097 /// toCharUnitsFromBits - Convert a size in bits to a size in characters.
1098 CharUnits toCharUnitsFromBits(int64_t BitSize) const;
1100 /// toBits - Convert a size in characters to a size in bits.
1101 int64_t toBits(CharUnits CharSize) const;
1103 /// getTypeSizeInChars - Return the size of the specified type, in characters.
1104 /// This method does not work on incomplete types.
1105 CharUnits getTypeSizeInChars(QualType T) const;
1106 CharUnits getTypeSizeInChars(const Type *T) const;
1108 /// getTypeAlign - Return the ABI-specified alignment of a type, in bits.
1109 /// This method does not work on incomplete types.
1110 unsigned getTypeAlign(QualType T) const {
1111 return getTypeInfo(T).second;
1113 unsigned getTypeAlign(const Type *T) const {
1114 return getTypeInfo(T).second;
1117 /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
1118 /// characters. This method does not work on incomplete types.
1119 CharUnits getTypeAlignInChars(QualType T) const;
1120 CharUnits getTypeAlignInChars(const Type *T) const;
1122 std::pair<CharUnits, CharUnits> getTypeInfoInChars(const Type *T) const;
1123 std::pair<CharUnits, CharUnits> getTypeInfoInChars(QualType T) const;
1125 /// getPreferredTypeAlign - Return the "preferred" alignment of the specified
1126 /// type for the current target in bits. This can be different than the ABI
1127 /// alignment in cases where it is beneficial for performance to overalign
1129 unsigned getPreferredTypeAlign(const Type *T) const;
1131 /// getDeclAlign - Return a conservative estimate of the alignment of
1132 /// the specified decl. Note that bitfields do not have a valid alignment, so
1133 /// this method will assert on them.
1134 /// If @p RefAsPointee, references are treated like their underlying type
1135 /// (for alignof), else they're treated like pointers (for CodeGen).
1136 CharUnits getDeclAlign(const Decl *D, bool RefAsPointee = false) const;
1138 /// getASTRecordLayout - Get or compute information about the layout of the
1139 /// specified record (struct/union/class), which indicates its size and field
1140 /// position information.
1141 const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D) const;
1143 /// getASTObjCInterfaceLayout - Get or compute information about the
1144 /// layout of the specified Objective-C interface.
1145 const ASTRecordLayout &getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D)
1148 void DumpRecordLayout(const RecordDecl *RD, llvm::raw_ostream &OS) const;
1150 /// getASTObjCImplementationLayout - Get or compute information about
1151 /// the layout of the specified Objective-C implementation. This may
1152 /// differ from the interface if synthesized ivars are present.
1153 const ASTRecordLayout &
1154 getASTObjCImplementationLayout(const ObjCImplementationDecl *D) const;
1156 /// getKeyFunction - Get the key function for the given record decl, or NULL
1157 /// if there isn't one. The key function is, according to the Itanium C++ ABI
1160 /// ...the first non-pure virtual function that is not inline at the point
1161 /// of class definition.
1162 const CXXMethodDecl *getKeyFunction(const CXXRecordDecl *RD);
1164 bool isNearlyEmpty(const CXXRecordDecl *RD) const;
1166 MangleContext *createMangleContext();
1168 void ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
1169 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars)
1172 void DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, bool leafClass,
1173 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) const;
1175 unsigned CountNonClassIvars(const ObjCInterfaceDecl *OI) const;
1176 void CollectInheritedProtocols(const Decl *CDecl,
1177 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols);
1179 //===--------------------------------------------------------------------===//
1181 //===--------------------------------------------------------------------===//
1183 /// getCanonicalType - Return the canonical (structural) type corresponding to
1184 /// the specified potentially non-canonical type. The non-canonical version
1185 /// of a type may have many "decorated" versions of types. Decorators can
1186 /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1187 /// to be free of any of these, allowing two canonical types to be compared
1188 /// for exact equality with a simple pointer comparison.
1189 CanQualType getCanonicalType(QualType T) const {
1190 return CanQualType::CreateUnsafe(T.getCanonicalType());
1193 const Type *getCanonicalType(const Type *T) const {
1194 return T->getCanonicalTypeInternal().getTypePtr();
1197 /// getCanonicalParamType - Return the canonical parameter type
1198 /// corresponding to the specific potentially non-canonical one.
1199 /// Qualifiers are stripped off, functions are turned into function
1200 /// pointers, and arrays decay one level into pointers.
1201 CanQualType getCanonicalParamType(QualType T) const;
1203 /// \brief Determine whether the given types are equivalent.
1204 bool hasSameType(QualType T1, QualType T2) {
1205 return getCanonicalType(T1) == getCanonicalType(T2);
1208 /// \brief Returns this type as a completely-unqualified array type,
1209 /// capturing the qualifiers in Quals. This will remove the minimal amount of
1210 /// sugaring from the types, similar to the behavior of
1211 /// QualType::getUnqualifiedType().
1213 /// \param T is the qualified type, which may be an ArrayType
1215 /// \param Quals will receive the full set of qualifiers that were
1216 /// applied to the array.
1218 /// \returns if this is an array type, the completely unqualified array type
1219 /// that corresponds to it. Otherwise, returns T.getUnqualifiedType().
1220 QualType getUnqualifiedArrayType(QualType T, Qualifiers &Quals);
1222 /// \brief Determine whether the given types are equivalent after
1223 /// cvr-qualifiers have been removed.
1224 bool hasSameUnqualifiedType(QualType T1, QualType T2) {
1225 return getCanonicalType(T1).getTypePtr() ==
1226 getCanonicalType(T2).getTypePtr();
1229 bool UnwrapSimilarPointerTypes(QualType &T1, QualType &T2);
1231 /// \brief Retrieves the "canonical" nested name specifier for a
1232 /// given nested name specifier.
1234 /// The canonical nested name specifier is a nested name specifier
1235 /// that uniquely identifies a type or namespace within the type
1236 /// system. For example, given:
1241 /// template<typename T> struct X { typename T* type; };
1245 /// template<typename T> struct Y {
1246 /// typename N::S::X<T>::type member;
1250 /// Here, the nested-name-specifier for N::S::X<T>:: will be
1251 /// S::X<template-param-0-0>, since 'S' and 'X' are uniquely defined
1252 /// by declarations in the type system and the canonical type for
1253 /// the template type parameter 'T' is template-param-0-0.
1254 NestedNameSpecifier *
1255 getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const;
1257 /// \brief Retrieves the default calling convention to use for
1258 /// C++ instance methods.
1259 CallingConv getDefaultMethodCallConv();
1261 /// \brief Retrieves the canonical representation of the given
1262 /// calling convention.
1263 CallingConv getCanonicalCallConv(CallingConv CC) const {
1269 /// \brief Determines whether two calling conventions name the same
1270 /// calling convention.
1271 bool isSameCallConv(CallingConv lcc, CallingConv rcc) {
1272 return (getCanonicalCallConv(lcc) == getCanonicalCallConv(rcc));
1275 /// \brief Retrieves the "canonical" template name that refers to a
1278 /// The canonical template name is the simplest expression that can
1279 /// be used to refer to a given template. For most templates, this
1280 /// expression is just the template declaration itself. For example,
1281 /// the template std::vector can be referred to via a variety of
1282 /// names---std::vector, ::std::vector, vector (if vector is in
1283 /// scope), etc.---but all of these names map down to the same
1284 /// TemplateDecl, which is used to form the canonical template name.
1286 /// Dependent template names are more interesting. Here, the
1287 /// template name could be something like T::template apply or
1288 /// std::allocator<T>::template rebind, where the nested name
1289 /// specifier itself is dependent. In this case, the canonical
1290 /// template name uses the shortest form of the dependent
1291 /// nested-name-specifier, which itself contains all canonical
1292 /// types, values, and templates.
1293 TemplateName getCanonicalTemplateName(TemplateName Name) const;
1295 /// \brief Determine whether the given template names refer to the same
1297 bool hasSameTemplateName(TemplateName X, TemplateName Y);
1299 /// \brief Retrieve the "canonical" template argument.
1301 /// The canonical template argument is the simplest template argument
1302 /// (which may be a type, value, expression, or declaration) that
1303 /// expresses the value of the argument.
1304 TemplateArgument getCanonicalTemplateArgument(const TemplateArgument &Arg)
1307 /// Type Query functions. If the type is an instance of the specified class,
1308 /// return the Type pointer for the underlying maximally pretty type. This
1309 /// is a member of ASTContext because this may need to do some amount of
1310 /// canonicalization, e.g. to move type qualifiers into the element type.
1311 const ArrayType *getAsArrayType(QualType T) const;
1312 const ConstantArrayType *getAsConstantArrayType(QualType T) const {
1313 return dyn_cast_or_null<ConstantArrayType>(getAsArrayType(T));
1315 const VariableArrayType *getAsVariableArrayType(QualType T) const {
1316 return dyn_cast_or_null<VariableArrayType>(getAsArrayType(T));
1318 const IncompleteArrayType *getAsIncompleteArrayType(QualType T) const {
1319 return dyn_cast_or_null<IncompleteArrayType>(getAsArrayType(T));
1321 const DependentSizedArrayType *getAsDependentSizedArrayType(QualType T)
1323 return dyn_cast_or_null<DependentSizedArrayType>(getAsArrayType(T));
1326 /// getBaseElementType - Returns the innermost element type of an array type.
1327 /// For example, will return "int" for int[m][n]
1328 QualType getBaseElementType(const ArrayType *VAT) const;
1330 /// getBaseElementType - Returns the innermost element type of a type
1331 /// (which needn't actually be an array type).
1332 QualType getBaseElementType(QualType QT) const;
1334 /// getConstantArrayElementCount - Returns number of constant array elements.
1335 uint64_t getConstantArrayElementCount(const ConstantArrayType *CA) const;
1337 /// \brief Perform adjustment on the parameter type of a function.
1339 /// This routine adjusts the given parameter type @p T to the actual
1340 /// parameter type used by semantic analysis (C99 6.7.5.3p[7,8],
1341 /// C++ [dcl.fct]p3). The adjusted parameter type is returned.
1342 QualType getAdjustedParameterType(QualType T);
1344 /// \brief Retrieve the parameter type as adjusted for use in the signature
1345 /// of a function, decaying array and function types and removing top-level
1347 QualType getSignatureParameterType(QualType T);
1349 /// getArrayDecayedType - Return the properly qualified result of decaying the
1350 /// specified array type to a pointer. This operation is non-trivial when
1351 /// handling typedefs etc. The canonical type of "T" must be an array type,
1352 /// this returns a pointer to a properly qualified element of the array.
1354 /// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1355 QualType getArrayDecayedType(QualType T) const;
1357 /// getPromotedIntegerType - Returns the type that Promotable will
1358 /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
1360 QualType getPromotedIntegerType(QualType PromotableType) const;
1362 /// \brief Recurses in pointer/array types until it finds an objc retainable
1363 /// type and returns its ownership.
1364 Qualifiers::ObjCLifetime getInnerObjCOwnership(QualType T) const;
1366 /// \brief Whether this is a promotable bitfield reference according
1367 /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
1369 /// \returns the type this bit-field will promote to, or NULL if no
1370 /// promotion occurs.
1371 QualType isPromotableBitField(Expr *E) const;
1373 /// getIntegerTypeOrder - Returns the highest ranked integer type:
1374 /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1375 /// LHS < RHS, return -1.
1376 int getIntegerTypeOrder(QualType LHS, QualType RHS) const;
1378 /// getFloatingTypeOrder - Compare the rank of the two specified floating
1379 /// point types, ignoring the domain of the type (i.e. 'double' ==
1380 /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1381 /// LHS < RHS, return -1.
1382 int getFloatingTypeOrder(QualType LHS, QualType RHS) const;
1384 /// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1385 /// point or a complex type (based on typeDomain/typeSize).
1386 /// 'typeDomain' is a real floating point or complex type.
1387 /// 'typeSize' is a real floating point or complex type.
1388 QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize,
1389 QualType typeDomain) const;
1391 unsigned getTargetAddressSpace(QualType T) const {
1392 return getTargetAddressSpace(T.getQualifiers());
1395 unsigned getTargetAddressSpace(Qualifiers Q) const {
1396 return getTargetAddressSpace(Q.getAddressSpace());
1399 unsigned getTargetAddressSpace(unsigned AS) const {
1400 if (AS < LangAS::Offset || AS >= LangAS::Offset + LangAS::Count)
1403 return AddrSpaceMap[AS - LangAS::Offset];
1407 // Helper for integer ordering
1408 unsigned getIntegerRank(const Type *T) const;
1412 //===--------------------------------------------------------------------===//
1413 // Type Compatibility Predicates
1414 //===--------------------------------------------------------------------===//
1416 /// Compatibility predicates used to check assignment expressions.
1417 bool typesAreCompatible(QualType T1, QualType T2,
1418 bool CompareUnqualified = false); // C99 6.2.7p1
1420 bool propertyTypesAreCompatible(QualType, QualType);
1421 bool typesAreBlockPointerCompatible(QualType, QualType);
1423 bool isObjCIdType(QualType T) const {
1424 return T == ObjCIdTypedefType;
1426 bool isObjCClassType(QualType T) const {
1427 return T == ObjCClassTypedefType;
1429 bool isObjCSelType(QualType T) const {
1430 return T == ObjCSelTypedefType;
1432 bool QualifiedIdConformsQualifiedId(QualType LHS, QualType RHS);
1433 bool ObjCQualifiedIdTypesAreCompatible(QualType LHS, QualType RHS,
1436 bool ObjCQualifiedClassTypesAreCompatible(QualType LHS, QualType RHS);
1438 // Check the safety of assignment from LHS to RHS
1439 bool canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
1440 const ObjCObjectPointerType *RHSOPT);
1441 bool canAssignObjCInterfaces(const ObjCObjectType *LHS,
1442 const ObjCObjectType *RHS);
1443 bool canAssignObjCInterfacesInBlockPointer(
1444 const ObjCObjectPointerType *LHSOPT,
1445 const ObjCObjectPointerType *RHSOPT,
1446 bool BlockReturnType);
1447 bool areComparableObjCPointerTypes(QualType LHS, QualType RHS);
1448 QualType areCommonBaseCompatible(const ObjCObjectPointerType *LHSOPT,
1449 const ObjCObjectPointerType *RHSOPT);
1450 bool canBindObjCObjectType(QualType To, QualType From);
1452 // Functions for calculating composite types
1453 QualType mergeTypes(QualType, QualType, bool OfBlockPointer=false,
1454 bool Unqualified = false, bool BlockReturnType = false);
1455 QualType mergeFunctionTypes(QualType, QualType, bool OfBlockPointer=false,
1456 bool Unqualified = false);
1457 QualType mergeFunctionArgumentTypes(QualType, QualType,
1458 bool OfBlockPointer=false,
1459 bool Unqualified = false);
1460 QualType mergeTransparentUnionType(QualType, QualType,
1461 bool OfBlockPointer=false,
1462 bool Unqualified = false);
1464 QualType mergeObjCGCQualifiers(QualType, QualType);
1466 void ResetObjCLayout(const ObjCContainerDecl *CD) {
1467 ObjCLayouts[CD] = 0;
1470 //===--------------------------------------------------------------------===//
1471 // Integer Predicates
1472 //===--------------------------------------------------------------------===//
1474 // The width of an integer, as defined in C99 6.2.6.2. This is the number
1475 // of bits in an integer type excluding any padding bits.
1476 unsigned getIntWidth(QualType T) const;
1478 // Per C99 6.2.5p6, for every signed integer type, there is a corresponding
1479 // unsigned integer type. This method takes a signed type, and returns the
1480 // corresponding unsigned integer type.
1481 QualType getCorrespondingUnsignedType(QualType T);
1483 //===--------------------------------------------------------------------===//
1485 //===--------------------------------------------------------------------===//
1487 typedef std::vector<Type*>::iterator type_iterator;
1488 typedef std::vector<Type*>::const_iterator const_type_iterator;
1490 type_iterator types_begin() { return Types.begin(); }
1491 type_iterator types_end() { return Types.end(); }
1492 const_type_iterator types_begin() const { return Types.begin(); }
1493 const_type_iterator types_end() const { return Types.end(); }
1495 //===--------------------------------------------------------------------===//
1497 //===--------------------------------------------------------------------===//
1499 /// MakeIntValue - Make an APSInt of the appropriate width and
1500 /// signedness for the given \arg Value and integer \arg Type.
1501 llvm::APSInt MakeIntValue(uint64_t Value, QualType Type) const {
1502 llvm::APSInt Res(getIntWidth(Type),
1503 !Type->isSignedIntegerOrEnumerationType());
1508 /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
1509 ObjCImplementationDecl *getObjCImplementation(ObjCInterfaceDecl *D);
1510 /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
1511 ObjCCategoryImplDecl *getObjCImplementation(ObjCCategoryDecl *D);
1513 /// \brief returns true if there is at lease one @implementation in TU.
1514 bool AnyObjCImplementation() {
1515 return !ObjCImpls.empty();
1518 /// \brief Set the implementation of ObjCInterfaceDecl.
1519 void setObjCImplementation(ObjCInterfaceDecl *IFaceD,
1520 ObjCImplementationDecl *ImplD);
1521 /// \brief Set the implementation of ObjCCategoryDecl.
1522 void setObjCImplementation(ObjCCategoryDecl *CatD,
1523 ObjCCategoryImplDecl *ImplD);
1525 /// \brief Set the copy inialization expression of a block var decl.
1526 void setBlockVarCopyInits(VarDecl*VD, Expr* Init);
1527 /// \brief Get the copy initialization expression of VarDecl,or NULL if
1529 Expr *getBlockVarCopyInits(const VarDecl*VD);
1531 /// \brief Allocate an uninitialized TypeSourceInfo.
1533 /// The caller should initialize the memory held by TypeSourceInfo using
1534 /// the TypeLoc wrappers.
1536 /// \param T the type that will be the basis for type source info. This type
1537 /// should refer to how the declarator was written in source code, not to
1538 /// what type semantic analysis resolved the declarator to.
1540 /// \param Size the size of the type info to create, or 0 if the size
1541 /// should be calculated based on the type.
1542 TypeSourceInfo *CreateTypeSourceInfo(QualType T, unsigned Size = 0) const;
1544 /// \brief Allocate a TypeSourceInfo where all locations have been
1545 /// initialized to a given location, which defaults to the empty
1548 getTrivialTypeSourceInfo(QualType T,
1549 SourceLocation Loc = SourceLocation()) const;
1551 TypeSourceInfo *getNullTypeSourceInfo() { return &NullTypeSourceInfo; }
1553 /// \brief Add a deallocation callback that will be invoked when the
1554 /// ASTContext is destroyed.
1556 /// \brief Callback A callback function that will be invoked on destruction.
1558 /// \brief Data Pointer data that will be provided to the callback function
1559 /// when it is called.
1560 void AddDeallocation(void (*Callback)(void*), void *Data);
1562 GVALinkage GetGVALinkageForFunction(const FunctionDecl *FD);
1563 GVALinkage GetGVALinkageForVariable(const VarDecl *VD);
1565 /// \brief Determines if the decl can be CodeGen'ed or deserialized from PCH
1566 /// lazily, only when used; this is only relevant for function or file scoped
1567 /// var definitions.
1569 /// \returns true if the function/var must be CodeGen'ed/deserialized even if
1571 bool DeclMustBeEmitted(const Decl *D);
1573 //===--------------------------------------------------------------------===//
1575 //===--------------------------------------------------------------------===//
1577 /// \brief The number of implicitly-declared default constructors.
1578 static unsigned NumImplicitDefaultConstructors;
1580 /// \brief The number of implicitly-declared default constructors for
1581 /// which declarations were built.
1582 static unsigned NumImplicitDefaultConstructorsDeclared;
1584 /// \brief The number of implicitly-declared copy constructors.
1585 static unsigned NumImplicitCopyConstructors;
1587 /// \brief The number of implicitly-declared copy constructors for
1588 /// which declarations were built.
1589 static unsigned NumImplicitCopyConstructorsDeclared;
1591 /// \brief The number of implicitly-declared move constructors.
1592 static unsigned NumImplicitMoveConstructors;
1594 /// \brief The number of implicitly-declared move constructors for
1595 /// which declarations were built.
1596 static unsigned NumImplicitMoveConstructorsDeclared;
1598 /// \brief The number of implicitly-declared copy assignment operators.
1599 static unsigned NumImplicitCopyAssignmentOperators;
1601 /// \brief The number of implicitly-declared copy assignment operators for
1602 /// which declarations were built.
1603 static unsigned NumImplicitCopyAssignmentOperatorsDeclared;
1605 /// \brief The number of implicitly-declared move assignment operators.
1606 static unsigned NumImplicitMoveAssignmentOperators;
1608 /// \brief The number of implicitly-declared move assignment operators for
1609 /// which declarations were built.
1610 static unsigned NumImplicitMoveAssignmentOperatorsDeclared;
1612 /// \brief The number of implicitly-declared destructors.
1613 static unsigned NumImplicitDestructors;
1615 /// \brief The number of implicitly-declared destructors for which
1616 /// declarations were built.
1617 static unsigned NumImplicitDestructorsDeclared;
1620 ASTContext(const ASTContext&); // DO NOT IMPLEMENT
1621 void operator=(const ASTContext&); // DO NOT IMPLEMENT
1623 void InitBuiltinTypes();
1624 void InitBuiltinType(CanQualType &R, BuiltinType::Kind K);
1626 // Return the ObjC type encoding for a given type.
1627 void getObjCEncodingForTypeImpl(QualType t, std::string &S,
1628 bool ExpandPointedToStructures,
1629 bool ExpandStructures,
1630 const FieldDecl *Field,
1631 bool OutermostType = false,
1632 bool EncodingProperty = false,
1633 bool StructField = false) const;
1635 // Adds the encoding of the structure's members.
1636 void getObjCEncodingForStructureImpl(RecordDecl *RD, std::string &S,
1637 const FieldDecl *Field,
1638 bool includeVBases = true) const;
1640 const ASTRecordLayout &
1641 getObjCLayout(const ObjCInterfaceDecl *D,
1642 const ObjCImplementationDecl *Impl) const;
1645 /// \brief A set of deallocations that should be performed when the
1646 /// ASTContext is destroyed.
1647 llvm::SmallVector<std::pair<void (*)(void*), void *>, 16> Deallocations;
1649 // FIXME: This currently contains the set of StoredDeclMaps used
1650 // by DeclContext objects. This probably should not be in ASTContext,
1651 // but we include it here so that ASTContext can quickly deallocate them.
1652 llvm::PointerIntPair<StoredDeclsMap*,1> LastSDM;
1654 /// \brief A counter used to uniquely identify "blocks".
1655 mutable unsigned int UniqueBlockByRefTypeID;
1657 friend class DeclContext;
1658 friend class DeclarationNameTable;
1659 void ReleaseDeclContextMaps();
1662 /// @brief Utility function for constructing a nullary selector.
1663 static inline Selector GetNullarySelector(llvm::StringRef name, ASTContext& Ctx) {
1664 IdentifierInfo* II = &Ctx.Idents.get(name);
1665 return Ctx.Selectors.getSelector(0, &II);
1668 /// @brief Utility function for constructing an unary selector.
1669 static inline Selector GetUnarySelector(llvm::StringRef name, ASTContext& Ctx) {
1670 IdentifierInfo* II = &Ctx.Idents.get(name);
1671 return Ctx.Selectors.getSelector(1, &II);
1674 } // end namespace clang
1676 // operator new and delete aren't allowed inside namespaces.
1677 // The throw specifications are mandated by the standard.
1678 /// @brief Placement new for using the ASTContext's allocator.
1680 /// This placement form of operator new uses the ASTContext's allocator for
1681 /// obtaining memory. It is a non-throwing new, which means that it returns
1682 /// null on error. (If that is what the allocator does. The current does, so if
1683 /// this ever changes, this operator will have to be changed, too.)
1684 /// Usage looks like this (assuming there's an ASTContext 'Context' in scope):
1686 /// // Default alignment (8)
1687 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
1688 /// // Specific alignment
1689 /// IntegerLiteral *Ex2 = new (Context, 4) IntegerLiteral(arguments);
1691 /// Please note that you cannot use delete on the pointer; it must be
1692 /// deallocated using an explicit destructor call followed by
1693 /// @c Context.Deallocate(Ptr).
1695 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
1696 /// @param C The ASTContext that provides the allocator.
1697 /// @param Alignment The alignment of the allocated memory (if the underlying
1698 /// allocator supports it).
1699 /// @return The allocated memory. Could be NULL.
1700 inline void *operator new(size_t Bytes, const clang::ASTContext &C,
1701 size_t Alignment) throw () {
1702 return C.Allocate(Bytes, Alignment);
1704 /// @brief Placement delete companion to the new above.
1706 /// This operator is just a companion to the new above. There is no way of
1707 /// invoking it directly; see the new operator for more details. This operator
1708 /// is called implicitly by the compiler if a placement new expression using
1709 /// the ASTContext throws in the object constructor.
1710 inline void operator delete(void *Ptr, const clang::ASTContext &C, size_t)
1715 /// This placement form of operator new[] uses the ASTContext's allocator for
1716 /// obtaining memory. It is a non-throwing new[], which means that it returns
1718 /// Usage looks like this (assuming there's an ASTContext 'Context' in scope):
1720 /// // Default alignment (8)
1721 /// char *data = new (Context) char[10];
1722 /// // Specific alignment
1723 /// char *data = new (Context, 4) char[10];
1725 /// Please note that you cannot use delete on the pointer; it must be
1726 /// deallocated using an explicit destructor call followed by
1727 /// @c Context.Deallocate(Ptr).
1729 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
1730 /// @param C The ASTContext that provides the allocator.
1731 /// @param Alignment The alignment of the allocated memory (if the underlying
1732 /// allocator supports it).
1733 /// @return The allocated memory. Could be NULL.
1734 inline void *operator new[](size_t Bytes, const clang::ASTContext& C,
1735 size_t Alignment = 8) throw () {
1736 return C.Allocate(Bytes, Alignment);
1739 /// @brief Placement delete[] companion to the new[] above.
1741 /// This operator is just a companion to the new[] above. There is no way of
1742 /// invoking it directly; see the new[] operator for more details. This operator
1743 /// is called implicitly by the compiler if a placement new[] expression using
1744 /// the ASTContext throws in the object constructor.
1745 inline void operator delete[](void *Ptr, const clang::ASTContext &C, size_t)