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