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