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