ASTContext.h revision 7da97d0f31e1ec16998d3de2cfd2e88fe3736673
11e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)//===--- ASTContext.h - Context to hold long-lived AST nodes ----*- C++ -*-===//
21e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)//
31e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)//                     The LLVM Compiler Infrastructure
41e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)//
51e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// This file is distributed under the University of Illinois Open Source
61e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// License. See LICENSE.TXT for details.
7116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch//
81e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)//===----------------------------------------------------------------------===//
91e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)//
101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)//  This file defines the ASTContext interface.
111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)//
121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)//===----------------------------------------------------------------------===//
131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#ifndef LLVM_CLANG_AST_ASTCONTEXT_H
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define LLVM_CLANG_AST_ASTCONTEXT_H
161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
176e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "clang/Basic/IdentifierTable.h"
186e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "clang/Basic/LangOptions.h"
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "clang/AST/Attr.h"
20f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "clang/AST/Builtins.h"
211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "clang/AST/Decl.h"
221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "clang/AST/NestedNameSpecifier.h"
231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "clang/AST/TemplateName.h"
241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "clang/AST/Type.h"
251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "clang/Basic/SourceLocation.h"
261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "llvm/ADT/DenseMap.h"
271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "llvm/ADT/FoldingSet.h"
281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "llvm/ADT/OwningPtr.h"
290f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "llvm/Support/Allocator.h"
300f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include <vector>
310f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)namespace llvm {
331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  struct fltSemantics;
341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
35116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
36116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace clang {
37116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  class FileManager;
38116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  class ASTRecordLayout;
391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  class Expr;
401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  class ExternalASTSource;
411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  class IdentifierTable;
421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  class SelectorTable;
431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  class SourceManager;
441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  class TargetInfo;
451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Decls
46116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  class Decl;
47116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  class ObjCPropertyDecl;
48cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  class RecordDecl;
491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  class TagDecl;
501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  class TranslationUnitDecl;
511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  class TypeDecl;
521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  class TypedefDecl;
53f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  class TemplateTypeParmDecl;
541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  class FieldDecl;
55cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  class ObjCIvarRefExpr;
56f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  class ObjCIvarDecl;
571e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
585f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)/// ASTContext - This class holds long-lived AST nodes (such as types and
595f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)/// decls) that can be referred to throughout the semantic analysis of a file.
605f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)class ASTContext {
615f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  std::vector<Type*> Types;
625f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  llvm::FoldingSet<ExtQualType> ExtQualTypes;
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  llvm::FoldingSet<ComplexType> ComplexTypes;
640f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  llvm::FoldingSet<PointerType> PointerTypes;
650f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  llvm::FoldingSet<BlockPointerType> BlockPointerTypes;
661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  llvm::FoldingSet<LValueReferenceType> LValueReferenceTypes;
671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  llvm::FoldingSet<RValueReferenceType> RValueReferenceTypes;
681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  llvm::FoldingSet<MemberPointerType> MemberPointerTypes;
691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  llvm::FoldingSet<ConstantArrayType> ConstantArrayTypes;
701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  llvm::FoldingSet<IncompleteArrayType> IncompleteArrayTypes;
711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  std::vector<VariableArrayType*> VariableArrayTypes;
72116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  std::vector<DependentSizedArrayType*> DependentSizedArrayTypes;
7346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  llvm::FoldingSet<VectorType> VectorTypes;
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  llvm::FoldingSet<FunctionNoProtoType> FunctionNoProtoTypes;
7546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  llvm::FoldingSet<FunctionProtoType> FunctionProtoTypes;
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  llvm::FoldingSet<TemplateTypeParmType> TemplateTypeParmTypes;
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  llvm::FoldingSet<TemplateSpecializationType> TemplateSpecializationTypes;
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  llvm::FoldingSet<QualifiedNameType> QualifiedNameTypes;
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  llvm::FoldingSet<TypenameType> TypenameTypes;
8046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  llvm::FoldingSet<ObjCQualifiedInterfaceType> ObjCQualifiedInterfaceTypes;
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  llvm::FoldingSet<ObjCQualifiedIdType> ObjCQualifiedIdTypes;
821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
83116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  llvm::FoldingSet<QualifiedTemplateName> QualifiedTemplateNames;
84116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  llvm::FoldingSet<DependentTemplateName> DependentTemplateNames;
85116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
86116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// \brief The set of nested name specifiers.
87116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ///
881e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  /// This set is managed by the NestedNameSpecifier class.
891e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  llvm::FoldingSet<NestedNameSpecifier> NestedNameSpecifiers;
901e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  NestedNameSpecifier *GlobalNestedNameSpecifier;
911e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  friend class NestedNameSpecifier;
921e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
931e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  /// ASTRecordLayouts - A cache mapping from RecordDecls to ASTRecordLayouts.
941e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ///  This is lazily created.  This is intentionally not serialized.
95  llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*> ASTRecordLayouts;
96  llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*> ObjCLayouts;
97
98  llvm::DenseMap<unsigned, FixedWidthIntType*> SignedFixedWidthIntTypes;
99  llvm::DenseMap<unsigned, FixedWidthIntType*> UnsignedFixedWidthIntTypes;
100
101  /// BuiltinVaListType - built-in va list type.
102  /// This is initially null and set by Sema::LazilyCreateBuiltin when
103  /// a builtin that takes a valist is encountered.
104  QualType BuiltinVaListType;
105
106  /// ObjCIdType - a pseudo built-in typedef type (set by Sema).
107  QualType ObjCIdType;
108  const RecordType *IdStructType;
109
110  /// ObjCSelType - another pseudo built-in typedef type (set by Sema).
111  QualType ObjCSelType;
112  const RecordType *SelStructType;
113
114  /// ObjCProtoType - another pseudo built-in typedef type (set by Sema).
115  QualType ObjCProtoType;
116  const RecordType *ProtoStructType;
117
118  /// ObjCClassType - another pseudo built-in typedef type (set by Sema).
119  QualType ObjCClassType;
120  const RecordType *ClassStructType;
121
122  QualType ObjCConstantStringType;
123  RecordDecl *CFConstantStringTypeDecl;
124
125  RecordDecl *ObjCFastEnumerationStateTypeDecl;
126
127  TranslationUnitDecl *TUDecl;
128
129  /// SourceMgr - The associated SourceManager object.
130  SourceManager &SourceMgr;
131
132  /// LangOpts - The language options used to create the AST associated with
133  ///  this ASTContext object.
134  LangOptions LangOpts;
135
136  /// MallocAlloc/BumpAlloc - The allocator objects used to create AST objects.
137  bool FreeMemory;
138  llvm::MallocAllocator MallocAlloc;
139  llvm::BumpPtrAllocator BumpAlloc;
140public:
141  TargetInfo &Target;
142  IdentifierTable &Idents;
143  SelectorTable &Selectors;
144  DeclarationNameTable DeclarationNames;
145  llvm::OwningPtr<ExternalASTSource> ExternalSource;
146
147  SourceManager& getSourceManager() { return SourceMgr; }
148  const SourceManager& getSourceManager() const { return SourceMgr; }
149  void *Allocate(unsigned Size, unsigned Align = 8) {
150    return FreeMemory ? MallocAlloc.Allocate(Size, Align) :
151                        BumpAlloc.Allocate(Size, Align);
152  }
153  void Deallocate(void *Ptr) {
154    if (FreeMemory)
155      MallocAlloc.Deallocate(Ptr);
156  }
157  const LangOptions& getLangOptions() const { return LangOpts; }
158
159  FullSourceLoc getFullLoc(SourceLocation Loc) const {
160    return FullSourceLoc(Loc,SourceMgr);
161  }
162
163  TranslationUnitDecl *getTranslationUnitDecl() const { return TUDecl; }
164
165  Builtin::Context BuiltinInfo;
166
167  // Builtin Types.
168  QualType VoidTy;
169  QualType BoolTy;
170  QualType CharTy;
171  QualType WCharTy; // [C++ 3.9.1p5], integer type in C99.
172  QualType SignedCharTy, ShortTy, IntTy, LongTy, LongLongTy, Int128Ty;
173  QualType UnsignedCharTy, UnsignedShortTy, UnsignedIntTy, UnsignedLongTy;
174  QualType UnsignedLongLongTy, UnsignedInt128Ty;
175  QualType FloatTy, DoubleTy, LongDoubleTy;
176  QualType FloatComplexTy, DoubleComplexTy, LongDoubleComplexTy;
177  QualType VoidPtrTy, NullPtrTy;
178  QualType OverloadTy;
179  QualType DependentTy;
180
181  ASTContext(const LangOptions& LOpts, SourceManager &SM, TargetInfo &t,
182             IdentifierTable &idents, SelectorTable &sels,
183             bool FreeMemory = true, unsigned size_reserve=0,
184             bool InitializeBuiltins = true);
185
186  ~ASTContext();
187
188  /// \brief Initialize builtins.
189  ///
190  /// Typically, this routine will be called automatically by the
191  /// constructor. However, in certain cases (e.g., when there is a
192  /// PCH file to be loaded), the constructor does not perform
193  /// initialization for builtins. This routine can be called to
194  /// perform the initialization.
195  void InitializeBuiltins(IdentifierTable &idents);
196
197  /// \brief Attach an external AST source to the AST context.
198  ///
199  /// The external AST source provides the ability to load parts of
200  /// the abstract syntax tree as needed from some external storage,
201  /// e.g., a precompiled header.
202  void setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source);
203
204  /// \brief Retrieve a pointer to the external AST source associated
205  /// with this AST context, if any.
206  ExternalASTSource *getExternalSource() const { return ExternalSource.get(); }
207
208  void PrintStats() const;
209  const std::vector<Type*>& getTypes() const { return Types; }
210
211  //===--------------------------------------------------------------------===//
212  //                           Type Constructors
213  //===--------------------------------------------------------------------===//
214
215  /// getAddSpaceQualType - Return the uniqued reference to the type for an
216  /// address space qualified type with the specified type and address space.
217  /// The resulting type has a union of the qualifiers from T and the address
218  /// space. If T already has an address space specifier, it is silently
219  /// replaced.
220  QualType getAddrSpaceQualType(QualType T, unsigned AddressSpace);
221
222  /// getObjCGCQualType - Returns the uniqued reference to the type for an
223  /// objc gc qualified type. The retulting type has a union of the qualifiers
224  /// from T and the gc attribute.
225  QualType getObjCGCQualType(QualType T, QualType::GCAttrTypes gcAttr);
226
227  /// getComplexType - Return the uniqued reference to the type for a complex
228  /// number with the specified element type.
229  QualType getComplexType(QualType T);
230
231  /// getPointerType - Return the uniqued reference to the type for a pointer to
232  /// the specified type.
233  QualType getPointerType(QualType T);
234
235  /// getBlockPointerType - Return the uniqued reference to the type for a block
236  /// of the specified type.
237  QualType getBlockPointerType(QualType T);
238
239  /// getLValueReferenceType - Return the uniqued reference to the type for an
240  /// lvalue reference to the specified type.
241  QualType getLValueReferenceType(QualType T);
242
243  /// getRValueReferenceType - Return the uniqued reference to the type for an
244  /// rvalue reference to the specified type.
245  QualType getRValueReferenceType(QualType T);
246
247  /// getMemberPointerType - Return the uniqued reference to the type for a
248  /// member pointer to the specified type in the specified class. The class
249  /// is a Type because it could be a dependent name.
250  QualType getMemberPointerType(QualType T, const Type *Cls);
251
252  /// getVariableArrayType - Returns a non-unique reference to the type for a
253  /// variable array of the specified element type.
254  QualType getVariableArrayType(QualType EltTy, Expr *NumElts,
255                                ArrayType::ArraySizeModifier ASM,
256                                unsigned EltTypeQuals);
257
258  /// getDependentSizedArrayType - Returns a non-unique reference to
259  /// the type for a dependently-sized array of the specified element
260  /// type. FIXME: We will need these to be uniqued, or at least
261  /// comparable, at some point.
262  QualType getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
263                                      ArrayType::ArraySizeModifier ASM,
264                                      unsigned EltTypeQuals);
265
266  /// getIncompleteArrayType - Returns a unique reference to the type for a
267  /// incomplete array of the specified element type.
268  QualType getIncompleteArrayType(QualType EltTy,
269                                  ArrayType::ArraySizeModifier ASM,
270                                  unsigned EltTypeQuals);
271
272  /// getConstantArrayType - Return the unique reference to the type for a
273  /// constant array of the specified element type.
274  QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize,
275                                ArrayType::ArraySizeModifier ASM,
276                                unsigned EltTypeQuals);
277
278  /// getVectorType - Return the unique reference to a vector type of
279  /// the specified element type and size. VectorType must be a built-in type.
280  QualType getVectorType(QualType VectorType, unsigned NumElts);
281
282  /// getExtVectorType - Return the unique reference to an extended vector type
283  /// of the specified element type and size.  VectorType must be a built-in
284  /// type.
285  QualType getExtVectorType(QualType VectorType, unsigned NumElts);
286
287  /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
288  ///
289  QualType getFunctionNoProtoType(QualType ResultTy);
290
291  /// getFunctionType - Return a normal function type with a typed argument
292  /// list.  isVariadic indicates whether the argument list includes '...'.
293  QualType getFunctionType(QualType ResultTy, const QualType *ArgArray,
294                           unsigned NumArgs, bool isVariadic,
295                           unsigned TypeQuals);
296
297  /// getTypeDeclType - Return the unique reference to the type for
298  /// the specified type declaration.
299  QualType getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl=0);
300
301  /// getTypedefType - Return the unique reference to the type for the
302  /// specified typename decl.
303  QualType getTypedefType(TypedefDecl *Decl);
304  QualType getObjCInterfaceType(const ObjCInterfaceDecl *Decl);
305
306  QualType getTemplateTypeParmType(unsigned Depth, unsigned Index,
307                                   IdentifierInfo *Name = 0);
308
309  QualType getTemplateSpecializationType(TemplateName T,
310                                         const TemplateArgument *Args,
311                                         unsigned NumArgs,
312                                         QualType Canon = QualType());
313
314  QualType getQualifiedNameType(NestedNameSpecifier *NNS,
315                                QualType NamedType);
316  QualType getTypenameType(NestedNameSpecifier *NNS,
317                           const IdentifierInfo *Name,
318                           QualType Canon = QualType());
319  QualType getTypenameType(NestedNameSpecifier *NNS,
320                           const TemplateSpecializationType *TemplateId,
321                           QualType Canon = QualType());
322
323  /// getObjCQualifiedInterfaceType - Return a
324  /// ObjCQualifiedInterfaceType type for the given interface decl and
325  /// the conforming protocol list.
326  QualType getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
327                                         ObjCProtocolDecl **ProtocolList,
328                                         unsigned NumProtocols);
329
330  /// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for a
331  /// given 'id' and conforming protocol list.
332  QualType getObjCQualifiedIdType(ObjCProtocolDecl **ProtocolList,
333                                  unsigned NumProtocols);
334
335
336  /// getTypeOfType - GCC extension.
337  QualType getTypeOfExprType(Expr *e);
338  QualType getTypeOfType(QualType t);
339
340  /// getTagDeclType - Return the unique reference to the type for the
341  /// specified TagDecl (struct/union/class/enum) decl.
342  QualType getTagDeclType(TagDecl *Decl);
343
344  /// getSizeType - Return the unique type for "size_t" (C99 7.17), defined
345  /// in <stddef.h>. The sizeof operator requires this (C99 6.5.3.4p4).
346  QualType getSizeType() const;
347
348  /// getWCharType - In C++, this returns the unique wchar_t type.  In C99, this
349  /// returns a type compatible with the type defined in <stddef.h> as defined
350  /// by the target.
351  QualType getWCharType() const { return WCharTy; }
352
353  /// getSignedWCharType - Return the type of "signed wchar_t".
354  /// Used when in C++, as a GCC extension.
355  QualType getSignedWCharType() const;
356
357  /// getUnsignedWCharType - Return the type of "unsigned wchar_t".
358  /// Used when in C++, as a GCC extension.
359  QualType getUnsignedWCharType() const;
360
361  /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
362  /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
363  QualType getPointerDiffType() const;
364
365  // getCFConstantStringType - Return the C structure type used to represent
366  // constant CFStrings.
367  QualType getCFConstantStringType();
368
369  /// Get the structure type used to representation CFStrings, or NULL
370  /// if it hasn't yet been built.
371  QualType getRawCFConstantStringType() {
372    if (CFConstantStringTypeDecl)
373      return getTagDeclType(CFConstantStringTypeDecl);
374    return QualType();
375  }
376  void setCFConstantStringType(QualType T);
377
378  // This setter/getter represents the ObjC type for an NSConstantString.
379  void setObjCConstantStringInterface(ObjCInterfaceDecl *Decl);
380  QualType getObjCConstantStringInterface() const {
381    return ObjCConstantStringType;
382  }
383
384  //// This gets the struct used to keep track of fast enumerations.
385  QualType getObjCFastEnumerationStateType();
386
387  /// Get the ObjCFastEnumerationState type, or NULL if it hasn't yet
388  /// been built.
389  QualType getRawObjCFastEnumerationStateType() {
390    if (ObjCFastEnumerationStateTypeDecl)
391      return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
392    return QualType();
393  }
394
395  void setObjCFastEnumerationStateType(QualType T);
396
397  /// getObjCEncodingForType - Emit the ObjC type encoding for the
398  /// given type into \arg S. If \arg NameFields is specified then
399  /// record field names are also encoded.
400  void getObjCEncodingForType(QualType t, std::string &S,
401                              const FieldDecl *Field=0);
402
403  void getLegacyIntegralTypeEncoding(QualType &t) const;
404
405  // Put the string version of type qualifiers into S.
406  void getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
407                                       std::string &S) const;
408
409  /// getObjCEncodingForMethodDecl - Return the encoded type for this method
410  /// declaration.
411  void getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, std::string &S);
412
413  /// getObjCEncodingForPropertyDecl - Return the encoded type for
414  /// this method declaration. If non-NULL, Container must be either
415  /// an ObjCCategoryImplDecl or ObjCImplementationDecl; it should
416  /// only be NULL when getting encodings for protocol properties.
417  void getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
418                                      const Decl *Container,
419                                      std::string &S);
420
421  /// getObjCEncodingTypeSize returns size of type for objective-c encoding
422  /// purpose.
423  int getObjCEncodingTypeSize(QualType t);
424
425  /// This setter/getter represents the ObjC 'id' type. It is setup lazily, by
426  /// Sema.  id is always a (typedef for a) pointer type, a pointer to a struct.
427  QualType getObjCIdType() const { return ObjCIdType; }
428  void setObjCIdType(QualType T);
429
430  void setObjCSelType(QualType T);
431  QualType getObjCSelType() const { return ObjCSelType; }
432
433  void setObjCProtoType(QualType QT);
434  QualType getObjCProtoType() const { return ObjCProtoType; }
435
436  /// This setter/getter repreents the ObjC 'Class' type. It is setup lazily, by
437  /// Sema.  'Class' is always a (typedef for a) pointer type, a pointer to a
438  /// struct.
439  QualType getObjCClassType() const { return ObjCClassType; }
440  void setObjCClassType(QualType T);
441
442  void setBuiltinVaListType(QualType T);
443  QualType getBuiltinVaListType() const { return BuiltinVaListType; }
444
445  QualType getFixedWidthIntType(unsigned Width, bool Signed);
446
447  TemplateName getQualifiedTemplateName(NestedNameSpecifier *NNS,
448                                        bool TemplateKeyword,
449                                        TemplateDecl *Template);
450
451  TemplateName getDependentTemplateName(NestedNameSpecifier *NNS,
452                                        const IdentifierInfo *Name);
453
454private:
455  QualType getFromTargetType(unsigned Type) const;
456
457  //===--------------------------------------------------------------------===//
458  //                         Type Predicates.
459  //===--------------------------------------------------------------------===//
460
461public:
462  /// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
463  /// to an object type.  This includes "id" and "Class" (two 'special' pointers
464  /// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
465  /// ID type).
466  bool isObjCObjectPointerType(QualType Ty) const;
467
468  /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
469  /// garbage collection attribute.
470  ///
471  QualType::GCAttrTypes getObjCGCAttrKind(const QualType &Ty) const;
472
473  /// isObjCNSObjectType - Return true if this is an NSObject object with
474  /// its NSObject attribute set.
475  bool isObjCNSObjectType(QualType Ty) const;
476
477  //===--------------------------------------------------------------------===//
478  //                         Type Sizing and Analysis
479  //===--------------------------------------------------------------------===//
480
481  /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
482  /// scalar floating point type.
483  const llvm::fltSemantics &getFloatTypeSemantics(QualType T) const;
484
485  /// getTypeInfo - Get the size and alignment of the specified complete type in
486  /// bits.
487  std::pair<uint64_t, unsigned> getTypeInfo(const Type *T);
488  std::pair<uint64_t, unsigned> getTypeInfo(QualType T) {
489    return getTypeInfo(T.getTypePtr());
490  }
491
492  /// getTypeSize - Return the size of the specified type, in bits.  This method
493  /// does not work on incomplete types.
494  uint64_t getTypeSize(QualType T) {
495    return getTypeInfo(T).first;
496  }
497  uint64_t getTypeSize(const Type *T) {
498    return getTypeInfo(T).first;
499  }
500
501  /// getTypeAlign - Return the ABI-specified alignment of a type, in bits.
502  /// This method does not work on incomplete types.
503  unsigned getTypeAlign(QualType T) {
504    return getTypeInfo(T).second;
505  }
506  unsigned getTypeAlign(const Type *T) {
507    return getTypeInfo(T).second;
508  }
509
510  /// getPreferredTypeAlign - Return the "preferred" alignment of the specified
511  /// type for the current target in bits.  This can be different than the ABI
512  /// alignment in cases where it is beneficial for performance to overalign
513  /// a data type.
514  unsigned getPreferredTypeAlign(const Type *T);
515
516  /// getDeclAlignInBytes - Return the alignment of the specified decl
517  /// that should be returned by __alignof().  Note that bitfields do
518  /// not have a valid alignment, so this method will assert on them.
519  unsigned getDeclAlignInBytes(const Decl *D);
520
521  /// getASTRecordLayout - Get or compute information about the layout of the
522  /// specified record (struct/union/class), which indicates its size and field
523  /// position information.
524  const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D);
525
526  /// getASTObjCInterfaceLayout - Get or compute information about the
527  /// layout of the specified Objective-C interface.
528  const ASTRecordLayout &getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D);
529
530  /// getASTObjCImplementationLayout - Get or compute information about
531  /// the layout of the specified Objective-C implementation. This may
532  /// differ from the interface if synthesized ivars are present.
533  const ASTRecordLayout &
534  getASTObjCImplementationLayout(const ObjCImplementationDecl *D);
535
536  void CollectObjCIvars(const ObjCInterfaceDecl *OI,
537                        llvm::SmallVectorImpl<FieldDecl*> &Fields);
538
539  //===--------------------------------------------------------------------===//
540  //                            Type Operators
541  //===--------------------------------------------------------------------===//
542
543  /// getCanonicalType - Return the canonical (structural) type corresponding to
544  /// the specified potentially non-canonical type.  The non-canonical version
545  /// of a type may have many "decorated" versions of types.  Decorators can
546  /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
547  /// to be free of any of these, allowing two canonical types to be compared
548  /// for exact equality with a simple pointer comparison.
549  QualType getCanonicalType(QualType T);
550  const Type *getCanonicalType(const Type *T) {
551    return T->getCanonicalTypeInternal().getTypePtr();
552  }
553
554  /// \brief Determine whether the given types are equivalent.
555  bool hasSameType(QualType T1, QualType T2) {
556    return getCanonicalType(T1) == getCanonicalType(T2);
557  }
558
559  /// \brief Determine whether the given types are equivalent after
560  /// cvr-qualifiers have been removed.
561  bool hasSameUnqualifiedType(QualType T1, QualType T2) {
562    T1 = getCanonicalType(T1);
563    T2 = getCanonicalType(T2);
564    return T1.getUnqualifiedType() == T2.getUnqualifiedType();
565  }
566
567  /// \brief Retrieves the "canonical" declaration of the given declaration.
568  Decl *getCanonicalDecl(Decl *D);
569
570  /// \brief Retrieves the "canonical" declaration of the given tag
571  /// declaration.
572  ///
573  /// The canonical declaration for the given tag declaration is
574  /// either the definition of the tag (if it is a complete type) or
575  /// the first declaration of that tag.
576  TagDecl *getCanonicalDecl(TagDecl *Tag) {
577    return cast<TagDecl>(getCanonicalDecl((Decl *)Tag));
578  }
579
580  /// \brief Retrieves the "canonical" declaration of
581
582  /// \brief Retrieves the "canonical" nested name specifier for a
583  /// given nested name specifier.
584  ///
585  /// The canonical nested name specifier is a nested name specifier
586  /// that uniquely identifies a type or namespace within the type
587  /// system. For example, given:
588  ///
589  /// \code
590  /// namespace N {
591  ///   struct S {
592  ///     template<typename T> struct X { typename T* type; };
593  ///   };
594  /// }
595  ///
596  /// template<typename T> struct Y {
597  ///   typename N::S::X<T>::type member;
598  /// };
599  /// \endcode
600  ///
601  /// Here, the nested-name-specifier for N::S::X<T>:: will be
602  /// S::X<template-param-0-0>, since 'S' and 'X' are uniquely defined
603  /// by declarations in the type system and the canonical type for
604  /// the template type parameter 'T' is template-param-0-0.
605  NestedNameSpecifier *
606  getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS);
607
608  /// \brief Retrieves the "canonical" template name that refers to a
609  /// given template.
610  ///
611  /// The canonical template name is the simplest expression that can
612  /// be used to refer to a given template. For most templates, this
613  /// expression is just the template declaration itself. For example,
614  /// the template std::vector can be referred to via a variety of
615  /// names---std::vector, ::std::vector, vector (if vector is in
616  /// scope), etc.---but all of these names map down to the same
617  /// TemplateDecl, which is used to form the canonical template name.
618  ///
619  /// Dependent template names are more interesting. Here, the
620  /// template name could be something like T::template apply or
621  /// std::allocator<T>::template rebind, where the nested name
622  /// specifier itself is dependent. In this case, the canonical
623  /// template name uses the shortest form of the dependent
624  /// nested-name-specifier, which itself contains all canonical
625  /// types, values, and templates.
626  TemplateName getCanonicalTemplateName(TemplateName Name);
627
628  /// Type Query functions.  If the type is an instance of the specified class,
629  /// return the Type pointer for the underlying maximally pretty type.  This
630  /// is a member of ASTContext because this may need to do some amount of
631  /// canonicalization, e.g. to move type qualifiers into the element type.
632  const ArrayType *getAsArrayType(QualType T);
633  const ConstantArrayType *getAsConstantArrayType(QualType T) {
634    return dyn_cast_or_null<ConstantArrayType>(getAsArrayType(T));
635  }
636  const VariableArrayType *getAsVariableArrayType(QualType T) {
637    return dyn_cast_or_null<VariableArrayType>(getAsArrayType(T));
638  }
639  const IncompleteArrayType *getAsIncompleteArrayType(QualType T) {
640    return dyn_cast_or_null<IncompleteArrayType>(getAsArrayType(T));
641  }
642
643  /// getBaseElementType - Returns the innermost element type of a variable
644  /// length array type. For example, will return "int" for int[m][n]
645  QualType getBaseElementType(const VariableArrayType *VAT);
646
647  /// getArrayDecayedType - Return the properly qualified result of decaying the
648  /// specified array type to a pointer.  This operation is non-trivial when
649  /// handling typedefs etc.  The canonical type of "T" must be an array type,
650  /// this returns a pointer to a properly qualified element of the array.
651  ///
652  /// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
653  QualType getArrayDecayedType(QualType T);
654
655  /// getIntegerTypeOrder - Returns the highest ranked integer type:
656  /// C99 6.3.1.8p1.  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
657  /// LHS < RHS, return -1.
658  int getIntegerTypeOrder(QualType LHS, QualType RHS);
659
660  /// getFloatingTypeOrder - Compare the rank of the two specified floating
661  /// point types, ignoring the domain of the type (i.e. 'double' ==
662  /// '_Complex double').  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
663  /// LHS < RHS, return -1.
664  int getFloatingTypeOrder(QualType LHS, QualType RHS);
665
666  /// getFloatingTypeOfSizeWithinDomain - Returns a real floating
667  /// point or a complex type (based on typeDomain/typeSize).
668  /// 'typeDomain' is a real floating point or complex type.
669  /// 'typeSize' is a real floating point or complex type.
670  QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize,
671                                             QualType typeDomain) const;
672
673private:
674  // Helper for integer ordering
675  unsigned getIntegerRank(Type* T);
676
677public:
678
679  //===--------------------------------------------------------------------===//
680  //                    Type Compatibility Predicates
681  //===--------------------------------------------------------------------===//
682
683  /// Compatibility predicates used to check assignment expressions.
684  bool typesAreCompatible(QualType, QualType); // C99 6.2.7p1
685  bool typesAreBlockCompatible(QualType lhs, QualType rhs);
686
687  bool isObjCIdType(QualType T) const {
688    return T == ObjCIdType;
689  }
690  bool isObjCIdStructType(QualType T) const {
691    if (!IdStructType) // ObjC isn't enabled
692      return false;
693    return T->getAsStructureType() == IdStructType;
694  }
695  bool isObjCClassType(QualType T) const {
696    return T == ObjCClassType;
697  }
698  bool isObjCClassStructType(QualType T) const {
699    if (!ClassStructType) // ObjC isn't enabled
700      return false;
701    return T->getAsStructureType() == ClassStructType;
702  }
703  bool isObjCSelType(QualType T) const {
704    assert(SelStructType && "isObjCSelType used before 'SEL' type is built");
705    return T->getAsStructureType() == SelStructType;
706  }
707
708  // Check the safety of assignment from LHS to RHS
709  bool canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
710                               const ObjCInterfaceType *RHS);
711  bool areComparableObjCPointerTypes(QualType LHS, QualType RHS);
712
713  // Functions for calculating composite types
714  QualType mergeTypes(QualType, QualType);
715  QualType mergeFunctionTypes(QualType, QualType);
716
717  //===--------------------------------------------------------------------===//
718  //                    Integer Predicates
719  //===--------------------------------------------------------------------===//
720
721  // The width of an integer, as defined in C99 6.2.6.2. This is the number
722  // of bits in an integer type excluding any padding bits.
723  unsigned getIntWidth(QualType T);
724
725  // Per C99 6.2.5p6, for every signed integer type, there is a corresponding
726  // unsigned integer type.  This method takes a signed type, and returns the
727  // corresponding unsigned integer type.
728  QualType getCorrespondingUnsignedType(QualType T);
729
730  //===--------------------------------------------------------------------===//
731  //                    Type Iterators.
732  //===--------------------------------------------------------------------===//
733
734  typedef std::vector<Type*>::iterator       type_iterator;
735  typedef std::vector<Type*>::const_iterator const_type_iterator;
736
737  type_iterator types_begin() { return Types.begin(); }
738  type_iterator types_end() { return Types.end(); }
739  const_type_iterator types_begin() const { return Types.begin(); }
740  const_type_iterator types_end() const { return Types.end(); }
741
742  //===--------------------------------------------------------------------===//
743  //                    Integer Values
744  //===--------------------------------------------------------------------===//
745
746  /// MakeIntValue - Make an APSInt of the appropriate width and
747  /// signedness for the given \arg Value and integer \arg Type.
748  llvm::APSInt MakeIntValue(uint64_t Value, QualType Type) {
749    llvm::APSInt Res(getIntWidth(Type), !Type->isSignedIntegerType());
750    Res = Value;
751    return Res;
752  }
753
754private:
755  ASTContext(const ASTContext&); // DO NOT IMPLEMENT
756  void operator=(const ASTContext&); // DO NOT IMPLEMENT
757
758  void InitBuiltinTypes();
759  void InitBuiltinType(QualType &R, BuiltinType::Kind K);
760
761  // Return the ObjC type encoding for a given type.
762  void getObjCEncodingForTypeImpl(QualType t, std::string &S,
763                                  bool ExpandPointedToStructures,
764                                  bool ExpandStructures,
765                                  const FieldDecl *Field,
766                                  bool OutermostType = false,
767                                  bool EncodingProperty = false);
768
769  const ASTRecordLayout &getObjCLayout(const ObjCInterfaceDecl *D,
770                                       const ObjCImplementationDecl *Impl);
771};
772
773}  // end namespace clang
774
775// operator new and delete aren't allowed inside namespaces.
776// The throw specifications are mandated by the standard.
777/// @brief Placement new for using the ASTContext's allocator.
778///
779/// This placement form of operator new uses the ASTContext's allocator for
780/// obtaining memory. It is a non-throwing new, which means that it returns
781/// null on error. (If that is what the allocator does. The current does, so if
782/// this ever changes, this operator will have to be changed, too.)
783/// Usage looks like this (assuming there's an ASTContext 'Context' in scope):
784/// @code
785/// // Default alignment (16)
786/// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
787/// // Specific alignment
788/// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
789/// @endcode
790/// Please note that you cannot use delete on the pointer; it must be
791/// deallocated using an explicit destructor call followed by
792/// @c Context.Deallocate(Ptr).
793///
794/// @param Bytes The number of bytes to allocate. Calculated by the compiler.
795/// @param C The ASTContext that provides the allocator.
796/// @param Alignment The alignment of the allocated memory (if the underlying
797///                  allocator supports it).
798/// @return The allocated memory. Could be NULL.
799inline void *operator new(size_t Bytes, clang::ASTContext &C,
800                          size_t Alignment) throw () {
801  return C.Allocate(Bytes, Alignment);
802}
803/// @brief Placement delete companion to the new above.
804///
805/// This operator is just a companion to the new above. There is no way of
806/// invoking it directly; see the new operator for more details. This operator
807/// is called implicitly by the compiler if a placement new expression using
808/// the ASTContext throws in the object constructor.
809inline void operator delete(void *Ptr, clang::ASTContext &C, size_t)
810              throw () {
811  C.Deallocate(Ptr);
812}
813
814/// This placement form of operator new[] uses the ASTContext's allocator for
815/// obtaining memory. It is a non-throwing new[], which means that it returns
816/// null on error.
817/// Usage looks like this (assuming there's an ASTContext 'Context' in scope):
818/// @code
819/// // Default alignment (16)
820/// char *data = new (Context) char[10];
821/// // Specific alignment
822/// char *data = new (Context, 8) char[10];
823/// @endcode
824/// Please note that you cannot use delete on the pointer; it must be
825/// deallocated using an explicit destructor call followed by
826/// @c Context.Deallocate(Ptr).
827///
828/// @param Bytes The number of bytes to allocate. Calculated by the compiler.
829/// @param C The ASTContext that provides the allocator.
830/// @param Alignment The alignment of the allocated memory (if the underlying
831///                  allocator supports it).
832/// @return The allocated memory. Could be NULL.
833inline void *operator new[](size_t Bytes, clang::ASTContext& C,
834                            size_t Alignment = 16) throw () {
835  return C.Allocate(Bytes, Alignment);
836}
837
838/// @brief Placement delete[] companion to the new[] above.
839///
840/// This operator is just a companion to the new[] above. There is no way of
841/// invoking it directly; see the new[] operator for more details. This operator
842/// is called implicitly by the compiler if a placement new[] expression using
843/// the ASTContext throws in the object constructor.
844inline void operator delete[](void *Ptr, clang::ASTContext &C) throw () {
845  C.Deallocate(Ptr);
846}
847
848#endif
849