ASTContext.h revision 34ebde404dc17d89487b07e6daaf1b47d5dfee39
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/Builtins.h"
20#include "clang/AST/DeclarationName.h"
21#include "clang/AST/DeclBase.h"
22#include "clang/AST/Type.h"
23#include "clang/Basic/SourceLocation.h"
24#include "llvm/ADT/DenseMap.h"
25#include "llvm/ADT/FoldingSet.h"
26#include "llvm/Bitcode/SerializationFwd.h"
27#include "llvm/Support/Allocator.h"
28#include <vector>
29
30namespace llvm {
31  struct fltSemantics;
32}
33
34namespace clang {
35  class ASTRecordLayout;
36  class Expr;
37  class IdentifierTable;
38  class SelectorTable;
39  class SourceManager;
40  class TargetInfo;
41  // Decls
42  class Decl;
43  class ObjCPropertyDecl;
44  class RecordDecl;
45  class TagDecl;
46  class TranslationUnitDecl;
47  class TypeDecl;
48  class TypedefDecl;
49  class TemplateTypeParmDecl;
50  class FieldDecl;
51  class ObjCIvarRefExpr;
52  class ObjCIvarDecl;
53
54/// ASTContext - This class holds long-lived AST nodes (such as types and
55/// decls) that can be referred to throughout the semantic analysis of a file.
56class ASTContext {
57  std::vector<Type*> Types;
58  llvm::FoldingSet<ASQualType> ASQualTypes;
59  llvm::FoldingSet<ComplexType> ComplexTypes;
60  llvm::FoldingSet<PointerType> PointerTypes;
61  llvm::FoldingSet<BlockPointerType> BlockPointerTypes;
62  llvm::FoldingSet<ReferenceType> ReferenceTypes;
63  llvm::FoldingSet<MemberPointerType> MemberPointerTypes;
64  llvm::FoldingSet<ConstantArrayType> ConstantArrayTypes;
65  llvm::FoldingSet<IncompleteArrayType> IncompleteArrayTypes;
66  std::vector<VariableArrayType*> VariableArrayTypes;
67  std::vector<DependentSizedArrayType*> DependentSizedArrayTypes;
68  llvm::FoldingSet<VectorType> VectorTypes;
69  llvm::FoldingSet<FunctionTypeNoProto> FunctionTypeNoProtos;
70  llvm::FoldingSet<FunctionTypeProto> FunctionTypeProtos;
71  llvm::FoldingSet<ObjCQualifiedInterfaceType> ObjCQualifiedInterfaceTypes;
72  llvm::FoldingSet<ObjCQualifiedIdType> ObjCQualifiedIdTypes;
73  /// ASTRecordLayouts - A cache mapping from RecordDecls to ASTRecordLayouts.
74  ///  This is lazily created.  This is intentionally not serialized.
75  llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*> ASTRecordLayouts;
76  llvm::DenseMap<const ObjCInterfaceDecl*,
77                 const ASTRecordLayout*> ASTObjCInterfaces;
78
79  // FIXME: Shouldn't ASTRecordForInterface/ASTFieldForIvarRef and
80  // addRecordToClass/getFieldDecl be part of the backend (i.e. CodeGenTypes and
81  // CodeGenFunction)?
82  llvm::DenseMap<const ObjCInterfaceDecl*,
83                 const RecordDecl*> ASTRecordForInterface;
84  llvm::DenseMap<const ObjCIvarRefExpr*, const FieldDecl*> ASTFieldForIvarRef;
85
86  /// BuiltinVaListType - built-in va list type.
87  /// This is initially null and set by Sema::LazilyCreateBuiltin when
88  /// a builtin that takes a valist is encountered.
89  QualType BuiltinVaListType;
90
91  /// ObjCIdType - a pseudo built-in typedef type (set by Sema).
92  QualType ObjCIdType;
93  const RecordType *IdStructType;
94
95  /// ObjCSelType - another pseudo built-in typedef type (set by Sema).
96  QualType ObjCSelType;
97  const RecordType *SelStructType;
98
99  /// ObjCProtoType - another pseudo built-in typedef type (set by Sema).
100  QualType ObjCProtoType;
101  const RecordType *ProtoStructType;
102
103  /// ObjCClassType - another pseudo built-in typedef type (set by Sema).
104  QualType ObjCClassType;
105  const RecordType *ClassStructType;
106
107  QualType ObjCConstantStringType;
108  RecordDecl *CFConstantStringTypeDecl;
109
110  RecordDecl *ObjCFastEnumerationStateTypeDecl;
111
112  TranslationUnitDecl *TUDecl;
113
114  /// SourceMgr - The associated SourceManager object.
115  SourceManager &SourceMgr;
116
117  /// LangOpts - The language options used to create the AST associated with
118  ///  this ASTContext object.
119  LangOptions LangOpts;
120
121  /// Allocator - The allocator object used to create AST objects.
122  llvm::MallocAllocator Allocator;
123
124public:
125  TargetInfo &Target;
126  IdentifierTable &Idents;
127  SelectorTable &Selectors;
128  DeclarationNameTable DeclarationNames;
129
130  SourceManager& getSourceManager() { return SourceMgr; }
131  llvm::MallocAllocator &getAllocator() { return Allocator; }
132  const LangOptions& getLangOptions() const { return LangOpts; }
133
134  FullSourceLoc getFullLoc(SourceLocation Loc) const {
135    return FullSourceLoc(Loc,SourceMgr);
136  }
137
138  TranslationUnitDecl *getTranslationUnitDecl() const { return TUDecl; }
139
140  /// This is intentionally not serialized.  It is populated by the
141  /// ASTContext ctor, and there are no external pointers/references to
142  /// internal variables of BuiltinInfo.
143  Builtin::Context BuiltinInfo;
144
145  // Builtin Types.
146  QualType VoidTy;
147  QualType BoolTy;
148  QualType CharTy;
149  QualType WCharTy; // [C++ 3.9.1p5]
150  QualType SignedCharTy, ShortTy, IntTy, LongTy, LongLongTy;
151  QualType UnsignedCharTy, UnsignedShortTy, UnsignedIntTy, UnsignedLongTy;
152  QualType UnsignedLongLongTy;
153  QualType FloatTy, DoubleTy, LongDoubleTy;
154  QualType FloatComplexTy, DoubleComplexTy, LongDoubleComplexTy;
155  QualType VoidPtrTy;
156  QualType OverloadTy;
157  QualType DependentTy;
158
159  ASTContext(const LangOptions& LOpts, SourceManager &SM, TargetInfo &t,
160             IdentifierTable &idents, SelectorTable &sels,
161             unsigned size_reserve=0);
162
163  ~ASTContext();
164
165  void PrintStats() const;
166  const std::vector<Type*>& getTypes() const { return Types; }
167
168  //===--------------------------------------------------------------------===//
169  //                           Type Constructors
170  //===--------------------------------------------------------------------===//
171
172  /// getASQualType - Return the uniqued reference to the type for an address
173  /// space qualified type with the specified type and address space.  The
174  /// resulting type has a union of the qualifiers from T and the address space.
175  // If T already has an address space specifier, it is silently replaced.
176  QualType getASQualType(QualType T, unsigned AddressSpace);
177
178  /// getComplexType - Return the uniqued reference to the type for a complex
179  /// number with the specified element type.
180  QualType getComplexType(QualType T);
181
182  /// getPointerType - Return the uniqued reference to the type for a pointer to
183  /// the specified type.
184  QualType getPointerType(QualType T);
185
186  /// getBlockPointerType - Return the uniqued reference to the type for a block
187  /// of the specified type.
188  QualType getBlockPointerType(QualType T);
189
190  /// getReferenceType - Return the uniqued reference to the type for a
191  /// reference to the specified type.
192  QualType getReferenceType(QualType T);
193
194  /// getMemberPointerType - Return the uniqued reference to the type for a
195  /// member pointer to the specified type in the specified class. The class
196  /// is a Type because it could be a dependent name.
197  QualType getMemberPointerType(QualType T, const Type *Cls);
198
199  /// getVariableArrayType - Returns a non-unique reference to the type for a
200  /// variable array of the specified element type.
201  QualType getVariableArrayType(QualType EltTy, Expr *NumElts,
202                                ArrayType::ArraySizeModifier ASM,
203                                unsigned EltTypeQuals);
204
205  /// getDependentSizedArrayType - Returns a non-unique reference to
206  /// the type for a dependently-sized array of the specified element
207  /// type. FIXME: We will need these to be uniqued, or at least
208  /// comparable, at some point.
209  QualType getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
210                                      ArrayType::ArraySizeModifier ASM,
211                                      unsigned EltTypeQuals);
212
213  /// getIncompleteArrayType - Returns a unique reference to the type for a
214  /// incomplete array of the specified element type.
215  QualType getIncompleteArrayType(QualType EltTy,
216                                  ArrayType::ArraySizeModifier ASM,
217                                  unsigned EltTypeQuals);
218
219  /// getConstantArrayType - Return the unique reference to the type for a
220  /// constant array of the specified element type.
221  QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize,
222                                ArrayType::ArraySizeModifier ASM,
223                                unsigned EltTypeQuals);
224
225  /// getVectorType - Return the unique reference to a vector type of
226  /// the specified element type and size. VectorType must be a built-in type.
227  QualType getVectorType(QualType VectorType, unsigned NumElts);
228
229  /// getExtVectorType - Return the unique reference to an extended vector type
230  /// of the specified element type and size.  VectorType must be a built-in
231  /// type.
232  QualType getExtVectorType(QualType VectorType, unsigned NumElts);
233
234  /// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
235  ///
236  QualType getFunctionTypeNoProto(QualType ResultTy);
237
238  /// getFunctionType - Return a normal function type with a typed argument
239  /// list.  isVariadic indicates whether the argument list includes '...'.
240  QualType getFunctionType(QualType ResultTy, const QualType *ArgArray,
241                           unsigned NumArgs, bool isVariadic,
242                           unsigned TypeQuals);
243
244  /// getTypeDeclType - Return the unique reference to the type for
245  /// the specified type declaration.
246  QualType getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl=0);
247
248  /// getTypedefType - Return the unique reference to the type for the
249  /// specified typename decl.
250  QualType getTypedefType(TypedefDecl *Decl);
251  QualType getTemplateTypeParmType(TemplateTypeParmDecl *Decl);
252  QualType getObjCInterfaceType(ObjCInterfaceDecl *Decl);
253
254  /// getObjCQualifiedInterfaceType - Return a
255  /// ObjCQualifiedInterfaceType type for the given interface decl and
256  /// the conforming protocol list.
257  QualType getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
258             ObjCProtocolDecl **ProtocolList, unsigned NumProtocols);
259
260  /// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for a
261  /// given 'id' and conforming protocol list.
262  QualType getObjCQualifiedIdType(ObjCProtocolDecl **ProtocolList,
263                                  unsigned NumProtocols);
264
265
266  /// getTypeOfType - GCC extension.
267  QualType getTypeOfExpr(Expr *e);
268  QualType getTypeOfType(QualType t);
269
270  /// getTagDeclType - Return the unique reference to the type for the
271  /// specified TagDecl (struct/union/class/enum) decl.
272  QualType getTagDeclType(TagDecl *Decl);
273
274  /// getSizeType - Return the unique type for "size_t" (C99 7.17), defined
275  /// in <stddef.h>. The sizeof operator requires this (C99 6.5.3.4p4).
276  QualType getSizeType() const;
277
278  /// getWCharType - Return the unique type for "wchar_t" (C99 7.17), defined
279  /// in <stddef.h>. Wide strings require this (C99 6.4.5p5).
280  QualType getWCharType() const;
281
282  /// getSignedWCharType - Return the type of "signed wchar_t".
283  /// Used when in C++, as a GCC extension.
284  QualType getSignedWCharType() const;
285
286  /// getUnsignedWCharType - Return the type of "unsigned wchar_t".
287  /// Used when in C++, as a GCC extension.
288  QualType getUnsignedWCharType() const;
289
290  /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
291  /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
292  QualType getPointerDiffType() const;
293
294  // getCFConstantStringType - Return the C structure type used to represent
295  // constant CFStrings.
296  QualType getCFConstantStringType();
297
298  // This setter/getter represents the ObjC type for an NSConstantString.
299  void setObjCConstantStringInterface(ObjCInterfaceDecl *Decl);
300  QualType getObjCConstantStringInterface() const {
301    return ObjCConstantStringType;
302  }
303
304  //// This gets the struct used to keep track of fast enumerations.
305  QualType getObjCFastEnumerationStateType();
306
307  /// getObjCEncodingForType - Emit the ObjC type encoding for the
308  /// given type into \arg S. If \arg NameFields is specified then
309  /// record field names are also encoded.
310  void getObjCEncodingForType(QualType t, std::string &S,
311                              FieldDecl *Field=NULL) const;
312
313  void getLegacyIntegralTypeEncoding(QualType &t) const;
314
315  // Put the string version of type qualifiers into S.
316  void getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
317                                       std::string &S) const;
318
319  /// getObjCEncodingForMethodDecl - Return the encoded type for this method
320  /// declaration.
321  void getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, std::string &S);
322
323  /// getObjCEncodingForPropertyDecl - Return the encoded type for
324  /// this method declaration. If non-NULL, Container must be either
325  /// an ObjCCategoryImplDecl or ObjCImplementationDecl; it should
326  /// only be NULL when getting encodings for protocol properties.
327  void getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
328                                      const Decl *Container,
329                                      std::string &S);
330
331  /// getObjCEncodingTypeSize returns size of type for objective-c encoding
332  /// purpose.
333  int getObjCEncodingTypeSize(QualType t);
334
335  /// This setter/getter represents the ObjC 'id' type. It is setup lazily, by
336  /// Sema.  id is always a (typedef for a) pointer type, a pointer to a struct.
337  QualType getObjCIdType() const { return ObjCIdType; }
338  void setObjCIdType(TypedefDecl *Decl);
339
340  void setObjCSelType(TypedefDecl *Decl);
341  QualType getObjCSelType() const { return ObjCSelType; }
342
343  void setObjCProtoType(QualType QT);
344  QualType getObjCProtoType() const { return ObjCProtoType; }
345
346  /// This setter/getter repreents the ObjC 'Class' type. It is setup lazily, by
347  /// Sema.  'Class' is always a (typedef for a) pointer type, a pointer to a
348  /// struct.
349  QualType getObjCClassType() const { return ObjCClassType; }
350  void setObjCClassType(TypedefDecl *Decl);
351
352  void setBuiltinVaListType(QualType T);
353  QualType getBuiltinVaListType() const { return BuiltinVaListType; }
354
355private:
356  QualType getFromTargetType(unsigned Type) const;
357
358  //===--------------------------------------------------------------------===//
359  //                         Type Predicates.
360  //===--------------------------------------------------------------------===//
361
362public:
363  /// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
364  /// to an object type.  This includes "id" and "Class" (two 'special' pointers
365  /// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
366  /// ID type).
367  bool isObjCObjectPointerType(QualType Ty) const;
368
369  /// isObjCNSObjectType - Return true if this is an NSObject object with
370  /// its NSObject attribute set.
371  bool isObjCNSObjectType(QualType Ty) const;
372
373  //===--------------------------------------------------------------------===//
374  //                         Type Sizing and Analysis
375  //===--------------------------------------------------------------------===//
376
377  /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
378  /// scalar floating point type.
379  const llvm::fltSemantics &getFloatTypeSemantics(QualType T) const;
380
381  /// getTypeInfo - Get the size and alignment of the specified complete type in
382  /// bits.
383  std::pair<uint64_t, unsigned> getTypeInfo(const Type *T);
384  std::pair<uint64_t, unsigned> getTypeInfo(QualType T) {
385    return getTypeInfo(T.getTypePtr());
386  }
387
388  /// getTypeSize - Return the size of the specified type, in bits.  This method
389  /// does not work on incomplete types.
390  uint64_t getTypeSize(QualType T) {
391    return getTypeInfo(T).first;
392  }
393  uint64_t getTypeSize(const Type *T) {
394    return getTypeInfo(T).first;
395  }
396
397  /// getTypeAlign - Return the ABI-specified alignment of a type, in bits.
398  /// This method does not work on incomplete types.
399  unsigned getTypeAlign(QualType T) {
400    return getTypeInfo(T).second;
401  }
402  unsigned getTypeAlign(const Type *T) {
403    return getTypeInfo(T).second;
404  }
405
406  /// getPreferredTypeAlign - Return the "preferred" alignment of the specified
407  /// type for the current target in bits.  This can be different than the ABI
408  /// alignment in cases where it is beneficial for performance to overalign
409  /// a data type.
410  unsigned getPreferredTypeAlign(const Type *T);
411
412  /// getDeclAlign - Return the alignment of the specified decl that should be
413  /// returned by __alignof().  Note that bitfields do not have a valid
414  /// alignment, so this method will assert on them.
415  unsigned getDeclAlign(const Decl *D);
416
417  /// getASTRecordLayout - Get or compute information about the layout of the
418  /// specified record (struct/union/class), which indicates its size and field
419  /// position information.
420  const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D);
421
422  const ASTRecordLayout &getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D);
423  const RecordDecl *addRecordToClass(const ObjCInterfaceDecl *D);
424  const FieldDecl *getFieldDecl(const ObjCIvarRefExpr *MRef) {
425    llvm::DenseMap<const ObjCIvarRefExpr *, const FieldDecl*>::iterator I
426      = ASTFieldForIvarRef.find(MRef);
427    assert (I != ASTFieldForIvarRef.end()  && "Unable to find field_decl");
428    return I->second;
429  }
430  void setFieldDecl(const ObjCInterfaceDecl *OI,
431                    const ObjCIvarDecl *Ivar,
432                    const ObjCIvarRefExpr *MRef);
433  //===--------------------------------------------------------------------===//
434  //                            Type Operators
435  //===--------------------------------------------------------------------===//
436
437  /// getCanonicalType - Return the canonical (structural) type corresponding to
438  /// the specified potentially non-canonical type.  The non-canonical version
439  /// of a type may have many "decorated" versions of types.  Decorators can
440  /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
441  /// to be free of any of these, allowing two canonical types to be compared
442  /// for exact equality with a simple pointer comparison.
443  QualType getCanonicalType(QualType T);
444  const Type *getCanonicalType(const Type *T) {
445    return T->getCanonicalTypeInternal().getTypePtr();
446  }
447
448  /// Type Query functions.  If the type is an instance of the specified class,
449  /// return the Type pointer for the underlying maximally pretty type.  This
450  /// is a member of ASTContext because this may need to do some amount of
451  /// canonicalization, e.g. to move type qualifiers into the element type.
452  const ArrayType *getAsArrayType(QualType T);
453  const ConstantArrayType *getAsConstantArrayType(QualType T) {
454    return dyn_cast_or_null<ConstantArrayType>(getAsArrayType(T));
455  }
456  const VariableArrayType *getAsVariableArrayType(QualType T) {
457    return dyn_cast_or_null<VariableArrayType>(getAsArrayType(T));
458  }
459  const IncompleteArrayType *getAsIncompleteArrayType(QualType T) {
460    return dyn_cast_or_null<IncompleteArrayType>(getAsArrayType(T));
461  }
462
463  /// getBaseElementType - Returns the innermost element type of a variable
464  /// length array type. For example, will return "int" for int[m][n]
465  QualType getBaseElementType(const VariableArrayType *VAT);
466
467  /// getArrayDecayedType - Return the properly qualified result of decaying the
468  /// specified array type to a pointer.  This operation is non-trivial when
469  /// handling typedefs etc.  The canonical type of "T" must be an array type,
470  /// this returns a pointer to a properly qualified element of the array.
471  ///
472  /// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
473  QualType getArrayDecayedType(QualType T);
474
475  /// getIntegerTypeOrder - Returns the highest ranked integer type:
476  /// C99 6.3.1.8p1.  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
477  /// LHS < RHS, return -1.
478  int getIntegerTypeOrder(QualType LHS, QualType RHS);
479
480  /// getFloatingTypeOrder - Compare the rank of the two specified floating
481  /// point types, ignoring the domain of the type (i.e. 'double' ==
482  /// '_Complex double').  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
483  /// LHS < RHS, return -1.
484  int getFloatingTypeOrder(QualType LHS, QualType RHS);
485
486  /// getFloatingTypeOfSizeWithinDomain - Returns a real floating
487  /// point or a complex type (based on typeDomain/typeSize).
488  /// 'typeDomain' is a real floating point or complex type.
489  /// 'typeSize' is a real floating point or complex type.
490  QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize,
491                                             QualType typeDomain) const;
492
493  //===--------------------------------------------------------------------===//
494  //                    Type Compatibility Predicates
495  //===--------------------------------------------------------------------===//
496
497  /// Compatibility predicates used to check assignment expressions.
498  bool typesAreCompatible(QualType, QualType); // C99 6.2.7p1
499  bool typesAreBlockCompatible(QualType lhs, QualType rhs);
500
501  bool isObjCIdType(QualType T) const {
502    if (!IdStructType) // ObjC isn't enabled
503      return false;
504    return T->getAsStructureType() == IdStructType;
505  }
506  bool isObjCClassType(QualType T) const {
507    if (!ClassStructType) // ObjC isn't enabled
508      return false;
509    return T->getAsStructureType() == ClassStructType;
510  }
511  bool isObjCSelType(QualType T) const {
512    assert(SelStructType && "isObjCSelType used before 'SEL' type is built");
513    return T->getAsStructureType() == SelStructType;
514  }
515
516  // Check the safety of assignment from LHS to RHS
517  bool canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
518                               const ObjCInterfaceType *RHS);
519
520  // Functions for calculating composite types
521  QualType mergeTypes(QualType, QualType);
522  QualType mergeFunctionTypes(QualType, QualType);
523
524  //===--------------------------------------------------------------------===//
525  //                    Integer Predicates
526  //===--------------------------------------------------------------------===//
527
528  // The width of an integer, as defined in C99 6.2.6.2. This is the number
529  // of bits in an integer type excluding any padding bits.
530  unsigned getIntWidth(QualType T);
531
532  // Per C99 6.2.5p6, for every signed integer type, there is a corresponding
533  // unsigned integer type.  This method takes a signed type, and returns the
534  // corresponding unsigned integer type.
535  QualType getCorrespondingUnsignedType(QualType T);
536
537  //===--------------------------------------------------------------------===//
538  //                    Type Iterators.
539  //===--------------------------------------------------------------------===//
540
541  typedef std::vector<Type*>::iterator       type_iterator;
542  typedef std::vector<Type*>::const_iterator const_type_iterator;
543
544  type_iterator types_begin() { return Types.begin(); }
545  type_iterator types_end() { return Types.end(); }
546  const_type_iterator types_begin() const { return Types.begin(); }
547  const_type_iterator types_end() const { return Types.end(); }
548
549  //===--------------------------------------------------------------------===//
550  //                    Serialization
551  //===--------------------------------------------------------------------===//
552
553  void Emit(llvm::Serializer& S) const;
554  static ASTContext* Create(llvm::Deserializer& D);
555
556private:
557  ASTContext(const ASTContext&); // DO NOT IMPLEMENT
558  void operator=(const ASTContext&); // DO NOT IMPLEMENT
559
560  void InitBuiltinTypes();
561  void InitBuiltinType(QualType &R, BuiltinType::Kind K);
562
563  // Return the ObjC type encoding for a given type.
564  void getObjCEncodingForTypeImpl(QualType t, std::string &S,
565                                  bool ExpandPointedToStructures,
566                                  bool ExpandStructures,
567                                  FieldDecl *Field,
568                                  bool OutermostType = false,
569                                  bool EncodingProperty = false) const;
570
571};
572
573}  // end namespace clang
574
575// operator new and delete aren't allowed inside namespaces.
576// The throw specifications are mandated by the standard.
577/// @brief Placement new for using the ASTContext's allocator.
578///
579/// This placement form of operator new uses the ASTContext's allocator for
580/// obtaining memory. It is a non-throwing new, which means that it returns
581/// null on error. (If that is what the allocator does. The current does, so if
582/// this ever changes, this operator will have to be changed, too.)
583/// Usage looks like this (assuming there's an ASTContext 'Context' in scope):
584/// @code
585/// // Default alignment (16)
586/// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
587/// // Specific alignment
588/// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
589/// @endcode
590/// Please note that you cannot use delete on the pointer; it must be
591/// deallocated using an explicit destructor call followed by
592/// @c Context.getAllocator().Deallocate(Ptr)
593///
594/// @param Bytes The number of bytes to allocate. Calculated by the compiler.
595/// @param C The ASTContext that provides the allocator.
596/// @param Alignment The alignment of the allocated memory (if the allocator
597///                  supports it, which the current one doesn't).
598/// @return The allocated memory. Could be NULL.
599inline void *operator new(size_t Bytes, clang::ASTContext &C,
600                          size_t Alignment = 16) throw () {
601  return C.getAllocator().Allocate(Bytes, Alignment);
602}
603/// @brief Placement delete companion to the new above.
604///
605/// This operator is just a companion to the new above. There is no way of
606/// invoking it directly; see the new operator for more details. This operator
607/// is called implicitly by the compiler if a placement new expression using
608/// the ASTContext throws in the object constructor.
609inline void operator delete(void *Ptr, clang::ASTContext &C)
610              throw () {
611  C.getAllocator().Deallocate(Ptr);
612}
613
614#endif
615