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