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/IdentifierTable.h"
18 #include "clang/Basic/LangOptions.h"
19 #include "clang/Basic/OperatorKinds.h"
20 #include "clang/Basic/PartialDiagnostic.h"
21 #include "clang/AST/Decl.h"
22 #include "clang/AST/NestedNameSpecifier.h"
23 #include "clang/AST/PrettyPrinter.h"
24 #include "clang/AST/TemplateName.h"
25 #include "clang/AST/Type.h"
26 #include "clang/AST/CanonicalType.h"
27 #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"
42 class ASTRecordLayout;
47 class ExternalASTSource;
48 class IdentifierTable;
60 class ObjCIvarRefExpr;
61 class ObjCPropertyDecl;
65 class TemplateTemplateParmDecl;
66 class TemplateTypeParmDecl;
67 class TranslationUnitDecl;
71 class UsingShadowDecl;
72 class UnresolvedSetIterator;
74 namespace Builtin { class Context; }
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.
79 ASTContext &this_() { return *this; }
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;
110 llvm::FoldingSet<QualifiedTemplateName> QualifiedTemplateNames;
111 llvm::FoldingSet<DependentTemplateName> DependentTemplateNames;
113 /// \brief The set of nested name specifiers.
115 /// This set is managed by the NestedNameSpecifier class.
116 llvm::FoldingSet<NestedNameSpecifier> NestedNameSpecifiers;
117 NestedNameSpecifier *GlobalNestedNameSpecifier;
118 friend class NestedNameSpecifier;
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;
125 /// KeyFunctions - A cache mapping from CXXRecordDecls to key functions.
126 llvm::DenseMap<const CXXRecordDecl*, const CXXMethodDecl*> KeyFunctions;
128 /// \brief Mapping from ObjCContainers to their ObjCImplementations.
129 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*> ObjCImpls;
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;
137 CanonicalTemplateTemplateParm(TemplateTemplateParmDecl *Parm)
140 TemplateTemplateParmDecl *getParam() const { return Parm; }
142 void Profile(llvm::FoldingSetNodeID &ID) { Profile(ID, Parm); }
144 static void Profile(llvm::FoldingSetNodeID &ID,
145 TemplateTemplateParmDecl *Parm);
147 llvm::FoldingSet<CanonicalTemplateTemplateParm> CanonTemplateTemplateParms;
149 TemplateTemplateParmDecl *getCanonicalTemplateTemplateParmDecl(
150 TemplateTemplateParmDecl *TTP);
152 /// \brief Whether __[u]int128_t identifier is installed.
153 bool IsInt128Installed;
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;
160 /// ObjCIdType - a pseudo built-in typedef type (set by Sema).
161 QualType ObjCIdTypedefType;
163 /// ObjCSelType - another pseudo built-in typedef type (set by Sema).
164 QualType ObjCSelTypedefType;
166 /// ObjCProtoType - another pseudo built-in typedef type (set by Sema).
167 QualType ObjCProtoType;
168 const RecordType *ProtoStructType;
170 /// ObjCClassType - another pseudo built-in typedef type (set by Sema).
171 QualType ObjCClassTypedefType;
173 QualType ObjCConstantStringType;
174 RecordDecl *CFConstantStringTypeDecl;
176 RecordDecl *NSConstantStringTypeDecl;
178 RecordDecl *ObjCFastEnumerationStateTypeDecl;
180 /// \brief The type for the C FILE type.
183 /// \brief The type for the C jmp_buf type.
184 TypeDecl *jmp_bufDecl;
186 /// \brief The type for the C sigjmp_buf type.
187 TypeDecl *sigjmp_bufDecl;
189 /// \brief Type for the Block descriptor for Blocks CodeGen.
190 RecordDecl *BlockDescriptorType;
192 /// \brief Type for the Block descriptor for Blocks CodeGen.
193 RecordDecl *BlockDescriptorExtendedType;
195 TypeSourceInfo NullTypeSourceInfo;
197 /// \brief Keeps track of all declaration attributes.
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;
203 /// \brief Keeps track of the static data member templates from which
204 /// static data members of class template specializations were instantiated.
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).
211 /// Given the following example:
214 /// template<typename T>
219 /// template<typename T>
220 /// T X<T>::value = T(17);
222 /// int *x = &X<int>::value;
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;
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.
238 /// template<typename T>
243 /// template<typename T>
244 /// struct B : A<T> {
248 /// template struct B<int>;
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;
255 llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>
256 InstantiatedFromUsingShadowDecl;
258 llvm::DenseMap<FieldDecl *, FieldDecl *> InstantiatedFromUnnamedFieldDecl;
260 /// \brief Mapping that stores the methods overridden by a given C++
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;
269 TranslationUnitDecl *TUDecl;
271 /// SourceMgr - The associated SourceManager object.
272 SourceManager &SourceMgr;
274 /// LangOpts - The language options used to create the AST associated with
275 /// this ASTContext object.
276 LangOptions LangOpts;
278 /// \brief The allocator used to create AST objects.
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;
284 /// \brief Allocator for partial diagnostics.
285 PartialDiagnostic::StorageAllocator DiagAllocator;
287 /// \brief The current C++ ABI.
288 llvm::OwningPtr<CXXABI> ABI;
289 CXXABI *createCXXABI(const TargetInfo &T);
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;
300 // Typedefs which may be provided defining the structure of Objective-C
302 QualType ObjCIdRedefinitionType;
303 QualType ObjCClassRedefinitionType;
304 QualType ObjCSelRedefinitionType;
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);
311 void Deallocate(void *Ptr) { }
313 PartialDiagnostic::StorageAllocator &getDiagAllocator() {
314 return DiagAllocator;
317 const LangOptions& getLangOptions() const { return LangOpts; }
319 FullSourceLoc getFullLoc(SourceLocation Loc) const {
320 return FullSourceLoc(Loc,SourceMgr);
323 /// \brief Retrieve the attributes for the given declaration.
324 AttrVec& getDeclAttrs(const Decl *D);
326 /// \brief Erase the attributes corresponding to the given declaration.
327 void eraseDeclAttrs(const Decl *D);
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(
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());
341 /// \brief If the given using decl is an instantiation of a
342 /// (possibly unresolved) using decl from a template instantiation,
344 NamedDecl *getInstantiatedFromUsingDecl(UsingDecl *Inst);
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);
350 void setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst,
351 UsingShadowDecl *Pattern);
352 UsingShadowDecl *getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst);
354 FieldDecl *getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field);
356 void setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, FieldDecl *Tmpl);
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;
363 overridden_cxx_method_iterator
364 overridden_methods_end(const CXXMethodDecl *Method) const;
366 unsigned overridden_methods_size(const CXXMethodDecl *Method) const;
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);
373 TranslationUnitDecl *getTranslationUnitDecl() const { return TUDecl; }
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;
394 ASTContext(const LangOptions& LOpts, SourceManager &SM, const TargetInfo &t,
395 IdentifierTable &idents, SelectorTable &sels,
396 Builtin::Context &builtins,
397 unsigned size_reserve);
401 /// \brief Attach an external AST source to the AST context.
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);
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(); }
412 void PrintStats() const;
413 const std::vector<Type*>& getTypes() const { return Types; }
415 //===--------------------------------------------------------------------===//
417 //===--------------------------------------------------------------------===//
420 /// getExtQualType - Return a type with extended qualifiers.
421 QualType getExtQualType(const Type *Base, Qualifiers Quals);
423 QualType getTypeDeclTypeSlow(const TypeDecl *Decl);
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
431 QualType getAddrSpaceQualType(QualType T, unsigned AddressSpace);
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);
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);
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);
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'.
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(); }
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
461 QualType getNoReturnType(QualType T, bool AddNoReturn = true);
463 /// getCallConvType - Adds the specified calling convention attribute to
464 /// the given type, which must be a FunctionType or a pointer to an
466 QualType getCallConvType(QualType T, CallingConv CallConv);
468 /// getRegParmType - Sets the specified regparm attribute to
469 /// the given type, which must be a FunctionType or a pointer to an
471 QualType getRegParmType(QualType T, unsigned RegParm);
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));
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));
487 /// getBlockPointerType - Return the uniqued reference to the type for a block
488 /// of the specified type.
489 QualType getBlockPointerType(QualType T);
491 /// This gets the struct used to keep track of the descriptor for pointer to
493 QualType getBlockDescriptorType();
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);
504 /// This gets the struct used to keep track of the extended descriptor for
505 /// pointer to blocks.
506 QualType getBlockDescriptorExtendedType();
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
512 QualType getRawBlockdescriptorExtendedType() {
513 if (BlockDescriptorExtendedType)
514 return getTagDeclType(BlockDescriptorExtendedType);
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);
523 /// This builds the struct used for __block variables.
524 QualType BuildByRefType(llvm::StringRef DeclName, QualType Ty);
526 /// Returns true iff we need copy/dispose helpers for the given type.
527 bool BlockRequiresCopying(QualType Ty);
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);
533 /// getRValueReferenceType - Return the uniqued reference to the type for an
534 /// rvalue reference to the specified type.
535 QualType getRValueReferenceType(QualType T);
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);
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);
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);
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);
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);
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);
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
578 QualType getExtVectorType(QualType VectorType, unsigned NumElts);
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,
586 SourceLocation AttrLoc);
588 /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
590 QualType getFunctionNoProtoType(QualType ResultTy,
591 const FunctionType::ExtInfo &Info);
593 QualType getFunctionNoProtoType(QualType ResultTy) {
594 return getFunctionNoProtoType(ResultTy, FunctionType::ExtInfo());
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);
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);
614 assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl");
615 Decl->TypeForDecl = PrevDecl->TypeForDecl;
616 return QualType(PrevDecl->TypeForDecl, 0);
619 return getTypeDeclTypeSlow(Decl);
622 /// getTypedefType - Return the unique reference to the type for the
623 /// specified typename decl.
624 QualType getTypedefType(const TypedefDecl *Decl, QualType Canon = QualType());
626 QualType getRecordType(const RecordDecl *Decl);
628 QualType getEnumType(const EnumDecl *Decl);
630 QualType getInjectedClassNameType(CXXRecordDecl *Decl, QualType TST);
632 QualType getSubstTemplateTypeParmType(const TemplateTypeParmType *Replaced,
633 QualType Replacement);
635 QualType getTemplateTypeParmType(unsigned Depth, unsigned Index,
637 IdentifierInfo *Name = 0);
639 QualType getTemplateSpecializationType(TemplateName T,
640 const TemplateArgument *Args,
642 QualType Canon = QualType());
644 QualType getCanonicalTemplateSpecializationType(TemplateName T,
645 const TemplateArgument *Args,
648 QualType getTemplateSpecializationType(TemplateName T,
649 const TemplateArgumentListInfo &Args,
650 QualType Canon = QualType());
653 getTemplateSpecializationTypeInfo(TemplateName T, SourceLocation TLoc,
654 const TemplateArgumentListInfo &Args,
655 QualType Canon = QualType());
657 QualType getElaboratedType(ElaboratedTypeKeyword Keyword,
658 NestedNameSpecifier *NNS,
660 QualType getDependentNameType(ElaboratedTypeKeyword Keyword,
661 NestedNameSpecifier *NNS,
662 const IdentifierInfo *Name,
663 QualType Canon = QualType());
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,
673 const TemplateArgument *Args);
675 QualType getObjCInterfaceType(const ObjCInterfaceDecl *Decl);
677 QualType getObjCObjectType(QualType Base,
678 ObjCProtocolDecl * const *Protocols,
679 unsigned NumProtocols);
681 /// getObjCObjectPointerType - Return a ObjCObjectPointerType type
682 /// for the given ObjCObjectType.
683 QualType getObjCObjectPointerType(QualType OIT);
685 /// getTypeOfType - GCC extension.
686 QualType getTypeOfExprType(Expr *e);
687 QualType getTypeOfType(QualType t);
689 /// getDecltypeType - C++0x decltype.
690 QualType getDecltypeType(Expr *e);
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);
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;
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
703 QualType getWCharType() const { return WCharTy; }
705 /// getSignedWCharType - Return the type of "signed wchar_t".
706 /// Used when in C++, as a GCC extension.
707 QualType getSignedWCharType() const;
709 /// getUnsignedWCharType - Return the type of "unsigned wchar_t".
710 /// Used when in C++, as a GCC extension.
711 QualType getUnsignedWCharType() const;
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;
717 // getCFConstantStringType - Return the C structure type used to represent
718 // constant CFStrings.
719 QualType getCFConstantStringType();
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);
731 void setNSConstantStringType(QualType T);
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);
741 void setCFConstantStringType(QualType T);
743 // This setter/getter represents the ObjC type for an NSConstantString.
744 void setObjCConstantStringInterface(ObjCInterfaceDecl *Decl);
745 QualType getObjCConstantStringInterface() const {
746 return ObjCConstantStringType;
749 //// This gets the struct used to keep track of fast enumerations.
750 QualType getObjCFastEnumerationStateType();
752 /// Get the ObjCFastEnumerationState type, or NULL if it hasn't yet
754 QualType getRawObjCFastEnumerationStateType() {
755 if (ObjCFastEnumerationStateTypeDecl)
756 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
760 void setObjCFastEnumerationStateType(QualType T);
762 /// \brief Set the type for the C FILE type.
763 void setFILEDecl(TypeDecl *FILEDecl) { this->FILEDecl = FILEDecl; }
765 /// \brief Retrieve the C FILE type.
766 QualType getFILEType() {
768 return getTypeDeclType(FILEDecl);
772 /// \brief Set the type for the C jmp_buf type.
773 void setjmp_bufDecl(TypeDecl *jmp_bufDecl) {
774 this->jmp_bufDecl = jmp_bufDecl;
777 /// \brief Retrieve the C jmp_buf type.
778 QualType getjmp_bufType() {
780 return getTypeDeclType(jmp_bufDecl);
784 /// \brief Set the type for the C sigjmp_buf type.
785 void setsigjmp_bufDecl(TypeDecl *sigjmp_bufDecl) {
786 this->sigjmp_bufDecl = sigjmp_bufDecl;
789 /// \brief Retrieve the C sigjmp_buf type.
790 QualType getsigjmp_bufType() {
792 return getTypeDeclType(sigjmp_bufDecl);
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);
802 void getLegacyIntegralTypeEncoding(QualType &t) const;
804 // Put the string version of type qualifiers into S.
805 void getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
806 std::string &S) const;
808 /// getObjCEncodingForMethodDecl - Return the encoded type for this method
810 void getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, std::string &S);
812 /// getObjCEncodingForBlockDecl - Return the encoded type for this block
814 void getObjCEncodingForBlock(const BlockExpr *Expr, std::string& S);
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,
824 bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
825 ObjCProtocolDecl *rProto);
827 /// getObjCEncodingTypeSize returns size of type for objective-c encoding
828 /// purpose in characters.
829 CharUnits getObjCEncodingTypeSize(QualType t);
831 /// \brief Whether __[u]int128_t identifier is installed.
832 bool isInt128Installed() const { return IsInt128Installed; }
833 void setInt128Installed() { IsInt128Installed = true; }
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);
840 void setObjCSelType(QualType T);
841 QualType getObjCSelType() const { return ObjCSelTypedefType; }
843 void setObjCProtoType(QualType QT);
844 QualType getObjCProtoType() const { return ObjCProtoType; }
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
849 QualType getObjCClassType() const { return ObjCClassTypedefType; }
850 void setObjCClassType(QualType T);
852 void setBuiltinVaListType(QualType T);
853 QualType getBuiltinVaListType() const { return BuiltinVaListType; }
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));
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);
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);
877 DeclarationNameInfo getNameForTemplate(TemplateName Name,
878 SourceLocation NameLoc);
880 TemplateName getOverloadedTemplateName(UnresolvedSetIterator Begin,
881 UnresolvedSetIterator End);
883 TemplateName getQualifiedTemplateName(NestedNameSpecifier *NNS,
884 bool TemplateKeyword,
885 TemplateDecl *Template);
887 TemplateName getDependentTemplateName(NestedNameSpecifier *NNS,
888 const IdentifierInfo *Name);
889 TemplateName getDependentTemplateName(NestedNameSpecifier *NNS,
890 OverloadedOperatorKind Operator);
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>
898 /// GetBuiltinType - Return the type for the specified builtin.
899 QualType GetBuiltinType(unsigned ID, GetBuiltinTypeError &Error);
902 CanQualType getFromTargetType(unsigned Type) const;
904 //===--------------------------------------------------------------------===//
906 //===--------------------------------------------------------------------===//
909 /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
910 /// garbage collection attribute.
912 Qualifiers::GC getObjCGCAttrKind(const QualType &Ty) const;
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);
919 /// isObjCNSObjectType - Return true if this is an NSObject object with
920 /// its NSObject attribute set.
921 bool isObjCNSObjectType(QualType Ty) const;
923 //===--------------------------------------------------------------------===//
924 // Type Sizing and Analysis
925 //===--------------------------------------------------------------------===//
927 /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
928 /// scalar floating point type.
929 const llvm::fltSemantics &getFloatTypeSemantics(QualType T) const;
931 /// getTypeInfo - Get the size and alignment of the specified complete type in
933 std::pair<uint64_t, unsigned> getTypeInfo(const Type *T);
934 std::pair<uint64_t, unsigned> getTypeInfo(QualType T) {
935 return getTypeInfo(T.getTypePtr());
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;
943 uint64_t getTypeSize(const Type *T) {
944 return getTypeInfo(T).first;
947 /// getCharWidth - Return the size of the character type, in bits
948 uint64_t getCharWidth() {
949 return getTypeSize(CharTy);
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);
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;
962 unsigned getTypeAlign(const Type *T) {
963 return getTypeInfo(T).second;
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);
971 std::pair<CharUnits, CharUnits> getTypeInfoInChars(const Type *T);
972 std::pair<CharUnits, CharUnits> getTypeInfoInChars(QualType T);
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
978 unsigned getPreferredTypeAlign(const Type *T);
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);
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);
992 /// getASTObjCInterfaceLayout - Get or compute information about the
993 /// layout of the specified Objective-C interface.
994 const ASTRecordLayout &getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D);
996 void DumpRecordLayout(const RecordDecl *RD, llvm::raw_ostream &OS);
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);
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
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);
1012 void ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
1013 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars);
1015 void DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, bool leafClass,
1016 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars);
1018 unsigned CountNonClassIvars(const ObjCInterfaceDecl *OI);
1019 void CollectInheritedProtocols(const Decl *CDecl,
1020 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols);
1022 //===--------------------------------------------------------------------===//
1024 //===--------------------------------------------------------------------===//
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();
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);
1043 /// \brief Determine whether the given types are equivalent.
1044 bool hasSameType(QualType T1, QualType T2) {
1045 return getCanonicalType(T1) == getCanonicalType(T2);
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().
1053 /// \param T is the qualified type, which may be an ArrayType
1055 /// \param Quals will receive the full set of qualifiers that were
1056 /// applied to the array.
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);
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);
1069 QualType UnqualT1 = getUnqualifiedArrayType(CT1, Quals);
1070 QualType UnqualT2 = getUnqualifiedArrayType(CT2, Quals);
1071 return UnqualT1 == UnqualT2;
1074 bool UnwrapSimilarPointerTypes(QualType &T1, QualType &T2);
1076 /// \brief Retrieves the "canonical" nested name specifier for a
1077 /// given nested name specifier.
1079 /// The canonical nested name specifier is a nested name specifier
1080 /// that uniquely identifies a type or namespace within the type
1081 /// system. For example, given:
1086 /// template<typename T> struct X { typename T* type; };
1090 /// template<typename T> struct Y {
1091 /// typename N::S::X<T>::type member;
1095 /// Here, the nested-name-specifier for N::S::X<T>:: will be
1096 /// S::X<template-param-0-0>, since 'S' and 'X' are uniquely defined
1097 /// by declarations in the type system and the canonical type for
1098 /// the template type parameter 'T' is template-param-0-0.
1099 NestedNameSpecifier *
1100 getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS);
1102 /// \brief Retrieves the canonical representation of the given
1103 /// calling convention.
1104 CallingConv getCanonicalCallConv(CallingConv CC) {
1110 /// \brief Determines whether two calling conventions name the same
1111 /// calling convention.
1112 bool isSameCallConv(CallingConv lcc, CallingConv rcc) {
1113 return (getCanonicalCallConv(lcc) == getCanonicalCallConv(rcc));
1116 /// \brief Retrieves the "canonical" template name that refers to a
1119 /// The canonical template name is the simplest expression that can
1120 /// be used to refer to a given template. For most templates, this
1121 /// expression is just the template declaration itself. For example,
1122 /// the template std::vector can be referred to via a variety of
1123 /// names---std::vector, ::std::vector, vector (if vector is in
1124 /// scope), etc.---but all of these names map down to the same
1125 /// TemplateDecl, which is used to form the canonical template name.
1127 /// Dependent template names are more interesting. Here, the
1128 /// template name could be something like T::template apply or
1129 /// std::allocator<T>::template rebind, where the nested name
1130 /// specifier itself is dependent. In this case, the canonical
1131 /// template name uses the shortest form of the dependent
1132 /// nested-name-specifier, which itself contains all canonical
1133 /// types, values, and templates.
1134 TemplateName getCanonicalTemplateName(TemplateName Name);
1136 /// \brief Determine whether the given template names refer to the same
1138 bool hasSameTemplateName(TemplateName X, TemplateName Y);
1140 /// \brief Retrieve the "canonical" template argument.
1142 /// The canonical template argument is the simplest template argument
1143 /// (which may be a type, value, expression, or declaration) that
1144 /// expresses the value of the argument.
1145 TemplateArgument getCanonicalTemplateArgument(const TemplateArgument &Arg);
1147 /// Type Query functions. If the type is an instance of the specified class,
1148 /// return the Type pointer for the underlying maximally pretty type. This
1149 /// is a member of ASTContext because this may need to do some amount of
1150 /// canonicalization, e.g. to move type qualifiers into the element type.
1151 const ArrayType *getAsArrayType(QualType T);
1152 const ConstantArrayType *getAsConstantArrayType(QualType T) {
1153 return dyn_cast_or_null<ConstantArrayType>(getAsArrayType(T));
1155 const VariableArrayType *getAsVariableArrayType(QualType T) {
1156 return dyn_cast_or_null<VariableArrayType>(getAsArrayType(T));
1158 const IncompleteArrayType *getAsIncompleteArrayType(QualType T) {
1159 return dyn_cast_or_null<IncompleteArrayType>(getAsArrayType(T));
1161 const DependentSizedArrayType *getAsDependentSizedArrayType(QualType T) {
1162 return dyn_cast_or_null<DependentSizedArrayType>(getAsArrayType(T));
1165 /// getBaseElementType - Returns the innermost element type of an array type.
1166 /// For example, will return "int" for int[m][n]
1167 QualType getBaseElementType(const ArrayType *VAT);
1169 /// getBaseElementType - Returns the innermost element type of a type
1170 /// (which needn't actually be an array type).
1171 QualType getBaseElementType(QualType QT);
1173 /// getConstantArrayElementCount - Returns number of constant array elements.
1174 uint64_t getConstantArrayElementCount(const ConstantArrayType *CA) const;
1176 /// getArrayDecayedType - Return the properly qualified result of decaying the
1177 /// specified array type to a pointer. This operation is non-trivial when
1178 /// handling typedefs etc. The canonical type of "T" must be an array type,
1179 /// this returns a pointer to a properly qualified element of the array.
1181 /// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1182 QualType getArrayDecayedType(QualType T);
1184 /// getPromotedIntegerType - Returns the type that Promotable will
1185 /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
1187 QualType getPromotedIntegerType(QualType PromotableType);
1189 /// \brief Whether this is a promotable bitfield reference according
1190 /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
1192 /// \returns the type this bit-field will promote to, or NULL if no
1193 /// promotion occurs.
1194 QualType isPromotableBitField(Expr *E);
1196 /// getIntegerTypeOrder - Returns the highest ranked integer type:
1197 /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1198 /// LHS < RHS, return -1.
1199 int getIntegerTypeOrder(QualType LHS, QualType RHS);
1201 /// getFloatingTypeOrder - Compare the rank of the two specified floating
1202 /// point types, ignoring the domain of the type (i.e. 'double' ==
1203 /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1204 /// LHS < RHS, return -1.
1205 int getFloatingTypeOrder(QualType LHS, QualType RHS);
1207 /// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1208 /// point or a complex type (based on typeDomain/typeSize).
1209 /// 'typeDomain' is a real floating point or complex type.
1210 /// 'typeSize' is a real floating point or complex type.
1211 QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize,
1212 QualType typeDomain) const;
1215 // Helper for integer ordering
1216 unsigned getIntegerRank(Type* T);
1220 //===--------------------------------------------------------------------===//
1221 // Type Compatibility Predicates
1222 //===--------------------------------------------------------------------===//
1224 /// Compatibility predicates used to check assignment expressions.
1225 bool typesAreCompatible(QualType T1, QualType T2,
1226 bool CompareUnqualified = false); // C99 6.2.7p1
1228 bool typesAreBlockPointerCompatible(QualType, QualType);
1230 bool isObjCIdType(QualType T) const {
1231 return T == ObjCIdTypedefType;
1233 bool isObjCClassType(QualType T) const {
1234 return T == ObjCClassTypedefType;
1236 bool isObjCSelType(QualType T) const {
1237 return T == ObjCSelTypedefType;
1239 bool QualifiedIdConformsQualifiedId(QualType LHS, QualType RHS);
1240 bool ObjCQualifiedIdTypesAreCompatible(QualType LHS, QualType RHS,
1243 bool ObjCQualifiedClassTypesAreCompatible(QualType LHS, QualType RHS);
1245 // Check the safety of assignment from LHS to RHS
1246 bool canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
1247 const ObjCObjectPointerType *RHSOPT);
1248 bool canAssignObjCInterfaces(const ObjCObjectType *LHS,
1249 const ObjCObjectType *RHS);
1250 bool canAssignObjCInterfacesInBlockPointer(
1251 const ObjCObjectPointerType *LHSOPT,
1252 const ObjCObjectPointerType *RHSOPT);
1253 bool areComparableObjCPointerTypes(QualType LHS, QualType RHS);
1254 QualType areCommonBaseCompatible(const ObjCObjectPointerType *LHSOPT,
1255 const ObjCObjectPointerType *RHSOPT);
1256 bool canBindObjCObjectType(QualType To, QualType From);
1258 // Functions for calculating composite types
1259 QualType mergeTypes(QualType, QualType, bool OfBlockPointer=false,
1260 bool Unqualified = false);
1261 QualType mergeFunctionTypes(QualType, QualType, bool OfBlockPointer=false,
1262 bool Unqualified = false);
1264 QualType mergeObjCGCQualifiers(QualType, QualType);
1266 /// UsualArithmeticConversionsType - handles the various conversions
1267 /// that are common to binary operators (C99 6.3.1.8, C++ [expr]p9)
1268 /// and returns the result type of that conversion.
1269 QualType UsualArithmeticConversionsType(QualType lhs, QualType rhs);
1271 void ResetObjCLayout(const ObjCContainerDecl *CD) {
1272 ObjCLayouts[CD] = 0;
1275 //===--------------------------------------------------------------------===//
1276 // Integer Predicates
1277 //===--------------------------------------------------------------------===//
1279 // The width of an integer, as defined in C99 6.2.6.2. This is the number
1280 // of bits in an integer type excluding any padding bits.
1281 unsigned getIntWidth(QualType T);
1283 // Per C99 6.2.5p6, for every signed integer type, there is a corresponding
1284 // unsigned integer type. This method takes a signed type, and returns the
1285 // corresponding unsigned integer type.
1286 QualType getCorrespondingUnsignedType(QualType T);
1288 //===--------------------------------------------------------------------===//
1290 //===--------------------------------------------------------------------===//
1292 typedef std::vector<Type*>::iterator type_iterator;
1293 typedef std::vector<Type*>::const_iterator const_type_iterator;
1295 type_iterator types_begin() { return Types.begin(); }
1296 type_iterator types_end() { return Types.end(); }
1297 const_type_iterator types_begin() const { return Types.begin(); }
1298 const_type_iterator types_end() const { return Types.end(); }
1300 //===--------------------------------------------------------------------===//
1302 //===--------------------------------------------------------------------===//
1304 /// MakeIntValue - Make an APSInt of the appropriate width and
1305 /// signedness for the given \arg Value and integer \arg Type.
1306 llvm::APSInt MakeIntValue(uint64_t Value, QualType Type) {
1307 llvm::APSInt Res(getIntWidth(Type), !Type->isSignedIntegerType());
1312 /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
1313 ObjCImplementationDecl *getObjCImplementation(ObjCInterfaceDecl *D);
1314 /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
1315 ObjCCategoryImplDecl *getObjCImplementation(ObjCCategoryDecl *D);
1317 /// \brief Set the implementation of ObjCInterfaceDecl.
1318 void setObjCImplementation(ObjCInterfaceDecl *IFaceD,
1319 ObjCImplementationDecl *ImplD);
1320 /// \brief Set the implementation of ObjCCategoryDecl.
1321 void setObjCImplementation(ObjCCategoryDecl *CatD,
1322 ObjCCategoryImplDecl *ImplD);
1324 /// \brief Allocate an uninitialized TypeSourceInfo.
1326 /// The caller should initialize the memory held by TypeSourceInfo using
1327 /// the TypeLoc wrappers.
1329 /// \param T the type that will be the basis for type source info. This type
1330 /// should refer to how the declarator was written in source code, not to
1331 /// what type semantic analysis resolved the declarator to.
1333 /// \param Size the size of the type info to create, or 0 if the size
1334 /// should be calculated based on the type.
1335 TypeSourceInfo *CreateTypeSourceInfo(QualType T, unsigned Size = 0);
1337 /// \brief Allocate a TypeSourceInfo where all locations have been
1338 /// initialized to a given location, which defaults to the empty
1341 getTrivialTypeSourceInfo(QualType T, SourceLocation Loc = SourceLocation());
1343 TypeSourceInfo *getNullTypeSourceInfo() { return &NullTypeSourceInfo; }
1345 /// \brief Add a deallocation callback that will be invoked when the
1346 /// ASTContext is destroyed.
1348 /// \brief Callback A callback function that will be invoked on destruction.
1350 /// \brief Data Pointer data that will be provided to the callback function
1351 /// when it is called.
1352 void AddDeallocation(void (*Callback)(void*), void *Data);
1354 GVALinkage GetGVALinkageForFunction(const FunctionDecl *FD);
1355 GVALinkage GetGVALinkageForVariable(const VarDecl *VD);
1357 /// \brief Determines if the decl can be CodeGen'ed or deserialized from PCH
1358 /// lazily, only when used; this is only relevant for function or file scoped
1359 /// var definitions.
1361 /// \returns true if the function/var must be CodeGen'ed/deserialized even if
1363 bool DeclMustBeEmitted(const Decl *D);
1365 //===--------------------------------------------------------------------===//
1367 //===--------------------------------------------------------------------===//
1369 /// \brief The number of implicitly-declared default constructors.
1370 static unsigned NumImplicitDefaultConstructors;
1372 /// \brief The number of implicitly-declared default constructors for
1373 /// which declarations were built.
1374 static unsigned NumImplicitDefaultConstructorsDeclared;
1376 /// \brief The number of implicitly-declared copy constructors.
1377 static unsigned NumImplicitCopyConstructors;
1379 /// \brief The number of implicitly-declared copy constructors for
1380 /// which declarations were built.
1381 static unsigned NumImplicitCopyConstructorsDeclared;
1383 /// \brief The number of implicitly-declared copy assignment operators.
1384 static unsigned NumImplicitCopyAssignmentOperators;
1386 /// \brief The number of implicitly-declared copy assignment operators for
1387 /// which declarations were built.
1388 static unsigned NumImplicitCopyAssignmentOperatorsDeclared;
1390 /// \brief The number of implicitly-declared destructors.
1391 static unsigned NumImplicitDestructors;
1393 /// \brief The number of implicitly-declared destructors for which
1394 /// declarations were built.
1395 static unsigned NumImplicitDestructorsDeclared;
1398 ASTContext(const ASTContext&); // DO NOT IMPLEMENT
1399 void operator=(const ASTContext&); // DO NOT IMPLEMENT
1401 void InitBuiltinTypes();
1402 void InitBuiltinType(CanQualType &R, BuiltinType::Kind K);
1404 // Return the ObjC type encoding for a given type.
1405 void getObjCEncodingForTypeImpl(QualType t, std::string &S,
1406 bool ExpandPointedToStructures,
1407 bool ExpandStructures,
1408 const FieldDecl *Field,
1409 bool OutermostType = false,
1410 bool EncodingProperty = false);
1412 const ASTRecordLayout &getObjCLayout(const ObjCInterfaceDecl *D,
1413 const ObjCImplementationDecl *Impl);
1416 /// \brief A set of deallocations that should be performed when the
1417 /// ASTContext is destroyed.
1418 llvm::SmallVector<std::pair<void (*)(void*), void *>, 16> Deallocations;
1420 // FIXME: This currently contains the set of StoredDeclMaps used
1421 // by DeclContext objects. This probably should not be in ASTContext,
1422 // but we include it here so that ASTContext can quickly deallocate them.
1423 llvm::PointerIntPair<StoredDeclsMap*,1> LastSDM;
1425 /// \brief A counter used to uniquely identify "blocks".
1426 unsigned int UniqueBlockByRefTypeID;
1427 unsigned int UniqueBlockParmTypeID;
1429 friend class DeclContext;
1430 friend class DeclarationNameTable;
1431 void ReleaseDeclContextMaps();
1434 /// @brief Utility function for constructing a nullary selector.
1435 static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
1436 IdentifierInfo* II = &Ctx.Idents.get(name);
1437 return Ctx.Selectors.getSelector(0, &II);
1440 /// @brief Utility function for constructing an unary selector.
1441 static inline Selector GetUnarySelector(const char* name, ASTContext& Ctx) {
1442 IdentifierInfo* II = &Ctx.Idents.get(name);
1443 return Ctx.Selectors.getSelector(1, &II);
1446 } // end namespace clang
1448 // operator new and delete aren't allowed inside namespaces.
1449 // The throw specifications are mandated by the standard.
1450 /// @brief Placement new for using the ASTContext's allocator.
1452 /// This placement form of operator new uses the ASTContext's allocator for
1453 /// obtaining memory. It is a non-throwing new, which means that it returns
1454 /// null on error. (If that is what the allocator does. The current does, so if
1455 /// this ever changes, this operator will have to be changed, too.)
1456 /// Usage looks like this (assuming there's an ASTContext 'Context' in scope):
1458 /// // Default alignment (8)
1459 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
1460 /// // Specific alignment
1461 /// IntegerLiteral *Ex2 = new (Context, 4) IntegerLiteral(arguments);
1463 /// Please note that you cannot use delete on the pointer; it must be
1464 /// deallocated using an explicit destructor call followed by
1465 /// @c Context.Deallocate(Ptr).
1467 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
1468 /// @param C The ASTContext that provides the allocator.
1469 /// @param Alignment The alignment of the allocated memory (if the underlying
1470 /// allocator supports it).
1471 /// @return The allocated memory. Could be NULL.
1472 inline void *operator new(size_t Bytes, clang::ASTContext &C,
1473 size_t Alignment) throw () {
1474 return C.Allocate(Bytes, Alignment);
1476 /// @brief Placement delete companion to the new above.
1478 /// This operator is just a companion to the new above. There is no way of
1479 /// invoking it directly; see the new operator for more details. This operator
1480 /// is called implicitly by the compiler if a placement new expression using
1481 /// the ASTContext throws in the object constructor.
1482 inline void operator delete(void *Ptr, clang::ASTContext &C, size_t)
1487 /// This placement form of operator new[] uses the ASTContext's allocator for
1488 /// obtaining memory. It is a non-throwing new[], which means that it returns
1490 /// Usage looks like this (assuming there's an ASTContext 'Context' in scope):
1492 /// // Default alignment (8)
1493 /// char *data = new (Context) char[10];
1494 /// // Specific alignment
1495 /// char *data = new (Context, 4) char[10];
1497 /// Please note that you cannot use delete on the pointer; it must be
1498 /// deallocated using an explicit destructor call followed by
1499 /// @c Context.Deallocate(Ptr).
1501 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
1502 /// @param C The ASTContext that provides the allocator.
1503 /// @param Alignment The alignment of the allocated memory (if the underlying
1504 /// allocator supports it).
1505 /// @return The allocated memory. Could be NULL.
1506 inline void *operator new[](size_t Bytes, clang::ASTContext& C,
1507 size_t Alignment = 8) throw () {
1508 return C.Allocate(Bytes, Alignment);
1511 /// @brief Placement delete[] companion to the new[] above.
1513 /// This operator is just a companion to the new[] above. There is no way of
1514 /// invoking it directly; see the new[] operator for more details. This operator
1515 /// is called implicitly by the compiler if a placement new[] expression using
1516 /// the ASTContext throws in the object constructor.
1517 inline void operator delete[](void *Ptr, clang::ASTContext &C, size_t)