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