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