ASTContext.h revision f30208ad5b334e93582e846a2a0c92f38a607b8a
1076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian//===--- ASTContext.h - Context to hold long-lived AST nodes ----*- C++ -*-===//
2076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian//
3076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian//                     The LLVM Compiler Infrastructure
4076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian//
5076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian// This file is distributed under the University of Illinois Open Source
6076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian// License. See LICENSE.TXT for details.
7076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian//
8076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian//===----------------------------------------------------------------------===//
9076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian//
10076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian//  This file defines the ASTContext interface.
11076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian//
12076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian//===----------------------------------------------------------------------===//
13076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian
14076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian#ifndef LLVM_CLANG_AST_ASTCONTEXT_H
15076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian#define LLVM_CLANG_AST_ASTCONTEXT_H
16076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian
17076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian#include "clang/Basic/IdentifierTable.h"
18076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian#include "clang/Basic/LangOptions.h"
19076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian#include "clang/AST/Builtins.h"
20076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian#include "clang/AST/DeclarationName.h"
21076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian#include "clang/AST/DeclBase.h"
228b765b7f5ea7f56963ea0e3141d043d20944dbccMathias Agopian#include "clang/AST/Type.h"
23076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian#include "clang/Basic/SourceLocation.h"
24076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian#include "llvm/ADT/DenseMap.h"
25076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian#include "llvm/ADT/FoldingSet.h"
26076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian#include "llvm/Bitcode/SerializationFwd.h"
27076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian#include "llvm/Support/Allocator.h"
28076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian#include <vector>
29076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian
30076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopiannamespace llvm {
31076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian  struct fltSemantics;
32076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian}
33076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian
34076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopiannamespace clang {
35076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian  class ASTRecordLayout;
363330b203039dea366d4981db1408a460134b2d2cMathias Agopian  class Expr;
37076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian  class IdentifierTable;
38076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian  class SelectorTable;
393330b203039dea366d4981db1408a460134b2d2cMathias Agopian  class SourceManager;
400926f50664c739eaee60341f8e8c694dc9a4f3ebMathias Agopian  class TargetInfo;
410926f50664c739eaee60341f8e8c694dc9a4f3ebMathias Agopian  // Decls
420926f50664c739eaee60341f8e8c694dc9a4f3ebMathias Agopian  class Decl;
430926f50664c739eaee60341f8e8c694dc9a4f3ebMathias Agopian  class ObjCPropertyDecl;
440926f50664c739eaee60341f8e8c694dc9a4f3ebMathias Agopian  class RecordDecl;
450926f50664c739eaee60341f8e8c694dc9a4f3ebMathias Agopian  class TagDecl;
460926f50664c739eaee60341f8e8c694dc9a4f3ebMathias Agopian  class TranslationUnitDecl;
470926f50664c739eaee60341f8e8c694dc9a4f3ebMathias Agopian  class TypeDecl;
48c43946b931de5dafd28f49963f9af78e05390b26Eino-Ville Talvala  class TypedefDecl;
49c43946b931de5dafd28f49963f9af78e05390b26Eino-Ville Talvala  class TemplateTypeParmDecl;
50c43946b931de5dafd28f49963f9af78e05390b26Eino-Ville Talvala  class FieldDecl;
51076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian  class ObjCIvarRefExpr;
528f3960179c56767e5077be8337792bd4e244b7d7Francis Hart  class ObjCIvarDecl;
538f3960179c56767e5077be8337792bd4e244b7d7Francis Hart
548f3960179c56767e5077be8337792bd4e244b7d7Francis Hart/// ASTContext - This class holds long-lived AST nodes (such as types and
558f3960179c56767e5077be8337792bd4e244b7d7Francis Hart/// decls) that can be referred to throughout the semantic analysis of a file.
568f3960179c56767e5077be8337792bd4e244b7d7Francis Hartclass ASTContext {
578f3960179c56767e5077be8337792bd4e244b7d7Francis Hart  std::vector<Type*> Types;
588f3960179c56767e5077be8337792bd4e244b7d7Francis Hart  llvm::FoldingSet<ASQualType> ASQualTypes;
598f3960179c56767e5077be8337792bd4e244b7d7Francis Hart  llvm::FoldingSet<ComplexType> ComplexTypes;
60076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian  llvm::FoldingSet<PointerType> PointerTypes;
618b765b7f5ea7f56963ea0e3141d043d20944dbccMathias Agopian  llvm::FoldingSet<BlockPointerType> BlockPointerTypes;
628b765b7f5ea7f56963ea0e3141d043d20944dbccMathias Agopian  llvm::FoldingSet<ReferenceType> ReferenceTypes;
638b765b7f5ea7f56963ea0e3141d043d20944dbccMathias Agopian  llvm::FoldingSet<MemberPointerType> MemberPointerTypes;
64076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian  llvm::FoldingSet<ConstantArrayType> ConstantArrayTypes;
653330b203039dea366d4981db1408a460134b2d2cMathias Agopian  llvm::FoldingSet<IncompleteArrayType> IncompleteArrayTypes;
663330b203039dea366d4981db1408a460134b2d2cMathias Agopian  std::vector<VariableArrayType*> VariableArrayTypes;
67076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian  std::vector<DependentSizedArrayType*> DependentSizedArrayTypes;
68076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian  llvm::FoldingSet<VectorType> VectorTypes;
69076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian  llvm::FoldingSet<FunctionTypeNoProto> FunctionTypeNoProtos;
70076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian  llvm::FoldingSet<FunctionTypeProto> FunctionTypeProtos;
71076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian  llvm::FoldingSet<ObjCQualifiedInterfaceType> ObjCQualifiedInterfaceTypes;
72076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian  llvm::FoldingSet<ObjCQualifiedIdType> ObjCQualifiedIdTypes;
73076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian  /// ASTRecordLayouts - A cache mapping from RecordDecls to ASTRecordLayouts.
74076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian  ///  This is lazily created.  This is intentionally not serialized.
75076b1cc3a9b90aa5b381a1ed268ca0b548444c9bMathias Agopian  llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*> ASTRecordLayouts;
76  llvm::DenseMap<const ObjCInterfaceDecl*,
77                 const ASTRecordLayout*> ASTObjCInterfaces;
78
79  // FIXME: Shouldn't ASTRecordForInterface/ASTFieldForIvarRef and
80  // addRecordToClass/getFieldDecl be part of the backend (i.e. CodeGenTypes and
81  // CodeGenFunction)?
82  llvm::DenseMap<const ObjCInterfaceDecl*,
83                 const RecordDecl*> ASTRecordForInterface;
84  llvm::DenseMap<const ObjCIvarRefExpr*, const FieldDecl*> ASTFieldForIvarRef;
85
86  /// BuiltinVaListType - built-in va list type.
87  /// This is initially null and set by Sema::LazilyCreateBuiltin when
88  /// a builtin that takes a valist is encountered.
89  QualType BuiltinVaListType;
90
91  /// ObjCIdType - a pseudo built-in typedef type (set by Sema).
92  QualType ObjCIdType;
93  const RecordType *IdStructType;
94
95  /// ObjCSelType - another pseudo built-in typedef type (set by Sema).
96  QualType ObjCSelType;
97  const RecordType *SelStructType;
98
99  /// ObjCProtoType - another pseudo built-in typedef type (set by Sema).
100  QualType ObjCProtoType;
101  const RecordType *ProtoStructType;
102
103  /// ObjCClassType - another pseudo built-in typedef type (set by Sema).
104  QualType ObjCClassType;
105  const RecordType *ClassStructType;
106
107  QualType ObjCConstantStringType;
108  RecordDecl *CFConstantStringTypeDecl;
109
110  RecordDecl *ObjCFastEnumerationStateTypeDecl;
111
112  TranslationUnitDecl *TUDecl;
113
114  /// SourceMgr - The associated SourceManager object.
115  SourceManager &SourceMgr;
116
117  /// LangOpts - The language options used to create the AST associated with
118  ///  this ASTContext object.
119  LangOptions LangOpts;
120
121  /// Allocator - The allocator object used to create AST objects.
122  llvm::MallocAllocator Allocator;
123
124public:
125  TargetInfo &Target;
126  IdentifierTable &Idents;
127  SelectorTable &Selectors;
128  DeclarationNameTable DeclarationNames;
129
130  SourceManager& getSourceManager() { return SourceMgr; }
131  llvm::MallocAllocator &getAllocator() { return Allocator; }
132  const LangOptions& getLangOptions() const { return LangOpts; }
133
134  FullSourceLoc getFullLoc(SourceLocation Loc) const {
135    return FullSourceLoc(Loc,SourceMgr);
136  }
137
138  TranslationUnitDecl *getTranslationUnitDecl() const { return TUDecl; }
139
140  /// This is intentionally not serialized.  It is populated by the
141  /// ASTContext ctor, and there are no external pointers/references to
142  /// internal variables of BuiltinInfo.
143  Builtin::Context BuiltinInfo;
144
145  // Builtin Types.
146  QualType VoidTy;
147  QualType BoolTy;
148  QualType CharTy;
149  QualType WCharTy; // [C++ 3.9.1p5]
150  QualType SignedCharTy, ShortTy, IntTy, LongTy, LongLongTy;
151  QualType UnsignedCharTy, UnsignedShortTy, UnsignedIntTy, UnsignedLongTy;
152  QualType UnsignedLongLongTy;
153  QualType FloatTy, DoubleTy, LongDoubleTy;
154  QualType FloatComplexTy, DoubleComplexTy, LongDoubleComplexTy;
155  QualType VoidPtrTy;
156  QualType OverloadTy;
157  QualType DependentTy;
158
159  ASTContext(const LangOptions& LOpts, SourceManager &SM, TargetInfo &t,
160             IdentifierTable &idents, SelectorTable &sels,
161             unsigned size_reserve=0);
162
163  ~ASTContext();
164
165  void PrintStats() const;
166  const std::vector<Type*>& getTypes() const { return Types; }
167
168  //===--------------------------------------------------------------------===//
169  //                           Type Constructors
170  //===--------------------------------------------------------------------===//
171
172  /// getASQualType - Return the uniqued reference to the type for an address
173  /// space qualified type with the specified type and address space.  The
174  /// resulting type has a union of the qualifiers from T and the address space.
175  // If T already has an address space specifier, it is silently replaced.
176  QualType getASQualType(QualType T, unsigned AddressSpace);
177
178  /// getComplexType - Return the uniqued reference to the type for a complex
179  /// number with the specified element type.
180  QualType getComplexType(QualType T);
181
182  /// getPointerType - Return the uniqued reference to the type for a pointer to
183  /// the specified type.
184  QualType getPointerType(QualType T);
185
186  /// getBlockPointerType - Return the uniqued reference to the type for a block
187  /// of the specified type.
188  QualType getBlockPointerType(QualType T);
189
190  /// getReferenceType - Return the uniqued reference to the type for a
191  /// reference to the specified type.
192  QualType getReferenceType(QualType T);
193
194  /// getMemberPointerType - Return the uniqued reference to the type for a
195  /// member pointer to the specified type in the specified class. The class
196  /// is a Type because it could be a dependent name.
197  QualType getMemberPointerType(QualType T, const Type *Cls);
198
199  /// getVariableArrayType - Returns a non-unique reference to the type for a
200  /// variable array of the specified element type.
201  QualType getVariableArrayType(QualType EltTy, Expr *NumElts,
202                                ArrayType::ArraySizeModifier ASM,
203                                unsigned EltTypeQuals);
204
205  /// getDependentSizedArrayType - Returns a non-unique reference to
206  /// the type for a dependently-sized array of the specified element
207  /// type. FIXME: We will need these to be uniqued, or at least
208  /// comparable, at some point.
209  QualType getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
210                                      ArrayType::ArraySizeModifier ASM,
211                                      unsigned EltTypeQuals);
212
213  /// getIncompleteArrayType - Returns a unique reference to the type for a
214  /// incomplete array of the specified element type.
215  QualType getIncompleteArrayType(QualType EltTy,
216                                  ArrayType::ArraySizeModifier ASM,
217                                  unsigned EltTypeQuals);
218
219  /// getConstantArrayType - Return the unique reference to the type for a
220  /// constant array of the specified element type.
221  QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize,
222                                ArrayType::ArraySizeModifier ASM,
223                                unsigned EltTypeQuals);
224
225  /// getVectorType - Return the unique reference to a vector type of
226  /// the specified element type and size. VectorType must be a built-in type.
227  QualType getVectorType(QualType VectorType, unsigned NumElts);
228
229  /// getExtVectorType - Return the unique reference to an extended vector type
230  /// of the specified element type and size.  VectorType must be a built-in
231  /// type.
232  QualType getExtVectorType(QualType VectorType, unsigned NumElts);
233
234  /// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
235  ///
236  QualType getFunctionTypeNoProto(QualType ResultTy);
237
238  /// getFunctionType - Return a normal function type with a typed argument
239  /// list.  isVariadic indicates whether the argument list includes '...'.
240  QualType getFunctionType(QualType ResultTy, const QualType *ArgArray,
241                           unsigned NumArgs, bool isVariadic,
242                           unsigned TypeQuals);
243
244  /// getTypeDeclType - Return the unique reference to the type for
245  /// the specified type declaration.
246  QualType getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl=0);
247
248  /// getTypedefType - Return the unique reference to the type for the
249  /// specified typename decl.
250  QualType getTypedefType(TypedefDecl *Decl);
251  QualType getTemplateTypeParmType(TemplateTypeParmDecl *Decl);
252  QualType getObjCInterfaceType(ObjCInterfaceDecl *Decl);
253
254  /// getObjCQualifiedInterfaceType - Return a
255  /// ObjCQualifiedInterfaceType type for the given interface decl and
256  /// the conforming protocol list.
257  QualType getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
258             ObjCProtocolDecl **ProtocolList, unsigned NumProtocols);
259
260  /// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for a
261  /// given 'id' and conforming protocol list.
262  QualType getObjCQualifiedIdType(ObjCProtocolDecl **ProtocolList,
263                                  unsigned NumProtocols);
264
265
266  /// getTypeOfType - GCC extension.
267  QualType getTypeOfExpr(Expr *e);
268  QualType getTypeOfType(QualType t);
269
270  /// getTagDeclType - Return the unique reference to the type for the
271  /// specified TagDecl (struct/union/class/enum) decl.
272  QualType getTagDeclType(TagDecl *Decl);
273
274  /// getSizeType - Return the unique type for "size_t" (C99 7.17), defined
275  /// in <stddef.h>. The sizeof operator requires this (C99 6.5.3.4p4).
276  QualType getSizeType() const;
277
278  /// getWCharType - Return the unique type for "wchar_t" (C99 7.17), defined
279  /// in <stddef.h>. Wide strings require this (C99 6.4.5p5).
280  QualType getWCharType() const;
281
282  /// getSignedWCharType - Return the type of "signed wchar_t".
283  /// Used when in C++, as a GCC extension.
284  QualType getSignedWCharType() const;
285
286  /// getUnsignedWCharType - Return the type of "unsigned wchar_t".
287  /// Used when in C++, as a GCC extension.
288  QualType getUnsignedWCharType() const;
289
290  /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
291  /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
292  QualType getPointerDiffType() const;
293
294  // getCFConstantStringType - Return the C structure type used to represent
295  // constant CFStrings.
296  QualType getCFConstantStringType();
297
298  // This setter/getter represents the ObjC type for an NSConstantString.
299  void setObjCConstantStringInterface(ObjCInterfaceDecl *Decl);
300  QualType getObjCConstantStringInterface() const {
301    return ObjCConstantStringType;
302  }
303
304  //// This gets the struct used to keep track of fast enumerations.
305  QualType getObjCFastEnumerationStateType();
306
307  /// getObjCEncodingForType - Emit the ObjC type encoding for the
308  /// given type into \arg S. If \arg NameFields is specified then
309  /// record field names are also encoded.
310  void getObjCEncodingForType(QualType t, std::string &S,
311                              FieldDecl *Field=NULL) const;
312
313  void getLegacyIntegralTypeEncoding(QualType &t) const;
314
315  // Put the string version of type qualifiers into S.
316  void getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
317                                       std::string &S) const;
318
319  /// getObjCEncodingForMethodDecl - Return the encoded type for this method
320  /// declaration.
321  void getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, std::string &S);
322
323  /// getObjCEncodingForPropertyDecl - Return the encoded type for
324  /// this method declaration. If non-NULL, Container must be either
325  /// an ObjCCategoryImplDecl or ObjCImplementationDecl; it should
326  /// only be NULL when getting encodings for protocol properties.
327  void getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
328                                      const Decl *Container,
329                                      std::string &S);
330
331  /// getObjCEncodingTypeSize returns size of type for objective-c encoding
332  /// purpose.
333  int getObjCEncodingTypeSize(QualType t);
334
335  /// This setter/getter represents the ObjC 'id' type. It is setup lazily, by
336  /// Sema.  id is always a (typedef for a) pointer type, a pointer to a struct.
337  QualType getObjCIdType() const { return ObjCIdType; }
338  void setObjCIdType(TypedefDecl *Decl);
339
340  void setObjCSelType(TypedefDecl *Decl);
341  QualType getObjCSelType() const { return ObjCSelType; }
342
343  void setObjCProtoType(QualType QT);
344  QualType getObjCProtoType() const { return ObjCProtoType; }
345
346  /// This setter/getter repreents the ObjC 'Class' type. It is setup lazily, by
347  /// Sema.  'Class' is always a (typedef for a) pointer type, a pointer to a
348  /// struct.
349  QualType getObjCClassType() const { return ObjCClassType; }
350  void setObjCClassType(TypedefDecl *Decl);
351
352  void setBuiltinVaListType(QualType T);
353  QualType getBuiltinVaListType() const { return BuiltinVaListType; }
354
355private:
356  QualType getFromTargetType(unsigned Type) const;
357
358  //===--------------------------------------------------------------------===//
359  //                         Type Predicates.
360  //===--------------------------------------------------------------------===//
361
362public:
363  /// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
364  /// to an object type.  This includes "id" and "Class" (two 'special' pointers
365  /// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
366  /// ID type).
367  bool isObjCObjectPointerType(QualType Ty) const;
368
369  /// isObjCNSObjectType - Return true if this is an NSObject object with
370  /// its NSObject attribute set.
371  bool isObjCNSObjectType(QualType Ty) const;
372
373  //===--------------------------------------------------------------------===//
374  //                         Type Sizing and Analysis
375  //===--------------------------------------------------------------------===//
376
377  /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
378  /// scalar floating point type.
379  const llvm::fltSemantics &getFloatTypeSemantics(QualType T) const;
380
381  /// getTypeInfo - Get the size and alignment of the specified complete type in
382  /// bits.
383  std::pair<uint64_t, unsigned> getTypeInfo(const Type *T);
384  std::pair<uint64_t, unsigned> getTypeInfo(QualType T) {
385    return getTypeInfo(T.getTypePtr());
386  }
387
388  /// getTypeSize - Return the size of the specified type, in bits.  This method
389  /// does not work on incomplete types.
390  uint64_t getTypeSize(QualType T) {
391    return getTypeInfo(T).first;
392  }
393  uint64_t getTypeSize(const Type *T) {
394    return getTypeInfo(T).first;
395  }
396
397  /// getTypeAlign - Return the alignment of the specified type, in bits.  This
398  /// method does not work on incomplete types.
399  unsigned getTypeAlign(QualType T) {
400    return getTypeInfo(T).second;
401  }
402  unsigned getTypeAlign(const Type *T) {
403    return getTypeInfo(T).second;
404  }
405
406  /// getASTRecordLayout - Get or compute information about the layout of the
407  /// specified record (struct/union/class), which indicates its size and field
408  /// position information.
409  const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D);
410
411  const ASTRecordLayout &getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D);
412  const RecordDecl *addRecordToClass(const ObjCInterfaceDecl *D);
413  const FieldDecl *getFieldDecl(const ObjCIvarRefExpr *MRef) {
414    llvm::DenseMap<const ObjCIvarRefExpr *, const FieldDecl*>::iterator I
415      = ASTFieldForIvarRef.find(MRef);
416    assert (I != ASTFieldForIvarRef.end()  && "Unable to find field_decl");
417    return I->second;
418  }
419  void setFieldDecl(const ObjCInterfaceDecl *OI,
420                    const ObjCIvarDecl *Ivar,
421                    const ObjCIvarRefExpr *MRef);
422  //===--------------------------------------------------------------------===//
423  //                            Type Operators
424  //===--------------------------------------------------------------------===//
425
426  /// getCanonicalType - Return the canonical (structural) type corresponding to
427  /// the specified potentially non-canonical type.  The non-canonical version
428  /// of a type may have many "decorated" versions of types.  Decorators can
429  /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
430  /// to be free of any of these, allowing two canonical types to be compared
431  /// for exact equality with a simple pointer comparison.
432  QualType getCanonicalType(QualType T);
433  const Type *getCanonicalType(const Type *T) {
434    return T->getCanonicalTypeInternal().getTypePtr();
435  }
436
437  /// Type Query functions.  If the type is an instance of the specified class,
438  /// return the Type pointer for the underlying maximally pretty type.  This
439  /// is a member of ASTContext because this may need to do some amount of
440  /// canonicalization, e.g. to move type qualifiers into the element type.
441  const ArrayType *getAsArrayType(QualType T);
442  const ConstantArrayType *getAsConstantArrayType(QualType T) {
443    return dyn_cast_or_null<ConstantArrayType>(getAsArrayType(T));
444  }
445  const VariableArrayType *getAsVariableArrayType(QualType T) {
446    return dyn_cast_or_null<VariableArrayType>(getAsArrayType(T));
447  }
448  const IncompleteArrayType *getAsIncompleteArrayType(QualType T) {
449    return dyn_cast_or_null<IncompleteArrayType>(getAsArrayType(T));
450  }
451
452  /// getBaseElementType - Returns the innermost element type of a variable
453  /// length array type. For example, will return "int" for int[m][n]
454  QualType getBaseElementType(const VariableArrayType *VAT);
455
456  /// getArrayDecayedType - Return the properly qualified result of decaying the
457  /// specified array type to a pointer.  This operation is non-trivial when
458  /// handling typedefs etc.  The canonical type of "T" must be an array type,
459  /// this returns a pointer to a properly qualified element of the array.
460  ///
461  /// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
462  QualType getArrayDecayedType(QualType T);
463
464  /// getIntegerTypeOrder - Returns the highest ranked integer type:
465  /// C99 6.3.1.8p1.  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
466  /// LHS < RHS, return -1.
467  int getIntegerTypeOrder(QualType LHS, QualType RHS);
468
469  /// getFloatingTypeOrder - Compare the rank of the two specified floating
470  /// point types, ignoring the domain of the type (i.e. 'double' ==
471  /// '_Complex double').  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
472  /// LHS < RHS, return -1.
473  int getFloatingTypeOrder(QualType LHS, QualType RHS);
474
475  /// getFloatingTypeOfSizeWithinDomain - Returns a real floating
476  /// point or a complex type (based on typeDomain/typeSize).
477  /// 'typeDomain' is a real floating point or complex type.
478  /// 'typeSize' is a real floating point or complex type.
479  QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize,
480                                             QualType typeDomain) const;
481
482  //===--------------------------------------------------------------------===//
483  //                    Type Compatibility Predicates
484  //===--------------------------------------------------------------------===//
485
486  /// Compatibility predicates used to check assignment expressions.
487  bool typesAreCompatible(QualType, QualType); // C99 6.2.7p1
488  bool typesAreBlockCompatible(QualType lhs, QualType rhs);
489
490  bool isObjCIdType(QualType T) const {
491    if (!IdStructType) // ObjC isn't enabled
492      return false;
493    return T->getAsStructureType() == IdStructType;
494  }
495  bool isObjCClassType(QualType T) const {
496    if (!ClassStructType) // ObjC isn't enabled
497      return false;
498    return T->getAsStructureType() == ClassStructType;
499  }
500  bool isObjCSelType(QualType T) const {
501    assert(SelStructType && "isObjCSelType used before 'SEL' type is built");
502    return T->getAsStructureType() == SelStructType;
503  }
504
505  // Check the safety of assignment from LHS to RHS
506  bool canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
507                               const ObjCInterfaceType *RHS);
508
509  // Functions for calculating composite types
510  QualType mergeTypes(QualType, QualType);
511  QualType mergeFunctionTypes(QualType, QualType);
512
513  //===--------------------------------------------------------------------===//
514  //                    Integer Predicates
515  //===--------------------------------------------------------------------===//
516
517  // The width of an integer, as defined in C99 6.2.6.2. This is the number
518  // of bits in an integer type excluding any padding bits.
519  unsigned getIntWidth(QualType T);
520
521  // Per C99 6.2.5p6, for every signed integer type, there is a corresponding
522  // unsigned integer type.  This method takes a signed type, and returns the
523  // corresponding unsigned integer type.
524  QualType getCorrespondingUnsignedType(QualType T);
525
526  //===--------------------------------------------------------------------===//
527  //                    Type Iterators.
528  //===--------------------------------------------------------------------===//
529
530  typedef std::vector<Type*>::iterator       type_iterator;
531  typedef std::vector<Type*>::const_iterator const_type_iterator;
532
533  type_iterator types_begin() { return Types.begin(); }
534  type_iterator types_end() { return Types.end(); }
535  const_type_iterator types_begin() const { return Types.begin(); }
536  const_type_iterator types_end() const { return Types.end(); }
537
538  //===--------------------------------------------------------------------===//
539  //                    Serialization
540  //===--------------------------------------------------------------------===//
541
542  void Emit(llvm::Serializer& S) const;
543  static ASTContext* Create(llvm::Deserializer& D);
544
545private:
546  ASTContext(const ASTContext&); // DO NOT IMPLEMENT
547  void operator=(const ASTContext&); // DO NOT IMPLEMENT
548
549  void InitBuiltinTypes();
550  void InitBuiltinType(QualType &R, BuiltinType::Kind K);
551
552  // Return the ObjC type encoding for a given type.
553  void getObjCEncodingForTypeImpl(QualType t, std::string &S,
554                                  bool ExpandPointedToStructures,
555                                  bool ExpandStructures,
556                                  FieldDecl *Field,
557                                  bool OutermostType = false,
558                                  bool EncodingProperty = false) const;
559
560};
561
562}  // end namespace clang
563
564// operator new and delete aren't allowed inside namespaces.
565// The throw specifications are mandated by the standard.
566/// @brief Placement new for using the ASTContext's allocator.
567///
568/// This placement form of operator new uses the ASTContext's allocator for
569/// obtaining memory. It is a non-throwing new, which means that it returns
570/// null on error. (If that is what the allocator does. The current does, so if
571/// this ever changes, this operator will have to be changed, too.)
572/// Usage looks like this (assuming there's an ASTContext 'Context' in scope):
573/// @code
574/// // Default alignment (16)
575/// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
576/// // Specific alignment
577/// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
578/// @endcode
579/// Please note that you cannot use delete on the pointer; it must be
580/// deallocated using an explicit destructor call followed by
581/// @c Context.getAllocator().Deallocate(Ptr)
582///
583/// @param Bytes The number of bytes to allocate. Calculated by the compiler.
584/// @param C The ASTContext that provides the allocator.
585/// @param Alignment The alignment of the allocated memory (if the allocator
586///                  supports it, which the current one doesn't).
587/// @return The allocated memory. Could be NULL.
588inline void *operator new(size_t Bytes, clang::ASTContext &C,
589                          size_t Alignment = 16) throw () {
590  return C.getAllocator().Allocate(Bytes, Alignment);
591}
592/// @brief Placement delete companion to the new above.
593///
594/// This operator is just a companion to the new above. There is no way of
595/// invoking it directly; see the new operator for more details. This operator
596/// is called implicitly by the compiler if a placement new expression using
597/// the ASTContext throws in the object constructor.
598inline void operator delete(void *Ptr, clang::ASTContext &C)
599              throw () {
600  C.getAllocator().Deallocate(Ptr);
601}
602
603#endif
604