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