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