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